~jsvoboda/helenos/sysel

« back to all changes in this revision

Viewing changes to uspace/app/sbi/src/stype_expr.c

  • Committer: Jiri Svoboda
  • Date: 2010-05-08 08:10:44 UTC
  • Revision ID: jiri@wiwaxia-20100508081044-5hvcjwu15rsfvgnv
Update SBI to rev. 244.

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
#include <stdio.h>
46
46
#include <stdlib.h>
47
47
#include <assert.h>
 
48
#include "cspan.h"
48
49
#include "debug.h"
49
50
#include "list.h"
50
51
#include "mytypes.h"
84
85
 
85
86
static void stype_binop_tobject(stype_t *stype, stree_binop_t *binop,
86
87
    tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem);
 
88
static void stype_binop_tenum(stype_t *stype, stree_binop_t *binop,
 
89
    tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem);
 
90
static void stype_binop_tvref(stype_t *stype, stree_binop_t *binop,
 
91
    tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem);
87
92
 
88
93
static void stype_unop(stype_t *stype, stree_unop_t *unop,
89
94
    tdata_item_t **rtitem);
91
96
    tdata_item_t *ta, tdata_item_t **rtitem);
92
97
static void stype_new(stype_t *stype, stree_new_t *new,
93
98
    tdata_item_t **rtitem);
 
99
static void stype_new_object_args(stype_t *stype, stree_new_t *new_op,
 
100
    tdata_item_t *obj_ti);
94
101
 
95
102
static void stype_access(stype_t *stype, stree_access_t *access,
96
103
    tdata_item_t **rtitem);
100
107
    tdata_item_t *arg_ti, tdata_item_t **rtitem);
101
108
static void stype_access_tarray(stype_t *stype, stree_access_t *access,
102
109
    tdata_item_t *arg_ti, tdata_item_t **rtitem);
 
110
static void stype_access_tebase(stype_t *stype, stree_access_t *access,
 
111
    tdata_item_t *arg_ti, tdata_item_t **rtitem);
103
112
 
104
113
static void stype_call(stype_t *stype, stree_call_t *call,
105
114
    tdata_item_t **rtitem);
 
115
static void stype_call_args(stype_t *stype, cspan_t *cspan, list_t *farg_tis,
 
116
    tdata_item_t *fvarg_ti, list_t *args);
106
117
 
107
118
static void stype_index(stype_t *stype, stree_index_t *index,
108
119
    tdata_item_t **rtitem);
132
143
        tdata_item_t *et;
133
144
 
134
145
#ifdef DEBUG_TYPE_TRACE
135
 
        printf("Type expression.\n");
 
146
        cspan_print(expr->cspan);
 
147
        printf(" Type expression.\n");
136
148
#endif
137
149
        /* Silence warning. */
138
150
        et = NULL;
155
167
        expr->titem = et;
156
168
 
157
169
#ifdef DEBUG_TYPE_TRACE
158
 
        printf("Expression type is '");
 
170
        cspan_print(expr->cspan);
 
171
        printf(" Expression type is '");
159
172
        tdata_item_print(et);
160
173
        printf("'.\n");
161
174
#endif
177
190
        tdata_object_t *tobject;
178
191
        stree_csi_t *csi;
179
192
        stree_deleg_t *deleg;
 
193
        stree_enum_t *enum_d;
 
194
        tdata_ebase_t *tebase;
180
195
        stree_fun_t *fun;
181
196
 
