resolve_control_flow.cpp 5.6 KB

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