mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-22 01:17:02 +00:00
Move/rename InstNamer::Namespace to TextualNamespace (#8131)
This class is now reusable independently of `InstNamer`, so it shouldn't be nested under it (but we have to choose a new name because `SemIR::Namespace` is already taken). --------- Co-authored-by: Dana Jansens <danakj@orodu.net>
This commit is contained in:
1 parent
873f7a3f73
commit
645e784aa0
3 files changed
+142
-139
No files matched your search
@@ -242,8 +242,8 @@ class MultiUnitCache {
|
||||
|
||||
auto short_file_names() -> const SemIR::ShortFileNameStore& {
|
||||
if (!short_file_names_) {
|
||||
SemIR::InstNamer::Namespace compilation_namespace;
|
||||
llvm::SmallVector<SemIR::InstNamer::Namespace::Name> namespace_names;
|
||||
SemIR::TextualNamespace compilation_namespace;
|
||||
llvm::SmallVector<SemIR::TextualNamespace::Name> namespace_names;
|
||||
namespace_names.reserve(units_.size());
|
||||
for (auto [i, unit] : llvm::enumerate(units_)) {
|
||||
std::string short_name;
|
||||
|
||||
@@ -32,6 +32,79 @@
|
||||
|
||||
namespace Carbon::SemIR {
|
||||
|
||||
auto TextualNamespace::Name::GetFullName() const -> llvm::StringRef {
|
||||
if (!value_) {
|
||||
return "<null name>";
|
||||
}
|
||||
llvm::StringMapEntry<NameResult>* value = value_;
|
||||
while (value->second.ambiguous && value->second.fallback) {
|
||||
value = value->second.fallback.value_;
|
||||
}
|
||||
return value->first();
|
||||
}
|
||||
|
||||
auto TextualNamespace::Name::GetBaseName() const -> llvm::StringRef {
|
||||
if (!value_) {
|
||||
return "<null name>";
|
||||
}
|
||||
return value_->first().take_front(base_name_size_);
|
||||
}
|
||||
|
||||
auto TextualNamespace::AllocateNameWithSuffixes(
|
||||
std::string full_name, llvm::ArrayRef<ssize_t> name_sizes) -> Name {
|
||||
// The best (shortest) name for this instruction so far, and the current
|
||||
// name for it.
|
||||
Name best;
|
||||
Name current;
|
||||
|
||||
const size_t base_name_size = name_sizes[0];
|
||||
|
||||
// Add `name` as a name for this entity.
|
||||
auto add_name = [&](llvm::StringRef name, bool mark_ambiguous = true) {
|
||||
auto [it, added] = allocated_.insert({name, NameResult()});
|
||||
Name new_name = Name(it, base_name_size);
|
||||
|
||||
if (!added) {
|
||||
if (mark_ambiguous) {
|
||||
// This name was allocated for a different instruction. Mark it as
|
||||
// ambiguous and keep looking for a name for this instruction.
|
||||
new_name.SetAmbiguous();
|
||||
}
|
||||
} else {
|
||||
if (!best) {
|
||||
best = new_name;
|
||||
} else {
|
||||
CARBON_CHECK(current);
|
||||
current.SetFallback(new_name);
|
||||
}
|
||||
current = new_name;
|
||||
}
|
||||
return added;
|
||||
};
|
||||
|
||||
int previous_size = -1;
|
||||
for (auto name_size : name_sizes) {
|
||||
CARBON_CHECK(name_size > previous_size, "`name_sizes` not sorted");
|
||||
previous_size = name_size;
|
||||
if (name_size > 0) {
|
||||
add_name(llvm::StringRef(full_name).take_front(name_size));
|
||||
}
|
||||
}
|
||||
CARBON_CHECK(static_cast<size_t>(previous_size) == full_name.size(),
|
||||
"`name_sizes` doesn't end with the size of `full_name`");
|
||||
|
||||
// Append numbers until we find an available name.
|
||||
full_name.push_back('.');
|
||||
auto name_size_without_counter = full_name.size();
|
||||
for (int counter = 1;; ++counter) {
|
||||
full_name.resize(name_size_without_counter);
|
||||
llvm::raw_string_ostream(full_name) << counter;
|
||||
if (add_name(full_name, /*mark_ambiguous=*/false)) {
|
||||
return best;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class InstNamer::NamingContext {
|
||||
public:
|
||||
explicit NamingContext(InstNamer* inst_namer, InstNamer::ScopeId scope_id,
|
||||
@@ -107,7 +180,8 @@ InstNamer::InstNamer(const File* sem_ir, int total_ir_count)
|
||||
: sem_ir_(sem_ir), fingerprinter_(total_ir_count) {
|
||||
CARBON_CHECK(sem_ir_->is_complete(),
|
||||
"Can't create InstNamer for pending SemIR::File");
|
||||
insts_.resize(sem_ir->insts().size(), {ScopeId::None, Namespace::Name()});
|
||||
insts_.resize(sem_ir->insts().size(),
|
||||
{ScopeId::None, TextualNamespace::Name()});
|
||||
labels_.resize(sem_ir->inst_blocks().size());
|
||||
scopes_.resize(GetScopeIdOffset(ScopeIdTypeEnum::None));
|
||||
generic_scopes_.resize(sem_ir->generics().size(), ScopeId::None);
|
||||
@@ -326,79 +400,6 @@ auto InstNamer::GetLabelFor(ScopeId scope_id, InstBlockId block_id) const
|
||||
return (GetScopeName(label_scope) + ".!" + label_name.GetFullName()).str();
|
||||
}
|
||||
|
||||
auto InstNamer::Namespace::Name::GetFullName() const -> llvm::StringRef {
|
||||
if (!value_) {
|
||||
return "<null name>";
|
||||
}
|
||||
llvm::StringMapEntry<NameResult>* value = value_;
|
||||
while (value->second.ambiguous && value->second.fallback) {
|
||||
value = value->second.fallback.value_;
|
||||
}
|
||||
return value->first();
|
||||
}
|
||||
|
||||
auto InstNamer::Namespace::Name::GetBaseName() const -> llvm::StringRef {
|
||||
if (!value_) {
|
||||
return "<null name>";
|
||||
}
|
||||
return value_->first().take_front(base_name_size_);
|
||||
}
|
||||
|
||||
auto InstNamer::Namespace::AllocateNameWithSuffixes(
|
||||
std::string full_name, llvm::ArrayRef<ssize_t> name_sizes) -> Name {
|
||||
// The best (shortest) name for this instruction so far, and the current
|
||||
// name for it.
|
||||
Name best;
|
||||
Name current;
|
||||
|
||||
const size_t base_name_size = name_sizes[0];
|
||||
|
||||
// Add `name` as a name for this entity.
|
||||
auto add_name = [&](llvm::StringRef name, bool mark_ambiguous = true) {
|
||||
auto [it, added] = allocated_.insert({name, NameResult()});
|
||||
Name new_name = Name(it, base_name_size);
|
||||
|
||||
if (!added) {
|
||||
if (mark_ambiguous) {
|
||||
// This name was allocated for a different instruction. Mark it as
|
||||
// ambiguous and keep looking for a name for this instruction.
|
||||
new_name.SetAmbiguous();
|
||||
}
|
||||
} else {
|
||||
if (!best) {
|
||||
best = new_name;
|
||||
} else {
|
||||
CARBON_CHECK(current);
|
||||
current.SetFallback(new_name);
|
||||
}
|
||||
current = new_name;
|
||||
}
|
||||
return added;
|
||||
};
|
||||
|
||||
int previous_size = -1;
|
||||
for (auto name_size : name_sizes) {
|
||||
CARBON_CHECK(name_size > previous_size, "`name_sizes` not sorted");
|
||||
previous_size = name_size;
|
||||
if (name_size > 0) {
|
||||
add_name(llvm::StringRef(full_name).take_front(name_size));
|
||||
}
|
||||
}
|
||||
CARBON_CHECK(static_cast<size_t>(previous_size) == full_name.size(),
|
||||
"`name_sizes` doesn't end with the size of `full_name`");
|
||||
|
||||
// Append numbers until we find an available name.
|
||||
full_name.push_back('.');
|
||||
auto name_size_without_counter = full_name.size();
|
||||
for (int counter = 1;; ++counter) {
|
||||
full_name.resize(name_size_without_counter);
|
||||
llvm::raw_string_ostream(full_name) << counter;
|
||||
if (add_name(full_name, /*mark_ambiguous=*/false)) {
|
||||
return best;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto InstNamer::FileNamespace::AllocateName(
|
||||
const InstNamer& inst_namer,
|
||||
std::variant<LocId, uint64_t> loc_id_or_fingerprint, std::string name)
|
||||
|
||||
@@ -19,6 +19,67 @@
|
||||
|
||||
namespace Carbon::SemIR {
|
||||
|
||||
// A space in which unique names can be allocated, for use in textual SemIR
|
||||
// formatting.
|
||||
class TextualNamespace {
|
||||
private:
|
||||
// A result of a name lookup.
|
||||
struct NameResult;
|
||||
|
||||
public:
|
||||
// A name in a namespace, which might be redirected to refer to another name
|
||||
// for disambiguation purposes.
|
||||
class Name {
|
||||
public:
|
||||
explicit Name() : value_(nullptr) {}
|
||||
explicit Name(llvm::StringMap<NameResult>::iterator it,
|
||||
size_t base_name_size)
|
||||
: value_(&*it), base_name_size_(base_name_size) {}
|
||||
|
||||
explicit operator bool() const { return value_; }
|
||||
|
||||
// Returns the disambiguated name. Note that this is not stable until
|
||||
// all names have been added to the `TextualNamespace`.
|
||||
auto GetFullName() const -> llvm::StringRef;
|
||||
|
||||
// Returns the base name, without any disambiguators.
|
||||
auto GetBaseName() const -> llvm::StringRef;
|
||||
|
||||
auto SetFallback(Name name) -> void { value_->second.fallback = name; }
|
||||
|
||||
auto SetAmbiguous() -> void { value_->second.ambiguous = true; }
|
||||
|
||||
private:
|
||||
llvm::StringMapEntry<NameResult>* value_;
|
||||
|
||||
// The base name length within `value_->first()`.
|
||||
size_t base_name_size_;
|
||||
};
|
||||
|
||||
// Allocates and returns a name, handling ambiguity.
|
||||
auto AllocateName(std::string name) [[clang::lifetimebound]] -> Name {
|
||||
auto size = std::ssize(name);
|
||||
return AllocateNameWithSuffixes(std::move(name), {size});
|
||||
}
|
||||
|
||||
// Allocates and returns a name, handling ambiguity. `full_name` is
|
||||
// expected to be structured as a base name followed by a series of
|
||||
// suffixes that add progressively more disambiguation. `name_sizes[N]`
|
||||
// is the size of the base name plus the first N such suffixes
|
||||
// (so `name_sizes.back()` should be `full_name.size()`).
|
||||
auto AllocateNameWithSuffixes(std::string full_name,
|
||||
llvm::ArrayRef<ssize_t> name_sizes)
|
||||
[[clang::lifetimebound]] -> Name;
|
||||
|
||||
private:
|
||||
struct NameResult {
|
||||
bool ambiguous = false;
|
||||
Name fallback = Name();
|
||||
};
|
||||
|
||||
llvm::StringMap<NameResult> allocated_;
|
||||
};
|
||||
|
||||
// Assigns names to instructions, blocks, and scopes in the Semantics IR.
|
||||
//
|
||||
// If `<unexpected>` occurs in output of valid SemIR, it often means the
|
||||
@@ -27,65 +88,6 @@ namespace Carbon::SemIR {
|
||||
// unable to correctly walk the SemIR.
|
||||
class InstNamer {
|
||||
public:
|
||||
// A space in which unique names can be allocated.
|
||||
class Namespace {
|
||||
private:
|
||||
// A result of a name lookup.
|
||||
struct NameResult;
|
||||
|
||||
public:
|
||||
// A name in a namespace, which might be redirected to refer to another name
|
||||
// for disambiguation purposes.
|
||||
class Name {
|
||||
public:
|
||||
explicit Name() : value_(nullptr) {}
|
||||
explicit Name(llvm::StringMap<NameResult>::iterator it,
|
||||
size_t base_name_size)
|
||||
: value_(&*it), base_name_size_(base_name_size) {}
|
||||
|
||||
explicit operator bool() const { return value_; }
|
||||
|
||||
// Returns the disambiguated name. Note that this is not stable until
|
||||
// all names have been added to the `Namespace`.
|
||||
auto GetFullName() const -> llvm::StringRef;
|
||||
|
||||
// Returns the base name, without any disambiguators.
|
||||
auto GetBaseName() const -> llvm::StringRef;
|
||||
|
||||
auto SetFallback(Name name) -> void { value_->second.fallback = name; }
|
||||
|
||||
auto SetAmbiguous() -> void { value_->second.ambiguous = true; }
|
||||
|
||||
private:
|
||||
llvm::StringMapEntry<NameResult>* value_;
|
||||
|
||||
// The base name length within `value_->first()`.
|
||||
size_t base_name_size_;
|
||||
};
|
||||
|
||||
// Allocates and returns a name, handling ambiguity.
|
||||
auto AllocateName(std::string name) -> Name {
|
||||
auto size = std::ssize(name);
|
||||
return AllocateNameWithSuffixes(std::move(name), {size});
|
||||
}
|
||||
|
||||
// Allocates and returns a name, handling ambiguity. `full_name` is
|
||||
// expected to be structured as a base name followed by a series of
|
||||
// suffixes that add progressively more disambiguation. `name_sizes[N]`
|
||||
// is the size of the base name plus the first N such suffixes
|
||||
// (so `name_sizes.back()` should be `full_name.size()`).
|
||||
auto AllocateNameWithSuffixes(std::string full_name,
|
||||
llvm::ArrayRef<ssize_t> name_sizes) -> Name;
|
||||
|
||||
private:
|
||||
struct NameResult {
|
||||
bool ambiguous = false;
|
||||
Name fallback = Name();
|
||||
};
|
||||
|
||||
llvm::StringMap<NameResult> allocated_;
|
||||
};
|
||||
|
||||
// int32_t matches the input value size.
|
||||
enum class ScopeId : int32_t {
|
||||
None = -1,
|
||||
@@ -205,7 +207,7 @@ class InstNamer {
|
||||
|
||||
private:
|
||||
// A namespace within a SemIR::File.
|
||||
class FileNamespace : public Namespace {
|
||||
class FileNamespace : public TextualNamespace {
|
||||
public:
|
||||
// Allocates and returns a name, handling ambiguity.
|
||||
auto AllocateName(const InstNamer& inst_namer,
|
||||
@@ -215,7 +217,7 @@ class InstNamer {
|
||||
|
||||
// A named scope that contains named entities.
|
||||
struct Scope {
|
||||
Namespace::Name name;
|
||||
TextualNamespace::Name name;
|
||||
FileNamespace insts;
|
||||
FileNamespace labels;
|
||||
};
|
||||
@@ -299,10 +301,10 @@ class InstNamer {
|
||||
FileNamespace globals_;
|
||||
// The enclosing scope and name for each instruction, indexed by the InstId's
|
||||
// index.
|
||||
std::vector<std::pair<ScopeId, Namespace::Name>> insts_;
|
||||
std::vector<std::pair<ScopeId, TextualNamespace::Name>> insts_;
|
||||
// The enclosing scope and name for each block that might be a branch target,
|
||||
// indexed by the InstBlockId's index.
|
||||
std::vector<std::pair<ScopeId, Namespace::Name>> labels_;
|
||||
std::vector<std::pair<ScopeId, TextualNamespace::Name>> labels_;
|
||||
// The scopes corresponding to ScopeId values.
|
||||
std::vector<Scope> scopes_;
|
||||
// The scope IDs corresponding to generics. The vector indexes are the
|
||||
|
||||
Reference in new issue
Block a user