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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 1999-2000 by the Free Pascal development team
This unit makes Free Pascal as much as possible Delphi compatible
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
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.
**********************************************************************}
{$ifdef FPC_HAS_FEATURE_VARIANTS}
procedure DoDispCallByIDError(res : Pointer; const disp : IDispatch;desc : PDispDesc; params : Pointer);
begin
handleerroraddrframeind(RuntimeErrorExitCodes[reVarDispatch],
get_pc_addr,get_frame);
end;
procedure fpc_dispatch_by_id(Result: Pointer; const Dispatch: pointer;
DispDesc: Pointer; Params: Pointer); compilerproc;
type
TDispProc = procedure(res : Pointer; const disp : IDispatch;desc : PDispDesc; params : Pointer);
begin
TDispProc(DispCallByIDProc)(Result,IDispatch(Dispatch),DispDesc,Params);
end;
{$endif FPC_HAS_FEATURE_VARIANTS}
{****************************************************************************
Internal Routines called from the Compiler
****************************************************************************}
{ the reverse order of the parameters make code generation easier }
function fpc_do_is(aclass : tclass;aobject : tobject) : boolean;[public,alias: 'FPC_DO_IS']; compilerproc;
begin
fpc_do_is:=assigned(aobject) and assigned(aclass) and
aobject.inheritsfrom(aclass);
end;
{ the reverse order of the parameters make code generation easier }
function fpc_do_as(aclass : tclass;aobject : tobject): tobject;[public,alias: 'FPC_DO_AS']; compilerproc;
begin
if assigned(aobject) and not(aobject.inheritsfrom(aclass)) then
handleerroraddrframeInd(219,get_pc_addr,get_frame);
result := aobject;
end;
{ interface helpers }
procedure fpc_intf_decr_ref(var i: pointer);[public,alias: 'FPC_INTF_DECR_REF']; compilerproc;
begin
if assigned(i) then
begin
IUnknown(i)._Release;
i:=nil;
end;
end;
{ local declaration for intf_decr_ref for local access }
procedure intf_decr_ref(var i: pointer); [external name 'FPC_INTF_DECR_REF'];
procedure fpc_intf_incr_ref(i: pointer);[public,alias: 'FPC_INTF_INCR_REF']; compilerproc;
begin
if assigned(i) then
IUnknown(i)._AddRef;
end;
{ local declaration of intf_incr_ref for local access }
procedure intf_incr_ref(i: pointer); [external name 'FPC_INTF_INCR_REF'];
procedure fpc_intf_assign(var D: pointer; const S: pointer);[public,alias: 'FPC_INTF_ASSIGN']; compilerproc;
begin
if assigned(S) then
IUnknown(S)._AddRef;
if assigned(D) then
IUnknown(D)._Release;
D:=S;
end;
procedure fpc_intf_assign(var D: pointer; const s: pointer); [external name 'FPC_INTF_ASSIGN'];
{procedure fpc_intf_assign_by_iid(var D: pointer; const S: pointer; const iid: TGUID);[public,alias: 'FPC_INTF_ASSIGN2']; compilerproc;
var
tmp : pointer;
begin
if assigned(S) then
begin
tmp:=nil;
if IUnknown(S).QueryInterface(iid,tmp)<>S_OK then
handleerror(219);
if assigned(D) then
IUnknown(D)._Release;
D:=tmp;
end
else
begin
if assigned(D) then
IUnknown(D)._Release;
D:=nil;
end;
end;}
function fpc_intf_is(const S: pointer; const iid: TGUID): Boolean;[public,alias: 'FPC_INTF_IS']; compilerproc;
var
tmpi: pointer;
begin
tmpi:=nil;
fpc_intf_is:=Assigned(S) and (IUnknown(S).QueryInterface(iid,tmpi)=S_OK);
if Assigned(tmpi) then
IUnknown(tmpi)._Release;
end;
function fpc_intf_is_class(const S: pointer; const aclass: tclass): Boolean;[public,alias: 'FPC_INTF_IS_CLASS']; compilerproc;
var
tmpo: tobject;
begin
fpc_intf_is_class:=Assigned(S) and (IUnknown(S).QueryInterface(IObjectInstance,tmpo)=S_OK) and tmpo.InheritsFrom(aclass);
end;
function fpc_class_is_intf(const S: pointer; const iid: TGUID): Boolean;[public,alias: 'FPC_CLASS_IS_INTF']; compilerproc;
var
tmpi: pointer;
tmpi2: pointer; // weak!
begin
tmpi:=nil;
tmpi2:=nil;
fpc_class_is_intf:=Assigned(S) and ((TObject(S).GetInterfaceWeak(IUnknown,tmpi2) and (IUnknown(tmpi2).QueryInterface(IID,tmpi)=S_OK)) or
TObject(S).GetInterface(IID,tmpi));
if Assigned(tmpi) then
IUnknown(tmpi)._Release;
end;
function fpc_class_is_corbaintf(const S: pointer; const iid: Shortstring): Boolean;[public,alias: 'FPC_CLASS_IS_CORBAINTF']; compilerproc;
begin
fpc_class_is_corbaintf:=Assigned(S) and Assigned(TObject(S).GetInterfaceEntryByStr(iid));
end;
function fpc_intf_cast(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_INTF_CAST']; compilerproc;
var
tmpi: pointer;
begin
tmpi:=nil;
if Assigned(S) and (IUnknown(S).QueryInterface(iid,tmpi)=S_OK) then
pointer(fpc_intf_cast):=tmpi
else
fpc_intf_cast:= nil;
end;
function fpc_intf_cast_class(const S: pointer; const aclass: tclass): pointer;[public,alias: 'FPC_INTF_CAST_CLASS']; compilerproc;
var
tmpo: tobject;
begin
if Assigned(S) and (IUnknown(S).QueryInterface(IObjectInstance,tmpo)=S_OK) and tmpo.InheritsFrom(aclass) then
fpc_intf_cast_class:=tmpo
else
fpc_intf_cast_class:=nil;
end;
function fpc_class_cast_intf(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_CLASS_CAST_INTF']; compilerproc;
var
tmpi: pointer;
tmpi2: pointer; // weak!
begin
tmpi:=nil;
tmpi2:=nil;
if Assigned(S) and ((TObject(S).GetInterfaceWeak(IUnknown,tmpi2) and (IUnknown(tmpi2).QueryInterface(IID,tmpi)=S_OK)) or
TObject(S).GetInterface(IID,tmpi)) then
begin
// decrease reference count
fpc_class_cast_intf:=nil;
pointer(fpc_class_cast_intf):=tmpi
end
else
fpc_class_cast_intf:=nil;
end;
function fpc_class_cast_corbaintf(const S: pointer; const iid: Shortstring): Pointer;[public,alias: 'FPC_CLASS_CAST_CORBAINTF']; compilerproc;
var
tmpi: pointer;
begin
if Assigned(S) and TObject(S).GetInterface(iid,tmpi) then
fpc_class_cast_corbaintf:=tmpi
else
fpc_class_cast_corbaintf:=nil;
end;
function fpc_intf_as(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_INTF_AS']; compilerproc;
var
tmpi: pointer; // _AddRef before _Release
begin
if assigned(S) then
begin
tmpi:=nil;
if IUnknown(S).QueryInterface(iid,tmpi)<>S_OK then
handleerror(219);
// decrease reference count
fpc_intf_as:=nil;
pointer(fpc_intf_as):=tmpi;
end
else
fpc_intf_as:=nil;
end;
function fpc_intf_as_class(const S: pointer; const aclass: tclass): pointer;[public,alias: 'FPC_INTF_AS_CLASS']; compilerproc;
var
tmpo: tobject;
begin
if assigned(S) then
begin
if not ((IUnknown(S).QueryInterface(IObjectInstance,tmpo)=S_OK) and tmpo.inheritsfrom(aclass)) then
handleerror(219);
fpc_intf_as_class:=tmpo;
end
else
fpc_intf_as_class:=nil;
end;
function fpc_class_as_intf(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_CLASS_AS_INTF']; compilerproc;
var
tmpi: pointer; // _AddRef before _Release
tmpi2: pointer; // weak!
begin
if assigned(S) then
begin
tmpi:=nil;
tmpi2:=nil;
if not ((TObject(S).GetInterfaceWeak(IUnknown,tmpi2) and (IUnknown(tmpi2).QueryInterface(IID,tmpi)=S_OK)) or TObject(S).GetInterface(IID,tmpi)) then
handleerror(219);
// decrease reference count
fpc_class_as_intf:=nil;
pointer(fpc_class_as_intf):=tmpi;
end
else
fpc_class_as_intf:=nil;
end;
function fpc_class_as_corbaintf(const S: pointer; const iid: Shortstring): Pointer;[public,alias: 'FPC_CLASS_AS_CORBAINTF']; compilerproc;
var
tmpi: pointer; // _AddRef before _Release
begin
if assigned(S) then
begin
tmpi:=nil;
if not TObject(S).GetInterface(iid,tmpi) then
handleerror(219);
fpc_class_as_corbaintf:=tmpi;
end
else
fpc_class_as_corbaintf:=nil;
end;
{****************************************************************************
TVMT
****************************************************************************}
function TVmt.GetvParent: PVmt;
begin
{$ifdef VER3_0}
GetvParent:=vParentRef;
{$else VER3_0}
if Assigned(vParentRef) then
GetvParent:=vParentRef^
else
GetvParent:=Nil;
{$endif VER3_0}
end;
{****************************************************************************
TINTERFACEENTRY
****************************************************************************}
function tinterfaceentry.GetIID: pguid;
begin
{$ifdef VER3_0}
GetIID:=IIDRef;
{$else VER3_0}
if Assigned(IIDRef) then
GetIID:=IIDRef^
else
GetIID:=Nil;
{$endif VER3_0}
end;
function tinterfaceentry.GetIIDStr: pshortstring;
begin
{$ifdef VER3_0}
GetIIDStr:=IIDStrRef;
{$else VER3_0}
if Assigned(IIDStrRef) then
GetIIDStr:=IIDStrRef^
else
GetIIDStr:=Nil;
{$endif VER3_0}
end;
{****************************************************************************
TOBJECT
****************************************************************************}
constructor TObject.Create;
begin
end;
destructor TObject.Destroy;
begin
end;
procedure TObject.Free;
begin
// the call via self avoids a warning
if self<>nil then
self.destroy;
end;
class function TObject.InstanceSize : SizeInt;
begin
InstanceSize := PVmt(Self)^.vInstanceSize;
end;
{$ifdef VER3_0}
var
emptyintf: ptruint; public name 'FPC_EMPTYINTF';
{$endif VER3_0}
procedure InitInterfacePointers(objclass: tclass;instance : pointer);
var
ovmt: PVmt;
i: longint;
intftable: pinterfacetable;
Res: pinterfaceentry;
begin
ovmt := PVmt(objclass);
while assigned(ovmt) and {$ifdef VER3_0}(ovmt^.vIntfTable <> @emptyintf){$else}assigned(ovmt^.vIntfTable){$endif} do
begin
intftable:=ovmt^.vIntfTable;
{$ifdef VER3_0}
if assigned(intftable) then
{$endif VER3_0}
begin
i:=intftable^.EntryCount;
Res:=@intftable^.Entries[0];
while i>0 do begin
if Res^.IType = etStandard then
ppointer(@(pbyte(instance)[Res^.IOffset]))^:=
pointer(Res^.VTable);
inc(Res);
dec(i);
end;
end;
ovmt:=ovmt^.vParent;
end;
end;
class function TObject.InitInstance(instance : pointer) : tobject; {$ifdef SYSTEMINLINE} inline; {$ENDIF}
{$ifndef VER3_0}
var
vmt : PVmt;
inittable : pointer;
{$ifdef FPC_HAS_FEATURE_RTTI}
mopinittable : PRTTIRecordOpOffsetTable;
{$endif def FPC_HAS_FEATURE_RTTI}
i : longint;
{$endif VER3_0}
begin
{ the size is saved at offset 0 }
fillchar(instance^, InstanceSize, 0);
{ insert VMT pointer into the new created memory area }
{ (in class methods self contains the VMT!) }
ppointer(instance)^:=pointer(self);
if {$ifdef VER3_0}PVmt(self)^.vIntfTable <> @emptyintf{$else}assigned(PVmt(self)^.vIntfTable){$endif} then
InitInterfacePointers(self,instance);
{$ifndef VER3_0}
{$ifdef FPC_HAS_FEATURE_RTTI}
{ for management operators like initialize call int_initialize }
vmt := PVmt(self);
if assigned(vmt) then
begin
inittable:=vmt^.vInitTable;
if assigned(inittable) then
begin
mopinittable:=RTTIRecordMopInitTable(inittable);
if assigned(mopinittable) then
begin
{$push}
{ ensure that no range check errors pop up with the [0..0] array }
{$R-}
for i:=0 to mopinittable^.Count-1 do
TRTTIRecVarOp(mopinittable^.Entries[i].ManagmentOperator)(PByte(Instance)+mopinittable^.Entries[i].FieldOffset);
{$pop}
end;
end;
end;
{$endif def FPC_HAS_FEATURE_RTTI}
{$endif VER3_0}
InitInstance:=TObject(Instance);
end;
class function TObject.ClassParent : tclass;
begin
{ type of self is class of tobject => it points to the vmt }
{ the parent vmt is saved at offset vmtParent }
classparent:=tclass(PVmt(Self)^.vParent);
end;
class function TObject.NewInstance : tobject;
var
p : pointer;
begin
getmem(p, InstanceSize);
if p <> nil then
InitInstance(p);
NewInstance:=TObject(p);
end;
procedure TObject.FreeInstance;
begin
CleanupInstance;
FreeMem(Pointer(Self));
end;
class function TObject.ClassType : TClass;
begin
ClassType:=TClass(Pointer(Self))
end;
type
tmethodnamerec = packed record
name : pshortstring;
addr : codepointer;
end;
tmethodnametable = packed record
count : dword;
entries : packed array[0..0] of tmethodnamerec;
end;
pmethodnametable = ^tmethodnametable;
class function TObject.MethodAddress(const name : shortstring) : codepointer;
var
methodtable : pmethodnametable;
i : dword;
ovmt : PVmt;
begin
ovmt:=PVmt(self);
while assigned(ovmt) do
begin
methodtable:=pmethodnametable(ovmt^.vMethodTable);
if assigned(methodtable) then
begin
for i:=0 to methodtable^.count-1 do
if ShortCompareText(methodtable^.entries[i].name^, name)=0 then
begin
MethodAddress:=methodtable^.entries[i].addr;
exit;
end;
end;
ovmt := ovmt^.vParent;
end;
MethodAddress:=nil;
end;
class function TObject.MethodName(address : codepointer) : shortstring;
var
methodtable : pmethodnametable;
i : dword;
ovmt : PVmt;
begin
ovmt:=PVmt(self);
while assigned(ovmt) do
begin
methodtable:=pmethodnametable(ovmt^.vMethodTable);
if assigned(methodtable) then
begin
for i:=0 to methodtable^.count-1 do
if methodtable^.entries[i].addr=address then
begin
MethodName:=methodtable^.entries[i].name^;
exit;
end;
end;
ovmt := ovmt^.vParent;
end;
MethodName:='';
end;
function TObject.FieldAddress(const name : shortstring) : pointer;
type
PFieldInfo = ^TFieldInfo;
TFieldInfo =
{$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
packed
{$endif FPC_REQUIRES_PROPER_ALIGNMENT}
record
FieldOffset: SizeUInt;
ClassTypeIndex: Word;
Name: ShortString;
end;
PFieldTable = ^TFieldTable;
TFieldTable =
{$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
packed
{$endif FPC_REQUIRES_PROPER_ALIGNMENT}
record
FieldCount: Word;
ClassTable: Pointer;
{ should be array[Word] of TFieldInfo; but
Elements have variant size! force at least proper alignment }
Fields: array[0..0] of TFieldInfo
end;
var
ovmt: PVmt;
FieldTable: PFieldTable;
FieldInfo: PFieldInfo;
i: longint;
begin
if Length(name) > 0 then
begin
ovmt := PVmt(ClassType);
while ovmt <> nil do
begin
FieldTable := PFieldTable(ovmt^.vFieldTable);
if FieldTable <> nil then
begin
FieldInfo := @FieldTable^.Fields[0];
for i := 0 to FieldTable^.FieldCount - 1 do
begin
if ShortCompareText(FieldInfo^.Name, name) = 0 then
begin
fieldaddress := Pointer(Self) + FieldInfo^.FieldOffset;
exit;
end;
FieldInfo := PFieldInfo(PByte(@FieldInfo^.Name) + 1 + Length(FieldInfo^.Name));
{$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
{ align to largest field of TFieldInfo }
FieldInfo := Align(FieldInfo, SizeOf(PtrUInt));
{$endif FPC_REQUIRES_PROPER_ALIGNMENT}
end;
end;
{ Try again with the parent class type }
ovmt:=ovmt^.vParent;
end;
end;
fieldaddress:=nil;
end;
function TObject.SafeCallException(exceptobject : tobject;
exceptaddr : codepointer) : HResult;
begin
safecallexception:=E_UNEXPECTED;
end;
class function TObject.ClassInfo : pointer;
begin
ClassInfo := PVmt(Self)^.vTypeInfo;
end;
class function TObject.ClassName : ShortString;
begin
ClassName := PVmt(Self)^.vClassName^;
end;
class function TObject.ClassNameIs(const name : string) : boolean;
begin
// call to ClassName inlined here, this eliminates stack and string copying.
ClassNameIs:=ShortCompareText(PVmt(Self)^.vClassName^, name) = 0;
end;
class function TObject.InheritsFrom(aclass : TClass) : Boolean;
var
vmt: PVmt;
begin
if assigned(aclass) then
begin
vmt:=PVmt(self);
while assigned(vmt) and (vmt <> PVmt(aclass)) do
vmt := vmt^.vParent;
InheritsFrom := (vmt = PVmt(aclass));
end
else
inheritsFrom := False;
end;
class function TObject.stringmessagetable : pstringmessagetable;
begin
stringmessagetable:=PVmt(Self)^.vMsgStrPtr;
end;
type
tmessagehandler = procedure(var msg) of object;
procedure TObject.Dispatch(var message);
type
{$PUSH}
{$PACKRECORDS NORMAL}
PMsgIntTable = ^TMsgIntTable;
TMsgIntTable = record
index : dword;
method : codepointer;
end;
PMsgInt = ^TMsgInt;
TMsgInt = record
count : longint;
msgs : array[0..0] of TMsgIntTable;
end;
{$POP}
var
index : dword;
count,i : longint;
msgtable : PMsgIntTable;
p : PMsgInt;
ovmt : PVmt;
msghandler : tmessagehandler;
begin
index:=dword(message);
ovmt := PVmt(ClassType);
while assigned(ovmt) do
begin
// See if we have messages at all in this class.
p:=PMsgInt(ovmt^.vDynamicTable);
If Assigned(p) then
begin
msgtable:=@p^.msgs;
count:=p^.count;
end
else
Count:=0;
{ later, we can implement a binary search here }
for i:=0 to count-1 do
begin
if index=msgtable[i].index then
begin
TMethod(msghandler).Code:=msgtable[i].method;
TMethod(msghandler).Data:=self;
msghandler(message);
exit;
end;
end;
ovmt:=ovmt^.vParent;
end;
DefaultHandler(message);
end;
procedure TObject.DispatchStr(var message);
var
name : shortstring;
count,i : longint;
msgstrtable : pmsgstrtable;
p: pstringmessagetable;
ovmt : PVmt;
msghandler : tmessagehandler;
begin
name:=pshortstring(@message)^;
ovmt:=PVmt(ClassType);
while assigned(ovmt) do
begin
p := ovmt^.vMsgStrPtr;
if (P<>Nil) and (p^.count<>0) then
begin
count:=p^.count;
msgstrtable:=@p^.msgstrtable;
end
else
Count:=0;
{ later, we can implement a binary search here }
for i:=0 to count-1 do
begin
if name=msgstrtable[i].name^ then
begin
TMethod(msghandler).Code:=msgstrtable[i].method;
TMethod(msghandler).Data:=self;
msghandler(message);
exit;
end;
end;
ovmt:=ovmt^.vParent;
end;
DefaultHandlerStr(message);
end;
procedure TObject.DefaultHandler(var message);
begin
end;
procedure TObject.DefaultHandlerStr(var message);
begin
end;
procedure TObject.CleanupInstance;
var
vmt : PVmt;
temp : pointer;
begin
vmt := PVmt(ClassType);
while vmt<>nil do
begin
Temp:= vmt^.vInitTable;
{$ifdef FPC_HAS_FEATURE_RTTI}
{ The RTTI format matches one for records, except the type is tkClass.
Since RecordRTTI does not check the type, calling it yields the desired result. }
if Assigned(Temp) then
RecordRTTI(Self,Temp,@int_finalize);
{$endif def FPC_HAS_FEATURE_RTTI}
vmt:= vmt^.vParent;
end;
end;
procedure TObject.AfterConstruction;
begin
end;
procedure TObject.BeforeDestruction;
begin
end;
function IsGUIDEqual(const guid1, guid2: tguid): boolean;
begin
IsGUIDEqual:=
(guid1.D1=guid2.D1) and
(PDWORD(@guid1.D2)^=PDWORD(@guid2.D2)^) and
(PDWORD(@guid1.D4[0])^=PDWORD(@guid2.D4[0])^) and
(PDWORD(@guid1.D4[4])^=PDWORD(@guid2.D4[4])^);
end;
// Use of managed types should be avoided here; implicit _Addref/_Release
// will end up in unpredictable behaviour if called on CORBA interfaces.
type
TInterfaceGetter = procedure(out Obj) of object;
TClassGetter = function: TObject of object;
function GetInterfaceByEntry(Instance: pointer; IEntry: pinterfaceentry; out obj): boolean;
var
Getter: TMethod;
begin
Pointer(Obj) := nil;
Getter.Data := Instance;
if Assigned(IEntry) and Assigned(Instance) then
begin
case IEntry^.IType of
etStandard:
Pointer(Obj) := PByte(instance)+IEntry^.IOffset;
etFieldValue, etFieldValueClass:
Pointer(obj) := PPointer(PByte(Instance)+IEntry^.IOffset)^;
etVirtualMethodResult:
begin
// IOffset is relative to the VMT, not to instance.
Getter.code := PCodePointer(PByte(PPointer(Instance)^) + IEntry^.IOffset)^;
TInterfaceGetter(Getter)(obj);
end;
etVirtualMethodClass:
begin
// IOffset is relative to the VMT, not to instance.
Getter.code := PCodePointer(PByte(PPointer(Instance)^) + IEntry^.IOffset)^;
TObject(obj) := TClassGetter(Getter)();
end;
etStaticMethodResult:
begin
Getter.code := IEntry^.IOffsetAsCodePtr;
TInterfaceGetter(Getter)(obj);
end;
etStaticMethodClass:
begin
Getter.code := IEntry^.IOffsetAsCodePtr;
TObject(obj) := TClassGetter(Getter)();
end;
end;
end;
result := assigned(pointer(obj));
end;
function TObject.GetInterface(const iid : tguid;out obj) : boolean;
var
IEntry: PInterfaceEntry;
Instance: TObject;
begin
if IsGUIDEqual(IObjectInstance,iid) then
begin
TObject(Obj) := Self;
Result := True;
Exit;
end;
Instance := self;
repeat
IEntry := Instance.GetInterfaceEntry(iid);
result := GetInterfaceByEntry(Instance, IEntry, obj);
if (not result) or
(IEntry^.IType in [etStandard, etFieldValue,
etStaticMethodResult, etVirtualMethodResult]) then
Break;
{ if interface is implemented by a class-type property or field,
continue search }
Instance := TObject(obj);
until False;
{ Getter function will normally AddRef, so adding another reference here
will cause memleak. }
if result and (IEntry^.IType in [etStandard, etFieldValue]) then
IInterface(obj)._AddRef;
end;
function TObject.GetInterfaceWeak(const iid : tguid; out obj) : boolean;
var
IEntry: PInterfaceEntry;
Instance: TObject;
begin
if IsGUIDEqual(IObjectInstance,iid) then
begin
TObject(Obj) := Self;
Result := True;
Exit;
end;
Instance := self;
repeat
IEntry := Instance.GetInterfaceEntry(iid);
result := GetInterfaceByEntry(Instance, IEntry, obj);
if (not result) or
(IEntry^.IType in [etStandard, etFieldValue,
etStaticMethodResult, etVirtualMethodResult]) then
Break;
{ if interface is implemented by a class-type property or field,
continue search }
Instance := TObject(obj);
until False;
{ Getter function will normally AddRef, so we have to release it,
else the ref is not weak. }
if result and not (IEntry^.IType in [etStandard, etFieldValue]) then
IInterface(obj)._Release;
end;
function TObject.GetInterfaceByStr(const iidstr : shortstring;out obj) : boolean;
var
IEntry: PInterfaceEntry;
Instance: TObject;
begin
Instance := self;
repeat
IEntry := Instance.GetInterfaceEntryByStr(iidstr);
result := GetInterfaceByEntry(Instance, IEntry, obj);
if (not result) or
(IEntry^.IType in [etStandard, etFieldValue,
etStaticMethodResult, etVirtualMethodResult]) then
Break;
{ if interface is implemented by a class-type property or field,
continue search }
Instance := TObject(obj);
until False;
{ Getter function will normally AddRef, so adding another reference here
will cause memleak. (com interfaces only!) }
if result and Assigned(IEntry^.IID) and (IEntry^.IType in [etStandard, etFieldValue]) then
IInterface(obj)._AddRef;
end;
function TObject.GetInterface(const iidstr : shortstring;out obj) : boolean;
begin
Result := GetInterfaceByStr(iidstr,obj);
end;
class function TObject.GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
var
i: longint;
intftable: pinterfacetable;
ovmt: PVmt;
begin
ovmt := PVmt(Self);
while Assigned(ovmt) and {$ifdef VER3_0}(ovmt^.vIntfTable <> @emptyintf){$else}Assigned(ovmt^.vIntftable){$endif} do
begin
intftable:=ovmt^.vIntfTable;
{$ifdef VER3_0}
if assigned(intftable) then
{$endif VER3_0}
begin
for i:=0 to intftable^.EntryCount-1 do
begin
result:=@intftable^.Entries[i];
if assigned(Result^.iid) and IsGUIDEqual(Result^.iid^,iid) then
Exit;
end;
end;
ovmt := ovmt^.vParent;
end;
result := nil;
end;
class function TObject.GetInterfaceEntryByStr(const iidstr : shortstring) : pinterfaceentry;
var
i: longint;
intftable: pinterfacetable;
ovmt: PVmt;
begin
ovmt := PVmt(Self);
while Assigned(ovmt) and {$ifdef VER3_0}(ovmt^.vIntfTable <> @emptyintf){$else}Assigned(ovmt^.vIntfTable){$endif} do
begin
intftable:=ovmt^.vIntfTable;
{$ifdef VER3_0}
if assigned(intftable) then
{$endif VER3_0}
begin
for i:=0 to intftable^.EntryCount-1 do
begin
result:=@intftable^.Entries[i];
if assigned(result^.iidstr) and (result^.iidstr^ = iidstr) then
Exit;
end;
end;
ovmt := ovmt^.vParent;
end;
result:=nil;
end;
class function TObject.GetInterfaceTable : pinterfacetable;
begin
getinterfacetable:=PVmt(Self)^.vIntfTable;
end;
class function TObject.UnitName : {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
{$ifdef FPC_HAS_FEATURE_RTTI}
type
TClassTypeInfo = {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
Attributes: Pointer;
case TTypeKind of
tkClass: (
ClassType: TClass;
ParentInfo: Pointer;
PropCount: SmallInt;
UnitName: ShortString;
);
{ include for proper alignment }
tkInt64: (
Dummy: Int64;
);
end;
PClassTypeInfo = ^TClassTypeInfo;
var
classtypeinfo: PClassTypeInfo;
begin
classtypeinfo:=ClassInfo;
if Assigned(classtypeinfo) then
begin
// offset PTypeInfo by Length(Name) + 2 (ShortString length byte + SizeOf(Kind))
inc(Pointer(classtypeinfo), PByte(Pointer(classtypeinfo)+1)^ + 2);
{$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
classtypeinfo:=aligntoqword(classtypeinfo);
{$endif}
result:=classtypeinfo^.UnitName;
end
else
result:='';
end;
{$else not FPC_HAS_FEATURE_RTTI}
begin
result:='';
end;
{$endif ndef FPC_HAS_FEATURE_RTTI}
class function TObject.QualifiedClassName: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
var
uname: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
begin
uname := UnitName; //TODO: change 'UnitName' to 'UnitScope' as soon as RTL implement it
if uname='' then
result:=ClassName
else
result:=Concat(uname, '.', ClassName);
end;
function TObject.Equals(Obj: TObject) : boolean;
begin
result:=Obj=Self;
end;
function TObject.GetHashCode: PtrInt;
begin
result:=PtrInt(Self);
end;
function TObject.ToString: {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}ansistring{$else FPC_HAS_FEATURE_ANSISTRINGS}shortstring{$endif FPC_HAS_FEATURE_ANSISTRINGS};
begin
result:=ClassName;
end;
{****************************************************************************
TINTERFACEDOBJECT
****************************************************************************}
function TInterfacedObject.QueryInterface(
{$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
if getinterface(iid,obj) then
result:=S_OK
else
result:=longint(E_NOINTERFACE);
end;
function TInterfacedObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
_addref:=interlockedincrement(frefcount);
end;
function TInterfacedObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
_Release:=interlockeddecrement(frefcount);
if _Release=0 then
begin
if interlockedincrement(fdestroycount)=1 then
self.destroy;
end;
end;
destructor TInterfacedObject.Destroy;
begin
// We must explicitly reset. Bug ID 32353
FRefCount:=0;
FDestroyCount:=0;
inherited Destroy;
end;
procedure TInterfacedObject.AfterConstruction;
begin
{ we need to fix the refcount we forced in newinstance }
{ further, it must be done in a thread safe way }
declocked(frefcount);
end;
procedure TInterfacedObject.BeforeDestruction;
begin
if frefcount<>0 then
HandleError(204);
end;
class function TInterfacedObject.NewInstance : TObject;
begin
NewInstance:=inherited NewInstance;
if NewInstance<>nil then
TInterfacedObject(NewInstance).frefcount:=1;
end;
{****************************************************************************
TAGGREGATEDOBJECT
****************************************************************************}
constructor TAggregatedObject.Create(const aController: IUnknown);
begin
inherited Create;
{ do not keep a counted reference to the controller! }
fcontroller := Pointer(aController);
end;
function TAggregatedObject.QueryInterface(
{$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
Result := IUnknown(fcontroller).QueryInterface(iid, obj);
end;
function TAggregatedObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
Result := IUnknown(fcontroller)._AddRef;
end;
function TAggregatedObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
Result := IUnknown(fcontroller)._Release;
end;
function TAggregatedObject.GetController : IUnknown;
begin
Result := IUnknown(fcontroller);
end;
{****************************************************************************
TContainedOBJECT
****************************************************************************}
function TContainedObject.QueryInterface(
{$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
if getinterface(iid,obj) then
result:=S_OK
else
result:=longint(E_NOINTERFACE);
end;
{****************************************************************************
TCustomAttribute
****************************************************************************}
constructor TCustomAttribute.Create;
begin
inherited;
end;
{****************************************************************************
TCustomStoredAttribute
****************************************************************************}
constructor StoredAttribute.Create;
begin
end;
constructor StoredAttribute.Create(Const aFlag : Boolean);
begin
FFlag:=aFlag;
end;
constructor StoredAttribute.Create(Const aName : string);
begin
FName:=aName;
end;
{****************************************************************************
Exception Support
****************************************************************************}
{$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
{$i except.inc}
{$endif FPC_HAS_FEATURE_EXCEPTIONS}
|