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