182
197
#ifdef DEBUG_TYPE_TRACE
183
 
        printf("Evaluate type of name reference '%s'.\n",
 
198
        cspan_print(nameref->expr->cspan);
 
199
        printf(" Evaluate type of name reference '%s'.\n",
184
200
            strtab_get_str(nameref->name->sid));
185
201
#endif
186
202
        /*
225
241
        if (sym == NULL) {
226
242
                /* Not found. */
227
243
                if (stype->current_csi != NULL) {
228
 
                        printf("Error: Symbol '%s' not found in '",
 
244
                        cspan_print(nameref->expr->cspan);
 
245
                        printf(" Error: Symbol '%s' not found in '",
229
246
                            strtab_get_str(nameref->name->sid));
230
247
                        symbol_print_fqn(csi_to_symbol(stype->current_csi));
231
248
                        printf("'.\n");
232
249
                } else {
233
 
                        printf("Error: Symbol '%s' not found.\n",
 
250
                        cspan_print(nameref->expr->cspan);
 
251
                        printf(" Error: Symbol '%s' not found.\n",
234
252
                            strtab_get_str(nameref->name->sid));
235
253
                }
236
254
                stype_note_error(stype);
259
277
                tobject->static_ref = b_true;
260
278
                tobject->csi = csi;
261
279
                break;
 
280
        case sc_ctor:
 
281
                /* It is not possible to reference a constructor explicitly. */
 
282
                assert(b_false);
262
283
        case sc_deleg:
263
 
                printf("referenced name is deleg\n");
264
284
                deleg = symbol_to_deleg(sym);
265
285
                assert(deleg != NULL);
266
286
                /* Type delegate if it has not been typed yet. */
267
287
                stype_deleg(stype, deleg);
268
288
                titem = deleg->titem;
269
289
                break;
 
290
        case sc_enum:
 
291
                enum_d = symbol_to_enum(sym);
 
292
                assert(enum_d != NULL);
 
293
 
 
294
                titem = tdata_item_new(tic_tebase);
 
295
                tebase = tdata_ebase_new();
 
296
                titem->u.tebase = tebase;
 
297
 
 
298
                /* This is an enum base reference. */
 
299
                tebase->enum_d = enum_d;
 
300
                break;
270
301
        case sc_fun:
271
302
                fun = symbol_to_fun(sym);
272
303
                assert(fun != NULL);
293
324
        tprimitive_class_t tpc;
294
325
 
295
326
#ifdef DEBUG_TYPE_TRACE
296
 
        printf("Evaluate type of literal.\n");
 
327
        cspan_print(literal->expr->cspan);
 
328
        printf(" Evaluate type of literal.\n");
297
329
#endif
298
330
        (void) stype;
299
331
 
321
353
static void stype_self_ref(stype_t *stype, stree_self_ref_t *self_ref,
322
354
    tdata_item_t **rtitem)
323
355
{
 
356
        stree_csi_t *cur_csi;
 
357
        tdata_item_t *titem;
 
358
        tdata_object_t *tobject;
 
359
 
324
360
#ifdef DEBUG_TYPE_TRACE
325
 
        printf("Evaluate type of self reference.\n");
 
361
        cspan_print(self_ref->expr->cspan);
 
362
        printf(" Evaluate type of self reference.\n");
326
363
#endif
327
364
        (void) stype;
328
365
        (void) self_ref;
329
366
 
330
 
        *rtitem = NULL;
 
367
        cur_csi = stype->proc_vr->proc->outer_symbol->outer_csi;
 
368
 
 
369
        /* No global symbols should have procedures. */
 
370
        assert(cur_csi != NULL);
 
371
 
 
372
        /* Construct type item. */
 
373
        titem = tdata_item_new(tic_tobject);
 
374
        tobject = tdata_object_new();
 
375
        titem->u.tobject = tobject;
 
376
 
 
377
        tobject->static_ref = b_false;
 
378
        tobject->csi = cur_csi;
 
379
        list_init(&tobject->targs);
 
380
 
 
381
        *rtitem = titem;
331
382
}
332
383
 
333
384
/** Type a binary operation.
343
394
        tdata_item_t *titem1, *titem2;
344
395
 
345
396
#ifdef DEBUG_TYPE_TRACE
346
 
        printf("Evaluate type of binary operation.\n");
 
397
        cspan_print(binop->expr->cspan);
 
398
        printf(" Evaluate type of binary operation.\n");
347
399
#endif
348
400
        stype_expr(stype, binop->arg1);
349
401
        stype_expr(stype, binop->arg2);
351
403
        titem1 = binop->arg1->titem;
352
404
        titem2 = binop->arg2->titem;
353
405
 
354
 
        if (titem1 == NULL || titem2 == NULL) {
355
 
                printf("Error: Binary operand has no value.\n");
 
406
        if (titem1 == NULL) {
 
407
                cspan_print(binop->arg1->cspan);
 
408
                printf(" Error: Binary operand has no value.\n");
 
409
                stype_note_error(stype);
 
410
                *rtitem = stype_recovery_titem(stype);
 
411
                return;
 
412
        }
 
413
 
 
414
        if (titem2 == NULL) {
 
415
                cspan_print(binop->arg2->cspan);
 
416
                printf(" Error: Binary operand has no value.\n");
356
417
                stype_note_error(stype);
357
418
                *rtitem = stype_recovery_titem(stype);
358
419
                return;
365
426
 
366
427
        equal = tdata_item_equal(titem1, titem2);
367
428
        if (equal != b_true) {
368
 
                printf("Error: Binary operation arguments "
 
429
                cspan_print(binop->expr->cspan);
 
430
                printf(" Error: Binary operation arguments "
369
431
                    "have different types ('");
370
432
                tdata_item_print(titem1);
371
433
                printf("' and '");
383
445
        case tic_tobject:
384
446
                stype_binop_tobject(stype, binop, titem1, titem2, rtitem);
385
447
                break;
 
448
        case tic_tenum:
 
449
                stype_binop_tenum(stype, binop, titem1, titem2, rtitem);
 
450
                break;
 
451
        case tic_tvref:
 
452
                stype_binop_tvref(stype, binop, titem1, titem2, rtitem);
 
453
                break;
386
454
        default:
387
 
                printf("Error: Binary operation on value which is not of a "
 
455
                cspan_print(binop->expr->cspan);
 
456
                printf(" Error: Binary operation on value which is not of a "
388
457
                    "supported type (found '");
389
458
                tdata_item_print(titem1);
390
459
                printf("').\n");
457
526
        case bo_minus:
458
527
        case bo_mult:
459
528
                /* Arithmetic -> error */
460
 
                printf("Error: Binary operation (%d) on booleans.\n",
 
529
                cspan_print(binop->expr->cspan);
 
530
                printf(" Error: Binary operation (%d) on booleans.\n",
461
531
                    binop->bc);
462
532
                stype_note_error(stype);
463
533
                *rtitem = stype_recovery_titem(stype);
464
534
                return;
 
535
        case bo_and:
 
536
        case bo_or:
 
537
                /* Boolean -> boolean type */
 
538
                rtpc = tpc_bool;
 
539
                break;
465
540
        }
466
541
 
467
542
        res_ti = tdata_item_new(tic_tprimitive);
497
572
        case bo_plus:
498
573
        case bo_minus:
499
574
        case bo_mult:
500
 
                /* Arithmetic -> error */
501
 
                printf("Error: Binary operation (%d) on characters.\n",
 
575
        case bo_and:
 
576
        case bo_or:
 
577
                /* Arithmetic, boolean -> error */
 
578
                cspan_print(binop->expr->cspan);
 
579
                printf(" Error: Binary operation (%d) on characters.\n",
502
580
                    binop->bc);
503
581
                stype_note_error(stype);
504
582
                rtpc = tpc_char;
541
619
                /* Arithmetic -> int type */
542
620
                rtpc = tpc_int;
543
621
                break;
 
622
        case bo_and:
 
623
        case bo_or:
 
624
                /* Boolean -> error */
 
625
                cspan_print(binop->expr->cspan);
 
626
                printf(" Error: Binary operation (%d) on integers.\n",
 
627
                    binop->bc);
 
628
                stype_note_error(stype);
 
629
                rtpc = tpc_char;
 
630
                break;
544
631
        }
545
632
 
546
633
        res_ti = tdata_item_new(tic_tprimitive);
560
647
{
561
648
        (void) binop;
562
649
 
563
 
        printf("Unimplemented; Binary operation on nil.\n");
 
650
        cspan_print(binop->expr->cspan);
 
651
        printf(" Unimplemented: Binary operation on nil.\n");
564
652
        stype_note_error(stype);
565
653
        *rtitem = stype_recovery_titem(stype);
566
654
}
577
665
        tprimitive_class_t rtpc;
578
666
        tdata_item_t *res_ti;
579
667
 
580
 
        if (binop->bc != bo_plus) {
581
 
                printf("Unimplemented: Binary operation(%d) "
582
 
                    "on strings.\n", binop->bc);
 
668
        switch (binop->bc) {
 
669
        case bo_equal:
 
670
        case bo_notequal:
 
671
                /* Comparison -> boolean type */
 
672
                rtpc = tpc_bool;
 
673
                break;
 
674
        case bo_plus:
 
675
                /* Concatenation -> string type */
 
676
                rtpc = tpc_string;
 
677
                break;
 
678
 
 
679
        case bo_lt:
 
680
        case bo_gt:
 
681
        case bo_lt_equal:
 
682
        case bo_gt_equal:
 
683
 
 
684
        case bo_minus:
 
685
        case bo_mult:
 
686
        case bo_and:
 
687
        case bo_or:
 
688
                /* Ordering, arithmetic, boolean -> error */
 
689
                cspan_print(binop->expr->cspan);
 
690
                printf(" Error: Binary operation (%d) on strings.\n",
 
691
                    binop->bc);
583
692
                stype_note_error(stype);
584
 
                *rtitem = stype_recovery_titem(stype);
585
 
                return;
 
693
                rtpc = tpc_char;
 
694
                break;
586
695
        }
587
696
 
588
 
        rtpc = tpc_string;
589
 
 
590
697
        res_ti = tdata_item_new(tic_tprimitive);
591
698
        res_ti->u.tprimitive = tdata_primitive_new(rtpc);
592
699
 
607
714
 
608
715
        (void) binop;
609
716
 
610
 
        printf("Error: Cannot apply operator to resource type.\n");
 
717
        cspan_print(binop->expr->cspan);
 
718
        printf(" Error: Cannot apply operator to resource type.\n");
611
719
        stype_note_error(stype);
612
720
        rtpc = tpc_resource;
613
721
 
644
752
                res_ti = stype_boolean_titem(stype);
645
753
                break;
646
754
        default:
647
 
                printf("Error: Binary operation (%d) on objects.\n",
648
 
                    binop->bc);
649
 
                stype_note_error(stype);
650
 
                *rtitem = stype_recovery_titem(stype);
651
 
                return;
652
 
        }
653
 
 
654
 
        *rtitem = res_ti;
655
 
}
656
 
 
 
755
                cspan_print(binop->expr->cspan);
 
756
                printf(" Error: Binary operation (%d) on objects.\n",
 
757
                    binop->bc);
 
758
                stype_note_error(stype);
 
759
                *rtitem = stype_recovery_titem(stype);
 
760
                return;
 
761
        }
 
762
 
 
763
        *rtitem = res_ti;
 
764
}
 
