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