8#ifndef FRONTENDS_P4_UNIQUENAMES_H_
9#define FRONTENDS_P4_UNIQUENAMES_H_
11#include "frontends/common/resolveReferences/referenceMap.h"
12#include "frontends/common/resolveReferences/resolveReferences.h"
13#include "frontends/p4/typeMap.h"
15#include "ir/pass_manager.h"
16#include "ir/visitor.h"
22 std::map<const IR::IDeclaration *, cstring> newName;
24 std::map<const IR::MethodCallExpression *, const IR::P4Action *> actionCall;
35 BUG_CHECK(n.has_value(),
"%1%: no new name", decl);
42 return newName.find(decl) != newName.end();
48 if (
auto it = newName.find(decl); it != newName.end()) {
54 void foundInTable(
const IR::P4Action *action);
55 void markActionCall(
const IR::P4Action *action,
const IR::MethodCallExpression *call);
56 const IR::P4Action *actionCalled(
const IR::MethodCallExpression *expression)
const;
61class UniqueNames :
public PassManager {
77 bool isTopLevel()
const {
78 return !isInContext<IR::P4Parser>() && !isInContext<IR::P4Control>();
80 explicit FindSymbols(
RenameMap *renameMap) : renameMap(renameMap) {
81 CHECK_NULL(renameMap);
82 setName(
"FindSymbols");
85 auto rv = Inspector::init_apply(node);
90 void doDecl(
const IR::Declaration *decl) {
91 cstring newName = nameGen.newName(decl->getName().string_view());
92 renameMap->setNewName(decl, newName);
94 void postorder(
const IR::Declaration_Variable *decl)
override { doDecl(decl); }
95 void postorder(
const IR::Declaration_Constant *decl)
override {
99 if (decl->getName().name.startsWith(
"__") && getParent<IR::P4Program>())
return;
102 void postorder(
const IR::Declaration_Instance *decl)
override {
103 if (!isTopLevel()) doDecl(decl);
105 void postorder(
const IR::P4Table *decl)
override { doDecl(decl); }
106 void postorder(
const IR::P4Action *decl)
override {
107 if (!isTopLevel()) doDecl(decl);
109 void postorder(
const IR::P4ValueSet *decl)
override {
110 if (!isTopLevel()) doDecl(decl);
114class RenameSymbols :
public Transform,
public ResolutionContext {
126 template <
typename D>
127 const IR::Node *renameDeclWithNameAnnotation(D *decl) {
129 if (name !=
nullptr && *name != decl->name) {
130 decl->addAnnotationIfNew(IR::Annotation::nameAnnotation,
131 new IR::StringLiteral(decl->name));
138 explicit RenameSymbols(
RenameMap *renameMap) : renameMap(renameMap) {
139 CHECK_NULL(renameMap);
140 visitDagOnce =
false;
141 setName(
"RenameSymbols");
143 const IR::Node *postorder(IR::Declaration_Variable *decl)
override;
144 const IR::Node *postorder(IR::Declaration_Constant *decl)
override;
145 const IR::Node *postorder(IR::PathExpression *expression)
override;
146 const IR::Node *postorder(IR::Declaration_Instance *decl)
override;
147 const IR::Node *postorder(IR::P4Table *decl)
override;
148 const IR::Node *postorder(IR::P4Action *decl)
override;
149 const IR::Node *postorder(IR::P4ValueSet *decl)
override;
150 const IR::Node *postorder(IR::Parameter *param)
override;
151 const IR::Node *postorder(IR::Argument *argument)
override;
159 void doParameters(
const IR::ParameterList *pl) {
160 for (
auto p : pl->parameters) {
161 cstring newName = nameGen.newName(p->name.string_view());
162 renameMap->setNewName(p, newName);
167 explicit FindParameters(
RenameMap *renameMap) : renameMap(renameMap) {
168 CHECK_NULL(renameMap);
169 setName(
"FindParameters");
171 void postorder(
const IR::P4Action *action)
override { doParameters(action->parameters); }
177class UniqueParameters :
public PassManager {
182 explicit UniqueParameters(
TypeMap *typeMap);
The Declaration interface, representing objects with names.
Definition declaration.h:17
Definition referenceMap.h:36
Definition uniqueNames.h:20
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:14
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:46
cstring getName(const IR::IDeclaration *decl) const
Get new name for the declaration, fails if none exists.
Definition uniqueNames.h:33
bool toRename(const IR::IDeclaration *decl) const
Definition uniqueNames.h:40
IR::ID * getName() const
Get new name of the current declaration or nullptr if the declaration is not to be renamed.
Definition uniqueNames.cpp:81
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13