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