17#ifndef FRONTENDS_P4_UNIQUENAMES_H_
18#define FRONTENDS_P4_UNIQUENAMES_H_
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/common/resolveReferences/resolveReferences.h"
22#include "frontends/p4/typeMap.h"
24#include "ir/pass_manager.h"
25#include "ir/visitor.h"
31 std::map<const IR::IDeclaration *, cstring> newName;
33 std::map<const IR::MethodCallExpression *, const IR::P4Action *> actionCall;
44 BUG_CHECK(n.has_value(),
"%1%: no new name", decl);
51 return newName.find(decl) != newName.end();
57 if (
auto it = newName.find(decl); it != newName.end()) {
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;
70class UniqueNames :
public PassManager {
86 bool isTopLevel()
const {
87 return findContext<IR::P4Parser>() ==
nullptr && findContext<IR::P4Control>() ==
nullptr;
89 explicit FindSymbols(
RenameMap *renameMap) : renameMap(renameMap) {
90 CHECK_NULL(renameMap);
91 setName(
"FindSymbols");
94 auto rv = Inspector::init_apply(node);
99 void doDecl(
const IR::Declaration *decl) {
100 cstring newName = nameGen.newName(decl->getName().name.string_view());
101 renameMap->setNewName(decl, newName);
103 void postorder(
const IR::Declaration_Variable *decl)
override { doDecl(decl); }
104 void postorder(
const IR::Declaration_Constant *decl)
override {
108 if (decl->getName().name.startsWith(
"__") && getParent<IR::P4Program>())
return;
111 void postorder(
const IR::Declaration_Instance *decl)
override {
112 if (!isTopLevel()) doDecl(decl);
114 void postorder(
const IR::P4Table *decl)
override { doDecl(decl); }
115 void postorder(
const IR::P4Action *decl)
override {
116 if (!isTopLevel()) doDecl(decl);
118 void postorder(
const IR::P4ValueSet *decl)
override {
119 if (!isTopLevel()) doDecl(decl);
123class RenameSymbols :
public Transform,
public ResolutionContext {
135 static const IR::Annotations *addNameAnnotation(
cstring name,
const IR::Annotations *annos);
139 template <
typename D>
140 const IR::Node *renameDeclWithNameAnnotation(D *decl) {
142 if (name !=
nullptr && *name != decl->name) {
143 decl->annotations = addNameAnnotation(decl->name, decl->annotations);
150 explicit RenameSymbols(
RenameMap *renameMap) : renameMap(renameMap) {
151 CHECK_NULL(renameMap);
152 visitDagOnce =
false;
153 setName(
"RenameSymbols");
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;
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);
179 explicit FindParameters(
RenameMap *renameMap) : renameMap(renameMap) {
180 CHECK_NULL(renameMap);
181 setName(
"FindParameters");
183 void postorder(
const IR::P4Action *action)
override { doParameters(action->parameters); }
189class UniqueParameters :
public PassManager {
194 explicit UniqueParameters(
TypeMap *typeMap);
The Declaration interface, representing objects with names.
Definition declaration.h:26
Definition referenceMap.h:36
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
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
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24