2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-03 11:37:35 +01:00
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-03 11:37:35 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-03 11:37:35 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-03 11:37:35 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Nokia.
|
2008-12-03 11:37:35 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-03 11:37:35 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2008-12-03 11:37:35 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2008-12-03 11:37:35 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-03 11:34:46 +01:00
|
|
|
|
2008-12-09 15:25:01 +01:00
|
|
|
#include <AST.h>
|
2009-01-02 16:10:28 +01:00
|
|
|
#include <ASTVisitor.h>
|
2008-12-09 15:25:01 +01:00
|
|
|
#include <Control.h>
|
|
|
|
#include <Scope.h>
|
|
|
|
#include <Semantic.h>
|
|
|
|
#include <TranslationUnit.h>
|
2009-06-05 13:16:36 +02:00
|
|
|
#include <Literals.h>
|
|
|
|
#include <Symbols.h>
|
|
|
|
#include <Names.h>
|
|
|
|
#include <CoreTypes.h>
|
2008-12-09 15:25:01 +01:00
|
|
|
|
2009-01-06 15:15:13 +01:00
|
|
|
#include <QFile>
|
|
|
|
#include <QList>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QFileInfo>
|
2009-06-05 13:16:36 +02:00
|
|
|
#include <QTime>
|
2009-01-02 16:10:28 +01:00
|
|
|
#include <QtDebug>
|
2008-12-03 11:34:46 +01:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2009-01-06 15:15:13 +01:00
|
|
|
#include <iostream>
|
2009-01-06 15:23:59 +01:00
|
|
|
#include <sstream>
|
2008-12-03 11:34:46 +01:00
|
|
|
|
2009-10-21 14:56:42 +02:00
|
|
|
using namespace CPlusPlus;
|
2009-01-02 16:10:28 +01:00
|
|
|
|
2009-06-05 13:16:36 +02:00
|
|
|
class CloneCG: protected ASTVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CloneCG(Control *control)
|
|
|
|
: ASTVisitor(control)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void operator()(AST *ast)
|
|
|
|
{
|
|
|
|
std::cout <<
|
|
|
|
"/**************************************************************************\n"
|
|
|
|
"**\n"
|
|
|
|
"** This file is part of Qt Creator\n"
|
|
|
|
"**\n"
|
|
|
|
"** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).\n"
|
|
|
|
"**\n"
|
2009-06-17 00:01:27 +10:00
|
|
|
"** Contact: Nokia Corporation (qt-info@nokia.com)\n"
|
2009-06-05 13:16:36 +02:00
|
|
|
"**\n"
|
|
|
|
"** Commercial Usage\n"
|
|
|
|
"**\n"
|
|
|
|
"** Licensees holding valid Qt Commercial licenses may use this file in\n"
|
|
|
|
"** accordance with the Qt Commercial License Agreement provided with the\n"
|
|
|
|
"** Software or, alternatively, in accordance with the terms contained in\n"
|
|
|
|
"** a written agreement between you and Nokia.\n"
|
|
|
|
"**\n"
|
|
|
|
"** GNU Lesser General Public License Usage\n"
|
|
|
|
"**\n"
|
|
|
|
"** Alternatively, this file may be used under the terms of the GNU Lesser\n"
|
|
|
|
"** General Public License version 2.1 as published by the Free Software\n"
|
|
|
|
"** Foundation and appearing in the file LICENSE.LGPL included in the\n"
|
|
|
|
"** packaging of this file. Please review the following information to\n"
|
|
|
|
"** ensure the GNU Lesser General Public License version 2.1 requirements\n"
|
|
|
|
"** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.\n"
|
|
|
|
"**\n"
|
|
|
|
"** If you are unsure which license is appropriate for your use, please\n"
|
2009-08-14 09:30:56 +02:00
|
|
|
"** contact the sales department at http://qt.nokia.com/contact.\n"
|
2009-06-05 13:16:36 +02:00
|
|
|
"**\n"
|
|
|
|
"**************************************************************************/\n"
|
|
|
|
"\n"
|
|
|
|
"#include \"AST.h\"\n"
|
2009-06-17 14:14:16 +02:00
|
|
|
"#include \"ASTVisitor.h\"\n"
|
2009-06-05 13:16:36 +02:00
|
|
|
"\n"
|
2009-10-21 15:00:10 +02:00
|
|
|
"using namespace CPlusPlus;\n" << std::endl;
|
2009-06-05 13:16:36 +02:00
|
|
|
|
|
|
|
accept(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
using ASTVisitor::visit;
|
|
|
|
|
|
|
|
QMap<QByteArray, ClassSpecifierAST *> classMap;
|
|
|
|
|
|
|
|
QByteArray id_cast(NameAST *name)
|
|
|
|
{
|
|
|
|
if (! name)
|
|
|
|
return QByteArray();
|
|
|
|
|
|
|
|
Identifier *id = identifier(name->asSimpleName()->identifier_token);
|
|
|
|
|
|
|
|
return QByteArray::fromRawData(id->chars(), id->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void copyMembers(Class *klass)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < klass->baseClassCount(); ++i) {
|
|
|
|
const QByteArray baseClassName = klass->baseClassAt(i)->identifier()->chars();
|
|
|
|
if (ClassSpecifierAST *baseClassSpec = classMap.value(baseClassName, 0)) {
|
|
|
|
copyMembers(baseClassSpec->symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray className = klass->name()->identifier()->chars();
|
|
|
|
|
|
|
|
std::cout << " // copy " << className.constData() << std::endl;
|
|
|
|
for (unsigned i = 0; i < klass->memberCount(); ++i) {
|
|
|
|
Symbol *member = klass->memberAt(i);
|
|
|
|
Identifier *id = member->name()->identifier();
|
|
|
|
|
|
|
|
if (! id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const QByteArray memberName = QByteArray::fromRawData(id->chars(), id->size());
|
|
|
|
if (member->type().isUnsigned() && memberName.endsWith("_token")) {
|
|
|
|
std::cout << " ast->" << id->chars() << " = " << id->chars() << ";" << std::endl;
|
|
|
|
} else if (PointerType *ptrTy = member->type()->asPointerType()) {
|
|
|
|
if (NamedType *namedTy = ptrTy->elementType()->asNamedType()) {
|
|
|
|
QByteArray typeName = namedTy->name()->identifier()->chars();
|
|
|
|
if (typeName.endsWith("AST")) {
|
|
|
|
std::cout << " if (" << memberName.constData() << ") ast->" << memberName.constData()
|
|
|
|
<< " = " << memberName.constData() << "->clone(pool);" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool visit(ClassSpecifierAST *ast)
|
|
|
|
{
|
|
|
|
Class *klass = ast->symbol;
|
|
|
|
|
|
|
|
const QByteArray className = id_cast(ast->name);
|
|
|
|
classMap.insert(className, ast);
|
|
|
|
|
|
|
|
Identifier *clone_id = control()->findOrInsertIdentifier("clone");
|
|
|
|
if (! klass->members()->lookat(clone_id))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
std::cout << className.constData() << " *" << className.constData() << "::clone(MemoryPool *pool) const" << std::endl
|
|
|
|
<< "{" << std::endl;
|
|
|
|
|
|
|
|
std::cout << " " << className.constData() << " *ast = new (pool) " << className.constData() << ";" << std::endl;
|
|
|
|
|
|
|
|
copyMembers(klass);
|
|
|
|
|
|
|
|
std::cout << " return ast;" << std::endl
|
|
|
|
<< "}" << std::endl
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-17 16:07:36 +02:00
|
|
|
class SearchListNodes: protected ASTVisitor
|
|
|
|
{
|
|
|
|
QList<QByteArray> _listNodes;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SearchListNodes(Control *control)
|
|
|
|
: ASTVisitor(control)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
QList<QByteArray> operator()(AST *ast)
|
|
|
|
{
|
|
|
|
_listNodes.clear();
|
|
|
|
accept(ast);
|
|
|
|
return _listNodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool visit(ClassSpecifierAST *ast)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < ast->symbol->memberCount(); ++i) {
|
|
|
|
Symbol *member = ast->symbol->memberAt(i);
|
|
|
|
if (! qstrcmp("next", member->name()->identifier()->chars())) {
|
|
|
|
_listNodes.append(ast->symbol->name()->identifier()->chars());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-17 14:14:16 +02:00
|
|
|
class VisitCG: protected ASTVisitor
|
|
|
|
{
|
2009-06-17 16:07:36 +02:00
|
|
|
QList<QByteArray> _listNodes;
|
|
|
|
|
2009-06-17 14:14:16 +02:00
|
|
|
public:
|
|
|
|
VisitCG(Control *control)
|
|
|
|
: ASTVisitor(control)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void operator()(AST *ast)
|
|
|
|
{
|
|
|
|
std::cout <<
|
|
|
|
"/**************************************************************************\n"
|
|
|
|
"**\n"
|
|
|
|
"** This file is part of Qt Creator\n"
|
|
|
|
"**\n"
|
|
|
|
"** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).\n"
|
|
|
|
"**\n"
|
|
|
|
"** Contact: Nokia Corporation (qt-info@nokia.com)\n"
|
|
|
|
"**\n"
|
|
|
|
"** Commercial Usage\n"
|
|
|
|
"**\n"
|
|
|
|
"** Licensees holding valid Qt Commercial licenses may use this file in\n"
|
|
|
|
"** accordance with the Qt Commercial License Agreement provided with the\n"
|
|
|
|
"** Software or, alternatively, in accordance with the terms contained in\n"
|
|
|
|
"** a written agreement between you and Nokia.\n"
|
|
|
|
"**\n"
|
|
|
|
"** GNU Lesser General Public License Usage\n"
|
|
|
|
"**\n"
|
|
|
|
"** Alternatively, this file may be used under the terms of the GNU Lesser\n"
|
|
|
|
"** General Public License version 2.1 as published by the Free Software\n"
|
|
|
|
"** Foundation and appearing in the file LICENSE.LGPL included in the\n"
|
|
|
|
"** packaging of this file. Please review the following information to\n"
|
|
|
|
"** ensure the GNU Lesser General Public License version 2.1 requirements\n"
|
|
|
|
"** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.\n"
|
|
|
|
"**\n"
|
|
|
|
"** If you are unsure which license is appropriate for your use, please\n"
|
2009-08-14 09:30:56 +02:00
|
|
|
"** contact the sales department at http://qt.nokia.com/contact.\n"
|
2009-06-17 14:14:16 +02:00
|
|
|
"**\n"
|
|
|
|
"**************************************************************************/\n"
|
|
|
|
"\n"
|
|
|
|
"#include \"AST.h\"\n"
|
|
|
|
"\n"
|
2009-10-21 15:00:10 +02:00
|
|
|
"using namespace CPlusPlus;\n" << std::endl;
|
2009-06-17 14:14:16 +02:00
|
|
|
|
2009-06-17 16:07:36 +02:00
|
|
|
SearchListNodes listNodes(control());
|
|
|
|
_listNodes = listNodes(ast);
|
|
|
|
|
2009-06-17 14:14:16 +02:00
|
|
|
accept(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
using ASTVisitor::visit;
|
|
|
|
|
|
|
|
QMap<QByteArray, ClassSpecifierAST *> classMap;
|
|
|
|
|
|
|
|
QByteArray id_cast(NameAST *name)
|
|
|
|
{
|
|
|
|
if (! name)
|
|
|
|
return QByteArray();
|
|
|
|
|
|
|
|
Identifier *id = identifier(name->asSimpleName()->identifier_token);
|
|
|
|
|
|
|
|
return QByteArray::fromRawData(id->chars(), id->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitMembers(Class *klass)
|
|
|
|
{
|
|
|
|
const QByteArray className = klass->name()->identifier()->chars();
|
|
|
|
|
|
|
|
std::cout << " // visit " << className.constData() << std::endl;
|
|
|
|
for (unsigned i = 0; i < klass->memberCount(); ++i) {
|
|
|
|
Symbol *member = klass->memberAt(i);
|
|
|
|
Identifier *id = member->name()->identifier();
|
|
|
|
|
|
|
|
if (! id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const QByteArray memberName = QByteArray::fromRawData(id->chars(), id->size());
|
|
|
|
if (member->type().isUnsigned() && memberName.endsWith("_token")) {
|
|
|
|
// nothing to do. The member is a token.
|
|
|
|
} else if (PointerType *ptrTy = member->type()->asPointerType()) {
|
|
|
|
if (NamedType *namedTy = ptrTy->elementType()->asNamedType()) {
|
|
|
|
QByteArray typeName = namedTy->name()->identifier()->chars();
|
2009-06-17 16:07:36 +02:00
|
|
|
if (_listNodes.contains(typeName) && memberName != "next") {
|
|
|
|
std::cout
|
|
|
|
<< " for (" << typeName.constData() << " *it = " << memberName.constData() << "; it; it = it->next)" << std::endl
|
|
|
|
<< " accept(it, visitor);" << std::endl;
|
|
|
|
} else if (typeName.endsWith("AST") && memberName != "next") {
|
2009-06-17 14:14:16 +02:00
|
|
|
std::cout << " accept(" << memberName.constData() << ", visitor);" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < klass->baseClassCount(); ++i) {
|
|
|
|
const QByteArray baseClassName = klass->baseClassAt(i)->identifier()->chars();
|
|
|
|
if (ClassSpecifierAST *baseClassSpec = classMap.value(baseClassName, 0)) {
|
|
|
|
visitMembers(baseClassSpec->symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool visit(ClassSpecifierAST *ast)
|
|
|
|
{
|
|
|
|
Class *klass = ast->symbol;
|
|
|
|
|
|
|
|
const QByteArray className = id_cast(ast->name);
|
|
|
|
classMap.insert(className, ast);
|
|
|
|
|
|
|
|
Identifier *visit_id = control()->findOrInsertIdentifier("accept0");
|
|
|
|
if (! klass->members()->lookat(visit_id))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
std::cout
|
|
|
|
<< "void " << className.constData() << "::accept0(ASTVisitor *visitor)" << std::endl
|
|
|
|
<< "{" << std::endl
|
|
|
|
<< " if (visitor->visit(this)) {" << std::endl;
|
|
|
|
|
|
|
|
visitMembers(klass);
|
|
|
|
|
|
|
|
std::cout
|
|
|
|
<< " }" << std::endl
|
|
|
|
<< " visitor->endVisit(this);" << std::endl
|
|
|
|
<< "}" << std::endl
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-01-06 15:15:13 +01:00
|
|
|
int main(int argc, char *argv[])
|
2009-01-02 16:10:28 +01:00
|
|
|
{
|
2009-01-06 15:15:13 +01:00
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
|
|
|
|
QStringList args = app.arguments();
|
|
|
|
const QString appName = args.first();
|
|
|
|
args.removeFirst();
|
|
|
|
|
2009-10-21 14:56:42 +02:00
|
|
|
foreach (const QString &arg, args) {
|
|
|
|
if (arg == QLatin1String("--help")) {
|
2009-01-06 15:15:13 +01:00
|
|
|
const QFileInfo appInfo(appName);
|
|
|
|
const QByteArray appFileName = QFile::encodeName(appInfo.fileName());
|
|
|
|
|
|
|
|
printf("Usage: %s [options]\n"
|
2009-10-21 15:00:10 +02:00
|
|
|
" --help Display this information\n",
|
2009-01-06 15:15:13 +01:00
|
|
|
appFileName.constData());
|
2009-10-21 14:56:42 +02:00
|
|
|
|
2009-01-06 15:15:13 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-03 11:34:46 +01:00
|
|
|
QFile in;
|
|
|
|
if (! in.open(stdin, QFile::ReadOnly))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
const QByteArray source = in.readAll();
|
|
|
|
|
2009-01-02 16:10:28 +01:00
|
|
|
Control control;
|
2009-06-17 14:14:16 +02:00
|
|
|
StringLiteral *fileId = control.findOrInsertStringLiteral("<stdin>");
|
2008-12-03 11:34:46 +01:00
|
|
|
TranslationUnit unit(&control, fileId);
|
2009-01-12 14:55:33 +01:00
|
|
|
unit.setObjCEnabled(true);
|
2008-12-03 11:34:46 +01:00
|
|
|
unit.setSource(source.constData(), source.size());
|
|
|
|
unit.parse();
|
2009-01-02 16:10:28 +01:00
|
|
|
if (! unit.ast())
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
TranslationUnitAST *ast = unit.ast()->asTranslationUnit();
|
|
|
|
Q_ASSERT(ast != 0);
|
|
|
|
|
2009-06-19 12:39:11 +02:00
|
|
|
Namespace *globalNamespace = control.newNamespace(0, 0);
|
2009-01-02 16:10:28 +01:00
|
|
|
Semantic sem(&control);
|
2009-11-10 16:19:52 +01:00
|
|
|
for (DeclarationListAST *it = ast->declarations; it; it = it->next) {
|
|
|
|
DeclarationAST *declaration = it->value;
|
|
|
|
sem.check(declaration, globalNamespace->members());
|
2008-12-03 11:34:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|