1 /*******************************************************************************
2 *
3 * HEADER: cttype.h
4 *
5 ********************************************************************************
6 *
7 * DESCRIPTION: ANSI C data type objects
8 *
9 ********************************************************************************
10 *
11 * Copyright (c) 2002-2020 Marcus Holland-Moritz. All rights reserved.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the same terms as Perl itself.
14 *
15 *******************************************************************************/
16 
17 #ifndef _CTLIB_CTTYPE_H
18 #define _CTLIB_CTTYPE_H
19 
20 /*===== GLOBAL INCLUDES ======================================================*/
21 
22 /*===== LOCAL INCLUDES =======================================================*/
23 
24 #include "arch.h"
25 #include "cttags.h"
26 #include "fileinfo.h"
27 #include "util/list.h"
28 
29 
30 /*===== DEFINES ==============================================================*/
31 
32 /* value flags */
33 
34 #define V_IS_UNDEF                     0x00000001
35 #define V_IS_UNSAFE                    0x08000000
36 #define V_IS_UNSAFE_UNDEF              0x10000000
37 #define V_IS_UNSAFE_CAST               0x20000000
38 #define V_IS_UNSAFE_PTROP              0x40000000
39 
40 #define IS_UNSAFE_VAL( val ) ( (val).flags & ( V_IS_UNSAFE       \
41                                              | V_IS_UNSAFE_UNDEF \
42                                              | V_IS_UNSAFE_CAST  \
43                                              | V_IS_UNSAFE_PTROP ) )
44 
45 /* type flags */
46 
47 #define T_VOID                         0x00000001
48 #define T_CHAR                         0x00000002
49 #define T_SHORT                        0x00000004
50 #define T_INT                          0x00000008
51 
52 #define T_LONG                         0x00000010
53 #define T_FLOAT                        0x00000020
54 #define T_DOUBLE                       0x00000040
55 #define T_SIGNED                       0x00000080
56 
57 #define T_UNSIGNED                     0x00000100
58 #define T_ENUM                         0x00000200
59 #define T_STRUCT                       0x00000400
60 #define T_UNION                        0x00000800
61 #define T_COMPOUND                     (T_STRUCT | T_UNION)
62 
63 #define T_TYPE                         0x00001000
64 #define T_TYPEDEF                      0x00002000
65 #define T_LONGLONG                     0x00004000
66 
67 /* these flags are reserved for user defined purposes */
68 #define T_USER_FLAG_1                  0x00100000
69 #define T_USER_FLAG_2                  0x00200000
70 #define T_USER_FLAG_3                  0x00400000
71 #define T_USER_FLAG_4                  0x00800000
72 
73 /* this flag indicates the usage of bitfields in structures as they're unsupported */
74 #define T_HASBITFIELD                  0x40000000
75 
76 /* this flag indicates the use of unsafe values (e.g. sizes of bitfields) */
77 #define T_UNSAFE_VAL                   0x80000000
78 
79 #define ANY_TYPE_NAME ( T_VOID | T_CHAR | T_SHORT | T_INT | T_LONG | T_FLOAT | T_DOUBLE \
80                         | T_SIGNED | T_UNSIGNED | T_ENUM | T_STRUCT | T_UNION | T_TYPE )
81 
82 /* get the type out of a pointer to EnumSpecifier / Struct / Typedef */
83 #define GET_CTYPE( ptr ) (*((CTType *) ptr))
84 
85 #define IS_TYP_ENUM( ptr )            ( GET_CTYPE( ptr ) == TYP_ENUM )
86 #define IS_TYP_STRUCT( ptr )          ( GET_CTYPE( ptr ) == TYP_STRUCT )
87 #define IS_TYP_TYPEDEF( ptr )         ( GET_CTYPE( ptr ) == TYP_TYPEDEF )
88 #define IS_TYP_TYPEDEF_LIST( ptr )    ( GET_CTYPE( ptr ) == TYP_TYPEDEF_LIST )
89 
90 #define CTT_IDLEN(ptr)  ((ptr)->id_len < 255 ? (ptr)->id_len                   \
91                          : 255 + strlen((ptr)->identifier + 255))
92 
93 /*===== TYPEDEFS =============================================================*/
94 
95 typedef enum {
96   TYP_ENUM,
97   TYP_STRUCT,
98   TYP_TYPEDEF,
99   TYP_TYPEDEF_LIST
100 } CTType;
101 
102 enum {
103   ES_UNSIGNED_SIZE,
104   ES_SIGNED_SIZE,
105   ES_NUM_ENUM_SIZES
106 };
107 
108 typedef struct {
109   signed long iv;
110   u_32        flags;
111 } Value;
112 
113 typedef struct {
114   FileInfo     *pFI;
115   unsigned long line;
116 } ContextInfo;
117 
118 typedef struct {
119   void       *ptr;
120   u_32        tflags;
121 } TypeSpec;
122 
123 typedef struct {
124   Value       value;
125   unsigned char id_len;
126   char        identifier[1];
127 } Enumerator;
128 
129 typedef struct {
130   CTType      ctype;
131   u_32        tflags;
132   unsigned    refcount;
133   unsigned    sizes[ES_NUM_ENUM_SIZES];
134   ContextInfo context;
135   LinkedList  enumerators;
136   CtTagList   tags;
137   unsigned char id_len;
138   char        identifier[1];
139 } EnumSpecifier;
140 
141 typedef struct {
142   unsigned char size;     /* size (in bytes), usually same as Declarator.size */
143   unsigned char bits;     /* size (in bits) of the bitfield                   */
144   unsigned char pos;      /* pos (in bits) of the bitfield (relative to LSB)  */
145 } BitfieldInfo;
146 
147 typedef struct {
148   signed      offset        : 29;
149   unsigned    pointer_flag  :  1;
150   unsigned    array_flag    :  1;
151   unsigned    bitfield_flag :  1;
152   signed      size, item_size;
153   CtTagList   tags;
154   union {
155     LinkedList   array;
156     BitfieldInfo bitfield;
157   }           ext;
158   unsigned char id_len;
159   char        identifier[1];
160 } Declarator;
161 
162 typedef struct {
163   int         pointer_flag;
164   int         multiplicator;
165 } AbstractDeclarator;
166 
167 typedef struct {
168   TypeSpec    type;
169   LinkedList  declarators;
170   int         offset, size;
171 } StructDeclaration;
172 
173 typedef struct {
174   CTType      ctype;
175   u_32        tflags;
176   unsigned    refcount;
177   unsigned    align : 16;
178   unsigned    pack  : 16;
179   unsigned    size;
180   ContextInfo context;
181   LinkedList  declarations;
182   CtTagList   tags;
183   unsigned char id_len;
184   char        identifier[1];
185 } Struct;
186 
187 typedef struct {
188   CTType      ctype;
189   TypeSpec   *pType;
190   Declarator *pDecl;
191 } Typedef;
192 
193 typedef struct {
194   CTType      ctype;
195   TypeSpec    type;
196   LinkedList  typedefs;
197 } TypedefList;
198 
199 /*===== FUNCTION PROTOTYPES ==================================================*/
200 
201 #define value_new CTlib_value_new
202 Value *value_new(signed long iv, u_32 flags);
203 #define value_delete CTlib_value_delete
204 void value_delete(Value *pValue);
205 #define value_clone CTlib_value_clone
206 Value *value_clone(const Value *pSrc);
207 
208 #define enum_new CTlib_enum_new
209 Enumerator *enum_new(const char *identifier, int id_len, Value *pValue);
210 #define enum_delete CTlib_enum_delete
211 void enum_delete(Enumerator *pEnum);
212 #define enum_clone CTlib_enum_clone
213 Enumerator *enum_clone(const Enumerator *pSrc);
214 
215 #define enumspec_new CTlib_enumspec_new
216 EnumSpecifier *enumspec_new(const char *identifier, int id_len, LinkedList enumerators);
217 #define enumspec_update CTlib_enumspec_update
218 void enumspec_update(EnumSpecifier *pEnumSpec, LinkedList enumerators);
219 #define enumspec_delete CTlib_enumspec_delete
220 void enumspec_delete(EnumSpecifier *pEnumSpec);
221 #define enumspec_clone CTlib_enumspec_clone
222 EnumSpecifier *enumspec_clone(const EnumSpecifier *pSrc);
223 
224 #define decl_new CTlib_decl_new
225 Declarator *decl_new(const char *identifier, int id_len);
226 #define decl_delete CTlib_decl_delete
227 void decl_delete(Declarator *pDecl);
228 #define decl_clone CTlib_decl_clone
229 Declarator *decl_clone(const Declarator *pSrc);
230 
231 #define structdecl_new CTlib_structdecl_new
232 StructDeclaration *structdecl_new(TypeSpec type, LinkedList declarators);
233 #define structdecl_delete CTlib_structdecl_delete
234 void structdecl_delete(StructDeclaration *pStructDecl);
235 #define structdecl_clone CTlib_structdecl_clone
236 StructDeclaration *structdecl_clone(const StructDeclaration *pSrc);
237 
238 #define struct_new CTlib_struct_new
239 Struct *struct_new(const char *identifier, int id_len, u_32 tflags, unsigned pack,
240                    LinkedList declarations);
241 #define struct_delete CTlib_struct_delete
242 void struct_delete(Struct *pStruct);
243 #define struct_clone CTlib_struct_clone
244 Struct *struct_clone(const Struct *pSrc);
245 
246 #define typedef_new CTlib_typedef_new
247 Typedef *typedef_new(TypeSpec *pType, Declarator *pDecl);
248 #define typedef_delete CTlib_typedef_delete
249 void typedef_delete(Typedef *pTypedef);
250 #define typedef_clone CTlib_typedef_clone
251 Typedef *typedef_clone(const Typedef *pSrc);
252 
253 #define typedef_list_new CTlib_typedef_list_new
254 TypedefList *typedef_list_new(TypeSpec type, LinkedList typedefs);
255 #define typedef_list_delete CTlib_typedef_list_delete
256 void typedef_list_delete(TypedefList *pTypedefList);
257 #define typedef_list_clone CTlib_typedef_list_clone
258 TypedefList *typedef_list_clone(const TypedefList *pSrc);
259 
260 #define get_typedef_list CTlib_get_typedef_list
261 TypedefList *get_typedef_list(Typedef *pTypedef);
262 
263 #define ctt_refcount_inc CTlib_ctt_refcount_inc
264 void ctt_refcount_inc(void *ptr);
265 
266 #endif
267