return.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/context.h"
  5. #include "toolchain/check/convert.h"
  6. namespace Carbon::Check {
  7. // Gets the function that lexically encloses the current location.
  8. static auto GetCurrentFunction(Context& context) -> SemIR::Function& {
  9. CARBON_CHECK(!context.return_scope_stack().empty())
  10. << "Handling return but not in a function";
  11. auto function_id = context.insts()
  12. .GetAs<SemIR::FunctionDecl>(
  13. context.return_scope_stack().back().decl_id)
  14. .function_id;
  15. return context.functions().Get(function_id);
  16. }
  17. // Gets the currently in scope `returned var`, if any, that would be returned
  18. // by a `return var;`.
  19. static auto GetCurrentReturnedVar(Context& context) -> SemIR::InstId {
  20. CARBON_CHECK(!context.return_scope_stack().empty())
  21. << "Handling return but not in a function";
  22. return context.return_scope_stack().back().returned_var;
  23. }
  24. // Produces a note that the given function has no explicit return type.
  25. static auto NoteNoReturnTypeProvided(Context::DiagnosticBuilder& diag,
  26. const SemIR::Function& function) {
  27. CARBON_DIAGNOSTIC(ReturnTypeOmittedNote, Note,
  28. "There was no return type provided.");
  29. diag.Note(function.decl_id, ReturnTypeOmittedNote);
  30. }
  31. // Produces a note describing the return type of the given function.
  32. static auto NoteReturnType(Context::DiagnosticBuilder& diag,
  33. const SemIR::Function& function) {
  34. CARBON_DIAGNOSTIC(ReturnTypeHereNote, Note,
  35. "Return type of function is `{0}`.", SemIR::TypeId);
  36. // TODO: This is using the location of the `fn` keyword. Find the location of
  37. // the return type.
  38. diag.Note(function.decl_id, ReturnTypeHereNote, function.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. if (!function.return_type_id.is_valid()) {
  53. CARBON_DIAGNOSTIC(ReturnedVarWithNoReturnType, Error,
  54. "Cannot declare a `returned var` in this function.");
  55. auto diag =
  56. context.emitter().Build(returned_node, ReturnedVarWithNoReturnType);
  57. NoteNoReturnTypeProvided(diag, function);
  58. diag.Emit();
  59. return SemIR::InstId::BuiltinError;
  60. }
  61. // The declared type of the var must match the return type of the function.
  62. if (function.return_type_id != type_id) {
  63. CARBON_DIAGNOSTIC(ReturnedVarWrongType, Error,
  64. "Type `{0}` of `returned var` does not match "
  65. "return type of enclosing function.",
  66. SemIR::TypeId);
  67. auto diag =
  68. context.emitter().Build(type_node, ReturnedVarWrongType, type_id);
  69. NoteReturnType(diag, function);
  70. diag.Emit();
  71. return SemIR::InstId::BuiltinError;
  72. }
  73. // The variable aliases the return slot if there is one. If not, it has its
  74. // own storage.
  75. if (function.return_slot_id.is_valid()) {
  76. return function.return_slot_id;
  77. }
  78. return context.AddInst({name_node, SemIR::VarStorage{type_id, name_id}});
  79. }
  80. auto RegisterReturnedVar(Context& context, SemIR::InstId bind_id) -> void {
  81. auto existing_id = context.scope_stack().SetReturnedVarOrGetExisting(bind_id);
  82. if (existing_id.is_valid()) {
  83. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  84. "Cannot declare a `returned var` in the scope of "
  85. "another `returned var`.");
  86. auto diag = context.emitter().Build(bind_id, ReturnedVarShadowed);
  87. NoteReturnedVar(diag, existing_id);
  88. diag.Emit();
  89. }
  90. }
  91. auto BuildReturnWithNoExpr(Context& context, Parse::ReturnStatementId node_id)
  92. -> void {
  93. const auto& function = GetCurrentFunction(context);
  94. if (function.return_type_id.is_valid()) {
  95. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  96. "Missing return value.");
  97. auto diag = context.emitter().Build(node_id, ReturnStatementMissingExpr);
  98. NoteReturnType(diag, function);
  99. diag.Emit();
  100. }
  101. context.AddInst({node_id, SemIR::Return{}});
  102. }
  103. auto BuildReturnWithExpr(Context& context, Parse::ReturnStatementId node_id,
  104. SemIR::InstId expr_id) -> void {
  105. const auto& function = GetCurrentFunction(context);
  106. auto returned_var_id = GetCurrentReturnedVar(context);
  107. if (!function.return_type_id.is_valid()) {
  108. CARBON_DIAGNOSTIC(
  109. ReturnStatementDisallowExpr, Error,
  110. "No return expression should be provided in this context.");
  111. auto diag = context.emitter().Build(node_id, ReturnStatementDisallowExpr);
  112. NoteNoReturnTypeProvided(diag, function);
  113. diag.Emit();
  114. expr_id = SemIR::InstId::BuiltinError;
  115. } else if (returned_var_id.is_valid()) {
  116. CARBON_DIAGNOSTIC(
  117. ReturnExprWithReturnedVar, Error,
  118. "Can only `return var;` in the scope of a `returned var`.");
  119. auto diag = context.emitter().Build(node_id, ReturnExprWithReturnedVar);
  120. NoteReturnedVar(diag, returned_var_id);
  121. diag.Emit();
  122. expr_id = SemIR::InstId::BuiltinError;
  123. } else if (function.return_slot_id.is_valid()) {
  124. expr_id = Initialize(context, node_id, function.return_slot_id, expr_id);
  125. } else {
  126. expr_id = ConvertToValueOfType(context, node_id, expr_id,
  127. function.return_type_id);
  128. }
  129. context.AddInst({node_id, SemIR::ReturnExpr{expr_id}});
  130. }
  131. auto BuildReturnVar(Context& context, Parse::ReturnStatementId node_id)
  132. -> void {
  133. const auto& function = GetCurrentFunction(context);
  134. auto returned_var_id = GetCurrentReturnedVar(context);
  135. if (!returned_var_id.is_valid()) {
  136. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  137. "`return var;` with no `returned var` in scope.");
  138. context.emitter().Emit(node_id, ReturnVarWithNoReturnedVar);
  139. returned_var_id = SemIR::InstId::BuiltinError;
  140. }
  141. if (!function.return_slot_id.is_valid()) {
  142. // If we don't have a return slot, we're returning by value. Convert to a
  143. // value expression.
  144. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  145. }
  146. context.AddInst({node_id, SemIR::ReturnExpr{returned_var_id}});
  147. }
  148. } // namespace Carbon::Check