File: jitclass.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (1675 lines) | stat: -rw-r--r-- 53,739 bytes parent folder | download
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
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
(*
This file is distributed under the Lesser GNU General Public License
(see the file COPYING.LGPL) with the following modification:

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,
and to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify this
library, you may extend this exception to your version of the library, but
you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.

If you didn't receive a copy of the file COPYING.LGPL, contact:
      Free Software Foundation, Inc.,
      675 Mass Ave
      Cambridge, MA  02139
      USA
*)
unit JitClass;

{$mode objfpc}{$H+}
{$ModeSwitch typehelpers}
{$ModeSwitch advancedrecords}
{$PointerMath on}
{.$Inline off}

{$WARN 4055 off : Conversion between ordinals and pointers is not portable}
interface

uses
  Classes, SysUtils, TypInfo, fgl, PackageDependencyIntf,
  LazLoggerBase, JitTypes, JitHelper, JitRttiWriter;

const
  JIT_PROP_NO_DEFAULT_VAL = longint($80000000);

type

  TJitClassCreator = class;
  TJitMethodList = class;

  { TJitMethod }

  TJitMethod = class(TCollectionItem)
  private
    FCodeAddress: CodePointer;
    FDeclaration: String;
    FName: String;
    FCreateDummyCodePointer: Boolean;
    FJitType: TJitTypeInfo;

    procedure DoJitTypeFreed(Sender: TObject);
    function GetTypeInfo: PTypeInfo;
    function MethodList: TJitMethodList;
  public
    constructor Create(ACollection: TCollection; AName, ADeclaration: String; ACodeAddr: CodePointer); reintroduce;
    constructor Create(ACollection: TCollection; AName: String; ACodeAddr: CodePointer); reintroduce;
    constructor Create(ACollection: TCollection; AName, ADeclaration: String; ACreateDummyCodeAddr: Boolean); reintroduce;
    constructor Create(ACollection: TCollection; AName: String; ACreateDummyCodeAddr: Boolean); reintroduce;
    destructor Destroy; override;

    property Name: String read FName;
    property CodeAddress: CodePointer read FCodeAddress;
    property Declaration: String read FDeclaration;
    property TypeInfo: PTypeInfo read GetTypeInfo;
  end;

  { TJitMethodList }

  TJitMethodList = class(TCollection)
  private
    FOwner: TJitClassCreator;
    FTypeLibrary: TJitTypeLibrary;
    function GetItem(AIndex: Integer): TJitMethod;
    function GetMeth(AName: String): TJitMethod;
  protected
    procedure Update(Item: TCollectionItem); override;
    property  TypeLibrary: TJitTypeLibrary read FTypeLibrary;
  public
    constructor Create(AOwner: TJitClassCreator); reintroduce;

    function Add(AName, ADeclaration: String; ACodeAddr: CodePointer): TJitMethod; reintroduce; overload;
    function Add(AName: String; ACodeAddr: CodePointer): TJitMethod; reintroduce; overload;
    function Add(AName, ADeclaration: String; ACreateDummyCodeAddr: Boolean): TJitMethod; reintroduce; overload;
    function Add(AName: String; ACreateDummyCodeAddr: Boolean): TJitMethod; reintroduce; overload;
    procedure Remove(AName: String);
    function  IndexOf(AName: String): integer;

    property  Meth[AName: String]: TJitMethod read GetMeth;
    property  Items[AIndex: Integer]: TJitMethod read GetItem; default;
  end;

  TJitPropertyList = class;

  { TJitProperty }

  TJitProperty = class(TCollectionItem)
  private
    FDefaultVal: Longint;
    FIsStored: Boolean;
    FName: String;
    FDeclaration: String;
    FJitType: TJitType;
    FNoDefault: Boolean;
    FTypeInfo: PTypeInfo; // For buildin
    FisBuildIn: Boolean;
    FInstanceMemOffset: Integer;
    FWriteAble: Boolean;
    FNameIndex: Integer; // temp storage

    procedure DoJitClassFreed(Sender: TObject);
    function GetInstanceDataPointer(AnInstance: TObject): Pointer;
    function GetJitType: TJitType;
    function GetKind: TTypeKind;
    function GetTypeInfo: PTypeInfo;
    function PropertyList: TJitPropertyList;

    procedure ParseFromDeclaration;
    procedure SetDefaultVal(AValue: Longint);
    procedure SetIsStored(AValue: Boolean);
    procedure SetNoDefault(AValue: Boolean);

  protected
    FRecursionWasTriggered: Boolean;
  public
    constructor Create(ACollection: TCollection; AName, ADeclaration: String;
      AWriteAble: Boolean = True; ADefault: LongInt = 0; ANoDefault: Boolean = False; AStored: Boolean = True); reintroduce;
    constructor Create(ACollection: TCollection; AName: String; AJitType: TJitType;
      AWriteAble: Boolean = True; ADefault: LongInt = 0; ANoDefault: Boolean = False; AStored: Boolean = True); reintroduce;
    destructor Destroy; override;

    procedure SetDefaultFromIdent(AnDefaultIdent: String); // enum

    property Name: String read FName;
    property Declaration: String read FDeclaration;
    property Kind: TTypeKind read GetKind;
    property WriteAble: Boolean read FWriteAble;
    property TypeInfo: PTypeInfo read GetTypeInfo;
    property JitType: TJitType read GetJitType;

    property NoDefault: Boolean read FNoDefault write SetNoDefault;
    property DefaultVal: Longint read FDefaultVal write SetDefaultVal;
    property IsStored: Boolean read FIsStored write SetIsStored;

    property InstanceDataPointer[AnInstance: TObject]: Pointer read GetInstanceDataPointer;
  end;

  TJitPropertyClass = class of TJitProperty;

  { TJitPropertyList }

  TJitPropertyList = class(TCollection)
  private type

    { TJitTypeInfoAcces }

    TJitTypeInfoParser = class(TJitTypeInfo)
    public
      constructor Create(ATypeName, AUnitName: String; AParser: PJitDeclarationParser;
        ATypeLibrary: TJitTypeLibrary = nil; AParseFlags: TJitTypeInfoParseFlags = []);
    end;
  private
    FOwner: TJitClassCreator;
    FTypeLibrary: TJitTypeLibrary;

    function GetItem(AIndex: Integer): TJitProperty;
    function GetKind(AName: String): TTypeKind;
    function GetProp(AName: String): TJitProperty;
    function DoCheckSectionEnd(AParser: PJitDeclarationParser): boolean;
  protected
    procedure Update(Item: TCollectionItem); override;
    property  TypeLibrary: TJitTypeLibrary read FTypeLibrary;
  public
    constructor Create(AOwner: TJitClassCreator); reintroduce; overload;
    constructor Create(AItemClass: TJitPropertyClass; AOwner: TJitClassCreator); reintroduce; overload;
    function  Add(AName, ADeclaration: String;
      AWriteAble: Boolean = True; ADefault: LongInt = 0; ANoDefault: Boolean = False; AStored: Boolean = True): TJitProperty; reintroduce;
    procedure Remove(AName: String);
    function  IndexOf(AName: String): integer;
    property  Prop[AName: String]: TJitProperty read GetProp;
    property  Items[AIndex: Integer]: TJitProperty read GetItem; default;
    procedure ParseFromClassDeclaration(ADecl: String);
  end;

  { TJitClassCreator }

  TJitClassCreator = class(TJitClassCreatorBase)
  private type

    { TVmtMem }

    TVmtMem = record
    strict private
      FExtraHeadSize: Integer; // Bytes allocated before the the actual typeinfo
    private
      FMemVmtPtr: PVmt;
      function GetHeadPtr: Pointer;
    public
      procedure Allocate(ASize: Integer; AExtraHeadSize: Integer = 0);
      procedure DeAllocate;
      procedure ClearMemPointer; // does not free
      property  HeadPtr: Pointer read GetHeadPtr;
      property  VmtPtr: PVmt read FMemVmtPtr;
      property  ExtraHeadSize: Integer read FExtraHeadSize;
    end;

    { TRefCountedJitClassReference }

    TRefCountedJitClassReference = class(TRefCountedJitNestedReference)
    private
      FJitPVmtMem: TVmtMem;
      FAnchorClassRef: TRefCountedJitReference;
      procedure SetJitPVmt(const AJitPVmtMem: TVmtMem);
      procedure FreePVmt;
      property FJitPVmt: PVmt read FJitPVmtMem.FMemVmtPtr;
    protected
      procedure DoRefCountZero; override;
      function NestedCount: integer; override;
      function GetNested(AnIndex: integer): TRefCountedJitReference; override;
    public
      constructor Create(const AJitPVmtMem: TVmtMem);
      procedure AddToList(AJitProp: TJitProperty);
      procedure ClearList; override;
    end;

    TJitClassCreatorFlag = (
      ccfModifiedMethods, ccfModifiedProps, ccfModifiedClassName,
      ccfContinueAfterVMTNeeded, // vTypeInfo is a stub and must still be finished
      ccfJitPropsPrepareDone
    );
    TJitClassCreatorFlags = set of TJitClassCreatorFlag;
  strict private
    FJitPVmtMem: TVmtMem;
  private
    FJitMethods: TJitMethodList;
    FJitProperties: TJitPropertyList;
    FFlags: TJitClassCreatorFlags;

    property FJitPVmt: PVmt read FJitPVmtMem.FMemVmtPtr;
  private
    FRefCountedJitPVmt: TRefCountedJitClassReference;
    FAncestorClass: TClass;
    FAncestorClassName: String;
    FAncestorJitClass: TJitClassCreator;
    FAncestorJitType: TJitTypeClassBase;
    FClassName: String;
    FTypeLibrary: TJitTypeLibrary;

    // Set by CreateJitPropsPrepare for CreateJitPropsFinish
    FTypeInfoMemSize, FRedirectPtrMemSize, FVmtParentMemSize: Integer;
    FUserInfoMemSize: Integer;
    FRttiWriterClass: TJitRttiWriterTkClass;

    function GetAncestorJitClass: TJitClassCreator;
    function RefCountedJitPvmt: TRefCountedJitClassReference;
    procedure AllocateJitPVmt(ASize: Integer);
    procedure DeAllocateJitPVmt;
    procedure DoTypeLibFreed(Sender: TObject);
    procedure DoAnchesterJitClassFreed(Sender: TObject);

    procedure SetClassName(AValue: String);
    procedure SetClassUnit(AValue: String);
    procedure SetTypeLibrary(AValue: TJitTypeLibrary);
    procedure ResolveAnchestor;
    procedure RaiseUnless(ACond: Boolean; const AMsg: string);
    function dbgsFlag(AFlags: TJitClassCreatorFlags): String;
  protected
    class procedure FreeJitClass(const AJitPVmtMem: TVmtMem);
    function GetLockReferenceObj: TRefCountedJitReference; override;
    function  GetTypeInfo: PTypeInfo; override;
    function GetJitClass: TClass; override;

    procedure CreateJitClass;
    procedure CreateJitClassPreCheck;
    procedure CreateJitClassVMT;
    procedure CreateJitClassCallAllProp(AFirstEntry: Boolean = False);
    procedure CreateJitClassContinueAfteVMT;

    procedure UpdateClassName;
    procedure CreateJitMethods;
    procedure CreateJitProps;
    procedure CreateJitPropsPrepare;
    procedure CreateJitPropsFinish;

    procedure Init; virtual;
    function  CreateJitPropertyList: TJitPropertyList; virtual;
  public
    constructor Create(AnAncestorClass: TClass; AClassName: String; AClassUnit: String; ATypeLibrary: TJitTypeLibrary = nil);
    constructor Create(AnAncestorClassName, AClassName: String; AClassUnit: String; ATypeLibrary: TJitTypeLibrary = nil);
    constructor Create(AnAncestorJitClass: TJitClassCreator; AClassName: String; AClassUnit: String; ATypeLibrary: TJitTypeLibrary = nil);
    constructor Create(AnAncestorJitType: TJitType; AClassName: String; AClassUnit: String; ATypeLibrary: TJitTypeLibrary = nil);
    destructor Destroy; override;

    (* UpdateJitClass
       * If no JitClass has been created, this forces creation of the JitClass.
         So calling the property JitClass later will not see any changes made
         from here on
       * If a JitClass already exists, this keeps the JitClass.
         It will update the following data:
         - Updates the ClassName
         - recreates the vmtMethodTable
         It will NOT update changes to the JitProperties
    *)
    procedure UpdateJitClass;
    (* This will trigger re-creation of the JitClass by removing the current
       JitClass.
       (The memory will be kept for any existing LockReference. So any instances
       created from the class, can be kept with the current class, by keeping
       a lock)
       The JitClass will be recreated, when the property JitClass is called
       again (or when UpdateJitClass is called).
    *)
    procedure RecreateJitClass;

    property AncestorClass: TClass read FAncestorClass; // write SetAncestorClass;
    property ClassName: String read FClassName write SetClassName; deprecated 'use JitClassName';
    property JitClassName: String read FClassName write SetClassName;

    property  TypeLibrary: TJitTypeLibrary read FTypeLibrary write SetTypeLibrary;
    property JitMethods: TJitMethodList read FJitMethods;
    property JitProperties: TJitPropertyList read FJitProperties;

    function FindPropertyRecursive(AName: String): TJitProperty;

    property JitClass: TClass read GetJitClass;
    property AncestorJitClass: TJitClassCreator read GetAncestorJitClass; experimental;

    property UserInfoMemSize: Integer read FUserInfoMemSize write FUserInfoMemSize; // User must call procedure UpdateJitClass for it to take effect
  end;

