Removed EXPR_BUILTIN_SYMBOL: Builtins are now predefined functions
[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_STRUCT,
49         NAMESPACE_UNION,
50         NAMESPACE_ENUM,
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_PACKED            = 1 << 13,
81         DM_TRANSPARENT_UNION = 1 << 14,
82         DM_CONST             = 1 << 15,
83         DM_PURE              = 1 << 16,
84         DM_CONSTRUCTOR       = 1 << 17,
85         DM_DESTRUCTOR        = 1 << 18,
86         DM_UNUSED            = 1 << 19,
87         DM_USED              = 1 << 20,
88         DM_CDECL             = 1 << 21,
89         DM_FASTCALL          = 1 << 22,
90         DM_STDCALL           = 1 << 23,
91         DM_THISCALL          = 1 << 24,
92         DM_DEPRECATED        = 1 << 25,
93         DM_RETURNS_TWICE     = 1 << 26,
94 } decl_modifier_t;
95
96 typedef unsigned decl_modifiers_t;
97
98 /**
99  * A scope containing entities.
100  */
101 struct scope_t {
102         entity_t *entities;
103         entity_t *last_entity;
104         unsigned  depth;        /**< while parsing, the depth of this scope in the
105                                      scope stack. */
106 };
107
108 /**
109  * a named entity is something which can be referenced by its name
110  * (a symbol)
111  */
112 struct entity_base_t {
113         entity_kind_t       kind;
114         entity_namespace_t  namespc;
115         symbol_t           *symbol;
116         source_position_t   source_position;
117         scope_t            *parent_scope;    /**< The scope where this entity
118                                                                                       is contained in */
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              complete            : 1;
132         bool              has_flexible_member : 1;
133
134         /* ast2firm info */
135         ir_type          *irtype;
136         bool              irtype_complete : 1;
137 };
138
139 struct enum_t {
140         entity_base_t  base;
141         entity_t      *alias; /* used for name mangling of anonymous types */
142         bool           complete : 1;
143
144         /* ast2firm info */
145         ir_type       *irtype;
146 };
147
148 struct enum_value_t {
149         entity_base_t  base;
150         expression_t  *value;
151         type_t        *enum_type;
152
153         /* ast2firm info */
154         tarval        *tv;
155 };
156
157 struct label_t {
158         entity_base_t  base;
159         bool           used : 1;
160         bool           address_taken : 1;
161         statement_t   *statement;
162
163         /* ast2firm info */
164         ir_node       *block;
165 };
166
167 struct namespace_t {
168         entity_base_t  base;
169         scope_t        members;
170 };
171
172 struct typedef_t {
173         entity_base_t     base;
174         decl_modifiers_t  modifiers;
175         type_t           *type;
176         bool              builtin : 1;
177 };
178
179 struct declaration_t {
180         entity_base_t     base;
181         storage_class_t   declared_storage_class;
182         storage_class_t   storage_class;
183         decl_modifiers_t  modifiers;
184         const char       *deprecated_string;  /**< MS deprecated string if any. */
185         bool              used          : 1;  /**< Set if the declaration is used. */
186         bool              implicit      : 1;  /**< Set for implicit (not found in source code) declarations. */
187         type_t           *type;
188
189         /* ast2firm info */
190         unsigned char     kind;
191 };
192
193 struct compound_member_t {
194         declaration_t  base;
195         unsigned char  alignment;
196         bool           read          : 1;
197         bool           address_taken : 1;  /**< Set if the address of this declaration was taken. */
198
199         /* ast2firm info */
200         ir_entity *entity;
201         il_size_t  offset;  /**< The offset of this member inside a compound. */
202 };
203
204 struct variable_t {
205         declaration_t  base;
206         bool           thread_local  : 1;  /**< GCC __thread */
207         bool           address_taken : 1;  /**< Set if the address of this declaration was taken. */
208         bool           read          : 1;
209         unsigned char  alignment;
210         symbol_t      *get_property_sym;   /**< MS get property. */
211         symbol_t      *put_property_sym;   /**< MS put property. */
212
213         initializer_t *initializer;
214
215         /* ast2firm info */
216         union {
217                 unsigned int  value_number;
218                 ir_entity    *entity;
219                 ir_node      *vla_base;
220         } v;
221 };
222
223 struct parameter_t {
224         declaration_t  base;
225         bool           address_taken : 1;
226         bool           read          : 1;
227
228         /* ast2firm info */
229         union {
230                 unsigned int  value_number;
231                 ir_entity    *entity;
232         } v;
233 };
234
235 /**
236  * GNU builtin or MS intrinsic functions.
237  */
238 typedef enum builtin_kind_t {
239         bk_none = 0,                   /**< no builtin */
240         bk_gnu_builtin_alloca,         /**< GNU __builtin_alloca */
241         bk_gnu_builtin_huge_val,       /**< GNU __builtin_huge_val */
242         bk_gnu_builtin_inf,            /**< GNU __builtin_inf */
243         bk_gnu_builtin_inff,           /**< GNU __builtin_inff */
244         bk_gnu_builtin_infl,           /**< GNU __builtin_infl */
245         bk_gnu_builtin_nan,            /**< GNU __builtin_nan */
246         bk_gnu_builtin_nanf,           /**< GNU __builtin_nanf */
247         bk_gnu_builtin_nanl,           /**< GNU __builtin_nanl */
248         bk_gnu_builtin_va_end,         /**< GNU __builtin_va_end */
249         bk_gnu_builtin_expect,         /**< GNU __builtin_expect */
250         bk_gnu_builtin_return_address, /**< GNU __builtin_return_address */
251         bk_gnu_builtin_frame_address,  /**< GNU __builtin_frame_address */
252         bk_gnu_builtin_ffs,            /**< GNU __builtin_ffs */
253         bk_gnu_builtin_clz,            /**< GNU __builtin_clz */
254         bk_gnu_builtin_ctz,            /**< GNU __builtin_ctz */
255         bk_gnu_builtin_popcount,       /**< GNU __builtin_popcount */
256         bk_gnu_builtin_parity,         /**< GNU __builtin_parity */
257         bk_gnu_builtin_prefetch,       /**< GNU __builtin_prefetch */
258         bk_gnu_builtin_trap,           /**< GNU __builtin_trap */
259
260         bk_ms__debugbreak,             /**< MS __debugbreak */
261         bk_ms_ReturnAddress,           /**< MS _ReturnAddress */
262         bk_ms__popcount,               /**< MS __popcount */
263         bk_ms__ud2,                    /**< MS __ud2 */
264 } builtin_kind_t;
265
266 struct function_t {
267         declaration_t  base;
268         bool           is_inline     : 1;
269         bool           need_closure  : 1;  /**< Inner function needs closure. */
270         bool           goto_to_outer : 1;  /**< Inner function has goto to outer function. */
271
272         builtin_kind_t btk;
273         scope_t        parameters;
274         statement_t   *statement;
275
276         /* ast2firm info */
277         ir_entity     *entity;
278 };
279
280 union entity_t {
281         entity_kind_t      kind;
282         entity_base_t      base;
283         compound_t         structe;
284         compound_t         unione;
285         compound_t         compound;
286         enum_t             enume;
287         enum_value_t       enum_value;
288         label_t            label;
289         namespace_t        namespacee;
290         typedef_t          typedefe;
291         declaration_t      declaration;
292         variable_t         variable;
293         parameter_t        parameter;
294         function_t         function;
295         compound_member_t  compound_member;
296 };
297
298 static inline bool is_declaration(const entity_t *entity)
299 {
300         return entity->kind == ENTITY_FUNCTION || entity->kind == ENTITY_VARIABLE
301                 || entity->kind == ENTITY_PARAMETER
302                 || entity->kind == ENTITY_COMPOUND_MEMBER;
303 }
304
305
306 const char *get_entity_kind_name(entity_kind_t kind);
307
308 #endif