Deleted Added
full compact
dt_decl.c (249575) dt_decl.c (253726)
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.
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 * Copyright (c) 2012 by Delphix. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident "%Z%%M% %I% %E% SMI"
28
29#include <strings.h>
30#include <stdlib.h>
31#include <limits.h>
32#include <alloca.h>
33#include <assert.h>
34
35#include <dt_decl.h>
36#include <dt_parser.h>
37#include <dt_module.h>
38#include <dt_impl.h>
39
40static dt_decl_t *
41dt_decl_check(dt_decl_t *ddp)
42{
43 if (ddp->dd_kind == CTF_K_UNKNOWN)
44 return (ddp); /* nothing to check if the type is not yet set */
45
46 if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "char") == 0 &&
47 (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG))) {
48 xyerror(D_DECL_CHARATTR, "invalid type declaration: short and "
49 "long may not be used with char type\n");
50 }
51
52 if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "void") == 0 &&
53 (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG |
54 (DT_DA_SIGNED | DT_DA_UNSIGNED)))) {
55 xyerror(D_DECL_VOIDATTR, "invalid type declaration: attributes "
56 "may not be used with void type\n");
57 }
58
59 if (ddp->dd_kind != CTF_K_INTEGER &&
60 (ddp->dd_attr & (DT_DA_SIGNED | DT_DA_UNSIGNED))) {
61 xyerror(D_DECL_SIGNINT, "invalid type declaration: signed and "
62 "unsigned may only be used with integer type\n");
63 }
64
65 if (ddp->dd_kind != CTF_K_INTEGER && ddp->dd_kind != CTF_K_FLOAT &&
66 (ddp->dd_attr & (DT_DA_LONG | DT_DA_LONGLONG))) {
67 xyerror(D_DECL_LONGINT, "invalid type declaration: long and "
68 "long long may only be used with integer or "
69 "floating-point type\n");
70 }
71
72 return (ddp);
73}
74
75dt_decl_t *
76dt_decl_alloc(ushort_t kind, char *name)
77{
78 dt_decl_t *ddp = malloc(sizeof (dt_decl_t));
79
80 if (ddp == NULL)
81 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
82
83 ddp->dd_kind = kind;
84 ddp->dd_attr = 0;
85 ddp->dd_ctfp = NULL;
86 ddp->dd_type = CTF_ERR;
87 ddp->dd_name = name;
88 ddp->dd_node = NULL;
89 ddp->dd_next = NULL;
90
91 return (ddp);
92}
93
94void
95dt_decl_free(dt_decl_t *ddp)
96{
97 dt_decl_t *ndp;
98
99 for (; ddp != NULL; ddp = ndp) {
100 ndp = ddp->dd_next;
101 free(ddp->dd_name);
102 dt_node_list_free(&ddp->dd_node);
103 free(ddp);
104 }
105}
106
107void
108dt_decl_reset(void)
109{
110 dt_scope_t *dsp = &yypcb->pcb_dstack;
111 dt_decl_t *ddp = dsp->ds_decl;
112
113 while (ddp->dd_next != NULL) {
114 dsp->ds_decl = ddp->dd_next;
115 ddp->dd_next = NULL;
116 dt_decl_free(ddp);
117 ddp = dsp->ds_decl;
118 }
119}
120
121dt_decl_t *
122dt_decl_push(dt_decl_t *ddp)
123{
124 dt_scope_t *dsp = &yypcb->pcb_dstack;
125 dt_decl_t *top = dsp->ds_decl;
126
127 if (top != NULL &&
128 top->dd_kind == CTF_K_UNKNOWN && top->dd_name == NULL) {
129 top->dd_kind = CTF_K_INTEGER;
130 (void) dt_decl_check(top);
131 }
132
133 assert(ddp->dd_next == NULL);
134 ddp->dd_next = top;
135 dsp->ds_decl = ddp;
136
137 return (ddp);
138}
139
140dt_decl_t *
141dt_decl_pop(void)
142{
143 dt_scope_t *dsp = &yypcb->pcb_dstack;
144 dt_decl_t *ddp = dt_decl_top();
145
146 dsp->ds_decl = NULL;
147 free(dsp->ds_ident);
148 dsp->ds_ident = NULL;
149 dsp->ds_ctfp = NULL;
150 dsp->ds_type = CTF_ERR;
151 dsp->ds_class = DT_DC_DEFAULT;
152 dsp->ds_enumval = -1;
153
154 return (ddp);
155}
156
157dt_decl_t *
158dt_decl_pop_param(char **idp)
159{
160 dt_scope_t *dsp = &yypcb->pcb_dstack;
161
162 if (dsp->ds_class != DT_DC_DEFAULT && dsp->ds_class != DT_DC_REGISTER) {
163 xyerror(D_DECL_PARMCLASS, "inappropriate storage class "
164 "for function or associative array parameter\n");
165 }
166
167 if (idp != NULL && dt_decl_top() != NULL) {
168 *idp = dsp->ds_ident;
169 dsp->ds_ident = NULL;
170 }
171
172 return (dt_decl_pop());
173}
174
175dt_decl_t *
176dt_decl_top(void)
177{
178 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
179
180 if (ddp == NULL)
181 longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);
182
183 if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {
184 ddp->dd_kind = CTF_K_INTEGER;
185 (void) dt_decl_check(ddp);
186 }
187
188 return (ddp);
189}
190
191dt_decl_t *
192dt_decl_ident(char *name)
193{
194 dt_scope_t *dsp = &yypcb->pcb_dstack;
195 dt_decl_t *ddp = dsp->ds_decl;
196
197 if (dsp->ds_ident != NULL) {
198 free(name);
199 xyerror(D_DECL_IDENT, "old-style declaration or "
200 "incorrect type specified\n");
201 }
202
203 dsp->ds_ident = name;
204
205 if (ddp == NULL)
206 ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));
207
208 return (ddp);
209}
210
211void
212dt_decl_class(dt_dclass_t class)
213{
214 dt_scope_t *dsp = &yypcb->pcb_dstack;
215
216 if (dsp->ds_class != DT_DC_DEFAULT) {
217 xyerror(D_DECL_CLASS, "only one storage class allowed "
218 "in a declaration\n");
219 }
220
221 dsp->ds_class = class;
222}
223
224/*
225 * Set the kind and name of the current declaration. If none is allocated,
226 * make a new decl and push it on to the top of our stack. If the name or kind
227 * is already set for the current decl, then we need to fail this declaration.
228 * This can occur because too many types were given (e.g. "int int"), etc.
229 */
230dt_decl_t *
231dt_decl_spec(ushort_t kind, char *name)
232{
233 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
234
235 if (ddp == NULL)
236 return (dt_decl_push(dt_decl_alloc(kind, name)));
237
238 /*
239 * If we already have a type name specified and we see another type
240 * name, this is an error if the declaration is a typedef. If the
241 * declaration is not a typedef, then the user may be trying to declare
242 * a variable whose name has been returned by lex as a TNAME token:
243 * call dt_decl_ident() as if the grammar's IDENT rule was matched.
244 */
245 if (ddp->dd_name != NULL && kind == CTF_K_TYPEDEF) {
246 if (yypcb->pcb_dstack.ds_class != DT_DC_TYPEDEF)
247 return (dt_decl_ident(name));
248 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n", name);
249 }
250
251 if (ddp->dd_name != NULL || ddp->dd_kind != CTF_K_UNKNOWN)
252 xyerror(D_DECL_COMBO, "invalid type combination\n");
253
254 ddp->dd_kind = kind;
255 ddp->dd_name = name;
256
25 * Use is subject to license terms.
26 */
27
28#pragma ident "%Z%%M% %I% %E% SMI"
29
30#include <strings.h>
31#include <stdlib.h>
32#include <limits.h>
33#include <alloca.h>
34#include <assert.h>
35
36#include <dt_decl.h>
37#include <dt_parser.h>
38#include <dt_module.h>
39#include <dt_impl.h>
40
41static dt_decl_t *
42dt_decl_check(dt_decl_t *ddp)
43{
44 if (ddp->dd_kind == CTF_K_UNKNOWN)
45 return (ddp); /* nothing to check if the type is not yet set */
46
47 if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "char") == 0 &&
48 (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG))) {
49 xyerror(D_DECL_CHARATTR, "invalid type declaration: short and "
50 "long may not be used with char type\n");
51 }
52
53 if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "void") == 0 &&
54 (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG |
55 (DT_DA_SIGNED | DT_DA_UNSIGNED)))) {
56 xyerror(D_DECL_VOIDATTR, "invalid type declaration: attributes "
57 "may not be used with void type\n");
58 }
59
60 if (ddp->dd_kind != CTF_K_INTEGER &&
61 (ddp->dd_attr & (DT_DA_SIGNED | DT_DA_UNSIGNED))) {
62 xyerror(D_DECL_SIGNINT, "invalid type declaration: signed and "
63 "unsigned may only be used with integer type\n");
64 }
65
66 if (ddp->dd_kind != CTF_K_INTEGER && ddp->dd_kind != CTF_K_FLOAT &&
67 (ddp->dd_attr & (DT_DA_LONG | DT_DA_LONGLONG))) {
68 xyerror(D_DECL_LONGINT, "invalid type declaration: long and "
69 "long long may only be used with integer or "
70 "floating-point type\n");
71 }
72
73 return (ddp);
74}
75
76dt_decl_t *
77dt_decl_alloc(ushort_t kind, char *name)
78{
79 dt_decl_t *ddp = malloc(sizeof (dt_decl_t));
80
81 if (ddp == NULL)
82 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
83
84 ddp->dd_kind = kind;
85 ddp->dd_attr = 0;
86 ddp->dd_ctfp = NULL;
87 ddp->dd_type = CTF_ERR;
88 ddp->dd_name = name;
89 ddp->dd_node = NULL;
90 ddp->dd_next = NULL;
91
92 return (ddp);
93}
94
95void
96dt_decl_free(dt_decl_t *ddp)
97{
98 dt_decl_t *ndp;
99
100 for (; ddp != NULL; ddp = ndp) {
101 ndp = ddp->dd_next;
102 free(ddp->dd_name);
103 dt_node_list_free(&ddp->dd_node);
104 free(ddp);
105 }
106}
107
108void
109dt_decl_reset(void)
110{
111 dt_scope_t *dsp = &yypcb->pcb_dstack;
112 dt_decl_t *ddp = dsp->ds_decl;
113
114 while (ddp->dd_next != NULL) {
115 dsp->ds_decl = ddp->dd_next;
116 ddp->dd_next = NULL;
117 dt_decl_free(ddp);
118 ddp = dsp->ds_decl;
119 }
120}
121
122dt_decl_t *
123dt_decl_push(dt_decl_t *ddp)
124{
125 dt_scope_t *dsp = &yypcb->pcb_dstack;
126 dt_decl_t *top = dsp->ds_decl;
127
128 if (top != NULL &&
129 top->dd_kind == CTF_K_UNKNOWN && top->dd_name == NULL) {
130 top->dd_kind = CTF_K_INTEGER;
131 (void) dt_decl_check(top);
132 }
133
134 assert(ddp->dd_next == NULL);
135 ddp->dd_next = top;
136 dsp->ds_decl = ddp;
137
138 return (ddp);
139}
140
141dt_decl_t *
142dt_decl_pop(void)
143{
144 dt_scope_t *dsp = &yypcb->pcb_dstack;
145 dt_decl_t *ddp = dt_decl_top();
146
147 dsp->ds_decl = NULL;
148 free(dsp->ds_ident);
149 dsp->ds_ident = NULL;
150 dsp->ds_ctfp = NULL;
151 dsp->ds_type = CTF_ERR;
152 dsp->ds_class = DT_DC_DEFAULT;
153 dsp->ds_enumval = -1;
154
155 return (ddp);
156}
157
158dt_decl_t *
159dt_decl_pop_param(char **idp)
160{
161 dt_scope_t *dsp = &yypcb->pcb_dstack;
162
163 if (dsp->ds_class != DT_DC_DEFAULT && dsp->ds_class != DT_DC_REGISTER) {
164 xyerror(D_DECL_PARMCLASS, "inappropriate storage class "
165 "for function or associative array parameter\n");
166 }
167
168 if (idp != NULL && dt_decl_top() != NULL) {
169 *idp = dsp->ds_ident;
170 dsp->ds_ident = NULL;
171 }
172
173 return (dt_decl_pop());
174}
175
176dt_decl_t *
177dt_decl_top(void)
178{
179 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
180
181 if (ddp == NULL)
182 longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);
183
184 if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {
185 ddp->dd_kind = CTF_K_INTEGER;
186 (void) dt_decl_check(ddp);
187 }
188
189 return (ddp);
190}
191
192dt_decl_t *
193dt_decl_ident(char *name)
194{
195 dt_scope_t *dsp = &yypcb->pcb_dstack;
196 dt_decl_t *ddp = dsp->ds_decl;
197
198 if (dsp->ds_ident != NULL) {
199 free(name);
200 xyerror(D_DECL_IDENT, "old-style declaration or "
201 "incorrect type specified\n");
202 }
203
204 dsp->ds_ident = name;
205
206 if (ddp == NULL)
207 ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));
208
209 return (ddp);
210}
211
212void
213dt_decl_class(dt_dclass_t class)
214{
215 dt_scope_t *dsp = &yypcb->pcb_dstack;
216
217 if (dsp->ds_class != DT_DC_DEFAULT) {
218 xyerror(D_DECL_CLASS, "only one storage class allowed "
219 "in a declaration\n");
220 }
221
222 dsp->ds_class = class;
223}
224
225/*
226 * Set the kind and name of the current declaration. If none is allocated,
227 * make a new decl and push it on to the top of our stack. If the name or kind
228 * is already set for the current decl, then we need to fail this declaration.
229 * This can occur because too many types were given (e.g. "int int"), etc.
230 */
231dt_decl_t *
232dt_decl_spec(ushort_t kind, char *name)
233{
234 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
235
236 if (ddp == NULL)
237 return (dt_decl_push(dt_decl_alloc(kind, name)));
238
239 /*
240 * If we already have a type name specified and we see another type
241 * name, this is an error if the declaration is a typedef. If the
242 * declaration is not a typedef, then the user may be trying to declare
243 * a variable whose name has been returned by lex as a TNAME token:
244 * call dt_decl_ident() as if the grammar's IDENT rule was matched.
245 */
246 if (ddp->dd_name != NULL && kind == CTF_K_TYPEDEF) {
247 if (yypcb->pcb_dstack.ds_class != DT_DC_TYPEDEF)
248 return (dt_decl_ident(name));
249 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n", name);
250 }
251
252 if (ddp->dd_name != NULL || ddp->dd_kind != CTF_K_UNKNOWN)
253 xyerror(D_DECL_COMBO, "invalid type combination\n");
254
255 ddp->dd_kind = kind;
256 ddp->dd_name = name;
257
257 if (name != NULL && strchr(name, '`') != NULL) {
258 xyerror(D_DECL_SCOPE, "D scoping operator may not be used "
259 "in a type name\n");
260 }
261
262 return (dt_decl_check(ddp));
263}
264
265dt_decl_t *
266dt_decl_attr(ushort_t attr)
267{
268 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
269
270 if (ddp == NULL) {
271 ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));
272 ddp->dd_attr = attr;
273 return (ddp);
274 }
275
276 if (attr == DT_DA_LONG && (ddp->dd_attr & DT_DA_LONG)) {
277 ddp->dd_attr &= ~DT_DA_LONG;
278 attr = DT_DA_LONGLONG;
279 }
280
281 ddp->dd_attr |= attr;
282 return (dt_decl_check(ddp));
283}
284
285/*
286 * Examine the list of formal parameters 'flist' and determine if the formal
287 * name fnp->dn_string is defined in this list (B_TRUE) or not (B_FALSE).
288 * If 'fnp' is in 'flist', do not search beyond 'fnp' itself in 'flist'.
289 */
290static int
291dt_decl_protoform(dt_node_t *fnp, dt_node_t *flist)
292{
293 dt_node_t *dnp;
294
295 for (dnp = flist; dnp != fnp && dnp != NULL; dnp = dnp->dn_list) {
296 if (dnp->dn_string != NULL &&
297 strcmp(dnp->dn_string, fnp->dn_string) == 0)
298 return (B_TRUE);
299 }
300
301 return (B_FALSE);
302}
303
304/*
305 * Common code for parsing array, function, and probe definition prototypes.
306 * The prototype node list is specified as 'plist'. The formal prototype
307 * against which to compare the prototype is specified as 'flist'. If plist
308 * and flist are the same, we require that named parameters are unique. If
309 * plist and flist are different, we require that named parameters in plist
310 * match a name that is present in flist.
311 */
312int
313dt_decl_prototype(dt_node_t *plist,
314 dt_node_t *flist, const char *kind, uint_t flags)
315{
316 char n[DT_TYPE_NAMELEN];
317 int is_void, v = 0, i = 1;
318 int form = plist != flist;
319 dt_node_t *dnp;
320
321 for (dnp = plist; dnp != NULL; dnp = dnp->dn_list, i++) {
322
323 if (dnp->dn_type == CTF_ERR && !(flags & DT_DP_VARARGS)) {
324 dnerror(dnp, D_DECL_PROTO_VARARGS, "%s prototype may "
325 "not use a variable-length argument list\n", kind);
326 }
327
328 if (dt_node_is_dynamic(dnp) && !(flags & DT_DP_DYNAMIC)) {
329 dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "
330 "use parameter of type %s: %s, parameter #%d\n",
331 kind, dt_node_type_name(dnp, n, sizeof (n)),
332 dnp->dn_string ? dnp->dn_string : "(anonymous)", i);
333 }
334
335 is_void = dt_node_is_void(dnp);
336 v += is_void;
337
338 if (is_void && !(flags & DT_DP_VOID)) {
339 dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "
340 "use parameter of type %s: %s, parameter #%d\n",
341 kind, dt_node_type_name(dnp, n, sizeof (n)),
342 dnp->dn_string ? dnp->dn_string : "(anonymous)", i);
343 }
344
345 if (is_void && dnp->dn_string != NULL) {
346 dnerror(dnp, D_DECL_PROTO_NAME, "void parameter may "
347 "not have a name: %s\n", dnp->dn_string);
348 }
349
350 if (dnp->dn_string != NULL &&
351 dt_decl_protoform(dnp, flist) != form) {
352 dnerror(dnp, D_DECL_PROTO_FORM, "parameter is "
353 "%s declared in %s prototype: %s, parameter #%d\n",
354 form ? "not" : "already", kind, dnp->dn_string, i);
355 }
356
357 if (dnp->dn_string == NULL &&
358 !is_void && !(flags & DT_DP_ANON)) {
359 dnerror(dnp, D_DECL_PROTO_NAME, "parameter declaration "
360 "requires a name: parameter #%d\n", i);
361 }
362 }
363
364 if (v != 0 && plist->dn_list != NULL)
365 xyerror(D_DECL_PROTO_VOID, "void must be sole parameter\n");
366
367 return (v ? 0 : i - 1); /* return zero if sole parameter is 'void' */
368}
369
370dt_decl_t *
371dt_decl_array(dt_node_t *dnp)
372{
373 dt_decl_t *ddp = dt_decl_push(dt_decl_alloc(CTF_K_ARRAY, NULL));
374 dt_scope_t *dsp = &yypcb->pcb_dstack;
375 dt_decl_t *ndp = ddp;
376
377 /*
378 * After pushing the array on to the decl stack, scan ahead for multi-
379 * dimensional array declarations and push the current decl to the
380 * bottom to match the resulting CTF type tree and data layout. Refer
381 * to the comments in dt_decl_type() and ISO C 6.5.2.1 for more info.
382 */
383 while (ndp->dd_next != NULL && ndp->dd_next->dd_kind == CTF_K_ARRAY)
384 ndp = ndp->dd_next; /* skip to bottom-most array declaration */
385
386 if (ndp != ddp) {
387 if (dnp != NULL && dnp->dn_kind == DT_NODE_TYPE) {
388 xyerror(D_DECL_DYNOBJ,
389 "cannot declare array of associative arrays\n");
390 }
391 dsp->ds_decl = ddp->dd_next;
392 ddp->dd_next = ndp->dd_next;
393 ndp->dd_next = ddp;
394 }
395
396 if (ddp->dd_next->dd_name != NULL &&
397 strcmp(ddp->dd_next->dd_name, "void") == 0)
398 xyerror(D_DECL_VOIDOBJ, "cannot declare array of void\n");
399
400 if (dnp != NULL && dnp->dn_kind != DT_NODE_TYPE) {
401 dnp = ddp->dd_node = dt_node_cook(dnp, DT_IDFLG_REF);
402
403 if (dt_node_is_posconst(dnp) == 0) {
404 xyerror(D_DECL_ARRSUB, "positive integral constant "
405 "expression or tuple signature expected as "
406 "array declaration subscript\n");
407 }
408
409 if (dnp->dn_value > UINT_MAX)
410 xyerror(D_DECL_ARRBIG, "array dimension too big\n");
411
412 } else if (dnp != NULL) {
413 ddp->dd_node = dnp;
414 (void) dt_decl_prototype(dnp, dnp, "array", DT_DP_ANON);
415 }
416
417 return (ddp);
418}
419
420/*
421 * When a function is declared, we need to fudge the decl stack a bit if the
422 * declaration uses the function pointer (*)() syntax. In this case, the
423 * dt_decl_func() call occurs *after* the dt_decl_ptr() call, even though the
424 * resulting type is "pointer to function". To make the pointer land on top,
425 * we check to see if 'pdp' is non-NULL and a pointer. If it is, we search
426 * backward for a decl tagged with DT_DA_PAREN, and if one is found, the func
427 * decl is inserted behind this node in the decl list instead of at the top.
428 * In all cases, the func decl's dd_next pointer is set to the decl chain
429 * for the function's return type and the function parameter list is discarded.
430 */
431dt_decl_t *
432dt_decl_func(dt_decl_t *pdp, dt_node_t *dnp)
433{
434 dt_decl_t *ddp = dt_decl_alloc(CTF_K_FUNCTION, NULL);
435
436 ddp->dd_node = dnp;
437
438 (void) dt_decl_prototype(dnp, dnp, "function",
439 DT_DP_VARARGS | DT_DP_VOID | DT_DP_ANON);
440
441 if (pdp == NULL || pdp->dd_kind != CTF_K_POINTER)
442 return (dt_decl_push(ddp));
443
444 while (pdp->dd_next != NULL && !(pdp->dd_next->dd_attr & DT_DA_PAREN))
445 pdp = pdp->dd_next;
446
447 if (pdp->dd_next == NULL)
448 return (dt_decl_push(ddp));
449
450 ddp->dd_next = pdp->dd_next;
451 pdp->dd_next = ddp;
452
453 return (pdp);
454}
455
456dt_decl_t *
457dt_decl_ptr(void)
458{
459 return (dt_decl_push(dt_decl_alloc(CTF_K_POINTER, NULL)));
460}
461
462dt_decl_t *
463dt_decl_sou(uint_t kind, char *name)
464{
465 dt_decl_t *ddp = dt_decl_spec(kind, name);
466 char n[DT_TYPE_NAMELEN];
467 ctf_file_t *ctfp;
468 ctf_id_t type;
469 uint_t flag;
470
471 if (yypcb->pcb_idepth != 0)
472 ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
473 else
474 ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;
475
476 if (yypcb->pcb_dstack.ds_next != NULL)
477 flag = CTF_ADD_NONROOT;
478 else
479 flag = CTF_ADD_ROOT;
480
481 (void) snprintf(n, sizeof (n), "%s %s",
482 kind == CTF_K_STRUCT ? "struct" : "union",
483 name == NULL ? "(anon)" : name);
484
485 if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR &&
486 ctf_type_kind(ctfp, type) != CTF_K_FORWARD)
487 xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);
488
489 if (kind == CTF_K_STRUCT)
490 type = ctf_add_struct(ctfp, flag, name);
491 else
492 type = ctf_add_union(ctfp, flag, name);
493
494 if (type == CTF_ERR || ctf_update(ctfp) == CTF_ERR) {
495 xyerror(D_UNKNOWN, "failed to define %s: %s\n",
496 n, ctf_errmsg(ctf_errno(ctfp)));
497 }
498
499 ddp->dd_ctfp = ctfp;
500 ddp->dd_type = type;
501
502 dt_scope_push(ctfp, type);
503 return (ddp);
504}
505
506void
507dt_decl_member(dt_node_t *dnp)
508{
509 dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;
510 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
511 char *ident = yypcb->pcb_dstack.ds_ident;
512
513 const char *idname = ident ? ident : "(anon)";
514 char n[DT_TYPE_NAMELEN];
515
516 dtrace_typeinfo_t dtt;
517 ctf_encoding_t cte;
518 ctf_id_t base;
519 uint_t kind;
520 ssize_t size;
521
522 if (dsp == NULL)
523 longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
524
525 if (ddp == NULL)
526 longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);
527
528 if (dnp == NULL && ident == NULL)
529 xyerror(D_DECL_MNAME, "member declaration requires a name\n");
530
531 if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {
532 ddp->dd_kind = CTF_K_INTEGER;
533 (void) dt_decl_check(ddp);
534 }
535
536 if (dt_decl_type(ddp, &dtt) != 0)
537 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
538
539 if (ident != NULL && strchr(ident, '`') != NULL) {
540 xyerror(D_DECL_SCOPE, "D scoping operator may not be used "
541 "in a member name (%s)\n", ident);
542 }
543
544 if (dtt.dtt_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
545 dtt.dtt_type == DT_DYN_TYPE(yypcb->pcb_hdl)) {
546 xyerror(D_DECL_DYNOBJ,
547 "cannot have dynamic member: %s\n", ident);
548 }
549
550 base = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
551 kind = ctf_type_kind(dtt.dtt_ctfp, base);
552 size = ctf_type_size(dtt.dtt_ctfp, base);
553
554 if (kind == CTF_K_FORWARD || ((kind == CTF_K_STRUCT ||
555 kind == CTF_K_UNION) && size == 0)) {
556 xyerror(D_DECL_INCOMPLETE, "incomplete struct/union/enum %s: "
557 "%s\n", dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
558 n, sizeof (n)), ident);
559 }
560
561 if (size == 0)
562 xyerror(D_DECL_VOIDOBJ, "cannot have void member: %s\n", ident);
563
564 /*
565 * If a bit-field qualifier was part of the member declaration, create
566 * a new integer type of the same name and attributes as the base type
567 * and size equal to the specified number of bits. We reset 'dtt' to
568 * refer to this new bit-field type and continue on to add the member.
569 */
570 if (dnp != NULL) {
571 dnp = dt_node_cook(dnp, DT_IDFLG_REF);
572
573 /*
574 * A bit-field member with no declarator is permitted to have
575 * size zero and indicates that no more fields are to be packed
576 * into the current storage unit. We ignore these directives
577 * as the underlying ctf code currently does so for all fields.
578 */
579 if (ident == NULL && dnp->dn_kind == DT_NODE_INT &&
580 dnp->dn_value == 0) {
581 dt_node_free(dnp);
582 goto done;
583 }
584
585 if (dt_node_is_posconst(dnp) == 0) {
586 xyerror(D_DECL_BFCONST, "positive integral constant "
587 "expression expected as bit-field size\n");
588 }
589
590 if (ctf_type_kind(dtt.dtt_ctfp, base) != CTF_K_INTEGER ||
591 ctf_type_encoding(dtt.dtt_ctfp, base, &cte) == CTF_ERR ||
592 IS_VOID(cte)) {
593 xyerror(D_DECL_BFTYPE, "invalid type for "
594 "bit-field: %s\n", idname);
595 }
596
597 if (dnp->dn_value > cte.cte_bits) {
598 xyerror(D_DECL_BFSIZE, "bit-field too big "
599 "for type: %s\n", idname);
600 }
601
602 cte.cte_offset = 0;
603 cte.cte_bits = (uint_t)dnp->dn_value;
604
605 dtt.dtt_type = ctf_add_integer(dsp->ds_ctfp,
606 CTF_ADD_NONROOT, ctf_type_name(dtt.dtt_ctfp,
607 dtt.dtt_type, n, sizeof (n)), &cte);
608
609 if (dtt.dtt_type == CTF_ERR ||
610 ctf_update(dsp->ds_ctfp) == CTF_ERR) {
611 xyerror(D_UNKNOWN, "failed to create type for "
612 "member '%s': %s\n", idname,
613 ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
614 }
615
616 dtt.dtt_ctfp = dsp->ds_ctfp;
617 dt_node_free(dnp);
618 }
619
620 /*
621 * If the member type is not defined in the same CTF container as the
622 * one associated with the current scope (i.e. the container for the
623 * struct or union itself) or its parent, copy the member type into
624 * this container and reset dtt to refer to the copied type.
625 */
626 if (dtt.dtt_ctfp != dsp->ds_ctfp &&
627 dtt.dtt_ctfp != ctf_parent_file(dsp->ds_ctfp)) {
628
629 dtt.dtt_type = ctf_add_type(dsp->ds_ctfp,
630 dtt.dtt_ctfp, dtt.dtt_type);
631 dtt.dtt_ctfp = dsp->ds_ctfp;
632
633 if (dtt.dtt_type == CTF_ERR ||
634 ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
635 xyerror(D_UNKNOWN, "failed to copy type of '%s': %s\n",
636 idname, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
637 }
638 }
639
640 if (ctf_add_member(dsp->ds_ctfp, dsp->ds_type,
641 ident, dtt.dtt_type) == CTF_ERR) {
642 xyerror(D_UNKNOWN, "failed to define member '%s': %s\n",
643 idname, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
644 }
645
646done:
647 free(ident);
648 yypcb->pcb_dstack.ds_ident = NULL;
649 dt_decl_reset();
650}
651
652/*ARGSUSED*/
653static int
654dt_decl_hasmembers(const char *name, int value, void *private)
655{
656 return (1); /* abort search and return true if a member exists */
657}
658
659dt_decl_t *
660dt_decl_enum(char *name)
661{
662 dt_decl_t *ddp = dt_decl_spec(CTF_K_ENUM, name);
663 char n[DT_TYPE_NAMELEN];
664 ctf_file_t *ctfp;
665 ctf_id_t type;
666 uint_t flag;
667
668 if (yypcb->pcb_idepth != 0)
669 ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
670 else
671 ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;
672
673 if (yypcb->pcb_dstack.ds_next != NULL)
674 flag = CTF_ADD_NONROOT;
675 else
676 flag = CTF_ADD_ROOT;
677
678 (void) snprintf(n, sizeof (n), "enum %s", name ? name : "(anon)");
679
680 if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR) {
681 if (ctf_enum_iter(ctfp, type, dt_decl_hasmembers, NULL))
682 xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);
683 } else if ((type = ctf_add_enum(ctfp, flag, name)) == CTF_ERR) {
684 xyerror(D_UNKNOWN, "failed to define %s: %s\n",
685 n, ctf_errmsg(ctf_errno(ctfp)));
686 }
687
688 ddp->dd_ctfp = ctfp;
689 ddp->dd_type = type;
690
691 dt_scope_push(ctfp, type);
692 return (ddp);
693}
694
695void
696dt_decl_enumerator(char *s, dt_node_t *dnp)
697{
698 dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;
699 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
700
701 dt_idnode_t *inp;
702 dt_ident_t *idp;
703 char *name;
704 int value;
705
706 name = alloca(strlen(s) + 1);
707 (void) strcpy(name, s);
708 free(s);
709
710 if (dsp == NULL)
711 longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
712
713 assert(dsp->ds_decl->dd_kind == CTF_K_ENUM);
714 value = dsp->ds_enumval + 1; /* default is previous value plus one */
715
716 if (strchr(name, '`') != NULL) {
717 xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
718 "an enumerator name (%s)\n", name);
719 }
720
721 /*
722 * If the enumerator is being assigned a value, cook and check the node
723 * and then free it after we get the value. We also permit references
724 * to identifiers which are previously defined enumerators in the type.
725 */
726 if (dnp != NULL) {
727 if (dnp->dn_kind != DT_NODE_IDENT || ctf_enum_value(
728 dsp->ds_ctfp, dsp->ds_type, dnp->dn_string, &value) != 0) {
729 dnp = dt_node_cook(dnp, DT_IDFLG_REF);
730
731 if (dnp->dn_kind != DT_NODE_INT) {
732 xyerror(D_DECL_ENCONST, "enumerator '%s' must "
733 "be assigned to an integral constant "
734 "expression\n", name);
735 }
736
737 if ((intmax_t)dnp->dn_value > INT_MAX ||
738 (intmax_t)dnp->dn_value < INT_MIN) {
739 xyerror(D_DECL_ENOFLOW, "enumerator '%s' value "
740 "overflows INT_MAX (%d)\n", name, INT_MAX);
741 }
742
743 value = (int)dnp->dn_value;
744 }
745 dt_node_free(dnp);
746 }
747
748 if (ctf_add_enumerator(dsp->ds_ctfp, dsp->ds_type,
749 name, value) == CTF_ERR || ctf_update(dsp->ds_ctfp) == CTF_ERR) {
750 xyerror(D_UNKNOWN, "failed to define enumerator '%s': %s\n",
751 name, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
752 }
753
754 dsp->ds_enumval = value; /* save most recent value */
755
756 /*
757 * If the enumerator name matches an identifier in the global scope,
758 * flag this as an error. We only do this for "D" enumerators to
759 * prevent "C" header file enumerators from conflicting with the ever-
760 * growing list of D built-in global variables and inlines. If a "C"
761 * enumerator conflicts with a global identifier, we add the enumerator
762 * but do not insert a corresponding inline (i.e. the D variable wins).
763 */
764 if (dt_idstack_lookup(&yypcb->pcb_globals, name) != NULL) {
765 if (dsp->ds_ctfp == dtp->dt_ddefs->dm_ctfp) {
766 xyerror(D_DECL_IDRED,
767 "identifier redeclared: %s\n", name);
768 } else
769 return;
770 }
771
772 dt_dprintf("add global enumerator %s = %d\n", name, value);
773
774 idp = dt_idhash_insert(dtp->dt_globals, name, DT_IDENT_ENUM,
775 DT_IDFLG_INLINE | DT_IDFLG_REF, 0, _dtrace_defattr, 0,
776 &dt_idops_inline, NULL, dtp->dt_gen);
777
778 if (idp == NULL)
779 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
780
781 yyintprefix = 0;
782 yyintsuffix[0] = '\0';
783 yyintdecimal = 0;
784
785 dnp = dt_node_int(value);
786 dt_node_type_assign(dnp, dsp->ds_ctfp, dsp->ds_type);
787
788 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
789 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
790
791 /*
792 * Remove the INT node from the node allocation list and store it in
793 * din_list and din_root so it persists with and is freed by the ident.
794 */
795 assert(yypcb->pcb_list == dnp);
796 yypcb->pcb_list = dnp->dn_link;
797 dnp->dn_link = NULL;
798
799 bzero(inp, sizeof (dt_idnode_t));
800 inp->din_list = dnp;
801 inp->din_root = dnp;
802
803 idp->di_iarg = inp;
804 idp->di_ctfp = dsp->ds_ctfp;
805 idp->di_type = dsp->ds_type;
806}
807
808/*
809 * Look up the type corresponding to the specified decl stack. The scoping of
810 * the underlying type names is handled by dt_type_lookup(). We build up the
811 * name from the specified string and prefixes and then lookup the type. If
812 * we fail, an errmsg is saved and the caller must abort with EDT_COMPILER.
813 */
814int
815dt_decl_type(dt_decl_t *ddp, dtrace_typeinfo_t *tip)
816{
817 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
818
819 dt_module_t *dmp;
820 ctf_arinfo_t r;
821 ctf_id_t type;
822
823 char n[DT_TYPE_NAMELEN];
824 uint_t flag;
825 char *name;
826 int rv;
827
828 /*
829 * Based on our current #include depth and decl stack depth, determine
830 * which dynamic CTF module and scope to use when adding any new types.
831 */
832 dmp = yypcb->pcb_idepth ? dtp->dt_cdefs : dtp->dt_ddefs;
833 flag = yypcb->pcb_dstack.ds_next ? CTF_ADD_NONROOT : CTF_ADD_ROOT;
834
835 /*
836 * If we have already cached a CTF type for this decl, then we just
837 * return the type information for the cached type.
838 */
839 if (ddp->dd_ctfp != NULL &&
840 (dmp = dt_module_lookup_by_ctf(dtp, ddp->dd_ctfp)) != NULL) {
841 tip->dtt_object = dmp->dm_name;
842 tip->dtt_ctfp = ddp->dd_ctfp;
843 tip->dtt_type = ddp->dd_type;
844 return (0);
845 }
846
847 /*
848 * Currently CTF treats all function pointers identically. We cache a
849 * representative ID of kind CTF_K_FUNCTION and just return that type.
850 * If we want to support full function declarations, dd_next refers to
851 * the declaration of the function return type, and the parameter list
852 * should be parsed and hung off a new pointer inside of this decl.
853 */
854 if (ddp->dd_kind == CTF_K_FUNCTION) {
855 tip->dtt_object = dtp->dt_ddefs->dm_name;
856 tip->dtt_ctfp = DT_FUNC_CTFP(dtp);
857 tip->dtt_type = DT_FUNC_TYPE(dtp);
858 return (0);
859 }
860
861 /*
862 * If the decl is a pointer, resolve the rest of the stack by calling
863 * dt_decl_type() recursively and then compute a pointer to the result.
864 * Similar to the code above, we return a cached id for function ptrs.
865 */
866 if (ddp->dd_kind == CTF_K_POINTER) {
867 if (ddp->dd_next->dd_kind == CTF_K_FUNCTION) {
868 tip->dtt_object = dtp->dt_ddefs->dm_name;
869 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
870 tip->dtt_type = DT_FPTR_TYPE(dtp);
871 return (0);
872 }
873
874 if ((rv = dt_decl_type(ddp->dd_next, tip)) == 0 &&
875 (rv = dt_type_pointer(tip)) != 0) {
876 xywarn(D_UNKNOWN, "cannot find type: %s*: %s\n",
877 dt_type_name(tip->dtt_ctfp, tip->dtt_type,
878 n, sizeof (n)), ctf_errmsg(dtp->dt_ctferr));
879 }
880
881 return (rv);
882 }
883
884 /*
885 * If the decl is an array, we must find the base type and then call
886 * dt_decl_type() recursively and then build an array of the result.
887 * The C and D multi-dimensional array syntax requires that consecutive
888 * array declarations be processed from right-to-left (i.e. top-down
889 * from the perspective of the declaration stack). For example, an
890 * array declaration such as int x[3][5] is stored on the stack as:
891 *
892 * (bottom) NULL <- ( INT "int" ) <- ( ARR [3] ) <- ( ARR [5] ) (top)
893 *
894 * but means that x is declared to be an array of 3 objects each of
895 * which is an array of 5 integers, or in CTF representation:
896 *
897 * type T1:( content=int, nelems=5 ) type T2:( content=T1, nelems=3 )
898 *
899 * For more details, refer to K&R[5.7] and ISO C 6.5.2.1. Rather than
900 * overcomplicate the implementation of dt_decl_type(), we push array
901 * declarations down into the stack in dt_decl_array(), above, so that
902 * by the time dt_decl_type() is called, the decl stack looks like:
903 *
904 * (bottom) NULL <- ( INT "int" ) <- ( ARR [5] ) <- ( ARR [3] ) (top)
905 *
906 * which permits a straightforward recursive descent of the decl stack
907 * to build the corresponding CTF type tree in the appropriate order.
908 */
909 if (ddp->dd_kind == CTF_K_ARRAY) {
910 /*
911 * If the array decl has a parameter list associated with it,
912 * this is an associative array declaration: return <DYN>.
913 */
914 if (ddp->dd_node != NULL &&
915 ddp->dd_node->dn_kind == DT_NODE_TYPE) {
916 tip->dtt_object = dtp->dt_ddefs->dm_name;
917 tip->dtt_ctfp = DT_DYN_CTFP(dtp);
918 tip->dtt_type = DT_DYN_TYPE(dtp);
919 return (0);
920 }
921
922 if ((rv = dt_decl_type(ddp->dd_next, tip)) != 0)
923 return (rv);
924
925 /*
926 * If the array base type is not defined in the target
927 * container or its parent, copy the type to the target
928 * container and reset dtt_ctfp and dtt_type to the copy.
929 */
930 if (tip->dtt_ctfp != dmp->dm_ctfp &&
931 tip->dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
932
933 tip->dtt_type = ctf_add_type(dmp->dm_ctfp,
934 tip->dtt_ctfp, tip->dtt_type);
935 tip->dtt_ctfp = dmp->dm_ctfp;
936
937 if (tip->dtt_type == CTF_ERR ||
938 ctf_update(tip->dtt_ctfp) == CTF_ERR) {
939 xywarn(D_UNKNOWN, "failed to copy type: %s\n",
940 ctf_errmsg(ctf_errno(tip->dtt_ctfp)));
941 return (-1);
942 }
943 }
944
945 /*
946 * The array index type is irrelevant in C and D: just set it
947 * to "long" for all array types that we create on-the-fly.
948 */
949 r.ctr_contents = tip->dtt_type;
950 r.ctr_index = ctf_lookup_by_name(tip->dtt_ctfp, "long");
951 r.ctr_nelems = ddp->dd_node ?
952 (uint_t)ddp->dd_node->dn_value : 0;
953
954 tip->dtt_object = dmp->dm_name;
955 tip->dtt_ctfp = dmp->dm_ctfp;
956 tip->dtt_type = ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &r);
957
958 if (tip->dtt_type == CTF_ERR ||
959 ctf_update(tip->dtt_ctfp) == CTF_ERR) {
960 xywarn(D_UNKNOWN, "failed to create array type: %s\n",
961 ctf_errmsg(ctf_errno(tip->dtt_ctfp)));
962 return (-1);
963 }
964
965 return (0);
966 }
967
968 /*
969 * Allocate space for the type name and enough space for the maximum
970 * additional text ("unsigned long long \0" requires 20 more bytes).
971 */
972 name = alloca(ddp->dd_name ? strlen(ddp->dd_name) + 20 : 20);
973 name[0] = '\0';
974
975 switch (ddp->dd_kind) {
976 case CTF_K_INTEGER:
977 case CTF_K_FLOAT:
978 if (ddp->dd_attr & DT_DA_SIGNED)
979 (void) strcat(name, "signed ");
980 if (ddp->dd_attr & DT_DA_UNSIGNED)
981 (void) strcat(name, "unsigned ");
982 if (ddp->dd_attr & DT_DA_SHORT)
983 (void) strcat(name, "short ");
984 if (ddp->dd_attr & DT_DA_LONG)
985 (void) strcat(name, "long ");
986 if (ddp->dd_attr & DT_DA_LONGLONG)
987 (void) strcat(name, "long long ");
988 if (ddp->dd_attr == 0 && ddp->dd_name == NULL)
989 (void) strcat(name, "int");
990 break;
991 case CTF_K_STRUCT:
992 (void) strcpy(name, "struct ");
993 break;
994 case CTF_K_UNION:
995 (void) strcpy(name, "union ");
996 break;
997 case CTF_K_ENUM:
998 (void) strcpy(name, "enum ");
999 break;
1000 case CTF_K_TYPEDEF:
1001 break;
1002 default:
1003 xywarn(D_UNKNOWN, "internal error -- "
1004 "bad decl kind %u\n", ddp->dd_kind);
1005 return (-1);
1006 }
1007
1008 /*
1009 * Add dd_name unless a short, long, or long long is explicitly
1010 * suffixed by int. We use the C/CTF canonical names for integers.
1011 */
1012 if (ddp->dd_name != NULL && (ddp->dd_kind != CTF_K_INTEGER ||
1013 (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG)) == 0))
1014 (void) strcat(name, ddp->dd_name);
1015
1016 /*
1017 * Lookup the type. If we find it, we're done. Otherwise create a
1018 * forward tag for the type if it is a struct, union, or enum. If
1019 * we can't find it and we can't create a tag, return failure.
1020 */
1021 if ((rv = dt_type_lookup(name, tip)) == 0)
1022 return (rv);
1023
1024 switch (ddp->dd_kind) {
1025 case CTF_K_STRUCT:
1026 case CTF_K_UNION:
1027 case CTF_K_ENUM:
1028 type = ctf_add_forward(dmp->dm_ctfp, flag,
1029 ddp->dd_name, ddp->dd_kind);
1030 break;
1031 default:
1032 xywarn(D_UNKNOWN, "failed to resolve type %s: %s\n", name,
1033 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1034 return (rv);
1035 }
1036
1037 if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
1038 xywarn(D_UNKNOWN, "failed to add forward tag for %s: %s\n",
1039 name, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1040 return (-1);
1041 }
1042
1043 ddp->dd_ctfp = dmp->dm_ctfp;
1044 ddp->dd_type = type;
1045
1046 tip->dtt_object = dmp->dm_name;
1047 tip->dtt_ctfp = dmp->dm_ctfp;
1048 tip->dtt_type = type;
1049
1050 return (0);
1051}
1052
1053void
1054dt_scope_create(dt_scope_t *dsp)
1055{
1056 dsp->ds_decl = NULL;
1057 dsp->ds_next = NULL;
1058 dsp->ds_ident = NULL;
1059 dsp->ds_ctfp = NULL;
1060 dsp->ds_type = CTF_ERR;
1061 dsp->ds_class = DT_DC_DEFAULT;
1062 dsp->ds_enumval = -1;
1063}
1064
1065void
1066dt_scope_destroy(dt_scope_t *dsp)
1067{
1068 dt_scope_t *nsp;
1069
1070 for (; dsp != NULL; dsp = nsp) {
1071 dt_decl_free(dsp->ds_decl);
1072 free(dsp->ds_ident);
1073 nsp = dsp->ds_next;
1074 if (dsp != &yypcb->pcb_dstack)
1075 free(dsp);
1076 }
1077}
1078
1079void
1080dt_scope_push(ctf_file_t *ctfp, ctf_id_t type)
1081{
1082 dt_scope_t *rsp = &yypcb->pcb_dstack;
1083 dt_scope_t *dsp = malloc(sizeof (dt_scope_t));
1084
1085 if (dsp == NULL)
1086 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1087
1088 dsp->ds_decl = rsp->ds_decl;
1089 dsp->ds_next = rsp->ds_next;
1090 dsp->ds_ident = rsp->ds_ident;
1091 dsp->ds_ctfp = ctfp;
1092 dsp->ds_type = type;
1093 dsp->ds_class = rsp->ds_class;
1094 dsp->ds_enumval = rsp->ds_enumval;
1095
1096 dt_scope_create(rsp);
1097 rsp->ds_next = dsp;
1098}
1099
1100dt_decl_t *
1101dt_scope_pop(void)
1102{
1103 dt_scope_t *rsp = &yypcb->pcb_dstack;
1104 dt_scope_t *dsp = rsp->ds_next;
1105
1106 if (dsp == NULL)
1107 longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
1108
1109 if (dsp->ds_ctfp != NULL && ctf_update(dsp->ds_ctfp) == CTF_ERR) {
1110 xyerror(D_UNKNOWN, "failed to update type definitions: %s\n",
1111 ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
1112 }
1113
1114 dt_decl_free(rsp->ds_decl);
1115 free(rsp->ds_ident);
1116
1117 rsp->ds_decl = dsp->ds_decl;
1118 rsp->ds_next = dsp->ds_next;
1119 rsp->ds_ident = dsp->ds_ident;
1120 rsp->ds_ctfp = dsp->ds_ctfp;
1121 rsp->ds_type = dsp->ds_type;
1122 rsp->ds_class = dsp->ds_class;
1123 rsp->ds_enumval = dsp->ds_enumval;
1124
1125 free(dsp);
1126 return (rsp->ds_decl);
1127}
258 return (dt_decl_check(ddp));
259}
260
261dt_decl_t *
262dt_decl_attr(ushort_t attr)
263{
264 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
265
266 if (ddp == NULL) {
267 ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));
268 ddp->dd_attr = attr;
269 return (ddp);
270 }
271
272 if (attr == DT_DA_LONG && (ddp->dd_attr & DT_DA_LONG)) {
273 ddp->dd_attr &= ~DT_DA_LONG;
274 attr = DT_DA_LONGLONG;
275 }
276
277 ddp->dd_attr |= attr;
278 return (dt_decl_check(ddp));
279}
280
281/*
282 * Examine the list of formal parameters 'flist' and determine if the formal
283 * name fnp->dn_string is defined in this list (B_TRUE) or not (B_FALSE).
284 * If 'fnp' is in 'flist', do not search beyond 'fnp' itself in 'flist'.
285 */
286static int
287dt_decl_protoform(dt_node_t *fnp, dt_node_t *flist)
288{
289 dt_node_t *dnp;
290
291 for (dnp = flist; dnp != fnp && dnp != NULL; dnp = dnp->dn_list) {
292 if (dnp->dn_string != NULL &&
293 strcmp(dnp->dn_string, fnp->dn_string) == 0)
294 return (B_TRUE);
295 }
296
297 return (B_FALSE);
298}
299
300/*
301 * Common code for parsing array, function, and probe definition prototypes.
302 * The prototype node list is specified as 'plist'. The formal prototype
303 * against which to compare the prototype is specified as 'flist'. If plist
304 * and flist are the same, we require that named parameters are unique. If
305 * plist and flist are different, we require that named parameters in plist
306 * match a name that is present in flist.
307 */
308int
309dt_decl_prototype(dt_node_t *plist,
310 dt_node_t *flist, const char *kind, uint_t flags)
311{
312 char n[DT_TYPE_NAMELEN];
313 int is_void, v = 0, i = 1;
314 int form = plist != flist;
315 dt_node_t *dnp;
316
317 for (dnp = plist; dnp != NULL; dnp = dnp->dn_list, i++) {
318
319 if (dnp->dn_type == CTF_ERR && !(flags & DT_DP_VARARGS)) {
320 dnerror(dnp, D_DECL_PROTO_VARARGS, "%s prototype may "
321 "not use a variable-length argument list\n", kind);
322 }
323
324 if (dt_node_is_dynamic(dnp) && !(flags & DT_DP_DYNAMIC)) {
325 dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "
326 "use parameter of type %s: %s, parameter #%d\n",
327 kind, dt_node_type_name(dnp, n, sizeof (n)),
328 dnp->dn_string ? dnp->dn_string : "(anonymous)", i);
329 }
330
331 is_void = dt_node_is_void(dnp);
332 v += is_void;
333
334 if (is_void && !(flags & DT_DP_VOID)) {
335 dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "
336 "use parameter of type %s: %s, parameter #%d\n",
337 kind, dt_node_type_name(dnp, n, sizeof (n)),
338 dnp->dn_string ? dnp->dn_string : "(anonymous)", i);
339 }
340
341 if (is_void && dnp->dn_string != NULL) {
342 dnerror(dnp, D_DECL_PROTO_NAME, "void parameter may "
343 "not have a name: %s\n", dnp->dn_string);
344 }
345
346 if (dnp->dn_string != NULL &&
347 dt_decl_protoform(dnp, flist) != form) {
348 dnerror(dnp, D_DECL_PROTO_FORM, "parameter is "
349 "%s declared in %s prototype: %s, parameter #%d\n",
350 form ? "not" : "already", kind, dnp->dn_string, i);
351 }
352
353 if (dnp->dn_string == NULL &&
354 !is_void && !(flags & DT_DP_ANON)) {
355 dnerror(dnp, D_DECL_PROTO_NAME, "parameter declaration "
356 "requires a name: parameter #%d\n", i);
357 }
358 }
359
360 if (v != 0 && plist->dn_list != NULL)
361 xyerror(D_DECL_PROTO_VOID, "void must be sole parameter\n");
362
363 return (v ? 0 : i - 1); /* return zero if sole parameter is 'void' */
364}
365
366dt_decl_t *
367dt_decl_array(dt_node_t *dnp)
368{
369 dt_decl_t *ddp = dt_decl_push(dt_decl_alloc(CTF_K_ARRAY, NULL));
370 dt_scope_t *dsp = &yypcb->pcb_dstack;
371 dt_decl_t *ndp = ddp;
372
373 /*
374 * After pushing the array on to the decl stack, scan ahead for multi-
375 * dimensional array declarations and push the current decl to the
376 * bottom to match the resulting CTF type tree and data layout. Refer
377 * to the comments in dt_decl_type() and ISO C 6.5.2.1 for more info.
378 */
379 while (ndp->dd_next != NULL && ndp->dd_next->dd_kind == CTF_K_ARRAY)
380 ndp = ndp->dd_next; /* skip to bottom-most array declaration */
381
382 if (ndp != ddp) {
383 if (dnp != NULL && dnp->dn_kind == DT_NODE_TYPE) {
384 xyerror(D_DECL_DYNOBJ,
385 "cannot declare array of associative arrays\n");
386 }
387 dsp->ds_decl = ddp->dd_next;
388 ddp->dd_next = ndp->dd_next;
389 ndp->dd_next = ddp;
390 }
391
392 if (ddp->dd_next->dd_name != NULL &&
393 strcmp(ddp->dd_next->dd_name, "void") == 0)
394 xyerror(D_DECL_VOIDOBJ, "cannot declare array of void\n");
395
396 if (dnp != NULL && dnp->dn_kind != DT_NODE_TYPE) {
397 dnp = ddp->dd_node = dt_node_cook(dnp, DT_IDFLG_REF);
398
399 if (dt_node_is_posconst(dnp) == 0) {
400 xyerror(D_DECL_ARRSUB, "positive integral constant "
401 "expression or tuple signature expected as "
402 "array declaration subscript\n");
403 }
404
405 if (dnp->dn_value > UINT_MAX)
406 xyerror(D_DECL_ARRBIG, "array dimension too big\n");
407
408 } else if (dnp != NULL) {
409 ddp->dd_node = dnp;
410 (void) dt_decl_prototype(dnp, dnp, "array", DT_DP_ANON);
411 }
412
413 return (ddp);
414}
415
416/*
417 * When a function is declared, we need to fudge the decl stack a bit if the
418 * declaration uses the function pointer (*)() syntax. In this case, the
419 * dt_decl_func() call occurs *after* the dt_decl_ptr() call, even though the
420 * resulting type is "pointer to function". To make the pointer land on top,
421 * we check to see if 'pdp' is non-NULL and a pointer. If it is, we search
422 * backward for a decl tagged with DT_DA_PAREN, and if one is found, the func
423 * decl is inserted behind this node in the decl list instead of at the top.
424 * In all cases, the func decl's dd_next pointer is set to the decl chain
425 * for the function's return type and the function parameter list is discarded.
426 */
427dt_decl_t *
428dt_decl_func(dt_decl_t *pdp, dt_node_t *dnp)
429{
430 dt_decl_t *ddp = dt_decl_alloc(CTF_K_FUNCTION, NULL);
431
432 ddp->dd_node = dnp;
433
434 (void) dt_decl_prototype(dnp, dnp, "function",
435 DT_DP_VARARGS | DT_DP_VOID | DT_DP_ANON);
436
437 if (pdp == NULL || pdp->dd_kind != CTF_K_POINTER)
438 return (dt_decl_push(ddp));
439
440 while (pdp->dd_next != NULL && !(pdp->dd_next->dd_attr & DT_DA_PAREN))
441 pdp = pdp->dd_next;
442
443 if (pdp->dd_next == NULL)
444 return (dt_decl_push(ddp));
445
446 ddp->dd_next = pdp->dd_next;
447 pdp->dd_next = ddp;
448
449 return (pdp);
450}
451
452dt_decl_t *
453dt_decl_ptr(void)
454{
455 return (dt_decl_push(dt_decl_alloc(CTF_K_POINTER, NULL)));
456}
457
458dt_decl_t *
459dt_decl_sou(uint_t kind, char *name)
460{
461 dt_decl_t *ddp = dt_decl_spec(kind, name);
462 char n[DT_TYPE_NAMELEN];
463 ctf_file_t *ctfp;
464 ctf_id_t type;
465 uint_t flag;
466
467 if (yypcb->pcb_idepth != 0)
468 ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
469 else
470 ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;
471
472 if (yypcb->pcb_dstack.ds_next != NULL)
473 flag = CTF_ADD_NONROOT;
474 else
475 flag = CTF_ADD_ROOT;
476
477 (void) snprintf(n, sizeof (n), "%s %s",
478 kind == CTF_K_STRUCT ? "struct" : "union",
479 name == NULL ? "(anon)" : name);
480
481 if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR &&
482 ctf_type_kind(ctfp, type) != CTF_K_FORWARD)
483 xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);
484
485 if (kind == CTF_K_STRUCT)
486 type = ctf_add_struct(ctfp, flag, name);
487 else
488 type = ctf_add_union(ctfp, flag, name);
489
490 if (type == CTF_ERR || ctf_update(ctfp) == CTF_ERR) {
491 xyerror(D_UNKNOWN, "failed to define %s: %s\n",
492 n, ctf_errmsg(ctf_errno(ctfp)));
493 }
494
495 ddp->dd_ctfp = ctfp;
496 ddp->dd_type = type;
497
498 dt_scope_push(ctfp, type);
499 return (ddp);
500}
501
502void
503dt_decl_member(dt_node_t *dnp)
504{
505 dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;
506 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
507 char *ident = yypcb->pcb_dstack.ds_ident;
508
509 const char *idname = ident ? ident : "(anon)";
510 char n[DT_TYPE_NAMELEN];
511
512 dtrace_typeinfo_t dtt;
513 ctf_encoding_t cte;
514 ctf_id_t base;
515 uint_t kind;
516 ssize_t size;
517
518 if (dsp == NULL)
519 longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
520
521 if (ddp == NULL)
522 longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);
523
524 if (dnp == NULL && ident == NULL)
525 xyerror(D_DECL_MNAME, "member declaration requires a name\n");
526
527 if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {
528 ddp->dd_kind = CTF_K_INTEGER;
529 (void) dt_decl_check(ddp);
530 }
531
532 if (dt_decl_type(ddp, &dtt) != 0)
533 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
534
535 if (ident != NULL && strchr(ident, '`') != NULL) {
536 xyerror(D_DECL_SCOPE, "D scoping operator may not be used "
537 "in a member name (%s)\n", ident);
538 }
539
540 if (dtt.dtt_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
541 dtt.dtt_type == DT_DYN_TYPE(yypcb->pcb_hdl)) {
542 xyerror(D_DECL_DYNOBJ,
543 "cannot have dynamic member: %s\n", ident);
544 }
545
546 base = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
547 kind = ctf_type_kind(dtt.dtt_ctfp, base);
548 size = ctf_type_size(dtt.dtt_ctfp, base);
549
550 if (kind == CTF_K_FORWARD || ((kind == CTF_K_STRUCT ||
551 kind == CTF_K_UNION) && size == 0)) {
552 xyerror(D_DECL_INCOMPLETE, "incomplete struct/union/enum %s: "
553 "%s\n", dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
554 n, sizeof (n)), ident);
555 }
556
557 if (size == 0)
558 xyerror(D_DECL_VOIDOBJ, "cannot have void member: %s\n", ident);
559
560 /*
561 * If a bit-field qualifier was part of the member declaration, create
562 * a new integer type of the same name and attributes as the base type
563 * and size equal to the specified number of bits. We reset 'dtt' to
564 * refer to this new bit-field type and continue on to add the member.
565 */
566 if (dnp != NULL) {
567 dnp = dt_node_cook(dnp, DT_IDFLG_REF);
568
569 /*
570 * A bit-field member with no declarator is permitted to have
571 * size zero and indicates that no more fields are to be packed
572 * into the current storage unit. We ignore these directives
573 * as the underlying ctf code currently does so for all fields.
574 */
575 if (ident == NULL && dnp->dn_kind == DT_NODE_INT &&
576 dnp->dn_value == 0) {
577 dt_node_free(dnp);
578 goto done;
579 }
580
581 if (dt_node_is_posconst(dnp) == 0) {
582 xyerror(D_DECL_BFCONST, "positive integral constant "
583 "expression expected as bit-field size\n");
584 }
585
586 if (ctf_type_kind(dtt.dtt_ctfp, base) != CTF_K_INTEGER ||
587 ctf_type_encoding(dtt.dtt_ctfp, base, &cte) == CTF_ERR ||
588 IS_VOID(cte)) {
589 xyerror(D_DECL_BFTYPE, "invalid type for "
590 "bit-field: %s\n", idname);
591 }
592
593 if (dnp->dn_value > cte.cte_bits) {
594 xyerror(D_DECL_BFSIZE, "bit-field too big "
595 "for type: %s\n", idname);
596 }
597
598 cte.cte_offset = 0;
599 cte.cte_bits = (uint_t)dnp->dn_value;
600
601 dtt.dtt_type = ctf_add_integer(dsp->ds_ctfp,
602 CTF_ADD_NONROOT, ctf_type_name(dtt.dtt_ctfp,
603 dtt.dtt_type, n, sizeof (n)), &cte);
604
605 if (dtt.dtt_type == CTF_ERR ||
606 ctf_update(dsp->ds_ctfp) == CTF_ERR) {
607 xyerror(D_UNKNOWN, "failed to create type for "
608 "member '%s': %s\n", idname,
609 ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
610 }
611
612 dtt.dtt_ctfp = dsp->ds_ctfp;
613 dt_node_free(dnp);
614 }
615
616 /*
617 * If the member type is not defined in the same CTF container as the
618 * one associated with the current scope (i.e. the container for the
619 * struct or union itself) or its parent, copy the member type into
620 * this container and reset dtt to refer to the copied type.
621 */
622 if (dtt.dtt_ctfp != dsp->ds_ctfp &&
623 dtt.dtt_ctfp != ctf_parent_file(dsp->ds_ctfp)) {
624
625 dtt.dtt_type = ctf_add_type(dsp->ds_ctfp,
626 dtt.dtt_ctfp, dtt.dtt_type);
627 dtt.dtt_ctfp = dsp->ds_ctfp;
628
629 if (dtt.dtt_type == CTF_ERR ||
630 ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
631 xyerror(D_UNKNOWN, "failed to copy type of '%s': %s\n",
632 idname, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
633 }
634 }
635
636 if (ctf_add_member(dsp->ds_ctfp, dsp->ds_type,
637 ident, dtt.dtt_type) == CTF_ERR) {
638 xyerror(D_UNKNOWN, "failed to define member '%s': %s\n",
639 idname, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
640 }
641
642done:
643 free(ident);
644 yypcb->pcb_dstack.ds_ident = NULL;
645 dt_decl_reset();
646}
647
648/*ARGSUSED*/
649static int
650dt_decl_hasmembers(const char *name, int value, void *private)
651{
652 return (1); /* abort search and return true if a member exists */
653}
654
655dt_decl_t *
656dt_decl_enum(char *name)
657{
658 dt_decl_t *ddp = dt_decl_spec(CTF_K_ENUM, name);
659 char n[DT_TYPE_NAMELEN];
660 ctf_file_t *ctfp;
661 ctf_id_t type;
662 uint_t flag;
663
664 if (yypcb->pcb_idepth != 0)
665 ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
666 else
667 ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;
668
669 if (yypcb->pcb_dstack.ds_next != NULL)
670 flag = CTF_ADD_NONROOT;
671 else
672 flag = CTF_ADD_ROOT;
673
674 (void) snprintf(n, sizeof (n), "enum %s", name ? name : "(anon)");
675
676 if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR) {
677 if (ctf_enum_iter(ctfp, type, dt_decl_hasmembers, NULL))
678 xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);
679 } else if ((type = ctf_add_enum(ctfp, flag, name)) == CTF_ERR) {
680 xyerror(D_UNKNOWN, "failed to define %s: %s\n",
681 n, ctf_errmsg(ctf_errno(ctfp)));
682 }
683
684 ddp->dd_ctfp = ctfp;
685 ddp->dd_type = type;
686
687 dt_scope_push(ctfp, type);
688 return (ddp);
689}
690
691void
692dt_decl_enumerator(char *s, dt_node_t *dnp)
693{
694 dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;
695 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
696
697 dt_idnode_t *inp;
698 dt_ident_t *idp;
699 char *name;
700 int value;
701
702 name = alloca(strlen(s) + 1);
703 (void) strcpy(name, s);
704 free(s);
705
706 if (dsp == NULL)
707 longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
708
709 assert(dsp->ds_decl->dd_kind == CTF_K_ENUM);
710 value = dsp->ds_enumval + 1; /* default is previous value plus one */
711
712 if (strchr(name, '`') != NULL) {
713 xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
714 "an enumerator name (%s)\n", name);
715 }
716
717 /*
718 * If the enumerator is being assigned a value, cook and check the node
719 * and then free it after we get the value. We also permit references
720 * to identifiers which are previously defined enumerators in the type.
721 */
722 if (dnp != NULL) {
723 if (dnp->dn_kind != DT_NODE_IDENT || ctf_enum_value(
724 dsp->ds_ctfp, dsp->ds_type, dnp->dn_string, &value) != 0) {
725 dnp = dt_node_cook(dnp, DT_IDFLG_REF);
726
727 if (dnp->dn_kind != DT_NODE_INT) {
728 xyerror(D_DECL_ENCONST, "enumerator '%s' must "
729 "be assigned to an integral constant "
730 "expression\n", name);
731 }
732
733 if ((intmax_t)dnp->dn_value > INT_MAX ||
734 (intmax_t)dnp->dn_value < INT_MIN) {
735 xyerror(D_DECL_ENOFLOW, "enumerator '%s' value "
736 "overflows INT_MAX (%d)\n", name, INT_MAX);
737 }
738
739 value = (int)dnp->dn_value;
740 }
741 dt_node_free(dnp);
742 }
743
744 if (ctf_add_enumerator(dsp->ds_ctfp, dsp->ds_type,
745 name, value) == CTF_ERR || ctf_update(dsp->ds_ctfp) == CTF_ERR) {
746 xyerror(D_UNKNOWN, "failed to define enumerator '%s': %s\n",
747 name, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
748 }
749
750 dsp->ds_enumval = value; /* save most recent value */
751
752 /*
753 * If the enumerator name matches an identifier in the global scope,
754 * flag this as an error. We only do this for "D" enumerators to
755 * prevent "C" header file enumerators from conflicting with the ever-
756 * growing list of D built-in global variables and inlines. If a "C"
757 * enumerator conflicts with a global identifier, we add the enumerator
758 * but do not insert a corresponding inline (i.e. the D variable wins).
759 */
760 if (dt_idstack_lookup(&yypcb->pcb_globals, name) != NULL) {
761 if (dsp->ds_ctfp == dtp->dt_ddefs->dm_ctfp) {
762 xyerror(D_DECL_IDRED,
763 "identifier redeclared: %s\n", name);
764 } else
765 return;
766 }
767
768 dt_dprintf("add global enumerator %s = %d\n", name, value);
769
770 idp = dt_idhash_insert(dtp->dt_globals, name, DT_IDENT_ENUM,
771 DT_IDFLG_INLINE | DT_IDFLG_REF, 0, _dtrace_defattr, 0,
772 &dt_idops_inline, NULL, dtp->dt_gen);
773
774 if (idp == NULL)
775 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
776
777 yyintprefix = 0;
778 yyintsuffix[0] = '\0';
779 yyintdecimal = 0;
780
781 dnp = dt_node_int(value);
782 dt_node_type_assign(dnp, dsp->ds_ctfp, dsp->ds_type);
783
784 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
785 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
786
787 /*
788 * Remove the INT node from the node allocation list and store it in
789 * din_list and din_root so it persists with and is freed by the ident.
790 */
791 assert(yypcb->pcb_list == dnp);
792 yypcb->pcb_list = dnp->dn_link;
793 dnp->dn_link = NULL;
794
795 bzero(inp, sizeof (dt_idnode_t));
796 inp->din_list = dnp;
797 inp->din_root = dnp;
798
799 idp->di_iarg = inp;
800 idp->di_ctfp = dsp->ds_ctfp;
801 idp->di_type = dsp->ds_type;
802}
803
804/*
805 * Look up the type corresponding to the specified decl stack. The scoping of
806 * the underlying type names is handled by dt_type_lookup(). We build up the
807 * name from the specified string and prefixes and then lookup the type. If
808 * we fail, an errmsg is saved and the caller must abort with EDT_COMPILER.
809 */
810int
811dt_decl_type(dt_decl_t *ddp, dtrace_typeinfo_t *tip)
812{
813 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
814
815 dt_module_t *dmp;
816 ctf_arinfo_t r;
817 ctf_id_t type;
818
819 char n[DT_TYPE_NAMELEN];
820 uint_t flag;
821 char *name;
822 int rv;
823
824 /*
825 * Based on our current #include depth and decl stack depth, determine
826 * which dynamic CTF module and scope to use when adding any new types.
827 */
828 dmp = yypcb->pcb_idepth ? dtp->dt_cdefs : dtp->dt_ddefs;
829 flag = yypcb->pcb_dstack.ds_next ? CTF_ADD_NONROOT : CTF_ADD_ROOT;
830
831 /*
832 * If we have already cached a CTF type for this decl, then we just
833 * return the type information for the cached type.
834 */
835 if (ddp->dd_ctfp != NULL &&
836 (dmp = dt_module_lookup_by_ctf(dtp, ddp->dd_ctfp)) != NULL) {
837 tip->dtt_object = dmp->dm_name;
838 tip->dtt_ctfp = ddp->dd_ctfp;
839 tip->dtt_type = ddp->dd_type;
840 return (0);
841 }
842
843 /*
844 * Currently CTF treats all function pointers identically. We cache a
845 * representative ID of kind CTF_K_FUNCTION and just return that type.
846 * If we want to support full function declarations, dd_next refers to
847 * the declaration of the function return type, and the parameter list
848 * should be parsed and hung off a new pointer inside of this decl.
849 */
850 if (ddp->dd_kind == CTF_K_FUNCTION) {
851 tip->dtt_object = dtp->dt_ddefs->dm_name;
852 tip->dtt_ctfp = DT_FUNC_CTFP(dtp);
853 tip->dtt_type = DT_FUNC_TYPE(dtp);
854 return (0);
855 }
856
857 /*
858 * If the decl is a pointer, resolve the rest of the stack by calling
859 * dt_decl_type() recursively and then compute a pointer to the result.
860 * Similar to the code above, we return a cached id for function ptrs.
861 */
862 if (ddp->dd_kind == CTF_K_POINTER) {
863 if (ddp->dd_next->dd_kind == CTF_K_FUNCTION) {
864 tip->dtt_object = dtp->dt_ddefs->dm_name;
865 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
866 tip->dtt_type = DT_FPTR_TYPE(dtp);
867 return (0);
868 }
869
870 if ((rv = dt_decl_type(ddp->dd_next, tip)) == 0 &&
871 (rv = dt_type_pointer(tip)) != 0) {
872 xywarn(D_UNKNOWN, "cannot find type: %s*: %s\n",
873 dt_type_name(tip->dtt_ctfp, tip->dtt_type,
874 n, sizeof (n)), ctf_errmsg(dtp->dt_ctferr));
875 }
876
877 return (rv);
878 }
879
880 /*
881 * If the decl is an array, we must find the base type and then call
882 * dt_decl_type() recursively and then build an array of the result.
883 * The C and D multi-dimensional array syntax requires that consecutive
884 * array declarations be processed from right-to-left (i.e. top-down
885 * from the perspective of the declaration stack). For example, an
886 * array declaration such as int x[3][5] is stored on the stack as:
887 *
888 * (bottom) NULL <- ( INT "int" ) <- ( ARR [3] ) <- ( ARR [5] ) (top)
889 *
890 * but means that x is declared to be an array of 3 objects each of
891 * which is an array of 5 integers, or in CTF representation:
892 *
893 * type T1:( content=int, nelems=5 ) type T2:( content=T1, nelems=3 )
894 *
895 * For more details, refer to K&R[5.7] and ISO C 6.5.2.1. Rather than
896 * overcomplicate the implementation of dt_decl_type(), we push array
897 * declarations down into the stack in dt_decl_array(), above, so that
898 * by the time dt_decl_type() is called, the decl stack looks like:
899 *
900 * (bottom) NULL <- ( INT "int" ) <- ( ARR [5] ) <- ( ARR [3] ) (top)
901 *
902 * which permits a straightforward recursive descent of the decl stack
903 * to build the corresponding CTF type tree in the appropriate order.
904 */
905 if (ddp->dd_kind == CTF_K_ARRAY) {
906 /*
907 * If the array decl has a parameter list associated with it,
908 * this is an associative array declaration: return <DYN>.
909 */
910 if (ddp->dd_node != NULL &&
911 ddp->dd_node->dn_kind == DT_NODE_TYPE) {
912 tip->dtt_object = dtp->dt_ddefs->dm_name;
913 tip->dtt_ctfp = DT_DYN_CTFP(dtp);
914 tip->dtt_type = DT_DYN_TYPE(dtp);
915 return (0);
916 }
917
918 if ((rv = dt_decl_type(ddp->dd_next, tip)) != 0)
919 return (rv);
920
921 /*
922 * If the array base type is not defined in the target
923 * container or its parent, copy the type to the target
924 * container and reset dtt_ctfp and dtt_type to the copy.
925 */
926 if (tip->dtt_ctfp != dmp->dm_ctfp &&
927 tip->dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
928
929 tip->dtt_type = ctf_add_type(dmp->dm_ctfp,
930 tip->dtt_ctfp, tip->dtt_type);
931 tip->dtt_ctfp = dmp->dm_ctfp;
932
933 if (tip->dtt_type == CTF_ERR ||
934 ctf_update(tip->dtt_ctfp) == CTF_ERR) {
935 xywarn(D_UNKNOWN, "failed to copy type: %s\n",
936 ctf_errmsg(ctf_errno(tip->dtt_ctfp)));
937 return (-1);
938 }
939 }
940
941 /*
942 * The array index type is irrelevant in C and D: just set it
943 * to "long" for all array types that we create on-the-fly.
944 */
945 r.ctr_contents = tip->dtt_type;
946 r.ctr_index = ctf_lookup_by_name(tip->dtt_ctfp, "long");
947 r.ctr_nelems = ddp->dd_node ?
948 (uint_t)ddp->dd_node->dn_value : 0;
949
950 tip->dtt_object = dmp->dm_name;
951 tip->dtt_ctfp = dmp->dm_ctfp;
952 tip->dtt_type = ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &r);
953
954 if (tip->dtt_type == CTF_ERR ||
955 ctf_update(tip->dtt_ctfp) == CTF_ERR) {
956 xywarn(D_UNKNOWN, "failed to create array type: %s\n",
957 ctf_errmsg(ctf_errno(tip->dtt_ctfp)));
958 return (-1);
959 }
960
961 return (0);
962 }
963
964 /*
965 * Allocate space for the type name and enough space for the maximum
966 * additional text ("unsigned long long \0" requires 20 more bytes).
967 */
968 name = alloca(ddp->dd_name ? strlen(ddp->dd_name) + 20 : 20);
969 name[0] = '\0';
970
971 switch (ddp->dd_kind) {
972 case CTF_K_INTEGER:
973 case CTF_K_FLOAT:
974 if (ddp->dd_attr & DT_DA_SIGNED)
975 (void) strcat(name, "signed ");
976 if (ddp->dd_attr & DT_DA_UNSIGNED)
977 (void) strcat(name, "unsigned ");
978 if (ddp->dd_attr & DT_DA_SHORT)
979 (void) strcat(name, "short ");
980 if (ddp->dd_attr & DT_DA_LONG)
981 (void) strcat(name, "long ");
982 if (ddp->dd_attr & DT_DA_LONGLONG)
983 (void) strcat(name, "long long ");
984 if (ddp->dd_attr == 0 && ddp->dd_name == NULL)
985 (void) strcat(name, "int");
986 break;
987 case CTF_K_STRUCT:
988 (void) strcpy(name, "struct ");
989 break;
990 case CTF_K_UNION:
991 (void) strcpy(name, "union ");
992 break;
993 case CTF_K_ENUM:
994 (void) strcpy(name, "enum ");
995 break;
996 case CTF_K_TYPEDEF:
997 break;
998 default:
999 xywarn(D_UNKNOWN, "internal error -- "
1000 "bad decl kind %u\n", ddp->dd_kind);
1001 return (-1);
1002 }
1003
1004 /*
1005 * Add dd_name unless a short, long, or long long is explicitly
1006 * suffixed by int. We use the C/CTF canonical names for integers.
1007 */
1008 if (ddp->dd_name != NULL && (ddp->dd_kind != CTF_K_INTEGER ||
1009 (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG)) == 0))
1010 (void) strcat(name, ddp->dd_name);
1011
1012 /*
1013 * Lookup the type. If we find it, we're done. Otherwise create a
1014 * forward tag for the type if it is a struct, union, or enum. If
1015 * we can't find it and we can't create a tag, return failure.
1016 */
1017 if ((rv = dt_type_lookup(name, tip)) == 0)
1018 return (rv);
1019
1020 switch (ddp->dd_kind) {
1021 case CTF_K_STRUCT:
1022 case CTF_K_UNION:
1023 case CTF_K_ENUM:
1024 type = ctf_add_forward(dmp->dm_ctfp, flag,
1025 ddp->dd_name, ddp->dd_kind);
1026 break;
1027 default:
1028 xywarn(D_UNKNOWN, "failed to resolve type %s: %s\n", name,
1029 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1030 return (rv);
1031 }
1032
1033 if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
1034 xywarn(D_UNKNOWN, "failed to add forward tag for %s: %s\n",
1035 name, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1036 return (-1);
1037 }
1038
1039 ddp->dd_ctfp = dmp->dm_ctfp;
1040 ddp->dd_type = type;
1041
1042 tip->dtt_object = dmp->dm_name;
1043 tip->dtt_ctfp = dmp->dm_ctfp;
1044 tip->dtt_type = type;
1045
1046 return (0);
1047}
1048
1049void
1050dt_scope_create(dt_scope_t *dsp)
1051{
1052 dsp->ds_decl = NULL;
1053 dsp->ds_next = NULL;
1054 dsp->ds_ident = NULL;
1055 dsp->ds_ctfp = NULL;
1056 dsp->ds_type = CTF_ERR;
1057 dsp->ds_class = DT_DC_DEFAULT;
1058 dsp->ds_enumval = -1;
1059}
1060
1061void
1062dt_scope_destroy(dt_scope_t *dsp)
1063{
1064 dt_scope_t *nsp;
1065
1066 for (; dsp != NULL; dsp = nsp) {
1067 dt_decl_free(dsp->ds_decl);
1068 free(dsp->ds_ident);
1069 nsp = dsp->ds_next;
1070 if (dsp != &yypcb->pcb_dstack)
1071 free(dsp);
1072 }
1073}
1074
1075void
1076dt_scope_push(ctf_file_t *ctfp, ctf_id_t type)
1077{
1078 dt_scope_t *rsp = &yypcb->pcb_dstack;
1079 dt_scope_t *dsp = malloc(sizeof (dt_scope_t));
1080
1081 if (dsp == NULL)
1082 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1083
1084 dsp->ds_decl = rsp->ds_decl;
1085 dsp->ds_next = rsp->ds_next;
1086 dsp->ds_ident = rsp->ds_ident;
1087 dsp->ds_ctfp = ctfp;
1088 dsp->ds_type = type;
1089 dsp->ds_class = rsp->ds_class;
1090 dsp->ds_enumval = rsp->ds_enumval;
1091
1092 dt_scope_create(rsp);
1093 rsp->ds_next = dsp;
1094}
1095
1096dt_decl_t *
1097dt_scope_pop(void)
1098{
1099 dt_scope_t *rsp = &yypcb->pcb_dstack;
1100 dt_scope_t *dsp = rsp->ds_next;
1101
1102 if (dsp == NULL)
1103 longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
1104
1105 if (dsp->ds_ctfp != NULL && ctf_update(dsp->ds_ctfp) == CTF_ERR) {
1106 xyerror(D_UNKNOWN, "failed to update type definitions: %s\n",
1107 ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
1108 }
1109
1110 dt_decl_free(rsp->ds_decl);
1111 free(rsp->ds_ident);
1112
1113 rsp->ds_decl = dsp->ds_decl;
1114 rsp->ds_next = dsp->ds_next;
1115 rsp->ds_ident = dsp->ds_ident;
1116 rsp->ds_ctfp = dsp->ds_ctfp;
1117 rsp->ds_type = dsp->ds_type;
1118 rsp->ds_class = dsp->ds_class;
1119 rsp->ds_enumval = dsp->ds_enumval;
1120
1121 free(dsp);
1122 return (rsp->ds_decl);
1123}