ASTTableGen.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. //=== ASTTableGen.h - Common definitions for AST node tablegen --*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef CLANG_AST_TABLEGEN_H
  9. #define CLANG_AST_TABLEGEN_H
  10. #include "llvm/TableGen/Record.h"
  11. #include "llvm/ADT/STLExtras.h"
  12. #include <optional>
  13. // These are spellings in the tblgen files.
  14. #define HasPropertiesClassName "HasProperties"
  15. // ASTNodes and their common fields. `Base` is actually defined
  16. // in subclasses, but it's still common across the hierarchies.
  17. #define ASTNodeClassName "ASTNode"
  18. #define BaseFieldName "Base"
  19. #define AbstractFieldName "Abstract"
  20. // Comment node hierarchy.
  21. #define CommentNodeClassName "CommentNode"
  22. // Decl node hierarchy.
  23. #define DeclNodeClassName "DeclNode"
  24. #define DeclContextNodeClassName "DeclContext"
  25. // Stmt node hierarchy.
  26. #define StmtNodeClassName "StmtNode"
  27. // Type node hierarchy.
  28. #define TypeNodeClassName "TypeNode"
  29. #define AlwaysDependentClassName "AlwaysDependent"
  30. #define NeverCanonicalClassName "NeverCanonical"
  31. #define NeverCanonicalUnlessDependentClassName "NeverCanonicalUnlessDependent"
  32. #define LeafTypeClassName "LeafType"
  33. // Cases of various non-ASTNode structured types like DeclarationName.
  34. #define TypeKindClassName "PropertyTypeKind"
  35. #define KindTypeFieldName "KindType"
  36. #define KindPropertyNameFieldName "KindPropertyName"
  37. #define TypeCaseClassName "PropertyTypeCase"
  38. // Properties of AST nodes.
  39. #define PropertyClassName "Property"
  40. #define ClassFieldName "Class"
  41. #define NameFieldName "Name"
  42. #define TypeFieldName "Type"
  43. #define ReadFieldName "Read"
  44. // Types of properties.
  45. #define PropertyTypeClassName "PropertyType"
  46. #define CXXTypeNameFieldName "CXXName"
  47. #define PassByReferenceFieldName "PassByReference"
  48. #define ConstWhenWritingFieldName "ConstWhenWriting"
  49. #define ConditionalCodeFieldName "Conditional"
  50. #define PackOptionalCodeFieldName "PackOptional"
  51. #define UnpackOptionalCodeFieldName "UnpackOptional"
  52. #define BufferElementTypesFieldName "BufferElementTypes"
  53. #define ArrayTypeClassName "Array"
  54. #define ArrayElementTypeFieldName "Element"
  55. #define OptionalTypeClassName "Optional"
  56. #define OptionalElementTypeFieldName "Element"
  57. #define SubclassPropertyTypeClassName "SubclassPropertyType"
  58. #define SubclassBaseTypeFieldName "Base"
  59. #define SubclassClassNameFieldName "SubclassName"
  60. #define EnumPropertyTypeClassName "EnumPropertyType"
  61. // Write helper rules.
  62. #define ReadHelperRuleClassName "ReadHelper"
  63. #define HelperCodeFieldName "Code"
  64. // Creation rules.
  65. #define CreationRuleClassName "Creator"
  66. #define CreateFieldName "Create"
  67. // Override rules.
  68. #define OverrideRuleClassName "Override"
  69. #define IgnoredPropertiesFieldName "IgnoredProperties"
  70. namespace clang {
  71. namespace tblgen {
  72. class WrappedRecord {
  73. llvm::Record *Record;
  74. protected:
  75. WrappedRecord(llvm::Record *record = nullptr) : Record(record) {}
  76. llvm::Record *get() const {
  77. assert(Record && "accessing null record");
  78. return Record;
  79. }
  80. public:
  81. llvm::Record *getRecord() const { return Record; }
  82. explicit operator bool() const { return Record != nullptr; }
  83. llvm::ArrayRef<llvm::SMLoc> getLoc() const {
  84. return get()->getLoc();
  85. }
  86. /// Does the node inherit from the given TableGen class?
  87. bool isSubClassOf(llvm::StringRef className) const {
  88. return get()->isSubClassOf(className);
  89. }
  90. template <class NodeClass>
  91. NodeClass getAs() const {
  92. return (isSubClassOf(NodeClass::getTableGenNodeClassName())
  93. ? NodeClass(get()) : NodeClass());
  94. }
  95. friend bool operator<(WrappedRecord lhs, WrappedRecord rhs) {
  96. assert(lhs && rhs && "sorting null nodes");
  97. return lhs.get()->getName() < rhs.get()->getName();
  98. }
  99. friend bool operator>(WrappedRecord lhs, WrappedRecord rhs) {
  100. return rhs < lhs;
  101. }
  102. friend bool operator<=(WrappedRecord lhs, WrappedRecord rhs) {
  103. return !(rhs < lhs);
  104. }
  105. friend bool operator>=(WrappedRecord lhs, WrappedRecord rhs) {
  106. return !(lhs < rhs);
  107. }
  108. friend bool operator==(WrappedRecord lhs, WrappedRecord rhs) {
  109. // This should handle null nodes.
  110. return lhs.getRecord() == rhs.getRecord();
  111. }
  112. friend bool operator!=(WrappedRecord lhs, WrappedRecord rhs) {
  113. return !(lhs == rhs);
  114. }
  115. };
  116. /// Anything in the AST that has properties.
  117. class HasProperties : public WrappedRecord {
  118. public:
  119. static constexpr llvm::StringRef ClassName = HasPropertiesClassName;
  120. HasProperties(llvm::Record *record = nullptr) : WrappedRecord(record) {}
  121. llvm::StringRef getName() const;
  122. static llvm::StringRef getTableGenNodeClassName() {
  123. return HasPropertiesClassName;
  124. }
  125. };
  126. /// An (optional) reference to a TableGen node representing a class
  127. /// in one of Clang's AST hierarchies.
  128. class ASTNode : public HasProperties {
  129. public:
  130. ASTNode(llvm::Record *record = nullptr) : HasProperties(record) {}
  131. llvm::StringRef getName() const {
  132. return get()->getName();
  133. }
  134. /// Return the node for the base, if there is one.
  135. ASTNode getBase() const {
  136. return get()->getValueAsOptionalDef(BaseFieldName);
  137. }
  138. /// Is the corresponding class abstract?
  139. bool isAbstract() const {
  140. return get()->getValueAsBit(AbstractFieldName);
  141. }
  142. static llvm::StringRef getTableGenNodeClassName() {
  143. return ASTNodeClassName;
  144. }
  145. };
  146. class DeclNode : public ASTNode {
  147. public:
  148. DeclNode(llvm::Record *record = nullptr) : ASTNode(record) {}
  149. llvm::StringRef getId() const;
  150. std::string getClassName() const;
  151. DeclNode getBase() const { return DeclNode(ASTNode::getBase().getRecord()); }
  152. static llvm::StringRef getASTHierarchyName() {
  153. return "Decl";
  154. }
  155. static llvm::StringRef getASTIdTypeName() {
  156. return "Decl::Kind";
  157. }
  158. static llvm::StringRef getASTIdAccessorName() {
  159. return "getKind";
  160. }
  161. static llvm::StringRef getTableGenNodeClassName() {
  162. return DeclNodeClassName;
  163. }
  164. };
  165. class TypeNode : public ASTNode {
  166. public:
  167. TypeNode(llvm::Record *record = nullptr) : ASTNode(record) {}
  168. llvm::StringRef getId() const;
  169. llvm::StringRef getClassName() const;
  170. TypeNode getBase() const { return TypeNode(ASTNode::getBase().getRecord()); }
  171. static llvm::StringRef getASTHierarchyName() {
  172. return "Type";
  173. }
  174. static llvm::StringRef getASTIdTypeName() {
  175. return "Type::TypeClass";
  176. }
  177. static llvm::StringRef getASTIdAccessorName() {
  178. return "getTypeClass";
  179. }
  180. static llvm::StringRef getTableGenNodeClassName() {
  181. return TypeNodeClassName;
  182. }
  183. };
  184. class StmtNode : public ASTNode {
  185. public:
  186. StmtNode(llvm::Record *record = nullptr) : ASTNode(record) {}
  187. std::string getId() const;
  188. llvm::StringRef getClassName() const;
  189. StmtNode getBase() const { return StmtNode(ASTNode::getBase().getRecord()); }
  190. static llvm::StringRef getASTHierarchyName() {
  191. return "Stmt";
  192. }
  193. static llvm::StringRef getASTIdTypeName() {
  194. return "Stmt::StmtClass";
  195. }
  196. static llvm::StringRef getASTIdAccessorName() {
  197. return "getStmtClass";
  198. }
  199. static llvm::StringRef getTableGenNodeClassName() {
  200. return StmtNodeClassName;
  201. }
  202. };
  203. /// The type of a property.
  204. class PropertyType : public WrappedRecord {
  205. public:
  206. PropertyType(llvm::Record *record = nullptr) : WrappedRecord(record) {}
  207. /// Is this a generic specialization (i.e. `Array<T>` or `Optional<T>`)?
  208. bool isGenericSpecialization() const {
  209. return get()->isAnonymous();
  210. }
  211. /// The abstract type name of the property. Doesn't work for generic
  212. /// specializations.
  213. llvm::StringRef getAbstractTypeName() const {
  214. return get()->getName();
  215. }
  216. /// The C++ type name of the property. Doesn't work for generic
  217. /// specializations.
  218. llvm::StringRef getCXXTypeName() const {
  219. return get()->getValueAsString(CXXTypeNameFieldName);
  220. }
  221. void emitCXXValueTypeName(bool forRead, llvm::raw_ostream &out) const;
  222. /// Whether the C++ type should be passed around by reference.
  223. bool shouldPassByReference() const {
  224. return get()->getValueAsBit(PassByReferenceFieldName);
  225. }
  226. /// Whether the C++ type should have 'const' prepended when working with
  227. /// a value of the type being written.
  228. bool isConstWhenWriting() const {
  229. return get()->getValueAsBit(ConstWhenWritingFieldName);
  230. }
  231. /// If this is `Array<T>`, return `T`; otherwise return null.
  232. PropertyType getArrayElementType() const {
  233. if (isSubClassOf(ArrayTypeClassName))
  234. return get()->getValueAsDef(ArrayElementTypeFieldName);
  235. return nullptr;
  236. }
  237. /// If this is `Optional<T>`, return `T`; otherwise return null.
  238. PropertyType getOptionalElementType() const {
  239. if (isSubClassOf(OptionalTypeClassName))
  240. return get()->getValueAsDef(OptionalElementTypeFieldName);
  241. return nullptr;
  242. }
  243. /// If this is a subclass type, return its superclass type.
  244. PropertyType getSuperclassType() const {
  245. if (isSubClassOf(SubclassPropertyTypeClassName))
  246. return get()->getValueAsDef(SubclassBaseTypeFieldName);
  247. return nullptr;
  248. }
  249. // Given that this is a subclass type, return the C++ name of its
  250. // subclass type. This is just the bare class name, suitable for
  251. // use in `cast<>`.
  252. llvm::StringRef getSubclassClassName() const {
  253. return get()->getValueAsString(SubclassClassNameFieldName);
  254. }
  255. /// Does this represent an enum type?
  256. bool isEnum() const {
  257. return isSubClassOf(EnumPropertyTypeClassName);
  258. }
  259. llvm::StringRef getPackOptionalCode() const {
  260. return get()->getValueAsString(PackOptionalCodeFieldName);
  261. }
  262. llvm::StringRef getUnpackOptionalCode() const {
  263. return get()->getValueAsString(UnpackOptionalCodeFieldName);
  264. }
  265. std::vector<llvm::Record*> getBufferElementTypes() const {
  266. return get()->getValueAsListOfDefs(BufferElementTypesFieldName);
  267. }
  268. static llvm::StringRef getTableGenNodeClassName() {
  269. return PropertyTypeClassName;
  270. }
  271. };
  272. /// A rule for returning the kind of a type.
  273. class TypeKindRule : public WrappedRecord {
  274. public:
  275. TypeKindRule(llvm::Record *record = nullptr) : WrappedRecord(record) {}
  276. /// Return the type to which this applies.
  277. PropertyType getParentType() const {
  278. return get()->getValueAsDef(TypeFieldName);
  279. }
  280. /// Return the type of the kind.
  281. PropertyType getKindType() const {
  282. return get()->getValueAsDef(KindTypeFieldName);
  283. }
  284. /// Return the name to use for the kind property.
  285. llvm::StringRef getKindPropertyName() const {
  286. return get()->getValueAsString(KindPropertyNameFieldName);
  287. }
  288. /// Return the code for reading the kind value.
  289. llvm::StringRef getReadCode() const {
  290. return get()->getValueAsString(ReadFieldName);
  291. }
  292. static llvm::StringRef getTableGenNodeClassName() {
  293. return TypeKindClassName;
  294. }
  295. };
  296. /// An implementation case of a property type.
  297. class TypeCase : public HasProperties {
  298. public:
  299. TypeCase(llvm::Record *record = nullptr) : HasProperties(record) {}
  300. /// Return the name of this case.
  301. llvm::StringRef getCaseName() const {
  302. return get()->getValueAsString(NameFieldName);
  303. }
  304. /// Return the type of which this is a case.
  305. PropertyType getParentType() const {
  306. return get()->getValueAsDef(TypeFieldName);
  307. }
  308. static llvm::StringRef getTableGenNodeClassName() {
  309. return TypeCaseClassName;
  310. }
  311. };
  312. /// A property of an AST node.
  313. class Property : public WrappedRecord {
  314. public:
  315. Property(llvm::Record *record = nullptr) : WrappedRecord(record) {}
  316. /// Return the name of this property.
  317. llvm::StringRef getName() const {
  318. return get()->getValueAsString(NameFieldName);
  319. }
  320. /// Return the type of this property.
  321. PropertyType getType() const {
  322. return get()->getValueAsDef(TypeFieldName);
  323. }
  324. /// Return the class of which this is a property.
  325. HasProperties getClass() const {
  326. return get()->getValueAsDef(ClassFieldName);
  327. }
  328. /// Return the code for reading this property.
  329. llvm::StringRef getReadCode() const {
  330. return get()->getValueAsString(ReadFieldName);
  331. }
  332. /// Return the code for determining whether to add this property.
  333. llvm::StringRef getCondition() const {
  334. return get()->getValueAsString(ConditionalCodeFieldName);
  335. }
  336. static llvm::StringRef getTableGenNodeClassName() {
  337. return PropertyClassName;
  338. }
  339. };
  340. /// A rule for running some helper code for reading properties from
  341. /// a value (which is actually done when writing the value out).
  342. class ReadHelperRule : public WrappedRecord {
  343. public:
  344. ReadHelperRule(llvm::Record *record = nullptr) : WrappedRecord(record) {}
  345. /// Return the class for which this is a creation rule.
  346. /// Should never be abstract.
  347. HasProperties getClass() const {
  348. return get()->getValueAsDef(ClassFieldName);
  349. }
  350. llvm::StringRef getHelperCode() const {
  351. return get()->getValueAsString(HelperCodeFieldName);
  352. }
  353. static llvm::StringRef getTableGenNodeClassName() {
  354. return ReadHelperRuleClassName;
  355. }
  356. };
  357. /// A rule for how to create an AST node from its properties.
  358. class CreationRule : public WrappedRecord {
  359. public:
  360. CreationRule(llvm::Record *record = nullptr) : WrappedRecord(record) {}
  361. /// Return the class for which this is a creation rule.
  362. /// Should never be abstract.
  363. HasProperties getClass() const {
  364. return get()->getValueAsDef(ClassFieldName);
  365. }
  366. llvm::StringRef getCreationCode() const {
  367. return get()->getValueAsString(CreateFieldName);
  368. }
  369. static llvm::StringRef getTableGenNodeClassName() {
  370. return CreationRuleClassName;
  371. }
  372. };
  373. /// A rule which overrides the standard rules for serializing an AST node.
  374. class OverrideRule : public WrappedRecord {
  375. public:
  376. OverrideRule(llvm::Record *record = nullptr) : WrappedRecord(record) {}
  377. /// Return the class for which this is an override rule.
  378. /// Should never be abstract.
  379. HasProperties getClass() const {
  380. return get()->getValueAsDef(ClassFieldName);
  381. }
  382. /// Return a set of properties that are unnecessary when serializing
  383. /// this AST node. Generally this is used for inherited properties
  384. /// that are derived for this subclass.
  385. std::vector<llvm::StringRef> getIgnoredProperties() const {
  386. return get()->getValueAsListOfStrings(IgnoredPropertiesFieldName);
  387. }
  388. static llvm::StringRef getTableGenNodeClassName() {
  389. return OverrideRuleClassName;
  390. }
  391. };
  392. /// A visitor for an AST node hierarchy. Note that `base` can be null for
  393. /// the root class.
  394. template <class NodeClass>
  395. using ASTNodeHierarchyVisitor =
  396. llvm::function_ref<void(NodeClass node, NodeClass base)>;
  397. void visitASTNodeHierarchyImpl(llvm::RecordKeeper &records,
  398. llvm::StringRef nodeClassName,
  399. ASTNodeHierarchyVisitor<ASTNode> visit);
  400. template <class NodeClass>
  401. void visitASTNodeHierarchy(llvm::RecordKeeper &records,
  402. ASTNodeHierarchyVisitor<NodeClass> visit) {
  403. visitASTNodeHierarchyImpl(records, NodeClass::getTableGenNodeClassName(),
  404. [visit](ASTNode node, ASTNode base) {
  405. visit(NodeClass(node.getRecord()),
  406. NodeClass(base.getRecord()));
  407. });
  408. }
  409. } // end namespace clang::tblgen
  410. } // end namespace clang
  411. #endif