blob: 5f80988b80ab483ffbb4c912061f2166c4b71673 [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 Moolenaar709664c2020-12-12 14:33:41 +010057 }
58 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061
62 // Find local in outer function scope.
63 if (cctx->ctx_outer != NULL)
64 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010065 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020066 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010067 if (lvar != NULL)
68 {
69 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +010070 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +010071 }
72 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020073 }
74 }
75
Bram Moolenaar709664c2020-12-12 14:33:41 +010076 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077}
78
79/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +020080 * Lookup an argument in the current function and an enclosing function.
81 * Returns the argument index in "idxp"
82 * Returns the argument type in "type"
83 * Sets "gen_load_outer" to TRUE if found in outer scope.
84 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +000086 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +020087arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +020088 char_u *name,
89 size_t len,
90 int *idxp,
91 type_T **type,
92 int *gen_load_outer,
93 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010094{
95 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +020096 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097
Bram Moolenaarae8d2de2020-02-13 21:42:24 +010098 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +020099 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200100 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100101 {
102 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
103
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200104 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
105 {
106 if (idxp != NULL)
107 {
108 // Arguments are located above the frame pointer. One further
109 // if there is a vararg argument
110 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
111 + STACK_FRAME_SIZE)
112 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
113
114 if (cctx->ctx_ufunc->uf_arg_types != NULL)
115 *type = cctx->ctx_ufunc->uf_arg_types[idx];
116 else
117 *type = &t_any;
118 }
119 return OK;
120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100122
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200123 va_name = cctx->ctx_ufunc->uf_va_name;
124 if (va_name != NULL
125 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
126 {
127 if (idxp != NULL)
128 {
129 // varargs is always the last argument
130 *idxp = -STACK_FRAME_SIZE - 1;
131 *type = cctx->ctx_ufunc->uf_va_type;
132 }
133 return OK;
134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200136 if (cctx->ctx_outer != NULL)
137 {
138 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200139 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200140 == OK)
141 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100142 if (gen_load_outer != NULL)
143 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200144 return OK;
145 }
146 }
147
148 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149}
150
151/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200152 * Lookup a script-local variable in the current script, possibly defined in a
153 * block that contains the function "cctx->ctx_ufunc".
154 * "cctx" is NULL at the script level.
Bram Moolenaardce24412022-02-08 20:35:30 +0000155 * "cstack_T" 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 Moolenaardce24412022-02-08 20:35:30 +0000187 // Not in a function scope, find variable with block ID equal to or
188 // smaller than the current block id. If "cstack" is not NULL go up
189 // the block scopes (more accurate).
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200190 while (sav != NULL)
191 {
Bram Moolenaardce24412022-02-08 20:35:30 +0000192 if (cstack != NULL)
193 {
194 int idx;
195
196 for (idx = cstack->cs_idx; idx >= 0; --idx)
197 if (cstack->cs_block_id[idx] == sav->sav_block_id)
198 break;
199 if (idx >= 0)
200 break;
201 }
202 else if (sav->sav_block_id <= si->sn_current_block_id)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200203 break;
204 sav = sav->sav_next;
205 }
206 return sav;
207 }
208
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200209 // Go over the variables with this name and find one that was visible
210 // from the function.
211 ufunc = cctx->ctx_ufunc;
212 while (sav != NULL)
213 {
214 int idx;
215
216 // Go over the blocks that this function was defined in. If the
217 // variable block ID matches it was visible to the function.
218 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
219 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
220 return sav;
221 sav = sav->sav_next;
222 }
223
Bram Moolenaar58493cf2022-01-06 12:23:30 +0000224 // Not found, variable was not visible.
225 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200226}
227
228/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100229 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200230 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000231 int
Bram Moolenaar84367732020-08-23 15:21:55 +0200232script_is_vim9()
233{
234 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
235}
236
237/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200238 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200239 * "cctx" is NULL at the script level.
Bram Moolenaardce24412022-02-08 20:35:30 +0000240 * "cstack" is NULL in a function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241 * Returns OK or FAIL.
242 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000243 int
Bram Moolenaardce24412022-02-08 20:35:30 +0000244script_var_exists(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200246 if (current_sctx.sc_sid <= 0)
247 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200248 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200249 {
250 // Check script variables that were visible where the function was
251 // defined.
Bram Moolenaardce24412022-02-08 20:35:30 +0000252 if (find_script_var(name, len, cctx, cstack) != NULL)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200253 return OK;
254 }
255 else
256 {
257 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
258 dictitem_T *di;
259 int cc;
260
261 // Check script variables that are currently visible
262 cc = name[len];
263 name[len] = NUL;
264 di = find_var_in_ht(ht, 0, name, TRUE);
265 name[len] = cc;
266 if (di != NULL)
267 return OK;
268 }
269
270 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271}
272
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100273/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100274 * Return TRUE if "name" is a local variable, argument, script variable or
275 * imported.
276 */
277 static int
278variable_exists(char_u *name, size_t len, cctx_T *cctx)
279{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100280 return (cctx != NULL
281 && (lookup_local(name, len, NULL, cctx) == OK
282 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaardce24412022-02-08 20:35:30 +0000283 || script_var_exists(name, len, cctx, NULL) == OK
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000284 || find_imported(name, len, FALSE, cctx) != NULL;
Bram Moolenaare0890d62021-02-17 14:52:14 +0100285}
286
287/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100288 * Return TRUE if "name" is a local variable, argument, script variable,
289 * imported or function.
290 */
291 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100292item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100293{
294 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100295 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100296
297 if (variable_exists(name, len, cctx))
298 return TRUE;
299
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100300 // This is similar to what is in lookup_scriptitem():
301 // Find a function, so that a following "->" works.
302 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
303 // a function call.
304 p = skipwhite(name + len);
305
306 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
307 {
308 // Do not check for an internal function, since it might also be a
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200309 // valid command, such as ":split" versus "split()".
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100310 // Skip "g:" before a function name.
311 is_global = (name[0] == 'g' && name[1] == ':');
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000312 return find_func(is_global ? name + 2 : name, is_global) != NULL;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100313 }
314 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100315}
316
317/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100318 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200319 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200320 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100321 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100322 * Return FAIL and give an error if it defined.
323 */
324 int
Bram Moolenaardce24412022-02-08 20:35:30 +0000325check_defined(
326 char_u *p,
327 size_t len,
328 cctx_T *cctx,
329 cstack_T *cstack,
330 int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100331{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200332 int c = p[len];
333 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200334
Bram Moolenaarda479c72021-04-10 21:01:38 +0200335 // underscore argument is OK
336 if (len == 1 && *p == '_')
337 return OK;
338
Bram Moolenaardce24412022-02-08 20:35:30 +0000339 if (script_var_exists(p, len, cctx, cstack) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100340 {
341 if (is_arg)
342 semsg(_(e_argument_already_declared_in_script_str), p);
343 else
344 semsg(_(e_variable_already_declared_in_script_str), p);
345 return FAIL;
346 }
347
Bram Moolenaarad486a02020-08-01 23:22:18 +0200348 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100349 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100350 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200351 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000352 || find_imported(p, len, FALSE, cctx) != NULL
Bram Moolenaaracc4b562022-01-24 13:54:45 +0000353 || (ufunc = find_func_even_dead(p, 0)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100354 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200355 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200356 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
357 && (!func_is_global(ufunc)
358 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200359 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100360 if (is_arg)
361 semsg(_(e_argument_name_shadows_existing_variable_str), p);
362 else
363 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200364 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200365 return FAIL;
366 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100367 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200368 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100369 return OK;
370}
371
Bram Moolenaar65b95452020-07-19 14:03:09 +0200372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200374 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
375 * used. Return FALSE if the types will never match.
376 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200377 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200378use_typecheck(type_T *actual, type_T *expected)
379{
380 if (actual->tt_type == VAR_ANY
381 || actual->tt_type == VAR_UNKNOWN
382 || (actual->tt_type == VAR_FUNC
383 && (expected->tt_type == VAR_FUNC
384 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000385 && (actual->tt_member == &t_any
386 || actual->tt_member == &t_unknown
387 || actual->tt_argcount < 0)
388 && (actual->tt_member == &t_unknown ||
389 (actual->tt_member == &t_void)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100390 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200391 return TRUE;
392 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
393 && actual->tt_type == expected->tt_type)
394 // This takes care of a nested list or dict.
395 return use_typecheck(actual->tt_member, expected->tt_member);
396 return FALSE;
397}
398
399/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200400 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200401 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200402 * - "actual" is a type that can be "expected" type: add a runtime check; or
403 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200404 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
405 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200406 */
Bram Moolenaara1c51952022-02-02 16:20:26 +0000407 int
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200408need_type_where(
409 type_T *actual,
410 type_T *expected,
411 int offset,
412 where_T where,
413 cctx_T *cctx,
414 int silent,
415 int actual_is_const)
416{
Bram Moolenaar44a89772021-12-18 12:31:33 +0000417 int ret;
418
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200419 if (expected == &t_bool && actual != &t_bool
420 && (actual->tt_flags & TTFLAG_BOOL_OK))
421 {
422 // Using "0", "1" or the result of an expression with "&&" or "||" as a
423 // boolean is OK but requires a conversion.
424 generate_2BOOL(cctx, FALSE, offset);
425 return OK;
426 }
427
Bram Moolenaar44a89772021-12-18 12:31:33 +0000428 ret = check_type_maybe(expected, actual, FALSE, where);
429 if (ret == OK)
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200430 return OK;
431
432 // If the actual type can be the expected type add a runtime check.
433 // If it's a constant a runtime check makes no sense.
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 {
436 generate_TYPECHECK(cctx, expected, offset, where.wt_index);
437 return OK;
438 }
439
440 if (!silent)
441 type_mismatch_where(expected, actual, where);
442 return FAIL;
443}
444
Bram Moolenaar351ead02021-01-16 16:07:01 +0100445 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200446need_type(
447 type_T *actual,
448 type_T *expected,
449 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100450 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200451 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200452 int silent,
453 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200454{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200455 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100456
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100457 where.wt_index = arg_idx;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200458 return need_type_where(actual, expected, offset, where,
459 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200460}
461
462/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100463 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200464 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000466 lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +0200467reserve_local(
468 cctx_T *cctx,
469 char_u *name,
470 size_t len,
471 int isConst,
472 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100473{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200475 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200477 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200479 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200480 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 }
482
Bram Moolenaar35578162021-08-02 19:10:38 +0200483 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200484 return NULL;
485 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +0200486 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200488 // Every local variable uses the next entry on the stack. We could re-use
489 // the last ones when leaving a scope, but then variables used in a closure
490 // might get overwritten. To keep things simple do not re-use stack
491 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200492 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
493 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200494
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200495 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 lvar->lv_const = isConst;
497 lvar->lv_type = type;
498
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200499 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +0200500 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200501 return NULL;
502 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
503 vim_strsave(lvar->lv_name);
504 ++dfunc->df_var_names.ga_len;
505
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200506 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507}
508
509/*
Bram Moolenaar08251752021-01-11 21:20:18 +0100510 * If "check_writable" is ASSIGN_CONST give an error if the variable was
511 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
512 * error if the variable was defined with :const.
513 */
514 static int
515check_item_writable(svar_T *sv, int check_writable, char_u *name)
516{
517 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
518 || (check_writable == ASSIGN_FINAL
519 && sv->sv_const == ASSIGN_CONST))
520 {
Bram Moolenaard8e44472021-07-21 22:20:33 +0200521 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +0100522 return FAIL;
523 }
524 return OK;
525}
526
527/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +0100529 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 * Returns the index in "sn_var_vals" if found.
531 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +0100532 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 */
534 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200535get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536{
537 hashtab_T *ht;
538 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100539 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200540 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541 int idx;
542
Bram Moolenaare3d46852020-08-29 13:39:17 +0200543 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200545 if (sid == current_sctx.sc_sid)
546 {
Bram Moolenaardce24412022-02-08 20:35:30 +0000547 sallvar_T *sav = find_script_var(name, 0, cctx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200548
549 if (sav == NULL)
550 return -2;
551 idx = sav->sav_var_vals_idx;
552 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +0100553 if (check_item_writable(sv, check_writable, name) == FAIL)
554 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200555 return idx;
556 }
557
558 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 ht = &SCRIPT_VARS(sid);
560 di = find_var_in_ht(ht, 0, name, TRUE);
561 if (di == NULL)
Bram Moolenaar577bd852022-01-10 18:42:52 +0000562 {
563 if (si->sn_autoload_prefix != NULL)
564 {
565 hashitem_T *hi;
566
567 // A variable exported from an autoload script is in the global
568 // variables, we can find it in the all_vars table.
569 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
570 if (!HASHITEM_EMPTY(hi))
571 return HI2SAV(hi)->sav_var_vals_idx;
572 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573 return -2;
Bram Moolenaar577bd852022-01-10 18:42:52 +0000574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575
576 // Now find the svar_T index in sn_var_vals.
577 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
578 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200579 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 if (sv->sv_tv == &di->di_tv)
581 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100582 if (check_item_writable(sv, check_writable, name) == FAIL)
583 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 return idx;
585 }
586 }
587 return -1;
588}
589
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000590 static imported_T *
591find_imported_in_script(char_u *name, size_t len, int sid)
592{
593 scriptitem_T *si;
594 int idx;
595
596 if (!SCRIPT_ID_VALID(sid))
597 return NULL;
598 si = SCRIPT_ITEM(sid);
599 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
600 {
601 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
602
603 if (len == 0 ? STRCMP(name, import->imp_name) == 0
604 : STRLEN(import->imp_name) == len
605 && STRNCMP(name, import->imp_name, len) == 0)
606 return import;
607 }
608 return NULL;
609}
610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200612 * Find "name" in imported items of the current script or in "cctx" if not
613 * NULL.
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000614 * If "load" is TRUE and the script was not loaded yet, load it now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 */
616 imported_T *
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000617find_imported(char_u *name, size_t len, int load, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619 int idx;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000620 imported_T *ret = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621
Bram Moolenaare3d46852020-08-29 13:39:17 +0200622 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +0200623 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 if (cctx != NULL)
625 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
626 {
627 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
628 + idx;
629
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100630 if (len == 0 ? STRCMP(name, import->imp_name) == 0
631 : STRLEN(import->imp_name) == len
632 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000633 {
634 ret = import;
635 break;
636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 }
638
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000639 if (ret == NULL)
640 ret = find_imported_in_script(name, len, current_sctx.sc_sid);
641
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000642 if (ret != NULL && load && (ret->imp_flags & IMP_FLAGS_AUTOLOAD))
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000643 {
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000644 scid_T dummy;
645
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000646 // script found before but not loaded yet
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000647 ret->imp_flags &= ~IMP_FLAGS_AUTOLOAD;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000648 (void)do_source(SCRIPT_ITEM(ret->imp_sid)->sn_name, FALSE,
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000649 DOSO_NONE, &dummy);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000650 }
651 return ret;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200652}
653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654/*
Bram Moolenaar20431c92020-03-20 18:39:46 +0100655 * Free all imported variables.
656 */
657 static void
658free_imported(cctx_T *cctx)
659{
660 int idx;
661
662 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
663 {
664 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
665
666 vim_free(import->imp_name);
667 }
668 ga_clear(&cctx->ctx_imports);
669}
670
671/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000672 * Called when checking for a following operator at "arg". When the rest of
673 * the line is empty or only a comment, peek the next line. If there is a next
674 * line return a pointer to it and set "nextp".
675 * Otherwise skip over white space.
676 */
677 char_u *
678may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
679{
680 char_u *p = skipwhite(arg);
681
682 *nextp = NULL;
683 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
684 {
685 *nextp = peek_next_line_from_context(cctx);
686 if (*nextp != NULL)
687 return *nextp;
688 }
689 return p;
690}
691
692/*
Bram Moolenaar23c55272020-06-21 16:58:13 +0200693 * Return a pointer to the next line that isn't empty or only contains a
694 * comment. Skips over white space.
695 * Returns NULL if there is none.
696 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200697 char_u *
698peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +0200699{
700 int lnum = cctx->ctx_lnum;
701
702 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
703 {
704 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +0200705 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200706
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200707 // ignore NULLs inserted for continuation lines
708 if (line != NULL)
709 {
710 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100711 if (vim9_bad_comment(p))
712 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200713 if (*p != NUL && !vim9_comment_start(p))
714 return p;
715 }
Bram Moolenaar23c55272020-06-21 16:58:13 +0200716 }
717 return NULL;
718}
719
720/*
Bram Moolenaare6085c52020-04-12 20:19:16 +0200721 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +0200722 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +0200723 * Returns NULL when at the end.
724 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200725 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +0200726next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +0200727{
Bram Moolenaar7a092242020-04-16 22:10:49 +0200728 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200729
730 do
731 {
732 ++cctx->ctx_lnum;
733 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200734 {
735 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200736 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +0200737 }
Bram Moolenaare6085c52020-04-12 20:19:16 +0200738 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +0200739 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200740 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200741 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +0200742 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +0200743 return line;
744}
745
746/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100747 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200748 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +0200749 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200750 * Return FAIL if beyond the last line, "*arg" is unmodified then.
751 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000752 int
Bram Moolenaar2c330432020-04-13 14:41:35 +0200753may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200754{
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100755 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100756 if (vim9_bad_comment(*arg))
757 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200758 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200759 {
Bram Moolenaar23c55272020-06-21 16:58:13 +0200760 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200761
762 if (next == NULL)
763 return FAIL;
764 *arg = skipwhite(next);
765 }
766 return OK;
767}
768
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200769/*
770 * Idem, and give an error when failed.
771 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 int
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200773may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
774{
775 if (may_get_next_line(whitep, arg, cctx) == FAIL)
776 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +0100777 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200778 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200779 return FAIL;
780 }
781 return OK;
782}
783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200785 * Get a line from the compilation context, compatible with exarg_T getline().
786 * Return a pointer to the line in allocated memory.
787 * Return NULL for end-of-file or some error.
788 */
789 static char_u *
790exarg_getline(
791 int c UNUSED,
792 void *cookie,
793 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200794 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200795{
796 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +0200797 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200798
Bram Moolenaar66250c92020-08-20 15:02:42 +0200799 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200800 {
Bram Moolenaar2914a202020-09-27 18:24:03 +0200801 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +0200802 return NULL;
803 ++cctx->ctx_lnum;
804 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
805 // Comment lines result in NULL pointers, skip them.
806 if (p != NULL)
807 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +0200808 }
Bram Moolenaar04b12692020-05-04 23:24:44 +0200809}
810
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100811 void
812fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
813{
814 eap->getline = exarg_getline;
815 eap->cookie = cctx;
816}
817
Bram Moolenaar04b12692020-05-04 23:24:44 +0200818/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000819 * Return TRUE if "ufunc" should be compiled, taking into account whether
820 * "profile" indicates profiling is to be done.
821 */
822 int
823func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
824{
825 switch (ufunc->uf_def_status)
826 {
827 case UF_TO_BE_COMPILED:
828 return TRUE;
829
830 case UF_COMPILED:
831 {
832 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
833 + ufunc->uf_dfunc_idx;
834
835 switch (compile_type)
836 {
837 case CT_PROFILE:
838#ifdef FEAT_PROFILE
839 return dfunc->df_instr_prof == NULL;
840#endif
841 case CT_NONE:
842 return dfunc->df_instr == NULL;
843 case CT_DEBUG:
844 return dfunc->df_instr_debug == NULL;
845 }
846 }
847
848 case UF_NOT_COMPILED:
849 case UF_COMPILE_ERROR:
850 case UF_COMPILING:
851 break;
852 }
853 return FALSE;
854}
855
856/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200857 * Compile a nested :def command.
858 */
859 static char_u *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000860compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200861{
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200862 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +0200863 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200864 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000865 int off;
866 char_u *func_name;
Bram Moolenaareef21022020-08-01 22:16:43 +0200867 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200868 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100869 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +0200870 compiletype_T compile_type;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200871
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200872 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +0200873 {
874 emsg(_(e_cannot_use_bang_with_nested_def));
875 return NULL;
876 }
877
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100878 if (*name_start == '/')
879 {
880 name_end = skip_regexp(name_start + 1, '/', TRUE);
881 if (*name_end == '/')
882 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +0200883 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +0100884 }
885 if (name_end == name_start || *skipwhite(name_end) != '(')
886 {
887 if (!ends_excmd2(name_start, name_end))
888 {
889 semsg(_(e_invalid_command_str), eap->cmd);
890 return NULL;
891 }
892
893 // "def" or "def Name": list functions
894 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
895 return NULL;
896 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
897 }
898
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200899 // Only g:Func() can use a namespace.
900 if (name_start[1] == ':' && !is_global)
901 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200902 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +0200903 return NULL;
904 }
Bram Moolenaardce24412022-02-08 20:35:30 +0000905 if (check_defined(name_start, name_end - name_start, cctx,
906 NULL, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +0200907 return NULL;
Bram Moolenaarf681cfb2022-02-07 20:30:57 +0000908 if (!ASCII_ISUPPER(is_global ? name_start[2] : name_start[0]))
909 {
Bram Moolenaar3787f262022-02-07 21:54:01 +0000910 semsg(_(e_function_name_must_start_with_capital_str), name_start);
Bram Moolenaarf681cfb2022-02-07 20:30:57 +0000911 return NULL;
912 }
Bram Moolenaareef21022020-08-01 22:16:43 +0200913
Bram Moolenaar04b12692020-05-04 23:24:44 +0200914 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100915 fill_exarg_from_cctx(eap, cctx);
916
Bram Moolenaar04b12692020-05-04 23:24:44 +0200917 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000918 // We use the special <Lamba>99 name, but it's not really a lambda.
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100919 lambda_name = vim_strsave(get_lambda_name());
920 if (lambda_name == NULL)
921 return NULL;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000922
923 // This may free the current line, make a copy of the name.
924 off = is_global ? 2 : 0;
925 func_name = vim_strnsave(name_start + off, name_end - name_start - off);
926 if (func_name == NULL)
927 {
928 r = FAIL;
929 goto theend;
930 }
931
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000932 ufunc = define_function(eap, lambda_name, lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +0200933 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100934 {
935 r = eap->skip ? OK : FAIL;
936 goto theend;
937 }
Bram Moolenaar7473a842021-12-28 17:55:26 +0000938 if (eap->nextcmd != NULL)
939 {
940 semsg(_(e_text_found_after_str_str),
941 eap->cmdidx == CMD_def ? "enddef" : "endfunction", eap->nextcmd);
942 r = FAIL;
Bram Moolenaard2939812021-12-30 17:09:05 +0000943 func_ptr_unref(ufunc);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000944 goto theend;
945 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +0100946
947 // copy over the block scope IDs before compiling
948 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
949 {
950 int block_depth = cctx->ctx_ufunc->uf_block_depth;
951
952 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
953 if (ufunc->uf_block_ids != NULL)
954 {
955 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
956 sizeof(int) * block_depth);
957 ufunc->uf_block_depth = block_depth;
958 }
959 }
960
Bram Moolenaarffcfddc2021-07-11 20:22:30 +0200961 compile_type = COMPILE_TYPE(ufunc);
962#ifdef FEAT_PROFILE
963 // If the outer function is profiled, also compile the nested function for
964 // profiling.
965 if (cctx->ctx_compile_type == CT_PROFILE)
966 compile_type = CT_PROFILE;
967#endif
968 if (func_needs_compiling(ufunc, compile_type)
969 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +0200970 {
971 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100972 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +0200973 }
Bram Moolenaar04b12692020-05-04 23:24:44 +0200974
Bram Moolenaar648594e2021-07-11 17:55:01 +0200975#ifdef FEAT_PROFILE
976 // When the outer function is compiled for profiling, the nested function
977 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +0200978 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200979 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
980#endif
981
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200982 if (is_global)
983 {
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000984 r = generate_NEWFUNC(cctx, lambda_name, func_name);
985 func_name = NULL;
986 lambda_name = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200987 }
988 else
989 {
990 // Define a local variable for the function reference.
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000991 lvar_T *lvar = reserve_local(cctx, func_name, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200992 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +0200993
Bram Moolenaareef21022020-08-01 22:16:43 +0200994 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100995 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +0200996 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +0100997 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200998 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
999 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001000
1001theend:
1002 vim_free(lambda_name);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001003 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001004 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02001005}
1006
1007/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 * Return the length of an assignment operator, or zero if there isn't one.
1009 */
1010 int
1011assignment_len(char_u *p, int *heredoc)
1012{
1013 if (*p == '=')
1014 {
1015 if (p[1] == '<' && p[2] == '<')
1016 {
1017 *heredoc = TRUE;
1018 return 3;
1019 }
1020 return 1;
1021 }
1022 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
1023 return 2;
1024 if (STRNCMP(p, "..=", 3) == 0)
1025 return 3;
1026 return 0;
1027}
1028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001030 * Generate the load instruction for "name".
1031 */
1032 static void
1033generate_loadvar(
1034 cctx_T *cctx,
1035 assign_dest_T dest,
1036 char_u *name,
1037 lvar_T *lvar,
1038 type_T *type)
1039{
1040 switch (dest)
1041 {
1042 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001043 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001044 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1045 break;
1046 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01001047 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
1048 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
1049 else
1050 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001051 break;
1052 case dest_buffer:
1053 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
1054 break;
1055 case dest_window:
1056 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
1057 break;
1058 case dest_tab:
1059 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
1060 break;
1061 case dest_script:
1062 compile_load_scriptvar(cctx,
1063 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
1064 break;
1065 case dest_env:
1066 // Include $ in the name here
1067 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
1068 break;
1069 case dest_reg:
1070 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
1071 break;
1072 case dest_vimvar:
1073 generate_LOADV(cctx, name + 2, TRUE);
1074 break;
1075 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01001076 if (lvar->lv_from_outer > 0)
1077 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
1078 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001079 else
1080 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
1081 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001082 case dest_expr:
1083 // list or dict value should already be on the stack.
1084 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001085 }
1086}
1087
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001088/*
1089 * Skip over "[expr]" or ".member".
1090 * Does not check for any errors.
1091 */
1092 static char_u *
1093skip_index(char_u *start)
1094{
1095 char_u *p = start;
1096
1097 if (*p == '[')
1098 {
1099 p = skipwhite(p + 1);
1100 (void)skip_expr(&p, NULL);
1101 p = skipwhite(p);
1102 if (*p == ']')
1103 return p + 1;
1104 return p;
1105 }
1106 // if (*p == '.')
1107 return to_name_end(p + 1, TRUE);
1108}
1109
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001110 void
1111vim9_declare_error(char_u *name)
1112{
1113 char *scope = "";
1114
1115 switch (*name)
1116 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001117 case 'g': scope = _("global"); break;
1118 case 'b': scope = _("buffer"); break;
1119 case 'w': scope = _("window"); break;
1120 case 't': scope = _("tab"); break;
1121 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001122 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001123 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001124 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001125 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001126 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001127 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001128 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001129 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001130 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001131}
1132
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001133/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001134 * For one assignment figure out the type of destination. Return it in "dest".
1135 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001136 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001137 * For a v:var "vimvaridx" is set.
1138 * "type" is set to the destination type if known, unchanted otherwise.
1139 * Return FAIL if an error message was given.
1140 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001141 int
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001142get_var_dest(
1143 char_u *name,
1144 assign_dest_T *dest,
1145 int cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001146 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001147 int *vimvaridx,
1148 type_T **type,
1149 cctx_T *cctx)
1150{
1151 char_u *p;
1152
1153 if (*name == '&')
1154 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001155 int cc;
1156 long numval;
1157 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001158 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001159
1160 *dest = dest_option;
1161 if (cmdidx == CMD_final || cmdidx == CMD_const)
1162 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001163 emsg(_(e_cannot_lock_option));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001164 return FAIL;
1165 }
1166 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001167 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001168 if (p == NULL)
1169 {
1170 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02001171 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001172 return FAIL;
1173 }
1174 cc = *p;
1175 *p = NUL;
1176 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001177 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001178 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001179 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001180 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001181 case gov_unknown:
Bram Moolenaare1242042021-12-16 20:56:57 +00001182 semsg(_(e_unknown_option_str), name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001183 return FAIL;
1184 case gov_string:
1185 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001186 if (opt_p_flags & P_FUNC)
1187 {
1188 // might be a Funcref, check the type later
1189 *type = &t_any;
1190 *dest = dest_func_option;
1191 }
1192 else
1193 {
1194 *type = &t_string;
1195 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001196 break;
1197 case gov_bool:
1198 case gov_hidden_bool:
1199 *type = &t_bool;
1200 break;
1201 case gov_number:
1202 case gov_hidden_number:
1203 *type = &t_number;
1204 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001205 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001206 }
1207 else if (*name == '$')
1208 {
1209 *dest = dest_env;
1210 *type = &t_string;
1211 }
1212 else if (*name == '@')
1213 {
Bram Moolenaar13e45d12021-06-26 13:28:35 +02001214 if (name[1] != '@'
1215 && (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001216 {
1217 emsg_invreg(name[1]);
1218 return FAIL;
1219 }
1220 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02001221 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001222 }
1223 else if (STRNCMP(name, "g:", 2) == 0)
1224 {
1225 *dest = dest_global;
1226 }
1227 else if (STRNCMP(name, "b:", 2) == 0)
1228 {
1229 *dest = dest_buffer;
1230 }
1231 else if (STRNCMP(name, "w:", 2) == 0)
1232 {
1233 *dest = dest_window;
1234 }
1235 else if (STRNCMP(name, "t:", 2) == 0)
1236 {
1237 *dest = dest_tab;
1238 }
1239 else if (STRNCMP(name, "v:", 2) == 0)
1240 {
1241 typval_T *vtv;
1242 int di_flags;
1243
1244 *vimvaridx = find_vim_var(name + 2, &di_flags);
1245 if (*vimvaridx < 0)
1246 {
1247 semsg(_(e_variable_not_found_str), name);
1248 return FAIL;
1249 }
1250 // We use the current value of "sandbox" here, is that OK?
1251 if (var_check_ro(di_flags, name, FALSE))
1252 return FAIL;
1253 *dest = dest_vimvar;
1254 vtv = get_vim_var_tv(*vimvaridx);
1255 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
1256 }
1257 return OK;
1258}
1259
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001260 static int
Bram Moolenaar752fc692021-01-04 21:57:11 +01001261is_decl_command(int cmdidx)
1262{
1263 return cmdidx == CMD_let || cmdidx == CMD_var
1264 || cmdidx == CMD_final || cmdidx == CMD_const;
1265}
1266
1267/*
1268 * Figure out the LHS type and other properties for an assignment or one item
1269 * of ":unlet" with an index.
1270 * Returns OK or FAIL.
1271 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001272 int
Bram Moolenaar752fc692021-01-04 21:57:11 +01001273compile_lhs(
1274 char_u *var_start,
1275 lhs_T *lhs,
1276 int cmdidx,
1277 int heredoc,
1278 int oplen,
1279 cctx_T *cctx)
1280{
1281 char_u *var_end;
1282 int is_decl = is_decl_command(cmdidx);
1283
1284 CLEAR_POINTER(lhs);
1285 lhs->lhs_dest = dest_local;
1286 lhs->lhs_vimvaridx = -1;
1287 lhs->lhs_scriptvar_idx = -1;
1288
1289 // "dest_end" is the end of the destination, including "[expr]" or
1290 // ".name".
1291 // "var_end" is the end of the variable/option/etc. name.
1292 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
1293 if (*var_start == '@')
1294 var_end = var_start + 2;
1295 else
1296 {
1297 // skip over the leading "&", "&l:", "&g:" and "$"
1298 var_end = skip_option_env_lead(var_start);
1299 var_end = to_name_end(var_end, TRUE);
1300 }
1301
1302 // "a: type" is declaring variable "a" with a type, not dict "a:".
1303 if (is_decl && lhs->lhs_dest_end == var_start + 2
1304 && lhs->lhs_dest_end[-1] == ':')
1305 --lhs->lhs_dest_end;
1306 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
1307 --var_end;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001308 lhs->lhs_end = lhs->lhs_dest_end;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001309
1310 // compute the length of the destination without "[expr]" or ".name"
1311 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02001312 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001313 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
1314 if (lhs->lhs_name == NULL)
1315 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01001316
1317 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
1318 // Something follows after the variable: "var[idx]" or "var.key".
1319 lhs->lhs_has_index = TRUE;
1320
Bram Moolenaar752fc692021-01-04 21:57:11 +01001321 if (heredoc)
1322 lhs->lhs_type = &t_list_string;
1323 else
1324 lhs->lhs_type = &t_any;
1325
1326 if (cctx->ctx_skip != SKIP_YES)
1327 {
1328 int declare_error = FALSE;
1329
1330 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
1331 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
1332 &lhs->lhs_type, cctx) == FAIL)
1333 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02001334 if (lhs->lhs_dest != dest_local
1335 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001336 {
1337 // Specific kind of variable recognized.
1338 declare_error = is_decl;
1339 }
1340 else
1341 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01001342 // No specific kind of variable recognized, just a name.
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001343 if (check_reserved_name(lhs->lhs_name) == FAIL)
1344 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001345
1346 if (lookup_local(var_start, lhs->lhs_varlen,
1347 &lhs->lhs_local_lvar, cctx) == OK)
1348 lhs->lhs_lvar = &lhs->lhs_local_lvar;
1349 else
1350 {
1351 CLEAR_FIELD(lhs->lhs_arg_lvar);
1352 if (arg_exists(var_start, lhs->lhs_varlen,
1353 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
1354 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
1355 {
1356 if (is_decl)
1357 {
1358 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
1359 return FAIL;
1360 }
1361 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
1362 }
1363 }
1364 if (lhs->lhs_lvar != NULL)
1365 {
1366 if (is_decl)
1367 {
1368 semsg(_(e_variable_already_declared), lhs->lhs_name);
1369 return FAIL;
1370 }
1371 }
1372 else
1373 {
1374 int script_namespace = lhs->lhs_varlen > 1
1375 && STRNCMP(var_start, "s:", 2) == 0;
1376 int script_var = (script_namespace
1377 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaardce24412022-02-08 20:35:30 +00001378 cctx, NULL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001379 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaardce24412022-02-08 20:35:30 +00001380 cctx, NULL)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001381 imported_T *import =
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001382 find_imported(var_start, lhs->lhs_varlen, FALSE, cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001383
1384 if (script_namespace || script_var || import != NULL)
1385 {
1386 char_u *rawname = lhs->lhs_name
1387 + (lhs->lhs_name[1] == ':' ? 2 : 0);
1388
1389 if (is_decl)
1390 {
1391 if (script_namespace)
1392 semsg(_(e_cannot_declare_script_variable_in_function),
1393 lhs->lhs_name);
1394 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001395 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01001396 lhs->lhs_name);
1397 return FAIL;
1398 }
1399 else if (cctx->ctx_ufunc->uf_script_ctx_version
1400 == SCRIPT_VERSION_VIM9
1401 && script_namespace
1402 && !script_var && import == NULL)
1403 {
1404 semsg(_(e_unknown_variable_str), lhs->lhs_name);
1405 return FAIL;
1406 }
1407
1408 lhs->lhs_dest = dest_script;
1409
1410 // existing script-local variables should have a type
1411 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
1412 if (import != NULL)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001413 {
1414 char_u *dot = vim_strchr(var_start, '.');
1415 char_u *p;
1416
1417 // for an import the name is what comes after the dot
1418 if (dot == NULL)
1419 {
1420 semsg(_(e_no_dot_after_imported_name_str),
1421 var_start);
1422 return FAIL;
1423 }
1424 p = skipwhite(dot + 1);
1425 var_end = to_name_end(p, TRUE);
1426 if (var_end == p)
1427 {
1428 semsg(_(e_missing_name_after_imported_name_str),
1429 var_start);
1430 return FAIL;
1431 }
1432 vim_free(lhs->lhs_name);
1433 lhs->lhs_varlen = var_end - p;
1434 lhs->lhs_name = vim_strnsave(p, lhs->lhs_varlen);
1435 if (lhs->lhs_name == NULL)
1436 return FAIL;
1437 rawname = lhs->lhs_name;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001438 lhs->lhs_scriptvar_sid = import->imp_sid;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001439 // TODO: where do we check this name is exported?
1440
1441 // Check if something follows: "exp.var[idx]" or
1442 // "exp.var.key".
1443 lhs->lhs_has_index = lhs->lhs_dest_end
1444 > skipwhite(var_end);
1445 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001446 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
1447 {
Bram Moolenaar08251752021-01-11 21:20:18 +01001448 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01001449 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01001450 lhs->lhs_scriptvar_sid, rawname,
1451 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
1452 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001453 if (lhs->lhs_scriptvar_idx >= 0)
1454 {
1455 scriptitem_T *si = SCRIPT_ITEM(
1456 lhs->lhs_scriptvar_sid);
1457 svar_T *sv =
1458 ((svar_T *)si->sn_var_vals.ga_data)
1459 + lhs->lhs_scriptvar_idx;
1460 lhs->lhs_type = sv->sv_type;
1461 }
1462 }
1463 }
Bram Moolenaardce24412022-02-08 20:35:30 +00001464 else if (check_defined(var_start, lhs->lhs_varlen, cctx,
1465 NULL, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001466 return FAIL;
1467 }
1468 }
1469
1470 if (declare_error)
1471 {
1472 vim9_declare_error(lhs->lhs_name);
1473 return FAIL;
1474 }
1475 }
1476
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001477 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01001478 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
1479 var_end = lhs->lhs_dest_end;
1480
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001481 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001482 {
1483 if (is_decl && *var_end == ':')
1484 {
1485 char_u *p;
1486
1487 // parse optional type: "let var: type = expr"
1488 if (!VIM_ISWHITE(var_end[1]))
1489 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001490 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001491 return FAIL;
1492 }
1493 p = skipwhite(var_end + 1);
1494 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
1495 if (lhs->lhs_type == NULL)
1496 return FAIL;
1497 lhs->lhs_has_type = TRUE;
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001498 lhs->lhs_end = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001499 }
1500 else if (lhs->lhs_lvar != NULL)
1501 lhs->lhs_type = lhs->lhs_lvar->lv_type;
1502 }
1503
Bram Moolenaare42939a2021-04-05 17:11:17 +02001504 if (oplen == 3 && !heredoc
1505 && lhs->lhs_dest != dest_global
1506 && !lhs->lhs_has_index
1507 && lhs->lhs_type->tt_type != VAR_STRING
1508 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001509 {
1510 emsg(_(e_can_only_concatenate_to_string));
1511 return FAIL;
1512 }
1513
1514 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
1515 && cctx->ctx_skip != SKIP_YES)
1516 {
1517 if (oplen > 1 && !heredoc)
1518 {
1519 // +=, /=, etc. require an existing variable
1520 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
1521 return FAIL;
1522 }
1523 if (!is_decl)
1524 {
1525 semsg(_(e_unknown_variable_str), lhs->lhs_name);
1526 return FAIL;
1527 }
1528
Bram Moolenaar3f327882021-03-17 20:56:38 +01001529 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01001530 if ((lhs->lhs_type->tt_type == VAR_FUNC
1531 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01001532 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01001533 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01001534
1535 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01001536 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
1537 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
1538 if (lhs->lhs_lvar == NULL)
1539 return FAIL;
1540 lhs->lhs_new_local = TRUE;
1541 }
1542
1543 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01001544 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001545 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00001546 char_u *after = var_start + lhs->lhs_varlen;
1547 char_u *p;
1548
Bram Moolenaar752fc692021-01-04 21:57:11 +01001549 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01001550 if (is_decl)
1551 {
1552 emsg(_(e_cannot_use_index_when_declaring_variable));
1553 return FAIL;
1554 }
1555
Bram Moolenaarc750d912021-11-29 22:02:12 +00001556 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
1557 // Only the last index is used below, if there are others
1558 // before it generate code for the expression. Thus for
1559 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
1560 for (;;)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001561 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00001562 p = skip_index(after);
1563 if (*p != '[' && *p != '.')
Bram Moolenaar752fc692021-01-04 21:57:11 +01001564 {
Bram Moolenaarc750d912021-11-29 22:02:12 +00001565 lhs->lhs_varlen_total = p - var_start;
1566 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001567 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00001568 after = p;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001569 }
Bram Moolenaarc750d912021-11-29 22:02:12 +00001570 if (after > var_start + lhs->lhs_varlen)
1571 {
1572 lhs->lhs_varlen = after - var_start;
1573 lhs->lhs_dest = dest_expr;
1574 // We don't know the type before evaluating the expression,
1575 // use "any" until then.
1576 lhs->lhs_type = &t_any;
1577 }
1578
1579 if (lhs->lhs_type->tt_member == NULL)
1580 lhs->lhs_member_type = &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001581 else
Bram Moolenaarc750d912021-11-29 22:02:12 +00001582 lhs->lhs_member_type = lhs->lhs_type->tt_member;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001583 }
1584 return OK;
1585}
1586
1587/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001588 * Figure out the LHS and check a few errors.
1589 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001590 int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001591compile_assign_lhs(
1592 char_u *var_start,
1593 lhs_T *lhs,
1594 int cmdidx,
1595 int is_decl,
1596 int heredoc,
1597 int oplen,
1598 cctx_T *cctx)
1599{
1600 if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
1601 return FAIL;
1602
1603 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
1604 {
1605 semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
1606 return FAIL;
1607 }
1608 if (!is_decl && lhs->lhs_lvar != NULL
1609 && lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
1610 {
1611 semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
1612 return FAIL;
1613 }
1614 return OK;
1615}
1616
1617/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001618 * Return TRUE if "lhs" has a range index: "[expr : expr]".
1619 */
1620 static int
1621has_list_index(char_u *idx_start, cctx_T *cctx)
1622{
1623 char_u *p = idx_start;
1624 int save_skip;
1625
1626 if (*p != '[')
1627 return FALSE;
1628
1629 p = skipwhite(p + 1);
1630 if (*p == ':')
1631 return TRUE;
1632
1633 save_skip = cctx->ctx_skip;
1634 cctx->ctx_skip = SKIP_YES;
1635 (void)compile_expr0(&p, cctx);
1636 cctx->ctx_skip = save_skip;
1637 return *skipwhite(p) == ':';
1638}
1639
1640/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02001641 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
1642 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01001643 */
1644 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02001645compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01001646 char_u *var_start,
1647 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02001648 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01001649 cctx_T *cctx)
1650{
Bram Moolenaar752fc692021-01-04 21:57:11 +01001651 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02001652 char_u *p;
1653 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02001654 int need_white_before = TRUE;
1655 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001656
Bram Moolenaar752fc692021-01-04 21:57:11 +01001657 p = var_start + varlen;
1658 if (*p == '[')
1659 {
1660 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02001661 if (*p == ':')
1662 {
1663 // empty first index, push zero
1664 r = generate_PUSHNR(cctx, 0);
1665 need_white_before = FALSE;
1666 }
1667 else
1668 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001669
1670 if (r == OK && *skipwhite(p) == ':')
1671 {
1672 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02001673 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02001674 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001675 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02001676 empty_second = *skipwhite(p + 1) == ']';
1677 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
1678 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001679 {
1680 semsg(_(e_white_space_required_before_and_after_str_at_str),
1681 ":", p);
1682 return FAIL;
1683 }
1684 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02001685 if (*p == ']')
1686 // empty second index, push "none"
1687 r = generate_PUSHSPEC(cctx, VVAL_NONE);
1688 else
1689 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001690 }
1691
Bram Moolenaar752fc692021-01-04 21:57:11 +01001692 if (r == OK && *skipwhite(p) != ']')
1693 {
1694 // this should not happen
Bram Moolenaare1242042021-12-16 20:56:57 +00001695 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar752fc692021-01-04 21:57:11 +01001696 r = FAIL;
1697 }
1698 }
1699 else // if (*p == '.')
1700 {
1701 char_u *key_end = to_name_end(p + 1, TRUE);
1702 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
1703
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001704 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001705 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02001706 return r;
1707}
1708
1709/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001710 * For a LHS with an index, load the variable to be indexed.
1711 */
1712 static int
1713compile_load_lhs(
1714 lhs_T *lhs,
1715 char_u *var_start,
1716 type_T *rhs_type,
1717 cctx_T *cctx)
1718{
1719 if (lhs->lhs_dest == dest_expr)
1720 {
1721 size_t varlen = lhs->lhs_varlen;
1722 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02001723 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001724 char_u *p = var_start;
Bram Moolenaare97976b2021-08-01 13:17:17 +02001725 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001726
Bram Moolenaare97976b2021-08-01 13:17:17 +02001727 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
1728 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001729 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02001730 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
1731 res = compile_expr0(&p, cctx);
1732 var_start[varlen] = c;
1733 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
1734 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001735 {
1736 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02001737 if (res != FAIL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001738 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001739 return FAIL;
1740 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001741
Bram Moolenaar078a4612022-01-04 15:17:03 +00001742 lhs->lhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
1743 : get_type_on_stack(cctx, 0);
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001744 // now we can properly check the type
1745 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
1746 && rhs_type != &t_void
1747 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
1748 FALSE, FALSE) == FAIL)
1749 return FAIL;
1750 }
1751 else
1752 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
1753 lhs->lhs_lvar, lhs->lhs_type);
1754 return OK;
1755}
1756
1757/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02001758 * Produce code for loading "lhs" and also take care of an index.
1759 * Return OK/FAIL.
1760 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001761 int
Bram Moolenaara369c3d2021-04-21 16:00:10 +02001762compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
1763{
1764 compile_load_lhs(lhs, var_start, NULL, cctx);
1765
1766 if (lhs->lhs_has_index)
1767 {
1768 int range = FALSE;
1769
1770 // Get member from list or dict. First compile the
1771 // index value.
1772 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
1773 return FAIL;
1774 if (range)
1775 {
1776 semsg(_(e_cannot_use_range_with_assignment_operator_str),
1777 var_start);
1778 return FAIL;
1779 }
1780
1781 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001782 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02001783 return FAIL;
1784 }
1785 return OK;
1786}
1787
1788/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02001789 * Assignment to a list or dict member, or ":unlet" for the item, using the
1790 * information in "lhs".
1791 * Returns OK or FAIL.
1792 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001793 int
Bram Moolenaare42939a2021-04-05 17:11:17 +02001794compile_assign_unlet(
1795 char_u *var_start,
1796 lhs_T *lhs,
1797 int is_assign,
1798 type_T *rhs_type,
1799 cctx_T *cctx)
1800{
Bram Moolenaare42939a2021-04-05 17:11:17 +02001801 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02001802 int range = FALSE;
1803
Bram Moolenaar68452172021-04-12 21:21:02 +02001804 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001805 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001806 if (is_assign && range
1807 && lhs->lhs_type->tt_type != VAR_LIST
1808 && lhs->lhs_type != &t_blob
1809 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02001810 {
1811 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
1812 return FAIL;
1813 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001814
1815 if (lhs->lhs_type == &t_any)
1816 {
1817 // Index on variable of unknown type: check at runtime.
1818 dest_type = VAR_ANY;
1819 }
1820 else
1821 {
1822 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001823 if (dest_type == VAR_DICT && range)
1824 {
1825 emsg(e_cannot_use_range_with_dictionary);
1826 return FAIL;
1827 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001828 if (dest_type == VAR_DICT
1829 && may_generate_2STRING(-1, FALSE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001830 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02001831 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001832 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02001833 type_T *type;
1834
1835 if (range)
1836 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001837 type = get_type_on_stack(cctx, 1);
Bram Moolenaar51e93322021-04-17 20:44:56 +02001838 if (need_type(type, &t_number,
1839 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001840 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02001841 }
Bram Moolenaar078a4612022-01-04 15:17:03 +00001842 type = get_type_on_stack(cctx, 0);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02001843 if ((dest_type != VAR_BLOB && type != &t_special)
Bram Moolenaar51e93322021-04-17 20:44:56 +02001844 && need_type(type, &t_number,
1845 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001846 return FAIL;
1847 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001848 }
1849
1850 // Load the dict or list. On the stack we then have:
1851 // - value (for assignment, not for :unlet)
1852 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001853 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01001854 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001855 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
1856 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001857
Bram Moolenaar68452172021-04-12 21:21:02 +02001858 if (dest_type == VAR_LIST || dest_type == VAR_DICT
1859 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001860 {
1861 if (is_assign)
1862 {
Bram Moolenaar68452172021-04-12 21:21:02 +02001863 if (range)
1864 {
1865 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
1866 return FAIL;
1867 }
1868 else
1869 {
1870 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001871
Bram Moolenaar68452172021-04-12 21:21:02 +02001872 if (isn == NULL)
1873 return FAIL;
1874 isn->isn_arg.vartype = dest_type;
1875 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001876 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001877 else if (range)
1878 {
1879 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
1880 return FAIL;
1881 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001882 else
1883 {
1884 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
1885 return FAIL;
1886 }
1887 }
1888 else
1889 {
1890 emsg(_(e_indexable_type_required));
1891 return FAIL;
1892 }
1893
1894 return OK;
1895}
1896
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001897/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001898 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001899 * "let name"
1900 * "var name = expr"
1901 * "final name = expr"
1902 * "const name = expr"
1903 * "name = expr"
1904 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02001905 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001906 * Return NULL for an error.
1907 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 */
1909 static char_u *
1910compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
1911{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001912 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001914 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 char_u *ret = NULL;
1916 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001917 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 int semicolon = 0;
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02001919 int did_generate_slice = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 int oplen = 0;
1923 int heredoc = FALSE;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02001924 int incdec = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001925 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001927 int is_decl = is_decl_command(cmdidx);
1928 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02001929 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001931 // Skip over the "var" or "[var, var]" to get to any "=".
1932 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
1933 if (p == NULL)
1934 return *arg == '[' ? arg : NULL;
1935
Bram Moolenaar752fc692021-01-04 21:57:11 +01001936 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 sp = p;
1939 p = skipwhite(p);
1940 op = p;
1941 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001942
1943 if (var_count > 0 && oplen == 0)
1944 // can be something like "[1, 2]->func()"
1945 return arg;
1946
Bram Moolenaar004d9b02020-11-30 21:40:03 +01001947 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001949 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001950 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02001951 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02001952 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
1953 {
Bram Moolenaar22480d12021-06-25 21:31:09 +02001954 if (VIM_ISWHITE(eap->cmd[2]))
1955 {
1956 semsg(_(e_no_white_space_allowed_after_str_str),
1957 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
1958 return NULL;
1959 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02001960 op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
1961 oplen = 2;
1962 incdec = TRUE;
1963 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 if (heredoc)
1966 {
1967 list_T *l;
1968 listitem_T *li;
1969
1970 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02001971 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02001973 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02001974 if (l == NULL)
1975 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976
Bram Moolenaar078269b2020-09-21 20:35:55 +02001977 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02001979 // Push each line and the create the list.
1980 FOR_ALL_LIST_ITEMS(l, li)
1981 {
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02001982 generate_PUSHS(cctx, &li->li_tv.vval.v_string);
Bram Moolenaar078269b2020-09-21 20:35:55 +02001983 li->li_tv.vval.v_string = NULL;
1984 }
1985 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 list_free(l);
1988 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001989 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001991 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01001993 char_u *wp;
1994
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001995 // for "[var, var] = expr" evaluate the expression here, loop over the
1996 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01001997 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02001998
Bram Moolenaar004d9b02020-11-30 21:40:03 +01001999 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002000 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01002001 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002002 if (compile_expr0(&p, cctx) == FAIL)
2003 return NULL;
2004 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002006 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002008 type_T *stacktype;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00002009 int needed_list_len;
2010 int did_check = FALSE;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002011
Bram Moolenaar078a4612022-01-04 15:17:03 +00002012 stacktype = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2013 : get_type_on_stack(cctx, 0);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002014 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002016 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002017 goto theend;
2018 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01002019 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002020 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002021 goto theend;
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00002022 // If a constant list was used we can check the length right here.
2023 needed_list_len = semicolon ? var_count - 1 : var_count;
2024 if (instr->ga_len > 0)
2025 {
2026 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2027
2028 if (isn->isn_type == ISN_NEWLIST)
2029 {
2030 did_check = TRUE;
2031 if (semicolon ? isn->isn_arg.number < needed_list_len
2032 : isn->isn_arg.number != needed_list_len)
2033 {
2034 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaarf60a6342022-01-15 14:16:37 +00002035 needed_list_len, (int)isn->isn_arg.number);
Bram Moolenaardb9ff9a2021-12-01 17:38:01 +00002036 goto theend;
2037 }
2038 }
2039 }
2040 if (!did_check)
2041 generate_CHECKLEN(cctx, needed_list_len, semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002042 if (stacktype->tt_member != NULL)
2043 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002044 }
2045 }
2046
2047 /*
2048 * Loop over variables in "[var, var] = expr".
2049 * For "var = expr" and "let var: type" this is done only once.
2050 */
2051 if (var_count > 0)
2052 var_start = skipwhite(arg + 1); // skip over the "["
2053 else
2054 var_start = arg;
2055 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
2056 {
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002057 int instr_count = -1;
2058 int save_lnum;
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002059 int skip_store = FALSE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002060
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02002061 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
2062 {
2063 // Ignore underscore in "[a, _, b] = list".
2064 if (var_count > 0)
2065 {
2066 var_start = skipwhite(var_start + 2);
2067 continue;
2068 }
2069 emsg(_(e_cannot_use_underscore_here));
2070 goto theend;
2071 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002072 vim_free(lhs.lhs_name);
2073
2074 /*
2075 * Figure out the LHS type and other properties.
2076 */
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002077 if (compile_assign_lhs(var_start, &lhs, cmdidx,
2078 is_decl, heredoc, oplen, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002079 goto theend;
Bram Moolenaar81530e32021-07-28 21:25:49 +02002080 if (heredoc)
2081 {
2082 SOURCING_LNUM = start_lnum;
2083 if (lhs.lhs_has_type
2084 && need_type(&t_list_string, lhs.lhs_type,
2085 -1, 0, cctx, FALSE, FALSE) == FAIL)
2086 goto theend;
2087 }
2088 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002089 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002090 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002091 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002092 if (oplen > 0 && var_count == 0)
2093 {
2094 // skip over the "=" and the expression
2095 p = skipwhite(op + oplen);
Bram Moolenaar169502f2021-04-21 12:19:35 +02002096 (void)compile_expr0(&p, cctx);
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002097 }
2098 }
2099 else if (oplen > 0)
2100 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002101 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01002102 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002103
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02002104 // for "+=", "*=", "..=" etc. first load the current value
2105 if (*op != '='
2106 && compile_load_lhs_with_index(&lhs, var_start,
2107 cctx) == FAIL)
2108 goto theend;
2109
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002110 // For "var = expr" evaluate the expression.
2111 if (var_count == 0)
2112 {
2113 int r;
2114
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002115 // Compile the expression.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002116 instr_count = instr->ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002117 if (incdec)
Bram Moolenaar21e51222020-12-04 12:43:29 +01002118 {
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002119 r = generate_PUSHNR(cctx, 1);
2120 }
2121 else
2122 {
2123 // Temporarily hide the new local variable here, it is
2124 // not available to this expression.
2125 if (lhs.lhs_new_local)
2126 --cctx->ctx_locals.ga_len;
2127 wp = op + oplen;
2128 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
2129 {
2130 if (lhs.lhs_new_local)
2131 ++cctx->ctx_locals.ga_len;
2132 goto theend;
2133 }
2134 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002135 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01002136 ++cctx->ctx_locals.ga_len;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002137 if (r == FAIL)
2138 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01002139 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002140 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02002141 else if (semicolon && var_idx == var_count - 1)
2142 {
2143 // For "[var; var] = expr" get the rest of the list
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02002144 did_generate_slice = TRUE;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002145 if (generate_SLICE(cctx, var_count - 1) == FAIL)
2146 goto theend;
2147 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002148 else
2149 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002150 // For "[var, var] = expr" get the "var_idx" item from the
2151 // list.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02002152 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01002153 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002154 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002155
Bram Moolenaar078a4612022-01-04 15:17:03 +00002156 rhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2157 : get_type_on_stack(cctx, 0);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002158 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002159 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002160 if ((rhs_type->tt_type == VAR_FUNC
2161 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01002162 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01002163 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02002164 goto theend;
2165
Bram Moolenaar752fc692021-01-04 21:57:11 +01002166 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002167 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002168 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002169 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002170 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002171 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002172 }
2173 else
2174 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02002175 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002176 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002177 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002178 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002179 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002180 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01002181 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002182 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002183 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01002184 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002185 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002186 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002187 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002188 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002189 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02002190 where_T where = WHERE_INIT;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002191
Bram Moolenaar77709b12021-04-03 21:01:01 +02002192 // Without operator check type here, otherwise below.
2193 // Use the line number of the assignment.
2194 SOURCING_LNUM = start_lnum;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02002195 where.wt_index = var_count > 0 ? var_idx + 1 : 0;
2196 where.wt_variable = var_count > 0;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002197 // If assigning to a list or dict member, use the
2198 // member type. Not for "list[:] =".
2199 if (lhs.lhs_has_index
2200 && !has_list_index(var_start + lhs.lhs_varlen,
2201 cctx))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002202 use_type = lhs.lhs_member_type;
Bram Moolenaar4270d8b2021-08-07 16:30:42 +02002203 if (need_type_where(rhs_type, use_type, -1, where,
2204 cctx, FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002205 goto theend;
2206 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002207 }
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002208 else
2209 {
2210 type_T *lhs_type = lhs.lhs_member_type;
2211
2212 // Special case: assigning to @# can use a number or a
2213 // string.
Bram Moolenaaraf647e72021-08-05 19:01:17 +02002214 // Also: can assign a number to a float.
2215 if ((lhs_type == &t_number_or_string
2216 || lhs_type == &t_float)
2217 && rhs_type->tt_type == VAR_NUMBER)
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002218 lhs_type = &t_number;
2219 if (*p != '=' && need_type(rhs_type, lhs_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01002220 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002221 goto theend;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002222 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002224 else if (cmdidx == CMD_final)
2225 {
2226 emsg(_(e_final_requires_a_value));
2227 goto theend;
2228 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002229 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002231 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002232 goto theend;
2233 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002234 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option
2235 || lhs.lhs_dest == dest_func_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002236 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02002237 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002238 goto theend;
2239 }
2240 else
2241 {
2242 // variables are always initialized
Bram Moolenaar35578162021-08-02 19:10:38 +02002243 if (GA_GROW_FAILS(instr, 1))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002244 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002245 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002246 {
2247 case VAR_BOOL:
2248 generate_PUSHBOOL(cctx, VVAL_FALSE);
2249 break;
2250 case VAR_FLOAT:
2251#ifdef FEAT_FLOAT
2252 generate_PUSHF(cctx, 0.0);
2253#endif
2254 break;
2255 case VAR_STRING:
2256 generate_PUSHS(cctx, NULL);
2257 break;
2258 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002259 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002260 break;
2261 case VAR_FUNC:
2262 generate_PUSHFUNC(cctx, NULL, &t_func_void);
2263 break;
2264 case VAR_LIST:
2265 generate_NEWLIST(cctx, 0);
2266 break;
2267 case VAR_DICT:
2268 generate_NEWDICT(cctx, 0);
2269 break;
2270 case VAR_JOB:
2271 generate_PUSHJOB(cctx, NULL);
2272 break;
2273 case VAR_CHANNEL:
2274 generate_PUSHCHANNEL(cctx, NULL);
2275 break;
2276 case VAR_NUMBER:
2277 case VAR_UNKNOWN:
2278 case VAR_ANY:
2279 case VAR_PARTIAL:
2280 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002281 case VAR_INSTR:
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002282 case VAR_SPECIAL: // cannot happen
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002283 // This is skipped for local variables, they are always
2284 // initialized to zero. But in a "for" or "while" loop
2285 // the value may have been changed.
2286 if (lhs.lhs_dest == dest_local
2287 && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002288 skip_store = TRUE;
2289 else
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002290 {
2291 instr_count = instr->ga_len;
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002292 generate_PUSHNR(cctx, 0);
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002293 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002294 break;
2295 }
2296 }
2297 if (var_count == 0)
2298 end = p;
2299 }
2300
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002301 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002302 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02002303 break;
2304
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002305 if (oplen > 0 && *op != '=')
2306 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002307 type_T *expected;
Bram Moolenaarc3160722021-08-02 21:12:05 +02002308 type_T *stacktype = NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002309
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002310 if (*op == '.')
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002311 {
2312 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2313 goto theend;
2314 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002315 else
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002316 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002317 expected = lhs.lhs_member_type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00002318 stacktype = get_type_on_stack(cctx, 0);
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002319 if (
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002320#ifdef FEAT_FLOAT
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002321 // If variable is float operation with number is OK.
Bram Moolenaar7bf9a072021-08-02 21:55:15 +02002322 !(expected == &t_float && (stacktype == &t_number
2323 || stacktype == &t_number_bool)) &&
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002324#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01002325 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002326 FALSE, FALSE) == FAIL)
Bram Moolenaarf5d52c92021-07-31 22:51:10 +02002327 goto theend;
2328 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002329
2330 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02002331 {
2332 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
2333 goto theend;
2334 }
2335 else if (*op == '+')
2336 {
2337 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01002338 operator_type(lhs.lhs_member_type, stacktype),
Bram Moolenaar07802042021-09-09 23:01:14 +02002339 lhs.lhs_member_type, stacktype,
2340 EXPR_APPEND) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02002341 goto theend;
2342 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02002343 else if (generate_two_op(cctx, op) == FAIL)
2344 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002347 // Use the line number of the assignment for store instruction.
2348 save_lnum = cctx->ctx_lnum;
2349 cctx->ctx_lnum = start_lnum - 1;
2350
Bram Moolenaar752fc692021-01-04 21:57:11 +01002351 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002352 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002353 // Use the info in "lhs" to store the value at the index in the
2354 // list or dict.
2355 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
2356 == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002357 {
2358 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002359 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002360 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002361 }
2362 else
2363 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002364 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02002365 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01002366 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002367 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002368 generate_LOCKCONST(cctx);
2369
Bram Moolenaar10d6f182022-01-04 15:54:38 +00002370 if ((lhs.lhs_type->tt_type == VAR_DICT
Bram Moolenaar752fc692021-01-04 21:57:11 +01002371 || lhs.lhs_type->tt_type == VAR_LIST)
2372 && lhs.lhs_type->tt_member != NULL
Bram Moolenaar752fc692021-01-04 21:57:11 +01002373 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01002374 // Set the type in the list or dict, so that it can be checked,
Bram Moolenaar10d6f182022-01-04 15:54:38 +00002375 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01002376 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01002377
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002378 if (!skip_store && generate_store_lhs(cctx, &lhs,
2379 instr_count, is_decl) == FAIL)
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002380 {
2381 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002382 goto theend;
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002383 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002384 }
Bram Moolenaar6977dba2021-07-04 22:48:12 +02002385 cctx->ctx_lnum = save_lnum;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002386
2387 if (var_idx + 1 < var_count)
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00002388 var_start = skipwhite(lhs.lhs_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002390
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02002391 // For "[var, var] = expr" drop the "expr" value.
2392 // Also for "[var, var; _] = expr".
2393 if (var_count > 0 && (!semicolon || !did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02002394 {
Bram Moolenaarec792292020-12-13 21:26:56 +01002395 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02002396 goto theend;
2397 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002398
Bram Moolenaarb2097502020-07-19 17:17:02 +02002399 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400
2401theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01002402 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 return ret;
2404}
2405
2406/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01002407 * Check for an assignment at "eap->cmd", compile it if found.
2408 * Return NOTDONE if there is none, FAIL for failure, OK if done.
2409 */
2410 static int
2411may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
2412{
2413 char_u *pskip;
2414 char_u *p;
2415
2416 // Assuming the command starts with a variable or function name,
2417 // find what follows.
2418 // Skip over "var.member", "var[idx]" and the like.
2419 // Also "&opt = val", "$ENV = val" and "@r = val".
2420 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
2421 ? eap->cmd + 1 : eap->cmd;
2422 p = to_name_end(pskip, TRUE);
2423 if (p > eap->cmd && *p != NUL)
2424 {
2425 char_u *var_end;
2426 int oplen;
2427 int heredoc;
2428
2429 if (eap->cmd[0] == '@')
2430 var_end = eap->cmd + 2;
2431 else
2432 var_end = find_name_end(pskip, NULL, NULL,
2433 FNE_CHECK_START | FNE_INCL_BR);
2434 oplen = assignment_len(skipwhite(var_end), &heredoc);
2435 if (oplen > 0)
2436 {
2437 size_t len = p - eap->cmd;
2438
2439 // Recognize an assignment if we recognize the variable
2440 // name:
2441 // "g:var = expr"
2442 // "local = expr" where "local" is a local var.
2443 // "script = expr" where "script" is a script-local var.
2444 // "import = expr" where "import" is an imported var
2445 // "&opt = expr"
2446 // "$ENV = expr"
2447 // "@r = expr"
2448 if (*eap->cmd == '&'
2449 || *eap->cmd == '$'
2450 || *eap->cmd == '@'
2451 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01002452 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002453 {
2454 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
2455 if (*line == NULL || *line == eap->cmd)
2456 return FAIL;
2457 return OK;
2458 }
2459 }
2460 }
2461
2462 if (*eap->cmd == '[')
2463 {
2464 // [var, var] = expr
2465 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
2466 if (*line == NULL)
2467 return FAIL;
2468 if (*line != eap->cmd)
2469 return OK;
2470 }
2471 return NOTDONE;
2472}
2473
Bram Moolenaar9a015112021-12-31 14:06:45 +00002474/*
2475 * Check if arguments of "ufunc" shadow variables in "cctx".
2476 * Return OK or FAIL.
2477 */
2478 static int
2479check_args_shadowing(ufunc_T *ufunc, cctx_T *cctx)
2480{
2481 int i;
2482 char_u *arg;
2483 int r = OK;
2484
2485 // Make sure arguments are not found when compiling a second time.
2486 ufunc->uf_args_visible = 0;
2487
2488 // Check for arguments shadowing variables from the context.
2489 for (i = 0; i < ufunc->uf_args.ga_len; ++i)
2490 {
2491 arg = ((char_u **)(ufunc->uf_args.ga_data))[i];
Bram Moolenaardce24412022-02-08 20:35:30 +00002492 if (check_defined(arg, STRLEN(arg), cctx, NULL, TRUE) == FAIL)
Bram Moolenaar9a015112021-12-31 14:06:45 +00002493 {
2494 r = FAIL;
2495 break;
2496 }
2497 }
2498 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
2499 return r;
2500}
2501
Bram Moolenaar7b829262021-10-13 15:04:34 +01002502
2503/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02002504 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002505 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02002506 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02002507 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02002508add_def_function(ufunc_T *ufunc)
2509{
2510 dfunc_T *dfunc;
2511
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002512 if (def_functions.ga_len == 0)
2513 {
2514 // The first position is not used, so that a zero uf_dfunc_idx means it
2515 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02002516 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002517 return FAIL;
2518 ++def_functions.ga_len;
2519 }
2520
Bram Moolenaar09689a02020-05-09 22:50:08 +02002521 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02002522 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02002523 return FAIL;
2524 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
2525 CLEAR_POINTER(dfunc);
2526 dfunc->df_idx = def_functions.ga_len;
2527 ufunc->uf_dfunc_idx = dfunc->df_idx;
2528 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002529 dfunc->df_name = vim_strsave(ufunc->uf_name);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002530 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002531 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02002532 ++def_functions.ga_len;
2533 return OK;
2534}
2535
2536/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 * After ex_function() has collected all the function lines: parse and compile
2538 * the lines into instructions.
2539 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01002540 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
2541 * the return statement (used for lambda). When uf_ret_type is already set
2542 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002543 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002544 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02002545 * This can be used recursively through compile_lambda(), which may reallocate
2546 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02002547 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02002549 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01002550compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02002551 ufunc_T *ufunc,
2552 int check_return_type,
2553 compiletype_T compile_type,
2554 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 char_u *line = NULL;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002557 garray_T lines_to_free;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 cctx_T cctx;
2561 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01002562 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02002563 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 int ret = FAIL;
2565 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02002566 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002567 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002568 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02002569 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01002570#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002571 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01002572#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002573 int debug_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002575 // allocated lines are freed at the end
2576 ga_init2(&lines_to_free, sizeof(char_u *), 50);
2577
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002578 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01002579 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002580 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02002582 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2583 + ufunc->uf_dfunc_idx;
Bram Moolenaar3b814af2021-06-15 10:22:17 +02002584 isn_T *instr_dest = NULL;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002585
2586 switch (compile_type)
2587 {
2588 case CT_PROFILE:
2589#ifdef FEAT_PROFILE
2590 instr_dest = dfunc->df_instr_prof; break;
2591#endif
2592 case CT_NONE: instr_dest = dfunc->df_instr; break;
2593 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
2594 }
2595 if (instr_dest != NULL)
2596 // Was compiled in this mode before: Free old instructions.
2597 delete_def_function_contents(dfunc, FALSE);
2598 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02002600 else
2601 {
2602 if (add_def_function(ufunc) == FAIL)
2603 return FAIL;
2604 new_def_function = TRUE;
2605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606
Bram Moolenaar985116a2020-07-12 17:31:09 +02002607 ufunc->uf_def_status = UF_COMPILING;
2608
Bram Moolenaara80faa82020-04-12 19:37:17 +02002609 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002610
Bram Moolenaare99d4222021-06-13 14:01:26 +02002611 cctx.ctx_compile_type = compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 cctx.ctx_ufunc = ufunc;
2613 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002614 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002616 // Each entry on the type stack consists of two type pointers.
2617 ga_init2(&cctx.ctx_type_stack, sizeof(type2_T), 50);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
2619 cctx.ctx_type_list = &ufunc->uf_type_list;
2620 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
2621 instr = &cctx.ctx_instr;
2622
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002623 // Set the context to the function, it may be compiled when called from
2624 // another script. Set the script version to the most modern one.
2625 // The line number will be set in next_line_from_context().
2626 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
2628
Bram Moolenaardc4c2302021-04-25 13:54:42 +02002629 // Don't use the flag from ":legacy" here.
2630 cmdmod.cmod_flags &= ~CMOD_LEGACY;
2631
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002632 // Make sure error messages are OK.
2633 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
2634 if (do_estack_push)
2635 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02002636 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002637
Bram Moolenaar9a015112021-12-31 14:06:45 +00002638 if (check_args_shadowing(ufunc, &cctx) == FAIL)
2639 goto erret;
2640
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002641 if (ufunc->uf_def_args.ga_len > 0)
2642 {
2643 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002644 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002645 int i;
2646 char_u *arg;
2647 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002648 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002649
2650 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01002651 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002652 for (i = 0; i < count; ++i)
2653 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002654 type_T *val_type;
2655 int arg_idx = first_def_arg + i;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002656 where_T where = WHERE_INIT;
Bram Moolenaar12bce952021-03-11 20:04:04 +01002657 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002658 int jump_instr_idx = instr->ga_len;
2659 isn_T *isn;
2660
2661 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
2662 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
2663 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01002664
2665 // Make sure later arguments are not found.
Bram Moolenaare28d9b32021-07-03 18:56:53 +02002666 ufunc->uf_args_visible = arg_idx;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002667
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002668 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01002669 r = compile_expr0(&arg, &cctx);
2670
Bram Moolenaar12bce952021-03-11 20:04:04 +01002671 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002672 goto erret;
2673
2674 // If no type specified use the type of the default value.
2675 // Otherwise check that the default value type matches the
2676 // specified type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00002677 val_type = get_type_on_stack(&cctx, 0);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002678 where.wt_index = arg_idx + 1;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002679 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002680 {
2681 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002682 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002683 }
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00002684 else if (need_type_where(val_type, ufunc->uf_arg_types[arg_idx],
2685 -1, where, &cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002686 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02002687
2688 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002689 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002690
2691 // set instruction index in JUMP_IF_ARG_SET to here
2692 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
2693 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002694 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002695
2696 if (did_set_arg_type)
2697 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002698 }
Bram Moolenaare28d9b32021-07-03 18:56:53 +02002699 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002700
2701 /*
2702 * Loop over all the lines of the function and generate instructions.
2703 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 for (;;)
2705 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002706 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002707 int starts_with_colon = FALSE;
2708 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002709 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01002710
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002711 // Bail out on the first error to avoid a flood of errors and report
2712 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01002713 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002714 goto erret;
2715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 if (line != NULL && *line == '|')
2717 // the line continues after a '|'
2718 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02002719 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02002720 && !(*line == '#' && (line == cctx.ctx_line_start
2721 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002723 semsg(_(e_trailing_characters_str), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 goto erret;
2725 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002726 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
2727 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 else
2729 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002730 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002731 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002732 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02002733 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01002734#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01002735 if (cctx.ctx_skip != SKIP_YES)
2736 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01002737#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01002739 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02002740 // Make a copy, splitting off nextcmd and removing trailing spaces
2741 // may change it.
2742 if (line != NULL)
2743 {
2744 line = vim_strsave(line);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002745 if (ga_add_string(&lines_to_free, line) == FAIL)
2746 goto erret;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02002747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 }
2749
Bram Moolenaara80faa82020-04-12 19:37:17 +02002750 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 ea.cmdlinep = &line;
2752 ea.cmd = skipwhite(line);
2753
Bram Moolenaarb2049902021-01-24 12:53:53 +01002754 if (*ea.cmd == '#')
2755 {
2756 // "#" starts a comment
2757 line = (char_u *)"";
2758 continue;
2759 }
2760
Bram Moolenaarf002a412021-01-24 13:34:18 +01002761#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02002762 if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
2763 && cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002764 {
2765 may_generate_prof_end(&cctx, prof_lnum);
2766
2767 prof_lnum = cctx.ctx_lnum;
2768 generate_instr(&cctx, ISN_PROF_START);
2769 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01002770#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02002771 if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
2772 && cctx.ctx_skip != SKIP_YES)
2773 {
2774 debug_lnum = cctx.ctx_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002775 generate_instr_debug(&cctx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02002776 }
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002777 cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
Bram Moolenaarb2049902021-01-24 12:53:53 +01002778
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02002779 // Some things can be recognized by the first character.
2780 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02002782 case '}':
2783 {
2784 // "}" ends a block scope
2785 scopetype_T stype = cctx.ctx_scope == NULL
2786 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02002788 if (stype == BLOCK_SCOPE)
2789 {
2790 compile_endblock(&cctx);
2791 line = ea.cmd;
2792 }
2793 else
2794 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002795 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02002796 goto erret;
2797 }
2798 if (line != NULL)
2799 line = skipwhite(ea.cmd + 1);
2800 continue;
2801 }
2802
2803 case '{':
2804 // "{" starts a block scope
2805 // "{'a': 1}->func() is something else
2806 if (ends_excmd(*skipwhite(ea.cmd + 1)))
2807 {
2808 line = compile_block(ea.cmd, &cctx);
2809 continue;
2810 }
2811 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 }
2813
2814 /*
2815 * COMMAND MODIFIERS
2816 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02002817 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002818 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
2819 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 {
2821 if (errormsg != NULL)
2822 goto erret;
2823 // empty line or comment
2824 line = (char_u *)"";
2825 continue;
2826 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002827 generate_cmdmods(&cctx, &local_cmdmod);
2828 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002830 // Check if there was a colon after the last command modifier or before
2831 // the current position.
2832 for (p = ea.cmd; p >= line; --p)
2833 {
2834 if (*p == ':')
2835 starts_with_colon = TRUE;
2836 if (p < ea.cmd && !VIM_ISWHITE(*p))
2837 break;
2838 }
2839
Bram Moolenaarce024c32021-06-26 13:00:49 +02002840 // Skip ":call" to get to the function name, unless using :legacy
Bram Moolenaar575f24b2020-08-12 14:21:11 +02002841 p = ea.cmd;
Bram Moolenaarce024c32021-06-26 13:00:49 +02002842 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02002843 {
Bram Moolenaarce024c32021-06-26 13:00:49 +02002844 if (checkforcmd(&ea.cmd, "call", 3))
2845 {
2846 if (*ea.cmd == '(')
2847 // not for "call()"
2848 ea.cmd = p;
2849 else
2850 ea.cmd = skipwhite(ea.cmd);
2851 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852
Bram Moolenaarce024c32021-06-26 13:00:49 +02002853 if (!starts_with_colon)
2854 {
2855 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002856
Bram Moolenaarce024c32021-06-26 13:00:49 +02002857 // Check for assignment after command modifiers.
2858 assign = may_compile_assignment(&ea, &line, &cctx);
2859 if (assign == OK)
2860 goto nextline;
2861 if (assign == FAIL)
2862 goto erret;
2863 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 }
2865
2866 /*
2867 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02002868 * 'text'->func() should not be confused with 'a mark
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002869 * "++nr" and "--nr" are eval commands
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02002870 * in "$ENV->func()" the "$" is not a range
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002872 cmd = ea.cmd;
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00002873 if ((*cmd != '$' || starts_with_colon)
Bram Moolenaarce024c32021-06-26 13:00:49 +02002874 && (starts_with_colon || !(*cmd == '\''
Bram Moolenaard3a11782022-01-05 16:50:40 +00002875 || (cmd[0] != NUL && cmd[0] == cmd[1]
2876 && (*cmd == '+' || *cmd == '-')))))
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002877 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002878 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02002879 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02002880 {
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00002881 if (!starts_with_colon
2882 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02002883 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01002884 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02002885 goto erret;
2886 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01002887 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02002888 if (ends_excmd2(line, ea.cmd))
2889 {
2890 // A range without a command: jump to the line.
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002891 generate_EXEC(&cctx, ISN_EXECRANGE,
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00002892 vim_strnsave(cmd, ea.cmd - cmd));
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002893 line = ea.cmd;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02002894 goto nextline;
2895 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02002896 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02002897 }
Bram Moolenaar47bc9c32021-07-17 21:24:56 +02002898 p = find_ex_command(&ea, NULL,
2899 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002900 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002902 if (p == NULL)
2903 {
2904 if (cctx.ctx_skip != SKIP_YES)
Bram Moolenaarbed34f02022-01-19 20:48:37 +00002905 semsg(_(e_ambiguous_use_of_user_defined_command_str), ea.cmd);
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002906 goto erret;
2907 }
2908
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002909 // When using ":legacy cmd" always use compile_exec().
2910 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002911 {
2912 char_u *start = ea.cmd;
2913
Bram Moolenaarc3cb1c92021-06-02 16:47:53 +02002914 switch (ea.cmdidx)
2915 {
2916 case CMD_if:
2917 case CMD_elseif:
2918 case CMD_else:
2919 case CMD_endif:
2920 case CMD_for:
2921 case CMD_endfor:
2922 case CMD_continue:
2923 case CMD_break:
2924 case CMD_while:
2925 case CMD_endwhile:
2926 case CMD_try:
2927 case CMD_catch:
2928 case CMD_finally:
2929 case CMD_endtry:
2930 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
2931 goto erret;
2932 default: break;
2933 }
2934
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002935 // ":legacy return expr" needs to be handled differently.
2936 if (checkforcmd(&start, "return", 4))
2937 ea.cmdidx = CMD_return;
2938 else
2939 ea.cmdidx = CMD_legacy;
2940 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
2943 {
Bram Moolenaare525bdd2021-08-07 18:12:40 +02002944 if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002945 {
2946 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01002947 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002948 }
Bram Moolenaare525bdd2021-08-07 18:12:40 +02002949 else if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01002951 // CMD_var cannot happen, compile_assignment() above would be
2952 // used. Most likely an assignment to a non-existing variable.
2953 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02002954 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 }
2957
Bram Moolenaar3988f642020-08-27 22:43:03 +02002958 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002959 && ea.cmdidx != CMD_elseif
2960 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02002961 && ea.cmdidx != CMD_endif
2962 && ea.cmdidx != CMD_endfor
2963 && ea.cmdidx != CMD_endwhile
2964 && ea.cmdidx != CMD_catch
2965 && ea.cmdidx != CMD_finally
2966 && ea.cmdidx != CMD_endtry)
2967 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02002968 emsg(_(e_unreachable_code_after_return));
2969 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02002970 }
2971
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002972 p = skipwhite(p);
2973 if (ea.cmdidx != CMD_SIZE
2974 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
2975 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02002976 if (ea.cmdidx >= 0)
2977 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002978 if ((ea.argt & EX_BANG) && *p == '!')
2979 {
2980 ea.forceit = TRUE;
2981 p = skipwhite(p + 1);
2982 }
2983 }
2984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 switch (ea.cmdidx)
2986 {
2987 case CMD_def:
Bram Moolenaar38453522021-11-28 22:00:12 +00002988 case CMD_function:
Bram Moolenaar04b12692020-05-04 23:24:44 +02002989 ea.arg = p;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002990 line = compile_nested_function(&ea, &cctx, &lines_to_free);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002991 break;
2992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 case CMD_return:
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002994 line = compile_return(p, check_return_type,
2995 local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02002996 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 break;
2998
2999 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02003000 emsg(_(e_cannot_use_let_in_vim9_script));
3001 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003002 case CMD_var:
3003 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 case CMD_const:
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003005 case CMD_increment:
3006 case CMD_decrement:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003008 if (line == p)
3009 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 break;
3011
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003012 case CMD_unlet:
3013 case CMD_unlockvar:
3014 case CMD_lockvar:
3015 line = compile_unletlock(p, &ea, &cctx);
3016 break;
3017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 case CMD_import:
Bram Moolenaar4db572e2021-07-18 18:21:38 +02003019 emsg(_(e_import_can_only_be_used_in_script));
3020 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 break;
3022
3023 case CMD_if:
3024 line = compile_if(p, &cctx);
3025 break;
3026 case CMD_elseif:
3027 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003028 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 break;
3030 case CMD_else:
3031 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003032 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 break;
3034 case CMD_endif:
3035 line = compile_endif(p, &cctx);
3036 break;
3037
3038 case CMD_while:
3039 line = compile_while(p, &cctx);
3040 break;
3041 case CMD_endwhile:
3042 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003043 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 break;
3045
3046 case CMD_for:
3047 line = compile_for(p, &cctx);
3048 break;
3049 case CMD_endfor:
3050 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003051 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 break;
3053 case CMD_continue:
3054 line = compile_continue(p, &cctx);
3055 break;
3056 case CMD_break:
3057 line = compile_break(p, &cctx);
3058 break;
3059
3060 case CMD_try:
3061 line = compile_try(p, &cctx);
3062 break;
3063 case CMD_catch:
3064 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003065 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 break;
3067 case CMD_finally:
3068 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02003069 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 break;
3071 case CMD_endtry:
3072 line = compile_endtry(p, &cctx);
3073 break;
3074 case CMD_throw:
3075 line = compile_throw(p, &cctx);
3076 break;
3077
Bram Moolenaar007f9d62020-07-06 23:04:49 +02003078 case CMD_eval:
Bram Moolenaarc3235272021-07-10 19:42:03 +02003079 line = compile_eval(p, &cctx);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02003080 break;
3081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003084 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003085 case CMD_echomsg:
3086 case CMD_echoerr:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003087 case CMD_echoconsole:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003088 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003089 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003091 case CMD_put:
3092 ea.cmd = cmd;
3093 line = compile_put(p, &ea, &cctx);
3094 break;
3095
Bram Moolenaar4c137212021-04-19 16:48:48 +02003096 case CMD_substitute:
Bram Moolenaar7b829262021-10-13 15:04:34 +01003097 if (check_global_and_subst(ea.cmd, p) == FAIL)
3098 goto erret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003099 if (cctx.ctx_skip == SKIP_YES)
3100 line = (char_u *)"";
3101 else
3102 {
3103 ea.arg = p;
3104 line = compile_substitute(line, &ea, &cctx);
3105 }
3106 break;
3107
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003108 case CMD_redir:
3109 ea.arg = p;
3110 line = compile_redir(line, &ea, &cctx);
3111 break;
3112
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003113 case CMD_cexpr:
3114 case CMD_lexpr:
3115 case CMD_caddexpr:
3116 case CMD_laddexpr:
3117 case CMD_cgetexpr:
3118 case CMD_lgetexpr:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003119#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003120 ea.arg = p;
3121 line = compile_cexpr(line, &ea, &cctx);
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003122#else
3123 ex_ni(&ea);
3124 line = NULL;
3125#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003126 break;
3127
Bram Moolenaarae616492020-07-28 20:07:27 +02003128 case CMD_append:
3129 case CMD_change:
3130 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003131 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02003132 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02003133 case CMD_xit:
3134 not_in_vim9(&ea);
3135 goto erret;
3136
Bram Moolenaar002262f2020-07-08 17:47:57 +02003137 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02003138 if (cctx.ctx_skip != SKIP_YES)
3139 {
3140 semsg(_(e_invalid_command_str), ea.cmd);
3141 goto erret;
3142 }
3143 // We don't check for a next command here.
3144 line = (char_u *)"";
3145 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02003146
Bram Moolenaar20677332021-06-06 17:02:53 +02003147 case CMD_lua:
3148 case CMD_mzscheme:
3149 case CMD_perl:
3150 case CMD_py3:
3151 case CMD_python3:
3152 case CMD_python:
3153 case CMD_pythonx:
3154 case CMD_ruby:
3155 case CMD_tcl:
3156 ea.arg = p;
3157 if (vim_strchr(line, '\n') == NULL)
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02003158 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar20677332021-06-06 17:02:53 +02003159 else
3160 // heredoc lines have been concatenated with NL
3161 // characters in get_function_body()
3162 line = compile_script(line, &cctx);
3163 break;
3164
Bram Moolenaar107f7322022-02-06 17:30:41 +00003165 case CMD_vim9script:
Bram Moolenaar8cbf2492022-02-06 20:28:13 +00003166 if (cctx.ctx_skip != SKIP_YES)
3167 {
3168 emsg(_(e_vim9script_can_only_be_used_in_script));
3169 goto erret;
3170 }
3171 line = (char_u *)"";
3172 break;
Bram Moolenaar107f7322022-02-06 17:30:41 +00003173
Bram Moolenaar7b829262021-10-13 15:04:34 +01003174 case CMD_global:
3175 if (check_global_and_subst(ea.cmd, p) == FAIL)
3176 goto erret;
3177 // FALLTHROUGH
Bram Moolenaar20677332021-06-06 17:02:53 +02003178 default:
3179 // Not recognized, execute with do_cmdline_cmd().
3180 ea.arg = p;
3181 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 break;
3183 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02003184nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 if (line == NULL)
3186 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02003187 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003189 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02003190 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 if (cctx.ctx_type_stack.ga_len < 0)
3193 {
3194 iemsg("Type stack underflow");
3195 goto erret;
3196 }
3197 }
3198
3199 if (cctx.ctx_scope != NULL)
3200 {
3201 if (cctx.ctx_scope->se_type == IF_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00003202 emsg(_(e_missing_endif));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00003204 emsg(_(e_missing_endwhile));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00003206 emsg(_(e_missing_endfor));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003208 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 goto erret;
3210 }
3211
Bram Moolenaarefd88552020-06-18 20:50:10 +02003212 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 {
Bram Moolenaare6174fd2021-06-12 18:30:56 +02003214 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
3215 ufunc->uf_ret_type = &t_void;
3216 else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003218 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 goto erret;
3220 }
3221
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003222 // Return void if there is no return at the end.
3223 generate_instr(&cctx, ISN_RETURN_VOID);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 }
3225
Bram Moolenaar599410c2021-04-10 14:03:43 +02003226 // When compiled with ":silent!" and there was an error don't consider the
3227 // function compiled.
3228 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02003229 {
3230 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3231 + ufunc->uf_dfunc_idx;
3232 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003233 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003234#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02003235 if (cctx.ctx_compile_type == CT_PROFILE)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003236 {
3237 dfunc->df_instr_prof = instr->ga_data;
3238 dfunc->df_instr_prof_count = instr->ga_len;
3239 }
3240 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01003241#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02003242 if (cctx.ctx_compile_type == CT_DEBUG)
3243 {
3244 dfunc->df_instr_debug = instr->ga_data;
3245 dfunc->df_instr_debug_count = instr->ga_len;
3246 }
3247 else
Bram Moolenaarb2049902021-01-24 12:53:53 +01003248 {
3249 dfunc->df_instr = instr->ga_data;
3250 dfunc->df_instr_count = instr->ga_len;
3251 }
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003252 dfunc->df_varcount = dfunc->df_var_names.ga_len;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003253 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003254 if (cctx.ctx_outer_used)
3255 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003256 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02003257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258
3259 ret = OK;
3260
3261erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02003262 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 {
Bram Moolenaar05afcee2020-03-31 23:32:31 +02003264 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3265 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003266
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02003267 // Compiling aborted, free the generated instructions.
Bram Moolenaar8238f082021-04-20 21:10:48 +02003268 clear_instr_ga(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003269 VIM_CLEAR(dfunc->df_name);
Bram Moolenaarcaf1a2f2021-06-15 11:27:21 +02003270 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003271
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02003272 // If using the last entry in the table and it was added above, we
3273 // might as well remove it.
3274 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02003275 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02003276 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003277 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02003278 ufunc->uf_dfunc_idx = 0;
3279 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02003280 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003281
Bram Moolenaar3cca2992020-04-02 22:57:36 +02003282 while (cctx.ctx_scope != NULL)
3283 drop_scope(&cctx);
3284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 if (errormsg != NULL)
3286 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01003287 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02003288 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 }
3290
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003291 if (cctx.ctx_redir_lhs.lhs_name != NULL)
3292 {
3293 if (ret == OK)
3294 {
3295 emsg(_(e_missing_redir_end));
3296 ret = FAIL;
3297 }
3298 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02003299 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003300 }
3301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02003303 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02003304 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003305 if (do_estack_push)
3306 estack_pop();
3307
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00003308 ga_clear_strings(&lines_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003309 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003310 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02003312 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313}
3314
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003315 void
3316set_function_type(ufunc_T *ufunc)
3317{
3318 int varargs = ufunc->uf_va_name != NULL;
3319 int argcount = ufunc->uf_args.ga_len;
3320
3321 // Create a type for the function, with the return type and any
3322 // argument types.
3323 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
3324 // The type is included in "tt_args".
3325 if (argcount > 0 || varargs)
3326 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01003327 if (ufunc->uf_type_list.ga_itemsize == 0)
3328 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003329 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
3330 argcount, &ufunc->uf_type_list);
3331 // Add argument types to the function type.
3332 if (func_type_add_arg_types(ufunc->uf_func_type,
3333 argcount + varargs,
3334 &ufunc->uf_type_list) == FAIL)
3335 return;
3336 ufunc->uf_func_type->tt_argcount = argcount + varargs;
3337 ufunc->uf_func_type->tt_min_argcount =
3338 argcount - ufunc->uf_def_args.ga_len;
3339 if (ufunc->uf_arg_types == NULL)
3340 {
3341 int i;
3342
3343 // lambda does not have argument types.
3344 for (i = 0; i < argcount; ++i)
3345 ufunc->uf_func_type->tt_args[i] = &t_any;
3346 }
3347 else
3348 mch_memmove(ufunc->uf_func_type->tt_args,
3349 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
3350 if (varargs)
3351 {
3352 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02003353 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003354 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
3355 }
3356 }
3357 else
3358 // No arguments, can use a predefined type.
3359 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
3360 argcount, &ufunc->uf_type_list);
3361}
3362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003364 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01003365 */
3366 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003367delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01003368{
3369 int idx;
3370
3371 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003372 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003373
3374 if (dfunc->df_instr != NULL)
3375 {
3376 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
3377 delete_instr(dfunc->df_instr + idx);
3378 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003379 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003380 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02003381 if (dfunc->df_instr_debug != NULL)
3382 {
3383 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
3384 delete_instr(dfunc->df_instr_debug + idx);
3385 VIM_CLEAR(dfunc->df_instr_debug);
3386 dfunc->df_instr_debug = NULL;
3387 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01003388#ifdef FEAT_PROFILE
3389 if (dfunc->df_instr_prof != NULL)
3390 {
3391 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
3392 delete_instr(dfunc->df_instr_prof + idx);
3393 VIM_CLEAR(dfunc->df_instr_prof);
3394 dfunc->df_instr_prof = NULL;
3395 }
3396#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01003397
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003398 if (mark_deleted)
3399 dfunc->df_deleted = TRUE;
3400 if (dfunc->df_ufunc != NULL)
3401 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003402}
3403
3404/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003405 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003406 * function, unless another user function still uses it.
3407 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 */
3409 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003410unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003412 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 {
3414 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3415 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003417 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003418 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003419 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003420 ufunc->uf_dfunc_idx = 0;
3421 if (dfunc->df_ufunc == ufunc)
3422 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 }
3424}
3425
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003426/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003427 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003428 */
3429 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003430link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003431{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003432 if (ufunc->uf_dfunc_idx > 0)
3433 {
3434 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
3435 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003436
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003437 ++dfunc->df_refcount;
3438 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003439}
3440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01003442/*
3443 * Free all functions defined with ":def".
3444 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 void
3446free_def_functions(void)
3447{
Bram Moolenaar20431c92020-03-20 18:39:46 +01003448 int idx;
3449
3450 for (idx = 0; idx < def_functions.ga_len; ++idx)
3451 {
3452 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
3453
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003454 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003455 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003456 }
3457
3458 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459}
3460#endif
3461
3462
3463#endif // FEAT_EVAL