1 #ifndef SYMBOL_H
2 #define SYMBOL_H
3 /*
4 * Basic symbol and namespace definitions.
5 *
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003 Linus Torvalds
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28 #include "token.h"
29 #include "target.h"
30
31 /*
32 * An identifier with semantic meaning is a "symbol".
33 *
34 * There's a 1:n relationship: each symbol is always
35 * associated with one identifier, while each identifier
36 * can have one or more semantic meanings due to C scope
37 * rules.
38 *
39 * The progression is symbol -> token -> identifier. The
40 * token contains the information on where the symbol was
41 * declared.
42 */
43 enum namespace {
44 NS_NONE = 0,
45 NS_MACRO = 1,
46 NS_TYPEDEF = 2,
47 NS_STRUCT = 4, // Also used for unions and enums.
48 NS_LABEL = 8,
49 NS_SYMBOL = 16,
50 NS_ITERATOR = 32,
51 NS_PREPROCESSOR = 64,
52 NS_UNDEF = 128,
53 NS_KEYWORD = 256,
54 };
55
56 enum type {
57 SYM_UNINITIALIZED,
58 SYM_PREPROCESSOR,
59 SYM_BASETYPE,
60 SYM_NODE,
61 SYM_PTR,
62 SYM_FN,
63 SYM_ARRAY,
64 SYM_STRUCT,
65 SYM_UNION,
66 SYM_ENUM,
67 SYM_TYPEDEF,
68 SYM_TYPEOF,
69 SYM_MEMBER,
70 SYM_BITFIELD,
71 SYM_LABEL,
72 SYM_RESTRICT,
73 SYM_FOULED,
74 SYM_KEYWORD,
75 SYM_BAD,
76 };
77
78 enum keyword {
79 KW_SPECIFIER = 1 << 0,
80 KW_MODIFIER = 1 << 1,
81 KW_QUALIFIER = 1 << 2,
82 KW_ATTRIBUTE = 1 << 3,
83 KW_STATEMENT = 1 << 4,
84 KW_ASM = 1 << 5,
85 KW_MODE = 1 << 6,
86 KW_SHORT = 1 << 7,
87 KW_LONG = 1 << 8,
88 KW_EXACT = 1 << 9,
89 };
90
91 struct context {
92 struct expression *context;
93 unsigned int in, out;
94 };
95
96 extern struct context *alloc_context(void);
97
98 DECLARE_PTR_LIST(context_list, struct context);
99
100 struct ctype {
101 unsigned long modifiers;
102 unsigned long alignment;
103 struct context_list *contexts;
104 struct ident *as;
105 struct symbol *base_type;
106 };
107
108 struct decl_state {
109 struct ctype ctype;
110 struct ident **ident;
111 struct symbol_op *mode;
112 unsigned char prefer_abstract, is_inline, storage_class, is_tls;
113 unsigned char is_ext_visible;
114 };
115
116 struct symbol_op {
117 enum keyword type;
118 int (*evaluate)(struct expression *);
119 int (*expand)(struct expression *, int);
120 int (*args)(struct expression *);
121
122 /* keywords */
123 struct token *(*declarator)(struct token *token, struct decl_state *ctx);
124 struct token *(*statement)(struct token *token, struct statement *stmt);
125 struct token *(*toplevel)(struct token *token, struct symbol_list **list);
126 struct token *(*attribute)(struct token *token, struct symbol *attr, struct decl_state *ctx);
127 struct symbol *(*to_mode)(struct symbol *);
128 void (*asm_modifier)(struct token *token, unsigned long *mods);
129
130 int test, set, class;
131 };
132
133
134 #define SYM_ATTR_WEAK 0
135 #define SYM_ATTR_NORMAL 1
136 #define SYM_ATTR_STRONG 2
137
138 struct symbol {
139 enum type type:8;
140 enum namespace namespace:9;
141 unsigned char used:1, attr:2, enum_member:1, bound:1;
142 struct position pos; /* Where this symbol was declared */
143 struct position endpos; /* Where this symbol ends*/
144 struct ident *ident; /* What identifier this symbol is associated with */
145 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
146 struct symbol *replace; /* What is this symbol shadowed by in copy-expression */
147 struct scope *scope;
148 union {
149 struct symbol *same_symbol;
150 struct symbol *next_subobject;
151 };
152
153 struct symbol_op *op;
154
155 union {
156 struct /* NS_MACRO */ {
157 struct token *expansion;
158 struct token *arglist;
159 struct scope *used_in;
160 void (*expander)(struct token *);
161 };
162 struct /* NS_PREPROCESSOR */ {
163 int (*handler)(struct stream *, struct token **, struct token *);
164 int normal;
165 };
166 struct /* NS_SYMBOL */ {
167 unsigned long offset;
168 int bit_size;
169 unsigned int bit_offset:8,
170 variadic:1,
171 initialized:1,
172 examined:1,
173 expanding:1,
174 evaluated:1,
175 string:1,
176 designated_init:1,
177 forced_arg:1,
178 accessed:1,
179 builtin:1,
180 torename:1,
181 transparent_union:1;
182 struct expression *array_size;
183 struct ctype ctype;
184 struct symbol_list *arguments;
185 struct statement *stmt;
186 struct symbol_list *symbol_list;
187 struct statement *inline_stmt;
188 struct symbol_list *inline_symbol_list;
189 struct expression *initializer;
190 struct entrypoint *ep;
191 struct symbol *definition;
192 };
193 };
194 union /* backend */ {
195 struct basic_block *bb_target; /* label */
196 void *aux; /* Auxiliary info, e.g. backend information */
197 struct { /* sparse ctags */
198 char kind;
199 unsigned char visited:1;
200 };
201 };
202 pseudo_t pseudo;
203 };
204
205 /* Modifiers */
206 #define MOD_AUTO 0x00000001
207 #define MOD_REGISTER 0x00000002
208 #define MOD_STATIC 0x00000004
209 #define MOD_EXTERN 0x00000008
210 #define MOD_TOPLEVEL 0x00000010 // scoping..
211 #define MOD_TLS 0x00000020
212 #define MOD_ASM_GOTO MOD_TLS // never used together
213 #define MOD_INLINE 0x00000040
214
215 #define MOD_ASSIGNED 0x00000080
216 #define MOD_ADDRESSABLE 0x00000100
217
218 #define MOD_CONST 0x00000200
219 #define MOD_VOLATILE 0x00000400
220 #define MOD_RESTRICT 0x00000800
221 #define MOD_ATOMIC 0x00001000
222
223 #define MOD_SIGNED 0x00002000
224 #define MOD_UNSIGNED 0x00004000
225 #define MOD_EXPLICITLY_SIGNED 0x00008000
226
227 #define MOD_TYPE 0x00010000
228 #define MOD_USERTYPE 0x00020000
229 #define MOD_CHAR 0x00040000
230 #define MOD_SHORT 0x00080000
231 #define MOD_LONG 0x00100000
232 #define MOD_LONGLONG 0x00200000
233 #define MOD_LONGLONGLONG 0x00400000
234
235 #define MOD_SAFE 0x00800000 // non-null/non-trapping pointer
236 #define MOD_PURE 0x01000000
237 #define MOD_BITWISE 0x02000000
238 #define MOD_NOCAST 0x04000000
239 #define MOD_NODEREF 0x08000000
240 #define MOD_NORETURN 0x10000000
241 #define MOD_EXT_VISIBLE 0x20000000
242
243
244 #define MOD_ACCESS (MOD_ASSIGNED | MOD_ADDRESSABLE)
245 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
246 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
247 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
248 #define MOD_LONG_ALL (MOD_LONG | MOD_LONGLONG | MOD_LONGLONGLONG)
249 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL | MOD_SIGNEDNESS)
250 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL)
251 #define MOD_IGNORE (MOD_STORAGE | MOD_ACCESS | MOD_USERTYPE | MOD_EXPLICITLY_SIGNED | MOD_EXT_VISIBLE)
252 #define MOD_QUALIFIER (MOD_CONST | MOD_VOLATILE | MOD_RESTRICT | MOD_ATOMIC)
253 #define MOD_PTRINHERIT (MOD_QUALIFIER | MOD_NODEREF | MOD_NORETURN | MOD_NOCAST)
254 /* modifiers preserved by typeof() operator */
255 #define MOD_TYPEOF (MOD_QUALIFIER | MOD_NOCAST | MOD_SPECIFIER)
256
257
258 /* Current parsing/evaluation function */
259 extern struct symbol *current_fn;
260
261 /* Abstract types */
262 extern struct symbol int_type,
263 fp_type;
264
265 /* C types */
266 extern struct symbol bool_ctype, void_ctype, type_ctype,
267 char_ctype, schar_ctype, uchar_ctype,
268 short_ctype, sshort_ctype, ushort_ctype,
269 int_ctype, sint_ctype, uint_ctype,
270 long_ctype, slong_ctype, ulong_ctype,
271 llong_ctype, sllong_ctype, ullong_ctype,
272 lllong_ctype, slllong_ctype, ulllong_ctype,
273 float_ctype, double_ctype, ldouble_ctype,
274 string_ctype, ptr_ctype, lazy_ptr_ctype,
275 incomplete_ctype, label_ctype, bad_ctype,
276 null_ctype;
277 extern struct symbol int_ptr_ctype, uint_ptr_ctype;
278 extern struct symbol long_ptr_ctype, ulong_ptr_ctype;
279 extern struct symbol llong_ptr_ctype, ullong_ptr_ctype;
280 extern struct symbol float32_ctype, float32x_ctype;
281 extern struct symbol float64_ctype, float64x_ctype;
282 extern struct symbol float128_ctype;
283 extern struct symbol const_void_ctype, const_char_ctype;
284 extern struct symbol const_ptr_ctype, const_string_ctype;
285
286 #define uintptr_ctype size_t_ctype
287 #define intptr_ctype ssize_t_ctype
288
289 /* Special internal symbols */
290 extern struct symbol zero_int;
291
292 #define __IDENT(n,str,res) \
293 extern struct ident n
294 #include "ident-list.h"
295
296
297 extern struct symbol_list *translation_unit_used_list;
298
299 extern void access_symbol(struct symbol *);
300
301 extern const char * type_difference(struct ctype *c1, struct ctype *c2,
302 unsigned long mod1, unsigned long mod2);
303
304 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
305 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
306 extern void init_symbols(void);
307 extern void init_builtins(int stream);
308 extern void declare_builtins(void);
309 extern void init_ctype(void);
310 extern void init_target(void);
311 extern struct symbol *alloc_symbol(struct position, int type);
312 extern void show_type(struct symbol *);
313 extern const char *modifier_string(unsigned long mod);
314 extern void show_symbol(struct symbol *);
315 extern int show_symbol_expr_init(struct symbol *sym);
316 extern void show_type_list(struct symbol *);
317 extern void show_symbol_list(struct symbol_list *, const char *);
318 extern void add_symbol(struct symbol_list **, struct symbol *);
319 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
320
321 extern struct symbol *examine_symbol_type(struct symbol *);
322 extern struct symbol *examine_pointer_target(struct symbol *);
323 extern const char *show_as(struct ident *as);
324 extern const char *show_typename(struct symbol *sym);
325 extern const char *builtin_typename(struct symbol *sym);
326 extern const char *builtin_type_suffix(struct symbol *sym);
327 extern const char *builtin_ctypename(struct ctype *ctype);
328 extern const char* get_type_name(enum type type);
329
330 extern void debug_symbol(struct symbol *);
331 extern void merge_type(struct symbol *sym, struct symbol *base_type);
332 extern void check_declaration(struct symbol *sym);
333 extern void check_duplicates(struct symbol *sym);
334
valid_type(const struct symbol * ctype)335 static inline int valid_type(const struct symbol *ctype)
336 {
337 return ctype && ctype != &bad_ctype;
338 }
339
get_base_type(const struct symbol * sym)340 static inline struct symbol *get_base_type(const struct symbol *sym)
341 {
342 return examine_symbol_type(sym->ctype.base_type);
343 }
344
345 ///
346 // test if type is an integer type
347 //
348 // @return: ``1`` for plain integer type, enums & bitfields
349 // but ``0`` for bitwise types!
is_int_type(const struct symbol * type)350 static inline int is_int_type(const struct symbol *type)
351 {
352 if (type->type == SYM_NODE)
353 type = type->ctype.base_type;
354 if (type->type == SYM_ENUM)
355 type = type->ctype.base_type;
356 return type->type == SYM_BITFIELD ||
357 type->ctype.base_type == &int_type;
358 }
359
is_enum_type(const struct symbol * type)360 static inline int is_enum_type(const struct symbol *type)
361 {
362 if (type->type == SYM_NODE)
363 type = type->ctype.base_type;
364 return (type->type == SYM_ENUM);
365 }
366
is_signed_type(struct symbol * sym)367 static inline int is_signed_type(struct symbol *sym)
368 {
369 if (sym->type == SYM_NODE)
370 sym = sym->ctype.base_type;
371 if (sym->type == SYM_PTR)
372 return 0;
373 return !(sym->ctype.modifiers & MOD_UNSIGNED);
374 }
375
is_type_type(struct symbol * type)376 static inline int is_type_type(struct symbol *type)
377 {
378 return (type->ctype.modifiers & MOD_TYPE) != 0;
379 }
380
is_ptr_type(struct symbol * type)381 static inline int is_ptr_type(struct symbol *type)
382 {
383 if (!type)
384 return 0;
385 if (type->type == SYM_NODE)
386 type = type->ctype.base_type;
387 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
388 }
389
is_func_type(struct symbol * type)390 static inline int is_func_type(struct symbol *type)
391 {
392 if (type->type == SYM_NODE)
393 type = type->ctype.base_type;
394 return type->type == SYM_FN;
395 }
396
is_array_type(struct symbol * type)397 static inline int is_array_type(struct symbol *type)
398 {
399 if (type->type == SYM_NODE)
400 type = type->ctype.base_type;
401 return type->type == SYM_ARRAY;
402 }
403
is_float_type(struct symbol * type)404 static inline int is_float_type(struct symbol *type)
405 {
406 if (type->type == SYM_NODE)
407 type = type->ctype.base_type;
408 return type->ctype.base_type == &fp_type;
409 }
410
is_byte_type(struct symbol * type)411 static inline int is_byte_type(struct symbol *type)
412 {
413 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
414 }
415
is_void_type(struct symbol * type)416 static inline int is_void_type(struct symbol *type)
417 {
418 if (type->type == SYM_NODE)
419 type = type->ctype.base_type;
420 return type == &void_ctype;
421 }
422
is_bool_type(struct symbol * type)423 static inline int is_bool_type(struct symbol *type)
424 {
425 if (type->type == SYM_NODE)
426 type = type->ctype.base_type;
427 return type == &bool_ctype;
428 }
429
is_scalar_type(struct symbol * type)430 static inline int is_scalar_type(struct symbol *type)
431 {
432 if (type->type == SYM_NODE)
433 type = type->ctype.base_type;
434 switch (type->type) {
435 case SYM_ENUM:
436 case SYM_BITFIELD:
437 case SYM_PTR:
438 case SYM_RESTRICT: // OK, always integer types
439 return 1;
440 default:
441 break;
442 }
443 if (type->ctype.base_type == &int_type)
444 return 1;
445 if (type->ctype.base_type == &fp_type)
446 return 1;
447 return 0;
448 }
449
450 /// return true for integer & pointer types
is_integral_type(struct symbol * type)451 static inline bool is_integral_type(struct symbol *type)
452 {
453 if (type->type == SYM_NODE)
454 type = type->ctype.base_type;
455 switch (type->type) {
456 case SYM_ENUM:
457 case SYM_PTR:
458 case SYM_RESTRICT: // OK, always integer types
459 return 1;
460 default:
461 break;
462 }
463 if (type->ctype.base_type == &int_type)
464 return 1;
465 return 0;
466 }
467
is_function(struct symbol * type)468 static inline int is_function(struct symbol *type)
469 {
470 return type && type->type == SYM_FN;
471 }
472
is_extern_inline(struct symbol * sym)473 static inline int is_extern_inline(struct symbol *sym)
474 {
475 return (sym->ctype.modifiers & MOD_EXTERN) &&
476 (sym->ctype.modifiers & MOD_INLINE) &&
477 is_function(sym->ctype.base_type);
478 }
479
get_sym_type(struct symbol * type)480 static inline int get_sym_type(struct symbol *type)
481 {
482 if (type->type == SYM_NODE)
483 type = type->ctype.base_type;
484 if (type->type == SYM_ENUM)
485 type = type->ctype.base_type;
486 return type->type;
487 }
488
extend_value(long long val,struct symbol * ctype)489 static inline long long extend_value(long long val, struct symbol *ctype)
490 {
491 int is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
492 unsigned size = ctype->bit_size;
493
494 return bits_extend(val, size, is_signed);
495 }
496
lookup_keyword(struct ident * ident,enum namespace ns)497 static inline struct symbol *lookup_keyword(struct ident *ident, enum namespace ns)
498 {
499 if (!ident->keyword)
500 return NULL;
501 return lookup_symbol(ident, ns);
502 }
503
504 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
505 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
506 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
507
508 void create_fouled(struct symbol *type);
509 struct symbol *befoul(struct symbol *type);
510
511
512 extern struct ident bad_address_space;
513
valid_as(struct ident * as)514 static inline bool valid_as(struct ident *as)
515 {
516 return as && as != &bad_address_space;
517 }
518
combine_address_space(struct position pos,struct ident ** tas,struct ident * sas)519 static inline void combine_address_space(struct position pos,
520 struct ident **tas, struct ident *sas)
521 {
522 struct ident *as;
523 if (!sas)
524 return;
525 as = *tas;
526 if (!as)
527 *tas = sas;
528 else if (as != sas) {
529 *tas = &bad_address_space;
530 sparse_error(pos, "multiple address spaces given");
531 }
532 }
533
534 #endif /* SYMBOL_H */
535