resolve_control_flow.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_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. RETURN_IF_ERROR(
  75. ResolveControlFlow(&if_stmt.then_block(), loop, function));
  76. if (if_stmt.else_block().has_value()) {
  77. 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. RETURN_IF_ERROR(ResolveControlFlow(block_statement, loop, function));
  86. }
  87. return Success();
  88. }
  89. case StatementKind::While:
  90. RETURN_IF_ERROR(ResolveControlFlow(&cast<While>(*statement).body(),
  91. statement, function));
  92. return Success();
  93. case StatementKind::Match: {
  94. auto& match = cast<Match>(*statement);
  95. for (Match::Clause& clause : match.clauses()) {
  96. RETURN_IF_ERROR(
  97. ResolveControlFlow(&clause.statement(), loop, function));
  98. }
  99. return Success();
  100. }
  101. case StatementKind::Continuation:
  102. RETURN_IF_ERROR(ResolveControlFlow(&cast<Continuation>(*statement).body(),
  103. std::nullopt, std::nullopt));
  104. return Success();
  105. case StatementKind::ExpressionStatement:
  106. case StatementKind::Assign:
  107. case StatementKind::VariableDefinition:
  108. case StatementKind::Run:
  109. case StatementKind::Await:
  110. return Success();
  111. }
  112. }
  113. auto ResolveControlFlow(Nonnull<Declaration*> declaration) -> ErrorOr<Success> {
  114. switch (declaration->kind()) {
  115. case DeclarationKind::FunctionDeclaration: {
  116. auto& function = cast<FunctionDeclaration>(*declaration);
  117. if (function.body().has_value()) {
  118. FunctionData data = {.declaration = &function};
  119. RETURN_IF_ERROR(
  120. ResolveControlFlow(*function.body(), std::nullopt, &data));
  121. }
  122. break;
  123. }
  124. case DeclarationKind::ClassDeclaration: {
  125. auto& class_decl = cast<ClassDeclaration>(*declaration);
  126. for (Nonnull<Declaration*> member : class_decl.members()) {
  127. RETURN_IF_ERROR(ResolveControlFlow(member));
  128. }
  129. break;
  130. }
  131. case DeclarationKind::InterfaceDeclaration: {
  132. auto& iface_decl = cast<InterfaceDeclaration>(*declaration);
  133. for (Nonnull<Declaration*> member : iface_decl.members()) {
  134. RETURN_IF_ERROR(ResolveControlFlow(member));
  135. }
  136. break;
  137. }
  138. case DeclarationKind::ImplDeclaration: {
  139. auto& impl_decl = cast<ImplDeclaration>(*declaration);
  140. for (Nonnull<Declaration*> member : impl_decl.members()) {
  141. RETURN_IF_ERROR(ResolveControlFlow(member));
  142. }
  143. break;
  144. }
  145. case DeclarationKind::ChoiceDeclaration:
  146. case DeclarationKind::VariableDeclaration:
  147. // do nothing
  148. break;
  149. }
  150. return Success();
  151. }
  152. auto ResolveControlFlow(AST& ast) -> ErrorOr<Success> {
  153. for (auto declaration : ast.declarations) {
  154. RETURN_IF_ERROR(ResolveControlFlow(declaration));
  155. }
  156. return Success();
  157. }
  158. } // namespace Carbon