0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -04:00

patch 9.0.1890: Vim9: lookup code for class/object repaeated

Problem:  Vim9: lookup code for class/object repaeated
Solution: Refactor and make use of lookup functions

closes: #13067

Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
Yegappan Lakshmanan
2023-09-10 18:19:06 +02:00
committed by Christian Brabandt
parent 0661033075
commit f36bbcd402
9 changed files with 437 additions and 317 deletions

View File

@@ -394,39 +394,32 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
if (type->tt_type == VAR_OBJECT)
{
for (int i = 0; i < cl->class_obj_member_count; ++i)
int m_idx = object_member_idx(cl, name, len);
if (m_idx >= 0)
{
ocmember_T *m = &cl->class_obj_members[i];
if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
ocmember_T *m = &cl->class_obj_members[m_idx];
if (*name == '_' && !inside_class(cctx, cl))
{
if (*name == '_' && !inside_class(cctx, cl))
{
semsg(_(e_cannot_access_private_member_str), m->ocm_name);
return FAIL;
}
*arg = name_end;
if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
return generate_GET_ITF_MEMBER(cctx, cl, i, m->ocm_type,
FALSE);
return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type, FALSE);
semsg(_(e_cannot_access_private_member_str), m->ocm_name);
return FAIL;
}
*arg = name_end;
if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))
return generate_GET_ITF_MEMBER(cctx, cl, m_idx, m->ocm_type,
FALSE);
return generate_GET_OBJ_MEMBER(cctx, m_idx, m->ocm_type, FALSE);
}
// Could be a function reference: "obj.Func".
for (int i = 0; i < cl->class_obj_method_count; ++i)
m_idx = object_method_idx(cl, name, len);
if (m_idx >= 0)
{
ufunc_T *fp = cl->class_obj_methods[i];
// Use a separate pointer to avoid that ASAN complains about
// uf_name[] only being 4 characters.
char_u *ufname = (char_u *)fp->uf_name;
if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
{
if (type->tt_type == VAR_OBJECT
&& (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
return generate_FUNCREF(cctx, fp, cl, i, NULL);
return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
}
ufunc_T *fp = cl->class_obj_methods[m_idx];
if (type->tt_type == VAR_OBJECT
&& (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
return generate_FUNCREF(cctx, fp, cl, m_idx, NULL);
return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
}
semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
@@ -435,28 +428,22 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
{
// load class member
int idx;
for (idx = 0; idx < cl->class_class_member_count; ++idx)
ocmember_T *m = class_member_lookup(cl, name, len, &idx);
if (m != NULL)
{
ocmember_T *m = &cl->class_class_members[idx];
if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
// Note: type->tt_type = VAR_CLASS
if ((cl->class_flags & CLASS_INTERFACE) != 0)
{
// Note: type->tt_type = VAR_CLASS
if ((cl->class_flags & CLASS_INTERFACE) != 0)
{
semsg(_(e_interface_static_direct_access_str),
cl->class_name, m->ocm_name);
return FAIL;
}
if (*name == '_' && !inside_class(cctx, cl))
{
semsg(_(e_cannot_access_private_member_str), m->ocm_name);
return FAIL;
}
break;
semsg(_(e_interface_static_direct_access_str),
cl->class_name, m->ocm_name);
return FAIL;
}
}
if (idx < cl->class_class_member_count)
{
if (*name == '_' && !inside_class(cctx, cl))
{
semsg(_(e_cannot_access_private_member_str), m->ocm_name);
return FAIL;
}
*arg = name_end;
return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
}
@@ -773,7 +760,7 @@ compile_load(
else
gen_load = TRUE;
}
else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
else if ((idx = cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0)
{
// Referencing a class member without the class name. Infer
// the class from the def function context.
@@ -1141,7 +1128,7 @@ compile_call(
goto theend;
}
}
else if ((mi = class_method_index(name, varlen, &cl, cctx)) >= 0)
else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0)
{
// Class method invocation without the class name. The
// generate_CALL() function expects the class type at the top of