resolve_control_flow.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "executable_semantics/interpreter/resolve_control_flow.h"
  5. #include "executable_semantics/ast/declaration.h"
  6. #include "executable_semantics/ast/statement.h"
  7. #include "executable_semantics/common/error.h"
  8. #include "llvm/Support/Casting.h"
  9. using llvm::cast;
  10. namespace Carbon {
  11. // Resolves control-flow edges in the AST rooted at `statement`. `return`
  12. // statements will resolve to `*function`, and `break` and `continue`
  13. // statements will resolve to `*loop`. If either parameter is nullopt, that
  14. // indicates a context where the corresponding statements are not permitted.
  15. static void ResolveControlFlow(
  16. Nonnull<Statement*> statement,
  17. std::optional<Nonnull<const FunctionDeclaration*>> function,
  18. std::optional<Nonnull<const Statement*>> loop) {
  19. switch (statement->kind()) {
  20. case Statement::Kind::Return:
  21. if (!function.has_value()) {
  22. FATAL_COMPILATION_ERROR(statement->source_loc())
  23. << "return is not within a function body";
  24. }
  25. cast<Return>(*statement).set_function(*function);
  26. return;
  27. case Statement::Kind::Break:
  28. if (!loop.has_value()) {
  29. FATAL_COMPILATION_ERROR(statement->source_loc())
  30. << "break is not within a loop body";
  31. }
  32. cast<Break>(*statement).set_loop(*loop);
  33. return;
  34. case Statement::Kind::Continue:
  35. if (!loop.has_value()) {
  36. FATAL_COMPILATION_ERROR(statement->source_loc())
  37. << "continue is not within a loop body";
  38. }
  39. cast<Continue>(*statement).set_loop(*loop);
  40. return;
  41. case Statement::Kind::If: {
  42. auto& if_stmt = cast<If>(*statement);
  43. ResolveControlFlow(&if_stmt.then_block(), function, loop);
  44. if (if_stmt.else_block().has_value()) {
  45. ResolveControlFlow(*if_stmt.else_block(), function, loop);
  46. }
  47. return;
  48. }
  49. case Statement::Kind::Block: {
  50. auto& block = cast<Block>(*statement);
  51. for (auto* block_statement : block.statements()) {
  52. ResolveControlFlow(block_statement, function, loop);
  53. }
  54. return;
  55. }
  56. case Statement::Kind::While:
  57. ResolveControlFlow(&cast<While>(*statement).body(), function, statement);
  58. return;
  59. case Statement::Kind::Match: {
  60. auto& match = cast<Match>(*statement);
  61. for (Match::Clause& clause : match.clauses()) {
  62. ResolveControlFlow(&clause.statement(), function, loop);
  63. }
  64. return;
  65. }
  66. case Statement::Kind::Continuation:
  67. ResolveControlFlow(&cast<Continuation>(*statement).body(), std::nullopt,
  68. std::nullopt);
  69. return;
  70. case Statement::Kind::ExpressionStatement:
  71. case Statement::Kind::Assign:
  72. case Statement::Kind::VariableDefinition:
  73. case Statement::Kind::Run:
  74. case Statement::Kind::Await:
  75. return;
  76. }
  77. }
  78. void ResolveControlFlow(AST& ast) {
  79. for (auto declaration : ast.declarations) {
  80. if (declaration->kind() != Declaration::Kind::FunctionDeclaration) {
  81. continue;
  82. }
  83. auto& function = cast<FunctionDeclaration>(*declaration);
  84. if (function.body().has_value()) {
  85. ResolveControlFlow(*function.body(), &function, std::nullopt);
  86. }
  87. }
  88. }
  89. } // namespace Carbon