function.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.h"
  8. #include "toolchain/check/type_completion.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. #include "toolchain/sem_ir/pattern.h"
  11. namespace Carbon::Check {
  12. auto FindSelfPattern(Context& context,
  13. SemIR::InstBlockId implicit_param_patterns_id)
  14. -> SemIR::InstId {
  15. auto implicit_param_patterns =
  16. context.inst_blocks().GetOrEmpty(implicit_param_patterns_id);
  17. return FindIfOrNone(implicit_param_patterns, [&](auto implicit_param_id) {
  18. return SemIR::IsSelfPattern(context.sem_ir(), implicit_param_id);
  19. });
  20. }
  21. auto CheckFunctionReturnTypeMatches(Context& context,
  22. const SemIR::Function& new_function,
  23. const SemIR::Function& prev_function,
  24. SemIR::SpecificId prev_specific_id,
  25. bool diagnose) -> bool {
  26. // TODO: Pass a specific ID for `prev_function` instead of substitutions and
  27. // use it here.
  28. auto new_return_type_id =
  29. new_function.GetDeclaredReturnType(context.sem_ir());
  30. auto prev_return_type_id =
  31. prev_function.GetDeclaredReturnType(context.sem_ir(), prev_specific_id);
  32. if (new_return_type_id == SemIR::ErrorInst::TypeId ||
  33. prev_return_type_id == SemIR::ErrorInst::TypeId) {
  34. return false;
  35. }
  36. if (!context.types().AreEqualAcrossDeclarations(new_return_type_id,
  37. prev_return_type_id)) {
  38. if (!diagnose) {
  39. return false;
  40. }
  41. CARBON_DIAGNOSTIC(
  42. FunctionRedeclReturnTypeDiffers, Error,
  43. "function redeclaration differs because return type is {0}",
  44. SemIR::TypeId);
  45. CARBON_DIAGNOSTIC(
  46. FunctionRedeclReturnTypeDiffersNoReturn, Error,
  47. "function redeclaration differs because no return type is provided");
  48. auto diag =
  49. new_return_type_id.has_value()
  50. ? context.emitter().Build(new_function.latest_decl_id(),
  51. FunctionRedeclReturnTypeDiffers,
  52. new_return_type_id)
  53. : context.emitter().Build(new_function.latest_decl_id(),
  54. FunctionRedeclReturnTypeDiffersNoReturn);
  55. if (prev_return_type_id.has_value()) {
  56. CARBON_DIAGNOSTIC(FunctionRedeclReturnTypePrevious, Note,
  57. "previously declared with return type {0}",
  58. SemIR::TypeId);
  59. diag.Note(prev_function.latest_decl_id(),
  60. FunctionRedeclReturnTypePrevious, prev_return_type_id);
  61. } else {
  62. CARBON_DIAGNOSTIC(FunctionRedeclReturnTypePreviousNoReturn, Note,
  63. "previously declared with no return type");
  64. diag.Note(prev_function.latest_decl_id(),
  65. FunctionRedeclReturnTypePreviousNoReturn);
  66. }
  67. diag.Emit();
  68. return false;
  69. }
  70. return true;
  71. }
  72. auto CheckFunctionTypeMatches(Context& context,
  73. const SemIR::Function& new_function,
  74. const SemIR::Function& prev_function,
  75. SemIR::SpecificId prev_specific_id,
  76. bool check_syntax, bool check_self, bool diagnose)
  77. -> bool {
  78. if (!CheckRedeclParamsMatch(context, DeclParams(new_function),
  79. DeclParams(prev_function), prev_specific_id,
  80. diagnose, check_syntax, check_self)) {
  81. return false;
  82. }
  83. return CheckFunctionReturnTypeMatches(context, new_function, prev_function,
  84. prev_specific_id, diagnose);
  85. }
  86. auto CheckFunctionReturnPatternType(Context& context, SemIR::LocId loc_id,
  87. SemIR::InstId return_pattern_id,
  88. SemIR::SpecificId specific_id)
  89. -> SemIR::TypeId {
  90. auto arg_type_id = SemIR::ExtractScrutineeType(
  91. context.sem_ir(), SemIR::GetTypeOfInstInSpecific(
  92. context.sem_ir(), specific_id, return_pattern_id));
  93. auto init_repr = SemIR::InitRepr::ForType(context.sem_ir(), arg_type_id);
  94. if (!init_repr.is_valid()) {
  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. arg_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. arg_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(
  110. context, arg_type_id, SemIR::LocId(return_pattern_id),
  111. diagnose_incomplete_return_type, diagnose_abstract_return_type)) {
  112. return SemIR::ErrorInst::TypeId;
  113. }
  114. }
  115. return arg_type_id;
  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. for (auto return_pattern_id :
  124. context.inst_blocks().GetOrEmpty(function.return_patterns_id)) {
  125. CheckFunctionReturnPatternType(context, SemIR::LocId(return_pattern_id),
  126. return_pattern_id, 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