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