Fix some stylistic issues with my last commit.
[oota-llvm.git] / utils / TableGen / ClangASTNodesEmitter.cpp
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
14 #include "ClangASTNodesEmitter.h"
15 #include "Record.h"
16 #include <map>
17 #include <cctype>
18 using namespace llvm;
19
20 //===----------------------------------------------------------------------===//
21 // Statement Node Tables (.inc file) generation.
22 //===----------------------------------------------------------------------===//
23
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]);
28
29   return S;
30 }
31
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;
35
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());
42
43   ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
44
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;
50
51   for (; i != e; ++i) {
52     Record *R = i->second;
53     bool Abstract = R->getValueAsBit("Abstract");
54     std::string NodeName = macroName(R->getName());
55
56     OS << "#ifndef " << NodeName << "\n";
57     OS << "#  define " << NodeName << "(Type, Base) "
58         << BaseName << "(Type, Base)\n";
59     OS << "#endif\n";
60
61     if (Abstract)
62       OS << "ABSTRACT(" << NodeName << "(" << R->getName() << ", "
63           << Base->getName() << "))\n";
64     else
65       OS << NodeName << "(" << R->getName() << ", "
66           << Base->getName() << ")\n";
67
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;
77
78         if (!First)
79           First = R;
80       }
81     }
82
83     OS << "#undef " << NodeName << "\n\n";
84   }
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);
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";
134 }