return.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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(Context& context, DiagnosticBuilder& diag,
  44. const SemIR::Function& function) {
  45. auto out_param_pattern = context.insts().GetAs<SemIR::OutParamPattern>(
  46. function.return_slot_pattern_id);
  47. auto return_type_inst_id =
  48. context.insts()
  49. .GetAs<SemIR::ReturnSlotPattern>(out_param_pattern.subpattern_id)
  50. .type_inst_id;
  51. CARBON_DIAGNOSTIC(ReturnTypeHereNote, Note, "return type of function is {0}",
  52. InstIdAsType);
  53. diag.Note(function.return_slot_pattern_id, ReturnTypeHereNote,
  54. return_type_inst_id);
  55. }
  56. // Produces a note pointing at the currently in scope `returned var`.
  57. static auto NoteReturnedVar(DiagnosticBuilder& diag,
  58. SemIR::InstId returned_var_id) {
  59. CARBON_DIAGNOSTIC(ReturnedVarHere, Note, "`returned var` was declared here");
  60. diag.Note(returned_var_id, ReturnedVarHere);
  61. }
  62. auto RegisterReturnedVar(Context& context, Parse::NodeId returned_node,
  63. Parse::NodeId type_node, SemIR::TypeId type_id,
  64. SemIR::InstId bind_id) -> void {
  65. auto& function = GetCurrentFunctionForReturn(context);
  66. auto return_info =
  67. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  68. if (!return_info.is_valid()) {
  69. // We already diagnosed this when we started defining the function.
  70. return;
  71. }
  72. // A `returned var` requires an explicit return type.
  73. if (!return_info.type_id.has_value()) {
  74. CARBON_DIAGNOSTIC(ReturnedVarWithNoReturnType, Error,
  75. "cannot declare a `returned var` in this function");
  76. auto diag =
  77. context.emitter().Build(returned_node, ReturnedVarWithNoReturnType);
  78. NoteNoReturnTypeProvided(diag, function);
  79. diag.Emit();
  80. return;
  81. }
  82. // The declared type of the var must match the return type of the function.
  83. if (return_info.type_id != type_id) {
  84. CARBON_DIAGNOSTIC(ReturnedVarWrongType, Error,
  85. "type {0} of `returned var` does not match "
  86. "return type of enclosing function",
  87. SemIR::TypeId);
  88. auto diag =
  89. context.emitter().Build(type_node, ReturnedVarWrongType, type_id);
  90. NoteReturnType(context, diag, function);
  91. diag.Emit();
  92. }
  93. auto existing_id = context.scope_stack().SetReturnedVarOrGetExisting(bind_id);
  94. if (existing_id.has_value()) {
  95. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  96. "cannot declare a `returned var` in the scope of "
  97. "another `returned var`");
  98. auto diag = context.emitter().Build(bind_id, ReturnedVarShadowed);
  99. NoteReturnedVar(diag, existing_id);
  100. diag.Emit();
  101. }
  102. }
  103. auto BuildReturnWithNoExpr(Context& context, SemIR::LocId loc_id) -> void {
  104. const auto& function = GetCurrentFunctionForReturn(context);
  105. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  106. if (return_type_id.has_value()) {
  107. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  108. "missing return value");
  109. auto diag = context.emitter().Build(loc_id, ReturnStatementMissingExpr);
  110. NoteReturnType(context, diag, function);
  111. diag.Emit();
  112. }
  113. AddReturnCleanupBlock(context, loc_id);
  114. }
  115. auto BuildReturnWithExpr(Context& context, SemIR::LocId loc_id,
  116. SemIR::InstId expr_id) -> void {
  117. const auto& function = GetCurrentFunctionForReturn(context);
  118. auto returned_var_id = GetCurrentReturnedVar(context);
  119. auto return_slot_id = SemIR::InstId::None;
  120. auto return_info =
  121. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  122. if (!return_info.type_id.has_value()) {
  123. CARBON_DIAGNOSTIC(
  124. ReturnStatementDisallowExpr, Error,
  125. "no return expression should be provided in this context");
  126. auto diag = context.emitter().Build(loc_id, ReturnStatementDisallowExpr);
  127. NoteNoReturnTypeProvided(diag, function);
  128. diag.Emit();
  129. expr_id = SemIR::ErrorInst::InstId;
  130. } else if (returned_var_id.has_value()) {
  131. CARBON_DIAGNOSTIC(
  132. ReturnExprWithReturnedVar, Error,
  133. "can only `return var;` in the scope of a `returned var`");
  134. auto diag = context.emitter().Build(loc_id, ReturnExprWithReturnedVar);
  135. NoteReturnedVar(diag, returned_var_id);
  136. diag.Emit();
  137. expr_id = SemIR::ErrorInst::InstId;
  138. } else if (!return_info.is_valid()) {
  139. // We already diagnosed that the return type is invalid. Don't try to
  140. // convert to it.
  141. expr_id = SemIR::ErrorInst::InstId;
  142. } else {
  143. return_slot_id = GetCurrentReturnSlot(context);
  144. CARBON_CHECK(return_slot_id.has_value());
  145. expr_id = Initialize(context, loc_id, return_slot_id, expr_id);
  146. }
  147. AddReturnCleanupBlockWithExpr(
  148. context, loc_id, {.expr_id = expr_id, .dest_id = return_slot_id});
  149. }
  150. auto BuildReturnVar(Context& context, Parse::ReturnStatementId node_id)
  151. -> void {
  152. const auto& function = GetCurrentFunctionForReturn(context);
  153. auto returned_var_id = GetCurrentReturnedVar(context);
  154. if (!returned_var_id.has_value()) {
  155. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  156. "`return var;` with no `returned var` in scope");
  157. context.emitter().Emit(node_id, ReturnVarWithNoReturnedVar);
  158. returned_var_id = SemIR::ErrorInst::InstId;
  159. }
  160. auto return_slot_id = GetCurrentReturnSlot(context);
  161. if (!SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function)
  162. .has_return_slot()) {
  163. // If we don't have a return slot, we're returning by value. Convert to a
  164. // value expression.
  165. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  166. return_slot_id = SemIR::InstId::None;
  167. }
  168. AddReturnCleanupBlockWithExpr(
  169. context, node_id,
  170. {.expr_id = returned_var_id, .dest_id = return_slot_id});
  171. }
  172. } // namespace Carbon::Check