17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*4d232658Sjohnlev * Common Development and Distribution License (the "License").
6*4d232658Sjohnlev * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*4d232658Sjohnlev * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * Routines used to traverse tdesc trees, invoking user-supplied callbacks
307c478bd9Sstevel@tonic-gate * as the tree is traversed.
317c478bd9Sstevel@tonic-gate */
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <assert.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include "ctftools.h"
377c478bd9Sstevel@tonic-gate #include "traverse.h"
387c478bd9Sstevel@tonic-gate #include "memory.h"
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate int (*tddescenders[])();
417c478bd9Sstevel@tonic-gate int (*tdnops[])();
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate int tdtraverse(tdesc_t *, tdesc_t **, tdtrav_data_t *);
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@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)467c478bd9Sstevel@tonic-gate tdtrav_init(tdtrav_data_t *tdtd, int *vgenp, tdtrav_cb_f *firstops,
477c478bd9Sstevel@tonic-gate tdtrav_cb_f *preops, tdtrav_cb_f *postops, void *private)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate tdtd->vgen = ++(*vgenp);
507c478bd9Sstevel@tonic-gate tdtd->firstops = firstops ? firstops : tdnops;
517c478bd9Sstevel@tonic-gate tdtd->preops = preops ? preops : tdnops;
527c478bd9Sstevel@tonic-gate tdtd->postops = postops ? postops : tdnops;
537c478bd9Sstevel@tonic-gate tdtd->private = private;
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate static int
tdtrav_plain(tdesc_t * this,tdtrav_data_t * tdtd)577c478bd9Sstevel@tonic-gate tdtrav_plain(tdesc_t *this, tdtrav_data_t *tdtd)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate return (tdtraverse(this->t_tdesc, &this->t_tdesc, tdtd));
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate static int
tdtrav_func(tdesc_t * this,tdtrav_data_t * tdtd)637c478bd9Sstevel@tonic-gate tdtrav_func(tdesc_t *this, tdtrav_data_t *tdtd)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate fndef_t *fn = this->t_fndef;
667c478bd9Sstevel@tonic-gate int i, rc;
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate if ((rc = tdtraverse(fn->fn_ret, &fn->fn_ret, tdtd)) < 0)
697c478bd9Sstevel@tonic-gate return (rc);
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate for (i = 0; i < fn->fn_nargs; i++) {
727c478bd9Sstevel@tonic-gate if ((rc = tdtraverse(fn->fn_args[i], &fn->fn_args[i],
737c478bd9Sstevel@tonic-gate tdtd)) < 0)
747c478bd9Sstevel@tonic-gate return (rc);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate return (0);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate static int
tdtrav_array(tdesc_t * this,tdtrav_data_t * tdtd)817c478bd9Sstevel@tonic-gate tdtrav_array(tdesc_t *this, tdtrav_data_t *tdtd)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate ardef_t *ardef = this->t_ardef;
847c478bd9Sstevel@tonic-gate int rc;
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate if ((rc = tdtraverse(ardef->ad_contents, &ardef->ad_contents,
877c478bd9Sstevel@tonic-gate tdtd)) < 0)
887c478bd9Sstevel@tonic-gate return (rc);
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate return (tdtraverse(ardef->ad_idxtype, &ardef->ad_idxtype, tdtd));
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate static int
tdtrav_su(tdesc_t * this,tdtrav_data_t * tdtd)947c478bd9Sstevel@tonic-gate tdtrav_su(tdesc_t *this, tdtrav_data_t *tdtd)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate mlist_t *ml;
97*4d232658Sjohnlev int rc = 0;
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate for (ml = this->t_members; ml; ml = ml->ml_next) {
1007c478bd9Sstevel@tonic-gate if ((rc = tdtraverse(ml->ml_type, &ml->ml_type, tdtd)) < 0)
1017c478bd9Sstevel@tonic-gate return (rc);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate return (rc);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1087c478bd9Sstevel@tonic-gate int
tdtrav_assert(tdesc_t * node,tdesc_t ** nodep,void * private)1097c478bd9Sstevel@tonic-gate tdtrav_assert(tdesc_t *node, tdesc_t **nodep, void *private)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate assert(1 == 0);
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate return (-1);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate tdtrav_cb_f tdnops[] = {
1177c478bd9Sstevel@tonic-gate NULL,
1187c478bd9Sstevel@tonic-gate NULL, /* intrinsic */
1197c478bd9Sstevel@tonic-gate NULL, /* pointer */
1207c478bd9Sstevel@tonic-gate NULL, /* array */
1217c478bd9Sstevel@tonic-gate NULL, /* function */
1227c478bd9Sstevel@tonic-gate NULL, /* struct */
1237c478bd9Sstevel@tonic-gate NULL, /* union */
1247c478bd9Sstevel@tonic-gate NULL, /* enum */
1257c478bd9Sstevel@tonic-gate NULL, /* forward */
1267c478bd9Sstevel@tonic-gate NULL, /* typedef */
1277c478bd9Sstevel@tonic-gate NULL, /* typedef_unres */
1287c478bd9Sstevel@tonic-gate NULL, /* volatile */
1297c478bd9Sstevel@tonic-gate NULL, /* const */
1307c478bd9Sstevel@tonic-gate NULL /* restrict */
1317c478bd9Sstevel@tonic-gate };
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate int (*tddescenders[])(tdesc_t *, tdtrav_data_t *) = {
1347c478bd9Sstevel@tonic-gate NULL,
1357c478bd9Sstevel@tonic-gate NULL, /* intrinsic */
1367c478bd9Sstevel@tonic-gate tdtrav_plain, /* pointer */
1377c478bd9Sstevel@tonic-gate tdtrav_array, /* array */
1387c478bd9Sstevel@tonic-gate tdtrav_func, /* function */
1397c478bd9Sstevel@tonic-gate tdtrav_su, /* struct */
1407c478bd9Sstevel@tonic-gate tdtrav_su, /* union */
1417c478bd9Sstevel@tonic-gate NULL, /* enum */
1427c478bd9Sstevel@tonic-gate NULL, /* forward */
1437c478bd9Sstevel@tonic-gate tdtrav_plain, /* typedef */
1447c478bd9Sstevel@tonic-gate NULL, /* typedef_unres */
1457c478bd9Sstevel@tonic-gate tdtrav_plain, /* volatile */
1467c478bd9Sstevel@tonic-gate tdtrav_plain, /* const */
1477c478bd9Sstevel@tonic-gate tdtrav_plain /* restrict */
1487c478bd9Sstevel@tonic-gate };
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate int
tdtraverse(tdesc_t * this,tdesc_t ** thisp,tdtrav_data_t * tdtd)1517c478bd9Sstevel@tonic-gate tdtraverse(tdesc_t *this, tdesc_t **thisp, tdtrav_data_t *tdtd)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate tdtrav_cb_f travcb;
1547c478bd9Sstevel@tonic-gate int (*descender)();
1557c478bd9Sstevel@tonic-gate int descend = 1;
1567c478bd9Sstevel@tonic-gate int rc;
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate if ((travcb = tdtd->firstops[this->t_type]) != NULL) {
1597c478bd9Sstevel@tonic-gate if ((rc = travcb(this, thisp, tdtd->private)) < 0)
1607c478bd9Sstevel@tonic-gate return (rc);
1617c478bd9Sstevel@tonic-gate else if (rc == 0)
1627c478bd9Sstevel@tonic-gate descend = 0;
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate if (this->t_vgen == tdtd->vgen)
1667c478bd9Sstevel@tonic-gate return (1);
1677c478bd9Sstevel@tonic-gate this->t_vgen = tdtd->vgen;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate if (descend && (travcb = tdtd->preops[this->t_type]) != NULL) {
1707c478bd9Sstevel@tonic-gate if ((rc = travcb(this, thisp, tdtd->private)) < 0)
1717c478bd9Sstevel@tonic-gate return (rc);
1727c478bd9Sstevel@tonic-gate else if (rc == 0)
1737c478bd9Sstevel@tonic-gate descend = 0;
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate if (descend) {
1777c478bd9Sstevel@tonic-gate if ((descender = tddescenders[this->t_type]) != NULL &&
1787c478bd9Sstevel@tonic-gate (rc = descender(this, tdtd)) < 0)
1797c478bd9Sstevel@tonic-gate return (rc);
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate if ((travcb = tdtd->postops[this->t_type]) != NULL &&
1827c478bd9Sstevel@tonic-gate (rc = travcb(this, thisp, tdtd->private)) < 0)
1837c478bd9Sstevel@tonic-gate return (rc);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate return (1);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate int
iitraverse_td(iidesc_t * ii,tdtrav_data_t * tdtd)1907c478bd9Sstevel@tonic-gate iitraverse_td(iidesc_t *ii, tdtrav_data_t *tdtd)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate int i, rc;
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate if ((rc = tdtraverse(ii->ii_dtype, &ii->ii_dtype, tdtd)) < 0)
1957c478bd9Sstevel@tonic-gate return (rc);
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate for (i = 0; i < ii->ii_nargs; i++) {
1987c478bd9Sstevel@tonic-gate if ((rc = tdtraverse(ii->ii_args[i], &ii->ii_args[i],
1997c478bd9Sstevel@tonic-gate tdtd)) < 0)
2007c478bd9Sstevel@tonic-gate return (rc);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate return (1);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate int
iitraverse(iidesc_t * ii,int * vgenp,tdtrav_cb_f * firstops,tdtrav_cb_f * preops,tdtrav_cb_f * postops,void * private)2077c478bd9Sstevel@tonic-gate iitraverse(iidesc_t *ii, int *vgenp, tdtrav_cb_f *firstops, tdtrav_cb_f *preops,
2087c478bd9Sstevel@tonic-gate tdtrav_cb_f *postops, void *private)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate tdtrav_data_t tdtd;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate tdtrav_init(&tdtd, vgenp, firstops, preops, postops, private);
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate return (iitraverse_td(ii, &tdtd));
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate int
iitraverse_hash(hash_t * iihash,int * vgenp,tdtrav_cb_f * firstops,tdtrav_cb_f * preops,tdtrav_cb_f * postops,void * private)2187c478bd9Sstevel@tonic-gate iitraverse_hash(hash_t *iihash, int *vgenp, tdtrav_cb_f *firstops,
2197c478bd9Sstevel@tonic-gate tdtrav_cb_f *preops, tdtrav_cb_f *postops, void *private)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate tdtrav_data_t tdtd;
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate tdtrav_init(&tdtd, vgenp, firstops, preops, postops, private);
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate return (hash_iter(iihash, (int (*)())iitraverse_td, &tdtd));
2267c478bd9Sstevel@tonic-gate }
227