function.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 `*ParamPattern` insts that
  28. // represent the function's `Call` parameters. The "`Call` parameters" are the
  29. // parameters corresponding to the arguments that are passed to a `Call` inst,
  30. // so they do not include compile-time parameters, but they do include the
  31. // return slot.
  32. //
  33. // The parameters appear in declaration order: `self` (if present), then the
  34. // explicit runtime parameters, then the return parameters (which are
  35. // "declared" by the function's return type declaration).
  36. InstBlockId call_param_patterns_id;
  37. // This block consists of references to the `AnyParam` insts that correspond
  38. // to call_param_patterns_id. This is not populated on imported functions,
  39. // because it is relevant only for a function definition.
  40. InstBlockId call_params_id;
  41. // The inst representing the type component of return_form_inst_id.
  42. // TODO: remove this in favor of return_form_inst_id.
  43. TypeInstId return_type_inst_id;
  44. // The inst representing the function's explicitly declared return form, if
  45. // any.
  46. InstId return_form_inst_id;
  47. // The call parameter pattern insts that are declared by the function's return
  48. // form declaration. They will all be OutParamPatterns, and there will be one
  49. // for each primitive initializing form in the return form, but they may or
  50. // may not be used, depending on whether the type has an in-place initializing
  51. // representation.
  52. //
  53. // Note: As of this writing we don't support non-initializing return forms,
  54. // so this will always be have exactly 1 element if the function has an
  55. // explicitly declared return type.
  56. InstBlockId return_patterns_id;
  57. // Which kind of special function this is, if any. This is used in cases where
  58. // a special function would otherwise be indistinguishable from a normal
  59. // function.
  60. SpecialFunctionKind special_function_kind = SpecialFunctionKind::None;
  61. // Which, if any, virtual modifier (virtual, abstract, or impl) is applied to
  62. // this function.
  63. VirtualModifier virtual_modifier = VirtualModifier::None;
  64. // The index of the vtable slot for this virtual function. -1 if the function
  65. // is not virtual (ie: (virtual_modifier == None) == (virtual_index == -1)).
  66. int32_t virtual_index = -1;
  67. // The implicit self parameter pattern, if any, in
  68. // implicit_param_patterns_id from EntityWithParamsBase.
  69. InstId self_param_id = InstId::None;
  70. // Data that is specific to the special function kind. Use
  71. // `builtin_function_kind()`, `thunk_decl_id()` or `cpp_thunk_decl_id()` to
  72. // access this.
  73. AnyRawId special_function_kind_data = AnyRawId(AnyRawId::NoneIndex);
  74. // The following members are accumulated throughout the function definition.
  75. // A list of the statically reachable code blocks in the body of the
  76. // function, in lexical order. The first block is the entry block. This will
  77. // be empty for declarations that don't have a visible definition.
  78. llvm::SmallVector<InstBlockId> body_block_ids = {};
  79. // If the function is imported from C++, the Clang function declaration. Used
  80. // for mangling and inline function definition code generation. The AST is
  81. // owned by `CompileSubcommand` so we expect it to be live from `Function`
  82. // creation to mangling.
  83. ClangDeclId clang_decl_id = ClangDeclId::None;
  84. };
  85. // A function. See EntityWithParamsBase regarding the inheritance here.
  86. struct Function : public EntityWithParamsBase,
  87. public FunctionFields,
  88. public Printable<Function> {
  89. struct ParamPatternInfo {
  90. InstId inst_id;
  91. AnyParamPattern inst;
  92. EntityNameId entity_name_id;
  93. };
  94. auto Print(llvm::raw_ostream& out) const -> void {
  95. out << "{";
  96. PrintBaseFields(out);
  97. if (call_param_patterns_id.has_value()) {
  98. out << ", call_param_patterns_id: " << call_param_patterns_id;
  99. }
  100. if (call_params_id.has_value()) {
  101. out << ", call_params_id: " << call_params_id;
  102. }
  103. if (return_type_inst_id.has_value()) {
  104. out << ", return_type_inst_id: " << return_type_inst_id;
  105. }
  106. if (return_type_inst_id.has_value()) {
  107. out << ", return_form_inst_id: " << return_form_inst_id;
  108. }
  109. if (return_patterns_id.has_value()) {
  110. out << ", return_patterns_id: " << return_patterns_id;
  111. }
  112. if (!body_block_ids.empty()) {
  113. out << llvm::formatv(
  114. ", body: [{0}]",
  115. llvm::make_range(body_block_ids.begin(), body_block_ids.end()));
  116. }
  117. out << "}";
  118. }
  119. // Returns the builtin function kind for this function, or None if this is not
  120. // a builtin function.
  121. auto builtin_function_kind() const -> BuiltinFunctionKind {
  122. return special_function_kind == SpecialFunctionKind::Builtin
  123. ? BuiltinFunctionKind::FromInt(special_function_kind_data.index)
  124. : BuiltinFunctionKind::None;
  125. }
  126. // Returns the declaration that this is a non C++ thunk for, or None if this
  127. // function is not a thunk.
  128. auto thunk_decl_id() const -> InstId {
  129. return special_function_kind == SpecialFunctionKind::Thunk
  130. ? InstId(special_function_kind_data.index)
  131. : InstId::None;
  132. }
  133. // Returns the declaration of the thunk that should be called to call this
  134. // function, or None if this function is not a C++ function that requires
  135. // calling a thunk.
  136. auto cpp_thunk_decl_id() const -> InstId {
  137. return special_function_kind == SpecialFunctionKind::HasCppThunk
  138. ? InstId(special_function_kind_data.index)
  139. : InstId::None;
  140. }
  141. // Gets the declared return type for a specific version of this function, or
  142. // the canonical return type for the original declaration no specific is
  143. // specified. Returns `None` if no return type was specified, in which
  144. // case the effective return type is an empty tuple.
  145. auto GetDeclaredReturnType(const File& file,
  146. SpecificId specific_id = SpecificId::None) const
  147. -> TypeId;
  148. // Gets the canonical declared return form for a specific version of this
  149. // function, or for the original declaration if no specific is specified.
  150. // Returns `None` if the function was declared without a return form, in which
  151. // case the effective return form is an empty tuple initializing expression.
  152. auto GetDeclaredReturnForm(const File& file,
  153. SpecificId specific_id = SpecificId::None) const
  154. -> InstId;
  155. // Sets that this function is a builtin function.
  156. auto SetBuiltinFunction(BuiltinFunctionKind kind) -> void {
  157. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  158. special_function_kind = SpecialFunctionKind::Builtin;
  159. special_function_kind_data = AnyRawId(kind.AsInt());
  160. }
  161. // Sets that this function is a thunk.
  162. auto SetThunk(InstId decl_id) -> void {
  163. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  164. special_function_kind = SpecialFunctionKind::Thunk;
  165. special_function_kind_data = AnyRawId(decl_id.index);
  166. }
  167. // Sets that this function is a C++ function that should be called using a C++
  168. // thunk.
  169. auto SetHasCppThunk(InstId decl_id) -> void {
  170. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  171. special_function_kind = SpecialFunctionKind::HasCppThunk;
  172. special_function_kind_data = AnyRawId(decl_id.index);
  173. }
  174. };
  175. using FunctionStore = ValueStore<FunctionId, Function, Tag<CheckIRId>>;
  176. class File;
  177. // Information about a callee that's a C++ overload set.
  178. struct CalleeCppOverloadSet {
  179. // The overload set.
  180. CppOverloadSetId cpp_overload_set_id;
  181. // The bound `self` parameter. `None` if not a method.
  182. InstId self_id;
  183. };
  184. // Information about a callee that's `ErrorInst`.
  185. struct CalleeError {};
  186. // Information about a callee that's a function.
  187. struct CalleeFunction {
  188. // The function.
  189. FunctionId function_id;
  190. // The specific that contains the function.
  191. SpecificId enclosing_specific_id;
  192. // The specific for the callee itself, in a resolved call.
  193. SpecificId resolved_specific_id;
  194. // The bound `Self` type or facet value. `None` if not a bound interface
  195. // member.
  196. InstId self_type_id;
  197. // The bound `self` parameter. `None` if not a method.
  198. InstId self_id;
  199. };
  200. // Information about a callee that may be a generic type, or could be an
  201. // invalid callee.
  202. struct CalleeNonFunction {};
  203. // A variant combining the callee forms.
  204. using Callee = std::variant<CalleeCppOverloadSet, CalleeError, CalleeFunction,
  205. CalleeNonFunction>;
  206. // Returns information for the function corresponding to callee_id in
  207. // caller_specific_id.
  208. auto GetCallee(const File& sem_ir, InstId callee_id,
  209. SpecificId caller_specific_id = SpecificId::None) -> Callee;
  210. // Like `GetCallee`, but restricts to the `Function` callee kind.
  211. auto GetCalleeAsFunction(const File& sem_ir, InstId callee_id,
  212. SpecificId caller_specific_id = SpecificId::None)
  213. -> CalleeFunction;
  214. struct DecomposedVirtualFunction {
  215. // The canonical instruction from the `fn_decl_const_id`.
  216. InstId fn_decl_id;
  217. // The constant for the underlying instruction.
  218. ConstantId fn_decl_const_id;
  219. // The function.
  220. FunctionId function_id;
  221. // The specific for the function.
  222. SpecificId specific_id;
  223. };
  224. // Returns information for the virtual function table entry instruction.
  225. auto DecomposeVirtualFunction(const File& sem_ir, InstId fn_decl_id,
  226. SpecificId base_class_specific_id)
  227. -> DecomposedVirtualFunction;
  228. } // namespace Carbon::SemIR
  229. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_