return.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.decl_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 = context.insts().Get(function.decl_id).parse_node();
  41. CARBON_DIAGNOSTIC(ReturnTypeHereNote, Note,
  42. "Return type of function is `{0}`.", std::string);
  43. diag.Note(type_parse_node, ReturnTypeHereNote,
  44. context.sem_ir().StringifyType(function.return_type_id, true));
  45. }
  46. // Produces a note pointing at the currently in scope `returned var`.
  47. static auto NoteReturnedVar(Context& context, Context::DiagnosticBuilder& diag,
  48. SemIR::InstId returned_var_id) {
  49. CARBON_DIAGNOSTIC(ReturnedVarHere, Note, "`returned var` was declared here.");
  50. diag.Note(context.insts().Get(returned_var_id).parse_node(), ReturnedVarHere);
  51. }
  52. auto CheckReturnedVar(Context& context, Parse::Node returned_node,
  53. Parse::Node name_node, SemIR::NameId name_id,
  54. Parse::Node type_node, SemIR::TypeId type_id)
  55. -> SemIR::InstId {
  56. // A `returned var` requires an explicit return type.
  57. auto& function = GetCurrentFunction(context);
  58. if (!function.return_type_id.is_valid()) {
  59. CARBON_DIAGNOSTIC(ReturnedVarWithNoReturnType, Error,
  60. "Cannot declare a `returned var` in this function.");
  61. auto diag =
  62. context.emitter().Build(returned_node, ReturnedVarWithNoReturnType);
  63. NoteNoReturnTypeProvided(context, diag, function);
  64. diag.Emit();
  65. return SemIR::InstId::BuiltinError;
  66. }
  67. // The declared type of the var must match the return type of the function.
  68. if (function.return_type_id != type_id) {
  69. CARBON_DIAGNOSTIC(ReturnedVarWrongType, Error,
  70. "Type `{0}` of `returned var` does not match "
  71. "return type of enclosing function.",
  72. std::string);
  73. auto diag =
  74. context.emitter().Build(type_node, ReturnedVarWrongType,
  75. context.sem_ir().StringifyType(type_id, true));
  76. NoteReturnType(context, diag, function);
  77. diag.Emit();
  78. return SemIR::InstId::BuiltinError;
  79. }
  80. // The variable aliases the return slot if there is one. If not, it has its
  81. // own storage.
  82. if (function.return_slot_id.is_valid()) {
  83. return function.return_slot_id;
  84. }
  85. return context.AddInst(SemIR::VarStorage{name_node, type_id, name_id});
  86. }
  87. auto RegisterReturnedVar(Context& context, SemIR::InstId bind_id) -> void {
  88. auto existing_id = context.SetReturnedVarOrGetExisting(bind_id);
  89. if (existing_id.is_valid()) {
  90. CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
  91. "Cannot declare a `returned var` in the scope of "
  92. "another `returned var`.");
  93. auto diag = context.emitter().Build(
  94. context.insts().Get(bind_id).parse_node(), ReturnedVarShadowed);
  95. NoteReturnedVar(context, diag, existing_id);
  96. diag.Emit();
  97. }
  98. }
  99. auto BuildReturnWithNoExpr(Context& context, Parse::Node parse_node) -> void {
  100. const auto& function = GetCurrentFunction(context);
  101. if (function.return_type_id.is_valid()) {
  102. CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
  103. "Missing return value.", std::string);
  104. auto diag = context.emitter().Build(
  105. parse_node, ReturnStatementMissingExpr,
  106. context.sem_ir().StringifyType(function.return_type_id));
  107. NoteReturnType(context, diag, function);
  108. diag.Emit();
  109. }
  110. context.AddInst(SemIR::Return{parse_node});
  111. }
  112. auto BuildReturnWithExpr(Context& context, Parse::Node parse_node,
  113. SemIR::InstId expr_id) -> void {
  114. const auto& function = GetCurrentFunction(context);
  115. auto returned_var_id = GetCurrentReturnedVar(context);
  116. if (!function.return_type_id.is_valid()) {
  117. CARBON_DIAGNOSTIC(
  118. ReturnStatementDisallowExpr, Error,
  119. "No return expression should be provided in this context.");
  120. auto diag =
  121. context.emitter().Build(parse_node, ReturnStatementDisallowExpr);
  122. NoteNoReturnTypeProvided(context, diag, function);
  123. diag.Emit();
  124. expr_id = SemIR::InstId::BuiltinError;
  125. } else if (returned_var_id.is_valid()) {
  126. CARBON_DIAGNOSTIC(
  127. ReturnExprWithReturnedVar, Error,
  128. "Can only `return var;` in the scope of a `returned var`.");
  129. auto diag = context.emitter().Build(parse_node, ReturnExprWithReturnedVar);
  130. NoteReturnedVar(context, diag, returned_var_id);
  131. diag.Emit();
  132. expr_id = SemIR::InstId::BuiltinError;
  133. } else if (function.return_slot_id.is_valid()) {
  134. expr_id = Initialize(context, parse_node, function.return_slot_id, expr_id);
  135. } else {
  136. expr_id = ConvertToValueOfType(context, parse_node, expr_id,
  137. function.return_type_id);
  138. }
  139. context.AddInst(SemIR::ReturnExpr{parse_node, expr_id});
  140. }
  141. auto BuildReturnVar(Context& context, Parse::Node parse_node) -> 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(parse_node, ReturnVarWithNoReturnedVar);
  148. returned_var_id = SemIR::InstId::BuiltinError;
  149. }
  150. if (!function.return_slot_id.is_valid()) {
  151. // If we don't have a return slot, we're returning by value. Convert to a
  152. // value expression.
  153. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  154. }
  155. context.AddInst(SemIR::ReturnExpr{parse_node, returned_var_id});
  156. }
  157. } // namespace Carbon::Check