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