-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsymtable.h
executable file
·113 lines (94 loc) · 2.51 KB
/
symtable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* symtable.h by William Ho */
#ifndef SYMTABLE_H
#define SYMTABLE_H
#define TABLE_LENGTH 128
enum scope_types {
S_FILE, S_BLOCK, S_FUNC, S_PROTO,
/* pseudo-scope: */ S_STRUCT
};
enum namespaces {
NS_OTHER, NS_STRUCT_TAG, NS_LABEL, NS_STRUCT_MEM
};
enum node_types {
N_VOID, N_CHAR, N_SHORT, N_INT, N_LONG, N_LONGLONG,
N_UCHAR, N_USHORT, N_UINT, N_ULONG, N_ULONGLONG,
N_FLOAT, N_DOUBLE, N_LONGDOUBLE,
N_BOOL, N_CFLOAT, N_CDOUBLE, N_CLONGDOUBLE,
N_STRUCT, N_UNION, N_ENUM, N_TYPEDEF,
N_PTR, N_ARR, N_FUNC, N_TYPENAME, N_VAR, N_STRUCT_MEM,
N_CONST, N_TMP,
};
// symtable_map maps file names to file-scope symbol tables
struct symtable_map {
struct symtable *st[TABLE_LENGTH];
};
struct symtable {
int scope_type;
char *file;
int line;
struct symbol *s[TABLE_LENGTH];
struct symtable *prev; // symbol table one level up
struct symtable *chain; // if file scope, next symbol table with same hashed file name
};
#define COMMON_NODE_ATTRIBUTES \
int nodetype
#define COMMON_SYMBOL_ATTRIBUTES \
COMMON_NODE_ATTRIBUTES; \
struct generic_node *type; \
char *id; \
char *file; \
int line; \
struct symtable *scope; \
struct symbol *chain; \
char namespace
struct generic_node {
COMMON_NODE_ATTRIBUTES;
};
struct symbol {
COMMON_SYMBOL_ATTRIBUTES;
char storage;
};
struct struct_member {
COMMON_SYMBOL_ATTRIBUTES;
struct struct_tag *parent;
int offset;
int bit_offset;
int size;
};
struct struct_tag {
COMMON_SYMBOL_ATTRIBUTES;
struct symtable *members; // mini-symtable to keep track of struct members
char complete;
};
struct func_node {
COMMON_SYMBOL_ATTRIBUTES;
struct generic_node *ret; // return type
struct symtable *args; // mini-symtable to keep track of function arguments
};
struct typedef_node {
COMMON_NODE_ATTRIBUTES;
struct generic_node *type;
};
struct ptr_node {
COMMON_NODE_ATTRIBUTES;
struct generic_node *to;
};
struct arr_node {
COMMON_NODE_ATTRIBUTES;
struct generic_node *base;
int size;
};
struct symtable *new_file(char *fname);
struct symtable *new_symtable(int stype);
int remove_symtable();
unsigned long hash(unsigned char *str);
struct generic_node *new_node(int ntype);
struct symbol *new_sym(char *sname);
struct symbol *add_sym(struct symbol *sym, struct symtable *table);
void free_sym(struct symbol *sym);
struct symbol *get_sym(char *sname, char nspace, struct symtable *table);
struct generic_node *new_arr_node(int size);
struct generic_node *new_ptr_node();
struct generic_node *new_func_node();
struct struct_tag *new_struct(char *struct_name, char complete);
#endif