0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 9.1.0361: Vim9: vim9type.c is too complicated

Problem:  Vim9: vim9type.c is too complicated
Solution: Refactor a few functions in vim9type.c
          (Yegappan Lakshmanan)

closes: #14605

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Yegappan Lakshmanan 2024-04-21 14:48:57 +02:00 committed by Christian Brabandt
parent 84f6dc7ed2
commit 5eaa4d98f7
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09
2 changed files with 443 additions and 348 deletions

View File

@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
361,
/**/
360,
/**/

View File

@ -209,25 +209,21 @@ set_tv_type_recurse(type_T *type)
}
/*
* Set the type of "tv" to "type" if it is a list or dict.
* Set the type of Dict "d" to "type"
*/
void
set_tv_type(typval_T *tv, type_T *type)
static void
set_tv_type_dict(dict_T *d, type_T *type)
{
if (type->tt_type == VAR_ANY)
// If the variable type is "any", then keep the value type.
// e.g. var x: any = [1, 2] or var y: any = {v: 1}
if (d->dv_type == type)
return;
if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
{
dict_T *d = tv->vval.v_dict;
if (d->dv_type != type)
{
free_type(d->dv_type);
d->dv_type = alloc_type(type);
if (set_tv_type_recurse(type))
{
// Need to recursively set the type of dict items?
if (!set_tv_type_recurse(type))
return;
int todo = (int)d->dv_hashtab.ht_used;
hashitem_T *hi;
dictitem_T *di;
@ -241,26 +237,45 @@ set_tv_type(typval_T *tv, type_T *type)
set_tv_type(&di->di_tv, type->tt_member);
}
}
}
}
}
else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
{
list_T *l = tv->vval.v_list;
}
/*
* Set the type of List "l" to "type"
*/
static void
set_tv_type_list(list_T *l, type_T *type)
{
if (l->lv_type == type)
return;
if (l->lv_type != type)
{
free_type(l->lv_type);
l->lv_type = alloc_type(type);
if (l->lv_first != &range_list_item && set_tv_type_recurse(type))
{
// Need to recursively set the type of list items?
if (l->lv_first == &range_list_item || !set_tv_type_recurse(type))
return;
listitem_T *li;
FOR_ALL_LIST_ITEMS(l, li)
set_tv_type(&li->li_tv, type->tt_member);
}
}
}
}
/*
* Set the type of "tv" to "type" if it is a list or dict.
*/
void
set_tv_type(typval_T *tv, type_T *type)
{
if (type->tt_type == VAR_ANY)
// If the variable type is "any", then keep the value type.
// e.g. var x: any = [1, 2] or var y: any = {v: 1}
return;
if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
set_tv_type_dict(tv->vval.v_dict, type);
else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
set_tv_type_list(tv->vval.v_list, type);
}
type_T *
@ -1268,6 +1283,194 @@ parse_type_member(
return get_dict_type(member_type, type_gap);
}
/*
* Parse a "func" type at "*arg" and advance over it.
* When "give_error" is TRUE give error messages, otherwise be quiet.
* Return NULL for failure.
*/
static type_T *
parse_type_func(char_u **arg, size_t len, garray_T *type_gap, int give_error)
{
char_u *p;
type_T *type;
type_T *ret_type = &t_unknown;
int argcount = -1;
int flags = 0;
int first_optional = -1;
type_T *arg_type[MAX_FUNC_ARGS + 1];
// func({type}, ...{type}): {type}
*arg += len;
if (**arg == '(')
{
// "func" may or may not return a value, "func()" does
// not return a value.
ret_type = &t_void;
p = ++*arg;
argcount = 0;
while (*p != NUL && *p != ')')
{
if (*p == '?')
{
if (first_optional == -1)
first_optional = argcount;
++p;
}
else if (STRNCMP(p, "...", 3) == 0)
{
flags |= TTFLAG_VARARGS;
p += 3;
}
else if (first_optional != -1)
{
if (give_error)
emsg(_(e_mandatory_argument_after_optional_argument));
return NULL;
}
type = parse_type(&p, type_gap, give_error);
if (type == NULL)
return NULL;
if ((flags & TTFLAG_VARARGS) != 0 && type->tt_type != VAR_LIST)
{
char *tofree;
semsg(_(e_variable_arguments_type_must_be_list_str),
type_name(type, &tofree));
vim_free(tofree);
return NULL;
}
arg_type[argcount++] = type;
// Nothing comes after "...{type}".
if (flags & TTFLAG_VARARGS)
break;
if (*p != ',' && *skipwhite(p) == ',')
{
if (give_error)
semsg(_(e_no_white_space_allowed_before_str_str),
",", p);
return NULL;
}
if (*p == ',')
{
++p;
if (!VIM_ISWHITE(*p))
{
if (give_error)
semsg(_(e_white_space_required_after_str_str),
",", p - 1);
return NULL;
}
}
p = skipwhite(p);
if (argcount == MAX_FUNC_ARGS)
{
if (give_error)
emsg(_(e_too_many_argument_types));
return NULL;
}
}
p = skipwhite(p);
if (*p != ')')
{
if (give_error)
emsg(_(e_missing_closing_paren));
return NULL;
}
*arg = p + 1;
}
if (**arg == ':')
{
// parse return type
++*arg;
if (!VIM_ISWHITE(**arg) && give_error)
semsg(_(e_white_space_required_after_str_str), ":", *arg - 1);
*arg = skipwhite(*arg);
ret_type = parse_type(arg, type_gap, give_error);
if (ret_type == NULL)
return NULL;
}
if (flags == 0 && first_optional == -1 && argcount <= 0)
type = get_func_type(ret_type, argcount, type_gap);
else
{
type = alloc_func_type(ret_type, argcount, type_gap);
type->tt_flags = flags;
if (argcount > 0)
{
type->tt_argcount = argcount;
type->tt_min_argcount = first_optional == -1
? argcount : first_optional;
if (func_type_add_arg_types(type, argcount, type_gap) == FAIL)
return NULL;
mch_memmove(type->tt_args, arg_type, sizeof(type_T *) * argcount);
}
}
return type;
}
/*
* Parse a user defined type at "*arg" and advance over it.
* It can be a class or an interface or a typealias name, possibly imported.
* Return NULL if a type is not found.
*/
static type_T *
parse_type_user_defined(
char_u **arg,
size_t len,
garray_T *type_gap,
int give_error)
{
int did_emsg_before = did_emsg;
typval_T tv;
tv.v_type = VAR_UNKNOWN;
if (eval_variable_import(*arg, &tv) == OK)
{
if (tv.v_type == VAR_CLASS && tv.vval.v_class != NULL)
{
type_T *type = get_type_ptr(type_gap);
if (type != NULL)
{
// Although the name is that of a class or interface, the type
// uses will be an object.
type->tt_type = VAR_OBJECT;
type->tt_class = tv.vval.v_class;
clear_tv(&tv);
*arg += len;
// Skip over ".ClassName".
while (ASCII_ISALNUM(**arg) || **arg == '_' || **arg == '.')
++*arg;
return type;
}
}
else if (tv.v_type == VAR_TYPEALIAS)
{
// user defined type
type_T *type = copy_type(tv.vval.v_typealias->ta_type, type_gap);
*arg += len;
clear_tv(&tv);
// Skip over ".TypeName".
while (ASCII_ISALNUM(**arg) || **arg == '_' || **arg == '.')
++*arg;
return type;
}
clear_tv(&tv);
}
if (give_error && (did_emsg == did_emsg_before))
semsg(_(e_type_not_recognized_str), *arg);
return NULL;
}
/*
* Parse a type at "arg" and advance over it.
* When "give_error" is TRUE give error messages, otherwise be quiet.
@ -1327,130 +1530,7 @@ parse_type(char_u **arg, garray_T *type_gap, int give_error)
return &t_float;
}
if (len == 4 && STRNCMP(*arg, "func", len) == 0)
{
type_T *type;
type_T *ret_type = &t_unknown;
int argcount = -1;
int flags = 0;
int first_optional = -1;
type_T *arg_type[MAX_FUNC_ARGS + 1];
// func({type}, ...{type}): {type}
*arg += len;
if (**arg == '(')
{
// "func" may or may not return a value, "func()" does
// not return a value.
ret_type = &t_void;
p = ++*arg;
argcount = 0;
while (*p != NUL && *p != ')')
{
if (*p == '?')
{
if (first_optional == -1)
first_optional = argcount;
++p;
}
else if (STRNCMP(p, "...", 3) == 0)
{
flags |= TTFLAG_VARARGS;
p += 3;
}
else if (first_optional != -1)
{
if (give_error)
emsg(_(e_mandatory_argument_after_optional_argument));
return NULL;
}
type = parse_type(&p, type_gap, give_error);
if (type == NULL)
return NULL;
if ((flags & TTFLAG_VARARGS) != 0
&& type->tt_type != VAR_LIST)
{
char *tofree;
semsg(_(e_variable_arguments_type_must_be_list_str),
type_name(type, &tofree));
vim_free(tofree);
return NULL;
}
arg_type[argcount++] = type;
// Nothing comes after "...{type}".
if (flags & TTFLAG_VARARGS)
break;
if (*p != ',' && *skipwhite(p) == ',')
{
if (give_error)
semsg(_(e_no_white_space_allowed_before_str_str),
",", p);
return NULL;
}
if (*p == ',')
{
++p;
if (!VIM_ISWHITE(*p))
{
if (give_error)
semsg(_(e_white_space_required_after_str_str),
",", p - 1);
return NULL;
}
}
p = skipwhite(p);
if (argcount == MAX_FUNC_ARGS)
{
if (give_error)
emsg(_(e_too_many_argument_types));
return NULL;
}
}
p = skipwhite(p);
if (*p != ')')
{
if (give_error)
emsg(_(e_missing_closing_paren));
return NULL;
}
*arg = p + 1;
}
if (**arg == ':')
{
// parse return type
++*arg;
if (!VIM_ISWHITE(**arg) && give_error)
semsg(_(e_white_space_required_after_str_str),
":", *arg - 1);
*arg = skipwhite(*arg);
ret_type = parse_type(arg, type_gap, give_error);
if (ret_type == NULL)
return NULL;
}
if (flags == 0 && first_optional == -1 && argcount <= 0)
type = get_func_type(ret_type, argcount, type_gap);
else
{
type = alloc_func_type(ret_type, argcount, type_gap);
type->tt_flags = flags;
if (argcount > 0)
{
type->tt_argcount = argcount;
type->tt_min_argcount = first_optional == -1
? argcount : first_optional;
if (func_type_add_arg_types(type, argcount,
type_gap) == FAIL)
return NULL;
mch_memmove(type->tt_args, arg_type,
sizeof(type_T *) * argcount);
}
}
return type;
}
return parse_type_func(arg, len, type_gap, give_error);
break;
case 'j':
if (len == 3 && STRNCMP(*arg, "job", len) == 0)
@ -1490,50 +1570,8 @@ parse_type(char_u **arg, garray_T *type_gap, int give_error)
break;
}
// It can be a class or interface name, possibly imported.
int did_emsg_before = did_emsg;
typval_T tv;
tv.v_type = VAR_UNKNOWN;
if (eval_variable_import(*arg, &tv) == OK)
{
if (tv.v_type == VAR_CLASS && tv.vval.v_class != NULL)
{
type_T *type = get_type_ptr(type_gap);
if (type != NULL)
{
// Although the name is that of a class or interface, the type
// uses will be an object.
type->tt_type = VAR_OBJECT;
type->tt_class = tv.vval.v_class;
clear_tv(&tv);
*arg += len;
// Skip over ".ClassName".
while (ASCII_ISALNUM(**arg) || **arg == '_' || **arg == '.')
++*arg;
return type;
}
}
else if (tv.v_type == VAR_TYPEALIAS)
{
// user defined type
type_T *type = copy_type(tv.vval.v_typealias->ta_type, type_gap);
*arg += len;
clear_tv(&tv);
// Skip over ".TypeName".
while (ASCII_ISALNUM(**arg) || **arg == '_' || **arg == '.')
++*arg;
return type;
}
clear_tv(&tv);
}
if (give_error && (did_emsg == did_emsg_before))
semsg(_(e_type_not_recognized_str), *arg);
return NULL;
// User defined type
return parse_type_user_defined(arg, len, type_gap, give_error);
}
/*
@ -1590,46 +1628,16 @@ equal_type(type_T *type1, type_T *type2, int flags)
}
/*
* Find the common type of "type1" and "type2" and put it in "dest".
* "type2" and "dest" may be the same.
* Find the common type of "type1" (VAR_FUNC) and "type2" (VAR_FUNC) and put it
* in "dest". "type2" and "dest" may be the same.
*/
void
common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
static void
common_type_var_func(
type_T *type1,
type_T *type2,
type_T **dest,
garray_T *type_gap)
{
if (equal_type(type1, type2, 0))
{
*dest = type1;
return;
}
// If either is VAR_UNKNOWN use the other type. An empty list/dict has no
// specific type.
if (type1 == NULL || type1->tt_type == VAR_UNKNOWN)
{
*dest = type2;
return;
}
if (type2 == NULL || type2->tt_type == VAR_UNKNOWN)
{
*dest = type1;
return;
}
if (type1->tt_type == type2->tt_type)
{
if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
{
type_T *common;
common_type(type1->tt_member, type2->tt_member, &common, type_gap);
if (type1->tt_type == VAR_LIST)
*dest = get_list_type(common, type_gap);
else
*dest = get_dict_type(common, type_gap);
return;
}
if (type1->tt_type == VAR_FUNC)
{
type_T *common;
// When one of the types is t_func_unknown return the other one.
@ -1671,6 +1679,51 @@ common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
(*dest)->tt_min_argcount =
type1->tt_min_argcount < type2->tt_min_argcount
? type1->tt_min_argcount : type2->tt_min_argcount;
}
/*
* Find the common type of "type1" and "type2" and put it in "dest".
* "type2" and "dest" may be the same.
*/
void
common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
{
if (equal_type(type1, type2, 0))
{
*dest = type1;
return;
}
// If either is VAR_UNKNOWN use the other type. An empty list/dict has no
// specific type.
if (type1 == NULL || type1->tt_type == VAR_UNKNOWN)
{
*dest = type2;
return;
}
if (type2 == NULL || type2->tt_type == VAR_UNKNOWN)
{
*dest = type1;
return;
}
if (type1->tt_type == type2->tt_type)
{
if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
{
type_T *common;
common_type(type1->tt_member, type2->tt_member, &common, type_gap);
if (type1->tt_type == VAR_LIST)
*dest = get_list_type(common, type_gap);
else
*dest = get_dict_type(common, type_gap);
return;
}
if (type1->tt_type == VAR_FUNC)
{
common_type_var_func(type1, type2, dest, type_gap);
return;
}
}
@ -1825,28 +1878,20 @@ vartype_name(vartype_T type)
}
/*
* Return the name of a type.
* Return the type name of a List (list<type>) or Dict (dict<type>).
* The result may be in allocated memory, in which case "tofree" is set.
*/
char *
type_name(type_T *type, char **tofree)
static char *
type_name_list_or_dict(char *name, type_T *type, char **tofree)
{
char *name;
char *arg_free = NULL;
*tofree = NULL;
if (type == NULL)
return "[unknown]";
name = vartype_name(type->tt_type);
if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
{
char *member_free;
char *member_name;
if (type->tt_member->tt_type == VAR_UNKNOWN)
member_name = type_name(&t_any, &member_free);
else
member_name = type_name(type->tt_member, &member_free);
size_t len = STRLEN(name) + STRLEN(member_name) + 3;
*tofree = alloc(len);
if (*tofree != NULL)
@ -1855,11 +1900,19 @@ type_name(type_T *type, char **tofree)
vim_free(member_free);
return *tofree;
}
}
if (type->tt_type == VAR_OBJECT || type->tt_type == VAR_CLASS)
{
return name;
}
/*
* Return the type name of a Class (class<name>) or Object (object<name>).
* The result may be in allocated memory, in which case "tofree" is set.
*/
static char *
type_name_class_or_obj(char *name, type_T *type, char **tofree)
{
char_u *class_name;
if (type->tt_class != NULL)
{
class_name = type->tt_class->class_name;
@ -1868,6 +1921,7 @@ type_name(type_T *type, char **tofree)
}
else
class_name = (char_u *)"Unknown";
size_t len = STRLEN(name) + STRLEN(class_name) + 3;
*tofree = alloc(len);
if (*tofree != NULL)
@ -1875,13 +1929,21 @@ type_name(type_T *type, char **tofree)
vim_snprintf(*tofree, len, "%s<%s>", name, class_name);
return *tofree;
}
}
if (type->tt_type == VAR_FUNC)
{
return name;
}
/*
* Return the type name of a functio.
* The result may be in allocated memory, in which case "tofree" is set.
*/
static char *
type_name_func(type_T *type, char **tofree)
{
garray_T ga;
int i;
int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
char *arg_free = NULL;
ga_init2(&ga, 1, 100);
if (ga_grow(&ga, 20) == FAIL)
@ -1939,6 +2001,37 @@ failed:
vim_free(arg_free);
ga_clear(&ga);
return "[unknown]";
}
/*
* Return the name of a type.
* The result may be in allocated memory, in which case "tofree" is set.
*/
char *
type_name(type_T *type, char **tofree)
{
char *name;
*tofree = NULL;
if (type == NULL)
return "[unknown]";
name = vartype_name(type->tt_type);
switch (type->tt_type)
{
case VAR_LIST:
case VAR_DICT:
return type_name_list_or_dict(name, type, tofree);
case VAR_CLASS:
case VAR_OBJECT:
return type_name_class_or_obj(name, type, tofree);
case VAR_FUNC:
return type_name_func(type, tofree);
default:
break;
}
return name;