ids.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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_inst_kind.h"
  13. namespace Carbon::SemIR {
  14. // Forward declare indexed types, for integration with ValueStore.
  15. class File;
  16. class Inst;
  17. struct EntityName;
  18. struct Class;
  19. struct Function;
  20. struct Generic;
  21. struct Specific;
  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. // BuiltinInst IDs.
  34. #define CARBON_SEM_IR_BUILTIN_INST_KIND_NAME(Name) \
  35. static const InstId Builtin##Name;
  36. #include "toolchain/sem_ir/builtin_inst_kind.def"
  37. // The namespace for a `package` expression.
  38. static const InstId PackageNamespace;
  39. // Returns the instruction ID for a builtin. This relies on File guarantees
  40. // for builtin placement.
  41. static constexpr auto ForBuiltin(BuiltinInstKind kind) -> InstId {
  42. return InstId(kind.AsInt());
  43. }
  44. using IdBase::IdBase;
  45. // Returns true if the instruction is a builtin. Requires is_valid.
  46. auto is_builtin() const -> bool {
  47. CARBON_CHECK(is_valid());
  48. return index < BuiltinInstKind::ValidCount;
  49. }
  50. // Returns the BuiltinInstKind. Requires is_builtin.
  51. auto builtin_inst_kind() const -> BuiltinInstKind {
  52. CARBON_CHECK(is_builtin());
  53. return BuiltinInstKind::FromInt(index);
  54. }
  55. auto Print(llvm::raw_ostream& out) const -> void {
  56. out << "inst";
  57. if (!is_valid()) {
  58. IdBase::Print(out);
  59. } else if (is_builtin()) {
  60. out << builtin_inst_kind();
  61. } else {
  62. // Use the `+` as a small reminder that this is a delta, rather than an
  63. // absolute index.
  64. out << "+" << index - BuiltinInstKind::ValidCount;
  65. }
  66. }
  67. };
  68. constexpr InstId InstId::Invalid = InstId(InvalidIndex);
  69. #define CARBON_SEM_IR_BUILTIN_INST_KIND_NAME(Name) \
  70. constexpr InstId InstId::Builtin##Name = \
  71. InstId::ForBuiltin(BuiltinInstKind::Name);
  72. #include "toolchain/sem_ir/builtin_inst_kind.def"
  73. // The package namespace will be the instruction after builtins.
  74. constexpr InstId InstId::PackageNamespace = InstId(BuiltinInstKind::ValidCount);
  75. // The ID of a constant value of an expression. An expression is either:
  76. //
  77. // - a template constant, with an immediate value, such as `42` or `i32*` or
  78. // `("hello", "world")`, or
  79. // - a symbolic constant, whose value includes a symbolic parameter, such as
  80. // `Vector(T*)`, or
  81. // - a runtime expression, such as `Print("hello")`.
  82. //
  83. // Template constants are a thin wrapper around the instruction ID of the
  84. // constant instruction that defines the constant. Symbolic constants are an
  85. // index into a separate table of `SymbolicConstant`s maintained by the constant
  86. // value store.
  87. struct ConstantId : public IdBase, public Printable<ConstantId> {
  88. // An ID for an expression that is not constant.
  89. static const ConstantId NotConstant;
  90. // An ID for an expression whose phase cannot be determined because it
  91. // contains an error. This is always modeled as a template constant.
  92. static const ConstantId Error;
  93. // An explicitly invalid ID.
  94. static const ConstantId Invalid;
  95. // Returns the constant ID corresponding to a template 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 ForTemplateConstant(InstId const_id) -> ConstantId {
  99. return ConstantId(const_id.index);
  100. }
  101. // Returns the constant ID corresponding to a symbolic constant index.
  102. static constexpr auto ForSymbolicConstantIndex(int32_t symbolic_index)
  103. -> ConstantId {
  104. return ConstantId(FirstSymbolicIndex - symbolic_index);
  105. }
  106. using IdBase::IdBase;
  107. // Returns whether this represents a constant. Requires is_valid.
  108. auto is_constant() const -> bool {
  109. CARBON_DCHECK(is_valid());
  110. return *this != ConstantId::NotConstant;
  111. }
  112. // Returns whether this represents a symbolic constant. Requires is_valid.
  113. auto is_symbolic() const -> bool {
  114. CARBON_DCHECK(is_valid());
  115. return index <= FirstSymbolicIndex;
  116. }
  117. // Returns whether this represents a template constant. Requires is_valid.
  118. auto is_template() const -> bool {
  119. CARBON_DCHECK(is_valid());
  120. return index >= 0;
  121. }
  122. // Prints this ID to the given output stream. `disambiguate` indicates whether
  123. // template constants should be wrapped with "templateConstant(...)" so that
  124. // they aren't printed the same as an InstId. This can be set to false if
  125. // there is no risk of ambiguity.
  126. auto Print(llvm::raw_ostream& out, bool disambiguate = true) const -> void {
  127. if (!is_valid()) {
  128. IdBase::Print(out);
  129. } else if (is_template()) {
  130. if (disambiguate) {
  131. out << "templateConstant(";
  132. }
  133. out << template_inst_id();
  134. if (disambiguate) {
  135. out << ")";
  136. }
  137. } else if (is_symbolic()) {
  138. out << "symbolicConstant" << symbolic_index();
  139. } else {
  140. out << "runtime";
  141. }
  142. }
  143. private:
  144. friend class ConstantValueStore;
  145. // TODO: C++23 makes std::abs constexpr, but until then we mirror std::abs
  146. // logic here. LLVM should still optimize this.
  147. static constexpr auto Abs(int32_t i) -> int32_t { return i > 0 ? i : -i; }
  148. // Returns the instruction that describes this template constant value.
  149. // Requires `is_template()`. Use `ConstantValueStore::GetInstId` to get the
  150. // instruction ID of a `ConstantId`.
  151. constexpr auto template_inst_id() const -> InstId {
  152. CARBON_DCHECK(is_template());
  153. return InstId(index);
  154. }
  155. // Returns the symbolic constant index that describes this symbolic constant
  156. // value. Requires `is_symbolic()`.
  157. constexpr auto symbolic_index() const -> int32_t {
  158. CARBON_DCHECK(is_symbolic());
  159. return FirstSymbolicIndex - index;
  160. }
  161. static constexpr int32_t NotConstantIndex = InvalidIndex - 1;
  162. static constexpr int32_t FirstSymbolicIndex = InvalidIndex - 2;
  163. };
  164. constexpr ConstantId ConstantId::NotConstant = ConstantId(NotConstantIndex);
  165. constexpr ConstantId ConstantId::Error =
  166. ConstantId::ForTemplateConstant(InstId::BuiltinError);
  167. constexpr ConstantId ConstantId::Invalid = ConstantId(InvalidIndex);
  168. // The ID of a EntityName.
  169. struct EntityNameId : public IdBase, public Printable<EntityNameId> {
  170. using ValueType = EntityName;
  171. // An explicitly invalid ID.
  172. static const EntityNameId Invalid;
  173. using IdBase::IdBase;
  174. auto Print(llvm::raw_ostream& out) const -> void {
  175. out << "entity_name";
  176. IdBase::Print(out);
  177. }
  178. };
  179. constexpr EntityNameId EntityNameId::Invalid = EntityNameId(InvalidIndex);
  180. // The index of a compile-time binding. This is the de Bruijn level for the
  181. // binding -- that is, this is the number of other compile time bindings whose
  182. // scope encloses this binding.
  183. struct CompileTimeBindIndex : public IndexBase,
  184. public Printable<CompileTimeBindIndex> {
  185. // An explicitly invalid index.
  186. static const CompileTimeBindIndex Invalid;
  187. using IndexBase::IndexBase;
  188. auto Print(llvm::raw_ostream& out) const -> void {
  189. out << "comp_time_bind";
  190. IndexBase::Print(out);
  191. }
  192. };
  193. constexpr CompileTimeBindIndex CompileTimeBindIndex::Invalid =
  194. CompileTimeBindIndex(InvalidIndex);
  195. // The index of a runtime parameter in a function. These are allocated
  196. // sequentially, left-to-right, to the function parameters that will have
  197. // arguments passed to them at runtime. In a `call` instruction, a runtime
  198. // argument will have the position in the argument list corresponding to its
  199. // runtime parameter index.
  200. struct RuntimeParamIndex : public IndexBase,
  201. public Printable<RuntimeParamIndex> {
  202. // An explicitly invalid index.
  203. static const RuntimeParamIndex Invalid;
  204. using IndexBase::IndexBase;
  205. auto Print(llvm::raw_ostream& out) const -> void {
  206. out << "runtime_param";
  207. IndexBase::Print(out);
  208. }
  209. };
  210. constexpr RuntimeParamIndex RuntimeParamIndex::Invalid =
  211. RuntimeParamIndex(InvalidIndex);
  212. // The ID of a function.
  213. struct FunctionId : public IdBase, public Printable<FunctionId> {
  214. using ValueType = Function;
  215. // An explicitly invalid ID.
  216. static const FunctionId Invalid;
  217. using IdBase::IdBase;
  218. auto Print(llvm::raw_ostream& out) const -> void {
  219. out << "function";
  220. IdBase::Print(out);
  221. }
  222. };
  223. constexpr FunctionId FunctionId::Invalid = FunctionId(InvalidIndex);
  224. // The ID of an IR within the set of all IRs being evaluated in the current
  225. // check execution.
  226. struct CheckIRId : public IdBase, public Printable<CheckIRId> {
  227. using IdBase::IdBase;
  228. auto Print(llvm::raw_ostream& out) const -> void {
  229. out << "check_ir";
  230. IdBase::Print(out);
  231. }
  232. };
  233. // The ID of a class.
  234. struct ClassId : public IdBase, public Printable<ClassId> {
  235. using ValueType = Class;
  236. // An explicitly invalid ID.
  237. static const ClassId Invalid;
  238. using IdBase::IdBase;
  239. auto Print(llvm::raw_ostream& out) const -> void {
  240. out << "class";
  241. IdBase::Print(out);
  242. }
  243. };
  244. constexpr ClassId ClassId::Invalid = ClassId(InvalidIndex);
  245. // The ID of an interface.
  246. struct InterfaceId : public IdBase, public Printable<InterfaceId> {
  247. using ValueType = Interface;
  248. // An explicitly invalid ID.
  249. static const InterfaceId Invalid;
  250. using IdBase::IdBase;
  251. auto Print(llvm::raw_ostream& out) const -> void {
  252. out << "interface";
  253. IdBase::Print(out);
  254. }
  255. };
  256. constexpr InterfaceId InterfaceId::Invalid = InterfaceId(InvalidIndex);
  257. // The ID of an impl.
  258. struct ImplId : public IdBase, public Printable<ImplId> {
  259. using ValueType = Impl;
  260. // An explicitly invalid ID.
  261. static const ImplId Invalid;
  262. using IdBase::IdBase;
  263. auto Print(llvm::raw_ostream& out) const -> void {
  264. out << "impl";
  265. IdBase::Print(out);
  266. }
  267. };
  268. constexpr ImplId ImplId::Invalid = ImplId(InvalidIndex);
  269. // The ID of a generic.
  270. struct GenericId : public IdBase, public Printable<GenericId> {
  271. using ValueType = Generic;
  272. // An explicitly invalid ID.
  273. static const GenericId Invalid;
  274. using IdBase::IdBase;
  275. auto Print(llvm::raw_ostream& out) const -> void {
  276. out << "generic";
  277. IdBase::Print(out);
  278. }
  279. };
  280. constexpr GenericId GenericId::Invalid = GenericId(InvalidIndex);
  281. // The ID of a specific, which is the result of specifying the generic arguments
  282. // for a generic.
  283. struct SpecificId : public IdBase, public Printable<SpecificId> {
  284. using ValueType = Specific;
  285. // An explicitly invalid ID. This is typically used to represent a non-generic
  286. // entity.
  287. static const SpecificId Invalid;
  288. using IdBase::IdBase;
  289. auto Print(llvm::raw_ostream& out) const -> void {
  290. out << "specific";
  291. IdBase::Print(out);
  292. }
  293. };
  294. constexpr SpecificId SpecificId::Invalid = SpecificId(InvalidIndex);
  295. // The index of an instruction that depends on generic parameters within a
  296. // region of a generic. A corresponding specific version of the instruction can
  297. // be found in each specific corresponding to that generic. This is a pair of a
  298. // region and an index, stored in 32 bits.
  299. struct GenericInstIndex : public IndexBase, public Printable<GenericInstIndex> {
  300. // Where the value is first used within the generic.
  301. enum Region : uint8_t {
  302. // In the declaration.
  303. Declaration,
  304. // In the definition.
  305. Definition,
  306. };
  307. // An explicitly invalid index.
  308. static const GenericInstIndex Invalid;
  309. explicit constexpr GenericInstIndex(Region region, int32_t index)
  310. : IndexBase(region == Declaration ? index
  311. : FirstDefinitionIndex - index) {
  312. CARBON_CHECK(index >= 0);
  313. }
  314. // Returns the index of the instruction within the region.
  315. auto index() const -> int32_t {
  316. CARBON_CHECK(is_valid());
  317. return IndexBase::index >= 0 ? IndexBase::index
  318. : FirstDefinitionIndex - IndexBase::index;
  319. }
  320. // Returns the region within which this instruction was first used.
  321. auto region() const -> Region {
  322. CARBON_CHECK(is_valid());
  323. return IndexBase::index >= 0 ? Declaration : Definition;
  324. }
  325. auto Print(llvm::raw_ostream& out) const -> void {
  326. out << "genericInst";
  327. if (is_valid()) {
  328. out << (region() == Declaration ? "InDecl" : "InDef") << index();
  329. } else {
  330. out << "<invalid>";
  331. }
  332. }
  333. private:
  334. static constexpr auto MakeInvalid() -> GenericInstIndex {
  335. GenericInstIndex result(Declaration, 0);
  336. result.IndexBase::index = InvalidIndex;
  337. return result;
  338. }
  339. static constexpr int32_t FirstDefinitionIndex = InvalidIndex - 1;
  340. };
  341. constexpr GenericInstIndex GenericInstIndex::Invalid =
  342. GenericInstIndex::MakeInvalid();
  343. // The ID of an IR within the set of imported IRs, both direct and indirect.
  344. struct ImportIRId : public IdBase, public Printable<ImportIRId> {
  345. using ValueType = ImportIR;
  346. // An explicitly invalid ID.
  347. static const ImportIRId Invalid;
  348. // The implicit `api` import, for an `impl` file. A null entry is added if
  349. // there is none, as in an `api`, in which case this ID should not show up in
  350. // instructions.
  351. static const ImportIRId ApiForImpl;
  352. using IdBase::IdBase;
  353. auto Print(llvm::raw_ostream& out) const -> void {
  354. out << "ir";
  355. IdBase::Print(out);
  356. }
  357. };
  358. constexpr ImportIRId ImportIRId::Invalid = ImportIRId(InvalidIndex);
  359. constexpr ImportIRId ImportIRId::ApiForImpl = ImportIRId(0);
  360. // A boolean value.
  361. struct BoolValue : public IdBase, public Printable<BoolValue> {
  362. static const BoolValue False;
  363. static const BoolValue True;
  364. // Returns the `BoolValue` corresponding to `b`.
  365. static constexpr auto From(bool b) -> BoolValue { return b ? True : False; }
  366. // Returns the `bool` corresponding to this `BoolValue`.
  367. constexpr auto ToBool() -> bool {
  368. CARBON_CHECK(*this == False || *this == True, "Invalid bool value {0}",
  369. index);
  370. return *this != False;
  371. }
  372. using IdBase::IdBase;
  373. auto Print(llvm::raw_ostream& out) const -> void {
  374. if (*this == False) {
  375. out << "false";
  376. } else if (*this == True) {
  377. out << "true";
  378. } else {
  379. CARBON_FATAL("Invalid bool value {0}", index);
  380. }
  381. }
  382. };
  383. constexpr BoolValue BoolValue::False = BoolValue(0);
  384. constexpr BoolValue BoolValue::True = BoolValue(1);
  385. // An integer kind value -- either "signed" or "unsigned".
  386. //
  387. // This might eventually capture any other properties of an integer type that
  388. // affect its semantics, such as overflow behavior.
  389. struct IntKind : public IdBase, public Printable<IntKind> {
  390. static const IntKind Unsigned;
  391. static const IntKind Signed;
  392. using IdBase::IdBase;
  393. // Returns whether this type is signed.
  394. constexpr auto is_signed() -> bool { return *this == Signed; }
  395. auto Print(llvm::raw_ostream& out) const -> void {
  396. if (*this == Unsigned) {
  397. out << "unsigned";
  398. } else if (*this == Signed) {
  399. out << "signed";
  400. } else {
  401. CARBON_FATAL("Invalid int kind value {0}", index);
  402. }
  403. }
  404. };
  405. constexpr IntKind IntKind::Unsigned = IntKind(0);
  406. constexpr IntKind IntKind::Signed = IntKind(1);
  407. // A float kind value
  408. struct FloatKind : public IdBase, public Printable<FloatKind> {
  409. using IdBase::IdBase;
  410. auto Print(llvm::raw_ostream& out) const -> void { out << "float"; }
  411. };
  412. // The ID of a name. A name is either a string or a special name such as
  413. // `self`, `Self`, or `base`.
  414. struct NameId : public IdBase, public Printable<NameId> {
  415. // names().GetFormatted() is used for diagnostics.
  416. using DiagnosticType = DiagnosticTypeInfo<std::string>;
  417. // An explicitly invalid ID.
  418. static const NameId Invalid;
  419. // The name of `self`.
  420. static const NameId SelfValue;
  421. // The name of `Self`.
  422. static const NameId SelfType;
  423. // The name of the return slot in a function.
  424. static const NameId ReturnSlot;
  425. // The name of `package`.
  426. static const NameId PackageNamespace;
  427. // The name of `base`.
  428. static const NameId Base;
  429. // The number of non-index (<0) that exist, and will need storage in name
  430. // lookup.
  431. static const int NonIndexValueCount;
  432. // Returns the NameId corresponding to a particular IdentifierId.
  433. static auto ForIdentifier(IdentifierId id) -> NameId {
  434. if (id.index >= 0) {
  435. return NameId(id.index);
  436. } else if (!id.is_valid()) {
  437. return NameId::Invalid;
  438. } else {
  439. CARBON_FATAL("Unexpected identifier ID {0}", id);
  440. }
  441. }
  442. using IdBase::IdBase;
  443. // Returns the IdentifierId corresponding to this NameId, or an invalid
  444. // IdentifierId if this is a special name.
  445. auto AsIdentifierId() const -> IdentifierId {
  446. return index >= 0 ? IdentifierId(index) : IdentifierId::Invalid;
  447. }
  448. auto Print(llvm::raw_ostream& out) const -> void {
  449. out << "name";
  450. if (*this == SelfValue) {
  451. out << "SelfValue";
  452. } else if (*this == SelfType) {
  453. out << "SelfType";
  454. } else if (*this == ReturnSlot) {
  455. out << "ReturnSlot";
  456. } else if (*this == PackageNamespace) {
  457. out << "PackageNamespace";
  458. } else if (*this == Base) {
  459. out << "Base";
  460. } else {
  461. CARBON_CHECK(!is_valid() || index >= 0, "Unknown index {0}", index);
  462. IdBase::Print(out);
  463. }
  464. }
  465. };
  466. constexpr NameId NameId::Invalid = NameId(InvalidIndex);
  467. constexpr NameId NameId::SelfValue = NameId(InvalidIndex - 1);
  468. constexpr NameId NameId::SelfType = NameId(InvalidIndex - 2);
  469. constexpr NameId NameId::ReturnSlot = NameId(InvalidIndex - 3);
  470. constexpr NameId NameId::PackageNamespace = NameId(InvalidIndex - 4);
  471. constexpr NameId NameId::Base = NameId(InvalidIndex - 5);
  472. constexpr int NameId::NonIndexValueCount = 6;
  473. // Enforce the link between SpecialValueCount and the last special value.
  474. static_assert(NameId::NonIndexValueCount == -NameId::Base.index);
  475. // The ID of a name scope.
  476. struct NameScopeId : public IdBase, public Printable<NameScopeId> {
  477. using ValueType = NameScope;
  478. // An explicitly invalid ID.
  479. static const NameScopeId Invalid;
  480. // The package (or file) name scope, guaranteed to be the first added.
  481. static const NameScopeId Package;
  482. using IdBase::IdBase;
  483. auto Print(llvm::raw_ostream& out) const -> void {
  484. out << "name_scope";
  485. IdBase::Print(out);
  486. }
  487. };
  488. constexpr NameScopeId NameScopeId::Invalid = NameScopeId(InvalidIndex);
  489. constexpr NameScopeId NameScopeId::Package = NameScopeId(0);
  490. // The ID of an instruction block.
  491. struct InstBlockId : public IdBase, public Printable<InstBlockId> {
  492. using ElementType = InstId;
  493. using ValueType = llvm::MutableArrayRef<ElementType>;
  494. // The canonical empty block, reused to avoid allocating empty vectors. Always
  495. // the 0-index block.
  496. static const InstBlockId Empty;
  497. // Exported instructions. Empty until the File is fully checked; intermediate
  498. // state is in the Check::Context.
  499. static const InstBlockId Exports;
  500. // ImportRef instructions. Empty until the File is fully checked; intermediate
  501. // state is in the Check::Context.
  502. static const InstBlockId ImportRefs;
  503. // Global declaration initialization instructions. Empty if none are present.
  504. // Otherwise, __global_init function will be generated and this block will
  505. // be inserted into it.
  506. static const InstBlockId GlobalInit;
  507. // An explicitly invalid ID.
  508. static const InstBlockId Invalid;
  509. // An ID for unreachable code.
  510. static const InstBlockId Unreachable;
  511. using IdBase::IdBase;
  512. auto Print(llvm::raw_ostream& out) const -> void {
  513. if (*this == Unreachable) {
  514. out << "unreachable";
  515. } else if (*this == Empty) {
  516. out << "empty";
  517. } else if (*this == Exports) {
  518. out << "exports";
  519. } else if (*this == ImportRefs) {
  520. out << "import_refs";
  521. } else if (*this == GlobalInit) {
  522. out << "global_init";
  523. } else {
  524. out << "block";
  525. IdBase::Print(out);
  526. }
  527. }
  528. };
  529. constexpr InstBlockId InstBlockId::Empty = InstBlockId(0);
  530. constexpr InstBlockId InstBlockId::Exports = InstBlockId(1);
  531. constexpr InstBlockId InstBlockId::ImportRefs = InstBlockId(2);
  532. constexpr InstBlockId InstBlockId::GlobalInit = InstBlockId(3);
  533. constexpr InstBlockId InstBlockId::Invalid = InstBlockId(InvalidIndex);
  534. constexpr InstBlockId InstBlockId::Unreachable = InstBlockId(InvalidIndex - 1);
  535. // The ID of a type.
  536. struct TypeId : public IdBase, public Printable<TypeId> {
  537. // StringifyType() is used for diagnostics.
  538. using DiagnosticType = DiagnosticTypeInfo<std::string>;
  539. // The builtin TypeType.
  540. static const TypeId TypeType;
  541. // The builtin Error.
  542. static const TypeId Error;
  543. // An explicitly invalid ID.
  544. static const TypeId Invalid;
  545. using IdBase::IdBase;
  546. // Returns the ID of the type corresponding to the constant `const_id`, which
  547. // must be of type `type`. As an exception, the type `Error` is of type
  548. // `Error`.
  549. static constexpr auto ForTypeConstant(ConstantId const_id) -> TypeId {
  550. return TypeId(const_id.index);
  551. }
  552. // Returns the constant ID that defines the type.
  553. auto AsConstantId() const -> ConstantId { return ConstantId(index); }
  554. auto Print(llvm::raw_ostream& out) const -> void {
  555. out << "type";
  556. if (*this == TypeType) {
  557. out << "TypeType";
  558. } else if (*this == Error) {
  559. out << "Error";
  560. } else {
  561. out << "(";
  562. AsConstantId().Print(out, /*disambiguate=*/false);
  563. out << ")";
  564. }
  565. }
  566. };
  567. constexpr TypeId TypeId::TypeType = TypeId::ForTypeConstant(
  568. ConstantId::ForTemplateConstant(InstId::BuiltinTypeType));
  569. constexpr TypeId TypeId::Error = TypeId::ForTypeConstant(ConstantId::Error);
  570. constexpr TypeId TypeId::Invalid = TypeId(InvalidIndex);
  571. // The ID of a type block.
  572. struct TypeBlockId : public IdBase, public Printable<TypeBlockId> {
  573. using ElementType = TypeId;
  574. using ValueType = llvm::MutableArrayRef<ElementType>;
  575. // An explicitly invalid ID.
  576. static const TypeBlockId Invalid;
  577. using IdBase::IdBase;
  578. auto Print(llvm::raw_ostream& out) const -> void {
  579. out << "type_block";
  580. IdBase::Print(out);
  581. }
  582. };
  583. constexpr TypeBlockId TypeBlockId::Invalid = TypeBlockId(InvalidIndex);
  584. // An index for element access, for structs, tuples, and classes.
  585. struct ElementIndex : public IndexBase, public Printable<ElementIndex> {
  586. using IndexBase::IndexBase;
  587. auto Print(llvm::raw_ostream& out) const -> void {
  588. out << "element";
  589. IndexBase::Print(out);
  590. }
  591. };
  592. // The ID of a library name. This is either a string literal or `default`.
  593. struct LibraryNameId : public IdBase, public Printable<NameId> {
  594. using DiagnosticType = DiagnosticTypeInfo<std::string>;
  595. // An explicitly invalid ID.
  596. static const LibraryNameId Invalid;
  597. // The name of `default`.
  598. static const LibraryNameId Default;
  599. // Track cases where the library name was set, but has been diagnosed and
  600. // shouldn't be used anymore.
  601. static const LibraryNameId Error;
  602. // Returns the LibraryNameId for a library name as a string literal.
  603. static auto ForStringLiteralValueId(StringLiteralValueId id)
  604. -> LibraryNameId {
  605. CARBON_CHECK(id.index >= InvalidIndex, "Unexpected library name ID {0}",
  606. id);
  607. if (id == StringLiteralValueId::Invalid) {
  608. // Prior to SemIR, we use invalid to indicate `default`.
  609. return LibraryNameId::Default;
  610. } else {
  611. return LibraryNameId(id.index);
  612. }
  613. }
  614. using IdBase::IdBase;
  615. // Converts a LibraryNameId back to a string literal.
  616. auto AsStringLiteralValueId() const -> StringLiteralValueId {
  617. CARBON_CHECK(index >= InvalidIndex, "{0} must be handled directly", *this);
  618. return StringLiteralValueId(index);
  619. }
  620. auto Print(llvm::raw_ostream& out) const -> void {
  621. out << "libraryName";
  622. if (*this == Default) {
  623. out << "Default";
  624. } else if (*this == Error) {
  625. out << "<error>";
  626. } else {
  627. IdBase::Print(out);
  628. }
  629. }
  630. };
  631. constexpr LibraryNameId LibraryNameId::Invalid = LibraryNameId(InvalidIndex);
  632. constexpr LibraryNameId LibraryNameId::Default =
  633. LibraryNameId(InvalidIndex - 1);
  634. constexpr LibraryNameId LibraryNameId::Error = LibraryNameId(InvalidIndex - 2);
  635. // The ID of an ImportIRInst.
  636. struct ImportIRInstId : public IdBase, public Printable<ImportIRInstId> {
  637. using ValueType = ImportIRInst;
  638. // An explicitly invalid ID.
  639. static const ImportIRInstId Invalid;
  640. using IdBase::IdBase;
  641. auto Print(llvm::raw_ostream& out) const -> void {
  642. out << "import_ir_inst";
  643. IdBase::Print(out);
  644. }
  645. };
  646. constexpr ImportIRInstId ImportIRInstId::Invalid = ImportIRInstId(InvalidIndex);
  647. // A SemIR location used exclusively for diagnostic locations.
  648. //
  649. // Contents:
  650. // - index > Invalid: A Parse::NodeId in the current IR.
  651. // - index < Invalid: An ImportIRInstId.
  652. // - index == Invalid: Can be used for either.
  653. struct LocId : public IdBase, public Printable<LocId> {
  654. // An explicitly invalid ID.
  655. static const LocId Invalid;
  656. using IdBase::IdBase;
  657. // NOLINTNEXTLINE(google-explicit-constructor)
  658. constexpr LocId(Parse::InvalidNodeId /*invalid*/) : IdBase(InvalidIndex) {}
  659. // NOLINTNEXTLINE(google-explicit-constructor)
  660. constexpr LocId(Parse::NodeId node_id) : IdBase(node_id.index) {
  661. CARBON_CHECK(node_id.is_valid() == is_valid());
  662. }
  663. // NOLINTNEXTLINE(google-explicit-constructor)
  664. constexpr LocId(ImportIRInstId inst_id)
  665. : IdBase(InvalidIndex + ImportIRInstId::InvalidIndex - inst_id.index) {
  666. CARBON_CHECK(inst_id.is_valid() == is_valid());
  667. }
  668. auto is_node_id() const -> bool { return index > InvalidIndex; }
  669. auto is_import_ir_inst_id() const -> bool { return index < InvalidIndex; }
  670. // This is allowed to return an invalid NodeId, but should never be used for a
  671. // valid InstId.
  672. auto node_id() const -> Parse::NodeId {
  673. CARBON_CHECK(is_node_id() || !is_valid());
  674. return Parse::NodeId(index);
  675. }
  676. // This is allowed to return an invalid InstId, but should never be used for a
  677. // valid NodeId.
  678. auto import_ir_inst_id() const -> ImportIRInstId {
  679. CARBON_CHECK(is_import_ir_inst_id() || !is_valid());
  680. return ImportIRInstId(InvalidIndex + ImportIRInstId::InvalidIndex - index);
  681. }
  682. auto Print(llvm::raw_ostream& out) const -> void {
  683. out << "loc_";
  684. if (is_node_id() || !is_valid()) {
  685. out << node_id();
  686. } else {
  687. out << import_ir_inst_id();
  688. }
  689. }
  690. };
  691. constexpr LocId LocId::Invalid = LocId(Parse::NodeId::Invalid);
  692. } // namespace Carbon::SemIR
  693. #endif // CARBON_TOOLCHAIN_SEM_IR_IDS_H_