states: rename unreachable() to is_unreachable()
[smatch.git] / symbol.h
blobe2c2ba9364706dc741642ba87dfcbbdb21ec9c2b
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_TYPEOF_UNQUAL,
69 SYM_BITFIELD,
70 SYM_LABEL,
71 SYM_RESTRICT,
72 SYM_FOULED,
73 SYM_KEYWORD,
74 SYM_BAD,
77 enum keyword {
78 KW_SPECIFIER = 1 << 0,
79 KW_MODIFIER = 1 << 1,
80 KW_QUALIFIER = 1 << 2,
81 KW_ATTRIBUTE = 1 << 3,
82 KW_BUILTIN = 1 << 4,
83 KW_ASM = 1 << 5,
84 KW_MODE = 1 << 6,
85 KW_STATIC = 1 << 7,
86 // KW UNUSED = 1 << 8,
87 KW_EXACT = 1 << 9,
90 struct context {
91 struct expression *context;
92 unsigned int in, out;
95 extern struct context *alloc_context(void);
97 DECLARE_PTR_LIST(context_list, struct context);
99 struct ctype {
100 struct symbol *base_type;
101 unsigned long modifiers;
102 unsigned long alignment;
103 struct context_list *contexts;
104 struct ident *as;
107 struct decl_state {
108 struct ctype ctype;
109 struct ident **ident;
110 struct symbol_op *mode;
111 struct expression *cleanup;
112 unsigned long f_modifiers; // function attributes
113 unsigned long storage_class;
114 unsigned char prefer_abstract;
115 unsigned char autotype;
116 unsigned char forced;
117 unsigned char packed;
120 struct pseudo;
121 struct entrypoint;
122 struct arg;
124 struct symbol_op {
125 enum keyword type;
126 int (*evaluate)(struct expression *);
127 int (*expand)(struct expression *, int);
128 int (*args)(struct expression *);
129 struct pseudo *(*linearize)(struct entrypoint *, struct expression *);
131 /* keywords */
132 struct token *(*declarator)(struct token *token, struct symbol *, struct decl_state *ctx);
133 struct token *(*statement)(struct token *token, struct statement *stmt);
134 struct token *(*toplevel)(struct token *token, struct symbol_list **list);
135 struct token *(*attribute)(struct token *token, struct symbol *attr, struct decl_state *ctx);
136 struct symbol *(*to_mode)(struct symbol *);
137 void (*asm_modifier)(struct token *token, unsigned long *mods, unsigned long mod);
139 int test, set, class;
143 #define SYM_ATTR_WEAK 0
144 #define SYM_ATTR_NORMAL 1
145 #define SYM_ATTR_STRONG 2
147 struct symbol {
148 enum type type:8;
149 enum namespace namespace:9;
150 unsigned char used:1, attr:2, enum_member:1, bound:1, parsed:1;
151 struct position pos; /* Where this symbol was declared */
152 struct position endpos; /* Where this symbol ends*/
153 struct ident *ident; /* What identifier this symbol is associated with */
154 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
155 struct symbol *replace; /* What is this symbol shadowed by in copy-expression */
156 struct scope *scope;
157 union {
158 struct symbol *same_symbol;
159 struct symbol *next_subobject;
162 struct symbol_op *op;
164 union {
165 struct /* NS_MACRO */ {
166 struct token *expansion;
167 struct token *arglist;
168 struct scope *used_in;
169 void (*expand_simple)(struct token *);
170 bool (*expand)(struct token *, struct arg *args);
172 struct /* NS_PREPROCESSOR */ {
173 int (*handler)(struct stream *, struct token **, struct token *);
174 int normal;
176 struct /* NS_LABEL */ {
177 struct scope *label_scope;
178 struct position label_pos;
179 unsigned long label_modifiers;
181 struct /* NS_SYMBOL */ {
182 unsigned long offset;
183 int bit_size;
184 unsigned int bit_offset:8,
185 bogus_linear:1,
186 variadic:1,
187 initialized:1,
188 examined:1,
189 expanding:1,
190 evaluated:1,
191 has_flex_array:1,
192 string:1,
193 designated_init:1,
194 forced_arg:1,
195 accessed:1,
196 builtin:1,
197 torename:1,
198 packed:1,
199 transparent_union:1;
200 int rank:3; // arithmetic's rank
201 struct expression *array_size;
202 struct ctype ctype;
203 struct symbol_list *arguments;
204 struct statement *stmt;
205 struct symbol_list *symbol_list;
206 struct statement *inline_stmt;
207 struct symbol_list *inline_symbol_list;
208 struct expression *initializer;
209 struct expression *cleanup;
210 struct entrypoint *ep;
211 struct symbol *definition;
214 union /* backend */ {
215 struct basic_block *bb_target; /* label */
216 void *aux; /* Auxiliary info, e.g. backend information */
217 struct {
218 char kind; /* used by ctags & dissect */
219 unsigned char visited:1;
220 unsigned char inspected:1;
223 pseudo_t pseudo;
226 /* Modifiers */
227 #define MOD_AUTO 0x00000001
228 #define MOD_REGISTER 0x00000002
229 #define MOD_STATIC 0x00000004
230 #define MOD_EXTERN 0x00000008
231 #define MOD_TOPLEVEL 0x00000010 // scoping..
232 #define MOD_TLS 0x00000020
233 #define MOD_ASM_GOTO MOD_TLS // never used together
234 #define MOD_INLINE 0x00000040
236 #define MOD_ASSIGNED 0x00000080
237 #define MOD_ADDRESSABLE 0x00000100
239 #define MOD_CONST 0x00000200
240 #define MOD_VOLATILE 0x00000400
241 #define MOD_RESTRICT 0x00000800
242 #define MOD_ATOMIC 0x00001000
244 #define MOD_SIGNED 0x00002000
245 #define MOD_UNSIGNED 0x00004000
246 #define MOD_EXPLICITLY_SIGNED 0x00008000
248 #define MOD_GNU_INLINE 0x00010000
249 #define MOD_USERTYPE 0x00020000
250 // MOD UNUSED 0x00040000
251 // MOD UNUSED 0x00080000
252 // MOD UNUSED 0x00100000
253 // MOD UNUSED 0x00200000
255 #define MOD_UNUSED 0x00400000
256 #define MOD_SAFE 0x00800000 // non-null/non-trapping pointer
257 #define MOD_PURE 0x01000000
258 #define MOD_BITWISE 0x02000000
259 #define MOD_NOCAST 0x04000000
260 #define MOD_NODEREF 0x08000000
261 #define MOD_NORETURN 0x10000000
262 #define MOD_EXT_VISIBLE 0x20000000
265 #define MOD_ACCESS (MOD_ASSIGNED | MOD_ADDRESSABLE)
266 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
267 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
268 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
269 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
270 #define MOD_SPECIFIER MOD_SIGNEDNESS
271 #define MOD_IGNORE (MOD_STORAGE | MOD_ACCESS | MOD_USERTYPE | MOD_EXPLICITLY_SIGNED | MOD_EXT_VISIBLE | MOD_UNUSED | MOD_GNU_INLINE)
272 #define MOD_QUALIFIER (MOD_CONST | MOD_VOLATILE | MOD_RESTRICT)
273 #define MOD_PTRINHERIT (MOD_QUALIFIER | MOD_ATOMIC | MOD_NODEREF | MOD_NORETURN | MOD_NOCAST)
274 /* modifiers preserved by typeof() operator */
275 #define MOD_TYPEOF (MOD_QUALIFIER | MOD_ATOMIC | MOD_NOCAST | MOD_SPECIFIER)
276 /* modifiers for function attributes */
277 #define MOD_FUN_ATTR (MOD_PURE|MOD_NORETURN)
278 /* like cvr-qualifiers but 'reversed' (OK: source <= target) */
279 #define MOD_REV_QUAL (MOD_PURE|MOD_NORETURN)
280 /* do not warn when these are duplicated */
281 #define MOD_DUP_OK (MOD_UNUSED|MOD_GNU_INLINE)
282 /* must be part of the declared symbol, not its type */
283 #define MOD_DECLARE (MOD_STORAGE|MOD_INLINE|MOD_TLS|MOD_GNU_INLINE|MOD_UNUSED|MOD_PURE|MOD_NORETURN|MOD_EXT_VISIBLE)
287 /* Current parsing/evaluation function */
288 extern struct symbol *current_fn;
290 /* Abstract types */
291 extern struct symbol int_type,
292 fp_type;
294 /* C types */
295 extern struct symbol bool_ctype, void_ctype, type_ctype,
296 char_ctype, schar_ctype, uchar_ctype,
297 short_ctype, sshort_ctype, ushort_ctype,
298 int_ctype, sint_ctype, uint_ctype,
299 long_ctype, slong_ctype, ulong_ctype,
300 llong_ctype, sllong_ctype, ullong_ctype,
301 int128_ctype, sint128_ctype, uint128_ctype,
302 float_ctype, double_ctype, ldouble_ctype,
303 string_ctype, ptr_ctype, lazy_ptr_ctype,
304 incomplete_ctype, label_ctype, bad_ctype,
305 null_ctype;
306 extern struct symbol autotype_ctype;
307 extern struct symbol schar_ptr_ctype, short_ptr_ctype;
308 extern struct symbol int_ptr_ctype, uint_ptr_ctype;
309 extern struct symbol long_ptr_ctype, ulong_ptr_ctype;
310 extern struct symbol llong_ptr_ctype, ullong_ptr_ctype;
311 extern struct symbol size_t_ptr_ctype, intmax_ptr_ctype, ptrdiff_ptr_ctype;
312 extern struct symbol float32_ctype, float32x_ctype;
313 extern struct symbol float64_ctype, float64x_ctype;
314 extern struct symbol float128_ctype;
315 extern struct symbol const_void_ctype, const_char_ctype;
316 extern struct symbol const_ptr_ctype, const_string_ctype;
317 extern struct symbol const_wchar_ctype, const_wstring_ctype;
318 extern struct symbol volatile_void_ctype, volatile_ptr_ctype;
319 extern struct symbol volatile_bool_ctype, volatile_bool_ptr_ctype;
321 /* Special internal symbols */
322 extern struct symbol zero_int;
324 #define __IDENT(n,str,res) \
325 extern struct ident n
326 #include "ident-list.h"
329 extern struct symbol_list *translation_unit_used_list;
331 extern void access_symbol(struct symbol *);
333 extern const char * type_difference(struct ctype *c1, struct ctype *c2,
334 unsigned long mod1, unsigned long mod2);
336 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
337 struct symbol *lookup_macro_symbol(const char *macro);
338 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
339 extern void init_symbols(void);
340 extern void init_builtins(int stream);
341 extern void init_linearized_builtins(int stream);
342 extern void init_ctype(void);
343 extern struct symbol *alloc_symbol(struct position, int type);
344 extern void show_type(struct symbol *);
345 extern const char *modifier_name(unsigned long mod);
346 extern const char *modifier_string(unsigned long mod);
347 extern void show_symbol(struct symbol *);
348 extern int show_symbol_expr_init(struct symbol *sym);
349 extern void show_type_list(struct symbol *);
350 extern void show_symbol_list(struct symbol_list *);
351 extern void add_symbol(struct symbol_list **, struct symbol *);
352 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
353 extern void bind_symbol_with_scope(struct symbol *, struct ident *, enum namespace, struct scope *);
355 extern struct symbol *examine_symbol_type(struct symbol *);
356 extern struct symbol *examine_pointer_target(struct symbol *);
357 extern const char *show_as(struct ident *as);
358 extern const char *show_typename(struct symbol *sym);
359 extern const char *builtin_typename(struct symbol *sym);
360 extern const char *builtin_type_suffix(struct symbol *sym);
361 extern const char *builtin_ctypename(struct ctype *ctype);
362 extern const char* get_type_name(enum type type);
364 extern void debug_symbol(struct symbol *);
365 extern void merge_type(struct symbol *sym, struct symbol *base_type);
366 extern void check_declaration(struct symbol *sym);
367 extern void check_duplicates(struct symbol *sym);
369 static inline int valid_type(const struct symbol *ctype)
371 return ctype && ctype != &bad_ctype;
374 static inline struct symbol *get_base_type(const struct symbol *sym)
376 return examine_symbol_type(sym->ctype.base_type);
380 // test if type is an integer type
382 // @return: ``1`` for plain integer type, enums & bitfields
383 // but ``0`` for bitwise types!
384 static inline int is_int_type(const struct symbol *type)
386 if (type->type == SYM_NODE)
387 type = type->ctype.base_type;
388 if (type->type == SYM_ENUM)
389 type = type->ctype.base_type;
390 return type->type == SYM_BITFIELD ||
391 type->ctype.base_type == &int_type;
394 static inline int is_enum_type(const struct symbol *type)
396 if (type->type == SYM_NODE)
397 type = type->ctype.base_type;
398 return (type->type == SYM_ENUM);
401 static inline int is_signed_type(struct symbol *sym)
403 if (sym->type == SYM_NODE)
404 sym = sym->ctype.base_type;
405 if (sym->type == SYM_PTR)
406 return 0;
407 return !(sym->ctype.modifiers & MOD_UNSIGNED);
410 static inline int is_type_type(struct symbol *type)
412 return type == &type_ctype;
415 static inline int is_ptr_type(struct symbol *type)
417 if (!type)
418 return 0;
419 if (type->type == SYM_NODE)
420 type = type->ctype.base_type;
421 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
424 static inline int is_func_type(struct symbol *type)
426 if (type->type == SYM_NODE)
427 type = type->ctype.base_type;
428 return type->type == SYM_FN;
431 static inline int is_array_type(struct symbol *type)
433 if (type->type == SYM_NODE)
434 type = type->ctype.base_type;
435 return type->type == SYM_ARRAY;
438 static inline int is_struct_type(struct symbol *type)
440 if (type->type == SYM_NODE)
441 type = type->ctype.base_type;
442 return type->type == SYM_STRUCT;
445 static inline int is_union_type(struct symbol *type)
447 if (type->type == SYM_NODE)
448 type = type->ctype.base_type;
449 return type->type == SYM_UNION;
452 static inline int is_float_type(struct symbol *type)
454 if (type->type == SYM_NODE)
455 type = type->ctype.base_type;
456 return type->ctype.base_type == &fp_type;
459 static inline int is_byte_type(struct symbol *type)
461 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
464 static inline int is_wchar_type(struct symbol *type)
466 if (type->type == SYM_NODE)
467 type = type->ctype.base_type;
468 return type == wchar_ctype;
471 static inline int is_void_type(struct symbol *type)
473 if (type->type == SYM_NODE)
474 type = type->ctype.base_type;
475 return type == &void_ctype;
478 static inline int is_bool_type(struct symbol *type)
480 if (type->type == SYM_NODE)
481 type = type->ctype.base_type;
482 return type == &bool_ctype;
485 static inline int is_scalar_type(struct symbol *type)
487 if (type->type == SYM_NODE)
488 type = type->ctype.base_type;
489 switch (type->type) {
490 case SYM_ENUM:
491 case SYM_BITFIELD:
492 case SYM_PTR:
493 case SYM_RESTRICT: // OK, always integer types
494 case SYM_FOULED: // idem
495 return 1;
496 default:
497 break;
499 if (type->ctype.base_type == &int_type)
500 return 1;
501 if (type->ctype.base_type == &fp_type)
502 return 1;
503 return 0;
506 /// return true for integer & pointer types
507 static inline bool is_integral_type(struct symbol *type)
509 if (type->type == SYM_NODE)
510 type = type->ctype.base_type;
511 switch (type->type) {
512 case SYM_ENUM:
513 case SYM_PTR:
514 case SYM_RESTRICT: // OK, always integer types
515 case SYM_FOULED: // idem
516 return 1;
517 default:
518 break;
520 if (type->ctype.base_type == &int_type)
521 return 1;
522 return 0;
525 static inline int is_function(struct symbol *type)
527 return type && type->type == SYM_FN;
530 static inline int is_extern_inline(struct symbol *sym)
532 return (sym->ctype.modifiers & MOD_EXTERN) &&
533 (sym->ctype.modifiers & MOD_INLINE) &&
534 is_function(sym->ctype.base_type);
537 static inline int has_flexible_array(struct symbol *type)
539 if (type->type == SYM_NODE)
540 type = type->ctype.base_type;
541 return type->has_flex_array;
544 static inline int get_sym_type(struct symbol *type)
546 if (type->type == SYM_NODE)
547 type = type->ctype.base_type;
548 if (type->type == SYM_ENUM)
549 type = type->ctype.base_type;
550 return type->type;
553 static inline long long extend_value(long long val, struct symbol *ctype)
555 int is_signed = !(ctype->ctype.modifiers & MOD_UNSIGNED);
556 unsigned size = ctype->bit_size;
558 return bits_extend(val, size, is_signed);
561 static inline struct symbol *lookup_keyword(struct ident *ident, enum namespace ns)
563 if (!ident->keyword)
564 return NULL;
565 return lookup_symbol(ident, ns);
568 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
569 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
570 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
572 void create_fouled(struct symbol *type);
573 struct symbol *befoul(struct symbol *type);
576 extern struct ident bad_address_space;
578 static inline bool valid_as(struct ident *as)
580 return as && as != &bad_address_space;
583 static inline void combine_address_space(struct position pos,
584 struct ident **tas, struct ident *sas)
586 struct ident *as;
587 if (!sas)
588 return;
589 as = *tas;
590 if (!as)
591 *tas = sas;
592 else if (as != sas) {
593 *tas = &bad_address_space;
594 sparse_error(pos, "multiple address spaces given");
598 #endif /* SYMBOL_H */