4 * Basic symbol and namespace definitions.
6 * Copyright (C) 2003 Transmeta Corp.
9 * Licensed under the Open Software License version 1.1
16 * An identifier with semantic meaning is a "symbol".
18 * There's a 1:n relationship: each symbol is always
19 * associated with one identifier, while each identifier
20 * can have one or more semantic meanings due to C scope
23 * The progression is symbol -> token -> identifier. The
24 * token contains the information on where the symbol was
31 NS_STRUCT
= 4, // Also used for unions and enums.
63 KW_SPECIFIER
= 1 << 0,
65 KW_QUALIFIER
= 1 << 2,
66 KW_ATTRIBUTE
= 1 << 3,
67 KW_STATEMENT
= 1 << 4,
76 struct expression
*context
;
80 extern struct context
*alloc_context(void);
82 DECLARE_PTR_LIST(context_list
, struct context
);
85 unsigned long modifiers
;
86 unsigned long alignment
;
87 struct context_list
*contexts
;
89 struct symbol
*base_type
;
95 struct symbol_op
*mode
;
96 unsigned char prefer_abstract
, is_inline
, storage_class
, is_tls
;
101 int (*evaluate
)(struct expression
*);
102 int (*expand
)(struct expression
*, int);
103 int (*args
)(struct expression
*);
106 struct token
*(*declarator
)(struct token
*token
, struct decl_state
*ctx
);
107 struct token
*(*statement
)(struct token
*token
, struct statement
*stmt
);
108 struct token
*(*toplevel
)(struct token
*token
, struct symbol_list
**list
);
109 struct token
*(*attribute
)(struct token
*token
, struct symbol
*attr
, struct decl_state
*ctx
);
110 struct symbol
*(*to_mode
)(struct symbol
*);
112 int test
, set
, class;
115 extern int expand_safe_p(struct expression
*expr
, int cost
);
116 extern int expand_constant_p(struct expression
*expr
, int cost
);
118 #define SYM_ATTR_WEAK 0
119 #define SYM_ATTR_NORMAL 1
120 #define SYM_ATTR_STRONG 2
124 enum namespace namespace:9;
125 unsigned char used
:1, attr
:2, enum_member
:1, bound
:1;
126 struct position pos
; /* Where this symbol was declared */
127 struct position endpos
; /* Where this symbol ends*/
128 struct ident
*ident
; /* What identifier this symbol is associated with */
129 struct symbol
*next_id
; /* Next semantic symbol that shares this identifier */
130 struct symbol
*replace
; /* What is this symbol shadowed by in copy-expression */
133 struct symbol
*same_symbol
;
134 struct symbol
*next_subobject
;
137 struct symbol_op
*op
;
140 struct /* NS_MACRO */ {
141 struct token
*expansion
;
142 struct token
*arglist
;
143 struct scope
*used_in
;
145 struct /* NS_PREPROCESSOR */ {
146 int (*handler
)(struct stream
*, struct token
**, struct token
*);
149 struct /* NS_SYMBOL */ {
150 unsigned long offset
;
152 unsigned int bit_offset
:8,
161 struct expression
*array_size
;
163 struct symbol_list
*arguments
;
164 struct statement
*stmt
;
165 struct symbol_list
*symbol_list
;
166 struct statement
*inline_stmt
;
167 struct symbol_list
*inline_symbol_list
;
168 struct expression
*initializer
;
169 struct entrypoint
*ep
;
170 long long value
; /* Initial value */
171 struct symbol
*definition
;
174 union /* backend */ {
175 struct basic_block
*bb_target
; /* label */
176 void *aux
; /* Auxiliary info, e.g. backend information */
177 struct { /* sparse ctags */
179 unsigned char visited
:1;
186 #define MOD_AUTO 0x0001
187 #define MOD_REGISTER 0x0002
188 #define MOD_STATIC 0x0004
189 #define MOD_EXTERN 0x0008
191 #define MOD_CONST 0x0010
192 #define MOD_VOLATILE 0x0020
193 #define MOD_SIGNED 0x0040
194 #define MOD_UNSIGNED 0x0080
196 #define MOD_CHAR 0x0100
197 #define MOD_SHORT 0x0200
198 #define MOD_LONG 0x0400
199 #define MOD_LONGLONG 0x0800
200 #define MOD_LONGLONGLONG 0x1000
202 #define MOD_TYPEDEF 0x10000
204 #define MOD_TLS 0x20000
205 #define MOD_INLINE 0x40000
206 #define MOD_ADDRESSABLE 0x80000
208 #define MOD_NOCAST 0x100000
209 #define MOD_NODEREF 0x200000
210 #define MOD_ACCESSED 0x400000
211 #define MOD_TOPLEVEL 0x800000 // scoping..
213 #define MOD_ASSIGNED 0x2000000
214 #define MOD_TYPE 0x4000000
215 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
217 #define MOD_USERTYPE 0x10000000
218 #define MOD_NORETURN 0x20000000
219 #define MOD_EXPLICITLY_SIGNED 0x40000000
220 #define MOD_BITWISE 0x80000000
223 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
224 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
225 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
226 #define MOD_LONG_ALL (MOD_LONG | MOD_LONGLONG | MOD_LONGLONGLONG)
227 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL | MOD_SIGNEDNESS)
228 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL)
229 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
230 MOD_ASSIGNED | MOD_USERTYPE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
231 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE | MOD_NORETURN)
234 /* Current parsing/evaluation function */
235 extern struct symbol
*current_fn
;
238 extern struct symbol int_type
,
242 extern struct symbol bool_ctype
, void_ctype
, type_ctype
,
243 char_ctype
, schar_ctype
, uchar_ctype
,
244 short_ctype
, sshort_ctype
, ushort_ctype
,
245 int_ctype
, sint_ctype
, uint_ctype
,
246 long_ctype
, slong_ctype
, ulong_ctype
,
247 llong_ctype
, sllong_ctype
, ullong_ctype
,
248 lllong_ctype
, slllong_ctype
, ulllong_ctype
,
249 float_ctype
, double_ctype
, ldouble_ctype
,
250 string_ctype
, ptr_ctype
, lazy_ptr_ctype
,
251 incomplete_ctype
, label_ctype
, bad_ctype
,
254 /* Special internal symbols */
255 extern struct symbol zero_int
;
257 #define __IDENT(n,str,res) \
258 extern struct ident n
259 #include "ident-list.h"
261 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
263 extern struct symbol_list
*translation_unit_used_list
;
265 extern void access_symbol(struct symbol
*);
267 extern const char * type_difference(struct ctype
*c1
, struct ctype
*c2
,
268 unsigned long mod1
, unsigned long mod2
);
270 extern struct symbol
*lookup_symbol(struct ident
*, enum namespace);
271 extern struct symbol
*create_symbol(int stream
, const char *name
, int type
, int namespace);
272 extern void init_symbols(void);
273 extern void init_ctype(void);
274 extern struct symbol
*alloc_symbol(struct position
, int type
);
275 extern void show_type(struct symbol
*);
276 extern const char *modifier_string(unsigned long mod
);
277 extern void show_symbol(struct symbol
*);
278 extern int show_symbol_expr_init(struct symbol
*sym
);
279 extern void show_type_list(struct symbol
*);
280 extern void show_symbol_list(struct symbol_list
*, const char *);
281 extern void add_symbol(struct symbol_list
**, struct symbol
*);
282 extern void bind_symbol(struct symbol
*, struct ident
*, enum namespace);
284 extern struct symbol
*examine_symbol_type(struct symbol
*);
285 extern struct symbol
*examine_pointer_target(struct symbol
*);
286 extern void examine_simple_symbol_type(struct symbol
*);
287 extern const char *show_typename(struct symbol
*sym
);
288 extern const char *builtin_typename(struct symbol
*sym
);
289 extern const char *builtin_ctypename(struct ctype
*ctype
);
290 extern const char* get_type_name(enum type type
);
292 extern void debug_symbol(struct symbol
*);
293 extern void merge_type(struct symbol
*sym
, struct symbol
*base_type
);
294 extern void check_declaration(struct symbol
*sym
);
296 static inline struct symbol
*get_base_type(const struct symbol
*sym
)
298 return examine_symbol_type(sym
->ctype
.base_type
);
301 static inline int is_int_type(const struct symbol
*type
)
303 if (type
->type
== SYM_NODE
)
304 type
= type
->ctype
.base_type
;
305 if (type
->type
== SYM_ENUM
)
306 type
= type
->ctype
.base_type
;
307 return type
->type
== SYM_BITFIELD
||
308 type
->ctype
.base_type
== &int_type
;
311 static inline int is_enum_type(const struct symbol
*type
)
313 if (type
->type
== SYM_NODE
)
314 type
= type
->ctype
.base_type
;
315 return (type
->type
== SYM_ENUM
);
318 static inline int is_type_type(struct symbol
*type
)
320 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
323 static inline int is_ptr_type(struct symbol
*type
)
325 if (type
->type
== SYM_NODE
)
326 type
= type
->ctype
.base_type
;
327 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
330 static inline int is_float_type(struct symbol
*type
)
332 if (type
->type
== SYM_NODE
)
333 type
= type
->ctype
.base_type
;
334 return type
->ctype
.base_type
== &fp_type
;
337 static inline int is_byte_type(struct symbol
*type
)
339 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
342 static inline int is_void_type(struct symbol
*type
)
344 if (type
->type
== SYM_NODE
)
345 type
= type
->ctype
.base_type
;
346 return type
== &void_ctype
;
349 static inline int is_bool_type(struct symbol
*type
)
351 if (type
->type
== SYM_NODE
)
352 type
= type
->ctype
.base_type
;
353 return type
== &bool_ctype
;
356 static inline int is_function(struct symbol
*type
)
358 return type
&& type
->type
== SYM_FN
;
361 static inline int is_extern_inline(struct symbol
*sym
)
363 return (sym
->ctype
.modifiers
& MOD_EXTERN
) &&
364 (sym
->ctype
.modifiers
& MOD_INLINE
) &&
365 is_function(sym
->ctype
.base_type
);
368 static inline int get_sym_type(struct symbol
*type
)
370 if (type
->type
== SYM_NODE
)
371 type
= type
->ctype
.base_type
;
372 if (type
->type
== SYM_ENUM
)
373 type
= type
->ctype
.base_type
;
377 static inline struct symbol
*lookup_keyword(struct ident
*ident
, enum namespace ns
)
381 return lookup_symbol(ident
, ns
);
384 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
385 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
386 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
387 extern int is_ptr_type(struct symbol
*);
389 void create_fouled(struct symbol
*type
);
390 struct symbol
*befoul(struct symbol
*type
);
392 #endif /* SYMBOL_H */