neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

typval_defs.h (14304B)


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