function.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "common/find.h"
  6. #include "toolchain/check/merge.h"
  7. #include "toolchain/check/type_completion.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. #include "toolchain/sem_ir/pattern.h"
  10. namespace Carbon::Check {
  11. auto FindSelfPattern(Context& context,
  12. SemIR::InstBlockId implicit_param_patterns_id)
  13. -> SemIR::InstId {
  14. auto implicit_param_patterns =
  15. context.inst_blocks().GetOrEmpty(implicit_param_patterns_id);
  16. return FindIfOrNone(implicit_param_patterns, [&](auto implicit_param_id) {
  17. return SemIR::IsSelfPattern(context.sem_ir(), implicit_param_id);
  18. });
  19. }
  20. auto CheckFunctionReturnTypeMatches(Context& context,
  21. const SemIR::Function& new_function,
  22. const SemIR::Function& prev_function,
  23. SemIR::SpecificId prev_specific_id,
  24. bool diagnose) -> bool {
  25. // TODO: Pass a specific ID for `prev_function` instead of substitutions and
  26. // use it here.
  27. auto new_return_type_id =
  28. new_function.GetDeclaredReturnType(context.sem_ir());
  29. auto prev_return_type_id =
  30. prev_function.GetDeclaredReturnType(context.sem_ir(), prev_specific_id);
  31. if (new_return_type_id == SemIR::ErrorInst::TypeId ||
  32. prev_return_type_id == SemIR::ErrorInst::TypeId) {
  33. return false;
  34. }
  35. if (!context.types().AreEqualAcrossDeclarations(new_return_type_id,
  36. prev_return_type_id)) {
  37. if (!diagnose) {
  38. return false;
  39. }
  40. CARBON_DIAGNOSTIC(
  41. FunctionRedeclReturnTypeDiffers, Error,
  42. "function redeclaration differs because return type is {0}",
  43. SemIR::TypeId);
  44. CARBON_DIAGNOSTIC(
  45. FunctionRedeclReturnTypeDiffersNoReturn, Error,
  46. "function redeclaration differs because no return type is provided");
  47. auto diag =
  48. new_return_type_id.has_value()
  49. ? context.emitter().Build(new_function.latest_decl_id(),
  50. FunctionRedeclReturnTypeDiffers,
  51. new_return_type_id)
  52. : context.emitter().Build(new_function.latest_decl_id(),
  53. FunctionRedeclReturnTypeDiffersNoReturn);
  54. if (prev_return_type_id.has_value()) {
  55. CARBON_DIAGNOSTIC(FunctionRedeclReturnTypePrevious, Note,
  56. "previously declared with return type {0}",
  57. SemIR::TypeId);
  58. diag.Note(prev_function.latest_decl_id(),
  59. FunctionRedeclReturnTypePrevious, prev_return_type_id);
  60. } else {
  61. CARBON_DIAGNOSTIC(FunctionRedeclReturnTypePreviousNoReturn, Note,
  62. "previously declared with no return type");
  63. diag.Note(prev_function.latest_decl_id(),
  64. FunctionRedeclReturnTypePreviousNoReturn);
  65. }
  66. diag.Emit();
  67. return false;
  68. }
  69. return true;
  70. }
  71. auto CheckFunctionTypeMatches(Context& context,
  72. const SemIR::Function& new_function,
  73. const SemIR::Function& prev_function,
  74. SemIR::SpecificId prev_specific_id,
  75. bool check_syntax, bool check_self, bool diagnose)
  76. -> bool {
  77. if (!CheckRedeclParamsMatch(context, DeclParams(new_function),
  78. DeclParams(prev_function), prev_specific_id,
  79. diagnose, check_syntax, check_self)) {
  80. return false;
  81. }
  82. return CheckFunctionReturnTypeMatches(context, new_function, prev_function,
  83. prev_specific_id, diagnose);
  84. }
  85. auto CheckFunctionReturnType(Context& context, SemIR::LocId loc_id,
  86. const SemIR::Function& function,
  87. SemIR::SpecificId specific_id)
  88. -> SemIR::ReturnTypeInfo {
  89. auto return_info = SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(),
  90. function, specific_id);
  91. // If we couldn't determine the return information due to the return type
  92. // being incomplete, try to complete it now.
  93. if (return_info.init_repr.kind == SemIR::InitRepr::Incomplete) {
  94. auto diagnose_incomplete_return_type = [&] {
  95. CARBON_DIAGNOSTIC(IncompleteTypeInFunctionReturnType, Error,
  96. "function returns incomplete type {0}", SemIR::TypeId);
  97. return context.emitter().Build(loc_id, IncompleteTypeInFunctionReturnType,
  98. return_info.type_id);
  99. };
  100. auto diagnose_abstract_return_type = [&] {
  101. CARBON_DIAGNOSTIC(AbstractTypeInFunctionReturnType, Error,
  102. "function returns abstract type {0}", SemIR::TypeId);
  103. return context.emitter().Build(loc_id, AbstractTypeInFunctionReturnType,
  104. return_info.type_id);
  105. };
  106. // TODO: Consider suppressing the diagnostic if we've already diagnosed a
  107. // definition or call to this function.
  108. if (RequireConcreteType(context, return_info.type_id, loc_id,
  109. diagnose_incomplete_return_type,
  110. diagnose_abstract_return_type)) {
  111. return_info = SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(),
  112. function, specific_id);
  113. }
  114. }
  115. return return_info;
  116. }
  117. auto CheckFunctionDefinitionSignature(Context& context,
  118. SemIR::FunctionId function_id) -> void {
  119. auto& function = context.functions().Get(function_id);
  120. auto params_to_complete =
  121. context.inst_blocks().GetOrEmpty(function.call_params_id);
  122. // Check the return type is complete.
  123. if (function.return_type_inst_id.has_value()) {
  124. CheckFunctionReturnType(context, SemIR::LocId(function.return_type_inst_id),
  125. function, SemIR::SpecificId::None);
  126. // Don't re-check the return type below.
  127. params_to_complete.consume_back();
  128. }
  129. // Check the parameter types are complete.
  130. for (auto param_ref_id : params_to_complete) {
  131. if (param_ref_id == SemIR::ErrorInst::InstId) {
  132. continue;
  133. }
  134. // The parameter types need to be complete.
  135. RequireCompleteType(
  136. context, context.insts().GetAs<SemIR::AnyParam>(param_ref_id).type_id,
  137. SemIR::LocId(param_ref_id), [&] {
  138. CARBON_DIAGNOSTIC(
  139. IncompleteTypeInFunctionParam, Error,
  140. "parameter has incomplete type {0} in function definition",
  141. TypeOfInstId);
  142. return context.emitter().Build(
  143. param_ref_id, IncompleteTypeInFunctionParam, param_ref_id);
  144. });
  145. }
  146. }
  147. } // namespace Carbon::Check