inst_categories.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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<BindingPattern, SymbolicBindingPattern>;
  57. InstKind kind;
  58. // Always a PatternType whose scrutinee type is the declared type of the
  59. // binding.
  60. TypeId type_id;
  61. // The name declared by the binding pattern. `None` indicates that the
  62. // pattern has `_` in the name position, and so does not truly declare
  63. // a name.
  64. EntityNameId entity_name_id;
  65. };
  66. // Common representation for various `bind*` nodes.
  67. struct AnyBindName {
  68. // TODO: Also handle BindTemplateName once it exists.
  69. using CategoryInfo = CategoryOf<BindAlias, BindName, BindSymbolicName>;
  70. InstKind kind;
  71. TypeId type_id;
  72. EntityNameId entity_name_id;
  73. InstId value_id;
  74. };
  75. // Common representation for various `bind*` nodes, and `export name`.
  76. struct AnyBindNameOrExportDecl {
  77. // TODO: Also handle BindTemplateName once it exists.
  78. using CategoryInfo =
  79. CategoryOf<BindAlias, BindName, BindSymbolicName, ExportDecl>;
  80. InstKind kind;
  81. TypeId type_id;
  82. EntityNameId entity_name_id;
  83. InstId value_id;
  84. };
  85. // Common representation for all kinds of `Branch*` node.
  86. struct AnyBranch {
  87. using CategoryInfo = CategoryOf<Branch, BranchIf, BranchWithArg>;
  88. InstKind kind;
  89. // Branches don't produce a value, so have no type.
  90. LabelId target_id;
  91. // Kind-specific data.
  92. AnyRawId arg1;
  93. };
  94. // Common representation for declarations describing the foundation type of a
  95. // class -- either its adapted type or its base class.
  96. struct AnyFoundationDecl {
  97. using CategoryInfo = CategoryOf<AdaptDecl, BaseDecl>;
  98. InstKind kind;
  99. TypeId type_id;
  100. TypeInstId foundation_type_inst_id;
  101. // Kind-specific data.
  102. AnyRawId arg1;
  103. };
  104. // Common representation for all kinds of `ImportRef*` node.
  105. struct AnyImportRef {
  106. using CategoryInfo = CategoryOf<ImportRefUnloaded, ImportRefLoaded>;
  107. InstKind kind;
  108. TypeId type_id;
  109. ImportIRInstId import_ir_inst_id;
  110. // A BindName is currently only set on directly imported names. It is not
  111. // generically available.
  112. EntityNameId entity_name_id;
  113. };
  114. // A `Call` parameter for a function or other parameterized block.
  115. struct AnyParam {
  116. using CategoryInfo = CategoryOf<OutParam, RefParam, ValueParam>;
  117. InstKind kind;
  118. TypeId type_id;
  119. CallParamIndex index;
  120. // A name to associate with this Param in pretty-printed IR. This is not
  121. // necessarily unique, and can even be `None`; it has no semantic
  122. // significance.
  123. NameId pretty_name_id;
  124. };
  125. // A pattern that represents a `Call` parameter. It delegates to subpattern_id
  126. // in pattern matching. The sub-kinds differ only in the expression category
  127. // of the corresponding parameter inst.
  128. struct AnyParamPattern {
  129. using CategoryInfo =
  130. CategoryOf<OutParamPattern, RefParamPattern, ValueParamPattern>;
  131. InstKind kind;
  132. // Always a PatternType that represents the same type as the type of
  133. // `subpattern_id`.
  134. TypeId type_id;
  135. InstId subpattern_id;
  136. CallParamIndex index;
  137. };
  138. // A struct-like type with a list of named fields.
  139. struct AnyStructType {
  140. using CategoryInfo = CategoryOf<StructType, CustomLayoutType>;
  141. InstKind kind;
  142. TypeId type_id;
  143. StructTypeFieldsId fields_id;
  144. AnyRawId arg1;
  145. };
  146. } // namespace Carbon::SemIR
  147. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_CATEGORIES_H_