resolve_unformed.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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::DotSelfExpression:
  117. case ExpressionKind::IntLiteral:
  118. case ExpressionKind::BoolLiteral:
  119. case ExpressionKind::BoolTypeLiteral:
  120. case ExpressionKind::IntTypeLiteral:
  121. case ExpressionKind::StringLiteral:
  122. case ExpressionKind::StringTypeLiteral:
  123. case ExpressionKind::TypeTypeLiteral:
  124. case ExpressionKind::ContinuationTypeLiteral:
  125. case ExpressionKind::ValueLiteral:
  126. case ExpressionKind::IndexExpression:
  127. case ExpressionKind::CompoundMemberAccessExpression:
  128. case ExpressionKind::IfExpression:
  129. case ExpressionKind::WhereExpression:
  130. case ExpressionKind::StructTypeLiteral:
  131. case ExpressionKind::IntrinsicExpression:
  132. case ExpressionKind::UnimplementedExpression:
  133. case ExpressionKind::FunctionTypeLiteral:
  134. case ExpressionKind::ArrayTypeLiteral:
  135. break;
  136. }
  137. return Success();
  138. }
  139. static auto ResolveUnformed(Nonnull<const Pattern*> pattern,
  140. FlowFacts& flow_facts, FlowFacts::ActionType action)
  141. -> ErrorOr<Success> {
  142. switch (pattern->kind()) {
  143. case PatternKind::BindingPattern: {
  144. const auto& binding_pattern = cast<BindingPattern>(*pattern);
  145. CARBON_RETURN_IF_ERROR(flow_facts.TakeAction(&binding_pattern, action,
  146. binding_pattern.source_loc(),
  147. binding_pattern.name()));
  148. } break;
  149. case PatternKind::TuplePattern:
  150. for (Nonnull<const Pattern*> field :
  151. cast<TuplePattern>(*pattern).fields()) {
  152. CARBON_RETURN_IF_ERROR(ResolveUnformed(field, flow_facts, action));
  153. }
  154. break;
  155. case PatternKind::GenericBinding:
  156. case PatternKind::AlternativePattern:
  157. case PatternKind::ExpressionPattern:
  158. case PatternKind::AutoPattern:
  159. case PatternKind::VarPattern:
  160. case PatternKind::AddrPattern:
  161. // do nothing
  162. break;
  163. }
  164. return Success();
  165. }
  166. static auto ResolveUnformed(Nonnull<const Statement*> statement,
  167. FlowFacts& flow_facts, FlowFacts::ActionType action)
  168. -> ErrorOr<Success> {
  169. switch (statement->kind()) {
  170. case StatementKind::Block: {
  171. const auto& block = cast<Block>(*statement);
  172. for (const auto* block_statement : block.statements()) {
  173. CARBON_RETURN_IF_ERROR(
  174. ResolveUnformed(block_statement, flow_facts, action));
  175. }
  176. break;
  177. }
  178. case StatementKind::VariableDefinition: {
  179. const auto& def = cast<VariableDefinition>(*statement);
  180. if (def.has_init()) {
  181. CARBON_RETURN_IF_ERROR(ResolveUnformed(&def.pattern(), flow_facts,
  182. FlowFacts::ActionType::AddInit));
  183. CARBON_RETURN_IF_ERROR(ResolveUnformed(&def.init(), flow_facts,
  184. FlowFacts::ActionType::Check));
  185. } else {
  186. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  187. &def.pattern(), flow_facts, FlowFacts::ActionType::AddUninit));
  188. }
  189. break;
  190. }
  191. case StatementKind::ReturnVar: {
  192. const auto& ret_var = cast<ReturnVar>(*statement);
  193. const auto& binding_pattern =
  194. cast<BindingPattern>(ret_var.value_node().base());
  195. CARBON_RETURN_IF_ERROR(
  196. flow_facts.TakeAction(&binding_pattern, FlowFacts::ActionType::Check,
  197. ret_var.source_loc(), binding_pattern.name()));
  198. break;
  199. }
  200. case StatementKind::ReturnExpression: {
  201. const auto& ret_exp_stmt = cast<ReturnExpression>(*statement);
  202. CARBON_RETURN_IF_ERROR(ResolveUnformed(&ret_exp_stmt.expression(),
  203. flow_facts,
  204. FlowFacts::ActionType::Check));
  205. break;
  206. }
  207. case StatementKind::Assign: {
  208. const auto& assign = cast<Assign>(*statement);
  209. if (assign.lhs().kind() == ExpressionKind::IdentifierExpression) {
  210. CARBON_RETURN_IF_ERROR(ResolveUnformed(&assign.lhs(), flow_facts,
  211. FlowFacts::ActionType::Form));
  212. } else {
  213. // TODO: Support checking non-identifier lhs expression.
  214. CARBON_RETURN_IF_ERROR(ResolveUnformed(&assign.lhs(), flow_facts,
  215. FlowFacts::ActionType::None));
  216. }
  217. CARBON_RETURN_IF_ERROR(ResolveUnformed(&assign.rhs(), flow_facts,
  218. FlowFacts::ActionType::Check));
  219. break;
  220. }
  221. case StatementKind::ExpressionStatement: {
  222. const auto& exp_stmt = cast<ExpressionStatement>(*statement);
  223. CARBON_RETURN_IF_ERROR(
  224. ResolveUnformed(&exp_stmt.expression(), flow_facts, action));
  225. break;
  226. }
  227. case StatementKind::If: {
  228. const auto& if_stmt = cast<If>(*statement);
  229. CARBON_RETURN_IF_ERROR(ResolveUnformed(&if_stmt.condition(), flow_facts,
  230. FlowFacts::ActionType::Check));
  231. CARBON_RETURN_IF_ERROR(
  232. ResolveUnformed(&if_stmt.then_block(), flow_facts, action));
  233. if (if_stmt.else_block().has_value()) {
  234. CARBON_RETURN_IF_ERROR(
  235. ResolveUnformed(*if_stmt.else_block(), flow_facts, action));
  236. }
  237. break;
  238. }
  239. case StatementKind::While: {
  240. const auto& while_stmt = cast<While>(*statement);
  241. CARBON_RETURN_IF_ERROR(ResolveUnformed(
  242. &while_stmt.condition(), flow_facts, FlowFacts::ActionType::Check));
  243. CARBON_RETURN_IF_ERROR(
  244. ResolveUnformed(&while_stmt.body(), flow_facts, action));
  245. break;
  246. }
  247. case StatementKind::Match: {
  248. const auto& match = cast<Match>(*statement);
  249. CARBON_RETURN_IF_ERROR(ResolveUnformed(&match.expression(), flow_facts,
  250. FlowFacts::ActionType::Check));
  251. for (const auto& clause : match.clauses()) {
  252. CARBON_RETURN_IF_ERROR(ResolveUnformed(&clause.pattern(), flow_facts,
  253. FlowFacts::ActionType::Check));
  254. CARBON_RETURN_IF_ERROR(
  255. ResolveUnformed(&clause.statement(), flow_facts, action));
  256. }
  257. break;
  258. }
  259. case StatementKind::Break:
  260. case StatementKind::Continue:
  261. case StatementKind::Continuation:
  262. case StatementKind::Run:
  263. case StatementKind::Await:
  264. case StatementKind::For:
  265. // do nothing
  266. break;
  267. }
  268. return Success();
  269. }
  270. static auto ResolveUnformed(Nonnull<const Declaration*> declaration)
  271. -> ErrorOr<Success> {
  272. switch (declaration->kind()) {
  273. // Checks formed/unformed state intraprocedurally.
  274. // Can be extended to an interprocedural analysis when a call graph is
  275. // available.
  276. case DeclarationKind::FunctionDeclaration:
  277. case DeclarationKind::DestructorDeclaration: {
  278. const auto& callable = cast<CallableDeclaration>(*declaration);
  279. if (callable.body().has_value()) {
  280. FlowFacts flow_facts;
  281. CARBON_RETURN_IF_ERROR(ResolveUnformed(*callable.body(), flow_facts,
  282. FlowFacts::ActionType::None));
  283. }
  284. break;
  285. }
  286. case DeclarationKind::ClassDeclaration:
  287. case DeclarationKind::MixDeclaration:
  288. case DeclarationKind::MixinDeclaration:
  289. case DeclarationKind::InterfaceDeclaration:
  290. case DeclarationKind::ImplDeclaration:
  291. case DeclarationKind::ChoiceDeclaration:
  292. case DeclarationKind::VariableDeclaration:
  293. case DeclarationKind::InterfaceExtendsDeclaration:
  294. case DeclarationKind::InterfaceImplDeclaration:
  295. case DeclarationKind::AssociatedConstantDeclaration:
  296. case DeclarationKind::SelfDeclaration:
  297. case DeclarationKind::AliasDeclaration:
  298. // do nothing
  299. break;
  300. }
  301. return Success();
  302. }
  303. auto ResolveUnformed(const AST& ast) -> ErrorOr<Success> {
  304. for (auto* declaration : ast.declarations) {
  305. CARBON_RETURN_IF_ERROR(ResolveUnformed(declaration));
  306. }
  307. return Success();
  308. }
  309. } // namespace Carbon