static_scope.h 5.9 KB

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