some more work towards (c++) namespace support
[cparser] / entity_t.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 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_CLASS,
37         ENTITY_STRUCT,
38         ENTITY_UNION,
39         ENTITY_ENUM,
40         ENTITY_ENUM_VALUE,
41         ENTITY_LABEL,
42         ENTITY_LOCAL_LABEL,
43         ENTITY_NAMESPACE
44 } entity_kind_tag_t;
45 typedef unsigned char entity_kind_t;
46
47 typedef enum namespace_tag_t {
48         NAMESPACE_INVALID,
49         NAMESPACE_NORMAL,
50         NAMESPACE_TAG,
51         NAMESPACE_LABEL
52 } namespace_tag_t;
53 typedef unsigned char entity_namespace_t;
54
55 typedef enum storage_class_tag_t {
56         STORAGE_CLASS_NONE,
57         STORAGE_CLASS_EXTERN,
58         STORAGE_CLASS_STATIC,
59         STORAGE_CLASS_TYPEDEF,
60         STORAGE_CLASS_AUTO,
61         STORAGE_CLASS_REGISTER,
62 } storage_class_tag_t;
63 typedef unsigned char storage_class_t;
64
65 typedef enum decl_modifier_t {
66         DM_NONE              = 0,
67         DM_DLLIMPORT         = 1 <<  0,
68         DM_DLLEXPORT         = 1 <<  1,
69         DM_THREAD            = 1 <<  2,
70         DM_NAKED             = 1 <<  3,
71         DM_MICROSOFT_INLINE  = 1 <<  4,
72         DM_FORCEINLINE       = 1 <<  5,
73         DM_SELECTANY         = 1 <<  6,
74         DM_NOTHROW           = 1 <<  7,
75         DM_NOVTABLE          = 1 <<  8,
76         DM_NORETURN          = 1 <<  9,
77         DM_NOINLINE          = 1 << 10,
78         DM_RESTRICT          = 1 << 11,
79         DM_NOALIAS           = 1 << 12,
80         DM_TRANSPARENT_UNION = 1 << 13,
81         DM_CONST             = 1 << 14,
82         DM_PURE              = 1 << 15,
83         DM_CONSTRUCTOR       = 1 << 16,
84         DM_DESTRUCTOR        = 1 << 17,
85         DM_UNUSED            = 1 << 18,
86         DM_USED              = 1 << 19,
87         DM_CDECL             = 1 << 20,
88         DM_FASTCALL          = 1 << 21,
89         DM_STDCALL           = 1 << 22,
90         DM_THISCALL          = 1 << 23,
91         DM_DEPRECATED        = 1 << 24,
92         DM_RETURNS_TWICE     = 1 << 25,
93         DM_MALLOC            = 1 << 26,
94 } decl_modifier_t;
95
96 /**
97  * A scope containing entities.
98  */
99 struct scope_t {
100         entity_t *entities;
101         entity_t *last_entity; /**< pointer to last entity (so appending is fast) */
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              layouted          : 1;
130         bool              complete          : 1;
131         bool              transparent_union : 1;
132         bool              packed            : 1;
133
134         il_alignment_t    alignment;
135         il_size_t         size;
136
137         /* ast2firm info */
138         ir_type          *irtype;
139         bool              irtype_complete : 1;
140 };
141
142 struct enum_t {
143         entity_base_t  base;
144         entity_t      *alias; /* used for name mangling of anonymous types */
145         bool           complete : 1;
146
147         /* ast2firm info */
148         ir_type       *irtype;
149 };
150
151 struct enum_value_t {
152         entity_base_t  base;
153         expression_t  *value;
154         type_t        *enum_type;
155
156         /* ast2firm info */
157         tarval        *tv;
158 };
159
160 struct label_t {
161         entity_base_t  base;
162         bool           used : 1;
163         bool           address_taken : 1;
164         statement_t   *statement;
165
166         /* ast2firm info */
167         ir_node       *block;
168 };
169
170 struct namespace_t {
171         entity_base_t  base;
172         scope_t        members;
173 };
174
175 struct typedef_t {
176         entity_base_t     base;
177         decl_modifiers_t  modifiers;
178         type_t           *type;
179         il_alignment_t    alignment;
180         bool              builtin : 1;
181 };
182
183 struct declaration_t {
184         entity_base_t     base;
185         type_t           *type;
186         storage_class_t   declared_storage_class;
187         storage_class_t   storage_class;
188         decl_modifiers_t  modifiers;
189         il_alignment_t    alignment;
190         attribute_t      *attributes;
191         bool              used     : 1;  /**< Set if the declaration is used. */
192         bool              implicit : 1;  /**< Set for implicit (not found in source code) declarations. */
193
194         /* ast2firm info */
195         unsigned char     kind;
196 };
197
198 struct compound_member_t {
199         declaration_t  base;
200         bool           read          : 1;
201         bool           address_taken : 1;  /**< Set if the address of this declaration was taken. */
202         unsigned short offset;     /**< the offset of this member in the compound */
203         unsigned char  bit_offset; /**< extra bit offset for bitfield members */
204
205         /* ast2firm info */
206         ir_entity *entity;
207 };
208
209 struct variable_t {
210         declaration_t     base;
211         bool              thread_local  : 1;  /**< GCC __thread */
212         bool              restricta     : 1;
213         bool              deprecated    : 1;
214         bool              noalias       : 1;
215
216         bool              address_taken : 1;  /**< Set if the address of this declaration was taken. */
217         bool              read          : 1;
218
219         initializer_t    *initializer;
220
221         /* ast2firm info */
222         union {
223                 unsigned int  value_number;
224                 ir_entity    *entity;
225                 ir_node      *vla_base;
226         } v;
227 };
228
229 struct parameter_t {
230         declaration_t  base;
231         bool           address_taken : 1;
232         bool           read          : 1;
233
234         /* ast2firm info */
235         union {
236                 unsigned int  value_number;
237                 ir_entity    *entity;
238         } v;
239 };
240
241 /**
242  * GNU builtin or MS intrinsic functions.
243  */
244 typedef enum builtin_kind_t {
245         bk_none = 0,                   /**< no builtin */
246         bk_gnu_builtin_alloca,         /**< GNU __builtin_alloca */
247         bk_gnu_builtin_huge_val,       /**< GNU __builtin_huge_val */
248         bk_gnu_builtin_inf,            /**< GNU __builtin_inf */
249         bk_gnu_builtin_inff,           /**< GNU __builtin_inff */
250         bk_gnu_builtin_infl,           /**< GNU __builtin_infl */
251         bk_gnu_builtin_nan,            /**< GNU __builtin_nan */
252         bk_gnu_builtin_nanf,           /**< GNU __builtin_nanf */
253         bk_gnu_builtin_nanl,           /**< GNU __builtin_nanl */
254         bk_gnu_builtin_va_end,         /**< GNU __builtin_va_end */
255         bk_gnu_builtin_expect,         /**< GNU __builtin_expect */
256         bk_gnu_builtin_return_address, /**< GNU __builtin_return_address */
257         bk_gnu_builtin_frame_address,  /**< GNU __builtin_frame_address */
258         bk_gnu_builtin_ffs,            /**< GNU __builtin_ffs */
259         bk_gnu_builtin_clz,            /**< GNU __builtin_clz */
260         bk_gnu_builtin_ctz,            /**< GNU __builtin_ctz */
261         bk_gnu_builtin_popcount,       /**< GNU __builtin_popcount */
262         bk_gnu_builtin_parity,         /**< GNU __builtin_parity */
263         bk_gnu_builtin_prefetch,       /**< GNU __builtin_prefetch */
264         bk_gnu_builtin_trap,           /**< GNU __builtin_trap */
265
266         bk_ms_rotl,                    /**< MS _rotl */
267         bk_ms_rotr,                    /**< MS _rotr */
268         bk_ms_rotl64,                  /**< MS _rotl64 */
269         bk_ms_rotr64,                  /**< MS _rotr64 */
270         bk_ms_byteswap_ushort,         /**< MS _byteswap_ushort */
271         bk_ms_byteswap_ulong,          /**< MS _byteswap_ulong */
272         bk_ms_byteswap_uint64,         /**< MS _byteswap_uint64 */
273
274         bk_ms__debugbreak,             /**< MS __debugbreak */
275         bk_ms_ReturnAddress,           /**< MS _ReturnAddress */
276         bk_ms_AddressOfReturnAddress,  /**< MS _AddressOfReturnAddress */
277         bk_ms__popcount,               /**< MS __popcount */
278         bk_ms_enable,                  /**< MS _enable */
279         bk_ms_disable,                 /**< MS _disable */
280         bk_ms__inbyte,                 /**< MS __inbyte */
281         bk_ms__inword,                 /**< MS __inword */
282         bk_ms__indword,                /**< MS __indword */
283         bk_ms__outbyte,                /**< MS __outbyte */
284         bk_ms__outword,                /**< MS __outword */
285         bk_ms__outdword,               /**< MS __outdword */
286         bk_ms__ud2,                    /**< MS __ud2 */
287         bk_ms_BitScanForward,          /**< MS _BitScanForward */
288         bk_ms_BitScanReverse,          /**< MS _BitScanReverse */
289         bk_ms_InterlockedExchange,     /**< MS _InterlockedExchange */
290         bk_ms_InterlockedExchange64,   /**< MS _InterlockedExchange64 */
291         bk_ms__readeflags,             /**< MS __readflags */
292         bk_ms__writeeflags,            /**< MS __writeflags */
293 } builtin_kind_t;
294
295 struct function_t {
296         declaration_t  base;
297         bool           is_inline     : 1;
298
299         bool           need_closure  : 1;  /**< Inner function needs closure. */
300         bool           goto_to_outer : 1;  /**< Inner function has goto to outer function. */
301
302         builtin_kind_t btk;
303         scope_t        parameters;
304         statement_t   *statement;
305
306         /* ast2firm info */
307         ir_entity     *irentity;
308         ir_node       *static_link;        /**< if need_closure is set, the node representing
309                                                                                     the static link. */
310 };
311
312 union entity_t {
313         entity_kind_t      kind;
314         entity_base_t      base;
315         compound_t         structe;
316         compound_t         unione;
317         compound_t         compound;
318         enum_t             enume;
319         enum_value_t       enum_value;
320         label_t            label;
321         namespace_t        namespacee;
322         typedef_t          typedefe;
323         declaration_t      declaration;
324         variable_t         variable;
325         parameter_t        parameter;
326         function_t         function;
327         compound_member_t  compound_member;
328 };
329
330 #define DECLARATION_KIND_CASES        \
331         case ENTITY_FUNCTION:             \
332         case ENTITY_VARIABLE:             \
333         case ENTITY_PARAMETER:            \
334         case ENTITY_COMPOUND_MEMBER:
335
336 static inline bool is_declaration(const entity_t *entity)
337 {
338         switch(entity->kind) {
339         DECLARATION_KIND_CASES
340                 return true;
341         default:
342                 return false;
343         }
344 }
345
346 const char *get_entity_kind_name(entity_kind_t kind);
347
348 #endif