implementation

const EmptyIntf : array [0..3] of PtrUInt = (0,0,0,0); // Count ond Entries

function GetVMTSize(AClass: TClass): integer;
const
  MAX_VMT_SIZE = 100000;
var
  p: PPointer;
begin
  assert(AClass <> nil, 'GetVMTSize: AClass <> nil');
  Result:=vmtMethodStart;
  p:=PPointer(pointer(AClass)+Result);
  while (p^<>nil) and (Result<MAX_VMT_SIZE) do begin
    inc(p);
    inc(Result,SizeOf(Pointer));
  end;
  inc(Result,SizeOf(Pointer)); // include the trailing 0
end;

{ TJitMethodList }

function TJitMethodList.GetItem(AIndex: Integer): TJitMethod;
begin
  Result := TJitMethod(inherited Items[AIndex]);
end;

function TJitMethodList.GetMeth(AName: String): TJitMethod;
var
  i: Integer;
begin
  i := IndexOf(AName);
  if i >= 0 then
    Result := TJitMethod(Items[i])
  else
    Result := nil;
end;

procedure TJitMethodList.Update(Item: TCollectionItem);
begin
  inherited Update(Item);
  include(FOwner.FFlags, ccfModifiedMethods);
end;

function TJitMethodList.IndexOf(AName: String): integer;
begin
  Result := Count - 1;
  while (Result >= 0) and (Items[Result].Name <> AName) do
    dec(Result);
end;

constructor TJitMethodList.Create(AOwner: TJitClassCreator);
begin
  FOwner := AOwner;
  inherited Create(TJitMethod);
end;

function TJitMethodList.Add(AName, ADeclaration: String; ACodeAddr: CodePointer
  ): TJitMethod;
begin
  Result := TJitMethod.Create(Self, AName, ADeclaration, ACodeAddr);
end;

function TJitMethodList.Add(AName: String; ACodeAddr: CodePointer): TJitMethod;
begin
  Result := TJitMethod.Create(Self, AName, ACodeAddr);
end;

function TJitMethodList.Add(AName, ADeclaration: String;
  ACreateDummyCodeAddr: Boolean): TJitMethod;
begin
  Result := TJitMethod.Create(Self, AName, ADeclaration, ACreateDummyCodeAddr);
end;

