resolve_control_flow.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "explorer/interpreter/resolve_control_flow.h"
  5. #include "explorer/ast/declaration.h"
  6. #include "explorer/ast/return_term.h"
  7. #include "explorer/ast/statement.h"
  8. #include "explorer/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<CallableDeclaration*> 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::ReturnVar:
  34. case StatementKind::ReturnExpression: {
  35. if (!function.has_value()) {
  36. return ProgramError(statement->source_loc())
  37. << "return is not within a function body";
  38. }
  39. const ReturnTerm& function_return =
  40. (*function)->declaration->return_term();
  41. if (function_return.is_auto()) {
  42. if ((*function)->saw_return_in_auto) {
  43. return ProgramError(statement->source_loc())
  44. << "Only one return is allowed in a function with an `auto` "
  45. "return type.";
  46. }
  47. (*function)->saw_return_in_auto = true;
  48. }
  49. auto& ret = cast<Return>(*statement);
  50. ret.set_function((*function)->declaration);
  51. if (statement->kind() == StatementKind::ReturnVar &&
  52. function_return.is_omitted()) {
  53. return ProgramError(statement->source_loc())
  54. << *statement
  55. << " should not provide a return value, to match the function's "
  56. "signature.";
  57. }
  58. if (statement->kind() == StatementKind::ReturnExpression) {
  59. auto& ret_exp = cast<ReturnExpression>(*statement);
  60. if (ret_exp.is_omitted_expression() != function_return.is_omitted()) {
  61. return ProgramError(ret_exp.source_loc())
  62. << ret_exp << " should"
  63. << (function_return.is_omitted() ? " not" : "")
  64. << " provide a return value, to match the function's "
  65. "signature.";
  66. }
  67. }
  68. return Success();
  69. }
  70. case StatementKind::Break:
  71. if (!loop.has_value()) {
  72. return ProgramError(statement->source_loc())
  73. << "break is not within a loop body";
  74. }
  75. cast<Break>(*statement).set_loop(*loop);
  76. return Success();
  77. case StatementKind::Continue:
  78. if (!loop.has_value()) {
  79. return ProgramError(statement->source_loc())
  80. << "continue is not within a loop body";
  81. }
  82. cast<Continue>(*statement).set_loop(*loop);
  83. return Success();
  84. case StatementKind::If: {
  85. auto& if_stmt = cast<If>(*statement);
  86. CARBON_RETURN_IF_ERROR(
  87. ResolveControlFlow(&if_stmt.then_block(), loop, function));
  88. if (if_stmt.else_block().has_value()) {
  89. CARBON_RETURN_IF_ERROR(
  90. ResolveControlFlow(*if_stmt.else_block(), loop, function));
  91. }
  92. return Success();
  93. }
  94. case StatementKind::Block: {
  95. auto& block = cast<Block>(*statement);
  96. for (auto* block_statement : block.statements()) {
  97. CARBON_RETURN_IF_ERROR(
  98. ResolveControlFlow(block_statement, loop, function));
  99. }
  100. return Success();
  101. }
  102. case StatementKind::For: {
  103. CARBON_RETURN_IF_ERROR(ResolveControlFlow(&cast<For>(*statement).body(),
  104. statement, function));
  105. return Success();
  106. }
  107. case StatementKind::While:
  108. CARBON_RETURN_IF_ERROR(ResolveControlFlow(&cast<While>(*statement).body(),
  109. statement, function));
  110. return Success();
  111. case StatementKind::Match: {
  112. auto& match = cast<Match>(*statement);
  113. for (Match::Clause& clause : match.clauses()) {
  114. CARBON_RETURN_IF_ERROR(
  115. ResolveControlFlow(&clause.statement(), loop, function));
  116. }
  117. return Success();
  118. }
  119. case StatementKind::Continuation:
  120. CARBON_RETURN_IF_ERROR(ResolveControlFlow(
  121. &cast<Continuation>(*statement).body(), std::nullopt, std::nullopt));
  122. return Success();
  123. case StatementKind::ExpressionStatement:
  124. case StatementKind::Assign:
  125. case StatementKind::VariableDefinition:
  126. case StatementKind::Run:
  127. case StatementKind::Await:
  128. return Success();
  129. }
  130. }
  131. auto ResolveControlFlow(Nonnull<Declaration*> declaration) -> ErrorOr<Success> {
  132. switch (declaration->kind()) {
  133. case DeclarationKind::DestructorDeclaration:
  134. case DeclarationKind::FunctionDeclaration: {
  135. auto& callable = cast<CallableDeclaration>(*declaration);
  136. if (callable.body().has_value()) {
  137. FunctionData data = {.declaration = &callable};
  138. CARBON_RETURN_IF_ERROR(
  139. ResolveControlFlow(*callable.body(), std::nullopt, &data));
  140. }
  141. break;
  142. }
  143. case DeclarationKind::ClassDeclaration: {
  144. auto& class_decl = cast<ClassDeclaration>(*declaration);
  145. for (Nonnull<Declaration*> member : class_decl.members()) {
  146. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  147. }
  148. break;
  149. }
  150. case DeclarationKind::MixinDeclaration: {
  151. auto& mixin_decl = cast<MixinDeclaration>(*declaration);
  152. for (Nonnull<Declaration*> member : mixin_decl.members()) {
  153. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  154. }
  155. break;
  156. }
  157. case DeclarationKind::InterfaceDeclaration: {
  158. auto& iface_decl = cast<InterfaceDeclaration>(*declaration);
  159. for (Nonnull<Declaration*> member : iface_decl.members()) {
  160. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  161. }
  162. break;
  163. }
  164. case DeclarationKind::ImplDeclaration: {
  165. auto& impl_decl = cast<ImplDeclaration>(*declaration);
  166. for (Nonnull<Declaration*> member : impl_decl.members()) {
  167. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  168. }
  169. break;
  170. }
  171. case DeclarationKind::ChoiceDeclaration:
  172. case DeclarationKind::VariableDeclaration:
  173. case DeclarationKind::InterfaceExtendsDeclaration:
  174. case DeclarationKind::InterfaceImplDeclaration:
  175. case DeclarationKind::AssociatedConstantDeclaration:
  176. case DeclarationKind::SelfDeclaration:
  177. case DeclarationKind::AliasDeclaration:
  178. case DeclarationKind::MixDeclaration:
  179. // do nothing
  180. break;
  181. }
  182. return Success();
  183. }
  184. auto ResolveControlFlow(AST& ast) -> ErrorOr<Success> {
  185. for (auto declaration : ast.declarations) {
  186. CARBON_RETURN_IF_ERROR(ResolveControlFlow(declaration));
  187. }
  188. return Success();
  189. }
  190. } // namespace Carbon