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