static_scope.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/ast_node.h"
  11. #include "executable_semantics/ast/source_location.h"
  12. #include "executable_semantics/common/nonnull.h"
  13. namespace Carbon {
  14. class NamedEntity : public virtual AstNode {
  15. public:
  16. virtual ~NamedEntity() = 0;
  17. NamedEntity() = default;
  18. // TODO: This is unused, but is intended for casts after lookup.
  19. auto kind() const -> NamedEntityKind {
  20. return static_cast<NamedEntityKind>(root_kind());
  21. }
  22. };
  23. // The set of declared names in a scope. This is not aware of child scopes, but
  24. // does include directions to parent or related scopes for lookup purposes.
  25. class StaticScope {
  26. public:
  27. void Add(std::string name, Nonnull<const NamedEntity*> entity);
  28. private:
  29. // Maps locally declared names to their entities.
  30. std::unordered_map<std::string, Nonnull<const NamedEntity*>> declared_names_;
  31. // A list of scopes used for name lookup within this scope.
  32. // TODO: This is unused, but is intended for name lookup cross-scope.
  33. std::vector<Nonnull<StaticScope*>> parent_scopes_;
  34. };
  35. } // namespace Carbon
  36. #endif // EXECUTABLE_SEMANTICS_AST_STATIC_SCOPE_H_