comparison generator/generator.cpp @ 1:e78566595089

initial import
author mandel
date Mon, 11 May 2009 16:01:50 +0000
parents
children cf8a415f3f32
comparison
equal deleted inserted replaced
0:36fb74dc547d 1:e78566595089
1 /****************************************************************************
2 **
3 ** Copyright (C) 1992-2008 Nokia. All rights reserved.
4 **
5 ** This file is part of Qt Jambi.
6 **
7 ** * Commercial Usage
8 * Licensees holding valid Qt Commercial licenses may use this file in
9 * accordance with the Qt Commercial License Agreement provided with the
10 * Software or, alternatively, in accordance with the terms contained in
11 * a written agreement between you and Nokia.
12 *
13 *
14 * GNU General Public License Usage
15 * Alternatively, this file may be used under the terms of the GNU
16 * General Public License versions 2.0 or 3.0 as published by the Free
17 * Software Foundation and appearing in the file LICENSE.GPL included in
18 * the packaging of this file. Please review the following information
19 * to ensure GNU General Public Licensing requirements will be met:
20 * http://www.fsf.org/licensing/licenses/info/GPLv2.html and
21 * http://www.gnu.org/copyleft/gpl.html. In addition, as a special
22 * exception, Nokia gives you certain additional rights. These rights
23 * are described in the Nokia Qt GPL Exception version 1.2, included in
24 * the file GPL_EXCEPTION.txt in this package.
25 *
26 * Qt for Windows(R) Licensees
27 * As a special exception, Nokia, as the sole copyright holder for Qt
28 * Designer, grants users of the Qt/Eclipse Integration plug-in the
29 * right for the Qt/Eclipse Integration to link to functionality
30 * provided by Qt Designer and its related libraries.
31 *
32 *
33 * If you are unsure which license is appropriate for your use, please
34 * contact the sales department at qt-sales@nokia.com.
35
36 **
37 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
38 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
39 **
40 ****************************************************************************/
41
42 #include "generator.h"
43 #include "reporthandler.h"
44 #include "fileout.h"
45
46 #include <QDir>
47 #include <QFile>
48 #include <QFileInfo>
49
50 Generator::Generator()
51 {
52 m_num_generated = 0;
53 m_num_generated_written = 0;
54 m_out_dir = ".";
55 }
56
57 void Generator::generate()
58 {
59 if (m_classes.size() == 0) {
60 ReportHandler::warning(QString("%1: no java classes, skipping")
61 .arg(metaObject()->className()));
62 return;
63 }
64
65
66 foreach (AbstractMetaClass *cls, m_classes) {
67 if (!shouldGenerate(cls))
68 continue;
69
70 QString fileName = fileNameForClass(cls);
71 ReportHandler::debugSparse(QString("generating: %1").arg(fileName));
72
73 FileOut fileOut(outputDirectory() + "/" + subDirectoryForClass(cls) + "/" + fileName);
74 write(fileOut.stream, cls);
75
76 if( fileOut.done() )
77 ++m_num_generated_written;
78 ++m_num_generated;
79 }
80 }
81
82
83 void Generator::printClasses()
84 {
85 QTextStream s(stdout);
86
87 AbstractMetaClassList classes = m_classes;
88 qSort(classes);
89
90 foreach (AbstractMetaClass *cls, classes) {
91 if (!shouldGenerate(cls))
92 continue;
93 write(s, cls);
94 s << endl << endl;
95 }
96 }
97
98 void Generator::verifyDirectoryFor(const QFile &file)
99 {
100 QDir dir = QFileInfo(file).dir();
101 if (!dir.exists()) {
102 if (!dir.mkpath(dir.absolutePath()))
103 ReportHandler::warning(QString("unable to create directory '%1'")
104 .arg(dir.absolutePath()));
105 }
106 }
107
108 QString Generator::subDirectoryForClass(const AbstractMetaClass *) const
109 {
110 Q_ASSERT(false);
111 return QString();
112 }
113
114 QString Generator::fileNameForClass(const AbstractMetaClass *) const
115 {
116 Q_ASSERT(false);
117 return QString();
118 }
119
120 void Generator::write(QTextStream &, const AbstractMetaClass *)
121 {
122 Q_ASSERT(false);
123 }
124
125 bool Generator::hasDefaultConstructor(const AbstractMetaType *type)
126 {
127 QString full_name = type->typeEntry()->qualifiedTargetLangName();
128 QString class_name = type->typeEntry()->targetLangName();
129
130 foreach (const AbstractMetaClass *java_class, m_classes) {
131 if (java_class->typeEntry()->qualifiedTargetLangName() == full_name) {
132 AbstractMetaFunctionList functions = java_class->functions();
133 foreach (const AbstractMetaFunction *function, functions) {
134 if (function->arguments().size() == 0 && function->name() == class_name)
135 return true;
136 }
137 return false;
138 }
139 }
140 return false;
141 }
142
143 bool isLinearContainer(const ContainerTypeEntry *type)
144 {
145 if (type->type() == ContainerTypeEntry::ListContainer
146 || type->type() == ContainerTypeEntry::VectorContainer
147 || type->type() == ContainerTypeEntry::StringListContainer
148 || type->type() == ContainerTypeEntry::LinkedListContainer
149 || type->type() == ContainerTypeEntry::StackContainer
150 || type->type() == ContainerTypeEntry::SetContainer
151 || type->type() == ContainerTypeEntry::QueueContainer)
152 return true;
153 else
154 return false;
155 }
156
157 bool skipType(const AbstractMetaType *d_type)
158 {
159 if (d_type->isContainer()) {
160 const ContainerTypeEntry* c_entry = static_cast<const ContainerTypeEntry*>(d_type->typeEntry());
161 if (c_entry->type() == ContainerTypeEntry::MapContainer ||
162 c_entry->type() == ContainerTypeEntry::MultiMapContainer ||
163 c_entry->type() == ContainerTypeEntry::MapContainer ||
164 c_entry->type() == ContainerTypeEntry::MultiHashContainer)
165 return true;
166
167 QList<AbstractMetaType *> args = d_type->instantiations();
168
169 if (args.size() != 1 || args.at(0)->isContainer()) // not QVector or QList
170 return true;
171 }
172
173 return d_type->isThread() || d_type->isTargetLangChar();
174 }
175
176 bool notWrappedYet(const AbstractMetaFunction *java_function)
177 {
178 AbstractMetaType *d_type = java_function->type();
179 if (d_type)
180 {
181 if(skipType(d_type))
182 return true;
183 }
184
185 AbstractMetaArgumentList arguments = java_function->arguments();
186 foreach (const AbstractMetaArgument *argument, arguments) {
187 if (!java_function->argumentRemoved(argument->argumentIndex() + 1))
188 if (skipType(argument->type()))
189 return true;
190 }
191
192 return false;
193 }
194
195 AbstractMetaFunctionList signalFunctions(const AbstractMetaClass *cls)
196 {
197 AbstractMetaFunctionList r;
198 if (cls->generateShellClass())
199 {
200 AbstractMetaClass *base = cls->baseClass();
201 if (base)
202 r += signalFunctions(base);
203
204 foreach (AbstractMetaFunction *f, cls->functions())
205 {
206 if (!f->isSignal()
207 || cls != f->implementingClass()
208 || notWrappedYet(f)
209 || f->isPrivate()
210 || f->isModifiedRemoved(TypeSystem::TargetLangCode))
211 continue;
212
213 r.append(f);
214 }
215 }
216 return r;
217 }