return.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #include "toolchain/parse/tree.h"
  7. #include "toolchain/sem_ir/inst.h"
  8. namespace Carbon::Check {
  9. // Gets the function that lexically encloses the current location.
  10. static auto GetCurrentFunction(Context& context) -> SemIR::Function& {
  11. CARBON_CHECK(!context.return_scope_stack().empty())
  12. << "Handling return but not in a function";
  13. auto function_id = context.insts()
  14. .GetAs<SemIR::FunctionDecl>(
  15. context.return_scope_stack().back().decl_id)
  16. .function_id;
  17. return context.functions().Get(function_id);
  18. }
  19. // Gets the currently in scope `returned var`, if any, that would be returned
  20. // by a `return var;`.
  21. static auto GetCurrentReturnedVar(Context& context) -> SemIR::InstId {
  22. CARBON_CHECK(!context.return_scope_stack().empty())
  23. << "Handling return but not in a function";
  24. return context.return_scope_stack().back().returned_var;
  25. }
  26. // Produces a note that the given function has no explicit return type.
  27. static auto NoteNoReturnTypeProvided(Context& context,
  28. Context::DiagnosticBuilder& diag,
  29. const SemIR::Function& function) {
  30. CARBON_DIAGNOSTIC(ReturnTypeOmittedNote, Note,
  31. "There was no return type provided.");
  32. diag.Note(context.insts().Get(function.declaration_id).parse_node(),
  33. ReturnTypeOmittedNote);
  34. }
  35. // Produces a note describing the return type of the given function.
  36. static auto NoteReturnType(Context& context, Context::DiagnosticBuilder& diag,
  37. const SemIR::Function& function) {
  38. // TODO: This is the location of the `fn` keyword. Find the location of the
  39. // return type.
  40. auto type_parse_node =
  41. context.insts().Get(function.declaration_id).parse_node();
  42. CARBON_DIAGNOSTIC(ReturnTypeHereNote, Note,
  43. "Return type of function is `{0}`.", std::string);
  44. diag.Note(type_parse_node, ReturnTypeHereNote,
  45. context.sem_ir().StringifyType(function.return_type_id, true));
  46. }
  47. // Produces a note pointing at the currently in scope `returned var`.
  48. static auto NoteReturnedVar(Context& context, Context::DiagnosticBuilder& diag,
  49. SemIR::InstId returned_var_id) {
  50. CARBON_DIAGNOSTIC(ReturnedVarHere, Note, "`returned var` was declared here.");
  51. diag.Note(context.insts().Get(returned_var_id).parse_node(), ReturnedVarHere);
  52. }
  53. auto CheckReturnedVar(Context& context, Parse::Node returned_node,
  54. Parse::Node name_node, SemIR::NameId name_id,
  55. Parse::Node type_node, SemIR::TypeId type_id)
  56. -> SemIR::InstId {
  57. // A `returned var` requires an explicit return type.
  58. auto& function = GetCurrentFunction(context);
  59. if (!function.return_type_id.is_valid()) {
  60. CARBON_DIAGNOSTIC(ReturnedVarWithNoReturnType, Error,
  61. "Cannot declare a `returned var` in this function.");
  62. auto diag =
  63. context.emitter().Build(returned_node, ReturnedVarWithNoReturnType);
  64. NoteNoReturnTypeProvided(context, diag, function);
  65. diag.Emit();
  66. return SemIR::InstId::BuiltinError;
  67. }
  68. // The declared type of the var must match the return type of the function.
  69. if (function.return_type_id != type_id) {
  70. CARBON_DIAGNOSTIC(ReturnedVarWrongType, Error,
  71. "Type `{0}` of `returned var` does not match "
  72. "return type of enclosing function.",
  73. std::string);
  74. auto diag =
  75. context.emitter().Build(type_node, ReturnedVarWrongType,
  76. context.sem_ir().StringifyType(type_id, true));
  77. NoteReturnType(context, diag, function);
  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 (function.return_slot_id.is_valid()) {
  84. return function.return_slot_id;
  85. }
  86. return context.AddInst(SemIR::VarStorage{name_node, type_id, name_id});
  87. }
  88. auto RegisterReturnedVar(Context& context, SemIR::InstId bind_id) -> void {
  89. auto existing_id = context.SetReturnedVarOrGetExisting(bind_id);
  90. if (existing_id.is_valid()) {
  91. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  92. "Cannot declare a `returned var` in the scope of "
  93. "another `returned var`.");
  94. auto diag = context.emitter().Build(
  95. context.insts().Get(bind_id).parse_node(), ReturnedVarShadowed);
  96. NoteReturnedVar(context, diag, existing_id);
  97. diag.Emit();
  98. }
  99. }
  100. auto BuildReturnWithNoExpr(Context& context, Parse::Node parse_node) -> void {
  101. const auto& function = GetCurrentFunction(context);
  102. if (function.return_type_id.is_valid()) {
  103. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  104. "Missing return value.", std::string);
  105. auto diag = context.emitter().Build(
  106. parse_node, ReturnStatementMissingExpr,
  107. context.sem_ir().StringifyType(function.return_type_id));
  108. NoteReturnType(context, diag, function);
  109. diag.Emit();
  110. }
  111. context.AddInst(SemIR::Return{parse_node});
  112. }
  113. auto BuildReturnWithExpr(Context& context, Parse::Node parse_node,
  114. SemIR::InstId expr_id) -> void {
  115. const auto& function = GetCurrentFunction(context);
  116. auto returned_var_id = GetCurrentReturnedVar(context);
  117. if (!function.return_type_id.is_valid()) {
  118. CARBON_DIAGNOSTIC(
  119. ReturnStatementDisallowExpr, Error,
  120. "No return expression should be provided in this context.");
  121. auto diag =
  122. context.emitter().Build(parse_node, ReturnStatementDisallowExpr);
  123. NoteNoReturnTypeProvided(context, diag, function);
  124. diag.Emit();
  125. expr_id = SemIR::InstId::BuiltinError;
  126. } else if (returned_var_id.is_valid()) {
  127. CARBON_DIAGNOSTIC(
  128. ReturnExprWithReturnedVar, Error,
  129. "Can only `return var;` in the scope of a `returned var`.");
  130. auto diag = context.emitter().Build(parse_node, ReturnExprWithReturnedVar);
  131. NoteReturnedVar(context, diag, returned_var_id);
  132. diag.Emit();
  133. expr_id = SemIR::InstId::BuiltinError;
  134. } else if (function.return_slot_id.is_valid()) {
  135. expr_id = Initialize(context, parse_node, function.return_slot_id, expr_id);
  136. } else {
  137. expr_id = ConvertToValueOfType(context, parse_node, expr_id,
  138. function.return_type_id);
  139. }
  140. context.AddInst(SemIR::ReturnExpr{parse_node, expr_id});
  141. }
  142. auto BuildReturnVar(Context& context, Parse::Node parse_node) -> void {
  143. const auto& function = GetCurrentFunction(context);
  144. auto returned_var_id = GetCurrentReturnedVar(context);
  145. if (!returned_var_id.is_valid()) {
  146. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  147. "`return var;` with no `returned var` in scope.");
  148. context.emitter().Emit(parse_node, ReturnVarWithNoReturnedVar);
  149. returned_var_id = SemIR::InstId::BuiltinError;
  150. }
  151. if (!function.return_slot_id.is_valid()) {
  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. }
  156. context.AddInst(SemIR::ReturnExpr{parse_node, returned_var_id});
  157. }
  158. } // namespace Carbon::Check