return.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/return.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/control_flow.h"
  7. #include "toolchain/check/convert.h"
  8. #include "toolchain/check/inst.h"
  9. namespace Carbon::Check {
  10. // Gets the function that lexically encloses the current location.
  11. auto GetCurrentFunctionForReturn(Context& context) -> SemIR::Function& {
  12. CARBON_CHECK(context.scope_stack().IsInFunctionScope(),
  13. "Handling return but not in a function");
  14. auto decl_id = context.scope_stack().GetReturnScopeDeclId();
  15. auto function_id =
  16. context.insts().GetAs<SemIR::FunctionDecl>(decl_id).function_id;
  17. return context.functions().Get(function_id);
  18. }
  19. auto GetCurrentReturnSlot(Context& context) -> SemIR::InstId {
  20. // TODO: this does some unnecessary work to compute non-lexical scopes,
  21. // so a separate API on ScopeStack could be more efficient.
  22. auto return_slot_id = context.scope_stack()
  23. .LookupInLexicalScopes(SemIR::NameId::ReturnSlot)
  24. .first;
  25. return return_slot_id;
  26. }
  27. // Gets the currently in scope `returned var`, if any, that would be returned
  28. // by a `return var;`.
  29. static auto GetCurrentReturnedVar(Context& context) -> SemIR::InstId {
  30. CARBON_CHECK(context.scope_stack().IsInFunctionScope(),
  31. "Handling return but not in a function");
  32. return context.scope_stack().GetReturnedVar();
  33. }
  34. // Produces a note that the given function has no explicit return type.
  35. static auto NoteNoReturnTypeProvided(DiagnosticBuilder& diag,
  36. const SemIR::Function& function) {
  37. CARBON_DIAGNOSTIC(ReturnTypeOmittedNote, Note,
  38. "there was no return type provided");
  39. diag.Note(function.latest_decl_id(), ReturnTypeOmittedNote);
  40. }
  41. // Produces a note describing the return type of the given function, which
  42. // must be a function whose definition is currently being checked.
  43. static auto NoteReturnType(DiagnosticBuilder& diag,
  44. const SemIR::Function& function) {
  45. CARBON_DIAGNOSTIC(ReturnTypeHereNote, Note, "return type of function is {0}",
  46. InstIdAsType);
  47. diag.Note(function.return_type_inst_id, ReturnTypeHereNote,
  48. function.return_type_inst_id);
  49. }
  50. // Produces a note pointing at the currently in scope `returned var`.
  51. static auto NoteReturnedVar(DiagnosticBuilder& diag,
  52. SemIR::InstId returned_var_id) {
  53. CARBON_DIAGNOSTIC(ReturnedVarHere, Note, "`returned var` was declared here");
  54. diag.Note(returned_var_id, ReturnedVarHere);
  55. }
  56. auto RegisterReturnedVar(Context& context, Parse::NodeId returned_node,
  57. Parse::NodeId type_node, SemIR::TypeId type_id,
  58. SemIR::InstId bind_id) -> void {
  59. auto& function = GetCurrentFunctionForReturn(context);
  60. auto return_info =
  61. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  62. if (!return_info.is_valid()) {
  63. // We already diagnosed this when we started defining the function.
  64. return;
  65. }
  66. // A `returned var` requires an explicit return type.
  67. if (!return_info.type_id.has_value()) {
  68. CARBON_DIAGNOSTIC(ReturnedVarWithNoReturnType, Error,
  69. "cannot declare a `returned var` in this function");
  70. auto diag =
  71. context.emitter().Build(returned_node, ReturnedVarWithNoReturnType);
  72. NoteNoReturnTypeProvided(diag, function);
  73. diag.Emit();
  74. return;
  75. }
  76. // The declared type of the var must match the return type of the function.
  77. if (return_info.type_id != type_id) {
  78. CARBON_DIAGNOSTIC(ReturnedVarWrongType, Error,
  79. "type {0} of `returned var` does not match "
  80. "return type of enclosing function",
  81. SemIR::TypeId);
  82. auto diag =
  83. context.emitter().Build(type_node, ReturnedVarWrongType, type_id);
  84. NoteReturnType(diag, function);
  85. diag.Emit();
  86. }
  87. auto existing_id = context.scope_stack().SetReturnedVarOrGetExisting(bind_id);
  88. if (existing_id.has_value()) {
  89. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  90. "cannot declare a `returned var` in the scope of "
  91. "another `returned var`");
  92. auto diag = context.emitter().Build(bind_id, ReturnedVarShadowed);
  93. NoteReturnedVar(diag, existing_id);
  94. diag.Emit();
  95. }
  96. }
  97. auto BuildReturnWithNoExpr(Context& context, SemIR::LocId loc_id) -> void {
  98. const auto& function = GetCurrentFunctionForReturn(context);
  99. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  100. if (return_type_id.has_value()) {
  101. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  102. "missing return value");
  103. auto diag = context.emitter().Build(loc_id, ReturnStatementMissingExpr);
  104. NoteReturnType(diag, function);
  105. diag.Emit();
  106. }
  107. AddReturnCleanupBlock(context, loc_id);
  108. }
  109. auto BuildReturnWithExpr(Context& context, SemIR::LocId loc_id,
  110. SemIR::InstId expr_id) -> void {
  111. const auto& function = GetCurrentFunctionForReturn(context);
  112. auto returned_var_id = GetCurrentReturnedVar(context);
  113. auto return_slot_id = SemIR::InstId::None;
  114. auto return_info =
  115. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  116. if (!return_info.type_id.has_value()) {
  117. CARBON_DIAGNOSTIC(
  118. ReturnStatementDisallowExpr, Error,
  119. "no return expression should be provided in this context");
  120. auto diag = context.emitter().Build(loc_id, ReturnStatementDisallowExpr);
  121. NoteNoReturnTypeProvided(diag, function);
  122. diag.Emit();
  123. expr_id = SemIR::ErrorInst::InstId;
  124. } else if (returned_var_id.has_value()) {
  125. CARBON_DIAGNOSTIC(
  126. ReturnExprWithReturnedVar, Error,
  127. "can only `return var;` in the scope of a `returned var`");
  128. auto diag = context.emitter().Build(loc_id, ReturnExprWithReturnedVar);
  129. NoteReturnedVar(diag, returned_var_id);
  130. diag.Emit();
  131. expr_id = SemIR::ErrorInst::InstId;
  132. } else if (!return_info.is_valid()) {
  133. // We already diagnosed that the return type is invalid. Don't try to
  134. // convert to it.
  135. expr_id = SemIR::ErrorInst::InstId;
  136. } else {
  137. return_slot_id = GetCurrentReturnSlot(context);
  138. CARBON_CHECK(return_slot_id.has_value());
  139. expr_id = Initialize(context, loc_id, return_slot_id, expr_id);
  140. }
  141. AddReturnCleanupBlockWithExpr(
  142. context, loc_id, {.expr_id = expr_id, .dest_id = return_slot_id});
  143. }
  144. auto BuildReturnVar(Context& context, Parse::ReturnStatementId node_id)
  145. -> void {
  146. const auto& function = GetCurrentFunctionForReturn(context);
  147. auto returned_var_id = GetCurrentReturnedVar(context);
  148. if (!returned_var_id.has_value()) {
  149. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  150. "`return var;` with no `returned var` in scope");
  151. context.emitter().Emit(node_id, ReturnVarWithNoReturnedVar);
  152. returned_var_id = SemIR::ErrorInst::InstId;
  153. }
  154. auto return_slot_id = GetCurrentReturnSlot(context);
  155. if (!SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function)
  156. .has_return_slot()) {
  157. // If we don't have a return slot, we're returning by value. Convert to a
  158. // value expression.
  159. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  160. return_slot_id = SemIR::InstId::None;
  161. }
  162. AddReturnCleanupBlockWithExpr(
  163. context, node_id,
  164. {.expr_id = returned_var_id, .dest_id = return_slot_id});
  165. }
  166. } // namespace Carbon::Check