function.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "clang/AST/Decl.h"
  7. #include "toolchain/base/value_store.h"
  8. #include "toolchain/sem_ir/builtin_function_kind.h"
  9. #include "toolchain/sem_ir/clang_decl.h"
  10. #include "toolchain/sem_ir/entity_with_params_base.h"
  11. #include "toolchain/sem_ir/ids.h"
  12. #include "toolchain/sem_ir/inst_categories.h"
  13. #include "toolchain/sem_ir/typed_insts.h"
  14. namespace Carbon::SemIR {
  15. // Function-specific fields.
  16. struct FunctionFields {
  17. // Kinds of special functions.
  18. enum class SpecialFunctionKind : uint8_t {
  19. None,
  20. Builtin,
  21. Thunk,
  22. HasCppThunk,
  23. };
  24. // Kinds of virtual modifiers that can apply to functions.
  25. enum class VirtualModifier : uint8_t { None, Virtual, Abstract, Override };
  26. // The following members always have values, and do not change throughout the
  27. // lifetime of the function.
  28. // This block consists of references to the `AnyParam` insts that represent
  29. // the function's `Call` parameters. The "`Call` parameters" are the
  30. // parameters corresponding to the arguments that are passed to a `Call`
  31. // inst, so they do not include compile-time parameters, but they do include
  32. // the return slot.
  33. //
  34. // The parameters appear in declaration order: `self` (if present), then the
  35. // explicit runtime parameters, then the return slot (which is "declared" by
  36. // the function's return type declaration). This is not populated on imported
  37. // functions, because it is relevant only for a function definition.
  38. InstBlockId call_params_id;
  39. // A reference to the instruction in the entity's pattern block that depends
  40. // on all other pattern insts pertaining to the return slot pattern. This may
  41. // or may not be used by the function, depending on whether the return type
  42. // needs a return slot, but is always present if the function has a declared
  43. // return type.
  44. InstId return_slot_pattern_id;
  45. // Which kind of special function this is, if any. This is used in cases where
  46. // a special function would otherwise be indistinguishable from a normal
  47. // function.
  48. SpecialFunctionKind special_function_kind = SpecialFunctionKind::None;
  49. // Which, if any, virtual modifier (virtual, abstract, or impl) is applied to
  50. // this function.
  51. VirtualModifier virtual_modifier;
  52. // The index of the vtable slot for this virtual function. -1 if the function
  53. // is not virtual (ie: (virtual_modifier == None) == (virtual_index == -1)).
  54. int32_t virtual_index = -1;
  55. // The implicit self parameter pattern, if any, in
  56. // implicit_param_patterns_id from EntityWithParamsBase.
  57. InstId self_param_id = InstId::None;
  58. // Data that is specific to the special function kind. Use
  59. // `builtin_function_kind()`, `thunk_decl_id()` or `cpp_thunk_decl_id()` to
  60. // access this.
  61. AnyRawId special_function_kind_data = AnyRawId(AnyRawId::NoneIndex);
  62. // The following members are accumulated throughout the function definition.
  63. // A list of the statically reachable code blocks in the body of the
  64. // function, in lexical order. The first block is the entry block. This will
  65. // be empty for declarations that don't have a visible definition.
  66. llvm::SmallVector<InstBlockId> body_block_ids = {};
  67. // If the function is imported from C++, the Clang function declaration. Used
  68. // for mangling and inline function definition code generation. The AST is
  69. // owned by `CompileSubcommand` so we expect it to be live from `Function`
  70. // creation to mangling.
  71. ClangDeclId clang_decl_id = ClangDeclId::None;
  72. };
  73. // A function. See EntityWithParamsBase regarding the inheritance here.
  74. struct Function : public EntityWithParamsBase,
  75. public FunctionFields,
  76. public Printable<Function> {
  77. struct ParamPatternInfo {
  78. InstId inst_id;
  79. AnyParamPattern inst;
  80. EntityNameId entity_name_id;
  81. KnownInstId<VarPattern> var_pattern_id;
  82. };
  83. auto Print(llvm::raw_ostream& out) const -> void {
  84. out << "{";
  85. PrintBaseFields(out);
  86. if (call_params_id.has_value()) {
  87. out << ", call_params_id: " << call_params_id;
  88. }
  89. if (return_slot_pattern_id.has_value()) {
  90. out << ", return_slot_pattern: " << return_slot_pattern_id;
  91. }
  92. if (!body_block_ids.empty()) {
  93. out << llvm::formatv(
  94. ", body: [{0}]",
  95. llvm::make_range(body_block_ids.begin(), body_block_ids.end()));
  96. }
  97. out << "}";
  98. }
  99. // Returns the builtin function kind for this function, or None if this is not
  100. // a builtin function.
  101. auto builtin_function_kind() const -> BuiltinFunctionKind {
  102. return special_function_kind == SpecialFunctionKind::Builtin
  103. ? BuiltinFunctionKind::FromInt(special_function_kind_data.index)
  104. : BuiltinFunctionKind::None;
  105. }
  106. // Returns the declaration that this is a non C++ thunk for, or None if this
  107. // function is not a thunk.
  108. auto thunk_decl_id() const -> InstId {
  109. return special_function_kind == SpecialFunctionKind::Thunk
  110. ? InstId(special_function_kind_data.index)
  111. : InstId::None;
  112. }
  113. // Returns the declaration of the thunk that should be called to call this
  114. // function, or None if this function is not a C++ function that requires
  115. // calling a thunk.
  116. auto cpp_thunk_decl_id() const -> InstId {
  117. return special_function_kind == SpecialFunctionKind::HasCppThunk
  118. ? InstId(special_function_kind_data.index)
  119. : InstId::None;
  120. }
  121. // Given the ID of an instruction from `param_patterns_id` or
  122. // `implicit_param_patterns_id`, returns a `ParamPatternInfo` value with the
  123. // corresponding `Call` parameter pattern, its ID, and the entity_name_id of
  124. // the underlying binding pattern, or std::nullopt if there is no
  125. // corresponding `Call` parameter.
  126. // TODO: Remove this, by exposing `Call` parameter patterns instead of `Call`
  127. // parameters in EntityWithParams.
  128. static auto GetParamPatternInfoFromPatternId(const File& sem_ir,
  129. InstId param_pattern_id)
  130. -> std::optional<ParamPatternInfo>;
  131. // Gets the declared return type for a specific version of this function, or
  132. // the canonical return type for the original declaration no specific is
  133. // specified. Returns `None` if no return type was specified, in which
  134. // case the effective return type is an empty tuple.
  135. auto GetDeclaredReturnType(const File& file,
  136. SpecificId specific_id = SpecificId::None) const
  137. -> TypeId;
  138. // Sets that this function is a builtin function.
  139. auto SetBuiltinFunction(BuiltinFunctionKind kind) -> void {
  140. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  141. special_function_kind = SpecialFunctionKind::Builtin;
  142. special_function_kind_data = AnyRawId(kind.AsInt());
  143. }
  144. // Sets that this function is a thunk.
  145. auto SetThunk(InstId decl_id) -> void {
  146. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  147. special_function_kind = SpecialFunctionKind::Thunk;
  148. special_function_kind_data = AnyRawId(decl_id.index);
  149. }
  150. // Sets that this function is a C++ function that should be called using a C++
  151. // thunk.
  152. auto SetHasCppThunk(InstId decl_id) -> void {
  153. CARBON_CHECK(special_function_kind == SpecialFunctionKind::None);
  154. special_function_kind = SpecialFunctionKind::HasCppThunk;
  155. special_function_kind_data = AnyRawId(decl_id.index);
  156. }
  157. };
  158. using FunctionStore = ValueStore<FunctionId, Function>;
  159. class File;
  160. struct CalleeFunction : public Printable<CalleeFunction> {
  161. // The function. `None` if not a function.
  162. FunctionId function_id;
  163. // The specific that contains the function.
  164. SpecificId enclosing_specific_id;
  165. // The specific for the callee itself, in a resolved call.
  166. SpecificId resolved_specific_id;
  167. // The bound `Self` type or facet value. `None` if not a bound interface
  168. // member.
  169. InstId self_type_id;
  170. // The bound `self` parameter. `None` if not a method.
  171. InstId self_id;
  172. // True if an error instruction was found.
  173. bool is_error;
  174. auto Print(llvm::raw_ostream& out) const -> void {
  175. out << "{function_id: " << function_id
  176. << ", enclosing_specific_id: " << enclosing_specific_id
  177. << ", resolved_specific_id: " << resolved_specific_id
  178. << ", self_type_id: " << self_type_id << ", self_id: " << self_id
  179. << ", is_error: " << is_error << "}";
  180. }
  181. };
  182. // Returns information for the function corresponding to callee_id.
  183. auto GetCalleeFunction(const File& sem_ir, InstId callee_id,
  184. SpecificId specific_id = SpecificId::None)
  185. -> CalleeFunction;
  186. struct DecomposedVirtualFunction {
  187. // The canonical instruction from the `fn_decl_const_id`.
  188. InstId fn_decl_id;
  189. // The constant for the underlying instruction.
  190. ConstantId fn_decl_const_id;
  191. // The function.
  192. FunctionId function_id;
  193. // The specific for the function.
  194. SpecificId specific_id;
  195. };
  196. // Returns information for the virtual function table entry instruction.
  197. auto DecomposeVirtualFunction(const File& sem_ir, InstId fn_decl_id,
  198. SpecificId base_class_specific_id)
  199. -> DecomposedVirtualFunction;
  200. } // namespace Carbon::SemIR
  201. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_