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 !isInContext<IR::P4Parser>() && !isInContext<IR::P4Control>();
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().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 template <
typename D>
136 const IR::Node *renameDeclWithNameAnnotation(D *decl) {
138 if (name !=
nullptr && *name != decl->name) {
139 decl->addAnnotationIfNew(IR::Annotation::nameAnnotation,
140 new IR::StringLiteral(decl->name));
147 explicit RenameSymbols(
RenameMap *renameMap) : renameMap(renameMap) {
148 CHECK_NULL(renameMap);
149 visitDagOnce =
false;
150 setName(
"RenameSymbols");
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;
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);
176 explicit FindParameters(
RenameMap *renameMap) : renameMap(renameMap) {
177 CHECK_NULL(renameMap);
178 setName(
"FindParameters");
180 void postorder(
const IR::P4Action *action)
override { doParameters(action->parameters); }
186class UniqueParameters :
public PassManager {
191 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