resolve_control_flow.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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::Return: {
  34. if (!function.has_value()) {
  35. return CompilationError(statement->source_loc())
  36. << "return is not within a function body";
  37. }
  38. const ReturnTerm& function_return =
  39. (*function)->declaration->return_term();
  40. if (function_return.is_auto()) {
  41. if ((*function)->saw_return_in_auto) {
  42. return CompilationError(statement->source_loc())
  43. << "Only one return is allowed in a function with an `auto` "
  44. "return type.";
  45. }
  46. (*function)->saw_return_in_auto = true;
  47. }
  48. auto& ret = cast<Return>(*statement);
  49. ret.set_function((*function)->declaration);
  50. if (ret.is_omitted_expression() != function_return.is_omitted()) {
  51. return CompilationError(ret.source_loc())
  52. << ret << " should"
  53. << (function_return.is_omitted() ? " not" : "")
  54. << " provide a return value, to match the function's signature.";
  55. }
  56. return Success();
  57. }
  58. case StatementKind::Break:
  59. if (!loop.has_value()) {
  60. return CompilationError(statement->source_loc())
  61. << "break is not within a loop body";
  62. }
  63. cast<Break>(*statement).set_loop(*loop);
  64. return Success();
  65. case StatementKind::Continue:
  66. if (!loop.has_value()) {
  67. return CompilationError(statement->source_loc())
  68. << "continue is not within a loop body";
  69. }
  70. cast<Continue>(*statement).set_loop(*loop);
  71. return Success();
  72. case StatementKind::If: {
  73. auto& if_stmt = cast<If>(*statement);
  74. CARBON_RETURN_IF_ERROR(
  75. ResolveControlFlow(&if_stmt.then_block(), loop, function));
  76. if (if_stmt.else_block().has_value()) {
  77. CARBON_RETURN_IF_ERROR(
  78. ResolveControlFlow(*if_stmt.else_block(), loop, function));
  79. }
  80. return Success();
  81. }
  82. case StatementKind::Block: {
  83. auto& block = cast<Block>(*statement);
  84. for (auto* block_statement : block.statements()) {
  85. CARBON_RETURN_IF_ERROR(
  86. ResolveControlFlow(block_statement, loop, function));
  87. }
  88. return Success();
  89. }
  90. case StatementKind::While:
  91. CARBON_RETURN_IF_ERROR(ResolveControlFlow(&cast<While>(*statement).body(),
  92. statement, function));
  93. return Success();
  94. case StatementKind::Match: {
  95. auto& match = cast<Match>(*statement);
  96. for (Match::Clause& clause : match.clauses()) {
  97. CARBON_RETURN_IF_ERROR(
  98. ResolveControlFlow(&clause.statement(), loop, function));
  99. }
  100. return Success();
  101. }
  102. case StatementKind::Continuation:
  103. CARBON_RETURN_IF_ERROR(ResolveControlFlow(
  104. &cast<Continuation>(*statement).body(), std::nullopt, std::nullopt));
  105. return Success();
  106. case StatementKind::ExpressionStatement:
  107. case StatementKind::Assign:
  108. case StatementKind::VariableDefinition:
  109. case StatementKind::Run:
  110. case StatementKind::Await:
  111. return Success();
  112. }
  113. }
  114. auto ResolveControlFlow(Nonnull<Declaration*> declaration) -> ErrorOr<Success> {
  115. switch (declaration->kind()) {
  116. case DeclarationKind::FunctionDeclaration: {
  117. auto& function = cast<FunctionDeclaration>(*declaration);
  118. if (function.body().has_value()) {
  119. FunctionData data = {.declaration = &function};
  120. CARBON_RETURN_IF_ERROR(
  121. ResolveControlFlow(*function.body(), std::nullopt, &data));
  122. }
  123. break;
  124. }
  125. case DeclarationKind::ClassDeclaration: {
  126. auto& class_decl = cast<ClassDeclaration>(*declaration);
  127. for (Nonnull<Declaration*> member : class_decl.members()) {
  128. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  129. }
  130. break;
  131. }
  132. case DeclarationKind::InterfaceDeclaration: {
  133. auto& iface_decl = cast<InterfaceDeclaration>(*declaration);
  134. for (Nonnull<Declaration*> member : iface_decl.members()) {
  135. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  136. }
  137. break;
  138. }
  139. case DeclarationKind::ImplDeclaration: {
  140. auto& impl_decl = cast<ImplDeclaration>(*declaration);
  141. for (Nonnull<Declaration*> member : impl_decl.members()) {
  142. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  143. }
  144. break;
  145. }
  146. case DeclarationKind::ChoiceDeclaration:
  147. case DeclarationKind::VariableDeclaration:
  148. case DeclarationKind::SelfDeclaration:
  149. // do nothing
  150. break;
  151. }
  152. return Success();
  153. }
  154. auto ResolveControlFlow(AST& ast) -> ErrorOr<Success> {
  155. for (auto declaration : ast.declarations) {
  156. CARBON_RETURN_IF_ERROR(ResolveControlFlow(declaration));
  157. }
  158. return Success();
  159. }
  160. } // namespace Carbon