1
0
forked from aniani/vim

patch 9.1.0349: Vim9: need static type for typealias

Problem:  Vim9: need static type for typealias
Solution: Refactor the typval2type() function and add a static type for
          typealias (Yegappan Lakshmanan)

closes: #14582

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-18 21:33:27 +02:00
committed by Christian Brabandt
parent baedc998b0
commit 76ba252e61
4 changed files with 285 additions and 167 deletions

View File

@@ -540,7 +540,10 @@ EXTERN int garbage_collect_at_exit INIT(= FALSE);
#define t_class (static_types[84]) #define t_class (static_types[84])
#define t_const_class (static_types[85]) #define t_const_class (static_types[85])
EXTERN type_T static_types[86] #define t_typealias (static_types[86])
#define t_const_typealias (static_types[87])
EXTERN type_T static_types[88]
#ifdef DO_INIT #ifdef DO_INIT
= { = {
// 0: t_unknown // 0: t_unknown
@@ -714,6 +717,10 @@ EXTERN type_T static_types[86]
// 84: t_class // 84: t_class
{VAR_CLASS, 0, 0, TTFLAG_STATIC, NULL, NULL, NULL}, {VAR_CLASS, 0, 0, TTFLAG_STATIC, NULL, NULL, NULL},
{VAR_CLASS, 0, 0, TTFLAG_STATIC|TTFLAG_CONST, NULL, NULL, NULL}, {VAR_CLASS, 0, 0, TTFLAG_STATIC|TTFLAG_CONST, NULL, NULL, NULL},
// 86: t_typealias
{VAR_TYPEALIAS, 0, 0, TTFLAG_STATIC, NULL, NULL, NULL},
{VAR_TYPEALIAS, 0, 0, TTFLAG_STATIC|TTFLAG_CONST, NULL, NULL, NULL},
} }
#endif #endif
; ;

View File

@@ -4783,6 +4783,8 @@ def Test_typename()
if has('channel') if has('channel')
assert_equal('channel', test_null_channel()->typename()) assert_equal('channel', test_null_channel()->typename())
endif endif
assert_equal('class<Unknown>', typename(null_class))
assert_equal('object<Unknown>', typename(null_object))
enddef enddef
def Test_undofile() def Test_undofile()

View File

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

View File

