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