Skip to content

Commit

Permalink
Add lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Borup Petersen committed Aug 6, 2024
1 parent 735a1c3 commit a73c2cd
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion include/circt/Support/Namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ class Namespace {

/// Removes a symbol from the namespace. Returns true if the symbol was
/// removed, false if the symbol was not found.
bool erase(llvm::StringRef symbol) { return nextIndex.erase(symbol); }
/// This is only allowed to be called _before_ any call to newName.
bool erase(llvm::StringRef symbol) {
assert(!locked && "Cannot erase names from a locked namespace");
return nextIndex.erase(symbol);
}

/// Empty the namespace.
void clear() { nextIndex.clear(); }
Expand All @@ -74,6 +78,7 @@ class Namespace {
/// 2. The name is given a `_<n>` suffix where `<n>` is a number starting from
/// `0` and incrementing by one each time (`_0`, ...).
StringRef newName(const Twine &name) {
locked = true;
// Special case the situation where there is no name collision to avoid
// messing with the SmallString allocation below.
llvm::SmallString<64> tryName;
Expand Down Expand Up @@ -146,6 +151,11 @@ class Namespace {
// namespace. It follows that all values less than the "next index" value are
// already used.
llvm::StringMap<size_t> nextIndex;

// When true, no names can be erased from the namespace. This is to prevent
// erasing names after they have been used, thus leaving users of the
// namespace in an inconsistent state.
bool locked = false;
};

} // namespace circt
Expand Down

0 comments on commit a73c2cd

Please sign in to comment.