Repository: carbon-language/carbon-lang
File: toolchain/base/value_ids.h (C/C++)

Code before:
  static constexpr llvm::StringLiteral Label = "real";
  static const RealId None;
  using IdBase::IdBase;
};
constexpr RealId RealId::None(RealId::NoneIndex);

// Corresponds to StringRefs for identifiers.
//
// `NameId` relies on the values of this type other than `None` all being
// non-negative.
struct IdentifierId : public IdBase<IdentifierId> {
  static constexpr llvm::StringLiteral Label = "identifier";
  static const IdentifierId None;
  using IdBase::IdBase;
};
constexpr IdentifierId IdentifierId::None(IdentifierId::NoneIndex);

// The name of a package, which is either an identifier or the special `Core`
// package name.
//
// TODO: Consider also treating `Main` and `Cpp` as special package names.
struct PackageNameId : public IdBase<PackageNameId> {
  static constexpr llvm::StringLiteral Label = "package";
  static const PackageNameId None;
  static const PackageNameId Core;

  // Returns the PackageNameId corresponding to a particular IdentifierId.
  static auto ForIdentifier(IdentifierId id) -> PackageNameId {
    return PackageNameId(id.index);
  }

  using IdBase::IdBase;

  // Returns the IdentifierId corresponding to this PackageNameId, or `None` if
  // this is a special package name.
  auto AsIdentifierId() const -> IdentifierId {
    return index >= 0 ? IdentifierId(index) : IdentifierId::None;
  }

  // Returns the special package name corresponding to this PackageNameId.
  // Requires that this name is not an identifier name.
  auto AsSpecialName() const -> llvm::StringLiteral {
    if (*this == None) {
      return "Main";
    }
    if (*this == Core) {
      return "Core";
    }
    CARBON_FATAL("Unknown special package name kind {0}", *this);
  }
};

Review comment (suggestion):
Looking at this, there's already a TODO in parse/handle_import_and_package.cpp to change `Cpp` to behave closer to `Core`. I think that's better than a `StringLiteral` here; I'll approve since the change otherwise seems fine and I can just do that fix on top.

Code after:
  static constexpr llvm::StringLiteral Label = "real";
  static const RealId None;
  using IdBase::IdBase;
};
constexpr RealId RealId::None(RealId::NoneIndex);

// Corresponds to StringRefs for identifiers.
//
// `NameId` relies on the values of this type other than `None` all being
// non-negative.
struct IdentifierId : public IdBase<IdentifierId> {
  static constexpr llvm::StringLiteral Label = "identifier";
  static const IdentifierId None;
  using IdBase::IdBase;
};
constexpr IdentifierId IdentifierId::None(IdentifierId::NoneIndex);

// The name of a package, which is either an identifier or the special `Core`
// package name.
//
// TODO: Consider also treating `Main` and `Cpp` as special package names.
struct PackageNameId : public IdBase<PackageNameId> {
  static constexpr llvm::StringLiteral Label = "package";
  static const PackageNameId None;
  static const PackageNameId Core;
  static constexpr llvm::StringLiteral CppName = "Cpp";

  // Returns the PackageNameId corresponding to a particular IdentifierId.
  static auto ForIdentifier(IdentifierId id) -> PackageNameId {
    return PackageNameId(id.index);
  }

  using IdBase::IdBase;

  // Returns the IdentifierId corresponding to this PackageNameId, or `None` if
  // this is a special package name.
  auto AsIdentifierId() const -> IdentifierId {
    return index >= 0 ? IdentifierId(index) : IdentifierId::None;
  }

  // Returns the special package name corresponding to this PackageNameId.
  // Requires that this name is not an identifier name.
  auto AsSpecialName() const -> llvm::StringLiteral {
    if (*this == None) {
      return "Main";
    }
    if (*this == Core) {
      return "Core";
    }
    CARBON_FATAL("Unknown special package name kind {0}", *this);
  }