function TJitMethodList.Add(AName: String; ACreateDummyCodeAddr: Boolean
  ): TJitMethod;
begin
  Result := TJitMethod.Create(Self, AName, ACreateDummyCodeAddr);
end;

procedure TJitMethodList.Remove(AName: String);
begin
  Delete(IndexOf(AName));
end;

{ TJitMethod }

function TJitMethod.MethodList: TJitMethodList;
begin
  Result := TJitMethodList(Collection);
end;

function TJitMethod.GetTypeInfo: PTypeInfo;
begin
  if FJitType = nil then begin
    FJitType := TJitTypeInfo.Create('', FDeclaration,
      MethodList.FOwner.ClassUnit, MethodList.TypeLibrary, [pfAllowProcName, pfAlwaysAsMethod]);
    FJitType.AddFreeNotification(@DoJitTypeFreed);
  end;

  Result := FJitType.TypeInfo;
end;

procedure TJitMethod.DoJitTypeFreed(Sender: TObject);
begin
  FJitType := nil;
end;

constructor TJitMethod.Create(ACollection: TCollection; AName,
  ADeclaration: String; ACodeAddr: CodePointer);
begin
  inherited Create(ACollection);
  if MethodList.IndexOf(AName) >= 0 then
    raise Exception.Create('duplicate method name');

  FName := AName;
  FDeclaration := ADeclaration;
  FCodeAddress := ACodeAddr;
end;

constructor TJitMethod.Create(ACollection: TCollection; AName: String;
  ACodeAddr: CodePointer);
begin
  Create(ACollection, AName, '', ACodeAddr);
end;

constructor TJitMethod.Create(ACollection: TCollection; AName,
  ADeclaration: String; ACreateDummyCodeAddr: Boolean);
begin
  Create(ACollection, AName, ADeclaration, nil);
  FCreateDummyCodePointer := True;
end;

constructor TJitMethod.Create(ACollection: TCollection; AName: String;
  ACreateDummyCodeAddr: Boolean);
begin
  Create(ACollection, AName, '', nil);
  FCreateDummyCodePointer := True;
end;

destructor TJitMethod.Destroy;
begin
  FJitType.Free;
  inherited Destroy;
end;

{ TJitClassCreator.TVmtMem }

function TJitClassCreator.TVmtMem.GetHeadPtr: Pointer;
begin
  Result := Pointer(FMemVmtPtr) - FExtraHeadSize;
end;

procedure TJitClassCreator.TVmtMem.Allocate(ASize: Integer;
  AExtraHeadSize: Integer);
begin
  assert(FMemVmtPtr=nil, 'TJitClassCreator.TVmtMem.Allocate: FMemVmtPtr=nil');
  AExtraHeadSize := align(AExtraHeadSize, sizeof(Pointer));
  FExtraHeadSize := AExtraHeadSize;
  FMemVmtPtr := AllocMem(ASize + AExtraHeadSize) + AExtraHeadSize;
end;

procedure TJitClassCreator.TVmtMem.DeAllocate;
begin
  Freemem(HeadPtr);
  FMemVmtPtr := nil;
  FExtraHeadSize := 0;
end;

procedure TJitClassCreator.TVmtMem.ClearMemPointer;
begin
  FMemVmtPtr := nil;
  FExtraHeadSize := 0;
end;

{ TJitClassCreator.TRefCountedJitClassReference }

procedure TJitClassCreator.TRefCountedJitClassReference.SetJitPVmt(
  const AJitPVmtMem: TVmtMem);
begin
  if (RefCount > 1) and (FJitPVmt <> nil) then
    raise Exception.Create('set TypeInfo while referrenced');

  if (FJitPVmt <> nil) then begin
    inherited ClearList;
    FreePVmt;
  end;

  FJitPVmtMem := AJitPVmtMem;
end;

procedure TJitClassCreator.TRefCountedJitClassReference.FreePVmt;
begin
  TJitClassCreator.FreeJitClass(FJitPVmtMem);
end;

procedure TJitClassCreator.TRefCountedJitClassReference.DoRefCountZero;
begin
  FreePVmt;
  if FAnchorClassRef <> nil then begin
    FAnchorClassRef.ReleaseLock;
    FAnchorClassRef := nil;
  end;
  inherited DoRefCountZero;
end;

function TJitClassCreator.TRefCountedJitClassReference.NestedCount: integer;
begin
  Result := inherited NestedCount;
  if FAnchorClassRef <> nil then
    inc(Result);
end;

function TJitClassCreator.TRefCountedJitClassReference.GetNested(
  AnIndex: integer): TRefCountedJitReference;
begin
  if FAnchorClassRef <> nil then begin
  if (AnIndex = 0) then
      Result := FAnchorClassRef
    else
      Result := inherited GetNested(AnIndex - 1);
  end
  else
    Result := inherited GetNested(AnIndex);
end;

constructor TJitClassCreator.TRefCountedJitClassReference.Create(
  const AJitPVmtMem: TVmtMem);
begin
  inherited Create;
  FJitPVmtMem := AJitPVmtMem;
end;

procedure TJitClassCreator.TRefCountedJitClassReference.AddToList(
  AJitProp: TJitProperty);
var
  jt: TJitType;
begin
  if AJitProp = nil then
    exit;
  jt := AJitProp.JitType;
  if jt = nil then
    exit;
  inherited AddToList(jt.LockReference);
end;

procedure TJitClassCreator.TRefCountedJitClassReference.ClearList;
begin
  inherited ClearList;
  if FAnchorClassRef <> nil then
    FAnchorClassRef.ReleaseLock;
  FAnchorClassRef := nil;
end;

{ TJitProperty }

function TJitProperty.PropertyList: TJitPropertyList;
begin
  Result := TJitPropertyList(Collection);
end;

function TJitProperty.GetKind: TTypeKind;
var
  t: PTypeInfo;
begin
  Result := tkUnknown;
  t := TypeInfo;
  if t <> nil then
    Result := t^.Kind;
end;

function TJitProperty.GetJitType: TJitType;
begin
  Result := FJitType;
  if Result <> nil then
    exit;

  ParseFromDeclaration;
  Result := FJitType;
end;

function TJitProperty.GetInstanceDataPointer(AnInstance: TObject): Pointer;
begin
  if (FInstanceMemOffset > 0) and (AnInstance <> nil) then
    Result := Pointer(AnInstance) + FInstanceMemOffset
  else
    Result := nil;
end;

procedure TJitProperty.DoJitClassFreed(Sender: TObject);
begin
  FJitType := nil;
end;

function TJitProperty.GetTypeInfo: PTypeInfo;
var
  jt: TJitType;
begin
  Result := nil;
  jt := JitType;
  if FisBuildIn then begin
    Result := FTypeInfo;
    exit;
  end;

  if jt = nil then
    exit;
  Result := jt.TypeInfo;
end;

constructor TJitProperty.Create(ACollection: TCollection; AName,
  ADeclaration: String; AWriteAble: Boolean; ADefault: LongInt;
  ANoDefault: Boolean; AStored: Boolean);
begin
  inherited Create(ACollection);
  if PropertyList.IndexOf(AName) >= 0 then
    raise Exception.Create('duplicate property name');

  FName := AName;
  FDeclaration := ADeclaration;
  FWriteAble   := AWriteAble;
  FDefaultVal  := ADefault;
  FNoDefault   := ANoDefault;
  FIsStored    := AStored;
end;

constructor TJitProperty.Create(ACollection: TCollection; AName: String;
  AJitType: TJitType; AWriteAble: Boolean; ADefault: LongInt;
  ANoDefault: Boolean; AStored: Boolean);
begin
  inherited Create(ACollection);
  if PropertyList.IndexOf(AName) >= 0 then
    raise Exception.Create('duplicate property name');

  FName := AName;
  FJitType := AJitType;
  FJitType.AddFreeNotification(@DoJitClassFreed);
  FWriteAble   := AWriteAble;
  FDefaultVal  := ADefault;
  FNoDefault   := ANoDefault;
  FIsStored    := AStored;
end;

