summaryrefslogtreecommitdiff
path: root/xdelta1/libedsio/edsio.el
blob: b28f62367127752208328d9a5f76d30d04288020 (plain)
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
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
;; -*- Emacs-Lisp -*-

(require 'cl)
(require 'pp)

(eval-and-compile (setq load-path (cons ".." (cons "." load-path))))
(provide 'edsio)
(eval-and-compile (setq load-path (cdr (cdr load-path))))

;; Turn of assertions in compiled code.
(eval-and-compile
  (setq cl-optimize-speed 3)
  (setq cl-optimize-safety 1)
  )

;; Begin

(defconst *definition-state* nil
  "List of all the names of variables containing state from the
definition file so that operations may be performed on everything in
the definition file.")
(defconst *definition-attrs* nil
  "List of attributes for sharing indices.")
(defconst *all-objects* nil)
(defconst *output-files* nil
  "List of lists (NAME BUFFER) used during output of headers.")
(defconst *output-prefix* nil
  "Prefix used for outputing header files.")
(defconst *cpp-extension* "c")

;; This defines several functions and one macro.  The indirection makes
;; it a little bit confusing to read.  It defines the macro DEFDNAME,
;; a function DEFDNAME*, MAKE-DNAME, and a setter and getter for each arg.
(eval-and-compile
(defmacro attr-index (attr)
  `(- (length *definition-attrs*) (length (memq ,attr *definition-attrs*))))

(defmacro defastmacro(dtype args attrs)
  "Defines a macro named DEFDTYPE for defining various AST properties."
  (let ((def-macr  (intern (format "def%s" dtype)))
	(def-func  (intern (format "def%s*" dtype)))
	(make-func (intern (format "make-%s" dtype)))
	(state     (intern (format "*%s-defs*" dtype)))
	(exprs nil))
    (if (not *definition-attrs*)
	(setq *definition-attrs* '(menunode menuline)))
    (let ((fields (append args attrs)))
      (while fields
	(if (not (memq (car fields) *definition-attrs*))
	    (setq *definition-attrs* (append *definition-attrs* (list (car fields)))))
	(setq fields (cdr fields))
	)
      )
    ;; Add it to *definition-state*
    (setq *definition-state* (cons state *definition-state*))
    ;; DEFCONST it
    (setq exprs (cons (list 'defconst state (quote nil)) exprs))
    ;; DEFMACRO DEFDTYPE
    (setq exprs (cons (list 'defmacro
			    def-macr
			    args
			    (append (list 'list (list 'quote def-func))
				    (mapcar (function (lambda (x)
							(list 'list (list 'quote 'quote) x)
							)
						      )
					    args)
				    )
			    )
		      exprs
		      )
	  )
    ;; DEFUN DEFDTYPE*
    (setq exprs (cons (list 'defun
			    def-func
			    args
			    (list 'setq
				  state
				  (list 'cons
					(cons make-func args)
					state
					)
				  )
			    )
		      exprs
		      )
	  )
    ;; MAKE-DTYPE
    (setq exprs (cons (list 'defun
			    make-func
			    args
			    (list 'let (list (list 'it (list 'make-vector (length *definition-attrs*) nil)))
				  (if args
				      (cons 'progn (mapcar
						    (function
						     (lambda (x)
						       (list 'aset 'it (attr-index x) x)
						       )
						     )
						    args
						    )
					    )
				    )
				  (if attrs
				      (cons 'progn (mapcar
						    (function
						     (lambda (x)
						       (list 'aset 'it (attr-index x) nil)
						       )
						     )
						    attrs
						    )
					    )
				    )
				  (if (memq 'menu args)
				      (list 'progn
					    (list 'aset 'it (attr-index 'menunode) (list 'function (intern (format "%s-menunode" dtype))))
					    (list 'aset 'it (attr-index 'menuline) (list 'function (intern (format "%s-menuline" dtype))))
					    )
				    )
				  (list 'cons (list 'quote dtype) 'it)
				  )
			    )
		      exprs
		      )
	  )
    ;; Add the fake arguments:
    (if (memq 'menu args)
	(setq attrs (append (list 'menunode 'menuline) attrs)))
    (setq args (append args attrs))
    (while args
      (let* ((thearg (car args))
	     (arg-set (intern (format "%s-%s-set" dtype thearg)))
	     (arg-get (intern (format "%s-%s-get" dtype thearg))))
	;; DTYPE-ARG-GET
	(setq exprs (cons (list 'defmacro
				(intern (format "%s-%s-get" dtype thearg))
				'(obj)
				(list 'list
				      (list 'quote 'aref)
				      (list 'list (list 'quote 'cdr) 'obj)
				      (attr-index thearg))
				)
			  exprs
			  )
	      )
	;; DTYPE-ARG-SET
	(setq exprs (cons (list 'defmacro
				(intern (format "%s-%s-set" dtype thearg))
				'(obj val)
				(list 'list
				      (list 'quote 'aset)
				      (list 'list (list 'quote 'cdr) 'obj)
				      (attr-index thearg)
				      'val)
				)
			  exprs
			  )
	      )
	)
      (setq args (cdr args))
      )
    ;; To see what it's generating uncomment the next 2 lines.
    ;;(setq message-log-max t)
    ;;(mapcar (function pp) exprs)
    (cons 'progn exprs)
    )
  )


;; This is, as the name suggests, really bogus.  Basically, each DEFASTMACRO
;; call adds to the list *definition-state*.  To compile it, however, it has
;; to be done at compile time, so this macro gets evaluated when being compiled
;; and clears the list.  Then the DEFASTMACRO calls are made, and then DEFCDS
;; is called to define CLEAR-DEFINITION-STATE which resets the list to the
;; compile-time computed value of *definition-state*, it would otherwise be
;; empty when running compiled code.
(defmacro bogus ()
  (setq *definition-state* nil)
  (setq *definition-attrs* nil)
  )

  (bogus)

;; Each DEFASTMACRO statement defines a directive for the definition
;; file along with it's argument names.
(defastmacro sertype      (name number fields transients)  ())
(defastmacro module       (name id header pheader)  ())
(defastmacro import       (name)  (prefix))

(defastmacro event        (name level uargs sargs desc)  ())
(defastmacro etype        (name ctype) ())

(defastmacro prophost     (name letter ctype persist)          (proptypes))
(defastmacro prophosttype (host type) ())

(defmacro defcds ()
  (let ((exprs nil))
    (setq exprs (list (list 'defun 'clear-definition-state nil
			    '(setq *all-objects* nil)
			    (list 'setq '*definition-state* (list 'quote *definition-state*))
			    (list 'setq '*definition-attrs* (list 'quote *definition-attrs*))
			    '(mapcar (function (lambda (x) (set x nil))) *definition-state*)
			    )

		      )
	  )
    (mapcar
     (function
      (lambda (x)
	(setq exprs (cons (list 'defmacro
				(intern (format "obj-%s-get" x))
				'(obj)
				(list 'list
				      (list 'quote 'aref)
				      (list 'list (list 'quote 'cdr) 'obj)
				      (attr-index x))
				)
			  exprs
			  )
	      )
	(setq exprs (cons (list 'defmacro
				(intern (format "obj-%s-set" x))
				'(obj val)
				(list 'list
				      (list 'quote 'aset)
				      (list 'list (list 'quote 'cdr) 'obj)
				      (attr-index x)
				      'val)
				)
			  exprs
			  )
	      )
	(let ((get (intern (format "obj-%s-get" x))))
	  (setq exprs (cons (list 'defun
				  (intern (format "obj-%s-eq" x))
				  '(val olist)
				  `(let ((ret nil))
				     (while (and (not ret) olist)
				       (if (eq val (,get (car olist)))
					   (setq ret (car olist))
					 )
				       (setq olist (cdr olist))
				       )
				     ret
				     )
				  )
			    exprs
			    )
		)
	  )
	)
      )
     *definition-attrs*
     )
    ;;(setq message-log-max t)
    ;;(mapcar (function pp) exprs)
    (cons 'progn exprs)
    )
  )

(defcds)
)
;; Entry Points

(defun generate-ser-noargs ()
  (interactive)
  (generate-ser "edsio.ser" "edsio")
  )

