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;
86 bool isTopLevel()
const {
87 return findContext<IR::P4Parser>() ==
nullptr && findContext<IR::P4Control>() ==
nullptr;
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());
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);
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);
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) {
180 CHECK_NULL(renameMap);
181 setName(
"FindParameters");
183 void postorder(
const IR::P4Action *action)
override { doParameters(action->parameters); }
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 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 ir/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:35
Definition uniqueNames.h:70
Definition uniqueNames.h:189
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24