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