robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #41065 from justinmk/optionfunc4836ecad1f

master
14.1 KiB385 linesraw
1#pragma once
2
3#include <inttypes.h>
4#include <limits.h>
5#include <stdbool.h>
6
7#include "nvim/garray_defs.h"
8#include "nvim/hashtab_defs.h"
9#include "nvim/lib/queue_defs.h"
10#include "nvim/pos_defs.h"
11#include "nvim/types_defs.h"
12
13/// Type used for Vimscript VAR_NUMBER values
14typedef int64_t varnumber_T;
15typedef uint64_t uvarnumber_T;
16
17enum {
18  /// Refcount for dict or list that should not be freed
19  DO_NOT_FREE_CNT = (INT_MAX / 2),
20};
21
22/// Additional values for tv_list_alloc() len argument
23enum ListLenSpecials {
24  /// List length is not known in advance
25  ///
26  /// To be used when there is neither a way to know how many elements will be
27  /// needed nor are any educated guesses.
28  kListLenUnknown = -1,
29  /// List length *should* be known, but is actually not
30  ///
31  /// All occurrences of this value should be eventually removed. This is for
32  /// the case when the only reason why list length is not known is that it
33  /// would be hard to code without refactoring, but refactoring is needed.
34  kListLenShouldKnow = -2,
35  /// List length may be known in advance, but it requires too much effort
36  ///
37  /// To be used when it looks impractical to determine list length.
38  kListLenMayKnow = -3,
39};
40
41/// Maximal possible value of varnumber_T variable
42#define VARNUMBER_MAX INT64_MAX
43#define UVARNUMBER_MAX UINT64_MAX
44
45/// Minimal possible value of varnumber_T variable
46#define VARNUMBER_MIN INT64_MIN
47
48/// %d printf format specifier for varnumber_T
49#define PRIdVARNUMBER PRId64
50
51typedef struct listvar_S list_T;
52typedef struct dictvar_S dict_T;
53typedef struct partial_S partial_T;
54typedef struct blobvar_S blob_T;
55
56typedef struct ufunc_S ufunc_T;
57
58typedef enum {
59  kCallbackNone = 0,
60  kCallbackFuncref,
61  kCallbackPartial,
62  kCallbackLua,
63  kCallbackExpr,   ///< Vimscript expression, for "expr" options.
64} CallbackType;
65
66typedef struct {
67  union {
68    char *funcref;
69    partial_T *partial;
70    LuaRef luaref;
71    char *expr;    ///< kCallbackExpr: Vimscript, for "expr" options.
72  } data;
73  CallbackType type;
74} Callback;
75
76#define CALLBACK_INIT { .type = kCallbackNone }
77#define CALLBACK_NONE ((Callback)CALLBACK_INIT)
78
79/// Structure holding dictionary watcher
80typedef struct {
81  Callback callback;
82  char *key_pattern;
83  size_t key_pattern_len;
84  QUEUE node;
85  bool busy;  // prevent recursion if the dict is changed in the callback
86  bool needs_free;
87} DictWatcher;
88
89/// Bool variable values
90typedef enum {
91  kBoolVarFalse,         ///< v:false
92  kBoolVarTrue,          ///< v:true
93} BoolVarValue;
94
95/// Special variable values
96typedef enum {
97  kSpecialVarNull,   ///< v:null
98} SpecialVarValue;
99
100/// Variable lock status for typval_T.v_lock
101typedef enum {
102  VAR_UNLOCKED = 0,  ///< Not locked.
103  VAR_LOCKED = 1,    ///< User lock, can be unlocked.
104  VAR_FIXED = 2,     ///< Locked forever.
105} VarLockStatus;
106
107/// Vimscript variable types, for use in typval_T.v_type
108typedef enum {
109  VAR_UNKNOWN = 0,  ///< Unknown (unspecified) value.
110  VAR_NUMBER,       ///< Number, .v_number is used.
111  VAR_STRING,       ///< String, .v_string is used.
112  VAR_FUNC,         ///< Function reference, .v_string is used as function name.
113  VAR_LIST,         ///< List, .v_list is used.
114  VAR_DICT,         ///< Dict, .v_dict is used.
115  VAR_FLOAT,        ///< Floating-point value, .v_float is used.
116  VAR_BOOL,         ///< true, false
117  VAR_SPECIAL,      ///< Special value (null), .v_special is used.
118  VAR_PARTIAL,      ///< Partial, .v_partial is used.
119  VAR_BLOB,         ///< Blob, .v_blob is used.
120} VarType;
121
122/// Type values for type().
123enum {
124  VAR_TYPE_NUMBER  = 0,
125  VAR_TYPE_STRING  = 1,
126  VAR_TYPE_FUNC    = 2,
127  VAR_TYPE_LIST    = 3,
128  VAR_TYPE_DICT    = 4,
129  VAR_TYPE_FLOAT   = 5,
130  VAR_TYPE_BOOL    = 6,
131  VAR_TYPE_SPECIAL = 7,
132  VAR_TYPE_BLOB    = 10,
133};
134
135/// Structure that holds an internal variable value
136typedef struct {
137  VarType v_type;               ///< Variable type.
138  VarLockStatus v_lock;         ///< Variable lock status.
139  union typval_vval_union {
140    varnumber_T v_number;       ///< Number, for VAR_NUMBER.
141    BoolVarValue v_bool;        ///< Bool value, for VAR_BOOL
142    SpecialVarValue v_special;  ///< Special value, for VAR_SPECIAL.
143    float_T v_float;            ///< Floating-point number, for VAR_FLOAT.
144    char *v_string;             ///< String, for VAR_STRING and VAR_FUNC, can be NULL.
145    list_T *v_list;             ///< List for VAR_LIST, can be NULL.
146    dict_T *v_dict;             ///< Dict for VAR_DICT, can be NULL.
147    partial_T *v_partial;       ///< Closure: function with args.
148    blob_T *v_blob;             ///< Blob for VAR_BLOB, can be NULL.
149  } vval;                       ///< Actual value.
150} typval_T;
151
152#define TV_INITIAL_VALUE \
153  ((typval_T) { \
154    .v_type = VAR_UNKNOWN, \
155    .v_lock = VAR_UNLOCKED, \
156  })
157
158/// Values for (struct dictvar_S).dv_scope
159typedef enum {
160  VAR_NO_SCOPE = 0,  ///< Not a scope dictionary.
161  VAR_SCOPE = 1,  ///< Scope dictionary which requires prefix (a:, v:, …).
162  VAR_DEF_SCOPE = 2,  ///< Scope dictionary which may be accessed without prefix
163                      ///< (l:, g:).
164} ScopeType;
165
166/// Structure to hold an item of a list
167typedef struct listitem_S listitem_T;
168
169struct listitem_S {
170  listitem_T *li_next;  ///< Next item in list.
171  listitem_T *li_prev;  ///< Previous item in list.
172  typval_T li_tv;  ///< Item value.
173};
174
175/// Structure used by those that are using an item in a list
176typedef struct listwatch_S listwatch_T;
177
178struct listwatch_S {
179  listitem_T *lw_item;  ///< Item being watched.
180  listwatch_T *lw_next;  ///< Next watcher.
181};
182
183/// Structure to hold info about a list
184/// Order of members is optimized to reduce padding.
185struct listvar_S {
186  listitem_T *lv_first;  ///< First item, NULL if none.
187  listitem_T *lv_last;  ///< Last item, NULL if none.
188  listwatch_T *lv_watch;  ///< First watcher, NULL if none.
189  listitem_T *lv_idx_item;  ///< When not NULL item at index "lv_idx".
190  list_T *lv_copylist;  ///< Copied list used by deepcopy().
191  list_T *lv_used_next;  ///< next list in used lists list.
192  list_T *lv_used_prev;  ///< Previous list in used lists list.
193  int lv_refcount;  ///< Reference count.
194  int lv_len;  ///< Number of items.
195  int lv_idx;  ///< Index of a cached item, used for optimising repeated l[idx].
196  int lv_copyID;  ///< ID used by deepcopy().
197  VarLockStatus lv_lock;  ///< Zero, VAR_LOCKED, VAR_FIXED.
198
199  LuaRef lua_table_ref;
200};
201
202/// Static list with 10 items. Use tv_list_init_static10() to initialize.
203typedef struct {
204  list_T sl_list;  // must be first
205  listitem_T sl_items[10];
206} staticList10_T;
207
208#define TV_LIST_STATIC10_INIT { \
209  .sl_list = { \
210  .lv_first = NULL, \
211  .lv_last = NULL, \
212  .lv_refcount = 0, \
213  .lv_len = 0, \
214  .lv_watch = NULL, \
215  .lv_idx_item = NULL, \
216  .lv_lock = VAR_FIXED, \
217  .lv_used_next = NULL, \
218  .lv_used_prev = NULL, \
219  }, \
220}
221
222#define TV_DICTITEM_STRUCT(...) \
223  struct { \
224    typval_T di_tv;  /* Structure that holds scope dictionary itself. */ \
225    uint8_t di_flags;  /* Flags. */ \
226    char di_key[__VA_ARGS__];  /* Key value. */  /* NOLINT(runtime/arrays)*/ \
227  }
228
229/// Structure to hold a scope dictionary
230///
231/// @warning Must be compatible with dictitem_T.
232///
233/// For use in find_var_in_ht to pretend that it found dictionary item when it
234/// finds scope dictionary.
235typedef TV_DICTITEM_STRUCT(1) ScopeDictDictItem;
236
237/// Structure to hold an item of a Dictionary
238///
239/// @warning Must be compatible with ScopeDictDictItem.
240///
241/// Also used for a variable.
242typedef TV_DICTITEM_STRUCT() dictitem_T;
243
244/// Flags for dictitem_T.di_flags
245typedef enum {
246  DI_FLAGS_RO = 1,  ///< Read-only value
247  DI_FLAGS_RO_SBX = 2,  ///< Value, read-only in the sandbox
248  DI_FLAGS_FIX = 4,  ///< Fixed value: cannot be :unlet or remove()d.
249  DI_FLAGS_LOCK = 8,  ///< Locked value.
250  DI_FLAGS_ALLOC = 16,  ///< Separately allocated.
251} DictItemFlags;
252
253/// Structure representing a Dictionary
254struct dictvar_S {
255  VarLockStatus dv_lock;  ///< Whole dictionary lock status.
256  ScopeType dv_scope;     ///< Non-zero (#VAR_SCOPE, #VAR_DEF_SCOPE) if
257                          ///< dictionary represents a scope (i.e. g:, l: …).
258  int dv_refcount;        ///< Reference count.
259  int dv_copyID;          ///< ID used when recursivery traversing a value.
260  hashtab_T dv_hashtab;   ///< Hashtab containing all items.
261  dict_T *dv_copydict;    ///< Copied dict used by deepcopy().
262  dict_T *dv_used_next;   ///< Next dictionary in used dictionaries list.
263  dict_T *dv_used_prev;   ///< Previous dictionary in used dictionaries list.
264  QUEUE watchers;         ///< Dict key watchers set by user code.
265
266  LuaRef lua_table_ref;
267};
268
269/// Structure to hold info about a Blob
270struct blobvar_S {
271  garray_T bv_ga;         ///< Growarray with the data.
272  int bv_refcount;        ///< Reference count.
273  VarLockStatus bv_lock;  ///< VAR_UNLOCKED, VAR_LOCKED, VAR_FIXED.
274};
275
276/// Type used for script ID
277typedef int scid_T;
278/// Format argument for scid_T
279#define PRIdSCID "d"
280
281/// SCript ConteXt (SCTX): identifies a script line.
282/// When sourcing a script "sc_lnum" is zero, "sourcing_lnum" is the current
283/// line number. When executing a user function "sc_lnum" is the line where the
284/// function was defined, "sourcing_lnum" is the line number inside the
285/// function.  When stored with a function, mapping, option, etc. "sc_lnum" is
286/// the line number in the script "sc_sid".
287typedef struct {
288  scid_T sc_sid;     ///< script ID
289  int sc_seq;        ///< sourcing sequence number
290  linenr_T sc_lnum;  ///< line number
291  uint64_t sc_chan;  ///< Only used when sc_sid is SID_API_CLIENT.
292} sctx_T;
293
294enum { MAX_FUNC_ARGS = 20, };  ///< Maximum number of function arguments
295enum { VAR_SHORT_LEN = 20, };  ///< Short variable name length
296enum { FIXVAR_CNT = 12, };     ///< Number of fixed variables used for arguments
297
298/// Structure to hold info for a function that is currently being executed.
299typedef struct funccall_S funccall_T;
300
301struct funccall_S {
302  ufunc_T *fc_func;                  ///< Function being called.
303  int fc_linenr;                     ///< Next line to be executed.
304  int fc_returned;                   ///< ":return" used.
305  TV_DICTITEM_STRUCT(VAR_SHORT_LEN + 1) fc_fixvar[FIXVAR_CNT];  ///< Fixed variables for arguments.
306  dict_T fc_l_vars;                  ///< l: local function variables.
307  ScopeDictDictItem fc_l_vars_var;   ///< Variable for l: scope.
308  dict_T fc_l_avars;                 ///< a: argument variables.
309  ScopeDictDictItem fc_l_avars_var;  ///< Variable for a: scope.
310  list_T fc_l_varlist;                       ///< List for a:000.
311  listitem_T fc_l_listitems[MAX_FUNC_ARGS];  ///< List items for a:000.
312  typval_T *fc_rettv;                ///< Return value.
313  linenr_T fc_breakpoint;            ///< Next line with breakpoint or zero.
314  int fc_dbg_tick;                   ///< "debug_tick" when breakpoint was set.
315  int fc_level;                      ///< Top nesting level of executed function.
316  garray_T fc_defer;                 ///< Functions to be called on return.
317  proftime_T fc_prof_child;          ///< Time spent in a child.
318  funccall_T *fc_caller;             ///< Calling function or NULL; or next funccal in
319                                     ///< list pointed to by previous_funccal.
320  int fc_refcount;                   ///< Number of user functions that reference this funccall.
321  int fc_copyID;                     ///< CopyID used for garbage collection.
322  garray_T fc_ufuncs;                ///< List of ufunc_T* which keep a reference to "fc_func".
323};
324
325/// Structure to hold info for a user function.
326struct ufunc_S {
327  int uf_varargs;       ///< variable nr of arguments
328  int uf_flags;
329  int uf_calls;         ///< nr of active calls
330  bool uf_cleared;       ///< func_clear() was already called
331  garray_T uf_args;          ///< arguments, including optional arguments
332  garray_T uf_def_args;      ///< default argument expressions
333  garray_T uf_lines;         ///< function lines
334  int uf_profiling;     ///< true when func is being profiled
335  int uf_prof_initialized;
336  LuaRef uf_luaref;      ///< lua callback, used if (uf_flags & FC_LUAREF)
337  // Profiling the function as a whole.
338  int uf_tm_count;      ///< nr of calls
339  proftime_T uf_tm_total;      ///< time spent in function + children
340  proftime_T uf_tm_self;       ///< time spent in function itself
341  proftime_T uf_tm_children;   ///< time spent in children this call
342  // Profiling the function per line.
343  int *uf_tml_count;     ///< nr of times line was executed
344  proftime_T *uf_tml_total;     ///< time spent in a line + children
345  proftime_T *uf_tml_self;      ///< time spent in a line itself
346  proftime_T uf_tml_start;     ///< start time for current line
347  proftime_T uf_tml_children;  ///< time spent in children for this line
348  proftime_T uf_tml_wait;      ///< start wait time for current line
349  int uf_tml_idx;       ///< index of line being timed; -1 if none
350  int uf_tml_execed;    ///< line being timed was executed
351  sctx_T uf_script_ctx;    ///< SCTX where function was defined,
352                           ///< used for s: variables
353  int uf_refcount;      ///< reference count, see func_name_refcount()
354  funccall_T *uf_scoped;       ///< l: local variables for closure
355  char *uf_name_exp;    ///< if "uf_name[]" starts with SNR the name with
356                        ///< "<SNR>" as a string, otherwise NULL
357  size_t uf_namelen;    ///< Length of uf_name (excluding the NUL)
358  char uf_name[];       ///< Name of function (actual size equals name);
359                        ///< can start with <SNR>123_
360                        ///< (<SNR> is K_SPECIAL KS_EXTRA KE_SNR)
361};
362
363struct partial_S {
364  int pt_refcount;    ///< Reference count.
365  int pt_copyID;
366  char *pt_name;      ///< Function name; when NULL use pt_func->name.
367  ufunc_T *pt_func;   ///< Function pointer; when NULL lookup function with pt_name.
368  bool pt_auto;       ///< When true the partial was created by using dict.member
369                      ///< in handle_subscript().
370  int pt_argc;        ///< Number of arguments.
371  typval_T *pt_argv;  ///< Arguments in allocated array.
372  dict_T *pt_dict;    ///< Dict for "self".
373};
374
375/// Structure used for explicit stack while garbage collecting hash tables
376typedef struct ht_stack_S {
377  hashtab_T *ht;
378  struct ht_stack_S *prev;
379} ht_stack_T;
380
381/// Structure used for explicit stack while garbage collecting lists
382typedef struct list_stack_S {
383  list_T *list;
384  struct list_stack_S *prev;
385} list_stack_T;