resolve_unformed.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #ifndef CARBON_EXPLORER_INTERPRETER_RESOLVE_UNFORMED_H_
  5. #define CARBON_EXPLORER_INTERPRETER_RESOLVE_UNFORMED_H_
  6. #include <string>
  7. #include <unordered_map>
  8. #include "explorer/ast/ast.h"
  9. #include "explorer/common/nonnull.h"
  10. namespace Carbon {
  11. // Maps AST nodes to flow facts within a function.
  12. class FlowFacts {
  13. public:
  14. enum class ActionType {
  15. // Adds a must-be-formed flow fact.
  16. // Used at `VariableDefinition` with initialization.
  17. AddInit,
  18. // Adds an unformed flow fact.
  19. // Used at `VariableDefinition` without initialization.
  20. AddUninit,
  21. // Marks an unformed flow fact as may-be-formed.
  22. // Used at AST nodes that potentially initializes a variable.
  23. Form,
  24. // Returns compilation error if the AST node is impossible to be formed.
  25. // Used at AST nodes that uses a variable.
  26. Check,
  27. // Used in traversing children nodes without an acion to take.
  28. None,
  29. };
  30. // Take action on flow facts based on `ActionType`.
  31. auto TakeAction(Nonnull<const AstNode*> node, ActionType action,
  32. SourceLocation source_loc, const std::string& name)
  33. -> ErrorOr<Success>;
  34. private:
  35. enum class FormedState {
  36. MustBeFormed,
  37. MayBeFormed,
  38. Unformed,
  39. };
  40. // Aggregate information about a AstNode being analyzed.
  41. struct Fact {
  42. FormedState formed_state;
  43. };
  44. void AddFact(Nonnull<const AstNode*> node, const FormedState state) {
  45. CARBON_CHECK(facts_.find(node) == facts_.end());
  46. facts_.insert({node, {state}});
  47. }
  48. std::unordered_map<Nonnull<const AstNode*>, Fact> facts_;
  49. };
  50. // An intraprocedural forward analysis that checks the may-be-formed states on
  51. // local variables. Returns compilation error on usage of must-be-unformed
  52. // variables.
  53. auto ResolveUnformed(const AST& ast) -> ErrorOr<Success>;
  54. } // namespace Carbon
  55. #endif // CARBON_EXPLORER_INTERPRETER_RESOLVE_UNFORMED_H_