xref: /vim-8.2.3635/src/vim9type.c (revision b7480cd8)
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_min_argcount != -1
530 		    && (actual->tt_argcount == -1
531 			|| (actual->tt_argcount < expected->tt_min_argcount
532 			    || actual->tt_argcount > expected->tt_argcount)))
533 		ret = FAIL;
534 	    if (ret == OK && expected->tt_args != NULL
535 						    && actual->tt_args != NULL)
536 	    {
537 		int i;
538 
539 		for (i = 0; i < expected->tt_argcount; ++i)
540 		    // Allow for using "any" argument type, lambda's have them.
541 		    if (actual->tt_args[i] != &t_any && check_type(
542 			    expected->tt_args[i], actual->tt_args[i], FALSE,
543 								where) == FAIL)
544 		    {
545 			ret = FAIL;
546 			break;
547 		    }
548 	    }
549 	}
550 	if (ret == FAIL && give_msg)
551 	    type_mismatch_where(expected, actual, where);
552     }
553     return ret;
554 }
555 
556 /*
557  * Check that the arguments of "type" match "argvars[argcount]".
558  * Return OK/FAIL.
559  */
560     int
561 check_argument_types(
562 	type_T	    *type,
563 	typval_T    *argvars,
564 	int	    argcount,
565 	char_u	    *name)
566 {
567     int	    varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
568     int	    i;
569 
570     if (type->tt_type != VAR_FUNC && type->tt_type != VAR_PARTIAL)
571 	return OK;  // just in case
572     if (argcount < type->tt_min_argcount - varargs)
573     {
574 	semsg(_(e_toofewarg), name);
575 	return FAIL;
576     }
577     if (!varargs && type->tt_argcount >= 0 && argcount > type->tt_argcount)
578     {
579 	semsg(_(e_toomanyarg), name);
580 	return FAIL;
581     }
582     if (type->tt_args == NULL)
583 	return OK;  // cannot check
584 
585 
586     for (i = 0; i < argcount; ++i)
587     {
588 	type_T	*expected;
589 
590 	if (varargs && i >= type->tt_argcount - 1)
591 	    expected = type->tt_args[type->tt_argcount - 1]->tt_member;
592 	else
593 	    expected = type->tt_args[i];
594 	if (check_typval_arg_type(expected, &argvars[i], i + 1) == FAIL)
595 	    return FAIL;
596     }
597     return OK;
598 }
599 
600 /*
601  * Skip over a type definition and return a pointer to just after it.
602  * When "optional" is TRUE then a leading "?" is accepted.
603  */
604     char_u *
605 skip_type(char_u *start, int optional)
606 {
607     char_u *p = start;
608 
609     if (optional && *p == '?')
610 	++p;
611     while (ASCII_ISALNUM(*p) || *p == '_')
612 	++p;
613 
614     // Skip over "<type>"; this is permissive about white space.
615     if (*skipwhite(p) == '<')
616     {
617 	p = skipwhite(p);
618 	p = skip_type(skipwhite(p + 1), FALSE);
619 	p = skipwhite(p);
620 	if (*p == '>')
621 	    ++p;
622     }
623     else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1])))
624 					     && STRNCMP("func", start, 4) == 0)
625     {
626 	if (*p == '(')
627 	{
628 	    // handle func(args): type
629 	    ++p;
630 	    while (*p != ')' && *p != NUL)
631 	    {
632 		char_u *sp = p;
633 
634 		if (STRNCMP(p, "...", 3) == 0)
635 		    p += 3;
636 		p = skip_type(p, TRUE);
637 		if (p == sp)
638 		    return p;  // syntax error
639 		if (*p == ',')
640 		    p = skipwhite(p + 1);
641 	    }
642 	    if (*p == ')')
643 	    {
644 		if (p[1] == ':')
645 		    p = skip_type(skipwhite(p + 2), FALSE);
646 		else
647 		    ++p;
648 	    }
649 	}
650 	else
651 	{
652 	    // handle func: return_type
653 	    p = skip_type(skipwhite(p + 1), FALSE);
654 	}
655     }
656 
657     return p;
658 }
659 
660 /*
661  * Parse the member type: "<type>" and return "type" with the member set.
662  * Use "type_gap" if a new type needs to be added.
663  * Returns NULL in case of failure.
664  */
665     static type_T *
666 parse_type_member(
667 	char_u	    **arg,
668 	type_T	    *type,
669 	garray_T    *type_gap,
670 	int	    give_error)
671 {
672     type_T  *member_type;
673     int	    prev_called_emsg = called_emsg;
674 
675     if (**arg != '<')
676     {
677 	if (give_error)
678 	{
679 	    if (*skipwhite(*arg) == '<')
680 		semsg(_(e_no_white_space_allowed_before_str_str), "<", *arg);
681 	    else
682 		emsg(_(e_missing_type));
683 	}
684 	return NULL;
685     }
686     *arg = skipwhite(*arg + 1);
687 
688     member_type = parse_type(arg, type_gap, give_error);
689     if (member_type == NULL)
690 	return NULL;
691 
692     *arg = skipwhite(*arg);
693     if (**arg != '>' && called_emsg == prev_called_emsg)
694     {
695 	if (give_error)
696 	    emsg(_(e_missing_gt_after_type));
697 	return NULL;
698     }
699     ++*arg;
700 
701     if (type->tt_type == VAR_LIST)
702 	return get_list_type(member_type, type_gap);
703     return get_dict_type(member_type, type_gap);
704 }
705 
706 /*
707  * Parse a type at "arg" and advance over it.
708  * When "give_error" is TRUE give error messages, otherwise be quiet.
709  * Return NULL for failure.
710  */
711     type_T *
712 parse_type(char_u **arg, garray_T *type_gap, int give_error)
713 {
714     char_u  *p = *arg;
715     size_t  len;
716 
717     // skip over the first word
718     while (ASCII_ISALNUM(*p) || *p == '_')
719 	++p;
720     len = p - *arg;
721 
722     switch (**arg)
723     {
724 	case 'a':
725 	    if (len == 3 && STRNCMP(*arg, "any", len) == 0)
726 	    {
727 		*arg += len;
728 		return &t_any;
729 	    }
730 	    break;
731 	case 'b':
732 	    if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
733 	    {
734 		*arg += len;
735 		return &t_bool;
736 	    }
737 	    if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
738 	    {
739 		*arg += len;
740 		return &t_blob;
741 	    }
742 	    break;
743 	case 'c':
744 	    if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
745 	    {
746 		*arg += len;
747 		return &t_channel;
748 	    }
749 	    break;
750 	case 'd':
751 	    if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
752 	    {
753 		*arg += len;
754 		return parse_type_member(arg, &t_dict_any,
755 							 type_gap, give_error);
756 	    }
757 	    break;
758 	case 'f':
759 	    if (len == 5 && STRNCMP(*arg, "float", len) == 0)
760 	    {
761 #ifdef FEAT_FLOAT
762 		*arg += len;
763 		return &t_float;
764 #else
765 		if (give_error)
766 		    emsg(_(e_this_vim_is_not_compiled_with_float_support));
767 		return NULL;
768 #endif
769 	    }
770 	    if (len == 4 && STRNCMP(*arg, "func", len) == 0)
771 	    {
772 		type_T  *type;
773 		type_T  *ret_type = &t_unknown;
774 		int	argcount = -1;
775 		int	flags = 0;
776 		int	first_optional = -1;
777 		type_T	*arg_type[MAX_FUNC_ARGS + 1];
778 
779 		// func({type}, ...{type}): {type}
780 		*arg += len;
781 		if (**arg == '(')
782 		{
783 		    // "func" may or may not return a value, "func()" does
784 		    // not return a value.
785 		    ret_type = &t_void;
786 
787 		    p = ++*arg;
788 		    argcount = 0;
789 		    while (*p != NUL && *p != ')')
790 		    {
791 			if (*p == '?')
792 			{
793 			    if (first_optional == -1)
794 				first_optional = argcount;
795 			    ++p;
796 			}
797 			else if (STRNCMP(p, "...", 3) == 0)
798 			{
799 			    flags |= TTFLAG_VARARGS;
800 			    p += 3;
801 			}
802 			else if (first_optional != -1)
803 			{
804 			    if (give_error)
805 				emsg(_(e_mandatory_argument_after_optional_argument));
806 			    return NULL;
807 			}
808 
809 			type = parse_type(&p, type_gap, give_error);
810 			if (type == NULL)
811 			    return NULL;
812 			arg_type[argcount++] = type;
813 
814 			// Nothing comes after "...{type}".
815 			if (flags & TTFLAG_VARARGS)
816 			    break;
817 
818 			if (*p != ',' && *skipwhite(p) == ',')
819 			{
820 			    if (give_error)
821 				semsg(_(e_no_white_space_allowed_before_str_str),
822 								       ",", p);
823 			    return NULL;
824 			}
825 			if (*p == ',')
826 			{
827 			    ++p;
828 			    if (!VIM_ISWHITE(*p))
829 			    {
830 				if (give_error)
831 				    semsg(_(e_white_space_required_after_str_str),
832 								   ",", p - 1);
833 				return NULL;
834 			    }
835 			}
836 			p = skipwhite(p);
837 			if (argcount == MAX_FUNC_ARGS)
838 			{
839 			    if (give_error)
840 				emsg(_(e_too_many_argument_types));
841 			    return NULL;
842 			}
843 		    }
844 
845 		    p = skipwhite(p);
846 		    if (*p != ')')
847 		    {
848 			if (give_error)
849 			    emsg(_(e_missing_close));
850 			return NULL;
851 		    }
852 		    *arg = p + 1;
853 		}
854 		if (**arg == ':')
855 		{
856 		    // parse return type
857 		    ++*arg;
858 		    if (!VIM_ISWHITE(**arg) && give_error)
859 			semsg(_(e_white_space_required_after_str_str),
860 								":", *arg - 1);
861 		    *arg = skipwhite(*arg);
862 		    ret_type = parse_type(arg, type_gap, give_error);
863 		    if (ret_type == NULL)
864 			return NULL;
865 		}
866 		if (flags == 0 && first_optional == -1 && argcount <= 0)
867 		    type = get_func_type(ret_type, argcount, type_gap);
868 		else
869 		{
870 		    type = alloc_func_type(ret_type, argcount, type_gap);
871 		    type->tt_flags = flags;
872 		    if (argcount > 0)
873 		    {
874 			type->tt_argcount = argcount;
875 			type->tt_min_argcount = first_optional == -1
876 						   ? argcount : first_optional;
877 			if (func_type_add_arg_types(type, argcount,
878 							     type_gap) == FAIL)
879 			    return NULL;
880 			mch_memmove(type->tt_args, arg_type,
881 						  sizeof(type_T *) * argcount);
882 		    }
883 		}
884 		return type;
885 	    }
886 	    break;
887 	case 'j':
888 	    if (len == 3 && STRNCMP(*arg, "job", len) == 0)
889 	    {
890 		*arg += len;
891 		return &t_job;
892 	    }
893 	    break;
894 	case 'l':
895 	    if (len == 4 && STRNCMP(*arg, "list", len) == 0)
896 	    {
897 		*arg += len;
898 		return parse_type_member(arg, &t_list_any,
899 							 type_gap, give_error);
900 	    }
901 	    break;
902 	case 'n':
903 	    if (len == 6 && STRNCMP(*arg, "number", len) == 0)
904 	    {
905 		*arg += len;
906 		return &t_number;
907 	    }
908 	    break;
909 	case 's':
910 	    if (len == 6 && STRNCMP(*arg, "string", len) == 0)
911 	    {
912 		*arg += len;
913 		return &t_string;
914 	    }
915 	    break;
916 	case 'v':
917 	    if (len == 4 && STRNCMP(*arg, "void", len) == 0)
918 	    {
919 		*arg += len;
920 		return &t_void;
921 	    }
922 	    break;
923     }
924 
925     if (give_error)
926 	semsg(_(e_type_not_recognized_str), *arg);
927     return NULL;
928 }
929 
930 /*
931  * Check if "type1" and "type2" are exactly the same.
932  */
933     int
934 equal_type(type_T *type1, type_T *type2)
935 {
936     int i;
937 
938     if (type1 == NULL || type2 == NULL)
939 	return FALSE;
940     if (type1->tt_type != type2->tt_type)
941 	return FALSE;
942     switch (type1->tt_type)
943     {
944 	case VAR_UNKNOWN:
945 	case VAR_ANY:
946 	case VAR_VOID:
947 	case VAR_SPECIAL:
948 	case VAR_BOOL:
949 	case VAR_NUMBER:
950 	case VAR_FLOAT:
951 	case VAR_STRING:
952 	case VAR_BLOB:
953 	case VAR_JOB:
954 	case VAR_CHANNEL:
955 	case VAR_INSTR:
956 	    break;  // not composite is always OK
957 	case VAR_LIST:
958 	case VAR_DICT:
959 	    return equal_type(type1->tt_member, type2->tt_member);
960 	case VAR_FUNC:
961 	case VAR_PARTIAL:
962 	    if (!equal_type(type1->tt_member, type2->tt_member)
963 		    || type1->tt_argcount != type2->tt_argcount)
964 		return FALSE;
965 	    if (type1->tt_argcount < 0
966 			   || type1->tt_args == NULL || type2->tt_args == NULL)
967 		return TRUE;
968 	    for (i = 0; i < type1->tt_argcount; ++i)
969 		if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
970 		    return FALSE;
971 	    return TRUE;
972     }
973     return TRUE;
974 }
975 
976 /*
977  * Find the common type of "type1" and "type2" and put it in "dest".
978  * "type2" and "dest" may be the same.
979  */
980     void
981 common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
982 {
983     if (equal_type(type1, type2))
984     {
985 	*dest = type1;
986 	return;
987     }
988 
989     // If either is VAR_UNKNOWN use the other type.  An empty list/dict has no
990     // specific type.
991     if (type1 == NULL || type1->tt_type == VAR_UNKNOWN)
992     {
993 	*dest = type2;
994 	return;
995     }
996     if (type2 == NULL || type2->tt_type == VAR_UNKNOWN)
997     {
998 	*dest = type1;
999 	return;
1000     }
1001 
1002     if (type1->tt_type == type2->tt_type)
1003     {
1004 	if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
1005 	{
1006 	    type_T *common;
1007 
1008 	    common_type(type1->tt_member, type2->tt_member, &common, type_gap);
1009 	    if (type1->tt_type == VAR_LIST)
1010 		*dest = get_list_type(common, type_gap);
1011 	    else
1012 		*dest = get_dict_type(common, type_gap);
1013 	    return;
1014 	}
1015 	if (type1->tt_type == VAR_FUNC)
1016 	{
1017 	    type_T *common;
1018 
1019 	    common_type(type1->tt_member, type2->tt_member, &common, type_gap);
1020 	    if (type1->tt_argcount == type2->tt_argcount
1021 						    && type1->tt_argcount >= 0)
1022 	    {
1023 		int argcount = type1->tt_argcount;
1024 		int i;
1025 
1026 		*dest = alloc_func_type(common, argcount, type_gap);
1027 		if (type1->tt_args != NULL && type2->tt_args != NULL)
1028 		{
1029 		    if (func_type_add_arg_types(*dest, argcount,
1030 							     type_gap) == OK)
1031 			for (i = 0; i < argcount; ++i)
1032 			    common_type(type1->tt_args[i], type2->tt_args[i],
1033 					       &(*dest)->tt_args[i], type_gap);
1034 		}
1035 	    }
1036 	    else
1037 		// Use -1 for "tt_argcount" to indicate an unknown number of
1038 		// arguments.
1039 		*dest = alloc_func_type(common, -1, type_gap);
1040 
1041 	    // Use the minimum of min_argcount.
1042 	    (*dest)->tt_min_argcount =
1043 			type1->tt_min_argcount < type2->tt_min_argcount
1044 			     ? type1->tt_min_argcount : type2->tt_min_argcount;
1045 	    return;
1046 	}
1047     }
1048 
1049     *dest = &t_any;
1050 }
1051 
1052 /*
1053  * Get the member type of a dict or list from the items on the stack.
1054  * "stack_top" points just after the last type on the type stack.
1055  * For a list "skip" is 1, for a dict "skip" is 2, keys are skipped.
1056  * Returns &t_void for an empty list or dict.
1057  * Otherwise finds the common type of all items.
1058  */
1059     type_T *
1060 get_member_type_from_stack(
1061 	type_T	    **stack_top,
1062 	int	    count,
1063 	int	    skip,
1064 	garray_T    *type_gap)
1065 {
1066     int	    i;
1067     type_T  *result;
1068     type_T  *type;
1069 
1070     // Use "any" for an empty list or dict.
1071     if (count == 0)
1072 	return &t_unknown;
1073 
1074     // Use the first value type for the list member type, then find the common
1075     // type from following items.
1076     result = *(stack_top -(count * skip) + skip - 1);
1077     for (i = 1; i < count; ++i)
1078     {
1079 	if (result == &t_any)
1080 	    break;  // won't get more common
1081 	type = *(stack_top -((count - i) * skip) + skip - 1);
1082 	common_type(type, result, &result, type_gap);
1083     }
1084 
1085     return result;
1086 }
1087 
1088     char *
1089 vartype_name(vartype_T type)
1090 {
1091     switch (type)
1092     {
1093 	case VAR_UNKNOWN: break;
1094 	case VAR_ANY: return "any";
1095 	case VAR_VOID: return "void";
1096 	case VAR_SPECIAL: return "special";
1097 	case VAR_BOOL: return "bool";
1098 	case VAR_NUMBER: return "number";
1099 	case VAR_FLOAT: return "float";
1100 	case VAR_STRING: return "string";
1101 	case VAR_BLOB: return "blob";
1102 	case VAR_JOB: return "job";
1103 	case VAR_CHANNEL: return "channel";
1104 	case VAR_LIST: return "list";
1105 	case VAR_DICT: return "dict";
1106 	case VAR_INSTR: return "instr";
1107 
1108 	case VAR_FUNC:
1109 	case VAR_PARTIAL: return "func";
1110     }
1111     return "unknown";
1112 }
1113 
1114 /*
1115  * Return the name of a type.
1116  * The result may be in allocated memory, in which case "tofree" is set.
1117  */
1118     char *
1119 type_name(type_T *type, char **tofree)
1120 {
1121     char *name;
1122 
1123     *tofree = NULL;
1124     if (type == NULL)
1125 	return "[unknown]";
1126     name = vartype_name(type->tt_type);
1127     if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
1128     {
1129 	char *member_free;
1130 	char *member_name = type_name(type->tt_member, &member_free);
1131 	size_t len;
1132 
1133 	len = STRLEN(name) + STRLEN(member_name) + 3;
1134 	*tofree = alloc(len);
1135 	if (*tofree != NULL)
1136 	{
1137 	    vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
1138 	    vim_free(member_free);
1139 	    return *tofree;
1140 	}
1141     }
1142     if (type->tt_type == VAR_FUNC)
1143     {
1144 	garray_T    ga;
1145 	int	    i;
1146 	int	    varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1147 
1148 	ga_init2(&ga, 1, 100);
1149 	if (ga_grow(&ga, 20) == FAIL)
1150 	    return "[unknown]";
1151 	STRCPY(ga.ga_data, "func(");
1152 	ga.ga_len += 5;
1153 
1154 	for (i = 0; i < type->tt_argcount; ++i)
1155 	{
1156 	    char *arg_free;
1157 	    char *arg_type;
1158 	    int  len;
1159 
1160 	    if (type->tt_args == NULL)
1161 		arg_type = "[unknown]";
1162 	    else
1163 		arg_type = type_name(type->tt_args[i], &arg_free);
1164 	    if (i > 0)
1165 	    {
1166 		STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
1167 		ga.ga_len += 2;
1168 	    }
1169 	    len = (int)STRLEN(arg_type);
1170 	    if (ga_grow(&ga, len + 8) == FAIL)
1171 	    {
1172 		vim_free(arg_free);
1173 		ga_clear(&ga);
1174 		return "[unknown]";
1175 	    }
1176 	    if (varargs && i == type->tt_argcount - 1)
1177 		ga_concat(&ga, (char_u *)"...");
1178 	    else if (i >= type->tt_min_argcount)
1179 		*((char *)ga.ga_data + ga.ga_len++) = '?';
1180 	    ga_concat(&ga, (char_u *)arg_type);
1181 	    vim_free(arg_free);
1182 	}
1183 	if (type->tt_argcount < 0)
1184 	    // any number of arguments
1185 	    ga_concat(&ga, (char_u *)"...");
1186 
1187 	if (type->tt_member == &t_void)
1188 	    STRCPY((char *)ga.ga_data + ga.ga_len, ")");
1189 	else
1190 	{
1191 	    char *ret_free;
1192 	    char *ret_name = type_name(type->tt_member, &ret_free);
1193 	    int  len;
1194 
1195 	    len = (int)STRLEN(ret_name) + 4;
1196 	    if (ga_grow(&ga, len) == FAIL)
1197 	    {
1198 		vim_free(ret_free);
1199 		ga_clear(&ga);
1200 		return "[unknown]";
1201 	    }
1202 	    STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
1203 	    STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
1204 	    vim_free(ret_free);
1205 	}
1206 	*tofree = ga.ga_data;
1207 	return ga.ga_data;
1208     }
1209 
1210     return name;
1211 }
1212 
1213 /*
1214  * "typename(expr)" function
1215  */
1216     void
1217 f_typename(typval_T *argvars, typval_T *rettv)
1218 {
1219     garray_T	type_list;
1220     type_T	*type;
1221     char	*tofree;
1222     char	*name;
1223 
1224     rettv->v_type = VAR_STRING;
1225     ga_init2(&type_list, sizeof(type_T *), 10);
1226     type = typval2type(argvars, get_copyID(), &type_list, TRUE);
1227     name = type_name(type, &tofree);
1228     if (tofree != NULL)
1229 	rettv->vval.v_string = (char_u *)tofree;
1230     else
1231     {
1232 	rettv->vval.v_string = vim_strsave((char_u *)name);
1233 	vim_free(tofree);
1234     }
1235     clear_type_list(&type_list);
1236 }
1237 
1238 #endif // FEAT_EVAL
1239