function.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/sem_ir/builtin_function_kind.h"
  7. #include "toolchain/sem_ir/entity_with_params_base.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. #include "toolchain/sem_ir/typed_insts.h"
  10. namespace Carbon::SemIR {
  11. // Function-specific fields.
  12. struct FunctionFields {
  13. // Kinds of virtual modifiers that can apply to functions.
  14. enum class VirtualModifier { None, Virtual, Abstract, Impl };
  15. // The following members always have values, and do not change throughout the
  16. // lifetime of the function.
  17. // A reference to the instruction in the entity's pattern block that depends
  18. // on all other pattern insts pertaining to the return slot pattern. This may
  19. // or may not be used by the function, depending on whether the return type
  20. // needs a return slot, but is always present if the function has a declared
  21. // return type.
  22. InstId return_slot_pattern_id;
  23. // The storage for the return value, which is a reference expression whose
  24. // type is the return type of the function. As with return_slot_pattern_id,
  25. // this is always present if the function has a declared return type.
  26. InstId return_slot_id;
  27. // Which, if any, virtual modifier (virtual, abstract, or impl) is applied to
  28. // this function.
  29. VirtualModifier virtual_modifier;
  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. };
  41. // A function. See EntityWithParamsBase regarding the inheritance here.
  42. struct Function : public EntityWithParamsBase,
  43. public FunctionFields,
  44. public Printable<Function> {
  45. auto Print(llvm::raw_ostream& out) const -> void {
  46. out << "{";
  47. PrintBaseFields(out);
  48. if (return_slot_id.is_valid()) {
  49. out << ", return_slot_pattern: " << return_slot_pattern_id;
  50. out << ", return_slot: " << return_slot_id;
  51. }
  52. if (!body_block_ids.empty()) {
  53. out << llvm::formatv(
  54. ", body: [{0}]",
  55. llvm::make_range(body_block_ids.begin(), body_block_ids.end()));
  56. }
  57. out << "}";
  58. }
  59. // Given an instruction from `param_patterns_id` or
  60. // `implicit_param_patterns_id`, returns a `ParamPatternInfo` value with the
  61. // corresponding instruction, its ID, and the entity_name_id of the underlying
  62. // binding pattern.
  63. struct ParamPatternInfo {
  64. InstId inst_id;
  65. AnyParamPattern inst;
  66. EntityNameId entity_name_id;
  67. auto GetNameId(const File& sem_ir) -> NameId;
  68. };
  69. static auto GetParamPatternInfoFromPatternId(const File& sem_ir,
  70. InstId param_pattern_id)
  71. -> ParamPatternInfo;
  72. // Gets the name from the name binding instruction, or invalid if this pattern
  73. // has been replaced with BuiltinError.
  74. static auto GetNameFromPatternId(const File& sem_ir, InstId param_pattern_id)
  75. -> SemIR::NameId;
  76. // Given a parameter reference instruction from `param_refs_id` or
  77. // `implicit_param_refs_id`, returns a `ParamInfo` value with the
  78. // corresponding instruction, its ID, and the name binding, if present.
  79. struct ParamInfo {
  80. InstId inst_id;
  81. AnyParam inst;
  82. std::optional<AnyBindName> bind_name;
  83. };
  84. static auto GetParamFromParamRefId(const File& sem_ir, InstId param_ref_id)
  85. -> ParamInfo;
  86. // Gets the declared return type for a specific version of this function, or
  87. // the canonical return type for the original declaration no specific is
  88. // specified. Returns `Invalid` if no return type was specified, in which
  89. // case the effective return type is an empty tuple.
  90. auto GetDeclaredReturnType(const File& file,
  91. SpecificId specific_id = SpecificId::Invalid) const
  92. -> TypeId;
  93. };
  94. class File;
  95. struct CalleeFunction {
  96. // The function. Invalid if not a function.
  97. SemIR::FunctionId function_id;
  98. // The specific that contains the function.
  99. SemIR::SpecificId enclosing_specific_id;
  100. // The specific for the callee itself, in a resolved call.
  101. SemIR::SpecificId resolved_specific_id;
  102. // The bound `self` parameter. Invalid if not a method.
  103. SemIR::InstId self_id;
  104. // True if an error instruction was found.
  105. bool is_error;
  106. };
  107. // Returns information for the function corresponding to callee_id.
  108. auto GetCalleeFunction(const File& sem_ir, InstId callee_id) -> CalleeFunction;
  109. } // namespace Carbon::SemIR
  110. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_