Skip to content

Commit

Permalink
Generalization & minor refactoring in RenameMap (#4677)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlstill authored May 27, 2024
1 parent ac9c3f8 commit ce5cb8c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
12 changes: 6 additions & 6 deletions frontends/p4/uniqueNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ limitations under the License.

namespace P4 {

void RenameMap::setNewName(const IR::IDeclaration *decl, cstring name) {
void RenameMap::setNewName(const IR::IDeclaration *decl, cstring name, bool allowOverride) {
CHECK_NULL(decl);
BUG_CHECK(!name.isNullOrEmpty(), "Empty name");
LOG1("Will rename " << dbp(decl) << " to " << name);
if (newName.find(decl) != newName.end()) BUG("%1%: already renamed", decl);
newName.emplace(decl, name);
BUG_CHECK(allowOverride || newName.find(decl) == newName.end(), "%1%: already renamed", decl);
newName.insert_or_assign(decl, name);
}

const IR::P4Action *RenameMap::actionCalled(const IR::MethodCallExpression *expression) const {
Expand Down Expand Up @@ -97,9 +97,9 @@ UniqueParameters::UniqueParameters(ReferenceMap *refMap, TypeMap *typeMap)

IR::ID *RenameSymbols::getName() const {
auto orig = getOriginal<IR::IDeclaration>();
if (!renameMap->toRename(orig)) return nullptr;
auto newName = renameMap->getName(orig);
auto name = new IR::ID(orig->getName().srcInfo, newName, orig->getName().originalName);
auto newName = renameMap->get(orig);
if (!newName.has_value()) return nullptr;
auto name = new IR::ID(orig->getName().srcInfo, *newName, orig->getName().originalName);
return name;
}

Expand Down
26 changes: 21 additions & 5 deletions frontends/p4/uniqueNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,33 @@ class RenameMap {
std::map<const IR::MethodCallExpression *, const IR::P4Action *> actionCall;

public:
void setNewName(const IR::IDeclaration *decl, cstring name);
/// @brief Add rename entry for the declaration to be named with the given name.
/// @param allowOverride If set to true, don't fail if a new name was already set but replace it
/// instead.
void setNewName(const IR::IDeclaration *decl, cstring name, bool allowOverride = false);

/// Get new name for the declaration, fails if none exists.
cstring getName(const IR::IDeclaration *decl) const {
CHECK_NULL(decl);
BUG_CHECK(newName.find(decl) != newName.end(), "%1%: no new name", decl);
auto result = ::get(newName, decl);
return result;
auto n = get(decl);
BUG_CHECK(n.has_value(), "%1%: no new name", decl);
return *n;
}

/// @returns true if there is a new name for the declaration, false otherwise.
bool toRename(const IR::IDeclaration *decl) const {
CHECK_NULL(decl);
return newName.find(decl) != newName.end();
}

/// Get new name for the declaration (wrapped in optional), or std::nullopt if there is none.
std::optional<cstring> get(const IR::IDeclaration *decl) const {
CHECK_NULL(decl);
if (auto it = newName.find(decl); it != newName.end()) {
return it->second;
}
return {};
}

void foundInTable(const IR::P4Action *action);
void markActionCall(const IR::P4Action *action, const IR::MethodCallExpression *call);
const IR::P4Action *actionCalled(const IR::MethodCallExpression *expression) const;
Expand Down

0 comments on commit ce5cb8c

Please sign in to comment.