destructor TJitProperty.Destroy;
begin
  inherited Destroy;
  if (FJitType <> nil) then
    FJitType.RemoveFreeNotification(@DoJitClassFreed);
  if (FJitType <> nil) and (not FJitType.OwnedByLibrary) then
    FJitType.Free;
end;

procedure TJitProperty.SetDefaultFromIdent(AnDefaultIdent: String);
var
  i: int64;
  ti: PTypeInfo;
begin
  if AnDefaultIdent = '' then begin
    DefaultVal := 0;
    exit;
  end;
  if TryStrToInt64(AnDefaultIdent, i) then begin
    DefaultVal := i;
    exit;
  end;
  ti := TypeInfo;

  if ti^.Kind = tkEnumeration then begin
    i := GetEnumValue(ti, AnDefaultIdent);
    if i >= 0 then begin
      DefaultVal := i;
      exit;
    end;
  end
  else
  if ti^.Kind = tkSet then begin
    i := StringToSet(ti, AnDefaultIdent);
    DefaultVal := i;
    exit;
  end;

  raise Exception.Create('Cannot parse default');
end;

procedure TJitProperty.ParseFromDeclaration;
var
  Decl: String;
  TypeLib: TJitTypeLibrary;
begin
  assert(FJitType = nil, 'TJitProperty.ParseFromDeclaration: FJitType = nil');
  if FisBuildIn then
    exit;

  Decl := TrimDeclaration(FDeclaration);
  if Decl = '' then
    exit;

  TypeLib := PropertyList.TypeLibrary;

  if TypeLib <> nil then begin
    FJitType := TypeLib[FDeclaration];
    if FJitType <> nil then begin
      FJitType.AddFreeNotification(@DoJitClassFreed);
      exit;
    end;
  end;

  FTypeInfo := TypeInfoByName(Decl);
  FisBuildIn := FTypeInfo <> nil;
  if FisBuildIn then
    exit;

  FJitType := TJitTypeInfo.Create(FName, Decl, PropertyList.FOwner.ClassUnit, PropertyList.TypeLibrary);
end;

procedure TJitProperty.SetDefaultVal(AValue: Longint);
begin
  if FDefaultVal = AValue then Exit;
  FDefaultVal := AValue;
  Changed(False);
end;

procedure TJitProperty.SetIsStored(AValue: Boolean);
begin
  if FIsStored = AValue then Exit;
  FIsStored := AValue;
  Changed(False);
end;

procedure TJitProperty.SetNoDefault(AValue: Boolean);
begin
  if FNoDefault = AValue then Exit;
  FNoDefault := AValue;
  Changed(False);
end;

{ TJitPropertyList.TJitTypeInfoParser }

constructor TJitPropertyList.TJitTypeInfoParser.Create(ATypeName,
  AUnitName: String; AParser: PJitDeclarationParser;
  ATypeLibrary: TJitTypeLibrary; AParseFlags: TJitTypeInfoParseFlags);
begin
  inherited Create(ATypeName, AUnitName, '', ATypeLibrary, AParseFlags);
  ParseFromDeclaration(AParser);
end;

{ TJitPropertyList }

function TJitPropertyList.DoCheckSectionEnd(AParser: PJitDeclarationParser
  ): boolean;
var
  s: String;
begin
  s := LowerCase(AParser^.PeekTokenRaw); // "Raw" => must not strip any escaping &ident
  Result := (s = 'write') or (s = 'read') or (s = 'default') or (s = 'nodefault') or (s = 'stored');
end;

function TJitPropertyList.GetItem(AIndex: Integer): TJitProperty;
begin
  Result := TJitProperty(inherited Items[AIndex]);
end;

function TJitPropertyList.GetKind(AName: String): TTypeKind;
begin
  Result := Prop[AName].Kind;
end;

function TJitPropertyList.GetProp(AName: String): TJitProperty;
var
  i: Integer;
begin
  i := IndexOf(AName);
  if i >= 0 then
    Result := TJitProperty(Items[i])
  else
    Result := nil;
end;

procedure TJitPropertyList.Update(Item: TCollectionItem);
begin
  inherited Update(Item);
  include(FOwner.FFlags, ccfModifiedProps);
end;

function TJitPropertyList.IndexOf(AName: String): integer;
begin
  Result := Count - 1;
  while (Result >= 0) and (Items[Result].Name <> AName) do
    dec(Result);
end;

constructor TJitPropertyList.Create(AOwner: TJitClassCreator);
begin
  Create(TJitProperty, AOwner);
end;

constructor TJitPropertyList.Create(AItemClass: TJitPropertyClass;
  AOwner: TJitClassCreator);
begin
  FOwner := AOwner;
  inherited Create(AItemClass);
end;

function TJitPropertyList.Add(AName, ADeclaration: String; AWriteAble: Boolean;
  ADefault: LongInt; ANoDefault: Boolean; AStored: Boolean): TJitProperty;
begin
  Result := TJitPropertyClass(ItemClass).Create(Self, AName, ADeclaration, AWriteAble,
    ADefault, ANoDefault, AStored);
end;

procedure TJitPropertyList.Remove(AName: String);
begin
  Delete(IndexOf(AName));
end;

procedure TJitPropertyList.ParseFromClassDeclaration(ADecl: String);
var
  Parser: TJitDeclarationParser;
  InPublished, NewWritable, NewNoDefault, NewIsStored, HasClass: Boolean;
  tk: TJitParserTkKind;
  NewName, s, NewDefault: String;
  NewPropJitType: TJitTypeInfoParser;
  TheProp: TJitProperty;
begin
  if ADecl = '' then
    exit;

  Parser := TJitDeclarationParser.Create(@ADecl[1]);
  InPublished := true;

  HasClass := False;
  if Parser.CurrentKind = kwClass then begin
    HasClass := True;
    // skipp "class (foo, intf)
    if Parser.PeekKind = ptRoundOpen then begin
      Parser.Next;
      tk := Parser.Next;
      while tk in [ptIdent, ptDot, ptComma] do
        tk := Parser.Next;
      if tk <> ptRoundClose then
        raise Exception.Create('expecting )');
    end;
    Parser.Next;
  end;

  while True do begin
    case Parser.CurrentKind of
      kwPublished: begin
          InPublished := True;
          Parser.Next;
        end;
      kwPrivate, kwProtected, kwPublic: begin
          InPublished := False;
          Parser.Next;
        end;
      kwFunction, kwProcedure: begin
          TJitTypeInfoParser.Create('', FOwner.ClassUnit, @Parser, TypeLibrary, [pfAllowProcName]).Free;
          tk := Parser.Next;
          if not (tk in [ptSemicolon, kwEnd]) then
            raise Exception.Create('expecting ;');
          Parser.Next;
        end;
      ptIdent: begin
          // skip variables
          tk := Parser.Next;
          while tk in [ptIdent, ptDot, ptComma] do
            tk := Parser.Next;
          if tk <> ptColon then
            raise Exception.Create('expecting :');
          Parser.Next;

          TJitTypeInfoParser.Create('', FOwner.ClassUnit, @Parser, TypeLibrary).Free;

          tk := Parser.Next;
          if not (tk in [ptSemicolon, kwEnd]) then
            raise Exception.Create('expecting ;');
          Parser.Next;
        end;
      kwProperty: begin
          tk := Parser.Next;
          if tk <> ptIdent then
            raise Exception.Create('expecting name');
          NewName := Parser.CurrentToken;

          tk := Parser.Next;
          if tk = ptSquareOpen then begin
            // no support for indexed properties / but skip over it
            tk := Parser.Next;
            while tk in [ptIdent, ptDot, ptColon, ptSemicolon] do
              tk := Parser.Next;
            if tk <> ptSquareClose then
              raise Exception.Create('expecting ]');
            tk := Parser.Next;
          end;
          if tk <> ptColon then
            raise Exception.Create('expecting :');
          Parser.Next;

          Parser.CheckSectionEndProc  := @DoCheckSectionEnd; // check for read/write keywords in property declaration
          NewPropJitType := TJitTypeInfoParser.Create('', FOwner.ClassUnit, @Parser, TypeLibrary);
          Parser.CheckSectionEndProc  := nil;

          NewWritable := False;
          NewDefault:= '';
          NewNoDefault := False;
          NewIsStored := True;
          tk := Parser.Next;
          if tk = ptIdent then begin // stopped at read or write
            while tk in [ptIdent, ptDot] do begin
              if (tk = ptIdent) then begin
                s := LowerCase(Parser.CurrentToken);
                if (s = 'write') then
                  NewWritable := True;
                if (s = 'default') then begin
                  tk := Parser.Next();
                  NewDefault := Parser.CurrentToken;
                  if not (tk in [ptIdent, ptNum]) then
                    raise Exception.Create('expecting default value');
                end;
                if (s = 'nodefault') then
                  NewNoDefault := True;
                if (s = 'stored') then begin
                  Parser.Next();
                  s := LowerCase(Parser.CurrentToken);
                  if s = 'true' then
                    NewIsStored := True
                  else
                  if s = 'false' then
                    NewIsStored := False
                  else
                    raise Exception.Create('expecting stored value');
                end;
              end;
              tk := Parser.Next;
            end;
          end;

          if not (tk in [ptSemicolon, kwEnd]) then
            raise Exception.Create('expecting ;');
          Parser.Next;
          // todo: skip deprecated and the lot

          if InPublished then begin
            TheProp := TJitPropertyClass(ItemClass).Create(Self, NewName, NewPropJitType, NewWritable, 0, NewNoDefault);
            TheProp.SetDefaultFromIdent(NewDefault);
            TheProp.SetIsStored(NewIsStored);
          end
          else
            NewPropJitType.Free;

        end;
      ptEOT: begin
          if HasClass then
            raise Exception.Create('expected "end", but got EOT')
          else
            break;
        end;
      kwEnd: break;
      else
        raise Exception.Create('unexpected: ' + Parser.CurrentToken);
    end;
  end;
