static_scope.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 EXECUTABLE_SEMANTICS_AST_STATIC_SCOPE_H_
  5. #define EXECUTABLE_SEMANTICS_AST_STATIC_SCOPE_H_
  6. #include <string>
  7. #include <unordered_map>
  8. #include <variant>
  9. #include <vector>
  10. #include "executable_semantics/ast/source_location.h"
  11. #include "executable_semantics/common/nonnull.h"
  12. namespace Carbon {
  13. class NamedEntityInterface {
  14. public:
  15. enum class NamedEntityKind {
  16. // Includes variable definitions and matching contexts.
  17. BindingPattern,
  18. // Used by entries in choices.
  19. ChoiceDeclarationAlternative,
  20. // Used by continuations.
  21. Continuation,
  22. // Includes choices, classes, and functions. Variables are handled through
  23. // BindingPattern.
  24. Declaration,
  25. // Used by functions.
  26. GenericBinding,
  27. // Used by entries in classes.
  28. Member,
  29. };
  30. virtual ~NamedEntityInterface() = default;
  31. // TODO: This is unused, but is intended for casts after lookup.
  32. virtual auto named_entity_kind() const -> NamedEntityKind = 0;
  33. virtual auto source_loc() const -> SourceLocation = 0;
  34. };
  35. // The set of declared names in a scope. This is not aware of child scopes, but
  36. // does include directions to parent or related scopes for lookup purposes.
  37. class StaticScope {
  38. public:
  39. void Add(std::string name, Nonnull<const NamedEntityInterface*> entity);
  40. private:
  41. // Maps locally declared names to their entities.
  42. std::unordered_map<std::string, Nonnull<const NamedEntityInterface*>>
  43. declared_names_;
  44. // A list of scopes used for name lookup within this scope.
  45. // TODO: This is unused, but is intended for name lookup cross-scope.
  46. std::vector<Nonnull<StaticScope*>> parent_scopes_;
  47. };
  48. } // namespace Carbon
  49. #endif // EXECUTABLE_SEMANTICS_AST_STATIC_SCOPE_H_