-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast-inspect.c
222 lines (196 loc) · 6.76 KB
/
ast-inspect.c
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "token.h"
#include "parse.h"
#include "symbol.h"
#include "ast-inspect.h"
#include "expression.h"
static inline void inspect_ptr_list(AstNode *node, const char *name, void (*inspect)(AstNode *))
{
struct ptr_list *ptrlist = node->ptr;
void *ptr;
int i = 0;
node->text = g_strdup_printf("%s %s:", node->text, name);
FOR_EACH_PTR(ptrlist, ptr) {
char *index = g_strdup_printf("%d: ", i++);
ast_append_child(node, index, ptr, inspect);
} END_FOR_EACH_PTR(ptr);
}
static const char *statement_type_name(enum statement_type type)
{
static const char *statement_type_name[] = {
[STMT_NONE] = "STMT_NONE",
[STMT_DECLARATION] = "STMT_DECLARATION",
[STMT_EXPRESSION] = "STMT_EXPRESSION",
[STMT_COMPOUND] = "STMT_COMPOUND",
[STMT_IF] = "STMT_IF",
[STMT_RETURN] = "STMT_RETURN",
[STMT_CASE] = "STMT_CASE",
[STMT_SWITCH] = "STMT_SWITCH",
[STMT_ITERATOR] = "STMT_ITERATOR",
[STMT_LABEL] = "STMT_LABEL",
[STMT_GOTO] = "STMT_GOTO",
[STMT_ASM] = "STMT_ASM",
[STMT_CONTEXT] = "STMT_CONTEXT",
[STMT_RANGE] = "STMT_RANGE",
};
return statement_type_name[type] ?: "UNKNOWN_STATEMENT_TYPE";
}
void inspect_statement(AstNode *node)
{
struct statement *stmt = node->ptr;
node->text = g_strdup_printf("%s %s:", node->text, statement_type_name(stmt->type));
switch (stmt->type) {
case STMT_COMPOUND:
ast_append_child(node, "stmts:", stmt->stmts, inspect_statement_list);
break;
case STMT_EXPRESSION:
ast_append_child(node, "expression:", stmt->expression, inspect_expression);
break;
case STMT_IF:
ast_append_child(node, "conditional:", stmt->if_conditional, inspect_expression);
ast_append_child(node, "if_true:", stmt->if_true, inspect_statement);
ast_append_child(node, "if_false:", stmt->if_false, inspect_statement);
break;
case STMT_ITERATOR:
ast_append_child(node, "break:", stmt->iterator_break, inspect_symbol);
ast_append_child(node, "continue:", stmt->iterator_continue, inspect_symbol);
ast_append_child(node, "pre_statement:", stmt->iterator_pre_statement,
inspect_statement);
ast_append_child(node, "statement:", stmt->iterator_statement,
inspect_statement);
ast_append_child(node, "post_statement:", stmt->iterator_post_statement,
inspect_statement);
break;
case STMT_SWITCH:
ast_append_child(node, "switch_expression:", stmt->switch_expression, inspect_expression);
ast_append_child(node, "switch_statement:", stmt->switch_statement, inspect_statement);
ast_append_child(node, "switch_break:", stmt->switch_break, inspect_symbol);
ast_append_child(node, "switch_case:", stmt->switch_case, inspect_symbol);
break;
case STMT_CASE:
ast_append_child(node, "case_expression:", stmt->case_expression, inspect_expression);
ast_append_child(node, "case_to:", stmt->case_to, inspect_expression);
ast_append_child(node, "case_statement:", stmt->case_statement, inspect_statement);
ast_append_child(node, "case_label:", stmt->case_label, inspect_symbol);
break;
case STMT_RETURN:
ast_append_child(node, "ret_value:", stmt->ret_value, inspect_expression);
ast_append_child(node, "ret_target:", stmt->ret_target, inspect_symbol);
break;
default:
break;
}
}
void inspect_statement_list(AstNode *node)
{
inspect_ptr_list(node, "statement_list", inspect_statement);
}
static const char *symbol_type_name(enum type type)
{
static const char *type_name[] = {
[SYM_UNINITIALIZED] = "SYM_UNINITIALIZED",
[SYM_PREPROCESSOR] = "SYM_PREPROCESSOR",
[SYM_BASETYPE] = "SYM_BASETYPE",
[SYM_NODE] = "SYM_NODE",
[SYM_PTR] = "SYM_PTR",
[SYM_FN] = "SYM_FN",
[SYM_ARRAY] = "SYM_ARRAY",
[SYM_STRUCT] = "SYM_STRUCT",
[SYM_UNION] = "SYM_UNION",
[SYM_ENUM] = "SYM_ENUM",
[SYM_TYPEDEF] = "SYM_TYPEDEF",
[SYM_TYPEOF] = "SYM_TYPEOF",
[SYM_MEMBER] = "SYM_MEMBER",
[SYM_BITFIELD] = "SYM_BITFIELD",
[SYM_LABEL] = "SYM_LABEL",
[SYM_RESTRICT] = "SYM_RESTRICT",
[SYM_FOULED] = "SYM_FOULED",
[SYM_KEYWORD] = "SYM_KEYWORD",
[SYM_BAD] = "SYM_BAD",
};
return type_name[type] ?: "UNKNOWN_TYPE";
}
void inspect_symbol(AstNode *node)
{
struct symbol *sym = node->ptr;
node->text = g_strdup_printf("%s %s: %s", node->text, symbol_type_name(sym->type),
builtin_typename(sym) ?: show_ident(sym->ident));
ast_append_child(node, "ctype.base_type:", sym->ctype.base_type,inspect_symbol);
switch (sym->namespace) {
case NS_PREPROCESSOR:
break;
default:
ast_append_child(node, "arguments:", sym->arguments, inspect_symbol_list);
ast_append_child(node, "symbol_list:", sym->symbol_list, inspect_symbol_list);
ast_append_child(node, "stmt:", sym->stmt, inspect_statement);
break;
}
}
void inspect_symbol_list(AstNode *node)
{
inspect_ptr_list(node, "symbol_list", inspect_symbol);
}
static const char *expression_type_name(enum expression_type type)
{
static const char *expression_type_name[] = {
[EXPR_VALUE] = "EXPR_VALUE",
[EXPR_STRING] = "EXPR_STRING",
[EXPR_SYMBOL] = "EXPR_SYMBOL",
[EXPR_TYPE] = "EXPR_TYPE",
[EXPR_BINOP] = "EXPR_BINOP",
[EXPR_ASSIGNMENT] = "EXPR_ASSIGNMENT",
[EXPR_LOGICAL] = "EXPR_LOGICAL",
[EXPR_DEREF] = "EXPR_DEREF",
[EXPR_PREOP] = "EXPR_PREOP",
[EXPR_POSTOP] = "EXPR_POSTOP",
[EXPR_CAST] = "EXPR_CAST",
[EXPR_FORCE_CAST] = "EXPR_FORCE_CAST",
[EXPR_IMPLIED_CAST] = "EXPR_IMPLIED_CAST",
[EXPR_SIZEOF] = "EXPR_SIZEOF",
[EXPR_ALIGNOF] = "EXPR_ALIGNOF",
[EXPR_PTRSIZEOF] = "EXPR_PTRSIZEOF",
[EXPR_CONDITIONAL] = "EXPR_CONDITIONAL",
[EXPR_SELECT] = "EXPR_SELECT",
[EXPR_STATEMENT] = "EXPR_STATEMENT",
[EXPR_CALL] = "EXPR_CALL",
[EXPR_COMMA] = "EXPR_COMMA",
[EXPR_COMPARE] = "EXPR_COMPARE",
[EXPR_LABEL] = "EXPR_LABEL",
[EXPR_INITIALIZER] = "EXPR_INITIALIZER",
[EXPR_IDENTIFIER] = "EXPR_IDENTIFIER",
[EXPR_INDEX] = "EXPR_INDEX",
[EXPR_POS] = "EXPR_POS",
[EXPR_FVALUE] = "EXPR_FVALUE",
[EXPR_SLICE] = "EXPR_SLICE",
[EXPR_OFFSETOF] = "EXPR_OFFSETOF",
};
return expression_type_name[type] ?: "UNKNOWN_EXPRESSION_TYPE";
}
void inspect_expression(AstNode *node)
{
struct expression *expr = node->ptr;
node->text = g_strdup_printf("%s %s", node->text, expression_type_name(expr->type));
switch (expr->type) {
case EXPR_STATEMENT:
ast_append_child(node, "statement:", expr->statement, inspect_statement);
break;
case EXPR_BINOP:
case EXPR_COMMA:
case EXPR_COMPARE:
case EXPR_LOGICAL:
case EXPR_ASSIGNMENT:
ast_append_child(node, "left:", expr->left, inspect_expression);
ast_append_child(node, "right:", expr->right, inspect_expression);
break;
case EXPR_CAST:
case EXPR_FORCE_CAST:
case EXPR_IMPLIED_CAST:
ast_append_child(node, "cast_type:", expr->cast_type, inspect_symbol);
ast_append_child(node, "cast_expression:", expr->cast_expression, inspect_expression);
break;
case EXPR_PREOP:
ast_append_child(node, "unop:", expr->unop, inspect_expression);
break;
default:
break;
}
}