return.cpp 7.0 KB

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