-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUTLBMembers.pas
More file actions
2679 lines (2455 loc) · 71 KB
/
UTLBMembers.pas
File metadata and controls
2679 lines (2455 loc) · 71 KB
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
unit UTLBMembers;
interface
uses
Winapi.ActiveX,
System.SysUtils,
System.Generics.Collections,
UTLBClasses;
type
TAliasList = class
strict private
type
TItem = record
Idx: Integer;
OldName: string;
end;
strict private
FList: TDictionary<string, TItem>;
public
constructor Create;
destructor Destroy; override;
procedure AddAlias(const ANewName, AOldName: string); overload;
function AddAlias(const AName: string; ARefCnt: Integer; AStdUnit: TStdUnits): string; overload;
function AddAlias(const AType: TPasTypeInfo): string; overload;
procedure Print(AOut: TCustomOut);
end;
TIntfItem = class
strict private
FName: string;
FComment: string;
public
constructor Create(const AName: string);
function Print(AOut: TCustomOut): Boolean; virtual;
procedure RequireUnits(AUnitManager: TUnitManager); virtual;
procedure RegisterAliases(AList: TAliasList); virtual;
public
property Name: string read FName;
end;
TTLBMember = class;
TTLBMemberDict = class(TDictionary<string, TTLBMember>);
TTLBMember = class(TIntfItem)
strict private
FMembers: TTLBMemberDict;
FPrinted: Boolean;
strict protected
procedure ParseTypeInfo(const ATypeInfo: ITypeInfo); virtual;
procedure ParseTypeAttr(const ATypeInfo: ITypeInfo; const ATypeAttr: TTypeAttr); virtual;
procedure PrintRecursive(AOut: TCustomOut); virtual;
public
constructor CreateTLB(const ATypeLib: ITypeLib; AIdx: Integer);
constructor Create(const ATypeInfo: ITypeInfo); virtual;
function Print(AOut: TCustomOut): Boolean; override;
public
property Members: TTLBMemberDict read FMembers write FMembers;
end;
TAlias = class(TTLBMember)
strict private
FAlias: TPasTypeInfo;
FWriteType: string;
strict protected
procedure ParseTypeAttr(const ATypeInfo: ITypeInfo; const ATypeAttr: TTypeAttr); override;
public
function Print(AOut: TCustomOut): Boolean; override;
procedure RequireUnits(AUnitManager: TUnitManager); override;
procedure RegisterAliases(AList: TAliasList); override;
end;
TTypeMember = class(TIntfItem)
public
constructor Create(const ATypeInfo: ITypeInfo; AMemberID: TMemberID);
end;
TMethodArg = class(TIntfItem)
strict private
const
CFlagIn = $01;
CFlagOut = $02;
CFlagRetVal = $04;
strict private
FType: TPasTypeInfo;
FWriteType: string;
FFlag: Byte;
public
constructor Create(const ATypeInfo: ITypeInfo; const AName: string;
const ADesc: TElemDesc);
function Print(AOut: TCustomOut): Boolean; override;
procedure RequireUnits(AUnitManager: TUnitManager); override;
function ToString: string; override;
function IsRetVal: Boolean;
procedure RegisterAliases(AList: TAliasList); override;
public
property Type_: TPasTypeInfo read FType;
property WriteType: string read FWriteType;
end;
TPrintMethodMode = (pmmIntf, pmmDisp, pmmDelphi);
TIntfMethod = class(TTypeMember)
strict private
type
TCallingConv = (ccRegister, ccPascal, ccCdecl, ccStdcall, ccSafecall);
TPropInfo = (piNone, piGet, piSet);
TBrackets = (bRound, bSquare);
const
CCallinvConvNames: array[TCallingConv] of string = (
'register', 'pascal', 'cdecl', 'stdcall', 'safecall'
);
strict private
FRetType: TPasTypeInfo;
FWriteRetType: string;
FArgs: TObjectList<TMethodArg>;
FCallingConv: TCallingConv;
FUseSafeCall: Boolean;
FPropInfo: TPropInfo;
FDispID: TMemberID;
strict private
function GetArgCount: Integer;
function GetArg(AIdx: Integer): TMethodArg;
strict private
function DecodeCallingConv(ACallingConv: TCallConv): TCallingConv;
function IsRetHRes: Boolean;
public
constructor Create(const ATypeInfo: ITypeInfo; AIdx: Integer; AUseSafecall: Boolean);
destructor Destroy; override;
function Print(AOut: TCustomOut): Boolean; override;
procedure PrintForDisp(AOut: TCustomOut; AMode: TPrintMethodMode;
const AClassName: string; out AIsFunc: Boolean); overload;
procedure PrintForDisp(AOut: TCustomOut; AMode: TPrintMethodMode; const AClassName: string = ''); overload;
/// <returns>Return True if need AOut.DecIdent</returns>
function PrintArgs(AOut: TCustomOut; ABuilder: TStringBuilder; ACnt: Integer;
ABrackets: TBrackets; AIsCall: Boolean = False): Boolean;
procedure RequireUnits(AUnitManager: TUnitManager); override;
procedure RegisterAliases(AList: TAliasList); override;
function IsSafecall: Boolean;
function GetEventType: string;
class function IsVoid(const AType: TPasTypeInfo): Boolean; static;
public
property RetType: TPasTypeInfo read FRetType;
property WriteRetType: string read FWriteRetType;
property DispID: TMemberID read FDispID;
property PropInfo: TPropInfo read FPropInfo;
property ArgCount: Integer read GetArgCount;
property Args[AIdx: Integer]: TMethodArg read GetArg;
end;
TGUIDMember = class(TTLBMember)
strict private
FUUID: TGUID;
strict protected
property UUID: TGUID read FUUID;
strict protected
procedure ParseTypeAttr(const ATypeInfo: ITypeInfo; const ATypeAttr: TTypeAttr); override;
function GetIIDPrefix: string; virtual; abstract;
public
procedure PrintIID(AOut: TCustomOut);
procedure PrintForward(AOut: TCustomOut); virtual; abstract;
end;
TPropMember = record
Read: TIntfMethod;
Write: TIntfMethod;
Index: Integer;
end;
TPropList = class(TDictionary<TMemberID, TPropMember>)
public
type
TPropPair = TPair<TMemberID, TPropMember>;
TPropArray = TArray<TPropPair>;
strict private
function GetPropMethod(const AProp: TPropMember): TIntfMethod; inline;
function PrintHeaderProp(AOut: TCustomOut; ABuilder: TStringBuilder;
const AProp: TPropMember; out AIdxCnt: Integer): Boolean;
function CheckProcessProp(const AProp: TPropMember): Boolean;
public
function ToSortedArray: TPropArray;
procedure PrintMethods(AOut: TCustomOut);
procedure PrintProps(AOut: TCustomOut);
procedure PrintDispProps(AOut: TCustomOut);
end;
TCustomInterface = class(TGUIDMember)
strict protected
function GetIIDPrefix: string; override;
public
procedure PrintForward(AOut: TCustomOut); override;
procedure PrintMethods(AOut: TCustomOut; APropList: TPropList;
AMode: TPrintMethodMode; const AClassName: string = ''); virtual;
end;
TInterface = class(TCustomInterface)
strict private
FMethods: TObjectList<TIntfMethod>;
FParent: TCustomInterface;
FFlags: Word;
strict private
function GetForwardDecl: string;
strict protected
property Flags: Word read FFlags;
strict protected
class function GetIsDisp(AFlags: Word): Boolean; overload;
function GetIsDisp: Boolean; overload;
strict protected
procedure ParseTypeAttr(const ATypeInfo: ITypeInfo; const ATypeAttr: TTypeAttr); override;
procedure ParseTypeInfo(const ATypeInfo: ITypeInfo); override;
procedure InternalPrint(AOut: TCustomOut; const AHeader: string;
APrintDisp: Boolean);
function CreateParent(const ATypeInfo: ITypeInfo): TCustomInterface; virtual;
procedure PrintSelf(AOut: TCustomOut); virtual;
procedure PrintRecursive(AOut: TCustomOut); override;
public
constructor Create(const ATypeInfo: ITypeInfo); override;
destructor Destroy; override;
function Print(AOut: TCustomOut): Boolean; override;
procedure PrintForward(AOut: TCustomOut); override;
procedure PrintMethods(AOut: TCustomOut; APropList: TPropList;
AMode: TPrintMethodMode; const AClassName: string = ''); override;
procedure PrintImplMethods(AOut: TCustomOut; const AClassName: string);
procedure PrintAsEvents(AOut: TCustomOut);
procedure PrintAsInvokeEvents(AOut: TCustomOut);
procedure PrintAsProps(AOut: TCustomOut);
procedure RequireUnits(AUnitManager: TUnitManager); override;
procedure RegisterAliases(AList: TAliasList); override;
public
property UUID;
end;
TDispInterface = class(TInterface)
strict private
function GetDispName: string;
function GetForwardDecl: string;
strict protected
function GetIIDPrefix: string; override;
function CreateParent(const ATypeInfo: ITypeInfo): TCustomInterface; override;
procedure PrintSelf(AOut: TCustomOut); override;
public
constructor Create(const ATypeInfo: ITypeInfo); override;
procedure PrintForward(AOut: TCustomOut); override;
end;
TCoClass = class(TGUIDMember)
strict private
FInterfaces: TObjectList<TInterface>;
FEvents: TObjectList<TInterface>;
FDefaultIntf: TInterface;
strict private
// Service methods
procedure PrintGetDefaultInterface(AOut: TCustomOut);
procedure PrintInitServerData(AOut: TCustomOut);
procedure PrintInvokeEvent(AOut: TCustomOut);
procedure PrintConnect(AOut: TCustomOut);
procedure PrintConnectTo(AOut: TCustomOut);
procedure PrintDisconnect(AOut: TCustomOut);
strict private
function GetCoClassName: string;
function GetOleClassName: string;
procedure PrintCoClass(AOut: TCustomOut);
procedure PrintOleClass(AOut: TCustomOut);
procedure PrintCoClassImpl(AOut: TCustomOut);
procedure PrintOlePropImpl(AOut: TCustomOut);
procedure PrintOleSvcMethodsImpl(AOut: TCustomOut);
procedure PrintOleClassImpl(AOut: TCustomOut);
/// <returns>Return True if need AOut.DecIdent</returns>
function PrintIntfPropImpl(AOut: TCustomOut; AMethod: TIntfMethod;
ABuilder: TStringBuilder): Boolean;
strict protected
function GetIIDPrefix: string; override;
procedure ParseTypeAttr(const ATypeInfo: ITypeInfo; const ATypeAttr: TTypeAttr); override;
public
constructor Create(const ATypeInfo: ITypeInfo); override;
destructor Destroy; override;
function Print(AOut: TCustomOut): Boolean; override;
procedure PrintForward(AOut: TCustomOut); override;
procedure RegisterAliases(AList: TAliasList); override;
procedure PrintImpl(AOut: TCustomOut);
procedure RequireUnits(AUnitManager: TUnitManager); override;
procedure RequireImplUnits(AUnitManager: TUnitManager);
end;
TEnumItem = class(TTypeMember)
strict private
FValue: Integer;
FValueFmt: string;
public
constructor Create(const ATypeInfo: ITypeInfo; AIdx: Integer);
function Print(AOut: TCustomOut): Boolean; override;
public
property ValueFmt: string read FValueFmt write FValueFmt;
end;
TEnum = class(TTLBMember)
strict private
FItems: TObjectList<TEnumItem>;
FShowEnum: Boolean;
FShowHex: Boolean;
strict private
procedure PrintItem(AOut: TCustomOut; AItem: TEnumItem; const AFmt: string);
strict protected
procedure ParseTypeAttr(const ATypeInfo: ITypeInfo; const ATypeAttr: TTypeAttr); override;
public
constructor Create(const ATypeInfo: ITypeInfo); override;
destructor Destroy; override;
function Print(AOut: TCustomOut): Boolean; override;
procedure RequireUnits(AUnitManager: TUnitManager); override;
end;
TVariable = class(TTypeMember)
strict private
FType: TPasTypeInfo;
FWriteType: string;
public
constructor Create(const ATypeInfo: ITypeInfo; AIdx: Integer);
function Print(AOut: TCustomOut): Boolean; override;
procedure RequireUnits(AUnitManager: TUnitManager); override;
procedure RegisterAliases(AList: TAliasList); override;
public
property WriteType: string read FWriteType;
end;
TRecord = class(TTLBMember)
strict private
FFields: TObjectList<TVariable>;
FAlign: Word;
strict protected
procedure ParseTypeAttr(const ATypeInfo: ITypeInfo; const ATypeAttr: TTypeAttr); override;
procedure PrintRecursive(AOut: TCustomOut); override;
public
constructor Create(const ATypeInfo: ITypeInfo); override;
destructor Destroy; override;
function Print(AOut: TCustomOut): Boolean; override;
procedure RequireUnits(AUnitManager: TUnitManager); override;
procedure RegisterAliases(AList: TAliasList); override;
end;
TTLBMemberList<T: TTLBMember> = class(TObjectList<T>)
strict private
type
TPrintProc = reference to procedure(AOut: TCustomOut; AItem: T);
public
constructor Create;
procedure Print(AOut: TCustomOut; const AComment: string; AAddEmptyLine: Boolean);
procedure CustomPrint(AOut: TCustomOut; const AComment: string;
AAddEmptyLine: Boolean; const APrintProc: TPrintProc);
procedure RequireUnits(AUnitManager: TUnitManager);
procedure RegisterAliases(AList: TAliasList);
function Add(const AItem: T; ADict: TTLBMemberDict = nil): T;
end;
TTLBInfo = class(TIntfItem)
strict private
FMajorVersion: Integer;
FMinorVersion: Integer;
FUUID: TGUID;
FMemberList: TObjectList<TTLBMemberList<TTLBMember>>;
FEnums: TTLBMemberList<TEnum>;
FRecords: TTLBMemberList<TRecord>;
FInterfaces: TTLBMemberList<TInterface>;
FCoClasses: TTLBMemberList<TCoClass>;
FAliases: TTLBMemberList<TAlias>;
FRecordDict: TTLBMemberDict;
FIntfDict: TTLBMemberDict;
strict private
function GetPasUnitName: string;
strict private
function CreateMemberList<T: TTLBMember>: TTLBMemberList<T>;
procedure Parse(const ATypeLib: ITypeLib);
procedure PrintHeaderConst(AOut: TCustomOut);
procedure PrintClassIDs(AOut: TCustomOut);
procedure PrintIIDs(AOut: TCustomOut);
procedure PrintForward<T: TGUIDMember>(AOut: TCustomOut; AItem: T);
procedure PrintClassImpl(AOut: TCustomOut);
public
constructor Create(const AFileName: string);
destructor Destroy; override;
function Print(AOut: TCustomOut): Boolean; override;
procedure RequireUnits(AUnitManager: TUnitManager); override;
procedure RequireImplUnits(AUnitManager: TUnitManager);
procedure RegisterAliases(AList: TAliasList); override;
public
property PasUnitName: string read GetPasUnitName;
end;
implementation
uses
System.Win.ComObj,
System.Generics.Defaults;
const
CEmpty = '';
CSpace = ' ';
CSemicolon = ';';
CDefaultInterfaceFld = 'DefaultInterface';
type
TUnitSections = record
const
CUnit = 'unit';
CInterface = 'interface';
CImplementation = 'implementation';
CType = 'type';
CConst = 'const';
CVar = 'var';
CBegin = 'begin';
CEnd = 'end.';
CComment = '// ';
end;
TClassSections = record
const
CClass = 'class';
CPrivate = 'private';
CProtected = 'protected';
CPublic = 'public';
CPublished = 'published';
CProperty = 'property';
CRead = 'read';
CWrite = 'write';
CDefault = 'default';
CGetPrfx = 'Get_';
CSetPrfx = 'Set_';
CProcedure = 'procedure';
CFunction = 'function';
COut = 'out';
CEnd = 'end;';
end;
function __TypeDescToPasType(const ATypeDesc: TTypeDesc; out AHRef: HRefType;
var APasType: TPasTypeInfo): Boolean;
begin
Result := True;
APasType.VarType := ATypeDesc.vt;
case ATypeDesc.vt of
VT_EMPTY: APasType.Name := '!!!UNKNOWN Type VT_EMPTY!!!';
VT_NULL: APasType.Name := '!!!UNKNOWN Type VT_NULL!!!';
VT_I2: APasType.Name := 'SmallInt';
VT_I4: APasType.Name := 'Integer';
VT_R4: APasType.Name := 'Single';
VT_R8: APasType.Name := 'Double';
VT_CY: APasType.Name := 'Currency';
VT_DATE: APasType.Name := 'TDateTime';
VT_BSTR: APasType.Name := 'WideString';
VT_DISPATCH: APasType.Name := 'IDispatch';
VT_ERROR: begin
APasType.Name := 'SCODE';
APasType.StdUnit := suActiveX;
end;
VT_BOOL: APasType.Name := 'WordBool';
VT_VARIANT: APasType.Name := 'OleVariant';
VT_UNKNOWN: APasType.Name := 'IUnknown';
VT_DECIMAL: APasType.Name := '!!!UNKNOWN Type VT_DECIMAL!!!';
VT_I1: APasType.Name := 'ShortInt';
VT_UI1: APasType.Name := 'Byte';
VT_UI2: APasType.Name := 'Word';
VT_UI4: APasType.Name := 'Cardinal';
VT_I8: APasType.Name := 'Int64';
VT_UI8: APasType.Name := 'UInt64';
VT_INT: begin
APasType.Name := 'SYSINT';
APasType.StdUnit := suActiveX;
end;
VT_UINT: begin
APasType.Name := 'SYSUINT';
APasType.StdUnit := suActiveX;
end;
VT_VOID: APasType.Name := 'VOID';
VT_HRESULT: APasType.Name := 'HRESULT';
VT_PTR: begin
Inc(APasType.Ref);
Result := __TypeDescToPasType(ATypeDesc.ptdesc^, AHRef, APasType);
end;
VT_SAFEARRAY: APasType.Name := '!!!UNKNOWN Type VT_SAFEARRAY!!!';
VT_CARRAY: APasType.Name := '!!!UNKNOWN Type VT_CARRAY!!!';
VT_USERDEFINED: begin
AHRef := ATypeDesc.hreftype;
APasType.Name := '';
APasType.StdUnit := suCustom;
Result := False;
end;
VT_LPSTR: APasType.Name := 'PAnsiChar';
VT_LPWSTR: APasType.Name := 'PWideChar';
VT_RECORD: APasType.Name := '!!!UNKNOWN Type VT_RECORD!!!';
VT_INT_PTR: begin
APasType.Name := 'LPARAM';
APasType.StdUnit := suWindows;
end;
VT_UINT_PTR: begin
APasType.Name := 'WPARAM';
APasType.StdUnit := suWindows;
end;
VT_FILETIME: begin
APasType.Name := 'FILETIME';
APasType.StdUnit := suWindows;
end;
VT_BLOB: APasType.Name := '!!!UNKNOWN Type VT_BLOB!!!';
VT_STREAM: APasType.Name := '!!!UNKNOWN Type VT_STREAM!!!';
VT_STORAGE: APasType.Name := '!!!UNKNOWN Type VT_STORAGE!!!';
VT_STREAMED_OBJECT: APasType.Name := '!!!UNKNOWN Type VT_STREAMED_OBJECT!!!';
VT_STORED_OBJECT: APasType.Name := '!!!UNKNOWN Type VT_STORED_OBJECT!!!';
VT_BLOB_OBJECT: APasType.Name := '!!!UNKNOWN Type VT_BLOB_OBJECT!!!';
VT_CF: APasType.Name := '!!!UNKNOWN Type VT_CF!!!';
VT_CLSID: APasType.Name := '!!!UNKNOWN Type VT_CLSID!!!';
// VT_VERSIONED_STREAM: APasType.Name := '!!!UNKNOWN Type VT_VERSIONED_STREAM!!!';
else
APasType.Name := Format('!!!UNKNOWN Type desc. VT: %.4x!!!', [ATypeDesc.vt]);
end;
end;
function TypeDescToPasType(const ATypeDesc: TTypeDesc; out AHRef: HRefType;
out APasType: TPasTypeInfo): Boolean;
begin
APasType.Ref := 0;
APasType.StdUnit := suSystem;
APasType.CustomUnit := CEmpty;
Result := __TypeDescToPasType(ATypeDesc, AHRef, APasType);
APasType.RefBase := APasType.Ref;
end;
procedure GetHRefName(const ATypeInfo: ITypeInfo; AHRef: HRefType;
var AType: TPasTypeInfo); forward;
function ElemDescToTypeStr(const ATypeInfo: ITypeInfo;
const ADesc: TTypeDesc): TPasTypeInfo; overload;
var
LHref: HRefType;
begin
if not TypeDescToPasType(ADesc, LHref, Result) then
GetHRefName(ATypeInfo, LHref, Result);
if (Result.VarType = VT_VOID) and (Result.Ref > 0) then begin
Result.Name := 'Pointer';
Result.VarType := VT_PTR;
Dec(Result.Ref);
Dec(Result.RefBase);
end;
end;
function ElemDescToTypeStr(const ATypeInfo: ITypeInfo;
const ADesc: TElemDesc): TPasTypeInfo; overload;
begin
Result := ElemDescToTypeStr(ATypeInfo, ADesc.tdesc);
end;
procedure GetHRefName(const ATypeInfo: ITypeInfo; AHRef: HRefType;
var AType: TPasTypeInfo);
var
LType: ITypeInfo;
LAttr: PTypeAttr;
LStr: WideString;
LPasType: TPasTypeInfo;
begin
OleCheck(ATypeInfo.GetRefTypeInfo(AHRef, LType));
OleCheck(LType.GetDocumentation(MEMBERID_NIL, @LStr, nil, nil, nil));
AType.Name := TReservedWords.Escape(LStr);
OleCheck(LType.GetTypeAttr(LAttr));
try
case LAttr^.typekind of
TKIND_ENUM: AType.VarType := VT_I4;
TKIND_RECORD: begin
AType.VarType := VT_RECORD;
if (LAttr^.cVars = 4) and (AType.Name = 'GUID') then begin
AType.Name := 'TGUID';
AType.StdUnit := suSystem;
end;
end;
TKIND_INTERFACE: AType.VarType := VT_UNKNOWN;
TKIND_DISPATCH: AType.VarType := VT_DISPATCH;
TKIND_ALIAS: begin
LPasType := ElemDescToTypeStr(ATypeInfo, LAttr^.tdescAlias);
AType.VarType := LPasType.VarType;
Inc(AType.RefBase, LPasType.Ref);
end;
end;
finally
LType.ReleaseTypeAttr(LAttr);
end;
if AType.VarType in [VT_UNKNOWN, VT_DISPATCH] then begin
Dec(AType.Ref);
Dec(AType.RefBase);
end;
end;
{ TAliasList }
constructor TAliasList.Create;
begin
inherited Create;
FList := TDictionary<string, TItem>.Create;
end;
destructor TAliasList.Destroy;
begin
FList.Free;
inherited Destroy;
end;
procedure TAliasList.AddAlias(const ANewName, AOldName: string);
var
LItem: TItem;
begin
if not FList.TryGetValue(ANewName, LItem) then begin
LItem.Idx := FList.Count;
LItem.OldName := AOldName;
FList.Add(ANewName, LItem);
end;
end;
function TAliasList.AddAlias(const AName: string; ARefCnt: Integer;
AStdUnit: TStdUnits): string;
const
CTypePrfx = 'T';
CPtrPrfx = 'P';
CPtrSign = '^';
var
LPrevName: string;
begin
if AName = CEmpty then
raise Exception.Create('Type must be not empty');
if ARefCnt <= 0 then
Result := AName
else if ARefCnt = 1 then begin
Result := AName;
if Result[1] = CTypePrfx then
Result[1] := CPtrPrfx
else
Result := CPtrPrfx + Result;
if AStdUnit = suCustom then
AddAlias(Result, CPtrSign + AName);
end else begin
LPrevName := AddAlias(AName, ARefCnt - 1, AStdUnit);
Result := CPtrPrfx + LPrevName;
AddAlias(Result, CPtrSign + LPrevName);
end;
end;
function TAliasList.AddAlias(const AType: TPasTypeInfo): string;
begin
Result := AddAlias(AType.Name, AType.Ref, AType.StdUnit);
end;
procedure TAliasList.Print(AOut: TCustomOut);
type
TItemPair = TPair<string, TItem>;
var
LData: TArray<TItemPair>;
Li: Integer;
begin
if FList.Count = 0 then
Exit;
LData := FList.ToArray;
TArray.Sort<TItemPair>(LData, TComparer<TItemPair>.Construct(
function (const ALeft, ARight: TItemPair): Integer
begin
Result := CompareInt(ALeft.Value.Idx, ARight.Value.Idx);
end
));
AOut.Write(TUnitSections.CComment + 'Custom aliaces');
for Li := 0 to Length(LData) - 1 do
AOut.WriteFmt('%s = %s;', [LData[Li].Key, LData[Li].Value.OldName]);
AOut.EmptyLine;
end;
{ TIntfItem }
constructor TIntfItem.Create(const AName: string);
begin
inherited Create;
FName := TReservedWords.Escape(AName);
end;
function TIntfItem.Print(AOut: TCustomOut): Boolean;
begin
Result := True;
end;
procedure TIntfItem.RequireUnits(AUnitManager: TUnitManager);
begin
// Abstract
end;
procedure TIntfItem.RegisterAliases(AList: TAliasList);
begin
// Abstract
end;
{ TTLBMember }
constructor TTLBMember.CreateTLB(const ATypeLib: ITypeLib; AIdx: Integer);
var
LType: ITypeInfo;
begin
OleCheck(ATypeLib.GetTypeInfo(AIdx, LType));
Create(LType);
end;
constructor TTLBMember.Create(const ATypeInfo: ITypeInfo);
var
LStr: WideString;
begin
OleCheck(ATypeInfo.GetDocumentation(MEMBERID_NIL, @LStr, nil, nil, nil));
inherited Create(LStr);
ParseTypeInfo(ATypeInfo);
end;
procedure TTLBMember.ParseTypeInfo(const ATypeInfo: ITypeInfo);
var
LAttr: PTypeAttr;
begin
OleCheck(ATypeInfo.GetTypeAttr(LAttr));
try
ParseTypeAttr(ATypeInfo, LAttr^);
finally
ATypeInfo.ReleaseTypeAttr(LAttr);
end;
end;
procedure TTLBMember.ParseTypeAttr(const ATypeInfo: ITypeInfo;
const ATypeAttr: TTypeAttr);
begin
// Abstract
end;
procedure TTLBMember.PrintRecursive(AOut: TCustomOut);
begin
// Abstract
end;
function TTLBMember.Print(AOut: TCustomOut): Boolean;
begin
Result := inherited Print(AOut);
if not Result then
Exit;
if FMembers <> nil then begin
if FPrinted then
Exit(False);
FPrinted := True;
PrintRecursive(AOut);
end;
end;
{ TAlias }
procedure TAlias.ParseTypeAttr(const ATypeInfo: ITypeInfo; const ATypeAttr: TTypeAttr);
begin
inherited ParseTypeAttr(ATypeInfo, ATypeAttr);
FAlias := ElemDescToTypeStr(ATypeInfo, ATypeAttr.tdescAlias);
end;
function TAlias.Print(AOut: TCustomOut): Boolean;
begin
Result := inherited Print(AOut);
if not Result then
Exit;
AOut.WriteFmt('%s = type %s;', [Name, FWriteType]);
end;
procedure TAlias.RequireUnits(AUnitManager: TUnitManager);
begin
inherited RequireUnits(AUnitManager);
AUnitManager.AddPasType(FAlias);
end;
procedure TAlias.RegisterAliases(AList: TAliasList);
begin
inherited RegisterAliases(AList);
FWriteType := AList.AddAlias(FAlias);
end;
{ TTypeMember }
constructor TTypeMember.Create(const ATypeInfo: ITypeInfo; AMemberID: TMemberID);
var
LStr: WideString;
begin
ATypeInfo.GetDocumentation(AMemberID, @LStr, nil, nil, nil);
inherited Create(LStr);
end;
{ TMethodArg }
constructor TMethodArg.Create(const ATypeInfo: ITypeInfo; const AName: string;
const ADesc: TElemDesc);
begin
inherited Create(AName);
FType := ElemDescToTypeStr(ATypeInfo, ADesc);
if ADesc.paramdesc.wParamFlags and PARAMFLAG_FIN = PARAMFLAG_FIN then
FFlag := FFlag or CFlagIn;
if ADesc.paramdesc.wParamFlags and PARAMFLAG_FOUT = PARAMFLAG_FOUT then
FFlag := FFlag or CFlagOut;
if ADesc.paramdesc.wParamFlags and PARAMFLAG_FRETVAL = PARAMFLAG_FRETVAL then
FFlag := FFlag or CFlagRetVal;
end;
function TMethodArg.Print(AOut: TCustomOut): Boolean;
begin
Result := inherited Print(AOut);
if not Result then
Exit;
AOut.Write(ToString);
end;
procedure TMethodArg.RequireUnits(AUnitManager: TUnitManager);
begin
inherited RequireUnits(AUnitManager);
AUnitManager.AddPasType(FType);
end;
function TMethodArg.ToString: string;
var
LPrfx: string;
begin
if (FType.Ref > 0) and (FFlag and CFlagOut = CFlagOut) then begin
if FFlag and CFlagIn = CFlagIn then
LPrfx := TUnitSections.CVar
else
LPrfx := TClassSections.COut;
end else begin
if FType.Ref = 0 then begin
if FType.VarType in [VT_BSTR, VT_UNKNOWN, VT_DISPATCH, VT_RECORD, VT_VARIANT] then
LPrfx := TUnitSections.CConst
else
LPrfx := CEmpty;
end else
LPrfx := TUnitSections.CVar;
end;
if LPrfx <> CEmpty then
LPrfx := LPrfx + CSpace;
Result := Format('%s%s: %s', [LPrfx, Name, FWriteType]);
end;
function TMethodArg.IsRetVal: Boolean;
const
CTestFlag = CFlagOut or CFlagRetVal;
begin
Result := FFlag and CTestFlag = CTestFlag;
end;
procedure TMethodArg.RegisterAliases(AList: TAliasList);
begin
inherited RegisterAliases(AList);
FWriteType := AList.AddAlias(FType.Name, FType.Ref - 1, FType.StdUnit);
end;
{ TIntfMethod }
constructor TIntfMethod.Create(const ATypeInfo: ITypeInfo; AIdx: Integer;
AUseSafecall: Boolean);
var
LDesc: PFuncDesc;
Li: Integer;
LParamNames: array of WideString;
LCnt: Integer;
LPrmName: string;
begin
FUseSafeCall := AUseSafecall;
FArgs := TObjectList<TMethodArg>.Create(True);
OleCheck(ATypeInfo.GetFuncDesc(AIdx, LDesc));
try
inherited Create(ATypeInfo, LDesc^.memid);
FDispID := LDesc^.memid;
FRetType := ElemDescToTypeStr(ATypeInfo, LDesc^.elemdescFunc);
FCallingConv := DecodeCallingConv(LDesc^.callconv);
case LDesc^.invkind of
INVOKE_PROPERTYGET: FPropInfo := piGet;
INVOKE_PROPERTYPUT, INVOKE_PROPERTYPUTREF: FPropInfo := piSet;
else
FPropInfo := piNone;
end;
if LDesc^.cParams > 0 then begin
SetLength(LParamNames, LDesc^.cParams + 1);
OleCheck(ATypeInfo.GetNames(LDesc^.memid, @LParamNames[0], LDesc^.cParams + 1, LCnt));
for Li := 0 to LDesc^.cParams - 1 do begin
LPrmName := LParamNames[Li + 1];
if LPrmName = CEmpty then
LPrmName := 'Param' + IntToStr(Li + 1);
FArgs.Add(TMethodArg.Create(ATypeInfo, LPrmName, LDesc^.lprgelemdescParam^[Li]));
end;
end;
finally
ATypeInfo.ReleaseFuncDesc(LDesc);
end;
end;
destructor TIntfMethod.Destroy;
begin
FArgs.Free;
inherited Destroy;
end;
function TIntfMethod.GetArgCount: Integer;
begin
Result := FArgs.Count;
end;
function TIntfMethod.GetArg(AIdx: Integer): TMethodArg;
begin
Result := FArgs[AIdx];
end;
function TIntfMethod.DecodeCallingConv(ACallingConv: TCallConv): TCallingConv;
begin
case ACallingConv of
CC_SAFECALL: Result := ccSafecall;
CC_CDECL, CC_MPWCDECL: Result := ccCdecl;
CC_PASCAL, CC_MACPASCAL, CC_MPWPASCAL: Result := ccPascal;
CC_STDCALL: Result := ccStdcall;
CC_FPFASTCALL: Result := ccRegister;
CC_SYSCALL: Result := ccCdecl;
else
Result := ccCdecl;
end;
end;
function TIntfMethod.IsRetHRes: Boolean;
begin
Result := (FRetType.RefBase = 0) and (FRetType.VarType = VT_HRESULT);
end;
function TIntfMethod.Print(AOut: TCustomOut): Boolean;
begin
Result := inherited Print(AOut);
if not Result then
Exit;
PrintForDisp(AOut, pmmIntf);
end;
procedure TIntfMethod.PrintForDisp(AOut: TCustomOut; AMode: TPrintMethodMode;
const AClassName: string; out AIsFunc: Boolean);
var
LIsIdent: Boolean;
LUseSafeCall: Boolean;
LCallConv: TCallingConv;
LRetType: TPasTypeInfo;
LRetTypeName: string;
LLastArg: TMethodArg;
LBuilder: TStringBuilder;
LArgCnt: Integer;
begin
if AMode <> pmmIntf then
LUseSafeCall := IsRetHRes
else
LUseSafeCall := IsSafecall;
LLastArg := nil;
LRetType := FRetType;
LRetTypeName := FWriteRetType;
LArgCnt := FArgs.Count;
if LUseSafeCall then begin
if LArgCnt > 0 then begin
LLastArg := FArgs.Last;
if LLastArg.IsRetVal then begin
LRetType := LLastArg.Type_;
LRetTypeName := LLastArg.WriteType;
Dec(LArgCnt);
end else
LLastArg := nil;
end;
LCallConv := ccSafecall;
end else
LCallConv := FCallingConv;
AIsFunc := (LLastArg <> nil) or (not LUseSafeCall and not IsVoid(FRetType));
// if AUseDisp and (LLastArg = nil) then
// LUseSafeCall := False;
LBuilder := TStringBuilder.Create;
try
if AIsFunc then
LBuilder.Append(TClassSections.CFunction)
else begin
LBuilder.Append(TClassSections.CProcedure);
LRetType.Ref := 0;
LRetType.VarType := VT_VOID;
end;
LBuilder.Append(CSpace);
if AClassName <> CEmpty then begin
LBuilder.Append(AClassName);
LBuilder.Append('.');
end;
case FPropInfo of
piGet: LBuilder.Append(TClassSections.CGetPrfx);
piSet: LBuilder.Append(TClassSections.CSetPrfx);
end;
LBuilder.Append(Name);
LIsIdent := PrintArgs(AOut, LBuilder, LArgCnt, bRound);
if AIsFunc then begin
LBuilder.Append(': ');
LBuilder.Append(LRetTypeName);
end;