17#ifndef MIDEND_NESTEDSTRUCTS_H_
18#define MIDEND_NESTEDSTRUCTS_H_
20#include "frontends/p4/typeChecking/typeChecker.h"
25class ComplexValues final {
32 virtual const IR::Expression *convertToExpression() = 0;
34 virtual void dbprint(std::ostream &out)
const = 0;
39 explicit FinalName(
cstring name) : newName(name) {}
40 const IR::Expression *convertToExpression()
override {
41 return new IR::PathExpression(
IR::ID(newName));
44 void dbprint(std::ostream &out)
const override { out << newName << Log::endl; }
50 explicit FieldsMap(
const IR::Type *type) : type(type) {
52 BUG_CHECK(type->is<IR::Type_Struct>(),
"%1%: expected a struct", type);
54 const IR::Expression *convertToExpression()
override {
56 for (
auto m : members) {
57 auto e = m.second->convertToExpression();
58 vec.push_back(
new IR::NamedExpression(m.first, e));
60 return new IR::StructExpression(type->getP4Type(), vec);
62 Component *getComponent(
cstring name)
override { return ::P4::get(members, name); }
63 void dbprint(std::ostream &out)
const override {
65 for (
auto m : members) out << m.first <<
"=>" << m.second;
70 std::map<const IR::Declaration_Variable *, Component *> values;
71 std::map<const IR::Expression *, Component *> translation;
84 void explode(
cstring 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) {
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);
134 explicit RemoveNestedStructs(
ComplexValues *values) : values(values) {
136 setName(
"RemoveNestedStructs");
148class NestedStructs final :
public PassManager {
152 if (!typeChecking) typeChecking =
new TypeChecking(refMap, typeMap);
153 passes.push_back(typeChecking);
156 setName(
"NestedStructs");
Definition typeChecker.h:32
Definition nestedStructs.h:25
bool isNestedStruct(const IR::Type *type)
Helper function that test if a struct is nested.
Definition nestedStructs.cpp:9
void explode(cstring 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
Definition stringify.h:33
The Declaration interface, representing objects with names.
Definition declaration.h:26
Definition indexed_vector.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition nestedStructs.h:130
const IR::Node * postorder(IR::Declaration_Variable *decl) override
rewrite nested structs to non-nested structs
Definition nestedStructs.cpp:45
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:31
T * to() noexcept
Definition rtti.h:226