Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9class.c: Vim9 script class support |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | // When not generating protos this is included in proto.h |
| 20 | #ifdef PROTO |
| 21 | # include "vim9.h" |
| 22 | #endif |
| 23 | |
| 24 | /* |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 25 | * Parse a member declaration, both object and class member. |
| 26 | * Returns OK or FAIL. When OK then "varname_end" is set to just after the |
| 27 | * variable name and "type_ret" is set to the decleared or detected type. |
| 28 | * "init_expr" is set to the initialisation expression (allocated), if there is |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 29 | * one. For an interface "init_expr" is NULL. |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 30 | */ |
| 31 | static int |
| 32 | parse_member( |
| 33 | exarg_T *eap, |
| 34 | char_u *line, |
| 35 | char_u *varname, |
| 36 | int has_public, // TRUE if "public" seen before "varname" |
| 37 | char_u **varname_end, |
| 38 | garray_T *type_list, |
| 39 | type_T **type_ret, |
| 40 | char_u **init_expr) |
| 41 | { |
| 42 | *varname_end = to_name_end(varname, FALSE); |
| 43 | if (*varname == '_' && has_public) |
| 44 | { |
| 45 | semsg(_(e_public_member_name_cannot_start_with_underscore_str), line); |
| 46 | return FAIL; |
| 47 | } |
| 48 | |
| 49 | char_u *colon = skipwhite(*varname_end); |
| 50 | char_u *type_arg = colon; |
| 51 | type_T *type = NULL; |
| 52 | if (*colon == ':') |
| 53 | { |
| 54 | if (VIM_ISWHITE(**varname_end)) |
| 55 | { |
| 56 | semsg(_(e_no_white_space_allowed_before_colon_str), varname); |
| 57 | return FAIL; |
| 58 | } |
| 59 | if (!VIM_ISWHITE(colon[1])) |
| 60 | { |
| 61 | semsg(_(e_white_space_required_after_str_str), ":", varname); |
| 62 | return FAIL; |
| 63 | } |
| 64 | type_arg = skipwhite(colon + 1); |
| 65 | type = parse_type(&type_arg, type_list, TRUE); |
| 66 | if (type == NULL) |
| 67 | return FAIL; |
| 68 | } |
| 69 | |
| 70 | char_u *expr_start = skipwhite(type_arg); |
| 71 | char_u *expr_end = expr_start; |
| 72 | if (type == NULL && *expr_start != '=') |
| 73 | { |
| 74 | emsg(_(e_type_or_initialization_required)); |
| 75 | return FAIL; |
| 76 | } |
| 77 | |
| 78 | if (*expr_start == '=') |
| 79 | { |
| 80 | if (!VIM_ISWHITE(expr_start[-1]) || !VIM_ISWHITE(expr_start[1])) |
| 81 | { |
| 82 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 83 | "=", type_arg); |
| 84 | return FAIL; |
| 85 | } |
| 86 | expr_start = skipwhite(expr_start + 1); |
| 87 | |
| 88 | expr_end = expr_start; |
| 89 | evalarg_T evalarg; |
| 90 | fill_evalarg_from_eap(&evalarg, eap, FALSE); |
| 91 | skip_expr(&expr_end, NULL); |
| 92 | |
| 93 | if (type == NULL) |
| 94 | { |
| 95 | // No type specified, use the type of the initializer. |
| 96 | typval_T tv; |
| 97 | tv.v_type = VAR_UNKNOWN; |
| 98 | char_u *expr = expr_start; |
| 99 | int res = eval0(expr, &tv, eap, &evalarg); |
| 100 | |
| 101 | if (res == OK) |
Bram Moolenaar | e83c133 | 2023-01-02 21:04:04 +0000 | [diff] [blame] | 102 | { |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 103 | type = typval2type(&tv, get_copyID(), type_list, |
| 104 | TVTT_DO_MEMBER); |
Bram Moolenaar | e83c133 | 2023-01-02 21:04:04 +0000 | [diff] [blame] | 105 | clear_tv(&tv); |
| 106 | } |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 107 | if (type == NULL) |
| 108 | { |
| 109 | semsg(_(e_cannot_get_object_member_type_from_initializer_str), |
| 110 | expr_start); |
| 111 | clear_evalarg(&evalarg, NULL); |
| 112 | return FAIL; |
| 113 | } |
| 114 | } |
| 115 | clear_evalarg(&evalarg, NULL); |
| 116 | } |
| 117 | if (!valid_declaration_type(type)) |
| 118 | return FAIL; |
| 119 | |
| 120 | *type_ret = type; |
| 121 | if (expr_end > expr_start) |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 122 | { |
| 123 | if (init_expr == NULL) |
| 124 | { |
| 125 | emsg(_(e_cannot_initialize_member_in_interface)); |
| 126 | return FAIL; |
| 127 | } |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 128 | *init_expr = vim_strnsave(expr_start, expr_end - expr_start); |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 129 | } |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 130 | return OK; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Add a member to an object or a class. |
| 135 | * Returns OK when successful, "init_expr" will be consumed then. |
| 136 | * Returns FAIL otherwise, caller might need to free "init_expr". |
| 137 | */ |
| 138 | static int |
| 139 | add_member( |
| 140 | garray_T *gap, |
| 141 | char_u *varname, |
| 142 | char_u *varname_end, |
| 143 | int has_public, |
| 144 | type_T *type, |
| 145 | char_u *init_expr) |
| 146 | { |
| 147 | if (ga_grow(gap, 1) == FAIL) |
| 148 | return FAIL; |
| 149 | ocmember_T *m = ((ocmember_T *)gap->ga_data) + gap->ga_len; |
| 150 | m->ocm_name = vim_strnsave(varname, varname_end - varname); |
| 151 | m->ocm_access = has_public ? ACCESS_ALL |
| 152 | : *varname == '_' ? ACCESS_PRIVATE : ACCESS_READ; |
| 153 | m->ocm_type = type; |
| 154 | if (init_expr != NULL) |
| 155 | m->ocm_init = init_expr; |
| 156 | ++gap->ga_len; |
| 157 | return OK; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Move the class or object members found while parsing a class into the class. |
| 162 | * "gap" contains the found members. |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 163 | * "parent_members" points to the members in the parent class (if any) |
| 164 | * "parent_count" is the number of members in the parent class |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 165 | * "members" will be set to the newly allocated array of members and |
| 166 | * "member_count" set to the number of members. |
| 167 | * Returns OK or FAIL. |
| 168 | */ |
| 169 | static int |
| 170 | add_members_to_class( |
| 171 | garray_T *gap, |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 172 | ocmember_T *parent_members, |
| 173 | int parent_count, |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 174 | ocmember_T **members, |
| 175 | int *member_count) |
| 176 | { |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 177 | *member_count = parent_count + gap->ga_len; |
| 178 | *members = *member_count == 0 ? NULL |
| 179 | : ALLOC_MULT(ocmember_T, *member_count); |
| 180 | if (*member_count > 0 && *members == NULL) |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 181 | return FAIL; |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 182 | for (int i = 0; i < parent_count; ++i) |
| 183 | { |
| 184 | // parent members need to be copied |
| 185 | *members[i] = parent_members[i]; |
| 186 | members[i]->ocm_name = vim_strsave(members[i]->ocm_name); |
| 187 | if (members[i]->ocm_init != NULL) |
| 188 | members[i]->ocm_init = vim_strsave(members[i]->ocm_init); |
| 189 | } |
Bram Moolenaar | 8efdcee | 2022-12-19 12:18:09 +0000 | [diff] [blame] | 190 | if (gap->ga_len > 0) |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 191 | // new members are moved |
| 192 | mch_memmove(*members + parent_count, |
| 193 | gap->ga_data, sizeof(ocmember_T) * gap->ga_len); |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 194 | VIM_CLEAR(gap->ga_data); |
| 195 | return OK; |
| 196 | } |
| 197 | |
| 198 | /* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 199 | * Handle ":class" and ":abstract class" up to ":endclass". |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 200 | * Handle ":interface" up to ":endinterface". |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 201 | */ |
| 202 | void |
| 203 | ex_class(exarg_T *eap) |
| 204 | { |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 205 | int is_class = eap->cmdidx == CMD_class; // FALSE for :interface |
| 206 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 207 | if (!current_script_is_vim9() |
| 208 | || (cmdmod.cmod_flags & CMOD_LEGACY) |
| 209 | || !getline_equal(eap->getline, eap->cookie, getsourceline)) |
| 210 | { |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 211 | if (is_class) |
| 212 | emsg(_(e_class_can_only_be_defined_in_vim9_script)); |
| 213 | else |
| 214 | emsg(_(e_interface_can_only_be_defined_in_vim9_script)); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 217 | |
| 218 | char_u *arg = eap->arg; |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 219 | int is_abstract = eap->cmdidx == CMD_abstract; |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 220 | if (is_abstract) |
| 221 | { |
| 222 | if (STRNCMP(arg, "class", 5) != 0 || !VIM_ISWHITE(arg[5])) |
| 223 | { |
| 224 | semsg(_(e_invalid_argument_str), arg); |
| 225 | return; |
| 226 | } |
| 227 | arg = skipwhite(arg + 5); |
| 228 | } |
| 229 | |
| 230 | if (!ASCII_ISUPPER(*arg)) |
| 231 | { |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 232 | if (is_class) |
| 233 | semsg(_(e_class_name_must_start_with_uppercase_letter_str), arg); |
| 234 | else |
| 235 | semsg(_(e_interface_name_must_start_with_uppercase_letter_str), |
| 236 | arg); |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 237 | return; |
| 238 | } |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 239 | char_u *name_end = find_name_end(arg, NULL, NULL, FNE_CHECK_START); |
| 240 | if (!IS_WHITE_OR_NUL(*name_end)) |
| 241 | { |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 242 | semsg(_(e_white_space_required_after_name_str), arg); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 243 | return; |
| 244 | } |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 245 | char_u *name_start = arg; |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 246 | |
| 247 | // TODO: |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 248 | // generics: <Tkey, Tentry> |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 249 | // handle "is_export" if it is set |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 250 | |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 251 | // Name for "extends BaseClass" |
| 252 | char_u *extends = NULL; |
| 253 | |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 254 | // Names for "implements SomeInterface" |
| 255 | garray_T ga_impl; |
| 256 | ga_init2(&ga_impl, sizeof(char_u *), 5); |
| 257 | |
| 258 | arg = skipwhite(name_end); |
| 259 | while (*arg != NUL && *arg != '#' && *arg != '\n') |
| 260 | { |
| 261 | // TODO: |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 262 | // specifies SomeInterface |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 263 | if (STRNCMP(arg, "extends", 7) == 0 && IS_WHITE_OR_NUL(arg[7])) |
| 264 | { |
| 265 | if (extends != NULL) |
| 266 | { |
| 267 | emsg(_(e_duplicate_extends)); |
| 268 | goto early_ret; |
| 269 | } |
| 270 | arg = skipwhite(arg + 7); |
| 271 | char_u *end = find_name_end(arg, NULL, NULL, FNE_CHECK_START); |
| 272 | if (!IS_WHITE_OR_NUL(*end)) |
| 273 | { |
| 274 | semsg(_(e_white_space_required_after_name_str), arg); |
| 275 | goto early_ret; |
| 276 | } |
| 277 | extends = vim_strnsave(arg, end - arg); |
| 278 | if (extends == NULL) |
| 279 | goto early_ret; |
| 280 | |
| 281 | arg = skipwhite(end + 1); |
| 282 | } |
| 283 | else if (STRNCMP(arg, "implements", 10) == 0 |
| 284 | && IS_WHITE_OR_NUL(arg[10])) |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 285 | { |
Bram Moolenaar | df8f947 | 2023-01-07 14:51:03 +0000 | [diff] [blame] | 286 | if (ga_impl.ga_len > 0) |
| 287 | { |
| 288 | emsg(_(e_duplicate_implements)); |
| 289 | goto early_ret; |
| 290 | } |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 291 | arg = skipwhite(arg + 10); |
Bram Moolenaar | df8f947 | 2023-01-07 14:51:03 +0000 | [diff] [blame] | 292 | |
| 293 | for (;;) |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 294 | { |
Bram Moolenaar | df8f947 | 2023-01-07 14:51:03 +0000 | [diff] [blame] | 295 | char_u *impl_end = find_name_end(arg, NULL, NULL, |
| 296 | FNE_CHECK_START); |
| 297 | if (!IS_WHITE_OR_NUL(*impl_end) && *impl_end != ',') |
| 298 | { |
| 299 | semsg(_(e_white_space_required_after_name_str), arg); |
| 300 | goto early_ret; |
| 301 | } |
| 302 | char_u *iname = vim_strnsave(arg, impl_end - arg); |
| 303 | if (iname == NULL) |
| 304 | goto early_ret; |
| 305 | for (int i = 0; i < ga_impl.ga_len; ++i) |
| 306 | if (STRCMP(((char_u **)ga_impl.ga_data)[i], iname) == 0) |
| 307 | { |
| 308 | semsg(_(e_duplicate_interface_after_implements_str), |
| 309 | iname); |
| 310 | vim_free(iname); |
| 311 | goto early_ret; |
| 312 | } |
| 313 | if (ga_add_string(&ga_impl, iname) == FAIL) |
| 314 | { |
| 315 | vim_free(iname); |
| 316 | goto early_ret; |
| 317 | } |
| 318 | if (*impl_end != ',') |
| 319 | { |
| 320 | arg = skipwhite(impl_end); |
| 321 | break; |
| 322 | } |
| 323 | arg = skipwhite(impl_end + 1); |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 324 | } |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 325 | } |
| 326 | else |
| 327 | { |
| 328 | semsg(_(e_trailing_characters_str), arg); |
| 329 | early_ret: |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 330 | vim_free(extends); |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 331 | ga_clear_strings(&ga_impl); |
| 332 | return; |
| 333 | } |
| 334 | } |
| 335 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 336 | garray_T type_list; // list of pointers to allocated types |
| 337 | ga_init2(&type_list, sizeof(type_T *), 10); |
| 338 | |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 339 | // Growarray with class members declared in the class. |
| 340 | garray_T classmembers; |
| 341 | ga_init2(&classmembers, sizeof(ocmember_T), 10); |
| 342 | |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 343 | // Growarray with functions declared in the class. |
| 344 | garray_T classfunctions; |
| 345 | ga_init2(&classfunctions, sizeof(ufunc_T *), 10); |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 346 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 347 | // Growarray with object members declared in the class. |
| 348 | garray_T objmembers; |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 349 | ga_init2(&objmembers, sizeof(ocmember_T), 10); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 350 | |
| 351 | // Growarray with object methods declared in the class. |
| 352 | garray_T objmethods; |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 353 | ga_init2(&objmethods, sizeof(ufunc_T *), 10); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 354 | |
| 355 | /* |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 356 | * Go over the body of the class/interface until "endclass" or |
| 357 | * "endinterface" is found. |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 358 | */ |
| 359 | char_u *theline = NULL; |
| 360 | int success = FALSE; |
| 361 | for (;;) |
| 362 | { |
| 363 | vim_free(theline); |
| 364 | theline = eap->getline(':', eap->cookie, 0, GETLINE_CONCAT_ALL); |
| 365 | if (theline == NULL) |
| 366 | break; |
| 367 | char_u *line = skipwhite(theline); |
| 368 | |
Bram Moolenaar | 418b547 | 2022-12-20 13:38:22 +0000 | [diff] [blame] | 369 | // Skip empty and comment lines. |
| 370 | if (*line == NUL) |
| 371 | continue; |
| 372 | if (*line == '#') |
| 373 | { |
| 374 | if (vim9_bad_comment(line)) |
| 375 | break; |
| 376 | continue; |
| 377 | } |
| 378 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 379 | char_u *p = line; |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 380 | char *end_name = is_class ? "endclass" : "endinterface"; |
| 381 | if (checkforcmd(&p, end_name, is_class ? 4 : 5)) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 382 | { |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 383 | if (STRNCMP(line, end_name, is_class ? 8 : 12) != 0) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 384 | semsg(_(e_command_cannot_be_shortened_str), line); |
| 385 | else if (*p == '|' || !ends_excmd2(line, p)) |
| 386 | semsg(_(e_trailing_characters_str), p); |
Bram Moolenaar | 98aeb21 | 2022-12-08 22:09:14 +0000 | [diff] [blame] | 387 | else |
| 388 | success = TRUE; |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 389 | break; |
| 390 | } |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 391 | char *wrong_name = is_class ? "endinterface" : "endclass"; |
| 392 | if (checkforcmd(&p, wrong_name, is_class ? 5 : 4)) |
| 393 | { |
| 394 | semsg(_(e_invalid_command_str), line); |
| 395 | break; |
| 396 | } |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 397 | |
Bram Moolenaar | 3d473ee | 2022-12-14 20:59:32 +0000 | [diff] [blame] | 398 | int has_public = FALSE; |
| 399 | if (checkforcmd(&p, "public", 3)) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 400 | { |
Bram Moolenaar | 3d473ee | 2022-12-14 20:59:32 +0000 | [diff] [blame] | 401 | if (STRNCMP(line, "public", 6) != 0) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 402 | { |
Bram Moolenaar | 3d473ee | 2022-12-14 20:59:32 +0000 | [diff] [blame] | 403 | semsg(_(e_command_cannot_be_shortened_str), line); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 404 | break; |
| 405 | } |
Bram Moolenaar | 3d473ee | 2022-12-14 20:59:32 +0000 | [diff] [blame] | 406 | has_public = TRUE; |
| 407 | p = skipwhite(line + 6); |
| 408 | |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 409 | if (STRNCMP(p, "this", 4) != 0 && STRNCMP(p, "static", 6) != 0) |
Bram Moolenaar | 3d473ee | 2022-12-14 20:59:32 +0000 | [diff] [blame] | 410 | { |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 411 | emsg(_(e_public_must_be_followed_by_this_or_static)); |
Bram Moolenaar | 3d473ee | 2022-12-14 20:59:32 +0000 | [diff] [blame] | 412 | break; |
| 413 | } |
| 414 | } |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 415 | |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 416 | int has_static = FALSE; |
| 417 | char_u *ps = p; |
| 418 | if (checkforcmd(&p, "static", 4)) |
| 419 | { |
| 420 | if (STRNCMP(ps, "static", 6) != 0) |
| 421 | { |
| 422 | semsg(_(e_command_cannot_be_shortened_str), ps); |
| 423 | break; |
| 424 | } |
| 425 | has_static = TRUE; |
| 426 | p = skipwhite(ps + 6); |
| 427 | } |
| 428 | |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 429 | // object members (public, read access, private): |
| 430 | // "this._varname" |
| 431 | // "this.varname" |
| 432 | // "public this.varname" |
Bram Moolenaar | 3d473ee | 2022-12-14 20:59:32 +0000 | [diff] [blame] | 433 | if (STRNCMP(p, "this", 4) == 0) |
| 434 | { |
| 435 | if (p[4] != '.' || !eval_isnamec1(p[5])) |
| 436 | { |
| 437 | semsg(_(e_invalid_object_member_declaration_str), p); |
| 438 | break; |
| 439 | } |
| 440 | char_u *varname = p + 5; |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 441 | char_u *varname_end = NULL; |
Bram Moolenaar | 74e1274 | 2022-12-13 21:14:28 +0000 | [diff] [blame] | 442 | type_T *type = NULL; |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 443 | char_u *init_expr = NULL; |
| 444 | if (parse_member(eap, line, varname, has_public, |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 445 | &varname_end, &type_list, &type, |
| 446 | is_class ? &init_expr: NULL) == FAIL) |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 447 | break; |
| 448 | if (add_member(&objmembers, varname, varname_end, |
| 449 | has_public, type, init_expr) == FAIL) |
Bram Moolenaar | 74e1274 | 2022-12-13 21:14:28 +0000 | [diff] [blame] | 450 | { |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 451 | vim_free(init_expr); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 452 | break; |
| 453 | } |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 454 | } |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 455 | |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 456 | // constructors: |
| 457 | // def new() |
| 458 | // enddef |
| 459 | // def newOther() |
| 460 | // enddef |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 461 | // object methods and class functions: |
| 462 | // def SomeMethod() |
| 463 | // enddef |
| 464 | // static def ClassFunction() |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 465 | // enddef |
| 466 | // TODO: |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 467 | // def <Tval> someMethod() |
| 468 | // enddef |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 469 | else if (checkforcmd(&p, "def", 3)) |
| 470 | { |
| 471 | exarg_T ea; |
| 472 | garray_T lines_to_free; |
| 473 | |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 474 | // TODO: error for "public static def Func()"? |
| 475 | |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 476 | CLEAR_FIELD(ea); |
| 477 | ea.cmd = line; |
| 478 | ea.arg = p; |
| 479 | ea.cmdidx = CMD_def; |
| 480 | ea.getline = eap->getline; |
| 481 | ea.cookie = eap->cookie; |
| 482 | |
| 483 | ga_init2(&lines_to_free, sizeof(char_u *), 50); |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 484 | ufunc_T *uf = define_function(&ea, NULL, &lines_to_free, |
| 485 | is_class ? CF_CLASS : CF_INTERFACE); |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 486 | ga_clear_strings(&lines_to_free); |
| 487 | |
Bram Moolenaar | 6acf757 | 2023-01-01 19:53:30 +0000 | [diff] [blame] | 488 | if (uf != NULL) |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 489 | { |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 490 | char_u *name = uf->uf_name; |
| 491 | int is_new = STRNCMP(name, "new", 3) == 0; |
Bram Moolenaar | 6acf757 | 2023-01-01 19:53:30 +0000 | [diff] [blame] | 492 | garray_T *fgap = has_static || is_new |
| 493 | ? &classfunctions : &objmethods; |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 494 | // Check the name isn't used already. |
| 495 | for (int i = 0; i < fgap->ga_len; ++i) |
| 496 | { |
| 497 | char_u *n = ((ufunc_T **)fgap->ga_data)[i]->uf_name; |
| 498 | if (STRCMP(name, n) == 0) |
| 499 | { |
| 500 | semsg(_(e_duplicate_function_str), name); |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | |
Bram Moolenaar | 6acf757 | 2023-01-01 19:53:30 +0000 | [diff] [blame] | 505 | if (ga_grow(fgap, 1) == OK) |
| 506 | { |
| 507 | if (is_new) |
| 508 | uf->uf_flags |= FC_NEW; |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 509 | |
Bram Moolenaar | 6acf757 | 2023-01-01 19:53:30 +0000 | [diff] [blame] | 510 | ((ufunc_T **)fgap->ga_data)[fgap->ga_len] = uf; |
| 511 | ++fgap->ga_len; |
| 512 | } |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | // class members |
| 517 | else if (has_static) |
| 518 | { |
| 519 | // class members (public, read access, private): |
| 520 | // "static _varname" |
| 521 | // "static varname" |
| 522 | // "public static varname" |
| 523 | char_u *varname = p; |
| 524 | char_u *varname_end = NULL; |
| 525 | type_T *type = NULL; |
| 526 | char_u *init_expr = NULL; |
| 527 | if (parse_member(eap, line, varname, has_public, |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 528 | &varname_end, &type_list, &type, |
| 529 | is_class ? &init_expr : NULL) == FAIL) |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 530 | break; |
| 531 | if (add_member(&classmembers, varname, varname_end, |
| 532 | has_public, type, init_expr) == FAIL) |
| 533 | { |
| 534 | vim_free(init_expr); |
| 535 | break; |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 539 | else |
| 540 | { |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 541 | if (is_class) |
| 542 | semsg(_(e_not_valid_command_in_class_str), line); |
| 543 | else |
| 544 | semsg(_(e_not_valid_command_in_interface_str), line); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 545 | break; |
| 546 | } |
| 547 | } |
| 548 | vim_free(theline); |
| 549 | |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 550 | class_T *extends_cl = NULL; // class from "extends" argument |
| 551 | |
| 552 | /* |
| 553 | * Check a few things before defining the class. |
| 554 | */ |
| 555 | |
| 556 | // Check the "extends" class is valid. |
| 557 | if (success && extends != NULL) |
| 558 | { |
| 559 | typval_T tv; |
| 560 | tv.v_type = VAR_UNKNOWN; |
| 561 | if (eval_variable(extends, 0, 0, &tv, NULL, EVAL_VAR_IMPORT) == FAIL) |
| 562 | { |
| 563 | semsg(_(e_class_name_not_found_str), extends); |
| 564 | success = FALSE; |
| 565 | } |
| 566 | else |
| 567 | { |
| 568 | if (tv.v_type != VAR_CLASS |
| 569 | || tv.vval.v_class == NULL |
| 570 | || (tv.vval.v_class->class_flags & CLASS_INTERFACE) != 0) |
| 571 | { |
| 572 | semsg(_(e_cannot_extend_str), extends); |
| 573 | success = FALSE; |
| 574 | } |
| 575 | else |
| 576 | { |
| 577 | extends_cl = tv.vval.v_class; |
| 578 | ++extends_cl->class_refcount; |
| 579 | } |
| 580 | clear_tv(&tv); |
| 581 | } |
| 582 | } |
| 583 | VIM_CLEAR(extends); |
| 584 | |
Bram Moolenaar | a94bd9d | 2023-01-12 15:01:32 +0000 | [diff] [blame^] | 585 | class_T **intf_classes = NULL; |
| 586 | |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 587 | // Check all "implements" entries are valid. |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 588 | if (success && ga_impl.ga_len > 0) |
| 589 | { |
Bram Moolenaar | a94bd9d | 2023-01-12 15:01:32 +0000 | [diff] [blame^] | 590 | intf_classes = ALLOC_CLEAR_MULT(class_T *, ga_impl.ga_len); |
| 591 | |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 592 | for (int i = 0; i < ga_impl.ga_len && success; ++i) |
| 593 | { |
| 594 | char_u *impl = ((char_u **)ga_impl.ga_data)[i]; |
| 595 | typval_T tv; |
| 596 | tv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 597 | if (eval_variable(impl, 0, 0, &tv, NULL, EVAL_VAR_IMPORT) == FAIL) |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 598 | { |
| 599 | semsg(_(e_interface_name_not_found_str), impl); |
| 600 | success = FALSE; |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | if (tv.v_type != VAR_CLASS |
| 605 | || tv.vval.v_class == NULL |
| 606 | || (tv.vval.v_class->class_flags & CLASS_INTERFACE) == 0) |
| 607 | { |
| 608 | semsg(_(e_not_valid_interface_str), impl); |
| 609 | success = FALSE; |
| 610 | } |
| 611 | |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 612 | class_T *ifcl = tv.vval.v_class; |
Bram Moolenaar | a94bd9d | 2023-01-12 15:01:32 +0000 | [diff] [blame^] | 613 | intf_classes[i] = ifcl; |
| 614 | ++ifcl->class_refcount; |
| 615 | |
| 616 | // check the members of the interface match the members of the class |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 617 | for (int loop = 1; loop <= 2 && success; ++loop) |
| 618 | { |
| 619 | // loop == 1: check class members |
| 620 | // loop == 2: check object members |
| 621 | int if_count = loop == 1 ? ifcl->class_class_member_count |
| 622 | : ifcl->class_obj_member_count; |
| 623 | if (if_count == 0) |
| 624 | continue; |
| 625 | ocmember_T *if_ms = loop == 1 ? ifcl->class_class_members |
| 626 | : ifcl->class_obj_members; |
| 627 | ocmember_T *cl_ms = (ocmember_T *)(loop == 1 |
| 628 | ? classmembers.ga_data |
| 629 | : objmembers.ga_data); |
| 630 | int cl_count = loop == 1 ? classmembers.ga_len |
| 631 | : objmembers.ga_len; |
| 632 | for (int if_i = 0; if_i < if_count; ++if_i) |
| 633 | { |
| 634 | int cl_i; |
| 635 | for (cl_i = 0; cl_i < cl_count; ++cl_i) |
| 636 | { |
| 637 | ocmember_T *m = &cl_ms[cl_i]; |
| 638 | if (STRCMP(if_ms[if_i].ocm_name, m->ocm_name) == 0) |
| 639 | { |
| 640 | // TODO: check type |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | if (cl_i == cl_count) |
| 645 | { |
| 646 | semsg(_(e_member_str_of_interface_str_not_implemented), |
| 647 | if_ms[if_i].ocm_name, impl); |
| 648 | success = FALSE; |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | // check the functions/methods of the interface match the |
| 655 | // functions/methods of the class |
| 656 | for (int loop = 1; loop <= 2 && success; ++loop) |
| 657 | { |
| 658 | // loop == 1: check class functions |
| 659 | // loop == 2: check object methods |
| 660 | int if_count = loop == 1 ? ifcl->class_class_function_count |
| 661 | : ifcl->class_obj_method_count; |
| 662 | if (if_count == 0) |
| 663 | continue; |
| 664 | ufunc_T **if_fp = loop == 1 ? ifcl->class_class_functions |
| 665 | : ifcl->class_obj_methods; |
| 666 | ufunc_T **cl_fp = (ufunc_T **)(loop == 1 |
| 667 | ? classfunctions.ga_data |
| 668 | : objmethods.ga_data); |
| 669 | int cl_count = loop == 1 ? classfunctions.ga_len |
| 670 | : objmethods.ga_len; |
| 671 | for (int if_i = 0; if_i < if_count; ++if_i) |
| 672 | { |
| 673 | char_u *if_name = if_fp[if_i]->uf_name; |
| 674 | int cl_i; |
| 675 | for (cl_i = 0; cl_i < cl_count; ++cl_i) |
| 676 | { |
| 677 | char_u *cl_name = cl_fp[cl_i]->uf_name; |
| 678 | if (STRCMP(if_name, cl_name) == 0) |
| 679 | { |
| 680 | // TODO: check return and argument types |
| 681 | break; |
| 682 | } |
| 683 | } |
| 684 | if (cl_i == cl_count) |
| 685 | { |
| 686 | semsg(_(e_function_str_of_interface_str_not_implemented), |
| 687 | if_name, impl); |
| 688 | success = FALSE; |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | clear_tv(&tv); |
| 695 | } |
| 696 | } |
| 697 | |
Bram Moolenaar | eb53350 | 2022-12-14 15:06:11 +0000 | [diff] [blame] | 698 | class_T *cl = NULL; |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 699 | if (success) |
| 700 | { |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 701 | // "endclass" encountered without failures: Create the class. |
| 702 | |
Bram Moolenaar | eb53350 | 2022-12-14 15:06:11 +0000 | [diff] [blame] | 703 | cl = ALLOC_CLEAR_ONE(class_T); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 704 | if (cl == NULL) |
| 705 | goto cleanup; |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 706 | if (!is_class) |
| 707 | cl->class_flags = CLASS_INTERFACE; |
| 708 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 709 | cl->class_refcount = 1; |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 710 | cl->class_name = vim_strnsave(name_start, name_end - name_start); |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 711 | if (cl->class_name == NULL) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 712 | goto cleanup; |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 713 | |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 714 | cl->class_extends = extends_cl; |
| 715 | |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 716 | if (ga_impl.ga_len > 0) |
| 717 | { |
| 718 | // Move the "implements" names into the class. |
| 719 | cl->class_interface_count = ga_impl.ga_len; |
| 720 | cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len); |
| 721 | if (cl->class_interfaces == NULL) |
| 722 | goto cleanup; |
| 723 | for (int i = 0; i < ga_impl.ga_len; ++i) |
| 724 | cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i]; |
Bram Moolenaar | 7d4d87b | 2023-01-06 18:59:08 +0000 | [diff] [blame] | 725 | VIM_CLEAR(ga_impl.ga_data); |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 726 | ga_impl.ga_len = 0; |
Bram Moolenaar | a94bd9d | 2023-01-12 15:01:32 +0000 | [diff] [blame^] | 727 | |
| 728 | cl->class_interfaces_cl = intf_classes; |
| 729 | intf_classes = NULL; |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 732 | // Add class and object members to "cl". |
| 733 | if (add_members_to_class(&classmembers, |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 734 | extends_cl == NULL ? NULL |
| 735 | : extends_cl->class_class_members, |
| 736 | extends_cl == NULL ? 0 |
| 737 | : extends_cl->class_class_member_count, |
| 738 | &cl->class_class_members, |
| 739 | &cl->class_class_member_count) == FAIL |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 740 | || add_members_to_class(&objmembers, |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 741 | extends_cl == NULL ? NULL |
| 742 | : extends_cl->class_obj_members, |
| 743 | extends_cl == NULL ? 0 |
| 744 | : extends_cl->class_obj_member_count, |
| 745 | &cl->class_obj_members, |
| 746 | &cl->class_obj_member_count) == FAIL) |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 747 | goto cleanup; |
| 748 | |
Bram Moolenaar | 554d031 | 2023-01-05 19:59:18 +0000 | [diff] [blame] | 749 | if (is_class && cl->class_class_member_count > 0) |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 750 | { |
| 751 | // Allocate a typval for each class member and initialize it. |
| 752 | cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T, |
| 753 | cl->class_class_member_count); |
| 754 | if (cl->class_members_tv != NULL) |
| 755 | for (int i = 0; i < cl->class_class_member_count; ++i) |
| 756 | { |
| 757 | ocmember_T *m = &cl->class_class_members[i]; |
| 758 | typval_T *tv = &cl->class_members_tv[i]; |
| 759 | if (m->ocm_init != NULL) |
| 760 | { |
| 761 | typval_T *etv = eval_expr(m->ocm_init, eap); |
| 762 | if (etv != NULL) |
| 763 | { |
| 764 | *tv = *etv; |
| 765 | vim_free(etv); |
| 766 | } |
| 767 | } |
| 768 | else |
| 769 | { |
| 770 | // TODO: proper default value |
| 771 | tv->v_type = m->ocm_type->tt_type; |
| 772 | tv->vval.v_string = NULL; |
| 773 | } |
| 774 | } |
| 775 | } |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 776 | |
| 777 | int have_new = FALSE; |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 778 | for (int i = 0; i < classfunctions.ga_len; ++i) |
| 779 | if (STRCMP(((ufunc_T **)classfunctions.ga_data)[i]->uf_name, |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 780 | "new") == 0) |
| 781 | { |
| 782 | have_new = TRUE; |
| 783 | break; |
| 784 | } |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 785 | if (is_class && !have_new) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 786 | { |
| 787 | // No new() method was defined, add the default constructor. |
| 788 | garray_T fga; |
| 789 | ga_init2(&fga, 1, 1000); |
| 790 | ga_concat(&fga, (char_u *)"new("); |
| 791 | for (int i = 0; i < cl->class_obj_member_count; ++i) |
| 792 | { |
| 793 | if (i > 0) |
| 794 | ga_concat(&fga, (char_u *)", "); |
| 795 | ga_concat(&fga, (char_u *)"this."); |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 796 | ocmember_T *m = cl->class_obj_members + i; |
| 797 | ga_concat(&fga, (char_u *)m->ocm_name); |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 798 | ga_concat(&fga, (char_u *)" = v:none"); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 799 | } |
| 800 | ga_concat(&fga, (char_u *)")\nenddef\n"); |
| 801 | ga_append(&fga, NUL); |
| 802 | |
| 803 | exarg_T fea; |
| 804 | CLEAR_FIELD(fea); |
| 805 | fea.cmdidx = CMD_def; |
| 806 | fea.cmd = fea.arg = fga.ga_data; |
| 807 | |
| 808 | garray_T lines_to_free; |
| 809 | ga_init2(&lines_to_free, sizeof(char_u *), 50); |
| 810 | |
Bram Moolenaar | 2c01131 | 2023-01-07 10:51:30 +0000 | [diff] [blame] | 811 | ufunc_T *nf = define_function(&fea, NULL, &lines_to_free, CF_CLASS); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 812 | |
| 813 | ga_clear_strings(&lines_to_free); |
| 814 | vim_free(fga.ga_data); |
| 815 | |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 816 | if (nf != NULL && ga_grow(&classfunctions, 1) == OK) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 817 | { |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 818 | ((ufunc_T **)classfunctions.ga_data)[classfunctions.ga_len] |
| 819 | = nf; |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 820 | ++classfunctions.ga_len; |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 821 | |
| 822 | nf->uf_flags |= FC_NEW; |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 823 | nf->uf_ret_type = get_type_ptr(&type_list); |
| 824 | if (nf->uf_ret_type != NULL) |
| 825 | { |
| 826 | nf->uf_ret_type->tt_type = VAR_OBJECT; |
| 827 | nf->uf_ret_type->tt_member = (type_T *)cl; |
| 828 | nf->uf_ret_type->tt_argcount = 0; |
| 829 | nf->uf_ret_type->tt_args = NULL; |
| 830 | } |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 834 | // Move all the functions into the created class. |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 835 | // loop 1: class functions, loop 2: object methods |
| 836 | for (int loop = 1; loop <= 2; ++loop) |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 837 | { |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 838 | garray_T *gap = loop == 1 ? &classfunctions : &objmethods; |
| 839 | int *fcount = loop == 1 ? &cl->class_class_function_count |
| 840 | : &cl->class_obj_method_count; |
| 841 | ufunc_T ***fup = loop == 1 ? &cl->class_class_functions |
| 842 | : &cl->class_obj_methods; |
| 843 | |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 844 | int parent_count = 0; |
| 845 | if (extends_cl != NULL) |
| 846 | // Include functions from the parent. |
| 847 | parent_count = loop == 1 |
| 848 | ? extends_cl->class_class_function_count |
| 849 | : extends_cl->class_obj_method_count; |
| 850 | |
| 851 | *fcount = parent_count + gap->ga_len; |
| 852 | if (*fcount == 0) |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 853 | { |
| 854 | *fup = NULL; |
| 855 | continue; |
| 856 | } |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 857 | *fup = ALLOC_MULT(ufunc_T *, *fcount); |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 858 | if (*fup == NULL) |
| 859 | goto cleanup; |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 860 | |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 861 | mch_memmove(*fup, gap->ga_data, sizeof(ufunc_T *) * gap->ga_len); |
| 862 | vim_free(gap->ga_data); |
| 863 | if (loop == 1) |
| 864 | cl->class_class_function_count_child = gap->ga_len; |
| 865 | else |
| 866 | cl->class_obj_method_count_child = gap->ga_len; |
| 867 | |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 868 | int skipped = 0; |
| 869 | for (int i = 0; i < parent_count; ++i) |
| 870 | { |
| 871 | // Copy functions from the parent. Can't use the same |
| 872 | // function, because "uf_class" is different and compilation |
| 873 | // will have a different result. |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 874 | // Put them after the functions in the current class, object |
| 875 | // methods may be overruled, then "super.Method()" is used to |
| 876 | // find a method from the parent. |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 877 | // Skip "new" functions. TODO: not all of them. |
| 878 | if (loop == 1 && STRNCMP( |
| 879 | extends_cl->class_class_functions[i]->uf_name, |
| 880 | "new", 3) == 0) |
| 881 | ++skipped; |
| 882 | else |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 883 | { |
| 884 | ufunc_T *pf = (loop == 1 |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 885 | ? extends_cl->class_class_functions |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 886 | : extends_cl->class_obj_methods)[i]; |
| 887 | (*fup)[gap->ga_len + i - skipped] = copy_function(pf); |
| 888 | |
| 889 | // If the child class overrides a function from the parent |
| 890 | // the signature must be equal. |
| 891 | char_u *pname = pf->uf_name; |
| 892 | for (int ci = 0; ci < gap->ga_len; ++ci) |
| 893 | { |
| 894 | ufunc_T *cf = (*fup)[ci]; |
| 895 | char_u *cname = cf->uf_name; |
| 896 | if (STRCMP(pname, cname) == 0) |
| 897 | { |
| 898 | where_T where = WHERE_INIT; |
| 899 | where.wt_func_name = (char *)pname; |
| 900 | (void)check_type(pf->uf_func_type, cf->uf_func_type, |
| 901 | TRUE, where); |
| 902 | } |
| 903 | } |
| 904 | } |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 907 | *fcount -= skipped; |
| 908 | |
| 909 | // Set the class pointer on all the functions and object methods. |
| 910 | for (int i = 0; i < *fcount; ++i) |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 911 | { |
| 912 | ufunc_T *fp = (*fup)[i]; |
| 913 | fp->uf_class = cl; |
| 914 | if (loop == 2) |
| 915 | fp->uf_flags |= FC_OBJECT; |
| 916 | } |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 919 | cl->class_type.tt_type = VAR_CLASS; |
| 920 | cl->class_type.tt_member = (type_T *)cl; |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 921 | cl->class_object_type.tt_type = VAR_OBJECT; |
| 922 | cl->class_object_type.tt_member = (type_T *)cl; |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 923 | cl->class_type_list = type_list; |
| 924 | |
| 925 | // TODO: |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 926 | // - Fill hashtab with object members and methods ? |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 927 | |
| 928 | // Add the class to the script-local variables. |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 929 | // TODO: handle other context, e.g. in a function |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 930 | typval_T tv; |
| 931 | tv.v_type = VAR_CLASS; |
| 932 | tv.vval.v_class = cl; |
| 933 | set_var_const(cl->class_name, current_sctx.sc_sid, |
| 934 | NULL, &tv, FALSE, ASSIGN_DECL, 0); |
| 935 | return; |
| 936 | } |
| 937 | |
| 938 | cleanup: |
Bram Moolenaar | eb53350 | 2022-12-14 15:06:11 +0000 | [diff] [blame] | 939 | if (cl != NULL) |
| 940 | { |
| 941 | vim_free(cl->class_name); |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 942 | vim_free(cl->class_class_functions); |
Bram Moolenaar | a94bd9d | 2023-01-12 15:01:32 +0000 | [diff] [blame^] | 943 | if (cl->class_interfaces != NULL) |
| 944 | { |
| 945 | for (int i = 0; i < cl->class_interface_count; ++i) |
| 946 | vim_free(cl->class_interfaces[i]); |
| 947 | vim_free(cl->class_interfaces); |
| 948 | } |
| 949 | if (cl->class_interfaces_cl != NULL) |
| 950 | { |
| 951 | for (int i = 0; i < cl->class_interface_count; ++i) |
| 952 | class_unref(cl->class_interfaces_cl[i]); |
| 953 | vim_free(cl->class_interfaces_cl); |
| 954 | } |
Bram Moolenaar | eb53350 | 2022-12-14 15:06:11 +0000 | [diff] [blame] | 955 | vim_free(cl->class_obj_members); |
| 956 | vim_free(cl->class_obj_methods); |
| 957 | vim_free(cl); |
| 958 | } |
| 959 | |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 960 | vim_free(extends); |
| 961 | class_unref(extends_cl); |
Bram Moolenaar | a94bd9d | 2023-01-12 15:01:32 +0000 | [diff] [blame^] | 962 | |
| 963 | if (intf_classes != NULL) |
| 964 | { |
| 965 | for (int i = 0; i < ga_impl.ga_len; ++i) |
| 966 | class_unref(intf_classes[i]); |
| 967 | vim_free(intf_classes); |
| 968 | } |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 969 | ga_clear_strings(&ga_impl); |
| 970 | |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 971 | for (int round = 1; round <= 2; ++round) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 972 | { |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 973 | garray_T *gap = round == 1 ? &classmembers : &objmembers; |
| 974 | if (gap->ga_len == 0 || gap->ga_data == NULL) |
| 975 | continue; |
| 976 | |
| 977 | for (int i = 0; i < gap->ga_len; ++i) |
| 978 | { |
| 979 | ocmember_T *m = ((ocmember_T *)gap->ga_data) + i; |
| 980 | vim_free(m->ocm_name); |
| 981 | vim_free(m->ocm_init); |
| 982 | } |
| 983 | ga_clear(gap); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 984 | } |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 985 | |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 986 | for (int i = 0; i < objmethods.ga_len; ++i) |
| 987 | { |
| 988 | ufunc_T *uf = ((ufunc_T **)objmethods.ga_data)[i]; |
| 989 | func_clear_free(uf, FALSE); |
| 990 | } |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 991 | ga_clear(&objmethods); |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 992 | |
| 993 | for (int i = 0; i < classfunctions.ga_len; ++i) |
| 994 | { |
| 995 | ufunc_T *uf = ((ufunc_T **)classfunctions.ga_data)[i]; |
| 996 | func_clear_free(uf, FALSE); |
| 997 | } |
| 998 | ga_clear(&classfunctions); |
| 999 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1000 | clear_type_list(&type_list); |
| 1001 | } |
| 1002 | |
| 1003 | /* |
Bram Moolenaar | f54cedd | 2022-12-23 17:56:27 +0000 | [diff] [blame] | 1004 | * Find member "name" in class "cl", set "member_idx" to the member index and |
| 1005 | * return its type. |
| 1006 | * When not found "member_idx" is set to -1 and t_any is returned. |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1007 | */ |
| 1008 | type_T * |
| 1009 | class_member_type( |
| 1010 | class_T *cl, |
| 1011 | char_u *name, |
| 1012 | char_u *name_end, |
| 1013 | int *member_idx) |
| 1014 | { |
| 1015 | *member_idx = -1; // not found (yet) |
| 1016 | size_t len = name_end - name; |
| 1017 | |
| 1018 | for (int i = 0; i < cl->class_obj_member_count; ++i) |
| 1019 | { |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 1020 | ocmember_T *m = cl->class_obj_members + i; |
| 1021 | if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1022 | { |
| 1023 | *member_idx = i; |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 1024 | return m->ocm_type; |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } |
Bram Moolenaar | f54cedd | 2022-12-23 17:56:27 +0000 | [diff] [blame] | 1027 | |
| 1028 | semsg(_(e_unknown_variable_str), name); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1029 | return &t_any; |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | /* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1033 | * Handle ":enum" up to ":endenum". |
| 1034 | */ |
| 1035 | void |
| 1036 | ex_enum(exarg_T *eap UNUSED) |
| 1037 | { |
| 1038 | // TODO |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * Handle ":type". |
| 1043 | */ |
| 1044 | void |
| 1045 | ex_type(exarg_T *eap UNUSED) |
| 1046 | { |
| 1047 | // TODO |
| 1048 | } |
| 1049 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1050 | /* |
| 1051 | * Evaluate what comes after a class: |
| 1052 | * - class member: SomeClass.varname |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 1053 | * - class function: SomeClass.SomeMethod() |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1054 | * - class constructor: SomeClass.new() |
| 1055 | * - object member: someObject.varname |
| 1056 | * - object method: someObject.SomeMethod() |
| 1057 | * |
| 1058 | * "*arg" points to the '.'. |
| 1059 | * "*arg" is advanced to after the member name or method call. |
| 1060 | * |
| 1061 | * Returns FAIL or OK. |
| 1062 | */ |
| 1063 | int |
| 1064 | class_object_index( |
| 1065 | char_u **arg, |
| 1066 | typval_T *rettv, |
| 1067 | evalarg_T *evalarg, |
| 1068 | int verbose UNUSED) // give error messages |
| 1069 | { |
| 1070 | // int evaluate = evalarg != NULL |
| 1071 | // && (evalarg->eval_flags & EVAL_EVALUATE); |
| 1072 | |
| 1073 | if (VIM_ISWHITE((*arg)[1])) |
| 1074 | { |
| 1075 | semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg); |
| 1076 | return FAIL; |
| 1077 | } |
| 1078 | |
| 1079 | ++*arg; |
| 1080 | char_u *name = *arg; |
| 1081 | char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START); |
| 1082 | if (name_end == name) |
| 1083 | return FAIL; |
| 1084 | size_t len = name_end - name; |
| 1085 | |
| 1086 | class_T *cl = rettv->v_type == VAR_CLASS ? rettv->vval.v_class |
| 1087 | : rettv->vval.v_object->obj_class; |
| 1088 | if (*name_end == '(') |
| 1089 | { |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 1090 | int on_class = rettv->v_type == VAR_CLASS; |
| 1091 | int count = on_class ? cl->class_class_function_count |
| 1092 | : cl->class_obj_method_count; |
| 1093 | for (int i = 0; i < count; ++i) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1094 | { |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 1095 | ufunc_T *fp = on_class ? cl->class_class_functions[i] |
| 1096 | : cl->class_obj_methods[i]; |
Bram Moolenaar | 4ae0057 | 2022-12-09 22:49:23 +0000 | [diff] [blame] | 1097 | // Use a separate pointer to avoid that ASAN complains about |
| 1098 | // uf_name[] only being 4 characters. |
| 1099 | char_u *ufname = (char_u *)fp->uf_name; |
| 1100 | if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1101 | { |
| 1102 | typval_T argvars[MAX_FUNC_ARGS + 1]; |
| 1103 | int argcount = 0; |
| 1104 | |
| 1105 | char_u *argp = name_end; |
| 1106 | int ret = get_func_arguments(&argp, evalarg, 0, |
| 1107 | argvars, &argcount); |
| 1108 | if (ret == FAIL) |
| 1109 | return FAIL; |
| 1110 | |
| 1111 | funcexe_T funcexe; |
| 1112 | CLEAR_FIELD(funcexe); |
| 1113 | funcexe.fe_evaluate = TRUE; |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 1114 | if (rettv->v_type == VAR_OBJECT) |
| 1115 | { |
| 1116 | funcexe.fe_object = rettv->vval.v_object; |
| 1117 | ++funcexe.fe_object->obj_refcount; |
| 1118 | } |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1119 | |
Bram Moolenaar | d28d7b9 | 2022-12-08 20:42:00 +0000 | [diff] [blame] | 1120 | // Clear the class or object after calling the function, in |
| 1121 | // case the refcount is one. |
| 1122 | typval_T tv_tofree = *rettv; |
| 1123 | rettv->v_type = VAR_UNKNOWN; |
| 1124 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1125 | // Call the user function. Result goes into rettv; |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1126 | int error = call_user_func_check(fp, argcount, argvars, |
| 1127 | rettv, &funcexe, NULL); |
| 1128 | |
Bram Moolenaar | d28d7b9 | 2022-12-08 20:42:00 +0000 | [diff] [blame] | 1129 | // Clear the previous rettv and the arguments. |
| 1130 | clear_tv(&tv_tofree); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1131 | for (int idx = 0; idx < argcount; ++idx) |
| 1132 | clear_tv(&argvars[idx]); |
| 1133 | |
| 1134 | if (error != FCERR_NONE) |
| 1135 | { |
| 1136 | user_func_error(error, printable_func_name(fp), |
| 1137 | funcexe.fe_found_var); |
| 1138 | return FAIL; |
| 1139 | } |
| 1140 | *arg = argp; |
| 1141 | return OK; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name); |
| 1146 | } |
| 1147 | |
| 1148 | else if (rettv->v_type == VAR_OBJECT) |
| 1149 | { |
| 1150 | for (int i = 0; i < cl->class_obj_member_count; ++i) |
| 1151 | { |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 1152 | ocmember_T *m = &cl->class_obj_members[i]; |
| 1153 | if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1154 | { |
Bram Moolenaar | 3d473ee | 2022-12-14 20:59:32 +0000 | [diff] [blame] | 1155 | if (*name == '_') |
| 1156 | { |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 1157 | semsg(_(e_cannot_access_private_member_str), m->ocm_name); |
Bram Moolenaar | 3d473ee | 2022-12-14 20:59:32 +0000 | [diff] [blame] | 1158 | return FAIL; |
| 1159 | } |
| 1160 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1161 | // The object only contains a pointer to the class, the member |
| 1162 | // values array follows right after that. |
| 1163 | object_T *obj = rettv->vval.v_object; |
| 1164 | typval_T *tv = (typval_T *)(obj + 1) + i; |
| 1165 | copy_tv(tv, rettv); |
| 1166 | object_unref(obj); |
| 1167 | |
| 1168 | *arg = name_end; |
| 1169 | return OK; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name); |
| 1174 | } |
| 1175 | |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 1176 | else if (rettv->v_type == VAR_CLASS) |
| 1177 | { |
| 1178 | // class member |
| 1179 | for (int i = 0; i < cl->class_class_member_count; ++i) |
| 1180 | { |
| 1181 | ocmember_T *m = &cl->class_class_members[i]; |
| 1182 | if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL) |
| 1183 | { |
| 1184 | if (*name == '_') |
| 1185 | { |
| 1186 | semsg(_(e_cannot_access_private_member_str), m->ocm_name); |
| 1187 | return FAIL; |
| 1188 | } |
| 1189 | |
| 1190 | typval_T *tv = &cl->class_members_tv[i]; |
| 1191 | copy_tv(tv, rettv); |
| 1192 | class_unref(cl); |
| 1193 | |
| 1194 | *arg = name_end; |
| 1195 | return OK; |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | semsg(_(e_member_not_found_on_class_str_str), cl->class_name, name); |
| 1200 | } |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1201 | |
| 1202 | return FAIL; |
| 1203 | } |
| 1204 | |
| 1205 | /* |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 1206 | * If "arg" points to a class or object method, return it. |
| 1207 | * Otherwise return NULL. |
| 1208 | */ |
| 1209 | ufunc_T * |
| 1210 | find_class_func(char_u **arg) |
| 1211 | { |
| 1212 | char_u *name = *arg; |
| 1213 | char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START); |
| 1214 | if (name_end == name || *name_end != '.') |
| 1215 | return NULL; |
| 1216 | |
| 1217 | size_t len = name_end - name; |
| 1218 | typval_T tv; |
| 1219 | tv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 993dbc3 | 2023-01-01 20:31:30 +0000 | [diff] [blame] | 1220 | if (eval_variable(name, (int)len, |
| 1221 | 0, &tv, NULL, EVAL_VAR_NOAUTOLOAD) == FAIL) |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 1222 | return NULL; |
| 1223 | if (tv.v_type != VAR_CLASS && tv.v_type != VAR_OBJECT) |
Bram Moolenaar | eb53350 | 2022-12-14 15:06:11 +0000 | [diff] [blame] | 1224 | goto fail_after_eval; |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 1225 | |
| 1226 | class_T *cl = tv.v_type == VAR_CLASS ? tv.vval.v_class |
| 1227 | : tv.vval.v_object->obj_class; |
| 1228 | if (cl == NULL) |
Bram Moolenaar | eb53350 | 2022-12-14 15:06:11 +0000 | [diff] [blame] | 1229 | goto fail_after_eval; |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 1230 | char_u *fname = name_end + 1; |
| 1231 | char_u *fname_end = find_name_end(fname, NULL, NULL, FNE_CHECK_START); |
| 1232 | if (fname_end == fname) |
Bram Moolenaar | eb53350 | 2022-12-14 15:06:11 +0000 | [diff] [blame] | 1233 | goto fail_after_eval; |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 1234 | len = fname_end - fname; |
| 1235 | |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 1236 | int count = tv.v_type == VAR_CLASS ? cl->class_class_function_count |
| 1237 | : cl->class_obj_method_count; |
| 1238 | ufunc_T **funcs = tv.v_type == VAR_CLASS ? cl->class_class_functions |
| 1239 | : cl->class_obj_methods; |
| 1240 | for (int i = 0; i < count; ++i) |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 1241 | { |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 1242 | ufunc_T *fp = funcs[i]; |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 1243 | // Use a separate pointer to avoid that ASAN complains about |
| 1244 | // uf_name[] only being 4 characters. |
| 1245 | char_u *ufname = (char_u *)fp->uf_name; |
| 1246 | if (STRNCMP(fname, ufname, len) == 0 && ufname[len] == NUL) |
Bram Moolenaar | eb53350 | 2022-12-14 15:06:11 +0000 | [diff] [blame] | 1247 | { |
| 1248 | clear_tv(&tv); |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 1249 | return fp; |
Bram Moolenaar | eb53350 | 2022-12-14 15:06:11 +0000 | [diff] [blame] | 1250 | } |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Bram Moolenaar | eb53350 | 2022-12-14 15:06:11 +0000 | [diff] [blame] | 1253 | fail_after_eval: |
| 1254 | clear_tv(&tv); |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 1255 | return NULL; |
| 1256 | } |
| 1257 | |
| 1258 | /* |
Bram Moolenaar | 6acf757 | 2023-01-01 19:53:30 +0000 | [diff] [blame] | 1259 | * If "name[len]" is a class member in cctx->ctx_ufunc->uf_class return the |
| 1260 | * index in class.class_class_members[]. |
| 1261 | * If "cl_ret" is not NULL set it to the class. |
| 1262 | * Otherwise return -1; |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 1263 | */ |
| 1264 | int |
Bram Moolenaar | 6acf757 | 2023-01-01 19:53:30 +0000 | [diff] [blame] | 1265 | class_member_index(char_u *name, size_t len, class_T **cl_ret, cctx_T *cctx) |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 1266 | { |
Bram Moolenaar | 6acf757 | 2023-01-01 19:53:30 +0000 | [diff] [blame] | 1267 | if (cctx == NULL || cctx->ctx_ufunc == NULL |
| 1268 | || cctx->ctx_ufunc->uf_class == NULL) |
| 1269 | return -1; |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 1270 | class_T *cl = cctx->ctx_ufunc->uf_class; |
| 1271 | |
Bram Moolenaar | 6acf757 | 2023-01-01 19:53:30 +0000 | [diff] [blame] | 1272 | for (int i = 0; i < cl->class_class_member_count; ++i) |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 1273 | { |
Bram Moolenaar | 6acf757 | 2023-01-01 19:53:30 +0000 | [diff] [blame] | 1274 | ocmember_T *m = &cl->class_class_members[i]; |
| 1275 | if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL) |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 1276 | { |
Bram Moolenaar | 6acf757 | 2023-01-01 19:53:30 +0000 | [diff] [blame] | 1277 | if (cl_ret != NULL) |
| 1278 | *cl_ret = cl; |
| 1279 | return i; |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 1280 | } |
| 1281 | } |
Bram Moolenaar | 6acf757 | 2023-01-01 19:53:30 +0000 | [diff] [blame] | 1282 | return -1; |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | /* |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1286 | * Make a copy of an object. |
| 1287 | */ |
| 1288 | void |
| 1289 | copy_object(typval_T *from, typval_T *to) |
| 1290 | { |
| 1291 | *to = *from; |
| 1292 | if (to->vval.v_object != NULL) |
| 1293 | ++to->vval.v_object->obj_refcount; |
| 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | * Free an object. |
| 1298 | */ |
| 1299 | static void |
| 1300 | object_clear(object_T *obj) |
| 1301 | { |
| 1302 | class_T *cl = obj->obj_class; |
| 1303 | |
| 1304 | // the member values are just after the object structure |
| 1305 | typval_T *tv = (typval_T *)(obj + 1); |
| 1306 | for (int i = 0; i < cl->class_obj_member_count; ++i) |
| 1307 | clear_tv(tv + i); |
| 1308 | |
Bram Moolenaar | d28d7b9 | 2022-12-08 20:42:00 +0000 | [diff] [blame] | 1309 | // Remove from the list headed by "first_object". |
| 1310 | object_cleared(obj); |
| 1311 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1312 | vim_free(obj); |
Bram Moolenaar | d28d7b9 | 2022-12-08 20:42:00 +0000 | [diff] [blame] | 1313 | class_unref(cl); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | * Unreference an object. |
| 1318 | */ |
| 1319 | void |
| 1320 | object_unref(object_T *obj) |
| 1321 | { |
| 1322 | if (obj != NULL && --obj->obj_refcount <= 0) |
| 1323 | object_clear(obj); |
| 1324 | } |
| 1325 | |
| 1326 | /* |
| 1327 | * Make a copy of a class. |
| 1328 | */ |
| 1329 | void |
| 1330 | copy_class(typval_T *from, typval_T *to) |
| 1331 | { |
| 1332 | *to = *from; |
| 1333 | if (to->vval.v_class != NULL) |
| 1334 | ++to->vval.v_class->class_refcount; |
| 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * Unreference a class. Free it when the reference count goes down to zero. |
| 1339 | */ |
| 1340 | void |
Bram Moolenaar | d28d7b9 | 2022-12-08 20:42:00 +0000 | [diff] [blame] | 1341 | class_unref(class_T *cl) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1342 | { |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 1343 | if (cl != NULL && --cl->class_refcount <= 0 && cl->class_name != NULL) |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1344 | { |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 1345 | // Freeing what the class contains may recursively come back here. |
| 1346 | // Clear "class_name" first, if it is NULL the class does not need to |
| 1347 | // be freed. |
| 1348 | VIM_CLEAR(cl->class_name); |
| 1349 | |
Bram Moolenaar | 8367716 | 2023-01-08 19:54:10 +0000 | [diff] [blame] | 1350 | class_unref(cl->class_extends); |
| 1351 | |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 1352 | for (int i = 0; i < cl->class_interface_count; ++i) |
Bram Moolenaar | a94bd9d | 2023-01-12 15:01:32 +0000 | [diff] [blame^] | 1353 | { |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 1354 | vim_free(((char_u **)cl->class_interfaces)[i]); |
Bram Moolenaar | a94bd9d | 2023-01-12 15:01:32 +0000 | [diff] [blame^] | 1355 | if (cl->class_interfaces_cl[i] != NULL) |
| 1356 | class_unref(cl->class_interfaces_cl[i]); |
| 1357 | } |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 1358 | vim_free(cl->class_interfaces); |
Bram Moolenaar | a94bd9d | 2023-01-12 15:01:32 +0000 | [diff] [blame^] | 1359 | vim_free(cl->class_interfaces_cl); |
Bram Moolenaar | 94674f2 | 2023-01-06 18:42:20 +0000 | [diff] [blame] | 1360 | |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 1361 | for (int i = 0; i < cl->class_class_member_count; ++i) |
| 1362 | { |
| 1363 | ocmember_T *m = &cl->class_class_members[i]; |
| 1364 | vim_free(m->ocm_name); |
| 1365 | vim_free(m->ocm_init); |
| 1366 | if (cl->class_members_tv != NULL) |
| 1367 | clear_tv(&cl->class_members_tv[i]); |
| 1368 | } |
| 1369 | vim_free(cl->class_class_members); |
| 1370 | vim_free(cl->class_members_tv); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1371 | |
| 1372 | for (int i = 0; i < cl->class_obj_member_count; ++i) |
| 1373 | { |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 1374 | ocmember_T *m = &cl->class_obj_members[i]; |
| 1375 | vim_free(m->ocm_name); |
| 1376 | vim_free(m->ocm_init); |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1377 | } |
| 1378 | vim_free(cl->class_obj_members); |
| 1379 | |
Bram Moolenaar | ec8b74f | 2023-01-01 14:11:27 +0000 | [diff] [blame] | 1380 | for (int i = 0; i < cl->class_class_function_count; ++i) |
| 1381 | { |
| 1382 | ufunc_T *uf = cl->class_class_functions[i]; |
| 1383 | func_clear_free(uf, FALSE); |
| 1384 | } |
| 1385 | vim_free(cl->class_class_functions); |
| 1386 | |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 1387 | for (int i = 0; i < cl->class_obj_method_count; ++i) |
| 1388 | { |
| 1389 | ufunc_T *uf = cl->class_obj_methods[i]; |
| 1390 | func_clear_free(uf, FALSE); |
| 1391 | } |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1392 | vim_free(cl->class_obj_methods); |
| 1393 | |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 1394 | clear_type_list(&cl->class_type_list); |
| 1395 | |
| 1396 | vim_free(cl); |
| 1397 | } |
| 1398 | } |
| 1399 | |
Bram Moolenaar | d28d7b9 | 2022-12-08 20:42:00 +0000 | [diff] [blame] | 1400 | static object_T *first_object = NULL; |
| 1401 | |
| 1402 | /* |
| 1403 | * Call this function when an object has been created. It will be added to the |
| 1404 | * list headed by "first_object". |
| 1405 | */ |
| 1406 | void |
| 1407 | object_created(object_T *obj) |
| 1408 | { |
| 1409 | if (first_object != NULL) |
| 1410 | { |
| 1411 | obj->obj_next_used = first_object; |
| 1412 | first_object->obj_prev_used = obj; |
| 1413 | } |
| 1414 | first_object = obj; |
| 1415 | } |
| 1416 | |
| 1417 | /* |
| 1418 | * Call this function when an object has been cleared and is about to be freed. |
| 1419 | * It is removed from the list headed by "first_object". |
| 1420 | */ |
| 1421 | void |
| 1422 | object_cleared(object_T *obj) |
| 1423 | { |
| 1424 | if (obj->obj_next_used != NULL) |
| 1425 | obj->obj_next_used->obj_prev_used = obj->obj_prev_used; |
| 1426 | if (obj->obj_prev_used != NULL) |
| 1427 | obj->obj_prev_used->obj_next_used = obj->obj_next_used; |
| 1428 | else if (first_object == obj) |
| 1429 | first_object = obj->obj_next_used; |
| 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * Go through the list of all objects and free items without "copyID". |
| 1434 | */ |
| 1435 | int |
| 1436 | object_free_nonref(int copyID) |
| 1437 | { |
| 1438 | int did_free = FALSE; |
| 1439 | object_T *next_obj; |
| 1440 | |
| 1441 | for (object_T *obj = first_object; obj != NULL; obj = next_obj) |
| 1442 | { |
| 1443 | next_obj = obj->obj_next_used; |
| 1444 | if ((obj->obj_copyID & COPYID_MASK) != (copyID & COPYID_MASK)) |
| 1445 | { |
| 1446 | // Free the object and items it contains. |
| 1447 | object_clear(obj); |
| 1448 | did_free = TRUE; |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | return did_free; |
| 1453 | } |
| 1454 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1455 | |
| 1456 | #endif // FEAT_EVAL |