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