11 static const char *form_strtab
[] = {
24 static int next_id
= 1;
26 const char *stree_form_str(int form
)
28 return form_strtab
[form
- STF_NONE
];
31 struct stree
*stree_create(void)
34 st
= emalloc(sizeof(struct stree
));
35 memset(st
, 0, sizeof (struct stree
));
40 void stree_append_child(struct stree
*st_parent
, struct stree
*st_child
)
42 struct stree
*st_child_prev
= st_parent
->child
;
43 if (!st_parent
|| !st_child
)
45 st_child
->parent
= st_parent
;
47 // insert at end of child list
48 while (st_child_prev
->next
){
49 st_child_prev
= st_child_prev
->next
;
51 st_child_prev
->next
= st_child
;
52 st_child
->prev
= st_child_prev
;
53 st_child
->next
= NULL
;
56 st_parent
->child
= st_child
;
57 st_child
->prev
= NULL
;
58 st_child
->next
= NULL
;
62 void stree_remove_child(struct stree
*st_child
)
64 struct stree
*st_parent
= st_child
->parent
;
66 st_child
->prev
->next
= st_child
->next
;
68 st_parent
->child
= st_child
->next
;
71 st_child
->next
->prev
= st_child
->prev
;
73 st_child
->prev
= NULL
;
74 st_child
->next
= NULL
;
75 st_child
->parent
= NULL
;
78 void stree_destroy(struct stree
*st
)
82 struct stree
*st_child
= st
->child
,
86 st_child_next
= st_child
->next
;
87 stree_destroy(st_child
);
88 st_child
= st_child_next
;
95 void stree_next_child(struct stree
*st
, struct stree
**pchild
)
100 *pchild
= (*pchild
)->next
;
104 // form a string representing 'node', for debug output
105 static char *make_type_str(struct stree
*node
)
108 // is the type info valid?
113 // XXX: erm, use arrays perhaps?
114 if (node
->btype
.qualifiers
& TQ_RESTRICT
)
115 strdcat(&s
, "restrict ");
116 if (node
->btype
.qualifiers
& TQ_VOLATILE
)
117 strdcat(&s
, "volatile ");
118 if (node
->btype
.qualifiers
& TQ_CONST
)
119 strdcat(&s
, "const ");
120 if (node
->btype
.qualifiers
& TQ_SIGNED
)
121 strdcat(&s
, "signed ");
122 if (node
->btype
.qualifiers
& TQ_UNSIGNED
)
123 strdcat(&s
, "unsigned ");
124 if (node
->btype
.qualifiers
& TQ_LONG_LONG
)
125 strdcat(&s
, "long long ");
126 if (node
->btype
.qualifiers
& TQ_LONG
)
127 strdcat(&s
, "long ");
128 if (node
->btype
.qualifiers
& TQ_SHORT
)
129 strdcat(&s
, "short ");
130 switch (node
->btype
.base_type
){
141 strdcat(&s
, "float");
144 strdcat(&s
, "double");
147 strdcat(&s
, "pointer");
148 // TODO: pointing to what?
151 strdcat(&s
, "struct");
152 // TODO: more informations!
162 static void stree_dump_internal(struct cc
*cc
, struct stree_stack
*stack
, FILE *output
)
164 struct stree_stack stack_item
;
167 struct stree_stack
*p
= stack
;
169 // locate bottom of stack
173 while (p
&& p
->next
){
174 if (p
->st_node
->next
){
175 fprintf(output
, " | ");
177 fprintf(output
, " ");
181 if (stack
->st_node
->next
){
182 fprintf(output
, " +--");
184 fprintf(output
, " L--");
187 char *type_str
= make_type_str(stack
->st_node
);
188 fprintf(output
, "%d:%s [%s]%s%s\n", stack
->st_node
->id
,
189 stree_form_str(stack
->st_node
->form
),
190 lex_get_tok_str(&cc
->lex
, stack
->st_node
->tok
, NULL
),
191 type_str
? " : " : "",
192 type_str
? type_str
: "");
197 stack_item
.prev
= stack
;
198 stack_item
.next
= NULL
;
199 stack
->next
= &stack_item
;
200 stack_item
.st_node
= stack
->st_node
->child
;
201 while (stack_item
.st_node
){
202 stree_dump_internal(cc
, &stack_item
, output
);
203 stack_item
.st_node
= stack_item
.st_node
->next
;
205 stack
->next
= NULL
; // pop stack
209 void stree_dump(struct cc
*cc
, struct stree
*st_root
, FILE *output
)
211 struct stree_stack stack_item
;
213 fprintf(output
, "[TREE:NULL]\n");
216 stack_item
.st_node
= st_root
;
217 stack_item
.prev
= NULL
;
218 stack_item
.next
= NULL
;
219 stree_dump_internal(cc
, &stack_item
, output
);
222 struct stree
*stree_right(struct stree
*st
)
224 struct stree
*p
= st
->child
;
225 while (p
&& p
->next
){
231 int stree_child_count(struct stree
*st
)
234 struct stree
*child_st
;
238 child_st
= st
->child
;
241 child_st
= child_st
->next
;
246 struct stree
*stree_find_local(struct stree
*scope
, enum stree_form form
, tok_t tok
)
250 struct stree
*p
= scope
->child
;
251 while (p
&& !(p
->form
== form
&& p
->tok
== tok
)){
257 struct stree
*stree_get_child_by_form(struct stree
*st
, enum stree_form form
)
259 struct stree
*child_st
;
260 child_st
= st
->child
;
261 while (child_st
&& child_st
->form
!= form
){
262 child_st
= child_st
->next
;