Mercurial > projects > qtd
annotate generator/typesystem.cpp @ 267:1124c49b9d58
qtdopengl fix
author | SokoL_SD |
---|---|
date | Fri, 11 Sep 2009 15:30:46 +0000 |
parents | ae34188ddd84 |
children | bc783e20da2b |
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 "typesystem.h" | |
43 #include "generator.h" | |
44 | |
45 #include "customtypes.h" | |
46 | |
47 #include <reporthandler.h> | |
48 | |
49 #include <QtXml> | |
50 | |
51 QString strings_Object = QLatin1String("Object"); | |
52 QString strings_String = QLatin1String("string"); | |
53 QString strings_Thread = QLatin1String("Thread"); | |
54 QString strings_char = QLatin1String("char"); | |
55 QString strings_java_lang = QLatin1String("java.lang"); | |
56 QString strings_jchar = QLatin1String("jchar"); | |
57 QString strings_jobject = QLatin1String("jobject"); | |
58 | |
59 static void addRemoveFunctionToTemplates(TypeDatabase *db); | |
60 | |
61 class StackElement | |
62 { | |
63 public: | |
64 enum ElementType { | |
65 None = 0x0, | |
66 | |
67 // Type tags (0x1, ... , 0xff) | |
68 ObjectTypeEntry = 0x1, | |
69 ValueTypeEntry = 0x2, | |
70 InterfaceTypeEntry = 0x3, | |
71 NamespaceTypeEntry = 0x4, | |
72 ComplexTypeEntryMask = 0xf, | |
73 | |
74 // Non-complex type tags (0x10, 0x20, ... , 0xf0) | |
75 PrimitiveTypeEntry = 0x10, | |
76 EnumTypeEntry = 0x20, | |
77 TypeEntryMask = 0xff, | |
78 | |
79 // Simple tags (0x100, 0x200, ... , 0xf00) | |
80 ExtraIncludes = 0x100, | |
81 Include = 0x200, | |
82 ModifyFunction = 0x300, | |
83 ModifyField = 0x400, | |
84 Root = 0x500, | |
85 CustomMetaConstructor = 0x600, | |
86 CustomMetaDestructor = 0x700, | |
87 ArgumentMap = 0x800, | |
88 SuppressedWarning = 0x900, | |
89 Rejection = 0xa00, | |
90 LoadTypesystem = 0xb00, | |
91 RejectEnumValue = 0xc00, | |
92 Template = 0xd00, | |
93 TemplateInstanceEnum = 0xe00, | |
94 Replace = 0xf00, | |
95 SimpleMask = 0xf00, | |
96 // qtd stuff | |
97 AddClass = 0x1100, | |
42 | 98 PackageDepend = 0x1200, |
1 | 99 |
100 // Code snip tags (0x1000, 0x2000, ... , 0xf000) | |
101 InjectCode = 0x1000, | |
102 InjectCodeInFunction = 0x2000, | |
103 CodeSnipMask = 0xf000, | |
104 | |
105 // Function modifier tags (0x010000, 0x020000, ... , 0xf00000) | |
106 Access = 0x010000, | |
107 Removal = 0x020000, | |
108 Rename = 0x040000, | |
109 ModifyArgument = 0x080000, | |
148
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
44
diff
changeset
|
110 PrivateSignal = 0x100000, |
1 | 111 FunctionModifiers = 0xff0000, |
112 StoreResult = 0x110000, | |
113 | |
114 // Argument modifier tags (0x01000000 ... 0xf0000000) | |
115 ConversionRule = 0x01000000, | |
116 ReplaceType = 0x02000000, | |
117 ReplaceDefaultExpression = 0x04000000, | |
118 RemoveArgument = 0x08000000, | |
119 DefineOwnership = 0x10000000, | |
120 RemoveDefaultExpression = 0x20000000, | |
121 NoNullPointers = 0x40000000, | |
122 ReferenceCount = 0x80000000, | |
123 ArgumentModifiers = 0xff000000 | |
124 | |
125 }; | |
126 | |
127 StackElement(StackElement *p) : entry(0), type(None), parent(p){ } | |
128 | |
129 TypeEntry *entry; | |
130 ElementType type; | |
131 StackElement *parent; | |
132 | |
133 union { | |
134 TemplateInstance *templateInstance; | |
135 TemplateEntry *templateEntry; | |
136 CustomFunction *customFunction; | |
137 } value; | |
138 }; | |
139 | |
140 class Handler : public QXmlDefaultHandler | |
141 { | |
142 public: | |
4 | 143 Handler(TypeDatabase *database, const QString &source_dir, bool generate) |
144 : m_database(database), m_generate(generate ? TypeEntry::GenerateAll : TypeEntry::GenerateForSubclass), m_source_dir(source_dir) | |
1 | 145 { |
146 m_current_enum = 0; | |
147 current = 0; | |
148 | |
149 tagNames["rejection"] = StackElement::Rejection; | |
150 tagNames["primitive-type"] = StackElement::PrimitiveTypeEntry; | |
151 tagNames["object-type"] = StackElement::ObjectTypeEntry; | |
152 tagNames["value-type"] = StackElement::ValueTypeEntry; | |
153 tagNames["interface-type"] = StackElement::InterfaceTypeEntry; | |
154 tagNames["namespace-type"] = StackElement::NamespaceTypeEntry; | |
155 tagNames["enum-type"] = StackElement::EnumTypeEntry; | |
156 tagNames["extra-includes"] = StackElement::ExtraIncludes; | |
157 tagNames["include"] = StackElement::Include; | |
158 tagNames["inject-code"] = StackElement::InjectCode; | |
159 tagNames["modify-function"] = StackElement::ModifyFunction; | |
160 tagNames["modify-field"] = StackElement::ModifyField; | |
161 tagNames["access"] = StackElement::Access; | |
162 tagNames["remove"] = StackElement::Removal; | |
163 tagNames["rename"] = StackElement::Rename; | |
164 tagNames["typesystem"] = StackElement::Root; | |
165 tagNames["custom-constructor"] = StackElement::CustomMetaConstructor; | |
166 tagNames["custom-destructor"] = StackElement::CustomMetaDestructor; | |
167 tagNames["argument-map"] = StackElement::ArgumentMap; | |
168 tagNames["suppress-warning"] = StackElement::SuppressedWarning; | |
169 tagNames["load-typesystem"] = StackElement::LoadTypesystem; | |
170 tagNames["define-ownership"] = StackElement::DefineOwnership; | |
171 tagNames["replace-default-expression"] = StackElement::ReplaceDefaultExpression; | |
172 tagNames["reject-enum-value"] = StackElement::RejectEnumValue; | |
173 tagNames["replace-type"] = StackElement::ReplaceType; | |
174 tagNames["conversion-rule"] = StackElement::ConversionRule; | |
175 tagNames["modify-argument"] = StackElement::ModifyArgument; | |
176 tagNames["remove-argument"] = StackElement::RemoveArgument; | |
177 tagNames["remove-default-expression"] = StackElement::RemoveDefaultExpression; | |
178 tagNames["template"] = StackElement::Template; | |
179 tagNames["insert-template"] = StackElement::TemplateInstanceEnum; | |
180 tagNames["replace"] = StackElement::Replace; | |
181 tagNames["no-null-pointer"] = StackElement::NoNullPointers; | |
182 tagNames["reference-count"] = StackElement::ReferenceCount; | |
183 // qtd | |
184 tagNames["add-class"] = StackElement::AddClass; | |
185 tagNames["store-result"] = StackElement::StoreResult; | |
42 | 186 |
44 | 187 tagNames["depend"] = StackElement::PackageDepend; |
1 | 188 } |
189 | |
190 bool startElement(const QString &namespaceURI, const QString &localName, | |
191 const QString &qName, const QXmlAttributes &atts); | |
192 bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName); | |
193 | |
194 QString errorString() const { return m_error; } | |
195 bool error(const QXmlParseException &exception); | |
196 bool fatalError(const QXmlParseException &exception); | |
197 bool warning(const QXmlParseException &exception); | |
198 | |
199 bool characters(const QString &ch); | |
200 | |
201 private: | |
202 void fetchAttributeValues(const QString &name, const QXmlAttributes &atts, | |
203 QHash<QString, QString> *acceptedAttributes); | |
204 | |
205 bool importFileElement(const QXmlAttributes &atts); | |
206 bool convertBoolean(const QString &, const QString &, bool); | |
207 | |
208 TypeDatabase *m_database; | |
209 StackElement* current; | |
210 QString m_defaultPackage; | |
211 QString m_defaultSuperclass; | |
212 QString m_error; | |
213 TypeEntry::CodeGeneration m_generate; | |
214 | |
215 EnumTypeEntry *m_current_enum; | |
216 | |
217 CodeSnipList m_code_snips; | |
218 FunctionModificationList m_function_mods; | |
219 FieldModificationList m_field_mods; | |
220 | |
221 QHash<QString, StackElement::ElementType> tagNames; | |
4 | 222 |
223 QString m_source_dir; | |
42 | 224 QStringList m_defaultDepends; |
1 | 225 }; |
226 | |
227 bool Handler::error(const QXmlParseException &e) | |
228 { | |
229 qWarning("Error: line=%d, column=%d, message=%s\n", | |
230 e.lineNumber(), e.columnNumber(), qPrintable(e.message())); | |
231 return false; | |
232 } | |
233 | |
234 bool Handler::fatalError(const QXmlParseException &e) | |
235 { | |
236 qWarning("Fatal error: line=%d, column=%d, message=%s\n", | |
237 e.lineNumber(), e.columnNumber(), qPrintable(e.message())); | |
238 | |
239 return false; | |
240 } | |
241 | |
242 bool Handler::warning(const QXmlParseException &e) | |
243 { | |
244 qWarning("Warning: line=%d, column=%d, message=%s\n", | |
245 e.lineNumber(), e.columnNumber(), qPrintable(e.message())); | |
246 | |
247 return false; | |
248 } | |
249 | |
250 void Handler::fetchAttributeValues(const QString &name, const QXmlAttributes &atts, | |
251 QHash<QString, QString> *acceptedAttributes) | |
252 { | |
253 Q_ASSERT(acceptedAttributes != 0); | |
254 | |
255 for (int i=0; i<atts.length(); ++i) { | |
256 QString key = atts.localName(i).toLower(); | |
257 QString val = atts.value(i); | |
258 | |
259 if (!acceptedAttributes->contains(key)) { | |
260 ReportHandler::warning(QString("Unknown attribute for '%1': '%2'").arg(name).arg(key)); | |
261 } else { | |
262 (*acceptedAttributes)[key] = val; | |
263 } | |
264 } | |
265 } | |
266 | |
267 bool Handler::endElement(const QString &, const QString &localName, const QString &) | |
268 { | |
269 QString tagName = localName.toLower(); | |
270 if(tagName == "import-file") | |
271 return true; | |
272 | |
273 if (!current) | |
274 return true; | |
275 | |
276 switch (current->type) { | |
277 case StackElement::ObjectTypeEntry: | |
278 case StackElement::ValueTypeEntry: | |
279 case StackElement::InterfaceTypeEntry: | |
280 case StackElement::NamespaceTypeEntry: | |
281 { | |
282 ComplexTypeEntry *centry = static_cast<ComplexTypeEntry *>(current->entry); | |
283 centry->setFunctionModifications(m_function_mods); | |
284 centry->setFieldModifications(m_field_mods); | |
285 centry->setCodeSnips(m_code_snips); | |
286 | |
287 if (centry->designatedInterface()) { | |
288 centry->designatedInterface()->setCodeSnips(m_code_snips); | |
289 centry->designatedInterface()->setFunctionModifications(m_function_mods); | |
290 } | |
291 m_code_snips = CodeSnipList(); | |
292 m_function_mods = FunctionModificationList(); | |
293 m_field_mods = FieldModificationList(); | |
294 } | |
295 break; | |
296 case StackElement::CustomMetaConstructor: | |
297 { | |
298 current->entry->setCustomConstructor(*current->value.customFunction); | |
299 delete current->value.customFunction; | |
300 } | |
301 break; | |
302 case StackElement::CustomMetaDestructor: | |
303 { | |
304 current->entry->setCustomDestructor(*current->value.customFunction); | |
305 delete current->value.customFunction; | |
306 } | |
307 break; | |
308 case StackElement::EnumTypeEntry: | |
309 m_current_enum = 0; | |
310 break; | |
311 case StackElement::Template: | |
312 m_database->addTemplate(current->value.templateEntry); | |
313 break; | |
314 case StackElement::TemplateInstanceEnum: | |
315 if(current->parent->type == StackElement::InjectCode){ | |
316 m_code_snips.last().addTemplateInstance(current->value.templateInstance); | |
317 }else if(current->parent->type == StackElement::Template){ | |
318 current->parent->value.templateEntry->addTemplateInstance(current->value.templateInstance); | |
319 }else if(current->parent->type == StackElement::CustomMetaConstructor || current->parent->type == StackElement::CustomMetaConstructor){ | |
320 current->parent->value.customFunction->addTemplateInstance(current->value.templateInstance); | |
321 }else if(current->parent->type == StackElement::ConversionRule){ | |
322 m_function_mods.last().argument_mods.last().conversion_rules.last().addTemplateInstance(current->value.templateInstance); | |
323 }else if(current->parent->type == StackElement::InjectCodeInFunction){ | |
324 m_function_mods.last().snips.last().addTemplateInstance(current->value.templateInstance); | |
325 } | |
326 break; | |
327 default: | |
328 break; | |
329 } | |
330 | |
331 StackElement *child = current; | |
332 current=current->parent; | |
333 delete(child); | |
334 | |
335 return true; | |
336 } | |
337 | |
338 bool Handler::characters(const QString &ch) | |
339 { | |
340 if(current->type == StackElement::Template){ | |
341 current->value.templateEntry->addCode(ch); | |
342 return true; | |
343 } | |
344 | |
345 if (current->type == StackElement::CustomMetaConstructor || current->type == StackElement::CustomMetaDestructor){ | |
346 current->value.customFunction->addCode(ch); | |
347 return true; | |
348 } | |
349 | |
350 if (current->type == StackElement::ConversionRule){ | |
351 m_function_mods.last().argument_mods.last().conversion_rules.last().addCode(ch); | |
352 return true; | |
353 } | |
354 | |
355 if (current->parent){ | |
356 if ((current->type & StackElement::CodeSnipMask) != 0) { | |
357 switch (current->parent->type) { | |
358 case StackElement::Root: | |
359 ((TypeSystemTypeEntry *) current->parent->entry)->snips.last().addCode(ch); | |
360 break; | |
361 case StackElement::ModifyFunction: | |
362 m_function_mods.last().snips.last().addCode(ch); | |
363 break; | |
364 case StackElement::NamespaceTypeEntry: | |
365 case StackElement::ObjectTypeEntry: | |
366 case StackElement::ValueTypeEntry: | |
367 case StackElement::InterfaceTypeEntry: | |
368 m_code_snips.last().addCode(ch); | |
369 break; | |
370 default: | |
371 Q_ASSERT(false); | |
372 }; | |
373 return true; | |
374 } | |
375 } | |
376 | |
377 return true; | |
378 } | |
379 | |
380 bool Handler::importFileElement(const QXmlAttributes &atts) | |
381 { | |
382 QString fileName = atts.value("name"); | |
383 if(fileName.isEmpty()){ | |
384 m_error = "Required attribute 'name' missing for include-file tag."; | |
385 return false; | |
386 } | |
387 | |
388 QFile file(fileName); | |
389 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { | |
390 file.setFileName(":/trolltech/generator/" + fileName); | |
391 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { | |
392 m_error = QString("Could not open file: '%1'").arg(fileName); | |
393 return false; | |
394 } | |
395 } | |
396 | |
397 QString quoteFrom = atts.value("quote-after-line"); | |
398 bool foundFromOk = quoteFrom.isEmpty(); | |
399 bool from = quoteFrom.isEmpty(); | |
400 | |
401 QString quoteTo = atts.value("quote-before-line"); | |
402 bool foundToOk = quoteTo.isEmpty(); | |
403 bool to = true; | |
404 | |
405 QTextStream in(&file); | |
406 while (!in.atEnd()) { | |
407 QString line = in.readLine(); | |
408 if(from && to && line.contains(quoteTo)) { | |
409 to = false; | |
410 foundToOk = true; | |
411 break; | |
412 } | |
413 if(from && to) | |
414 characters(line + "\n"); | |
415 if(!from && line.contains(quoteFrom)) { | |
416 from = true; | |
417 foundFromOk = true; | |
418 } | |
419 } | |
420 if(!foundFromOk || !foundToOk){ | |
421 QString fromError = QString("Could not find quote-after-line='%1' in file '%2'.").arg(quoteFrom).arg(fileName); | |
422 QString toError = QString("Could not find quote-before-line='%1' in file '%2'.").arg(quoteTo).arg(fileName); | |
423 | |
424 if(!foundToOk) | |
425 m_error = toError; | |
426 if(!foundFromOk) | |
427 m_error = fromError; | |
428 if(!foundFromOk && !foundToOk) | |
429 m_error = fromError + " " + toError; | |
430 return false; | |
431 } | |
432 | |
433 return true; | |
434 } | |
435 | |
436 bool Handler::convertBoolean(const QString &_value, const QString &attributeName, bool defaultValue) | |
437 { | |
438 QString value = _value.toLower(); | |
439 if (value == "true" || value == "yes") { | |
440 return true; | |
441 } else if (value == "false" || value == "no") { | |
442 return false; | |
443 } else { | |
444 QString warn = QString("Boolean value '%1' not supported in attribute '%2'. Use 'yes' or 'no'. Defaulting to '%3'.") | |
445 .arg(value).arg(attributeName).arg(defaultValue ? "yes" : "no"); | |
446 | |
447 ReportHandler::warning(warn); | |
448 return defaultValue; | |
449 } | |
450 } | |
451 | |
452 bool Handler::startElement(const QString &, const QString &n, | |
453 const QString &, const QXmlAttributes &atts) | |
454 { | |
455 QString tagName = n.toLower(); | |
456 if(tagName == "import-file"){ | |
457 return importFileElement(atts); | |
458 } | |
459 | |
460 StackElement *element = new StackElement(current); | |
461 | |
462 if (!tagNames.contains(tagName)) { | |
463 m_error = QString("Unknown tag name: '%1'").arg(tagName); | |
464 return false; | |
465 } | |
466 | |
467 element->type = tagNames[tagName]; | |
468 if (element->type & StackElement::TypeEntryMask) { | |
469 if (current->type != StackElement::Root) { | |
470 m_error = "Nested types not supported"; | |
471 return false; | |
472 } | |
473 | |
474 QHash<QString, QString> attributes; | |
475 attributes["name"] = QString(); | |
476 | |
477 switch (element->type) { | |
478 case StackElement::PrimitiveTypeEntry: | |
479 attributes["java-name"] = QString(); | |
480 attributes["jni-name"] = QString(); | |
481 attributes["preferred-conversion"] = "yes"; | |
482 attributes["preferred-java-type"] = "yes"; | |
483 break; | |
484 case StackElement::EnumTypeEntry: | |
485 attributes["flags"] = "no"; | |
486 attributes["upper-bound"] = QString(); | |
487 attributes["lower-bound"] = QString(); | |
488 attributes["force-integer"] = "no"; | |
489 attributes["extensible"] = "no"; | |
490 | |
491 break; | |
492 | |
493 case StackElement::ObjectTypeEntry: | |
494 case StackElement::ValueTypeEntry: | |
495 attributes["force-abstract"] = QString("no"); | |
496 attributes["deprecated"] = QString("no"); | |
497 attributes["wrap"] = QString(); | |
498 // fall throooough | |
499 case StackElement::InterfaceTypeEntry: | |
500 attributes["default-superclass"] = m_defaultSuperclass; | |
501 attributes["polymorphic-id-expression"] = QString(); | |
502 attributes["delete-in-main-thread"] = QString("no"); | |
503 // fall through | |
504 case StackElement::NamespaceTypeEntry: | |
505 attributes["java-name"] = QString(); | |
506 attributes["package"] = m_defaultPackage; | |
507 attributes["expense-cost"] = "1"; | |
508 attributes["expense-limit"] = "none"; | |
509 attributes["polymorphic-base"] = QString("no"); | |
510 attributes["generate"] = QString("yes"); | |
511 attributes["target-type"] = QString(); | |
512 attributes["generic-class"] = QString("no"); | |
513 break; | |
514 default: | |
515 ; // nada | |
516 }; | |
517 | |
518 fetchAttributeValues(tagName, atts, &attributes); | |
519 | |
520 QString name = attributes["name"]; | |
521 | |
522 // We need to be able to have duplicate primitive type entries, or it's not possible to | |
523 // cover all primitive java types (which we need to do in order to support fake | |
524 // meta objects) | |
525 if (element->type != StackElement::PrimitiveTypeEntry) { | |
526 TypeEntry *tmp = m_database->findType(name); | |
527 if (tmp != 0) { | |
528 ReportHandler::warning(QString("Duplicate type entry: '%1'").arg(name)); | |
529 } | |
530 } | |
531 | |
532 if (name.isEmpty()) { | |
533 m_error = "no 'name' attribute specified"; | |
534 return false; | |
535 } | |
536 switch (element->type) { | |
537 case StackElement::PrimitiveTypeEntry: | |
538 { | |
539 QString java_name = attributes["java-name"]; | |
540 QString jni_name = attributes["jni-name"]; | |
541 QString preferred_conversion = attributes["preferred-conversion"].toLower(); | |
542 QString preferred_java_type = attributes["preferred-java-type"].toLower(); | |
543 | |
544 if (java_name.isEmpty()) | |
545 java_name = name; | |
546 if (jni_name.isEmpty()) | |
547 jni_name = name; | |
548 | |
549 PrimitiveTypeEntry *type = new PrimitiveTypeEntry(name); | |
550 type->setCodeGeneration(m_generate); | |
551 type->setTargetLangName(java_name); | |
552 type->setJniName(jni_name); | |
553 | |
554 type->setPreferredConversion(convertBoolean(preferred_conversion, "preferred-conversion", true)); | |
555 type->setPreferredTargetLangType(convertBoolean(preferred_java_type, "preferred-java-type", true)); | |
556 | |
557 element->entry = type; | |
558 } | |
559 break; | |
560 case StackElement::EnumTypeEntry: { | |
561 QStringList names = name.split(QLatin1String("::")); | |
562 | |
563 if (names.size() == 1) { | |
564 m_current_enum = new EnumTypeEntry(QString(), name); | |
565 } | |
566 else | |
567 m_current_enum = | |
568 new EnumTypeEntry(QStringList(names.mid(0, names.size() - 1)).join("::"), | |
569 names.last()); | |
570 element->entry = m_current_enum; | |
571 m_current_enum->setCodeGeneration(m_generate); | |
572 m_current_enum->setTargetLangPackage(m_defaultPackage); | |
42 | 573 //m_current_enum->setDepends(m_defaultDepends); // For enums its not needed? |
1 | 574 m_current_enum->setUpperBound(attributes["upper-bound"]); |
575 m_current_enum->setLowerBound(attributes["lower-bound"]); | |
576 m_current_enum->setForceInteger(convertBoolean(attributes["force-integer"], "force-integer", false)); | |
577 m_current_enum->setExtensible(convertBoolean(attributes["extensible"], "extensible", false)); | |
578 | |
579 // put in the flags parallel... | |
580 if (!attributes["flags"].isEmpty() && attributes["flags"].toLower() != "no") { | |
581 FlagsTypeEntry *ftype = new FlagsTypeEntry("QFlags<" + name + ">"); | |
582 ftype->setOriginator(m_current_enum); | |
583 ftype->setOriginalName(attributes["flags"]); | |
584 ftype->setCodeGeneration(m_generate); | |
585 QString n = ftype->originalName(); | |
586 | |
587 QStringList lst = n.split("::"); | |
588 if (QStringList(lst.mid(0, lst.size() - 1)).join("::") != m_current_enum->javaQualifier()) { | |
589 ReportHandler::warning(QString("enum %1 and flags %2 differ in qualifiers") | |
590 .arg(m_current_enum->javaQualifier()) | |
591 .arg(lst.at(0))); | |
592 } | |
593 | |
594 ftype->setFlagsName(lst.last()); | |
595 m_current_enum->setFlags(ftype); | |
596 | |
597 m_database->addFlagsType(ftype); | |
598 m_database->addType(ftype); | |
599 } | |
600 } | |
601 break; | |
602 | |
603 case StackElement::InterfaceTypeEntry: | |
604 { | |
605 ObjectTypeEntry *otype = new ObjectTypeEntry(name); | |
606 QString javaName = attributes["java-name"]; | |
607 if (javaName.isEmpty()) | |
608 javaName = name; | |
609 InterfaceTypeEntry *itype = | |
610 new InterfaceTypeEntry(InterfaceTypeEntry::interfaceName(javaName)); | |
611 | |
612 if (!convertBoolean(attributes["generate"], "generate", true)) | |
613 itype->setCodeGeneration(TypeEntry::GenerateForSubclass); | |
614 else | |
615 itype->setCodeGeneration(m_generate); | |
616 otype->setDesignatedInterface(itype); | |
617 itype->setOrigin(otype); | |
618 element->entry = otype; | |
619 } | |
620 // fall through | |
621 case StackElement::NamespaceTypeEntry: | |
622 if (element->entry == 0) { | |
623 element->entry = new NamespaceTypeEntry(name); | |
624 } | |
625 // fall through | |
626 case StackElement::ObjectTypeEntry: | |
627 if (element->entry == 0) { | |
628 element->entry = new ObjectTypeEntry(name); | |
629 } | |
630 // fall through | |
631 case StackElement::ValueTypeEntry: | |
632 { | |
633 if (element->entry == 0) { | |
634 if(name == "QVariant") | |
635 element->entry = new VariantTypeEntry(name); | |
636 else | |
637 element->entry = new ValueTypeEntry(name); | |
638 } | |
639 | |
640 ComplexTypeEntry *ctype = static_cast<ComplexTypeEntry *>(element->entry); | |
641 ctype->setTargetLangPackage(attributes["package"]); | |
42 | 642 //ctype->setDepends(m_defaultDepends); |
1 | 643 ctype->setDefaultSuperclass(attributes["default-superclass"]); |
644 ctype->setGenericClass(convertBoolean(attributes["generic-class"], "generic-class", false)); | |
645 | |
646 // qtd | |
647 QString wrap = attributes["wrap"]; | |
648 ctype->setStructInD(wrap == "struct" ? true : false); | |
649 // | |
650 | |
651 if (!convertBoolean(attributes["generate"], "generate", true)) | |
652 element->entry->setCodeGeneration(TypeEntry::GenerateForSubclass); | |
653 else | |
654 element->entry->setCodeGeneration(m_generate); | |
655 | |
656 QString javaName = attributes["java-name"]; | |
657 if (!javaName.isEmpty()) | |
658 ctype->setTargetLangName(javaName); | |
659 | |
660 // The expense policy | |
661 QString limit = attributes["expense-limit"]; | |
662 if (!limit.isEmpty() && limit != "none") { | |
663 ExpensePolicy ep; | |
664 ep.limit = limit.toInt(); | |
665 ep.cost = attributes["expense-cost"]; | |
666 ctype->setExpensePolicy(ep); | |
667 } | |
668 | |
669 ctype->setIsPolymorphicBase(convertBoolean(attributes["polymorphic-base"], "polymorphic-base", false)); | |
670 ctype->setPolymorphicIdValue(attributes["polymorphic-id-expression"]); | |
671 | |
672 if (element->type == StackElement::ObjectTypeEntry || element->type == StackElement::ValueTypeEntry) { | |
673 if (convertBoolean(attributes["force-abstract"], "force-abstract", false)) | |
674 ctype->setTypeFlags(ctype->typeFlags() | ComplexTypeEntry::ForceAbstract); | |
675 if (convertBoolean(attributes["deprecated"], "deprecated", false)) | |
676 ctype->setTypeFlags(ctype->typeFlags() | ComplexTypeEntry::Deprecated); | |
677 } | |
678 | |
679 if (element->type == StackElement::InterfaceTypeEntry || | |
680 element->type == StackElement::ValueTypeEntry || | |
681 element->type == StackElement::ObjectTypeEntry) { | |
682 if (convertBoolean(attributes["delete-in-main-thread"], "delete-in-main-thread", false)) | |
683 ctype->setTypeFlags(ctype->typeFlags() | ComplexTypeEntry::DeleteInMainThread); | |
684 } | |
685 | |
686 QString targetType = attributes["target-type"]; | |
687 if (!targetType.isEmpty() && element->entry->isComplex()) | |
688 static_cast<ComplexTypeEntry *>(element->entry)->setTargetType(targetType); | |
689 | |
690 // ctype->setInclude(Include(Include::IncludePath, ctype->name())); | |
691 ctype = ctype->designatedInterface(); | |
692 if (ctype != 0) | |
42 | 693 { |
1 | 694 ctype->setTargetLangPackage(attributes["package"]); |
42 | 695 ctype->setDepends(m_defaultDepends); |
696 } | |
1 | 697 } |
698 break; | |
699 default: | |
700 Q_ASSERT(false); | |
701 }; | |
702 | |
703 if (element->entry) | |
704 m_database->addType(element->entry); | |
705 else | |
706 ReportHandler::warning(QString("Type: %1 was rejected by typesystem").arg(name)); | |
707 | |
708 } else if (element->type != StackElement::None) { | |
709 bool topLevel = element->type == StackElement::Root | |
710 || element->type == StackElement::SuppressedWarning | |
711 || element->type == StackElement::Rejection | |
712 || element->type == StackElement::LoadTypesystem | |
713 || element->type == StackElement::InjectCode | |
42 | 714 || element->type == StackElement::Template |
715 || element->type == StackElement::PackageDepend; | |
1 | 716 |
717 if (!topLevel && current->type == StackElement::Root) { | |
718 m_error = QString("Tag requires parent: '%1'").arg(tagName); | |
719 return false; | |
720 } | |
721 | |
722 StackElement topElement = current==0 ? StackElement(0) : *current; | |
723 element->entry = topElement.entry; | |
724 | |
725 QHash<QString, QString> attributes; | |
726 switch (element->type) { | |
727 case StackElement::Root: | |
728 attributes["package"] = QString(); | |
729 attributes["default-superclass"] = QString(); | |
730 break; | |
731 case StackElement::LoadTypesystem: | |
732 attributes["name"] = QString(); | |
733 attributes["generate"] = "yes"; | |
734 break; | |
735 case StackElement::NoNullPointers: | |
736 attributes["default-value"] = QString(); | |
737 break; | |
738 case StackElement::SuppressedWarning: | |
739 attributes["text"] = QString(); | |
740 break; | |
741 case StackElement::ReplaceDefaultExpression: | |
742 attributes["with"] = QString(); | |
743 break; | |
744 case StackElement::DefineOwnership: | |
745 attributes["class"] = "java"; | |
746 attributes["owner"] = ""; | |
747 break; | |
748 case StackElement::ModifyFunction: | |
749 attributes["signature"] = QString(); | |
750 attributes["access"] = QString(); | |
751 attributes["remove"] = QString(); | |
752 attributes["rename"] = QString(); | |
753 attributes["deprecated"] = QString("no"); | |
754 attributes["associated-to"] = QString(); | |
755 attributes["virtual-slot"] = QString("no"); | |
148
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
44
diff
changeset
|
756 attributes["allow-as-slot"] = QString("no"); |
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
44
diff
changeset
|
757 attributes["private-signal"] = QString("no"); |
1 | 758 break; |
759 case StackElement::ModifyArgument: | |
760 attributes["index"] = QString(); | |
761 attributes["replace-value"] = QString(); | |
762 attributes["invalidate-after-use"] = QString("no"); | |
763 break; | |
764 case StackElement::ModifyField: | |
765 attributes["name"] = QString(); | |
766 attributes["write"] = "true"; | |
767 attributes["read"] = "true"; | |
768 break; | |
769 case StackElement::Access: | |
770 attributes["modifier"] = QString(); | |
771 break; | |
772 case StackElement::Include: | |
773 attributes["file-name"] = QString(); | |
774 attributes["location"] = QString(); | |
775 break; | |
776 case StackElement::CustomMetaConstructor: | |
777 attributes["name"] = topElement.entry->name().toLower() + "_create"; | |
778 attributes["param-name"] = "copy"; | |
779 break; | |
780 case StackElement::CustomMetaDestructor: | |
781 attributes["name"] = topElement.entry->name().toLower() + "_delete"; | |
782 attributes["param-name"] = "copy"; | |
783 break; | |
784 case StackElement::ReplaceType: | |
785 attributes["modified-type"] = QString(); | |
786 break; | |
787 case StackElement::InjectCode: | |
788 attributes["class"] = "java"; | |
789 attributes["position"] = "beginning"; | |
790 break; | |
791 case StackElement::ConversionRule: | |
792 attributes["class"] = ""; | |
793 break; | |
794 case StackElement::RejectEnumValue: | |
795 attributes["name"] = ""; | |
796 break; | |
797 case StackElement::ArgumentMap: | |
798 attributes["index"] = "1"; | |
799 attributes["meta-name"] = QString(); | |
800 break; | |
801 case StackElement::Rename: | |
802 attributes["to"] = QString(); | |
803 break; | |
804 case StackElement::Rejection: | |
805 attributes["class"] = "*"; | |
806 attributes["function-name"] = "*"; | |
807 attributes["field-name"] = "*"; | |
808 attributes["enum-name"] = "*"; | |
809 break; | |
810 case StackElement::Removal: | |
811 attributes["class"] = "all"; | |
812 break; | |
813 case StackElement::Template: | |
814 attributes["name"] = QString(); | |
815 break; | |
816 case StackElement::TemplateInstanceEnum: | |
817 attributes["name"] = QString(); | |
818 break; | |
819 case StackElement::Replace: | |
820 attributes["from"] = QString(); | |
821 attributes["to"] = QString(); | |
822 break; | |
823 case StackElement::ReferenceCount: | |
824 attributes["action"] = QString(); | |
825 attributes["variable-name"] = QString(); | |
826 attributes["thread-safe"] = QString("no"); | |
827 attributes["declare-variable"] = QString(); | |
828 attributes["access"] = QString("private"); | |
829 attributes["conditional"] = QString(""); | |
830 break; | |
831 // qtd | |
832 case StackElement::AddClass: | |
833 attributes["name"] = QString(); | |
834 break; | |
42 | 835 case StackElement::PackageDepend: |
836 attributes["package"] = QString(); | |
837 break; | |
1 | 838 default: |
839 ; // nada | |
840 }; | |
841 | |
842 if (attributes.count() > 0) | |
843 fetchAttributeValues(tagName, atts, &attributes); | |
844 | |
845 switch (element->type) { | |
846 case StackElement::Root: | |
847 m_defaultPackage = attributes["package"]; | |
848 m_defaultSuperclass = attributes["default-superclass"]; | |
849 element->type = StackElement::Root; | |
850 element->entry = new TypeSystemTypeEntry(m_defaultPackage); | |
851 TypeDatabase::instance()->addType(element->entry); | |
852 break; | |
853 case StackElement::LoadTypesystem: | |
854 { | |
855 QString name = attributes["name"]; | |
856 if (name.isEmpty()) { | |
857 m_error = "No typesystem name specified"; | |
858 return false; | |
859 } | |
860 | |
4 | 861 if (!m_database->parseFile(name, m_source_dir, convertBoolean(attributes["generate"], "generate", true))) { |
1 | 862 m_error = QString("Failed to parse: '%1'").arg(name); |
863 return false; | |
864 } | |
865 } | |
866 break; | |
867 case StackElement::RejectEnumValue: { | |
868 if (!m_current_enum) { | |
869 m_error = "<reject-enum-value> node must be used inside a <enum-type> node"; | |
870 return false; | |
871 } | |
872 QString name = attributes["name"]; | |
873 | |
874 bool added = false; | |
875 if (!name.isEmpty()) { | |
876 added = true; | |
877 m_current_enum->addEnumValueRejection(name); | |
878 } | |
879 | |
880 } break; | |
881 case StackElement::ReplaceType: | |
882 { | |
883 if (topElement.type != StackElement::ModifyArgument) { | |
884 m_error = "Type replacement can only be specified for argument modifications"; | |
885 return false; | |
886 } | |
887 | |
888 if (attributes["modified-type"].isEmpty()) { | |
889 m_error = "Type replacement requires 'modified-type' attribute"; | |
890 return false; | |
891 } | |
892 | |
893 m_function_mods.last().argument_mods.last().modified_type = attributes["modified-type"]; | |
894 } | |
895 break; | |
896 case StackElement::ConversionRule: | |
897 { | |
898 if (topElement.type != StackElement::ModifyArgument) { | |
899 m_error = "Conversion rules can only be specified for argument modification"; | |
900 return false; | |
901 } | |
902 | |
903 static QHash<QString, TypeSystem::Language> languageNames; | |
904 if (languageNames.isEmpty()) { | |
905 languageNames["native"] = TypeSystem::NativeCode; | |
906 languageNames["shell"] = TypeSystem::ShellCode; | |
907 } | |
908 | |
909 CodeSnip snip; | |
910 QString languageAttribute = attributes["class"].toLower(); | |
911 TypeSystem::Language lang = languageNames.value(languageAttribute, TypeSystem::NoLanguage); | |
912 if (lang == TypeSystem::NoLanguage) { | |
913 m_error = QString("unsupported class attribute: '%1'").arg(languageAttribute); | |
914 return false; | |
915 } | |
916 | |
917 snip.language = lang; | |
918 m_function_mods.last().argument_mods.last().conversion_rules.append(snip); | |
919 } | |
920 | |
921 break; | |
922 case StackElement::StoreResult: | |
923 { | |
924 if (topElement.type != StackElement::ModifyFunction) { | |
925 m_error = QString::fromLatin1("result storage requires function" | |
926 " modification as parent, was %1") | |
927 .arg(topElement.type, 0, 16); | |
928 return false; | |
929 } | |
930 | |
931 m_function_mods.last().store_result = true; | |
932 } | |
933 break; | |
934 case StackElement::ModifyArgument: | |
935 { | |
936 if (topElement.type != StackElement::ModifyFunction) { | |
937 m_error = QString::fromLatin1("argument modification requires function" | |
938 " modification as parent, was %1") | |
939 .arg(topElement.type, 0, 16); | |
940 return false; | |
941 } | |
942 | |
943 QString index = attributes["index"]; | |
944 if (index == "return") | |
945 index = "0"; | |
946 else if (index == "this") | |
947 index = "-1"; | |
948 | |
949 bool ok = false; | |
950 int idx = index.toInt(&ok); | |
951 if (!ok) { | |
952 m_error = QString("Cannot convert '%1' to integer").arg(index); | |
953 return false; | |
954 } | |
955 | |
956 QString replace_value = attributes["replace-value"]; | |
957 | |
958 if (!replace_value.isEmpty() && idx != 0) { | |
959 m_error = QString("replace-value is only supported for return values (index=0)."); | |
960 return false; | |
961 } | |
962 | |
963 ArgumentModification argumentModification = ArgumentModification(idx); | |
964 argumentModification.replace_value = replace_value; | |
965 argumentModification.reset_after_use = convertBoolean(attributes["invalidate-after-use"], "invalidate-after-use", false); | |
966 m_function_mods.last().argument_mods.append(argumentModification); | |
967 } | |
968 break; | |
969 case StackElement::NoNullPointers: | |
970 { | |
971 if (topElement.type != StackElement::ModifyArgument) { | |
972 m_error = "no-null-pointer requires argument modification as parent"; | |
973 return false; | |
974 } | |
975 | |
976 m_function_mods.last().argument_mods.last().no_null_pointers = true; | |
977 if (m_function_mods.last().argument_mods.last().index == 0) { | |
978 m_function_mods.last().argument_mods.last().null_pointer_default_value = attributes["default-value"]; | |
979 } else if (!attributes["default-value"].isEmpty()) { | |
980 ReportHandler::warning("default values for null pointer guards are only effective for return values"); | |
981 } | |
982 } | |
983 break; | |
984 case StackElement::DefineOwnership: | |
985 { | |
986 if (topElement.type != StackElement::ModifyArgument) { | |
987 m_error = "define-ownership requires argument modification as parent"; | |
988 return false; | |
989 } | |
990 | |
991 static QHash<QString, TypeSystem::Language> languageNames; | |
992 if (languageNames.isEmpty()) { | |
993 languageNames["java"] = TypeSystem::TargetLangCode; | |
994 languageNames["shell"] = TypeSystem::ShellCode; | |
995 } | |
996 | |
997 QString classAttribute = attributes["class"].toLower(); | |
998 TypeSystem::Language lang = languageNames.value(classAttribute, TypeSystem::NoLanguage); | |
999 if (lang == TypeSystem::NoLanguage) { | |
1000 m_error = QString("unsupported class attribute: '%1'").arg(classAttribute); | |
1001 return false; | |
1002 } | |
1003 | |
1004 static QHash<QString, TypeSystem::Ownership> ownershipNames; | |
1005 if (ownershipNames.isEmpty()) { | |
1006 ownershipNames["java"] = TypeSystem::TargetLangOwnership; | |
1007 ownershipNames["c++"] = TypeSystem::CppOwnership; | |
1008 ownershipNames["default"] = TypeSystem::DefaultOwnership; | |
1009 } | |
1010 | |
1011 QString ownershipAttribute = attributes["owner"].toLower(); | |
1012 TypeSystem::Ownership owner = ownershipNames.value(ownershipAttribute, TypeSystem::InvalidOwnership); | |
1013 if (owner == TypeSystem::InvalidOwnership) { | |
1014 m_error = QString("unsupported owner attribute: '%1'").arg(ownershipAttribute); | |
1015 return false; | |
1016 } | |
1017 | |
1018 m_function_mods.last().argument_mods.last().ownerships[lang] = owner; | |
1019 } | |
1020 break; | |
1021 case StackElement::SuppressedWarning: | |
1022 if (attributes["text"].isEmpty()) | |
1023 ReportHandler::warning("Suppressed warning with no text specified"); | |
1024 else | |
1025 m_database->addSuppressedWarning(attributes["text"]); | |
1026 break; | |
1027 case StackElement::ArgumentMap: | |
1028 { | |
1029 if (!(topElement.type & StackElement::CodeSnipMask)) { | |
1030 m_error = "Argument maps requires code injection as parent"; | |
1031 return false; | |
1032 } | |
1033 | |
1034 bool ok; | |
1035 int pos = attributes["index"].toInt(&ok); | |
1036 if (!ok) { | |
1037 m_error = QString("Can't convert position '%1' to integer") | |
1038 .arg(attributes["position"]); | |
1039 return false; | |
1040 } | |
1041 | |
1042 if (pos <= 0) { | |
1043 m_error = QString("Argument position %1 must be a positive number").arg(pos); | |
1044 return false; | |
1045 } | |
1046 | |
1047 QString meta_name = attributes["meta-name"]; | |
1048 if (meta_name.isEmpty()) { | |
1049 ReportHandler::warning("Empty meta name in argument map"); | |
1050 } | |
1051 | |
1052 if (topElement.type == StackElement::InjectCodeInFunction) { | |
1053 m_function_mods.last().snips.last().argumentMap[pos] = meta_name; | |
1054 } else { | |
1055 ReportHandler::warning("Argument maps are only useful for injection of code " | |
1056 "into functions."); | |
1057 } | |
1058 } | |
1059 break; | |
1060 case StackElement::Removal: | |
1061 { | |
1062 if (topElement.type != StackElement::ModifyFunction) { | |
1063 m_error = "Function modification parent required"; | |
1064 return false; | |
1065 } | |
1066 | |
1067 static QHash<QString, TypeSystem::Language> languageNames; | |
1068 if (languageNames.isEmpty()) { | |
1069 languageNames["java"] = TypeSystem::TargetLangAndNativeCode; | |
1070 languageNames["all"] = TypeSystem::All; | |
1071 } | |
1072 | |
1073 QString languageAttribute = attributes["class"].toLower(); | |
1074 TypeSystem::Language lang = languageNames.value(languageAttribute, TypeSystem::NoLanguage); | |
1075 if (lang == TypeSystem::NoLanguage) { | |
1076 m_error = QString("unsupported class attribute: '%1'").arg(languageAttribute); | |
1077 return false; | |
1078 } | |
1079 | |
1080 m_function_mods.last().removal = lang; | |
1081 } | |
1082 break; | |
1083 case StackElement::Rename: | |
1084 case StackElement::Access: | |
1085 { | |
1086 if (topElement.type != StackElement::ModifyField | |
1087 && topElement.type != StackElement::ModifyFunction) { | |
1088 m_error = "Function or field modification parent required"; | |
1089 return false; | |
1090 } | |
1091 | |
1092 Modification *mod = 0; | |
1093 if (topElement.type == StackElement::ModifyFunction) | |
1094 mod = &m_function_mods.last(); | |
1095 else | |
1096 mod = &m_field_mods.last(); | |
1097 | |
1098 QString modifier; | |
1099 if (element->type == StackElement::Rename) { | |
1100 modifier = "rename"; | |
1101 QString renamed_to = attributes["to"]; | |
1102 if (renamed_to.isEmpty()) { | |
1103 m_error = "Rename modifier requires 'to' attribute"; | |
1104 return false; | |
1105 } | |
1106 | |
1107 if (topElement.type == StackElement::ModifyFunction) | |
1108 mod->setRenamedTo(renamed_to); | |
1109 else | |
1110 mod->setRenamedTo(renamed_to); | |
1111 } else { | |
1112 modifier = attributes["modifier"].toLower(); | |
1113 } | |
1114 | |
1115 if (modifier.isEmpty()) { | |
1116 m_error = "No access modification specified"; | |
1117 return false; | |
1118 } | |
1119 | |
1120 static QHash<QString, FunctionModification::Modifiers> modifierNames; | |
1121 if (modifierNames.isEmpty()) { | |
1122 modifierNames["private"] = Modification::Private; | |
1123 modifierNames["public"] = Modification::Public; | |
1124 modifierNames["protected"] = Modification::Protected; | |
1125 modifierNames["friendly"] = Modification::Friendly; | |
1126 modifierNames["rename"] = Modification::Rename; | |
1127 modifierNames["final"] = Modification::Final; | |
1128 modifierNames["non-final"] = Modification::NonFinal; | |
1129 } | |
1130 | |
1131 if (!modifierNames.contains(modifier)) { | |
1132 m_error = QString("Unknown access modifier: '%1'").arg(modifier); | |
1133 return false; | |
1134 } | |
1135 | |
1136 mod->modifiers |= modifierNames[modifier]; | |
1137 } | |
1138 break; | |
1139 case StackElement::RemoveArgument: | |
1140 if (topElement.type != StackElement::ModifyArgument) { | |
1141 m_error = "Removing argument requires argument modification as parent"; | |
1142 return false; | |
1143 } | |
1144 | |
1145 m_function_mods.last().argument_mods.last().removed = true; | |
1146 | |
1147 break; | |
1148 | |
1149 case StackElement::ModifyField: | |
1150 { | |
1151 QString name = attributes["name"]; | |
1152 if (name.isEmpty()) | |
1153 break; | |
1154 FieldModification fm; | |
1155 fm.name = name; | |
1156 fm.modifiers = 0; | |
1157 | |
1158 QString read = attributes["read"]; | |
1159 QString write = attributes["write"]; | |
1160 | |
1161 if (read == "true") fm.modifiers |= FieldModification::Readable; | |
1162 if (write == "true") fm.modifiers |= FieldModification::Writable; | |
1163 | |
1164 m_field_mods << fm; | |
1165 } | |
1166 break; | |
1167 case StackElement::AddClass: // qtd - fully :) | |
1168 { | |
1169 if (!(topElement.type & StackElement::ComplexTypeEntryMask)) { | |
1170 m_error = QString::fromLatin1("Add class to module requires complex type as parent" | |
1171 ", was=%1").arg(topElement.type, 0, 16); | |
1172 return false; | |
1173 } | |
1174 QString class_name = attributes["name"]; | |
1175 ComplexTypeEntry *ctype = static_cast<ComplexTypeEntry *>(topElement.entry); | |
1176 if(!class_name.isEmpty()) | |
1177 ctype->includedClasses << class_name; | |
1178 } | |
1179 break; | |
1180 case StackElement::ModifyFunction: | |
1181 { | |
1182 if (!(topElement.type & StackElement::ComplexTypeEntryMask)) { | |
1183 m_error = QString::fromLatin1("Modify function requires complex type as parent" | |
1184 ", was=%1").arg(topElement.type, 0, 16); | |
1185 return false; | |
1186 } | |
1187 QString signature = attributes["signature"]; | |
1188 | |
1189 signature = QMetaObject::normalizedSignature(signature.toLocal8Bit().constData()); | |
1190 if (signature.isEmpty()) { | |
1191 m_error = "No signature for modified function"; | |
1192 return false; | |
1193 } | |
1194 | |
1195 FunctionModification mod; | |
1196 mod.signature = signature; | |
1197 | |
1198 QString access = attributes["access"].toLower(); | |
1199 if (!access.isEmpty()) { | |
1200 if (access == QLatin1String("private")) | |
1201 mod.modifiers |= Modification::Private; | |
1202 else if (access == QLatin1String("protected")) | |
1203 mod.modifiers |= Modification::Protected; | |
1204 else if (access == QLatin1String("public")) | |
1205 mod.modifiers |= Modification::Public; | |
1206 else if (access == QLatin1String("final")) | |
1207 mod.modifiers |= Modification::Final; | |
1208 else if (access == QLatin1String("non-final")) | |
1209 mod.modifiers |= Modification::NonFinal; | |
1210 else { | |
1211 m_error = QString::fromLatin1("Bad access type '%1'").arg(access); | |
1212 return false; | |
1213 } | |
1214 } | |
1215 | |
1216 if (convertBoolean(attributes["deprecated"], "deprecated", false)) { | |
1217 mod.modifiers |= Modification::Deprecated; | |
1218 } | |
1219 | |
1220 QString remove = attributes["remove"].toLower(); | |
1221 if (!remove.isEmpty()) { | |
1222 if (remove == QLatin1String("all")) | |
1223 mod.removal = TypeSystem::All; | |
1224 else if (remove == QLatin1String("java")) | |
1225 mod.removal = TypeSystem::TargetLangAndNativeCode; | |
1226 else { | |
1227 m_error = QString::fromLatin1("Bad removal type '%1'").arg(remove); | |
1228 return false; | |
1229 } | |
1230 } | |
1231 | |
1232 QString rename = attributes["rename"]; | |
1233 if (!rename.isEmpty()) { | |
1234 mod.renamedToName = rename; | |
1235 mod.modifiers |= Modification::Rename; | |
1236 } | |
1237 | |
1238 QString association = attributes["associated-to"]; | |
1239 if (!association.isEmpty()) | |
1240 mod.association = association; | |
1241 | |
1242 mod.modifiers |= (convertBoolean(attributes["virtual-slot"], "virtual-slot", false) ? Modification::VirtualSlot : 0); | |
148
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
44
diff
changeset
|
1243 mod.modifiers |= (convertBoolean(attributes["allow-as-slot"], "allow-as-slot", false) ? Modification::AllowAsSlot : 0); |
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
44
diff
changeset
|
1244 mod.modifiers |= (convertBoolean(attributes["private-signal"], "private-signal", false) ? Modification::PrivateSignal : 0); |
1 | 1245 |
1246 m_function_mods << mod; | |
1247 } | |
1248 break; | |
1249 case StackElement::ReplaceDefaultExpression: | |
1250 if (!(topElement.type & StackElement::ModifyArgument)) { | |
1251 m_error = "Replace default expression only allowed as child of argument modification"; | |
1252 return false; | |
1253 } | |
1254 | |
1255 if (attributes["with"].isEmpty()) { | |
1256 m_error = "Default expression replaced with empty string. Use remove-default-expression instead."; | |
1257 return false; | |
1258 } | |
1259 | |
1260 m_function_mods.last().argument_mods.last().replaced_default_expression = attributes["with"]; | |
1261 break; | |
1262 case StackElement::RemoveDefaultExpression: | |
1263 m_function_mods.last().argument_mods.last().removed_default_expression = true; | |
1264 break; | |
1265 case StackElement::CustomMetaConstructor: | |
1266 case StackElement::CustomMetaDestructor: | |
1267 { | |
1268 CustomFunction *func = new CustomFunction(attributes["name"]); | |
1269 func->param_name = attributes["param-name"]; | |
1270 element->value.customFunction = func; | |
1271 } | |
1272 break; | |
1273 case StackElement::ReferenceCount: | |
1274 { | |
1275 if (topElement.type != StackElement::ModifyArgument) { | |
1276 m_error = "reference-count must be child of modify-argument"; | |
1277 return false; | |
1278 } | |
1279 | |
1280 ReferenceCount rc; | |
1281 rc.threadSafe = convertBoolean(attributes["thread-safe"], "thread-safe", false); | |
1282 | |
1283 static QHash<QString, ReferenceCount::Action> actions; | |
1284 if (actions.isEmpty()) { | |
1285 actions["add"] = ReferenceCount::Add; | |
1286 actions["add-all"] = ReferenceCount::AddAll; | |
1287 actions["remove"] = ReferenceCount::Remove; | |
1288 actions["set"] = ReferenceCount::Set; | |
1289 actions["ignore"] = ReferenceCount::Ignore; | |
1290 } | |
1291 rc.action = actions.value(attributes["action"].toLower(), ReferenceCount::Invalid); | |
1292 | |
1293 rc.variableName = attributes["variable-name"]; | |
1294 if (rc.action != ReferenceCount::Ignore && rc.variableName.isEmpty()) { | |
1295 m_error = "variable-name attribute must be specified"; | |
1296 return false; | |
1297 } | |
1298 | |
1299 rc.declareVariable = attributes["declare-variable"]; | |
1300 rc.conditional = attributes["conditional"]; | |
1301 | |
1302 static QHash<QString, int> accessRights; | |
1303 if (accessRights.isEmpty()) { | |
1304 accessRights["private"] = ReferenceCount::Private; | |
1305 accessRights["public"] = ReferenceCount::Public; | |
1306 accessRights["protected"] = ReferenceCount::Protected; | |
1307 accessRights["friendly"] = ReferenceCount::Friendly; | |
1308 } | |
1309 rc.access = accessRights.value(attributes["access"].toLower(), 0); | |
1310 if (rc.access == 0) { | |
1311 m_error = "unrecognized access value: " + attributes["access"]; | |
1312 return false; | |
1313 } | |
1314 | |
1315 if (rc.action == ReferenceCount::Invalid) { | |
1316 m_error = "unrecognized value for action attribute. supported actions:"; | |
1317 foreach (QString action, actions.keys()) | |
1318 m_error += " " + action; | |
1319 } | |
1320 | |
1321 m_function_mods.last().argument_mods.last().referenceCounts.append(rc); | |
1322 } | |
1323 break; | |
1324 case StackElement::InjectCode: | |
1325 { | |
1326 if (((topElement.type & StackElement::ComplexTypeEntryMask) == 0) | |
1327 && (topElement.type != StackElement::ModifyFunction) | |
1328 && (topElement.type != StackElement::Root)) { | |
1329 m_error = "wrong parent type for code injection"; | |
1330 return false; | |
1331 } | |
1332 | |
1333 static QHash<QString, TypeSystem::Language> languageNames; | |
1334 if (languageNames.isEmpty()) { | |
1335 languageNames["java"] = TypeSystem::TargetLangCode; | |
14 | 1336 languageNames["java-free"] = TypeSystem::TargetLangFreeCode; |
1 | 1337 languageNames["native"] = TypeSystem::NativeCode; |
1338 languageNames["shell"] = TypeSystem::ShellCode; | |
1339 languageNames["shell-declaration"] = TypeSystem::ShellDeclaration; | |
1340 languageNames["library-initializer"] = TypeSystem::PackageInitializer; | |
1341 languageNames["destructor-function"] = TypeSystem::DestructorFunction; | |
1342 languageNames["constructors"] = TypeSystem::Constructors; | |
1343 languageNames["interface"] = TypeSystem::Interface; | |
1344 } | |
1345 | |
1346 QString className = attributes["class"].toLower(); | |
1347 if (!languageNames.contains(className)) { | |
1348 m_error = QString("Invalid class specifier: '%1'").arg(className); | |
1349 return false; | |
1350 } | |
1351 | |
1352 | |
1353 static QHash<QString, CodeSnip::Position> positionNames; | |
1354 if (positionNames.isEmpty()) { | |
1355 positionNames["beginning"] = CodeSnip::Beginning; | |
1356 positionNames["end"] = CodeSnip::End; | |
1357 } | |
1358 | |
1359 QString position = attributes["position"].toLower(); | |
1360 if (!positionNames.contains(position)) { | |
1361 m_error = QString("Invalid position: '%1'").arg(position); | |
1362 return false; | |
1363 } | |
1364 | |
1365 CodeSnip snip; | |
1366 snip.language = languageNames[className]; | |
1367 snip.position = positionNames[position]; | |
1368 | |
1369 if (snip.language == TypeSystem::Interface && topElement.type != StackElement::InterfaceTypeEntry) { | |
1370 m_error = "Interface code injections must be direct child of an interface type entry"; | |
1371 return false; | |
1372 } | |
1373 | |
1374 if (topElement.type == StackElement::ModifyFunction) { | |
1375 FunctionModification mod = m_function_mods.last(); | |
1376 if (snip.language == TypeSystem::ShellDeclaration) { | |
1377 m_error = "no function implementation in shell declaration in which to inject code"; | |
1378 return false; | |
1379 } | |
1380 | |
1381 m_function_mods.last().snips << snip; | |
1382 element->type = StackElement::InjectCodeInFunction; | |
1383 } else if (topElement.type == StackElement::Root) { | |
1384 ((TypeSystemTypeEntry *) element->entry)->snips << snip; | |
1385 | |
1386 } else if (topElement.type != StackElement::Root) { | |
1387 m_code_snips << snip; | |
1388 } | |
1389 } | |
1390 break; | |
1391 case StackElement::Include: | |
1392 { | |
1393 QString location = attributes["location"].toLower(); | |
1394 | |
1395 static QHash<QString, Include::IncludeType> locationNames; | |
1396 if (locationNames.isEmpty()) { | |
1397 locationNames["global"] = Include::IncludePath; | |
1398 locationNames["local"] = Include::LocalPath; | |
1399 locationNames["java"] = Include::TargetLangImport; | |
1400 } | |
1401 | |
1402 if (!locationNames.contains(location)) { | |
1403 m_error = QString("Location not recognized: '%1'").arg(location); | |
1404 return false; | |
1405 } | |
1406 | |
1407 Include::IncludeType loc = locationNames[location]; | |
1408 Include inc(loc, attributes["file-name"]); | |
1409 | |
1410 ComplexTypeEntry *ctype = static_cast<ComplexTypeEntry *>(element->entry); | |
1411 if (topElement.type & StackElement::ComplexTypeEntryMask) { | |
1412 ctype->setInclude(inc); | |
1413 } else if (topElement.type == StackElement::ExtraIncludes) { | |
1414 ctype->addExtraInclude(inc); | |
1415 } else { | |
1416 m_error = "Only supported parents are complex types and extra-includes"; | |
1417 return false; | |
1418 } | |
1419 | |
1420 inc = ctype->include(); | |
1421 IncludeList lst = ctype->extraIncludes(); | |
1422 ctype = ctype->designatedInterface(); | |
1423 if (ctype != 0) { | |
1424 ctype->setExtraIncludes(lst); | |
1425 ctype->setInclude(inc); | |
1426 } | |
1427 } | |
1428 break; | |
1429 case StackElement::Rejection: | |
1430 { | |
1431 QString cls = attributes["class"]; | |
1432 QString function = attributes["function-name"]; | |
1433 QString field = attributes["field-name"]; | |
1434 QString enum_ = attributes["enum-name"]; | |
1435 if (cls == "*" && function == "*" && field == "*" && enum_ == "*") { | |
1436 m_error = "bad reject entry, neither 'class', 'function-name' nor " | |
1437 "'field' specified"; | |
1438 return false; | |
1439 } | |
1440 m_database->addRejection(cls, function, field, enum_); | |
1441 } | |
1442 break; | |
1443 case StackElement::Template: | |
1444 element->value.templateEntry = new TemplateEntry(attributes["name"]); | |
1445 break; | |
1446 case StackElement::TemplateInstanceEnum: | |
1447 if (!(topElement.type & StackElement::CodeSnipMask) && | |
1448 (topElement.type != StackElement::Template) && | |
1449 (topElement.type != StackElement::CustomMetaConstructor) && | |
1450 (topElement.type != StackElement::CustomMetaDestructor) && | |
1451 (topElement.type != StackElement::ConversionRule)) | |
1452 { | |
1453 m_error = "Can only insert templates into code snippets, templates, custom-constructors, custom-destructors or conversion-rule."; | |
1454 return false; | |
1455 } | |
1456 element->value.templateInstance = new TemplateInstance(attributes["name"]); | |
1457 break; | |
1458 case StackElement::Replace: | |
1459 if (topElement.type != StackElement::TemplateInstanceEnum) { | |
1460 m_error = "Can only insert replace rules into insert-template."; | |
1461 return false; | |
1462 } | |
1463 element->parent->value.templateInstance->addReplaceRule(attributes["from"],attributes["to"]); | |
1464 break; | |
42 | 1465 case StackElement::PackageDepend: |
1466 { | |
1467 QString package = attributes["package"]; | |
1468 if (package.isEmpty()) { | |
1469 m_error = "bad package depend entry"; | |
1470 return false; | |
1471 } | |
1472 m_defaultDepends << package; | |
1473 } | |
1474 break; | |
1 | 1475 default: |
1476 break; // nada | |
1477 }; | |
1478 } | |
1479 | |
1480 current = element; | |
1481 return true; | |
1482 } | |
1483 | |
1484 TypeDatabase *TypeDatabase::instance() | |
1485 { | |
1486 static TypeDatabase *db = new TypeDatabase(); | |
1487 return db; | |
1488 } | |
1489 | |
1490 TypeDatabase::TypeDatabase() : m_suppressWarnings(true), m_includeEclipseWarnings(false) | |
1491 { | |
1492 addType(new StringTypeEntry("QString")); | |
1493 | |
1494 StringTypeEntry *e = new StringTypeEntry("QLatin1String"); | |
1495 e->setPreferredConversion(false); | |
1496 addType(e); | |
1497 | |
1498 e = new StringTypeEntry("QStringRef"); | |
1499 e->setPreferredConversion(false); | |
1500 addType(e); | |
1501 | |
1502 e = new StringTypeEntry("QXmlStreamStringRef"); | |
1503 e->setPreferredConversion(false); | |
1504 addType(e); | |
1505 | |
1506 addType(new CharTypeEntry("QChar")); | |
1507 | |
1508 CharTypeEntry *c = new CharTypeEntry("QLatin1Char"); | |
1509 c->setPreferredConversion(false); | |
1510 addType(c); | |
1511 | |
1512 { | |
1513 VariantTypeEntry *qvariant = new VariantTypeEntry("QVariant"); | |
1514 qvariant->setCodeGeneration(TypeEntry::GenerateAll); | |
1515 addType(qvariant); | |
1516 } | |
1517 | |
1518 { | |
1519 JObjectWrapperTypeEntry *wrapper = new JObjectWrapperTypeEntry("JObjectWrapper"); | |
1520 wrapper->setCodeGeneration(TypeEntry::GenerateNothing); | |
1521 addType(wrapper); | |
1522 } | |
1523 | |
1524 addType(new ThreadTypeEntry()); | |
1525 addType(new VoidTypeEntry()); | |
1526 | |
1527 // Predefined containers... | |
1528 addType(new ContainerTypeEntry("QList", ContainerTypeEntry::ListContainer)); | |
1529 addType(new ContainerTypeEntry("QStringList", ContainerTypeEntry::StringListContainer)); | |
1530 addType(new ContainerTypeEntry("QLinkedList", ContainerTypeEntry::LinkedListContainer)); | |
1531 addType(new ContainerTypeEntry("QVector", ContainerTypeEntry::VectorContainer)); | |
1532 addType(new ContainerTypeEntry("QStack", ContainerTypeEntry::StackContainer)); | |
1533 addType(new ContainerTypeEntry("QSet", ContainerTypeEntry::SetContainer)); | |
1534 addType(new ContainerTypeEntry("QMap", ContainerTypeEntry::MapContainer)); | |
1535 addType(new ContainerTypeEntry("QHash", ContainerTypeEntry::HashContainer)); | |
1536 addType(new ContainerTypeEntry("QPair", ContainerTypeEntry::PairContainer)); | |
1537 addType(new ContainerTypeEntry("QQueue", ContainerTypeEntry::QueueContainer)); | |
1538 addType(new ContainerTypeEntry("QMultiMap", ContainerTypeEntry::MultiMapContainer)); | |
1539 | |
1540 // Custom types... | |
1541 // addType(new QModelIndexTypeEntry()); | |
1542 | |
1543 addRemoveFunctionToTemplates(this); | |
1544 } | |
1545 | |
4 | 1546 bool TypeDatabase::parseFile(const QString &filename, const QString &source_dir, bool generate) |
1 | 1547 { |
27
a9626891eca6
CMake: generator will work only for needed packages.
SokoL_SD
parents:
14
diff
changeset
|
1548 QString name(filename); |
a9626891eca6
CMake: generator will work only for needed packages.
SokoL_SD
parents:
14
diff
changeset
|
1549 QFileInfo info(name); |
a9626891eca6
CMake: generator will work only for needed packages.
SokoL_SD
parents:
14
diff
changeset
|
1550 if(!info.exists()) |
a9626891eca6
CMake: generator will work only for needed packages.
SokoL_SD
parents:
14
diff
changeset
|
1551 { |
a9626891eca6
CMake: generator will work only for needed packages.
SokoL_SD
parents:
14
diff
changeset
|
1552 Q_ASSERT(!source_dir.isEmpty()); |
4 | 1553 name = source_dir + "/" + filename; |
27
a9626891eca6
CMake: generator will work only for needed packages.
SokoL_SD
parents:
14
diff
changeset
|
1554 } |
4 | 1555 QFile file(name); |
1 | 1556 Q_ASSERT(file.exists()); |
1557 QXmlInputSource source(&file); | |
1558 | |
1559 int count = m_entries.size(); | |
1560 | |
1561 QXmlSimpleReader reader; | |
4 | 1562 Handler handler(this, source_dir, generate); |
1 | 1563 |
1564 reader.setContentHandler(&handler); | |
1565 reader.setErrorHandler(&handler); | |
1566 | |
1567 bool ok = reader.parse(&source, false); | |
1568 | |
1569 int newCount = m_entries.size(); | |
1570 | |
1571 ReportHandler::debugSparse(QString::fromLatin1("Parsed: '%1', %2 new entries") | |
1572 .arg(filename) | |
1573 .arg(newCount - count)); | |
1574 | |
1575 return ok; | |
1576 } | |
1577 | |
1578 QString PrimitiveTypeEntry::javaObjectName() const | |
1579 { | |
1580 static QHash<QString, QString> table; | |
1581 if (table.isEmpty()) { | |
1582 table["boolean"] = "Boolean"; | |
1583 table["byte"] = "Byte"; | |
1584 table["char"] = "Character"; | |
1585 table["short"] = "Short"; | |
1586 table["int"] = "Integer"; | |
1587 table["long"] = "Long"; | |
1588 table["float"] = "Float"; | |
1589 table["double"] = "Double"; | |
1590 } | |
1591 Q_ASSERT(table.contains(targetLangName())); | |
1592 return table[targetLangName()]; | |
1593 } | |
1594 | |
1595 ContainerTypeEntry *TypeDatabase::findContainerType(const QString &name) | |
1596 { | |
1597 QString template_name = name; | |
1598 | |
1599 int pos = name.indexOf('<'); | |
1600 if (pos > 0) | |
1601 template_name = name.left(pos); | |
1602 | |
1603 TypeEntry *type_entry = findType(template_name); | |
1604 if (type_entry && type_entry->isContainer()) | |
1605 return static_cast<ContainerTypeEntry *>(type_entry); | |
1606 return 0; | |
1607 } | |
1608 | |
1609 PrimitiveTypeEntry *TypeDatabase::findTargetLangPrimitiveType(const QString &java_name) | |
1610 { | |
1611 foreach (QList<TypeEntry *> entries, m_entries.values()) { | |
1612 foreach (TypeEntry *e, entries) { | |
1613 if (e && e->isPrimitive()) { | |
1614 PrimitiveTypeEntry *pe = static_cast<PrimitiveTypeEntry *>(e); | |
1615 if (pe->targetLangName() == java_name && pe->preferredConversion()) | |
1616 return pe; | |
1617 } | |
1618 } | |
1619 } | |
1620 | |
1621 return 0; | |
1622 } | |
1623 | |
1624 IncludeList TypeDatabase::extraIncludes(const QString &className) | |
1625 { | |
1626 ComplexTypeEntry *typeEntry = findComplexType(className); | |
1627 if (typeEntry != 0) | |
1628 return typeEntry->extraIncludes(); | |
1629 else | |
1630 return IncludeList(); | |
1631 } | |
1632 | |
1633 | |
1634 | |
1635 QString Include::toString() const | |
1636 { | |
1637 if (type == IncludePath) | |
1638 return "#include <" + name + '>'; | |
1639 else if (type == LocalPath) | |
1640 return "#include \"" + name + "\""; | |
1641 else | |
1642 return "import " + name + ";"; | |
1643 } | |
1644 | |
1645 QString Modification::accessModifierString() const | |
1646 { | |
1647 if (isPrivate()) return "private"; | |
1648 if (isProtected()) return "protected"; | |
1649 if (isPublic()) return "public"; | |
1650 if (isFriendly()) return "friendly"; | |
1651 return QString(); | |
1652 } | |
1653 | |
1654 FunctionModificationList ComplexTypeEntry::functionModifications(const QString &signature) const | |
1655 { | |
1656 FunctionModificationList lst; | |
1657 for (int i=0; i<m_function_mods.count(); ++i) { | |
1658 FunctionModification mod = m_function_mods.at(i); | |
1659 if (mod.signature == signature) { | |
1660 lst << mod; | |
1661 } | |
1662 } | |
1663 | |
1664 return lst; | |
1665 } | |
1666 | |
1667 FieldModification ComplexTypeEntry::fieldModification(const QString &name) const | |
1668 { | |
1669 for (int i=0; i<m_field_mods.size(); ++i) | |
1670 if (m_field_mods.at(i).name == name) | |
1671 return m_field_mods.at(i); | |
1672 FieldModification mod; | |
1673 mod.name = name; | |
1674 mod.modifiers = FieldModification::Readable | FieldModification::Writable; | |
1675 return mod; | |
1676 } | |
1677 | |
1678 QString ContainerTypeEntry::javaPackage() const | |
1679 { | |
1680 if (m_type == PairContainer) | |
1681 return "qt"; | |
1682 return "java.util"; | |
1683 } | |
1684 | |
1685 QString ContainerTypeEntry::targetLangName() const | |
1686 { | |
1687 | |
1688 switch (m_type) { | |
1689 case StringListContainer: return "List"; | |
1690 case ListContainer: return "List"; | |
1691 case LinkedListContainer: return "LinkedList"; | |
1692 case VectorContainer: return "List"; | |
1693 case StackContainer: return "Stack"; | |
1694 case QueueContainer: return "Queue"; | |
1695 case SetContainer: return "Set"; | |
1696 case MapContainer: return "SortedMap"; | |
1697 case MultiMapContainer: return "SortedMap"; | |
1698 case HashContainer: return "HashMap"; | |
1699 // case MultiHashCollectio: return "MultiHash"; | |
1700 case PairContainer: return "QPair"; | |
1701 default: | |
1702 qWarning("bad type... %d", m_type); | |
1703 break; | |
1704 } | |
1705 return QString(); | |
1706 } | |
1707 | |
1708 QString ContainerTypeEntry::qualifiedCppName() const | |
1709 { | |
1710 if (m_type == StringListContainer) | |
1711 return "QStringList"; | |
1712 return ComplexTypeEntry::qualifiedCppName(); | |
1713 } | |
1714 | |
1715 QString EnumTypeEntry::javaQualifier() const | |
1716 { | |
1717 TypeEntry *te = TypeDatabase::instance()->findType(m_qualifier); | |
1718 if (te != 0) | |
1719 return te->targetLangName(); | |
1720 else | |
1721 return m_qualifier; | |
1722 } | |
1723 | |
1724 QString EnumTypeEntry::jniName() const | |
1725 { | |
1726 return "jint"; | |
1727 } | |
1728 | |
1729 QString FlagsTypeEntry::jniName() const | |
1730 { | |
1731 return "jint"; | |
1732 } | |
1733 | |
1734 void EnumTypeEntry::addEnumValueRedirection(const QString &rejected, const QString &usedValue) | |
1735 { | |
1736 m_enum_redirections << EnumValueRedirection(rejected, usedValue); | |
1737 } | |
1738 | |
1739 QString EnumTypeEntry::enumValueRedirection(const QString &value) const | |
1740 { | |
1741 for (int i=0; i<m_enum_redirections.size(); ++i) | |
1742 if (m_enum_redirections.at(i).rejected == value) | |
1743 return m_enum_redirections.at(i).used; | |
1744 return QString(); | |
1745 } | |
1746 | |
1747 QString FlagsTypeEntry::qualifiedTargetLangName() const | |
1748 { | |
1749 return m_enum->javaQualifier() + "." + targetLangName(); | |
1750 } | |
1751 | |
1752 | |
1753 void TypeDatabase::addRejection(const QString &class_name, const QString &function_name, | |
1754 const QString &field_name, const QString &enum_name) | |
1755 { | |
1756 TypeRejection r; | |
1757 r.class_name = class_name; | |
1758 r.function_name = function_name; | |
1759 r.field_name = field_name; | |
1760 r.enum_name = enum_name; | |
1761 | |
1762 m_rejections << r; | |
1763 } | |
1764 | |
1765 bool TypeDatabase::isClassRejected(const QString &class_name) | |
1766 { | |
1767 if (!m_rebuild_classes.isEmpty()) | |
1768 return !m_rebuild_classes.contains(class_name); | |
1769 | |
1770 foreach (const TypeRejection &r, m_rejections) | |
1771 if (r.class_name == class_name && r.function_name == "*" && r.field_name == "*" && r.enum_name == "*") { | |
1772 return true; | |
1773 } | |
1774 return false; | |
1775 } | |
1776 | |
1777 bool TypeDatabase::isEnumRejected(const QString &class_name, const QString &enum_name) | |
1778 { | |
1779 foreach (const TypeRejection &r, m_rejections) { | |
1780 if (r.enum_name == enum_name | |
1781 && (r.class_name == class_name || r.class_name == "*")) { | |
1782 return true; | |
1783 } | |
1784 } | |
1785 | |
1786 return false; | |
1787 } | |
1788 | |
1789 bool TypeDatabase::isFunctionRejected(const QString &class_name, const QString &function_name) | |
1790 { | |
1791 foreach (const TypeRejection &r, m_rejections) | |
1792 if (r.function_name == function_name && | |
1793 (r.class_name == class_name || r.class_name == "*")) | |
1794 return true; | |
1795 return false; | |
1796 } | |
1797 | |
1798 | |
1799 bool TypeDatabase::isFieldRejected(const QString &class_name, const QString &field_name) | |
1800 { | |
1801 foreach (const TypeRejection &r, m_rejections) | |
1802 if (r.field_name == field_name && | |
1803 (r.class_name == class_name || r.class_name == "*")) | |
1804 return true; | |
1805 return false; | |
1806 } | |
1807 | |
1808 FlagsTypeEntry *TypeDatabase::findFlagsType(const QString &name) const | |
1809 { | |
1810 FlagsTypeEntry *fte = (FlagsTypeEntry *) findType(name); | |
1811 return fte ? fte : (FlagsTypeEntry *) m_flags_entries.value(name); | |
1812 } | |
1813 | |
1814 QString TypeDatabase::globalNamespaceClassName(const TypeEntry * /*entry*/) { | |
1815 return QLatin1String("Global"); | |
1816 } | |
1817 | |
1818 | |
1819 /*! | |
1820 * The Visual Studio 2002 compiler doesn't support these symbols, | |
1821 * which our typedefs unforntuatly expand to. | |
1822 */ | |
1823 QString fixCppTypeName(const QString &name) | |
1824 { | |
1825 if (name == "long long") return "qint64"; | |
1826 else if (name == "unsigned long long") return "quint64"; | |
1827 return name; | |
1828 } | |
1829 | |
1830 QString formattedCodeHelper(QTextStream &s, Indentor &indentor, QStringList &lines) { | |
1831 bool multilineComment = false; | |
1832 bool lastEmpty = true; | |
1833 QString lastLine; | |
1834 while (!lines.isEmpty()) { | |
1835 const QString line = lines.takeFirst().trimmed(); | |
1836 if (line.isEmpty()) { | |
1837 if (!lastEmpty) | |
1838 s << endl; | |
1839 lastEmpty = true; | |
1840 continue; | |
1841 } else { | |
1842 lastEmpty = false; | |
1843 } | |
1844 if (line.startsWith("/*")) | |
1845 multilineComment = true; | |
1846 | |
1847 if (multilineComment) { | |
1848 s << indentor; | |
1849 if (line.startsWith("*")) | |
1850 s << " "; | |
1851 s << line << endl; | |
1852 if (line.endsWith("*/")) | |
1853 multilineComment = false; | |
1854 } else if (line.startsWith("}")) { | |
1855 return line; | |
1856 } else if (line.endsWith("}")) { | |
1857 s << indentor << line << endl; | |
1858 return 0; | |
1859 } else if(line.endsWith("{")) { | |
1860 s << indentor << line << endl; | |
1861 QString tmp; | |
1862 { | |
1863 Indentation indent(indentor); | |
1864 tmp = formattedCodeHelper(s, indentor, lines); | |
1865 } | |
1866 if (!tmp.isNull()) { | |
1867 s << indentor << tmp << endl; | |
1868 } | |
1869 lastLine = tmp; | |
1870 continue; | |
1871 } else { | |
1872 s << indentor; | |
1873 if (!lastLine.isEmpty() && | |
1874 !lastLine.endsWith(";") && | |
1875 !line.startsWith("@") && | |
1876 !line.startsWith("//") && | |
1877 !lastLine.startsWith("//") && | |
1878 !lastLine.endsWith("}") && | |
1879 !line.startsWith("{")) | |
1880 s << " "; | |
1881 s << line << endl; | |
1882 } | |
1883 lastLine = line; | |
1884 } | |
1885 return 0; | |
1886 } | |
1887 | |
1888 | |
1889 QTextStream &CodeSnip::formattedCode(QTextStream &s, Indentor &indentor) const | |
1890 { | |
1891 QStringList lst(code().split("\n")); | |
1892 while (!lst.isEmpty()) { | |
1893 QString tmp = formattedCodeHelper(s, indentor, lst); | |
1894 if (!tmp.isNull()) { | |
1895 s << indentor << tmp << endl; | |
1896 } | |
1897 } | |
1898 s.flush(); | |
1899 return s; | |
1900 } | |
1901 | |
1902 QString TemplateInstance::expandCode() const{ | |
1903 TemplateEntry *templateEntry = TypeDatabase::instance()->findTemplate(m_name); | |
1904 if(templateEntry){ | |
1905 QString res = templateEntry->code(); | |
1906 foreach(QString key, replaceRules.keys()){ | |
1907 res.replace(key, replaceRules[key]); | |
1908 } | |
1909 return "// TEMPLATE - " + m_name + " - START" + res + "// TEMPLATE - " + m_name + " - END"; | |
1910 } | |
1911 else{ | |
1912 ReportHandler::warning("insert-template referring to non-existing template '" + m_name + "'"); | |
1913 } | |
1914 return QString(); | |
1915 } | |
1916 | |
1917 | |
1918 QString CodeSnipAbstract::code() const{ | |
1919 QString res; | |
1920 foreach(CodeSnipFragment *codeFrag, codeList){ | |
1921 res.append(codeFrag->code()); | |
1922 } | |
1923 return res; | |
1924 } | |
1925 | |
1926 QString CodeSnipFragment::code() const{ | |
1927 if(m_instance) | |
1928 return m_instance->expandCode(); | |
1929 else | |
1930 return m_code; | |
1931 } | |
1932 | |
1933 QString FunctionModification::toString() const | |
1934 { | |
1935 QString str = signature + QLatin1String("->"); | |
1936 if (modifiers & AccessModifierMask) { | |
1937 switch (modifiers & AccessModifierMask) { | |
1938 case Private: str += QLatin1String("private"); break; | |
1939 case Protected: str += QLatin1String("protected"); break; | |
1940 case Public: str += QLatin1String("public"); break; | |
1941 case Friendly: str += QLatin1String("friendly"); break; | |
1942 } | |
1943 } | |
1944 | |
1945 if (modifiers & Final) str += QLatin1String("final"); | |
1946 if (modifiers & NonFinal) str += QLatin1String("non-final"); | |
1947 | |
1948 if (modifiers & Readable) str += QLatin1String("readable"); | |
1949 if (modifiers & Writable) str += QLatin1String("writable"); | |
1950 | |
1951 if (modifiers & CodeInjection) { | |
1952 foreach (CodeSnip s, snips) { | |
1953 str += QLatin1String("\n//code injection:\n"); | |
1954 str += s.code(); | |
1955 } | |
1956 } | |
1957 | |
1958 if (modifiers & Rename) str += QLatin1String("renamed:") + renamedToName; | |
1959 | |
1960 if (modifiers & Deprecated) str += QLatin1String("deprecate"); | |
1961 | |
1962 if (modifiers & ReplaceExpression) str += QLatin1String("replace-expression"); | |
1963 | |
1964 return str; | |
1965 } | |
1966 | |
1967 static void removeFunction(ComplexTypeEntry *e, const char *signature) | |
1968 { | |
1969 FunctionModification mod; | |
1970 mod.signature = QMetaObject::normalizedSignature(signature); | |
1971 mod.removal = TypeSystem::All; | |
1972 | |
1973 e->addFunctionModification(mod); | |
1974 } | |
1975 | |
1976 | |
1977 | |
1978 | |
1979 static void injectCode(ComplexTypeEntry *e, | |
1980 const char *signature, | |
1981 const QByteArray &code, | |
1982 const ArgumentMap &args, | |
1983 TypeSystem::Language lang = TypeSystem::NativeCode) | |
1984 { | |
1985 CodeSnip snip; | |
1986 snip.language = lang; | |
1987 snip.position = CodeSnip::Beginning; | |
1988 snip.addCode(QString::fromLatin1(code)); | |
1989 snip.argumentMap = args; | |
1990 | |
1991 FunctionModification mod; | |
1992 mod.signature = QMetaObject::normalizedSignature(signature); | |
1993 mod.snips << snip; | |
1994 mod.modifiers = Modification::CodeInjection; | |
1995 e->addFunctionModification(mod); | |
1996 } | |
1997 | |
1998 | |
1999 static void addRemoveFunctionToTemplates(TypeDatabase *db) | |
2000 { | |
2001 ContainerTypeEntry *qvector = db->findContainerType(QLatin1String("QVector")); | |
2002 removeFunction(qvector, "constData() const"); | |
2003 removeFunction(qvector, "data() const"); | |
2004 removeFunction(qvector, "data()"); | |
2005 removeFunction(qvector, "first()"); | |
2006 removeFunction(qvector, "last()"); | |
2007 removeFunction(qvector, "operator[](int)"); | |
2008 removeFunction(qvector, "operator[](int) const"); | |
2009 removeFunction(qvector, "operator=(QVector<T>)"); | |
2010 | |
2011 ContainerTypeEntry *qlist = db->findContainerType(QLatin1String("QList")); | |
2012 removeFunction(qlist, "constData() const"); | |
2013 removeFunction(qlist, "data() const"); | |
2014 removeFunction(qlist, "data()"); | |
2015 removeFunction(qlist, "back()"); | |
2016 removeFunction(qlist, "front()"); | |
2017 removeFunction(qlist, "first()"); | |
2018 removeFunction(qlist, "last()"); | |
2019 removeFunction(qlist, "operator[](int)"); | |
2020 removeFunction(qlist, "operator[](int) const"); | |
2021 removeFunction(qlist, "operator=(QList<T>)"); | |
2022 | |
2023 ContainerTypeEntry *qqueue = db->findContainerType(QLatin1String("QQueue")); | |
2024 removeFunction(qqueue, "head() const"); | |
2025 | |
2026 | |
2027 ArgumentMap args1; | |
2028 args1[1] = QLatin1String("$1"); | |
2029 ArgumentMap args2 = args1; | |
2030 args2[2] = QLatin1String("$2"); | |
2031 | |
2032 QByteArray code = | |
2033 "\nif ($1 >= __qt_this->size() || $1 < 0) {" | |
2034 "\n __jni_env->ThrowNew(__jni_env->FindClass(\"java/lang/IndexOutOfBoundsException\")," | |
2035 "\n QString::fromLatin1(\"Accessing container of size %3 at %4\")" | |
2036 "\n .arg(__qt_this->size()).arg($1).toLatin1());" | |
2037 "\n return;" | |
2038 "\n}"; | |
2039 | |
2040 QByteArray code_with_return = QByteArray(code).replace("return;", "return 0;"); | |
2041 | |
2042 QByteArray code_index_length = | |
2043 "\nif ($1 < 0 || $2 < 0 || ($1 + $2) >= __qt_this->size()) {" | |
2044 "\n __jni_env->ThrowNew(__jni_env->FindClass(\"java/lang/IndexOutOfBoundsException\")," | |
2045 "\n QString::fromLatin1(\"Accessing container of size %3 from %4 to %5\")" | |
2046 "\n .arg(__qt_this->size()).arg($1).arg($1+$2).toLatin1());" | |
2047 "\n return;" | |
2048 "\n}"; | |
2049 | |
2050 QByteArray code_non_empty = | |
2051 "\nif (__qt_this->isEmpty()) {" | |
2052 "\n __jni_env->ThrowNew(__jni_env->FindClass(\"java/lang/IndexOutOfBoundsException\")," | |
2053 "\n QString::fromLatin1(\"Accessing empty container...\").toLatin1());" | |
2054 "\n return;" | |
2055 "\n}"; | |
2056 | |
2057 QByteArray code_two_indices = | |
2058 "\nif ($1 < 0 || $2 < 0 || $1 >= __qt_this->size() || $2 >= __qt_this->size()) {" | |
2059 "\n __jni_env->ThrowNew(__jni_env->FindClass(\"java/lang/IndexOutOfBoundsException\")," | |
2060 "\n QString::fromLatin1(\"Accessing container of size %3 from %4 to %5\")" | |
2061 "\n .arg(__qt_this->size()).arg($1).arg($1+$2).toLatin1());" | |
2062 "\n return;" | |
2063 "\n}"; | |
2064 /* qtd2 | |
2065 { // QVector safty... | |
2066 injectCode(qvector, "at(int) const", code_with_return, args1, TypeSystem::TargetLangCode); | |
2067 injectCode(qvector, "replace(int,T)", code, args1, TypeSystem::TargetLangCode); | |
2068 injectCode(qvector, "remove(int)", code, args1, TypeSystem::TargetLangCode); | |
2069 injectCode(qvector, "remove(int, int)", code_index_length, args2, TypeSystem::TargetLangCode); | |
2070 injectCode(qvector, "pop_back()", code_non_empty, ArgumentMap(), TypeSystem::TargetLangCode); | |
2071 injectCode(qvector, "pop_front()", code_non_empty, ArgumentMap(), TypeSystem::TargetLangCode); | |
2072 } | |
2073 | |
2074 { // QList safty... | |
2075 injectCode(qlist, "at(int) const", code_with_return, args1); | |
2076 injectCode(qlist, "replace(int, T)", code, args1); | |
2077 injectCode(qlist, "pop_back()", code_non_empty, ArgumentMap()); | |
2078 injectCode(qlist, "pop_front()", code_non_empty, ArgumentMap()); | |
2079 injectCode(qlist, "swap(int, int)", code_two_indices, args2); | |
2080 injectCode(qlist, "move(int, int)", code_two_indices, args2); | |
2081 injectCode(qlist, "removeAt(int)", code, args1); | |
2082 injectCode(qlist, "takeAt(int)", code_with_return, args1); | |
2083 } | |
2084 */ | |
2085 } |