buf_size: allow strncmp("foo", bar, 100) where 100 is larger than "foo"
[smatch.git] / symbol.h
blobf615cc879b9ffbf1729aa24ed5bd43cbc970c61d
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"
30 #include "allocate.h"
33 * An identifier with semantic meaning is a "symbol".
35 * There's a 1:n relationship: each symbol is always
36 * associated with one identifier, while each identifier
37 * can have one or more semantic meanings due to C scope
38 * rules.
40 * The progression is symbol -> token -> identifier. The
41 * token contains the information on where the symbol was
42 * declared.
44 enum namespace {
45 NS_NONE = 0,
46 NS_MACRO = 1,
47 NS_TYPEDEF = 2,
48 NS_STRUCT = 4, // Also used for unions and enums.
49 NS_LABEL = 8,
50 NS_SYMBOL = 16,
51 NS_ITERATOR = 32,
52 NS_PREPROCESSOR = 64,
53 NS_UNDEF = 128,
54 NS_KEYWORD = 256,
57 enum type {
58 SYM_UNINITIALIZED,
59 SYM_PREPROCESSOR,
60 SYM_BASETYPE,
61 SYM_NODE,
62 SYM_PTR,
63 SYM_FN,
64 SYM_ARRAY,
65 SYM_STRUCT,
66 SYM_UNION,
67 SYM_ENUM,
68 SYM_TYPEDEF,
69 SYM_TYPEOF,
70 SYM_MEMBER,
71 SYM_BITFIELD,
72 SYM_LABEL,
73 SYM_RESTRICT,
74 SYM_FOULED,
75 SYM_KEYWORD,
76 SYM_BAD,
79 enum keyword {
80 KW_SPECIFIER = 1 << 0,
81 KW_MODIFIER = 1 << 1,
82 KW_QUALIFIER = 1 << 2,
83 KW_ATTRIBUTE = 1 << 3,
84 KW_STATEMENT = 1 << 4,
85 KW_ASM = 1 << 5,
86 KW_MODE = 1 << 6,
87 KW_SHORT = 1 << 7,
88 KW_LONG = 1 << 8,
89 KW_EXACT = 1 << 9,
92 struct context {
93 struct expression *context;
94 unsigned int in, out;
97 extern struct context *alloc_context(void);
99 struct attribute {
100 struct context_list *contexts;
101 unsigned int as;
102 unsigned int is_packed:1;
106 struct ctype {
107 unsigned long modifiers;
108 unsigned long alignment;
109 struct attribute *attribute;
110 struct symbol *base_type;
113 struct decl_state {
114 struct ctype ctype;
115 struct ident **ident;
116 struct symbol_op *mode;
117 unsigned char prefer_abstract, is_inline, storage_class, is_tls;
120 struct symbol_op {
121 enum keyword type;
122 int (*evaluate)(struct expression *);
123 int (*expand)(struct expression *, int);
124 int (*args)(struct expression *);
126 /* keywords */
127 struct token *(*declarator)(struct token *token, struct decl_state *ctx);
128 struct token *(*statement)(struct token *token, struct statement *stmt);
129 struct token *(*toplevel)(struct token *token, struct symbol_list **list);
130 struct token *(*attribute)(struct token *token, struct symbol *attr, struct decl_state *ctx);
131 struct symbol *(*to_mode)(struct symbol *);
133 int test, set, class;
136 extern int expand_safe_p(struct expression *expr, int cost);
137 extern int expand_constant_p(struct expression *expr, int cost);
139 #define SYM_ATTR_WEAK 0
140 #define SYM_ATTR_NORMAL 1
141 #define SYM_ATTR_STRONG 2
143 struct symbol {
144 enum type type:8;
145 enum namespace namespace:9;
146 unsigned char used:1, attr:2, enum_member:1, bound:1;
147 struct position pos; /* Where this symbol was declared */
148 struct position endpos; /* Where this symbol ends*/
149 struct ident *ident; /* What identifier this symbol is associated with */
150 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
151 struct symbol *replace; /* What is this symbol shadowed by in copy-expression */
152 struct scope *scope;
153 union {
154 struct symbol *same_symbol;
155 struct symbol *next_subobject;
158 struct symbol_op *op;
160 union {
161 struct /* NS_MACRO */ {
162 struct token *expansion;
163 struct token *arglist;
164 struct scope *used_in;
166 struct /* NS_PREPROCESSOR */ {
167 int (*handler)(struct stream *, struct token **, struct token *);
168 int normal;
170 struct /* NS_SYMBOL */ {
171 unsigned long offset;
172 int bit_size;
173 unsigned int bit_offset:8,
174 arg_count:10,
175 variadic:1,
176 initialized:1,
177 examined:1,
178 expanding:1,
179 evaluated:1,
180 string:1,
181 designated_init:1,
182 forced_arg:1;
183 struct expression *array_size;
184 struct ctype ctype;
185 struct symbol_list *arguments;
186 struct statement *stmt;
187 struct symbol_list *symbol_list;
188 struct statement *inline_stmt;
189 struct symbol_list *inline_symbol_list;
190 struct expression *initializer;
191 struct entrypoint *ep;
192 long long value; /* Initial value */
193 struct symbol *definition;
196 union /* backend */ {
197 struct basic_block *bb_target; /* label */
198 void *aux; /* Auxiliary info, e.g. backend information */
199 struct { /* sparse ctags */
200 char kind;
201 unsigned char visited:1;
204 pseudo_t pseudo;
207 /* Modifiers */
208 #define MOD_AUTO 0x0001
209 #define MOD_REGISTER 0x0002
210 #define MOD_STATIC 0x0004
211 #define MOD_EXTERN 0x0008
213 #define MOD_CONST 0x0010
214 #define MOD_VOLATILE 0x0020
215 #define MOD_SIGNED 0x0040
216 #define MOD_UNSIGNED 0x0080
218 #define MOD_CHAR 0x0100
219 #define MOD_SHORT 0x0200
220 #define MOD_LONG 0x0400
221 #define MOD_LONGLONG 0x0800
222 #define MOD_LONGLONGLONG 0x1000
223 #define MOD_PURE 0x2000
225 #define MOD_TYPEDEF 0x10000
227 #define MOD_TLS 0x20000
228 #define MOD_INLINE 0x40000
229 #define MOD_ADDRESSABLE 0x80000
231 #define MOD_NOCAST 0x100000
232 #define MOD_NODEREF 0x200000
233 #define MOD_ACCESSED 0x400000
234 #define MOD_TOPLEVEL 0x800000 // scoping..
236 #define MOD_ASSIGNED 0x2000000
237 #define MOD_TYPE 0x4000000
238 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
240 #define MOD_USERTYPE 0x10000000
241 #define MOD_NORETURN 0x20000000
242 #define MOD_EXPLICITLY_SIGNED 0x40000000
243 #define MOD_BITWISE 0x80000000
246 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
247 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
248 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
249 #define MOD_LONG_ALL (MOD_LONG | MOD_LONGLONG | MOD_LONGLONGLONG)
250 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL | MOD_SIGNEDNESS)
251 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL)
252 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
253 MOD_ASSIGNED | MOD_USERTYPE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
254 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE | MOD_NORETURN)
256 /* default empty attribute */
257 extern struct attribute null_attr;
259 /* Current parsing/evaluation function */
260 extern struct symbol *current_fn;
262 /* Abstract types */
263 extern struct symbol int_type,
264 fp_type;
266 /* C types */
267 extern struct symbol bool_ctype, void_ctype, type_ctype,
268 char_ctype, schar_ctype, uchar_ctype,
269 short_ctype, sshort_ctype, ushort_ctype,
270 int_ctype, sint_ctype, uint_ctype,
271 long_ctype, slong_ctype, ulong_ctype,
272 llong_ctype, sllong_ctype, ullong_ctype,
273 lllong_ctype, slllong_ctype, ulllong_ctype,
274 float_ctype, double_ctype, ldouble_ctype,
275 string_ctype, ptr_ctype, lazy_ptr_ctype,
276 incomplete_ctype, label_ctype, bad_ctype,
277 null_ctype;
279 /* Special internal symbols */
280 extern struct symbol zero_int;
282 #define __IDENT(n,str,res) \
283 extern struct ident n
284 #include "ident-list.h"
286 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
288 extern struct symbol_list *translation_unit_used_list;
290 extern void access_symbol(struct symbol *);
292 extern const char * type_difference(struct ctype *c1, struct ctype *c2,
293 unsigned long mod1, unsigned long mod2);
295 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
296 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
297 extern void init_symbols(void);
298 extern void init_ctype(void);
299 extern struct symbol *alloc_symbol(struct position, int type);
300 extern void show_type(struct symbol *);
301 extern const char *modifier_string(unsigned long mod);
302 extern void show_symbol(struct symbol *);
303 extern int show_symbol_expr_init(struct symbol *sym);
304 extern void show_type_list(struct symbol *);
305 extern void show_symbol_list(struct symbol_list *, const char *);
306 extern void add_symbol(struct symbol_list **, struct symbol *);
307 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
309 extern struct symbol *examine_symbol_type(struct symbol *);
310 extern struct symbol *examine_pointer_target(struct symbol *);
311 extern void examine_simple_symbol_type(struct symbol *);
312 extern const char *show_typename(struct symbol *sym);
313 extern const char *builtin_typename(struct symbol *sym);
314 extern const char *builtin_ctypename(struct ctype *ctype);
315 extern const char* get_type_name(enum type type);
317 extern void debug_symbol(struct symbol *);
318 extern void merge_type(struct symbol *sym, struct symbol *base_type);
319 extern void check_declaration(struct symbol *sym);
321 static inline struct symbol *get_base_type(const struct symbol *sym)
323 return examine_symbol_type(sym->ctype.base_type);
326 static inline int is_int_type(const struct symbol *type)
328 if (type->type == SYM_NODE)
329 type = type->ctype.base_type;
330 if (type->type == SYM_ENUM)
331 type = type->ctype.base_type;
332 return type->type == SYM_BITFIELD ||
333 type->ctype.base_type == &int_type;
336 static inline int is_enum_type(const struct symbol *type)
338 if (type->type == SYM_NODE)
339 type = type->ctype.base_type;
340 return (type->type == SYM_ENUM);
343 static inline int is_type_type(struct symbol *type)
345 return (type->ctype.modifiers & MOD_TYPE) != 0;
348 static inline int is_ptr_type(struct symbol *type)
350 if (type->type == SYM_NODE)
351 type = type->ctype.base_type;
352 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
355 static inline int is_float_type(struct symbol *type)
357 if (type->type == SYM_NODE)
358 type = type->ctype.base_type;
359 return type->ctype.base_type == &fp_type;
362 static inline int is_byte_type(struct symbol *type)
364 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
367 static inline int is_void_type(struct symbol *type)
369 if (type->type == SYM_NODE)
370 type = type->ctype.base_type;
371 return type == &void_ctype;
374 static inline int is_bool_type(struct symbol *type)
376 if (type->type == SYM_NODE)
377 type = type->ctype.base_type;
378 return type == &bool_ctype;
381 static inline int is_function(struct symbol *type)
383 return type && type->type == SYM_FN;
386 static inline int is_extern_inline(struct symbol *sym)
388 return (sym->ctype.modifiers & MOD_EXTERN) &&
389 (sym->ctype.modifiers & MOD_INLINE) &&
390 is_function(sym->ctype.base_type);
393 static inline int get_sym_type(struct symbol *type)
395 if (type->type == SYM_NODE)
396 type = type->ctype.base_type;
397 if (type->type == SYM_ENUM)
398 type = type->ctype.base_type;
399 return type->type;
402 static inline struct symbol *lookup_keyword(struct ident *ident, enum namespace ns)
404 if (!ident->keyword)
405 return NULL;
406 return lookup_symbol(ident, ns);
409 static inline struct attribute *duplicate_attribute(struct attribute *attr)
411 struct attribute *newattr = __alloc_attribute(0);
412 *newattr = *attr;
413 return newattr;
416 static inline void attr_set_as(struct ctype *ctype, unsigned int as)
418 if (ctype->attribute->as != as) {
419 ctype->attribute = duplicate_attribute(ctype->attribute);
420 ctype->attribute->as = as;
424 static inline void attr_add_context(struct ctype *ctype, struct context *context)
426 ctype->attribute = duplicate_attribute(ctype->attribute);
427 add_ptr_list(&ctype->attribute->contexts, context);
430 static inline void merge_attr(struct ctype *dst, struct ctype *src)
432 struct attribute *attr;
434 if (src->attribute == &null_attr)
435 return;
436 if (dst->attribute == &null_attr) {
437 dst->attribute = src->attribute;
438 return;
441 dst->attribute = attr = duplicate_attribute(dst->attribute);
442 attr->as |= src->attribute->as;
443 concat_ptr_list((struct ptr_list *)src->attribute->contexts,
444 (struct ptr_list **)&attr->contexts);
447 static inline void set_attr_is_packed(struct ctype *ctype)
449 if (ctype->attribute == &null_attr)
450 ctype->attribute = __alloc_attribute(0);
451 ctype->attribute->is_packed = 1;
454 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
455 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
456 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
457 extern int is_ptr_type(struct symbol *);
459 void create_fouled(struct symbol *type);
460 struct symbol *befoul(struct symbol *type);
462 #endif /* SYMBOL_H */