ids.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/sem_ir/builtin_kind.h"
  11. namespace Carbon::SemIR {
  12. // Forward declare indexed types, for integration with ValueStore.
  13. class File;
  14. class Inst;
  15. struct Class;
  16. struct Function;
  17. struct Interface;
  18. struct NameScope;
  19. struct TypeInfo;
  20. // The ID of an instruction.
  21. struct InstId : public IdBase, public Printable<InstId> {
  22. using ValueType = Inst;
  23. // An explicitly invalid instruction ID.
  24. static const InstId Invalid;
  25. // Builtin instruction IDs.
  26. #define CARBON_SEM_IR_BUILTIN_KIND_NAME(Name) static const InstId Builtin##Name;
  27. #include "toolchain/sem_ir/builtin_kind.def"
  28. // The namespace for a `package` expression.
  29. static const InstId PackageNamespace;
  30. // Returns the cross-reference instruction ID for a builtin. This relies on
  31. // File guarantees for builtin cross-reference placement.
  32. static constexpr auto ForBuiltin(BuiltinKind kind) -> InstId {
  33. return InstId(kind.AsInt());
  34. }
  35. using IdBase::IdBase;
  36. auto Print(llvm::raw_ostream& out) const -> void {
  37. out << "inst";
  38. if (!is_valid()) {
  39. IdBase::Print(out);
  40. } else if (index < BuiltinKind::ValidCount) {
  41. out << BuiltinKind::FromInt(index);
  42. } else {
  43. // Use the `+` as a small reminder that this is a delta, rather than an
  44. // absolute index.
  45. out << "+" << index - BuiltinKind::ValidCount;
  46. }
  47. }
  48. };
  49. constexpr InstId InstId::Invalid = InstId(InstId::InvalidIndex);
  50. #define CARBON_SEM_IR_BUILTIN_KIND_NAME(Name) \
  51. constexpr InstId InstId::Builtin##Name = \
  52. InstId::ForBuiltin(BuiltinKind::Name);
  53. #include "toolchain/sem_ir/builtin_kind.def"
  54. // The package namespace will be the instruction after builtins.
  55. constexpr InstId InstId::PackageNamespace = InstId(BuiltinKind::ValidCount);
  56. // The ID of a function.
  57. struct FunctionId : public IdBase, public Printable<FunctionId> {
  58. using ValueType = Function;
  59. // An explicitly invalid function ID.
  60. static const FunctionId Invalid;
  61. using IdBase::IdBase;
  62. auto Print(llvm::raw_ostream& out) const -> void {
  63. out << "function";
  64. IdBase::Print(out);
  65. }
  66. };
  67. constexpr FunctionId FunctionId::Invalid = FunctionId(FunctionId::InvalidIndex);
  68. // The ID of a class.
  69. struct ClassId : public IdBase, public Printable<ClassId> {
  70. using ValueType = Class;
  71. // An explicitly invalid class ID.
  72. static const ClassId Invalid;
  73. using IdBase::IdBase;
  74. auto Print(llvm::raw_ostream& out) const -> void {
  75. out << "class";
  76. IdBase::Print(out);
  77. }
  78. };
  79. constexpr ClassId ClassId::Invalid = ClassId(ClassId::InvalidIndex);
  80. // The ID of an interface.
  81. struct InterfaceId : public IdBase, public Printable<InterfaceId> {
  82. using ValueType = Interface;
  83. // An explicitly invalid interface ID.
  84. static const InterfaceId Invalid;
  85. using IdBase::IdBase;
  86. auto Print(llvm::raw_ostream& out) const -> void {
  87. out << "interface";
  88. IdBase::Print(out);
  89. }
  90. };
  91. constexpr InterfaceId InterfaceId::Invalid =
  92. InterfaceId(InterfaceId::InvalidIndex);
  93. // The ID of a cross-referenced IR.
  94. struct CrossRefIRId : public IdBase, public Printable<CrossRefIRId> {
  95. using ValueType = const File*;
  96. static const CrossRefIRId Builtins;
  97. using IdBase::IdBase;
  98. auto Print(llvm::raw_ostream& out) const -> void {
  99. out << "ir";
  100. IdBase::Print(out);
  101. }
  102. };
  103. constexpr CrossRefIRId CrossRefIRId::Builtins = CrossRefIRId(0);
  104. // A boolean value.
  105. struct BoolValue : public IdBase, public Printable<BoolValue> {
  106. static const BoolValue False;
  107. static const BoolValue True;
  108. using IdBase::IdBase;
  109. auto Print(llvm::raw_ostream& out) const -> void {
  110. switch (index) {
  111. case 0:
  112. out << "false";
  113. break;
  114. case 1:
  115. out << "true";
  116. break;
  117. default:
  118. CARBON_FATAL() << "Invalid bool value " << index;
  119. }
  120. }
  121. };
  122. constexpr BoolValue BoolValue::False = BoolValue(0);
  123. constexpr BoolValue BoolValue::True = BoolValue(1);
  124. // The ID of a name. A name is either a string or a special name such as
  125. // `self`, `Self`, or `base`.
  126. struct NameId : public IdBase, public Printable<NameId> {
  127. // An explicitly invalid ID.
  128. static const NameId Invalid;
  129. // The name of `self`.
  130. static const NameId SelfValue;
  131. // The name of `Self`.
  132. static const NameId SelfType;
  133. // The name of the return slot in a function.
  134. static const NameId ReturnSlot;
  135. // The name of `package`.
  136. static const NameId PackageNamespace;
  137. // The name of `base`.
  138. static const NameId Base;
  139. // Returns the NameId corresponding to a particular IdentifierId.
  140. static auto ForIdentifier(IdentifierId id) -> NameId {
  141. // NOLINTNEXTLINE(misc-redundant-expression): Asserting to be sure.
  142. static_assert(NameId::InvalidIndex == IdentifierId::InvalidIndex);
  143. CARBON_CHECK(id.index >= 0 || id.index == InvalidIndex)
  144. << "Unexpected identifier ID";
  145. return NameId(id.index);
  146. }
  147. using IdBase::IdBase;
  148. // Returns the IdentifierId corresponding to this NameId, or an invalid
  149. // IdentifierId if this is a special name.
  150. auto AsIdentifierId() const -> IdentifierId {
  151. return index >= 0 ? IdentifierId(index) : IdentifierId::Invalid;
  152. }
  153. auto Print(llvm::raw_ostream& out) const -> void {
  154. out << "name";
  155. if (*this == SelfValue) {
  156. out << "SelfValue";
  157. } else if (*this == SelfType) {
  158. out << "SelfType";
  159. } else if (*this == ReturnSlot) {
  160. out << "ReturnSlot";
  161. } else if (*this == PackageNamespace) {
  162. out << "PackageNamespace";
  163. } else if (*this == Base) {
  164. out << "Base";
  165. } else {
  166. CARBON_CHECK(index >= 0) << "Unknown index";
  167. IdBase::Print(out);
  168. }
  169. }
  170. };
  171. constexpr NameId NameId::Invalid = NameId(NameId::InvalidIndex);
  172. constexpr NameId NameId::SelfValue = NameId(NameId::InvalidIndex - 1);
  173. constexpr NameId NameId::SelfType = NameId(NameId::InvalidIndex - 2);
  174. constexpr NameId NameId::ReturnSlot = NameId(NameId::InvalidIndex - 3);
  175. constexpr NameId NameId::PackageNamespace = NameId(NameId::InvalidIndex - 4);
  176. constexpr NameId NameId::Base = NameId(NameId::InvalidIndex - 5);
  177. // The ID of a name scope.
  178. struct NameScopeId : public IdBase, public Printable<NameScopeId> {
  179. using ValueType = NameScope;
  180. // An explicitly invalid ID.
  181. static const NameScopeId Invalid;
  182. // The package (or file) name scope, guaranteed to be the first added.
  183. static const NameScopeId Package;
  184. using IdBase::IdBase;
  185. auto Print(llvm::raw_ostream& out) const -> void {
  186. out << "name_scope";
  187. IdBase::Print(out);
  188. }
  189. };
  190. constexpr NameScopeId NameScopeId::Invalid =
  191. NameScopeId(NameScopeId::InvalidIndex);
  192. constexpr NameScopeId NameScopeId::Package = NameScopeId(0);
  193. // The ID of an instruction block.
  194. struct InstBlockId : public IdBase, public Printable<InstBlockId> {
  195. using ElementType = InstId;
  196. using ValueType = llvm::MutableArrayRef<ElementType>;
  197. // All File instances must provide the 0th instruction block as empty.
  198. static const InstBlockId Empty;
  199. // An explicitly invalid ID.
  200. static const InstBlockId Invalid;
  201. // An ID for unreachable code.
  202. static const InstBlockId Unreachable;
  203. using IdBase::IdBase;
  204. auto Print(llvm::raw_ostream& out) const -> void {
  205. if (index == Unreachable.index) {
  206. out << "unreachable";
  207. } else {
  208. out << "block";
  209. IdBase::Print(out);
  210. }
  211. }
  212. };
  213. constexpr InstBlockId InstBlockId::Empty = InstBlockId(0);
  214. constexpr InstBlockId InstBlockId::Invalid =
  215. InstBlockId(InstBlockId::InvalidIndex);
  216. constexpr InstBlockId InstBlockId::Unreachable =
  217. InstBlockId(InstBlockId::InvalidIndex - 1);
  218. // The ID of a type.
  219. struct TypeId : public IdBase, public Printable<TypeId> {
  220. using ValueType = TypeInfo;
  221. // The builtin TypeType.
  222. static const TypeId TypeType;
  223. // The builtin Error.
  224. static const TypeId Error;
  225. // An explicitly invalid ID.
  226. static const TypeId Invalid;
  227. using IdBase::IdBase;
  228. auto Print(llvm::raw_ostream& out) const -> void {
  229. out << "type";
  230. if (index == TypeType.index) {
  231. out << "TypeType";
  232. } else if (index == Error.index) {
  233. out << "Error";
  234. } else {
  235. IdBase::Print(out);
  236. }
  237. }
  238. };
  239. constexpr TypeId TypeId::TypeType = TypeId(TypeId::InvalidIndex - 2);
  240. constexpr TypeId TypeId::Error = TypeId(TypeId::InvalidIndex - 1);
  241. constexpr TypeId TypeId::Invalid = TypeId(TypeId::InvalidIndex);
  242. // The ID of a type block.
  243. struct TypeBlockId : public IdBase, public Printable<TypeBlockId> {
  244. using ElementType = TypeId;
  245. using ValueType = llvm::MutableArrayRef<ElementType>;
  246. using IdBase::IdBase;
  247. auto Print(llvm::raw_ostream& out) const -> void {
  248. out << "typeBlock";
  249. IdBase::Print(out);
  250. }
  251. };
  252. // An index for element access, for structs, tuples, and classes.
  253. struct ElementIndex : public IndexBase, public Printable<ElementIndex> {
  254. using IndexBase::IndexBase;
  255. auto Print(llvm::raw_ostream& out) const -> void {
  256. out << "element";
  257. IndexBase::Print(out);
  258. }
  259. };
  260. } // namespace Carbon::SemIR
  261. // Support use of Id types as DenseMap/DenseSet keys.
  262. template <>
  263. struct llvm::DenseMapInfo<Carbon::SemIR::InstBlockId>
  264. : public Carbon::IndexMapInfo<Carbon::SemIR::InstBlockId> {};
  265. template <>
  266. struct llvm::DenseMapInfo<Carbon::SemIR::InstId>
  267. : public Carbon::IndexMapInfo<Carbon::SemIR::InstId> {};
  268. #endif // CARBON_TOOLCHAIN_SEM_IR_IDS_H_