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