end;

{ TJitClassCreator }

procedure TJitClassCreator.DoTypeLibFreed(Sender: TObject);
begin
  FTypeLibrary := nil;
  FJitProperties.FTypeLibrary := nil;
  FJitMethods.FTypeLibrary := nil;
end;

procedure TJitClassCreator.DoAnchesterJitClassFreed(Sender: TObject);
begin
  FAncestorJitClass := nil;
end;

function TJitClassCreator.RefCountedJitPvmt: TRefCountedJitClassReference;
begin
  if FRefCountedJitPVmt = nil then begin
    (* FTypeInfo may be nil, but a refernce can be got anyway *)
    FRefCountedJitPVmt := TRefCountedJitClassReference.Create(FJitPVmtMem);
  end;
  Result := FRefCountedJitPVmt;
end;

function TJitClassCreator.GetAncestorJitClass: TJitClassCreator;
begin
  if FAncestorJitClass = nil then
    ResolveAnchestor;
  Result := FAncestorJitClass;
end;

procedure TJitClassCreator.AllocateJitPVmt(ASize: Integer);
begin
  DeAllocateJitPVmt;
  FJitPVmtMem.Allocate(ASize, FUserInfoMemSize);

  if FRefCountedJitPVmt <> nil then begin
    if (FRefCountedJitPVmt.RefCount = 1) or (FRefCountedJitPVmt.FJitPVmt = nil) then
      FRefCountedJitPVmt.SetJitPVmt(FJitPVmtMem)
    else begin
      FRefCountedJitPVmt.ReleaseLock;
      FRefCountedJitPVmt := TRefCountedJitClassReference.Create(FJitPVmtMem);
    end;
  end;
end;

procedure TJitClassCreator.DeAllocateJitPVmt;
begin
  if FJitPVmt = nil then
    exit;

  if (FRefCountedJitPVmt <> nil) and (FRefCountedJitPVmt.RefCount > 1) then begin
    FRefCountedJitPVmt.ReleaseLock;
    FJitPVmtMem.ClearMemPointer; // memory is kept bi ref
  end
  else begin
    FJitPVmtMem.DeAllocate;
    if (FRefCountedJitPVmt <> nil) then
      FRefCountedJitPVmt.SetJitPVmt(FJitPVmtMem);
  end;
end;

function TJitClassCreator.GetJitClass: TClass;
begin
  if not assigned(FJitPVmt) then
    CreateJitClass
  else begin
    CreateJitClassCallAllProp; // Finish triggering all recursions
    if ccfContinueAfterVMTNeeded in FFlags then
      CreateJitClassContinueAfteVMT;
  end;

  Result := TClass(FJitPVmt);
end;

procedure TJitClassCreator.SetClassName(AValue: String);
begin
  if FClassName = AValue then Exit;
  FClassName := AValue;
  include(FFlags, ccfModifiedClassName);
end;

procedure TJitClassCreator.SetClassUnit(AValue: String);
begin
  if FClassUnit = AValue then Exit;
  FClassUnit := AValue;
end;

procedure TJitClassCreator.SetTypeLibrary(AValue: TJitTypeLibrary);
begin
  if FTypeLibrary <> nil then
    FTypeLibrary.RemoveFreeNotification(@DoTypeLibFreed);

  FTypeLibrary := AValue;
  FJitProperties.FTypeLibrary := AValue;
  FJitMethods.FTypeLibrary := AValue;

  if FTypeLibrary <> nil then
    FTypeLibrary.AddFreeNotification(@DoTypeLibFreed);
end;

procedure TJitClassCreator.ResolveAnchestor;
var
  at: TJitType;
begin
  if (FAncestorClass = nil) and (FAncestorJitClass = nil) then begin
    if (FAncestorJitType = nil) and (FAncestorClassName <> '') and (FTypeLibrary <> nil) then begin
      at := FTypeLibrary.FindType(FAncestorClassName, FClassUnit);
      if not (at is TJitTypeClassBase) then
        raise Exception.Create('Incorrect type for anchestor');
      FAncestorJitType := TJitTypeJitClass(at);
    end;

    if FAncestorJitType <> nil then begin
      if (FAncestorJitType is TJitTypeJitClass) then
        FAncestorJitClass := TJitClassCreator(TJitTypeJitClass(FAncestorJitType).JitClassCreator)
      else
        FAncestorClass := FAncestorJitType.JitClass;
    end;
  end;
end;

procedure TJitClassCreator.RaiseUnless(ACond: Boolean; const AMsg: string);
begin
  if not ACond then
    raise Exception.Create(AMsg);
end;

function TJitClassCreator.dbgsFlag(AFlags: TJitClassCreatorFlags): String;
var
  i: TJitClassCreatorFlag;
  s: String;
begin
  Result := '';
  for i in TJitClassCreatorFlags do
    if i in AFlags then begin
      WriteStr(s, i);
      Result := Result + ',' + s;
    end;
  if Result = '' then
    exit;
  Result[1] := '[';
  Result := Result + ']';
end;

function TJitClassCreator.GetLockReferenceObj: TRefCountedJitReference;
begin
  Result := RefCountedJitPvmt;
end;

procedure TJitClassCreator.CreateJitClassPreCheck;
begin
  assert(FJitPVmt = nil, 'TJitClassCreator.CreateJitClassPreCheck: FJitPVmt = nil');
  RaiseUnless((FAncestorClass <> nil) or (FAncestorJitClass <> nil), 'Missing Ancestor');
  RaiseUnless(IsValidIdent(FClassName), 'Invalid or missing ClassName');
  RaiseUnless(IsValidUnitName(FClassUnit), 'Invalid or missing UnitName');
end;

procedure TJitClassCreator.CreateJitClassVMT;
var
  AncestorVMT: PVmt;
  VmtFullSize, VmtMethodsSize: Integer;
