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