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 == NULL ? &t_unknown : 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 || ret_type == NULL) 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 * When "do_member" is TRUE also get the member type, otherwise use "any". 256 */ 257 static type_T * 258 typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int do_member) 259 { 260 type_T *type; 261 type_T *member_type = &t_any; 262 int argcount = 0; 263 int min_argcount = 0; 264 265 if (tv->v_type == VAR_NUMBER) 266 return &t_number; 267 if (tv->v_type == VAR_BOOL) 268 return &t_bool; 269 if (tv->v_type == VAR_STRING) 270 return &t_string; 271 272 if (tv->v_type == VAR_LIST) 273 { 274 list_T *l = tv->vval.v_list; 275 listitem_T *li; 276 277 if (l == NULL || l->lv_first == NULL) 278 return &t_list_empty; 279 if (!do_member) 280 return &t_list_any; 281 if (l->lv_first == &range_list_item) 282 return &t_list_number; 283 if (l->lv_copyID == copyID) 284 // avoid recursion 285 return &t_list_any; 286 l->lv_copyID = copyID; 287 288 // Use the common type of all members. 289 member_type = typval2type(&l->lv_first->li_tv, copyID, type_gap, TRUE); 290 for (li = l->lv_first->li_next; li != NULL; li = li->li_next) 291 common_type(typval2type(&li->li_tv, copyID, type_gap, TRUE), 292 member_type, &member_type, type_gap); 293 return get_list_type(member_type, type_gap); 294 } 295 296 if (tv->v_type == VAR_DICT) 297 { 298 dict_iterator_T iter; 299 typval_T *value; 300 dict_T *d = tv->vval.v_dict; 301 302 if (d == NULL || d->dv_hashtab.ht_used == 0) 303 return &t_dict_empty; 304 if (!do_member) 305 return &t_dict_any; 306 if (d->dv_copyID == copyID) 307 // avoid recursion 308 return &t_dict_any; 309 d->dv_copyID = copyID; 310 311 // Use the common type of all values. 312 dict_iterate_start(tv, &iter); 313 dict_iterate_next(&iter, &value); 314 member_type = typval2type(value, copyID, type_gap, TRUE); 315 while (dict_iterate_next(&iter, &value) != NULL) 316 common_type(typval2type(value, copyID, type_gap, TRUE), 317 member_type, &member_type, type_gap); 318 return get_dict_type(member_type, type_gap); 319 } 320 321 if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL) 322 { 323 char_u *name = NULL; 324 ufunc_T *ufunc = NULL; 325 326 if (tv->v_type == VAR_PARTIAL) 327 { 328 if (tv->vval.v_partial->pt_func != NULL) 329 ufunc = tv->vval.v_partial->pt_func; 330 else 331 name = tv->vval.v_partial->pt_name; 332 } 333 else 334 name = tv->vval.v_string; 335 if (name != NULL) 336 { 337 int idx = find_internal_func(name); 338 339 if (idx >= 0) 340 { 341 internal_func_get_argcount(idx, &argcount, &min_argcount); 342 member_type = internal_func_ret_type(idx, 0, NULL); 343 } 344 else 345 ufunc = find_func(name, FALSE, NULL); 346 } 347 if (ufunc != NULL) 348 { 349 // May need to get the argument types from default values by 350 // compiling the function. 351 if (ufunc->uf_def_status == UF_TO_BE_COMPILED 352 && compile_def_function(ufunc, TRUE, CT_NONE, NULL) 353 == FAIL) 354 return NULL; 355 if (ufunc->uf_func_type == NULL) 356 set_function_type(ufunc); 357 if (ufunc->uf_func_type != NULL) 358 { 359 if (tv->v_type == VAR_PARTIAL 360 && tv->vval.v_partial->pt_argc > 0) 361 { 362 type = get_type_ptr(type_gap); 363 if (type == NULL) 364 return NULL; 365 *type = *ufunc->uf_func_type; 366 type->tt_argcount -= tv->vval.v_partial->pt_argc; 367 type->tt_min_argcount -= tv->vval.v_partial->pt_argc; 368 return type; 369 } 370 return ufunc->uf_func_type; 371 } 372 } 373 } 374 375 type = get_type_ptr(type_gap); 376 if (type == NULL) 377 return NULL; 378 type->tt_type = tv->v_type; 379 type->tt_argcount = argcount; 380 type->tt_min_argcount = min_argcount; 381 type->tt_member = member_type; 382 383 return type; 384 } 385 386 /* 387 * Return TRUE if "tv" is not a bool but should be converted to bool. 388 */ 389 int 390 need_convert_to_bool(type_T *type, typval_T *tv) 391 { 392 return type != NULL && type == &t_bool && tv->v_type != VAR_BOOL 393 && (tv->v_type == VAR_NUMBER 394 && (tv->vval.v_number == 0 || tv->vval.v_number == 1)); 395 } 396 397 /* 398 * Get a type_T for a typval_T. 399 * "type_list" is used to temporarily create types in. 400 * When "do_member" is TRUE also get the member type, otherwise use "any". 401 */ 402 type_T * 403 typval2type(typval_T *tv, int copyID, garray_T *type_gap, int do_member) 404 { 405 type_T *type = typval2type_int(tv, copyID, type_gap, do_member); 406 407 if (type != NULL && type != &t_bool 408 && (tv->v_type == VAR_NUMBER 409 && (tv->vval.v_number == 0 || tv->vval.v_number == 1))) 410 // Number 0 and 1 and expression with "&&" or "||" can also be used for 411 // bool. 412 type = &t_number_bool; 413 return type; 414 } 415 416 /* 417 * Get a type_T for a typval_T, used for v: variables. 418 * "type_list" is used to temporarily create types in. 419 */ 420 type_T * 421 typval2type_vimvar(typval_T *tv, garray_T *type_gap) 422 { 423 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles 424 return &t_list_string; 425 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item 426 return &t_dict_any; 427 return typval2type(tv, get_copyID(), type_gap, TRUE); 428 } 429 430 int 431 check_typval_arg_type(type_T *expected, typval_T *actual_tv, int arg_idx) 432 { 433 where_T where; 434 435 where.wt_index = arg_idx; 436 where.wt_variable = FALSE; 437 return check_typval_type(expected, actual_tv, where); 438 } 439 440 /* 441 * Return FAIL if "expected" and "actual" don't match. 442 * When "argidx" > 0 it is included in the error message. 443 */ 444 int 445 check_typval_type(type_T *expected, typval_T *actual_tv, where_T where) 446 { 447 garray_T type_list; 448 type_T *actual_type; 449 int res = FAIL; 450 451 ga_init2(&type_list, sizeof(type_T *), 10); 452 actual_type = typval2type(actual_tv, get_copyID(), &type_list, TRUE); 453 if (actual_type != NULL) 454 res = check_type(expected, actual_type, TRUE, where); 455 clear_type_list(&type_list); 456 return res; 457 } 458 459 void 460 type_mismatch(type_T *expected, type_T *actual) 461 { 462 arg_type_mismatch(expected, actual, 0); 463 } 464 465 void 466 arg_type_mismatch(type_T *expected, type_T *actual, int arg_idx) 467 { 468 where_T where; 469 470 where.wt_index = arg_idx; 471 where.wt_variable = FALSE; 472 type_mismatch_where(expected, actual, where); 473 } 474 475 void 476 type_mismatch_where(type_T *expected, type_T *actual, where_T where) 477 { 478 char *tofree1, *tofree2; 479 char *typename1 = type_name(expected, &tofree1); 480 char *typename2 = type_name(actual, &tofree2); 481 482 if (where.wt_index > 0) 483 { 484 semsg(_(where.wt_variable 485 ? e_variable_nr_type_mismatch_expected_str_but_got_str 486 : e_argument_nr_type_mismatch_expected_str_but_got_str), 487 where.wt_index, typename1, typename2); 488 } 489 else 490 semsg(_(e_type_mismatch_expected_str_but_got_str), 491 typename1, typename2); 492 vim_free(tofree1); 493 vim_free(tofree2); 494 } 495 496 /* 497 * Check if the expected and actual types match. 498 * Does not allow for assigning "any" to a specific type. 499 * When "argidx" > 0 it is included in the error message. 500 */ 501 int 502 check_type(type_T *expected, type_T *actual, int give_msg, where_T where) 503 { 504 int ret = OK; 505 506 // When expected is "unknown" we accept any actual type. 507 // When expected is "any" we accept any actual type except "void". 508 if (expected->tt_type != VAR_UNKNOWN 509 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) 510 511 { 512 // tt_type should match, except that a "partial" can be assigned to a 513 // variable with type "func". 514 if (!(expected->tt_type == actual->tt_type 515 || (expected->tt_type == VAR_FUNC 516 && actual->tt_type == VAR_PARTIAL))) 517 { 518 if (expected->tt_type == VAR_BOOL 519 && (actual->tt_flags & TTFLAG_BOOL_OK)) 520 // Using number 0 or 1 for bool is OK. 521 return OK; 522 if (give_msg) 523 type_mismatch_where(expected, actual, where); 524 return FAIL; 525 } 526 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) 527 { 528 // "unknown" is used for an empty list or dict 529 if (actual->tt_member != &t_unknown) 530 ret = check_type(expected->tt_member, actual->tt_member, 531 FALSE, where); 532 } 533 else if (expected->tt_type == VAR_FUNC) 534 { 535 // If the return type is unknown it can be anything, including 536 // nothing, thus there is no point in checking. 537 if (expected->tt_member != &t_unknown 538 && actual->tt_member != &t_unknown) 539 ret = check_type(expected->tt_member, actual->tt_member, 540 FALSE, where); 541 if (ret == OK && expected->tt_argcount != -1 542 && actual->tt_min_argcount != -1 543 && (actual->tt_argcount == -1 544 || (actual->tt_argcount < expected->tt_min_argcount 545 || actual->tt_argcount > expected->tt_argcount))) 546 ret = FAIL; 547 if (ret == OK && expected->tt_args != NULL 548 && actual->tt_args != NULL) 549 { 550 int i; 551 552 for (i = 0; i < expected->tt_argcount; ++i) 553 // Allow for using "any" argument type, lambda's have them. 554 if (actual->tt_args[i] != &t_any && check_type( 555 expected->tt_args[i], actual->tt_args[i], FALSE, 556 where) == FAIL) 557 { 558 ret = FAIL; 559 break; 560 } 561 } 562 } 563 if (ret == FAIL && give_msg) 564 type_mismatch_where(expected, actual, where); 565 } 566 return ret; 567 } 568 569 /* 570 * Check that the arguments of "type" match "argvars[argcount]". 571 * Return OK/FAIL. 572 */ 573 int 574 check_argument_types( 575 type_T *type, 576 typval_T *argvars, 577 int argcount, 578 char_u *name) 579 { 580 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; 581 int i; 582 583 if (type->tt_type != VAR_FUNC && type->tt_type != VAR_PARTIAL) 584 return OK; // just in case 585 if (argcount < type->tt_min_argcount - varargs) 586 { 587 semsg(_(e_toofewarg), name); 588 return FAIL; 589 } 590 if (!varargs && type->tt_argcount >= 0 && argcount > type->tt_argcount) 591 { 592 semsg(_(e_toomanyarg), name); 593 return FAIL; 594 } 595 if (type->tt_args == NULL) 596 return OK; // cannot check 597 598 599 for (i = 0; i < argcount; ++i) 600 { 601 type_T *expected; 602 603 if (varargs && i >= type->tt_argcount - 1) 604 expected = type->tt_args[type->tt_argcount - 1]->tt_member; 605 else 606 expected = type->tt_args[i]; 607 if (check_typval_arg_type(expected, &argvars[i], i + 1) == FAIL) 608 return FAIL; 609 } 610 return OK; 611 } 612 613 /* 614 * Skip over a type definition and return a pointer to just after it. 615 * When "optional" is TRUE then a leading "?" is accepted. 616 */ 617 char_u * 618 skip_type(char_u *start, int optional) 619 { 620 char_u *p = start; 621 622 if (optional && *p == '?') 623 ++p; 624 while (ASCII_ISALNUM(*p) || *p == '_') 625 ++p; 626 627 // Skip over "<type>"; this is permissive about white space. 628 if (*skipwhite(p) == '<') 629 { 630 p = skipwhite(p); 631 p = skip_type(skipwhite(p + 1), FALSE); 632 p = skipwhite(p); 633 if (*p == '>') 634 ++p; 635 } 636 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1]))) 637 && STRNCMP("func", start, 4) == 0) 638 { 639 if (*p == '(') 640 { 641 // handle func(args): type 642 ++p; 643 while (*p != ')' && *p != NUL) 644 { 645 char_u *sp = p; 646 647 if (STRNCMP(p, "...", 3) == 0) 648 p += 3; 649 p = skip_type(p, TRUE); 650 if (p == sp) 651 return p; // syntax error 652 if (*p == ',') 653 p = skipwhite(p + 1); 654 } 655 if (*p == ')') 656 { 657 if (p[1] == ':') 658 p = skip_type(skipwhite(p + 2), FALSE); 659 else 660 ++p; 661 } 662 } 663 else 664 { 665 // handle func: return_type 666 p = skip_type(skipwhite(p + 1), FALSE); 667 } 668 } 669 670 return p; 671 } 672 673 /* 674 * Parse the member type: "<type>" and return "type" with the member set. 675 * Use "type_gap" if a new type needs to be added. 676 * Returns NULL in case of failure. 677 */ 678 static type_T * 679 parse_type_member( 680 char_u **arg, 681 type_T *type, 682 garray_T *type_gap, 683 int give_error) 684 { 685 type_T *member_type; 686 int prev_called_emsg = called_emsg; 687 688 if (**arg != '<') 689 { 690 if (give_error) 691 { 692 if (*skipwhite(*arg) == '<') 693 semsg(_(e_no_white_space_allowed_before_str_str), "<", *arg); 694 else 695 emsg(_(e_missing_type)); 696 } 697 return NULL; 698 } 699 *arg = skipwhite(*arg + 1); 700 701 member_type = parse_type(arg, type_gap, give_error); 702 if (member_type == NULL) 703 return NULL; 704 705 *arg = skipwhite(*arg); 706 if (**arg != '>' && called_emsg == prev_called_emsg) 707 { 708 if (give_error) 709 emsg(_(e_missing_gt_after_type)); 710 return NULL; 711 } 712 ++*arg; 713 714 if (type->tt_type == VAR_LIST) 715 return get_list_type(member_type, type_gap); 716 return get_dict_type(member_type, type_gap); 717 } 718 719 /* 720 * Parse a type at "arg" and advance over it. 721 * When "give_error" is TRUE give error messages, otherwise be quiet. 722 * Return NULL for failure. 723 */ 724 type_T * 725 parse_type(char_u **arg, garray_T *type_gap, int give_error) 726 { 727 char_u *p = *arg; 728 size_t len; 729 730 // skip over the first word 731 while (ASCII_ISALNUM(*p) || *p == '_') 732 ++p; 733 len = p - *arg; 734 735 switch (**arg) 736 { 737 case 'a': 738 if (len == 3 && STRNCMP(*arg, "any", len) == 0) 739 { 740 *arg += len; 741 return &t_any; 742 } 743 break; 744 case 'b': 745 if (len == 4 && STRNCMP(*arg, "bool", len) == 0) 746 { 747 *arg += len; 748 return &t_bool; 749 } 750 if (len == 4 && STRNCMP(*arg, "blob", len) == 0) 751 { 752 *arg += len; 753 return &t_blob; 754 } 755 break; 756 case 'c': 757 if (len == 7 && STRNCMP(*arg, "channel", len) == 0) 758 { 759 *arg += len; 760 return &t_channel; 761 } 762 break; 763 case 'd': 764 if (len == 4 && STRNCMP(*arg, "dict", len) == 0) 765 { 766 *arg += len; 767 return parse_type_member(arg, &t_dict_any, 768 type_gap, give_error); 769 } 770 break; 771 case 'f': 772 if (len == 5 && STRNCMP(*arg, "float", len) == 0) 773 { 774 #ifdef FEAT_FLOAT 775 *arg += len; 776 return &t_float; 777 #else 778 if (give_error) 779 emsg(_(e_this_vim_is_not_compiled_with_float_support)); 780 return NULL; 781 #endif 782 } 783 if (len == 4 && STRNCMP(*arg, "func", len) == 0) 784 { 785 type_T *type; 786 type_T *ret_type = &t_unknown; 787 int argcount = -1; 788 int flags = 0; 789 int first_optional = -1; 790 type_T *arg_type[MAX_FUNC_ARGS + 1]; 791 792 // func({type}, ...{type}): {type} 793 *arg += len; 794 if (**arg == '(') 795 { 796 // "func" may or may not return a value, "func()" does 797 // not return a value. 798 ret_type = &t_void; 799 800 p = ++*arg; 801 argcount = 0; 802 while (*p != NUL && *p != ')') 803 { 804 if (*p == '?') 805 { 806 if (first_optional == -1) 807 first_optional = argcount; 808 ++p; 809 } 810 else if (STRNCMP(p, "...", 3) == 0) 811 { 812 flags |= TTFLAG_VARARGS; 813 p += 3; 814 } 815 else if (first_optional != -1) 816 { 817 if (give_error) 818 emsg(_(e_mandatory_argument_after_optional_argument)); 819 return NULL; 820 } 821 822 type = parse_type(&p, type_gap, give_error); 823 if (type == NULL) 824 return NULL; 825 arg_type[argcount++] = type; 826 827 // Nothing comes after "...{type}". 828 if (flags & TTFLAG_VARARGS) 829 break; 830 831 if (*p != ',' && *skipwhite(p) == ',') 832 { 833 if (give_error) 834 semsg(_(e_no_white_space_allowed_before_str_str), 835 ",", p); 836 return NULL; 837 } 838 if (*p == ',') 839 { 840 ++p; 841 if (!VIM_ISWHITE(*p)) 842 { 843 if (give_error) 844 semsg(_(e_white_space_required_after_str_str), 845 ",", p - 1); 846 return NULL; 847 } 848 } 849 p = skipwhite(p); 850 if (argcount == MAX_FUNC_ARGS) 851 { 852 if (give_error) 853 emsg(_(e_too_many_argument_types)); 854 return NULL; 855 } 856 } 857 858 p = skipwhite(p); 859 if (*p != ')') 860 { 861 if (give_error) 862 emsg(_(e_missing_close)); 863 return NULL; 864 } 865 *arg = p + 1; 866 } 867 if (**arg == ':') 868 { 869 // parse return type 870 ++*arg; 871 if (!VIM_ISWHITE(**arg) && give_error) 872 semsg(_(e_white_space_required_after_str_str), 873 ":", *arg - 1); 874 *arg = skipwhite(*arg); 875 ret_type = parse_type(arg, type_gap, give_error); 876 if (ret_type == NULL) 877 return NULL; 878 } 879 if (flags == 0 && first_optional == -1 && argcount <= 0) 880 type = get_func_type(ret_type, argcount, type_gap); 881 else 882 { 883 type = alloc_func_type(ret_type, argcount, type_gap); 884 type->tt_flags = flags; 885 if (argcount > 0) 886 { 887 type->tt_argcount = argcount; 888 type->tt_min_argcount = first_optional == -1 889 ? argcount : first_optional; 890 if (func_type_add_arg_types(type, argcount, 891 type_gap) == FAIL) 892 return NULL; 893 mch_memmove(type->tt_args, arg_type, 894 sizeof(type_T *) * argcount); 895 } 896 } 897 return type; 898 } 899 break; 900 case 'j': 901 if (len == 3 && STRNCMP(*arg, "job", len) == 0) 902 { 903 *arg += len; 904 return &t_job; 905 } 906 break; 907 case 'l': 908 if (len == 4 && STRNCMP(*arg, "list", len) == 0) 909 { 910 *arg += len; 911 return parse_type_member(arg, &t_list_any, 912 type_gap, give_error); 913 } 914 break; 915 case 'n': 916 if (len == 6 && STRNCMP(*arg, "number", len) == 0) 917 { 918 *arg += len; 919 return &t_number; 920 } 921 break; 922 case 's': 923 if (len == 6 && STRNCMP(*arg, "string", len) == 0) 924 { 925 *arg += len; 926 return &t_string; 927 } 928 break; 929 case 'v': 930 if (len == 4 && STRNCMP(*arg, "void", len) == 0) 931 { 932 *arg += len; 933 return &t_void; 934 } 935 break; 936 } 937 938 if (give_error) 939 semsg(_(e_type_not_recognized_str), *arg); 940 return NULL; 941 } 942 943 /* 944 * Check if "type1" and "type2" are exactly the same. 945 */ 946 int 947 equal_type(type_T *type1, type_T *type2) 948 { 949 int i; 950 951 if (type1 == NULL || type2 == NULL) 952 return FALSE; 953 if (type1->tt_type != type2->tt_type) 954 return FALSE; 955 switch (type1->tt_type) 956 { 957 case VAR_UNKNOWN: 958 case VAR_ANY: 959 case VAR_VOID: 960 case VAR_SPECIAL: 961 case VAR_BOOL: 962 case VAR_NUMBER: 963 case VAR_FLOAT: 964 case VAR_STRING: 965 case VAR_BLOB: 966 case VAR_JOB: 967 case VAR_CHANNEL: 968 case VAR_INSTR: 969 break; // not composite is always OK 970 case VAR_LIST: 971 case VAR_DICT: 972 return equal_type(type1->tt_member, type2->tt_member); 973 case VAR_FUNC: 974 case VAR_PARTIAL: 975 if (!equal_type(type1->tt_member, type2->tt_member) 976 || type1->tt_argcount != type2->tt_argcount) 977 return FALSE; 978 if (type1->tt_argcount < 0 979 || type1->tt_args == NULL || type2->tt_args == NULL) 980 return TRUE; 981 for (i = 0; i < type1->tt_argcount; ++i) 982 if (!equal_type(type1->tt_args[i], type2->tt_args[i])) 983 return FALSE; 984 return TRUE; 985 } 986 return TRUE; 987 } 988 989 /* 990 * Find the common type of "type1" and "type2" and put it in "dest". 991 * "type2" and "dest" may be the same. 992 */ 993 void 994 common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap) 995 { 996 if (equal_type(type1, type2)) 997 { 998 *dest = type1; 999 return; 1000 } 1001 1002 // If either is VAR_UNKNOWN use the other type. An empty list/dict has no 1003 // specific type. 1004 if (type1 == NULL || type1->tt_type == VAR_UNKNOWN) 1005 { 1006 *dest = type2; 1007 return; 1008 } 1009 if (type2 == NULL || type2->tt_type == VAR_UNKNOWN) 1010 { 1011 *dest = type1; 1012 return; 1013 } 1014 1015 if (type1->tt_type == type2->tt_type) 1016 { 1017 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) 1018 { 1019 type_T *common; 1020 1021 common_type(type1->tt_member, type2->tt_member, &common, type_gap); 1022 if (type1->tt_type == VAR_LIST) 1023 *dest = get_list_type(common, type_gap); 1024 else 1025 *dest = get_dict_type(common, type_gap); 1026 return; 1027 } 1028 if (type1->tt_type == VAR_FUNC) 1029 { 1030 type_T *common; 1031 1032 common_type(type1->tt_member, type2->tt_member, &common, type_gap); 1033 if (type1->tt_argcount == type2->tt_argcount 1034 && type1->tt_argcount >= 0) 1035 { 1036 int argcount = type1->tt_argcount; 1037 int i; 1038 1039 *dest = alloc_func_type(common, argcount, type_gap); 1040 if (type1->tt_args != NULL && type2->tt_args != NULL) 1041 { 1042 if (func_type_add_arg_types(*dest, argcount, 1043 type_gap) == OK) 1044 for (i = 0; i < argcount; ++i) 1045 common_type(type1->tt_args[i], type2->tt_args[i], 1046 &(*dest)->tt_args[i], type_gap); 1047 } 1048 } 1049 else 1050 // Use -1 for "tt_argcount" to indicate an unknown number of 1051 // arguments. 1052 *dest = alloc_func_type(common, -1, type_gap); 1053 1054 // Use the minimum of min_argcount. 1055 (*dest)->tt_min_argcount = 1056 type1->tt_min_argcount < type2->tt_min_argcount 1057 ? type1->tt_min_argcount : type2->tt_min_argcount; 1058 return; 1059 } 1060 } 1061 1062 *dest = &t_any; 1063 } 1064 1065 /* 1066 * Get the member type of a dict or list from the items on the stack. 1067 * "stack_top" points just after the last type on the type stack. 1068 * For a list "skip" is 1, for a dict "skip" is 2, keys are skipped. 1069 * Returns &t_void for an empty list or dict. 1070 * Otherwise finds the common type of all items. 1071 */ 1072 type_T * 1073 get_member_type_from_stack( 1074 type_T **stack_top, 1075 int count, 1076 int skip, 1077 garray_T *type_gap) 1078 { 1079 int i; 1080 type_T *result; 1081 type_T *type; 1082 1083 // Use "any" for an empty list or dict. 1084 if (count == 0) 1085 return &t_unknown; 1086 1087 // Use the first value type for the list member type, then find the common 1088 // type from following items. 1089 result = *(stack_top -(count * skip) + skip - 1); 1090 for (i = 1; i < count; ++i) 1091 { 1092 if (result == &t_any) 1093 break; // won't get more common 1094 type = *(stack_top -((count - i) * skip) + skip - 1); 1095 common_type(type, result, &result, type_gap); 1096 } 1097 1098 return result; 1099 } 1100 1101 char * 1102 vartype_name(vartype_T type) 1103 { 1104 switch (type) 1105 { 1106 case VAR_UNKNOWN: break; 1107 case VAR_ANY: return "any"; 1108 case VAR_VOID: return "void"; 1109 case VAR_SPECIAL: return "special"; 1110 case VAR_BOOL: return "bool"; 1111 case VAR_NUMBER: return "number"; 1112 case VAR_FLOAT: return "float"; 1113 case VAR_STRING: return "string"; 1114 case VAR_BLOB: return "blob"; 1115 case VAR_JOB: return "job"; 1116 case VAR_CHANNEL: return "channel"; 1117 case VAR_LIST: return "list"; 1118 case VAR_DICT: return "dict"; 1119 case VAR_INSTR: return "instr"; 1120 1121 case VAR_FUNC: 1122 case VAR_PARTIAL: return "func"; 1123 } 1124 return "unknown"; 1125 } 1126 1127 /* 1128 * Return the name of a type. 1129 * The result may be in allocated memory, in which case "tofree" is set. 1130 */ 1131 char * 1132 type_name(type_T *type, char **tofree) 1133 { 1134 char *name; 1135 1136 *tofree = NULL; 1137 if (type == NULL) 1138 return "[unknown]"; 1139 name = vartype_name(type->tt_type); 1140 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) 1141 { 1142 char *member_free; 1143 char *member_name = type_name(type->tt_member, &member_free); 1144 size_t len; 1145 1146 len = STRLEN(name) + STRLEN(member_name) + 3; 1147 *tofree = alloc(len); 1148 if (*tofree != NULL) 1149 { 1150 vim_snprintf(*tofree, len, "%s<%s>", name, member_name); 1151 vim_free(member_free); 1152 return *tofree; 1153 } 1154 } 1155 if (type->tt_type == VAR_FUNC) 1156 { 1157 garray_T ga; 1158 int i; 1159 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; 1160 1161 ga_init2(&ga, 1, 100); 1162 if (ga_grow(&ga, 20) == FAIL) 1163 return "[unknown]"; 1164 STRCPY(ga.ga_data, "func("); 1165 ga.ga_len += 5; 1166 1167 for (i = 0; i < type->tt_argcount; ++i) 1168 { 1169 char *arg_free; 1170 char *arg_type; 1171 int len; 1172 1173 if (type->tt_args == NULL) 1174 arg_type = "[unknown]"; 1175 else 1176 arg_type = type_name(type->tt_args[i], &arg_free); 1177 if (i > 0) 1178 { 1179 STRCPY((char *)ga.ga_data + ga.ga_len, ", "); 1180 ga.ga_len += 2; 1181 } 1182 len = (int)STRLEN(arg_type); 1183 if (ga_grow(&ga, len + 8) == FAIL) 1184 { 1185 vim_free(arg_free); 1186 ga_clear(&ga); 1187 return "[unknown]"; 1188 } 1189 if (varargs && i == type->tt_argcount - 1) 1190 ga_concat(&ga, (char_u *)"..."); 1191 else if (i >= type->tt_min_argcount) 1192 *((char *)ga.ga_data + ga.ga_len++) = '?'; 1193 ga_concat(&ga, (char_u *)arg_type); 1194 vim_free(arg_free); 1195 } 1196 if (type->tt_argcount < 0) 1197 // any number of arguments 1198 ga_concat(&ga, (char_u *)"..."); 1199 1200 if (type->tt_member == &t_void) 1201 STRCPY((char *)ga.ga_data + ga.ga_len, ")"); 1202 else 1203 { 1204 char *ret_free; 1205 char *ret_name = type_name(type->tt_member, &ret_free); 1206 int len; 1207 1208 len = (int)STRLEN(ret_name) + 4; 1209 if (ga_grow(&ga, len) == FAIL) 1210 { 1211 vim_free(ret_free); 1212 ga_clear(&ga); 1213 return "[unknown]"; 1214 } 1215 STRCPY((char *)ga.ga_data + ga.ga_len, "): "); 1216 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); 1217 vim_free(ret_free); 1218 } 1219 *tofree = ga.ga_data; 1220 return ga.ga_data; 1221 } 1222 1223 return name; 1224 } 1225 1226 /* 1227 * "typename(expr)" function 1228 */ 1229 void 1230 f_typename(typval_T *argvars, typval_T *rettv) 1231 { 1232 garray_T type_list; 1233 type_T *type; 1234 char *tofree; 1235 char *name; 1236 1237 rettv->v_type = VAR_STRING; 1238 ga_init2(&type_list, sizeof(type_T *), 10); 1239 type = typval2type(argvars, get_copyID(), &type_list, TRUE); 1240 name = type_name(type, &tofree); 1241 if (tofree != NULL) 1242 rettv->vval.v_string = (char_u *)tofree; 1243 else 1244 { 1245 rettv->vval.v_string = vim_strsave((char_u *)name); 1246 vim_free(tofree); 1247 } 1248 clear_type_list(&type_list); 1249 } 1250 1251 #endif // FEAT_EVAL 1252