P4C
The P4 Compiler
Loading...
Searching...
No Matches
nestedStructs.h
1/*
2Copyright 2016 VMware, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef MIDEND_NESTEDSTRUCTS_H_
18#define MIDEND_NESTEDSTRUCTS_H_
19
20#include "frontends/common/resolveReferences/resolveReferences.h"
21#include "frontends/p4/typeChecking/typeChecker.h"
22#include "ir/ir.h"
23
24namespace P4 {
25
26class ComplexValues final {
27 public:
32 struct Component : public IHasDbPrint {
33 virtual const IR::Expression *convertToExpression() = 0;
34 virtual Component *getComponent(cstring name) = 0;
35 virtual void dbprint(std::ostream &out) const = 0;
36 };
37
38 struct FinalName : public Component {
39 cstring newName;
40 explicit FinalName(cstring name) : newName(name) {}
41 const IR::Expression *convertToExpression() override {
42 return new IR::PathExpression(IR::ID(newName));
43 }
44 Component *getComponent(cstring) override { return nullptr; }
45 void dbprint(std::ostream &out) const override { out << newName << Log::endl; }
46 };
47
48 struct FieldsMap : public Component {
50 const IR::Type *type;
51 explicit FieldsMap(const IR::Type *type) : type(type) {
52 CHECK_NULL(type);
53 BUG_CHECK(type->is<IR::Type_Struct>(), "%1%: expected a struct", type);
54 }
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));
60 }
61 return new IR::StructExpression(type->getP4Type(), vec);
62 }
63 Component *getComponent(cstring name) override { return ::P4::get(members, name); }
64 void dbprint(std::ostream &out) const override {
65 out << Log::indent;
66 for (auto m : members) out << m.first << "=>" << m.second;
67 out << Log::unindent;
68 }
69 };
70
71 std::map<const IR::Declaration_Variable *, Component *> values;
72 std::map<const IR::Expression *, Component *> translation;
73
74 TypeMap *typeMap;
75 NameGenerator &nameGen;
76
77 ComplexValues(TypeMap *typeMap, NameGenerator &nameGen) : typeMap(typeMap), nameGen(nameGen) {
78 CHECK_NULL(typeMap);
79 }
81 bool isNestedStruct(const IR::Type *type) const;
83 template <class T>
84 void explode(std::string_view prefix, const IR::Type_Struct *type, FieldsMap *map,
85 IR::Vector<T> *result);
86 Component *getTranslation(const IR::IDeclaration *decl) const {
87 auto dv = decl->to<IR::Declaration_Variable>();
88 if (dv == nullptr) return nullptr;
89 return ::P4::get(values, dv);
90 }
91 Component *getTranslation(const IR::Expression *expression) const {
92 LOG2("Check translation " << dbp(expression));
93 return ::P4::get(translation, expression);
94 }
95 void setTranslation(const IR::Expression *expression, Component *comp) {
96 translation.emplace(expression, comp);
97 LOG2("Translated " << dbp(expression) << " to " << comp);
98 }
99};
100
130class RemoveNestedStructs final : public Transform, public ResolutionContext {
131 ComplexValues values;
132 MinimalNameGenerator nameGen;
133
134 public:
135 explicit RemoveNestedStructs(TypeMap *typeMap) : values(typeMap, nameGen) {
136 setName("RemoveNestedStructs");
137 }
138 Visitor::profile_t init_apply(const IR::Node *node) override {
139 auto rv = Transform::init_apply(node);
140 node->apply(nameGen);
141
142 return rv;
143 }
144
146 const IR::Node *postorder(IR::Declaration_Variable *decl) override;
148 const IR::Node *postorder(IR::Member *expression) override;
150 const IR::Node *postorder(IR::PathExpression *expression) override;
151 const IR::Node *postorder(IR::MethodCallExpression *expression) override;
152};
153
154class NestedStructs final : public PassManager {
155 public:
156 explicit NestedStructs(TypeMap *typeMap, TypeChecking *typeChecking = nullptr) {
157 if (!typeChecking) typeChecking = new TypeChecking(nullptr, typeMap);
158 passes.push_back(typeChecking);
159 passes.push_back(new RemoveNestedStructs(typeMap));
160 passes.push_back(new ClearTypeMap(typeMap));
161 setName("NestedStructs");
162 }
163};
164
165} // namespace P4
166
167#endif /* MIDEND_NESTEDSTRUCTS_H_ */
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 node.h:52
Definition node.h:94
Definition vector.h:59
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 visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
Definition visitor.h:78
Definition cstring.h:85
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
Definition id.h:28
T * to() noexcept
Definition rtti.h:226