function.h 7.9 KB

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