llvm: remove unneeded OP_COPY support
[smatch.git] / symbol.h
blob51be81fbcd6d8ab15f3b0431a77e22a2a313f2a5
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 * 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
25 * THE SOFTWARE.
28 #include "token.h"
29 #include "target.h"
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
37 * rules.
39 * The progression is symbol -> token -> identifier. The
40 * token contains the information on where the symbol was
41 * declared.
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,
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,
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,
91 struct context {
92 struct expression *context;
93 unsigned int in, out;
96 extern struct context *alloc_context(void);
98 DECLARE_PTR_LIST(context_list, struct context);
100 struct ctype {
101 unsigned long modifiers;
102 unsigned long alignment;
103 struct context_list *contexts;
104 unsigned int as;
105 struct symbol *base_type;
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;
115 struct symbol_op {
116 enum keyword type;
117 int (*evaluate)(struct expression *);
118 int (*expand)(struct expression *, int);
119 int (*args)(struct expression *);
121 /* keywords */
122 struct token *(*declarator)(struct token *token, struct decl_state *ctx);
123 struct token *(*statement)(struct token *token, struct statement *stmt);
124 struct token *(*toplevel)(struct token *token, struct symbol_list **list);
125 struct token *(*attribute)(struct token *token, struct symbol *attr, struct decl_state *ctx);
126 struct symbol *(*to_mode)(struct symbol *);
128 int test, set, class;
131 extern int expand_safe_p(struct expression *expr, int cost);
132 extern int expand_constant_p(struct expression *expr, int cost);
134 #define SYM_ATTR_WEAK 0
135 #define SYM_ATTR_NORMAL 1
136 #define SYM_ATTR_STRONG 2
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;
153 struct symbol_op *op;
155 union {
156 struct /* NS_MACRO */ {
157 struct token *expansion;
158 struct token *arglist;
159 struct scope *used_in;
161 struct /* NS_PREPROCESSOR */ {
162 int (*handler)(struct stream *, struct token **, struct token *);
163 int normal;
165 struct /* NS_SYMBOL */ {
166 unsigned long offset;
167 int bit_size;
168 unsigned int bit_offset:8,
169 arg_count:10,
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 transparent_union:1;
179 struct expression *array_size;
180 struct ctype ctype;
181 struct symbol_list *arguments;
182 struct statement *stmt;
183 struct symbol_list *symbol_list;
184 struct statement *inline_stmt;
185 struct symbol_list *inline_symbol_list;
186 struct expression *initializer;
187 struct entrypoint *ep;
188 long long value; /* Initial value */
189 struct symbol *definition;
192 union /* backend */ {
193 struct basic_block *bb_target; /* label */
194 void *aux; /* Auxiliary info, e.g. backend information */
195 struct { /* sparse ctags */
196 char kind;
197 unsigned char visited:1;
200 pseudo_t pseudo;
203 /* Modifiers */
204 #define MOD_AUTO 0x0001
205 #define MOD_REGISTER 0x0002
206 #define MOD_STATIC 0x0004
207 #define MOD_EXTERN 0x0008
209 #define MOD_CONST 0x0010
210 #define MOD_VOLATILE 0x0020
211 #define MOD_SIGNED 0x0040
212 #define MOD_UNSIGNED 0x0080
214 #define MOD_CHAR 0x0100
215 #define MOD_SHORT 0x0200
216 #define MOD_LONG 0x0400
217 #define MOD_LONGLONG 0x0800
218 #define MOD_LONGLONGLONG 0x1000
219 #define MOD_PURE 0x2000
221 #define MOD_TYPEDEF 0x10000
223 #define MOD_TLS 0x20000
224 #define MOD_INLINE 0x40000
225 #define MOD_ADDRESSABLE 0x80000
227 #define MOD_NOCAST 0x100000
228 #define MOD_NODEREF 0x200000
229 #define MOD_ACCESSED 0x400000
230 #define MOD_TOPLEVEL 0x800000 // scoping..
232 #define MOD_ASSIGNED 0x2000000
233 #define MOD_TYPE 0x4000000
234 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
236 #define MOD_USERTYPE 0x10000000
237 #define MOD_NORETURN 0x20000000
238 #define MOD_EXPLICITLY_SIGNED 0x40000000
239 #define MOD_BITWISE 0x80000000
242 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
243 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
244 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
245 #define MOD_LONG_ALL (MOD_LONG | MOD_LONGLONG | MOD_LONGLONGLONG)
246 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL | MOD_SIGNEDNESS)
247 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL)
248 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
249 MOD_ASSIGNED | MOD_USERTYPE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
250 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_NORETURN | MOD_NOCAST)
251 /* modifiers preserved by typeof() operator */
252 #define MOD_TYPEOF (MOD_VOLATILE | MOD_CONST | MOD_NOCAST | MOD_SPECIFIER)
255 /* Current parsing/evaluation function */
256 extern struct symbol *current_fn;
258 /* Abstract types */
259 extern struct symbol int_type,
260 fp_type;
262 /* C types */
263 extern struct symbol bool_ctype, void_ctype, type_ctype,
264 char_ctype, schar_ctype, uchar_ctype,
265 short_ctype, sshort_ctype, ushort_ctype,
266 int_ctype, sint_ctype, uint_ctype,
267 long_ctype, slong_ctype, ulong_ctype,
268 llong_ctype, sllong_ctype, ullong_ctype,
269 lllong_ctype, slllong_ctype, ulllong_ctype,
270 float_ctype, double_ctype, ldouble_ctype,
271 string_ctype, ptr_ctype, lazy_ptr_ctype,
272 incomplete_ctype, label_ctype, bad_ctype,
273 null_ctype;
275 /* Special internal symbols */
276 extern struct symbol zero_int;
278 #define __IDENT(n,str,res) \
279 extern struct ident n
280 #include "ident-list.h"
282 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
284 extern struct symbol_list *translation_unit_used_list;
286 extern void access_symbol(struct symbol *);
288 extern const char * type_difference(struct ctype *c1, struct ctype *c2,
289 unsigned long mod1, unsigned long mod2);
291 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
292 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
293 extern void init_symbols(void);
294 extern void init_ctype(void);
295 extern struct symbol *alloc_symbol(struct position, int type);
296 extern void show_type(struct symbol *);
297 extern const char *modifier_string(unsigned long mod);
298 extern void show_symbol(struct symbol *);
299 extern int show_symbol_expr_init(struct symbol *sym);
300 extern void show_type_list(struct symbol *);
301 extern void show_symbol_list(struct symbol_list *, const char *);
302 extern void add_symbol(struct symbol_list **, struct symbol *);
303 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
305 extern struct symbol *examine_symbol_type(struct symbol *);
306 extern struct symbol *examine_pointer_target(struct symbol *);
307 extern void examine_simple_symbol_type(struct symbol *);
308 extern const char *show_typename(struct symbol *sym);
309 extern const char *builtin_typename(struct symbol *sym);
310 extern const char *builtin_ctypename(struct ctype *ctype);
311 extern const char* get_type_name(enum type type);
313 extern void debug_symbol(struct symbol *);
314 extern void merge_type(struct symbol *sym, struct symbol *base_type);
315 extern void check_declaration(struct symbol *sym);
317 static inline struct symbol *get_base_type(const struct symbol *sym)
319 return examine_symbol_type(sym->ctype.base_type);
322 static inline int is_int_type(const struct symbol *type)
324 if (type->type == SYM_NODE)
325 type = type->ctype.base_type;
326 if (type->type == SYM_ENUM)
327 type = type->ctype.base_type;
328 return type->type == SYM_BITFIELD ||
329 type->ctype.base_type == &int_type;
332 static inline int is_enum_type(const struct symbol *type)
334 if (type->type == SYM_NODE)
335 type = type->ctype.base_type;
336 return (type->type == SYM_ENUM);
339 static inline int is_type_type(struct symbol *type)
341 return (type->ctype.modifiers & MOD_TYPE) != 0;
344 static inline int is_ptr_type(struct symbol *type)
346 if (type->type == SYM_NODE)
347 type = type->ctype.base_type;
348 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
351 static inline int is_float_type(struct symbol *type)
353 if (type->type == SYM_NODE)
354 type = type->ctype.base_type;
355 return type->ctype.base_type == &fp_type;
358 static inline int is_byte_type(struct symbol *type)
360 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
363 static inline int is_void_type(struct symbol *type)
365 if (type->type == SYM_NODE)
366 type = type->ctype.base_type;
367 return type == &void_ctype;
370 static inline int is_bool_type(struct symbol *type)
372 if (type->type == SYM_NODE)
373 type = type->ctype.base_type;
374 return type == &bool_ctype;
377 static inline int is_function(struct symbol *type)
379 return type && type->type == SYM_FN;
382 static inline int is_extern_inline(struct symbol *sym)
384 return (sym->ctype.modifiers & MOD_EXTERN) &&
385 (sym->ctype.modifiers & MOD_INLINE) &&
386 is_function(sym->ctype.base_type);
389 static inline int get_sym_type(struct symbol *type)
391 if (type->type == SYM_NODE)
392 type = type->ctype.base_type;
393 if (type->type == SYM_ENUM)
394 type = type->ctype.base_type;
395 return type->type;
398 static inline struct symbol *lookup_keyword(struct ident *ident, enum namespace ns)
400 if (!ident->keyword)
401 return NULL;
402 return lookup_symbol(ident, ns);
405 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
406 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
407 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
408 extern int is_ptr_type(struct symbol *);
410 void create_fouled(struct symbol *type);
411 struct symbol *befoul(struct symbol *type);
413 #endif /* SYMBOL_H */