function.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. See `Function::Set*` for details on each; these
  17. // shouldn't be assigned directly (but are used for reads/switches).
  18. enum class SpecialFunctionKind : uint8_t {
  19. None,
  20. Builtin,
  21. CoreWitness,
  22. Thunk,
  23. HasCppThunk,
  24. };
  25. // Kinds of virtual modifiers that can apply to functions.
  26. enum class VirtualModifier : uint8_t { None, Virtual, Abstract, Override };
  27. // Kinds of evaluation modifiers that can apply to functions.
  28. enum class EvaluationMode : uint8_t { None, Eval, MustEval };
  29. // The following members always have values, and do not change throughout the
  30. // lifetime of the function.
  31. // This block consists of references to the `*ParamPattern` insts that
  32. // represent the function's `Call` parameters. The "`Call` parameters" are the
  33. // parameters corresponding to the arguments that are passed to a `Call` inst,
  34. // so they do not include compile-time parameters, but they do include the
  35. // return slot.
  36. //
  37. // The parameters appear in declaration order: `self` (if present), then the
  38. // explicit runtime parameters, then the return parameters (which are
  39. // "declared" by the function's return type declaration).
  40. InstBlockId call_param_patterns_id;
  41. // This block consists of references to the `AnyParam` insts that correspond
  42. // to call_param_patterns_id. This is not populated on imported functions,
  43. // because it is relevant only for a function definition.
  44. InstBlockId call_params_id;
  45. // The index ranges within the `Call` parameters that correspond to the
  46. // implicit parameters, explicit parameters, and return.
  47. //
  48. // Those sub-ranges are represented in terms of their end indices, but for
  49. // convenience and clarity it provides `begin`, `end`, and `size` accessors
  50. // for all three ranges, which should be preferred over directly accessing the
  51. // fields. The accessors follow STL conventions but with indices rather than
  52. // iterators (because they can index into `call_params`,
  53. // `call_param_patterns`, and the argument list): all indices in a range are
  54. // greater than or equal to `begin`, and less than `end`.
  55. class CallParamIndexRanges {
  56. public:
  57. // A CallParamIndexRanges representing an entity with no `Call` parameters.
  58. static const CallParamIndexRanges Empty;
  59. constexpr CallParamIndexRanges()
  60. : implicit_end_(CallParamIndex(0)),
  61. explicit_end_(CallParamIndex(0)),
  62. return_end_(CallParamIndex(0)) {}
  63. // Constructs a CallParamIndexRanges with the given end indices. None
  64. // of the arguments can be CallParamIndex::None.
  65. constexpr CallParamIndexRanges(CallParamIndex implicit_end,
  66. CallParamIndex explicit_end,
  67. CallParamIndex return_end)
  68. : implicit_end_(implicit_end),
  69. explicit_end_(explicit_end),
  70. return_end_(return_end) {
  71. CARBON_CHECK(implicit_end_.has_value() && explicit_end_.has_value() &&
  72. return_end_.has_value());
  73. }
  74. auto implicit_size() const -> int { return implicit_end_.index; }
  75. auto implicit_begin() const -> CallParamIndex { return CallParamIndex(0); }
  76. auto implicit_end() const -> CallParamIndex { return implicit_end_; }
  77. auto explicit_size() const -> int {
  78. return explicit_end_.index - implicit_end_.index;
  79. }
  80. auto explicit_begin() const -> CallParamIndex { return implicit_end_; }
  81. auto explicit_end() const -> CallParamIndex { return explicit_end_; }
  82. auto return_size() const -> int {
  83. return return_end_.index - explicit_end_.index;
  84. }
  85. auto return_begin() const -> CallParamIndex { return explicit_end_; }
  86. auto return_end() const -> CallParamIndex { return return_end_; }
  87. private:
  88. CallParamIndex implicit_end_;
  89. CallParamIndex explicit_end_;
  90. CallParamIndex return_end_;
  91. };
  92. CallParamIndexRanges call_param_ranges;
  93. // The inst representing the type component of return_form_inst_id.
  94. // TODO: remove this in favor of return_form_inst_id.
  95. TypeInstId return_type_inst_id;
  96. // The inst representing the function's explicitly declared return form, if
  97. // any.
  98. InstId return_form_inst_id;
  99. // The parameter pattern insts that are declared by the function's return
  100. // form declaration. They will all be OutParamPatterns, and there will be one
  101. // for each primitive initializing form in the return form, but they may or
  102. // may not be used, depending on whether the type has an in-place initializing
  103. // representation.
  104. //
  105. // Note: As of this writing we don't support non-initializing return forms,
  106. // so this will always be have exactly 1 element if the function has an
  107. // explicitly declared return type.
  108. InstBlockId return_patterns_id;
  109. // Which kind of special function this is, if any. This is used in cases where
  110. // a special function would otherwise be indistinguishable from a normal
  111. // function.
  112. SpecialFunctionKind special_function_kind = SpecialFunctionKind::None;
  113. // Which, if any, virtual modifier (virtual, abstract, or impl) is applied to
  114. // this function.
  115. VirtualModifier virtual_modifier = VirtualModifier::None;
  116. // The index of the vtable slot for this virtual function. -1 if the function
  117. // is not virtual (ie: (virtual_modifier == None) == (virtual_index == -1)).
  118. int32_t virtual_index = -1;
  119. // Which, if any, evaluation modifier (eval or musteval) is applied to this
  120. // function.
  121. EvaluationMode evaluation_mode = EvaluationMode::None;
  122. // The implicit self parameter pattern, if any, in
  123. // implicit_param_patterns_id from EntityWithParamsBase.
  124. InstId self_param_id = InstId::None;
  125. // Data that is specific to the special function kind. Use
  126. // `builtin_function_kind()`, `thunk_decl_id()` or `cpp_thunk_decl_id()` to
  127. // access this.
  128. AnyRawId special_function_kind_data = AnyRawId(AnyRawId::NoneIndex);
  129. // The following members are accumulated throughout the function definition.
  130. // A list of the statically reachable code blocks in the body of the
  131. // function, in lexical order. The first block is the entry block. This will
  132. // be empty for declarations that don't have a visible definition.
  133. llvm::SmallVector<InstBlockId> body_block_ids = {};
  134. // If the function is imported from C++, the Clang function declaration. Used
  135. // for mangling and inline function definition code generation. The AST is
  136. // owned by `CompileSubcommand` so we expect it to be live from `Function`
  137. // creation to mangling.
  138. ClangDeclId clang_decl_id = ClangDeclId::None;
  139. };
  140. inline constexpr FunctionFields::CallParamIndexRanges
  141. FunctionFields::CallParamIndexRanges::Empty;
  142. // A function. See EntityWithParamsBase regarding the inheritance here.
  143. struct Function : public EntityWithParamsBase,
  144. public FunctionFields,
  145. public Printable<Function> {
  146. struct ParamPatternInfo {
  147. InstId inst_id;
  148. AnyParamPattern inst;
  149. EntityNameId entity_name_id;
  150. };
  151. auto Print(llvm::raw_ostream& out) const -> void {
  152. out << "{";
  153. PrintBaseFields(out);
  154. if (call_param_patterns_id.has_value()) {
  155. out << ", call_param_patterns_id: " << call_param_patterns_id;
  156. }
  157. if (call_params_id.has_value()) {
  158. out << ", call_params_id: " << call_params_id;
  159. }
  160. if (return_type_inst_id.has_value()) {
  161. out << ", return_type_inst_id: " << return_type_inst_id;
  162. }
  163. if (return_type_inst_id.has_value()) {
  164. out << ", return_form_inst_id: " << return_form_inst_id;
  165. }
  166. if (return_patterns_id.has_value()) {
  167. out << ", return_patterns_id: " << return_patterns_id;
  168. }
  169. if (!body_block_ids.empty()) {
  170. out << llvm::formatv(
  171. ", body: [{0}]",
  172. llvm::make_range(body_block_ids.begin(), body_block_ids.end()));
  173. }
  174. out << "}";
  175. }
  176. // Returns the builtin function kind for this function, or None if this is not
  177. // a builtin function.
  178. auto builtin_function_kind() const -> BuiltinFunctionKind {
  179. return (special_function_kind == SpecialFunctionKind::Builtin ||
  180. special_function_kind == SpecialFunctionKind::CoreWitness)
  181. ? BuiltinFunctionKind::FromInt(special_function_kind_data.index)
  182. : BuiltinFunctionKind::None;
  183. }
  184. // Returns the declaration that this is a non C++ thunk for, or None if this
  185. // function is not a thunk.
  186. auto thunk_decl_id() const -> InstId {
  187. return special_function_kind == SpecialFunctionKind::Thunk
  188. ? InstId(special_function_kind_data.index)
  189. : InstId::None;
  190. }
  191. // Returns the declaration of the thunk that should be called to call this
  192. // function, or None if this function is not a C++ function that requires
  193. // calling a thunk.
  194. auto cpp_thunk_decl_id() const -> InstId {
  195. return special_function_kind == SpecialFunctionKind::HasCppThunk
  196. ? InstId(special_function_kind_data.index)
  197. : InstId::None;
  198. }
  199. // Gets the declared return type for a specific version of this function, or
  200. // the canonical return type for the original declaration no specific is
  201. // specified. Returns `None` if no return type was specified, in which
  202. // case the effective return type is an empty tuple.
  203. auto GetDeclaredReturnType(const File& file,
  204. SpecificId specific_id = SpecificId::None) const
  205. -> TypeId;
  206. // Gets the canonical declared return form for a specific version of this
  207. // function, or for the original declaration if no specific is specified.
  208. // Returns `None` if the function was declared without a return form, in which
  209. // case the effective return form is an empty tuple initializing expression.
  210. auto GetDeclaredReturnForm(const File& file,
  211. SpecificId specific_id = SpecificId::None) const
  212. -> InstId;
  213. // Sets that this function is a builtin function.
  214. auto SetBuiltinFunction(BuiltinFunctionKind kind) -> void {
  215. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  216. special_function_kind = SpecialFunctionKind::Builtin;
  217. special_function_kind_data = AnyRawId(kind.AsInt());
  218. }
  219. // Sets that this function is generated for a `Core` witness. These will
  220. // typically have a custom implementation, but may use builtin functions, such
  221. // as `NoOp`. We still track them differently in order to support mangling.
  222. auto SetCoreWitness(BuiltinFunctionKind kind = BuiltinFunctionKind::None)
  223. -> void {
  224. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  225. special_function_kind = SpecialFunctionKind::CoreWitness;
  226. special_function_kind_data = AnyRawId(kind.AsInt());
  227. }
  228. // Sets that this function is a thunk.
  229. auto SetThunk(InstId decl_id) -> void {
  230. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  231. special_function_kind = SpecialFunctionKind::Thunk;
  232. special_function_kind_data = AnyRawId(decl_id.index);
  233. }
  234. // Sets that this function is a C++ function that should be called using a C++
  235. // thunk.
  236. auto SetHasCppThunk(InstId decl_id) -> void {
  237. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  238. special_function_kind = SpecialFunctionKind::HasCppThunk;
  239. special_function_kind_data = AnyRawId(decl_id.index);
  240. }
  241. };
  242. using FunctionStore = ValueStore<FunctionId, Function, Tag<CheckIRId>>;
  243. class File;
  244. // Information about a callee that's a C++ overload set.
  245. struct CalleeCppOverloadSet {
  246. // The overload set.
  247. CppOverloadSetId cpp_overload_set_id;
  248. // The bound `self` parameter. `None` if not a method.
  249. InstId self_id;
  250. };
  251. // Information about a callee that's `ErrorInst`.
  252. struct CalleeError {};
  253. // Information about a callee that's a function.
  254. struct CalleeFunction {
  255. // The function.
  256. FunctionId function_id;
  257. // The specific that contains the function.
  258. SpecificId enclosing_specific_id;
  259. // The specific for the callee itself, in a resolved call.
  260. SpecificId resolved_specific_id;
  261. // The bound `Self` type or facet value. `None` if not a bound interface
  262. // member.
  263. InstId self_type_id;
  264. // The bound `self` parameter. `None` if not a method.
  265. InstId self_id;
  266. };
  267. // Information about a callee that may be a generic type, or could be an
  268. // invalid callee.
  269. struct CalleeNonFunction {};
  270. // A variant combining the callee forms.
  271. using Callee = std::variant<CalleeCppOverloadSet, CalleeError, CalleeFunction,
  272. CalleeNonFunction>;
  273. // Returns information for the function corresponding to callee_id in
  274. // caller_specific_id.
  275. auto GetCallee(const File& sem_ir, InstId callee_id,
  276. SpecificId caller_specific_id = SpecificId::None) -> Callee;
  277. // Like `GetCallee`, but restricts to the `Function` callee kind.
  278. //
  279. // It is invalid to call this with a callee that has an error inside it.
  280. auto GetCalleeAsFunction(const File& sem_ir, InstId callee_id,
  281. SpecificId caller_specific_id = SpecificId::None)
  282. -> CalleeFunction;
  283. struct DecomposedVirtualFunction {
  284. // The canonical instruction from the `fn_decl_const_id`.
  285. InstId fn_decl_id;
  286. // The constant for the underlying instruction.
  287. ConstantId fn_decl_const_id;
  288. // The function.
  289. FunctionId function_id;
  290. // The specific for the function.
  291. SpecificId specific_id;
  292. };
  293. // Returns information for the virtual function table entry instruction.
  294. auto DecomposeVirtualFunction(const File& sem_ir, InstId fn_decl_id,
  295. SpecificId base_class_specific_id)
  296. -> DecomposedVirtualFunction;
  297. } // namespace Carbon::SemIR
  298. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_