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