Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/ids.h (C/C++)

Code before:
  auto Print(llvm::raw_ostream& out) const -> void {
    out << "element";
    IndexBase::Print(out);
  }
};

constexpr ElementIndex ElementIndex::Invalid = ElementIndex(InvalidIndex);

// 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 {0}",
                 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, "{0} must be handled directly", *this);
    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);
    }
  }

Review comment (suggestion):
```suggestion
// Polymorphic id for fields in `Any[...]` typed instruction categories. Used for
// fields where the specific instruction structs have different field types in
// that position or do not have a field in that position at all. Allows
// conversion with `Inst::As<>` from the specific typed instruction to the
// `Any[...]` instruction category.
//
// This type participates in `Inst::FromRaw` in order to convert from specific
// instructions to an `Any[...]` instruction category:
// - In the case the specific instruction has a field of some `IdKind` in the
//   same position, the `Any[...]` type will hold its raw value in the
//   `AnyRawId` field.
// - In the case the specific instruction has no field in the same position, the
//   `Any[...]` type will hold a default constructed `AnyRawId` with an invalid
//   value.
```
(To match terminology we use elsewhere.)

Code after:
  // This is allowed to return an invalid InstId, but should never be used for a
  // valid NodeId.
  auto import_ir_inst_id() const -> ImportIRInstId {
    CARBON_CHECK(is_import_ir_inst_id() || !is_valid());
    return ImportIRInstId(InvalidIndex + ImportIRInstId::InvalidIndex - index);
  }

  auto Print(llvm::raw_ostream& out) const -> void;
};

constexpr LocId LocId::Invalid = LocId(Parse::NodeId::Invalid);

// Polymorphic id for fields in `Any[...]` typed instruction category. Used for
// fields where the specific instruction structs have different field types in
// that position or do not have a field in that position at all. Allows
// conversion with `Inst::As<>` from the specific typed instruction to the
// `Any[...]` instruction category.
//
// This type participates in `Inst::FromRaw` in order to convert from specific
// instructions to an `Any[...]` instruction category:
// - In the case the specific instruction has a field of some `IdKind` in the
//   same position, the `Any[...]` type will hold its raw value in the
//   `AnyRawId` field.
// - In the case the specific instruction has no field in the same position, the
//   `Any[...]` type will hold a default constructed `AnyRawId` with an invalid
//   value.
struct AnyRawId : public IdBase {
  constexpr AnyRawId() : IdBase(IdBase::InvalidIndex) {}
  constexpr explicit AnyRawId(int32_t id) : IdBase(id) {}
};

}  // namespace Carbon::SemIR

#endif  // CARBON_TOOLCHAIN_SEM_IR_IDS_H_