765
 
 
766
/** Type a binary operation with arguments of an enum type.
 
767
 *
 
768
 * @param stype         Static typing object
 
769
 * @param binop         Binary operation
 
770
 * @param ta            Type of first argument
 
771
 * @param tb            Type of second argument
 
772
 * @param rtitem        Place to store result type
 
773
 */
 
774
static void stype_binop_tenum(stype_t *stype, stree_binop_t *binop,
 
775
    tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem)
 
776
{
 
777
        tdata_item_t *res_ti;
 
778
 
 
779
        assert(ta->tic == tic_tenum);
 
780
        assert(tb->tic == tic_tenum);
 
781
 
 
782
        switch (binop->bc) {
 
783
        case bo_equal:
 
784
        case bo_notequal:
 
785
                /* Comparison -> boolean type */
 
786
                res_ti = stype_boolean_titem(stype);
 
787
                break;
 
788
        default:
 
789
                cspan_print(binop->expr->cspan);
 
790
                printf(" Error: Binary operation (%d) on values of enum "
 
791
                    "type.\n", binop->bc);
 
792
                stype_note_error(stype);
 
793
                *rtitem = stype_recovery_titem(stype);
 
794
                return;
 
795
        }
 
796
 
 
797
        *rtitem = res_ti;
 
798
}
 
