function.h 14 KB

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