8#ifndef MIDEND_NESTEDSTRUCTS_H_
9#define MIDEND_NESTEDSTRUCTS_H_
11#include "frontends/common/resolveReferences/resolveReferences.h"
12#include "frontends/p4/typeChecking/typeChecker.h"
17class ComplexValues final {
24 virtual const IR::Expression *convertToExpression() = 0;
26 virtual void dbprint(std::ostream &out)
const = 0;
31 explicit FinalName(
cstring name) : newName(name) {}
32 const IR::Expression *convertToExpression()
override {
33 return new IR::PathExpression(
IR::ID(newName));
36 void dbprint(std::ostream &out)
const override { out << newName << Log::endl; }
42 explicit FieldsMap(
const IR::Type *type) : type(type) {
44 BUG_CHECK(type->is<IR::Type_Struct>(),
"%1%: expected a struct", type);
46 const IR::Expression *convertToExpression()
override {
48 for (
auto m : members) {
49 auto e = m.second->convertToExpression();
50 vec.push_back(
new IR::NamedExpression(m.first, e));
52 return new IR::StructExpression(type->getP4Type(), vec);
54 Component *getComponent(
cstring name)
override { return ::P4::get(members, name); }
55 void dbprint(std::ostream &out)
const override {
57 for (
auto m : members) out << m.first <<
"=>" << m.second;
62 std::map<const IR::Declaration_Variable *, Component *> values;
63 std::map<const IR::Expression *, Component *> translation;
75 void explode(std::string_view
prefix,
const IR::Type_Struct *type, FieldsMap *map,
78 auto dv = decl->
to<IR::Declaration_Variable>();
79 if (dv ==
nullptr)
return nullptr;
80 return ::P4::get(values, dv);
82 Component *getTranslation(
const IR::Expression *expression)
const {
83 LOG2(
"Check translation " << dbp(expression));
84 return ::P4::get(translation, expression);
86 void setTranslation(
const IR::Expression *expression,
Component *comp) {
87 translation.emplace(expression, comp);
88 LOG2(
"Translated " << dbp(expression) <<
" to " << comp);
121class RemoveNestedStructs final :
public Transform,
public ResolutionContext {
126 explicit RemoveNestedStructs(
TypeMap *typeMap) : values(typeMap, nameGen) {
127 setName(
"RemoveNestedStructs");
130 auto rv = Transform::init_apply(node);
131 node->apply(nameGen);
145class NestedStructs final :
public PassManager {
148 if (!typeChecking) typeChecking =
new TypeChecking(
nullptr, typeMap);
149 passes.push_back(typeChecking);
152 setName(
"NestedStructs");
Definition typeChecker.h:32
Definition nestedStructs.h:17
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:28
bool isNestedStruct(const IR::Type *type) const
Helper function that test if a struct is nested.
Definition nestedStructs.cpp:13
Definition stringify.h:33
The Declaration interface, representing objects with names.
Definition declaration.h:17
Definition indexed_vector.h:31
Definition ir/vector.h:59
Definition referenceMap.h:36
Definition referenceMap.h:29
Definition nestedStructs.h:121
const IR::Node * postorder(IR::Declaration_Variable *decl) override
rewrite nested structs to non-nested structs
Definition nestedStructs.cpp:49
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:13
Definition nestedStructs.h:23
T * to() noexcept
Definition rtti.h:226
Definition register_reference.h:28