799
 
 
800
/** Type a binary operation with arguments of a variable type.
 
801
 *
 
802
 * @param stype         Static typing object
 
803
 * @param binop         Binary operation
 
804
 * @param ta            Type of first argument
 
805
 * @param tb            Type of second argument
 
806
 * @param rtitem        Place to store result type
 
807
 */
 
808
static void stype_binop_tvref(stype_t *stype, stree_binop_t *binop,
 
809
    tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem)
 
810
{
 
811
        tdata_item_t *res_ti;
 
812
 
 
813
        assert(ta->tic == tic_tvref || (ta->tic == tic_tprimitive &&
 
814
            ta->u.tprimitive->tpc == tpc_nil));
 
815
        assert(tb->tic == tic_tvref || (tb->tic == tic_tprimitive &&
 
816
            tb->u.tprimitive->tpc == tpc_nil));
 
817
 
 
818
        switch (binop->bc) {
 
819
        case bo_equal:
 
820
        case bo_notequal:
 
821
                /* Comparison -> boolean type */
 
822
                res_ti = stype_boolean_titem(stype);
 
823
                break;
 
824
        default:
 
825
                cspan_print(binop->expr->cspan);
 
826
                printf(" Error: Binary operation (%d) on variable types.\n",
 
827
                    binop->bc);
 
828
                stype_note_error(stype);
 
829
                *rtitem = stype_recovery_titem(stype);
 
830
                return;
 
831
        }
 
832
 
 
833
        *rtitem = res_ti;
 
834
}
657
835
 
658
836
/** Type a unary operation.
659
837
 *
667
845
        tdata_item_t *titem;
668
846
 
669
847
#ifdef DEBUG_TYPE_TRACE
670
 
        printf("Evaluate type of unary operation.\n");
 
848
        cspan_print(unop->expr->cspan);
 
849
        printf(" Evaluate type of unary operation.\n");
671
850
#endif
672
851
        stype_expr(stype, unop->arg);
673
852
 
683
862
                stype_unop_tprimitive(stype, unop, titem, rtitem);
684
863
                break;
685
864
        default:
686
 
                printf("Error: Unary operation on value which is not of a "
 
865
                cspan_print(unop->arg->cspan);
 
866
                printf(" Error: Unary operation on value which is not of a "
687
867
                    "supported type (found '");
688
868
                tdata_item_print(titem);
689
869
                printf("').\n");
719
899
                rtpc = tpc_int;
720
900
                break;
721
901
        default:
722
 
                printf("Error: Unary operator applied on unsupported "
 
902
                cspan_print(unop->arg->cspan);
 
903
                printf(" Error: Unary operator applied on unsupported "
723
904
                    "primitive type %d.\n", ta->u.tprimitive->tpc);
724
905
                stype_note_error(stype);
725
906
                *rtitem = stype_recovery_titem(stype);
742
923
    tdata_item_t **rtitem)
743
924
{
744
925
#ifdef DEBUG_TYPE_TRACE
 
926
        cspan_print(new_op->expr->cspan);
745
927
        printf("Evaluate type of 'new' operation.\n");
746
928
#endif
747
929
        /*
753
935
        if ((*rtitem)->tic == tic_ignore) {
754
936
                /* An error occured when evaluating the type expression. */
755
937
                stype_note_error(stype);
756
 
        }
 
938
                *rtitem = stype_recovery_titem(stype);
 
939
                return;
 
940
        }
 
941
 
 
942
        if ((*rtitem)->tic == tic_tobject)
 
943
                stype_new_object_args(stype, new_op, *rtitem);
 
944
}
 
945
 
 
946
/** Type a new object operation arguments.
 
947
 *
 
948
 * @param stype         Static typing object
 
949
 * @param new_op        @c new operation
 
950
 */
 
951
static void stype_new_object_args(stype_t *stype, stree_new_t *new_op,
 
952
    tdata_item_t *obj_ti)
 
953
{
 
954
        stree_csi_t *csi;
 
955
        stree_ctor_t *ctor;
 
956
        stree_symbol_t *ctor_sym;
 
957
        stree_ident_t *ctor_ident;
 
958
        tdata_fun_sig_t *tsig;
 
959
 
 
960
        assert(obj_ti->tic == tic_tobject);
 
961
        csi = obj_ti->u.tobject->csi;
 
962
        ctor_ident = stree_ident_new();
 
963
        ctor_ident->sid = strtab_get_sid(CTOR_IDENT);
 
964
 
 
965
        /* Find constructor. */
 
966
        ctor_sym = symbol_search_csi_no_base(stype->program, csi,
 
967
            ctor_ident);
 
968
 
 
969
        if (ctor_sym == NULL && !list_is_empty(&new_op->ctor_args)) {
 
970
                cspan_print(new_op->expr->cspan);
 
971
                printf(" Error: Passing arguments to 'new' but no "
 
972
                    "constructor found.\n");
 
973
                stype_note_error(stype);
 
974
                return;
 
975
        }
 
976
 
 
977
        if (ctor_sym == NULL)
 
978
                return;
 
979
 
 
980
        ctor = symbol_to_ctor(ctor_sym);
 
981
        assert(ctor != NULL);
 
982
 
 
983
        /* Type constructor header if it has not been typed yet. */
 
984
        stype_ctor_header(stype, ctor);
 
985
        if (ctor->titem->tic == tic_ignore)
 
986
                return;
 
987
 
 
988
        assert(ctor->titem->tic == tic_tfun);
 
989
        tsig = ctor->titem->u.tfun->tsig;
 
990
 
 
991
        stype_call_args(stype, new_op->expr->cspan, &tsig->arg_ti,
 
992
            tsig->varg_ti, &new_op->ctor_args);
757
993
}
758
994
 
