ids.h 15 KB

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