blob: 42a30b192681dec69a046bba5175dde26aef9e6b [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +000011 * vim9compile.c: compiling a :def function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010012 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaardc7c3662021-12-20 15:04:29 +000024// Functions defined with :def are stored in this growarray.
25// They are never removed, so that they can be found by index.
26// Deleted functions have the df_deleted flag set.
27garray_T def_functions = {0, 0, sizeof(dfunc_T), 50, NULL};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010029static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010030
31/*
Bram Moolenaar709664c2020-12-12 14:33:41 +010032 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +010033 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +010034 * If "lvar" is NULL only check if the variable can be found.
35 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +000037 int
Bram Moolenaar709664c2020-12-12 14:33:41 +010038lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010039{
40 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +010041 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042
Bram Moolenaarae8d2de2020-02-13 21:42:24 +010043 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +010044 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020045
Bram Moolenaar58b40092023-01-11 15:59:05 +000046 if (((len == 4 && STRNCMP(name, "this", 4) == 0)
47 || (len == 5 && STRNCMP(name, "super", 5) == 0))
Bram Moolenaar00b28d62022-12-08 15:32:33 +000048 && cctx->ctx_ufunc != NULL
Bram Moolenaar574950d2023-01-03 19:08:50 +000049 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)))
Bram Moolenaar00b28d62022-12-08 15:32:33 +000050 {
Bram Moolenaar58b40092023-01-11 15:59:05 +000051 int is_super = *name == 's';
Bram Moolenaar6aa09372023-01-11 17:59:38 +000052 if (is_super)
53 {
54 if (name[5] != '.')
55 {
56 emsg(_(e_super_must_be_followed_by_dot));
57 return FAIL;
58 }
59 if (cctx->ctx_ufunc->uf_class != NULL
60 && cctx->ctx_ufunc->uf_class->class_extends == NULL)
61 {
62 emsg(_(e_using_super_not_in_child_class));
63 return FAIL;
64 }
65 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +000066 if (lvar != NULL)
67 {
68 CLEAR_POINTER(lvar);
Bram Moolenaar486fc252023-01-18 14:51:07 +000069 lvar->lv_loop_depth = -1;
Bram Moolenaar58b40092023-01-11 15:59:05 +000070 lvar->lv_name = (char_u *)(is_super ? "super" : "this");
Bram Moolenaar00b28d62022-12-08 15:32:33 +000071 if (cctx->ctx_ufunc->uf_class != NULL)
Bram Moolenaar58b40092023-01-11 15:59:05 +000072 {
Bram Moolenaarffdaca92022-12-09 21:41:48 +000073 lvar->lv_type = &cctx->ctx_ufunc->uf_class->class_object_type;
Bram Moolenaar58b40092023-01-11 15:59:05 +000074 if (is_super)
75 {
76 type_T *type = get_type_ptr(cctx->ctx_type_list);
77
78 if (type != NULL)
79 {
80 *type = *lvar->lv_type;
81 lvar->lv_type = type;
82 type->tt_flags |= TTFLAG_SUPER;
83 }
84 }
85 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +000086 }
87 return OK;
88 }
89
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020090 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010091 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
92 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010093 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaara275f2c2022-10-11 20:04:09 +010094 if (lvp->lv_name != NULL
95 && STRNCMP(name, lvp->lv_name, len) == 0
Bram Moolenaar709664c2020-12-12 14:33:41 +010096 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020097 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010098 if (lvar != NULL)
99 {
100 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100101 lvar->lv_from_outer = 0;
Bram Moolenaar65449bd2022-09-19 16:02:43 +0100102 // If the variable was declared inside a loop set
103 // lvar->lv_loop_idx and lvar->lv_loop_depth.
104 get_loop_var_idx(cctx, idx, lvar);
Bram Moolenaar709664c2020-12-12 14:33:41 +0100105 }
106 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100108 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200109
110 // Find local in outer function scope.
111 if (cctx->ctx_outer != NULL)
112 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100113 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200114 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100115 if (lvar != NULL)
116 {
117 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100118 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100119 }
120 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200121 }
122 }
123
Bram Moolenaar709664c2020-12-12 14:33:41 +0100124 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125}
126
127/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200128 * Lookup an argument in the current function and an enclosing function.
129 * Returns the argument index in "idxp"
130 * Returns the argument type in "type"
131 * Sets "gen_load_outer" to TRUE if found in outer scope.
132 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000134 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200135arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200136 char_u *name,
137 size_t len,
138 int *idxp,
139 type_T **type,
140 int *gen_load_outer,
141 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142{
143 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200144 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100146 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200147 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200148 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 {
150 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
151
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200152 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
153 {
154 if (idxp != NULL)
155 {
156 // Arguments are located above the frame pointer. One further
157 // if there is a vararg argument
158 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
159 + STACK_FRAME_SIZE)
160 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
161
162 if (cctx->ctx_ufunc->uf_arg_types != NULL)
163 *type = cctx->ctx_ufunc->uf_arg_types[idx];
164 else
165 *type = &t_any;
166 }
167 return OK;
168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200171 va_name = cctx->ctx_ufunc->uf_va_name;
172 if (va_name != NULL
173 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
174 {
175 if (idxp != NULL)
176 {
177 // varargs is always the last argument
178 *idxp = -STACK_FRAME_SIZE - 1;
179 *type = cctx->ctx_ufunc->uf_va_type;
180 }
181 return OK;
182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200184 if (cctx->ctx_outer != NULL)
185 {
186 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200187 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200188 == OK)
189 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100190 if (gen_load_outer != NULL)
191 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200192 return OK;
193 }
194 }
195
196 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197}
198
199/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200200 * Lookup a script-local variable in the current script, possibly defined in a
201 * block that contains the function "cctx->ctx_ufunc".
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000202 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100203 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200204 * Return NULL when not found.
205 */
206 static sallvar_T *
Bram Moolenaardce24412022-02-08 20:35:30 +0000207find_script_var(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200208{
209 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
210 hashitem_T *hi;
211 int cc;
212 sallvar_T *sav;
213 ufunc_T *ufunc;
214
215 // Find the list of all script variables with the right name.
216 if (len > 0)
217 {
218 cc = name[len];
219 name[len] = NUL;
220 }
221 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
222 if (len > 0)
223 name[len] = cc;
224 if (HASHITEM_EMPTY(hi))
225 return NULL;
226
227 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200228 if (sav->sav_block_id == 0)
229 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200230 return sav;
231
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200232 if (cctx == NULL)
233 {
Bram Moolenaar766ae5b2022-09-14 00:30:51 +0100234 if (cstack == NULL)
235 return NULL;
236
Bram Moolenaardce24412022-02-08 20:35:30 +0000237 // Not in a function scope, find variable with block ID equal to or
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000238 // smaller than the current block id. Use "cstack" to go up the block
239 // scopes.
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200240 while (sav != NULL)
241 {
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000242 int idx;
Bram Moolenaardce24412022-02-08 20:35:30 +0000243
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000244 for (idx = cstack->cs_idx; idx >= 0; --idx)
245 if (cstack->cs_block_id[idx] == sav->sav_block_id)
Bram Moolenaardce24412022-02-08 20:35:30 +0000246 break;
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000247 if (idx >= 0)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200248 break;
249 sav = sav->sav_next;
250 }
251 return sav;
252 }
253
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200254 // Go over the variables with this name and find one that was visible
255 // from the function.
256 ufunc = cctx->ctx_ufunc;
257 while (sav != NULL)
258 {
259 int idx;
260
261 // Go over the blocks that this function was defined in. If the
262 // variable block ID matches it was visible to the function.
263 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
264 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
265 return sav;
266 sav = sav->sav_next;
267 }
268
Bram Moolenaar58493cf2022-01-06 12:23:30 +0000269 // Not found, variable was not visible.
270 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200271}
272
273/*
Bram Moolenaar766ae5b2022-09-14 00:30:51 +0100274 * If "name" can be found in the current script set it's "block_id".
275 */
276 void
277update_script_var_block_id(char_u *name, int block_id)
278{
279 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
280 hashitem_T *hi;
281 sallvar_T *sav;
282
283 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
284 if (HASHITEM_EMPTY(hi))
285 return;
286 sav = HI2SAV(hi);
287 sav->sav_block_id = block_id;
288}
289
290/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100291 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200292 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000293 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000294script_is_vim9(void)
Bram Moolenaar84367732020-08-23 15:21:55 +0200295{
296 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
297}
298
299/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200300 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000301 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100302 * Returns OK or FAIL.
303 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000304 int
Bram Moolenaardce24412022-02-08 20:35:30 +0000305script_var_exists(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200307 if (current_sctx.sc_sid <= 0)
308 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200309 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200310 {
311 // Check script variables that were visible where the function was
312 // defined.
Bram Moolenaardce24412022-02-08 20:35:30 +0000313 if (find_script_var(name, len, cctx, cstack) != NULL)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200314 return OK;
315 }
316 else
317 {
318 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
319 dictitem_T *di;
320 int cc;
321
322 // Check script variables that are currently visible
323 cc = name[len];
324 name[len] = NUL;
325 di = find_var_in_ht(ht, 0, name, TRUE);
326 name[len] = cc;
327 if (di != NULL)
328 return OK;
329 }
330
331 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332}
333
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100334/*
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200335 * Returns the index of a class method or class variable with name "name"
336 * accessible in the currently compiled function.
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200337 * If "cl_ret" is not NULL set it to the class.
338 * Otherwise return -1.
339 */
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200340 static int
341cctx_class_midx(
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200342 cctx_T *cctx,
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200343 int is_method,
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200344 char_u *name,
345 size_t len,
346 class_T **cl_ret)
347{
348 if (cctx == NULL || cctx->ctx_ufunc == NULL
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200349 || cctx->ctx_ufunc->uf_class == NULL
350 || cctx->ctx_ufunc->uf_defclass == NULL)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200351 return -1;
352
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200353 // Search for the class method or variable in the class where the calling
354 // function is defined.
355 class_T *cl = cctx->ctx_ufunc->uf_defclass;
356 int m_idx = is_method ? class_method_idx(cl, name, len)
357 : class_member_idx(cl, name, len);
358 if (m_idx < 0)
359 {
360 cl = cl->class_extends;
361 while (cl != NULL)
362 {
363 m_idx = is_method ? class_method_idx(cl, name, len)
364 : class_member_idx(cl, name, len);
365 if (m_idx >= 0)
366 break;
367 cl = cl->class_extends;
368 }
369 }
370
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200371 if (m_idx >= 0)
372 {
373 if (cl_ret != NULL)
374 *cl_ret = cl;
375 }
376
377 return m_idx;
378}
379
380/*
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200381 * Returns the index of a class method with name "name" accessible in the
382 * currently compiled function. Returns -1 if not found. The class where the
383 * method is defined is returned in "cl_ret".
384 */
385 int
386cctx_class_method_idx(
387 cctx_T *cctx,
388 char_u *name,
389 size_t len,
390 class_T **cl_ret)
391{
392 return cctx_class_midx(cctx, TRUE, name, len, cl_ret);
393}
394
395/*
396 * Returns the index of a class variable with name "name" accessible in the
397 * currently compiled function. Returns -1 if not found. The class where the
398 * variable is defined is returned in "cl_ret".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200399 */
400 int
401cctx_class_member_idx(
402 cctx_T *cctx,
403 char_u *name,
404 size_t len,
405 class_T **cl_ret)
406{
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200407 return cctx_class_midx(cctx, FALSE, name, len, cl_ret);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200408}
409
410/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100411 * Return TRUE if "name" is a local variable, argument, script variable or
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000412 * imported. Also if "name" is "this" and in a class method.
Bram Moolenaare0890d62021-02-17 14:52:14 +0100413 */
414 static int
415variable_exists(char_u *name, size_t len, cctx_T *cctx)
416{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100417 return (cctx != NULL
418 && (lookup_local(name, len, NULL, cctx) == OK
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000419 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK
420 || (len == 4
421 && cctx->ctx_ufunc != NULL
Bram Moolenaar574950d2023-01-03 19:08:50 +0000422 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000423 && STRNCMP(name, "this", 4) == 0)))
Bram Moolenaardce24412022-02-08 20:35:30 +0000424 || script_var_exists(name, len, cctx, NULL) == OK
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200425 || cctx_class_member_idx(cctx, name, len, NULL) >= 0
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000426 || find_imported(name, len, FALSE) != NULL;
Bram Moolenaare0890d62021-02-17 14:52:14 +0100427}
428
429/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100430 * Return TRUE if "name" is a local variable, argument, script variable,
Bram Moolenaar97f8c102022-04-02 19:43:57 +0100431 * imported or function. Or commands are being skipped, a declaration may have
432 * been skipped then.
Bram Moolenaar6914e872021-03-06 21:01:09 +0100433 */
434 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100435item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100436{
Bram Moolenaar0631bb42022-02-13 21:20:21 +0000437 return variable_exists(name, len, cctx);
Bram Moolenaar6914e872021-03-06 21:01:09 +0100438}
439
440/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100441 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000442 * compilation context "cctx".
443 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200444 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100445 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100446 * Return FAIL and give an error if it defined.
447 */
448 int
Bram Moolenaardce24412022-02-08 20:35:30 +0000449check_defined(
450 char_u *p,
451 size_t len,
452 cctx_T *cctx,
453 cstack_T *cstack,
454 int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100455{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200456 int c = p[len];
457 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200458
Bram Moolenaarda479c72021-04-10 21:01:38 +0200459 // underscore argument is OK
460 if (len == 1 && *p == '_')
461 return OK;
462
Bram Moolenaardce24412022-02-08 20:35:30 +0000463 if (script_var_exists(p, len, cctx, cstack) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100464 {
465 if (is_arg)
466 semsg(_(e_argument_already_declared_in_script_str), p);
467 else
468 semsg(_(e_variable_already_declared_in_script_str), p);
469 return FAIL;
470 }
471
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200472 if (cctx_class_member_idx(cctx, p, len, NULL) >= 0)
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000473 {
474 if (is_arg)
475 semsg(_(e_argument_already_declared_in_class_str), p);
476 else
477 semsg(_(e_variable_already_declared_in_class_str), p);
478 return FAIL;
479 }
480
Bram Moolenaarad486a02020-08-01 23:22:18 +0200481 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100482 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100483 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200484 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000485 || find_imported(p, len, FALSE) != NULL
Bram Moolenaaracc4b562022-01-24 13:54:45 +0000486 || (ufunc = find_func_even_dead(p, 0)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100487 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200488 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200489 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
490 && (!func_is_global(ufunc)
491 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200492 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100493 if (is_arg)
494 semsg(_(e_argument_name_shadows_existing_variable_str), p);
495 else
496 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200497 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200498 return FAIL;
499 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100500 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200501 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100502 return OK;
503}
504
Bram Moolenaar65b95452020-07-19 14:03:09 +0200505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200507 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
508 * used. Return FALSE if the types will never match.
509 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200510 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200511use_typecheck(type_T *actual, type_T *expected)
512{
513 if (actual->tt_type == VAR_ANY
514 || actual->tt_type == VAR_UNKNOWN
515 || (actual->tt_type == VAR_FUNC
516 && (expected->tt_type == VAR_FUNC
517 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000518 && (actual->tt_member == &t_any
519 || actual->tt_member == &t_unknown
520 || actual->tt_argcount < 0)
521 && (actual->tt_member == &t_unknown ||
522 (actual->tt_member == &t_void)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100523 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200524 return TRUE;
LemonBoy50d48542024-07-04 17:03:17 +0200525 if (actual->tt_type == VAR_OBJECT && expected->tt_type == VAR_OBJECT)
526 return TRUE;
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200527 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
528 && actual->tt_type == expected->tt_type)
529 // This takes care of a nested list or dict.
530 return use_typecheck(actual->tt_member, expected->tt_member);
531 return FALSE;
532}
533
534/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200535 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200536 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200537 * - "actual" is a type that can be "expected" type: add a runtime check; or
538 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200539 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
540 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200541 */
Bram Moolenaara1c51952022-02-02 16:20:26 +0000542 int
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200543need_type_where(
544 type_T *actual,
545 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000546 int number_ok, // expect VAR_FLOAT but VAR_NUMBER is OK
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200547 int offset,
548 where_T where,
549 cctx_T *cctx,
550 int silent,
551 int actual_is_const)
552{
Bram Moolenaar44a89772021-12-18 12:31:33 +0000553 int ret;
554
Ernie Raele75fde62023-12-21 17:18:54 +0100555 if (expected->tt_type != VAR_CLASS && expected->tt_type != VAR_TYPEALIAS)
556 {
557 if (check_type_is_value(actual) == FAIL)
558 return FAIL;
559 }
560
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200561 if (expected == &t_bool && actual != &t_bool
562 && (actual->tt_flags & TTFLAG_BOOL_OK))
563 {
564 // Using "0", "1" or the result of an expression with "&&" or "||" as a
565 // boolean is OK but requires a conversion.
566 generate_2BOOL(cctx, FALSE, offset);
567 return OK;
568 }
569
Bram Moolenaar44a89772021-12-18 12:31:33 +0000570 ret = check_type_maybe(expected, actual, FALSE, where);
571 if (ret == OK)
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200572 return OK;
573
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000574 // If actual a constant a runtime check makes no sense. If it's
575 // null_function it is OK.
576 if (actual_is_const && ret == MAYBE && actual == &t_func_unknown)
577 return OK;
578
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200579 // If the actual type can be the expected type add a runtime check.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000580 if (!actual_is_const && ret == MAYBE && use_typecheck(actual, expected))
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200581 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000582 generate_TYPECHECK(cctx, expected, number_ok, offset,
LemonBoyc5d27442023-08-19 13:02:35 +0200583 where.wt_kind == WT_VARIABLE, where.wt_index);
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200584 return OK;
585 }
586
587 if (!silent)
588 type_mismatch_where(expected, actual, where);
589 return FAIL;
590}
591
Bram Moolenaar351ead02021-01-16 16:07:01 +0100592 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200593need_type(
594 type_T *actual,
595 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000596 int number_ok, // when expected is float number is also OK
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200597 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100598 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200599 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200600 int silent,
601 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200602{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200603 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100604
LemonBoyc5d27442023-08-19 13:02:35 +0200605 if (arg_idx > 0)
606 {
607 where.wt_index = arg_idx;
608 where.wt_kind = WT_ARGUMENT;
609 }
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000610 return need_type_where(actual, expected, number_ok, offset, where,
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200611 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200612}
613
614/*
Bram Moolenaarfa103972022-09-29 19:14:42 +0100615 * Set type of variable "lvar" to "type". If the variable is a constant then
616 * the type gets TTFLAG_CONST.
617 */
618 static void
619set_var_type(lvar_T *lvar, type_T *type_arg, cctx_T *cctx)
620{
621 type_T *type = type_arg;
622
Bram Moolenaar6586a012022-09-30 11:04:50 +0100623 if (lvar->lv_const == ASSIGN_CONST && (type->tt_flags & TTFLAG_CONST) == 0)
Bram Moolenaarfa103972022-09-29 19:14:42 +0100624 {
625 if (type->tt_flags & TTFLAG_STATIC)
626 // entry in static_types[] is followed by const type
627 type = type + 1;
628 else
629 {
630 type = copy_type(type, cctx->ctx_type_list);
631 type->tt_flags |= TTFLAG_CONST;
632 }
633 }
634 lvar->lv_type = type;
635}
636
637/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 * Reserve space for a local variable.
Bram Moolenaar6586a012022-09-30 11:04:50 +0100639 * "assign" can be ASSIGN_VAR for :var, ASSIGN_CONST for :const and
640 * ASSIGN_FINAL for :final.
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200641 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000643 lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +0200644reserve_local(
645 cctx_T *cctx,
646 char_u *name,
647 size_t len,
Bram Moolenaar6586a012022-09-30 11:04:50 +0100648 int assign,
Bram Moolenaare8211a32020-10-09 22:04:29 +0200649 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200652 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200654 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200656 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200657 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658 }
659
Bram Moolenaar35578162021-08-02 19:10:38 +0200660 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200661 return NULL;
662 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +0200663 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200665 // Every local variable uses the next entry on the stack. We could re-use
666 // the last ones when leaving a scope, but then variables used in a closure
667 // might get overwritten. To keep things simple do not re-use stack
668 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200669 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
670 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200671
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200672 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar6586a012022-09-30 11:04:50 +0100673 lvar->lv_const = assign;
Bram Moolenaarfa103972022-09-29 19:14:42 +0100674 if (type == &t_unknown || type == &t_any)
675 // type not known yet, may be inferred from RHS
676 lvar->lv_type = type;
677 else
678 // may use TTFLAG_CONST
679 set_var_type(lvar, type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100680
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200681 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +0200682 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200683 return NULL;
684 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
685 vim_strsave(lvar->lv_name);
686 ++dfunc->df_var_names.ga_len;
687
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200688 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689}
690
691/*
Bram Moolenaar08251752021-01-11 21:20:18 +0100692 * If "check_writable" is ASSIGN_CONST give an error if the variable was
693 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
694 * error if the variable was defined with :const.
695 */
696 static int
697check_item_writable(svar_T *sv, int check_writable, char_u *name)
698{
699 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
700 || (check_writable == ASSIGN_FINAL
701 && sv->sv_const == ASSIGN_CONST))
702 {
Bram Moolenaard8e44472021-07-21 22:20:33 +0200703 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +0100704 return FAIL;
705 }
706 return OK;
707}
708
709/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +0100711 * Pass "check_writable" to check_item_writable().
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000712 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713 * Returns the index in "sn_var_vals" if found.
714 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +0100715 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 */
717 int
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000718get_script_item_idx(
719 int sid,
720 char_u *name,
721 int check_writable,
722 cctx_T *cctx,
723 cstack_T *cstack)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724{
725 hashtab_T *ht;
726 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100727 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200728 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729 int idx;
730
Bram Moolenaare3d46852020-08-29 13:39:17 +0200731 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200733 if (sid == current_sctx.sc_sid)
734 {
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000735 sallvar_T *sav = find_script_var(name, 0, cctx, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200736
737 if (sav == NULL)
738 return -2;
739 idx = sav->sav_var_vals_idx;
740 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +0100741 if (check_item_writable(sv, check_writable, name) == FAIL)
742 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200743 return idx;
744 }
745
746 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 ht = &SCRIPT_VARS(sid);
748 di = find_var_in_ht(ht, 0, name, TRUE);
749 if (di == NULL)
Bram Moolenaar577bd852022-01-10 18:42:52 +0000750 {
751 if (si->sn_autoload_prefix != NULL)
752 {
753 hashitem_T *hi;
754
755 // A variable exported from an autoload script is in the global
756 // variables, we can find it in the all_vars table.
757 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
758 if (!HASHITEM_EMPTY(hi))
759 return HI2SAV(hi)->sav_var_vals_idx;
760 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 return -2;
Bram Moolenaar577bd852022-01-10 18:42:52 +0000762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763
764 // Now find the svar_T index in sn_var_vals.
765 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
766 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200767 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 if (sv->sv_tv == &di->di_tv)
769 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100770 if (check_item_writable(sv, check_writable, name) == FAIL)
771 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 return idx;
773 }
774 }
775 return -1;
776}
777
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000778 static imported_T *
779find_imported_in_script(char_u *name, size_t len, int sid)
780{
781 scriptitem_T *si;
782 int idx;
783
784 if (!SCRIPT_ID_VALID(sid))
785 return NULL;
786 si = SCRIPT_ITEM(sid);
787 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
788 {
789 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
790
791 if (len == 0 ? STRCMP(name, import->imp_name) == 0
792 : STRLEN(import->imp_name) == len
793 && STRNCMP(name, import->imp_name, len) == 0)
794 return import;
795 }
796 return NULL;
797}
798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799/*
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000800 * Find "name" in imported items of the current script.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100801 * If "len" is 0 use any length that works.
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000802 * If "load" is TRUE and the script was not loaded yet, load it now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803 */
804 imported_T *
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000805find_imported(char_u *name, size_t len, int load)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200807 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +0200808 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809
Bram Moolenaarb775e722022-11-22 18:12:44 +0000810 // Skip over "s:" before "s:something" to find the import name.
811 int off = name[0] == 's' && name[1] == ':' ? 2 : 0;
812
813 imported_T *ret = find_imported_in_script(name + off, len - off,
814 current_sctx.sc_sid);
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000815 if (ret != NULL && load && (ret->imp_flags & IMP_FLAGS_AUTOLOAD))
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000816 {
Bram Moolenaar753885b2022-08-24 16:30:36 +0100817 scid_T actual_sid = 0;
Bram Moolenaar6809ff92022-07-26 15:10:56 +0100818 int save_emsg_off = emsg_off;
819
820 // "emsg_off" will be set when evaluating an expression silently, but
821 // we do want to know about errors in a script. Also because it then
822 // aborts when an error is encountered.
823 emsg_off = FALSE;
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000824
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000825 // script found before but not loaded yet
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000826 ret->imp_flags &= ~IMP_FLAGS_AUTOLOAD;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000827 (void)do_source(SCRIPT_ITEM(ret->imp_sid)->sn_name, FALSE,
Bram Moolenaar753885b2022-08-24 16:30:36 +0100828 DOSO_NONE, &actual_sid);
829 // If the script is a symlink it may be sourced with another name, may
830 // need to adjust the script ID for that.
831 if (actual_sid != 0)
832 ret->imp_sid = actual_sid;
Bram Moolenaar6809ff92022-07-26 15:10:56 +0100833
834 emsg_off = save_emsg_off;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000835 }
836 return ret;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200837}
838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839/*
Hirohito Higashibf7c88d2025-02-13 21:04:07 +0100840 * Find "name" in imported items of extended base class of the class to which
841 * the context :def function belongs.
842 */
843 imported_T *
844find_imported_from_extends(cctx_T *cctx, char_u *name, size_t len, int load)
845{
846 imported_T *ret = NULL;
847 class_T *cl_extends;
848
849 if (cctx == NULL || cctx->ctx_ufunc == NULL
850 || cctx->ctx_ufunc->uf_class == NULL)
851 return NULL;
852
853 cl_extends = cctx->ctx_ufunc->uf_class->class_extends;
854
855 if (cl_extends == NULL || cl_extends->class_class_function_count_child <= 0)
856 return NULL;
857 else
858 {
859 sctx_T current_sctx_save = current_sctx;
860
861 current_sctx = cl_extends->class_class_functions[0]->uf_script_ctx;
862 ret = find_imported(name, len, load);
863 current_sctx = current_sctx_save;
864
865 return ret;
866 }
867}
868
869/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000870 * Called when checking for a following operator at "arg". When the rest of
871 * the line is empty or only a comment, peek the next line. If there is a next
872 * line return a pointer to it and set "nextp".
873 * Otherwise skip over white space.
874 */
875 char_u *
876may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
877{
878 char_u *p = skipwhite(arg);
879
880 *nextp = NULL;
881 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
882 {
883 *nextp = peek_next_line_from_context(cctx);
884 if (*nextp != NULL)
885 return *nextp;
886 }
887 return p;
888}
889
890/*
Bram Moolenaar23c55272020-06-21 16:58:13 +0200891 * Return a pointer to the next line that isn't empty or only contains a
892 * comment. Skips over white space.
893 * Returns NULL if there is none.
894 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200895 char_u *
896peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +0200897{
898 int lnum = cctx->ctx_lnum;
899
900 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
901 {
902 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +0200903 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200904
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200905 // ignore NULLs inserted for continuation lines
906 if (line != NULL)
907 {
908 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100909 if (vim9_bad_comment(p))
910 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200911 if (*p != NUL && !vim9_comment_start(p))
912 return p;
913 }
Bram Moolenaar23c55272020-06-21 16:58:13 +0200914 }
915 return NULL;
916}
917
918/*
Bram Moolenaare6085c52020-04-12 20:19:16 +0200919 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +0200920 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +0200921 * Returns NULL when at the end.
922 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200923 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +0200924next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +0200925{
Bram Moolenaar7a092242020-04-16 22:10:49 +0200926 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200927
928 do
929 {
930 ++cctx->ctx_lnum;
931 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200932 {
933 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200934 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +0200935 }
Bram Moolenaare6085c52020-04-12 20:19:16 +0200936 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +0200937 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200938 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200939 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +0200940 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +0200941 return line;
942}
943
944/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100945 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200946 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +0200947 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200948 * Return FAIL if beyond the last line, "*arg" is unmodified then.
949 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000950 int
Bram Moolenaar2c330432020-04-13 14:41:35 +0200951may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200952{
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100953 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100954 if (vim9_bad_comment(*arg))
955 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200956 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200957 {
Bram Moolenaar23c55272020-06-21 16:58:13 +0200958 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200959
960 if (next == NULL)
961 return FAIL;
962 *arg = skipwhite(next);
963 }
964 return OK;
965}
966
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200967/*
968 * Idem, and give an error when failed.
969 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000970 int
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200971may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
972{
973 if (may_get_next_line(whitep, arg, cctx) == FAIL)
974 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +0100975 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200976 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200977 return FAIL;
978 }
979 return OK;
980}
981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200983 * Get a line from the compilation context, compatible with exarg_T getline().
984 * Return a pointer to the line in allocated memory.
985 * Return NULL for end-of-file or some error.
986 */
987 static char_u *
988exarg_getline(
989 int c UNUSED,
990 void *cookie,
991 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200992 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200993{
994 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +0200995 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200996
Bram Moolenaar66250c92020-08-20 15:02:42 +0200997 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200998 {
Bram Moolenaar2914a202020-09-27 18:24:03 +0200999 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02001000 return NULL;
1001 ++cctx->ctx_lnum;
1002 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
1003 // Comment lines result in NULL pointers, skip them.
1004 if (p != NULL)
1005 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02001006 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02001007}
1008
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001009 void
1010fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
1011{
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001012 eap->ea_getline = exarg_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001013 eap->cookie = cctx;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00001014 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001015}
1016
Bram Moolenaar04b12692020-05-04 23:24:44 +02001017/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001018 * Return TRUE if "ufunc" should be compiled, taking into account whether
1019 * "profile" indicates profiling is to be done.
1020 */
1021 int
1022func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
1023{
1024 switch (ufunc->uf_def_status)
1025 {
1026 case UF_TO_BE_COMPILED:
1027 return TRUE;
1028
1029 case UF_COMPILED:
1030 {
1031 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1032 + ufunc->uf_dfunc_idx;
1033
1034 switch (compile_type)
1035 {
1036 case CT_PROFILE:
1037#ifdef FEAT_PROFILE
1038 return dfunc->df_instr_prof == NULL;
1039#endif
1040 case CT_NONE:
1041 return dfunc->df_instr == NULL;
1042 case CT_DEBUG:
1043 return dfunc->df_instr_debug == NULL;
1044 }
1045 }
1046
1047 case UF_NOT_COMPILED:
1048 case UF_COMPILE_ERROR:
1049 case UF_COMPILING:
1050 break;
1051 }
1052 return FALSE;
1053}
1054
1055/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02001056 * Compile a nested :def command.
1057 */
1058 static char_u *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001059compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
Bram Moolenaar04b12692020-05-04 23:24:44 +02001060{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001061 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02001062 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001063 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001064 int off;
1065 char_u *func_name;
Bram Moolenaareef21022020-08-01 22:16:43 +02001066 char_u *lambda_name;
John Marriottb32800f2025-02-01 15:25:34 +01001067 size_t lambda_namelen;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001068 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001069 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001070 compiletype_T compile_type;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001071 int funcref_isn_idx = -1;
Bram Moolenaar1889f492022-08-16 19:34:44 +01001072 lvar_T *lvar = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001073
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001074 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02001075 {
1076 emsg(_(e_cannot_use_bang_with_nested_def));
1077 return NULL;
1078 }
1079
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001080 if (*name_start == '/')
1081 {
1082 name_end = skip_regexp(name_start + 1, '/', TRUE);
1083 if (*name_end == '/')
1084 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02001085 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001086 }
1087 if (name_end == name_start || *skipwhite(name_end) != '(')
1088 {
1089 if (!ends_excmd2(name_start, name_end))
1090 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00001091 if (*skipwhite(name_end) == '.')
1092 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
1093 eap->cmd);
1094 else
1095 semsg(_(e_invalid_command_str), eap->cmd);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001096 return NULL;
1097 }
1098
1099 // "def" or "def Name": list functions
1100 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
1101 return NULL;
1102 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
1103 }
1104
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001105 // Only g:Func() can use a namespace.
1106 if (name_start[1] == ':' && !is_global)
1107 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001108 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001109 return NULL;
1110 }
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00001111 if (cctx->ctx_skip != SKIP_YES
1112 && check_defined(name_start, name_end - name_start, cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +00001113 NULL, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02001114 return NULL;
Bram Moolenaarf681cfb2022-02-07 20:30:57 +00001115 if (!ASCII_ISUPPER(is_global ? name_start[2] : name_start[0]))
1116 {
Bram Moolenaar3787f262022-02-07 21:54:01 +00001117 semsg(_(e_function_name_must_start_with_capital_str), name_start);
Bram Moolenaarf681cfb2022-02-07 20:30:57 +00001118 return NULL;
1119 }
Bram Moolenaareef21022020-08-01 22:16:43 +02001120
Bram Moolenaar04b12692020-05-04 23:24:44 +02001121 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001122 fill_exarg_from_cctx(eap, cctx);
1123
Bram Moolenaar04b12692020-05-04 23:24:44 +02001124 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00001125 // We use the special <Lamba>99 name, but it's not really a lambda.
John Marriottb32800f2025-02-01 15:25:34 +01001126 lambda_name = get_lambda_name();
1127 lambda_namelen = get_lambda_name_len();
1128 lambda_name = vim_strnsave(lambda_name, lambda_namelen);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001129 if (lambda_name == NULL)
1130 return NULL;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001131
1132 // This may free the current line, make a copy of the name.
1133 off = is_global ? 2 : 0;
1134 func_name = vim_strnsave(name_start + off, name_end - name_start - off);
1135 if (func_name == NULL)
1136 {
1137 r = FAIL;
1138 goto theend;
1139 }
1140
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001141 // Make sure "KeyTyped" is not set, it may cause indent to be written.
1142 int save_KeyTyped = KeyTyped;
1143 KeyTyped = FALSE;
1144
h-eastb895b0f2023-09-24 15:46:31 +02001145 ufunc = define_function(eap, lambda_name, lines_to_free, 0, NULL, 0);
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001146
1147 KeyTyped = save_KeyTyped;
1148
Bram Moolenaar822ba242020-05-24 23:00:18 +02001149 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001150 {
1151 r = eap->skip ? OK : FAIL;
1152 goto theend;
1153 }
Bram Moolenaar7473a842021-12-28 17:55:26 +00001154 if (eap->nextcmd != NULL)
1155 {
1156 semsg(_(e_text_found_after_str_str),
1157 eap->cmdidx == CMD_def ? "enddef" : "endfunction", eap->nextcmd);
1158 r = FAIL;
Bram Moolenaard2939812021-12-30 17:09:05 +00001159 func_ptr_unref(ufunc);
Bram Moolenaar7473a842021-12-28 17:55:26 +00001160 goto theend;
1161 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01001162
1163 // copy over the block scope IDs before compiling
1164 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
1165 {
1166 int block_depth = cctx->ctx_ufunc->uf_block_depth;
1167
1168 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
1169 if (ufunc->uf_block_ids != NULL)
1170 {
1171 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
1172 sizeof(int) * block_depth);
1173 ufunc->uf_block_depth = block_depth;
1174 }
1175 }
1176
Bram Moolenaara915fa02022-03-23 11:29:15 +00001177 // Define the funcref before compiling, so that it is found by any
1178 // recursive call.
1179 if (is_global)
1180 {
Bram Moolenaar65449bd2022-09-19 16:02:43 +01001181 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaara915fa02022-03-23 11:29:15 +00001182 func_name = NULL;
1183 lambda_name = NULL;
1184 }
1185 else
1186 {
1187 // Define a local variable for the function reference.
Bram Moolenaar1889f492022-08-16 19:34:44 +01001188 lvar = reserve_local(cctx, func_name, name_end - name_start,
Bram Moolenaar6586a012022-09-30 11:04:50 +01001189 ASSIGN_CONST, ufunc->uf_func_type);
Bram Moolenaara915fa02022-03-23 11:29:15 +00001190 if (lvar == NULL)
1191 goto theend;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001192 if (generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, &funcref_isn_idx) == FAIL)
Bram Moolenaara915fa02022-03-23 11:29:15 +00001193 goto theend;
1194 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
1195 }
1196
Bram Moolenaar139575d2022-03-15 19:29:30 +00001197 compile_type = get_compile_type(ufunc);
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001198#ifdef FEAT_PROFILE
1199 // If the outer function is profiled, also compile the nested function for
1200 // profiling.
1201 if (cctx->ctx_compile_type == CT_PROFILE)
1202 compile_type = CT_PROFILE;
1203#endif
1204 if (func_needs_compiling(ufunc, compile_type)
1205 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02001206 {
1207 func_ptr_unref(ufunc);
Bram Moolenaar1889f492022-08-16 19:34:44 +01001208 if (lvar != NULL)
1209 // Now the local variable can't be used.
1210 *lvar->lv_name = '/'; // impossible value
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001211 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02001212 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02001213
Bram Moolenaar648594e2021-07-11 17:55:01 +02001214#ifdef FEAT_PROFILE
1215 // When the outer function is compiled for profiling, the nested function
1216 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001217 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02001218 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1219#endif
1220
Bram Moolenaara915fa02022-03-23 11:29:15 +00001221 // If a FUNCREF instruction was generated, set the index after compiling.
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001222 if (funcref_isn_idx != -1 && ufunc->uf_def_status == UF_COMPILED)
1223 {
1224 isn_T *funcref_isn = ((isn_T *)cctx->ctx_instr.ga_data) +
1225 funcref_isn_idx;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001226 funcref_isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001227 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001228
1229theend:
1230 vim_free(lambda_name);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001231 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001232 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02001233}
1234
1235/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001236 * Compile one Vim expression {expr} in string "p".
1237 * "p" points to the opening "{".
1238 * Return a pointer to the character after "}", NULL for an error.
1239 */
1240 char_u *
1241compile_one_expr_in_str(char_u *p, cctx_T *cctx)
1242{
1243 char_u *block_start;
1244 char_u *block_end;
1245
1246 // Skip the opening {.
1247 block_start = skipwhite(p + 1);
1248 block_end = block_start;
1249 if (*block_start != NUL && skip_expr(&block_end, NULL) == FAIL)
1250 return NULL;
1251 block_end = skipwhite(block_end);
1252 // The block must be closed by a }.
1253 if (*block_end != '}')
1254 {
1255 semsg(_(e_missing_close_curly_str), p);
1256 return NULL;
1257 }
1258 if (compile_expr0(&block_start, cctx) == FAIL)
1259 return NULL;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001260 may_generate_2STRING(-1, TOSTRING_INTERPOLATE, cctx);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001261
1262 return block_end + 1;
1263}
1264
1265/*
LemonBoy2eaef102022-05-06 13:14:50 +01001266 * Compile a string "str" (either containing a literal string or a mix of
1267 * literal strings and Vim expressions of the form `{expr}`). This is used
1268 * when compiling a heredoc assignment to a variable or an interpolated string
1269 * in a Vim9 def function. Vim9 instructions are generated to push strings,
1270 * evaluate expressions, concatenate them and create a list of lines. When
1271 * "evalstr" is TRUE, Vim expressions in "str" are evaluated.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001272 */
1273 int
LemonBoy2eaef102022-05-06 13:14:50 +01001274compile_all_expr_in_str(char_u *str, int evalstr, cctx_T *cctx)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001275{
LemonBoy2eaef102022-05-06 13:14:50 +01001276 char_u *p = str;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001277 char_u *val;
LemonBoy2eaef102022-05-06 13:14:50 +01001278 int count = 0;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001279
1280 if (cctx->ctx_skip == SKIP_YES)
1281 return OK;
1282
LemonBoy2eaef102022-05-06 13:14:50 +01001283 if (!evalstr || *str == NUL)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001284 {
LemonBoy2eaef102022-05-06 13:14:50 +01001285 // Literal string, possibly empty.
1286 val = *str != NUL ? vim_strsave(str) : NULL;
1287 return generate_PUSHS(cctx, &val);
1288 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001289
LemonBoy2eaef102022-05-06 13:14:50 +01001290 // Push all the string pieces to the stack, followed by a ISN_CONCAT.
1291 while (*p != NUL)
1292 {
1293 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +01001294 int escaped_brace = FALSE;
1295
1296 // Look for a block start.
1297 lit_start = p;
1298 while (*p != '{' && *p != '}' && *p != NUL)
1299 ++p;
1300
1301 if (*p != NUL && *p == p[1])
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001302 {
LemonBoy2eaef102022-05-06 13:14:50 +01001303 // Escaped brace, unescape and continue.
1304 // Include the brace in the literal string.
1305 ++p;
1306 escaped_brace = TRUE;
1307 }
1308 else if (*p == '}')
1309 {
1310 semsg(_(e_stray_closing_curly_str), str);
1311 return FAIL;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001312 }
LemonBoy372bcce2022-04-25 12:43:20 +01001313
LemonBoy2eaef102022-05-06 13:14:50 +01001314 // Append the literal part.
1315 if (p != lit_start)
1316 {
1317 val = vim_strnsave(lit_start, (size_t)(p - lit_start));
1318 if (generate_PUSHS(cctx, &val) == FAIL)
1319 return FAIL;
1320 ++count;
1321 }
1322
1323 if (*p == NUL)
1324 break;
1325
1326 if (escaped_brace)
1327 {
1328 // Skip the second brace.
1329 ++p;
1330 continue;
1331 }
1332
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001333 p = compile_one_expr_in_str(p, cctx);
1334 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +01001335 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01001336 ++count;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001337 }
LemonBoy2eaef102022-05-06 13:14:50 +01001338
1339 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001340 if (count > 1)
LemonBoy2eaef102022-05-06 13:14:50 +01001341 return generate_CONCAT(cctx, count);
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001342
1343 return OK;
1344}
1345
1346/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 * Return the length of an assignment operator, or zero if there isn't one.
1348 */
1349 int
1350assignment_len(char_u *p, int *heredoc)
1351{
1352 if (*p == '=')
1353 {
1354 if (p[1] == '<' && p[2] == '<')
1355 {
1356 *heredoc = TRUE;
1357 return 3;
1358 }
1359 return 1;
1360 }
1361 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
1362 return 2;
1363 if (STRNCMP(p, "..=", 3) == 0)
1364 return 3;
1365 return 0;
1366}
1367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001369 * Generate the load instruction for "name".
1370 */
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001371 static int
Bram Moolenaard505d172022-12-18 21:42:55 +00001372generate_loadvar(cctx_T *cctx, lhs_T *lhs)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001373{
Bram Moolenaard505d172022-12-18 21:42:55 +00001374 char_u *name = lhs->lhs_name;
1375 type_T *type = lhs->lhs_type;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001376 int res = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001377
1378 switch (lhs->lhs_dest)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001379 {
1380 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001381 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001382 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1383 break;
1384 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01001385 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001386 {
1387 if (name[2] == NUL)
1388 generate_instr_type(cctx, ISN_LOADGDICT, &t_dict_any);
1389 else
1390 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
1391 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01001392 else
1393 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001394 break;
1395 case dest_buffer:
1396 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
1397 break;
1398 case dest_window:
1399 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
1400 break;
1401 case dest_tab:
1402 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
1403 break;
1404 case dest_script:
Ernie Rael3f821d62024-04-24 20:07:50 +02001405 case dest_script_v9:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001406 res = compile_load_scriptvar(cctx,
Hirohito Higashibf7c88d2025-02-13 21:04:07 +01001407 name + (name[1] == ':' ? 2 : 0), NULL, NULL, NULL);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001408 break;
1409 case dest_env:
1410 // Include $ in the name here
1411 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
1412 break;
1413 case dest_reg:
1414 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
1415 break;
1416 case dest_vimvar:
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001417 generate_LOADV(cctx, name + 2);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001418 break;
1419 case dest_local:
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001420 if (cctx->ctx_skip != SKIP_YES)
1421 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001422 lvar_T *lvar = lhs->lhs_lvar;
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001423 if (lvar->lv_from_outer > 0)
1424 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
Bram Moolenaar65449bd2022-09-19 16:02:43 +01001425 lvar->lv_loop_depth, lvar->lv_loop_idx, type);
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001426 else
1427 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
1428 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001429 break;
Bram Moolenaard505d172022-12-18 21:42:55 +00001430 case dest_class_member:
1431 generate_CLASSMEMBER(cctx, TRUE, lhs->lhs_class,
1432 lhs->lhs_classmember_idx);
1433 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001434 case dest_expr:
1435 // list or dict value should already be on the stack.
1436 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001437 }
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001438
1439 return res;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001440}
1441
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001442/*
1443 * Skip over "[expr]" or ".member".
1444 * Does not check for any errors.
1445 */
1446 static char_u *
1447skip_index(char_u *start)
1448{
1449 char_u *p = start;
1450
1451 if (*p == '[')
1452 {
1453 p = skipwhite(p + 1);
1454 (void)skip_expr(&p, NULL);
1455 p = skipwhite(p);
1456 if (*p == ']')
1457 return p + 1;
1458 return p;
1459 }
1460 // if (*p == '.')
1461 return to_name_end(p + 1, TRUE);
1462}
1463
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001464 void
1465vim9_declare_error(char_u *name)
1466{
1467 char *scope = "";
1468
1469 switch (*name)
1470 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001471 case 'g': scope = _("global"); break;
1472 case 'b': scope = _("buffer"); break;
1473 case 'w': scope = _("window"); break;
1474 case 't': scope = _("tab"); break;
1475 case 'v': scope = "v:"; break;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001476 case '$': semsg(_(e_cannot_declare_an_environment_variable_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001477 return;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001478 case '&': semsg(_(e_cannot_declare_an_option_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001479 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001480 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001481 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001482 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001483 }
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001484 semsg(_(e_cannot_declare_a_scope_variable_str), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001485}
1486
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001487/*
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001488 * Return TRUE if "name" is a valid register to use.
1489 * Return FALSE and give an error message if not.
1490 */
1491 static int
1492valid_dest_reg(int name)
1493{
1494 if ((name == '@' || valid_yank_reg(name, FALSE)) && name != '.')
1495 return TRUE;
1496 emsg_invreg(name);
1497 return FAIL;
1498}
1499
1500/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001501 * For one assignment figure out the type of destination. Return it in "dest".
1502 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001503 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001504 * For a v:var "vimvaridx" is set.
1505 * "type" is set to the destination type if known, unchanted otherwise.
1506 * Return FAIL if an error message was given.
1507 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001508 int
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001509get_var_dest(
1510 char_u *name,
1511 assign_dest_T *dest,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001512 cmdidx_T cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001513 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001514 int *vimvaridx,
1515 type_T **type,
1516 cctx_T *cctx)
1517{
1518 char_u *p;
1519
1520 if (*name == '&')
1521 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001522 int cc;
1523 long numval;
1524 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001525 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001526
1527 *dest = dest_option;
1528 if (cmdidx == CMD_final || cmdidx == CMD_const)
1529 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001530 emsg(_(e_cannot_lock_option));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001531 return FAIL;
1532 }
1533 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001534 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001535 if (p == NULL)
1536 {
1537 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02001538 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001539 return FAIL;
1540 }
1541 cc = *p;
1542 *p = NUL;
1543 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001544 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001545 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001546 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001547 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001548 case gov_unknown:
Bram Moolenaare1242042021-12-16 20:56:57 +00001549 semsg(_(e_unknown_option_str), name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001550 return FAIL;
1551 case gov_string:
1552 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001553 if (opt_p_flags & P_FUNC)
1554 {
1555 // might be a Funcref, check the type later
1556 *type = &t_any;
1557 *dest = dest_func_option;
1558 }
1559 else
1560 {
1561 *type = &t_string;
1562 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001563 break;
1564 case gov_bool:
1565 case gov_hidden_bool:
1566 *type = &t_bool;
1567 break;
1568 case gov_number:
1569 case gov_hidden_number:
1570 *type = &t_number;
1571 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001572 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001573 }
1574 else if (*name == '$')
1575 {
1576 *dest = dest_env;
1577 *type = &t_string;
1578 }
1579 else if (*name == '@')
1580 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001581 if (!valid_dest_reg(name[1]))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001582 return FAIL;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001583 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02001584 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001585 }
1586 else if (STRNCMP(name, "g:", 2) == 0)
1587 {
1588 *dest = dest_global;
1589 }
1590 else if (STRNCMP(name, "b:", 2) == 0)
1591 {
1592 *dest = dest_buffer;
1593 }
1594 else if (STRNCMP(name, "w:", 2) == 0)
1595 {
1596 *dest = dest_window;
1597 }
1598 else if (STRNCMP(name, "t:", 2) == 0)
1599 {
1600 *dest = dest_tab;
1601 }
1602 else if (STRNCMP(name, "v:", 2) == 0)
1603 {
1604 typval_T *vtv;
1605 int di_flags;
1606
1607 *vimvaridx = find_vim_var(name + 2, &di_flags);
1608 if (*vimvaridx < 0)
1609 {
1610 semsg(_(e_variable_not_found_str), name);
1611 return FAIL;
1612 }
1613 // We use the current value of "sandbox" here, is that OK?
1614 if (var_check_ro(di_flags, name, FALSE))
1615 return FAIL;
1616 *dest = dest_vimvar;
1617 vtv = get_vim_var_tv(*vimvaridx);
1618 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
1619 }
1620 return OK;
1621}
1622
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001623 static int
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001624is_decl_command(cmdidx_T cmdidx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001625{
1626 return cmdidx == CMD_let || cmdidx == CMD_var
1627 || cmdidx == CMD_final || cmdidx == CMD_const;
1628}
1629
1630/*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001631 * Returns TRUE if the class or object variable in "lhs" is modifiable.
1632 * "var_start" points to the start of the variable name and "lhs->lhs_varlen"
1633 * has the total length. Note that the "lhs" can be nested an object reference
1634 * (e.g. a.b.c.d.var).
1635 */
1636 static int
1637lhs_class_member_modifiable(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
1638{
1639 size_t varlen = lhs->lhs_varlen;
1640 class_T *cl = lhs->lhs_type->tt_class;
1641 int is_object = lhs->lhs_type->tt_type == VAR_OBJECT;
1642 char_u *name = var_start + varlen + 1;
1643 size_t namelen = lhs->lhs_end - var_start - varlen - 1;
1644 ocmember_T *m;
1645
1646 m = member_lookup(cl, lhs->lhs_type->tt_type, name, namelen, NULL);
1647 if (m == NULL)
1648 {
1649 member_not_found_msg(cl, lhs->lhs_type->tt_type, name, namelen);
1650 return FALSE;
1651 }
1652
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001653 if (IS_ENUM(cl))
1654 {
1655 semsg(_(e_enumvalue_str_cannot_be_modified), cl->class_name,
1656 m->ocm_name);
1657 return FALSE;
1658 }
1659
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001660 // If it is private member variable, then accessing it outside the
1661 // class is not allowed.
1662 // If it is a read only class variable, then it can be modified
1663 // only inside the class where it is defined.
1664 if ((m->ocm_access != VIM_ACCESS_ALL) &&
1665 ((is_object && !inside_class(cctx, cl))
1666 || (!is_object && cctx->ctx_ufunc->uf_class != cl)))
1667 {
1668 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
Ernie Rael03042a22023-11-11 08:53:32 +01001669 ? e_cannot_access_protected_variable_str
RestorerZ7fe8f432023-09-24 23:21:24 +02001670 : e_variable_is_not_writable_str;
Ernie Raele6c9aa52023-10-06 19:55:52 +02001671 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001672 return FALSE;
1673 }
1674
1675 return TRUE;
1676}
1677
1678/*
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001679 * Initialize "lhs" with default values
Bram Moolenaar752fc692021-01-04 21:57:11 +01001680 */
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001681 static void
1682lhs_init_defaults(lhs_T *lhs)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001683{
Bram Moolenaar752fc692021-01-04 21:57:11 +01001684 CLEAR_POINTER(lhs);
1685 lhs->lhs_dest = dest_local;
1686 lhs->lhs_vimvaridx = -1;
1687 lhs->lhs_scriptvar_idx = -1;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001688 lhs->lhs_member_idx = -1;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001689}
Bram Moolenaar752fc692021-01-04 21:57:11 +01001690
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001691/*
1692 * When compiling a LHS variable name, find the end of the destination and the
1693 * end of the variable name.
1694 */
1695 static int
1696lhs_find_var_end(
1697 lhs_T *lhs,
1698 char_u *var_start,
1699 int is_decl,
1700 char_u **var_endp)
1701{
1702 char_u *var_end = *var_endp;
1703
1704 // "lhs_dest_end" is the end of the destination, including "[expr]" or
Bram Moolenaar752fc692021-01-04 21:57:11 +01001705 // ".name".
1706 // "var_end" is the end of the variable/option/etc. name.
1707 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
1708 if (*var_start == '@')
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001709 {
1710 if (!valid_dest_reg(var_start[1]))
1711 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001712 var_end = var_start + 2;
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001713 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001714 else
1715 {
1716 // skip over the leading "&", "&l:", "&g:" and "$"
1717 var_end = skip_option_env_lead(var_start);
1718 var_end = to_name_end(var_end, TRUE);
1719 }
1720
1721 // "a: type" is declaring variable "a" with a type, not dict "a:".
1722 if (is_decl && lhs->lhs_dest_end == var_start + 2
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001723 && lhs->lhs_dest_end[-1] == ':')
Bram Moolenaar752fc692021-01-04 21:57:11 +01001724 --lhs->lhs_dest_end;
1725 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
1726 --var_end;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001727
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001728 lhs->lhs_end = lhs->lhs_dest_end;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001729 *var_endp = var_end;
1730
1731 return OK;
1732}
1733
1734/*
1735 * Set various fields in "lhs"
1736 */
1737 static int
1738lhs_init(
1739 lhs_T *lhs,
1740 char_u *var_start,
1741 int is_decl,
1742 int heredoc,
1743 char_u **var_endp)
1744{
1745 char_u *var_end = *var_endp;
1746
1747 lhs_init_defaults(lhs);
1748
1749 // Find the end of the variable and the destination
1750 if (lhs_find_var_end(lhs, var_start, is_decl, &var_end) == FAIL)
1751 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001752
1753 // compute the length of the destination without "[expr]" or ".name"
1754 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02001755 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001756 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
1757 if (lhs->lhs_name == NULL)
1758 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01001759
1760 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
1761 // Something follows after the variable: "var[idx]" or "var.key".
1762 lhs->lhs_has_index = TRUE;
1763
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001764 lhs->lhs_type = heredoc ? &t_list_string : &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001765
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001766 *var_endp = var_end;
1767
1768 return OK;
1769}
1770
1771/*
1772 * Compile a LHS class variable name.
1773 */
1774 static int
1775compile_lhs_class_variable(
1776 cctx_T *cctx,
1777 lhs_T *lhs,
1778 class_T *defcl,
1779 int is_decl)
1780{
1781 if (cctx->ctx_ufunc->uf_defclass != defcl)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001782 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001783 // A class variable can be accessed without the class name
1784 // only inside a class.
1785 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
1786 lhs->lhs_name, defcl->class_name);
1787 return FAIL;
1788 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001789
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001790 if (is_decl)
1791 {
1792 semsg(_(e_variable_already_declared_in_class_str), lhs->lhs_name);
1793 return FAIL;
1794 }
1795
1796 ocmember_T *m = &defcl->class_class_members[lhs->lhs_classmember_idx];
1797 if (oc_var_check_ro(defcl, m))
1798 return FAIL;
1799
1800 lhs->lhs_dest = dest_class_member;
1801 // The class variable is defined either in the current class or
1802 // in one of the parent class in the hierarchy.
1803 lhs->lhs_class = defcl;
1804 lhs->lhs_type = oc_member_type_by_idx(defcl, FALSE,
1805 lhs->lhs_classmember_idx);
1806
1807 return OK;
1808}
1809
1810/*
1811 * Compile an imported LHS variable
1812 */
1813 static int
1814compile_lhs_import_var(
1815 lhs_T *lhs,
1816 imported_T *import,
1817 char_u *var_start,
1818 char_u **var_endp,
1819 char_u **rawnamep)
1820{
1821 char_u *var_end = *var_endp;
1822 char_u *dot = vim_strchr(var_start, '.');
1823 char_u *p;
1824
1825 // for an import the name is what comes after the dot
1826 if (dot == NULL)
1827 {
1828 semsg(_(e_no_dot_after_imported_name_str), var_start);
1829 return FAIL;
1830 }
1831
1832 p = skipwhite(dot + 1);
1833 var_end = to_name_end(p, TRUE);
1834 if (var_end == p)
1835 {
1836 semsg(_(e_missing_name_after_imported_name_str), var_start);
1837 return FAIL;
1838 }
1839
1840 vim_free(lhs->lhs_name);
1841 lhs->lhs_varlen = var_end - p;
1842 lhs->lhs_name = vim_strnsave(p, lhs->lhs_varlen);
1843 if (lhs->lhs_name == NULL)
1844 return FAIL;
1845 *rawnamep = lhs->lhs_name;
1846 lhs->lhs_scriptvar_sid = import->imp_sid;
1847
1848 // TODO: where do we check this name is exported?
1849
1850 // Check if something follows: "exp.var[idx]" or
1851 // "exp.var.key".
1852 lhs->lhs_has_index = lhs->lhs_dest_end > skipwhite(var_end);
1853
1854 *var_endp = var_end;
1855
1856 return OK;
1857}
1858
1859/*
1860 * Process a script-local variable when compiling a LHS variable name.
1861 */
1862 static int
1863compile_lhs_script_var(
1864 cctx_T *cctx,
1865 lhs_T *lhs,
1866 char_u *var_start,
1867 char_u *var_end,
1868 int is_decl)
1869{
1870 int script_namespace = FALSE;
1871 int script_var = FALSE;
1872 imported_T *import;
1873 char_u *var_name;
Yegappan Lakshmanan084529c2024-12-24 09:50:01 +01001874 size_t var_name_len;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001875
1876 if (lhs->lhs_varlen > 1 && STRNCMP(var_start, "s:", 2) == 0)
1877 script_namespace = TRUE;
1878
1879 if (script_namespace)
1880 {
1881 var_name = var_start + 2;
1882 var_name_len = lhs->lhs_varlen - 2;
1883 }
1884 else
1885 {
1886 var_name = var_start;
1887 var_name_len = lhs->lhs_varlen;
1888 }
1889
1890 if (script_var_exists(var_name, var_name_len, cctx, NULL) == OK)
1891 script_var = TRUE;
1892
1893 import = find_imported(var_start, lhs->lhs_varlen, FALSE);
1894
1895 if (script_namespace || script_var || import != NULL)
1896 {
1897 char_u *rawname = lhs->lhs_name + (lhs->lhs_name[1] == ':' ? 2 : 0);
1898
1899 if (script_namespace && current_script_is_vim9())
Bram Moolenaar752fc692021-01-04 21:57:11 +01001900 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001901 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), var_start);
1902 return FAIL;
1903 }
1904
1905 if (is_decl)
1906 {
1907 if (script_namespace)
1908 semsg(_(e_cannot_declare_script_variable_in_function_str),
1909 lhs->lhs_name);
1910 else
1911 semsg(_(e_variable_already_declared_in_script_str),
1912 lhs->lhs_name);
1913 return FAIL;
1914 }
1915 else if (cctx->ctx_ufunc->uf_script_ctx_version == SCRIPT_VERSION_VIM9
1916 && script_namespace
1917 && !script_var && import == NULL)
1918 {
1919 semsg(_(e_unknown_variable_str), lhs->lhs_name);
1920 return FAIL;
1921 }
1922
1923 lhs->lhs_dest = current_script_is_vim9() ? dest_script_v9 :
1924 dest_script;
1925
1926 // existing script-local variables should have a type
1927 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
1928 if (import != NULL)
1929 {
1930 if (compile_lhs_import_var(lhs, import, var_start, &var_end,
1931 &rawname) == FAIL)
1932 return FAIL;
1933 }
1934
1935 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
1936 {
1937 // Check writable only when no index follows.
1938 lhs->lhs_scriptvar_idx = get_script_item_idx(
1939 lhs->lhs_scriptvar_sid, rawname,
1940 lhs->lhs_has_index ? ASSIGN_FINAL :
1941 ASSIGN_CONST, cctx, NULL);
1942 if (lhs->lhs_scriptvar_idx >= 0)
1943 {
1944 scriptitem_T *si = SCRIPT_ITEM(lhs->lhs_scriptvar_sid);
1945 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1946 + lhs->lhs_scriptvar_idx;
1947
1948 lhs->lhs_type = sv->sv_type;
1949 }
1950 }
1951
1952 return OK;
1953 }
1954
1955 return check_defined(var_start, lhs->lhs_varlen, cctx, NULL, FALSE);
1956}
1957
1958/*
1959 * Compile the LHS destination.
1960 */
1961 static int
1962compile_lhs_var_dest(
1963 cctx_T *cctx,
1964 lhs_T *lhs,
1965 int cmdidx,
1966 char_u *var_start,
1967 char_u *var_end,
1968 int is_decl)
1969{
1970 int declare_error = FALSE;
1971
1972 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
1973 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
1974 &lhs->lhs_type, cctx) == FAIL)
1975 return FAIL;
1976
1977 if (lhs->lhs_dest != dest_local && cmdidx != CMD_const
1978 && cmdidx != CMD_final)
1979 {
1980 // Specific kind of variable recognized.
1981 declare_error = is_decl;
1982 }
1983 else
1984 {
1985 class_T *defcl;
1986
1987 // No specific kind of variable recognized, just a name.
1988 if (check_reserved_name(lhs->lhs_name, lhs->lhs_has_index
1989 && *var_end == '.') == FAIL)
1990 return FAIL;
1991
1992 if (lookup_local(var_start, lhs->lhs_varlen, &lhs->lhs_local_lvar,
1993 cctx) == OK)
1994 {
1995 lhs->lhs_lvar = &lhs->lhs_local_lvar;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001996 }
1997 else
1998 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001999 CLEAR_FIELD(lhs->lhs_arg_lvar);
2000 if (arg_exists(var_start, lhs->lhs_varlen,
2001 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
2002 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002003 {
2004 if (is_decl)
2005 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002006 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002007 return FAIL;
2008 }
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002009 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002010 }
2011 }
2012
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002013 if (lhs->lhs_lvar != NULL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002014 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002015 if (is_decl)
2016 {
2017 // if we come here with what looks like an assignment like
2018 // .= but which has been rejected by assignment_len() from
2019 // may_compile_assignment give a better error message
2020 char_u *p = skipwhite(lhs->lhs_end);
2021 if (p[0] == '.' && p[1] == '=')
2022 emsg(_(e_dot_equal_not_supported_with_script_version_two));
2023 else if (p[0] == ':')
2024 // type specified in a non-var assignment
2025 semsg(_(e_trailing_characters_str), p);
2026 else
2027 semsg(_(e_variable_already_declared_str), lhs->lhs_name);
2028 return FAIL;
2029 }
2030 }
2031 else if ((lhs->lhs_classmember_idx = cctx_class_member_idx(
2032 cctx, var_start, lhs->lhs_varlen, &defcl)) >= 0)
2033 {
2034 if (compile_lhs_class_variable(cctx, lhs, defcl, is_decl)
2035 == FAIL)
2036 return FAIL;
2037 }
2038 else
2039 {
2040 if (compile_lhs_script_var(cctx, lhs, var_start, var_end,
2041 is_decl) == FAIL)
2042 return FAIL;
2043 }
2044 }
2045
2046 if (declare_error)
2047 {
2048 vim9_declare_error(lhs->lhs_name);
2049 return FAIL;
2050 }
2051
2052 return OK;
2053}
2054
2055/*
2056 * When compiling a LHS variable name, for a class or an object, set the LHS
2057 * member type.
2058 */
2059 static int
2060compile_lhs_set_oc_member_type(
2061 cctx_T *cctx,
2062 lhs_T *lhs,
2063 char_u *var_start)
2064{
2065 class_T *cl = lhs->lhs_type->tt_class;
2066 int is_object = lhs->lhs_type->tt_type == VAR_OBJECT;
2067 char_u *name = var_start + lhs->lhs_varlen + 1;
2068 size_t namelen = lhs->lhs_end - var_start - lhs->lhs_varlen - 1;
2069
2070 ocmember_T *m = member_lookup(cl, lhs->lhs_type->tt_type,
2071 name, namelen, &lhs->lhs_member_idx);
2072 if (m == NULL)
2073 {
2074 member_not_found_msg(cl, lhs->lhs_type->tt_type, name, namelen);
2075 return FAIL;
2076 }
2077
2078 if (IS_ENUM(cl))
2079 {
2080 if (!inside_class(cctx, cl))
2081 {
2082 semsg(_(e_enumvalue_str_cannot_be_modified),
2083 cl->class_name, m->ocm_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002084 return FAIL;
2085 }
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002086 if (lhs->lhs_type->tt_type == VAR_OBJECT &&
2087 lhs->lhs_member_idx < 2)
2088 {
2089 char *msg = lhs->lhs_member_idx == 0 ?
2090 e_enum_str_name_cannot_be_modified :
2091 e_enum_str_ordinal_cannot_be_modified;
2092 semsg(_(msg), cl->class_name);
2093 return FAIL;
2094 }
2095 }
2096
2097 // If it is private member variable, then accessing it outside the
2098 // class is not allowed.
2099 // If it is a read only class variable, then it can be modified
2100 // only inside the class where it is defined.
2101 if ((m->ocm_access != VIM_ACCESS_ALL) &&
2102 ((is_object && !inside_class(cctx, cl))
2103 || (!is_object && cctx->ctx_ufunc->uf_class != cl)))
2104 {
2105 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
2106 ? e_cannot_access_protected_variable_str
2107 : e_variable_is_not_writable_str;
2108 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
2109 return FAIL;
2110 }
2111
2112 if (!IS_CONSTRUCTOR_METHOD(cctx->ctx_ufunc)
2113 && oc_var_check_ro(cl, m))
2114 return FAIL;
2115
2116 lhs->lhs_member_type = m->ocm_type;
2117
2118 return OK;
2119}
2120
2121/*
2122 * When compiling a LHS variable, set the LHS variable type.
2123 */
2124 static int
2125compile_lhs_set_type(cctx_T *cctx, lhs_T *lhs, char_u *var_end, int is_decl)
2126{
2127 if (is_decl && *skipwhite(var_end) == ':')
2128 {
2129 char_u *p;
2130
2131 // parse optional type: "let var: type = expr"
2132 if (VIM_ISWHITE(*var_end))
2133 {
2134 semsg(_(e_no_white_space_allowed_before_colon_str), var_end);
2135 return FAIL;
2136 }
2137
2138 if (!VIM_ISWHITE(var_end[1]))
2139 {
2140 semsg(_(e_white_space_required_after_str_str), ":", var_end);
2141 return FAIL;
2142 }
2143
2144 p = skipwhite(var_end + 1);
2145 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
2146 if (lhs->lhs_type == NULL)
2147 return FAIL;
2148
2149 lhs->lhs_has_type = TRUE;
2150 lhs->lhs_end = p;
2151 }
2152 else if (lhs->lhs_lvar != NULL)
2153 lhs->lhs_type = lhs->lhs_lvar->lv_type;
2154
2155 return OK;
2156}
2157
2158/*
2159 * Returns TRUE if "lhs" is a concatenable string.
2160 */
2161 static int
2162lhs_concatenable(lhs_T *lhs)
2163{
2164 return lhs->lhs_dest == dest_global
2165 || lhs->lhs_has_index
2166 || lhs->lhs_type->tt_type == VAR_STRING
2167 || lhs->lhs_type->tt_type == VAR_ANY;
2168}
2169
2170/*
2171 * Create a new local variable when compiling a LHS variable.
2172 */
2173 static int
2174compile_lhs_new_local_var(
2175 cctx_T *cctx,
2176 lhs_T *lhs,
2177 char_u *var_start,
2178 int cmdidx,
2179 int oplen,
2180 int is_decl,
2181 int has_cmd,
2182 int heredoc)
2183{
2184 if (oplen > 1 && !heredoc)
2185 {
2186 // +=, /=, etc. require an existing variable
2187 semsg(_(e_cannot_use_operator_on_new_variable_str), lhs->lhs_name);
2188 return FAIL;
2189 }
2190
2191 if (!is_decl || (lhs->lhs_has_index && !has_cmd
2192 && cctx->ctx_skip != SKIP_YES))
2193 {
2194 semsg(_(e_unknown_variable_str), lhs->lhs_name);
2195 return FAIL;
2196 }
2197
2198 // Check the name is valid for a funcref.
2199 if (lhs->lhs_type->tt_type == VAR_FUNC
2200 || lhs->lhs_type->tt_type == VAR_PARTIAL)
2201 {
2202 if (var_wrong_func_name(lhs->lhs_name, TRUE))
2203 return FAIL;
2204 }
2205
2206 // New local variable.
2207 int assign;
2208 switch (cmdidx)
2209 {
2210 case CMD_final:
2211 assign = ASSIGN_FINAL; break;
2212 case CMD_const:
2213 assign = ASSIGN_CONST; break;
2214 default:
2215 assign = ASSIGN_VAR; break;
2216 }
2217
2218 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, assign,
2219 lhs->lhs_type);
2220 if (lhs->lhs_lvar == NULL)
2221 return FAIL;
2222
2223 lhs->lhs_new_local = TRUE;
2224
2225 return OK;
2226}
2227
2228/*
2229 * When compiling a LHS variable name, set the LHS member type.
2230 */
2231 static int
2232compile_lhs_set_member_type(
2233 cctx_T *cctx,
2234 lhs_T *lhs,
2235 char_u *var_start,
2236 int is_decl,
2237 int has_cmd)
2238{
2239 lhs->lhs_member_type = lhs->lhs_type;
2240
2241 if (!lhs->lhs_has_index)
2242 return OK;
2243
2244 char_u *after = var_start + lhs->lhs_varlen;
2245 char_u *p;
2246
2247 // Something follows after the variable: "var[idx]" or "var.key".
2248 if (is_decl && cctx->ctx_skip != SKIP_YES)
2249 {
2250 if (has_cmd)
2251 emsg(_(e_cannot_use_index_when_declaring_variable));
2252 else
2253 semsg(_(e_unknown_variable_str), lhs->lhs_name);
2254 return FAIL;
2255 }
2256
2257 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
2258 // Only the last index is used below, if there are others
2259 // before it generate code for the expression. Thus for
2260 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
2261 for (;;)
2262 {
2263 p = skip_index(after);
2264 if (*p != '[' && *p != '.')
2265 {
2266 lhs->lhs_varlen_total = p - var_start;
2267 break;
2268 }
2269 after = p;
2270 }
2271 if (after > var_start + lhs->lhs_varlen)
2272 {
2273 lhs->lhs_varlen = after - var_start;
2274 lhs->lhs_dest = dest_expr;
2275 // We don't know the type before evaluating the expression,
2276 // use "any" until then.
2277 lhs->lhs_type = &t_any;
2278 }
2279
2280 int use_class = lhs->lhs_type != NULL
2281 && (lhs->lhs_type->tt_type == VAR_CLASS
2282 || lhs->lhs_type->tt_type == VAR_OBJECT);
2283
2284 if (lhs->lhs_type == NULL
2285 || (use_class ? lhs->lhs_type->tt_class == NULL
2286 : lhs->lhs_type->tt_member == NULL))
2287 {
2288 lhs->lhs_member_type = &t_any;
2289 }
2290 else if (use_class)
2291 {
2292 // for an object or class member get the type of the member
2293 if (compile_lhs_set_oc_member_type(cctx, lhs, var_start) == FAIL)
2294 return FAIL;
2295 }
2296 else
2297 lhs->lhs_member_type = lhs->lhs_type->tt_member;
2298
2299 return OK;
2300}
2301
2302/*
2303 * Figure out the LHS type and other properties for an assignment or one item
2304 * of ":unlet" with an index.
2305 * Returns OK or FAIL.
2306 */
2307 int
2308compile_lhs(
2309 char_u *var_start,
2310 lhs_T *lhs,
2311 cmdidx_T cmdidx,
2312 int heredoc,
2313 int has_cmd, // "var" before "var_start"
2314 int oplen,
2315 cctx_T *cctx)
2316{
Yegappan Lakshmanan59c88802024-12-19 20:00:31 +01002317 char_u *var_end = NULL;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002318 int is_decl = is_decl_command(cmdidx);
2319
2320 if (lhs_init(lhs, var_start, is_decl, heredoc, &var_end) == FAIL)
2321 return FAIL;
2322
2323 if (cctx->ctx_skip != SKIP_YES)
2324 {
2325 // compile the LHS destination
2326 if (compile_lhs_var_dest(cctx, lhs, cmdidx, var_start, var_end,
2327 is_decl) == FAIL)
2328 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002329 }
2330
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00002331 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01002332 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
2333 var_end = lhs->lhs_dest_end;
2334
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002335 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002336 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002337 // set the LHS variable type
2338 if (compile_lhs_set_type(cctx, lhs, var_end, is_decl) == FAIL)
2339 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002340 }
2341
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002342 if (oplen == 3 && !heredoc && !lhs_concatenable(lhs))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002343 {
2344 emsg(_(e_can_only_concatenate_to_string));
2345 return FAIL;
2346 }
2347
2348 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002349 && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002350 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002351 if (compile_lhs_new_local_var(cctx, lhs, var_start, cmdidx, oplen,
2352 is_decl, has_cmd, heredoc) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002353 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002354 }
2355
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002356 if (compile_lhs_set_member_type(cctx, lhs, var_start, is_decl, has_cmd)
2357 == FAIL)
2358 return FAIL;
Bram Moolenaarc750d912021-11-29 22:02:12 +00002359
Bram Moolenaar752fc692021-01-04 21:57:11 +01002360 return OK;
2361}
2362
2363/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002364 * Figure out the LHS and check a few errors.
2365 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002366 int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002367compile_assign_lhs(
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002368 char_u *var_start,
2369 lhs_T *lhs,
2370 cmdidx_T cmdidx,
2371 int is_decl,
2372 int heredoc,
2373 int has_cmd, // "var" before "var_start"
2374 int oplen,
2375 cctx_T *cctx)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002376{
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002377 if (compile_lhs(var_start, lhs, cmdidx, heredoc, has_cmd, oplen, cctx)
2378 == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002379 return FAIL;
2380
2381 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
2382 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002383 semsg(_(e_cannot_assign_to_argument_str), lhs->lhs_name);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002384 return FAIL;
2385 }
2386 if (!is_decl && lhs->lhs_lvar != NULL
Bram Moolenaar6586a012022-09-30 11:04:50 +01002387 && lhs->lhs_lvar->lv_const != ASSIGN_VAR
2388 && !lhs->lhs_has_index)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002389 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002390 semsg(_(e_cannot_assign_to_constant_str), lhs->lhs_name);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002391 return FAIL;
2392 }
2393 return OK;
2394}
2395
2396/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002397 * Return TRUE if "lhs" has a range index: "[expr : expr]".
2398 */
2399 static int
2400has_list_index(char_u *idx_start, cctx_T *cctx)
2401{
2402 char_u *p = idx_start;
2403 int save_skip;
2404
2405 if (*p != '[')
2406 return FALSE;
2407
2408 p = skipwhite(p + 1);
2409 if (*p == ':')
2410 return TRUE;
2411
2412 save_skip = cctx->ctx_skip;
2413 cctx->ctx_skip = SKIP_YES;
2414 (void)compile_expr0(&p, cctx);
2415 cctx->ctx_skip = save_skip;
2416 return *skipwhite(p) == ':';
2417}
2418
2419/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002420 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
2421 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01002422 */
2423 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02002424compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01002425 char_u *var_start,
2426 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002427 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01002428 cctx_T *cctx)
2429{
Bram Moolenaar752fc692021-01-04 21:57:11 +01002430 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002431 char_u *p;
2432 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02002433 int need_white_before = TRUE;
2434 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002435
Bram Moolenaar752fc692021-01-04 21:57:11 +01002436 p = var_start + varlen;
2437 if (*p == '[')
2438 {
2439 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02002440 if (*p == ':')
2441 {
2442 // empty first index, push zero
2443 r = generate_PUSHNR(cctx, 0);
2444 need_white_before = FALSE;
2445 }
2446 else
2447 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002448
2449 if (r == OK && *skipwhite(p) == ':')
2450 {
2451 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02002452 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02002453 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002454 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02002455 empty_second = *skipwhite(p + 1) == ']';
2456 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
2457 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002458 {
2459 semsg(_(e_white_space_required_before_and_after_str_at_str),
2460 ":", p);
2461 return FAIL;
2462 }
2463 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02002464 if (*p == ']')
2465 // empty second index, push "none"
2466 r = generate_PUSHSPEC(cctx, VVAL_NONE);
2467 else
2468 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002469 }
2470
Bram Moolenaar752fc692021-01-04 21:57:11 +01002471 if (r == OK && *skipwhite(p) != ']')
2472 {
2473 // this should not happen
Bram Moolenaare1242042021-12-16 20:56:57 +00002474 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar752fc692021-01-04 21:57:11 +01002475 r = FAIL;
2476 }
2477 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002478 else if (lhs->lhs_member_idx >= 0)
2479 {
2480 // object member index
2481 r = generate_PUSHNR(cctx, lhs->lhs_member_idx);
2482 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002483 else // if (*p == '.')
2484 {
2485 char_u *key_end = to_name_end(p + 1, TRUE);
2486 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
2487
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002488 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002489 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002490 return r;
2491}
2492
2493/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002494 * For a LHS with an index, load the variable to be indexed.
2495 */
2496 static int
2497compile_load_lhs(
2498 lhs_T *lhs,
2499 char_u *var_start,
2500 type_T *rhs_type,
2501 cctx_T *cctx)
2502{
2503 if (lhs->lhs_dest == dest_expr)
2504 {
2505 size_t varlen = lhs->lhs_varlen;
2506 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02002507 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002508 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002509
Bram Moolenaare97976b2021-08-01 13:17:17 +02002510 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
2511 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002512 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002513 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002514 char_u *p = var_start;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002515 res = compile_expr0(&p, cctx);
2516 var_start[varlen] = c;
2517 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
2518 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002519 {
2520 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02002521 if (res != FAIL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002522 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002523 return FAIL;
2524 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002525
Bram Moolenaar078a4612022-01-04 15:17:03 +00002526 lhs->lhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2527 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002528
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002529 if (lhs->lhs_type->tt_type == VAR_CLASS
2530 || lhs->lhs_type->tt_type == VAR_OBJECT)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002531 {
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002532 // Check whether the class or object variable is modifiable
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002533 if (!lhs_class_member_modifiable(lhs, var_start, cctx))
2534 return FAIL;
2535 }
2536
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002537 // Now we can properly check the type. The variable is indexed, thus
2538 // we need the member type. For a class or object we don't know the
2539 // type yet, it depends on what member is used.
Yegappan Lakshmananc229a6a2023-10-26 23:05:07 +02002540 // The top item in the stack is the Dict, followed by the key and then
2541 // the type of the value.
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002542 vartype_T vartype = lhs->lhs_type->tt_type;
2543 type_T *member_type = lhs->lhs_type->tt_member;
2544 if (rhs_type != NULL && member_type != NULL
2545 && vartype != VAR_OBJECT && vartype != VAR_CLASS
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002546 && rhs_type != &t_void
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002547 && need_type(rhs_type, member_type, FALSE,
Yegappan Lakshmananc229a6a2023-10-26 23:05:07 +02002548 -3, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002549 return FAIL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002550
2551 return OK;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002552 }
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002553
2554 return generate_loadvar(cctx, lhs);
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002555}
2556
2557/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002558 * Produce code for loading "lhs" and also take care of an index.
2559 * Return OK/FAIL.
2560 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002561 int
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002562compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
2563{
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002564 if (lhs->lhs_type->tt_type == VAR_OBJECT)
2565 {
Bram Moolenaar22363c62023-04-24 17:15:25 +01002566 // "this.value": load "this" object and get the value at index for an
2567 // object or class member get the type of the member.
2568 // Also for "obj.value".
Yegappan Lakshmananff6f0d52023-12-21 16:46:18 +01002569 char_u *dot = vim_strchr(var_start, '.');
2570 if (dot == NULL)
2571 {
2572 semsg(_(e_missing_dot_after_object_str), lhs->lhs_name);
2573 return FAIL;
2574 }
Bram Moolenaar22363c62023-04-24 17:15:25 +01002575
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002576 class_T *cl = lhs->lhs_type->tt_class;
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002577 type_T *type = oc_member_type(cl, TRUE, dot + 1,
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002578 lhs->lhs_end, &lhs->lhs_member_idx);
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002579 if (lhs->lhs_member_idx < 0)
2580 return FAIL;
2581
Bram Moolenaar22363c62023-04-24 17:15:25 +01002582 if (dot - var_start == 4 && STRNCMP(var_start, "this", 4) == 0)
2583 {
2584 // load "this"
Yegappan Lakshmanand990bf02024-03-22 19:56:17 +01002585 lvar_T *lvar = lhs->lhs_lvar;
2586 int rc;
2587
2588 if (lvar->lv_from_outer > 0)
2589 rc = generate_LOADOUTER(cctx, lvar->lv_idx,
2590 lvar->lv_from_outer, lvar->lv_loop_depth,
2591 lvar->lv_loop_idx, type);
2592 else
2593 rc = generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
2594
2595 if (rc == FAIL)
Bram Moolenaar22363c62023-04-24 17:15:25 +01002596 return FAIL;
2597 }
2598 else
2599 {
2600 // load object variable or argument
2601 if (compile_load_lhs(lhs, var_start, lhs->lhs_type, cctx) == FAIL)
2602 return FAIL;
2603 }
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002604 if (IS_INTERFACE(cl))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002605 return generate_GET_ITF_MEMBER(cctx, cl, lhs->lhs_member_idx, type);
2606 return generate_GET_OBJ_MEMBER(cctx, lhs->lhs_member_idx, type);
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002607 }
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002608 else if (lhs->lhs_type->tt_type == VAR_CLASS)
2609 {
2610 // "<classname>.value": load class variable "classname.value"
Yegappan Lakshmananff6f0d52023-12-21 16:46:18 +01002611 char_u *dot = vim_strchr(var_start, '.');
2612 if (dot == NULL)
2613 {
2614 check_type_is_value(lhs->lhs_type);
2615 return FAIL;
2616 }
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002617
2618 class_T *cl = lhs->lhs_type->tt_class;
2619 ocmember_T *m = class_member_lookup(cl, dot + 1,
2620 lhs->lhs_end - dot - 1,
2621 &lhs->lhs_member_idx);
2622 if (m == NULL)
2623 return FAIL;
2624
2625 return generate_CLASSMEMBER(cctx, TRUE, cl, lhs->lhs_member_idx);
2626 }
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002627
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002628 if (compile_load_lhs(lhs, var_start, NULL, cctx) == FAIL)
2629 return FAIL;
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002630
2631 if (lhs->lhs_has_index)
2632 {
2633 int range = FALSE;
2634
2635 // Get member from list or dict. First compile the
2636 // index value.
2637 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
2638 return FAIL;
2639 if (range)
2640 {
2641 semsg(_(e_cannot_use_range_with_assignment_operator_str),
2642 var_start);
2643 return FAIL;
2644 }
2645
2646 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002647 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002648 return FAIL;
2649 }
2650 return OK;
2651}
2652
2653/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002654 * Assignment to a list or dict member, or ":unlet" for the item, using the
2655 * information in "lhs".
2656 * Returns OK or FAIL.
2657 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002658 int
Bram Moolenaare42939a2021-04-05 17:11:17 +02002659compile_assign_unlet(
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002660 char_u *var_start,
2661 lhs_T *lhs,
2662 int is_assign,
2663 type_T *rhs_type,
2664 cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002665{
Bram Moolenaare42939a2021-04-05 17:11:17 +02002666 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002667 int range = FALSE;
2668
Bram Moolenaar68452172021-04-12 21:21:02 +02002669 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002670 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002671 if (is_assign && range
2672 && lhs->lhs_type->tt_type != VAR_LIST
2673 && lhs->lhs_type != &t_blob
2674 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02002675 {
2676 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
2677 return FAIL;
2678 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002679
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002680 if (lhs->lhs_type == NULL || lhs->lhs_type == &t_any)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002681 {
2682 // Index on variable of unknown type: check at runtime.
2683 dest_type = VAR_ANY;
2684 }
2685 else
2686 {
2687 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002688 if (dest_type == VAR_DICT && range)
2689 {
zeertzjq276410e2023-05-07 21:59:33 +01002690 emsg(_(e_cannot_use_range_with_dictionary));
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002691 return FAIL;
2692 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002693 if (dest_type == VAR_DICT
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02002694 && may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002695 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002696 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002697 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002698 type_T *type;
2699
2700 if (range)
2701 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00002702 type = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002703 if (need_type(type, &t_number, FALSE,
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002704 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002705 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002706 }
Bram Moolenaar078a4612022-01-04 15:17:03 +00002707 type = get_type_on_stack(cctx, 0);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00002708 if ((dest_type != VAR_BLOB && type->tt_type != VAR_SPECIAL)
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002709 && need_type(type, &t_number, FALSE,
Bram Moolenaar51e93322021-04-17 20:44:56 +02002710 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002711 return FAIL;
2712 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002713 }
2714
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01002715 if (cctx->ctx_skip == SKIP_YES)
2716 return OK;
2717
Bram Moolenaar22363c62023-04-24 17:15:25 +01002718 // Load the dict, list or object. On the stack we then have:
Bram Moolenaar752fc692021-01-04 21:57:11 +01002719 // - value (for assignment, not for :unlet)
2720 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002721 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01002722 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002723 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
2724 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002725
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002726 if (dest_type == VAR_LIST
2727 || dest_type == VAR_DICT
2728 || dest_type == VAR_BLOB
2729 || dest_type == VAR_CLASS
2730 || dest_type == VAR_OBJECT
2731 || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002732 {
2733 if (is_assign)
2734 {
Bram Moolenaar68452172021-04-12 21:21:02 +02002735 if (range)
2736 {
2737 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
2738 return FAIL;
2739 }
2740 else
2741 {
2742 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002743
Bram Moolenaar68452172021-04-12 21:21:02 +02002744 if (isn == NULL)
2745 return FAIL;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002746 isn->isn_arg.storeindex.si_vartype = dest_type;
2747 isn->isn_arg.storeindex.si_class = NULL;
2748
2749 if (dest_type == VAR_OBJECT)
2750 {
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00002751 class_T *cl = lhs->lhs_type->tt_class;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002752
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002753 if (IS_INTERFACE(cl))
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002754 {
2755 // "this.value": load "this" object and get the value
2756 // at index for an object or class member get the type
2757 // of the member
2758 isn->isn_arg.storeindex.si_class = cl;
2759 ++cl->class_refcount;
2760 }
2761 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002762 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002763 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002764 else if (range)
2765 {
2766 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
2767 return FAIL;
2768 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002769 else
2770 {
2771 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
2772 return FAIL;
2773 }
2774 }
2775 else
2776 {
2777 emsg(_(e_indexable_type_required));
2778 return FAIL;
2779 }
2780
2781 return OK;
2782}
2783
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002784/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002785 * Generate an instruction to push the default value for "vartype".
2786 * if "dest_local" is TRUE then for some types no instruction is generated.
2787 * "skip_store" is set to TRUE if no PUSH instruction is generated.
2788 * Returns OK or FAIL.
2789 */
2790 static int
2791push_default_value(
2792 cctx_T *cctx,
2793 vartype_T vartype,
2794 int dest_is_local,
2795 int *skip_store)
2796{
2797 int r = OK;
2798
2799 switch (vartype)
2800 {
2801 case VAR_BOOL:
2802 r = generate_PUSHBOOL(cctx, VVAL_FALSE);
2803 break;
2804 case VAR_FLOAT:
2805 r = generate_PUSHF(cctx, 0.0);
2806 break;
2807 case VAR_STRING:
2808 r = generate_PUSHS(cctx, NULL);
2809 break;
2810 case VAR_BLOB:
2811 r = generate_PUSHBLOB(cctx, blob_alloc());
2812 break;
2813 case VAR_FUNC:
2814 r = generate_PUSHFUNC(cctx, NULL, &t_func_void, TRUE);
2815 break;
2816 case VAR_LIST:
2817 r = generate_NEWLIST(cctx, 0, FALSE);
2818 break;
2819 case VAR_DICT:
2820 r = generate_NEWDICT(cctx, 0, FALSE);
2821 break;
2822 case VAR_JOB:
2823 r = generate_PUSHJOB(cctx);
2824 break;
2825 case VAR_CHANNEL:
2826 r = generate_PUSHCHANNEL(cctx);
2827 break;
Ernie Rael5c018be2023-08-27 18:40:26 +02002828 case VAR_OBJECT:
2829 r = generate_PUSHOBJ(cctx);
2830 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002831 case VAR_NUMBER:
2832 case VAR_UNKNOWN:
2833 case VAR_ANY:
2834 case VAR_PARTIAL:
2835 case VAR_VOID:
2836 case VAR_INSTR:
2837 case VAR_CLASS:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002838 case VAR_TYPEALIAS:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002839 case VAR_SPECIAL: // cannot happen
2840 // This is skipped for local variables, they are always
2841 // initialized to zero. But in a "for" or "while" loop
2842 // the value may have been changed.
2843 if (dest_is_local && !inside_loop_scope(cctx))
2844 *skip_store = TRUE;
2845 else
2846 r = generate_PUSHNR(cctx, 0);
2847 break;
2848 }
2849 return r;
2850}
2851
2852/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002853 * Compile assignment context. Used when compiling an assignment statement.
2854 */
2855typedef struct cac_S cac_T;
2856struct cac_S
2857{
2858 cmdidx_T cac_cmdidx; // assignment command
2859 char_u *cac_nextc; // next character to parse
2860 lhs_T cac_lhs; // lhs of the assignment
2861 type_T *cac_rhs_type; // rhs type of an assignment
2862 char_u *cac_op; // assignment operator
2863 int cac_oplen; // assignment operator length
2864 char_u *cac_var_start; // start of the variable names
2865 char_u *cac_var_end; // end of the variable names
2866 int cac_var_count; // number of variables in assignment
2867 int cac_var_idx; // variable index in a list
2868 int cac_semicolon; // semicolon in [var1, var2; var3]
2869 garray_T *cac_instr;
2870 int cac_instr_count;
2871 int cac_incdec;
2872 int cac_did_generate_slice;
2873 int cac_is_decl;
2874 int cac_is_const;
2875 int cac_start_lnum;
2876 type_T *cac_inferred_type;
2877 int cac_skip_store;
2878};
2879
2880/*
2881 * Initialize the compile assignment context.
2882 */
2883 static void
2884compile_assign_context_init(cac_T *cac, cctx_T *cctx, int cmdidx, char_u *arg)
2885{
2886 CLEAR_FIELD(*cac);
2887 cac->cac_cmdidx = cmdidx;
2888 cac->cac_instr = &cctx->ctx_instr;
2889 cac->cac_rhs_type = &t_any;
2890 cac->cac_is_decl = is_decl_command(cmdidx);
2891 cac->cac_start_lnum = SOURCING_LNUM;
2892 cac->cac_instr_count = -1;
2893 cac->cac_var_end = arg;
2894}
2895
2896/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002897 * Compile an object member variable assignment in the arguments passed to a
2898 * class new() method.
2899 *
2900 * Instruction format:
2901 *
2902 * ifargisset <n> this.<varname> = <value>
2903 *
2904 * where <n> is the index of the default argument.
2905 *
2906 * Generates the ISN_JUMP_IF_ARG_NOT_SET instruction to skip the assignment if
2907 * the value is passed as an argument to the new() method call.
2908 *
2909 * Returns OK on success.
2910 */
2911 static int
2912compile_assign_obj_new_arg(char_u **argp, cctx_T *cctx)
2913{
2914 char_u *arg = *argp;
2915
2916 arg += 11; // skip "ifargisset"
2917 int def_arg_idx = getdigits(&arg);
2918 arg = skipwhite(arg);
2919
2920 // Use a JUMP_IF_ARG_NOT_SET instruction to skip if the value was not
2921 // given and the default value is "v:none".
2922 int stack_offset = STACK_FRAME_SIZE +
2923 (cctx->ctx_ufunc->uf_va_name != NULL ? 1 : 0);
2924 int def_arg_count = cctx->ctx_ufunc->uf_def_args.ga_len;
2925 int arg_offset = def_arg_idx - def_arg_count - stack_offset;
2926
2927 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_NOT_SET,
2928 arg_offset) == FAIL)
2929 return FAIL;
2930
2931 *argp = arg;
2932 return OK;
2933}
2934
2935/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002936 * Translate the increment (++) and decrement (--) operators to the
2937 * corresponding compound operators (+= or -=).
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002938 *
2939 * Returns OK on success and FAIL on syntax error.
2940 */
2941 static int
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002942translate_incdec_op(exarg_T *eap, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002943{
2944 if (VIM_ISWHITE(eap->cmd[2]))
2945 {
2946 semsg(_(e_no_white_space_allowed_after_str_str),
2947 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
2948 return FAIL;
2949 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002950 cac->cac_op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
2951 cac->cac_oplen = 2;
2952 cac->cac_incdec = TRUE;
2953
2954 return OK;
2955}
2956
2957/*
2958 * Process the operator in an assignment statement.
2959 */
2960 static int
2961compile_assign_process_operator(
2962 exarg_T *eap,
2963 char_u *arg,
2964 cac_T *cac,
2965 int *heredoc,
2966 char_u **retstr)
2967{
2968 *retstr = NULL;
2969
2970 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002971 // Change an unary operator to a compound operator
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002972 return translate_incdec_op(eap, cac);
2973
2974 char_u *sp = cac->cac_nextc;
2975 cac->cac_nextc = skipwhite(cac->cac_nextc);
2976 cac->cac_op = cac->cac_nextc;
2977 cac->cac_oplen = assignment_len(cac->cac_nextc, heredoc);
2978
2979 if (cac->cac_var_count > 0 && cac->cac_oplen == 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002980 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002981 // can be something like "[1, 2]->func()"
2982 *retstr = arg;
2983 return FAIL;
2984 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002985
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002986 // need white space before and after the operator
2987 if (cac->cac_oplen > 0 && (!VIM_ISWHITE(*sp)
2988 || !IS_WHITE_OR_NUL(cac->cac_op[cac->cac_oplen])))
2989 {
2990 error_white_both(cac->cac_op, cac->cac_oplen);
2991 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002992 }
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002993
2994 return OK;
2995}
2996
2997/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002998 * Find the start of an assignment statement.
2999 */
3000 static char_u *
3001compile_assign_compute_start(char_u *arg, int var_count)
3002{
3003 if (var_count > 0)
3004 // [var1, var2] = [val1, val2]
3005 // skip over the "["
3006 return skipwhite(arg + 1);
3007
3008 return arg;
3009}
3010
3011/*
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003012 * Parse a heredoc assignment starting at "p". Returns a pointer to the
3013 * beginning of the heredoc content.
3014 */
3015 static char_u *
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003016parse_heredoc_assignment(exarg_T *eap, cctx_T *cctx, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003017{
3018 // [let] varname =<< [trim] {end}
3019 eap->ea_getline = exarg_getline;
3020 eap->cookie = cctx;
3021
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003022 list_T *l = heredoc_get(eap, cac->cac_nextc + 3, FALSE, TRUE);
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003023 if (l == NULL)
3024 return NULL;
3025
3026 list_free(l);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003027 cac->cac_nextc += STRLEN(cac->cac_nextc);
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003028
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003029 return cac->cac_nextc;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003030}
3031
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003032/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003033 * Check the type of a RHS expression in a list assignment statement.
3034 * The RHS expression is already compiled. So the type is on the stack.
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003035 */
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003036 static int
3037compile_assign_list_check_rhs_type(cctx_T *cctx, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003038{
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003039 type_T *stacktype;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003040
3041 stacktype = cctx->ctx_type_stack.ga_len == 0 ? &t_void
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003042 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003043 if (stacktype->tt_type == VAR_VOID)
3044 {
3045 emsg(_(e_cannot_use_void_value));
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003046 return FAIL;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003047 }
3048
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003049 if (need_type(stacktype, &t_list_any, FALSE, -1, 0, cctx,
3050 FALSE, FALSE) == FAIL)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003051 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003052
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003053 if (stacktype->tt_member != NULL)
3054 cac->cac_rhs_type = stacktype->tt_member;
3055
3056 return OK;
3057}
3058
3059/*
3060 * In a list assignment statement, if a constant list was used, check the
3061 * length. Returns OK if the length check succeeds. Returns FAIL otherwise.
3062 */
3063 static int
3064compile_assign_list_check_length(cctx_T *cctx, cac_T *cac)
3065{
3066 int needed_list_len;
3067 int did_check = FALSE;
3068
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003069 needed_list_len = cac->cac_semicolon
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003070 ? cac->cac_var_count - 1
3071 : cac->cac_var_count;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003072 if (cac->cac_instr->ga_len > 0)
3073 {
3074 isn_T *isn = ((isn_T *)cac->cac_instr->ga_data) +
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003075 cac->cac_instr->ga_len - 1;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003076
3077 if (isn->isn_type == ISN_NEWLIST)
3078 {
3079 did_check = TRUE;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003080 if (cac->cac_semicolon ?
3081 isn->isn_arg.number < needed_list_len
3082 : isn->isn_arg.number != needed_list_len)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003083 {
3084 semsg(_(e_expected_nr_items_but_got_nr),
3085 needed_list_len, (int)isn->isn_arg.number);
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003086 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003087 }
3088 }
3089 }
3090
3091 if (!did_check)
3092 generate_CHECKLEN(cctx, needed_list_len, cac->cac_semicolon);
3093
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003094 return OK;
3095}
3096
3097/*
3098 * Evaluate the expression for "[var, var] = expr" assignment.
3099 * A line break may follow the assignment operator "=".
3100 */
3101 static char_u *
3102compile_assign_list_expr(cctx_T *cctx, cac_T *cac)
3103{
3104 char_u *whitep;
3105
3106 whitep = cac->cac_op + cac->cac_oplen;
3107
3108 if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
3109 return NULL;
3110
3111 // compile RHS expression
3112 if (compile_expr0(&cac->cac_nextc, cctx) == FAIL)
3113 return NULL;
3114
3115 if (cctx->ctx_skip == SKIP_YES)
3116 // no need to parse more when skipping
3117 return cac->cac_nextc;
3118
3119 if (compile_assign_list_check_rhs_type(cctx, cac) == FAIL)
3120 return NULL;
3121
3122 // If a constant list was used we can check the length right here.
3123 if (compile_assign_list_check_length(cctx, cac) == FAIL)
3124 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003125
3126 return cac->cac_nextc;
3127}
3128
3129/*
3130 * Find and return the end of a heredoc or a list of variables assignment
3131 * statement. For a single variable assignment statement, returns the current
3132 * end.
3133 * Returns NULL on failure.
3134 */
3135 static char_u *
3136compile_assign_compute_end(
3137 exarg_T *eap,
3138 cctx_T *cctx,
3139 cac_T *cac,
3140 int heredoc)
3141{
3142 if (heredoc)
3143 {
3144 cac->cac_nextc = parse_heredoc_assignment(eap, cctx, cac);
3145 return cac->cac_nextc;
3146 }
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003147
3148 if (cac->cac_var_count > 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003149 {
3150 // for "[var, var] = expr" evaluate the expression. The list of
3151 // variables are processed later.
3152 // A line break may follow the "=".
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003153 cac->cac_nextc = compile_assign_list_expr(cctx, cac);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003154 return cac->cac_nextc;
3155 }
3156
3157 return cac->cac_var_end;
3158}
3159
3160/*
3161 * For "var = expr" evaluate the expression.
3162 */
3163 static int
3164compile_assign_single_eval_expr(cctx_T *cctx, cac_T *cac)
3165{
3166 int ret = OK;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003167 char_u *whitep;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003168 lhs_T *lhs = &cac->cac_lhs;
3169
3170 // Compile the expression.
3171 if (cac->cac_incdec)
3172 return generate_PUSHNR(cctx, 1);
3173
3174 // Temporarily hide the new local variable here, it is
3175 // not available to this expression.
3176 if (lhs->lhs_new_local)
3177 --cctx->ctx_locals.ga_len;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003178 whitep = cac->cac_op + cac->cac_oplen;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003179
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003180 if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003181 {
3182 if (lhs->lhs_new_local)
3183 ++cctx->ctx_locals.ga_len;
3184 return FAIL;
3185 }
3186
3187 ret = compile_expr0_ext(&cac->cac_nextc, cctx, &cac->cac_is_const);
3188 if (lhs->lhs_new_local)
3189 ++cctx->ctx_locals.ga_len;
3190
3191 return ret;
3192}
3193
3194/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003195 * When compiling an assignment, set the LHS type to the RHS type.
3196 */
3197 static int
3198compile_assign_set_lhs_type_from_rhs(
3199 cctx_T *cctx,
3200 cac_T *cac,
3201 lhs_T *lhs,
3202 type_T *rhs_type)
3203{
3204 if (rhs_type->tt_type == VAR_VOID)
3205 {
3206 emsg(_(e_cannot_use_void_value));
3207 return FAIL;
3208 }
3209
3210 type_T *type;
3211
3212 // An empty list or dict has a &t_unknown member, for a variable that
3213 // implies &t_any.
3214 if (rhs_type == &t_list_empty)
3215 type = &t_list_any;
3216 else if (rhs_type == &t_dict_empty)
3217 type = &t_dict_any;
3218 else if (rhs_type == &t_unknown)
3219 type = &t_any;
3220 else
3221 {
3222 type = rhs_type;
3223 cac->cac_inferred_type = rhs_type;
3224 }
3225
3226 set_var_type(lhs->lhs_lvar, type, cctx);
3227
3228 return OK;
3229}
3230
3231/*
3232 * Returns TRUE if the "rhs_type" can be assigned to the "lhs" variable.
3233 * Used when compiling an assignment statement.
3234 */
3235 static int
3236compile_assign_valid_rhs_type(
3237 cctx_T *cctx,
3238 cac_T *cac,
3239 lhs_T *lhs,
3240 type_T *rhs_type)
3241{
3242 type_T *use_type = lhs->lhs_lvar->lv_type;
3243 where_T where = WHERE_INIT;
3244
3245 // Without operator check type here, otherwise below.
3246 // Use the line number of the assignment.
3247 SOURCING_LNUM = cac->cac_start_lnum;
3248 if (cac->cac_var_count > 0)
3249 {
3250 where.wt_index = cac->cac_var_idx + 1;
3251 where.wt_kind = WT_VARIABLE;
3252 }
3253
3254 // If assigning to a list or dict member, use the member type.
3255 // Not for "list[:] =".
3256 if (lhs->lhs_has_index &&
3257 !has_list_index(cac->cac_var_start + lhs->lhs_varlen, cctx))
3258 use_type = lhs->lhs_member_type;
3259
3260 if (need_type_where(rhs_type, use_type, FALSE, -1, where, cctx, FALSE,
3261 cac->cac_is_const) == FAIL)
3262 return FALSE;
3263
3264 return TRUE;
3265}
3266
3267/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003268 * Compare the LHS type with the RHS type in an assignment.
3269 */
3270 static int
3271compile_assign_check_type(cctx_T *cctx, cac_T *cac)
3272{
3273 lhs_T *lhs = &cac->cac_lhs;
3274 type_T *rhs_type;
3275
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003276 rhs_type = cctx->ctx_type_stack.ga_len == 0
3277 ? &t_void
3278 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003279 cac->cac_rhs_type = rhs_type;
3280
3281 if (check_type_is_value(rhs_type) == FAIL)
3282 return FAIL;
3283
3284 if (lhs->lhs_lvar != NULL && (cac->cac_is_decl || !lhs->lhs_has_type))
3285 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003286 if (rhs_type->tt_type == VAR_FUNC
3287 || rhs_type->tt_type == VAR_PARTIAL)
3288 {
3289 // Make sure the variable name can be used as a funcref
3290 if (!lhs->lhs_has_index
3291 && var_wrong_func_name(lhs->lhs_name, TRUE))
3292 return FAIL;
3293 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003294
3295 if (lhs->lhs_new_local && !lhs->lhs_has_type)
3296 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003297 // The LHS variable doesn't have a type. Set it to the RHS type.
3298 if (compile_assign_set_lhs_type_from_rhs(cctx, cac, lhs, rhs_type)
3299 == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003300 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003301 }
3302 else if (*cac->cac_op == '=')
3303 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003304 if (!compile_assign_valid_rhs_type(cctx, cac, lhs, rhs_type))
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003305 return FAIL;
3306 }
3307 }
3308 else
3309 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003310 // Assigning to a register using @r = "abc"
3311
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003312 type_T *lhs_type = lhs->lhs_member_type;
3313
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003314 // Special case: assigning to @# can use a number or a string.
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003315 // Also: can assign a number to a float.
3316 if ((lhs_type == &t_number_or_string || lhs_type == &t_float)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003317 && rhs_type->tt_type == VAR_NUMBER)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003318 lhs_type = &t_number;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003319
3320 if (*cac->cac_nextc != '=')
3321 {
3322 if (need_type(rhs_type, lhs_type, FALSE, -1, 0, cctx, FALSE,
3323 FALSE) == FAIL)
3324 return FAIL;
3325 }
3326 }
3327
3328 return OK;
3329}
3330
3331/*
3332 * Compile the RHS expression in an assignment statement and generate the
3333 * instructions.
3334 */
3335 static int
3336compile_assign_rhs_expr(cctx_T *cctx, cac_T *cac)
3337{
3338 cac->cac_is_const = FALSE;
3339
3340 // for "+=", "*=", "..=" etc. first load the current value
3341 if (*cac->cac_op != '='
3342 && compile_load_lhs_with_index(&cac->cac_lhs, cac->cac_var_start,
3343 cctx) == FAIL)
3344 return FAIL;
3345
3346 // For "var = expr" evaluate the expression.
3347 if (cac->cac_var_count == 0)
3348 {
3349 int ret;
3350
3351 // Compile the expression.
3352 cac->cac_instr_count = cac->cac_instr->ga_len;
3353 ret = compile_assign_single_eval_expr(cctx, cac);
3354 if (ret == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003355 return FAIL;
3356 }
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003357 else if (cac->cac_semicolon && cac->cac_var_idx == cac->cac_var_count - 1)
3358 {
3359 // For "[var; var] = expr" get the rest of the list
3360 cac->cac_did_generate_slice = TRUE;
3361 if (generate_SLICE(cctx, cac->cac_var_count - 1) == FAIL)
3362 return FAIL;
3363 }
3364 else
3365 {
3366 // For "[var, var] = expr" get the "var_idx" item from the
3367 // list.
3368 int with_op = *cac->cac_op != '=';
3369 if (generate_GETITEM(cctx, cac->cac_var_idx, with_op) == FAIL)
3370 return FAIL;
3371 }
3372
3373 if (compile_assign_check_type(cctx, cac) == FAIL)
3374 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003375
3376 return OK;
3377}
3378
3379/*
3380 * Compile the RHS expression in an assignment
3381 */
3382 static int
3383compile_assign_rhs(cctx_T *cctx, cac_T *cac)
3384{
3385 lhs_T *lhs = &cac->cac_lhs;
3386
3387 if (cctx->ctx_skip == SKIP_YES)
3388 {
3389 if (cac->cac_oplen > 0 && cac->cac_var_count == 0)
3390 {
3391 // skip over the "=" and the expression
3392 cac->cac_nextc = skipwhite(cac->cac_op + cac->cac_oplen);
3393 (void)compile_expr0(&cac->cac_nextc, cctx);
3394 }
3395 return OK;
3396 }
3397
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003398 // If RHS is specified, then generate instructions for RHS expression
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003399 if (cac->cac_oplen > 0)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003400 return compile_assign_rhs_expr(cctx, cac);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003401
3402 if (cac->cac_cmdidx == CMD_final)
3403 {
3404 emsg(_(e_final_requires_a_value));
3405 return FAIL;
3406 }
3407
3408 if (cac->cac_cmdidx == CMD_const)
3409 {
3410 emsg(_(e_const_requires_a_value));
3411 return FAIL;
3412 }
3413
3414 if (!lhs->lhs_has_type || lhs->lhs_dest == dest_option
3415 || lhs->lhs_dest == dest_func_option)
3416 {
3417 emsg(_(e_type_or_initialization_required));
3418 return FAIL;
3419 }
3420
3421 // variables are always initialized
3422 if (GA_GROW_FAILS(cac->cac_instr, 1))
3423 return FAIL;
3424
3425 cac->cac_instr_count = cac->cac_instr->ga_len;
3426
3427 return push_default_value(cctx, lhs->lhs_member_type->tt_type,
3428 lhs->lhs_dest == dest_local,
3429 &cac->cac_skip_store);
3430}
3431
3432/*
3433 * Compile a compound op assignment statement (+=, -=, *=, %=, etc.)
3434 */
3435 static int
3436compile_assign_compound_op(cctx_T *cctx, cac_T *cac)
3437{
3438 lhs_T *lhs = &cac->cac_lhs;
3439 type_T *expected;
3440 type_T *stacktype = NULL;
3441
3442 if (*cac->cac_op == '.')
3443 {
3444 if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
3445 return FAIL;
3446 }
3447 else
3448 {
3449 expected = lhs->lhs_member_type;
3450 stacktype = get_type_on_stack(cctx, 0);
3451 if (
3452 // If variable is float operation with number is OK.
3453 !(expected == &t_float && (stacktype == &t_number
3454 || stacktype == &t_number_bool))
3455 && need_type(stacktype, expected, TRUE, -1, 0, cctx,
3456 FALSE, FALSE) == FAIL)
3457 return FAIL;
3458 }
3459
3460 if (*cac->cac_op == '.')
3461 {
3462 if (generate_CONCAT(cctx, 2) == FAIL)
3463 return FAIL;
3464 }
3465 else if (*cac->cac_op == '+')
3466 {
3467 if (generate_add_instr(cctx,
3468 operator_type(lhs->lhs_member_type, stacktype),
3469 lhs->lhs_member_type, stacktype,
3470 EXPR_APPEND) == FAIL)
3471 return FAIL;
3472 }
3473 else if (generate_two_op(cctx, cac->cac_op) == FAIL)
3474 return FAIL;
3475
3476 return OK;
3477}
3478
3479/*
3480 * Generate the STORE and SETTYPE instructions for an assignment statement.
3481 */
3482 static int
3483compile_assign_generate_store(cctx_T *cctx, cac_T *cac)
3484{
3485 lhs_T *lhs = &cac->cac_lhs;
3486 int save_lnum;
3487
3488 // Use the line number of the assignment for store instruction.
3489 save_lnum = cctx->ctx_lnum;
3490 cctx->ctx_lnum = cac->cac_start_lnum - 1;
3491
3492 if (lhs->lhs_has_index)
3493 {
3494 // Use the info in "lhs" to store the value at the index in the
3495 // list, dict or object.
3496 if (compile_assign_unlet(cac->cac_var_start, &cac->cac_lhs,
3497 TRUE, cac->cac_rhs_type, cctx) == FAIL)
3498 {
3499 cctx->ctx_lnum = save_lnum;
3500 return FAIL;
3501 }
3502 }
3503 else
3504 {
3505 if (cac->cac_is_decl && cac->cac_cmdidx == CMD_const &&
3506 (lhs->lhs_dest == dest_script
3507 || lhs->lhs_dest == dest_script_v9
3508 || lhs->lhs_dest == dest_global
3509 || lhs->lhs_dest == dest_local))
3510 // ":const var": lock the value, but not referenced variables
3511 generate_LOCKCONST(cctx);
3512
3513 type_T *inferred_type = cac->cac_inferred_type;
3514
3515 if ((lhs->lhs_type->tt_type == VAR_DICT
3516 || lhs->lhs_type->tt_type == VAR_LIST)
3517 && lhs->lhs_type->tt_member != NULL
3518 && lhs->lhs_type->tt_member != &t_any
3519 && lhs->lhs_type->tt_member != &t_unknown)
3520 // Set the type in the list or dict, so that it can be checked,
3521 // also in legacy script.
3522 generate_SETTYPE(cctx, lhs->lhs_type);
3523 else if (inferred_type != NULL
3524 && (inferred_type->tt_type == VAR_DICT
3525 || inferred_type->tt_type == VAR_LIST)
3526 && inferred_type->tt_member != NULL
3527 && inferred_type->tt_member != &t_unknown
3528 && inferred_type->tt_member != &t_any)
3529 // Set the type in the list or dict, so that it can be checked,
3530 // also in legacy script.
3531 generate_SETTYPE(cctx, inferred_type);
3532
3533 if (!cac->cac_skip_store &&
3534 generate_store_lhs(cctx, &cac->cac_lhs,
3535 cac->cac_instr_count,
3536 cac->cac_is_decl) == FAIL)
3537 {
3538 cctx->ctx_lnum = save_lnum;
3539 return FAIL;
3540 }
3541 }
3542
3543 cctx->ctx_lnum = save_lnum;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003544
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003545 return OK;
3546}
3547
3548/*
3549 * Process the variable(s) in an assignment statement
3550 */
3551 static int
3552compile_assign_process_variables(
3553 cctx_T *cctx,
3554 cac_T *cac,
3555 int cmdidx,
3556 int heredoc,
3557 int has_cmd,
3558 int has_argisset_prefix,
3559 int jump_instr_idx)
3560{
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003561 /*
3562 * Loop over variables in "[var, var] = expr".
3563 * For "name = expr" and "var name: type" this is done only once.
3564 */
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003565 for (cac->cac_var_idx = 0; cac->cac_var_idx == 0 ||
3566 cac->cac_var_idx < cac->cac_var_count; cac->cac_var_idx++)
3567 {
3568 if (cac->cac_var_start[0] == '_'
3569 && !eval_isnamec(cac->cac_var_start[1]))
3570 {
3571 // Ignore underscore in "[a, _, b] = list".
3572 if (cac->cac_var_count > 0)
3573 {
3574 cac->cac_var_start = skipwhite(cac->cac_var_start + 2);
3575 continue;
3576 }
3577 emsg(_(e_cannot_use_underscore_here));
3578 return FAIL;
3579 }
3580 vim_free(cac->cac_lhs.lhs_name);
3581
3582 /*
3583 * Figure out the LHS type and other properties.
3584 */
3585 if (compile_assign_lhs(cac->cac_var_start, &cac->cac_lhs, cmdidx,
3586 cac->cac_is_decl, heredoc, has_cmd,
3587 cac->cac_oplen, cctx) == FAIL)
3588 return FAIL;
3589
3590 // Compile the RHS expression
3591 if (heredoc)
3592 {
3593 SOURCING_LNUM = cac->cac_start_lnum;
3594 if (cac->cac_lhs.lhs_has_type
3595 && need_type(&t_list_string, cac->cac_lhs.lhs_type,
3596 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
3597 return FAIL;
3598 }
3599 else
3600 {
3601 if (compile_assign_rhs(cctx, cac) == FAIL)
3602 return FAIL;
3603 if (cac->cac_var_count == 0)
3604 cac->cac_var_end = cac->cac_nextc;
3605 }
3606
3607 // no need to parse more when skipping
3608 if (cctx->ctx_skip == SKIP_YES)
3609 break;
3610
3611 if (cac->cac_oplen > 0 && *cac->cac_op != '=')
3612 {
3613 if (compile_assign_compound_op(cctx, cac) == FAIL)
3614 return FAIL;
3615 }
3616
3617 // generate the store instructions
3618 if (compile_assign_generate_store(cctx, cac) == FAIL)
3619 return FAIL;
3620
3621 if (cac->cac_var_idx + 1 < cac->cac_var_count)
3622 cac->cac_var_start = skipwhite(cac->cac_lhs.lhs_end + 1);
3623
3624 if (has_argisset_prefix)
3625 {
3626 // set instruction index in JUMP_IF_ARG_SET to here
3627 isn_T *isn = ((isn_T *)cac->cac_instr->ga_data) + jump_instr_idx;
3628 isn->isn_arg.jumparg.jump_where = cac->cac_instr->ga_len;
3629 }
3630 }
3631
3632 return OK;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003633}
3634
3635/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003636 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003637 * "let name"
3638 * "var name = expr"
3639 * "final name = expr"
3640 * "const name = expr"
3641 * "name = expr"
3642 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003643 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003644 * Return NULL for an error.
3645 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 */
3647 static char_u *
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003648compile_assignment(
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003649 char_u *arg_start,
3650 exarg_T *eap,
3651 cmdidx_T cmdidx,
3652 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653{
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003654 cac_T cac;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003655 char_u *arg = arg_start;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003656 char_u *retstr = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 int heredoc = FALSE;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003658 int jump_instr_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003660 compile_assign_context_init(&cac, cctx, cmdidx, arg);
3661
3662 jump_instr_idx = cac.cac_instr->ga_len;
3663
3664 // process object variable initialization in a new() constructor method
3665 int has_argisset_prefix = STRNCMP(arg, "ifargisset ", 11) == 0;
3666 if (has_argisset_prefix &&
3667 compile_assign_obj_new_arg(&arg, cctx) == FAIL)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003668 goto theend;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003669
Bram Moolenaare1d12112022-03-05 11:37:48 +00003670 // Skip over the "varname" or "[varname, varname]" to get to any "=".
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003671 cac.cac_nextc = skip_var_list(arg, TRUE, &cac.cac_var_count,
3672 &cac.cac_semicolon, TRUE);
3673 if (cac.cac_nextc == NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003674 return *arg == '[' ? arg : NULL;
3675
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003676 if (compile_assign_process_operator(eap, arg, &cac, &heredoc,
3677 &retstr) == FAIL)
3678 return retstr;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003679
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003680 // Compute the start of the assignment
3681 cac.cac_var_start = compile_assign_compute_start(arg, cac.cac_var_count);
3682
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003683 // Compute the end of the assignment
3684 cac.cac_var_end = compile_assign_compute_end(eap, cctx, &cac, heredoc);
3685 if (cac.cac_var_end == NULL)
3686 return NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003687
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003688 int has_cmd = cac.cac_var_start > eap->cmd;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003689
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003690 /* process the variable(s) */
3691 if (compile_assign_process_variables(cctx, &cac, cmdidx, heredoc,
3692 has_cmd, has_argisset_prefix,
3693 jump_instr_idx) == FAIL)
3694 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003695
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02003696 // For "[var, var] = expr" drop the "expr" value.
3697 // Also for "[var, var; _] = expr".
Yegappan Lakshmanane2038412024-12-14 19:59:24 +01003698 if (cctx->ctx_skip != SKIP_YES && cac.cac_var_count > 0 &&
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003699 (!cac.cac_semicolon || !cac.cac_did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02003700 {
Bram Moolenaarec792292020-12-13 21:26:56 +01003701 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02003702 goto theend;
3703 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003704
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003705 retstr = skipwhite(cac.cac_var_end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706
3707theend:
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003708 vim_free(cac.cac_lhs.lhs_name);
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003709 return retstr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710}
3711
3712/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01003713 * Check for an assignment at "eap->cmd", compile it if found.
3714 * Return NOTDONE if there is none, FAIL for failure, OK if done.
3715 */
3716 static int
3717may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
3718{
3719 char_u *pskip;
3720 char_u *p;
3721
3722 // Assuming the command starts with a variable or function name,
3723 // find what follows.
3724 // Skip over "var.member", "var[idx]" and the like.
3725 // Also "&opt = val", "$ENV = val" and "@r = val".
3726 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
3727 ? eap->cmd + 1 : eap->cmd;
3728 p = to_name_end(pskip, TRUE);
3729 if (p > eap->cmd && *p != NUL)
3730 {
3731 char_u *var_end;
3732 int oplen;
3733 int heredoc;
3734
3735 if (eap->cmd[0] == '@')
3736 var_end = eap->cmd + 2;
3737 else
3738 var_end = find_name_end(pskip, NULL, NULL,
3739 FNE_CHECK_START | FNE_INCL_BR);
3740 oplen = assignment_len(skipwhite(var_end), &heredoc);
3741 if (oplen > 0)
3742 {
3743 size_t len = p - eap->cmd;
3744
3745 // Recognize an assignment if we recognize the variable
3746 // name:
Bram Moolenaar17126b12021-01-07 22:03:02 +01003747 // "&opt = expr"
3748 // "$ENV = expr"
3749 // "@r = expr"
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003750 // "g:var = expr"
3751 // "g:[key] = expr"
3752 // "local = expr" where "local" is a local var.
3753 // "script = expr" where "script" is a script-local var.
3754 // "import = expr" where "import" is an imported var
Bram Moolenaar17126b12021-01-07 22:03:02 +01003755 if (*eap->cmd == '&'
3756 || *eap->cmd == '$'
3757 || *eap->cmd == '@'
3758 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003759 || STRNCMP(eap->cmd, "g:[", 3) == 0
Bram Moolenaare0890d62021-02-17 14:52:14 +01003760 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01003761 {
3762 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
3763 if (*line == NULL || *line == eap->cmd)
3764 return FAIL;
3765 return OK;
3766 }
3767 }
3768 }
3769
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003770 // might be "[var, var] = expr" or "ifargisset this.member = expr"
3771 if (*eap->cmd == '[' || STRNCMP(eap->cmd, "ifargisset ", 11) == 0)
Bram Moolenaar17126b12021-01-07 22:03:02 +01003772 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01003773 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
3774 if (*line == NULL)
3775 return FAIL;
3776 if (*line != eap->cmd)
3777 return OK;
3778 }
3779 return NOTDONE;
3780}
3781
Bram Moolenaar9a015112021-12-31 14:06:45 +00003782/*
3783 * Check if arguments of "ufunc" shadow variables in "cctx".
3784 * Return OK or FAIL.
3785 */
3786 static int
3787check_args_shadowing(ufunc_T *ufunc, cctx_T *cctx)
3788{
3789 int i;
3790 char_u *arg;
3791 int r = OK;
3792
3793 // Make sure arguments are not found when compiling a second time.
3794 ufunc->uf_args_visible = 0;
3795
3796 // Check for arguments shadowing variables from the context.
3797 for (i = 0; i < ufunc->uf_args.ga_len; ++i)
3798 {
3799 arg = ((char_u **)(ufunc->uf_args.ga_data))[i];
Bram Moolenaardce24412022-02-08 20:35:30 +00003800 if (check_defined(arg, STRLEN(arg), cctx, NULL, TRUE) == FAIL)
Bram Moolenaar9a015112021-12-31 14:06:45 +00003801 {
3802 r = FAIL;
3803 break;
3804 }
3805 }
3806 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
3807 return r;
3808}
3809
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01003810#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar139575d2022-03-15 19:29:30 +00003811/*
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003812 * Get a count before a command. Can only be a number.
3813 * Returns zero if there is no count.
3814 * Returns -1 if there is something wrong.
3815 */
3816 static long
3817get_cmd_count(char_u *line, exarg_T *eap)
3818{
3819 char_u *p;
3820
3821 // skip over colons and white space
3822 for (p = line; *p == ':' || VIM_ISWHITE(*p); ++p)
3823 ;
Keith Thompson184f71c2024-01-04 21:19:04 +01003824 if (!SAFE_isdigit(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003825 {
Bram Moolenaar2435adf2022-10-21 12:05:46 +01003826 // The command or modifiers must be following. Assume a lower case
3827 // character means there is a modifier.
3828 if (p < eap->cmd && !vim_islower(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003829 {
3830 emsg(_(e_invalid_range));
3831 return -1;
3832 }
3833 return 0;
3834 }
3835 return atol((char *)p);
3836}
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01003837#endif
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003838
3839/*
Bram Moolenaar139575d2022-03-15 19:29:30 +00003840 * Get the compilation type that should be used for "ufunc".
3841 * Keep in sync with INSTRUCTIONS().
3842 */
3843 compiletype_T
3844get_compile_type(ufunc_T *ufunc)
3845{
3846 // Update uf_has_breakpoint if needed.
3847 update_has_breakpoint(ufunc);
3848
3849 if (debug_break_level > 0 || may_break_in_function(ufunc))
3850 return CT_DEBUG;
3851#ifdef FEAT_PROFILE
3852 if (do_profiling == PROF_YES)
3853 {
Ernie Rael21d32122023-09-02 15:09:18 +02003854 if (!ufunc->uf_profiling && has_profiling(FALSE, ufunc->uf_name, NULL,
3855 &ufunc->uf_hash))
Bram Moolenaar139575d2022-03-15 19:29:30 +00003856 func_do_profile(ufunc);
3857 if (ufunc->uf_profiling)
3858 return CT_PROFILE;
3859 }
3860#endif
3861 return CT_NONE;
3862}
3863
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01003864/*
3865 * Free the compiled instructions saved for a def function. This is used when
3866 * compiling a def function and the function was compiled before.
3867 * The index is reused.
3868 */
3869 static void
3870clear_def_function(ufunc_T *ufunc, compiletype_T compile_type)
3871{
3872 isn_T *instr_dest = NULL;
3873 dfunc_T *dfunc;
3874
3875 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3876
3877 switch (compile_type)
3878 {
3879 case CT_PROFILE:
3880#ifdef FEAT_PROFILE
3881 instr_dest = dfunc->df_instr_prof; break;
3882#endif
3883 case CT_NONE: instr_dest = dfunc->df_instr; break;
3884 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
3885 }
3886
3887 if (instr_dest != NULL)
3888 // Was compiled in this mode before: Free old instructions.
3889 delete_def_function_contents(dfunc, FALSE);
3890
3891 ga_clear_strings(&dfunc->df_var_names);
3892 dfunc->df_defer_var_idx = 0;
3893}
Bram Moolenaar7b829262021-10-13 15:04:34 +01003894
3895/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02003896 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003897 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02003898 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02003899 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02003900add_def_function(ufunc_T *ufunc)
3901{
3902 dfunc_T *dfunc;
3903
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003904 if (def_functions.ga_len == 0)
3905 {
3906 // The first position is not used, so that a zero uf_dfunc_idx means it
3907 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02003908 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003909 return FAIL;
3910 ++def_functions.ga_len;
3911 }
3912
Bram Moolenaar09689a02020-05-09 22:50:08 +02003913 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02003914 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02003915 return FAIL;
3916 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
3917 CLEAR_POINTER(dfunc);
3918 dfunc->df_idx = def_functions.ga_len;
3919 ufunc->uf_dfunc_idx = dfunc->df_idx;
3920 dfunc->df_ufunc = ufunc;
John Marriottb32800f2025-02-01 15:25:34 +01003921 dfunc->df_name = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003922 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003923 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02003924 ++def_functions.ga_len;
3925 return OK;
3926}
3927
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01003928 static int
3929compile_dfunc_ufunc_init(
3930 ufunc_T *ufunc,
3931 cctx_T *outer_cctx,
3932 compiletype_T compile_type,
3933 int *new_def_function)
3934{
3935 // When using a function that was compiled before: Free old instructions.
3936 // The index is reused. Otherwise add a new entry in "def_functions".
3937 if (ufunc->uf_dfunc_idx > 0)
3938 clear_def_function(ufunc, compile_type);
3939 else
3940 {
3941 if (add_def_function(ufunc) == FAIL)
3942 return FAIL;
3943
3944 *new_def_function = TRUE;
3945 }
3946
3947 if ((ufunc->uf_flags & FC_CLOSURE) && outer_cctx == NULL)
3948 {
3949 semsg(_(e_compiling_closure_without_context_str),
3950 printable_func_name(ufunc));
3951 return FAIL;
3952 }
3953
3954 ufunc->uf_def_status = UF_COMPILING;
3955
3956 return OK;
3957}
3958
3959/*
3960 * Initialize the compilation context for compiling a def function.
3961 */
3962 static void
3963compile_dfunc_cctx_init(
3964 cctx_T *cctx,
3965 cctx_T *outer_cctx,
3966 ufunc_T *ufunc,
3967 compiletype_T compile_type)
3968{
3969 CLEAR_FIELD(*cctx);
3970
3971 cctx->ctx_compile_type = compile_type;
3972 cctx->ctx_ufunc = ufunc;
3973 cctx->ctx_lnum = -1;
3974 cctx->ctx_outer = outer_cctx;
3975 ga_init2(&cctx->ctx_locals, sizeof(lvar_T), 10);
3976 // Each entry on the type stack consists of two type pointers.
3977 ga_init2(&cctx->ctx_type_stack, sizeof(type2_T), 50);
3978 cctx->ctx_type_list = &ufunc->uf_type_list;
3979 ga_init2(&cctx->ctx_instr, sizeof(isn_T), 50);
3980}
3981
Bram Moolenaar09689a02020-05-09 22:50:08 +02003982/*
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02003983 * For an object constructor, generate instruction to setup "this" (the first
3984 * local variable) and to initialize the object variables.
3985 */
3986 static int
3987obj_constructor_prologue(ufunc_T *ufunc, cctx_T *cctx)
3988{
3989 generate_CONSTRUCT(cctx, ufunc->uf_class);
3990
3991 for (int i = 0; i < ufunc->uf_class->class_obj_member_count; ++i)
3992 {
3993 ocmember_T *m = &ufunc->uf_class->class_obj_members[i];
3994
3995 if (i < 2 && IS_ENUM(ufunc->uf_class))
3996 // The first two object variables in an enum are the name
3997 // and the ordinal. These are set by the ISN_CONSTRUCT
3998 // instruction. So don't generate instructions to set
3999 // these variables.
4000 continue;
4001
4002 if (m->ocm_init != NULL)
4003 {
4004 char_u *expr = m->ocm_init;
4005
4006 if (compile_expr0(&expr, cctx) == FAIL)
4007 return FAIL;
4008
4009 if (!ends_excmd2(m->ocm_init, expr))
4010 {
4011 semsg(_(e_trailing_characters_str), expr);
4012 return FAIL;
4013 }
4014
4015 type_T *type = get_type_on_stack(cctx, 0);
4016 if (m->ocm_type->tt_type == VAR_ANY
4017 && !(m->ocm_flags & OCMFLAG_HAS_TYPE)
4018 && type->tt_type != VAR_SPECIAL)
4019 {
4020 // If the member variable type is not yet set, then use
4021 // the initialization expression type.
4022 m->ocm_type = type;
4023 }
LemonBoyf4af3312024-07-04 13:43:12 +02004024 else
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004025 {
4026 // The type of the member initialization expression is
4027 // determined at run time. Add a runtime type check.
4028 where_T where = WHERE_INIT;
4029 where.wt_kind = WT_MEMBER;
4030 where.wt_func_name = (char *)m->ocm_name;
4031 if (need_type_where(type, m->ocm_type, FALSE, -1,
4032 where, cctx, FALSE, FALSE) == FAIL)
4033 return FAIL;
4034 }
4035 }
4036 else
4037 push_default_value(cctx, m->ocm_type->tt_type, FALSE, NULL);
4038
LemonBoyf4af3312024-07-04 13:43:12 +02004039 if ((m->ocm_type->tt_type == VAR_DICT
4040 || m->ocm_type->tt_type == VAR_LIST)
4041 && m->ocm_type->tt_member != NULL
4042 && m->ocm_type->tt_member != &t_any
4043 && m->ocm_type->tt_member != &t_unknown)
4044 // Set the type in the list or dict, so that it can be checked,
4045 // also in legacy script.
4046 generate_SETTYPE(cctx, m->ocm_type);
4047
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004048 generate_STORE_THIS(cctx, i);
4049 }
4050
4051 return OK;
4052}
4053
4054/*
4055 * For an object method and an constructor, generate instruction to setup
4056 * "this" (the first local variable). For a constructor, generate instructions
4057 * to initialize the object variables.
4058 */
4059 static int
4060obj_method_prologue(ufunc_T *ufunc, cctx_T *cctx)
4061{
4062 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4063
4064 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
4065 return FAIL;
4066
4067 ((char_u **)dfunc->df_var_names.ga_data)[0] =
4068 vim_strsave((char_u *)"this");
4069 ++dfunc->df_var_names.ga_len;
4070
4071 // In the constructor allocate memory for the object and initialize the
4072 // object members.
4073 if (IS_CONSTRUCTOR_METHOD(ufunc))
4074 return obj_constructor_prologue(ufunc, cctx);
4075
4076 return OK;
4077}
4078
4079/*
4080 * Produce instructions for the default values of optional arguments.
4081 */
4082 static int
4083compile_def_function_default_args(
4084 ufunc_T *ufunc,
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004085 garray_T *instr,
4086 cctx_T *cctx)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004087{
4088 int count = ufunc->uf_def_args.ga_len;
4089 int first_def_arg = ufunc->uf_args.ga_len - count;
4090 int i;
4091 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
4092 int did_set_arg_type = FALSE;
4093
4094 // Produce instructions for the default values of optional arguments.
4095 SOURCING_LNUM = 0; // line number unknown
4096 for (i = 0; i < count; ++i)
4097 {
4098 char_u *arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
4099 if (STRCMP(arg, "v:none") == 0)
4100 // "arg = v:none" means the argument is optional without
4101 // setting a value when the argument is missing.
4102 continue;
4103
4104 type_T *val_type;
4105 int arg_idx = first_def_arg + i;
4106 where_T where = WHERE_INIT;
4107 int jump_instr_idx = instr->ga_len;
4108 isn_T *isn;
4109
4110 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
4111 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_SET,
4112 i - count - off) == FAIL)
4113 return FAIL;
4114
4115 // Make sure later arguments are not found.
4116 ufunc->uf_args_visible = arg_idx;
4117
4118 int r = compile_expr0(&arg, cctx);
4119 if (r == FAIL)
4120 return FAIL;
4121
4122 // If no type specified use the type of the default value.
4123 // Otherwise check that the default value type matches the
4124 // specified type.
4125 val_type = get_type_on_stack(cctx, 0);
4126 where.wt_index = arg_idx + 1;
4127 where.wt_kind = WT_ARGUMENT;
4128 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
4129 {
4130 did_set_arg_type = TRUE;
4131 ufunc->uf_arg_types[arg_idx] = val_type;
4132 }
4133 else if (need_type_where(val_type, ufunc->uf_arg_types[arg_idx],
4134 FALSE, -1, where, cctx, FALSE, FALSE) == FAIL)
4135 return FAIL;
4136
4137 if (generate_STORE(cctx, ISN_STORE, i - count - off, NULL) == FAIL)
4138 return FAIL;
4139
4140 // set instruction index in JUMP_IF_ARG_SET to here
4141 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
4142 isn->isn_arg.jumparg.jump_where = instr->ga_len;
4143 }
4144
4145 if (did_set_arg_type)
4146 set_function_type(ufunc);
4147
4148 return OK;
4149}
4150
4151/*
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004152 * Compile def function body. Loop over all the lines in the function and
4153 * generate instructions.
4154 */
4155 static int
4156compile_def_function_body(
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004157 int last_func_lnum,
4158 int check_return_type,
4159 garray_T *lines_to_free,
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004160 char **errormsg,
4161 cctx_T *cctx)
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004162{
4163 char_u *line = NULL;
4164 char_u *p;
4165 int did_emsg_before = did_emsg;
4166#ifdef FEAT_PROFILE
4167 int prof_lnum = -1;
4168#endif
4169 int debug_lnum = -1;
4170
4171 for (;;)
4172 {
4173 exarg_T ea;
4174 int starts_with_colon = FALSE;
4175 char_u *cmd;
4176 cmdmod_T local_cmdmod;
4177
4178 // Bail out on the first error to avoid a flood of errors and report
4179 // the right line number when inside try/catch.
4180 if (did_emsg_before != did_emsg)
4181 return FAIL;
4182
4183 if (line != NULL && *line == '|')
4184 // the line continues after a '|'
4185 ++line;
4186 else if (line != NULL && *skipwhite(line) != NUL
4187 && !(*line == '#' && (line == cctx->ctx_line_start
4188 || VIM_ISWHITE(line[-1]))))
4189 {
4190 semsg(_(e_trailing_characters_str), line);
4191 return FAIL;
4192 }
4193 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
4194 return FAIL;
4195 else
4196 {
4197 line = next_line_from_context(cctx, FALSE);
4198 if (cctx->ctx_lnum >= last_func_lnum)
4199 {
4200 // beyond the last line
4201#ifdef FEAT_PROFILE
4202 if (cctx->ctx_skip != SKIP_YES)
4203 may_generate_prof_end(cctx, prof_lnum);
4204#endif
4205 break;
4206 }
4207 // Make a copy, splitting off nextcmd and removing trailing spaces
4208 // may change it.
4209 if (line != NULL)
4210 {
4211 line = vim_strsave(line);
4212 if (ga_add_string(lines_to_free, line) == FAIL)
4213 return FAIL;
4214 }
4215 }
4216
4217 CLEAR_FIELD(ea);
4218 ea.cmdlinep = &line;
4219 ea.cmd = skipwhite(line);
4220 ea.skip = cctx->ctx_skip == SKIP_YES;
4221
4222 if (*ea.cmd == '#')
4223 {
4224 // "#" starts a comment, but "#{" is an error
4225 if (vim9_bad_comment(ea.cmd))
4226 return FAIL;
4227 line = (char_u *)"";
4228 continue;
4229 }
4230
4231#ifdef FEAT_PROFILE
4232 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_lnum != prof_lnum
4233 && cctx->ctx_skip != SKIP_YES)
4234 {
4235 may_generate_prof_end(cctx, prof_lnum);
4236
4237 prof_lnum = cctx->ctx_lnum;
4238 generate_instr(cctx, ISN_PROF_START);
4239 }
4240#endif
4241 if (cctx->ctx_compile_type == CT_DEBUG && cctx->ctx_lnum != debug_lnum
4242 && cctx->ctx_skip != SKIP_YES)
4243 {
4244 debug_lnum = cctx->ctx_lnum;
4245 generate_instr_debug(cctx);
4246 }
4247 cctx->ctx_prev_lnum = cctx->ctx_lnum + 1;
4248
4249 // Some things can be recognized by the first character.
4250 switch (*ea.cmd)
4251 {
4252 case '}':
4253 {
4254 // "}" ends a block scope
4255 scopetype_T stype = cctx->ctx_scope == NULL
4256 ? NO_SCOPE : cctx->ctx_scope->se_type;
4257
4258 if (stype == BLOCK_SCOPE)
4259 {
4260 compile_endblock(cctx);
4261 line = ea.cmd;
4262 }
4263 else
4264 {
4265 emsg(_(e_using_rcurly_outside_if_block_scope));
4266 return FAIL;
4267 }
4268 if (line != NULL)
4269 line = skipwhite(ea.cmd + 1);
4270 continue;
4271 }
4272
4273 case '{':
4274 // "{" starts a block scope
4275 // "{'a': 1}->func() is something else
4276 if (ends_excmd(*skipwhite(ea.cmd + 1)))
4277 {
4278 line = compile_block(ea.cmd, cctx);
4279 continue;
4280 }
4281 break;
4282 }
4283
4284 /*
4285 * COMMAND MODIFIERS
4286 */
4287 cctx->ctx_has_cmdmod = FALSE;
4288 if (parse_command_modifiers(&ea, errormsg, &local_cmdmod, FALSE)
4289 == FAIL)
4290 return FAIL;
4291 generate_cmdmods(cctx, &local_cmdmod);
4292 undo_cmdmod(&local_cmdmod);
4293
4294 // Check if there was a colon after the last command modifier or before
4295 // the current position.
4296 for (p = ea.cmd; p >= line; --p)
4297 {
4298 if (*p == ':')
4299 starts_with_colon = TRUE;
4300 if (p < ea.cmd && !VIM_ISWHITE(*p))
4301 break;
4302 }
4303
4304 // Skip ":call" to get to the function name, unless using :legacy
4305 p = ea.cmd;
4306 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
4307 {
4308 if (checkforcmd(&ea.cmd, "call", 3))
4309 {
4310 if (*ea.cmd == '(')
4311 // not for "call()"
4312 ea.cmd = p;
4313 else
4314 ea.cmd = skipwhite(ea.cmd);
4315 }
4316
4317 if (!starts_with_colon)
4318 {
4319 int assign;
4320
4321 // Check for assignment after command modifiers.
4322 assign = may_compile_assignment(&ea, &line, cctx);
4323 if (assign == OK)
4324 goto nextline;
4325 if (assign == FAIL)
4326 return FAIL;
4327 }
4328 }
4329
4330 /*
4331 * COMMAND after range
4332 * 'text'->func() should not be confused with 'a mark
4333 * 0z1234->func() should not be confused with a zero line number
4334 * "++nr" and "--nr" are eval commands
4335 * in "$ENV->func()" the "$" is not a range
4336 * "123->func()" is a method call
4337 */
4338 cmd = ea.cmd;
4339 if ((*cmd != '$' || starts_with_colon)
4340 && (starts_with_colon
4341 || !(*cmd == '\''
4342 || (cmd[0] == '0' && cmd[1] == 'z')
4343 || (cmd[0] != NUL && cmd[0] == cmd[1]
4344 && (*cmd == '+' || *cmd == '-'))
4345 || number_method(cmd))))
4346 {
4347 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
4348 if (ea.cmd > cmd)
4349 {
4350 if (!starts_with_colon
4351 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
4352 {
4353 semsg(_(e_colon_required_before_range_str), cmd);
4354 return FAIL;
4355 }
4356 ea.addr_count = 1;
4357 if (ends_excmd2(line, ea.cmd))
4358 {
4359 // A range without a command: jump to the line.
4360 generate_EXEC(cctx, ISN_EXECRANGE,
4361 vim_strnsave(cmd, ea.cmd - cmd));
4362 line = ea.cmd;
4363 goto nextline;
4364 }
4365 }
4366 }
4367 p = find_ex_command(&ea, NULL,
4368 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
4369 ? NULL : item_exists, cctx);
4370
4371 if (p == NULL)
4372 {
4373 if (cctx->ctx_skip != SKIP_YES)
4374 semsg(_(e_ambiguous_use_of_user_defined_command_str), ea.cmd);
4375 return FAIL;
4376 }
4377
4378 // When using ":legacy cmd" always use compile_exec().
4379 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
4380 {
4381 char_u *start = ea.cmd;
4382
4383 switch (ea.cmdidx)
4384 {
4385 case CMD_if:
4386 case CMD_elseif:
4387 case CMD_else:
4388 case CMD_endif:
4389 case CMD_for:
4390 case CMD_endfor:
4391 case CMD_continue:
4392 case CMD_break:
4393 case CMD_while:
4394 case CMD_endwhile:
4395 case CMD_try:
4396 case CMD_catch:
4397 case CMD_finally:
4398 case CMD_endtry:
4399 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
4400 return FAIL;
4401 default: break;
4402 }
4403
4404 // ":legacy return expr" needs to be handled differently.
4405 if (checkforcmd(&start, "return", 4))
4406 ea.cmdidx = CMD_return;
4407 else
4408 ea.cmdidx = CMD_legacy;
4409 }
4410
4411 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
4412 {
4413 // "eval" is used for "val->func()" and "var" for "var = val", then
4414 // "p" is equal to "ea.cmd" for a valid command.
4415 if (ea.cmdidx == CMD_eval || ea.cmdidx == CMD_var)
4416 ;
4417 else if (cctx->ctx_skip == SKIP_YES)
4418 {
4419 line += STRLEN(line);
4420 goto nextline;
4421 }
4422 else
4423 {
4424 semsg(_(e_command_not_recognized_str), ea.cmd);
4425 return FAIL;
4426 }
4427 }
4428
4429 if ((cctx->ctx_had_return || cctx->ctx_had_throw)
4430 && ea.cmdidx != CMD_elseif
4431 && ea.cmdidx != CMD_else
4432 && ea.cmdidx != CMD_endif
4433 && ea.cmdidx != CMD_endfor
4434 && ea.cmdidx != CMD_endwhile
4435 && ea.cmdidx != CMD_catch
4436 && ea.cmdidx != CMD_finally
4437 && ea.cmdidx != CMD_endtry
4438 && !ignore_unreachable_code_for_testing)
4439 {
4440 semsg(_(e_unreachable_code_after_str),
4441 cctx->ctx_had_return ? "return" : "throw");
4442 return FAIL;
4443 }
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004444
4445 // When processing the end of an if-else block, don't clear the
4446 // "ctx_had_throw" flag. If an if-else block ends in a "throw"
4447 // statement, then it is considered to end in a "return" statement.
4448 // The "ctx_had_throw" is cleared immediately after processing the
4449 // if-else block ending statement.
4450 // Otherwise, clear the "had_throw" flag.
4451 if (ea.cmdidx != CMD_else && ea.cmdidx != CMD_elseif
4452 && ea.cmdidx != CMD_endif)
4453 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004454
4455 p = skipwhite(p);
4456 if (ea.cmdidx != CMD_SIZE
4457 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
4458 {
4459 if (ea.cmdidx >= 0)
4460 ea.argt = excmd_get_argt(ea.cmdidx);
4461 if ((ea.argt & EX_BANG) && *p == '!')
4462 {
4463 ea.forceit = TRUE;
4464 p = skipwhite(p + 1);
4465 }
4466 if ((ea.argt & EX_RANGE) == 0 && ea.addr_count > 0)
4467 {
4468 emsg(_(e_no_range_allowed));
4469 return FAIL;
4470 }
4471 }
4472
4473 switch (ea.cmdidx)
4474 {
4475 case CMD_def:
4476 case CMD_function:
4477 ea.arg = p;
4478 line = compile_nested_function(&ea, cctx, lines_to_free);
4479 break;
4480
4481 case CMD_return:
4482 line = compile_return(p, check_return_type,
4483 local_cmdmod.cmod_flags & CMOD_LEGACY, cctx);
4484 cctx->ctx_had_return = TRUE;
4485 break;
4486
4487 case CMD_let:
4488 emsg(_(e_cannot_use_let_in_vim9_script));
4489 break;
4490 case CMD_var:
4491 case CMD_final:
4492 case CMD_const:
4493 case CMD_increment:
4494 case CMD_decrement:
4495 line = compile_assignment(p, &ea, ea.cmdidx, cctx);
4496 if (line == p)
4497 {
4498 emsg(_(e_invalid_assignment));
4499 line = NULL;
4500 }
4501 break;
4502
4503 case CMD_unlet:
4504 case CMD_unlockvar:
4505 case CMD_lockvar:
4506 line = compile_unletlock(p, &ea, cctx);
4507 break;
4508
4509 case CMD_import:
4510 emsg(_(e_import_can_only_be_used_in_script));
4511 line = NULL;
4512 break;
4513
4514 case CMD_if:
4515 line = compile_if(p, cctx);
4516 break;
4517 case CMD_elseif:
4518 line = compile_elseif(p, cctx);
4519 cctx->ctx_had_return = FALSE;
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004520 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004521 break;
4522 case CMD_else:
4523 line = compile_else(p, cctx);
4524 cctx->ctx_had_return = FALSE;
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004525 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004526 break;
4527 case CMD_endif:
4528 line = compile_endif(p, cctx);
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004529 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004530 break;
4531
4532 case CMD_while:
4533 line = compile_while(p, cctx);
4534 break;
4535 case CMD_endwhile:
4536 line = compile_endwhile(p, cctx);
4537 cctx->ctx_had_return = FALSE;
4538 break;
4539
4540 case CMD_for:
4541 line = compile_for(p, cctx);
4542 break;
4543 case CMD_endfor:
4544 line = compile_endfor(p, cctx);
4545 cctx->ctx_had_return = FALSE;
4546 break;
4547 case CMD_continue:
4548 line = compile_continue(p, cctx);
4549 break;
4550 case CMD_break:
4551 line = compile_break(p, cctx);
4552 break;
4553
4554 case CMD_try:
4555 line = compile_try(p, cctx);
4556 break;
4557 case CMD_catch:
4558 line = compile_catch(p, cctx);
4559 cctx->ctx_had_return = FALSE;
4560 break;
4561 case CMD_finally:
4562 line = compile_finally(p, cctx);
4563 cctx->ctx_had_return = FALSE;
4564 break;
4565 case CMD_endtry:
4566 line = compile_endtry(p, cctx);
4567 break;
4568 case CMD_throw:
4569 line = compile_throw(p, cctx);
4570 cctx->ctx_had_throw = TRUE;
4571 break;
4572
4573 case CMD_eval:
4574 line = compile_eval(p, cctx);
4575 break;
4576
4577 case CMD_defer:
4578 line = compile_defer(p, cctx);
4579 break;
4580
4581#ifdef HAS_MESSAGE_WINDOW
4582 case CMD_echowindow:
4583 {
4584 long cmd_count = get_cmd_count(line, &ea);
4585 if (cmd_count < 0)
4586 line = NULL;
4587 else
4588 line = compile_mult_expr(p, ea.cmdidx,
4589 cmd_count, cctx);
4590 }
4591 break;
4592#endif
4593 case CMD_echo:
4594 case CMD_echon:
4595 case CMD_echoconsole:
4596 case CMD_echoerr:
4597 case CMD_echomsg:
4598 case CMD_execute:
4599 line = compile_mult_expr(p, ea.cmdidx, 0, cctx);
4600 break;
4601
4602 case CMD_put:
4603 ea.cmd = cmd;
4604 line = compile_put(p, &ea, cctx);
4605 break;
4606
4607 case CMD_substitute:
4608 if (check_global_and_subst(ea.cmd, p) == FAIL)
4609 return FAIL;
4610 if (cctx->ctx_skip == SKIP_YES)
4611 line = (char_u *)"";
4612 else
4613 {
4614 ea.arg = p;
4615 line = compile_substitute(line, &ea, cctx);
4616 }
4617 break;
4618
4619 case CMD_redir:
4620 ea.arg = p;
4621 line = compile_redir(line, &ea, cctx);
4622 break;
4623
4624 case CMD_cexpr:
4625 case CMD_lexpr:
4626 case CMD_caddexpr:
4627 case CMD_laddexpr:
4628 case CMD_cgetexpr:
4629 case CMD_lgetexpr:
4630#ifdef FEAT_QUICKFIX
4631 ea.arg = p;
4632 line = compile_cexpr(line, &ea, cctx);
4633#else
4634 ex_ni(&ea);
4635 line = NULL;
4636#endif
4637 break;
4638
4639 case CMD_append:
4640 case CMD_change:
4641 case CMD_insert:
4642 case CMD_k:
4643 case CMD_t:
4644 case CMD_xit:
4645 not_in_vim9(&ea);
4646 return FAIL;
4647
4648 case CMD_SIZE:
4649 if (cctx->ctx_skip != SKIP_YES)
4650 {
4651 semsg(_(e_invalid_command_str), ea.cmd);
4652 return FAIL;
4653 }
4654 // We don't check for a next command here.
4655 line = (char_u *)"";
4656 break;
4657
4658 case CMD_lua:
4659 case CMD_mzscheme:
4660 case CMD_perl:
4661 case CMD_py3:
4662 case CMD_python3:
4663 case CMD_python:
4664 case CMD_pythonx:
4665 case CMD_ruby:
4666 case CMD_tcl:
4667 ea.arg = p;
4668 if (vim_strchr(line, '\n') == NULL)
4669 line = compile_exec(line, &ea, cctx);
4670 else
4671 // heredoc lines have been concatenated with NL
4672 // characters in get_function_body()
4673 line = compile_script(line, cctx);
4674 break;
4675
4676 case CMD_vim9script:
4677 if (cctx->ctx_skip != SKIP_YES)
4678 {
4679 emsg(_(e_vim9script_can_only_be_used_in_script));
4680 return FAIL;
4681 }
4682 line = (char_u *)"";
4683 break;
4684
4685 case CMD_class:
4686 emsg(_(e_class_can_only_be_used_in_script));
4687 return FAIL;
4688
4689 case CMD_type:
4690 emsg(_(e_type_can_only_be_used_in_script));
4691 return FAIL;
4692
4693 case CMD_global:
4694 if (check_global_and_subst(ea.cmd, p) == FAIL)
4695 return FAIL;
4696 // FALLTHROUGH
4697 default:
4698 // Not recognized, execute with do_cmdline_cmd().
4699 ea.arg = p;
4700 line = compile_exec(line, &ea, cctx);
4701 break;
4702 }
4703nextline:
4704 if (line == NULL)
4705 return FAIL;
4706 line = skipwhite(line);
4707
4708 // Undo any command modifiers.
4709 generate_undo_cmdmods(cctx);
4710
4711 if (cctx->ctx_type_stack.ga_len < 0)
4712 {
4713 iemsg("Type stack underflow");
4714 return FAIL;
4715 }
4716 } // END of the loop over all the function body lines.
4717
4718 return OK;
4719}
4720
4721/*
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004722 * Returns TRUE if the end of a scope (if, while, for, block) is missing.
4723 * Called after compiling a def function body.
4724 */
4725 static int
4726compile_dfunc_scope_end_missing(cctx_T *cctx)
4727{
4728 if (cctx->ctx_scope == NULL)
4729 return FALSE;
4730
4731 if (cctx->ctx_scope->se_type == IF_SCOPE)
4732 emsg(_(e_missing_endif));
4733 else if (cctx->ctx_scope->se_type == WHILE_SCOPE)
4734 emsg(_(e_missing_endwhile));
4735 else if (cctx->ctx_scope->se_type == FOR_SCOPE)
4736 emsg(_(e_missing_endfor));
4737 else
4738 emsg(_(e_missing_rcurly));
4739
4740 return TRUE;
4741}
4742
4743/*
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004744 * When compiling a def function, if it doesn't have an explicit return
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004745 * statement, then generate a default return instruction. For an object
4746 * constructor, return the object.
4747 */
4748 static int
4749compile_dfunc_generate_default_return(ufunc_T *ufunc, cctx_T *cctx)
4750{
4751 // TODO: if a function ends in "throw" but there was a return elsewhere we
4752 // should not assume the return type is "void".
4753 if (cctx->ctx_had_return || cctx->ctx_had_throw)
4754 return OK;
4755
4756 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
4757 ufunc->uf_ret_type = &t_void;
4758 else if (ufunc->uf_ret_type->tt_type != VAR_VOID
4759 && !IS_CONSTRUCTOR_METHOD(ufunc))
4760 {
4761 emsg(_(e_missing_return_statement));
4762 return FAIL;
4763 }
4764
4765 // Return void if there is no return at the end.
4766 // For a constructor return the object.
4767 if (IS_CONSTRUCTOR_METHOD(ufunc))
4768 {
4769 generate_instr(cctx, ISN_RETURN_OBJECT);
4770 ufunc->uf_ret_type = &ufunc->uf_class->class_object_type;
4771 }
4772 else
4773 generate_instr(cctx, ISN_RETURN_VOID);
4774
4775 return OK;
4776}
4777
4778/*
4779 * Perform the chores after successfully compiling a def function.
4780 */
4781 static void
4782compile_dfunc_epilogue(
4783 cctx_T *outer_cctx,
4784 ufunc_T *ufunc,
4785 garray_T *instr,
4786 cctx_T *cctx)
4787{
4788 dfunc_T *dfunc;
4789
4790 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4791 dfunc->df_deleted = FALSE;
4792 dfunc->df_script_seq = current_sctx.sc_seq;
4793
4794#ifdef FEAT_PROFILE
4795 if (cctx->ctx_compile_type == CT_PROFILE)
4796 {
4797 dfunc->df_instr_prof = instr->ga_data;
4798 dfunc->df_instr_prof_count = instr->ga_len;
4799 }
4800 else
4801#endif
4802 if (cctx->ctx_compile_type == CT_DEBUG)
4803 {
4804 dfunc->df_instr_debug = instr->ga_data;
4805 dfunc->df_instr_debug_count = instr->ga_len;
4806 }
4807 else
4808 {
4809 dfunc->df_instr = instr->ga_data;
4810 dfunc->df_instr_count = instr->ga_len;
4811 }
4812 dfunc->df_varcount = dfunc->df_var_names.ga_len;
4813 dfunc->df_has_closure = cctx->ctx_has_closure;
4814
4815 if (cctx->ctx_outer_used)
4816 {
4817 ufunc->uf_flags |= FC_CLOSURE;
4818 if (outer_cctx != NULL)
4819 ++outer_cctx->ctx_closure_count;
4820 }
4821
4822 ufunc->uf_def_status = UF_COMPILED;
4823}
4824
4825/*
4826 * Perform the cleanup when a def function compilation fails.
4827 */
4828 static void
4829compile_dfunc_ufunc_cleanup(
4830 ufunc_T *ufunc,
4831 garray_T *instr,
4832 int new_def_function,
4833 char *errormsg,
4834 int did_emsg_before,
4835 cctx_T *cctx)
4836{
4837 dfunc_T *dfunc;
4838
4839 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4840
4841 // Compiling aborted, free the generated instructions.
4842 clear_instr_ga(instr);
4843 VIM_CLEAR(dfunc->df_name);
4844 ga_clear_strings(&dfunc->df_var_names);
4845
4846 // If using the last entry in the table and it was added above, we
4847 // might as well remove it.
4848 if (!dfunc->df_deleted && new_def_function
4849 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
4850 {
4851 --def_functions.ga_len;
4852 ufunc->uf_dfunc_idx = 0;
4853 }
4854 ufunc->uf_def_status = UF_COMPILE_ERROR;
4855
4856 while (cctx->ctx_scope != NULL)
4857 drop_scope(cctx);
4858
4859 if (errormsg != NULL)
4860 emsg(errormsg);
4861 else if (did_emsg == did_emsg_before)
4862 emsg(_(e_compiling_def_function_failed));
4863}
4864
4865/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 * After ex_function() has collected all the function lines: parse and compile
4867 * the lines into instructions.
4868 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004869 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
4870 * the return statement (used for lambda). When uf_ret_type is already set
4871 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01004872 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004873 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02004874 * This can be used recursively through compile_lambda(), which may reallocate
4875 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02004876 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02004878 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01004879compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02004880 ufunc_T *ufunc,
4881 int check_return_type,
4882 compiletype_T compile_type,
4883 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004885 garray_T lines_to_free;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 cctx_T cctx;
4888 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01004889 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02004890 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891 int ret = FAIL;
4892 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004893 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004894 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004895 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02004896 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004898 // allocated lines are freed at the end
4899 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4900
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004901 // Initialize the ufunc and the compilation context
4902 if (compile_dfunc_ufunc_init(ufunc, outer_cctx, compile_type,
4903 &new_def_function) == FAIL)
Bram Moolenaar96923b72022-03-15 15:57:04 +00004904 return FAIL;
Bram Moolenaar96923b72022-03-15 15:57:04 +00004905
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004906 compile_dfunc_cctx_init(&cctx, outer_cctx, ufunc, compile_type);
Bram Moolenaar985116a2020-07-12 17:31:09 +02004907
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 instr = &cctx.ctx_instr;
4909
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004910 // Set the context to the function, it may be compiled when called from
4911 // another script. Set the script version to the most modern one.
4912 // The line number will be set in next_line_from_context().
4913 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4915
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004916 // Don't use the flag from ":legacy" here.
4917 cmdmod.cmod_flags &= ~CMOD_LEGACY;
4918
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004919 // Make sure error messages are OK.
4920 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
4921 if (do_estack_push)
4922 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004923 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004924
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004925 // Make sure arguments don't shadow variables in the context
Bram Moolenaar9a015112021-12-31 14:06:45 +00004926 if (check_args_shadowing(ufunc, &cctx) == FAIL)
4927 goto erret;
4928
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004929 // For an object method and a constructor generate instructions to
4930 // initialize "this" and the object variables.
Bram Moolenaar574950d2023-01-03 19:08:50 +00004931 if (ufunc->uf_flags & (FC_OBJECT|FC_NEW))
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004932 if (obj_method_prologue(ufunc, &cctx) == FAIL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004933 goto erret;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004934
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004935 if (ufunc->uf_def_args.ga_len > 0)
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004936 if (compile_def_function_default_args(ufunc, instr, &cctx) == FAIL)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004937 goto erret;
Bram Moolenaare28d9b32021-07-03 18:56:53 +02004938 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004939
Ernie Rael7c92e882025-01-18 17:26:39 +01004940 // Compiling an abstract method or a function in an interface is done to
4941 // get the function type. No code is actually compiled.
4942 if (ufunc->uf_class != NULL && (IS_INTERFACE(ufunc->uf_class)
4943 || IS_ABSTRACT_METHOD(ufunc)))
Bram Moolenaarb8bebd02023-01-30 20:24:23 +00004944 {
4945 ufunc->uf_def_status = UF_NOT_COMPILED;
4946 ret = OK;
4947 goto erret;
4948 }
4949
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004950 // compile the function body
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004951 if (compile_def_function_body(ufunc->uf_lines.ga_len, check_return_type,
4952 &lines_to_free, &errormsg, &cctx) == FAIL)
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004953 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004955 if (compile_dfunc_scope_end_missing(&cctx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004958 if (compile_dfunc_generate_default_return(ufunc, &cctx) == FAIL)
4959 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960
Bram Moolenaar599410c2021-04-10 14:03:43 +02004961 // When compiled with ":silent!" and there was an error don't consider the
4962 // function compiled.
4963 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004964 compile_dfunc_epilogue(outer_cctx, ufunc, instr, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965
4966 ret = OK;
4967
4968erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02004969 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 {
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004971 // compilation failed. do cleanup.
4972 compile_dfunc_ufunc_cleanup(ufunc, instr, new_def_function,
4973 errormsg, did_emsg_before, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 }
4975
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004976 if (cctx.ctx_redir_lhs.lhs_name != NULL)
4977 {
4978 if (ret == OK)
4979 {
4980 emsg(_(e_missing_redir_end));
4981 ret = FAIL;
4982 }
4983 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02004984 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004985 }
4986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004988 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004989 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004990 if (do_estack_push)
4991 estack_pop();
4992
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004993 ga_clear_strings(&lines_to_free);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004994 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004996 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997}
4998
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004999 void
5000set_function_type(ufunc_T *ufunc)
5001{
5002 int varargs = ufunc->uf_va_name != NULL;
5003 int argcount = ufunc->uf_args.ga_len;
5004
5005 // Create a type for the function, with the return type and any
5006 // argument types.
5007 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5008 // The type is included in "tt_args".
5009 if (argcount > 0 || varargs)
5010 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01005011 if (ufunc->uf_type_list.ga_itemsize == 0)
5012 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005013 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
5014 argcount, &ufunc->uf_type_list);
5015 // Add argument types to the function type.
5016 if (func_type_add_arg_types(ufunc->uf_func_type,
5017 argcount + varargs,
5018 &ufunc->uf_type_list) == FAIL)
5019 return;
5020 ufunc->uf_func_type->tt_argcount = argcount + varargs;
5021 ufunc->uf_func_type->tt_min_argcount =
5022 argcount - ufunc->uf_def_args.ga_len;
5023 if (ufunc->uf_arg_types == NULL)
5024 {
5025 int i;
5026
5027 // lambda does not have argument types.
5028 for (i = 0; i < argcount; ++i)
5029 ufunc->uf_func_type->tt_args[i] = &t_any;
5030 }
5031 else
5032 mch_memmove(ufunc->uf_func_type->tt_args,
5033 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
5034 if (varargs)
5035 {
5036 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02005037 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005038 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
5039 }
5040 }
5041 else
5042 // No arguments, can use a predefined type.
5043 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5044 argcount, &ufunc->uf_type_list);
5045}
5046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005048 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01005049 */
5050 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005051delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005052{
5053 int idx;
5054
Bram Moolenaard505d172022-12-18 21:42:55 +00005055 // In same cases the instructions may refer to a class in which the
5056 // function is defined and unreferencing the class may call back here
5057 // recursively. Set the df_delete_busy to avoid problems.
5058 if (dfunc->df_delete_busy)
5059 return;
5060 dfunc->df_delete_busy = TRUE;
5061
Bram Moolenaar20431c92020-03-20 18:39:46 +01005062 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005063 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005064
5065 if (dfunc->df_instr != NULL)
5066 {
5067 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5068 delete_instr(dfunc->df_instr + idx);
5069 VIM_CLEAR(dfunc->df_instr);
5070 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02005071 if (dfunc->df_instr_debug != NULL)
5072 {
5073 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
5074 delete_instr(dfunc->df_instr_debug + idx);
5075 VIM_CLEAR(dfunc->df_instr_debug);
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02005076 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01005077#ifdef FEAT_PROFILE
5078 if (dfunc->df_instr_prof != NULL)
5079 {
5080 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
5081 delete_instr(dfunc->df_instr_prof + idx);
5082 VIM_CLEAR(dfunc->df_instr_prof);
Bram Moolenaarc05fe072021-01-24 21:30:48 +01005083 }
5084#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01005085
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005086 if (mark_deleted)
5087 dfunc->df_deleted = TRUE;
5088 if (dfunc->df_ufunc != NULL)
5089 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaard505d172022-12-18 21:42:55 +00005090
5091 dfunc->df_delete_busy = FALSE;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005092}
5093
5094/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005095 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005096 * function, unless another user function still uses it.
5097 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098 */
5099 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005100unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005102 if (ufunc->uf_dfunc_idx <= 0)
5103 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005105 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5106 + ufunc->uf_dfunc_idx;
5107
5108 if (--dfunc->df_refcount <= 0)
5109 delete_def_function_contents(dfunc, TRUE);
5110 ufunc->uf_def_status = UF_NOT_COMPILED;
5111 ufunc->uf_dfunc_idx = 0;
5112 if (dfunc->df_ufunc == ufunc)
5113 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114}
5115
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005116/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005117 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005118 */
5119 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005120link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005121{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005122 if (ufunc->uf_dfunc_idx <= 0)
5123 return;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005124
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005125 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5126 + ufunc->uf_dfunc_idx;
5127
5128 ++dfunc->df_refcount;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005129}
5130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005132/*
5133 * Free all functions defined with ":def".
5134 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005135 void
5136free_def_functions(void)
5137{
Bram Moolenaar20431c92020-03-20 18:39:46 +01005138 int idx;
5139
5140 for (idx = 0; idx < def_functions.ga_len; ++idx)
5141 {
5142 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
5143
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005144 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005145 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005146 }
5147
5148 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149}
5150#endif
5151
5152
5153#endif // FEAT_EVAL