return.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 GetReturnedVarParam(Context& context, const SemIR::Function& function)
  21. -> SemIR::InstId {
  22. auto return_form_id = function.GetDeclaredReturnForm(context.sem_ir());
  23. if (auto return_form =
  24. context.insts().TryGetAsIfValid<SemIR::InitForm>(return_form_id)) {
  25. auto call_params = context.inst_blocks().Get(function.call_params_id);
  26. auto return_param_id = call_params[return_form->index.index];
  27. auto return_type_id = context.insts().Get(return_param_id).type_id();
  28. if (SemIR::InitRepr::ForType(context.sem_ir(), return_type_id)
  29. .MightBeInPlace()) {
  30. return return_param_id;
  31. }
  32. }
  33. return SemIR::InstId::None;
  34. }
  35. // Gets the currently in scope `returned var` binding, if any, that would be
  36. // returned by a `return var;`.
  37. static auto GetCurrentReturnedVar(Context& context) -> SemIR::InstId {
  38. CARBON_CHECK(context.scope_stack().IsInFunctionScope(),
  39. "Handling return but not in a function");
  40. return context.scope_stack().GetReturnedVar();
  41. }
  42. // Produces a note that the given function has no explicit return type.
  43. static auto NoteNoReturnTypeProvided(DiagnosticBuilder& diag,
  44. const SemIR::Function& function) {
  45. CARBON_DIAGNOSTIC(ReturnTypeOmittedNote, Note,
  46. "there was no return type provided");
  47. diag.Note(function.latest_decl_id(), ReturnTypeOmittedNote);
  48. }
  49. // Produces a note describing the return type of the given function, which
  50. // must be a function whose definition is currently being checked.
  51. static auto NoteReturnType(DiagnosticBuilder& diag,
  52. const SemIR::Function& function) {
  53. CARBON_DIAGNOSTIC(ReturnTypeHereNote, Note, "return type of function is {0}",
  54. InstIdAsType);
  55. diag.Note(function.return_type_inst_id, ReturnTypeHereNote,
  56. function.return_type_inst_id);
  57. }
  58. // Produces a note pointing at the currently in scope `returned var`.
  59. static auto NoteReturnedVar(DiagnosticBuilder& diag,
  60. SemIR::InstId returned_var_id) {
  61. CARBON_DIAGNOSTIC(ReturnedVarHere, Note, "`returned var` was declared here");
  62. diag.Note(returned_var_id, ReturnedVarHere);
  63. }
  64. auto RegisterReturnedVar(Context& context, Parse::NodeId returned_node,
  65. Parse::NodeId type_node, SemIR::TypeId type_id,
  66. SemIR::InstId bind_id, SemIR::NameId name_id) -> void {
  67. auto& function = GetCurrentFunctionForReturn(context);
  68. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  69. // A `returned var` requires an explicit return type.
  70. if (!return_type_id.has_value()) {
  71. CARBON_DIAGNOSTIC(ReturnedVarWithNoReturnType, Error,
  72. "cannot declare a `returned var` in this function");
  73. auto diag =
  74. context.emitter().Build(returned_node, ReturnedVarWithNoReturnType);
  75. NoteNoReturnTypeProvided(diag, function);
  76. diag.Emit();
  77. return;
  78. }
  79. // The declared type of the var must match the return type of the function.
  80. if (return_type_id != type_id) {
  81. CARBON_DIAGNOSTIC(ReturnedVarWrongType, Error,
  82. "type {0} of `returned var` does not match "
  83. "return type of enclosing function",
  84. SemIR::TypeId);
  85. auto diag =
  86. context.emitter().Build(type_node, ReturnedVarWrongType, type_id);
  87. NoteReturnType(diag, function);
  88. diag.Emit();
  89. }
  90. auto form_inst_id = function.GetDeclaredReturnForm(context.sem_ir());
  91. if (!context.insts().Is<SemIR::InitForm>(form_inst_id)) {
  92. CARBON_DIAGNOSTIC(ReturnedVarNotInit, Error,
  93. "`returned var` declaration in function with "
  94. "non-initializing return form");
  95. auto diag = context.emitter().Build(returned_node, ReturnedVarNotInit);
  96. CARBON_DIAGNOSTIC(ReturnFormHereNote, Note, "return form declared here");
  97. diag.Note(function.return_form_inst_id, ReturnFormHereNote);
  98. diag.Emit();
  99. }
  100. auto existing_id =
  101. context.scope_stack().SetReturnedVarOrGetExisting(bind_id, name_id);
  102. if (existing_id.has_value()) {
  103. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  104. "cannot declare a `returned var` in the scope of "
  105. "another `returned var`");
  106. auto diag = context.emitter().Build(bind_id, ReturnedVarShadowed);
  107. NoteReturnedVar(diag, existing_id);
  108. diag.Emit();
  109. }
  110. }
  111. auto BuildReturnWithNoExpr(Context& context, SemIR::LocId loc_id) -> void {
  112. const auto& function = GetCurrentFunctionForReturn(context);
  113. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  114. if (return_type_id.has_value()) {
  115. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  116. "missing return value");
  117. auto diag = context.emitter().Build(loc_id, ReturnStatementMissingExpr);
  118. NoteReturnType(diag, function);
  119. diag.Emit();
  120. }
  121. AddReturnCleanupBlock(context, loc_id);
  122. }
  123. auto BuildReturnWithExpr(Context& context, SemIR::LocId loc_id,
  124. SemIR::InstId expr_id) -> void {
  125. const auto& function = GetCurrentFunctionForReturn(context);
  126. auto returned_var_id = GetCurrentReturnedVar(context);
  127. auto out_param_id = SemIR::InstId::None;
  128. auto return_type_id = SemIR::TypeId::None;
  129. if (function.return_type_inst_id.has_value()) {
  130. return_type_id =
  131. context.types().GetTypeIdForTypeInstId(function.return_type_inst_id);
  132. }
  133. if (!return_type_id.has_value()) {
  134. CARBON_DIAGNOSTIC(
  135. ReturnStatementDisallowExpr, Error,
  136. "no return expression should be provided in this context");
  137. auto diag = context.emitter().Build(loc_id, ReturnStatementDisallowExpr);
  138. NoteNoReturnTypeProvided(diag, function);
  139. diag.Emit();
  140. expr_id = SemIR::ErrorInst::InstId;
  141. } else if (returned_var_id.has_value()) {
  142. CARBON_DIAGNOSTIC(
  143. ReturnExprWithReturnedVar, Error,
  144. "can only `return var;` in the scope of a `returned var`");
  145. auto diag = context.emitter().Build(loc_id, ReturnExprWithReturnedVar);
  146. NoteReturnedVar(diag, returned_var_id);
  147. diag.Emit();
  148. expr_id = SemIR::ErrorInst::InstId;
  149. } else if (!SemIR::InitRepr::ForType(context.sem_ir(), return_type_id)
  150. .is_valid() ||
  151. return_type_id == SemIR::ErrorInst::TypeId) {
  152. // We already diagnosed that the return type is invalid. Don't try to
  153. // convert to it.
  154. expr_id = SemIR::ErrorInst::InstId;
  155. } else {
  156. auto return_form =
  157. context.insts().Get(function.GetDeclaredReturnForm(context.sem_ir()));
  158. CARBON_KIND_SWITCH(return_form) {
  159. case CARBON_KIND(SemIR::InitForm init_form): {
  160. auto call_params = context.inst_blocks().Get(
  161. GetCurrentFunctionForReturn(context).call_params_id);
  162. out_param_id = call_params[init_form.index.index];
  163. CARBON_CHECK(out_param_id.has_value());
  164. expr_id = Initialize(context, loc_id, out_param_id, expr_id,
  165. /*for_return=*/true);
  166. if (!SemIR::InitRepr::ForType(context.sem_ir(), return_type_id)
  167. .MightBeInPlace()) {
  168. out_param_id = SemIR::InstId::None;
  169. }
  170. break;
  171. }
  172. case CARBON_KIND(SemIR::RefForm ref_form): {
  173. expr_id = Convert(
  174. context, loc_id, expr_id,
  175. ConversionTarget{.kind = ConversionTarget::DurableRef,
  176. .type_id = context.types().GetTypeIdForTypeInstId(
  177. ref_form.type_component_inst_id)});
  178. break;
  179. }
  180. default:
  181. CARBON_FATAL("Unexpected inst kind: {0}", return_form);
  182. }
  183. }
  184. AddReturnCleanupBlockWithExpr(context, loc_id,
  185. {.expr_id = expr_id, .dest_id = out_param_id});
  186. }
  187. auto BuildReturnVar(Context& context, Parse::ReturnStatementId node_id)
  188. -> void {
  189. const auto& function = GetCurrentFunctionForReturn(context);
  190. auto returned_var_id = GetCurrentReturnedVar(context);
  191. if (!returned_var_id.has_value()) {
  192. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  193. "`return var;` with no `returned var` in scope");
  194. context.emitter().Emit(node_id, ReturnVarWithNoReturnedVar);
  195. returned_var_id = SemIR::ErrorInst::InstId;
  196. }
  197. auto return_param_id = GetReturnedVarParam(context, function);
  198. if (!return_param_id.has_value()) {
  199. // If we don't have a return slot, we're returning by value. Convert to a
  200. // value expression.
  201. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  202. }
  203. AddReturnCleanupBlockWithExpr(
  204. context, node_id,
  205. {.expr_id = returned_var_id, .dest_id = return_param_id});
  206. }
  207. } // namespace Carbon::Check