return.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 currently in scope `returned var`, if any, that would be returned
  19. // by a `return var;`.
  20. static auto GetCurrentReturnedVar(Context& context) -> SemIR::InstId {
  21. CARBON_CHECK(!context.return_scope_stack().empty())
  22. << "Handling return but not in a function";
  23. return context.return_scope_stack().back().returned_var;
  24. }
  25. // Produces a note that the given function has no explicit return type.
  26. static auto NoteNoReturnTypeProvided(Context::DiagnosticBuilder& diag,
  27. const SemIR::Function& function) {
  28. CARBON_DIAGNOSTIC(ReturnTypeOmittedNote, Note,
  29. "There was no return type provided.");
  30. diag.Note(function.decl_id, ReturnTypeOmittedNote);
  31. }
  32. // Produces a note describing the return type of the given function.
  33. static auto NoteReturnType(Context::DiagnosticBuilder& diag,
  34. const SemIR::Function& function,
  35. SemIR::TypeId return_type_id) {
  36. CARBON_DIAGNOSTIC(ReturnTypeHereNote, Note,
  37. "Return type of function is `{0}`.", SemIR::TypeId);
  38. diag.Note(function.return_storage_id, ReturnTypeHereNote, return_type_id);
  39. }
  40. // Produces a note pointing at the currently in scope `returned var`.
  41. static auto NoteReturnedVar(Context::DiagnosticBuilder& diag,
  42. SemIR::InstId returned_var_id) {
  43. CARBON_DIAGNOSTIC(ReturnedVarHere, Note, "`returned var` was declared here.");
  44. diag.Note(returned_var_id, ReturnedVarHere);
  45. }
  46. auto CheckReturnedVar(Context& context, Parse::NodeId returned_node,
  47. Parse::NodeId name_node, SemIR::NameId name_id,
  48. Parse::NodeId type_node, SemIR::TypeId type_id)
  49. -> SemIR::InstId {
  50. // A `returned var` requires an explicit return type.
  51. auto& function = GetCurrentFunction(context);
  52. auto return_type_id = function.declared_return_type(context.sem_ir());
  53. if (!return_type_id.is_valid()) {
  54. CARBON_DIAGNOSTIC(ReturnedVarWithNoReturnType, Error,
  55. "Cannot declare a `returned var` in this function.");
  56. auto diag =
  57. context.emitter().Build(returned_node, ReturnedVarWithNoReturnType);
  58. NoteNoReturnTypeProvided(diag, function);
  59. diag.Emit();
  60. return SemIR::InstId::BuiltinError;
  61. }
  62. // The declared type of the var must match the return type of the function.
  63. if (return_type_id != type_id) {
  64. CARBON_DIAGNOSTIC(ReturnedVarWrongType, Error,
  65. "Type `{0}` of `returned var` does not match "
  66. "return type of enclosing function.",
  67. SemIR::TypeId);
  68. auto diag =
  69. context.emitter().Build(type_node, ReturnedVarWrongType, type_id);
  70. NoteReturnType(diag, function, return_type_id);
  71. diag.Emit();
  72. return SemIR::InstId::BuiltinError;
  73. }
  74. // The variable aliases the return slot if there is one. If not, it has its
  75. // own storage.
  76. if (function.has_return_slot()) {
  77. return function.return_storage_id;
  78. }
  79. return context.AddInst<SemIR::VarStorage>(
  80. name_node, {.type_id = type_id, .name_id = name_id});
  81. }
  82. auto RegisterReturnedVar(Context& context, SemIR::InstId bind_id) -> void {
  83. auto existing_id = context.scope_stack().SetReturnedVarOrGetExisting(bind_id);
  84. if (existing_id.is_valid()) {
  85. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  86. "Cannot declare a `returned var` in the scope of "
  87. "another `returned var`.");
  88. auto diag = context.emitter().Build(bind_id, ReturnedVarShadowed);
  89. NoteReturnedVar(diag, existing_id);
  90. diag.Emit();
  91. }
  92. }
  93. auto BuildReturnWithNoExpr(Context& context, Parse::ReturnStatementId node_id)
  94. -> void {
  95. const auto& function = GetCurrentFunction(context);
  96. auto return_type_id = function.declared_return_type(context.sem_ir());
  97. if (return_type_id.is_valid()) {
  98. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  99. "Missing return value.");
  100. auto diag = context.emitter().Build(node_id, ReturnStatementMissingExpr);
  101. NoteReturnType(diag, function, return_type_id);
  102. diag.Emit();
  103. }
  104. context.AddInst<SemIR::Return>(node_id, {});
  105. }
  106. auto BuildReturnWithExpr(Context& context, Parse::ReturnStatementId node_id,
  107. SemIR::InstId expr_id) -> void {
  108. const auto& function = GetCurrentFunction(context);
  109. auto returned_var_id = GetCurrentReturnedVar(context);
  110. auto return_slot_id = SemIR::InstId::Invalid;
  111. auto return_type_id = function.declared_return_type(context.sem_ir());
  112. if (!return_type_id.is_valid()) {
  113. CARBON_DIAGNOSTIC(
  114. ReturnStatementDisallowExpr, Error,
  115. "No return expression should be provided in this context.");
  116. auto diag = context.emitter().Build(node_id, ReturnStatementDisallowExpr);
  117. NoteNoReturnTypeProvided(diag, function);
  118. diag.Emit();
  119. expr_id = SemIR::InstId::BuiltinError;
  120. } else if (returned_var_id.is_valid()) {
  121. CARBON_DIAGNOSTIC(
  122. ReturnExprWithReturnedVar, Error,
  123. "Can only `return var;` in the scope of a `returned var`.");
  124. auto diag = context.emitter().Build(node_id, ReturnExprWithReturnedVar);
  125. NoteReturnedVar(diag, returned_var_id);
  126. diag.Emit();
  127. expr_id = SemIR::InstId::BuiltinError;
  128. } else if (function.has_return_slot()) {
  129. expr_id = Initialize(context, node_id, function.return_storage_id, expr_id);
  130. return_slot_id = function.return_storage_id;
  131. } else if (function.return_slot == SemIR::Function::ReturnSlot::Error) {
  132. // Don't produce a second error complaining the return type is incomplete.
  133. expr_id = SemIR::InstId::BuiltinError;
  134. } else {
  135. expr_id = ConvertToValueOfType(context, node_id, expr_id, return_type_id);
  136. }
  137. context.AddInst<SemIR::ReturnExpr>(
  138. node_id, {.expr_id = expr_id, .dest_id = return_slot_id});
  139. }
  140. auto BuildReturnVar(Context& context, Parse::ReturnStatementId node_id)
  141. -> void {
  142. const auto& function = GetCurrentFunction(context);
  143. auto returned_var_id = GetCurrentReturnedVar(context);
  144. if (!returned_var_id.is_valid()) {
  145. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  146. "`return var;` with no `returned var` in scope.");
  147. context.emitter().Emit(node_id, ReturnVarWithNoReturnedVar);
  148. returned_var_id = SemIR::InstId::BuiltinError;
  149. }
  150. auto return_slot_id = function.return_storage_id;
  151. if (!function.has_return_slot()) {
  152. // If we don't have a return slot, we're returning by value. Convert to a
  153. // value expression.
  154. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  155. return_slot_id = SemIR::InstId::Invalid;
  156. }
  157. context.AddInst<SemIR::ReturnExpr>(
  158. node_id, {.expr_id = returned_var_id, .dest_id = return_slot_id});
  159. }
  160. } // namespace Carbon::Check