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