diff src/vim9compile.c @ 33506:f61713271934 v9.0.2002

patch 9.0.2002: Vim9: need cleanup of class related interface code Commit: https://github.com/vim/vim/commit/b852305dbf42f1206ecc6ae414fc200235fe2963 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sun Oct 8 19:07:39 2023 +0200 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>
author Christian Brabandt <cb@256bit.org>
date Sun, 08 Oct 2023 19:15:06 +0200
parents bff8ac203a22
children c8bd88bdb630
line wrap: on
line diff
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -2011,16 +2011,33 @@ compile_lhs(
 	    // 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;
-
-	    if (!lhs_class_member_modifiable(lhs, var_start, cctx))
+	    char_u	*name = var_start + lhs->lhs_varlen + 1;
+	    size_t	namelen = lhs->lhs_end - var_start - lhs->lhs_varlen - 1;
+
+	    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
 	{