xref: /onnv-gate/usr/src/tools/ctf/cvt/traverse.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Routines used to traverse tdesc trees, invoking user-supplied callbacks
31*0Sstevel@tonic-gate  * as the tree is traversed.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <assert.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "ctftools.h"
38*0Sstevel@tonic-gate #include "traverse.h"
39*0Sstevel@tonic-gate #include "memory.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate int (*tddescenders[])();
42*0Sstevel@tonic-gate int (*tdnops[])();
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate int tdtraverse(tdesc_t *, tdesc_t **, tdtrav_data_t *);
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate void
47*0Sstevel@tonic-gate tdtrav_init(tdtrav_data_t *tdtd, int *vgenp, tdtrav_cb_f *firstops,
48*0Sstevel@tonic-gate     tdtrav_cb_f *preops, tdtrav_cb_f *postops, void *private)
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate 	tdtd->vgen = ++(*vgenp);
51*0Sstevel@tonic-gate 	tdtd->firstops = firstops ? firstops : tdnops;
52*0Sstevel@tonic-gate 	tdtd->preops = preops ? preops : tdnops;
53*0Sstevel@tonic-gate 	tdtd->postops = postops ? postops : tdnops;
54*0Sstevel@tonic-gate 	tdtd->private = private;
55*0Sstevel@tonic-gate }
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate static int
58*0Sstevel@tonic-gate tdtrav_plain(tdesc_t *this, tdtrav_data_t *tdtd)
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	return (tdtraverse(this->t_tdesc, &this->t_tdesc, tdtd));
61*0Sstevel@tonic-gate }
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate static int
64*0Sstevel@tonic-gate tdtrav_func(tdesc_t *this, tdtrav_data_t *tdtd)
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate 	fndef_t *fn = this->t_fndef;
67*0Sstevel@tonic-gate 	int i, rc;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if ((rc = tdtraverse(fn->fn_ret, &fn->fn_ret, tdtd)) < 0)
70*0Sstevel@tonic-gate 		return (rc);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	for (i = 0; i < fn->fn_nargs; i++) {
73*0Sstevel@tonic-gate 		if ((rc = tdtraverse(fn->fn_args[i], &fn->fn_args[i],
74*0Sstevel@tonic-gate 		    tdtd)) < 0)
75*0Sstevel@tonic-gate 			return (rc);
76*0Sstevel@tonic-gate 	}
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	return (0);
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate static int
82*0Sstevel@tonic-gate tdtrav_array(tdesc_t *this, tdtrav_data_t *tdtd)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	ardef_t *ardef = this->t_ardef;
85*0Sstevel@tonic-gate 	int rc;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	if ((rc = tdtraverse(ardef->ad_contents, &ardef->ad_contents,
88*0Sstevel@tonic-gate 	    tdtd)) < 0)
89*0Sstevel@tonic-gate 		return (rc);
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	return (tdtraverse(ardef->ad_idxtype, &ardef->ad_idxtype, tdtd));
92*0Sstevel@tonic-gate }
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate static int
95*0Sstevel@tonic-gate tdtrav_su(tdesc_t *this, tdtrav_data_t *tdtd)
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate 	mlist_t *ml;
98*0Sstevel@tonic-gate 	int rc;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	for (ml = this->t_members; ml; ml = ml->ml_next) {
101*0Sstevel@tonic-gate 		if ((rc = tdtraverse(ml->ml_type, &ml->ml_type, tdtd)) < 0)
102*0Sstevel@tonic-gate 			return (rc);
103*0Sstevel@tonic-gate 	}
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	return (rc);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate /*ARGSUSED*/
109*0Sstevel@tonic-gate int
110*0Sstevel@tonic-gate tdtrav_assert(tdesc_t *node, tdesc_t **nodep, void *private)
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 	assert(1 == 0);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	return (-1);
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate tdtrav_cb_f tdnops[] = {
118*0Sstevel@tonic-gate 	NULL,
119*0Sstevel@tonic-gate 	NULL,			/* intrinsic */
120*0Sstevel@tonic-gate 	NULL,			/* pointer */
121*0Sstevel@tonic-gate 	NULL,			/* array */
122*0Sstevel@tonic-gate 	NULL,			/* function */
123*0Sstevel@tonic-gate 	NULL,			/* struct */
124*0Sstevel@tonic-gate 	NULL,			/* union */
125*0Sstevel@tonic-gate 	NULL,			/* enum */
126*0Sstevel@tonic-gate 	NULL,			/* forward */
127*0Sstevel@tonic-gate 	NULL,			/* typedef */
128*0Sstevel@tonic-gate 	NULL,			/* typedef_unres */
129*0Sstevel@tonic-gate 	NULL,			/* volatile */
130*0Sstevel@tonic-gate 	NULL,			/* const */
131*0Sstevel@tonic-gate 	NULL			/* restrict */
132*0Sstevel@tonic-gate };
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate int (*tddescenders[])(tdesc_t *, tdtrav_data_t *) = {
135*0Sstevel@tonic-gate 	NULL,
136*0Sstevel@tonic-gate 	NULL,			/* intrinsic */
137*0Sstevel@tonic-gate 	tdtrav_plain,		/* pointer */
138*0Sstevel@tonic-gate 	tdtrav_array,		/* array */
139*0Sstevel@tonic-gate 	tdtrav_func,		/* function */
140*0Sstevel@tonic-gate 	tdtrav_su,		/* struct */
141*0Sstevel@tonic-gate 	tdtrav_su,		/* union */
142*0Sstevel@tonic-gate 	NULL,			/* enum */
143*0Sstevel@tonic-gate 	NULL,			/* forward */
144*0Sstevel@tonic-gate 	tdtrav_plain,		/* typedef */
145*0Sstevel@tonic-gate 	NULL,			/* typedef_unres */
146*0Sstevel@tonic-gate 	tdtrav_plain,		/* volatile */
147*0Sstevel@tonic-gate 	tdtrav_plain,		/* const */
148*0Sstevel@tonic-gate 	tdtrav_plain		/* restrict */
149*0Sstevel@tonic-gate };
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate int
152*0Sstevel@tonic-gate tdtraverse(tdesc_t *this, tdesc_t **thisp, tdtrav_data_t *tdtd)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	tdtrav_cb_f travcb;
155*0Sstevel@tonic-gate 	int (*descender)();
156*0Sstevel@tonic-gate 	int descend = 1;
157*0Sstevel@tonic-gate 	int rc;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	if ((travcb = tdtd->firstops[this->t_type]) != NULL) {
160*0Sstevel@tonic-gate 		if ((rc = travcb(this, thisp, tdtd->private)) < 0)
161*0Sstevel@tonic-gate 			return (rc);
162*0Sstevel@tonic-gate 		else if (rc == 0)
163*0Sstevel@tonic-gate 			descend = 0;
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if (this->t_vgen == tdtd->vgen)
167*0Sstevel@tonic-gate 		return (1);
168*0Sstevel@tonic-gate 	this->t_vgen = tdtd->vgen;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	if (descend && (travcb = tdtd->preops[this->t_type]) != NULL) {
171*0Sstevel@tonic-gate 		if ((rc = travcb(this, thisp, tdtd->private)) < 0)
172*0Sstevel@tonic-gate 			return (rc);
173*0Sstevel@tonic-gate 		else if (rc == 0)
174*0Sstevel@tonic-gate 			descend = 0;
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	if (descend) {
178*0Sstevel@tonic-gate 		if ((descender = tddescenders[this->t_type]) != NULL &&
179*0Sstevel@tonic-gate 		    (rc = descender(this, tdtd)) < 0)
180*0Sstevel@tonic-gate 			return (rc);
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 		if ((travcb = tdtd->postops[this->t_type]) != NULL &&
183*0Sstevel@tonic-gate 		    (rc = travcb(this, thisp, tdtd->private)) < 0)
184*0Sstevel@tonic-gate 			return (rc);
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	return (1);
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate int
191*0Sstevel@tonic-gate iitraverse_td(iidesc_t *ii, tdtrav_data_t *tdtd)
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate 	int i, rc;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	if ((rc = tdtraverse(ii->ii_dtype, &ii->ii_dtype, tdtd)) < 0)
196*0Sstevel@tonic-gate 		return (rc);
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	for (i = 0; i < ii->ii_nargs; i++) {
199*0Sstevel@tonic-gate 		if ((rc = tdtraverse(ii->ii_args[i], &ii->ii_args[i],
200*0Sstevel@tonic-gate 		    tdtd)) < 0)
201*0Sstevel@tonic-gate 			return (rc);
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	return (1);
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate int
208*0Sstevel@tonic-gate iitraverse(iidesc_t *ii, int *vgenp, tdtrav_cb_f *firstops, tdtrav_cb_f *preops,
209*0Sstevel@tonic-gate     tdtrav_cb_f *postops, void *private)
210*0Sstevel@tonic-gate {
211*0Sstevel@tonic-gate 	tdtrav_data_t tdtd;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	tdtrav_init(&tdtd, vgenp, firstops, preops, postops, private);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	return (iitraverse_td(ii, &tdtd));
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate int
219*0Sstevel@tonic-gate iitraverse_hash(hash_t *iihash, int *vgenp, tdtrav_cb_f *firstops,
220*0Sstevel@tonic-gate     tdtrav_cb_f *preops, tdtrav_cb_f *postops, void *private)
221*0Sstevel@tonic-gate {
222*0Sstevel@tonic-gate 	tdtrav_data_t tdtd;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	tdtrav_init(&tdtd, vgenp, firstops, preops, postops, private);
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	return (hash_iter(iihash, (int (*)())iitraverse_td, &tdtd));
227*0Sstevel@tonic-gate }
228