static_scope.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "common/check.h"
  12. #include "executable_semantics/ast/ast_node.h"
  13. #include "executable_semantics/ast/source_location.h"
  14. #include "executable_semantics/ast/value_category.h"
  15. #include "executable_semantics/common/nonnull.h"
  16. namespace Carbon {
  17. class Value;
  18. // The placeholder name exposed by anonymous ValueNodes.
  19. static constexpr std::string_view AnonymousName = "_";
  20. // ImplementsValueNode is true if NodeType::ImplementsCarbonValueNode
  21. // is valid and names a type, indicating that NodeType implements the
  22. // ValueNode interface, defined below.
  23. template <typename NodeType, typename = void>
  24. static constexpr bool ImplementsValueNode = false;
  25. /*
  26. ValueNode is an interface implemented by AstNodes that can be associated
  27. with a value, such as declarations and bindings. The interface consists of
  28. the following methods:
  29. // Returns the static type of an IdentifierExpression that names *this.
  30. auto static_type() const -> const Value&;
  31. // Returns the value category of an IdentifierExpression that names *this.
  32. auto value_category() const -> ValueCategory;
  33. // Print the node for diagnostic or tracing purposes.
  34. void Print(llvm::raw_ostream& out) const;
  35. */
  36. // TODO: consider turning the above documentation into real code, as sketched
  37. // at https://godbolt.org/z/186oEozhc
  38. template <typename T>
  39. static constexpr bool
  40. ImplementsValueNode<T, typename T::ImplementsCarbonValueNode> = true;
  41. class ValueNodeView {
  42. public:
  43. template <typename NodeType,
  44. typename = std::enable_if_t<ImplementsValueNode<NodeType>>>
  45. // NOLINTNEXTLINE(google-explicit-constructor)
  46. ValueNodeView(Nonnull<const NodeType*> node)
  47. // Type-erase NodeType, retaining a pointer to the base class AstNode
  48. // and using std::function to encapsulate the ability to call
  49. // the derived class's methods.
  50. : base_(node),
  51. constant_value_(
  52. [](const AstNode& base) -> std::optional<Nonnull<const Value*>> {
  53. return llvm::cast<NodeType>(base).constant_value();
  54. }),
  55. print_([](const AstNode& base, llvm::raw_ostream& out) -> void {
  56. // TODO: change this to print a summary of the node
  57. return llvm::cast<NodeType>(base).Print(out);
  58. }),
  59. static_type_([](const AstNode& base) -> const Value& {
  60. return llvm::cast<NodeType>(base).static_type();
  61. }),
  62. value_category_([](const AstNode& base) -> ValueCategory {
  63. return llvm::cast<NodeType>(base).value_category();
  64. }) {}
  65. ValueNodeView(const ValueNodeView&) = default;
  66. ValueNodeView(ValueNodeView&&) = default;
  67. auto operator=(const ValueNodeView&) -> ValueNodeView& = default;
  68. auto operator=(ValueNodeView&&) -> ValueNodeView& = default;
  69. // Returns `node` as an instance of the base class AstNode.
  70. auto base() const -> const AstNode& { return *base_; }
  71. // Returns node->constant_value()
  72. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  73. return constant_value_(*base_);
  74. }
  75. void Print(llvm::raw_ostream& out) const { print_(*base_, out); }
  76. // Returns node->static_type()
  77. auto static_type() const -> const Value& { return static_type_(*base_); }
  78. // Returns node->value_category()
  79. auto value_category() const -> ValueCategory {
  80. return value_category_(*base_);
  81. }
  82. friend auto operator==(const ValueNodeView& lhs, const ValueNodeView& rhs)
  83. -> bool {
  84. return lhs.base_ == rhs.base_;
  85. }
  86. friend auto operator!=(const ValueNodeView& lhs, const ValueNodeView& rhs)
  87. -> bool {
  88. return lhs.base_ != rhs.base_;
  89. }
  90. friend auto operator<(const ValueNodeView& lhs, const ValueNodeView& rhs)
  91. -> bool {
  92. return std::less<>()(lhs.base_, rhs.base_);
  93. }
  94. private:
  95. Nonnull<const AstNode*> base_;
  96. std::function<std::optional<Nonnull<const Value*>>(const AstNode&)>
  97. constant_value_;
  98. std::function<void(const AstNode&, llvm::raw_ostream&)> print_;
  99. std::function<const Value&(const AstNode&)> static_type_;
  100. std::function<ValueCategory(const AstNode&)> value_category_;
  101. };
  102. // Maps the names visible in a given scope to the entities they name.
  103. // A scope may have parent scopes, whose names will also be visible in the
  104. // child scope.
  105. class StaticScope {
  106. public:
  107. // Defines `name` to be `entity` in this scope, or reports a compilation error
  108. // if `name` is already defined to be a different entity in this scope.
  109. void Add(std::string name, ValueNodeView entity);
  110. // Make `parent` a parent of this scope.
  111. // REQUIRES: `parent` is not already a parent of this scope.
  112. void AddParent(Nonnull<StaticScope*> parent) {
  113. parent_scopes_.push_back(parent);
  114. }
  115. // Returns the nearest definition of `name` in the ancestor graph of this
  116. // scope, or reports a compilation error at `source_loc` there isn't exactly
  117. // one such definition.
  118. auto Resolve(const std::string& name, SourceLocation source_loc) const
  119. -> ValueNodeView;
  120. private:
  121. // Equivalent to Resolve, but returns `nullopt` instead of raising an error
  122. // if no definition can be found. Still raises a compilation error if more
  123. // than one definition is found.
  124. auto TryResolve(const std::string& name, SourceLocation source_loc) const
  125. -> std::optional<ValueNodeView>;
  126. // Maps locally declared names to their entities.
  127. std::unordered_map<std::string, ValueNodeView> declared_names_;
  128. // A list of scopes used for name lookup within this scope.
  129. std::vector<Nonnull<StaticScope*>> parent_scopes_;
  130. };
  131. } // namespace Carbon
  132. #endif // EXECUTABLE_SEMANTICS_AST_STATIC_SCOPE_H_