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