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