blob: 4890de34610e7d5f8bfe6f3ce7ab518b61ceb288 [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
46 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
48 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010049 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
50 if (STRNCMP(name, lvp->lv_name, len) == 0
51 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020052 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010053 if (lvar != NULL)
54 {
55 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +010056 lvar->lv_from_outer = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +010057 lvar->lv_loop_idx = get_loop_var_idx(cctx);
Bram Moolenaar709664c2020-12-12 14:33:41 +010058 }
59 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020060 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010061 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020062
63 // Find local in outer function scope.
64 if (cctx->ctx_outer != NULL)
65 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010066 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020067 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010068 if (lvar != NULL)
69 {
70 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +010071 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +010072 }
73 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020074 }
75 }
76
Bram Moolenaar709664c2020-12-12 14:33:41 +010077 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078}
79
80/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +020081 * Lookup an argument in the current function and an enclosing function.
82 * Returns the argument index in "idxp"
83 * Returns the argument type in "type"
84 * Sets "gen_load_outer" to TRUE if found in outer scope.
85 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +000087 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +020088arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +020089 char_u *name,
90 size_t len,
91 int *idxp,
92 type_T **type,
93 int *gen_load_outer,
94 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095{
96 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +020097 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaarae8d2de2020-02-13 21:42:24 +010099 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200100 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200101 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102 {
103 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
104
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200105 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
106 {
107 if (idxp != NULL)
108 {
109 // Arguments are located above the frame pointer. One further
110 // if there is a vararg argument
111 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
112 + STACK_FRAME_SIZE)
113 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
114
115 if (cctx->ctx_ufunc->uf_arg_types != NULL)
116 *type = cctx->ctx_ufunc->uf_arg_types[idx];
117 else
118 *type = &t_any;
119 }
120 return OK;
121 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100123
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200124 va_name = cctx->ctx_ufunc->uf_va_name;
125 if (va_name != NULL
126 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
127 {
128 if (idxp != NULL)
129 {
130 // varargs is always the last argument
131 *idxp = -STACK_FRAME_SIZE - 1;
132 *type = cctx->ctx_ufunc->uf_va_type;
133 }
134 return OK;
135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100136
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200137 if (cctx->ctx_outer != NULL)
138 {
139 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200140 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200141 == OK)
142 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100143 if (gen_load_outer != NULL)
144 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200145 return OK;
146 }
147 }
148
149 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150}
151
152/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200153 * Lookup a script-local variable in the current script, possibly defined in a
154 * block that contains the function "cctx->ctx_ufunc".
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000155 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100156 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200157 * Return NULL when not found.
158 */
159 static sallvar_T *
Bram Moolenaardce24412022-02-08 20:35:30 +0000160find_script_var(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200161{
162 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
163 hashitem_T *hi;
164 int cc;
165 sallvar_T *sav;
166 ufunc_T *ufunc;
167
168 // Find the list of all script variables with the right name.
169 if (len > 0)
170 {
171 cc = name[len];
172 name[len] = NUL;
173 }
174 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
175 if (len > 0)
176 name[len] = cc;
177 if (HASHITEM_EMPTY(hi))
178 return NULL;
179
180 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200181 if (sav->sav_block_id == 0)
182 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200183 return sav;
184
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200185 if (cctx == NULL)
186 {
Bram Moolenaar766ae5b2022-09-14 00:30:51 +0100187 if (cstack == NULL)
188 return NULL;
189
Bram Moolenaardce24412022-02-08 20:35:30 +0000190 // Not in a function scope, find variable with block ID equal to or
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000191 // smaller than the current block id. Use "cstack" to go up the block
192 // scopes.
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200193 while (sav != NULL)
194 {
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000195 int idx;
Bram Moolenaardce24412022-02-08 20:35:30 +0000196
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000197 for (idx = cstack->cs_idx; idx >= 0; --idx)
198 if (cstack->cs_block_id[idx] == sav->sav_block_id)
Bram Moolenaardce24412022-02-08 20:35:30 +0000199 break;
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000200 if (idx >= 0)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200201 break;
202 sav = sav->sav_next;
203 }
204 return sav;
205 }
206
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200207 // Go over the variables with this name and find one that was visible
208 // from the function.
209 ufunc = cctx->ctx_ufunc;
210 while (sav != NULL)
211 {
212 int idx;
213
214 // Go over the blocks that this function was defined in. If the
215 // variable block ID matches it was visible to the function.
216 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
217 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
218 return sav;
219 sav = sav->sav_next;
220 }
221
Bram Moolenaar58493cf2022-01-06 12:23:30 +0000222 // Not found, variable was not visible.
223 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200224}
225
226/*
Bram Moolenaar766ae5b2022-09-14 00:30:51 +0100227 * If "name" can be found in the current script set it's "block_id".
228 */
229 void
230update_script_var_block_id(char_u *name, int block_id)
231{
232 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
233 hashitem_T *hi;
234 sallvar_T *sav;
235
236 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
237 if (HASHITEM_EMPTY(hi))
238 return;
239 sav = HI2SAV(hi);
240 sav->sav_block_id = block_id;
241}
242
243/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100244 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200245 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000246 int
Bram Moolenaar84367732020-08-23 15:21:55 +0200247script_is_vim9()
248{
249 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
250}
251
252/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200253 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000254 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 * Returns OK or FAIL.
256 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000257 int
Bram Moolenaardce24412022-02-08 20:35:30 +0000258script_var_exists(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200260 if (current_sctx.sc_sid <= 0)
261 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200262 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200263 {
264 // Check script variables that were visible where the function was
265 // defined.
Bram Moolenaardce24412022-02-08 20:35:30 +0000266 if (find_script_var(name, len, cctx, cstack) != NULL)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200267 return OK;
268 }
269 else
270 {
271 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
272 dictitem_T *di;
273 int cc;
274
275 // Check script variables that are currently visible
276 cc = name[len];
277 name[len] = NUL;
278 di = find_var_in_ht(ht, 0, name, TRUE);
279 name[len] = cc;
280 if (di != NULL)
281 return OK;
282 }
283
284 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285}
286
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100287/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100288 * Return TRUE if "name" is a local variable, argument, script variable or
289 * imported.
290 */
291 static int
292variable_exists(char_u *name, size_t len, cctx_T *cctx)
293{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100294 return (cctx != NULL
295 && (lookup_local(name, len, NULL, cctx) == OK
296 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaardce24412022-02-08 20:35:30 +0000297 || script_var_exists(name, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000298 || find_imported(name, len, FALSE) != NULL;
Bram Moolenaare0890d62021-02-17 14:52:14 +0100299}
300
301/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100302 * Return TRUE if "name" is a local variable, argument, script variable,
Bram Moolenaar97f8c102022-04-02 19:43:57 +0100303 * imported or function. Or commands are being skipped, a declaration may have
304 * been skipped then.
Bram Moolenaar6914e872021-03-06 21:01:09 +0100305 */
306 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100307item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100308{
Bram Moolenaar0631bb42022-02-13 21:20:21 +0000309 return variable_exists(name, len, cctx);
Bram Moolenaar6914e872021-03-06 21:01:09 +0100310}
311
312/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100313 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000314 * compilation context "cctx".
315 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200316 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100317 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100318 * Return FAIL and give an error if it defined.
319 */
320 int
Bram Moolenaardce24412022-02-08 20:35:30 +0000321check_defined(
322 char_u *p,
323 size_t len,
324 cctx_T *cctx,
325 cstack_T *cstack,
326 int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100327{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200328 int c = p[len];
329 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200330
Bram Moolenaarda479c72021-04-10 21:01:38 +0200331 // underscore argument is OK
332 if (len == 1 && *p == '_')
333 return OK;
334
Bram Moolenaardce24412022-02-08 20:35:30 +0000335 if (script_var_exists(p, len, cctx, cstack) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100336 {
337 if (is_arg)
338 semsg(_(e_argument_already_declared_in_script_str), p);
339 else
340 semsg(_(e_variable_already_declared_in_script_str), p);
341 return FAIL;
342 }
343
Bram Moolenaarad486a02020-08-01 23:22:18 +0200344 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100345 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100346 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200347 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000348 || find_imported(p, len, FALSE) != NULL
Bram Moolenaaracc4b562022-01-24 13:54:45 +0000349 || (ufunc = find_func_even_dead(p, 0)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100350 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200351 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200352 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
353 && (!func_is_global(ufunc)
354 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200355 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100356 if (is_arg)
357 semsg(_(e_argument_name_shadows_existing_variable_str), p);
358 else
359 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200360 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200361 return FAIL;
362 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100363 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200364 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100365 return OK;
366}
367
Bram Moolenaar65b95452020-07-19 14:03:09 +0200368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200370 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
371 * used. Return FALSE if the types will never match.
372 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200373 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200374use_typecheck(type_T *actual, type_T *expected)
375{
376 if (actual->tt_type == VAR_ANY
377 || actual->tt_type == VAR_UNKNOWN
378 || (actual->tt_type == VAR_FUNC
379 && (expected->tt_type == VAR_FUNC
380 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000381 && (actual->tt_member == &t_any
382 || actual->tt_member == &t_unknown
383 || actual->tt_argcount < 0)
384 && (actual->tt_member == &t_unknown ||
385 (actual->tt_member == &t_void)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100386 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200387 return TRUE;
388 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
389 && actual->tt_type == expected->tt_type)
390 // This takes care of a nested list or dict.
391 return use_typecheck(actual->tt_member, expected->tt_member);
392 return FALSE;
393}
394
395/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200396 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200397 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200398 * - "actual" is a type that can be "expected" type: add a runtime check; or
399 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200400 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
401 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200402 */
Bram Moolenaara1c51952022-02-02 16:20:26 +0000403 int
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200404need_type_where(
405 type_T *actual,
406 type_T *expected,
407 int offset,
408 where_T where,
409 cctx_T *cctx,
410 int silent,
411 int actual_is_const)
412{
Bram Moolenaar44a89772021-12-18 12:31:33 +0000413 int ret;
414
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200415 if (expected == &t_bool && actual != &t_bool
416 && (actual->tt_flags & TTFLAG_BOOL_OK))
417 {
418 // Using "0", "1" or the result of an expression with "&&" or "||" as a
419 // boolean is OK but requires a conversion.
420 generate_2BOOL(cctx, FALSE, offset);
421 return OK;
422 }
423
Bram Moolenaar44a89772021-12-18 12:31:33 +0000424 ret = check_type_maybe(expected, actual, FALSE, where);
425 if (ret == OK)
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200426 return OK;
427
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000428 // If actual a constant a runtime check makes no sense. If it's
429 // null_function it is OK.
430 if (actual_is_const && ret == MAYBE && actual == &t_func_unknown)
431 return OK;
432
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200433 // If the actual type can be the expected type add a runtime check.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000434 if (!actual_is_const && ret == MAYBE && use_typecheck(actual, expected))
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200435 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100436 generate_TYPECHECK(cctx, expected, offset,
437 where.wt_variable, where.wt_index);
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200438 return OK;
439 }
440
441 if (!silent)
442 type_mismatch_where(expected, actual, where);
443 return FAIL;
444}
445
Bram Moolenaar351ead02021-01-16 16:07:01 +0100446 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200447need_type(
448 type_T *actual,
449 type_T *expected,
450 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100451 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200452 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200453 int silent,
454 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200455{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200456 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100457
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100458 where.wt_index = arg_idx;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200459 return need_type_where(actual, expected, offset, where,
460 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200461}
462
463/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200465 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100466 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000467 lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +0200468reserve_local(
469 cctx_T *cctx,
470 char_u *name,
471 size_t len,
472 int isConst,
473 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200476 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200478 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100479 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200480 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200481 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 }
483
Bram Moolenaar35578162021-08-02 19:10:38 +0200484 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200485 return NULL;
486 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +0200487 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100488
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200489 // Every local variable uses the next entry on the stack. We could re-use
490 // the last ones when leaving a scope, but then variables used in a closure
491 // might get overwritten. To keep things simple do not re-use stack
492 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200493 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
494 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200495
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200496 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497 lvar->lv_const = isConst;
498 lvar->lv_type = type;
499
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200500 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +0200501 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200502 return NULL;
503 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
504 vim_strsave(lvar->lv_name);
505 ++dfunc->df_var_names.ga_len;
506
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200507 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508}
509
510/*
Bram Moolenaar08251752021-01-11 21:20:18 +0100511 * If "check_writable" is ASSIGN_CONST give an error if the variable was
512 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
513 * error if the variable was defined with :const.
514 */
515 static int
516check_item_writable(svar_T *sv, int check_writable, char_u *name)
517{
518 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
519 || (check_writable == ASSIGN_FINAL
520 && sv->sv_const == ASSIGN_CONST))
521 {
Bram Moolenaard8e44472021-07-21 22:20:33 +0200522 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +0100523 return FAIL;
524 }
525 return OK;
526}
527
528/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +0100530 * Pass "check_writable" to check_item_writable().
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000531 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 * Returns the index in "sn_var_vals" if found.
533 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +0100534 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 */
536 int
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000537get_script_item_idx(
538 int sid,
539 char_u *name,
540 int check_writable,
541 cctx_T *cctx,
542 cstack_T *cstack)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543{
544 hashtab_T *ht;
545 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100546 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200547 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 int idx;
549
Bram Moolenaare3d46852020-08-29 13:39:17 +0200550 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200552 if (sid == current_sctx.sc_sid)
553 {
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000554 sallvar_T *sav = find_script_var(name, 0, cctx, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200555
556 if (sav == NULL)
557 return -2;
558 idx = sav->sav_var_vals_idx;
559 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +0100560 if (check_item_writable(sv, check_writable, name) == FAIL)
561 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200562 return idx;
563 }
564
565 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 ht = &SCRIPT_VARS(sid);
567 di = find_var_in_ht(ht, 0, name, TRUE);
568 if (di == NULL)
Bram Moolenaar577bd852022-01-10 18:42:52 +0000569 {
570 if (si->sn_autoload_prefix != NULL)
571 {
572 hashitem_T *hi;
573
574 // A variable exported from an autoload script is in the global
575 // variables, we can find it in the all_vars table.
576 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
577 if (!HASHITEM_EMPTY(hi))
578 return HI2SAV(hi)->sav_var_vals_idx;
579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 return -2;
Bram Moolenaar577bd852022-01-10 18:42:52 +0000581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582
583 // Now find the svar_T index in sn_var_vals.
584 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
585 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200586 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587 if (sv->sv_tv == &di->di_tv)
588 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100589 if (check_item_writable(sv, check_writable, name) == FAIL)
590 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 return idx;
592 }
593 }
594 return -1;
595}
596
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000597 static imported_T *
598find_imported_in_script(char_u *name, size_t len, int sid)
599{
600 scriptitem_T *si;
601 int idx;
602
603 if (!SCRIPT_ID_VALID(sid))
604 return NULL;
605 si = SCRIPT_ITEM(sid);
606 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
607 {
608 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
609
610 if (len == 0 ? STRCMP(name, import->imp_name) == 0
611 : STRLEN(import->imp_name) == len
612 && STRNCMP(name, import->imp_name, len) == 0)
613 return import;
614 }
615 return NULL;
616}
617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618/*
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000619 * Find "name" in imported items of the current script.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100620 * If "len" is 0 use any length that works.
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000621 * If "load" is TRUE and the script was not loaded yet, load it now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622 */
623 imported_T *
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000624find_imported(char_u *name, size_t len, int load)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625{
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000626 imported_T *ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627
Bram Moolenaare3d46852020-08-29 13:39:17 +0200628 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +0200629 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100630
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000631 ret = find_imported_in_script(name, len, current_sctx.sc_sid);
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000632 if (ret != NULL && load && (ret->imp_flags & IMP_FLAGS_AUTOLOAD))
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000633 {
Bram Moolenaar753885b2022-08-24 16:30:36 +0100634 scid_T actual_sid = 0;
Bram Moolenaar6809ff92022-07-26 15:10:56 +0100635 int save_emsg_off = emsg_off;
636
637 // "emsg_off" will be set when evaluating an expression silently, but
638 // we do want to know about errors in a script. Also because it then
639 // aborts when an error is encountered.
640 emsg_off = FALSE;
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000641
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000642 // script found before but not loaded yet
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000643 ret->imp_flags &= ~IMP_FLAGS_AUTOLOAD;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000644 (void)do_source(SCRIPT_ITEM(ret->imp_sid)->sn_name, FALSE,
Bram Moolenaar753885b2022-08-24 16:30:36 +0100645 DOSO_NONE, &actual_sid);
646 // If the script is a symlink it may be sourced with another name, may
647 // need to adjust the script ID for that.
648 if (actual_sid != 0)
649 ret->imp_sid = actual_sid;
Bram Moolenaar6809ff92022-07-26 15:10:56 +0100650
651 emsg_off = save_emsg_off;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000652 }
653 return ret;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200654}
655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000657 * Called when checking for a following operator at "arg". When the rest of
658 * the line is empty or only a comment, peek the next line. If there is a next
659 * line return a pointer to it and set "nextp".
660 * Otherwise skip over white space.
661 */
662 char_u *
663may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
664{
665 char_u *p = skipwhite(arg);
666
667 *nextp = NULL;
668 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
669 {
670 *nextp = peek_next_line_from_context(cctx);
671 if (*nextp != NULL)
672 return *nextp;
673 }
674 return p;
675}
676
677/*
Bram Moolenaar23c55272020-06-21 16:58:13 +0200678 * Return a pointer to the next line that isn't empty or only contains a
679 * comment. Skips over white space.
680 * Returns NULL if there is none.
681 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200682 char_u *
683peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +0200684{
685 int lnum = cctx->ctx_lnum;
686
687 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
688 {
689 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +0200690 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200691
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200692 // ignore NULLs inserted for continuation lines
693 if (line != NULL)
694 {
695 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100696 if (vim9_bad_comment(p))
697 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200698 if (*p != NUL && !vim9_comment_start(p))
699 return p;
700 }
Bram Moolenaar23c55272020-06-21 16:58:13 +0200701 }
702 return NULL;
703}
704
705/*
Bram Moolenaare6085c52020-04-12 20:19:16 +0200706 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +0200707 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +0200708 * Returns NULL when at the end.
709 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200710 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +0200711next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +0200712{
Bram Moolenaar7a092242020-04-16 22:10:49 +0200713 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200714
715 do
716 {
717 ++cctx->ctx_lnum;
718 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200719 {
720 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200721 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +0200722 }
Bram Moolenaare6085c52020-04-12 20:19:16 +0200723 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +0200724 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200725 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200726 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +0200727 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +0200728 return line;
729}
730
731/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100732 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200733 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +0200734 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200735 * Return FAIL if beyond the last line, "*arg" is unmodified then.
736 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000737 int
Bram Moolenaar2c330432020-04-13 14:41:35 +0200738may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200739{
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100740 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100741 if (vim9_bad_comment(*arg))
742 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200743 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200744 {
Bram Moolenaar23c55272020-06-21 16:58:13 +0200745 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200746
747 if (next == NULL)
748 return FAIL;
749 *arg = skipwhite(next);
750 }
751 return OK;
752}
753
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200754/*
755 * Idem, and give an error when failed.
756 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000757 int
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200758may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
759{
760 if (may_get_next_line(whitep, arg, cctx) == FAIL)
761 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +0100762 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200763 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200764 return FAIL;
765 }
766 return OK;
767}
768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200770 * Get a line from the compilation context, compatible with exarg_T getline().
771 * Return a pointer to the line in allocated memory.
772 * Return NULL for end-of-file or some error.
773 */
774 static char_u *
775exarg_getline(
776 int c UNUSED,
777 void *cookie,
778 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200779 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200780{
781 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +0200782 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200783
Bram Moolenaar66250c92020-08-20 15:02:42 +0200784 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200785 {
Bram Moolenaar2914a202020-09-27 18:24:03 +0200786 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +0200787 return NULL;
788 ++cctx->ctx_lnum;
789 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
790 // Comment lines result in NULL pointers, skip them.
791 if (p != NULL)
792 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +0200793 }
Bram Moolenaar04b12692020-05-04 23:24:44 +0200794}
795
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100796 void
797fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
798{
799 eap->getline = exarg_getline;
800 eap->cookie = cctx;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +0000801 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100802}
803
Bram Moolenaar04b12692020-05-04 23:24:44 +0200804/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000805 * Return TRUE if "ufunc" should be compiled, taking into account whether
806 * "profile" indicates profiling is to be done.
807 */
808 int
809func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
810{
811 switch (ufunc->uf_def_status)
812 {
813 case UF_TO_BE_COMPILED:
814 return TRUE;
815
816 case UF_COMPILED:
817 {
818 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
819 + ufunc->uf_dfunc_idx;
820
821 switch (compile_type)
822 {
823 case CT_PROFILE:
824#ifdef FEAT_PROFILE
825 return dfunc->df_instr_prof == NULL;
826#endif
827 case CT_NONE:
828 return dfunc->df_instr == NULL;
829 case CT_DEBUG:
830 return dfunc->df_instr_debug == NULL;
831 }
832 }
833
834 case UF_NOT_COMPILED:
835 case UF_COMPILE_ERROR:
836 case UF_COMPILING:
837 break;
838 }
839 return FALSE;
840}
841
842/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200843 * Compile a nested :def command.
844 */
845 static char_u *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000846compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200847{
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200848 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +0200849 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200850 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000851 int off;
852 char_u *func_name;
Bram Moolenaareef21022020-08-01 22:16:43 +0200853 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200854 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100855 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +0200856 compiletype_T compile_type;
Bram Moolenaara915fa02022-03-23 11:29:15 +0000857 isn_T *funcref_isn = NULL;
Bram Moolenaar1889f492022-08-16 19:34:44 +0100858 lvar_T *lvar = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200859
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200860 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200861 {
862 emsg(_(e_cannot_use_bang_with_nested_def));
863 return NULL;
864 }
865
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100866 if (*name_start == '/')
867 {
868 name_end = skip_regexp(name_start + 1, '/', TRUE);
869 if (*name_end == '/')
870 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +0200871 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100872 }
873 if (name_end == name_start || *skipwhite(name_end) != '(')
874 {
875 if (!ends_excmd2(name_start, name_end))
876 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +0000877 if (*skipwhite(name_end) == '.')
878 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
879 eap->cmd);
880 else
881 semsg(_(e_invalid_command_str), eap->cmd);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100882 return NULL;
883 }
884
885 // "def" or "def Name": list functions
886 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
887 return NULL;
888 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
889 }
890
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200891 // Only g:Func() can use a namespace.
892 if (name_start[1] == ':' && !is_global)
893 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200894 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200895 return NULL;
896 }
Bram Moolenaar4f3321f2022-03-13 13:12:27 +0000897 if (cctx->ctx_skip != SKIP_YES
898 && check_defined(name_start, name_end - name_start, cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000899 NULL, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +0200900 return NULL;
Bram Moolenaarf681cfb2022-02-07 20:30:57 +0000901 if (!ASCII_ISUPPER(is_global ? name_start[2] : name_start[0]))
902 {
Bram Moolenaar3787f262022-02-07 21:54:01 +0000903 semsg(_(e_function_name_must_start_with_capital_str), name_start);
Bram Moolenaarf681cfb2022-02-07 20:30:57 +0000904 return NULL;
905 }
Bram Moolenaareef21022020-08-01 22:16:43 +0200906
Bram Moolenaar04b12692020-05-04 23:24:44 +0200907 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100908 fill_exarg_from_cctx(eap, cctx);
909
Bram Moolenaar04b12692020-05-04 23:24:44 +0200910 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000911 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100912 lambda_name = vim_strsave(get_lambda_name());
913 if (lambda_name == NULL)
914 return NULL;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000915
916 // This may free the current line, make a copy of the name.
917 off = is_global ? 2 : 0;
918 func_name = vim_strnsave(name_start + off, name_end - name_start - off);
919 if (func_name == NULL)
920 {
921 r = FAIL;
922 goto theend;
923 }
924
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000925 ufunc = define_function(eap, lambda_name, lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +0200926 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100927 {
928 r = eap->skip ? OK : FAIL;
929 goto theend;
930 }
Bram Moolenaar7473a842021-12-28 17:55:26 +0000931 if (eap->nextcmd != NULL)
932 {
933 semsg(_(e_text_found_after_str_str),
934 eap->cmdidx == CMD_def ? "enddef" : "endfunction", eap->nextcmd);
935 r = FAIL;
Bram Moolenaard2939812021-12-30 17:09:05 +0000936 func_ptr_unref(ufunc);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000937 goto theend;
938 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100939
940 // copy over the block scope IDs before compiling
941 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
942 {
943 int block_depth = cctx->ctx_ufunc->uf_block_depth;
944
945 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
946 if (ufunc->uf_block_ids != NULL)
947 {
948 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
949 sizeof(int) * block_depth);
950 ufunc->uf_block_depth = block_depth;
951 }
952 }
953
Bram Moolenaara915fa02022-03-23 11:29:15 +0000954 // Define the funcref before compiling, so that it is found by any
955 // recursive call.
956 if (is_global)
957 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100958 // TODO: loop variable index and count
959 r = generate_NEWFUNC(cctx, lambda_name, func_name, 0, 0);
Bram Moolenaara915fa02022-03-23 11:29:15 +0000960 func_name = NULL;
961 lambda_name = NULL;
962 }
963 else
964 {
965 // Define a local variable for the function reference.
Bram Moolenaar1889f492022-08-16 19:34:44 +0100966 lvar = reserve_local(cctx, func_name, name_end - name_start,
Bram Moolenaara915fa02022-03-23 11:29:15 +0000967 TRUE, ufunc->uf_func_type);
Bram Moolenaara915fa02022-03-23 11:29:15 +0000968 if (lvar == NULL)
969 goto theend;
970 if (generate_FUNCREF(cctx, ufunc, &funcref_isn) == FAIL)
971 goto theend;
972 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
973 }
974
Bram Moolenaar139575d2022-03-15 19:29:30 +0000975 compile_type = get_compile_type(ufunc);
Bram Moolenaarffcfddc2021-07-11 20:22:30 +0200976#ifdef FEAT_PROFILE
977 // If the outer function is profiled, also compile the nested function for
978 // profiling.
979 if (cctx->ctx_compile_type == CT_PROFILE)
980 compile_type = CT_PROFILE;
981#endif
982 if (func_needs_compiling(ufunc, compile_type)
983 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +0200984 {
985 func_ptr_unref(ufunc);
Bram Moolenaar1889f492022-08-16 19:34:44 +0100986 if (lvar != NULL)
987 // Now the local variable can't be used.
988 *lvar->lv_name = '/'; // impossible value
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100989 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +0200990 }
Bram Moolenaar04b12692020-05-04 23:24:44 +0200991
Bram Moolenaar648594e2021-07-11 17:55:01 +0200992#ifdef FEAT_PROFILE
993 // When the outer function is compiled for profiling, the nested function
994 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +0200995 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200996 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
997#endif
998
Bram Moolenaara915fa02022-03-23 11:29:15 +0000999 // If a FUNCREF instruction was generated, set the index after compiling.
1000 if (funcref_isn != NULL && ufunc->uf_def_status == UF_COMPILED)
1001 funcref_isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001002
1003theend:
1004 vim_free(lambda_name);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001005 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001006 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02001007}
1008
1009/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001010 * Compile one Vim expression {expr} in string "p".
1011 * "p" points to the opening "{".
1012 * Return a pointer to the character after "}", NULL for an error.
1013 */
1014 char_u *
1015compile_one_expr_in_str(char_u *p, cctx_T *cctx)
1016{
1017 char_u *block_start;
1018 char_u *block_end;
1019
1020 // Skip the opening {.
1021 block_start = skipwhite(p + 1);
1022 block_end = block_start;
1023 if (*block_start != NUL && skip_expr(&block_end, NULL) == FAIL)
1024 return NULL;
1025 block_end = skipwhite(block_end);
1026 // The block must be closed by a }.
1027 if (*block_end != '}')
1028 {
1029 semsg(_(e_missing_close_curly_str), p);
1030 return NULL;
1031 }
1032 if (compile_expr0(&block_start, cctx) == FAIL)
1033 return NULL;
1034 may_generate_2STRING(-1, TRUE, cctx);
1035
1036 return block_end + 1;
1037}
1038
1039/*
LemonBoy2eaef102022-05-06 13:14:50 +01001040 * Compile a string "str" (either containing a literal string or a mix of
1041 * literal strings and Vim expressions of the form `{expr}`). This is used
1042 * when compiling a heredoc assignment to a variable or an interpolated string
1043 * in a Vim9 def function. Vim9 instructions are generated to push strings,
1044 * evaluate expressions, concatenate them and create a list of lines. When
1045 * "evalstr" is TRUE, Vim expressions in "str" are evaluated.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001046 */
1047 int
LemonBoy2eaef102022-05-06 13:14:50 +01001048compile_all_expr_in_str(char_u *str, int evalstr, cctx_T *cctx)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001049{
LemonBoy2eaef102022-05-06 13:14:50 +01001050 char_u *p = str;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001051 char_u *val;
LemonBoy2eaef102022-05-06 13:14:50 +01001052 int count = 0;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001053
1054 if (cctx->ctx_skip == SKIP_YES)
1055 return OK;
1056
LemonBoy2eaef102022-05-06 13:14:50 +01001057 if (!evalstr || *str == NUL)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001058 {
LemonBoy2eaef102022-05-06 13:14:50 +01001059 // Literal string, possibly empty.
1060 val = *str != NUL ? vim_strsave(str) : NULL;
1061 return generate_PUSHS(cctx, &val);
1062 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001063
LemonBoy2eaef102022-05-06 13:14:50 +01001064 // Push all the string pieces to the stack, followed by a ISN_CONCAT.
1065 while (*p != NUL)
1066 {
1067 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +01001068 int escaped_brace = FALSE;
1069
1070 // Look for a block start.
1071 lit_start = p;
1072 while (*p != '{' && *p != '}' && *p != NUL)
1073 ++p;
1074
1075 if (*p != NUL && *p == p[1])
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001076 {
LemonBoy2eaef102022-05-06 13:14:50 +01001077 // Escaped brace, unescape and continue.
1078 // Include the brace in the literal string.
1079 ++p;
1080 escaped_brace = TRUE;
1081 }
1082 else if (*p == '}')
1083 {
1084 semsg(_(e_stray_closing_curly_str), str);
1085 return FAIL;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001086 }
LemonBoy372bcce2022-04-25 12:43:20 +01001087
LemonBoy2eaef102022-05-06 13:14:50 +01001088 // Append the literal part.
1089 if (p != lit_start)
1090 {
1091 val = vim_strnsave(lit_start, (size_t)(p - lit_start));
1092 if (generate_PUSHS(cctx, &val) == FAIL)
1093 return FAIL;
1094 ++count;
1095 }
1096
1097 if (*p == NUL)
1098 break;
1099
1100 if (escaped_brace)
1101 {
1102 // Skip the second brace.
1103 ++p;
1104 continue;
1105 }
1106
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001107 p = compile_one_expr_in_str(p, cctx);
1108 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +01001109 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01001110 ++count;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001111 }
LemonBoy2eaef102022-05-06 13:14:50 +01001112
1113 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001114 if (count > 1)
LemonBoy2eaef102022-05-06 13:14:50 +01001115 return generate_CONCAT(cctx, count);
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001116
1117 return OK;
1118}
1119
1120/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 * Return the length of an assignment operator, or zero if there isn't one.
1122 */
1123 int
1124assignment_len(char_u *p, int *heredoc)
1125{
1126 if (*p == '=')
1127 {
1128 if (p[1] == '<' && p[2] == '<')
1129 {
1130 *heredoc = TRUE;
1131 return 3;
1132 }
1133 return 1;
1134 }
1135 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
1136 return 2;
1137 if (STRNCMP(p, "..=", 3) == 0)
1138 return 3;
1139 return 0;
1140}
1141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001143 * Generate the load instruction for "name".
1144 */
1145 static void
1146generate_loadvar(
1147 cctx_T *cctx,
1148 assign_dest_T dest,
1149 char_u *name,
1150 lvar_T *lvar,
1151 type_T *type)
1152{
1153 switch (dest)
1154 {
1155 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001156 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001157 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1158 break;
1159 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01001160 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001161 {
1162 if (name[2] == NUL)
1163 generate_instr_type(cctx, ISN_LOADGDICT, &t_dict_any);
1164 else
1165 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
1166 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01001167 else
1168 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001169 break;
1170 case dest_buffer:
1171 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
1172 break;
1173 case dest_window:
1174 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
1175 break;
1176 case dest_tab:
1177 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
1178 break;
1179 case dest_script:
1180 compile_load_scriptvar(cctx,
Bram Moolenaard0132f42022-05-12 11:05:40 +01001181 name + (name[1] == ':' ? 2 : 0), NULL, NULL);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001182 break;
1183 case dest_env:
1184 // Include $ in the name here
1185 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
1186 break;
1187 case dest_reg:
1188 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
1189 break;
1190 case dest_vimvar:
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001191 generate_LOADV(cctx, name + 2);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001192 break;
1193 case dest_local:
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001194 if (cctx->ctx_skip != SKIP_YES)
1195 {
1196 if (lvar->lv_from_outer > 0)
1197 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001198 lvar->lv_loop_idx, type);
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001199 else
1200 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
1201 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001202 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001203 case dest_expr:
1204 // list or dict value should already be on the stack.
1205 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001206 }
1207}
1208
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001209/*
1210 * Skip over "[expr]" or ".member".
1211 * Does not check for any errors.
1212 */
1213 static char_u *
1214skip_index(char_u *start)
1215{
1216 char_u *p = start;
1217
1218 if (*p == '[')
1219 {
1220 p = skipwhite(p + 1);
1221 (void)skip_expr(&p, NULL);
1222 p = skipwhite(p);
1223 if (*p == ']')
1224 return p + 1;
1225 return p;
1226 }
1227 // if (*p == '.')
1228 return to_name_end(p + 1, TRUE);
1229}
1230
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001231 void
1232vim9_declare_error(char_u *name)
1233{
1234 char *scope = "";
1235
1236 switch (*name)
1237 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001238 case 'g': scope = _("global"); break;
1239 case 'b': scope = _("buffer"); break;
1240 case 'w': scope = _("window"); break;
1241 case 't': scope = _("tab"); break;
1242 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001243 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001244 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001245 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001246 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001247 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001248 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001249 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001250 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001251 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001252}
1253
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001254/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001255 * For one assignment figure out the type of destination. Return it in "dest".
1256 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001257 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001258 * For a v:var "vimvaridx" is set.
1259 * "type" is set to the destination type if known, unchanted otherwise.
1260 * Return FAIL if an error message was given.
1261 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001262 int
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001263get_var_dest(
1264 char_u *name,
1265 assign_dest_T *dest,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001266 cmdidx_T cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001267 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001268 int *vimvaridx,
1269 type_T **type,
1270 cctx_T *cctx)
1271{
1272 char_u *p;
1273
1274 if (*name == '&')
1275 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001276 int cc;
1277 long numval;
1278 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001279 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001280
1281 *dest = dest_option;
1282 if (cmdidx == CMD_final || cmdidx == CMD_const)
1283 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001284 emsg(_(e_cannot_lock_option));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001285 return FAIL;
1286 }
1287 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001288 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001289 if (p == NULL)
1290 {
1291 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02001292 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001293 return FAIL;
1294 }
1295 cc = *p;
1296 *p = NUL;
1297 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001298 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001299 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001300 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001301 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001302 case gov_unknown:
Bram Moolenaare1242042021-12-16 20:56:57 +00001303 semsg(_(e_unknown_option_str), name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001304 return FAIL;
1305 case gov_string:
1306 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001307 if (opt_p_flags & P_FUNC)
1308 {
1309 // might be a Funcref, check the type later
1310 *type = &t_any;
1311 *dest = dest_func_option;
1312 }
1313 else
1314 {
1315 *type = &t_string;
1316 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001317 break;
1318 case gov_bool:
1319 case gov_hidden_bool:
1320 *type = &t_bool;
1321 break;
1322 case gov_number:
1323 case gov_hidden_number:
1324 *type = &t_number;
1325 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001326 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001327 }
1328 else if (*name == '$')
1329 {
1330 *dest = dest_env;
1331 *type = &t_string;
1332 }
1333 else if (*name == '@')
1334 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02001335 if (name[1] != '@'
1336 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001337 {
1338 emsg_invreg(name[1]);
1339 return FAIL;
1340 }
1341 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02001342 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001343 }
1344 else if (STRNCMP(name, "g:", 2) == 0)
1345 {
1346 *dest = dest_global;
1347 }
1348 else if (STRNCMP(name, "b:", 2) == 0)
1349 {
1350 *dest = dest_buffer;
1351 }
1352 else if (STRNCMP(name, "w:", 2) == 0)
1353 {
1354 *dest = dest_window;
1355 }
1356 else if (STRNCMP(name, "t:", 2) == 0)
1357 {
1358 *dest = dest_tab;
1359 }
1360 else if (STRNCMP(name, "v:", 2) == 0)
1361 {
1362 typval_T *vtv;
1363 int di_flags;
1364
1365 *vimvaridx = find_vim_var(name + 2, &di_flags);
1366 if (*vimvaridx < 0)
1367 {
1368 semsg(_(e_variable_not_found_str), name);
1369 return FAIL;
1370 }
1371 // We use the current value of "sandbox" here, is that OK?
1372 if (var_check_ro(di_flags, name, FALSE))
1373 return FAIL;
1374 *dest = dest_vimvar;
1375 vtv = get_vim_var_tv(*vimvaridx);
1376 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
1377 }
1378 return OK;
1379}
1380
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001381 static int
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001382is_decl_command(cmdidx_T cmdidx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001383{
1384 return cmdidx == CMD_let || cmdidx == CMD_var
1385 || cmdidx == CMD_final || cmdidx == CMD_const;
1386}
1387
1388/*
1389 * Figure out the LHS type and other properties for an assignment or one item
1390 * of ":unlet" with an index.
1391 * Returns OK or FAIL.
1392 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001393 int
Bram Moolenaar752fc692021-01-04 21:57:11 +01001394compile_lhs(
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001395 char_u *var_start,
1396 lhs_T *lhs,
1397 cmdidx_T cmdidx,
1398 int heredoc,
1399 int has_cmd, // "var" before "var_start"
1400 int oplen,
1401 cctx_T *cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001402{
1403 char_u *var_end;
1404 int is_decl = is_decl_command(cmdidx);
1405
1406 CLEAR_POINTER(lhs);
1407 lhs->lhs_dest = dest_local;
1408 lhs->lhs_vimvaridx = -1;
1409 lhs->lhs_scriptvar_idx = -1;
1410
1411 // "dest_end" is the end of the destination, including "[expr]" or
1412 // ".name".
1413 // "var_end" is the end of the variable/option/etc. name.
1414 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
1415 if (*var_start == '@')
1416 var_end = var_start + 2;
1417 else
1418 {
1419 // skip over the leading "&", "&l:", "&g:" and "$"
1420 var_end = skip_option_env_lead(var_start);
1421 var_end = to_name_end(var_end, TRUE);
1422 }
1423
1424 // "a: type" is declaring variable "a" with a type, not dict "a:".
1425 if (is_decl && lhs->lhs_dest_end == var_start + 2
1426 && lhs->lhs_dest_end[-1] == ':')
1427 --lhs->lhs_dest_end;
1428 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
1429 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001430 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001431
1432 // compute the length of the destination without "[expr]" or ".name"
1433 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02001434 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001435 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
1436 if (lhs->lhs_name == NULL)
1437 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01001438
1439 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
1440 // Something follows after the variable: "var[idx]" or "var.key".
1441 lhs->lhs_has_index = TRUE;
1442
Bram Moolenaar752fc692021-01-04 21:57:11 +01001443 if (heredoc)
1444 lhs->lhs_type = &t_list_string;
1445 else
1446 lhs->lhs_type = &t_any;
1447
1448 if (cctx->ctx_skip != SKIP_YES)
1449 {
1450 int declare_error = FALSE;
1451
1452 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
1453 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
1454 &lhs->lhs_type, cctx) == FAIL)
1455 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02001456 if (lhs->lhs_dest != dest_local
1457 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001458 {
1459 // Specific kind of variable recognized.
1460 declare_error = is_decl;
1461 }
1462 else
1463 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01001464 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001465 if (check_reserved_name(lhs->lhs_name) == FAIL)
1466 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001467
1468 if (lookup_local(var_start, lhs->lhs_varlen,
1469 &lhs->lhs_local_lvar, cctx) == OK)
1470 lhs->lhs_lvar = &lhs->lhs_local_lvar;
1471 else
1472 {
1473 CLEAR_FIELD(lhs->lhs_arg_lvar);
1474 if (arg_exists(var_start, lhs->lhs_varlen,
1475 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
1476 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
1477 {
1478 if (is_decl)
1479 {
1480 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
1481 return FAIL;
1482 }
1483 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
1484 }
1485 }
1486 if (lhs->lhs_lvar != NULL)
1487 {
1488 if (is_decl)
1489 {
1490 semsg(_(e_variable_already_declared), lhs->lhs_name);
1491 return FAIL;
1492 }
1493 }
1494 else
1495 {
1496 int script_namespace = lhs->lhs_varlen > 1
1497 && STRNCMP(var_start, "s:", 2) == 0;
1498 int script_var = (script_namespace
1499 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaardce24412022-02-08 20:35:30 +00001500 cctx, NULL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001501 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaardce24412022-02-08 20:35:30 +00001502 cctx, NULL)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001503 imported_T *import =
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001504 find_imported(var_start, lhs->lhs_varlen, FALSE);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001505
1506 if (script_namespace || script_var || import != NULL)
1507 {
1508 char_u *rawname = lhs->lhs_name
1509 + (lhs->lhs_name[1] == ':' ? 2 : 0);
1510
Bram Moolenaarafa048f2022-02-22 20:43:36 +00001511 if (script_namespace && current_script_is_vim9())
1512 {
1513 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
1514 var_start);
1515 return FAIL;
1516 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001517 if (is_decl)
1518 {
1519 if (script_namespace)
Bram Moolenaara749a422022-02-12 19:52:25 +00001520 semsg(_(e_cannot_declare_script_variable_in_function_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01001521 lhs->lhs_name);
1522 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001523 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01001524 lhs->lhs_name);
1525 return FAIL;
1526 }
1527 else if (cctx->ctx_ufunc->uf_script_ctx_version
1528 == SCRIPT_VERSION_VIM9
1529 && script_namespace
1530 && !script_var && import == NULL)
1531 {
1532 semsg(_(e_unknown_variable_str), lhs->lhs_name);
1533 return FAIL;
1534 }
1535
1536 lhs->lhs_dest = dest_script;
1537
1538 // existing script-local variables should have a type
1539 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
1540 if (import != NULL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001541 {
1542 char_u *dot = vim_strchr(var_start, '.');
1543 char_u *p;
1544
1545 // for an import the name is what comes after the dot
1546 if (dot == NULL)
1547 {
1548 semsg(_(e_no_dot_after_imported_name_str),
1549 var_start);
1550 return FAIL;
1551 }
1552 p = skipwhite(dot + 1);
1553 var_end = to_name_end(p, TRUE);
1554 if (var_end == p)
1555 {
1556 semsg(_(e_missing_name_after_imported_name_str),
1557 var_start);
1558 return FAIL;
1559 }
1560 vim_free(lhs->lhs_name);
1561 lhs->lhs_varlen = var_end - p;
1562 lhs->lhs_name = vim_strnsave(p, lhs->lhs_varlen);
1563 if (lhs->lhs_name == NULL)
1564 return FAIL;
1565 rawname = lhs->lhs_name;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001566 lhs->lhs_scriptvar_sid = import->imp_sid;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001567 // TODO: where do we check this name is exported?
1568
1569 // Check if something follows: "exp.var[idx]" or
1570 // "exp.var.key".
1571 lhs->lhs_has_index = lhs->lhs_dest_end
1572 > skipwhite(var_end);
1573 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001574 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
1575 {
Bram Moolenaar08251752021-01-11 21:20:18 +01001576 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01001577 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01001578 lhs->lhs_scriptvar_sid, rawname,
1579 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001580 cctx, NULL);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001581 if (lhs->lhs_scriptvar_idx >= 0)
1582 {
1583 scriptitem_T *si = SCRIPT_ITEM(
1584 lhs->lhs_scriptvar_sid);
1585 svar_T *sv =
1586 ((svar_T *)si->sn_var_vals.ga_data)
1587 + lhs->lhs_scriptvar_idx;
1588 lhs->lhs_type = sv->sv_type;
1589 }
1590 }
1591 }
Bram Moolenaardce24412022-02-08 20:35:30 +00001592 else if (check_defined(var_start, lhs->lhs_varlen, cctx,
1593 NULL, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001594 return FAIL;
1595 }
1596 }
1597
1598 if (declare_error)
1599 {
1600 vim9_declare_error(lhs->lhs_name);
1601 return FAIL;
1602 }
1603 }
1604
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001605 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01001606 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
1607 var_end = lhs->lhs_dest_end;
1608
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001609 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001610 {
1611 if (is_decl && *var_end == ':')
1612 {
1613 char_u *p;
1614
1615 // parse optional type: "let var: type = expr"
1616 if (!VIM_ISWHITE(var_end[1]))
1617 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001618 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001619 return FAIL;
1620 }
1621 p = skipwhite(var_end + 1);
1622 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
1623 if (lhs->lhs_type == NULL)
1624 return FAIL;
1625 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001626 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001627 }
1628 else if (lhs->lhs_lvar != NULL)
1629 lhs->lhs_type = lhs->lhs_lvar->lv_type;
1630 }
1631
Bram Moolenaare42939a2021-04-05 17:11:17 +02001632 if (oplen == 3 && !heredoc
1633 && lhs->lhs_dest != dest_global
1634 && !lhs->lhs_has_index
1635 && lhs->lhs_type->tt_type != VAR_STRING
1636 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001637 {
1638 emsg(_(e_can_only_concatenate_to_string));
1639 return FAIL;
1640 }
1641
1642 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
1643 && cctx->ctx_skip != SKIP_YES)
1644 {
1645 if (oplen > 1 && !heredoc)
1646 {
1647 // +=, /=, etc. require an existing variable
1648 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
1649 return FAIL;
1650 }
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001651 if (!is_decl || (lhs->lhs_has_index && !has_cmd
1652 && cctx->ctx_skip != SKIP_YES))
Bram Moolenaar752fc692021-01-04 21:57:11 +01001653 {
1654 semsg(_(e_unknown_variable_str), lhs->lhs_name);
1655 return FAIL;
1656 }
1657
Bram Moolenaar3f327882021-03-17 20:56:38 +01001658 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01001659 if ((lhs->lhs_type->tt_type == VAR_FUNC
1660 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01001661 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01001662 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01001663
1664 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01001665 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
1666 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
1667 if (lhs->lhs_lvar == NULL)
1668 return FAIL;
1669 lhs->lhs_new_local = TRUE;
1670 }
1671
1672 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01001673 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001674 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00001675 char_u *after = var_start + lhs->lhs_varlen;
1676 char_u *p;
1677
Bram Moolenaar752fc692021-01-04 21:57:11 +01001678 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001679 if (is_decl && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001680 {
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001681 if (has_cmd)
1682 emsg(_(e_cannot_use_index_when_declaring_variable));
1683 else
1684 semsg(_(e_unknown_variable_str), lhs->lhs_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001685 return FAIL;
1686 }
1687
Bram Moolenaarc750d912021-11-29 22:02:12 +00001688 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
1689 // Only the last index is used below, if there are others
1690 // before it generate code for the expression. Thus for
1691 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
1692 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001693 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00001694 p = skip_index(after);
1695 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01001696 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00001697 lhs->lhs_varlen_total = p - var_start;
1698 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001699 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00001700 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001701 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00001702 if (after > var_start + lhs->lhs_varlen)
1703 {
1704 lhs->lhs_varlen = after - var_start;
1705 lhs->lhs_dest = dest_expr;
1706 // We don't know the type before evaluating the expression,
1707 // use "any" until then.
1708 lhs->lhs_type = &t_any;
1709 }
1710
1711 if (lhs->lhs_type->tt_member == NULL)
1712 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001713 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00001714 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001715 }
1716 return OK;
1717}
1718
1719/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001720 * Figure out the LHS and check a few errors.
1721 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001722 int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001723compile_assign_lhs(
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001724 char_u *var_start,
1725 lhs_T *lhs,
1726 cmdidx_T cmdidx,
1727 int is_decl,
1728 int heredoc,
1729 int has_cmd, // "var" before "var_start"
1730 int oplen,
1731 cctx_T *cctx)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001732{
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001733 if (compile_lhs(var_start, lhs, cmdidx, heredoc, has_cmd, oplen, cctx)
1734 == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001735 return FAIL;
1736
1737 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
1738 {
1739 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
1740 return FAIL;
1741 }
1742 if (!is_decl && lhs->lhs_lvar != NULL
1743 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
1744 {
1745 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
1746 return FAIL;
1747 }
1748 return OK;
1749}
1750
1751/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001752 * Return TRUE if "lhs" has a range index: "[expr : expr]".
1753 */
1754 static int
1755has_list_index(char_u *idx_start, cctx_T *cctx)
1756{
1757 char_u *p = idx_start;
1758 int save_skip;
1759
1760 if (*p != '[')
1761 return FALSE;
1762
1763 p = skipwhite(p + 1);
1764 if (*p == ':')
1765 return TRUE;
1766
1767 save_skip = cctx->ctx_skip;
1768 cctx->ctx_skip = SKIP_YES;
1769 (void)compile_expr0(&p, cctx);
1770 cctx->ctx_skip = save_skip;
1771 return *skipwhite(p) == ':';
1772}
1773
1774/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02001775 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
1776 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01001777 */
1778 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02001779compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01001780 char_u *var_start,
1781 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02001782 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01001783 cctx_T *cctx)
1784{
Bram Moolenaar752fc692021-01-04 21:57:11 +01001785 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02001786 char_u *p;
1787 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02001788 int need_white_before = TRUE;
1789 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001790
Bram Moolenaar752fc692021-01-04 21:57:11 +01001791 p = var_start + varlen;
1792 if (*p == '[')
1793 {
1794 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02001795 if (*p == ':')
1796 {
1797 // empty first index, push zero
1798 r = generate_PUSHNR(cctx, 0);
1799 need_white_before = FALSE;
1800 }
1801 else
1802 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001803
1804 if (r == OK && *skipwhite(p) == ':')
1805 {
1806 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02001807 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02001808 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001809 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02001810 empty_second = *skipwhite(p + 1) == ']';
1811 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
1812 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001813 {
1814 semsg(_(e_white_space_required_before_and_after_str_at_str),
1815 ":", p);
1816 return FAIL;
1817 }
1818 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02001819 if (*p == ']')
1820 // empty second index, push "none"
1821 r = generate_PUSHSPEC(cctx, VVAL_NONE);
1822 else
1823 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001824 }
1825
Bram Moolenaar752fc692021-01-04 21:57:11 +01001826 if (r == OK && *skipwhite(p) != ']')
1827 {
1828 // this should not happen
Bram Moolenaare1242042021-12-16 20:56:57 +00001829 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar752fc692021-01-04 21:57:11 +01001830 r = FAIL;
1831 }
1832 }
1833 else // if (*p == '.')
1834 {
1835 char_u *key_end = to_name_end(p + 1, TRUE);
1836 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
1837
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001838 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001839 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02001840 return r;
1841}
1842
1843/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001844 * For a LHS with an index, load the variable to be indexed.
1845 */
1846 static int
1847compile_load_lhs(
1848 lhs_T *lhs,
1849 char_u *var_start,
1850 type_T *rhs_type,
1851 cctx_T *cctx)
1852{
1853 if (lhs->lhs_dest == dest_expr)
1854 {
1855 size_t varlen = lhs->lhs_varlen;
1856 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02001857 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001858 char_u *p = var_start;
Bram Moolenaare97976b2021-08-01 13:17:17 +02001859 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001860
Bram Moolenaare97976b2021-08-01 13:17:17 +02001861 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
1862 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001863 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02001864 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
1865 res = compile_expr0(&p, cctx);
1866 var_start[varlen] = c;
1867 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
1868 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001869 {
1870 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02001871 if (res != FAIL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001872 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001873 return FAIL;
1874 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001875
Bram Moolenaar078a4612022-01-04 15:17:03 +00001876 lhs->lhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
1877 : get_type_on_stack(cctx, 0);
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001878 // now we can properly check the type
1879 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
1880 && rhs_type != &t_void
1881 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
1882 FALSE, FALSE) == FAIL)
1883 return FAIL;
1884 }
1885 else
1886 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
1887 lhs->lhs_lvar, lhs->lhs_type);
1888 return OK;
1889}
1890
1891/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02001892 * Produce code for loading "lhs" and also take care of an index.
1893 * Return OK/FAIL.
1894 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001895 int
Bram Moolenaara369c3d2021-04-21 16:00:10 +02001896compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
1897{
1898 compile_load_lhs(lhs, var_start, NULL, cctx);
1899
1900 if (lhs->lhs_has_index)
1901 {
1902 int range = FALSE;
1903
1904 // Get member from list or dict. First compile the
1905 // index value.
1906 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
1907 return FAIL;
1908 if (range)
1909 {
1910 semsg(_(e_cannot_use_range_with_assignment_operator_str),
1911 var_start);
1912 return FAIL;
1913 }
1914
1915 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001916 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02001917 return FAIL;
1918 }
1919 return OK;
1920}
1921
1922/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02001923 * Assignment to a list or dict member, or ":unlet" for the item, using the
1924 * information in "lhs".
1925 * Returns OK or FAIL.
1926 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001927 int
Bram Moolenaare42939a2021-04-05 17:11:17 +02001928compile_assign_unlet(
1929 char_u *var_start,
1930 lhs_T *lhs,
1931 int is_assign,
1932 type_T *rhs_type,
1933 cctx_T *cctx)
1934{
Bram Moolenaare42939a2021-04-05 17:11:17 +02001935 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02001936 int range = FALSE;
1937
Bram Moolenaar68452172021-04-12 21:21:02 +02001938 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001939 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001940 if (is_assign && range
1941 && lhs->lhs_type->tt_type != VAR_LIST
1942 && lhs->lhs_type != &t_blob
1943 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02001944 {
1945 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
1946 return FAIL;
1947 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001948
1949 if (lhs->lhs_type == &t_any)
1950 {
1951 // Index on variable of unknown type: check at runtime.
1952 dest_type = VAR_ANY;
1953 }
1954 else
1955 {
1956 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001957 if (dest_type == VAR_DICT && range)
1958 {
1959 emsg(e_cannot_use_range_with_dictionary);
1960 return FAIL;
1961 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001962 if (dest_type == VAR_DICT
1963 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001964 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02001965 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001966 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02001967 type_T *type;
1968
1969 if (range)
1970 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001971 type = get_type_on_stack(cctx, 1);
Bram Moolenaar51e93322021-04-17 20:44:56 +02001972 if (need_type(type, &t_number,
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001973 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001974 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02001975 }
Bram Moolenaar078a4612022-01-04 15:17:03 +00001976 type = get_type_on_stack(cctx, 0);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001977 if ((dest_type != VAR_BLOB && type->tt_type != VAR_SPECIAL)
Bram Moolenaar51e93322021-04-17 20:44:56 +02001978 && need_type(type, &t_number,
1979 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001980 return FAIL;
1981 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001982 }
1983
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001984 if (cctx->ctx_skip == SKIP_YES)
1985 return OK;
1986
Bram Moolenaar752fc692021-01-04 21:57:11 +01001987 // Load the dict or list. On the stack we then have:
1988 // - value (for assignment, not for :unlet)
1989 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001990 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01001991 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001992 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
1993 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001994
Bram Moolenaar68452172021-04-12 21:21:02 +02001995 if (dest_type == VAR_LIST || dest_type == VAR_DICT
1996 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001997 {
1998 if (is_assign)
1999 {
Bram Moolenaar68452172021-04-12 21:21:02 +02002000 if (range)
2001 {
2002 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
2003 return FAIL;
2004 }
2005 else
2006 {
2007 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002008
Bram Moolenaar68452172021-04-12 21:21:02 +02002009 if (isn == NULL)
2010 return FAIL;
2011 isn->isn_arg.vartype = dest_type;
2012 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002013 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002014 else if (range)
2015 {
2016 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
2017 return FAIL;
2018 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002019 else
2020 {
2021 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
2022 return FAIL;
2023 }
2024 }
2025 else
2026 {
2027 emsg(_(e_indexable_type_required));
2028 return FAIL;
2029 }
2030
2031 return OK;
2032}
2033
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002034/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002035 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002036 * "let name"
2037 * "var name = expr"
2038 * "final name = expr"
2039 * "const name = expr"
2040 * "name = expr"
2041 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002042 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002043 * Return NULL for an error.
2044 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 */
2046 static char_u *
2047compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
2048{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002049 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002051 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 char_u *ret = NULL;
2053 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002054 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02002056 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 int oplen = 0;
2060 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002061 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002062 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002064 int is_decl = is_decl_command(cmdidx);
2065 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02002066 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067
Bram Moolenaare1d12112022-03-05 11:37:48 +00002068 // Skip over the "varname" or "[varname, varname]" to get to any "=".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002069 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
2070 if (p == NULL)
2071 return *arg == '[' ? arg : NULL;
2072
Bram Moolenaar752fc692021-01-04 21:57:11 +01002073 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002075 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
2076 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02002077 if (VIM_ISWHITE(eap->cmd[2]))
2078 {
2079 semsg(_(e_no_white_space_allowed_after_str_str),
2080 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
2081 return NULL;
2082 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002083 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
2084 oplen = 2;
2085 incdec = TRUE;
2086 }
Bram Moolenaar70d87692022-05-07 10:03:27 +01002087 else
2088 {
2089 sp = p;
2090 p = skipwhite(p);
2091 op = p;
2092 oplen = assignment_len(p, &heredoc);
2093
2094 if (var_count > 0 && oplen == 0)
2095 // can be something like "[1, 2]->func()"
2096 return arg;
2097
2098 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
2099 {
2100 error_white_both(op, oplen);
2101 return NULL;
2102 }
2103 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 if (heredoc)
2106 {
2107 list_T *l;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108
2109 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02002110 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 eap->cookie = cctx;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01002112 l = heredoc_get(eap, op + 3, FALSE, TRUE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02002113 if (l == NULL)
2114 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 list_free(l);
2117 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002118 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002120 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01002122 char_u *wp;
2123
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002124 // for "[var, var] = expr" evaluate the expression here, loop over the
2125 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01002126 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02002127
Bram Moolenaar004d9b02020-11-30 21:40:03 +01002128 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002129 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01002130 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002131 if (compile_expr0(&p, cctx) == FAIL)
2132 return NULL;
2133 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002135 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002137 type_T *stacktype;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00002138 int needed_list_len;
2139 int did_check = FALSE;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002140
Bram Moolenaar078a4612022-01-04 15:17:03 +00002141 stacktype = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2142 : get_type_on_stack(cctx, 0);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002143 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002145 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002146 goto theend;
2147 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01002148 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002149 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002150 goto theend;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00002151 // If a constant list was used we can check the length right here.
2152 needed_list_len = semicolon ? var_count - 1 : var_count;
2153 if (instr->ga_len > 0)
2154 {
2155 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2156
2157 if (isn->isn_type == ISN_NEWLIST)
2158 {
2159 did_check = TRUE;
2160 if (semicolon ? isn->isn_arg.number < needed_list_len
2161 : isn->isn_arg.number != needed_list_len)
2162 {
2163 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaarf60a6342022-01-15 14:16:37 +00002164 needed_list_len, (int)isn->isn_arg.number);
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00002165 goto theend;
2166 }
2167 }
2168 }
2169 if (!did_check)
2170 generate_CHECKLEN(cctx, needed_list_len, semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002171 if (stacktype->tt_member != NULL)
2172 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002173 }
2174 }
2175
2176 /*
2177 * Loop over variables in "[var, var] = expr".
2178 * For "var = expr" and "let var: type" this is done only once.
2179 */
2180 if (var_count > 0)
2181 var_start = skipwhite(arg + 1); // skip over the "["
2182 else
2183 var_start = arg;
2184 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
2185 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002186 int instr_count = -1;
2187 int save_lnum;
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002188 int skip_store = FALSE;
Bram Moolenaare88c6b72022-02-15 15:37:11 +00002189 type_T *inferred_type = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002190
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02002191 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
2192 {
2193 // Ignore underscore in "[a, _, b] = list".
2194 if (var_count > 0)
2195 {
2196 var_start = skipwhite(var_start + 2);
2197 continue;
2198 }
2199 emsg(_(e_cannot_use_underscore_here));
2200 goto theend;
2201 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002202 vim_free(lhs.lhs_name);
2203
2204 /*
2205 * Figure out the LHS type and other properties.
2206 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002207 if (compile_assign_lhs(var_start, &lhs, cmdidx,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002208 is_decl, heredoc, var_start > eap->cmd,
2209 oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002210 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02002211 if (heredoc)
2212 {
2213 SOURCING_LNUM = start_lnum;
2214 if (lhs.lhs_has_type
2215 && need_type(&t_list_string, lhs.lhs_type,
2216 -1, 0, cctx, FALSE, FALSE) == FAIL)
2217 goto theend;
2218 }
2219 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002220 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002221 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002222 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002223 if (oplen > 0 && var_count == 0)
2224 {
2225 // skip over the "=" and the expression
2226 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02002227 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002228 }
2229 }
2230 else if (oplen > 0)
2231 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002232 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01002233 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002234
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02002235 // for "+=", "*=", "..=" etc. first load the current value
2236 if (*op != '='
2237 && compile_load_lhs_with_index(&lhs, var_start,
2238 cctx) == FAIL)
2239 goto theend;
2240
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002241 // For "var = expr" evaluate the expression.
2242 if (var_count == 0)
2243 {
2244 int r;
2245
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002246 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002247 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002248 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01002249 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002250 r = generate_PUSHNR(cctx, 1);
2251 }
2252 else
2253 {
2254 // Temporarily hide the new local variable here, it is
2255 // not available to this expression.
2256 if (lhs.lhs_new_local)
2257 --cctx->ctx_locals.ga_len;
2258 wp = op + oplen;
2259 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
2260 {
2261 if (lhs.lhs_new_local)
2262 ++cctx->ctx_locals.ga_len;
2263 goto theend;
2264 }
2265 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002266 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01002267 ++cctx->ctx_locals.ga_len;
Bram Moolenaar21e51222020-12-04 12:43:29 +01002268 }
Yegappan Lakshmananc99e1822022-09-03 10:52:24 +01002269 if (r == FAIL)
2270 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002271 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02002272 else if (semicolon && var_idx == var_count - 1)
2273 {
2274 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02002275 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002276 if (generate_SLICE(cctx, var_count - 1) == FAIL)
2277 goto theend;
2278 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002279 else
2280 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002281 // For "[var, var] = expr" get the "var_idx" item from the
2282 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02002283 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01002284 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002285 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002286
Bram Moolenaar078a4612022-01-04 15:17:03 +00002287 rhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2288 : get_type_on_stack(cctx, 0);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002289 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002290 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002291 if ((rhs_type->tt_type == VAR_FUNC
2292 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01002293 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01002294 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02002295 goto theend;
2296
Bram Moolenaar752fc692021-01-04 21:57:11 +01002297 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002298 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002299 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002300 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002301 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002302 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002303 }
2304 else
2305 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02002306 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002307 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002308 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002309 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002310 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002311 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002312 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002313 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002314 else
Bram Moolenaare88c6b72022-02-15 15:37:11 +00002315 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002316 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaare88c6b72022-02-15 15:37:11 +00002317 inferred_type = rhs_type;
2318 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002319 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002320 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002321 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002322 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002323 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02002324 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002325
Bram Moolenaar77709b12021-04-03 21:01:01 +02002326 // Without operator check type here, otherwise below.
2327 // Use the line number of the assignment.
2328 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02002329 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
2330 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002331 // If assigning to a list or dict member, use the
2332 // member type. Not for "list[:] =".
2333 if (lhs.lhs_has_index
2334 && !has_list_index(var_start + lhs.lhs_varlen,
2335 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002336 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02002337 if (need_type_where(rhs_type, use_type, -1, where,
Bram Moolenaare88c6b72022-02-15 15:37:11 +00002338 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002339 goto theend;
2340 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002341 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002342 else
2343 {
2344 type_T *lhs_type = lhs.lhs_member_type;
2345
2346 // Special case: assigning to @# can use a number or a
2347 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02002348 // Also: can assign a number to a float.
2349 if ((lhs_type == &t_number_or_string
2350 || lhs_type == &t_float)
2351 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002352 lhs_type = &t_number;
2353 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01002354 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002355 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002358 else if (cmdidx == CMD_final)
2359 {
2360 emsg(_(e_final_requires_a_value));
2361 goto theend;
2362 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002363 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002365 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002366 goto theend;
2367 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002368 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option
2369 || lhs.lhs_dest == dest_func_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002370 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002371 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002372 goto theend;
2373 }
2374 else
2375 {
Bram Moolenaarc9af6172022-05-04 16:46:54 +01002376 int r = OK;
2377
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002378 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02002379 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002380 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002381 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002382 {
2383 case VAR_BOOL:
Bram Moolenaarc9af6172022-05-04 16:46:54 +01002384 r = generate_PUSHBOOL(cctx, VVAL_FALSE);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002385 break;
2386 case VAR_FLOAT:
Bram Moolenaarc9af6172022-05-04 16:46:54 +01002387 r = generate_PUSHF(cctx, 0.0);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002388 break;
2389 case VAR_STRING:
Bram Moolenaarc9af6172022-05-04 16:46:54 +01002390 r = generate_PUSHS(cctx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002391 break;
2392 case VAR_BLOB:
Bram Moolenaarc9af6172022-05-04 16:46:54 +01002393 r = generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002394 break;
2395 case VAR_FUNC:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002396 r = generate_PUSHFUNC(cctx, NULL, &t_func_void, TRUE);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002397 break;
2398 case VAR_LIST:
Bram Moolenaarc9af6172022-05-04 16:46:54 +01002399 r = generate_NEWLIST(cctx, 0, FALSE);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002400 break;
2401 case VAR_DICT:
Bram Moolenaarc9af6172022-05-04 16:46:54 +01002402 r = generate_NEWDICT(cctx, 0, FALSE);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002403 break;
2404 case VAR_JOB:
Bram Moolenaarc9af6172022-05-04 16:46:54 +01002405 r = generate_PUSHJOB(cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002406 break;
2407 case VAR_CHANNEL:
Bram Moolenaarc9af6172022-05-04 16:46:54 +01002408 r = generate_PUSHCHANNEL(cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002409 break;
2410 case VAR_NUMBER:
2411 case VAR_UNKNOWN:
2412 case VAR_ANY:
2413 case VAR_PARTIAL:
2414 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002415 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002416 case VAR_SPECIAL: // cannot happen
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002417 // This is skipped for local variables, they are always
2418 // initialized to zero. But in a "for" or "while" loop
2419 // the value may have been changed.
2420 if (lhs.lhs_dest == dest_local
2421 && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002422 skip_store = TRUE;
2423 else
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002424 {
2425 instr_count = instr->ga_len;
Bram Moolenaarc9af6172022-05-04 16:46:54 +01002426 r = generate_PUSHNR(cctx, 0);
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002427 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002428 break;
2429 }
Bram Moolenaarc9af6172022-05-04 16:46:54 +01002430 if (r == FAIL)
2431 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002432 }
2433 if (var_count == 0)
2434 end = p;
2435 }
2436
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002437 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002438 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002439 break;
2440
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002441 if (oplen > 0 && *op != '=')
2442 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002443 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02002444 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002445
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002446 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002447 {
2448 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2449 goto theend;
2450 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002451 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002452 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002453 expected = lhs.lhs_member_type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00002454 stacktype = get_type_on_stack(cctx, 0);
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002455 if (
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002456 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02002457 !(expected == &t_float && (stacktype == &t_number
2458 || stacktype == &t_number_bool)) &&
Bram Moolenaar351ead02021-01-16 16:07:01 +01002459 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002460 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002461 goto theend;
2462 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002463
2464 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02002465 {
LemonBoy372bcce2022-04-25 12:43:20 +01002466 if (generate_CONCAT(cctx, 2) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02002467 goto theend;
2468 }
2469 else if (*op == '+')
2470 {
2471 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01002472 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02002473 lhs.lhs_member_type, stacktype,
2474 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02002475 goto theend;
2476 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002477 else if (generate_two_op(cctx, op) == FAIL)
2478 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002481 // Use the line number of the assignment for store instruction.
2482 save_lnum = cctx->ctx_lnum;
2483 cctx->ctx_lnum = start_lnum - 1;
2484
Bram Moolenaar752fc692021-01-04 21:57:11 +01002485 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002486 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002487 // Use the info in "lhs" to store the value at the index in the
2488 // list or dict.
2489 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
2490 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002491 {
2492 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002493 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002494 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002495 }
2496 else
2497 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002498 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02002499 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01002500 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002501 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002502 generate_LOCKCONST(cctx);
2503
Bram Moolenaar10d6f182022-01-04 15:54:38 +00002504 if ((lhs.lhs_type->tt_type == VAR_DICT
Bram Moolenaar752fc692021-01-04 21:57:11 +01002505 || lhs.lhs_type->tt_type == VAR_LIST)
2506 && lhs.lhs_type->tt_member != NULL
Bram Moolenaare88c6b72022-02-15 15:37:11 +00002507 && lhs.lhs_type->tt_member != &t_any
Bram Moolenaar752fc692021-01-04 21:57:11 +01002508 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01002509 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar10d6f182022-01-04 15:54:38 +00002510 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01002511 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaare88c6b72022-02-15 15:37:11 +00002512 else if (inferred_type != NULL
2513 && (inferred_type->tt_type == VAR_DICT
2514 || inferred_type->tt_type == VAR_LIST)
2515 && inferred_type->tt_member != NULL
2516 && inferred_type->tt_member != &t_unknown
2517 && inferred_type->tt_member != &t_any)
2518 // Set the type in the list or dict, so that it can be checked,
2519 // also in legacy script.
2520 generate_SETTYPE(cctx, inferred_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01002521
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002522 if (!skip_store && generate_store_lhs(cctx, &lhs,
2523 instr_count, is_decl) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002524 {
2525 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002526 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002527 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002528 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002529 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002530
2531 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00002532 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002534
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02002535 // For "[var, var] = expr" drop the "expr" value.
2536 // Also for "[var, var; _] = expr".
2537 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02002538 {
Bram Moolenaarec792292020-12-13 21:26:56 +01002539 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02002540 goto theend;
2541 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002542
Bram Moolenaarb2097502020-07-19 17:17:02 +02002543 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544
2545theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01002546 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 return ret;
2548}
2549
2550/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01002551 * Check for an assignment at "eap->cmd", compile it if found.
2552 * Return NOTDONE if there is none, FAIL for failure, OK if done.
2553 */
2554 static int
2555may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
2556{
2557 char_u *pskip;
2558 char_u *p;
2559
2560 // Assuming the command starts with a variable or function name,
2561 // find what follows.
2562 // Skip over "var.member", "var[idx]" and the like.
2563 // Also "&opt = val", "$ENV = val" and "@r = val".
2564 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
2565 ? eap->cmd + 1 : eap->cmd;
2566 p = to_name_end(pskip, TRUE);
2567 if (p > eap->cmd && *p != NUL)
2568 {
2569 char_u *var_end;
2570 int oplen;
2571 int heredoc;
2572
2573 if (eap->cmd[0] == '@')
2574 var_end = eap->cmd + 2;
2575 else
2576 var_end = find_name_end(pskip, NULL, NULL,
2577 FNE_CHECK_START | FNE_INCL_BR);
2578 oplen = assignment_len(skipwhite(var_end), &heredoc);
2579 if (oplen > 0)
2580 {
2581 size_t len = p - eap->cmd;
2582
2583 // Recognize an assignment if we recognize the variable
2584 // name:
Bram Moolenaar17126b12021-01-07 22:03:02 +01002585 // "&opt = expr"
2586 // "$ENV = expr"
2587 // "@r = expr"
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00002588 // "g:var = expr"
2589 // "g:[key] = expr"
2590 // "local = expr" where "local" is a local var.
2591 // "script = expr" where "script" is a script-local var.
2592 // "import = expr" where "import" is an imported var
Bram Moolenaar17126b12021-01-07 22:03:02 +01002593 if (*eap->cmd == '&'
2594 || *eap->cmd == '$'
2595 || *eap->cmd == '@'
2596 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00002597 || STRNCMP(eap->cmd, "g:[", 3) == 0
Bram Moolenaare0890d62021-02-17 14:52:14 +01002598 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002599 {
2600 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
2601 if (*line == NULL || *line == eap->cmd)
2602 return FAIL;
2603 return OK;
2604 }
2605 }
2606 }
2607
2608 if (*eap->cmd == '[')
2609 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00002610 // might be "[var, var] = expr"
Bram Moolenaar17126b12021-01-07 22:03:02 +01002611 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
2612 if (*line == NULL)
2613 return FAIL;
2614 if (*line != eap->cmd)
2615 return OK;
2616 }
2617 return NOTDONE;
2618}
2619
Bram Moolenaar9a015112021-12-31 14:06:45 +00002620/*
2621 * Check if arguments of "ufunc" shadow variables in "cctx".
2622 * Return OK or FAIL.
2623 */
2624 static int
2625check_args_shadowing(ufunc_T *ufunc, cctx_T *cctx)
2626{
2627 int i;
2628 char_u *arg;
2629 int r = OK;
2630
2631 // Make sure arguments are not found when compiling a second time.
2632 ufunc->uf_args_visible = 0;
2633
2634 // Check for arguments shadowing variables from the context.
2635 for (i = 0; i < ufunc->uf_args.ga_len; ++i)
2636 {
2637 arg = ((char_u **)(ufunc->uf_args.ga_data))[i];
Bram Moolenaardce24412022-02-08 20:35:30 +00002638 if (check_defined(arg, STRLEN(arg), cctx, NULL, TRUE) == FAIL)
Bram Moolenaar9a015112021-12-31 14:06:45 +00002639 {
2640 r = FAIL;
2641 break;
2642 }
2643 }
2644 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
2645 return r;
2646}
2647
Bram Moolenaar139575d2022-03-15 19:29:30 +00002648/*
2649 * Get the compilation type that should be used for "ufunc".
2650 * Keep in sync with INSTRUCTIONS().
2651 */
2652 compiletype_T
2653get_compile_type(ufunc_T *ufunc)
2654{
2655 // Update uf_has_breakpoint if needed.
2656 update_has_breakpoint(ufunc);
2657
2658 if (debug_break_level > 0 || may_break_in_function(ufunc))
2659 return CT_DEBUG;
2660#ifdef FEAT_PROFILE
2661 if (do_profiling == PROF_YES)
2662 {
2663 if (!ufunc->uf_profiling && has_profiling(FALSE, ufunc->uf_name, NULL))
2664 func_do_profile(ufunc);
2665 if (ufunc->uf_profiling)
2666 return CT_PROFILE;
2667 }
2668#endif
2669 return CT_NONE;
2670}
2671
Bram Moolenaar7b829262021-10-13 15:04:34 +01002672
2673/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02002674 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002675 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02002676 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02002677 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02002678add_def_function(ufunc_T *ufunc)
2679{
2680 dfunc_T *dfunc;
2681
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002682 if (def_functions.ga_len == 0)
2683 {
2684 // The first position is not used, so that a zero uf_dfunc_idx means it
2685 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02002686 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002687 return FAIL;
2688 ++def_functions.ga_len;
2689 }
2690
Bram Moolenaar09689a02020-05-09 22:50:08 +02002691 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02002692 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02002693 return FAIL;
2694 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
2695 CLEAR_POINTER(dfunc);
2696 dfunc->df_idx = def_functions.ga_len;
2697 ufunc->uf_dfunc_idx = dfunc->df_idx;
2698 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002699 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002700 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002701 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02002702 ++def_functions.ga_len;
2703 return OK;
2704}
2705
2706/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 * After ex_function() has collected all the function lines: parse and compile
2708 * the lines into instructions.
2709 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01002710 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
2711 * the return statement (used for lambda). When uf_ret_type is already set
2712 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002713 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002714 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02002715 * This can be used recursively through compile_lambda(), which may reallocate
2716 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02002717 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02002719 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01002720compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02002721 ufunc_T *ufunc,
2722 int check_return_type,
2723 compiletype_T compile_type,
2724 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 char_u *line = NULL;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002727 garray_T lines_to_free;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 cctx_T cctx;
2731 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01002732 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02002733 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 int ret = FAIL;
2735 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02002736 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002737 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002738 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02002739 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01002740#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002741 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01002742#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002743 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002745 // allocated lines are freed at the end
2746 ga_init2(&lines_to_free, sizeof(char_u *), 50);
2747
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002748 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01002749 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002750 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02002752 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2753 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02002754 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002755
2756 switch (compile_type)
2757 {
2758 case CT_PROFILE:
2759#ifdef FEAT_PROFILE
2760 instr_dest = dfunc->df_instr_prof; break;
2761#endif
2762 case CT_NONE: instr_dest = dfunc->df_instr; break;
2763 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
2764 }
2765 if (instr_dest != NULL)
2766 // Was compiled in this mode before: Free old instructions.
2767 delete_def_function_contents(dfunc, FALSE);
2768 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002769 dfunc->df_defer_var_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02002771 else
2772 {
2773 if (add_def_function(ufunc) == FAIL)
2774 return FAIL;
2775 new_def_function = TRUE;
2776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777
Bram Moolenaar96923b72022-03-15 15:57:04 +00002778 if ((ufunc->uf_flags & FC_CLOSURE) && outer_cctx == NULL)
2779 {
2780 semsg(_(e_compiling_closure_without_context_str),
2781 printable_func_name(ufunc));
2782 return FAIL;
2783 }
2784
Bram Moolenaar985116a2020-07-12 17:31:09 +02002785 ufunc->uf_def_status = UF_COMPILING;
2786
Bram Moolenaara80faa82020-04-12 19:37:17 +02002787 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002788
Bram Moolenaare99d4222021-06-13 14:01:26 +02002789 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 cctx.ctx_ufunc = ufunc;
2791 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002792 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002794 // Each entry on the type stack consists of two type pointers.
2795 ga_init2(&cctx.ctx_type_stack, sizeof(type2_T), 50);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 cctx.ctx_type_list = &ufunc->uf_type_list;
2797 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
2798 instr = &cctx.ctx_instr;
2799
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002800 // Set the context to the function, it may be compiled when called from
2801 // another script. Set the script version to the most modern one.
2802 // The line number will be set in next_line_from_context().
2803 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
2805
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002806 // Don't use the flag from ":legacy" here.
2807 cmdmod.cmod_flags &= ~CMOD_LEGACY;
2808
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002809 // Make sure error messages are OK.
2810 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
2811 if (do_estack_push)
2812 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02002813 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002814
Bram Moolenaar9a015112021-12-31 14:06:45 +00002815 if (check_args_shadowing(ufunc, &cctx) == FAIL)
2816 goto erret;
2817
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002818 if (ufunc->uf_def_args.ga_len > 0)
2819 {
2820 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002821 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002822 int i;
2823 char_u *arg;
2824 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002825 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002826
2827 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01002828 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002829 for (i = 0; i < count; ++i)
2830 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002831 type_T *val_type;
2832 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002833 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01002834 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002835 int jump_instr_idx = instr->ga_len;
2836 isn_T *isn;
2837
2838 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
2839 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
2840 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01002841
2842 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02002843 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002844
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002845 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01002846 r = compile_expr0(&arg, &cctx);
2847
Bram Moolenaar12bce952021-03-11 20:04:04 +01002848 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002849 goto erret;
2850
2851 // If no type specified use the type of the default value.
2852 // Otherwise check that the default value type matches the
2853 // specified type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00002854 val_type = get_type_on_stack(&cctx, 0);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002855 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002856 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002857 {
2858 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002859 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002860 }
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00002861 else if (need_type_where(val_type, ufunc->uf_arg_types[arg_idx],
2862 -1, where, &cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002863 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002864
2865 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002866 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002867
2868 // set instruction index in JUMP_IF_ARG_SET to here
2869 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
2870 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002871 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002872
2873 if (did_set_arg_type)
2874 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002875 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02002876 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002877
2878 /*
2879 * Loop over all the lines of the function and generate instructions.
2880 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 for (;;)
2882 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002883 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002884 int starts_with_colon = FALSE;
2885 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002886 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01002887
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002888 // Bail out on the first error to avoid a flood of errors and report
2889 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01002890 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002891 goto erret;
2892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 if (line != NULL && *line == '|')
2894 // the line continues after a '|'
2895 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02002896 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02002897 && !(*line == '#' && (line == cctx.ctx_line_start
2898 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002900 semsg(_(e_trailing_characters_str), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 goto erret;
2902 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002903 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
2904 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 else
2906 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002907 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002908 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002909 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02002910 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01002911#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01002912 if (cctx.ctx_skip != SKIP_YES)
2913 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01002914#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01002916 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02002917 // Make a copy, splitting off nextcmd and removing trailing spaces
2918 // may change it.
2919 if (line != NULL)
2920 {
2921 line = vim_strsave(line);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002922 if (ga_add_string(&lines_to_free, line) == FAIL)
2923 goto erret;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02002924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 }
2926
Bram Moolenaara80faa82020-04-12 19:37:17 +02002927 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 ea.cmdlinep = &line;
2929 ea.cmd = skipwhite(line);
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002930 ea.skip = cctx.ctx_skip == SKIP_YES;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931
Bram Moolenaarb2049902021-01-24 12:53:53 +01002932 if (*ea.cmd == '#')
2933 {
Bram Moolenaarad6d9cc2022-08-08 21:43:11 +01002934 // "#" starts a comment, but "#{" is an error
2935 if (vim9_bad_comment(ea.cmd))
2936 goto erret;
Bram Moolenaarb2049902021-01-24 12:53:53 +01002937 line = (char_u *)"";
2938 continue;
2939 }
2940
Bram Moolenaarf002a412021-01-24 13:34:18 +01002941#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002942 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
2943 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002944 {
2945 may_generate_prof_end(&cctx, prof_lnum);
2946
2947 prof_lnum = cctx.ctx_lnum;
2948 generate_instr(&cctx, ISN_PROF_START);
2949 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01002950#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002951 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
2952 && cctx.ctx_skip != SKIP_YES)
2953 {
2954 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002955 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02002956 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002957 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01002958
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02002959 // Some things can be recognized by the first character.
2960 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02002962 case '}':
2963 {
2964 // "}" ends a block scope
2965 scopetype_T stype = cctx.ctx_scope == NULL
2966 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02002968 if (stype == BLOCK_SCOPE)
2969 {
2970 compile_endblock(&cctx);
2971 line = ea.cmd;
2972 }
2973 else
2974 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002975 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02002976 goto erret;
2977 }
2978 if (line != NULL)
2979 line = skipwhite(ea.cmd + 1);
2980 continue;
2981 }
2982
2983 case '{':
2984 // "{" starts a block scope
2985 // "{'a': 1}->func() is something else
2986 if (ends_excmd(*skipwhite(ea.cmd + 1)))
2987 {
2988 line = compile_block(ea.cmd, &cctx);
2989 continue;
2990 }
2991 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 }
2993
2994 /*
2995 * COMMAND MODIFIERS
2996 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02002997 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002998 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
2999 == FAIL)
Bram Moolenaarbc510062022-02-14 21:19:04 +00003000 goto erret;
Bram Moolenaare1004402020-10-24 20:49:43 +02003001 generate_cmdmods(&cctx, &local_cmdmod);
3002 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003004 // Check if there was a colon after the last command modifier or before
3005 // the current position.
3006 for (p = ea.cmd; p >= line; --p)
3007 {
3008 if (*p == ':')
3009 starts_with_colon = TRUE;
3010 if (p < ea.cmd && !VIM_ISWHITE(*p))
3011 break;
3012 }
3013
Bram Moolenaarce024c32021-06-26 13:00:49 +02003014 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02003015 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02003016 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02003017 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02003018 if (checkforcmd(&ea.cmd, "call", 3))
3019 {
3020 if (*ea.cmd == '(')
3021 // not for "call()"
3022 ea.cmd = p;
3023 else
3024 ea.cmd = skipwhite(ea.cmd);
3025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026
Bram Moolenaarce024c32021-06-26 13:00:49 +02003027 if (!starts_with_colon)
3028 {
3029 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003030
Bram Moolenaarce024c32021-06-26 13:00:49 +02003031 // Check for assignment after command modifiers.
3032 assign = may_compile_assignment(&ea, &line, &cctx);
3033 if (assign == OK)
3034 goto nextline;
3035 if (assign == FAIL)
3036 goto erret;
3037 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 }
3039
3040 /*
3041 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003042 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003043 * 0z1234->func() should not be confused with a zero line number
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003044 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003045 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar31d99482022-05-26 22:24:43 +01003046 * "123->func()" is a method call
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003048 cmd = ea.cmd;
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00003049 if ((*cmd != '$' || starts_with_colon)
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003050 && (starts_with_colon
3051 || !(*cmd == '\''
3052 || (cmd[0] == '0' && cmd[1] == 'z')
3053 || (cmd[0] != NUL && cmd[0] == cmd[1]
Bram Moolenaar31d99482022-05-26 22:24:43 +01003054 && (*cmd == '+' || *cmd == '-'))
3055 || number_method(cmd))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003056 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003057 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003058 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003059 {
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00003060 if (!starts_with_colon
3061 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003062 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01003063 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003064 goto erret;
3065 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01003066 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003067 if (ends_excmd2(line, ea.cmd))
3068 {
3069 // A range without a command: jump to the line.
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003070 generate_EXEC(&cctx, ISN_EXECRANGE,
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00003071 vim_strnsave(cmd, ea.cmd - cmd));
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003072 line = ea.cmd;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003073 goto nextline;
3074 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02003075 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003076 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02003077 p = find_ex_command(&ea, NULL,
3078 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003079 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003081 if (p == NULL)
3082 {
3083 if (cctx.ctx_skip != SKIP_YES)
Bram Moolenaarbed34f02022-01-19 20:48:37 +00003084 semsg(_(e_ambiguous_use_of_user_defined_command_str), ea.cmd);
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003085 goto erret;
3086 }
3087
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003088 // When using ":legacy cmd" always use compile_exec().
3089 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003090 {
3091 char_u *start = ea.cmd;
3092
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02003093 switch (ea.cmdidx)
3094 {
3095 case CMD_if:
3096 case CMD_elseif:
3097 case CMD_else:
3098 case CMD_endif:
3099 case CMD_for:
3100 case CMD_endfor:
3101 case CMD_continue:
3102 case CMD_break:
3103 case CMD_while:
3104 case CMD_endwhile:
3105 case CMD_try:
3106 case CMD_catch:
3107 case CMD_finally:
3108 case CMD_endtry:
3109 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
3110 goto erret;
3111 default: break;
3112 }
3113
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003114 // ":legacy return expr" needs to be handled differently.
3115 if (checkforcmd(&start, "return", 4))
3116 ea.cmdidx = CMD_return;
3117 else
3118 ea.cmdidx = CMD_legacy;
3119 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
3122 {
Bram Moolenaar97f8c102022-04-02 19:43:57 +01003123 // "eval" is used for "val->func()" and "var" for "var = val", then
3124 // "p" is equal to "ea.cmd" for a valid command.
3125 if (ea.cmdidx == CMD_eval || ea.cmdidx == CMD_var)
3126 ;
3127 else if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003128 {
3129 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01003130 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003131 }
Bram Moolenaar97f8c102022-04-02 19:43:57 +01003132 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01003134 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02003135 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 }
3138
Bram Moolenaar3988f642020-08-27 22:43:03 +02003139 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003140 && ea.cmdidx != CMD_elseif
3141 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02003142 && ea.cmdidx != CMD_endif
3143 && ea.cmdidx != CMD_endfor
3144 && ea.cmdidx != CMD_endwhile
3145 && ea.cmdidx != CMD_catch
3146 && ea.cmdidx != CMD_finally
3147 && ea.cmdidx != CMD_endtry)
3148 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02003149 emsg(_(e_unreachable_code_after_return));
3150 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02003151 }
3152
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003153 p = skipwhite(p);
3154 if (ea.cmdidx != CMD_SIZE
3155 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
3156 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02003157 if (ea.cmdidx >= 0)
3158 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003159 if ((ea.argt & EX_BANG) && *p == '!')
3160 {
3161 ea.forceit = TRUE;
3162 p = skipwhite(p + 1);
3163 }
Bram Moolenaar09d94212022-05-05 15:20:03 +01003164 if ((ea.argt & EX_RANGE) == 0 && ea.addr_count > 0)
3165 {
3166 emsg(_(e_no_range_allowed));
3167 goto erret;
3168 }
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003169 }
3170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 switch (ea.cmdidx)
3172 {
3173 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +00003174 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02003175 ea.arg = p;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00003176 line = compile_nested_function(&ea, &cctx, &lines_to_free);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003177 break;
3178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003180 line = compile_return(p, check_return_type,
3181 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003182 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 break;
3184
3185 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02003186 emsg(_(e_cannot_use_let_in_vim9_script));
3187 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003188 case CMD_var:
3189 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003191 case CMD_increment:
3192 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003194 if (line == p)
Bram Moolenaar8b716f52022-02-15 21:17:56 +00003195 {
3196 emsg(_(e_invalid_assignment));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003197 line = NULL;
Bram Moolenaar8b716f52022-02-15 21:17:56 +00003198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 break;
3200
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003201 case CMD_unlet:
3202 case CMD_unlockvar:
3203 case CMD_lockvar:
3204 line = compile_unletlock(p, &ea, &cctx);
3205 break;
3206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02003208 emsg(_(e_import_can_only_be_used_in_script));
3209 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 break;
3211
3212 case CMD_if:
3213 line = compile_if(p, &cctx);
3214 break;
3215 case CMD_elseif:
3216 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003217 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 break;
3219 case CMD_else:
3220 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003221 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 break;
3223 case CMD_endif:
3224 line = compile_endif(p, &cctx);
3225 break;
3226
3227 case CMD_while:
3228 line = compile_while(p, &cctx);
3229 break;
3230 case CMD_endwhile:
3231 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003232 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 break;
3234
3235 case CMD_for:
3236 line = compile_for(p, &cctx);
3237 break;
3238 case CMD_endfor:
3239 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003240 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 break;
3242 case CMD_continue:
3243 line = compile_continue(p, &cctx);
3244 break;
3245 case CMD_break:
3246 line = compile_break(p, &cctx);
3247 break;
3248
3249 case CMD_try:
3250 line = compile_try(p, &cctx);
3251 break;
3252 case CMD_catch:
3253 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003254 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 break;
3256 case CMD_finally:
3257 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003258 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 break;
3260 case CMD_endtry:
3261 line = compile_endtry(p, &cctx);
3262 break;
3263 case CMD_throw:
3264 line = compile_throw(p, &cctx);
3265 break;
3266
Bram Moolenaar007f9d62020-07-06 23:04:49 +02003267 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02003268 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02003269 break;
3270
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003271 case CMD_defer:
3272 line = compile_defer(p, &cctx);
3273 break;
3274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 case CMD_echon:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003277 case CMD_echoconsole:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003278 case CMD_echoerr:
3279 case CMD_echomsg:
3280#ifdef HAS_MESSAGE_WINDOW
3281 case CMD_echowindow:
3282#endif
3283 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003284 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003285 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003287 case CMD_put:
3288 ea.cmd = cmd;
3289 line = compile_put(p, &ea, &cctx);
3290 break;
3291
Bram Moolenaar4c137212021-04-19 16:48:48 +02003292 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +01003293 if (check_global_and_subst(ea.cmd, p) == FAIL)
3294 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003295 if (cctx.ctx_skip == SKIP_YES)
3296 line = (char_u *)"";
3297 else
3298 {
3299 ea.arg = p;
3300 line = compile_substitute(line, &ea, &cctx);
3301 }
3302 break;
3303
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003304 case CMD_redir:
3305 ea.arg = p;
3306 line = compile_redir(line, &ea, &cctx);
3307 break;
3308
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003309 case CMD_cexpr:
3310 case CMD_lexpr:
3311 case CMD_caddexpr:
3312 case CMD_laddexpr:
3313 case CMD_cgetexpr:
3314 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003315#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003316 ea.arg = p;
3317 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003318#else
3319 ex_ni(&ea);
3320 line = NULL;
3321#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003322 break;
3323
Bram Moolenaarae616492020-07-28 20:07:27 +02003324 case CMD_append:
3325 case CMD_change:
3326 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003327 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02003328 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02003329 case CMD_xit:
3330 not_in_vim9(&ea);
3331 goto erret;
3332
Bram Moolenaar002262f2020-07-08 17:47:57 +02003333 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02003334 if (cctx.ctx_skip != SKIP_YES)
3335 {
3336 semsg(_(e_invalid_command_str), ea.cmd);
3337 goto erret;
3338 }
3339 // We don't check for a next command here.
3340 line = (char_u *)"";
3341 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02003342
Bram Moolenaar20677332021-06-06 17:02:53 +02003343 case CMD_lua:
3344 case CMD_mzscheme:
3345 case CMD_perl:
3346 case CMD_py3:
3347 case CMD_python3:
3348 case CMD_python:
3349 case CMD_pythonx:
3350 case CMD_ruby:
3351 case CMD_tcl:
3352 ea.arg = p;
3353 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02003354 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02003355 else
3356 // heredoc lines have been concatenated with NL
3357 // characters in get_function_body()
3358 line = compile_script(line, &cctx);
3359 break;
3360
Bram Moolenaar107f7322022-02-06 17:30:41 +00003361 case CMD_vim9script:
Bram Moolenaar8cbf2492022-02-06 20:28:13 +00003362 if (cctx.ctx_skip != SKIP_YES)
3363 {
3364 emsg(_(e_vim9script_can_only_be_used_in_script));
3365 goto erret;
3366 }
3367 line = (char_u *)"";
3368 break;
Bram Moolenaar107f7322022-02-06 17:30:41 +00003369
Bram Moolenaar7b829262021-10-13 15:04:34 +01003370 case CMD_global:
3371 if (check_global_and_subst(ea.cmd, p) == FAIL)
3372 goto erret;
3373 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +02003374 default:
3375 // Not recognized, execute with do_cmdline_cmd().
3376 ea.arg = p;
3377 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 break;
3379 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003380nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 if (line == NULL)
3382 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02003383 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003385 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02003386 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 if (cctx.ctx_type_stack.ga_len < 0)
3389 {
3390 iemsg("Type stack underflow");
3391 goto erret;
3392 }
3393 }
3394
3395 if (cctx.ctx_scope != NULL)
3396 {
3397 if (cctx.ctx_scope->se_type == IF_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00003398 emsg(_(e_missing_endif));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00003400 emsg(_(e_missing_endwhile));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00003402 emsg(_(e_missing_endfor));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003404 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 goto erret;
3406 }
3407
Bram Moolenaarefd88552020-06-18 20:50:10 +02003408 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02003410 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
3411 ufunc->uf_ret_type = &t_void;
3412 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003414 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 goto erret;
3416 }
3417
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003418 // Return void if there is no return at the end.
3419 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 }
3421
Bram Moolenaar599410c2021-04-10 14:03:43 +02003422 // When compiled with ":silent!" and there was an error don't consider the
3423 // function compiled.
3424 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02003425 {
3426 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3427 + ufunc->uf_dfunc_idx;
3428 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003429 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003430#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02003431 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003432 {
3433 dfunc->df_instr_prof = instr->ga_data;
3434 dfunc->df_instr_prof_count = instr->ga_len;
3435 }
3436 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01003437#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02003438 if (cctx.ctx_compile_type == CT_DEBUG)
3439 {
3440 dfunc->df_instr_debug = instr->ga_data;
3441 dfunc->df_instr_debug_count = instr->ga_len;
3442 }
3443 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01003444 {
3445 dfunc->df_instr = instr->ga_data;
3446 dfunc->df_instr_count = instr->ga_len;
3447 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003448 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003449 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01003450
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003451 if (cctx.ctx_outer_used)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01003452 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003453 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01003454 if (outer_cctx != NULL)
3455 ++outer_cctx->ctx_closure_count;
3456 }
3457
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003458 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02003459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460
3461 ret = OK;
3462
3463erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02003464 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02003466 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3467 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003468
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02003469 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02003470 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003471 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02003472 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003473
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02003474 // If using the last entry in the table and it was added above, we
3475 // might as well remove it.
3476 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02003477 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02003478 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003479 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02003480 ufunc->uf_dfunc_idx = 0;
3481 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003482 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003483
Bram Moolenaar3cca2992020-04-02 22:57:36 +02003484 while (cctx.ctx_scope != NULL)
3485 drop_scope(&cctx);
3486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 if (errormsg != NULL)
3488 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01003489 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02003490 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 }
3492
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003493 if (cctx.ctx_redir_lhs.lhs_name != NULL)
3494 {
3495 if (ret == OK)
3496 {
3497 emsg(_(e_missing_redir_end));
3498 ret = FAIL;
3499 }
3500 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02003501 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003502 }
3503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02003505 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02003506 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003507 if (do_estack_push)
3508 estack_pop();
3509
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00003510 ga_clear_strings(&lines_to_free);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003511 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02003513 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514}
3515
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003516 void
3517set_function_type(ufunc_T *ufunc)
3518{
3519 int varargs = ufunc->uf_va_name != NULL;
3520 int argcount = ufunc->uf_args.ga_len;
3521
3522 // Create a type for the function, with the return type and any
3523 // argument types.
3524 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
3525 // The type is included in "tt_args".
3526 if (argcount > 0 || varargs)
3527 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01003528 if (ufunc->uf_type_list.ga_itemsize == 0)
3529 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003530 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
3531 argcount, &ufunc->uf_type_list);
3532 // Add argument types to the function type.
3533 if (func_type_add_arg_types(ufunc->uf_func_type,
3534 argcount + varargs,
3535 &ufunc->uf_type_list) == FAIL)
3536 return;
3537 ufunc->uf_func_type->tt_argcount = argcount + varargs;
3538 ufunc->uf_func_type->tt_min_argcount =
3539 argcount - ufunc->uf_def_args.ga_len;
3540 if (ufunc->uf_arg_types == NULL)
3541 {
3542 int i;
3543
3544 // lambda does not have argument types.
3545 for (i = 0; i < argcount; ++i)
3546 ufunc->uf_func_type->tt_args[i] = &t_any;
3547 }
3548 else
3549 mch_memmove(ufunc->uf_func_type->tt_args,
3550 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
3551 if (varargs)
3552 {
3553 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02003554 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003555 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
3556 }
3557 }
3558 else
3559 // No arguments, can use a predefined type.
3560 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
3561 argcount, &ufunc->uf_type_list);
3562}
3563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003565 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01003566 */
3567 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003568delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01003569{
3570 int idx;
3571
3572 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003573 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003574
3575 if (dfunc->df_instr != NULL)
3576 {
3577 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
3578 delete_instr(dfunc->df_instr + idx);
3579 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003580 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003581 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02003582 if (dfunc->df_instr_debug != NULL)
3583 {
3584 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
3585 delete_instr(dfunc->df_instr_debug + idx);
3586 VIM_CLEAR(dfunc->df_instr_debug);
3587 dfunc->df_instr_debug = NULL;
3588 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01003589#ifdef FEAT_PROFILE
3590 if (dfunc->df_instr_prof != NULL)
3591 {
3592 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
3593 delete_instr(dfunc->df_instr_prof + idx);
3594 VIM_CLEAR(dfunc->df_instr_prof);
3595 dfunc->df_instr_prof = NULL;
3596 }
3597#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01003598
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003599 if (mark_deleted)
3600 dfunc->df_deleted = TRUE;
3601 if (dfunc->df_ufunc != NULL)
3602 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003603}
3604
3605/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003606 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003607 * function, unless another user function still uses it.
3608 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 */
3610 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003611unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003613 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 {
3615 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3616 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003618 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003619 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003620 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003621 ufunc->uf_dfunc_idx = 0;
3622 if (dfunc->df_ufunc == ufunc)
3623 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 }
3625}
3626
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003627/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003628 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003629 */
3630 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003631link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003632{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003633 if (ufunc->uf_dfunc_idx > 0)
3634 {
3635 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3636 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003637
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003638 ++dfunc->df_refcount;
3639 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003640}
3641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01003643/*
3644 * Free all functions defined with ":def".
3645 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 void
3647free_def_functions(void)
3648{
Bram Moolenaar20431c92020-03-20 18:39:46 +01003649 int idx;
3650
3651 for (idx = 0; idx < def_functions.ga_len; ++idx)
3652 {
3653 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
3654
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003655 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003656 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003657 }
3658
3659 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660}
3661#endif
3662
3663
3664#endif // FEAT_EVAL