rememeber attributes on struct types
[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         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;
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 function_t {
245         declaration_t  base;
246         bool           is_inline      : 1;
247
248         bool           need_closure   : 1;  /**< Inner function needs closure. */
249         bool           goto_to_outer  : 1;  /**< Inner function has goto to outer function. */
250         unsigned       elf_visibility : 2;
251
252         builtin_kind_t btk;
253         scope_t        parameters;
254         statement_t   *statement;
255         symbol_t      *actual_name;        /**< gnu extension __REDIRECT */
256
257         /* ast2firm info */
258         union {
259                 ir_builtin_kind firm_builtin_kind;
260                 unsigned        chk_arg_pos;
261         } b;
262         ir_entity      *irentity;
263         ir_node        *static_link;        /**< if need_closure is set, the node
264                                                  representing the static link. */
265 };
266
267 union entity_t {
268         entity_kind_t      kind;
269         entity_base_t      base;
270         compound_t         compound;
271         enum_t             enume;
272         enum_value_t       enum_value;
273         label_t            label;
274         namespace_t        namespacee;
275         typedef_t          typedefe;
276         declaration_t      declaration;
277         variable_t         variable;
278         function_t         function;
279         compound_member_t  compound_member;
280 };
281
282 #define DECLARATION_KIND_CASES \
283              ENTITY_FUNCTION:        \
284         case ENTITY_VARIABLE:        \
285         case ENTITY_PARAMETER:       \
286         case ENTITY_COMPOUND_MEMBER
287
288 static inline bool is_declaration(const entity_t *entity)
289 {
290         switch(entity->kind) {
291         case DECLARATION_KIND_CASES:
292                 return true;
293         default:
294                 return false;
295         }
296 }
297
298 const char *get_entity_kind_name(entity_kind_t kind);
299
300 entity_t *allocate_entity_zero(entity_kind_t, entity_namespace_t, symbol_t*, source_position_t const*);
301
302 elf_visibility_tag_t get_elf_visibility_from_string(const char *string);
303
304 entity_t *skip_unnamed_bitfields(entity_t*);
305
306 #endif