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