ast_node.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include "llvm/Support/Casting.h"
  9. namespace Carbon {
  10. // Base class for all nodes in the AST.
  11. //
  12. // Every class derived from this class must be listed in ast_rtti.txt. See
  13. // the documentation of gen_rtti.py for details about the format. As a result,
  14. // every abstract class `Foo` will have a `FooKind` enumerated type, whose
  15. // 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.txt, and are implicitly provided by this header.
  33. //
  34. // TODO: To support generic traversal, add children() method, and ensure that
  35. // all AstNodes are reachable from a root AstNode.
  36. class AstNode {
  37. public:
  38. AstNode(AstNode&&) = delete;
  39. auto operator=(AstNode&&) -> AstNode& = delete;
  40. virtual ~AstNode() = 0;
  41. virtual void Print(llvm::raw_ostream& out) const = 0;
  42. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  43. // Returns an enumerator specifying the concrete type of this node.
  44. //
  45. // Abstract subclasses of AstNode will provide their own `kind()` method
  46. // which hides this one, and provides a narrower return type.
  47. auto kind() const -> AstNodeKind { return kind_; }
  48. // The location of the code described by this node.
  49. auto source_loc() const -> SourceLocation { return source_loc_; }
  50. protected:
  51. // Constructs an AstNode representing code at the given location. `kind`
  52. // must be the enumerator that exactly matches the concrete type being
  53. // constructed.
  54. explicit AstNode(AstNodeKind kind, SourceLocation source_loc)
  55. : kind_(kind), source_loc_(source_loc) {}
  56. // Equivalent to kind(), but will not be hidden by `kind()` methods of
  57. // derived classes.
  58. auto root_kind() const -> AstNodeKind { return kind_; }
  59. private:
  60. AstNodeKind kind_;
  61. SourceLocation source_loc_;
  62. };
  63. } // namespace Carbon
  64. #endif // EXECUTABLE_SEMANTICS_AST_AST_NODE_H_