bitfields are an entity attribute now, not a type
[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_INVALID,
32         ENTITY_VARIABLE,
33         ENTITY_COMPOUND_MEMBER,
34         ENTITY_PARAMETER,
35         ENTITY_FUNCTION,
36         ENTITY_TYPEDEF,
37         ENTITY_CLASS,
38         ENTITY_STRUCT,
39         ENTITY_UNION,
40         ENTITY_ENUM,
41         ENTITY_ENUM_VALUE,
42         ENTITY_LABEL,
43         ENTITY_LOCAL_LABEL,
44         ENTITY_NAMESPACE
45 } entity_kind_tag_t;
46 typedef unsigned char entity_kind_t;
47
48 typedef enum namespace_tag_t {
49         NAMESPACE_INVALID,
50         NAMESPACE_NORMAL,
51         NAMESPACE_TAG,
52         NAMESPACE_LABEL
53 } namespace_tag_t;
54 typedef unsigned char entity_namespace_t;
55
56 typedef enum storage_class_tag_t {
57         STORAGE_CLASS_NONE,
58         STORAGE_CLASS_EXTERN,
59         STORAGE_CLASS_STATIC,
60         STORAGE_CLASS_TYPEDEF,
61         STORAGE_CLASS_AUTO,
62         STORAGE_CLASS_REGISTER,
63 } storage_class_tag_t;
64 typedef unsigned char storage_class_t;
65
66 typedef enum decl_modifier_t {
67         DM_NONE              = 0,
68         DM_DLLIMPORT         = 1 <<  0,
69         DM_DLLEXPORT         = 1 <<  1,
70         DM_THREAD            = 1 <<  2,
71         DM_NAKED             = 1 <<  3,
72         DM_MICROSOFT_INLINE  = 1 <<  4,
73         DM_FORCEINLINE       = 1 <<  5,
74         DM_SELECTANY         = 1 <<  6,
75         DM_NOTHROW           = 1 <<  7,
76         DM_NOVTABLE          = 1 <<  8,
77         DM_NORETURN          = 1 <<  9,
78         DM_NOINLINE          = 1 << 10,
79         DM_RESTRICT          = 1 << 11,
80         DM_NOALIAS           = 1 << 12,
81         DM_TRANSPARENT_UNION = 1 << 13,
82         DM_CONST             = 1 << 14,
83         DM_PURE              = 1 << 15,
84         DM_CONSTRUCTOR       = 1 << 16,
85         DM_DESTRUCTOR        = 1 << 17,
86         DM_UNUSED            = 1 << 18,
87         DM_USED              = 1 << 19,
88         DM_CDECL             = 1 << 20,
89         DM_FASTCALL          = 1 << 21,
90         DM_STDCALL           = 1 << 22,
91         DM_THISCALL          = 1 << 23,
92         DM_DEPRECATED        = 1 << 24,
93         DM_RETURNS_TWICE     = 1 << 25,
94         DM_MALLOC            = 1 << 26,
95         DM_WEAK              = 1 << 27,
96 } decl_modifier_t;
97
98 typedef enum elf_visibility_tag_t {
99         ELF_VISIBILITY_DEFAULT,
100         ELF_VISIBILITY_HIDDEN,
101         ELF_VISIBILITY_INTERNAL,
102         ELF_VISIBILITY_PROTECTED,
103         ELF_VISIBILITY_ERROR
104 } elf_visibility_tag_t;
105
106 /**
107  * A scope containing entities.
108  */
109 struct scope_t {
110         entity_t *entities;
111         entity_t *last_entity; /**< pointer to last entity (so appending is fast) */
112         unsigned  depth;       /**< while parsing, the depth of this scope in the
113                                     scope stack. */
114 };
115
116 /**
117  * a named entity is something which can be referenced by its name
118  * (a symbol)
119  */
120 struct entity_base_t {
121         entity_kind_t       kind;
122         entity_namespace_t  namespc;
123         symbol_t           *symbol;
124         source_position_t   source_position;
125         scope_t            *parent_scope;    /**< The scope where this entity
126                                                                                       is contained in */
127         entity_t           *parent_entity;
128
129         /** next declaration in a scope */
130         entity_t           *next;
131         /** next declaration with same symbol */
132         entity_t           *symbol_next;
133 };
134
135 struct compound_t {
136         entity_base_t     base;
137         entity_t         *alias; /* used for name mangling of anonymous types */
138         scope_t           members;
139         decl_modifiers_t  modifiers;
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         statement_t   *statement;
176
177         /* ast2firm info */
178         ir_node       *block;
179 };
180
181 struct namespace_t {
182         entity_base_t  base;
183         scope_t        members;
184 };
185
186 struct typedef_t {
187         entity_base_t     base;
188         decl_modifiers_t  modifiers;
189         type_t           *type;
190         il_alignment_t    alignment;
191         bool              builtin : 1;
192 };
193
194 struct declaration_t {
195         entity_base_t     base;
196         type_t           *type;
197         storage_class_t   declared_storage_class;
198         storage_class_t   storage_class;
199         decl_modifiers_t  modifiers;
200         il_alignment_t    alignment;
201         attribute_t      *attributes;
202         bool              used     : 1;  /**< Set if the declaration is used. */
203         bool              implicit : 1;  /**< Set for implicit (not found in source code) declarations. */
204
205         /* ast2firm info */
206         unsigned char     kind;
207 };
208
209 struct compound_member_t {
210         declaration_t  base;
211         il_size_t      offset;     /**< the offset of this member in the compound */
212         unsigned char  bit_offset; /**< extra bit offset for bitfield members */
213         unsigned char  bit_size;   /**< bitsize for bitfield members */
214         bool           bitfield      : 1;  /**< member is (part of) a bitfield */
215         bool           read          : 1;
216         bool           address_taken : 1;  /**< Set if the address of this
217                                                 declaration was taken. */
218
219         /* ast2firm info */
220         ir_entity *entity;
221 };
222
223 struct variable_t {
224         declaration_t     base;
225         bool              thread_local   : 1;  /**< GCC __thread */
226         bool              restricta      : 1;
227         bool              deprecated     : 1;
228         bool              noalias        : 1;
229
230         bool              address_taken  : 1;  /**< Set if the address of this declaration was taken. */
231         bool              read           : 1;
232         unsigned          elf_visibility : 2;
233
234         initializer_t    *initializer;
235
236         /* ast2firm info */
237         union {
238                 unsigned int  value_number;
239                 ir_entity    *entity;
240                 ir_node      *vla_base;
241         } v;
242 };
243
244 struct parameter_t {
245         declaration_t  base;
246         bool           address_taken : 1;
247         bool           read          : 1;
248
249         /* ast2firm info */
250         union {
251                 unsigned int  value_number;
252                 ir_entity    *entity;
253         } v;
254 };
255
256 struct function_t {
257         declaration_t  base;
258         bool           is_inline      : 1;
259
260         bool           need_closure   : 1;  /**< Inner function needs closure. */
261         bool           goto_to_outer  : 1;  /**< Inner function has goto to outer function. */
262         unsigned       elf_visibility : 2;
263
264         builtin_kind_t btk;
265         scope_t        parameters;
266         statement_t   *statement;
267         symbol_t      *actual_name;        /**< gnu extension __REDIRECT */
268
269         /* ast2firm info */
270         ir_entity     *irentity;
271         ir_node       *static_link;        /**< if need_closure is set, the node
272                                                 representing the static link. */
273 };
274
275 union entity_t {
276         entity_kind_t      kind;
277         entity_base_t      base;
278         compound_t         compound;
279         enum_t             enume;
280         enum_value_t       enum_value;
281         label_t            label;
282         namespace_t        namespacee;
283         typedef_t          typedefe;
284         declaration_t      declaration;
285         variable_t         variable;
286         parameter_t        parameter;
287         function_t         function;
288         compound_member_t  compound_member;
289 };
290
291 #define DECLARATION_KIND_CASES        \
292         case ENTITY_FUNCTION:             \
293         case ENTITY_VARIABLE:             \
294         case ENTITY_PARAMETER:            \
295         case ENTITY_COMPOUND_MEMBER:
296
297 static inline bool is_declaration(const entity_t *entity)
298 {
299         switch(entity->kind) {
300         DECLARATION_KIND_CASES
301                 return true;
302         default:
303                 return false;
304         }
305 }
306
307 const char *get_entity_kind_name(entity_kind_t kind);
308
309 entity_t *allocate_entity_zero(entity_kind_t, entity_namespace_t, symbol_t*);
310
311 elf_visibility_tag_t get_elf_visibility_from_string(const char *string);
312
313 #endif