[PATCH] Fix address space ordering problem
[smatch.git] / symbol.h
blob21a3e4cf8a79e3f977492d5a2a825d6afdcd8312
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 *);
94 struct /* NS_SYMBOL */ {
95 unsigned long offset;
96 int bit_size;
97 unsigned int bit_offset:8,
98 arg_count:10,
99 variadic:1,
100 initialized:1,
101 examined:1,
102 expanding:1;
103 struct expression *array_size;
104 struct ctype ctype;
105 struct symbol_list *arguments;
106 struct statement *stmt;
107 struct symbol_list *symbol_list;
108 struct statement *inline_stmt;
109 struct symbol_list *inline_symbol_list;
110 struct expression *initializer;
111 long long value; /* Initial value */
114 union /* backend */ {
115 struct basic_block *bb_target; /* label */
116 void *aux; /* Auxiliary info, eg. backend information */
118 pseudo_t pseudo;
121 /* Modifiers */
122 #define MOD_AUTO 0x0001
123 #define MOD_REGISTER 0x0002
124 #define MOD_STATIC 0x0004
125 #define MOD_EXTERN 0x0008
127 #define MOD_CONST 0x0010
128 #define MOD_VOLATILE 0x0020
129 #define MOD_SIGNED 0x0040
130 #define MOD_UNSIGNED 0x0080
132 #define MOD_CHAR 0x0100
133 #define MOD_SHORT 0x0200
134 #define MOD_LONG 0x0400
135 #define MOD_LONGLONG 0x0800
137 #define MOD_TYPEDEF 0x1000
138 #define MOD_STRUCTOF 0x2000
139 #define MOD_UNIONOF 0x4000
140 #define MOD_ENUMOF 0x8000
142 #define MOD_TYPEOF 0x10000
143 #define MOD_ATTRIBUTE 0x20000
144 #define MOD_INLINE 0x40000
145 #define MOD_ADDRESSABLE 0x80000
147 #define MOD_NOCAST 0x100000
148 #define MOD_NODEREF 0x200000
149 #define MOD_ACCESSED 0x400000
150 #define MOD_TOPLEVEL 0x800000 // scoping..
152 #define MOD_LABEL 0x1000000
153 #define MOD_ASSIGNED 0x2000000
154 #define MOD_TYPE 0x4000000
155 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
157 #define MOD_USERTYPE 0x10000000
158 #define MOD_FORCE 0x20000000
159 #define MOD_EXPLICITLY_SIGNED 0x40000000
160 #define MOD_BITWISE 0x80000000
162 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
163 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
164 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
165 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
166 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNEDNESS)
167 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
168 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
169 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
172 /* Current parsing/evaluation function */
173 extern struct symbol *current_fn;
175 /* Abstract types */
176 extern struct symbol int_type,
177 fp_type;
179 /* C types */
180 extern struct symbol bool_ctype, void_ctype, type_ctype,
181 char_ctype, schar_ctype, uchar_ctype,
182 short_ctype, sshort_ctype, ushort_ctype,
183 int_ctype, sint_ctype, uint_ctype,
184 long_ctype, slong_ctype, ulong_ctype,
185 llong_ctype, sllong_ctype, ullong_ctype,
186 float_ctype, double_ctype, ldouble_ctype,
187 string_ctype, ptr_ctype, lazy_ptr_ctype,
188 incomplete_ctype, label_ctype, bad_ctype;
191 #define __IDENT(n,str,res) \
192 extern struct ident n
193 #include "ident-list.h"
195 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
197 extern struct symbol_list *translation_unit_used_list;
199 extern void access_symbol(struct symbol *);
201 extern const char * type_difference(struct symbol *target, struct symbol *source,
202 unsigned long target_mod_ignore, unsigned long source_mod_ignore);
204 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
205 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
206 extern void init_symbols(void);
207 extern void init_ctype(void);
208 extern struct symbol *alloc_symbol(struct position, int type);
209 extern void show_type(struct symbol *);
210 extern const char *modifier_string(unsigned long mod);
211 extern void show_symbol(struct symbol *);
212 extern int show_symbol_expr_init(struct symbol *sym);
213 extern void show_type_list(struct symbol *);
214 extern void show_symbol_list(struct symbol_list *, const char *);
215 extern void add_symbol(struct symbol_list **, struct symbol *);
216 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
218 extern struct symbol *examine_symbol_type(struct symbol *);
219 extern void examine_simple_symbol_type(struct symbol *);
220 extern const char *show_typename(struct symbol *sym);
222 extern void debug_symbol(struct symbol *);
223 extern void merge_type(struct symbol *sym, struct symbol *base_type);
224 extern void check_declaration(struct symbol *sym);
226 static inline struct symbol *get_base_type(const struct symbol *sym)
228 return examine_symbol_type(sym->ctype.base_type);
231 static inline int is_int_type(const struct symbol *type)
233 if (type->type == SYM_NODE)
234 type = type->ctype.base_type;
235 if (type->type == SYM_ENUM)
236 type = type->ctype.base_type;
237 return type->type == SYM_BITFIELD ||
238 type->ctype.base_type == &int_type;
241 static inline int get_sym_type(struct symbol *type)
243 if (type->type == SYM_NODE)
244 type = type->ctype.base_type;
245 if (type->type == SYM_ENUM)
246 type = type->ctype.base_type;
247 return type->type;
250 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
251 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
252 extern int is_ptr_type(struct symbol *);
254 #endif /* SEMANTIC_H */