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 | /* |
| 11 | * vim9instr.c: Dealing with instructions of a compiled function |
| 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 | |
| 24 | |
| 25 | ///////////////////////////////////////////////////////////////////// |
| 26 | // Following generate_ functions expect the caller to call ga_grow(). |
| 27 | |
| 28 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 29 | #define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK |
| 30 | |
| 31 | /* |
| 32 | * Generate an instruction without arguments. |
| 33 | * Returns a pointer to the new instruction, NULL if failed. |
| 34 | */ |
| 35 | isn_T * |
| 36 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 37 | { |
| 38 | garray_T *instr = &cctx->ctx_instr; |
| 39 | isn_T *isn; |
| 40 | |
| 41 | RETURN_NULL_IF_SKIP(cctx); |
| 42 | if (GA_GROW_FAILS(instr, 1)) |
| 43 | return NULL; |
| 44 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 45 | isn->isn_type = isn_type; |
| 46 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 47 | ++instr->ga_len; |
| 48 | |
| 49 | return isn; |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Generate an instruction without arguments. |
| 54 | * "drop" will be removed from the stack. |
| 55 | * Returns a pointer to the new instruction, NULL if failed. |
| 56 | */ |
| 57 | isn_T * |
| 58 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 59 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 60 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 61 | cctx->ctx_type_stack.ga_len -= drop; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 62 | return generate_instr(cctx, isn_type); |
| 63 | } |
| 64 | |
| 65 | /* |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 66 | * Generate instruction "isn_type" and put "type" on the type stack, |
| 67 | * use "decl_type" for the declared type. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 68 | */ |
Yegappan Lakshmanan | 782b43d | 2022-01-08 18:43:40 +0000 | [diff] [blame] | 69 | static isn_T * |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 70 | generate_instr_type2( |
| 71 | cctx_T *cctx, |
| 72 | isntype_T isn_type, |
| 73 | type_T *type, |
| 74 | type_T *decl_type) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 75 | { |
| 76 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 77 | |
| 78 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 79 | return NULL; |
| 80 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 81 | if (push_type_stack2(cctx, type == NULL ? &t_any : type, |
| 82 | decl_type == NULL ? &t_any : decl_type) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 83 | return NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 84 | |
| 85 | return isn; |
| 86 | } |
| 87 | |
| 88 | /* |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 89 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 90 | * Uses "any" for the declared type, which works for constants. For declared |
| 91 | * variables use generate_instr_type2(). |
| 92 | */ |
| 93 | isn_T * |
| 94 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 95 | { |
| 96 | return generate_instr_type2(cctx, isn_type, type, &t_any); |
| 97 | } |
| 98 | |
| 99 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 100 | * Generate an ISN_DEBUG instruction. |
| 101 | */ |
| 102 | isn_T * |
| 103 | generate_instr_debug(cctx_T *cctx) |
| 104 | { |
| 105 | isn_T *isn; |
| 106 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 107 | + cctx->ctx_ufunc->uf_dfunc_idx; |
| 108 | |
| 109 | if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL) |
| 110 | return NULL; |
| 111 | isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len; |
| 112 | isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum; |
| 113 | return isn; |
| 114 | } |
| 115 | |
| 116 | /* |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 117 | * Generate an ISN_CONSTRUCT instruction. |
| 118 | * The object will have "size" members. |
| 119 | */ |
| 120 | int |
| 121 | generate_CONSTRUCT(cctx_T *cctx, class_T *cl) |
| 122 | { |
| 123 | isn_T *isn; |
| 124 | |
| 125 | RETURN_OK_IF_SKIP(cctx); |
| 126 | if ((isn = generate_instr(cctx, ISN_CONSTRUCT)) == NULL) |
| 127 | return FAIL; |
| 128 | isn->isn_arg.construct.construct_size = sizeof(object_T) |
| 129 | + cl->class_obj_member_count * sizeof(typval_T); |
| 130 | isn->isn_arg.construct.construct_class = cl; |
| 131 | return OK; |
| 132 | } |
| 133 | |
| 134 | /* |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 135 | * Generate ISN_GET_OBJ_MEMBER - access member of object at bottom of stack by |
| 136 | * index. |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 137 | */ |
| 138 | int |
Yegappan Lakshmanan | 5a05d37 | 2023-09-29 19:43:11 +0200 | [diff] [blame] | 139 | generate_GET_OBJ_MEMBER(cctx_T *cctx, int idx, type_T *type) |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 140 | { |
| 141 | RETURN_OK_IF_SKIP(cctx); |
| 142 | |
| 143 | // drop the object type |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 144 | isn_T *isn = generate_instr_drop(cctx, ISN_GET_OBJ_MEMBER, 1); |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 145 | if (isn == NULL) |
| 146 | return FAIL; |
| 147 | |
Ernie Rael | 18143d3 | 2023-09-04 22:30:41 +0200 | [diff] [blame] | 148 | isn->isn_arg.classmember.cm_class = NULL; |
| 149 | isn->isn_arg.classmember.cm_idx = idx; |
Bram Moolenaar | ffdaca9 | 2022-12-09 21:41:48 +0000 | [diff] [blame] | 150 | return push_type_stack2(cctx, type, &t_any); |
| 151 | } |
| 152 | |
| 153 | /* |
Bram Moolenaar | 29ac5df | 2023-01-16 19:43:47 +0000 | [diff] [blame] | 154 | * Generate ISN_GET_ITF_MEMBER - access member of interface at bottom of stack |
| 155 | * by index. |
| 156 | */ |
| 157 | int |
Yegappan Lakshmanan | 5a05d37 | 2023-09-29 19:43:11 +0200 | [diff] [blame] | 158 | generate_GET_ITF_MEMBER(cctx_T *cctx, class_T *itf, int idx, type_T *type) |
Bram Moolenaar | 29ac5df | 2023-01-16 19:43:47 +0000 | [diff] [blame] | 159 | { |
| 160 | RETURN_OK_IF_SKIP(cctx); |
| 161 | |
| 162 | // drop the object type |
| 163 | isn_T *isn = generate_instr_drop(cctx, ISN_GET_ITF_MEMBER, 1); |
| 164 | if (isn == NULL) |
| 165 | return FAIL; |
| 166 | |
| 167 | isn->isn_arg.classmember.cm_class = itf; |
| 168 | ++itf->class_refcount; |
| 169 | isn->isn_arg.classmember.cm_idx = idx; |
| 170 | return push_type_stack2(cctx, type, &t_any); |
| 171 | } |
| 172 | |
| 173 | /* |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 174 | * Generate ISN_STORE_THIS - store value in member of "this" object with member |
| 175 | * index "idx". |
| 176 | */ |
| 177 | int |
| 178 | generate_STORE_THIS(cctx_T *cctx, int idx) |
| 179 | { |
| 180 | RETURN_OK_IF_SKIP(cctx); |
| 181 | |
| 182 | // drop the value type |
| 183 | isn_T *isn = generate_instr_drop(cctx, ISN_STORE_THIS, 1); |
| 184 | if (isn == NULL) |
| 185 | return FAIL; |
| 186 | |
| 187 | isn->isn_arg.number = idx; |
| 188 | return OK; |
| 189 | } |
| 190 | |
| 191 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 192 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 193 | * But only for simple types. |
Yegappan Lakshmanan | bce51d9 | 2024-04-15 19:19:52 +0200 | [diff] [blame] | 194 | * When tostring_flags has TOSTRING_TOLERANT, convert a List to a series of |
| 195 | * strings. When tostring_flags has TOSTRING_INTERPOLATE, convert a List or a |
| 196 | * Dict to the corresponding textual representation. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 197 | */ |
| 198 | int |
Yegappan Lakshmanan | bce51d9 | 2024-04-15 19:19:52 +0200 | [diff] [blame] | 199 | may_generate_2STRING(int offset, int tostring_flags, cctx_T *cctx) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 200 | { |
| 201 | isn_T *isn; |
| 202 | isntype_T isntype = ISN_2STRING; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 203 | type_T *type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 204 | |
| 205 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 206 | type = get_type_on_stack(cctx, -1 - offset); |
| 207 | switch (type->tt_type) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 208 | { |
| 209 | // nothing to be done |
| 210 | case VAR_STRING: return OK; |
| 211 | |
| 212 | // conversion possible |
| 213 | case VAR_SPECIAL: |
| 214 | case VAR_BOOL: |
| 215 | case VAR_NUMBER: |
| 216 | case VAR_FLOAT: |
| 217 | break; |
| 218 | |
| 219 | // conversion possible (with runtime check) |
| 220 | case VAR_ANY: |
| 221 | case VAR_UNKNOWN: |
| 222 | isntype = ISN_2STRING_ANY; |
| 223 | break; |
| 224 | |
| 225 | // conversion possible when tolerant |
| 226 | case VAR_LIST: |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 227 | case VAR_TUPLE: |
Yegappan Lakshmanan | f01493c | 2024-04-14 23:21:02 +0200 | [diff] [blame] | 228 | case VAR_DICT: |
Yegappan Lakshmanan | bce51d9 | 2024-04-15 19:19:52 +0200 | [diff] [blame] | 229 | if (tostring_flags & TOSTRING_TOLERANT) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 230 | { |
| 231 | isntype = ISN_2STRING_ANY; |
| 232 | break; |
| 233 | } |
Yegappan Lakshmanan | bce51d9 | 2024-04-15 19:19:52 +0200 | [diff] [blame] | 234 | if (tostring_flags & TOSTRING_INTERPOLATE) |
| 235 | break; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 236 | // FALLTHROUGH |
| 237 | |
| 238 | // conversion not possible |
| 239 | case VAR_VOID: |
| 240 | case VAR_BLOB: |
| 241 | case VAR_FUNC: |
| 242 | case VAR_PARTIAL: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 243 | case VAR_JOB: |
| 244 | case VAR_CHANNEL: |
| 245 | case VAR_INSTR: |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 246 | case VAR_CLASS: |
| 247 | case VAR_OBJECT: |
Yegappan Lakshmanan | ec3cebb | 2023-10-27 19:35:26 +0200 | [diff] [blame] | 248 | case VAR_TYPEALIAS: |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 249 | to_string_error(type->tt_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 250 | return FAIL; |
| 251 | } |
| 252 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 253 | set_type_on_stack(cctx, &t_string, -1 - offset); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 254 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 255 | return FAIL; |
| 256 | isn->isn_arg.tostring.offset = offset; |
Yegappan Lakshmanan | bce51d9 | 2024-04-15 19:19:52 +0200 | [diff] [blame] | 257 | isn->isn_arg.tostring.flags = tostring_flags; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 258 | |
| 259 | return OK; |
| 260 | } |
| 261 | |
| 262 | static int |
Ernie Rael | e75fde6 | 2023-12-21 17:18:54 +0100 | [diff] [blame] | 263 | check_number_or_float(type_T *typ1, type_T *typ2, char_u *op) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 264 | { |
Ernie Rael | e75fde6 | 2023-12-21 17:18:54 +0100 | [diff] [blame] | 265 | vartype_T type1 = typ1->tt_type; |
| 266 | vartype_T type2 = typ2->tt_type; |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 267 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT |
| 268 | || type1 == VAR_ANY || type1 == VAR_UNKNOWN) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 269 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 270 | || type2 == VAR_ANY || type2 == VAR_UNKNOWN))) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 271 | { |
Ernie Rael | e75fde6 | 2023-12-21 17:18:54 +0100 | [diff] [blame] | 272 | if (check_type_is_value(typ1) == FAIL |
| 273 | || check_type_is_value(typ2) == FAIL) |
| 274 | return FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 275 | if (*op == '+') |
| 276 | emsg(_(e_wrong_argument_type_for_plus)); |
| 277 | else |
| 278 | semsg(_(e_char_requires_number_or_float_arguments), *op); |
| 279 | return FAIL; |
| 280 | } |
| 281 | return OK; |
| 282 | } |
| 283 | |
| 284 | /* |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 285 | * Append the tuple item types from "tuple_type" to the grow array "gap". |
| 286 | */ |
| 287 | static int |
| 288 | ga_append_tuple_types(type_T *tuple_type, garray_T *gap) |
| 289 | { |
| 290 | for (int i = 0; i < tuple_type->tt_argcount; i++) |
| 291 | { |
| 292 | if (ga_grow(gap, 1) == FAIL) |
| 293 | return FAIL; |
| 294 | |
| 295 | ((type_T **)gap->ga_data)[gap->ga_len] = tuple_type->tt_args[i]; |
| 296 | gap->ga_len++; |
| 297 | } |
| 298 | |
| 299 | return OK; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * When concatenating two tuples, the resulting tuple gets a union of item |
| 304 | * types from both the tuples. This function sets the union tuple type in the |
| 305 | * stack. |
| 306 | * |
| 307 | * Returns OK on success and FAIL on memory allocation failure. |
| 308 | */ |
| 309 | static int |
| 310 | set_tuple_union_type_on_stack(type_T *type1, type_T *type2, cctx_T *cctx) |
| 311 | { |
| 312 | // The concatenated tuple has the union of types from both the tuples |
| 313 | garray_T tuple_types_ga; |
| 314 | |
| 315 | ga_init2(&tuple_types_ga, sizeof(type_T *), 10); |
| 316 | |
| 317 | if (type1->tt_argcount > 0) |
| 318 | ga_append_tuple_types(type1, &tuple_types_ga); |
| 319 | if (!(type1->tt_flags & TTFLAG_VARARGS) && (type2->tt_argcount > 0)) |
| 320 | ga_append_tuple_types(type2, &tuple_types_ga); |
| 321 | |
| 322 | type_T *new_tuple_type = get_tuple_type(&tuple_types_ga, |
| 323 | cctx->ctx_type_list); |
| 324 | // result inherits the variadic flag from the operands |
| 325 | new_tuple_type->tt_flags |= (type1->tt_flags & TTFLAG_VARARGS) |
| 326 | | (type2->tt_flags & TTFLAG_VARARGS); |
| 327 | |
| 328 | // set the type on the stack for the resulting tuple |
| 329 | set_type_on_stack(cctx, new_tuple_type, 0); |
| 330 | |
| 331 | ga_clear(&tuple_types_ga); |
| 332 | |
| 333 | return OK; |
| 334 | } |
| 335 | |
| 336 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 337 | * Generate instruction for "+". For a list this creates a new list. |
| 338 | */ |
| 339 | int |
| 340 | generate_add_instr( |
Bram Moolenaar | bcf31ec | 2023-01-02 20:32:24 +0000 | [diff] [blame] | 341 | cctx_T *cctx, |
| 342 | vartype_T vartype, |
| 343 | type_T *type1, |
| 344 | type_T *type2, |
| 345 | exprtype_T expr_type) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 346 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 347 | isn_T *isn = generate_instr_drop(cctx, |
| 348 | vartype == VAR_NUMBER ? ISN_OPNR |
| 349 | : vartype == VAR_LIST ? ISN_ADDLIST |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 350 | : vartype == VAR_TUPLE ? ISN_ADDTUPLE |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 351 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 352 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 353 | : ISN_OPANY, 1); |
| 354 | |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 355 | if (vartype != VAR_LIST && vartype != VAR_BLOB && vartype != VAR_TUPLE |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 356 | && type1->tt_type != VAR_ANY |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 357 | && type1->tt_type != VAR_UNKNOWN |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 358 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 359 | && type2->tt_type != VAR_UNKNOWN |
Ernie Rael | e75fde6 | 2023-12-21 17:18:54 +0100 | [diff] [blame] | 360 | && check_number_or_float(type1, type2, (char_u *)"+") == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 361 | return FAIL; |
| 362 | |
| 363 | if (isn != NULL) |
| 364 | { |
| 365 | if (isn->isn_type == ISN_ADDLIST) |
| 366 | isn->isn_arg.op.op_type = expr_type; |
| 367 | else |
| 368 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 369 | } |
| 370 | |
| 371 | // When concatenating two lists with different member types the member type |
| 372 | // becomes "any". |
| 373 | if (vartype == VAR_LIST |
| 374 | && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST |
| 375 | && type1->tt_member != type2->tt_member) |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 376 | set_type_on_stack(cctx, &t_list_any, 0); |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 377 | else if (vartype == VAR_TUPLE) |
| 378 | { |
| 379 | if (!check_tuples_addable(type1, type2)) |
| 380 | return FAIL; |
| 381 | |
| 382 | if (set_tuple_union_type_on_stack(type1, type2, cctx) == FAIL) |
| 383 | return FAIL; |
| 384 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 385 | |
| 386 | return isn == NULL ? FAIL : OK; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Get the type to use for an instruction for an operation on "type1" and |
| 391 | * "type2". If they are matching use a type-specific instruction. Otherwise |
| 392 | * fall back to runtime type checking. |
| 393 | */ |
| 394 | vartype_T |
| 395 | operator_type(type_T *type1, type_T *type2) |
| 396 | { |
| 397 | if (type1->tt_type == type2->tt_type |
| 398 | && (type1->tt_type == VAR_NUMBER |
| 399 | || type1->tt_type == VAR_LIST |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 400 | || type1->tt_type == VAR_TUPLE |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 401 | || type1->tt_type == VAR_FLOAT |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 402 | || type1->tt_type == VAR_BLOB)) |
| 403 | return type1->tt_type; |
| 404 | return VAR_ANY; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Generate an instruction with two arguments. The instruction depends on the |
| 409 | * type of the arguments. |
| 410 | */ |
| 411 | int |
| 412 | generate_two_op(cctx_T *cctx, char_u *op) |
| 413 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 414 | type_T *type1; |
| 415 | type_T *type2; |
| 416 | vartype_T vartype; |
| 417 | isn_T *isn; |
| 418 | |
| 419 | RETURN_OK_IF_SKIP(cctx); |
| 420 | |
| 421 | // Get the known type of the two items on the stack. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 422 | type1 = get_type_on_stack(cctx, 1); |
| 423 | type2 = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 424 | vartype = operator_type(type1, type2); |
| 425 | |
| 426 | switch (*op) |
| 427 | { |
| 428 | case '+': |
| 429 | if (generate_add_instr(cctx, vartype, type1, type2, |
| 430 | EXPR_COPY) == FAIL) |
| 431 | return FAIL; |
| 432 | break; |
| 433 | |
| 434 | case '-': |
| 435 | case '*': |
Ernie Rael | e75fde6 | 2023-12-21 17:18:54 +0100 | [diff] [blame] | 436 | case '/': if (check_number_or_float(type1, type2, op) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 437 | return FAIL; |
| 438 | if (vartype == VAR_NUMBER) |
| 439 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 440 | else if (vartype == VAR_FLOAT) |
| 441 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 442 | else |
| 443 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 444 | if (isn != NULL) |
| 445 | isn->isn_arg.op.op_type = *op == '*' |
| 446 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 447 | break; |
| 448 | |
| 449 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 450 | && type1->tt_type != VAR_UNKNOWN |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 451 | && type1->tt_type != VAR_NUMBER) |
| 452 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 453 | && type2->tt_type != VAR_UNKNOWN |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 454 | && type2->tt_type != VAR_NUMBER)) |
| 455 | { |
| 456 | emsg(_(e_percent_requires_number_arguments)); |
| 457 | return FAIL; |
| 458 | } |
| 459 | isn = generate_instr_drop(cctx, |
| 460 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 461 | if (isn != NULL) |
| 462 | isn->isn_arg.op.op_type = EXPR_REM; |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | // correct type of result |
| 467 | if (vartype == VAR_ANY) |
| 468 | { |
| 469 | type_T *type = &t_any; |
| 470 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 471 | // float+number and number+float results in float |
| 472 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 473 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 474 | type = &t_float; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 475 | set_type_on_stack(cctx, type, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | return OK; |
| 479 | } |
| 480 | |
| 481 | /* |
Ernie Rael | e75fde6 | 2023-12-21 17:18:54 +0100 | [diff] [blame] | 482 | * Choose correct error message for the specified type information. |
| 483 | */ |
| 484 | static isntype_T |
| 485 | compare_isn_not_values(typval_T *tv, type_T *type) |
| 486 | { |
| 487 | if (tv != NULL) |
Christian Brabandt | 6b9492e | 2023-12-24 11:14:37 +0100 | [diff] [blame] | 488 | (void)check_typval_is_value(tv); |
Ernie Rael | e75fde6 | 2023-12-21 17:18:54 +0100 | [diff] [blame] | 489 | else |
Christian Brabandt | 6b9492e | 2023-12-24 11:14:37 +0100 | [diff] [blame] | 490 | (void)check_type_is_value(type); |
Ernie Rael | e75fde6 | 2023-12-21 17:18:54 +0100 | [diff] [blame] | 491 | return ISN_DROP; |
| 492 | } |
| 493 | |
| 494 | /* |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 495 | * Get the instruction to use for comparing two values with specified types. |
| 496 | * Either "tv1" and "tv2" are passed or "type1" and "type2". |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 497 | * Return ISN_DROP when failed. |
| 498 | */ |
| 499 | static isntype_T |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 500 | get_compare_isn( |
Bram Moolenaar | 2ed57ac | 2023-04-01 22:05:38 +0100 | [diff] [blame] | 501 | exprtype_T exprtype, |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 502 | typval_T *tv1, |
| 503 | typval_T *tv2, |
| 504 | type_T *type1, |
| 505 | type_T *type2) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 506 | { |
| 507 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 508 | vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type; |
| 509 | vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 510 | |
Ernie Rael | e75fde6 | 2023-12-21 17:18:54 +0100 | [diff] [blame] | 511 | if (vartype1 == VAR_CLASS || vartype1 == VAR_TYPEALIAS) |
| 512 | return compare_isn_not_values(tv1, type1); |
| 513 | if (vartype2 == VAR_CLASS || vartype2 == VAR_TYPEALIAS) |
| 514 | return compare_isn_not_values(tv2, type2); |
| 515 | |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 516 | if (vartype1 == vartype2) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 517 | { |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 518 | switch (vartype1) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 519 | { |
| 520 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 521 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 522 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 523 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 524 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 525 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 526 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 527 | case VAR_TUPLE: isntype = ISN_COMPARETUPLE; break; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 528 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 529 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | bcf31ec | 2023-01-02 20:32:24 +0000 | [diff] [blame] | 530 | case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 531 | default: isntype = ISN_COMPAREANY; break; |
| 532 | } |
| 533 | } |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 534 | else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY |
| 535 | || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT) |
| 536 | && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT)) |
| 537 | || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL) |
| 538 | || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC)) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 539 | isntype = ISN_COMPAREANY; |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 540 | else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL) |
Bram Moolenaar | 7a22224 | 2022-03-01 19:23:24 +0000 | [diff] [blame] | 541 | { |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 542 | if ((vartype1 == VAR_SPECIAL |
| 543 | && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE |
| 544 | : type1 == &t_none) |
| 545 | && vartype2 != VAR_STRING) |
| 546 | || (vartype2 == VAR_SPECIAL |
| 547 | && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE |
| 548 | : type2 == &t_none) |
| 549 | && vartype1 != VAR_STRING)) |
| 550 | { |
| 551 | semsg(_(e_cannot_compare_str_with_str), |
| 552 | vartype_name(vartype1), vartype_name(vartype2)); |
| 553 | return ISN_DROP; |
| 554 | } |
Bram Moolenaar | 0566781 | 2022-03-15 20:21:33 +0000 | [diff] [blame] | 555 | // although comparing null with number, float or bool is not useful, we |
| 556 | // allow it |
Bram Moolenaar | 7a22224 | 2022-03-01 19:23:24 +0000 | [diff] [blame] | 557 | isntype = ISN_COMPARENULL; |
| 558 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 559 | |
| 560 | if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT) |
| 561 | && (isntype == ISN_COMPAREBOOL |
| 562 | || isntype == ISN_COMPARESPECIAL |
| 563 | || isntype == ISN_COMPARENR |
| 564 | || isntype == ISN_COMPAREFLOAT)) |
| 565 | { |
| 566 | semsg(_(e_cannot_use_str_with_str), |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 567 | exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 568 | return ISN_DROP; |
| 569 | } |
Bram Moolenaar | bcf31ec | 2023-01-02 20:32:24 +0000 | [diff] [blame] | 570 | if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT |
| 571 | || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL) |
Ernie Rael | e75fde6 | 2023-12-21 17:18:54 +0100 | [diff] [blame] | 572 | && (isntype == ISN_COMPAREOBJECT)) |
Bram Moolenaar | bcf31ec | 2023-01-02 20:32:24 +0000 | [diff] [blame] | 573 | { |
| 574 | semsg(_(e_invalid_operation_for_str), vartype_name(vartype1)); |
| 575 | return ISN_DROP; |
| 576 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 577 | if (isntype == ISN_DROP |
Bram Moolenaar | 2ed57ac | 2023-04-01 22:05:38 +0100 | [diff] [blame] | 578 | || (isntype != ISN_COMPARENULL |
| 579 | && (((exprtype != EXPR_EQUAL |
| 580 | && exprtype != EXPR_NEQUAL |
| 581 | && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL |
| 582 | || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL))) |
| 583 | || ((exprtype != EXPR_EQUAL |
| 584 | && exprtype != EXPR_NEQUAL |
| 585 | && exprtype != EXPR_IS |
| 586 | && exprtype != EXPR_ISNOT |
| 587 | && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB |
| 588 | || vartype1 == VAR_LIST || vartype2 == VAR_LIST)))))) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 589 | { |
| 590 | semsg(_(e_cannot_compare_str_with_str), |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 591 | vartype_name(vartype1), vartype_name(vartype2)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 592 | return ISN_DROP; |
| 593 | } |
| 594 | return isntype; |
| 595 | } |
| 596 | |
| 597 | int |
| 598 | check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2) |
| 599 | { |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 600 | if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 601 | return FAIL; |
| 602 | return OK; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 607 | */ |
| 608 | int |
| 609 | generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic) |
| 610 | { |
| 611 | isntype_T isntype; |
| 612 | isn_T *isn; |
| 613 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 614 | |
| 615 | RETURN_OK_IF_SKIP(cctx); |
| 616 | |
| 617 | // Get the known type of the two items on the stack. If they are matching |
| 618 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 619 | // checking. |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 620 | isntype = get_compare_isn(exprtype, NULL, NULL, |
| 621 | get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 622 | if (isntype == ISN_DROP) |
| 623 | return FAIL; |
| 624 | |
| 625 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 626 | return FAIL; |
| 627 | isn->isn_arg.op.op_type = exprtype; |
| 628 | isn->isn_arg.op.op_ic = ic; |
| 629 | |
| 630 | // takes two arguments, puts one bool back |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 631 | --stack->ga_len; |
| 632 | set_type_on_stack(cctx, &t_bool, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 633 | |
| 634 | return OK; |
| 635 | } |
| 636 | |
| 637 | /* |
LemonBoy | 372bcce | 2022-04-25 12:43:20 +0100 | [diff] [blame] | 638 | * Generate an ISN_CONCAT instruction. |
| 639 | * "count" is the number of stack elements to join together and it must be |
| 640 | * greater or equal to one. |
| 641 | * The caller ensures all the "count" elements on the stack have the right type. |
| 642 | */ |
| 643 | int |
| 644 | generate_CONCAT(cctx_T *cctx, int count) |
| 645 | { |
| 646 | isn_T *isn; |
| 647 | garray_T *stack = &cctx->ctx_type_stack; |
| 648 | |
| 649 | RETURN_OK_IF_SKIP(cctx); |
| 650 | |
LemonBoy | 372bcce | 2022-04-25 12:43:20 +0100 | [diff] [blame] | 651 | if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL) |
| 652 | return FAIL; |
| 653 | isn->isn_arg.number = count; |
| 654 | |
| 655 | // drop the argument types |
| 656 | stack->ga_len -= count - 1; |
| 657 | |
| 658 | return OK; |
| 659 | } |
| 660 | |
| 661 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 662 | * Generate an ISN_2BOOL instruction. |
| 663 | * "offset" is the offset in the type stack. |
| 664 | */ |
| 665 | int |
| 666 | generate_2BOOL(cctx_T *cctx, int invert, int offset) |
| 667 | { |
| 668 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 669 | |
| 670 | RETURN_OK_IF_SKIP(cctx); |
| 671 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 672 | return FAIL; |
| 673 | isn->isn_arg.tobool.invert = invert; |
| 674 | isn->isn_arg.tobool.offset = offset; |
| 675 | |
| 676 | // type becomes bool |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 677 | set_type_on_stack(cctx, &t_bool, -1 - offset); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 678 | |
| 679 | return OK; |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * Generate an ISN_COND2BOOL instruction. |
| 684 | */ |
| 685 | int |
| 686 | generate_COND2BOOL(cctx_T *cctx) |
| 687 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 688 | RETURN_OK_IF_SKIP(cctx); |
Yegappan Lakshmanan | 6b085b9 | 2022-09-04 12:47:21 +0100 | [diff] [blame] | 689 | if (generate_instr(cctx, ISN_COND2BOOL) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 690 | return FAIL; |
| 691 | |
| 692 | // type becomes bool |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 693 | set_type_on_stack(cctx, &t_bool, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 694 | |
| 695 | return OK; |
| 696 | } |
| 697 | |
| 698 | int |
| 699 | generate_TYPECHECK( |
| 700 | cctx_T *cctx, |
| 701 | type_T *expected, |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 702 | int number_ok, // add TTFLAG_NUMBER_OK flag |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 703 | int offset, |
Bram Moolenaar | bd3a9d2 | 2022-05-17 16:12:39 +0100 | [diff] [blame] | 704 | int is_var, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 705 | int argidx) |
| 706 | { |
| 707 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 708 | |
| 709 | RETURN_OK_IF_SKIP(cctx); |
| 710 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 711 | return FAIL; |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 712 | type_T *tt; |
| 713 | if (expected->tt_type == VAR_FLOAT && number_ok) |
| 714 | { |
| 715 | // always allocate, also for static types |
| 716 | tt = ALLOC_ONE(type_T); |
| 717 | if (tt != NULL) |
| 718 | { |
| 719 | *tt = *expected; |
Bram Moolenaar | c4b3f64 | 2022-12-30 10:36:34 +0000 | [diff] [blame] | 720 | tt->tt_flags &= ~TTFLAG_STATIC; |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 721 | tt->tt_flags |= TTFLAG_NUMBER_OK; |
| 722 | } |
| 723 | } |
| 724 | else |
| 725 | tt = alloc_type(expected); |
| 726 | |
| 727 | isn->isn_arg.type.ct_type = tt; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 728 | isn->isn_arg.type.ct_off = (int8_T)offset; |
Bram Moolenaar | bd3a9d2 | 2022-05-17 16:12:39 +0100 | [diff] [blame] | 729 | isn->isn_arg.type.ct_is_var = is_var; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 730 | isn->isn_arg.type.ct_arg_idx = (int8_T)argidx; |
| 731 | |
| 732 | // type becomes expected |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 733 | set_type_on_stack(cctx, expected, -1 - offset); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 734 | |
| 735 | return OK; |
| 736 | } |
| 737 | |
| 738 | int |
| 739 | generate_SETTYPE( |
| 740 | cctx_T *cctx, |
| 741 | type_T *expected) |
| 742 | { |
| 743 | isn_T *isn; |
| 744 | |
| 745 | RETURN_OK_IF_SKIP(cctx); |
| 746 | if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL) |
| 747 | return FAIL; |
| 748 | isn->isn_arg.type.ct_type = alloc_type(expected); |
| 749 | return OK; |
| 750 | } |
| 751 | |
| 752 | /* |
Bram Moolenaar | c4e1b86 | 2023-02-26 18:58:23 +0000 | [diff] [blame] | 753 | * Generate an ISN_PUSHOBJ instruction. Object is always NULL. |
| 754 | */ |
Ernie Rael | 5c018be | 2023-08-27 18:40:26 +0200 | [diff] [blame] | 755 | int |
Bram Moolenaar | c4e1b86 | 2023-02-26 18:58:23 +0000 | [diff] [blame] | 756 | generate_PUSHOBJ(cctx_T *cctx) |
| 757 | { |
| 758 | RETURN_OK_IF_SKIP(cctx); |
Yegappan Lakshmanan | de8f8f7 | 2025-04-01 20:43:36 +0200 | [diff] [blame] | 759 | if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_object_any) == NULL) |
Bram Moolenaar | c4e1b86 | 2023-02-26 18:58:23 +0000 | [diff] [blame] | 760 | return FAIL; |
| 761 | return OK; |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | * Generate an ISN_PUSHCLASS instruction. "class" can be NULL. |
| 766 | */ |
| 767 | static int |
| 768 | generate_PUSHCLASS(cctx_T *cctx, class_T *class) |
| 769 | { |
| 770 | RETURN_OK_IF_SKIP(cctx); |
| 771 | isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS, |
| 772 | class == NULL ? &t_any : &class->class_type); |
| 773 | if (isn == NULL) |
| 774 | return FAIL; |
Bram Moolenaar | 30a8447 | 2023-02-27 08:07:14 +0000 | [diff] [blame] | 775 | isn->isn_arg.classarg = class; |
Bram Moolenaar | c4e1b86 | 2023-02-26 18:58:23 +0000 | [diff] [blame] | 776 | if (class != NULL) |
| 777 | ++class->class_refcount; |
| 778 | return OK; |
| 779 | } |
| 780 | |
| 781 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 782 | * Generate a PUSH instruction for "tv". |
| 783 | * "tv" will be consumed or cleared. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 784 | */ |
| 785 | int |
| 786 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 787 | { |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 788 | switch (tv->v_type) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 789 | { |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 790 | case VAR_BOOL: |
| 791 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 792 | break; |
| 793 | case VAR_SPECIAL: |
| 794 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 795 | break; |
| 796 | case VAR_NUMBER: |
| 797 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 798 | break; |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 799 | case VAR_FLOAT: |
| 800 | generate_PUSHF(cctx, tv->vval.v_float); |
| 801 | break; |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 802 | case VAR_BLOB: |
| 803 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 804 | tv->vval.v_blob = NULL; |
| 805 | break; |
| 806 | case VAR_LIST: |
| 807 | if (tv->vval.v_list != NULL) |
| 808 | iemsg("non-empty list constant not supported"); |
| 809 | generate_NEWLIST(cctx, 0, TRUE); |
| 810 | break; |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 811 | case VAR_TUPLE: |
| 812 | if (tv->vval.v_tuple != NULL) |
| 813 | iemsg("non-empty tuple constant not supported"); |
| 814 | generate_NEWTUPLE(cctx, 0, TRUE); |
| 815 | break; |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 816 | case VAR_DICT: |
| 817 | if (tv->vval.v_dict != NULL) |
| 818 | iemsg("non-empty dict constant not supported"); |
| 819 | generate_NEWDICT(cctx, 0, TRUE); |
| 820 | break; |
Bram Moolenaar | 8acb9cc | 2022-03-08 13:18:55 +0000 | [diff] [blame] | 821 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 822 | case VAR_JOB: |
| 823 | if (tv->vval.v_job != NULL) |
| 824 | iemsg("non-null job constant not supported"); |
| 825 | generate_PUSHJOB(cctx); |
| 826 | break; |
| 827 | case VAR_CHANNEL: |
| 828 | if (tv->vval.v_channel != NULL) |
| 829 | iemsg("non-null channel constant not supported"); |
| 830 | generate_PUSHCHANNEL(cctx); |
| 831 | break; |
Bram Moolenaar | 8acb9cc | 2022-03-08 13:18:55 +0000 | [diff] [blame] | 832 | #endif |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 833 | case VAR_FUNC: |
| 834 | if (tv->vval.v_string != NULL) |
| 835 | iemsg("non-null function constant not supported"); |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 836 | generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE); |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 837 | break; |
| 838 | case VAR_PARTIAL: |
| 839 | if (tv->vval.v_partial != NULL) |
| 840 | iemsg("non-null partial constant not supported"); |
| 841 | if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown) |
| 842 | == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 843 | return FAIL; |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 844 | break; |
| 845 | case VAR_STRING: |
| 846 | generate_PUSHS(cctx, &tv->vval.v_string); |
| 847 | tv->vval.v_string = NULL; |
| 848 | break; |
Bram Moolenaar | c4e1b86 | 2023-02-26 18:58:23 +0000 | [diff] [blame] | 849 | case VAR_OBJECT: |
| 850 | if (tv->vval.v_object != NULL) |
| 851 | { |
| 852 | emsg(_(e_cannot_use_non_null_object)); |
| 853 | return FAIL; |
| 854 | } |
| 855 | generate_PUSHOBJ(cctx); |
| 856 | break; |
| 857 | case VAR_CLASS: |
| 858 | generate_PUSHCLASS(cctx, tv->vval.v_class); |
| 859 | break; |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 860 | default: |
| 861 | siemsg("constant type %d not supported", tv->v_type); |
| 862 | clear_tv(tv); |
| 863 | return FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 864 | } |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 865 | tv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 866 | return OK; |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Generate an ISN_PUSHNR instruction. |
| 871 | */ |
| 872 | int |
| 873 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 874 | { |
| 875 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 876 | |
| 877 | RETURN_OK_IF_SKIP(cctx); |
| 878 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 879 | return FAIL; |
| 880 | isn->isn_arg.number = number; |
| 881 | |
| 882 | if (number == 0 || number == 1) |
| 883 | // A 0 or 1 number can also be used as a bool. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 884 | set_type_on_stack(cctx, &t_number_bool, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 885 | return OK; |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | * Generate an ISN_PUSHBOOL instruction. |
| 890 | */ |
| 891 | int |
| 892 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 893 | { |
| 894 | isn_T *isn; |
| 895 | |
| 896 | RETURN_OK_IF_SKIP(cctx); |
| 897 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 898 | return FAIL; |
| 899 | isn->isn_arg.number = number; |
| 900 | |
| 901 | return OK; |
| 902 | } |
| 903 | |
| 904 | /* |
| 905 | * Generate an ISN_PUSHSPEC instruction. |
| 906 | */ |
| 907 | int |
| 908 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 909 | { |
| 910 | isn_T *isn; |
| 911 | |
| 912 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 913 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, |
| 914 | number == VVAL_NULL ? &t_null : &t_none)) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 915 | return FAIL; |
| 916 | isn->isn_arg.number = number; |
| 917 | |
| 918 | return OK; |
| 919 | } |
| 920 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 921 | /* |
| 922 | * Generate an ISN_PUSHF instruction. |
| 923 | */ |
| 924 | int |
| 925 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 926 | { |
| 927 | isn_T *isn; |
| 928 | |
| 929 | RETURN_OK_IF_SKIP(cctx); |
| 930 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 931 | return FAIL; |
| 932 | isn->isn_arg.fnumber = fnumber; |
| 933 | |
| 934 | return OK; |
| 935 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 936 | |
| 937 | /* |
| 938 | * Generate an ISN_PUSHS instruction. |
| 939 | * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL. |
Bram Moolenaar | 0abc287 | 2022-05-10 13:24:30 +0100 | [diff] [blame] | 940 | * Note that if "str" is used in the instruction OK is returned and "*str" is |
| 941 | * not set to NULL. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 942 | */ |
| 943 | int |
| 944 | generate_PUSHS(cctx_T *cctx, char_u **str) |
| 945 | { |
| 946 | isn_T *isn; |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 947 | int ret = OK; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 948 | |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 949 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 950 | { |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 951 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 952 | ret = FAIL; |
| 953 | else |
| 954 | { |
| 955 | isn->isn_arg.string = str == NULL ? NULL : *str; |
| 956 | return OK; |
| 957 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 958 | } |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 959 | if (str != NULL) |
| 960 | VIM_CLEAR(*str); |
| 961 | return ret; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | /* |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 965 | * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 966 | */ |
| 967 | int |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 968 | generate_PUSHCHANNEL(cctx_T *cctx) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 969 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 970 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | c9af617 | 2022-05-04 16:46:54 +0100 | [diff] [blame] | 971 | #ifdef FEAT_JOB_CHANNEL |
Yegappan Lakshmanan | 6b085b9 | 2022-09-04 12:47:21 +0100 | [diff] [blame] | 972 | if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 973 | return FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 974 | return OK; |
Bram Moolenaar | c9af617 | 2022-05-04 16:46:54 +0100 | [diff] [blame] | 975 | #else |
| 976 | emsg(_(e_channel_job_feature_not_available)); |
| 977 | return FAIL; |
| 978 | #endif |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /* |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 982 | * Generate an ISN_PUSHJOB instruction. Job is always NULL. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 983 | */ |
| 984 | int |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 985 | generate_PUSHJOB(cctx_T *cctx) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 986 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 987 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | c9af617 | 2022-05-04 16:46:54 +0100 | [diff] [blame] | 988 | #ifdef FEAT_JOB_CHANNEL |
Yegappan Lakshmanan | 6b085b9 | 2022-09-04 12:47:21 +0100 | [diff] [blame] | 989 | if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 990 | return FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 991 | return OK; |
Bram Moolenaar | c9af617 | 2022-05-04 16:46:54 +0100 | [diff] [blame] | 992 | #else |
| 993 | emsg(_(e_channel_job_feature_not_available)); |
| 994 | return FAIL; |
| 995 | #endif |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Generate an ISN_PUSHBLOB instruction. |
| 1000 | * Consumes "blob". |
| 1001 | */ |
| 1002 | int |
| 1003 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1004 | { |
| 1005 | isn_T *isn; |
| 1006 | |
| 1007 | RETURN_OK_IF_SKIP(cctx); |
| 1008 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1009 | return FAIL; |
| 1010 | isn->isn_arg.blob = blob; |
| 1011 | |
| 1012 | return OK; |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * Generate an ISN_PUSHFUNC instruction with name "name". |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1017 | * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or |
| 1018 | * autoload. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1019 | */ |
| 1020 | int |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1021 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1022 | { |
| 1023 | isn_T *isn; |
| 1024 | char_u *funcname; |
| 1025 | |
| 1026 | RETURN_OK_IF_SKIP(cctx); |
| 1027 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
| 1028 | return FAIL; |
| 1029 | if (name == NULL) |
| 1030 | funcname = NULL; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 1031 | else if (!may_prefix |
| 1032 | || *name == K_SPECIAL // script-local |
Bram Moolenaar | d041f42 | 2022-01-12 19:54:00 +0000 | [diff] [blame] | 1033 | || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1034 | funcname = vim_strsave(name); |
| 1035 | else |
| 1036 | { |
| 1037 | funcname = alloc(STRLEN(name) + 3); |
| 1038 | if (funcname != NULL) |
| 1039 | { |
| 1040 | STRCPY(funcname, "g:"); |
| 1041 | STRCPY(funcname + 2, name); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | isn->isn_arg.string = funcname; |
| 1046 | return OK; |
| 1047 | } |
| 1048 | |
| 1049 | /* |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 1050 | * Generate an ISN_AUTOLOAD instruction. |
| 1051 | */ |
| 1052 | int |
| 1053 | generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type) |
| 1054 | { |
| 1055 | isn_T *isn; |
| 1056 | |
| 1057 | RETURN_OK_IF_SKIP(cctx); |
| 1058 | if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL) |
| 1059 | return FAIL; |
| 1060 | isn->isn_arg.string = vim_strsave(name); |
| 1061 | if (isn->isn_arg.string == NULL) |
| 1062 | return FAIL; |
| 1063 | return OK; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1067 | * Generate an ISN_GETITEM instruction with "index". |
| 1068 | * "with_op" is TRUE for "+=" and other operators, the stack has the current |
| 1069 | * value below the list with values. |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 1070 | * Caller must check the type is a list. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1071 | */ |
| 1072 | int |
| 1073 | generate_GETITEM(cctx_T *cctx, int index, int with_op) |
| 1074 | { |
| 1075 | isn_T *isn; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1076 | type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1077 | type_T *item_type = &t_any; |
| 1078 | |
| 1079 | RETURN_OK_IF_SKIP(cctx); |
| 1080 | |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 1081 | item_type = get_item_type(type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1082 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1083 | return FAIL; |
| 1084 | isn->isn_arg.getitem.gi_index = index; |
| 1085 | isn->isn_arg.getitem.gi_with_op = with_op; |
| 1086 | |
| 1087 | // add the item type to the type stack |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1088 | return push_type_stack(cctx, item_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | * Generate an ISN_SLICE instruction with "count". |
| 1093 | */ |
| 1094 | int |
| 1095 | generate_SLICE(cctx_T *cctx, int count) |
| 1096 | { |
| 1097 | isn_T *isn; |
| 1098 | |
| 1099 | RETURN_OK_IF_SKIP(cctx); |
| 1100 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1101 | return FAIL; |
| 1102 | isn->isn_arg.number = count; |
| 1103 | return OK; |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1108 | */ |
| 1109 | int |
| 1110 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1111 | { |
| 1112 | isn_T *isn; |
| 1113 | |
| 1114 | RETURN_OK_IF_SKIP(cctx); |
| 1115 | |
| 1116 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1117 | return FAIL; |
| 1118 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1119 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1120 | |
| 1121 | return OK; |
| 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | * Generate an ISN_STORE instruction. |
| 1126 | */ |
| 1127 | int |
| 1128 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1129 | { |
| 1130 | isn_T *isn; |
| 1131 | |
| 1132 | RETURN_OK_IF_SKIP(cctx); |
| 1133 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1134 | return FAIL; |
| 1135 | if (name != NULL) |
| 1136 | isn->isn_arg.string = vim_strsave(name); |
| 1137 | else |
| 1138 | isn->isn_arg.number = idx; |
| 1139 | |
| 1140 | return OK; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 1144 | * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER |
| 1145 | * ("load" == FALSE) instruction. |
| 1146 | */ |
| 1147 | int |
| 1148 | generate_CLASSMEMBER( |
| 1149 | cctx_T *cctx, |
| 1150 | int load, |
| 1151 | class_T *cl, |
| 1152 | int idx) |
| 1153 | { |
| 1154 | isn_T *isn; |
| 1155 | |
| 1156 | RETURN_OK_IF_SKIP(cctx); |
| 1157 | if (load) |
| 1158 | { |
| 1159 | ocmember_T *m = &cl->class_class_members[idx]; |
| 1160 | isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type); |
| 1161 | } |
| 1162 | else |
| 1163 | { |
| 1164 | isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1); |
| 1165 | } |
| 1166 | if (isn == NULL) |
| 1167 | return FAIL; |
| 1168 | isn->isn_arg.classmember.cm_class = cl; |
| 1169 | ++cl->class_refcount; |
| 1170 | isn->isn_arg.classmember.cm_idx = idx; |
| 1171 | |
| 1172 | return OK; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1176 | * Generate an ISN_STOREOUTER instruction. |
| 1177 | */ |
Yegappan Lakshmanan | 782b43d | 2022-01-08 18:43:40 +0000 | [diff] [blame] | 1178 | static int |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1179 | generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1180 | { |
| 1181 | isn_T *isn; |
| 1182 | |
| 1183 | RETURN_OK_IF_SKIP(cctx); |
| 1184 | if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL) |
| 1185 | return FAIL; |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1186 | if (level == 1 && loop_idx >= 0 && idx >= loop_idx) |
| 1187 | { |
| 1188 | // Store a variable defined in a loop. A copy will be made at the end |
| 1189 | // of the loop. TODO: how about deeper nesting? |
| 1190 | isn->isn_arg.outer.outer_idx = idx - loop_idx; |
| 1191 | isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH; |
| 1192 | } |
| 1193 | else |
| 1194 | { |
| 1195 | isn->isn_arg.outer.outer_idx = idx; |
| 1196 | isn->isn_arg.outer.outer_depth = level; |
| 1197 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1198 | |
| 1199 | return OK; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1204 | */ |
| 1205 | int |
| 1206 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1207 | { |
| 1208 | isn_T *isn; |
| 1209 | |
| 1210 | RETURN_OK_IF_SKIP(cctx); |
| 1211 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1212 | return FAIL; |
| 1213 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1214 | isn->isn_arg.storenr.stnr_val = value; |
| 1215 | |
| 1216 | return OK; |
| 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction |
| 1221 | */ |
Yegappan Lakshmanan | 782b43d | 2022-01-08 18:43:40 +0000 | [diff] [blame] | 1222 | static int |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1223 | generate_STOREOPT( |
| 1224 | cctx_T *cctx, |
| 1225 | isntype_T isn_type, |
| 1226 | char_u *name, |
| 1227 | int opt_flags) |
| 1228 | { |
| 1229 | isn_T *isn; |
| 1230 | |
| 1231 | RETURN_OK_IF_SKIP(cctx); |
| 1232 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1233 | return FAIL; |
| 1234 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1235 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1236 | |
| 1237 | return OK; |
| 1238 | } |
| 1239 | |
| 1240 | /* |
| 1241 | * Generate an ISN_LOAD or similar instruction. |
| 1242 | */ |
| 1243 | int |
| 1244 | generate_LOAD( |
| 1245 | cctx_T *cctx, |
| 1246 | isntype_T isn_type, |
| 1247 | int idx, |
| 1248 | char_u *name, |
| 1249 | type_T *type) |
| 1250 | { |
| 1251 | isn_T *isn; |
| 1252 | |
| 1253 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1254 | if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1255 | return FAIL; |
| 1256 | if (name != NULL) |
| 1257 | isn->isn_arg.string = vim_strsave(name); |
| 1258 | else |
| 1259 | isn->isn_arg.number = idx; |
| 1260 | |
| 1261 | return OK; |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | * Generate an ISN_LOADOUTER instruction |
| 1266 | */ |
| 1267 | int |
| 1268 | generate_LOADOUTER( |
| 1269 | cctx_T *cctx, |
| 1270 | int idx, |
| 1271 | int nesting, |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1272 | int loop_depth, |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1273 | int loop_idx, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1274 | type_T *type) |
| 1275 | { |
| 1276 | isn_T *isn; |
| 1277 | |
| 1278 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1279 | if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1280 | return FAIL; |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1281 | if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx) |
| 1282 | { |
| 1283 | // Load a variable defined in a loop. A copy will be made at the end |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1284 | // of the loop. |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1285 | isn->isn_arg.outer.outer_idx = idx - loop_idx; |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1286 | isn->isn_arg.outer.outer_depth = -loop_depth - 1; |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1287 | } |
| 1288 | else |
| 1289 | { |
| 1290 | isn->isn_arg.outer.outer_idx = idx; |
| 1291 | isn->isn_arg.outer.outer_depth = nesting; |
| 1292 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1293 | |
| 1294 | return OK; |
| 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * Generate an ISN_LOADV instruction for v:var. |
| 1299 | */ |
| 1300 | int |
| 1301 | generate_LOADV( |
| 1302 | cctx_T *cctx, |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 1303 | char_u *name) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1304 | { |
| 1305 | int di_flags; |
| 1306 | int vidx = find_vim_var(name, &di_flags); |
| 1307 | type_T *type; |
| 1308 | |
| 1309 | RETURN_OK_IF_SKIP(cctx); |
| 1310 | if (vidx < 0) |
| 1311 | { |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 1312 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1313 | return FAIL; |
| 1314 | } |
Bram Moolenaar | d787e40 | 2021-12-24 21:36:12 +0000 | [diff] [blame] | 1315 | type = get_vim_var_type(vidx, cctx->ctx_type_list); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1316 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
| 1317 | } |
| 1318 | |
| 1319 | /* |
| 1320 | * Generate an ISN_UNLET instruction. |
| 1321 | */ |
| 1322 | int |
| 1323 | generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit) |
| 1324 | { |
| 1325 | isn_T *isn; |
| 1326 | |
| 1327 | RETURN_OK_IF_SKIP(cctx); |
| 1328 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 1329 | return FAIL; |
| 1330 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1331 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1332 | |
| 1333 | return OK; |
| 1334 | } |
| 1335 | |
| 1336 | /* |
| 1337 | * Generate an ISN_LOCKCONST instruction. |
| 1338 | */ |
| 1339 | int |
| 1340 | generate_LOCKCONST(cctx_T *cctx) |
| 1341 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1342 | RETURN_OK_IF_SKIP(cctx); |
Yegappan Lakshmanan | 6b085b9 | 2022-09-04 12:47:21 +0100 | [diff] [blame] | 1343 | if (generate_instr(cctx, ISN_LOCKCONST) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1344 | return FAIL; |
| 1345 | return OK; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * Generate an ISN_LOADS instruction. |
| 1350 | */ |
| 1351 | int |
| 1352 | generate_OLDSCRIPT( |
| 1353 | cctx_T *cctx, |
| 1354 | isntype_T isn_type, |
| 1355 | char_u *name, |
| 1356 | int sid, |
| 1357 | type_T *type) |
| 1358 | { |
| 1359 | isn_T *isn; |
| 1360 | |
| 1361 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 1362 | if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1363 | isn = generate_instr_type(cctx, isn_type, type); |
| 1364 | else |
| 1365 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1366 | if (isn == NULL) |
| 1367 | return FAIL; |
| 1368 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1369 | isn->isn_arg.loadstore.ls_sid = sid; |
| 1370 | |
| 1371 | return OK; |
| 1372 | } |
| 1373 | |
| 1374 | /* |
| 1375 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1376 | */ |
| 1377 | int |
| 1378 | generate_VIM9SCRIPT( |
| 1379 | cctx_T *cctx, |
| 1380 | isntype_T isn_type, |
| 1381 | int sid, |
| 1382 | int idx, |
| 1383 | type_T *type) |
| 1384 | { |
| 1385 | isn_T *isn; |
| 1386 | scriptref_T *sref; |
| 1387 | scriptitem_T *si = SCRIPT_ITEM(sid); |
| 1388 | |
| 1389 | RETURN_OK_IF_SKIP(cctx); |
| 1390 | if (isn_type == ISN_LOADSCRIPT) |
Bram Moolenaar | 160afdb | 2022-02-06 17:17:02 +0000 | [diff] [blame] | 1391 | isn = generate_instr_type2(cctx, isn_type, type, type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1392 | else |
| 1393 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1394 | if (isn == NULL) |
| 1395 | return FAIL; |
| 1396 | |
| 1397 | // This requires three arguments, which doesn't fit in an instruction, thus |
| 1398 | // we need to allocate a struct for this. |
| 1399 | sref = ALLOC_ONE(scriptref_T); |
| 1400 | if (sref == NULL) |
| 1401 | return FAIL; |
| 1402 | isn->isn_arg.script.scriptref = sref; |
| 1403 | sref->sref_sid = sid; |
| 1404 | sref->sref_idx = idx; |
| 1405 | sref->sref_seq = si->sn_script_seq; |
| 1406 | sref->sref_type = type; |
| 1407 | return OK; |
| 1408 | } |
| 1409 | |
| 1410 | /* |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 1411 | * Generate an ISN_NEWLIST instruction for "count" items. |
| 1412 | * "use_null" is TRUE for null_list. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1413 | */ |
| 1414 | int |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 1415 | generate_NEWLIST(cctx_T *cctx, int count, int use_null) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1416 | { |
| 1417 | isn_T *isn; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1418 | type_T *member_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1419 | type_T *type; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1420 | type_T *decl_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1421 | |
| 1422 | RETURN_OK_IF_SKIP(cctx); |
| 1423 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1424 | return FAIL; |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 1425 | isn->isn_arg.number = use_null ? -1 : count; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1426 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1427 | // Get the member type and the declared member type from all the items on |
| 1428 | // the stack. |
Ernie Rael | fa83110 | 2023-12-14 20:06:39 +0100 | [diff] [blame] | 1429 | if ((member_type = get_member_type_from_stack(count, 1, cctx)) == NULL) |
| 1430 | return FAIL; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1431 | type = get_list_type(member_type, cctx->ctx_type_list); |
Bram Moolenaar | 2626d6a | 2022-02-06 15:49:35 +0000 | [diff] [blame] | 1432 | decl_type = get_list_type(&t_any, cctx->ctx_type_list); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1433 | |
| 1434 | // drop the value types |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1435 | cctx->ctx_type_stack.ga_len -= count; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1436 | |
| 1437 | // add the list type to the type stack |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1438 | return push_type_stack2(cctx, type, decl_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | /* |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 1442 | * Generate an ISN_NEWTUPLE instruction for "count" items. |
| 1443 | * "use_null" is TRUE for null_tuple. |
| 1444 | */ |
| 1445 | int |
| 1446 | generate_NEWTUPLE(cctx_T *cctx, int count, int use_null) |
| 1447 | { |
| 1448 | isn_T *isn; |
| 1449 | type_T *type; |
| 1450 | type_T *decl_type; |
| 1451 | |
| 1452 | RETURN_OK_IF_SKIP(cctx); |
| 1453 | if ((isn = generate_instr(cctx, ISN_NEWTUPLE)) == NULL) |
| 1454 | return FAIL; |
| 1455 | isn->isn_arg.number = use_null ? -1 : count; |
| 1456 | |
| 1457 | // Get the member type and the declared member type from all the items on |
| 1458 | // the stack. |
| 1459 | garray_T tuple_types_ga; |
| 1460 | ga_init2(&tuple_types_ga, sizeof(type_T *), 10); |
| 1461 | |
| 1462 | if (get_tuple_type_from_stack(count, &tuple_types_ga, cctx) < 0) |
| 1463 | { |
| 1464 | ga_clear(&tuple_types_ga); |
| 1465 | return FAIL; |
| 1466 | } |
| 1467 | |
| 1468 | type = get_tuple_type(&tuple_types_ga, cctx->ctx_type_list); |
| 1469 | decl_type = &t_tuple_any; |
| 1470 | |
| 1471 | ga_clear(&tuple_types_ga); |
| 1472 | |
| 1473 | // drop the value types |
| 1474 | cctx->ctx_type_stack.ga_len -= count; |
| 1475 | |
| 1476 | // add the tuple type to the type stack |
| 1477 | return push_type_stack2(cctx, type, decl_type); |
| 1478 | } |
| 1479 | |
| 1480 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1481 | * Generate an ISN_NEWDICT instruction. |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 1482 | * "use_null" is TRUE for null_dict. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1483 | */ |
| 1484 | int |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 1485 | generate_NEWDICT(cctx_T *cctx, int count, int use_null) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1486 | { |
| 1487 | isn_T *isn; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1488 | type_T *member_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1489 | type_T *type; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1490 | type_T *decl_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1491 | |
| 1492 | RETURN_OK_IF_SKIP(cctx); |
| 1493 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1494 | return FAIL; |
Bram Moolenaar | ec15b1c | 2022-03-27 16:29:53 +0100 | [diff] [blame] | 1495 | isn->isn_arg.number = use_null ? -1 : count; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1496 | |
Ernie Rael | fa83110 | 2023-12-14 20:06:39 +0100 | [diff] [blame] | 1497 | if ((member_type = get_member_type_from_stack(count, 2, cctx)) == NULL) |
| 1498 | return FAIL; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1499 | type = get_dict_type(member_type, cctx->ctx_type_list); |
Bram Moolenaar | 2626d6a | 2022-02-06 15:49:35 +0000 | [diff] [blame] | 1500 | decl_type = get_dict_type(&t_any, cctx->ctx_type_list); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1501 | |
| 1502 | // drop the key and value types |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1503 | cctx->ctx_type_stack.ga_len -= 2 * count; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1504 | |
| 1505 | // add the dict type to the type stack |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1506 | return push_type_stack2(cctx, type, decl_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | /* |
| 1510 | * Generate an ISN_FUNCREF instruction. |
Bram Moolenaar | 313e472 | 2023-02-08 20:55:27 +0000 | [diff] [blame] | 1511 | * For "obj.Method" "cl" is the class of the object (can be an interface or a |
| 1512 | * base class) and "fi" the index of the method on that class. |
Yegappan Lakshmanan | a76fbe6 | 2023-09-27 18:51:43 +0200 | [diff] [blame] | 1513 | * "isn_idx" is set to the index of the instruction, so that fr_dfunc_idx can |
| 1514 | * be set later. The index is used instead of a pointer to the instruction |
| 1515 | * because the instruction memory can be reallocated. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1516 | */ |
| 1517 | int |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1518 | generate_FUNCREF( |
| 1519 | cctx_T *cctx, |
| 1520 | ufunc_T *ufunc, |
Bram Moolenaar | 313e472 | 2023-02-08 20:55:27 +0000 | [diff] [blame] | 1521 | class_T *cl, |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 1522 | int object_method, |
Bram Moolenaar | 313e472 | 2023-02-08 20:55:27 +0000 | [diff] [blame] | 1523 | int fi, |
Yegappan Lakshmanan | a76fbe6 | 2023-09-27 18:51:43 +0200 | [diff] [blame] | 1524 | int *isn_idx) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1525 | { |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1526 | isn_T *isn; |
| 1527 | type_T *type; |
| 1528 | funcref_extra_T *extra; |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1529 | loopvarinfo_T loopinfo; |
| 1530 | int has_vars; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1531 | |
| 1532 | RETURN_OK_IF_SKIP(cctx); |
| 1533 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1534 | return FAIL; |
Yegappan Lakshmanan | a76fbe6 | 2023-09-27 18:51:43 +0200 | [diff] [blame] | 1535 | if (isn_idx != NULL) |
| 1536 | // save the index of the new instruction |
| 1537 | *isn_idx = cctx->ctx_instr.ga_len - 1; |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1538 | |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1539 | has_vars = get_loop_var_info(cctx, &loopinfo); |
Bram Moolenaar | 313e472 | 2023-02-08 20:55:27 +0000 | [diff] [blame] | 1540 | if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL) |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1541 | { |
| 1542 | extra = ALLOC_CLEAR_ONE(funcref_extra_T); |
| 1543 | if (extra == NULL) |
| 1544 | return FAIL; |
| 1545 | isn->isn_arg.funcref.fr_extra = extra; |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1546 | extra->fre_loopvar_info = loopinfo; |
Bram Moolenaar | 313e472 | 2023-02-08 20:55:27 +0000 | [diff] [blame] | 1547 | if (cl != NULL) |
| 1548 | { |
| 1549 | extra->fre_class = cl; |
| 1550 | ++cl->class_refcount; |
Yegappan Lakshmanan | 29bb67f | 2023-10-14 11:18:50 +0200 | [diff] [blame] | 1551 | extra->fre_object_method = object_method; |
Bram Moolenaar | 313e472 | 2023-02-08 20:55:27 +0000 | [diff] [blame] | 1552 | extra->fre_method_idx = fi; |
| 1553 | } |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1554 | } |
Bram Moolenaar | 313e472 | 2023-02-08 20:55:27 +0000 | [diff] [blame] | 1555 | if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL) |
John Marriott | b32800f | 2025-02-01 15:25:34 +0100 | [diff] [blame] | 1556 | extra->fre_func_name = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen); |
Bram Moolenaar | 313e472 | 2023-02-08 20:55:27 +0000 | [diff] [blame] | 1557 | if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL) |
Bram Moolenaar | 8dbab1d | 2023-01-27 20:14:02 +0000 | [diff] [blame] | 1558 | { |
Yegappan Lakshmanan | a76fbe6 | 2023-09-27 18:51:43 +0200 | [diff] [blame] | 1559 | if (isn_idx == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 8dbab1d | 2023-01-27 20:14:02 +0000 | [diff] [blame] | 1560 | // compile the function now, we need the uf_dfunc_idx value |
| 1561 | (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1562 | isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8dbab1d | 2023-01-27 20:14:02 +0000 | [diff] [blame] | 1563 | } |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1564 | |
| 1565 | // Reserve an extra variable to keep track of the number of closures |
| 1566 | // created. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1567 | cctx->ctx_has_closure = 1; |
| 1568 | |
| 1569 | // If the referenced function is a closure, it may use items further up in |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 1570 | // the nested context, including this one. But not a function defined at |
| 1571 | // the script level. |
| 1572 | if ((ufunc->uf_flags & FC_CLOSURE) |
| 1573 | && func_name_refcount(cctx->ctx_ufunc->uf_name)) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1574 | cctx->ctx_ufunc->uf_flags |= FC_CLOSURE; |
| 1575 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1576 | type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
| 1577 | return push_type_stack(cctx, type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | /* |
| 1581 | * Generate an ISN_NEWFUNC instruction. |
| 1582 | * "lambda_name" and "func_name" must be in allocated memory and will be |
| 1583 | * consumed. |
| 1584 | */ |
| 1585 | int |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1586 | generate_NEWFUNC( |
| 1587 | cctx_T *cctx, |
| 1588 | char_u *lambda_name, |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1589 | char_u *func_name) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1590 | { |
| 1591 | isn_T *isn; |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 1592 | int ret = OK; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1593 | |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 1594 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1595 | { |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 1596 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
| 1597 | ret = FAIL; |
| 1598 | else |
| 1599 | { |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1600 | newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T); |
| 1601 | |
| 1602 | if (arg == NULL) |
| 1603 | ret = FAIL; |
| 1604 | else |
| 1605 | { |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1606 | // Reserve an extra variable to keep track of the number of |
| 1607 | // closures created. |
| 1608 | cctx->ctx_has_closure = 1; |
| 1609 | |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1610 | isn->isn_arg.newfunc.nf_arg = arg; |
| 1611 | arg->nfa_lambda = lambda_name; |
| 1612 | arg->nfa_global = func_name; |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1613 | (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info); |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 1614 | return OK; |
| 1615 | } |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 1616 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1617 | } |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 1618 | vim_free(lambda_name); |
| 1619 | vim_free(func_name); |
| 1620 | return ret; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /* |
| 1624 | * Generate an ISN_DEF instruction: list functions |
| 1625 | */ |
| 1626 | int |
| 1627 | generate_DEF(cctx_T *cctx, char_u *name, size_t len) |
| 1628 | { |
| 1629 | isn_T *isn; |
| 1630 | |
| 1631 | RETURN_OK_IF_SKIP(cctx); |
| 1632 | if ((isn = generate_instr(cctx, ISN_DEF)) == NULL) |
| 1633 | return FAIL; |
| 1634 | if (len > 0) |
| 1635 | { |
| 1636 | isn->isn_arg.string = vim_strnsave(name, len); |
| 1637 | if (isn->isn_arg.string == NULL) |
| 1638 | return FAIL; |
| 1639 | } |
| 1640 | return OK; |
| 1641 | } |
| 1642 | |
| 1643 | /* |
| 1644 | * Generate an ISN_JUMP instruction. |
| 1645 | */ |
| 1646 | int |
| 1647 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1648 | { |
| 1649 | isn_T *isn; |
| 1650 | garray_T *stack = &cctx->ctx_type_stack; |
| 1651 | |
| 1652 | RETURN_OK_IF_SKIP(cctx); |
| 1653 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1654 | return FAIL; |
| 1655 | isn->isn_arg.jump.jump_when = when; |
| 1656 | isn->isn_arg.jump.jump_where = where; |
| 1657 | |
| 1658 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1659 | --stack->ga_len; |
| 1660 | |
| 1661 | return OK; |
| 1662 | } |
| 1663 | |
| 1664 | /* |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1665 | * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while |
| 1666 | */ |
| 1667 | int |
| 1668 | generate_WHILE(cctx_T *cctx, int funcref_idx) |
| 1669 | { |
| 1670 | isn_T *isn; |
| 1671 | garray_T *stack = &cctx->ctx_type_stack; |
| 1672 | |
| 1673 | RETURN_OK_IF_SKIP(cctx); |
| 1674 | if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL) |
| 1675 | return FAIL; |
| 1676 | isn->isn_arg.whileloop.while_funcref_idx = funcref_idx; |
| 1677 | isn->isn_arg.whileloop.while_end = 0; // filled in later |
| 1678 | |
| 1679 | if (stack->ga_len > 0) |
| 1680 | --stack->ga_len; |
| 1681 | |
| 1682 | return OK; |
| 1683 | } |
| 1684 | |
| 1685 | /* |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 1686 | * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1687 | */ |
| 1688 | int |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 1689 | generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1690 | { |
| 1691 | isn_T *isn; |
| 1692 | |
| 1693 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 1694 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1695 | return FAIL; |
| 1696 | isn->isn_arg.jumparg.jump_arg_off = arg_off; |
| 1697 | // jump_where is set later |
| 1698 | return OK; |
| 1699 | } |
| 1700 | |
| 1701 | int |
| 1702 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1703 | { |
| 1704 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1705 | |
| 1706 | RETURN_OK_IF_SKIP(cctx); |
| 1707 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1708 | return FAIL; |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1709 | isn->isn_arg.forloop.for_loop_idx = loop_idx; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1710 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1711 | // type doesn't matter, will be stored next |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1712 | return push_type_stack(cctx, &t_any); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1713 | } |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1714 | |
| 1715 | int |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1716 | generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info) |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1717 | { |
| 1718 | isn_T *isn; |
| 1719 | |
| 1720 | RETURN_OK_IF_SKIP(cctx); |
| 1721 | if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL) |
| 1722 | return FAIL; |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1723 | isn->isn_arg.endloop.end_depth = loop_info->li_depth; |
| 1724 | isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx; |
| 1725 | isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count; |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1726 | isn->isn_arg.endloop.end_var_count = |
Bram Moolenaar | cc34181 | 2022-09-19 15:54:34 +0100 | [diff] [blame] | 1727 | cctx->ctx_locals.ga_len - loop_info->li_local_count; |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 1728 | return OK; |
| 1729 | } |
| 1730 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1731 | /* |
| 1732 | * Generate an ISN_TRYCONT instruction. |
| 1733 | */ |
| 1734 | int |
| 1735 | generate_TRYCONT(cctx_T *cctx, int levels, int where) |
| 1736 | { |
| 1737 | isn_T *isn; |
| 1738 | |
| 1739 | RETURN_OK_IF_SKIP(cctx); |
| 1740 | if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL) |
| 1741 | return FAIL; |
| 1742 | isn->isn_arg.trycont.tct_levels = levels; |
| 1743 | isn->isn_arg.trycont.tct_where = where; |
| 1744 | |
| 1745 | return OK; |
| 1746 | } |
| 1747 | |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 1748 | /* |
| 1749 | * Check "argount" arguments and their types on the type stack. |
| 1750 | * Give an error and return FAIL if something is wrong. |
| 1751 | * When "method_call" is NULL no code is generated. |
| 1752 | */ |
| 1753 | int |
| 1754 | check_internal_func_args( |
| 1755 | cctx_T *cctx, |
| 1756 | int func_idx, |
| 1757 | int argcount, |
| 1758 | int method_call, |
| 1759 | type2_T **argtypes, |
| 1760 | type2_T *shuffled_argtypes) |
| 1761 | { |
| 1762 | garray_T *stack = &cctx->ctx_type_stack; |
| 1763 | int argoff = check_internal_func(func_idx, argcount); |
| 1764 | |
| 1765 | if (argoff < 0) |
| 1766 | return FAIL; |
| 1767 | |
| 1768 | if (method_call && argoff > 1) |
| 1769 | { |
Bram Moolenaar | b7f2270 | 2023-04-27 16:24:07 +0100 | [diff] [blame] | 1770 | if (argcount < argoff) |
| 1771 | { |
| 1772 | semsg(_(e_not_enough_arguments_for_function_str), |
| 1773 | internal_func_name(func_idx)); |
| 1774 | return FAIL; |
| 1775 | } |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 1776 | |
Bram Moolenaar | b7f2270 | 2023-04-27 16:24:07 +0100 | [diff] [blame] | 1777 | isn_T *isn = generate_instr(cctx, ISN_SHUFFLE); |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 1778 | if (isn == NULL) |
| 1779 | return FAIL; |
| 1780 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1781 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1782 | } |
| 1783 | |
| 1784 | if (argcount > 0) |
| 1785 | { |
| 1786 | type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount; |
| 1787 | |
| 1788 | // Check the types of the arguments. |
| 1789 | if (method_call && argoff > 1) |
| 1790 | { |
| 1791 | int i; |
| 1792 | |
| 1793 | for (i = 0; i < argcount; ++i) |
| 1794 | shuffled_argtypes[i] = (i < argoff - 1) |
| 1795 | ? typep[i + 1] |
| 1796 | : (i == argoff - 1) ? typep[0] : typep[i]; |
| 1797 | *argtypes = shuffled_argtypes; |
| 1798 | } |
| 1799 | else |
| 1800 | { |
| 1801 | int i; |
| 1802 | |
| 1803 | for (i = 0; i < argcount; ++i) |
| 1804 | shuffled_argtypes[i] = typep[i]; |
| 1805 | *argtypes = shuffled_argtypes; |
| 1806 | } |
| 1807 | if (internal_func_check_arg_types(*argtypes, func_idx, argcount, |
| 1808 | cctx) == FAIL) |
| 1809 | return FAIL; |
| 1810 | } |
| 1811 | return OK; |
| 1812 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1813 | |
| 1814 | /* |
| 1815 | * Generate an ISN_BCALL instruction. |
| 1816 | * "method_call" is TRUE for "value->method()" |
| 1817 | * Return FAIL if the number of arguments is wrong. |
| 1818 | */ |
| 1819 | int |
| 1820 | generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call) |
| 1821 | { |
| 1822 | isn_T *isn; |
| 1823 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1824 | type2_T *argtypes = NULL; |
| 1825 | type2_T shuffled_argtypes[MAX_FUNC_ARGS]; |
| 1826 | type2_T *maptype = NULL; |
| 1827 | type_T *type; |
Bram Moolenaar | 8133018 | 2022-02-01 12:11:58 +0000 | [diff] [blame] | 1828 | type_T *decl_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1829 | |
| 1830 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 1831 | |
| 1832 | if (check_internal_func_args(cctx, func_idx, argcount, method_call, |
| 1833 | &argtypes, shuffled_argtypes) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1834 | return FAIL; |
| 1835 | |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 1836 | if (internal_func_is_map(func_idx)) |
| 1837 | maptype = argtypes; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1838 | |
| 1839 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1840 | return FAIL; |
| 1841 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1842 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1843 | |
| 1844 | // Drop the argument types and push the return type. |
| 1845 | stack->ga_len -= argcount; |
Bram Moolenaar | 32517c4 | 2023-01-15 18:17:12 +0000 | [diff] [blame] | 1846 | type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type, |
| 1847 | cctx->ctx_type_list); |
Bram Moolenaar | 8133018 | 2022-02-01 12:11:58 +0000 | [diff] [blame] | 1848 | if (push_type_stack2(cctx, type, decl_type) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1849 | return FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1850 | |
Bram Moolenaar | 35c807d | 2022-01-27 16:36:29 +0000 | [diff] [blame] | 1851 | if (maptype != NULL && maptype[0].type_decl->tt_member != NULL |
| 1852 | && maptype[0].type_decl->tt_member != &t_any) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1853 | // Check that map() didn't change the item types. |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 1854 | generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1855 | |
| 1856 | return OK; |
| 1857 | } |
| 1858 | |
| 1859 | /* |
| 1860 | * Generate an ISN_LISTAPPEND instruction. Works like add(). |
| 1861 | * Argument count is already checked. |
| 1862 | */ |
| 1863 | int |
| 1864 | generate_LISTAPPEND(cctx_T *cctx) |
| 1865 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1866 | type_T *list_type; |
| 1867 | type_T *item_type; |
| 1868 | type_T *expected; |
| 1869 | |
| 1870 | // Caller already checked that list_type is a list. |
Bram Moolenaar | 8133018 | 2022-02-01 12:11:58 +0000 | [diff] [blame] | 1871 | // For checking the item type we use the declared type of the list and the |
| 1872 | // current type of the added item, adding a string to [1, 2] is OK. |
| 1873 | list_type = get_decl_type_on_stack(cctx, 1); |
Bram Moolenaar | fa10397 | 2022-09-29 19:14:42 +0100 | [diff] [blame] | 1874 | if (arg_type_modifiable(list_type, 1) == FAIL) |
| 1875 | return FAIL; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1876 | item_type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1877 | expected = list_type->tt_member; |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 1878 | if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1879 | return FAIL; |
| 1880 | |
| 1881 | if (generate_instr(cctx, ISN_LISTAPPEND) == NULL) |
| 1882 | return FAIL; |
| 1883 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1884 | --cctx->ctx_type_stack.ga_len; // drop the argument |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1885 | return OK; |
| 1886 | } |
| 1887 | |
| 1888 | /* |
| 1889 | * Generate an ISN_BLOBAPPEND instruction. Works like add(). |
| 1890 | * Argument count is already checked. |
| 1891 | */ |
| 1892 | int |
| 1893 | generate_BLOBAPPEND(cctx_T *cctx) |
| 1894 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1895 | type_T *item_type; |
| 1896 | |
Bram Moolenaar | fa10397 | 2022-09-29 19:14:42 +0100 | [diff] [blame] | 1897 | // Caller already checked that blob_type is a blob, check it is modifiable. |
| 1898 | if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL) |
| 1899 | return FAIL; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1900 | item_type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 1901 | if (need_type(item_type, &t_number, FALSE, |
| 1902 | -1, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1903 | return FAIL; |
| 1904 | |
| 1905 | if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL) |
| 1906 | return FAIL; |
| 1907 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1908 | --cctx->ctx_type_stack.ga_len; // drop the argument |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1909 | return OK; |
| 1910 | } |
| 1911 | |
| 1912 | /* |
Bram Moolenaar | d0200c8 | 2023-01-28 15:19:40 +0000 | [diff] [blame] | 1913 | * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction. |
| 1914 | * When calling a method on an object, of which we know the interface only, |
| 1915 | * then "cl" is the interface and "mi" the method index on the interface. |
Ernie Rael | 58c9579 | 2024-08-13 23:27:22 +0200 | [diff] [blame] | 1916 | * save is_super in the "isn->isn_arg"; it flags execution to use mfunc |
| 1917 | * directly to determine ufunc. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1918 | * Return FAIL if the number of arguments is wrong. |
| 1919 | */ |
| 1920 | int |
Bram Moolenaar | d0200c8 | 2023-01-28 15:19:40 +0000 | [diff] [blame] | 1921 | generate_CALL( |
| 1922 | cctx_T *cctx, |
| 1923 | ufunc_T *ufunc, |
| 1924 | class_T *cl, |
| 1925 | int mi, |
Ernie Rael | 58c9579 | 2024-08-13 23:27:22 +0200 | [diff] [blame] | 1926 | int pushed_argcount, |
| 1927 | int is_super) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1928 | { |
| 1929 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1930 | int regular_args = ufunc->uf_args.ga_len; |
| 1931 | int argcount = pushed_argcount; |
| 1932 | |
| 1933 | RETURN_OK_IF_SKIP(cctx); |
| 1934 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1935 | { |
| 1936 | semsg(_(e_too_many_arguments_for_function_str), |
| 1937 | printable_func_name(ufunc)); |
| 1938 | return FAIL; |
| 1939 | } |
| 1940 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1941 | { |
| 1942 | semsg(_(e_not_enough_arguments_for_function_str), |
| 1943 | printable_func_name(ufunc)); |
| 1944 | return FAIL; |
| 1945 | } |
| 1946 | |
| 1947 | if (ufunc->uf_def_status != UF_NOT_COMPILED |
| 1948 | && ufunc->uf_def_status != UF_COMPILE_ERROR) |
| 1949 | { |
| 1950 | int i; |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 1951 | compiletype_T compile_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1952 | |
| 1953 | for (i = 0; i < argcount; ++i) |
| 1954 | { |
| 1955 | type_T *expected; |
| 1956 | type_T *actual; |
| 1957 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1958 | actual = get_type_on_stack(cctx, argcount - i - 1); |
Ernie Rael | b077b58 | 2023-12-14 20:11:44 +0100 | [diff] [blame] | 1959 | if (check_type_is_value(actual) == FAIL) |
| 1960 | return FAIL; |
Bram Moolenaar | 53ba6ca | 2022-03-10 19:23:28 +0000 | [diff] [blame] | 1961 | if (actual->tt_type == VAR_SPECIAL |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1962 | && i >= regular_args - ufunc->uf_def_args.ga_len) |
| 1963 | { |
| 1964 | // assume v:none used for default argument value |
| 1965 | continue; |
| 1966 | } |
| 1967 | if (i < regular_args) |
| 1968 | { |
| 1969 | if (ufunc->uf_arg_types == NULL) |
| 1970 | continue; |
| 1971 | expected = ufunc->uf_arg_types[i]; |
| 1972 | } |
| 1973 | else if (ufunc->uf_va_type == NULL |
| 1974 | || ufunc->uf_va_type == &t_list_any) |
| 1975 | // possibly a lambda or "...: any" |
| 1976 | expected = &t_any; |
| 1977 | else |
| 1978 | expected = ufunc->uf_va_type->tt_member; |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 1979 | if (need_type(actual, expected, FALSE, |
| 1980 | -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1981 | { |
| 1982 | arg_type_mismatch(expected, actual, i + 1); |
| 1983 | return FAIL; |
| 1984 | } |
| 1985 | } |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 1986 | compile_type = get_compile_type(ufunc); |
| 1987 | if (func_needs_compiling(ufunc, compile_type) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1988 | && compile_def_function(ufunc, ufunc->uf_ret_type == NULL, |
Bram Moolenaar | 139575d | 2022-03-15 19:29:30 +0000 | [diff] [blame] | 1989 | compile_type, NULL) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1990 | return FAIL; |
| 1991 | } |
| 1992 | if (ufunc->uf_def_status == UF_COMPILE_ERROR) |
| 1993 | { |
Bram Moolenaar | a6c18d3 | 2022-03-31 20:02:56 +0100 | [diff] [blame] | 1994 | emsg_funcname(e_call_to_function_that_failed_to_compile_str, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1995 | ufunc->uf_name); |
| 1996 | return FAIL; |
| 1997 | } |
| 1998 | |
Bram Moolenaar | d0200c8 | 2023-01-28 15:19:40 +0000 | [diff] [blame] | 1999 | if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL |
| 2000 | : ufunc->uf_def_status != UF_NOT_COMPILED |
| 2001 | ? ISN_DCALL : ISN_UCALL)) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2002 | return FAIL; |
Bram Moolenaar | 836137d | 2023-01-29 14:11:24 +0000 | [diff] [blame] | 2003 | if (cl != NULL /* isn->isn_type == ISN_METHODCALL */) |
Bram Moolenaar | d0200c8 | 2023-01-28 15:19:40 +0000 | [diff] [blame] | 2004 | { |
| 2005 | isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T); |
| 2006 | if (isn->isn_arg.mfunc == NULL) |
| 2007 | return FAIL; |
| 2008 | isn->isn_arg.mfunc->cmf_itf = cl; |
| 2009 | ++cl->class_refcount; |
| 2010 | isn->isn_arg.mfunc->cmf_idx = mi; |
| 2011 | isn->isn_arg.mfunc->cmf_argcount = argcount; |
Ernie Rael | 58c9579 | 2024-08-13 23:27:22 +0200 | [diff] [blame] | 2012 | isn->isn_arg.mfunc->cmf_is_super = is_super; |
Bram Moolenaar | d0200c8 | 2023-01-28 15:19:40 +0000 | [diff] [blame] | 2013 | } |
| 2014 | else if (isn->isn_type == ISN_DCALL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2015 | { |
| 2016 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 2017 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 2018 | } |
| 2019 | else |
| 2020 | { |
| 2021 | // A user function may be deleted and redefined later, can't use the |
| 2022 | // ufunc pointer, need to look it up again at runtime. |
John Marriott | b32800f | 2025-02-01 15:25:34 +0100 | [diff] [blame] | 2023 | isn->isn_arg.ufunc.cuf_name = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2024 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 2025 | } |
| 2026 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2027 | // drop the argument types |
| 2028 | cctx->ctx_type_stack.ga_len -= argcount; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2029 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 2030 | // For an object or class method call, drop the object/class type. |
Yegappan Lakshmanan | 639751d | 2023-08-27 19:23:37 +0200 | [diff] [blame] | 2031 | if (ufunc->uf_class != NULL) |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 2032 | { |
| 2033 | // When a class method is called without the class name prefix, then |
| 2034 | // the type will not be in the stack. |
| 2035 | type_T *stype = get_type_on_stack(cctx, 0); |
| 2036 | if (stype->tt_type == VAR_CLASS || stype->tt_type == VAR_OBJECT) |
| 2037 | cctx->ctx_type_stack.ga_len--; |
| 2038 | } |
Yegappan Lakshmanan | 639751d | 2023-08-27 19:23:37 +0200 | [diff] [blame] | 2039 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2040 | // add return type |
| 2041 | return push_type_stack(cctx, ufunc->uf_ret_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | /* |
| 2045 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 2046 | */ |
| 2047 | int |
| 2048 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 2049 | { |
| 2050 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2051 | |
| 2052 | RETURN_OK_IF_SKIP(cctx); |
| 2053 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 2054 | return FAIL; |
| 2055 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 2056 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 2057 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2058 | // drop the argument types |
| 2059 | cctx->ctx_type_stack.ga_len -= argcount; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2060 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2061 | // add return value |
| 2062 | return push_type_stack(cctx, &t_any); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | /* |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 2066 | * Check the arguments of function "type" against the types on the stack. |
| 2067 | * Returns OK or FAIL; |
| 2068 | */ |
| 2069 | int |
| 2070 | check_func_args_from_type( |
| 2071 | cctx_T *cctx, |
| 2072 | type_T *type, |
| 2073 | int argcount, |
| 2074 | int at_top, |
| 2075 | char_u *name) |
| 2076 | { |
| 2077 | if (type->tt_argcount != -1) |
| 2078 | { |
| 2079 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 2080 | |
| 2081 | if (argcount < type->tt_min_argcount - varargs) |
| 2082 | { |
| 2083 | emsg_funcname(e_not_enough_arguments_for_function_str, name); |
| 2084 | return FAIL; |
| 2085 | } |
| 2086 | if (!varargs && argcount > type->tt_argcount) |
| 2087 | { |
| 2088 | emsg_funcname(e_too_many_arguments_for_function_str, name); |
| 2089 | return FAIL; |
| 2090 | } |
| 2091 | if (type->tt_args != NULL) |
| 2092 | { |
| 2093 | int i; |
| 2094 | |
| 2095 | for (i = 0; i < argcount; ++i) |
| 2096 | { |
| 2097 | int offset = -argcount + i - (at_top ? 0 : 1); |
| 2098 | type_T *actual = get_type_on_stack(cctx, -1 - offset); |
| 2099 | type_T *expected; |
| 2100 | |
Ernie Rael | b077b58 | 2023-12-14 20:11:44 +0100 | [diff] [blame] | 2101 | if (check_type_is_value(actual) == FAIL) |
| 2102 | return FAIL; |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 2103 | if (varargs && i >= type->tt_argcount - 1) |
Bram Moolenaar | 36818a9 | 2023-01-03 12:33:26 +0000 | [diff] [blame] | 2104 | { |
| 2105 | expected = type->tt_args[type->tt_argcount - 1]; |
| 2106 | if (expected != NULL && expected->tt_type == VAR_LIST) |
| 2107 | expected = expected->tt_member; |
| 2108 | if (expected == NULL) |
| 2109 | expected = &t_any; |
| 2110 | } |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 2111 | else if (i >= type->tt_min_argcount |
| 2112 | && actual->tt_type == VAR_SPECIAL) |
| 2113 | expected = &t_any; |
| 2114 | else |
| 2115 | expected = type->tt_args[i]; |
Bram Moolenaar | c6951a7 | 2022-12-29 20:56:24 +0000 | [diff] [blame] | 2116 | if (need_type(actual, expected, FALSE, |
| 2117 | offset, i + 1, cctx, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 2118 | { |
| 2119 | arg_type_mismatch(expected, actual, i + 1); |
| 2120 | return FAIL; |
| 2121 | } |
| 2122 | } |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | return OK; |
| 2127 | } |
| 2128 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2129 | * Generate an ISN_PCALL instruction. |
| 2130 | * "type" is the type of the FuncRef. |
| 2131 | */ |
| 2132 | int |
| 2133 | generate_PCALL( |
| 2134 | cctx_T *cctx, |
| 2135 | int argcount, |
| 2136 | char_u *name, |
| 2137 | type_T *type, |
| 2138 | int at_top) |
| 2139 | { |
| 2140 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2141 | type_T *ret_type; |
| 2142 | |
| 2143 | RETURN_OK_IF_SKIP(cctx); |
| 2144 | |
Yegappan Lakshmanan | de8f8f7 | 2025-04-01 20:43:36 +0200 | [diff] [blame] | 2145 | if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN |
| 2146 | || type == &t_object_any) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2147 | ret_type = &t_any; |
| 2148 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
| 2149 | { |
Bram Moolenaar | c4e1b86 | 2023-02-26 18:58:23 +0000 | [diff] [blame] | 2150 | if (check_func_args_from_type(cctx, type, argcount, at_top, name) |
| 2151 | == FAIL) |
Bram Moolenaar | 1690032 | 2022-09-08 19:51:45 +0100 | [diff] [blame] | 2152 | return FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2153 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2154 | ret_type = type->tt_member; |
| 2155 | if (ret_type == &t_unknown) |
| 2156 | // return type not known yet, use a runtime check |
| 2157 | ret_type = &t_any; |
| 2158 | } |
| 2159 | else |
| 2160 | { |
| 2161 | semsg(_(e_not_callable_type_str), name); |
| 2162 | return FAIL; |
| 2163 | } |
| 2164 | |
| 2165 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 2166 | return FAIL; |
| 2167 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 2168 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 2169 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2170 | // drop the arguments and the funcref/partial |
| 2171 | cctx->ctx_type_stack.ga_len -= argcount + 1; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2172 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2173 | // push the return value |
| 2174 | push_type_stack(cctx, ret_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2175 | |
| 2176 | // If partial is above the arguments it must be cleared and replaced with |
| 2177 | // the return value. |
| 2178 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 2179 | return FAIL; |
| 2180 | |
| 2181 | return OK; |
| 2182 | } |
| 2183 | |
| 2184 | /* |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 2185 | * Generate an ISN_DEFER instruction. |
| 2186 | */ |
| 2187 | int |
Yegappan Lakshmanan | f3eac69 | 2023-10-17 11:00:45 +0200 | [diff] [blame] | 2188 | generate_DEFER(cctx_T *cctx, int var_idx, int argcount) |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 2189 | { |
| 2190 | isn_T *isn; |
| 2191 | |
| 2192 | RETURN_OK_IF_SKIP(cctx); |
Yegappan Lakshmanan | f3eac69 | 2023-10-17 11:00:45 +0200 | [diff] [blame] | 2193 | if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL) |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 2194 | return FAIL; |
| 2195 | isn->isn_arg.defer.defer_var_idx = var_idx; |
| 2196 | isn->isn_arg.defer.defer_argcount = argcount; |
| 2197 | return OK; |
| 2198 | } |
| 2199 | |
| 2200 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2201 | * Generate an ISN_STRINGMEMBER instruction. |
| 2202 | */ |
| 2203 | int |
| 2204 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
| 2205 | { |
| 2206 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2207 | type_T *type; |
| 2208 | |
| 2209 | RETURN_OK_IF_SKIP(cctx); |
| 2210 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
| 2211 | return FAIL; |
| 2212 | isn->isn_arg.string = vim_strnsave(name, len); |
| 2213 | |
| 2214 | // check for dict type |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2215 | type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | 0089ce2 | 2022-10-08 14:39:36 +0100 | [diff] [blame] | 2216 | if (type->tt_type != VAR_DICT |
Yegappan Lakshmanan | de8f8f7 | 2025-04-01 20:43:36 +0200 | [diff] [blame] | 2217 | && type->tt_type != VAR_OBJECT |
| 2218 | && type->tt_type != VAR_ANY |
| 2219 | && type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2220 | { |
| 2221 | char *tofree; |
| 2222 | |
| 2223 | semsg(_(e_expected_dictionary_for_using_key_str_but_got_str), |
| 2224 | name, type_name(type, &tofree)); |
| 2225 | vim_free(tofree); |
| 2226 | return FAIL; |
| 2227 | } |
| 2228 | // change dict type to dict member type |
| 2229 | if (type->tt_type == VAR_DICT) |
| 2230 | { |
Bram Moolenaar | 0089ce2 | 2022-10-08 14:39:36 +0100 | [diff] [blame] | 2231 | type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2232 | ? &t_any : type->tt_member; |
| 2233 | set_type_on_stack(cctx, ntype, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | return OK; |
| 2237 | } |
| 2238 | |
| 2239 | /* |
| 2240 | * Generate an ISN_ECHO instruction. |
| 2241 | */ |
| 2242 | int |
| 2243 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 2244 | { |
| 2245 | isn_T *isn; |
| 2246 | |
| 2247 | RETURN_OK_IF_SKIP(cctx); |
| 2248 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 2249 | return FAIL; |
| 2250 | isn->isn_arg.echo.echo_with_white = with_white; |
| 2251 | isn->isn_arg.echo.echo_count = count; |
| 2252 | |
| 2253 | return OK; |
| 2254 | } |
| 2255 | |
| 2256 | /* |
| 2257 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
| 2258 | */ |
| 2259 | int |
| 2260 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
| 2261 | { |
| 2262 | isn_T *isn; |
| 2263 | |
Bram Moolenaar | 2eae3d2 | 2022-10-07 15:09:27 +0100 | [diff] [blame] | 2264 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2265 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
| 2266 | return FAIL; |
| 2267 | isn->isn_arg.number = count; |
Bram Moolenaar | bdc09a1 | 2022-10-07 14:31:45 +0100 | [diff] [blame] | 2268 | return OK; |
| 2269 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2270 | |
Bram Moolenaar | bdc09a1 | 2022-10-07 14:31:45 +0100 | [diff] [blame] | 2271 | /* |
| 2272 | * Generate an ISN_ECHOWINDOW instruction |
| 2273 | */ |
| 2274 | int |
| 2275 | generate_ECHOWINDOW(cctx_T *cctx, int count, long time) |
| 2276 | { |
| 2277 | isn_T *isn; |
| 2278 | |
| 2279 | if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL) |
| 2280 | return FAIL; |
| 2281 | isn->isn_arg.echowin.ewin_count = count; |
| 2282 | isn->isn_arg.echowin.ewin_time = time; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2283 | return OK; |
| 2284 | } |
| 2285 | |
| 2286 | /* |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 2287 | * Generate an ISN_SOURCE instruction. |
| 2288 | */ |
| 2289 | int |
| 2290 | generate_SOURCE(cctx_T *cctx, int sid) |
| 2291 | { |
| 2292 | isn_T *isn; |
| 2293 | |
| 2294 | if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL) |
| 2295 | return FAIL; |
| 2296 | isn->isn_arg.number = sid; |
| 2297 | |
| 2298 | return OK; |
| 2299 | } |
| 2300 | |
| 2301 | /* |
64-bitman | e08f10a | 2025-03-18 22:14:34 +0100 | [diff] [blame] | 2302 | * Generate an ISN_PUT or ISN_IPUT instruction depending on fixindent. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2303 | */ |
| 2304 | int |
64-bitman | e08f10a | 2025-03-18 22:14:34 +0100 | [diff] [blame] | 2305 | generate_PUT(cctx_T *cctx, int regname, linenr_T lnum, int fixindent) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2306 | { |
| 2307 | isn_T *isn; |
| 2308 | |
| 2309 | RETURN_OK_IF_SKIP(cctx); |
64-bitman | e08f10a | 2025-03-18 22:14:34 +0100 | [diff] [blame] | 2310 | isn = (fixindent) ? generate_instr(cctx, ISN_IPUT) : |
| 2311 | generate_instr(cctx, ISN_PUT); |
| 2312 | if (isn == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2313 | return FAIL; |
| 2314 | isn->isn_arg.put.put_regname = regname; |
| 2315 | isn->isn_arg.put.put_lnum = lnum; |
| 2316 | return OK; |
| 2317 | } |
| 2318 | |
| 2319 | /* |
Ernie Rael | 6488564 | 2023-10-04 20:16:22 +0200 | [diff] [blame] | 2320 | * Generate a LOCKUNLOCK instruction.The root item, where the indexing starts |
| 2321 | * to find the variable, is on the stack. The instr takes |
| 2322 | * - the string to parse, "root.b[idx1][idx2].d.val", to find the variable |
| 2323 | * - the class, if any, in which the string executes. |
| 2324 | * - if the root item is a function argument |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2325 | * A copy is made of "line". |
| 2326 | */ |
| 2327 | int |
Ernie Rael | ee865f3 | 2023-09-29 19:53:55 +0200 | [diff] [blame] | 2328 | generate_LOCKUNLOCK(cctx_T *cctx, char_u *line, int is_arg) |
| 2329 | { |
| 2330 | isn_T *isn; |
| 2331 | |
| 2332 | RETURN_OK_IF_SKIP(cctx); |
| 2333 | if ((isn = generate_instr(cctx, ISN_LOCKUNLOCK)) == NULL) |
| 2334 | return FAIL; |
Ernie Rael | 6488564 | 2023-10-04 20:16:22 +0200 | [diff] [blame] | 2335 | class_T *cl = cctx->ctx_ufunc != NULL ? cctx->ctx_ufunc->uf_class : NULL; |
| 2336 | isn->isn_arg.lockunlock.lu_string = vim_strsave(line); |
| 2337 | isn->isn_arg.lockunlock.lu_cl_exec = cl; |
| 2338 | if (cl != NULL) |
| 2339 | ++cl->class_refcount; |
| 2340 | isn->isn_arg.lockunlock.lu_is_arg = is_arg; |
Ernie Rael | ee865f3 | 2023-09-29 19:53:55 +0200 | [diff] [blame] | 2341 | return OK; |
| 2342 | } |
| 2343 | |
| 2344 | /* |
| 2345 | * Generate an EXEC instruction that takes a string argument. |
| 2346 | * A copy is made of "line". |
| 2347 | */ |
| 2348 | int |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2349 | generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line) |
| 2350 | { |
| 2351 | isn_T *isn; |
| 2352 | |
| 2353 | RETURN_OK_IF_SKIP(cctx); |
| 2354 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 2355 | return FAIL; |
| 2356 | isn->isn_arg.string = vim_strsave(line); |
| 2357 | return OK; |
| 2358 | } |
| 2359 | |
| 2360 | /* |
| 2361 | * Generate an EXEC instruction that takes a string argument. |
| 2362 | * "str" must be allocated, it is consumed. |
| 2363 | */ |
| 2364 | int |
| 2365 | generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str) |
| 2366 | { |
| 2367 | isn_T *isn; |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 2368 | int ret = OK; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2369 | |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 2370 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2371 | { |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 2372 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 2373 | ret = FAIL; |
| 2374 | else |
| 2375 | { |
| 2376 | isn->isn_arg.string = str; |
| 2377 | return OK; |
| 2378 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2379 | } |
Bram Moolenaar | c3caa7f | 2022-05-25 19:15:10 +0100 | [diff] [blame] | 2380 | vim_free(str); |
| 2381 | return ret; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | int |
| 2385 | generate_LEGACY_EVAL(cctx_T *cctx, char_u *line) |
| 2386 | { |
| 2387 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2388 | |
| 2389 | RETURN_OK_IF_SKIP(cctx); |
| 2390 | if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL) |
| 2391 | return FAIL; |
| 2392 | isn->isn_arg.string = vim_strsave(line); |
| 2393 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2394 | return push_type_stack(cctx, &t_any); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | int |
| 2398 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 2399 | { |
| 2400 | isn_T *isn; |
| 2401 | |
| 2402 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 2403 | return FAIL; |
| 2404 | isn->isn_arg.number = count; |
| 2405 | return OK; |
| 2406 | } |
| 2407 | |
| 2408 | /* |
| 2409 | * Generate ISN_RANGE. Consumes "range". Return OK/FAIL. |
| 2410 | */ |
| 2411 | int |
| 2412 | generate_RANGE(cctx_T *cctx, char_u *range) |
| 2413 | { |
| 2414 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2415 | |
| 2416 | if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL) |
| 2417 | return FAIL; |
| 2418 | isn->isn_arg.string = range; |
| 2419 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2420 | return push_type_stack(cctx, &t_number); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
| 2423 | int |
| 2424 | generate_UNPACK(cctx_T *cctx, int var_count, int semicolon) |
| 2425 | { |
| 2426 | isn_T *isn; |
| 2427 | |
| 2428 | RETURN_OK_IF_SKIP(cctx); |
| 2429 | if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL) |
| 2430 | return FAIL; |
| 2431 | isn->isn_arg.unpack.unp_count = var_count; |
| 2432 | isn->isn_arg.unpack.unp_semicolon = semicolon; |
| 2433 | return OK; |
| 2434 | } |
| 2435 | |
| 2436 | /* |
| 2437 | * Generate an instruction for any command modifiers. |
| 2438 | */ |
| 2439 | int |
| 2440 | generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod) |
| 2441 | { |
| 2442 | isn_T *isn; |
| 2443 | |
| 2444 | if (has_cmdmod(cmod, FALSE)) |
| 2445 | { |
| 2446 | cctx->ctx_has_cmdmod = TRUE; |
| 2447 | |
| 2448 | if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL) |
| 2449 | return FAIL; |
| 2450 | isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T); |
| 2451 | if (isn->isn_arg.cmdmod.cf_cmdmod == NULL) |
| 2452 | return FAIL; |
| 2453 | mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T)); |
| 2454 | // filter program now belongs to the instruction |
| 2455 | cmod->cmod_filter_regmatch.regprog = NULL; |
| 2456 | } |
| 2457 | |
| 2458 | return OK; |
| 2459 | } |
| 2460 | |
| 2461 | int |
| 2462 | generate_undo_cmdmods(cctx_T *cctx) |
| 2463 | { |
| 2464 | if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL) |
| 2465 | return FAIL; |
| 2466 | cctx->ctx_has_cmdmod = FALSE; |
| 2467 | return OK; |
| 2468 | } |
| 2469 | |
| 2470 | /* |
| 2471 | * Generate a STORE instruction for "dest", not being "dest_local". |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 2472 | * "lhs" might be NULL. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2473 | * Return FAIL when out of memory. |
| 2474 | */ |
| 2475 | int |
| 2476 | generate_store_var( |
| 2477 | cctx_T *cctx, |
| 2478 | assign_dest_T dest, |
| 2479 | int opt_flags, |
| 2480 | int vimvaridx, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2481 | type_T *type, |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 2482 | char_u *name, |
| 2483 | lhs_T *lhs) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2484 | { |
| 2485 | switch (dest) |
| 2486 | { |
| 2487 | case dest_option: |
| 2488 | return generate_STOREOPT(cctx, ISN_STOREOPT, |
| 2489 | skip_option_env_lead(name), opt_flags); |
| 2490 | case dest_func_option: |
| 2491 | return generate_STOREOPT(cctx, ISN_STOREFUNCOPT, |
| 2492 | skip_option_env_lead(name), opt_flags); |
| 2493 | case dest_global: |
| 2494 | // include g: with the name, easier to execute that way |
| 2495 | return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL |
| 2496 | ? ISN_STOREG : ISN_STOREAUTO, 0, name); |
| 2497 | case dest_buffer: |
| 2498 | // include b: with the name, easier to execute that way |
| 2499 | return generate_STORE(cctx, ISN_STOREB, 0, name); |
| 2500 | case dest_window: |
| 2501 | // include w: with the name, easier to execute that way |
| 2502 | return generate_STORE(cctx, ISN_STOREW, 0, name); |
| 2503 | case dest_tab: |
| 2504 | // include t: with the name, easier to execute that way |
| 2505 | return generate_STORE(cctx, ISN_STORET, 0, name); |
| 2506 | case dest_env: |
| 2507 | return generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 2508 | case dest_reg: |
| 2509 | return generate_STORE(cctx, ISN_STOREREG, |
| 2510 | name[1] == '@' ? '"' : name[1], NULL); |
| 2511 | case dest_vimvar: |
| 2512 | return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 2513 | case dest_script: |
Ernie Rael | 3f821d6 | 2024-04-24 20:07:50 +0200 | [diff] [blame] | 2514 | case dest_script_v9: |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 2515 | { |
Bram Moolenaar | c336ae3 | 2022-12-18 22:01:42 +0000 | [diff] [blame] | 2516 | int scriptvar_idx = lhs->lhs_scriptvar_idx; |
| 2517 | int scriptvar_sid = lhs->lhs_scriptvar_sid; |
| 2518 | if (scriptvar_idx < 0) |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 2519 | { |
Bram Moolenaar | c336ae3 | 2022-12-18 22:01:42 +0000 | [diff] [blame] | 2520 | isntype_T isn_type = ISN_STORES; |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 2521 | |
Ernie Rael | 3f821d6 | 2024-04-24 20:07:50 +0200 | [diff] [blame] | 2522 | // If "sn_import_autoload", generate ISN_STOREEXPORT (not |
| 2523 | // ISN_STORES) if destination is in a vim9script or if |
| 2524 | // there is no "sn_autoload_prefix". |
Bram Moolenaar | c336ae3 | 2022-12-18 22:01:42 +0000 | [diff] [blame] | 2525 | if (SCRIPT_ID_VALID(scriptvar_sid) |
| 2526 | && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload |
Ernie Rael | 3f821d6 | 2024-04-24 20:07:50 +0200 | [diff] [blame] | 2527 | && ((SCRIPT_ITEM(scriptvar_sid) |
| 2528 | ->sn_autoload_prefix == NULL) |
| 2529 | || lhs->lhs_dest == dest_script_v9)) |
Bram Moolenaar | c336ae3 | 2022-12-18 22:01:42 +0000 | [diff] [blame] | 2530 | { |
| 2531 | // "import autoload './dir/script.vim'" - load script |
| 2532 | // first |
| 2533 | if (generate_SOURCE(cctx, scriptvar_sid) == FAIL) |
| 2534 | return FAIL; |
| 2535 | isn_type = ISN_STOREEXPORT; |
| 2536 | } |
| 2537 | |
| 2538 | // "s:" may be included in the name. |
| 2539 | return generate_OLDSCRIPT(cctx, isn_type, name, |
| 2540 | scriptvar_sid, type); |
| 2541 | } |
| 2542 | return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2543 | scriptvar_sid, scriptvar_idx, type); |
Bram Moolenaar | c336ae3 | 2022-12-18 22:01:42 +0000 | [diff] [blame] | 2544 | } |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 2545 | case dest_class_member: |
| 2546 | return generate_CLASSMEMBER(cctx, FALSE, |
| 2547 | lhs->lhs_class, lhs->lhs_classmember_idx); |
| 2548 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2549 | case dest_local: |
| 2550 | case dest_expr: |
| 2551 | // cannot happen |
| 2552 | break; |
| 2553 | } |
| 2554 | return FAIL; |
| 2555 | } |
| 2556 | |
Bram Moolenaar | 38ecd97 | 2022-01-15 21:44:44 +0000 | [diff] [blame] | 2557 | /* |
| 2558 | * Return TRUE when inside a "for" or "while" loop. |
| 2559 | */ |
| 2560 | int |
| 2561 | inside_loop_scope(cctx_T *cctx) |
| 2562 | { |
| 2563 | scope_T *scope = cctx->ctx_scope; |
| 2564 | |
| 2565 | for (;;) |
| 2566 | { |
| 2567 | if (scope == NULL) |
| 2568 | break; |
| 2569 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 2570 | return TRUE; |
| 2571 | scope = scope->se_outer; |
| 2572 | } |
| 2573 | return FALSE; |
| 2574 | } |
| 2575 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2576 | int |
Bram Moolenaar | 5cd6479 | 2021-12-25 18:23:24 +0000 | [diff] [blame] | 2577 | generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2578 | { |
| 2579 | if (lhs->lhs_dest != dest_local) |
| 2580 | return generate_store_var(cctx, lhs->lhs_dest, |
| 2581 | lhs->lhs_opt_flags, lhs->lhs_vimvaridx, |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 2582 | lhs->lhs_type, lhs->lhs_name, lhs); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2583 | |
Yegappan Lakshmanan | 142ed77 | 2023-01-26 12:00:00 +0000 | [diff] [blame] | 2584 | if (lhs->lhs_lvar == NULL) |
| 2585 | return OK; |
| 2586 | |
| 2587 | garray_T *instr = &cctx->ctx_instr; |
| 2588 | isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2589 | |
| 2590 | // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into |
| 2591 | // ISN_STORENR. |
| 2592 | // And "var = 0" does not need any instruction. |
| 2593 | if (lhs->lhs_lvar->lv_from_outer == 0 |
| 2594 | && instr->ga_len == instr_count + 1 |
| 2595 | && isn->isn_type == ISN_PUSHNR) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2596 | { |
Yegappan Lakshmanan | 142ed77 | 2023-01-26 12:00:00 +0000 | [diff] [blame] | 2597 | varnumber_T val = isn->isn_arg.number; |
| 2598 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2599 | |
Yegappan Lakshmanan | 142ed77 | 2023-01-26 12:00:00 +0000 | [diff] [blame] | 2600 | if (val == 0 && is_decl && !inside_loop_scope(cctx)) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2601 | { |
Yegappan Lakshmanan | 142ed77 | 2023-01-26 12:00:00 +0000 | [diff] [blame] | 2602 | // zero is the default value, no need to do anything |
| 2603 | --instr->ga_len; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2604 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2605 | else |
Yegappan Lakshmanan | 142ed77 | 2023-01-26 12:00:00 +0000 | [diff] [blame] | 2606 | { |
| 2607 | isn->isn_type = ISN_STORENR; |
| 2608 | isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx; |
| 2609 | isn->isn_arg.storenr.stnr_val = val; |
| 2610 | } |
| 2611 | if (stack->ga_len > 0) |
| 2612 | --stack->ga_len; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2613 | } |
Yegappan Lakshmanan | 142ed77 | 2023-01-26 12:00:00 +0000 | [diff] [blame] | 2614 | else if (lhs->lhs_lvar->lv_from_outer > 0) |
| 2615 | generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx, |
| 2616 | lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx); |
| 2617 | else |
| 2618 | generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2619 | return OK; |
| 2620 | } |
| 2621 | |
Yegappan Lakshmanan | 16f2d3a | 2025-02-24 19:23:43 +0100 | [diff] [blame] | 2622 | /* |
| 2623 | * Generate instruction to set the script context. Used to evaluate an |
| 2624 | * object member variable initialization expression in the context of the |
| 2625 | * script where the class is defined. |
| 2626 | */ |
| 2627 | int |
| 2628 | generate_SCRIPTCTX_SET(cctx_T *cctx, sctx_T new_sctx) |
| 2629 | { |
| 2630 | isn_T *isn; |
| 2631 | |
| 2632 | RETURN_OK_IF_SKIP(cctx); |
| 2633 | if ((isn = generate_instr(cctx, ISN_SCRIPTCTX_SET)) == NULL) |
| 2634 | return FAIL; |
| 2635 | isn->isn_arg.setsctx = new_sctx; |
| 2636 | return OK; |
| 2637 | } |
| 2638 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2639 | #if defined(FEAT_PROFILE) || defined(PROTO) |
| 2640 | void |
| 2641 | may_generate_prof_end(cctx_T *cctx, int prof_lnum) |
| 2642 | { |
| 2643 | if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0) |
| 2644 | generate_instr(cctx, ISN_PROF_END); |
| 2645 | } |
| 2646 | #endif |
| 2647 | |
| 2648 | |
| 2649 | /* |
| 2650 | * Delete an instruction, free what it contains. |
| 2651 | */ |
| 2652 | void |
| 2653 | delete_instr(isn_T *isn) |
| 2654 | { |
| 2655 | switch (isn->isn_type) |
| 2656 | { |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 2657 | case ISN_AUTOLOAD: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2658 | case ISN_DEF: |
| 2659 | case ISN_EXEC: |
| 2660 | case ISN_EXECRANGE: |
| 2661 | case ISN_EXEC_SPLIT: |
| 2662 | case ISN_LEGACY_EVAL: |
| 2663 | case ISN_LOADAUTO: |
| 2664 | case ISN_LOADB: |
| 2665 | case ISN_LOADENV: |
| 2666 | case ISN_LOADG: |
| 2667 | case ISN_LOADOPT: |
| 2668 | case ISN_LOADT: |
| 2669 | case ISN_LOADW: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2670 | case ISN_PUSHEXC: |
| 2671 | case ISN_PUSHFUNC: |
| 2672 | case ISN_PUSHS: |
| 2673 | case ISN_RANGE: |
| 2674 | case ISN_STOREAUTO: |
| 2675 | case ISN_STOREB: |
| 2676 | case ISN_STOREENV: |
| 2677 | case ISN_STOREG: |
| 2678 | case ISN_STORET: |
| 2679 | case ISN_STOREW: |
| 2680 | case ISN_STRINGMEMBER: |
| 2681 | vim_free(isn->isn_arg.string); |
| 2682 | break; |
| 2683 | |
Ernie Rael | 6488564 | 2023-10-04 20:16:22 +0200 | [diff] [blame] | 2684 | case ISN_LOCKUNLOCK: |
| 2685 | class_unref(isn->isn_arg.lockunlock.lu_cl_exec); |
| 2686 | vim_free(isn->isn_arg.lockunlock.lu_string); |
| 2687 | break; |
| 2688 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2689 | case ISN_SUBSTITUTE: |
| 2690 | { |
| 2691 | int idx; |
| 2692 | isn_T *list = isn->isn_arg.subs.subs_instr; |
| 2693 | |
| 2694 | vim_free(isn->isn_arg.subs.subs_cmd); |
| 2695 | for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx) |
| 2696 | delete_instr(list + idx); |
| 2697 | vim_free(list); |
| 2698 | } |
| 2699 | break; |
| 2700 | |
| 2701 | case ISN_INSTR: |
| 2702 | { |
| 2703 | int idx; |
| 2704 | isn_T *list = isn->isn_arg.instr; |
| 2705 | |
| 2706 | for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx) |
| 2707 | delete_instr(list + idx); |
| 2708 | vim_free(list); |
| 2709 | } |
| 2710 | break; |
| 2711 | |
| 2712 | case ISN_LOADS: |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 2713 | case ISN_LOADEXPORT: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2714 | case ISN_STORES: |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 2715 | case ISN_STOREEXPORT: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2716 | vim_free(isn->isn_arg.loadstore.ls_name); |
| 2717 | break; |
| 2718 | |
| 2719 | case ISN_UNLET: |
| 2720 | case ISN_UNLETENV: |
| 2721 | vim_free(isn->isn_arg.unlet.ul_name); |
| 2722 | break; |
| 2723 | |
| 2724 | case ISN_STOREOPT: |
| 2725 | case ISN_STOREFUNCOPT: |
| 2726 | vim_free(isn->isn_arg.storeopt.so_name); |
| 2727 | break; |
| 2728 | |
| 2729 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 2730 | blob_unref(isn->isn_arg.blob); |
| 2731 | break; |
| 2732 | |
Bram Moolenaar | c4e1b86 | 2023-02-26 18:58:23 +0000 | [diff] [blame] | 2733 | case ISN_PUSHCLASS: |
Bram Moolenaar | 30a8447 | 2023-02-27 08:07:14 +0000 | [diff] [blame] | 2734 | class_unref(isn->isn_arg.classarg); |
Bram Moolenaar | c4e1b86 | 2023-02-26 18:58:23 +0000 | [diff] [blame] | 2735 | break; |
| 2736 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2737 | case ISN_UCALL: |
| 2738 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 2739 | break; |
| 2740 | |
| 2741 | case ISN_FUNCREF: |
| 2742 | { |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 2743 | funcref_T *funcref = &isn->isn_arg.funcref; |
| 2744 | funcref_extra_T *extra = funcref->fr_extra; |
| 2745 | |
| 2746 | if (extra == NULL || extra->fre_func_name == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2747 | { |
| 2748 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 2749 | + funcref->fr_dfunc_idx; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2750 | ufunc_T *ufunc = dfunc->df_ufunc; |
| 2751 | |
| 2752 | if (ufunc != NULL && func_name_refcount(ufunc->uf_name)) |
| 2753 | func_ptr_unref(ufunc); |
| 2754 | } |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 2755 | if (extra != NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2756 | { |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 2757 | char_u *name = extra->fre_func_name; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2758 | |
| 2759 | if (name != NULL) |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 2760 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2761 | func_unref(name); |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 2762 | vim_free(name); |
| 2763 | } |
Bram Moolenaar | 313e472 | 2023-02-08 20:55:27 +0000 | [diff] [blame] | 2764 | if (extra->fre_class != NULL) |
| 2765 | class_unref(extra->fre_class); |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 2766 | vim_free(extra); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2767 | } |
| 2768 | } |
| 2769 | break; |
| 2770 | |
| 2771 | case ISN_DCALL: |
| 2772 | { |
| 2773 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 2774 | + isn->isn_arg.dfunc.cdf_idx; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2775 | |
| 2776 | if (dfunc->df_ufunc != NULL |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 2777 | && func_name_refcount(dfunc->df_ufunc->uf_name)) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2778 | func_ptr_unref(dfunc->df_ufunc); |
| 2779 | } |
| 2780 | break; |
| 2781 | |
Bram Moolenaar | d0200c8 | 2023-01-28 15:19:40 +0000 | [diff] [blame] | 2782 | case ISN_METHODCALL: |
| 2783 | { |
| 2784 | cmfunc_T *mfunc = isn->isn_arg.mfunc; |
| 2785 | class_unref(mfunc->cmf_itf); |
| 2786 | vim_free(mfunc); |
| 2787 | } |
| 2788 | break; |
| 2789 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2790 | case ISN_NEWFUNC: |
| 2791 | { |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 2792 | newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2793 | |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 2794 | if (arg != NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2795 | { |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 2796 | ufunc_T *ufunc = find_func_even_dead( |
| 2797 | arg->nfa_lambda, FFED_IS_GLOBAL); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2798 | |
Bram Moolenaar | 8fa745e | 2022-09-16 19:04:24 +0100 | [diff] [blame] | 2799 | if (ufunc != NULL) |
| 2800 | { |
| 2801 | unlink_def_function(ufunc); |
| 2802 | func_ptr_unref(ufunc); |
| 2803 | } |
| 2804 | |
| 2805 | vim_free(arg->nfa_lambda); |
| 2806 | vim_free(arg->nfa_global); |
| 2807 | vim_free(arg); |
| 2808 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2809 | } |
| 2810 | break; |
| 2811 | |
| 2812 | case ISN_CHECKTYPE: |
| 2813 | case ISN_SETTYPE: |
| 2814 | free_type(isn->isn_arg.type.ct_type); |
| 2815 | break; |
| 2816 | |
| 2817 | case ISN_CMDMOD: |
| 2818 | vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 2819 | ->cmod_filter_regmatch.regprog); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2820 | vim_free(isn->isn_arg.cmdmod.cf_cmdmod); |
| 2821 | break; |
| 2822 | |
| 2823 | case ISN_LOADSCRIPT: |
| 2824 | case ISN_STORESCRIPT: |
| 2825 | vim_free(isn->isn_arg.script.scriptref); |
| 2826 | break; |
| 2827 | |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 2828 | case ISN_LOAD_CLASSMEMBER: |
| 2829 | case ISN_STORE_CLASSMEMBER: |
Bram Moolenaar | 29ac5df | 2023-01-16 19:43:47 +0000 | [diff] [blame] | 2830 | case ISN_GET_ITF_MEMBER: |
Bram Moolenaar | d505d17 | 2022-12-18 21:42:55 +0000 | [diff] [blame] | 2831 | class_unref(isn->isn_arg.classmember.cm_class); |
| 2832 | break; |
| 2833 | |
Bram Moolenaar | f7d1c6e | 2023-01-16 20:47:57 +0000 | [diff] [blame] | 2834 | case ISN_STOREINDEX: |
| 2835 | class_unref(isn->isn_arg.storeindex.si_class); |
| 2836 | break; |
| 2837 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2838 | case ISN_TRY: |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 2839 | vim_free(isn->isn_arg.tryref.try_ref); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2840 | break; |
| 2841 | |
| 2842 | case ISN_CEXPR_CORE: |
| 2843 | vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline); |
| 2844 | vim_free(isn->isn_arg.cexpr.cexpr_ref); |
| 2845 | break; |
| 2846 | |
| 2847 | case ISN_2BOOL: |
| 2848 | case ISN_2STRING: |
| 2849 | case ISN_2STRING_ANY: |
| 2850 | case ISN_ADDBLOB: |
| 2851 | case ISN_ADDLIST: |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 2852 | case ISN_ADDTUPLE: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2853 | case ISN_ANYINDEX: |
| 2854 | case ISN_ANYSLICE: |
| 2855 | case ISN_BCALL: |
| 2856 | case ISN_BLOBAPPEND: |
| 2857 | case ISN_BLOBINDEX: |
| 2858 | case ISN_BLOBSLICE: |
| 2859 | case ISN_CATCH: |
| 2860 | case ISN_CEXPR_AUCMD: |
| 2861 | case ISN_CHECKLEN: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2862 | case ISN_CLEARDICT: |
| 2863 | case ISN_CMDMOD_REV: |
| 2864 | case ISN_COMPAREANY: |
| 2865 | case ISN_COMPAREBLOB: |
| 2866 | case ISN_COMPAREBOOL: |
| 2867 | case ISN_COMPAREDICT: |
| 2868 | case ISN_COMPAREFLOAT: |
| 2869 | case ISN_COMPAREFUNC: |
| 2870 | case ISN_COMPARELIST: |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 2871 | case ISN_COMPARETUPLE: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2872 | case ISN_COMPARENR: |
Bram Moolenaar | 7a22224 | 2022-03-01 19:23:24 +0000 | [diff] [blame] | 2873 | case ISN_COMPARENULL: |
Bram Moolenaar | bcf31ec | 2023-01-02 20:32:24 +0000 | [diff] [blame] | 2874 | case ISN_COMPAREOBJECT: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2875 | case ISN_COMPARESPECIAL: |
| 2876 | case ISN_COMPARESTRING: |
| 2877 | case ISN_CONCAT: |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 2878 | case ISN_CONSTRUCT: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2879 | case ISN_COND2BOOL: |
| 2880 | case ISN_DEBUG: |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 2881 | case ISN_DEFER: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2882 | case ISN_DROP: |
| 2883 | case ISN_ECHO: |
| 2884 | case ISN_ECHOCONSOLE: |
| 2885 | case ISN_ECHOERR: |
| 2886 | case ISN_ECHOMSG: |
Bram Moolenaar | 68a635a | 2022-09-01 17:26:17 +0100 | [diff] [blame] | 2887 | case ISN_ECHOWINDOW: |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 2888 | case ISN_ENDLOOP: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2889 | case ISN_ENDTRY: |
| 2890 | case ISN_EXECCONCAT: |
| 2891 | case ISN_EXECUTE: |
| 2892 | case ISN_FINALLY: |
| 2893 | case ISN_FINISH: |
| 2894 | case ISN_FOR: |
| 2895 | case ISN_GETITEM: |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 2896 | case ISN_GET_OBJ_MEMBER: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2897 | case ISN_JUMP: |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 2898 | case ISN_JUMP_IF_ARG_NOT_SET: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2899 | case ISN_JUMP_IF_ARG_SET: |
| 2900 | case ISN_LISTAPPEND: |
| 2901 | case ISN_LISTINDEX: |
| 2902 | case ISN_LISTSLICE: |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 2903 | case ISN_TUPLEINDEX: |
| 2904 | case ISN_TUPLESLICE: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2905 | case ISN_LOAD: |
| 2906 | case ISN_LOADBDICT: |
| 2907 | case ISN_LOADGDICT: |
| 2908 | case ISN_LOADOUTER: |
| 2909 | case ISN_LOADREG: |
| 2910 | case ISN_LOADTDICT: |
| 2911 | case ISN_LOADV: |
| 2912 | case ISN_LOADWDICT: |
| 2913 | case ISN_LOCKCONST: |
| 2914 | case ISN_MEMBER: |
| 2915 | case ISN_NEGATENR: |
| 2916 | case ISN_NEWDICT: |
| 2917 | case ISN_NEWLIST: |
Yegappan Lakshmanan | 9cb865e | 2025-03-23 16:42:16 +0100 | [diff] [blame] | 2918 | case ISN_NEWTUPLE: |
Bram Moolenaar | 8acb9cc | 2022-03-08 13:18:55 +0000 | [diff] [blame] | 2919 | case ISN_NEWPARTIAL: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2920 | case ISN_OPANY: |
| 2921 | case ISN_OPFLOAT: |
| 2922 | case ISN_OPNR: |
| 2923 | case ISN_PCALL: |
| 2924 | case ISN_PCALL_END: |
| 2925 | case ISN_PROF_END: |
| 2926 | case ISN_PROF_START: |
| 2927 | case ISN_PUSHBOOL: |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 2928 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2929 | case ISN_PUSHF: |
Bram Moolenaar | 397a87a | 2022-03-20 21:14:15 +0000 | [diff] [blame] | 2930 | case ISN_PUSHJOB: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2931 | case ISN_PUSHNR: |
Bram Moolenaar | c4e1b86 | 2023-02-26 18:58:23 +0000 | [diff] [blame] | 2932 | case ISN_PUSHOBJ: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2933 | case ISN_PUSHSPEC: |
| 2934 | case ISN_PUT: |
64-bitman | e08f10a | 2025-03-18 22:14:34 +0100 | [diff] [blame] | 2935 | case ISN_IPUT: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2936 | case ISN_REDIREND: |
| 2937 | case ISN_REDIRSTART: |
| 2938 | case ISN_RETURN: |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 2939 | case ISN_RETURN_OBJECT: |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 2940 | case ISN_RETURN_VOID: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2941 | case ISN_SHUFFLE: |
| 2942 | case ISN_SLICE: |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 2943 | case ISN_SOURCE: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2944 | case ISN_STORE: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2945 | case ISN_STORENR: |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 2946 | case ISN_STOREOUTER: |
Bram Moolenaar | 7ce7daf | 2022-12-10 18:42:12 +0000 | [diff] [blame] | 2947 | case ISN_STORE_THIS: |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 2948 | case ISN_STORERANGE: |
| 2949 | case ISN_STOREREG: |
| 2950 | case ISN_STOREV: |
| 2951 | case ISN_STRINDEX: |
| 2952 | case ISN_STRSLICE: |
| 2953 | case ISN_THROW: |
| 2954 | case ISN_TRYCONT: |
| 2955 | case ISN_UNLETINDEX: |
| 2956 | case ISN_UNLETRANGE: |
| 2957 | case ISN_UNPACK: |
| 2958 | case ISN_USEDICT: |
Bram Moolenaar | b46c083 | 2022-09-15 17:19:37 +0100 | [diff] [blame] | 2959 | case ISN_WHILE: |
Yegappan Lakshmanan | 16f2d3a | 2025-02-24 19:23:43 +0100 | [diff] [blame] | 2960 | case ISN_SCRIPTCTX_SET: |
Bram Moolenaar | c0ceeeb | 2022-03-30 21:12:27 +0100 | [diff] [blame] | 2961 | // nothing allocated |
| 2962 | break; |
Bram Moolenaar | 1d84f76 | 2022-09-03 21:35:53 +0100 | [diff] [blame] | 2963 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | void |
| 2967 | clear_instr_ga(garray_T *gap) |
| 2968 | { |
| 2969 | int idx; |
| 2970 | |
| 2971 | for (idx = 0; idx < gap->ga_len; ++idx) |
| 2972 | delete_instr(((isn_T *)gap->ga_data) + idx); |
| 2973 | ga_clear(gap); |
| 2974 | } |
| 2975 | |
| 2976 | |
| 2977 | #endif // defined(FEAT_EVAL) |