begin
  (* Create the VMT, so it can be used as anchestor.
     Do not yet access any Properties, to avoid recursion. This may be called
     by a child-class that can not yet provide typeinfo
  *)
  if FAncestorClass = nil then begin
    (* Only get the class VMT
       Do not yet trigger the vTypeInfo(ClassInfo), as that may cause recursion
       => Access field direct. It was set by calling FAncestorJitClass.CreateJitClass(True)
    *)
    FAncestorClass := TClass(FAncestorJitClass.FJitPVmt);
    RaiseUnless((FAncestorClass <> nil), 'Missing Ancestor');
    RefCountedJitPvmt.FAnchorClassRef := FAncestorJitClass.LockReference;
    assert(FJitPVmt = nil, 'TJitClassCreator.CreateJitClassVMT: Not called recursively by anchestor');
// TODO: adjust FieldOffsets
  end;

  AncestorVMT:=PVmt(FAncestorClass);
  DebugLn(AncestorVMT^.vAutoTable <> nil, 'vmtAutoTable is not yet supported');

  // create vmt
  VmtFullSize:=GetVMTSize(FAncestorClass);
  VmtMethodsSize:=VmtFullSize-vmtMethodStart;
  AllocateJitPVmt(VmtFullSize);

  (* The following entries are searched recursively in the base classes,
     and do not need to be copied.
     vmtDynamicTable
     vmtMethodTable: look up published methods
     vmtFieldTable:  published fields for componenents
     vmtIntfTable
     vmtMsgStrPtr
  *)
  FJitPVmt^.vIntfTable:=@EmptyIntf; // A nil pointer stops the recursion

  // set vmtParent
  GetMem(FJitPVmt^.vParentRef,SizeOf(Pointer));
  FJitPVmt^.vParentRef^:=AncestorVMT;

  // copy the methods part
  System.Move(Pointer(Pointer(AncestorVMT)+vmtMethodStart)^,
              Pointer(Pointer(FJitPVmt)+vmtMethodStart)^,
              VmtMethodsSize);
end;

procedure TJitClassCreator.CreateJitClassCallAllProp(AFirstEntry: Boolean);
var
  i: Integer;
