P4C
The P4 Compiler
Loading...
Searching...
No Matches
uniqueNames.h
1/*
2Copyright 2013-present Barefoot Networks, 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 FRONTENDS_P4_UNIQUENAMES_H_
18#define FRONTENDS_P4_UNIQUENAMES_H_
19
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/common/resolveReferences/resolveReferences.h"
22#include "frontends/p4/typeMap.h"
23#include "ir/ir.h"
24#include "ir/pass_manager.h"
25#include "ir/visitor.h"
26
27namespace P4 {
28
29class RenameMap {
31 std::map<const IR::IDeclaration *, cstring> newName;
33 std::map<const IR::MethodCallExpression *, const IR::P4Action *> actionCall;
34
35 public:
39 void setNewName(const IR::IDeclaration *decl, cstring name, bool allowOverride = false);
40
42 cstring getName(const IR::IDeclaration *decl) const {
43 auto n = get(decl);
44 BUG_CHECK(n.has_value(), "%1%: no new name", decl);
45 return *n;
46 }
47
49 bool toRename(const IR::IDeclaration *decl) const {
50 CHECK_NULL(decl);
51 return newName.find(decl) != newName.end();
52 }
53
55 std::optional<cstring> get(const IR::IDeclaration *decl) const {
56 CHECK_NULL(decl);
57 if (auto it = newName.find(decl); it != newName.end()) {
58 return it->second;
59 }
60 return {};
61 }
62
63 void foundInTable(const IR::P4Action *action);
64 void markActionCall(const IR::P4Action *action, const IR::MethodCallExpression *call);
65 const IR::P4Action *actionCalled(const IR::MethodCallExpression *expression) const;
66};
67
70class UniqueNames : public PassManager {
71 private:
72 RenameMap *renameMap;
73
74 public:
76};
77
81class FindSymbols : public Inspector {
82 MinimalNameGenerator nameGen; // used to generate new names
83 RenameMap *renameMap;
84
85 public:
86 bool isTopLevel() const {
87 return findContext<IR::P4Parser>() == nullptr && findContext<IR::P4Control>() == nullptr;
88 }
89 explicit FindSymbols(RenameMap *renameMap) : renameMap(renameMap) {
90 CHECK_NULL(renameMap);
91 setName("FindSymbols");
92 }
93 profile_t init_apply(const IR::Node *node) override {
94 auto rv = Inspector::init_apply(node);
95 node->apply(nameGen);
96 return rv;
97 }
98
99 void doDecl(const IR::Declaration *decl) {
100 cstring newName = nameGen.newName(decl->getName().string_view());
101 renameMap->setNewName(decl, newName);
102 }
103 void postorder(const IR::Declaration_Variable *decl) override { doDecl(decl); }
104 void postorder(const IR::Declaration_Constant *decl) override {
105 // Skip toplevel constants with names like __
106 // We assume that these do not clash and no new symbols with
107 // these names will be added.
108 if (decl->getName().name.startsWith("__") && getParent<IR::P4Program>()) return;
109 doDecl(decl);
110 }
111 void postorder(const IR::Declaration_Instance *decl) override {
112 if (!isTopLevel()) doDecl(decl);
113 }
114 void postorder(const IR::P4Table *decl) override { doDecl(decl); }
115 void postorder(const IR::P4Action *decl) override {
116 if (!isTopLevel()) doDecl(decl);
117 }
118 void postorder(const IR::P4ValueSet *decl) override {
119 if (!isTopLevel()) doDecl(decl);
120 }
121};
122
124 protected:
125 RenameMap *renameMap;
126
128 IR::ID *getName() const;
131 IR::ID *getName(const IR::IDeclaration *decl) const;
132
135 template <typename D>
136 const IR::Node *renameDeclWithNameAnnotation(D *decl) {
137 auto name = getName();
138 if (name != nullptr && *name != decl->name) {
139 decl->addAnnotationIfNew(IR::Annotation::nameAnnotation,
140 new IR::StringLiteral(decl->name));
141 decl->name = *name;
142 }
143 return decl;
144 }
145
146 public:
147 explicit RenameSymbols(RenameMap *renameMap) : renameMap(renameMap) {
148 CHECK_NULL(renameMap);
149 visitDagOnce = false;
150 setName("RenameSymbols");
151 }
152 const IR::Node *postorder(IR::Declaration_Variable *decl) override;
153 const IR::Node *postorder(IR::Declaration_Constant *decl) override;
154 const IR::Node *postorder(IR::PathExpression *expression) override;
155 const IR::Node *postorder(IR::Declaration_Instance *decl) override;
156 const IR::Node *postorder(IR::P4Table *decl) override;
157 const IR::Node *postorder(IR::P4Action *decl) override;
158 const IR::Node *postorder(IR::P4ValueSet *decl) override;
159 const IR::Node *postorder(IR::Parameter *param) override;
160 const IR::Node *postorder(IR::Argument *argument) override;
161};
162
164class FindParameters : public Inspector {
165 MinimalNameGenerator nameGen;
166 RenameMap *renameMap;
167
168 void doParameters(const IR::ParameterList *pl) {
169 for (auto p : pl->parameters) {
170 cstring newName = nameGen.newName(p->name.string_view());
171 renameMap->setNewName(p, newName);
172 }
173 }
174
175 public:
176 explicit FindParameters(RenameMap *renameMap) : renameMap(renameMap) {
177 CHECK_NULL(renameMap);
178 setName("FindParameters");
179 }
180 void postorder(const IR::P4Action *action) override { doParameters(action->parameters); }
181 profile_t init_apply(const IR::Node *node) override;
182};
183
187 private:
188 RenameMap *renameMap;
189
190 public:
191 explicit UniqueParameters(TypeMap *typeMap);
192};
193
194} // namespace P4
195
196#endif /* FRONTENDS_P4_UNIQUENAMES_H_ */
Finds parameters for actions that will be given unique names.
Definition uniqueNames.h:164
Definition uniqueNames.h:81
The Declaration interface, representing objects with names.
Definition declaration.h:26
Definition node.h:94
Definition visitor.h:400
Definition referenceMap.h:36
cstring newName(std::string_view base) override
Generate a name from base that does not appear in usedNames.
Definition referenceMap.cpp:118
Definition ir/pass_manager.h:40
Definition uniqueNames.h:29
void setNewName(const IR::IDeclaration *decl, cstring name, bool allowOverride=false)
Add rename entry for the declaration to be named with the given name.
Definition uniqueNames.cpp:25
std::optional< cstring > get(const IR::IDeclaration *decl) const
Get new name for the declaration (wrapped in optional), or std::nullopt if there is none.
Definition uniqueNames.h:55
cstring getName(const IR::IDeclaration *decl) const
Get new name for the declaration, fails if none exists.
Definition uniqueNames.h:42
bool toRename(const IR::IDeclaration *decl) const
Definition uniqueNames.h:49
Definition uniqueNames.h:123
IR::ID * getName() const
Get new name of the current declaration or nullptr if the declaration is not to be renamed.
Definition uniqueNames.cpp:92
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Definition visitor.h:424
Definition typeMap.h:41
Definition uniqueNames.h:70
Definition uniqueNames.h:186
Definition visitor.h:78
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition id.h:28