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/p4/typeChecking/typeChecker.h"
21#include "ir/ir.h"
22
23namespace P4 {
24
25class ComplexValues final {
26 public:
31 struct Component : public IHasDbPrint {
32 virtual const IR::Expression *convertToExpression() = 0;
33 virtual Component *getComponent(cstring name) = 0;
34 virtual void dbprint(std::ostream &out) const = 0;
35 };
36
37 struct FinalName : public Component {
38 cstring newName;
39 explicit FinalName(cstring name) : newName(name) {}
40 const IR::Expression *convertToExpression() override {
41 return new IR::PathExpression(IR::ID(newName));
42 }
43 Component *getComponent(cstring) override { return nullptr; }
44 void dbprint(std::ostream &out) const override { out << newName << Log::endl; }
45 };
46
47 struct FieldsMap : public Component {
49 const IR::Type *type;
50 explicit FieldsMap(const IR::Type *type) : type(type) {
51 CHECK_NULL(type);
52 BUG_CHECK(type->is<IR::Type_Struct>(), "%1%: expected a struct", type);
53 }
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));
59 }
60 return new IR::StructExpression(type->getP4Type(), vec);
61 }
62 Component *getComponent(cstring name) override { return ::P4::get(members, name); }
63 void dbprint(std::ostream &out) const override {
64 out << Log::indent;
65 for (auto m : members) out << m.first << "=>" << m.second;
66 out << Log::unindent;
67 }
68 };
69
70 std::map<const IR::Declaration_Variable *, Component *> values;
71 std::map<const IR::Expression *, Component *> translation;
72
73 ReferenceMap *refMap;
74 TypeMap *typeMap;
75
76 ComplexValues(ReferenceMap *refMap, TypeMap *typeMap) : refMap(refMap), typeMap(typeMap) {
77 CHECK_NULL(refMap);
78 CHECK_NULL(typeMap);
79 }
81 bool isNestedStruct(const IR::Type *type);
83 template <class T>
84 void explode(cstring prefix, const IR::Type_Struct *type, FieldsMap *map,
85 IR::Vector<T> *result);
86 Component *getTranslation(const IR::IDeclaration *decl) {
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) {
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 {
131 ComplexValues *values;
132
133 public:
134 explicit RemoveNestedStructs(ComplexValues *values) : values(values) {
135 CHECK_NULL(values);
136 setName("RemoveNestedStructs");
137 }
138
140 const IR::Node *postorder(IR::Declaration_Variable *decl) override;
142 const IR::Node *postorder(IR::Member *expression) override;
144 const IR::Node *postorder(IR::PathExpression *expression) override;
145 const IR::Node *postorder(IR::MethodCallExpression *expression) override;
146};
147
148class NestedStructs final : public PassManager {
149 public:
150 NestedStructs(ReferenceMap *refMap, TypeMap *typeMap, TypeChecking *typeChecking = nullptr) {
151 auto values = new ComplexValues(refMap, typeMap);
152 if (!typeChecking) typeChecking = new TypeChecking(refMap, typeMap);
153 passes.push_back(typeChecking);
154 passes.push_back(new RemoveNestedStructs(values));
155 passes.push_back(new ClearTypeMap(typeMap));
156 setName("NestedStructs");
157 }
158};
159
160} // namespace P4
161
162#endif /* MIDEND_NESTEDSTRUCTS_H_ */
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 node.h:52
Definition node.h:95
Definition vector.h:58
Definition nestedStructs.h:148
Definition pass_manager.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 visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
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:31
Definition nestedStructs.h:47
Definition nestedStructs.h:37
Definition id.h:28
T * to() noexcept
Definition rtti.h:226