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