blob: 2e7c52166f81b19f7002c2ab8d0cb9bff4194105 [file] [log] [blame]
Sean Hunt44ed2c32010-05-06 05:24:38 +00001//=== 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 Hunt84e2f952010-05-05 04:13:08 +000014#include "ClangASTNodesEmitter.h"
15#include "Record.h"
Sean Hunt84e2f952010-05-05 04:13:08 +000016#include <map>
Sean Huntdc896a42010-05-05 04:31:44 +000017#include <cctype>
Sean Hunt84e2f952010-05-05 04:13:08 +000018using namespace llvm;
19
20//===----------------------------------------------------------------------===//
21// Statement Node Tables (.inc file) generation.
22//===----------------------------------------------------------------------===//
23
Sean Hunt44ed2c32010-05-06 05:24:38 +000024// Create a macro-ized version of a name
25static std::string macroName(std::string S) {
26 for (unsigned i = 0; i < S.size(); ++i)
27 S[i] = std::toupper(S[i]);
Sean Hunt84e2f952010-05-05 04:13:08 +000028
Sean Hunt44ed2c32010-05-06 05:24:38 +000029 return S;
30}
Sean Hunt84e2f952010-05-05 04:13:08 +000031
Sean Hunt44ed2c32010-05-06 05:24:38 +000032// A map from a node to each of its derived nodes.
33typedef std::multimap<Record*, Record*> ChildMap;
34typedef ChildMap::const_iterator ChildIterator;
Sean Hunt84e2f952010-05-05 04:13:08 +000035
Sean Hunt44ed2c32010-05-06 05:24:38 +000036// Returns the first and last non-abstract subrecords
37// Called recursively to ensure that nodes remain contiguous
38static std::pair<Record *, Record *> EmitStmtNode(const ChildMap &Tree,
39 raw_ostream &OS,
40 Record *Base) {
41 std::string BaseName = macroName(Base->getName());
Sean Hunt84e2f952010-05-05 04:13:08 +000042
Sean Hunt44ed2c32010-05-06 05:24:38 +000043 ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
Sean Hunt84e2f952010-05-05 04:13:08 +000044
Sean Hunt44ed2c32010-05-06 05:24:38 +000045 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 Hunt84e2f952010-05-05 04:13:08 +000050
Sean Hunt44ed2c32010-05-06 05:24:38 +000051 for (; i != e; ++i) {
52 Record *R = i->second;
53 bool Abstract = R->getValueAsBit("Abstract");
54 std::string NodeName = macroName(R->getName());
Sean Hunt84e2f952010-05-05 04:13:08 +000055
Sean Hunt44ed2c32010-05-06 05:24:38 +000056 OS << "#ifndef " << NodeName << "\n";
57 OS << "# define " << NodeName << "(Type, Base) "
58 << BaseName << "(Type, Base)\n";
59 OS << "#endif\n";
Sean Hunt84e2f952010-05-05 04:13:08 +000060
Sean Hunt44ed2c32010-05-06 05:24:38 +000061 if (Abstract)
62 OS << "ABSTRACT(" << NodeName << "(" << R->getName() << ", "
63 << Base->getName() << "))\n";
64 else
65 OS << NodeName << "(" << R->getName() << ", "
66 << Base->getName() << ")\n";
Sean Hunt84e2f952010-05-05 04:13:08 +000067
Sean Hunt44ed2c32010-05-06 05:24:38 +000068 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 Hunt84e2f952010-05-05 04:13:08 +000077
Sean Hunt44ed2c32010-05-06 05:24:38 +000078 if (!First)
79 First = R;
Sean Hunt84e2f952010-05-05 04:13:08 +000080 }
Sean Hunt84e2f952010-05-05 04:13:08 +000081 }
82
Sean Hunt44ed2c32010-05-06 05:24:38 +000083 OS << "#undef " << NodeName << "\n\n";
Sean Hunt84e2f952010-05-05 04:13:08 +000084 }
Sean Hunt44ed2c32010-05-06 05:24:38 +000085
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 Hunt84e2f952010-05-05 04:13:08 +0000104}
105
106void 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 Send1dd5ed2010-05-05 13:56:46 +0000134}