/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* vim9type.c: handling of types
*/
#define USING_FLOAT_STUFF
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
#ifdef VMS
# include <float.h>
#endif
/*
* Allocate memory for a type_T and add the pointer to type_gap, so that it can
* be freed later.
*/
static type_T *
alloc_type(garray_T *type_gap)
{
type_T *type;
if (ga_grow(type_gap, 1) == FAIL)
return NULL;
type = ALLOC_CLEAR_ONE(type_T);
if (type != NULL)
{
((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
++type_gap->ga_len;
}
return type;
}
void
clear_type_list(garray_T *gap)
{
while (gap->ga_len > 0)
vim_free(((type_T **)gap->ga_data)[--gap->ga_len]);
ga_clear(gap);
}
type_T *
get_list_type(type_T *member_type, garray_T *type_gap)
{
type_T *type;
// recognize commonly used types
if (member_type->tt_type == VAR_ANY)
return &t_list_any;
if (member_type->tt_type == VAR_VOID
|| member_type->tt_type == VAR_UNKNOWN)
return &t_list_empty;
if (member_type->tt_type == VAR_BOOL)
return &t_list_bool;
if (member_type->tt_type == VAR_NUMBER)
return &t_list_number;
if (member_type->tt_type == VAR_STRING)
return &t_list_string;
// Not a common type, create a new entry.
type = alloc_type(type_gap);
if (type == NULL)
return &t_any;
type->tt_type = VAR_LIST;
type->tt_member = member_type;
type->tt_argcount = 0;
type->tt_args = NULL;
return type;
}
type_T *
get_dict_type(type_T *member_type, garray_T *type_gap)
{
type_T *type;
// recognize commonly used types
if (member_type->tt_type == VAR_ANY)
return &t_dict_any;
if (member_type->tt_type == VAR_VOID
|| member_type->tt_type == VAR_UNKNOWN)
return &t_dict_empty;
if (member_type->tt_type == VAR_BOOL)
return &t_dict_bool;
if (member_type->tt_type == VAR_NUMBER)
return &t_dict_number;
if (member_type->tt_type == VAR_STRING)
return &t_dict_string;
// Not a common type, create a new entry.
type = alloc_type(type_gap);
if (type == NULL)
return &t_any;
type->tt_type = VAR_DICT;
type->tt_member = member_type;
type->tt_argcount = 0;
type->tt_args = NULL;
return type;
}
/*
* Allocate a new type for a function.
*/
type_T *
alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
{
type_T *type = alloc_type(type_gap);
if (type == NULL)
return &t_any;
type->tt_type = VAR_FUNC;
type->tt_member = ret_type;
type->tt_argcount = argcount;
type->tt_args = NULL;
return type;
}
/*
* Get a function type, based on the return type "ret_type".
* If "argcount" is -1 or 0 a predefined type can be used.
* If "argcount" > 0 always create a new type, so that arguments can be added.
*/
type_T *
get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
{
// recognize commonly used types
if (argcount <= 0)
{
if (ret_type == &t_unknown)
{
// (argcount == 0) is not possible
return &t_func_unknown;
}
if (ret_type == &t_void)
{
if (argcount == 0)
return &t_func_0_void;
else
return &t_func_void;
}
if (ret_type == &t_any)
{
if (argcount == 0)
return &t_func_0_any;
else
return &t_func_any;
}
if (ret_type == &t_number)
{
if (argcount == 0)
return &t_func_0_number;
else
return &t_func_number;
}
if (ret_type == &t_string)
{
if (argcount == 0)
return &t_func_0_string;
else
return &t_func_string;
}
}
return alloc_func_type(ret_t