value_node.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 CARBON_EXPLORER_AST_VALUE_NODE_H_
  5. #define CARBON_EXPLORER_AST_VALUE_NODE_H_
  6. #include <functional>
  7. #include <optional>
  8. #include <string_view>
  9. #include "explorer/ast/ast_node.h"
  10. #include "explorer/ast/value_category.h"
  11. #include "explorer/common/nonnull.h"
  12. namespace Carbon {
  13. class Value;
  14. // The placeholder name exposed by anonymous ValueNodes.
  15. static constexpr std::string_view AnonymousName = "_";
  16. // ImplementsValueNode is true if NodeType::ImplementsCarbonValueNode
  17. // is valid and names a type, indicating that NodeType implements the
  18. // ValueNode interface, defined below.
  19. template <typename NodeType, typename = void>
  20. static constexpr bool ImplementsValueNode = false;
  21. // ValueNode is an interface implemented by AstNodes that can be associated
  22. // with a value, such as declarations and bindings. The interface consists of
  23. // the following methods:
  24. //
  25. // // Returns the constant associated with the node.
  26. // // This is called by the interpreter, not the type checker.
  27. // auto constant_value() const -> std::optional<Nonnull<const Value*>>;
  28. //
  29. // // Returns the symbolic compile-time identity of the node.
  30. // // This is called by the type checker, not the interpreter.
  31. // auto symbolic_identity() const -> std::optional<Nonnull<const Value*>>;
  32. //
  33. // // Returns the static type of an IdentifierExpression that names *this.
  34. // auto static_type() const -> const Value&;
  35. //
  36. // // Returns the value category of an IdentifierExpression that names *this.
  37. // auto value_category() const -> ValueCategory;
  38. //
  39. // // Print the node's identity (e.g. its name).
  40. // void PrintID(llvm::raw_ostream& out) const;
  41. //
  42. // TODO: consider turning the above documentation into real code, as sketched
  43. // at https://godbolt.org/z/186oEozhc
  44. template <typename T>
  45. static constexpr bool
  46. ImplementsValueNode<T, typename T::ImplementsCarbonValueNode> = true;
  47. class ValueNodeView {
  48. public:
  49. template <typename NodeType,
  50. typename = std::enable_if_t<ImplementsValueNode<NodeType>>>
  51. // NOLINTNEXTLINE(google-explicit-constructor)
  52. ValueNodeView(Nonnull<const NodeType*> node)
  53. // Type-erase NodeType, retaining a pointer to the base class AstNode
  54. // and using std::function to encapsulate the ability to call
  55. // the derived class's methods.
  56. : base_(node),
  57. constant_value_(
  58. [](const AstNode& base) -> std::optional<Nonnull<const Value*>> {
  59. return llvm::cast<NodeType>(base).constant_value();
  60. }),
  61. symbolic_identity_(
  62. [](const AstNode& base) -> std::optional<Nonnull<const Value*>> {
  63. return llvm::cast<NodeType>(base).symbolic_identity();
  64. }),
  65. print_([](const AstNode& base, llvm::raw_ostream& out) -> void {
  66. // TODO: change this to print a summary of the node
  67. return llvm::cast<NodeType>(base).PrintID(out);
  68. }),
  69. static_type_([](const AstNode& base) -> const Value& {
  70. return llvm::cast<NodeType>(base).static_type();
  71. }),
  72. value_category_([](const AstNode& base) -> ValueCategory {
  73. return llvm::cast<NodeType>(base).value_category();
  74. }) {}
  75. ValueNodeView(const ValueNodeView&) = default;
  76. ValueNodeView(ValueNodeView&&) = default;
  77. auto operator=(const ValueNodeView&) -> ValueNodeView& = default;
  78. auto operator=(ValueNodeView&&) -> ValueNodeView& = default;
  79. // Returns `node` as an instance of the base class AstNode.
  80. auto base() const -> const AstNode& { return *base_; }
  81. // Returns node->constant_value()
  82. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  83. return constant_value_(*base_);
  84. }
  85. // Returns node->symbolic_identity()
  86. auto symbolic_identity() const -> std::optional<Nonnull<const Value*>> {
  87. return symbolic_identity_(*base_);
  88. }
  89. void Print(llvm::raw_ostream& out) const { print_(*base_, out); }
  90. // Returns node->static_type()
  91. auto static_type() const -> const Value& { return static_type_(*base_); }
  92. // Returns node->value_category()
  93. auto value_category() const -> ValueCategory {
  94. return value_category_(*base_);
  95. }
  96. friend auto operator==(const ValueNodeView& lhs, const ValueNodeView& rhs)
  97. -> bool {
  98. return lhs.base_ == rhs.base_;
  99. }
  100. friend auto operator!=(const ValueNodeView& lhs, const ValueNodeView& rhs)
  101. -> bool {
  102. return lhs.base_ != rhs.base_;
  103. }
  104. friend auto operator<(const ValueNodeView& lhs, const ValueNodeView& rhs)
  105. -> bool {
  106. return std::less<>()(lhs.base_, rhs.base_);
  107. }
  108. private:
  109. Nonnull<const AstNode*> base_;
  110. std::function<std::optional<Nonnull<const Value*>>(const AstNode&)>
  111. constant_value_;
  112. std::function<std::optional<Nonnull<const Value*>>(const AstNode&)>
  113. symbolic_identity_;
  114. std::function<void(const AstNode&, llvm::raw_ostream&)> print_;
  115. std::function<const Value&(const AstNode&)> static_type_;
  116. std::function<ValueCategory(const AstNode&)> value_category_;
  117. };
  118. } // namespace Carbon
  119. #endif // CARBON_EXPLORER_AST_VALUE_NODE_H_