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