dereferences_param: mark more parameters as dereferenced
[smatch.git] / symbol.h
blob166f0e6bea2b9616a4c97ece47265b2a910ca929
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_TYPEOF,
68 SYM_BITFIELD,
69 SYM_LABEL,
70 SYM_RESTRICT,
71 SYM_FOULED,
72 SYM_KEYWORD,
73 SYM_BAD,
76 enum keyword {
77 KW_SPECIFIER = 1 << 0,
78 KW_MODIFIER = 1 << 1,
79 KW_QUALIFIER = 1 << 2,
80 KW_ATTRIBUTE = 1 << 3,
81 KW_BUILTIN = 1 << 4,
82 KW_ASM = 1 << 5,
83 KW_MODE = 1 << 6,
84 // KW UNUSED = 1 << 7,
85 // KW UNUSED = 1 << 8,
86 KW_EXACT = 1 << 9,
89 struct context {
90 struct expression *context;
91 unsigned int in, out;
94 extern struct context *alloc_context(void);
96 DECLARE_PTR_LIST(context_list, struct context);
98 struct ctype {
99 struct symbol *base_type;
100 unsigned long modifiers;
101 unsigned long alignment;
102 struct context_list *contexts;
103 struct ident *as;
106 struct decl_state {
107 struct ctype ctype;
108 struct ident **ident;
109 struct symbol_op *mode;
110 unsigned long f_modifiers; // function attributes
111 unsigned char prefer_abstract, storage_class;
112 unsigned char autotype;
115 struct pseudo;
116 struct entrypoint;
117 struct arg;
119 struct symbol_op {
120 enum keyword type;
121 int (*evaluate)(struct expression *);
122 int (*expand)(struct expression *, int);
123 int (*args)(struct expression *);
124 struct pseudo *(*linearize)(struct entrypoint *, 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 *);
132 void (*asm_modifier)(struct token *token, unsigned long *mods);
134 int test, set, class;
138 #define SYM_ATTR_WEAK 0
139 #define SYM_ATTR_NORMAL 1
140 #define SYM_ATTR_STRONG 2
142 struct symbol {
143 enum type type:8;
144 enum namespace namespace:9;
145 unsigned char used:1, attr:2, enum_member:1, bound:1;
146 struct position pos; /* Where this symbol was declared */
147 struct position endpos; /* Where this symbol ends*/
148 struct ident *ident; /* What identifier this symbol is associated with */
149 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
150 struct symbol *replace; /* What is this symbol shadowed by in copy-expression */
151 struct scope *scope;
152 union {
153 struct symbol *same_symbol;
154 struct symbol *next_subobject;
157 struct symbol_op *op;
159 union {
160 struct /* NS_MACRO */ {
161 struct token *expansion;
162 struct token *arglist;
163 struct scope *used_in;
164 void (*expand_simple)(struct token *);
165 bool (*expand)(struct token *, struct arg *args);
167 struct /* NS_PREPROCESSOR */ {
168 int (*handler)(struct stream *, struct token **, struct token *);
169 int normal;
171 struct /* NS_LABEL */ {
172 struct scope *label_scope;
173 struct position label_pos;
174 unsigned long label_modifiers;
176 struct /* NS_SYMBOL */ {
177 unsigned long offset;
178 int bit_size;
179 unsigned int bit_offset:8,
180 bogus_linear:1,
181 variadic:1,
182 initialized:1,
183 examined:1,
184 expanding:1,
185 evaluated:1,
186 string:1,
187 designated_init:1,
188 forced_arg:1,
189 accessed:1,
190 builtin:1,
191 torename:1,
192 transparent_union:1;
193 int rank:3; // arithmetic's rank
194 struct expression *array_size;
195 struct ctype ctype;
196 struct symbol_list *arguments;
197 struct statement *stmt;
198 struct symbol_list *symbol_list;
199 struct statement *inline_stmt;
200 struct symbol_list *inline_symbol_list;
201 struct expression *initializer;
202 struct entrypoint *ep;
203 struct symbol *definition;
206 union /* backend */ {
207 struct basic_block *bb_target; /* label */
208 void *aux; /* Auxiliary info, e.g. backend information */
209 struct { /* sparse ctags */
210 char kind;
211 unsigned char visited:1;
214 pseudo_t pseudo;
217 /* Modifiers */
218 #define MOD_AUTO 0x00000001
219 #define MOD_REGISTER 0x00000002
220 #define MOD_STATIC 0x00000004
221 #define MOD_EXTERN 0x00000008
222 #define MOD_TOPLEVEL 0x00000010 // scoping..
223 #define MOD_TLS 0x00000020
224 #define MOD_ASM_GOTO MOD_TLS // never used together
225 #define MOD_INLINE 0x00000040
227 #define MOD_ASSIGNED 0x00000080
228 #define MOD_ADDRESSABLE 0x00000100
230 #define MOD_CONST 0x00000200
231 #define MOD_VOLATILE 0x00000400
232 #define MOD_RESTRICT 0x00000800
233 #define MOD_ATOMIC 0x00001000
235 #define MOD_SIGNED 0x00002000
236 #define MOD_UNSIGNED 0x00004000
237 #define MOD_EXPLICITLY_SIGNED 0x00008000
239 #define MOD_GNU_INLINE 0x00010000
240 #define MOD_USERTYPE 0x00020000
241 // MOD UNUSED 0x00040000
242 // MOD UNUSED 0x00080000
243 // MOD UNUSED 0x00100000
244 // MOD UNUSED 0x00200000
246 #define MOD_UNUSED 0x00400000
247 #define MOD_SAFE 0x00800000 // non-null/non-trapping pointer
248 #define MOD_PURE 0x01000000
249 #define MOD_BITWISE 0x02000000
250 #define MOD_NOCAST 0x04000000
251 #define MOD_NODEREF 0x08000000
252 #define MOD_NORETURN 0x10000000
253 #define MOD_EXT_VISIBLE 0x20000000
256 #define MOD_ACCESS (MOD_ASSIGNED | MOD_ADDRESSABLE)
257 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
258 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
259 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
260 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
261 #define MOD_SPECIFIER MOD_SIGNEDNESS
262 #define MOD_IGNORE (MOD_STORAGE | MOD_ACCESS | MOD_USERTYPE | MOD_EXPLICITLY_SIGNED | MOD_EXT_VISIBLE | MOD_UNUSED | MOD_GNU_INLINE)
263 #define MOD_QUALIFIER (MOD_CONST | MOD_VOLATILE | MOD_RESTRICT)
264 #define MOD_PTRINHERIT (MOD_QUALIFIER | MOD_ATOMIC | MOD_NODEREF | MOD_NORETURN | MOD_NOCAST)
265 /* modifiers preserved by typeof() operator */
266 #define MOD_TYPEOF (MOD_QUALIFIER | MOD_ATOMIC | MOD_NOCAST | MOD_SPECIFIER)
267 /* modifiers for function attributes */
268 #define MOD_FUN_ATTR (MOD_PURE|MOD_NORETURN)
269 /* like cvr-qualifiers but 'reversed' (OK: source <= target) */
270 #define MOD_REV_QUAL (MOD_PURE|MOD_NORETURN)
271 /* do not warn when these are duplicated */
272 #define MOD_DUP_OK (MOD_UNUSED|MOD_GNU_INLINE)
273 /* must be part of the declared symbol, not its type */
274 #define MOD_DECLARE (MOD_STORAGE|MOD_INLINE|MOD_TLS|MOD_GNU_INLINE|MOD_UNUSED|MOD_PURE|MOD_NORETURN|MOD_EXT_VISIBLE)
278 /* Current parsing/evaluation function */
279 extern struct symbol *current_fn;
281 /* Abstract types */
282 extern struct symbol int_type,
283 fp_type;
285 /* C types */
286 extern struct symbol bool_ctype, void_ctype, type_ctype,
287 char_ctype, schar_ctype, uchar_ctype,
288 short_ctype, sshort_ctype, ushort_ctype,
289 int_ctype, sint_ctype, uint_ctype,
290 long_ctype, slong_ctype, ulong_ctype,
291 llong_ctype, sllong_ctype, ullong_ctype,
292 int128_ctype, sint128_ctype, uint128_ctype,
293 float_ctype, double_ctype, ldouble_ctype,
294 string_ctype, ptr_ctype, lazy_ptr_ctype,
295 incomplete_ctype, label_ctype, bad_ctype,
296 null_ctype;
297 extern struct symbol autotype_ctype;
298 extern struct symbol int_ptr_ctype, uint_ptr_ctype;
299 extern struct symbol long_ptr_ctype, ulong_ptr_ctype;
300 extern struct symbol llong_ptr_ctype, ullong_ptr_ctype;
301 extern struct symbol float32_ctype, float32x_ctype;
302 extern struct symbol float64_ctype, float64x_ctype;
303 extern struct symbol float128_ctype;
304 extern struct symbol const_void_ctype, const_char_ctype;
305 extern struct symbol const_ptr_ctype, const_string_ctype;
307 #define uintptr_ctype size_t_ctype
308 #define intptr_ctype ssize_t_ctype
310 /* Special internal symbols */
311 extern struct symbol zero_int;
313 #define __IDENT(n,str,res) \
314 extern struct ident n
315 #include "ident-list.h"
318 extern struct symbol_list *translation_unit_used_list;
320 extern void access_symbol(struct symbol *);
322 extern const char * type_difference(struct ctype *c1, struct ctype *c2,
323 unsigned long mod1, unsigned long mod2);
325 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
326 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
327 extern void init_symbols(void);
328 extern void init_builtins(int stream);
329 extern void init_linearized_builtins(int stream);
330 extern void init_ctype(void);
331 extern struct symbol *alloc_symbol(struct position, int type);
332 extern void show_type(struct symbol *);
333 extern const char *modifier_string(unsigned long mod);
334 extern void show_symbol(struct symbol *);
335 extern int show_symbol_expr_init(struct symbol *sym);
336 extern void show_type_list(struct symbol *);
337 extern void show_symbol_list(struct symbol_list *, const char *);
338 extern void add_symbol(struct symbol_list **, struct symbol *);
339 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
340 extern void bind_symbol_with_scope(struct symbol *, struct ident *, enum namespace, struct scope *);
342 extern struct symbol *examine_symbol_type(struct symbol *);
343 extern struct symbol *examine_pointer_target(struct symbol *);
344 extern const char *show_as(struct ident *as);
345 extern const char *show_typename(struct symbol *sym);
346 extern const char *builtin_typename(struct symbol *sym);
347 extern const char *builtin_type_suffix(struct symbol *sym);
348 extern const char *builtin_ctypename(struct ctype *ctype);
349 extern const char* get_type_name(enum type type);
351 extern void debug_symbol(struct symbol *);
352 extern void merge_type(struct symbol *sym, struct symbol *base_type);
353 extern void check_declaration(struct symbol *sym);
354 extern void check_duplicates(struct symbol *sym);
356 static inline int valid_type(const struct symbol *ctype)
358 return ctype && ctype != &bad_ctype;
361 static inline struct symbol *get_base_type(const struct symbol *sym)
363 return examine_symbol_type(sym->ctype.base_type);
367 // test if type is an integer type
369 // @return: ``1`` for plain integer type, enums & bitfields
370 // but ``0`` for bitwise types!
371 static inline int is_int_type(const struct symbol *type)
373 if (type->type == SYM_NODE)
374 type = type->ctype.base_type;
375 if (type->type == SYM_ENUM)
376 type = type->ctype.base_type;
377 return type->type == SYM_BITFIELD ||
378 type->ctype.base_type == &int_type;
381 static inline int is_enum_type(const struct symbol *type)
383 if (type->type == SYM_NODE)
384 type = type->ctype.base_type;
385 return (type->type == SYM_ENUM);
388 static inline int is_signed_type(struct symbol *sym)
390 if (sym->type == SYM_NODE)
391 sym = sym->ctype.base_type;
392 if (sym->type == SYM_PTR)
393 return 0;
394 return !(sym->ctype.modifiers & MOD_UNSIGNED);
397 static inline int is_type_type(struct symbol *type)
399 return type == &type_ctype;
402 static inline int is_ptr_type(struct symbol *type)
404 if (!type)
405 return 0;
406 if (type->type == SYM_NODE)
407 type = type->ctype.base_type;
408 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
411 static inline int is_func_type(struct symbol *type)
413 if (type->type == SYM_NODE)
414 type = type->ctype.base_type;
415 return type->type == SYM_FN;
418 static inline int is_array_type(struct symbol *type)
420 if (type->type == SYM_NODE)
421 type = type->ctype.base_type;
422 return type->type == SYM_ARRAY;
425 static inline int is_float_type(struct symbol *type)
427 if (type->type == SYM_NODE)
428 type = type->ctype.base_type;
429 return type->ctype.base_type == &fp_type;
432 static inline int is_byte_type(struct symbol *type)
434 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
437 static inline int is_void_type(struct symbol *type)
439 if (type->type == SYM_NODE)
440 type = type->ctype.base_type;
441 return type == &void_ctype;
444 static inline int is_bool_type(struct symbol *type)
446 if (type->type == SYM_NODE)
447 type = type->ctype.base_type;
448 return type == &bool_ctype;
451 static inline int is_scalar_type(struct symbol *type)
453 if (type->type == SYM_NODE)
454 type = type->ctype.base_type;
455 switch (type->type) {
456 case SYM_ENUM:
457 case SYM_BITFIELD:
458 case SYM_PTR:
459 case SYM_RESTRICT: // OK, always integer types
460 return 1;
461 default:
462 break;
464 if (type->ctype.base_type == &int_type)
465 return 1;
466 if (type->ctype.base_type == &fp_type)
467 return 1;
468 return 0;
471 /// return true for integer & pointer types
472 static inline bool is_integral_type(struct symbol *type)
474 if (type->type == SYM_NODE)
475 type = type->ctype.base_type;
476 switch (type->type) {
477 case SYM_ENUM:
478 case SYM_PTR:
479 case SYM_RESTRICT: // OK, always integer types
480 return 1;
481 default:
482 break;
484 if (type->ctype.base_type == &int_type)
485 return 1;
486 return 0;
489 static inline int is_function(struct symbol *type)
491 return type && type->type == SYM_FN;
494 static inline int is_extern_inline(struct symbol *sym)
496 return (sym->ctype.modifiers & MOD_EXTERN) &&
497 (sym->ctype.modifiers & MOD_INLINE) &&
498 is_function(sym->ctype.base_type);
501 static inline int get_sym_type(struct symbol *type)
503 if (type->type == SYM_NODE)
504 type = type->ctype.base_type;
505 if (type->type == SYM_ENUM)
506 type = type->ctype.base_type;
507 return type->type;
510 static inline long long extend_value(long long val, struct symbol *ctype)
512 int is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
513 unsigned size = ctype->bit_size;
515 return bits_extend(val, size, is_signed);
518 static inline struct symbol *lookup_keyword(struct ident *ident, enum namespace ns)
520 if (!ident->keyword)
521 return NULL;
522 return lookup_symbol(ident, ns);
525 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
526 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
527 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
529 void create_fouled(struct symbol *type);
530 struct symbol *befoul(struct symbol *type);
533 extern struct ident bad_address_space;
535 static inline bool valid_as(struct ident *as)
537 return as && as != &bad_address_space;
540 static inline void combine_address_space(struct position pos,
541 struct ident **tas, struct ident *sas)
543 struct ident *as;
544 if (!sas)
545 return;
546 as = *tas;
547 if (!as)
548 *tas = sas;
549 else if (as != sas) {
550 *tas = &bad_address_space;
551 sparse_error(pos, "multiple address spaces given");
555 #endif /* SYMBOL_H */