return.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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));
  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::NodeId returned_node,
  53. Parse::NodeId name_node, SemIR::NameId name_id,
  54. Parse::NodeId 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));
  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::NodeId 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.");
  104. auto diag = context.emitter().Build(parse_node, ReturnStatementMissingExpr);
  105. NoteReturnType(context, diag, function);
  106. diag.Emit();
  107. }
  108. context.AddInst(SemIR::Return{parse_node});
  109. }
  110. auto BuildReturnWithExpr(Context& context, Parse::NodeId parse_node,
  111. SemIR::InstId expr_id) -> void {
  112. const auto& function = GetCurrentFunction(context);
  113. auto returned_var_id = GetCurrentReturnedVar(context);
  114. if (!function.return_type_id.is_valid()) {
  115. CARBON_DIAGNOSTIC(
  116. ReturnStatementDisallowExpr, Error,
  117. "No return expression should be provided in this context.");
  118. auto diag =
  119. context.emitter().Build(parse_node, ReturnStatementDisallowExpr);
  120. NoteNoReturnTypeProvided(context, diag, function);
  121. diag.Emit();
  122. expr_id = SemIR::InstId::BuiltinError;
  123. } else if (returned_var_id.is_valid()) {
  124. CARBON_DIAGNOSTIC(
  125. ReturnExprWithReturnedVar, Error,
  126. "Can only `return var;` in the scope of a `returned var`.");
  127. auto diag = context.emitter().Build(parse_node, ReturnExprWithReturnedVar);
  128. NoteReturnedVar(context, diag, returned_var_id);
  129. diag.Emit();
  130. expr_id = SemIR::InstId::BuiltinError;
  131. } else if (function.return_slot_id.is_valid()) {
  132. expr_id = Initialize(context, parse_node, function.return_slot_id, expr_id);
  133. } else {
  134. expr_id = ConvertToValueOfType(context, parse_node, expr_id,
  135. function.return_type_id);
  136. }
  137. context.AddInst(SemIR::ReturnExpr{parse_node, expr_id});
  138. }
  139. auto BuildReturnVar(Context& context, Parse::NodeId parse_node) -> void {
  140. const auto& function = GetCurrentFunction(context);
  141. auto returned_var_id = GetCurrentReturnedVar(context);
  142. if (!returned_var_id.is_valid()) {
  143. CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
  144. "`return var;` with no `returned var` in scope.");
  145. context.emitter().Emit(parse_node, ReturnVarWithNoReturnedVar);
  146. returned_var_id = SemIR::InstId::BuiltinError;
  147. }
  148. if (!function.return_slot_id.is_valid()) {
  149. // If we don't have a return slot, we're returning by value. Convert to a
  150. // value expression.
  151. returned_var_id = ConvertToValueExpr(context, returned_var_id);
  152. }
  153. context.AddInst(SemIR::ReturnExpr{parse_node, returned_var_id});
  154. }
  155. } // namespace Carbon::Check