function.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 inst that is declared by the function's return
  101. // declaration. This will be a ReturnSlotPattern, or None if the function
  102. // doesn't have a return declaration. It may or may not be used, depending on
  103. // whether the type has an in-place initializing representation.
  104. //
  105. // TODO: Extend this to support composite return forms.
  106. InstId return_pattern_id;
  107. // Which kind of special function this is, if any. This is used in cases where
  108. // a special function would otherwise be indistinguishable from a normal
  109. // function.
  110. SpecialFunctionKind special_function_kind = SpecialFunctionKind::None;
  111. // Which, if any, virtual modifier (virtual, abstract, or impl) is applied to
  112. // this function.
  113. VirtualModifier virtual_modifier = VirtualModifier::None;
  114. // The index of the vtable slot for this virtual function. -1 if the function
  115. // is not virtual (ie: (virtual_modifier == None) == (virtual_index == -1)).
  116. int32_t virtual_index = -1;
  117. // Which, if any, evaluation modifier (eval or musteval) is applied to this
  118. // function.
  119. EvaluationMode evaluation_mode = EvaluationMode::None;
  120. // The implicit self parameter pattern, if any, in
  121. // implicit_param_patterns_id from EntityWithParamsBase.
  122. InstId self_param_id = InstId::None;
  123. // Data that is specific to the special function kind. Use
  124. // `builtin_function_kind()`, `thunk_decl_id()` or `cpp_thunk_decl_id()` to
  125. // access this.
  126. AnyRawId special_function_kind_data = AnyRawId(AnyRawId::NoneIndex);
  127. // The following members are accumulated throughout the function definition.
  128. // A list of the statically reachable code blocks in the body of the
  129. // function, in lexical order. The first block is the entry block. This will
  130. // be empty for declarations that don't have a visible definition.
  131. llvm::SmallVector<InstBlockId> body_block_ids = {};
  132. // If the function is imported from C++, the Clang function declaration. Used
  133. // for mangling and inline function definition code generation. The AST is
  134. // owned by `CompileSubcommand` so we expect it to be live from `Function`
  135. // creation to mangling.
  136. ClangDeclId clang_decl_id = ClangDeclId::None;
  137. };
  138. inline constexpr FunctionFields::CallParamIndexRanges
  139. FunctionFields::CallParamIndexRanges::Empty;
  140. // A function. See EntityWithParamsBase regarding the inheritance here.
  141. struct Function : public EntityWithParamsBase,
  142. public FunctionFields,
  143. public Printable<Function> {
  144. struct ParamPatternInfo {
  145. InstId inst_id;
  146. AnyParamPattern inst;
  147. EntityNameId entity_name_id;
  148. };
  149. auto Print(llvm::raw_ostream& out) const -> void {
  150. out << "{";
  151. PrintBaseFields(out);
  152. if (call_param_patterns_id.has_value()) {
  153. out << ", call_param_patterns_id: " << call_param_patterns_id;
  154. }
  155. if (call_params_id.has_value()) {
  156. out << ", call_params_id: " << call_params_id;
  157. }
  158. if (return_type_inst_id.has_value()) {
  159. out << ", return_type_inst_id: " << return_type_inst_id;
  160. }
  161. if (return_type_inst_id.has_value()) {
  162. out << ", return_form_inst_id: " << return_form_inst_id;
  163. }
  164. if (return_pattern_id.has_value()) {
  165. out << ", return_pattern_id: " << return_pattern_id;
  166. }
  167. if (!body_block_ids.empty()) {
  168. out << llvm::formatv(
  169. ", body: [{0}]",
  170. llvm::make_range(body_block_ids.begin(), body_block_ids.end()));
  171. }
  172. out << "}";
  173. }
  174. // Returns the builtin function kind for this function, or None if this is not
  175. // a builtin function.
  176. auto builtin_function_kind() const -> BuiltinFunctionKind {
  177. return (special_function_kind == SpecialFunctionKind::Builtin ||
  178. special_function_kind == SpecialFunctionKind::CoreWitness)
  179. ? BuiltinFunctionKind::FromInt(special_function_kind_data.index)
  180. : BuiltinFunctionKind::None;
  181. }
  182. // Returns the declaration that this is a non C++ thunk for, or None if this
  183. // function is not a thunk.
  184. auto thunk_decl_id() const -> InstId {
  185. return special_function_kind == SpecialFunctionKind::Thunk
  186. ? InstId(special_function_kind_data.index)
  187. : InstId::None;
  188. }
  189. // Returns the declaration of the thunk that should be called to call this
  190. // function, or None if this function is not a C++ function that requires
  191. // calling a thunk.
  192. auto cpp_thunk_decl_id() const -> InstId {
  193. return special_function_kind == SpecialFunctionKind::HasCppThunk
  194. ? InstId(special_function_kind_data.index)
  195. : InstId::None;
  196. }
  197. // Gets the `InstId` of the C++ function called by this thunk.
  198. auto cpp_thunk_callee() const -> InstId {
  199. return special_function_kind == SpecialFunctionKind::CppThunk
  200. ? InstId(special_function_kind_data.index)
  201. : InstId::None;
  202. }
  203. // Gets the declared return type for a specific version of this function, or
  204. // the canonical return type for the original declaration no specific is
  205. // specified. Returns `None` if no return type was specified, in which
  206. // case the effective return type is an empty tuple.
  207. auto GetDeclaredReturnType(const File& file,
  208. SpecificId specific_id = SpecificId::None) const
  209. -> TypeId;
  210. // Gets the canonical declared return form for a specific version of this
  211. // function, or for the original declaration if no specific is specified.
  212. // Returns `None` if the function was declared without a return form, in which
  213. // case the effective return form is an empty tuple initializing expression.
  214. auto GetDeclaredReturnForm(const File& file,
  215. SpecificId specific_id = SpecificId::None) const
  216. -> InstId;
  217. // Sets that this function is a builtin function.
  218. auto SetBuiltinFunction(BuiltinFunctionKind kind) -> void {
  219. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  220. special_function_kind = SpecialFunctionKind::Builtin;
  221. special_function_kind_data = AnyRawId(kind.AsInt());
  222. }
  223. // Sets that this function is generated for a `Core` witness. These will
  224. // typically have a custom implementation for a `None` kind, but may use
  225. // builtin functions, most often `NoOp`. We still track them differently in
  226. // order to support mangling.
  227. auto SetCoreWitness(BuiltinFunctionKind kind) -> void {
  228. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  229. special_function_kind = SpecialFunctionKind::CoreWitness;
  230. special_function_kind_data = AnyRawId(kind.AsInt());
  231. }
  232. // Sets that this function is a thunk.
  233. auto SetThunk(InstId decl_id) -> void {
  234. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  235. special_function_kind = SpecialFunctionKind::Thunk;
  236. special_function_kind_data = AnyRawId(decl_id.index);
  237. }
  238. // Sets that this function is a C++ thunk.
  239. auto SetCppThunk(InstId decl_id) -> void {
  240. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  241. special_function_kind = SpecialFunctionKind::CppThunk;
  242. special_function_kind_data = AnyRawId(decl_id.index);
  243. }
  244. // Sets that this function is a C++ function that should be called using a C++
  245. // thunk.
  246. auto SetHasCppThunk(InstId decl_id) -> void {
  247. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  248. special_function_kind = SpecialFunctionKind::HasCppThunk;
  249. special_function_kind_data = AnyRawId(decl_id.index);
  250. }
  251. };
  252. using FunctionStore = ValueStore<FunctionId, Function, Tag<CheckIRId>>;
  253. class File;
  254. // Information about a callee that's a C++ overload set.
  255. struct CalleeCppOverloadSet {
  256. // The overload set.
  257. CppOverloadSetId cpp_overload_set_id;
  258. // The bound `self` parameter. `None` if not a method.
  259. InstId self_id;
  260. };
  261. // Information about a callee that's `ErrorInst`.
  262. struct CalleeError {};
  263. // Information about a callee that's a function.
  264. struct CalleeFunction {
  265. // The function.
  266. FunctionId function_id;
  267. // The specific that contains the function.
  268. SpecificId enclosing_specific_id;
  269. // The specific for the callee itself, in a resolved call.
  270. SpecificId resolved_specific_id;
  271. // The bound `Self` type or facet value. `None` if not a bound interface
  272. // member.
  273. InstId self_type_id;
  274. // The bound `self` parameter. `None` if not a method.
  275. InstId self_id;
  276. };
  277. // Information about a callee that may be a generic type, or could be an
  278. // invalid callee.
  279. struct CalleeNonFunction {};
  280. // A variant combining the callee forms.
  281. using Callee = std::variant<CalleeCppOverloadSet, CalleeError, CalleeFunction,
  282. CalleeNonFunction>;
  283. // Returns information for the function corresponding to callee_id in
  284. // caller_specific_id.
  285. auto GetCallee(const File& sem_ir, InstId callee_id,
  286. SpecificId caller_specific_id = SpecificId::None) -> Callee;
  287. // Like `GetCallee`, but restricts to the `Function` callee kind.
  288. //
  289. // It is invalid to call this with a callee that has an error inside it.
  290. auto GetCalleeAsFunction(const File& sem_ir, InstId callee_id,
  291. SpecificId caller_specific_id = SpecificId::None)
  292. -> CalleeFunction;
  293. struct DecomposedVirtualFunction {
  294. // The canonical instruction from the `fn_decl_const_id`.
  295. InstId fn_decl_id;
  296. // The constant for the underlying instruction.
  297. ConstantId fn_decl_const_id;
  298. // The function.
  299. FunctionId function_id;
  300. // The specific for the function.
  301. SpecificId specific_id;
  302. };
  303. // Returns information for the virtual function table entry instruction.
  304. auto DecomposeVirtualFunction(const File& sem_ir, InstId fn_decl_id,
  305. SpecificId base_class_specific_id)
  306. -> DecomposedVirtualFunction;
  307. } // namespace Carbon::SemIR
  308. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_