Mercurial > projects > qtd
annotate generator/generator.cpp @ 393:1049b01aebd2
Fixed generator segfaults on OS X 10.6.
Regarding the change to dgenerator.cpp, I am not actually sure if 0 is the correct value to pass, but it seems to work and the code which had been there before was just plain incorrect ? it read past the list boundaries.
author | David Nadlinger <code@klickverbot.at> |
---|---|
date | Sat, 28 Aug 2010 02:55:55 +0200 |
parents | 9784459f0750 |
children |
rev | line source |
---|---|
1 | 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 | |
357
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
143 // hackery to get qt module names |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
144 QString Generator::packageToQtModule(QString package, ModuleNameType nameType) |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
145 { |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
146 if (!package.startsWith("qt.")) |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
147 qFatal(qPrintable("Package " + package + " does not start with 'qt.'")); |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
148 |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
149 QString module = package.right(package.length() - 3); |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
150 |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
151 if (nameType == ShortUpper) |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
152 return module.toUpper(); |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
153 else if (nameType == LongCamel) { |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
154 module[0] = module[0].toTitleCase(); |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
155 return "Qt" + module; |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
156 } |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
157 else |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
158 Q_ASSERT(0); |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
159 } |
9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Max Samukha <maxter@spambox.com>
parents:
253
diff
changeset
|
160 |
1 | 161 bool isLinearContainer(const ContainerTypeEntry *type) |
162 { | |
163 if (type->type() == ContainerTypeEntry::ListContainer | |
164 || type->type() == ContainerTypeEntry::VectorContainer | |
165 || type->type() == ContainerTypeEntry::StringListContainer | |
166 || type->type() == ContainerTypeEntry::LinkedListContainer | |
167 || type->type() == ContainerTypeEntry::StackContainer | |
168 || type->type() == ContainerTypeEntry::SetContainer | |
169 || type->type() == ContainerTypeEntry::QueueContainer) | |
170 return true; | |
171 else | |
172 return false; | |
173 } | |
174 | |
175 bool skipType(const AbstractMetaType *d_type) | |
176 { | |
177 if (d_type->isContainer()) { | |
178 const ContainerTypeEntry* c_entry = static_cast<const ContainerTypeEntry*>(d_type->typeEntry()); | |
179 if (c_entry->type() == ContainerTypeEntry::MapContainer || | |
180 c_entry->type() == ContainerTypeEntry::MultiMapContainer || | |
181 c_entry->type() == ContainerTypeEntry::MapContainer || | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
1
diff
changeset
|
182 c_entry->type() == ContainerTypeEntry::MultiHashContainer || |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
1
diff
changeset
|
183 c_entry->type() == ContainerTypeEntry::SetContainer) |
1 | 184 return true; |
185 | |
186 QList<AbstractMetaType *> args = d_type->instantiations(); | |
187 | |
188 if (args.size() != 1 || args.at(0)->isContainer()) // not QVector or QList | |
189 return true; | |
190 } | |
191 | |
192 return d_type->isThread() || d_type->isTargetLangChar(); | |
193 } | |
194 | |
195 bool notWrappedYet(const AbstractMetaFunction *java_function) | |
196 { | |
197 AbstractMetaType *d_type = java_function->type(); | |
198 if (d_type) | |
199 { | |
200 if(skipType(d_type)) | |
201 return true; | |
202 } | |
203 | |
204 AbstractMetaArgumentList arguments = java_function->arguments(); | |
205 foreach (const AbstractMetaArgument *argument, arguments) { | |
206 if (!java_function->argumentRemoved(argument->argumentIndex() + 1)) | |
207 if (skipType(argument->type())) | |
208 return true; | |
209 } | |
210 | |
211 return false; | |
212 } | |
213 | |
253 | 214 AbstractMetaFunctionList signalFunctions(const AbstractMetaClass *cls, bool includeBase) |
1 | 215 { |
216 AbstractMetaFunctionList r; | |
217 if (cls->generateShellClass()) | |
218 { | |
253 | 219 if (includeBase) |
220 { | |
221 AbstractMetaClass *base = cls->baseClass(); | |
222 if (base) | |
223 r += signalFunctions(base); | |
224 } | |
1 | 225 |
226 foreach (AbstractMetaFunction *f, cls->functions()) | |
227 { | |
228 if (!f->isSignal() | |
229 || cls != f->implementingClass() | |
230 || notWrappedYet(f) | |
231 || f->isModifiedRemoved(TypeSystem::TargetLangCode)) | |
232 continue; | |
233 | |
234 r.append(f); | |
235 } | |
236 } | |
237 return r; | |
238 } |