Turn on -Wcontext by default
[smatch.git] / symbol.h
blob9d939cf5ebe374fe24115e5d3e8d75e2203270c8
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 context {
59 struct expression *context;
60 unsigned int in, out;
63 extern struct context *alloc_context(void);
65 DECLARE_PTR_LIST(context_list, struct context);
67 struct ctype {
68 unsigned long modifiers;
69 unsigned long alignment;
70 struct context_list *contexts;
71 unsigned int as;
72 struct symbol *base_type;
75 struct symbol_op {
76 int (*evaluate)(struct expression *);
77 int (*expand)(struct expression *, int);
78 };
80 extern int expand_safe_p(struct expression *expr, int cost);
81 extern int expand_constant_p(struct expression *expr, int cost);
83 struct symbol {
84 enum namespace namespace:8;
85 enum type type:8;
86 unsigned char used:1, weak:1;
87 struct position pos; /* Where this symbol was declared */
88 struct ident *ident; /* What identifier this symbol is associated with */
89 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
90 struct symbol **id_list; /* Backpointer to symbol list head */
91 struct symbol *replace; /* What is this symbol shadowed by in copy-expression */
92 struct scope *scope;
93 struct symbol *same_symbol;
94 struct symbol_op *op;
96 union {
97 struct /* NS_MACRO */ {
98 struct token *expansion;
99 struct token *arglist;
101 struct /* NS_PREPROCESSOR */ {
102 int (*handler)(struct stream *, struct token **, struct token *);
103 int normal;
105 struct /* NS_SYMBOL */ {
106 unsigned long offset;
107 int bit_size;
108 unsigned int bit_offset:8,
109 arg_count:10,
110 variadic:1,
111 initialized:1,
112 examined:1,
113 expanding:1,
114 evaluated:1;
115 struct expression *array_size;
116 struct ctype ctype;
117 struct symbol_list *arguments;
118 struct statement *stmt;
119 struct symbol_list *symbol_list;
120 struct statement *inline_stmt;
121 struct symbol_list *inline_symbol_list;
122 struct expression *initializer;
123 long long value; /* Initial value */
126 union /* backend */ {
127 struct basic_block *bb_target; /* label */
128 void *aux; /* Auxiliary info, eg. backend information */
130 pseudo_t pseudo;
133 /* Modifiers */
134 #define MOD_AUTO 0x0001
135 #define MOD_REGISTER 0x0002
136 #define MOD_STATIC 0x0004
137 #define MOD_EXTERN 0x0008
139 #define MOD_CONST 0x0010
140 #define MOD_VOLATILE 0x0020
141 #define MOD_SIGNED 0x0040
142 #define MOD_UNSIGNED 0x0080
144 #define MOD_CHAR 0x0100
145 #define MOD_SHORT 0x0200
146 #define MOD_LONG 0x0400
147 #define MOD_LONGLONG 0x0800
149 #define MOD_TYPEDEF 0x1000
150 #define MOD_STRUCTOF 0x2000
151 #define MOD_UNIONOF 0x4000
152 #define MOD_ENUMOF 0x8000
154 #define MOD_TYPEOF 0x10000
155 #define MOD_ATTRIBUTE 0x20000
156 #define MOD_INLINE 0x40000
157 #define MOD_ADDRESSABLE 0x80000
159 #define MOD_NOCAST 0x100000
160 #define MOD_NODEREF 0x200000
161 #define MOD_ACCESSED 0x400000
162 #define MOD_TOPLEVEL 0x800000 // scoping..
164 #define MOD_LABEL 0x1000000
165 #define MOD_ASSIGNED 0x2000000
166 #define MOD_TYPE 0x4000000
167 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
169 #define MOD_USERTYPE 0x10000000
170 #define MOD_FORCE 0x20000000
171 #define MOD_EXPLICITLY_SIGNED 0x40000000
172 #define MOD_BITWISE 0x80000000
174 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
175 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
176 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
177 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
178 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNEDNESS)
179 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
180 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
181 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
184 /* Current parsing/evaluation function */
185 extern struct symbol *current_fn;
187 /* Abstract types */
188 extern struct symbol int_type,
189 fp_type;
191 /* C types */
192 extern struct symbol bool_ctype, void_ctype, type_ctype,
193 char_ctype, schar_ctype, uchar_ctype,
194 short_ctype, sshort_ctype, ushort_ctype,
195 int_ctype, sint_ctype, uint_ctype,
196 long_ctype, slong_ctype, ulong_ctype,
197 llong_ctype, sllong_ctype, ullong_ctype,
198 float_ctype, double_ctype, ldouble_ctype,
199 string_ctype, ptr_ctype, lazy_ptr_ctype,
200 incomplete_ctype, label_ctype, bad_ctype;
202 /* Special internal symbols */
203 extern struct symbol zero_int;
205 #define __IDENT(n,str,res) \
206 extern struct ident n
207 #include "ident-list.h"
209 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
211 extern struct symbol_list *translation_unit_used_list;
213 extern void access_symbol(struct symbol *);
215 extern const char * type_difference(struct symbol *target, struct symbol *source,
216 unsigned long target_mod_ignore, unsigned long source_mod_ignore);
218 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
219 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
220 extern void init_symbols(void);
221 extern void init_ctype(void);
222 extern struct symbol *alloc_symbol(struct position, int type);
223 extern void show_type(struct symbol *);
224 extern const char *modifier_string(unsigned long mod);
225 extern void show_symbol(struct symbol *);
226 extern int show_symbol_expr_init(struct symbol *sym);
227 extern void show_type_list(struct symbol *);
228 extern void show_symbol_list(struct symbol_list *, const char *);
229 extern void add_symbol(struct symbol_list **, struct symbol *);
230 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
232 extern struct symbol *examine_symbol_type(struct symbol *);
233 extern void examine_simple_symbol_type(struct symbol *);
234 extern const char *show_typename(struct symbol *sym);
236 extern void debug_symbol(struct symbol *);
237 extern void merge_type(struct symbol *sym, struct symbol *base_type);
238 extern void check_declaration(struct symbol *sym);
240 static inline struct symbol *get_base_type(const struct symbol *sym)
242 return examine_symbol_type(sym->ctype.base_type);
245 static inline int is_int_type(const struct symbol *type)
247 if (type->type == SYM_NODE)
248 type = type->ctype.base_type;
249 if (type->type == SYM_ENUM)
250 type = type->ctype.base_type;
251 return type->type == SYM_BITFIELD ||
252 type->ctype.base_type == &int_type;
255 static inline int is_enum_type(const struct symbol *type)
257 if (type->type == SYM_NODE)
258 type = type->ctype.base_type;
259 return (type->type == SYM_ENUM);
262 static inline int get_sym_type(struct symbol *type)
264 if (type->type == SYM_NODE)
265 type = type->ctype.base_type;
266 if (type->type == SYM_ENUM)
267 type = type->ctype.base_type;
268 return type->type;
271 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
272 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
273 extern int is_ptr_type(struct symbol *);
275 #endif /* SEMANTIC_H */