return.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. static auto GetCurrentFunction(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. // Gets the return slot of the function that lexically encloses the current
  19. // location.
  20. static auto GetCurrentReturnSlot(Context& context) -> SemIR::InstId {
  21. // TODO: this does some unnecessary work to compute non-lexical scopes,
  22. // so a separate API on ScopeStack could be more efficient.
  23. auto return_slot_id = context.scope_stack()
  24. .LookupInLexicalScopes(SemIR::NameId::ReturnSlot)
  25. .first;
  26. CARBON_CHECK(return_slot_id.is_valid());
  27. return return_slot_id;
  28. }
  29. // Gets the currently in scope `returned var`, if any, that would be returned
  30. // by a `return var;`.
  31. static auto GetCurrentReturnedVar(Context& context) -> SemIR::InstId {
  32. CARBON_CHECK(!context.return_scope_stack().empty(),
  33. "Handling return but not in a function");
  34. return context.return_scope_stack().back().returned_var;
  35. }
  36. // Produces a note that the given function has no explicit return type.
  37. static auto NoteNoReturnTypeProvided(Context::DiagnosticBuilder& diag,
  38. const SemIR::Function& function) {
  39. CARBON_DIAGNOSTIC(ReturnTypeOmittedNote, Note,
  40. "there was no return type provided");
  41. diag.Note(function.latest_decl_id(), ReturnTypeOmittedNote);
  42. }
  43. // Produces a note describing the return type of the given function, which
  44. // must be a function whose definition is currently being checked.
  45. static auto NoteReturnType(Context& context, Context::DiagnosticBuilder& diag,
  46. const SemIR::Function& function) {
  47. auto out_param_pattern = context.insts().GetAs<SemIR::OutParamPattern>(
  48. function.return_slot_pattern_id);
  49. auto return_type_inst_id =
  50. context.insts()
  51. .GetAs<SemIR::ReturnSlotPattern>(out_param_pattern.subpattern_id)
  52. .type_inst_id;
  53. CARBON_DIAGNOSTIC(ReturnTypeHereNote, Note, "return type of function is {0}",
  54. InstIdAsType);
  55. diag.Note(function.return_slot_pattern_id, ReturnTypeHereNote,
  56. return_type_inst_id);
  57. }
  58. // Produces a note pointing at the currently in scope `returned var`.
  59. static auto NoteReturnedVar(Context::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 CheckReturnedVar(Context& context, Parse::NodeId returned_node,
  65. Parse::NodeId name_node, SemIR::NameId name_id,
  66. Parse::NodeId type_node, SemIR::TypeId type_id)
  67. -> SemIR::InstId {
  68. auto& function = GetCurrentFunction(context);
  69. auto return_info =
  70. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  71. if (!return_info.is_valid()) {
  72. // We already diagnosed this when we started defining the function. Create a
  73. // placeholder for error recovery.
  74. return context.AddInst<SemIR::VarStorage>(
  75. name_node, {.type_id = type_id, .name_id = name_id});
  76. }
  77. // A `returned var` requires an explicit return type.
  78. if (!return_info.type_id.is_valid()) {
  79. CARBON_DIAGNOSTIC(ReturnedVarWithNoReturnType, Error,
  80. "cannot declare a `returned var` in this function");
  81. auto diag =
  82. context.emitter().Build(returned_node, ReturnedVarWithNoReturnType);
  83. NoteNoReturnTypeProvided(diag, function);
  84. diag.Emit();
  85. return SemIR::ErrorInst::SingletonInstId;
  86. }
  87. // The declared type of the var must match the return type of the function.
  88. if (return_info.type_id != type_id) {
  89. CARBON_DIAGNOSTIC(ReturnedVarWrongType, Error,
  90. "type {0} of `returned var` does not match "
  91. "return type of enclosing function",
  92. SemIR::TypeId);
  93. auto diag =
  94. context.emitter().Build(type_node, ReturnedVarWrongType, type_id);
  95. NoteReturnType(context, diag, function);
  96. diag.Emit();
  97. return SemIR::ErrorInst::SingletonInstId;
  98. }
  99. // The variable aliases the return slot if there is one. If not, it has its
  100. // own storage.
  101. if (return_info.has_return_slot()) {
  102. return GetCurrentReturnSlot(context);
  103. }
  104. return context.AddInst<SemIR::VarStorage>(
  105. name_node, {.type_id = type_id, .name_id = name_id});
  106. }
  107. auto RegisterReturnedVar(Context& context, SemIR::InstId bind_id) -> void {
  108. auto existing_id = context.scope_stack().SetReturnedVarOrGetExisting(bind_id);
  109. if (existing_id.is_valid()) {
  110. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  111. "cannot declare a `returned var` in the scope of "
  112. "another `returned var`");
  113. auto diag = context.emitter().Build(bind_id, ReturnedVarShadowed);
  114. NoteReturnedVar(diag, existing_id);
  115. diag.Emit();
  116. }
  117. }
  118. auto BuildReturnWithNoExpr(Context& context, Parse::ReturnStatementId node_id)
  119. -> void {
  120. const auto& function = GetCurrentFunction(context);
  121. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  122. if (return_type_id.is_valid()) {
  123. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  124. "missing return value");
  125. auto diag = context.emitter().Build(node_id, ReturnStatementMissingExpr);
  126. NoteReturnType(context, diag, function);
  127. diag.Emit();
  128. }
  129. context.AddInst<SemIR::Return>(node_id, {});
  130. }
  131. auto BuildReturnWithExpr(Context& context, Parse::ReturnStatementId node_id,
  132. SemIR::InstId expr_id) -> void {
  133. const auto& function = GetCurrentFunction(context);
  134. auto returned_var_id = GetCurrentReturnedVar(context);
  135. auto return_slot_id = SemIR::InstId::Invalid;
  136. auto return_info =
  137. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  138. if (!return_info.type_id.is_valid()) {
  139. CARBON_DIAGNOSTIC(
  140. ReturnStatementDisallowExpr, Error,
  141. "no return expression should be provided in this context");
  142. auto diag = context.emitter().Build(node_id, ReturnStatementDisallowExpr);
  143. NoteNoReturnTypeProvided(diag, function);
  144. diag.Emit();
  145. expr_id = SemIR::ErrorInst::SingletonInstId;
  146. } else if (returned_var_id.is_valid()) {
  147. CARBON_DIAGNOSTIC(
  148. ReturnExprWithReturnedVar, Error,
  149. "can only `return var;` in the scope of a `returned var`");
  150. auto diag = context.emitter().Build(node_id, ReturnExprWithReturnedVar);
  151. NoteReturnedVar(diag, returned_var_id);
  152. diag.Emit();
  153. expr_id = SemIR::ErrorInst::SingletonInstId;
  154. } else if (!return_info.is_valid()) {
  155. // We already diagnosed that the return type is invalid. Don't try to
  156. // convert to it.
  157. expr_id = SemIR::ErrorInst::SingletonInstId;
  158. } else if (return_info.has_return_slot()) {
  159. return_slot_id = GetCurrentReturnSlot(context);
  160. // Note that this can import a function and invalidate `function`.
  161. expr_id = Initialize(context, node_id, return_slot_id, expr_id);
  162. } else {
  163. expr_id =
  164. ConvertToValueOfType(context, node_id, expr_id, return_info.type_id);
  165. }
  166. context.AddInst<SemIR::ReturnExpr>(
  167. node_id, {.expr_id = expr_id, .dest_id = return_slot_id});
  168. }
  169. auto BuildReturnVar(Context& context, Parse::ReturnStatementId node_id)
  170. -> void {
  171. const auto& function = GetCurrentFunction(context);
  172. auto returned_var_id = GetCurrentReturnedVar(context);
  173. if (!returned_var_id.is_valid()) {
  174. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  175. "`return var;` with no `returned var` in scope");
  176. context.emitter().Emit(node_id, ReturnVarWithNoReturnedVar);
  177. returned_var_id = SemIR::ErrorInst::SingletonInstId;
  178. }
  179. auto return_slot_id = GetCurrentReturnSlot(context);
  180. if (!SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function)
  181. .has_return_slot()) {
  182. // If we don't have a return slot, we're returning by value. Convert to a
  183. // value expression.
  184. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  185. return_slot_id = SemIR::InstId::Invalid;
  186. }
  187. context.AddInst<SemIR::ReturnExpr>(
  188. node_id, {.expr_id = returned_var_id, .dest_id = return_slot_id});
  189. }
  190. } // namespace Carbon::Check