blob: 9fe850775c8ddb284d42f71796ce065d3bc87806 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* 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/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +000011 * vim9compile.c: compiling a :def function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010012 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaardc7c3662021-12-20 15:04:29 +000024// Functions defined with :def are stored in this growarray.
25// They are never removed, so that they can be found by index.
26// Deleted functions have the df_deleted flag set.
27garray_T def_functions = {0, 0, sizeof(dfunc_T), 50, NULL};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010029static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010030
31/*
Bram Moolenaar709664c2020-12-12 14:33:41 +010032 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +010033 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +010034 * If "lvar" is NULL only check if the variable can be found.
35 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +000037 int
Bram Moolenaar709664c2020-12-12 14:33:41 +010038lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010039{
40 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +010041 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042
Bram Moolenaarae8d2de2020-02-13 21:42:24 +010043 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +010044 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020045
Bram Moolenaar58b40092023-01-11 15:59:05 +000046 if (((len == 4 && STRNCMP(name, "this", 4) == 0)
47 || (len == 5 && STRNCMP(name, "super", 5) == 0))
Bram Moolenaar00b28d62022-12-08 15:32:33 +000048 && cctx->ctx_ufunc != NULL
Bram Moolenaar574950d2023-01-03 19:08:50 +000049 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)))
Bram Moolenaar00b28d62022-12-08 15:32:33 +000050 {
Bram Moolenaar58b40092023-01-11 15:59:05 +000051 int is_super = *name == 's';
Bram Moolenaar00b28d62022-12-08 15:32:33 +000052 if (lvar != NULL)
53 {
54 CLEAR_POINTER(lvar);
Bram Moolenaar58b40092023-01-11 15:59:05 +000055 lvar->lv_name = (char_u *)(is_super ? "super" : "this");
Bram Moolenaar00b28d62022-12-08 15:32:33 +000056 if (cctx->ctx_ufunc->uf_class != NULL)
Bram Moolenaar58b40092023-01-11 15:59:05 +000057 {
Bram Moolenaarffdaca92022-12-09 21:41:48 +000058 lvar->lv_type = &cctx->ctx_ufunc->uf_class->class_object_type;
Bram Moolenaar58b40092023-01-11 15:59:05 +000059 if (is_super)
60 {
61 type_T *type = get_type_ptr(cctx->ctx_type_list);
62
63 if (type != NULL)
64 {
65 *type = *lvar->lv_type;
66 lvar->lv_type = type;
67 type->tt_flags |= TTFLAG_SUPER;
68 }
69 }
70 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +000071 }
72 return OK;
73 }
74
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020075 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
77 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010078 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaara275f2c2022-10-11 20:04:09 +010079 if (lvp->lv_name != NULL
80 && STRNCMP(name, lvp->lv_name, len) == 0
Bram Moolenaar709664c2020-12-12 14:33:41 +010081 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020082 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010083 if (lvar != NULL)
84 {
85 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +010086 lvar->lv_from_outer = 0;
Bram Moolenaar65449bd2022-09-19 16:02:43 +010087 // If the variable was declared inside a loop set
88 // lvar->lv_loop_idx and lvar->lv_loop_depth.
89 get_loop_var_idx(cctx, idx, lvar);
Bram Moolenaar709664c2020-12-12 14:33:41 +010090 }
91 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020092 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010093 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020094
95 // Find local in outer function scope.
96 if (cctx->ctx_outer != NULL)
97 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010098 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020099 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100100 if (lvar != NULL)
101 {
102 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100103 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100104 }
105 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200106 }
107 }
108
Bram Moolenaar709664c2020-12-12 14:33:41 +0100109 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100110}
111
112/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200113 * Lookup an argument in the current function and an enclosing function.
114 * Returns the argument index in "idxp"
115 * Returns the argument type in "type"
116 * Sets "gen_load_outer" to TRUE if found in outer scope.
117 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100118 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000119 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200120arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200121 char_u *name,
122 size_t len,
123 int *idxp,
124 type_T **type,
125 int *gen_load_outer,
126 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100127{
128 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200129 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100130
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100131 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200132 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200133 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100134 {
135 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
136
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200137 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
138 {
139 if (idxp != NULL)
140 {
141 // Arguments are located above the frame pointer. One further
142 // if there is a vararg argument
143 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
144 + STACK_FRAME_SIZE)
145 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
146
147 if (cctx->ctx_ufunc->uf_arg_types != NULL)
148 *type = cctx->ctx_ufunc->uf_arg_types[idx];
149 else
150 *type = &t_any;
151 }
152 return OK;
153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200156 va_name = cctx->ctx_ufunc->uf_va_name;
157 if (va_name != NULL
158 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
159 {
160 if (idxp != NULL)
161 {
162 // varargs is always the last argument
163 *idxp = -STACK_FRAME_SIZE - 1;
164 *type = cctx->ctx_ufunc->uf_va_type;
165 }
166 return OK;
167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200169 if (cctx->ctx_outer != NULL)
170 {
171 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200172 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200173 == OK)
174 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100175 if (gen_load_outer != NULL)
176 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200177 return OK;
178 }
179 }
180
181 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182}
183
184/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200185 * Lookup a script-local variable in the current script, possibly defined in a
186 * block that contains the function "cctx->ctx_ufunc".
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000187 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100188 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200189 * Return NULL when not found.
190 */
191 static sallvar_T *
Bram Moolenaardce24412022-02-08 20:35:30 +0000192find_script_var(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200193{
194 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
195 hashitem_T *hi;
196 int cc;
197 sallvar_T *sav;
198 ufunc_T *ufunc;
199
200 // Find the list of all script variables with the right name.
201 if (len > 0)
202 {
203 cc = name[len];
204 name[len] = NUL;
205 }
206 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
207 if (len > 0)
208 name[len] = cc;
209 if (HASHITEM_EMPTY(hi))
210 return NULL;
211
212 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200213 if (sav->sav_block_id == 0)
214 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200215 return sav;
216
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200217 if (cctx == NULL)
218 {
Bram Moolenaar766ae5b2022-09-14 00:30:51 +0100219 if (cstack == NULL)
220 return NULL;
221
Bram Moolenaardce24412022-02-08 20:35:30 +0000222 // Not in a function scope, find variable with block ID equal to or
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000223 // smaller than the current block id. Use "cstack" to go up the block
224 // scopes.
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200225 while (sav != NULL)
226 {
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000227 int idx;
Bram Moolenaardce24412022-02-08 20:35:30 +0000228
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000229 for (idx = cstack->cs_idx; idx >= 0; --idx)
230 if (cstack->cs_block_id[idx] == sav->sav_block_id)
Bram Moolenaardce24412022-02-08 20:35:30 +0000231 break;
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000232 if (idx >= 0)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200233 break;
234 sav = sav->sav_next;
235 }
236 return sav;
237 }
238
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200239 // Go over the variables with this name and find one that was visible
240 // from the function.
241 ufunc = cctx->ctx_ufunc;
242 while (sav != NULL)
243 {
244 int idx;
245
246 // Go over the blocks that this function was defined in. If the
247 // variable block ID matches it was visible to the function.
248 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
249 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
250 return sav;
251 sav = sav->sav_next;
252 }
253
Bram Moolenaar58493cf2022-01-06 12:23:30 +0000254 // Not found, variable was not visible.
255 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200256}
257
258/*
Bram Moolenaar766ae5b2022-09-14 00:30:51 +0100259 * If "name" can be found in the current script set it's "block_id".
260 */
261 void
262update_script_var_block_id(char_u *name, int block_id)
263{
264 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
265 hashitem_T *hi;
266 sallvar_T *sav;
267
268 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
269 if (HASHITEM_EMPTY(hi))
270 return;
271 sav = HI2SAV(hi);
272 sav->sav_block_id = block_id;
273}
274
275/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100276 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200277 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000278 int
Bram Moolenaar84367732020-08-23 15:21:55 +0200279script_is_vim9()
280{
281 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
282}
283
284/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200285 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000286 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287 * Returns OK or FAIL.
288 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000289 int
Bram Moolenaardce24412022-02-08 20:35:30 +0000290script_var_exists(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100291{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200292 if (current_sctx.sc_sid <= 0)
293 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200294 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200295 {
296 // Check script variables that were visible where the function was
297 // defined.
Bram Moolenaardce24412022-02-08 20:35:30 +0000298 if (find_script_var(name, len, cctx, cstack) != NULL)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200299 return OK;
300 }
301 else
302 {
303 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
304 dictitem_T *di;
305 int cc;
306
307 // Check script variables that are currently visible
308 cc = name[len];
309 name[len] = NUL;
310 di = find_var_in_ht(ht, 0, name, TRUE);
311 name[len] = cc;
312 if (di != NULL)
313 return OK;
314 }
315
316 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317}
318
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100319/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100320 * Return TRUE if "name" is a local variable, argument, script variable or
321 * imported.
322 */
323 static int
324variable_exists(char_u *name, size_t len, cctx_T *cctx)
325{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100326 return (cctx != NULL
327 && (lookup_local(name, len, NULL, cctx) == OK
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000328 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK
329 || (len == 4
330 && cctx->ctx_ufunc != NULL
Bram Moolenaar574950d2023-01-03 19:08:50 +0000331 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000332 && STRNCMP(name, "this", 4) == 0)))
Bram Moolenaardce24412022-02-08 20:35:30 +0000333 || script_var_exists(name, len, cctx, NULL) == OK
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000334 || class_member_index(name, len, NULL, cctx) >= 0
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000335 || find_imported(name, len, FALSE) != NULL;
Bram Moolenaare0890d62021-02-17 14:52:14 +0100336}
337
338/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100339 * Return TRUE if "name" is a local variable, argument, script variable,
Bram Moolenaar97f8c102022-04-02 19:43:57 +0100340 * imported or function. Or commands are being skipped, a declaration may have
341 * been skipped then.
Bram Moolenaar6914e872021-03-06 21:01:09 +0100342 */
343 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100344item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100345{
Bram Moolenaar0631bb42022-02-13 21:20:21 +0000346 return variable_exists(name, len, cctx);
Bram Moolenaar6914e872021-03-06 21:01:09 +0100347}
348
349/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100350 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000351 * compilation context "cctx".
352 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200353 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100354 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100355 * Return FAIL and give an error if it defined.
356 */
357 int
Bram Moolenaardce24412022-02-08 20:35:30 +0000358check_defined(
359 char_u *p,
360 size_t len,
361 cctx_T *cctx,
362 cstack_T *cstack,
363 int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100364{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200365 int c = p[len];
366 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200367
Bram Moolenaarda479c72021-04-10 21:01:38 +0200368 // underscore argument is OK
369 if (len == 1 && *p == '_')
370 return OK;
371
Bram Moolenaardce24412022-02-08 20:35:30 +0000372 if (script_var_exists(p, len, cctx, cstack) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100373 {
374 if (is_arg)
375 semsg(_(e_argument_already_declared_in_script_str), p);
376 else
377 semsg(_(e_variable_already_declared_in_script_str), p);
378 return FAIL;
379 }
380
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000381 if (class_member_index(p, len, NULL, cctx) >= 0)
382 {
383 if (is_arg)
384 semsg(_(e_argument_already_declared_in_class_str), p);
385 else
386 semsg(_(e_variable_already_declared_in_class_str), p);
387 return FAIL;
388 }
389
Bram Moolenaarad486a02020-08-01 23:22:18 +0200390 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100391 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100392 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200393 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000394 || find_imported(p, len, FALSE) != NULL
Bram Moolenaaracc4b562022-01-24 13:54:45 +0000395 || (ufunc = find_func_even_dead(p, 0)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100396 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200397 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200398 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
399 && (!func_is_global(ufunc)
400 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200401 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100402 if (is_arg)
403 semsg(_(e_argument_name_shadows_existing_variable_str), p);
404 else
405 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200406 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200407 return FAIL;
408 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100409 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200410 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100411 return OK;
412}
413
Bram Moolenaar65b95452020-07-19 14:03:09 +0200414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100415/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200416 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
417 * used. Return FALSE if the types will never match.
418 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200419 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200420use_typecheck(type_T *actual, type_T *expected)
421{
422 if (actual->tt_type == VAR_ANY
423 || actual->tt_type == VAR_UNKNOWN
424 || (actual->tt_type == VAR_FUNC
425 && (expected->tt_type == VAR_FUNC
426 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000427 && (actual->tt_member == &t_any
428 || actual->tt_member == &t_unknown
429 || actual->tt_argcount < 0)
430 && (actual->tt_member == &t_unknown ||
431 (actual->tt_member == &t_void)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100432 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200433 return TRUE;
434 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
435 && actual->tt_type == expected->tt_type)
436 // This takes care of a nested list or dict.
437 return use_typecheck(actual->tt_member, expected->tt_member);
438 return FALSE;
439}
440
441/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200442 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200443 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200444 * - "actual" is a type that can be "expected" type: add a runtime check; or
445 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200446 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
447 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200448 */
Bram Moolenaara1c51952022-02-02 16:20:26 +0000449 int
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200450need_type_where(
451 type_T *actual,
452 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000453 int number_ok, // expect VAR_FLOAT but VAR_NUMBER is OK
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200454 int offset,
455 where_T where,
456 cctx_T *cctx,
457 int silent,
458 int actual_is_const)
459{
Bram Moolenaar44a89772021-12-18 12:31:33 +0000460 int ret;
461
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200462 if (expected == &t_bool && actual != &t_bool
463 && (actual->tt_flags & TTFLAG_BOOL_OK))
464 {
465 // Using "0", "1" or the result of an expression with "&&" or "||" as a
466 // boolean is OK but requires a conversion.
467 generate_2BOOL(cctx, FALSE, offset);
468 return OK;
469 }
470
Bram Moolenaar44a89772021-12-18 12:31:33 +0000471 ret = check_type_maybe(expected, actual, FALSE, where);
472 if (ret == OK)
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200473 return OK;
474
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000475 // If actual a constant a runtime check makes no sense. If it's
476 // null_function it is OK.
477 if (actual_is_const && ret == MAYBE && actual == &t_func_unknown)
478 return OK;
479
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200480 // If the actual type can be the expected type add a runtime check.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000481 if (!actual_is_const && ret == MAYBE && use_typecheck(actual, expected))
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200482 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000483 generate_TYPECHECK(cctx, expected, number_ok, offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100484 where.wt_variable, where.wt_index);
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200485 return OK;
486 }
487
488 if (!silent)
489 type_mismatch_where(expected, actual, where);
490 return FAIL;
491}
492
Bram Moolenaar351ead02021-01-16 16:07:01 +0100493 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200494need_type(
495 type_T *actual,
496 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000497 int number_ok, // when expected is float number is also OK
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200498 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100499 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200500 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200501 int silent,
502 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200503{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200504 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100505
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100506 where.wt_index = arg_idx;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000507 return need_type_where(actual, expected, number_ok, offset, where,
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200508 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200509}
510
511/*
Bram Moolenaarfa103972022-09-29 19:14:42 +0100512 * Set type of variable "lvar" to "type". If the variable is a constant then
513 * the type gets TTFLAG_CONST.
514 */
515 static void
516set_var_type(lvar_T *lvar, type_T *type_arg, cctx_T *cctx)
517{
518 type_T *type = type_arg;
519
Bram Moolenaar6586a012022-09-30 11:04:50 +0100520 if (lvar->lv_const == ASSIGN_CONST && (type->tt_flags & TTFLAG_CONST) == 0)
Bram Moolenaarfa103972022-09-29 19:14:42 +0100521 {
522 if (type->tt_flags & TTFLAG_STATIC)
523 // entry in static_types[] is followed by const type
524 type = type + 1;
525 else
526 {
527 type = copy_type(type, cctx->ctx_type_list);
528 type->tt_flags |= TTFLAG_CONST;
529 }
530 }
531 lvar->lv_type = type;
532}
533
534/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 * Reserve space for a local variable.
Bram Moolenaar6586a012022-09-30 11:04:50 +0100536 * "assign" can be ASSIGN_VAR for :var, ASSIGN_CONST for :const and
537 * ASSIGN_FINAL for :final.
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200538 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000540 lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +0200541reserve_local(
542 cctx_T *cctx,
543 char_u *name,
544 size_t len,
Bram Moolenaar6586a012022-09-30 11:04:50 +0100545 int assign,
Bram Moolenaare8211a32020-10-09 22:04:29 +0200546 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100547{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200549 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200551 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200553 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200554 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 }
556
Bram Moolenaar35578162021-08-02 19:10:38 +0200557 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200558 return NULL;
559 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +0200560 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100561
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200562 // Every local variable uses the next entry on the stack. We could re-use
563 // the last ones when leaving a scope, but then variables used in a closure
564 // might get overwritten. To keep things simple do not re-use stack
565 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200566 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
567 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200568
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200569 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar6586a012022-09-30 11:04:50 +0100570 lvar->lv_const = assign;
Bram Moolenaarfa103972022-09-29 19:14:42 +0100571 if (type == &t_unknown || type == &t_any)
572 // type not known yet, may be inferred from RHS
573 lvar->lv_type = type;
574 else
575 // may use TTFLAG_CONST
576 set_var_type(lvar, type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200578 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +0200579 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200580 return NULL;
581 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
582 vim_strsave(lvar->lv_name);
583 ++dfunc->df_var_names.ga_len;
584
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200585 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586}
587
588/*
Bram Moolenaar08251752021-01-11 21:20:18 +0100589 * If "check_writable" is ASSIGN_CONST give an error if the variable was
590 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
591 * error if the variable was defined with :const.
592 */
593 static int
594check_item_writable(svar_T *sv, int check_writable, char_u *name)
595{
596 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
597 || (check_writable == ASSIGN_FINAL
598 && sv->sv_const == ASSIGN_CONST))
599 {
Bram Moolenaard8e44472021-07-21 22:20:33 +0200600 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +0100601 return FAIL;
602 }
603 return OK;
604}
605
606/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +0100608 * Pass "check_writable" to check_item_writable().
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000609 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610 * Returns the index in "sn_var_vals" if found.
611 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +0100612 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613 */
614 int
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000615get_script_item_idx(
616 int sid,
617 char_u *name,
618 int check_writable,
619 cctx_T *cctx,
620 cstack_T *cstack)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621{
622 hashtab_T *ht;
623 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100624 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200625 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 int idx;
627
Bram Moolenaare3d46852020-08-29 13:39:17 +0200628 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200630 if (sid == current_sctx.sc_sid)
631 {
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000632 sallvar_T *sav = find_script_var(name, 0, cctx, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200633
634 if (sav == NULL)
635 return -2;
636 idx = sav->sav_var_vals_idx;
637 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +0100638 if (check_item_writable(sv, check_writable, name) == FAIL)
639 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200640 return idx;
641 }
642
643 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 ht = &SCRIPT_VARS(sid);
645 di = find_var_in_ht(ht, 0, name, TRUE);
646 if (di == NULL)
Bram Moolenaar577bd852022-01-10 18:42:52 +0000647 {
648 if (si->sn_autoload_prefix != NULL)
649 {
650 hashitem_T *hi;
651
652 // A variable exported from an autoload script is in the global
653 // variables, we can find it in the all_vars table.
654 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
655 if (!HASHITEM_EMPTY(hi))
656 return HI2SAV(hi)->sav_var_vals_idx;
657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658 return -2;
Bram Moolenaar577bd852022-01-10 18:42:52 +0000659 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660
661 // Now find the svar_T index in sn_var_vals.
662 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
663 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200664 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 if (sv->sv_tv == &di->di_tv)
666 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100667 if (check_item_writable(sv, check_writable, name) == FAIL)
668 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 return idx;
670 }
671 }
672 return -1;
673}
674
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000675 static imported_T *
676find_imported_in_script(char_u *name, size_t len, int sid)
677{
678 scriptitem_T *si;
679 int idx;
680
681 if (!SCRIPT_ID_VALID(sid))
682 return NULL;
683 si = SCRIPT_ITEM(sid);
684 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
685 {
686 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
687
688 if (len == 0 ? STRCMP(name, import->imp_name) == 0
689 : STRLEN(import->imp_name) == len
690 && STRNCMP(name, import->imp_name, len) == 0)
691 return import;
692 }
693 return NULL;
694}
695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696/*
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000697 * Find "name" in imported items of the current script.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100698 * If "len" is 0 use any length that works.
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000699 * If "load" is TRUE and the script was not loaded yet, load it now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 */
701 imported_T *
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000702find_imported(char_u *name, size_t len, int load)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200704 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +0200705 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706
Bram Moolenaarb775e722022-11-22 18:12:44 +0000707 // Skip over "s:" before "s:something" to find the import name.
708 int off = name[0] == 's' && name[1] == ':' ? 2 : 0;
709
710 imported_T *ret = find_imported_in_script(name + off, len - off,
711 current_sctx.sc_sid);
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000712 if (ret != NULL && load && (ret->imp_flags & IMP_FLAGS_AUTOLOAD))
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000713 {
Bram Moolenaar753885b2022-08-24 16:30:36 +0100714 scid_T actual_sid = 0;
Bram Moolenaar6809ff92022-07-26 15:10:56 +0100715 int save_emsg_off = emsg_off;
716
717 // "emsg_off" will be set when evaluating an expression silently, but
718 // we do want to know about errors in a script. Also because it then
719 // aborts when an error is encountered.
720 emsg_off = FALSE;
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000721
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000722 // script found before but not loaded yet
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000723 ret->imp_flags &= ~IMP_FLAGS_AUTOLOAD;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000724 (void)do_source(SCRIPT_ITEM(ret->imp_sid)->sn_name, FALSE,
Bram Moolenaar753885b2022-08-24 16:30:36 +0100725 DOSO_NONE, &actual_sid);
726 // If the script is a symlink it may be sourced with another name, may
727 // need to adjust the script ID for that.
728 if (actual_sid != 0)
729 ret->imp_sid = actual_sid;
Bram Moolenaar6809ff92022-07-26 15:10:56 +0100730
731 emsg_off = save_emsg_off;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000732 }
733 return ret;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200734}
735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000737 * Called when checking for a following operator at "arg". When the rest of
738 * the line is empty or only a comment, peek the next line. If there is a next
739 * line return a pointer to it and set "nextp".
740 * Otherwise skip over white space.
741 */
742 char_u *
743may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
744{
745 char_u *p = skipwhite(arg);
746
747 *nextp = NULL;
748 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
749 {
750 *nextp = peek_next_line_from_context(cctx);
751 if (*nextp != NULL)
752 return *nextp;
753 }
754 return p;
755}
756
757/*
Bram Moolenaar23c55272020-06-21 16:58:13 +0200758 * Return a pointer to the next line that isn't empty or only contains a
759 * comment. Skips over white space.
760 * Returns NULL if there is none.
761 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200762 char_u *
763peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +0200764{
765 int lnum = cctx->ctx_lnum;
766
767 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
768 {
769 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +0200770 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200771
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200772 // ignore NULLs inserted for continuation lines
773 if (line != NULL)
774 {
775 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100776 if (vim9_bad_comment(p))
777 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200778 if (*p != NUL && !vim9_comment_start(p))
779 return p;
780 }
Bram Moolenaar23c55272020-06-21 16:58:13 +0200781 }
782 return NULL;
783}
784
785/*
Bram Moolenaare6085c52020-04-12 20:19:16 +0200786 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +0200787 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +0200788 * Returns NULL when at the end.
789 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200790 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +0200791next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +0200792{
Bram Moolenaar7a092242020-04-16 22:10:49 +0200793 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200794
795 do
796 {
797 ++cctx->ctx_lnum;
798 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200799 {
800 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200801 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +0200802 }
Bram Moolenaare6085c52020-04-12 20:19:16 +0200803 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +0200804 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200805 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200806 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +0200807 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +0200808 return line;
809}
810
811/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100812 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200813 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +0200814 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200815 * Return FAIL if beyond the last line, "*arg" is unmodified then.
816 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000817 int
Bram Moolenaar2c330432020-04-13 14:41:35 +0200818may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200819{
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100820 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100821 if (vim9_bad_comment(*arg))
822 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200823 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200824 {
Bram Moolenaar23c55272020-06-21 16:58:13 +0200825 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200826
827 if (next == NULL)
828 return FAIL;
829 *arg = skipwhite(next);
830 }
831 return OK;
832}
833
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200834/*
835 * Idem, and give an error when failed.
836 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000837 int
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200838may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
839{
840 if (may_get_next_line(whitep, arg, cctx) == FAIL)
841 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +0100842 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200843 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200844 return FAIL;
845 }
846 return OK;
847}
848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200850 * Get a line from the compilation context, compatible with exarg_T getline().
851 * Return a pointer to the line in allocated memory.
852 * Return NULL for end-of-file or some error.
853 */
854 static char_u *
855exarg_getline(
856 int c UNUSED,
857 void *cookie,
858 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200859 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200860{
861 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +0200862 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200863
Bram Moolenaar66250c92020-08-20 15:02:42 +0200864 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200865 {
Bram Moolenaar2914a202020-09-27 18:24:03 +0200866 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +0200867 return NULL;
868 ++cctx->ctx_lnum;
869 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
870 // Comment lines result in NULL pointers, skip them.
871 if (p != NULL)
872 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +0200873 }
Bram Moolenaar04b12692020-05-04 23:24:44 +0200874}
875
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100876 void
877fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
878{
879 eap->getline = exarg_getline;
880 eap->cookie = cctx;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +0000881 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100882}
883
Bram Moolenaar04b12692020-05-04 23:24:44 +0200884/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885 * Return TRUE if "ufunc" should be compiled, taking into account whether
886 * "profile" indicates profiling is to be done.
887 */
888 int
889func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
890{
891 switch (ufunc->uf_def_status)
892 {
893 case UF_TO_BE_COMPILED:
894 return TRUE;
895
896 case UF_COMPILED:
897 {
898 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
899 + ufunc->uf_dfunc_idx;
900
901 switch (compile_type)
902 {
903 case CT_PROFILE:
904#ifdef FEAT_PROFILE
905 return dfunc->df_instr_prof == NULL;
906#endif
907 case CT_NONE:
908 return dfunc->df_instr == NULL;
909 case CT_DEBUG:
910 return dfunc->df_instr_debug == NULL;
911 }
912 }
913
914 case UF_NOT_COMPILED:
915 case UF_COMPILE_ERROR:
916 case UF_COMPILING:
917 break;
918 }
919 return FALSE;
920}
921
922/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200923 * Compile a nested :def command.
924 */
925 static char_u *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000926compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200927{
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200928 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +0200929 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200930 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000931 int off;
932 char_u *func_name;
Bram Moolenaareef21022020-08-01 22:16:43 +0200933 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200934 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100935 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +0200936 compiletype_T compile_type;
Bram Moolenaara915fa02022-03-23 11:29:15 +0000937 isn_T *funcref_isn = NULL;
Bram Moolenaar1889f492022-08-16 19:34:44 +0100938 lvar_T *lvar = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200939
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200940 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200941 {
942 emsg(_(e_cannot_use_bang_with_nested_def));
943 return NULL;
944 }
945
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100946 if (*name_start == '/')
947 {
948 name_end = skip_regexp(name_start + 1, '/', TRUE);
949 if (*name_end == '/')
950 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +0200951 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100952 }
953 if (name_end == name_start || *skipwhite(name_end) != '(')
954 {
955 if (!ends_excmd2(name_start, name_end))
956 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +0000957 if (*skipwhite(name_end) == '.')
958 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
959 eap->cmd);
960 else
961 semsg(_(e_invalid_command_str), eap->cmd);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100962 return NULL;
963 }
964
965 // "def" or "def Name": list functions
966 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
967 return NULL;
968 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
969 }
970
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200971 // Only g:Func() can use a namespace.
972 if (name_start[1] == ':' && !is_global)
973 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200974 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200975 return NULL;
976 }
Bram Moolenaar4f3321f2022-03-13 13:12:27 +0000977 if (cctx->ctx_skip != SKIP_YES
978 && check_defined(name_start, name_end - name_start, cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000979 NULL, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +0200980 return NULL;
Bram Moolenaarf681cfb2022-02-07 20:30:57 +0000981 if (!ASCII_ISUPPER(is_global ? name_start[2] : name_start[0]))
982 {
Bram Moolenaar3787f262022-02-07 21:54:01 +0000983 semsg(_(e_function_name_must_start_with_capital_str), name_start);
Bram Moolenaarf681cfb2022-02-07 20:30:57 +0000984 return NULL;
985 }
Bram Moolenaareef21022020-08-01 22:16:43 +0200986
Bram Moolenaar04b12692020-05-04 23:24:44 +0200987 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100988 fill_exarg_from_cctx(eap, cctx);
989
Bram Moolenaar04b12692020-05-04 23:24:44 +0200990 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000991 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100992 lambda_name = vim_strsave(get_lambda_name());
993 if (lambda_name == NULL)
994 return NULL;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000995
996 // This may free the current line, make a copy of the name.
997 off = is_global ? 2 : 0;
998 func_name = vim_strnsave(name_start + off, name_end - name_start - off);
999 if (func_name == NULL)
1000 {
1001 r = FAIL;
1002 goto theend;
1003 }
1004
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001005 // Make sure "KeyTyped" is not set, it may cause indent to be written.
1006 int save_KeyTyped = KeyTyped;
1007 KeyTyped = FALSE;
1008
Bram Moolenaar554d0312023-01-05 19:59:18 +00001009 ufunc = define_function(eap, lambda_name, lines_to_free, 0);
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001010
1011 KeyTyped = save_KeyTyped;
1012
Bram Moolenaar822ba242020-05-24 23:00:18 +02001013 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001014 {
1015 r = eap->skip ? OK : FAIL;
1016 goto theend;
1017 }
Bram Moolenaar7473a842021-12-28 17:55:26 +00001018 if (eap->nextcmd != NULL)
1019 {
1020 semsg(_(e_text_found_after_str_str),
1021 eap->cmdidx == CMD_def ? "enddef" : "endfunction", eap->nextcmd);
1022 r = FAIL;
Bram Moolenaard2939812021-12-30 17:09:05 +00001023 func_ptr_unref(ufunc);
Bram Moolenaar7473a842021-12-28 17:55:26 +00001024 goto theend;
1025 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01001026
1027 // copy over the block scope IDs before compiling
1028 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
1029 {
1030 int block_depth = cctx->ctx_ufunc->uf_block_depth;
1031
1032 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
1033 if (ufunc->uf_block_ids != NULL)
1034 {
1035 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
1036 sizeof(int) * block_depth);
1037 ufunc->uf_block_depth = block_depth;
1038 }
1039 }
1040
Bram Moolenaara915fa02022-03-23 11:29:15 +00001041 // Define the funcref before compiling, so that it is found by any
1042 // recursive call.
1043 if (is_global)
1044 {
Bram Moolenaar65449bd2022-09-19 16:02:43 +01001045 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaara915fa02022-03-23 11:29:15 +00001046 func_name = NULL;
1047 lambda_name = NULL;
1048 }
1049 else
1050 {
1051 // Define a local variable for the function reference.
Bram Moolenaar1889f492022-08-16 19:34:44 +01001052 lvar = reserve_local(cctx, func_name, name_end - name_start,
Bram Moolenaar6586a012022-09-30 11:04:50 +01001053 ASSIGN_CONST, ufunc->uf_func_type);
Bram Moolenaara915fa02022-03-23 11:29:15 +00001054 if (lvar == NULL)
1055 goto theend;
1056 if (generate_FUNCREF(cctx, ufunc, &funcref_isn) == FAIL)
1057 goto theend;
1058 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
1059 }
1060
Bram Moolenaar139575d2022-03-15 19:29:30 +00001061 compile_type = get_compile_type(ufunc);
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001062#ifdef FEAT_PROFILE
1063 // If the outer function is profiled, also compile the nested function for
1064 // profiling.
1065 if (cctx->ctx_compile_type == CT_PROFILE)
1066 compile_type = CT_PROFILE;
1067#endif
1068 if (func_needs_compiling(ufunc, compile_type)
1069 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02001070 {
1071 func_ptr_unref(ufunc);
Bram Moolenaar1889f492022-08-16 19:34:44 +01001072 if (lvar != NULL)
1073 // Now the local variable can't be used.
1074 *lvar->lv_name = '/'; // impossible value
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001075 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02001076 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02001077
Bram Moolenaar648594e2021-07-11 17:55:01 +02001078#ifdef FEAT_PROFILE
1079 // When the outer function is compiled for profiling, the nested function
1080 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001081 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02001082 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1083#endif
1084
Bram Moolenaara915fa02022-03-23 11:29:15 +00001085 // If a FUNCREF instruction was generated, set the index after compiling.
1086 if (funcref_isn != NULL && ufunc->uf_def_status == UF_COMPILED)
1087 funcref_isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001088
1089theend:
1090 vim_free(lambda_name);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001091 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001092 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02001093}
1094
1095/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001096 * Compile one Vim expression {expr} in string "p".
1097 * "p" points to the opening "{".
1098 * Return a pointer to the character after "}", NULL for an error.
1099 */
1100 char_u *
1101compile_one_expr_in_str(char_u *p, cctx_T *cctx)
1102{
1103 char_u *block_start;
1104 char_u *block_end;
1105
1106 // Skip the opening {.
1107 block_start = skipwhite(p + 1);
1108 block_end = block_start;
1109 if (*block_start != NUL && skip_expr(&block_end, NULL) == FAIL)
1110 return NULL;
1111 block_end = skipwhite(block_end);
1112 // The block must be closed by a }.
1113 if (*block_end != '}')
1114 {
1115 semsg(_(e_missing_close_curly_str), p);
1116 return NULL;
1117 }
1118 if (compile_expr0(&block_start, cctx) == FAIL)
1119 return NULL;
1120 may_generate_2STRING(-1, TRUE, cctx);
1121
1122 return block_end + 1;
1123}
1124
1125/*
LemonBoy2eaef102022-05-06 13:14:50 +01001126 * Compile a string "str" (either containing a literal string or a mix of
1127 * literal strings and Vim expressions of the form `{expr}`). This is used
1128 * when compiling a heredoc assignment to a variable or an interpolated string
1129 * in a Vim9 def function. Vim9 instructions are generated to push strings,
1130 * evaluate expressions, concatenate them and create a list of lines. When
1131 * "evalstr" is TRUE, Vim expressions in "str" are evaluated.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001132 */
1133 int
LemonBoy2eaef102022-05-06 13:14:50 +01001134compile_all_expr_in_str(char_u *str, int evalstr, cctx_T *cctx)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001135{
LemonBoy2eaef102022-05-06 13:14:50 +01001136 char_u *p = str;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001137 char_u *val;
LemonBoy2eaef102022-05-06 13:14:50 +01001138 int count = 0;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001139
1140 if (cctx->ctx_skip == SKIP_YES)
1141 return OK;
1142
LemonBoy2eaef102022-05-06 13:14:50 +01001143 if (!evalstr || *str == NUL)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001144 {
LemonBoy2eaef102022-05-06 13:14:50 +01001145 // Literal string, possibly empty.
1146 val = *str != NUL ? vim_strsave(str) : NULL;
1147 return generate_PUSHS(cctx, &val);
1148 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001149
LemonBoy2eaef102022-05-06 13:14:50 +01001150 // Push all the string pieces to the stack, followed by a ISN_CONCAT.
1151 while (*p != NUL)
1152 {
1153 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +01001154 int escaped_brace = FALSE;
1155
1156 // Look for a block start.
1157 lit_start = p;
1158 while (*p != '{' && *p != '}' && *p != NUL)
1159 ++p;
1160
1161 if (*p != NUL && *p == p[1])
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001162 {
LemonBoy2eaef102022-05-06 13:14:50 +01001163 // Escaped brace, unescape and continue.
1164 // Include the brace in the literal string.
1165 ++p;
1166 escaped_brace = TRUE;
1167 }
1168 else if (*p == '}')
1169 {
1170 semsg(_(e_stray_closing_curly_str), str);
1171 return FAIL;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001172 }
LemonBoy372bcce2022-04-25 12:43:20 +01001173
LemonBoy2eaef102022-05-06 13:14:50 +01001174 // Append the literal part.
1175 if (p != lit_start)
1176 {
1177 val = vim_strnsave(lit_start, (size_t)(p - lit_start));
1178 if (generate_PUSHS(cctx, &val) == FAIL)
1179 return FAIL;
1180 ++count;
1181 }
1182
1183 if (*p == NUL)
1184 break;
1185
1186 if (escaped_brace)
1187 {
1188 // Skip the second brace.
1189 ++p;
1190 continue;
1191 }
1192
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001193 p = compile_one_expr_in_str(p, cctx);
1194 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +01001195 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01001196 ++count;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001197 }
LemonBoy2eaef102022-05-06 13:14:50 +01001198
1199 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001200 if (count > 1)
LemonBoy2eaef102022-05-06 13:14:50 +01001201 return generate_CONCAT(cctx, count);
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001202
1203 return OK;
1204}
1205
1206/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 * Return the length of an assignment operator, or zero if there isn't one.
1208 */
1209 int
1210assignment_len(char_u *p, int *heredoc)
1211{
1212 if (*p == '=')
1213 {
1214 if (p[1] == '<' && p[2] == '<')
1215 {
1216 *heredoc = TRUE;
1217 return 3;
1218 }
1219 return 1;
1220 }
1221 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
1222 return 2;
1223 if (STRNCMP(p, "..=", 3) == 0)
1224 return 3;
1225 return 0;
1226}
1227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001229 * Generate the load instruction for "name".
1230 */
1231 static void
Bram Moolenaard505d172022-12-18 21:42:55 +00001232generate_loadvar(cctx_T *cctx, lhs_T *lhs)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001233{
Bram Moolenaard505d172022-12-18 21:42:55 +00001234 char_u *name = lhs->lhs_name;
1235 type_T *type = lhs->lhs_type;
1236
1237 switch (lhs->lhs_dest)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001238 {
1239 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001240 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001241 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1242 break;
1243 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01001244 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001245 {
1246 if (name[2] == NUL)
1247 generate_instr_type(cctx, ISN_LOADGDICT, &t_dict_any);
1248 else
1249 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
1250 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01001251 else
1252 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001253 break;
1254 case dest_buffer:
1255 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
1256 break;
1257 case dest_window:
1258 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
1259 break;
1260 case dest_tab:
1261 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
1262 break;
1263 case dest_script:
1264 compile_load_scriptvar(cctx,
Bram Moolenaard0132f42022-05-12 11:05:40 +01001265 name + (name[1] == ':' ? 2 : 0), NULL, NULL);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001266 break;
1267 case dest_env:
1268 // Include $ in the name here
1269 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
1270 break;
1271 case dest_reg:
1272 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
1273 break;
1274 case dest_vimvar:
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001275 generate_LOADV(cctx, name + 2);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001276 break;
1277 case dest_local:
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001278 if (cctx->ctx_skip != SKIP_YES)
1279 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001280 lvar_T *lvar = lhs->lhs_lvar;
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001281 if (lvar->lv_from_outer > 0)
1282 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
Bram Moolenaar65449bd2022-09-19 16:02:43 +01001283 lvar->lv_loop_depth, lvar->lv_loop_idx, type);
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001284 else
1285 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
1286 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001287 break;
Bram Moolenaard505d172022-12-18 21:42:55 +00001288 case dest_class_member:
1289 generate_CLASSMEMBER(cctx, TRUE, lhs->lhs_class,
1290 lhs->lhs_classmember_idx);
1291 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001292 case dest_expr:
1293 // list or dict value should already be on the stack.
1294 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001295 }
1296}
1297
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001298/*
1299 * Skip over "[expr]" or ".member".
1300 * Does not check for any errors.
1301 */
1302 static char_u *
1303skip_index(char_u *start)
1304{
1305 char_u *p = start;
1306
1307 if (*p == '[')
1308 {
1309 p = skipwhite(p + 1);
1310 (void)skip_expr(&p, NULL);
1311 p = skipwhite(p);
1312 if (*p == ']')
1313 return p + 1;
1314 return p;
1315 }
1316 // if (*p == '.')
1317 return to_name_end(p + 1, TRUE);
1318}
1319
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001320 void
1321vim9_declare_error(char_u *name)
1322{
1323 char *scope = "";
1324
1325 switch (*name)
1326 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001327 case 'g': scope = _("global"); break;
1328 case 'b': scope = _("buffer"); break;
1329 case 'w': scope = _("window"); break;
1330 case 't': scope = _("tab"); break;
1331 case 'v': scope = "v:"; break;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001332 case '$': semsg(_(e_cannot_declare_an_environment_variable_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001333 return;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001334 case '&': semsg(_(e_cannot_declare_an_option_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001335 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001336 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001337 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001338 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001339 }
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001340 semsg(_(e_cannot_declare_a_scope_variable_str), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001341}
1342
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001343/*
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001344 * Return TRUE if "name" is a valid register to use.
1345 * Return FALSE and give an error message if not.
1346 */
1347 static int
1348valid_dest_reg(int name)
1349{
1350 if ((name == '@' || valid_yank_reg(name, FALSE)) && name != '.')
1351 return TRUE;
1352 emsg_invreg(name);
1353 return FAIL;
1354}
1355
1356/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001357 * For one assignment figure out the type of destination. Return it in "dest".
1358 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001359 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001360 * For a v:var "vimvaridx" is set.
1361 * "type" is set to the destination type if known, unchanted otherwise.
1362 * Return FAIL if an error message was given.
1363 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001364 int
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001365get_var_dest(
1366 char_u *name,
1367 assign_dest_T *dest,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001368 cmdidx_T cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001369 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001370 int *vimvaridx,
1371 type_T **type,
1372 cctx_T *cctx)
1373{
1374 char_u *p;
1375
1376 if (*name == '&')
1377 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001378 int cc;
1379 long numval;
1380 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001381 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001382
1383 *dest = dest_option;
1384 if (cmdidx == CMD_final || cmdidx == CMD_const)
1385 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001386 emsg(_(e_cannot_lock_option));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001387 return FAIL;
1388 }
1389 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001390 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001391 if (p == NULL)
1392 {
1393 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02001394 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001395 return FAIL;
1396 }
1397 cc = *p;
1398 *p = NUL;
1399 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001400 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001401 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001402 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001403 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001404 case gov_unknown:
Bram Moolenaare1242042021-12-16 20:56:57 +00001405 semsg(_(e_unknown_option_str), name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001406 return FAIL;
1407 case gov_string:
1408 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001409 if (opt_p_flags & P_FUNC)
1410 {
1411 // might be a Funcref, check the type later
1412 *type = &t_any;
1413 *dest = dest_func_option;
1414 }
1415 else
1416 {
1417 *type = &t_string;
1418 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001419 break;
1420 case gov_bool:
1421 case gov_hidden_bool:
1422 *type = &t_bool;
1423 break;
1424 case gov_number:
1425 case gov_hidden_number:
1426 *type = &t_number;
1427 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001428 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001429 }
1430 else if (*name == '$')
1431 {
1432 *dest = dest_env;
1433 *type = &t_string;
1434 }
1435 else if (*name == '@')
1436 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001437 if (!valid_dest_reg(name[1]))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001438 return FAIL;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001439 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02001440 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001441 }
1442 else if (STRNCMP(name, "g:", 2) == 0)
1443 {
1444 *dest = dest_global;
1445 }
1446 else if (STRNCMP(name, "b:", 2) == 0)
1447 {
1448 *dest = dest_buffer;
1449 }
1450 else if (STRNCMP(name, "w:", 2) == 0)
1451 {
1452 *dest = dest_window;
1453 }
1454 else if (STRNCMP(name, "t:", 2) == 0)
1455 {
1456 *dest = dest_tab;
1457 }
1458 else if (STRNCMP(name, "v:", 2) == 0)
1459 {
1460 typval_T *vtv;
1461 int di_flags;
1462
1463 *vimvaridx = find_vim_var(name + 2, &di_flags);
1464 if (*vimvaridx < 0)
1465 {
1466 semsg(_(e_variable_not_found_str), name);
1467 return FAIL;
1468 }
1469 // We use the current value of "sandbox" here, is that OK?
1470 if (var_check_ro(di_flags, name, FALSE))
1471 return FAIL;
1472 *dest = dest_vimvar;
1473 vtv = get_vim_var_tv(*vimvaridx);
1474 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
1475 }
1476 return OK;
1477}
1478
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001479 static int
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001480is_decl_command(cmdidx_T cmdidx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001481{
1482 return cmdidx == CMD_let || cmdidx == CMD_var
1483 || cmdidx == CMD_final || cmdidx == CMD_const;
1484}
1485
1486/*
1487 * Figure out the LHS type and other properties for an assignment or one item
1488 * of ":unlet" with an index.
1489 * Returns OK or FAIL.
1490 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001491 int
Bram Moolenaar752fc692021-01-04 21:57:11 +01001492compile_lhs(
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001493 char_u *var_start,
1494 lhs_T *lhs,
1495 cmdidx_T cmdidx,
1496 int heredoc,
1497 int has_cmd, // "var" before "var_start"
1498 int oplen,
1499 cctx_T *cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001500{
1501 char_u *var_end;
1502 int is_decl = is_decl_command(cmdidx);
1503
1504 CLEAR_POINTER(lhs);
1505 lhs->lhs_dest = dest_local;
1506 lhs->lhs_vimvaridx = -1;
1507 lhs->lhs_scriptvar_idx = -1;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001508 lhs->lhs_member_idx = -1;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001509
1510 // "dest_end" is the end of the destination, including "[expr]" or
1511 // ".name".
1512 // "var_end" is the end of the variable/option/etc. name.
1513 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
1514 if (*var_start == '@')
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001515 {
1516 if (!valid_dest_reg(var_start[1]))
1517 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001518 var_end = var_start + 2;
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001519 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001520 else
1521 {
1522 // skip over the leading "&", "&l:", "&g:" and "$"
1523 var_end = skip_option_env_lead(var_start);
1524 var_end = to_name_end(var_end, TRUE);
1525 }
1526
1527 // "a: type" is declaring variable "a" with a type, not dict "a:".
1528 if (is_decl && lhs->lhs_dest_end == var_start + 2
1529 && lhs->lhs_dest_end[-1] == ':')
1530 --lhs->lhs_dest_end;
1531 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
1532 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001533 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001534
1535 // compute the length of the destination without "[expr]" or ".name"
1536 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02001537 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001538 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
1539 if (lhs->lhs_name == NULL)
1540 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01001541
1542 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
1543 // Something follows after the variable: "var[idx]" or "var.key".
1544 lhs->lhs_has_index = TRUE;
1545
Bram Moolenaar752fc692021-01-04 21:57:11 +01001546 if (heredoc)
1547 lhs->lhs_type = &t_list_string;
1548 else
1549 lhs->lhs_type = &t_any;
1550
1551 if (cctx->ctx_skip != SKIP_YES)
1552 {
1553 int declare_error = FALSE;
1554
1555 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
1556 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
1557 &lhs->lhs_type, cctx) == FAIL)
1558 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02001559 if (lhs->lhs_dest != dest_local
1560 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001561 {
1562 // Specific kind of variable recognized.
1563 declare_error = is_decl;
1564 }
1565 else
1566 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01001567 // No specific kind of variable recognized, just a name.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001568 if (check_reserved_name(lhs->lhs_name, cctx) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001569 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001570
1571 if (lookup_local(var_start, lhs->lhs_varlen,
1572 &lhs->lhs_local_lvar, cctx) == OK)
Bram Moolenaard505d172022-12-18 21:42:55 +00001573 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01001574 lhs->lhs_lvar = &lhs->lhs_local_lvar;
Bram Moolenaard505d172022-12-18 21:42:55 +00001575 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001576 else
1577 {
1578 CLEAR_FIELD(lhs->lhs_arg_lvar);
1579 if (arg_exists(var_start, lhs->lhs_varlen,
1580 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
1581 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
1582 {
1583 if (is_decl)
1584 {
1585 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
1586 return FAIL;
1587 }
1588 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
1589 }
1590 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001591
Bram Moolenaar752fc692021-01-04 21:57:11 +01001592 if (lhs->lhs_lvar != NULL)
1593 {
1594 if (is_decl)
1595 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001596 semsg(_(e_variable_already_declared_str), lhs->lhs_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001597 return FAIL;
1598 }
1599 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001600 else if ((lhs->lhs_classmember_idx = class_member_index(
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001601 var_start, lhs->lhs_varlen, NULL, cctx)) >= 0)
Bram Moolenaard505d172022-12-18 21:42:55 +00001602 {
Bram Moolenaar6acf7572023-01-01 19:53:30 +00001603 if (is_decl)
1604 {
1605 semsg(_(e_variable_already_declared_in_class_str),
1606 lhs->lhs_name);
1607 return FAIL;
1608 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001609 lhs->lhs_dest = dest_class_member;
1610 lhs->lhs_class = cctx->ctx_ufunc->uf_class;
1611 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001612 else
1613 {
1614 int script_namespace = lhs->lhs_varlen > 1
1615 && STRNCMP(var_start, "s:", 2) == 0;
1616 int script_var = (script_namespace
1617 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaardce24412022-02-08 20:35:30 +00001618 cctx, NULL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001619 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaardce24412022-02-08 20:35:30 +00001620 cctx, NULL)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001621 imported_T *import =
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001622 find_imported(var_start, lhs->lhs_varlen, FALSE);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001623
1624 if (script_namespace || script_var || import != NULL)
1625 {
1626 char_u *rawname = lhs->lhs_name
1627 + (lhs->lhs_name[1] == ':' ? 2 : 0);
1628
Bram Moolenaarafa048f2022-02-22 20:43:36 +00001629 if (script_namespace && current_script_is_vim9())
1630 {
1631 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
1632 var_start);
1633 return FAIL;
1634 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001635 if (is_decl)
1636 {
1637 if (script_namespace)
Bram Moolenaara749a422022-02-12 19:52:25 +00001638 semsg(_(e_cannot_declare_script_variable_in_function_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01001639 lhs->lhs_name);
1640 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001641 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01001642 lhs->lhs_name);
1643 return FAIL;
1644 }
1645 else if (cctx->ctx_ufunc->uf_script_ctx_version
1646 == SCRIPT_VERSION_VIM9
1647 && script_namespace
1648 && !script_var && import == NULL)
1649 {
1650 semsg(_(e_unknown_variable_str), lhs->lhs_name);
1651 return FAIL;
1652 }
1653
1654 lhs->lhs_dest = dest_script;
1655
1656 // existing script-local variables should have a type
1657 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
1658 if (import != NULL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001659 {
1660 char_u *dot = vim_strchr(var_start, '.');
1661 char_u *p;
1662
1663 // for an import the name is what comes after the dot
1664 if (dot == NULL)
1665 {
1666 semsg(_(e_no_dot_after_imported_name_str),
1667 var_start);
1668 return FAIL;
1669 }
1670 p = skipwhite(dot + 1);
1671 var_end = to_name_end(p, TRUE);
1672 if (var_end == p)
1673 {
1674 semsg(_(e_missing_name_after_imported_name_str),
1675 var_start);
1676 return FAIL;
1677 }
1678 vim_free(lhs->lhs_name);
1679 lhs->lhs_varlen = var_end - p;
1680 lhs->lhs_name = vim_strnsave(p, lhs->lhs_varlen);
1681 if (lhs->lhs_name == NULL)
1682 return FAIL;
1683 rawname = lhs->lhs_name;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001684 lhs->lhs_scriptvar_sid = import->imp_sid;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001685 // TODO: where do we check this name is exported?
1686
1687 // Check if something follows: "exp.var[idx]" or
1688 // "exp.var.key".
1689 lhs->lhs_has_index = lhs->lhs_dest_end
1690 > skipwhite(var_end);
1691 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001692 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
1693 {
Bram Moolenaar08251752021-01-11 21:20:18 +01001694 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01001695 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01001696 lhs->lhs_scriptvar_sid, rawname,
1697 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001698 cctx, NULL);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001699 if (lhs->lhs_scriptvar_idx >= 0)
1700 {
1701 scriptitem_T *si = SCRIPT_ITEM(
1702 lhs->lhs_scriptvar_sid);
1703 svar_T *sv =
1704 ((svar_T *)si->sn_var_vals.ga_data)
1705 + lhs->lhs_scriptvar_idx;
1706 lhs->lhs_type = sv->sv_type;
1707 }
1708 }
1709 }
Bram Moolenaardce24412022-02-08 20:35:30 +00001710 else if (check_defined(var_start, lhs->lhs_varlen, cctx,
1711 NULL, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001712 return FAIL;
1713 }
1714 }
1715
1716 if (declare_error)
1717 {
1718 vim9_declare_error(lhs->lhs_name);
1719 return FAIL;
1720 }
1721 }
1722
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001723 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01001724 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
1725 var_end = lhs->lhs_dest_end;
1726
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001727 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001728 {
1729 if (is_decl && *var_end == ':')
1730 {
1731 char_u *p;
1732
1733 // parse optional type: "let var: type = expr"
1734 if (!VIM_ISWHITE(var_end[1]))
1735 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001736 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001737 return FAIL;
1738 }
1739 p = skipwhite(var_end + 1);
1740 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
1741 if (lhs->lhs_type == NULL)
1742 return FAIL;
1743 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001744 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001745 }
1746 else if (lhs->lhs_lvar != NULL)
1747 lhs->lhs_type = lhs->lhs_lvar->lv_type;
1748 }
1749
Bram Moolenaare42939a2021-04-05 17:11:17 +02001750 if (oplen == 3 && !heredoc
1751 && lhs->lhs_dest != dest_global
1752 && !lhs->lhs_has_index
1753 && lhs->lhs_type->tt_type != VAR_STRING
1754 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001755 {
1756 emsg(_(e_can_only_concatenate_to_string));
1757 return FAIL;
1758 }
1759
1760 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
1761 && cctx->ctx_skip != SKIP_YES)
1762 {
1763 if (oplen > 1 && !heredoc)
1764 {
1765 // +=, /=, etc. require an existing variable
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001766 semsg(_(e_cannot_use_operator_on_new_variable_str), lhs->lhs_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001767 return FAIL;
1768 }
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001769 if (!is_decl || (lhs->lhs_has_index && !has_cmd
1770 && cctx->ctx_skip != SKIP_YES))
Bram Moolenaar752fc692021-01-04 21:57:11 +01001771 {
1772 semsg(_(e_unknown_variable_str), lhs->lhs_name);
1773 return FAIL;
1774 }
1775
Bram Moolenaar3f327882021-03-17 20:56:38 +01001776 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01001777 if ((lhs->lhs_type->tt_type == VAR_FUNC
1778 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01001779 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01001780 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01001781
1782 // New local variable.
Bram Moolenaar6586a012022-09-30 11:04:50 +01001783 int assign = cmdidx == CMD_final ? ASSIGN_FINAL
1784 : cmdidx == CMD_const ? ASSIGN_CONST : ASSIGN_VAR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001785 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
Bram Moolenaar6586a012022-09-30 11:04:50 +01001786 assign, lhs->lhs_type);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001787 if (lhs->lhs_lvar == NULL)
1788 return FAIL;
1789 lhs->lhs_new_local = TRUE;
1790 }
1791
1792 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01001793 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001794 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00001795 char_u *after = var_start + lhs->lhs_varlen;
1796 char_u *p;
1797
Bram Moolenaar752fc692021-01-04 21:57:11 +01001798 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001799 if (is_decl && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001800 {
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001801 if (has_cmd)
1802 emsg(_(e_cannot_use_index_when_declaring_variable));
1803 else
1804 semsg(_(e_unknown_variable_str), lhs->lhs_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001805 return FAIL;
1806 }
1807
Bram Moolenaarc750d912021-11-29 22:02:12 +00001808 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
1809 // Only the last index is used below, if there are others
1810 // before it generate code for the expression. Thus for
1811 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
1812 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001813 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00001814 p = skip_index(after);
1815 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01001816 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00001817 lhs->lhs_varlen_total = p - var_start;
1818 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001819 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00001820 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001821 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00001822 if (after > var_start + lhs->lhs_varlen)
1823 {
1824 lhs->lhs_varlen = after - var_start;
1825 lhs->lhs_dest = dest_expr;
1826 // We don't know the type before evaluating the expression,
1827 // use "any" until then.
1828 lhs->lhs_type = &t_any;
1829 }
1830
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001831 if (lhs->lhs_type == NULL || lhs->lhs_type->tt_member == NULL)
Bram Moolenaarc750d912021-11-29 22:02:12 +00001832 lhs->lhs_member_type = &t_any;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001833 else if (lhs->lhs_type->tt_type == VAR_CLASS
1834 || lhs->lhs_type->tt_type == VAR_OBJECT)
1835 {
1836 // for an object or class member get the type of the member
1837 class_T *cl = (class_T *)lhs->lhs_type->tt_member;
1838 lhs->lhs_member_type = class_member_type(cl, after + 1,
1839 lhs->lhs_end, &lhs->lhs_member_idx);
Bram Moolenaarf54cedd2022-12-23 17:56:27 +00001840 if (lhs->lhs_member_idx < 0)
1841 return FAIL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001842 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001843 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00001844 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001845 }
1846 return OK;
1847}
1848
1849/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001850 * Figure out the LHS and check a few errors.
1851 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001852 int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001853compile_assign_lhs(
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001854 char_u *var_start,
1855 lhs_T *lhs,
1856 cmdidx_T cmdidx,
1857 int is_decl,
1858 int heredoc,
1859 int has_cmd, // "var" before "var_start"
1860 int oplen,
1861 cctx_T *cctx)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001862{
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001863 if (compile_lhs(var_start, lhs, cmdidx, heredoc, has_cmd, oplen, cctx)
1864 == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001865 return FAIL;
1866
1867 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
1868 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001869 semsg(_(e_cannot_assign_to_argument_str), lhs->lhs_name);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001870 return FAIL;
1871 }
1872 if (!is_decl && lhs->lhs_lvar != NULL
Bram Moolenaar6586a012022-09-30 11:04:50 +01001873 && lhs->lhs_lvar->lv_const != ASSIGN_VAR
1874 && !lhs->lhs_has_index)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001875 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001876 semsg(_(e_cannot_assign_to_constant_str), lhs->lhs_name);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001877 return FAIL;
1878 }
1879 return OK;
1880}
1881
1882/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001883 * Return TRUE if "lhs" has a range index: "[expr : expr]".
1884 */
1885 static int
1886has_list_index(char_u *idx_start, cctx_T *cctx)
1887{
1888 char_u *p = idx_start;
1889 int save_skip;
1890
1891 if (*p != '[')
1892 return FALSE;
1893
1894 p = skipwhite(p + 1);
1895 if (*p == ':')
1896 return TRUE;
1897
1898 save_skip = cctx->ctx_skip;
1899 cctx->ctx_skip = SKIP_YES;
1900 (void)compile_expr0(&p, cctx);
1901 cctx->ctx_skip = save_skip;
1902 return *skipwhite(p) == ':';
1903}
1904
1905/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02001906 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
1907 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01001908 */
1909 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02001910compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01001911 char_u *var_start,
1912 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02001913 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01001914 cctx_T *cctx)
1915{
Bram Moolenaar752fc692021-01-04 21:57:11 +01001916 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02001917 char_u *p;
1918 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02001919 int need_white_before = TRUE;
1920 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001921
Bram Moolenaar752fc692021-01-04 21:57:11 +01001922 p = var_start + varlen;
1923 if (*p == '[')
1924 {
1925 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02001926 if (*p == ':')
1927 {
1928 // empty first index, push zero
1929 r = generate_PUSHNR(cctx, 0);
1930 need_white_before = FALSE;
1931 }
1932 else
1933 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001934
1935 if (r == OK && *skipwhite(p) == ':')
1936 {
1937 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02001938 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02001939 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001940 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02001941 empty_second = *skipwhite(p + 1) == ']';
1942 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
1943 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001944 {
1945 semsg(_(e_white_space_required_before_and_after_str_at_str),
1946 ":", p);
1947 return FAIL;
1948 }
1949 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02001950 if (*p == ']')
1951 // empty second index, push "none"
1952 r = generate_PUSHSPEC(cctx, VVAL_NONE);
1953 else
1954 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001955 }
1956
Bram Moolenaar752fc692021-01-04 21:57:11 +01001957 if (r == OK && *skipwhite(p) != ']')
1958 {
1959 // this should not happen
Bram Moolenaare1242042021-12-16 20:56:57 +00001960 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar752fc692021-01-04 21:57:11 +01001961 r = FAIL;
1962 }
1963 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001964 else if (lhs->lhs_member_idx >= 0)
1965 {
1966 // object member index
1967 r = generate_PUSHNR(cctx, lhs->lhs_member_idx);
1968 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001969 else // if (*p == '.')
1970 {
1971 char_u *key_end = to_name_end(p + 1, TRUE);
1972 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
1973
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001974 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001975 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02001976 return r;
1977}
1978
1979/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001980 * For a LHS with an index, load the variable to be indexed.
1981 */
1982 static int
1983compile_load_lhs(
1984 lhs_T *lhs,
1985 char_u *var_start,
1986 type_T *rhs_type,
1987 cctx_T *cctx)
1988{
1989 if (lhs->lhs_dest == dest_expr)
1990 {
1991 size_t varlen = lhs->lhs_varlen;
1992 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02001993 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001994 char_u *p = var_start;
Bram Moolenaare97976b2021-08-01 13:17:17 +02001995 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001996
Bram Moolenaare97976b2021-08-01 13:17:17 +02001997 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
1998 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001999 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002000 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
2001 res = compile_expr0(&p, cctx);
2002 var_start[varlen] = c;
2003 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
2004 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002005 {
2006 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02002007 if (res != FAIL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002008 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002009 return FAIL;
2010 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002011
Bram Moolenaar078a4612022-01-04 15:17:03 +00002012 lhs->lhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2013 : get_type_on_stack(cctx, 0);
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002014 // now we can properly check the type
2015 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
2016 && rhs_type != &t_void
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002017 && need_type(rhs_type, lhs->lhs_type->tt_member, FALSE,
2018 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002019 return FAIL;
2020 }
2021 else
Bram Moolenaard505d172022-12-18 21:42:55 +00002022 generate_loadvar(cctx, lhs);
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002023 return OK;
2024}
2025
2026/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002027 * Produce code for loading "lhs" and also take care of an index.
2028 * Return OK/FAIL.
2029 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002030 int
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002031compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
2032{
2033 compile_load_lhs(lhs, var_start, NULL, cctx);
2034
2035 if (lhs->lhs_has_index)
2036 {
2037 int range = FALSE;
2038
2039 // Get member from list or dict. First compile the
2040 // index value.
2041 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
2042 return FAIL;
2043 if (range)
2044 {
2045 semsg(_(e_cannot_use_range_with_assignment_operator_str),
2046 var_start);
2047 return FAIL;
2048 }
2049
2050 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002051 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002052 return FAIL;
2053 }
2054 return OK;
2055}
2056
2057/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002058 * Assignment to a list or dict member, or ":unlet" for the item, using the
2059 * information in "lhs".
2060 * Returns OK or FAIL.
2061 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002062 int
Bram Moolenaare42939a2021-04-05 17:11:17 +02002063compile_assign_unlet(
2064 char_u *var_start,
2065 lhs_T *lhs,
2066 int is_assign,
2067 type_T *rhs_type,
2068 cctx_T *cctx)
2069{
Bram Moolenaare42939a2021-04-05 17:11:17 +02002070 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002071 int range = FALSE;
2072
Bram Moolenaar68452172021-04-12 21:21:02 +02002073 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002074 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002075 if (is_assign && range
2076 && lhs->lhs_type->tt_type != VAR_LIST
2077 && lhs->lhs_type != &t_blob
2078 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02002079 {
2080 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
2081 return FAIL;
2082 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002083
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002084 if (lhs->lhs_type == NULL || lhs->lhs_type == &t_any)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002085 {
2086 // Index on variable of unknown type: check at runtime.
2087 dest_type = VAR_ANY;
2088 }
2089 else
2090 {
2091 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002092 if (dest_type == VAR_DICT && range)
2093 {
2094 emsg(e_cannot_use_range_with_dictionary);
2095 return FAIL;
2096 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002097 if (dest_type == VAR_DICT
2098 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002099 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002100 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002101 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002102 type_T *type;
2103
2104 if (range)
2105 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00002106 type = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002107 if (need_type(type, &t_number, FALSE,
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002108 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002109 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002110 }
Bram Moolenaar078a4612022-01-04 15:17:03 +00002111 type = get_type_on_stack(cctx, 0);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00002112 if ((dest_type != VAR_BLOB && type->tt_type != VAR_SPECIAL)
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002113 && need_type(type, &t_number, FALSE,
Bram Moolenaar51e93322021-04-17 20:44:56 +02002114 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002115 return FAIL;
2116 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002117 }
2118
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01002119 if (cctx->ctx_skip == SKIP_YES)
2120 return OK;
2121
Bram Moolenaar752fc692021-01-04 21:57:11 +01002122 // Load the dict or list. On the stack we then have:
2123 // - value (for assignment, not for :unlet)
2124 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002125 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01002126 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002127 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
2128 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002129
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002130 if (dest_type == VAR_LIST
2131 || dest_type == VAR_DICT
2132 || dest_type == VAR_BLOB
2133 || dest_type == VAR_CLASS
2134 || dest_type == VAR_OBJECT
2135 || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002136 {
2137 if (is_assign)
2138 {
Bram Moolenaar68452172021-04-12 21:21:02 +02002139 if (range)
2140 {
2141 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
2142 return FAIL;
2143 }
2144 else
2145 {
2146 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002147
Bram Moolenaar68452172021-04-12 21:21:02 +02002148 if (isn == NULL)
2149 return FAIL;
2150 isn->isn_arg.vartype = dest_type;
2151 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002152 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002153 else if (range)
2154 {
2155 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
2156 return FAIL;
2157 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002158 else
2159 {
2160 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
2161 return FAIL;
2162 }
2163 }
2164 else
2165 {
2166 emsg(_(e_indexable_type_required));
2167 return FAIL;
2168 }
2169
2170 return OK;
2171}
2172
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002173/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002174 * Generate an instruction to push the default value for "vartype".
2175 * if "dest_local" is TRUE then for some types no instruction is generated.
2176 * "skip_store" is set to TRUE if no PUSH instruction is generated.
2177 * Returns OK or FAIL.
2178 */
2179 static int
2180push_default_value(
2181 cctx_T *cctx,
2182 vartype_T vartype,
2183 int dest_is_local,
2184 int *skip_store)
2185{
2186 int r = OK;
2187
2188 switch (vartype)
2189 {
2190 case VAR_BOOL:
2191 r = generate_PUSHBOOL(cctx, VVAL_FALSE);
2192 break;
2193 case VAR_FLOAT:
2194 r = generate_PUSHF(cctx, 0.0);
2195 break;
2196 case VAR_STRING:
2197 r = generate_PUSHS(cctx, NULL);
2198 break;
2199 case VAR_BLOB:
2200 r = generate_PUSHBLOB(cctx, blob_alloc());
2201 break;
2202 case VAR_FUNC:
2203 r = generate_PUSHFUNC(cctx, NULL, &t_func_void, TRUE);
2204 break;
2205 case VAR_LIST:
2206 r = generate_NEWLIST(cctx, 0, FALSE);
2207 break;
2208 case VAR_DICT:
2209 r = generate_NEWDICT(cctx, 0, FALSE);
2210 break;
2211 case VAR_JOB:
2212 r = generate_PUSHJOB(cctx);
2213 break;
2214 case VAR_CHANNEL:
2215 r = generate_PUSHCHANNEL(cctx);
2216 break;
2217 case VAR_NUMBER:
2218 case VAR_UNKNOWN:
2219 case VAR_ANY:
2220 case VAR_PARTIAL:
2221 case VAR_VOID:
2222 case VAR_INSTR:
2223 case VAR_CLASS:
2224 case VAR_OBJECT:
2225 case VAR_SPECIAL: // cannot happen
2226 // This is skipped for local variables, they are always
2227 // initialized to zero. But in a "for" or "while" loop
2228 // the value may have been changed.
2229 if (dest_is_local && !inside_loop_scope(cctx))
2230 *skip_store = TRUE;
2231 else
2232 r = generate_PUSHNR(cctx, 0);
2233 break;
2234 }
2235 return r;
2236}
2237
2238/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002239 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002240 * "let name"
2241 * "var name = expr"
2242 * "final name = expr"
2243 * "const name = expr"
2244 * "name = expr"
2245 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002246 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002247 * Return NULL for an error.
2248 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 */
2250 static char_u *
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002251compile_assignment(
2252 char_u *arg_start,
2253 exarg_T *eap,
2254 cmdidx_T cmdidx,
2255 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256{
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002257 char_u *arg = arg_start;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002258 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002260 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 char_u *ret = NULL;
2262 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002263 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02002265 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002267 int jump_instr_idx = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 int oplen = 0;
2270 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002271 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002272 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002274 int is_decl = is_decl_command(cmdidx);
2275 lhs_T lhs;
Bram Moolenaarf593fc82022-12-14 13:50:02 +00002276 CLEAR_FIELD(lhs);
Bram Moolenaar77709b12021-04-03 21:01:01 +02002277 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278
Bram Moolenaar6acf7572023-01-01 19:53:30 +00002279 int has_arg_is_set_prefix = STRNCMP(arg, "ifargisset ", 11) == 0;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002280 if (has_arg_is_set_prefix)
2281 {
2282 arg += 11;
2283 int def_idx = getdigits(&arg);
2284 arg = skipwhite(arg);
2285
2286 // Use a JUMP_IF_ARG_NOT_SET instruction to skip if the value was not
2287 // given and the default value is "v:none".
2288 int off = STACK_FRAME_SIZE + (cctx->ctx_ufunc->uf_va_name != NULL
2289 ? 1 : 0);
2290 int count = cctx->ctx_ufunc->uf_def_args.ga_len;
2291 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_NOT_SET,
2292 def_idx - count - off) == FAIL)
2293 goto theend;
2294 }
2295
Bram Moolenaare1d12112022-03-05 11:37:48 +00002296 // Skip over the "varname" or "[varname, varname]" to get to any "=".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002297 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
2298 if (p == NULL)
2299 return *arg == '[' ? arg : NULL;
2300
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002301 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
2302 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02002303 if (VIM_ISWHITE(eap->cmd[2]))
2304 {
2305 semsg(_(e_no_white_space_allowed_after_str_str),
2306 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
2307 return NULL;
2308 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002309 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
2310 oplen = 2;
2311 incdec = TRUE;
2312 }
Bram Moolenaar70d87692022-05-07 10:03:27 +01002313 else
2314 {
2315 sp = p;
2316 p = skipwhite(p);
2317 op = p;
2318 oplen = assignment_len(p, &heredoc);
2319
2320 if (var_count > 0 && oplen == 0)
2321 // can be something like "[1, 2]->func()"
2322 return arg;
2323
2324 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
2325 {
2326 error_white_both(op, oplen);
2327 return NULL;
2328 }
2329 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002331 if (heredoc)
2332 {
2333 list_T *l;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334
2335 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02002336 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 eap->cookie = cctx;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01002338 l = heredoc_get(eap, op + 3, FALSE, TRUE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02002339 if (l == NULL)
2340 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 list_free(l);
2343 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002344 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002346 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01002348 char_u *wp;
2349
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002350 // for "[var, var] = expr" evaluate the expression here, loop over the
2351 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01002352 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02002353
Bram Moolenaar004d9b02020-11-30 21:40:03 +01002354 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002355 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01002356 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002357 if (compile_expr0(&p, cctx) == FAIL)
2358 return NULL;
2359 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002360
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002361 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002363 type_T *stacktype;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00002364 int needed_list_len;
2365 int did_check = FALSE;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002366
Bram Moolenaar078a4612022-01-04 15:17:03 +00002367 stacktype = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2368 : get_type_on_stack(cctx, 0);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002369 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002371 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002372 goto theend;
2373 }
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002374 if (need_type(stacktype, &t_list_any, FALSE, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002375 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002376 goto theend;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00002377 // If a constant list was used we can check the length right here.
2378 needed_list_len = semicolon ? var_count - 1 : var_count;
2379 if (instr->ga_len > 0)
2380 {
2381 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2382
2383 if (isn->isn_type == ISN_NEWLIST)
2384 {
2385 did_check = TRUE;
2386 if (semicolon ? isn->isn_arg.number < needed_list_len
2387 : isn->isn_arg.number != needed_list_len)
2388 {
2389 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaarf60a6342022-01-15 14:16:37 +00002390 needed_list_len, (int)isn->isn_arg.number);
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00002391 goto theend;
2392 }
2393 }
2394 }
2395 if (!did_check)
2396 generate_CHECKLEN(cctx, needed_list_len, semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002397 if (stacktype->tt_member != NULL)
2398 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002399 }
2400 }
2401
2402 /*
2403 * Loop over variables in "[var, var] = expr".
2404 * For "var = expr" and "let var: type" this is done only once.
2405 */
2406 if (var_count > 0)
2407 var_start = skipwhite(arg + 1); // skip over the "["
2408 else
2409 var_start = arg;
2410 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
2411 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002412 int instr_count = -1;
2413 int save_lnum;
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002414 int skip_store = FALSE;
Bram Moolenaare88c6b72022-02-15 15:37:11 +00002415 type_T *inferred_type = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002416
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02002417 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
2418 {
2419 // Ignore underscore in "[a, _, b] = list".
2420 if (var_count > 0)
2421 {
2422 var_start = skipwhite(var_start + 2);
2423 continue;
2424 }
2425 emsg(_(e_cannot_use_underscore_here));
2426 goto theend;
2427 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002428 vim_free(lhs.lhs_name);
2429
2430 /*
2431 * Figure out the LHS type and other properties.
2432 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002433 if (compile_assign_lhs(var_start, &lhs, cmdidx,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002434 is_decl, heredoc, var_start > eap->cmd,
2435 oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002436 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02002437 if (heredoc)
2438 {
2439 SOURCING_LNUM = start_lnum;
2440 if (lhs.lhs_has_type
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002441 && need_type(&t_list_string, lhs.lhs_type, FALSE,
Bram Moolenaar81530e32021-07-28 21:25:49 +02002442 -1, 0, cctx, FALSE, FALSE) == FAIL)
2443 goto theend;
2444 }
2445 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002446 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002447 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002448 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002449 if (oplen > 0 && var_count == 0)
2450 {
2451 // skip over the "=" and the expression
2452 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02002453 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002454 }
2455 }
2456 else if (oplen > 0)
2457 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002458 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01002459 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002460
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02002461 // for "+=", "*=", "..=" etc. first load the current value
2462 if (*op != '='
2463 && compile_load_lhs_with_index(&lhs, var_start,
2464 cctx) == FAIL)
2465 goto theend;
2466
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002467 // For "var = expr" evaluate the expression.
2468 if (var_count == 0)
2469 {
2470 int r;
2471
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002472 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002473 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002474 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01002475 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002476 r = generate_PUSHNR(cctx, 1);
2477 }
2478 else
2479 {
2480 // Temporarily hide the new local variable here, it is
2481 // not available to this expression.
2482 if (lhs.lhs_new_local)
2483 --cctx->ctx_locals.ga_len;
2484 wp = op + oplen;
2485 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
2486 {
2487 if (lhs.lhs_new_local)
2488 ++cctx->ctx_locals.ga_len;
2489 goto theend;
2490 }
2491 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002492 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01002493 ++cctx->ctx_locals.ga_len;
Bram Moolenaar21e51222020-12-04 12:43:29 +01002494 }
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01002495 if (r == FAIL)
2496 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002497 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02002498 else if (semicolon && var_idx == var_count - 1)
2499 {
2500 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02002501 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002502 if (generate_SLICE(cctx, var_count - 1) == FAIL)
2503 goto theend;
2504 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002505 else
2506 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002507 // For "[var, var] = expr" get the "var_idx" item from the
2508 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02002509 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01002510 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002511 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002512
Bram Moolenaar078a4612022-01-04 15:17:03 +00002513 rhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2514 : get_type_on_stack(cctx, 0);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002515 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002516 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002517 if ((rhs_type->tt_type == VAR_FUNC
2518 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01002519 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01002520 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02002521 goto theend;
2522
Bram Moolenaar752fc692021-01-04 21:57:11 +01002523 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002524 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002525 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002526 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002527 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002528 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002529 }
2530 else
2531 {
Bram Moolenaarfa103972022-09-29 19:14:42 +01002532 type_T *type;
2533
Bram Moolenaar04bdd572020-09-23 13:25:32 +02002534 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002535 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002536 if (rhs_type == &t_list_empty)
Bram Moolenaarfa103972022-09-29 19:14:42 +01002537 type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002538 else if (rhs_type == &t_dict_empty)
Bram Moolenaarfa103972022-09-29 19:14:42 +01002539 type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002540 else if (rhs_type == &t_unknown)
Bram Moolenaarfa103972022-09-29 19:14:42 +01002541 type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002542 else
Bram Moolenaare88c6b72022-02-15 15:37:11 +00002543 {
Bram Moolenaarfa103972022-09-29 19:14:42 +01002544 type = rhs_type;
Bram Moolenaare88c6b72022-02-15 15:37:11 +00002545 inferred_type = rhs_type;
2546 }
Bram Moolenaarfa103972022-09-29 19:14:42 +01002547 set_var_type(lhs.lhs_lvar, type, cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002548 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002549 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002550 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002551 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002552 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02002553 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002554
Bram Moolenaar77709b12021-04-03 21:01:01 +02002555 // Without operator check type here, otherwise below.
2556 // Use the line number of the assignment.
2557 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02002558 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
2559 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002560 // If assigning to a list or dict member, use the
2561 // member type. Not for "list[:] =".
2562 if (lhs.lhs_has_index
2563 && !has_list_index(var_start + lhs.lhs_varlen,
2564 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002565 use_type = lhs.lhs_member_type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002566 if (need_type_where(rhs_type, use_type, FALSE, -1,
2567 where, cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002568 goto theend;
2569 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002570 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002571 else
2572 {
2573 type_T *lhs_type = lhs.lhs_member_type;
2574
2575 // Special case: assigning to @# can use a number or a
2576 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02002577 // Also: can assign a number to a float.
2578 if ((lhs_type == &t_number_or_string
2579 || lhs_type == &t_float)
2580 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002581 lhs_type = &t_number;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002582 if (*p != '=' && need_type(rhs_type, lhs_type, FALSE,
Bram Moolenaar351ead02021-01-16 16:07:01 +01002583 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002584 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002585 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002587 else if (cmdidx == CMD_final)
2588 {
2589 emsg(_(e_final_requires_a_value));
2590 goto theend;
2591 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002592 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002594 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002595 goto theend;
2596 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002597 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option
2598 || lhs.lhs_dest == dest_func_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002599 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002600 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002601 goto theend;
2602 }
2603 else
2604 {
2605 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02002606 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002607 goto theend;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002608 instr_count = instr->ga_len;
2609 int r = push_default_value(cctx, lhs.lhs_member_type->tt_type,
2610 lhs.lhs_dest == dest_local, &skip_store);
Bram Moolenaarc9af6172022-05-04 16:46:54 +01002611 if (r == FAIL)
2612 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002613 }
2614 if (var_count == 0)
2615 end = p;
2616 }
2617
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002618 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002619 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002620 break;
2621
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002622 if (oplen > 0 && *op != '=')
2623 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002624 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02002625 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002626
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002627 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002628 {
2629 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2630 goto theend;
2631 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002632 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002633 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002634 expected = lhs.lhs_member_type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00002635 stacktype = get_type_on_stack(cctx, 0);
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002636 if (
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002637 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02002638 !(expected == &t_float && (stacktype == &t_number
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002639 || stacktype == &t_number_bool))
2640 && need_type(stacktype, expected, TRUE, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002641 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002642 goto theend;
2643 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002644
2645 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02002646 {
LemonBoy372bcce2022-04-25 12:43:20 +01002647 if (generate_CONCAT(cctx, 2) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02002648 goto theend;
2649 }
2650 else if (*op == '+')
2651 {
2652 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01002653 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02002654 lhs.lhs_member_type, stacktype,
2655 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02002656 goto theend;
2657 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002658 else if (generate_two_op(cctx, op) == FAIL)
2659 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002662 // Use the line number of the assignment for store instruction.
2663 save_lnum = cctx->ctx_lnum;
2664 cctx->ctx_lnum = start_lnum - 1;
2665
Bram Moolenaar752fc692021-01-04 21:57:11 +01002666 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002667 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002668 // Use the info in "lhs" to store the value at the index in the
2669 // list or dict.
2670 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
2671 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002672 {
2673 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002674 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002675 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002676 }
2677 else
2678 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002679 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02002680 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01002681 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002682 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002683 generate_LOCKCONST(cctx);
2684
Bram Moolenaar10d6f182022-01-04 15:54:38 +00002685 if ((lhs.lhs_type->tt_type == VAR_DICT
Bram Moolenaar752fc692021-01-04 21:57:11 +01002686 || lhs.lhs_type->tt_type == VAR_LIST)
2687 && lhs.lhs_type->tt_member != NULL
Bram Moolenaare88c6b72022-02-15 15:37:11 +00002688 && lhs.lhs_type->tt_member != &t_any
Bram Moolenaar752fc692021-01-04 21:57:11 +01002689 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01002690 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar10d6f182022-01-04 15:54:38 +00002691 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01002692 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaare88c6b72022-02-15 15:37:11 +00002693 else if (inferred_type != NULL
2694 && (inferred_type->tt_type == VAR_DICT
2695 || inferred_type->tt_type == VAR_LIST)
2696 && inferred_type->tt_member != NULL
2697 && inferred_type->tt_member != &t_unknown
2698 && inferred_type->tt_member != &t_any)
2699 // Set the type in the list or dict, so that it can be checked,
2700 // also in legacy script.
2701 generate_SETTYPE(cctx, inferred_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01002702
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002703 if (!skip_store && generate_store_lhs(cctx, &lhs,
2704 instr_count, is_decl) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002705 {
2706 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002707 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002708 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002709 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002710 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002711
2712 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00002713 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002714
2715 if (has_arg_is_set_prefix)
2716 {
2717 // set instruction index in JUMP_IF_ARG_SET to here
2718 isn_T *isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
2719 isn->isn_arg.jumparg.jump_where = instr->ga_len;
2720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002722
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02002723 // For "[var, var] = expr" drop the "expr" value.
2724 // Also for "[var, var; _] = expr".
2725 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02002726 {
Bram Moolenaarec792292020-12-13 21:26:56 +01002727 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02002728 goto theend;
2729 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002730
Bram Moolenaarb2097502020-07-19 17:17:02 +02002731 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732
2733theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01002734 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 return ret;
2736}
2737
2738/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01002739 * Check for an assignment at "eap->cmd", compile it if found.
2740 * Return NOTDONE if there is none, FAIL for failure, OK if done.
2741 */
2742 static int
2743may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
2744{
2745 char_u *pskip;
2746 char_u *p;
2747
2748 // Assuming the command starts with a variable or function name,
2749 // find what follows.
2750 // Skip over "var.member", "var[idx]" and the like.
2751 // Also "&opt = val", "$ENV = val" and "@r = val".
2752 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
2753 ? eap->cmd + 1 : eap->cmd;
2754 p = to_name_end(pskip, TRUE);
2755 if (p > eap->cmd && *p != NUL)
2756 {
2757 char_u *var_end;
2758 int oplen;
2759 int heredoc;
2760
2761 if (eap->cmd[0] == '@')
2762 var_end = eap->cmd + 2;
2763 else
2764 var_end = find_name_end(pskip, NULL, NULL,
2765 FNE_CHECK_START | FNE_INCL_BR);
2766 oplen = assignment_len(skipwhite(var_end), &heredoc);
2767 if (oplen > 0)
2768 {
2769 size_t len = p - eap->cmd;
2770
2771 // Recognize an assignment if we recognize the variable
2772 // name:
Bram Moolenaar17126b12021-01-07 22:03:02 +01002773 // "&opt = expr"
2774 // "$ENV = expr"
2775 // "@r = expr"
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00002776 // "g:var = expr"
2777 // "g:[key] = expr"
2778 // "local = expr" where "local" is a local var.
2779 // "script = expr" where "script" is a script-local var.
2780 // "import = expr" where "import" is an imported var
Bram Moolenaar17126b12021-01-07 22:03:02 +01002781 if (*eap->cmd == '&'
2782 || *eap->cmd == '$'
2783 || *eap->cmd == '@'
2784 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00002785 || STRNCMP(eap->cmd, "g:[", 3) == 0
Bram Moolenaare0890d62021-02-17 14:52:14 +01002786 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002787 {
2788 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
2789 if (*line == NULL || *line == eap->cmd)
2790 return FAIL;
2791 return OK;
2792 }
2793 }
2794 }
2795
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002796 // might be "[var, var] = expr" or "ifargisset this.member = expr"
2797 if (*eap->cmd == '[' || STRNCMP(eap->cmd, "ifargisset ", 11) == 0)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002798 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01002799 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
2800 if (*line == NULL)
2801 return FAIL;
2802 if (*line != eap->cmd)
2803 return OK;
2804 }
2805 return NOTDONE;
2806}
2807
Bram Moolenaar9a015112021-12-31 14:06:45 +00002808/*
2809 * Check if arguments of "ufunc" shadow variables in "cctx".
2810 * Return OK or FAIL.
2811 */
2812 static int
2813check_args_shadowing(ufunc_T *ufunc, cctx_T *cctx)
2814{
2815 int i;
2816 char_u *arg;
2817 int r = OK;
2818
2819 // Make sure arguments are not found when compiling a second time.
2820 ufunc->uf_args_visible = 0;
2821
2822 // Check for arguments shadowing variables from the context.
2823 for (i = 0; i < ufunc->uf_args.ga_len; ++i)
2824 {
2825 arg = ((char_u **)(ufunc->uf_args.ga_data))[i];
Bram Moolenaardce24412022-02-08 20:35:30 +00002826 if (check_defined(arg, STRLEN(arg), cctx, NULL, TRUE) == FAIL)
Bram Moolenaar9a015112021-12-31 14:06:45 +00002827 {
2828 r = FAIL;
2829 break;
2830 }
2831 }
2832 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
2833 return r;
2834}
2835
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01002836#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar139575d2022-03-15 19:29:30 +00002837/*
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002838 * Get a count before a command. Can only be a number.
2839 * Returns zero if there is no count.
2840 * Returns -1 if there is something wrong.
2841 */
2842 static long
2843get_cmd_count(char_u *line, exarg_T *eap)
2844{
2845 char_u *p;
2846
2847 // skip over colons and white space
2848 for (p = line; *p == ':' || VIM_ISWHITE(*p); ++p)
2849 ;
2850 if (!isdigit(*p))
2851 {
Bram Moolenaar2435adf2022-10-21 12:05:46 +01002852 // The command or modifiers must be following. Assume a lower case
2853 // character means there is a modifier.
2854 if (p < eap->cmd && !vim_islower(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002855 {
2856 emsg(_(e_invalid_range));
2857 return -1;
2858 }
2859 return 0;
2860 }
2861 return atol((char *)p);
2862}
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01002863#endif
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002864
2865/*
Bram Moolenaar139575d2022-03-15 19:29:30 +00002866 * Get the compilation type that should be used for "ufunc".
2867 * Keep in sync with INSTRUCTIONS().
2868 */
2869 compiletype_T
2870get_compile_type(ufunc_T *ufunc)
2871{
2872 // Update uf_has_breakpoint if needed.
2873 update_has_breakpoint(ufunc);
2874
2875 if (debug_break_level > 0 || may_break_in_function(ufunc))
2876 return CT_DEBUG;
2877#ifdef FEAT_PROFILE
2878 if (do_profiling == PROF_YES)
2879 {
2880 if (!ufunc->uf_profiling && has_profiling(FALSE, ufunc->uf_name, NULL))
2881 func_do_profile(ufunc);
2882 if (ufunc->uf_profiling)
2883 return CT_PROFILE;
2884 }
2885#endif
2886 return CT_NONE;
2887}
2888
Bram Moolenaar7b829262021-10-13 15:04:34 +01002889
2890/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02002891 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002892 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02002893 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02002894 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02002895add_def_function(ufunc_T *ufunc)
2896{
2897 dfunc_T *dfunc;
2898
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002899 if (def_functions.ga_len == 0)
2900 {
2901 // The first position is not used, so that a zero uf_dfunc_idx means it
2902 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02002903 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002904 return FAIL;
2905 ++def_functions.ga_len;
2906 }
2907
Bram Moolenaar09689a02020-05-09 22:50:08 +02002908 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02002909 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02002910 return FAIL;
2911 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
2912 CLEAR_POINTER(dfunc);
2913 dfunc->df_idx = def_functions.ga_len;
2914 ufunc->uf_dfunc_idx = dfunc->df_idx;
2915 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002916 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002917 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002918 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02002919 ++def_functions.ga_len;
2920 return OK;
2921}
2922
2923/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 * After ex_function() has collected all the function lines: parse and compile
2925 * the lines into instructions.
2926 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01002927 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
2928 * the return statement (used for lambda). When uf_ret_type is already set
2929 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002930 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002931 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02002932 * This can be used recursively through compile_lambda(), which may reallocate
2933 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02002934 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02002936 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01002937compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02002938 ufunc_T *ufunc,
2939 int check_return_type,
2940 compiletype_T compile_type,
2941 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 char_u *line = NULL;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002944 garray_T lines_to_free;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 cctx_T cctx;
2948 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01002949 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02002950 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 int ret = FAIL;
2952 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02002953 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002954 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002955 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02002956 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01002957#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002958 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01002959#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002960 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002962 // allocated lines are freed at the end
2963 ga_init2(&lines_to_free, sizeof(char_u *), 50);
2964
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002965 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01002966 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002967 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02002969 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2970 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02002971 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002972
2973 switch (compile_type)
2974 {
2975 case CT_PROFILE:
2976#ifdef FEAT_PROFILE
2977 instr_dest = dfunc->df_instr_prof; break;
2978#endif
2979 case CT_NONE: instr_dest = dfunc->df_instr; break;
2980 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
2981 }
2982 if (instr_dest != NULL)
2983 // Was compiled in this mode before: Free old instructions.
2984 delete_def_function_contents(dfunc, FALSE);
2985 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002986 dfunc->df_defer_var_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02002988 else
2989 {
2990 if (add_def_function(ufunc) == FAIL)
2991 return FAIL;
2992 new_def_function = TRUE;
2993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994
Bram Moolenaar96923b72022-03-15 15:57:04 +00002995 if ((ufunc->uf_flags & FC_CLOSURE) && outer_cctx == NULL)
2996 {
2997 semsg(_(e_compiling_closure_without_context_str),
2998 printable_func_name(ufunc));
2999 return FAIL;
3000 }
3001
Bram Moolenaar985116a2020-07-12 17:31:09 +02003002 ufunc->uf_def_status = UF_COMPILING;
3003
Bram Moolenaara80faa82020-04-12 19:37:17 +02003004 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01003005
Bram Moolenaare99d4222021-06-13 14:01:26 +02003006 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 cctx.ctx_ufunc = ufunc;
3008 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003009 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
Bram Moolenaar078a4612022-01-04 15:17:03 +00003011 // Each entry on the type stack consists of two type pointers.
3012 ga_init2(&cctx.ctx_type_stack, sizeof(type2_T), 50);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 cctx.ctx_type_list = &ufunc->uf_type_list;
3014 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
3015 instr = &cctx.ctx_instr;
3016
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003017 // Set the context to the function, it may be compiled when called from
3018 // another script. Set the script version to the most modern one.
3019 // The line number will be set in next_line_from_context().
3020 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
3022
Bram Moolenaardc4c2302021-04-25 13:54:42 +02003023 // Don't use the flag from ":legacy" here.
3024 cmdmod.cmod_flags &= ~CMOD_LEGACY;
3025
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003026 // Make sure error messages are OK.
3027 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
3028 if (do_estack_push)
3029 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02003030 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003031
Bram Moolenaar9a015112021-12-31 14:06:45 +00003032 if (check_args_shadowing(ufunc, &cctx) == FAIL)
3033 goto erret;
3034
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003035 // For an object method and constructor "this" is the first local variable.
Bram Moolenaar574950d2023-01-03 19:08:50 +00003036 if (ufunc->uf_flags & (FC_OBJECT|FC_NEW))
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003037 {
3038 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3039 + ufunc->uf_dfunc_idx;
3040 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
3041 goto erret;
3042 ((char_u **)dfunc->df_var_names.ga_data)[0] =
3043 vim_strsave((char_u *)"this");
3044 ++dfunc->df_var_names.ga_len;
3045
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003046 // In the constructor allocate memory for the object and initialize the
3047 // object members.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003048 if ((ufunc->uf_flags & FC_NEW) == FC_NEW)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003049 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003050 generate_CONSTRUCT(&cctx, ufunc->uf_class);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003051
3052 for (int i = 0; i < ufunc->uf_class->class_obj_member_count; ++i)
3053 {
Bram Moolenaard505d172022-12-18 21:42:55 +00003054 ocmember_T *m = &ufunc->uf_class->class_obj_members[i];
3055 if (m->ocm_init != NULL)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003056 {
Bram Moolenaard505d172022-12-18 21:42:55 +00003057 char_u *expr = m->ocm_init;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003058 if (compile_expr0(&expr, &cctx) == FAIL)
3059 goto erret;
Bram Moolenaard505d172022-12-18 21:42:55 +00003060 if (!ends_excmd2(m->ocm_init, expr))
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003061 {
3062 semsg(_(e_trailing_characters_str), expr);
3063 goto erret;
3064 }
3065 }
3066 else
Bram Moolenaard505d172022-12-18 21:42:55 +00003067 push_default_value(&cctx, m->ocm_type->tt_type,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003068 FALSE, NULL);
3069 generate_STORE_THIS(&cctx, i);
3070 }
3071 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003072 }
3073
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003074 if (ufunc->uf_def_args.ga_len > 0)
3075 {
3076 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003077 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003078 int i;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003079 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003080 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003081
3082 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01003083 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003084 for (i = 0; i < count; ++i)
3085 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003086 char_u *arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
3087 if (STRCMP(arg, "v:none") == 0)
3088 // "arg = v:none" means the argument is optional without
3089 // setting a value when the argument is missing.
3090 continue;
3091
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003092 type_T *val_type;
3093 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02003094 where_T where = WHERE_INIT;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003095 int jump_instr_idx = instr->ga_len;
3096 isn_T *isn;
3097
3098 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003099 if (generate_JUMP_IF_ARG(&cctx, ISN_JUMP_IF_ARG_SET,
3100 i - count - off) == FAIL)
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003101 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01003102
3103 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02003104 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003105
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003106 int r = compile_expr0(&arg, &cctx);
Bram Moolenaar12bce952021-03-11 20:04:04 +01003107 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003108 goto erret;
3109
3110 // If no type specified use the type of the default value.
3111 // Otherwise check that the default value type matches the
3112 // specified type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003113 val_type = get_type_on_stack(&cctx, 0);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003114 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003115 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003116 {
3117 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003118 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003119 }
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003120 else if (need_type_where(val_type, ufunc->uf_arg_types[arg_idx],
Bram Moolenaarc6951a72022-12-29 20:56:24 +00003121 FALSE, -1, where, &cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003122 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003123
3124 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003125 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003126
3127 // set instruction index in JUMP_IF_ARG_SET to here
3128 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
3129 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003130 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02003131
3132 if (did_set_arg_type)
3133 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003134 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02003135 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003136
3137 /*
3138 * Loop over all the lines of the function and generate instructions.
3139 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 for (;;)
3141 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003142 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02003143 int starts_with_colon = FALSE;
3144 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003145 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01003146
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003147 // Bail out on the first error to avoid a flood of errors and report
3148 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01003149 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003150 goto erret;
3151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 if (line != NULL && *line == '|')
3153 // the line continues after a '|'
3154 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003155 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02003156 && !(*line == '#' && (line == cctx.ctx_line_start
3157 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003159 semsg(_(e_trailing_characters_str), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 goto erret;
3161 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01003162 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
3163 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 else
3165 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003166 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003167 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003168 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02003169 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01003170#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01003171 if (cctx.ctx_skip != SKIP_YES)
3172 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003173#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003175 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02003176 // Make a copy, splitting off nextcmd and removing trailing spaces
3177 // may change it.
3178 if (line != NULL)
3179 {
3180 line = vim_strsave(line);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00003181 if (ga_add_string(&lines_to_free, line) == FAIL)
3182 goto erret;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02003183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 }
3185
Bram Moolenaara80faa82020-04-12 19:37:17 +02003186 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 ea.cmdlinep = &line;
3188 ea.cmd = skipwhite(line);
Bram Moolenaar97f8c102022-04-02 19:43:57 +01003189 ea.skip = cctx.ctx_skip == SKIP_YES;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190
Bram Moolenaarb2049902021-01-24 12:53:53 +01003191 if (*ea.cmd == '#')
3192 {
Bram Moolenaarad6d9cc2022-08-08 21:43:11 +01003193 // "#" starts a comment, but "#{" is an error
3194 if (vim9_bad_comment(ea.cmd))
3195 goto erret;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003196 line = (char_u *)"";
3197 continue;
3198 }
3199
Bram Moolenaarf002a412021-01-24 13:34:18 +01003200#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02003201 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
3202 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003203 {
3204 may_generate_prof_end(&cctx, prof_lnum);
3205
3206 prof_lnum = cctx.ctx_lnum;
3207 generate_instr(&cctx, ISN_PROF_START);
3208 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01003209#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02003210 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
3211 && cctx.ctx_skip != SKIP_YES)
3212 {
3213 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003214 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02003215 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02003216 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003217
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02003218 // Some things can be recognized by the first character.
3219 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02003221 case '}':
3222 {
3223 // "}" ends a block scope
3224 scopetype_T stype = cctx.ctx_scope == NULL
3225 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02003227 if (stype == BLOCK_SCOPE)
3228 {
3229 compile_endblock(&cctx);
3230 line = ea.cmd;
3231 }
3232 else
3233 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003234 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02003235 goto erret;
3236 }
3237 if (line != NULL)
3238 line = skipwhite(ea.cmd + 1);
3239 continue;
3240 }
3241
3242 case '{':
3243 // "{" starts a block scope
3244 // "{'a': 1}->func() is something else
3245 if (ends_excmd(*skipwhite(ea.cmd + 1)))
3246 {
3247 line = compile_block(ea.cmd, &cctx);
3248 continue;
3249 }
3250 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 }
3252
3253 /*
3254 * COMMAND MODIFIERS
3255 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02003256 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02003257 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
3258 == FAIL)
Bram Moolenaarbc510062022-02-14 21:19:04 +00003259 goto erret;
Bram Moolenaare1004402020-10-24 20:49:43 +02003260 generate_cmdmods(&cctx, &local_cmdmod);
3261 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003263 // Check if there was a colon after the last command modifier or before
3264 // the current position.
3265 for (p = ea.cmd; p >= line; --p)
3266 {
3267 if (*p == ':')
3268 starts_with_colon = TRUE;
3269 if (p < ea.cmd && !VIM_ISWHITE(*p))
3270 break;
3271 }
3272
Bram Moolenaarce024c32021-06-26 13:00:49 +02003273 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02003274 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02003275 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02003276 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02003277 if (checkforcmd(&ea.cmd, "call", 3))
3278 {
3279 if (*ea.cmd == '(')
3280 // not for "call()"
3281 ea.cmd = p;
3282 else
3283 ea.cmd = skipwhite(ea.cmd);
3284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285
Bram Moolenaarce024c32021-06-26 13:00:49 +02003286 if (!starts_with_colon)
3287 {
3288 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003289
Bram Moolenaarce024c32021-06-26 13:00:49 +02003290 // Check for assignment after command modifiers.
3291 assign = may_compile_assignment(&ea, &line, &cctx);
3292 if (assign == OK)
3293 goto nextline;
3294 if (assign == FAIL)
3295 goto erret;
3296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 }
3298
3299 /*
3300 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003301 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003302 * 0z1234->func() should not be confused with a zero line number
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003303 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003304 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar31d99482022-05-26 22:24:43 +01003305 * "123->func()" is a method call
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003307 cmd = ea.cmd;
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00003308 if ((*cmd != '$' || starts_with_colon)
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003309 && (starts_with_colon
3310 || !(*cmd == '\''
3311 || (cmd[0] == '0' && cmd[1] == 'z')
3312 || (cmd[0] != NUL && cmd[0] == cmd[1]
Bram Moolenaar31d99482022-05-26 22:24:43 +01003313 && (*cmd == '+' || *cmd == '-'))
3314 || number_method(cmd))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003315 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003316 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003317 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003318 {
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00003319 if (!starts_with_colon
3320 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003321 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01003322 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003323 goto erret;
3324 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01003325 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003326 if (ends_excmd2(line, ea.cmd))
3327 {
3328 // A range without a command: jump to the line.
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003329 generate_EXEC(&cctx, ISN_EXECRANGE,
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00003330 vim_strnsave(cmd, ea.cmd - cmd));
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003331 line = ea.cmd;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003332 goto nextline;
3333 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003334 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003335 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02003336 p = find_ex_command(&ea, NULL,
3337 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003338 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003340 if (p == NULL)
3341 {
3342 if (cctx.ctx_skip != SKIP_YES)
Bram Moolenaarbed34f02022-01-19 20:48:37 +00003343 semsg(_(e_ambiguous_use_of_user_defined_command_str), ea.cmd);
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003344 goto erret;
3345 }
3346
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003347 // When using ":legacy cmd" always use compile_exec().
3348 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003349 {
3350 char_u *start = ea.cmd;
3351
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02003352 switch (ea.cmdidx)
3353 {
3354 case CMD_if:
3355 case CMD_elseif:
3356 case CMD_else:
3357 case CMD_endif:
3358 case CMD_for:
3359 case CMD_endfor:
3360 case CMD_continue:
3361 case CMD_break:
3362 case CMD_while:
3363 case CMD_endwhile:
3364 case CMD_try:
3365 case CMD_catch:
3366 case CMD_finally:
3367 case CMD_endtry:
3368 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
3369 goto erret;
3370 default: break;
3371 }
3372
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003373 // ":legacy return expr" needs to be handled differently.
3374 if (checkforcmd(&start, "return", 4))
3375 ea.cmdidx = CMD_return;
3376 else
3377 ea.cmdidx = CMD_legacy;
3378 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
3381 {
Bram Moolenaar97f8c102022-04-02 19:43:57 +01003382 // "eval" is used for "val->func()" and "var" for "var = val", then
3383 // "p" is equal to "ea.cmd" for a valid command.
3384 if (ea.cmdidx == CMD_eval || ea.cmdidx == CMD_var)
3385 ;
3386 else if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003387 {
3388 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01003389 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003390 }
Bram Moolenaar97f8c102022-04-02 19:43:57 +01003391 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01003393 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02003394 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 }
3397
Bram Moolenaar3988f642020-08-27 22:43:03 +02003398 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003399 && ea.cmdidx != CMD_elseif
3400 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02003401 && ea.cmdidx != CMD_endif
3402 && ea.cmdidx != CMD_endfor
3403 && ea.cmdidx != CMD_endwhile
3404 && ea.cmdidx != CMD_catch
3405 && ea.cmdidx != CMD_finally
3406 && ea.cmdidx != CMD_endtry)
3407 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02003408 emsg(_(e_unreachable_code_after_return));
3409 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02003410 }
3411
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003412 p = skipwhite(p);
3413 if (ea.cmdidx != CMD_SIZE
3414 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
3415 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02003416 if (ea.cmdidx >= 0)
3417 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003418 if ((ea.argt & EX_BANG) && *p == '!')
3419 {
3420 ea.forceit = TRUE;
3421 p = skipwhite(p + 1);
3422 }
Bram Moolenaar09d94212022-05-05 15:20:03 +01003423 if ((ea.argt & EX_RANGE) == 0 && ea.addr_count > 0)
3424 {
3425 emsg(_(e_no_range_allowed));
3426 goto erret;
3427 }
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003428 }
3429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 switch (ea.cmdidx)
3431 {
3432 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +00003433 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02003434 ea.arg = p;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00003435 line = compile_nested_function(&ea, &cctx, &lines_to_free);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003436 break;
3437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003439 line = compile_return(p, check_return_type,
3440 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003441 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 break;
3443
3444 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02003445 emsg(_(e_cannot_use_let_in_vim9_script));
3446 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003447 case CMD_var:
3448 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003450 case CMD_increment:
3451 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003453 if (line == p)
Bram Moolenaar8b716f52022-02-15 21:17:56 +00003454 {
3455 emsg(_(e_invalid_assignment));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003456 line = NULL;
Bram Moolenaar8b716f52022-02-15 21:17:56 +00003457 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 break;
3459
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003460 case CMD_unlet:
3461 case CMD_unlockvar:
3462 case CMD_lockvar:
3463 line = compile_unletlock(p, &ea, &cctx);
3464 break;
3465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02003467 emsg(_(e_import_can_only_be_used_in_script));
3468 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 break;
3470
3471 case CMD_if:
3472 line = compile_if(p, &cctx);
3473 break;
3474 case CMD_elseif:
3475 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003476 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 break;
3478 case CMD_else:
3479 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003480 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 break;
3482 case CMD_endif:
3483 line = compile_endif(p, &cctx);
3484 break;
3485
3486 case CMD_while:
3487 line = compile_while(p, &cctx);
3488 break;
3489 case CMD_endwhile:
3490 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003491 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 break;
3493
3494 case CMD_for:
3495 line = compile_for(p, &cctx);
3496 break;
3497 case CMD_endfor:
3498 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003499 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 break;
3501 case CMD_continue:
3502 line = compile_continue(p, &cctx);
3503 break;
3504 case CMD_break:
3505 line = compile_break(p, &cctx);
3506 break;
3507
3508 case CMD_try:
3509 line = compile_try(p, &cctx);
3510 break;
3511 case CMD_catch:
3512 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003513 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 break;
3515 case CMD_finally:
3516 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003517 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 break;
3519 case CMD_endtry:
3520 line = compile_endtry(p, &cctx);
3521 break;
3522 case CMD_throw:
3523 line = compile_throw(p, &cctx);
3524 break;
3525
Bram Moolenaar007f9d62020-07-06 23:04:49 +02003526 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02003527 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02003528 break;
3529
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003530 case CMD_defer:
3531 line = compile_defer(p, &cctx);
3532 break;
3533
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003534#ifdef HAS_MESSAGE_WINDOW
3535 case CMD_echowindow:
3536 {
3537 long cmd_count = get_cmd_count(line, &ea);
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01003538 if (cmd_count < 0)
3539 line = NULL;
3540 else
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003541 line = compile_mult_expr(p, ea.cmdidx,
3542 cmd_count, &cctx);
3543 }
3544 break;
3545#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 case CMD_echon:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003548 case CMD_echoconsole:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003549 case CMD_echoerr:
3550 case CMD_echomsg:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003551 case CMD_execute:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003552 line = compile_mult_expr(p, ea.cmdidx, 0, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003553 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003555 case CMD_put:
3556 ea.cmd = cmd;
3557 line = compile_put(p, &ea, &cctx);
3558 break;
3559
Bram Moolenaar4c137212021-04-19 16:48:48 +02003560 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +01003561 if (check_global_and_subst(ea.cmd, p) == FAIL)
3562 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003563 if (cctx.ctx_skip == SKIP_YES)
3564 line = (char_u *)"";
3565 else
3566 {
3567 ea.arg = p;
3568 line = compile_substitute(line, &ea, &cctx);
3569 }
3570 break;
3571
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003572 case CMD_redir:
3573 ea.arg = p;
3574 line = compile_redir(line, &ea, &cctx);
3575 break;
3576
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003577 case CMD_cexpr:
3578 case CMD_lexpr:
3579 case CMD_caddexpr:
3580 case CMD_laddexpr:
3581 case CMD_cgetexpr:
3582 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003583#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003584 ea.arg = p;
3585 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003586#else
3587 ex_ni(&ea);
3588 line = NULL;
3589#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003590 break;
3591
Bram Moolenaarae616492020-07-28 20:07:27 +02003592 case CMD_append:
3593 case CMD_change:
3594 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003595 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02003596 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02003597 case CMD_xit:
3598 not_in_vim9(&ea);
3599 goto erret;
3600
Bram Moolenaar002262f2020-07-08 17:47:57 +02003601 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02003602 if (cctx.ctx_skip != SKIP_YES)
3603 {
3604 semsg(_(e_invalid_command_str), ea.cmd);
3605 goto erret;
3606 }
3607 // We don't check for a next command here.
3608 line = (char_u *)"";
3609 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02003610
Bram Moolenaar20677332021-06-06 17:02:53 +02003611 case CMD_lua:
3612 case CMD_mzscheme:
3613 case CMD_perl:
3614 case CMD_py3:
3615 case CMD_python3:
3616 case CMD_python:
3617 case CMD_pythonx:
3618 case CMD_ruby:
3619 case CMD_tcl:
3620 ea.arg = p;
3621 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02003622 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02003623 else
3624 // heredoc lines have been concatenated with NL
3625 // characters in get_function_body()
3626 line = compile_script(line, &cctx);
3627 break;
3628
Bram Moolenaar107f7322022-02-06 17:30:41 +00003629 case CMD_vim9script:
Bram Moolenaar8cbf2492022-02-06 20:28:13 +00003630 if (cctx.ctx_skip != SKIP_YES)
3631 {
3632 emsg(_(e_vim9script_can_only_be_used_in_script));
3633 goto erret;
3634 }
3635 line = (char_u *)"";
3636 break;
Bram Moolenaar107f7322022-02-06 17:30:41 +00003637
Bram Moolenaar7b829262021-10-13 15:04:34 +01003638 case CMD_global:
3639 if (check_global_and_subst(ea.cmd, p) == FAIL)
3640 goto erret;
3641 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +02003642 default:
3643 // Not recognized, execute with do_cmdline_cmd().
3644 ea.arg = p;
3645 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 break;
3647 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003648nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 if (line == NULL)
3650 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02003651 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003653 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02003654 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 if (cctx.ctx_type_stack.ga_len < 0)
3657 {
3658 iemsg("Type stack underflow");
3659 goto erret;
3660 }
3661 }
3662
3663 if (cctx.ctx_scope != NULL)
3664 {
3665 if (cctx.ctx_scope->se_type == IF_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00003666 emsg(_(e_missing_endif));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00003668 emsg(_(e_missing_endwhile));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00003670 emsg(_(e_missing_endfor));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003672 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 goto erret;
3674 }
3675
Bram Moolenaarefd88552020-06-18 20:50:10 +02003676 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02003678 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
3679 ufunc->uf_ret_type = &t_void;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003680 else if (ufunc->uf_ret_type->tt_type != VAR_VOID
3681 && (ufunc->uf_flags & FC_NEW) != FC_NEW)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003683 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 goto erret;
3685 }
3686
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003687 // Return void if there is no return at the end.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003688 // For a constructor return the object.
3689 if ((ufunc->uf_flags & FC_NEW) == FC_NEW)
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003690 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003691 generate_instr(&cctx, ISN_RETURN_OBJECT);
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003692 ufunc->uf_ret_type = &ufunc->uf_class->class_object_type;
3693 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003694 else
3695 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 }
3697
Bram Moolenaar599410c2021-04-10 14:03:43 +02003698 // When compiled with ":silent!" and there was an error don't consider the
3699 // function compiled.
3700 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02003701 {
3702 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3703 + ufunc->uf_dfunc_idx;
3704 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003705 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003706#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02003707 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003708 {
3709 dfunc->df_instr_prof = instr->ga_data;
3710 dfunc->df_instr_prof_count = instr->ga_len;
3711 }
3712 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01003713#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02003714 if (cctx.ctx_compile_type == CT_DEBUG)
3715 {
3716 dfunc->df_instr_debug = instr->ga_data;
3717 dfunc->df_instr_debug_count = instr->ga_len;
3718 }
3719 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01003720 {
3721 dfunc->df_instr = instr->ga_data;
3722 dfunc->df_instr_count = instr->ga_len;
3723 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003724 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003725 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01003726
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003727 if (cctx.ctx_outer_used)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01003728 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003729 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01003730 if (outer_cctx != NULL)
3731 ++outer_cctx->ctx_closure_count;
3732 }
3733
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003734 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02003735 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736
3737 ret = OK;
3738
3739erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02003740 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02003742 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3743 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003744
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02003745 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02003746 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003747 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02003748 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003749
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02003750 // If using the last entry in the table and it was added above, we
3751 // might as well remove it.
3752 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02003753 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02003754 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003755 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02003756 ufunc->uf_dfunc_idx = 0;
3757 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003758 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003759
Bram Moolenaar3cca2992020-04-02 22:57:36 +02003760 while (cctx.ctx_scope != NULL)
3761 drop_scope(&cctx);
3762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 if (errormsg != NULL)
3764 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01003765 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02003766 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 }
3768
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003769 if (cctx.ctx_redir_lhs.lhs_name != NULL)
3770 {
3771 if (ret == OK)
3772 {
3773 emsg(_(e_missing_redir_end));
3774 ret = FAIL;
3775 }
3776 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02003777 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003778 }
3779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02003781 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02003782 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003783 if (do_estack_push)
3784 estack_pop();
3785
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00003786 ga_clear_strings(&lines_to_free);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003787 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02003789 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790}
3791
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003792 void
3793set_function_type(ufunc_T *ufunc)
3794{
3795 int varargs = ufunc->uf_va_name != NULL;
3796 int argcount = ufunc->uf_args.ga_len;
3797
3798 // Create a type for the function, with the return type and any
3799 // argument types.
3800 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
3801 // The type is included in "tt_args".
3802 if (argcount > 0 || varargs)
3803 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01003804 if (ufunc->uf_type_list.ga_itemsize == 0)
3805 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003806 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
3807 argcount, &ufunc->uf_type_list);
3808 // Add argument types to the function type.
3809 if (func_type_add_arg_types(ufunc->uf_func_type,
3810 argcount + varargs,
3811 &ufunc->uf_type_list) == FAIL)
3812 return;
3813 ufunc->uf_func_type->tt_argcount = argcount + varargs;
3814 ufunc->uf_func_type->tt_min_argcount =
3815 argcount - ufunc->uf_def_args.ga_len;
3816 if (ufunc->uf_arg_types == NULL)
3817 {
3818 int i;
3819
3820 // lambda does not have argument types.
3821 for (i = 0; i < argcount; ++i)
3822 ufunc->uf_func_type->tt_args[i] = &t_any;
3823 }
3824 else
3825 mch_memmove(ufunc->uf_func_type->tt_args,
3826 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
3827 if (varargs)
3828 {
3829 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02003830 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003831 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
3832 }
3833 }
3834 else
3835 // No arguments, can use a predefined type.
3836 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
3837 argcount, &ufunc->uf_type_list);
3838}
3839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003841 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01003842 */
3843 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003844delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01003845{
3846 int idx;
3847
Bram Moolenaard505d172022-12-18 21:42:55 +00003848 // In same cases the instructions may refer to a class in which the
3849 // function is defined and unreferencing the class may call back here
3850 // recursively. Set the df_delete_busy to avoid problems.
3851 if (dfunc->df_delete_busy)
3852 return;
3853 dfunc->df_delete_busy = TRUE;
3854
Bram Moolenaar20431c92020-03-20 18:39:46 +01003855 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003856 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003857
3858 if (dfunc->df_instr != NULL)
3859 {
3860 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
3861 delete_instr(dfunc->df_instr + idx);
3862 VIM_CLEAR(dfunc->df_instr);
3863 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02003864 if (dfunc->df_instr_debug != NULL)
3865 {
3866 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
3867 delete_instr(dfunc->df_instr_debug + idx);
3868 VIM_CLEAR(dfunc->df_instr_debug);
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02003869 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01003870#ifdef FEAT_PROFILE
3871 if (dfunc->df_instr_prof != NULL)
3872 {
3873 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
3874 delete_instr(dfunc->df_instr_prof + idx);
3875 VIM_CLEAR(dfunc->df_instr_prof);
Bram Moolenaarc05fe072021-01-24 21:30:48 +01003876 }
3877#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01003878
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003879 if (mark_deleted)
3880 dfunc->df_deleted = TRUE;
3881 if (dfunc->df_ufunc != NULL)
3882 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaard505d172022-12-18 21:42:55 +00003883
3884 dfunc->df_delete_busy = FALSE;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003885}
3886
3887/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003888 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003889 * function, unless another user function still uses it.
3890 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 */
3892 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003893unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003895 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 {
3897 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3898 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003900 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003901 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003902 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003903 ufunc->uf_dfunc_idx = 0;
3904 if (dfunc->df_ufunc == ufunc)
3905 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 }
3907}
3908
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003909/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003910 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003911 */
3912 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003913link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003914{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003915 if (ufunc->uf_dfunc_idx > 0)
3916 {
3917 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3918 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003919
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003920 ++dfunc->df_refcount;
3921 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003922}
3923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01003925/*
3926 * Free all functions defined with ":def".
3927 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 void
3929free_def_functions(void)
3930{
Bram Moolenaar20431c92020-03-20 18:39:46 +01003931 int idx;
3932
3933 for (idx = 0; idx < def_functions.ga_len; ++idx)
3934 {
3935 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
3936
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003937 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003938 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003939 }
3940
3941 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942}
3943#endif
3944
3945
3946#endif // FEAT_EVAL