robin/laufey

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

bfredlMerge pull request #35206 from bfredl/nopreproc_cleanup38986188ba

master
5.0 KiB132 linesraw
1/// @file eval/typval_encode.h
2///
3/// Contains common definitions for eval/typval_encode.c.h. Most of time should
4/// not be included directly.
5#pragma once
6
7#include <assert.h>
8#include <inttypes.h>
9#include <stddef.h>
10#include <string.h>
11
12#include "klib/kvec.h"
13#include "nvim/eval/typval_defs.h"
14
15#include "eval/typval_encode.h.inline.generated.h"
16
17/// Type of the stack entry
18typedef enum {
19  kMPConvDict,  ///< Convert dict_T *dictionary.
20  kMPConvList,  ///< Convert list_T *list.
21  kMPConvPairs,  ///< Convert mapping represented as a list_T* of pairs.
22  kMPConvPartial,  ///< Convert partial_T* partial.
23  kMPConvPartialList,  ///< Convert argc/argv pair coming from a partial.
24} MPConvStackValType;
25
26/// Stage at which partial is being converted
27typedef enum {
28  kMPConvPartialArgs,  ///< About to convert arguments.
29  kMPConvPartialSelf,  ///< About to convert self dictionary.
30  kMPConvPartialEnd,  ///< Already converted everything.
31} MPConvPartialStage;
32
33/// Structure representing current Vimscript to messagepack conversion state
34typedef struct {
35  MPConvStackValType type;  ///< Type of the stack entry.
36  typval_T *tv;  ///< Currently converted typval_T.
37  int saved_copyID;  ///< copyID item used to have.
38  union {
39    struct {
40      dict_T *dict;    ///< Currently converted dictionary.
41      dict_T **dictp;  ///< Location where that dictionary is stored.
42                       ///< Normally it is &.tv->vval.v_dict, but not when
43                       ///< converting partials.
44      hashitem_T *hi;  ///< Currently converted dictionary item.
45      size_t todo;     ///< Amount of items left to process.
46    } d;  ///< State of dictionary conversion.
47    struct {
48      list_T *list;    ///< Currently converted list.
49      listitem_T *li;  ///< Currently converted list item.
50    } l;  ///< State of list or generic mapping conversion.
51    struct {
52      MPConvPartialStage stage;  ///< Stage at which partial is being converted.
53      partial_T *pt;  ///< Currently converted partial.
54    } p;  ///< State of partial conversion.
55    struct {
56      typval_T *arg;    ///< Currently converted argument.
57      typval_T *argv;    ///< Start of the argument list.
58      size_t todo;  ///< Number of items left to process.
59    } a;  ///< State of list or generic mapping conversion.
60  } data;  ///< Data to convert.
61} MPConvStackVal;
62
63/// Stack used to convert Vimscript values to messagepack.
64typedef kvec_withinit_t(MPConvStackVal, 8) MPConvStack;
65
66/// Length of the string stored in typval_T
67///
68/// @param[in]  tv  String for which to compute length for. Must be typval_T
69///                 with VAR_STRING.
70///
71/// @return Length of the string stored in typval_T, including 0 for NULL
72///         string.
73static inline size_t tv_strlen(const typval_T *const tv)
74  FUNC_ATTR_ALWAYS_INLINE FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
75  FUNC_ATTR_NONNULL_ALL
76{
77  assert(tv->v_type == VAR_STRING);
78  return (tv->vval.v_string == NULL ? 0 : strlen(tv->vval.v_string));
79}
80
81/// Code for checking whether container references itself
82///
83/// @param[in,out]  val  Container to check.
84/// @param  copyID_attr  Name of the container attribute that holds copyID.
85///                      After checking whether value of this attribute is
86///                      copyID (variable) it is set to copyID.
87/// @param[in]  copyID  CopyID used by the caller.
88/// @param  conv_type  Type of the conversion, @see MPConvStackValType.
89#define TYPVAL_ENCODE_DO_CHECK_SELF_REFERENCE(val, copyID_attr, copyID, \
90                                              conv_type) \
91  do { \
92    const int te_csr_ret = TYPVAL_ENCODE_CHECK_SELF_REFERENCE(TYPVAL_ENCODE_FIRST_ARG_NAME, \
93                                                              (val), &(val)->copyID_attr, mpstack, \
94                                                              copyID, conv_type, objname); \
95    if (te_csr_ret != NOTDONE) { \
96      return te_csr_ret; \
97    } \
98  } while (0)
99
100#define TYPVAL_ENCODE_FUNC_NAME_INNER_2(pref, name, suf) \
101  pref##name##suf
102#define TYPVAL_ENCODE_FUNC_NAME_INNER(pref, name, suf) \
103  TYPVAL_ENCODE_FUNC_NAME_INNER_2(pref, name, suf)
104
105/// Construct function name, possibly using macros
106///
107/// Is used to expand macros that may appear in arguments.
108///
109/// @note Expands all arguments, even if only one is needed.
110///
111/// @param[in]  pref  Prefix.
112/// @param[in]  suf  Suffix.
113///
114/// @return Concat: pref + #TYPVAL_ENCODE_NAME + suf.
115#define TYPVAL_ENCODE_FUNC_NAME(pref, suf) \
116  TYPVAL_ENCODE_FUNC_NAME_INNER(pref, TYPVAL_ENCODE_NAME, suf)
117
118/// Self reference checker function name
119#define TYPVAL_ENCODE_CHECK_SELF_REFERENCE \
120  TYPVAL_ENCODE_FUNC_NAME(_typval_encode_, _check_self_reference)
121
122/// Entry point function name
123#define TYPVAL_ENCODE_ENCODE \
124  TYPVAL_ENCODE_FUNC_NAME(encode_vim_to_, )
125
126/// Name of the …convert_one_value function
127#define TYPVAL_ENCODE_CONVERT_ONE_VALUE \
128  TYPVAL_ENCODE_FUNC_NAME(_typval_encode_, _convert_one_value)
129
130/// Name of the dummy const dict_T *const variable
131#define TYPVAL_ENCODE_NODICT_VAR \
132  TYPVAL_ENCODE_FUNC_NAME(_typval_encode_, _nodict_var)