resolve_control_flow.cpp 7.6 KB

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