Implement __builtin_huge_valf and __builtin_huge_vall.
[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         DM_WEAK              = 1 << 27,
95 } decl_modifier_t;
96
97 /**
98  * A scope containing entities.
99  */
100 struct scope_t {
101         entity_t *entities;
102         entity_t *last_entity; /**< pointer to last entity (so appending is fast) */
103         unsigned  depth;       /**< while parsing, the depth of this scope in the
104                                     scope stack. */
105 };
106
107 /**
108  * a named entity is something which can be referenced by its name
109  * (a symbol)
110  */
111 struct entity_base_t {
112         entity_kind_t       kind;
113         entity_namespace_t  namespc;
114         symbol_t           *symbol;
115         source_position_t   source_position;
116         scope_t            *parent_scope;    /**< The scope where this entity
117                                                                                       is contained in */
118         entity_t           *parent_entity;
119
120         /** next declaration in a scope */
121         entity_t           *next;
122         /** next declaration with same symbol */
123         entity_t           *symbol_next;
124 };
125
126 struct compound_t {
127         entity_base_t     base;
128         entity_t         *alias; /* used for name mangling of anonymous types */
129         scope_t           members;
130         decl_modifiers_t  modifiers;
131         bool              layouted          : 1;
132         bool              complete          : 1;
133         bool              transparent_union : 1;
134         bool              packed            : 1;
135
136         il_alignment_t    alignment;
137         il_size_t         size;
138
139         /* ast2firm info */
140         ir_type          *irtype;
141         bool              irtype_complete : 1;
142 };
143
144 struct enum_t {
145         entity_base_t  base;
146         entity_t      *alias; /* used for name mangling of anonymous types */
147         bool           complete : 1;
148
149         /* ast2firm info */
150         ir_type       *irtype;
151 };
152
153 struct enum_value_t {
154         entity_base_t  base;
155         expression_t  *value;
156         type_t        *enum_type;
157
158         /* ast2firm info */
159         tarval        *tv;
160 };
161
162 struct label_t {
163         entity_base_t  base;
164         bool           used : 1;
165         bool           address_taken : 1;
166         statement_t   *statement;
167
168         /* ast2firm info */
169         ir_node       *block;
170 };
171
172 struct namespace_t {
173         entity_base_t  base;
174         scope_t        members;
175 };
176
177 struct typedef_t {
178         entity_base_t     base;
179         decl_modifiers_t  modifiers;
180         type_t           *type;
181         il_alignment_t    alignment;
182         bool              builtin : 1;
183 };
184
185 struct declaration_t {
186         entity_base_t     base;
187         type_t           *type;
188         storage_class_t   declared_storage_class;
189         storage_class_t   storage_class;
190         decl_modifiers_t  modifiers;
191         il_alignment_t    alignment;
192         attribute_t      *attributes;
193         bool              used     : 1;  /**< Set if the declaration is used. */
194         bool              implicit : 1;  /**< Set for implicit (not found in source code) declarations. */
195
196         /* ast2firm info */
197         unsigned char     kind;
198 };
199
200 struct compound_member_t {
201         declaration_t  base;
202         bool           read          : 1;
203         bool           address_taken : 1;  /**< Set if the address of this declaration was taken. */
204         unsigned short offset;     /**< the offset of this member in the compound */
205         unsigned char  bit_offset; /**< extra bit offset for bitfield members */
206
207         /* ast2firm info */
208         ir_entity *entity;
209 };
210
211 struct variable_t {
212         declaration_t     base;
213         bool              thread_local  : 1;  /**< GCC __thread */
214         bool              restricta     : 1;
215         bool              deprecated    : 1;
216         bool              noalias       : 1;
217
218         bool              address_taken : 1;  /**< Set if the address of this declaration was taken. */
219         bool              read          : 1;
220
221         initializer_t    *initializer;
222
223         /* ast2firm info */
224         union {
225                 unsigned int  value_number;
226                 ir_entity    *entity;
227                 ir_node      *vla_base;
228         } v;
229 };
230
231 struct parameter_t {
232         declaration_t  base;
233         bool           address_taken : 1;
234         bool           read          : 1;
235
236         /* ast2firm info */
237         union {
238                 unsigned int  value_number;
239                 ir_entity    *entity;
240         } v;
241 };
242
243 /**
244  * GNU builtin or MS intrinsic functions.
245  */
246 typedef enum builtin_kind_t {
247         bk_none = 0,                   /**< no builtin */
248         bk_gnu_builtin_alloca,         /**< GNU __builtin_alloca */
249         bk_gnu_builtin_huge_val,       /**< GNU __builtin_huge_val */
250         bk_gnu_builtin_huge_valf,      /**< GNU __builtin_huge_valf */
251         bk_gnu_builtin_huge_vall,      /**< GNU __builtin_huge_vall */
252         bk_gnu_builtin_inf,            /**< GNU __builtin_inf */
253         bk_gnu_builtin_inff,           /**< GNU __builtin_inff */
254         bk_gnu_builtin_infl,           /**< GNU __builtin_infl */
255         bk_gnu_builtin_nan,            /**< GNU __builtin_nan */
256         bk_gnu_builtin_nanf,           /**< GNU __builtin_nanf */
257         bk_gnu_builtin_nanl,           /**< GNU __builtin_nanl */
258         bk_gnu_builtin_va_end,         /**< GNU __builtin_va_end */
259         bk_gnu_builtin_expect,         /**< GNU __builtin_expect */
260         bk_gnu_builtin_return_address, /**< GNU __builtin_return_address */
261         bk_gnu_builtin_frame_address,  /**< GNU __builtin_frame_address */
262         bk_gnu_builtin_ffs,            /**< GNU __builtin_ffs */
263         bk_gnu_builtin_clz,            /**< GNU __builtin_clz */
264         bk_gnu_builtin_ctz,            /**< GNU __builtin_ctz */
265         bk_gnu_builtin_popcount,       /**< GNU __builtin_popcount */
266         bk_gnu_builtin_parity,         /**< GNU __builtin_parity */
267         bk_gnu_builtin_prefetch,       /**< GNU __builtin_prefetch */
268         bk_gnu_builtin_trap,           /**< GNU __builtin_trap */
269
270         bk_ms_rotl,                    /**< MS _rotl */
271         bk_ms_rotr,                    /**< MS _rotr */
272         bk_ms_rotl64,                  /**< MS _rotl64 */
273         bk_ms_rotr64,                  /**< MS _rotr64 */
274         bk_ms_byteswap_ushort,         /**< MS _byteswap_ushort */
275         bk_ms_byteswap_ulong,          /**< MS _byteswap_ulong */
276         bk_ms_byteswap_uint64,         /**< MS _byteswap_uint64 */
277
278         bk_ms__debugbreak,             /**< MS __debugbreak */
279         bk_ms_ReturnAddress,           /**< MS _ReturnAddress */
280         bk_ms_AddressOfReturnAddress,  /**< MS _AddressOfReturnAddress */
281         bk_ms__popcount,               /**< MS __popcount */
282         bk_ms_enable,                  /**< MS _enable */
283         bk_ms_disable,                 /**< MS _disable */
284         bk_ms__inbyte,                 /**< MS __inbyte */
285         bk_ms__inword,                 /**< MS __inword */
286         bk_ms__indword,                /**< MS __indword */
287         bk_ms__outbyte,                /**< MS __outbyte */
288         bk_ms__outword,                /**< MS __outword */
289         bk_ms__outdword,               /**< MS __outdword */
290         bk_ms__ud2,                    /**< MS __ud2 */
291         bk_ms_BitScanForward,          /**< MS _BitScanForward */
292         bk_ms_BitScanReverse,          /**< MS _BitScanReverse */
293         bk_ms_InterlockedExchange,     /**< MS _InterlockedExchange */
294         bk_ms_InterlockedExchange64,   /**< MS _InterlockedExchange64 */
295         bk_ms__readeflags,             /**< MS __readflags */
296         bk_ms__writeeflags,            /**< MS __writeflags */
297 } builtin_kind_t;
298
299 struct function_t {
300         declaration_t  base;
301         bool           is_inline     : 1;
302
303         bool           need_closure  : 1;  /**< Inner function needs closure. */
304         bool           goto_to_outer : 1;  /**< Inner function has goto to outer function. */
305
306         builtin_kind_t btk;
307         scope_t        parameters;
308         statement_t   *statement;
309
310         /* ast2firm info */
311         ir_entity     *irentity;
312         ir_node       *static_link;        /**< if need_closure is set, the node
313                                                 representing the static link. */
314 };
315
316 union entity_t {
317         entity_kind_t      kind;
318         entity_base_t      base;
319         compound_t         structe;
320         compound_t         unione;
321         compound_t         compound;
322         enum_t             enume;
323         enum_value_t       enum_value;
324         label_t            label;
325         namespace_t        namespacee;
326         typedef_t          typedefe;
327         declaration_t      declaration;
328         variable_t         variable;
329         parameter_t        parameter;
330         function_t         function;
331         compound_member_t  compound_member;
332 };
333
334 #define DECLARATION_KIND_CASES        \
335         case ENTITY_FUNCTION:             \
336         case ENTITY_VARIABLE:             \
337         case ENTITY_PARAMETER:            \
338         case ENTITY_COMPOUND_MEMBER:
339
340 static inline bool is_declaration(const entity_t *entity)
341 {
342         switch(entity->kind) {
343         DECLARATION_KIND_CASES
344                 return true;
345         default:
346                 return false;
347         }
348 }
349
350 const char *get_entity_kind_name(entity_kind_t kind);
351
352 #endif