ast_node.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_AST_NODE_H_
  5. #define EXECUTABLE_SEMANTICS_AST_AST_NODE_H_
  6. #include "executable_semantics/ast/ast_rtti.h"
  7. #include "executable_semantics/ast/source_location.h"
  8. namespace Carbon {
  9. // Base class for all nodes in the AST.
  10. //
  11. // Every class derived from this class must be listed in ast_rtti.txt. See
  12. // the documentation of gen_rtti.py for details about the format. As a result,
  13. // every abstract class `Foo` will have a `FooKind` enumerated type, whose
  14. // enumerators correspond to the subclasses of `Foo`.
  15. //
  16. // AstNode and its derived classes support LLVM-style RTTI, including
  17. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  18. // class derived from Declaration must provide a `classof` operation, with
  19. // the following form, where `Foo` is the name of the derived class:
  20. //
  21. // static auto classof(const AstNode* node) -> bool {
  22. // return InheritsFromFoo(node->kind());
  23. // }
  24. //
  25. // Furthermore, if the class is abstract, it must provide a `kind()` operation,
  26. // with the following form:
  27. //
  28. // auto kind() const -> FooKind { return static_cast<FooKind>(root_kind()); }
  29. //
  30. // The definitions of `InheritsFromFoo` and `FooKind` are generated from
  31. // ast_rtti.txt, and are implicitly provided by this header.
  32. //
  33. // When inheriting from this class, the inheritance must me marked `virtual`.
  34. //
  35. // TODO: To support generic traversal, add children() method, and ensure that
  36. // all AstNodes are reachable from a root AstNode.
  37. class AstNode {
  38. public:
  39. AstNode(AstNode&&) = delete;
  40. auto operator=(AstNode&&) -> AstNode& = delete;
  41. virtual ~AstNode() = 0;
  42. // Returns an enumerator specifying the concrete type of this node.
  43. //
  44. // Abstract subclasses of AstNode will provide their own `kind()` method
  45. // which hides this one, and provides a narrower return type.
  46. auto kind() const -> AstNodeKind { return kind_; }
  47. // The location of the code described by this node.
  48. auto source_loc() const -> SourceLocation { return source_loc_; }
  49. protected:
  50. // Constructs an AstNode representing code at the given location. `kind`
  51. // must be the enumerator that exactly matches the concrete type being
  52. // constructed.
  53. explicit AstNode(AstNodeKind kind, SourceLocation source_loc)
  54. : kind_(kind), source_loc_(source_loc) {}
  55. // Equivalent to kind(), but will not be hidden by `kind()` methods of
  56. // derived classes.
  57. auto root_kind() const -> AstNodeKind { return kind_; }
  58. private:
  59. AstNodeKind kind_;
  60. SourceLocation source_loc_;
  61. };
  62. } // namespace Carbon
  63. #endif // EXECUTABLE_SEMANTICS_AST_AST_NODE_H_