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

Code before:
// The ID of a `Specific`, which is the result of specifying the generic
// arguments for a generic.
struct SpecificId : public IdBase<SpecificId> {
  using DiagnosticType = Diagnostics::TypeInfo<std::string>;

  static constexpr llvm::StringLiteral Label = "specific";

  using IdBase::IdBase;
};

// The ID of a `SpecificInterface`, which is an interface and a specific pair.
struct SpecificInterfaceId : public IdBase<SpecificInterfaceId> {
  using DiagnosticType = Diagnostics::TypeInfo<std::string>;

  static constexpr llvm::StringLiteral Label = "specific_interface";

  using IdBase::IdBase;

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

// The index of an instruction that depends on generic parameters within a
// region of a generic. A corresponding specific version of the instruction can
// be found in each specific corresponding to that generic. This is a pair of a
// region and an index, stored in 32 bits.
struct GenericInstIndex : public IndexBase<GenericInstIndex> {
  // Where the value is first used within the generic.
  enum Region : uint8_t {
    // In the declaration.
    Declaration,
    // In the definition.
    Definition,
  };

  // An index with no value.
  static const GenericInstIndex None;

  explicit constexpr GenericInstIndex(Region region, int32_t index)
      : IndexBase(region == Declaration ? index
                                        : FirstDefinitionIndex - index) {
    CARBON_CHECK(index >= 0);
  }

  // Returns the index of the instruction within the region.
  auto index() const -> int32_t {
    CARBON_CHECK(has_value());
    return IndexBase::index >= 0 ? IndexBase::index
                                 : FirstDefinitionIndex - IndexBase::index;
  }

  // Returns the region within which this instruction was first used.

Review comment (performance):
As noted on #toolchain, WDYT of changing all ids to print as hex instead of doing that one-by-one? It shouldn't be a significant behavioral change because it doesn't affect construction, just cuts complexity from IdBase and lldbinit.py to remove decimal printing support (which, if you continue down this route, I assume would be the end goal)

Code after:
// The ID of a `Generic`.
struct GenericId : public IdBase<GenericId> {
  static constexpr llvm::StringLiteral Label = "generic";

  using IdBase::IdBase;
};

// The ID of a `Specific`, which is the result of specifying the generic
// arguments for a generic.
struct SpecificId : public IdBase<SpecificId> {
  using DiagnosticType = Diagnostics::TypeInfo<std::string>;

  static constexpr llvm::StringLiteral Label = "specific";

  using IdBase::IdBase;
};

// The ID of a `SpecificInterface`, which is an interface and a specific pair.
struct SpecificInterfaceId : public IdBase<SpecificInterfaceId> {
  using DiagnosticType = Diagnostics::TypeInfo<std::string>;

  static constexpr llvm::StringLiteral Label = "specific_interface";

  using IdBase::IdBase;

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

// The index of an instruction that depends on generic parameters within a
// region of a generic. A corresponding specific version of the instruction can
// be found in each specific corresponding to that generic. This is a pair of a
// region and an index, stored in 32 bits.
struct GenericInstIndex : public IndexBase<GenericInstIndex> {
  // Where the value is first used within the generic.
  enum Region : uint8_t {
    // In the declaration.
    Declaration,
    // In the definition.
    Definition,
  };

  // An index with no value.
  static const GenericInstIndex None;

  explicit constexpr GenericInstIndex(Region region, int32_t index)
      : IndexBase(region == Declaration ? index
                                        : FirstDefinitionIndex - index) {
    CARBON_CHECK(index >= 0);
  }

  // Returns the index of the instruction within the region.