robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
4836ecad1f
master
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 19DO_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. 28kListLenUnknown = -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. 34kListLenShouldKnow = -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. 38kListLenMayKnow = -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 { 59kCallbackNone = 0 , 60kCallbackFuncref , 61kCallbackPartial , 62kCallbackLua , 63kCallbackExpr ,///< Vimscript expression, for "expr" options. 64}CallbackType ; 65 66typedef struct { 67union { 68char * funcref ; 69partial_T * partial ; 70LuaRef luaref ; 71char * expr ;///< kCallbackExpr: Vimscript, for "expr" options. 72 }data ; 73CallbackType 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 { 81Callback callback ; 82char * key_pattern ; 83size_t key_pattern_len ; 84QUEUE node ; 85bool busy ;// prevent recursion if the dict is changed in the callback 86bool needs_free ; 87}DictWatcher ; 88 89/// Bool variable values 90typedef enum { 91kBoolVarFalse ,///< v:false 92kBoolVarTrue ,///< v:true 93}BoolVarValue ; 94 95/// Special variable values 96typedef enum { 97kSpecialVarNull ,///< v:null 98}SpecialVarValue ; 99 100/// Variable lock status for typval_T.v_lock 101typedef enum { 102VAR_UNLOCKED = 0 ,///< Not locked. 103VAR_LOCKED = 1 ,///< User lock, can be unlocked. 104VAR_FIXED = 2 ,///< Locked forever. 105}VarLockStatus ; 106 107/// Vimscript variable types, for use in typval_T.v_type 108typedef enum { 109VAR_UNKNOWN = 0 ,///< Unknown (unspecified) value. 110VAR_NUMBER ,///< Number, .v_number is used. 111VAR_STRING ,///< String, .v_string is used. 112VAR_FUNC ,///< Function reference, .v_string is used as function name. 113VAR_LIST ,///< List, .v_list is used. 114VAR_DICT ,///< Dict, .v_dict is used. 115VAR_FLOAT ,///< Floating-point value, .v_float is used. 116VAR_BOOL ,///< true, false 117VAR_SPECIAL ,///< Special value (null), .v_special is used. 118VAR_PARTIAL ,///< Partial, .v_partial is used. 119VAR_BLOB ,///< Blob, .v_blob is used. 120}VarType ; 121 122/// Type values for type(). 123enum { 124VAR_TYPE_NUMBER = 0 , 125VAR_TYPE_STRING = 1 , 126VAR_TYPE_FUNC = 2 , 127VAR_TYPE_LIST = 3 , 128VAR_TYPE_DICT = 4 , 129VAR_TYPE_FLOAT = 5 , 130VAR_TYPE_BOOL = 6 , 131VAR_TYPE_SPECIAL = 7 , 132VAR_TYPE_BLOB = 10 , 133}; 134 135/// Structure that holds an internal variable value 136typedef struct { 137VarType v_type ;///< Variable type. 138VarLockStatus v_lock ;///< Variable lock status. 139union typval_vval_union { 140varnumber_T v_number ;///< Number, for VAR_NUMBER. 141BoolVarValue v_bool ;///< Bool value, for VAR_BOOL 142SpecialVarValue v_special ;///< Special value, for VAR_SPECIAL. 143float_T v_float ;///< Floating-point number, for VAR_FLOAT. 144char * v_string ;///< String, for VAR_STRING and VAR_FUNC, can be NULL. 145list_T * v_list ;///< List for VAR_LIST, can be NULL. 146dict_T * v_dict ;///< Dict for VAR_DICT, can be NULL. 147partial_T * v_partial ;///< Closure: function with args. 148blob_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 { 160VAR_NO_SCOPE = 0 ,///< Not a scope dictionary. 161VAR_SCOPE = 1 ,///< Scope dictionary which requires prefix (a:, v:, …). 162VAR_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 { 170listitem_T * li_next ;///< Next item in list. 171listitem_T * li_prev ;///< Previous item in list. 172typval_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 { 179listitem_T * lw_item ;///< Item being watched. 180listwatch_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 { 186listitem_T * lv_first ;///< First item, NULL if none. 187listitem_T * lv_last ;///< Last item, NULL if none. 188listwatch_T * lv_watch ;///< First watcher, NULL if none. 189listitem_T * lv_idx_item ;///< When not NULL item at index "lv_idx". 190list_T * lv_copylist ;///< Copied list used by deepcopy(). 191list_T * lv_used_next ;///< next list in used lists list. 192list_T * lv_used_prev ;///< Previous list in used lists list. 193int lv_refcount ;///< Reference count. 194int lv_len ;///< Number of items. 195int lv_idx ;///< Index of a cached item, used for optimising repeated l[idx]. 196int lv_copyID ;///< ID used by deepcopy(). 197VarLockStatus lv_lock ;///< Zero, VAR_LOCKED, VAR_FIXED. 198 199LuaRef lua_table_ref ; 200}; 201 202/// Static list with 10 items. Use tv_list_init_static10() to initialize. 203typedef struct { 204list_T sl_list ;// must be first 205listitem_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. */ \ 225uint8_t di_flags ;/* Flags. */ \ 226char 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 { 246DI_FLAGS_RO = 1 , ///< Read-only value 247DI_FLAGS_RO_SBX = 2 , ///< Value, read-only in the sandbox 248DI_FLAGS_FIX = 4 , ///< Fixed value: cannot be :unlet or remove()d. 249DI_FLAGS_LOCK = 8 , ///< Locked value. 250DI_FLAGS_ALLOC = 16 , ///< Separately allocated. 251} DictItemFlags; 252 253/// Structure representing a Dictionary 254struct dictvar_S { 255VarLockStatus dv_lock ; ///< Whole dictionary lock status. 256ScopeType dv_scope ; ///< Non-zero (#VAR_SCOPE, #VAR_DEF_SCOPE) if 257///< dictionary represents a scope (i.e. g:, l: …). 258int dv_refcount ; ///< Reference count. 259int dv_copyID ; ///< ID used when recursivery traversing a value. 260hashtab_T dv_hashtab ; ///< Hashtab containing all items. 261dict_T * dv_copydict ; ///< Copied dict used by deepcopy(). 262dict_T * dv_used_next ; ///< Next dictionary in used dictionaries list. 263dict_T * dv_used_prev ; ///< Previous dictionary in used dictionaries list. 264QUEUE watchers ; ///< Dict key watchers set by user code. 265 266LuaRef lua_table_ref ; 267}; 268 269/// Structure to hold info about a Blob 270struct blobvar_S { 271garray_T bv_ga ; ///< Growarray with the data. 272int bv_refcount ; ///< Reference count. 273VarLockStatus 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 { 288scid_T sc_sid ; ///< script ID 289int sc_seq ; ///< sourcing sequence number 290linenr_T sc_lnum ; ///< line number 291uint64_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 { 302ufunc_T * fc_func ; ///< Function being called. 303int fc_linenr ; ///< Next line to be executed. 304int fc_returned ; ///< ":return" used. 305TV_DICTITEM_STRUCT (VAR_SHORT_LEN + 1 ) fc_fixvar [ FIXVAR_CNT ]; ///< Fixed variables for arguments. 306dict_T fc_l_vars ; ///< l: local function variables. 307ScopeDictDictItem fc_l_vars_var ; ///< Variable for l: scope. 308dict_T fc_l_avars ; ///< a: argument variables. 309ScopeDictDictItem fc_l_avars_var ; ///< Variable for a: scope. 310list_T fc_l_varlist ; ///< List for a:000. 311listitem_T fc_l_listitems [ MAX_FUNC_ARGS ]; ///< List items for a:000. 312typval_T * fc_rettv ; ///< Return value. 313linenr_T fc_breakpoint ; ///< Next line with breakpoint or zero. 314int fc_dbg_tick ; ///< "debug_tick" when breakpoint was set. 315int fc_level ; ///< Top nesting level of executed function. 316garray_T fc_defer ; ///< Functions to be called on return. 317proftime_T fc_prof_child ; ///< Time spent in a child. 318funccall_T * fc_caller ; ///< Calling function or NULL; or next funccal in 319///< list pointed to by previous_funccal. 320int fc_refcount ; ///< Number of user functions that reference this funccall. 321int fc_copyID ; ///< CopyID used for garbage collection. 322garray_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 { 327int uf_varargs ; ///< variable nr of arguments 328int uf_flags ; 329int uf_calls ; ///< nr of active calls 330bool uf_cleared ; ///< func_clear() was already called 331garray_T uf_args ; ///< arguments, including optional arguments 332garray_T uf_def_args ; ///< default argument expressions 333garray_T uf_lines ; ///< function lines 334int uf_profiling ; ///< true when func is being profiled 335int uf_prof_initialized ; 336LuaRef uf_luaref ; ///< lua callback, used if (uf_flags & FC_LUAREF) 337// Profiling the function as a whole. 338int uf_tm_count ; ///< nr of calls 339proftime_T uf_tm_total ; ///< time spent in function + children 340proftime_T uf_tm_self ; ///< time spent in function itself 341proftime_T uf_tm_children ; ///< time spent in children this call 342// Profiling the function per line. 343int * uf_tml_count ; ///< nr of times line was executed 344proftime_T * uf_tml_total ; ///< time spent in a line + children 345proftime_T * uf_tml_self ; ///< time spent in a line itself 346proftime_T uf_tml_start ; ///< start time for current line 347proftime_T uf_tml_children ; ///< time spent in children for this line 348proftime_T uf_tml_wait ; ///< start wait time for current line 349int uf_tml_idx ; ///< index of line being timed; -1 if none 350int uf_tml_execed ; ///< line being timed was executed 351sctx_T uf_script_ctx ; ///< SCTX where function was defined, 352///< used for s: variables 353int uf_refcount ; ///< reference count, see func_name_refcount() 354funccall_T * uf_scoped ; ///< l: local variables for closure 355char * uf_name_exp ; ///< if "uf_name[]" starts with SNR the name with 356///< "<SNR>" as a string, otherwise NULL 357size_t uf_namelen ; ///< Length of uf_name (excluding the NUL) 358char 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 { 364int pt_refcount ; ///< Reference count. 365int pt_copyID ; 366char * pt_name ; ///< Function name; when NULL use pt_func->name. 367ufunc_T * pt_func ; ///< Function pointer; when NULL lookup function with pt_name. 368bool pt_auto ; ///< When true the partial was created by using dict.member 369///< in handle_subscript(). 370int pt_argc ; ///< Number of arguments. 371typval_T * pt_argv ; ///< Arguments in allocated array. 372dict_T * pt_dict ; ///< Dict for "self". 373}; 374 375/// Structure used for explicit stack while garbage collecting hash tables 376typedef struct ht_stack_S { 377hashtab_T * ht ; 378struct ht_stack_S * prev ; 379} ht_stack_T; 380 381/// Structure used for explicit stack while garbage collecting lists 382typedef struct list_stack_S { 383list_T * list ; 384struct list_stack_S * prev ; 385} list_stack_T ;