The variants of advance_current_object() and descend_into_subtype() in ast2firm must...
[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 #include "builtins.h"
29
30 typedef enum {
31         ENTITY_VARIABLE = 1,
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_NORMAL = 1,
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         DM_WEAK              = 1 << 27,
94 } decl_modifier_t;
95
96 typedef enum elf_visibility_tag_t {
97         ELF_VISIBILITY_DEFAULT,
98         ELF_VISIBILITY_HIDDEN,
99         ELF_VISIBILITY_INTERNAL,
100         ELF_VISIBILITY_PROTECTED,
101         ELF_VISIBILITY_ERROR
102 } elf_visibility_tag_t;
103
104 /**
105  * A scope containing entities.
106  */
107 struct scope_t {
108         entity_t *entities;
109         entity_t *last_entity; /**< pointer to last entity (so appending is fast) */
110         unsigned  depth;       /**< while parsing, the depth of this scope in the
111                                     scope stack. */
112 };
113
114 /**
115  * a named entity is something which can be referenced by its name
116  * (a symbol)
117  */
118 struct entity_base_t {
119         entity_kind_t       kind;
120         entity_namespace_t  namespc;
121         symbol_t           *symbol;
122         source_position_t   source_position;
123         scope_t            *parent_scope;    /**< The scope where this entity
124                                                                                       is contained in */
125         entity_t           *parent_entity;
126
127         /** next declaration in a scope */
128         entity_t           *next;
129         /** next declaration with same symbol */
130         entity_t           *symbol_next;
131 };
132
133 struct compound_t {
134         entity_base_t     base;
135         entity_t         *alias; /* used for name mangling of anonymous types */
136         scope_t           members;
137         decl_modifiers_t  modifiers;
138         bool              layouted          : 1;
139         bool              complete          : 1;
140         bool              transparent_union : 1;
141         bool              packed            : 1;
142
143         il_alignment_t    alignment;
144         il_size_t         size;
145
146         /* ast2firm info */
147         ir_type          *irtype;
148         bool              irtype_complete : 1;
149 };
150
151 struct enum_t {
152         entity_base_t  base;
153         entity_t      *alias; /* used for name mangling of anonymous types */
154         bool           complete : 1;
155
156         /* ast2firm info */
157         ir_type       *irtype;
158 };
159
160 struct enum_value_t {
161         entity_base_t  base;
162         expression_t  *value;
163         type_t        *enum_type;
164
165         /* ast2firm info */
166         ir_tarval     *tv;
167 };
168
169 struct label_t {
170         entity_base_t  base;
171         bool           used : 1;
172         bool           address_taken : 1;
173         statement_t   *statement;
174
175         /* ast2firm info */
176         ir_node       *block;
177 };
178
179 struct namespace_t {
180         entity_base_t  base;
181         scope_t        members;
182 };
183
184 struct typedef_t {
185         entity_base_t     base;
186         decl_modifiers_t  modifiers;
187         type_t           *type;
188         il_alignment_t    alignment;
189         bool              builtin : 1;
190 };
191
192 struct declaration_t {
193         entity_base_t     base;
194         type_t           *type;
195         storage_class_t   declared_storage_class;
196         storage_class_t   storage_class;
197         decl_modifiers_t  modifiers;
198         il_alignment_t    alignment;
199         attribute_t      *attributes;
200         bool              used     : 1;  /**< Set if the declaration is used. */
201         bool              implicit : 1;  /**< Set for implicit (not found in source code) declarations. */
202
203         /* ast2firm info */
204         unsigned char     kind;
205 };
206
207 struct compound_member_t {
208         declaration_t  base;
209         il_size_t      offset;     /**< the offset of this member in the compound */
210         unsigned char  bit_offset; /**< extra bit offset for bitfield members */
211         unsigned char  bit_size;   /**< bitsize for bitfield members */
212         bool           bitfield      : 1;  /**< member is (part of) a bitfield */
213         bool           read          : 1;
214         bool           address_taken : 1;  /**< Set if the address of this
215                                                 declaration was taken. */
216
217         /* ast2firm info */
218         ir_entity *entity;
219 };
220
221 struct variable_t {
222         declaration_t     base;
223         bool              thread_local   : 1;  /**< GCC __thread */
224         bool              restricta      : 1;
225         bool              deprecated     : 1;
226         bool              noalias        : 1;
227
228         bool              address_taken  : 1;  /**< Set if the address of this declaration was taken. */
229         bool              read           : 1;
230         unsigned          elf_visibility : 2;
231
232         initializer_t    *initializer;
233
234         /* ast2firm info */
235         union {
236                 unsigned int  value_number;
237                 ir_entity    *entity;
238                 ir_node      *vla_base;
239         } v;
240 };
241
242 struct parameter_t {
243         declaration_t  base;
244         bool           address_taken : 1;
245         bool           read          : 1;
246
247         /* ast2firm info */
248         union {
249                 unsigned int  value_number;
250                 ir_entity    *entity;
251         } v;
252 };
253
254 struct function_t {
255         declaration_t  base;
256         bool           is_inline      : 1;
257
258         bool           need_closure   : 1;  /**< Inner function needs closure. */
259         bool           goto_to_outer  : 1;  /**< Inner function has goto to outer function. */
260         unsigned       elf_visibility : 2;
261
262         builtin_kind_t btk;
263         scope_t        parameters;
264         statement_t   *statement;
265         symbol_t      *actual_name;        /**< gnu extension __REDIRECT */
266
267         /* ast2firm info */
268         union {
269                 ir_builtin_kind firm_builtin_kind;
270                 unsigned        chk_arg_pos;
271         } b;
272         ir_entity      *irentity;
273         ir_node        *static_link;        /**< if need_closure is set, the node
274                                                  representing the static link. */
275 };
276
277 union entity_t {
278         entity_kind_t      kind;
279         entity_base_t      base;
280         compound_t         compound;
281         enum_t             enume;
282         enum_value_t       enum_value;
283         label_t            label;
284         namespace_t        namespacee;
285         typedef_t          typedefe;
286         declaration_t      declaration;
287         variable_t         variable;
288         parameter_t        parameter;
289         function_t         function;
290         compound_member_t  compound_member;
291 };
292
293 #define DECLARATION_KIND_CASES        \
294         case ENTITY_FUNCTION:             \
295         case ENTITY_VARIABLE:             \
296         case ENTITY_PARAMETER:            \
297         case ENTITY_COMPOUND_MEMBER:
298
299 static inline bool is_declaration(const entity_t *entity)
300 {
301         switch(entity->kind) {
302         DECLARATION_KIND_CASES
303                 return true;
304         default:
305                 return false;
306         }
307 }
308
309 const char *get_entity_kind_name(entity_kind_t kind);
310
311 entity_t *allocate_entity_zero(entity_kind_t, entity_namespace_t, symbol_t*);
312
313 elf_visibility_tag_t get_elf_visibility_from_string(const char *string);
314
315 entity_t *skip_unnamed_bitfields(entity_t*);
316
317 #endif