ids.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_SEM_IR_IDS_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_IDS_H_
  6. #include "common/check.h"
  7. #include "common/ostream.h"
  8. #include "toolchain/base/index_base.h"
  9. #include "toolchain/base/value_store.h"
  10. #include "toolchain/diagnostics/diagnostic_emitter.h"
  11. #include "toolchain/parse/node_ids.h"
  12. #include "toolchain/sem_ir/builtin_kind.h"
  13. namespace Carbon::SemIR {
  14. // Forward declare indexed types, for integration with ValueStore.
  15. class File;
  16. class Inst;
  17. struct BindNameInfo;
  18. struct Class;
  19. struct Function;
  20. struct Generic;
  21. struct GenericInstance;
  22. struct ImportIR;
  23. struct ImportIRInst;
  24. struct Interface;
  25. struct Impl;
  26. struct NameScope;
  27. struct TypeInfo;
  28. // The ID of an instruction.
  29. struct InstId : public IdBase, public Printable<InstId> {
  30. using ValueType = Inst;
  31. // An explicitly invalid ID.
  32. static const InstId Invalid;
  33. // Builtin instruction IDs.
  34. #define CARBON_SEM_IR_BUILTIN_KIND_NAME(Name) static const InstId Builtin##Name;
  35. #include "toolchain/sem_ir/builtin_kind.def"
  36. // The namespace for a `package` expression.
  37. static const InstId PackageNamespace;
  38. // Returns the instruction ID for a builtin. This relies on File guarantees
  39. // for builtin placement.
  40. static constexpr auto ForBuiltin(BuiltinKind kind) -> InstId {
  41. return InstId(kind.AsInt());
  42. }
  43. using IdBase::IdBase;
  44. // Returns true if the instruction is a builtin. Requires is_valid.
  45. auto is_builtin() const -> bool {
  46. CARBON_CHECK(is_valid());
  47. return index < BuiltinKind::ValidCount;
  48. }
  49. // Returns the BuiltinKind. Requires is_builtin.
  50. auto builtin_kind() const -> BuiltinKind {
  51. CARBON_CHECK(is_builtin());
  52. return BuiltinKind::FromInt(index);
  53. }
  54. auto Print(llvm::raw_ostream& out) const -> void {
  55. out << "inst";
  56. if (!is_valid()) {
  57. IdBase::Print(out);
  58. } else if (is_builtin()) {
  59. out << builtin_kind();
  60. } else {
  61. // Use the `+` as a small reminder that this is a delta, rather than an
  62. // absolute index.
  63. out << "+" << index - BuiltinKind::ValidCount;
  64. }
  65. }
  66. };
  67. constexpr InstId InstId::Invalid = InstId(InvalidIndex);
  68. #define CARBON_SEM_IR_BUILTIN_KIND_NAME(Name) \
  69. constexpr InstId InstId::Builtin##Name = \
  70. InstId::ForBuiltin(BuiltinKind::Name);
  71. #include "toolchain/sem_ir/builtin_kind.def"
  72. // The package namespace will be the instruction after builtins.
  73. constexpr InstId InstId::PackageNamespace = InstId(BuiltinKind::ValidCount);
  74. // The ID of a constant value of an expression. An expression is either:
  75. //
  76. // - a template constant, with an immediate value, such as `42` or `i32*` or
  77. // `("hello", "world")`, or
  78. // - a symbolic constant, whose value includes a symbolic parameter, such as
  79. // `Vector(T*)`, or
  80. // - a runtime expression, such as `Print("hello")`.
  81. struct ConstantId : public IdBase, public Printable<ConstantId> {
  82. // An ID for an expression that is not constant.
  83. static const ConstantId NotConstant;
  84. // An ID for an expression whose phase cannot be determined because it
  85. // contains an error. This is always modeled as a template constant.
  86. static const ConstantId Error;
  87. // An explicitly invalid ID.
  88. static const ConstantId Invalid;
  89. // Returns the constant ID corresponding to a template constant, which should
  90. // either be in the `constants` block in the file or should be known to be
  91. // unique.
  92. static constexpr auto ForTemplateConstant(InstId const_id) -> ConstantId {
  93. return ConstantId(const_id.index + IndexOffset);
  94. }
  95. // Returns the constant ID corresponding to a symbolic constant, which should
  96. // either be in the `constants` block in the file or should be known to be
  97. // unique.
  98. static constexpr auto ForSymbolicConstant(InstId const_id) -> ConstantId {
  99. return ConstantId(-const_id.index - IndexOffset);
  100. }
  101. using IdBase::IdBase;
  102. // Returns whether this represents a constant. Requires is_valid.
  103. auto is_constant() const -> bool {
  104. CARBON_CHECK(is_valid());
  105. return *this != ConstantId::NotConstant;
  106. }
  107. // Returns whether this represents a symbolic constant. Requires is_valid.
  108. auto is_symbolic() const -> bool {
  109. CARBON_CHECK(is_valid());
  110. return index <= -IndexOffset;
  111. }
  112. // Returns whether this represents a template constant. Requires is_valid.
  113. auto is_template() const -> bool {
  114. CARBON_CHECK(is_valid());
  115. return index >= IndexOffset;
  116. }
  117. auto Print(llvm::raw_ostream& out) const -> void {
  118. if (!is_valid()) {
  119. IdBase::Print(out);
  120. } else if (is_template()) {
  121. out << "template " << inst_id();
  122. } else if (is_symbolic()) {
  123. out << "symbolic " << inst_id();
  124. } else {
  125. out << "runtime";
  126. }
  127. }
  128. private:
  129. friend class ConstantValueStore;
  130. // TODO: C++23 makes std::abs constexpr, but until then we mirror std::abs
  131. // logic here. LLVM should still optimize this.
  132. static constexpr auto Abs(int32_t i) -> int32_t { return i > 0 ? i : -i; }
  133. // Returns the instruction that describes this constant value, or
  134. // InstId::Invalid for a runtime value. This is not part of the public
  135. // interface of `ConstantId`. Use `ConstantValueStore::GetInstId` to get the
  136. // instruction ID of a `ConstantId`.
  137. constexpr auto inst_id() const -> InstId {
  138. CARBON_CHECK(is_valid());
  139. return InstId(Abs(index) - IndexOffset);
  140. }
  141. static constexpr int32_t NotConstantIndex = InvalidIndex - 1;
  142. // The offset of InstId indices to ConstantId indices.
  143. static constexpr int32_t IndexOffset = -NotConstantIndex + 1;
  144. };
  145. constexpr ConstantId ConstantId::NotConstant = ConstantId(NotConstantIndex);
  146. constexpr ConstantId ConstantId::Error =
  147. ConstantId::ForTemplateConstant(InstId::BuiltinError);
  148. constexpr ConstantId ConstantId::Invalid = ConstantId(InvalidIndex);
  149. // The ID of a bind name.
  150. struct BindNameId : public IdBase, public Printable<BindNameId> {
  151. using ValueType = BindNameInfo;
  152. // An explicitly invalid ID.
  153. static const BindNameId Invalid;
  154. using IdBase::IdBase;
  155. auto Print(llvm::raw_ostream& out) const -> void {
  156. out << "bindName";
  157. IdBase::Print(out);
  158. }
  159. };
  160. constexpr BindNameId BindNameId::Invalid = BindNameId(InvalidIndex);
  161. // The index of a compile-time binding. This is the de Bruijn level for the
  162. // binding -- that is, this is the number of other compile time bindings whose
  163. // scope encloses this binding.
  164. struct CompileTimeBindIndex : public IndexBase,
  165. public Printable<CompileTimeBindIndex> {
  166. // An explicitly invalid index.
  167. static const CompileTimeBindIndex Invalid;
  168. using IndexBase::IndexBase;
  169. auto Print(llvm::raw_ostream& out) const -> void {
  170. out << "compTimeBind";
  171. IndexBase::Print(out);
  172. }
  173. };
  174. constexpr CompileTimeBindIndex CompileTimeBindIndex::Invalid =
  175. CompileTimeBindIndex(InvalidIndex);
  176. // Note that InvalidIndex - 1 and InvalidIndex - 2 are used by
  177. // DenseMapInfo<BindNameInfo>.
  178. // The ID of a function.
  179. struct FunctionId : public IdBase, public Printable<FunctionId> {
  180. using ValueType = Function;
  181. // An explicitly invalid ID.
  182. static const FunctionId Invalid;
  183. using IdBase::IdBase;
  184. auto Print(llvm::raw_ostream& out) const -> void {
  185. out << "function";
  186. IdBase::Print(out);
  187. }
  188. };
  189. constexpr FunctionId FunctionId::Invalid = FunctionId(InvalidIndex);
  190. // The ID of an IR within the set of all IRs being evaluated in the current
  191. // check execution.
  192. struct CheckIRId : public IdBase, public Printable<CheckIRId> {
  193. using IdBase::IdBase;
  194. auto Print(llvm::raw_ostream& out) const -> void {
  195. out << "check_ir";
  196. IdBase::Print(out);
  197. }
  198. };
  199. // The ID of a class.
  200. struct ClassId : public IdBase, public Printable<ClassId> {
  201. using ValueType = Class;
  202. // An explicitly invalid ID.
  203. static const ClassId Invalid;
  204. using IdBase::IdBase;
  205. auto Print(llvm::raw_ostream& out) const -> void {
  206. out << "class";
  207. IdBase::Print(out);
  208. }
  209. };
  210. constexpr ClassId ClassId::Invalid = ClassId(InvalidIndex);
  211. // The ID of an interface.
  212. struct InterfaceId : public IdBase, public Printable<InterfaceId> {
  213. using ValueType = Interface;
  214. // An explicitly invalid ID.
  215. static const InterfaceId Invalid;
  216. using IdBase::IdBase;
  217. auto Print(llvm::raw_ostream& out) const -> void {
  218. out << "interface";
  219. IdBase::Print(out);
  220. }
  221. };
  222. constexpr InterfaceId InterfaceId::Invalid = InterfaceId(InvalidIndex);
  223. // The ID of an impl.
  224. struct ImplId : public IdBase, public Printable<ImplId> {
  225. using ValueType = Impl;
  226. // An explicitly invalid ID.
  227. static const ImplId Invalid;
  228. using IdBase::IdBase;
  229. auto Print(llvm::raw_ostream& out) const -> void {
  230. out << "impl";
  231. IdBase::Print(out);
  232. }
  233. };
  234. constexpr ImplId ImplId::Invalid = ImplId(InvalidIndex);
  235. // The ID of a generic.
  236. struct GenericId : public IdBase, public Printable<GenericId> {
  237. using ValueType = Generic;
  238. // An explicitly invalid ID.
  239. static const GenericId Invalid;
  240. using IdBase::IdBase;
  241. auto Print(llvm::raw_ostream& out) const -> void {
  242. out << "generic";
  243. IdBase::Print(out);
  244. }
  245. };
  246. constexpr GenericId GenericId::Invalid = GenericId(InvalidIndex);
  247. // The ID of an instance of a generic.
  248. struct GenericInstanceId : public IdBase, public Printable<GenericInstanceId> {
  249. using ValueType = GenericInstance;
  250. // An explicitly invalid ID. This is typically used to represent a non-generic
  251. // instance.
  252. static const GenericInstanceId Invalid;
  253. using IdBase::IdBase;
  254. auto Print(llvm::raw_ostream& out) const -> void {
  255. out << "genericInstance";
  256. IdBase::Print(out);
  257. }
  258. };
  259. constexpr GenericInstanceId GenericInstanceId::Invalid =
  260. GenericInstanceId(InvalidIndex);
  261. // The ID of an IR within the set of imported IRs, both direct and indirect.
  262. struct ImportIRId : public IdBase, public Printable<ImportIRId> {
  263. using ValueType = ImportIR;
  264. // An explicitly invalid ID.
  265. static const ImportIRId Invalid;
  266. // The implicit `api` import, for an `impl` file. A null entry is added if
  267. // there is none, as in an `api`, in which case this ID should not show up in
  268. // instructions.
  269. static const ImportIRId ApiForImpl;
  270. using IdBase::IdBase;
  271. auto Print(llvm::raw_ostream& out) const -> void {
  272. out << "ir";
  273. IdBase::Print(out);
  274. }
  275. };
  276. constexpr ImportIRId ImportIRId::Invalid = ImportIRId(InvalidIndex);
  277. constexpr ImportIRId ImportIRId::ApiForImpl = ImportIRId(0);
  278. // A boolean value.
  279. struct BoolValue : public IdBase, public Printable<BoolValue> {
  280. static const BoolValue False;
  281. static const BoolValue True;
  282. // Returns the `BoolValue` corresponding to `b`.
  283. static constexpr auto From(bool b) -> BoolValue { return b ? True : False; }
  284. // Returns the `bool` corresponding to this `BoolValue`.
  285. constexpr auto ToBool() -> bool {
  286. CARBON_CHECK(*this == False || *this == True)
  287. << "Invalid bool value " << index;
  288. return *this != False;
  289. }
  290. using IdBase::IdBase;
  291. auto Print(llvm::raw_ostream& out) const -> void {
  292. if (*this == False) {
  293. out << "false";
  294. } else if (*this == True) {
  295. out << "true";
  296. } else {
  297. CARBON_FATAL() << "Invalid bool value " << index;
  298. }
  299. }
  300. };
  301. constexpr BoolValue BoolValue::False = BoolValue(0);
  302. constexpr BoolValue BoolValue::True = BoolValue(1);
  303. // An integer kind value -- either "signed" or "unsigned".
  304. //
  305. // This might eventually capture any other properties of an integer type that
  306. // affect its semantics, such as overflow behavior.
  307. struct IntKind : public IdBase, public Printable<IntKind> {
  308. static const IntKind Unsigned;
  309. static const IntKind Signed;
  310. using IdBase::IdBase;
  311. // Returns whether this type is signed.
  312. constexpr auto is_signed() -> bool { return *this == Signed; }
  313. auto Print(llvm::raw_ostream& out) const -> void {
  314. if (*this == Unsigned) {
  315. out << "unsigned";
  316. } else if (*this == Signed) {
  317. out << "signed";
  318. } else {
  319. CARBON_FATAL() << "Invalid int kind value " << index;
  320. }
  321. }
  322. };
  323. constexpr IntKind IntKind::Unsigned = IntKind(0);
  324. constexpr IntKind IntKind::Signed = IntKind(1);
  325. // A float kind value
  326. struct FloatKind : public IdBase, public Printable<FloatKind> {
  327. using IdBase::IdBase;
  328. auto Print(llvm::raw_ostream& out) const -> void { out << "float"; }
  329. };
  330. // The ID of a name. A name is either a string or a special name such as
  331. // `self`, `Self`, or `base`.
  332. struct NameId : public IdBase, public Printable<NameId> {
  333. // names().GetFormatted() is used for diagnostics.
  334. using DiagnosticType = DiagnosticTypeInfo<std::string>;
  335. // An explicitly invalid ID.
  336. static const NameId Invalid;
  337. // The name of `self`.
  338. static const NameId SelfValue;
  339. // The name of `Self`.
  340. static const NameId SelfType;
  341. // The name of the return slot in a function.
  342. static const NameId ReturnSlot;
  343. // The name of `package`.
  344. static const NameId PackageNamespace;
  345. // The name of `base`.
  346. static const NameId Base;
  347. // The number of non-index (<0) that exist, and will need storage in name
  348. // lookup.
  349. static const int NonIndexValueCount;
  350. // Returns the NameId corresponding to a particular IdentifierId.
  351. static auto ForIdentifier(IdentifierId id) -> NameId {
  352. if (id.index >= 0) {
  353. return NameId(id.index);
  354. } else if (!id.is_valid()) {
  355. return NameId::Invalid;
  356. } else {
  357. CARBON_FATAL() << "Unexpected identifier ID " << id;
  358. }
  359. }
  360. using IdBase::IdBase;
  361. // Returns the IdentifierId corresponding to this NameId, or an invalid
  362. // IdentifierId if this is a special name.
  363. auto AsIdentifierId() const -> IdentifierId {
  364. return index >= 0 ? IdentifierId(index) : IdentifierId::Invalid;
  365. }
  366. auto Print(llvm::raw_ostream& out) const -> void {
  367. out << "name";
  368. if (*this == SelfValue) {
  369. out << "SelfValue";
  370. } else if (*this == SelfType) {
  371. out << "SelfType";
  372. } else if (*this == ReturnSlot) {
  373. out << "ReturnSlot";
  374. } else if (*this == PackageNamespace) {
  375. out << "PackageNamespace";
  376. } else if (*this == Base) {
  377. out << "Base";
  378. } else {
  379. CARBON_CHECK(!is_valid() || index >= 0) << "Unknown index " << index;
  380. IdBase::Print(out);
  381. }
  382. }
  383. };
  384. constexpr NameId NameId::Invalid = NameId(InvalidIndex);
  385. constexpr NameId NameId::SelfValue = NameId(InvalidIndex - 1);
  386. constexpr NameId NameId::SelfType = NameId(InvalidIndex - 2);
  387. constexpr NameId NameId::ReturnSlot = NameId(InvalidIndex - 3);
  388. constexpr NameId NameId::PackageNamespace = NameId(InvalidIndex - 4);
  389. constexpr NameId NameId::Base = NameId(InvalidIndex - 5);
  390. constexpr int NameId::NonIndexValueCount = 6;
  391. // Enforce the link between SpecialValueCount and the last special value.
  392. static_assert(NameId::NonIndexValueCount == -NameId::Base.index);
  393. // The ID of a name scope.
  394. struct NameScopeId : public IdBase, public Printable<NameScopeId> {
  395. using ValueType = NameScope;
  396. // An explicitly invalid ID.
  397. static const NameScopeId Invalid;
  398. // The package (or file) name scope, guaranteed to be the first added.
  399. static const NameScopeId Package;
  400. using IdBase::IdBase;
  401. auto Print(llvm::raw_ostream& out) const -> void {
  402. out << "name_scope";
  403. IdBase::Print(out);
  404. }
  405. };
  406. constexpr NameScopeId NameScopeId::Invalid = NameScopeId(InvalidIndex);
  407. constexpr NameScopeId NameScopeId::Package = NameScopeId(0);
  408. // The ID of an instruction block.
  409. struct InstBlockId : public IdBase, public Printable<InstBlockId> {
  410. using ElementType = InstId;
  411. using ValueType = llvm::MutableArrayRef<ElementType>;
  412. // An empty block, reused to avoid allocating empty vectors. Always the
  413. // 0-index block.
  414. static const InstBlockId Empty;
  415. // Exported instructions. Always the 1-index block. Empty until the File is
  416. // fully checked; intermediate state is in the Check::Context.
  417. static const InstBlockId Exports;
  418. // Global declaration initialization instructions. Empty if none are present.
  419. // Otherwise, __global_init function will be generated and this block will
  420. // be inserted into it.
  421. static const InstBlockId GlobalInit;
  422. // An explicitly invalid ID.
  423. static const InstBlockId Invalid;
  424. // An ID for unreachable code.
  425. static const InstBlockId Unreachable;
  426. using IdBase::IdBase;
  427. auto Print(llvm::raw_ostream& out) const -> void {
  428. if (*this == Unreachable) {
  429. out << "unreachable";
  430. } else if (*this == Empty) {
  431. out << "empty";
  432. } else if (*this == Exports) {
  433. out << "exports";
  434. } else if (*this == GlobalInit) {
  435. out << "global_init";
  436. } else {
  437. out << "block";
  438. IdBase::Print(out);
  439. }
  440. }
  441. };
  442. constexpr InstBlockId InstBlockId::Empty = InstBlockId(0);
  443. constexpr InstBlockId InstBlockId::Exports = InstBlockId(1);
  444. constexpr InstBlockId InstBlockId::Invalid = InstBlockId(InvalidIndex);
  445. constexpr InstBlockId InstBlockId::Unreachable = InstBlockId(InvalidIndex - 1);
  446. constexpr InstBlockId InstBlockId::GlobalInit = InstBlockId(2);
  447. // The ID of a type.
  448. struct TypeId : public IdBase, public Printable<TypeId> {
  449. using ValueType = TypeInfo;
  450. // StringifyType() is used for diagnostics.
  451. using DiagnosticType = DiagnosticTypeInfo<std::string>;
  452. // The builtin TypeType.
  453. static const TypeId TypeType;
  454. // The builtin Error.
  455. static const TypeId Error;
  456. // An explicitly invalid ID.
  457. static const TypeId Invalid;
  458. using IdBase::IdBase;
  459. auto Print(llvm::raw_ostream& out) const -> void {
  460. out << "type";
  461. if (*this == TypeType) {
  462. out << "TypeType";
  463. } else if (*this == Error) {
  464. out << "Error";
  465. } else {
  466. IdBase::Print(out);
  467. }
  468. }
  469. };
  470. constexpr TypeId TypeId::TypeType = TypeId(InvalidIndex - 2);
  471. constexpr TypeId TypeId::Error = TypeId(InvalidIndex - 1);
  472. constexpr TypeId TypeId::Invalid = TypeId(InvalidIndex);
  473. // The ID of a type block.
  474. struct TypeBlockId : public IdBase, public Printable<TypeBlockId> {
  475. using ElementType = TypeId;
  476. using ValueType = llvm::MutableArrayRef<ElementType>;
  477. // An explicitly invalid ID.
  478. static const TypeBlockId Invalid;
  479. using IdBase::IdBase;
  480. auto Print(llvm::raw_ostream& out) const -> void {
  481. out << "typeBlock";
  482. IdBase::Print(out);
  483. }
  484. };
  485. constexpr TypeBlockId TypeBlockId::Invalid = TypeBlockId(InvalidIndex);
  486. // An index for element access, for structs, tuples, and classes.
  487. struct ElementIndex : public IndexBase, public Printable<ElementIndex> {
  488. using IndexBase::IndexBase;
  489. auto Print(llvm::raw_ostream& out) const -> void {
  490. out << "element";
  491. IndexBase::Print(out);
  492. }
  493. };
  494. // The ID of an ImportIRInst.
  495. struct ImportIRInstId : public IdBase, public Printable<InstId> {
  496. using ValueType = ImportIRInst;
  497. // An explicitly invalid ID.
  498. static const ImportIRInstId Invalid;
  499. using IdBase::IdBase;
  500. };
  501. constexpr ImportIRInstId ImportIRInstId::Invalid = ImportIRInstId(InvalidIndex);
  502. // A SemIR location used exclusively for diagnostic locations.
  503. //
  504. // Contents:
  505. // - index > Invalid: A Parse::NodeId in the current IR.
  506. // - index < Invalid: An ImportIRInstId.
  507. // - index == Invalid: Can be used for either.
  508. struct LocId : public IdBase, public Printable<LocId> {
  509. // An explicitly invalid ID.
  510. static const LocId Invalid;
  511. using IdBase::IdBase;
  512. // NOLINTNEXTLINE(google-explicit-constructor)
  513. constexpr LocId(Parse::InvalidNodeId /*invalid*/) : IdBase(InvalidIndex) {}
  514. // NOLINTNEXTLINE(google-explicit-constructor)
  515. constexpr LocId(Parse::NodeId node_id) : IdBase(node_id.index) {
  516. CARBON_CHECK(node_id.is_valid() == is_valid());
  517. }
  518. // NOLINTNEXTLINE(google-explicit-constructor)
  519. constexpr LocId(ImportIRInstId inst_id)
  520. : IdBase(InvalidIndex + ImportIRInstId::InvalidIndex - inst_id.index) {
  521. CARBON_CHECK(inst_id.is_valid() == is_valid());
  522. }
  523. auto is_node_id() const -> bool { return index > InvalidIndex; }
  524. auto is_import_ir_inst_id() const -> bool { return index < InvalidIndex; }
  525. // This is allowed to return an invalid NodeId, but should never be used for a
  526. // valid InstId.
  527. auto node_id() const -> Parse::NodeId {
  528. CARBON_CHECK(is_node_id() || !is_valid());
  529. return Parse::NodeId(index);
  530. }
  531. // This is allowed to return an invalid InstId, but should never be used for a
  532. // valid NodeId.
  533. auto import_ir_inst_id() const -> ImportIRInstId {
  534. CARBON_CHECK(is_import_ir_inst_id() || !is_valid());
  535. return ImportIRInstId(InvalidIndex + ImportIRInstId::InvalidIndex - index);
  536. }
  537. auto Print(llvm::raw_ostream& out) const -> void {
  538. out << "loc_";
  539. if (is_node_id() || !is_valid()) {
  540. out << node_id();
  541. } else {
  542. out << import_ir_inst_id();
  543. }
  544. }
  545. };
  546. constexpr LocId LocId::Invalid = LocId(Parse::NodeId::Invalid);
  547. } // namespace Carbon::SemIR
  548. // Support use of Id types as DenseMap/DenseSet keys.
  549. template <>
  550. struct llvm::DenseMapInfo<Carbon::SemIR::ConstantId>
  551. : public Carbon::IndexMapInfo<Carbon::SemIR::ConstantId> {};
  552. template <>
  553. struct llvm::DenseMapInfo<Carbon::SemIR::InstBlockId>
  554. : public Carbon::IndexMapInfo<Carbon::SemIR::InstBlockId> {};
  555. template <>
  556. struct llvm::DenseMapInfo<Carbon::SemIR::InstId>
  557. : public Carbon::IndexMapInfo<Carbon::SemIR::InstId> {};
  558. template <>
  559. struct llvm::DenseMapInfo<Carbon::SemIR::NameId>
  560. : public Carbon::IndexMapInfo<Carbon::SemIR::NameId> {};
  561. template <>
  562. struct llvm::DenseMapInfo<Carbon::SemIR::NameScopeId>
  563. : public Carbon::IndexMapInfo<Carbon::SemIR::NameScopeId> {};
  564. template <>
  565. struct llvm::DenseMapInfo<Carbon::SemIR::TypeId>
  566. : public Carbon::IndexMapInfo<Carbon::SemIR::TypeId> {};
  567. #endif // CARBON_TOOLCHAIN_SEM_IR_IDS_H_