759
995
/** Type a member access operation.
768
1004
        tdata_item_t *arg_ti;
769
1005
 
770
1006
#ifdef DEBUG_TYPE_TRACE
771
 
        printf("Evaluate type of access operation.\n");
 
1007
        cspan_print(access->expr->cspan);
 
1008
        printf(" Evaluate type of access operation.\n");
772
1009
#endif
773
1010
        stype_expr(stype, access->arg);
774
1011
        arg_ti = access->arg->titem;
775
1012
 
776
1013
        if (arg_ti == NULL) {
777
 
                printf("Error: Argument of access has no value.\n");
 
1014
                cspan_print(access->arg->cspan);
 
1015
                printf(" Error: Argument of access operation has no value.\n");
778
1016
                stype_note_error(stype);
779
1017
                *rtitem = stype_recovery_titem(stype);
780
1018
                return;
791
1029
                stype_access_tarray(stype, access, arg_ti, rtitem);
792
1030
                break;
793
1031
        case tic_tdeleg:
794
 
                printf("Error: Using '.' operator on a function.\n");
 
1032
                cspan_print(access->arg->cspan);
 
1033
                printf(" Error: Using '.' operator on a delegate.\n");
 
1034
                stype_note_error(stype);
 
1035
                *rtitem = stype_recovery_titem(stype);
 
1036
                break;
 
1037
        case tic_tebase:
 
1038
                stype_access_tebase(stype, access, arg_ti, rtitem);
 
1039
                break;
 
1040
        case tic_tenum:
 
1041
                cspan_print(access->arg->cspan);
 
1042
                printf(" Error: Using '.' operator on expression of enum "
 
1043
                    "type.\n");
795
1044
                stype_note_error(stype);
796
1045
                *rtitem = stype_recovery_titem(stype);
797
1046
                break;
798
1047
        case tic_tfun:
799
 
                printf("Error: Using '.' operator on a delegate.\n");
 
1048
                cspan_print(access->arg->cspan);
 
1049
                printf(" Error: Using '.' operator on a function.\n");
800
1050
                stype_note_error(stype);
801
1051
                *rtitem = stype_recovery_titem(stype);
802
1052
                break;
803
1053
        case tic_tvref:
804
1054
                /* Cannot allow this without some constraint. */
805
 
                printf("Error: Using '.' operator on generic data.\n");
 
1055
                cspan_print(access->arg->cspan);
 
1056
                printf(" Error: Using '.' operator on generic data.\n");
806
1057
                *rtitem = stype_recovery_titem(stype);
807
1058
                break;
808
1059
        case tic_ignore:
821
1072
static void stype_access_tprimitive(stype_t *stype, stree_access_t *access,
822
1073
    tdata_item_t *arg_ti, tdata_item_t **rtitem)
823
1074
{
824
 
        (void) stype;
825
 
        (void) access;
826
 
        (void) rtitem;
827
 
 
828
 
        printf("Error: Unimplemented: Accessing primitive type '");
829
 
        tdata_item_print(arg_ti);
830
 
        printf("'.\n");
831
 
        stype_note_error(stype);
832
 
        *rtitem = stype_recovery_titem(stype);
 
1075
        (void) arg_ti;
 
1076
 
 
1077
        /* Box the value. */
 
1078
        access->arg = stype_box_expr(stype, access->arg);
 
1079
        if (access->arg->titem->tic == tic_ignore) {
 
1080
                *rtitem = stype_recovery_titem(stype);
 
1081
                return;
 
1082
        }
 
1083
 
 
1084
        /* Access the boxed object. */
 
1085
        stype_access_tobject(stype, access, access->arg->titem, rtitem);
833
1086
}
834
1087
 
835
1088
/** Type an object access operation.
844
1097
{
845
1098
        stree_symbol_t *member_sym;
846
1099
        stree_var_t *var;
 
1100
        stree_enum_t *enum_d;
847
1101
        stree_fun_t *fun;
848
1102
        stree_prop_t *prop;
849
1103
        tdata_object_t *tobject;
862
1116
 
863
1117
        if (member_sym == NULL) {
864
1118
                /* No such member found. */
865
 
                printf("Error: CSI '");
 
1119
                cspan_print(access->member_name->cspan);
 
1120
                printf(" Error: CSI '");
866
1121
                symbol_print_fqn(csi_to_symbol(tobject->csi));
867
1122
                printf("' has no member named '%s'.\n",
868
1123
                    strtab_get_str(access->member_name->sid));
878
1133
 
