@@ -1,377 +1,380 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Script Generator project on Qt Labs. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 | 10 | ** No Commercial Usage |
|
11 | 11 | ** This file contains pre-release code and may not be distributed. |
|
12 | 12 | ** You may use this file in accordance with the terms and conditions |
|
13 | 13 | ** contained in the Technology Preview License Agreement accompanying |
|
14 | 14 | ** this package. |
|
15 | 15 | ** |
|
16 | 16 | ** GNU Lesser General Public License Usage |
|
17 | 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 | 18 | ** General Public License version 2.1 as published by the Free Software |
|
19 | 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 | 20 | ** packaging of this file. Please review the following information to |
|
21 | 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 | 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 | 23 | ** |
|
24 | 24 | ** In addition, as a special exception, Nokia gives you certain additional |
|
25 | 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 | 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 | 27 | ** |
|
28 | 28 | ** If you have questions regarding the use of this file, please contact |
|
29 | 29 | ** Nokia at qt-info@nokia.com. |
|
30 | 30 | ** |
|
31 | 31 | ** |
|
32 | 32 | ** |
|
33 | 33 | ** |
|
34 | 34 | ** |
|
35 | 35 | ** |
|
36 | 36 | ** |
|
37 | 37 | ** |
|
38 | 38 | ** $QT_END_LICENSE$ |
|
39 | 39 | ** |
|
40 | 40 | ****************************************************************************/ |
|
41 | 41 | |
|
42 | 42 | #include "shellgenerator.h" |
|
43 | 43 | #include "reporthandler.h" |
|
44 | 44 | |
|
45 | 45 | #include "metaqtscript.h" |
|
46 | 46 | |
|
47 | 47 | bool ShellGenerator::shouldGenerate(const AbstractMetaClass *meta_class) const |
|
48 | 48 | { |
|
49 | 49 | uint cg = meta_class->typeEntry()->codeGeneration(); |
|
50 | 50 | if (meta_class->name().startsWith("QtScript")) return false; |
|
51 | 51 | if (meta_class->name().startsWith("QFuture")) return false; |
|
52 | 52 | if (meta_class->name().startsWith("Global")) return false; |
|
53 | 53 | if (meta_class->name().startsWith("QStyleOptionComplex")) return false; |
|
54 | 54 | if (meta_class->name().startsWith("QTextLayout")) return false; |
|
55 | 55 | //if (meta_class->name().startsWith("QTextStream")) return false; // because of >> operators |
|
56 | 56 | //if (meta_class->name().startsWith("QDataStream")) return false; // " |
|
57 | 57 | return ((cg & TypeEntry::GenerateCode) != 0); |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | void ShellGenerator::writeTypeInfo(QTextStream &s, const AbstractMetaType *type, Option options) |
|
61 | 61 | { |
|
62 | 62 | if ((options & OriginalTypeDescription) && !type->originalTypeDescription().isEmpty()) { |
|
63 | 63 | s << type->originalTypeDescription(); |
|
64 | 64 | return; |
|
65 | 65 | } |
|
66 | 66 | |
|
67 | 67 | if (type->isArray()) { |
|
68 | 68 | writeTypeInfo(s, type->arrayElementType(), options); |
|
69 | 69 | if (options & ArrayAsPointer) { |
|
70 | 70 | s << "*"; |
|
71 | 71 | } else { |
|
72 | 72 | s << "[" << type->arrayElementCount() << "]"; |
|
73 | 73 | } |
|
74 | 74 | return; |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | const TypeEntry *te = type->typeEntry(); |
|
78 | 78 | |
|
79 | 79 | if (type->isConstant() && !(options & ExcludeConst)) |
|
80 | 80 | s << "const "; |
|
81 | 81 | |
|
82 | 82 | if ((options & EnumAsInts) && (te->isEnum() || te->isFlags())) { |
|
83 | 83 | s << "int"; |
|
84 | 84 | } else if (te->isFlags()) { |
|
85 | 85 | s << ((FlagsTypeEntry *) te)->originalName(); |
|
86 | 86 | } else { |
|
87 | 87 | s << fixCppTypeName(te->qualifiedCppName()); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | if (type->instantiations().size() > 0 |
|
91 | 91 | && (!type->isContainer() |
|
92 | 92 | || (static_cast<const ContainerTypeEntry *>(te))->type() != ContainerTypeEntry::StringListContainer)) { |
|
93 | 93 | s << '<'; |
|
94 | 94 | QList<AbstractMetaType *> args = type->instantiations(); |
|
95 | 95 | bool nested_template = false; |
|
96 | 96 | for (int i=0; i<args.size(); ++i) { |
|
97 | 97 | if (i != 0) |
|
98 | 98 | s << ", "; |
|
99 | 99 | nested_template |= args.at(i)->isContainer(); |
|
100 | 100 | writeTypeInfo(s, args.at(i)); |
|
101 | 101 | } |
|
102 | 102 | if (nested_template) |
|
103 | 103 | s << ' '; |
|
104 | 104 | s << '>'; |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | s << QString(type->indirections(), '*'); |
|
108 | 108 | |
|
109 | 109 | if (type->isReference() && !(options & ExcludeReference) && !(options & ConvertReferenceToPtr)) |
|
110 | 110 | s << "&"; |
|
111 | 111 | |
|
112 | 112 | if (type->isReference() && (options & ConvertReferenceToPtr)) { |
|
113 | 113 | s << "*"; |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | |
|
117 | 117 | if (!(options & SkipName)) |
|
118 | 118 | s << ' '; |
|
119 | 119 | } |
|
120 | 120 | |
|
121 | 121 | |
|
122 | 122 | void ShellGenerator::writeFunctionArguments(QTextStream &s, const AbstractMetaClass* owner, |
|
123 | 123 | const AbstractMetaArgumentList &arguments, |
|
124 | 124 | Option option, |
|
125 | 125 | int numArguments) |
|
126 | 126 | { |
|
127 | 127 | if (numArguments < 0) numArguments = arguments.size(); |
|
128 | 128 | |
|
129 | 129 | for (int i=0; i<numArguments; ++i) { |
|
130 | 130 | if (i != 0) |
|
131 | 131 | s << ", "; |
|
132 | 132 | AbstractMetaArgument *arg = arguments.at(i); |
|
133 | 133 | writeTypeInfo(s, arg->type(), option); |
|
134 | 134 | if (!(option & SkipName)) |
|
135 | 135 | s << " " << arg->argumentName(); |
|
136 | 136 | if ((option & IncludeDefaultExpression) && !arg->originalDefaultValueExpression().isEmpty()) { |
|
137 | 137 | s << " = "; |
|
138 | 138 | |
|
139 | 139 | QString expr = arg->originalDefaultValueExpression(); |
|
140 | 140 | if (expr != "0") { |
|
141 | 141 | QString qualifier; |
|
142 | 142 | if (arg->type()->typeEntry()->isEnum() && expr.indexOf("::") < 0) { |
|
143 | 143 | qualifier = ((EnumTypeEntry *)arg->type()->typeEntry())->qualifier(); |
|
144 | 144 | } else if (arg->type()->typeEntry()->isFlags() && expr.indexOf("::") < 0) { |
|
145 | 145 | qualifier = ((FlagsTypeEntry *)arg->type()->typeEntry())->originator()->qualifier(); |
|
146 | 146 | } |
|
147 | 147 | if (!qualifier.isEmpty()) { |
|
148 | 148 | s << qualifier << "::"; |
|
149 | 149 | } |
|
150 | 150 | } |
|
151 | 151 | if (expr.contains("defaultConnection")) { |
|
152 | 152 | expr.replace("defaultConnection","QSqlDatabase::defaultConnection"); |
|
153 | 153 | } |
|
154 | 154 | if (expr == "MediaSource()") { |
|
155 | 155 | expr = "Phonon::MediaSource()"; |
|
156 | 156 | } |
|
157 | 157 | s << expr; |
|
158 | 158 | } |
|
159 | 159 | } |
|
160 | 160 | } |
|
161 | 161 | |
|
162 | 162 | /*! |
|
163 | 163 | * Writes the function \a meta_function signature to the textstream \a s. |
|
164 | 164 | * |
|
165 | 165 | * The \a name_prefix can be used to give the function name a prefix, |
|
166 | 166 | * like "__public_" or "__override_" and \a classname_prefix can |
|
167 | 167 | * be used to give the class name a prefix. |
|
168 | 168 | * |
|
169 | 169 | * The \a option flags can be used to tweak various parameters, such as |
|
170 | 170 | * showing static, original vs renamed name, underscores for space etc. |
|
171 | 171 | * |
|
172 | 172 | * The \a extra_arguments list is a list of extra arguments on the |
|
173 | 173 | * form "bool static_call". |
|
174 | 174 | */ |
|
175 | 175 | |
|
176 | 176 | void ShellGenerator::writeFunctionSignature(QTextStream &s, |
|
177 | 177 | const AbstractMetaFunction *meta_function, |
|
178 | 178 | const AbstractMetaClass *implementor, |
|
179 | 179 | const QString &name_prefix, |
|
180 | 180 | Option option, |
|
181 | 181 | const QString &classname_prefix, |
|
182 | 182 | const QStringList &extra_arguments, |
|
183 | 183 | int numArguments) |
|
184 | 184 | { |
|
185 | 185 | // ### remove the implementor |
|
186 | 186 | AbstractMetaType *function_type = meta_function->type(); |
|
187 | 187 | |
|
188 | 188 | |
|
189 | 189 | if ((option & SkipReturnType) == 0) { |
|
190 | 190 | if (function_type) { |
|
191 | 191 | writeTypeInfo(s, function_type, option); |
|
192 | 192 | s << " "; |
|
193 | 193 | } else if (!meta_function->isConstructor()) { |
|
194 | 194 | s << "void "; |
|
195 | 195 | } |
|
196 | 196 | } |
|
197 | 197 | |
|
198 | 198 | if (implementor) { |
|
199 | 199 | if (classname_prefix.isEmpty()) |
|
200 | 200 | s << wrapperClassName(implementor) << "::"; |
|
201 | 201 | else |
|
202 | 202 | s << classname_prefix << implementor->name() << "::"; |
|
203 | 203 | } |
|
204 | 204 | |
|
205 | 205 | |
|
206 | 206 | QString function_name; |
|
207 | 207 | if (option & OriginalName) |
|
208 | 208 | function_name = meta_function->originalName(); |
|
209 | 209 | else |
|
210 | 210 | function_name = meta_function->name(); |
|
211 | 211 | |
|
212 | 212 | if (option & UnderscoreSpaces) |
|
213 | 213 | function_name = function_name.replace(' ', '_'); |
|
214 | 214 | |
|
215 | 215 | if (meta_function->isConstructor()) |
|
216 | 216 | function_name = meta_function->ownerClass()->name(); |
|
217 | 217 | |
|
218 | 218 | if (meta_function->isStatic() && (option & ShowStatic)) { |
|
219 | 219 | function_name = "static_" + meta_function->ownerClass()->name() + "_" + function_name; |
|
220 | 220 | } |
|
221 | 221 | |
|
222 | 222 | if (function_name.startsWith("operator")) { |
|
223 | 223 | function_name = meta_function->name(); |
|
224 | 224 | } |
|
225 | 225 | |
|
226 | 226 | s << name_prefix << function_name; |
|
227 | 227 | |
|
228 | 228 | if (meta_function->attributes() & AbstractMetaAttributes::SetterFunction) |
|
229 | 229 | s << "_setter"; |
|
230 | 230 | else if (meta_function->attributes() & AbstractMetaAttributes::GetterFunction) |
|
231 | 231 | s << "_getter"; |
|
232 | 232 | |
|
233 | 233 | s << "("; |
|
234 | 234 | |
|
235 | 235 | if ((option & FirstArgIsWrappedObject) && meta_function->ownerClass() && !meta_function->isConstructor() && !meta_function->isStatic()) { |
|
236 | 236 | s << meta_function->ownerClass()->qualifiedCppName() << "* theWrappedObject"; |
|
237 | 237 | if (meta_function->arguments().size() != 0) { |
|
238 | 238 | s << ", "; |
|
239 | 239 | } |
|
240 | 240 | } |
|
241 | 241 | |
|
242 | 242 | writeFunctionArguments(s, meta_function->ownerClass(), meta_function->arguments(), Option(option & Option(~ConvertReferenceToPtr)), numArguments); |
|
243 | 243 | |
|
244 | 244 | // The extra arguments... |
|
245 | 245 | for (int i=0; i<extra_arguments.size(); ++i) { |
|
246 | 246 | if (i > 0 || meta_function->arguments().size() != 0) |
|
247 | 247 | s << ", "; |
|
248 | 248 | s << extra_arguments.at(i); |
|
249 | 249 | } |
|
250 | 250 | |
|
251 | 251 | s << ")"; |
|
252 | 252 | if (meta_function->isConstant()) |
|
253 | 253 | s << " const"; |
|
254 | 254 | } |
|
255 | 255 | |
|
256 | 256 | AbstractMetaFunctionList ShellGenerator::getFunctionsToWrap(const AbstractMetaClass* meta_class) |
|
257 | 257 | { |
|
258 | 258 | AbstractMetaFunctionList functions = meta_class->queryFunctions( |
|
259 | 259 | AbstractMetaClass::NormalFunctions | AbstractMetaClass::WasPublic |
|
260 | 260 | | AbstractMetaClass::NotRemovedFromTargetLang | AbstractMetaClass::ClassImplements |
|
261 | 261 | ); |
|
262 | 262 | AbstractMetaFunctionList functions2 = meta_class->queryFunctions( |
|
263 | 263 | AbstractMetaClass::VirtualFunctions | AbstractMetaClass::WasVisible |
|
264 | 264 | | AbstractMetaClass::NotRemovedFromTargetLang | AbstractMetaClass::ClassImplements |
|
265 | 265 | ); |
|
266 | 266 | QSet<AbstractMetaFunction*> set1 = QSet<AbstractMetaFunction*>::fromList(functions); |
|
267 | 267 | foreach(AbstractMetaFunction* func, functions2) { |
|
268 | 268 | set1.insert(func); |
|
269 | 269 | } |
|
270 | 270 | |
|
271 | 271 | AbstractMetaFunctionList resultFunctions; |
|
272 | 272 | |
|
273 | 273 | foreach(AbstractMetaFunction* func, set1.toList()) { |
|
274 | 274 | if (!func->isAbstract() && func->implementingClass()==meta_class) { |
|
275 | 275 | resultFunctions << func; |
|
276 | 276 | } |
|
277 | 277 | } |
|
278 | qStableSort(resultFunctions); | |
|
278 | 279 | return resultFunctions; |
|
279 | 280 | } |
|
280 | 281 | |
|
281 | 282 | AbstractMetaFunctionList ShellGenerator::getVirtualFunctionsForShell(const AbstractMetaClass* meta_class) |
|
282 | 283 | { |
|
283 | 284 | AbstractMetaFunctionList functions = meta_class->queryFunctions( |
|
284 | 285 | AbstractMetaClass::VirtualFunctions | AbstractMetaClass::WasVisible |
|
285 | 286 | // | AbstractMetaClass::NotRemovedFromTargetLang |
|
286 | 287 | ); |
|
288 | qStableSort(functions); | |
|
287 | 289 | return functions; |
|
288 | 290 | } |
|
289 | 291 | |
|
290 | 292 | AbstractMetaFunctionList ShellGenerator::getProtectedFunctionsThatNeedPromotion(const AbstractMetaClass* meta_class) |
|
291 | 293 | { |
|
292 | 294 | AbstractMetaFunctionList functions; |
|
293 | 295 | AbstractMetaFunctionList functions1 = getFunctionsToWrap(meta_class); |
|
294 | 296 | foreach(AbstractMetaFunction* func, functions1) { |
|
295 | 297 | if (!func->isPublic() || func->isVirtual()) { |
|
296 | 298 | functions << func; |
|
297 | 299 | } |
|
298 | 300 | } |
|
301 | qStableSort(functions); | |
|
299 | 302 | return functions; |
|
300 | 303 | } |
|
301 | 304 | |
|
302 | 305 | /*! |
|
303 | 306 | Writes the include defined by \a inc to \a stream. |
|
304 | 307 | */ |
|
305 | 308 | void ShellGenerator::writeInclude(QTextStream &stream, const Include &inc) |
|
306 | 309 | { |
|
307 | 310 | if (inc.name.isEmpty()) |
|
308 | 311 | return; |
|
309 | 312 | if (inc.type == Include::TargetLangImport) |
|
310 | 313 | return; |
|
311 | 314 | stream << "#include "; |
|
312 | 315 | if (inc.type == Include::IncludePath) |
|
313 | 316 | stream << "<"; |
|
314 | 317 | else |
|
315 | 318 | stream << "\""; |
|
316 | 319 | stream << inc.name; |
|
317 | 320 | if (inc.type == Include::IncludePath) |
|
318 | 321 | stream << ">"; |
|
319 | 322 | else |
|
320 | 323 | stream << "\""; |
|
321 | 324 | stream << endl; |
|
322 | 325 | } |
|
323 | 326 | |
|
324 | 327 | /*! |
|
325 | 328 | Returns true if the given function \a fun is operator>>() or |
|
326 | 329 | operator<<() that streams from/to a Q{Data,Text}Stream, false |
|
327 | 330 | otherwise. |
|
328 | 331 | */ |
|
329 | 332 | bool ShellGenerator::isSpecialStreamingOperator(const AbstractMetaFunction *fun) |
|
330 | 333 | { |
|
331 | 334 | return ((fun->functionType() == AbstractMetaFunction::GlobalScopeFunction) |
|
332 | 335 | && (fun->arguments().size() == 1) |
|
333 | 336 | && (((fun->originalName() == "operator>>") && (fun->modifiedName() == "readFrom")) |
|
334 | 337 | || ((fun->originalName() == "operator<<") && (fun->modifiedName() == "writeTo")))); |
|
335 | 338 | } |
|
336 | 339 | |
|
337 | 340 | bool ShellGenerator::isBuiltIn(const QString& name) { |
|
338 | 341 | |
|
339 | 342 | static QSet<QString> builtIn; |
|
340 | 343 | if (builtIn.isEmpty()) { |
|
341 | 344 | builtIn.insert("Qt"); |
|
342 | 345 | builtIn.insert("QFont"); |
|
343 | 346 | builtIn.insert("QPixmap"); |
|
344 | 347 | builtIn.insert("QBrush"); |
|
345 | 348 | builtIn.insert("QBitArray"); |
|
346 | 349 | builtIn.insert("QPalette"); |
|
347 | 350 | builtIn.insert("QPen"); |
|
348 | 351 | builtIn.insert("QIcon"); |
|
349 | 352 | builtIn.insert("QImage"); |
|
350 | 353 | builtIn.insert("QPolygon"); |
|
351 | 354 | builtIn.insert("QRegion"); |
|
352 | 355 | builtIn.insert("QBitmap"); |
|
353 | 356 | builtIn.insert("QCursor"); |
|
354 | 357 | builtIn.insert("QColor"); |
|
355 | 358 | builtIn.insert("QSizePolicy"); |
|
356 | 359 | builtIn.insert("QKeySequence"); |
|
357 | 360 | builtIn.insert("QTextLength"); |
|
358 | 361 | builtIn.insert("QTextFormat"); |
|
359 | 362 | builtIn.insert("QMatrix"); |
|
360 | 363 | builtIn.insert("QDate"); |
|
361 | 364 | builtIn.insert("QTime"); |
|
362 | 365 | builtIn.insert("QDateTime"); |
|
363 | 366 | builtIn.insert("QUrl"); |
|
364 | 367 | builtIn.insert("QLocale"); |
|
365 | 368 | builtIn.insert("QRect"); |
|
366 | 369 | builtIn.insert("QRectF"); |
|
367 | 370 | builtIn.insert("QSize"); |
|
368 | 371 | builtIn.insert("QSizeF"); |
|
369 | 372 | builtIn.insert("QLine"); |
|
370 | 373 | builtIn.insert("QLineF"); |
|
371 | 374 | builtIn.insert("QPoint"); |
|
372 | 375 | builtIn.insert("QPointF"); |
|
373 | 376 | builtIn.insert("QRegExp"); |
|
374 | 377 | } |
|
375 | 378 | return builtIn.contains(name); |
|
376 | 379 | } |
|
377 | 380 |
General Comments 0
You need to be logged in to leave comments.
Login now