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