xref: /vim-8.2.3635/src/vim9type.c (revision cb80aa2d)
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->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->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 		return ufunc->uf_func_type;
343 	}
344     }
345 
346     type = get_type_ptr(type_gap);
347     if (type == NULL)
348 	return NULL;
349     type->tt_type = tv->v_type;
350     type->tt_argcount = argcount;
351     type->tt_member = member_type;
352 
353     return type;
354 }
355 
356 /*
357  * Return TRUE if "tv" is not a bool but should be converted to bool.
358  */
359     int
360 need_convert_to_bool(type_T *type, typval_T *tv)
361 {
362     return type != NULL && type == &t_bool && tv->v_type != VAR_BOOL
363 	    && (tv->v_type == VAR_NUMBER
364 		       && (tv->vval.v_number == 0 || tv->vval.v_number == 1));
365 }
366 
367 /*
368  * Get a type_T for a typval_T.
369  * "type_list" is used to temporarily create types in.
370  */
371     type_T *
372 typval2type(typval_T *tv, garray_T *type_gap)
373 {
374     type_T *type = typval2type_int(tv, type_gap);
375 
376     if (type != NULL && type != &t_bool
377 	    && (tv->v_type == VAR_NUMBER
378 		    && (tv->vval.v_number == 0 || tv->vval.v_number == 1)))
379     {
380 	type_T *newtype = get_type_ptr(type_gap);
381 
382 	// Number 0 and 1 and expression with "&&" or "||" can also be used
383 	// for bool.
384 	if (newtype != NULL)
385 	{
386 	    *newtype = *type;
387 	    newtype->tt_flags = TTFLAG_BOOL_OK;
388 	    type = newtype;
389 	}
390     }
391     return type;
392 }
393 
394 /*
395  * Get a type_T for a typval_T, used for v: variables.
396  * "type_list" is used to temporarily create types in.
397  */
398     type_T *
399 typval2type_vimvar(typval_T *tv, garray_T *type_gap)
400 {
401     if (tv->v_type == VAR_LIST)  // e.g. for v:oldfiles
402 	return &t_list_string;
403     if (tv->v_type == VAR_DICT)  // e.g. for v:completed_item
404 	return &t_dict_any;
405     return typval2type(tv, type_gap);
406 }
407 
408 
409 /*
410  * Return FAIL if "expected" and "actual" don't match.
411  */
412     int
413 check_typval_type(type_T *expected, typval_T *actual_tv, int argidx)
414 {
415     garray_T	type_list;
416     type_T	*actual_type;
417     int		res = FAIL;
418 
419     ga_init2(&type_list, sizeof(type_T *), 10);
420     actual_type = typval2type(actual_tv, &type_list);
421     if (actual_type != NULL)
422 	res = check_type(expected, actual_type, TRUE, argidx);
423     clear_type_list(&type_list);
424     return res;
425 }
426 
427     void
428 type_mismatch(type_T *expected, type_T *actual)
429 {
430     arg_type_mismatch(expected, actual, 0);
431 }
432 
433     void
434 arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
435 {
436     char *tofree1, *tofree2;
437     char *typename1 = type_name(expected, &tofree1);
438     char *typename2 = type_name(actual, &tofree2);
439 
440     if (argidx > 0)
441 	semsg(_(e_argument_nr_type_mismatch_expected_str_but_got_str),
442 						 argidx, typename1, typename2);
443     else
444 	semsg(_(e_type_mismatch_expected_str_but_got_str),
445 							 typename1, typename2);
446     vim_free(tofree1);
447     vim_free(tofree2);
448 }
449 
450 /*
451  * Check if the expected and actual types match.
452  * Does not allow for assigning "any" to a specific type.
453  * When "argidx" > 0 it is included in the error message.
454  */
455     int
456 check_type(type_T *expected, type_T *actual, int give_msg, int argidx)
457 {
458     int ret = OK;
459 
460     // When expected is "unknown" we accept any actual type.
461     // When expected is "any" we accept any actual type except "void".
462     if (expected->tt_type != VAR_UNKNOWN
463 	    && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
464 
465     {
466 	// tt_type should match, except that a "partial" can be assigned to a
467 	// variable with type "func".
468 	if (!(expected->tt_type == actual->tt_type
469 		    || (expected->tt_type == VAR_FUNC
470 					   && actual->tt_type == VAR_PARTIAL)))
471 	{
472 	    if (expected->tt_type == VAR_BOOL
473 					&& (actual->tt_flags & TTFLAG_BOOL_OK))
474 		// Using number 0 or 1 for bool is OK.
475 		return OK;
476 	    if (give_msg)
477 		arg_type_mismatch(expected, actual, argidx);
478 	    return FAIL;
479 	}
480 	if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
481 	{
482 	    // "unknown" is used for an empty list or dict
483 	    if (actual->tt_member != &t_unknown)
484 		ret = check_type(expected->tt_member, actual->tt_member,
485 								     FALSE, 0);
486 	}
487 	else if (expected->tt_type == VAR_FUNC)
488 	{
489 	    if (expected->tt_member != &t_unknown)
490 		ret = check_type(expected->tt_member, actual->tt_member,
491 								     FALSE, 0);
492 	    if (ret == OK && expected->tt_argcount != -1
493 		    && actual->tt_argcount != -1
494 		    && (actual->tt_argcount < expected->tt_min_argcount
495 			|| actual->tt_argcount > expected->tt_argcount))
496 		    ret = FAIL;
497 	    if (expected->tt_args != NULL && actual->tt_args != NULL)
498 	    {
499 		int i;
500 
501 		for (i = 0; i < expected->tt_argcount; ++i)
502 		    // Allow for using "any" argument type, lambda's have them.
503 		    if (actual->tt_args[i] != &t_any && check_type(
504 			    expected->tt_args[i], actual->tt_args[i], FALSE, 0)
505 								       == FAIL)
506 		    {
507 			ret = FAIL;
508 			break;
509 		    }
510 	    }
511 	}
512 	if (ret == FAIL && give_msg)
513 	    arg_type_mismatch(expected, actual, argidx);
514     }
515     return ret;
516 }
517 
518 /*
519  * Skip over a type definition and return a pointer to just after it.
520  * When "optional" is TRUE then a leading "?" is accepted.
521  */
522     char_u *
523 skip_type(char_u *start, int optional)
524 {
525     char_u *p = start;
526 
527     if (optional && *p == '?')
528 	++p;
529     while (ASCII_ISALNUM(*p) || *p == '_')
530 	++p;
531 
532     // Skip over "<type>"; this is permissive about white space.
533     if (*skipwhite(p) == '<')
534     {
535 	p = skipwhite(p);
536 	p = skip_type(skipwhite(p + 1), FALSE);
537 	p = skipwhite(p);
538 	if (*p == '>')
539 	    ++p;
540     }
541     else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
542 					     && STRNCMP("func", start, 4) == 0)
543     {
544 	if (*p == '(')
545 	{
546 	    // handle func(args): type
547 	    ++p;
548 	    while (*p != ')' && *p != NUL)
549 	    {
550 		char_u *sp = p;
551 
552 		if (STRNCMP(p, "...", 3) == 0)
553 		    p += 3;
554 		p = skip_type(p, TRUE);
555 		if (p == sp)
556 		    return p;  // syntax error
557 		if (*p == ',')
558 		    p = skipwhite(p + 1);
559 	    }
560 	    if (*p == ')')
561 	    {
562 		if (p[1] == ':')
563 		    p = skip_type(skipwhite(p + 2), FALSE);
564 		else
565 		    ++p;
566 	    }
567 	}
568 	else
569 	{
570 	    // handle func: return_type
571 	    p = skip_type(skipwhite(p + 1), FALSE);
572 	}
573     }
574 
575     return p;
576 }
577 
578 /*
579  * Parse the member type: "<type>" and return "type" with the member set.
580  * Use "type_gap" if a new type needs to be added.
581  * Returns NULL in case of failure.
582  */
583     static type_T *
584 parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
585 {
586     type_T  *member_type;
587     int	    prev_called_emsg = called_emsg;
588 
589     if (**arg != '<')
590     {
591 	if (*skipwhite(*arg) == '<')
592 	    semsg(_(e_no_white_space_allowed_before_str), "<");
593 	else
594 	    emsg(_(e_missing_type));
595 	return type;
596     }
597     *arg = skipwhite(*arg + 1);
598 
599     member_type = parse_type(arg, type_gap);
600 
601     *arg = skipwhite(*arg);
602     if (**arg != '>' && called_emsg == prev_called_emsg)
603     {
604 	emsg(_(e_missing_gt_after_type));
605 	return type;
606     }
607     ++*arg;
608 
609     if (type->tt_type == VAR_LIST)
610 	return get_list_type(member_type, type_gap);
611     return get_dict_type(member_type, type_gap);
612 }
613 
614 /*
615  * Parse a type at "arg" and advance over it.
616  * Return &t_any for failure.
617  */
618     type_T *
619 parse_type(char_u **arg, garray_T *type_gap)
620 {
621     char_u  *p = *arg;
622     size_t  len;
623 
624     // skip over the first word
625     while (ASCII_ISALNUM(*p) || *p == '_')
626 	++p;
627     len = p - *arg;
628 
629     switch (**arg)
630     {
631 	case 'a':
632 	    if (len == 3 && STRNCMP(*arg, "any", len) == 0)
633 	    {
634 		*arg += len;
635 		return &t_any;
636 	    }
637 	    break;
638 	case 'b':
639 	    if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
640 	    {
641 		*arg += len;
642 		return &t_bool;
643 	    }
644 	    if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
645 	    {
646 		*arg += len;
647 		return &t_blob;
648 	    }
649 	    break;
650 	case 'c':
651 	    if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
652 	    {
653 		*arg += len;
654 		return &t_channel;
655 	    }
656 	    break;
657 	case 'd':
658 	    if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
659 	    {
660 		*arg += len;
661 		return parse_type_member(arg, &t_dict_any, type_gap);
662 	    }
663 	    break;
664 	case 'f':
665 	    if (len == 5 && STRNCMP(*arg, "float", len) == 0)
666 	    {
667 #ifdef FEAT_FLOAT
668 		*arg += len;
669 		return &t_float;
670 #else
671 		emsg(_(e_this_vim_is_not_compiled_with_float_support));
672 		return &t_any;
673 #endif
674 	    }
675 	    if (len == 4 && STRNCMP(*arg, "func", len) == 0)
676 	    {
677 		type_T  *type;
678 		type_T  *ret_type = &t_unknown;
679 		int	argcount = -1;
680 		int	flags = 0;
681 		int	first_optional = -1;
682 		type_T	*arg_type[MAX_FUNC_ARGS + 1];
683 
684 		// func({type}, ...{type}): {type}
685 		*arg += len;
686 		if (**arg == '(')
687 		{
688 		    // "func" may or may not return a value, "func()" does
689 		    // not return a value.
690 		    ret_type = &t_void;
691 
692 		    p = ++*arg;
693 		    argcount = 0;
694 		    while (*p != NUL && *p != ')')
695 		    {
696 			if (*p == '?')
697 			{
698 			    if (first_optional == -1)
699 				first_optional = argcount;
700 			    ++p;
701 			}
702 			else if (STRNCMP(p, "...", 3) == 0)
703 			{
704 			    flags |= TTFLAG_VARARGS;
705 			    p += 3;
706 			}
707 			else if (first_optional != -1)
708 			{
709 			    emsg(_(e_mandatory_argument_after_optional_argument));
710 			    return &t_any;
711 			}
712 
713 			arg_type[argcount++] = parse_type(&p, type_gap);
714 
715 			// Nothing comes after "...{type}".
716 			if (flags & TTFLAG_VARARGS)
717 			    break;
718 
719 			if (*p != ',' && *skipwhite(p) == ',')
720 			{
721 			    semsg(_(e_no_white_space_allowed_before_str), ",");
722 			    return &t_any;
723 			}
724 			if (*p == ',')
725 			{
726 			    ++p;
727 			    if (!VIM_ISWHITE(*p))
728 			    {
729 				semsg(_(e_white_space_required_after_str), ",");
730 				return &t_any;
731 			    }
732 			}
733 			p = skipwhite(p);
734 			if (argcount == MAX_FUNC_ARGS)
735 			{
736 			    emsg(_(e_too_many_argument_types));
737 			    return &t_any;
738 			}
739 		    }
740 
741 		    p = skipwhite(p);
742 		    if (*p != ')')
743 		    {
744 			emsg(_(e_missing_close));
745 			return &t_any;
746 		    }
747 		    *arg = p + 1;
748 		}
749 		if (**arg == ':')
750 		{
751 		    // parse return type
752 		    ++*arg;
753 		    if (!VIM_ISWHITE(**arg))
754 			semsg(_(e_white_space_required_after_str), ":");
755 		    *arg = skipwhite(*arg);
756 		    ret_type = parse_type(arg, type_gap);
757 		}
758 		if (flags == 0 && first_optional == -1 && argcount <= 0)
759 		    type = get_func_type(ret_type, argcount, type_gap);
760 		else
761 		{
762 		    type = alloc_func_type(ret_type, argcount, type_gap);
763 		    type->tt_flags = flags;
764 		    if (argcount > 0)
765 		    {
766 			type->tt_argcount = argcount;
767 			type->tt_min_argcount = first_optional == -1
768 						   ? argcount : first_optional;
769 			if (func_type_add_arg_types(type, argcount,
770 							     type_gap) == FAIL)
771 			    return &t_any;
772 			mch_memmove(type->tt_args, arg_type,
773 						  sizeof(type_T *) * argcount);
774 		    }
775 		}
776 		return type;
777 	    }
778 	    break;
779 	case 'j':
780 	    if (len == 3 && STRNCMP(*arg, "job", len) == 0)
781 	    {
782 		*arg += len;
783 		return &t_job;
784 	    }
785 	    break;
786 	case 'l':
787 	    if (len == 4 && STRNCMP(*arg, "list", len) == 0)
788 	    {
789 		*arg += len;
790 		return parse_type_member(arg, &t_list_any, type_gap);
791 	    }
792 	    break;
793 	case 'n':
794 	    if (len == 6 && STRNCMP(*arg, "number", len) == 0)
795 	    {
796 		*arg += len;
797 		return &t_number;
798 	    }
799 	    break;
800 	case 's':
801 	    if (len == 6 && STRNCMP(*arg, "string", len) == 0)
802 	    {
803 		*arg += len;
804 		return &t_string;
805 	    }
806 	    break;
807 	case 'v':
808 	    if (len == 4 && STRNCMP(*arg, "void", len) == 0)
809 	    {
810 		*arg += len;
811 		return &t_void;
812 	    }
813 	    break;
814     }
815 
816     semsg(_(e_type_not_recognized_str), *arg);
817     return &t_any;
818 }
819 
820 /*
821  * Check if "type1" and "type2" are exactly the same.
822  */
823     static int
824 equal_type(type_T *type1, type_T *type2)
825 {
826     int i;
827 
828     if (type1->tt_type != type2->tt_type)
829 	return FALSE;
830     switch (type1->tt_type)
831     {
832 	case VAR_UNKNOWN:
833 	case VAR_ANY:
834 	case VAR_VOID:
835 	case VAR_SPECIAL:
836 	case VAR_BOOL:
837 	case VAR_NUMBER:
838 	case VAR_FLOAT:
839 	case VAR_STRING:
840 	case VAR_BLOB:
841 	case VAR_JOB:
842 	case VAR_CHANNEL:
843 	    break;  // not composite is always OK
844 	case VAR_LIST:
845 	case VAR_DICT:
846 	    return equal_type(type1->tt_member, type2->tt_member);
847 	case VAR_FUNC:
848 	case VAR_PARTIAL:
849 	    if (!equal_type(type1->tt_member, type2->tt_member)
850 		    || type1->tt_argcount != type2->tt_argcount)
851 		return FALSE;
852 	    if (type1->tt_argcount < 0
853 			   || type1->tt_args == NULL || type2->tt_args == NULL)
854 		return TRUE;
855 	    for (i = 0; i < type1->tt_argcount; ++i)
856 		if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
857 		    return FALSE;
858 	    return TRUE;
859     }
860     return TRUE;
861 }
862 
863 /*
864  * Find the common type of "type1" and "type2" and put it in "dest".
865  * "type2" and "dest" may be the same.
866  */
867     void
868 common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
869 {
870     if (equal_type(type1, type2))
871     {
872 	*dest = type1;
873 	return;
874     }
875 
876     // If either is VAR_UNKNOWN use the other type.  An empty list/dict has no
877     // specific type.
878     if (type1->tt_type == VAR_UNKNOWN)
879     {
880 	*dest = type2;
881 	return;
882     }
883     if (type2->tt_type == VAR_UNKNOWN)
884     {
885 	*dest = type1;
886 	return;
887     }
888 
889     if (type1->tt_type == type2->tt_type)
890     {
891 	if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
892 	{
893 	    type_T *common;
894 
895 	    common_type(type1->tt_member, type2->tt_member, &common, type_gap);
896 	    if (type1->tt_type == VAR_LIST)
897 		*dest = get_list_type(common, type_gap);
898 	    else
899 		*dest = get_dict_type(common, type_gap);
900 	    return;
901 	}
902 	if (type1->tt_type == VAR_FUNC)
903 	{
904 	    type_T *common;
905 
906 	    common_type(type1->tt_member, type2->tt_member, &common, type_gap);
907 	    if (type1->tt_argcount == type2->tt_argcount
908 						    && type1->tt_argcount >= 0)
909 	    {
910 		int argcount = type1->tt_argcount;
911 		int i;
912 
913 		*dest = alloc_func_type(common, argcount, type_gap);
914 		if (type1->tt_args != NULL && type2->tt_args != NULL)
915 		{
916 		    if (func_type_add_arg_types(*dest, argcount,
917 							     type_gap) == OK)
918 			for (i = 0; i < argcount; ++i)
919 			    common_type(type1->tt_args[i], type2->tt_args[i],
920 					       &(*dest)->tt_args[i], type_gap);
921 		}
922 	    }
923 	    else
924 		*dest = alloc_func_type(common, -1, type_gap);
925 	    // Use the minimum of min_argcount.
926 	    (*dest)->tt_min_argcount =
927 			type1->tt_min_argcount < type2->tt_min_argcount
928 			     ? type1->tt_min_argcount : type2->tt_min_argcount;
929 	    return;
930 	}
931     }
932 
933     *dest = &t_any;
934 }
935 
936 /*
937  * Get the member type of a dict or list from the items on the stack.
938  * "stack_top" points just after the last type on the type stack.
939  * For a list "skip" is 1, for a dict "skip" is 2, keys are skipped.
940  * Returns &t_void for an empty list or dict.
941  * Otherwise finds the common type of all items.
942  */
943     type_T *
944 get_member_type_from_stack(
945 	type_T	    **stack_top,
946 	int	    count,
947 	int	    skip,
948 	garray_T    *type_gap)
949 {
950     int	    i;
951     type_T  *result;
952     type_T  *type;
953 
954     // Use "any" for an empty list or dict.
955     if (count == 0)
956 	return &t_unknown;
957 
958     // Use the first value type for the list member type, then find the common
959     // type from following items.
960     result = *(stack_top -(count * skip) + skip - 1);
961     for (i = 1; i < count; ++i)
962     {
963 	if (result == &t_any)
964 	    break;  // won't get more common
965 	type = *(stack_top -((count - i) * skip) + skip - 1);
966 	common_type(type, result, &result, type_gap);
967     }
968 
969     return result;
970 }
971 
972     char *
973 vartype_name(vartype_T type)
974 {
975     switch (type)
976     {
977 	case VAR_UNKNOWN: break;
978 	case VAR_ANY: return "any";
979 	case VAR_VOID: return "void";
980 	case VAR_SPECIAL: return "special";
981 	case VAR_BOOL: return "bool";
982 	case VAR_NUMBER: return "number";
983 	case VAR_FLOAT: return "float";
984 	case VAR_STRING: return "string";
985 	case VAR_BLOB: return "blob";
986 	case VAR_JOB: return "job";
987 	case VAR_CHANNEL: return "channel";
988 	case VAR_LIST: return "list";
989 	case VAR_DICT: return "dict";
990 
991 	case VAR_FUNC:
992 	case VAR_PARTIAL: return "func";
993     }
994     return "unknown";
995 }
996 
997 /*
998  * Return the name of a type.
999  * The result may be in allocated memory, in which case "tofree" is set.
1000  */
1001     char *
1002 type_name(type_T *type, char **tofree)
1003 {
1004     char *name = vartype_name(type->tt_type);
1005 
1006     *tofree = NULL;
1007     if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1008     {
1009 	char *member_free;
1010 	char *member_name = type_name(type->tt_member, &member_free);
1011 	size_t len;
1012 
1013 	len = STRLEN(name) + STRLEN(member_name) + 3;
1014 	*tofree = alloc(len);
1015 	if (*tofree != NULL)
1016 	{
1017 	    vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1018 	    vim_free(member_free);
1019 	    return *tofree;
1020 	}
1021     }
1022     if (type->tt_type == VAR_FUNC)
1023     {
1024 	garray_T    ga;
1025 	int	    i;
1026 	int	    varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1027 
1028 	ga_init2(&ga, 1, 100);
1029 	if (ga_grow(&ga, 20) == FAIL)
1030 	    return "[unknown]";
1031 	STRCPY(ga.ga_data, "func(");
1032 	ga.ga_len += 5;
1033 
1034 	for (i = 0; i < type->tt_argcount; ++i)
1035 	{
1036 	    char *arg_free;
1037 	    char *arg_type;
1038 	    int  len;
1039 
1040 	    if (type->tt_args == NULL)
1041 		arg_type = "[unknown]";
1042 	    else
1043 		arg_type = type_name(type->tt_args[i], &arg_free);
1044 	    if (i > 0)
1045 	    {
1046 		STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
1047 		ga.ga_len += 2;
1048 	    }
1049 	    len = (int)STRLEN(arg_type);
1050 	    if (ga_grow(&ga, len + 8) == FAIL)
1051 	    {
1052 		vim_free(arg_free);
1053 		ga_clear(&ga);
1054 		return "[unknown]";
1055 	    }
1056 	    if (varargs && i == type->tt_argcount - 1)
1057 		ga_concat(&ga, (char_u *)"...");
1058 	    else if (i >= type->tt_min_argcount)
1059 		*((char *)ga.ga_data + ga.ga_len++) = '?';
1060 	    ga_concat(&ga, (char_u *)arg_type);
1061 	    vim_free(arg_free);
1062 	}
1063 	if (type->tt_argcount < 0)
1064 	    // any number of arguments
1065 	    ga_concat(&ga, (char_u *)"...");
1066 
1067 	if (type->tt_member == &t_void)
1068 	    STRCPY((char *)ga.ga_data + ga.ga_len, ")");
1069 	else
1070 	{
1071 	    char *ret_free;
1072 	    char *ret_name = type_name(type->tt_member, &ret_free);
1073 	    int  len;
1074 
1075 	    len = (int)STRLEN(ret_name) + 4;
1076 	    if (ga_grow(&ga, len) == FAIL)
1077 	    {
1078 		vim_free(ret_free);
1079 		ga_clear(&ga);
1080 		return "[unknown]";
1081 	    }
1082 	    STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
1083 	    STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
1084 	    vim_free(ret_free);
1085 	}
1086 	*tofree = ga.ga_data;
1087 	return ga.ga_data;
1088     }
1089 
1090     return name;
1091 }
1092 
1093 #endif // FEAT_EVAL
1094