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