1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * vim9type.c: handling of types 12 */ 13 14 #define USING_FLOAT_STUFF 15 #include "vim.h" 16 17 #if defined(FEAT_EVAL) || defined(PROTO) 18 19 #ifdef VMS 20 # include <float.h> 21 #endif 22 23 /* 24 * Allocate memory for a type_T and add the pointer to type_gap, so that it can 25 * be easily freed later. 26 */ 27 type_T * 28 get_type_ptr(garray_T *type_gap) 29 { 30 type_T *type; 31 32 if (ga_grow(type_gap, 1) == FAIL) 33 return NULL; 34 type = ALLOC_CLEAR_ONE(type_T); 35 if (type != NULL) 36 { 37 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; 38 ++type_gap->ga_len; 39 } 40 return type; 41 } 42 43 void 44 clear_type_list(garray_T *gap) 45 { 46 while (gap->ga_len > 0) 47 vim_free(((type_T **)gap->ga_data)[--gap->ga_len]); 48 ga_clear(gap); 49 } 50 51 /* 52 * Take a type that is using entries in a growarray and turn it into a type 53 * with allocated entries. 54 */ 55 type_T * 56 alloc_type(type_T *type) 57 { 58 type_T *ret; 59 60 if (type == NULL) 61 return NULL; 62 63 // A fixed type never contains allocated types, return as-is. 64 if (type->tt_flags & TTFLAG_STATIC) 65 return type; 66 67 ret = ALLOC_ONE(type_T); 68 *ret = *type; 69 70 if (ret->tt_member != NULL) 71 ret->tt_member = alloc_type(ret->tt_member); 72 if (type->tt_args != NULL) 73 { 74 int i; 75 76 ret->tt_args = ALLOC_MULT(type_T *, type->tt_argcount); 77 if (ret->tt_args != NULL) 78 for (i = 0; i < type->tt_argcount; ++i) 79 ret->tt_args[i] = alloc_type(type->tt_args[i]); 80 } 81 82 return ret; 83 } 84 85 /* 86 * Free a type that was created with alloc_type(). 87 */ 88 void 89 free_type(type_T *type) 90 { 91 int i; 92 93 if (type == NULL || (type->tt_flags & TTFLAG_STATIC)) 94 return; 95 if (type->tt_args != NULL) 96 { 97 for (i = 0; i < type->tt_argcount; ++i) 98 free_type(type->tt_args[i]); 99 vim_free(type->tt_args); 100 } 101 free_type(type->tt_member); 102 vim_free(type); 103 } 104 105 type_T * 106 get_list_type(type_T *member_type, garray_T *type_gap) 107 { 108 type_T *type; 109 110 // recognize commonly used types 111 if (member_type == NULL || member_type->tt_type == VAR_ANY) 112 return &t_list_any; 113 if (member_type->tt_type == VAR_VOID 114 || member_type->tt_type == VAR_UNKNOWN) 115 return &t_list_empty; 116 if (member_type->tt_type == VAR_BOOL) 117 return &t_list_bool; 118 if (member_type->tt_type == VAR_NUMBER) 119 return &t_list_number; 120 if (member_type->tt_type == VAR_STRING) 121 return &t_list_string; 122 123 // Not a common type, create a new entry. 124 type = get_type_ptr(type_gap); 125 if (type == NULL) 126 return &t_any; 127 type->tt_type = VAR_LIST; 128 type->tt_member = member_type; 129 type->tt_argcount = 0; 130 type->tt_args = NULL; 131 return type; 132 } 133 134 type_T * 135 get_dict_type(type_T *member_type, garray_T *type_gap) 136 { 137 type_T *type; 138 139 // recognize commonly used types 140 if (member_type == NULL || member_type->tt_type == VAR_ANY) 141 return &t_dict_any; 142 if (member_type->tt_type == VAR_VOID 143 || member_type->tt_type == VAR_UNKNOWN) 144 return &t_dict_empty; 145 if (member_type->tt_type == VAR_BOOL) 146 return &t_dict_bool; 147 if (member_type->tt_type == VAR_NUMBER) 148 return &t_dict_number; 149 if (member_type->tt_type == VAR_STRING) 150 return &t_dict_string; 151 152 // Not a common type, create a new entry. 153 type = get_type_ptr(type_gap); 154 if (type == NULL) 155 return &t_any; 156 type->tt_type = VAR_DICT; 157 type->tt_member = member_type; 158 type->tt_argcount = 0; 159 type->tt_args = NULL; 160 return type; 161 } 162 163 /* 164 * Allocate a new type for a function. 165 */ 166 type_T * 167 alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) 168 { 169 type_T *type = get_type_ptr(type_gap); 170 171 if (type == NULL) 172 return &t_any; 173 type->tt_type = VAR_FUNC; 174 type->tt_member = ret_type; 175 type->tt_argcount = argcount; 176 type->tt_args = NULL; 177 return type; 178 } 179 180 /* 181 * Get a function type, based on the return type "ret_type". 182 * If "argcount" is -1 or 0 a predefined type can be used. 183 * If "argcount" > 0 always create a new type, so that arguments can be added. 184 */ 185 type_T * 186 get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) 187 { 188 // recognize commonly used types 189 if (argcount <= 0) 190 { 191 if (ret_type == &t_unknown) 192 { 193 // (argcount == 0) is not possible 194 return &t_func_unknown; 195 } 196 if (ret_type == &t_void) 197 { 198 if (argcount == 0) 199 return &t_func_0_void; 200 else 201 return &t_func_void; 202 } 203 if (ret_type == &t_any) 204 { 205 if (argcount == 0) 206 return &t_func_0_any; 207 else 208 return &t_func_any; 209 } 210 if (ret_type == &t_number) 211 { 212 if (argcount == 0) 213 return &t_func_0_number; 214 else 215 return &t_func_number; 216 } 217 if (ret_type == &t_string) 218 { 219 if (argcount == 0) 220 return &t_func_0_string; 221 else 222 return &t_func_string; 223 } 224 } 225 226 return alloc_func_type(ret_type, argcount, type_gap); 227 } 228 229 /* 230 * For a function type, reserve space for "argcount" argument types (including 231 * vararg). 232 */ 233 int 234 func_type_add_arg_types( 235 type_T *functype, 236 int argcount, 237 garray_T *type_gap) 238 { 239 // To make it easy to free the space needed for the argument types, add the 240 // pointer to type_gap. 241 if (ga_grow(type_gap, 1) == FAIL) 242 return FAIL; 243 functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); 244 if (functype->tt_args == NULL) 245 return FAIL; 246 ((type_T **)type_gap->ga_data)[type_gap->ga_len] = 247 (void *)functype->tt_args; 248 ++type_gap->ga_len; 249 return OK; 250 } 251 252 /* 253 * Get a type_T for a typval_T. 254 * "type_gap" is used to temporarily create types in. 255 */ 256 static type_T * 257 typval2type_int(typval_T *tv, garray_T *type_gap) 258 { 259 type_T *type; 260 type_T *member_type = &t_any; 261 int argcount = 0; 262 263 if (tv->v_type == VAR_NUMBER) 264 return &t_number; 265 if (tv->v_type == VAR_BOOL) 266 return &t_bool; 267 if (tv->v_type == VAR_STRING) 268 return &t_string; 269 270 if (tv->v_type == VAR_LIST) 271 { 272 list_T *l = tv->vval.v_list; 273 listitem_T *li; 274 275 if (l == NULL || l->lv_first == NULL) 276 return &t_list_empty; 277 if (l->lv_first == &range_list_item) 278 return &t_list_number; 279 280 // Use the common type of all members. 281 member_type = typval2type(&l->lv_first->li_tv, type_gap); 282 for (li = l->lv_first->li_next; li != NULL; li = li->li_next) 283 common_type(typval2type(&li->li_tv, type_gap), 284 member_type, &member_type, type_gap); 285 return get_list_type(member_type, type_gap); 286 } 287 288 if (tv->v_type == VAR_DICT) 289 { 290 dict_iterator_T iter; 291 typval_T *value; 292 293 if (tv->vval.v_dict == NULL 294 || tv->vval.v_dict->dv_hashtab.ht_used == 0) 295 return &t_dict_empty; 296 297 // Use the common type of all values. 298 dict_iterate_start(tv, &iter); 299 dict_iterate_next(&iter, &value); 300 member_type = typval2type(value, type_gap); 301 while (dict_iterate_next(&iter, &value) != NULL) 302 common_type(typval2type(value, type_gap), 303 member_type, &member_type, type_gap); 304 return get_dict_type(member_type, type_gap); 305 } 306 307 if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL) 308 { 309 char_u *name = NULL; 310 ufunc_T *ufunc = NULL; 311 312 if (tv->v_type == VAR_PARTIAL) 313 { 314 if (tv->vval.v_partial->pt_func != NULL) 315 ufunc = tv->vval.v_partial->pt_func; 316 else 317 name = tv->vval.v_partial->pt_name; 318 } 319 else 320 name = tv->vval.v_string; 321 if (name != NULL) 322 { 323 int idx = find_internal_func(name); 324 325 if (idx >= 0) 326 { 327 // TODO: get actual arg count and types 328 argcount = -1; 329 member_type = internal_func_ret_type(idx, 0, NULL); 330 } 331 else 332 ufunc = find_func(name, FALSE, NULL); 333 } 334 if (ufunc != NULL) 335 { 336 // May need to get the argument types from default values by 337 // compiling the function. 338 if (ufunc->uf_def_status == UF_TO_BE_COMPILED 339 && compile_def_function(ufunc, TRUE, NULL) == FAIL) 340 return NULL; 341 if (ufunc->uf_func_type == NULL) 342 set_function_type(ufunc); 343 if (ufunc->uf_func_type != NULL) 344 return ufunc->uf_func_type; 345 } 346 } 347 348 type = get_type_ptr(type_gap); 349 if (type == NULL) 350 return NULL; 351 type->tt_type = tv->v_type; 352 type->tt_argcount = argcount; 353 type->tt_member = member_type; 354 355 return type; 356 } 357 358 /* 359 * Return TRUE if "tv" is not a bool but should be converted to bool. 360 */ 361 int 362 need_convert_to_bool(type_T *type, typval_T *tv) 363 { 364 return type != NULL && type == &t_bool && tv->v_type != VAR_BOOL 365 && (tv->v_type == VAR_NUMBER 366 && (tv->vval.v_number == 0 || tv->vval.v_number == 1)); 367 } 368 369 /* 370 * Get a type_T for a typval_T. 371 * "type_list" is used to temporarily create types in. 372 */ 373 type_T * 374 typval2type(typval_T *tv, garray_T *type_gap) 375 { 376 type_T *type = typval2type_int(tv, type_gap); 377 378 if (type != NULL && type != &t_bool 379 && (tv->v_type == VAR_NUMBER 380 && (tv->vval.v_number == 0 || tv->vval.v_number == 1))) 381 // Number 0 and 1 and expression with "&&" or "||" can also be used for 382 // bool. 383 type = &t_number_bool; 384 return type; 385 } 386 387 /* 388 * Get a type_T for a typval_T, used for v: variables. 389 * "type_list" is used to temporarily create types in. 390 */ 391 type_T * 392 typval2type_vimvar(typval_T *tv, garray_T *type_gap) 393 { 394 if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles 395 return &t_list_string; 396 if (tv->v_type == VAR_DICT) // e.g. for v:completed_item 397 return &t_dict_any; 398 return typval2type(tv, type_gap); 399 } 400 401 402 /* 403 * Return FAIL if "expected" and "actual" don't match. 404 * When "argidx" > 0 it is included in the error message. 405 */ 406 int 407 check_typval_type(type_T *expected, typval_T *actual_tv, int argidx) 408 { 409 garray_T type_list; 410 type_T *actual_type; 411 int res = FAIL; 412 413 ga_init2(&type_list, sizeof(type_T *), 10); 414 actual_type = typval2type(actual_tv, &type_list); 415 if (actual_type != NULL) 416 res = check_type(expected, actual_type, TRUE, argidx); 417 clear_type_list(&type_list); 418 return res; 419 } 420 421 void 422 type_mismatch(type_T *expected, type_T *actual) 423 { 424 arg_type_mismatch(expected, actual, 0); 425 } 426 427 void 428 arg_type_mismatch(type_T *expected, type_T *actual, int argidx) 429 { 430 char *tofree1, *tofree2; 431 char *typename1 = type_name(expected, &tofree1); 432 char *typename2 = type_name(actual, &tofree2); 433 434 if (argidx > 0) 435 semsg(_(e_argument_nr_type_mismatch_expected_str_but_got_str), 436 argidx, typename1, typename2); 437 else 438 semsg(_(e_type_mismatch_expected_str_but_got_str), 439 typename1, typename2); 440 vim_free(tofree1); 441 vim_free(tofree2); 442 } 443 444 /* 445 * Check if the expected and actual types match. 446 * Does not allow for assigning "any" to a specific type. 447 * When "argidx" > 0 it is included in the error message. 448 */ 449 int 450 check_type(type_T *expected, type_T *actual, int give_msg, int argidx) 451 { 452 int ret = OK; 453 454 // When expected is "unknown" we accept any actual type. 455 // When expected is "any" we accept any actual type except "void". 456 if (expected->tt_type != VAR_UNKNOWN 457 && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) 458 459 { 460 // tt_type should match, except that a "partial" can be assigned to a 461 // variable with type "func". 462 if (!(expected->tt_type == actual->tt_type 463 || (expected->tt_type == VAR_FUNC 464 && actual->tt_type == VAR_PARTIAL))) 465 { 466 if (expected->tt_type == VAR_BOOL 467 && (actual->tt_flags & TTFLAG_BOOL_OK)) 468 // Using number 0 or 1 for bool is OK. 469 return OK; 470 if (give_msg) 471 arg_type_mismatch(expected, actual, argidx); 472 return FAIL; 473 } 474 if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) 475 { 476 // "unknown" is used for an empty list or dict 477 if (actual->tt_member != &t_unknown) 478 ret = check_type(expected->tt_member, actual->tt_member, 479 FALSE, 0); 480 } 481 else if (expected->tt_type == VAR_FUNC) 482 { 483 // If the return type is unknown it can be anything, including 484 // nothing, thus there is no point in checking. 485 if (expected->tt_member != &t_unknown 486 && actual->tt_member != &t_unknown) 487 ret = check_type(expected->tt_member, actual->tt_member, 488 FALSE, 0); 489 if (ret == OK && expected->tt_argcount != -1 490 && actual->tt_argcount != -1 491 && (actual->tt_argcount < expected->tt_min_argcount 492 || actual->tt_argcount > expected->tt_argcount)) 493 ret = FAIL; 494 if (ret == OK && expected->tt_args != NULL 495 && actual->tt_args != NULL) 496 { 497 int i; 498 499 for (i = 0; i < expected->tt_argcount; ++i) 500 // Allow for using "any" argument type, lambda's have them. 501 if (actual->tt_args[i] != &t_any && check_type( 502 expected->tt_args[i], actual->tt_args[i], FALSE, 0) 503 == FAIL) 504 { 505 ret = FAIL; 506 break; 507 } 508 } 509 } 510 if (ret == FAIL && give_msg) 511 arg_type_mismatch(expected, actual, argidx); 512 } 513 return ret; 514 } 515 516 /* 517 * Like check_type() but also allow for a runtime type check. E.g. "any" can be 518 * used for "number". 519 */ 520 int 521 check_arg_type(type_T *expected, type_T *actual, int argidx) 522 { 523 if (check_type(expected, actual, FALSE, 0) == OK 524 || use_typecheck(actual, expected)) 525 return OK; 526 // TODO: should generate a TYPECHECK instruction. 527 return check_type(expected, actual, TRUE, argidx); 528 } 529 530 /* 531 * Check that the arguments of "type" match "argvars[argcount]". 532 * Return OK/FAIL. 533 */ 534 int 535 check_argument_types(type_T *type, typval_T *argvars, int argcount, char_u *name) 536 { 537 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; 538 int i; 539 540 if (type->tt_type != VAR_FUNC && type->tt_type != VAR_PARTIAL) 541 return OK; // just in case 542 if (argcount < type->tt_min_argcount - varargs) 543 { 544 semsg(_(e_toofewarg), name); 545 return FAIL; 546 } 547 if (!varargs && type->tt_argcount >= 0 && argcount > type->tt_argcount) 548 { 549 semsg(_(e_toomanyarg), name); 550 return FAIL; 551 } 552 if (type->tt_args == NULL) 553 return OK; // cannot check 554 555 556 for (i = 0; i < argcount; ++i) 557 { 558 type_T *expected; 559 560 if (varargs && i >= type->tt_argcount - 1) 561 expected = type->tt_args[type->tt_argcount - 1]->tt_member; 562 else 563 expected = type->tt_args[i]; 564 if (check_typval_type(expected, &argvars[i], i + 1) == FAIL) 565 return FAIL; 566 } 567 return OK; 568 } 569 570 /* 571 * Skip over a type definition and return a pointer to just after it. 572 * When "optional" is TRUE then a leading "?" is accepted. 573 */ 574 char_u * 575 skip_type(char_u *start, int optional) 576 { 577 char_u *p = start; 578 579 if (optional && *p == '?') 580 ++p; 581 while (ASCII_ISALNUM(*p) || *p == '_') 582 ++p; 583 584 // Skip over "<type>"; this is permissive about white space. 585 if (*skipwhite(p) == '<') 586 { 587 p = skipwhite(p); 588 p = skip_type(skipwhite(p + 1), FALSE); 589 p = skipwhite(p); 590 if (*p == '>') 591 ++p; 592 } 593 else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1]))) 594 && STRNCMP("func", start, 4) == 0) 595 { 596 if (*p == '(') 597 { 598 // handle func(args): type 599 ++p; 600 while (*p != ')' && *p != NUL) 601 { 602 char_u *sp = p; 603 604 if (STRNCMP(p, "...", 3) == 0) 605 p += 3; 606 p = skip_type(p, TRUE); 607 if (p == sp) 608 return p; // syntax error 609 if (*p == ',') 610 p = skipwhite(p + 1); 611 } 612 if (*p == ')') 613 { 614 if (p[1] == ':') 615 p = skip_type(skipwhite(p + 2), FALSE); 616 else 617 ++p; 618 } 619 } 620 else 621 { 622 // handle func: return_type 623 p = skip_type(skipwhite(p + 1), FALSE); 624 } 625 } 626 627 return p; 628 } 629 630 /* 631 * Parse the member type: "<type>" and return "type" with the member set. 632 * Use "type_gap" if a new type needs to be added. 633 * Returns NULL in case of failure. 634 */ 635 static type_T * 636 parse_type_member( 637 char_u **arg, 638 type_T *type, 639 garray_T *type_gap, 640 int give_error) 641 { 642 type_T *member_type; 643 int prev_called_emsg = called_emsg; 644 645 if (**arg != '<') 646 { 647 if (give_error) 648 { 649 if (*skipwhite(*arg) == '<') 650 semsg(_(e_no_white_space_allowed_before_str), "<"); 651 else 652 emsg(_(e_missing_type)); 653 } 654 return NULL; 655 } 656 *arg = skipwhite(*arg + 1); 657 658 member_type = parse_type(arg, type_gap, give_error); 659 if (member_type == NULL) 660 return NULL; 661 662 *arg = skipwhite(*arg); 663 if (**arg != '>' && called_emsg == prev_called_emsg) 664 { 665 if (give_error) 666 emsg(_(e_missing_gt_after_type)); 667 return NULL; 668 } 669 ++*arg; 670 671 if (type->tt_type == VAR_LIST) 672 return get_list_type(member_type, type_gap); 673 return get_dict_type(member_type, type_gap); 674 } 675 676 /* 677 * Parse a type at "arg" and advance over it. 678 * When "give_error" is TRUE give error messages, otherwise be quiet. 679 * Return NULL for failure. 680 */ 681 type_T * 682 parse_type(char_u **arg, garray_T *type_gap, int give_error) 683 { 684 char_u *p = *arg; 685 size_t len; 686 687 // skip over the first word 688 while (ASCII_ISALNUM(*p) || *p == '_') 689 ++p; 690 len = p - *arg; 691 692 switch (**arg) 693 { 694 case 'a': 695 if (len == 3 && STRNCMP(*arg, "any", len) == 0) 696 { 697 *arg += len; 698 return &t_any; 699 } 700 break; 701 case 'b': 702 if (len == 4 && STRNCMP(*arg, "bool", len) == 0) 703 { 704 *arg += len; 705 return &t_bool; 706 } 707 if (len == 4 && STRNCMP(*arg, "blob", len) == 0) 708 { 709 *arg += len; 710 return &t_blob; 711 } 712 break; 713 case 'c': 714 if (len == 7 && STRNCMP(*arg, "channel", len) == 0) 715 { 716 *arg += len; 717 return &t_channel; 718 } 719 break; 720 case 'd': 721 if (len == 4 && STRNCMP(*arg, "dict", len) == 0) 722 { 723 *arg += len; 724 return parse_type_member(arg, &t_dict_any, 725 type_gap, give_error); 726 } 727 break; 728 case 'f': 729 if (len == 5 && STRNCMP(*arg, "float", len) == 0) 730 { 731 #ifdef FEAT_FLOAT 732 *arg += len; 733 return &t_float; 734 #else 735 if (give_error) 736 emsg(_(e_this_vim_is_not_compiled_with_float_support)); 737 return NULL; 738 #endif 739 } 740 if (len == 4 && STRNCMP(*arg, "func", len) == 0) 741 { 742 type_T *type; 743 type_T *ret_type = &t_unknown; 744 int argcount = -1; 745 int flags = 0; 746 int first_optional = -1; 747 type_T *arg_type[MAX_FUNC_ARGS + 1]; 748 749 // func({type}, ...{type}): {type} 750 *arg += len; 751 if (**arg == '(') 752 { 753 // "func" may or may not return a value, "func()" does 754 // not return a value. 755 ret_type = &t_void; 756 757 p = ++*arg; 758 argcount = 0; 759 while (*p != NUL && *p != ')') 760 { 761 if (*p == '?') 762 { 763 if (first_optional == -1) 764 first_optional = argcount; 765 ++p; 766 } 767 else if (STRNCMP(p, "...", 3) == 0) 768 { 769 flags |= TTFLAG_VARARGS; 770 p += 3; 771 } 772 else if (first_optional != -1) 773 { 774 if (give_error) 775 emsg(_(e_mandatory_argument_after_optional_argument)); 776 return NULL; 777 } 778 779 type = parse_type(&p, type_gap, give_error); 780 if (type == NULL) 781 return NULL; 782 arg_type[argcount++] = type; 783 784 // Nothing comes after "...{type}". 785 if (flags & TTFLAG_VARARGS) 786 break; 787 788 if (*p != ',' && *skipwhite(p) == ',') 789 { 790 if (give_error) 791 semsg(_(e_no_white_space_allowed_before_str), ","); 792 return NULL; 793 } 794 if (*p == ',') 795 { 796 ++p; 797 if (!VIM_ISWHITE(*p)) 798 { 799 if (give_error) 800 semsg(_(e_white_space_required_after_str), ","); 801 return NULL; 802 } 803 } 804 p = skipwhite(p); 805 if (argcount == MAX_FUNC_ARGS) 806 { 807 if (give_error) 808 emsg(_(e_too_many_argument_types)); 809 return NULL; 810 } 811 } 812 813 p = skipwhite(p); 814 if (*p != ')') 815 { 816 if (give_error) 817 emsg(_(e_missing_close)); 818 return NULL; 819 } 820 *arg = p + 1; 821 } 822 if (**arg == ':') 823 { 824 // parse return type 825 ++*arg; 826 if (!VIM_ISWHITE(**arg) && give_error) 827 semsg(_(e_white_space_required_after_str), ":"); 828 *arg = skipwhite(*arg); 829 ret_type = parse_type(arg, type_gap, give_error); 830 if (ret_type == NULL) 831 return NULL; 832 } 833 if (flags == 0 && first_optional == -1 && argcount <= 0) 834 type = get_func_type(ret_type, argcount, type_gap); 835 else 836 { 837 type = alloc_func_type(ret_type, argcount, type_gap); 838 type->tt_flags = flags; 839 if (argcount > 0) 840 { 841 type->tt_argcount = argcount; 842 type->tt_min_argcount = first_optional == -1 843 ? argcount : first_optional; 844 if (func_type_add_arg_types(type, argcount, 845 type_gap) == FAIL) 846 return NULL; 847 mch_memmove(type->tt_args, arg_type, 848 sizeof(type_T *) * argcount); 849 } 850 } 851 return type; 852 } 853 break; 854 case 'j': 855 if (len == 3 && STRNCMP(*arg, "job", len) == 0) 856 { 857 *arg += len; 858 return &t_job; 859 } 860 break; 861 case 'l': 862 if (len == 4 && STRNCMP(*arg, "list", len) == 0) 863 { 864 *arg += len; 865 return parse_type_member(arg, &t_list_any, 866 type_gap, give_error); 867 } 868 break; 869 case 'n': 870 if (len == 6 && STRNCMP(*arg, "number", len) == 0) 871 { 872 *arg += len; 873 return &t_number; 874 } 875 break; 876 case 's': 877 if (len == 6 && STRNCMP(*arg, "string", len) == 0) 878 { 879 *arg += len; 880 return &t_string; 881 } 882 break; 883 case 'v': 884 if (len == 4 && STRNCMP(*arg, "void", len) == 0) 885 { 886 *arg += len; 887 return &t_void; 888 } 889 break; 890 } 891 892 if (give_error) 893 semsg(_(e_type_not_recognized_str), *arg); 894 return NULL; 895 } 896 897 /* 898 * Check if "type1" and "type2" are exactly the same. 899 */ 900 int 901 equal_type(type_T *type1, type_T *type2) 902 { 903 int i; 904 905 if (type1->tt_type != type2->tt_type) 906 return FALSE; 907 switch (type1->tt_type) 908 { 909 case VAR_UNKNOWN: 910 case VAR_ANY: 911 case VAR_VOID: 912 case VAR_SPECIAL: 913 case VAR_BOOL: 914 case VAR_NUMBER: 915 case VAR_FLOAT: 916 case VAR_STRING: 917 case VAR_BLOB: 918 case VAR_JOB: 919 case VAR_CHANNEL: 920 break; // not composite is always OK 921 case VAR_LIST: 922 case VAR_DICT: 923 return equal_type(type1->tt_member, type2->tt_member); 924 case VAR_FUNC: 925 case VAR_PARTIAL: 926 if (!equal_type(type1->tt_member, type2->tt_member) 927 || type1->tt_argcount != type2->tt_argcount) 928 return FALSE; 929 if (type1->tt_argcount < 0 930 || type1->tt_args == NULL || type2->tt_args == NULL) 931 return TRUE; 932 for (i = 0; i < type1->tt_argcount; ++i) 933 if (!equal_type(type1->tt_args[i], type2->tt_args[i])) 934 return FALSE; 935 return TRUE; 936 } 937 return TRUE; 938 } 939 940 /* 941 * Find the common type of "type1" and "type2" and put it in "dest". 942 * "type2" and "dest" may be the same. 943 */ 944 void 945 common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap) 946 { 947 if (equal_type(type1, type2)) 948 { 949 *dest = type1; 950 return; 951 } 952 953 // If either is VAR_UNKNOWN use the other type. An empty list/dict has no 954 // specific type. 955 if (type1->tt_type == VAR_UNKNOWN) 956 { 957 *dest = type2; 958 return; 959 } 960 if (type2->tt_type == VAR_UNKNOWN) 961 { 962 *dest = type1; 963 return; 964 } 965 966 if (type1->tt_type == type2->tt_type) 967 { 968 if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) 969 { 970 type_T *common; 971 972 common_type(type1->tt_member, type2->tt_member, &common, type_gap); 973 if (type1->tt_type == VAR_LIST) 974 *dest = get_list_type(common, type_gap); 975 else 976 *dest = get_dict_type(common, type_gap); 977 return; 978 } 979 if (type1->tt_type == VAR_FUNC) 980 { 981 type_T *common; 982 983 common_type(type1->tt_member, type2->tt_member, &common, type_gap); 984 if (type1->tt_argcount == type2->tt_argcount 985 && type1->tt_argcount >= 0) 986 { 987 int argcount = type1->tt_argcount; 988 int i; 989 990 *dest = alloc_func_type(common, argcount, type_gap); 991 if (type1->tt_args != NULL && type2->tt_args != NULL) 992 { 993 if (func_type_add_arg_types(*dest, argcount, 994 type_gap) == OK) 995 for (i = 0; i < argcount; ++i) 996 common_type(type1->tt_args[i], type2->tt_args[i], 997 &(*dest)->tt_args[i], type_gap); 998 } 999 } 1000 else 1001 *dest = alloc_func_type(common, -1, type_gap); 1002 // Use the minimum of min_argcount. 1003 (*dest)->tt_min_argcount = 1004 type1->tt_min_argcount < type2->tt_min_argcount 1005 ? type1->tt_min_argcount : type2->tt_min_argcount; 1006 return; 1007 } 1008 } 1009 1010 *dest = &t_any; 1011 } 1012 1013 /* 1014 * Get the member type of a dict or list from the items on the stack. 1015 * "stack_top" points just after the last type on the type stack. 1016 * For a list "skip" is 1, for a dict "skip" is 2, keys are skipped. 1017 * Returns &t_void for an empty list or dict. 1018 * Otherwise finds the common type of all items. 1019 */ 1020 type_T * 1021 get_member_type_from_stack( 1022 type_T **stack_top, 1023 int count, 1024 int skip, 1025 garray_T *type_gap) 1026 { 1027 int i; 1028 type_T *result; 1029 type_T *type; 1030 1031 // Use "any" for an empty list or dict. 1032 if (count == 0) 1033 return &t_unknown; 1034 1035 // Use the first value type for the list member type, then find the common 1036 // type from following items. 1037 result = *(stack_top -(count * skip) + skip - 1); 1038 for (i = 1; i < count; ++i) 1039 { 1040 if (result == &t_any) 1041 break; // won't get more common 1042 type = *(stack_top -((count - i) * skip) + skip - 1); 1043 common_type(type, result, &result, type_gap); 1044 } 1045 1046 return result; 1047 } 1048 1049 char * 1050 vartype_name(vartype_T type) 1051 { 1052 switch (type) 1053 { 1054 case VAR_UNKNOWN: break; 1055 case VAR_ANY: return "any"; 1056 case VAR_VOID: return "void"; 1057 case VAR_SPECIAL: return "special"; 1058 case VAR_BOOL: return "bool"; 1059 case VAR_NUMBER: return "number"; 1060 case VAR_FLOAT: return "float"; 1061 case VAR_STRING: return "string"; 1062 case VAR_BLOB: return "blob"; 1063 case VAR_JOB: return "job"; 1064 case VAR_CHANNEL: return "channel"; 1065 case VAR_LIST: return "list"; 1066 case VAR_DICT: return "dict"; 1067 1068 case VAR_FUNC: 1069 case VAR_PARTIAL: return "func"; 1070 } 1071 return "unknown"; 1072 } 1073 1074 /* 1075 * Return the name of a type. 1076 * The result may be in allocated memory, in which case "tofree" is set. 1077 */ 1078 char * 1079 type_name(type_T *type, char **tofree) 1080 { 1081 char *name; 1082 1083 *tofree = NULL; 1084 if (type == NULL) 1085 return "[unknown]"; 1086 name = vartype_name(type->tt_type); 1087 if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) 1088 { 1089 char *member_free; 1090 char *member_name = type_name(type->tt_member, &member_free); 1091 size_t len; 1092 1093 len = STRLEN(name) + STRLEN(member_name) + 3; 1094 *tofree = alloc(len); 1095 if (*tofree != NULL) 1096 { 1097 vim_snprintf(*tofree, len, "%s<%s>", name, member_name); 1098 vim_free(member_free); 1099 return *tofree; 1100 } 1101 } 1102 if (type->tt_type == VAR_FUNC) 1103 { 1104 garray_T ga; 1105 int i; 1106 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; 1107 1108 ga_init2(&ga, 1, 100); 1109 if (ga_grow(&ga, 20) == FAIL) 1110 return "[unknown]"; 1111 STRCPY(ga.ga_data, "func("); 1112 ga.ga_len += 5; 1113 1114 for (i = 0; i < type->tt_argcount; ++i) 1115 { 1116 char *arg_free; 1117 char *arg_type; 1118 int len; 1119 1120 if (type->tt_args == NULL) 1121 arg_type = "[unknown]"; 1122 else 1123 arg_type = type_name(type->tt_args[i], &arg_free); 1124 if (i > 0) 1125 { 1126 STRCPY((char *)ga.ga_data + ga.ga_len, ", "); 1127 ga.ga_len += 2; 1128 } 1129 len = (int)STRLEN(arg_type); 1130 if (ga_grow(&ga, len + 8) == FAIL) 1131 { 1132 vim_free(arg_free); 1133 ga_clear(&ga); 1134 return "[unknown]"; 1135 } 1136 if (varargs && i == type->tt_argcount - 1) 1137 ga_concat(&ga, (char_u *)"..."); 1138 else if (i >= type->tt_min_argcount) 1139 *((char *)ga.ga_data + ga.ga_len++) = '?'; 1140 ga_concat(&ga, (char_u *)arg_type); 1141 vim_free(arg_free); 1142 } 1143 if (type->tt_argcount < 0) 1144 // any number of arguments 1145 ga_concat(&ga, (char_u *)"..."); 1146 1147 if (type->tt_member == &t_void) 1148 STRCPY((char *)ga.ga_data + ga.ga_len, ")"); 1149 else 1150 { 1151 char *ret_free; 1152 char *ret_name = type_name(type->tt_member, &ret_free); 1153 int len; 1154 1155 len = (int)STRLEN(ret_name) + 4; 1156 if (ga_grow(&ga, len) == FAIL) 1157 { 1158 vim_free(ret_free); 1159 ga_clear(&ga); 1160 return "[unknown]"; 1161 } 1162 STRCPY((char *)ga.ga_data + ga.ga_len, "): "); 1163 STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); 1164 vim_free(ret_free); 1165 } 1166 *tofree = ga.ga_data; 1167 return ga.ga_data; 1168 } 1169 1170 return name; 1171 } 1172 1173 #endif // FEAT_EVAL 1174