return.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/convert.h"
  7. #include "toolchain/check/inst.h"
  8. namespace Carbon::Check {
  9. // Gets the function that lexically encloses the current location.
  10. auto GetCurrentFunctionForReturn(Context& context) -> SemIR::Function& {
  11. CARBON_CHECK(!context.return_scope_stack().empty(),
  12. "Handling return but not in a function");
  13. auto function_id = context.insts()
  14. .GetAs<SemIR::FunctionDecl>(
  15. context.return_scope_stack().back().decl_id)
  16. .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.return_scope_stack().empty(),
  31. "Handling return but not in a function");
  32. return context.return_scope_stack().back().returned_var;
  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, Parse::ReturnStatementId node_id)
  104. -> void {
  105. const auto& function = GetCurrentFunctionForReturn(context);
  106. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  107. if (return_type_id.has_value()) {
  108. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  109. "missing return value");
  110. auto diag = context.emitter().Build(node_id, ReturnStatementMissingExpr);
  111. NoteReturnType(context, diag, function);
  112. diag.Emit();
  113. }
  114. AddInst<SemIR::Return>(context, node_id, {});
  115. }
  116. auto BuildReturnWithExpr(Context& context, Parse::ReturnStatementId node_id,
  117. SemIR::InstId expr_id) -> void {
  118. const auto& function = GetCurrentFunctionForReturn(context);
  119. auto returned_var_id = GetCurrentReturnedVar(context);
  120. auto return_slot_id = SemIR::InstId::None;
  121. auto return_info =
  122. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  123. if (!return_info.type_id.has_value()) {
  124. CARBON_DIAGNOSTIC(
  125. ReturnStatementDisallowExpr, Error,
  126. "no return expression should be provided in this context");
  127. auto diag = context.emitter().Build(node_id, ReturnStatementDisallowExpr);
  128. NoteNoReturnTypeProvided(diag, function);
  129. diag.Emit();
  130. expr_id = SemIR::ErrorInst::InstId;
  131. } else if (returned_var_id.has_value()) {
  132. CARBON_DIAGNOSTIC(
  133. ReturnExprWithReturnedVar, Error,
  134. "can only `return var;` in the scope of a `returned var`");
  135. auto diag = context.emitter().Build(node_id, ReturnExprWithReturnedVar);
  136. NoteReturnedVar(diag, returned_var_id);
  137. diag.Emit();
  138. expr_id = SemIR::ErrorInst::InstId;
  139. } else if (!return_info.is_valid()) {
  140. // We already diagnosed that the return type is invalid. Don't try to
  141. // convert to it.
  142. expr_id = SemIR::ErrorInst::InstId;
  143. } else if (return_info.has_return_slot()) {
  144. return_slot_id = GetCurrentReturnSlot(context);
  145. CARBON_CHECK(return_slot_id.has_value());
  146. // Note that this can import a function and invalidate `function`.
  147. expr_id = Initialize(context, node_id, return_slot_id, expr_id);
  148. } else {
  149. expr_id =
  150. ConvertToValueOfType(context, node_id, expr_id, return_info.type_id);
  151. }
  152. AddInst<SemIR::ReturnExpr>(context, node_id,
  153. {.expr_id = expr_id, .dest_id = return_slot_id});
  154. }
  155. auto BuildReturnVar(Context& context, Parse::ReturnStatementId node_id)
  156. -> void {
  157. const auto& function = GetCurrentFunctionForReturn(context);
  158. auto returned_var_id = GetCurrentReturnedVar(context);
  159. if (!returned_var_id.has_value()) {
  160. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  161. "`return var;` with no `returned var` in scope");
  162. context.emitter().Emit(node_id, ReturnVarWithNoReturnedVar);
  163. returned_var_id = SemIR::ErrorInst::InstId;
  164. }
  165. auto return_slot_id = GetCurrentReturnSlot(context);
  166. if (!SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function)
  167. .has_return_slot()) {
  168. // If we don't have a return slot, we're returning by value. Convert to a
  169. // value expression.
  170. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  171. return_slot_id = SemIR::InstId::None;
  172. }
  173. AddInst<SemIR::ReturnExpr>(
  174. context, node_id,
  175. {.expr_id = returned_var_id, .dest_id = return_slot_id});
  176. }
  177. } // namespace Carbon::Check