5 #include "ast-inspect.h"
6 #include "expression.h"
8 static inline void inspect_ptr_list(AstNode
*node
, const char *name
, void (*inspect
)(AstNode
*))
10 struct ptr_list
*ptrlist
= node
->ptr
;
14 node
->text
= g_strdup_printf("%s %s:", node
->text
, name
);
15 FOR_EACH_PTR(ptrlist
, ptr
) {
16 char *index
= g_strdup_printf("%d: ", i
++);
17 ast_append_child(node
, index
, ptr
, inspect
);
18 } END_FOR_EACH_PTR(ptr
);
22 static const char *statement_type_name(enum statement_type type
)
24 static const char *statement_type_name
[] = {
25 [STMT_NONE
] = "STMT_NONE",
26 [STMT_DECLARATION
] = "STMT_DECLARATION",
27 [STMT_EXPRESSION
] = "STMT_EXPRESSION",
28 [STMT_COMPOUND
] = "STMT_COMPOUND",
29 [STMT_IF
] = "STMT_IF",
30 [STMT_RETURN
] = "STMT_RETURN",
31 [STMT_CASE
] = "STMT_CASE",
32 [STMT_SWITCH
] = "STMT_SWITCH",
33 [STMT_ITERATOR
] = "STMT_ITERATOR",
34 [STMT_LABEL
] = "STMT_LABEL",
35 [STMT_GOTO
] = "STMT_GOTO",
36 [STMT_ASM
] = "STMT_ASM",
37 [STMT_CONTEXT
] = "STMT_CONTEXT",
38 [STMT_RANGE
] = "STMT_RANGE",
40 return statement_type_name
[type
] ?: "UNKNOWN_STATEMENT_TYPE";
43 void inspect_statement(AstNode
*node
)
45 struct statement
*stmt
= node
->ptr
;
46 node
->text
= g_strdup_printf("%s %s:", node
->text
, statement_type_name(stmt
->type
));
49 ast_append_child(node
, "stmts:", stmt
->stmts
, inspect_statement_list
);
52 ast_append_child(node
, "expression:", stmt
->expression
, inspect_expression
);
55 ast_append_child(node
, "conditional:", stmt
->if_conditional
, inspect_expression
);
56 ast_append_child(node
, "if_true:", stmt
->if_true
, inspect_statement
);
57 ast_append_child(node
, "if_false:", stmt
->if_false
, inspect_statement
);
60 ast_append_child(node
, "break:", stmt
->iterator_break
, inspect_symbol
);
61 ast_append_child(node
, "continue:", stmt
->iterator_continue
, inspect_symbol
);
62 ast_append_child(node
, "pre_statement:", stmt
->iterator_pre_statement
,
64 ast_append_child(node
, "statement:", stmt
->iterator_statement
,
66 ast_append_child(node
, "post_statement:", stmt
->iterator_post_statement
,
75 void inspect_statement_list(AstNode
*node
)
77 inspect_ptr_list(node
, "statement_list", inspect_statement
);
81 static const char *symbol_type_name(enum type type
)
83 static const char *type_name
[] = {
84 [SYM_UNINITIALIZED
] = "SYM_UNINITIALIZED",
85 [SYM_PREPROCESSOR
] = "SYM_PREPROCESSOR",
86 [SYM_BASETYPE
] = "SYM_BASETYPE",
87 [SYM_NODE
] = "SYM_NODE",
88 [SYM_PTR
] = "SYM_PTR",
90 [SYM_ARRAY
] = "SYM_ARRAY",
91 [SYM_STRUCT
] = "SYM_STRUCT",
92 [SYM_UNION
] = "SYM_UNION",
93 [SYM_ENUM
] = "SYM_ENUM",
94 [SYM_TYPEDEF
] = "SYM_TYPEDEF",
95 [SYM_TYPEOF
] = "SYM_TYPEOF",
96 [SYM_MEMBER
] = "SYM_MEMBER",
97 [SYM_BITFIELD
] = "SYM_BITFIELD",
98 [SYM_LABEL
] = "SYM_LABEL",
99 [SYM_RESTRICT
] = "SYM_RESTRICT",
100 [SYM_FOULED
] = "SYM_FOULED",
101 [SYM_KEYWORD
] = "SYM_KEYWORD",
102 [SYM_BAD
] = "SYM_BAD",
104 return type_name
[type
] ?: "UNKNOWN_TYPE";
108 void inspect_symbol(AstNode
*node
)
110 struct symbol
*sym
= node
->ptr
;
111 node
->text
= g_strdup_printf("%s %s: %s", node
->text
, symbol_type_name(sym
->type
),
112 show_ident(sym
->ident
));
113 ast_append_child(node
, "ctype.base_type:", sym
->ctype
.base_type
,inspect_symbol
);
117 ast_append_child(node
, "stmt:", sym
->stmt
, inspect_statement
);
125 void inspect_symbol_list(AstNode
*node
)
127 inspect_ptr_list(node
, "symbol_list", inspect_symbol
);
131 static const char *expression_type_name(enum expression_type type
)
133 static const char *expression_type_name
[] = {
134 [EXPR_VALUE
] = "EXPR_VALUE",
135 [EXPR_STRING
] = "EXPR_STRING",
136 [EXPR_SYMBOL
] = "EXPR_SYMBOL",
137 [EXPR_TYPE
] = "EXPR_TYPE",
138 [EXPR_BINOP
] = "EXPR_BINOP",
139 [EXPR_ASSIGNMENT
] = "EXPR_ASSIGNMENT",
140 [EXPR_LOGICAL
] = "EXPR_LOGICAL",
141 [EXPR_DEREF
] = "EXPR_DEREF",
142 [EXPR_PREOP
] = "EXPR_PREOP",
143 [EXPR_POSTOP
] = "EXPR_POSTOP",
144 [EXPR_CAST
] = "EXPR_CAST",
145 [EXPR_FORCE_CAST
] = "EXPR_FORCE_CAST",
146 [EXPR_IMPLIED_CAST
] = "EXPR_IMPLIED_CAST",
147 [EXPR_SIZEOF
] = "EXPR_SIZEOF",
148 [EXPR_ALIGNOF
] = "EXPR_ALIGNOF",
149 [EXPR_PTRSIZEOF
] = "EXPR_PTRSIZEOF",
150 [EXPR_CONDITIONAL
] = "EXPR_CONDITIONAL",
151 [EXPR_SELECT
] = "EXPR_SELECT",
152 [EXPR_STATEMENT
] = "EXPR_STATEMENT",
153 [EXPR_CALL
] = "EXPR_CALL",
154 [EXPR_COMMA
] = "EXPR_COMMA",
155 [EXPR_COMPARE
] = "EXPR_COMPARE",
156 [EXPR_LABEL
] = "EXPR_LABEL",
157 [EXPR_INITIALIZER
] = "EXPR_INITIALIZER",
158 [EXPR_IDENTIFIER
] = "EXPR_IDENTIFIER",
159 [EXPR_INDEX
] = "EXPR_INDEX",
160 [EXPR_POS
] = "EXPR_POS",
161 [EXPR_FVALUE
] = "EXPR_FVALUE",
162 [EXPR_SLICE
] = "EXPR_SLICE",
163 [EXPR_OFFSETOF
] = "EXPR_OFFSETOF",
165 return expression_type_name
[type
] ?: "UNKNOWN_EXPRESSION_TYPE";
168 void inspect_expression(AstNode
*node
)
170 struct expression
*expr
= node
->ptr
;
171 node
->text
= g_strdup_printf("%s %s", node
->text
, expression_type_name(expr
->type
));
172 switch (expr
->type
) {
174 ast_append_child(node
, "statement:", expr
->statement
, inspect_statement
);
180 case EXPR_ASSIGNMENT
:
181 ast_append_child(node
, "left:", expr
->left
, inspect_expression
);
182 ast_append_child(node
, "right:", expr
->right
, inspect_expression
);