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