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 (ret == OK && expected->tt_args != NULL 495 && actual->tt_args != NULL) 496 { 497 int i; 498 499 for (i = 0; i < expected->tt_argcount; ++i) 500 // Allow for using "any" argument type, lambda's have them. 501 if (actual->tt_args[i] != &t_any && check_type( 502 expected->tt_args[i], actual->tt_args[i], FALSE, 0) 503 == FAIL) 504 { 505 ret = FAIL; 506 break; 507 } 508 } 509 } 510 if (ret == FAIL && give_msg) 511 arg_type_mismatch(expected, actual, argidx); 512 } 513 return ret; 514 } 515 516 /* 517 * Check that the arguments of "type" match "argvars[argcount]". 518 * Return OK/FAIL. 519 */ 520 int 521 check_argument_types( 522 type_T *type, 523 typval_T *argvars, 524 int argcount, 525 char_u *name) 526 { 527 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; 528 int i; 529 530 if (type->tt_type != VAR_FUNC && type->tt_type != VAR_PARTIAL) 531 return OK; // just in case 532 if (argcount < type->tt_min_argcount - varargs) 533 { 534 semsg(_(e_toofewarg), name); 535 return FAIL; 536 } 537 if (!varargs && type->tt_argcount >= 0 && argcount > type->tt_argcount) 538 { 539 semsg(_(e_toomanyarg), name); 540 return FAIL; 541 } 542 if (type->tt_args == NULL) 543 return OK; // cannot check 544 545 546 for (i = 0; i < argcount; ++i) 547 { 548 type_T *expected; 549 550 if (varargs && i >= type->tt_argcount - 1) 551 expected = type->tt_args[type->tt_argcount - 1]->tt_member; 552 else 553 expected = type->tt_args[i]; 554 if (check_typval_type(expected, &argvars[i], i + 1) == FAIL) 555 return FAIL; 556 } 557 return OK; 558 } 559 560 /* 561 * Skip over a type definition and return a pointer to just after it. 562 * When "optional" is TRUE then a leading "?" is accepted. 563 */ 564 char_u * 565 skip_type(char_u *start, int optional) 566 { 567 char_u *p = start; 568 569 if (optional && *p == '?') 570 ++p; 571 while (ASCII_ISALNUM(*p) || *p == '_') 572 ++p; 573 574 // Skip over "<type>"; this is permissive about white space. 575 if (*skipwhite(p) == '<') 576 { 577 p = skipwhite(p); 578 p = skip_type(skipwhite(p + 1), FALSE); 579 p = skipwhite(p); 580 if (*p == '>') 581 ++p; 582 } 583 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1]))) 584 && STRNCMP("func", start, 4) == 0) 585 { 586 if (*p == '(') 587 { 588 // handle func(args): type 589 ++p; 590 while (*p != ')' && *p != NUL) 591 { 592 char_u *sp = p; 593 594 if (STRNCMP(p, "...", 3) == 0) 595 p += 3; 596 p = skip_type(p, TRUE); 597 if (p == sp) 598 return p; // syntax error 599 if (*p == ',') 600 p = skipwhite(p + 1); 601 } 602 if (*p == ')') 603 { 604 if (p[1] == ':') 605 p = skip_type(skipwhite(p + 2), FALSE); 606 else 607 ++p; 608 } 609 } 610 else 611 { 612 // handle func: return_type 613 p = skip_type(skipwhite(p + 1), FALSE); 614 } 615 } 616 617 return p; 618 } 619 620 /* 621 * Parse the member type: "<type>" and return "type" with the member set. 622 * Use "type_gap" if a new type needs to be added. 623 * Returns NULL in case of failure. 624 */ 625 static type_T * 626 parse_type_member( 627 char_u **arg, 628 type_T *type, 629 garray_T *type_gap, 630 int give_error) 631 { 632 type_T *member_type; 633 int prev_called_emsg = called_emsg; 634 635 if (**arg != '<') 636 { 637 if (give_error) 638 { 639 if (*skipwhite(*arg) == '<') 640 semsg(_(e_no_white_space_allowed_before_str), "<"); 641 else 642 emsg(_(e_missing_type)); 643 } 644 return NULL; 645 } 646 *arg = skipwhite(*arg + 1); 647 648 member_type = parse_type(arg, type_gap, give_error); 649 if (member_type == NULL) 650 return NULL; 651 652 *arg = skipwhite(*arg); 653 if (**arg != '>' && called_emsg == prev_called_emsg) 654 { 655 if (give_error) 656 emsg(_(e_missing_gt_after_type)); 657 return NULL; 658 } 659 ++*arg; 660 661 if (type->tt_type == VAR_LIST) 662 return get_list_type(member_type, type_gap); 663 return get_dict_type(member_type, type_gap); 664 } 665 666 /* 667 * Parse a type at "arg" and advance over it. 668 * When "give_error" is TRUE give error messages, otherwise be quiet. 669 * Return NULL for failure. 670 */ 671 type_T * 672 parse_type(char_u **arg, garray_T *type_gap, int give_error) 673 { 674 char_u *p = *arg; 675 size_t len; 676 677 // skip over the first word 678 while (ASCII_ISALNUM(*p) || *p == '_') 679 ++p; 680 len = p - *arg; 681 682 switch (**arg) 683 { 684 case 'a': 685 if (len == 3 && STRNCMP(*arg, "any", len) == 0) 686 { 687 *arg += len; 688 return &t_any; 689 } 690 break; 691 case 'b': 692 if (len == 4 && STRNCMP(*arg, "bool", len) == 0) 693 { 694 *arg += len; 695 return &t_bool; 696 } 697 if (len == 4 && STRNCMP(*arg, "blob", len) == 0) 698 { 699 *arg += len; 700 return &t_blob; 701 } 702 break; 703 case 'c': 704 if (len == 7 && STRNCMP(*arg, "channel", len) == 0) 705 { 706 *arg += len; 707 return &t_channel; 708 } 709 break; 710 case 'd': 711 if (len == 4 && STRNCMP(*arg, "dict", len) == 0) 712 { 713 *arg += len; 714 return parse_type_member(arg, &t_dict_any, 715 type_gap, give_error); 716 } 717 break; 718 case 'f': 719 if (len == 5 && STRNCMP(*arg, "float", len) == 0) 720 { 721 #ifdef FEAT_FLOAT 722 *arg += len; 723 return &t_float; 724 #else 725 if (give_error) 726 emsg(_(e_this_vim_is_not_compiled_with_float_support)); 727 return NULL; 728 #endif 729 } 730 if (len == 4 && STRNCMP(*arg, "func", len) == 0) 731 { 732 type_T *type; 733 type_T *ret_type = &t_unknown; 734 int argcount = -1; 735 int flags = 0; 736 int first_optional = -1; 737 type_T *arg_type[MAX_FUNC_ARGS + 1]; 738 739 // func({type}, ...{type}): {type} 740 *arg += len; 741 if (**arg == '(') 742 { 743 // "func" may or may not return a value, "func()" does 744 // not return a value. 745 ret_type = &t_void; 746 747 p = ++*arg; 748 argcount = 0; 749 while (*p != NUL && *p != ')') 750 { 751 if (*p == '?') 752 { 753 if (first_optional == -1) 754 first_optional = argcount; 755 ++p; 756 } 757 else if (STRNCMP(p, "...", 3) == 0) 758 { 759 flags |= TTFLAG_VARARGS; 760 p += 3; 761 } 762 else if (first_optional != -1) 763 { 764 if (give_error) 765 emsg(_(e_mandatory_argument_after_optional_argument)); 766 return NULL; 767 } 768 769 type = parse_type(&p, type_gap, give_error); 770 if (type == NULL) 771 return NULL; 772 arg_type[argcount++] = type; 773 774 // Nothing comes after "...{type}". 775 if (flags & TTFLAG_VARARGS) 776 break; 777 778 if (*p != ',' && *skipwhite(p) == ',') 779 { 780 if (give_error) 781 semsg(_(e_no_white_space_allowed_before_str), ","); 782 return NULL; 783 } 784 if (*p == ',') 785 { 786 ++p; 787 if (!VIM_ISWHITE(*p)) 788 { 789 if (give_error) 790 semsg(_(e_white_space_required_after_str), ","); 791 return NULL; 792 } 793 } 794 p = skipwhite(p); 795 if (argcount == MAX_FUNC_ARGS) 796 { 797 if (give_error) 798 emsg(_(e_too_many_argument_types)); 799 return NULL; 800 } 801 } 802 803 p = skipwhite(p); 804 if (*p != ')') 805 { 806 if (give_error) 807 emsg(_(e_missing_close)); 808 return NULL; 809 } 810 *arg = p + 1; 811 } 812 if (**arg == ':') 813 { 814 // parse return type 815 ++*arg; 816 if (!VIM_ISWHITE(**arg) && give_error) 817 semsg(_(e_white_space_required_after_str), ":"); 818 *arg = skipwhite(*arg); 819 ret_type = parse_type(arg, type_gap, give_error); 820 if (ret_type == NULL) 821 return NULL; 822 } 823 if (flags == 0 && first_optional == -1 && argcount <= 0) 824 type = get_func_type(ret_type, argcount, type_gap); 825 else 826 { 827 type = alloc_func_type(ret_type, argcount, type_gap); 828 type->tt_flags = flags; 829 if (argcount > 0) 830 { 831 type->tt_argcount = argcount; 832 type->tt_min_argcount = first_optional == -1 833 ? argcount : first_optional; 834 if (func_type_add_arg_types(type, argcount, 835 type_gap) == FAIL) 836 return NULL; 837 mch_memmove(type->tt_args, arg_type, 838 sizeof(type_T *) * argcount); 839 } 840 } 841 return type; 842 } 843 break; 844 case 'j': 845 if (len == 3 && STRNCMP(*arg, "job", len) == 0) 846 { 847 *arg += len; 848 return &t_job; 849 } 850 break; 851 case 'l': 852 if (len == 4 && STRNCMP(*arg, "list", len) == 0) 853 { 854 *arg += len; 855 return parse_type_member(arg, &t_list_any, 856 type_gap, give_error); 857 } 858 break; 859 case 'n': 860 if (len == 6 && STRNCMP(*arg, "number", len) == 0) 861 { 862 *arg += len; 863 return &t_number; 864 } 865 break; 866 case 's': 867 if (len == 6 && STRNCMP(*arg, "string", len) == 0) 868 { 869 *arg += len; 870 return &t_string; 871 } 872 break; 873 case 'v': 874 if (len == 4 && STRNCMP(*arg, "void", len) == 0) 875 { 876 *arg += len; 877 return &t_void; 878 } 879 break; 880 } 881 882 if (give_error) 883 semsg(_(e_type_not_recognized_str), *arg); 884 return NULL; 885 } 886 887 /* 888 * Check if "type1" and "type2" are exactly the same. 889 */ 890 int 891 equal_type(type_T *type1, type_T *type2) 892 { 893 int i; 894 895 if (type1->tt_type != type2->tt_type) 896 return FALSE; 897 switch (type1->tt_type) 898 { 899 case VAR_UNKNOWN: 900 case VAR_ANY: 901 case VAR_VOID: 902 case VAR_SPECIAL: 903 case VAR_BOOL: 904 case VAR_NUMBER: 905 case VAR_FLOAT: 906 case VAR_STRING: 907 case VAR_BLOB: 908 case VAR_JOB: 909 case VAR_CHANNEL: 910 break; // not composite is always OK 911 case VAR_LIST: 912 case VAR_DICT: 913 return equal_type(type1->tt_member, type2->tt_member); 914 case VAR_FUNC: 915 case VAR_PARTIAL: 916 if (!equal_type(type1->tt_member, type2->tt_member) 917 || type1->tt_argcount != type2->tt_argcount) 918 return FALSE; 919 if (type1->tt_argcount < 0 920 || type1->tt_args == NULL || type2->tt_args == NULL) 921 return TRUE; 922 for (i = 0; i < type1->tt_argcount; ++i) 923 if (!equal_type(type1->tt_args[i], type2->tt_args[i])) 924 return FALSE; 925 return TRUE; 926 } 927 return TRUE; 928 } 929 930 /* 931 * Find the common type of "type1" and "type2" and put it in "dest". 932 * "type2" and "dest" may be the same. 933 */ 934 void 935 common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap) 936 { 937 if (equal_type(type1, type2)) 938 { 939 *dest = type1; 940 return; 941 } 942 943 // If either is VAR_UNKNOWN use the other type. An empty list/dict has no 944 // specific type. 945 if (type1->tt_type == VAR_UNKNOWN) 946 { 947 *dest = type2; 948 return; 949 } 950 if (type2->tt_type == VAR_UNKNOWN) 951 { 952 *dest = type1; 953 return; 954 } 955 956 if (type1->tt_type == type2->tt_type) 957 { 958 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) 959 { 960 type_T *common; 961 962 common_type(type1->tt_member, type2->tt_member, &common, type_gap); 963 if (type1->tt_type == VAR_LIST) 964 *dest = get_list_type(common, type_gap); 965 else 966 *dest = get_dict_type(common, type_gap); 967 return; 968 } 969 if (type1->tt_type == VAR_FUNC) 970 { 971 type_T *common; 972 973 common_type(type1->tt_member, type2->tt_member, &common, type_gap); 974 if (type1->tt_argcount == type2->tt_argcount 975 && type1->tt_argcount >= 0) 976 { 977 int argcount = type1->tt_argcount; 978 int i; 979 980 *dest = alloc_func_type(common, argcount, type_gap); 981 if (type1->tt_args != NULL && type2->tt_args != NULL) 982 { 983 if (func_type_add_arg_types(*dest, argcount, 984 type_gap) == OK) 985 for (i = 0; i < argcount; ++i) 986 common_type(type1->tt_args[i], type2->tt_args[i], 987 &(*dest)->tt_args[i], type_gap); 988 } 989 } 990 else 991 *dest = alloc_func_type(common, -1, type_gap); 992 // Use the minimum of min_argcount. 993 (*dest)->tt_min_argcount = 994 type1->tt_min_argcount < type2->tt_min_argcount 995 ? type1->tt_min_argcount : type2->tt_min_argcount; 996 return; 997 } 998 } 999 1000 *dest = &t_any; 1001 } 1002 1003 /* 1004 * Get the member type of a dict or list from the items on the stack. 1005 * "stack_top" points just after the last type on the type stack. 1006 * For a list "skip" is 1, for a dict "skip" is 2, keys are skipped. 1007 * Returns &t_void for an empty list or dict. 1008 * Otherwise finds the common type of all items. 1009 */ 1010 type_T * 1011 get_member_type_from_stack( 1012 type_T **stack_top, 1013 int count, 1014 int skip, 1015 garray_T *type_gap) 1016 { 1017 int i; 1018 type_T *result; 1019 type_T *type; 1020 1021 // Use "any" for an empty list or dict. 1022 if (count == 0) 1023 return &t_unknown; 1024 1025 // Use the first value type for the list member type, then find the common 1026 // type from following items. 1027 result = *(stack_top -(count * skip) + skip - 1); 1028 for (i = 1; i < count; ++i) 1029 { 1030 if (result == &t_any) 1031 break; // won't get more common 1032 type = *(stack_top -((count - i) * skip) + skip - 1); 1033 common_type(type, result, &result, type_gap); 1034 } 1035 1036 return result; 1037 } 1038 1039 char * 1040 vartype_name(vartype_T type) 1041 { 1042 switch (type) 1043 { 1044 case VAR_UNKNOWN: break; 1045 case VAR_ANY: return "any"; 1046 case VAR_VOID: return "void"; 1047 case VAR_SPECIAL: return "special"; 1048 case VAR_BOOL: return "bool"; 1049 case VAR_NUMBER: return "number"; 1050 case VAR_FLOAT: return "float"; 1051 case VAR_STRING: return "string"; 1052 case VAR_BLOB: return "blob"; 1053 case VAR_JOB: return "job"; 1054 case VAR_CHANNEL: return "channel"; 1055 case VAR_LIST: return "list"; 1056 case VAR_DICT: return "dict"; 1057 1058 case VAR_FUNC: 1059 case VAR_PARTIAL: return "func"; 1060 } 1061 return "unknown"; 1062 } 1063 1064 /* 1065 * Return the name of a type. 1066 * The result may be in allocated memory, in which case "tofree" is set. 1067 */ 1068 char * 1069 type_name(type_T *type, char **tofree) 1070 { 1071 char *name; 1072 1073 *tofree = NULL; 1074 if (type == NULL) 1075 return "[unknown]"; 1076 name = vartype_name(type->tt_type); 1077 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) 1078 { 1079 char *member_free; 1080 char *member_name = type_name(type->tt_member, &member_free); 1081 size_t len; 1082 1083 len = STRLEN(name) + STRLEN(member_name) + 3; 1084 *tofree = alloc(len); 1085 if (*tofree != NULL) 1086 { 1087 vim_snprintf(*tofree, len, "%s<%s>", name, member_name); 1088 vim_free(member_free); 1089 return *tofree; 1090 } 1091 } 1092 if (type->tt_type == VAR_FUNC) 1093 { 1094 garray_T ga; 1095 int i; 1096 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; 1097 1098 ga_init2(&ga, 1, 100); 1099 if (ga_grow(&ga, 20) == FAIL) 1100 return "[unknown]"; 1101 STRCPY(ga.ga_data, "func("); 1102 ga.ga_len += 5; 1103 1104 for (i = 0; i < type->tt_argcount; ++i) 1105 { 1106 char *arg_free; 1107 char *arg_type; 1108 int len; 1109 1110 if (type->tt_args == NULL) 1111 arg_type = "[unknown]"; 1112 else 1113 arg_type = type_name(type->tt_args[i], &arg_free); 1114 if (i > 0) 1115 { 1116 STRCPY((char *)ga.ga_data + ga.ga_len, ", "); 1117 ga.ga_len += 2; 1118 } 1119 len = (int)STRLEN(arg_type); 1120 if (ga_grow(&ga, len + 8) == FAIL) 1121 { 1122 vim_free(arg_free); 1123 ga_clear(&ga); 1124 return "[unknown]"; 1125 } 1126 if (varargs && i == type->tt_argcount - 1) 1127 ga_concat(&ga, (char_u *)"..."); 1128 else if (i >= type->tt_min_argcount) 1129 *((char *)ga.ga_data + ga.ga_len++) = '?'; 1130 ga_concat(&ga, (char_u *)arg_type); 1131 vim_free(arg_free); 1132 } 1133 if (type->tt_argcount < 0) 1134 // any number of arguments 1135 ga_concat(&ga, (char_u *)"..."); 1136 1137 if (type->tt_member == &t_void) 1138 STRCPY((char *)ga.ga_data + ga.ga_len, ")"); 1139 else 1140 { 1141 char *ret_free; 1142 char *ret_name = type_name(type->tt_member, &ret_free); 1143 int len; 1144 1145 len = (int)STRLEN(ret_name) + 4; 1146 if (ga_grow(&ga, len) == FAIL) 1147 { 1148 vim_free(ret_free); 1149 ga_clear(&ga); 1150 return "[unknown]"; 1151 } 1152 STRCPY((char *)ga.ga_data + ga.ga_len, "): "); 1153 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); 1154 vim_free(ret_free); 1155 } 1156 *tofree = ga.ga_data; 1157 return ga.ga_data; 1158 } 1159 1160 return name; 1161 } 1162 1163 /* 1164 * "typename(expr)" function 1165 */ 1166 void 1167 f_typename(typval_T *argvars, typval_T *rettv) 1168 { 1169 garray_T type_list; 1170 type_T *type; 1171 char *tofree; 1172 char *name; 1173 1174 rettv->v_type = VAR_STRING; 1175 ga_init2(&type_list, sizeof(type_T *), 10); 1176 type = typval2type(argvars, &type_list); 1177 name = type_name(type, &tofree); 1178 if (tofree != NULL) 1179 rettv->vval.v_string = (char_u *)tofree; 1180 else 1181 { 1182 rettv->vval.v_string = vim_strsave((char_u *)name); 1183 vim_free(tofree); 1184 } 1185 clear_type_list(&type_list); 1186 } 1187 1188 #endif // FEAT_EVAL 1189