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