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 // A map from a node to each of its child nodes. 64 using ChildMap = std::multimap<ASTNode, ASTNode>; 65 66 static void visitASTNodeRecursive(ASTNode node, ASTNode base, 67 const ChildMap &map, 68 ASTNodeHierarchyVisitor<ASTNode> visit) { 69 visit(node, base); 70 71 auto i = map.lower_bound(node), e = map.upper_bound(node); 72 for (; i != e; ++i) { 73 visitASTNodeRecursive(i->second, node, map, visit); 74 } 75 } 76 77 static void visitHierarchy(RecordKeeper &records, 78 StringRef nodeClassName, 79 ASTNodeHierarchyVisitor<ASTNode> visit) { 80 // Check for the node class, just as a sanity check. 81 if (!records.getClass(nodeClassName)) { 82 PrintFatalError(Twine("cannot find definition for node class ") 83 + nodeClassName); 84 } 85 86 // Find all the nodes in the hierarchy. 87 auto nodes = records.getAllDerivedDefinitions(nodeClassName); 88 89 // Derive the child map. 90 ChildMap hierarchy; 91 ASTNode root; 92 for (ASTNode node : nodes) { 93 if (auto base = node.getBase()) 94 hierarchy.insert(std::make_pair(base, node)); 95 else if (root) 96 PrintFatalError(node.getLoc(), 97 "multiple root nodes in " + nodeClassName + " hierarchy"); 98 else 99 root = node; 100 } 101 if (!root) 102 PrintFatalError(Twine("no root node in ") + nodeClassName + " hierarchy"); 103 104 // Now visit the map recursively, starting at the root node. 105 visitASTNodeRecursive(root, ASTNode(), hierarchy, visit); 106 } 107 108 void clang::tblgen::visitASTNodeHierarchyImpl(RecordKeeper &records, 109 StringRef nodeClassName, 110 ASTNodeHierarchyVisitor<ASTNode> visit) { 111 visitHierarchy(records, nodeClassName, visit); 112 } 113