resolve_control_flow.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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::Sequence: {
  50. auto& seq = cast<Sequence>(*statement);
  51. ResolveControlFlow(&seq.statement(), function, loop);
  52. if (seq.next().has_value()) {
  53. ResolveControlFlow(*seq.next(), function, loop);
  54. }
  55. return;
  56. }
  57. case Statement::Kind::Block: {
  58. auto& block = cast<Block>(*statement);
  59. if (block.sequence().has_value()) {
  60. ResolveControlFlow(*block.sequence(), function, loop);
  61. }
  62. return;
  63. }
  64. case Statement::Kind::While:
  65. ResolveControlFlow(&cast<While>(*statement).body(), function, statement);
  66. return;
  67. case Statement::Kind::Match: {
  68. auto& match = cast<Match>(*statement);
  69. for (Match::Clause& clause : match.clauses()) {
  70. ResolveControlFlow(&clause.statement(), function, loop);
  71. }
  72. return;
  73. }
  74. case Statement::Kind::Continuation:
  75. ResolveControlFlow(&cast<Continuation>(*statement).body(), std::nullopt,
  76. std::nullopt);
  77. return;
  78. case Statement::Kind::ExpressionStatement:
  79. case Statement::Kind::Assign:
  80. case Statement::Kind::VariableDefinition:
  81. case Statement::Kind::Run:
  82. case Statement::Kind::Await:
  83. return;
  84. }
  85. }
  86. void ResolveControlFlow(AST& ast) {
  87. for (auto declaration : ast.declarations) {
  88. if (declaration->kind() != Declaration::Kind::FunctionDeclaration) {
  89. continue;
  90. }
  91. auto& function = cast<FunctionDeclaration>(*declaration);
  92. if (function.body().has_value()) {
  93. ResolveControlFlow(*function.body(), &function, std::nullopt);
  94. }
  95. }
  96. }
  97. } // namespace Carbon