function.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/sem_ir/builtin_function_kind.h"
  8. #include "toolchain/sem_ir/entity_with_params_base.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. #include "toolchain/sem_ir/typed_insts.h"
  11. namespace Carbon::SemIR {
  12. // Function-specific fields.
  13. struct FunctionFields {
  14. // Kinds of virtual modifiers that can apply to functions.
  15. enum class VirtualModifier : uint8_t { None, Virtual, Abstract, Impl };
  16. // The following members always have values, and do not change throughout the
  17. // lifetime of the function.
  18. // This block consists of references to the `AnyParam` insts that represent
  19. // the function's `Call` parameters. The "`Call` parameters" are the
  20. // parameters corresponding to the arguments that are passed to a `Call`
  21. // inst, so they do not include compile-time parameters, but they do include
  22. // the return slot.
  23. //
  24. // The parameters appear in declaration order: `self` (if present), then the
  25. // explicit runtime parameters, then the return slot (which is "declared" by
  26. // the function's return type declaration). This is not populated on imported
  27. // functions, because it is relevant only for a function definition.
  28. InstBlockId call_params_id;
  29. // A reference to the instruction in the entity's pattern block that depends
  30. // on all other pattern insts pertaining to the return slot pattern. This may
  31. // or may not be used by the function, depending on whether the return type
  32. // needs a return slot, but is always present if the function has a declared
  33. // return type.
  34. InstId return_slot_pattern_id;
  35. // Which, if any, virtual modifier (virtual, abstract, or impl) is applied to
  36. // this function.
  37. VirtualModifier virtual_modifier;
  38. // The index of the vtable slot for this virtual function. -1 if the function
  39. // is not virtual (ie: (virtual_modifier == None) == (virtual_index == -1)).
  40. int32_t virtual_index = -1;
  41. // The implicit self parameter, if any, in implicit_param_patterns_id from
  42. // EntityWithParamsBase.
  43. InstId self_param_id = InstId::None;
  44. // The following member is set on the first call to the function, or at the
  45. // point where the function is defined.
  46. // The following members are set at the end of a builtin function definition.
  47. // If this is a builtin function, the corresponding builtin kind.
  48. BuiltinFunctionKind builtin_function_kind = BuiltinFunctionKind::None;
  49. // The following members are accumulated throughout the function definition.
  50. // A list of the statically reachable code blocks in the body of the
  51. // function, in lexical order. The first block is the entry block. This will
  52. // be empty for declarations that don't have a visible definition.
  53. llvm::SmallVector<InstBlockId> body_block_ids = {};
  54. // If the function is imported from C++, points to the Clang declaration in
  55. // the AST. Used for mangling. The AST is owned by `CompileSubcommand` so we
  56. // expect it to be live from `Function` creation to mangling.
  57. // TODO: #4666 Ensure we can easily serialize/deserialize this. Consider decl
  58. // ID to point into the AST.
  59. const clang::NamedDecl* cpp_decl = nullptr;
  60. };
  61. // A function. See EntityWithParamsBase regarding the inheritance here.
  62. struct Function : public EntityWithParamsBase,
  63. public FunctionFields,
  64. public Printable<Function> {
  65. struct ParamPatternInfo {
  66. InstId inst_id;
  67. AnyParamPattern inst;
  68. EntityNameId entity_name_id;
  69. };
  70. auto Print(llvm::raw_ostream& out) const -> void {
  71. out << "{";
  72. PrintBaseFields(out);
  73. if (call_params_id.has_value()) {
  74. out << ", call_params_id: " << call_params_id;
  75. }
  76. if (return_slot_pattern_id.has_value()) {
  77. out << ", return_slot_pattern: " << return_slot_pattern_id;
  78. }
  79. if (!body_block_ids.empty()) {
  80. out << llvm::formatv(
  81. ", body: [{0}]",
  82. llvm::make_range(body_block_ids.begin(), body_block_ids.end()));
  83. }
  84. out << "}";
  85. }
  86. // Given the ID of an instruction from `param_patterns_id` or
  87. // `implicit_param_patterns_id`, returns a `ParamPatternInfo` value with the
  88. // corresponding `Call` parameter pattern, its ID, and the entity_name_id of
  89. // the underlying binding pattern, or std::nullopt if there is no
  90. // corresponding `Call` parameter.
  91. // TODO: Remove this, by exposing `Call` parameter patterns instead of `Call`
  92. // parameters in EntityWithParams.
  93. static auto GetParamPatternInfoFromPatternId(const File& sem_ir,
  94. InstId param_pattern_id)
  95. -> std::optional<ParamPatternInfo>;
  96. // Gets the declared return type for a specific version of this function, or
  97. // the canonical return type for the original declaration no specific is
  98. // specified. Returns `None` if no return type was specified, in which
  99. // case the effective return type is an empty tuple.
  100. auto GetDeclaredReturnType(const File& file,
  101. SpecificId specific_id = SpecificId::None) const
  102. -> TypeId;
  103. };
  104. class File;
  105. struct CalleeFunction {
  106. // The function. `None` if not a function.
  107. FunctionId function_id;
  108. // The specific that contains the function.
  109. SpecificId enclosing_specific_id;
  110. // The specific for the callee itself, in a resolved call.
  111. SpecificId resolved_specific_id;
  112. // The bound `Self` type or facet value. `None` if not a bound interface
  113. // member.
  114. InstId self_type_id;
  115. // The bound `self` parameter. `None` if not a method.
  116. InstId self_id;
  117. // True if an error instruction was found.
  118. bool is_error;
  119. };
  120. // Returns information for the function corresponding to callee_id.
  121. auto GetCalleeFunction(const File& sem_ir, InstId callee_id,
  122. SpecificId specific_id = SpecificId::None)
  123. -> CalleeFunction;
  124. } // namespace Carbon::SemIR
  125. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_