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