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