function.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 << ", enclosing_scope: " << enclosing_scope_id
  27. << ", param_refs: " << param_refs_id;
  28. if (return_type_id.is_valid()) {
  29. out << ", return_type: " << return_type_id;
  30. }
  31. if (return_storage_id.is_valid()) {
  32. out << ", return_storage: " << return_storage_id;
  33. out << ", return_slot: ";
  34. switch (return_slot) {
  35. case ReturnSlot::NotComputed:
  36. out << "unknown";
  37. break;
  38. case ReturnSlot::Absent:
  39. out << "absent";
  40. break;
  41. case ReturnSlot::Present:
  42. out << "present";
  43. break;
  44. case ReturnSlot::Error:
  45. out << "error";
  46. break;
  47. }
  48. }
  49. if (!body_block_ids.empty()) {
  50. out << llvm::formatv(
  51. ", body: [{0}]",
  52. llvm::make_range(body_block_ids.begin(), body_block_ids.end()));
  53. }
  54. out << "}";
  55. }
  56. // Given a parameter reference instruction from `param_refs_id` or
  57. // `implicit_param_refs_id`, returns the corresponding `Param` instruction
  58. // and its ID.
  59. static auto GetParamFromParamRefId(const File& sem_ir, InstId param_ref_id)
  60. -> std::pair<InstId, Param>;
  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 enclosing scope.
  74. NameScopeId enclosing_scope_id;
  75. // The first declaration of the function. This is a FunctionDecl.
  76. InstId decl_id;
  77. // A block containing a single reference instruction per implicit parameter.
  78. InstBlockId implicit_param_refs_id;
  79. // A block containing a single reference instruction per parameter.
  80. InstBlockId param_refs_id;
  81. // The return type. This will be invalid if the return type wasn't specified.
  82. TypeId return_type_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.
  86. InstId return_storage_id;
  87. // Whether the declaration is extern.
  88. bool is_extern;
  89. // The following member is set on the first call to the function, or at the
  90. // point where the function is defined.
  91. // Whether the function uses a return slot.
  92. ReturnSlot return_slot;
  93. // The following members are set at the end of a builtin function definition.
  94. // If this is a builtin function, the corresponding builtin kind.
  95. BuiltinFunctionKind builtin_kind = BuiltinFunctionKind::None;
  96. // The following members are set at the `{` of the function definition.
  97. // The definition, if the function has been defined or is currently being
  98. // defined. This is a FunctionDecl.
  99. InstId definition_id = InstId::Invalid;
  100. // The following members are accumulated throughout the function definition.
  101. // A list of the statically reachable code blocks in the body of the
  102. // function, in lexical order. The first block is the entry block. This will
  103. // be empty for declarations that don't have a visible definition.
  104. llvm::SmallVector<InstBlockId> body_block_ids = {};
  105. };
  106. class File;
  107. struct CalleeFunction {
  108. // The function. Invalid if not a function.
  109. SemIR::FunctionId function_id;
  110. // The bound `self` parameter. Invalid if not a method.
  111. SemIR::InstId self_id;
  112. // True if an error instruction was found.
  113. bool is_error;
  114. };
  115. // Returns information for the function corresponding to callee_id.
  116. auto GetCalleeFunction(const File& sem_ir, InstId callee_id) -> CalleeFunction;
  117. } // namespace Carbon::SemIR
  118. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_