simple support for __attribute__((alias("symbol")))
[cparser] / entity_t.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
4  */
5 #ifndef ENTITY_T_H
6 #define ENTITY_T_H
7
8 #include "ast.h"
9 #include "symbol.h"
10 #include "entity.h"
11 #include "attribute.h"
12 #include <libfirm/firm_types.h>
13 #include "builtins.h"
14 #include "jump_target.h"
15 #include "token_t.h"
16
17 typedef enum {
18         ENTITY_VARIABLE = 1,
19         ENTITY_COMPOUND_MEMBER,
20         ENTITY_PARAMETER,
21         ENTITY_FUNCTION,
22         ENTITY_TYPEDEF,
23         ENTITY_CLASS,
24         ENTITY_STRUCT,
25         ENTITY_UNION,
26         ENTITY_ENUM,
27         ENTITY_ENUM_VALUE,
28         ENTITY_LABEL,
29         ENTITY_LOCAL_LABEL,
30         ENTITY_NAMESPACE
31 } entity_kind_tag_t;
32 typedef unsigned char entity_kind_t;
33
34 typedef enum namespace_tag_t {
35         NAMESPACE_NORMAL = 1,
36         NAMESPACE_TAG,
37         NAMESPACE_LABEL
38 } namespace_tag_t;
39 typedef unsigned char entity_namespace_t;
40
41 typedef enum storage_class_tag_t {
42         STORAGE_CLASS_NONE,
43         STORAGE_CLASS_EXTERN,
44         STORAGE_CLASS_STATIC,
45         STORAGE_CLASS_TYPEDEF,
46         STORAGE_CLASS_AUTO,
47         STORAGE_CLASS_REGISTER,
48 } storage_class_tag_t;
49 typedef unsigned char storage_class_t;
50
51 typedef enum decl_modifier_t {
52         DM_NONE              = 0,
53         DM_DLLIMPORT         = 1 <<  0,
54         DM_DLLEXPORT         = 1 <<  1,
55         DM_THREAD            = 1 <<  2,
56         DM_NAKED             = 1 <<  3,
57         DM_MICROSOFT_INLINE  = 1 <<  4,
58         DM_FORCEINLINE       = 1 <<  5,
59         DM_SELECTANY         = 1 <<  6,
60         DM_NOTHROW           = 1 <<  7,
61         DM_NOVTABLE          = 1 <<  8,
62         DM_NORETURN          = 1 <<  9,
63         DM_NOINLINE          = 1 << 10,
64         DM_RESTRICT          = 1 << 11,
65         DM_NOALIAS           = 1 << 12,
66         DM_TRANSPARENT_UNION = 1 << 13,
67         DM_CONST             = 1 << 14,
68         DM_PURE              = 1 << 15,
69         DM_CONSTRUCTOR       = 1 << 16,
70         DM_DESTRUCTOR        = 1 << 17,
71         DM_UNUSED            = 1 << 18,
72         DM_USED              = 1 << 19,
73         DM_CDECL             = 1 << 20,
74         DM_FASTCALL          = 1 << 21,
75         DM_STDCALL           = 1 << 22,
76         DM_THISCALL          = 1 << 23,
77         DM_DEPRECATED        = 1 << 24,
78         DM_RETURNS_TWICE     = 1 << 25,
79         DM_MALLOC            = 1 << 26,
80         DM_WEAK              = 1 << 27,
81         DM_LEAF              = 1 << 28,
82 } decl_modifier_t;
83
84 typedef enum elf_visibility_tag_t {
85         ELF_VISIBILITY_DEFAULT,
86         ELF_VISIBILITY_HIDDEN,
87         ELF_VISIBILITY_INTERNAL,
88         ELF_VISIBILITY_PROTECTED,
89         ELF_VISIBILITY_ERROR
90 } elf_visibility_tag_t;
91
92 /**
93  * A scope containing entities.
94  */
95 struct scope_t {
96         entity_t *entities;
97         entity_t *last_entity; /**< pointer to last entity (so appending is fast) */
98         unsigned  depth;       /**< while parsing, the depth of this scope in the
99                                     scope stack. */
100 };
101
102 /**
103  * a named entity is something which can be referenced by its name
104  * (a symbol)
105  */
106 struct entity_base_t {
107         entity_kind_t       kind;
108         entity_namespace_t  namespc;
109         symbol_t           *symbol;
110         position_t          pos;
111         scope_t            *parent_scope;    /**< The scope where this entity
112                                                                                       is contained in */
113         entity_t           *parent_entity;
114
115         /** next declaration in a scope */
116         entity_t           *next;
117         /** next declaration with same symbol */
118         entity_t           *symbol_next;
119 };
120
121 struct compound_t {
122         entity_base_t     base;
123         entity_t         *alias; /* used for name mangling of anonymous types */
124         scope_t           members;
125         decl_modifiers_t  modifiers;
126         attribute_t      *attributes;
127         bool              layouted          : 1;
128         bool              complete          : 1;
129         bool              transparent_union : 1;
130         bool              packed            : 1;
131
132         il_alignment_t    alignment;
133         il_size_t         size;
134
135         /* ast2firm info */
136         ir_type          *irtype;
137         bool              irtype_complete : 1;
138 };
139
140 struct enum_t {
141         entity_base_t  base;
142         entity_t      *alias; /* used for name mangling of anonymous types */
143         bool           complete : 1;
144 };
145
146 struct enum_value_t {
147         entity_base_t  base;
148         expression_t  *value;
149         type_t        *enum_type;
150
151         /* ast2firm info */
152         ir_tarval     *tv;
153 };
154
155 struct label_t {
156         entity_base_t  base;
157         bool           used : 1;
158         bool           address_taken : 1;
159         unsigned       n_users; /* Reference counter to mature the label block as early as possible. */
160         statement_t   *statement;
161
162         /* ast2firm info */
163         jump_target    target;
164         ir_node       *indirect_block;
165 };
166
167 struct namespace_t {
168         entity_base_t  base;
169         scope_t        members;
170 };
171
172 struct typedef_t {
173         entity_base_t     base;
174         decl_modifiers_t  modifiers;
175         type_t           *type;
176         il_alignment_t    alignment;
177         bool              builtin : 1;
178 };
179
180 struct declaration_t {
181         entity_base_t     base;
182         type_t           *type;
183         storage_class_t   declared_storage_class;
184         storage_class_t   storage_class;
185         decl_modifiers_t  modifiers;
186         il_alignment_t    alignment;
187         attribute_t      *attributes;
188         bool              used     : 1;  /**< Set if the declaration is used. */
189         bool              implicit : 1;  /**< Set for implicit (not found in source code) declarations. */
190
191         /* ast2firm info */
192         unsigned char     kind;
193 };
194
195 struct compound_member_t {
196         declaration_t  base;
197         il_size_t      offset;     /**< the offset of this member in the compound */
198         unsigned char  bit_offset; /**< extra bit offset for bitfield members */
199         unsigned char  bit_size;   /**< bitsize for bitfield members */
200         bool           bitfield      : 1;  /**< member is (part of) a bitfield */
201
202         /* ast2firm info */
203         ir_entity *entity;
204 };
205
206 struct variable_t {
207         declaration_t     base;
208         bool              thread_local   : 1;
209
210         bool              address_taken  : 1;  /**< Set if the address of this declaration was taken. */
211         bool              read           : 1;
212         unsigned          elf_visibility : 2;
213
214         initializer_t    *initializer;
215         symbol_t         *alias;               /**< gnu extension attribute alias */
216
217         /* ast2firm info */
218         union {
219                 unsigned int  value_number;
220                 ir_entity    *entity;
221                 ir_node      *vla_base;
222         } v;
223 };
224
225 struct function_t {
226         declaration_t  base;
227         bool           is_inline      : 1;
228
229         bool           need_closure   : 1;  /**< Inner function needs closure. */
230         bool           goto_to_outer  : 1;  /**< Inner function has goto to outer function. */
231         unsigned       elf_visibility : 2;
232
233         builtin_kind_t btk;
234         scope_t        parameters;
235         statement_t   *body;
236         symbol_t      *actual_name;        /**< gnu extension __REDIRECT */
237         symbol_t      *alias;              /**< gnu extension attribute alias */
238
239         /* ast2firm info */
240         union {
241                 ir_builtin_kind firm_builtin_kind;
242                 unsigned        chk_arg_pos;
243         } b;
244         ir_entity      *irentity;
245         ir_node        *static_link;        /**< if need_closure is set, the node
246                                                  representing the static link. */
247 };
248
249 union entity_t {
250         entity_kind_t      kind;
251         entity_base_t      base;
252         compound_t         compound;
253         enum_t             enume;
254         enum_value_t       enum_value;
255         label_t            label;
256         namespace_t        namespacee;
257         typedef_t          typedefe;
258         declaration_t      declaration;
259         variable_t         variable;
260         function_t         function;
261         compound_member_t  compound_member;
262 };
263
264 #define DECLARATION_KIND_CASES \
265              ENTITY_FUNCTION:        \
266         case ENTITY_VARIABLE:        \
267         case ENTITY_PARAMETER:       \
268         case ENTITY_COMPOUND_MEMBER
269
270 static inline bool is_declaration(const entity_t *entity)
271 {
272         switch (entity->kind) {
273         case DECLARATION_KIND_CASES:
274                 return true;
275         default:
276                 return false;
277         }
278 }
279
280 const char *get_entity_kind_name(entity_kind_t kind);
281
282 entity_t *allocate_entity_zero(entity_kind_t, entity_namespace_t, symbol_t*, position_t const*);
283
284 elf_visibility_tag_t get_elf_visibility_from_string(const char *string);
285
286 entity_t *skip_unnamed_bitfields(entity_t*);
287
288 #endif