comparison generator/typesystem.h @ 1:e78566595089

initial import
author mandel
date Mon, 11 May 2009 16:01:50 +0000
parents
children 0a29ce1ae854
comparison
equal deleted inserted replaced
0:36fb74dc547d 1:e78566595089
1 /****************************************************************************
2 **
3 ** Copyright (C) 1992-2008 Nokia. All rights reserved.
4 **
5 ** This file is part of Qt Jambi.
6 **
7 ** * Commercial Usage
8 * Licensees holding valid Qt Commercial licenses may use this file in
9 * accordance with the Qt Commercial License Agreement provided with the
10 * Software or, alternatively, in accordance with the terms contained in
11 * a written agreement between you and Nokia.
12 *
13 *
14 * GNU General Public License Usage
15 * Alternatively, this file may be used under the terms of the GNU
16 * General Public License versions 2.0 or 3.0 as published by the Free
17 * Software Foundation and appearing in the file LICENSE.GPL included in
18 * the packaging of this file. Please review the following information
19 * to ensure GNU General Public Licensing requirements will be met:
20 * http://www.fsf.org/licensing/licenses/info/GPLv2.html and
21 * http://www.gnu.org/copyleft/gpl.html. In addition, as a special
22 * exception, Nokia gives you certain additional rights. These rights
23 * are described in the Nokia Qt GPL Exception version 1.2, included in
24 * the file GPL_EXCEPTION.txt in this package.
25 *
26 * Qt for Windows(R) Licensees
27 * As a special exception, Nokia, as the sole copyright holder for Qt
28 * Designer, grants users of the Qt/Eclipse Integration plug-in the
29 * right for the Qt/Eclipse Integration to link to functionality
30 * provided by Qt Designer and its related libraries.
31 *
32 *
33 * If you are unsure which license is appropriate for your use, please
34 * contact the sales department at qt-sales@nokia.com.
35
36 **
37 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
38 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
39 **
40 ****************************************************************************/
41
42 #ifndef TYPESYSTEM_H
43 #define TYPESYSTEM_H
44
45 #include <QtCore/QHash>
46 #include <QtCore/QString>
47 #include <QtCore/QStringList>
48 #include <QtCore/QMap>
49 #include <QDebug>
50
51 class Indentor;
52
53 class AbstractMetaType;
54 class QTextStream;
55
56 class EnumTypeEntry;
57 class FlagsTypeEntry;
58
59 extern QString strings_Object;
60 extern QString strings_String;
61 extern QString strings_Thread;
62 extern QString strings_char;
63 extern QString strings_java_lang;
64 extern QString strings_jchar;
65 extern QString strings_jobject;
66
67 struct Include
68 {
69 enum IncludeType {
70 IncludePath,
71 LocalPath,
72 TargetLangImport
73 };
74
75 Include() : type(IncludePath) { }
76 Include(IncludeType t, const QString &nam) : type(t), name(nam) { };
77
78 bool isValid() { return !name.isEmpty(); }
79
80 IncludeType type;
81 QString name;
82
83 QString toString() const;
84
85 bool operator<(const Include &other) const { return name < other.name; }
86 };
87 typedef QList<Include> IncludeList;
88
89 typedef QMap<int, QString> ArgumentMap;
90
91 class TemplateInstance;
92
93 namespace TypeSystem {
94 enum Language {
95 NoLanguage = 0x0000,
96 TargetLangCode = 0x0001,
97 NativeCode = 0x0002,
98 ShellCode = 0x0004,
99 ShellDeclaration = 0x0008,
100 PackageInitializer = 0x0010,
101 DestructorFunction = 0x0020,
102 Constructors = 0x0040,
103 Interface = 0x0080,
104
105 // masks
106 All = TargetLangCode
107 | NativeCode
108 | ShellCode
109 | ShellDeclaration
110 | PackageInitializer
111 | Constructors
112 | Interface
113 | DestructorFunction,
114
115 JavaAndNativeCode = TargetLangCode | NativeCode,
116 TargetLangAndNativeCode = TargetLangCode | NativeCode
117 };
118
119 enum Ownership {
120 InvalidOwnership,
121 DefaultOwnership,
122 TargetLangOwnership,
123 CppOwnership
124 };
125 };
126
127 struct ReferenceCount
128 {
129 ReferenceCount() : threadSafe(false), access(Public) { }
130 enum Action { // 0x01 - 0xff
131 Invalid = 0x00,
132 Add = 0x01,
133 AddAll = 0x02,
134 Remove = 0x04,
135 Set = 0x08,
136 Ignore = 0x10,
137
138 ActionsMask = 0xff,
139
140 Padding = 0xffffffff
141 };
142
143 enum Flag { // 0x100 - 0xf00
144 ThreadSafe = 0x100,
145 Static = 0x200,
146 DeclareVariable = 0x400,
147
148 FlagsMask = 0xf00
149 };
150
151 enum Access { // 0x1000 - 0xf000
152 Private = 0x1000,
153 Protected = 0x2000,
154 Friendly = 0x3000,
155 Public = 0x4000,
156
157 AccessMask = 0xf000
158 };
159
160 Action action;
161 QString variableName;
162 QString conditional;
163 QString declareVariable;
164
165 uint threadSafe : 1;
166
167 uint access;
168 };
169
170 class CodeSnipFragment{
171 private:
172 const QString m_code;
173 TemplateInstance *m_instance;
174
175 public:
176 CodeSnipFragment(const QString &code)
177 : m_code(code),
178 m_instance(0)
179 {}
180
181 CodeSnipFragment(TemplateInstance *instance)
182 : m_instance(instance)
183 {}
184
185 QString code() const;
186 };
187
188 class CodeSnipAbstract{
189 public:
190 QString code() const;
191
192 void addCode(const QString &code){
193 codeList.append(new CodeSnipFragment(code));
194 }
195
196 void addTemplateInstance(TemplateInstance *ti){
197 codeList.append(new CodeSnipFragment(ti));
198 }
199
200 QList<CodeSnipFragment*> codeList;
201 };
202
203 class CustomFunction : public CodeSnipAbstract
204 {
205 public:
206 CustomFunction(const QString &n = QString()) : name(n) { }
207
208 QString name;
209 QString param_name;
210 };
211
212 class TemplateEntry : public CodeSnipAbstract
213 {
214 public:
215 TemplateEntry(const QString &name)
216 : m_name(name)
217 {
218 };
219
220 QString name() const {
221 return m_name;
222 };
223
224 private:
225 QString m_name;
226 };
227
228 typedef QHash<QString, TemplateEntry *> TemplateEntryHash;
229
230 class TemplateInstance
231 {
232 public:
233 TemplateInstance(const QString &name)
234 : m_name(name)
235 {}
236
237 void addReplaceRule(const QString &name, const QString &value){
238 replaceRules[name]=value;
239 }
240
241 QString expandCode() const;
242
243 QString name() const {
244 return m_name;
245 }
246
247 private:
248 const QString m_name;
249 QHash<QString, QString> replaceRules;
250 };
251
252
253 class CodeSnip : public CodeSnipAbstract
254 {
255 public:
256 enum Position {
257 Beginning,
258 End,
259 AfterThis
260 };
261
262 CodeSnip() : language(TypeSystem::TargetLangCode) { }
263 CodeSnip(TypeSystem::Language lang) : language(lang) { }
264
265 // Very simple, easy to make code ugly if you try
266 QTextStream &formattedCode(QTextStream &s, Indentor &indentor) const;
267
268 TypeSystem::Language language;
269 Position position;
270 ArgumentMap argumentMap;
271 };
272 typedef QList<CodeSnip> CodeSnipList;
273
274 struct ArgumentModification
275 {
276 ArgumentModification(int idx) : removed_default_expression(false), removed(false), no_null_pointers(false), index(idx)
277 {}
278
279 // Should the default expression be removed?
280 uint removed_default_expression : 1;
281 uint removed : 1;
282 uint no_null_pointers : 1;
283 uint reset_after_use : 1;
284
285 // The index of this argument
286 int index;
287
288 // Reference count flags for this argument
289 QList<ReferenceCount> referenceCounts;
290
291 // The text given for the new type of the argument
292 QString modified_type;
293
294 QString replace_value;
295
296 // The code to be used to construct a return value when no_null_pointers is true and
297 // the returned value is null. If no_null_pointers is true and this string is
298 // empty, then the base class implementation will be used (or a default construction
299 // if there is no implementation)
300 QString null_pointer_default_value;
301
302 // The text of the new default expression of the argument
303 QString replaced_default_expression;
304
305 // The new definition of ownership for a specific argument
306 QHash<TypeSystem::Language, TypeSystem::Ownership> ownerships;
307
308 // Different conversion rules
309 CodeSnipList conversion_rules;
310 };
311
312 struct Modification {
313 enum Modifiers {
314 Private = 0x0001,
315 Protected = 0x0002,
316 Public = 0x0003,
317 Friendly = 0x0004,
318 AccessModifierMask = 0x000f,
319
320 Final = 0x0010,
321 NonFinal = 0x0020,
322 FinalMask = Final | NonFinal,
323
324 Readable = 0x0100,
325 Writable = 0x0200,
326
327 CodeInjection = 0x1000,
328 Rename = 0x2000,
329 Deprecated = 0x4000,
330 ReplaceExpression = 0x8000,
331 VirtualSlot = 0x10000 | NonFinal
332 };
333
334 Modification() : modifiers(0) { }
335
336 bool isAccessModifier() const { return modifiers & AccessModifierMask; }
337 Modifiers accessModifier() const { return Modifiers(modifiers & AccessModifierMask); }
338 bool isPrivate() const { return accessModifier() == Private; }
339 bool isProtected() const { return accessModifier() == Protected; }
340 bool isPublic() const { return accessModifier() == Public; }
341 bool isFriendly() const { return accessModifier() == Friendly; }
342 bool isFinal() const { return modifiers & Final; }
343 bool isNonFinal() const { return modifiers & NonFinal; }
344 bool isVirtualSlot() const { return (modifiers & VirtualSlot) == VirtualSlot; }
345 QString accessModifierString() const;
346
347 bool isDeprecated() const { return modifiers & Deprecated; }
348
349 void setRenamedTo(const QString &name) { renamedToName = name; }
350 QString renamedTo() const { return renamedToName; }
351 bool isRenameModifier() const { return modifiers & Rename; }
352
353 uint modifiers;
354 QString renamedToName;
355 };
356
357 struct FunctionModification: public Modification
358 {
359 FunctionModification() : removal(TypeSystem::NoLanguage), store_result(false) { }
360
361 bool isCodeInjection() const { return modifiers & CodeInjection; }
362 bool isRemoveModifier() const { return removal != TypeSystem::NoLanguage; }
363
364 QString toString() const;
365
366 QString signature;
367 QString association;
368 CodeSnipList snips;
369 TypeSystem::Language removal;
370 bool store_result;
371
372 QList<ArgumentModification> argument_mods;
373 };
374 typedef QList<FunctionModification> FunctionModificationList;
375
376 struct FieldModification: public Modification
377 {
378 bool isReadable() const { return modifiers & Readable; }
379 bool isWritable() const { return modifiers & Writable; }
380
381 QString name;
382 };
383 typedef QList<FieldModification> FieldModificationList;
384
385 struct ExpensePolicy {
386 ExpensePolicy() : limit(-1) { }
387 int limit;
388 QString cost;
389 bool isValid() const { return limit >= 0; }
390 };
391
392 class InterfaceTypeEntry;
393 class ObjectTypeEntry;
394
395 class TypeEntry
396 {
397 public:
398 enum Type {
399 PrimitiveType,
400 VoidType,
401 FlagsType,
402 EnumType,
403 TemplateArgumentType,
404 ThreadType,
405 BasicValueType,
406 StringType,
407 ContainerType,
408 InterfaceType,
409 ObjectType,
410 NamespaceType,
411 VariantType,
412 JObjectWrapperType,
413 CharType,
414 ArrayType,
415 TypeSystemType,
416 CustomType,
417 };
418
419 enum CodeGeneration {
420 GenerateTargetLang = 0x0001,
421 GenerateCpp = 0x0002,
422 GenerateForSubclass = 0x0004,
423
424 GenerateNothing = 0,
425 GenerateAll = 0xffff,
426 GenerateCode = GenerateTargetLang | GenerateCpp
427 };
428
429 TypeEntry(const QString &name, Type t)
430 : m_name(name),
431 m_type(t),
432 m_code_generation(GenerateAll),
433 m_preferred_conversion(true)
434 {
435 };
436
437 virtual ~TypeEntry() { }
438
439 Type type() const { return m_type; }
440 bool isPrimitive() const { return m_type == PrimitiveType; }
441 bool isEnum() const { return m_type == EnumType; }
442 bool isFlags() const { return m_type == FlagsType; }
443 bool isInterface() const { return m_type == InterfaceType; }
444 bool isObject() const { return m_type == ObjectType; }
445 bool isString() const { return m_type == StringType; }
446 bool isChar() const { return m_type == CharType; }
447 bool isNamespace() const { return m_type == NamespaceType; }
448 bool isContainer() const { return m_type == ContainerType; }
449 bool isVariant() const { return m_type == VariantType; }
450 bool isJObjectWrapper() const { return m_type == JObjectWrapperType; }
451 bool isArray() const { return m_type == ArrayType; }
452 bool isTemplateArgument() const { return m_type == TemplateArgumentType; }
453 bool isVoid() const { return m_type == VoidType; }
454 bool isThread() const { return m_type == ThreadType; }
455 bool isCustom() const { return m_type == CustomType; }
456 bool isBasicValue() const { return m_type == BasicValueType; }
457 bool isTypeSystem() const { return m_type == TypeSystemType; }
458
459 virtual bool preferredConversion() const { return m_preferred_conversion; }
460 virtual void setPreferredConversion(bool b) { m_preferred_conversion = b; }
461
462 virtual QString javaQualifier() const { return QString(); }
463
464 // The type's name in C++, fully qualified
465 QString name() const { return m_name; }
466
467 uint codeGeneration() const { return m_code_generation; }
468 void setCodeGeneration(uint cg) { m_code_generation = cg; }
469
470 virtual QString qualifiedCppName() const { return m_name; }
471
472 // Its type's name in JNI
473 virtual QString jniName() const { return m_name; }
474
475 // The type's name in TargetLang
476 virtual QString targetLangName() const { return m_name; }
477
478 // The type to lookup when converting to TargetLang
479 virtual QString lookupName() const { return targetLangName(); }
480
481 // The package
482 virtual QString javaPackage() const { return QString(); }
483
484 virtual QString qualifiedTargetLangName() const {
485 // QString pkg = javaPackage();
486 /* if (pkg.isEmpty())*/ return targetLangName();
487 // return pkg + '.' + targetLangName();
488 }
489
490 virtual InterfaceTypeEntry *designatedInterface() const { return 0; }
491
492 void setCustomConstructor(const CustomFunction &func) { m_customConstructor = func; }
493 CustomFunction customConstructor() const { return m_customConstructor; }
494
495 void setCustomDestructor(const CustomFunction &func) { m_customDestructor = func; }
496 CustomFunction customDestructor() const { return m_customDestructor; }
497
498 virtual bool isValue() const { return false; }
499 virtual bool isComplex() const { return false; }
500
501 virtual bool isNativeIdBased() const { return false; }
502
503 // qtd
504 virtual bool isStructInD() const { return false; }
505
506 private:
507 QString m_name;
508 Type m_type;
509 uint m_code_generation;
510 CustomFunction m_customConstructor;
511 CustomFunction m_customDestructor;
512 bool m_preferred_conversion;
513 };
514 typedef QHash<QString, QList<TypeEntry *> > TypeEntryHash;
515 typedef QHash<QString, TypeEntry *> SingleTypeEntryHash;
516
517
518 class TypeSystemTypeEntry : public TypeEntry
519 {
520 public:
521 TypeSystemTypeEntry(const QString &name)
522 : TypeEntry(name, TypeSystemType)
523 {
524 };
525
526 QList<CodeSnip> snips;
527 };
528
529
530 class ThreadTypeEntry : public TypeEntry
531 {
532 public:
533 ThreadTypeEntry() : TypeEntry("QThread", ThreadType) { setCodeGeneration(GenerateNothing); }
534
535 QString jniName() const { return strings_jobject; }
536 QString targetLangName() const { return strings_Thread; }
537 QString javaPackage() const { return strings_java_lang; }
538 };
539
540 class VoidTypeEntry : public TypeEntry
541 {
542 public:
543 VoidTypeEntry() : TypeEntry("void", VoidType) { }
544 };
545
546 class TemplateArgumentEntry : public TypeEntry
547 {
548 public:
549 TemplateArgumentEntry(const QString &name)
550 : TypeEntry(name, TemplateArgumentType), m_ordinal(0)
551 {
552 }
553
554 int ordinal() const { return m_ordinal; }
555 void setOrdinal(int o) { m_ordinal = o; }
556
557 private:
558 int m_ordinal;
559 };
560
561 class ArrayTypeEntry : public TypeEntry
562 {
563 public:
564 ArrayTypeEntry(const TypeEntry *nested_type) : TypeEntry("Array", ArrayType), m_nested_type(nested_type)
565 {
566 Q_ASSERT(m_nested_type);
567 }
568
569 void setNestedTypeEntry(TypeEntry *nested) { m_nested_type = nested; }
570 const TypeEntry *nestedTypeEntry() const { return m_nested_type; }
571
572 QString targetLangName() const { return m_nested_type->targetLangName() + "[]"; }
573 QString jniName() const
574 {
575 if (m_nested_type->isPrimitive())
576 return m_nested_type->jniName() + "Array";
577 else
578 return "jobjectArray";
579 }
580
581 private:
582 const TypeEntry *m_nested_type;
583 };
584
585
586 class PrimitiveTypeEntry : public TypeEntry
587 {
588 public:
589 PrimitiveTypeEntry(const QString &name)
590 : TypeEntry(name, PrimitiveType), m_preferred_conversion(true), m_preferred_java_type(true)
591 {
592 }
593
594 QString targetLangName() const { return m_java_name; }
595 void setTargetLangName(const QString &targetLangName) { m_java_name = targetLangName; }
596
597 QString jniName() const { return m_jni_name; }
598 void setJniName(const QString &jniName) { m_jni_name = jniName; }
599
600 QString javaObjectFullName() const { return javaObjectPackage() + "." + javaObjectName(); }
601 QString javaObjectName() const;
602 QString javaObjectPackage() const { return strings_java_lang; }
603
604 virtual bool preferredConversion() const { return m_preferred_conversion; }
605 virtual void setPreferredConversion(bool b) { m_preferred_conversion = b; }
606
607 virtual bool preferredTargetLangType() const { return m_preferred_java_type; }
608 virtual void setPreferredTargetLangType(bool b) { m_preferred_java_type = b; }
609
610 private:
611 QString m_java_name;
612 QString m_jni_name;
613 uint m_preferred_conversion : 1;
614 uint m_preferred_java_type : 1;
615 };
616
617
618
619
620 struct EnumValueRedirection
621 {
622 EnumValueRedirection(const QString &rej, const QString &us)
623 : rejected(rej),
624 used(us)
625 {
626 }
627 QString rejected;
628 QString used;
629 };
630
631 class EnumTypeEntry : public TypeEntry
632 {
633 public:
634 EnumTypeEntry(const QString &nspace, const QString &enumName)
635 : TypeEntry(nspace.isEmpty() ? enumName : nspace + QLatin1String("::") + enumName,
636 EnumType),
637 m_flags(0),
638 m_extensible(false)
639 {
640 m_qualifier = nspace;
641 m_java_name = enumName;
642 }
643
644 QString javaPackage() const { return m_package_name; }
645 void setTargetLangPackage(const QString &package) { m_package_name = package; }
646
647 QString targetLangName() const { return m_java_name; }
648 QString javaQualifier() const;
649 QString qualifiedTargetLangName() const {
650 return javaQualifier() + '_' + targetLangName();
651 // return targetLangName();
652 }
653
654 QString jniName() const;
655
656 QString qualifier() const { return m_qualifier; }
657 void setQualifier(const QString &q) { m_qualifier = q; }
658
659 virtual bool preferredConversion() const { return false; }
660
661 bool isBoundsChecked() const { return m_lower_bound.isEmpty() && m_upper_bound.isEmpty(); }
662
663 QString upperBound() const { return m_upper_bound; }
664 void setUpperBound(const QString &bound) { m_upper_bound = bound; }
665
666 QString lowerBound() const { return m_lower_bound; }
667 void setLowerBound(const QString &bound) { m_lower_bound = bound; }
668
669 void setFlags(FlagsTypeEntry *flags) { m_flags = flags; }
670 FlagsTypeEntry *flags() const { return m_flags; }
671
672 bool isExtensible() const { return m_extensible; }
673 void setExtensible(bool is) { m_extensible = is; }
674
675 bool isEnumValueRejected(const QString &name) { return m_rejected_enums.contains(name); }
676 void addEnumValueRejection(const QString &name) { m_rejected_enums << name; }
677 QStringList enumValueRejections() const { return m_rejected_enums; }
678
679 void addEnumValueRedirection(const QString &rejected, const QString &usedValue);
680 QString enumValueRedirection(const QString &value) const;
681
682 bool forceInteger() const { return m_force_integer; }
683 void setForceInteger(bool force) { m_force_integer = force; }
684
685 private:
686 QString m_package_name;
687 QString m_qualifier;
688 QString m_java_name;
689
690 QString m_lower_bound;
691 QString m_upper_bound;
692
693 QStringList m_rejected_enums;
694 QList<EnumValueRedirection> m_enum_redirections;
695
696 FlagsTypeEntry *m_flags;
697
698 bool m_extensible;
699 bool m_force_integer;
700 };
701
702 class FlagsTypeEntry : public TypeEntry
703 {
704 public:
705 FlagsTypeEntry(const QString &name) : TypeEntry(name, FlagsType), m_enum(0)
706 {
707 }
708
709 QString qualifiedTargetLangName() const;
710 QString targetLangName() const { return m_java_name; }
711 QString jniName() const;
712 virtual bool preferredConversion() const { return false; }
713
714 QString originalName() const { return m_original_name; }
715 void setOriginalName(const QString &s) { m_original_name = s; }
716
717 QString flagsName() const { return m_java_name; }
718 void setFlagsName(const QString &name) { m_java_name = name; }
719
720 bool forceInteger() const { return m_enum->forceInteger(); }
721
722 EnumTypeEntry *originator() const { return m_enum; }
723 void setOriginator(EnumTypeEntry *e) { m_enum = e; }
724
725 QString javaPackage() const { return m_enum->javaPackage(); }
726 QString javaQualifier() const { return m_enum->javaQualifier(); }
727 QString qualifier() const { return m_enum->qualifier(); }
728
729 private:
730 QString m_original_name;
731 QString m_java_name;
732 EnumTypeEntry *m_enum;
733 };
734
735
736 class ComplexTypeEntry : public TypeEntry
737 {
738 public:
739 enum TypeFlag {
740 ForceAbstract = 0x1,
741 DeleteInMainThread = 0x2,
742 Deprecated = 0x4
743 };
744 typedef QFlags<TypeFlag> TypeFlags;
745
746 ComplexTypeEntry(const QString &name, Type t)
747 : TypeEntry(QString(name).replace("::", "_"), t),
748 m_qualified_cpp_name(name),
749 m_qobject(false),
750 m_polymorphic_base(false),
751 m_generic_class(false),
752 m_type_flags(0),
753 m_isStructInD(false),
754 m_isAbstract(false)
755 {
756 Include inc;
757 inc.name = "QVariant";
758 inc.type = Include::IncludePath;
759
760 addExtraInclude(inc);
761 }
762
763 bool isComplex() const { return true; }
764
765 IncludeList extraIncludes() const { return m_extra_includes; }
766 void setExtraIncludes(const IncludeList &includes) { m_extra_includes = includes; }
767 void addExtraInclude(const Include &include)
768 {
769 if (!m_includes_used.value(include.name, false)) {
770 m_extra_includes << include;
771 m_includes_used[include.name] = true;
772 }
773 }
774
775 ComplexTypeEntry *copy() const
776 {
777 ComplexTypeEntry *centry = new ComplexTypeEntry(name(), type());
778 centry->setInclude(include());
779 centry->setExtraIncludes(extraIncludes());
780 centry->setFunctionModifications(functionModifications());
781 centry->setFieldModifications(fieldModifications());
782 centry->setQObject(isQObject());
783 centry->setDefaultSuperclass(defaultSuperclass());
784 centry->setCodeSnips(codeSnips());
785 centry->setTargetLangPackage(javaPackage());
786
787 return centry;
788 }
789
790 void setLookupName(const QString &name)
791 {
792 m_lookup_name = name;
793 }
794
795 virtual QString lookupName() const
796 {
797 return m_lookup_name.isEmpty() ? targetLangName() : m_lookup_name;
798 }
799
800 QString jniName() const { return strings_jobject; }
801
802
803 Include include() const { return m_include; }
804 void setInclude(const Include &inc) { m_include = inc; }
805
806 void setTypeFlags(TypeFlags flags)
807 {
808 m_type_flags = flags;
809 }
810
811 TypeFlags typeFlags() const
812 {
813 return m_type_flags;
814 }
815
816 CodeSnipList codeSnips() const { return m_code_snips; }
817 void setCodeSnips(const CodeSnipList &codeSnips) { m_code_snips = codeSnips; }
818 void addCodeSnip(const CodeSnip &codeSnip) { m_code_snips << codeSnip; }
819
820 FunctionModificationList functionModifications() const { return m_function_mods; }
821 void setFunctionModifications(const FunctionModificationList &functionModifications) {
822 m_function_mods = functionModifications;
823 }
824 void addFunctionModification(const FunctionModification &functionModification) {
825 m_function_mods << functionModification;
826 }
827 FunctionModificationList functionModifications(const QString &signature) const;
828
829 FieldModification fieldModification(const QString &name) const;
830 void setFieldModifications(const FieldModificationList &mods) { m_field_mods = mods; }
831 FieldModificationList fieldModifications() const { return m_field_mods; }
832
833 QString javaPackage() const { return m_package; }
834 void setTargetLangPackage(const QString &package) { m_package = package; }
835
836 bool isQObject() const { return m_qobject; }
837 void setQObject(bool qobject) { m_qobject = qobject; }
838
839 QString defaultSuperclass() const { return m_default_superclass; }
840 void setDefaultSuperclass(const QString &sc) { m_default_superclass = sc; }
841
842 virtual QString qualifiedCppName() const { return m_qualified_cpp_name; }
843
844
845 void setIsPolymorphicBase(bool on)
846 {
847 m_polymorphic_base = on;
848 }
849 bool isPolymorphicBase() const { return m_polymorphic_base; }
850
851 void setPolymorphicIdValue(const QString &value)
852 {
853 m_polymorphic_id_value = value;
854 }
855 QString polymorphicIdValue() const { return m_polymorphic_id_value; }
856
857 void setExpensePolicy(const ExpensePolicy &policy) { m_expense_policy = policy; }
858 const ExpensePolicy &expensePolicy() const { return m_expense_policy; }
859
860 QString targetType() const { return m_target_type; }
861 void setTargetType(const QString &code) { m_target_type = code; }
862
863 QString targetLangName() const { return m_java_name.isEmpty()
864 ? TypeEntry::targetLangName()
865 : m_java_name;
866 }
867 void setTargetLangName(const QString &name) { m_java_name = name; }
868
869 bool isGenericClass() const { return m_generic_class; }
870 void setGenericClass(bool isGeneric) { m_generic_class = isGeneric; }
871
872 QString injectedImports;
873
874 // qtd
875 bool isStructInD() const { return m_isStructInD; }
876 void setStructInD(bool isStruct) { m_isStructInD = isStruct; }
877
878 bool isAbstract() const { return m_isAbstract; }
879 void setAbstract(bool isAbstract) { m_isAbstract = isAbstract; }
880
881 QString addedTo;
882 QStringList includedClasses;
883
884
885 private:
886 IncludeList m_extra_includes;
887 Include m_include;
888 QHash<QString, bool> m_includes_used;
889 FunctionModificationList m_function_mods;
890 FieldModificationList m_field_mods;
891 CodeSnipList m_code_snips;
892 QString m_package;
893 QString m_default_superclass;
894 QString m_qualified_cpp_name;
895 QString m_java_name;
896
897 uint m_qobject : 1;
898 uint m_polymorphic_base : 1;
899 uint m_generic_class : 1;
900
901 QString m_polymorphic_id_value;
902 QString m_lookup_name;
903 QString m_target_type;
904 ExpensePolicy m_expense_policy;
905 TypeFlags m_type_flags;
906
907 // qtd
908 bool m_isStructInD;
909 bool m_isAbstract;
910 };
911
912 class ContainerTypeEntry : public ComplexTypeEntry
913 {
914 public:
915 enum Type {
916 NoContainer,
917 ListContainer,
918 StringListContainer,
919 LinkedListContainer,
920 VectorContainer,
921 StackContainer,
922 QueueContainer,
923 SetContainer,
924 MapContainer,
925 MultiMapContainer,
926 HashContainer,
927 MultiHashContainer,
928 PairContainer,
929 };
930
931 ContainerTypeEntry(const QString &name, Type type)
932 : ComplexTypeEntry(name, ContainerType)
933 {
934 m_type = type;
935 setCodeGeneration(GenerateForSubclass);
936 }
937
938 Type type() const { return m_type; }
939 QString targetLangName() const;
940 QString javaPackage() const;
941 QString qualifiedCppName() const;
942
943 private:
944 Type m_type;
945 };
946
947
948 class NamespaceTypeEntry : public ComplexTypeEntry
949 {
950 public:
951 NamespaceTypeEntry(const QString &name) : ComplexTypeEntry(name, NamespaceType) { }
952 };
953
954
955 class ValueTypeEntry : public ComplexTypeEntry
956 {
957 public:
958 ValueTypeEntry(const QString &name) : ComplexTypeEntry(name, BasicValueType) { }
959
960 bool isValue() const { return true; }
961
962 virtual bool isNativeIdBased() const { return true; }
963
964 protected:
965 ValueTypeEntry(const QString &name, Type t) : ComplexTypeEntry(name, t) { }
966 };
967
968
969 class StringTypeEntry : public ValueTypeEntry
970 {
971 public:
972 StringTypeEntry(const QString &name)
973 : ValueTypeEntry(name, StringType)
974 {
975 setCodeGeneration(GenerateNothing);
976 }
977
978 QString jniName() const { return strings_jobject; }
979 QString targetLangName() const { return strings_String; }
980 QString javaPackage() const { return strings_java_lang; }
981
982 virtual bool isNativeIdBased() const { return false; }
983 };
984
985 class CharTypeEntry : public ValueTypeEntry
986 {
987 public:
988 CharTypeEntry(const QString &name) : ValueTypeEntry(name, CharType)
989 {
990 setCodeGeneration(GenerateNothing);
991 }
992
993 QString jniName() const { return strings_jchar; }
994 QString targetLangName() const { return strings_char; }
995 QString javaPackage() const { return QString(); }
996
997 virtual bool isNativeIdBased() const { return false; }
998 };
999
1000 class JObjectWrapperTypeEntry: public ValueTypeEntry
1001 {
1002 public:
1003 JObjectWrapperTypeEntry(const QString &name) : ValueTypeEntry(name, JObjectWrapperType) { }
1004
1005 QString jniName() const { return strings_jobject; }
1006 QString targetLangName() const { return strings_Object; }
1007 QString javaPackage() const { return strings_java_lang; }
1008
1009 bool isNativeIdBased() const { return false; }
1010 };
1011
1012 class VariantTypeEntry: public ValueTypeEntry
1013 {
1014 public:
1015 VariantTypeEntry(const QString &name) : ValueTypeEntry(name, VariantType) { }
1016
1017 QString jniName() const { return strings_jobject; }
1018 QString targetLangName() const { return "QVariant"; }
1019 QString javaPackage() const { return "qt.core"; }
1020 virtual bool isNativeIdBased() const { return false; }
1021 };
1022
1023
1024 class InterfaceTypeEntry : public ComplexTypeEntry
1025 {
1026 public:
1027 InterfaceTypeEntry(const QString &name)
1028 : ComplexTypeEntry(name, InterfaceType)
1029 {
1030 }
1031
1032 static QString interfaceName(const QString &name) {
1033 return "I" + name;
1034 }
1035
1036 ObjectTypeEntry *origin() const { return m_origin; }
1037 void setOrigin(ObjectTypeEntry *origin) { m_origin = origin; }
1038
1039 virtual bool isNativeIdBased() const { return true; }
1040 virtual QString qualifiedCppName() const {
1041 return ComplexTypeEntry::qualifiedCppName().right(ComplexTypeEntry::qualifiedCppName().length() - interfaceName("").length());
1042 }
1043
1044 private:
1045 ObjectTypeEntry *m_origin;
1046 };
1047
1048
1049 class ObjectTypeEntry : public ComplexTypeEntry
1050 {
1051 public:
1052 ObjectTypeEntry(const QString &name)
1053 : ComplexTypeEntry(name, ObjectType), m_interface(0)
1054 {
1055 }
1056
1057 InterfaceTypeEntry *designatedInterface() const { return m_interface; }
1058 void setDesignatedInterface(InterfaceTypeEntry *entry) { m_interface = entry; }
1059
1060 virtual bool isNativeIdBased() const { return true; }
1061
1062 private:
1063 InterfaceTypeEntry *m_interface;
1064 };
1065
1066 class CustomTypeEntry : public ComplexTypeEntry
1067 {
1068 public:
1069 CustomTypeEntry(const QString &name) : ComplexTypeEntry(name, CustomType) { }
1070
1071 virtual void generateCppJavaToQt(QTextStream &s,
1072 const AbstractMetaType *java_type,
1073 const QString &env_name,
1074 const QString &qt_name,
1075 const QString &java_name) const = 0;
1076
1077 virtual void generateCppQtToJava(QTextStream &s,
1078 const AbstractMetaType *java_type,
1079 const QString &env_name,
1080 const QString &qt_name,
1081 const QString &java_name) const = 0;
1082 };
1083
1084 struct TypeRejection
1085 {
1086 QString class_name;
1087 QString function_name;
1088 QString field_name;
1089 QString enum_name;
1090 };
1091
1092 class TypeDatabase
1093 {
1094 public:
1095 TypeDatabase();
1096
1097 static TypeDatabase *instance();
1098
1099 QList<Include> extraIncludes(const QString &className);
1100
1101 inline PrimitiveTypeEntry *findPrimitiveType(const QString &name);
1102 inline ComplexTypeEntry *findComplexType(const QString &name);
1103 inline ObjectTypeEntry *findObjectType(const QString &name);
1104 inline NamespaceTypeEntry *findNamespaceType(const QString &name);
1105 ContainerTypeEntry *findContainerType(const QString &name);
1106
1107 TypeEntry *findType(const QString &name) const {
1108 QList<TypeEntry *> entries = findTypes(name);
1109 foreach (TypeEntry *entry, entries) {
1110 if (entry != 0 &&
1111 (!entry->isPrimitive() || static_cast<PrimitiveTypeEntry *>(entry)->preferredTargetLangType())) {
1112 return entry;
1113 }
1114 }
1115 return 0;
1116 }
1117 QList<TypeEntry *> findTypes(const QString &name) const { return m_entries.value(name); }
1118 TypeEntryHash allEntries() { return m_entries; }
1119 SingleTypeEntryHash entries() {
1120 TypeEntryHash entries = allEntries();
1121
1122 SingleTypeEntryHash returned;
1123 QList<QString> keys = entries.keys();
1124
1125 foreach(QString key, keys) {
1126 returned[key] = findType(key);
1127 }
1128
1129 return returned;
1130 }
1131
1132 PrimitiveTypeEntry *findTargetLangPrimitiveType(const QString &java_name);
1133
1134 void addRejection(const QString &class_name, const QString &function_name,
1135 const QString &field_name, const QString &enum_name);
1136 bool isClassRejected(const QString &class_name);
1137 bool isFunctionRejected(const QString &class_name, const QString &function_name);
1138 bool isFieldRejected(const QString &class_name, const QString &field_name);
1139 bool isEnumRejected(const QString &class_name, const QString &enum_name);
1140
1141 void addType(TypeEntry *e) { m_entries[e->qualifiedCppName()].append(e); }
1142
1143 SingleTypeEntryHash flagsEntries() const { return m_flags_entries; }
1144 FlagsTypeEntry *findFlagsType(const QString &name) const;
1145 void addFlagsType(FlagsTypeEntry *fte) { m_flags_entries[fte->originalName()] = fte; }
1146
1147 TemplateEntry *findTemplate(const QString &name) { return m_templates[name]; }
1148 void addTemplate(TemplateEntry *t) { m_templates[t->name()] = t; }
1149
1150 void setIncludeEclipseWarnings(bool on) { m_includeEclipseWarnings = on; }
1151 bool includeEclipseWarnings() const { return m_includeEclipseWarnings; }
1152
1153 void setSuppressWarnings(bool on) { m_suppressWarnings = on; }
1154 void addSuppressedWarning(const QString &s)
1155 {
1156 m_suppressedWarnings.append(s);
1157 }
1158
1159 bool isSuppressedWarning(const QString &s)
1160 {
1161 if (!m_suppressWarnings)
1162 return false;
1163
1164 foreach (const QString &_warning, m_suppressedWarnings) {
1165 QString warning(QString(_warning).replace("\\*", "&place_holder_for_asterisk;"));
1166
1167 QStringList segs = warning.split("*", QString::SkipEmptyParts);
1168 if (segs.size() == 0)
1169 continue ;
1170
1171 int i = 0;
1172 int pos = s.indexOf(QString(segs.at(i++)).replace("&place_holder_for_asterisk;", "*"));
1173 //qDebug() << "s == " << s << ", warning == " << segs;
1174 while (pos != -1) {
1175 if (i == segs.size())
1176 return true;
1177 pos = s.indexOf(QString(segs.at(i++)).replace("&place_holder_for_asterisk;", "*"), pos);
1178 }
1179 }
1180
1181 return false;
1182 }
1183
1184 void setRebuildClasses(const QStringList &cls) { m_rebuild_classes = cls; }
1185
1186 static QString globalNamespaceClassName(const TypeEntry *te);
1187 QString filename() const { return "typesystem.txt"; }
1188
1189 bool parseFile(const QString &filename, bool generate = true);
1190
1191 private:
1192 uint m_suppressWarnings : 1;
1193 uint m_includeEclipseWarnings : 1;
1194 uint m_reserved : 30;
1195
1196 TypeEntryHash m_entries;
1197 SingleTypeEntryHash m_flags_entries;
1198 TemplateEntryHash m_templates;
1199 QStringList m_suppressedWarnings;
1200
1201 QList<TypeRejection> m_rejections;
1202 QStringList m_rebuild_classes;
1203 };
1204
1205 inline PrimitiveTypeEntry *TypeDatabase::findPrimitiveType(const QString &name)
1206 {
1207 QList<TypeEntry *> entries = findTypes(name);
1208
1209 foreach (TypeEntry *entry, entries) {
1210 if (entry != 0 && entry->isPrimitive() && static_cast<PrimitiveTypeEntry *>(entry)->preferredTargetLangType())
1211 return static_cast<PrimitiveTypeEntry *>(entry);
1212 }
1213
1214 return 0;
1215 }
1216
1217 inline ComplexTypeEntry *TypeDatabase::findComplexType(const QString &name)
1218 {
1219 TypeEntry *entry = findType(name);
1220 if (entry != 0 && entry->isComplex())
1221 return static_cast<ComplexTypeEntry *>(entry);
1222 else
1223 return 0;
1224 }
1225
1226 inline ObjectTypeEntry *TypeDatabase::findObjectType(const QString &name)
1227 {
1228 TypeEntry *entry = findType(name);
1229 if (entry != 0 && entry->isObject())
1230 return static_cast<ObjectTypeEntry *>(entry);
1231 else
1232 return 0;
1233 }
1234
1235 inline NamespaceTypeEntry *TypeDatabase::findNamespaceType(const QString &name)
1236 {
1237 TypeEntry *entry = findType(name);
1238 if (entry != 0 && entry->isNamespace())
1239 return static_cast<NamespaceTypeEntry *>(entry);
1240 else
1241 return 0;
1242 }
1243
1244 QString fixCppTypeName(const QString &name);
1245
1246 #endif // TYPESYSTEM_H