Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +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 | /* |
Kota Kato | 948a389 | 2022-08-16 16:09:59 +0100 | [diff] [blame] | 11 | * vim9expr.c: Dealing with compiled function expressions |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | d02dce2 | 2022-01-18 17:43:04 +0000 | [diff] [blame] | 24 | // flag passed from compile_subscript() to compile_load_scriptvar() |
| 25 | static int paren_follows_after_expr = 0; |
| 26 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 27 | /* |
| 28 | * Generate code for any ppconst entries. |
| 29 | */ |
| 30 | int |
| 31 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 32 | { |
| 33 | int i; |
| 34 | int ret = OK; |
| 35 | int save_skip = cctx->ctx_skip; |
| 36 | |
| 37 | cctx->ctx_skip = SKIP_NOT; |
| 38 | for (i = 0; i < ppconst->pp_used; ++i) |
| 39 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 40 | ret = FAIL; |
| 41 | ppconst->pp_used = 0; |
| 42 | cctx->ctx_skip = save_skip; |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Check that the last item of "ppconst" is a bool, if there is an item. |
| 48 | */ |
| 49 | static int |
| 50 | check_ppconst_bool(ppconst_T *ppconst) |
| 51 | { |
| 52 | if (ppconst->pp_used > 0) |
| 53 | { |
| 54 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 55 | where_T where = WHERE_INIT; |
| 56 | |
| 57 | return check_typval_type(&t_bool, tv, where); |
| 58 | } |
| 59 | return OK; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Clear ppconst constants. Used when failing. |
| 64 | */ |
| 65 | void |
| 66 | clear_ppconst(ppconst_T *ppconst) |
| 67 | { |
| 68 | int i; |
| 69 | |
| 70 | for (i = 0; i < ppconst->pp_used; ++i) |
| 71 | clear_tv(&ppconst->pp_tv[i]); |
| 72 | ppconst->pp_used = 0; |
| 73 | } |
| 74 | |
| 75 | /* |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 76 | * Compile getting a member from a tuple. Stack has the indexable value and |
| 77 | * the index or the two indexes of a slice. |
| 78 | */ |
| 79 | static int |
| 80 | compile_tuple_member( |
| 81 | type2_T *typep, |
| 82 | int is_slice, |
| 83 | cctx_T *cctx) |
| 84 | { |
| 85 | if (is_slice) |
| 86 | { |
| 87 | if (generate_instr_drop(cctx, ISN_TUPLESLICE, 2) == FAIL) |
| 88 | return FAIL; |
| 89 | // a copy is made so the member type is no longer declared |
| 90 | if (typep->type_decl->tt_type == VAR_TUPLE) |
| 91 | typep->type_decl = &t_tuple_any; |
| 92 | |
| 93 | // a copy is made, the composite is no longer "const" |
| 94 | if (typep->type_curr->tt_flags & TTFLAG_CONST) |
| 95 | { |
| 96 | type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list); |
| 97 | |
| 98 | if (type != typep->type_curr) // did get a copy |
| 99 | { |
| 100 | type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC); |
| 101 | typep->type_curr = type; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | if (typep->type_curr->tt_type == VAR_TUPLE) |
| 108 | { |
| 109 | if (typep->type_curr->tt_argcount == 1) |
| 110 | { |
| 111 | if (typep->type_curr->tt_flags & TTFLAG_VARARGS) |
| 112 | typep->type_curr |
| 113 | = typep->type_curr->tt_args[0]->tt_member; |
| 114 | else |
| 115 | typep->type_curr = typep->type_curr->tt_args[0]; |
| 116 | } |
| 117 | else |
| 118 | typep->type_curr = &t_any; |
| 119 | if (typep->type_decl->tt_type == VAR_TUPLE) |
| 120 | { |
| 121 | if (typep->type_decl->tt_argcount == 1) |
| 122 | { |
| 123 | if (typep->type_decl->tt_flags & TTFLAG_VARARGS) |
| 124 | typep->type_decl |
| 125 | = typep->type_decl->tt_args[0]->tt_member; |
| 126 | else |
| 127 | typep->type_decl = typep->type_decl->tt_args[0]; |
| 128 | } |
| 129 | else |
| 130 | typep->type_curr = &t_any; |
| 131 | } |
| 132 | else |
| 133 | typep->type_decl = typep->type_curr; |
| 134 | } |
| 135 | if (generate_instr_drop(cctx, ISN_TUPLEINDEX, 1) == FAIL) |
| 136 | return FAIL; |
| 137 | } |
| 138 | |
| 139 | return OK; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Compile getting a member from a list/tuple/dict/string/blob. Stack has the |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 144 | * indexable value and the index or the two indexes of a slice. |
| 145 | * "keeping_dict" is used for dict[func](arg) to pass dict to func. |
| 146 | */ |
| 147 | int |
| 148 | compile_member(int is_slice, int *keeping_dict, cctx_T *cctx) |
| 149 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 150 | type2_T *typep; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 151 | garray_T *stack = &cctx->ctx_type_stack; |
| 152 | vartype_T vartype; |
| 153 | type_T *idxtype; |
| 154 | |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 155 | // We can index a list, tuple, dict and blob. If we don't know the type |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 156 | // we can use the index value type. If we still don't know use an "ANY" |
| 157 | // instruction. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 158 | // TODO: what about the decl type? |
| 159 | typep = (((type2_T *)stack->ga_data) + stack->ga_len - (is_slice ? 3 : 2)); |
| 160 | vartype = typep->type_curr->tt_type; |
| 161 | idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 162 | // If the index is a string, the variable must be a Dict. |
Bram Moolenaar | 4913d42 | 2022-10-17 13:13:32 +0100 | [diff] [blame] | 163 | if ((typep->type_curr->tt_type == VAR_ANY |
| 164 | || typep->type_curr->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 165 | && idxtype == &t_string) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 166 | vartype = VAR_DICT; |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 167 | if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB |
| 168 | || vartype == VAR_TUPLE) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 169 | { |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 170 | if (need_type(idxtype, &t_number, FALSE, |
| 171 | -1, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 172 | return FAIL; |
| 173 | if (is_slice) |
| 174 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 175 | idxtype = get_type_on_stack(cctx, 1); |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 176 | if (need_type(idxtype, &t_number, FALSE, |
| 177 | -2, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 178 | return FAIL; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (vartype == VAR_DICT) |
| 183 | { |
| 184 | if (is_slice) |
| 185 | { |
| 186 | emsg(_(e_cannot_slice_dictionary)); |
| 187 | return FAIL; |
| 188 | } |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 189 | if (typep->type_curr->tt_type == VAR_DICT) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 190 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 191 | typep->type_curr = typep->type_curr->tt_member; |
| 192 | if (typep->type_curr == &t_unknown) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 193 | // empty dict was used |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 194 | typep->type_curr = &t_any; |
| 195 | if (typep->type_decl->tt_type == VAR_DICT) |
| 196 | { |
| 197 | typep->type_decl = typep->type_decl->tt_member; |
| 198 | if (typep->type_decl == &t_unknown) |
| 199 | // empty dict was used |
| 200 | typep->type_decl = &t_any; |
| 201 | } |
| 202 | else |
| 203 | typep->type_decl = typep->type_curr; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 204 | } |
| 205 | else |
| 206 | { |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 207 | if (need_type(typep->type_curr, &t_dict_any, FALSE, |
| 208 | -2, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 209 | return FAIL; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 210 | typep->type_curr = &t_any; |
| 211 | typep->type_decl = &t_any; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 212 | } |
Yegappan Lakshmanan | bce51d9 | 2024-04-15 19:19:52 +0200 | [diff] [blame] | 213 | if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL |
Bram Moolenaar | d0132f4 | 2022-05-12 11:05:40 +0100 | [diff] [blame] | 214 | || generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 215 | return FAIL; |
| 216 | if (keeping_dict != NULL) |
| 217 | *keeping_dict = TRUE; |
| 218 | } |
| 219 | else if (vartype == VAR_STRING) |
| 220 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 221 | typep->type_curr = &t_string; |
| 222 | typep->type_decl = &t_string; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 223 | if ((is_slice |
| 224 | ? generate_instr_drop(cctx, ISN_STRSLICE, 2) |
| 225 | : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL) |
| 226 | return FAIL; |
| 227 | } |
| 228 | else if (vartype == VAR_BLOB) |
| 229 | { |
| 230 | if (is_slice) |
| 231 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 232 | typep->type_curr = &t_blob; |
| 233 | typep->type_decl = &t_blob; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 234 | if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL) |
| 235 | return FAIL; |
| 236 | } |
| 237 | else |
| 238 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 239 | typep->type_curr = &t_number; |
| 240 | typep->type_decl = &t_number; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 241 | if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL) |
| 242 | return FAIL; |
| 243 | } |
| 244 | } |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 245 | else if (vartype == VAR_TUPLE) |
| 246 | { |
| 247 | if (compile_tuple_member(typep, is_slice, cctx) == FAIL) |
| 248 | return FAIL; |
| 249 | } |
Bram Moolenaar | 4913d42 | 2022-10-17 13:13:32 +0100 | [diff] [blame] | 250 | else if (vartype == VAR_LIST || typep->type_curr->tt_type == VAR_ANY |
| 251 | || typep->type_curr->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 252 | { |
| 253 | if (is_slice) |
| 254 | { |
| 255 | if (generate_instr_drop(cctx, |
| 256 | vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE, |
| 257 | 2) == FAIL) |
| 258 | return FAIL; |
Bram Moolenaar | 5f4ef5f | 2022-02-06 18:36:53 +0000 | [diff] [blame] | 259 | // a copy is made so the member type is no longer declared |
| 260 | if (typep->type_decl->tt_type == VAR_LIST) |
| 261 | typep->type_decl = &t_list_any; |
Bram Moolenaar | adbc08f | 2022-11-06 18:27:17 +0000 | [diff] [blame] | 262 | |
| 263 | // a copy is made, the composite is no longer "const" |
| 264 | if (typep->type_curr->tt_flags & TTFLAG_CONST) |
| 265 | { |
| 266 | type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list); |
| 267 | |
| 268 | if (type != typep->type_curr) // did get a copy |
| 269 | { |
| 270 | type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC); |
| 271 | typep->type_curr = type; |
| 272 | } |
| 273 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 274 | } |
| 275 | else |
| 276 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 277 | if (typep->type_curr->tt_type == VAR_LIST) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 278 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 279 | typep->type_curr = typep->type_curr->tt_member; |
| 280 | if (typep->type_curr == &t_unknown) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 281 | // empty list was used |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 282 | typep->type_curr = &t_any; |
| 283 | if (typep->type_decl->tt_type == VAR_LIST) |
| 284 | { |
| 285 | typep->type_decl = typep->type_decl->tt_member; |
| 286 | if (typep->type_decl == &t_unknown) |
| 287 | // empty list was used |
| 288 | typep->type_decl = &t_any; |
| 289 | } |
| 290 | else |
| 291 | typep->type_decl = typep->type_curr; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 292 | } |
| 293 | if (generate_instr_drop(cctx, |
| 294 | vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) |
| 295 | == FAIL) |
| 296 | return FAIL; |
| 297 | } |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | switch (vartype) |
| 302 | { |
| 303 | case VAR_FUNC: |
| 304 | case VAR_PARTIAL: |
| 305 | emsg(_(e_cannot_index_a_funcref)); |
| 306 | break; |
| 307 | case VAR_BOOL: |
| 308 | case VAR_SPECIAL: |
| 309 | case VAR_JOB: |
| 310 | case VAR_CHANNEL: |
| 311 | case VAR_INSTR: |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 312 | case VAR_CLASS: |
| 313 | case VAR_OBJECT: |
Yegappan Lakshmanan | ec3cebb | 2023-10-27 19:35:26 +0200 | [diff] [blame] | 314 | case VAR_TYPEALIAS: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 315 | case VAR_UNKNOWN: |
| 316 | case VAR_ANY: |
| 317 | case VAR_VOID: |
| 318 | emsg(_(e_cannot_index_special_variable)); |
| 319 | break; |
| 320 | default: |
| 321 | emsg(_(e_string_list_dict_or_blob_required)); |
| 322 | } |
| 323 | return FAIL; |
| 324 | } |
| 325 | return OK; |
| 326 | } |
| 327 | |
| 328 | /* |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 329 | * Returns TRUE if the current function is inside the class "cl" or one of the |
| 330 | * parent classes. |
| 331 | */ |
| 332 | static int |
| 333 | inside_class_hierarchy(cctx_T *cctx_arg, class_T *cl) |
| 334 | { |
| 335 | for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer) |
| 336 | { |
| 337 | if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class != NULL) |
| 338 | { |
| 339 | class_T *clp = cctx->ctx_ufunc->uf_class; |
| 340 | while (clp != NULL) |
| 341 | { |
| 342 | if (clp == cl) |
| 343 | return TRUE; |
| 344 | clp = clp->class_extends; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return FALSE; |
| 350 | } |
| 351 | |
| 352 | /* |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 353 | * Compile ".member" coming after an object or class. |
| 354 | */ |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 355 | static int |
| 356 | compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type) |
| 357 | { |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 358 | int m_idx; |
| 359 | |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 360 | if (VIM_ISWHITE((*arg)[1])) |
| 361 | { |
| 362 | semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg); |
| 363 | return FAIL; |
| 364 | } |
| 365 | |
Bram Moolenaar | b1e32ac | 2023-02-21 12:38:51 +0000 | [diff] [blame] | 366 | class_T *cl = type->tt_class; |
Yegappan Lakshmanan | b5f463c | 2025-02-16 16:25:24 +0100 | [diff] [blame] | 367 | int is_super = ((type->tt_flags & TTFLAG_SUPER) == TTFLAG_SUPER); |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 368 | if (type == &t_super) |
| 369 | { |
| 370 | if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL) |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 371 | { |
RestorerZ | 7fe8f43 | 2023-09-24 23:21:24 +0200 | [diff] [blame] | 372 | emsg(_(e_using_super_not_in_class_method)); |
Bram Moolenaar | 6aa0937 | 2023-01-11 17:59:38 +0000 | [diff] [blame] | 373 | return FAIL; |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 374 | } |
Bram Moolenaar | 6aa0937 | 2023-01-11 17:59:38 +0000 | [diff] [blame] | 375 | is_super = TRUE; |
| 376 | cl = cctx->ctx_ufunc->uf_class; |
| 377 | // Remove &t_super from the stack. |
| 378 | --cctx->ctx_type_stack.ga_len; |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 379 | } |
| 380 | else if (type->tt_type == VAR_CLASS) |
Bram Moolenaar | 3259ff3 | 2023-01-04 18:54:09 +0000 | [diff] [blame] | 381 | { |
| 382 | garray_T *instr = &cctx->ctx_instr; |
| 383 | if (instr->ga_len > 0) |
| 384 | { |
| 385 | isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 386 | if (isn->isn_type == ISN_LOADSCRIPT) |
| 387 | { |
| 388 | // The class was recognized as a script item. We only need |
| 389 | // to know what class it is, drop the instruction. |
| 390 | --instr->ga_len; |
| 391 | vim_free(isn->isn_arg.script.scriptref); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Ernie Rael | f77a7f7 | 2023-03-03 15:05:30 +0000 | [diff] [blame] | 396 | if (cl == NULL) |
| 397 | { |
| 398 | // TODO: this should not give an error but be handled at runtime |
| 399 | emsg(_(e_incomplete_type)); |
| 400 | return FAIL; |
| 401 | } |
| 402 | |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 403 | ++*arg; |
| 404 | char_u *name = *arg; |
| 405 | char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START); |
| 406 | if (name_end == name) |
Yegappan Lakshmanan | 5ce1e4a | 2025-04-07 21:09:18 +0200 | [diff] [blame] | 407 | { |
| 408 | emsg(_(e_missing_name_after_dot)); |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 409 | return FAIL; |
Yegappan Lakshmanan | 5ce1e4a | 2025-04-07 21:09:18 +0200 | [diff] [blame] | 410 | } |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 411 | size_t len = name_end - name; |
| 412 | |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 413 | if (*name_end == '(') |
| 414 | { |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 415 | int function_count; |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 416 | int child_count; |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 417 | ufunc_T **functions; |
| 418 | |
Bram Moolenaar | 46ab925 | 2023-01-03 14:01:21 +0000 | [diff] [blame] | 419 | if (type->tt_type == VAR_CLASS) |
| 420 | { |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 421 | function_count = cl->class_class_function_count; |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 422 | child_count = cl->class_class_function_count_child; |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 423 | functions = cl->class_class_functions; |
Bram Moolenaar | 46ab925 | 2023-01-03 14:01:21 +0000 | [diff] [blame] | 424 | } |
| 425 | else |
| 426 | { |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 427 | // type->tt_type == VAR_OBJECT: method call |
Ernie Rael | 58c9579 | 2024-08-13 23:27:22 +0200 | [diff] [blame] | 428 | // When compiling Func and doing "super.SomeFunc()", must be in the |
| 429 | // class context that defines Func. |
| 430 | if (is_super) |
| 431 | cl = cctx->ctx_ufunc->uf_defclass; |
| 432 | |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 433 | function_count = cl->class_obj_method_count; |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 434 | child_count = cl->class_obj_method_count_child; |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 435 | functions = cl->class_obj_methods; |
Bram Moolenaar | 46ab925 | 2023-01-03 14:01:21 +0000 | [diff] [blame] | 436 | } |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 437 | |
| 438 | ufunc_T *ufunc = NULL; |
Bram Moolenaar | d0200c8 | 2023-01-28 15:19:40 +0000 | [diff] [blame] | 439 | int fi; |
| 440 | for (fi = is_super ? child_count : 0; fi < function_count; ++fi) |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 441 | { |
Bram Moolenaar | d0200c8 | 2023-01-28 15:19:40 +0000 | [diff] [blame] | 442 | ufunc_T *fp = functions[fi]; |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 443 | // Use a separate pointer to avoid that ASAN complains about |
| 444 | // uf_name[] only being 4 characters. |
| 445 | char_u *ufname = (char_u *)fp->uf_name; |
| 446 | if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL) |
| 447 | { |
| 448 | ufunc = fp; |
| 449 | break; |
| 450 | } |
| 451 | } |
Ernie Rael | bce60c4 | 2025-01-19 10:03:00 +0100 | [diff] [blame] | 452 | |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 453 | ocmember_T *ocm = NULL; |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 454 | if (ufunc == NULL) |
| 455 | { |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 456 | // could be a funcref in a member variable |
| 457 | ocm = member_lookup(cl, type->tt_type, name, len, &m_idx); |
| 458 | if (ocm == NULL || ocm->ocm_type->tt_type != VAR_FUNC) |
| 459 | { |
| 460 | method_not_found_msg(cl, type->tt_type, name, len); |
| 461 | return FAIL; |
| 462 | } |
| 463 | if (type->tt_type == VAR_CLASS) |
| 464 | { |
Yegappan Lakshmanan | d7b616d | 2023-10-19 10:47:53 +0200 | [diff] [blame] | 465 | // Remove the class type from the stack |
| 466 | --cctx->ctx_type_stack.ga_len; |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 467 | if (generate_CLASSMEMBER(cctx, TRUE, cl, m_idx) == FAIL) |
| 468 | return FAIL; |
| 469 | } |
| 470 | else |
| 471 | { |
Yegappan Lakshmanan | c10342d | 2025-01-11 09:39:01 +0100 | [diff] [blame] | 472 | int status; |
| 473 | |
| 474 | if (IS_INTERFACE(cl)) |
| 475 | status = generate_GET_ITF_MEMBER(cctx, cl, m_idx, |
| 476 | ocm->ocm_type); |
| 477 | else |
| 478 | status = generate_GET_OBJ_MEMBER(cctx, m_idx, |
| 479 | ocm->ocm_type); |
| 480 | if (status == FAIL) |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 481 | return FAIL; |
| 482 | } |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Yegappan Lakshmanan | cb848b6 | 2025-01-20 21:38:09 +0100 | [diff] [blame] | 485 | if (is_super && ufunc != NULL && IS_ABSTRACT_METHOD(ufunc)) |
Ernie Rael | bce60c4 | 2025-01-19 10:03:00 +0100 | [diff] [blame] | 486 | { |
| 487 | // Trying to invoke an abstract method in a super class is not |
| 488 | // allowed. |
| 489 | semsg(_(e_abstract_method_str_direct), ufunc->uf_name, |
| 490 | ufunc->uf_defclass->class_name); |
| 491 | return FAIL; |
| 492 | } |
| 493 | |
Yegappan Lakshmanan | c30a90d | 2023-09-15 20:14:55 +0200 | [diff] [blame] | 494 | // A private object method can be used only inside the class where it |
| 495 | // is defined or in one of the child classes. |
| 496 | // A private class method can be used only in the class where it is |
| 497 | // defined. |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 498 | if (ocm == NULL && *ufunc->uf_name == '_' && |
Yegappan Lakshmanan | c30a90d | 2023-09-15 20:14:55 +0200 | [diff] [blame] | 499 | ((type->tt_type == VAR_OBJECT |
| 500 | && !inside_class_hierarchy(cctx, cl)) |
| 501 | || (type->tt_type == VAR_CLASS |
| 502 | && cctx->ctx_ufunc->uf_class != cl))) |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 503 | { |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 504 | semsg(_(e_cannot_access_protected_method_str), name); |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 505 | return FAIL; |
| 506 | } |
| 507 | |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 508 | // Compile the arguments and call the class function or object method. |
| 509 | // The object method will know that the object is on the stack, just |
| 510 | // before the arguments. |
| 511 | *arg = skipwhite(name_end + 1); |
| 512 | int argcount = 0; |
| 513 | if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL) |
| 514 | return FAIL; |
Bram Moolenaar | d0200c8 | 2023-01-28 15:19:40 +0000 | [diff] [blame] | 515 | |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 516 | if (ocm != NULL) |
| 517 | return generate_PCALL(cctx, argcount, name, ocm->ocm_type, TRUE); |
Bram Moolenaar | d0200c8 | 2023-01-28 15:19:40 +0000 | [diff] [blame] | 518 | if (type->tt_type == VAR_OBJECT |
| 519 | && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED))) |
Ernie Rael | 58c9579 | 2024-08-13 23:27:22 +0200 | [diff] [blame] | 520 | return generate_CALL(cctx, ufunc, cl, fi, argcount, is_super); |
| 521 | return generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE); |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 522 | } |
Bram Moolenaar | 574950d | 2023-01-03 19:08:50 +0000 | [diff] [blame] | 523 | |
| 524 | if (type->tt_type == VAR_OBJECT) |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 525 | { |
zeertzjq | d9be94c | 2024-07-14 10:20:20 +0200 | [diff] [blame] | 526 | ocmember_T *m = object_member_lookup(cl, name, len, &m_idx); |
Yegappan Lakshmanan | f36bbcd | 2023-09-10 18:19:06 +0200 | [diff] [blame] | 527 | if (m_idx >= 0) |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 528 | { |
Yegappan Lakshmanan | f36bbcd | 2023-09-10 18:19:06 +0200 | [diff] [blame] | 529 | if (*name == '_' && !inside_class(cctx, cl)) |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 530 | { |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 531 | emsg_var_cl_define(e_cannot_access_protected_variable_str, |
Ernie Rael | e6c9aa5 | 2023-10-06 19:55:52 +0200 | [diff] [blame] | 532 | m->ocm_name, 0, cl); |
Yegappan Lakshmanan | f36bbcd | 2023-09-10 18:19:06 +0200 | [diff] [blame] | 533 | return FAIL; |
Ernie Rael | 18143d3 | 2023-09-04 22:30:41 +0200 | [diff] [blame] | 534 | } |
Yegappan Lakshmanan | f36bbcd | 2023-09-10 18:19:06 +0200 | [diff] [blame] | 535 | |
| 536 | *arg = name_end; |
| 537 | if (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)) |
Yegappan Lakshmanan | 5a05d37 | 2023-09-29 19:43:11 +0200 | [diff] [blame] | 538 | return generate_GET_ITF_MEMBER(cctx, cl, m_idx, m->ocm_type); |
| 539 | return generate_GET_OBJ_MEMBER(cctx, m_idx, m->ocm_type); |
Ernie Rael | 18143d3 | 2023-09-04 22:30:41 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 542 | // Could be an object method reference: "obj.Func". |
Yegappan Lakshmanan | f36bbcd | 2023-09-10 18:19:06 +0200 | [diff] [blame] | 543 | m_idx = object_method_idx(cl, name, len); |
| 544 | if (m_idx >= 0) |
Bram Moolenaar | 8dbab1d | 2023-01-27 20:14:02 +0000 | [diff] [blame] | 545 | { |
Yegappan Lakshmanan | f36bbcd | 2023-09-10 18:19:06 +0200 | [diff] [blame] | 546 | ufunc_T *fp = cl->class_obj_methods[m_idx]; |
Yegappan Lakshmanan | 3164cf8 | 2024-03-28 10:36:42 +0100 | [diff] [blame] | 547 | // Private object methods are not accessible outside the class |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 548 | if (*name == '_' && !inside_class(cctx, cl)) |
| 549 | { |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 550 | semsg(_(e_cannot_access_protected_method_str), fp->uf_name); |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 551 | return FAIL; |
| 552 | } |
| 553 | *arg = name_end; |
Yegappan Lakshmanan | f3eac69 | 2023-10-17 11:00:45 +0200 | [diff] [blame] | 554 | // Remove the object type from the stack |
| 555 | --cctx->ctx_type_stack.ga_len; |
| 556 | return generate_FUNCREF(cctx, fp, cl, TRUE, m_idx, NULL); |
Bram Moolenaar | 8dbab1d | 2023-01-27 20:14:02 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Yegappan Lakshmanan | c30a90d | 2023-09-15 20:14:55 +0200 | [diff] [blame] | 559 | member_not_found_msg(cl, VAR_OBJECT, name, len); |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 560 | } |
| 561 | else |
| 562 | { |
Bram Moolenaar | 3259ff3 | 2023-01-04 18:54:09 +0000 | [diff] [blame] | 563 | // load class member |
| 564 | int idx; |
Yegappan Lakshmanan | f36bbcd | 2023-09-10 18:19:06 +0200 | [diff] [blame] | 565 | ocmember_T *m = class_member_lookup(cl, name, len, &idx); |
| 566 | if (m != NULL) |
Bram Moolenaar | 3259ff3 | 2023-01-04 18:54:09 +0000 | [diff] [blame] | 567 | { |
Yegappan Lakshmanan | f36bbcd | 2023-09-10 18:19:06 +0200 | [diff] [blame] | 568 | // Note: type->tt_type = VAR_CLASS |
Yegappan Lakshmanan | c30a90d | 2023-09-15 20:14:55 +0200 | [diff] [blame] | 569 | // A private class variable can be accessed only in the class where |
| 570 | // it is defined. |
| 571 | if (*name == '_' && cctx->ctx_ufunc->uf_class != cl) |
Yegappan Lakshmanan | f36bbcd | 2023-09-10 18:19:06 +0200 | [diff] [blame] | 572 | { |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 573 | emsg_var_cl_define(e_cannot_access_protected_variable_str, |
Ernie Rael | e6c9aa5 | 2023-10-06 19:55:52 +0200 | [diff] [blame] | 574 | m->ocm_name, 0, cl); |
Yegappan Lakshmanan | f36bbcd | 2023-09-10 18:19:06 +0200 | [diff] [blame] | 575 | return FAIL; |
| 576 | } |
| 577 | |
Bram Moolenaar | 3259ff3 | 2023-01-04 18:54:09 +0000 | [diff] [blame] | 578 | *arg = name_end; |
Yegappan Lakshmanan | d7b616d | 2023-10-19 10:47:53 +0200 | [diff] [blame] | 579 | // Remove the class type from the stack |
| 580 | --cctx->ctx_type_stack.ga_len; |
Bram Moolenaar | 3259ff3 | 2023-01-04 18:54:09 +0000 | [diff] [blame] | 581 | return generate_CLASSMEMBER(cctx, TRUE, cl, idx); |
| 582 | } |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 583 | |
| 584 | // Could be a class method reference: "class.Func". |
| 585 | m_idx = class_method_idx(cl, name, len); |
| 586 | if (m_idx >= 0) |
| 587 | { |
| 588 | ufunc_T *fp = cl->class_class_functions[m_idx]; |
Yegappan Lakshmanan | 3164cf8 | 2024-03-28 10:36:42 +0100 | [diff] [blame] | 589 | // Private class methods are not accessible outside the class |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 590 | if (*name == '_' && !inside_class(cctx, cl)) |
| 591 | { |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 592 | semsg(_(e_cannot_access_protected_method_str), fp->uf_name); |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 593 | return FAIL; |
| 594 | } |
| 595 | *arg = name_end; |
Yegappan Lakshmanan | f3eac69 | 2023-10-17 11:00:45 +0200 | [diff] [blame] | 596 | // Remove the class type from the stack |
| 597 | --cctx->ctx_type_stack.ga_len; |
| 598 | return generate_FUNCREF(cctx, fp, cl, FALSE, m_idx, NULL); |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 599 | } |
| 600 | |
Yegappan Lakshmanan | c30a90d | 2023-09-15 20:14:55 +0200 | [diff] [blame] | 601 | member_not_found_msg(cl, VAR_CLASS, name, len); |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | return FAIL; |
| 605 | } |
| 606 | |
| 607 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 608 | * Generate an instruction to load script-local variable "name", without the |
| 609 | * leading "s:". |
| 610 | * Also finds imported variables. |
| 611 | */ |
| 612 | int |
| 613 | compile_load_scriptvar( |
| 614 | cctx_T *cctx, |
| 615 | char_u *name, // variable NUL terminated |
| 616 | char_u *start, // start of variable |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 617 | char_u **end, // end of variable, may be NULL |
| 618 | imported_T *import) // found imported item, can be NULL |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 619 | { |
| 620 | scriptitem_T *si; |
| 621 | int idx; |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 622 | imported_T *imp; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 623 | |
| 624 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
| 625 | return FAIL; |
| 626 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | b6a138e | 2022-02-08 21:17:22 +0000 | [diff] [blame] | 627 | idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 628 | if (idx >= 0) |
| 629 | { |
| 630 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 631 | |
| 632 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 633 | current_sctx.sc_sid, idx, sv->sv_type); |
| 634 | return OK; |
| 635 | } |
| 636 | |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 637 | if (end == NULL) |
| 638 | imp = NULL; |
| 639 | else if (import == NULL) |
| 640 | imp = find_imported(name, 0, FALSE); |
| 641 | else |
| 642 | imp = import; |
| 643 | |
| 644 | if (imp != NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 645 | { |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 646 | char_u *p = skipwhite(*end); |
| 647 | char_u *exp_name; |
| 648 | int cc; |
Bram Moolenaar | ffe6e64 | 2022-04-01 13:23:47 +0100 | [diff] [blame] | 649 | ufunc_T *ufunc = NULL; |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 650 | type_T *type; |
Bram Moolenaar | d041f42 | 2022-01-12 19:54:00 +0000 | [diff] [blame] | 651 | int done = FALSE; |
| 652 | int res = OK; |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 653 | |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 654 | check_script_symlink(imp->imp_sid); |
| 655 | import_check_sourced_sid(&imp->imp_sid); |
Ernie Rael | 9a90179 | 2024-04-16 22:11:56 +0200 | [diff] [blame] | 656 | |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 657 | // Need to lookup the member. |
| 658 | if (*p != '.') |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 659 | { |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 660 | semsg(_(e_expected_dot_after_name_str), start); |
| 661 | return FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 662 | } |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 663 | ++p; |
| 664 | if (VIM_ISWHITE(*p)) |
| 665 | { |
| 666 | emsg(_(e_no_white_space_allowed_after_dot)); |
| 667 | return FAIL; |
| 668 | } |
| 669 | |
| 670 | // isolate one name |
| 671 | exp_name = p; |
| 672 | while (eval_isnamec(*p)) |
| 673 | ++p; |
| 674 | cc = *p; |
| 675 | *p = NUL; |
| 676 | |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 677 | si = SCRIPT_ITEM(imp->imp_sid); |
Bram Moolenaar | ccbfd48 | 2022-03-31 16:18:23 +0100 | [diff] [blame] | 678 | if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED) |
| 679 | // "import autoload './dir/script.vim'" or |
| 680 | // "import autoload './autoload/script.vim'" - load script first |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 681 | res = generate_SOURCE(cctx, imp->imp_sid); |
Bram Moolenaar | ccbfd48 | 2022-03-31 16:18:23 +0100 | [diff] [blame] | 682 | |
| 683 | if (res == OK) |
| 684 | { |
| 685 | if (si->sn_autoload_prefix != NULL |
| 686 | && si->sn_state == SN_STATE_NOT_LOADED) |
| 687 | { |
| 688 | char_u *auto_name = |
| 689 | concat_str(si->sn_autoload_prefix, exp_name); |
| 690 | |
| 691 | // autoload script must be loaded later, access by the autoload |
| 692 | // name. If a '(' follows it must be a function. Otherwise we |
| 693 | // don't know, it can be "script.Func". |
| 694 | if (cc == '(' || paren_follows_after_expr) |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 695 | res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE); |
Bram Moolenaar | ccbfd48 | 2022-03-31 16:18:23 +0100 | [diff] [blame] | 696 | else |
| 697 | res = generate_AUTOLOAD(cctx, auto_name, &t_any); |
| 698 | vim_free(auto_name); |
| 699 | done = TRUE; |
| 700 | } |
| 701 | else if (si->sn_import_autoload |
| 702 | && si->sn_state == SN_STATE_NOT_LOADED) |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 703 | { |
| 704 | // If a '(' follows it must be a function. Otherwise we don't |
| 705 | // know, it can be "script.Func". |
| 706 | if (cc == '(' || paren_follows_after_expr) |
| 707 | { |
| 708 | char_u sid_name[MAX_FUNC_NAME_LEN]; |
| 709 | |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 710 | func_name_with_sid(exp_name, imp->imp_sid, sid_name); |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 711 | res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE); |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 712 | } |
| 713 | else |
| 714 | res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name, |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 715 | imp->imp_sid, &t_any); |
Bram Moolenaar | ccbfd48 | 2022-03-31 16:18:23 +0100 | [diff] [blame] | 716 | done = TRUE; |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 717 | } |
Bram Moolenaar | ccbfd48 | 2022-03-31 16:18:23 +0100 | [diff] [blame] | 718 | else |
| 719 | { |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 720 | idx = find_exported(imp->imp_sid, exp_name, &ufunc, &type, |
Bram Moolenaar | ccbfd48 | 2022-03-31 16:18:23 +0100 | [diff] [blame] | 721 | cctx, NULL, TRUE); |
| 722 | } |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 723 | } |
Bram Moolenaar | ccbfd48 | 2022-03-31 16:18:23 +0100 | [diff] [blame] | 724 | |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 725 | *p = cc; |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 726 | *end = p; |
Bram Moolenaar | d041f42 | 2022-01-12 19:54:00 +0000 | [diff] [blame] | 727 | if (done) |
| 728 | return res; |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 729 | |
| 730 | if (idx < 0) |
| 731 | { |
| 732 | if (ufunc != NULL) |
| 733 | { |
| 734 | // function call or function reference |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 735 | generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE); |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 736 | return OK; |
| 737 | } |
| 738 | return FAIL; |
| 739 | } |
| 740 | |
| 741 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 742 | imp->imp_sid, |
Bram Moolenaar | d5f400c | 2022-01-06 21:10:28 +0000 | [diff] [blame] | 743 | idx, |
| 744 | type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 745 | return OK; |
| 746 | } |
| 747 | |
Bram Moolenaar | d0132f4 | 2022-05-12 11:05:40 +0100 | [diff] [blame] | 748 | // Can only get here if we know "name" is a script variable and not in a |
| 749 | // Vim9 script (variable is not in sn_var_vals): old style script. |
| 750 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 751 | &t_any); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | static int |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 755 | generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 756 | { |
Bram Moolenaar | d9d2fd0 | 2022-01-13 21:15:21 +0000 | [diff] [blame] | 757 | ufunc_T *ufunc = find_func(name, FALSE); |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 758 | compiletype_T compile_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 759 | |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 760 | // Reject a global non-autoload function found without the "g:" prefix. |
| 761 | if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc))) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 762 | return FAIL; |
| 763 | |
| 764 | // Need to compile any default values to get the argument types. |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 765 | compile_type = get_compile_type(ufunc); |
| 766 | if (func_needs_compiling(ufunc, compile_type) |
Bram Moolenaar | 21dc8f1 | 2022-03-16 17:54:17 +0000 | [diff] [blame] | 767 | && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 768 | return FAIL; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 769 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | /* |
Yegappan Lakshmanan | b5f463c | 2025-02-16 16:25:24 +0100 | [diff] [blame] | 773 | * Returns TRUE if compiling a class method. |
| 774 | */ |
| 775 | static int |
| 776 | compiling_a_class_method(cctx_T *cctx) |
| 777 | { |
| 778 | // For an object method, the FC_OBJECT flag will be set. |
| 779 | // For a constructor method, the FC_NEW flag will be set. |
| 780 | // Excluding these methods, the others are class methods. |
| 781 | // When compiling a closure function inside an object method, |
| 782 | // cctx->ctx_outer->ctx_func will point to the object method. |
| 783 | return cctx->ctx_ufunc != NULL |
| 784 | && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0 |
| 785 | && (cctx->ctx_outer == NULL |
| 786 | || cctx->ctx_outer->ctx_ufunc == NULL |
| 787 | || cctx->ctx_outer->ctx_ufunc->uf_class == NULL |
| 788 | || (cctx->ctx_outer->ctx_ufunc->uf_flags |
| 789 | & (FC_OBJECT|FC_NEW)) == 0); |
| 790 | } |
| 791 | |
| 792 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 793 | * Compile a variable name into a load instruction. |
| 794 | * "end" points to just after the name. |
| 795 | * "is_expr" is TRUE when evaluating an expression, might be a funcref. |
| 796 | * When "error" is FALSE do not give an error when not found. |
| 797 | */ |
| 798 | int |
| 799 | compile_load( |
| 800 | char_u **arg, |
| 801 | char_u *end_arg, |
| 802 | cctx_T *cctx, |
| 803 | int is_expr, |
| 804 | int error) |
| 805 | { |
| 806 | type_T *type; |
| 807 | char_u *name = NULL; |
| 808 | char_u *end = end_arg; |
| 809 | int res = FAIL; |
| 810 | int prev_called_emsg = called_emsg; |
| 811 | |
| 812 | if (*(*arg + 1) == ':') |
| 813 | { |
| 814 | if (end <= *arg + 2) |
| 815 | { |
| 816 | isntype_T isn_type; |
| 817 | |
| 818 | // load dictionary of namespace |
| 819 | switch (**arg) |
| 820 | { |
| 821 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 822 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 823 | case 't': isn_type = ISN_LOADTDICT; break; |
| 824 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 825 | default: |
| 826 | semsg(_(e_namespace_not_supported_str), *arg); |
| 827 | goto theend; |
| 828 | } |
| 829 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 830 | goto theend; |
| 831 | res = OK; |
| 832 | } |
| 833 | else |
| 834 | { |
| 835 | isntype_T isn_type = ISN_DROP; |
| 836 | |
| 837 | // load namespaced variable |
| 838 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 839 | if (name == NULL) |
| 840 | return FAIL; |
| 841 | |
| 842 | switch (**arg) |
| 843 | { |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 844 | case 'v': res = generate_LOADV(cctx, name); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 845 | break; |
Bram Moolenaar | afa048f | 2022-02-22 20:43:36 +0000 | [diff] [blame] | 846 | case 's': if (current_script_is_vim9()) |
| 847 | { |
| 848 | semsg(_(e_cannot_use_s_colon_in_vim9_script_str), |
| 849 | *arg); |
| 850 | vim_free(name); |
| 851 | return FAIL; |
| 852 | } |
Kota Kato | 948a389 | 2022-08-16 16:09:59 +0100 | [diff] [blame] | 853 | if (is_expr && find_func(name, FALSE) != NULL) |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 854 | res = generate_funcref(cctx, name, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 855 | else |
| 856 | res = compile_load_scriptvar(cctx, name, |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 857 | NULL, &end, NULL); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 858 | break; |
| 859 | case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL) |
| 860 | { |
| 861 | if (is_expr && ASCII_ISUPPER(*name) |
Bram Moolenaar | d9d2fd0 | 2022-01-13 21:15:21 +0000 | [diff] [blame] | 862 | && find_func(name, FALSE) != NULL) |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 863 | res = generate_funcref(cctx, name, TRUE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 864 | else |
| 865 | isn_type = ISN_LOADG; |
| 866 | } |
| 867 | else |
| 868 | { |
| 869 | isn_type = ISN_LOADAUTO; |
| 870 | vim_free(name); |
| 871 | name = vim_strnsave(*arg, end - *arg); |
| 872 | if (name == NULL) |
| 873 | return FAIL; |
| 874 | } |
| 875 | break; |
| 876 | case 'w': isn_type = ISN_LOADW; break; |
| 877 | case 't': isn_type = ISN_LOADT; break; |
| 878 | case 'b': isn_type = ISN_LOADB; break; |
| 879 | default: // cannot happen, just in case |
| 880 | semsg(_(e_namespace_not_supported_str), *arg); |
| 881 | goto theend; |
| 882 | } |
| 883 | if (isn_type != ISN_DROP) |
| 884 | { |
| 885 | // Global, Buffer-local, Window-local and Tabpage-local |
| 886 | // variables can be defined later, thus we don't check if it |
| 887 | // exists, give an error at runtime. |
Bram Moolenaar | fa46ead | 2021-12-22 13:18:39 +0000 | [diff] [blame] | 888 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | } |
| 892 | else |
| 893 | { |
| 894 | size_t len = end - *arg; |
| 895 | int idx; |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 896 | int method_idx; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 897 | int gen_load = FALSE; |
| 898 | int gen_load_outer = 0; |
Bram Moolenaar | c9e4a6f | 2022-09-19 16:08:04 +0100 | [diff] [blame] | 899 | int outer_loop_depth = -1; |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 900 | int outer_loop_idx = -1; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 901 | |
Hirohito Higashi | f7cb9f9 | 2025-02-04 16:37:19 +0100 | [diff] [blame] | 902 | name = vim_strnsave(*arg, len); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 903 | if (name == NULL) |
| 904 | return FAIL; |
| 905 | |
Yegappan Lakshmanan | b5f463c | 2025-02-16 16:25:24 +0100 | [diff] [blame] | 906 | if (STRCMP(name, "super") == 0 && compiling_a_class_method(cctx)) |
Bram Moolenaar | 58b4009 | 2023-01-11 15:59:05 +0000 | [diff] [blame] | 907 | { |
| 908 | // super.SomeFunc() in a class function: push &t_super type, this |
| 909 | // is recognized in compile_subscript(). |
| 910 | res = push_type_stack(cctx, &t_super); |
| 911 | if (*end != '.') |
| 912 | emsg(_(e_super_must_be_followed_by_dot)); |
| 913 | } |
| 914 | else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 915 | { |
| 916 | script_autoload(name, FALSE); |
| 917 | res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any); |
| 918 | } |
| 919 | else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) |
| 920 | == OK) |
| 921 | { |
| 922 | if (gen_load_outer == 0) |
| 923 | gen_load = TRUE; |
| 924 | } |
| 925 | else |
| 926 | { |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 927 | lvar_T lvar; |
| 928 | class_T *cl = NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 929 | |
| 930 | if (lookup_local(*arg, len, &lvar, cctx) == OK) |
| 931 | { |
| 932 | type = lvar.lv_type; |
| 933 | idx = lvar.lv_idx; |
| 934 | if (lvar.lv_from_outer != 0) |
Bram Moolenaar | f8addf1 | 2022-09-23 12:44:25 +0100 | [diff] [blame] | 935 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 936 | gen_load_outer = lvar.lv_from_outer; |
Bram Moolenaar | f8addf1 | 2022-09-23 12:44:25 +0100 | [diff] [blame] | 937 | outer_loop_depth = lvar.lv_loop_depth; |
| 938 | outer_loop_idx = lvar.lv_loop_idx; |
| 939 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 940 | else |
| 941 | gen_load = TRUE; |
| 942 | } |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 943 | else if (cctx->ctx_ufunc->uf_defclass != NULL && |
| 944 | (((idx = |
| 945 | cctx_class_member_idx(cctx, *arg, len, &cl)) >= 0) |
| 946 | || ((method_idx = |
| 947 | cctx_class_method_idx(cctx, *arg, len, &cl)) >= 0))) |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 948 | { |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 949 | // Referencing a class variable or method without the class |
| 950 | // name. A class variable or method can be referenced without |
| 951 | // the class name only in the class where the function is |
| 952 | // defined. |
Yegappan Lakshmanan | c30a90d | 2023-09-15 20:14:55 +0200 | [diff] [blame] | 953 | if (cctx->ctx_ufunc->uf_defclass == cl) |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 954 | { |
| 955 | if (idx >= 0) |
| 956 | res = generate_CLASSMEMBER(cctx, TRUE, cl, idx); |
| 957 | else |
| 958 | { |
| 959 | ufunc_T *fp = cl->class_class_functions[method_idx]; |
| 960 | res = generate_FUNCREF(cctx, fp, cl, FALSE, method_idx, |
| 961 | NULL); |
| 962 | } |
| 963 | } |
Yegappan Lakshmanan | c30a90d | 2023-09-15 20:14:55 +0200 | [diff] [blame] | 964 | else |
| 965 | { |
RestorerZ | 7fe8f43 | 2023-09-24 23:21:24 +0200 | [diff] [blame] | 966 | semsg(_(e_class_variable_str_accessible_only_inside_class_str), |
Yegappan Lakshmanan | c30a90d | 2023-09-15 20:14:55 +0200 | [diff] [blame] | 967 | name, cl->class_name); |
| 968 | res = FAIL; |
| 969 | } |
Bram Moolenaar | 6bafdd4 | 2023-01-01 12:58:33 +0000 | [diff] [blame] | 970 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 971 | else |
| 972 | { |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 973 | imported_T *imp = NULL; |
| 974 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 975 | // "var" can be script-local even without using "s:" if it |
| 976 | // already exists in a Vim9 script or when it's imported. |
Bram Moolenaar | dce2441 | 2022-02-08 20:35:30 +0000 | [diff] [blame] | 977 | if (script_var_exists(*arg, len, cctx, NULL) == OK |
Hirohito Higashi | 6c01283 | 2025-02-25 20:29:50 +0100 | [diff] [blame] | 978 | || (imp = find_imported(name, 0, FALSE)) != NULL) |
| 979 | res = compile_load_scriptvar(cctx, name, *arg, &end, imp); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 980 | |
| 981 | // When evaluating an expression and the name starts with an |
| 982 | // uppercase letter it can be a user defined function. |
| 983 | // generate_funcref() will fail if the function can't be found. |
| 984 | if (res == FAIL && is_expr && ASCII_ISUPPER(*name)) |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 985 | res = generate_funcref(cctx, name, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | if (gen_load) |
| 989 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
| 990 | if (gen_load_outer > 0) |
| 991 | { |
Bram Moolenaar | c9e4a6f | 2022-09-19 16:08:04 +0100 | [diff] [blame] | 992 | res = generate_LOADOUTER(cctx, idx, gen_load_outer, |
| 993 | outer_loop_depth, outer_loop_idx, type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 994 | cctx->ctx_outer_used = TRUE; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | *arg = end; |
| 999 | |
| 1000 | theend: |
| 1001 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
| 1002 | semsg(_(e_variable_not_found_str), name); |
| 1003 | vim_free(name); |
| 1004 | return res; |
| 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR. |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 1009 | * "str_offset" is the number of leading bytes to skip from the string. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1010 | * Returns FAIL if compilation fails. |
| 1011 | */ |
| 1012 | static int |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 1013 | compile_string(isn_T *isn, cctx_T *cctx, int str_offset) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1014 | { |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 1015 | char_u *s = isn->isn_arg.string + str_offset; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1016 | garray_T save_ga = cctx->ctx_instr; |
| 1017 | int expr_res; |
| 1018 | int trailing_error; |
| 1019 | int instr_count; |
| 1020 | isn_T *instr = NULL; |
| 1021 | |
| 1022 | // Remove the string type from the stack. |
| 1023 | --cctx->ctx_type_stack.ga_len; |
| 1024 | |
| 1025 | // Temporarily reset the list of instructions so that the jump labels are |
| 1026 | // correct. |
| 1027 | cctx->ctx_instr.ga_len = 0; |
| 1028 | cctx->ctx_instr.ga_maxlen = 0; |
| 1029 | cctx->ctx_instr.ga_data = NULL; |
h-east | 01c5f2a | 2023-01-09 15:10:40 +0000 | [diff] [blame] | 1030 | |
| 1031 | // avoid peeking a next line |
| 1032 | int galen_save = cctx->ctx_ufunc->uf_lines.ga_len; |
| 1033 | cctx->ctx_ufunc->uf_lines.ga_len = 0; |
| 1034 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1035 | expr_res = compile_expr0(&s, cctx); |
h-east | 01c5f2a | 2023-01-09 15:10:40 +0000 | [diff] [blame] | 1036 | |
| 1037 | cctx->ctx_ufunc->uf_lines.ga_len = galen_save; |
| 1038 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1039 | s = skipwhite(s); |
| 1040 | trailing_error = *s != NUL; |
| 1041 | |
| 1042 | if (expr_res == FAIL || trailing_error |
| 1043 | || GA_GROW_FAILS(&cctx->ctx_instr, 1)) |
| 1044 | { |
| 1045 | if (trailing_error) |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 1046 | semsg(_(e_trailing_characters_str), s); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1047 | clear_instr_ga(&cctx->ctx_instr); |
| 1048 | cctx->ctx_instr = save_ga; |
| 1049 | ++cctx->ctx_type_stack.ga_len; |
| 1050 | return FAIL; |
| 1051 | } |
| 1052 | |
| 1053 | // Move the generated instructions into the ISN_INSTR instruction, then |
| 1054 | // restore the list of instructions. |
| 1055 | instr_count = cctx->ctx_instr.ga_len; |
| 1056 | instr = cctx->ctx_instr.ga_data; |
| 1057 | instr[instr_count].isn_type = ISN_FINISH; |
| 1058 | |
| 1059 | cctx->ctx_instr = save_ga; |
| 1060 | vim_free(isn->isn_arg.string); |
| 1061 | isn->isn_type = ISN_INSTR; |
| 1062 | isn->isn_arg.instr = instr; |
| 1063 | return OK; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * Compile the argument expressions. |
| 1068 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 1069 | */ |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1070 | int |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 1071 | compile_arguments( |
| 1072 | char_u **arg, |
| 1073 | cctx_T *cctx, |
| 1074 | int *argcount, |
| 1075 | ca_special_T special_fn) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1076 | { |
| 1077 | char_u *p = *arg; |
| 1078 | char_u *whitep = *arg; |
| 1079 | int must_end = FALSE; |
| 1080 | int instr_count; |
| 1081 | |
| 1082 | for (;;) |
| 1083 | { |
| 1084 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 1085 | goto failret; |
| 1086 | if (*p == ')') |
| 1087 | { |
| 1088 | *arg = p + 1; |
| 1089 | return OK; |
| 1090 | } |
| 1091 | if (must_end) |
| 1092 | { |
| 1093 | semsg(_(e_missing_comma_before_argument_str), p); |
| 1094 | return FAIL; |
| 1095 | } |
| 1096 | |
| 1097 | instr_count = cctx->ctx_instr.ga_len; |
| 1098 | if (compile_expr0(&p, cctx) == FAIL) |
| 1099 | return FAIL; |
| 1100 | ++*argcount; |
| 1101 | |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 1102 | if (special_fn == CA_SEARCHPAIR && *argcount == 5 |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1103 | && cctx->ctx_instr.ga_len == instr_count + 1) |
| 1104 | { |
| 1105 | isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count; |
| 1106 | |
| 1107 | // {skip} argument of searchpair() can be compiled if not empty |
| 1108 | if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL) |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 1109 | compile_string(isn, cctx, 0); |
| 1110 | } |
| 1111 | else if (special_fn == CA_SUBSTITUTE && *argcount == 3 |
| 1112 | && cctx->ctx_instr.ga_len == instr_count + 1) |
| 1113 | { |
| 1114 | isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count; |
| 1115 | |
| 1116 | // {sub} argument of substitute() can be compiled if it starts |
| 1117 | // with \= |
| 1118 | if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\' |
Bram Moolenaar | 4913d42 | 2022-10-17 13:13:32 +0100 | [diff] [blame] | 1119 | && isn->isn_arg.string[1] == '=') |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 1120 | compile_string(isn, cctx, 2); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | if (*p != ',' && *skipwhite(p) == ',') |
| 1124 | { |
| 1125 | semsg(_(e_no_white_space_allowed_before_str_str), ",", p); |
| 1126 | p = skipwhite(p); |
| 1127 | } |
| 1128 | if (*p == ',') |
| 1129 | { |
| 1130 | ++p; |
| 1131 | if (*p != NUL && !VIM_ISWHITE(*p)) |
| 1132 | semsg(_(e_white_space_required_after_str_str), ",", p - 1); |
| 1133 | } |
| 1134 | else |
| 1135 | must_end = TRUE; |
| 1136 | whitep = p; |
| 1137 | p = skipwhite(p); |
| 1138 | } |
| 1139 | failret: |
| 1140 | emsg(_(e_missing_closing_paren)); |
| 1141 | return FAIL; |
| 1142 | } |
| 1143 | |
| 1144 | /* |
Yegappan Lakshmanan | d3eae7b | 2024-03-03 16:26:58 +0100 | [diff] [blame] | 1145 | * Compile a builtin method call of an object (e.g. string(), len(), empty(), |
| 1146 | * etc.) if the class implements it. |
| 1147 | */ |
| 1148 | static int |
| 1149 | compile_builtin_method_call(cctx_T *cctx, class_builtin_T builtin_method) |
| 1150 | { |
| 1151 | type_T *type = get_decl_type_on_stack(cctx, 0); |
| 1152 | int res = FAIL; |
| 1153 | |
| 1154 | // If the built in function is invoked on an object and the class |
| 1155 | // implements the corresponding built in method, then invoke the object |
| 1156 | // method. |
| 1157 | if (type->tt_type == VAR_OBJECT) |
| 1158 | { |
| 1159 | int method_idx; |
| 1160 | ufunc_T *uf = class_get_builtin_method(type->tt_class, builtin_method, |
| 1161 | &method_idx); |
| 1162 | if (uf != NULL) |
Ernie Rael | 58c9579 | 2024-08-13 23:27:22 +0200 | [diff] [blame] | 1163 | res = generate_CALL(cctx, uf, type->tt_class, method_idx, 0, FALSE); |
Yegappan Lakshmanan | d3eae7b | 2024-03-03 16:26:58 +0100 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | return res; |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1171 | * Compile a function call: name(arg1, arg2) |
| 1172 | * "arg" points to "name", "arg + varlen" to the "(". |
| 1173 | * "argcount_init" is 1 for "value->method()" |
| 1174 | * Instructions: |
| 1175 | * EVAL arg1 |
| 1176 | * EVAL arg2 |
| 1177 | * BCALL / DCALL / UCALL |
| 1178 | */ |
| 1179 | static int |
| 1180 | compile_call( |
| 1181 | char_u **arg, |
| 1182 | size_t varlen, |
| 1183 | cctx_T *cctx, |
| 1184 | ppconst_T *ppconst, |
| 1185 | int argcount_init) |
| 1186 | { |
| 1187 | char_u *name = *arg; |
| 1188 | char_u *p; |
| 1189 | int argcount = argcount_init; |
Bram Moolenaar | a6c18d3 | 2022-03-31 20:02:56 +0100 | [diff] [blame] | 1190 | char_u namebuf[MAX_FUNC_NAME_LEN]; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1191 | char_u fname_buf[FLEN_FIXED + 1]; |
| 1192 | char_u *tofree = NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1193 | ufunc_T *ufunc = NULL; |
| 1194 | int res = FAIL; |
| 1195 | int is_autoload; |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1196 | int has_g_namespace; |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 1197 | ca_special_T special_fn; |
Bram Moolenaar | f67c717 | 2022-01-19 17:23:05 +0000 | [diff] [blame] | 1198 | imported_T *import; |
| 1199 | |
| 1200 | if (varlen >= sizeof(namebuf)) |
| 1201 | { |
| 1202 | semsg(_(e_name_too_long_str), name); |
| 1203 | return FAIL; |
| 1204 | } |
| 1205 | vim_strncpy(namebuf, *arg, varlen); |
| 1206 | |
Bram Moolenaar | 4b1d963 | 2022-02-13 21:51:08 +0000 | [diff] [blame] | 1207 | import = find_imported(name, varlen, FALSE); |
Bram Moolenaar | f67c717 | 2022-01-19 17:23:05 +0000 | [diff] [blame] | 1208 | if (import != NULL) |
| 1209 | { |
| 1210 | semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf); |
| 1211 | return FAIL; |
| 1212 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1213 | |
| 1214 | // We can evaluate "has('name')" at compile time. |
LemonBoy | 58f331a | 2022-04-02 21:59:06 +0100 | [diff] [blame] | 1215 | // We can evaluate "len('string')" at compile time. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1216 | // We always evaluate "exists_compiled()" at compile time. |
LemonBoy | 58f331a | 2022-04-02 21:59:06 +0100 | [diff] [blame] | 1217 | if ((varlen == 3 |
| 1218 | && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0)) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1219 | || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0)) |
| 1220 | { |
| 1221 | char_u *s = skipwhite(*arg + varlen + 1); |
| 1222 | typval_T argvars[2]; |
| 1223 | int is_has = **arg == 'h'; |
LemonBoy | 58f331a | 2022-04-02 21:59:06 +0100 | [diff] [blame] | 1224 | int is_len = **arg == 'l'; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1225 | |
| 1226 | argvars[0].v_type = VAR_UNKNOWN; |
| 1227 | if (*s == '"') |
Bram Moolenaar | 0abc287 | 2022-05-10 13:24:30 +0100 | [diff] [blame] | 1228 | (void)eval_string(&s, &argvars[0], TRUE, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1229 | else if (*s == '\'') |
Bram Moolenaar | 0abc287 | 2022-05-10 13:24:30 +0100 | [diff] [blame] | 1230 | (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1231 | s = skipwhite(s); |
| 1232 | if (*s == ')' && argvars[0].v_type == VAR_STRING |
| 1233 | && ((is_has && !dynamic_feature(argvars[0].vval.v_string)) |
| 1234 | || !is_has)) |
| 1235 | { |
| 1236 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 1237 | |
| 1238 | *arg = s + 1; |
| 1239 | argvars[1].v_type = VAR_UNKNOWN; |
| 1240 | tv->v_type = VAR_NUMBER; |
| 1241 | tv->vval.v_number = 0; |
| 1242 | if (is_has) |
| 1243 | f_has(argvars, tv); |
LemonBoy | 58f331a | 2022-04-02 21:59:06 +0100 | [diff] [blame] | 1244 | else if (is_len) |
| 1245 | f_len(argvars, tv); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1246 | else |
| 1247 | f_exists(argvars, tv); |
| 1248 | clear_tv(&argvars[0]); |
| 1249 | ++ppconst->pp_used; |
| 1250 | return OK; |
| 1251 | } |
| 1252 | clear_tv(&argvars[0]); |
LemonBoy | 58f331a | 2022-04-02 21:59:06 +0100 | [diff] [blame] | 1253 | if (!is_has && !is_len) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1254 | { |
| 1255 | emsg(_(e_argument_of_exists_compiled_must_be_literal_string)); |
| 1256 | return FAIL; |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 1261 | return FAIL; |
| 1262 | |
Bram Moolenaar | 3e1ac14 | 2023-02-18 19:49:32 +0000 | [diff] [blame] | 1263 | funcerror_T error; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1264 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
| 1265 | |
| 1266 | // We handle the "skip" argument of searchpair() and searchpairpos() |
| 1267 | // differently. |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 1268 | if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0) |
| 1269 | || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0) |
| 1270 | || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0) |
| 1271 | || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0)) |
| 1272 | special_fn = CA_SEARCHPAIR; |
| 1273 | else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0) |
| 1274 | special_fn = CA_SUBSTITUTE; |
| 1275 | else |
| 1276 | special_fn = CA_NOT_SPECIAL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1277 | |
| 1278 | *arg = skipwhite(*arg + varlen + 1); |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 1279 | if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1280 | goto theend; |
| 1281 | |
| 1282 | is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL; |
| 1283 | if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload) |
| 1284 | { |
| 1285 | int idx; |
| 1286 | |
| 1287 | // builtin function |
| 1288 | idx = find_internal_func(name); |
| 1289 | if (idx >= 0) |
| 1290 | { |
| 1291 | if (STRCMP(name, "flatten") == 0) |
| 1292 | { |
| 1293 | emsg(_(e_cannot_use_flatten_in_vim9_script)); |
| 1294 | goto theend; |
| 1295 | } |
| 1296 | |
| 1297 | if (STRCMP(name, "add") == 0 && argcount == 2) |
| 1298 | { |
h-east | b895b0f | 2023-09-24 15:46:31 +0200 | [diff] [blame] | 1299 | type_T *type = get_decl_type_on_stack(cctx, 1); |
Ernie Rael | d8bf87c | 2023-12-16 14:03:33 +0100 | [diff] [blame] | 1300 | if (check_type_is_value(get_type_on_stack(cctx, 0)) == FAIL) |
| 1301 | goto theend; |
h-east | b895b0f | 2023-09-24 15:46:31 +0200 | [diff] [blame] | 1302 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1303 | // add() can be compiled to instructions if we know the type |
| 1304 | if (type->tt_type == VAR_LIST) |
| 1305 | { |
| 1306 | // inline "add(list, item)" so that the type can be checked |
| 1307 | res = generate_LISTAPPEND(cctx); |
| 1308 | idx = -1; |
| 1309 | } |
| 1310 | else if (type->tt_type == VAR_BLOB) |
| 1311 | { |
| 1312 | // inline "add(blob, nr)" so that the type can be checked |
| 1313 | res = generate_BLOBAPPEND(cctx); |
| 1314 | idx = -1; |
| 1315 | } |
| 1316 | } |
| 1317 | |
Bram Moolenaar | f5fec05 | 2022-09-11 11:49:22 +0100 | [diff] [blame] | 1318 | if ((STRCMP(name, "writefile") == 0 && argcount > 2) |
| 1319 | || (STRCMP(name, "mkdir") == 0 && argcount > 1)) |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 1320 | { |
Bram Moolenaar | f5fec05 | 2022-09-11 11:49:22 +0100 | [diff] [blame] | 1321 | // May have the "D" or "R" flag, reserve a variable for a |
| 1322 | // deferred function call. |
Bram Moolenaar | 806a273 | 2022-09-04 15:40:36 +0100 | [diff] [blame] | 1323 | if (get_defer_var_idx(cctx) == 0) |
| 1324 | idx = -1; |
| 1325 | } |
| 1326 | |
Yegappan Lakshmanan | d3eae7b | 2024-03-03 16:26:58 +0100 | [diff] [blame] | 1327 | class_builtin_T builtin_method = CLASS_BUILTIN_INVALID; |
| 1328 | if (STRCMP(name, "string") == 0) |
| 1329 | builtin_method = CLASS_BUILTIN_STRING; |
| 1330 | else if (STRCMP(name, "empty") == 0) |
| 1331 | builtin_method = CLASS_BUILTIN_EMPTY; |
| 1332 | else if (STRCMP(name, "len") == 0) |
| 1333 | builtin_method = CLASS_BUILTIN_LEN; |
| 1334 | if (builtin_method != CLASS_BUILTIN_INVALID) |
| 1335 | { |
| 1336 | res = compile_builtin_method_call(cctx, builtin_method); |
| 1337 | if (res == OK) |
| 1338 | idx = -1; |
| 1339 | } |
| 1340 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1341 | if (idx >= 0) |
| 1342 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
| 1343 | } |
| 1344 | else |
Bram Moolenaar | a6c18d3 | 2022-03-31 20:02:56 +0100 | [diff] [blame] | 1345 | emsg_funcname(e_unknown_function_str, namebuf); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1346 | goto theend; |
| 1347 | } |
| 1348 | |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1349 | has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0; |
| 1350 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1351 | // An argument or local variable can be a function reference, this |
| 1352 | // overrules a function name. |
| 1353 | if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL |
| 1354 | && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK) |
| 1355 | { |
Yegappan Lakshmanan | 342f4f6 | 2023-09-09 11:37:23 +0200 | [diff] [blame] | 1356 | class_T *cl = NULL; |
| 1357 | int mi = 0; |
| 1358 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1359 | // If we can find the function by name generate the right call. |
| 1360 | // Skip global functions here, a local funcref takes precedence. |
Bram Moolenaar | d9d2fd0 | 2022-01-13 21:15:21 +0000 | [diff] [blame] | 1361 | ufunc = find_func(name, FALSE); |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1362 | if (ufunc != NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1363 | { |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1364 | if (!func_is_global(ufunc)) |
| 1365 | { |
Ernie Rael | 58c9579 | 2024-08-13 23:27:22 +0200 | [diff] [blame] | 1366 | res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE); |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1367 | goto theend; |
| 1368 | } |
| 1369 | if (!has_g_namespace |
| 1370 | && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL) |
| 1371 | { |
| 1372 | // A function name without g: prefix must be found locally. |
Bram Moolenaar | a6c18d3 | 2022-03-31 20:02:56 +0100 | [diff] [blame] | 1373 | emsg_funcname(e_unknown_function_str, namebuf); |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1374 | goto theend; |
| 1375 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1376 | } |
Yegappan Lakshmanan | f36bbcd | 2023-09-10 18:19:06 +0200 | [diff] [blame] | 1377 | else if ((mi = cctx_class_method_idx(cctx, name, varlen, &cl)) >= 0) |
Yegappan Lakshmanan | 342f4f6 | 2023-09-09 11:37:23 +0200 | [diff] [blame] | 1378 | { |
Yegappan Lakshmanan | c30a90d | 2023-09-15 20:14:55 +0200 | [diff] [blame] | 1379 | // Class method invocation without the class name. |
| 1380 | // A class method can be referenced without the class name only in |
| 1381 | // the class where the function is defined. |
| 1382 | if (cctx->ctx_ufunc->uf_defclass == cl) |
| 1383 | { |
Yegappan Lakshmanan | c30a90d | 2023-09-15 20:14:55 +0200 | [diff] [blame] | 1384 | res = generate_CALL(cctx, cl->class_class_functions[mi], NULL, |
Ernie Rael | 58c9579 | 2024-08-13 23:27:22 +0200 | [diff] [blame] | 1385 | 0, argcount, FALSE); |
Yegappan Lakshmanan | c30a90d | 2023-09-15 20:14:55 +0200 | [diff] [blame] | 1386 | } |
| 1387 | else |
| 1388 | { |
RestorerZ | 7fe8f43 | 2023-09-24 23:21:24 +0200 | [diff] [blame] | 1389 | semsg(_(e_class_method_str_accessible_only_inside_class_str), |
Yegappan Lakshmanan | c30a90d | 2023-09-15 20:14:55 +0200 | [diff] [blame] | 1390 | name, cl->class_name); |
| 1391 | res = FAIL; |
| 1392 | } |
Yegappan Lakshmanan | 342f4f6 | 2023-09-09 11:37:23 +0200 | [diff] [blame] | 1393 | goto theend; |
| 1394 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | // If the name is a variable, load it and use PCALL. |
| 1398 | // Not for g:Func(), we don't know if it is a variable or not. |
Bram Moolenaar | 848fadd | 2022-01-30 15:28:30 +0000 | [diff] [blame] | 1399 | // Not for some#Func(), it will be loaded later. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1400 | p = namebuf; |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1401 | if (!has_g_namespace && !is_autoload |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1402 | && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK) |
| 1403 | { |
Christian Brabandt | d5475e8 | 2023-08-17 23:34:09 +0200 | [diff] [blame] | 1404 | type_T *s_type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1405 | |
Christian Brabandt | d5475e8 | 2023-08-17 23:34:09 +0200 | [diff] [blame] | 1406 | res = generate_PCALL(cctx, argcount, namebuf, s_type, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1407 | goto theend; |
| 1408 | } |
| 1409 | |
| 1410 | // If we can find a global function by name generate the right call. |
| 1411 | if (ufunc != NULL) |
| 1412 | { |
Ernie Rael | 58c9579 | 2024-08-13 23:27:22 +0200 | [diff] [blame] | 1413 | res = generate_CALL(cctx, ufunc, NULL, 0, argcount, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1414 | goto theend; |
| 1415 | } |
| 1416 | |
| 1417 | // A global function may be defined only later. Need to figure out at |
| 1418 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 1419 | if (has_g_namespace || is_autoload) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1420 | res = generate_UCALL(cctx, name, argcount); |
| 1421 | else |
Bram Moolenaar | a6c18d3 | 2022-03-31 20:02:56 +0100 | [diff] [blame] | 1422 | emsg_funcname(e_unknown_function_str, namebuf); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1423 | |
| 1424 | theend: |
| 1425 | vim_free(tofree); |
| 1426 | return res; |
| 1427 | } |
| 1428 | |
| 1429 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 1430 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 1431 | |
| 1432 | /* |
| 1433 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 1434 | * does not recognize magic braces. |
| 1435 | * When "use_namespace" is TRUE recognize "b:", "s:", etc. |
| 1436 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 1437 | * valid name. |
| 1438 | */ |
| 1439 | char_u * |
| 1440 | to_name_end(char_u *arg, int use_namespace) |
| 1441 | { |
| 1442 | char_u *p; |
| 1443 | |
| 1444 | // Quick check for valid starting character. |
| 1445 | if (!eval_isnamec1(*arg)) |
| 1446 | return arg; |
| 1447 | |
| 1448 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 1449 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 1450 | // and can be used in slice "[n:]". |
| 1451 | if (*p == ':' && (p != arg + 1 |
| 1452 | || !use_namespace |
| 1453 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 1454 | break; |
| 1455 | return p; |
| 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * Like to_name_end() but also skip over a list or dict constant. |
| 1460 | * Also accept "<SNR>123_Func". |
| 1461 | * This intentionally does not handle line continuation. |
| 1462 | */ |
| 1463 | char_u * |
| 1464 | to_name_const_end(char_u *arg) |
| 1465 | { |
| 1466 | char_u *p = arg; |
| 1467 | typval_T rettv; |
| 1468 | |
| 1469 | if (STRNCMP(p, "<SNR>", 5) == 0) |
| 1470 | p = skipdigits(p + 5); |
| 1471 | p = to_name_end(p, TRUE); |
| 1472 | if (p == arg && *arg == '[') |
| 1473 | { |
| 1474 | |
| 1475 | // Can be "[1, 2, 3]->Func()". |
| 1476 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
| 1477 | p = arg; |
| 1478 | } |
| 1479 | return p; |
| 1480 | } |
| 1481 | |
| 1482 | /* |
| 1483 | * parse a list: [expr, expr] |
| 1484 | * "*arg" points to the '['. |
| 1485 | * ppconst->pp_is_const is set if all items are a constant. |
| 1486 | */ |
| 1487 | static int |
| 1488 | compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 1489 | { |
| 1490 | char_u *p = skipwhite(*arg + 1); |
| 1491 | char_u *whitep = *arg + 1; |
| 1492 | int count = 0; |
| 1493 | int is_const; |
| 1494 | int is_all_const = TRUE; // reset when non-const encountered |
Bram Moolenaar | 2984ed3 | 2022-08-20 14:51:17 +0100 | [diff] [blame] | 1495 | int must_end = FALSE; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1496 | |
| 1497 | for (;;) |
| 1498 | { |
| 1499 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 1500 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1501 | semsg(_(e_missing_end_of_list_rsb_str), *arg); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1502 | return FAIL; |
| 1503 | } |
| 1504 | if (*p == ',') |
| 1505 | { |
| 1506 | semsg(_(e_no_white_space_allowed_before_str_str), ",", p); |
| 1507 | return FAIL; |
| 1508 | } |
| 1509 | if (*p == ']') |
| 1510 | { |
| 1511 | ++p; |
| 1512 | break; |
| 1513 | } |
Bram Moolenaar | 2984ed3 | 2022-08-20 14:51:17 +0100 | [diff] [blame] | 1514 | if (must_end) |
| 1515 | { |
| 1516 | semsg(_(e_missing_comma_in_list_str), p); |
| 1517 | return FAIL; |
| 1518 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1519 | if (compile_expr0_ext(&p, cctx, &is_const) == FAIL) |
| 1520 | return FAIL; |
| 1521 | if (!is_const) |
| 1522 | is_all_const = FALSE; |
| 1523 | ++count; |
| 1524 | if (*p == ',') |
| 1525 | { |
| 1526 | ++p; |
| 1527 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 1528 | { |
| 1529 | semsg(_(e_white_space_required_after_str_str), ",", p - 1); |
| 1530 | return FAIL; |
| 1531 | } |
| 1532 | } |
Bram Moolenaar | 2984ed3 | 2022-08-20 14:51:17 +0100 | [diff] [blame] | 1533 | else |
| 1534 | must_end = TRUE; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1535 | whitep = p; |
| 1536 | p = skipwhite(p); |
| 1537 | } |
| 1538 | *arg = p; |
| 1539 | |
| 1540 | ppconst->pp_is_const = is_all_const; |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 1541 | return generate_NEWLIST(cctx, count, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | /* |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 1545 | * parse a tuple: (expr, expr) |
| 1546 | * "*arg" points to the ','. |
| 1547 | * ppconst->pp_is_const is set if all the items are constants. |
| 1548 | */ |
| 1549 | static int |
| 1550 | compile_tuple( |
| 1551 | char_u **arg, |
| 1552 | cctx_T *cctx, |
| 1553 | ppconst_T *ppconst, |
| 1554 | int first_item_const) |
| 1555 | { |
| 1556 | char_u *p = *arg + 1; |
| 1557 | char_u *whitep = *arg + 1; |
| 1558 | int count = 0; |
| 1559 | int is_const; |
| 1560 | int is_all_const = TRUE; // reset when non-const encountered |
| 1561 | int must_end = FALSE; |
| 1562 | |
| 1563 | if (**arg != ')') |
| 1564 | { |
| 1565 | if (*p != ')' && !IS_WHITE_OR_NUL(*p)) |
| 1566 | { |
| 1567 | semsg(_(e_white_space_required_after_str_str), ",", p - 1); |
| 1568 | return FAIL; |
| 1569 | } |
| 1570 | count = 1; // the first tuple item is already processed |
| 1571 | is_all_const = first_item_const; |
| 1572 | for (;;) |
| 1573 | { |
| 1574 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 1575 | { |
| 1576 | semsg(_(e_missing_end_of_tuple_rsp_str), *arg); |
| 1577 | return FAIL; |
| 1578 | } |
| 1579 | if (*p == ',') |
| 1580 | { |
| 1581 | semsg(_(e_no_white_space_allowed_before_str_str), ",", p); |
| 1582 | return FAIL; |
| 1583 | } |
| 1584 | if (*p == ')') |
| 1585 | { |
| 1586 | ++p; |
| 1587 | break; |
| 1588 | } |
| 1589 | if (must_end) |
| 1590 | { |
| 1591 | semsg(_(e_missing_comma_in_tuple_str), p); |
| 1592 | return FAIL; |
| 1593 | } |
| 1594 | if (compile_expr0_ext(&p, cctx, &is_const) == FAIL) |
| 1595 | return FAIL; |
| 1596 | if (!is_const) |
| 1597 | is_all_const = FALSE; |
| 1598 | ++count; |
| 1599 | if (*p == ',') |
| 1600 | { |
| 1601 | ++p; |
| 1602 | if (*p != ')' && !IS_WHITE_OR_NUL(*p)) |
| 1603 | { |
| 1604 | semsg(_(e_white_space_required_after_str_str), ",", p - 1); |
| 1605 | return FAIL; |
| 1606 | } |
| 1607 | } |
| 1608 | else |
| 1609 | must_end = TRUE; |
| 1610 | whitep = p; |
| 1611 | p = skipwhite(p); |
| 1612 | } |
| 1613 | } |
| 1614 | *arg = p; |
| 1615 | |
| 1616 | ppconst->pp_is_const = is_all_const; |
| 1617 | return generate_NEWTUPLE(cctx, count, FALSE); |
| 1618 | } |
| 1619 | |
| 1620 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1621 | * Parse a lambda: "(arg, arg) => expr" |
| 1622 | * "*arg" points to the '('. |
| 1623 | * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda. |
| 1624 | */ |
| 1625 | static int |
| 1626 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 1627 | { |
| 1628 | int r; |
| 1629 | typval_T rettv; |
| 1630 | ufunc_T *ufunc; |
| 1631 | evalarg_T evalarg; |
| 1632 | |
| 1633 | init_evalarg(&evalarg); |
| 1634 | evalarg.eval_flags = EVAL_EVALUATE; |
| 1635 | evalarg.eval_cctx = cctx; |
| 1636 | |
| 1637 | // Get the funcref in "rettv". |
| 1638 | r = get_lambda_tv(arg, &rettv, TRUE, &evalarg); |
| 1639 | if (r != OK) |
| 1640 | { |
| 1641 | clear_evalarg(&evalarg, NULL); |
| 1642 | return r; |
| 1643 | } |
| 1644 | |
| 1645 | // "rettv" will now be a partial referencing the function. |
| 1646 | ufunc = rettv.vval.v_partial->pt_func; |
| 1647 | ++ufunc->uf_refcount; |
| 1648 | clear_tv(&rettv); |
| 1649 | |
Yegappan Lakshmanan | 69e6ab6 | 2025-04-22 19:55:38 +0200 | [diff] [blame] | 1650 | if (cctx->ctx_ufunc != NULL) |
| 1651 | // This lambda might be defined in a class method. Inherit the class |
| 1652 | // from the current function. |
| 1653 | ufunc->uf_defclass = cctx->ctx_ufunc->uf_defclass; |
| 1654 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1655 | // Compile it here to get the return type. The return type is optional, |
| 1656 | // when it's missing use t_unknown. This is recognized in |
| 1657 | // compile_return(). |
| 1658 | if (ufunc->uf_ret_type->tt_type == VAR_VOID) |
| 1659 | ufunc->uf_ret_type = &t_unknown; |
| 1660 | compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx); |
| 1661 | |
| 1662 | // When the outer function is compiled for profiling or debugging, the |
| 1663 | // lambda may be called without profiling or debugging. Compile it here in |
| 1664 | // the right context. |
| 1665 | if (cctx->ctx_compile_type == CT_DEBUG |
| 1666 | #ifdef FEAT_PROFILE |
| 1667 | || cctx->ctx_compile_type == CT_PROFILE |
| 1668 | #endif |
| 1669 | ) |
| 1670 | compile_def_function(ufunc, FALSE, CT_NONE, cctx); |
| 1671 | |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 1672 | // if the outer function is not compiled for debugging or profiling, this |
| 1673 | // one might be |
| 1674 | if (cctx->ctx_compile_type == CT_NONE) |
Bram Moolenaar | 96923b7 | 2022-03-15 15:57:04 +0000 | [diff] [blame] | 1675 | { |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 1676 | compiletype_T compile_type = get_compile_type(ufunc); |
| 1677 | |
| 1678 | if (compile_type != CT_NONE) |
| 1679 | compile_def_function(ufunc, FALSE, compile_type, cctx); |
Bram Moolenaar | 96923b7 | 2022-03-15 15:57:04 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1682 | // The last entry in evalarg.eval_tofree_ga is a copy of the last line and |
| 1683 | // "*arg" may point into it. Point into the original line to avoid a |
| 1684 | // dangling pointer. |
| 1685 | if (evalarg.eval_using_cmdline) |
| 1686 | { |
| 1687 | garray_T *gap = &evalarg.eval_tofree_ga; |
| 1688 | size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1]; |
| 1689 | |
| 1690 | *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum] |
| 1691 | + off; |
Bram Moolenaar | f8addf1 | 2022-09-23 12:44:25 +0100 | [diff] [blame] | 1692 | evalarg.eval_using_cmdline = FALSE; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | clear_evalarg(&evalarg, NULL); |
| 1696 | |
| 1697 | if (ufunc->uf_def_status == UF_COMPILED) |
| 1698 | { |
| 1699 | // The return type will now be known. |
| 1700 | set_function_type(ufunc); |
| 1701 | |
| 1702 | // The function reference count will be 1. When the ISN_FUNCREF |
| 1703 | // instruction is deleted the reference count is decremented and the |
| 1704 | // function is freed. |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 1705 | return generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, NULL); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | func_ptr_unref(ufunc); |
| 1709 | return FAIL; |
| 1710 | } |
| 1711 | |
| 1712 | /* |
| 1713 | * Get a lambda and compile it. Uses Vim9 syntax. |
| 1714 | */ |
| 1715 | int |
| 1716 | get_lambda_tv_and_compile( |
| 1717 | char_u **arg, |
| 1718 | typval_T *rettv, |
| 1719 | int types_optional, |
| 1720 | evalarg_T *evalarg) |
| 1721 | { |
| 1722 | int r; |
| 1723 | ufunc_T *ufunc; |
| 1724 | int save_sc_version = current_sctx.sc_version; |
| 1725 | |
| 1726 | // Get the funcref in "rettv". |
| 1727 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 1728 | r = get_lambda_tv(arg, rettv, types_optional, evalarg); |
| 1729 | current_sctx.sc_version = save_sc_version; |
| 1730 | if (r != OK) |
Bram Moolenaar | 7f8a3b1 | 2022-05-12 22:03:01 +0100 | [diff] [blame] | 1731 | return r; // currently unreachable |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1732 | |
| 1733 | // "rettv" will now be a partial referencing the function. |
| 1734 | ufunc = rettv->vval.v_partial->pt_func; |
| 1735 | |
| 1736 | // Compile it here to get the return type. The return type is optional, |
| 1737 | // when it's missing use t_unknown. This is recognized in |
| 1738 | // compile_return(). |
| 1739 | if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID) |
| 1740 | ufunc->uf_ret_type = &t_unknown; |
| 1741 | compile_def_function(ufunc, FALSE, CT_NONE, NULL); |
| 1742 | |
| 1743 | if (ufunc->uf_def_status == UF_COMPILED) |
| 1744 | { |
| 1745 | // The return type will now be known. |
| 1746 | set_function_type(ufunc); |
| 1747 | return OK; |
| 1748 | } |
| 1749 | clear_tv(rettv); |
| 1750 | return FAIL; |
| 1751 | } |
| 1752 | |
| 1753 | /* |
| 1754 | * parse a dict: {key: val, [key]: val} |
| 1755 | * "*arg" points to the '{'. |
| 1756 | * ppconst->pp_is_const is set if all item values are a constant. |
| 1757 | */ |
| 1758 | static int |
| 1759 | compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 1760 | { |
| 1761 | garray_T *instr = &cctx->ctx_instr; |
| 1762 | int count = 0; |
| 1763 | dict_T *d = dict_alloc(); |
| 1764 | dictitem_T *item; |
| 1765 | char_u *whitep = *arg + 1; |
| 1766 | char_u *p; |
| 1767 | int is_const; |
| 1768 | int is_all_const = TRUE; // reset when non-const encountered |
| 1769 | |
| 1770 | if (d == NULL) |
| 1771 | return FAIL; |
| 1772 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Christian Brabandt | 915f3bf | 2024-04-05 20:12:19 +0200 | [diff] [blame] | 1773 | { |
| 1774 | dict_unref(d); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1775 | return FAIL; |
Christian Brabandt | 915f3bf | 2024-04-05 20:12:19 +0200 | [diff] [blame] | 1776 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1777 | for (;;) |
| 1778 | { |
| 1779 | char_u *key = NULL; |
| 1780 | |
| 1781 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1782 | { |
| 1783 | *arg = NULL; |
| 1784 | goto failret; |
| 1785 | } |
| 1786 | |
| 1787 | if (**arg == '}') |
| 1788 | break; |
| 1789 | |
| 1790 | if (**arg == '[') |
| 1791 | { |
| 1792 | isn_T *isn; |
| 1793 | |
| 1794 | // {[expr]: value} uses an evaluated key. |
| 1795 | *arg = skipwhite(*arg + 1); |
| 1796 | if (compile_expr0(arg, cctx) == FAIL) |
| 1797 | return FAIL; |
| 1798 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 1799 | if (isn->isn_type == ISN_PUSHNR) |
| 1800 | { |
| 1801 | char buf[NUMBUFLEN]; |
| 1802 | |
| 1803 | // Convert to string at compile time. |
| 1804 | vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number); |
| 1805 | isn->isn_type = ISN_PUSHS; |
| 1806 | isn->isn_arg.string = vim_strsave((char_u *)buf); |
| 1807 | } |
| 1808 | if (isn->isn_type == ISN_PUSHS) |
| 1809 | key = isn->isn_arg.string; |
Yegappan Lakshmanan | bce51d9 | 2024-04-15 19:19:52 +0200 | [diff] [blame] | 1810 | else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1811 | return FAIL; |
| 1812 | *arg = skipwhite(*arg); |
| 1813 | if (**arg != ']') |
| 1814 | { |
| 1815 | emsg(_(e_missing_matching_bracket_after_dict_key)); |
| 1816 | return FAIL; |
| 1817 | } |
| 1818 | ++*arg; |
| 1819 | } |
| 1820 | else |
| 1821 | { |
| 1822 | // {"name": value}, |
| 1823 | // {'name': value}, |
| 1824 | // {name: value} use "name" as a literal key |
| 1825 | key = get_literal_key(arg); |
| 1826 | if (key == NULL) |
| 1827 | return FAIL; |
| 1828 | if (generate_PUSHS(cctx, &key) == FAIL) |
| 1829 | return FAIL; |
| 1830 | } |
| 1831 | |
| 1832 | // Check for duplicate keys, if using string keys. |
| 1833 | if (key != NULL) |
| 1834 | { |
| 1835 | item = dict_find(d, key, -1); |
| 1836 | if (item != NULL) |
| 1837 | { |
Bram Moolenaar | a9fa8c5 | 2023-01-02 18:10:04 +0000 | [diff] [blame] | 1838 | semsg(_(e_duplicate_key_in_dictionary_str), key); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1839 | goto failret; |
| 1840 | } |
| 1841 | item = dictitem_alloc(key); |
| 1842 | if (item != NULL) |
| 1843 | { |
| 1844 | item->di_tv.v_type = VAR_UNKNOWN; |
| 1845 | item->di_tv.v_lock = 0; |
| 1846 | if (dict_add(d, item) == FAIL) |
| 1847 | dictitem_free(item); |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | if (**arg != ':') |
| 1852 | { |
| 1853 | if (*skipwhite(*arg) == ':') |
| 1854 | semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg); |
| 1855 | else |
Bram Moolenaar | a9fa8c5 | 2023-01-02 18:10:04 +0000 | [diff] [blame] | 1856 | semsg(_(e_missing_colon_in_dictionary_str), *arg); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1857 | return FAIL; |
| 1858 | } |
| 1859 | whitep = *arg + 1; |
| 1860 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 1861 | { |
| 1862 | semsg(_(e_white_space_required_after_str_str), ":", *arg); |
| 1863 | return FAIL; |
| 1864 | } |
| 1865 | |
| 1866 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1867 | { |
| 1868 | *arg = NULL; |
| 1869 | goto failret; |
| 1870 | } |
| 1871 | |
| 1872 | if (compile_expr0_ext(arg, cctx, &is_const) == FAIL) |
| 1873 | return FAIL; |
| 1874 | if (!is_const) |
| 1875 | is_all_const = FALSE; |
| 1876 | ++count; |
| 1877 | |
| 1878 | whitep = *arg; |
| 1879 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1880 | { |
| 1881 | *arg = NULL; |
| 1882 | goto failret; |
| 1883 | } |
| 1884 | if (**arg == '}') |
| 1885 | break; |
| 1886 | if (**arg != ',') |
| 1887 | { |
Bram Moolenaar | a9fa8c5 | 2023-01-02 18:10:04 +0000 | [diff] [blame] | 1888 | semsg(_(e_missing_comma_in_dictionary_str), *arg); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1889 | goto failret; |
| 1890 | } |
| 1891 | if (IS_WHITE_OR_NUL(*whitep)) |
| 1892 | { |
| 1893 | semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep); |
| 1894 | return FAIL; |
| 1895 | } |
| 1896 | whitep = *arg + 1; |
| 1897 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 1898 | { |
| 1899 | semsg(_(e_white_space_required_after_str_str), ",", *arg); |
| 1900 | return FAIL; |
| 1901 | } |
| 1902 | *arg = skipwhite(whitep); |
| 1903 | } |
| 1904 | |
| 1905 | *arg = *arg + 1; |
| 1906 | |
| 1907 | // Allow for following comment, after at least one space. |
| 1908 | p = skipwhite(*arg); |
| 1909 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
| 1910 | *arg += STRLEN(*arg); |
| 1911 | |
| 1912 | dict_unref(d); |
| 1913 | ppconst->pp_is_const = is_all_const; |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 1914 | return generate_NEWDICT(cctx, count, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1915 | |
| 1916 | failret: |
| 1917 | if (*arg == NULL) |
| 1918 | { |
Bram Moolenaar | a9fa8c5 | 2023-01-02 18:10:04 +0000 | [diff] [blame] | 1919 | semsg(_(e_missing_dict_end_str), _("[end of lines]")); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1920 | *arg = (char_u *)""; |
| 1921 | } |
| 1922 | dict_unref(d); |
| 1923 | return FAIL; |
| 1924 | } |
| 1925 | |
| 1926 | /* |
| 1927 | * Compile "&option". |
| 1928 | */ |
| 1929 | static int |
| 1930 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 1931 | { |
| 1932 | typval_T rettv; |
| 1933 | char_u *start = *arg; |
| 1934 | int ret; |
| 1935 | |
| 1936 | // parse the option and get the current value to get the type. |
| 1937 | rettv.v_type = VAR_UNKNOWN; |
| 1938 | ret = eval_option(arg, &rettv, TRUE); |
| 1939 | if (ret == OK) |
| 1940 | { |
| 1941 | // include the '&' in the name, eval_option() expects it. |
| 1942 | char_u *name = vim_strnsave(start, *arg - start); |
| 1943 | type_T *type = rettv.v_type == VAR_BOOL ? &t_bool |
| 1944 | : rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 1945 | |
| 1946 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 1947 | vim_free(name); |
| 1948 | } |
| 1949 | clear_tv(&rettv); |
| 1950 | |
| 1951 | return ret; |
| 1952 | } |
| 1953 | |
| 1954 | /* |
| 1955 | * Compile "$VAR". |
| 1956 | */ |
| 1957 | static int |
| 1958 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 1959 | { |
| 1960 | char_u *start = *arg; |
| 1961 | int len; |
| 1962 | int ret; |
| 1963 | char_u *name; |
| 1964 | |
| 1965 | ++*arg; |
| 1966 | len = get_env_len(arg); |
| 1967 | if (len == 0) |
| 1968 | { |
Bram Moolenaar | 5f25c38 | 2022-01-09 13:36:28 +0000 | [diff] [blame] | 1969 | semsg(_(e_syntax_error_at_str), start); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1970 | return FAIL; |
| 1971 | } |
| 1972 | |
| 1973 | // include the '$' in the name, eval_env_var() expects it. |
| 1974 | name = vim_strnsave(start, len + 1); |
| 1975 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 1976 | vim_free(name); |
| 1977 | return ret; |
| 1978 | } |
| 1979 | |
| 1980 | /* |
Bram Moolenaar | 0abc287 | 2022-05-10 13:24:30 +0100 | [diff] [blame] | 1981 | * Compile $"string" or $'string'. |
LemonBoy | 2eaef10 | 2022-05-06 13:14:50 +0100 | [diff] [blame] | 1982 | */ |
| 1983 | static int |
| 1984 | compile_interp_string(char_u **arg, cctx_T *cctx) |
| 1985 | { |
| 1986 | typval_T tv; |
| 1987 | int ret; |
Bram Moolenaar | 0abc287 | 2022-05-10 13:24:30 +0100 | [diff] [blame] | 1988 | int quote; |
LemonBoy | 2eaef10 | 2022-05-06 13:14:50 +0100 | [diff] [blame] | 1989 | int evaluate = cctx->ctx_skip != SKIP_YES; |
Bram Moolenaar | 0abc287 | 2022-05-10 13:24:30 +0100 | [diff] [blame] | 1990 | int count = 0; |
| 1991 | char_u *p; |
LemonBoy | 2eaef10 | 2022-05-06 13:14:50 +0100 | [diff] [blame] | 1992 | |
Bram Moolenaar | 0abc287 | 2022-05-10 13:24:30 +0100 | [diff] [blame] | 1993 | // *arg is on the '$' character, move it to the first string character. |
| 1994 | ++*arg; |
| 1995 | quote = **arg; |
| 1996 | ++*arg; |
LemonBoy | 2eaef10 | 2022-05-06 13:14:50 +0100 | [diff] [blame] | 1997 | |
Bram Moolenaar | 0abc287 | 2022-05-10 13:24:30 +0100 | [diff] [blame] | 1998 | for (;;) |
| 1999 | { |
| 2000 | // Get the string up to the matching quote or to a single '{'. |
| 2001 | // "arg" is advanced to either the quote or the '{'. |
| 2002 | if (quote == '"') |
| 2003 | ret = eval_string(arg, &tv, evaluate, TRUE); |
| 2004 | else |
| 2005 | ret = eval_lit_string(arg, &tv, evaluate, TRUE); |
| 2006 | if (ret == FAIL) |
| 2007 | break; |
| 2008 | if (evaluate) |
| 2009 | { |
| 2010 | if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL) |
| 2011 | || (**arg != '{' && count == 0)) |
| 2012 | { |
| 2013 | // generate non-empty string or empty string if it's the only |
| 2014 | // one |
| 2015 | if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL) |
| 2016 | return FAIL; |
| 2017 | tv.vval.v_string = NULL; // don't free it now |
| 2018 | ++count; |
| 2019 | } |
| 2020 | clear_tv(&tv); |
| 2021 | } |
| 2022 | |
| 2023 | if (**arg != '{') |
| 2024 | { |
| 2025 | // found terminating quote |
| 2026 | ++*arg; |
| 2027 | break; |
| 2028 | } |
| 2029 | |
| 2030 | p = compile_one_expr_in_str(*arg, cctx); |
| 2031 | if (p == NULL) |
| 2032 | { |
| 2033 | ret = FAIL; |
| 2034 | break; |
| 2035 | } |
| 2036 | ++count; |
| 2037 | *arg = p; |
| 2038 | } |
LemonBoy | 2eaef10 | 2022-05-06 13:14:50 +0100 | [diff] [blame] | 2039 | |
| 2040 | if (ret == FAIL || !evaluate) |
| 2041 | return ret; |
| 2042 | |
Bram Moolenaar | 0abc287 | 2022-05-10 13:24:30 +0100 | [diff] [blame] | 2043 | // Small optimization, if there's only a single piece skip the ISN_CONCAT. |
| 2044 | if (count > 1) |
| 2045 | return generate_CONCAT(cctx, count); |
LemonBoy | 2eaef10 | 2022-05-06 13:14:50 +0100 | [diff] [blame] | 2046 | |
Bram Moolenaar | 0abc287 | 2022-05-10 13:24:30 +0100 | [diff] [blame] | 2047 | return OK; |
LemonBoy | 2eaef10 | 2022-05-06 13:14:50 +0100 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2051 | * Compile "@r". |
| 2052 | */ |
| 2053 | static int |
| 2054 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 2055 | { |
| 2056 | int ret; |
| 2057 | |
| 2058 | ++*arg; |
| 2059 | if (**arg == NUL) |
| 2060 | { |
| 2061 | semsg(_(e_syntax_error_at_str), *arg - 1); |
| 2062 | return FAIL; |
| 2063 | } |
| 2064 | if (!valid_yank_reg(**arg, FALSE)) |
| 2065 | { |
| 2066 | emsg_invreg(**arg); |
| 2067 | return FAIL; |
| 2068 | } |
| 2069 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 2070 | ++*arg; |
| 2071 | return ret; |
| 2072 | } |
| 2073 | |
| 2074 | /* |
| 2075 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 2076 | * When "numeric_only" is TRUE do not apply '!'. |
| 2077 | */ |
| 2078 | static int |
| 2079 | apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end) |
| 2080 | { |
| 2081 | char_u *p = *end; |
| 2082 | |
| 2083 | // this works from end to start |
| 2084 | while (p > start) |
| 2085 | { |
| 2086 | --p; |
| 2087 | if (*p == '-' || *p == '+') |
| 2088 | { |
| 2089 | // only '-' has an effect, for '+' we only check the type |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2090 | if (rettv->v_type == VAR_FLOAT) |
| 2091 | { |
| 2092 | if (*p == '-') |
| 2093 | rettv->vval.v_float = -rettv->vval.v_float; |
| 2094 | } |
| 2095 | else |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2096 | { |
| 2097 | varnumber_T val; |
| 2098 | int error = FALSE; |
| 2099 | |
| 2100 | // tv_get_number_chk() accepts a string, but we don't want that |
| 2101 | // here |
| 2102 | if (check_not_string(rettv) == FAIL) |
| 2103 | return FAIL; |
| 2104 | val = tv_get_number_chk(rettv, &error); |
| 2105 | clear_tv(rettv); |
| 2106 | if (error) |
| 2107 | return FAIL; |
| 2108 | if (*p == '-') |
| 2109 | val = -val; |
| 2110 | rettv->v_type = VAR_NUMBER; |
| 2111 | rettv->vval.v_number = val; |
| 2112 | } |
| 2113 | } |
| 2114 | else if (numeric_only) |
| 2115 | { |
| 2116 | ++p; |
| 2117 | break; |
| 2118 | } |
| 2119 | else if (*p == '!') |
| 2120 | { |
| 2121 | int v = tv2bool(rettv); |
| 2122 | |
| 2123 | // '!' is permissive in the type. |
| 2124 | clear_tv(rettv); |
| 2125 | rettv->v_type = VAR_BOOL; |
| 2126 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 2127 | } |
| 2128 | } |
| 2129 | *end = p; |
| 2130 | return OK; |
| 2131 | } |
| 2132 | |
| 2133 | /* |
| 2134 | * Recognize v: variables that are constants and set "rettv". |
| 2135 | */ |
| 2136 | static void |
| 2137 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 2138 | { |
| 2139 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 2140 | { |
| 2141 | rettv->v_type = VAR_BOOL; |
| 2142 | rettv->vval.v_number = VVAL_TRUE; |
| 2143 | *arg += 6; |
| 2144 | } |
| 2145 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 2146 | { |
| 2147 | rettv->v_type = VAR_BOOL; |
| 2148 | rettv->vval.v_number = VVAL_FALSE; |
| 2149 | *arg += 7; |
| 2150 | } |
| 2151 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 2152 | { |
| 2153 | rettv->v_type = VAR_SPECIAL; |
| 2154 | rettv->vval.v_number = VVAL_NULL; |
| 2155 | *arg += 6; |
| 2156 | } |
| 2157 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 2158 | { |
| 2159 | rettv->v_type = VAR_SPECIAL; |
| 2160 | rettv->vval.v_number = VVAL_NONE; |
| 2161 | *arg += 6; |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | exprtype_T |
| 2166 | get_compare_type(char_u *p, int *len, int *type_is) |
| 2167 | { |
| 2168 | exprtype_T type = EXPR_UNKNOWN; |
| 2169 | int i; |
| 2170 | |
| 2171 | switch (p[0]) |
| 2172 | { |
| 2173 | case '=': if (p[1] == '=') |
| 2174 | type = EXPR_EQUAL; |
| 2175 | else if (p[1] == '~') |
| 2176 | type = EXPR_MATCH; |
| 2177 | break; |
| 2178 | case '!': if (p[1] == '=') |
| 2179 | type = EXPR_NEQUAL; |
| 2180 | else if (p[1] == '~') |
| 2181 | type = EXPR_NOMATCH; |
| 2182 | break; |
| 2183 | case '>': if (p[1] != '=') |
| 2184 | { |
| 2185 | type = EXPR_GREATER; |
| 2186 | *len = 1; |
| 2187 | } |
| 2188 | else |
| 2189 | type = EXPR_GEQUAL; |
| 2190 | break; |
| 2191 | case '<': if (p[1] != '=') |
| 2192 | { |
| 2193 | type = EXPR_SMALLER; |
| 2194 | *len = 1; |
| 2195 | } |
| 2196 | else |
| 2197 | type = EXPR_SEQUAL; |
| 2198 | break; |
| 2199 | case 'i': if (p[1] == 's') |
| 2200 | { |
| 2201 | // "is" and "isnot"; but not a prefix of a name |
| 2202 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 2203 | *len = 5; |
| 2204 | i = p[*len]; |
Keith Thompson | 184f71c | 2024-01-04 21:19:04 +0100 | [diff] [blame] | 2205 | if (!SAFE_isalnum(i) && i != '_') |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2206 | { |
| 2207 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 2208 | *type_is = TRUE; |
| 2209 | } |
| 2210 | } |
| 2211 | break; |
| 2212 | } |
| 2213 | return type; |
| 2214 | } |
| 2215 | |
| 2216 | /* |
| 2217 | * Skip over an expression, ignoring most errors. |
| 2218 | */ |
| 2219 | void |
| 2220 | skip_expr_cctx(char_u **arg, cctx_T *cctx) |
| 2221 | { |
| 2222 | evalarg_T evalarg; |
| 2223 | |
| 2224 | init_evalarg(&evalarg); |
| 2225 | evalarg.eval_cctx = cctx; |
| 2226 | skip_expr(arg, &evalarg); |
| 2227 | clear_evalarg(&evalarg, NULL); |
| 2228 | } |
| 2229 | |
| 2230 | /* |
| 2231 | * Check that the top of the type stack has a type that can be used as a |
| 2232 | * condition. Give an error and return FAIL if not. |
| 2233 | */ |
| 2234 | int |
| 2235 | bool_on_stack(cctx_T *cctx) |
| 2236 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2237 | type_T *type; |
| 2238 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2239 | type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2240 | if (type == &t_bool) |
| 2241 | return OK; |
| 2242 | |
Bram Moolenaar | 4913d42 | 2022-10-17 13:13:32 +0100 | [diff] [blame] | 2243 | if (type->tt_type == VAR_ANY |
| 2244 | || type->tt_type == VAR_UNKNOWN |
| 2245 | || type->tt_type == VAR_NUMBER |
| 2246 | || type == &t_number_bool |
| 2247 | || type == &t_const_number_bool) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2248 | // Number 0 and 1 are OK to use as a bool. "any" could also be a bool. |
| 2249 | // This requires a runtime type check. |
| 2250 | return generate_COND2BOOL(cctx); |
| 2251 | |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 2252 | return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | /* |
| 2256 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 2257 | */ |
| 2258 | void |
| 2259 | error_white_both(char_u *op, int len) |
| 2260 | { |
| 2261 | char_u buf[10]; |
| 2262 | |
| 2263 | vim_strncpy(buf, op, len); |
| 2264 | semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op); |
| 2265 | } |
| 2266 | |
| 2267 | /* |
| 2268 | * Compile code to apply '-', '+' and '!'. |
| 2269 | * When "numeric_only" is TRUE do not apply '!'. |
| 2270 | */ |
| 2271 | static int |
| 2272 | compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end) |
| 2273 | { |
| 2274 | char_u *p = *end; |
| 2275 | |
| 2276 | // this works from end to start |
| 2277 | while (p > start) |
| 2278 | { |
| 2279 | --p; |
| 2280 | while (VIM_ISWHITE(*p)) |
| 2281 | --p; |
| 2282 | if (*p == '-' || *p == '+') |
| 2283 | { |
Bram Moolenaar | 73ade49 | 2022-12-27 20:54:41 +0000 | [diff] [blame] | 2284 | type_T *type = get_type_on_stack(cctx, 0); |
| 2285 | if (type->tt_type != VAR_FLOAT && need_type(type, &t_number, |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 2286 | FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2287 | return FAIL; |
| 2288 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2289 | // only '-' has an effect, for '+' we only check the type |
Bram Moolenaar | 73ade49 | 2022-12-27 20:54:41 +0000 | [diff] [blame] | 2290 | if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL) |
| 2291 | return FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2292 | } |
| 2293 | else if (numeric_only) |
| 2294 | { |
| 2295 | ++p; |
| 2296 | break; |
| 2297 | } |
| 2298 | else |
| 2299 | { |
| 2300 | int invert = *p == '!'; |
| 2301 | |
| 2302 | while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1]))) |
| 2303 | { |
| 2304 | if (p[-1] == '!') |
| 2305 | invert = !invert; |
| 2306 | --p; |
| 2307 | } |
| 2308 | if (generate_2BOOL(cctx, invert, -1) == FAIL) |
| 2309 | return FAIL; |
| 2310 | } |
| 2311 | } |
| 2312 | *end = p; |
| 2313 | return OK; |
| 2314 | } |
| 2315 | |
| 2316 | /* |
| 2317 | * Compile "(expression)": recursive! |
| 2318 | * Return FAIL/OK. |
| 2319 | */ |
| 2320 | static int |
| 2321 | compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 2322 | { |
| 2323 | int ret; |
| 2324 | char_u *p = *arg + 1; |
| 2325 | |
| 2326 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 2327 | return FAIL; |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 2328 | |
| 2329 | if (**arg == ')') |
| 2330 | // empty tuple |
| 2331 | return compile_tuple(arg, cctx, ppconst, FALSE); |
| 2332 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2333 | if (ppconst->pp_used <= PPSIZE - 10) |
| 2334 | { |
| 2335 | ret = compile_expr1(arg, cctx, ppconst); |
| 2336 | } |
| 2337 | else |
| 2338 | { |
| 2339 | // Not enough space in ppconst, flush constants. |
| 2340 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2341 | return FAIL; |
| 2342 | ret = compile_expr0(arg, cctx); |
| 2343 | } |
| 2344 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 2345 | return FAIL; |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 2346 | if (ret == OK && **arg == ',') |
| 2347 | { |
| 2348 | // tuple |
| 2349 | int is_const = ppconst->pp_used > 0 || ppconst->pp_is_const; |
| 2350 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2351 | return FAIL; |
| 2352 | return compile_tuple(arg, cctx, ppconst, is_const); |
| 2353 | } |
| 2354 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2355 | if (**arg == ')') |
| 2356 | ++*arg; |
| 2357 | else if (ret == OK) |
| 2358 | { |
| 2359 | emsg(_(e_missing_closing_paren)); |
| 2360 | ret = FAIL; |
| 2361 | } |
| 2362 | return ret; |
| 2363 | } |
| 2364 | |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 2365 | static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
Bram Moolenaar | c734993 | 2022-01-16 20:59:39 +0000 | [diff] [blame] | 2366 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2367 | /* |
| 2368 | * Compile whatever comes after "name" or "name()". |
| 2369 | * Advances "*arg" only when something was recognized. |
| 2370 | */ |
| 2371 | static int |
| 2372 | compile_subscript( |
| 2373 | char_u **arg, |
| 2374 | cctx_T *cctx, |
| 2375 | char_u *start_leader, |
| 2376 | char_u **end_leader, |
| 2377 | ppconst_T *ppconst) |
| 2378 | { |
| 2379 | char_u *name_start = *end_leader; |
| 2380 | int keeping_dict = FALSE; |
| 2381 | |
| 2382 | for (;;) |
| 2383 | { |
| 2384 | char_u *p = skipwhite(*arg); |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 2385 | type_T *type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2386 | |
| 2387 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
| 2388 | { |
| 2389 | char_u *next = peek_next_line_from_context(cctx); |
| 2390 | |
Bram Moolenaar | d0fbb41 | 2022-10-19 18:04:49 +0100 | [diff] [blame] | 2391 | // If a following line starts with "->{", "->(" or "->X" advance to |
| 2392 | // that line, so that a line break before "->" is allowed. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2393 | // Also if a following line starts with ".x". |
| 2394 | if (next != NULL && |
| 2395 | ((next[0] == '-' && next[1] == '>' |
| 2396 | && (next[2] == '{' |
Bram Moolenaar | d0fbb41 | 2022-10-19 18:04:49 +0100 | [diff] [blame] | 2397 | || next[2] == '(' |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2398 | || ASCII_ISALPHA(*skipwhite(next + 2)))) |
| 2399 | || (next[0] == '.' && eval_isdictc(next[1])))) |
| 2400 | { |
| 2401 | next = next_line_from_context(cctx, TRUE); |
| 2402 | if (next == NULL) |
| 2403 | return FAIL; |
| 2404 | *arg = next; |
| 2405 | p = skipwhite(*arg); |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | // Do not skip over white space to find the "(", "execute 'x' (expr)" |
| 2410 | // is not a function call. |
| 2411 | if (**arg == '(') |
| 2412 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2413 | int argcount = 0; |
| 2414 | |
| 2415 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2416 | return FAIL; |
| 2417 | ppconst->pp_is_const = FALSE; |
| 2418 | |
| 2419 | // funcref(arg) |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2420 | type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2421 | |
| 2422 | *arg = skipwhite(p + 1); |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 2423 | if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2424 | return FAIL; |
| 2425 | if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) |
| 2426 | return FAIL; |
| 2427 | if (keeping_dict) |
| 2428 | { |
| 2429 | keeping_dict = FALSE; |
| 2430 | if (generate_instr(cctx, ISN_CLEARDICT) == NULL) |
| 2431 | return FAIL; |
| 2432 | } |
| 2433 | } |
| 2434 | else if (*p == '-' && p[1] == '>') |
| 2435 | { |
Bram Moolenaar | c734993 | 2022-01-16 20:59:39 +0000 | [diff] [blame] | 2436 | char_u *pstart = p; |
| 2437 | int alt; |
| 2438 | char_u *paren; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2439 | |
Bram Moolenaar | c734993 | 2022-01-16 20:59:39 +0000 | [diff] [blame] | 2440 | // something->method() |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2441 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2442 | return FAIL; |
| 2443 | ppconst->pp_is_const = FALSE; |
| 2444 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2445 | // Apply the '!', '-' and '+' first: |
| 2446 | // -1.0->func() works like (-1.0)->func() |
| 2447 | if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) |
| 2448 | return FAIL; |
| 2449 | |
| 2450 | p += 2; |
| 2451 | *arg = skipwhite(p); |
| 2452 | // No line break supported right after "->". |
Bram Moolenaar | c734993 | 2022-01-16 20:59:39 +0000 | [diff] [blame] | 2453 | |
| 2454 | // Three alternatives handled here: |
| 2455 | // 1. "base->name(" only a name, use compile_call() |
| 2456 | // 2. "base->(expr)(" evaluate "expr", then use PCALL |
| 2457 | // 3. "base->expr(" Same, find the end of "expr" by "(" |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2458 | if (**arg == '(') |
Bram Moolenaar | c734993 | 2022-01-16 20:59:39 +0000 | [diff] [blame] | 2459 | alt = 2; |
| 2460 | else |
| 2461 | { |
| 2462 | // alternative 1 or 3 |
| 2463 | p = *arg; |
| 2464 | if (!eval_isnamec1(*p)) |
| 2465 | { |
| 2466 | semsg(_(e_trailing_characters_str), pstart); |
| 2467 | return FAIL; |
| 2468 | } |
| 2469 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 2470 | p += 2; |
| 2471 | for ( ; eval_isnamec(*p); ++p) |
| 2472 | ; |
| 2473 | if (*p == '(') |
| 2474 | { |
| 2475 | // alternative 1 |
| 2476 | alt = 1; |
| 2477 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
| 2478 | return FAIL; |
| 2479 | } |
| 2480 | else |
| 2481 | { |
| 2482 | // Must be alternative 3, find the "(". Only works within |
| 2483 | // one line. |
| 2484 | alt = 3; |
| 2485 | paren = vim_strchr(p, '('); |
| 2486 | if (paren == NULL) |
| 2487 | { |
| 2488 | semsg(_(e_missing_parenthesis_str), *arg); |
| 2489 | return FAIL; |
| 2490 | } |
| 2491 | } |
| 2492 | } |
| 2493 | |
| 2494 | if (alt != 1) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2495 | { |
| 2496 | int argcount = 1; |
| 2497 | garray_T *stack = &cctx->ctx_type_stack; |
| 2498 | int type_idx_start = stack->ga_len; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2499 | int expr_isn_start = cctx->ctx_instr.ga_len; |
| 2500 | int expr_isn_end; |
| 2501 | int arg_isn_count; |
| 2502 | |
Bram Moolenaar | c734993 | 2022-01-16 20:59:39 +0000 | [diff] [blame] | 2503 | if (alt == 2) |
| 2504 | { |
| 2505 | // Funcref call: list->(Refs[2])(arg) |
| 2506 | // or lambda: list->((arg) => expr)(arg) |
| 2507 | // |
| 2508 | // Fist compile the function expression. |
| 2509 | if (compile_parenthesis(arg, cctx, ppconst) == FAIL) |
| 2510 | return FAIL; |
| 2511 | } |
| 2512 | else |
| 2513 | { |
Bram Moolenaar | 6389baa | 2022-01-17 20:50:40 +0000 | [diff] [blame] | 2514 | int fail; |
| 2515 | int save_len = cctx->ctx_ufunc->uf_lines.ga_len; |
Bram Moolenaar | 31ad32a | 2022-05-13 16:23:37 +0100 | [diff] [blame] | 2516 | int prev_did_emsg = did_emsg; |
Bram Moolenaar | 6389baa | 2022-01-17 20:50:40 +0000 | [diff] [blame] | 2517 | |
Bram Moolenaar | c734993 | 2022-01-16 20:59:39 +0000 | [diff] [blame] | 2518 | *paren = NUL; |
Bram Moolenaar | d02dce2 | 2022-01-18 17:43:04 +0000 | [diff] [blame] | 2519 | |
| 2520 | // instead of using LOADG for "import.Func" use PUSHFUNC |
| 2521 | ++paren_follows_after_expr; |
| 2522 | |
Bram Moolenaar | 6389baa | 2022-01-17 20:50:40 +0000 | [diff] [blame] | 2523 | // do not look in the next line |
| 2524 | cctx->ctx_ufunc->uf_lines.ga_len = 1; |
Bram Moolenaar | d02dce2 | 2022-01-18 17:43:04 +0000 | [diff] [blame] | 2525 | |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 2526 | fail = compile_expr9(arg, cctx, ppconst) == FAIL |
Bram Moolenaar | 6389baa | 2022-01-17 20:50:40 +0000 | [diff] [blame] | 2527 | || *skipwhite(*arg) != NUL; |
| 2528 | *paren = '('; |
Bram Moolenaar | d02dce2 | 2022-01-18 17:43:04 +0000 | [diff] [blame] | 2529 | --paren_follows_after_expr; |
Bram Moolenaar | 6389baa | 2022-01-17 20:50:40 +0000 | [diff] [blame] | 2530 | cctx->ctx_ufunc->uf_lines.ga_len = save_len; |
Bram Moolenaar | d02dce2 | 2022-01-18 17:43:04 +0000 | [diff] [blame] | 2531 | |
Bram Moolenaar | 6389baa | 2022-01-17 20:50:40 +0000 | [diff] [blame] | 2532 | if (fail) |
Bram Moolenaar | c734993 | 2022-01-16 20:59:39 +0000 | [diff] [blame] | 2533 | { |
Bram Moolenaar | 31ad32a | 2022-05-13 16:23:37 +0100 | [diff] [blame] | 2534 | if (did_emsg == prev_did_emsg) |
| 2535 | semsg(_(e_invalid_expression_str), pstart); |
Bram Moolenaar | c734993 | 2022-01-16 20:59:39 +0000 | [diff] [blame] | 2536 | return FAIL; |
| 2537 | } |
Bram Moolenaar | c734993 | 2022-01-16 20:59:39 +0000 | [diff] [blame] | 2538 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2539 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2540 | // Compile the arguments. |
| 2541 | if (**arg != '(') |
| 2542 | { |
| 2543 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | 3a846e6 | 2022-01-01 16:21:00 +0000 | [diff] [blame] | 2544 | emsg(_(e_no_white_space_allowed_before_parenthesis)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2545 | else |
| 2546 | semsg(_(e_missing_parenthesis_str), *arg); |
| 2547 | return FAIL; |
| 2548 | } |
Bram Moolenaar | 6389baa | 2022-01-17 20:50:40 +0000 | [diff] [blame] | 2549 | |
| 2550 | // Remember the next instruction index, where the instructions |
| 2551 | // for arguments are being written. |
| 2552 | expr_isn_end = cctx->ctx_instr.ga_len; |
| 2553 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2554 | *arg = skipwhite(*arg + 1); |
LemonBoy | f3b4895 | 2022-05-05 13:53:03 +0100 | [diff] [blame] | 2555 | if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) |
| 2556 | == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2557 | return FAIL; |
| 2558 | |
| 2559 | // Move the instructions for the arguments to before the |
| 2560 | // instructions of the expression and move the type of the |
| 2561 | // expression after the argument types. This is what ISN_PCALL |
| 2562 | // expects. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2563 | arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end; |
| 2564 | if (arg_isn_count > 0) |
| 2565 | { |
| 2566 | int expr_isn_count = expr_isn_end - expr_isn_start; |
| 2567 | isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2568 | type_T *decl_type; |
| 2569 | type2_T *typep; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2570 | |
| 2571 | if (isn == NULL) |
| 2572 | return FAIL; |
| 2573 | mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data) |
| 2574 | + expr_isn_start, |
| 2575 | sizeof(isn_T) * expr_isn_count); |
| 2576 | mch_memmove(((isn_T *)cctx->ctx_instr.ga_data) |
| 2577 | + expr_isn_start, |
| 2578 | ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end, |
| 2579 | sizeof(isn_T) * arg_isn_count); |
| 2580 | mch_memmove(((isn_T *)cctx->ctx_instr.ga_data) |
| 2581 | + expr_isn_start + arg_isn_count, |
| 2582 | isn, sizeof(isn_T) * expr_isn_count); |
| 2583 | vim_free(isn); |
| 2584 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2585 | typep = ((type2_T *)stack->ga_data) + type_idx_start; |
| 2586 | type = typep->type_curr; |
| 2587 | decl_type = typep->type_decl; |
| 2588 | mch_memmove(((type2_T *)stack->ga_data) + type_idx_start, |
| 2589 | ((type2_T *)stack->ga_data) + type_idx_start + 1, |
| 2590 | sizeof(type2_T) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2591 | * (stack->ga_len - type_idx_start - 1)); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2592 | typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1; |
| 2593 | typep->type_curr = type; |
| 2594 | typep->type_decl = decl_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2597 | type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2598 | if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL) |
| 2599 | return FAIL; |
| 2600 | } |
Bram Moolenaar | c734993 | 2022-01-16 20:59:39 +0000 | [diff] [blame] | 2601 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2602 | if (keeping_dict) |
| 2603 | { |
| 2604 | keeping_dict = FALSE; |
| 2605 | if (generate_instr(cctx, ISN_CLEARDICT) == NULL) |
| 2606 | return FAIL; |
| 2607 | } |
| 2608 | } |
| 2609 | else if (**arg == '[') |
| 2610 | { |
| 2611 | int is_slice = FALSE; |
| 2612 | |
| 2613 | // list index: list[123] |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 2614 | // tuple index: tuple[123] |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2615 | // dict member: dict[key] |
| 2616 | // string index: text[123] |
| 2617 | // blob index: blob[123] |
| 2618 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2619 | return FAIL; |
| 2620 | ppconst->pp_is_const = FALSE; |
| 2621 | |
| 2622 | ++p; |
| 2623 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 2624 | return FAIL; |
| 2625 | if (**arg == ':') |
| 2626 | { |
| 2627 | // missing first index is equal to zero |
| 2628 | generate_PUSHNR(cctx, 0); |
| 2629 | } |
| 2630 | else |
| 2631 | { |
| 2632 | if (compile_expr0(arg, cctx) == FAIL) |
| 2633 | return FAIL; |
| 2634 | if (**arg == ':') |
| 2635 | { |
| 2636 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 2637 | ":", *arg); |
| 2638 | return FAIL; |
| 2639 | } |
| 2640 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 2641 | return FAIL; |
| 2642 | *arg = skipwhite(*arg); |
| 2643 | } |
| 2644 | if (**arg == ':') |
| 2645 | { |
| 2646 | is_slice = TRUE; |
| 2647 | ++*arg; |
| 2648 | if (!IS_WHITE_OR_NUL(**arg) && **arg != ']') |
| 2649 | { |
| 2650 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 2651 | ":", *arg); |
| 2652 | return FAIL; |
| 2653 | } |
| 2654 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 2655 | return FAIL; |
| 2656 | if (**arg == ']') |
| 2657 | // missing second index is equal to end of string |
| 2658 | generate_PUSHNR(cctx, -1); |
| 2659 | else |
| 2660 | { |
| 2661 | if (compile_expr0(arg, cctx) == FAIL) |
| 2662 | return FAIL; |
| 2663 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 2664 | return FAIL; |
| 2665 | *arg = skipwhite(*arg); |
| 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | if (**arg != ']') |
| 2670 | { |
| 2671 | emsg(_(e_missing_closing_square_brace)); |
| 2672 | return FAIL; |
| 2673 | } |
| 2674 | *arg = *arg + 1; |
| 2675 | |
| 2676 | if (keeping_dict) |
| 2677 | { |
| 2678 | keeping_dict = FALSE; |
| 2679 | if (generate_instr(cctx, ISN_CLEARDICT) == NULL) |
| 2680 | return FAIL; |
| 2681 | } |
| 2682 | if (compile_member(is_slice, &keeping_dict, cctx) == FAIL) |
| 2683 | return FAIL; |
| 2684 | } |
| 2685 | else if (*p == '.' && p[1] != '.') |
| 2686 | { |
| 2687 | // dictionary member: dict.name |
| 2688 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2689 | return FAIL; |
| 2690 | ppconst->pp_is_const = FALSE; |
| 2691 | |
Yegappan Lakshmanan | 3164cf8 | 2024-03-28 10:36:42 +0100 | [diff] [blame] | 2692 | type = get_type_on_stack(cctx, 0); |
| 2693 | if (type != &t_unknown |
Bram Moolenaar | 912bfee | 2023-01-15 20:18:55 +0000 | [diff] [blame] | 2694 | && (type->tt_type == VAR_CLASS |
Yegappan Lakshmanan | de8f8f7 | 2025-04-01 20:43:36 +0200 | [diff] [blame] | 2695 | || (type->tt_type == VAR_OBJECT |
| 2696 | && type != &t_object_any))) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2697 | { |
Bram Moolenaar | 912bfee | 2023-01-15 20:18:55 +0000 | [diff] [blame] | 2698 | // class member: SomeClass.varname |
| 2699 | // class method: SomeClass.SomeMethod() |
| 2700 | // class constructor: SomeClass.new() |
| 2701 | // object member: someObject.varname, this.varname |
| 2702 | // object method: someObject.SomeMethod(), this.SomeMethod() |
| 2703 | *arg = p; |
| 2704 | if (compile_class_object_index(cctx, arg, type) == FAIL) |
| 2705 | return FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2706 | } |
Bram Moolenaar | 912bfee | 2023-01-15 20:18:55 +0000 | [diff] [blame] | 2707 | else |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2708 | { |
Bram Moolenaar | 912bfee | 2023-01-15 20:18:55 +0000 | [diff] [blame] | 2709 | *arg = p + 1; |
| 2710 | if (IS_WHITE_OR_NUL(**arg)) |
| 2711 | { |
| 2712 | emsg(_(e_missing_name_after_dot)); |
| 2713 | return FAIL; |
| 2714 | } |
| 2715 | p = *arg; |
| 2716 | if (eval_isdictc(*p)) |
| 2717 | while (eval_isnamec(*p)) |
| 2718 | MB_PTR_ADV(p); |
| 2719 | if (p == *arg) |
| 2720 | { |
| 2721 | semsg(_(e_syntax_error_at_str), *arg); |
| 2722 | return FAIL; |
| 2723 | } |
| 2724 | if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL) |
| 2725 | return FAIL; |
| 2726 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
| 2727 | return FAIL; |
| 2728 | keeping_dict = TRUE; |
| 2729 | *arg = p; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2730 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2731 | } |
| 2732 | else |
| 2733 | break; |
| 2734 | } |
| 2735 | |
| 2736 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 2737 | // This needs to be done at runtime to be able to check the type. |
Bram Moolenaar | 1ff9c44 | 2022-05-17 15:03:33 +0100 | [diff] [blame] | 2738 | if (keeping_dict && cctx->ctx_skip != SKIP_YES |
| 2739 | && generate_instr(cctx, ISN_USEDICT) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2740 | return FAIL; |
| 2741 | |
| 2742 | return OK; |
| 2743 | } |
| 2744 | |
| 2745 | /* |
| 2746 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 2747 | * "arg" is advanced until after the expression, skipping white space. |
| 2748 | * |
| 2749 | * If the value is a constant "ppconst->pp_used" will be non-zero. |
| 2750 | * Before instructions are generated, any values in "ppconst" will generated. |
| 2751 | * |
| 2752 | * This is the compiling equivalent of eval1(), eval2(), etc. |
| 2753 | */ |
| 2754 | |
| 2755 | /* |
| 2756 | * number number constant |
| 2757 | * 0zFFFFFFFF Blob constant |
| 2758 | * "string" string constant |
| 2759 | * 'string' literal string constant |
| 2760 | * &option-name option value |
| 2761 | * @r register contents |
| 2762 | * identifier variable value |
| 2763 | * function() function call |
| 2764 | * $VAR environment variable |
| 2765 | * (expression) nested expression |
| 2766 | * [expr, expr] List |
| 2767 | * {key: val, [key]: val} Dictionary |
| 2768 | * |
| 2769 | * Also handle: |
| 2770 | * ! in front logical NOT |
| 2771 | * - in front unary minus |
| 2772 | * + in front unary plus (ignored) |
| 2773 | * trailing (arg) funcref/partial call |
| 2774 | * trailing [] subscript in String or List |
| 2775 | * trailing .name entry in Dictionary |
| 2776 | * trailing ->name() method call |
| 2777 | */ |
| 2778 | static int |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 2779 | compile_expr9( |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2780 | char_u **arg, |
| 2781 | cctx_T *cctx, |
| 2782 | ppconst_T *ppconst) |
| 2783 | { |
| 2784 | char_u *start_leader, *end_leader; |
| 2785 | int ret = OK; |
| 2786 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2787 | int used_before = ppconst->pp_used; |
| 2788 | |
| 2789 | ppconst->pp_is_const = FALSE; |
| 2790 | |
| 2791 | /* |
| 2792 | * Skip '!', '-' and '+' characters. They are handled later. |
| 2793 | */ |
| 2794 | start_leader = *arg; |
| 2795 | if (eval_leader(arg, TRUE) == FAIL) |
| 2796 | return FAIL; |
| 2797 | end_leader = *arg; |
| 2798 | |
| 2799 | rettv->v_type = VAR_UNKNOWN; |
| 2800 | switch (**arg) |
| 2801 | { |
| 2802 | /* |
| 2803 | * Number constant. |
| 2804 | */ |
| 2805 | case '0': // also for blob starting with 0z |
| 2806 | case '1': |
| 2807 | case '2': |
| 2808 | case '3': |
| 2809 | case '4': |
| 2810 | case '5': |
| 2811 | case '6': |
| 2812 | case '7': |
| 2813 | case '8': |
| 2814 | case '9': |
| 2815 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
| 2816 | return FAIL; |
| 2817 | // Apply "-" and "+" just before the number now, right to |
| 2818 | // left. Matters especially when "->" follows. Stops at |
| 2819 | // '!'. |
| 2820 | if (apply_leader(rettv, TRUE, |
| 2821 | start_leader, &end_leader) == FAIL) |
| 2822 | { |
| 2823 | clear_tv(rettv); |
| 2824 | return FAIL; |
| 2825 | } |
| 2826 | break; |
| 2827 | |
| 2828 | /* |
| 2829 | * String constant: "string". |
| 2830 | */ |
Bram Moolenaar | 0abc287 | 2022-05-10 13:24:30 +0100 | [diff] [blame] | 2831 | case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2832 | return FAIL; |
| 2833 | break; |
| 2834 | |
| 2835 | /* |
| 2836 | * Literal string constant: 'str''ing'. |
| 2837 | */ |
Bram Moolenaar | 0abc287 | 2022-05-10 13:24:30 +0100 | [diff] [blame] | 2838 | case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2839 | return FAIL; |
| 2840 | break; |
| 2841 | |
| 2842 | /* |
| 2843 | * Constant Vim variable. |
| 2844 | */ |
| 2845 | case 'v': get_vim_constant(arg, rettv); |
| 2846 | ret = NOTDONE; |
| 2847 | break; |
| 2848 | |
| 2849 | /* |
| 2850 | * "true" constant |
| 2851 | */ |
| 2852 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 2853 | && !eval_isnamec((*arg)[4])) |
| 2854 | { |
| 2855 | *arg += 4; |
| 2856 | rettv->v_type = VAR_BOOL; |
| 2857 | rettv->vval.v_number = VVAL_TRUE; |
| 2858 | } |
| 2859 | else |
| 2860 | ret = NOTDONE; |
| 2861 | break; |
| 2862 | |
| 2863 | /* |
| 2864 | * "false" constant |
| 2865 | */ |
| 2866 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 2867 | && !eval_isnamec((*arg)[5])) |
| 2868 | { |
| 2869 | *arg += 5; |
| 2870 | rettv->v_type = VAR_BOOL; |
| 2871 | rettv->vval.v_number = VVAL_FALSE; |
| 2872 | } |
| 2873 | else |
| 2874 | ret = NOTDONE; |
| 2875 | break; |
| 2876 | |
| 2877 | /* |
Bram Moolenaar | 8acb9cc | 2022-03-08 13:18:55 +0000 | [diff] [blame] | 2878 | * "null" or "null_*" constant |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2879 | */ |
Bram Moolenaar | 8acb9cc | 2022-03-08 13:18:55 +0000 | [diff] [blame] | 2880 | case 'n': if (STRNCMP(*arg, "null", 4) == 0) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2881 | { |
Bram Moolenaar | 8acb9cc | 2022-03-08 13:18:55 +0000 | [diff] [blame] | 2882 | char_u *p = *arg + 4; |
| 2883 | int len; |
| 2884 | |
| 2885 | for (len = 0; eval_isnamec(p[len]); ++len) |
| 2886 | ; |
| 2887 | ret = handle_predefined(*arg, len + 4, rettv); |
| 2888 | if (ret == FAIL) |
| 2889 | ret = NOTDONE; |
| 2890 | else |
| 2891 | *arg += len + 4; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2892 | } |
| 2893 | else |
| 2894 | ret = NOTDONE; |
| 2895 | break; |
| 2896 | |
| 2897 | /* |
| 2898 | * List: [expr, expr] |
| 2899 | */ |
| 2900 | case '[': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2901 | return FAIL; |
| 2902 | ret = compile_list(arg, cctx, ppconst); |
| 2903 | break; |
| 2904 | |
| 2905 | /* |
| 2906 | * Dictionary: {'key': val, 'key': val} |
| 2907 | */ |
| 2908 | case '{': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2909 | return FAIL; |
| 2910 | ret = compile_dict(arg, cctx, ppconst); |
| 2911 | break; |
| 2912 | |
| 2913 | /* |
| 2914 | * Option value: &name |
| 2915 | */ |
| 2916 | case '&': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2917 | return FAIL; |
| 2918 | ret = compile_get_option(arg, cctx); |
| 2919 | break; |
| 2920 | |
| 2921 | /* |
| 2922 | * Environment variable: $VAR. |
LemonBoy | 2eaef10 | 2022-05-06 13:14:50 +0100 | [diff] [blame] | 2923 | * Interpolated string: $"string" or $'string'. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2924 | */ |
| 2925 | case '$': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2926 | return FAIL; |
LemonBoy | 2eaef10 | 2022-05-06 13:14:50 +0100 | [diff] [blame] | 2927 | if ((*arg)[1] == '"' || (*arg)[1] == '\'') |
| 2928 | ret = compile_interp_string(arg, cctx); |
| 2929 | else |
| 2930 | ret = compile_get_env(arg, cctx); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2931 | break; |
| 2932 | |
| 2933 | /* |
| 2934 | * Register contents: @r. |
| 2935 | */ |
| 2936 | case '@': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2937 | return FAIL; |
| 2938 | ret = compile_get_register(arg, cctx); |
| 2939 | break; |
| 2940 | /* |
| 2941 | * nested expression: (expression). |
| 2942 | * lambda: (arg, arg) => expr |
| 2943 | * funcref: (arg, arg) => { statement } |
| 2944 | */ |
| 2945 | case '(': // if compile_lambda returns NOTDONE then it must be (expr) |
| 2946 | ret = compile_lambda(arg, cctx); |
| 2947 | if (ret == NOTDONE) |
| 2948 | ret = compile_parenthesis(arg, cctx, ppconst); |
| 2949 | break; |
| 2950 | |
| 2951 | default: ret = NOTDONE; |
| 2952 | break; |
| 2953 | } |
| 2954 | if (ret == FAIL) |
| 2955 | return FAIL; |
| 2956 | |
| 2957 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
| 2958 | { |
| 2959 | if (cctx->ctx_skip == SKIP_YES) |
| 2960 | clear_tv(rettv); |
| 2961 | else |
| 2962 | // A constant expression can possibly be handled compile time, |
| 2963 | // return the value instead of generating code. |
| 2964 | ++ppconst->pp_used; |
| 2965 | } |
| 2966 | else if (ret == NOTDONE) |
| 2967 | { |
| 2968 | char_u *p; |
| 2969 | int r; |
| 2970 | |
| 2971 | if (!eval_isnamec1(**arg)) |
| 2972 | { |
| 2973 | if (!vim9_bad_comment(*arg)) |
| 2974 | { |
| 2975 | if (ends_excmd(*skipwhite(*arg))) |
| 2976 | semsg(_(e_empty_expression_str), *arg); |
| 2977 | else |
| 2978 | semsg(_(e_name_expected_str), *arg); |
| 2979 | } |
| 2980 | return FAIL; |
| 2981 | } |
| 2982 | |
| 2983 | // "name" or "name()" |
| 2984 | p = to_name_end(*arg, TRUE); |
| 2985 | if (p - *arg == (size_t)1 && **arg == '_') |
| 2986 | { |
| 2987 | emsg(_(e_cannot_use_underscore_here)); |
| 2988 | return FAIL; |
| 2989 | } |
| 2990 | |
| 2991 | if (*p == '(') |
| 2992 | { |
| 2993 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 2994 | } |
| 2995 | else |
| 2996 | { |
| 2997 | if (cctx->ctx_skip != SKIP_YES |
| 2998 | && generate_ppconst(cctx, ppconst) == FAIL) |
| 2999 | return FAIL; |
| 3000 | r = compile_load(arg, p, cctx, TRUE, TRUE); |
| 3001 | } |
| 3002 | if (r == FAIL) |
| 3003 | return FAIL; |
| 3004 | } |
| 3005 | |
| 3006 | // Handle following "[]", ".member", etc. |
| 3007 | // Then deal with prefixed '-', '+' and '!', if not done already. |
| 3008 | if (compile_subscript(arg, cctx, start_leader, &end_leader, |
| 3009 | ppconst) == FAIL) |
| 3010 | return FAIL; |
Yegappan Lakshmanan | 5d77364 | 2024-03-22 19:37:29 +0100 | [diff] [blame] | 3011 | if ((ppconst->pp_used > 0) && (cctx->ctx_skip != SKIP_YES)) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3012 | { |
| 3013 | // apply the '!', '-' and '+' before the constant |
| 3014 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3015 | if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) |
| 3016 | return FAIL; |
| 3017 | return OK; |
| 3018 | } |
| 3019 | if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) |
| 3020 | return FAIL; |
| 3021 | return OK; |
| 3022 | } |
| 3023 | |
| 3024 | /* |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3025 | * <type>expr9: runtime type check / conversion |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3026 | */ |
| 3027 | static int |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3028 | compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3029 | { |
| 3030 | type_T *want_type = NULL; |
| 3031 | |
| 3032 | // Recognize <type> |
| 3033 | if (**arg == '<' && eval_isnamec1((*arg)[1])) |
| 3034 | { |
| 3035 | ++*arg; |
| 3036 | want_type = parse_type(arg, cctx->ctx_type_list, TRUE); |
| 3037 | if (want_type == NULL) |
| 3038 | return FAIL; |
| 3039 | |
| 3040 | if (**arg != '>') |
| 3041 | { |
| 3042 | if (*skipwhite(*arg) == '>') |
| 3043 | semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg); |
| 3044 | else |
| 3045 | emsg(_(e_missing_gt)); |
| 3046 | return FAIL; |
| 3047 | } |
| 3048 | ++*arg; |
| 3049 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 3050 | return FAIL; |
| 3051 | } |
| 3052 | |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3053 | if (compile_expr9(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3054 | return FAIL; |
| 3055 | |
| 3056 | if (want_type != NULL) |
| 3057 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3058 | type_T *actual; |
| 3059 | where_T where = WHERE_INIT; |
| 3060 | |
LemonBoy | 50d4854 | 2024-07-04 17:03:17 +0200 | [diff] [blame] | 3061 | where.wt_kind = WT_CAST; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3062 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 3063 | actual = get_type_on_stack(cctx, 0); |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 3064 | if (check_type_maybe(want_type, actual, FALSE, where) != OK) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3065 | { |
LemonBoy | 50d4854 | 2024-07-04 17:03:17 +0200 | [diff] [blame] | 3066 | if (need_type_where(actual, want_type, FALSE, -1, where, cctx, FALSE, FALSE) |
| 3067 | == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3068 | return FAIL; |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | return OK; |
| 3073 | } |
| 3074 | |
| 3075 | /* |
| 3076 | * * number multiplication |
| 3077 | * / number division |
| 3078 | * % number modulo |
| 3079 | */ |
| 3080 | static int |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3081 | compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3082 | { |
| 3083 | char_u *op; |
| 3084 | char_u *next; |
| 3085 | int ppconst_used = ppconst->pp_used; |
| 3086 | |
| 3087 | // get the first expression |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3088 | if (compile_expr8(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3089 | return FAIL; |
| 3090 | |
| 3091 | /* |
| 3092 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3093 | */ |
| 3094 | for (;;) |
| 3095 | { |
| 3096 | op = may_peek_next_line(cctx, *arg, &next); |
| 3097 | if (*op != '*' && *op != '/' && *op != '%') |
| 3098 | break; |
| 3099 | if (next != NULL) |
| 3100 | { |
| 3101 | *arg = next_line_from_context(cctx, TRUE); |
| 3102 | op = skipwhite(*arg); |
| 3103 | } |
| 3104 | |
| 3105 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
| 3106 | { |
| 3107 | error_white_both(op, 1); |
| 3108 | return FAIL; |
| 3109 | } |
| 3110 | if (may_get_next_line_error(op + 1, arg, cctx) == FAIL) |
| 3111 | return FAIL; |
| 3112 | |
| 3113 | // get the second expression |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3114 | if (compile_expr8(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3115 | return FAIL; |
| 3116 | |
| 3117 | if (ppconst->pp_used == ppconst_used + 2 |
| 3118 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3119 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
| 3120 | { |
| 3121 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3122 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
| 3123 | varnumber_T res = 0; |
| 3124 | int failed = FALSE; |
| 3125 | |
| 3126 | // both are numbers: compute the result |
| 3127 | switch (*op) |
| 3128 | { |
| 3129 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
| 3130 | break; |
| 3131 | case '/': res = num_divide(tv1->vval.v_number, |
| 3132 | tv2->vval.v_number, &failed); |
| 3133 | break; |
| 3134 | case '%': res = num_modulus(tv1->vval.v_number, |
| 3135 | tv2->vval.v_number, &failed); |
| 3136 | break; |
| 3137 | } |
| 3138 | if (failed) |
| 3139 | return FAIL; |
| 3140 | tv1->vval.v_number = res; |
| 3141 | --ppconst->pp_used; |
| 3142 | } |
| 3143 | else |
| 3144 | { |
| 3145 | generate_ppconst(cctx, ppconst); |
| 3146 | generate_two_op(cctx, op); |
| 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | return OK; |
| 3151 | } |
| 3152 | |
| 3153 | /* |
| 3154 | * + number addition or list/blobl concatenation |
| 3155 | * - number subtraction |
| 3156 | * .. string concatenation |
| 3157 | */ |
| 3158 | static int |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3159 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3160 | { |
| 3161 | char_u *op; |
| 3162 | char_u *next; |
| 3163 | int oplen; |
| 3164 | int ppconst_used = ppconst->pp_used; |
| 3165 | |
| 3166 | // get the first variable |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3167 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3168 | return FAIL; |
| 3169 | |
| 3170 | /* |
| 3171 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3172 | */ |
| 3173 | for (;;) |
| 3174 | { |
| 3175 | op = may_peek_next_line(cctx, *arg, &next); |
| 3176 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
| 3177 | break; |
| 3178 | if (op[0] == op[1] && *op != '.' && next) |
| 3179 | // Finding "++" or "--" on the next line is a separate command. |
| 3180 | // But ".." is concatenation. |
| 3181 | break; |
| 3182 | oplen = (*op == '.' ? 2 : 1); |
| 3183 | if (next != NULL) |
| 3184 | { |
| 3185 | *arg = next_line_from_context(cctx, TRUE); |
| 3186 | op = skipwhite(*arg); |
| 3187 | } |
| 3188 | |
| 3189 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
| 3190 | { |
| 3191 | error_white_both(op, oplen); |
| 3192 | return FAIL; |
| 3193 | } |
| 3194 | |
| 3195 | if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL) |
| 3196 | return FAIL; |
| 3197 | |
| 3198 | // get the second expression |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3199 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3200 | return FAIL; |
| 3201 | |
| 3202 | if (ppconst->pp_used == ppconst_used + 2 |
| 3203 | && (*op == '.' |
| 3204 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 3205 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 3206 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3207 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
| 3208 | { |
| 3209 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3210 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
| 3211 | |
| 3212 | // concat/subtract/add constant numbers |
| 3213 | if (*op == '+') |
| 3214 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 3215 | else if (*op == '-') |
| 3216 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 3217 | else |
| 3218 | { |
| 3219 | // concatenate constant strings |
| 3220 | char_u *s1 = tv1->vval.v_string; |
| 3221 | char_u *s2 = tv2->vval.v_string; |
| 3222 | size_t len1 = STRLEN(s1); |
| 3223 | |
| 3224 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 3225 | if (tv1->vval.v_string == NULL) |
| 3226 | { |
| 3227 | clear_ppconst(ppconst); |
| 3228 | return FAIL; |
| 3229 | } |
| 3230 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 3231 | STRCPY(tv1->vval.v_string + len1, s2); |
| 3232 | vim_free(s1); |
| 3233 | vim_free(s2); |
| 3234 | } |
| 3235 | --ppconst->pp_used; |
| 3236 | } |
| 3237 | else |
| 3238 | { |
| 3239 | generate_ppconst(cctx, ppconst); |
| 3240 | ppconst->pp_is_const = FALSE; |
| 3241 | if (*op == '.') |
| 3242 | { |
Yegappan Lakshmanan | bce51d9 | 2024-04-15 19:19:52 +0200 | [diff] [blame] | 3243 | if (may_generate_2STRING(-2, TOSTRING_NONE, cctx) == FAIL |
| 3244 | || may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3245 | return FAIL; |
LemonBoy | 372bcce | 2022-04-25 12:43:20 +0100 | [diff] [blame] | 3246 | if (generate_CONCAT(cctx, 2) == FAIL) |
| 3247 | return FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3248 | } |
| 3249 | else |
| 3250 | generate_two_op(cctx, op); |
| 3251 | } |
| 3252 | } |
| 3253 | |
| 3254 | return OK; |
| 3255 | } |
| 3256 | |
| 3257 | /* |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3258 | * expr6a >> expr6b |
| 3259 | * expr6a << expr6b |
| 3260 | * |
| 3261 | * Produces instructions: |
| 3262 | * OPNR bitwise left or right shift |
| 3263 | */ |
| 3264 | static int |
| 3265 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3266 | { |
| 3267 | exprtype_T type = EXPR_UNKNOWN; |
| 3268 | char_u *p; |
| 3269 | char_u *next; |
| 3270 | int len = 2; |
| 3271 | int ppconst_used = ppconst->pp_used; |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3272 | isn_T *isn; |
| 3273 | |
| 3274 | // get the first variable |
| 3275 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
| 3276 | return FAIL; |
| 3277 | |
| 3278 | /* |
| 3279 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3280 | */ |
| 3281 | for (;;) |
| 3282 | { |
| 3283 | type = EXPR_UNKNOWN; |
| 3284 | |
| 3285 | p = may_peek_next_line(cctx, *arg, &next); |
| 3286 | if (p[0] == '<' && p[1] == '<') |
| 3287 | type = EXPR_LSHIFT; |
| 3288 | else if (p[0] == '>' && p[1] == '>') |
| 3289 | type = EXPR_RSHIFT; |
| 3290 | |
| 3291 | if (type == EXPR_UNKNOWN) |
| 3292 | return OK; |
| 3293 | |
| 3294 | // Handle a bitwise left or right shift operator |
| 3295 | if (ppconst->pp_used == ppconst_used + 1) |
| 3296 | { |
Bram Moolenaar | 5b52923 | 2022-05-22 21:53:26 +0100 | [diff] [blame] | 3297 | if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER) |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3298 | { |
| 3299 | // left operand should be a number |
| 3300 | emsg(_(e_bitshift_ops_must_be_number)); |
| 3301 | return FAIL; |
| 3302 | } |
| 3303 | } |
| 3304 | else |
| 3305 | { |
| 3306 | type_T *t = get_type_on_stack(cctx, 0); |
| 3307 | |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 3308 | if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL) |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3309 | { |
| 3310 | emsg(_(e_bitshift_ops_must_be_number)); |
| 3311 | return FAIL; |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | if (next != NULL) |
| 3316 | { |
| 3317 | *arg = next_line_from_context(cctx, TRUE); |
| 3318 | p = skipwhite(*arg); |
| 3319 | } |
| 3320 | |
| 3321 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
| 3322 | { |
| 3323 | error_white_both(p, len); |
| 3324 | return FAIL; |
| 3325 | } |
| 3326 | |
| 3327 | // get the second variable |
| 3328 | if (may_get_next_line_error(p + len, arg, cctx) == FAIL) |
| 3329 | return FAIL; |
| 3330 | |
| 3331 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
| 3332 | return FAIL; |
| 3333 | |
| 3334 | if (ppconst->pp_used == ppconst_used + 2) |
| 3335 | { |
Bram Moolenaar | 5b52923 | 2022-05-22 21:53:26 +0100 | [diff] [blame] | 3336 | typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 3337 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3338 | |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3339 | // Both sides are a constant, compute the result now. |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3340 | if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0) |
| 3341 | { |
| 3342 | // right operand should be a positive number |
| 3343 | if (tv2->v_type != VAR_NUMBER) |
| 3344 | emsg(_(e_bitshift_ops_must_be_number)); |
| 3345 | else |
dundargoc | c57b5bc | 2022-11-02 13:30:51 +0000 | [diff] [blame] | 3346 | emsg(_(e_bitshift_ops_must_be_positive)); |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3347 | return FAIL; |
| 3348 | } |
| 3349 | |
| 3350 | if (tv2->vval.v_number > MAX_LSHIFT_BITS) |
| 3351 | tv1->vval.v_number = 0; |
| 3352 | else if (type == EXPR_LSHIFT) |
Bram Moolenaar | 68e64d2 | 2022-05-22 22:07:52 +0100 | [diff] [blame] | 3353 | tv1->vval.v_number = |
| 3354 | (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number; |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3355 | else |
Bram Moolenaar | 338bf58 | 2022-05-22 20:16:32 +0100 | [diff] [blame] | 3356 | tv1->vval.v_number = |
| 3357 | (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number; |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3358 | clear_tv(tv2); |
| 3359 | --ppconst->pp_used; |
| 3360 | } |
| 3361 | else |
| 3362 | { |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 3363 | if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE, |
| 3364 | 0, 0, cctx, FALSE, FALSE) == FAIL) |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3365 | { |
| 3366 | emsg(_(e_bitshift_ops_must_be_number)); |
| 3367 | return FAIL; |
| 3368 | } |
| 3369 | |
| 3370 | generate_ppconst(cctx, ppconst); |
| 3371 | |
| 3372 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 3373 | if (isn == NULL) |
| 3374 | return FAIL; |
| 3375 | |
| 3376 | if (isn != NULL) |
| 3377 | isn->isn_arg.op.op_type = type; |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | return OK; |
| 3382 | } |
| 3383 | |
| 3384 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3385 | * expr5a == expr5b |
| 3386 | * expr5a =~ expr5b |
| 3387 | * expr5a != expr5b |
| 3388 | * expr5a !~ expr5b |
| 3389 | * expr5a > expr5b |
| 3390 | * expr5a >= expr5b |
| 3391 | * expr5a < expr5b |
| 3392 | * expr5a <= expr5b |
| 3393 | * expr5a is expr5b |
| 3394 | * expr5a isnot expr5b |
| 3395 | * |
| 3396 | * Produces instructions: |
| 3397 | * EVAL expr5a Push result of "expr5a" |
| 3398 | * EVAL expr5b Push result of "expr5b" |
| 3399 | * COMPARE one of the compare instructions |
| 3400 | */ |
| 3401 | static int |
| 3402 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3403 | { |
| 3404 | exprtype_T type = EXPR_UNKNOWN; |
| 3405 | char_u *p; |
| 3406 | char_u *next; |
| 3407 | int len = 2; |
| 3408 | int type_is = FALSE; |
| 3409 | int ppconst_used = ppconst->pp_used; |
| 3410 | |
| 3411 | // get the first variable |
| 3412 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
| 3413 | return FAIL; |
| 3414 | |
| 3415 | p = may_peek_next_line(cctx, *arg, &next); |
Yegappan Lakshmanan | a061f34 | 2022-05-22 19:13:49 +0100 | [diff] [blame] | 3416 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3417 | type = get_compare_type(p, &len, &type_is); |
| 3418 | |
| 3419 | /* |
| 3420 | * If there is a comparative operator, use it. |
| 3421 | */ |
| 3422 | if (type != EXPR_UNKNOWN) |
| 3423 | { |
| 3424 | int ic = FALSE; // Default: do not ignore case |
| 3425 | |
| 3426 | if (next != NULL) |
| 3427 | { |
| 3428 | *arg = next_line_from_context(cctx, TRUE); |
| 3429 | p = skipwhite(*arg); |
| 3430 | } |
| 3431 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 3432 | { |
| 3433 | semsg(_(e_invalid_expression_str), *arg); |
| 3434 | return FAIL; |
| 3435 | } |
| 3436 | // extra question mark appended: ignore case |
| 3437 | if (p[len] == '?') |
| 3438 | { |
| 3439 | ic = TRUE; |
| 3440 | ++len; |
| 3441 | } |
| 3442 | // extra '#' appended: match case (ignored) |
| 3443 | else if (p[len] == '#') |
| 3444 | ++len; |
| 3445 | // nothing appended: match case |
| 3446 | |
| 3447 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
| 3448 | { |
| 3449 | error_white_both(p, len); |
| 3450 | return FAIL; |
| 3451 | } |
| 3452 | |
| 3453 | // get the second variable |
| 3454 | if (may_get_next_line_error(p + len, arg, cctx) == FAIL) |
| 3455 | return FAIL; |
| 3456 | |
| 3457 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
| 3458 | return FAIL; |
| 3459 | |
| 3460 | if (ppconst->pp_used == ppconst_used + 2) |
| 3461 | { |
Bram Moolenaar | 5b52923 | 2022-05-22 21:53:26 +0100 | [diff] [blame] | 3462 | typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3463 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3464 | int ret; |
| 3465 | |
| 3466 | // Both sides are a constant, compute the result now. |
| 3467 | // First check for a valid combination of types, this is more |
| 3468 | // strict than typval_compare(). |
| 3469 | if (check_compare_types(type, tv1, tv2) == FAIL) |
| 3470 | ret = FAIL; |
| 3471 | else |
| 3472 | { |
| 3473 | ret = typval_compare(tv1, tv2, type, ic); |
| 3474 | tv1->v_type = VAR_BOOL; |
| 3475 | tv1->vval.v_number = tv1->vval.v_number |
| 3476 | ? VVAL_TRUE : VVAL_FALSE; |
| 3477 | clear_tv(tv2); |
| 3478 | --ppconst->pp_used; |
| 3479 | } |
| 3480 | return ret; |
| 3481 | } |
| 3482 | |
| 3483 | generate_ppconst(cctx, ppconst); |
| 3484 | return generate_COMPARE(cctx, type, ic); |
| 3485 | } |
| 3486 | |
| 3487 | return OK; |
| 3488 | } |
| 3489 | |
| 3490 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 3491 | |
| 3492 | /* |
| 3493 | * Compile || or &&. |
| 3494 | */ |
| 3495 | static int |
| 3496 | compile_and_or( |
| 3497 | char_u **arg, |
| 3498 | cctx_T *cctx, |
| 3499 | char *op, |
| 3500 | ppconst_T *ppconst, |
| 3501 | int ppconst_used UNUSED) |
| 3502 | { |
| 3503 | char_u *next; |
| 3504 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
| 3505 | int opchar = *op; |
| 3506 | |
| 3507 | if (p[0] == opchar && p[1] == opchar) |
| 3508 | { |
| 3509 | garray_T *instr = &cctx->ctx_instr; |
| 3510 | garray_T end_ga; |
| 3511 | int save_skip = cctx->ctx_skip; |
| 3512 | |
| 3513 | /* |
| 3514 | * Repeat until there is no following "||" or "&&" |
| 3515 | */ |
| 3516 | ga_init2(&end_ga, sizeof(int), 10); |
| 3517 | while (p[0] == opchar && p[1] == opchar) |
| 3518 | { |
| 3519 | long start_lnum = SOURCING_LNUM; |
| 3520 | long save_sourcing_lnum; |
| 3521 | int start_ctx_lnum = cctx->ctx_lnum; |
| 3522 | int save_lnum; |
| 3523 | int const_used; |
| 3524 | int status; |
| 3525 | jumpwhen_T jump_when = opchar == '|' |
| 3526 | ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE; |
| 3527 | |
| 3528 | if (next != NULL) |
| 3529 | { |
| 3530 | *arg = next_line_from_context(cctx, TRUE); |
| 3531 | p = skipwhite(*arg); |
| 3532 | } |
| 3533 | |
| 3534 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 3535 | { |
| 3536 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 3537 | op, p); |
| 3538 | ga_clear(&end_ga); |
| 3539 | return FAIL; |
| 3540 | } |
| 3541 | |
| 3542 | save_sourcing_lnum = SOURCING_LNUM; |
| 3543 | SOURCING_LNUM = start_lnum; |
| 3544 | save_lnum = cctx->ctx_lnum; |
| 3545 | cctx->ctx_lnum = start_ctx_lnum; |
| 3546 | |
| 3547 | status = check_ppconst_bool(ppconst); |
| 3548 | if (status != FAIL) |
| 3549 | { |
| 3550 | // Use the last ppconst if possible. |
| 3551 | if (ppconst->pp_used > 0) |
| 3552 | { |
| 3553 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3554 | int is_true = tv2bool(tv); |
| 3555 | |
| 3556 | if ((is_true && opchar == '|') |
| 3557 | || (!is_true && opchar == '&')) |
| 3558 | { |
| 3559 | // For "false && expr" and "true || expr" the "expr" |
| 3560 | // does not need to be evaluated. |
| 3561 | cctx->ctx_skip = SKIP_YES; |
| 3562 | clear_tv(tv); |
| 3563 | tv->v_type = VAR_BOOL; |
| 3564 | tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE; |
| 3565 | } |
| 3566 | else |
| 3567 | { |
| 3568 | // For "true && expr" and "false || expr" only "expr" |
| 3569 | // needs to be evaluated. |
| 3570 | --ppconst->pp_used; |
| 3571 | jump_when = JUMP_NEVER; |
| 3572 | } |
| 3573 | } |
| 3574 | else |
| 3575 | { |
| 3576 | // Every part must evaluate to a bool. |
| 3577 | status = bool_on_stack(cctx); |
| 3578 | } |
| 3579 | } |
| 3580 | if (status != FAIL) |
| 3581 | status = ga_grow(&end_ga, 1); |
| 3582 | cctx->ctx_lnum = save_lnum; |
| 3583 | if (status == FAIL) |
| 3584 | { |
| 3585 | ga_clear(&end_ga); |
| 3586 | return FAIL; |
| 3587 | } |
| 3588 | |
| 3589 | if (jump_when != JUMP_NEVER) |
| 3590 | { |
| 3591 | if (cctx->ctx_skip != SKIP_YES) |
| 3592 | { |
| 3593 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 3594 | ++end_ga.ga_len; |
| 3595 | } |
| 3596 | generate_JUMP(cctx, jump_when, 0); |
| 3597 | } |
| 3598 | |
| 3599 | // eval the next expression |
| 3600 | SOURCING_LNUM = save_sourcing_lnum; |
| 3601 | if (may_get_next_line_error(p + 2, arg, cctx) == FAIL) |
| 3602 | { |
| 3603 | ga_clear(&end_ga); |
| 3604 | return FAIL; |
| 3605 | } |
| 3606 | |
| 3607 | const_used = ppconst->pp_used; |
| 3608 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 3609 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
| 3610 | { |
| 3611 | ga_clear(&end_ga); |
| 3612 | return FAIL; |
| 3613 | } |
| 3614 | |
| 3615 | // "0 || 1" results in true, "1 && 0" results in false. |
| 3616 | if (ppconst->pp_used == const_used + 1) |
| 3617 | { |
| 3618 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3619 | |
| 3620 | if (tv->v_type == VAR_NUMBER |
| 3621 | && (tv->vval.v_number == 1 || tv->vval.v_number == 0)) |
| 3622 | { |
| 3623 | tv->vval.v_number = tv->vval.v_number == 1 |
| 3624 | ? VVAL_TRUE : VVAL_FALSE; |
| 3625 | tv->v_type = VAR_BOOL; |
| 3626 | } |
| 3627 | } |
| 3628 | |
| 3629 | p = may_peek_next_line(cctx, *arg, &next); |
| 3630 | } |
| 3631 | |
| 3632 | if (check_ppconst_bool(ppconst) == FAIL) |
| 3633 | { |
| 3634 | ga_clear(&end_ga); |
| 3635 | return FAIL; |
| 3636 | } |
| 3637 | |
| 3638 | if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0) |
| 3639 | // Every part must evaluate to a bool. |
| 3640 | if (bool_on_stack(cctx) == FAIL) |
| 3641 | { |
| 3642 | ga_clear(&end_ga); |
| 3643 | return FAIL; |
| 3644 | } |
| 3645 | |
| 3646 | if (end_ga.ga_len > 0) |
| 3647 | { |
| 3648 | // Fill in the end label in all jumps. |
| 3649 | generate_ppconst(cctx, ppconst); |
| 3650 | while (end_ga.ga_len > 0) |
| 3651 | { |
| 3652 | isn_T *isn; |
| 3653 | |
| 3654 | --end_ga.ga_len; |
| 3655 | isn = ((isn_T *)instr->ga_data) |
| 3656 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 3657 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3658 | } |
| 3659 | } |
| 3660 | ga_clear(&end_ga); |
| 3661 | |
| 3662 | cctx->ctx_skip = save_skip; |
| 3663 | } |
| 3664 | |
| 3665 | return OK; |
| 3666 | } |
| 3667 | |
| 3668 | /* |
| 3669 | * expr4a && expr4a && expr4a logical AND |
| 3670 | * |
| 3671 | * Produces instructions: |
| 3672 | * EVAL expr4a Push result of "expr4a" |
| 3673 | * COND2BOOL convert to bool if needed |
| 3674 | * JUMP_IF_COND_FALSE end |
| 3675 | * EVAL expr4b Push result of "expr4b" |
| 3676 | * JUMP_IF_COND_FALSE end |
| 3677 | * EVAL expr4c Push result of "expr4c" |
| 3678 | * end: |
| 3679 | */ |
| 3680 | static int |
| 3681 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3682 | { |
| 3683 | int ppconst_used = ppconst->pp_used; |
| 3684 | |
| 3685 | // get the first variable |
| 3686 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
| 3687 | return FAIL; |
| 3688 | |
| 3689 | // || and && work almost the same |
| 3690 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
| 3691 | } |
| 3692 | |
| 3693 | /* |
| 3694 | * expr3a || expr3b || expr3c logical OR |
| 3695 | * |
| 3696 | * Produces instructions: |
| 3697 | * EVAL expr3a Push result of "expr3a" |
| 3698 | * COND2BOOL convert to bool if needed |
| 3699 | * JUMP_IF_COND_TRUE end |
| 3700 | * EVAL expr3b Push result of "expr3b" |
| 3701 | * JUMP_IF_COND_TRUE end |
| 3702 | * EVAL expr3c Push result of "expr3c" |
| 3703 | * end: |
| 3704 | */ |
| 3705 | static int |
| 3706 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3707 | { |
| 3708 | int ppconst_used = ppconst->pp_used; |
| 3709 | |
| 3710 | // eval the first expression |
| 3711 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
| 3712 | return FAIL; |
| 3713 | |
| 3714 | // || and && work almost the same |
| 3715 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
| 3716 | } |
| 3717 | |
| 3718 | /* |
| 3719 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 3720 | * Produces instructions: |
| 3721 | * EVAL expr2 Push result of "expr2" |
| 3722 | * JUMP_IF_FALSE alt jump if false |
| 3723 | * EVAL expr1a |
| 3724 | * JUMP_ALWAYS end |
| 3725 | * alt: EVAL expr1b |
| 3726 | * end: |
| 3727 | * |
| 3728 | * Toplevel expression: expr2 ?? expr1 |
| 3729 | * Produces instructions: |
| 3730 | * EVAL expr2 Push result of "expr2" |
| 3731 | * JUMP_AND_KEEP_IF_TRUE end jump if true |
| 3732 | * EVAL expr1 |
| 3733 | * end: |
| 3734 | */ |
| 3735 | int |
| 3736 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3737 | { |
| 3738 | char_u *p; |
| 3739 | int ppconst_used = ppconst->pp_used; |
| 3740 | char_u *next; |
| 3741 | |
| 3742 | // Ignore all kinds of errors when not producing code. |
| 3743 | if (cctx->ctx_skip == SKIP_YES) |
| 3744 | { |
Bram Moolenaar | 83d0cec | 2022-02-04 21:17:58 +0000 | [diff] [blame] | 3745 | int prev_did_emsg = did_emsg; |
| 3746 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3747 | skip_expr_cctx(arg, cctx); |
Bram Moolenaar | 83d0cec | 2022-02-04 21:17:58 +0000 | [diff] [blame] | 3748 | return did_emsg == prev_did_emsg ? OK : FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3749 | } |
| 3750 | |
| 3751 | // Evaluate the first expression. |
| 3752 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
| 3753 | return FAIL; |
| 3754 | |
| 3755 | p = may_peek_next_line(cctx, *arg, &next); |
| 3756 | if (*p == '?') |
| 3757 | { |
| 3758 | int op_falsy = p[1] == '?'; |
| 3759 | garray_T *instr = &cctx->ctx_instr; |
| 3760 | garray_T *stack = &cctx->ctx_type_stack; |
| 3761 | int alt_idx = instr->ga_len; |
| 3762 | int end_idx = 0; |
| 3763 | isn_T *isn; |
| 3764 | type_T *type1 = NULL; |
| 3765 | int has_const_expr = FALSE; |
| 3766 | int const_value = FALSE; |
| 3767 | int save_skip = cctx->ctx_skip; |
| 3768 | |
| 3769 | if (next != NULL) |
| 3770 | { |
| 3771 | *arg = next_line_from_context(cctx, TRUE); |
| 3772 | p = skipwhite(*arg); |
| 3773 | } |
| 3774 | |
| 3775 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy])) |
| 3776 | { |
| 3777 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 3778 | op_falsy ? "??" : "?", p); |
| 3779 | return FAIL; |
| 3780 | } |
| 3781 | |
| 3782 | if (ppconst->pp_used == ppconst_used + 1) |
| 3783 | { |
| 3784 | // the condition is a constant, we know whether the ? or the : |
| 3785 | // expression is to be evaluated. |
| 3786 | has_const_expr = TRUE; |
| 3787 | if (op_falsy) |
| 3788 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 3789 | else |
| 3790 | { |
| 3791 | int error = FALSE; |
| 3792 | |
| 3793 | const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used], |
| 3794 | &error); |
| 3795 | if (error) |
| 3796 | return FAIL; |
| 3797 | } |
| 3798 | cctx->ctx_skip = save_skip == SKIP_YES || |
| 3799 | (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT; |
| 3800 | |
| 3801 | if (op_falsy && cctx->ctx_skip == SKIP_YES) |
| 3802 | // "left ?? right" and "left" is truthy: produce "left" |
| 3803 | generate_ppconst(cctx, ppconst); |
| 3804 | else |
| 3805 | { |
| 3806 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 3807 | --ppconst->pp_used; |
| 3808 | } |
| 3809 | } |
| 3810 | else |
| 3811 | { |
| 3812 | generate_ppconst(cctx, ppconst); |
| 3813 | if (op_falsy) |
| 3814 | end_idx = instr->ga_len; |
| 3815 | generate_JUMP(cctx, op_falsy |
| 3816 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0); |
| 3817 | if (op_falsy) |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 3818 | type1 = get_type_on_stack(cctx, -1); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | // evaluate the second expression; any type is accepted |
| 3822 | if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL) |
| 3823 | return FAIL; |
| 3824 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
| 3825 | return FAIL; |
| 3826 | |
| 3827 | if (!has_const_expr) |
| 3828 | { |
| 3829 | generate_ppconst(cctx, ppconst); |
| 3830 | |
| 3831 | if (!op_falsy) |
| 3832 | { |
| 3833 | // remember the type and drop it |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 3834 | type1 = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3835 | --stack->ga_len; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3836 | |
| 3837 | end_idx = instr->ga_len; |
| 3838 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 3839 | |
| 3840 | // jump here from JUMP_IF_FALSE |
| 3841 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 3842 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3843 | } |
| 3844 | } |
| 3845 | |
| 3846 | if (!op_falsy) |
| 3847 | { |
| 3848 | // Check for the ":". |
| 3849 | p = may_peek_next_line(cctx, *arg, &next); |
| 3850 | if (*p != ':') |
| 3851 | { |
| 3852 | emsg(_(e_missing_colon_after_questionmark)); |
| 3853 | return FAIL; |
| 3854 | } |
| 3855 | if (next != NULL) |
| 3856 | { |
| 3857 | *arg = next_line_from_context(cctx, TRUE); |
| 3858 | p = skipwhite(*arg); |
| 3859 | } |
| 3860 | |
| 3861 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 3862 | { |
| 3863 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 3864 | ":", p); |
| 3865 | return FAIL; |
| 3866 | } |
| 3867 | |
| 3868 | // evaluate the third expression |
| 3869 | if (has_const_expr) |
| 3870 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 3871 | ? SKIP_YES : SKIP_NOT; |
| 3872 | if (may_get_next_line_error(p + 1, arg, cctx) == FAIL) |
| 3873 | return FAIL; |
| 3874 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
| 3875 | return FAIL; |
| 3876 | } |
| 3877 | |
| 3878 | if (!has_const_expr) |
| 3879 | { |
| 3880 | type_T **typep; |
| 3881 | |
| 3882 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | fa46ead | 2021-12-22 13:18:39 +0000 | [diff] [blame] | 3883 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3884 | |
| 3885 | // If the types differ, the result has a more generic type. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 3886 | typep = &((((type2_T *)stack->ga_data) |
| 3887 | + stack->ga_len - 1)->type_curr); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 3888 | common_type(type1, *typep, typep, cctx->ctx_type_list); |
| 3889 | |
| 3890 | // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE |
| 3891 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 3892 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3893 | } |
| 3894 | |
| 3895 | cctx->ctx_skip = save_skip; |
| 3896 | } |
| 3897 | return OK; |
| 3898 | } |
| 3899 | |
| 3900 | /* |
| 3901 | * Toplevel expression. |
| 3902 | * Sets "is_const" (if not NULL) to indicate the value is a constant. |
| 3903 | * Returns OK or FAIL. |
| 3904 | */ |
| 3905 | int |
| 3906 | compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const) |
| 3907 | { |
| 3908 | ppconst_T ppconst; |
| 3909 | |
| 3910 | CLEAR_FIELD(ppconst); |
| 3911 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 3912 | { |
| 3913 | clear_ppconst(&ppconst); |
| 3914 | return FAIL; |
| 3915 | } |
| 3916 | if (is_const != NULL) |
| 3917 | *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const; |
| 3918 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 3919 | return FAIL; |
| 3920 | return OK; |
| 3921 | } |
| 3922 | |
| 3923 | /* |
| 3924 | * Toplevel expression. |
| 3925 | */ |
| 3926 | int |
| 3927 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 3928 | { |
| 3929 | return compile_expr0_ext(arg, cctx, NULL); |
| 3930 | } |
| 3931 | |
| 3932 | |
| 3933 | #endif // defined(FEAT_EVAL) |