function.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. auto CheckFunctionTypeMatches(Context& context,
  10. 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 CheckFunctionReturnType(Context& context, SemIRLoc loc,
  58. SemIR::Function& function) -> void {
  59. // If we have already checked the return type, we have nothing to do.
  60. if (function.return_slot != SemIR::Function::ReturnSlot::NotComputed) {
  61. return;
  62. }
  63. if (!function.return_type_id.is_valid()) {
  64. // Implicit `-> ()` has no return slot.
  65. function.return_slot = SemIR::Function::ReturnSlot::Absent;
  66. return;
  67. }
  68. // Check the return type is complete. Only diagnose incompleteness if we've
  69. // not already done so.
  70. auto diagnose_incomplete_return_type = [&] {
  71. CARBON_DIAGNOSTIC(IncompleteTypeInFunctionReturnType, Error,
  72. "Function returns incomplete type `{0}`.", SemIR::TypeId);
  73. return context.emitter().Build(loc, IncompleteTypeInFunctionReturnType,
  74. function.return_type_id);
  75. };
  76. if (!context.TryToCompleteType(
  77. function.return_type_id,
  78. function.return_slot == SemIR::Function::ReturnSlot::Error
  79. ? std::nullopt
  80. : std::optional(diagnose_incomplete_return_type))) {
  81. function.return_slot = SemIR::Function::ReturnSlot::Error;
  82. } else if (SemIR::GetInitRepr(context.sem_ir(), function.return_type_id)
  83. .has_return_slot()) {
  84. function.return_slot = SemIR::Function::ReturnSlot::Present;
  85. } else {
  86. function.return_slot = SemIR::Function::ReturnSlot::Absent;
  87. }
  88. }
  89. } // namespace Carbon::Check