1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 
10 /*
11  * vim9.h: types and globals used for Vim9 script.
12  */
13 
14 typedef enum {
15     ISN_EXEC,	    // execute Ex command line isn_arg.string
16     ISN_EXECCONCAT, // execute Ex command from isn_arg.number items on stack
17     ISN_EXEC_SPLIT, // execute Ex command from isn_arg.string split at NL
18     ISN_EXECRANGE,  // execute EX command that is only a range
19     ISN_LEGACY_EVAL, // evaluate expression isn_arg.string with legacy syntax.
20     ISN_ECHO,	    // :echo with isn_arg.echo.echo_count items on top of stack
21     ISN_EXECUTE,    // :execute with isn_arg.number items on top of stack
22     ISN_ECHOMSG,    // :echomsg with isn_arg.number items on top of stack
23     ISN_ECHOCONSOLE, // :echoconsole with isn_arg.number items on top of stack
24     ISN_ECHOERR,    // :echoerr with isn_arg.number items on top of stack
25     ISN_RANGE,	    // compute range from isn_arg.string, push to stack
26     ISN_SUBSTITUTE, // :s command with expression
27     ISN_INSTR,	    // instructions compiled from expression
28 
29     // get and set variables
30     ISN_LOAD,	    // push local variable isn_arg.number
31     ISN_LOADV,	    // push v: variable isn_arg.number
32     ISN_LOADG,	    // push g: variable isn_arg.string
33     ISN_LOADAUTO,   // push g: autoload variable isn_arg.string
34     ISN_LOADB,	    // push b: variable isn_arg.string
35     ISN_LOADW,	    // push w: variable isn_arg.string
36     ISN_LOADT,	    // push t: variable isn_arg.string
37     ISN_LOADGDICT,  // push g: dict
38     ISN_LOADBDICT,  // push b: dict
39     ISN_LOADWDICT,  // push w: dict
40     ISN_LOADTDICT,  // push t: dict
41     ISN_LOADS,	    // push s: variable isn_arg.loadstore
42     ISN_LOADOUTER,  // push variable from outer scope isn_arg.outer
43     ISN_LOADSCRIPT, // push script-local variable isn_arg.script.
44     ISN_LOADOPT,    // push option isn_arg.string
45     ISN_LOADENV,    // push environment variable isn_arg.string
46     ISN_LOADREG,    // push register isn_arg.number
47 
48     ISN_STORE,	    // pop into local variable isn_arg.number
49     ISN_STOREV,	    // pop into v: variable isn_arg.number
50     ISN_STOREG,	    // pop into global variable isn_arg.string
51     ISN_STOREAUTO,  // pop into global autoload variable isn_arg.string
52     ISN_STOREB,	    // pop into buffer-local variable isn_arg.string
53     ISN_STOREW,	    // pop into window-local variable isn_arg.string
54     ISN_STORET,	    // pop into tab-local variable isn_arg.string
55     ISN_STORES,	    // pop into script variable isn_arg.loadstore
56     ISN_STOREOUTER,  // pop variable into outer scope isn_arg.outer
57     ISN_STORESCRIPT, // pop into script variable isn_arg.script
58     ISN_STOREOPT,    // pop into option isn_arg.string
59     ISN_STOREENV,    // pop into environment variable isn_arg.string
60     ISN_STOREREG,    // pop into register isn_arg.number
61     // ISN_STOREOTHER, // pop into other script variable isn_arg.other.
62 
63     ISN_STORENR,    // store number into local variable isn_arg.storenr.stnr_idx
64     ISN_STOREINDEX,	// store into list or dictionary, type isn_arg.vartype,
65 			// value/index/variable on stack
66     ISN_STORERANGE,	// store into blob,
67 			// value/index 1/index 2/variable on stack
68 
69     ISN_UNLET,		// unlet variable isn_arg.unlet.ul_name
70     ISN_UNLETENV,	// unlet environment variable isn_arg.unlet.ul_name
71     ISN_UNLETINDEX,	// unlet item of list or dict
72     ISN_UNLETRANGE,	// unlet items of list
73 
74     ISN_LOCKUNLOCK,	// :lock and :unlock for local variable member
75     ISN_LOCKCONST,	// lock constant value
76 
77     // constants
78     ISN_PUSHNR,		// push number isn_arg.number
79     ISN_PUSHBOOL,	// push bool value isn_arg.number
80     ISN_PUSHSPEC,	// push special value isn_arg.number
81     ISN_PUSHF,		// push float isn_arg.fnumber
82     ISN_PUSHS,		// push string isn_arg.string
83     ISN_PUSHBLOB,	// push blob isn_arg.blob
84     ISN_PUSHFUNC,	// push func isn_arg.string
85     ISN_PUSHCHANNEL,	// push channel isn_arg.channel
86     ISN_PUSHJOB,	// push channel isn_arg.job
87     ISN_NEWLIST,	// push list from stack items, size is isn_arg.number
88     ISN_NEWDICT,	// push dict from stack items, size is isn_arg.number
89 
90     // function call
91     ISN_BCALL,	    // call builtin function isn_arg.bfunc
92     ISN_DCALL,	    // call def function isn_arg.dfunc
93     ISN_UCALL,	    // call user function or funcref/partial isn_arg.ufunc
94     ISN_PCALL,	    // call partial, use isn_arg.pfunc
95     ISN_PCALL_END,  // cleanup after ISN_PCALL with cpf_top set
96     ISN_RETURN,	    // return, result is on top of stack
97     ISN_RETURN_VOID, // Push void, then return
98     ISN_FUNCREF,    // push a function ref to dfunc isn_arg.funcref
99     ISN_NEWFUNC,    // create a global function from a lambda function
100     ISN_DEF,	    // list functions
101 
102     // expression operations
103     ISN_JUMP,	    // jump if condition is matched isn_arg.jump
104     ISN_JUMP_IF_ARG_SET, // jump if argument is already set, uses
105 			 // isn_arg.jumparg
106 
107     // loop
108     ISN_FOR,	    // get next item from a list, uses isn_arg.forloop
109 
110     ISN_TRY,	    // add entry to ec_trystack, uses isn_arg.try
111     ISN_THROW,	    // pop value of stack, store in v:exception
112     ISN_PUSHEXC,    // push v:exception
113     ISN_CATCH,	    // drop v:exception
114     ISN_FINALLY,    // start of :finally block
115     ISN_ENDTRY,	    // take entry off from ec_trystack
116     ISN_TRYCONT,    // handle :continue inside a :try statement
117 
118     // more expression operations
119     ISN_ADDLIST,    // add two lists
120     ISN_ADDBLOB,    // add two blobs
121 
122     // operation with two arguments; isn_arg.op.op_type is exprtype_T
123     ISN_OPNR,
124     ISN_OPFLOAT,
125     ISN_OPANY,
126 
127     // comparative operations; isn_arg.op.op_type is exprtype_T, op_ic used
128     ISN_COMPAREBOOL,
129     ISN_COMPARESPECIAL,
130     ISN_COMPARENR,
131     ISN_COMPAREFLOAT,
132     ISN_COMPARESTRING,
133     ISN_COMPAREBLOB,
134     ISN_COMPARELIST,
135     ISN_COMPAREDICT,
136     ISN_COMPAREFUNC,
137     ISN_COMPAREANY,
138 
139     // expression operations
140     ISN_CONCAT,
141     ISN_STRINDEX,   // [expr] string index
142     ISN_STRSLICE,   // [expr:expr] string slice
143     ISN_LISTAPPEND, // append to a list, like add()
144     ISN_LISTINDEX,  // [expr] list index
145     ISN_LISTSLICE,  // [expr:expr] list slice
146     ISN_BLOBINDEX,  // [expr] blob index
147     ISN_BLOBSLICE,  // [expr:expr] blob slice
148     ISN_ANYINDEX,   // [expr] runtime index
149     ISN_ANYSLICE,   // [expr:expr] runtime slice
150     ISN_SLICE,	    // drop isn_arg.number items from start of list
151     ISN_BLOBAPPEND, // append to a blob, like add()
152     ISN_GETITEM,    // push list item, isn_arg.number is the index
153     ISN_MEMBER,	    // dict[member]
154     ISN_STRINGMEMBER, // dict.member using isn_arg.string
155     ISN_2BOOL,	    // falsy/truthy to bool, uses isn_arg.tobool
156     ISN_COND2BOOL,  // convert value to bool
157     ISN_2STRING,    // convert value to string at isn_arg.tostring on stack
158     ISN_2STRING_ANY, // like ISN_2STRING but check type
159     ISN_NEGATENR,   // apply "-" to number
160 
161     ISN_CHECKNR,    // check value can be used as a number
162     ISN_CHECKTYPE,  // check value type is isn_arg.type.ct_type
163     ISN_CHECKLEN,   // check list length is isn_arg.checklen.cl_min_len
164     ISN_SETTYPE,    // set dict type to isn_arg.type.ct_type
165 
166     ISN_CLEARDICT,  // clear dict saved by ISN_MEMBER/ISN_STRINGMEMBER
167     ISN_USEDICT,    // use or clear dict saved by ISN_MEMBER/ISN_STRINGMEMBER
168 
169     ISN_PUT,	    // ":put", uses isn_arg.put
170 
171     ISN_CMDMOD,	    // set cmdmod
172     ISN_CMDMOD_REV, // undo ISN_CMDMOD
173 
174     ISN_PROF_START, // start a line for profiling
175     ISN_PROF_END,   // end a line for profiling
176 
177     ISN_DEBUG,	    // check for debug breakpoint, uses isn_arg.debug
178 
179     ISN_UNPACK,	    // unpack list into items, uses isn_arg.unpack
180     ISN_SHUFFLE,    // move item on stack up or down
181     ISN_DROP,	    // pop stack and discard value
182 
183     ISN_REDIRSTART, // :redir =>
184     ISN_REDIREND,   // :redir END, isn_arg.number == 1 for append
185 
186     ISN_CEXPR_AUCMD, // first part of :cexpr  isn_arg.number is cmdidx
187     ISN_CEXPR_CORE,  // second part of :cexpr, uses isn_arg.cexpr
188 
189     ISN_FINISH	    // end marker in list of instructions
190 } isntype_T;
191 
192 
193 // arguments to ISN_BCALL
194 typedef struct {
195     int	    cbf_idx;	    // index in "global_functions"
196     int	    cbf_argcount;   // number of arguments on top of stack
197 } cbfunc_T;
198 
199 // arguments to ISN_DCALL
200 typedef struct {
201     int	    cdf_idx;	    // index in "def_functions" for ISN_DCALL
202     int	    cdf_argcount;   // number of arguments on top of stack
203 } cdfunc_T;
204 
205 // arguments to ISN_PCALL
206 typedef struct {
207     int	    cpf_top;	    // when TRUE partial is above the arguments
208     int	    cpf_argcount;   // number of arguments on top of stack
209 } cpfunc_T;
210 
211 // arguments to ISN_UCALL and ISN_XCALL
212 typedef struct {
213     char_u  *cuf_name;
214     int	    cuf_argcount;   // number of arguments on top of stack
215 } cufunc_T;
216 
217 // arguments to ISN_GETITEM
218 typedef struct {
219     varnumber_T	gi_index;
220     int		gi_with_op;
221 } getitem_T;
222 
223 typedef enum {
224     JUMP_ALWAYS,
225     JUMP_NEVER,
226     JUMP_IF_FALSE,		// pop and jump if false
227     JUMP_AND_KEEP_IF_TRUE,	// jump if top of stack is truthy, drop if not
228     JUMP_AND_KEEP_IF_FALSE,	// jump if top of stack is falsy, drop if not
229     JUMP_IF_COND_TRUE,		// jump if top of stack is true, drop if not
230     JUMP_IF_COND_FALSE,		// jump if top of stack is false, drop if not
231 } jumpwhen_T;
232 
233 // arguments to ISN_JUMP
234 typedef struct {
235     jumpwhen_T	jump_when;
236     int		jump_where;	    // position to jump to
237 } jump_T;
238 
239 // arguments to ISN_JUMP_IF_ARG_SET
240 typedef struct {
241     int		jump_arg_off;	    // argument index, negative
242     int		jump_where;	    // position to jump to
243 } jumparg_T;
244 
245 // arguments to ISN_FOR
246 typedef struct {
247     int	    for_idx;	    // loop variable index
248     int	    for_end;	    // position to jump to after done
249 } forloop_T;
250 
251 // indirect arguments to ISN_TRY
252 typedef struct {
253     int	    try_catch;	    // position to jump to on throw
254     int	    try_finally;    // :finally or :endtry position to jump to
255     int	    try_endtry;	    // :endtry position to jump to
256 } tryref_T;
257 
258 // arguments to ISN_TRY
259 typedef struct {
260     tryref_T *try_ref;
261 } try_T;
262 
263 // arguments to ISN_TRYCONT
264 typedef struct {
265     int	    tct_levels;	    // number of nested try statements
266     int	    tct_where;	    // position to jump to, WHILE or FOR
267 } trycont_T;
268 
269 // arguments to ISN_ECHO
270 typedef struct {
271     int	    echo_with_white;    // :echo instead of :echon
272     int	    echo_count;		// number of expressions
273 } echo_T;
274 
275 // arguments to ISN_OPNR, ISN_OPFLOAT, etc.
276 typedef struct {
277     exprtype_T	op_type;
278     int		op_ic;	    // TRUE with '#', FALSE with '?', else MAYBE
279 } opexpr_T;
280 
281 // arguments to ISN_CHECKTYPE
282 typedef struct {
283     type_T	*ct_type;
284     int8_T	ct_off;		// offset in stack, -1 is bottom
285     int8_T	ct_arg_idx;	// argument index or zero
286 } checktype_T;
287 
288 // arguments to ISN_STORENR
289 typedef struct {
290     int		stnr_idx;
291     varnumber_T	stnr_val;
292 } storenr_T;
293 
294 // arguments to ISN_STOREOPT
295 typedef struct {
296     char_u	*so_name;
297     int		so_flags;
298 } storeopt_T;
299 
300 // arguments to ISN_LOADS and ISN_STORES
301 typedef struct {
302     char_u	*ls_name;	// variable name (with s: for ISN_STORES)
303     int		ls_sid;		// script ID
304 } loadstore_T;
305 
306 // arguments to ISN_LOADSCRIPT and ISN_STORESCRIPT
307 typedef struct {
308     int		sref_sid;	// script ID
309     int		sref_idx;	// index in sn_var_vals
310     int		sref_seq;	// sn_script_seq when compiled
311     type_T	*sref_type;	// type of the variable when compiled
312 } scriptref_T;
313 
314 typedef struct {
315     scriptref_T	*scriptref;
316 } script_T;
317 
318 // arguments to ISN_UNLET
319 typedef struct {
320     char_u	*ul_name;	// variable name with g:, w:, etc.
321     int		ul_forceit;	// forceit flag
322 } unlet_T;
323 
324 // arguments to ISN_FUNCREF
325 typedef struct {
326     int		fr_dfunc_idx;	// function index for :def function
327     char_u	*fr_func_name;	// function name for legacy function
328 } funcref_T;
329 
330 // arguments to ISN_NEWFUNC
331 typedef struct {
332     char_u	*nf_lambda;	// name of the lambda already defined
333     char_u	*nf_global;	// name of the global function to be created
334 } newfunc_T;
335 
336 // arguments to ISN_CHECKLEN
337 typedef struct {
338     int		cl_min_len;	// minimum length
339     int		cl_more_OK;	// longer is allowed
340 } checklen_T;
341 
342 // arguments to ISN_SHUFFLE
343 typedef struct {
344     int		shfl_item;	// item to move (relative to top of stack)
345     int		shfl_up;	// places to move upwards
346 } shuffle_T;
347 
348 // arguments to ISN_PUT
349 typedef struct {
350     int		put_regname;	// register, can be NUL
351     linenr_T	put_lnum;	// line number to put below
352 } put_T;
353 
354 // arguments to ISN_CMDMOD
355 typedef struct {
356     cmdmod_T	*cf_cmdmod;	// allocated
357 } cmod_T;
358 
359 // arguments to ISN_UNPACK
360 typedef struct {
361     int		unp_count;	// number of items to produce
362     int		unp_semicolon;	// last item gets list of remainder
363 } unpack_T;
364 
365 // arguments to ISN_LOADOUTER and ISN_STOREOUTER
366 typedef struct {
367     int		outer_idx;	// index
368     int		outer_depth;	// nesting level, stack frames to go up
369 } isn_outer_T;
370 
371 // arguments to ISN_SUBSTITUTE
372 typedef struct {
373     char_u	*subs_cmd;	// :s command
374     isn_T	*subs_instr;	// sequence of instructions
375 } subs_T;
376 
377 // indirect arguments to ISN_TRY
378 typedef struct {
379     int		cer_cmdidx;
380     char_u	*cer_cmdline;
381     int		cer_forceit;
382 } cexprref_T;
383 
384 // arguments to ISN_CEXPR_CORE
385 typedef struct {
386     cexprref_T *cexpr_ref;
387 } cexpr_T;
388 
389 // arguments to ISN_2STRING and ISN_2STRING_ANY
390 typedef struct {
391     int		offset;
392     int		tolerant;
393 } tostring_T;
394 
395 // arguments to ISN_2BOOL
396 typedef struct {
397     int		offset;
398     int		invert;
399 } tobool_T;
400 
401 // arguments to ISN_DEBUG
402 typedef struct {
403     varnumber_T	dbg_var_names_len;  // current number of local variables
404     int		dbg_break_lnum;	    // first line to break after
405 } debug_T;
406 
407 /*
408  * Instruction
409  */
410 struct isn_S {
411     isntype_T	isn_type;
412     int		isn_lnum;
413     union {
414 	char_u		    *string;
415 	varnumber_T	    number;
416 	blob_T		    *blob;
417 	vartype_T	    vartype;
418 #ifdef FEAT_FLOAT
419 	float_T		    fnumber;
420 #endif
421 	channel_T	    *channel;
422 	job_T		    *job;
423 	partial_T	    *partial;
424 	jump_T		    jump;
425 	jumparg_T	    jumparg;
426 	forloop_T	    forloop;
427 	try_T		    try;
428 	trycont_T	    trycont;
429 	cbfunc_T	    bfunc;
430 	cdfunc_T	    dfunc;
431 	cpfunc_T	    pfunc;
432 	cufunc_T	    ufunc;
433 	echo_T		    echo;
434 	opexpr_T	    op;
435 	checktype_T	    type;
436 	storenr_T	    storenr;
437 	storeopt_T	    storeopt;
438 	loadstore_T	    loadstore;
439 	script_T	    script;
440 	unlet_T		    unlet;
441 	funcref_T	    funcref;
442 	newfunc_T	    newfunc;
443 	checklen_T	    checklen;
444 	shuffle_T	    shuffle;
445 	put_T		    put;
446 	cmod_T		    cmdmod;
447 	unpack_T	    unpack;
448 	isn_outer_T	    outer;
449 	subs_T		    subs;
450 	cexpr_T		    cexpr;
451 	isn_T		    *instr;
452 	tostring_T	    tostring;
453 	tobool_T	    tobool;
454 	getitem_T	    getitem;
455 	debug_T		    debug;
456     } isn_arg;
457 };
458 
459 /*
460  * Info about a function defined with :def.  Used in "def_functions".
461  */
462 struct dfunc_S {
463     ufunc_T	*df_ufunc;	    // struct containing most stuff
464     int		df_refcount;	    // how many ufunc_T point to this dfunc_T
465     int		df_idx;		    // index in def_functions
466     int		df_deleted;	    // if TRUE function was deleted
467     int		df_script_seq;	    // Value of sctx_T sc_seq when the function
468 				    // was compiled.
469     char_u	*df_name;	    // name used for error messages
470 
471     garray_T	df_def_args_isn;    // default argument instructions
472     garray_T	df_var_names;	    // names of local vars
473 
474     // After compiling "df_instr" and/or "df_instr_prof" is not NULL.
475     isn_T	*df_instr;	    // function body to be executed
476     int		df_instr_count;	    // size of "df_instr"
477     int		df_instr_debug_count; // size of "df_instr_debug"
478     isn_T	*df_instr_debug;      // like "df_instr" with debugging
479 #ifdef FEAT_PROFILE
480     isn_T	*df_instr_prof;	     // like "df_instr" with profiling
481     int		df_instr_prof_count; // size of "df_instr_prof"
482 #endif
483 
484     int		df_varcount;	    // number of local variables
485     int		df_has_closure;	    // one if a closure was created
486 };
487 
488 // Number of entries used by stack frame for a function call.
489 // - ec_dfunc_idx:   function index
490 // - ec_iidx:        instruction index
491 // - ec_instr:       instruction list pointer
492 // - ec_outer:	     stack used for closures
493 // - funclocal:	     function-local data
494 // - ec_frame_idx:   previous frame index
495 #define STACK_FRAME_FUNC_OFF 0
496 #define STACK_FRAME_IIDX_OFF 1
497 #define STACK_FRAME_INSTR_OFF 2
498 #define STACK_FRAME_OUTER_OFF 3
499 #define STACK_FRAME_FUNCLOCAL_OFF 4
500 #define STACK_FRAME_IDX_OFF 5
501 #define STACK_FRAME_SIZE 6
502 
503 
504 #ifdef DEFINE_VIM9_GLOBALS
505 // Functions defined with :def are stored in this growarray.
506 // They are never removed, so that they can be found by index.
507 // Deleted functions have the df_deleted flag set.
508 garray_T def_functions = {0, 0, sizeof(dfunc_T), 50, NULL};
509 #else
510 extern garray_T def_functions;
511 #endif
512 
513 // Used for "lnum" when a range is to be taken from the stack.
514 #define LNUM_VARIABLE_RANGE -999
515 
516 // Used for "lnum" when a range is to be taken from the stack and "!" is used.
517 #define LNUM_VARIABLE_RANGE_ABOVE -888
518 
519 // Keep in sync with COMPILE_TYPE()
520 #ifdef FEAT_PROFILE
521 # define INSTRUCTIONS(dfunc) \
522 	(debug_break_level > 0 || may_break_in_function(dfunc->df_ufunc) \
523 	    ? (dfunc)->df_instr_debug \
524 	    : ((do_profiling == PROF_YES && (dfunc->df_ufunc)->uf_profiling) \
525 		? (dfunc)->df_instr_prof \
526 		: (dfunc)->df_instr))
527 #else
528 # define INSTRUCTIONS(dfunc) \
529 	(debug_break_level > 0 || may_break_in_function(dfunc->df_ufunc) \
530 		? (dfunc)->df_instr_debug \
531 		: (dfunc)->df_instr)
532 #endif
533