resolve_control_flow.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "explorer/interpreter/resolve_control_flow.h"
  5. #include "explorer/ast/declaration.h"
  6. #include "explorer/ast/return_term.h"
  7. #include "explorer/ast/statement.h"
  8. #include "explorer/common/error_builders.h"
  9. #include "llvm/Support/Casting.h"
  10. #include "llvm/Support/Error.h"
  11. using llvm::cast;
  12. namespace Carbon {
  13. // Aggregate information about a function being analyzed.
  14. struct FunctionData {
  15. // The function declaration.
  16. Nonnull<FunctionDeclaration*> declaration;
  17. // True if the function has a deduced return type, and we've already seen
  18. // a `return` statement in its body.
  19. bool saw_return_in_auto = false;
  20. };
  21. // Resolves control-flow edges such as `Return::function()` and `Break::loop()`
  22. // in the AST rooted at `statement`. `loop` is the innermost loop that
  23. // statically encloses `statement`, or nullopt if there is no such loop.
  24. // `function` carries information about the function body that `statement`
  25. // belongs to, and that information may be updated by this call. `function`
  26. // can be nullopt if `statement` does not belong to a function body, for
  27. // example if it is part of a continuation body instead.
  28. static auto ResolveControlFlow(Nonnull<Statement*> statement,
  29. std::optional<Nonnull<const Statement*>> loop,
  30. std::optional<Nonnull<FunctionData*>> function)
  31. -> ErrorOr<Success> {
  32. switch (statement->kind()) {
  33. case StatementKind::ReturnVar:
  34. case StatementKind::ReturnExpression: {
  35. if (!function.has_value()) {
  36. return CompilationError(statement->source_loc())
  37. << "return is not within a function body";
  38. }
  39. const ReturnTerm& function_return =
  40. (*function)->declaration->return_term();
  41. if (function_return.is_auto()) {
  42. if ((*function)->saw_return_in_auto) {
  43. return CompilationError(statement->source_loc())
  44. << "Only one return is allowed in a function with an `auto` "
  45. "return type.";
  46. }
  47. (*function)->saw_return_in_auto = true;
  48. }
  49. auto& ret = cast<Return>(*statement);
  50. ret.set_function((*function)->declaration);
  51. if (statement->kind() == StatementKind::ReturnVar &&
  52. function_return.is_omitted()) {
  53. return CompilationError(statement->source_loc())
  54. << *statement
  55. << " should not provide a return value, to match the function's "
  56. "signature.";
  57. }
  58. if (statement->kind() == StatementKind::ReturnExpression) {
  59. auto& ret_exp = cast<ReturnExpression>(*statement);
  60. if (ret_exp.is_omitted_expression() != function_return.is_omitted()) {
  61. return CompilationError(ret_exp.source_loc())
  62. << ret_exp << " should"
  63. << (function_return.is_omitted() ? " not" : "")
  64. << " provide a return value, to match the function's "
  65. "signature.";
  66. }
  67. }
  68. return Success();
  69. }
  70. case StatementKind::Break:
  71. if (!loop.has_value()) {
  72. return CompilationError(statement->source_loc())
  73. << "break is not within a loop body";
  74. }
  75. cast<Break>(*statement).set_loop(*loop);
  76. return Success();
  77. case StatementKind::Continue:
  78. if (!loop.has_value()) {
  79. return CompilationError(statement->source_loc())
  80. << "continue is not within a loop body";
  81. }
  82. cast<Continue>(*statement).set_loop(*loop);
  83. return Success();
  84. case StatementKind::If: {
  85. auto& if_stmt = cast<If>(*statement);
  86. CARBON_RETURN_IF_ERROR(
  87. ResolveControlFlow(&if_stmt.then_block(), loop, function));
  88. if (if_stmt.else_block().has_value()) {
  89. CARBON_RETURN_IF_ERROR(
  90. ResolveControlFlow(*if_stmt.else_block(), loop, function));
  91. }
  92. return Success();
  93. }
  94. case StatementKind::Block: {
  95. auto& block = cast<Block>(*statement);
  96. for (auto* block_statement : block.statements()) {
  97. CARBON_RETURN_IF_ERROR(
  98. ResolveControlFlow(block_statement, loop, function));
  99. }
  100. return Success();
  101. }
  102. case StatementKind::While:
  103. CARBON_RETURN_IF_ERROR(ResolveControlFlow(&cast<While>(*statement).body(),
  104. statement, function));
  105. return Success();
  106. case StatementKind::Match: {
  107. auto& match = cast<Match>(*statement);
  108. for (Match::Clause& clause : match.clauses()) {
  109. CARBON_RETURN_IF_ERROR(
  110. ResolveControlFlow(&clause.statement(), loop, function));
  111. }
  112. return Success();
  113. }
  114. case StatementKind::Continuation:
  115. CARBON_RETURN_IF_ERROR(ResolveControlFlow(
  116. &cast<Continuation>(*statement).body(), std::nullopt, std::nullopt));
  117. return Success();
  118. case StatementKind::ExpressionStatement:
  119. case StatementKind::Assign:
  120. case StatementKind::VariableDefinition:
  121. case StatementKind::Run:
  122. case StatementKind::Await:
  123. return Success();
  124. }
  125. }
  126. auto ResolveControlFlow(Nonnull<Declaration*> declaration) -> ErrorOr<Success> {
  127. switch (declaration->kind()) {
  128. case DeclarationKind::FunctionDeclaration: {
  129. auto& function = cast<FunctionDeclaration>(*declaration);
  130. if (function.body().has_value()) {
  131. FunctionData data = {.declaration = &function};
  132. CARBON_RETURN_IF_ERROR(
  133. ResolveControlFlow(*function.body(), std::nullopt, &data));
  134. }
  135. break;
  136. }
  137. case DeclarationKind::ClassDeclaration: {
  138. auto& class_decl = cast<ClassDeclaration>(*declaration);
  139. for (Nonnull<Declaration*> member : class_decl.members()) {
  140. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  141. }
  142. break;
  143. }
  144. case DeclarationKind::InterfaceDeclaration: {
  145. auto& iface_decl = cast<InterfaceDeclaration>(*declaration);
  146. for (Nonnull<Declaration*> member : iface_decl.members()) {
  147. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  148. }
  149. break;
  150. }
  151. case DeclarationKind::ImplDeclaration: {
  152. auto& impl_decl = cast<ImplDeclaration>(*declaration);
  153. for (Nonnull<Declaration*> member : impl_decl.members()) {
  154. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  155. }
  156. break;
  157. }
  158. case DeclarationKind::ChoiceDeclaration:
  159. case DeclarationKind::VariableDeclaration:
  160. case DeclarationKind::AssociatedConstantDeclaration:
  161. case DeclarationKind::SelfDeclaration:
  162. case DeclarationKind::AliasDeclaration:
  163. // do nothing
  164. break;
  165. }
  166. return Success();
  167. }
  168. auto ResolveControlFlow(AST& ast) -> ErrorOr<Success> {
  169. for (auto declaration : ast.declarations) {
  170. CARBON_RETURN_IF_ERROR(ResolveControlFlow(declaration));
  171. }
  172. return Success();
  173. }
  174. } // namespace Carbon