resolve_unformed.cpp 13 KB

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