64 P4::ConvertEnums::EnumMapping *enumMap;
67 const IR::ToplevelBlock *toplevel =
nullptr;
71 P4::ConvertEnums::EnumMapping *enumMap)
76 corelib(P4::P4CoreLibrary::instance()),
78 refMap->
setIsV1(options.isv1());
80 void serialize(std::ostream &out)
const { json->toplevel->serialize(out); }
81 virtual void convert(
const IR::ToplevelBlock *block) = 0;
131 const IR::Type_Struct *userMetaType;
135 bool renamed =
false;
140 : refMap(refMap), userMetaType(userMetaType), namePrefix(namePrefix) {
141 setName(
"RenameUserMetadata");
143 visitDagOnce =
false;
146 const IR::Node *postorder(IR::Type_Struct *type)
override {
148 auto orig = getOriginal<IR::Type_Struct>();
149 if (userMetaType->name != orig->name)
return type;
151 auto vec =
new IR::IndexedVector<IR::Node>();
152 LOG2(
"Creating clone of " << orig);
154 auto clone = type->clone();
155 clone->name = namePrefix;
156 vec->push_back(clone);
159 IR::IndexedVector<IR::StructField> fields;
160 for (
auto f : type->fields) {
161 auto anno = f->getAnnotation(IR::Annotation::nameAnnotation);
163 if (anno !=
nullptr) suffix = anno->getName();
164 if (suffix.startsWith(
".")) {
167 fields.push_back(f->clone());
171 if (!suffix.isNullOrEmpty())
172 suffix =
cstring(
".") + suffix;
174 suffix =
cstring(
".") + f->name;
175 cstring newName = namePrefix + suffix;
176 auto stringLit =
new IR::StringLiteral(newName);
177 LOG2(
"Renaming " << f <<
" to " << newName);
178 auto annos = f->annotations->addOrReplace(IR::Annotation::nameAnnotation, stringLit);
179 auto field =
new IR::StructField(f->srcInfo, f->name, annos, f->type);
180 fields.push_back(field);
184 new IR::Type_Struct(type->srcInfo, type->name, type->annotations, std::move(fields));
185 vec->push_back(annotated);
189 const IR::Node *preorder(IR::Type_Name *type)
override {
192 if (decl == userMetaType)
193 type->path =
new IR::Path(type->path->srcInfo, IR::ID(type->path->srcInfo, namePrefix));
194 LOG2(
"Replacing reference with " << type);
198 void end_apply(
const IR::Node *)
override {
199 BUG_CHECK(renamed,
"Could not identify user metadata type declaration %1%", userMetaType);