function.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_FUNCTION_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_
  6. #include "toolchain/base/value_store.h"
  7. #include "toolchain/sem_ir/builtin_function_kind.h"
  8. #include "toolchain/sem_ir/clang_decl.h"
  9. #include "toolchain/sem_ir/entity_with_params_base.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. #include "toolchain/sem_ir/inst_categories.h"
  12. #include "toolchain/sem_ir/typed_insts.h"
  13. namespace Carbon::SemIR {
  14. // Function-specific fields.
  15. struct FunctionFields {
  16. // Kinds of special functions.
  17. enum class SpecialFunctionKind : uint8_t {
  18. None,
  19. Builtin,
  20. Thunk,
  21. HasCppThunk,
  22. };
  23. // Kinds of virtual modifiers that can apply to functions.
  24. enum class VirtualModifier : uint8_t { None, Virtual, Abstract, Override };
  25. // The following members always have values, and do not change throughout the
  26. // lifetime of the function.
  27. // This block consists of references to the `AnyParam` insts that represent
  28. // the function's `Call` parameters. The "`Call` parameters" are the
  29. // parameters corresponding to the arguments that are passed to a `Call`
  30. // inst, so they do not include compile-time parameters, but they do include
  31. // the return slot.
  32. //
  33. // The parameters appear in declaration order: `self` (if present), then the
  34. // explicit runtime parameters, then the return slot (which is "declared" by
  35. // the function's return type declaration). This is not populated on imported
  36. // functions, because it is relevant only for a function definition.
  37. InstBlockId call_params_id;
  38. // A reference to the instruction in the entity's pattern block that depends
  39. // on all other pattern insts pertaining to the return slot pattern. This may
  40. // or may not be used by the function, depending on whether the return type
  41. // needs a return slot, but is always present if the function has a declared
  42. // return type.
  43. InstId return_slot_pattern_id;
  44. // Which kind of special function this is, if any. This is used in cases where
  45. // a special function would otherwise be indistinguishable from a normal
  46. // function.
  47. SpecialFunctionKind special_function_kind = SpecialFunctionKind::None;
  48. // Which, if any, virtual modifier (virtual, abstract, or impl) is applied to
  49. // this function.
  50. VirtualModifier virtual_modifier;
  51. // The index of the vtable slot for this virtual function. -1 if the function
  52. // is not virtual (ie: (virtual_modifier == None) == (virtual_index == -1)).
  53. int32_t virtual_index = -1;
  54. // The implicit self parameter pattern, if any, in
  55. // implicit_param_patterns_id from EntityWithParamsBase.
  56. InstId self_param_id = InstId::None;
  57. // Data that is specific to the special function kind. Use
  58. // `builtin_function_kind()`, `thunk_decl_id()` or `cpp_thunk_decl_id()` to
  59. // access this.
  60. AnyRawId special_function_kind_data = AnyRawId(AnyRawId::NoneIndex);
  61. // The following members are accumulated throughout the function definition.
  62. // A list of the statically reachable code blocks in the body of the
  63. // function, in lexical order. The first block is the entry block. This will
  64. // be empty for declarations that don't have a visible definition.
  65. llvm::SmallVector<InstBlockId> body_block_ids = {};
  66. // If the function is imported from C++, the Clang function declaration. Used
  67. // for mangling and inline function definition code generation. The AST is
  68. // owned by `CompileSubcommand` so we expect it to be live from `Function`
  69. // creation to mangling.
  70. ClangDeclId clang_decl_id = ClangDeclId::None;
  71. };
  72. // A function. See EntityWithParamsBase regarding the inheritance here.
  73. struct Function : public EntityWithParamsBase,
  74. public FunctionFields,
  75. public Printable<Function> {
  76. struct ParamPatternInfo {
  77. InstId inst_id;
  78. AnyParamPattern inst;
  79. EntityNameId entity_name_id;
  80. KnownInstId<VarPattern> var_pattern_id;
  81. };
  82. auto Print(llvm::raw_ostream& out) const -> void {
  83. out << "{";
  84. PrintBaseFields(out);
  85. if (call_params_id.has_value()) {
  86. out << ", call_params_id: " << call_params_id;
  87. }
  88. if (return_slot_pattern_id.has_value()) {
  89. out << ", return_slot_pattern: " << return_slot_pattern_id;
  90. }
  91. if (!body_block_ids.empty()) {
  92. out << llvm::formatv(
  93. ", body: [{0}]",
  94. llvm::make_range(body_block_ids.begin(), body_block_ids.end()));
  95. }
  96. out << "}";
  97. }
  98. // Returns the builtin function kind for this function, or None if this is not
  99. // a builtin function.
  100. auto builtin_function_kind() const -> BuiltinFunctionKind {
  101. return special_function_kind == SpecialFunctionKind::Builtin
  102. ? BuiltinFunctionKind::FromInt(special_function_kind_data.index)
  103. : BuiltinFunctionKind::None;
  104. }
  105. // Returns the declaration that this is a non C++ thunk for, or None if this
  106. // function is not a thunk.
  107. auto thunk_decl_id() const -> InstId {
  108. return special_function_kind == SpecialFunctionKind::Thunk
  109. ? InstId(special_function_kind_data.index)
  110. : InstId::None;
  111. }
  112. // Returns the declaration of the thunk that should be called to call this
  113. // function, or None if this function is not a C++ function that requires
  114. // calling a thunk.
  115. auto cpp_thunk_decl_id() const -> InstId {
  116. return special_function_kind == SpecialFunctionKind::HasCppThunk
  117. ? InstId(special_function_kind_data.index)
  118. : InstId::None;
  119. }
  120. // Given the ID of an instruction from `param_patterns_id` or
  121. // `implicit_param_patterns_id`, returns a `ParamPatternInfo` value with the
  122. // corresponding `Call` parameter pattern, its ID, and the entity_name_id of
  123. // the underlying binding pattern, or std::nullopt if there is no
  124. // corresponding `Call` parameter.
  125. // TODO: Remove this, by exposing `Call` parameter patterns instead of `Call`
  126. // parameters in EntityWithParams.
  127. static auto GetParamPatternInfoFromPatternId(const File& sem_ir,
  128. InstId param_pattern_id)
  129. -> std::optional<ParamPatternInfo>;
  130. // Gets the declared return type for a specific version of this function, or
  131. // the canonical return type for the original declaration no specific is
  132. // specified. Returns `None` if no return type was specified, in which
  133. // case the effective return type is an empty tuple.
  134. auto GetDeclaredReturnType(const File& file,
  135. SpecificId specific_id = SpecificId::None) const
  136. -> TypeId;
  137. // Sets that this function is a builtin function.
  138. auto SetBuiltinFunction(BuiltinFunctionKind kind) -> void {
  139. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  140. special_function_kind = SpecialFunctionKind::Builtin;
  141. special_function_kind_data = AnyRawId(kind.AsInt());
  142. }
  143. // Sets that this function is a thunk.
  144. auto SetThunk(InstId decl_id) -> void {
  145. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  146. special_function_kind = SpecialFunctionKind::Thunk;
  147. special_function_kind_data = AnyRawId(decl_id.index);
  148. }
  149. // Sets that this function is a C++ function that should be called using a C++
  150. // thunk.
  151. auto SetHasCppThunk(InstId decl_id) -> void {
  152. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  153. special_function_kind = SpecialFunctionKind::HasCppThunk;
  154. special_function_kind_data = AnyRawId(decl_id.index);
  155. }
  156. };
  157. using FunctionStore = ValueStore<FunctionId, Function>;
  158. class File;
  159. // Information about a callee that's a C++ overload set.
  160. struct CalleeCppOverloadSet {
  161. // The overload set.
  162. CppOverloadSetId cpp_overload_set_id;
  163. // The bound `self` parameter. `None` if not a method.
  164. InstId self_id;
  165. };
  166. // Information about a callee that's `ErrorInst`.
  167. struct CalleeError {};
  168. // Information about a callee that's a function.
  169. struct CalleeFunction {
  170. // The function.
  171. FunctionId function_id;
  172. // The specific that contains the function.
  173. SpecificId enclosing_specific_id;
  174. // The specific for the callee itself, in a resolved call.
  175. SpecificId resolved_specific_id;
  176. // The bound `Self` type or facet value. `None` if not a bound interface
  177. // member.
  178. InstId self_type_id;
  179. // The bound `self` parameter. `None` if not a method.
  180. InstId self_id;
  181. };
  182. // Information about a callee that may be a generic type, or could be an
  183. // invalid callee.
  184. struct CalleeNonFunction {};
  185. // A variant combining the callee forms.
  186. using Callee = std::variant<CalleeCppOverloadSet, CalleeError, CalleeFunction,
  187. CalleeNonFunction>;
  188. // Returns information for the function corresponding to callee_id.
  189. auto GetCallee(const File& sem_ir, InstId callee_id,
  190. SpecificId specific_id = SpecificId::None) -> Callee;
  191. // Like `GetCallee`, but restricts to the `Function` callee kind.
  192. auto GetCalleeAsFunction(const File& sem_ir, InstId callee_id,
  193. SpecificId specific_id = SpecificId::None)
  194. -> CalleeFunction;
  195. struct DecomposedVirtualFunction {
  196. // The canonical instruction from the `fn_decl_const_id`.
  197. InstId fn_decl_id;
  198. // The constant for the underlying instruction.
  199. ConstantId fn_decl_const_id;
  200. // The function.
  201. FunctionId function_id;
  202. // The specific for the function.
  203. SpecificId specific_id;
  204. };
  205. // Returns information for the virtual function table entry instruction.
  206. auto DecomposeVirtualFunction(const File& sem_ir, InstId fn_decl_id,
  207. SpecificId base_class_specific_id)
  208. -> DecomposedVirtualFunction;
  209. } // namespace Carbon::SemIR
  210. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_