aboutsummaryrefslogtreecommitdiff
path: root/semantics.c
blob: 68884ce90a6e7945dd793cf39b7e47586bb20097 (plain) (blame)
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
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "semantics.h"
#include "ast.h"
#define NEW(type) ((type *)malloc(sizeof(type)))
#define CHECK_TYPE(p, _type) assert(p->type == _type)
#define ERROR(ast) print_error(err_buff, NULL, (ast)->loc.row, (ast)->loc.col, 0)
#define WARNING(ast) print_error(err_buff, NULL, (ast)->loc.row, (ast)->loc.col, 1)

extern void print_error(char *, char *, int, int, int);
extern char *load_line(int);
static char err_buff[MAX_ERROR_BUFF];
static CType_t basic_type_int; 
static CType_t basic_type_char;
static CType_t basic_type_void;

#ifdef CIBIC_DEBUG
CTable_t ctable_create(Hashfunc_t hfunc, Printfunc_t pfunc) {
    CTable_t ct = NEW(CTable);
    memset(ct->head, 0, sizeof(CTNode*) * MAX_TABLE_SIZE);
    ct->hfunc = hfunc;
    ct->pfunc = pfunc;
    return ct;
}
#else
CTable_t ctable_create(Hashfunc_t hfunc) {
    CTable_t ct = NEW(CTable);
    memset(ct->head, 0, sizeof(CTNode*) * MAX_TABLE_SIZE);
    ct->hfunc = hfunc;
    return ct;
}
#endif
void ctable_destory(CTable_t ct) {
    int i;
    for (i = 0; i < MAX_TABLE_SIZE; i++)
    {
        CTNode *p, *np;
        for (p = ct->head[i]; p; p = np)
        {
            np = p->next;
            free(p);
        }
    }
}

void *ctable_lookup(CTable_t ct, const char *key) {
    unsigned int hv = ct->hfunc(key) % MAX_TABLE_SIZE;
    CTNode *p = ct->head[hv];
    for (; p; p = p->next)
        if (!strcmp(p->key, key))
            return p->val;
    return NULL; /* not found */
}

int ctable_insert(CTable_t ct, const char *key, void *val, int lvl) {
    unsigned int hv = ct->hfunc(key) % MAX_TABLE_SIZE;
    CTNode *p = ct->head[hv];
    CTNode *np;
    for (; p && p->lvl == lvl; p = p->next)
        if (!strcmp(p->key, key))
            return 0; /* conflict */
    np = NEW(CTNode);
    np->key = key;
    np->val = val;
    np->lvl = lvl;
    np->next = ct->head[hv];
    ct->head[hv] = np;
    return 1;
}

void ctable_clip(CTable_t ct, const char *key, int max_lvl) {
    unsigned int hv = ct->hfunc(key) % MAX_TABLE_SIZE;
    CTNode *p = ct->head[hv], *np;
    for (; p && p->lvl > max_lvl; p = np)
    {
        np = p->next;
        free(p);
    }
    ct->head[hv] = p;
}

CScope_t cscope_create() {
    CScope_t p = NEW(CScope);
    p->lvl = -1;
    p->top = NULL;
    p->func = NULL;
    p->inside_loop = 0;
#ifdef CIBIC_DEBUG
    p->tvar = ctable_create(bkdr_hash, ctable_cvar_print);
    p->ttype = ctable_create(bkdr_hash, ctable_ctype_print);
#else
    p->tvar = ctable_create(bkdr_hash);
    p->ttype = ctable_create(bkdr_hash);
#endif
    cscope_enter(p);
    return p;
}

int cscope_push_var(CScope_t cs, CVar_t var) {
#ifdef CIBIC_DEBUG
    assert(cs->top);
#endif
    if (ctable_insert(cs->tvar, var->name, var, cs->lvl))
    {
        CSVar *csvar = NEW(CSVar);
        csvar->var = var;
        csvar->next = cs->top->vhead;
        cs->top->vhead = csvar;
        return 1;
    }
    else return 0; /* naming conflict */
}

int cscope_push_type(CScope_t cs, CType_t type) {
#ifdef CIBIC_DEBUG
    assert(cs->top);
#endif
    if (ctable_insert(cs->ttype, type->name, type, cs->lvl))
    {
        CSType *cstype = NEW(CSType);
        cstype->type = type;
        cstype->next = cs->top->thead;
        cs->top->thead = cstype;
        return 1;
    }
    else return 0; /* naming conflict */
}

void cscope_enter(CScope_t cs) {
    CSNode *np = NEW(CSNode);
    np->next = cs->top;
    np->vhead = NULL;
    np->thead = NULL;
    cs->top = np;
    cs->lvl++;
}

void cscope_exit(CScope_t cs) {
    CSNode *top_o = cs->top;
    CSVar *vp;
    CSType *tp;
    cs->lvl--;
    cs->top = top_o->next;
    for (vp = top_o->vhead; vp; vp = vp->next)
        ctable_clip(cs->tvar, vp->var->name, cs->lvl);
    for (tp = top_o->thead; tp; tp = tp->next)
        ctable_clip(cs->ttype, tp->type->name, cs->lvl);
    free(top_o);
}

void ctable_debug_print(CTable_t ct) {
    int i;
    fprintf(stderr, "*** CTable ***\n");
    for (i = 0; i < MAX_TABLE_SIZE; i++)
        if (ct->head[i])
        {
            CTNode *p;
            fprintf(stderr, "[%04d]", i);
            for (p = ct->head[i]; p; p = p->next)
                fprintf(stderr, "->[%s:%d]", ct->pfunc(p->val), p->lvl);
            fprintf(stderr, "\n");
        }
    fprintf(stderr, "*** CTable ***\n");
}

