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