Fix typo in symbol.h: s/keywrods/keywords/
[smatch.git] / symbol.h
blobbb51961af46e6bc296015292c9a7440362f3b15d
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_UNDEF = 128,
36 NS_KEYWORD = 256,
39 enum type {
40 SYM_UNINITIALIZED,
41 SYM_PREPROCESSOR,
42 SYM_BASETYPE,
43 SYM_NODE,
44 SYM_PTR,
45 SYM_FN,
46 SYM_ARRAY,
47 SYM_STRUCT,
48 SYM_UNION,
49 SYM_ENUM,
50 SYM_TYPEDEF,
51 SYM_TYPEOF,
52 SYM_MEMBER,
53 SYM_BITFIELD,
54 SYM_LABEL,
55 SYM_RESTRICT,
56 SYM_FOULED,
57 SYM_KEYWORD,
58 SYM_BAD,
61 enum keyword {
62 KW_SPECIFIER = 1 << 0,
63 KW_MODIFIER = 1 << 1,
64 KW_QUALIFIER = 1 << 2,
65 KW_ATTRIBUTE = 1 << 3,
66 KW_TYPEOF = 1 << 4,
67 KW_STATEMENT = 1 << 5,
70 struct context {
71 struct expression *context;
72 unsigned int in, out;
75 extern struct context *alloc_context(void);
77 DECLARE_PTR_LIST(context_list, struct context);
79 struct ctype {
80 unsigned long modifiers;
81 unsigned long alignment;
82 struct context_list *contexts;
83 unsigned int as;
84 struct symbol *base_type;
87 struct symbol_op {
88 enum keyword type;
89 int (*evaluate)(struct expression *);
90 int (*expand)(struct expression *, int);
91 int (*args)(struct expression *);
93 /* keywords */
94 struct token *(*declarator)(struct token *token, struct ctype *ctype);
95 struct token *(*statement)(struct token *token, struct statement *stmt);
96 struct token *(*toplevel)(struct token *token, struct symbol_list **list);
99 extern int expand_safe_p(struct expression *expr, int cost);
100 extern int expand_constant_p(struct expression *expr, int cost);
102 #define SYM_ATTR_WEAK 0
103 #define SYM_ATTR_NORMAL 1
104 #define SYM_ATTR_STRONG 2
106 struct symbol {
107 enum type type:8;
108 enum namespace namespace:9;
109 unsigned char used:1, attr:2;
110 struct position pos; /* Where this symbol was declared */
111 struct ident *ident; /* What identifier this symbol is associated with */
112 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
113 struct symbol **id_list; /* Backpointer to symbol list head */
114 struct symbol *replace; /* What is this symbol shadowed by in copy-expression */
115 struct scope *scope;
116 struct symbol *same_symbol;
117 struct symbol_op *op;
119 union {
120 struct /* NS_MACRO */ {
121 struct token *expansion;
122 struct token *arglist;
123 struct scope *used_in;
125 struct /* NS_PREPROCESSOR */ {
126 int (*handler)(struct stream *, struct token **, struct token *);
127 int normal;
129 struct /* NS_SYMBOL */ {
130 unsigned long offset;
131 int bit_size;
132 unsigned int bit_offset:8,
133 arg_count:10,
134 variadic:1,
135 initialized:1,
136 examined:1,
137 expanding:1,
138 evaluated:1,
139 string:1;
140 struct expression *array_size;
141 struct ctype ctype;
142 struct symbol_list *arguments;
143 struct statement *stmt;
144 struct symbol_list *symbol_list;
145 struct statement *inline_stmt;
146 struct symbol_list *inline_symbol_list;
147 struct expression *initializer;
148 long long value; /* Initial value */
151 union /* backend */ {
152 struct basic_block *bb_target; /* label */
153 void *aux; /* Auxiliary info, eg. backend information */
154 struct { /* sparse ctags */
155 char kind;
156 unsigned char visited:1;
159 pseudo_t pseudo;
162 /* Modifiers */
163 #define MOD_AUTO 0x0001
164 #define MOD_REGISTER 0x0002
165 #define MOD_STATIC 0x0004
166 #define MOD_EXTERN 0x0008
168 #define MOD_CONST 0x0010
169 #define MOD_VOLATILE 0x0020
170 #define MOD_SIGNED 0x0040
171 #define MOD_UNSIGNED 0x0080
173 #define MOD_CHAR 0x0100
174 #define MOD_SHORT 0x0200
175 #define MOD_LONG 0x0400
176 #define MOD_LONGLONG 0x0800
178 #define MOD_TYPEDEF 0x1000
180 #define MOD_INLINE 0x40000
181 #define MOD_ADDRESSABLE 0x80000
183 #define MOD_NOCAST 0x100000
184 #define MOD_NODEREF 0x200000
185 #define MOD_ACCESSED 0x400000
186 #define MOD_TOPLEVEL 0x800000 // scoping..
188 #define MOD_LABEL 0x1000000
189 #define MOD_ASSIGNED 0x2000000
190 #define MOD_TYPE 0x4000000
191 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
193 #define MOD_USERTYPE 0x10000000
194 #define MOD_FORCE 0x20000000
195 #define MOD_EXPLICITLY_SIGNED 0x40000000
196 #define MOD_BITWISE 0x80000000
198 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
199 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
200 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
201 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNEDNESS)
202 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
203 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
204 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
205 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
208 /* Current parsing/evaluation function */
209 extern struct symbol *current_fn;
211 /* Abstract types */
212 extern struct symbol int_type,
213 fp_type;
215 /* C types */
216 extern struct symbol bool_ctype, void_ctype, type_ctype,
217 char_ctype, schar_ctype, uchar_ctype,
218 short_ctype, sshort_ctype, ushort_ctype,
219 int_ctype, sint_ctype, uint_ctype,
220 long_ctype, slong_ctype, ulong_ctype,
221 llong_ctype, sllong_ctype, ullong_ctype,
222 float_ctype, double_ctype, ldouble_ctype,
223 string_ctype, ptr_ctype, lazy_ptr_ctype,
224 incomplete_ctype, label_ctype, bad_ctype;
226 /* Special internal symbols */
227 extern struct symbol zero_int;
229 #define __IDENT(n,str,res) \
230 extern struct ident n
231 #include "ident-list.h"
233 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
235 extern struct symbol_list *translation_unit_used_list;
237 extern void access_symbol(struct symbol *);
239 extern const char * type_difference(struct symbol *target, struct symbol *source,
240 unsigned long target_mod_ignore, unsigned long source_mod_ignore);
242 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
243 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
244 extern void init_symbols(void);
245 extern void init_ctype(void);
246 extern struct symbol *alloc_symbol(struct position, int type);
247 extern void show_type(struct symbol *);
248 extern const char *modifier_string(unsigned long mod);
249 extern void show_symbol(struct symbol *);
250 extern int show_symbol_expr_init(struct symbol *sym);
251 extern void show_type_list(struct symbol *);
252 extern void show_symbol_list(struct symbol_list *, const char *);
253 extern void add_symbol(struct symbol_list **, struct symbol *);
254 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
256 extern struct symbol *examine_symbol_type(struct symbol *);
257 extern void examine_simple_symbol_type(struct symbol *);
258 extern const char *show_typename(struct symbol *sym);
259 extern const char *builtin_typename(struct symbol *sym);
260 extern const char *builtin_ctypename(struct ctype *ctype);
262 extern void debug_symbol(struct symbol *);
263 extern void merge_type(struct symbol *sym, struct symbol *base_type);
264 extern void check_declaration(struct symbol *sym);
266 static inline struct symbol *get_base_type(const struct symbol *sym)
268 return examine_symbol_type(sym->ctype.base_type);
271 static inline int is_int_type(const struct symbol *type)
273 if (type->type == SYM_NODE)
274 type = type->ctype.base_type;
275 if (type->type == SYM_ENUM)
276 type = type->ctype.base_type;
277 return type->type == SYM_BITFIELD ||
278 type->ctype.base_type == &int_type;
281 static inline int is_enum_type(const struct symbol *type)
283 if (type->type == SYM_NODE)
284 type = type->ctype.base_type;
285 return (type->type == SYM_ENUM);
288 static inline int get_sym_type(struct symbol *type)
290 if (type->type == SYM_NODE)
291 type = type->ctype.base_type;
292 if (type->type == SYM_ENUM)
293 type = type->ctype.base_type;
294 return type->type;
297 static inline struct symbol *lookup_keyword(struct ident *ident, enum namespace ns)
299 if (!ident->keyword)
300 return NULL;
301 return lookup_symbol(ident, ns);
304 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
305 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
306 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
307 extern int is_ptr_type(struct symbol *);
309 void create_fouled(struct symbol *type);
310 struct symbol *befoul(struct symbol *type);
312 #endif /* SYMBOL_H */