return.cpp 9.8 KB

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