11 static const char *form_strtab
[] = {
23 static int next_id
= 1;
25 const char *stree_form_str(int form
)
27 return form_strtab
[form
- STF_NONE
];
30 struct stree
*stree_create(void)
33 st
= emalloc(sizeof(struct stree
));
34 memset(st
, 0, sizeof (struct stree
));
39 void stree_append_child(struct stree
*st_parent
, struct stree
*st_child
)
41 struct stree
*st_child_prev
= st_parent
->child
;
42 if (!st_parent
|| !st_child
)
44 st_child
->parent
= st_parent
;
46 // insert at end of child list
47 while (st_child_prev
->next
){
48 st_child_prev
= st_child_prev
->next
;
50 st_child_prev
->next
= st_child
;
51 st_child
->prev
= st_child_prev
;
52 st_child
->next
= NULL
;
55 st_parent
->child
= st_child
;
56 st_child
->prev
= NULL
;
57 st_child
->next
= NULL
;
61 void stree_remove_child(struct stree
*st_child
)
63 struct stree
*st_parent
= st_child
->parent
;
65 st_child
->prev
->next
= st_child
->next
;
67 st_parent
->child
= st_child
->next
;
70 st_child
->next
->prev
= st_child
->prev
;
72 st_child
->prev
= NULL
;
73 st_child
->next
= NULL
;
74 st_child
->parent
= NULL
;
77 void stree_destroy(struct stree
*st
)
81 struct stree
*st_child
= st
->child
,
85 st_child_next
= st_child
->next
;
86 stree_destroy(st_child
);
87 st_child
= st_child_next
;
94 void stree_next_child(struct stree
*st
, struct stree
**pchild
)
99 *pchild
= (*pchild
)->next
;
103 // form a string representing 'node', for debug output
104 static char *make_type_str(struct stree
*node
)
107 // is the type info valid?
112 // XXX: erm, use arrays perhaps?
113 if (node
->btype
.qualifiers
& TQ_RESTRICT
)
114 strdcat(&s
, "restrict ");
115 if (node
->btype
.qualifiers
& TQ_VOLATILE
)
116 strdcat(&s
, "volatile ");
117 if (node
->btype
.qualifiers
& TQ_CONST
)
118 strdcat(&s
, "const ");
119 if (node
->btype
.qualifiers
& TQ_SIGNED
)
120 strdcat(&s
, "signed ");
121 if (node
->btype
.qualifiers
& TQ_UNSIGNED
)
122 strdcat(&s
, "unsigned ");
123 if (node
->btype
.qualifiers
& TQ_LONG_LONG
)
124 strdcat(&s
, "long long ");
125 if (node
->btype
.qualifiers
& TQ_LONG
)
126 strdcat(&s
, "long ");
127 if (node
->btype
.qualifiers
& TQ_SHORT
)
128 strdcat(&s
, "short ");
129 switch (node
->btype
.base_type
){
140 strdcat(&s
, "float");
143 strdcat(&s
, "double");
146 strdcat(&s
, "pointer");
147 // TODO: pointing to what?
150 strdcat(&s
, "struct");
151 // TODO: more informations!
161 static void stree_dump_internal(struct cc
*cc
, struct stree_stack
*stack
, FILE *output
)
163 struct stree_stack stack_item
;
166 struct stree_stack
*p
= stack
;
168 // locate bottom of stack
172 while (p
&& p
->next
){
173 if (p
->st_node
->next
){
174 fprintf(output
, " | ");
176 fprintf(output
, " ");
180 if (stack
->st_node
->next
){
181 fprintf(output
, " +--");
183 fprintf(output
, " L--");
186 char *type_str
= make_type_str(stack
->st_node
);
187 fprintf(output
, "%d:%s [%s]%s%s\n", stack
->st_node
->id
,
188 stree_form_str(stack
->st_node
->form
),
189 lex_get_tok_str(&cc
->lex
, stack
->st_node
->tok
, NULL
),
190 type_str
? " : " : "",
191 type_str
? type_str
: "");
196 stack_item
.prev
= stack
;
197 stack_item
.next
= NULL
;
198 stack
->next
= &stack_item
;
199 stack_item
.st_node
= stack
->st_node
->child
;
200 while (stack_item
.st_node
){
201 stree_dump_internal(cc
, &stack_item
, output
);
202 stack_item
.st_node
= stack_item
.st_node
->next
;
204 stack
->next
= NULL
; // pop stack
208 void stree_dump(struct cc
*cc
, struct stree
*st_root
, FILE *output
)
210 struct stree_stack stack_item
;
212 fprintf(output
, "[TREE:NULL]\n");
215 stack_item
.st_node
= st_root
;
216 stack_item
.prev
= NULL
;
217 stack_item
.next
= NULL
;
218 stree_dump_internal(cc
, &stack_item
, output
);
221 struct stree
*stree_right(struct stree
*st
)
223 struct stree
*p
= st
->child
;
224 while (p
&& p
->next
){
230 int stree_child_count(struct stree
*st
)
233 struct stree
*child_st
;
237 child_st
= st
->child
;
240 child_st
= child_st
->next
;
245 struct stree
*stree_find_local(struct stree
*scope
, enum stree_form form
, tok_t tok
)
249 struct stree
*p
= scope
->child
;
250 while (p
&& !(p
->form
== form
&& p
->tok
== tok
)){
256 struct stree
*stree_get_child_by_form(struct stree
*st
, enum stree_form form
)
258 struct stree
*child_st
;
259 child_st
= st
->child
;
260 while (child_st
&& child_st
->form
!= form
){
261 child_st
= child_st
->next
;