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