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