void cscope_debug_print(CScope_t cs) {
    int lvl = cs->lvl;
    CSNode *p;
    fprintf(stderr, "\n****** CScope ******\n");
    for (p = cs->top; p; p = p->next)
    {
        CSVar *vp;
        CSType *tp;
        fprintf(stderr, "Level %d:\n", lvl--);
        fprintf(stderr, "Vars: ");
        for (vp = p->vhead; vp; vp = vp->next)
            fprintf(stderr, "%s ", vp->var->name);
        fprintf(stderr, "\nTypes: ");
        for (tp = p->thead; tp; tp = tp->next)
            fprintf(stderr, "%s ", tp->type->name);
        fprintf(stderr, "\n\n");
    }
    fprintf(stderr, "Var Table:\n");
    ctable_debug_print(cs->tvar);
    fprintf(stderr, "Type Table:\n");
    ctable_debug_print(cs->ttype);
    fprintf(stderr, "****** CScope ******\n\n");
}

CVar_t cscope_lookup_var(CScope_t cs, const char *name) {
    return ctable_lookup(cs->tvar, name);
}

CType_t cscope_lookup_type(CScope_t cs, const char *name) {
    return ctable_lookup(cs->ttype, name);
}

unsigned int bkdr_hash(const char *str) {
    unsigned int seed = 131;
    unsigned int hv = 0;
    while (*str)
        hv = hv * seed + (unsigned)(*str++);
    return hv;
}

const char *ctable_cvar_print(void *var) {
    static char buff[MAX_DEBUG_PRINT_BUFF];
    sprintf(buff, "%s@%lx", ((CVar_t )var)->name, (size_t)var);
    return buff;
}

const char *ctable_ctype_print(void *type) {
    static char buff[MAX_DEBUG_PRINT_BUFF];
    sprintf(buff, "%s@%lx", ((CType_t )type)->name, (size_t)type);
    return buff;
}

CVar_t cvar_create(const char *name, CType_t type, CNode *ast) {
    CVar_t cv = NEW(CVar);
    cv->name = name;
    cv->type = type;
    cv->ast = ast;
    return cv;
}

CType_t ctype_create(const char *name, int type, CNode *ast) {
    CType_t ct = NEW(CType);
    ct->name = name;
    ct->type = type;
    ct->ast = ast;
    switch (type)
    {
        case CINT: ct->size = INT_SIZE; break;
        case CCHAR: ct->size = CHAR_SIZE; break;
        case CVOID: ct->size = 0; break;
    }
    return ct;
}

void ctype_print(CType_t);

void cvar_print(CVar_t cv) {
    fprintf(stderr, "[var@%lx:%s]->", (size_t)cv, cv->name);
    ctype_print(cv->type);
}

void ctype_print(CType_t ct) {
    switch (ct->type)
    {
        case CINT: 
            fprintf(stderr, "[int]"); break;
        case CCHAR:
            fprintf(stderr, "[char]"); break;
        case CVOID:
            fprintf(stderr, "[void]"); break;
        case CSTRUCT:
        case CUNION:
            {
                CTable_t f = ct->rec.fields;
                int i;
                CTNode *fn; 
                fprintf(stderr, "[%s@%lx:(name:%s|fields:",
                        ct->type == CSTRUCT ? "struct" : "union",
                        (size_t)ct,
                        ct->name);
                if (f)
                {
                    int first = 1;
                    for (i = 0; i < MAX_TABLE_SIZE; i++)
                        for (fn = f->head[i]; fn; fn = fn->next)
                        {
                            fprintf(stderr, "%s", first ? (first = 0, "") : ",");
                            cvar_print((CVar_t)fn->val);
                        }
                }
                fprintf(stderr, ")]");
            }
            break;
        case CARR:
            {
                fprintf(stderr, "[arr:(%d)]->", ct->rec.arr.len);
                ctype_print(ct->rec.arr.elem);
            }
            break;
        case CPTR:
            {
                fprintf(stderr, "[ptr]->");
                ctype_print(ct->rec.ref);
            }
            break;
        case CFUNC:
            {
                CVar_t p;
                fprintf(stderr, "[func:(name:%s|params:", ct->name);
                for (p = ct->rec.func.params; p; p = p->next)
                {
                    cvar_print(p);
                    if (p->next) fprintf(stderr, ",");
                }
                fprintf(stderr, "|local:");
                for (p = ct->rec.func.local; p; p = p->next)
                {
                    cvar_print(p);
                    if (p->next) fprintf(stderr, ",");
                }
                fprintf(stderr, ")]->");
                ctype_print(ct->rec.func.ret);
            }
            break;
    }
}

static CType_t struct_type_merge(CType_t new, CScope_t scope) {
    /* Note: we shall try to lookup first instead of pushing !! */
    CType_t old = cscope_lookup_type(scope, new->name);
    if (!old) /* create it if it does not exist */
    {
        cscope_push_type(scope, new);
        return new;
    } /* otherwise we have it */
    if (old->type != new->type) /* not a struct or union */
    {
        sprintf(err_buff, "conflicting types of '%s'", new->name);
        ERROR(new->ast);
    }
    /* otherwise it is a struct or union */
    if (!new->rec.fields) /* use the old definition */
        return old;
    /* otherwise there's a completion definition */
    if (cscope_push_type(scope, new)) /* try to push the defintion */
        return new;
    /* conflict appears */
    if (old->rec.fields) /* if the old one is complete */
    {
        sprintf(err_buff, "redefinition of '%s'", new->name);
        ERROR(new->ast);
    }
    /* otherwise incomplete, thus complete the type */
    old->next = new->next;
    old->rec.fields = new->rec.fields;
    old->ast = new->ast;
    free(new);
    return old;
}

