1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * CTF Declaration Stack
31*0Sstevel@tonic-gate *
32*0Sstevel@tonic-gate * In order to implement ctf_type_name(), we must convert a type graph back
33*0Sstevel@tonic-gate * into a C type declaration. Unfortunately, a type graph represents a storage
34*0Sstevel@tonic-gate * class ordering of the type whereas a type declaration must obey the C rules
35*0Sstevel@tonic-gate * for operator precedence, and the two orderings are frequently in conflict.
36*0Sstevel@tonic-gate * For example, consider these CTF type graphs and their C declarations:
37*0Sstevel@tonic-gate *
38*0Sstevel@tonic-gate * CTF_K_POINTER -> CTF_K_FUNCTION -> CTF_K_INTEGER : int (*)()
39*0Sstevel@tonic-gate * CTF_K_POINTER -> CTF_K_ARRAY -> CTF_K_INTEGER : int (*)[]
40*0Sstevel@tonic-gate *
41*0Sstevel@tonic-gate * In each case, parentheses are used to raise operator * to higher lexical
42*0Sstevel@tonic-gate * precedence, so the string form of the C declaration cannot be constructed by
43*0Sstevel@tonic-gate * walking the type graph links and forming the string from left to right.
44*0Sstevel@tonic-gate *
45*0Sstevel@tonic-gate * The functions in this file build a set of stacks from the type graph nodes
46*0Sstevel@tonic-gate * corresponding to the C operator precedence levels in the appropriate order.
47*0Sstevel@tonic-gate * The code in ctf_type_name() can then iterate over the levels and nodes in
48*0Sstevel@tonic-gate * lexical precedence order and construct the final C declaration string.
49*0Sstevel@tonic-gate */
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate #include <ctf_impl.h>
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate void
ctf_decl_init(ctf_decl_t * cd,char * buf,size_t len)54*0Sstevel@tonic-gate ctf_decl_init(ctf_decl_t *cd, char *buf, size_t len)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate int i;
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate bzero(cd, sizeof (ctf_decl_t));
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++)
61*0Sstevel@tonic-gate cd->cd_order[i] = CTF_PREC_BASE - 1;
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate cd->cd_qualp = CTF_PREC_BASE;
64*0Sstevel@tonic-gate cd->cd_ordp = CTF_PREC_BASE;
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate cd->cd_buf = buf;
67*0Sstevel@tonic-gate cd->cd_ptr = buf;
68*0Sstevel@tonic-gate cd->cd_end = buf + len;
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate void
ctf_decl_fini(ctf_decl_t * cd)72*0Sstevel@tonic-gate ctf_decl_fini(ctf_decl_t *cd)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate ctf_decl_node_t *cdp, *ndp;
75*0Sstevel@tonic-gate int i;
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++) {
78*0Sstevel@tonic-gate for (cdp = ctf_list_next(&cd->cd_nodes[i]);
79*0Sstevel@tonic-gate cdp != NULL; cdp = ndp) {
80*0Sstevel@tonic-gate ndp = ctf_list_next(cdp);
81*0Sstevel@tonic-gate ctf_free(cdp, sizeof (ctf_decl_node_t));
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate void
ctf_decl_push(ctf_decl_t * cd,ctf_file_t * fp,ctf_id_t type)87*0Sstevel@tonic-gate ctf_decl_push(ctf_decl_t *cd, ctf_file_t *fp, ctf_id_t type)
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate ctf_decl_node_t *cdp;
90*0Sstevel@tonic-gate ctf_decl_prec_t prec;
91*0Sstevel@tonic-gate uint_t kind, n = 1;
92*0Sstevel@tonic-gate int is_qual = 0;
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate const ctf_type_t *tp;
95*0Sstevel@tonic-gate ctf_arinfo_t ar;
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate if ((tp = ctf_lookup_by_id(&fp, type)) == NULL) {
98*0Sstevel@tonic-gate cd->cd_err = fp->ctf_errno;
99*0Sstevel@tonic-gate return;
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate switch (kind = LCTF_INFO_KIND(fp, tp->ctt_info)) {
103*0Sstevel@tonic-gate case CTF_K_ARRAY:
104*0Sstevel@tonic-gate (void) ctf_array_info(fp, type, &ar);
105*0Sstevel@tonic-gate ctf_decl_push(cd, fp, ar.ctr_contents);
106*0Sstevel@tonic-gate n = ar.ctr_nelems;
107*0Sstevel@tonic-gate prec = CTF_PREC_ARRAY;
108*0Sstevel@tonic-gate break;
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate case CTF_K_TYPEDEF:
111*0Sstevel@tonic-gate if (ctf_strptr(fp, tp->ctt_name)[0] == '\0') {
112*0Sstevel@tonic-gate ctf_decl_push(cd, fp, tp->ctt_type);
113*0Sstevel@tonic-gate return;
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate prec = CTF_PREC_BASE;
116*0Sstevel@tonic-gate break;
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate case CTF_K_FUNCTION:
119*0Sstevel@tonic-gate ctf_decl_push(cd, fp, tp->ctt_type);
120*0Sstevel@tonic-gate prec = CTF_PREC_FUNCTION;
121*0Sstevel@tonic-gate break;
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate case CTF_K_POINTER:
124*0Sstevel@tonic-gate ctf_decl_push(cd, fp, tp->ctt_type);
125*0Sstevel@tonic-gate prec = CTF_PREC_POINTER;
126*0Sstevel@tonic-gate break;
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate case CTF_K_VOLATILE:
129*0Sstevel@tonic-gate case CTF_K_CONST:
130*0Sstevel@tonic-gate case CTF_K_RESTRICT:
131*0Sstevel@tonic-gate ctf_decl_push(cd, fp, tp->ctt_type);
132*0Sstevel@tonic-gate prec = cd->cd_qualp;
133*0Sstevel@tonic-gate is_qual++;
134*0Sstevel@tonic-gate break;
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate default:
137*0Sstevel@tonic-gate prec = CTF_PREC_BASE;
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate if ((cdp = ctf_alloc(sizeof (ctf_decl_node_t))) == NULL) {
141*0Sstevel@tonic-gate cd->cd_err = EAGAIN;
142*0Sstevel@tonic-gate return;
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gate cdp->cd_type = type;
146*0Sstevel@tonic-gate cdp->cd_kind = kind;
147*0Sstevel@tonic-gate cdp->cd_n = n;
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate if (ctf_list_next(&cd->cd_nodes[prec]) == NULL)
150*0Sstevel@tonic-gate cd->cd_order[prec] = cd->cd_ordp++;
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate /*
153*0Sstevel@tonic-gate * Reset cd_qualp to the highest precedence level that we've seen so
154*0Sstevel@tonic-gate * far that can be qualified (CTF_PREC_BASE or CTF_PREC_POINTER).
155*0Sstevel@tonic-gate */
156*0Sstevel@tonic-gate if (prec > cd->cd_qualp && prec < CTF_PREC_ARRAY)
157*0Sstevel@tonic-gate cd->cd_qualp = prec;
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate /*
160*0Sstevel@tonic-gate * C array declarators are ordered inside out so prepend them. Also by
161*0Sstevel@tonic-gate * convention qualifiers of base types precede the type specifier (e.g.
162*0Sstevel@tonic-gate * const int vs. int const) even though the two forms are equivalent.
163*0Sstevel@tonic-gate */
164*0Sstevel@tonic-gate if (kind == CTF_K_ARRAY || (is_qual && prec == CTF_PREC_BASE))
165*0Sstevel@tonic-gate ctf_list_prepend(&cd->cd_nodes[prec], cdp);
166*0Sstevel@tonic-gate else
167*0Sstevel@tonic-gate ctf_list_append(&cd->cd_nodes[prec], cdp);
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate /*PRINTFLIKE2*/
171*0Sstevel@tonic-gate void
ctf_decl_sprintf(ctf_decl_t * cd,const char * format,...)172*0Sstevel@tonic-gate ctf_decl_sprintf(ctf_decl_t *cd, const char *format, ...)
173*0Sstevel@tonic-gate {
174*0Sstevel@tonic-gate size_t len = (size_t)(cd->cd_end - cd->cd_ptr);
175*0Sstevel@tonic-gate va_list ap;
176*0Sstevel@tonic-gate size_t n;
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate va_start(ap, format);
179*0Sstevel@tonic-gate n = vsnprintf(cd->cd_ptr, len, format, ap);
180*0Sstevel@tonic-gate va_end(ap);
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate cd->cd_ptr += MIN(n, len);
183*0Sstevel@tonic-gate cd->cd_len += n;
184*0Sstevel@tonic-gate }
185