function.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // A value that describes whether the function uses a return slot.
  14. enum class ReturnSlot : int8_t {
  15. // Not yet known: the function has not been called or defined.
  16. NotComputed,
  17. // The function is known to not use a return slot.
  18. Absent,
  19. // The function has a return slot, and a call to the function is expected to
  20. // have an additional final argument corresponding to the return slot.
  21. Present,
  22. // Computing whether the function should have a return slot failed, for
  23. // example because the return type was incomplete.
  24. Error
  25. };
  26. // The following members always have values, and do not change throughout the
  27. // lifetime of the function.
  28. // The storage for the return value, which is a reference expression whose
  29. // type is the return type of the function. This may or may not be used by the
  30. // function, depending on whether the return type needs a return slot, but is
  31. // always present if the function has a declared return type.
  32. InstId return_storage_id;
  33. // Whether the declaration is extern.
  34. bool is_extern;
  35. // The following member is set on the first call to the function, or at the
  36. // point where the function is defined.
  37. // Whether the function uses a return slot. For a generic function, this
  38. // tracks information about the generic, not a specific.
  39. ReturnSlot return_slot;
  40. // The following members are set at the end of a builtin function definition.
  41. // If this is a builtin function, the corresponding builtin kind.
  42. BuiltinFunctionKind builtin_function_kind = BuiltinFunctionKind::None;
  43. // The following members are accumulated throughout the function definition.
  44. // A list of the statically reachable code blocks in the body of the
  45. // function, in lexical order. The first block is the entry block. This will
  46. // be empty for declarations that don't have a visible definition.
  47. llvm::SmallVector<InstBlockId> body_block_ids = {};
  48. };
  49. // A function. See EntityWithParamsBase regarding the inheritance here.
  50. struct Function : public EntityWithParamsBase,
  51. public FunctionFields,
  52. public Printable<Function> {
  53. auto Print(llvm::raw_ostream& out) const -> void {
  54. out << "{";
  55. PrintBaseFields(out);
  56. if (return_storage_id.is_valid()) {
  57. out << ", return_storage: " << return_storage_id;
  58. out << ", return_slot: ";
  59. switch (return_slot) {
  60. case ReturnSlot::NotComputed:
  61. out << "unknown";
  62. break;
  63. case ReturnSlot::Absent:
  64. out << "absent";
  65. break;
  66. case ReturnSlot::Present:
  67. out << "present";
  68. break;
  69. case ReturnSlot::Error:
  70. out << "error";
  71. break;
  72. }
  73. }
  74. if (!body_block_ids.empty()) {
  75. out << llvm::formatv(
  76. ", body: [{0}]",
  77. llvm::make_range(body_block_ids.begin(), body_block_ids.end()));
  78. }
  79. out << "}";
  80. }
  81. // Given a parameter reference instruction from `param_refs_id` or
  82. // `implicit_param_refs_id`, returns the corresponding `Param` instruction
  83. // and its ID.
  84. static auto GetParamFromParamRefId(const File& sem_ir, InstId param_ref_id)
  85. -> std::pair<InstId, Param>;
  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. // Returns whether the function has a return slot. Can only be called for a
  94. // function that has either been called or defined, otherwise this is not
  95. // known.
  96. //
  97. // For a generic function, this only returns information about the generic
  98. // itself, not a specific. Because a generic function can't be called (only a
  99. // specific can be), this information is only available for generic functions
  100. // that are defined.
  101. auto has_return_slot() const -> bool {
  102. CARBON_CHECK(return_slot != ReturnSlot::NotComputed);
  103. // On error, we assume no return slot is used.
  104. return return_slot == ReturnSlot::Present;
  105. }
  106. };
  107. class File;
  108. struct CalleeFunction {
  109. // The function. Invalid if not a function.
  110. SemIR::FunctionId function_id;
  111. // The specific that contains the function.
  112. SemIR::SpecificId specific_id;
  113. // The bound `self` parameter. Invalid if not a method.
  114. SemIR::InstId self_id;
  115. // True if an error instruction was found.
  116. bool is_error;
  117. };
  118. // Returns information for the function corresponding to callee_id.
  119. auto GetCalleeFunction(const File& sem_ir, InstId callee_id) -> CalleeFunction;
  120. } // namespace Carbon::SemIR
  121. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_