inst_categories.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 = CategoryOf<X, Y, Z>;
  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. // Common representation for aggregate access nodes, which access a fixed
  29. // element of an aggregate.
  30. struct AnyAggregateAccess {
  31. using CategoryInfo =
  32. CategoryOf<ClassElementAccess, StructAccess, TupleAccess>;
  33. InstKind kind;
  34. TypeId type_id;
  35. InstId aggregate_id;
  36. ElementIndex index;
  37. };
  38. // Common representation for all kinds of aggregate initialization.
  39. struct AnyAggregateInit {
  40. using CategoryInfo = CategoryOf<ArrayInit, ClassInit, StructInit, TupleInit>;
  41. InstKind kind;
  42. TypeId type_id;
  43. InstBlockId elements_id;
  44. DestInstId dest_id;
  45. };
  46. // Common representation for all kinds of aggregate value.
  47. struct AnyAggregateValue {
  48. using CategoryInfo = CategoryOf<StructValue, TupleValue>;
  49. InstKind kind;
  50. TypeId type_id;
  51. InstBlockId elements_id;
  52. };
  53. // Common representation for various `*binding_pattern` nodes.
  54. struct AnyBindingPattern {
  55. // TODO: Also handle TemplateBindingPattern once it exists.
  56. using CategoryInfo = CategoryOf<FormBindingPattern, RefBindingPattern,
  57. SymbolicBindingPattern, ValueBindingPattern>;
  58. InstKind kind;
  59. // Always a PatternType whose scrutinee type is the declared type of the
  60. // binding.
  61. TypeId type_id;
  62. // The name declared by the binding pattern. `None` indicates that the
  63. // pattern has `_` in the name position, and so does not truly declare
  64. // a name.
  65. EntityNameId entity_name_id;
  66. };
  67. // Common representation for various `bind*` nodes.
  68. struct AnyBinding {
  69. // TODO: Also handle BindTemplateName once it exists.
  70. using CategoryInfo = CategoryOf<AliasBinding, FormBinding, RefBinding,
  71. SymbolicBinding, ValueBinding>;
  72. InstKind kind;
  73. TypeId type_id;
  74. EntityNameId entity_name_id;
  75. // The value is inline in the inst so that value access doesn't require an
  76. // indirection.
  77. InstId value_id;
  78. };
  79. // Common representation for various `bind*` nodes, and `export name`.
  80. struct AnyBindingOrExportDecl {
  81. // TODO: Also handle BindTemplateName once it exists.
  82. using CategoryInfo = CategoryOf<AliasBinding, FormBinding, RefBinding,
  83. SymbolicBinding, ValueBinding, ExportDecl>;
  84. InstKind kind;
  85. TypeId type_id;
  86. EntityNameId entity_name_id;
  87. InstId value_id;
  88. };
  89. // Common representation for all kinds of `Branch*` node.
  90. struct AnyBranch {
  91. using CategoryInfo = CategoryOf<Branch, BranchIf, BranchWithArg>;
  92. InstKind kind;
  93. // Branches don't produce a value, so have no type.
  94. LabelId target_id;
  95. // Kind-specific data.
  96. AnyRawId arg1;
  97. };
  98. // Common representation for declarations describing the foundation type of a
  99. // class -- either its adapted type or its base class.
  100. struct AnyFoundationDecl {
  101. using CategoryInfo = CategoryOf<AdaptDecl, BaseDecl>;
  102. InstKind kind;
  103. TypeId type_id;
  104. TypeInstId foundation_type_inst_id;
  105. // Kind-specific data.
  106. AnyRawId arg1;
  107. };
  108. // Common representation for all kinds of `ImportRef*` node.
  109. struct AnyImportRef {
  110. using CategoryInfo = CategoryOf<ImportRefUnloaded, ImportRefLoaded>;
  111. InstKind kind;
  112. TypeId type_id;
  113. ImportIRInstId import_ir_inst_id;
  114. // A BindName is currently only set on directly imported names. It is not
  115. // generically available.
  116. EntityNameId entity_name_id;
  117. };
  118. // A `Call` parameter for a function or other parameterized block.
  119. struct AnyParam {
  120. using CategoryInfo = CategoryOf<OutParam, RefParam, ValueParam>;
  121. InstKind kind;
  122. TypeId type_id;
  123. CallParamIndex index;
  124. // A name to associate with this Param in pretty-printed IR. This is not
  125. // necessarily unique, and can even be `None`; it has no semantic
  126. // significance.
  127. NameId pretty_name_id;
  128. };
  129. // A pattern that represents a `Call` parameter. It delegates to subpattern_id
  130. // in pattern matching.
  131. struct AnyParamPattern {
  132. using CategoryInfo =
  133. CategoryOf<FormParamPattern, OutParamPattern, RefParamPattern,
  134. ValueParamPattern, VarParamPattern>;
  135. InstKind kind;
  136. // Always a PatternType that represents the same type as the type of
  137. // `subpattern_id`.
  138. TypeId type_id;
  139. InstId subpattern_id;
  140. CallParamIndex index;
  141. };
  142. // An inst that represents a primitive form.
  143. struct AnyPrimitiveForm {
  144. using CategoryInfo = CategoryOf<InitForm, RefForm, ValueForm>;
  145. InstKind kind;
  146. // Always FormType.
  147. TypeId type_id;
  148. // The type component of the form.
  149. TypeInstId type_component_id;
  150. AnyRawId arg1;
  151. };
  152. // A type qualifier that wraps another type and has the same object
  153. // representation. Qualifiers are arranged so that adding a qualifier is
  154. // generally safe, and removing a qualifier is not necessarily safe or correct.
  155. struct AnyQualifiedType {
  156. using CategoryInfo = CategoryOf<ConstType, PartialType, MaybeUnformedType>;
  157. InstKind kind;
  158. TypeId type_id;
  159. TypeInstId inner_id;
  160. };
  161. // A struct-like type with a list of named fields.
  162. struct AnyStructType {
  163. using CategoryInfo = CategoryOf<StructType, CustomLayoutType>;
  164. InstKind kind;
  165. TypeId type_id;
  166. StructTypeFieldsId fields_id;
  167. AnyRawId arg1;
  168. };
  169. } // namespace Carbon::SemIR
  170. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_CATEGORIES_H_