This file is indexed.

/usr/bin/freehdl-gennodes is in freehdl 0.0.8-2.2ubuntu2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

   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
#! /bin/sh
# restart with -*- scheme -*-
exec guile -s $0 "$@"
!#

(debug-enable 'debug)
(debug-enable 'backtrace)
(read-enable 'positions)

;;;  Copyright (C) 1998,1999,2000,2001  Marius Vollmer
;;;
;;;  VAUL is free software; you can redistribute it and/or modify it
;;;  under the terms of the GNU Library General Public License as
;;;  published by the Free Software Foundation; either version 2 of the
;;;  License, or (at your option) any later version.
;;;
;;;  VAUL 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 Library General
;;;  Public License for more details.
;;;
;;;  You should have received a copy of the GNU Library General Public
;;;  License along with VAUL; see the file COPYING.LIB.  If not, write
;;;  to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
;;;  Boston, MA 02111-1307 USA.

;; TODO
;;
;; - give correct position for (err ...)

;; This is `gen-nodes', the Scheme program used to generate the C++
;; rendition of the data types used in the intermediate
;; representation.

;;; Used modules.

;; We include them here inline to make gen-nodes selfcontained.

(define-module (struct)
  :use-module (ice-9 common-list))

(defmacro-public define-struct (tag . fields)
  (let ((type-sym (gensym))
	(field-syms (map (lambda (f)
			   (if (pair? f) (car f) f))
			 fields))
	(make-parms (remove-if pair? fields))
	(make-args (map (lambda (f)
			  (if (pair? f) (cadr f) f))
			fields)))
    `(begin
       (define ,type-sym (make-record-type ',tag ',field-syms))
       (define ,(symbol-append 'make- tag)
	 (let ((maker (record-constructor ,type-sym)))
	   (lambda ,make-parms
	     (maker ,@make-args))))
       (define ,(symbol-append tag '?) (record-predicate ,type-sym))
       ,@(map (lambda (f)
		`(define ,(symbol-append tag '- f)
		   (record-accessor ,type-sym ',f)))
	      field-syms)
       ,@(map (lambda (f)
		`(define ,(symbol-append 'set- tag '- f '!)
		   (record-modifier ,type-sym ',f)))
	      field-syms))))

(define-module (mini-format))

(define-public (format-with-list-template dst fmt . args)
  (cond
   ((eq? dst #t)
    (apply format-with-list-template (current-output-port) fmt args))
   ((eq? dst #f)
    (call-with-output-string
     (lambda (p)
       (apply format-with-list-template p fmt args))))
   (else
    (let loop ((fmt fmt)
	       (args args))
      (if (null? fmt)
	  #t
	  (let ((f (car fmt)))
	    (cond
	     ((string? f)
	      (display f dst)
	      (loop (cdr fmt) args))
	     ((procedure? f)
	      (loop (cdr fmt) (f args dst)))
	     (else
	      (error "unknown formatting op" f)))))))))

(define (fmt-display args dst)
  (display (car args) dst)
  (cdr args))

(define (fmt-write args dst)
  (write (car args) dst)
  (cdr args))

(define (fmt-newline args dst)
  (newline dst)
  args)

(define-public (string-template->list-template fmt)
  (let ((tilde (string-index fmt #\~)))
    (if (and tilde (< tilde (string-length fmt)))
	(let* ((prefix (substring fmt 0 tilde))
	       (arg (string-ref fmt (+ tilde 1))))
	  (if (not (memq arg '(#\a #\d #\s #\%)))
	      #f
	      (let* ((rest (string-template->list-template
			    (substring fmt (+ tilde 2))))
		     (subst
		      (case arg
			((#\a #\d)
			 fmt-display)
			((#\s)
			 fmt-write)
			((#\%)
			 (set! prefix (string-append prefix "\n"))
			 #f))))
		(and rest
		     (if (zero? (string-length prefix))
			 (cons subst rest)
			 (cons prefix (if subst
					  (cons subst rest)
					  rest)))))))
	;; no ~ in fmt
	(if (zero? (string-length fmt))
	    '()
	    (list fmt)))))

(defmacro-public mini-format-macro (dst fmt . args)
  (let ((m-fmt (and (string? fmt) (string-template->list-template fmt))))
    (if m-fmt
	`(format-with-list-template ,dst ',m-fmt ,@args)
	(error "unsupported format template" fmt))))

(define-public (mini-format dst fmt . args)
  (let ((m-fmt (and (string? fmt) (string-template->list-template fmt))))
    (if m-fmt
	(apply format-with-list-template dst m-fmt args)
	(error "unsupported format template" fmt))))

;;; Start of gen-nodes.

(define-module (gen-nodes)
  :use-module (mini-format)
  :use-module (struct)
  :use-module (ice-9 common-list)
  :use-module (ice-9 regex))

(if (not (defined? 'read-line))
    (use-modules (ice-9 rdelim)))

;;; Configuration

(define default-include-path "/usr/include")

;;; Some utility functions

;; Like STRING-APPEND but also works on symbols.

(define (string-append* . args)
  (apply string-append
	 (map (lambda (s)
		(if (symbol? s)
		    (symbol->string s)
		    s))
	      args)))

;; Write ARGS to the current error port and return the last arg.

(define (pk . args)
  (write args (current-error-port))
  (newline (current-error-port))
  (car (last-pair args)))

;; Return a string whose characters are the result of applying PROC to
;; the characters of STR, from left to right.

(define (string-map proc str)
  (if (symbol? str)
      (string-map proc (symbol->string str))
      (let* ((len (string-length str))
	     (str2 (make-string len)))
	(do ((i 0 (1+ i)))
	    ((= i len) str2)
	  (string-set! str2 i (proc (string-ref str i)))))))
  
;; Return a new string that is the upper-case version of STR

(define (upcase str)
  (string-map char-upcase str))

;; A weak imitation of CL push.  Insert X at the front of the list
;; stored in PLACE.  When PLACE is a symbol, this macro expands to
;;
;;   (set! place (cons x place))
;;
;; When PLACE is a list whose first element is a symbol,
;;
;;   (push! x (field s))
;;
;; it essentially expands to
;;
;;   (set-field! s (cons x (field s)))
;;
;; but S is only evaluated once.  This works well with the structures
;; defined by `define-struct'.

(define-macro (push! x place)
  (cond ((symbol? place)
	 `(set! ,place (cons ,x ,place)))
	((and (list? place) (symbol? (car place)))
	 (let ((getter (car place))
	       (setter (symbol-append 'set- (car place) '!))
	       (stmp (gensym)))
	   `(let ((,stmp ,(cadr place)))
	      (,setter ,stmp (cons ,x (,getter ,stmp))))))
	(else
	 (error "bad push! syntax"))))

;; Split STR into its syllables.  Syllable delimiters are "_", "-" and
;; the transition from lower to upper case.  Thus, the strings
;; "Holz_Hacker", "Holz-Hacker" and "HolzHacker" have all three the
;; syllables ("Holz" "Hacker").

(define (syllables str)
  (if (symbol? str)
      (syllables (symbol->string str))
      (do ((syls '())
	   (start 0)
	   (pos 0 (1+ pos))
	   (was-lower #f)
	   (len (string-length str)))
	  ((>= pos len) (reverse (cons (substring str start pos) syls)))
	(let ((ch (string-ref str pos)))
	  (cond ((memq ch '(#\- #\_))
		 (push! (substring str start pos) syls)
		 (set! start (1+ pos)))
		((and was-lower (char-upper-case? ch))
		 (push! (substring str start pos) syls)
		 (set! start pos)))
	  (set! was-lower (char-lower-case? ch))))))

    
;; Concatenate the strings in L (a list) with DEL between them

(define (concat-strings l del)
  (cond ((null? l)
	 "")
	((null? (cdr l))
	 (car l))
	(else
	 (string-append* (car l) del (concat-strings (cdr l) del)))))

;; Execute the BODY once for each element of the list L.  Within BODY,
;; the variable V is bound to the current list element.

(define-macro (do-list v l . body)
  `(for-each (lambda (,v) ,@body) ,l))

;; Print formatted output to the current error port.

(define (info fmt . rest)
  (apply mini-format (current-error-port) fmt rest))

;; Like INFO, but also print the position of the current input port.
;; Then throw a `processing-error'.  That is, the execution of
;; gen-nodes is aborted.

(define (err fmt . rest)
  (let ((port (current-input-port)))
    (info "~a:~a: " (port-filename port) (port-line port))
    (apply info fmt rest)
    (info "~%")
    (throw 'processing-error)))

;; Print formatted output to the current output port.  The current
;; output port is usually the generated file when this function is
;; called.

(define (emit fmt . rest)
  (apply mini-format (current-output-port) fmt rest))

;;; Input

;; The input files are evaluated as regular Scheme code.  Definition
;; statements like `defnode', etc are really macros.  These macros
;; just store away the interesting bits of their arguments and thus
;; build an internal, `abstract' representation of the things defined
;; in the input files.  When the input files have been sucessfully
;; read, this internal representation is used to generate the
;; requested output.

;; The input files are evaluated in the "gen-nodes" module.  This sets
;; them on an equal footing with the code in this file.  They see the
;; same functions and can access the same variables.  That means that
;; you can write arbitrarily complicated code in the input files
;; themselves.  Currently, the support to cleanly extend gen-nodes is
;; weak, tho.

(define this-module (current-module))

(define my-eval
  (if (false-if-exception (eval #t))
      eval
      (lambda (form) (eval form this-module))))

;; Evaluate the code in the file NAME.  Add the directory of NAME to
;; the front of %load-path while doing so.  This allows the input file
;; to specify included files relative to itself.

(define (include name)
  (let ((dir (or "." (dirname name)))   ; XXX - implement dirname
	(oldpath %load-path)
	(oldmodule (current-module)))
    (dynamic-wind
     (lambda () 
       (push! dir %load-path)
       (set-current-module this-module))
     (lambda ()
       (primitive-load-path name))
     (lambda ()
       (set! %load-path oldpath)
       (set-current-module oldmodule)))))

;; The rest of the provided statements are quite simple.  If the
;; macros are too restrictive (because you want to compute the defined
;; features with custom Scheme code, say), you can always use the
;; corresponding `register-*' functions.

(define-macro (chunk name . opts)
  `(start-chunk ',name ',opts))

;; XXX - the following two need to be rewritten to allow more precise
;; placement of custom code.

(define (header-add . lines)
  (register-add-lines 'header lines))

(define (impl-add . lines)
  (register-add-lines 'impl lines))

(define-macro (defctype name . opts)
  `(register-ctype ',name ',opts))

(define-macro (defnode name bases slots . opts)
  `(register-node ',name ',bases ',slots ',opts))

(define-macro (deflist name node . opts)
  `(register-list ',name ',node ',opts))

(define-macro (defextension node slots . opts)
  `(register-extension ',node ',slots ',opts))

(define-macro (defgeneric name . opts)
  `(register-generic ',name ',opts))

(define-macro (defmethods name methods . opts)
  `(register-methods ',name ',methods ',opts))

;; XXX - creators and unions are very VAUL specific and should
;; probably be abstracted into a general mechanism for extending the
;; code generation of gen-nodes from the input files.

(define-macro (defcreator name . opts)
  `(register-creator ',name ',opts))

(define-macro (defunion name . opts)
  `(register-union ',name ',opts))

;;; Data structures for the internal representation

;; A chunk holds them all together.

(define-struct chunk
  name
  pointer-fmt-func
  struct-fmt-func
  kind-fmt-func
  creator-fmt-func
  opts
  (nodes '())
  (ctypes '())
  (extensions '())
  (generics '())
  (num-generics 0)
  (num-methods 0)
  (methods '())
  (base-nodes '())      ; the nodes that either don't have a base
                        ; at all or whose base is in another chunk
  (creators '())
  (unions '()))

(define (chunk-pointer-fmt ch . rest)
  (apply (chunk-pointer-fmt-func ch) rest))

(define (chunk-struct-fmt ch . rest)
  (apply (chunk-struct-fmt-func ch) rest))

(define (chunk-kind-fmt ch . rest)
  (apply (chunk-kind-fmt-func ch) rest))

(define (chunk-creator-fmt ch . rest)
  (apply (chunk-creator-fmt-func ch) rest))

;; A `type' is either a ctype that maps directly to a type defined in
;; C++ (like "int"), or a node.

(define-struct ctype
  name
  chunk
  opts)

(define-struct node
  name
  base
  slots
  chunk
  opts
  (derived '())        ; All nodes that have been derived from this one,
                       ; and are in the same chunk as this one.
  (referenced '()))    ; All nodes of the same chunk that have a slot
		       ; pointing to this one

;; Return a list of all slots of a node, both inherited ones and
;; directly specified ones. The inherited slots precede the directly
;; specified ones in the list.

(define (node-effective-slots node)
  (if node
      (append (node-effective-slots (node-base node))
	      (node-slots node))
      '()))

(define-struct slot
  name
  typesym      ; either the name of the type, or the type itself
  opts)

(define (find-slot name node)
  (or (find-if (lambda (s) (eq? (slot-name s) name))
	       (node-effective-slots node))
      (err "node ~a has no ~a slot" (node-name node) name)))

;; Return the real type for the slot S.  If the typesym of S is a
;; symbol, replace it with the real type.  That is, slot types are
;; searched for only when they are needed.  This allows one to refer
;; to types that have not been defined.

(define (slot-type s)
  (let ((t (slot-typesym s)))
    (cond ((symbol? t)
	   (set! t (find-type t))
	   (set-slot-typesym! s t)))
    t))

;; Is T the C++ "void" type?  A void type is one that is flagged with
;; the option `(void #t)'.  The canonical void type is predefined
;; below, after `register-ctype' has been defined.

(define (type-void? t)
  (and (ctype? t) (get-opt-val (ctype-opts t) 'void #f)))

;; Return the C++ rendition of T as a string.

(define (type->c t)
  (cond ((ctype? t)
	 (get-opt-val (ctype-opts t) 'cname (ctype-name t)))
	((node? t)
	 (node-pointer t))))

;; Return the C++ rendition of a declaration that declares V (a
;; symbol) to be of type T.

(define (decl->c t v)
  (string-append* (type->c t) " " v))

(define-struct extension
  node
  slots
  chunk
  opts)

(define-struct generic
  name
  ret-type
  args        ; ((type . name) ...)
  id          ; a unique id (within the chunk).  This is necessary
              ; because there can be more than one generic with the
              ; same name but with different args.
  chunk
  opts)

(define-struct methods
  generic
  nodes       ; the nodes that we should generate specialized methods for
  id
  chunk
  opts)

;; Return the additional arguments of GEN in declaration form,
;; suitable for a prototype.  The returned string is either empty or
;; starts with a ",".  That makes it suitable to be tacked right on
;; the mandatory first argument of a generic function.

(define (generic-args-decl gen)
  (apply string-append* (map (lambda (a)
			      (string-append* ", " (decl->c (car a) (cdr a))))
			    (generic-args gen))))

;; Return the additional arguments of GEN in a form suitable for a
;; function call.  This is, just the names, separated by commas.
;; Like, the declaration form, the returned string is either empty or
;; starts with a ","

(define (generic-arg-names gen)
  (apply string-append* (map (lambda (a)
			      (string-append* ", " (cdr a)))
			    (generic-args gen))))

;; Is GEN a member function instead of an ordinary global function ?

(define (generic-class-member? gen)
  (pair? (generic-name gen)))

;; Return the prefix of the qualified name of GEN.  For global
;; functions this is the empty string, for a member function, it is
;; the the string formed by concatenating the class names separated
;; with "::" and appending a final "::".

(define (generic-class-prefix gen)
  (if (generic-class-member? gen)
      (apply string-append* (map (lambda (c)
				  (string-append* c "::"))
				(butlast (generic-name gen) 1)))
      ""))

;; Return the complete C++ name of GEN as a string.

(define (generic-func-name gen)
  (let ((n (generic-name gen)))
    (if (pair? n)
	(concat-strings n "::")
	n)))

;; Return the name of a method of GEN.  For generic member functions,
;; this does not include the class prefix.

(define (generic-method-name gen meth)
  (let ((n (generic-name gen))
	(prefix (get-opt-val (methods-opts meth) 'prefix "m_")))
    (string-append* prefix (if (pair? n)
			      (car (last-pair n))
			      n))))

(define-struct creator name opts)
(define-struct union name opts)

(define all-chunks '())    ; the alist of all chunks seen so far
(define all-types '())     ; the alist of all types in all chunks
(define all-generics '())  ; the list of all generics in all chunks

(define current-chunk #f)  ; the chunk that has been started most recently

;; Return the chunk named NAME or issue an error.  Issuing an error
;; aborts the program, so this function always returns a valid chunk,
;; or does not return at all.

(define (find-chunk name)
  (let ((cell (assq name all-chunks)))
    (if cell
	(cdr cell)
	(err "unknown chunk: ~a" name))))

;; Try to find the type named NAME.  When it does not exist, return
;; `#f'.

(define (find-type? name)
  (let ((cell (assq name all-types)))
    (if cell (cdr cell) #f)))

;; Find the type named NAME or issue an error.

(define (get-dummy-type name)
  (pk 'dummy name)
  (register-node name '() '() '()))

(define (find-type name)
  (let ((t (find-type? name)))
    (or t
	; (get-dummy-type name)
	(err "unknown type: ~a" name))))

(define (build-node-xref)
  (do-list nn all-types
    (let ((n (cdr nn)))
      (if (node? n)
	  (do-list s (node-slots n)
	    (let ((t (slot-type s)))
	      (if (node? t)
		  (push! n (node-referenced t)))))))))
  
;; Find the type named NAME and make sure that it is a node type.

(define (find-node name)
  (let ((t (find-type name)))
    (if (not (node? t)) (err "not a node type: ~a" name))
    t))

;;; Find the generic named NAME that has args ARGS.  ARGS should be
;;; the unprocessed list from the options.  That is, it should contain
;;; the type names and not the types themself.

(define (find-generic name args)
  (define (match? g)
    (and (eq? (generic-name g) name)
	 (or (not args) 
	     (equal? (get-opt (generic-opts g) 'args '()) args))))
  (or (find-if match? all-generics)
      (err "unknown generic ~a ~a" name args)))

;;; Building the internal representation

(define (start-chunk name opts)

  (define (get-fmt-func fmt-key func-key def)
    (let ((func (get-opt-val opts func-key #f)))
      (if func
	  (my-eval func)
	  (let ((fmt (get-opt-val opts fmt-key def)))
	    (lambda (sym) (@@ fmt sym))))))
  
  (let ((c (make-chunk 
	    name
	    (get-fmt-func 'pointer-fmt 'pointer-fmt-func "n~a")
	    (get-fmt-func 'struct-fmt 'struct-fmt-func "s~a")
	    (get-fmt-func 'kind-fmt 'kind-fmt-func "nk~a")
	    (get-fmt-func 'creator-fmt 'creator-fmt-func "m~a")
	    opts)))
    (set! all-chunks (acons name c all-chunks))
    (set! current-chunk c)
    c))

(define (register-node name bases slots opts)
  (let* ((base-node (if (null? bases) #f (find-node (car bases))))
	 (slots (map 
		 (lambda (s)
		   (make-slot (cadr s) (car s) (cddr s)))
		 slots))
	 (node (make-node name base-node slots current-chunk opts)))
    (if (find-type? name)
	(err "type ~a already defined" name))

    ; When this node has a base node, and it is in the same chunk, add
    ; this node to the `derived' list of the base node.  Else, add it
    ; to the list of `base-nodes' of the current chunk.

    (if (and base-node (eq? (node-chunk base-node) current-chunk))
	(push! node (node-derived base-node))
	(push! node (chunk-base-nodes current-chunk)))

    (push! node (chunk-nodes current-chunk))
    (push! (cons name node) all-types)
    node))

(define (force-slot-types ch)
  (do-list n (chunk-nodes ch)
    (do-list s (node-slots n)
      (slot-type s))))

(define (register-ctype name opts)
  (let ((t (make-ctype name current-chunk opts)))
    (if current-chunk (push! t (chunk-ctypes current-chunk)))
    (push! (cons name t) all-types)
    t))

;; We predefine the "void" type, because we need it as the default for
;; the return type of generic functions.  No other types are
;; predefined, not even "int".

(defctype void
  (void #t))

(define void-type (find-type 'void))

(define (register-extension node-name slots opts)
  (let* ((node (find-node node-name))
	 (slots (map (lambda (s)
		       (make-slot (cadr s) (car s) (cddr s)))
		     slots))
	 (ext (make-extension node slots current-chunk opts)))
    (push! ext (chunk-extensions current-chunk))
    ext))

(define (register-generic name opts)
  (let* ((return (find-type (get-opt-val opts 'return 'void)))
	 (args (map (lambda (a)
		      (cons (find-type (car a)) (cadr a)))
		    (get-opt opts 'args '())))
	 (gen (make-generic
	       name return args (chunk-num-generics current-chunk)
	       current-chunk opts)))
    (push! gen (chunk-generics current-chunk))
    (push! gen all-generics)
    (set-chunk-num-generics! current-chunk
			     (1+ (chunk-num-generics current-chunk)))
    (let ((methods (get-opt opts 'methods #f)))
      (if methods
	  (register-methods name methods opts)))
    gen))

(define (register-methods name methods opts)
  (let* ((gen (find-generic name (get-opt opts 'args #f)))
	 (nodes (map find-node methods))
	 (m (make-methods gen nodes (chunk-num-methods current-chunk)
			  current-chunk opts)))
    (push! m (chunk-methods current-chunk))
    (set-chunk-num-methods! current-chunk
			    (1+ (chunk-num-methods current-chunk)))
    m))

(define (register-creator name opts)
  (let ((c (make-creator name opts)))
    (push! c (chunk-creators current-chunk))
    c))

(define (register-union name opts)
  (let ((u (make-union name opts)))
    (push! u (chunk-unions current-chunk))
    u))

(define add-lines '())

(define (register-add-lines cmd lines)
  (if (and (eq? arg-cmd cmd)
	   (eq? (chunk-name current-chunk) arg-chunk))
      (set! add-lines (append add-lines lines))))
  
;;; Generating C++ code

;; Some shortcuts for often needed things.

;; Pass ARGS to emit and output a newline.

(define (@ . args)
  (apply emit args)
  (emit "~%"))

;; Return the result of formatting REST according to FMT as a string.

(define (@@ fmt . rest)
  (apply mini-format #f fmt rest))

;; The enum literal that denotes the kind of N

(define (node-kind n) 
  (chunk-kind-fmt (node-chunk n) (node-name n)))

;; The struct name for the representation of N

(define (node-struct n)
  (chunk-struct-fmt (node-chunk n) (node-name n)))

;; A pointer to the struct for N

(define (node-pointer n)
  (chunk-pointer-fmt (node-chunk n) (node-name n)))

;; The name of a function for creating a instance of N

(define (node-creator n)
  (chunk-creator-fmt (node-chunk n) (node-name n)))

;; Apply PROC to each node of the chunk CH in a certain order.  The
;; order is so that all nodes derived from a certain node N come
;; directly after that node, without intervening nodes that are not
;; derived from N.

(define (visit-chunk-nodes proc ch)
  (define (visit n)
    (proc n)
    (for-each visit (node-derived n)))
  (for-each visit (chunk-base-nodes ch)))

(define (emit-lines lines)
  (cond ((not (null? lines))
	 (@ "")
	 (do-list l lines
	   (@ "~a" l)))))

;; Return all values of the option KEY in the list OPTS.  When there
;; is no KEY option, return DEF.  The values of an option is the cdr
;; of the sublist that starts with KEY.  For example
;;
;;   (get-opt '((foo 1 2) (bar 3 4)) 'bar #f)
;;   =>
;;   (3 4)
;;
;;   (get-opt '((foo 1 2) (bar 3 4)) 'blubb #f)
;;   =>
;;   #f

(define (get-opt opts key def)
  (let ((cell (assq key opts)))
    (if cell (cdr cell) def)))

;; Get only the first value of option KEY.  Return DEF when there is
;; no KEY option.  For example
;;
;;   (get-opt-val '((foo 1) (bar 2)) 'bar #f)
;;   =>
;;   2

(define (get-opt-val opts key def)
  (let ((tail (get-opt opts key (list def))))
    (car tail)))

;; Creator support.

;; Distinguish between the `traditional' and `modern' forms of
;; NODE-INIT (sans node type) specifications.  The modern form can
;; have options, while the traditional one can't.
;;
;; Modern:      (((SLOT INIT) ...) OPTION...) and (() OPTION...)
;; Traditional: ((SLOT INIT) ...)             and ()

(define (modern-node-init? node-init)
  (and (not (null? node-init)) 
       (or (null? (car node-init))
	   (pair? (caar node-init)))))

;; Canonicalize a NODE-INIT.  The traditional forms gets rewritten
;; into the modern one, with no options.

(define (canonicalize-node-init node-init)
  (if (modern-node-init? node-init)
      node-init
      (cons node-init '())))

;; Accessors for the canonical form

(define (node-init-slots node-init)
  (car node-init))

(define (node-init-options node-init)
  (cdr node-init))

;; Merge two slot-init/options in canonical form.  Merging is done by
;; appending the base information to the node information so that the
;; latter overrides the former.

(define (merge-node-inits base derived)
  (cons (append (node-init-slots derived)
		(node-init-slots base))
	(append (node-init-options derived)
		(node-init-options base))))

;; Collect all slot-inits and options pertaining to NODE.  The result
;; is a list of merged node-inits in canonical form for NODE and its
;; bases.  All combinations are formed.

(define (find-inits node inits)
  (if node
      (let ((n-inits (map (lambda (i) (canonicalize-node-init (cdr i)))
			  (remove-if 
			   (lambda (i)
			     (not (eq? (car i) (node-name node))))
			   inits)))
	    (b-inits (find-inits (node-base node) inits)))
	(if (null? n-inits)
	    b-inits
	    (apply append 
		   (map (lambda (ni)
			  (map (lambda (bi) (merge-node-inits bi ni)) b-inits))
			n-inits))))
      '((()))))

;; From the `inits' option of the creator, INITS, find the init value
;; for the SLOT of NODE.  When there is no init value specified,
;; return `#f'.  SLOT does not need to be a direct slot of NODE.  When
;; it is inherited from some base of NODE, the init values for that
;; base are consulted.

;; Inits can be:
;;
;; #f:   no init, use the slot as parameter
;;
;; symbol/string: 
;;       constant init, no parameter
;;
;; (wrap ((TYPE NAME) ...) STRING):
;;       computed init, use TYPE and NAME for the parameter and 
;;       the string as the constructor argument.

(define (find-slot-init slot node-init)
  (and=> (assq (slot-name slot) (node-init-slots node-init)) cadr))

;; Return the declaration of the parameter for INIT, if it needs one.
;; Return `#f' when INIT does not need one.  SLOT is the slot for this
;; initializer.

(define (init-decl init slot)
  (cond ((eq? #f init)
	 (decl->c (slot-type slot) (slot-name slot)))
	((or (string? init) (symbol? init))
	 #f)
	((list? init)
	 (case (car init)
	   ((wrap)
	    (let ((parms (cadr init)))
	      (concat-strings (map (lambda (p)
				     (decl->c (find-type (car p)) (cadr p)))
				   parms)
			      ", ")))
	   (else
	    (err "unknown1 init ~a" init))))
	(else
	 (err "unknown2 init ~a" init))))

;; Return the constructor argument for INIT/SLOT.

(define (init-arg init slot)
  (cond ((eq? #f init)
	 (slot-name slot))
	((or (string? init) (symbol? init))
	 init)
	((list? init)
	 (case (car init)
	   ((wrap)
	    (caddr init))
	   (else
	    (err "unknown3 init ~a" init))))
	(else
	 (err "unknown4 init ~a" init))))

;; Return a string containing a ","-separated list of all
;; init-parameter declarations of N.

(define (creator-decls n inits)
  (concat-strings (remove-if not
			     (map (lambda (s)
				    (init-decl (find-slot-init s inits) s))
				  (node-effective-slots n)))
		  ", "))

(define (creator-args n inits)
  (concat-strings (map (lambda (s)
			 (init-arg (find-slot-init s inits) s))
		       (node-effective-slots n))
		  ", "))

(define (creator-construct-extras node-init)
  (reverse
   (pick-mappings (lambda (opt)
		    (and (eq? 'construct-extra (car opt))
			 (cadr opt)))
		  (node-init-options node-init))))

(define (emit-list-header node info)
  (define (i->t i) (type->c (slot-type (find-slot i node))))
  (let ((first-t (i->t (car info)))
	(rest-t (i->t (cadr info))))
    (@ "  ~a get_successor_element (~a element)" first-t first-t)
    (@ "  { return ~a(IR_GenericList::get_successor_element (element)); }"
       first-t)))

(define (emit-list-impl node info)
  (define (i->t i) (type->c (slot-type (find-slot i node))))
  (let ((first-t (i->t (car info)))
	(rest-t (i->t (cadr info))))
    (@ "")
    (@ "~a" first-t)
    (@ "~a::get_successor_element (~a element)" (node-struct node) first-t)
    (@ "{")
    (@ "  ~a l = this;" (node-pointer node))
    (@ "  while (l && l->~a != element)" (car info))
    (@ "    l = l->~a;" (cadr info))
    (@ "  return l? l->~a : 0;" (car info))
    (@ "}")))

(define (slot-init s)
  (let ((ifilt (get-opt-val (slot-opts s) 'init-filter #f)))
    (or ifilt (slot-name s))))

;; Emit the C++ header for chunk CH.

(define (emit-header ch)
  (@ "// generated by gen-nodes from `~a'.  Do not edit." arg-in-file)
  (@ "")
  (@ "#ifndef ~a_H" (upcase (chunk-name ch)))
  (@ "#define ~a_H" (upcase (chunk-name ch)))

  (force-slot-types ch)

  (emit-lines add-lines)

  ;; Emit a forward declaration of the struct name for every node, a
  ;; typedef for the pointer type and the real node kind, that is
  ;; unique among all nodes in all chunks.  The forward reference is
  ;; needed so that we can freely refer to all node structs when later
  ;; defining them for real.

  ;; Additionally, emit the prototype for a reversion function, if
  ;; this node type is used as a list.  The reversion feature will
  ;; probably go away when we implement a more sophisticated list
  ;; mechanism for AIRE.

  (@ "")
  (@ "extern tree_chunk_info ~a_chunk_info;" (chunk-name ch));
  (do-list ctype (reverse (chunk-ctypes ch))
    (@ "extern tree_ctype_info ~a_ctype_info;" (ctype-name ctype)))
  (do-list node (reverse (chunk-nodes ch))
    (@ "struct ~a;" (node-struct node))
    (@ "typedef ~a *~a;" (node-struct node) (node-pointer node))
    (@ "extern tree_kind_info ~a_kind_info;" (node-kind node))
    (@ "#define ~a (&~a_kind_info)" (node-kind node) (node-kind node))
    (if (get-opt-val (node-opts node) 'reverse #f)
	(@ "~a reverse (~a);" (node-pointer node) (node-pointer node))))

  ;; Emit the actual node struct definitions.

  (do-list node (reverse (chunk-nodes ch))
    (@ "")
    (@ "struct ~a : ~a {" 
       (node-struct node)
       (if (node-base node) 
	   (node-struct (node-base node))
	   (get-opt-val (chunk-opts ch) 'default-base 'tree_base_node)))
    (do-list s (node-slots node)
      (@ "  ~a;" (decl->c (slot-type s) (slot-name s))))
    (@ "")
    (@ "  ~a (~a) " 
       (node-struct node)
       (concat-strings (map (lambda (s)
			      (decl->c (slot-type s) (slot-name s)))
			    (node-effective-slots node))
		       ", "))

    ;; The list of all inits is first put into a list and then emitted
    ;; as a whole to get the C++ syntax strangeness right.
    (let ((inits '())
	  (b (node-base node)))
      (if b
	  (set! inits (list (@@ "~a (~a)"
				(node-struct b)
				(concat-strings
				 (map slot-name (node-effective-slots b))
				 ", ")))))
      (do-list s (node-slots node)
	(set! inits (cons (@@ "~a (~a)" (slot-name s) (slot-init s))
			  inits)))
      (cond ((not (null? inits))
	     (@ "  : ~a" (concat-strings (reverse inits) ",\n    "))))
      (@ "    { }"))

    (@ "")
    (@ "  tree_kind kind ();")
    (do-list e (get-opt (node-opts node) 'extra '())
      (@ "  ~a" e))
    (let ((list-info (get-opt (node-opts node) 'list #f)))
      (if list-info (emit-list-header node list-info)))
    (@ "};"))

  ;; Emit the prototypes for the generic functions and their methods,
  ;; but only for generics that are not member functions.  The user is
  ;; supposed to supply its own prototypes with the class definition.

  (do-list gen (reverse (chunk-generics ch))
    (@ "")
    (@ "typedef ~a (~a*~a_generic_~a_mtype) (tree_base_node*~a);"
       (type->c (generic-ret-type gen))
       (generic-class-prefix gen)
       (chunk-name ch)
       (generic-id gen)
       (generic-args-decl gen))
    (@ "extern tree_generic<~a_generic_~a_mtype> ~a_generic_~a;"
       (chunk-name ch) (generic-id gen) (chunk-name ch) (generic-id gen))
    (cond ((not (generic-class-member? gen))
	   (let ((args-decl (generic-args-decl gen))
		 (cret (type->c (generic-ret-type gen))))
	     (@ "~a ~a (tree_base_node *~a);"
		cret
		(generic-func-name gen)
		args-decl)))))

  (do-list meth (reverse (chunk-methods ch))
    (let* ((gen (methods-generic meth))
	   (args-decl (generic-args-decl gen))
	   (cret (type->c (generic-ret-type gen)))
	   (name (generic-method-name gen meth)))
      (do-list node (reverse (methods-nodes meth))
	(@ "~a ~a (~a ~a);" cret name (node-pointer node) args-decl))))

  ;; Emit the necessary structs and inline accessors for node
  ;; extensions.

  ;; XXX - Maybe it is a good idea to also emit such accessors for the
  ;; regular node slots.

  (do-list ext (reverse (chunk-extensions ch))
    (@ "")
    (@ "struct ~a_~a_ext : tree_prop {"
       (chunk-name ch) (node-struct (extension-node ext)))
    (@ "  ~a_~a_ext ();" (chunk-name ch) (node-struct (extension-node ext)))
    (@ "  tree_prop_info *get_info ();")
    (do-list s (reverse (extension-slots ext))
      (@ "  ~a;" (decl->c (slot-type s) (slot-name s))))
    (@ "};")
    (@ "")
    (@ "struct ~a_~a_ext *get_~a_ext (~a n);"
       (chunk-name ch) (node-struct (extension-node ext))
       (chunk-name ch) (node-pointer (extension-node ext)))
    (do-list s (reverse (extension-slots ext))
      (@ "static inline ~a& ~a (~a n) { return get_~a_ext (n)->~a; }"
	 (type->c (slot-type s))
	 (slot-name s)
	 (node-pointer (extension-node ext))
	 (chunk-name ch)
	 (slot-name s))))

  ;; Emit definitions for all creator structs.

  (do-list c (reverse (chunk-creators ch))
    (@ "")
    (let ((base (get-opt-val (creator-opts c) 'base #f))
	  (chunks (get-opt (creator-opts c) 'chunks '()))
	  (inits (get-opt (creator-opts c) 'inits '())))
      (@ "struct ~a~a {" 
	 (creator-name c) (if base (@@ " : ~a" base) ""))
      (do-list c chunks
	(do-list n (chunk-nodes (find-chunk c))
	  (do-list i (find-inits n inits)
	    (@ "  ~a ~a (~a);" 
	       (node-pointer n) (node-creator n)
	       (creator-decls n i)))))
      (@ "};")))

  ;; And finally the unions.

  (do-list u (reverse (chunk-unions ch))
    (@ "")
    (@ "union ~a {" (union-name u))
    (do-list c (get-opt (union-opts u) 'chunks '())
      (do-list n (chunk-nodes (find-chunk c))
	(@ "  ~a ~a;" (node-pointer n) (node-name n))))
    (@ "};"))

  (@ "")
  (@ "void init_~a_chunk ();" (chunk-name ch))
  
  (@ "")
  (@ "#endif"))


;; Return the node that is directly or indirectly derived from NODE
;; and comes last in the sequence of nodes produced by
;; visit-chunk-nodes.  All nodes derived from NODE have kind ids that
;; lie between the kind id of NODE and the kind id of the node
;; returned by this function.

(define (last-derived node)
  (if (null? (node-derived node))
      node
      (last-derived (car (last-pair (node-derived node))))))

;; Follow the chain of base nodes of NODE and return the first node
;; that does not belong the same chunk as NODE.  Return `#f' when
;; there is no such node.

(define (chunk-base node)
  (let ((base (node-base node)))
    (cond ((not base)
	   #f)
	  ((not (eq? (node-chunk base) (node-chunk node)))
	   base)
	  (else
	   (chunk-base base)))))

;; Follow the chain of base nodes of NODE and return the first node
;; that is a member of the list NODES.  Return `#f' when there is no
;; such node.

(define (find-member-base node nodes)
  (if (member node nodes)
      node
      (let ((base (node-base node)))
	(if base
	    (find-member-base base nodes)
	    #f))))

;; Emit the C++ implementation of the various things in the chunk CH.

(define (emit-impl ch)

  (define (visit-chunk-nodes-counted proc chunk)
    (let ((i 0))
      (visit-chunk-nodes
       (lambda (node)
	 (let ((res (proc node i)))
	   (set! i (1+ i))
	   res))
       chunk)))

  (define used-chunks '())

  (define (use-chunk uch)
    (if (and uch (not (eq? uch ch)) (not (memq uch used-chunks)))
	(set! used-chunks (cons uch used-chunks))))

  (@ "// generated by gen-nodes from `~a'.  Do not edit." arg-in-file)

  (force-slot-types ch)

  (emit-lines add-lines)

  ;; Emit info structs for the defined C types.

  (do-list t (chunk-ctypes ch)
    (@ "tree_ctype_info ~a_ctype_info = {" (ctype-name t))
    (@ "  -1,")
    (@ "  \"~a\"," (ctype-name t))
    (@ "  ~a," (get-opt-val (ctype-opts t) 'printer "NULL"))
    (@ "};"))

  ;; Emit the run-time type info for all node kinds.

  (visit-chunk-nodes-counted
   (lambda (node i)
     (use-chunk (and=> (chunk-base node) node-chunk))
     (let ((slots (node-slots node))
	   (n-links 0)
	   (n-slots 0))
       (@ "")
       (@ "static tree_slot_info ~a_slot_info[] = {" (node-name node))
       (do-list s slots
	 (let ((t (slot-type s)))
	   (cond
	    ((node? t)
	     (@ "  { ~a, \"~a\", (tree_base_node*tree_base_node::*)&~a::~a }," 
		(node-kind t) (slot-name s) (node-struct node) (slot-name s))
	     (set! n-links (1+ n-links))))))
       (set! n-slots n-links)
       (do-list s slots
	 (let ((t (slot-type s)))
	   (cond
	    ((ctype? t)
	     (@ "  { (tree_kind_info*)&~a_ctype_info, \"~a\", (tree_base_node*tree_base_node::*)&~a::~a }," 
		(ctype-name t) (slot-name s) (node-struct node) (slot-name s))
	     (set! n-slots (1+ n-slots))))))
	 
       (@ "};")
       (@ "")
       (@ "tree_kind_info ~a_kind_info = {" (node-kind node))
       (@ "  ~a," i)    ; kind_id
       (@ "  \"~a\"," (node-name node))
       (@ "  &~a_chunk_info," (chunk-name ch))
       (@ "  ~a," (let ((b (node-base node)))
		    (if b (node-kind b) "0")))
       (@ "  ~a," (node-kind (last-derived node)))
       (@ "  ~a," (let ((b (chunk-base node)))
		    (if b (node-kind b) "0")))
       (@ "  ~a, ~a, ~a_slot_info," n-links n-slots (node-name node))
       (@ "  sizeof(~a)" (node-struct node))
       (@ "};")))
   ch)

  ;; Emit the implementation of the node structs.  Currently, this is
  ;; only the virtual function for returning the node kind.  The
  ;; reversion function is also emitted here, if one has been
  ;; requested.

  (do-list node (reverse (chunk-nodes ch))
    (@ "")
    (@ "tree_kind ~a::kind () { return ~a; }"
       (node-struct node) (node-kind node))
    (let ((revslot (get-opt-val (node-opts node) 'reverse #f)))
      (if revslot
	  (let ((p (node-pointer node)))
	    (@ "")
	    (@ "~a reverse (~a n)" p p)
	    (@ "{")
	    (@ "  ~a r = NULL;" p)
	    (@ "  while (n)")
	    (@ "    {")
	    (@ "      ~a n2 = n->~a;" p revslot)
	    (@ "      n->~a = r;" revslot)
	    (@ "      r = n;")
	    (@ "      n = n2;")
	    (@ "    }")
	    (@ "  return r;")
	    (@ "}"))))
    (let ((list-info (get-opt (node-opts node) 'list #f)))
      (if list-info (emit-list-impl node list-info))))

  ;; Now for the interesting part, the dispatching mechanism of the
  ;; generic functions.  We climb the inheritance hierarchy
  ;; chunk-by-chunk until we have found a chunk that we have a method
  ;; table for.  Then we index this method table with the constant
  ;; kind_id of the last node obtained while climbing.  When there
  ;; indeed is a function at this location, we call it.

  ;; The code is a little bit complicated by the fact that we want to
  ;; deal with both non-member function pointers and member function
  ;; pointers in a mostly uniform way, but you can't cast a
  ;; member-pointer into `void*' and back.

  ;; As of now, the code is in a template class, so you wont find much
  ;; interesting code here.  It's in "tree-supp.cc".

  (do-list gen (reverse (chunk-generics ch))
    (@ "")
    (@ "tree_generic<~a_generic_~a_mtype> ~a_generic_~a;"
       (chunk-name ch) (generic-id gen) (chunk-name ch) (generic-id gen))
    (@ "")
    (@ "~a ~a (tree_base_node *__node__~a)"
       (type->c (generic-ret-type gen))
       (generic-func-name gen)
       (generic-args-decl gen))
    (@ "{")
    (@ "  ~a_generic_~a_mtype __method__ = "
       (chunk-name ch) (generic-id gen))
    (@ "    ~a_generic_~a.find (__node__->kind ());"
       (chunk-name ch) (generic-id gen))
    (@ "  ~a(~a*__method__) (__node__~a);"
       (if (type-void? (generic-ret-type gen)) "" "return ")
       (if (generic-class-member? gen) "this->" "")
       (generic-arg-names gen))
    (@ "}"))

  ;; Emit method tabs for every set of methods defined.

  (do-list meth (reverse (chunk-methods ch))
    ;; Find the list of chunks that have methods.
    (let* ((gen (methods-generic meth))
	   (gen-ch (chunk-name (generic-chunk gen)))
	   (chunks (uniq (map node-chunk (methods-nodes meth)))))

      (use-chunk (generic-chunk gen))
      (for-each use-chunk chunks)

      ;; Then emit a table for each of these chunks
      (do-list chunk chunks
	(@ "")
	(@ "static ~a_generic_~a_mtype mtab_~a_~a[] = {"
	   gen-ch (generic-id gen) (methods-id meth) (chunk-name chunk))

	;; For each node, find the method that applies to it.
	(visit-chunk-nodes
	 (lambda (node)
	   (let ((base (find-member-base node (methods-nodes meth))))
	     (if base
		 ;; The & operator is necessary here. gcc bug?
		 (@ "  (~a_generic_~a_mtype) ((~a (~a*)(~a~a))&~a),  // ~a"
		    gen-ch (generic-id gen)
		    (type->c (generic-ret-type gen))
		    (generic-class-prefix gen)
		    (node-pointer base)
		    (generic-args-decl gen)
		    (generic-method-name gen meth)
		    (node-name node))
		 (@ "  0, // ~a"
		    (node-name node)))))
	 chunk)
	(@ "};"))

      ;; Collect all method tables into a `partial generic'.  These
      ;; partial generics will be merged at run-time.
      (@ "static tree_chunk_tab ctab_~a[~a] = {" 
	 (methods-id meth) (length chunks))
      (do-list chunk chunks
	(@ "  { &~a_chunk_info, ~a, mtab_~a_~a },"
	   (chunk-name chunk) (length (chunk-nodes chunk))
	   (methods-id meth) (chunk-name chunk)))
      (@ "};")
      (@ "")
      ;; (@ "static tree_generic<~a_generic_~a_mtype> pgen_~a (~a, ctab_~a);"
      ;;    gen-ch (generic-id gen) (methods-id meth) 
      ;;    (length chunks) (methods-id meth)
      ))

  ;; Emit the functions that return the extension struct for the
  ;; inline accessors emitted in the header.  Also emit the
  ;; constructor for the extension struct.

  (do-list ext (reverse (chunk-extensions ch))
    (let* ((n (extension-node ext))
	   (n-struct (node-struct n))
	   (c-name (chunk-name ch)))
      (use-chunk (chunk-base n))
      (@ "")
      (@ "~a_~a_ext::~a_~a_ext ()" c-name n-struct c-name n-struct)
      (@ "{")
      (do-list slot (extension-slots ext)
        (let ((init (get-opt-val (slot-opts slot) '= #f)))
	  (if init
	      (@ "  ~a = ~a;" (slot-name slot) init))))
      (@ "}")
      (@ "")
      (let ((n-links 0)
	    (n-slots 0))
	(@ "static tree_propslot_info ~a_~a_propslot_info[] = {"
	   c-name n-struct)
	(do-list s (extension-slots ext)
          (let ((t (slot-type s)))
	    (cond
	     ((node? t)
	      (@ "  { ~a, \"~a\",(tree_base_node*tree_prop::*)&~a_~a_ext::~a },"
		 (node-kind t) (slot-name s) c-name n-struct (slot-name s))
	      (set! n-links (1+ n-links))))))
	(set! n-slots n-links)
	(do-list s (extension-slots ext)
          (let ((t (slot-type s)))
	    (cond
	     ((ctype? t)
	      (@ "  { (tree_kind_info*)&~a_ctype_info, \"~a\",(tree_base_node*tree_prop::*)&~a_~a_ext::~a },"
		 (ctype-name t) (slot-name s) c-name n-struct (slot-name s))
	      (set! n-slots (1+ n-slots))))))
	(@ "};")
	(@ "")
	(@ "static struct tree_prop_info ~a_~a_prop_info = {" c-name n-struct)
	(@ "  ~a, ~a," n-links n-slots)
	(@ "  ~a_~a_propslot_info" c-name n-struct)
	(@ "};")
	(@ "")
	(@ "tree_prop_info *")
	(@ "~a_~a_ext::get_info ()" c-name n-struct)
	(@ "{")
	(@ "  return &~a_~a_prop_info;" c-name n-struct)
	(@ "}")
	(@ "")
	(@ "static int ~a_~a_key;" c-name n-struct)
	(@ "")
	(@ "~a_~a_ext *get_~a_ext (~a n)"
	   c-name n-struct c-name (node-pointer n))
	(@ "{")
	(@ "  ~a_~a_ext *attrs = (~a_~a_ext *) n->get (~a_~a_key);"
	   c-name n-struct c-name n-struct c-name n-struct)
	(@ "  if (attrs == 0)")
	(@ "    {")
	(@ "       attrs = new ~a_~a_ext;" c-name n-struct)
	(@ "       n->put (~a_~a_key, attrs);" c-name n-struct)
	(@ "    }")
	(@ "  return attrs;")
	(@ "}"))))

  ;; Emit the creation functions.  This is straightforward with the
  ;; support functions defined above.

  (do-list c (chunk-creators ch)
    (@ "")
    (let ((base (get-opt-val (creator-opts c) 'base #f))
	  (chunks (map find-chunk (get-opt (creator-opts c) 'chunks '())))
	  (inits (get-opt (creator-opts c) 'inits '()))
	  (placement (get-opt-val (creator-opts c) 'placement #f)))
      (do-list ch chunks
	(use-chunk ch)
	(do-list n (chunk-nodes ch)
	  (do-list i (find-inits n inits)
	    (@ "")
	    (@ "~a ~a::~a (~a)"
	       (node-pointer n) (creator-name c) (node-creator n)
	       (creator-decls n i))
	    (@ "{")
	    (@ "  ~a n = new~a ~a (~a);"
	       (node-pointer n)
	       (if placement (@@ " (~a)" placement) "")
	       (node-struct n)
	       (creator-args n i))
	    (do-list e (creator-construct-extras i)
	      (@ "  ~a (n);" e))
	    (@ "  return n;")
	    (@ "}"))))))

  ;; Emit the init function.

  (@ "")
  (@ "static tree_kind ~a_kinds[~a] = {"
     (chunk-name ch) (if (>= 0 (length (chunk-nodes ch))) ""
			 (length (chunk-nodes ch))))
  (do-list n (reverse (chunk-nodes ch))
    (@ "  ~a," (node-kind n)))
  (@ "};")
  (@ "")
  (@ "struct tree_chunk_info ~a_chunk_info = {" (chunk-name ch))
  (@ "  -1,")      ; chunk_id, will be filled in by tree_register_chunk
  (@ "  \"~a\"," (chunk-name ch))
  (@ "  ~a," (length (chunk-nodes ch)))
  (@ "  ~a_kinds" (chunk-name ch))
  (@ "};")
  (@ "")
  (@ "void init_~a_chunk ()" (chunk-name ch))
  (@ "{")
  (@ "  if (~a_chunk_info.chunk_id != -1)" (chunk-name ch))
  (@ "    return;")
  (pk 'used-chunks (map chunk-name used-chunks))
  (do-list used-ch used-chunks
    (@ "  init_~a_chunk ();" (chunk-name used-ch)))
  (@ "")
  (@ "  tree_register_chunk (&~a_chunk_info);" (chunk-name ch))
  (do-list gen (reverse (chunk-generics ch))
    (@ "  ~a_generic_~a.init (\"~a\");"
       (chunk-name ch) (generic-id gen) (generic-name gen)))
  (do-list meth (reverse (chunk-methods ch))
    (let ((gen (methods-generic meth))
	  (n-chunks (length (uniq (map node-chunk (methods-nodes meth))))))
      (@ "  ~a_generic_~a.merge (~a, ctab_~a);"
	 (chunk-name (generic-chunk gen)) (generic-id gen)
	 n-chunks (methods-id meth))))
  (do-list ext (reverse (chunk-extensions ch))
    (@ "  ~a_~a_key = tree_uniq_prop_key (~a);"
       (chunk-name ch) (node-struct (extension-node ext))
       (node-kind (extension-node ext))))
  (@ "}")

  ;; Emit some C++ magic to have the init function be called
  ;; automatically.  XXX - this doesn't seem to work with all shared
  ;; library implementations.

  (@ "")
  (@ "struct ~a_auto_initializer {" (chunk-name ch))
  (@ "  ~a_auto_initializer () { init_~a_chunk (); }"
     (chunk-name ch) (chunk-name ch))
  (@ "} ~a_ignition;" (chunk-name ch)))

(define (emit-noext ch)
  (@ "// generated by gen-nodes from `~a'.  Do not edit." arg-in-file)

  (@ "#ifndef ~a_NOEXT_H" (upcase (chunk-name ch)))
  (@ "#define ~a_NOEXT_H" (upcase (chunk-name ch)))

  (@ "")
  (@ "typedef IIRBase IIR;")
  (@ "typedef pIIRBase pIIR;")
  (do-list n (reverse (chunk-nodes ch))
    (@ "typedef ~a IIR_~a;" (node-struct n) (node-name n))
    (@ "typedef ~a pIIR_~a;" (node-pointer n) (node-name n)))

  (@ "")
  (@ "#endif"))

(define (emit-node-list ch)
  (do-list n (reverse (chunk-nodes ch))
    (let ((syls (cdr (syllables (node-name n)))))
      (@ "s/m~a/m~a/g" (node-name n) (node-name n)))))

(define (emit-type-hierarchy ch)

  (define (emit-node-list p nl)
    (if (not (null? nl))
	(let ((n (car nl))
	      (nl (cdr nl)))
	  (@ "~a|" p)
	  (@ "~a~a~a" p (if (null? nl) "\\-" "+-") (node-name n))
	  (let ((p (string-append* p (if (null? nl) "  " "| "))))
	    (emit-node-list p (node-derived n)))
	  (emit-node-list p nl))))

  (@ "tree_base_node")
  (emit-node-list "" (chunk-base-nodes ch)))

(define (emit-node-doc n)
  (define (slot->c slot)
    (@@ "@t{~a}" (decl->c (slot-type slot) (slot-name slot))))
  (define (table-row label items)
    (if (null? items)
	(@ "@item ~a @tab none" label)
	(begin
	  (@ "@item ~a @tab ~a" label (car items))
	  (do-list i (cdr items)
	    (@ "@item @tab ~a" i)))))

  (@ "@deftp Node ~a" (node-name n))
  (@ "@multitable {Inherited slots:} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}")
  (@ "@item Inherits from: @tab ~a" (if (node-base n)
				       (node-name (node-base n))
				       "tree_base_node"))
  (table-row "Inherited by:" (map node-name (node-derived n)))
  (table-row "Referenced by:" (map node-name (node-referenced n)))
  (table-row "Inherited slots:" (map slot->c 
				     (node-effective-slots (node-base n))))
  (table-row "Direct slots:" (map slot->c (node-slots n)))
  (@ "@end multitable")
  (@ ""))

(define (emit-all-node-docs ch)
  (define (sort l c) l)
  (define (node-compare n1 n2) (string<? (node-name n1) (node-name n2)))
  (do-list n (sort (chunk-nodes ch) node-compare)
    (emit-node-doc n)
    (@ "@end deftp")
    (@ "")))

(define (emit-doc)
  (build-node-xref)
  (let loop ((line (read-line)))
    (cond 
     ((not (eof-object? line))
      (cond
       ((string-match "^@typehierarchy ([^ ]*)" line)
	=> (lambda (m)
	     (let ((chunk-name (string->symbol (match:substring m 1))))
	       (emit-type-hierarchy (find-chunk chunk-name)))))
       ((string-match "^@defnode ([^ ]*)" line)
	=> (lambda (m)
	     (let ((name (string->symbol (match:substring m 1))))
	       (emit-node-doc (find-node name)))))
       ((string-match "^@end defnode" line)
	(@ "@end deftp"))
       ((string-match "^@allnodedocs ([^ ]*)" line)
	=> (lambda (m)
	     (let ((chunk-name (string->symbol (match:substring m 1))))
	       (emit-all-node-docs (find-chunk chunk-name)))))
       (else
	(@ "~a" line)))
      (loop (read-line))))))

;;; Main

(define (error-cleanup)
  (delete-file arg-out-file))

(define argv (command-line))

(define (peek-argv)
  (and (not (null? argv)) (car argv)))

(define (pop-argv)
  (let ((arg (peek-argv)))
    (if arg 
	(set! argv (cdr argv))
	(error "too few arguments"))
    arg))

(define prog-name (pop-argv))

(define extra-search-dirs '())

(set! %load-path (cons default-include-path %load-path))

(let loop ()
  (cond ((and (peek-argv) (string=? (peek-argv) "-I"))
	 (pop-argv)
	 (set! extra-search-dirs (append! extra-search-dirs (list (pop-argv))))
	 (loop))))

(set! %load-path (append! extra-search-dirs %load-path))

(cond ((< (length argv) 4)
       (info "usage: ~a OPTIONS CMD CHUNK IN-FILE OUT-FILE~%" prog-name)
       (info "where CMD is `header' or `impl'.~%")
       (info "OPTIONS can be:~%")
       (info "  -I <dir>      add <dir> to the search path~%")
       (quit 1)))

(define arg-cmd      (string->symbol (list-ref argv 0)))
(define arg-chunk    (string->symbol (list-ref argv 1)))
(define arg-in-file  (list-ref argv 2))
(define arg-out-file (list-ref argv 3))

(catch #t
       (lambda ()
	 (with-output-to-file arg-out-file
	   (lambda ()
	     (include arg-in-file)
	     (let ((ch (find-chunk arg-chunk)))
	       (case arg-cmd
		 ((header)
		  (emit-header ch))
		 ((impl)
		  (emit-impl ch))
		 ((type-hierarchy)
		  (emit-type-hierarchy ch))
		 ((noext)
		  (emit-noext ch))
		 ((doc)
		  (emit-doc)))))))
       (lambda args
	 (error-cleanup)
	 (apply throw args)))