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