inst_categories.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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_INST_CATEGORIES_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_INST_CATEGORIES_H_
  6. #include "toolchain/sem_ir/ids.h"
  7. #include "toolchain/sem_ir/inst_kind.h"
  8. #include "toolchain/sem_ir/typed_insts.h"
  9. // An inst category is a set of inst kinds that can be treated polymorphically.
  10. // Each inst category is represented by a C++ type, just like an inst kind,
  11. // which can losslessly represent any inst in the category. `CategoryOf`
  12. // is used to declare the typed insts that belong to the category.
  13. namespace Carbon::SemIR {
  14. // Declares a category consisting of `TypedInsts...`, which is a list of typed
  15. // insts (not kinds). Should only be used to define a public type alias member
  16. // of a category inst type:
  17. //
  18. // struct MyCategory {
  19. // using CategoryInfo = CARBON_INST_CATEGORY_INFO(MyCategory);
  20. // InstKind kind;
  21. // ...
  22. // }
  23. template <typename... TypedInsts>
  24. struct CategoryOf {
  25. // The InstKinds that belong to the category.
  26. static constexpr InstKind Kinds[] = {TypedInsts::Kind...};
  27. };
  28. // For each category, we provide `CategoryName_CARBON_INST_CATEGORY` for the
  29. // `CARBON_KIND_ANY` macro. This macro uses the same expansion to provide a
  30. // `CategoryOf` for the category.
  31. #define CARBON_INST_CATEGORY_INFO(Name) \
  32. CategoryOf<Name##_CARBON_INST_CATEGORY( \
  33. CARBON_INST_CATEGORY_INFO_INTERNAL_NAME, \
  34. CARBON_INST_CATEGORY_INFO_INTERNAL_COMMA)>
  35. #define CARBON_INST_CATEGORY_INFO_INTERNAL_NAME(Name) Name
  36. #define CARBON_INST_CATEGORY_INFO_INTERNAL_COMMA() ,
  37. // Helper for defining `AnyKind_CARBON_KIND_ANY_EXPAND`.
  38. #define CARBON_INST_CATEGORY_ANY_EXPAND(AnyKind) \
  39. CARBON_KIND_ANY_EXPAND_BEGIN() \
  40. AnyKind##_CARBON_INST_CATEGORY(CARBON_KIND_ANY_EXPAND_CASE, \
  41. CARBON_KIND_ANY_EXPAND_SEP)
  42. // clang-format off
  43. #define AnyAggregateAccess_CARBON_INST_CATEGORY(X, Sep) \
  44. X(::Carbon::SemIR::ClassElementAccess) Sep() \
  45. X(::Carbon::SemIR::StructAccess) Sep() \
  46. X(::Carbon::SemIR::TupleAccess)
  47. // clang-format on
  48. #define AnyAggregateAccess_CARBON_KIND_ANY_EXPAND \
  49. CARBON_INST_CATEGORY_ANY_EXPAND(AnyAggregateAccess)
  50. // Common representation for aggregate access nodes, which access a fixed
  51. // element of an aggregate.
  52. struct AnyAggregateAccess {
  53. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyAggregateAccess);
  54. InstKind kind;
  55. TypeId type_id;
  56. InstId aggregate_id;
  57. ElementIndex index;
  58. };
  59. // clang-format off
  60. #define AnyAggregateInit_CARBON_INST_CATEGORY(X, Sep) \
  61. X(::Carbon::SemIR::ArrayInit) Sep() \
  62. X(::Carbon::SemIR::ClassInit) Sep() \
  63. X(::Carbon::SemIR::StructInit) Sep() \
  64. X(::Carbon::SemIR::TupleInit)
  65. // clang-format on
  66. #define AnyAggregateInit_CARBON_KIND_ANY_EXPAND \
  67. CARBON_INST_CATEGORY_ANY_EXPAND(AnyAggregateInit)
  68. // Common representation for all kinds of aggregate initialization.
  69. struct AnyAggregateInit {
  70. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyAggregateInit);
  71. InstKind kind;
  72. TypeId type_id;
  73. InstBlockId elements_id;
  74. DestInstId dest_id;
  75. };
  76. // clang-format off
  77. #define AnyAggregateValue_CARBON_INST_CATEGORY(X, Sep) \
  78. X(::Carbon::SemIR::StructValue) Sep() \
  79. X(::Carbon::SemIR::TupleValue)
  80. // clang-format on
  81. #define AnyAggregateValue_CARBON_KIND_ANY_EXPAND \
  82. CARBON_INST_CATEGORY_ANY_EXPAND(AnyAggregateValue)
  83. // Common representation for all kinds of aggregate value.
  84. struct AnyAggregateValue {
  85. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyAggregateValue);
  86. InstKind kind;
  87. TypeId type_id;
  88. InstBlockId elements_id;
  89. };
  90. // clang-format off
  91. #define AnyBindingPattern_CARBON_INST_CATEGORY(X, Sep) \
  92. X(::Carbon::SemIR::FormBindingPattern) Sep() \
  93. X(::Carbon::SemIR::RefBindingPattern) Sep() \
  94. X(::Carbon::SemIR::SymbolicBindingPattern) Sep() \
  95. X(::Carbon::SemIR::ValueBindingPattern) Sep() \
  96. X(::Carbon::SemIR::WrapperBindingPattern)
  97. // clang-format on
  98. #define AnyBindingPattern_CARBON_KIND_ANY_EXPAND \
  99. CARBON_INST_CATEGORY_ANY_EXPAND(AnyBindingPattern)
  100. // Common representation for various `*binding_pattern` nodes.
  101. struct AnyBindingPattern {
  102. // TODO: Also handle TemplateBindingPattern once it exists.
  103. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyBindingPattern);
  104. InstKind kind;
  105. // Always a PatternType whose scrutinee type is the declared type of the
  106. // binding.
  107. TypeId type_id;
  108. // The name declared by the binding pattern. `None` indicates that the
  109. // pattern has `_` in the name position, and so does not truly declare
  110. // a name.
  111. EntityNameId entity_name_id;
  112. // None unless this is an WrapperBindingPattern.
  113. InstId subpattern_id;
  114. };
  115. // clang-format off
  116. #define AnyBinding_CARBON_INST_CATEGORY(X, Sep) \
  117. X(::Carbon::SemIR::AliasBinding) Sep() \
  118. X(::Carbon::SemIR::FormBinding) Sep() \
  119. X(::Carbon::SemIR::RefBinding) Sep() \
  120. X(::Carbon::SemIR::SymbolicBinding) Sep() \
  121. X(::Carbon::SemIR::ValueBinding)
  122. // clang-format on
  123. #define AnyBinding_CARBON_KIND_ANY_EXPAND \
  124. CARBON_INST_CATEGORY_ANY_EXPAND(AnyBinding)
  125. // Common representation for various `bind*` nodes.
  126. struct AnyBinding {
  127. // TODO: Also handle BindTemplateName once it exists.
  128. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyBinding);
  129. InstKind kind;
  130. TypeId type_id;
  131. EntityNameId entity_name_id;
  132. // The value is inline in the inst so that value access doesn't require an
  133. // indirection.
  134. InstId value_id;
  135. };
  136. // clang-format off
  137. #define AnyBindingOrExportDecl_CARBON_INST_CATEGORY(X, Sep) \
  138. AnyBinding_CARBON_INST_CATEGORY(X, Sep) Sep() \
  139. X(::Carbon::SemIR::ExportDecl)
  140. // clang-format on
  141. #define AnyBindingOrExportDecl_CARBON_KIND_ANY_EXPAND \
  142. CARBON_INST_CATEGORY_ANY_EXPAND(AnyBindingOrExportDecl)
  143. // Common representation for various `bind*` nodes, and `export name`.
  144. struct AnyBindingOrExportDecl {
  145. // TODO: Also handle BindTemplateName once it exists.
  146. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyBindingOrExportDecl);
  147. InstKind kind;
  148. TypeId type_id;
  149. EntityNameId entity_name_id;
  150. InstId value_id;
  151. };
  152. // clang-format off
  153. #define AnyBranch_CARBON_INST_CATEGORY(X, Sep) \
  154. X(::Carbon::SemIR::Branch) Sep() \
  155. X(::Carbon::SemIR::BranchIf) Sep() \
  156. X(::Carbon::SemIR::BranchWithArg)
  157. // clang-format on
  158. #define AnyBranch_CARBON_KIND_ANY_EXPAND \
  159. CARBON_INST_CATEGORY_ANY_EXPAND(AnyBranch)
  160. // Common representation for all kinds of `Branch*` node.
  161. struct AnyBranch {
  162. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyBranch);
  163. InstKind kind;
  164. // Branches don't produce a value, so have no type.
  165. LabelId target_id;
  166. // Kind-specific data.
  167. AnyRawId arg1;
  168. };
  169. // clang-format off
  170. #define AnyFoundationDecl_CARBON_INST_CATEGORY(X, Sep) \
  171. X(::Carbon::SemIR::AdaptDecl) Sep() \
  172. X(::Carbon::SemIR::BaseDecl)
  173. // clang-format on
  174. #define AnyFoundationDecl_CARBON_KIND_ANY_EXPAND \
  175. CARBON_INST_CATEGORY_ANY_EXPAND(AnyFoundationDecl)
  176. // Common representation for declarations describing the foundation type of a
  177. // class -- either its adapted type or its base class.
  178. struct AnyFoundationDecl {
  179. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyFoundationDecl);
  180. InstKind kind;
  181. TypeId type_id;
  182. TypeInstId foundation_type_inst_id;
  183. // Kind-specific data.
  184. AnyRawId arg1;
  185. };
  186. // clang-format off
  187. #define AnyImportRef_CARBON_INST_CATEGORY(X, Sep) \
  188. X(::Carbon::SemIR::ImportRefLoaded) Sep() \
  189. X(::Carbon::SemIR::ImportRefUnloaded)
  190. // clang-format on
  191. #define AnyImportRef_CARBON_KIND_ANY_EXPAND \
  192. CARBON_INST_CATEGORY_ANY_EXPAND(AnyImportRef)
  193. // Common representation for all kinds of `ImportRef*` node.
  194. struct AnyImportRef {
  195. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyImportRef);
  196. InstKind kind;
  197. TypeId type_id;
  198. ImportIRInstId import_ir_inst_id;
  199. // A BindName is currently only set on directly imported names. It is not
  200. // generically available.
  201. EntityNameId entity_name_id;
  202. };
  203. // clang-format off
  204. #define AnyParam_CARBON_INST_CATEGORY(X, Sep) \
  205. X(::Carbon::SemIR::OutParam) Sep() \
  206. X(::Carbon::SemIR::RefParam) Sep() \
  207. X(::Carbon::SemIR::ValueParam)
  208. // clang-format on
  209. #define AnyParam_CARBON_KIND_ANY_EXPAND \
  210. CARBON_INST_CATEGORY_ANY_EXPAND(AnyParam)
  211. // A `Call` parameter for a function or other parameterized block.
  212. struct AnyParam {
  213. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyParam);
  214. InstKind kind;
  215. TypeId type_id;
  216. CallParamIndex index;
  217. // A name to associate with this Param in pretty-printed IR. This is not
  218. // necessarily unique, and can even be `None`; it has no semantic
  219. // significance.
  220. NameId pretty_name_id;
  221. };
  222. // clang-format off
  223. #define AnyLeafParamPattern_CARBON_INST_CATEGORY(X, Sep) \
  224. X(::Carbon::SemIR::FormParamPattern) Sep() \
  225. X(::Carbon::SemIR::OutParamPattern) Sep() \
  226. X(::Carbon::SemIR::RefParamPattern) Sep() \
  227. X(::Carbon::SemIR::ValueParamPattern)
  228. // clang-format on
  229. #define AnyLeafParamPattern_CARBON_KIND_ANY_EXPAND \
  230. CARBON_INST_CATEGORY_ANY_EXPAND(AnyLeafParamPattern)
  231. // A pattern that represents a `Call` parameter.
  232. struct AnyLeafParamPattern {
  233. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyLeafParamPattern);
  234. InstKind kind;
  235. // Always a PatternType.
  236. TypeId type_id;
  237. // A name to associate with this parameter in pretty-printed IR. This is not
  238. // necessarily unique, and can even be `None`; it has no semantic
  239. // significance.
  240. NameId pretty_name_id;
  241. AnyRawId arg1 = AnyRawId(AnyIdBase::NoneIndex);
  242. };
  243. // clang-format off
  244. #define AnyParamPattern_CARBON_INST_CATEGORY(X, Sep) \
  245. AnyLeafParamPattern_CARBON_INST_CATEGORY(X, Sep) Sep() \
  246. X(::Carbon::SemIR::VarParamPattern)
  247. // clang-format on
  248. #define AnyParamPattern_CARBON_KIND_ANY_EXPAND \
  249. CARBON_INST_CATEGORY_ANY_EXPAND(AnyParamPattern)
  250. // A pattern that represents a `Call` parameter.
  251. struct AnyParamPattern {
  252. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyParamPattern);
  253. InstKind kind;
  254. // Always a PatternType.
  255. TypeId type_id;
  256. AnyRawId arg0;
  257. AnyRawId arg1;
  258. };
  259. // clang-format off
  260. #define AnyPrimitiveForm_CARBON_INST_CATEGORY(X, Sep) \
  261. X(::Carbon::SemIR::InitForm) Sep() \
  262. X(::Carbon::SemIR::RefForm) Sep() \
  263. X(::Carbon::SemIR::ValueForm)
  264. // clang-format on
  265. #define AnyPrimitiveForm_CARBON_KIND_ANY_EXPAND \
  266. CARBON_INST_CATEGORY_ANY_EXPAND(AnyPrimitiveForm)
  267. // An inst that represents a primitive form.
  268. struct AnyPrimitiveForm {
  269. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyPrimitiveForm);
  270. InstKind kind;
  271. // Always FormType.
  272. TypeId type_id;
  273. // The type component of the form.
  274. TypeInstId type_component_id;
  275. };
  276. // clang-format off
  277. #define AnyQualifiedType_CARBON_INST_CATEGORY(X, Sep) \
  278. X(::Carbon::SemIR::ConstType) Sep() \
  279. X(::Carbon::SemIR::MaybeUnformedType) Sep() \
  280. X(::Carbon::SemIR::PartialType)
  281. // clang-format on
  282. #define AnyQualifiedType_CARBON_KIND_ANY_EXPAND \
  283. CARBON_INST_CATEGORY_ANY_EXPAND(AnyQualifiedType)
  284. // A type qualifier that wraps another type and has the same object
  285. // representation. Qualifiers are arranged so that adding a qualifier is
  286. // generally safe, and removing a qualifier is not necessarily safe or correct.
  287. struct AnyQualifiedType {
  288. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyQualifiedType);
  289. InstKind kind;
  290. TypeId type_id;
  291. TypeInstId inner_id;
  292. };
  293. // clang-format off
  294. #define AnyStructType_CARBON_INST_CATEGORY(X, Sep) \
  295. X(::Carbon::SemIR::CustomLayoutType) Sep() \
  296. X(::Carbon::SemIR::StructType)
  297. // clang-format on
  298. #define AnyStructType_CARBON_KIND_ANY_EXPAND \
  299. CARBON_INST_CATEGORY_ANY_EXPAND(AnyStructType)
  300. // A struct-like type with a list of named fields.
  301. struct AnyStructType {
  302. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyStructType);
  303. InstKind kind;
  304. TypeId type_id;
  305. StructTypeFieldsId fields_id;
  306. AnyRawId arg1;
  307. };
  308. // clang-format off
  309. #define AnyVarPattern_CARBON_INST_CATEGORY(X, Sep) \
  310. X(::Carbon::SemIR::VarParamPattern) Sep() \
  311. X(::Carbon::SemIR::VarPattern)
  312. // clang-format on
  313. #define AnyVarPattern_CARBON_KIND_ANY_EXPAND \
  314. CARBON_INST_CATEGORY_ANY_EXPAND(AnyVarPattern)
  315. // A `var` pattern.
  316. struct AnyVarPattern {
  317. using CategoryInfo = CARBON_INST_CATEGORY_INFO(AnyVarPattern);
  318. InstKind kind;
  319. // Always a PatternType.
  320. TypeId type_id;
  321. // The pattern nested inside the `var`.
  322. InstId subpattern_id;
  323. };
  324. } // namespace Carbon::SemIR
  325. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_CATEGORIES_H_