return.cpp 8.4 KB

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