xref: /titanic_52/usr/src/common/ctf/ctf_decl.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 2005 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  * CTF Declaration Stack
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * In order to implement ctf_type_name(), we must convert a type graph back
33*7c478bd9Sstevel@tonic-gate  * into a C type declaration.  Unfortunately, a type graph represents a storage
34*7c478bd9Sstevel@tonic-gate  * class ordering of the type whereas a type declaration must obey the C rules
35*7c478bd9Sstevel@tonic-gate  * for operator precedence, and the two orderings are frequently in conflict.
36*7c478bd9Sstevel@tonic-gate  * For example, consider these CTF type graphs and their C declarations:
37*7c478bd9Sstevel@tonic-gate  *
38*7c478bd9Sstevel@tonic-gate  * CTF_K_POINTER -> CTF_K_FUNCTION -> CTF_K_INTEGER  : int (*)()
39*7c478bd9Sstevel@tonic-gate  * CTF_K_POINTER -> CTF_K_ARRAY -> CTF_K_INTEGER     : int (*)[]
40*7c478bd9Sstevel@tonic-gate  *
41*7c478bd9Sstevel@tonic-gate  * In each case, parentheses are used to raise operator * to higher lexical
42*7c478bd9Sstevel@tonic-gate  * precedence, so the string form of the C declaration cannot be constructed by
43*7c478bd9Sstevel@tonic-gate  * walking the type graph links and forming the string from left to right.
44*7c478bd9Sstevel@tonic-gate  *
45*7c478bd9Sstevel@tonic-gate  * The functions in this file build a set of stacks from the type graph nodes
46*7c478bd9Sstevel@tonic-gate  * corresponding to the C operator precedence levels in the appropriate order.
47*7c478bd9Sstevel@tonic-gate  * The code in ctf_type_name() can then iterate over the levels and nodes in
48*7c478bd9Sstevel@tonic-gate  * lexical precedence order and construct the final C declaration string.
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #include <ctf_impl.h>
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate void
54*7c478bd9Sstevel@tonic-gate ctf_decl_init(ctf_decl_t *cd, char *buf, size_t len)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	int i;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	bzero(cd, sizeof (ctf_decl_t));
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++)
61*7c478bd9Sstevel@tonic-gate 		cd->cd_order[i] = CTF_PREC_BASE - 1;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	cd->cd_qualp = CTF_PREC_BASE;
64*7c478bd9Sstevel@tonic-gate 	cd->cd_ordp = CTF_PREC_BASE;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	cd->cd_buf = buf;
67*7c478bd9Sstevel@tonic-gate 	cd->cd_ptr = buf;
68*7c478bd9Sstevel@tonic-gate 	cd->cd_end = buf + len;
69*7c478bd9Sstevel@tonic-gate }
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate void
72*7c478bd9Sstevel@tonic-gate ctf_decl_fini(ctf_decl_t *cd)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	ctf_decl_node_t *cdp, *ndp;
75*7c478bd9Sstevel@tonic-gate 	int i;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	for (i = CTF_PREC_BASE; i < CTF_PREC_MAX; i++) {
78*7c478bd9Sstevel@tonic-gate 		for (cdp = ctf_list_next(&cd->cd_nodes[i]);
79*7c478bd9Sstevel@tonic-gate 		    cdp != NULL; cdp = ndp) {
80*7c478bd9Sstevel@tonic-gate 			ndp = ctf_list_next(cdp);
81*7c478bd9Sstevel@tonic-gate 			ctf_free(cdp, sizeof (ctf_decl_node_t));
82*7c478bd9Sstevel@tonic-gate 		}
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate void
87*7c478bd9Sstevel@tonic-gate ctf_decl_push(ctf_decl_t *cd, ctf_file_t *fp, ctf_id_t type)
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	ctf_decl_node_t *cdp;
90*7c478bd9Sstevel@tonic-gate 	ctf_decl_prec_t prec;
91*7c478bd9Sstevel@tonic-gate 	uint_t kind, n = 1;
92*7c478bd9Sstevel@tonic-gate 	int is_qual = 0;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	const ctf_type_t *tp;
95*7c478bd9Sstevel@tonic-gate 	ctf_arinfo_t ar;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	if ((tp = ctf_lookup_by_id(&fp, type)) == NULL) {
98*7c478bd9Sstevel@tonic-gate 		cd->cd_err = fp->ctf_errno;
99*7c478bd9Sstevel@tonic-gate 		return;
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	switch (kind = LCTF_INFO_KIND(fp, tp->ctt_info)) {
103*7c478bd9Sstevel@tonic-gate 	case CTF_K_ARRAY:
104*7c478bd9Sstevel@tonic-gate 		(void) ctf_array_info(fp, type, &ar);
105*7c478bd9Sstevel@tonic-gate 		ctf_decl_push(cd, fp, ar.ctr_contents);
106*7c478bd9Sstevel@tonic-gate 		n = ar.ctr_nelems;
107*7c478bd9Sstevel@tonic-gate 		prec = CTF_PREC_ARRAY;
108*7c478bd9Sstevel@tonic-gate 		break;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	case CTF_K_TYPEDEF:
111*7c478bd9Sstevel@tonic-gate 		if (ctf_strptr(fp, tp->ctt_name)[0] == '\0') {
112*7c478bd9Sstevel@tonic-gate 			ctf_decl_push(cd, fp, tp->ctt_type);
113*7c478bd9Sstevel@tonic-gate 			return;
114*7c478bd9Sstevel@tonic-gate 		}
115*7c478bd9Sstevel@tonic-gate 		prec = CTF_PREC_BASE;
116*7c478bd9Sstevel@tonic-gate 		break;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	case CTF_K_FUNCTION:
119*7c478bd9Sstevel@tonic-gate 		ctf_decl_push(cd, fp, tp->ctt_type);
120*7c478bd9Sstevel@tonic-gate 		prec = CTF_PREC_FUNCTION;
121*7c478bd9Sstevel@tonic-gate 		break;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	case CTF_K_POINTER:
124*7c478bd9Sstevel@tonic-gate 		ctf_decl_push(cd, fp, tp->ctt_type);
125*7c478bd9Sstevel@tonic-gate 		prec = CTF_PREC_POINTER;
126*7c478bd9Sstevel@tonic-gate 		break;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	case CTF_K_VOLATILE:
129*7c478bd9Sstevel@tonic-gate 	case CTF_K_CONST:
130*7c478bd9Sstevel@tonic-gate 	case CTF_K_RESTRICT:
131*7c478bd9Sstevel@tonic-gate 		ctf_decl_push(cd, fp, tp->ctt_type);
132*7c478bd9Sstevel@tonic-gate 		prec = cd->cd_qualp;
133*7c478bd9Sstevel@tonic-gate 		is_qual++;
134*7c478bd9Sstevel@tonic-gate 		break;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	default:
137*7c478bd9Sstevel@tonic-gate 		prec = CTF_PREC_BASE;
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	if ((cdp = ctf_alloc(sizeof (ctf_decl_node_t))) == NULL) {
141*7c478bd9Sstevel@tonic-gate 		cd->cd_err = EAGAIN;
142*7c478bd9Sstevel@tonic-gate 		return;
143*7c478bd9Sstevel@tonic-gate 	}
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	cdp->cd_type = type;
146*7c478bd9Sstevel@tonic-gate 	cdp->cd_kind = kind;
147*7c478bd9Sstevel@tonic-gate 	cdp->cd_n = n;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	if (ctf_list_next(&cd->cd_nodes[prec]) == NULL)
150*7c478bd9Sstevel@tonic-gate 		cd->cd_order[prec] = cd->cd_ordp++;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	/*
153*7c478bd9Sstevel@tonic-gate 	 * Reset cd_qualp to the highest precedence level that we've seen so
154*7c478bd9Sstevel@tonic-gate 	 * far that can be qualified (CTF_PREC_BASE or CTF_PREC_POINTER).
155*7c478bd9Sstevel@tonic-gate 	 */
156*7c478bd9Sstevel@tonic-gate 	if (prec > cd->cd_qualp && prec < CTF_PREC_ARRAY)
157*7c478bd9Sstevel@tonic-gate 		cd->cd_qualp = prec;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	/*
160*7c478bd9Sstevel@tonic-gate 	 * C array declarators are ordered inside out so prepend them.  Also by
161*7c478bd9Sstevel@tonic-gate 	 * convention qualifiers of base types precede the type specifier (e.g.
162*7c478bd9Sstevel@tonic-gate 	 * const int vs. int const) even though the two forms are equivalent.
163*7c478bd9Sstevel@tonic-gate 	 */
164*7c478bd9Sstevel@tonic-gate 	if (kind == CTF_K_ARRAY || (is_qual && prec == CTF_PREC_BASE))
165*7c478bd9Sstevel@tonic-gate 		ctf_list_prepend(&cd->cd_nodes[prec], cdp);
166*7c478bd9Sstevel@tonic-gate 	else
167*7c478bd9Sstevel@tonic-gate 		ctf_list_append(&cd->cd_nodes[prec], cdp);
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
171*7c478bd9Sstevel@tonic-gate void
172*7c478bd9Sstevel@tonic-gate ctf_decl_sprintf(ctf_decl_t *cd, const char *format, ...)
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	size_t len = (size_t)(cd->cd_end - cd->cd_ptr);
175*7c478bd9Sstevel@tonic-gate 	va_list ap;
176*7c478bd9Sstevel@tonic-gate 	size_t n;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	va_start(ap, format);
179*7c478bd9Sstevel@tonic-gate 	n = vsnprintf(cd->cd_ptr, len, format, ap);
180*7c478bd9Sstevel@tonic-gate 	va_end(ap);
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	cd->cd_ptr += MIN(n, len);
183*7c478bd9Sstevel@tonic-gate 	cd->cd_len += n;
184*7c478bd9Sstevel@tonic-gate }
185