resolve_unformed.cpp 19 KB

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