Add symbol scoping for proper parsing.
[smatch.git] / symbol.h
blobc2cc4606c356a747d6e7b046ab7061b2fa4b3a7e
1 #ifndef SEMANTIC_H
2 #define SEMANTIC_H
4 #include "token.h"
6 /*
7 * An identifier with semantic meaning is a "symbol".
9 * There's a 1:n relationship: each symbol is always
10 * associated with one identifier, while each identifier
11 * can have one or more semantic meanings due to C scope
12 * rules.
14 * The progression is symbol -> token -> identifier. The
15 * token contains the information on where the symbol was
16 * declared.
18 enum namespace {
19 NS_NONE,
20 NS_PREPROCESSOR,
21 NS_TYPEDEF,
22 NS_STRUCT,
23 NS_ENUM,
24 NS_LABEL,
27 enum type {
28 SYM_NONE,
29 SYM_TYPE,
30 SYM_PTR,
31 SYM_FN,
32 SYM_ARRAY,
33 SYM_STRUCT,
34 SYM_UNION,
35 SYM_ENUM,
36 SYM_TYPEDEF,
37 SYM_MEMBER,
38 SYM_BITFIELD,
41 struct symbol {
42 enum namespace namespace:8;
43 enum type type:8;
44 struct token *token; /* Where this symbol was declared */
45 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
46 struct symbol **id_list; /* Backpointer to symbol list head */
47 union {
48 struct preprocessor_sym {
49 int busy;
50 struct token *expansion;
51 struct token *arglist;
53 struct ctype_symbol {
54 struct symbol *next; /* Next symbol at this level */
55 unsigned long size;
56 unsigned long modifiers;
57 struct symbol *base_type;
58 struct symbol *children;
59 struct statement *stmt;
60 struct symbol_list *symbol_list;
65 /* Modifiers */
66 #define SYM_AUTO 0x0001
67 #define SYM_REGISTER 0x0002
68 #define SYM_STATIC 0x0004
69 #define SYM_EXTERN 0x0008
71 #define SYM_CONST 0x0010
72 #define SYM_VOLATILE 0x0020
73 #define SYM_SIGNED 0x0040
74 #define SYM_UNSIGNED 0x0080
76 #define SYM_CHAR 0x0100
77 #define SYM_SHORT 0x0200
78 #define SYM_LONG 0x0400
79 #define SYM_LONGLONG 0x0800
81 #define SYM_TYPEDEF 0x1000
82 #define SYM_STRUCTOF 0x2000
83 #define SYM_UNIONOF 0x4000
84 #define SYM_ENUMOF 0x8000
86 #define SYM_TYPEOF 0x10000
87 #define SYM_ATTRIBUTE 0x20000
89 /* Basic types */
90 extern struct symbol void_type,
91 int_type,
92 fp_type,
93 vector_type,
94 bad_type;
96 /* Basic identifiers */
97 extern struct ident sizeof_ident,
98 alignof_ident,
99 __alignof_ident,
100 __alignof___ident,
101 if_ident,
102 else_ident,
103 switch_ident,
104 case_ident,
105 default_ident,
106 break_ident,
107 continue_ident,
108 for_ident,
109 while_ident,
110 do_ident,
111 goto_ident,
112 return_ident;
114 extern struct ident __asm___ident,
115 __asm_ident,
116 asm_ident,
117 __volatile___ident,
118 __volatile_ident,
119 volatile_ident,
120 __attribute___ident,
121 __attribute_ident;
123 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
125 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
126 extern void init_symbols(void);
127 extern struct symbol *alloc_symbol(struct token *, int type);
128 extern void show_type(struct symbol *);
129 extern const char *modifier_string(unsigned long mod);
130 extern void show_symbol(struct symbol *);
131 extern void show_type_list(struct symbol *);
132 extern void show_symbol_list(struct symbol_list *);
133 extern void add_symbol(struct symbol_list **, struct symbol *);
134 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
136 #endif /* SEMANTIC_H */