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