xref: /vim-8.2.3635/src/vim9type.c (revision fa57335e)
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 freed later.
26  */
27     static type_T *
28 alloc_type(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     type_T *
52 get_list_type(type_T *member_type, garray_T *type_gap)
53 {
54     type_T *type;
55 
56     // recognize commonly used types
57     if (member_type->tt_type == VAR_ANY)
58 	return &t_list_any;
59     if (member_type->tt_type == VAR_VOID
60 	    || member_type->tt_type == VAR_UNKNOWN)
61 	return &t_list_empty;
62     if (member_type->tt_type == VAR_BOOL)
63 	return &t_list_bool;
64     if (member_type->tt_type == VAR_NUMBER)
65 	return &t_list_number;
66     if (member_type->tt_type == VAR_STRING)
67 	return &t_list_string;
68 
69     // Not a common type, create a new entry.
70     type = alloc_type(type_gap);
71     if (type == NULL)
72 	return &t_any;
73     type->tt_type = VAR_LIST;
74     type->tt_member = member_type;
75     type->tt_argcount = 0;
76     type->tt_args = NULL;
77     return type;
78 }
79 
80     type_T *
81 get_dict_type(type_T *member_type, garray_T *type_gap)
82 {
83     type_T *type;
84 
85     // recognize commonly used types
86     if (member_type->tt_type == VAR_ANY)
87 	return &t_dict_any;
88     if (member_type->tt_type == VAR_VOID
89 	    || member_type->tt_type == VAR_UNKNOWN)
90 	return &t_dict_empty;
91     if (member_type->tt_type == VAR_BOOL)
92 	return &t_dict_bool;
93     if (member_type->tt_type == VAR_NUMBER)
94 	return &t_dict_number;
95     if (member_type->tt_type == VAR_STRING)
96 	return &t_dict_string;
97 
98     // Not a common type, create a new entry.
99     type = alloc_type(type_gap);
100     if (type == NULL)
101 	return &t_any;
102     type->tt_type = VAR_DICT;
103     type->tt_member = member_type;
104     type->tt_argcount = 0;
105     type->tt_args = NULL;
106     return type;
107 }
108 
109 /*
110  * Allocate a new type for a function.
111  */
112     type_T *
113 alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
114 {
115     type_T *type = alloc_type(type_gap);
116 
117     if (type == NULL)
118 	return &t_any;
119     type->tt_type = VAR_FUNC;
120     type->tt_member = ret_type;
121     type->tt_argcount = argcount;
122     type->tt_args = NULL;
123     return type;
124 }
125 
126 /*
127  * Get a function type, based on the return type "ret_type".
128  * If "argcount" is -1 or 0 a predefined type can be used.
129  * If "argcount" > 0 always create a new type, so that arguments can be added.
130  */
131     type_T *
132 get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
133 {
134     // recognize commonly used types
135     if (argcount <= 0)
136     {
137 	if (ret_type == &t_unknown)
138 	{
139 	    // (argcount == 0) is not possible
140 	    return &t_func_unknown;
141 	}
142 	if (ret_type == &t_void)
143 	{
144 	    if (argcount == 0)
145 		return &t_func_0_void;
146 	    else
147 		return &t_func_void;
148 	}
149 	if (ret_type == &t_any)
150 	{
151 	    if (argcount == 0)
152 		return &t_func_0_any;
153 	    else
154 		return &t_func_any;
155 	}
156 	if (ret_type == &t_number)
157 	{
158 	    if (argcount == 0)
159 		return &t_func_0_number;
160 	    else
161 		return &t_func_number;
162 	}
163 	if (ret_type == &t_string)
164 	{
165 	    if (argcount == 0)
166 		return &t_func_0_string;
167 	    else
168 		return &t_func_string;
169 	}
170     }
171 
172     return alloc_func_type(ret_type, argcount, type_gap);
173 }
174 
175 /*
176  * For a function type, reserve space for "argcount" argument types (including
177  * vararg).
178  */
179     int
180 func_type_add_arg_types(
181 	type_T	    *functype,
182 	int	    argcount,
183 	garray_T    *type_gap)
184 {
185     // To make it easy to free the space needed for the argument types, add the
186     // pointer to type_gap.
187     if (ga_grow(type_gap, 1) == FAIL)
188 	return FAIL;
189     functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
190     if (functype->tt_args == NULL)
191 	return FAIL;
192     ((type_T **)type_gap->ga_data)[type_gap->ga_len] =
193 						     (void *)functype->tt_args;
194     ++type_gap->ga_len;
195     return OK;
196 }
197 
198 /*
199  * Get a type_T for a typval_T.
200  * "type_list" is used to temporarily create types in.
201  */
202     type_T *
203 typval2type(typval_T *tv, garray_T *type_gap)
204 {
205     type_T  *actual;
206     type_T  *member_type;
207 
208     if (tv->v_type == VAR_NUMBER)
209 	return &t_number;
210     if (tv->v_type == VAR_BOOL)
211 	return &t_bool;  // not used
212     if (tv->v_type == VAR_STRING)
213 	return &t_string;
214 
215     if (tv->v_type == VAR_LIST)
216     {
217 	listitem_T *li;
218 
219 	if (tv->vval.v_list == NULL || tv->vval.v_list->lv_first == NULL)
220 	    return &t_list_empty;
221 
222 	// Use the common type of all members.
223 	member_type = typval2type(&tv->vval.v_list->lv_first->li_tv, type_gap);
224 	for (li = tv->vval.v_list->lv_first->li_next; li != NULL;
225 							     li = li->li_next)
226 	    common_type(typval2type(&li->li_tv, type_gap),
227 					  member_type, &member_type, type_gap);
228 	return get_list_type(member_type, type_gap);
229     }
230 
231     if (tv->v_type == VAR_DICT)
232     {
233 	dict_iterator_T iter;
234 	typval_T	*value;
235 
236 	if (tv->vval.v_dict == NULL
237 				   || tv->vval.v_dict->dv_hashtab.ht_used == 0)
238 	    return &t_dict_empty;
239 
240 	// Use the common type of all values.
241 	dict_iterate_start(tv, &iter);
242 	dict_iterate_next(&iter, &value);
243 	member_type = typval2type(value, type_gap);
244 	while (dict_iterate_next(&iter, &value) != NULL)
245 	    common_type(typval2type(value, type_gap),
246 					  member_type, &member_type, type_gap);
247 	return get_dict_type(member_type, type_gap);
248     }
249 
250     if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
251     {
252 	char_u	*name = NULL;
253 	ufunc_T *ufunc = NULL;
254 
255 	if (tv->v_type == VAR_PARTIAL)
256 	{
257 	    if (tv->vval.v_partial->pt_func != NULL)
258 		ufunc = tv->vval.v_partial->pt_func;
259 	    else
260 		name = tv->vval.v_partial->pt_name;
261 	}
262 	else
263 	    name = tv->vval.v_string;
264 	if (name != NULL)
265 	    // TODO: how about a builtin function?
266 	    ufunc = find_func(name, FALSE, NULL);
267 	if (ufunc != NULL)
268 	{
269 	    // May need to get the argument types from default values by
270 	    // compiling the function.
271 	    if (ufunc->uf_def_status == UF_TO_BE_COMPILED
272 			    && compile_def_function(ufunc, TRUE, NULL) == FAIL)
273 		return NULL;
274 	    if (ufunc->uf_func_type != NULL)
275 		return ufunc->uf_func_type;
276 	}
277     }
278 
279     actual = alloc_type(type_gap);
280     if (actual == NULL)
281 	return NULL;
282     actual->tt_type = tv->v_type;
283     actual->tt_member = &t_any;
284 
285     return actual;
286 }
287 
288 /*
289  * Get a type_T for a typval_T, used for v: variables.
290  * "type_list" is used to temporarily create types in.
291  */
292     type_T *
293 typval2type_vimvar(typval_T *tv, garray_T *type_gap)
294 {
295     if (tv->v_type == VAR_LIST)  // e.g. for v:oldfiles
296 	return &t_list_string;
297     if (tv->v_type == VAR_DICT)  // e.g. for v:completed_item
298 	return &t_dict_any;
299     return typval2type(tv, type_gap);
300 }
301 
302 
303 /*
304  * Return FAIL if "expected" and "actual" don't match.
305  */
306     int
307 check_typval_type(type_T *expected, typval_T *actual_tv, int argidx)
308 {
309     garray_T	type_list;
310     type_T	*actual_type;
311     int		res = FAIL;
312 
313     ga_init2(&type_list, sizeof(type_T *), 10);
314     actual_type = typval2type(actual_tv, &type_list);
315     if (actual_type != NULL)
316 	res = check_type(expected, actual_type, TRUE, argidx);
317     clear_type_list(&type_list);
318     return res;
319 }
320 
321     void
322 type_mismatch(type_T *expected, type_T *actual)
323 {
324     arg_type_mismatch(expected, actual, 0);
325 }
326 
327     void
328 arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
329 {
330     char *tofree1, *tofree2;
331     char *typename1 = type_name(expected, &tofree1);
332     char *typename2 = type_name(actual, &tofree2);
333 
334     if (argidx > 0)
335 	semsg(_(e_argument_nr_type_mismatch_expected_str_but_got_str),
336 						 argidx, typename1, typename2);
337     else
338 	semsg(_(e_type_mismatch_expected_str_but_got_str),
339 							 typename1, typename2);
340     vim_free(tofree1);
341     vim_free(tofree2);
342 }
343 
344 /*
345  * Check if the expected and actual types match.
346  * Does not allow for assigning "any" to a specific type.
347  * When "argidx" > 0 it is included in the error message.
348  */
349     int
350 check_type(type_T *expected, type_T *actual, int give_msg, int argidx)
351 {
352     int ret = OK;
353 
354     // When expected is "unknown" we accept any actual type.
355     // When expected is "any" we accept any actual type except "void".
356     if (expected->tt_type != VAR_UNKNOWN
357 	    && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
358 
359     {
360 	if (expected->tt_type != actual->tt_type)
361 	{
362 	    if (give_msg)
363 		arg_type_mismatch(expected, actual, argidx);
364 	    return FAIL;
365 	}
366 	if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
367 	{
368 	    // "unknown" is used for an empty list or dict
369 	    if (actual->tt_member != &t_unknown)
370 		ret = check_type(expected->tt_member, actual->tt_member,
371 								     FALSE, 0);
372 	}
373 	else if (expected->tt_type == VAR_FUNC)
374 	{
375 	    if (expected->tt_member != &t_unknown)
376 		ret = check_type(expected->tt_member, actual->tt_member,
377 								     FALSE, 0);
378 	    if (ret == OK && expected->tt_argcount != -1
379 		    && (actual->tt_argcount < expected->tt_min_argcount
380 			|| actual->tt_argcount > expected->tt_argcount))
381 		    ret = FAIL;
382 	    if (expected->tt_args != NULL && actual->tt_args != NULL)
383 	    {
384 		int i;
385 
386 		for (i = 0; i < expected->tt_argcount; ++i)
387 		    // Allow for using "any" argument type, lambda's have them.
388 		    if (actual->tt_args[i] != &t_any && check_type(
389 			    expected->tt_args[i], actual->tt_args[i], FALSE, 0)
390 								       == FAIL)
391 		    {
392 			ret = FAIL;
393 			break;
394 		    }
395 	    }
396 	}
397 	if (ret == FAIL && give_msg)
398 	    arg_type_mismatch(expected, actual, argidx);
399     }
400     return ret;
401 }
402 
403 /*
404  * Skip over a type definition and return a pointer to just after it.
405  * When "optional" is TRUE then a leading "?" is accepted.
406  */
407     char_u *
408 skip_type(char_u *start, int optional)
409 {
410     char_u *p = start;
411 
412     if (optional && *p == '?')
413 	++p;
414     while (ASCII_ISALNUM(*p) || *p == '_')
415 	++p;
416 
417     // Skip over "<type>"; this is permissive about white space.
418     if (*skipwhite(p) == '<')
419     {
420 	p = skipwhite(p);
421 	p = skip_type(skipwhite(p + 1), FALSE);
422 	p = skipwhite(p);
423 	if (*p == '>')
424 	    ++p;
425     }
426     else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
427 					     && STRNCMP("func", start, 4) == 0)
428     {
429 	if (*p == '(')
430 	{
431 	    // handle func(args): type
432 	    ++p;
433 	    while (*p != ')' && *p != NUL)
434 	    {
435 		char_u *sp = p;
436 
437 		if (STRNCMP(p, "...", 3) == 0)
438 		    p += 3;
439 		p = skip_type(p, TRUE);
440 		if (p == sp)
441 		    return p;  // syntax error
442 		if (*p == ',')
443 		    p = skipwhite(p + 1);
444 	    }
445 	    if (*p == ')')
446 	    {
447 		if (p[1] == ':')
448 		    p = skip_type(skipwhite(p + 2), FALSE);
449 		else
450 		    ++p;
451 	    }
452 	}
453 	else
454 	{
455 	    // handle func: return_type
456 	    p = skip_type(skipwhite(p + 1), FALSE);
457 	}
458     }
459 
460     return p;
461 }
462 
463 /*
464  * Parse the member type: "<type>" and return "type" with the member set.
465  * Use "type_gap" if a new type needs to be added.
466  * Returns NULL in case of failure.
467  */
468     static type_T *
469 parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
470 {
471     type_T  *member_type;
472     int	    prev_called_emsg = called_emsg;
473 
474     if (**arg != '<')
475     {
476 	if (*skipwhite(*arg) == '<')
477 	    semsg(_(e_no_white_space_allowed_before_str), "<");
478 	else
479 	    emsg(_(e_missing_type));
480 	return type;
481     }
482     *arg = skipwhite(*arg + 1);
483 
484     member_type = parse_type(arg, type_gap);
485 
486     *arg = skipwhite(*arg);
487     if (**arg != '>' && called_emsg == prev_called_emsg)
488     {
489 	emsg(_(e_missing_gt_after_type));
490 	return type;
491     }
492     ++*arg;
493 
494     if (type->tt_type == VAR_LIST)
495 	return get_list_type(member_type, type_gap);
496     return get_dict_type(member_type, type_gap);
497 }
498 
499 /*
500  * Parse a type at "arg" and advance over it.
501  * Return &t_any for failure.
502  */
503     type_T *
504 parse_type(char_u **arg, garray_T *type_gap)
505 {
506     char_u  *p = *arg;
507     size_t  len;
508 
509     // skip over the first word
510     while (ASCII_ISALNUM(*p) || *p == '_')
511 	++p;
512     len = p - *arg;
513 
514     switch (**arg)
515     {
516 	case 'a':
517 	    if (len == 3 && STRNCMP(*arg, "any", len) == 0)
518 	    {
519 		*arg += len;
520 		return &t_any;
521 	    }
522 	    break;
523 	case 'b':
524 	    if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
525 	    {
526 		*arg += len;
527 		return &t_bool;
528 	    }
529 	    if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
530 	    {
531 		*arg += len;
532 		return &t_blob;
533 	    }
534 	    break;
535 	case 'c':
536 	    if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
537 	    {
538 		*arg += len;
539 		return &t_channel;
540 	    }
541 	    break;
542 	case 'd':
543 	    if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
544 	    {
545 		*arg += len;
546 		return parse_type_member(arg, &t_dict_any, type_gap);
547 	    }
548 	    break;
549 	case 'f':
550 	    if (len == 5 && STRNCMP(*arg, "float", len) == 0)
551 	    {
552 #ifdef FEAT_FLOAT
553 		*arg += len;
554 		return &t_float;
555 #else
556 		emsg(_(e_this_vim_is_not_compiled_with_float_support));
557 		return &t_any;
558 #endif
559 	    }
560 	    if (len == 4 && STRNCMP(*arg, "func", len) == 0)
561 	    {
562 		type_T  *type;
563 		type_T  *ret_type = &t_unknown;
564 		int	argcount = -1;
565 		int	flags = 0;
566 		int	first_optional = -1;
567 		type_T	*arg_type[MAX_FUNC_ARGS + 1];
568 
569 		// func({type}, ...{type}): {type}
570 		*arg += len;
571 		if (**arg == '(')
572 		{
573 		    // "func" may or may not return a value, "func()" does
574 		    // not return a value.
575 		    ret_type = &t_void;
576 
577 		    p = ++*arg;
578 		    argcount = 0;
579 		    while (*p != NUL && *p != ')')
580 		    {
581 			if (*p == '?')
582 			{
583 			    if (first_optional == -1)
584 				first_optional = argcount;
585 			    ++p;
586 			}
587 			else if (STRNCMP(p, "...", 3) == 0)
588 			{
589 			    flags |= TTFLAG_VARARGS;
590 			    p += 3;
591 			}
592 			else if (first_optional != -1)
593 			{
594 			    emsg(_(e_mandatory_argument_after_optional_argument));
595 			    return &t_any;
596 			}
597 
598 			arg_type[argcount++] = parse_type(&p, type_gap);
599 
600 			// Nothing comes after "...{type}".
601 			if (flags & TTFLAG_VARARGS)
602 			    break;
603 
604 			if (*p != ',' && *skipwhite(p) == ',')
605 			{
606 			    semsg(_(e_no_white_space_allowed_before_str), ",");
607 			    return &t_any;
608 			}
609 			if (*p == ',')
610 			{
611 			    ++p;
612 			    if (!VIM_ISWHITE(*p))
613 			    {
614 				semsg(_(e_white_space_required_after_str), ",");
615 				return &t_any;
616 			    }
617 			}
618 			p = skipwhite(p);
619 			if (argcount == MAX_FUNC_ARGS)
620 			{
621 			    emsg(_(e_too_many_argument_types));
622 			    return &t_any;
623 			}
624 		    }
625 
626 		    p = skipwhite(p);
627 		    if (*p != ')')
628 		    {
629 			emsg(_(e_missing_close));
630 			return &t_any;
631 		    }
632 		    *arg = p + 1;
633 		}
634 		if (**arg == ':')
635 		{
636 		    // parse return type
637 		    ++*arg;
638 		    if (!VIM_ISWHITE(**arg))
639 			semsg(_(e_white_space_required_after_str), ":");
640 		    *arg = skipwhite(*arg);
641 		    ret_type = parse_type(arg, type_gap);
642 		}
643 		if (flags == 0 && first_optional == -1 && argcount <= 0)
644 		    type = get_func_type(ret_type, argcount, type_gap);
645 		else
646 		{
647 		    type = alloc_func_type(ret_type, argcount, type_gap);
648 		    type->tt_flags = flags;
649 		    if (argcount > 0)
650 		    {
651 			type->tt_argcount = argcount;
652 			type->tt_min_argcount = first_optional == -1
653 						   ? argcount : first_optional;
654 			if (func_type_add_arg_types(type, argcount,
655 							     type_gap) == FAIL)
656 			    return &t_any;
657 			mch_memmove(type->tt_args, arg_type,
658 						  sizeof(type_T *) * argcount);
659 		    }
660 		}
661 		return type;
662 	    }
663 	    break;
664 	case 'j':
665 	    if (len == 3 && STRNCMP(*arg, "job", len) == 0)
666 	    {
667 		*arg += len;
668 		return &t_job;
669 	    }
670 	    break;
671 	case 'l':
672 	    if (len == 4 && STRNCMP(*arg, "list", len) == 0)
673 	    {
674 		*arg += len;
675 		return parse_type_member(arg, &t_list_any, type_gap);
676 	    }
677 	    break;
678 	case 'n':
679 	    if (len == 6 && STRNCMP(*arg, "number", len) == 0)
680 	    {
681 		*arg += len;
682 		return &t_number;
683 	    }
684 	    break;
685 	case 's':
686 	    if (len == 6 && STRNCMP(*arg, "string", len) == 0)
687 	    {
688 		*arg += len;
689 		return &t_string;
690 	    }
691 	    break;
692 	case 'v':
693 	    if (len == 4 && STRNCMP(*arg, "void", len) == 0)
694 	    {
695 		*arg += len;
696 		return &t_void;
697 	    }
698 	    break;
699     }
700 
701     semsg(_(e_type_not_recognized_str), *arg);
702     return &t_any;
703 }
704 
705 /*
706  * Check if "type1" and "type2" are exactly the same.
707  */
708     static int
709 equal_type(type_T *type1, type_T *type2)
710 {
711     int i;
712 
713     if (type1->tt_type != type2->tt_type)
714 	return FALSE;
715     switch (type1->tt_type)
716     {
717 	case VAR_UNKNOWN:
718 	case VAR_ANY:
719 	case VAR_VOID:
720 	case VAR_SPECIAL:
721 	case VAR_BOOL:
722 	case VAR_NUMBER:
723 	case VAR_FLOAT:
724 	case VAR_STRING:
725 	case VAR_BLOB:
726 	case VAR_JOB:
727 	case VAR_CHANNEL:
728 	    break;  // not composite is always OK
729 	case VAR_LIST:
730 	case VAR_DICT:
731 	    return equal_type(type1->tt_member, type2->tt_member);
732 	case VAR_FUNC:
733 	case VAR_PARTIAL:
734 	    if (!equal_type(type1->tt_member, type2->tt_member)
735 		    || type1->tt_argcount != type2->tt_argcount)
736 		return FALSE;
737 	    if (type1->tt_argcount < 0
738 			   || type1->tt_args == NULL || type2->tt_args == NULL)
739 		return TRUE;
740 	    for (i = 0; i < type1->tt_argcount; ++i)
741 		if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
742 		    return FALSE;
743 	    return TRUE;
744     }
745     return TRUE;
746 }
747 
748 /*
749  * Find the common type of "type1" and "type2" and put it in "dest".
750  * "type2" and "dest" may be the same.
751  */
752     void
753 common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
754 {
755     if (equal_type(type1, type2))
756     {
757 	*dest = type1;
758 	return;
759     }
760 
761     if (type1->tt_type == type2->tt_type)
762     {
763 	if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
764 	{
765 	    type_T *common;
766 
767 	    common_type(type1->tt_member, type2->tt_member, &common, type_gap);
768 	    if (type1->tt_type == VAR_LIST)
769 		*dest = get_list_type(common, type_gap);
770 	    else
771 		*dest = get_dict_type(common, type_gap);
772 	    return;
773 	}
774 	if (type1->tt_type == VAR_FUNC)
775 	{
776 	    type_T *common;
777 
778 	    common_type(type1->tt_member, type2->tt_member, &common, type_gap);
779 	    if (type1->tt_argcount == type2->tt_argcount
780 						    && type1->tt_argcount >= 0)
781 	    {
782 		int argcount = type1->tt_argcount;
783 		int i;
784 
785 		*dest = alloc_func_type(common, argcount, type_gap);
786 		if (type1->tt_args != NULL && type2->tt_args != NULL)
787 		{
788 		    if (func_type_add_arg_types(*dest, argcount,
789 							     type_gap) == OK)
790 			for (i = 0; i < argcount; ++i)
791 			    common_type(type1->tt_args[i], type2->tt_args[i],
792 					       &(*dest)->tt_args[i], type_gap);
793 		}
794 	    }
795 	    else
796 		*dest = alloc_func_type(common, -1, type_gap);
797 	    return;
798 	}
799     }
800 
801     *dest = &t_any;
802 }
803 
804 /*
805  * Get the member type of a dict or list from the items on the stack.
806  * "stack_top" points just after the last type on the type stack.
807  * For a list "skip" is 1, for a dict "skip" is 2, keys are skipped.
808  * Returns &t_void for an empty list or dict.
809  * Otherwise finds the common type of all items.
810  */
811     type_T *
812 get_member_type_from_stack(
813 	type_T	    **stack_top,
814 	int	    count,
815 	int	    skip,
816 	garray_T    *type_gap)
817 {
818     int	    i;
819     type_T  *result;
820     type_T  *type;
821 
822     // Use "any" for an empty list or dict.
823     if (count == 0)
824 	return &t_void;
825 
826     // Use the first value type for the list member type, then find the common
827     // type from following items.
828     result = *(stack_top -(count * skip) + skip - 1);
829     for (i = 1; i < count; ++i)
830     {
831 	if (result == &t_any)
832 	    break;  // won't get more common
833 	type = *(stack_top -((count - i) * skip) + skip - 1);
834 	common_type(type, result, &result, type_gap);
835     }
836 
837     return result;
838 }
839 
840     char *
841 vartype_name(vartype_T type)
842 {
843     switch (type)
844     {
845 	case VAR_UNKNOWN: break;
846 	case VAR_ANY: return "any";
847 	case VAR_VOID: return "void";
848 	case VAR_SPECIAL: return "special";
849 	case VAR_BOOL: return "bool";
850 	case VAR_NUMBER: return "number";
851 	case VAR_FLOAT: return "float";
852 	case VAR_STRING: return "string";
853 	case VAR_BLOB: return "blob";
854 	case VAR_JOB: return "job";
855 	case VAR_CHANNEL: return "channel";
856 	case VAR_LIST: return "list";
857 	case VAR_DICT: return "dict";
858 
859 	case VAR_FUNC:
860 	case VAR_PARTIAL: return "func";
861     }
862     return "unknown";
863 }
864 
865 /*
866  * Return the name of a type.
867  * The result may be in allocated memory, in which case "tofree" is set.
868  */
869     char *
870 type_name(type_T *type, char **tofree)
871 {
872     char *name = vartype_name(type->tt_type);
873 
874     *tofree = NULL;
875     if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
876     {
877 	char *member_free;
878 	char *member_name = type_name(type->tt_member, &member_free);
879 	size_t len;
880 
881 	len = STRLEN(name) + STRLEN(member_name) + 3;
882 	*tofree = alloc(len);
883 	if (*tofree != NULL)
884 	{
885 	    vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
886 	    vim_free(member_free);
887 	    return *tofree;
888 	}
889     }
890     if (type->tt_type == VAR_FUNC)
891     {
892 	garray_T    ga;
893 	int	    i;
894 	int	    varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
895 
896 	ga_init2(&ga, 1, 100);
897 	if (ga_grow(&ga, 20) == FAIL)
898 	    return "[unknown]";
899 	*tofree = ga.ga_data;
900 	STRCPY(ga.ga_data, "func(");
901 	ga.ga_len += 5;
902 
903 	for (i = 0; i < type->tt_argcount; ++i)
904 	{
905 	    char *arg_free;
906 	    char *arg_type;
907 	    int  len;
908 
909 	    if (type->tt_args == NULL)
910 		arg_type = "[unknown]";
911 	    else
912 		arg_type = type_name(type->tt_args[i], &arg_free);
913 	    if (i > 0)
914 	    {
915 		STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
916 		ga.ga_len += 2;
917 	    }
918 	    len = (int)STRLEN(arg_type);
919 	    if (ga_grow(&ga, len + 8) == FAIL)
920 	    {
921 		vim_free(arg_free);
922 		return "[unknown]";
923 	    }
924 	    *tofree = ga.ga_data;
925 	    if (varargs && i == type->tt_argcount - 1)
926 	    {
927 		STRCPY((char *)ga.ga_data + ga.ga_len, "...");
928 		ga.ga_len += 3;
929 	    }
930 	    else if (i >= type->tt_min_argcount)
931 		*((char *)ga.ga_data + ga.ga_len++) = '?';
932 	    STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
933 	    ga.ga_len += len;
934 	    vim_free(arg_free);
935 	}
936 
937 	if (type->tt_member == &t_void)
938 	    STRCPY((char *)ga.ga_data + ga.ga_len, ")");
939 	else
940 	{
941 	    char *ret_free;
942 	    char *ret_name = type_name(type->tt_member, &ret_free);
943 	    int  len;
944 
945 	    len = (int)STRLEN(ret_name) + 4;
946 	    if (ga_grow(&ga, len) == FAIL)
947 	    {
948 		vim_free(ret_free);
949 		return "[unknown]";
950 	    }
951 	    *tofree = ga.ga_data;
952 	    STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
953 	    STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
954 	    vim_free(ret_free);
955 	}
956 	return ga.ga_data;
957     }
958 
959     return name;
960 }
961 
962 
963 #endif // FEAT_EVAL
964