-
Notifications
You must be signed in to change notification settings - Fork 8
/
typesystem.h
1960 lines (1685 loc) · 44.5 KB
/
typesystem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the API Extractor project.
*
* Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
*
* Contact: PySide team <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#ifndef TYPESYSTEM_H
#define TYPESYSTEM_H
#include <QtCore/QHash>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QMap>
#include <QtCore/QDebug>
#include "apiextractormacros.h"
#include "include.h"
//Used to identify the conversion rule to avoid break API
#define TARGET_CONVERSION_RULE_FLAG "0"
#define NATIVE_CONVERSION_RULE_FLAG "1"
class Indentor;
class AbstractMetaType;
class QTextStream;
class EnumTypeEntry;
class FlagsTypeEntry;
typedef QMap<int, QString> ArgumentMap;
class TemplateInstance;
namespace TypeSystem
{
enum Language {
NoLanguage = 0x0000,
TargetLangCode = 0x0001,
NativeCode = 0x0002,
ShellCode = 0x0004,
ShellDeclaration = 0x0008,
PackageInitializer = 0x0010,
DestructorFunction = 0x0020,
Constructors = 0x0040,
Interface = 0x0080,
// masks
All = TargetLangCode
| NativeCode
| ShellCode
| ShellDeclaration
| PackageInitializer
| Constructors
| Interface
| DestructorFunction,
TargetLangAndNativeCode = TargetLangCode | NativeCode
};
enum Ownership {
InvalidOwnership,
DefaultOwnership,
TargetLangOwnership,
CppOwnership
};
};
struct APIEXTRACTOR_API ReferenceCount
{
ReferenceCount() {}
enum Action { // 0x01 - 0xff
Invalid = 0x00,
Add = 0x01,
AddAll = 0x02,
Remove = 0x04,
Set = 0x08,
Ignore = 0x10,
ActionsMask = 0xff,
Padding = 0xffffffff
};
Action action;
QString varName;
};
struct APIEXTRACTOR_API ArgumentOwner
{
enum Action {
Invalid = 0x00,
Add = 0x01,
Remove = 0x02
};
enum {
InvalidIndex = -2,
ThisIndex = -1,
ReturnIndex = 0,
FirstArgumentIndex = 1
};
ArgumentOwner() : action(ArgumentOwner::Invalid), index(ArgumentOwner::InvalidIndex) {}
Action action;
int index;
};
class APIEXTRACTOR_API CodeSnipFragment
{
private:
QString m_code;
TemplateInstance *m_instance;
public:
CodeSnipFragment(const QString &code)
: m_code(code),
m_instance(0) {}
CodeSnipFragment(TemplateInstance *instance)
: m_instance(instance) {}
QString code() const;
};
class APIEXTRACTOR_API CodeSnipAbstract
{
public:
QString code() const;
void addCode(const QString &code)
{
codeList.append(CodeSnipFragment(code));
}
void addTemplateInstance(TemplateInstance *ti)
{
codeList.append(CodeSnipFragment(ti));
}
QList<CodeSnipFragment> codeList;
};
class APIEXTRACTOR_API CustomFunction : public CodeSnipAbstract
{
public:
CustomFunction(const QString &n = QString()) : name(n) { }
QString name;
QString paramName;
};
class APIEXTRACTOR_API TemplateEntry : public CodeSnipAbstract
{
public:
TemplateEntry(const QString &name, double vr)
: m_name(name), m_version(vr)
{
};
QString name() const
{
return m_name;
};
double version() const
{
return m_version;
}
private:
QString m_name;
double m_version;
};
typedef QHash<QString, TemplateEntry *> TemplateEntryHash;
class APIEXTRACTOR_API TemplateInstance
{
public:
TemplateInstance(const QString &name, double vr)
: m_name(name), m_version(vr) {}
void addReplaceRule(const QString &name, const QString &value)
{
replaceRules[name] = value;
}
QString expandCode() const;
QString name() const
{
return m_name;
}
double version() const
{
return m_version;
}
private:
const QString m_name;
double m_version;
QHash<QString, QString> replaceRules;
};
class APIEXTRACTOR_API CodeSnip : public CodeSnipAbstract
{
public:
enum Position {
Beginning,
End,
AfterThis,
// QtScript
Declaration,
PrototypeInitialization,
ConstructorInitialization,
Constructor,
Any
};
CodeSnip(double vr) : language(TypeSystem::TargetLangCode), version(vr) { }
CodeSnip(double vr, TypeSystem::Language lang) : language(lang), version(vr) { }
TypeSystem::Language language;
Position position;
ArgumentMap argumentMap;
double version;
};
typedef QList<CodeSnip> CodeSnipList;
struct APIEXTRACTOR_API ArgumentModification
{
ArgumentModification(int idx, double vr)
: removedDefaultExpression(false), removed(false),
noNullPointers(false), index(idx), version(vr) {}
// Should the default expression be removed?
uint removedDefaultExpression : 1;
uint removed : 1;
uint noNullPointers : 1;
uint resetAfterUse : 1;
// The index of this argument
int index;
// Reference count flags for this argument
QList<ReferenceCount> referenceCounts;
// The text given for the new type of the argument
QString modified_type;
QString replace_value;
// The code to be used to construct a return value when noNullPointers is true and
// the returned value is null. If noNullPointers is true and this string is
// empty, then the base class implementation will be used (or a default construction
// if there is no implementation)
QString nullPointerDefaultValue;
// The text of the new default expression of the argument
QString replacedDefaultExpression;
// The new definition of ownership for a specific argument
QHash<TypeSystem::Language, TypeSystem::Ownership> ownerships;
// Different conversion rules
CodeSnipList conversion_rules;
//QObject parent(owner) of this argument
ArgumentOwner owner;
//Api version
double version;
//New name
QString renamed_to;
};
struct APIEXTRACTOR_API Modification
{
enum Modifiers {
Private = 0x0001,
Protected = 0x0002,
Public = 0x0003,
Friendly = 0x0004,
AccessModifierMask = 0x000f,
Final = 0x0010,
NonFinal = 0x0020,
FinalMask = Final | NonFinal,
Readable = 0x0100,
Writable = 0x0200,
CodeInjection = 0x1000,
Rename = 0x2000,
Deprecated = 0x4000,
ReplaceExpression = 0x8000,
VirtualSlot = 0x10000 | NonFinal
};
Modification() : modifiers(0), removal(TypeSystem::NoLanguage) { }
bool isAccessModifier() const
{
return modifiers & AccessModifierMask;
}
Modifiers accessModifier() const
{
return Modifiers(modifiers & AccessModifierMask);
}
bool isPrivate() const
{
return accessModifier() == Private;
}
bool isProtected() const
{
return accessModifier() == Protected;
}
bool isPublic() const
{
return accessModifier() == Public;
}
bool isFriendly() const
{
return accessModifier() == Friendly;
}
bool isFinal() const
{
return modifiers & Final;
}
bool isNonFinal() const
{
return modifiers & NonFinal;
}
bool isVirtualSlot() const
{
return (modifiers & VirtualSlot) == VirtualSlot;
}
QString accessModifierString() const;
bool isDeprecated() const
{
return modifiers & Deprecated;
}
void setRenamedTo(const QString &name)
{
renamedToName = name;
}
QString renamedTo() const
{
return renamedToName;
}
bool isRenameModifier() const
{
return modifiers & Rename;
}
bool isRemoveModifier() const
{
return removal != TypeSystem::NoLanguage;
}
uint modifiers;
QString renamedToName;
TypeSystem::Language removal;
};
struct APIEXTRACTOR_API FunctionModification: public Modification
{
FunctionModification(double vr) : m_thread(false), m_allowThread(false), m_version(vr) {}
bool isCodeInjection() const
{
return modifiers & CodeInjection;
}
void setIsThread(bool flag)
{
m_thread = flag;
}
bool isThread() const
{
return m_thread;
}
bool allowThread() const
{
return m_allowThread;
}
void setAllowThread(bool allow)
{
m_allowThread = allow;
}
double version() const
{
return m_version;
}
bool operator!=(const FunctionModification& other) const;
bool operator==(const FunctionModification& other) const;
QString toString() const;
QString signature;
QString association;
CodeSnipList snips;
QList<ArgumentModification> argument_mods;
private:
FunctionModification() {}
bool m_thread;
bool m_allowThread;
double m_version;
};
typedef QList<FunctionModification> FunctionModificationList;
struct APIEXTRACTOR_API FieldModification: public Modification
{
bool isReadable() const
{
return modifiers & Readable;
}
bool isWritable() const
{
return modifiers & Writable;
}
QString name;
};
typedef QList<FieldModification> FieldModificationList;
/**
* \internal
* Struct used to store information about functions added by the typesystem.
* This info will be used later to create a fake AbstractMetaFunction which
* will be inserted into the right AbstractMetaClass.
*/
struct APIEXTRACTOR_API AddedFunction
{
/// Function access types.
enum Access {
Protected = 0x1,
Public = 0x2
};
/**
* \internal
* Internal struct used to store information about arguments and return type of the
* functions added by the type system. This information is later used to create
* AbstractMetaType and AbstractMetaArgument for the AbstractMetaFunctions.
*/
struct TypeInfo {
TypeInfo() : isConstant(false), indirections(0), isReference(false) {}
QString name;
bool isConstant;
int indirections;
bool isReference;
QString defaultValue;
};
/// Creates a new AddedFunction with a signature and a return type.
AddedFunction(QString signature, QString returnType, double vr);
/// Returns the function name.
QString name() const
{
return m_name;
}
/// Set the function access type.
void setAccess(Access access)
{
m_access = access;
}
/// Returns the function access type.
Access access() const
{
return m_access;
}
/// Returns the function return type.
TypeInfo returnType() const
{
return m_returnType;
}
/// Returns a list of argument type infos.
QList<TypeInfo> arguments() const
{
return m_arguments;
}
/// Returns true if this is a constant method.
bool isConstant() const
{
return m_isConst;
}
/// Set this method static.
void setStatic(bool value)
{
m_isStatic = value;
}
/// Returns true if this is a static method.
bool isStatic() const
{
return m_isStatic;
}
double version() const
{
return m_version;
}
private:
QString m_name;
Access m_access;
QList<TypeInfo> m_arguments;
TypeInfo m_returnType;
bool m_isConst;
bool m_isStatic;
double m_version;
};
typedef QList<AddedFunction> AddedFunctionList;
struct APIEXTRACTOR_API ExpensePolicy
{
ExpensePolicy() : limit(-1) {}
int limit;
QString cost;
bool isValid() const
{
return limit >= 0;
}
};
class InterfaceTypeEntry;
class ObjectTypeEntry;
class APIEXTRACTOR_API DocModification
{
public:
enum Mode {
Append,
Prepend,
Replace,
XPathReplace
};
DocModification(const QString& xpath, const QString& signature, double vr)
: format(TypeSystem::NativeCode), m_mode(XPathReplace),
m_xpath(xpath), m_signature(signature), m_version(vr) {}
DocModification(Mode mode, const QString& signature, double vr)
: m_mode(mode), m_signature(signature), m_version(vr) {}
void setCode(const QString& code)
{
m_code = code;
}
QString code() const
{
return m_code;
}
QString xpath() const
{
return m_xpath;
}
QString signature() const
{
return m_signature;
}
Mode mode() const
{
return m_mode;
}
double version() const
{
return m_version;
}
TypeSystem::Language format;
private:
Mode m_mode;
QString m_code;
QString m_xpath;
QString m_signature;
double m_version;
};
typedef QList<DocModification> DocModificationList;
class CustomConversion;
class APIEXTRACTOR_API TypeEntry
{
public:
enum Type {
PrimitiveType,
VoidType,
VarargsType,
FlagsType,
EnumType,
EnumValue,
TemplateArgumentType,
ThreadType,
BasicValueType,
StringType,
ContainerType,
InterfaceType,
ObjectType,
NamespaceType,
VariantType,
JObjectWrapperType,
CharType,
ArrayType,
TypeSystemType,
CustomType,
TargetLangType,
FunctionType
};
enum CodeGeneration {
GenerateTargetLang = 0x0001,
GenerateCpp = 0x0002,
GenerateForSubclass = 0x0004,
GenerateNothing = 0,
GenerateAll = 0xffff,
GenerateCode = GenerateTargetLang | GenerateCpp
};
TypeEntry(const QString &name, Type t, double vr)
: m_name(name),
m_type(t),
m_codeGeneration(GenerateAll),
m_preferredConversion(true),
m_stream(false),
m_version(vr)
{
};
virtual ~TypeEntry();
Type type() const
{
return m_type;
}
bool isPrimitive() const
{
return m_type == PrimitiveType;
}
bool isEnum() const
{
return m_type == EnumType;
}
bool isFlags() const
{
return m_type == FlagsType;
}
bool isInterface() const
{
return m_type == InterfaceType;
}
bool isObject() const
{
return m_type == ObjectType;
}
bool isString() const
{
return m_type == StringType;
}
bool isChar() const
{
return m_type == CharType;
}
bool isNamespace() const
{
return m_type == NamespaceType;
}
bool isContainer() const
{
return m_type == ContainerType;
}
bool isVariant() const
{
return m_type == VariantType;
}
bool isJObjectWrapper() const
{
return m_type == JObjectWrapperType;
}
bool isArray() const
{
return m_type == ArrayType;
}
bool isTemplateArgument() const
{
return m_type == TemplateArgumentType;
}
bool isVoid() const
{
return m_type == VoidType;
}
bool isVarargs() const
{
return m_type == VarargsType;
}
bool isThread() const
{
return m_type == ThreadType;
}
bool isCustom() const
{
return m_type == CustomType;
}
bool isBasicValue() const
{
return m_type == BasicValueType;
}
bool isTypeSystem() const
{
return m_type == TypeSystemType;
}
bool isFunction() const
{
return m_type == FunctionType;
}
bool isEnumValue() const
{
return m_type == EnumValue;
}
virtual bool preferredConversion() const
{
return m_preferredConversion;
}
virtual void setPreferredConversion(bool b)
{
m_preferredConversion = b;
}
bool stream() const
{
return m_stream;
}
void setStream(bool b)
{
m_stream = b;
}
// The type's name in C++, fully qualified
QString name() const
{
return m_name;
}
uint codeGeneration() const
{
return m_codeGeneration;
}
void setCodeGeneration(uint cg)
{
m_codeGeneration = cg;
}
// Returns true if code must be generated for this entry,
// it will return false in case of types coming from typesystems
// included for reference only.
// NOTE: 'GenerateForSubclass' means 'generate="no"'
// on 'load-typesystem' tag
inline bool generateCode() const
{
return m_codeGeneration != TypeEntry::GenerateForSubclass
&& m_codeGeneration != TypeEntry::GenerateNothing;
}
virtual QString qualifiedCppName() const
{
return m_name;
}
/**
* Its type's name in target language API
* The target language API name represents how this type is
* referred on low level code for the target language.
* Examples: for Java this would be a JNI name, for Python
* it should represent the CPython type name.
* /return string representing the target language API name
* for this type entry
*/
virtual QString targetLangApiName() const
{
return m_name;
}
// The type's name in TargetLang
virtual QString targetLangName() const
{
return m_name;
}
// The type to lookup when converting to TargetLang
virtual QString lookupName() const
{
return targetLangName();
}
// The package
virtual QString targetLangPackage() const
{
return QString();
}
virtual QString qualifiedTargetLangName() const
{
QString pkg = targetLangPackage();
if (pkg.isEmpty())
return targetLangName();
return pkg + '.' + targetLangName();
}
virtual InterfaceTypeEntry *designatedInterface() const
{
return 0;
}
void setCustomConstructor(const CustomFunction &func)
{
m_customConstructor = func;
}
CustomFunction customConstructor() const
{
return m_customConstructor;
}
void setCustomDestructor(const CustomFunction &func)
{
m_customDestructor = func;
}
CustomFunction customDestructor() const
{
return m_customDestructor;
}
virtual bool isValue() const
{
return false;
}
virtual bool isComplex() const
{
return false;
}
virtual bool isNativeIdBased() const
{
return false;
}
CodeSnipList codeSnips() const;
void setCodeSnips(const CodeSnipList &codeSnips)
{
m_codeSnips = codeSnips;
}
void addCodeSnip(const CodeSnip &codeSnip)
{
m_codeSnips << codeSnip;
}
void setDocModification(const DocModificationList& docMods)
{
m_docModifications << docMods;
}
DocModificationList docModifications() const
{
return m_docModifications;
}
IncludeList extraIncludes() const
{
return m_extraIncludes;
}
void setExtraIncludes(const IncludeList &includes)
{
m_extraIncludes = includes;
}
void addExtraInclude(const Include &include)
{
if (!m_includesUsed.value(include.name(), false)) {
m_extraIncludes << include;
m_includesUsed[include.name()] = true;
}
}
Include include() const
{
return m_include;
}
void setInclude(const Include &inc)
{
m_include = inc;
}
// Replace conversionRule arg to CodeSnip in future version
/// Set the type convertion rule
void setConversionRule(const QString& conversionRule)
{
m_conversionRule = conversionRule;
}
/// Returns the type convertion rule
QString conversionRule() const
{
//skip conversions flag
return m_conversionRule.mid(1);
}
/// Returns true if there are any conversiton rule for this type, false otherwise.
bool hasConversionRule() const
{
return !m_conversionRule.isEmpty();
}
double version() const
{
return m_version;
}
/// TODO-CONVERTER: mark as deprecated
bool hasNativeConversionRule() const
{
return m_conversionRule.startsWith(NATIVE_CONVERSION_RULE_FLAG);
}
/// TODO-CONVERTER: mark as deprecated
bool hasTargetConversionRule() const
{
return m_conversionRule.startsWith(TARGET_CONVERSION_RULE_FLAG);
}
bool isCppPrimitive() const;
bool hasCustomConversion() const;
void setCustomConversion(CustomConversion* customConversion);
CustomConversion* customConversion() const;
private:
QString m_name;
Type m_type;
uint m_codeGeneration;
CustomFunction m_customConstructor;
CustomFunction m_customDestructor;
bool m_preferredConversion;
CodeSnipList m_codeSnips;
DocModificationList m_docModifications;
IncludeList m_extraIncludes;
Include m_include;
QHash<QString, bool> m_includesUsed;
QString m_conversionRule;
bool m_stream;
double m_version;
};
typedef QHash<QString, QList<TypeEntry *> > TypeEntryHash;
typedef QHash<QString, TypeEntry *> SingleTypeEntryHash;
class APIEXTRACTOR_API TypeSystemTypeEntry : public TypeEntry
{
public:
TypeSystemTypeEntry(const QString &name, double vr)
: TypeEntry(name, TypeSystemType, vr)
{
};
};
class APIEXTRACTOR_API VoidTypeEntry : public TypeEntry