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