function.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. return_info.init_repr.kind == SemIR::InitRepr::Abstract) {
  95. auto diagnose_incomplete_return_type = [&] {
  96. CARBON_DIAGNOSTIC(IncompleteTypeInFunctionReturnType, Error,
  97. "function returns incomplete type {0}", SemIR::TypeId);
  98. return context.emitter().Build(loc_id, IncompleteTypeInFunctionReturnType,
  99. return_info.type_id);
  100. };
  101. auto diagnose_abstract_return_type = [&] {
  102. CARBON_DIAGNOSTIC(AbstractTypeInFunctionReturnType, Error,
  103. "function returns abstract type {0}", SemIR::TypeId);
  104. return context.emitter().Build(loc_id, AbstractTypeInFunctionReturnType,
  105. return_info.type_id);
  106. };
  107. // TODO: Consider suppressing the diagnostic if we've already diagnosed a
  108. // definition or call to this function.
  109. if (RequireConcreteType(context, return_info.type_id, loc_id,
  110. diagnose_incomplete_return_type,
  111. diagnose_abstract_return_type)) {
  112. return_info = SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(),
  113. function, specific_id);
  114. }
  115. }
  116. return return_info;
  117. }
  118. auto CheckFunctionDefinitionSignature(Context& context,
  119. SemIR::FunctionId function_id) -> void {
  120. auto& function = context.functions().Get(function_id);
  121. auto params_to_complete =
  122. context.inst_blocks().GetOrEmpty(function.call_params_id);
  123. // Check the return type is complete.
  124. if (function.return_type_inst_id.has_value()) {
  125. CheckFunctionReturnType(context, SemIR::LocId(function.return_type_inst_id),
  126. function, SemIR::SpecificId::None);
  127. }
  128. // Check the parameter types are complete.
  129. for (auto param_ref_id : params_to_complete) {
  130. if (param_ref_id == SemIR::ErrorInst::InstId) {
  131. continue;
  132. }
  133. // The parameter types need to be complete.
  134. RequireCompleteType(
  135. context, context.insts().GetAs<SemIR::AnyParam>(param_ref_id).type_id,
  136. SemIR::LocId(param_ref_id), [&] {
  137. CARBON_DIAGNOSTIC(
  138. IncompleteTypeInFunctionParam, Error,
  139. "parameter has incomplete type {0} in function definition",
  140. TypeOfInstId);
  141. return context.emitter().Build(
  142. param_ref_id, IncompleteTypeInFunctionParam, param_ref_id);
  143. });
  144. }
  145. }
  146. } // namespace Carbon::Check