resolve_control_flow.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/base/error_builders.h"
  9. #include "explorer/base/print_as_id.h"
  10. #include "llvm/Support/Casting.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<TraceStream*> trace_stream,
  29. Nonnull<Statement*> statement,
  30. std::optional<Nonnull<const Statement*>> loop,
  31. std::optional<Nonnull<FunctionData*>> function)
  32. -> ErrorOr<Success> {
  33. SetFileContext set_file_ctx(*trace_stream, statement->source_loc());
  34. switch (statement->kind()) {
  35. case StatementKind::ReturnVar:
  36. case StatementKind::ReturnExpression: {
  37. if (!function.has_value()) {
  38. return ProgramError(statement->source_loc())
  39. << "return is not within a function body";
  40. }
  41. const ReturnTerm& function_return =
  42. (*function)->declaration->return_term();
  43. if (function_return.is_auto()) {
  44. if ((*function)->saw_return_in_auto) {
  45. return ProgramError(statement->source_loc())
  46. << "Only one return is allowed in a function with an `auto` "
  47. "return type.";
  48. }
  49. (*function)->saw_return_in_auto = true;
  50. }
  51. auto& ret = cast<Return>(*statement);
  52. ret.set_function((*function)->declaration);
  53. if (statement->kind() == StatementKind::ReturnVar &&
  54. function_return.is_omitted()) {
  55. return ProgramError(statement->source_loc())
  56. << *statement
  57. << " should not provide a return value, to match the function's "
  58. "signature.";
  59. }
  60. if (statement->kind() == StatementKind::ReturnExpression) {
  61. auto& ret_exp = cast<ReturnExpression>(*statement);
  62. if (ret_exp.is_omitted_expression() != function_return.is_omitted()) {
  63. return ProgramError(ret_exp.source_loc())
  64. << ret_exp << " should"
  65. << (function_return.is_omitted() ? " not" : "")
  66. << " provide a return value, to match the function's "
  67. "signature.";
  68. }
  69. }
  70. if (trace_stream->is_enabled()) {
  71. trace_stream->Result()
  72. << "flow-resolved return statement `" << *statement << "` in `"
  73. << PrintAsID(*((*function)->declaration)) << "` ("
  74. << statement->source_loc() << ")\n";
  75. }
  76. return Success();
  77. }
  78. case StatementKind::Break:
  79. if (!loop.has_value()) {
  80. return ProgramError(statement->source_loc())
  81. << "break is not within a loop body";
  82. }
  83. cast<Break>(*statement).set_loop(*loop);
  84. if (trace_stream->is_enabled()) {
  85. trace_stream->Result()
  86. << "flow-resolved break statement `" << *statement << "` for `"
  87. << PrintAsID(**loop) << "`\n";
  88. }
  89. return Success();
  90. case StatementKind::Continue:
  91. if (!loop.has_value()) {
  92. return ProgramError(statement->source_loc())
  93. << "continue is not within a loop body";
  94. }
  95. cast<Continue>(*statement).set_loop(*loop);
  96. if (trace_stream->is_enabled()) {
  97. trace_stream->Result()
  98. << "flow-resolved continue statement `" << *statement << "` in `"
  99. << PrintAsID(**loop) << "` (" << statement->source_loc() << ")\n";
  100. }
  101. return Success();
  102. case StatementKind::If: {
  103. auto& if_stmt = cast<If>(*statement);
  104. CARBON_RETURN_IF_ERROR(ResolveControlFlow(
  105. trace_stream, &if_stmt.then_block(), loop, function));
  106. if (if_stmt.else_block().has_value()) {
  107. CARBON_RETURN_IF_ERROR(ResolveControlFlow(
  108. trace_stream, *if_stmt.else_block(), loop, function));
  109. }
  110. return Success();
  111. }
  112. case StatementKind::Block: {
  113. auto& block = cast<Block>(*statement);
  114. for (auto* block_statement : block.statements()) {
  115. CARBON_RETURN_IF_ERROR(
  116. ResolveControlFlow(trace_stream, block_statement, loop, function));
  117. }
  118. return Success();
  119. }
  120. case StatementKind::For: {
  121. CARBON_RETURN_IF_ERROR(ResolveControlFlow(
  122. trace_stream, &cast<For>(*statement).body(), statement, function));
  123. if (trace_stream->is_enabled()) {
  124. trace_stream->Result()
  125. << "flow-resolved for statement `" << PrintAsID(*statement) << "` ("
  126. << statement->source_loc() << ")\n";
  127. }
  128. return Success();
  129. }
  130. case StatementKind::While:
  131. CARBON_RETURN_IF_ERROR(ResolveControlFlow(
  132. trace_stream, &cast<While>(*statement).body(), statement, function));
  133. if (trace_stream->is_enabled()) {
  134. trace_stream->Result()
  135. << "flow-resolved while statement `" << PrintAsID(*statement)
  136. << "` (" << statement->source_loc() << ")\n";
  137. }
  138. return Success();
  139. case StatementKind::Match: {
  140. auto& match = cast<Match>(*statement);
  141. for (Match::Clause& clause : match.clauses()) {
  142. CARBON_RETURN_IF_ERROR(ResolveControlFlow(
  143. trace_stream, &clause.statement(), loop, function));
  144. }
  145. return Success();
  146. }
  147. case StatementKind::ExpressionStatement:
  148. case StatementKind::Assign:
  149. case StatementKind::IncrementDecrement:
  150. case StatementKind::VariableDefinition:
  151. return Success();
  152. }
  153. }
  154. auto ResolveControlFlow(Nonnull<TraceStream*> trace_stream,
  155. Nonnull<Declaration*> declaration) -> ErrorOr<Success> {
  156. switch (declaration->kind()) {
  157. case DeclarationKind::DestructorDeclaration:
  158. case DeclarationKind::FunctionDeclaration: {
  159. auto& callable = cast<CallableDeclaration>(*declaration);
  160. if (callable.body().has_value()) {
  161. FunctionData data = {.declaration = &callable};
  162. CARBON_RETURN_IF_ERROR(ResolveControlFlow(
  163. trace_stream, *callable.body(), std::nullopt, &data));
  164. }
  165. break;
  166. }
  167. case DeclarationKind::ClassDeclaration: {
  168. auto& class_decl = cast<ClassDeclaration>(*declaration);
  169. for (Nonnull<Declaration*> member : class_decl.members()) {
  170. CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, member));
  171. }
  172. break;
  173. }
  174. case DeclarationKind::MixinDeclaration: {
  175. auto& mixin_decl = cast<MixinDeclaration>(*declaration);
  176. for (Nonnull<Declaration*> member : mixin_decl.members()) {
  177. CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, member));
  178. }
  179. break;
  180. }
  181. case DeclarationKind::InterfaceDeclaration:
  182. case DeclarationKind::ConstraintDeclaration: {
  183. auto& iface_decl = cast<ConstraintTypeDeclaration>(*declaration);
  184. for (Nonnull<Declaration*> member : iface_decl.members()) {
  185. CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, member));
  186. }
  187. break;
  188. }
  189. case DeclarationKind::ImplDeclaration: {
  190. auto& impl_decl = cast<ImplDeclaration>(*declaration);
  191. for (Nonnull<Declaration*> member : impl_decl.members()) {
  192. CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, member));
  193. }
  194. break;
  195. }
  196. case DeclarationKind::MatchFirstDeclaration: {
  197. auto& match_first_decl = cast<MatchFirstDeclaration>(*declaration);
  198. for (Nonnull<Declaration*> impl : match_first_decl.impl_declarations()) {
  199. CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, impl));
  200. }
  201. break;
  202. }
  203. case DeclarationKind::NamespaceDeclaration:
  204. case DeclarationKind::ChoiceDeclaration:
  205. case DeclarationKind::VariableDeclaration:
  206. case DeclarationKind::InterfaceExtendDeclaration:
  207. case DeclarationKind::InterfaceRequireDeclaration:
  208. case DeclarationKind::AssociatedConstantDeclaration:
  209. case DeclarationKind::SelfDeclaration:
  210. case DeclarationKind::AliasDeclaration:
  211. case DeclarationKind::MixDeclaration:
  212. case DeclarationKind::ExtendBaseDeclaration:
  213. // do nothing
  214. break;
  215. }
  216. return Success();
  217. }
  218. auto ResolveControlFlow(Nonnull<TraceStream*> trace_stream, AST& ast)
  219. -> ErrorOr<Success> {
  220. for (auto* declaration : ast.declarations) {
  221. CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, declaration));
  222. }
  223. return Success();
  224. }
  225. } // namespace Carbon