ids.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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/sem_ir/builtin_kind.h"
  12. namespace Carbon::SemIR {
  13. // Forward declare indexed types, for integration with ValueStore.
  14. class File;
  15. class Inst;
  16. struct BindNameInfo;
  17. struct Class;
  18. struct Function;
  19. struct ImportIR;
  20. struct Interface;
  21. struct Impl;
  22. struct NameScope;
  23. struct TypeInfo;
  24. // The ID of an instruction.
  25. struct InstId : public IdBase, public Printable<InstId> {
  26. using ValueType = Inst;
  27. // An explicitly invalid instruction ID.
  28. static const InstId Invalid;
  29. // Builtin instruction IDs.
  30. #define CARBON_SEM_IR_BUILTIN_KIND_NAME(Name) static const InstId Builtin##Name;
  31. #include "toolchain/sem_ir/builtin_kind.def"
  32. // The namespace for a `package` expression.
  33. static const InstId PackageNamespace;
  34. // Returns the instruction ID for a builtin. This relies on File guarantees
  35. // for builtin ImportRefUsed placement.
  36. static constexpr auto ForBuiltin(BuiltinKind kind) -> InstId {
  37. return InstId(kind.AsInt());
  38. }
  39. using IdBase::IdBase;
  40. // Returns true if the instruction is a builtin. Requires is_valid.
  41. auto is_builtin() const -> bool {
  42. CARBON_CHECK(is_valid());
  43. return index < BuiltinKind::ValidCount;
  44. }
  45. // Returns the BuiltinKind. Requires is_builtin.
  46. auto builtin_kind() const -> BuiltinKind {
  47. CARBON_CHECK(is_builtin());
  48. return BuiltinKind::FromInt(index);
  49. }
  50. auto Print(llvm::raw_ostream& out) const -> void {
  51. out << "inst";
  52. if (!is_valid()) {
  53. IdBase::Print(out);
  54. } else if (is_builtin()) {
  55. out << builtin_kind();
  56. } else {
  57. // Use the `+` as a small reminder that this is a delta, rather than an
  58. // absolute index.
  59. out << "+" << index - BuiltinKind::ValidCount;
  60. }
  61. }
  62. };
  63. constexpr InstId InstId::Invalid = InstId(InvalidIndex);
  64. #define CARBON_SEM_IR_BUILTIN_KIND_NAME(Name) \
  65. constexpr InstId InstId::Builtin##Name = \
  66. InstId::ForBuiltin(BuiltinKind::Name);
  67. #include "toolchain/sem_ir/builtin_kind.def"
  68. // The package namespace will be the instruction after builtins.
  69. constexpr InstId InstId::PackageNamespace = InstId(BuiltinKind::ValidCount);
  70. // The ID of a constant value of an expression. An expression is either:
  71. //
  72. // - a template constant, with an immediate value, such as `42` or `i32*` or
  73. // `("hello", "world")`, or
  74. // - a symbolic constant, whose value includes a symbolic parameter, such as
  75. // `Vector(T*)`, or
  76. // - a runtime expression, such as `Print("hello")`.
  77. struct ConstantId : public IdBase, public Printable<ConstantId> {
  78. // An ID for an expression that is not constant.
  79. static const ConstantId NotConstant;
  80. // An ID for an expression whose phase cannot be determined because it
  81. // contains an error. This is always modeled as a template constant.
  82. static const ConstantId Error;
  83. // An explicitly invalid ID.
  84. static const ConstantId Invalid;
  85. // Returns the constant ID corresponding to a template constant, which should
  86. // either be in the `constants` block in the file or should be known to be
  87. // unique.
  88. static constexpr auto ForTemplateConstant(InstId const_id) -> ConstantId {
  89. return ConstantId(const_id.index + IndexOffset);
  90. }
  91. // Returns the constant ID corresponding to a symbolic constant, which should
  92. // either be in the `constants` block in the file or should be known to be
  93. // unique.
  94. static constexpr auto ForSymbolicConstant(InstId const_id) -> ConstantId {
  95. return ConstantId(-const_id.index - IndexOffset);
  96. }
  97. using IdBase::IdBase;
  98. // Returns whether this represents a constant. Requires is_valid.
  99. auto is_constant() const -> bool {
  100. CARBON_CHECK(is_valid());
  101. return *this != ConstantId::NotConstant;
  102. }
  103. // Returns whether this represents a symbolic constant. Requires is_valid.
  104. auto is_symbolic() const -> bool {
  105. CARBON_CHECK(is_valid());
  106. return index <= -IndexOffset;
  107. }
  108. // Returns whether this represents a template constant. Requires is_valid.
  109. auto is_template() const -> bool {
  110. CARBON_CHECK(is_valid());
  111. return index >= IndexOffset;
  112. }
  113. // Returns the instruction that describes this constant value, or
  114. // InstId::Invalid for a runtime value. Requires is_valid.
  115. constexpr auto inst_id() const -> InstId {
  116. CARBON_CHECK(is_valid());
  117. return InstId(Abs(index) - IndexOffset);
  118. }
  119. auto Print(llvm::raw_ostream& out) const -> void {
  120. if (!is_valid()) {
  121. IdBase::Print(out);
  122. } else if (is_template()) {
  123. out << "template " << inst_id();
  124. } else if (is_symbolic()) {
  125. out << "symbolic " << inst_id();
  126. } else {
  127. out << "runtime";
  128. }
  129. }
  130. private:
  131. // TODO: C++23 makes std::abs constexpr, but until then we mirror std::abs
  132. // logic here. LLVM should still optimize this.
  133. static constexpr auto Abs(int32_t i) -> int32_t { return i > 0 ? i : -i; }
  134. static constexpr int32_t NotConstantIndex = InvalidIndex - 1;
  135. // The offset of InstId indices to ConstantId indices.
  136. static constexpr int32_t IndexOffset = -NotConstantIndex + 1;
  137. };
  138. constexpr ConstantId ConstantId::NotConstant = ConstantId(NotConstantIndex);
  139. static_assert(ConstantId::NotConstant.inst_id() == InstId::Invalid);
  140. constexpr ConstantId ConstantId::Error =
  141. ConstantId::ForTemplateConstant(InstId::BuiltinError);
  142. constexpr ConstantId ConstantId::Invalid = ConstantId(InvalidIndex);
  143. // The ID of a bind name.
  144. struct BindNameId : public IdBase, public Printable<BindNameId> {
  145. using ValueType = BindNameInfo;
  146. // An explicitly invalid function ID.
  147. static const BindNameId Invalid;
  148. using IdBase::IdBase;
  149. auto Print(llvm::raw_ostream& out) const -> void {
  150. out << "bindName";
  151. IdBase::Print(out);
  152. }
  153. };
  154. constexpr BindNameId BindNameId::Invalid = BindNameId(InvalidIndex);
  155. // The ID of a function.
  156. struct FunctionId : public IdBase, public Printable<FunctionId> {
  157. using ValueType = Function;
  158. // An explicitly invalid function ID.
  159. static const FunctionId Invalid;
  160. using IdBase::IdBase;
  161. auto Print(llvm::raw_ostream& out) const -> void {
  162. out << "function";
  163. IdBase::Print(out);
  164. }
  165. };
  166. constexpr FunctionId FunctionId::Invalid = FunctionId(InvalidIndex);
  167. // The ID of a class.
  168. struct ClassId : public IdBase, public Printable<ClassId> {
  169. using ValueType = Class;
  170. // An explicitly invalid class ID.
  171. static const ClassId Invalid;
  172. using IdBase::IdBase;
  173. auto Print(llvm::raw_ostream& out) const -> void {
  174. out << "class";
  175. IdBase::Print(out);
  176. }
  177. };
  178. constexpr ClassId ClassId::Invalid = ClassId(InvalidIndex);
  179. // The ID of an interface.
  180. struct InterfaceId : public IdBase, public Printable<InterfaceId> {
  181. using ValueType = Interface;
  182. // An explicitly invalid interface ID.
  183. static const InterfaceId Invalid;
  184. using IdBase::IdBase;
  185. auto Print(llvm::raw_ostream& out) const -> void {
  186. out << "interface";
  187. IdBase::Print(out);
  188. }
  189. };
  190. constexpr InterfaceId InterfaceId::Invalid = InterfaceId(InvalidIndex);
  191. // The ID of an impl.
  192. struct ImplId : public IdBase, public Printable<ImplId> {
  193. using ValueType = Impl;
  194. // An explicitly invalid interface ID.
  195. static const ImplId Invalid;
  196. using IdBase::IdBase;
  197. auto Print(llvm::raw_ostream& out) const -> void {
  198. out << "impl";
  199. IdBase::Print(out);
  200. }
  201. };
  202. constexpr ImplId ImplId::Invalid = ImplId(InvalidIndex);
  203. // The ID of an imported IR.
  204. struct ImportIRId : public IdBase, public Printable<ImportIRId> {
  205. using ValueType = ImportIR;
  206. static const ImportIRId Builtins;
  207. using IdBase::IdBase;
  208. auto Print(llvm::raw_ostream& out) const -> void {
  209. out << "ir";
  210. IdBase::Print(out);
  211. }
  212. };
  213. constexpr ImportIRId ImportIRId::Builtins = ImportIRId(0);
  214. // A boolean value.
  215. struct BoolValue : public IdBase, public Printable<BoolValue> {
  216. static const BoolValue False;
  217. static const BoolValue True;
  218. // Returns the `BoolValue` corresponding to `b`.
  219. static constexpr auto From(bool b) -> BoolValue { return b ? True : False; }
  220. // Returns the `bool` corresponding to this `BoolValue`.
  221. constexpr auto ToBool() -> bool {
  222. CARBON_CHECK(*this == False || *this == True)
  223. << "Invalid bool value " << index;
  224. return *this != False;
  225. }
  226. using IdBase::IdBase;
  227. auto Print(llvm::raw_ostream& out) const -> void {
  228. if (*this == False) {
  229. out << "false";
  230. } else if (*this == True) {
  231. out << "true";
  232. } else {
  233. CARBON_FATAL() << "Invalid bool value " << index;
  234. }
  235. }
  236. };
  237. constexpr BoolValue BoolValue::False = BoolValue(0);
  238. constexpr BoolValue BoolValue::True = BoolValue(1);
  239. // The ID of a name. A name is either a string or a special name such as
  240. // `self`, `Self`, or `base`.
  241. struct NameId : public IdBase, public Printable<NameId> {
  242. // names().GetFormatted() is used for diagnostics.
  243. using DiagnosticType = DiagnosticTypeInfo<std::string>;
  244. // An explicitly invalid ID.
  245. static const NameId Invalid;
  246. // The name of `self`.
  247. static const NameId SelfValue;
  248. // The name of `Self`.
  249. static const NameId SelfType;
  250. // The name of the return slot in a function.
  251. static const NameId ReturnSlot;
  252. // The name of `package`.
  253. static const NameId PackageNamespace;
  254. // The name of `base`.
  255. static const NameId Base;
  256. // The number of non-index (<0) that exist, and will need storage in name
  257. // lookup.
  258. static const int NonIndexValueCount;
  259. // Returns the NameId corresponding to a particular IdentifierId.
  260. static auto ForIdentifier(IdentifierId id) -> NameId {
  261. if (id.index >= 0) {
  262. return NameId(id.index);
  263. } else if (!id.is_valid()) {
  264. return NameId::Invalid;
  265. } else {
  266. CARBON_FATAL() << "Unexpected identifier ID " << id;
  267. }
  268. }
  269. using IdBase::IdBase;
  270. // Returns the IdentifierId corresponding to this NameId, or an invalid
  271. // IdentifierId if this is a special name.
  272. auto AsIdentifierId() const -> IdentifierId {
  273. return index >= 0 ? IdentifierId(index) : IdentifierId::Invalid;
  274. }
  275. auto Print(llvm::raw_ostream& out) const -> void {
  276. out << "name";
  277. if (*this == SelfValue) {
  278. out << "SelfValue";
  279. } else if (*this == SelfType) {
  280. out << "SelfType";
  281. } else if (*this == ReturnSlot) {
  282. out << "ReturnSlot";
  283. } else if (*this == PackageNamespace) {
  284. out << "PackageNamespace";
  285. } else if (*this == Base) {
  286. out << "Base";
  287. } else {
  288. CARBON_CHECK(!is_valid() || index >= 0) << "Unknown index " << index;
  289. IdBase::Print(out);
  290. }
  291. }
  292. };
  293. constexpr NameId NameId::Invalid = NameId(InvalidIndex);
  294. constexpr NameId NameId::SelfValue = NameId(InvalidIndex - 1);
  295. constexpr NameId NameId::SelfType = NameId(InvalidIndex - 2);
  296. constexpr NameId NameId::ReturnSlot = NameId(InvalidIndex - 3);
  297. constexpr NameId NameId::PackageNamespace = NameId(InvalidIndex - 4);
  298. constexpr NameId NameId::Base = NameId(InvalidIndex - 5);
  299. constexpr int NameId::NonIndexValueCount = 6;
  300. // Enforce the link between SpecialValueCount and the last special value.
  301. static_assert(NameId::NonIndexValueCount == -NameId::Base.index);
  302. // The ID of a name scope.
  303. struct NameScopeId : public IdBase, public Printable<NameScopeId> {
  304. using ValueType = NameScope;
  305. // An explicitly invalid ID.
  306. static const NameScopeId Invalid;
  307. // The package (or file) name scope, guaranteed to be the first added.
  308. static const NameScopeId Package;
  309. using IdBase::IdBase;
  310. auto Print(llvm::raw_ostream& out) const -> void {
  311. out << "name_scope";
  312. IdBase::Print(out);
  313. }
  314. };
  315. constexpr NameScopeId NameScopeId::Invalid = NameScopeId(InvalidIndex);
  316. constexpr NameScopeId NameScopeId::Package = NameScopeId(0);
  317. // The ID of an instruction block.
  318. struct InstBlockId : public IdBase, public Printable<InstBlockId> {
  319. using ElementType = InstId;
  320. using ValueType = llvm::MutableArrayRef<ElementType>;
  321. // An empty block, reused to avoid allocating empty vectors. Always the
  322. // 0-index block.
  323. static const InstBlockId Empty;
  324. // Exported instructions. Always the 1-index block. Empty until the File is
  325. // fully checked; intermediate state is in the Check::Context.
  326. static const InstBlockId Exports;
  327. // Global declaration initialization instructions. Empty if none are present.
  328. // Otherwise, __global_init function will be generated and this block will
  329. // be inserted into it.
  330. static const InstBlockId GlobalInit;
  331. // An explicitly invalid ID.
  332. static const InstBlockId Invalid;
  333. // An ID for unreachable code.
  334. static const InstBlockId Unreachable;
  335. using IdBase::IdBase;
  336. auto Print(llvm::raw_ostream& out) const -> void {
  337. if (*this == Unreachable) {
  338. out << "unreachable";
  339. } else if (*this == Empty) {
  340. out << "empty";
  341. } else if (*this == Exports) {
  342. out << "exports";
  343. } else if (*this == GlobalInit) {
  344. out << "global_init";
  345. } else {
  346. out << "block";
  347. IdBase::Print(out);
  348. }
  349. }
  350. };
  351. constexpr InstBlockId InstBlockId::Empty = InstBlockId(0);
  352. constexpr InstBlockId InstBlockId::Exports = InstBlockId(1);
  353. constexpr InstBlockId InstBlockId::Invalid = InstBlockId(InvalidIndex);
  354. constexpr InstBlockId InstBlockId::Unreachable = InstBlockId(InvalidIndex - 1);
  355. constexpr InstBlockId InstBlockId::GlobalInit = InstBlockId(2);
  356. // The ID of a type.
  357. struct TypeId : public IdBase, public Printable<TypeId> {
  358. using ValueType = TypeInfo;
  359. // StringifyType() is used for diagnostics.
  360. using DiagnosticType = DiagnosticTypeInfo<std::string>;
  361. // The builtin TypeType.
  362. static const TypeId TypeType;
  363. // The builtin Error.
  364. static const TypeId Error;
  365. // An explicitly invalid ID.
  366. static const TypeId Invalid;
  367. using IdBase::IdBase;
  368. auto Print(llvm::raw_ostream& out) const -> void {
  369. out << "type";
  370. if (*this == TypeType) {
  371. out << "TypeType";
  372. } else if (*this == Error) {
  373. out << "Error";
  374. } else {
  375. IdBase::Print(out);
  376. }
  377. }
  378. };
  379. constexpr TypeId TypeId::TypeType = TypeId(InvalidIndex - 2);
  380. constexpr TypeId TypeId::Error = TypeId(InvalidIndex - 1);
  381. constexpr TypeId TypeId::Invalid = TypeId(InvalidIndex);
  382. // The ID of a type block.
  383. struct TypeBlockId : public IdBase, public Printable<TypeBlockId> {
  384. using ElementType = TypeId;
  385. using ValueType = llvm::MutableArrayRef<ElementType>;
  386. using IdBase::IdBase;
  387. auto Print(llvm::raw_ostream& out) const -> void {
  388. out << "typeBlock";
  389. IdBase::Print(out);
  390. }
  391. };
  392. // An index for element access, for structs, tuples, and classes.
  393. struct ElementIndex : public IndexBase, public Printable<ElementIndex> {
  394. using IndexBase::IndexBase;
  395. auto Print(llvm::raw_ostream& out) const -> void {
  396. out << "element";
  397. IndexBase::Print(out);
  398. }
  399. };
  400. } // namespace Carbon::SemIR
  401. // Support use of Id types as DenseMap/DenseSet keys.
  402. template <>
  403. struct llvm::DenseMapInfo<Carbon::SemIR::ConstantId>
  404. : public Carbon::IndexMapInfo<Carbon::SemIR::ConstantId> {};
  405. template <>
  406. struct llvm::DenseMapInfo<Carbon::SemIR::InstBlockId>
  407. : public Carbon::IndexMapInfo<Carbon::SemIR::InstBlockId> {};
  408. template <>
  409. struct llvm::DenseMapInfo<Carbon::SemIR::InstId>
  410. : public Carbon::IndexMapInfo<Carbon::SemIR::InstId> {};
  411. template <>
  412. struct llvm::DenseMapInfo<Carbon::SemIR::NameId>
  413. : public Carbon::IndexMapInfo<Carbon::SemIR::NameId> {};
  414. template <>
  415. struct llvm::DenseMapInfo<Carbon::SemIR::NameScopeId>
  416. : public Carbon::IndexMapInfo<Carbon::SemIR::NameScopeId> {};
  417. template <>
  418. struct llvm::DenseMapInfo<Carbon::SemIR::TypeId>
  419. : public Carbon::IndexMapInfo<Carbon::SemIR::TypeId> {};
  420. #endif // CARBON_TOOLCHAIN_SEM_IR_IDS_H_