73 flatten(typeMap, cstring::empty, type, type->annotations, vec, policy);
74 if (type->is<IR::Type_Struct>()) {
75 replacementType =
new IR::Type_Struct(type->srcInfo, type->name, std::move(vec));
76 }
else if (type->is<IR::Type_Header>()) {
77 replacementType =
new IR::Type_Header(type->srcInfo, type->name, std::move(vec));
79 BUG(
"Unexpected type %1%", type);
89 std::map<cstring, cstring> fieldNameRemap;
93 std::map<cstring, const IR::Type_StructLike *> structFieldMap;
101 const IR::Type *replacementType;
102 virtual void dbprint(std::ostream &out)
const { out << replacementType; }
109 auto ann = IR::Annotations::withoutNameAnnotation(annotations);
110 if (
auto st = type->to<T>()) {
112 st->annotations.where([policy](
const IR::Annotation *annot) {
113 if (!policy)
return false;
114 return policy->
keep(annot);
116 structFieldMap.emplace(prefix, st);
117 for (
auto f : st->fields) {
120 fann.append(f->annotations);
121 auto ft = typeMap->getType(f,
true);
122 flatten(typeMap, prefix +
"." + f->name, ft, fann, fields, policy);
126 cstring fieldName = prefix.replace(
'.',
'_') + std::to_string(fieldNameRemap.size());
127 fieldNameRemap.emplace(prefix, fieldName);
128 fields.push_back(
new IR::StructField(
IR::ID(fieldName), ann, type->getP4Type()));
129 LOG3(
"Flatten: " << type <<
" | " << prefix);
136 const IR::StructExpression *
explode(
const IR::Expression *root,
cstring prefix) {
138 auto fieldType = ::P4::get(structFieldMap, prefix);
139 BUG_CHECK(fieldType,
"No field for %1%", prefix);
140 for (
auto f : fieldType->fields) {
141 cstring fieldName = prefix +
"." + f->name.name;
142 auto newFieldname = ::P4::get(fieldNameRemap, fieldName);
143 const IR::Expression *expr;
144 if (!newFieldname.isNullOrEmpty()) {
145 expr =
new IR::Member(root, newFieldname);
147 expr =
explode(root, fieldName);
149 vec.push_back(
new IR::NamedExpression(f->name, expr));
151 auto type = fieldType->getP4Type()->template to<IR::Type_Name>();
152 return new IR::StructExpression(root->srcInfo, type, type, std::move(vec));
232 std::map<const IR::Parameter *, StructTypeReplacement<IR::Type_Struct> *> toReplace;
237 setName(
"ReplaceStructs");
240 const IR::Node *preorder(IR::P4Program *program)
override;
241 const IR::Node *postorder(IR::Member *expression)
override;
242 const IR::Node *preorder(IR::P4Parser *parser)
override;
243 const IR::Node *preorder(IR::P4Control *control)
override;
244 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