static_scope.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. NamedEntityInterface() = default;
  31. virtual ~NamedEntityInterface() = default;
  32. NamedEntityInterface(NamedEntityInterface&&) = delete;
  33. auto operator=(NamedEntityInterface&&) -> NamedEntityInterface& = delete;
  34. // TODO: This is unused, but is intended for casts after lookup.
  35. virtual auto named_entity_kind() const -> NamedEntityKind = 0;
  36. virtual auto source_loc() const -> SourceLocation = 0;
  37. };
  38. // The set of declared names in a scope. This is not aware of child scopes, but
  39. // does include directions to parent or related scopes for lookup purposes.
  40. class StaticScope {
  41. public:
  42. void Add(std::string name, Nonnull<const NamedEntityInterface*> entity);
  43. private:
  44. // Maps locally declared names to their entities.
  45. std::unordered_map<std::string, Nonnull<const NamedEntityInterface*>>
  46. declared_names_;
  47. // A list of scopes used for name lookup within this scope.
  48. // TODO: This is unused, but is intended for name lookup cross-scope.
  49. std::vector<Nonnull<StaticScope*>> parent_scopes_;
  50. };
  51. } // namespace Carbon
  52. #endif // EXECUTABLE_SEMANTICS_AST_STATIC_SCOPE_H_