function.h 5.1 KB

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