72 flatten(typeMap, cstring::empty, type, type->annotations, vec, policy);
73 if (type->is<IR::Type_Struct>()) {
75 new IR::Type_Struct(type->srcInfo, type->name, IR::Annotations::empty, *vec);
76 }
else if (type->is<IR::Type_Header>()) {
78 new IR::Type_Header(type->srcInfo, type->name, IR::Annotations::empty, *vec);
80 BUG(
"Unexpected type %1%", type);
90 std::map<cstring, cstring> fieldNameRemap;
94 std::map<cstring, const IR::Type_StructLike *> structFieldMap;
102 const IR::Type *replacementType;
103 virtual void dbprint(std::ostream &out)
const { out << replacementType; }
110 annotations = annotations->where(
111 [](
const IR::Annotation *a) {
return a->name != IR::Annotation::nameAnnotation; });
112 if (
auto st = type->to<T>()) {
113 auto sannotations = st->annotations->where([policy](
const IR::Annotation *annot) {
114 if (!policy)
return false;
115 return policy->
keep(annot);
117 structFieldMap.emplace(prefix, st);
118 for (
auto f : st->fields) {
119 auto na =
new IR::Annotations();
120 na->append(sannotations);
121 na->append(annotations);
122 na->append(f->annotations);
123 auto ft = typeMap->getType(f,
true);
124 flatten(typeMap, prefix +
"." + f->name, ft, na, fields, policy);
128 cstring fieldName = prefix.replace(
'.',
'_') + std::to_string(fieldNameRemap.size());
129 fieldNameRemap.emplace(prefix, fieldName);
130 fields->push_back(
new IR::StructField(
IR::ID(fieldName), annotations, type->getP4Type()));
131 LOG3(
"Flatten: " << type <<
" | " << prefix);
138 const IR::StructExpression *
explode(
const IR::Expression *root,
cstring prefix) {
140 auto fieldType = ::P4::get(structFieldMap, prefix);
141 BUG_CHECK(fieldType,
"No field for %1%", prefix);
142 for (
auto f : fieldType->fields) {
143 cstring fieldName = prefix +
"." + f->name.name;
144 auto newFieldname = ::P4::get(fieldNameRemap, fieldName);
145 const IR::Expression *expr;
146 if (!newFieldname.isNullOrEmpty()) {
147 expr =
new IR::Member(root, newFieldname);
149 expr =
explode(root, fieldName);
151 vec->push_back(
new IR::NamedExpression(f->name, expr));
153 auto type = fieldType->getP4Type()->template to<IR::Type_Name>();
154 return new IR::StructExpression(root->srcInfo, type, type, *vec);
234 std::map<const IR::Parameter *, StructTypeReplacement<IR::Type_Struct> *> toReplace;
239 setName(
"ReplaceStructs");
242 const IR::Node *preorder(IR::P4Program *program)
override;
243 const IR::Node *postorder(IR::Member *expression)
override;
244 const IR::Node *preorder(IR::P4Parser *parser)
override;
245 const IR::Node *preorder(IR::P4Control *control)
override;
246 const IR::Node *postorder(IR::Type_Struct *type)
override;
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24