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