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