1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * vim9type.c: handling of types 12 */ 13 14 #define USING_FLOAT_STUFF 15 #include "vim.h" 16 17 #if defined(FEAT_EVAL) || defined(PROTO) 18 19 #ifdef VMS 20 # include <float.h> 21 #endif 22 23 /* 24 * Allocate memory for a type_T and add the pointer to type_gap, so that it can 25 * be easily freed later. 26 */ 27 type_T * 28 get_type_ptr(garray_T *type_gap) 29 { 30 type_T *type; 31 32 if (ga_grow(type_gap, 1) == FAIL) 33 return NULL; 34 type = ALLOC_CLEAR_ONE(type_T); 35 if (type != NULL) 36 { 37 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; 38 ++type_gap->ga_len; 39 } 40 return type; 41 } 42 43 void 44 clear_type_list(garray_T *gap) 45 { 46 while (gap->ga_len > 0) 47 vim_free(((type_T **)gap->ga_data)[--gap->ga_len]); 48 ga_clear(gap); 49 } 50 51 /* 52 * Take a type that is using entries in a growarray and turn it into a type 53 * with allocated entries. 54 */ 55 type_T * 56 alloc_type(type_T *type) 57 { 58 type_T *ret; 59 60 if (type == NULL) 61 return NULL; 62 63 // A fixed type never contains allocated types, return as-is. 64 if (type->tt_flags & TTFLAG_STATIC) 65 return type; 66 67 ret = ALLOC_ONE(type_T); 68 *ret = *type; 69 70 if (ret->tt_member != NULL) 71 ret->tt_member = alloc_type(ret->tt_member); 72 if (type->tt_args != NULL) 73 { 74 int i; 75 76 ret->tt_args = ALLOC_MULT(type_T *, type->tt_argcount); 77 if (ret->tt_args != NULL) 78 for (i = 0; i < type->tt_argcount; ++i) 79 ret->tt_args[i] = alloc_type(type->tt_args[i]); 80 } 81 82 return ret; 83 } 84 85 /* 86 * Free a type that was created with alloc_type(). 87 */ 88 void 89 free_type(type_T *type) 90 { 91 int i; 92 93 if (type == NULL || (type->tt_flags & TTFLAG_STATIC)) 94 return; 95 if (type->tt_args != NULL) 96 { 97 for (i = 0; i < type->tt_argcount; ++i) 98 free_type(type->tt_args[i]); 99 vim_free(type->tt_args); 100 } 101 free_type(type->tt_member); 102 vim_free(type); 103 } 104 105 type_T * 106 get_list_type(type_T *member_type, garray_T *type_gap) 107 { 108 type_T *type; 109 110 // recognize commonly used types 111 if (member_type == NULL || member_type->tt_type == VAR_ANY) 112 return &t_list_any; 113 if (member_type->tt_type == VAR_VOID 114 || member_type->tt_type == VAR_UNKNOWN) 115 return &t_list_empty; 116 if (member_type->tt_type == VAR_BOOL) 117 return &t_list_bool; 118 if (member_type->tt_type == VAR_NUMBER) 119 return &t_list_number; 120 if (member_type->tt_type == VAR_STRING) 121 return &t_list_string; 122 123 // Not a common type, create a new entry. 124 type = get_type_ptr(type_gap); 125 if (type == NULL) 126 return &t_any; 127 type->tt_type = VAR_LIST; 128 type->tt_member = member_type; 129 type->tt_argcount = 0; 130 type->tt_args = NULL; 131 return type; 132 } 133 134 type_T * 135 get_dict_type(type_T *member_type, garray_T *type_gap) 136 { 137 type_T *type; 138 139 // recognize commonly used types 140 if (member_type == NULL || member_type->tt_type == VAR_ANY) 141 return &t_dict_any; 142 if (member_type->tt_type == VAR_VOID 143 || member_type->tt_type == VAR_UNKNOWN) 144 return &t_dict_empty; 145 if (member_type->tt_type == VAR_BOOL) 146 return &t_dict_bool; 147 if (member_type->tt_type == VAR_NUMBER) 148 return &t_dict_number; 149 if (member_type->tt_type == VAR_STRING) 150 return &t_dict_string; 151 152 // Not a common type, create a new entry. 153 type = get_type_ptr(type_gap); 154 if (type == NULL) 155 return &t_any; 156 type->tt_type = VAR_DICT; 157 type->tt_member = member_type; 158 type->tt_argcount = 0; 159 type->tt_args = NULL; 160 return type; 161 } 162 163 /* 164 * Allocate a new type for a function. 165 */ 166 type_T * 167 alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) 168 { 169 type_T *type = get_type_ptr(type_gap); 170 171 if (type == NULL) 172 return &t_any; 173 type->tt_type = VAR_FUNC; 174 type->tt_member = ret_type; 175 type->tt_argcount = argcount; 176 type->tt_args = NULL; 177 return type; 178 } 179 180 /* 181 * Get a function type, based on the return type "ret_type". 182 * If "argcount" is -1 or 0 a predefined type can be used. 183 * If "argcount" > 0 always create a new type, so that arguments can be added. 184 */ 185 type_T * 186 get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) 187 { 188 // recognize commonly used types 189 if (argcount <= 0) 190 { 191 if (ret_type == &t_unknown) 192 { 193 // (argcount == 0) is not possible 194 return &t_func_unknown; 195 } 196 if (ret_type == &t_void) 197 { 198 if (argcount == 0) 199 return &t_func_0_void; 200 else 201 return &t_func_void; 202 } 203 if (ret_type == &t_any) 204 { 205 if (argcount == 0) 206 return &t_func_0_any; 207 else 208 return &t_func_any; 209 } 210 if (ret_type == &t_number) 211 { 212 if (argcount == 0) 213 return &t_func_0_number; 214 else 215 return &t_func_number; 216 } 217 if (ret_type == &t_string) 218 { 219 if (argcount == 0) 220 return &t_func_0_string; 221 else 222 return &t_func_string; 223 } 224 } 225 226 return alloc_func_type(ret_type, argcount, type_gap); 227 } 228 229 /* 230 * For a function type, reserve space for "argcount" argument types (including 231 * vararg). 232 */ 233 int 234 func_type_add_arg_types( 235 type_T *functype, 236 int argcount, 237 garray_T *type_gap) 238 { 239 // To make it easy to free the space needed for the argument types, add the 240 // pointer to type_gap. 241 if (ga_grow(type_gap, 1) == FAIL) 242 return FAIL; 243 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); 244 if (functype->tt_args == NULL) 245 return FAIL; 246 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = 247 (void *)functype->tt_args; 248 ++type_gap->ga_len; 249 return OK; 250 } 251 252 /* 253 * Get a type_T for a typval_T. 254 * "type_gap" is used to temporarily create types in. 255 */ 256 static type_T * 257 typval2type_int(typval_T *tv, garray_T *type_gap) 258 { 259 type_T *type; 260 type_T *member_type = &t_any; 261 int argcount = 0; 262 263 if (tv->v_type == VAR_NUMBER) 264 return &t_number; 265 if (tv->v_type == VAR_BOOL) 266 return &t_bool; 267 if (tv->v_type == VAR_STRING) 268 return &t_string; 269 270 if (tv->v_type == VAR_LIST) 271 { 272 list_T *l = tv->vval.v_list; 273 listitem_T *li; 274 275 if (l == NULL || l->lv_first == NULL) 276 return &t_list_empty; 277 if (l->lv_first == &range_list_item) 278 return &t_list_number; 279 280 // Use the common type of all members. 281 member_type = typval2type(&l->lv_first->li_tv, type_gap); 282 for (li = l->lv_first->li_next; li != NULL; li = li->li_next) 283 common_type(typval2type(&li->li_tv, type_gap), 284 member_type, &member_type, type_gap); 285 return get_list_type(member_type, type_gap); 286 } 287 288 if (tv->v_type == VAR_DICT) 289 { 290 dict_iterator_T iter; 291 typval_T *value; 292 293 if (tv->vval.v_dict == NULL 294 || tv->vval.v_dict->dv_hashtab.ht_used == 0) 295 return &t_dict_empty; 296 297 // Use the common type of all values. 298 dict_iterate_start(tv, &iter); 299 dict_iterate_next(&iter, &value); 300 member_type = typval2type(value, type_gap); 301 while (dict_iterate_next(&iter, &value) != NULL) 302 common_type(typval2type(value, type_gap), 303 member_type, &member_type, type_gap); 304 return get_dict_type(member_type, type_gap); 305 } 306 307 if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL) 308 { 309 char_u *name = NULL; 310 ufunc_T *ufunc = NULL; 311 312 if (tv->v_type == VAR_PARTIAL) 313 { 314 if (tv->vval.v_partial->pt_func != NULL) 315 ufunc = tv->vval.v_partial->pt_func; 316 else 317 name = tv->vval.v_partial->pt_name; 318 } 319 else 320 name = tv->vval.v_string; 321 if (name != NULL) 322 { 323 int idx = find_internal_func(name); 324 325 if (idx >= 0) 326 { 327 // TODO: get actual arg count and types 328 argcount = -1; 329 member_type = internal_func_ret_type(idx, 0, NULL); 330 } 331 else 332 ufunc = find_func(name, FALSE, NULL); 333 } 334 if (ufunc != NULL) 335 { 336 // May need to get the argument types from default values by 337 // compiling the function. 338 if (ufunc->uf_def_status == UF_TO_BE_COMPILED 339 && compile_def_function(ufunc, TRUE, NULL) == FAIL) 340 return NULL; 341 if (ufunc->uf_func_type == NULL) 342 set_function_type(ufunc); 343 if (ufunc->uf_func_type != NULL) 344 return ufunc->uf_func_type; 345 } 346 } 347 348 type = get_type_ptr(type_gap); 349 if (type == NULL) 350 return NULL; 351 type->tt_type = tv->v_type; 352 type->tt_argcount = argcount; 353 type->tt_member = member_type; 354 355 return type; 356 } 357 358 /* 359 * Return TRUE if "tv" is not a bool but should be converted to bool. 360 */ 361 int 362 need_convert_to_bool(type_T *type, typval_T *tv) 363 { 364 return type != NULL && type == &t_bool && tv->v_type != VAR_BOOL 365 && (tv->v_type == VAR_NUMBER 366 && (tv->vval.v_number == 0 || tv->vval.v_number == 1)); 367 } 368 369 /* 370 * Get a type_T for a typval_T. 371 * "type_list" is used to temporarily create types in. 372 */ 373 type_T * 374 typval2type(typval_T *tv, garray_T *type_gap) 375 { 376 type_T *type = typval2type_int(tv, type_gap); 377 378 if (type != NULL && type != &t_bool 379 && (tv->v_type == VAR_NUMBER 380 && (tv->vval.v_number == 0 || tv->vval.v_number == 1))) 381 // Number 0 and 1 and expression with "&&" or "||" can also be used for 382 // bool. 383 type = &t_number_bool; 384 return type; 385 } 386 387 /* 388 * Get a type_T for a typval_T, used for v: variables. 389 * "type_list" is used to temporarily create types in. 390 */ 391 type_T * 392 typval2type_vimvar(typval_T *tv, garray_T *type_gap) 393 { 394 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles 395 return &t_list_string; 396 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item 397 return &t_dict_any; 398 return typval2type(tv, type_gap); 399 } 400 401 402 /* 403 * Return FAIL if "expected" and "actual" don't match. 404 * When "argidx" > 0 it is included in the error message. 405 */ 406 int 407 check_typval_type(type_T *expected, typval_T *actual_tv, int argidx) 408 { 409 garray_T type_list; 410 type_T *actual_type; 411 int res = FAIL; 412 413 ga_init2(&type_list, sizeof(type_T *), 10); 414 actual_type = typval2type(actual_tv, &type_list); 415 if (actual_type != NULL) 416 res = check_type(expected, actual_type, TRUE, argidx); 417 clear_type_list(&type_list); 418 return res; 419 } 420 421 void 422 type_mismatch(type_T *expected, type_T *actual) 423 { 424 arg_type_mismatch(expected, actual, 0); 425 } 426 427 void 428 arg_type_mismatch(type_T *expected, type_T *actual, int argidx) 429 { 430 char *tofree1, *tofree2; 431 char *typename1 = type_name(expected, &tofree1); 432 char *typename2 = type_name(actual, &tofree2); 433 434 if (argidx > 0) 435 semsg(_(e_argument_nr_type_mismatch_expected_str_but_got_str), 436 argidx, typename1, typename2); 437 else 438 semsg(_(e_type_mismatch_expected_str_but_got_str), 439 typename1, typename2); 440 vim_free(tofree1); 441 vim_free(tofree2); 442 } 443 444 /* 445 * Check if the expected and actual types match. 446 * Does not allow for assigning "any" to a specific type. 447 * When "argidx" > 0 it is included in the error message. 448 */ 449 int 450 check_type(type_T *expected, type_T *actual, int give_msg, int argidx) 451 { 452 int ret = OK; 453 454 // When expected is "unknown" we accept any actual type. 455 // When expected is "any" we accept any actual type except "void". 456 if (expected->tt_type != VAR_UNKNOWN 457 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) 458 459 { 460 // tt_type should match, except that a "partial" can be assigned to a 461 // variable with type "func". 462 if (!(expected->tt_type == actual->tt_type 463 || (expected->tt_type == VAR_FUNC 464 && actual->tt_type == VAR_PARTIAL))) 465 { 466 if (expected->tt_type == VAR_BOOL 467 && (actual->tt_flags & TTFLAG_BOOL_OK)) 468 // Using number 0 or 1 for bool is OK. 469 return OK; 470 if (give_msg) 471 arg_type_mismatch(expected, actual, argidx); 472 return FAIL; 473 } 474 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) 475 { 476 // "unknown" is used for an empty list or dict 477 if (actual->tt_member != &t_unknown) 478 ret = check_type(expected->tt_member, actual->tt_member, 479 FALSE, 0); 480 } 481 else if (expected->tt_type == VAR_FUNC) 482 { 483 // If the return type is unknown it can be anything, including 484 // nothing, thus there is no point in checking. 485 if (expected->tt_member != &t_unknown 486 && actual->tt_member != &t_unknown) 487 ret = check_type(expected->tt_member, actual->tt_member, 488 FALSE, 0); 489 if (ret == OK && expected->tt_argcount != -1 490 && actual->tt_argcount != -1 491 && (actual->tt_argcount < expected->tt_min_argcount 492 || actual->tt_argcount > expected->tt_argcount)) 493 ret = FAIL; 494 if (expected->tt_args != NULL && actual->tt_args != NULL) 495 { 496 int i; 497 498 for (i = 0; i < expected->tt_argcount; ++i) 499 // Allow for using "any" argument type, lambda's have them. 500 if (actual->tt_args[i] != &t_any && check_type( 501 expected->tt_args[i], actual->tt_args[i], FALSE, 0) 502 == FAIL) 503 { 504 ret = FAIL; 505 break; 506 } 507 } 508 } 509 if (ret == FAIL && give_msg) 510 arg_type_mismatch(expected, actual, argidx); 511 } 512 return ret; 513 } 514 515 /* 516 * Like check_type() but also allow for a runtime type check. E.g. "any" can be 517 * used for "number". 518 */ 519 int 520 check_arg_type(type_T *expected, type_T *actual, int argidx) 521 { 522 if (check_type(expected, actual, FALSE, 0) == OK 523 || use_typecheck(actual, expected)) 524 return OK; 525 // TODO: should generate a TYPECHECK instruction. 526 return check_type(expected, actual, TRUE, argidx); 527 } 528 529 /* 530 * Skip over a type definition and return a pointer to just after it. 531 * When "optional" is TRUE then a leading "?" is accepted. 532 */ 533 char_u * 534 skip_type(char_u *start, int optional) 535 { 536 char_u *p = start; 537 538 if (optional && *p == '?') 539 ++p; 540 while (ASCII_ISALNUM(*p) || *p == '_') 541 ++p; 542 543 // Skip over "<type>"; this is permissive about white space. 544 if (*skipwhite(p) == '<') 545 { 546 p = skipwhite(p); 547 p = skip_type(skipwhite(p + 1), FALSE); 548 p = skipwhite(p); 549 if (*p == '>') 550 ++p; 551 } 552 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1]))) 553 && STRNCMP("func", start, 4) == 0) 554 { 555 if (*p == '(') 556 { 557 // handle func(args): type 558 ++p; 559 while (*p != ')' && *p != NUL) 560 { 561 char_u *sp = p; 562 563 if (STRNCMP(p, "...", 3) == 0) 564 p += 3; 565 p = skip_type(p, TRUE); 566 if (p == sp) 567 return p; // syntax error 568 if (*p == ',') 569 p = skipwhite(p + 1); 570 } 571 if (*p == ')') 572 { 573 if (p[1] == ':') 574 p = skip_type(skipwhite(p + 2), FALSE); 575 else 576 ++p; 577 } 578 } 579 else 580 { 581 // handle func: return_type 582 p = skip_type(skipwhite(p + 1), FALSE); 583 } 584 } 585 586 return p; 587 } 588 589 /* 590 * Parse the member type: "<type>" and return "type" with the member set. 591 * Use "type_gap" if a new type needs to be added. 592 * Returns NULL in case of failure. 593 */ 594 static type_T * 595 parse_type_member( 596 char_u **arg, 597 type_T *type, 598 garray_T *type_gap, 599 int give_error) 600 { 601 type_T *member_type; 602 int prev_called_emsg = called_emsg; 603 604 if (**arg != '<') 605 { 606 if (give_error) 607 { 608 if (*skipwhite(*arg) == '<') 609 semsg(_(e_no_white_space_allowed_before_str), "<"); 610 else 611 emsg(_(e_missing_type)); 612 } 613 return NULL; 614 } 615 *arg = skipwhite(*arg + 1); 616 617 member_type = parse_type(arg, type_gap, give_error); 618 if (member_type == NULL) 619 return NULL; 620 621 *arg = skipwhite(*arg); 622 if (**arg != '>' && called_emsg == prev_called_emsg) 623 { 624 if (give_error) 625 emsg(_(e_missing_gt_after_type)); 626 return NULL; 627 } 628 ++*arg; 629 630 if (type->tt_type == VAR_LIST) 631 return get_list_type(member_type, type_gap); 632 return get_dict_type(member_type, type_gap); 633 } 634 635 /* 636 * Parse a type at "arg" and advance over it. 637 * When "give_error" is TRUE give error messages, otherwise be quiet. 638 * Return NULL for failure. 639 */ 640 type_T * 641 parse_type(char_u **arg, garray_T *type_gap, int give_error) 642 { 643 char_u *p = *arg; 644 size_t len; 645 646 // skip over the first word 647 while (ASCII_ISALNUM(*p) || *p == '_') 648 ++p; 649 len = p - *arg; 650 651 switch (**arg) 652 { 653 case 'a': 654 if (len == 3 && STRNCMP(*arg, "any", len) == 0) 655 { 656 *arg += len; 657 return &t_any; 658 } 659 break; 660 case 'b': 661 if (len == 4 && STRNCMP(*arg, "bool", len) == 0) 662 { 663 *arg += len; 664 return &t_bool; 665 } 666 if (len == 4 && STRNCMP(*arg, "blob", len) == 0) 667 { 668 *arg += len; 669 return &t_blob; 670 } 671 break; 672 case 'c': 673 if (len == 7 && STRNCMP(*arg, "channel", len) == 0) 674 { 675 *arg += len; 676 return &t_channel; 677 } 678 break; 679 case 'd': 680 if (len == 4 && STRNCMP(*arg, "dict", len) == 0) 681 { 682 *arg += len; 683 return parse_type_member(arg, &t_dict_any, 684 type_gap, give_error); 685 } 686 break; 687 case 'f': 688 if (len == 5 && STRNCMP(*arg, "float", len) == 0) 689 { 690 #ifdef FEAT_FLOAT 691 *arg += len; 692 return &t_float; 693 #else 694 if (give_error) 695 emsg(_(e_this_vim_is_not_compiled_with_float_support)); 696 return NULL; 697 #endif 698 } 699 if (len == 4 && STRNCMP(*arg, "func", len) == 0) 700 { 701 type_T *type; 702 type_T *ret_type = &t_unknown; 703 int argcount = -1; 704 int flags = 0; 705 int first_optional = -1; 706 type_T *arg_type[MAX_FUNC_ARGS + 1]; 707 708 // func({type}, ...{type}): {type} 709 *arg += len; 710 if (**arg == '(') 711 { 712 // "func" may or may not return a value, "func()" does 713 // not return a value. 714 ret_type = &t_void; 715 716 p = ++*arg; 717 argcount = 0; 718 while (*p != NUL && *p != ')') 719 { 720 if (*p == '?') 721 { 722 if (first_optional == -1) 723 first_optional = argcount; 724 ++p; 725 } 726 else if (STRNCMP(p, "...", 3) == 0) 727 { 728 flags |= TTFLAG_VARARGS; 729 p += 3; 730 } 731 else if (first_optional != -1) 732 { 733 if (give_error) 734 emsg(_(e_mandatory_argument_after_optional_argument)); 735 return NULL; 736 } 737 738 type = parse_type(&p, type_gap, give_error); 739 if (type == NULL) 740 return NULL; 741 arg_type[argcount++] = type; 742 743 // Nothing comes after "...{type}". 744 if (flags & TTFLAG_VARARGS) 745 break; 746 747 if (*p != ',' && *skipwhite(p) == ',') 748 { 749 if (give_error) 750 semsg(_(e_no_white_space_allowed_before_str), ","); 751 return NULL; 752 } 753 if (*p == ',') 754 { 755 ++p; 756 if (!VIM_ISWHITE(*p)) 757 { 758 if (give_error) 759 semsg(_(e_white_space_required_after_str), ","); 760 return NULL; 761 } 762 } 763 p = skipwhite(p); 764 if (argcount == MAX_FUNC_ARGS) 765 { 766 if (give_error) 767 emsg(_(e_too_many_argument_types)); 768 return NULL; 769 } 770 } 771 772 p = skipwhite(p); 773 if (*p != ')') 774 { 775 if (give_error) 776 emsg(_(e_missing_close)); 777 return NULL; 778 } 779 *arg = p + 1; 780 } 781 if (**arg == ':') 782 { 783 // parse return type 784 ++*arg; 785 if (!VIM_ISWHITE(**arg) && give_error) 786 semsg(_(e_white_space_required_after_str), ":"); 787 *arg = skipwhite(*arg); 788 ret_type = parse_type(arg, type_gap, give_error); 789 if (ret_type == NULL) 790 return NULL; 791 } 792 if (flags == 0 && first_optional == -1 && argcount <= 0) 793 type = get_func_type(ret_type, argcount, type_gap); 794 else 795 { 796 type = alloc_func_type(ret_type, argcount, type_gap); 797 type->tt_flags = flags; 798 if (argcount > 0) 799 { 800 type->tt_argcount = argcount; 801 type->tt_min_argcount = first_optional == -1 802 ? argcount : first_optional; 803 if (func_type_add_arg_types(type, argcount, 804 type_gap) == FAIL) 805 return NULL; 806 mch_memmove(type->tt_args, arg_type, 807 sizeof(type_T *) * argcount); 808 } 809 } 810 return type; 811 } 812 break; 813 case 'j': 814 if (len == 3 && STRNCMP(*arg, "job", len) == 0) 815 { 816 *arg += len; 817 return &t_job; 818 } 819 break; 820 case 'l': 821 if (len == 4 && STRNCMP(*arg, "list", len) == 0) 822 { 823 *arg += len; 824 return parse_type_member(arg, &t_list_any, 825 type_gap, give_error); 826 } 827 break; 828 case 'n': 829 if (len == 6 && STRNCMP(*arg, "number", len) == 0) 830 { 831 *arg += len; 832 return &t_number; 833 } 834 break; 835 case 's': 836 if (len == 6 && STRNCMP(*arg, "string", len) == 0) 837 { 838 *arg += len; 839 return &t_string; 840 } 841 break; 842 case 'v': 843 if (len == 4 && STRNCMP(*arg, "void", len) == 0) 844 { 845 *arg += len; 846 return &t_void; 847 } 848 break; 849 } 850 851 if (give_error) 852 semsg(_(e_type_not_recognized_str), *arg); 853 return NULL; 854 } 855 856 /* 857 * Check if "type1" and "type2" are exactly the same. 858 */ 859 int 860 equal_type(type_T *type1, type_T *type2) 861 { 862 int i; 863 864 if (type1->tt_type != type2->tt_type) 865 return FALSE; 866 switch (type1->tt_type) 867 { 868 case VAR_UNKNOWN: 869 case VAR_ANY: 870 case VAR_VOID: 871 case VAR_SPECIAL: 872 case VAR_BOOL: 873 case VAR_NUMBER: 874 case VAR_FLOAT: 875 case VAR_STRING: 876 case VAR_BLOB: 877 case VAR_JOB: 878 case VAR_CHANNEL: 879 break; // not composite is always OK 880 case VAR_LIST: 881 case VAR_DICT: 882 return equal_type(type1->tt_member, type2->tt_member); 883 case VAR_FUNC: 884 case VAR_PARTIAL: 885 if (!equal_type(type1->tt_member, type2->tt_member) 886 || type1->tt_argcount != type2->tt_argcount) 887 return FALSE; 888 if (type1->tt_argcount < 0 889 || type1->tt_args == NULL || type2->tt_args == NULL) 890 return TRUE; 891 for (i = 0; i < type1->tt_argcount; ++i) 892 if (!equal_type(type1->tt_args[i], type2->tt_args[i])) 893 return FALSE; 894 return TRUE; 895 } 896 return TRUE; 897 } 898 899 /* 900 * Find the common type of "type1" and "type2" and put it in "dest". 901 * "type2" and "dest" may be the same. 902 */ 903 void 904 common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap) 905 { 906 if (equal_type(type1, type2)) 907 { 908 *dest = type1; 909 return; 910 } 911 912 // If either is VAR_UNKNOWN use the other type. An empty list/dict has no 913 // specific type. 914 if (type1->tt_type == VAR_UNKNOWN) 915 { 916 *dest = type2; 917 return; 918 } 919 if (type2->tt_type == VAR_UNKNOWN) 920 { 921 *dest = type1; 922 return; 923 } 924 925 if (type1->tt_type == type2->tt_type) 926 { 927 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) 928 { 929 type_T *common; 930 931 common_type(type1->tt_member, type2->tt_member, &common, type_gap); 932 if (type1->tt_type == VAR_LIST) 933 *dest = get_list_type(common, type_gap); 934 else 935 *dest = get_dict_type(common, type_gap); 936 return; 937 } 938 if (type1->tt_type == VAR_FUNC) 939 { 940 type_T *common; 941 942 common_type(type1->tt_member, type2->tt_member, &common, type_gap); 943 if (type1->tt_argcount == type2->tt_argcount 944 && type1->tt_argcount >= 0) 945 { 946 int argcount = type1->tt_argcount; 947 int i; 948 949 *dest = alloc_func_type(common, argcount, type_gap); 950 if (type1->tt_args != NULL && type2->tt_args != NULL) 951 { 952 if (func_type_add_arg_types(*dest, argcount, 953 type_gap) == OK) 954 for (i = 0; i < argcount; ++i) 955 common_type(type1->tt_args[i], type2->tt_args[i], 956 &(*dest)->tt_args[i], type_gap); 957 } 958 } 959 else 960 *dest = alloc_func_type(common, -1, type_gap); 961 // Use the minimum of min_argcount. 962 (*dest)->tt_min_argcount = 963 type1->tt_min_argcount < type2->tt_min_argcount 964 ? type1->tt_min_argcount : type2->tt_min_argcount; 965 return; 966 } 967 } 968 969 *dest = &t_any; 970 } 971 972 /* 973 * Get the member type of a dict or list from the items on the stack. 974 * "stack_top" points just after the last type on the type stack. 975 * For a list "skip" is 1, for a dict "skip" is 2, keys are skipped. 976 * Returns &t_void for an empty list or dict. 977 * Otherwise finds the common type of all items. 978 */ 979 type_T * 980 get_member_type_from_stack( 981 type_T **stack_top, 982 int count, 983 int skip, 984 garray_T *type_gap) 985 { 986 int i; 987 type_T *result; 988 type_T *type; 989 990 // Use "any" for an empty list or dict. 991 if (count == 0) 992 return &t_unknown; 993 994 // Use the first value type for the list member type, then find the common 995 // type from following items. 996 result = *(stack_top -(count * skip) + skip - 1); 997 for (i = 1; i < count; ++i) 998 { 999 if (result == &t_any) 1000 break; // won't get more common 1001 type = *(stack_top -((count - i) * skip) + skip - 1); 1002 common_type(type, result, &result, type_gap); 1003 } 1004 1005 return result; 1006 } 1007 1008 char * 1009 vartype_name(vartype_T type) 1010 { 1011 switch (type) 1012 { 1013 case VAR_UNKNOWN: break; 1014 case VAR_ANY: return "any"; 1015 case VAR_VOID: return "void"; 1016 case VAR_SPECIAL: return "special"; 1017 case VAR_BOOL: return "bool"; 1018 case VAR_NUMBER: return "number"; 1019 case VAR_FLOAT: return "float"; 1020 case VAR_STRING: return "string"; 1021 case VAR_BLOB: return "blob"; 1022 case VAR_JOB: return "job"; 1023 case VAR_CHANNEL: return "channel"; 1024 case VAR_LIST: return "list"; 1025 case VAR_DICT: return "dict"; 1026 1027 case VAR_FUNC: 1028 case VAR_PARTIAL: return "func"; 1029 } 1030 return "unknown"; 1031 } 1032 1033 /* 1034 * Return the name of a type. 1035 * The result may be in allocated memory, in which case "tofree" is set. 1036 */ 1037 char * 1038 type_name(type_T *type, char **tofree) 1039 { 1040 char *name; 1041 1042 *tofree = NULL; 1043 if (type == NULL) 1044 return "[unknown]"; 1045 name = vartype_name(type->tt_type); 1046 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) 1047 { 1048 char *member_free; 1049 char *member_name = type_name(type->tt_member, &member_free); 1050 size_t len; 1051 1052 len = STRLEN(name) + STRLEN(member_name) + 3; 1053 *tofree = alloc(len); 1054 if (*tofree != NULL) 1055 { 1056 vim_snprintf(*tofree, len, "%s<%s>", name, member_name); 1057 vim_free(member_free); 1058 return *tofree; 1059 } 1060 } 1061 if (type->tt_type == VAR_FUNC) 1062 { 1063 garray_T ga; 1064 int i; 1065 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; 1066 1067 ga_init2(&ga, 1, 100); 1068 if (ga_grow(&ga, 20) == FAIL) 1069 return "[unknown]"; 1070 STRCPY(ga.ga_data, "func("); 1071 ga.ga_len += 5; 1072 1073 for (i = 0; i < type->tt_argcount; ++i) 1074 { 1075 char *arg_free; 1076 char *arg_type; 1077 int len; 1078 1079 if (type->tt_args == NULL) 1080 arg_type = "[unknown]"; 1081 else 1082 arg_type = type_name(type->tt_args[i], &arg_free); 1083 if (i > 0) 1084 { 1085 STRCPY((char *)ga.ga_data + ga.ga_len, ", "); 1086 ga.ga_len += 2; 1087 } 1088 len = (int)STRLEN(arg_type); 1089 if (ga_grow(&ga, len + 8) == FAIL) 1090 { 1091 vim_free(arg_free); 1092 ga_clear(&ga); 1093 return "[unknown]"; 1094 } 1095 if (varargs && i == type->tt_argcount - 1) 1096 ga_concat(&ga, (char_u *)"..."); 1097 else if (i >= type->tt_min_argcount) 1098 *((char *)ga.ga_data + ga.ga_len++) = '?'; 1099 ga_concat(&ga, (char_u *)arg_type); 1100 vim_free(arg_free); 1101 } 1102 if (type->tt_argcount < 0) 1103 // any number of arguments 1104 ga_concat(&ga, (char_u *)"..."); 1105 1106 if (type->tt_member == &t_void) 1107 STRCPY((char *)ga.ga_data + ga.ga_len, ")"); 1108 else 1109 { 1110 char *ret_free; 1111 char *ret_name = type_name(type->tt_member, &ret_free); 1112 int len; 1113 1114 len = (int)STRLEN(ret_name) + 4; 1115 if (ga_grow(&ga, len) == FAIL) 1116 { 1117 vim_free(ret_free); 1118 ga_clear(&ga); 1119 return "[unknown]"; 1120 } 1121 STRCPY((char *)ga.ga_data + ga.ga_len, "): "); 1122 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); 1123 vim_free(ret_free); 1124 } 1125 *tofree = ga.ga_data; 1126 return ga.ga_data; 1127 } 1128 1129 return name; 1130 } 1131 1132 #endif // FEAT_EVAL 1133