resolve_control_flow.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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::IncrementDecrement:
  126. case StatementKind::VariableDefinition:
  127. case StatementKind::Run:
  128. case StatementKind::Await:
  129. return Success();
  130. }
  131. }
  132. auto ResolveControlFlow(Nonnull<Declaration*> declaration) -> ErrorOr<Success> {
  133. switch (declaration->kind()) {
  134. case DeclarationKind::DestructorDeclaration:
  135. case DeclarationKind::FunctionDeclaration: {
  136. auto& callable = cast<CallableDeclaration>(*declaration);
  137. if (callable.body().has_value()) {
  138. FunctionData data = {.declaration = &callable};
  139. CARBON_RETURN_IF_ERROR(
  140. ResolveControlFlow(*callable.body(), std::nullopt, &data));
  141. }
  142. break;
  143. }
  144. case DeclarationKind::ClassDeclaration: {
  145. auto& class_decl = cast<ClassDeclaration>(*declaration);
  146. for (Nonnull<Declaration*> member : class_decl.members()) {
  147. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  148. }
  149. break;
  150. }
  151. case DeclarationKind::MixinDeclaration: {
  152. auto& mixin_decl = cast<MixinDeclaration>(*declaration);
  153. for (Nonnull<Declaration*> member : mixin_decl.members()) {
  154. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  155. }
  156. break;
  157. }
  158. case DeclarationKind::InterfaceDeclaration:
  159. case DeclarationKind::ConstraintDeclaration: {
  160. auto& iface_decl = cast<ConstraintTypeDeclaration>(*declaration);
  161. for (Nonnull<Declaration*> member : iface_decl.members()) {
  162. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  163. }
  164. break;
  165. }
  166. case DeclarationKind::ImplDeclaration: {
  167. auto& impl_decl = cast<ImplDeclaration>(*declaration);
  168. for (Nonnull<Declaration*> member : impl_decl.members()) {
  169. CARBON_RETURN_IF_ERROR(ResolveControlFlow(member));
  170. }
  171. break;
  172. }
  173. case DeclarationKind::MatchFirstDeclaration: {
  174. auto& match_first_decl = cast<MatchFirstDeclaration>(*declaration);
  175. for (Nonnull<Declaration*> impl : match_first_decl.impls()) {
  176. CARBON_RETURN_IF_ERROR(ResolveControlFlow(impl));
  177. }
  178. break;
  179. }
  180. case DeclarationKind::ChoiceDeclaration:
  181. case DeclarationKind::VariableDeclaration:
  182. case DeclarationKind::InterfaceExtendsDeclaration:
  183. case DeclarationKind::InterfaceImplDeclaration:
  184. case DeclarationKind::AssociatedConstantDeclaration:
  185. case DeclarationKind::SelfDeclaration:
  186. case DeclarationKind::AliasDeclaration:
  187. case DeclarationKind::MixDeclaration:
  188. // do nothing
  189. break;
  190. }
  191. return Success();
  192. }
  193. auto ResolveControlFlow(AST& ast) -> ErrorOr<Success> {
  194. for (auto* declaration : ast.declarations) {
  195. CARBON_RETURN_IF_ERROR(ResolveControlFlow(declaration));
  196. }
  197. return Success();
  198. }
  199. } // namespace Carbon