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. return return_slot_id;
  27. }
  28. // Gets the currently in scope `returned var`, if any, that would be returned
  29. // by a `return var;`.
  30. static auto GetCurrentReturnedVar(Context& context) -> SemIR::InstId {
  31. CARBON_CHECK(!context.return_scope_stack().empty(),
  32. "Handling return but not in a function");
  33. return context.return_scope_stack().back().returned_var;
  34. }
  35. // Produces a note that the given function has no explicit return type.
  36. static auto NoteNoReturnTypeProvided(Context::DiagnosticBuilder& diag,
  37. const SemIR::Function& function) {
  38. CARBON_DIAGNOSTIC(ReturnTypeOmittedNote, Note,
  39. "there was no return type provided");
  40. diag.Note(function.latest_decl_id(), ReturnTypeOmittedNote);
  41. }
  42. // Produces a note describing the return type of the given function, which
  43. // must be a function whose definition is currently being checked.
  44. static auto NoteReturnType(Context& context, Context::DiagnosticBuilder& diag,
  45. const SemIR::Function& function) {
  46. auto out_param_pattern = context.insts().GetAs<SemIR::OutParamPattern>(
  47. function.return_slot_pattern_id);
  48. auto return_type_inst_id =
  49. context.insts()
  50. .GetAs<SemIR::ReturnSlotPattern>(out_param_pattern.subpattern_id)
  51. .type_inst_id;
  52. CARBON_DIAGNOSTIC(ReturnTypeHereNote, Note, "return type of function is {0}",
  53. InstIdAsType);
  54. diag.Note(function.return_slot_pattern_id, ReturnTypeHereNote,
  55. return_type_inst_id);
  56. }
  57. // Produces a note pointing at the currently in scope `returned var`.
  58. static auto NoteReturnedVar(Context::DiagnosticBuilder& diag,
  59. SemIR::InstId returned_var_id) {
  60. CARBON_DIAGNOSTIC(ReturnedVarHere, Note, "`returned var` was declared here");
  61. diag.Note(returned_var_id, ReturnedVarHere);
  62. }
  63. auto CheckReturnedVar(Context& context, Parse::NodeId returned_node,
  64. Parse::NodeId name_node, SemIR::NameId name_id,
  65. Parse::NodeId type_node, SemIR::TypeId type_id)
  66. -> SemIR::InstId {
  67. auto& function = GetCurrentFunction(context);
  68. auto return_info =
  69. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  70. if (!return_info.is_valid()) {
  71. // We already diagnosed this when we started defining the function. Create a
  72. // placeholder for error recovery.
  73. return context.AddInst<SemIR::VarStorage>(
  74. name_node, {.type_id = type_id, .name_id = name_id});
  75. }
  76. // A `returned var` requires an explicit return type.
  77. if (!return_info.type_id.is_valid()) {
  78. CARBON_DIAGNOSTIC(ReturnedVarWithNoReturnType, Error,
  79. "cannot declare a `returned var` in this function");
  80. auto diag =
  81. context.emitter().Build(returned_node, ReturnedVarWithNoReturnType);
  82. NoteNoReturnTypeProvided(diag, function);
  83. diag.Emit();
  84. return SemIR::ErrorInst::SingletonInstId;
  85. }
  86. // The declared type of the var must match the return type of the function.
  87. if (return_info.type_id != type_id) {
  88. CARBON_DIAGNOSTIC(ReturnedVarWrongType, Error,
  89. "type {0} of `returned var` does not match "
  90. "return type of enclosing function",
  91. SemIR::TypeId);
  92. auto diag =
  93. context.emitter().Build(type_node, ReturnedVarWrongType, type_id);
  94. NoteReturnType(context, diag, function);
  95. diag.Emit();
  96. return SemIR::ErrorInst::SingletonInstId;
  97. }
  98. // The variable aliases the return slot if there is one. If not, it has its
  99. // own storage.
  100. if (return_info.has_return_slot()) {
  101. return GetCurrentReturnSlot(context);
  102. }
  103. return context.AddInst<SemIR::VarStorage>(
  104. name_node, {.type_id = type_id, .name_id = name_id});
  105. }
  106. auto RegisterReturnedVar(Context& context, SemIR::InstId bind_id) -> void {
  107. auto existing_id = context.scope_stack().SetReturnedVarOrGetExisting(bind_id);
  108. if (existing_id.is_valid()) {
  109. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  110. "cannot declare a `returned var` in the scope of "
  111. "another `returned var`");
  112. auto diag = context.emitter().Build(bind_id, ReturnedVarShadowed);
  113. NoteReturnedVar(diag, existing_id);
  114. diag.Emit();
  115. }
  116. }
  117. auto BuildReturnWithNoExpr(Context& context, Parse::ReturnStatementId node_id)
  118. -> void {
  119. const auto& function = GetCurrentFunction(context);
  120. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  121. if (return_type_id.is_valid()) {
  122. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  123. "missing return value");
  124. auto diag = context.emitter().Build(node_id, ReturnStatementMissingExpr);
  125. NoteReturnType(context, diag, function);
  126. diag.Emit();
  127. }
  128. context.AddInst<SemIR::Return>(node_id, {});
  129. }
  130. auto BuildReturnWithExpr(Context& context, Parse::ReturnStatementId node_id,
  131. SemIR::InstId expr_id) -> void {
  132. const auto& function = GetCurrentFunction(context);
  133. auto returned_var_id = GetCurrentReturnedVar(context);
  134. auto return_slot_id = SemIR::InstId::Invalid;
  135. auto return_info =
  136. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  137. if (!return_info.type_id.is_valid()) {
  138. CARBON_DIAGNOSTIC(
  139. ReturnStatementDisallowExpr, Error,
  140. "no return expression should be provided in this context");
  141. auto diag = context.emitter().Build(node_id, ReturnStatementDisallowExpr);
  142. NoteNoReturnTypeProvided(diag, function);
  143. diag.Emit();
  144. expr_id = SemIR::ErrorInst::SingletonInstId;
  145. } else if (returned_var_id.is_valid()) {
  146. CARBON_DIAGNOSTIC(
  147. ReturnExprWithReturnedVar, Error,
  148. "can only `return var;` in the scope of a `returned var`");
  149. auto diag = context.emitter().Build(node_id, ReturnExprWithReturnedVar);
  150. NoteReturnedVar(diag, returned_var_id);
  151. diag.Emit();
  152. expr_id = SemIR::ErrorInst::SingletonInstId;
  153. } else if (!return_info.is_valid()) {
  154. // We already diagnosed that the return type is invalid. Don't try to
  155. // convert to it.
  156. expr_id = SemIR::ErrorInst::SingletonInstId;
  157. } else if (return_info.has_return_slot()) {
  158. return_slot_id = GetCurrentReturnSlot(context);
  159. CARBON_CHECK(return_slot_id.is_valid());
  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