forked from farisawan-2000/cfront-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_copy.c
320 lines (286 loc) · 11.9 KB
/
tree_copy.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*ident "@(#)cls4:src/tree_copy.c 1.7" */
/*******************************************************************************
C++ source for the C++ Language System, Release 3.0. This product
is a new release of the original cfront developed in the computer
science research center of AT&T Bell Laboratories.
Copyright (c) 1993 UNIX System Laboratories, Inc.
Copyright (c) 1991, 1992 AT&T and UNIX System Laboratories, Inc.
Copyright (c) 1984, 1989, 1990 AT&T. All Rights Reserved.
THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE of AT&T and UNIX System
Laboratories, Inc. The copyright notice above does not evidence
any actual or intended publication of such source code.
*******************************************************************************/
/******************************************************************************
* Copyright (c) 1989 by Object Design, Inc., Burlington, Mass.
* All rights reserved.
*******************************************************************************/
/* utilities to copy pieces of cfront trees.
what we have here is a somewhat parameterizable
action procedure for the tree walker.
We may make more versions of this for template expansion
and saving things in files, or we may just make it possible
for this to swing more ways.
*/
#include "cfront.h"
#include "template.h"
#include "tree_walk.h"
#include "tree_copy.h"
#include <memory.h>
/* determine whether type "n" is already DEFINED or has been COPIED */
inline unsigned int type_is_defined(Pnode n) {
return Ptype(n)->defined & (DEFINED | COPIED);
}
void copy_walker(Pnode &node, node_class cl, void *info, tree_node_action &action, int, Pnode,
tree_walk_tree &, int ®ister_in_hash) {
union {
Pnode node;
Pvirt vr;
Plist list;
Pgen g;
Pvec vc;
Pfct f;
Ptable t;
Pktab kt;
Pbase bt;
Pexpr x;
Pstmt s;
Ptype tp;
Penum e;
Pclass c;
Pbcl bcl;
Pin iline;
ia *_ia;
Pname n;
Pptr p;
} n;
Pnode original_node = node;
tree_copy_info *tci = (tree_copy_info *) info;
action = tna_continue;
/* first, give an application-specific hook a shot at the node. */
tci->check_node(node, cl, action, register_in_hash);
if (action != tna_continue)
return;
if (node != original_node) { /* a replacement */
n.node = node;
} else /* ok, nothing funny, we just go ahead and copy */
switch (cl) {
case nc_unused:
break;
case nc_eof:
n.node = Pnode(tci->malloc(sizeof(node)));
*n.node = *node;
goto Replace;
case nc_virt:
n.vr = Pvirt(tci->malloc(sizeof(virt)));
*n.vr = *Pvirt(node);
goto Replace;
case nc_nlist:
n.list = Plist(tci->malloc(sizeof(name_list)));
*n.list = *Plist(node);
goto Replace;
case nc_gen:
n.g = Pgen(tci->malloc(sizeof(gen)));
*n.g = *Pgen(node);
goto Replace;
case nc_vec:
n.vc = Pvec(tci->malloc(sizeof(vec)));
*n.vc = *Pvec(node);
goto Replace;
case nc_ptr:
n.p = Pptr(tci->malloc(sizeof(ptr)));
*n.p = *Pptr(node);
goto Replace;
case nc_fct:
n.f = Pfct(tci->malloc(sizeof(fct)));
*n.f = *Pfct(node);
if (n.f->f_signature) {
n.f->f_signature = (char *) tci->malloc(strlen(Pfct(node)->f_signature) + 1);
strcpy(n.f->f_signature, Pfct(node)->f_signature);
};
goto Replace;
case nc_table:
n.t = Ptable(tci->malloc(sizeof(table)));
*n.t = *Ptable(node);
patch_tree::add(n.node);
n.t->entries = (Pname *) tci->malloc(sizeof(Pname) * n.t->size);
memcpy((char *) n.t->entries, (char *) Ptable(node)->entries,
n.t->size * sizeof(Pname));
n.t->hashtbl = (short *) tci->malloc(sizeof(short) * n.t->hashsize);
memcpy((char *) n.t->hashtbl, (char *) Ptable(node)->hashtbl,
n.t->hashsize * sizeof(short));
goto Replace;
case nc_ktable:
// hope that nc_table gets the k_t
n.kt = Pktab(tci->malloc(sizeof(ktable)));
*n.kt = *Pktab(node);
patch_tree::add(n.node);
goto Replace;
case nc_basetype:
// Don't copy types that have already been dealt with
if (type_is_defined(node)) {
action = tna_stop;
return;
}
n.bt = Pbase(tci->malloc(sizeof(basetype)));
*n.bt = *Pbase(node);
n.bt->defined |= COPIED;
if (n.bt->discriminator(0) == 2 && n.bt->b_linkage) {
n.bt->b_linkage = tci->malloc(strlen(n.bt->b_linkage) + 1);
strcpy(n.bt->b_linkage, Pbase(node)->b_linkage);
}
goto Replace;
case nc_expr:
// cfront needs identity maintaied for these nodes
if ((node == dummy) || (node == zero)) {
action = tna_stop;
return;
}
n.x = Pexpr(tci->malloc(sizeof(expr)));
*n.x = *Pexpr(node);
if (n.x->discriminator(1) == 3 && n.x->string) {
n.x->string = tci->malloc(strlen(n.x->string) + 1);
strcpy(n.x->string, Pexpr(node)->string);
}
if (n.x->discriminator(2) == 3 && n.x->string2) {
n.x->string2 = tci->malloc(strlen(n.x->string2) + 1);
strcpy(n.x->string2, Pexpr(node)->string2);
}
goto Replace;
case nc_stmt:
n.s = Pstmt(tci->malloc(sizeof(stmt)));
*n.s = *Pstmt(node);
goto Replace;
case nc_enumdef:
if (type_is_defined(node)) {
action = tna_stop;
return;
}
n.e = Penum(tci->malloc(sizeof(enumdef)));
*n.e = *Penum(node);
n.e->defined |= COPIED;
if (n.e->string) {
n.e->string = tci->malloc(n.e->e_strlen + 1);
strcpy(n.e->string, Penum(node)->string);
}
if (n.e->nested_sig) {
n.e->nested_sig = 0; // deleted in 'name::dcl' anyway
}
goto Replace;
case nc_classdef:
// Don't copy types that have already been dealt with
if (type_is_defined(node)) {
action = tna_stop;
return;
}
n.c = Pclass(tci->malloc(sizeof(classdef)));
*n.c = *Pclass(node);
n.c->defined |= COPIED;
if (n.c->string) {
n.c->string = tci->malloc(strlen(n.c->string) + 1);
strcpy(n.c->string, Pclass(node)->string);
}
if (n.c->nested_sig) {
n.c->nested_sig = tci->malloc(strlen(n.c->nested_sig) + 1);
strcpy(n.c->nested_sig, Pclass(node)->nested_sig);
}
goto Replace;
case nc_baseclass:
n.bcl = Pbcl(tci->malloc(sizeof(struct basecl)));
*n.bcl = *Pbcl(node);
goto Replace;
case nc_tpdef:
n.tp = Ptype(tci->malloc(sizeof(struct type)));
*n.tp = *Ptype(node);
if (n.tp->nested_sig) {
n.tp->nested_sig = tci->malloc(strlen(n.tp->nested_sig) + 1);
strcpy(n.tp->nested_sig, Ptype(node)->nested_sig);
}
goto Replace;
case nc_iline:
n.iline = Pin(tci->malloc(sizeof(iline)));
*n.iline = *Pin(node);
goto Replace;
case nc_ia:
n._ia = (ia *) tci->malloc(sizeof(ia));
*n._ia = *(ia *) node;
goto Replace;
case nc_name:
/* check for globalosity */
if (Pname(node)->string) {
// ::error('d',"tree_copy: nc_name: %n", Pname(node));
Pname n;
if (node->base == TNAME) {
n = k_find_name(Pname(node)->string, Gtbl, 0);
if (n && n->base == NAME)
n = 0;
} else
n = gtbl->look(Pname(node)->string, 0);
if ((Pname) node == n) {
action = tna_stop;
return;
}
}
n.n = Pname(tci->malloc(sizeof(name)));
*n.n = *Pname(node);
if (n.n->n_ktable)
patch_tree::add(n.node);
/* First, hack exprosity */
{
int done = 0;
if (n.n->base == MEMQ && n.n->string) {
Pbase_inst bi = (Pbase_inst) tci->hook_info;
for (Plist formal = bi->inst_formals; formal; formal = formal->l) {
if (formal->f->n_template_arg != template_type_formal)
continue;
if (strcmp(formal->f->string, n.n->string) == 0) {
Ptype t = formal->f->tp;
Pname cn = t->is_cl_obj();
if (cn || eobj) {
n.n->string = tci->malloc(
strlen(eobj ? Penum(eobj->tp)->string : Pclass(cn->tp)->string)
+ 1);
strcpy(n.n->string,
eobj ? Penum(eobj->tp)->string : Pclass(cn->tp)->string);
} else {
n.n->string = 0;
}
done = 1;
break;
}
}
} else if (!done && n.x->discriminator(1) == 3 && n.n->string) {
n.n->string = tci->malloc(strlen(n.n->string) + 1);
strcpy(n.n->string, Pexpr(node)->string);
}
}
if (n.x->discriminator(2) == 3 && n.n->string2) {
n.n->string2 = tci->malloc(strlen(n.n->string2) + 1);
strcpy(n.n->string2, Pexpr(node)->string2);
/* ok, name stuff */
}
if (n.n->n_anon) {
n.n->n_anon = tci->malloc(strlen(n.n->n_anon) + 1);
strcpy(n.n->n_anon, Pname(node)->n_anon);
}
if (n.n->n_template_arg_string) {
n.n->n_template_arg_string = tci->malloc(strlen(n.n->n_template_arg_string) + 1);
strcpy(n.n->n_template_arg_string, Pname(node)->n_template_arg_string);
}
}
Replace:
node = n.node;
action = tna_continue;
return;
}
static int call_error(int i, const char *s) {
return error(i, s);
}
void copy_tree(Pnode &node, tree_copy_info &tci, Hash *cht) {
tree_walk_control twc;
twc.call_i_error = 1;
twc.i_error = call_error; /* ... in type of error confuses compiler */
twc.action_proc = copy_walker;
twc.nodes_seen_hash = cht;
twc.callback_info = (void *) &tci;
walk_tree(twc, node);
}