return.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. auto& function = GetCurrentFunction(context);
  51. auto return_info =
  52. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  53. if (!return_info.is_valid()) {
  54. // We already diagnosed this when we started defining the function. Create a
  55. // placeholder for error recovery.
  56. return context.AddInst<SemIR::VarStorage>(
  57. name_node, {.type_id = type_id, .name_id = name_id});
  58. }
  59. // A `returned var` requires an explicit return type.
  60. if (!return_info.type_id.is_valid()) {
  61. CARBON_DIAGNOSTIC(ReturnedVarWithNoReturnType, Error,
  62. "Cannot declare a `returned var` in this function.");
  63. auto diag =
  64. context.emitter().Build(returned_node, ReturnedVarWithNoReturnType);
  65. NoteNoReturnTypeProvided(diag, function);
  66. diag.Emit();
  67. return SemIR::InstId::BuiltinError;
  68. }
  69. // The declared type of the var must match the return type of the function.
  70. if (return_info.type_id != type_id) {
  71. CARBON_DIAGNOSTIC(ReturnedVarWrongType, Error,
  72. "Type `{0}` of `returned var` does not match "
  73. "return type of enclosing function.",
  74. SemIR::TypeId);
  75. auto diag =
  76. context.emitter().Build(type_node, ReturnedVarWrongType, type_id);
  77. NoteReturnType(diag, function, return_info.type_id);
  78. diag.Emit();
  79. return SemIR::InstId::BuiltinError;
  80. }
  81. // The variable aliases the return slot if there is one. If not, it has its
  82. // own storage.
  83. if (return_info.has_return_slot()) {
  84. return function.return_storage_id;
  85. }
  86. return context.AddInst<SemIR::VarStorage>(
  87. name_node, {.type_id = type_id, .name_id = name_id});
  88. }
  89. auto RegisterReturnedVar(Context& context, SemIR::InstId bind_id) -> void {
  90. auto existing_id = context.scope_stack().SetReturnedVarOrGetExisting(bind_id);
  91. if (existing_id.is_valid()) {
  92. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  93. "Cannot declare a `returned var` in the scope of "
  94. "another `returned var`.");
  95. auto diag = context.emitter().Build(bind_id, ReturnedVarShadowed);
  96. NoteReturnedVar(diag, existing_id);
  97. diag.Emit();
  98. }
  99. }
  100. auto BuildReturnWithNoExpr(Context& context, Parse::ReturnStatementId node_id)
  101. -> void {
  102. const auto& function = GetCurrentFunction(context);
  103. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  104. if (return_type_id.is_valid()) {
  105. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  106. "Missing return value.");
  107. auto diag = context.emitter().Build(node_id, ReturnStatementMissingExpr);
  108. NoteReturnType(diag, function, return_type_id);
  109. diag.Emit();
  110. }
  111. context.AddInst<SemIR::Return>(node_id, {});
  112. }
  113. auto BuildReturnWithExpr(Context& context, Parse::ReturnStatementId node_id,
  114. SemIR::InstId expr_id) -> void {
  115. const auto& function = GetCurrentFunction(context);
  116. auto returned_var_id = GetCurrentReturnedVar(context);
  117. auto return_slot_id = SemIR::InstId::Invalid;
  118. auto return_info =
  119. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  120. if (!return_info.type_id.is_valid()) {
  121. CARBON_DIAGNOSTIC(
  122. ReturnStatementDisallowExpr, Error,
  123. "No return expression should be provided in this context.");
  124. auto diag = context.emitter().Build(node_id, ReturnStatementDisallowExpr);
  125. NoteNoReturnTypeProvided(diag, function);
  126. diag.Emit();
  127. expr_id = SemIR::InstId::BuiltinError;
  128. } else if (returned_var_id.is_valid()) {
  129. CARBON_DIAGNOSTIC(
  130. ReturnExprWithReturnedVar, Error,
  131. "Can only `return var;` in the scope of a `returned var`.");
  132. auto diag = context.emitter().Build(node_id, ReturnExprWithReturnedVar);
  133. NoteReturnedVar(diag, returned_var_id);
  134. diag.Emit();
  135. expr_id = SemIR::InstId::BuiltinError;
  136. } else if (!return_info.is_valid()) {
  137. // We already diagnosed that the return type is invalid. Don't try to
  138. // convert to it.
  139. expr_id = SemIR::InstId::BuiltinError;
  140. } else if (return_info.has_return_slot()) {
  141. expr_id = Initialize(context, node_id, function.return_storage_id, expr_id);
  142. return_slot_id = function.return_storage_id;
  143. } else {
  144. expr_id =
  145. ConvertToValueOfType(context, node_id, expr_id, return_info.type_id);
  146. }
  147. context.AddInst<SemIR::ReturnExpr>(
  148. node_id, {.expr_id = expr_id, .dest_id = return_slot_id});
  149. }
  150. auto BuildReturnVar(Context& context, Parse::ReturnStatementId node_id)
  151. -> void {
  152. const auto& function = GetCurrentFunction(context);
  153. auto returned_var_id = GetCurrentReturnedVar(context);
  154. if (!returned_var_id.is_valid()) {
  155. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  156. "`return var;` with no `returned var` in scope.");
  157. context.emitter().Emit(node_id, ReturnVarWithNoReturnedVar);
  158. returned_var_id = SemIR::InstId::BuiltinError;
  159. }
  160. auto return_slot_id = function.return_storage_id;
  161. if (!SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function)
  162. .has_return_slot()) {
  163. // If we don't have a return slot, we're returning by value. Convert to a
  164. // value expression.
  165. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  166. return_slot_id = SemIR::InstId::Invalid;
  167. }
  168. context.AddInst<SemIR::ReturnExpr>(
  169. node_id, {.expr_id = returned_var_id, .dest_id = return_slot_id});
  170. }
  171. } // namespace Carbon::Check