Changeset 883fedc in mainline for uspace/app/sbi/src/stype_expr.c
- Timestamp:
- 2010-04-23T23:09:56Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 37c9fc8
- Parents:
- 80badbe (diff), 6c39a907 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sbi/src/stype_expr.c
r80badbe r883fedc 27 27 */ 28 28 29 /** @file Type expressions. */ 29 /** @file Typing of expressions. 30 * 31 * This module types (data) expressions -- not to be confused with evaluating 32 * type expressions! Thus the type of each (sub-)expression is determined 33 * and stored in its @c titem field. 34 * 35 * It can also happen that, due to implicit conversions, the expression 36 * needs to be patched to insert these conversions. 37 * 38 * If a type error occurs within an expression, @c stype->error is set 39 * and the type of the expression will be @c tic_ignore. This type item 40 * is propagated upwards and causes further typing errors to be ignored 41 * (this prevents a type error avalanche). Type checking is thus resumed 42 * at the next expression. 43 */ 30 44 31 45 #include <stdio.h> … … 103 117 tdata_item_t **rtitem); 104 118 static void stype_as(stype_t *stype, stree_as_t *as_op, tdata_item_t **rtitem); 105 106 107 /** Type expression. */ 119 static void stype_box(stype_t *stype, stree_box_t *box, tdata_item_t **rtitem); 120 121 122 /** Type expression 123 * 124 * The type is stored in @a expr->titem. If the express contains a type error, 125 * @a stype->error will be set when this function returns. 126 * 127 * @param stype Static typing object 128 * @param expr Expression 129 */ 108 130 void stype_expr(stype_t *stype, stree_expr_t *expr) 109 131 { … … 128 150 case ec_assign: stype_assign(stype, expr->u.assign, &et); break; 129 151 case ec_as: stype_as(stype, expr->u.as_op, &et); break; 152 case ec_box: stype_box(stype, expr->u.box, &et); break; 130 153 } 131 154 … … 139 162 } 140 163 141 /** Type name reference. */ 164 /** Type name reference. 165 * 166 * @param stype Static typing object 167 * @param nameref Name reference 168 * @param rtitem Place to store result type 169 */ 142 170 static void stype_nameref(stype_t *stype, stree_nameref_t *nameref, 143 171 tdata_item_t **rtitem) … … 149 177 tdata_object_t *tobject; 150 178 stree_csi_t *csi; 179 stree_deleg_t *deleg; 151 180 stree_fun_t *fun; 152 181 … … 231 260 tobject->csi = csi; 232 261 break; 262 case sc_deleg: 263 printf("referenced name is deleg\n"); 264 deleg = symbol_to_deleg(sym); 265 assert(deleg != NULL); 266 /* Type delegate if it has not been typed yet. */ 267 stype_deleg(stype, deleg); 268 titem = deleg->titem; 269 break; 233 270 case sc_fun: 234 271 fun = symbol_to_fun(sym); 235 272 assert(fun != NULL); 236 237 titem = tdata_item_new(tic_tfun); 238 titem->u.tfun = tdata_fun_new(); 239 titem->u.tfun->fun = fun; 273 /* Type function header if it has not been typed yet. */ 274 stype_fun_header(stype, fun); 275 titem = fun->titem; 240 276 break; 241 277 } … … 244 280 } 245 281 246 /** Type a literal. */ 282 /** Type a literal. 283 * 284 * @param stype Static typing object 285 * @param literal Literal 286 * @param rtitem Place to store result type 287 */ 247 288 static void stype_literal(stype_t *stype, stree_literal_t *literal, 248 289 tdata_item_t **rtitem) … … 272 313 } 273 314 274 /** Type a self reference. */ 315 /** Type @c self reference. 316 * 317 * @param stype Static typing object 318 * @param self_ref @c self reference 319 * @param rtitem Place to store result type 320 */ 275 321 static void stype_self_ref(stype_t *stype, stree_self_ref_t *self_ref, 276 322 tdata_item_t **rtitem) … … 285 331 } 286 332 287 /** Type a binary operation. */ 333 /** Type a binary operation. 334 * 335 * @param stype Static typing object 336 * @param binop Binary operation 337 * @param rtitem Place to store result type 338 */ 288 339 static void stype_binop(stype_t *stype, stree_binop_t *binop, 289 340 tdata_item_t **rtitem) … … 345 396 } 346 397 347 /** Type a binary operation with arguments of primitive type. */ 398 /** Type a binary operation with arguments of primitive type. 399 * 400 * @param stype Static typing object 401 * @param binop Binary operation 402 * @param ta Type of first argument 403 * @param tb Type of second argument 404 * @param rtitem Place to store result type 405 */ 348 406 static void stype_binop_tprimitive(stype_t *stype, stree_binop_t *binop, 349 407 tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem) … … 374 432 } 375 433 376 /** Type a binary operation with bool arguments. */ 434 /** Type a binary operation with @c bool arguments. 435 * 436 * @param stype Static typing object 437 * @param binop Binary operation 438 * @param rtitem Place to store result type 439 */ 377 440 static void stype_binop_bool(stype_t *stype, stree_binop_t *binop, 378 441 tdata_item_t **rtitem) … … 408 471 } 409 472 410 /** Type a binary operation with char arguments. */ 473 /** Type a binary operation with @c char arguments. 474 * 475 * @param stype Static typing object 476 * @param binop Binary operation 477 * @param rtitem Place to store result type 478 */ 411 479 static void stype_binop_char(stype_t *stype, stree_binop_t *binop, 412 480 tdata_item_t **rtitem) … … 444 512 } 445 513 446 /** Type a binary operation with int arguments. */ 514 /** Type a binary operation with @c int arguments. 515 * 516 * @param stype Static typing object 517 * @param binop Binary operation 518 * @param rtitem Place to store result type 519 */ 447 520 static void stype_binop_int(stype_t *stype, stree_binop_t *binop, 448 521 tdata_item_t **rtitem) … … 477 550 } 478 551 479 /** Type a binary operation with nil arguments. */ 552 /** Type a binary operation with @c nil arguments. 553 * 554 * @param stype Static typing object 555 * @param binop Binary operation 556 * @param rtitem Place to store result type 557 */ 480 558 static void stype_binop_nil(stype_t *stype, stree_binop_t *binop, 481 559 tdata_item_t **rtitem) … … 488 566 } 489 567 490 /** Type a binary operation with string arguments. */ 568 /** Type a binary operation with @c string arguments. 569 * 570 * @param stype Static typing object 571 * @param binop Binary operation 572 * @param rtitem Place to store result type 573 */ 491 574 static void stype_binop_string(stype_t *stype, stree_binop_t *binop, 492 575 tdata_item_t **rtitem) … … 511 594 } 512 595 513 /** Type a binary operation with resource arguments. */ 596 /** Type a binary operation with resource arguments. 597 * 598 * @param stype Static typing object 599 * @param binop Binary operation 600 * @param rtitem Place to store result type 601 */ 514 602 static void stype_binop_resource(stype_t *stype, stree_binop_t *binop, 515 603 tdata_item_t **rtitem) … … 530 618 } 531 619 532 /** Type a binary operation with arguments of an object type. */ 620 /** Type a binary operation with arguments of an object type. 621 * 622 * @param stype Static typing object 623 * @param binop Binary operation 624 * @param ta Type of first argument 625 * @param tb Type of second argument 626 * @param rtitem Place to store result type 627 */ 533 628 static void stype_binop_tobject(stype_t *stype, stree_binop_t *binop, 534 629 tdata_item_t *ta, tdata_item_t *tb, tdata_item_t **rtitem) … … 561 656 562 657 563 /** Type a unary operation. */ 658 /** Type a unary operation. 659 * 660 * @param stype Static typing object 661 * @param unop Unary operation 662 * @param rtitem Place to store result type 663 */ 564 664 static void stype_unop(stype_t *stype, stree_unop_t *unop, 565 665 tdata_item_t **rtitem) … … 594 694 } 595 695 596 /** Type a binary operation arguments of primitive type. */ 696 /** Type a binary operation arguments of primitive type. 697 * 698 * @param stype Static typing object 699 * @param unop Binary operation 700 * @param ta Type of argument 701 * @param rtitem Place to store result type 702 */ 597 703 static void stype_unop_tprimitive(stype_t *stype, stree_unop_t *unop, 598 704 tdata_item_t *ta, tdata_item_t **rtitem) … … 627 733 } 628 734 629 /** Type a @c new operation. */ 735 /** Type a @c new operation. 736 * 737 * @param stype Static typing object 738 * @param new_op @c new operation 739 * @param rtitem Place to store result type 740 */ 630 741 static void stype_new(stype_t *stype, stree_new_t *new_op, 631 742 tdata_item_t **rtitem) … … 646 757 } 647 758 648 /** Type a field access operation */ 759 /** Type a member access operation. 760 * 761 * @param stype Static typing object 762 * @param access Member access operation 763 * @param rtitem Place to store result type 764 */ 649 765 static void stype_access(stype_t *stype, stree_access_t *access, 650 766 tdata_item_t **rtitem) … … 675 791 stype_access_tarray(stype, access, arg_ti, rtitem); 676 792 break; 793 case tic_tdeleg: 794 printf("Error: Using '.' operator on a function.\n"); 795 stype_note_error(stype); 796 *rtitem = stype_recovery_titem(stype); 797 break; 677 798 case tic_tfun: 678 printf("Error: Using '.' operator on a function.\n"); 679 stype_note_error(stype); 799 printf("Error: Using '.' operator on a delegate.\n"); 800 stype_note_error(stype); 801 *rtitem = stype_recovery_titem(stype); 802 break; 803 case tic_tvref: 804 /* Cannot allow this without some constraint. */ 805 printf("Error: Using '.' operator on generic data.\n"); 680 806 *rtitem = stype_recovery_titem(stype); 681 807 break; … … 686 812 } 687 813 688 /** Type a primitive type access operation. */ 814 /** Type a primitive type access operation. 815 * 816 * @param stype Static typing object 817 * @param access Member access operation 818 * @param arg_ti Base type 819 * @param rtitem Place to store result type 820 */ 689 821 static void stype_access_tprimitive(stype_t *stype, stree_access_t *access, 690 822 tdata_item_t *arg_ti, tdata_item_t **rtitem) … … 701 833 } 702 834 703 /** Type an object access operation. */ 835 /** Type an object access operation. 836 * 837 * @param stype Static typing object 838 * @param access Member access operation 839 * @param arg_ti Base type 840 * @param rtitem Place to store result type 841 */ 704 842 static void stype_access_tobject(stype_t *stype, stree_access_t *access, 705 843 tdata_item_t *arg_ti, tdata_item_t **rtitem) … … 710 848 stree_prop_t *prop; 711 849 tdata_object_t *tobject; 850 tdata_item_t *mtitem; 851 tdata_tvv_t *tvv; 712 852 713 853 #ifdef DEBUG_TYPE_TRACE … … 743 883 stype_note_error(stype); 744 884 *rtitem = stype_recovery_titem(stype); 745 break; 885 return; 886 case sc_deleg: 887 printf("Error: Accessing object member which is a " 888 "delegate.\n"); 889 stype_note_error(stype); 890 *rtitem = stype_recovery_titem(stype); 891 return; 746 892 case sc_fun: 747 893 fun = symbol_to_fun(member_sym); 748 894 assert(fun != NULL); 749 *rtitem = tdata_item_new(tic_tfun);750 (*rtitem)->u.tfun = tdata_fun_new();751 (*rtitem)->u.tfun->fun = fun;895 /* Type function header now */ 896 stype_fun_header(stype, fun); 897 mtitem = fun->titem; 752 898 break; 753 899 case sc_var: 754 900 var = symbol_to_var(member_sym); 755 901 assert(var != NULL); 756 /* XXX Memoize to avoid recomputing every time. */757 902 run_texpr(stype->program, member_sym->outer_csi, 758 var->type, rtitem);903 var->type, &mtitem); 759 904 break; 760 905 case sc_prop: 761 906 prop = symbol_to_prop(member_sym); 762 907 assert(prop != NULL); 763 /* XXX Memoize to avoid recomputing every time. */764 908 run_texpr(stype->program, member_sym->outer_csi, 765 prop->type, rtitem); 766 break; 767 } 768 } 769 770 /** Type an array access operation. */ 909 prop->type, &mtitem); 910 break; 911 } 912 913 /* 914 * Substitute type arguments in member titem. 915 * 916 * Since the CSI can be generic the actual type of the member 917 * is obtained by substituting our type arguments into the 918 * (generic) type of the member. 919 */ 920 921 stype_titem_to_tvv(stype, arg_ti, &tvv); 922 tdata_item_subst(mtitem, tvv, rtitem); 923 } 924 925 /** Type an array access operation. 926 * 927 * @param stype Static typing object 928 * @param access Member access operation 929 * @param arg_ti Base type 930 * @param rtitem Place to store result type 931 */ 771 932 static void stype_access_tarray(stype_t *stype, stree_access_t *access, 772 933 tdata_item_t *arg_ti, tdata_item_t **rtitem) … … 783 944 } 784 945 785 /** Type a call operation. */ 946 /** Type a call operation. 947 * 948 * @param stype Static typing object 949 * @param call Call operation 950 * @param rtitem Place to store result type 951 */ 786 952 static void stype_call(stype_t *stype, stree_call_t *call, 787 953 tdata_item_t **rtitem) 788 954 { 789 list_node_t *farg_n; 790 stree_proc_arg_t *farg; 955 list_node_t *fargt_n; 791 956 tdata_item_t *farg_ti; 792 957 tdata_item_t *varg_ti; … … 797 962 798 963 tdata_item_t *fun_ti; 799 stree_fun_t *fun; 800 stree_symbol_t *fun_sym; 964 tdata_fun_sig_t *tsig; 965 966 int cnt; 801 967 802 968 #ifdef DEBUG_TYPE_TRACE … … 807 973 808 974 /* Check type item class */ 809 810 975 fun_ti = call->fun->titem; 811 976 switch (fun_ti->tic) { 977 case tic_tdeleg: 978 tsig = stype_deleg_get_sig(stype, fun_ti->u.tdeleg); 979 assert(tsig != NULL); 980 break; 812 981 case tic_tfun: 813 /* The expected case */982 tsig = fun_ti->u.tfun->tsig; 814 983 break; 815 984 case tic_ignore: … … 826 995 } 827 996 828 fun = fun_ti->u.tfun->fun;829 fun_sym = fun_to_symbol(fun);830 831 997 /* Type and check the arguments. */ 832 farg _n = list_first(&fun->args);998 fargt_n = list_first(&tsig->arg_ti); 833 999 arg_n = list_first(&call->args); 834 while (farg_n != NULL && arg_n != NULL) { 835 farg = list_node_data(farg_n, stree_proc_arg_t *); 1000 1001 cnt = 0; 1002 while (fargt_n != NULL && arg_n != NULL) { 1003 farg_ti = list_node_data(fargt_n, tdata_item_t *); 836 1004 arg = list_node_data(arg_n, stree_expr_t *); 837 1005 stype_expr(stype, arg); 838 1006 839 1007 /* XXX Because of overloaded bultin WriteLine */ 840 if (farg ->type== NULL) {1008 if (farg_ti == NULL) { 841 1009 /* Skip the check */ 842 farg _n = list_next(&fun->args, farg_n);1010 fargt_n = list_next(&tsig->arg_ti, fargt_n); 843 1011 arg_n = list_next(&call->args, arg_n); 844 1012 continue; 845 1013 } 846 1014 847 /* XXX Memoize to avoid recomputing every time. */848 run_texpr(stype->program, fun_sym->outer_csi, farg->type,849 &farg_ti);850 851 1015 /* Convert expression to type of formal argument. */ 852 1016 carg = stype_convert(stype, arg, farg_ti); … … 855 1019 list_node_setdata(arg_n, carg); 856 1020 857 farg _n = list_next(&fun->args, farg_n);1021 fargt_n = list_next(&tsig->arg_ti, fargt_n); 858 1022 arg_n = list_next(&call->args, arg_n); 859 1023 } 860 1024 861 1025 /* Type and check variadic arguments. */ 862 if (fun->varg != NULL) { 863 /* XXX Memoize to avoid recomputing every time. */ 864 run_texpr(stype->program, fun_sym->outer_csi, fun->varg->type, 865 &farg_ti); 1026 if (tsig->varg_ti != NULL) { 1027 /* Obtain type of packed argument. */ 1028 farg_ti = tsig->varg_ti; 866 1029 867 1030 /* Get array element type */ … … 883 1046 } 884 1047 885 if (farg_n != NULL) { 886 printf("Error: Too few arguments to function '"); 887 symbol_print_fqn(fun_to_symbol(fun)); 888 printf("'.\n"); 1048 if (fargt_n != NULL) { 1049 printf("Error: Too few arguments to function.\n"); 889 1050 stype_note_error(stype); 890 1051 } 891 1052 892 1053 if (arg_n != NULL) { 893 printf("Error: Too many arguments to function '"); 894 symbol_print_fqn(fun_to_symbol(fun)); 895 printf("'.\n"); 896 stype_note_error(stype); 897 } 898 899 if (fun->rtype != NULL) { 900 /* XXX Memoize to avoid recomputing every time. */ 901 run_texpr(stype->program, fun_sym->outer_csi, fun->rtype, 902 rtitem); 1054 printf("Error: Too many arguments to function.\n"); 1055 stype_note_error(stype); 1056 } 1057 1058 if (tsig->rtype != NULL) { 1059 /* XXX Might be better to clone here. */ 1060 *rtitem = tsig->rtype; 903 1061 } else { 904 1062 *rtitem = NULL; … … 906 1064 } 907 1065 908 /** Type an indexing operation. */ 1066 /** Type an indexing operation. 1067 * 1068 * @param stype Static typing object 1069 * @param index Indexing operation 1070 * @param rtitem Place to store result type 1071 */ 909 1072 static void stype_index(stype_t *stype, stree_index_t *index, 910 1073 tdata_item_t **rtitem) … … 939 1102 stype_index_tarray(stype, index, base_ti, rtitem); 940 1103 break; 1104 case tic_tdeleg: 1105 printf("Error: Indexing a delegate.\n"); 1106 stype_note_error(stype); 1107 *rtitem = stype_recovery_titem(stype); 1108 break; 941 1109 case tic_tfun: 942 1110 printf("Error: Indexing a function.\n"); … … 944 1112 *rtitem = stype_recovery_titem(stype); 945 1113 break; 1114 case tic_tvref: 1115 /* Cannot allow this without some constraint. */ 1116 printf("Error: Indexing generic data.\n"); 1117 *rtitem = stype_recovery_titem(stype); 1118 break; 946 1119 case tic_ignore: 947 1120 *rtitem = stype_recovery_titem(stype); … … 950 1123 } 951 1124 952 /** Type a primitive indexing operation. */ 1125 /** Type a primitive indexing operation. 1126 * 1127 * @param stype Static typing object 1128 * @param index Indexing operation 1129 * @param base_ti Base type (primitive being indexed) 1130 * @param rtitem Place to store result type 1131 */ 953 1132 static void stype_index_tprimitive(stype_t *stype, stree_index_t *index, 954 1133 tdata_item_t *base_ti, tdata_item_t **rtitem) … … 977 1156 } 978 1157 979 /** Type an object indexing operation. */ 1158 /** Type an object indexing operation. 1159 * 1160 * @param stype Static typing object 1161 * @param index Indexing operation 1162 * @param base_ti Base type (object being indexed) 1163 * @param rtitem Place to store result type 1164 */ 980 1165 static void stype_index_tobject(stype_t *stype, stree_index_t *index, 981 1166 tdata_item_t *base_ti, tdata_item_t **rtitem) … … 985 1170 stree_prop_t *idx; 986 1171 stree_ident_t *idx_ident; 1172 tdata_item_t *mtitem; 1173 tdata_tvv_t *tvv; 987 1174 988 1175 (void) index; … … 1015 1202 1016 1203 /* XXX Memoize to avoid recomputing every time. */ 1017 run_texpr(stype->program, idx_sym->outer_csi, idx->type, rtitem); 1018 } 1019 1020 /** Type an array indexing operation. */ 1204 run_texpr(stype->program, idx_sym->outer_csi, idx->type, &mtitem); 1205 1206 /* 1207 * Substitute type arguments in member titem. 1208 * 1209 * Since the CSI can be generic the actual type of the member 1210 * is obtained by substituting our type arguments into the 1211 * (generic) type of the member. 1212 */ 1213 1214 stype_titem_to_tvv(stype, base_ti, &tvv); 1215 tdata_item_subst(mtitem, tvv, rtitem); 1216 } 1217 1218 /** Type an array indexing operation. 1219 * 1220 * @param stype Static typing object 1221 * @param index Indexing operation 1222 * @param base_ti Base type (array being indexed) 1223 * @param rtitem Place to store result type 1224 */ 1021 1225 static void stype_index_tarray(stype_t *stype, stree_index_t *index, 1022 1226 tdata_item_t *base_ti, tdata_item_t **rtitem) … … 1058 1262 } 1059 1263 1060 /** Type an assignment. */ 1264 /** Type an assignment. 1265 * 1266 * @param stype Static typing object 1267 * @param assign Assignment operation 1268 * @param rtitem Place to store result type 1269 */ 1061 1270 static void stype_assign(stype_t *stype, stree_assign_t *assign, 1062 1271 tdata_item_t **rtitem) … … 1077 1286 } 1078 1287 1079 /** Type @c as conversion. */ 1288 /** Type @c as conversion. 1289 * 1290 * @param stype Static typing object 1291 * @param as_op @c as conversion operation 1292 * @param rtitem Place to store result type 1293 */ 1080 1294 static void stype_as(stype_t *stype, stree_as_t *as_op, tdata_item_t **rtitem) 1081 1295 { … … 1100 1314 *rtitem = titem; 1101 1315 } 1316 1317 /** Type boxing operation. 1318 * 1319 * While there is no boxing operation on the first typing pass, we do want 1320 * to allow potential re-evaluation (with same results). 1321 * 1322 * @param stype Static typing object 1323 * @param box Boxing operation 1324 * @param rtitem Place to store result type 1325 */ 1326 static void stype_box(stype_t *stype, stree_box_t *box, tdata_item_t **rtitem) 1327 { 1328 tdata_item_t *ptitem, *btitem; 1329 tdata_object_t *tobject; 1330 stree_symbol_t *csi_sym; 1331 builtin_t *bi; 1332 1333 #ifdef DEBUG_TYPE_TRACE 1334 printf("Evaluate type of boxing operation.\n"); 1335 #endif 1336 bi = stype->program->builtin; 1337 1338 stype_expr(stype, box->arg); 1339 ptitem = box->arg->titem; 1340 1341 /* Make compiler happy. */ 1342 csi_sym = NULL; 1343 1344 assert(ptitem->tic == tic_tprimitive); 1345 switch (ptitem->u.tprimitive->tpc) { 1346 case tpc_bool: csi_sym = bi->boxed_bool; break; 1347 case tpc_char: csi_sym = bi->boxed_char; break; 1348 case tpc_int: csi_sym = bi->boxed_int; break; 1349 case tpc_nil: assert(b_false); 1350 case tpc_string: csi_sym = bi->boxed_string; break; 1351 case tpc_resource: assert(b_false); 1352 } 1353 1354 btitem = tdata_item_new(tic_tobject); 1355 tobject = tdata_object_new(); 1356 1357 btitem->u.tobject = tobject; 1358 tobject->static_ref = b_false; 1359 tobject->csi = symbol_to_csi(csi_sym); 1360 assert(tobject->csi != NULL); 1361 list_init(&tobject->targs); 1362 1363 *rtitem = btitem; 1364 }
Note:
See TracChangeset
for help on using the changeset viewer.