11673e404SJohn Birrell /*
21673e404SJohn Birrell * CDDL HEADER START
31673e404SJohn Birrell *
41673e404SJohn Birrell * The contents of this file are subject to the terms of the
51673e404SJohn Birrell * Common Development and Distribution License (the "License").
61673e404SJohn Birrell * You may not use this file except in compliance with the License.
71673e404SJohn Birrell *
81673e404SJohn Birrell * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91673e404SJohn Birrell * or http://www.opensolaris.org/os/licensing.
101673e404SJohn Birrell * See the License for the specific language governing permissions
111673e404SJohn Birrell * and limitations under the License.
121673e404SJohn Birrell *
131673e404SJohn Birrell * When distributing Covered Code, include this CDDL HEADER in each
141673e404SJohn Birrell * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151673e404SJohn Birrell * If applicable, add the following below this CDDL HEADER, with the
161673e404SJohn Birrell * fields enclosed by brackets "[]" replaced with your own identifying
171673e404SJohn Birrell * information: Portions Copyright [yyyy] [name of copyright owner]
181673e404SJohn Birrell *
191673e404SJohn Birrell * CDDL HEADER END
201673e404SJohn Birrell */
211673e404SJohn Birrell /*
221673e404SJohn Birrell * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
231673e404SJohn Birrell * Use is subject to license terms.
241673e404SJohn Birrell */
251673e404SJohn Birrell
261673e404SJohn Birrell #pragma ident "%Z%%M% %I% %E% SMI"
271673e404SJohn Birrell
281673e404SJohn Birrell /*
291673e404SJohn Birrell * Routines used to traverse tdesc trees, invoking user-supplied callbacks
301673e404SJohn Birrell * as the tree is traversed.
311673e404SJohn Birrell */
321673e404SJohn Birrell
331673e404SJohn Birrell #include <stdio.h>
341673e404SJohn Birrell #include <assert.h>
351673e404SJohn Birrell
361673e404SJohn Birrell #include "ctftools.h"
371673e404SJohn Birrell #include "traverse.h"
381673e404SJohn Birrell #include "memory.h"
391673e404SJohn Birrell
40*c0794a84SEd Schouten static int (*tddescenders[])(tdesc_t *, tdtrav_data_t *);
41*c0794a84SEd Schouten static tdtrav_cb_f tdnops[];
421673e404SJohn Birrell
431673e404SJohn Birrell void
tdtrav_init(tdtrav_data_t * tdtd,int * vgenp,tdtrav_cb_f * firstops,tdtrav_cb_f * preops,tdtrav_cb_f * postops,void * private)441673e404SJohn Birrell tdtrav_init(tdtrav_data_t *tdtd, int *vgenp, tdtrav_cb_f *firstops,
451673e404SJohn Birrell tdtrav_cb_f *preops, tdtrav_cb_f *postops, void *private)
461673e404SJohn Birrell {
471673e404SJohn Birrell tdtd->vgen = ++(*vgenp);
481673e404SJohn Birrell tdtd->firstops = firstops ? firstops : tdnops;
491673e404SJohn Birrell tdtd->preops = preops ? preops : tdnops;
501673e404SJohn Birrell tdtd->postops = postops ? postops : tdnops;
511673e404SJohn Birrell tdtd->private = private;
521673e404SJohn Birrell }
531673e404SJohn Birrell
541673e404SJohn Birrell static int
tdtrav_plain(tdesc_t * this,tdtrav_data_t * tdtd)551673e404SJohn Birrell tdtrav_plain(tdesc_t *this, tdtrav_data_t *tdtd)
561673e404SJohn Birrell {
571673e404SJohn Birrell return (tdtraverse(this->t_tdesc, &this->t_tdesc, tdtd));
581673e404SJohn Birrell }
591673e404SJohn Birrell
601673e404SJohn Birrell static int
tdtrav_func(tdesc_t * this,tdtrav_data_t * tdtd)611673e404SJohn Birrell tdtrav_func(tdesc_t *this, tdtrav_data_t *tdtd)
621673e404SJohn Birrell {
631673e404SJohn Birrell fndef_t *fn = this->t_fndef;
641673e404SJohn Birrell int i, rc;
651673e404SJohn Birrell
661673e404SJohn Birrell if ((rc = tdtraverse(fn->fn_ret, &fn->fn_ret, tdtd)) < 0)
671673e404SJohn Birrell return (rc);
681673e404SJohn Birrell
694cc75139SJohn Birrell for (i = 0; i < (int) fn->fn_nargs; i++) {
701673e404SJohn Birrell if ((rc = tdtraverse(fn->fn_args[i], &fn->fn_args[i],
711673e404SJohn Birrell tdtd)) < 0)
721673e404SJohn Birrell return (rc);
731673e404SJohn Birrell }
741673e404SJohn Birrell
751673e404SJohn Birrell return (0);
761673e404SJohn Birrell }
771673e404SJohn Birrell
781673e404SJohn Birrell static int
tdtrav_array(tdesc_t * this,tdtrav_data_t * tdtd)791673e404SJohn Birrell tdtrav_array(tdesc_t *this, tdtrav_data_t *tdtd)
801673e404SJohn Birrell {
811673e404SJohn Birrell ardef_t *ardef = this->t_ardef;
821673e404SJohn Birrell int rc;
831673e404SJohn Birrell
841673e404SJohn Birrell if ((rc = tdtraverse(ardef->ad_contents, &ardef->ad_contents,
851673e404SJohn Birrell tdtd)) < 0)
861673e404SJohn Birrell return (rc);
871673e404SJohn Birrell
881673e404SJohn Birrell return (tdtraverse(ardef->ad_idxtype, &ardef->ad_idxtype, tdtd));
891673e404SJohn Birrell }
901673e404SJohn Birrell
911673e404SJohn Birrell static int
tdtrav_su(tdesc_t * this,tdtrav_data_t * tdtd)921673e404SJohn Birrell tdtrav_su(tdesc_t *this, tdtrav_data_t *tdtd)
931673e404SJohn Birrell {
941673e404SJohn Birrell mlist_t *ml;
951673e404SJohn Birrell int rc = 0;
961673e404SJohn Birrell
971673e404SJohn Birrell for (ml = this->t_members; ml; ml = ml->ml_next) {
981673e404SJohn Birrell if ((rc = tdtraverse(ml->ml_type, &ml->ml_type, tdtd)) < 0)
991673e404SJohn Birrell return (rc);
1001673e404SJohn Birrell }
1011673e404SJohn Birrell
1021673e404SJohn Birrell return (rc);
1031673e404SJohn Birrell }
1041673e404SJohn Birrell
1051673e404SJohn Birrell /*ARGSUSED*/
1061673e404SJohn Birrell int
tdtrav_assert(tdesc_t * node __unused,tdesc_t ** nodep __unused,void * private __unused)1074cc75139SJohn Birrell tdtrav_assert(tdesc_t *node __unused, tdesc_t **nodep __unused, void *private __unused)
1081673e404SJohn Birrell {
1091673e404SJohn Birrell assert(1 == 0);
1101673e404SJohn Birrell
1111673e404SJohn Birrell return (-1);
1121673e404SJohn Birrell }
1131673e404SJohn Birrell
114*c0794a84SEd Schouten static tdtrav_cb_f tdnops[] = {
1151673e404SJohn Birrell NULL,
1161673e404SJohn Birrell NULL, /* intrinsic */
1171673e404SJohn Birrell NULL, /* pointer */
1181673e404SJohn Birrell NULL, /* array */
1191673e404SJohn Birrell NULL, /* function */
1201673e404SJohn Birrell NULL, /* struct */
1211673e404SJohn Birrell NULL, /* union */
1221673e404SJohn Birrell NULL, /* enum */
1231673e404SJohn Birrell NULL, /* forward */
1241673e404SJohn Birrell NULL, /* typedef */
1251673e404SJohn Birrell NULL, /* typedef_unres */
1261673e404SJohn Birrell NULL, /* volatile */
1271673e404SJohn Birrell NULL, /* const */
1281673e404SJohn Birrell NULL /* restrict */
1291673e404SJohn Birrell };
1301673e404SJohn Birrell
131*c0794a84SEd Schouten static int (*tddescenders[])(tdesc_t *, tdtrav_data_t *) = {
1321673e404SJohn Birrell NULL,
1331673e404SJohn Birrell NULL, /* intrinsic */
1341673e404SJohn Birrell tdtrav_plain, /* pointer */
1351673e404SJohn Birrell tdtrav_array, /* array */
1361673e404SJohn Birrell tdtrav_func, /* function */
1371673e404SJohn Birrell tdtrav_su, /* struct */
1381673e404SJohn Birrell tdtrav_su, /* union */
1391673e404SJohn Birrell NULL, /* enum */
1401673e404SJohn Birrell NULL, /* forward */
1411673e404SJohn Birrell tdtrav_plain, /* typedef */
1421673e404SJohn Birrell NULL, /* typedef_unres */
1431673e404SJohn Birrell tdtrav_plain, /* volatile */
1441673e404SJohn Birrell tdtrav_plain, /* const */
1451673e404SJohn Birrell tdtrav_plain /* restrict */
1461673e404SJohn Birrell };
1471673e404SJohn Birrell
1481673e404SJohn Birrell int
tdtraverse(tdesc_t * this,tdesc_t ** thisp,tdtrav_data_t * tdtd)1491673e404SJohn Birrell tdtraverse(tdesc_t *this, tdesc_t **thisp, tdtrav_data_t *tdtd)
1501673e404SJohn Birrell {
1511673e404SJohn Birrell tdtrav_cb_f travcb;
1524cc75139SJohn Birrell int (*descender)(tdesc_t *, tdtrav_data_t *);
1531673e404SJohn Birrell int descend = 1;
1541673e404SJohn Birrell int rc;
1551673e404SJohn Birrell
1561673e404SJohn Birrell if ((travcb = tdtd->firstops[this->t_type]) != NULL) {
1571673e404SJohn Birrell if ((rc = travcb(this, thisp, tdtd->private)) < 0)
1581673e404SJohn Birrell return (rc);
1591673e404SJohn Birrell else if (rc == 0)
1601673e404SJohn Birrell descend = 0;
1611673e404SJohn Birrell }
1621673e404SJohn Birrell
1631673e404SJohn Birrell if (this->t_vgen == tdtd->vgen)
1641673e404SJohn Birrell return (1);
1651673e404SJohn Birrell this->t_vgen = tdtd->vgen;
1661673e404SJohn Birrell
1671673e404SJohn Birrell if (descend && (travcb = tdtd->preops[this->t_type]) != NULL) {
1681673e404SJohn Birrell if ((rc = travcb(this, thisp, tdtd->private)) < 0)
1691673e404SJohn Birrell return (rc);
1701673e404SJohn Birrell else if (rc == 0)
1711673e404SJohn Birrell descend = 0;
1721673e404SJohn Birrell }
1731673e404SJohn Birrell
1741673e404SJohn Birrell if (descend) {
1751673e404SJohn Birrell if ((descender = tddescenders[this->t_type]) != NULL &&
1761673e404SJohn Birrell (rc = descender(this, tdtd)) < 0)
1771673e404SJohn Birrell return (rc);
1781673e404SJohn Birrell
1791673e404SJohn Birrell if ((travcb = tdtd->postops[this->t_type]) != NULL &&
1801673e404SJohn Birrell (rc = travcb(this, thisp, tdtd->private)) < 0)
1811673e404SJohn Birrell return (rc);
1821673e404SJohn Birrell }
1831673e404SJohn Birrell
1841673e404SJohn Birrell return (1);
1851673e404SJohn Birrell }
1861673e404SJohn Birrell
1871673e404SJohn Birrell int
iitraverse_td(void * arg1,void * arg2)1884cc75139SJohn Birrell iitraverse_td(void *arg1, void *arg2)
1891673e404SJohn Birrell {
1904cc75139SJohn Birrell iidesc_t *ii = arg1;
1914cc75139SJohn Birrell tdtrav_data_t *tdtd = arg2;
1921673e404SJohn Birrell int i, rc;
1931673e404SJohn Birrell
1941673e404SJohn Birrell if ((rc = tdtraverse(ii->ii_dtype, &ii->ii_dtype, tdtd)) < 0)
1951673e404SJohn Birrell return (rc);
1961673e404SJohn Birrell
1971673e404SJohn Birrell for (i = 0; i < ii->ii_nargs; i++) {
1981673e404SJohn Birrell if ((rc = tdtraverse(ii->ii_args[i], &ii->ii_args[i],
1991673e404SJohn Birrell tdtd)) < 0)
2001673e404SJohn Birrell return (rc);
2011673e404SJohn Birrell }
2021673e404SJohn Birrell
2031673e404SJohn Birrell return (1);
2041673e404SJohn Birrell }
2051673e404SJohn Birrell
2061673e404SJohn Birrell int
iitraverse(iidesc_t * ii,int * vgenp,tdtrav_cb_f * firstops,tdtrav_cb_f * preops,tdtrav_cb_f * postops,void * private)2071673e404SJohn Birrell iitraverse(iidesc_t *ii, int *vgenp, tdtrav_cb_f *firstops, tdtrav_cb_f *preops,
2081673e404SJohn Birrell tdtrav_cb_f *postops, void *private)
2091673e404SJohn Birrell {
2101673e404SJohn Birrell tdtrav_data_t tdtd;
2111673e404SJohn Birrell
2121673e404SJohn Birrell tdtrav_init(&tdtd, vgenp, firstops, preops, postops, private);
2131673e404SJohn Birrell
2141673e404SJohn Birrell return (iitraverse_td(ii, &tdtd));
2151673e404SJohn Birrell }
2161673e404SJohn Birrell
2171673e404SJohn Birrell int
iitraverse_hash(hash_t * iihash,int * vgenp,tdtrav_cb_f * firstops,tdtrav_cb_f * preops,tdtrav_cb_f * postops,void * private)2181673e404SJohn Birrell iitraverse_hash(hash_t *iihash, int *vgenp, tdtrav_cb_f *firstops,
2191673e404SJohn Birrell tdtrav_cb_f *preops, tdtrav_cb_f *postops, void *private)
2201673e404SJohn Birrell {
2211673e404SJohn Birrell tdtrav_data_t tdtd;
2221673e404SJohn Birrell
2231673e404SJohn Birrell tdtrav_init(&tdtd, vgenp, firstops, preops, postops, private);
2241673e404SJohn Birrell
2254cc75139SJohn Birrell return (hash_iter(iihash, iitraverse_td, &tdtd));
2261673e404SJohn Birrell }
227