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,
68 KW_STATEMENT
= 1 << 5,
74 struct expression
*context
;
78 extern struct context
*alloc_context(void);
80 DECLARE_PTR_LIST(context_list
, struct context
);
83 unsigned long modifiers
;
84 unsigned long alignment
;
85 struct context_list
*contexts
;
87 struct symbol
*base_type
;
92 int (*evaluate
)(struct expression
*);
93 int (*expand
)(struct expression
*, int);
94 int (*args
)(struct expression
*);
97 struct token
*(*declarator
)(struct token
*token
, struct ctype
*ctype
);
98 struct token
*(*statement
)(struct token
*token
, struct statement
*stmt
);
99 struct token
*(*toplevel
)(struct token
*token
, struct symbol_list
**list
);
100 struct token
*(*attribute
)(struct token
*token
, struct symbol
*attr
, struct ctype
*ctype
);
103 extern int expand_safe_p(struct expression
*expr
, int cost
);
104 extern int expand_constant_p(struct expression
*expr
, int cost
);
106 #define SYM_ATTR_WEAK 0
107 #define SYM_ATTR_NORMAL 1
108 #define SYM_ATTR_STRONG 2
112 enum namespace namespace:9;
113 unsigned char used
:1, attr
:2, enum_member
:1, bound
:1;
114 struct position pos
; /* Where this symbol was declared */
115 struct position endpos
; /* Where this symbol ends*/
116 struct ident
*ident
; /* What identifier this symbol is associated with */
117 struct symbol
*next_id
; /* Next semantic symbol that shares this identifier */
118 struct symbol
*replace
; /* What is this symbol shadowed by in copy-expression */
121 struct symbol
*same_symbol
;
122 struct symbol
*next_subobject
;
125 struct symbol_op
*op
;
128 struct /* NS_MACRO */ {
129 struct token
*expansion
;
130 struct token
*arglist
;
131 struct scope
*used_in
;
133 struct /* NS_PREPROCESSOR */ {
134 int (*handler
)(struct stream
*, struct token
**, struct token
*);
137 struct /* NS_SYMBOL */ {
138 unsigned long offset
;
140 unsigned int bit_offset
:8,
148 struct expression
*array_size
;
150 struct symbol_list
*arguments
;
151 struct statement
*stmt
;
152 struct symbol_list
*symbol_list
;
153 struct statement
*inline_stmt
;
154 struct symbol_list
*inline_symbol_list
;
155 struct expression
*initializer
;
156 struct entrypoint
*ep
;
157 long long value
; /* Initial value */
158 struct symbol
*definition
;
161 union /* backend */ {
162 struct basic_block
*bb_target
; /* label */
163 void *aux
; /* Auxiliary info, e.g. backend information */
164 struct { /* sparse ctags */
166 unsigned char visited
:1;
173 #define MOD_AUTO 0x0001
174 #define MOD_REGISTER 0x0002
175 #define MOD_STATIC 0x0004
176 #define MOD_EXTERN 0x0008
178 #define MOD_CONST 0x0010
179 #define MOD_VOLATILE 0x0020
180 #define MOD_SIGNED 0x0040
181 #define MOD_UNSIGNED 0x0080
183 #define MOD_CHAR 0x0100
184 #define MOD_SHORT 0x0200
185 #define MOD_LONG 0x0400
186 #define MOD_LONGLONG 0x0800
188 #define MOD_TYPEDEF 0x1000
190 #define MOD_INLINE 0x40000
191 #define MOD_ADDRESSABLE 0x80000
193 #define MOD_NOCAST 0x100000
194 #define MOD_NODEREF 0x200000
195 #define MOD_ACCESSED 0x400000
196 #define MOD_TOPLEVEL 0x800000 // scoping..
198 #define MOD_LABEL 0x1000000
199 #define MOD_ASSIGNED 0x2000000
200 #define MOD_TYPE 0x4000000
201 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
203 #define MOD_USERTYPE 0x10000000
204 #define MOD_FORCE 0x20000000
205 #define MOD_EXPLICITLY_SIGNED 0x40000000
206 #define MOD_BITWISE 0x80000000
208 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
209 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL | MOD_FORCE)
210 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
211 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNEDNESS)
212 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
213 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
214 MOD_ASSIGNED | MOD_USERTYPE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
215 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
218 /* Current parsing/evaluation function */
219 extern struct symbol
*current_fn
;
222 extern struct symbol int_type
,
226 extern struct symbol bool_ctype
, void_ctype
, type_ctype
,
227 char_ctype
, schar_ctype
, uchar_ctype
,
228 short_ctype
, sshort_ctype
, ushort_ctype
,
229 int_ctype
, sint_ctype
, uint_ctype
,
230 long_ctype
, slong_ctype
, ulong_ctype
,
231 llong_ctype
, sllong_ctype
, ullong_ctype
,
232 float_ctype
, double_ctype
, ldouble_ctype
,
233 string_ctype
, ptr_ctype
, lazy_ptr_ctype
,
234 incomplete_ctype
, label_ctype
, bad_ctype
,
237 /* Special internal symbols */
238 extern struct symbol zero_int
;
240 #define __IDENT(n,str,res) \
241 extern struct ident n
242 #include "ident-list.h"
244 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
246 extern struct symbol_list
*translation_unit_used_list
;
248 extern void access_symbol(struct symbol
*);
250 extern const char * type_difference(struct ctype
*c1
, struct ctype
*c2
,
251 unsigned long mod1
, unsigned long mod2
);
253 extern struct symbol
*lookup_symbol(struct ident
*, enum namespace);
254 extern struct symbol
*create_symbol(int stream
, const char *name
, int type
, int namespace);
255 extern void init_symbols(void);
256 extern void init_ctype(void);
257 extern struct symbol
*alloc_symbol(struct position
, int type
);
258 extern void show_type(struct symbol
*);
259 extern const char *modifier_string(unsigned long mod
);
260 extern void show_symbol(struct symbol
*);
261 extern int show_symbol_expr_init(struct symbol
*sym
);
262 extern void show_type_list(struct symbol
*);
263 extern void show_symbol_list(struct symbol_list
*, const char *);
264 extern void add_symbol(struct symbol_list
**, struct symbol
*);
265 extern void bind_symbol(struct symbol
*, struct ident
*, enum namespace);
267 extern struct symbol
*examine_symbol_type(struct symbol
*);
268 extern struct symbol
*examine_pointer_target(struct symbol
*);
269 extern void examine_simple_symbol_type(struct symbol
*);
270 extern const char *show_typename(struct symbol
*sym
);
271 extern const char *builtin_typename(struct symbol
*sym
);
272 extern const char *builtin_ctypename(struct ctype
*ctype
);
273 extern const char* get_type_name(enum type type
);
275 extern void debug_symbol(struct symbol
*);
276 extern void merge_type(struct symbol
*sym
, struct symbol
*base_type
);
277 extern void check_declaration(struct symbol
*sym
);
279 static inline struct symbol
*get_base_type(const struct symbol
*sym
)
281 return examine_symbol_type(sym
->ctype
.base_type
);
284 static inline int is_int_type(const struct symbol
*type
)
286 if (type
->type
== SYM_NODE
)
287 type
= type
->ctype
.base_type
;
288 if (type
->type
== SYM_ENUM
)
289 type
= type
->ctype
.base_type
;
290 return type
->type
== SYM_BITFIELD
||
291 type
->ctype
.base_type
== &int_type
;
294 static inline int is_enum_type(const struct symbol
*type
)
296 if (type
->type
== SYM_NODE
)
297 type
= type
->ctype
.base_type
;
298 return (type
->type
== SYM_ENUM
);
301 static inline int is_type_type(struct symbol
*type
)
303 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
306 static inline int is_ptr_type(struct symbol
*type
)
308 if (type
->type
== SYM_NODE
)
309 type
= type
->ctype
.base_type
;
310 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
313 static inline int is_float_type(struct symbol
*type
)
315 if (type
->type
== SYM_NODE
)
316 type
= type
->ctype
.base_type
;
317 return type
->ctype
.base_type
== &fp_type
;
320 static inline int is_byte_type(struct symbol
*type
)
322 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
325 static inline int is_void_type(struct symbol
*type
)
327 if (type
->type
== SYM_NODE
)
328 type
= type
->ctype
.base_type
;
329 return type
== &void_ctype
;
332 static inline int get_sym_type(struct symbol
*type
)
334 if (type
->type
== SYM_NODE
)
335 type
= type
->ctype
.base_type
;
336 if (type
->type
== SYM_ENUM
)
337 type
= type
->ctype
.base_type
;
341 static inline struct symbol
*lookup_keyword(struct ident
*ident
, enum namespace ns
)
345 return lookup_symbol(ident
, ns
);
348 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
349 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
350 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
351 extern int is_ptr_type(struct symbol
*);
353 void create_fouled(struct symbol
*type
);
354 struct symbol
*befoul(struct symbol
*type
);
356 #endif /* SYMBOL_H */