return.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/context.h"
  5. #include "toolchain/check/convert.h"
  6. namespace Carbon::Check {
  7. // Gets the function that lexically encloses the current location.
  8. static auto GetCurrentFunction(Context& context) -> SemIR::Function& {
  9. CARBON_CHECK(!context.return_scope_stack().empty())
  10. << "Handling return but not in a function";
  11. auto function_id = context.insts()
  12. .GetAs<SemIR::FunctionDecl>(
  13. context.return_scope_stack().back().decl_id)
  14. .function_id;
  15. return context.functions().Get(function_id);
  16. }
  17. // Gets the currently in scope `returned var`, if any, that would be returned
  18. // by a `return var;`.
  19. static auto GetCurrentReturnedVar(Context& context) -> SemIR::InstId {
  20. CARBON_CHECK(!context.return_scope_stack().empty())
  21. << "Handling return but not in a function";
  22. return context.return_scope_stack().back().returned_var;
  23. }
  24. // Produces a note that the given function has no explicit return type.
  25. static auto NoteNoReturnTypeProvided(Context::DiagnosticBuilder& diag,
  26. const SemIR::Function& function) {
  27. CARBON_DIAGNOSTIC(ReturnTypeOmittedNote, Note,
  28. "There was no return type provided.");
  29. diag.Note(function.decl_id, ReturnTypeOmittedNote);
  30. }
  31. // Produces a note describing the return type of the given function.
  32. static auto NoteReturnType(Context::DiagnosticBuilder& diag,
  33. const SemIR::Function& function) {
  34. CARBON_DIAGNOSTIC(ReturnTypeHereNote, Note,
  35. "Return type of function is `{0}`.", SemIR::TypeId);
  36. diag.Note(function.return_storage_id, ReturnTypeHereNote,
  37. function.return_type_id);
  38. }
  39. // Produces a note pointing at the currently in scope `returned var`.
  40. static auto NoteReturnedVar(Context::DiagnosticBuilder& diag,
  41. SemIR::InstId returned_var_id) {
  42. CARBON_DIAGNOSTIC(ReturnedVarHere, Note, "`returned var` was declared here.");
  43. diag.Note(returned_var_id, ReturnedVarHere);
  44. }
  45. auto CheckReturnedVar(Context& context, Parse::NodeId returned_node,
  46. Parse::NodeId name_node, SemIR::NameId name_id,
  47. Parse::NodeId type_node, SemIR::TypeId type_id)
  48. -> SemIR::InstId {
  49. // A `returned var` requires an explicit return type.
  50. auto& function = GetCurrentFunction(context);
  51. if (!function.return_type_id.is_valid()) {
  52. CARBON_DIAGNOSTIC(ReturnedVarWithNoReturnType, Error,
  53. "Cannot declare a `returned var` in this function.");
  54. auto diag =
  55. context.emitter().Build(returned_node, ReturnedVarWithNoReturnType);
  56. NoteNoReturnTypeProvided(diag, function);
  57. diag.Emit();
  58. return SemIR::InstId::BuiltinError;
  59. }
  60. // The declared type of the var must match the return type of the function.
  61. if (function.return_type_id != type_id) {
  62. CARBON_DIAGNOSTIC(ReturnedVarWrongType, Error,
  63. "Type `{0}` of `returned var` does not match "
  64. "return type of enclosing function.",
  65. SemIR::TypeId);
  66. auto diag =
  67. context.emitter().Build(type_node, ReturnedVarWrongType, type_id);
  68. NoteReturnType(diag, function);
  69. diag.Emit();
  70. return SemIR::InstId::BuiltinError;
  71. }
  72. // The variable aliases the return slot if there is one. If not, it has its
  73. // own storage.
  74. if (function.has_return_slot()) {
  75. return function.return_storage_id;
  76. }
  77. return context.AddInst({name_node, SemIR::VarStorage{type_id, name_id}});
  78. }
  79. auto RegisterReturnedVar(Context& context, SemIR::InstId bind_id) -> void {
  80. auto existing_id = context.scope_stack().SetReturnedVarOrGetExisting(bind_id);
  81. if (existing_id.is_valid()) {
  82. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  83. "Cannot declare a `returned var` in the scope of "
  84. "another `returned var`.");
  85. auto diag = context.emitter().Build(bind_id, ReturnedVarShadowed);
  86. NoteReturnedVar(diag, existing_id);
  87. diag.Emit();
  88. }
  89. }
  90. auto BuildReturnWithNoExpr(Context& context, Parse::ReturnStatementId node_id)
  91. -> void {
  92. const auto& function = GetCurrentFunction(context);
  93. if (function.return_type_id.is_valid()) {
  94. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  95. "Missing return value.");
  96. auto diag = context.emitter().Build(node_id, ReturnStatementMissingExpr);
  97. NoteReturnType(diag, function);
  98. diag.Emit();
  99. }
  100. context.AddInst({node_id, SemIR::Return{}});
  101. }
  102. auto BuildReturnWithExpr(Context& context, Parse::ReturnStatementId node_id,
  103. SemIR::InstId expr_id) -> void {
  104. const auto& function = GetCurrentFunction(context);
  105. auto returned_var_id = GetCurrentReturnedVar(context);
  106. if (!function.return_type_id.is_valid()) {
  107. CARBON_DIAGNOSTIC(
  108. ReturnStatementDisallowExpr, Error,
  109. "No return expression should be provided in this context.");
  110. auto diag = context.emitter().Build(node_id, ReturnStatementDisallowExpr);
  111. NoteNoReturnTypeProvided(diag, function);
  112. diag.Emit();
  113. expr_id = SemIR::InstId::BuiltinError;
  114. } else if (returned_var_id.is_valid()) {
  115. CARBON_DIAGNOSTIC(
  116. ReturnExprWithReturnedVar, Error,
  117. "Can only `return var;` in the scope of a `returned var`.");
  118. auto diag = context.emitter().Build(node_id, ReturnExprWithReturnedVar);
  119. NoteReturnedVar(diag, returned_var_id);
  120. diag.Emit();
  121. expr_id = SemIR::InstId::BuiltinError;
  122. } else if (function.has_return_slot()) {
  123. expr_id = Initialize(context, node_id, function.return_storage_id, expr_id);
  124. } else if (function.return_slot == SemIR::Function::ReturnSlot::Error) {
  125. // Don't produce a second error complaining the return type is incomplete.
  126. expr_id = SemIR::InstId::BuiltinError;
  127. } else {
  128. expr_id = ConvertToValueOfType(context, node_id, expr_id,
  129. function.return_type_id);
  130. }
  131. context.AddInst({node_id, SemIR::ReturnExpr{expr_id}});
  132. }
  133. auto BuildReturnVar(Context& context, Parse::ReturnStatementId node_id)
  134. -> void {
  135. const auto& function = GetCurrentFunction(context);
  136. auto returned_var_id = GetCurrentReturnedVar(context);
  137. if (!returned_var_id.is_valid()) {
  138. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  139. "`return var;` with no `returned var` in scope.");
  140. context.emitter().Emit(node_id, ReturnVarWithNoReturnedVar);
  141. returned_var_id = SemIR::InstId::BuiltinError;
  142. }
  143. if (!function.has_return_slot()) {
  144. // If we don't have a return slot, we're returning by value. Convert to a
  145. // value expression.
  146. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  147. }
  148. context.AddInst({node_id, SemIR::ReturnExpr{returned_var_id}});
  149. }
  150. } // namespace Carbon::Check