int is_same_type(CType_t typea, CType_t typeb) {
    if (typea == typeb) return 1;
    if (typea->type != typeb->type) return 0;
    switch (typea->type)
    {
        case CSTRUCT: case CUNION:
            return typea == typeb;
        case CARR:
            if (typea->rec.arr.len != typeb->rec.arr.len)
                return 0;
            return is_same_type(typea->rec.arr.elem, typeb->rec.arr.elem);
        case CPTR:
            return is_same_type(typea->rec.ref, typeb->rec.ref);
        case CFUNC:
            {
                CVar_t pa, pb;
                for (pa = typea->rec.func.params, 
                        pb = typeb->rec.func.params; pa && pb;
                        pa = pa->next, pb = pb->next)
                    if (!is_same_type(pa->type, pb->type))
                        return 0;
                if (pa || pb) 
                    return 0; /* different number of parameters */
                return is_same_type(typea->rec.func.ret, typeb->rec.func.ret);
            }
        case CINT: case CCHAR: case CVOID:
            ;
            break;
    }
    return 1;
}

static CVar_t var_merge(CVar_t new, CScope_t scope) {
    CVar_t old;
    if (cscope_push_var(scope, new))
        return new;
    else
        old = cscope_lookup_var(scope, new->name);
    if (!is_same_type(old->type, new->type) || scope->lvl > 0)
    {
        sprintf(err_buff, "conflicting types of '%s'", new->name);
        ERROR(new->ast);
    }
    free(new);
    return old;
}

CTable_t semantics_fields(CNode *, CScope_t scope);
CType_t semantics_type_spec(CNode *p, CScope_t scope) {
    CHECK_TYPE(p, TYPE_SPEC);
    CType_t type;
    switch (p->rec.subtype)
    {
        case KW_VOID:
            type = ctype_create("", CVOID, p); break;
        case KW_CHAR:
            type = ctype_create("", CCHAR, p); break;
        case KW_INT:
            type = ctype_create("", CINT, p); break;
        case KW_STRUCT: case KW_UNION:
            {
                CNode *id = p->chd,
                      *fields = p->chd->next;
                type = ctype_create(id->type == NOP ? "" : id->rec.strval,
                        p->rec.subtype == KW_STRUCT ? CSTRUCT : CUNION,
                        p);
                if (fields->type == NOP)
                    type->rec.fields = NULL; /* incomplete type */
                else
                    type->rec.fields = semantics_fields(fields, scope);

                if (id->type != NOP)
                    type = struct_type_merge(type, scope);
            }
            break;
        default: assert(0);
    }
    return type;
}

int type_is_complete(CType_t type) {
    switch(type->type)
    {
        case CINT: case CCHAR: case CPTR: case CARR:
            return 1;
        case CSTRUCT: case CUNION:
            return type->rec.fields != NULL;
        default: ; /* TODO: CFUNC CVOID */
    }
    return 1;
}

CVar_t semantics_declr(CNode *, CType_t, CScope_t, int);
CVar_t semantics_p_decl(CNode *p, CScope_t scope) {
    CHECK_TYPE(p, PLAIN_DECL);
    CVar_t var = semantics_declr(p->chd->next,
                                semantics_type_spec(p->chd, scope),
                                scope, 0);
    if (!type_is_complete(var->type))
    {
        sprintf(err_buff, "parameter '%s' has incomplete type", var->name);
        ERROR(var->ast);
    }
    return var;
}


CVar_t semantics_params(CNode *p, CScope_t scope) {
    CHECK_TYPE(p, PARAMS);
    p = p->chd;
    if (!p) return NULL; /* no parameters */
    CVar_t params = semantics_p_decl(p, scope), tail = params;
#ifdef CIBIC_DEBUG
    CTable_t tparams = ctable_create(bkdr_hash, ctable_cvar_print);
#else
    CTable_t tparams = ctable_create(bkdr_hash);
#endif
    ctable_insert(tparams, params->name, params, 0);
    for (p = p->next; p; p = p->next)
    {
        CVar_t var = semantics_p_decl(p, scope);
        if (scope)  /* params inside a function definition */
            if (!ctable_insert(tparams, var->name, var, 0))
            {
                sprintf(err_buff, "redefinition of parameter '%s'", var->name);
                ERROR(var->ast);
            }
        tail->next = var;
        tail = var;
    }
    ctable_destory(tparams);
    return params;
}

#define CHECK_CVOID(name, ast) \
    if (type_spec->type == CVOID) \
do { \
    sprintf(err_buff, "variable or field '%s' declared void", name); \
    ERROR(ast); \
} while (0)

CVar_t semantics_declr(CNode *p, CType_t type_spec, CScope_t scope, int func_chk) {
    CVar_t type;
    if (p->type == ID) 
    {
        if (!func_chk) CHECK_CVOID(p->rec.strval, p);
        return cvar_create(p->rec.strval, type_spec, p);
    }

    switch (p->rec.subtype)
    {
        case DECLR_FUNC: 
            {
                CType_t func = ctype_create("", CFUNC, p); /* function declr */
                cscope_enter(scope);
                func->rec.func.params = semantics_params(p->chd->next, scope);
                cscope_exit(scope);
                /* incomplete type */
                func->rec.func.local = NULL;
                func->rec.func.ret = type_spec;
                func->rec.func.body = NULL;         /* not a definition */
                type = semantics_declr(p->chd, func, scope, 1);
                if (type_spec->type == CARR)
                {
                    sprintf(err_buff, "'%s' declared as function returning an array",
                            type->name);
                    ERROR(p);
                }
                if (type_spec->type == CFUNC)
                {
                    sprintf(err_buff, "'%s' declared as function returing a function",
                            type->name);
                    ERROR(p);
                }
            }
            break;
        case DECLR_ARR:
            {
                CType_t arr = ctype_create("", CARR, p);    /* array declr */
                arr->rec.arr.elem = type_spec;
                arr->rec.arr.len = p->chd->next->rec.intval;
                type = semantics_declr(p->chd, arr, scope, 0);
            }
            break;
        case '*':
            {
                CType_t ptr = ctype_create("", CPTR, p);    /* pointer */
                ptr->rec.ref = type_spec;
                type = semantics_declr(p->chd, ptr, scope, 0);
            }
            break;
        default: assert(0);
    }
    return type;
}

