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