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