2010-05-06 05:24:38 +00:00
|
|
|
//=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// These tablegen backends emit Clang AST node tables
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-05-05 04:13:08 +00:00
|
|
|
#include "ClangASTNodesEmitter.h"
|
2010-05-30 07:21:42 +00:00
|
|
|
#include <set>
|
2010-05-05 04:13:08 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Statement Node Tables (.inc file) generation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-05-06 05:24:38 +00:00
|
|
|
// Returns the first and last non-abstract subrecords
|
|
|
|
// Called recursively to ensure that nodes remain contiguous
|
2010-05-30 07:21:42 +00:00
|
|
|
std::pair<Record *, Record *> ClangASTNodesEmitter::EmitNode(
|
|
|
|
const ChildMap &Tree,
|
|
|
|
raw_ostream &OS,
|
|
|
|
Record *Base) {
|
2010-05-06 05:24:38 +00:00
|
|
|
std::string BaseName = macroName(Base->getName());
|
|
|
|
|
|
|
|
ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
|
|
|
|
|
|
|
|
Record *First = 0, *Last = 0;
|
|
|
|
// This might be the pseudo-node for Stmt; don't assume it has an Abstract
|
|
|
|
// bit
|
|
|
|
if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract"))
|
|
|
|
First = Last = Base;
|
|
|
|
|
|
|
|
for (; i != e; ++i) {
|
|
|
|
Record *R = i->second;
|
|
|
|
bool Abstract = R->getValueAsBit("Abstract");
|
|
|
|
std::string NodeName = macroName(R->getName());
|
|
|
|
|
|
|
|
OS << "#ifndef " << NodeName << "\n";
|
|
|
|
OS << "# define " << NodeName << "(Type, Base) "
|
|
|
|
<< BaseName << "(Type, Base)\n";
|
|
|
|
OS << "#endif\n";
|
|
|
|
|
|
|
|
if (Abstract)
|
2010-05-30 07:21:42 +00:00
|
|
|
OS << "ABSTRACT_" << macroName(Root.getName()) << "(" << NodeName << "("
|
|
|
|
<< R->getName() << ", " << baseName(*Base) << "))\n";
|
2010-05-06 05:24:38 +00:00
|
|
|
else
|
|
|
|
OS << NodeName << "(" << R->getName() << ", "
|
2010-05-30 07:21:42 +00:00
|
|
|
<< baseName(*Base) << ")\n";
|
2010-05-06 05:24:38 +00:00
|
|
|
|
|
|
|
if (Tree.find(R) != Tree.end()) {
|
2010-05-18 06:22:50 +00:00
|
|
|
const std::pair<Record *, Record *> &Result
|
2010-05-30 07:21:42 +00:00
|
|
|
= EmitNode(Tree, OS, R);
|
2010-05-06 05:24:38 +00:00
|
|
|
if (!First && Result.first)
|
|
|
|
First = Result.first;
|
|
|
|
if (Result.second)
|
|
|
|
Last = Result.second;
|
|
|
|
} else {
|
|
|
|
if (!Abstract) {
|
|
|
|
Last = R;
|
|
|
|
|
|
|
|
if (!First)
|
|
|
|
First = R;
|
2010-05-05 04:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 05:24:38 +00:00
|
|
|
OS << "#undef " << NodeName << "\n\n";
|
|
|
|
}
|
2010-05-05 04:13:08 +00:00
|
|
|
|
2010-05-06 05:24:38 +00:00
|
|
|
if (First) {
|
2010-05-18 06:22:50 +00:00
|
|
|
assert (Last && "Got a first node but not a last node for a range!");
|
2010-05-30 07:21:42 +00:00
|
|
|
if (Base == &Root)
|
|
|
|
OS << "LAST_" << macroName(Root.getName()) << "_RANGE(";
|
2010-05-18 06:22:50 +00:00
|
|
|
else
|
2010-05-30 07:21:42 +00:00
|
|
|
OS << macroName(Root.getName()) << "_RANGE(";
|
2010-05-18 06:22:50 +00:00
|
|
|
OS << Base->getName() << ", " << First->getName() << ", "
|
|
|
|
<< Last->getName() << ")\n\n";
|
2010-05-05 04:13:08 +00:00
|
|
|
}
|
2010-05-06 05:24:38 +00:00
|
|
|
|
|
|
|
return std::make_pair(First, Last);
|
2010-05-05 04:13:08 +00:00
|
|
|
}
|
|
|
|
|
2010-05-30 07:21:42 +00:00
|
|
|
void ClangASTNodesEmitter::run(raw_ostream &OS) {
|
2010-05-05 04:13:08 +00:00
|
|
|
// Write the preamble
|
2010-05-30 07:21:42 +00:00
|
|
|
OS << "#ifndef ABSTRACT_" << macroName(Root.getName()) << "\n";
|
|
|
|
OS << "# define ABSTRACT_" << macroName(Root.getName()) << "(Type) Type\n";
|
2010-05-18 06:22:50 +00:00
|
|
|
OS << "#endif\n";
|
|
|
|
|
2010-05-30 07:21:42 +00:00
|
|
|
OS << "#ifndef " << macroName(Root.getName()) << "_RANGE\n";
|
|
|
|
OS << "# define "
|
|
|
|
<< macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
|
2010-05-05 04:13:08 +00:00
|
|
|
OS << "#endif\n\n";
|
|
|
|
|
2010-05-30 07:21:42 +00:00
|
|
|
OS << "#ifndef LAST_" << macroName(Root.getName()) << "_RANGE\n";
|
|
|
|
OS << "# define LAST_"
|
|
|
|
<< macroName(Root.getName()) << "_RANGE(Base, First, Last) "
|
|
|
|
<< macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
|
2010-05-18 06:22:50 +00:00
|
|
|
OS << "#endif\n\n";
|
|
|
|
|
2010-05-05 04:13:08 +00:00
|
|
|
// Emit statements
|
2010-05-30 07:21:42 +00:00
|
|
|
const std::vector<Record*> Stmts
|
|
|
|
= Records.getAllDerivedDefinitions(Root.getName());
|
2010-05-05 04:13:08 +00:00
|
|
|
|
|
|
|
ChildMap Tree;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
|
|
|
|
Record *R = Stmts[i];
|
|
|
|
|
|
|
|
if (R->getValue("Base"))
|
|
|
|
Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
|
|
|
|
else
|
2010-05-30 07:21:42 +00:00
|
|
|
Tree.insert(std::make_pair(&Root, R));
|
2010-05-05 04:13:08 +00:00
|
|
|
}
|
|
|
|
|
2010-05-30 07:21:42 +00:00
|
|
|
EmitNode(Tree, OS, &Root);
|
|
|
|
|
|
|
|
OS << "#undef " << macroName(Root.getName()) << "\n";
|
|
|
|
OS << "#undef " << macroName(Root.getName()) << "_RANGE\n";
|
|
|
|
OS << "#undef LAST_" << macroName(Root.getName()) << "_RANGE\n";
|
|
|
|
OS << "#undef ABSTRACT_" << macroName(Root.getName()) << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClangDeclContextEmitter::run(raw_ostream &OS) {
|
|
|
|
// FIXME: Find a .td file format to allow for this to be represented better.
|
|
|
|
|
|
|
|
OS << "#ifndef DECL_CONTEXT\n";
|
|
|
|
OS << "# define DECL_CONTEXT(DECL)\n";
|
|
|
|
OS << "#endif\n";
|
|
|
|
|
|
|
|
OS << "#ifndef DECL_CONTEXT_BASE\n";
|
|
|
|
OS << "# define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n";
|
|
|
|
OS << "#endif\n";
|
|
|
|
|
|
|
|
typedef std::set<Record*> RecordSet;
|
|
|
|
typedef std::vector<Record*> RecordVector;
|
|
|
|
|
|
|
|
RecordVector DeclContextsVector
|
|
|
|
= Records.getAllDerivedDefinitions("DeclContext");
|
|
|
|
RecordVector Decls = Records.getAllDerivedDefinitions("Decl");
|
|
|
|
RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end());
|
|
|
|
|
|
|
|
for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) {
|
|
|
|
Record *R = *i;
|
|
|
|
|
|
|
|
if (R->getValue("Base")) {
|
|
|
|
Record *B = R->getValueAsDef("Base");
|
|
|
|
if (DeclContexts.find(B) != DeclContexts.end()) {
|
|
|
|
OS << "DECL_CONTEXT_BASE(" << B->getName() << ")\n";
|
|
|
|
DeclContexts.erase(B);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-11 23:20:28 +00:00
|
|
|
// To keep identical order, RecordVector may be used
|
|
|
|
// instead of RecordSet.
|
|
|
|
for (RecordVector::iterator
|
|
|
|
i = DeclContextsVector.begin(), e = DeclContextsVector.end();
|
|
|
|
i != e; ++i)
|
|
|
|
if (DeclContexts.find(*i) != DeclContexts.end())
|
|
|
|
OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n";
|
2010-05-05 04:13:08 +00:00
|
|
|
|
2010-05-30 07:21:42 +00:00
|
|
|
OS << "#undef DECL_CONTEXT\n";
|
|
|
|
OS << "#undef DECL_CONTEXT_BASE\n";
|
2010-05-05 13:56:46 +00:00
|
|
|
}
|