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