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/resolveReferences.h"
21#include "frontends/p4/typeMap.h"
22#include "ir/ir.h"
23#include "ir/pass_manager.h"
24
25namespace P4 {
26
27class RenameMap {
29 std::map<const IR::IDeclaration *, cstring> newName;
31 std::map<const IR::MethodCallExpression *, const IR::P4Action *> actionCall;
32
33 public:
34 void setNewName(const IR::IDeclaration *decl, cstring name);
35 cstring getName(const IR::IDeclaration *decl) const {
36 CHECK_NULL(decl);
37 BUG_CHECK(newName.find(decl) != newName.end(), "%1%: no new name", decl);
38 auto result = ::get(newName, decl);
39 return result;
40 }
41 bool toRename(const IR::IDeclaration *decl) const {
42 CHECK_NULL(decl);
43 return newName.find(decl) != newName.end();
44 }
45 void foundInTable(const IR::P4Action *action);
46 void markActionCall(const IR::P4Action *action, const IR::MethodCallExpression *call);
47 const IR::P4Action *actionCalled(const IR::MethodCallExpression *expression) const;
48};
49
52class UniqueNames : public PassManager {
53 private:
54 RenameMap *renameMap;
55
56 public:
57 explicit UniqueNames(ReferenceMap *refMap);
58};
59
63class FindSymbols : public Inspector {
64 ReferenceMap *refMap; // used to generate new names
65 RenameMap *renameMap;
66
67 public:
68 bool isTopLevel() const {
69 return findContext<IR::P4Parser>() == nullptr && findContext<IR::P4Control>() == nullptr;
70 }
71 FindSymbols(ReferenceMap *refMap, RenameMap *renameMap) : refMap(refMap), renameMap(renameMap) {
72 CHECK_NULL(refMap);
73 CHECK_NULL(renameMap);
74 setName("FindSymbols");
75 }
76 void doDecl(const IR::Declaration *decl) {
77 cstring newName = refMap->newName(decl->getName());
78 renameMap->setNewName(decl, newName);
79 }
80 void postorder(const IR::Declaration_Variable *decl) override { doDecl(decl); }
81 void postorder(const IR::Declaration_Constant *decl) override {
82 // Skip toplevel constants with names like __
83 // We assume that these do not clash and no new symbols with
84 // these names will be added.
85 if (decl->getName().name.startsWith("__") && getParent<IR::P4Program>()) return;
86 doDecl(decl);
87 }
88 void postorder(const IR::Declaration_Instance *decl) override {
89 if (!isTopLevel()) doDecl(decl);
90 }
91 void postorder(const IR::P4Table *decl) override { doDecl(decl); }
92 void postorder(const IR::P4Action *decl) override {
93 if (!isTopLevel()) doDecl(decl);
94 }
95 void postorder(const IR::P4ValueSet *decl) override {
96 if (!isTopLevel()) doDecl(decl);
97 }
98};
99
100class RenameSymbols : public Transform {
101 ReferenceMap *refMap;
102 RenameMap *renameMap;
103
104 IR::ID *getName() const;
105
106 public:
107 RenameSymbols(ReferenceMap *refMap, RenameMap *renameMap)
108 : refMap(refMap), renameMap(renameMap) {
109 CHECK_NULL(refMap);
110 CHECK_NULL(renameMap);
111 visitDagOnce = false;
112 setName("RenameSymbols");
113 }
114 const IR::Node *postorder(IR::Declaration_Variable *decl) override;
115 const IR::Node *postorder(IR::Declaration_Constant *decl) override;
116 const IR::Node *postorder(IR::PathExpression *expression) override;
117 const IR::Node *postorder(IR::Declaration_Instance *decl) override;
118 const IR::Node *postorder(IR::P4Table *decl) override;
119 const IR::Node *postorder(IR::P4Action *decl) override;
120 const IR::Node *postorder(IR::P4ValueSet *decl) override;
121 const IR::Node *postorder(IR::Parameter *param) override;
122 const IR::Node *postorder(IR::Argument *argument) override;
123};
124
126class FindParameters : public Inspector {
127 ReferenceMap *refMap; // used to generate new names
128 RenameMap *renameMap;
129
130 void doParameters(const IR::ParameterList *pl) {
131 for (auto p : pl->parameters) {
132 cstring newName = refMap->newName(p->name);
133 renameMap->setNewName(p, newName);
134 }
135 }
136
137 public:
138 FindParameters(ReferenceMap *refMap, RenameMap *renameMap)
139 : refMap(refMap), renameMap(renameMap) {
140 CHECK_NULL(refMap);
141 CHECK_NULL(renameMap);
142 setName("FindParameters");
143 }
144 void postorder(const IR::P4Action *action) override { doParameters(action->parameters); }
145};
146
149class UniqueParameters : public PassManager {
150 private:
151 RenameMap *renameMap;
152
153 public:
154 UniqueParameters(ReferenceMap *refMap, TypeMap *typeMap);
155};
156
157} // namespace P4
158
159#endif /* FRONTENDS_P4_UNIQUENAMES_H_ */
Finds parameters for actions that will be given unique names.
Definition uniqueNames.h:126
Definition uniqueNames.h:63
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
cstring newName(cstring base) override
Generate a name from base that fresh for the program.
Definition referenceMap.cpp:96
Definition uniqueNames.h:27
Definition uniqueNames.h:100
Definition typeMap.h:42
Definition uniqueNames.h:52
Definition uniqueNames.h:149
Definition cstring.h:72
Definition applyOptionsPragmas.cpp:24