static_scope.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <functional>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <variant>
  10. #include <vector>
  11. #include "executable_semantics/ast/ast_node.h"
  12. #include "executable_semantics/ast/source_location.h"
  13. #include "executable_semantics/ast/value_category.h"
  14. #include "executable_semantics/common/nonnull.h"
  15. namespace Carbon {
  16. class Value;
  17. // True if NodeType::ImplementsCarbonNamedEntity is valid and names a type,
  18. // indicating that NodeType implements the NamedEntity interface, which means
  19. // it must define the following methods, with contracts as documented.
  20. #if 0
  21. // Returns the static type of an IdentifierExpression that names *this.
  22. auto static_type() const -> const Value&;
  23. // Returns the value category of an IdentifierExpression that names *this.
  24. auto value_category() const -> ValueCategory;
  25. #endif
  26. // NodeType must be derived from AstNode.
  27. //
  28. // TODO: consider turning the above documentation into real code, as sketched
  29. // at https://godbolt.org/z/186oEozhc
  30. template <typename T, typename = void>
  31. static constexpr bool ImplementsNamedEntity = false;
  32. template <typename T>
  33. static constexpr bool
  34. ImplementsNamedEntity<T, typename T::ImplementsCarbonNamedEntity> = true;
  35. // Non-owning type-erased wrapper around a const NodeType* `node`, where
  36. // NodeType implements the NamedEntity interface.
  37. class NamedEntityView {
  38. public:
  39. template <typename NodeType,
  40. typename = std::enable_if_t<ImplementsNamedEntity<NodeType>>>
  41. // NOLINTNEXTLINE(google-explicit-constructor)
  42. NamedEntityView(Nonnull<const NodeType*> node)
  43. // Type-erase NodeType, retaining a pointer to the base class AstNode
  44. // and using std::function to encapsulate the ability to call
  45. // the derived class's methods.
  46. : base_(node),
  47. static_type_([](const AstNode& base) -> const Value& {
  48. return llvm::cast<NodeType>(base).static_type();
  49. }),
  50. value_category_([](const AstNode& base) -> ValueCategory {
  51. return llvm::cast<NodeType>(base).value_category();
  52. }) {}
  53. NamedEntityView(const NamedEntityView&) = default;
  54. NamedEntityView(NamedEntityView&&) = default;
  55. auto operator=(const NamedEntityView&) -> NamedEntityView& = default;
  56. auto operator=(NamedEntityView&&) -> NamedEntityView& = default;
  57. // Returns `node` as an instance of the base class AstNode.
  58. auto base() const -> const AstNode& { return *base_; }
  59. // Returns node->static_type()
  60. auto static_type() const -> const Value& { return static_type_(*base_); }
  61. // Returns node->value_category()
  62. auto value_category() const -> ValueCategory {
  63. return value_category_(*base_);
  64. }
  65. friend auto operator==(const NamedEntityView& lhs,
  66. const NamedEntityView& rhs) {
  67. return lhs.base_ == rhs.base_;
  68. }
  69. friend auto operator!=(const NamedEntityView& lhs,
  70. const NamedEntityView& rhs) {
  71. return lhs.base_ != rhs.base_;
  72. }
  73. private:
  74. Nonnull<const AstNode*> base_;
  75. std::function<const Value&(const AstNode&)> static_type_;
  76. std::function<ValueCategory(const AstNode&)> value_category_;
  77. };
  78. // Maps the names visible in a given scope to the entities they name.
  79. // A scope may have parent scopes, whose names will also be visible in the
  80. // child scope.
  81. class StaticScope {
  82. public:
  83. // Defines `name` to be `entity` in this scope, or reports a compilation error
  84. // if `name` is already defined to be a different entity in this scope.
  85. void Add(std::string name, NamedEntityView entity);
  86. // Make `parent` a parent of this scope.
  87. // REQUIRES: `parent` is not already a parent of this scope.
  88. void AddParent(Nonnull<StaticScope*> parent) {
  89. parent_scopes_.push_back(parent);
  90. }
  91. // Returns the nearest definition of `name` in the ancestor graph of this
  92. // scope, or reports a compilation error at `source_loc` there isn't exactly
  93. // one such definition.
  94. auto Resolve(const std::string& name, SourceLocation source_loc) const
  95. -> NamedEntityView;
  96. private:
  97. // Equivalent to Resolve, but returns `nullopt` instead of raising an error
  98. // if no definition can be found. Still raises a compilation error if more
  99. // than one definition is found.
  100. auto TryResolve(const std::string& name, SourceLocation source_loc) const
  101. -> std::optional<NamedEntityView>;
  102. // Maps locally declared names to their entities.
  103. std::unordered_map<std::string, NamedEntityView> declared_names_;
  104. // A list of scopes used for name lookup within this scope.
  105. std::vector<Nonnull<StaticScope*>> parent_scopes_;
  106. };
  107. } // namespace Carbon
  108. #endif // EXECUTABLE_SEMANTICS_AST_STATIC_SCOPE_H_