CTable_t semantics_fields(CNode *p, CScope_t scope) {
#ifdef CIBIC_DEBUG
    CTable_t ct = ctable_create(bkdr_hash, ctable_cvar_print);
#else
    CTable_t ct = ctable_create(bkdr_hash);
#endif
    for (p = p->chd; p; p = p->next)
    {
        CNode *declr = p->chd->next->chd;
        for (; declr; declr = declr->next)
        {
            CVar_t var = semantics_declr(declr, 
                                        semantics_type_spec(p->chd, scope),
                                        scope, 0);
            if (var->type->type == CFUNC)
            {
                sprintf(err_buff, "field '%s' declared as a function", var->name);
                ERROR(var->ast);
            }
            /* incomplete type checking */
            if (!type_is_complete(var->type))
            {
                sprintf(err_buff, "field '%s' has incomplete type", var->name);
                ERROR(var->ast);
            }
            if (!ctable_insert(ct, var->name, var, 0))
            {
                sprintf(err_buff, "duplicate member '%s'", var->name);
                ERROR(var->ast);
            }
        }
    }
    return ct;
}

CVar_t semantics_decl(CNode *p, CScope_t scope) {
    CHECK_TYPE(p, DECL);
    CNode *init = p->chd->next;
    CType_t type = semantics_type_spec(p->chd, scope);
    CVar_t res = NULL;
    int useful = 0;
    if ((type->type == CSTRUCT || type->type == CUNION) && 
            (*type->name) != '\0')
    {
        cscope_push_type(scope, type);
        useful = 1;
    }
    if (init->chd->type != NOP)
    {
        CNode *p;
        for (p = init->chd; p; p = p->next)
        {
            /* TODO: initializer checking */
            CVar_t var = semantics_declr(p->chd, type, scope, 0);
            if (scope->lvl && !type_is_complete(var->type))
            {
                sprintf(err_buff, "storage size of '%s' isn’t known", var->name);
                ERROR(var->ast);
            }
            var = var_merge(var, scope);
            var->next = res;
            res = var;
        }
        useful = 1;
    }
    if (!useful)
    {
        /* useless typename warning */
        sprintf(err_buff, "useless declaration");
        WARNING(type->ast);
    }
    return res;
}

#define NOT_IGNORE_VOID(et, ast) \
    if (et->type == CVOID) \
do \
{ \
    sprintf(err_buff, "void value not ignored as it ought to be"); \
    ERROR(ast); \
} while (0)

#define INCOMP_TYPE(ast) \
    do { \
        sprintf(err_buff, "incompatible types when assigning"); \
        ERROR(ast); \
    } while (0)

void exp_check_aseq_(CType_t lhs, CType_t rhs, CNode *ast) {
    NOT_IGNORE_VOID(lhs, ast);
    NOT_IGNORE_VOID(rhs, ast);
    switch (lhs->type)
    {
        case CSTRUCT: case CUNION:
            if (!is_same_type(lhs, rhs))
                INCOMP_TYPE(ast);
            break;
        case CARR: case CFUNC: /* constant */
            INCOMP_TYPE(ast);
            break;
        case CINT: case CCHAR:
            switch (rhs->type)
            {
                case CINT: case CCHAR:
                    ; break; /* ok */
                case CPTR: case CARR:
                    sprintf(err_buff, "assignment makes integer from pointer without a cast");
                    WARNING(ast);
                    break;
                default: INCOMP_TYPE(ast);
            }
            break;
        case CPTR:
            switch (rhs->type)
            {
                case CPTR: case CARR:
                    if (!is_same_type(lhs->rec.ref, rhs->rec.ref))
                    {
                        sprintf(err_buff, "assignment from incompatible pointer type");
                        WARNING(ast);
                    }
                    break;
                case CINT: case CCHAR:
                    sprintf(err_buff, "assignment makes pointer from integer without a cast");
                    WARNING(ast);
                    break;
                default: INCOMP_TYPE(ast);
            }
            break;
        default: assert(0);
    }
}

ExpType exp_check_aseq(ExpType lhs, ExpType rhs, CNode *ast) {
    exp_check_aseq_(lhs.type, rhs.type, ast);
    return lhs;
}

#define IS_INT(tt) ((tt) == CINT || (tt) == CCHAR)
#define IS_ARITH(tt) IS_INT(tt)
#define IS_SCALAR(tt) (IS_ARITH(tt) || (tt) == CPTR || (tt) == CARR)

CType_t semantics_cast(CNode *p, CScope_t scope) {
    CNode *t, *ast;
    CType_t tt, type;
    if (p->type == TYPE_SPEC) 
    {
        type = semantics_type_spec(p, scope);
        ast = p;
    }
    else
    {
        type = ctype_create("", CPTR, p); /* pointer */
        for (t = p, tt = type;; t = t->chd)
        {
            if (t->chd->type == TYPE_SPEC)
            {
                tt->rec.ref = semantics_type_spec(t->chd, scope);
                ast = t;
                break;
            }
            tt->rec.ref = ctype_create("", CPTR, t);
            tt = tt->rec.ref;
        }
    }
    if (!IS_SCALAR(type->type))
    {
        sprintf(err_buff, "conversion to non-scalar type requested");
        ERROR(ast);
    }
    type->ast = ast;
    return type;
}


