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