P4C
The P4 Compiler
Loading...
Searching...
No Matches
flattenHeaders.h
1/*
2 Copyright 2018 MNK Consulting, LLC.
3 http://mnkcg.com
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16*/
17
18#ifndef MIDEND_FLATTENHEADERS_H_
19#define MIDEND_FLATTENHEADERS_H_
20
21#include "flattenInterfaceStructs.h"
22#include "frontends/p4/typeChecking/typeChecker.h"
23#include "ir/ir.h"
24
25namespace P4 {
26
31 P4::TypeMap *typeMap;
34
35 public:
37 : typeMap(typeMap), policy(policy) {
38 setName("FindHeaderTypesToReplace");
39 CHECK_NULL(typeMap);
40 }
41 bool preorder(const IR::Type_Header *type) override;
42 void createReplacement(const IR::Type_Header *type, AnnotationSelectionPolicy *policy);
43 StructTypeReplacement<IR::Type_StructLike> *getReplacement(const cstring name) const {
44 return ::P4::get(replacement, name);
45 }
46 bool empty() const { return replacement.empty(); }
47};
48
122 P4::TypeMap *typeMap;
123 FindHeaderTypesToReplace *findHeaderTypesToReplace;
124
125 public:
126 explicit ReplaceHeaders(P4::TypeMap *typeMap,
127 FindHeaderTypesToReplace *findHeaderTypesToReplace)
128 : typeMap(typeMap), findHeaderTypesToReplace(findHeaderTypesToReplace) {
129 CHECK_NULL(typeMap);
130 CHECK_NULL(findHeaderTypesToReplace);
131 setName("ReplaceHeaders");
132 }
133
134 const IR::Node *preorder(IR::P4Program *program) override;
135 const IR::Node *postorder(IR::Member *expression) override;
136 const IR::Node *postorder(IR::Type_Header *type) override;
137};
138
139class FlattenHeaders final : public PassManager {
140 public:
141 FlattenHeaders(TypeMap *typeMap, AnnotationSelectionPolicy *policy = nullptr) {
142 auto findHeadersToReplace = new FindHeaderTypesToReplace(typeMap, policy);
143 passes.push_back(new TypeChecking(nullptr, typeMap));
144 passes.push_back(findHeadersToReplace);
145 passes.push_back(new ReplaceHeaders(typeMap, findHeadersToReplace));
146 passes.push_back(new ClearTypeMap(typeMap));
147 setName("FlattenHeaders");
148 }
149};
150
151} // namespace P4
152
153#endif /* MIDEND_FLATTENHEADERS_H_ */
Definition flattenInterfaceStructs.h:32
Definition typeChecker.h:32
Definition flattenHeaders.h:30
Definition flattenHeaders.h:139
Definition node.h:94
Definition visitor.h:400
Definition visitor.h:788
Definition ir/pass_manager.h:40
Definition flattenHeaders.h:121
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 flattenInterfaceStructs.h:69