ExpType exp_check_arith(ExpType op1, ExpType op2, CNode *ast) {
    int t1 = op1.type->type,
        t2 = op2.type->type;
    ExpType res;
    NOT_IGNORE_VOID(op1.type, ast);
    NOT_IGNORE_VOID(op2.type, ast);
    res.lval = 0;
    res.type = basic_type_int;
    if (!(IS_ARITH(t1) && IS_ARITH(t2)))
    {
        sprintf(err_buff, "invalid operands to binary operator");
        ERROR(ast);
    }
    return res;
}

ExpType exp_check_bitwise(ExpType op1, ExpType op2, CNode *ast) {
    int t1 = op1.type->type,
        t2 = op2.type->type;
    ExpType res;
    NOT_IGNORE_VOID(op1.type, ast);
    NOT_IGNORE_VOID(op2.type, ast);
    res.lval = 0;
    res.type = basic_type_int;
    if (!(IS_INT(t1) && IS_INT(t2)))
    {
        sprintf(err_buff, "invalid operands to binary operator");
        ERROR(ast);
    }
    return res;
}

ExpType exp_check_add(ExpType op1, ExpType op2, CNode *ast, int sub) {
    int t1 = op1.type->type,
        t2 = op2.type->type;
    NOT_IGNORE_VOID(op1.type, ast);
    NOT_IGNORE_VOID(op2.type, ast);
    if (!sub && t2 == CPTR) 
    {
        /* place the pointer type in the first place */
        int t = t1;
        ExpType te = op1;

        t1 = t2;
        t2 = t;

        op1 = op2;
        op2 = te;

        CNode *n1 = ast->chd;
        CNode *n2 = n1->next;
        n2->next = n1;
        n1->next = NULL;
        ast->chd = n2;
    }
    if (!((t1 == CINT || t1 == CCHAR || t1 == CPTR) &&
                (t2 == CINT || t2 == CCHAR)))
    {
        sprintf(err_buff, "invalid operands to binary operator");
        ERROR(ast);
    }
    return op1; /* int or pointer */
}

ExpType exp_check_int(ExpType op1, CNode *ast) {
    if (!IS_INT(op1.type->type))
    {
        sprintf(err_buff, "wrong type argument to unary operator");
        ERROR(ast);
    }
    op1.lval = 0;
    return op1;
}

ExpType exp_check_scalar(ExpType op1, CNode *ast) {
    if (!IS_SCALAR(op1.type->type))
    {
        sprintf(err_buff, "wrong type argument to unary operator");
        ERROR(ast);
    }
    op1.lval = 0;
    return op1;
}

ExpType exp_check_deref(ExpType op1, CNode *ast) {
    if (op1.type->type != CPTR)
    {
        sprintf(err_buff, "invalid type argument of unary '*'");
        ERROR(ast);
    }
    if (op1.type->rec.ref->type == CFUNC)
        return op1;
    op1.lval = 1;   /* deref changes exp to lval */
    if (!type_is_complete(op1.type = op1.type->rec.ref))
    {
        sprintf(err_buff, "dereferencing pointer to incomplete type");
        ERROR(ast);
    }
    return op1;
}

ExpType exp_check_ref(ExpType op1, CNode *ast) {
    ExpType res;
    CType_t t = op1.type;
    if (t->type == CARR || (t->type == CPTR && t->rec.ref->type == CFUNC))
        return op1;
    if (!op1.lval)
    {
        sprintf(err_buff, "lvalue required as unary '&' operand");
        ERROR(ast);
    }
    res.lval = 0;
    res.type = ctype_create("", CPTR, ast);
    res.type->rec.ref = op1.type;
    return res;
}

ExpType exp_check_sizeof(ExpType op1) {
    op1.lval = 0;
    op1.type = basic_type_int;
    return op1;
}

ExpType exp_check_inc(ExpType op1, CNode *ast) {
    if (!IS_SCALAR(op1.type->type))
    {
        sprintf(err_buff, "wrong type argument to increment/decrement");
        ERROR(ast);
    }
    if (!op1.lval)
    {
        sprintf(err_buff, "lvalue required as increment/decrement operand");
        ERROR(ast);
    }
    return op1;
}

ExpType semantics_exp(CNode *, CScope_t);

ExpType exp_check_logical(ExpType op1, ExpType op2, CNode *ast) {
    int t1 = op1.type->type,
        t2 = op2.type->type;
    ExpType res;
    NOT_IGNORE_VOID(op1.type, ast);
    NOT_IGNORE_VOID(op2.type, ast);
    res.lval = 0;
    res.type = basic_type_int;
    if (!(IS_SCALAR(t1) && IS_SCALAR(t2)))
    {
        sprintf(err_buff, "invalid operands to binary operator");
        ERROR(ast);
    }
    return res;
}

ExpType exp_check_ass(ExpType lhs, ExpType rhs, CNode *p) {
    NOT_IGNORE_VOID(lhs.type, p);
    NOT_IGNORE_VOID(rhs.type, p);
    if (!lhs.lval)
    {
        sprintf(err_buff, "lvalue required as left operand of assignment");
        ERROR(p);
    }
    switch (p->rec.subtype)
    {
        case '=' : return exp_check_aseq(lhs, rhs, p);
        case ASS_MUL: return exp_check_aseq(lhs, exp_check_arith(lhs, rhs, p), p);
        case ASS_DIV: return exp_check_aseq(lhs, exp_check_arith(lhs, rhs, p), p); 
        case ASS_MOD: return exp_check_aseq(lhs, exp_check_arith(lhs, rhs, p), p); 

        case ASS_ADD: return exp_check_aseq(lhs, exp_check_add(lhs, rhs, p, 0), p);
        case ASS_SUB: return exp_check_aseq(lhs, exp_check_add(lhs, rhs, p, 1), p);

        case ASS_SHL: return exp_check_aseq(lhs, exp_check_bitwise(lhs, rhs, p), p);
        case ASS_SHR: return exp_check_aseq(lhs, exp_check_bitwise(lhs, rhs, p), p);
        case ASS_AND: return exp_check_aseq(lhs, exp_check_bitwise(lhs, rhs, p), p);
        case ASS_XOR: return exp_check_aseq(lhs, exp_check_bitwise(lhs, rhs, p), p);
        case ASS_OR: return exp_check_aseq(lhs, exp_check_bitwise(lhs, rhs, p), p);
        default: assert(0);
    }
}

