Clean up and split declaration_specifiers()
[smatch.git] / symbol.h
blob07da99c51dcd2295f69738fada99a34068d66ebe
1 #ifndef SYMBOL_H
2 #define SYMBOL_H
3 /*
4 * Basic symbol and namespace definitions.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
12 #include "token.h"
13 #include "target.h"
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
21 * rules.
23 * The progression is symbol -> token -> identifier. The
24 * token contains the information on where the symbol was
25 * declared.
27 enum namespace {
28 NS_NONE = 0,
29 NS_MACRO = 1,
30 NS_TYPEDEF = 2,
31 NS_STRUCT = 4, // Also used for unions and enums.
32 NS_LABEL = 8,
33 NS_SYMBOL = 16,
34 NS_ITERATOR = 32,
35 NS_PREPROCESSOR = 64,
36 NS_UNDEF = 128,
37 NS_KEYWORD = 256,
40 enum type {
41 SYM_UNINITIALIZED,
42 SYM_PREPROCESSOR,
43 SYM_BASETYPE,
44 SYM_NODE,
45 SYM_PTR,
46 SYM_FN,
47 SYM_ARRAY,
48 SYM_STRUCT,
49 SYM_UNION,
50 SYM_ENUM,
51 SYM_TYPEDEF,
52 SYM_TYPEOF,
53 SYM_MEMBER,
54 SYM_BITFIELD,
55 SYM_LABEL,
56 SYM_RESTRICT,
57 SYM_FOULED,
58 SYM_KEYWORD,
59 SYM_BAD,
62 enum keyword {
63 KW_SPECIFIER = 1 << 0,
64 KW_MODIFIER = 1 << 1,
65 KW_QUALIFIER = 1 << 2,
66 KW_ATTRIBUTE = 1 << 3,
67 KW_STATEMENT = 1 << 4,
68 KW_ASM = 1 << 5,
69 KW_MODE = 1 << 6,
70 KW_SHORT = 1 << 7,
71 KW_LONG = 1 << 8,
72 KW_EXACT = 1 << 9,
75 struct context {
76 struct expression *context;
77 unsigned int in, out;
80 extern struct context *alloc_context(void);
82 DECLARE_PTR_LIST(context_list, struct context);
84 struct ctype {
85 unsigned long modifiers;
86 unsigned long alignment;
87 struct context_list *contexts;
88 unsigned int as;
89 struct symbol *base_type;
92 struct decl_state {
93 struct ctype ctype;
94 int prefer_abstract;
95 struct ident **ident;
98 struct symbol_op {
99 enum keyword type;
100 int (*evaluate)(struct expression *);
101 int (*expand)(struct expression *, int);
102 int (*args)(struct expression *);
104 /* keywords */
105 struct token *(*declarator)(struct token *token, struct ctype *ctype);
106 struct token *(*statement)(struct token *token, struct statement *stmt);
107 struct token *(*toplevel)(struct token *token, struct symbol_list **list);
108 struct token *(*attribute)(struct token *token, struct symbol *attr, struct ctype *ctype);
110 int test, set, class;
113 extern int expand_safe_p(struct expression *expr, int cost);
114 extern int expand_constant_p(struct expression *expr, int cost);
116 #define SYM_ATTR_WEAK 0
117 #define SYM_ATTR_NORMAL 1
118 #define SYM_ATTR_STRONG 2
120 struct symbol {
121 enum type type:8;
122 enum namespace namespace:9;
123 unsigned char used:1, attr:2, enum_member:1, bound:1;
124 struct position pos; /* Where this symbol was declared */
125 struct position endpos; /* Where this symbol ends*/
126 struct ident *ident; /* What identifier this symbol is associated with */
127 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
128 struct symbol *replace; /* What is this symbol shadowed by in copy-expression */
129 struct scope *scope;
130 union {
131 struct symbol *same_symbol;
132 struct symbol *next_subobject;
135 struct symbol_op *op;
137 union {
138 struct /* NS_MACRO */ {
139 struct token *expansion;
140 struct token *arglist;
141 struct scope *used_in;
143 struct /* NS_PREPROCESSOR */ {
144 int (*handler)(struct stream *, struct token **, struct token *);
145 int normal;
147 struct /* NS_SYMBOL */ {
148 unsigned long offset;
149 int bit_size;
150 unsigned int bit_offset:8,
151 arg_count:10,
152 variadic:1,
153 initialized:1,
154 examined:1,
155 expanding:1,
156 evaluated:1,
157 string:1;
158 struct expression *array_size;
159 struct ctype ctype;
160 struct symbol_list *arguments;
161 struct statement *stmt;
162 struct symbol_list *symbol_list;
163 struct statement *inline_stmt;
164 struct symbol_list *inline_symbol_list;
165 struct expression *initializer;
166 struct entrypoint *ep;
167 long long value; /* Initial value */
168 struct symbol *definition;
171 union /* backend */ {
172 struct basic_block *bb_target; /* label */
173 void *aux; /* Auxiliary info, e.g. backend information */
174 struct { /* sparse ctags */
175 char kind;
176 unsigned char visited:1;
179 pseudo_t pseudo;
182 /* Modifiers */
183 #define MOD_AUTO 0x0001
184 #define MOD_REGISTER 0x0002
185 #define MOD_STATIC 0x0004
186 #define MOD_EXTERN 0x0008
188 #define MOD_CONST 0x0010
189 #define MOD_VOLATILE 0x0020
190 #define MOD_SIGNED 0x0040
191 #define MOD_UNSIGNED 0x0080
193 #define MOD_CHAR 0x0100
194 #define MOD_SHORT 0x0200
195 #define MOD_LONG 0x0400
196 #define MOD_LONGLONG 0x0800
198 #define MOD_TYPEDEF 0x1000
200 #define MOD_INLINE 0x40000
201 #define MOD_ADDRESSABLE 0x80000
203 #define MOD_NOCAST 0x100000
204 #define MOD_NODEREF 0x200000
205 #define MOD_ACCESSED 0x400000
206 #define MOD_TOPLEVEL 0x800000 // scoping..
208 #define MOD_LABEL 0x1000000
209 #define MOD_ASSIGNED 0x2000000
210 #define MOD_TYPE 0x4000000
211 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
213 #define MOD_USERTYPE 0x10000000
214 #define MOD_FORCE 0x20000000
215 #define MOD_EXPLICITLY_SIGNED 0x40000000
216 #define MOD_BITWISE 0x80000000
218 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
219 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL | MOD_FORCE)
220 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
221 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNEDNESS)
222 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
223 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
224 MOD_ASSIGNED | MOD_USERTYPE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
225 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
228 /* Current parsing/evaluation function */
229 extern struct symbol *current_fn;
231 /* Abstract types */
232 extern struct symbol int_type,
233 fp_type;
235 /* C types */
236 extern struct symbol bool_ctype, void_ctype, type_ctype,
237 char_ctype, schar_ctype, uchar_ctype,
238 short_ctype, sshort_ctype, ushort_ctype,
239 int_ctype, sint_ctype, uint_ctype,
240 long_ctype, slong_ctype, ulong_ctype,
241 llong_ctype, sllong_ctype, ullong_ctype,
242 float_ctype, double_ctype, ldouble_ctype,
243 string_ctype, ptr_ctype, lazy_ptr_ctype,
244 incomplete_ctype, label_ctype, bad_ctype,
245 null_ctype;
247 /* Special internal symbols */
248 extern struct symbol zero_int;
250 #define __IDENT(n,str,res) \
251 extern struct ident n
252 #include "ident-list.h"
254 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
256 extern struct symbol_list *translation_unit_used_list;
258 extern void access_symbol(struct symbol *);
260 extern const char * type_difference(struct ctype *c1, struct ctype *c2,
261 unsigned long mod1, unsigned long mod2);
263 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
264 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
265 extern void init_symbols(void);
266 extern void init_ctype(void);
267 extern struct symbol *alloc_symbol(struct position, int type);
268 extern void show_type(struct symbol *);
269 extern const char *modifier_string(unsigned long mod);
270 extern void show_symbol(struct symbol *);
271 extern int show_symbol_expr_init(struct symbol *sym);
272 extern void show_type_list(struct symbol *);
273 extern void show_symbol_list(struct symbol_list *, const char *);
274 extern void add_symbol(struct symbol_list **, struct symbol *);
275 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
277 extern struct symbol *examine_symbol_type(struct symbol *);
278 extern struct symbol *examine_pointer_target(struct symbol *);
279 extern void examine_simple_symbol_type(struct symbol *);
280 extern const char *show_typename(struct symbol *sym);
281 extern const char *builtin_typename(struct symbol *sym);
282 extern const char *builtin_ctypename(struct ctype *ctype);
283 extern const char* get_type_name(enum type type);
285 extern void debug_symbol(struct symbol *);
286 extern void merge_type(struct symbol *sym, struct symbol *base_type);
287 extern void check_declaration(struct symbol *sym);
289 static inline struct symbol *get_base_type(const struct symbol *sym)
291 return examine_symbol_type(sym->ctype.base_type);
294 static inline int is_int_type(const struct symbol *type)
296 if (type->type == SYM_NODE)
297 type = type->ctype.base_type;
298 if (type->type == SYM_ENUM)
299 type = type->ctype.base_type;
300 return type->type == SYM_BITFIELD ||
301 type->ctype.base_type == &int_type;
304 static inline int is_enum_type(const struct symbol *type)
306 if (type->type == SYM_NODE)
307 type = type->ctype.base_type;
308 return (type->type == SYM_ENUM);
311 static inline int is_type_type(struct symbol *type)
313 return (type->ctype.modifiers & MOD_TYPE) != 0;
316 static inline int is_ptr_type(struct symbol *type)
318 if (type->type == SYM_NODE)
319 type = type->ctype.base_type;
320 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
323 static inline int is_float_type(struct symbol *type)
325 if (type->type == SYM_NODE)
326 type = type->ctype.base_type;
327 return type->ctype.base_type == &fp_type;
330 static inline int is_byte_type(struct symbol *type)
332 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
335 static inline int is_void_type(struct symbol *type)
337 if (type->type == SYM_NODE)
338 type = type->ctype.base_type;
339 return type == &void_ctype;
342 static inline int get_sym_type(struct symbol *type)
344 if (type->type == SYM_NODE)
345 type = type->ctype.base_type;
346 if (type->type == SYM_ENUM)
347 type = type->ctype.base_type;
348 return type->type;
351 static inline struct symbol *lookup_keyword(struct ident *ident, enum namespace ns)
353 if (!ident->keyword)
354 return NULL;
355 return lookup_symbol(ident, ns);
358 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
359 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
360 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
361 extern int is_ptr_type(struct symbol *);
363 void create_fouled(struct symbol *type);
364 struct symbol *befoul(struct symbol *type);
366 #endif /* SYMBOL_H */