type: Make an assert()ion independent of the last entry of an enum.
[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
145 struct enum_value_t {
146         entity_base_t  base;
147         expression_t  *value;
148         type_t        *enum_type;
149
150         /* ast2firm info */
151         ir_tarval     *tv;
152 };
153
154 struct label_t {
155         entity_base_t  base;
156         bool           used : 1;
157         bool           address_taken : 1;
158         unsigned       n_users; /* Reference counter to mature the label block as early as possible. */
159         statement_t   *statement;
160
161         /* ast2firm info */
162         jump_target    target;
163         ir_node       *indirect_block;
164 };
165
166 struct namespace_t {
167         entity_base_t  base;
168         scope_t        members;
169 };
170
171 struct typedef_t {
172         entity_base_t     base;
173         decl_modifiers_t  modifiers;
174         type_t           *type;
175         il_alignment_t    alignment;
176         bool              builtin : 1;
177 };
178
179 struct declaration_t {
180         entity_base_t     base;
181         type_t           *type;
182         storage_class_t   declared_storage_class;
183         storage_class_t   storage_class;
184         decl_modifiers_t  modifiers;
185         il_alignment_t    alignment;
186         attribute_t      *attributes;
187         bool              used     : 1;  /**< Set if the declaration is used. */
188         bool              implicit : 1;  /**< Set for implicit (not found in source code) declarations. */
189
190         /* ast2firm info */
191         unsigned char     kind;
192 };
193
194 struct compound_member_t {
195         declaration_t  base;
196         il_size_t      offset;     /**< the offset of this member in the compound */
197         unsigned char  bit_offset; /**< extra bit offset for bitfield members */
198         unsigned char  bit_size;   /**< bitsize for bitfield members */
199         bool           bitfield      : 1;  /**< member is (part of) a bitfield */
200
201         /* ast2firm info */
202         ir_entity *entity;
203 };
204
205 struct variable_t {
206         declaration_t     base;
207         bool              thread_local   : 1;
208
209         bool              address_taken  : 1;  /**< Set if the address of this declaration was taken. */
210         bool              read           : 1;
211         unsigned          elf_visibility : 2;
212
213         initializer_t    *initializer;
214
215         /* ast2firm info */
216         union {
217                 unsigned int  value_number;
218                 ir_entity    *entity;
219                 ir_node      *vla_base;
220         } v;
221 };
222
223 struct function_t {
224         declaration_t  base;
225         bool           is_inline      : 1;
226
227         bool           need_closure   : 1;  /**< Inner function needs closure. */
228         bool           goto_to_outer  : 1;  /**< Inner function has goto to outer function. */
229         unsigned       elf_visibility : 2;
230
231         builtin_kind_t btk;
232         scope_t        parameters;
233         statement_t   *body;
234         symbol_t      *actual_name;        /**< gnu extension __REDIRECT */
235
236         /* ast2firm info */
237         union {
238                 ir_builtin_kind firm_builtin_kind;
239                 unsigned        chk_arg_pos;
240         } b;
241         ir_entity      *irentity;
242         ir_node        *static_link;        /**< if need_closure is set, the node
243                                                  representing the static link. */
244 };
245
246 union entity_t {
247         entity_kind_t      kind;
248         entity_base_t      base;
249         compound_t         compound;
250         enum_t             enume;
251         enum_value_t       enum_value;
252         label_t            label;
253         namespace_t        namespacee;
254         typedef_t          typedefe;
255         declaration_t      declaration;
256         variable_t         variable;
257         function_t         function;
258         compound_member_t  compound_member;
259 };
260
261 #define DECLARATION_KIND_CASES \
262              ENTITY_FUNCTION:        \
263         case ENTITY_VARIABLE:        \
264         case ENTITY_PARAMETER:       \
265         case ENTITY_COMPOUND_MEMBER
266
267 static inline bool is_declaration(const entity_t *entity)
268 {
269         switch (entity->kind) {
270         case DECLARATION_KIND_CASES:
271                 return true;
272         default:
273                 return false;
274         }
275 }
276
277 const char *get_entity_kind_name(entity_kind_t kind);
278
279 entity_t *allocate_entity_zero(entity_kind_t, entity_namespace_t, symbol_t*, position_t const*);
280
281 elf_visibility_tag_t get_elf_visibility_from_string(const char *string);
282
283 entity_t *skip_unnamed_bitfields(entity_t*);
284
285 #endif