File: theme-d-compilation1.scm

package info (click to toggle)
theme-d 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 12,784 kB
  • sloc: lisp: 47,684; sh: 4,200; makefile: 455; ansic: 319
file content (1683 lines) | stat: -rw-r--r-- 54,942 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
;; Copyright (C) 2008-2013 Tommi Höynälänmaa
;; Distributed under GNU General Public License version 3,
;; see file doc/GPL-3.



;; *** Translation 1 ***


(import (rnrs exceptions)
	(srfi srfi-1)
	(th-scheme-utilities stdutils)
	(th-scheme-utilities hrecord))


(define xlat-table '())


(define xlat-default-fwd '())


(define (translate-location compiler symtbl expr)
  (let ((location (get-symbol symtbl expr)))
    (if location
	(begin
	  (cond
	   ((hrecord-is-instance? location <keyword>)
	    location)
	   ((hrecord-is-instance? location <normal-variable>)
	    (begin
	      (if (not (check-normal-variable? location))
		  (begin
		    (dvar1-set! location)
		    (raise 'internal-invalid-variable))
		  (if (hfield-ref location 'letrec-variable?)
		      (if (access-letrec-variable?
			   (hfield-ref compiler 'current-letrec-env)
			   location
			   #f)
			  (make-var-ref-to-var location)
			  (raise (list 'unaccessible-letrec-variable
				       (cons 's-name expr))))
		      (make-var-ref-to-var location)))))
	   ((is-target-object? location)
	    location)
	   ((is-t-type-variable? location)
	    location)
	   (else
	    (begin
	      (raise 'internal-invalid-binding)))))
        (begin
	  (set-error-info! compiler expr)
	  (raise 'unbound-variable)))))


(define (translate-atom compiler symtbl toplevel? quoted? sexpr)
  (if (and (not quoted?) (symbol? sexpr))
      (begin (translate-location compiler symtbl sexpr))
      (let* ((type (get-primitive-type sexpr))
	     ;; Should type be an expression or a variable in the following?
	     (to (make-primitive-object
		  type
		  sexpr)))
	to)))


(define (check-illegal-keywords l-exprs)
  (for-each (lambda (sexpr)
	      (if (and (symbol? sexpr) (assq sexpr keywords))
		  (raise (list 'illegal-use-of-keyword
			       (cons 's-keyword sexpr)))))
	    l-exprs))

;;; Translation of lists

(define (translate-list-expr compiler symtbl toplevel? expr-head expr-tail)
  (if (not (hrecord? expr-head))
      (raise 'internal-error)
      (let ((proc (hrecord-type-inquire
		   xlat-table (hrecord-type-of expr-head))))
	(if (not (eq? (hrecord-type-of expr-head) <keyword-quote>))
	    (begin
	      (if (not (list? expr-tail))
		  (raise 'expected-list))
	      (check-illegal-keywords expr-tail)))
	(let ((result
	       (if proc
		   (proc compiler symtbl toplevel? expr-head expr-tail)
		   (xlat-default-fwd compiler symtbl toplevel?
				     expr-head expr-tail))))
	  result))))


;; Translation of an arbitrary expression
;; If the expression is a list translate the head
;; and call translate-expr recursively (dispatch used).
(define (translate-expr compiler symtbl toplevel? expr)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? symtbl <environment>))
  (assert (boolean? toplevel?))
  (assert (not (hrecord? expr)))
  (check-env symtbl)
  (let ((result
	 (cond
	  ((pair? expr)
	   (let ((prev-expr (hfield-ref compiler 'current-expr)))
	     (hfield-set! compiler 'current-expr expr)
	     (let ((res2
		    (if (source-code-element? (car expr))
			(translate-list-expr
			 compiler
			 symtbl
			 toplevel?
			 (translate-expr compiler symtbl #f (car expr))
			 (cdr expr))
			(raise 'dispatch-error))))
	       (hfield-set! compiler 'current-expr prev-expr)
	       res2)))
	  ((source-code-element? expr)
	   (translate-atom compiler symtbl #f #f expr))
	  (else
	   (dvar1-set! expr)
	   (raise 'invalid-dispatch)))))
    result))


(set! translate-expr-fwd translate-expr)


(define (wrap-compound-expression compiler env s-subexprs)
  (assert (list? s-subexprs))
  (assert (hrecord-is-instance? env <environment>))
  (do-wrap-compound-expression
   s-subexprs
   (lambda (s-expr)
     (translate-expr compiler env #f s-expr))))


(define (translate-define-mutable-expression
         compiler symtbl toplevel? expr-tail volatile?)
  (if (not (= (length expr-tail) 3))
      (raise 'invalid-define-mutable-expr)
      (let ((s-name (car expr-tail))
            (s-type (cadr expr-tail))
            (s-value (caddr expr-tail)))
        (if (not (s-symbol? s-name))
            (raise 'invalid-variable-name)
	    (begin
	      (hfield-set! compiler 's-cur-toplevel-def s-name)
	      (let ((type
		     (translate-expr compiler symtbl #f s-type))
		    (value-expr 
		     (translate-expr compiler symtbl #f s-value)))
		(if (not (is-legal-value-ref? compiler value-expr))
		    (raise (list 'illegal-incomplete-ref
				 (cons 's-name s-name)
				 (cons 'address '()))))
		(if (is-known-object? type)
		    (let* ((bound-value (get-symbol symtbl s-name))
			   (rebind? (check-existing-binding? compiler
							     bound-value))
			   (type-val (get-entity-type value-expr))
			   (val (get-entity-value value-expr))
			   (old-type (if (eq? bound-value #f)
					 #f
					 (get-entity-type bound-value)))
			   (binder (compiler-get-binder compiler)))
		      (cond
		       ((and rebind? (is-target-object? bound-value))
			(raise 'definition-mutability-mismatch))
		       ((and rebind? (not (is-normal-variable? bound-value)))
			;; If the code works correctly we should never enter here.
			(raise 'internal-error))
		       ((and rebind?
			     (not (eq? volatile?
				       (hfield-ref bound-value 'volatile?))))
			(raise 'volatility-mismatch))
		       ((not (is-t-instance? binder type tt-type))
			(raise 'not-a-type))
		       ((not (is-t-subtype? binder type-val type))
			(raise 'type-spec-violated))
		       ((and (not (eq? old-type #f))
			     (not (equal-types? binder type old-type)))
			(raise (list 'mutable-forward-def-type-mismatch
				     (cons 's-name s-name)
				     (cons 'tt-new type)
				     (cons 'tt-old old-type))))
		       (else
			(let* ((variable
				(if (not (eq? bound-value #f))
				    (begin
				      (assert (hrecord-is-instance?
					       bound-value
					       <normal-variable>))
				      (hfield-set! bound-value 'forward-decl?
						   #f)
				      bound-value)
				    (let ((address
					   (compiler-alloc-loc compiler
							       s-name #t)))
				      (make-normal-variable0
				       address type #t #f #f
				       volatile?
				       #f #f '() '()
				       #f #f))))
			       (result
				(make-normal-var-def
				 type
				 variable
				 value-expr
				 rebind?)))
			  (if (not rebind?)
			      (add-symbol! symtbl s-name variable))
			  (hfield-set! compiler 's-cur-toplevel-def '())
			  result))))
		    (raise 'define-mutable:invalid-type))))))))


(define (translate-define-expression compiler symtbl toplevel? expr-tail)
  (let ((arg-count (length expr-tail)))
    (if (not (or (= arg-count 2) (= arg-count 3)))
	(raise 'invalid-define)
	(let* ((has-type-spec? (= arg-count 3))
	       (s-name (car expr-tail))
	       (s-type (if has-type-spec? (cadr expr-tail) '()))
	       (s-value (if has-type-spec? (caddr expr-tail) (cadr expr-tail)))
	       (binder (compiler-get-binder compiler)))
	  (if (not (s-symbol? s-name))
	      (raise 'invalid-variable-name)
	      (begin
		(hfield-set! compiler 's-cur-toplevel-def s-name)
		(let* ((bound-value (get-symbol symtbl s-name))
		       (rebind? (check-existing-binding? compiler bound-value))
		       (tt-declared0
			(if has-type-spec?
			    (translate-expr compiler symtbl #f s-type)
			    '()))
		       (tmp1 (begin (set-proc-expr compiler s-value
						   'toplevel s-name) 0))
		       (value-expr 
			(translate-expr compiler symtbl #f s-value))
		       (tmp2 (begin (unset-proc-expr compiler s-value) 0))
		       (tt-old (if (eq? bound-value #f)
				   #f
				   (get-entity-type bound-value)))
		       (tt-actual (get-entity-type value-expr))
		       (tt-declared (if has-type-spec?
					tt-declared0
					tt-actual)))
		  (cond
		   ((and rebind?
			 (not (is-target-object? bound-value)))
		    (raise 'declaration-mutability-mismatch))
		   ((and rebind?
			 (null? (hfield-ref bound-value 'address)))
		    (raise 'internal:invalid-declaration))
		   ((or (not (is-known-object? tt-actual))
			(not (is-t-instance? binder tt-actual tt-type)))
		    (raise 'invalid-value-type))
		   ((or (not (is-known-object? tt-declared))
			(not (is-t-instance? binder tt-declared tt-type)))
		    (raise 'invalid-declared-type))
		   ((and (not (eq? tt-old #f))
			 (not (is-t-subtype? binder tt-declared tt-old)))
		    (raise (list 'forward-def-type-mismatch
				 (cons 's-name s-name)
				 (cons 'tt-new tt-declared)
				 (cons 'tt-old tt-old))))
		   ((and has-type-spec?
			 (not (is-t-subtype? binder tt-actual tt-declared)))
		    (raise 'type-spec-violated))
		   (else
		    (let* ((to-value (get-entity-value value-expr))
			   (to2
			    ;; If to-value is null we know only the type
			    ;; and address of the variable and we set
			    ;; al-field-values to #f.
			    (if (and 
				 (not-null? to-value)
				 (not
				  (hrecord-is-instance? value-expr
							<prim-proc-ref>))
				 (not
				  (hrecord-is-instance? value-expr
							<checked-prim-proc>)))
				to-value
				(make-unknown-object
				 tt-declared
				 (is-final-class? binder tt-declared))))
			   (address (if rebind?
					(hfield-ref bound-value 'address) 
					(compiler-alloc-loc compiler s-name #t)))
			   (to-new 
			    (if (null? (hfield-ref to2 'address))
				(make-object-with-address
				 to2 address)
				to2))
			   (variable
			    (make-normal-variable
			     address
			     tt-actual
			     (hfield-ref to2 'exact-type?)
			     #t
			     #f
			     to2
			     #f)))
		      (if (and
			   rebind?
			   (not (is-legal-value-object? compiler to-new)))
			  (raise (list 'illegal-incomplete-value
				       (cons 's-name s-name)
				       (cons 'address address))))
		      (if (not (is-legal-value-ref? compiler value-expr))
			  (raise (list 'illegal-incomplete-ref
				       (cons 's-name s-name)
				       (cons 'address address))))
		      ;; We bind the variable name to an object.
		      (if (not rebind?)
			  (add-symbol! symtbl s-name to-new)
			  (begin
			    (set-object1! bound-value to-new)
			    ;; to-new may be incomplete if the value
			    ;; expression is a declared value.
			    (hfield-set! bound-value 'incomplete? #f)))
		      (hfield-set! compiler 's-cur-toplevel-def '())
		      (make-normal-var-def
		       tt-declared variable value-expr rebind?)))))))))))


(define (translate-constructor-expr compiler symtbl to-class)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? symtbl <environment>))
  (assert (is-target-object? to-class))
  (let ((binder (compiler-get-binder compiler)))
    (cond
     ((is-t-apti? to-class)
      (if (hfield-ref compiler 'inside-param-def?)
	  (get-empty-constructor to-class)
	  (raise 'illegal-abstract-class-for-constructor)))
     ((not (is-known-object? to-class))
      (raise 'illegal-class-for-constructor))
     ((and (not (hfield-ref compiler 'inside-param-def?))
	   (not (is-t-instance? binder to-class tc-class)))
      (raise 'constructor-not-a-class))
     ((is-t-type-variable? to-class)
      (make-hrecord <expr-constructor>
		    tt-general-proc-with-value
		    #f
		    #f
		    '() 
		    #t
		    #t
		    #t
		    '()
		    to-class))
     ((is-t-instance? binder to-class tpc-pair)
      (let ((type (translate-simple-proc-class-expression
		   binder
		   (tno-field-ref to-class 'l-tvar-values)
		   to-class #t #t #f #f)))
	(make-hrecord <expr-constructor>
		      type
		      #t
		      #t
		      '() 
		      #t
		      #t
		      #f
		      '()
		      to-class)))
     (else
      (let ((type-constructor
	     (tno-field-ref to-class 'type-constructor))
	    (access
	     (tno-field-ref to-class 's-ctr-access))
	    (modname-cl (tno-field-ref to-class 'module))
	    (modname-cur (get-current-module-name compiler)))
	(cond
	 ((null? type-constructor)
	  (raise 'internal-undefined-constructor))
	 ((not (check-constructor-access? access modname-cur modname-cl))
	  (raise 'constructor-access-violation))
	 (else (get-constructor-expr to-class type-constructor))))))))


(define (translate-set-expression
	 compiler symtbl toplevel? constant? expr-tail)
  (if (not (= (length expr-tail) 2))
      (raise 'invalid-set)
      (let ((s-name (car expr-tail))
	    (s-value (cadr expr-tail)))
	(let* ((value-expr 
		(translate-expr compiler symtbl #f s-value))
	       (var (translate-expr compiler symtbl #f s-name))
	       (variable
		(cond
		 ((is-target-object? var) var)
		 ((hrecord-is-instance? var <variable-reference>)
		  (hfield-ref var 'variable))
		 (else
		  (raise 'set!:invalid-variable)))))
	  (cond
	   ((eqv? variable #f)
	    (raise 'unbound-variable-in-set!))
	   ((not (is-normal-variable? variable))
	    (raise 'trying-to-set-nonmutable-variable))
	   ((hfield-ref variable 'read-only?)
	    (raise 'attempted-setting-of-read-only-variable))
	   ((entity-type-is-none1? (compiler-get-binder compiler) value-expr)
	    (raise 'set-expression-with-type-none))
	   ;; We need not check may-change-variable? for module
	   ;; references since they are toplevel variables.
	   ((and (symbol? s-name) (not (may-change-variable? symtbl s-name)))
	    (raise 'purity-violation-in-set-expr))
	   (else
	    ;; We may set the <set-expression>'s to be pure
	    ;; because testing may-change-variable? takes care
	    ;; of purity violations.
	    (if (hfield-ref compiler 'inside-param-def?)
		(make-hrecord <set-expression>
			      tt-none
			      #t
			      #t
			      '()
			      #t
			      #f
			      #t
			      '()
			      (entity-always-returns? value-expr)
			      (entity-never-returns? value-expr)
			      variable
			      value-expr)
		(let ((value-type (get-entity-type value-expr))
		      (variable-type (get-entity-type variable)))
		  (if (not (is-t-subtype?
			    (compiler-get-binder compiler)
			    value-type
			    variable-type))
		      (begin
			(dvar1-set! value-type)
			(dvar2-set! variable-type)
			(raise 'type-mismatch-in-set!))
		      (make-hrecord <set-expression>
				    tt-none
				    #t
				    #t
				    '()
				    #t
				    #f
				    #f
				    '()
				    (entity-always-returns? value-expr)
				    (entity-never-returns? value-expr)
				    variable
				    value-expr))))))))))


(define (translate-field compiler env s-field)
  (if (pair? s-field)
      (let ((len (length s-field)))
	(if (or (= len 4) (= len 5))
	    (let* ((field-name 
		    (car s-field))
		   (r-field-type
		    (translate-expr compiler env #f 
				    (cadr s-field)))
		   (r-read-access (list-ref s-field 2))
		   (r-write-access (list-ref s-field 3))
		   (has-init-value? (= len 5))
		   (r-init-value
		    (if has-init-value?
			(translate-expr compiler env #f 
					(list-ref s-field 
						  i-source-field-init-value))
			'()))
		   (binder (compiler-get-binder compiler)))
	      (cond
	       ((entity-is-none1? binder r-field-type)
		(raise 'none-as-a-field-type))
	       ((not (memv r-read-access '(hidden module public)))
		(raise 'invalid-read-access))
	       ((not (memv r-write-access '(hidden module public)))
		(raise 'invalid-write-access))
	       ((not (is-t-subtype?
		      binder
		      (get-entity-type r-field-type)
		      tt-type))
		(raise 'invalid-field-type))
	       ((and has-init-value?
		     (not
		      (is-t-instance?
		       binder
		       r-init-value
		       r-field-type)))
		(raise (list 'field-initializer-type-mismatch
			     (cons 's-field-name field-name)
			     (cons 'tt-field r-field-type)
			     (cons 'tt-init (get-entity-type r-init-value)))))
	       (else
		(make-field field-name r-field-type
			    r-read-access r-write-access
			    has-init-value? r-init-value))))
	    (raise 'invalid-field-spec-length)))
      (raise 'invalid-field-spec)))


(define (translate-field-list compiler s-field-list env)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (list? s-field-list))
  (assert (hrecord-is-instance? env <environment>))
  (let ((result
	 (map (lambda (s-field) (translate-field compiler env s-field))
	      s-field-list)))
    result))


(define (do-translate-class compiler env s-name decl
			    r-superclass t-field-list
			    inheritable? immutable? eq-by-value?
			    ctr-access)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? env <environment>))
  (assert (symbol? s-name))
  (assert (and (is-target-object? decl) (hfield-ref decl 'incomplete?)))
  (assert (hrecord-is-instance? r-superclass <target-object>))
  (assert (list? t-field-list))
  (assert (boolean? inheritable?))
  (assert (boolean? immutable?))
  (assert (boolean? eq-by-value?))
  (assert (memq ctr-access gl-access-specifiers))
  ;; Should we pass address to make-target-class?
  (let* ((to (make-target-class
	      (hfield-ref decl 'address)
	      (get-current-module-name compiler)
	      r-superclass t-field-list
	      inheritable? immutable? eq-by-value?
	      ctr-access))
	 ;; Classes are always declared forward.
	 (variable
	  (bind-object! compiler env s-name decl to tc-class))
	 (binder (compiler-get-binder compiler)))
    (make-constructor! binder decl)
    (make-hrecord <class-definition>
		  tt-none #t #t '()
		  #f #f #f '()
		  variable tc-class '() #t #f #f)))


(define (translate-define-class compiler symtbl expr-tail)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? symtbl <environment>))
  (assert (list? expr-tail))
  (if (= (length expr-tail) 8)
      (let ((s-name (car expr-tail))
	    (s-superclass (cadr expr-tail))
	    (inheritable? (caddr expr-tail))
	    (immutable? (list-ref expr-tail 3))
	    (eq-by-value? (list-ref expr-tail 4))
	    (ctr-access (list-ref expr-tail 5))
	    (s-zero-proc (list-ref expr-tail 6))
	    (s-field-list (list-ref expr-tail 7)))
	(if (or
	     (not (boolean? inheritable?))
	     (not (boolean? immutable?))
	     (not (boolean? eq-by-value?))
	     (not (memq ctr-access gl-access-specifiers)))
	    (raise 'syntax-error-in-define-class))
	(if (null? s-superclass)
	    (raise 'superclass-not-specified))
	(let* ((bound-value (get-symbol symtbl s-name))
	       (rebind? (check-existing-binding? compiler bound-value))
	       (decl
		(if rebind?
		    empty-expression
		    (translate-forward-declaration compiler symtbl s-name
						   tc-class #t #f)))
	       (to-decl
		(if rebind?
		    (begin
		      (assert (not (eq? bound-value #f)))
		      (get-entity-value bound-value))
		    (hfield-ref (hfield-ref decl 'variable) 'value))))
	  (let* ((to-superclass
		  (translate-expr compiler symtbl #f s-superclass))
		 (binder (compiler-get-binder compiler)))
	    (if (not
		 (and (is-target-object? to-superclass)
		      (is-t-instance? binder to-superclass tc-class)))
		(raise 'invalid-superclass))
	    (let ((t-field-list (translate-field-list compiler s-field-list
						      symtbl)))
	      (cond
	       ((entity-is-none1? (compiler-get-binder compiler)
				  to-superclass)
		(raise 'none-as-superclass))
	       ;; Myöskään piilossa olevat kenttien nimet eivät
	       ;; saa mennä päällekäin.
	       ((contains-duplicate-field-names? to-superclass t-field-list)
		(raise 'duplicate-field-name))
	       ;; If the superclass is incomplete to-superclass should be null.
;;	       ((is-forward-decl? var-superclass)
;;		(raise 'forward-declared-superclass))
	       ((not (tno-field-ref to-superclass 'inheritable?))
		(raise 'noninheritable-superclass))
	       ((tno-field-ref to-superclass 'goops?)
		(raise 'illegal-goops-superclass))
	       (else
		(let* ((class-def
			(do-translate-class compiler symtbl s-name to-decl
					    to-superclass t-field-list
					    inheritable? immutable? eq-by-value?
					    ctr-access))
		       (var (hfield-ref class-def 'variable))
;;		       (to-clas (hfield-ref var 'value)))
		       (to-clas to-decl))
		  (assert (not-null? to-clas))
		  (if (and (tno-field-ref to-clas 'immutable?)
			   (not (is-valid-immutable-class? to-clas)))
		      (raise 'invalid-immutable-class))
		  (if (not (is-null-sexpr? s-zero-proc))
		      (begin
			(let* ((zero-proc-body
				(translate-expr compiler symtbl #f s-zero-proc))
			       (zero-proc-class
				(translate-simple-proc-class-expression
				 binder
				 '()
				 to-clas #t #t #f #f))
			       (zero-proc
				(make-hrecord
				 <procedure-expression>
				 zero-proc-class #t #t '()
				 #t #f #f '()
				 '() '() '() to-clas
				 zero-proc-body
				 'zero
				 '()
				 '()
				 #t
				 #f
				 #t
				 #f
				 #f))
			       (zero-setting
				(make-hrecord <zero-setting-expr>
					      zero-proc-class #t #t '()
					      #f #f #f '()
					      var zero-proc #f)))
			  (list decl class-def zero-setting)))
		      (list decl class-def)))))))))
      (raise 'syntax-error-in-define-class)))


(define (get-first-arg-type arg-types)
  (if (pair? arg-types)
      (car arg-types)
      (raise 'syntax-error-in-make)))


(define (make-type-var compiler type-var-name)
  ;; Type variables are lexical.
  (let ((type-var
	 (make-type-variable
	  (compiler-alloc-loc compiler type-var-name #f))))
    (cons type-var-name type-var)))


(define (make-type-vars compiler type-var-names)
  (map (lambda (type-var-name) (make-type-var compiler type-var-name))
       type-var-names))


(define (translate-procedure-expression compiler env
					s-arglist s-result-type
					s-attr
					s-body
					s-kind s-name)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? env <environment>))
  (strong-assert (list? s-arglist))
  (strong-assert (list? s-body))
  (check-env env)
  (let* ((names (map car s-arglist))
	 (s-arg-descs (map cadr s-arglist))
	 (arg-descs
	  (map (lambda (subexpr)
		 (translate-expr compiler env #f subexpr))
	       s-arg-descs))
	 (binder (compiler-get-binder compiler))
	 (impl-arg-types (get-impl-arg-types binder arg-descs)))
    (let* ((alloc-var (lambda (name toplevel?)
			(compiler-alloc-loc compiler name toplevel?)))
	   (arg-bindings
	    (make-argument-bindings binder alloc-var names
				    impl-arg-types))
	   (arg-variables (map cdr arg-bindings))
	   (t-result-type
	    (if (not-null? s-result-type)
		(translate-expr compiler env #f
				s-result-type)
		tt-none))
	   (attr (parse-proc-attributes s-attr))
	   (pure-proc? (hfield-ref attr 'pure?))
	   (force-pure? (hfield-ref attr 'force-pure?))
	   (appl-always-returns? (hfield-ref attr 'always-returns?))
	   (appl-never-returns? (hfield-ref attr 'never-returns?))
	   (static-method? (hfield-ref attr 'static-method?))
	   (proc-type (translate-simple-proc-class-expression
		       binder
		       arg-descs t-result-type
		       pure-proc?
		       appl-always-returns?
		       appl-never-returns?
		       static-method?))
	   (env-with-args
	    (if pure-proc?
		(make-pure-proc-env
		 env arg-bindings)
		(make-environment
		 env arg-bindings))))
      (let* ((cur-letrec-env (hfield-ref compiler
					 'current-letrec-env))
	     (old-inside-proc?
	      (if (null? cur-letrec-env)
		  #f
		  (hfield-ref cur-letrec-env 'inside-proc?))))
	(if (not-null? cur-letrec-env)
	    (hfield-set! cur-letrec-env 'inside-proc? #t))
	(let ((t-body
	       (wrap-compound-expression compiler env-with-args
					 s-body))
	      (inside-param-def? (hfield-ref compiler 'inside-param-def?)))
	  (if (not-null? cur-letrec-env)
	      (hfield-set! cur-letrec-env 'inside-proc?
			   old-inside-proc?))
	  ;; If purity specifier is gl-force-pure
	  ;; the procedure body is allowed to be nonpure.
	  (cond
	   ((and (not inside-param-def?)
		 pure-proc?
		 (not force-pure?)
		 (not (is-pure-entity? t-body)))
	    (dvar1-set! t-body)
	    (raise 'purity-mismatch))
	   ((and (not inside-param-def?)
		 appl-always-returns?
		 (not (entity-always-returns? t-body)))
	    (raise 'proc-attribute-mismatch))
	   ((and (not inside-param-def?)
		 appl-never-returns?
		 (not (entity-never-returns? t-body)))
	    (raise 'proc-attribute-mismatch))
	   (else
	    (let ((body-never-returns?
		   (entity-never-returns? t-body)))
	      (if (and (not (hfield-ref compiler 'inside-param-def?))
		       (not body-never-returns?)
		       (not (check-procedure-result-type?
			     binder
			     (get-entity-type t-body)
			     t-result-type)))
		  (begin
		    (dvar1-set! compiler)
		    (dvar2-set! env)
		    (dvar3-set! (list s-body body-never-returns?))
		    (dvar4-set! (list t-body t-result-type))
		    (raise (list 'result-type-mismatch-1
				 (cons 'declared-type t-result-type)
				 (cons 'actual-type (get-entity-type t-body)))))
		  (let ((to (make-procedure proc-type #t #f
					    '() s-name '()))
			(need-revision?
			 (hfield-ref compiler
				     'inside-param-def?)))
		    (make-hrecord <procedure-expression>
				  proc-type #t #t '()
				  #t #t need-revision? to
				  names
				  arg-descs
				  arg-variables
				  t-result-type
				  t-body
				  s-kind
				  s-name
				  '()
				  pure-proc?
				  force-pure?
				  appl-always-returns?
				  appl-never-returns?
				  static-method?)))))))))))


(define (translate-procedure-expression-aut compiler env
					    s-arglist
					    s-attr
					    s-body
					    s-kind s-name)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? env <environment>))
  (strong-assert (list? s-arglist))
  (strong-assert (list? s-body))
  (check-env env)
  (let* ((names (map car s-arglist))
	 (s-arg-descs (map cadr s-arglist))
	 (arg-descs
	  (map (lambda (subexpr)
		 (translate-expr compiler env #f subexpr))
	       s-arg-descs))
	 (binder (compiler-get-binder compiler))
	 (impl-arg-types (get-impl-arg-types binder arg-descs)))
    (let* ((alloc-var (lambda (name toplevel?)
			(compiler-alloc-loc compiler name toplevel?)))
	   (arg-bindings
	    (make-argument-bindings binder alloc-var names
				    impl-arg-types))
	   (arg-variables (map cdr arg-bindings))
	   (attr (parse-proc-attributes s-attr))
	   (pure-proc? (hfield-ref attr 'pure?))
	   (force-pure? (hfield-ref attr 'force-pure?))
	   (appl-always-returns? (hfield-ref attr 'always-returns?))
	   (appl-never-returns? (hfield-ref attr 'never-returns?))
	   (static-method? (hfield-ref attr 'static-method?))
	   (env-with-args
	    (if pure-proc?
		(make-pure-proc-env
		 env arg-bindings)
		(make-environment
		 env arg-bindings))))
      (let* ((cur-letrec-env (hfield-ref compiler
					 'current-letrec-env))
	     (old-inside-proc?
	      (if (null? cur-letrec-env)
		  #f
		  (hfield-ref cur-letrec-env 'inside-proc?))))
	(if (not-null? cur-letrec-env)
	    (hfield-set! cur-letrec-env 'inside-proc? #t))
	(let* ((t-body
		(wrap-compound-expression compiler env-with-args
					  s-body))
	       (t-result-type (get-entity-type t-body))
	       (proc-type (translate-simple-proc-class-expression
			   binder
			   arg-descs t-result-type
			   pure-proc?
			   appl-always-returns?
			   appl-never-returns?
			   static-method?))
	       (inside-param-def? (hfield-ref compiler 'inside-param-def?)))
	  (if (not-null? cur-letrec-env)
	      (hfield-set! cur-letrec-env 'inside-proc?
			   old-inside-proc?))
	  ;; If purity specifier is gl-force-pure
	  ;; the procedure body is allowed to be nonpure.
	  (cond
	   ((and (not inside-param-def?)
		 pure-proc?
		 (not force-pure?)
		 (not (is-pure-entity? t-body)))
	    (dvar1-set! t-body)
	    (raise 'purity-mismatch))
	   ((and (not inside-param-def?)
		 appl-always-returns?
		 (not (entity-always-returns? t-body)))
	    (raise 'proc-attribute-mismatch))
	   ((and (not inside-param-def?)
		 appl-never-returns?
		 (not (entity-never-returns? t-body)))
	    (raise 'proc-attribute-mismatch))
	   (else
	    (let ((body-never-returns?
		   (entity-never-returns? t-body)))
	      (let ((to (make-procedure proc-type #t #f '()
					s-name '()))
		    (need-revision?
		     (hfield-ref compiler
				 'inside-param-def?)))
		(make-hrecord <procedure-expression>
			      proc-type #t #t '()
			      #t #t need-revision? to
			      names
			      arg-descs
			      arg-variables
			      t-result-type
			      t-body
			      s-kind
			      s-name
			      '()
			      pure-proc?
			      force-pure?
			      appl-always-returns?
			      appl-never-returns?
			      static-method?))))))))))


(define (translate-param-proc-expr compiler symtbl
				   type-params
				   s-arglist
				   s-result-type
				   s-body
				   s-attr
				   s-kind s-name
				   aut-result-type?)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? symtbl <environment>))
  (assert (boolean? aut-result-type?))
  (let ((inside-param-def-old?
	 (hfield-ref compiler 'inside-param-def?)))
    (hfield-set! compiler 'inside-param-def? #t)
    (let ((r-type-params (map cdr type-params))
	  (old-fixed-tvars (compiler-get-fixed-tvars compiler)))
      (compiler-set-fixed-tvars!
       compiler
       (append old-fixed-tvars r-type-params))
      (let* ((r-proc0
	      ;; The parametrized procedure is named but the component
	      ;; procedure is not.
	      (if aut-result-type?
		  (translate-procedure-expression-aut
		   compiler symtbl s-arglist
		   s-attr s-body '() '())
		  (translate-procedure-expression
		   compiler symtbl s-arglist s-result-type
		   s-attr s-body '() '())))
	     (r-proc-type (get-entity-type r-proc0))
	     (to-param-proc-class
	      (make-param-proc-class-object
	       "instance of :param-proc"
	       r-type-params
	       r-proc-type))
	     (to-param-proc
	      (make-param-proc-object
	       s-name
	       to-param-proc-class
	       '()
	       '()))
	     (r-proc (make-param-proc2 r-type-params
				       to-param-proc-class r-proc0
				       s-kind
				       '()
				       to-param-proc)))
	(tno-field-set! to-param-proc 'x-value-expr r-proc0)
	(compiler-set-fixed-tvars! compiler old-fixed-tvars)
	(hfield-set! compiler 'inside-param-def? inside-param-def-old?)
	r-proc))))


(define (translate-general-proc-type1 compiler env1 simple? s-arg-list
				      s-result-type s-attributes)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? env1 <environment>))
  (assert (boolean? simple?))
  (let* ((arg-desc-exprs
	  (map* (lambda (expr)
		  (translate-expr compiler env1 #f expr))
		s-arg-list))
	 (binder (compiler-get-binder compiler))
	 (parsed-attr (parse-proc-attributes s-attributes))
	 (pure-proc? (hfield-ref parsed-attr 'pure?))
	 (appl-always-returns? (hfield-ref parsed-attr 'always-returns?))
	 (appl-never-returns? (hfield-ref parsed-attr 'never-returns?))
	 (static-method? (hfield-ref parsed-attr 'static-method?))
	 (result-type (translate-expr compiler env1 #f s-result-type)))
    (if (not (and-map? (lambda (argdesc) (is-type? binder argdesc))
		       arg-desc-exprs))
	(raise 'procedure-type-arg-syntax-error))
    (translate-general-proc-type-expression
     binder
     simple?
     arg-desc-exprs
     result-type
     pure-proc?
     appl-always-returns?
     appl-never-returns?
     static-method?)))


(define (translate-general-proc-type compiler env1 expr-tail simple?)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? env1 <environment>))
  (assert (boolean? simple?))
  (if (or (not (pair? expr-tail)) (not (= (length expr-tail) 3)))
      (raise 'invalid-procedure-class-definition)
      (let ((s-arg-list (car expr-tail))
	    (s-result-type (list-ref expr-tail 1))
	    (s-attributes (list-ref expr-tail 2)))
	(translate-general-proc-type1 compiler env1 simple? s-arg-list
				      s-result-type
				      s-attributes))))


(define (do-translate-param-proc-class compiler local-env
				       type-params s-arg-list s-result-type
				       s-attr)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? local-env <environment>))
  (let* ((r-type-params (map cdr type-params))
	 (arg-desc-exprs
	  (map* (lambda (expr)
		  (translate-expr compiler local-env #f expr))
		s-arg-list))
	 (attr (parse-proc-attributes s-attr))
	 (pure-proc? (hfield-ref attr 'pure?))
	 (appl-always-returns? (hfield-ref attr 'always-returns?))
	 (appl-never-returns? (hfield-ref attr 'never-returns?))
	 ;; Maybe we should not allow attribute "static" with procedure types.
	 (static-method? (hfield-ref attr 'static-method?))
	 (result-type (translate-expr compiler local-env #f s-result-type))
	 (binder (compiler-get-binder compiler))
	 (inst-type (translate-simple-proc-class-expression
		     binder
		     arg-desc-exprs result-type
		     pure-proc? appl-always-returns? appl-never-returns?
		     static-method?)))
    (make-param-proc-class-object "instance of :param-proc"
				  r-type-params
				  inst-type)))


(define (do-translate-param-proc-class2 compiler env1
					s-tvars s-arg-list s-result-type
					s-attr)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? env1 <environment>))
  (let* ((type-params (make-type-vars compiler s-tvars))
	 (local-env (make-environment
		     env1 type-params)))
    (do-translate-param-proc-class compiler local-env type-params
				   s-arg-list s-result-type
				   s-attr)))


(define (translate-param-proc-class compiler env1 expr-tail)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? env1 <environment>))
  (if (or (not (pair? expr-tail)) (not (= (length expr-tail) 4)))
      (raise 'invalid-param-proc-class-definition)
      (let ((s-tvars (car expr-tail))
	    (s-arg-list (cadr expr-tail))
	    (s-result-type (list-ref expr-tail 2))
	    (s-attr (list-ref expr-tail 3)))
	(do-translate-param-proc-class2 compiler env1
					s-tvars s-arg-list s-result-type
					s-attr))))


(define (parse-let-var-nonconstant compiler env1 varspec volatile?)
  (if (not (= (length varspec) 3))
      (raise 'invalid-let-variable)
      (let ((name (car varspec)))
	(if (not (symbol? name)) (raise 'invalid-let-var-name))
	(let* ((to-name (make-primitive-object tc-symbol name))
	       (type (translate-expr compiler env1 #f (cadr varspec))))
	  (if (not (is-type?
		    (compiler-get-binder compiler) type))
	      (raise (list 'invalid-let-var-type (cons 's-name name))))
	  (let* ((init-expr (translate-expr
			     compiler
			     env1
			     #f 
			     (caddr varspec)))
		 (binder (compiler-get-binder compiler))
		 (result
		  (cond
		   ((and (not (hfield-ref compiler 'inside-param-def?))
			 (entity-is-none1? binder type))
		    (raise 'none-as-declared-type))
		   ((and (not (hfield-ref compiler 'inside-param-def?))
			 (entity-type-is-none1? binder init-expr))
		    (raise (list 'initializer-expression-with-type-none
				 (cons 's-name name))))
		   (else
		    (if (not (hfield-ref compiler 'inside-param-def?))
			(let ((to-init-type (get-entity-type init-expr)))
			  (cond
			   ;; The following check is probably unnecessary.
			   ((null? type)
			    (raise 'internal-invalid-letvar-type-1))
			   ((null? to-init-type)
			    (raise 'internal-invalid-letvar-type-2))
			   ((is-t-subtype?
			     binder
			     to-init-type
			     type)
			    (let ((variable
				   (make-normal-variable5
				    (compiler-alloc-loc compiler name #f)
				    type
				    #f
				    #f
				    volatile?
				    '())))
			      (list to-name
				    variable
				    variable type init-expr
				    gl-false)))
			   (else
			    (raise (list 'let:type-mismatch
					 (cons 's-name name)
					 (cons 'tt-declared type)
					 (cons 'tt-actual to-init-type))))))
			(let ((variable 
			       (make-normal-variable6
				(compiler-alloc-loc compiler name #f)
				type
				#t
				(is-final-class? binder type)
				#f
				volatile?
				#f
				'()
				'())))
			  (list to-name
				variable
				variable type init-expr
				gl-false)))))))
	    result)))))


(define (parse-let-var-constant compiler env1 varspec)
  (if (not (or (= (length varspec) 2) (= (length varspec) 3)))
      (raise 'invalid-let-variable)
      (let ((has-type-spec? (= (length varspec) 3))
	    (name (car varspec)))
	(if (not (symbol? name)) (raise 'invalid-let-var-name))
	(let* ((to-name (make-primitive-object tc-symbol name))
	       (sexpr-init (if has-type-spec? (caddr varspec) (cadr varspec)))
	       (tmp1 (begin (set-proc-expr compiler sexpr-init 'local name) 0))
	       (init-expr (translate-expr
			   compiler
			   env1
			   #f 
			   sexpr-init))
	       (tmp2 (begin (unset-proc-expr compiler sexpr-init) 0))
	       (type-actual (get-entity-type init-expr))
	       (type-declared
		(if has-type-spec?
		    (translate-expr compiler env1 #f
				    (cadr varspec))
		    '()))
	       (type-var (if (not-null? type-declared)
			     type-declared
			     type-actual)))
	  (if (and has-type-spec?
		   (not (is-type?
			 (compiler-get-binder compiler) type-declared)))
	      (raise (list 'invalid-let-var-type (cons 's-name name))))
	  ;; If we are inside a parametrized procedure
	  ;; the type of the initializer expression may not be final.
	  (let* ((binder (compiler-get-binder compiler))
		 (value (get-entity-value init-expr))
		 (address (compiler-alloc-loc compiler name #f))
		 (to-new
		  ;; Optimizing types caused problems with
		  ;; let expressions result types when a result type
		  ;; was a variable.
		  (if (and
		       (is-known-object? value)
		       (not (is-type? binder value)))
		      (make-object-with-address value address)
		      value))
		 (known-init? (is-known-object? init-expr))
		 (init-expr2
		  (if known-init?
		      to-new
		      init-expr))
		 (variable
		  (let ((type-dispatched?
			 (entity-type-dispatched? init-expr))
			(free-tvars?
			 (or (contains-free-tvars? type-actual)
			     (contains-free-tvars? type-declared))))
		    (cond
		     ((and (not type-dispatched?)
			   (not (hfield-ref compiler 'inside-param-def?)))
		      (raise 'let:illegal-nondispatched-expression))
		     ((entity-is-none1? binder type-actual)
		      (raise (list 'let:initializer-none
				   (cons 's-name name))))
		     ((and (not-null? type-declared)
			   (entity-is-none1? binder type-declared))
		      (raise 'let:type-none))
		     ((and (not type-dispatched?)
			   (hfield-ref compiler 'inside-param-def?))
		      (make-normal-variable3
		       address
		       type-var
		       #f
		       #f
		       #t
		       #f
		       to-new
		       '()))
		     ((or (not (is-known-object? type-actual))
			  (and (not-null? type-declared)
			       (not (is-known-object? type-declared))))
		      (raise 'let:invalid-type))
		     ((or
		       (and (hfield-ref compiler 'inside-param-def?)
			    (or (not type-dispatched?) free-tvars?))
		       (or (null? type-declared)
			   (is-t-subtype? binder type-actual type-declared)))
		      (make-normal-variable3
		       address
		       type-var
		       type-dispatched?
		       (is-final-class? binder type-var)
		       #t
		       #f
		       to-new
		       '()))
		     (else
		      (raise (list 'let:type-mismatch
				   (cons 's-name name)
				   (cons 'tt-declared type-declared)
				   (cons 'tt-actual type-actual))))))))
	    (list to-name variable
		  (if known-init? init-expr2 variable)
		  type-declared init-expr
		  (if known-init? gl-true gl-false)))))))


(define (parse-let-vars compiler env1 readonly? volatile? s-vars)
  (if readonly?
      (map-in-order (lambda (varspec)
		      (parse-let-var-constant compiler env1 varspec))
		    s-vars)
      (map-in-order (lambda (varspec)
		      (parse-let-var-nonconstant compiler env1 varspec
						 volatile?))
		    s-vars)))


(define (get-bindings vars)
  (map (lambda (var)
	 (assert (is-t-primitive-object? (car var)))
	 (cons (get-contents (car var)) (caddr var)))
       vars))


(define (translate-let-expression compiler env1 expr-tail constant-bindings?
				  volatile?)
  ;; TBD: Raise an exception if both constant-bindings? and volatile? are #t.
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? env1 <environment>))
  (assert (list? expr-tail))
  (assert (not (and constant-bindings? volatile?)))
  (check-env env1)
  (if (< (length expr-tail) 2)
      (raise 'too-short-let)
      (let* ((s-vars (car expr-tail))
	     (s-body (cdr expr-tail))
	     (varspecs (parse-let-vars compiler env1 constant-bindings?
				       volatile? s-vars))
	     (bindings (get-bindings varspecs))
	     (local-env (make-environment env1 bindings))
	     (init-exprs
	      (map pick5th varspecs)))
	(let* ((t-body
		(wrap-compound-expression compiler local-env s-body))
	       (subexpr-type (get-entity-type t-body))
	       (exact-type? (hfield-ref t-body 'exact-type?))
	       (subexpr-pure? (is-pure-entity? t-body))
	       (subexpr-value (get-entity-value t-body))
	       (init-exprs-pure?
		(and-map? is-pure-entity? init-exprs))
	       (let-expr-pure? (and subexpr-pure? init-exprs-pure?))
	       (type-dispatched? (entity-type-dispatched? t-body))
	       (always-returns?
		(and (entity-always-returns? t-body)
		     (and-map? entity-always-returns? init-exprs)))
	       (never-returns?
		(or (entity-never-returns? t-body)
		    (or-map? entity-never-returns? init-exprs))))
	  (if (and (not type-dispatched?)
		   (not (hfield-ref compiler 'inside-param-def?)))
	      (raise 'let:illegal-nondispatched-expr))
	  (make-hrecord <let-expression>
			subexpr-type           ; <expression>
			type-dispatched?
			exact-type?
			'()
			let-expr-pure?
			#f
			;; TBD: Should we set need-revision? to #f if
			;; type-dispatched? = #t ?
			(hfield-ref compiler 'inside-param-def?)
			subexpr-value
			always-returns?        ; <dynamic-expression>
			never-returns?
			constant-bindings?     ; <let-expression>
			#f
			#f
			varspecs
			t-body)))))


(define (translate-letrec-expression compiler env expr-tail
				     constant-bindings? volatile? order?)
  ;; TBD: Raise an exception if both constant-bindings? and volatile? are #t.
  (if (< (length expr-tail) 2)
      (raise 'too-short-letrec)
      (let* ((s-varspecs (car expr-tail))
	     (s-body (cdr expr-tail))
	     (letrec-vars-parse-result
	      (parse-letrec-vars compiler env s-varspecs constant-bindings?
				 volatile?))
	     (letrec-vars (car letrec-vars-parse-result))
	     (new-env (cadr letrec-vars-parse-result))
	     (t-body
	      (wrap-compound-expression
	       compiler new-env s-body))
	     (subexpr-type (get-entity-type t-body))
	     (exact-type? (hfield-ref t-body 'exact-type?))
	     (subexpr-pure? (is-pure-entity? t-body))
	     (subexpr-value (get-entity-value t-body))
	     (type-dispatched? (entity-type-dispatched? t-body))
	     (init-exprs (map pick5th letrec-vars))
	     (init-exprs-pure?
	      (and-map? is-pure-entity? init-exprs))
	     (letrec-pure? (and subexpr-pure? init-exprs-pure?))
	     (always-returns?
	      (and (entity-always-returns? t-body)
		   (and-map? entity-always-returns? init-exprs)))
	     (never-returns?
	      (or (entity-never-returns? t-body)
		  (or-map? entity-never-returns? init-exprs))))
	(if (and (not type-dispatched?)
		 (not (hfield-ref compiler 'inside-param-def?)))
	    (raise 'letrec:illegal-nondispatched-expr))
	(make-hrecord <let-expression>
		      subexpr-type           ; <expression>
		      type-dispatched?
		      exact-type?
		      '()
		      letrec-pure?
		      #f
		      (hfield-ref compiler 'inside-param-def?)
		      subexpr-value
		      always-returns?        ; <dynamic-expression>
		      never-returns?
		      constant-bindings?     ; <let-expression>
		      #t
		      order?
		      letrec-vars
		      t-body))))


(define (translate-genproc-appl compiler symtbl expr-head expr-tail)
  ;; It is essential to use map* here.
  ;; tarkista seuraava
  (let ((arglist (map* (lambda (expr)
			 (translate-expr compiler symtbl #f expr))
		       expr-tail))
	(inside-param-def? (hfield-ref compiler 'inside-param-def?))
	(binder (compiler-get-binder compiler)))
    ;; TBD: Should we check that expr-head is a target object?
    (translate-general-genproc-appl
     binder
     expr-head arglist #t)))


(define (translate-param-proc-appl compiler symtbl expr-head expr-tail)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? symtbl <environment>))
  (assert (is-entity? expr-head))
  (assert (list? expr-tail))
  (check-env symtbl)
  (let ((arglist (map* (lambda (expr)
			 (translate-expr compiler symtbl #f expr))
		       expr-tail)))
    (do-translate-param-proc-appl (compiler-get-binder compiler)
				  (hfield-ref compiler 'inside-param-def?)
				  expr-head arglist '())))


(define (translate-simple-proc-appl compiler symtbl expr-head expr-tail)
  (let ((arguments (map* (lambda (expr)
			   (translate-expr compiler symtbl #f expr))
			 expr-tail)))
    (let ((result '()))
      (set! result
	    ;; Procedures field-ref and field-set!
	    ;; are handled differently by the compiler
	    ;; and the linker. That's why we handle them here.
	    ;; TBD: use eq? instead of eqv?
	    (cond
	     ((eqv? expr-head tp-field-ref)
	      (translate-field-ref-appl compiler expr-head arguments))
	     ((eqv? expr-head tp-field-set)
	      (translate-field-set-appl compiler expr-head arguments))
	     (else '())))
      (if (null? result)
	  (let* ((inside-param-def? (hfield-ref compiler 'inside-param-def?))
		 (result2
		  (do-translate-simple-proc-appl
		   expr-head arguments #f
		   (compiler-get-binder compiler)
		   #t
		   inside-param-def?
		   inside-param-def?)))
	    result2)
	  (begin
	    result)))))


(define (translate-pair-class compiler symtbl expr-tail)
  (if (>= (length expr-tail) 1)
      (let ((args
	     (map*
	      (lambda (expr)
		(translate-expr compiler symtbl #f expr))
	      expr-tail))
	    (binder (compiler-get-binder compiler)))
	(translate-pair-class-expression binder args))
      (raise 'invalid-pair-class-expression)))


;; It is legal to give <none> as a member of a union type.
;; In that case the union type will be equal to <none>.
(define (translate-union compiler symtbl expr-tail)
  (if (null? expr-tail)
      tt-none
      (let ((subexprs (map*
		       (lambda (expr)
			 (translate-expr compiler symtbl #f expr))
		       expr-tail))
	    (binder (compiler-get-binder compiler)))
	(get-union-of-types binder subexprs))))


(define (translate-vector compiler symtbl expr-tail)
  (if (= (length expr-tail) 1)
      (let ((member-type (translate-expr compiler symtbl #f (car expr-tail)))
 	    (binder (compiler-get-binder compiler)))
	(translate-vector-expression binder (list member-type)))
      ;; TBD: remove "uniform"
      (raise 'invalid-uniform-vector-expression)))


(define (translate-mutable-vector compiler symtbl expr-tail)
  (if (= (length expr-tail) 1)
      (let ((member-type (translate-expr compiler symtbl #f (car expr-tail)))
 	    (binder (compiler-get-binder compiler)))
	(translate-mutable-vector-expression binder
					     (list member-type)))
      (raise 'invalid-mutable-vector-expression)))


(define (translate-value-vector compiler symtbl expr-tail)
  (if (= (length expr-tail) 1)
      (let ((member-type (translate-expr compiler symtbl #f (car expr-tail)))
 	    (binder (compiler-get-binder compiler)))
	(translate-value-vector-expression binder (list member-type)))
      (raise 'invalid-value-vector-expression)))


(define (translate-mutable-value-vector compiler symtbl expr-tail)
  (if (= (length expr-tail) 1)
      (let ((member-type (translate-expr compiler symtbl #f (car expr-tail)))
 	    (binder (compiler-get-binder compiler)))
	(translate-mutable-value-vector-expression binder (list member-type)))
      (raise 'invalid-mutable-value-vector-expression)))


(define (translate-param-class-instance compiler symtbl expr-head expr-tail)
  (assert (is-target-object? expr-head))
  (let* ((r-param-class expr-head)
	 (r-type-params
	  (map (lambda (subexpr) (translate-expr compiler symtbl #f subexpr))
	       expr-tail)))
    (strong-assert (and-map? is-target-object? r-type-params))
;;    (check-no-none-types r-type-params)
    (translate-param-class-instance-expr
     (compiler-get-binder compiler)
     r-param-class
     r-type-params
     (hfield-ref compiler 'inside-param-def?)
     #t)))


(define (translate-param-sgn-instance compiler symtbl expr-head expr-tail)
  (assert (is-target-object? expr-head))
  (let* ((r-param-sgn expr-head)
	 (r-type-params
	  (map (lambda (subexpr) (translate-expr compiler symtbl #f subexpr))
	       expr-tail)))
    (strong-assert (and-map? is-target-object? r-type-params))
;;    (check-no-none-types r-type-params)
    (translate-param-sgn-instance-expr
     (compiler-get-binder compiler)
     r-param-sgn
     r-type-params)))


(define (translate-param-logical-type-instance compiler symtbl
					       expr-head expr-tail)
  (assert (is-target-object? expr-head))
  (let* ((r-param-ltype expr-head)
	 (r-type-params
	  (map (lambda (subexpr) (translate-expr compiler symtbl #f subexpr))
	       expr-tail)))
    (strong-assert (and-map? is-target-object? r-type-params))
    (let ((result
	   (if (hfield-ref r-param-ltype 'incomplete?)
	       (make-apti r-param-ltype r-type-params)
	       (translate-param-ltype-instance-expr
		(compiler-get-binder compiler)
		r-param-ltype
		r-type-params))))
      result)))


(define (translate-uniform-list compiler symtbl toplevel? expr-tail)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? symtbl <environment>))
  (assert (list? expr-tail))
  ;;  (check-env symtbl)
  (if (not (= (length expr-tail) 1))
      (raise 'syntax-error-in-uniform-list-type)
      (let* ((s-arg (car expr-tail))
	     (r-arg
	      (translate-expr compiler symtbl #f s-arg))
	     (binder (compiler-get-binder compiler)))
	(translate-uniform-list-type-expression binder r-arg))))


(define (get-prim-proc-arg-names count)
  (let ((result '()))
    (do ((i 0 (+ i 1))) ((>= i count) result)
      (set! result (append result
			   (list
			    (string->symbol
			     (string-append
			      "_i_prim_arg_" (number->string i)))))))))


(define (translate-primitive-procedure compiler symtbl expr-tail check?)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? symtbl <environment>))
  (assert (list? expr-tail))
  (assert (boolean? check?))
  (check-env symtbl)
  (if (not (list? expr-tail))
      (raise 'invalid-procedure-definition)
      (let ((target-name (car expr-tail))
	    (header (cdr expr-tail)))
	(if (or (not (list? header)) (not (= (length header) 3)))
	    (raise 'invalid-procedure-header)
	    (let ((s-arg-desc-expr (list-ref header 0))
		  (s-result-expr (list-ref header 1))
		  (s-attr (list-ref header 2)))
	      (let* ((arg-descs
		      (map (lambda (expr)
			     (translate-expr compiler symtbl #f expr))
			   s-arg-desc-expr))
		     (result-type
		      ;; TBD: This check should probably be removed.
		      (if (not-null? s-result-expr)
			  (translate-expr compiler symtbl #f
					  s-result-expr)
			  tt-none))
		     ;; Should we use entity-is-none1? in the following?
		     (has-result? (not (target-type=?
					result-type tt-none)))
		     (attr (parse-proc-attributes s-attr))
		     (pure-proc? (hfield-ref attr 'pure?))
		     (appl-always-returns? (hfield-ref attr 'always-returns?))
		     (appl-never-returns? (hfield-ref attr 'never-returns?))
		     (static-method? (hfield-ref attr 'static-method?))
		     (binder (compiler-get-binder compiler))
		     ;; The class of a primitive procedure is
		     ;; a :simple-proc class.
		     (proc-type (translate-simple-proc-class-expression
				 binder
				 arg-descs
				 result-type
				 pure-proc?
				 appl-always-returns?
				 appl-never-returns?
				 static-method?)))
		(if (and has-result? check?)
		    (begin
		      (get-checked-prim-proc target-name proc-type))
		    (begin
		      (get-prim-proc-expression
		       target-name proc-type)))))))))


(define (translate-param-prim-proc compiler symtbl expr-tail check?)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? symtbl <environment>))
  (assert (list? expr-tail))
  (assert (boolean? check?))
  (check-env symtbl)
  (if (not (and (list? expr-tail) (= (length expr-tail) 5)))
      (raise 'invalid-procedure-definition)
      (let ((target-name (car expr-tail))
	    (s-tvars (cadr expr-tail))
	    (s-arg-types (list-ref expr-tail 2))
	    (s-result-type (list-ref expr-tail 3))
	    (s-attr (list-ref expr-tail 4)))
	(let* ((proc-type 
		(do-translate-param-proc-class2 compiler symtbl
						s-tvars
						s-arg-types
						s-result-type
						s-attr))
	       (result-type (tno-field-ref
			     (tno-field-ref proc-type 'type-contents)
			     'type-result))
	       ;; Should we use entity-is-none1? in the following?
	       (has-result? (not (target-type=? result-type tt-none))))
	  (if (and has-result? check?)
	      (begin
		(get-checked-prim-proc target-name proc-type))
	      (begin
		(get-prim-proc-expression
		 target-name proc-type)))))))


(define (translate-assertion compiler symtbl condition cond-to-print strong?)
  (assert (hrecord-is-instance? compiler <compiler>))
  (assert (hrecord-is-instance? symtbl <environment>))
  (assert (boolean? strong?))
  (let* ((r-condition (translate-expr compiler symtbl #f condition))
	 (condition-bool?
	  (target-type=? (get-entity-type r-condition)
			 tc-boolean)))
    (cond
     ((and (not condition-bool?)
	   (not (hfield-ref compiler 'inside-param-def?)))
      (raise 'assertion-condition-not-boolean))
     ((and (not (is-pure-entity? r-condition))
	   (not (hfield-ref compiler 'inside-param-def?)))
      (raise 'assertion-condition-not-pure))
     (else
      ;; An assertion is a pure expression even though it may not return.
      (make-hrecord <assertion-expr>
		    tt-none #t #t '()
		    #t #f #f '()
		    #f #f
		    r-condition cond-to-print strong?)))))


(define (parse-signature-member-sexpr compiler symtbl s-member)
  (if (not (and (list? s-member) (= (length s-member) 4)))
      (begin
	;; TBD: fix this error
	(display s-member)
	(raise 'signature-syntax-error))
      (let ((s-name (car s-member))
	    (s-arg-list (cadr s-member))
	    (s-result-type (caddr s-member))
	    (s-attr (list-ref s-member 3)))
	(if (not (symbol? s-name))
	    (raise 'invalid-signature-member-name)
	    (let ((var (get-symbol symtbl s-name)))
	      (if (eq? var #f)
		  (raise 'undefined-signature-member)
		  (let* ((binder (compiler-get-binder compiler))
			 (r-arg-list
			  (map* (lambda (arg-type)
				  (translate-expr compiler symtbl #f arg-type))
				s-arg-list))
			 (r-result-type
			  (translate-expr compiler symtbl #f s-result-type))
			 (attr (parse-proc-attributes s-attr))
			 (pure-proc? (hfield-ref attr 'pure?))
			 (appl-always-returns?
			  (hfield-ref attr 'always-returns?))
			 (appl-never-returns?
			  (hfield-ref attr 'never-returns?))
			 (static-method?
			  (hfield-ref attr 'static-method?))
			 (type (translate-proc-type-expression
				binder
				r-arg-list
				r-result-type
				pure-proc?
				appl-always-returns?
				appl-never-returns?
				static-method?)))
		    (cons var type))))))))