[go: up one dir, main page]

Menu

[f77b10]: / zc_ctypes.d  Maximize  Restore  History

Download this file

1533 lines (1316 with data), 40.9 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
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
/* VS4/Zrec - (formerly AVLDB) DataBase library/engine.
SPDX-License-Identifier: GPL-3.0-or-later
Meta-scheme content, C symbol descriptions + types for RECC
(was: sd-defs.h, was: zc-ctypes.h)
This file is part of VS4/zrec/avldb.
Copyright ⓒ 2019,2020,2021,2022,2023
Petr Šilhavý <petr.silhavy@yandex.com>
VS4/Žrec is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
VS4/Žrec is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Žrec. If not, see <http://www.gnu.org/licenses/>.
*/
module zc_ctypes ;
import std.string : fromStringz ;
import std.conv ;
import std.stdio;
import std.format : format;
import libclang ;
import clang_parser : verbose_flag, clang_parse ;
import zc_magic ;
import mm ;
/** Sematics of struct*
===================
struct A
{
...
I. struct B b { ... }; <= HasA B ( dump/load: nested data expected/possible )
II. struct B[] ba [] { ... } ; <= HasMany B (nested data expected)
III.struct C *c ; <= HasA C ( no nested data expected )
IV. struct C *ca[] ; <= HasMany C ( no nested data expected )
V. struct { ... } d ; or <= various kinds of ANON struct
struct _No-file { ... } dn
VI. struct { ... } da[] ;
struct _No-file { ... } dan[] ;
} a ;
I. b is table B's record b + a has b ( b is part of/property of a )
II. ba is array of I.
III.a has c. c is part of/property of a
IV. ca is array of III.
V. d is anonymous structured data (ASD/No-file-struct) A.#/...
VI. da is array of V.
.zc :
{
...
link pets ; // cats,dogs,HW,planes,... <= polymorphic/type-less HasMany
same as unix directory with symlinks:
pets/
Cindy -> ../../dogs/cindy/777
Towser -> ../../dogs/towser/12345
Hermenegilde -> ../../cats/...
int i = pets.qty
link
====
- has no fixed table destination ( if ! fixed destination )
- has qty for each destination
- flags :
- fixed destination
- is BLOB
- IS_PTR -> conv to C
- IS_ARR -> conv to C
struct *clnk, *clarr[] should be replaced with link w/ fixed destination
!!! link field is NOT in *.h !!!!
C struct => disk mapping
========================
IS_POD_STRUCT
-------------
cs.itype & IS_POD_STRUCT => HDR + 1:1
IS_DYN_STRUCT
-------------
cs.itype & IS_DYN_STRUCT
HDR +
field ca attribute ba2 ;
field c attribute ba2 ;
link l attribute ba2 ;
// rest is unsuported :
field c attribute "struct _No-filexyz { int i ; struct A a } ;"
link l attribute "{ unsigned long long ull ; }"
}
struct _NF
{
same as notable/nofile ;
// attribute only/ASD - open table => error ;
// My oath: I'll never use NF as normal data table
...
}
Lenght of field
===============
Helper field is size_t or ***BOOOM***
*/
void * dmemcpy ( void * destination, const void * source, size_t num ) pure nothrow
{
(cast(ubyte*)destination)[0 .. num][]=(cast(const(ubyte)*)source)[0 .. num];
return destination;
}
/**
* compose ( or Integrated TYPE ) for dump/load to/from CSV,JSON
*
* ``everything`` is possible, except void V[42] ;
*/
enum ZCIT
{
UNSET = 0 ,
IS_PTR = 0x1, // lives in pointer's space
IS_LINK = 0x2,
IS_SUB_STRUCT = IS_LINK, /**< sample: struct MUMBLE m ;
- sub STRUCT AKA ``link``
- lives in link parts
*/
IS_LINK_DATA = 0x4, // ????
IS_POD_STRUCT = 0x8, /**
* struct contains fixed length items only
* struct X { char s[14] ; int i ; long l ; double d;}
*/
IS_DYN_STRUCT = 0x10, /**
* struct contains too
* variable len arrays { char *c ; int *i ; ... }
*/
IS_STRUCT_FIELD = 0x20,
IS_ARRAY = 0x40, // has [LEN] : cv.array_length
IS_ANON = 0x80, /**
* No name tag :
* struct { int i ; float f ; }
*/
// pointer to/array of
IS_OF_CHAR = 0x100 , // char*
IS_OF_DIGIT = 0x200 , // int*
IS_DIGIT = 0x400, /**< sample: long ll ;
- char ... long double
- lives is static part
*/
DIGIT = IS_DIGIT,
HAS_LEN = 0x1000, // GString , C++ string , D string
HAS_LEN_OF = 0x2000, /** lenght of this variable is stored in other var :
* C_VAR.length -> C_VAR
* - lenght holding variable type should be 4/8 bytes unisigned
* - size MUST be in BYTES
*/
ANON_STRUCT = IS_ANON, /** struct { int i ; } or struct _NONANE { ... }
* sample: struct _MUMBLE { int m[42] ; }
*
* acceptable types:
* - all POD
* - link as struct *S, struct *S[], struct J[]
*
* lives in POD part
*/
POINTER_TO_CHAR = IS_OF_CHAR | IS_PTR, /**< sample : char *p ;
- variable length
- zero \0 terminated => strlen() works
- lives is pointer part,
- for array of bytes use void * ,
which'll be evaluated as POINTER_TO_DIGIT :P
- CSV encoded as ?
- JSON encoded as ?
*/
POINTER_TO_CHAR_WL = IS_OF_CHAR | IS_PTR | HAS_LEN_OF, /**< sample : char *p ; w/ length_of;
- variable length
- zero \0 terminated => strlen() may work
- lives is pointer part,
- acceptable as array of bytes
- CSV encoded as ?
- JSON encoded as ?
*/
POINTER_TO_CHAR_GS = IS_OF_CHAR | IS_PTR | HAS_LEN,/** glib GString *str/ D/C++ string
sample : GString *p
- variable length in str->len
- zero \0 terminated => strlen() may work
- lives is pointer part,
- acceptable as array of bytes
- CSV encoded as ?
- JSON encoded as ?
_GS version differs from other char * types
only on :
- input from (user's) struct
- output
*/
deprecated POINTER_TO_DIGIT = IS_PTR | IS_OF_DIGIT ,/**< float *ftp;
- variable length
- everything from void * ... int * ... double *
- CSV/JSON encoded : [ VAL0 ... ] ,
- MUST have lenght of
- lives is pointer part,
*/
POINTER_TO_DIGIT_WL = IS_OF_DIGIT | IS_PTR | HAS_LEN_OF,/**< float *ftp;
- variable length
- everything from void * ... int * ... double *
- CSV/JSON encoded : [ VAL0 ... ] ,
- MUST have lenght of
- lives is pointer part,
*/
/+
deprecated PTPT_DIGIT = 0x,/**< float **ftpa;
- variable length
- everything from void * ... int * ... double *
- CSV/JSON encoded : [ VAL0 ... ] ,
- MUST be NULL terminated
- lives is pointer part,
- differs from POINTER_TO_DIGIT - on I/O
- no lenght of required
*/
+/
ARRAY_OF_CHAR = IS_OF_CHAR | IS_ARRAY, /**< sample: char [7];
- fixed length [MAXLEN]
- zero \0 terminated => strlen() works
- has array_length set to [MAXLEN]
- lives is static part
- CSV encoded as ?
- JSON encoded as ?
*/
ARRAY_OF_DIGIT = IS_OF_DIGIT | IS_ARRAY, /**< sample: int[42];
- char ... long double
- fixed length [MAXLEN]
- has array_length set to [MAXLEN]
- lives is static part
- CSV encoded as [ VAL0 ... ]
- JSON encoded as [ VAL0 ... ]
*/
// ARRAY_OF_STRUCT = STRUCT | IS_ARRAY ,
ARRAY_OF_ANON_STRUCT = ANON_STRUCT | IS_ARRAY ,
// POINTER_TO_STRUCT = STRUCT | IS_LINK , /// NO POINTER Is correct here,
/// thanks to silly semantics
/**< sample: struct MUMBLE *m
- sub STRUCT AKA ``link``
- lives in link part
- with length_of ->
non NULL terminated array of pointers
*/
deprecated PTPT_STRUCT, /** Pointer To Pointer To struct AKA array of pointers to struct
sample: struct MUMBLE **m
- MUST be NULL terminated array of pointers
- sub STRUCT AKA ``link`` array
- lives in link part
*/
// FIXME: add GString or similar ``C++`` string
// ZCIT_EOF = -1 ,
LINK = IS_LINK,
LINK_ARRAY = LINK | IS_ARRAY ,
IGNORED = 0x10_0000, /** pseudo type to ignore var */
}
/**
*
* Keyword ``length_of`` and composite types
*
* ======================== ========= ======== ===================================
* IType Mandatory Optional Other
* ======================== ========= ======== ===================================
* ZCIT__POINTER_TO_CHAR N N w/ len => ZCIT__POINTER_TO_CHAR_WL
* ZCIT__POINTER_TO_CHAR_WL ND* N Upgraded from char *, if possible
* ZCIT__POINTER_TO_CHAR_GS N N Redundant - gstring->len
* ZCIT__POINTER_TO_DIGIT ND* N
* ZCIT__PTPT_DIGIT N N
* ZCIT__ARRAY_OF_CHAR N N
* ZCIT__ARRAY_OF_DIGIT N N
* ZCIT__DIGIT N N
* ZCIT__STRUCT N N
* ZCIT__ARRAY_OF_STRUCT N N
* ZCIT__POINTER_TO_STRUCT N* N
* ZCIT__PTPT_STRUCT DN* N
* ======================== ========= ======== ===================================
*/
string itype_to_string(ZCIT tp)
{
string result ;
ZCIT t = tp ;
void append_if(ZCIT arg, string add)
{
if ( t & arg )
{
result ~= ( add ~ " ") ;
t &= ~arg ;
}
}
if ( t == 0 ) return "*** empty ***" ;
append_if( ZCIT.IGNORED, "Ignored" );
append_if( ZCIT.IS_PTR, "Pointer to");
append_if( ZCIT.IS_ARRAY, "Array of" );
append_if( ZCIT.IS_LINK, "Link to");
append_if( ZCIT.HAS_LEN, "C++/D/GString" );
append_if( ZCIT.IS_OF_CHAR, "char" );
append_if( ZCIT.IS_OF_DIGIT, "digit" );
append_if( ZCIT.ANON_STRUCT, "anonymous struct" );
append_if( ZCIT.HAS_LEN_OF, "with lenght of variable" );
stderr.writeln(__FUNCTION__, "Remainder : ", cast(void*)t);
return result ;
}
alias ZC_Itype_to_string = itype_to_string ;
enum ZC_ASC_DESC
{
ASCENDING = 0,
DESCENDING = 1,
}
public import enum_ctypes ;
alias ZC_ctypes_to_string = ctypes_to_string ;
string[C_TYPES.max + 1] ctypes_to_string =
[
C_TYPES.UNDEF : "<*** Undefined ***>",
C_TYPES.BOOL : "bool",
C_TYPES.CHAR : "char", // implementation defined, gcc's default: signed
C_TYPES.SIGNED_CHAR : "signed char",
C_TYPES.UNSIGNED_CHAR : "unsigned char" ,
C_TYPES.SHORT : "short" ,
C_TYPES.UNSIGNED_SHORT : "unsigned short",
C_TYPES.INT : "int" ,
C_TYPES.UNSIGNED_INT : "unsigned int",
C_TYPES.LONG : "long",
C_TYPES.UNSIGNED_LONG : "unsigned long",
C_TYPES.LONG_LONG : "long long",
C_TYPES.UNSIGNED_LONG_LONG : "unsigned long long",
C_TYPES.FLOAT : "float",
C_TYPES.DOUBLE : "double",
C_TYPES.LONG_DOUBLE : "long double",
C_TYPES.INT_128 : "int128",
C_TYPES.UNSIGNED_INT_128 : "unsigned int128",
C_TYPES.VOID : "void",
C_TYPES.STRUCT : "struct",
C_TYPES.END_STRUCT : "<END struct>",
C_TYPES.WCHAR : "wchar_t",
// C_TYPES.EOF : "EOF",
] ;
alias ZC_ctypes_lenght = ctypes_lenght ;
uint[C_TYPES.max + 1] ctypes_lenght =
[
C_TYPES.BOOL : bool.sizeof ,
C_TYPES.CHAR : char.sizeof,
C_TYPES.SIGNED_CHAR : char.sizeof,
C_TYPES.UNSIGNED_CHAR : char.sizeof,
C_TYPES.SHORT : C_SHORT_SIZE, // 2
C_TYPES.UNSIGNED_SHORT : C_SHORT_SIZE, // 2
C_TYPES.INT : C_INT_SIZE , // 4
C_TYPES.UNSIGNED_INT : C_INT_SIZE, // 4
C_TYPES.LONG : C_LONG_SIZE,
C_TYPES.UNSIGNED_LONG : C_LONG_SIZE,
C_TYPES.LONG_LONG : C_LONG_LONG_SIZE,
C_TYPES.UNSIGNED_LONG_LONG : C_LONG_LONG_SIZE,
C_TYPES.FLOAT : C_FLOAT_SIZE ,
C_TYPES.DOUBLE : C_DOUBLE_SIZE,
C_TYPES.LONG_DOUBLE : C_LONG_DOUBLE_SIZE ,
C_TYPES.INT_128 : C_INT128_SIZE,
C_TYPES.UNSIGNED_INT_128 : C_INT128_SIZE,
C_TYPES.VOID : void.sizeof,
C_TYPES.STRUCT : 0,
C_TYPES.END_STRUCT : 0,
C_TYPES.WCHAR : dchar.sizeof,
] ;
class IDX_VAR_ATTR
{
// string name ;
public:
bool dsc ;
int function() cmp_fce ;
string cmp_name ;
C_VAR cv ;
zc_node node , pos_node ;
int pos ; // 0 if 1st index member ...
void[] mem ;
this(C_VAR c, bool d)
{
this.cv = c;
this.dsc = d ;
}
// search version:
this(string s)
{
this.cv = new C_VAR(s);
}
string
name() => this.cv ? this.cv.name.dstr : null ;
// void opDispatch(string name, T...)(T vals) {
// writeln("called ", name);
// mixin("content." ~ name)(vals);
// }
}
extern (C) int
Zc_IDX_VAR_ATTR_cmp(const void *a, const void *b,
const void *c, const void *d)
{
IDX_VAR_ATTR ia = cast(IDX_VAR_ATTR) a ;
IDX_VAR_ATTR ib = cast(IDX_VAR_ATTR) b ;
assert(ib.cv && ia.cv );
return ( ( ia.cv.name.str > ib.cv.name.str ) -
( ia.cv.name.str < ib.cv.name.str ) );
}
extern (C) int
Zc_IDX_VAR_ATTR_pos_cmp(const void *a, const void *b,
const void *c, const void *d)
{
IDX_VAR_ATTR ia = cast(IDX_VAR_ATTR) a ;
IDX_VAR_ATTR ib = cast(IDX_VAR_ATTR) b ;
return ( ( ia.pos > ib.pos ) - ( ia.pos < ib.pos ) );
}
class INDEX
{
uint magic = ZC_MAGIC_INDEX ;
string name ;
zc_node node ;
zc_node node_by_pos ;
int pos ; // 0 == primary etc.
static int cnt ;
///
/// fields by name/position
///
zc_table fields_table , pos_fields_table ;
TAVLTree!IDX_VAR_ATTR fields_by_name, fields_by_pos ;
uint nfields ;
zc_pos idx_root ;
void[] mem ; /// pointer to self
public:
ptrdiff_t index ;
C_STRUCT cs ;
bool is_primary() { if(pos == 0) return true ; return false ; }
Position*
get_start_pos() => &idx_root ;
void
set_start_pos( Position* p, Transaction tnx = null)
{
// bool utnx ;
// tnx = db.start_utnx_if_no_tnx(tnx, utnx);
// tnx.add_rw_addr(db.meta_fd, db.meta);
// when called from ~tnx, starting new tnx is bit silly
if ( tnx ) tnx.add_ch_start_idx(this.index, &this.idx_root, p);
this.idx_root = *p ;
// if (utnx) tnx.commit ;
}
this(string name, C_STRUCT cs) //Database db,)
{
this.name = name ;
this.pos = cnt++ ;
this.index = this.mem.ptr - cs.db.meta.ptr ;
// zc_table_props* p = add_zc_table_props(&Zc_IDX_VAR_ATTR_cmp,
// IDX_VAR_ATTR.node.offsetof) ;
fields_by_name = new TAVLTree!IDX_VAR_ATTR(cs.db.meta_bh,
&this.fields_table,
TTP_OFFSET.TTP_IVA_NAME,
null);
fields_by_pos = new TAVLTree!IDX_VAR_ATTR(cs.db.meta_bh,
&this.pos_fields_table,
TTP_OFFSET.TTP_IVA_POS,
null);
}
// search version
this(string name)
{
this.name = name ;
}
IDX_VAR_ATTR
add_member (C_STRUCT cs, string fld_name, bool dsc, Database db)
{
// C_VAR *cvp = fld_name in cs.vars ;
// if ( cvp is null )
// {
// writefln("Field «%s» non existent in struct «%s»", fld_name, cs.name );
// return ;
// }
if ( auto cvp = /*fld_name in*/ cs.vars.FindA(fld_name) )
{
auto ia = db.meta_bh.AllocE!IDX_VAR_ATTR( cvp, dsc );
ia.pos = this.nfields++ ;
// auto ia = new IDX_VAR_ATTR(cvp, dsc) ;
// ia.cv = cvp ;
// ia.dsc = dsc ;
// this.fields[fld_name] = cvp ;
// this.attrs[fld_name] = new IDX_VAR_ATTR ;
// this.attrs[fld_name].dsc = dsc ;
this.fields_by_name.insert(ia);
this.fields_by_pos.insert(ia);
return ia ;
}
else
{
writefln("Field «%s» non existent in struct «%s»", fld_name, cs.name );
return null ;
}
}
void set_cmp (C_STRUCT cs, string fld_name, string fce)
{
// C_VAR *cvp = fld_name in cs.vars ;
// if ( cvp is null )
// {
// writefln("Field «%s» non existent in struct «%s»", fld_name, cs.name );
// return ;
// }
// else
// this.attrs[fld_name].cmp_name = fce ;
if ( auto ip = // fld_name in
this.fields_by_name.FindA(fld_name) )
ip.cmp_name = fce ;
else
{
writefln("Field «%s» non existent in struct «%s»", fld_name, cs.name );
return ;
}
}
}
/* token */
/+
struct TK
{
char *s ;
int tn ;
}
+/
struct ATTR
{
bool is_pointer, /* is_built_in , */ is_array , is_last ;
size_t alen ; // if is_array
string name ;
int tn ; // token #
}
//enum int ZC_VAR_DIM_MAX = 16 ;
/+
string
fce_to_string(const char *s, CXString function(CXCursor) fce , CXCursor c)
{
// const char *tmp = clang_getCString(
CXString cxs = fce(c);
string rs = fromStringz(cxs);
return rs ;
}
+/
// tools
string
cxs2string(CXString cxs)
{
const char *tmp = clang_getCString(cxs);
string str = ToStr(tmp);
clang_disposeString(cxs);
return str ;
}
class C_VAR
{
zc_string name , cmp ;
int function() cmp_fce;
zc_node node ;
// void *(*copy_fce)(void *, const void *, size_t);
ZCIT itype ; //< composed type
CXType cxt ;
C_TYPES type ; //< all basic types
// pos info (file-wise) :
uint line ;
uint col , foff ;
string file ;
long offset ; // long long in C++/C
//
uint flag ; //< filled by ZC parser
C_VAR length ; //< the same as a[length] for pointers
union {
size_t array_length ; //< the same as a[length]
size_t anon_struct_length ;
}
C_STRUCT cs, parent ; //< nested struct , parent struct
zc_string struct_name ; //< struct tag
// size_t fld_offset ; //< offsetof(a)
ushort id ; //< varible ID
ubyte ptl ; //< number of '*', for pointer to pointer to ... ptl == 2
CXTypeKind cxkind ;
// unsigned short ndim ; //< original dimansions
// const char *tmp ;
void[] mem ; // where I reside
invariant {
if ( cs ) assert(cs.magic == MAGIC_C_STRUCT) ;
}
this(CXCursor c, C_STRUCT cs) {
const char *tmp = clang_getCString(clang_getCursorSpelling(c));
this.name.inp(tmp, cs.db.meta_bh.zs); //= ToStr(tmp);
this.cs = cs ;
this.struct_name = cs.name ;
cs.add_var(this);
immutable long bit_offset = clang_Cursor_getOffsetOfField (c);
if ( bit_offset > 0 ) { this.offset = bit_offset / 8 ; }
this.cxt = clang_getCursorType (c);
this.cxkind = cxt.kind ;
this.CXType_to_ctype(this.cxkind);
debug (on) { writeln(__FUNCTION__, " Newname[", cs.nvar, "]: ", cs.name,
".", name, " @", offset, "---> ", cs); }
}
this(string s)
{
this.name.set(s) ; // not persistent
}
public string
print_type()
{
string o = format("%s%s %s%s",
ZC_ctypes_to_string[this.type],
this.ptl ? "*" : " ",
this.name,
array_length ? format("[%d];", array_length ) : ";" );
return o ;
}
public string
print_itype() => ZC_Itype_to_string(this.itype) ;
public void
cxt_to_type(int t)
{
switch(t)
{
case CXTypeKind.CXType_Char16: this.type = C_TYPES.SHORT ; break ;
case CXTypeKind.CXType_Char32: this.type = C_TYPES.INT ; break ;
case CXTypeKind.CXType_UShort: this.type = C_TYPES.UNSIGNED_SHORT ;break;
case CXTypeKind.CXType_UInt: this.type = C_TYPES.UNSIGNED_INT ; break ;
case CXTypeKind.CXType_ULong: this.type = C_TYPES.UNSIGNED_LONG; break;
case CXTypeKind.CXType_ULongLong: this.type = C_TYPES.UNSIGNED_LONG_LONG;break;
case CXTypeKind.CXType_UInt128:this.type = C_TYPES.UNSIGNED_INT_128 ; break ;
case CXTypeKind.CXType_WChar:this.type = C_TYPES.WCHAR ; break ;
case CXTypeKind.CXType_Short:this.type = C_TYPES.SHORT ; break ;
case CXTypeKind.CXType_Int:this.type = C_TYPES.INT ; break ;
case CXTypeKind.CXType_Long:this.type = C_TYPES.LONG ; break ;
case CXTypeKind.CXType_LongLong: this.type = C_TYPES.LONG_LONG ; break ;
case CXTypeKind.CXType_Int128:this.type = C_TYPES.INT_128 ; break ;
case CXTypeKind.CXType_Float:this.type = C_TYPES.FLOAT ; break ;
case CXTypeKind.CXType_Double:this.type = C_TYPES.DOUBLE ; break ;
case CXTypeKind.CXType_LongDouble:this.type = C_TYPES.LONG_DOUBLE ; break ;
default: break ;
}
}
private void CXType_to_ctype(CXTypeKind k)
{
// with(CXTypeKind.);
if ( this.type != C_TYPES.UNDEF )
return ;
switch ( k )
{
case CXTypeKind.CXType_Invalid:
case CXTypeKind.CXType_Unexposed:
default:
return ;
case CXTypeKind.CXType_Void: this.type = C_TYPES.VOID ; break ;
case CXTypeKind.CXType_Bool: this.type = C_TYPES.BOOL ; break ;
case CXTypeKind.CXType_Char_U: this.type = C_TYPES.UNSIGNED_CHAR; break ;
case CXTypeKind.CXType_UChar: this.type = C_TYPES.UNSIGNED_CHAR ; break ;
case CXTypeKind.CXType_Char16: this.type = C_TYPES.SHORT ; break ;
case CXTypeKind.CXType_Char32: this.type = C_TYPES.INT ; break ;
case CXTypeKind.CXType_UShort: this.type = C_TYPES.UNSIGNED_SHORT ; break ;
case CXTypeKind.CXType_UInt: this.type = C_TYPES.UNSIGNED_INT ; break ;
case CXTypeKind.CXType_ULong: this.type = C_TYPES.UNSIGNED_LONG; break ;
case CXTypeKind.CXType_ULongLong: this.type = C_TYPES.UNSIGNED_LONG_LONG;break;
case CXTypeKind.CXType_UInt128: this.type = C_TYPES.UNSIGNED_INT_128 ; break ;
// static if ( 1 ) // FIXME ZC__CHAR_IS_SIGNED == 1
// {
case CXTypeKind.CXType_Char_S: this.type = C_TYPES.CHAR ; break ;
case CXTypeKind.CXType_SChar: this.type = C_TYPES.CHAR ; break ;
// }
// else // ZC__CHAR_IS_SIGNED
// {
// [CXType_Char_S] this.type = C_TYPES_SIGNED_CHAR,
// [CXType_SChar] this.type = C_TYPES_SIGNED_CHAR,
// }
case CXTypeKind.CXType_WChar: this.type = C_TYPES.WCHAR ; break ;
case CXTypeKind.CXType_Short: this.type = C_TYPES.SHORT ; break ;
case CXTypeKind.CXType_Int: this.type = C_TYPES.INT ; break ;
case CXTypeKind.CXType_Long: this.type = C_TYPES.LONG ; break ;
case CXTypeKind.CXType_LongLong: this.type = C_TYPES.LONG_LONG ; break ;
case CXTypeKind.CXType_Int128: this.type = C_TYPES.INT_128 ; break ;
case CXTypeKind.CXType_Float: this.type = C_TYPES.FLOAT ; break ;
case CXTypeKind.CXType_Double: this.type = C_TYPES.DOUBLE ; break ;
case CXTypeKind.CXType_LongDouble: this.type = C_TYPES.LONG_DOUBLE ; break ;
case CXTypeKind.CXType_Enum: this.type = C_TYPES.INT ; break ;
case CXTypeKind.CXType_Elaborated: this.type = C_TYPES.STRUCT ; break ;
case CXTypeKind.CXType_IncompleteArray: this.type = C_TYPES.STRUCT ; break ;// struct BLA ble[] ;
case CXTypeKind.CXType_Record: this.type = C_TYPES.STRUCT ; break ;
}
}
size_t
type_size() => ctypes_lenght[this.type] ;
size_t
get_len(ubyte[] content)
{
uint sz = ZC_ctypes_lenght[length.type] ;
assert(sz == size_t.sizeof, "len_of variable MUST be unsigned size_t, not size :" ~ sz.stringof );
ubyte[] bytes = content[length.offset..length.offset + sz] ;
return *cast(size_t*)&bytes[0];
}
} // C_VAR
enum TRIGGER_TYPE
{
_BEFORE_SHUTDOWN = 0,
_BEFORE_META_CHANGE = 1,
_BEFORE_READ = 2,
_BEFORE_WRITE = 3,
_BEFORE_DELETE = 4,
_AFTER_STARTUP = 5,
_AFTER_META_CHANGE = 6,
_AFTER_ERROR = 7,
_AFTER_READ = 8,
_AFTER_WRITE = 9,
_AFTER_DELETE = 10,
_LAST = 11,
}
struct TRIGGER_TABLE
{
union {
char *name ;
// TODO: trigger args !!!
// int (*file_trigger)(struct C_STRUCT *, void *);
// int (*global_trigger)(void *, void *);
}
}
string
itype_to_str(ZCIT itype)
{
return ZC_Itype_to_string(itype) ;
}
alias Zc_itype_to_str = itype_to_str ;
string
type_to_str(C_TYPES type)
{
return ZC_ctypes_to_string[type] ;
}
alias Zc_type_to_str = type_to_str ;
TRIGGER_TABLE[TRIGGER_TYPE._LAST] trigger_table ;
// FIXME import zc.magic ;
enum MAGIC_C_STRUCT = 0xbeef0100 ;
enum MAGIC_INDEX = 0xbeef0101 ;
alias C_STRUCT_diff = ptrdiff_t ;
import std.outbuffer ;
class C_STRUCT
{
uint magic = MAGIC_C_STRUCT ;
zc_node node ;
ZCIT itype ;
// void *shlib ; //< dlopen handle
zc_string name ;
// ZC_STRUCT_FLAG flag ;
// GSList *var_list ; // C_VAR
uint nvar ;
/// "C" struct members/variables tree
zc_table vars_table ;
TAVLTree!C_VAR vars ;
//TAVLTree!C_VAR vars_table ; // WTF ??
// C_VAR[] variables ;
/// positons in source file:
uint line ;
uint col ;
uint foff ; //< file offset
zc_string file ;
///
/// INDEX tree of struct
///
uint nidx ;
zc_table index_table , index_table_by_pos;
TAVLTree!INDEX idxs ;
TAVLTree!INDEX idxs_by_pos ;
//
/// parent of nested struct
// C_STRUCT_diff
//PIDP!C_STRUCT parent ; //< references
C_STRUCT parent ;
/// not yet/never? implemented
zc_string locale ;
int block_size , max_blocks , auto_replace /* ,index_copy, huge_index_block, huge_interval_block */;
//
size_t static_size ;
// string[] prefixes ; /**< file's volumes will be created under db prefixes */
// int n_prefixes ; //, next_prefix ;
Database db ;
/// where the data are well hidden
zc_pos data_root ;
/// Optional multi-volume file support
///
/// If table/C struct/file occupy default volume only,
/// num_dedicated_volumes is zero and vols tree is empty.
/// For multi-volume table/C struct/file:
/// num_dedicated_volumes == vols.size
/// and tree vols (VOLN.num(s)) contains all volume# ,
/// where this file will spread.
ushort num_dedicated_volumes ; // #of
zc_table vols_table ;
TAVLTreeUINT!(VOLN*) vols ;
ptrdiff_t c_struct ; // my offset
void[] mem ; // self ptr
// ulong[ VOL_BITMAP_SIZE ] volumes_bitmap ; // makes ^^^ prefixes obsolent
//
//
public:
union XSTR
{
void *v ; // C struct
ubyte[] src ;
}
ubyte[]
to_bytes(ubyte[] src, out ulong nvar, out size_t[ulong] sztab)
{
OutBuffer buf = new OutBuffer();
size_t[ulong] sv ;
foreach ( i,cv ; vars )
{
size_t s ;
with (ZCIT)
if ( cv.itype & IS_PTR )
{
POINTER_ON_DISK p ;
p.bytes = src[cv.offset..cv.offset + (ubyte*).sizeof] ; //cv.type_size] ;
s = cv.get_len(src) ;
// ubyte[] add = new ubyte[s] ;
// add = (*p.bp)[s] ;
// ubyte[] add ;
if ( cv.itype & IS_OF_DIGIT )
s *= cv.type_size ;
ubyte[] add = new ubyte[s] ;
dmemcpy(&add[0] , p.bp , s ) ;
buf.write(add);
// foreach ( j ; 0..s )
// buf.write((*p.bp+j));
// cv.length
}
else if ( cv.itype & IS_ARRAY )
{
s = cv.array_length * cv.type_size ;
ubyte[] add = new ubyte[s] ;
add[0..s] = src[cv.offset..cv.offset + s] ;
buf.write(add);
}
else if ( cv.itype & IS_DIGIT )
{
}
sv[i] = s ;
nvar = i ;
}
sztab = sv ;
return buf.toBytes ;
}
Position*
get_start_pos() => &data_root ;
void
set_start_pos(Position* p, Transaction tnx = null)
{
// bool utnx ;
// tnx = db.start_utnx_if_no_tnx(tnx, utnx);
// tnx.add_rw_addr(db.meta_fd, db.meta);
tnx.add_ch_start_cs(this.c_struct, &this.data_root, p);
this.data_root = *p ;
// if (utnx) tnx.commit ;
}
int
add_var(C_VAR cv)
{
if ( auto cvp = // cv.name in
this.vars.FindA(cv.name) )
{
writeln("C_VAR ", cv.name, " exists in ", this.name,
" as ", cvp.name, " ", cvp.type);
return 0 ;
}
else // if ( cvp is null )
{
this.vars.assert_insert( cv );
return ++this.nvar ;
}
}
INDEX
add_idx(string name, C_STRUCT cs ) // Database db)
{
INDEX ip = // name in
this.idxs.FindA(name) ;
if ( ip is null )
{
// INDEX idx = new INDEX(name, db) ;
INDEX idx = cs.db.meta_bh.AllocE!INDEX( name, cs);
this.idxs.assert_insert(idx); // [name] = new INDEX(name) ;
this.nidx++ ;
return idx ;
}
return ip ;
}
private void
init_cs(CXCursor c, string sn)
{
CXSourceLocation loc = clang_getCursorLocation(c);
// C_STRUCT *cs = new C_STRUCT ;
CXFile filename;
clang_getExpansionLocation(loc, &filename, &this.line, &this.col, &this.foff);
CXString src = clang_getFileName(filename);
this.magic = MAGIC_C_STRUCT ;
this.name.inp(sn, db.meta_bh.zs ); // = db.meta_bh.AllocE!string(sn) ;
// this.file = ToStr( clang_getCString(src)).dup ;
this.file.inp( ToStr( clang_getCString(src)),
db.meta_bh.zs); // = db.meta_bh.AllocE!string(ToStr( clang_getCString(src)));
clang_disposeString(src);
}
private void append_cs(Database db)
{
// c_struct_aa[this.name] = this ;
while ( db.files.insert(this) == false )
db.files.remove(this);
debug { writeln("C_STRUCT ", this.name ," len now : ", db.files.size ); }
}
private void visit_children()
{
}
/**
*
* Params:
* vn = name C variable ( pointer type )
* ln = name of C variable where the size of "vn" variable is stored
*
* IOW "ln" is length of "vn"
*/
extern (C) int yycolumn, yylineno ;
extern (C) char *zc_file ;
void
set_length_is_in(string vn, string ln)
{
auto vn_cv = this.vars.FindA(vn) ;
if ( vn_cv is null ) {
// writeln(zc_file, ":", yylineno, ":" , yycolumn, ": Non existent C_VAR ", vn);
writeln(eprefix, ": Non existent C_VAR ", vn);
return ;
}
auto ln_cv = this.vars.FindA(ln) ;
if ( ln_cv is null ) {
writeln(eprefix, "Non existent C_VAR ", vn);
}
if ( vn_cv.itype & ZCIT.HAS_LEN_OF )
{
writeln(eprefix, "Variable's ", vn, " «is lenght of» helper variable exists : ",
vn_cv.length.name);
writeln(eprefix, "Setting ", ln, " as Nth ", vn, "'s «is lenght of» ignored. ");
return ;
}
if ( ln_cv.itype != ZCIT.DIGIT )
{
writeln(eprefix, "Only variable type ulong/size_t/uint is usable as 'lenght of' helper variable.");
writeln(eprefix, "Not ", itype_to_string(ln_cv.itype), " ", ln);
return ;
}
switch ( ln_cv.type )
{
case C_TYPES.UNSIGNED_INT,C_TYPES.UNSIGNED_LONG, C_TYPES.UNSIGNED_LONG_LONG:
break ;
default:
writeln(eprefix, "Only 4/8 bytes variable type ulong/size_t/uint is usable as 'lenght of' helper variable.");
writeln(eprefix, "Not a «", ctypes_to_string[ln_cv.type], " ", ln, "»");
return ;
}
vn_cv.itype |= ZCIT.HAS_LEN_OF ;
vn_cv.length = ln_cv ;
}
this(CXCursor c, string sn, Database db)
{
this.db = db ;
this.c_struct = this.mem.ptr - db.meta.ptr ;
// this.zcstr = db.meta_bh.zcstr ;
zc_table_props* v = add_zc_table_props(&C_VAR_cmp,
C_VAR.node.offsetof) ;
zc_table_props* i = add_zc_table_props(&INDEX_cmp,
INDEX.node.offsetof) ;
zc_table_props* ip = add_zc_table_props(&INDEX_pos_cmp,
INDEX.node_by_pos.offsetof) ;
this.vars = new TAVLTree!C_VAR(db.meta_bh, &this.vars_table, v, null);
this.idxs = new TAVLTree!INDEX(db.meta_bh, &this.index_table, i, null);
this.idxs_by_pos = new TAVLTree!INDEX(db.meta_bh, &this.index_table_by_pos, ip, null);
this.init_cs(c, sn);
this.append_cs(db);
}
//for searching only !!!
this(string sn)
{
this.magic = MAGIC_C_STRUCT ;
this.name.set(sn) ;
}
// this(CXCursor c, C_VAR[string] va, string sn)
// {
// this.init(c, sn);
// this.copy_cvar(va);
// this.append_cs();
// }
}
enum D_TYPES
{
UNDEF = 0,
BOOL,
BYTE,
UBYTE,
CHAR,
DCHAR,
SHORT,
USHORT,
INT,
UINT,
LONG,
ULONG,
FLOAT,
DOUBLE,
REAL,
// non numeric:
VOID,
STRUCT,
STRING,
} ;
D_TYPES
ctype_to_dtype(C_TYPES ct)
{
switch (ct)
{
case C_TYPES.UNDEF: return D_TYPES.UNDEF ;
case C_TYPES.BOOL : return D_TYPES.BOOL ;
case C_TYPES.CHAR : return D_TYPES.CHAR ;
case C_TYPES.SIGNED_CHAR: return D_TYPES.CHAR ;
case C_TYPES.UNSIGNED_CHAR: return D_TYPES.UBYTE ;
case C_TYPES.INT : return D_TYPES.INT ;
case C_TYPES.UNSIGNED_INT : return D_TYPES.UINT ;
case C_TYPES.LONG : return D_TYPES.INT ;
case C_TYPES.UNSIGNED_LONG : return D_TYPES.UINT ;
case C_TYPES.LONG_LONG : return D_TYPES.LONG ;
case C_TYPES.UNSIGNED_LONG_LONG: return D_TYPES.ULONG ;
case C_TYPES.FLOAT : return D_TYPES.FLOAT ;
case C_TYPES.DOUBLE : return D_TYPES.DOUBLE ;
case C_TYPES.LONG_DOUBLE: return D_TYPES.REAL ;
case C_TYPES.INT_128 : return D_TYPES.UNDEF ;
case C_TYPES.UNSIGNED_INT_128 : return D_TYPES.UNDEF ;
case C_TYPES.VOID : return D_TYPES.VOID ;
case C_TYPES.STRUCT : return D_TYPES.STRUCT ;
case C_TYPES.WCHAR : return D_TYPES.DCHAR ;
default: return D_TYPES.UNDEF ;
}
}
/*
D: C 32 64
void void
byte signed char
ubyte unsigned char
char char (chars are unsigned in D)
wchar wchar_t (when sizeof(wchar_t) is 2)
dchar wchar_t (when sizeof(wchar_t) is 4)
short short
ushort unsigned short
int int
uint unsigned
core.stdc.config.c_long long long
core.stdc.config.c_ulong unsigned long unsigned long
core.stdc.stdint.intptr_t intptr_t intptr_t
core.stdc.stdint.uintptr_t uintptr_t uintptr_t
long long long long (or long long)
ulong unsigned long long unsigned long (or unsigned long long)
float float
double double
real long double
*/
/**
* Internal record
*/
class IVAR
{
private:
C_VAR cv ;
D_TYPES dtype ;
union VAL
{
byte byte_v ;
ubyte ubyte_v ;
char char_v ;
wchar wchar_v ;
dchar dchar_v ;
short short_v ;
ushort ushort_v ;
int int_v ;
uint uint_v ;
long long_v ;
ulong ulong_v ;
float float_v ;
double double_v ;
real real_v ;
string string_v ;
}
VAL val ;
void
copy_digit(void* cdata)
{
void* src = cdata + ( cv ? cv.offset : 0 ) ;
void* dst = &val ;
size_t s = -1 ;
with(D_TYPES)
switch (dtype)
{
case BOOL, BYTE, UBYTE, CHAR: s = char.sizeof ; break ;
case DCHAR: s = dchar.sizeof ; break ;
case SHORT, USHORT: s = short.sizeof ; break ;
case INT, UINT: s = int.sizeof ; break ;
case LONG, ULONG : s = long.sizeof ; break ;
case FLOAT: s = float.sizeof ; break ;
case DOUBLE: s = double.sizeof; break ;
case REAL: s = real.sizeof ; break ;
default: assert( 0 == 1, "Fixme dtype " ~ dtype.stringof );
}
dmemcpy(dst, src, s );
}
unittest {
int i = 123456789 ;
IVAR iv = new IVAR(null, &i);
iv.cv = null ;
iv.copy_digit(&i);
assert( i == iv.val.int_v );
}
this(C_VAR cv, void* cdata)
{
this.cv = cv ;
if ( cv && cv.itype & ZCIT.IS_DIGIT )
{
dtype = ctype_to_dtype(cv.type);
copy_digit(cdata);
}
else assert( 0 == 1, "Fixme itype : " ~ cv.itype.stringof );
}
}
class IREC
{
/* from C struct */
this(C_STRUCT cs, void* C_data)
{
}
}
extern (C) int
C_VAR_cmp(const void *a, const void *b,
const void *c, const void *d)
{
C_VAR cva = cast(C_VAR) a ;
C_VAR cvb = cast(C_VAR) b ;
return ( ( cva.name > cvb.name ) - ( cva.name < cvb.name ) );
}
extern (C) int
INDEX_cmp(const void *a, const void *b,
const void *c, const void *d)
{
INDEX cva = cast(INDEX) a ;
INDEX cvb = cast(INDEX) b ;
return ( ( cva.name > cvb.name ) - ( cva.name < cvb.name ) );
}
extern (C) int
INDEX_pos_cmp(const void *a, const void *b,
const void *c, const void *d)
{
INDEX cva = cast(INDEX) a ;
INDEX cvb = cast(INDEX) b ;
return ( ( cva.pos > cvb.pos ) - ( cva.pos < cvb.pos ) );
}
extern (C) int
VOLN_cmp(const void *a, const void *b,
const void *c, const void *d)
{
VOLN* cva = cast(VOLN*) a ;
VOLN* cvb = cast(VOLN*) b ;
return ( ( cva.num > cvb.num ) - ( cva.num < cvb.num ) );
}
extern (C) int
Zc_file_cmp(const void *a, const void *b ,
const void *c, const void *d)
{
/+
const zc_meta_file_T *file_a = a ;
const zc_meta_file_T *file_b = b ;
+/
const C_STRUCT file_a = cast(C_STRUCT)a ;
const C_STRUCT file_b = cast(C_STRUCT)b ;
return ( ( file_a.name > file_b.name ) -
( file_a.name < file_b.name ) );
// return g_strcmp0(file_a->name, file_b->name) ;
}
//__gshared C_STRUCT[string] c_struct_aa; /// Associative array of Cstructs that are indexed by string keys.
//alias tk_T = TK ;
/+
struct USERDEF
{
string file ;
string compile_opt ;
string link_opt ;
}
struct zrec_position
{
long offset ;
uint size ; // for blobs // mmap
uint vol_num ;
}
struct ZREC_LINK
{
zrec_position pos ;
ulong id ;
}
+/
//typedef struct ZREC_LINK zrec_link ;
alias off_t = long ;
//alias zrec_link = ZREC_LINK ;
enum ZC_REC_PTR_BITS = 8 ;
enum ZC_REC_LINK_BITS = 8;
/+
struct ZC_REC_HEADER
{
ushort magic ; //< use continuation or record data
union {
char[14] stuff ;
zc_pos cont ; /**< continuation inode parody */
struct {
uint xlen , pointers_offset ;
/** # pointer variables => pointer table size */
ushort has_pointers; //:ZC_REC_PTR_BITS ;
/** are we pointing to somebody else ?
if non-zero, number of ``database pointers``
*/
ushort has_links;//:ZC_REC_LINK_BITS ;
long back_links_offset ; /**< BLOB like back-link pool of TODO!!!
type */
} ;
} ;
} /* __attribute__ ((__packed__)) */ ;
+/
static if ( (int *).sizeof == 2 * int.sizeof )
mixin("alias ZC_PTR_UNION_TYPE = uint ;");
static if ( (int *).sizeof == 2 * short.sizeof )
mixin("alias ZC_PTR_UNION_TYPE = ushort ;");
static assert(ZC_PTR_UNION_TYPE.sizeof > 0, "This HW is too strange, fix this manually! Thank you.");
union ZC_PTR_2SIZE
{
void *p ;
struct
{
ZC_PTR_UNION_TYPE offset ;
ZC_PTR_UNION_TYPE nbytes ;
}
}
static assert( (int *).sizeof == (ZC_PTR_2SIZE).sizeof ,
"Union ZC_PTR_2SIZE MUST be the same size as pointer");
//typedef union ZC_PTR_2SIZE ZC_ptu_T ;
struct OBS___ZC_REC_HDR
{
ushort header_size ; /**< where is the rest of former hidden ,
pointer_part_offset ==
sizeof(REST_OF_STRUCT)
+ sizeof ( struct ZC_REC_HDR ) */
uint struct_size ; // :21 ;
ubyte link_table_len ; /**< are we pointing to somebody else ?
if yes, number of ``database pointers``
*/
uint linked_by_len ; /**< are we destination of ``database pointers`` ?
if yes, size of ``backward links table`` */
}
struct OBS_ZC_REC_PTR_TABLE
{
// unsigned char ptr_no ; /**< Nth pointer variable */
/** NULL pointers are encoded as .len = 0 . .offset = 0 */
uint offset; //:21 ; /**< offset of variable from heeder/record start */
uint len; //:21 ; /**< length of pointer variable's space [bytes] */
}
//typedef struct ZC_REC_PTR_TABLE ZC_PT_T ;
// alias ZC_PT_T = OBS_ZC_REC_PTR_TABLE ;
struct ZC_REC_LINK_HEADER
{
ushort no ; /**< Nth link variable, some may be NULL */
ushort chlink_idx ; /**< index to link[] part */
uint id_idx; //:ZC_REC_LINK_BITS ; /**< nth item in file's sub_file tree */
}
/// dump c_struct_aa{}
export void
list(Database gdb)
{
writeln("Keys: ", gdb.files.keys);
writeln("Values: ", gdb.files.values);
foreach(cs_k, cs_v ; gdb.files )
{
// writefln("C struct %s => %x", cs_k, &cs_v );
writeln("C struct ", cs_k, " => " , cs_v.name );
foreach ( k, cv ; cs_v.vars )
writeln("\tVar: [", k, "] ", cv.name, " | ", cv.print_type , " ", cv.print_itype);
// writeln("\tVar: ", k, " type ", ZC_ctypes_to_string[cv.type] , " ", cv.name);
if ( cs_v.nidx )
{
writeln("Idx «", cs_v.name, "»'s : ", cs_v.nidx, " K/V ", cs_v.idxs.keys,
" / ", cs_v.idxs.values);
// writeln("Idx Values «", cs_k, "» : ", cs_v.idxs.values);
foreach ( ik, iv ; cs_v.idxs )
{
writeln("\tCS ", cs_v.name," Index: ", iv.name, " #fields: ", iv.fields_by_pos.size);
foreach ( ia_k, ia_v ; iv.fields_by_pos )
{
writeln("\t\tField: ", ia_v.cv.name,
ia_v.dsc ? " DSC " : " ASC ",
ia_v.cmp_name ? ia_v.cmp_name : "" ,
ia_v.cmp_name ? "()" : "" );
}
// foreach ( i ; cs_v.idxs.values)
// {
// writeln("Index: ", i.name); //, " type ", v.type );
// foreach ( k, v ; i.fields )
// {
// writeln("IdxVar: ", k, " type ", v.cv.type );
// }
// }
}
}
}
}
/** (FORWARD)_LINK_TABLE is the struct zc_position */
// copy from zc_meta_file.h
//#include <zc_table.h>
// struct ZC_REC_BACK_LINK_TABLE
// {
// FILE_ID_TYPE id ;
// struct zc_position pos ;
// } ;
// struct ZC_PARSER_DATA
// {
// string infile ;
// // immutable
// extern (C) char * cinfile;
// void *voidp ;
// C_STRUCT *cs ;
// } ;
//alias PD = ZC_PARSER_DATA ;
/* Local Variables: */
/* mode: d */
/* buffer-save-without-query: t */
/* End: */