resolve_unformed.cpp 13 KB

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