879
1134
        switch (member_sym->sc) {
880
1135
        case sc_csi:
881
 
                printf("Error: Accessing object member which is nested "
 
1136
                cspan_print(access->member_name->cspan);
 
1137
                printf(" Error: Accessing object member which is nested "
882
1138
                    "CSI.\n");
883
1139
                stype_note_error(stype);
884
1140
                *rtitem = stype_recovery_titem(stype);
885
1141
                return;
 
1142
        case sc_ctor:
 
1143
                /* It is not possible to reference a constructor explicitly. */
 
1144
                assert(b_false);
886
1145
        case sc_deleg:
887
 
                printf("Error: Accessing object member which is a "
 
1146
                cspan_print(access->member_name->cspan);
 
1147
                printf(" Error: Accessing object member which is a "
888
1148
                    "delegate.\n");
889
1149
                stype_note_error(stype);
890
1150
                *rtitem = stype_recovery_titem(stype);
891
1151
                return;
 
1152
        case sc_enum:
 
1153
                enum_d = symbol_to_enum(member_sym);
 
1154
                assert(enum_d != NULL);
 
1155
                /* Type enum if it has not been typed yet. */
 
1156
                stype_enum(stype, enum_d);
 
1157
                mtitem = enum_d->titem;
 
1158
                break;
892
1159
        case sc_fun:
893
1160
                fun = symbol_to_fun(member_sym);
894
1161
                assert(fun != NULL);
936
1203
        (void) access;
937
1204
        (void) rtitem;
938
1205
 
939
 
        printf("Error: Unimplemented: Accessing array type '");
 
1206
        cspan_print(access->arg->cspan);
 
1207
        printf(" Error: Unimplemented: Accessing array type '");
940
1208
        tdata_item_print(arg_ti);
941
1209
        printf("'.\n");
942
1210
        stype_note_error(stype);
943
1211
        *rtitem = stype_recovery_titem(stype);
944
1212
}
945
1213
 
 
1214
/** Type an enum access operation.
 
1215
 *
 
1216
 * @param stype         Static typing object
 
1217
 * @param access        Member access operation
 
1218
 * @param arg_ti        Base type
 
1219
 * @param rtitem        Place to store result type
 
1220
*/
 
1221
static void stype_access_tebase(stype_t *stype, stree_access_t *access,
 
1222
    tdata_item_t *arg_ti, tdata_item_t **rtitem)
 
1223
{
 
1224
        tdata_ebase_t *tebase;
 
1225
        tdata_enum_t *tenum;
 
1226
        tdata_item_t *mtitem;
 
1227
        stree_embr_t *embr;
 
1228
 
 
1229
#ifdef DEBUG_TYPE_TRACE
 
1230
        printf("Type an ebase access operation.\n");
 
1231
#endif
 
1232
        assert(arg_ti->tic == tic_tebase);
 
1233
        tebase = arg_ti->u.tebase;
 
1234
 
 
1235
        /* Look for a member with the specified name. */
 
1236
        embr = stree_enum_find_mbr(tebase->enum_d, access->member_name);
 
1237
 
 
1238
        if (embr == NULL) {
 
1239
                /* No such member found. */
 
1240
                cspan_print(access->member_name->cspan);
 
1241
                printf(" Error: Enum type '");
 
1242
                symbol_print_fqn(enum_to_symbol(tebase->enum_d));
 
1243
                printf("' has no member named '%s'.\n",
 
1244
                    strtab_get_str(access->member_name->sid));
 
1245
                stype_note_error(stype);
 
1246
                *rtitem = stype_recovery_titem(stype);
 
1247
                return;
 
1248
        }
 
1249
 
 
1250
#ifdef DEBUG_RUN_TRACE
 
1251
        printf("Found member '%s'.\n",
 
1252
            strtab_get_str(access->member_name->sid));
 
1253
#endif
 
1254
 
 
1255
        mtitem = tdata_item_new(tic_tenum);
 
1256
        tenum = tdata_enum_new();
 
1257
        mtitem->u.tenum = tenum;
 
1258
        tenum->enum_d = tebase->enum_d;
 
1259
 
 
1260
        *rtitem = mtitem;
 
1261
}
 
1262
 
 
1263
 
946
1264
/** Type a call operation.
947
1265
 *
948
1266
 * @param stype         Static typing object
952
1270
static void stype_call(stype_t *stype, stree_call_t *call,
953
1271
    tdata_item_t **rtitem)
954
1272
{
955
 
        list_node_t *fargt_n;
956
 
        tdata_item_t *farg_ti;
957
 
        tdata_item_t *varg_ti;
958
 
 
959
 
        list_node_t *arg_n;
960
 
        stree_expr_t *arg;
961
 
        stree_expr_t *carg;
962
 
 
963
1273
        tdata_item_t *fun_ti;
964
1274
        tdata_fun_sig_t *tsig;
965
1275
 
966
 
        int cnt;
967
 
 
968
1276
#ifdef DEBUG_TYPE_TRACE
969
 
        printf("Evaluate type of call operation.\n");
 
1277
        cspan_print(call->expr->cspan);
 
1278
        printf(" Evaluate type of call operation.\n");
970
1279
#endif
971
1280
        /* Type the function */
972
1281
        stype_expr(stype, call->fun);
985
1294
                *rtitem = stype_recovery_titem(stype);
986
1295
                return;
987
1296
        default:
988
 
                printf("Error: Calling something which is not a function ");
 
1297
                cspan_print(call->fun->cspan);
 
1298
                printf(" Error: Calling something which is not a function ");
989
1299
                printf("(found '");
990
1300
                tdata_item_print(fun_ti);
991
1301
                printf("').\n");
994
1304
                return;
995
1305
        }
996
1306
 
997
 
        /* Type and check the arguments. */
998
 
        fargt_n = list_first(&tsig->arg_ti);
999
 
        arg_n = list_first(&call->args);
 
1307
        /* Type call arguments. */
 
1308
        stype_call_args(stype, call->expr->cspan, &tsig->arg_ti, tsig->varg_ti,
 
1309
            &call->args);
 
1310
 
 
1311
        if (tsig->rtype != NULL) {
 
1312
                /* XXX Might be better to clone here. */
 
1313
                *rtitem = tsig->rtype;
 
1314
        } else {
 
1315
                *rtitem = NULL;
 
1316
        }
 
1317
}
 
