ast_node.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_AST_NODE_H_
  5. #define CARBON_EXPLORER_AST_AST_NODE_H_
  6. #include "explorer/ast/ast_rtti.h"
  7. #include "explorer/base/source_location.h"
  8. #include "llvm/Support/Casting.h"
  9. namespace Carbon {
  10. class CloneContext;
  11. // Base class for all nodes in the AST.
  12. //
  13. // Every class derived from this class must be listed in ast_kinds.h. As a
  14. // result, every abstract class `Foo` will have a `FooKind` enumerated type,
  15. // whose enumerators correspond to the subclasses of `Foo`.
  16. //
  17. // AstNode and its derived classes support LLVM-style RTTI, including
  18. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  19. // class derived from Declaration must provide a `classof` operation, with
  20. // the following form, where `Foo` is the name of the derived class:
  21. //
  22. // static auto classof(const AstNode* node) -> bool {
  23. // return InheritsFromFoo(node->kind());
  24. // }
  25. //
  26. // Furthermore, if the class is abstract, it must provide a `kind()` operation,
  27. // with the following form:
  28. //
  29. // auto kind() const -> FooKind { return static_cast<FooKind>(root_kind()); }
  30. //
  31. // The definitions of `InheritsFromFoo` and `FooKind` are generated from
  32. // ast_rtti.h, and are implicitly provided by this header.
  33. //
  34. // Every AST node is expected to provide a cloning constructor:
  35. //
  36. // explicit MyAstNode(CloneContext& context, const MyAstNode& other);
  37. //
  38. // The cloning constructor should behave like a copy constructor, but pointers
  39. // to other AST nodes should be passed through context.Clone to clone the
  40. // referenced object.
  41. //
  42. // TODO: To support generic traversal, add children() method, and ensure that
  43. // all AstNodes are reachable from a root AstNode.
  44. class AstNode : public Printable<AstNode> {
  45. public:
  46. AstNode(AstNode&&) = delete;
  47. auto operator=(AstNode&&) -> AstNode& = delete;
  48. virtual ~AstNode() = 0;
  49. // Print the AST rooted at the node.
  50. virtual void Print(llvm::raw_ostream& out) const = 0;
  51. // Print identifying information about the node, such as its name.
  52. virtual void PrintID(llvm::raw_ostream& out) const = 0;
  53. // Returns an enumerator specifying the concrete type of this node.
  54. //
  55. // Abstract subclasses of AstNode will provide their own `kind()` method
  56. // which hides this one, and provides a narrower return type.
  57. auto kind() const -> AstNodeKind { return kind_; }
  58. // The location of the code described by this node.
  59. auto source_loc() const -> SourceLocation { return source_loc_; }
  60. protected:
  61. // Constructs an AstNode representing code at the given location. `kind`
  62. // must be the enumerator that exactly matches the concrete type being
  63. // constructed.
  64. explicit AstNode(AstNodeKind kind, SourceLocation source_loc)
  65. : kind_(kind), source_loc_(source_loc) {}
  66. // Clone this AstNode.
  67. explicit AstNode(CloneContext& /*context*/, const AstNode& other)
  68. : kind_(other.kind_), source_loc_(other.source_loc_) {}
  69. // Equivalent to kind(), but will not be hidden by `kind()` methods of
  70. // derived classes.
  71. auto root_kind() const -> AstNodeKind { return kind_; }
  72. private:
  73. AstNodeKind kind_;
  74. SourceLocation source_loc_;
  75. };
  76. } // namespace Carbon
  77. #endif // CARBON_EXPLORER_AST_AST_NODE_H_