function.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 CheckFunctionReturnType(Context& context, SemIR::LocId loc_id,
  87. const SemIR::Function& function,
  88. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  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. if (return_info.init_repr.kind == SemIR::InitRepr::Incomplete ||
  117. return_info.init_repr.kind == SemIR::InitRepr::Abstract) {
  118. return SemIR::ErrorInst::TypeId;
  119. }
  120. if (!return_info.type_id.has_value()) {
  121. return GetTupleType(context, {});
  122. }
  123. return return_info.type_id;
  124. }
  125. auto CheckFunctionDefinitionSignature(Context& context,
  126. SemIR::FunctionId function_id) -> void {
  127. auto& function = context.functions().Get(function_id);
  128. auto params_to_complete =
  129. context.inst_blocks().GetOrEmpty(function.call_params_id);
  130. // Check the return type is complete.
  131. if (function.return_type_inst_id.has_value()) {
  132. CheckFunctionReturnType(context, SemIR::LocId(function.return_type_inst_id),
  133. function, SemIR::SpecificId::None);
  134. }
  135. // Check the parameter types are complete.
  136. for (auto param_ref_id : params_to_complete) {
  137. if (param_ref_id == SemIR::ErrorInst::InstId) {
  138. continue;
  139. }
  140. // The parameter types need to be complete.
  141. RequireCompleteType(
  142. context, context.insts().GetAs<SemIR::AnyParam>(param_ref_id).type_id,
  143. SemIR::LocId(param_ref_id), [&] {
  144. CARBON_DIAGNOSTIC(
  145. IncompleteTypeInFunctionParam, Error,
  146. "parameter has incomplete type {0} in function definition",
  147. TypeOfInstId);
  148. return context.emitter().Build(
  149. param_ref_id, IncompleteTypeInFunctionParam, param_ref_id);
  150. });
  151. }
  152. }
  153. } // namespace Carbon::Check