function.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 : uint8_t { 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. // Which, if any, virtual modifier (virtual, abstract, or impl) is applied to
  24. // this function.
  25. VirtualModifier virtual_modifier;
  26. // The implicit self parameter, if any, in implicit_param_patterns_id from
  27. // EntityWithParamsBase.
  28. InstId self_param_id = SemIR::InstId::None;
  29. // The following member is set on the first call to the function, or at the
  30. // point where the function is defined.
  31. // The following members are set at the end of a builtin function definition.
  32. // If this is a builtin function, the corresponding builtin kind.
  33. BuiltinFunctionKind builtin_function_kind = BuiltinFunctionKind::None;
  34. // The following members are accumulated throughout the function definition.
  35. // A list of the statically reachable code blocks in the body of the
  36. // function, in lexical order. The first block is the entry block. This will
  37. // be empty for declarations that don't have a visible definition.
  38. llvm::SmallVector<InstBlockId> body_block_ids = {};
  39. };
  40. // A function. See EntityWithParamsBase regarding the inheritance here.
  41. struct Function : public EntityWithParamsBase,
  42. public FunctionFields,
  43. public Printable<Function> {
  44. struct ParamPatternInfo {
  45. InstId inst_id;
  46. AnyParamPattern inst;
  47. EntityNameId entity_name_id;
  48. };
  49. auto Print(llvm::raw_ostream& out) const -> void {
  50. out << "{";
  51. PrintBaseFields(out);
  52. if (return_slot_pattern_id.has_value()) {
  53. out << ", return_slot_pattern: " << return_slot_pattern_id;
  54. }
  55. if (!body_block_ids.empty()) {
  56. out << llvm::formatv(
  57. ", body: [{0}]",
  58. llvm::make_range(body_block_ids.begin(), body_block_ids.end()));
  59. }
  60. out << "}";
  61. }
  62. // Given an instruction from `param_patterns_id` or
  63. // `implicit_param_patterns_id`, returns a `ParamPatternInfo` value with the
  64. // corresponding instruction, its ID, and the entity_name_id of the underlying
  65. // binding pattern.
  66. static auto GetParamPatternInfoFromPatternId(const File& sem_ir,
  67. InstId param_pattern_id)
  68. -> ParamPatternInfo;
  69. // Gets the name from the name binding instruction, or `None` if this pattern
  70. // has been replaced with BuiltinErrorInst.
  71. static auto GetNameFromPatternId(const File& sem_ir, InstId param_pattern_id)
  72. -> SemIR::NameId;
  73. // Gets the declared return type for a specific version of this function, or
  74. // the canonical return type for the original declaration no specific is
  75. // specified. Returns `None` if no return type was specified, in which
  76. // case the effective return type is an empty tuple.
  77. auto GetDeclaredReturnType(const File& file,
  78. SpecificId specific_id = SpecificId::None) const
  79. -> TypeId;
  80. };
  81. class File;
  82. struct CalleeFunction {
  83. // The function. `None` if not a function.
  84. SemIR::FunctionId function_id;
  85. // The specific that contains the function.
  86. SemIR::SpecificId enclosing_specific_id;
  87. // The specific for the callee itself, in a resolved call.
  88. SemIR::SpecificId resolved_specific_id;
  89. // The bound `Self` type. `None` if not a bound interface member.
  90. SemIR::InstId self_type_id;
  91. // The bound `self` parameter. `None` if not a method.
  92. SemIR::InstId self_id;
  93. // True if an error instruction was found.
  94. bool is_error;
  95. };
  96. // Returns information for the function corresponding to callee_id.
  97. auto GetCalleeFunction(const File& sem_ir, InstId callee_id) -> CalleeFunction;
  98. } // namespace Carbon::SemIR
  99. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_