function.h 4.5 KB

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