4 * Basic symbol and namespace definitions.
6 * Copyright (C) 2003 Transmeta Corp.
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
32 * An identifier with semantic meaning is a "symbol".
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
39 * The progression is symbol -> token -> identifier. The
40 * token contains the information on where the symbol was
47 NS_STRUCT
= 4, // Also used for unions and enums.
77 KW_SPECIFIER
= 1 << 0,
79 KW_QUALIFIER
= 1 << 2,
80 KW_ATTRIBUTE
= 1 << 3,
85 // KW UNUSED = 1 << 8,
90 struct expression
*context
;
94 extern struct context
*alloc_context(void);
96 DECLARE_PTR_LIST(context_list
, struct context
);
99 struct symbol
*base_type
;
100 unsigned long modifiers
;
101 unsigned long alignment
;
102 struct context_list
*contexts
;
108 struct ident
**ident
;
109 struct symbol_op
*mode
;
110 unsigned long f_modifiers
; // function attributes
111 unsigned long storage_class
;
112 unsigned char prefer_abstract
;
113 unsigned char autotype
;
114 unsigned char forced
;
115 unsigned char packed
;
124 int (*evaluate
)(struct expression
*);
125 int (*expand
)(struct expression
*, int);
126 int (*args
)(struct expression
*);
127 struct pseudo
*(*linearize
)(struct entrypoint
*, struct expression
*);
130 struct token
*(*declarator
)(struct token
*token
, struct symbol
*, struct decl_state
*ctx
);
131 struct token
*(*statement
)(struct token
*token
, struct statement
*stmt
);
132 struct token
*(*toplevel
)(struct token
*token
, struct symbol_list
**list
);
133 struct token
*(*attribute
)(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
);
134 struct symbol
*(*to_mode
)(struct symbol
*);
135 void (*asm_modifier
)(struct token
*token
, unsigned long *mods
, unsigned long mod
);
137 int test
, set
, class;
141 #define SYM_ATTR_WEAK 0
142 #define SYM_ATTR_NORMAL 1
143 #define SYM_ATTR_STRONG 2
147 enum namespace namespace:9;
148 unsigned char used
:1, attr
:2, enum_member
:1, bound
:1, parsed
:1;
149 struct position pos
; /* Where this symbol was declared */
150 struct position endpos
; /* Where this symbol ends*/
151 struct ident
*ident
; /* What identifier this symbol is associated with */
152 struct symbol
*next_id
; /* Next semantic symbol that shares this identifier */
153 struct symbol
*replace
; /* What is this symbol shadowed by in copy-expression */
156 struct symbol
*same_symbol
;
157 struct symbol
*next_subobject
;
160 struct symbol_op
*op
;
163 struct /* NS_MACRO */ {
164 struct token
*expansion
;
165 struct token
*arglist
;
166 struct scope
*used_in
;
167 void (*expand_simple
)(struct token
*);
168 bool (*expand
)(struct token
*, struct arg
*args
);
170 struct /* NS_PREPROCESSOR */ {
171 int (*handler
)(struct stream
*, struct token
**, struct token
*);
174 struct /* NS_LABEL */ {
175 struct scope
*label_scope
;
176 struct position label_pos
;
177 unsigned long label_modifiers
;
179 struct /* NS_SYMBOL */ {
180 unsigned long offset
;
182 unsigned int bit_offset
:8,
198 int rank
:3; // arithmetic's rank
199 struct expression
*array_size
;
201 struct symbol_list
*arguments
;
202 struct statement
*stmt
;
203 struct symbol_list
*symbol_list
;
204 struct statement
*inline_stmt
;
205 struct symbol_list
*inline_symbol_list
;
206 struct expression
*initializer
;
207 struct entrypoint
*ep
;
208 struct symbol
*definition
;
211 union /* backend */ {
212 struct basic_block
*bb_target
; /* label */
213 void *aux
; /* Auxiliary info, e.g. backend information */
215 char kind
; /* used by ctags & dissect */
216 unsigned char visited
:1;
217 unsigned char inspected
:1;
224 #define MOD_AUTO 0x00000001
225 #define MOD_REGISTER 0x00000002
226 #define MOD_STATIC 0x00000004
227 #define MOD_EXTERN 0x00000008
228 #define MOD_TOPLEVEL 0x00000010 // scoping..
229 #define MOD_TLS 0x00000020
230 #define MOD_ASM_GOTO MOD_TLS // never used together
231 #define MOD_INLINE 0x00000040
233 #define MOD_ASSIGNED 0x00000080
234 #define MOD_ADDRESSABLE 0x00000100
236 #define MOD_CONST 0x00000200
237 #define MOD_VOLATILE 0x00000400
238 #define MOD_RESTRICT 0x00000800
239 #define MOD_ATOMIC 0x00001000
241 #define MOD_SIGNED 0x00002000
242 #define MOD_UNSIGNED 0x00004000
243 #define MOD_EXPLICITLY_SIGNED 0x00008000
245 #define MOD_GNU_INLINE 0x00010000
246 #define MOD_USERTYPE 0x00020000
247 // MOD UNUSED 0x00040000
248 // MOD UNUSED 0x00080000
249 // MOD UNUSED 0x00100000
250 // MOD UNUSED 0x00200000
252 #define MOD_UNUSED 0x00400000
253 #define MOD_SAFE 0x00800000 // non-null/non-trapping pointer
254 #define MOD_PURE 0x01000000
255 #define MOD_BITWISE 0x02000000
256 #define MOD_NOCAST 0x04000000
257 #define MOD_NODEREF 0x08000000
258 #define MOD_NORETURN 0x10000000
259 #define MOD_EXT_VISIBLE 0x20000000
262 #define MOD_ACCESS (MOD_ASSIGNED | MOD_ADDRESSABLE)
263 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
264 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
265 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
266 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
267 #define MOD_SPECIFIER MOD_SIGNEDNESS
268 #define MOD_IGNORE (MOD_STORAGE | MOD_ACCESS | MOD_USERTYPE | MOD_EXPLICITLY_SIGNED | MOD_EXT_VISIBLE | MOD_UNUSED | MOD_GNU_INLINE)
269 #define MOD_QUALIFIER (MOD_CONST | MOD_VOLATILE | MOD_RESTRICT)
270 #define MOD_PTRINHERIT (MOD_QUALIFIER | MOD_ATOMIC | MOD_NODEREF | MOD_NORETURN | MOD_NOCAST)
271 /* modifiers preserved by typeof() operator */
272 #define MOD_TYPEOF (MOD_QUALIFIER | MOD_ATOMIC | MOD_NOCAST | MOD_SPECIFIER)
273 /* modifiers for function attributes */
274 #define MOD_FUN_ATTR (MOD_PURE|MOD_NORETURN)
275 /* like cvr-qualifiers but 'reversed' (OK: source <= target) */
276 #define MOD_REV_QUAL (MOD_PURE|MOD_NORETURN)
277 /* do not warn when these are duplicated */
278 #define MOD_DUP_OK (MOD_UNUSED|MOD_GNU_INLINE)
279 /* must be part of the declared symbol, not its type */
280 #define MOD_DECLARE (MOD_STORAGE|MOD_INLINE|MOD_TLS|MOD_GNU_INLINE|MOD_UNUSED|MOD_PURE|MOD_NORETURN|MOD_EXT_VISIBLE)
284 /* Current parsing/evaluation function */
285 extern struct symbol
*current_fn
;
288 extern struct symbol int_type
,
292 extern struct symbol bool_ctype
, void_ctype
, type_ctype
,
293 char_ctype
, schar_ctype
, uchar_ctype
,
294 short_ctype
, sshort_ctype
, ushort_ctype
,
295 int_ctype
, sint_ctype
, uint_ctype
,
296 long_ctype
, slong_ctype
, ulong_ctype
,
297 llong_ctype
, sllong_ctype
, ullong_ctype
,
298 int128_ctype
, sint128_ctype
, uint128_ctype
,
299 float_ctype
, double_ctype
, ldouble_ctype
,
300 string_ctype
, ptr_ctype
, lazy_ptr_ctype
,
301 incomplete_ctype
, label_ctype
, bad_ctype
,
303 extern struct symbol autotype_ctype
;
304 extern struct symbol schar_ptr_ctype
, short_ptr_ctype
;
305 extern struct symbol int_ptr_ctype
, uint_ptr_ctype
;
306 extern struct symbol long_ptr_ctype
, ulong_ptr_ctype
;
307 extern struct symbol llong_ptr_ctype
, ullong_ptr_ctype
;
308 extern struct symbol size_t_ptr_ctype
, intmax_ptr_ctype
, ptrdiff_ptr_ctype
;
309 extern struct symbol float32_ctype
, float32x_ctype
;
310 extern struct symbol float64_ctype
, float64x_ctype
;
311 extern struct symbol float128_ctype
;
312 extern struct symbol const_void_ctype
, const_char_ctype
;
313 extern struct symbol const_ptr_ctype
, const_string_ctype
;
314 extern struct symbol const_wchar_ctype
, const_wstring_ctype
;
315 extern struct symbol volatile_void_ctype
, volatile_ptr_ctype
;
316 extern struct symbol volatile_bool_ctype
, volatile_bool_ptr_ctype
;
318 /* Special internal symbols */
319 extern struct symbol zero_int
;
321 #define __IDENT(n,str,res) \
322 extern struct ident n
323 #include "ident-list.h"
326 extern struct symbol_list
*translation_unit_used_list
;
328 extern void access_symbol(struct symbol
*);
330 extern const char * type_difference(struct ctype
*c1
, struct ctype
*c2
,
331 unsigned long mod1
, unsigned long mod2
);
333 extern struct symbol
*lookup_symbol(struct ident
*, enum namespace);
334 struct symbol
*lookup_macro_symbol(const char *macro
);
335 extern struct symbol
*create_symbol(int stream
, const char *name
, int type
, int namespace);
336 extern void init_symbols(void);
337 extern void init_builtins(int stream
);
338 extern void init_linearized_builtins(int stream
);
339 extern void init_ctype(void);
340 extern struct symbol
*alloc_symbol(struct position
, int type
);
341 extern void show_type(struct symbol
*);
342 extern const char *modifier_name(unsigned long mod
);
343 extern const char *modifier_string(unsigned long mod
);
344 extern void show_symbol(struct symbol
*);
345 extern int show_symbol_expr_init(struct symbol
*sym
);
346 extern void show_type_list(struct symbol
*);
347 extern void show_symbol_list(struct symbol_list
*);
348 extern void add_symbol(struct symbol_list
**, struct symbol
*);
349 extern void bind_symbol(struct symbol
*, struct ident
*, enum namespace);
350 extern void bind_symbol_with_scope(struct symbol
*, struct ident
*, enum namespace, struct scope
*);
352 extern struct symbol
*examine_symbol_type(struct symbol
*);
353 extern struct symbol
*examine_pointer_target(struct symbol
*);
354 extern const char *show_as(struct ident
*as
);
355 extern const char *show_typename(struct symbol
*sym
);
356 extern const char *builtin_typename(struct symbol
*sym
);
357 extern const char *builtin_type_suffix(struct symbol
*sym
);
358 extern const char *builtin_ctypename(struct ctype
*ctype
);
359 extern const char* get_type_name(enum type type
);
361 extern void debug_symbol(struct symbol
*);
362 extern void merge_type(struct symbol
*sym
, struct symbol
*base_type
);
363 extern void check_declaration(struct symbol
*sym
);
364 extern void check_duplicates(struct symbol
*sym
);
366 static inline int valid_type(const struct symbol
*ctype
)
368 return ctype
&& ctype
!= &bad_ctype
;
371 static inline struct symbol
*get_base_type(const struct symbol
*sym
)
373 return examine_symbol_type(sym
->ctype
.base_type
);
377 // test if type is an integer type
379 // @return: ``1`` for plain integer type, enums & bitfields
380 // but ``0`` for bitwise types!
381 static inline int is_int_type(const struct symbol
*type
)
383 if (type
->type
== SYM_NODE
)
384 type
= type
->ctype
.base_type
;
385 if (type
->type
== SYM_ENUM
)
386 type
= type
->ctype
.base_type
;
387 return type
->type
== SYM_BITFIELD
||
388 type
->ctype
.base_type
== &int_type
;
391 static inline int is_enum_type(const struct symbol
*type
)
393 if (type
->type
== SYM_NODE
)
394 type
= type
->ctype
.base_type
;
395 return (type
->type
== SYM_ENUM
);
398 static inline int is_signed_type(struct symbol
*sym
)
400 if (sym
->type
== SYM_NODE
)
401 sym
= sym
->ctype
.base_type
;
402 if (sym
->type
== SYM_PTR
)
404 return !(sym
->ctype
.modifiers
& MOD_UNSIGNED
);
407 static inline int is_type_type(struct symbol
*type
)
409 return type
== &type_ctype
;
412 static inline int is_ptr_type(struct symbol
*type
)
416 if (type
->type
== SYM_NODE
)
417 type
= type
->ctype
.base_type
;
418 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
421 static inline int is_func_type(struct symbol
*type
)
423 if (type
->type
== SYM_NODE
)
424 type
= type
->ctype
.base_type
;
425 return type
->type
== SYM_FN
;
428 static inline int is_array_type(struct symbol
*type
)
430 if (type
->type
== SYM_NODE
)
431 type
= type
->ctype
.base_type
;
432 return type
->type
== SYM_ARRAY
;
435 static inline int is_struct_type(struct symbol
*type
)
437 if (type
->type
== SYM_NODE
)
438 type
= type
->ctype
.base_type
;
439 return type
->type
== SYM_STRUCT
;
442 static inline int is_union_type(struct symbol
*type
)
444 if (type
->type
== SYM_NODE
)
445 type
= type
->ctype
.base_type
;
446 return type
->type
== SYM_UNION
;
449 static inline int is_float_type(struct symbol
*type
)
451 if (type
->type
== SYM_NODE
)
452 type
= type
->ctype
.base_type
;
453 return type
->ctype
.base_type
== &fp_type
;
456 static inline int is_byte_type(struct symbol
*type
)
458 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
461 static inline int is_wchar_type(struct symbol
*type
)
463 if (type
->type
== SYM_NODE
)
464 type
= type
->ctype
.base_type
;
465 return type
== wchar_ctype
;
468 static inline int is_void_type(struct symbol
*type
)
470 if (type
->type
== SYM_NODE
)
471 type
= type
->ctype
.base_type
;
472 return type
== &void_ctype
;
475 static inline int is_bool_type(struct symbol
*type
)
477 if (type
->type
== SYM_NODE
)
478 type
= type
->ctype
.base_type
;
479 return type
== &bool_ctype
;
482 static inline int is_scalar_type(struct symbol
*type
)
484 if (type
->type
== SYM_NODE
)
485 type
= type
->ctype
.base_type
;
486 switch (type
->type
) {
490 case SYM_RESTRICT
: // OK, always integer types
491 case SYM_FOULED
: // idem
496 if (type
->ctype
.base_type
== &int_type
)
498 if (type
->ctype
.base_type
== &fp_type
)
503 /// return true for integer & pointer types
504 static inline bool is_integral_type(struct symbol
*type
)
506 if (type
->type
== SYM_NODE
)
507 type
= type
->ctype
.base_type
;
508 switch (type
->type
) {
511 case SYM_RESTRICT
: // OK, always integer types
512 case SYM_FOULED
: // idem
517 if (type
->ctype
.base_type
== &int_type
)
522 static inline int is_function(struct symbol
*type
)
524 return type
&& type
->type
== SYM_FN
;
527 static inline int is_extern_inline(struct symbol
*sym
)
529 return (sym
->ctype
.modifiers
& MOD_EXTERN
) &&
530 (sym
->ctype
.modifiers
& MOD_INLINE
) &&
531 is_function(sym
->ctype
.base_type
);
534 static inline int has_flexible_array(struct symbol
*type
)
536 if (type
->type
== SYM_NODE
)
537 type
= type
->ctype
.base_type
;
538 return type
->has_flex_array
;
541 static inline int get_sym_type(struct symbol
*type
)
543 if (type
->type
== SYM_NODE
)
544 type
= type
->ctype
.base_type
;
545 if (type
->type
== SYM_ENUM
)
546 type
= type
->ctype
.base_type
;
550 static inline long long extend_value(long long val
, struct symbol
*ctype
)
552 int is_signed
= !(ctype
->ctype
.modifiers
& MOD_UNSIGNED
);
553 unsigned size
= ctype
->bit_size
;
555 return bits_extend(val
, size
, is_signed
);
558 static inline struct symbol
*lookup_keyword(struct ident
*ident
, enum namespace ns
)
562 return lookup_symbol(ident
, ns
);
565 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
566 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
567 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
569 void create_fouled(struct symbol
*type
);
570 struct symbol
*befoul(struct symbol
*type
);
573 extern struct ident bad_address_space
;
575 static inline bool valid_as(struct ident
*as
)
577 return as
&& as
!= &bad_address_space
;
580 static inline void combine_address_space(struct position pos
,
581 struct ident
**tas
, struct ident
*sas
)
589 else if (as
!= sas
) {
590 *tas
= &bad_address_space
;
591 sparse_error(pos
, "multiple address spaces given");
595 #endif /* SYMBOL_H */