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