ExpType exp_check_equality(ExpType op1, ExpType op2, CNode *ast) {
    int t1 = op1.type->type,
        t2 = op2.type->type;
    ExpType res;
    NOT_IGNORE_VOID(op1.type, ast);
    NOT_IGNORE_VOID(op2.type, ast);
    res.lval = 0;
    res.type = basic_type_int;
    if (IS_ARITH(t1) && IS_ARITH(t2))
        return res;
    if (!(IS_SCALAR(t1) && IS_SCALAR(t2)))
    {
        sprintf(err_buff, "invalid operands to binary operator");
        ERROR(ast);
    }
    if (t1 == CPTR && t2 == CPTR)
    {
        if (!is_same_type(op1.type->rec.ref, op2.type->rec.ref))
        {
            sprintf(err_buff, "comparison of distinct pointer types lacks a cast");
            WARNING(ast);
        }
    }
    else if (t1 == CPTR || t2 == CPTR)
    {
        sprintf(err_buff, "comparison between pointer and integer");
        WARNING(ast);
    }
    return res;
}

ExpType exp_check_postfix(CNode *p, CScope_t scope) {
    CNode *post = p->chd->next;
    ExpType op1 = semantics_exp(p->chd, scope), op2;
    int t1 = op1.type->type, t2;
    switch (post->rec.subtype)
    {
        case POSTFIX_ARR:
            if (!(t1 == CARR || t1 == CPTR))
            {
                sprintf(err_buff, "subscripted value is neither array nor pointer");
                ERROR(p);
            }
            op2 = semantics_exp(post->chd, scope);
            t2 = op2.type->type;
            if (!IS_INT(t2))
            {
                sprintf(err_buff, "array subscript is not an integer");
                ERROR(p);
            }
            op1.type = op1.type->rec.arr.elem;
            op1.lval = 1;
            break;
        case POSTFIX_CALL:
            if (!((t1 == CFUNC) ||
                (t1 == CPTR && op1.type->rec.ref->type == CFUNC)))
            {
                sprintf(err_buff, "called object is not a function");
                ERROR(p);
            }
            {
                CNode *arg = post->chd->chd;
                CType_t func = p->chd->ext.type;
                CVar_t param;
                /* pointer to function */
                if (func->type == CPTR) func = func->rec.ref;
                param = func->rec.func.params;
                for (; arg && param;
                        arg = arg->next, param = param->next) 
                {
                    semantics_exp(arg, scope);
                    exp_check_aseq_(param->type, arg->ext.type, arg);
                }
                if (arg || param)
                {
                    sprintf(err_buff, "too many/few arguments to the function");
                    ERROR(p);
                }
                op1.type = func->rec.func.ret;
                op1.lval = 0;
                break;
            }
        case POSTFIX_DOT:
            if (!(t1 == CSTRUCT || t1 == CUNION))
            {
                sprintf(err_buff, "request for the member in something not a structure or union");
                ERROR(p);
            }
            {
                CVar_t fv = ctable_lookup(op1.type->rec.fields, post->chd->rec.strval);
                if (!fv)
                {
                    sprintf(err_buff, "struct/union has no member named '%s'", post->chd->rec.strval);
                    ERROR(p);
                }
                p->ext.var = fv;
                op1.type = fv->type;
                op1.lval = 1;
            }
            break;
        case POSTFIX_PTR:
            if (t1 != CPTR)
            {
                sprintf(err_buff, "invalid type argument of '->'");
                ERROR(p);
            }
            {
                CType_t tref = op1.type->rec.ref;
                if (!(tref->type == CSTRUCT || tref->type == CUNION))
                {
                    sprintf(err_buff, "request for the member in something not a structure or union");
                    ERROR(p);
                }
                if (!tref->rec.fields)
                {
                    sprintf(err_buff, "dereferencing pointer to incomplete type");
                    ERROR(p);
                }
                CVar_t fv = ctable_lookup(tref->rec.fields, post->chd->rec.strval);
                if (!fv)
                {
                    sprintf(err_buff, "struct/union has no member named '%s'", post->chd->rec.strval);
                    ERROR(p);
                }
                p->ext.var = fv;
                op1.type = fv->type;
                op1.lval = 1;
            }
            break;
        case OPT_INC: case OPT_DEC:
            exp_check_inc(op1, p);
            break;
        default: assert(0);
    }
    return op1;
}

