Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 1 | //=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // These tablegen backends emit Clang AST node tables |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 14 | #include "ClangASTNodesEmitter.h" |
| 15 | #include "Record.h" |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 16 | #include <map> |
Sean Hunt | dc896a4 | 2010-05-05 04:31:44 +0000 | [diff] [blame] | 17 | #include <cctype> |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | // Statement Node Tables (.inc file) generation. |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 24 | // Create a macro-ized version of a name |
| 25 | static std::string macroName(std::string S) { |
| 26 | for (unsigned i = 0; i < S.size(); ++i) |
| 27 | S[i] = std::toupper(S[i]); |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 28 | |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 29 | return S; |
| 30 | } |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 31 | |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 32 | // A map from a node to each of its derived nodes. |
| 33 | typedef std::multimap<Record*, Record*> ChildMap; |
| 34 | typedef ChildMap::const_iterator ChildIterator; |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 35 | |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 36 | // Returns the first and last non-abstract subrecords |
| 37 | // Called recursively to ensure that nodes remain contiguous |
| 38 | static std::pair<Record *, Record *> EmitStmtNode(const ChildMap &Tree, |
| 39 | raw_ostream &OS, |
| 40 | Record *Base) { |
| 41 | std::string BaseName = macroName(Base->getName()); |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 42 | |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 43 | ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base); |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 44 | |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 45 | Record *First = 0, *Last = 0; |
| 46 | // This might be the pseudo-node for Stmt; don't assume it has an Abstract |
| 47 | // bit |
| 48 | if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract")) |
| 49 | First = Last = Base; |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 50 | |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 51 | for (; i != e; ++i) { |
| 52 | Record *R = i->second; |
| 53 | bool Abstract = R->getValueAsBit("Abstract"); |
| 54 | std::string NodeName = macroName(R->getName()); |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 55 | |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 56 | OS << "#ifndef " << NodeName << "\n"; |
| 57 | OS << "# define " << NodeName << "(Type, Base) " |
| 58 | << BaseName << "(Type, Base)\n"; |
| 59 | OS << "#endif\n"; |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 60 | |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 61 | if (Abstract) |
| 62 | OS << "ABSTRACT(" << NodeName << "(" << R->getName() << ", " |
| 63 | << Base->getName() << "))\n"; |
| 64 | else |
| 65 | OS << NodeName << "(" << R->getName() << ", " |
| 66 | << Base->getName() << ")\n"; |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 67 | |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 68 | if (Tree.find(R) != Tree.end()) { |
| 69 | const std::pair<Record *, Record *> &Result = EmitStmtNode(Tree, OS, R); |
| 70 | if (!First && Result.first) |
| 71 | First = Result.first; |
| 72 | if (Result.second) |
| 73 | Last = Result.second; |
| 74 | } else { |
| 75 | if (!Abstract) { |
| 76 | Last = R; |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 77 | |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 78 | if (!First) |
| 79 | First = R; |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 80 | } |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 83 | OS << "#undef " << NodeName << "\n\n"; |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 84 | } |
Sean Hunt | 44ed2c3 | 2010-05-06 05:24:38 +0000 | [diff] [blame] | 85 | |
| 86 | assert(!First == !Last && "Got a first or last node, but not the other"); |
| 87 | |
| 88 | if (First) { |
| 89 | OS << "#ifndef FIRST_" << BaseName << "\n"; |
| 90 | OS << "# define FIRST_" << BaseName << "(CLASS)\n"; |
| 91 | OS << "#endif\n"; |
| 92 | OS << "#ifndef LAST_" << BaseName << "\n"; |
| 93 | OS << "# define LAST_" << BaseName << "(CLASS)\n"; |
| 94 | OS << "#endif\n\n"; |
| 95 | |
| 96 | OS << "FIRST_" << BaseName << "(" << First->getName() << ")\n"; |
| 97 | OS << "LAST_" << BaseName << "(" << Last->getName() << ")\n\n"; |
| 98 | } |
| 99 | |
| 100 | OS << "#undef FIRST_" << BaseName << "\n"; |
| 101 | OS << "#undef LAST_" << BaseName << "\n\n"; |
| 102 | |
| 103 | return std::make_pair(First, Last); |
Sean Hunt | 84e2f95 | 2010-05-05 04:13:08 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void ClangStmtNodesEmitter::run(raw_ostream &OS) { |
| 107 | // Write the preamble |
| 108 | OS << "#ifndef ABSTRACT\n"; |
| 109 | OS << "# define ABSTRACT(Stmt) Stmt\n"; |
| 110 | OS << "#endif\n\n"; |
| 111 | |
| 112 | // Emit statements |
| 113 | const std::vector<Record*> Stmts = Records.getAllDerivedDefinitions("Stmt"); |
| 114 | |
| 115 | ChildMap Tree; |
| 116 | |
| 117 | // Create a pseudo-record to serve as the Stmt node, which isn't actually |
| 118 | // output. |
| 119 | Record Stmt ("Stmt", SMLoc()); |
| 120 | |
| 121 | for (unsigned i = 0, e = Stmts.size(); i != e; ++i) { |
| 122 | Record *R = Stmts[i]; |
| 123 | |
| 124 | if (R->getValue("Base")) |
| 125 | Tree.insert(std::make_pair(R->getValueAsDef("Base"), R)); |
| 126 | else |
| 127 | Tree.insert(std::make_pair(&Stmt, R)); |
| 128 | } |
| 129 | |
| 130 | EmitStmtNode(Tree, OS, &Stmt); |
| 131 | |
| 132 | OS << "#undef STMT\n"; |
| 133 | OS << "#undef ABSTRACT\n"; |
Shantonu Sen | d1dd5ed | 2010-05-05 13:56:46 +0000 | [diff] [blame] | 134 | } |