Rework compound type handling:
[cparser] / entity_t.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #ifndef ENTITY_T_H
21 #define ENTITY_T_H
22
23 #include "lexer.h"
24 #include "symbol.h"
25 #include "entity.h"
26 #include "attribute.h"
27 #include <libfirm/firm_types.h>
28
29 typedef enum {
30         ENTITY_INVALID,
31         ENTITY_VARIABLE,
32         ENTITY_COMPOUND_MEMBER,
33         ENTITY_PARAMETER,
34         ENTITY_FUNCTION,
35         ENTITY_TYPEDEF,
36         ENTITY_STRUCT,
37         ENTITY_UNION,
38         ENTITY_ENUM,
39         ENTITY_ENUM_VALUE,
40         ENTITY_LABEL,
41         ENTITY_LOCAL_LABEL,
42         ENTITY_NAMESPACE
43 } entity_kind_tag_t;
44 typedef unsigned char entity_kind_t;
45
46 typedef enum namespace_tag_t {
47         NAMESPACE_INVALID,
48         NAMESPACE_NORMAL,
49         NAMESPACE_TAG,
50         NAMESPACE_LABEL
51 } namespace_tag_t;
52 typedef unsigned char entity_namespace_t;
53
54 typedef enum storage_class_tag_t {
55         STORAGE_CLASS_NONE,
56         STORAGE_CLASS_EXTERN,
57         STORAGE_CLASS_STATIC,
58         STORAGE_CLASS_TYPEDEF,
59         STORAGE_CLASS_AUTO,
60         STORAGE_CLASS_REGISTER,
61 } storage_class_tag_t;
62 typedef unsigned char storage_class_t;
63
64 typedef enum decl_modifier_t {
65         DM_NONE              = 0,
66         DM_DLLIMPORT         = 1 <<  0,
67         DM_DLLEXPORT         = 1 <<  1,
68         DM_THREAD            = 1 <<  2,
69         DM_NAKED             = 1 <<  3,
70         DM_MICROSOFT_INLINE  = 1 <<  4,
71         DM_FORCEINLINE       = 1 <<  5,
72         DM_SELECTANY         = 1 <<  6,
73         DM_NOTHROW           = 1 <<  7,
74         DM_NOVTABLE          = 1 <<  8,
75         DM_NORETURN          = 1 <<  9,
76         DM_NOINLINE          = 1 << 10,
77         DM_RESTRICT          = 1 << 11,
78         DM_NOALIAS           = 1 << 12,
79         DM_TRANSPARENT_UNION = 1 << 13,
80         DM_CONST             = 1 << 14,
81         DM_PURE              = 1 << 15,
82         DM_CONSTRUCTOR       = 1 << 16,
83         DM_DESTRUCTOR        = 1 << 17,
84         DM_UNUSED            = 1 << 18,
85         DM_USED              = 1 << 19,
86         DM_CDECL             = 1 << 20,
87         DM_FASTCALL          = 1 << 21,
88         DM_STDCALL           = 1 << 22,
89         DM_THISCALL          = 1 << 23,
90         DM_DEPRECATED        = 1 << 24,
91         DM_RETURNS_TWICE     = 1 << 25,
92         DM_MALLOC            = 1 << 26,
93 } decl_modifier_t;
94
95 /**
96  * A scope containing entities.
97  */
98 struct scope_t {
99         entity_t *entities;
100         entity_t *last_entity;
101         unsigned  depth;        /**< while parsing, the depth of this scope in the
102                                      scope stack. */
103 };
104
105 /**
106  * a named entity is something which can be referenced by its name
107  * (a symbol)
108  */
109 struct entity_base_t {
110         entity_kind_t       kind;
111         entity_namespace_t  namespc;
112         symbol_t           *symbol;
113         source_position_t   source_position;
114         scope_t            *parent_scope;    /**< The scope where this entity
115                                                                                       is contained in */
116
117         /** next declaration in a scope */
118         entity_t           *next;
119         /** next declaration with same symbol */
120         entity_t           *symbol_next;
121 };
122
123 struct compound_t {
124         entity_base_t     base;
125         entity_t         *alias; /* used for name mangling of anonymous types */
126         scope_t           members;
127         decl_modifiers_t  modifiers;
128         bool              complete          : 1;
129         bool              transparent_union : 1;
130         bool              packed            : 1;
131
132         il_alignment_t    alignment;
133         il_size_t         size;
134
135         /* ast2firm info */
136         ir_type          *irtype;
137         bool              irtype_complete : 1;
138 };
139
140 struct enum_t {
141         entity_base_t  base;
142         entity_t      *alias; /* used for name mangling of anonymous types */
143         bool           complete : 1;
144
145         /* ast2firm info */
146         ir_type       *irtype;
147 };
148
149 struct enum_value_t {
150         entity_base_t  base;
151         expression_t  *value;
152         type_t        *enum_type;
153
154         /* ast2firm info */
155         tarval        *tv;
156 };
157
158 struct label_t {
159         entity_base_t  base;
160         bool           used : 1;
161         bool           address_taken : 1;
162         statement_t   *statement;
163
164         /* ast2firm info */
165         ir_node       *block;
166 };
167
168 struct namespace_t {
169         entity_base_t  base;
170         scope_t        members;
171 };
172
173 struct typedef_t {
174         entity_base_t     base;
175         decl_modifiers_t  modifiers;
176         type_t           *type;
177         il_alignment_t    alignment;
178         bool              builtin : 1;
179 };
180
181 struct declaration_t {
182         entity_base_t     base;
183         type_t           *type;
184         storage_class_t   declared_storage_class;
185         storage_class_t   storage_class;
186         decl_modifiers_t  modifiers;
187         il_alignment_t    alignment;
188         attribute_t      *attributes;
189         bool              used     : 1;  /**< Set if the declaration is used. */
190         bool              implicit : 1;  /**< Set for implicit (not found in source code) declarations. */
191
192         /* ast2firm info */
193         unsigned char     kind;
194 };
195
196 struct compound_member_t {
197         declaration_t  base;
198         bool           read          : 1;
199         bool           address_taken : 1;  /**< Set if the address of this declaration was taken. */
200         unsigned short offset;     /**< the offset of this member in the compound */
201         unsigned char  bit_offset; /**< extra bit offset for bitfield members */
202
203         /* ast2firm info */
204         ir_entity *entity;
205 };
206
207 struct variable_t {
208         declaration_t     base;
209         bool              thread_local  : 1;  /**< GCC __thread */
210         bool              restricta     : 1;
211         bool              deprecated    : 1;
212         bool              noalias       : 1;
213
214         bool              address_taken : 1;  /**< Set if the address of this declaration was taken. */
215         bool              read          : 1;
216
217         initializer_t    *initializer;
218
219         /* ast2firm info */
220         union {
221                 unsigned int  value_number;
222                 ir_entity    *entity;
223                 ir_node      *vla_base;
224         } v;
225 };
226
227 struct parameter_t {
228         declaration_t  base;
229         bool           address_taken : 1;
230         bool           read          : 1;
231
232         /* ast2firm info */
233         union {
234                 unsigned int  value_number;
235                 ir_entity    *entity;
236         } v;
237 };
238
239 /**
240  * GNU builtin or MS intrinsic functions.
241  */
242 typedef enum builtin_kind_t {
243         bk_none = 0,                   /**< no builtin */
244         bk_gnu_builtin_alloca,         /**< GNU __builtin_alloca */
245         bk_gnu_builtin_huge_val,       /**< GNU __builtin_huge_val */
246         bk_gnu_builtin_inf,            /**< GNU __builtin_inf */
247         bk_gnu_builtin_inff,           /**< GNU __builtin_inff */
248         bk_gnu_builtin_infl,           /**< GNU __builtin_infl */
249         bk_gnu_builtin_nan,            /**< GNU __builtin_nan */
250         bk_gnu_builtin_nanf,           /**< GNU __builtin_nanf */
251         bk_gnu_builtin_nanl,           /**< GNU __builtin_nanl */
252         bk_gnu_builtin_va_end,         /**< GNU __builtin_va_end */
253         bk_gnu_builtin_expect,         /**< GNU __builtin_expect */
254         bk_gnu_builtin_return_address, /**< GNU __builtin_return_address */
255         bk_gnu_builtin_frame_address,  /**< GNU __builtin_frame_address */
256         bk_gnu_builtin_ffs,            /**< GNU __builtin_ffs */
257         bk_gnu_builtin_clz,            /**< GNU __builtin_clz */
258         bk_gnu_builtin_ctz,            /**< GNU __builtin_ctz */
259         bk_gnu_builtin_popcount,       /**< GNU __builtin_popcount */
260         bk_gnu_builtin_parity,         /**< GNU __builtin_parity */
261         bk_gnu_builtin_prefetch,       /**< GNU __builtin_prefetch */
262         bk_gnu_builtin_trap,           /**< GNU __builtin_trap */
263
264         bk_ms_rotl,                    /**< MS _rotl */
265         bk_ms_rotr,                    /**< MS _rotr */
266         bk_ms_rotl64,                  /**< MS _rotl64 */
267         bk_ms_rotr64,                  /**< MS _rotr64 */
268         bk_ms_byteswap_ushort,         /**< MS _byteswap_ushort */
269         bk_ms_byteswap_ulong,          /**< MS _byteswap_ulong */
270         bk_ms_byteswap_uint64,         /**< MS _byteswap_uint64 */
271
272         bk_ms__debugbreak,             /**< MS __debugbreak */
273         bk_ms_ReturnAddress,           /**< MS _ReturnAddress */
274         bk_ms_AddressOfReturnAddress,  /**< MS _AddressOfReturnAddress */
275         bk_ms__popcount,               /**< MS __popcount */
276         bk_ms_enable,                  /**< MS _enable */
277         bk_ms_disable,                 /**< MS _disable */
278         bk_ms__inbyte,                 /**< MS __inbyte */
279         bk_ms__inword,                 /**< MS __inword */
280         bk_ms__indword,                /**< MS __indword */
281         bk_ms__outbyte,                /**< MS __outbyte */
282         bk_ms__outword,                /**< MS __outword */
283         bk_ms__outdword,               /**< MS __outdword */
284         bk_ms__ud2,                    /**< MS __ud2 */
285         bk_ms_BitScanForward,          /**< MS _BitScanForward */
286         bk_ms_BitScanReverse,          /**< MS _BitScanReverse */
287         bk_ms_InterlockedExchange,     /**< MS _InterlockedExchange */
288         bk_ms_InterlockedExchange64,   /**< MS _InterlockedExchange64 */
289         bk_ms__readeflags,             /**< MS __readflags */
290         bk_ms__writeeflags,            /**< MS __writeflags */
291 } builtin_kind_t;
292
293 struct function_t {
294         declaration_t  base;
295         bool           is_inline     : 1;
296
297         bool           need_closure  : 1;  /**< Inner function needs closure. */
298         bool           goto_to_outer : 1;  /**< Inner function has goto to outer function. */
299
300         builtin_kind_t btk;
301         scope_t        parameters;
302         statement_t   *statement;
303
304         /* ast2firm info */
305         ir_entity     *entity;
306         ir_node       *static_link;        /**< if need_closure is set, the node representing
307                                                                                     the static link. */
308 };
309
310 union entity_t {
311         entity_kind_t      kind;
312         entity_base_t      base;
313         compound_t         structe;
314         compound_t         unione;
315         compound_t         compound;
316         enum_t             enume;
317         enum_value_t       enum_value;
318         label_t            label;
319         namespace_t        namespacee;
320         typedef_t          typedefe;
321         declaration_t      declaration;
322         variable_t         variable;
323         parameter_t        parameter;
324         function_t         function;
325         compound_member_t  compound_member;
326 };
327
328 #define DECLARATION_KIND_CASES        \
329         case ENTITY_FUNCTION:             \
330         case ENTITY_VARIABLE:             \
331         case ENTITY_PARAMETER:            \
332         case ENTITY_COMPOUND_MEMBER:
333
334 static inline bool is_declaration(const entity_t *entity)
335 {
336         switch(entity->kind) {
337         DECLARATION_KIND_CASES
338                 return true;
339         default:
340                 return false;
341         }
342 }
343
344 const char *get_entity_kind_name(entity_kind_t kind);
345
346 #endif