@@ -418,7 +418,127 @@ type_any_or_unknown(type_T *type)
} }
/* /*
* Get a type_T for a partial typval in "tv". * Get a type_T for a "special" typval in "tv".
*/
static type_T *
special_typval2type(typval_T *tv)
{
switch (tv->vval.v_number)
{
case VVAL_NULL:
return &t_null;
case VVAL_NONE:
return &t_none;
case VVAL_TRUE:
case VVAL_FALSE:
return &t_bool;
default:
return &t_unknown;
}
}
/*
* Get a type_T for a List typval in "tv".
* When "flags" has TVTT_DO_MEMBER also get the member type, otherwise use
* "any".
* When "flags" has TVTT_MORE_SPECIFIC get the more specific member type if it
* is "any".
*/
static type_T *
list_typval2type(typval_T *tv, int copyID, garray_T *type_gap, int flags)
{
list_T *l = tv->vval.v_list;
listitem_T *li;
type_T *member_type = NULL;
// An empty list has type list<unknown>, unless the type was specified
// and is not list<any>. This matters when assigning to a variable
// with a specific list type.
if (l == NULL || (l->lv_first == NULL
&& (l->lv_type == NULL || l->lv_type->tt_member == &t_any)))
return &t_list_empty;
if ((flags & TVTT_DO_MEMBER) == 0)
return &t_list_any;
// If the type is list<any> go through the members, it may end up a
// more specific type.
if (l->lv_type != NULL && (l->lv_first == NULL
|| (flags & TVTT_MORE_SPECIFIC) == 0
|| l->lv_type->tt_member != &t_any))
// make a copy, lv_type may be freed if the list is freed
return copy_type_deep(l->lv_type, type_gap);
if (l->lv_first == &range_list_item)
return &t_list_number;
if (l->lv_copyID == copyID)
// avoid recursion
return &t_list_any;
l->lv_copyID = copyID;
// Use the common type of all members.
member_type = typval2type(&l->lv_first->li_tv, copyID, type_gap,
TVTT_DO_MEMBER);
for (li = l->lv_first->li_next; li != NULL; li = li->li_next)
common_type(typval2type(&li->li_tv, copyID, type_gap, TVTT_DO_MEMBER),
member_type, &member_type, type_gap);
return get_list_type(member_type, type_gap);
}
/*
* Get a type_T for a Dict typval in "tv".
* When "flags" has TVTT_DO_MEMBER also get the member type, otherwise use
* "any".
* When "flags" has TVTT_MORE_SPECIFIC get the more specific member type if it
* is "any".
*/
static type_T *
dict_typval2type(typval_T *tv, int copyID, garray_T *type_gap, int flags)
{
dict_iterator_T iter;
typval_T *value;
dict_T *d = tv->vval.v_dict;
type_T *member_type = NULL;
if (d == NULL || (d->dv_hashtab.ht_used == 0 && d->dv_type == NULL))
return &t_dict_empty;
if ((flags & TVTT_DO_MEMBER) == 0)
return &t_dict_any;
// If the type is dict<any> go through the members, it may end up a
// more specific type.
if (d->dv_type != NULL && (d->dv_hashtab.ht_used == 0
|| (flags & TVTT_MORE_SPECIFIC) == 0
|| d->dv_type->tt_member != &t_any))
return d->dv_type;
if (d->dv_copyID == copyID)
// avoid recursion
return &t_dict_any;
d->dv_copyID = copyID;
// Use the common type of all values.
dict_iterate_start(tv, &iter);
dict_iterate_next(&iter, &value);
member_type = typval2type(value, copyID, type_gap, TVTT_DO_MEMBER);
while (dict_iterate_next(&iter, &value) != NULL)
common_type(typval2type(value, copyID, type_gap, TVTT_DO_MEMBER),
member_type, &member_type, type_gap);
return get_dict_type(member_type, type_gap);
}
/*
* Get a type_T for a "partial" typval in "tv".
*/ */
static type_T * static type_T *
partial_typval2type(typval_T *tv, ufunc_T *ufunc, garray_T *type_gap) partial_typval2type(typval_T *tv, ufunc_T *ufunc, garray_T *type_gap)
@@ -448,118 +568,37 @@ partial_typval2type(typval_T *tv, ufunc_T *ufunc, garray_T *type_gap)
} }
/* /*
* Get a type_T for a typval_T. * Get a type_T for a "class" or an "object" typval in "tv".
* "type_gap" is used to temporarily create types in.
* When "flags" has TVTT_DO_MEMBER also get the member type, otherwise use
* "any".
* When "flags" has TVTT_MORE_SPECIFIC get the more specific member type if it
* is "any".
*/ */
static type_T * static type_T *
typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int flags) oc_typval2type(typval_T *tv)
{ {
type_T *type; if (tv->v_type == VAR_CLASS)
type_T *member_type = NULL; {
class_T *class_type = NULL; if (tv->vval.v_class == NULL)
int argcount = 0; return &t_class;
int min_argcount = 0;
if (tv->v_type == VAR_NUMBER) return &tv->vval.v_class->class_type;
return &t_number;
if (tv->v_type == VAR_BOOL)
return &t_bool;
if (tv->v_type == VAR_SPECIAL)
{
if (tv->vval.v_number == VVAL_NULL)
return &t_null;
if (tv->vval.v_number == VVAL_NONE)
return &t_none;
if (tv->vval.v_number == VVAL_TRUE
|| tv->vval.v_number == VVAL_FALSE)
return &t_bool;
return &t_unknown;
}
if (tv->v_type == VAR_STRING)
return &t_string;
if (tv->v_type == VAR_BLOB)
{
if (tv->vval.v_blob == NULL)
return &t_blob_null;
return &t_blob;
} }
if (tv->v_type == VAR_LIST) if (tv->vval.v_object != NULL)
{ return &tv->vval.v_object->obj_class->class_object_type;
list_T *l = tv->vval.v_list;
listitem_T *li;
// An empty list has type list<unknown>, unless the type was specified return &t_object;
// and is not list<any>. This matters when assigning to a variable }
// with a specific list type.
if (l == NULL || (l->lv_first == NULL
&& (l->lv_type == NULL || l->lv_type->tt_member == &t_any)))
return &t_list_empty;
if ((flags & TVTT_DO_MEMBER) == 0)
return &t_list_any;
// If the type is list<any> go through the members, it may end up a
// more specific type.
if (l->lv_type != NULL && (l->lv_first == NULL
|| (flags & TVTT_MORE_SPECIFIC) == 0
|| l->lv_type->tt_member != &t_any))
// make a copy, lv_type may be freed if the list is freed
return copy_type_deep(l->lv_type, type_gap);
if (l->lv_first == &range_list_item)
return &t_list_number;
if (l->lv_copyID == copyID)
// avoid recursion
return &t_list_any;
l->lv_copyID = copyID;
// Use the common type of all members. /*
member_type = typval2type(&l->lv_first->li_tv, copyID, type_gap, * Get a type_T for a "function" or a "partial"
TVTT_DO_MEMBER); */
for (li = l->lv_first->li_next; li != NULL; li = li->li_next) static type_T *
common_type(typval2type(&li->li_tv, copyID, type_gap, fp_typval2type(typval_T *tv, garray_T *type_gap)
TVTT_DO_MEMBER), {
member_type, &member_type, type_gap);
return get_list_type(member_type, type_gap);
}
if (tv->v_type == VAR_DICT)
{
dict_iterator_T iter;
typval_T *value;
dict_T *d = tv->vval.v_dict;
if (d == NULL || (d->dv_hashtab.ht_used == 0 && d->dv_type == NULL))
return &t_dict_empty;
if ((flags & TVTT_DO_MEMBER) == 0)
return &t_dict_any;
// If the type is dict<any> go through the members, it may end up a
// more specific type.
if (d->dv_type != NULL && (d->dv_hashtab.ht_used == 0
|| (flags & TVTT_MORE_SPECIFIC) == 0
|| d->dv_type->tt_member != &t_any))
return d->dv_type;
if (d->dv_copyID == copyID)
// avoid recursion
return &t_dict_any;
d->dv_copyID = copyID;
// Use the common type of all values.
dict_iterate_start(tv, &iter);
dict_iterate_next(&iter, &value);
member_type = typval2type(value, copyID, type_gap, TVTT_DO_MEMBER);
while (dict_iterate_next(&iter, &value) != NULL)
common_type(typval2type(value, copyID, type_gap, TVTT_DO_MEMBER),
member_type, &member_type, type_gap);
return get_dict_type(member_type, type_gap);
}
if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
{
char_u *name = NULL; char_u *name = NULL;
ufunc_T *ufunc = NULL; ufunc_T *ufunc = NULL;
type_T *type;
type_T *member_type = NULL;
int argcount = 0;
int min_argcount = 0;
if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL) if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
{ {
@@ -570,8 +609,10 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int flags)
} }
else else
name = tv->vval.v_string; name = tv->vval.v_string;
if (name == NULL && ufunc == NULL) if (name == NULL && ufunc == NULL)
return &t_func_unknown; return &t_func_unknown;
if (name != NULL) if (name != NULL)
{ {
int idx = find_internal_func(name); int idx = find_internal_func(name);
@@ -604,12 +645,6 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int flags)
return ufunc->uf_func_type; return ufunc->uf_func_type;
} }
} }
}
if (tv->v_type == VAR_CLASS)
class_type = tv->vval.v_class;
else if (tv->v_type == VAR_OBJECT && tv->vval.v_object != NULL)
class_type = tv->vval.v_object->obj_class;
type = get_type_ptr(type_gap); type = get_type_ptr(type_gap);
if (type == NULL) if (type == NULL)
@@ -624,11 +659,83 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int flags)
type->tt_min_argcount -= tv->vval.v_partial->pt_argc; type->tt_min_argcount -= tv->vval.v_partial->pt_argc;
} }
type->tt_member = member_type; type->tt_member = member_type;
type->tt_class = class_type;
return type; return type;
} }
/*
* Get a type_T for a typval_T.
* "type_gap" is used to temporarily create types in.
* When "flags" has TVTT_DO_MEMBER also get the member type, otherwise use
* "any".
* When "flags" has TVTT_MORE_SPECIFIC get the more specific member type if it
* is "any".
*/
static type_T *
typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int flags)
{
switch (tv->v_type)
{
case VAR_UNKNOWN:
return &t_unknown;
case VAR_ANY:
return &t_any;
case VAR_VOID:
return &t_void;
case VAR_BOOL:
return &t_bool;
case VAR_SPECIAL:
return special_typval2type(tv);
case VAR_NUMBER:
return &t_number;
case VAR_FLOAT:
return &t_float;
case VAR_STRING:
return &t_string;
case VAR_BLOB:
if (tv->vval.v_blob == NULL)
return &t_blob_null;
return &t_blob;
case VAR_LIST:
return list_typval2type(tv, copyID, type_gap, flags);
case VAR_DICT:
return dict_typval2type(tv, copyID, type_gap, flags);
case VAR_JOB:
return &t_job;
case VAR_CHANNEL:
return &t_channel;
case VAR_CLASS:
case VAR_OBJECT:
return oc_typval2type(tv);
case VAR_TYPEALIAS:
return &t_typealias;
case VAR_FUNC:
case VAR_PARTIAL:
return fp_typval2type(tv, type_gap);
case VAR_INSTR:
default:
break;
}
return NULL;
}
/* /*
* Return TRUE if "tv" is not a bool but should be converted to bool. * Return TRUE if "tv" is not a bool but should be converted to bool.
*/ */