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
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
|
/* -----------------------------------------------------------------------------
* This file is part of SWIG, which is licensed as a whole under version 3
* (or any later version) of the GNU General Public License. Some additional
* terms also apply to certain portions of SWIG. The full details of the SWIG
* license and copyrights can be found in the LICENSE and COPYRIGHT files
* included with the SWIG source code as distributed by the SWIG developers
* and at https://www.swig.org/legal.html.
*
* naming.c
*
* Functions for generating various kinds of names during code generation.
*
* Swig_name_register is used to register a format string for generating names.
* The format string makes use of the following format specifiers:
*
* %c - class name is substituted
* %f - function name is substituted
* %m - member name is substituted
* %n - namespace is substituted
* %v - variable name is substituted
* ----------------------------------------------------------------------------- */
#include "swig.h"
#include "cparse.h"
#include <ctype.h>
/* Hash table containing naming data */
static Hash *naming_hash = 0;
#if 0
#define SWIG_DEBUG
#endif
/* -----------------------------------------------------------------------------
* Swig_name_register()
*
* Register a new naming format.
* ----------------------------------------------------------------------------- */
void Swig_name_register(const_String_or_char_ptr method, const_String_or_char_ptr format) {
if (!naming_hash)
naming_hash = NewHash();
Setattr(naming_hash, method, format);
}
void Swig_name_unregister(const_String_or_char_ptr method) {
if (naming_hash) {
Delattr(naming_hash, method);
}
}
/* Return naming format for the specified method or the default format if none was explicitly registered */
static String* get_naming_format_for(const char *method, const char *def_format) {
String* f = naming_hash ? Getattr(naming_hash, method) : NULL;
return f ? Copy(f) : NewString(def_format);
}
static int name_mangle(String *r) {
char *c;
int special;
special = 0;
Replaceall(r, "::", "_");
c = Char(r);
while (*c) {
if (!isalnum((int) *c) && (*c != '_')) {
special = 1;
switch (*c) {
case '+':
*c = 'a';
break;
case '-':
*c = 's';
break;
case '*':
*c = 'm';
break;
case '/':
*c = 'd';
break;
case '<':
*c = 'l';
break;
case '>':
*c = 'g';
break;
case '=':
*c = 'e';
break;
case ',':
*c = 'c';
break;
case '(':
*c = 'p';
break;
case ')':
*c = 'P';
break;
case '[':
*c = 'b';
break;
case ']':
*c = 'B';
break;
case '^':
*c = 'x';
break;
case '&':
*c = 'A';
break;
case '|':
*c = 'o';
break;
case '~':
*c = 'n';
break;
case '!':
*c = 'N';
break;
case '%':
*c = 'M';
break;
case '.':
*c = 'f';
break;
case '?':
*c = 'q';
break;
default:
*c = '_';
break;
}
}
c++;
}
if (special)
Append(r, "___");
return special;
}
/* -----------------------------------------------------------------------------
* replace_nspace()
*
* Mangles in the namespace from nspace by replacing %n in name if nspace feature required.
* ----------------------------------------------------------------------------- */
static void replace_nspace(String *name, const_String_or_char_ptr nspace) {
if (nspace) {
String *namspace = NewStringf("%s_", nspace);
Replaceall(namspace, NSPACE_SEPARATOR, "_");
Replace(name, "%n", namspace, DOH_REPLACE_ANY);
Delete(namspace);
} else {
Replace(name, "%n", "", DOH_REPLACE_ANY);
}
}
/* -----------------------------------------------------------------------------
* Swig_name_mangle()
*
* Converts all of the non-identifier characters of a string to underscores.
* ----------------------------------------------------------------------------- */
String *Swig_name_mangle(const_String_or_char_ptr s) {
#if 0
String *r = NewString(s);
name_mangle(r);
return r;
#else
return Swig_string_mangle(s);
#endif
}
/* -----------------------------------------------------------------------------
* Swig_name_wrapper()
*
* Returns the name of a wrapper function.
* ----------------------------------------------------------------------------- */
String *Swig_name_wrapper(const_String_or_char_ptr fname) {
String *r = get_naming_format_for("wrapper", "_wrap_%f");
Replace(r, "%f", fname, DOH_REPLACE_ANY);
name_mangle(r);
return r;
}
/* -----------------------------------------------------------------------------
* Swig_name_member()
*
* Returns the name of a class method.
* ----------------------------------------------------------------------------- */
String *Swig_name_member(const_String_or_char_ptr nspace, const_String_or_char_ptr classname, const_String_or_char_ptr membername) {
String *r;
String *rclassname;
char *cname;
rclassname = SwigType_namestr(classname);
r = get_naming_format_for("member", "%n%c_%m");
cname = Char(rclassname);
if ((strncmp(cname, "struct ", 7) == 0) || ((strncmp(cname, "class ", 6) == 0)) || ((strncmp(cname, "union ", 6) == 0))) {
cname = strchr(cname, ' ') + 1;
}
replace_nspace(r, nspace);
Replace(r, "%c", cname, DOH_REPLACE_ANY);
Replace(r, "%m", membername, DOH_REPLACE_ANY);
/* name_mangle(r); */
Delete(rclassname);
return r;
}
/* -----------------------------------------------------------------------------
* Swig_name_get()
*
* Returns the name of the accessor function used to get a variable.
* ----------------------------------------------------------------------------- */
String *Swig_name_get(const_String_or_char_ptr nspace, const_String_or_char_ptr vname) {
String *r = get_naming_format_for("get", "%n%v_get");
#ifdef SWIG_DEBUG
Printf(stdout, "Swig_name_get: '%s'\n", vname);
#endif
replace_nspace(r, nspace);
Replace(r, "%v", vname, DOH_REPLACE_ANY);
/* name_mangle(r); */
return r;
}
/* -----------------------------------------------------------------------------
* Swig_name_set()
*
* Returns the name of the accessor function used to set a variable.
* ----------------------------------------------------------------------------- */
String *Swig_name_set(const_String_or_char_ptr nspace, const_String_or_char_ptr vname) {
String *r = get_naming_format_for("set", "%n%v_set");
replace_nspace(r, nspace);
Replace(r, "%v", vname, DOH_REPLACE_ANY);
/* name_mangle(r); */
return r;
}
/* Common implementation of all Swig_name_<special-method>() functions below. */
static String *make_full_name_for(const char *method, const char *def_format, const_String_or_char_ptr nspace, const_String_or_char_ptr classname) {
String *r;
String *rclassname;
char *cname;
rclassname = SwigType_namestr(classname);
r = get_naming_format_for(method, def_format);
cname = Char(rclassname);
if ((strncmp(cname, "struct ", 7) == 0) || ((strncmp(cname, "class ", 6) == 0)) || ((strncmp(cname, "union ", 6) == 0))) {
cname = strchr(cname, ' ') + 1;
}
replace_nspace(r, nspace);
Replace(r, "%c", cname, DOH_REPLACE_ANY);
Delete(rclassname);
return r;
}
/* -----------------------------------------------------------------------------
* Swig_name_construct()
*
* Returns the name of the accessor function used to create an object.
* ----------------------------------------------------------------------------- */
String *Swig_name_construct(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) {
return make_full_name_for("construct", "new_%n%c", nspace, classname);
}
/* -----------------------------------------------------------------------------
* Swig_name_copyconstructor()
*
* Returns the name of the accessor function used to copy an object.
* ----------------------------------------------------------------------------- */
String *Swig_name_copyconstructor(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) {
return make_full_name_for("copy", "copy_%n%c", nspace, classname);
}
/* -----------------------------------------------------------------------------
* Swig_name_destroy()
*
* Returns the name of the accessor function used to destroy an object.
* ----------------------------------------------------------------------------- */
String *Swig_name_destroy(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) {
return make_full_name_for("destroy", "delete_%n%c", nspace, classname);
}
/* -----------------------------------------------------------------------------
* Swig_name_disown()
*
* Returns the name of the accessor function used to disown an object.
* ----------------------------------------------------------------------------- */
String *Swig_name_disown(const_String_or_char_ptr nspace, const_String_or_char_ptr classname) {
return make_full_name_for("disown", "disown_%n%c", nspace, classname);
}
/* -----------------------------------------------------------------------------
* Swig_name_object_set()
*
* Sets an object associated with a name and optional declarators.
* ----------------------------------------------------------------------------- */
void Swig_name_object_set(Hash *namehash, String *name, SwigType *decl, DOH *object) {
DOH *n;
#ifdef SWIG_DEBUG
Printf(stdout, "Swig_name_object_set: '%s', '%s'\n", name, decl);
#endif
n = Getattr(namehash, name);
if (!n) {
n = NewHash();
Setattr(namehash, name, n);
Delete(n);
}
/* Add an object based on the declarator value */
if (!decl) {
Setattr(n, "start", object);
} else {
SwigType *cd = Copy(decl);
Setattr(n, cd, object);
Delete(cd);
}
}
/* -----------------------------------------------------------------------------
* Swig_name_object_get()
*
* Return an object associated with an optional class prefix, name, and
* declarator. This function operates according to name matching rules
* described for the %rename directive in the SWIG manual.
* ----------------------------------------------------------------------------- */
static DOH *get_object(Hash *n, String *decl) {
DOH *rn = 0;
if (!n)
return 0;
if (decl) {
rn = Getattr(n, decl);
} else {
rn = Getattr(n, "start");
}
return rn;
}
static DOH *name_object_get(Hash *namehash, String *tname, SwigType *decl, SwigType *ncdecl) {
DOH *rn = 0;
Hash *n = Getattr(namehash, tname);
if (n) {
rn = get_object(n, decl);
if ((!rn) && ncdecl)
rn = get_object(n, ncdecl);
if (!rn)
rn = get_object(n, 0);
}
return rn;
}
DOH *Swig_name_object_get(Hash *namehash, String *prefix, String *name, SwigType *decl) {
String *tname = NewStringEmpty();
DOH *rn = 0;
char *ncdecl = 0;
if (!namehash)
return 0;
/* DB: This removed to more tightly control feature/name matching */
/* if ((decl) && (SwigType_isqualifier(decl))) {
ncdecl = strchr(Char(decl),'.');
ncdecl++;
}
*/
#ifdef SWIG_DEBUG
Printf(stdout, "Swig_name_object_get: '%s' '%s', '%s'\n", prefix, name, decl);
#endif
/* Perform a class-based lookup (if class prefix supplied) */
if (prefix) {
if (Len(prefix)) {
Printf(tname, "%s::%s", prefix, name);
rn = name_object_get(namehash, tname, decl, ncdecl);
if (!rn) {
String *cls = Swig_scopename_last(prefix);
if (!Equal(cls, prefix)) {
Clear(tname);
Printf(tname, "*::%s::%s", cls, name);
rn = name_object_get(namehash, tname, decl, ncdecl);
}
Delete(cls);
}
/* Lookup a name within a templated-based class */
if (!rn) {
String *t_name = SwigType_istemplate_templateprefix(prefix);
if (t_name) {
Clear(tname);
Printf(tname, "%s::%s", t_name, name);
rn = name_object_get(namehash, tname, decl, ncdecl);
Delete(t_name);
}
}
/* Lookup a template-based name within a class */
if (!rn) {
String *t_name = SwigType_istemplate_templateprefix(name);
if (t_name)
rn = Swig_name_object_get(namehash, prefix, t_name, decl);
Delete(t_name);
}
}
/* A wildcard-based class lookup */
if (!rn) {
Clear(tname);
Printf(tname, "*::%s", name);
rn = name_object_get(namehash, tname, decl, ncdecl);
}
} else {
/* Lookup in the global namespace only */
Clear(tname);
Printf(tname, "::%s", name);
rn = name_object_get(namehash, tname, decl, ncdecl);
}
/* Catch-all */
if (!rn) {
rn = name_object_get(namehash, name, decl, ncdecl);
}
if (!rn && Swig_scopename_check(name)) {
String *nprefix = 0;
String *nlast = 0;
Swig_scopename_split(name, &nprefix, &nlast);
rn = name_object_get(namehash, nlast, decl, ncdecl);
Delete(nlast);
Delete(nprefix);
}
Delete(tname);
#ifdef SWIG_DEBUG
Printf(stdout, "Swig_name_object_get: found %d\n", rn ? 1 : 0);
#endif
return rn;
}
/* -----------------------------------------------------------------------------
* Swig_name_object_inherit()
*
* Implements name-based inheritance scheme.
* ----------------------------------------------------------------------------- */
void Swig_name_object_inherit(Hash *namehash, String *base, String *derived) {
Iterator ki;
Hash *derh;
String *bprefix;
String *dprefix;
char *cbprefix;
int plen;
if (!namehash)
return;
/* Temporary hash holding all the entries we add while we iterate over
namehash itself as we can't modify the latter while iterating over it. */
derh = NULL;
bprefix = NewStringf("%s::", base);
dprefix = NewStringf("%s::", derived);
cbprefix = Char(bprefix);
plen = (int)strlen(cbprefix);
for (ki = First(namehash); ki.key; ki = Next(ki)) {
char *k = Char(ki.key);
if (strncmp(k, cbprefix, plen) == 0) {
/* Copy, adjusting name, this element to the derived hash. */
Iterator oi;
String *nkey = NewStringf("%s%s", dprefix, k + plen);
Hash *n = ki.item;
Hash *newh;
/* Don't overwrite an existing value for the derived class, if any. */
newh = Getattr(namehash, nkey);
if (!newh) {
if (!derh)
derh = NewHash();
newh = NewHash();
Setattr(derh, nkey, newh);
Delete(newh);
}
for (oi = First(n); oi.key; oi = Next(oi)) {
if (!Getattr(newh, oi.key)) {
String *ci = Copy(oi.item);
Setattr(newh, oi.key, ci);
Delete(ci);
}
}
Delete(nkey);
}
}
/* Merge the contents of derived hash into the main hash. */
if (derh) {
for (ki = First(derh); ki.key; ki = Next(ki)) {
Setattr(namehash, ki.key, ki.item);
}
}
Delete(bprefix);
Delete(dprefix);
Delete(derh);
}
/* -----------------------------------------------------------------------------
* merge_features()
*
* Given a hash, this function merges the features in the hash into the node.
* ----------------------------------------------------------------------------- */
static void merge_features(Hash *features, Node *n) {
Iterator ki;
if (!features)
return;
for (ki = First(features); ki.key; ki = Next(ki)) {
String *ci = Copy(ki.item);
Setattr(n, ki.key, ci);
Delete(ci);
}
}
/* -----------------------------------------------------------------------------
* Swig_features_get()
*
* Attaches any features in the features hash to the node that matches
* the declaration, decl.
* ----------------------------------------------------------------------------- */
static void features_get(Hash *features, const String *tname, SwigType *decl, SwigType *ncdecl, Node *node) {
Node *n = Getattr(features, tname);
#ifdef SWIG_DEBUG
Printf(stdout, " features_get: %s\n", tname);
#endif
if (n) {
merge_features(get_object(n, 0), node);
if (ncdecl)
merge_features(get_object(n, ncdecl), node);
merge_features(get_object(n, decl), node);
}
}
void Swig_features_get(Hash *features, String *prefix, String *name, SwigType *decl, Node *node) {
char *ncdecl = 0;
String *rdecl = 0;
String *rname = 0;
if (!features)
return;
/* MM: This removed to more tightly control feature/name matching */
/*
if ((decl) && (SwigType_isqualifier(decl))) {
ncdecl = strchr(Char(decl),'.');
ncdecl++;
}
*/
/* very specific hack for template constructors/destructors */
if (name && SwigType_istemplate(name)) {
String *nodetype = nodeType(node);
if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) {
String *nprefix = 0;
String *nlast = 0;
String *tprefix;
Swig_scopename_split(name, &nprefix, &nlast);
tprefix = SwigType_templateprefix(nlast);
Delete(nlast);
if (Len(nprefix)) {
Append(nprefix, "::");
Append(nprefix, tprefix);
Delete(tprefix);
rname = nprefix;
} else {
rname = tprefix;
Delete(nprefix);
}
rdecl = Copy(decl);
Replaceall(rdecl, name, rname);
decl = rdecl;
name = rname;
}
}
#ifdef SWIG_DEBUG
Printf(stdout, "Swig_features_get: '%s' '%s' '%s'\n", prefix, name, decl);
#endif
/* Global features */
features_get(features, "", 0, 0, node);
if (name) {
String *tname = NewStringEmpty();
/* add features for 'root' template */
String *dname = SwigType_istemplate_templateprefix(name);
if (dname) {
features_get(features, dname, decl, ncdecl, node);
}
/* Catch-all */
features_get(features, name, decl, ncdecl, node);
/* Perform a class-based lookup (if class prefix supplied) */
if (prefix) {
/* A class-generic feature */
if (Len(prefix)) {
Printf(tname, "%s::", prefix);
features_get(features, tname, decl, ncdecl, node);
}
/* A wildcard-based class lookup */
Clear(tname);
Printf(tname, "*::%s", name);
features_get(features, tname, decl, ncdecl, node);
/* A specific class lookup */
if (Len(prefix)) {
/* A template-based class lookup */
String *tprefix = SwigType_istemplate_templateprefix(prefix);
if (tprefix) {
Clear(tname);
Printf(tname, "%s::%s", tprefix, name);
features_get(features, tname, decl, ncdecl, node);
}
Clear(tname);
Printf(tname, "%s::%s", prefix, name);
features_get(features, tname, decl, ncdecl, node);
Delete(tprefix);
}
} else {
/* Lookup in the global namespace only */
Clear(tname);
Printf(tname, "::%s", name);
features_get(features, tname, decl, ncdecl, node);
}
Delete(tname);
Delete(dname);
}
if (name && SwigType_istemplate(name)) {
/* add features for complete template type */
String *dname = Swig_symbol_template_deftype(name, 0);
if (!Equal(dname, name)) {
Swig_features_get(features, prefix, dname, decl, node);
}
Delete(dname);
}
if (rname)
Delete(rname);
if (rdecl)
Delete(rdecl);
}
/* -----------------------------------------------------------------------------
* Swig_feature_set()
*
* Sets a feature name and value. Also sets optional feature attributes as
* passed in by featureattribs. Optional feature attributes are given a full name
* concatenating the feature name plus ':' plus the attribute name.
* ----------------------------------------------------------------------------- */
void Swig_feature_set(Hash *features, const_String_or_char_ptr name, SwigType *decl, const_String_or_char_ptr featurename, const_String_or_char_ptr value, Hash *featureattribs) {
Hash *n;
Hash *fhash;
#ifdef SWIG_DEBUG
Printf(stdout, "Swig_feature_set: '%s' '%s' '%s' '%s'\n", name, decl, featurename, value);
#endif
n = Getattr(features, name);
if (!n) {
n = NewHash();
Setattr(features, name, n);
Delete(n);
}
if (!decl) {
fhash = Getattr(n, "start");
if (!fhash) {
fhash = NewHash();
Setattr(n, "start", fhash);
Delete(fhash);
}
} else {
fhash = Getattr(n, decl);
if (!fhash) {
String *cdecl_ = Copy(decl);
fhash = NewHash();
Setattr(n, cdecl_, fhash);
Delete(cdecl_);
Delete(fhash);
}
}
if (value) {
Setattr(fhash, featurename, value);
} else {
Delattr(fhash, featurename);
}
{
/* Add in the optional feature attributes */
Hash *attribs = featureattribs;
while (attribs) {
String *attribname = Getattr(attribs, "name");
String *featureattribname = NewStringf("%s:%s", featurename, attribname);
if (value) {
String *attribvalue = Getattr(attribs, "value");
Setattr(fhash, featureattribname, attribvalue);
} else {
Delattr(fhash, featureattribname);
}
attribs = nextSibling(attribs);
Delete(featureattribname);
}
}
if (name && SwigType_istemplate(name)) {
String *dname = Swig_symbol_template_deftype(name, 0);
if (Strcmp(dname, name)) {
Swig_feature_set(features, dname, decl, featurename, value, featureattribs);
}
Delete(dname);
}
}
/* -----------------------------------------------------------------------------
* The rename/namewarn engine
*
* Code below was in parser.y for a while
* ----------------------------------------------------------------------------- */
static Hash *namewarn_hash = 0;
static Hash *name_namewarn_hash(void) {
if (!namewarn_hash)
namewarn_hash = NewHash();
return namewarn_hash;
}
static Hash *rename_hash = 0;
static Hash *name_rename_hash(void) {
if (!rename_hash)
rename_hash = NewHash();
return rename_hash;
}
static List *namewarn_list = 0;
static List *name_namewarn_list(void) {
if (!namewarn_list)
namewarn_list = NewList();
return namewarn_list;
}
static List *rename_list = 0;
static List *name_rename_list(void) {
if (!rename_list)
rename_list = NewList();
return rename_list;
}
/* -----------------------------------------------------------------------------
* int need_name_warning(Node *n)
*
* Detects if a node needs name warnings
*
* ----------------------------------------------------------------------------- */
static int need_name_warning(Node *n) {
int need = 1;
/*
We don't use name warnings for:
- class forwards, no symbol is generated at the target language.
- template declarations, only for real instances using %template(name).
- typedefs, have no effect at the target language.
- using declarations and using directives, have no effect at the target language.
*/
if (checkAttribute(n, "nodeType", "classforward")) {
need = 0;
} else if (checkAttribute(n, "nodeType", "using")) {
need = 0;
} else if (checkAttribute(n, "storage", "typedef")) {
need = 0;
} else if (Getattr(n, "hidden")) {
need = 0;
} else if (Getattr(n, "ignore")) {
need = 0;
} else if (Getattr(n, "templatetype")) {
need = 0;
} else if (GetFlag(n, "parsing_template_declaration")) {
need = 0;
}
return need;
}
/* -----------------------------------------------------------------------------
* int Swig_need_redefined_warn()
*
* Detects when a redefined object needs a warning
*
* ----------------------------------------------------------------------------- */
static int nodes_are_equivalent(Node *a, Node *b, int a_inclass) {
/* they must have the same type */
String *ta = nodeType(a);
String *tb = nodeType(b);
if (!Equal(ta, tb)) {
if (!(Equal(ta, "using") && Equal(tb, "cdecl"))) {
return 0;
}
}
if (Cmp(ta, "cdecl") == 0) {
/* both cdecl case */
/* typedef */
String *a_storage = Getattr(a, "storage");
String *b_storage = Getattr(b, "storage");
if ((Cmp(a_storage, "typedef") == 0)
|| (Cmp(b_storage, "typedef") == 0)) {
if (Cmp(a_storage, b_storage) == 0) {
String *a_type = (Getattr(a, "type"));
String *b_type = (Getattr(b, "type"));
if (Cmp(a_type, b_type) == 0)
return 1;
}
return 0;
}
/* static functions */
if (Swig_storage_isstatic(a) || Swig_storage_isstatic(b)) {
if (Cmp(a_storage, b_storage) != 0)
return 0;
}
/* friend methods */
if (!a_inclass || (Cmp(a_storage, "friend") == 0)) {
/* check declaration */
String *a_decl = (Getattr(a, "decl"));
String *b_decl = (Getattr(b, "decl"));
if (Cmp(a_decl, b_decl) == 0) {
/* check return type */
String *a_type = (Getattr(a, "type"));
String *b_type = (Getattr(b, "type"));
if (Cmp(a_type, b_type) == 0) {
/* check parameters */
Parm *ap = (Getattr(a, "parms"));
Parm *bp = (Getattr(b, "parms"));
while (ap && bp) {
SwigType *at = Getattr(ap, "type");
SwigType *bt = Getattr(bp, "type");
if (Cmp(at, bt) != 0)
return 0;
ap = nextSibling(ap);
bp = nextSibling(bp);
}
if (ap || bp) {
return 0;
} else {
Node *a_template = Getattr(a, "template");
Node *b_template = Getattr(b, "template");
/* Not equivalent if one is a template instantiation (via %template) and the other is a non-templated function */
if ((a_template && !b_template) || (!a_template && b_template))
return 0;
}
return 1;
}
}
}
} else if (Equal(ta, "using")) {
/* using and cdecl case */
String *b_storage = Getattr(b, "storage");
if (Equal(b_storage, "typedef")) {
String *a_name = Getattr(a, "name");
String *b_name = Getattr(b, "name");
if (Equal(a_name, b_name))
return 1;
}
} else {
/* both %constant case */
String *a_storage = Getattr(a, "storage");
String *b_storage = Getattr(b, "storage");
if ((Cmp(a_storage, "%constant") == 0)
|| (Cmp(b_storage, "%constant") == 0)) {
if (Cmp(a_storage, b_storage) == 0) {
String *a_type = (Getattr(a, "type"));
String *b_type = (Getattr(b, "type"));
if ((Cmp(a_type, b_type) == 0)
&& (Cmp(Getattr(a, "value"), Getattr(b, "value")) == 0))
return 1;
}
return 0;
}
if (Equal(ta, "template") && Equal(tb, "template")) {
if (Cmp(a_storage, "friend") == 0 || Cmp(b_storage, "friend") == 0)
return 1;
}
}
return 0;
}
int Swig_need_redefined_warn(Node *a, Node *b, int InClass) {
String *a_name = Getattr(a, "name");
String *b_name = Getattr(b, "name");
String *a_symname = Getattr(a, "sym:name");
String *b_symname = Getattr(b, "sym:name");
/* always send a warning if a 'rename' is involved */
if ((a_symname && !Equal(a_symname, a_name))
|| (b_symname && !Equal(b_symname, b_name))) {
if (!Equal(a_name, b_name)) {
return 1;
}
}
return !nodes_are_equivalent(a, b, InClass);
}
/* -----------------------------------------------------------------------------
* int Swig_need_protected(Node* n)
*
* Detects when we need to fully register the protected member.
* This is basically any protected members when the allprotected mode is set.
* Otherwise we take just the protected virtual methods and non-static methods
* (potentially virtual methods) as well as constructors/destructors.
* Also any "using" statements in a class may potentially be virtual.
* ----------------------------------------------------------------------------- */
int Swig_need_protected(Node *n) {
String *nodetype = nodeType(n);
if (checkAttribute(n, "access", "protected")) {
if ((Equal(nodetype, "cdecl"))) {
if (Swig_director_mode() && Swig_director_protected_mode() && Swig_all_protected_mode()) {
return 1;
}
if (SwigType_isfunction(Getattr(n, "decl"))) {
String *storage = Getattr(n, "storage");
/* The function is declared virtual, or it has no storage. This eliminates typedef, static etc. */
return !storage || Equal(storage, "virtual");
}
} else if (Equal(nodetype, "constructor") || Equal(nodetype, "destructor")) {
return 1;
} else if (Equal(nodetype, "using") && !Getattr(n, "namespace")) {
return 1;
}
}
return 0;
}
/* -----------------------------------------------------------------------------
* void name_nameobj_add()
*
* Add nameobj (rename/namewarn)
*
* ----------------------------------------------------------------------------- */
static List *make_attrlist(const char *ckey) {
List *list = NewList();
const char *cattr = strchr(ckey, '$');
if (cattr) {
String *nattr;
const char *rattr = strchr(++cattr, '$');
while (rattr) {
nattr = NewStringWithSize(cattr, (int)(rattr - cattr));
Append(list, nattr);
Delete(nattr);
cattr = rattr + 1;
rattr = strchr(cattr, '$');
}
nattr = NewString(cattr);
Append(list, nattr);
Delete(nattr);
} else {
Append(list, "nodeType");
}
return list;
}
static void name_object_attach_keys(const char *keys[], Hash *nameobj) {
Node *kw = nextSibling(nameobj);
List *matchlist = 0;
while (kw) {
Node *next = nextSibling(kw);
String *kname = Getattr(kw, "name");
char *ckey = kname ? Char(kname) : 0;
if (ckey) {
const char **rkey;
int isnotmatch = 0;
int isregexmatch = 0;
if ((strncmp(ckey, "match", 5) == 0)
|| (isnotmatch = (strncmp(ckey, "notmatch", 8) == 0))
|| (isregexmatch = (strncmp(ckey, "regexmatch", 10) == 0))
|| (isnotmatch = isregexmatch = (strncmp(ckey, "notregexmatch", 13) == 0))) {
Hash *mi = NewHash();
List *attrlist = make_attrlist(ckey);
if (!matchlist)
matchlist = NewList();
Setattr(mi, "value", Getattr(kw, "value"));
Setattr(mi, "attrlist", attrlist);
if (isnotmatch)
SetFlag(mi, "notmatch");
if (isregexmatch)
SetFlag(mi, "regexmatch");
Delete(attrlist);
Append(matchlist, mi);
Delete(mi);
removeNode(kw);
} else {
for (rkey = keys; *rkey != 0; ++rkey) {
if (strcmp(ckey, *rkey) == 0) {
Setattr(nameobj, *rkey, Getattr(kw, "value"));
removeNode(kw);
}
}
}
}
kw = next;
}
if (matchlist) {
Setattr(nameobj, "matchlist", matchlist);
Delete(matchlist);
}
}
static void name_nameobj_add(Hash *name_hash, List *name_list, String *prefix, String *name, SwigType *decl, Hash *nameobj) {
String *nname = 0;
if (name && Len(name)) {
String *target_fmt = Getattr(nameobj, "targetfmt");
nname = prefix ? NewStringf("%s::%s", prefix, name) : NewString(name);
if (target_fmt) {
String *tmp = NewStringf(target_fmt, nname);
Delete(nname);
nname = tmp;
}
}
if (!nname || !Len(nname) || Getattr(nameobj, "fullname") || /* any of these options trigger a 'list' nameobj */
Getattr(nameobj, "sourcefmt") || Getattr(nameobj, "matchlist") || Getattr(nameobj, "regextarget")) {
if (decl)
Setattr(nameobj, "decl", decl);
if (nname && Len(nname))
Setattr(nameobj, "targetname", nname);
/* put the new nameobj at the beginning of the list, such that the
last inserted rule take precedence */
Insert(name_list, 0, nameobj);
} else {
/* here we add an old 'hash' nameobj, simple and fast */
Swig_name_object_set(name_hash, nname, decl, nameobj);
}
Delete(nname);
}
/* -----------------------------------------------------------------------------
* int name_match_nameobj()
*
* Apply and check the nameobj's math list to the node
*
* ----------------------------------------------------------------------------- */
static DOH *get_lattr(Node *n, List *lattr) {
DOH *res = 0;
int ilen = Len(lattr);
int i;
for (i = 0; n && (i < ilen); ++i) {
String *nattr = Getitem(lattr, i);
res = Getattr(n, nattr);
#ifdef SWIG_DEBUG
if (!res) {
Printf(stdout, "missing %s %s %s\n", nattr, Getattr(n, "name"), Getattr(n, "member"));
} else {
Printf(stdout, "lattr %d %s %s\n", i, nattr, DohIsString(res) ? res : Getattr(res, "name"));
}
#endif
n = res;
}
return res;
}
#ifdef HAVE_PCRE
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
static int name_regexmatch_value(Node *n, String *pattern, String *s) {
pcre2_code *compiled_pat;
PCRE2_UCHAR err[256];
int errornum;
size_t errpos;
int rc;
pcre2_match_data *match_data = 0;
compiled_pat = pcre2_compile((PCRE2_SPTR8)Char(pattern), PCRE2_ZERO_TERMINATED, 0, &errornum, &errpos, NULL);
if (!compiled_pat) {
pcre2_get_error_message (errornum, err, sizeof err);
Swig_error("SWIG", Getline(n),
"Invalid regex \"%s\": compilation failed at %d: %s\n",
Char(pattern), errpos, err);
Exit(EXIT_FAILURE);
}
match_data = pcre2_match_data_create_from_pattern (compiled_pat, NULL);
rc = pcre2_match(compiled_pat, (PCRE2_SPTR8)Char(s), PCRE2_ZERO_TERMINATED, 0, 0, match_data, 0);
pcre2_code_free(compiled_pat);
pcre2_match_data_free(match_data);
if (rc == PCRE2_ERROR_NOMATCH)
return 0;
if (rc < 0 ) {
Swig_error("SWIG", Getline(n),
"Matching \"%s\" against regex \"%s\" failed: %d\n",
Char(s), Char(pattern), rc);
Exit(EXIT_FAILURE);
}
return 1;
}
#else /* !HAVE_PCRE */
static int name_regexmatch_value(Node *n, String *pattern, String *s) {
(void)pattern;
(void)s;
Swig_error("SWIG", Getline(n),
"PCRE regex matching is not available in this SWIG build.\n");
Exit(EXIT_FAILURE);
return 0;
}
#endif /* HAVE_PCRE/!HAVE_PCRE */
static int name_match_value(String *mvalue, String *value) {
#if defined(SWIG_USE_SIMPLE_MATCHOR)
int match = 0;
char *cvalue = Char(value);
char *cmvalue = Char(mvalue);
char *sep = strchr(cmvalue, '|');
while (sep && !match) {
match = strncmp(cvalue, cmvalue, sep - cmvalue) == 0;
#ifdef SWIG_DEBUG
Printf(stdout, "match_value: %s %s %d\n", cvalue, cmvalue, match);
#endif
cmvalue = sep + 1;
sep = strchr(cmvalue, '|');
}
if (!match) {
match = strcmp(cvalue, cmvalue) == 0;
#ifdef SWIG_DEBUG
Printf(stdout, "match_value: %s %s %d\n", cvalue, cmvalue, match);
#endif
}
return match;
#else
return Equal(mvalue, value);
#endif
}
static int name_match_nameobj(Hash *rn, Node *n) {
int match = 1;
List *matchlist = Getattr(rn, "matchlist");
#ifdef SWIG_DEBUG
Printf(stdout, "name_match_nameobj: %s\n", Getattr(n, "name"));
#endif
if (matchlist) {
int ilen = Len(matchlist);
int i;
for (i = 0; match && (i < ilen); ++i) {
Node *mi = Getitem(matchlist, i);
List *lattr = Getattr(mi, "attrlist");
String *nval = get_lattr(n, lattr);
int notmatch = GetFlag(mi, "notmatch");
int regexmatch = GetFlag(mi, "regexmatch");
match = 0;
if (nval) {
String *kwval = Getattr(mi, "value");
match = regexmatch ? name_regexmatch_value(n, kwval, nval)
: name_match_value(kwval, nval);
#ifdef SWIG_DEBUG
Printf(stdout, "val %s %s %d %d \n", nval, kwval, match, ilen);
#endif
}
if (notmatch)
match = !match;
}
}
#ifdef SWIG_DEBUG
Printf(stdout, "name_match_nameobj: %d\n", match);
#endif
return match;
}
/* -----------------------------------------------------------------------------
* Hash *name_nameobj_lget()
*
* Get a nameobj (rename/namewarn) from the list of filters
*
* ----------------------------------------------------------------------------- */
static Hash *name_nameobj_lget(List *namelist, Node *n, String *prefix, String *name, String *decl) {
Hash *res = 0;
if (namelist) {
int len = Len(namelist);
int i;
int match = 0;
for (i = 0; !match && (i < len); i++) {
Hash *rn = Getitem(namelist, i);
String *rdecl = Getattr(rn, "decl");
if (rdecl && (!decl || !Equal(rdecl, decl))) {
continue;
} else if (name_match_nameobj(rn, n)) {
String *tname = Getattr(rn, "targetname");
if (tname) {
String *sfmt = Getattr(rn, "sourcefmt");
String *sname = 0;
int fullname = GetFlag(rn, "fullname");
int regextarget = GetFlag(rn, "regextarget");
if (sfmt) {
if (fullname && prefix) {
String *pname = NewStringf("%s::%s", prefix, name);
sname = NewStringf(sfmt, pname);
Delete(pname);
} else {
sname = NewStringf(sfmt, name);
}
} else {
if (fullname && prefix) {
sname = NewStringf("%s::%s", prefix, name);
} else {
sname = name;
DohIncref(name);
}
}
match = regextarget ? name_regexmatch_value(n, tname, sname)
: name_match_value(tname, sname);
Delete(sname);
} else {
/* Applying the renaming rule may fail if it contains a %(regex)s expression that doesn't match the given name. */
String *sname = NewStringf(Getattr(rn, "name"), name);
if (sname) {
if (Len(sname))
match = 1;
Delete(sname);
}
}
}
if (match) {
res = rn;
break;
}
}
}
return res;
}
/* -----------------------------------------------------------------------------
* Swig_name_namewarn_add
*
* Add a namewarn objects
*
* ----------------------------------------------------------------------------- */
void Swig_name_namewarn_add(String *prefix, String *name, SwigType *decl, Hash *namewrn) {
const char *namewrn_keys[] = { "rename", "error", "fullname", "sourcefmt", "targetfmt", 0 };
name_object_attach_keys(namewrn_keys, namewrn);
name_nameobj_add(name_namewarn_hash(), name_namewarn_list(), prefix, name, decl, namewrn);
}
/* -----------------------------------------------------------------------------
* Hash *name_namewarn_get()
*
* Return the namewarn object, if there is one.
*
* ----------------------------------------------------------------------------- */
static Hash *name_namewarn_get(Node *n, String *prefix, String *name, SwigType *decl) {
if (!namewarn_hash && !namewarn_list)
return 0;
if (n) {
/* Return in the obvious cases */
if (!name || !need_name_warning(n)) {
return 0;
} else {
String *access = Getattr(n, "access");
int is_public = !access || Equal(access, "public");
if (!is_public && !Swig_need_protected(n)) {
return 0;
}
}
}
if (name) {
/* Check to see if the name is in the hash */
Hash *wrn = Swig_name_object_get(name_namewarn_hash(), prefix, name, decl);
if (wrn && !name_match_nameobj(wrn, n))
wrn = 0;
if (!wrn) {
wrn = name_nameobj_lget(name_namewarn_list(), n, prefix, name, decl);
}
if (wrn && Getattr(wrn, "error")) {
if (n) {
Swig_error(Getfile(n), Getline(n), "%s\n", Getattr(wrn, "name"));
} else {
Swig_error(cparse_file, cparse_line, "%s\n", Getattr(wrn, "name"));
}
}
return wrn;
} else {
return 0;
}
}
/* -----------------------------------------------------------------------------
* String *Swig_name_warning()
*
* Return the name warning, if there is one.
*
* ----------------------------------------------------------------------------- */
String *Swig_name_warning(Node *n, String *prefix, String *name, SwigType *decl) {
Hash *wrn = name_namewarn_get(n, prefix, name, decl);
return (name && wrn) ? Getattr(wrn, "name") : 0;
}
/* -----------------------------------------------------------------------------
* Swig_name_rename_add()
*
* Manage the rename objects
*
* ----------------------------------------------------------------------------- */
static void single_rename_add(String *prefix, String *name, SwigType *decl, Hash *newname) {
name_nameobj_add(name_rename_hash(), name_rename_list(), prefix, name, decl, newname);
}
/* Add a new rename. Works much like new_feature including default argument handling. */
void Swig_name_rename_add(String *prefix, String *name, SwigType *decl, Hash *newname, ParmList *declaratorparms) {
ParmList *declparms = declaratorparms;
const char *rename_keys[] = { "fullname", "sourcefmt", "targetfmt", "continue", "regextarget", 0 };
name_object_attach_keys(rename_keys, newname);
/* Add the name */
single_rename_add(prefix, name, decl, newname);
/* Add extra names if there are default parameters in the parameter list */
if (decl) {
int constqualifier = SwigType_isconst(decl);
while (declparms) {
if (ParmList_has_defaultargs(declparms)) {
/* Create a parameter list for the new rename by copying all
but the last (defaulted) parameter */
ParmList *newparms = CopyParmListMax(declparms,ParmList_len(declparms)-1);
/* Create new declaration - with the last parameter removed */
SwigType *newdecl = Copy(decl);
Delete(SwigType_pop_function(newdecl)); /* remove the old parameter list from newdecl */
SwigType_add_function(newdecl, newparms);
if (constqualifier)
SwigType_add_qualifier(newdecl, "const");
single_rename_add(prefix, name, newdecl, newname);
declparms = newparms;
Delete(newdecl);
} else {
declparms = 0;
}
}
}
}
/* Create a name for the given node applying rename/namewarn if needed */
static String *apply_rename(Node* n, String *newname, int fullname, String *prefix, String *name) {
String *result = 0;
if (newname && Len(newname)) {
if (Strcmp(newname, "$ignore") == 0) {
/* $ignore doesn't apply to parameters and while it's rare to explicitly write %ignore directives for them they could be caught by a wildcard ignore using
regex match, just ignore the attempt to ignore them in this case */
if (!Equal(nodeType(n), "parm"))
result = Copy(newname);
} else {
char *cnewname = Char(newname);
if (cnewname) {
int destructor = name && (*(Char(name)) == '~');
String *fmt = newname;
/* use name as a fmt, but avoid C++ "%" and "%=" operators */
if (Len(newname) > 1 && strchr(cnewname, '%') && !(strcmp(cnewname, "%=") == 0)) {
if (fullname && prefix) {
result = NewStringf(fmt, prefix, name);
} else {
result = NewStringf(fmt, name);
}
} else {
result = Copy(newname);
}
if (destructor && result && (*(Char(result)) != '~')) {
Insert(result, 0, "~");
}
}
}
}
return result;
}
/* -----------------------------------------------------------------------------
* String *Swig_name_make()
*
* Make a name after applying all the rename/namewarn objects
*
* ----------------------------------------------------------------------------- */
String *Swig_name_make(Node *n, String *prefix, const_String_or_char_ptr cname, SwigType *decl, String *oldname) {
String *nname = 0;
String *result = 0;
String *name = NewString(cname);
Hash *wrn = 0;
String *rdecl = 0;
String *rname = 0;
/* very specific hack for template constructors/destructors */
#ifdef SWIG_DEBUG
Printf(stdout, "Swig_name_make: looking for %s %s %s %s\n", prefix, name, decl, oldname);
#endif
if (name && n && SwigType_istemplate(name)) {
String *nodetype = nodeType(n);
if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) {
String *nprefix = 0;
String *nlast = 0;
String *tprefix;
Swig_scopename_split(name, &nprefix, &nlast);
tprefix = SwigType_templateprefix(nlast);
Delete(nlast);
if (Len(nprefix)) {
Append(nprefix, "::");
Append(nprefix, tprefix);
Delete(tprefix);
rname = nprefix;
} else {
rname = tprefix;
Delete(nprefix);
}
rdecl = Copy(decl);
Replaceall(rdecl, name, rname);
#ifdef SWIG_DEBUG
Printf(stdout, "SWIG_name_make: use new name %s %s : %s %s\n", name, decl, rname, rdecl);
#endif
decl = rdecl;
Delete(name);
name = rname;
}
}
if (rename_hash || rename_list || namewarn_hash || namewarn_list) {
Hash *rn = Swig_name_object_get(name_rename_hash(), prefix, name, decl);
if (!rn || !name_match_nameobj(rn, n)) {
rn = name_nameobj_lget(name_rename_list(), n, prefix, name, decl);
if (rn) {
String *sfmt = Getattr(rn, "sourcefmt");
int fullname = GetFlag(rn, "fullname");
if (fullname && prefix) {
String *sname = NewStringf("%s::%s", prefix, name);
Delete(name);
name = sname;
prefix = 0;
}
if (sfmt) {
String *sname = NewStringf(sfmt, name);
Delete(name);
name = sname;
}
}
}
if (rn) {
String *newname = Getattr(rn, "name");
int fullname = GetFlag(rn, "fullname");
result = apply_rename(n, newname, fullname, prefix, name);
}
if (result && !Equal(result, name)) {
/* operators in C++ allow aliases, we look for them */
char *cresult = Char(result);
if (cresult && (strncmp(cresult, "operator ", 9) == 0)) {
String *nresult = Swig_name_make(n, prefix, result, decl, oldname);
if (!Equal(nresult, result)) {
Delete(result);
result = nresult;
} else {
Delete(nresult);
}
}
}
nname = result ? result : name;
wrn = name_namewarn_get(n, prefix, nname, decl);
if (wrn) {
String *rename = Getattr(wrn, "rename");
if (rename) {
String *msg = Getattr(wrn, "name");
int fullname = GetFlag(wrn, "fullname");
if (result)
Delete(result);
result = apply_rename(n, rename, fullname, prefix, name);
if ((msg) && (Len(msg))) {
if (!Getmeta(nname, "already_warned")) {
String* suffix = 0;
if (Strcmp(result, "$ignore") == 0) {
suffix = NewStringf(": ignoring '%s'\n", name);
} else if (Strcmp(result, name) != 0) {
suffix = NewStringf(", renaming to '%s'\n", result);
} else {
/* No rename was performed */
suffix = NewString("\n");
}
if (n) {
/* Parameter renaming is not fully implemented. Mainly because there is no C/C++ syntax to
* for %rename to fully qualify a function's parameter name from outside the function. Hence it
* is not possible to implemented targeted warning suppression on one parameter in one function. */
int suppress_parameter_rename_warning = Equal(nodeType(n), "parm");
if (!suppress_parameter_rename_warning) {
SWIG_WARN_NODE_BEGIN(n);
Swig_warning(0, Getfile(n), Getline(n), "%s%s", msg, suffix);
SWIG_WARN_NODE_END(n);
}
} else {
Swig_warning(0, Getfile(name), Getline(name), "%s%s", msg, suffix);
}
Setmeta(nname, "already_warned", "1");
Delete(suffix);
}
}
}
}
}
if (!result || !Len(result)) {
if (result)
Delete(result);
if (oldname) {
result = NewString(oldname);
} else {
result = NewString(cname);
}
}
Delete(name);
#ifdef SWIG_DEBUG
Printf(stdout, "Swig_name_make: result '%s' '%s'\n", cname, result);
#endif
return result;
}
/* -----------------------------------------------------------------------------
* void Swig_name_inherit()
*
* Inherit namewarn, rename, and feature objects
*
* ----------------------------------------------------------------------------- */
void Swig_name_inherit(String *base, String *derived) {
/* Printf(stdout,"base = '%s', derived = '%s'\n", base, derived); */
Swig_name_object_inherit(name_rename_hash(), base, derived);
Swig_name_object_inherit(name_namewarn_hash(), base, derived);
Swig_name_object_inherit(Swig_cparse_features(), base, derived);
}
/* -----------------------------------------------------------------------------
* Swig_inherit_base_symbols()
* ----------------------------------------------------------------------------- */
void Swig_inherit_base_symbols(List *bases) {
if (bases) {
Iterator s;
for (s = First(bases); s.item; s = Next(s)) {
Symtab *st = Getattr(s.item, "symtab");
if (st) {
Setfile(st, Getfile(s.item));
Setline(st, Getline(s.item));
Swig_symbol_inherit(st);
}
}
Delete(bases);
}
}
/* -----------------------------------------------------------------------------
* Swig_make_inherit_list()
* ----------------------------------------------------------------------------- */
List *Swig_make_inherit_list(String *clsname, List *names, String *Namespaceprefix) {
int i, ilen;
String *derived;
List *bases = NewList();
if (Namespaceprefix)
derived = NewStringf("%s::%s", Namespaceprefix, clsname);
else
derived = NewString(clsname);
ilen = Len(names);
for (i = 0; i < ilen; i++) {
String *base;
String *n = Getitem(names, i);
/* Try to figure out where this symbol is */
Node *s = Swig_symbol_clookup(n, 0);
if (s) {
while (s && (Strcmp(nodeType(s), "class") != 0)) {
/* Not a class. Could be a typedef though. */
String *storage = Getattr(s, "storage");
if (storage && (Strcmp(storage, "typedef") == 0)) {
String *nn = Getattr(s, "type");
s = Swig_symbol_clookup(nn, Getattr(s, "sym:symtab"));
} else {
break;
}
}
if (s && ((Strcmp(nodeType(s), "class") == 0) || (Strcmp(nodeType(s), "template") == 0))) {
String *q = Swig_symbol_qualified(s);
Append(bases, s);
if (q) {
base = NewStringf("%s::%s", q, Getattr(s, "name"));
Delete(q);
} else {
base = NewString(Getattr(s, "name"));
}
} else {
base = NewString(n);
}
} else {
base = NewString(n);
}
if (base) {
Swig_name_inherit(base, derived);
Delete(base);
}
}
return bases;
}
/* -----------------------------------------------------------------------------
* void Swig_name_str()
*
* Return a stringified version of a C/C++ symbol from a node.
* The node passed in is expected to be a function, constructor, destructor or
* variable. Some example return values:
* "MyNameSpace::MyTemplate<MyNameSpace::ABC >::~MyTemplate"
* "MyNameSpace::ABC::ABC"
* "MyNameSpace::ABC::constmethod"
* "MyNameSpace::ABC::variablename"
*
* ----------------------------------------------------------------------------- */
String *Swig_name_str(Node *n) {
String *qname;
String *qualifier = Swig_symbol_qualified(n);
String *name = Swig_scopename_last(Getattr(n, "name"));
if (qualifier)
qualifier = SwigType_namestr(qualifier);
/* Very specific hack for template constructors/destructors */
if (SwigType_istemplate(name)) {
String *nodetype = nodeType(n);
if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) {
String *nprefix = 0;
String *nlast = 0;
String *tprefix;
Swig_scopename_split(name, &nprefix, &nlast);
tprefix = SwigType_templateprefix(nlast);
Delete(nlast);
Delete(nprefix);
Delete(name);
name = tprefix;
}
}
qname = NewString("");
if (qualifier && Len(qualifier) > 0)
Printf(qname, "%s::", qualifier);
Printf(qname, "%s", SwigType_str(name, 0));
Delete(name);
Delete(qualifier);
return qname;
}
/* -----------------------------------------------------------------------------
* void Swig_name_decl()
*
* Return a stringified version of a C/C++ declaration without the return type.
* The node passed in is expected to be a function, constructor, destructor or
* variable. Some example return values:
* "MyNameSpace::MyTemplate<MyNameSpace::ABC >::~MyTemplate()"
* "MyNameSpace::ABC::ABC(int,double)"
* "MyNameSpace::ABC::constmethod(int) const"
* "MyNameSpace::ABC::refqualifiermethod(int) const &"
* "MyNameSpace::ABC::variablename"
*
* ----------------------------------------------------------------------------- */
String *Swig_name_decl(Node *n) {
String *qname;
String *decl;
qname = Swig_name_str(n);
decl = NewStringf("%s", qname);
if (!checkAttribute(n, "kind", "variable")) {
String *d = Getattr(n, "decl");
Printv(decl, "(", ParmList_errorstr(Getattr(n, "parms")), ")", NIL);
if (SwigType_isfunction(d)) {
SwigType *decl_temp = Copy(d);
SwigType *qualifiers = SwigType_pop_function_qualifiers(decl_temp);
if (qualifiers) {
String *qualifiers_string = SwigType_str(qualifiers, 0);
Printv(decl, " ", qualifiers_string, NIL);
Delete(qualifiers_string);
}
Delete(decl_temp);
}
}
Delete(qname);
return decl;
}
/* -----------------------------------------------------------------------------
* void Swig_name_fulldecl()
*
* Return a stringified version of a C/C++ declaration including the return type.
* The node passed in is expected to be a function, constructor or destructor.
* Some example return values:
* "MyNameSpace::MyTemplate<MyNameSpace::ABC >::~MyTemplate()"
* "MyNameSpace::ABC::ABC(int,double)"
* "int * MyNameSpace::ABC::constmethod(int) const"
*
* ----------------------------------------------------------------------------- */
String *Swig_name_fulldecl(Node *n) {
String *decl = Swig_name_decl(n);
String *type = Getattr(n, "type");
String *nodetype = nodeType(n);
String *fulldecl;
/* add on the return type */
if (nodetype && (Equal(nodetype, "constructor") || Equal(nodetype, "destructor"))) {
fulldecl = decl;
} else {
String *t = SwigType_str(type, 0);
fulldecl = NewStringf("%s %s", t, decl);
Delete(decl);
Delete(t);
}
return fulldecl;
}
|