function.h 11 KB

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