17#ifndef MIDEND_NESTEDSTRUCTS_H_
18#define MIDEND_NESTEDSTRUCTS_H_
20#include "frontends/common/resolveReferences/resolveReferences.h"
21#include "frontends/p4/typeChecking/typeChecker.h"
33 virtual const IR::Expression *convertToExpression() = 0;
35 virtual void dbprint(std::ostream &out)
const = 0;
41 const IR::Expression *convertToExpression()
override {
42 return new IR::PathExpression(
IR::ID(newName));
45 void dbprint(std::ostream &out)
const override { out << newName << Log::endl; }
51 explicit FieldsMap(
const IR::Type *type) : type(type) {
53 BUG_CHECK(type->is<IR::Type_Struct>(),
"%1%: expected a struct", type);
55 const IR::Expression *convertToExpression()
override {
57 for (
auto m : members) {
58 auto e = m.second->convertToExpression();
59 vec.push_back(
new IR::NamedExpression(m.first, e));
61 return new IR::StructExpression(type->getP4Type(), vec);
63 Component *getComponent(
cstring name)
override { return ::P4::get(members, name); }
64 void dbprint(std::ostream &out)
const override {
66 for (
auto m : members) out << m.first <<
"=>" << m.second;
71 std::map<const IR::Declaration_Variable *, Component *> values;
72 std::map<const IR::Expression *, Component *> translation;
84 void explode(std::string_view prefix,
const IR::Type_Struct *type, FieldsMap *map,
87 auto dv = decl->
to<IR::Declaration_Variable>();
88 if (dv ==
nullptr)
return nullptr;
89 return ::P4::get(values, dv);
91 Component *getTranslation(
const IR::Expression *expression)
const {
92 LOG2(
"Check translation " << dbp(expression));
93 return ::P4::get(translation, expression);
95 void setTranslation(
const IR::Expression *expression, Component *comp) {
96 translation.emplace(expression, comp);
97 LOG2(
"Translated " << dbp(expression) <<
" to " << comp);
136 setName(
"RemoveNestedStructs");
139 auto rv = Transform::init_apply(node);
140 node->apply(nameGen);
157 if (!typeChecking) typeChecking =
new TypeChecking(
nullptr, typeMap);
158 passes.push_back(typeChecking);
161 setName(
"NestedStructs");
Definition typeChecker.h:32
Definition nestedStructs.h:26
void explode(std::string_view prefix, const IR::Type_Struct *type, FieldsMap *map, IR::Vector< T > *result)
Flatten a nested struct to only contain field declaration or non-nested struct.
Definition nestedStructs.cpp:24
bool isNestedStruct(const IR::Type *type) const
Helper function that test if a struct is nested.
Definition nestedStructs.cpp:9
Definition stringify.h:33
The Declaration interface, representing objects with names.
Definition declaration.h:26
Definition referenceMap.h:36
Definition referenceMap.h:29
Definition nestedStructs.h:154
Definition ir/pass_manager.h:40
Definition nestedStructs.h:130
const IR::Node * postorder(IR::Declaration_Variable *decl) override
rewrite nested structs to non-nested structs
Definition nestedStructs.cpp:45
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Definition typeChecker.h:55
Definition ordered_map.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition nestedStructs.h:32
Definition nestedStructs.h:48
Definition nestedStructs.h:38
T * to() noexcept
Definition rtti.h:226