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