function.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include "toolchain/check/function.h"
  5. #include "toolchain/check/merge.h"
  6. #include "toolchain/check/subst.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::Check {
  9. // Returns false if the provided function declarations differ.
  10. static auto CheckRedecl(Context& context, const SemIR::Function& new_function,
  11. const SemIR::Function& prev_function,
  12. Substitutions substitutions) -> bool {
  13. if (!CheckRedeclParamsMatch(context, DeclParams(new_function),
  14. DeclParams(prev_function), substitutions)) {
  15. return false;
  16. }
  17. if (new_function.return_type_id == SemIR::TypeId::Error ||
  18. prev_function.return_type_id == SemIR::TypeId::Error) {
  19. return false;
  20. }
  21. auto prev_return_type_id =
  22. prev_function.return_type_id.is_valid()
  23. ? SubstType(context, prev_function.return_type_id, substitutions)
  24. : SemIR::TypeId::Invalid;
  25. if (new_function.return_type_id != prev_return_type_id) {
  26. CARBON_DIAGNOSTIC(
  27. FunctionRedeclReturnTypeDiffers, Error,
  28. "Function redeclaration differs because return type is `{0}`.",
  29. SemIR::TypeId);
  30. CARBON_DIAGNOSTIC(
  31. FunctionRedeclReturnTypeDiffersNoReturn, Error,
  32. "Function redeclaration differs because no return type is provided.");
  33. auto diag =
  34. new_function.return_type_id.is_valid()
  35. ? context.emitter().Build(new_function.decl_id,
  36. FunctionRedeclReturnTypeDiffers,
  37. new_function.return_type_id)
  38. : context.emitter().Build(new_function.decl_id,
  39. FunctionRedeclReturnTypeDiffersNoReturn);
  40. if (prev_return_type_id.is_valid()) {
  41. CARBON_DIAGNOSTIC(FunctionRedeclReturnTypePrevious, Note,
  42. "Previously declared with return type `{0}`.",
  43. SemIR::TypeId);
  44. diag.Note(prev_function.decl_id, FunctionRedeclReturnTypePrevious,
  45. prev_return_type_id);
  46. } else {
  47. CARBON_DIAGNOSTIC(FunctionRedeclReturnTypePreviousNoReturn, Note,
  48. "Previously declared with no return type.");
  49. diag.Note(prev_function.decl_id,
  50. FunctionRedeclReturnTypePreviousNoReturn);
  51. }
  52. diag.Emit();
  53. return false;
  54. }
  55. return true;
  56. }
  57. auto CheckFunctionTypeMatches(Context& context,
  58. SemIR::FunctionId new_function_id,
  59. SemIR::FunctionId prev_function_id,
  60. Substitutions substitutions) -> bool {
  61. return CheckRedecl(context, context.functions().Get(new_function_id),
  62. context.functions().Get(prev_function_id), substitutions);
  63. }
  64. // Returns the return slot usage for a function given the computed usage for two
  65. // different declarations of the function.
  66. static auto MergeReturnSlot(SemIR::Function::ReturnSlot a,
  67. SemIR::Function::ReturnSlot b)
  68. -> SemIR::Function::ReturnSlot {
  69. if (a == SemIR::Function::ReturnSlot::NotComputed) {
  70. return b;
  71. }
  72. if (b == SemIR::Function::ReturnSlot::NotComputed) {
  73. return a;
  74. }
  75. if (a == SemIR::Function::ReturnSlot::Error) {
  76. return b;
  77. }
  78. if (b == SemIR::Function::ReturnSlot::Error) {
  79. return a;
  80. }
  81. CARBON_CHECK(a == b)
  82. << "Different return slot usage computed for the same function.";
  83. return a;
  84. }
  85. auto MergeFunctionRedecl(Context& context, SemIRLoc new_loc,
  86. SemIR::Function& new_function, bool new_is_import,
  87. bool new_is_definition,
  88. SemIR::FunctionId prev_function_id,
  89. SemIR::ImportIRId prev_import_ir_id) -> bool {
  90. auto& prev_function = context.functions().Get(prev_function_id);
  91. if (!CheckRedecl(context, new_function, prev_function, {})) {
  92. return false;
  93. }
  94. CheckIsAllowedRedecl(context, Lex::TokenKind::Fn, prev_function.name_id,
  95. {.loc = new_loc,
  96. .is_definition = new_is_definition,
  97. .is_extern = new_function.is_extern},
  98. {.loc = prev_function.definition_id.is_valid()
  99. ? prev_function.definition_id
  100. : prev_function.decl_id,
  101. .is_definition = prev_function.definition_id.is_valid(),
  102. .is_extern = prev_function.is_extern},
  103. prev_import_ir_id);
  104. if (new_is_definition) {
  105. // Track the signature from the definition, so that IDs in the body
  106. // match IDs in the signature.
  107. prev_function.definition_id = new_function.definition_id;
  108. prev_function.implicit_param_refs_id = new_function.implicit_param_refs_id;
  109. prev_function.param_refs_id = new_function.param_refs_id;
  110. prev_function.return_type_id = new_function.return_type_id;
  111. prev_function.return_storage_id = new_function.return_storage_id;
  112. }
  113. // The new function might have return slot information if it was imported.
  114. prev_function.return_slot =
  115. MergeReturnSlot(prev_function.return_slot, new_function.return_slot);
  116. if ((prev_import_ir_id.is_valid() && !new_is_import) ||
  117. (prev_function.is_extern && !new_function.is_extern)) {
  118. prev_function.is_extern = new_function.is_extern;
  119. prev_function.decl_id = new_function.decl_id;
  120. ReplacePrevInstForMerge(context, prev_function.enclosing_scope_id,
  121. prev_function.name_id, new_function.decl_id);
  122. }
  123. return true;
  124. }
  125. auto CheckFunctionReturnType(Context& context, SemIRLoc loc,
  126. SemIR::Function& function) -> void {
  127. // If we have already checked the return type, we have nothing to do.
  128. if (function.return_slot != SemIR::Function::ReturnSlot::NotComputed) {
  129. return;
  130. }
  131. if (!function.return_type_id.is_valid()) {
  132. // Implicit `-> ()` has no return slot.
  133. function.return_slot = SemIR::Function::ReturnSlot::Absent;
  134. return;
  135. }
  136. // Check the return type is complete. Only diagnose incompleteness if we've
  137. // not already done so.
  138. auto diagnose_incomplete_return_type = [&] {
  139. CARBON_DIAGNOSTIC(IncompleteTypeInFunctionReturnType, Error,
  140. "Function returns incomplete type `{0}`.", SemIR::TypeId);
  141. return context.emitter().Build(loc, IncompleteTypeInFunctionReturnType,
  142. function.return_type_id);
  143. };
  144. if (!context.TryToCompleteType(
  145. function.return_type_id,
  146. function.return_slot == SemIR::Function::ReturnSlot::Error
  147. ? std::nullopt
  148. : std::optional(diagnose_incomplete_return_type))) {
  149. function.return_slot = SemIR::Function::ReturnSlot::Error;
  150. } else if (SemIR::GetInitRepr(context.sem_ir(), function.return_type_id)
  151. .has_return_slot()) {
  152. function.return_slot = SemIR::Function::ReturnSlot::Present;
  153. } else {
  154. function.return_slot = SemIR::Function::ReturnSlot::Absent;
  155. }
  156. }
  157. } // namespace Carbon::Check