(defun generate-ser (input-file output-prefix)
  ;(interactive "finput: \nsoutput: \nsid: ")
  (let ((make-backup-files nil)
	(executing-kbd-macro t))
    (clear-definition-state)

    (do-it input-file output-prefix)
    )
  )

(defconst *library-id* nil
  "Identifier of this library.")

(defconst *library-header* nil
  "Header of this library.")
(defconst *library-pheader* nil
  "Header of this library.")

(defconst *prefix-with-library-header* t)

(defun load-defs(file)
  (load-file file)
  (setq *import-defs* (reverse *import-defs*))
  (setq *module-defs* (reverse *module-defs*))
  (setq *sertype-defs* (reverse *sertype-defs*))

  (setq *event-defs* (reverse *event-defs*))
  (setq *etype-defs* (reverse *etype-defs*))

  (setq *prophost-defs* (reverse *prophost-defs*))
  (setq *prophosttype-defs* (reverse *prophosttype-defs*))
  )

(defconst *header-typedef-marker* nil)
(defconst *source-init-marker* nil)
(defconst *source-top-marker* nil)

(defun do-it (input-file output-prefix)
  (setq *output-files* nil)
  (setq *output-prefix* output-prefix)

  (load-defs input-file)

  (if (not *module-defs*)
      (error "no defmodule in %s" input-file))

  (if (> (length *module-defs*) 1)
      (error "too many defmodules in %s" input-file))

  (setq *library-id* (module-id-get (car *module-defs*)))
  (setq *library-header* (module-header-get (car *module-defs*)))
  (setq *library-pheader* (module-pheader-get (car *module-defs*)))

  (when (not *library-header*)
    (setq *prefix-with-library-header* nil)
    (setq *library-header* (format "%s_edsio.h" *output-prefix*))
    )

  (if (or (<= *library-id* 0)
	  (>= *library-id* 256))
      (error "Library-id is out of range"))

  (if (> (length *sertype-defs*) 24)
      (error "no more than 24 types"))

  (unwind-protect
      (progn

	(output-header-file "_edsio")

	(read-imports)

	(insert "/* Initialize this library. */\n\n")
	(insert "gboolean " *output-prefix* "_edsio_init (void);\n\n")

	(insert "/* Types defined here. */\n\n")
	(setq *header-typedef-marker* (point-marker))

	(insert "/* Functions declared here. */\n\n")

	(output-source-file "_edsio")

	(insert "#include \"" *library-header* "\"\n\n")
	(insert "#include <errno.h>\n\n")

	(if *library-pheader*
	    (insert "#include \"" *library-pheader* "\"\n\n"))

	(insert "/* Declarations. */\n\n")
	(setq *source-top-marker* (point-marker))

	(insert "\n")

	(insert "/* initialize this library. */\n\n")
	(insert "gboolean\n" *output-prefix* "_edsio_init (void)\n{\n")
	(insert "  static gboolean once = FALSE;\n")
	(insert "  static gboolean result = FALSE;\n")
	(insert "  if (once) return result;\n")
	(insert "  once = TRUE;\n")

	(setq *source-init-marker* (point-marker))

	(insert (format "  edsio_library_register (%d, \"%s\");\n" *library-id* *output-prefix*))
	(insert "  result = TRUE;\n")
	(insert "  return TRUE;\n")
	(insert "};\n\n")

 	(if *prophosttype-defs*
 	    (generate-properties))

 	(if *sertype-defs*
 	    (generate-code))

 	(if *event-defs*
 	    (generate-events))

;	(message "source file:\n%s" (buffer-string))

	(mapcar (function (lambda (x) (output-finish-file x))) *output-files*)
	)
    (mapcar (function (lambda (x) (kill-buffer (cadr x)))) *output-files*)
    )
  )

(defvar *all-sertype-defs* nil)
(defvar *all-prophost-defs* nil)
(defvar *all-etype-defs* nil)

(defun read-imports ()

  (setq *all-sertype-defs* *sertype-defs*)
  (setq *all-etype-defs* *etype-defs*)
  (setq *all-prophost-defs* *prophost-defs*)

  (let ((mods *module-defs*)
	(imps0 *import-defs*)
	(imps  *import-defs*)
	(types *sertype-defs*)
	(events *event-defs*)
	(etypes *etype-defs*)
	(phosts *prophost-defs*)
	(phts  *prophosttype-defs*)
	)

    (while imps
      (clear-definition-state)

      (load-defs (import-name-get (car imps)))

      (setq *all-sertype-defs* (append *all-sertype-defs* *sertype-defs*))
      (setq *all-etype-defs* (append *all-etype-defs* *etype-defs*))
      (setq *all-prophost-defs* (append *all-prophost-defs* *prophost-defs*))

      (import-prefix-set (car imps) (module-name-get (car *module-defs*)))

      (when (or *sertype-defs* *event-defs*)
	(output-header-file "_edsio")
	(insert (format "#include \"%s_edsio.h\"\n\n" (import-prefix-get (car imps))))
	)

      (setq imps (cdr imps))
      )

    (setq *module-defs* mods)
    (setq *import-defs* imps0)
    (setq *sertype-defs* types)
    (setq *event-defs* events)
    (setq *etype-defs* etypes)
    (setq *prophost-defs* phosts)
    (setq *prophosttype-defs* phts)
    )
  )


