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