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