(defun output-header-file (name)
  (output-file (format "%s.h" name) 'c-header *output-prefix*))

(defun output-source-file (name)
  (output-file (format "%s.%s" name *cpp-extension*) 'c *output-prefix*))

(defun output-source-include-file (name)
  (output-file (format "%s.%si" name *cpp-extension*) 'c *output-prefix*))

(defun output-plain-file (name)
  (output-file (format "%s" name) 'plain ""))

(defun output-file (name type prefix)
  (let* ((name (format "%s%s" prefix name))
	 (it (assoc name *output-files*)))
    (if it
	(set-buffer (cadr it))
      (let ((nbuf (get-buffer-create (generate-new-buffer-name name))))
	(setq *output-files* (cons (list name nbuf type) *output-files*))
	(set-buffer nbuf)
	)
      )
    )
  )

(defun output-finish-file (file)
  (let ((name (car file))
	(buf (cadr file))
	(type (caddr file)))
    (set-buffer buf)
    ;(message "printing %s: %s" file (buffer-string))
    (cond ((eq type 'c)
	   (output-to-c name nil))
	  ((eq type 'c-header)
	   (output-to-c name t))
	  )
    (write-file-if-different buf name)
    )
  )

(defun output-to-c (name is-header)
  (goto-char (point-min))
  (insert "/* -*-Mode: C;-*-
 * Copyright (C) 1997, 1998, 1999  Josh MacDonald
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Author: Josh MacDonald <jmacd@CS.Berkeley.EDU>
 *
 * This file was AUTOMATICALLY GENERATED using:
 *
 * $Id: edsio.el 1.1 Sun, 28 Jan 2007 10:02:26 -0800 jmacd $
 */

")

  (if is-header
      (let ((cppname (string-replace-regexp (upcase name) "[-./]" "_")))
	(insert "#include \"edsio.h\"\n\n")

	(if *prefix-with-library-header*
	    (insert "#include \"" *library-header* "\"\n\n"))

	(insert "#ifndef _" cppname "_\n")
	(insert "#define _" cppname "_\n\n")
	(insert "#ifdef __cplusplus\n")
	(insert "extern \"C\" {\n")
	(insert "#endif\n\n")
	(goto-char (point-max))
	(insert "#ifdef __cplusplus\n")
	(insert "}\n")
	(insert "#endif\n")
	(insert "\n#endif /* _" cppname "_ */\n\n")
	)
    )
  )

(defun string-replace-regexp (str regexp to-string)
  "Result of replacing all occurrences in STR of REGEXP by TO-STRING.  The
replacement is as for replace-regexp."
  (let ((work (get-buffer-create "*string-tmp*")))
    (save-excursion
      (set-buffer work)
      (erase-buffer)
      (insert str)
      (beginning-of-buffer)
      (while (re-search-forward regexp nil t)
	(replace-match to-string nil nil))
      (buffer-string))))

(defun write-file-if-different (buf filename)
  (save-excursion
    (if (not (file-exists-p filename))
	(write-file filename)
      (set-buffer buf)
      (let ((old (get-buffer-create (generate-new-buffer-name filename)))
	    (bmin (point-min))
	    (bmax (point-max)))
	(unwind-protect
	    (progn
	      (set-buffer old)
	      (insert-file filename)
	      (let ((omin (point-min))
		    (omax (point-max))
		    (case-fold-search nil))
		(if (= 0 (compare-buffer-substrings old omin omax buf bmin bmax))
		    (message "Output file %s is unchanged." filename)
		  (set-buffer buf)
		  (write-file filename)
		  )
		)
	      )
	  (kill-buffer old)
	  )
	)
      )
    )
  )


(defun format-comlist (func l)
  (let ((x ""))
    (while l
      (setq x (concat x (funcall func (car l))))
      (if (cdr l)
	  (setq x (concat x ", ")))
      (setq l (cdr l))
      )
    x
    )
  )

(defun format-semilist (func l)
  (let ((x ""))
    (while l
      (setq x (concat x (funcall func (car l))))
      (if (cdr l)
	  (setq x (concat x "; ")))
      (setq l (cdr l))
      )
    x
    )
  )

(defun format-numbered-comlist (func l)
  (let ((x "")
	(n 0))
    (while l
      (setq x (concat x (funcall func (car l) n)))
      (setq n (+ n 1))
      (if (cdr l)
	  (setq x (concat x ", ")))
      (setq l (cdr l))
      )
    x
    )
  )

(defun capitalize1(s)
  (let ((work (get-buffer-create "*string-tmp*")))
    (save-excursion
      (set-buffer work)
      (erase-buffer)
      (insert (format "%s" s))
      (upcase-region (point-min) (+ (point-min) 1))
      (buffer-substring-no-properties (point-min) (point-max))
      )
    )
  )

(defun upcase-string (s)
  (let ((work (get-buffer-create "*string-tmp*")))
    (save-excursion
      (set-buffer work)
      (erase-buffer)
      (insert (format "%s" s))
      (upcase-region (point-min) (point-max))
      (buffer-substring-no-properties (point-min) (point-max))
      )
    )
  )

(defun downcase-string (s)
  (let ((work (get-buffer-create "*string-tmp*")))
    (save-excursion
      (set-buffer work)
      (erase-buffer)
      (insert (format "%s" s))
      (downcase-region (point-min) (point-max))
      (buffer-substring-no-properties (point-min) (point-max))
      )
    )
  )

;; HERE IT IS

(defun generate-code ()

  (let ((all-codes nil))
    (mapcar
     (function
      (lambda (st)
	(let ((x (sertype-number-get st)))
	  (cond ((member x all-codes)
		 (error "serial type number %d defined twice" x))
		((> x 24)
		 (error "serial type value %d too high" x))
		((< x 0)
		 (error "serial type value %d too low" x))
		(t (setq all-codes (cons x all-codes))))))
      )
     *sertype-defs*)
    )

  (output-header-file "_edsio")

  (insert "/* Serial Types */\n\n")
  (insert (format "enum _Serial%sType {\n" (capitalize1 *output-prefix*)))
  (insert (format-comlist
	   (function
	    (lambda (x)
	      (format "\n  ST_%s = (1<<(%d+EDSIO_LIBRARY_OFFSET_BITS))+%d" (sertype-name-get x) (sertype-number-get x) *library-id*))) *sertype-defs*))
  (insert "\n};\n\n")

  (insert "\n\n")

  (output-source-file "_edsio")

  (save-excursion
    (goto-char *source-top-marker*)

    (insert "static void print_spaces (guint n) { int i; for (i = 0; i < n; i += 1) g_print (\" \"); }\n\n")
    )

  (mapcar (function generate-code-entry) *sertype-defs*)

  )

(defun generate-code-entry (entry)
  (let ((ent-upcase   (sertype-name-get entry))
	(ent-downcase (downcase-string (sertype-name-get entry))))

    (output-header-file "_edsio")

    ;; The typedef, structure, and declarations.

    (save-excursion
      (goto-char *header-typedef-marker*)
      (insert (format "typedef struct _Serial%s Serial%s;\n" ent-upcase ent-upcase))
      )

    (insert (format "/* %s Structure\n */\n\n" ent-upcase))

    (insert (format "struct _Serial%s {\n" ent-upcase))

    (apply (function insert)
	   (mapcar (function
		    (lambda (x)
		      (format "  %s;\n" x)))
		   (entry-typename-pairs entry nil)))

    (apply (function insert)
	   (mapcar (function
		    (lambda (x)
		      (format "  %s;\n" x)))
		   (sertype-transients-get entry)))

    (insert "};\n\n")

    (insert (format "void     serializeio_print_%s_obj        (Serial%s* obj, guint indent_spaces);\n\n" ent-downcase ent-upcase))

    (insert (format "gboolean unserialize_%s                  (SerialSource *source, Serial%s**);\n" ent-downcase ent-upcase))
    (insert (format "gboolean unserialize_%s_internal         (SerialSource *source, Serial%s** );\n" ent-downcase ent-upcase))
    (insert (format "gboolean unserialize_%s_internal_noalloc (SerialSource *source, Serial%s* );\n" ent-downcase ent-upcase))
    (insert (format "gboolean serialize_%s                    (SerialSink *sink%s);\n" ent-downcase (entry-arglist t entry)))
    (insert (format "gboolean serialize_%s_obj                (SerialSink *sink, const Serial%s* obj);\n" ent-downcase ent-upcase))
    (insert (format "gboolean serialize_%s_internal           (SerialSink *sink%s);\n" ent-downcase (entry-arglist t entry)))
    (insert (format "gboolean serialize_%s_obj_internal (SerialSink *sink, Serial%s* obj);\n" ent-downcase ent-upcase))
    (insert (format "guint    serializeio_count_%s            (%s);\n" ent-downcase (entry-arglist nil entry)))
    (insert (format "guint    serializeio_count_%s_obj        (Serial%s const* obj);\n" ent-downcase ent-upcase))
    (insert (format "\n"))

    (output-source-file "_edsio")

    ;; The init entry

    (save-excursion
      (goto-char *source-init-marker*)
      (insert (format "  serializeio_initialize_type (\"ST_%s\", ST_%s, &unserialize_%s_internal, &serialize_%s_obj_internal, &serializeio_count_%s_obj, &serializeio_print_%s_obj);\n" ent-upcase ent-upcase ent-downcase ent-downcase ent-downcase ent-downcase))
      )

    ;; Count code

    (insert (format "/* %s Count\n */\n\n" ent-upcase))

    (insert (format "guint\nserializeio_count_%s (%s) {\n" ent-downcase (entry-arglist nil entry)))
    (insert (format "  guint size = sizeof (Serial%s);\n" ent-upcase))
    (apply (function insert)
	   (mapcar (function (lambda (x) (concat
					  (format "  ALIGN_8 (size);\n")
					  (entry-count-field entry x (format "%s" (car x)) "  " t))))
		   (sertype-fields-get entry)))
    (insert (format "  ALIGN_8 (size);\n"))
    (insert (format "  return size;\n"))
    (insert (format "}\n\n"))

    ;; Count Object code

    (insert (format "guint\nserializeio_count_%s_obj (Serial%s const* obj) {\n" ent-downcase ent-upcase))
    (insert (format "  return serializeio_count_%s (%s);\n" ent-downcase (entry-plist t nil "obj->" entry)))
    (insert (format "}\n\n"))

    ;; Print object code

    (insert (format "/* %s Print\n */\n\n" ent-upcase))

    (insert (format "void\nserializeio_print_%s_obj (Serial%s* obj, guint indent_spaces) {\n" ent-downcase ent-upcase))
    (insert (format "  print_spaces (indent_spaces);\n"))

    (insert (format "  g_print (\"[ST_%s]\\n\");\n" ent-upcase))

    (apply (function insert)
	   (mapcar (function (lambda (x) (entry-print-field entry x (format "obj->%s" (car x)) "  " t)))
		   (sertype-fields-get entry)))

    (insert (format "}\n\n"))

    ;; Print internal code

    ;; Serialize code

    (insert (format "/* %s Serialize\n */\n\n" ent-upcase))

    (insert (format "gboolean\nserialize_%s_internal (SerialSink *sink%s)\n" ent-downcase (entry-arglist t entry)))
    (insert (format "{\n"))

    (apply (function insert)
	   (mapcar (function (lambda (x) (entry-serialize-field entry x (format "%s" (car x)) "  " t)))
		   (sertype-fields-get entry)))

    (insert (format "  return TRUE;\n"))
    (if (sertype-fields-get entry)
	(insert (format "bail:\n  return FALSE;\n")))
    (insert (format "}\n\n"))

    ;; Internal Serialize Object code

    (insert (format "gboolean\nserialize_%s_obj_internal (SerialSink *sink, Serial%s* obj)\n" ent-downcase ent-upcase))
    (insert (format "{\n"))
    (insert (format "  return serialize_%s_internal (sink%s);\n" ent-downcase (entry-plist t t "obj->" entry)))
    (insert (format "}\n\n"))

    ;; External Serialize code

    (insert (format "gboolean\nserialize_%s (SerialSink *sink%s)\n" ent-downcase (entry-arglist t entry)))
    (insert (format "{\n"))

    (insert (format "  if (! (* sink->sink_type) (sink, ST_%s, serializeio_count_%s (%s), TRUE)) goto bail;\n" ent-upcase ent-downcase (entry-plist nil nil "" entry)))

    (insert (format "  if (! serialize_%s_internal (sink%s)) goto bail;\n" ent-downcase (entry-plist nil t "" entry)))
    (insert (format "  if (sink->sink_quantum && ! sink->sink_quantum (sink)) goto bail;\n"))

    (insert (format "  return TRUE;\n"))
    (insert (format "bail:\n"))
    (insert (format "  return FALSE;\n"))
    (insert (format "}\n\n"))

    ;; External serialize_obj

    (insert (format "gboolean\nserialize_%s_obj (SerialSink *sink, const Serial%s* obj) {\n\n" ent-downcase ent-upcase))
    (insert (format "  return serialize_%s (sink%s);\n" ent-downcase (entry-plist t t "obj->" entry)))
    (insert (format "}\n\n"))

    ;; Unserialize code

    (insert (format "/* %s Unserialize\n */\n\n" ent-upcase))

    (insert (format "gboolean\nunserialize_%s_internal_noalloc (SerialSource *source, Serial%s* result)\n" ent-downcase ent-upcase))
    (insert (format "{\n"))

    (apply (function insert)
	   (mapcar (function (lambda (x) (entry-unserialize-field entry x (format "result->%s" (car x)) "  ")))
		   (sertype-fields-get entry)))

    (insert (format "  return TRUE;\n"))
    (if (sertype-fields-get entry)
	(insert (format "bail:\n  return FALSE;\n")))
    (insert (format "}\n\n"))


    (insert (format "gboolean\nunserialize_%s_internal (SerialSource *source, Serial%s** result)\n" ent-downcase ent-upcase))
    (insert (format "{\n"))

    (insert (format "  Serial%s* unser;\n" ent-upcase))
    (insert (format "  (*result) = NULL;\n"))
    (insert (format "  unser = serializeio_source_alloc (source, sizeof (Serial%s));\n" ent-upcase))
    (insert (format "  if (! unser) goto bail;\n"))

    (insert (format "  if (! unserialize_%s_internal_noalloc (source, unser)) goto bail;\n" ent-downcase))

    (insert (format "  (*result) = unser;\n"))
    (insert (format "  return TRUE;\n"))
    (insert (format "bail:\n"))
    (insert (format "  return FALSE;\n"))
    (insert (format "}\n\n"))

    ;; External unserialize

    (insert (format "gboolean\nunserialize_%s (SerialSource *source, Serial%s** result)\n"  ent-downcase ent-upcase))
    (insert (format "{\n"))

    (insert (format "  if ( (* source->source_type) (source, TRUE) != ST_%s) goto bail;\n" ent-upcase))

    (insert (format "  if (! unserialize_%s_internal (source, result)) goto bail;\n" ent-downcase))

    (insert (format "  if (! serializeio_source_object_received (source)) goto bail;\n"))

    (insert (format "  return TRUE;\n"))
    (insert (format "bail:\n"))
    (insert (format "  return FALSE;\n"))

    (insert (format "}\n\n"))

    )
  )

(defun entry-typename-pairs (entry is-param)
  (let ((pairs nil)
	(fields (sertype-fields-get entry)))
    (while fields
      (let ((field (car fields)))
	(when (or (equal (cadr field) 'bytes)
		  (and (consp (cadr field)) (equal (caadr field) 'array)))
	  (setq pairs (cons (format "guint32 %s_len" (car field)) pairs))
	  )
	(when (equal (cadr field) 'object)
	  (setq pairs (cons (format "guint32 %s_type" (car field)) pairs))
	  )
	(setq pairs (cons (field-decl field is-param) pairs))
	)
      (setq fields (cdr fields))
      )
    (nreverse pairs)
    )
  )

(defun entry-param-names (prefix entry need_pbr)
  (let ((pairs nil)
	(fields (sertype-fields-get entry)))
    (while fields
      (let ((field (car fields)))
	(when (or (equal (cadr field) 'bytes)
		  (and (consp (cadr field)) (equal (caadr field) 'array)))
	  (setq pairs (cons (format "%s%s_len" prefix (car field)) pairs))
	  )
	(when (equal (cadr field) 'object)
	  (setq pairs (cons (format "%s%s_type" prefix (car field)) pairs))
	  )
	(setq pairs (cons (format "%s%s%s" (if (and need_pbr (needs-ref field)) "&" "") prefix (car field)) pairs))
	)
      (setq fields (cdr fields))
      )
    (nreverse pairs)
    )
  )

(defun field-ctype (field)
  (cond ((equal (cadr field) 'string)
	 "const gchar*")
	((equal (cadr field) 'uint)
	 "guint32")
	((equal (cadr field) 'uint32)
	 "guint32")
	((equal (cadr field) 'uint16)
	 "guint16")
	((equal (cadr field) 'uint8)
	 "guint8")
	((equal (cadr field) 'boolean)
	 "gboolean")
	((equal (cadr field) 'bytes)
	 "const guint8*")
	((equal (cadr field) 'object)
	 "void*")
	((member (cadr field) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*))
	 (format "Serial%s" (cadr field)))
	((equal (car (cadr field)) 'bytes)
	 "const guint8*")
	((member (car (cadr field)) '(array ptr))
	 (concat (field-ctype (cadr field)) "*"))
	(t (error "unrecognized field type: %s" (cadr field))))
  )

(defun field-decl (field is-param)
  (if (and (consp (cadr field))
	   (equal (car (cadr field)) 'bytes))
      (format "%sguint8 %s[%d]" (if is-param "const " "") (car field) (cadr (cadr field)))
    ;(message "foo %s %s" field (member (cadr field) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*)))
    (format "%s %s"
	    (cond ((member (cadr field) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*))
		   (format "Serial%s%s" (cadr field) (if is-param " const*" "")))
		  ((equal (cadr field) 'string)
		   "const gchar*")
		  ((equal (cadr field) 'uint)
		   "guint32")
		  ((equal (cadr field) 'uint32)
		   "guint32")
		  ((equal (cadr field) 'uint16)
		   "guint16")
		  ((equal (cadr field) 'uint8)
		   "guint8")
		  ((equal (cadr field) 'boolean)
		   "gboolean")
		  ((equal (cadr field) 'bytes)
		   "const guint8*")
		  ((equal (cadr field) 'object)
		   "void*")
		  ((member (car (cadr field)) '(array ptr))
		   (concat (field-ctype (cadr field)) (if is-param " const*" "*")))
		  (t (error "unrecognized field type: %s" (cadr field))))
	    (car field)))
  )

(defun entry-arglist (need_first entry)
  (concat
   (if (and need_first (sertype-fields-get entry)) ", " "")
   (format-comlist (function (lambda (x) x)) (entry-typename-pairs entry t))))

(defun needs-ref (field)
  (member (cadr field) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*))
  )

(defun entry-plist (need_pbr need_first prefix entry)
  (concat
   (if (and need_first (sertype-fields-get entry)) ", " "")
   (format-comlist (function (lambda (x) (format "%s" x)))
		   (entry-param-names prefix entry need_pbr))))

(defun entry-unserialize-field (entry field name prefix)
  (cond ((equal (cadr field) 'uint)
	 (format "%sif (! (* source->next_uint) (source, &%s)) goto bail;\n" prefix name))
	((equal (cadr field) 'uint32)
	 (format "%sif (! (* source->next_uint32) (source, &%s)) goto bail;\n" prefix name))
	((equal (cadr field) 'uint16)
	 (format "%sif (! (* source->next_uint16) (source, &%s)) goto bail;\n" prefix name))
	((equal (cadr field) 'uint8)
	 (format "%sif (! (* source->next_uint8) (source, &%s)) goto bail;\n" prefix name))
	((equal (cadr field) 'boolean)
	 (format "%sif (! (* source->next_bool) (source, &%s)) goto bail;\n" prefix name))
	((equal (cadr field) 'string)
	 (format "%sif (! (* source->next_string) (source, &%s)) goto bail;\n" prefix name))
	((equal (cadr field) 'bytes)
	 (format "%sif (! (* source->next_bytes) (source, &%s, &%s_len)) goto bail;\n" prefix name name))
	((equal (cadr field) 'object)
	 (format "%sif (! serializeio_unserialize_generic_internal (source, &%s_type, &%s, FALSE)) goto bail;\n" prefix name name))
	((member (cadr field) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*))
	 (format "%sif (! unserialize_%s_internal_noalloc (source, &%s)) goto bail;\n" prefix (downcase-string (cadr field)) name))
	((and (equal (car (cadr field)) 'ptr)
	      (member (cadr (cadr field)) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*)))
	 (format "%sif (! unserialize_%s_internal (source, &%s)) goto bail;\n" prefix (downcase-string (cadr (cadr field))) name))
	((equal (car (cadr field)) 'bytes)
	 (format "%sif (! (* source->next_bytes_known) (source, %s, %d)) goto bail;\n" prefix name (cadr (cadr field))))
	((equal (car (cadr field)) 'array)
	 (format "%s{
%s  gint i;
%s  if (! (* source->next_uint) (source, &%s_len)) goto bail;
%s  if (! (%s = serializeio_source_alloc (source, sizeof (%s) * %s_len))) goto bail;
%s  for (i = 0; i < %s_len; i += 1)
%s    {
%s%s      }
%s}
"
		 prefix
		 prefix prefix
		 name
		 prefix
		 name
		 (field-ctype (cadr field))
		 name
		 prefix
		 name
		 prefix
		 prefix
		 (entry-unserialize-field entry (cadr field) (concat "(" name "[i])") (concat prefix "    "))
		 prefix
		 ))
	(t (error "unrecognized field type: %s" (cadr field)))))


(defun entry-serialize-field (entry field name prefix is-param)
  (cond ((equal (cadr field) 'uint)
	 (format "%sif (! (* sink->next_uint) (sink, %s)) goto bail;\n" prefix name))
	((equal (cadr field) 'uint16)
	 (format "%sif (! (* sink->next_uint16) (sink, %s)) goto bail;\n" prefix name))
	((equal (cadr field) 'uint8)
	 (format "%sif (! (* sink->next_uint8) (sink, %s)) goto bail;\n" prefix name))
	((equal (cadr field) 'uint32)
	 (format "%sif (! (* sink->next_uint32) (sink, %s)) goto bail;\n" prefix name))
	((equal (cadr field) 'boolean)
	 (format "%sif (! (* sink->next_bool) (sink, %s)) goto bail;\n" prefix name))
	((equal (cadr field) 'string)
	 (format "%sif (! (* sink->next_string) (sink, %s)) goto bail;\n" prefix name))
	((equal (cadr field) 'bytes)
	 (format "%sif (! (* sink->next_bytes) (sink, %s, %s_len)) goto bail;\n" prefix name name))
	((equal (cadr field) 'object)
	 (format "%sif (! serializeio_serialize_generic_internal (sink, %s_type, %s, FALSE)) goto bail;\n" prefix name name))
	((member (cadr field) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*))
	 (format "%sif (! serialize_%s_internal (sink%s)) goto bail;\n" prefix (downcase-string (cadr field))
		 (entry-plist t t (concat name (if is-param "->" ".")) (obj-name-eq (cadr field) *all-sertype-defs*))))
	((and (equal (car (cadr field)) 'ptr)
	      (member (cadr (cadr field)) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*)))
	 (format "%sif (! serialize_%s_internal (sink%s)) goto bail;\n" prefix (downcase-string (cadr (cadr field)))
		 (entry-plist t t (concat name "->") (obj-name-eq (cadr (cadr field)) *all-sertype-defs*))))
	((equal (car (cadr field)) 'bytes)
	 (format "%sif (! (* sink->next_bytes_known) (sink, %s, %d)) goto bail;\n" prefix name (cadr (cadr field))))
	((equal (car (cadr field)) 'array)
	 (format "%s{
%s  gint i;
%s  if (! (* sink->next_uint) (sink, %s_len)) goto bail;
%s  for (i = 0; i < %s_len; i += 1)
%s    {
%s%s      }
%s}
"
		 prefix prefix prefix
		 name
		 prefix
		 name
		 prefix
		 prefix
		 (entry-serialize-field entry (cadr field) (array-index name (cadr field)) (concat prefix "    ") nil)
		 prefix
		 ))
	(t (error "unrecognized field type: %s" (cadr field)))))

(defun array-index (name field)
  ;(concat "(" (if (needs-ref field) "&" "") name "[i])")
  (concat "(" name "[i])")
  )

(defun entry-count-field (entry field name prefix is-param)
  (cond ((equal (cadr field) 'uint)
	 ;(format "%ssize += sizeof (guint32);\n" prefix)
	 ""
	 )
	((equal (cadr field) 'uint32)
	 ;(format "%ssize += sizeof (guint32);\n" prefix)
	 ""
	 )
	((equal (cadr field) 'uint16)
	 ;(format "%ssize += sizeof (guint16);\n" prefix)
	 ""
	 )
	((equal (cadr field) 'uint8)
	 ;(format "%ssize += sizeof (guint8);\n" prefix)
	 ""
	 )
	((equal (cadr field) 'boolean)
	 ;(format "%ssize += sizeof (gboolean);\n" prefix)
	 ""
	 )
	((equal (cadr field) 'string)
	 (format "%ssize += strlen (%s) + 1;\n" prefix name)
	 )
	((equal (cadr field) 'bytes)
	 (format "%ssize += %s_len;\n" prefix name)
	 )
	((equal (cadr field) 'object)
	 (format "%ssize += serializeio_generic_count (%s_type, %s);\n" prefix name name)
	 )
	((member (cadr field) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*))
 	 (format "%ssize += serializeio_count_%s_obj (%s%s) - sizeof (Serial%s);\n"
		 prefix
		 (downcase-string (cadr field))
		 (if is-param "" "& ")
		 name
		 (cadr field)
 		 )
	 )
	((and (equal (car (cadr field)) 'ptr)
	      (member (cadr (cadr field)) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*)))
 	 (format "%ssize += serializeio_count_%s_obj (%s);\n" prefix (downcase-string (cadr (cadr field))) name)
	 )
	((equal (car (cadr field)) 'bytes)
	 ;(format "%ssize += 0;\n" prefix (cadr (cadr field)))
	 ""
	 )
	((equal (car (cadr field)) 'array)
	 (format "%s{
%s  gint i;
%s  for (i = 0; i < %s_len; i += 1)
%s    {
%s%s      }
%s}
"
		 prefix prefix prefix
		 name
		 prefix
		 prefix
		 (entry-count-array-field entry (cadr field) (array-index name (cadr field)) (concat prefix "    ") nil)
		 prefix
		 ))
	(t (error "unrecognized field type: %s" (cadr field)))))

(defun entry-count-array-field (entry field name prefix is-param)
  (cond ((equal (cadr field) 'uint)
	 (format "%ssize += sizeof (guint32);\n" prefix)
	 )
	((equal (cadr field) 'uint32)
	 (format "%ssize += sizeof (guint32);\n" prefix)
	 )
	((equal (cadr field) 'uint16)
	 (format "%ssize += sizeof (guint16);\n" prefix)
	 )
	((equal (cadr field) 'uint8)
	 (format "%ssize += sizeof (guint8);\n" prefix)
	 )
	((equal (cadr field) 'boolean)
	 (format "%ssize += sizeof (gboolean);\n" prefix)
	 )
	((equal (cadr field) 'string)
	 (format "%ssize += strlen (%s) + 1 + sizeof (void*);\n" prefix name)
	 )
	((equal (cadr field) 'bytes)
	 (error "can't do that: bytes1")
	 )
	((equal (cadr field) 'object)
	 (error "can't do that: object")
	 )
	((member (cadr field) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*))
 	 (format "%ssize += serializeio_count_%s_obj (%s%s);\n"
		 prefix
		 (downcase-string (cadr field))
		 (if is-param "" "& ")
		 name
		 (cadr field)
 		 )
	 )
	((and (equal (car (cadr field)) 'ptr)
	      (member (cadr (cadr field)) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*)))
 	 (format "%ssize += serializeio_count_%s_obj (%s) + sizeof (void*);\n" prefix (downcase-string (cadr (cadr field))) name)
	 )
	((equal (car (cadr field)) 'bytes)
	 (error "can't do that: bytes2")
	 )
	((equal (car (cadr field)) 'array)
	 (error "can't do that: array")
	 )
	(t (error "unrecognized field type: %s" (cadr field)))))

(defun entry-print-field (entry field name prefix is-param)
  (concat
   (format "%sprint_spaces (indent_spaces);\n" prefix)
   (if is-param (format "%sg_print (\"%s = \");\n" prefix (car field)) "")
   (cond ((equal (cadr field) 'uint)
	  (format "%sg_print (\"%%d\\n\", %s);\n" prefix name))
	 ((equal (cadr field) 'uint32)
	  (format "%sg_print (\"%%d\\n\", %s);\n" prefix name))
	 ((equal (cadr field) 'uint16)
	  (format "%sg_print (\"%%d\\n\", %s);\n" prefix name))
	 ((equal (cadr field) 'uint8)
	  (format "%sg_print (\"%%d\\n\", %s);\n" prefix name))
	 ((equal (cadr field) 'boolean)
	  (format "%sg_print (\"%%s\\n\", %s ? \"true\" : \"false\");\n" prefix name))
	 ((equal (cadr field) 'string)
	  (format "%sg_print (\"%%s\\n\", %s);\n" prefix name))
	 ((equal (cadr field) 'bytes)
	  (format "%sserializeio_print_bytes (%s, %s_len);\n" prefix name name))
	 ((equal (cadr field) 'object)
	  (concat
 	   (if is-param (format "%sg_print (\"{\\n\");\n" prefix) "")
	   (format "%sserializeio_generic_print (%s_type, %s, indent_spaces + 2);\n" prefix name name)
	   (format "%sprint_spaces (indent_spaces);\n;\n" prefix)
	   (if is-param (format "%sg_print (\"}\\n\");\n" prefix) "")
	   )
	  )
	 ((member (cadr field) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*))
	  (concat
	   (if is-param (format "%sg_print (\"{\\n\");\n" prefix) "")
	   (format "%sserializeio_print_%s_obj (& %s, indent_spaces + 2);\n" prefix (downcase-string (cadr field)) name name)
	   (format "%sprint_spaces (indent_spaces);\n;\n" prefix)
	   (if is-param (format "%sg_print (\"}\\n\");\n" prefix) "")
	   )
	  )
	 ((and (equal (car (cadr field)) 'ptr)
	       (member (cadr (cadr field)) (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*)))
	  (concat
	   (if is-param (format "%sg_print (\"{\\n\");\n" prefix) "")
	   (format "%sserializeio_print_%s_obj (%s, indent_spaces + 2);\n"
		  prefix (downcase-string (cadr (cadr field))) name name)
	   (format "%sprint_spaces (indent_spaces);\n;\n" prefix)
	   (if is-param (format "%sg_print (\"}\\n\");\n" prefix) "")
	   )
	  )
	 ((equal (car (cadr field)) 'bytes)
	  (format "%sserializeio_print_bytes (%s, %d);\n" prefix name (cadr (cadr field))))
	 ((equal (car (cadr field)) 'array)
	  (concat
	   (if is-param (format "%sg_print (\"{\\n\");\n" prefix) "")
	   (format "%s{
%s  gint i;
%s  for (i = 0; i < %s_len; i += 1)
%s    {
%s      print_spaces (indent_spaces);
%s      g_print (\"%%d:\\n\", i);
%s%s      }
%s}
"
		   prefix prefix prefix
		   name
		   prefix
		   prefix
		   prefix
		   prefix
		   (entry-print-field entry (cadr field) (array-index name (cadr field)) (concat prefix "    ") nil)
		   prefix
		   )
	   (if is-param (format "%sg_print (\"}\\n\");\n" prefix) "")))
	 (t (error "unrecognized field type: %s" (cadr field)))))
  )

(defconst *event-id* 0)

(defconst *event-types* nil)

(defun generate-events ()
  (let ((events *event-defs*))
    (while events

      (let* ((event (car events))
	     (uargs (event-uargs-get event))
	     (sargs (event-sargs-get event))
	     (type-prefix (intern (apply (function concat)
					 (append (mapcar (function (lambda (x) (capitalize1 (cadr x)))) uargs)
						 (mapcar (function (lambda (x) (capitalize1 x))) sargs)))))
	     (capprefix (capitalize1 *output-prefix*)))

	(if (and (not uargs) (not sargs))
	    (setq type-prefix "Void"))

	(when (not (member type-prefix *event-types*))
	  (setq *event-types* (cons type-prefix *event-types*))

	  (output-header-file "_edsio")

	  (save-excursion
	    (goto-char *header-typedef-marker*)

	    (insert (format "/* %s%sEventCode.\n */\n\n" capprefix type-prefix))

	    (insert (format "typedef struct _%s%sEventCode %s%sEventCode;\n" capprefix type-prefix capprefix type-prefix))
	    (insert (format "struct _%s%sEventCode { gint code; };\n\n" capprefix type-prefix))

	    (insert (format "typedef struct _%s%sEvent %s%sEvent;\n" capprefix type-prefix capprefix type-prefix))
	    (insert (format "struct _%s%sEvent { gint code; const char* srcfile; guint srcline;%s%s };\n\n" capprefix type-prefix (event-struct-entries event) (event-struct-sys-entries event)))
	    )

	  (insert (format "void %s_generate_%s_event_internal (%s%sEventCode code, const char* srcfile, gint srcline%s);\n"
			  *output-prefix*
			  (downcase-string type-prefix)
			  capprefix
			  type-prefix
			  (event-uargs-plist uargs t)
			  ))
	  (insert (format "#define %s_generate_%s_event(ecode%s) %s_generate_%s_event_internal((ecode),__FILE__,__LINE__%s)\n\n"
			  *output-prefix*
			  (downcase-string type-prefix)
			  (event-uargs-alist uargs t)
			  *output-prefix*
			  (downcase-string type-prefix)
			  (event-uargs-mlist uargs t)))

	  (output-source-file "_edsio")

	  (insert (format "void\n%s_generate_%s_event_internal (%s%sEventCode _code, const char* _srcfile, gint _srcline%s)\n"
			  *output-prefix*
			  (downcase-string type-prefix)
			  capprefix
			  type-prefix
			  (event-uargs-plist uargs t)
			  ))
	  (insert (format "{\n"))
	  (insert (format "  %s%sEvent *_e = g_new0 (%s%sEvent, 1);\n" capprefix type-prefix capprefix type-prefix))
	  (insert (format "  _e->code = _code.code;\n  _e->srcline = _srcline;\n  _e->srcfile = _srcfile;\n"))
	  (insert (event-uargs-copy "_e" event))
	  (insert (event-sargs-copy "_e" event))
	  (insert (format "  eventdelivery_event_deliver ((GenericEvent*) _e);\n"))
	  (insert (format "}\n\n"))

	  ;; Field to string def

	  (unless (equal type-prefix "Void")
	    (save-excursion
	      (goto-char *source-top-marker*)
	      (insert (format "static const char* %s_%s_event_field_to_string (GenericEvent* ev, gint field);\n" capprefix type-prefix))
	      )

	    (insert (format "const char*\n%s_%s_event_field_to_string (GenericEvent* ev, gint field)\n"
			    capprefix type-prefix))
	    (insert (format "{\n"))

	    (unless (equal type-prefix (intern "Ssl"))
	      (insert (format "  %s%sEvent* it = (%s%sEvent*) ev;\n" capprefix type-prefix capprefix type-prefix)))
	    (insert (format "  switch (field)\n"))
	    (insert (format "    {\n"))

	    (let ((uargs (event-uargs-get event))
		  (i 0))
	      (while uargs
		(let ((uarg (car uargs)))
		  (insert (format "    case %d: return eventdelivery_%s_to_string (it->%s);\n" i (cadr uarg) (car uarg)))
		  )
		(setq i (+ i 1))
		(setq uargs (cdr uargs))
		)
	      )

	    (if (< 1 (length (event-sargs-get event)))
		(error "unhandled case, too many sargs"))

	    (when (event-sargs-get event)
	      (let ((sarg (car (event-sargs-get event))))
		(insert (format "    case %d: " (length (event-uargs-get event))))

		(if (not (member sarg '(ssl errno)))
		    (error "what type of sarg is %s" sarg))

		(if (eq sarg 'errno)
		    (insert (format "return g_strdup (g_strerror (it->ev_errno));\n")))

		(if (eq sarg 'ssl)
		    (insert (format "return eventdelivery_ssl_errors_to_string ();\n")))
		)
	      )

	      (insert (format "    default: abort ();\n"))
	      (insert (format "    }\n"))

	    (insert (format "}\n\n"))
	    )
	  )

	(output-header-file "_edsio")

	(insert (format "extern const %s%sEventCode EC_%s%s;\n"
			capprefix
			type-prefix
			capprefix
			(event-name-get event)))

	(insert (format "#define EC_%s%sValue ((%d<<EDSIO_LIBRARY_OFFSET_BITS)+%d)\n\n"
			capprefix
			(event-name-get event)
			*event-id*
			*library-id*))

	(output-source-file "_edsio")

	(insert (format "const %s%sEventCode EC_%s%s = { EC_%s%sValue };\n\n"
			capprefix
			type-prefix
			capprefix
			(event-name-get event)
			capprefix
			(event-name-get event)))


	(save-excursion
	  (goto-char *source-init-marker*)

	  (insert (format "  eventdelivery_initialize_event_def (EC_%s%sValue, EL_%s, %s, \"%s\", \"%s\", %s);\n"
			  capprefix
			  (event-name-get event)
			  (event-level-get event)
			  (event-flags-string event)
			  (event-name-get event)
			  (fixup-oneline event (event-desc-get event))
			  (if (equal type-prefix "Void")
			      "NULL"
			    (format "& %s_%s_event_field_to_string" capprefix type-prefix))))
	  )

	(setq *event-id* (+ 1 *event-id*))

	)

      (setq events (cdr events))
      )
    )
  )

(defun event-flags-string (event)
  (if (member 'ssl (event-sargs-get event))
      "EF_OpenSSL"
    "EF_None")
  )

(defun event-struct-entries (event)
  (apply (function concat)
	 (mapcar (function (lambda (x) (format " %s %s;" (event-type-to-ctype (cadr x)) (car x))))
		 (event-uargs-get event)))
  )

(defun event-struct-sys-entries (event)
  (if (member 'errno (event-sargs-get event))
      " gint ev_errno;"
    "")
  )

(defun event-uargs-copy (name event)
  (apply (function concat)
	 (mapcar (function (lambda (x) (format "  %s->%s = %s;\n" name (car x) (car x))))
		 (event-uargs-get event)))
  )

(defun event-sargs-copy (name event)
  (if (member 'errno (event-sargs-get event))
      (format "  %s->ev_errno = errno;\n" name)
    "")
  )

(defun event-type-to-ctype (etype)
  (let ((it (obj-name-eq etype *all-etype-defs*)))
    (if (not it)
	(message "no ctype for %s" etype))
    (etype-ctype-get it)
    )
  )

(defun event-uargs-plist(uargs need_first)
  (concat
   (if (and need_first uargs) ", " "")
   (format-comlist (function (lambda (x) (format "%s %s" (event-type-to-ctype (cadr x)) (car x)))) uargs))
  )

(defun event-uargs-alist(uargs need_first)
  (concat
   (if (and need_first uargs) ", " "")
   (format-comlist (function (lambda (x) (format "%s" (car x)))) uargs))
  )

(defun event-uargs-mlist(uargs need_first)
  (concat
   (if (and need_first uargs) ", " "")
   (format-comlist (function (lambda (x) (format "(%s)" (car x)))) uargs))
  )

(defun fixup-oneline (event oneline)
  (let ((work (get-buffer-create "*string-tmp2*")))
    (save-excursion
      (set-buffer work)
      (erase-buffer)
      (insert oneline)
      (beginning-of-buffer)

      (while (re-search-forward "${\\(\\w+\\)}" nil t)

 	(let* ((it    (intern (downcase-string (match-string 1))))
	       (uargs (event-uargs-get event))
	       (i     0)
	       (repl  nil))

	  (while uargs

	    (if (eq (car (car uargs)) it)
		(setq repl (format "${%d}" i)))

	    (setq uargs (cdr uargs))
	    (setq i (+ i 1))
	    )

	  (when (eq it 'strerror)
	    (if repl
		(error "No wildcards named STRERROR"))
	    (setq repl (format "${%d}" i))
	    )

	  (when (eq it 'ssl)
	    (if repl
		(error "No wildcards named SSL"))
	    (setq repl (format "${%d}" i))
	    )

	  (if (not repl)
	      (error "Illegal wildcard %s in %s" it oneline))

	  (replace-match repl nil nil)
 	  )
 	)

      (buffer-string)
      )
    )
  )

;; Properties

(defun generate-properties ()
  (let ((cap-prefix (capitalize1 *output-prefix*))
	(unique-types nil))
    (output-header-file "_edsio")

    (insert (format "/* Property definitions */\n\n"))

    ;; Types

    (output-source-file "_edsio")

    (mapcar
     (function
      (lambda (pht)
	(let ((type (prophosttype-type-get pht)))
	  (unless (member type unique-types)
	    (setq unique-types (cons type unique-types))

	    (save-excursion
	      (goto-char *source-init-marker*)

	      ;(message "%s -> %s %s" type (type-free-func type) (member type (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*)))

	      (insert (format "  edsio_initialize_property_type (\"%s\", %s, %s, %s, %s, %s);\n"
			      type
			      (type-free-func type)
			      (type-gs-func type "getter")
			      (type-gs-func type "setter")
			      (type-serialize-func type)
			      (type-unserialize-func type)))
	      )
	    )
	  )
	)
      )
     *prophosttype-defs*
     )

    ;; Host reg

    (mapcar
     (function
      (lambda (prophost)
	(save-excursion
	  (goto-char *source-init-marker*)
	  (insert (format "  edsio_initialize_host_type (\"%s\", %s, %s, %s, %s, %s);\n"
			  (prophost-name-get prophost)
			  (format "(PropertyTableFunc) & edsio_%s_property_table"
				  (downcase-string (prophost-name-get prophost)))
			  (prophost-persist prophost "source")
			  (prophost-persist prophost "sink")
			  (prophost-persist prophost "isset")
			  (prophost-persist prophost "unset")
			  ))
	  )
	)
      )
     *prophost-defs*)

    ;; Compute each distinct (host type) x (prop type)

    (mapcar
     (function
      (lambda (prophost)

	(mapcar
	 (function
	  (lambda (prophosttype)

	    (when (equal (prophosttype-host-get prophosttype) (prophost-name-get prophost))

	      (when (not (member (prophosttype-type-get prophosttype) (prophost-proptypes-get prophost)))
		(prophost-proptypes-set prophost (cons (prophosttype-type-get prophosttype) (prophost-proptypes-get prophost)))
		)
	      )))
	 *prophosttype-defs*
	 )

	;; Output the get/set functions for each property type

	(mapcar
	 (function
	  (lambda (type)

	    (let ((it (property-code-typename type prophost)))

	      ;; Header

	      (output-header-file "_edsio")

	      (insert (format "/* Property get/set for %s/%s\n */\n\n" (prophost-name-get prophost) type))

	      (insert (format "typedef struct _%s %s;\n" it it))

	      (insert (format "struct _%s { guint32 code; };\n\n" it))

	      (insert (format "gboolean edsio_new_%s_%s_property (const char* name, guint32 flags, %s* prop);\n"
			      (downcase-string (prophost-name-get prophost))
			      (type-canon-name type)
			      it
			      ))

	      (insert (format "gboolean %s_get_%s (%s obj, %s prop%s);\n"
			      (downcase-string (prophost-name-get prophost))
			      (type-canon-name type)
			      (prophost-ctype-get prophost)
			      it
			      (prop-type-to-get-fps type)))

	      (insert (format "gboolean %s_set_%s (%s obj, %s prop%s);\n"
			      (downcase-string (prophost-name-get prophost))
			      (type-canon-name type)
			      (prophost-ctype-get prophost)
			      it
			      (prop-type-to-set-fps type)))

	      (insert (format "gboolean %s_unset_%s (%s obj, %s prop);\n"
			      (downcase-string (prophost-name-get prophost))
			      (type-canon-name type)
			      (prophost-ctype-get prophost)
			      it))

	      (insert (format "gboolean %s_isset_%s (%s obj, %s prop);\n\n"
			      (downcase-string (prophost-name-get prophost))
			      (type-canon-name type)
			      (prophost-ctype-get prophost)
			      it))

	      ;; Source

	      (output-source-file "_edsio")

	      (insert (format "gboolean edsio_new_%s_%s_property (const char* name, guint32 flags, %s* prop)\n{\n"
			      (downcase-string (prophost-name-get prophost))
			      (type-canon-name type)
			      it
			      ))
	      (insert (format "  return edsio_new_property (name, \"%s\", \"%s\", flags, (EdsioGenericProperty*) prop);\n" (prophost-name-get prophost) type))
	      (insert (format "}\n\n"))

	      (insert (format "gboolean\n%s_get_%s (%s obj, %s prop%s)\n{\n"
			      (downcase-string (prophost-name-get prophost))
			      (type-canon-name type)
			      (prophost-ctype-get prophost)
			      it
			      (prop-type-to-get-fps type)))
	      (insert (format "  EdsioProperty* ep;\n"))
	      (insert (format "  g_return_val_if_fail (obj, FALSE);\n"))
	      (insert (format "  return (* edsio_property_getter (\"%s\", \"%s\", prop.code, & ep)) (obj, ep%s);\n"
			      (prophost-name-get prophost)
			      type
			      (prop-type-to-args type)
			      ))

	      (insert (format "}\n\n"))

	      (insert (format "gboolean\n%s_set_%s (%s obj, %s prop%s)\n{\n"
			      (downcase-string (prophost-name-get prophost))
			      (type-canon-name type)
			      (prophost-ctype-get prophost)
			      it
			      (prop-type-to-set-fps type)))
	      (insert (format "  EdsioProperty* ep;\n"))
	      (insert (format "  g_return_val_if_fail (obj, FALSE);\n"))
	      (insert (format "  return (* edsio_property_setter (\"%s\", \"%s\", prop.code, & ep)) (obj, ep%s);\n"
			      (prophost-name-get prophost)
			      type
			      (prop-type-to-args type)
			      ))

	      (insert (format "}\n\n"))

	      (insert (format "gboolean\n%s_unset_%s (%s obj, %s prop)\n{\n"
			      (downcase-string (prophost-name-get prophost))
			      (type-canon-name type)
			      (prophost-ctype-get prophost)
			      it))
	      (insert (format "  g_return_val_if_fail (obj, FALSE);\n"))
	      (insert (format "  return edsio_property_unset (\"%s\", \"%s\", prop.code, obj);\n"
			      (prophost-name-get prophost)
			      type
			      ""
			      ))

	      (insert (format "}\n\n"))

	      (insert (format "gboolean\n%s_isset_%s (%s obj, %s prop)\n{\n"
			      (downcase-string (prophost-name-get prophost))
			      (type-canon-name type)
			      (prophost-ctype-get prophost)
			      it))
	      (insert (format "  g_return_val_if_fail (obj, FALSE);\n"))
	      (insert (format "  return edsio_property_isset (\"%s\", \"%s\", prop.code, obj);\n"
			      (prophost-name-get prophost)
			      type
			      ))

	      (insert (format "}\n\n"))

	      )
	    )
	  )
	 (prophost-proptypes-get prophost)
	 )
	)
      )
     *all-prophost-defs*
     )
    )
  )

(defun property-code-typename(type prophost)
  (format "%s%s%sProperty"
	  (capitalize1 *output-prefix*)
	  (prophost-name-get prophost)
	  (capitalize1 type))
  )

(defun prop-typename-ctypes (type)
  (cond ((equal type 'string)
	 (list (list 'arg "const gchar*")))
	((equal type 'uint)
	 (list (list 'arg "guint32")))
	((equal type 'uint32)
	 (list (list 'arg "guint32")))
	((equal type 'uint16)
	 (list (list 'arg "guint16")))
	((equal type 'uint8)
	 (list (list 'arg "guint8")))
	((equal type 'boolean)
	 (list (list 'arg "gboolean")))
	((equal type 'bytes)
	 (list (list 'arg "const guint8*") (list 'arg_len "guint32")))
	((equal type 'object)
	 (list (list 'arg "void*") (list 'arg_type "guint32")))
	((member type (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*))
	 (list (list 'arg (format "Serial%s*" type))))
	((equal (car type) 'bytes)
	 (list (list 'arg "const guint8*")))
	((equal (car type) 'array)
	 (list (list 'arg (format "%s*" (cadr (car (prop-typename-ctypes (cadr type))))))
	       (list 'arg_len "guint32")))
	((equal (car type) 'ptr)
	 (list (list 'arg (format "%s*" (cadr (car (prop-typename-ctypes (cadr type))))))))
	(t (error "unrecognized field type: %s" type)))
  )

(defun prop-type-to-get-fps (type)
  (concat ", "
	  (format-comlist
	   (function
	    (lambda (pair)
	      (format "%s* %s" (cadr pair) (car pair))
	      )
	    )
	   (prop-typename-ctypes type))
	  )
  )

(defun prop-type-to-set-fps (type)
  (concat ", "
	  (format-comlist
	   (function
	    (lambda (pair)
	      (format "%s %s" (cadr pair) (car pair))
	      )
	    )
	   (prop-typename-ctypes type))
	  )
  )

(defun prop-type-to-args (type)
  (concat ", "
	  (format-comlist
	   (function
	    (lambda (pair)
	      (format "%s" (car pair))
	      )
	    )
	   (prop-typename-ctypes type))
	  )
  )

(defun type-canon-name (type)
  ; @@@ does not work for (array ...), etc
  (downcase-string type))

(defun type-serialize-func (type)
  (format "serialize_%s_obj" (downcase-string type))
  )

(defun type-unserialize-func (type)
  (format "unserialize_%s" (downcase-string type))
  )

(defun type-gs-func (type name)
  (if (member type (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*))
      (format "& edsio_property_vptr_%s" name)
    (format "& edsio_property_%s_%s" type name)))

(defun type-free-func (type)
  (if (member type (mapcar (lambda (x) (sertype-name-get x)) *all-sertype-defs*))
      (format "& edsio_property_vptr_free")
    (format "& edsio_property_%s_free" type)))

(defun prophost-persist (prophost func)
  (if (prophost-persist-get prophost)
      (format "(Persist%sFunc) & %s_persist_%s_%s"
	      (capitalize1 func)
	      *output-prefix*
	      (downcase-string (prophost-name-get prophost))
	      func)
    "NULL"))