1318
 
 
1319
/** Type call arguments.
 
1320
 *
 
1321
 * Type arguments in call to a function or constructor.
 
1322
 *
 
1323
 * @param stype         Static typing object
 
1324
 * @param cpsan         Cspan to print in case of error.
 
1325
 * @param farg_tis      Formal argument types (list of tdata_item_t)
 
1326
 * @param args          Real arguments (list of stree_expr_t)
 
1327
 */
 
1328
static void stype_call_args(stype_t *stype, cspan_t *cspan, list_t *farg_tis,
 
1329
    tdata_item_t *fvarg_ti, list_t *args)
 
1330
{
 
1331
        list_node_t *fargt_n;
 
1332
        tdata_item_t *farg_ti;
 
1333
        tdata_item_t *varg_ti;
 
1334
 
 
1335
        list_node_t *arg_n;
 
1336
        stree_expr_t *arg;
 
1337
        stree_expr_t *carg;
 
1338
 
 
1339
        int cnt;
 
1340
 
 
1341
        /* Type and check regular arguments. */
 
1342
        fargt_n = list_first(farg_tis);
 
1343
        arg_n = list_first(args);
1000
1344
 
1001
1345
        cnt = 0;
1002
1346
        while (fargt_n != NULL && arg_n != NULL) {
1007
1351
                /* XXX Because of overloaded bultin WriteLine */
1008
1352
                if (farg_ti == NULL) {
1009
1353
                        /* Skip the check */
1010
 
                        fargt_n = list_next(&tsig->arg_ti, fargt_n);
1011
 
                        arg_n = list_next(&call->args, arg_n);
 
1354
                        fargt_n = list_next(farg_tis, fargt_n);
 
1355
                        arg_n = list_next(args, arg_n);
1012
1356
                        continue;
1013
1357
                }
1014
1358
 
1018
1362
                /* Patch code with augmented expression. */
1019
1363
                list_node_setdata(arg_n, carg);
1020
1364
 
1021
 
                fargt_n = list_next(&tsig->arg_ti, fargt_n);
1022
 
                arg_n = list_next(&call->args, arg_n);
 
1365
                fargt_n = list_next(farg_tis, fargt_n);
 
1366
                arg_n = list_next(args, arg_n);
1023
1367
        }
1024
1368
 
1025
1369
        /* Type and check variadic arguments. */
1026
 
        if (tsig->varg_ti != NULL) {
 
1370
        if (fvarg_ti != NULL) {
1027
1371
                /* Obtain type of packed argument. */
1028
 
                farg_ti = tsig->varg_ti;
 
1372
                farg_ti = fvarg_ti;
1029
1373
 
1030
1374
                /* Get array element type */
1031
1375
                assert(farg_ti->tic == tic_tarray);
1041
1385
                        /* Patch code with augmented expression. */
1042
1386
                        list_node_setdata(arg_n, carg);
1043
1387
 
1044
 
                        arg_n = list_next(&call->args, arg_n);
 
1388
                        arg_n = list_next(args, arg_n);
1045
1389
                }
1046
1390
        }
1047
1391
 
1048
1392
        if (fargt_n != NULL) {
1049
 
                printf("Error: Too few arguments to function.\n");
 
1393
                cspan_print(cspan);
 
1394
                printf(" Error: Too few arguments.\n");
1050
1395
                stype_note_error(stype);
1051
1396
        }
1052
1397
 
1053
1398
        if (arg_n != NULL) {
1054
 
                printf("Error: Too many arguments to function.\n");
 
1399
                cspan_print(cspan);
 
1400
                printf(" Error: Too many arguments.\n");
1055
1401
                stype_note_error(stype);
1056
1402
        }
1057
 
 
1058
 
        if (tsig->rtype != NULL) {
1059
 
                /* XXX Might be better to clone here. */
1060
 
                *rtitem = tsig->rtype;
1061
 
        } else {
1062
 
                *rtitem = NULL;
1063
 
        }
1064
1403
}
1065
1404
 
