return.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/check/context.h"
  6. #include "toolchain/check/convert.h"
  7. namespace Carbon::Check {
  8. // Gets the function that lexically encloses the current location.
  9. auto GetCurrentFunctionForReturn(Context& context) -> SemIR::Function& {
  10. CARBON_CHECK(!context.return_scope_stack().empty(),
  11. "Handling return but not in a function");
  12. auto function_id = context.insts()
  13. .GetAs<SemIR::FunctionDecl>(
  14. context.return_scope_stack().back().decl_id)
  15. .function_id;
  16. return context.functions().Get(function_id);
  17. }
  18. auto GetCurrentReturnSlot(Context& context) -> SemIR::InstId {
  19. // TODO: this does some unnecessary work to compute non-lexical scopes,
  20. // so a separate API on ScopeStack could be more efficient.
  21. auto return_slot_id = context.scope_stack()
  22. .LookupInLexicalScopes(SemIR::NameId::ReturnSlot)
  23. .first;
  24. return return_slot_id;
  25. }
  26. // Gets the currently in scope `returned var`, if any, that would be returned
  27. // by a `return var;`.
  28. static auto GetCurrentReturnedVar(Context& context) -> SemIR::InstId {
  29. CARBON_CHECK(!context.return_scope_stack().empty(),
  30. "Handling return but not in a function");
  31. return context.return_scope_stack().back().returned_var;
  32. }
  33. // Produces a note that the given function has no explicit return type.
  34. static auto NoteNoReturnTypeProvided(Context::DiagnosticBuilder& diag,
  35. const SemIR::Function& function) {
  36. CARBON_DIAGNOSTIC(ReturnTypeOmittedNote, Note,
  37. "there was no return type provided");
  38. diag.Note(function.latest_decl_id(), ReturnTypeOmittedNote);
  39. }
  40. // Produces a note describing the return type of the given function, which
  41. // must be a function whose definition is currently being checked.
  42. static auto NoteReturnType(Context& context, Context::DiagnosticBuilder& diag,
  43. const SemIR::Function& function) {
  44. auto out_param_pattern = context.insts().GetAs<SemIR::OutParamPattern>(
  45. function.return_slot_pattern_id);
  46. auto return_type_inst_id =
  47. context.insts()
  48. .GetAs<SemIR::ReturnSlotPattern>(out_param_pattern.subpattern_id)
  49. .type_inst_id;
  50. CARBON_DIAGNOSTIC(ReturnTypeHereNote, Note, "return type of function is {0}",
  51. InstIdAsType);
  52. diag.Note(function.return_slot_pattern_id, ReturnTypeHereNote,
  53. return_type_inst_id);
  54. }
  55. // Produces a note pointing at the currently in scope `returned var`.
  56. static auto NoteReturnedVar(Context::DiagnosticBuilder& diag,
  57. SemIR::InstId returned_var_id) {
  58. CARBON_DIAGNOSTIC(ReturnedVarHere, Note, "`returned var` was declared here");
  59. diag.Note(returned_var_id, ReturnedVarHere);
  60. }
  61. auto RegisterReturnedVar(Context& context, Parse::NodeId returned_node,
  62. Parse::NodeId type_node, SemIR::TypeId type_id,
  63. SemIR::InstId bind_id) -> void {
  64. auto& function = GetCurrentFunctionForReturn(context);
  65. auto return_info =
  66. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  67. if (!return_info.is_valid()) {
  68. // We already diagnosed this when we started defining the function.
  69. return;
  70. }
  71. // A `returned var` requires an explicit return type.
  72. if (!return_info.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_info.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(context, diag, function);
  90. diag.Emit();
  91. }
  92. auto existing_id = context.scope_stack().SetReturnedVarOrGetExisting(bind_id);
  93. if (existing_id.has_value()) {
  94. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  95. "cannot declare a `returned var` in the scope of "
  96. "another `returned var`");
  97. auto diag = context.emitter().Build(bind_id, ReturnedVarShadowed);
  98. NoteReturnedVar(diag, existing_id);
  99. diag.Emit();
  100. }
  101. }
  102. auto BuildReturnWithNoExpr(Context& context, Parse::ReturnStatementId node_id)
  103. -> void {
  104. const auto& function = GetCurrentFunctionForReturn(context);
  105. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  106. if (return_type_id.has_value()) {
  107. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  108. "missing return value");
  109. auto diag = context.emitter().Build(node_id, ReturnStatementMissingExpr);
  110. NoteReturnType(context, diag, function);
  111. diag.Emit();
  112. }
  113. context.AddInst<SemIR::Return>(node_id, {});
  114. }
  115. auto BuildReturnWithExpr(Context& context, Parse::ReturnStatementId node_id,
  116. SemIR::InstId expr_id) -> void {
  117. const auto& function = GetCurrentFunctionForReturn(context);
  118. auto returned_var_id = GetCurrentReturnedVar(context);
  119. auto return_slot_id = SemIR::InstId::None;
  120. auto return_info =
  121. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  122. if (!return_info.type_id.has_value()) {
  123. CARBON_DIAGNOSTIC(
  124. ReturnStatementDisallowExpr, Error,
  125. "no return expression should be provided in this context");
  126. auto diag = context.emitter().Build(node_id, ReturnStatementDisallowExpr);
  127. NoteNoReturnTypeProvided(diag, function);
  128. diag.Emit();
  129. expr_id = SemIR::ErrorInst::SingletonInstId;
  130. } else if (returned_var_id.has_value()) {
  131. CARBON_DIAGNOSTIC(
  132. ReturnExprWithReturnedVar, Error,
  133. "can only `return var;` in the scope of a `returned var`");
  134. auto diag = context.emitter().Build(node_id, ReturnExprWithReturnedVar);
  135. NoteReturnedVar(diag, returned_var_id);
  136. diag.Emit();
  137. expr_id = SemIR::ErrorInst::SingletonInstId;
  138. } else if (!return_info.is_valid()) {
  139. // We already diagnosed that the return type is invalid. Don't try to
  140. // convert to it.
  141. expr_id = SemIR::ErrorInst::SingletonInstId;
  142. } else if (return_info.has_return_slot()) {
  143. return_slot_id = GetCurrentReturnSlot(context);
  144. CARBON_CHECK(return_slot_id.has_value());
  145. // Note that this can import a function and invalidate `function`.
  146. expr_id = Initialize(context, node_id, return_slot_id, expr_id);
  147. } else {
  148. expr_id =
  149. ConvertToValueOfType(context, node_id, expr_id, return_info.type_id);
  150. }
  151. context.AddInst<SemIR::ReturnExpr>(
  152. node_id, {.expr_id = expr_id, .dest_id = return_slot_id});
  153. }
  154. auto BuildReturnVar(Context& context, Parse::ReturnStatementId node_id)
  155. -> void {
  156. const auto& function = GetCurrentFunctionForReturn(context);
  157. auto returned_var_id = GetCurrentReturnedVar(context);
  158. if (!returned_var_id.has_value()) {
  159. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  160. "`return var;` with no `returned var` in scope");
  161. context.emitter().Emit(node_id, ReturnVarWithNoReturnedVar);
  162. returned_var_id = SemIR::ErrorInst::SingletonInstId;
  163. }
  164. auto return_slot_id = GetCurrentReturnSlot(context);
  165. if (!SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function)
  166. .has_return_slot()) {
  167. // If we don't have a return slot, we're returning by value. Convert to a
  168. // value expression.
  169. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  170. return_slot_id = SemIR::InstId::None;
  171. }
  172. context.AddInst<SemIR::ReturnExpr>(
  173. node_id, {.expr_id = returned_var_id, .dest_id = return_slot_id});
  174. }
  175. } // namespace Carbon::Check