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