resolve_unformed.cpp 16 KB

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