Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor library names into their own ID structure. #4219

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion toolchain/check/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,8 @@ static auto CheckParseTree(
library_id = packaging->names.library_id;
}
unit_info.unit->sem_ir->emplace(
unit_info.check_ir_id, package_id, library_id,
unit_info.check_ir_id, package_id,
SemIR::LibraryNameId::ForStringLiteralValueId(library_id),
*unit_info.unit->value_stores,
unit_info.unit->tokens->source().filename().str());

Expand Down
2 changes: 1 addition & 1 deletion toolchain/sem_ir/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
namespace Carbon::SemIR {

File::File(CheckIRId check_ir_id, IdentifierId package_id,
StringLiteralValueId library_id, SharedValueStores& value_stores,
LibraryNameId library_id, SharedValueStores& value_stores,
std::string filename)
: check_ir_id_(check_ir_id),
package_id_(package_id),
Expand Down
8 changes: 4 additions & 4 deletions toolchain/sem_ir/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class File : public Printable<File> {
public:
// Starts a new file for Check::CheckParseTree.
explicit File(CheckIRId check_ir_id, IdentifierId package_id,
StringLiteralValueId library_id,
SharedValueStores& value_stores, std::string filename);
LibraryNameId library_id, SharedValueStores& value_stores,
std::string filename);

File(const File&) = delete;
auto operator=(const File&) -> File& = delete;
Expand Down Expand Up @@ -82,7 +82,7 @@ class File : public Printable<File> {

auto check_ir_id() const -> CheckIRId { return check_ir_id_; }
auto package_id() const -> IdentifierId { return package_id_; }
auto library_id() const -> StringLiteralValueId { return library_id_; }
auto library_id() const -> SemIR::LibraryNameId { return library_id_; }

// Directly expose SharedValueStores members.
auto identifiers() -> CanonicalValueStore<IdentifierId>& {
Expand Down Expand Up @@ -181,7 +181,7 @@ class File : public Printable<File> {
IdentifierId package_id_ = IdentifierId::Invalid;

// The file's library.
StringLiteralValueId library_id_ = StringLiteralValueId::Invalid;
LibraryNameId library_id_ = LibraryNameId::Invalid;

// Shared, compile-scoped values.
SharedValueStores* value_stores_;
Expand Down
8 changes: 5 additions & 3 deletions toolchain/sem_ir/formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,10 +900,12 @@ class FormatterImpl {
out_ << "Main";
}
out_ << "//";
if (import_ir.library_id().is_valid()) {
out_ << import_ir.string_literal_values().Get(import_ir.library_id());
} else {
CARBON_CHECK(import_ir.library_id().is_valid());
if (import_ir.library_id() == LibraryNameId::Default) {
out_ << "default";
} else {
out_ << import_ir.string_literal_values().Get(
import_ir.library_id().AsStringLiteralValueId());
}
}

Expand Down
2 changes: 1 addition & 1 deletion toolchain/sem_ir/id_kind.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ using IdKind = TypeEnum<
InstId, ConstantId, EntityNameId, CompileTimeBindIndex, FunctionId, ClassId,
InterfaceId, ImplId, GenericId, SpecificId, ImportIRId, ImportIRInstId,
LocId, BoolValue, IntKind, NameId, NameScopeId, InstBlockId, TypeId,
TypeBlockId, ElementIndex, FloatKind>;
TypeBlockId, ElementIndex, LibraryNameId, FloatKind>;

} // namespace Carbon::SemIR

Expand Down
50 changes: 50 additions & 0 deletions toolchain/sem_ir/ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,56 @@ struct ElementIndex : public IndexBase, public Printable<ElementIndex> {
}
};

// The ID of a library name. This is either a string literal or `default`.
struct LibraryNameId : public IdBase, public Printable<NameId> {
using DiagnosticType = DiagnosticTypeInfo<std::string>;

// An explicitly invalid ID.
static const LibraryNameId Invalid;
// The name of `default`.
static const LibraryNameId Default;
// Track cases where the library name was set, but has been diagnosed and
// shouldn't be used anymore.
static const LibraryNameId Error;

// Returns the LibraryNameId for a library name as a string literal.
static auto ForStringLiteralValueId(StringLiteralValueId id)
-> LibraryNameId {
CARBON_CHECK(id.index >= InvalidIndex)
<< "Unexpected library name ID " << id;
if (id == StringLiteralValueId::Invalid) {
// Prior to SemIR, we use invalid to indicate `default`.
return LibraryNameId::Default;
} else {
return LibraryNameId(id.index);
}
}

using IdBase::IdBase;

// Converts a LibraryNameId back to a string literal.
auto AsStringLiteralValueId() const -> StringLiteralValueId {
CARBON_CHECK(index >= InvalidIndex) << *this << " must be handled directly";
return StringLiteralValueId(index);
}

auto Print(llvm::raw_ostream& out) const -> void {
out << "libraryName";
if (*this == Default) {
out << "Default";
} else if (*this == Error) {
out << "<error>";
} else {
IdBase::Print(out);
}
}
};

constexpr LibraryNameId LibraryNameId::Invalid = LibraryNameId(InvalidIndex);
constexpr LibraryNameId LibraryNameId::Default =
LibraryNameId(InvalidIndex - 1);
constexpr LibraryNameId LibraryNameId::Error = LibraryNameId(InvalidIndex - 2);

// The ID of an ImportIRInst.
struct ImportIRInstId : public IdBase, public Printable<ImportIRInstId> {
using ValueType = ImportIRInst;
Expand Down
Loading