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