1066
1405
/** Type an indexing operation.
1077
1416
        stree_expr_t *arg;
1078
1417
 
1079
1418
#ifdef DEBUG_TYPE_TRACE
1080
 
        printf("Evaluate type of index operation.\n");
 
1419
        cspan_print(index->expr->cspan);
 
1420
        printf(" Evaluate type of index operation.\n");
1081
1421
#endif
1082
1422
        stype_expr(stype, index->base);
1083
1423
        base_ti = index->base->titem;
1102
1442
                stype_index_tarray(stype, index, base_ti, rtitem);
1103
1443
                break;
1104
1444
        case tic_tdeleg:
1105
 
                printf("Error: Indexing a delegate.\n");
 
1445
                cspan_print(index->base->cspan);
 
1446
                printf(" Error: Indexing a delegate.\n");
 
1447
                stype_note_error(stype);
 
1448
                *rtitem = stype_recovery_titem(stype);
 
1449
                break;
 
1450
        case tic_tebase:
 
1451
                cspan_print(index->base->cspan);
 
1452
                printf(" Error: Indexing an enum declaration.\n");
 
1453
                stype_note_error(stype);
 
1454
                *rtitem = stype_recovery_titem(stype);
 
1455
                break;
 
1456
        case tic_tenum:
 
1457
                cspan_print(index->base->cspan);
 
1458
                printf(" Error: Indexing an enum value.\n");
1106
1459
                stype_note_error(stype);
1107
1460
                *rtitem = stype_recovery_titem(stype);
1108
1461
                break;
1109
1462
        case tic_tfun:
1110
 
                printf("Error: Indexing a function.\n");
 
1463
                cspan_print(index->base->cspan);
 
1464
                printf(" Error: Indexing a function.\n");
1111
1465
                stype_note_error(stype);
1112
1466
                *rtitem = stype_recovery_titem(stype);
1113
1467
                break;
1114
1468
        case tic_tvref:
1115
1469
                /* Cannot allow this without some constraint. */
1116
 
                printf("Error: Indexing generic data.\n");
 
1470
                cspan_print(index->base->cspan);
 
1471
                printf(" Error: Indexing generic data.\n");
1117
1472
                *rtitem = stype_recovery_titem(stype);
1118
1473
                break;
1119
1474
        case tic_ignore:
1148
1503
                return;
1149
1504
        }
1150
1505
 
1151
 
        printf("Error: Indexing primitive type '");
 
1506
        cspan_print(index->base->cspan);
 
1507
        printf(" Error: Indexing primitive type '");
1152
1508
        tdata_item_print(base_ti);
1153
1509
        printf("'.\n");
1154
1510
        stype_note_error(stype);
1175
1531
        (void) index;
1176
1532
 
1177
1533
#ifdef DEBUG_TYPE_TRACE
1178
 
        printf("Indexing object type '");
 
1534
        cspan_print(index->expr->cspan);
 
1535
        printf(" Indexing object type '");
1179
1536
        tdata_item_print(base_ti);
1180
1537
        printf("'.\n");
1181
1538
#endif
1189
1546
        idx_sym = symbol_search_csi(stype->program, tobject->csi, idx_ident);
1190
1547
 
1191
1548
        if (idx_sym == NULL) {
1192
 
                printf("Error: Indexing object of type '");
 
1549
                cspan_print(index->base->cspan);
 
1550
                printf(" Error: Indexing object of type '");
1193
1551
                tdata_item_print(base_ti);
1194
1552
                printf("' which does not have an indexer.\n");
1195
1553
                stype_note_error(stype);
1245
1603
                if (arg->titem->tic != tic_tprimitive ||
1246
1604
                    arg->titem->u.tprimitive->tpc != tpc_int) {
1247
1605
 
1248
 
                        printf("Error: Array index is not an integer.\n");
 
1606
                        cspan_print(arg->cspan);
 
1607
                        printf(" Error: Array index is not an integer.\n");
1249
1608
                        stype_note_error(stype);
1250
1609
                }
1251
1610
 
1253
1612
        }
1254
1613
 
1255
1614
        if (arg_count != base_ti->u.tarray->rank) {
1256
 
                printf("Error: Using %d indices with array of rank %d.\n",
 
1615
                cspan_print(index->expr->cspan);
 
1616
                printf(" Error: Using %d indices with array of rank %d.\n",
1257
1617
                    arg_count, base_ti->u.tarray->rank);
1258
1618
                stype_note_error(stype);
1259
1619
        }
1273
1633
        stree_expr_t *csrc;
1274
1634
 
1275
1635
#ifdef DEBUG_TYPE_TRACE
1276
 
        printf("Evaluate type of assignment.\n");
 
1636
        cspan_print(assign->expr->cspan);
 
1637
        printf(" Evaluate type of assignment.\n");
1277
1638
#endif
1278
1639
        stype_expr(stype, assign->dest);
1279
1640
        stype_expr(stype, assign->src);
1296
1657
        tdata_item_t *titem;
1297
1658
 
1298
1659
#ifdef DEBUG_TYPE_TRACE
1299
 
        printf("Evaluate type of @c as conversion.\n");
 
1660
        cspan_print(as_op->expr->cspan);
 
1661
        printf(" Evaluate type of @c as conversion.\n");
1300
1662
#endif
1301
1663
        stype_expr(stype, as_op->arg);
1302
1664
        run_texpr(stype->program, stype->current_csi, as_op->dtype, &titem);
1303
1665
 
1304
1666
        /* Check that target type is derived from argument type. */
1305
1667
        if (tdata_is_ti_derived_from_ti(titem, as_op->arg->titem) != b_true) {
1306
 
                printf("Error: Target of 'as' operator '");
 
1668
                cspan_print(as_op->dtype->cspan);
 
1669
                printf(" Error: Target of 'as' operator '");
1307
1670
                tdata_item_print(titem);
1308
1671
                printf("' is not derived from '");
1309
1672
                tdata_item_print(as_op->arg->titem);
1331
1694
        builtin_t *bi;
1332
1695
 
1333
1696
#ifdef DEBUG_TYPE_TRACE
1334
 
        printf("Evaluate type of boxing operation.\n");
 
1697
        cspan_print(box->expr->cspan);
 
1698
        printf(" Evaluate type of boxing operation.\n");
1335
1699
#endif
1336
1700
        bi = stype->program->builtin;
1337
1701