begin
  (* Trigger all properties.
     - This may trigger an child-class, which needs to call (and wait for) CreateJitPropsFinish
     - Any child class that is triggered will call our GetJitClass.
       - If that happens we must finish CreateJitPropsFinish (calculate vInstanceSize
         before we return.
       - An we may have refernces to several children of ours, we must trigger
         all of them, before entering CreateJitPropsFinish.
         Once in CreateJitPropsFinish, all props are called again, but would get
         a stub (vInstanceSize = zero) back.
         Therefore we continue this loop when we enter GetJitClass.
         (Recursively, until we have done all entries. Then each entry can be given
         a full TypeInfo)
  *)
  if AFirstEntry then
    For i := 0 to FJitProperties.Count - 1 do
      FJitProperties.Items[i].FRecursionWasTriggered := False;

  For i := 0 to FJitProperties.Count - 1 do
    if not FJitProperties.Items[i].FRecursionWasTriggered then begin
      FJitProperties.Items[i].FRecursionWasTriggered := True;
      FJitProperties.Items[i].TypeInfo;
    end;
end;

procedure TJitClassCreator.CreateJitClassContinueAfteVMT;
begin
  if (FAncestorJitClass <> nil) and (PVmt(FAncestorClass)^.vInstanceSize  = 0) then begin
    include(FFlags, ccfContinueAfterVMTNeeded); // come back, when the anchestor is ready
    exit;
  end;
  Exclude(FFlags, ccfContinueAfterVMTNeeded);
  CreateJitProps;
  CreateJitMethods;
end;

procedure TJitClassCreator.CreateJitClass;
var
  HasJitAnchestor: Boolean;
begin
  ResolveAnchestor;
  CreateJitClassPreCheck;
  FFlags := FFlags - [ccfModifiedMethods, ccfModifiedProps, ccfModifiedClassName];

  (* * We always need the anchestor first
       - vInstanceSize
       - PropList (with Count and NameIndex)
     * Once the anchestor has some stub ready
       (At the end of CreateJitPropsPrepare, incl PropList, excl vInstanceSize)
       it will trigger all recursions.
       CreateJitClass may be re-enteded. (Including being called as anchestor)
     * Re-entry of CreateJitClass will
       - build the full VMT and TypeInfo (incl PropList and vInstanceSize)
       - in the process, it will itself trigger recursion
       - It will make a further call to the anchestor, which will complete
         the anchestors vInstanceSize
  *)
  HasJitAnchestor := (FAncestorClass = nil);
  if HasJitAnchestor then begin
    FAncestorJitClass.JitClass;
    (* If we are
       *>> OUTSIDE a recursion   then
           - FAncestorJitClass.FJitPVmt           is fully created
           - FAncestorJitClass.FJitPVmt.vTypeInfo is fully created
           => We can fully create our own FJitPVmt and TypeInfo
       *>> WITHIN a recursion   then  (only if recursing from the anchestor)
           - FAncestorJitClass.FJitPVmt           is a  STUB  (vInstanceSize = 0)
           - FAncestorJitClass.FJitPVmt.vTypeInfo is a  STUB  (with a PropList stub)
           => We will create a  STUB  for our own FJitPVmt and TypeInfo
              We will finish the stub, once back in our outer call.
    *)
    assert(((FAncestorJitClass.FJitPVmt <> nil) and (FAncestorJitClass.FJitPVmt^.vTypeInfo <> nil)), 'TJitClassCreator.CreateJitClass: ((FAncestorJitClass.FJitPVmt <> nil) and (FAncestorJitClass.FJitPVmt^.vTypeInfo <> nil))');
  end;

  if (FJitPVmt = nil) then begin
    CreateJitClassVMT;
    // FAncestorClass is set now
  end;

  UpdateClassName;
  // FJitPVmt^.vTypeInfo may have been done (as stub) in a recursive call
  if FJitPVmt^.vTypeInfo = nil then begin
    CreateJitPropsPrepare;
    (* Trigger all recursions (we may be re-enterd / re-entry to GetJitClass)
       If we are re-entered, we go straight to CreateJitClassContinueAfteVMT (directly from GetJitClass)
    *)
    CreateJitClassCallAllProp(True);
  end;

  (* * If we are in a recursion from our anchestor then anchestors.vInstanceSize
       will be zero. CreateJitClassContinueAfteVMT will defer its work
     * If a property class (outside the anchestor recursion) has (re-)entered
        our (Get)JitClass, then CreateJitClassContinueAfteVMT may have finished.
        (if it finished it will have cleared ccfContinueAfterVMTNeeded)
  *)
  if ccfContinueAfterVMTNeeded in FFlags then
    CreateJitClassContinueAfteVMT;
end;

class procedure TJitClassCreator.FreeJitClass(const AJitPVmtMem: TVmtMem);
begin
  if AJitPVmtMem.VmtPtr = nil then
    exit;

  if AJitPVmtMem.VmtPtr^.vTypeInfo <> nil then
    Freemem(AJitPVmtMem.VmtPtr^.vTypeInfo);
  if AJitPVmtMem.VmtPtr^.vInitTable <> nil then
    Freemem(AJitPVmtMem.VmtPtr^.vInitTable);
  if AJitPVmtMem.VmtPtr^.vMethodTable <> nil then
    Freemem(AJitPVmtMem.VmtPtr^.vMethodTable);
  if AJitPVmtMem.VmtPtr^.vClassName <> nil then
    Freemem(AJitPVmtMem.VmtPtr^.vClassName);
  if AJitPVmtMem.VmtPtr^.vParentRef<> nil then
    Freemem(AJitPVmtMem.VmtPtr^.vParentRef);

  AJitPVmtMem.DeAllocate;
end;

procedure TJitClassCreator.UpdateClassName;
begin
  Exclude(FFlags, ccfModifiedClassName);
  if Pointer(FJitPVmt^.vClassName) <> nil then
    Freemem(Pointer(FJitPVmt^.vClassName));

  GetMem(Pointer(FJitPVmt^.vClassName), SizeOf(ShortString));
  FJitPVmt^.vClassName^ := FClassName;
end;

procedure TJitClassCreator.CreateJitMethods;
var
  MemSize, StringMemSize, i: integer;
  RttiWriterMethods: TJitRttiWriterVmtMethodTable;
  DummyAddr: PtrUInt;
begin
  Exclude(FFlags, ccfModifiedMethods);
  if FJitPVmt^.vMethodTable <> nil then begin
    Freemem(FJitPVmt^.vMethodTable);
    FJitPVmt^.vMethodTable := nil;
  end;

  if FJitMethods.Count = 0 then
    exit;

  MemSize := TJitRttiWriterVmtMethodTable.NewSizeFor(FJitMethods.Count);
  StringMemSize := 0;
  for i := 0 to FJitMethods.Count - 1 do
    TJitRttiWriterVmtMethodTable.AddSizeForShortStringPtr(StringMemSize, FJitMethods[i].Name);

  MemSize := aligntoptr(MemSize);
  FJitPVmt^.vMethodTable := AllocMem(MemSize + StringMemSize);
  DummyAddr := PtrUInt(FJitPVmt^.vMethodTable);

  RttiWriterMethods := TJitRttiWriterVmtMethodTable.Create(FJitPVmt^.vMethodTable,
    pointer(FJitPVmt^.vMethodTable) + MemSize, JitMethods.Count);

  for i := 0 to FJitMethods.Count - 1 do
    if FJitMethods[i].FCreateDummyCodePointer then begin
      RttiWriterMethods.WriteMethodEntry(FJitMethods[i].Name, CodePointer(DummyAddr));
      inc(DummyAddr);
    end
    else
      RttiWriterMethods.WriteMethodEntry(FJitMethods[i].Name, FJitMethods[i].CodeAddress);

  assert(RttiWriterMethods.CurDestMemPos <= Pointer(FJitPVmt^.vMethodTable)+MemSize, 'TJitClassCreator.CreateJitMethods: RttiWriterMethods.CurDestMemPos <= Pointer(FJitPVmt^.vMethodTable)+MemSize');
  assert(RttiWriterMethods.CurNamesTargetMem <= Pointer(FJitPVmt^.vMethodTable)+MemSize+StringMemSize, 'TJitClassCreator.CreateJitMethods: RttiWriterMethods.CurNamesTargetMem <= Pointer(FJitPVmt^.vMethodTable)+MemSize+StringMemSize');

  RttiWriterMethods.Free;
end;

procedure TJitClassCreator.CreateJitProps;
begin
  Exclude(FFlags, ccfModifiedProps);
  if not (ccfJitPropsPrepareDone in FFlags) then
    CreateJitPropsPrepare;
  CreateJitPropsFinish;
end;

procedure TJitClassCreator.CreateJitPropsPrepare;
type
  TNameIndexMap = specialize TFPGMap<string, integer>;
var
  PropCount, i, idx: integer;
  PropList: PPropList;
  NameIdxMap: TNameIndexMap;
  Itm: TJitProperty;
  NewTypeInfo: Pointer;
  NewPropInfo: PPropInfo;
  VmtParentMem: TypeInfoPtr;
begin
  assert(not (ccfJitPropsPrepareDone in FFlags), 'TJitClassCreator.CreateJitPropsPrepare: not (ccfJitPropsPrepareDone in FFlags)');
  assert(FRttiWriterClass = nil, 'TJitClassCreator.CreateJitPropsPrepare: FRttiWriterClass = nil');
  assert(FJitPVmt^.vTypeInfo = nil, 'TJitClassCreator.CreateJitPropsPrepare: FJitPVmt^.vTypeInfo = nil');
  (* Prepare TypeInfo but do not yet trigger any recursion
     A child class may need Property count *BEFORE* it can create its own typeinfo.
     Therefore no recursion on the child must be triggered.
  *)
  include(FFlags, ccfJitPropsPrepareDone);
  // Get existing properties (parent class)
  PropCount := GetPropList(FAncestorClass, PropList);
  NameIdxMap := TNameIndexMap.Create;
  for i := 0 to PropCount - 1 do
    NameIdxMap.Add(PropList^[i]^.Name, PropList^[i]^.NameIndex);
  if PropCount > 0 then
    Freemem(PropList);
  NameIdxMap.Sorted := True;

  FTypeInfoMemSize := TJitRttiWriterTypeInfo.NewSizeForClass(FClassName, FClassUnit); // already aligned
  FRedirectPtrMemSize := 0;
  For i := 0 to FJitProperties.Count - 1 do begin
    Itm := FJitProperties.Items[i];
    TJitRttiWriterTkClass.AddSizeForProperty(FTypeInfoMemSize, FRedirectPtrMemSize, Itm.Name);
    if NameIdxMap.Find(Itm.Name, idx) then begin
      Itm.FNameIndex := idx; // override/reintroduce name
    end
    else begin
      Itm.FNameIndex := PropCount; // new name (not in any anchestor)
      inc(PropCount);
    end;
  end;
  NameIdxMap.Free;

  FVmtParentMemSize := SizeOf(Pointer);

  (* vmtTypeInfo = pointer to
        TTypeInfo (Kind, Name)
        TTypeData
        TPropData (PropCount, TPropInfo, TPropInfo, ...) // Size = PropInfoMemSize  *)
  NewTypeInfo := AllocMem(FTypeInfoMemSize
                        + FVmtParentMemSize
                        + FRedirectPtrMemSize);

  FJitPVmt^.vTypeInfo:=NewTypeInfo;
  FJitPVmt^.vInstanceSize  := 0; // not yet ready

  VmtParentMem  := Pointer(NewTypeInfo) + FTypeInfoMemSize;
  VmtParentMem^ :=FAncestorClass.ClassInfo;

  FRttiWriterClass := TJitRttiWriterTkClass.Create(NewTypeInfo,
    FClassName, FClassUnit, TClass(FJitPVmt), VmtParentMem,
    FJitProperties.Count, PropCount);

  try
    (* Pre-init the properties => if we request the typeinfo of an inherited class
       then all the names must be present *)
    NewPropInfo := FRttiWriterClass.FirstPropInfo;
    For i := 0 to FJitProperties.Count - 1 do begin
      Itm := FJitProperties.Items[i];
      NewPropInfo^.GetProc := nil;
      NewPropInfo^.SetProc := nil;
      NewPropInfo^.StoredProc := nil;
      NewPropInfo^.Index := 0;
      NewPropInfo^.Default := 0;
      NewPropInfo^.PropProcs := 0; // all ptField;
      NewPropInfo^.Name := Itm.Name;
      NewPropInfo^.NameIndex := Itm.FNameIndex;
      // Skip typeinfo => must not yet trigger any recursive types
      NewPropInfo := NewPropInfo^.Next;
    end;
    assert(NewPropInfo <= Pointer(Pointer(NewTypeInfo) + FTypeInfoMemSize), 'TJitClassCreator.CreateJitProps: NewPropInfo <= Pointer(Pointer(NewTypeInfo) + FTypeInfoMemSize)');

    Include(FFlags, ccfContinueAfterVMTNeeded);

    (*    ***** FJitPVmt^.vTypeInfo is set for recursion                *****
          ***** From this point on, we can access any Property.TypeInfo *****
          ***** - Child-Classes can get all, except for vInstanceSize   ***** *)
  except
    FRttiWriterClass.Free;
    raise;
  end
end;

procedure TJitClassCreator.CreateJitPropsFinish;
var
  Itm: TJitProperty;
  i, DSize: Integer;
  ManagedFieldCount, ManagedFieldDataSize: Integer;
  InitTableMemSize: Integer;
  NewPropInfo: PPropInfo;
  NewInitTypeInfo: PTypeInfo;
  RedirectPtrMem: Pointer;
//  RttiWriterClass: TJitRttiWriterTkClass;
  RttiWriterInit: TJitRttiWriterRecInitInfo;
  InstanceDataSize: SizeInt;
begin
  assert(FJitPVmt^.vTypeInfo <> nil, 'TJitClassCreator.CreateJitPropsFinish: FJitPVmt^.vTypeInfo <> nil');
  assert(FRttiWriterClass <> nil, 'TJitClassCreator.CreateJitPropsFinish: FRttiWriterClass <> nil');
  assert(ccfJitPropsPrepareDone in FFlags, 'TJitClassCreator.CreateJitPropsFinish: ccfJitPropsPrepareDone in FFlags');

  exclude(FFlags, ccfJitPropsPrepareDone);
  RttiWriterInit := nil;
  RedirectPtrMem  := Pointer(FJitPVmt^.vTypeInfo) + FTypeInfoMemSize + FVmtParentMemSize;
  try
    ManagedFieldCount := 0;
    ManagedFieldDataSize := 0;
    For i := 0 to FJitProperties.Count - 1 do begin
      Itm := FJitProperties.Items[i];
      (* *****  This triggers any recursions that may be ***** *)
      if Itm.TypeInfo.IsManaged then begin
        inc(ManagedFieldCount);
        inc(ManagedFieldDataSize, Itm.TypeInfo.DataSize);
      end;
    end;
    InitTableMemSize := TJitRttiWriterRecInitInfo.NewSizeForInitTable(FClassName, ManagedFieldCount);

  // TODO: all typeinfos have been triggered: get anchestor.vInstanceSize
  // yet, if we have properties.child_class_of_this => then child_class_of_this goes wrong

    InstanceDataSize := FAncestorClass.InstanceSize;

    NewInitTypeInfo := AllocMem(InitTableMemSize);
    FJitPVmt^.vInitTable := NewInitTypeInfo;
    RttiWriterInit := TJitRttiWriterRecInitInfo.Create(NewInitTypeInfo, FClassName, tkClass,
      ManagedFieldCount, ManagedFieldDataSize);

    NewPropInfo := FRttiWriterClass.FirstPropInfo;
    For i := 0 to FJitProperties.Count - 1 do begin
      Itm := FJitProperties.Items[i];
      RefCountedJitPvmt.AddToList(Itm);
      Itm.FInstanceMemOffset := InstanceDataSize;

      NewPropInfo^.GetProc := {%H-}CodePointer(PtrUInt(InstanceDataSize));
      if Itm.WriteAble then
        NewPropInfo^.SetProc := {%H-}CodePointer(PtrUInt(InstanceDataSize));
      NewPropInfo^.PropTypeRef := PTypeInfoToTypeInfoPtr(Itm.TypeInfo, RedirectPtrMem);

      if Itm.NoDefault then
        NewPropInfo^.Default := JIT_PROP_NO_DEFAULT_VAL
      else
        NewPropInfo^.Default := Itm.DefaultVal;

      NewPropInfo^.StoredProc := {%H-}CodePointer(PtrUInt(( Itm.IsStored)));
      NewPropInfo^.PropProcs := (ptConst shl 4); // IsStored

      if Itm.TypeInfo.IsManaged then
        RttiWriterInit.WriteField(NewPropInfo^.PropTypeRef, InstanceDataSize);

      DSize := Itm.TypeInfo.DataSize;
      {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
      InstanceDataSize := Align(InstanceDataSize, DSize);
      {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
      InstanceDataSize := InstanceDataSize + DSize;

      NewPropInfo := NewPropInfo^.Next;
    end;

    FJitPVmt^.vInstanceSize  := InstanceDataSize;
    FJitPVmt^.vInstanceSize2 := -InstanceDataSize;

    assert(NewPropInfo <= Pointer(FJitPVmt^.vTypeInfo) + FTypeInfoMemSize, 'TJitClassCreator.CreateJitProps: NewPropInfo <= Pointer(FJitPVmt^.vTypeInfo) + FTypeInfoMemSize');
    assert(RedirectPtrMem <= Pointer(FJitPVmt^.vTypeInfo) + FTypeInfoMemSize + FVmtParentMemSize + FRedirectPtrMemSize, 'TJitClassCreator.CreateJitProps: RedirectPtrMem <= Pointer(FJitPVmt^.vTypeInfo) + FTypeInfoMemSize + FVmtParentMemSize + FRedirectPtrMemSize');
    assert(RttiWriterInit.CurDestMemPos <= Pointer(NewInitTypeInfo) + InitTableMemSize, 'TJitClassCreator.CreateJitProps: RttiWriterInit.CurDestMemPos <= Pointer(NewInitTypeInfo) + InitTableMemSize');

  finally
    FreeAndNil(FRttiWriterClass);
    FreeAndNil(RttiWriterInit);
  end;
end;

procedure TJitClassCreator.Init;
begin
  //
end;

function TJitClassCreator.CreateJitPropertyList: TJitPropertyList;
begin
  Result := TJitPropertyList.Create(Self);
end;

function TJitClassCreator.GetTypeInfo: PTypeInfo;
begin
  GetJitClass;
  Result := PTypeInfo(FJitPVmt^.vTypeInfo);
end;

constructor TJitClassCreator.Create(AnAncestorClass: TClass;
  AClassName: String; AClassUnit: String; ATypeLibrary: TJitTypeLibrary);
begin
  FJitPVmtMem.ClearMemPointer;
  FJitMethods := TJitMethodList.Create(Self);
  FJitProperties := CreateJitPropertyList;

  inherited Create;

  FAncestorClass := AnAncestorClass;
  FClassName := AClassName;
  FClassUnit := AClassUnit;
  TypeLibrary := ATypeLibrary;
  Init;
end;

constructor TJitClassCreator.Create(AnAncestorClassName, AClassName: String;
  AClassUnit: String; ATypeLibrary: TJitTypeLibrary);
begin
  Create(TClass(nil), AClassName, AClassUnit, ATypeLibrary);
  FAncestorClassName := AnAncestorClassName;
  Init;
end;

constructor TJitClassCreator.Create(AnAncestorJitClass: TJitClassCreator;
  AClassName: String; AClassUnit: String; ATypeLibrary: TJitTypeLibrary);
begin
  Create(TClass(nil), AClassName, AClassUnit, ATypeLibrary);
  FAncestorJitClass := AnAncestorJitClass;
  if FAncestorJitClass <> nil then
    FAncestorJitClass.AddFreeNotification(@DoAnchesterJitClassFreed);
  Init;
end;

constructor TJitClassCreator.Create(AnAncestorJitType: TJitType;
  AClassName: String; AClassUnit: String; ATypeLibrary: TJitTypeLibrary);
begin
  if not (AnAncestorJitType is TJitTypeClassBase) then
    raise Exception.Create('Incorrect type for anchestor');
  Create(TClass(nil), AClassName, AClassUnit, ATypeLibrary);
  FAncestorJitType := TJitTypeClassBase(AnAncestorJitType);
  Init;
end;

destructor TJitClassCreator.Destroy;
begin
  inherited Destroy;
  if FRefCountedJitPVmt <> nil then
    FRefCountedJitPVmt.ReleaseLock
  else
    FreeJitClass(FJitPVmtMem);

  if FTypeLibrary <> nil then
    FTypeLibrary.RemoveFreeNotification(@DoTypeLibFreed);

  FJitMethods.Free;
  FJitProperties.Free;
  if FAncestorJitClass <> nil then
    FAncestorJitClass.RemoveFreeNotification(@DoAnchesterJitClassFreed);
end;

procedure TJitClassCreator.UpdateJitClass;
begin
  if not Assigned(FJitPVmt) then begin
    CreateJitClass;
    exit;
  end;

  if ccfModifiedMethods in FFlags then
    CreateJitMethods;
  if ccfModifiedClassName in FFlags then
    UpdateClassName;
end;

procedure TJitClassCreator.RecreateJitClass;
begin
  FFlags := [];
  DeAllocateJitPVmt;
end;

function TJitClassCreator.FindPropertyRecursive(AName: String): TJitProperty;
var
  Creator: TJitClassCreator;
begin
  Creator := Self;
  while Creator <> nil do begin
    Result := Creator.JitProperties.Prop[AName];
    if Result <> nil then
      exit;
    Creator := Creator.AncestorJitClass;
  end;
end;

end.