return.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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) -> 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 = context.scope_stack().SetReturnedVarOrGetExisting(bind_id);
  101. if (existing_id.has_value()) {
  102. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  103. "cannot declare a `returned var` in the scope of "
  104. "another `returned var`");
  105. auto diag = context.emitter().Build(bind_id, ReturnedVarShadowed);
  106. NoteReturnedVar(diag, existing_id);
  107. diag.Emit();
  108. }
  109. }
  110. auto BuildReturnWithNoExpr(Context& context, SemIR::LocId loc_id) -> void {
  111. const auto& function = GetCurrentFunctionForReturn(context);
  112. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  113. if (return_type_id.has_value()) {
  114. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  115. "missing return value");
  116. auto diag = context.emitter().Build(loc_id, ReturnStatementMissingExpr);
  117. NoteReturnType(diag, function);
  118. diag.Emit();
  119. }
  120. AddReturnCleanupBlock(context, loc_id);
  121. }
  122. auto BuildReturnWithExpr(Context& context, SemIR::LocId loc_id,
  123. SemIR::InstId expr_id) -> void {
  124. const auto& function = GetCurrentFunctionForReturn(context);
  125. auto returned_var_id = GetCurrentReturnedVar(context);
  126. auto out_param_id = SemIR::InstId::None;
  127. auto return_type_id = SemIR::TypeId::None;
  128. if (function.return_type_inst_id.has_value()) {
  129. return_type_id =
  130. context.types().GetTypeIdForTypeInstId(function.return_type_inst_id);
  131. }
  132. if (!return_type_id.has_value()) {
  133. CARBON_DIAGNOSTIC(
  134. ReturnStatementDisallowExpr, Error,
  135. "no return expression should be provided in this context");
  136. auto diag = context.emitter().Build(loc_id, ReturnStatementDisallowExpr);
  137. NoteNoReturnTypeProvided(diag, function);
  138. diag.Emit();
  139. expr_id = SemIR::ErrorInst::InstId;
  140. } else if (returned_var_id.has_value()) {
  141. CARBON_DIAGNOSTIC(
  142. ReturnExprWithReturnedVar, Error,
  143. "can only `return var;` in the scope of a `returned var`");
  144. auto diag = context.emitter().Build(loc_id, ReturnExprWithReturnedVar);
  145. NoteReturnedVar(diag, returned_var_id);
  146. diag.Emit();
  147. expr_id = SemIR::ErrorInst::InstId;
  148. } else if (!SemIR::InitRepr::ForType(context.sem_ir(), return_type_id)
  149. .is_valid() ||
  150. return_type_id == SemIR::ErrorInst::TypeId) {
  151. // We already diagnosed that the return type is invalid. Don't try to
  152. // convert to it.
  153. expr_id = SemIR::ErrorInst::InstId;
  154. } else {
  155. auto return_form =
  156. context.insts().Get(function.GetDeclaredReturnForm(context.sem_ir()));
  157. CARBON_KIND_SWITCH(return_form) {
  158. case CARBON_KIND(SemIR::InitForm init_form): {
  159. auto call_params = context.inst_blocks().Get(
  160. GetCurrentFunctionForReturn(context).call_params_id);
  161. out_param_id = call_params[init_form.index.index];
  162. CARBON_CHECK(out_param_id.has_value());
  163. expr_id = Initialize(context, loc_id, out_param_id, expr_id);
  164. if (!SemIR::InitRepr::ForType(context.sem_ir(), return_type_id)
  165. .MightBeInPlace()) {
  166. out_param_id = SemIR::InstId::None;
  167. }
  168. break;
  169. }
  170. case CARBON_KIND(SemIR::RefForm ref_form): {
  171. expr_id = Convert(
  172. context, loc_id, expr_id,
  173. ConversionTarget{.kind = ConversionTarget::DurableRef,
  174. .type_id = context.types().GetTypeIdForTypeInstId(
  175. ref_form.type_component_inst_id)});
  176. break;
  177. }
  178. default:
  179. CARBON_FATAL("Unexpected inst kind: {0}", return_form);
  180. }
  181. }
  182. AddReturnCleanupBlockWithExpr(context, loc_id,
  183. {.expr_id = expr_id, .dest_id = out_param_id});
  184. }
  185. auto BuildReturnVar(Context& context, Parse::ReturnStatementId node_id)
  186. -> void {
  187. const auto& function = GetCurrentFunctionForReturn(context);
  188. auto returned_var_id = GetCurrentReturnedVar(context);
  189. if (!returned_var_id.has_value()) {
  190. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  191. "`return var;` with no `returned var` in scope");
  192. context.emitter().Emit(node_id, ReturnVarWithNoReturnedVar);
  193. returned_var_id = SemIR::ErrorInst::InstId;
  194. }
  195. auto return_param_id = GetReturnedVarParam(context, function);
  196. if (!return_param_id.has_value()) {
  197. // If we don't have a return slot, we're returning by value. Convert to a
  198. // value expression.
  199. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  200. }
  201. AddReturnCleanupBlockWithExpr(
  202. context, node_id,
  203. {.expr_id = returned_var_id, .dest_id = return_param_id});
  204. }
  205. } // namespace Carbon::Check