return.cpp 7.0 KB

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