resolve_control_flow.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // Aggregate information about a function being analyzed.
  12. struct FunctionData {
  13. // The function declaration.
  14. Nonnull<FunctionDeclaration*> declaration;
  15. // True if the function has a deduced return type, and we've already seen
  16. // a `return` statement in its body.
  17. bool saw_return_in_auto = false;
  18. };
  19. // Resolves control-flow edges such as `Return::function()` and `Break::loop()`
  20. // in the AST rooted at `statement`. `loop` is the innermost loop that
  21. // statically encloses `statement`, or nullopt if there is no such loop.
  22. // `function` carries information about the function body that `statement`
  23. // belongs to, and that information may be updated by this call. `function`
  24. // can be nullopt if `statement` does not belong to a function body, for
  25. // example if it is part of a continuation body instead.
  26. static void ResolveControlFlow(Nonnull<Statement*> statement,
  27. std::optional<Nonnull<const Statement*>> loop,
  28. std::optional<Nonnull<FunctionData*>> function) {
  29. switch (statement->kind()) {
  30. case StatementKind::Return: {
  31. if (!function.has_value()) {
  32. FATAL_COMPILATION_ERROR(statement->source_loc())
  33. << "return is not within a function body";
  34. }
  35. const ReturnTerm& function_return =
  36. (*function)->declaration->return_term();
  37. if (function_return.is_auto()) {
  38. if ((*function)->saw_return_in_auto) {
  39. FATAL_COMPILATION_ERROR(statement->source_loc())
  40. << "Only one return is allowed in a function with an `auto` "
  41. "return type.";
  42. }
  43. (*function)->saw_return_in_auto = true;
  44. }
  45. auto& ret = cast<Return>(*statement);
  46. ret.set_function((*function)->declaration);
  47. if (ret.is_omitted_expression() != function_return.is_omitted()) {
  48. FATAL_COMPILATION_ERROR(ret.source_loc())
  49. << ret << " should" << (function_return.is_omitted() ? " not" : "")
  50. << " provide a return value, to match the function's signature.";
  51. }
  52. return;
  53. }
  54. case StatementKind::Break:
  55. if (!loop.has_value()) {
  56. FATAL_COMPILATION_ERROR(statement->source_loc())
  57. << "break is not within a loop body";
  58. }
  59. cast<Break>(*statement).set_loop(*loop);
  60. return;
  61. case StatementKind::Continue:
  62. if (!loop.has_value()) {
  63. FATAL_COMPILATION_ERROR(statement->source_loc())
  64. << "continue is not within a loop body";
  65. }
  66. cast<Continue>(*statement).set_loop(*loop);
  67. return;
  68. case StatementKind::If: {
  69. auto& if_stmt = cast<If>(*statement);
  70. ResolveControlFlow(&if_stmt.then_block(), loop, function);
  71. if (if_stmt.else_block().has_value()) {
  72. ResolveControlFlow(*if_stmt.else_block(), loop, function);
  73. }
  74. return;
  75. }
  76. case StatementKind::Block: {
  77. auto& block = cast<Block>(*statement);
  78. for (auto* block_statement : block.statements()) {
  79. ResolveControlFlow(block_statement, loop, function);
  80. }
  81. return;
  82. }
  83. case StatementKind::While:
  84. ResolveControlFlow(&cast<While>(*statement).body(), statement, function);
  85. return;
  86. case StatementKind::Match: {
  87. auto& match = cast<Match>(*statement);
  88. for (Match::Clause& clause : match.clauses()) {
  89. ResolveControlFlow(&clause.statement(), loop, function);
  90. }
  91. return;
  92. }
  93. case StatementKind::Continuation:
  94. ResolveControlFlow(&cast<Continuation>(*statement).body(), std::nullopt,
  95. std::nullopt);
  96. return;
  97. case StatementKind::ExpressionStatement:
  98. case StatementKind::Assign:
  99. case StatementKind::VariableDefinition:
  100. case StatementKind::Run:
  101. case StatementKind::Await:
  102. return;
  103. }
  104. }
  105. void ResolveControlFlow(AST& ast) {
  106. for (auto declaration : ast.declarations) {
  107. if (declaration->kind() != DeclarationKind::FunctionDeclaration) {
  108. continue;
  109. }
  110. auto& function = cast<FunctionDeclaration>(*declaration);
  111. if (function.body().has_value()) {
  112. FunctionData data = {.declaration = &function};
  113. ResolveControlFlow(*function.body(), std::nullopt, &data);
  114. }
  115. }
  116. }
  117. } // namespace Carbon