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().name.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 static const IR::Annotations *addNameAnnotation(cstring name, const IR::Annotations *annos);
136
139 template <typename D>
140 const IR::Node *renameDeclWithNameAnnotation(D *decl) {
141 auto name = getName();
142 if (name != nullptr && *name != decl->name) {
143 decl->annotations = addNameAnnotation(decl->name, decl->annotations);
144 decl->name = *name;
145 }
146 return decl;
147 }
148
149 public:
150 explicit RenameSymbols(RenameMap *renameMap) : renameMap(renameMap) {
151 CHECK_NULL(renameMap);
152 visitDagOnce = false;
153 setName("RenameSymbols");
154 }
155 const IR::Node *postorder(IR::Declaration_Variable *decl) override;
156 const IR::Node *postorder(IR::Declaration_Constant *decl) override;
157 const IR::Node *postorder(IR::PathExpression *expression) override;
158 const IR::Node *postorder(IR::Declaration_Instance *decl) override;
159 const IR::Node *postorder(IR::P4Table *decl) override;
160 const IR::Node *postorder(IR::P4Action *decl) override;
161 const IR::Node *postorder(IR::P4ValueSet *decl) override;
162 const IR::Node *postorder(IR::Parameter *param) override;
163 const IR::Node *postorder(IR::Argument *argument) override;
164};
165
167class FindParameters : public Inspector {
168 MinimalNameGenerator nameGen;
169 RenameMap *renameMap;
170
171 void doParameters(const IR::ParameterList *pl) {
172 for (auto p : pl->parameters) {
173 cstring newName = nameGen.newName(p->name.name.string_view());
174 renameMap->setNewName(p, newName);
175 }
176 }
177
178 public:
179 explicit FindParameters(RenameMap *renameMap) : renameMap(renameMap) {
180 CHECK_NULL(renameMap);
181 setName("FindParameters");
182 }
183 void postorder(const IR::P4Action *action) override { doParameters(action->parameters); }
184 profile_t init_apply(const IR::Node *node) override;
185};
186
190 private:
191 RenameMap *renameMap;
192
193 public:
194 explicit UniqueParameters(TypeMap *typeMap);
195};
196
197} // namespace P4
198
199#endif /* FRONTENDS_P4_UNIQUENAMES_H_ */
Finds parameters for actions that will be given unique names.
Definition uniqueNames.h:167
Definition uniqueNames.h:81
The Declaration interface, representing objects with names.
Definition declaration.h:26
Definition node.h:95
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 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:33
Definition visitor.h:424
Definition typeMap.h:41
Definition uniqueNames.h:70
Definition uniqueNames.h:189
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