ast_node.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // When inheriting from this class, the inheritance must me marked `virtual`.
  35. //
  36. // TODO: To support generic traversal, add children() method, and ensure that
  37. // all AstNodes are reachable from a root AstNode.
  38. class AstNode {
  39. public:
  40. AstNode(AstNode&&) = delete;
  41. auto operator=(AstNode&&) -> AstNode& = delete;
  42. virtual ~AstNode() = 0;
  43. virtual void Print(llvm::raw_ostream& out) const = 0;
  44. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  45. // Returns an enumerator specifying the concrete type of this node.
  46. //
  47. // Abstract subclasses of AstNode will provide their own `kind()` method
  48. // which hides this one, and provides a narrower return type.
  49. auto kind() const -> AstNodeKind { return kind_; }
  50. // The location of the code described by this node.
  51. auto source_loc() const -> SourceLocation { return source_loc_; }
  52. protected:
  53. // Constructs an AstNode representing code at the given location. `kind`
  54. // must be the enumerator that exactly matches the concrete type being
  55. // constructed.
  56. explicit AstNode(AstNodeKind kind, SourceLocation source_loc)
  57. : kind_(kind), source_loc_(source_loc) {}
  58. // Equivalent to kind(), but will not be hidden by `kind()` methods of
  59. // derived classes.
  60. auto root_kind() const -> AstNodeKind { return kind_; }
  61. private:
  62. AstNodeKind kind_;
  63. SourceLocation source_loc_;
  64. };
  65. } // namespace Carbon
  66. // Ensure that LLVM casts from AstNode use dynamic_cast, because static_cast
  67. // doesn't work with a virtual base class.
  68. namespace llvm {
  69. template <typename To>
  70. struct cast_convert_val<To, const Carbon::AstNode*, const Carbon::AstNode*> {
  71. using ResultType = typename cast_retty<To, const Carbon::AstNode*>::ret_type;
  72. static ResultType doit(const Carbon::AstNode* node) {
  73. return dynamic_cast<ResultType>(node);
  74. }
  75. };
  76. template <typename To>
  77. struct cast_convert_val<To, Carbon::AstNode*, Carbon::AstNode*> {
  78. using ResultType = typename cast_retty<To, Carbon::AstNode*>::ret_type;
  79. static ResultType doit(Carbon::AstNode* node) {
  80. return dynamic_cast<ResultType>(node);
  81. }
  82. };
  83. template <typename To>
  84. struct cast_convert_val<To, const Carbon::AstNode, const Carbon::AstNode> {
  85. using ResultType = typename cast_retty<To, const Carbon::AstNode>::ret_type;
  86. static ResultType doit(const Carbon::AstNode& node) {
  87. return dynamic_cast<ResultType>(node);
  88. }
  89. };
  90. template <typename To>
  91. struct cast_convert_val<To, Carbon::AstNode, Carbon::AstNode> {
  92. using ResultType = typename cast_retty<To, Carbon::AstNode>::ret_type;
  93. static ResultType doit(Carbon::AstNode& node) {
  94. return dynamic_cast<ResultType>(node);
  95. }
  96. };
  97. } // namespace llvm
  98. #endif // EXECUTABLE_SEMANTICS_AST_AST_NODE_H_