blob: 5d6423da08a37a4425f2f9c4bfb4bbe86ad223fd [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,
Sean Hunt85319752010-05-18 06:22:50 +000040 Record *Base,
41 bool Root = true) {
Sean Hunt44ed2c32010-05-06 05:24:38 +000042 std::string BaseName = macroName(Base->getName());
Sean Hunt84e2f952010-05-05 04:13:08 +000043
Sean Hunt44ed2c32010-05-06 05:24:38 +000044 ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
Sean Hunt84e2f952010-05-05 04:13:08 +000045
Sean Hunt44ed2c32010-05-06 05:24:38 +000046 Record *First = 0, *Last = 0;
47 // This might be the pseudo-node for Stmt; don't assume it has an Abstract
48 // bit
49 if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract"))
50 First = Last = Base;
Sean Hunt84e2f952010-05-05 04:13:08 +000051
Sean Hunt44ed2c32010-05-06 05:24:38 +000052 for (; i != e; ++i) {
53 Record *R = i->second;
54 bool Abstract = R->getValueAsBit("Abstract");
55 std::string NodeName = macroName(R->getName());
Sean Hunt84e2f952010-05-05 04:13:08 +000056
Sean Hunt44ed2c32010-05-06 05:24:38 +000057 OS << "#ifndef " << NodeName << "\n";
58 OS << "# define " << NodeName << "(Type, Base) "
59 << BaseName << "(Type, Base)\n";
60 OS << "#endif\n";
Sean Hunt84e2f952010-05-05 04:13:08 +000061
Sean Hunt44ed2c32010-05-06 05:24:38 +000062 if (Abstract)
Sean Hunt85319752010-05-18 06:22:50 +000063 OS << "ABSTRACT_STMT(" << NodeName << "(" << R->getName() << ", "
Sean Hunt44ed2c32010-05-06 05:24:38 +000064 << Base->getName() << "))\n";
65 else
66 OS << NodeName << "(" << R->getName() << ", "
67 << Base->getName() << ")\n";
Sean Hunt84e2f952010-05-05 04:13:08 +000068
Sean Hunt44ed2c32010-05-06 05:24:38 +000069 if (Tree.find(R) != Tree.end()) {
Sean Hunt85319752010-05-18 06:22:50 +000070 const std::pair<Record *, Record *> &Result
71 = EmitStmtNode(Tree, OS, R, false);
Sean Hunt44ed2c32010-05-06 05:24:38 +000072 if (!First && Result.first)
73 First = Result.first;
74 if (Result.second)
75 Last = Result.second;
76 } else {
77 if (!Abstract) {
78 Last = R;
Sean Hunt84e2f952010-05-05 04:13:08 +000079
Sean Hunt44ed2c32010-05-06 05:24:38 +000080 if (!First)
81 First = R;
Sean Hunt84e2f952010-05-05 04:13:08 +000082 }
Sean Hunt84e2f952010-05-05 04:13:08 +000083 }
84
Sean Hunt44ed2c32010-05-06 05:24:38 +000085 OS << "#undef " << NodeName << "\n\n";
Sean Hunt84e2f952010-05-05 04:13:08 +000086 }
Sean Hunt44ed2c32010-05-06 05:24:38 +000087
Sean Hunt44ed2c32010-05-06 05:24:38 +000088 if (First) {
Sean Hunt85319752010-05-18 06:22:50 +000089 assert (Last && "Got a first node but not a last node for a range!");
90 if (Root)
91 OS << "LAST_STMT_RANGE(";
92 else
93 OS << "STMT_RANGE(";
94
95 OS << Base->getName() << ", " << First->getName() << ", "
96 << Last->getName() << ")\n\n";
Sean Hunt44ed2c32010-05-06 05:24:38 +000097 }
98
Sean Hunt44ed2c32010-05-06 05:24:38 +000099 return std::make_pair(First, Last);
Sean Hunt84e2f952010-05-05 04:13:08 +0000100}
101
102void ClangStmtNodesEmitter::run(raw_ostream &OS) {
103 // Write the preamble
Sean Hunt85319752010-05-18 06:22:50 +0000104 OS << "#ifndef ABSTRACT_STMT\n";
105 OS << "# define ABSTRACT_STMT(Stmt) Stmt\n";
106 OS << "#endif\n";
107
108 OS << "#ifndef STMT_RANGE\n";
109 OS << "# define STMT_RANGE(Base, First, Last)\n";
Sean Hunt84e2f952010-05-05 04:13:08 +0000110 OS << "#endif\n\n";
111
Sean Hunt85319752010-05-18 06:22:50 +0000112 OS << "#ifndef LAST_STMT_RANGE\n";
113 OS << "# define LAST_STMT_RANGE(Base, First, Last) "
114 "STMT_RANGE(Base, First, Last)\n";
115 OS << "#endif\n\n";
116
Sean Hunt84e2f952010-05-05 04:13:08 +0000117 // Emit statements
118 const std::vector<Record*> Stmts = Records.getAllDerivedDefinitions("Stmt");
119
120 ChildMap Tree;
121
122 // Create a pseudo-record to serve as the Stmt node, which isn't actually
123 // output.
124 Record Stmt ("Stmt", SMLoc());
125
126 for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
127 Record *R = Stmts[i];
128
129 if (R->getValue("Base"))
130 Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
131 else
132 Tree.insert(std::make_pair(&Stmt, R));
133 }
134
135 EmitStmtNode(Tree, OS, &Stmt);
136
137 OS << "#undef STMT\n";
Sean Hunt85319752010-05-18 06:22:50 +0000138 OS << "#undef STMT_RANGE\n";
139 OS << "#undef LAST_STMT_RANGE\n";
140 OS << "#undef ABSTRACT_STMT\n";
Shantonu Send1dd5ed2010-05-05 13:56:46 +0000141}