1 //=== ASTTableGen.cpp - Helper functions for working with AST records -----===// 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 // 9 // This file defines some helper functions for working with tblegen reocrds 10 // for the Clang AST: that is, the contents of files such as DeclNodes.td, 11 // StmtNodes.td, and TypeNodes.td. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "ASTTableGen.h" 16 #include "llvm/TableGen/Record.h" 17 #include "llvm/TableGen/Error.h" 18 19 using namespace llvm; 20 using namespace clang; 21 using namespace clang::tblgen; 22 23 static StringRef removeExpectedNodeNameSuffix(Record *node, StringRef suffix) { 24 StringRef nodeName = node->getName(); 25 if (!nodeName.endswith(suffix)) { 26 PrintFatalError(node->getLoc(), 27 Twine("name of node doesn't end in ") + suffix); 28 } 29 return nodeName.drop_back(suffix.size()); 30 } 31 32 // Decl node names don't end in Decl for historical reasons, and it would 33 // be somewhat annoying to fix now. Conveniently, this means the ID matches 34 // is exactly the node name, and the class name is simply that plus Decl. 35 std::string clang::tblgen::DeclNode::getClassName() const { 36 return (Twine(getName()) + "Decl").str(); 37 } 38 StringRef clang::tblgen::DeclNode::getId() const { 39 return getName(); 40 } 41 42 // Type nodes are all named ending in Type, just like the corresponding 43 // C++ class, and the ID just strips this suffix. 44 StringRef clang::tblgen::TypeNode::getClassName() const { 45 return getName(); 46 } 47 StringRef clang::tblgen::TypeNode::getId() const { 48 return removeExpectedNodeNameSuffix(getRecord(), "Type"); 49 } 50 51 // Stmt nodes are named the same as the C++ class, which has no regular 52 // naming convention (all the non-expression statements end in Stmt, 53 // and *many* expressions end in Expr, but there are also several 54 // core expression classes like IntegerLiteral and BinaryOperator with 55 // no standard suffix). The ID adds "Class" for historical reasons. 56 StringRef clang::tblgen::StmtNode::getClassName() const { 57 return getName(); 58 } 59 std::string clang::tblgen::StmtNode::getId() const { 60 return (Twine(getName()) + "Class").str(); 61 } 62 63 /// Emit a string spelling out the C++ value type. 64 void PropertyType::emitCXXValueTypeName(bool forRead, raw_ostream &out) const { 65 if (!isGenericSpecialization()) { 66 if (!forRead && isConstWhenWriting()) 67 out << "const "; 68 out << getCXXTypeName(); 69 } else if (auto elementType = getArrayElementType()) { 70 out << "llvm::ArrayRef<"; 71 elementType.emitCXXValueTypeName(forRead, out); 72 out << ">"; 73 } else if (auto valueType = getOptionalElementType()) { 74 out << "llvm::Optional<"; 75 valueType.emitCXXValueTypeName(forRead, out); 76 out << ">"; 77 } else { 78 //PrintFatalError(getLoc(), "unexpected generic property type"); 79 abort(); 80 } 81 } 82 83 // A map from a node to each of its child nodes. 84 using ChildMap = std::multimap<ASTNode, ASTNode>; 85 86 static void visitASTNodeRecursive(ASTNode node, ASTNode base, 87 const ChildMap &map, 88 ASTNodeHierarchyVisitor<ASTNode> visit) { 89 visit(node, base); 90 91 auto i = map.lower_bound(node), e = map.upper_bound(node); 92 for (; i != e; ++i) { 93 visitASTNodeRecursive(i->second, node, map, visit); 94 } 95 } 96 97 static void visitHierarchy(RecordKeeper &records, 98 StringRef nodeClassName, 99 ASTNodeHierarchyVisitor<ASTNode> visit) { 100 // Check for the node class, just as a sanity check. 101 if (!records.getClass(nodeClassName)) { 102 PrintFatalError(Twine("cannot find definition for node class ") 103 + nodeClassName); 104 } 105 106 // Find all the nodes in the hierarchy. 107 auto nodes = records.getAllDerivedDefinitions(nodeClassName); 108 109 // Derive the child map. 110 ChildMap hierarchy; 111 ASTNode root; 112 for (ASTNode node : nodes) { 113 if (auto base = node.getBase()) 114 hierarchy.insert(std::make_pair(base, node)); 115 else if (root) 116 PrintFatalError(node.getLoc(), 117 "multiple root nodes in " + nodeClassName + " hierarchy"); 118 else 119 root = node; 120 } 121 if (!root) 122 PrintFatalError(Twine("no root node in ") + nodeClassName + " hierarchy"); 123 124 // Now visit the map recursively, starting at the root node. 125 visitASTNodeRecursive(root, ASTNode(), hierarchy, visit); 126 } 127 128 void clang::tblgen::visitASTNodeHierarchyImpl(RecordKeeper &records, 129 StringRef nodeClassName, 130 ASTNodeHierarchyVisitor<ASTNode> visit) { 131 visitHierarchy(records, nodeClassName, visit); 132 } 133