ExpType semantics_exp(CNode *p, CScope_t scope) {
    ExpType res;
    switch (p->type)
    {
        case ID:
            if (!(p->ext.var = cscope_lookup_var(scope, p->rec.strval)))
            {
                sprintf(err_buff, "'%s' undeclared", p->rec.strval);
                ERROR(p);
            }
            res.type = p->ext.var->type;
            res.lval = !(res.type->type == CARR || res.type->type == CFUNC);
            if (res.type->type == CFUNC)
            {
                CType_t f = ctype_create("", CPTR, p);
                f->rec.ref = res.type;
                res.type = f;
            }
            break;
        case INT:
            res.type = basic_type_int;
            res.lval = 0;
            break;
        case CHAR:
            res.type = basic_type_char;
            res.lval = 0;
            break;
        case STR:
            {
                CType_t type = ctype_create("", CPTR, NULL);
                type->rec.ref = basic_type_char;
                res.type = type;
            }
            break;
        case EXP:
            {
                ExpType op1;
                ExpType op2;
                if (!(p->rec.subtype == EXP_CAST || p->rec.subtype == EXP_POSTFIX))
                {
                    op1 = semantics_exp(p->chd, scope);
                    if (p->chd->next)
                        op2 = semantics_exp(p->chd->next, scope);
                }
                switch (p->rec.subtype)
                {
                    /* following cases are binary expressions */
                    case ',': 
                        res = op2;
                        res.lval = 0;
                        break;
                    case '=' : 
                    case ASS_MUL:
                    case ASS_DIV:
                    case ASS_MOD:
                    case ASS_ADD:
                    case ASS_SUB:
                    case ASS_SHL:
                    case ASS_SHR:
                    case ASS_AND:
                    case ASS_XOR:
                    case ASS_OR:
                        res = exp_check_ass(op1, op2, p);
                        break;
                    case OPT_OR:
                    case OPT_AND:
                        res = exp_check_logical(op1, op2, p);
                        break;
                    case OPT_SHL:
                    case OPT_SHR:
                    case '|':
                    case '^':
                        res = exp_check_bitwise(op1, op2, p);
                        break;
                    case OPT_EQ:
                    case OPT_NE:
                    case '<':
                    case '>' :
                    case OPT_LE:
                    case OPT_GE:
                        res = exp_check_equality(op1, op2, p);
                        break;
                    case '/': case '%':
                        res = exp_check_arith(op1, op2, p);
                        break;
                    case EXP_CAST:
                        res.type = semantics_cast(p->chd, scope);
                        res.lval = 0;
                        break;
                    case '&':
                        if (p->chd->next)
                            res = exp_check_bitwise(op1, op2, p);
                        else
                            res = exp_check_ref(op1, p);
                        break;
                    case '*': 
                        if (p->chd->next)
                            res = exp_check_arith(op1, op2, p);
                        else
                            res = exp_check_deref(op1, p);
                        break;
                    case '+':
                        if (p->chd->next)
                            res = exp_check_add(op1, op2, p, 0);
                        else
                        {
                            res = op1;
                            res.lval = 0;
                        }
                        break;
                    case '-':
                        if (p->chd->next)
                            res = exp_check_add(op1, op2, p, 1);
                        else
                        {
                            res = op1;
                            res.lval = 0;
                        }
                        break;
                    case '~':
                        res = exp_check_int(op1, p);
                        break;
                    case '!':
                        res = exp_check_scalar(op1, p);
                        break;
                    case OPT_INC: case OPT_DEC:
                        res = exp_check_inc(op1, p);
                        break;
                    case KW_SIZEOF:
                        res = exp_check_sizeof(op1);
                        break;
                    case EXP_POSTFIX:
                        res = exp_check_postfix(p, scope);
                        break;
                    default: 
                        printf("%d\n", p->rec.subtype);
                        assert(0);
                }
            }
            break;
        case NOP: ; break;
        default: assert(0);
    }
    p->ext.type = res.type;
    return res;
}

CVar_t semantics_stmt(CNode *p, CScope_t scope);

CVar_t semantics_if(CNode *p, CScope_t scope) {
    ExpType exp = semantics_exp(p->chd, scope);
    CNode *body1 = p->chd->next,
          *body2 = body1->next;
    CVar_t res;
    if (!IS_SCALAR(exp.type->type))
    {
        sprintf(err_buff, "a scalar is required in 'if' condition");
        ERROR(p->chd);
    }
    cscope_enter(scope);
    res = semantics_stmt(body1, scope);
    cscope_exit(scope);
    if (body2->type != NOP)
    {
        CVar_t t;
        cscope_enter(scope);
        if ((t = semantics_stmt(p->chd->next->next, scope)))
        {
            t->next = res;
            res = t;
        }
        cscope_exit(scope);
    }
    return res;
}

CVar_t semantics_for(CNode *p, CScope_t scope) {
    ExpType exp = semantics_exp(p->chd->next, scope);
    semantics_exp(p->chd, scope);
    semantics_exp(p->chd->next->next, scope);
    CVar_t res;
    if (p->chd->next->type != NOP && !IS_SCALAR(exp.type->type))
    {
        sprintf(err_buff, "a scalar is required in 'for' condition");
        ERROR(p->chd->next);
    }
    cscope_enter(scope);
    scope->inside_loop++;
    res = semantics_stmt(p->chd->next->next->next, scope);
    scope->inside_loop--;
    cscope_exit(scope);
    return res;
}

CVar_t semantics_while(CNode *p, CScope_t scope) {
    ExpType exp = semantics_exp(p->chd, scope);
    CVar_t res;
    if (!IS_SCALAR(exp.type->type))
    {
        sprintf(err_buff, "a scalar is required in 'while' condition");
        ERROR(p->chd);
    }
    cscope_enter(scope);
    scope->inside_loop++;
    res = semantics_stmt(p->chd->next, scope);
    scope->inside_loop--;
    cscope_exit(scope);
    return res;
}

CVar_t semantics_check_loop(CNode *p, CScope_t scope, const char *stmt_name) {
    if (!scope->inside_loop)
    {
        sprintf(err_buff, "%s statement not within a loop", stmt_name);
        ERROR(p);
    }
    return NULL;
}
CVar_t semantics_return(CNode *p, CScope_t scope) {
    assert(scope->func);
    CType_t rt = scope->func->rec.func.ret;
    if (p->chd->type != NOP)
    {
        ExpType t = semantics_exp(p->chd, scope);
        if (rt->type == CVOID)
        {
            if (t.type->type != CVOID)
            {
                sprintf(err_buff, "'return' with a value, in function returning void");
                WARNING(p->chd);
            }
        }
        else
            exp_check_aseq_(rt, p->chd->ext.type, p->chd);
    }
    return NULL;
}

