resolve_unformed.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_unformed.h"
  5. #include <unordered_map>
  6. #include "common/check.h"
  7. #include "explorer/ast/ast.h"
  8. #include "explorer/ast/expression.h"
  9. #include "explorer/ast/pattern.h"
  10. #include "explorer/base/nonnull.h"
  11. #include "explorer/interpreter/stack_space.h"
  12. using llvm::cast;
  13. namespace Carbon {
  14. auto FlowFacts::action_type_string(ActionType action) const
  15. -> std::string_view {
  16. switch (action) {
  17. case ActionType::AddInit:
  18. return "add init";
  19. case ActionType::AddUninit:
  20. return "add uninit";
  21. case ActionType::Form:
  22. return "form";
  23. case ActionType::Check:
  24. return "check";
  25. case ActionType::None:
  26. return "none";
  27. }
  28. }
  29. auto FlowFacts::TakeAction(Nonnull<const AstNode*> node, ActionType action,
  30. SourceLocation source_loc, const std::string& name)
  31. -> ErrorOr<Success> {
  32. switch (action) {
  33. case ActionType::AddInit: {
  34. AddFact(node, FormedState::MustBeFormed);
  35. break;
  36. }
  37. case ActionType::AddUninit: {
  38. AddFact(node, FormedState::Unformed);
  39. break;
  40. }
  41. case ActionType::Form: {
  42. // TODO: Use CARBON_CHECK when we are able to handle global variables.
  43. auto entry = facts_.find(node);
  44. if (entry != facts_.end() &&
  45. entry->second.formed_state == FormedState::Unformed) {
  46. entry->second.formed_state = FormedState::MayBeFormed;
  47. }
  48. break;
  49. }
  50. case ActionType::Check: {
  51. // TODO: @slaterlatiao add all available value nodes to flow facts and use
  52. // CARBON_CHECK on the following line.
  53. auto entry = facts_.find(node);
  54. if (entry != facts_.end() &&
  55. entry->second.formed_state == FormedState::Unformed) {
  56. return ProgramError(source_loc)
  57. << "use of uninitialized variable " << name;
  58. }
  59. break;
  60. }
  61. case ActionType::None:
  62. break;
  63. }
  64. if (trace_stream_->is_enabled()) {
  65. trace_stream_->Result() << action_type_string(action) << " `" << name
  66. << "` (" << source_loc << ")\n";
  67. }
  68. return Success();
  69. }
  70. static auto ResolveUnformedImpl(Nonnull<TraceStream*> trace_stream,
  71. Nonnull<const Expression*> expression,
  72. FlowFacts& flow_facts,
  73. FlowFacts::ActionType action)
  74. -> ErrorOr<Success>;
  75. static auto ResolveUnformedImpl(Nonnull<TraceStream*> trace_stream,
  76. Nonnull<const Pattern*> pattern,
  77. FlowFacts& flow_facts,
  78. FlowFacts::ActionType action)
  79. -> ErrorOr<Success>;
  80. static auto ResolveUnformedImpl(Nonnull<TraceStream*> trace_stream,
  81. Nonnull<const Statement*> statement,
  82. FlowFacts& flow_facts,
  83. FlowFacts::ActionType action)
  84. -> ErrorOr<Success>;
  85. // Traverses the sub-AST rooted at the given node, resolving the formed/unformed
  86. // states of local variables within it and updating the flow facts.
  87. template <typename T>
  88. static auto ResolveUnformed(Nonnull<TraceStream*> trace_stream,
  89. Nonnull<const T*> expression, FlowFacts& flow_facts,
  90. FlowFacts::ActionType action) -> ErrorOr<Success> {
  91. return RunWithExtraStack([&] {
  92. return ResolveUnformedImpl(trace_stream, expression, flow_facts, action);
  93. });
  94. }
  95. static auto ResolveUnformedImpl(Nonnull<TraceStream*> trace_stream,
  96. Nonnull<const Expression*> expression,
  97. FlowFacts& flow_facts,
  98. FlowFacts::ActionType action)
  99. -> ErrorOr<Success> {
  100. switch (expression->kind()) {
  101. case ExpressionKind::IdentifierExpression: {
  102. const auto& identifier = cast<IdentifierExpression>(*expression);
  103. CARBON_RETURN_IF_ERROR(
  104. flow_facts.TakeAction(&identifier.value_node().base(), action,
  105. identifier.source_loc(), identifier.name()));
  106. break;
  107. }
  108. case ExpressionKind::CallExpression: {
  109. const auto& call = cast<CallExpression>(*expression);
  110. CARBON_RETURN_IF_ERROR(
  111. ResolveUnformed(trace_stream, &call.argument(), flow_facts, action));
  112. break;
  113. }
  114. case ExpressionKind::IntrinsicExpression: {
  115. const auto& intrin = cast<IntrinsicExpression>(*expression);
  116. CARBON_RETURN_IF_ERROR(
  117. ResolveUnformed(trace_stream, &intrin.args(), flow_facts, action));
  118. break;
  119. }
  120. case ExpressionKind::TupleLiteral:
  121. for (Nonnull<const Expression*> field :
  122. cast<TupleLiteral>(*expression).fields()) {
  123. CARBON_RETURN_IF_ERROR(
  124. ResolveUnformed(trace_stream, field, flow_facts, action));
  125. }
  126. break;
  127. case ExpressionKind::OperatorExpression: {
  128. const auto& opt_exp = cast<OperatorExpression>(*expression);
  129. if (opt_exp.op() == Operator::AddressOf) {
  130. CARBON_CHECK(opt_exp.arguments().size() == 1)
  131. << "OperatorExpression with op & can only have 1 argument";
  132. CARBON_RETURN_IF_ERROR(
  133. // When a variable is taken address of, defer the unformed check to
  134. // runtime. A more sound analysis can be implemented when a
  135. // points-to analysis is available.
  136. // TODO: This isn't enough to permit &x.y or &x[i] when x is
  137. // uninitialized, because x.y and x[i] both require x to be
  138. // initialized.
  139. ResolveUnformed(trace_stream, opt_exp.arguments().front(),
  140. flow_facts, FlowFacts::ActionType::Form));
  141. } else {
  142. for (Nonnull<const Expression*> operand : opt_exp.arguments()) {
  143. CARBON_RETURN_IF_ERROR(
  144. ResolveUnformed(trace_stream, operand, flow_facts, action));
  145. }
  146. }
  147. break;
  148. }
  149. case ExpressionKind::StructLiteral:
  150. for (const FieldInitializer& init :
  151. cast<StructLiteral>(*expression).fields()) {
  152. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, &init.expression(),
  153. flow_facts,
  154. FlowFacts::ActionType::Check));
  155. }
  156. break;
  157. case ExpressionKind::SimpleMemberAccessExpression:
  158. case ExpressionKind::CompoundMemberAccessExpression:
  159. case ExpressionKind::BaseAccessExpression:
  160. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  161. trace_stream, &cast<MemberAccessExpression>(*expression).object(),
  162. flow_facts, FlowFacts::ActionType::Check));
  163. break;
  164. case ExpressionKind::BuiltinConvertExpression:
  165. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  166. trace_stream,
  167. cast<BuiltinConvertExpression>(*expression).source_expression(),
  168. flow_facts, FlowFacts::ActionType::Check));
  169. break;
  170. case ExpressionKind::IndexExpression:
  171. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  172. trace_stream, &cast<IndexExpression>(*expression).object(),
  173. flow_facts, FlowFacts::ActionType::Check));
  174. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  175. trace_stream, &cast<IndexExpression>(*expression).offset(),
  176. flow_facts, FlowFacts::ActionType::Check));
  177. break;
  178. case ExpressionKind::IfExpression: {
  179. const auto& if_exp = cast<IfExpression>(*expression);
  180. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, &if_exp.condition(),
  181. flow_facts,
  182. FlowFacts::ActionType::Check));
  183. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  184. trace_stream, &if_exp.then_expression(), flow_facts, action));
  185. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  186. trace_stream, &if_exp.else_expression(), flow_facts, action));
  187. break;
  188. }
  189. case ExpressionKind::DotSelfExpression:
  190. case ExpressionKind::IntLiteral:
  191. case ExpressionKind::BoolLiteral:
  192. case ExpressionKind::BoolTypeLiteral:
  193. case ExpressionKind::IntTypeLiteral:
  194. case ExpressionKind::StringLiteral:
  195. case ExpressionKind::StringTypeLiteral:
  196. case ExpressionKind::TypeTypeLiteral:
  197. case ExpressionKind::ValueLiteral:
  198. case ExpressionKind::WhereExpression:
  199. case ExpressionKind::StructTypeLiteral:
  200. case ExpressionKind::UnimplementedExpression:
  201. case ExpressionKind::FunctionTypeLiteral:
  202. case ExpressionKind::ArrayTypeLiteral:
  203. break;
  204. }
  205. return Success();
  206. }
  207. static auto ResolveUnformedImpl(Nonnull<TraceStream*> trace_stream,
  208. Nonnull<const Pattern*> pattern,
  209. FlowFacts& flow_facts,
  210. FlowFacts::ActionType action)
  211. -> ErrorOr<Success> {
  212. switch (pattern->kind()) {
  213. case PatternKind::BindingPattern: {
  214. const auto& binding_pattern = cast<BindingPattern>(*pattern);
  215. CARBON_RETURN_IF_ERROR(flow_facts.TakeAction(&binding_pattern, action,
  216. binding_pattern.source_loc(),
  217. binding_pattern.name()));
  218. } break;
  219. case PatternKind::TuplePattern:
  220. for (Nonnull<const Pattern*> field :
  221. cast<TuplePattern>(*pattern).fields()) {
  222. CARBON_RETURN_IF_ERROR(
  223. ResolveUnformed(trace_stream, field, flow_facts, action));
  224. }
  225. break;
  226. case PatternKind::GenericBinding:
  227. case PatternKind::AlternativePattern:
  228. case PatternKind::ExpressionPattern:
  229. case PatternKind::AutoPattern:
  230. case PatternKind::VarPattern:
  231. case PatternKind::AddrPattern:
  232. // do nothing
  233. break;
  234. }
  235. return Success();
  236. }
  237. static auto ResolveUnformedImpl(Nonnull<TraceStream*> trace_stream,
  238. Nonnull<const Statement*> statement,
  239. FlowFacts& flow_facts,
  240. FlowFacts::ActionType action)
  241. -> ErrorOr<Success> {
  242. if (trace_stream->is_enabled()) {
  243. trace_stream->Start() << "resolving-unformed in stmt `"
  244. << PrintAsID(*statement) << "` ("
  245. << statement->source_loc() << ")\n";
  246. }
  247. switch (statement->kind()) {
  248. case StatementKind::Block: {
  249. const auto& block = cast<Block>(*statement);
  250. for (const auto* block_statement : block.statements()) {
  251. CARBON_RETURN_IF_ERROR(
  252. ResolveUnformed(trace_stream, block_statement, flow_facts, action));
  253. }
  254. break;
  255. }
  256. case StatementKind::VariableDefinition: {
  257. const auto& def = cast<VariableDefinition>(*statement);
  258. if (def.has_init()) {
  259. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, &def.pattern(),
  260. flow_facts,
  261. FlowFacts::ActionType::AddInit));
  262. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, &def.init(),
  263. flow_facts,
  264. FlowFacts::ActionType::Check));
  265. } else {
  266. CARBON_RETURN_IF_ERROR(
  267. ResolveUnformed(trace_stream, &def.pattern(), flow_facts,
  268. FlowFacts::ActionType::AddUninit));
  269. }
  270. break;
  271. }
  272. case StatementKind::ReturnVar: {
  273. const auto& ret_var = cast<ReturnVar>(*statement);
  274. const auto& binding_pattern =
  275. cast<BindingPattern>(ret_var.value_node().base());
  276. CARBON_RETURN_IF_ERROR(
  277. flow_facts.TakeAction(&binding_pattern, FlowFacts::ActionType::Check,
  278. ret_var.source_loc(), binding_pattern.name()));
  279. break;
  280. }
  281. case StatementKind::ReturnExpression: {
  282. const auto& ret_exp_stmt = cast<ReturnExpression>(*statement);
  283. CARBON_RETURN_IF_ERROR(
  284. ResolveUnformed(trace_stream, &ret_exp_stmt.expression(), flow_facts,
  285. FlowFacts::ActionType::Check));
  286. break;
  287. }
  288. case StatementKind::Assign: {
  289. const auto& assign = cast<Assign>(*statement);
  290. if (assign.op() != AssignOperator::Plain) {
  291. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, &assign.lhs(),
  292. flow_facts,
  293. FlowFacts::ActionType::Check));
  294. } else if (assign.lhs().kind() == ExpressionKind::IdentifierExpression) {
  295. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, &assign.lhs(),
  296. flow_facts,
  297. FlowFacts::ActionType::Form));
  298. } else {
  299. // TODO: Support checking non-identifier lhs expression.
  300. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, &assign.lhs(),
  301. flow_facts,
  302. FlowFacts::ActionType::None));
  303. }
  304. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, &assign.rhs(),
  305. flow_facts,
  306. FlowFacts::ActionType::Check));
  307. break;
  308. }
  309. case StatementKind::IncrementDecrement: {
  310. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  311. trace_stream, &cast<IncrementDecrement>(statement)->argument(),
  312. flow_facts, FlowFacts::ActionType::Check));
  313. break;
  314. }
  315. case StatementKind::ExpressionStatement: {
  316. const auto& exp_stmt = cast<ExpressionStatement>(*statement);
  317. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  318. trace_stream, &exp_stmt.expression(), flow_facts, action));
  319. break;
  320. }
  321. case StatementKind::If: {
  322. const auto& if_stmt = cast<If>(*statement);
  323. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, &if_stmt.condition(),
  324. flow_facts,
  325. FlowFacts::ActionType::Check));
  326. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  327. trace_stream, &if_stmt.then_block(), flow_facts, action));
  328. if (if_stmt.else_block().has_value()) {
  329. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  330. trace_stream, *if_stmt.else_block(), flow_facts, action));
  331. }
  332. break;
  333. }
  334. case StatementKind::While: {
  335. const auto& while_stmt = cast<While>(*statement);
  336. CARBON_RETURN_IF_ERROR(
  337. ResolveUnformed(trace_stream, &while_stmt.condition(), flow_facts,
  338. FlowFacts::ActionType::Check));
  339. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, &while_stmt.body(),
  340. flow_facts, action));
  341. break;
  342. }
  343. case StatementKind::Match: {
  344. const auto& match = cast<Match>(*statement);
  345. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, &match.expression(),
  346. flow_facts,
  347. FlowFacts::ActionType::Check));
  348. for (const auto& clause : match.clauses()) {
  349. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, &clause.pattern(),
  350. flow_facts,
  351. FlowFacts::ActionType::Check));
  352. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  353. trace_stream, &clause.statement(), flow_facts, action));
  354. }
  355. break;
  356. }
  357. case StatementKind::For: {
  358. const auto& for_stmt = cast<For>(*statement);
  359. CARBON_RETURN_IF_ERROR(
  360. ResolveUnformed(trace_stream, &for_stmt.loop_target(), flow_facts,
  361. FlowFacts::ActionType::Check));
  362. CARBON_RETURN_IF_ERROR(
  363. ResolveUnformed(trace_stream, &for_stmt.body(), flow_facts, action));
  364. break;
  365. }
  366. case StatementKind::Break:
  367. case StatementKind::Continue:
  368. // do nothing
  369. break;
  370. }
  371. return Success();
  372. }
  373. static auto ResolveUnformed(Nonnull<TraceStream*> trace_stream,
  374. Nonnull<const Declaration*> declaration)
  375. -> ErrorOr<Success>;
  376. static auto ResolveUnformed(
  377. Nonnull<TraceStream*> trace_stream,
  378. llvm::ArrayRef<Nonnull<const Declaration*>> declarations)
  379. -> ErrorOr<Success> {
  380. return RunWithExtraStack([trace_stream, declarations]() -> ErrorOr<Success> {
  381. for (Nonnull<const Declaration*> declaration : declarations) {
  382. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, declaration));
  383. }
  384. return Success();
  385. });
  386. }
  387. static auto ResolveUnformed(Nonnull<TraceStream*> trace_stream,
  388. Nonnull<const Declaration*> declaration)
  389. -> ErrorOr<Success> {
  390. SetFileContext set_file_ctx(*trace_stream, declaration->source_loc());
  391. if (trace_stream->is_enabled()) {
  392. trace_stream->Start() << "resolving-unformed in decl `"
  393. << PrintAsID(*declaration) << "` ("
  394. << declaration->source_loc() << ")\n";
  395. }
  396. switch (declaration->kind()) {
  397. // Checks formed/unformed state intraprocedurally.
  398. // Can be extended to an interprocedural analysis when a call graph is
  399. // available.
  400. case DeclarationKind::FunctionDeclaration:
  401. case DeclarationKind::DestructorDeclaration: {
  402. const auto& callable = cast<CallableDeclaration>(*declaration);
  403. const auto callable_body = callable.body();
  404. if (callable_body) {
  405. FlowFacts flow_facts(trace_stream);
  406. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, *callable_body,
  407. flow_facts,
  408. FlowFacts::ActionType::None));
  409. }
  410. break;
  411. }
  412. case DeclarationKind::NamespaceDeclaration:
  413. case DeclarationKind::MixDeclaration:
  414. case DeclarationKind::MatchFirstDeclaration:
  415. case DeclarationKind::ChoiceDeclaration:
  416. case DeclarationKind::VariableDeclaration:
  417. case DeclarationKind::InterfaceExtendDeclaration:
  418. case DeclarationKind::InterfaceRequireDeclaration:
  419. case DeclarationKind::AssociatedConstantDeclaration:
  420. case DeclarationKind::SelfDeclaration:
  421. case DeclarationKind::AliasDeclaration:
  422. case DeclarationKind::ExtendBaseDeclaration:
  423. // do nothing
  424. break;
  425. case DeclarationKind::ClassDeclaration:
  426. return ResolveUnformed(trace_stream,
  427. cast<ClassDeclaration>(declaration)->members());
  428. case DeclarationKind::MixinDeclaration:
  429. return ResolveUnformed(trace_stream,
  430. cast<MixinDeclaration>(declaration)->members());
  431. case DeclarationKind::InterfaceDeclaration:
  432. case DeclarationKind::ConstraintDeclaration:
  433. return ResolveUnformed(
  434. trace_stream,
  435. cast<ConstraintTypeDeclaration>(declaration)->members());
  436. case DeclarationKind::ImplDeclaration:
  437. return ResolveUnformed(trace_stream,
  438. cast<ImplDeclaration>(declaration)->members());
  439. }
  440. return Success();
  441. }
  442. auto ResolveUnformed(Nonnull<TraceStream*> trace_stream, const AST& ast)
  443. -> ErrorOr<Success> {
  444. for (auto* declaration : ast.declarations) {
  445. CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, declaration));
  446. }
  447. return Success();
  448. }
  449. } // namespace Carbon