patch 9.0.2002: Vim9: need cleanup of class related interface code
Problem: Vim9: need cleanup of class related interface code
Solution: Remove the unused class variable and class method related code
for interfaces.
Remove unused class variable and class method related code for
interfaces.
Refactor the code.
Optimize the object/class member double lookup in compile_lhs().
Change unused global functions to static functions.
closes: #13302
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 828fe02..7e1914b 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -2011,16 +2011,33 @@
// for an object or class member get the type of the member
class_T *cl = lhs->lhs_type->tt_class;
int is_object = lhs->lhs_type->tt_type == VAR_OBJECT;
+ char_u *name = var_start + lhs->lhs_varlen + 1;
+ size_t namelen = lhs->lhs_end - var_start - lhs->lhs_varlen - 1;
- if (!lhs_class_member_modifiable(lhs, var_start, cctx))
+ ocmember_T *m = member_lookup(cl, lhs->lhs_type->tt_type,
+ name, namelen, &lhs->lhs_member_idx);
+ if (m == NULL)
+ {
+ member_not_found_msg(cl, lhs->lhs_type->tt_type, name, namelen);
return FAIL;
+ }
- lhs->lhs_member_type = class_member_type(cl,
- is_object,
- after + 1, lhs->lhs_end,
- &lhs->lhs_member_idx);
- if (lhs->lhs_member_idx < 0)
+ // If it is private member variable, then accessing it outside the
+ // class is not allowed.
+ // If it is a read only class variable, then it can be modified
+ // only inside the class where it is defined.
+ if ((m->ocm_access != VIM_ACCESS_ALL) &&
+ ((is_object && !inside_class(cctx, cl))
+ || (!is_object && cctx->ctx_ufunc->uf_class != cl)))
+ {
+ char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
+ ? e_cannot_access_private_variable_str
+ : e_variable_is_not_writable_str;
+ emsg_var_cl_define(msg, m->ocm_name, 0, cl);
return FAIL;
+ }
+
+ lhs->lhs_member_type = m->ocm_type;
}
else
{