[PATCH] Add -Wno-cast-truncate
[smatch.git] / symbol.h
bloba773a0a8103a5013900a599f123978658370bb13
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"
15 * An identifier with semantic meaning is a "symbol".
17 * There's a 1:n relationship: each symbol is always
18 * associated with one identifier, while each identifier
19 * can have one or more semantic meanings due to C scope
20 * rules.
22 * The progression is symbol -> token -> identifier. The
23 * token contains the information on where the symbol was
24 * declared.
26 enum namespace {
27 NS_NONE = 0,
28 NS_MACRO = 1,
29 NS_TYPEDEF = 2,
30 NS_STRUCT = 4, // Also used for unions and enums.
31 NS_LABEL = 8,
32 NS_SYMBOL = 16,
33 NS_ITERATOR = 32,
34 NS_PREPROCESSOR = 64,
35 NS_INVISIBLEMACRO = 128,
38 enum type {
39 SYM_UNINITIALIZED,
40 SYM_PREPROCESSOR,
41 SYM_BASETYPE,
42 SYM_NODE,
43 SYM_PTR,
44 SYM_FN,
45 SYM_ARRAY,
46 SYM_STRUCT,
47 SYM_UNION,
48 SYM_ENUM,
49 SYM_TYPEDEF,
50 SYM_TYPEOF,
51 SYM_MEMBER,
52 SYM_BITFIELD,
53 SYM_LABEL,
54 SYM_RESTRICT,
55 SYM_BAD,
58 struct ctype {
59 unsigned long modifiers;
60 unsigned long alignment;
61 unsigned int in_context, out_context, as;
62 struct symbol *base_type;
65 struct symbol_op {
66 int (*evaluate)(struct expression *);
67 int (*expand)(struct expression *, int);
68 };
70 extern int expand_safe_p(struct expression *expr, int cost);
71 extern int expand_constant_p(struct expression *expr, int cost);
73 struct symbol {
74 enum namespace namespace:8;
75 enum type type:8;
76 unsigned char used:1, weak:1;
77 struct position pos; /* Where this symbol was declared */
78 struct ident *ident; /* What identifier this symbol is associated with */
79 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
80 struct symbol **id_list; /* Backpointer to symbol list head */
81 struct symbol *replace; /* What is this symbol shadowed by in copy-expression */
82 struct scope *scope;
83 struct symbol *same_symbol;
84 struct symbol_op *op;
86 union {
87 struct /* NS_MACRO */ {
88 struct token *expansion;
89 struct token *arglist;
91 struct /* NS_PREPROCESSOR */ {
92 int (*handler)(struct stream *, struct token **, struct token *);
93 int normal;
95 struct /* NS_SYMBOL */ {
96 unsigned long offset;
97 int bit_size;
98 unsigned int bit_offset:8,
99 arg_count:10,
100 variadic:1,
101 initialized:1,
102 examined:1,
103 expanding:1,
104 evaluated:1;
105 struct expression *array_size;
106 struct ctype ctype;
107 struct symbol_list *arguments;
108 struct statement *stmt;
109 struct symbol_list *symbol_list;
110 struct statement *inline_stmt;
111 struct symbol_list *inline_symbol_list;
112 struct expression *initializer;
113 long long value; /* Initial value */
116 union /* backend */ {
117 struct basic_block *bb_target; /* label */
118 void *aux; /* Auxiliary info, eg. backend information */
120 pseudo_t pseudo;
123 /* Modifiers */
124 #define MOD_AUTO 0x0001
125 #define MOD_REGISTER 0x0002
126 #define MOD_STATIC 0x0004
127 #define MOD_EXTERN 0x0008
129 #define MOD_CONST 0x0010
130 #define MOD_VOLATILE 0x0020
131 #define MOD_SIGNED 0x0040
132 #define MOD_UNSIGNED 0x0080
134 #define MOD_CHAR 0x0100
135 #define MOD_SHORT 0x0200
136 #define MOD_LONG 0x0400
137 #define MOD_LONGLONG 0x0800
139 #define MOD_TYPEDEF 0x1000
140 #define MOD_STRUCTOF 0x2000
141 #define MOD_UNIONOF 0x4000
142 #define MOD_ENUMOF 0x8000
144 #define MOD_TYPEOF 0x10000
145 #define MOD_ATTRIBUTE 0x20000
146 #define MOD_INLINE 0x40000
147 #define MOD_ADDRESSABLE 0x80000
149 #define MOD_NOCAST 0x100000
150 #define MOD_NODEREF 0x200000
151 #define MOD_ACCESSED 0x400000
152 #define MOD_TOPLEVEL 0x800000 // scoping..
154 #define MOD_LABEL 0x1000000
155 #define MOD_ASSIGNED 0x2000000
156 #define MOD_TYPE 0x4000000
157 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
159 #define MOD_USERTYPE 0x10000000
160 #define MOD_FORCE 0x20000000
161 #define MOD_EXPLICITLY_SIGNED 0x40000000
162 #define MOD_BITWISE 0x80000000
164 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
165 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
166 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
167 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
168 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNEDNESS)
169 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
170 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
171 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
174 /* Current parsing/evaluation function */
175 extern struct symbol *current_fn;
177 /* Abstract types */
178 extern struct symbol int_type,
179 fp_type;
181 /* C types */
182 extern struct symbol bool_ctype, void_ctype, type_ctype,
183 char_ctype, schar_ctype, uchar_ctype,
184 short_ctype, sshort_ctype, ushort_ctype,
185 int_ctype, sint_ctype, uint_ctype,
186 long_ctype, slong_ctype, ulong_ctype,
187 llong_ctype, sllong_ctype, ullong_ctype,
188 float_ctype, double_ctype, ldouble_ctype,
189 string_ctype, ptr_ctype, lazy_ptr_ctype,
190 incomplete_ctype, label_ctype, bad_ctype;
192 /* Special internal symbols */
193 extern struct symbol zero_int;
195 #define __IDENT(n,str,res) \
196 extern struct ident n
197 #include "ident-list.h"
199 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
201 extern struct symbol_list *translation_unit_used_list;
203 extern void access_symbol(struct symbol *);
205 extern const char * type_difference(struct symbol *target, struct symbol *source,
206 unsigned long target_mod_ignore, unsigned long source_mod_ignore);
208 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
209 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
210 extern void init_symbols(void);
211 extern void init_ctype(void);
212 extern struct symbol *alloc_symbol(struct position, int type);
213 extern void show_type(struct symbol *);
214 extern const char *modifier_string(unsigned long mod);
215 extern void show_symbol(struct symbol *);
216 extern int show_symbol_expr_init(struct symbol *sym);
217 extern void show_type_list(struct symbol *);
218 extern void show_symbol_list(struct symbol_list *, const char *);
219 extern void add_symbol(struct symbol_list **, struct symbol *);
220 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
222 extern struct symbol *examine_symbol_type(struct symbol *);
223 extern void examine_simple_symbol_type(struct symbol *);
224 extern const char *show_typename(struct symbol *sym);
226 extern void debug_symbol(struct symbol *);
227 extern void merge_type(struct symbol *sym, struct symbol *base_type);
228 extern void check_declaration(struct symbol *sym);
230 static inline struct symbol *get_base_type(const struct symbol *sym)
232 return examine_symbol_type(sym->ctype.base_type);
235 static inline int is_int_type(const struct symbol *type)
237 if (type->type == SYM_NODE)
238 type = type->ctype.base_type;
239 if (type->type == SYM_ENUM)
240 type = type->ctype.base_type;
241 return type->type == SYM_BITFIELD ||
242 type->ctype.base_type == &int_type;
245 static inline int is_enum_type(const struct symbol *type)
247 if (type->type == SYM_NODE)
248 type = type->ctype.base_type;
249 return (type->type == SYM_ENUM);
252 static inline int get_sym_type(struct symbol *type)
254 if (type->type == SYM_NODE)
255 type = type->ctype.base_type;
256 if (type->type == SYM_ENUM)
257 type = type->ctype.base_type;
258 return type->type;
261 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
262 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
263 extern int is_ptr_type(struct symbol *);
265 #endif /* SEMANTIC_H */