CVar_t semantics_comp(CNode *, CScope_t);
CVar_t semantics_stmt(CNode *p, CScope_t scope) {
    CHECK_TYPE(p, STMT);
    switch (p->rec.subtype)
    {
        case STMT_EXP: 
            semantics_exp(p->chd, scope);
            break;
        case STMT_COMP:
            {
                CVar_t res;
                cscope_enter(scope);
                res = semantics_comp(p, scope);
                cscope_exit(scope);
                return res;
            }
        case STMT_IF:
            return semantics_if(p, scope);
        case STMT_FOR:
            return semantics_for(p, scope);
        case STMT_WHILE:
            return semantics_while(p, scope);
        case STMT_CONT:
            return semantics_check_loop(p, scope, "continue");
        case STMT_BREAK:
            return semantics_check_loop(p, scope, "break");
        case STMT_RET:
            return semantics_return(p, scope);
        default: assert(0);
    }
    return NULL;
}

CVar_t semantics_comp(CNode *p, CScope_t scope) {
    CNode *decls = p->chd,
          *stmts = p->chd->next, *i;
    CVar_t res = NULL;
    if (decls->chd->type != NOP)
        for (i = decls->chd; i; i = i->next)
        {
            CVar_t vlist = semantics_decl(i, scope);
            if (vlist)  /* collect local vars */
            {
                CVar_t p;
                for (p = vlist; p->next; p = p->next);
                p->next = res;
                res = vlist;
            }
        }
    if (stmts->chd->type != NOP)
        for (i = stmts->chd; i; i = i->next)
        {
            CVar_t vlist = semantics_stmt(i, scope);
            if (vlist)  /* collect nested local vars */
            {
                CVar_t p;
                for (p = vlist; p->next; p = p->next);
                p->next = res;
                res = vlist;
            }
        }
    return res;
}

CVar_t semantics_func(CNode *p, CScope_t scope) {
    CHECK_TYPE(p, FUNC_DEF);
    CVar_t head = semantics_declr(p->chd->next,
                                semantics_type_spec(p->chd, scope),
                                scope, 0);
    CType_t func = head->type, funco;
    CVar_t res = cvar_create(head->name, func, p), old = NULL;

    if (!type_is_complete(func->rec.func.ret))
    {
        sprintf(err_buff, "return type is an incomplete type");
        ERROR(func->rec.func.ret->ast);
    }

    scope->func = func;
    cscope_enter(scope);                /* enter function local scope */
    {   /* Note: here is a dirty hack to forcibly push function definition to
           the global scope, while all the types specified in parameters retain in local scope.
           The key point is to make sure semantics_params does not push any var */
        CSNode *ntop = scope->top;
        CVar_t p;
        scope->top = ntop->next;
        scope->lvl--;
        if (cscope_push_var(scope, res))
            old = res;
        scope->top = ntop;
        scope->lvl++;

        for (p = func->rec.func.params; p; p = p->next)
            cscope_push_var(scope, p);
    }
    func->rec.func.local = semantics_comp(p->chd->next->next, scope);   /* check comp */
    func->rec.func.body = p->chd->next->next;
    cscope_exit(scope);                                        /* exit from local scope */

    if (!old)
    {
        old = cscope_lookup_var(scope, res->name);
        funco = old->type;
        if (funco->type != CFUNC)
        {
            sprintf(err_buff, "conflicting types of '%s'", res->name);
            ERROR(res->ast);
        }
        else if (funco->rec.func.body)
        {
            sprintf(err_buff, "redefintion of function '%s'", res->name);
            ERROR(res->ast);
        }
        else if (!is_same_type(funco, res->type))
        {
            sprintf(err_buff, "function defintion does not match the prototype");
            ERROR(res->ast);
        }
        funco->rec.func.local = res->type->rec.func.local;
        funco->rec.func.body = res->type->rec.func.body;
        free(res);
    }
    return old;
}

void semantics_check_(CNode *p, CScope_t scope) {
    p = p->chd;
    switch (p->type)
    {
        case FUNC_DEF: semantics_func(p, scope); break;
        default: ;
    }
}

void semantics_check(CNode *ast) {
    CScope_t scope = cscope_create();
    basic_type_int = ctype_create("int", CINT, NULL);
    basic_type_char = ctype_create("char", CCHAR, NULL);
    basic_type_void = ctype_create("void", CVOID, NULL);
    /* add top-level basic types */
    cscope_push_type(scope, basic_type_int);
    cscope_push_type(scope, basic_type_char);
    cscope_push_type(scope, basic_type_void);
    /* check all definitions and declarations */
    for (ast = ast->chd; ast; ast = ast->next)
    {
        switch (ast->type)
        {
            case FUNC_DEF: 
                semantics_func(ast, scope); break;
            case DECL: 
                semantics_decl(ast, scope); break;
            default: assert(0);
        }
    }
    cscope_debug_print(scope);
    {
        CTNode *p;
        int i;
        for (i = 0; i < MAX_TABLE_SIZE; i++)
            for (p = scope->tvar->head[i]; p; p = p->next)
            {
                cvar_print((CVar_t)p->val);
                fprintf(stderr, "\n");
            }
        for (i = 0; i < MAX_TABLE_SIZE; i++)
            for (p = scope->ttype->head[i]; p; p = p->next)
            {
                ctype_print((CType_t)p->val);
                fprintf(stderr, "\n");
            }
    }
}