parse gnu __leaf__ attribute
[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         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         bool              layouted          : 1;
140         bool              complete          : 1;
141         bool              transparent_union : 1;
142         bool              packed            : 1;
143
144         il_alignment_t    alignment;
145         il_size_t         size;
146
147         /* ast2firm info */
148         ir_type          *irtype;
149         bool              irtype_complete : 1;
150 };
151
152 struct enum_t {
153         entity_base_t  base;
154         entity_t      *alias; /* used for name mangling of anonymous types */
155         bool           complete : 1;
156
157         /* ast2firm info */
158         ir_type       *irtype;
159 };
160
161 struct enum_value_t {
162         entity_base_t  base;
163         expression_t  *value;
164         type_t        *enum_type;
165
166         /* ast2firm info */
167         ir_tarval     *tv;
168 };
169
170 struct label_t {
171         entity_base_t  base;
172         bool           used : 1;
173         bool           address_taken : 1;
174         statement_t   *statement;
175
176         /* ast2firm info */
177         ir_node       *block;
178 };
179
180 struct namespace_t {
181         entity_base_t  base;
182         scope_t        members;
183 };
184
185 struct typedef_t {
186         entity_base_t     base;
187         decl_modifiers_t  modifiers;
188         type_t           *type;
189         il_alignment_t    alignment;
190         bool              builtin : 1;
191 };
192
193 struct declaration_t {
194         entity_base_t     base;
195         type_t           *type;
196         storage_class_t   declared_storage_class;
197         storage_class_t   storage_class;
198         decl_modifiers_t  modifiers;
199         il_alignment_t    alignment;
200         attribute_t      *attributes;
201         bool              used     : 1;  /**< Set if the declaration is used. */
202         bool              implicit : 1;  /**< Set for implicit (not found in source code) declarations. */
203
204         /* ast2firm info */
205         unsigned char     kind;
206 };
207
208 struct compound_member_t {
209         declaration_t  base;
210         il_size_t      offset;     /**< the offset of this member in the compound */
211         unsigned char  bit_offset; /**< extra bit offset for bitfield members */
212         unsigned char  bit_size;   /**< bitsize for bitfield members */
213         bool           bitfield      : 1;  /**< member is (part of) a bitfield */
214         bool           read          : 1;
215         bool           address_taken : 1;  /**< Set if the address of this
216                                                 declaration was taken. */
217
218         /* ast2firm info */
219         ir_entity *entity;
220 };
221
222 struct variable_t {
223         declaration_t     base;
224         bool              thread_local   : 1;  /**< GCC __thread */
225         bool              restricta      : 1;
226         bool              deprecated     : 1;
227         bool              noalias        : 1;
228
229         bool              address_taken  : 1;  /**< Set if the address of this declaration was taken. */
230         bool              read           : 1;
231         unsigned          elf_visibility : 2;
232
233         initializer_t    *initializer;
234
235         /* ast2firm info */
236         union {
237                 unsigned int  value_number;
238                 ir_entity    *entity;
239                 ir_node      *vla_base;
240         } v;
241 };
242
243 struct parameter_t {
244         declaration_t  base;
245         bool           address_taken : 1;
246         bool           read          : 1;
247
248         /* ast2firm info */
249         union {
250                 unsigned int  value_number;
251                 ir_entity    *entity;
252         } v;
253 };
254
255 struct function_t {
256         declaration_t  base;
257         bool           is_inline      : 1;
258
259         bool           need_closure   : 1;  /**< Inner function needs closure. */
260         bool           goto_to_outer  : 1;  /**< Inner function has goto to outer function. */
261         unsigned       elf_visibility : 2;
262
263         builtin_kind_t btk;
264         scope_t        parameters;
265         statement_t   *statement;
266         symbol_t      *actual_name;        /**< gnu extension __REDIRECT */
267
268         /* ast2firm info */
269         union {
270                 ir_builtin_kind firm_builtin_kind;
271                 unsigned        chk_arg_pos;
272         } b;
273         ir_entity      *irentity;
274         ir_node        *static_link;        /**< if need_closure is set, the node
275                                                  representing the static link. */
276 };
277
278 union entity_t {
279         entity_kind_t      kind;
280         entity_base_t      base;
281         compound_t         compound;
282         enum_t             enume;
283         enum_value_t       enum_value;
284         label_t            label;
285         namespace_t        namespacee;
286         typedef_t          typedefe;
287         declaration_t      declaration;
288         variable_t         variable;
289         parameter_t        parameter;
290         function_t         function;
291         compound_member_t  compound_member;
292 };
293
294 #define DECLARATION_KIND_CASES \
295              ENTITY_FUNCTION:        \
296         case ENTITY_VARIABLE:        \
297         case ENTITY_PARAMETER:       \
298         case ENTITY_COMPOUND_MEMBER
299
300 static inline bool is_declaration(const entity_t *entity)
301 {
302         switch(entity->kind) {
303         case DECLARATION_KIND_CASES:
304                 return true;
305         default:
306                 return false;
307         }
308 }
309
310 const char *get_entity_kind_name(entity_kind_t kind);
311
312 entity_t *allocate_entity_zero(entity_kind_t, entity_namespace_t, symbol_t*, source_position_t const*);
313
314 elf_visibility_tag_t get_elf_visibility_from_string(const char *string);
315
316 entity_t *skip_unnamed_bitfields(entity_t*);
317
318 #endif