return.cpp 9.7 KB

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