10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1882Sjohnlev * Common Development and Distribution License (the "License").
6*1882Sjohnlev * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*1882Sjohnlev * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Routines used to traverse tdesc trees, invoking user-supplied callbacks
300Sstevel@tonic-gate * as the tree is traversed.
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <assert.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include "ctftools.h"
370Sstevel@tonic-gate #include "traverse.h"
380Sstevel@tonic-gate #include "memory.h"
390Sstevel@tonic-gate
400Sstevel@tonic-gate int (*tddescenders[])();
410Sstevel@tonic-gate int (*tdnops[])();
420Sstevel@tonic-gate
430Sstevel@tonic-gate int tdtraverse(tdesc_t *, tdesc_t **, tdtrav_data_t *);
440Sstevel@tonic-gate
450Sstevel@tonic-gate void
tdtrav_init(tdtrav_data_t * tdtd,int * vgenp,tdtrav_cb_f * firstops,tdtrav_cb_f * preops,tdtrav_cb_f * postops,void * private)460Sstevel@tonic-gate tdtrav_init(tdtrav_data_t *tdtd, int *vgenp, tdtrav_cb_f *firstops,
470Sstevel@tonic-gate tdtrav_cb_f *preops, tdtrav_cb_f *postops, void *private)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate tdtd->vgen = ++(*vgenp);
500Sstevel@tonic-gate tdtd->firstops = firstops ? firstops : tdnops;
510Sstevel@tonic-gate tdtd->preops = preops ? preops : tdnops;
520Sstevel@tonic-gate tdtd->postops = postops ? postops : tdnops;
530Sstevel@tonic-gate tdtd->private = private;
540Sstevel@tonic-gate }
550Sstevel@tonic-gate
560Sstevel@tonic-gate static int
tdtrav_plain(tdesc_t * this,tdtrav_data_t * tdtd)570Sstevel@tonic-gate tdtrav_plain(tdesc_t *this, tdtrav_data_t *tdtd)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate return (tdtraverse(this->t_tdesc, &this->t_tdesc, tdtd));
600Sstevel@tonic-gate }
610Sstevel@tonic-gate
620Sstevel@tonic-gate static int
tdtrav_func(tdesc_t * this,tdtrav_data_t * tdtd)630Sstevel@tonic-gate tdtrav_func(tdesc_t *this, tdtrav_data_t *tdtd)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate fndef_t *fn = this->t_fndef;
660Sstevel@tonic-gate int i, rc;
670Sstevel@tonic-gate
680Sstevel@tonic-gate if ((rc = tdtraverse(fn->fn_ret, &fn->fn_ret, tdtd)) < 0)
690Sstevel@tonic-gate return (rc);
700Sstevel@tonic-gate
710Sstevel@tonic-gate for (i = 0; i < fn->fn_nargs; i++) {
720Sstevel@tonic-gate if ((rc = tdtraverse(fn->fn_args[i], &fn->fn_args[i],
730Sstevel@tonic-gate tdtd)) < 0)
740Sstevel@tonic-gate return (rc);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate
770Sstevel@tonic-gate return (0);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate
800Sstevel@tonic-gate static int
tdtrav_array(tdesc_t * this,tdtrav_data_t * tdtd)810Sstevel@tonic-gate tdtrav_array(tdesc_t *this, tdtrav_data_t *tdtd)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate ardef_t *ardef = this->t_ardef;
840Sstevel@tonic-gate int rc;
850Sstevel@tonic-gate
860Sstevel@tonic-gate if ((rc = tdtraverse(ardef->ad_contents, &ardef->ad_contents,
870Sstevel@tonic-gate tdtd)) < 0)
880Sstevel@tonic-gate return (rc);
890Sstevel@tonic-gate
900Sstevel@tonic-gate return (tdtraverse(ardef->ad_idxtype, &ardef->ad_idxtype, tdtd));
910Sstevel@tonic-gate }
920Sstevel@tonic-gate
930Sstevel@tonic-gate static int
tdtrav_su(tdesc_t * this,tdtrav_data_t * tdtd)940Sstevel@tonic-gate tdtrav_su(tdesc_t *this, tdtrav_data_t *tdtd)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate mlist_t *ml;
97*1882Sjohnlev int rc = 0;
980Sstevel@tonic-gate
990Sstevel@tonic-gate for (ml = this->t_members; ml; ml = ml->ml_next) {
1000Sstevel@tonic-gate if ((rc = tdtraverse(ml->ml_type, &ml->ml_type, tdtd)) < 0)
1010Sstevel@tonic-gate return (rc);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate return (rc);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate /*ARGSUSED*/
1080Sstevel@tonic-gate int
tdtrav_assert(tdesc_t * node,tdesc_t ** nodep,void * private)1090Sstevel@tonic-gate tdtrav_assert(tdesc_t *node, tdesc_t **nodep, void *private)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate assert(1 == 0);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate return (-1);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate tdtrav_cb_f tdnops[] = {
1170Sstevel@tonic-gate NULL,
1180Sstevel@tonic-gate NULL, /* intrinsic */
1190Sstevel@tonic-gate NULL, /* pointer */
1200Sstevel@tonic-gate NULL, /* array */
1210Sstevel@tonic-gate NULL, /* function */
1220Sstevel@tonic-gate NULL, /* struct */
1230Sstevel@tonic-gate NULL, /* union */
1240Sstevel@tonic-gate NULL, /* enum */
1250Sstevel@tonic-gate NULL, /* forward */
1260Sstevel@tonic-gate NULL, /* typedef */
1270Sstevel@tonic-gate NULL, /* typedef_unres */
1280Sstevel@tonic-gate NULL, /* volatile */
1290Sstevel@tonic-gate NULL, /* const */
1300Sstevel@tonic-gate NULL /* restrict */
1310Sstevel@tonic-gate };
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate int (*tddescenders[])(tdesc_t *, tdtrav_data_t *) = {
1340Sstevel@tonic-gate NULL,
1350Sstevel@tonic-gate NULL, /* intrinsic */
1360Sstevel@tonic-gate tdtrav_plain, /* pointer */
1370Sstevel@tonic-gate tdtrav_array, /* array */
1380Sstevel@tonic-gate tdtrav_func, /* function */
1390Sstevel@tonic-gate tdtrav_su, /* struct */
1400Sstevel@tonic-gate tdtrav_su, /* union */
1410Sstevel@tonic-gate NULL, /* enum */
1420Sstevel@tonic-gate NULL, /* forward */
1430Sstevel@tonic-gate tdtrav_plain, /* typedef */
1440Sstevel@tonic-gate NULL, /* typedef_unres */
1450Sstevel@tonic-gate tdtrav_plain, /* volatile */
1460Sstevel@tonic-gate tdtrav_plain, /* const */
1470Sstevel@tonic-gate tdtrav_plain /* restrict */
1480Sstevel@tonic-gate };
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate int
tdtraverse(tdesc_t * this,tdesc_t ** thisp,tdtrav_data_t * tdtd)1510Sstevel@tonic-gate tdtraverse(tdesc_t *this, tdesc_t **thisp, tdtrav_data_t *tdtd)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate tdtrav_cb_f travcb;
1540Sstevel@tonic-gate int (*descender)();
1550Sstevel@tonic-gate int descend = 1;
1560Sstevel@tonic-gate int rc;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate if ((travcb = tdtd->firstops[this->t_type]) != NULL) {
1590Sstevel@tonic-gate if ((rc = travcb(this, thisp, tdtd->private)) < 0)
1600Sstevel@tonic-gate return (rc);
1610Sstevel@tonic-gate else if (rc == 0)
1620Sstevel@tonic-gate descend = 0;
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate if (this->t_vgen == tdtd->vgen)
1660Sstevel@tonic-gate return (1);
1670Sstevel@tonic-gate this->t_vgen = tdtd->vgen;
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate if (descend && (travcb = tdtd->preops[this->t_type]) != NULL) {
1700Sstevel@tonic-gate if ((rc = travcb(this, thisp, tdtd->private)) < 0)
1710Sstevel@tonic-gate return (rc);
1720Sstevel@tonic-gate else if (rc == 0)
1730Sstevel@tonic-gate descend = 0;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate if (descend) {
1770Sstevel@tonic-gate if ((descender = tddescenders[this->t_type]) != NULL &&
1780Sstevel@tonic-gate (rc = descender(this, tdtd)) < 0)
1790Sstevel@tonic-gate return (rc);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate if ((travcb = tdtd->postops[this->t_type]) != NULL &&
1820Sstevel@tonic-gate (rc = travcb(this, thisp, tdtd->private)) < 0)
1830Sstevel@tonic-gate return (rc);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate return (1);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate int
iitraverse_td(iidesc_t * ii,tdtrav_data_t * tdtd)1900Sstevel@tonic-gate iitraverse_td(iidesc_t *ii, tdtrav_data_t *tdtd)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate int i, rc;
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate if ((rc = tdtraverse(ii->ii_dtype, &ii->ii_dtype, tdtd)) < 0)
1950Sstevel@tonic-gate return (rc);
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate for (i = 0; i < ii->ii_nargs; i++) {
1980Sstevel@tonic-gate if ((rc = tdtraverse(ii->ii_args[i], &ii->ii_args[i],
1990Sstevel@tonic-gate tdtd)) < 0)
2000Sstevel@tonic-gate return (rc);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate return (1);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate int
iitraverse(iidesc_t * ii,int * vgenp,tdtrav_cb_f * firstops,tdtrav_cb_f * preops,tdtrav_cb_f * postops,void * private)2070Sstevel@tonic-gate iitraverse(iidesc_t *ii, int *vgenp, tdtrav_cb_f *firstops, tdtrav_cb_f *preops,
2080Sstevel@tonic-gate tdtrav_cb_f *postops, void *private)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate tdtrav_data_t tdtd;
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate tdtrav_init(&tdtd, vgenp, firstops, preops, postops, private);
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate return (iitraverse_td(ii, &tdtd));
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate int
iitraverse_hash(hash_t * iihash,int * vgenp,tdtrav_cb_f * firstops,tdtrav_cb_f * preops,tdtrav_cb_f * postops,void * private)2180Sstevel@tonic-gate iitraverse_hash(hash_t *iihash, int *vgenp, tdtrav_cb_f *firstops,
2190Sstevel@tonic-gate tdtrav_cb_f *preops, tdtrav_cb_f *postops, void *private)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate tdtrav_data_t tdtd;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate tdtrav_init(&tdtd, vgenp, firstops, preops, postops, private);
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate return (hash_iter(iihash, (int (*)())iitraverse_td, &tdtd));
2260Sstevel@tonic-gate }
227