xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_decl.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM  * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*13093SRoger.Faulkner@Oracle.COM 
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <strings.h>
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <limits.h>
290Sstevel@tonic-gate #include <alloca.h>
300Sstevel@tonic-gate #include <assert.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <dt_decl.h>
330Sstevel@tonic-gate #include <dt_parser.h>
340Sstevel@tonic-gate #include <dt_module.h>
350Sstevel@tonic-gate #include <dt_impl.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate static dt_decl_t *
dt_decl_check(dt_decl_t * ddp)380Sstevel@tonic-gate dt_decl_check(dt_decl_t *ddp)
390Sstevel@tonic-gate {
400Sstevel@tonic-gate 	if (ddp->dd_kind == CTF_K_UNKNOWN)
410Sstevel@tonic-gate 		return (ddp); /* nothing to check if the type is not yet set */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 	if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "char") == 0 &&
440Sstevel@tonic-gate 	    (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG))) {
450Sstevel@tonic-gate 		xyerror(D_DECL_CHARATTR, "invalid type declaration: short and "
460Sstevel@tonic-gate 		    "long may not be used with char type\n");
470Sstevel@tonic-gate 	}
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 	if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "void") == 0 &&
500Sstevel@tonic-gate 	    (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG |
510Sstevel@tonic-gate 	    (DT_DA_SIGNED | DT_DA_UNSIGNED)))) {
520Sstevel@tonic-gate 		xyerror(D_DECL_VOIDATTR, "invalid type declaration: attributes "
530Sstevel@tonic-gate 		    "may not be used with void type\n");
540Sstevel@tonic-gate 	}
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	if (ddp->dd_kind != CTF_K_INTEGER &&
570Sstevel@tonic-gate 	    (ddp->dd_attr & (DT_DA_SIGNED | DT_DA_UNSIGNED))) {
580Sstevel@tonic-gate 		xyerror(D_DECL_SIGNINT, "invalid type declaration: signed and "
590Sstevel@tonic-gate 		    "unsigned may only be used with integer type\n");
600Sstevel@tonic-gate 	}
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	if (ddp->dd_kind != CTF_K_INTEGER && ddp->dd_kind != CTF_K_FLOAT &&
630Sstevel@tonic-gate 	    (ddp->dd_attr & (DT_DA_LONG | DT_DA_LONGLONG))) {
640Sstevel@tonic-gate 		xyerror(D_DECL_LONGINT, "invalid type declaration: long and "
650Sstevel@tonic-gate 		    "long long may only be used with integer or "
660Sstevel@tonic-gate 		    "floating-point type\n");
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	return (ddp);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate 
720Sstevel@tonic-gate dt_decl_t *
dt_decl_alloc(ushort_t kind,char * name)730Sstevel@tonic-gate dt_decl_alloc(ushort_t kind, char *name)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	dt_decl_t *ddp = malloc(sizeof (dt_decl_t));
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	if (ddp == NULL)
780Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	ddp->dd_kind = kind;
810Sstevel@tonic-gate 	ddp->dd_attr = 0;
820Sstevel@tonic-gate 	ddp->dd_ctfp = NULL;
830Sstevel@tonic-gate 	ddp->dd_type = CTF_ERR;
840Sstevel@tonic-gate 	ddp->dd_name = name;
850Sstevel@tonic-gate 	ddp->dd_node = NULL;
860Sstevel@tonic-gate 	ddp->dd_next = NULL;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	return (ddp);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate void
dt_decl_free(dt_decl_t * ddp)920Sstevel@tonic-gate dt_decl_free(dt_decl_t *ddp)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	dt_decl_t *ndp;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	for (; ddp != NULL; ddp = ndp) {
970Sstevel@tonic-gate 		ndp = ddp->dd_next;
980Sstevel@tonic-gate 		free(ddp->dd_name);
990Sstevel@tonic-gate 		dt_node_list_free(&ddp->dd_node);
1000Sstevel@tonic-gate 		free(ddp);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate void
dt_decl_reset(void)1050Sstevel@tonic-gate dt_decl_reset(void)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	dt_scope_t *dsp = &yypcb->pcb_dstack;
1080Sstevel@tonic-gate 	dt_decl_t *ddp = dsp->ds_decl;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	while (ddp->dd_next != NULL) {
1110Sstevel@tonic-gate 		dsp->ds_decl = ddp->dd_next;
1120Sstevel@tonic-gate 		ddp->dd_next = NULL;
1130Sstevel@tonic-gate 		dt_decl_free(ddp);
1140Sstevel@tonic-gate 		ddp = dsp->ds_decl;
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate dt_decl_t *
dt_decl_push(dt_decl_t * ddp)1190Sstevel@tonic-gate dt_decl_push(dt_decl_t *ddp)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	dt_scope_t *dsp = &yypcb->pcb_dstack;
1220Sstevel@tonic-gate 	dt_decl_t *top = dsp->ds_decl;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if (top != NULL &&
1250Sstevel@tonic-gate 	    top->dd_kind == CTF_K_UNKNOWN && top->dd_name == NULL) {
1260Sstevel@tonic-gate 		top->dd_kind = CTF_K_INTEGER;
1270Sstevel@tonic-gate 		(void) dt_decl_check(top);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	assert(ddp->dd_next == NULL);
1310Sstevel@tonic-gate 	ddp->dd_next = top;
1320Sstevel@tonic-gate 	dsp->ds_decl = ddp;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	return (ddp);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate dt_decl_t *
dt_decl_pop(void)1380Sstevel@tonic-gate dt_decl_pop(void)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	dt_scope_t *dsp = &yypcb->pcb_dstack;
1410Sstevel@tonic-gate 	dt_decl_t *ddp = dt_decl_top();
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	dsp->ds_decl = NULL;
1440Sstevel@tonic-gate 	free(dsp->ds_ident);
1450Sstevel@tonic-gate 	dsp->ds_ident = NULL;
1460Sstevel@tonic-gate 	dsp->ds_ctfp = NULL;
1470Sstevel@tonic-gate 	dsp->ds_type = CTF_ERR;
1480Sstevel@tonic-gate 	dsp->ds_class = DT_DC_DEFAULT;
1490Sstevel@tonic-gate 	dsp->ds_enumval = -1;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	return (ddp);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate dt_decl_t *
dt_decl_pop_param(char ** idp)1550Sstevel@tonic-gate dt_decl_pop_param(char **idp)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	dt_scope_t *dsp = &yypcb->pcb_dstack;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if (dsp->ds_class != DT_DC_DEFAULT && dsp->ds_class != DT_DC_REGISTER) {
1600Sstevel@tonic-gate 		xyerror(D_DECL_PARMCLASS, "inappropriate storage class "
1610Sstevel@tonic-gate 		    "for function or associative array parameter\n");
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (idp != NULL && dt_decl_top() != NULL) {
1650Sstevel@tonic-gate 		*idp = dsp->ds_ident;
1660Sstevel@tonic-gate 		dsp->ds_ident = NULL;
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	return (dt_decl_pop());
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate dt_decl_t *
dt_decl_top(void)1730Sstevel@tonic-gate dt_decl_top(void)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (ddp == NULL)
1780Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {
1810Sstevel@tonic-gate 		ddp->dd_kind = CTF_K_INTEGER;
1820Sstevel@tonic-gate 		(void) dt_decl_check(ddp);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	return (ddp);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate dt_decl_t *
dt_decl_ident(char * name)1890Sstevel@tonic-gate dt_decl_ident(char *name)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	dt_scope_t *dsp = &yypcb->pcb_dstack;
1920Sstevel@tonic-gate 	dt_decl_t *ddp = dsp->ds_decl;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if (dsp->ds_ident != NULL) {
1950Sstevel@tonic-gate 		free(name);
1960Sstevel@tonic-gate 		xyerror(D_DECL_IDENT, "old-style declaration or "
1970Sstevel@tonic-gate 		    "incorrect type specified\n");
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	dsp->ds_ident = name;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	if (ddp == NULL)
2030Sstevel@tonic-gate 		ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	return (ddp);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate void
dt_decl_class(dt_dclass_t class)2090Sstevel@tonic-gate dt_decl_class(dt_dclass_t class)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	dt_scope_t *dsp = &yypcb->pcb_dstack;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if (dsp->ds_class != DT_DC_DEFAULT) {
2140Sstevel@tonic-gate 		xyerror(D_DECL_CLASS, "only one storage class allowed "
2150Sstevel@tonic-gate 		    "in a declaration\n");
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	dsp->ds_class = class;
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate  * Set the kind and name of the current declaration.  If none is allocated,
2230Sstevel@tonic-gate  * make a new decl and push it on to the top of our stack.  If the name or kind
2240Sstevel@tonic-gate  * is already set for the current decl, then we need to fail this declaration.
2250Sstevel@tonic-gate  * This can occur because too many types were given (e.g. "int int"), etc.
2260Sstevel@tonic-gate  */
2270Sstevel@tonic-gate dt_decl_t *
dt_decl_spec(ushort_t kind,char * name)2280Sstevel@tonic-gate dt_decl_spec(ushort_t kind, char *name)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (ddp == NULL)
2330Sstevel@tonic-gate 		return (dt_decl_push(dt_decl_alloc(kind, name)));
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/*
2360Sstevel@tonic-gate 	 * If we already have a type name specified and we see another type
2370Sstevel@tonic-gate 	 * name, this is an error if the declaration is a typedef.  If the
2380Sstevel@tonic-gate 	 * declaration is not a typedef, then the user may be trying to declare
2390Sstevel@tonic-gate 	 * a variable whose name has been returned by lex as a TNAME token:
2400Sstevel@tonic-gate 	 * call dt_decl_ident() as if the grammar's IDENT rule was matched.
2410Sstevel@tonic-gate 	 */
2420Sstevel@tonic-gate 	if (ddp->dd_name != NULL && kind == CTF_K_TYPEDEF) {
2430Sstevel@tonic-gate 		if (yypcb->pcb_dstack.ds_class != DT_DC_TYPEDEF)
2440Sstevel@tonic-gate 			return (dt_decl_ident(name));
2450Sstevel@tonic-gate 		xyerror(D_DECL_IDRED, "identifier redeclared: %s\n", name);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	if (ddp->dd_name != NULL || ddp->dd_kind != CTF_K_UNKNOWN)
2490Sstevel@tonic-gate 		xyerror(D_DECL_COMBO, "invalid type combination\n");
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	ddp->dd_kind = kind;
2520Sstevel@tonic-gate 	ddp->dd_name = name;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if (name != NULL && strchr(name, '`') != NULL) {
2550Sstevel@tonic-gate 		xyerror(D_DECL_SCOPE, "D scoping operator may not be used "
2560Sstevel@tonic-gate 		    "in a type name\n");
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	return (dt_decl_check(ddp));
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate dt_decl_t *
dt_decl_attr(ushort_t attr)2630Sstevel@tonic-gate dt_decl_attr(ushort_t attr)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	if (ddp == NULL) {
2680Sstevel@tonic-gate 		ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));
2690Sstevel@tonic-gate 		ddp->dd_attr = attr;
2700Sstevel@tonic-gate 		return (ddp);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if (attr == DT_DA_LONG && (ddp->dd_attr & DT_DA_LONG)) {
2740Sstevel@tonic-gate 		ddp->dd_attr &= ~DT_DA_LONG;
2750Sstevel@tonic-gate 		attr = DT_DA_LONGLONG;
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	ddp->dd_attr |= attr;
2790Sstevel@tonic-gate 	return (dt_decl_check(ddp));
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate  * Examine the list of formal parameters 'flist' and determine if the formal
2840Sstevel@tonic-gate  * name fnp->dn_string is defined in this list (B_TRUE) or not (B_FALSE).
2850Sstevel@tonic-gate  * If 'fnp' is in 'flist', do not search beyond 'fnp' itself in 'flist'.
2860Sstevel@tonic-gate  */
2870Sstevel@tonic-gate static int
dt_decl_protoform(dt_node_t * fnp,dt_node_t * flist)2880Sstevel@tonic-gate dt_decl_protoform(dt_node_t *fnp, dt_node_t *flist)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	dt_node_t *dnp;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	for (dnp = flist; dnp != fnp && dnp != NULL; dnp = dnp->dn_list) {
2930Sstevel@tonic-gate 		if (dnp->dn_string != NULL &&
2940Sstevel@tonic-gate 		    strcmp(dnp->dn_string, fnp->dn_string) == 0)
2950Sstevel@tonic-gate 			return (B_TRUE);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	return (B_FALSE);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate  * Common code for parsing array, function, and probe definition prototypes.
3030Sstevel@tonic-gate  * The prototype node list is specified as 'plist'.  The formal prototype
3040Sstevel@tonic-gate  * against which to compare the prototype is specified as 'flist'.  If plist
3050Sstevel@tonic-gate  * and flist are the same, we require that named parameters are unique.  If
3060Sstevel@tonic-gate  * plist and flist are different, we require that named parameters in plist
3070Sstevel@tonic-gate  * match a name that is present in flist.
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate int
dt_decl_prototype(dt_node_t * plist,dt_node_t * flist,const char * kind,uint_t flags)3100Sstevel@tonic-gate dt_decl_prototype(dt_node_t *plist,
3110Sstevel@tonic-gate     dt_node_t *flist, const char *kind, uint_t flags)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
3140Sstevel@tonic-gate 	int is_void, v = 0, i = 1;
3150Sstevel@tonic-gate 	int form = plist != flist;
3160Sstevel@tonic-gate 	dt_node_t *dnp;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	for (dnp = plist; dnp != NULL; dnp = dnp->dn_list, i++) {
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 		if (dnp->dn_type == CTF_ERR && !(flags & DT_DP_VARARGS)) {
3210Sstevel@tonic-gate 			dnerror(dnp, D_DECL_PROTO_VARARGS, "%s prototype may "
3220Sstevel@tonic-gate 			    "not use a variable-length argument list\n", kind);
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		if (dt_node_is_dynamic(dnp) && !(flags & DT_DP_DYNAMIC)) {
3260Sstevel@tonic-gate 			dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "
3270Sstevel@tonic-gate 			    "use parameter of type %s: %s, parameter #%d\n",
3280Sstevel@tonic-gate 			    kind, dt_node_type_name(dnp, n, sizeof (n)),
3290Sstevel@tonic-gate 			    dnp->dn_string ? dnp->dn_string : "(anonymous)", i);
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		is_void = dt_node_is_void(dnp);
3330Sstevel@tonic-gate 		v += is_void;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 		if (is_void && !(flags & DT_DP_VOID)) {
3360Sstevel@tonic-gate 			dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "
3370Sstevel@tonic-gate 			    "use parameter of type %s: %s, parameter #%d\n",
3380Sstevel@tonic-gate 			    kind, dt_node_type_name(dnp, n, sizeof (n)),
3390Sstevel@tonic-gate 			    dnp->dn_string ? dnp->dn_string : "(anonymous)", i);
3400Sstevel@tonic-gate 		}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		if (is_void && dnp->dn_string != NULL) {
3430Sstevel@tonic-gate 			dnerror(dnp, D_DECL_PROTO_NAME, "void parameter may "
3440Sstevel@tonic-gate 			    "not have a name: %s\n", dnp->dn_string);
3450Sstevel@tonic-gate 		}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		if (dnp->dn_string != NULL &&
3480Sstevel@tonic-gate 		    dt_decl_protoform(dnp, flist) != form) {
3490Sstevel@tonic-gate 			dnerror(dnp, D_DECL_PROTO_FORM, "parameter is "
3500Sstevel@tonic-gate 			    "%s declared in %s prototype: %s, parameter #%d\n",
3510Sstevel@tonic-gate 			    form ? "not" : "already", kind, dnp->dn_string, i);
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		if (dnp->dn_string == NULL &&
3550Sstevel@tonic-gate 		    !is_void && !(flags & DT_DP_ANON)) {
3560Sstevel@tonic-gate 			dnerror(dnp, D_DECL_PROTO_NAME, "parameter declaration "
3570Sstevel@tonic-gate 			    "requires a name: parameter #%d\n", i);
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	if (v != 0 && plist->dn_list != NULL)
3620Sstevel@tonic-gate 		xyerror(D_DECL_PROTO_VOID, "void must be sole parameter\n");
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	return (v ? 0 : i - 1); /* return zero if sole parameter is 'void' */
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate dt_decl_t *
dt_decl_array(dt_node_t * dnp)3680Sstevel@tonic-gate dt_decl_array(dt_node_t *dnp)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	dt_decl_t *ddp = dt_decl_push(dt_decl_alloc(CTF_K_ARRAY, NULL));
3710Sstevel@tonic-gate 	dt_scope_t *dsp = &yypcb->pcb_dstack;
3720Sstevel@tonic-gate 	dt_decl_t *ndp = ddp;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	/*
3750Sstevel@tonic-gate 	 * After pushing the array on to the decl stack, scan ahead for multi-
3760Sstevel@tonic-gate 	 * dimensional array declarations and push the current decl to the
3770Sstevel@tonic-gate 	 * bottom to match the resulting CTF type tree and data layout.  Refer
3780Sstevel@tonic-gate 	 * to the comments in dt_decl_type() and ISO C 6.5.2.1 for more info.
3790Sstevel@tonic-gate 	 */
3800Sstevel@tonic-gate 	while (ndp->dd_next != NULL && ndp->dd_next->dd_kind == CTF_K_ARRAY)
3810Sstevel@tonic-gate 		ndp = ndp->dd_next; /* skip to bottom-most array declaration */
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	if (ndp != ddp) {
3840Sstevel@tonic-gate 		if (dnp != NULL && dnp->dn_kind == DT_NODE_TYPE) {
3850Sstevel@tonic-gate 			xyerror(D_DECL_DYNOBJ,
3860Sstevel@tonic-gate 			    "cannot declare array of associative arrays\n");
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 		dsp->ds_decl = ddp->dd_next;
3890Sstevel@tonic-gate 		ddp->dd_next = ndp->dd_next;
3900Sstevel@tonic-gate 		ndp->dd_next = ddp;
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	if (ddp->dd_next->dd_name != NULL &&
3940Sstevel@tonic-gate 	    strcmp(ddp->dd_next->dd_name, "void") == 0)
3950Sstevel@tonic-gate 		xyerror(D_DECL_VOIDOBJ, "cannot declare array of void\n");
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (dnp != NULL && dnp->dn_kind != DT_NODE_TYPE) {
3980Sstevel@tonic-gate 		dnp = ddp->dd_node = dt_node_cook(dnp, DT_IDFLG_REF);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 		if (dt_node_is_posconst(dnp) == 0) {
4010Sstevel@tonic-gate 			xyerror(D_DECL_ARRSUB, "positive integral constant "
4020Sstevel@tonic-gate 			    "expression or tuple signature expected as "
4030Sstevel@tonic-gate 			    "array declaration subscript\n");
4040Sstevel@tonic-gate 		}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 		if (dnp->dn_value > UINT_MAX)
4070Sstevel@tonic-gate 			xyerror(D_DECL_ARRBIG, "array dimension too big\n");
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	} else if (dnp != NULL) {
4100Sstevel@tonic-gate 		ddp->dd_node = dnp;
4110Sstevel@tonic-gate 		(void) dt_decl_prototype(dnp, dnp, "array", DT_DP_ANON);
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	return (ddp);
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate  * When a function is declared, we need to fudge the decl stack a bit if the
4190Sstevel@tonic-gate  * declaration uses the function pointer (*)() syntax.  In this case, the
4200Sstevel@tonic-gate  * dt_decl_func() call occurs *after* the dt_decl_ptr() call, even though the
4210Sstevel@tonic-gate  * resulting type is "pointer to function".  To make the pointer land on top,
4220Sstevel@tonic-gate  * we check to see if 'pdp' is non-NULL and a pointer.  If it is, we search
4230Sstevel@tonic-gate  * backward for a decl tagged with DT_DA_PAREN, and if one is found, the func
4240Sstevel@tonic-gate  * decl is inserted behind this node in the decl list instead of at the top.
4250Sstevel@tonic-gate  * In all cases, the func decl's dd_next pointer is set to the decl chain
4260Sstevel@tonic-gate  * for the function's return type and the function parameter list is discarded.
4270Sstevel@tonic-gate  */
4280Sstevel@tonic-gate dt_decl_t *
dt_decl_func(dt_decl_t * pdp,dt_node_t * dnp)4290Sstevel@tonic-gate dt_decl_func(dt_decl_t *pdp, dt_node_t *dnp)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate 	dt_decl_t *ddp = dt_decl_alloc(CTF_K_FUNCTION, NULL);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	ddp->dd_node = dnp;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	(void) dt_decl_prototype(dnp, dnp, "function",
4360Sstevel@tonic-gate 	    DT_DP_VARARGS | DT_DP_VOID | DT_DP_ANON);
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	if (pdp == NULL || pdp->dd_kind != CTF_K_POINTER)
4390Sstevel@tonic-gate 		return (dt_decl_push(ddp));
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	while (pdp->dd_next != NULL && !(pdp->dd_next->dd_attr & DT_DA_PAREN))
4420Sstevel@tonic-gate 		pdp = pdp->dd_next;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	if (pdp->dd_next == NULL)
4450Sstevel@tonic-gate 		return (dt_decl_push(ddp));
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	ddp->dd_next = pdp->dd_next;
4480Sstevel@tonic-gate 	pdp->dd_next = ddp;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	return (pdp);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate dt_decl_t *
dt_decl_ptr(void)4540Sstevel@tonic-gate dt_decl_ptr(void)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	return (dt_decl_push(dt_decl_alloc(CTF_K_POINTER, NULL)));
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate dt_decl_t *
dt_decl_sou(uint_t kind,char * name)4600Sstevel@tonic-gate dt_decl_sou(uint_t kind, char *name)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate 	dt_decl_t *ddp = dt_decl_spec(kind, name);
4630Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
4640Sstevel@tonic-gate 	ctf_file_t *ctfp;
4650Sstevel@tonic-gate 	ctf_id_t type;
4660Sstevel@tonic-gate 	uint_t flag;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	if (yypcb->pcb_idepth != 0)
4690Sstevel@tonic-gate 		ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
4700Sstevel@tonic-gate 	else
4710Sstevel@tonic-gate 		ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	if (yypcb->pcb_dstack.ds_next != NULL)
4740Sstevel@tonic-gate 		flag = CTF_ADD_NONROOT;
4750Sstevel@tonic-gate 	else
4760Sstevel@tonic-gate 		flag = CTF_ADD_ROOT;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	(void) snprintf(n, sizeof (n), "%s %s",
4790Sstevel@tonic-gate 	    kind == CTF_K_STRUCT ? "struct" : "union",
4800Sstevel@tonic-gate 	    name == NULL ? "(anon)" : name);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR &&
4830Sstevel@tonic-gate 	    ctf_type_kind(ctfp, type) != CTF_K_FORWARD)
4840Sstevel@tonic-gate 		xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	if (kind == CTF_K_STRUCT)
4870Sstevel@tonic-gate 		type = ctf_add_struct(ctfp, flag, name);
4880Sstevel@tonic-gate 	else
4890Sstevel@tonic-gate 		type = ctf_add_union(ctfp, flag, name);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	if (type == CTF_ERR || ctf_update(ctfp) == CTF_ERR) {
4920Sstevel@tonic-gate 		xyerror(D_UNKNOWN, "failed to define %s: %s\n",
4930Sstevel@tonic-gate 		    n, ctf_errmsg(ctf_errno(ctfp)));
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	ddp->dd_ctfp = ctfp;
4970Sstevel@tonic-gate 	ddp->dd_type = type;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	dt_scope_push(ctfp, type);
5000Sstevel@tonic-gate 	return (ddp);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate void
dt_decl_member(dt_node_t * dnp)5040Sstevel@tonic-gate dt_decl_member(dt_node_t *dnp)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate 	dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;
5070Sstevel@tonic-gate 	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
5080Sstevel@tonic-gate 	char *ident = yypcb->pcb_dstack.ds_ident;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	const char *idname = ident ? ident : "(anon)";
5110Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
5140Sstevel@tonic-gate 	ctf_encoding_t cte;
5150Sstevel@tonic-gate 	ctf_id_t base;
5160Sstevel@tonic-gate 	uint_t kind;
5170Sstevel@tonic-gate 	ssize_t size;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	if (dsp == NULL)
5200Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	if (ddp == NULL)
5230Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	if (dnp == NULL && ident == NULL)
5260Sstevel@tonic-gate 		xyerror(D_DECL_MNAME, "member declaration requires a name\n");
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {
5290Sstevel@tonic-gate 		ddp->dd_kind = CTF_K_INTEGER;
5300Sstevel@tonic-gate 		(void) dt_decl_check(ddp);
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	if (dt_decl_type(ddp, &dtt) != 0)
5340Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	if (ident != NULL && strchr(ident, '`') != NULL) {
5370Sstevel@tonic-gate 		xyerror(D_DECL_SCOPE, "D scoping operator may not be used "
5380Sstevel@tonic-gate 		    "in a member name (%s)\n", ident);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	if (dtt.dtt_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
5420Sstevel@tonic-gate 	    dtt.dtt_type == DT_DYN_TYPE(yypcb->pcb_hdl)) {
5430Sstevel@tonic-gate 		xyerror(D_DECL_DYNOBJ,
5440Sstevel@tonic-gate 		    "cannot have dynamic member: %s\n", ident);
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	base = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
5480Sstevel@tonic-gate 	kind = ctf_type_kind(dtt.dtt_ctfp, base);
5490Sstevel@tonic-gate 	size = ctf_type_size(dtt.dtt_ctfp, base);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	if (kind == CTF_K_FORWARD || ((kind == CTF_K_STRUCT ||
5520Sstevel@tonic-gate 	    kind == CTF_K_UNION) && size == 0)) {
5530Sstevel@tonic-gate 		xyerror(D_DECL_INCOMPLETE, "incomplete struct/union/enum %s: "
5540Sstevel@tonic-gate 		    "%s\n", dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
5550Sstevel@tonic-gate 		    n, sizeof (n)), ident);
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	if (size == 0)
5590Sstevel@tonic-gate 		xyerror(D_DECL_VOIDOBJ, "cannot have void member: %s\n", ident);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	/*
5620Sstevel@tonic-gate 	 * If a bit-field qualifier was part of the member declaration, create
5630Sstevel@tonic-gate 	 * a new integer type of the same name and attributes as the base type
5640Sstevel@tonic-gate 	 * and size equal to the specified number of bits.  We reset 'dtt' to
5650Sstevel@tonic-gate 	 * refer to this new bit-field type and continue on to add the member.
5660Sstevel@tonic-gate 	 */
5670Sstevel@tonic-gate 	if (dnp != NULL) {
5680Sstevel@tonic-gate 		dnp = dt_node_cook(dnp, DT_IDFLG_REF);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		/*
5710Sstevel@tonic-gate 		 * A bit-field member with no declarator is permitted to have
5720Sstevel@tonic-gate 		 * size zero and indicates that no more fields are to be packed
5730Sstevel@tonic-gate 		 * into the current storage unit.  We ignore these directives
5740Sstevel@tonic-gate 		 * as the underlying ctf code currently does so for all fields.
5750Sstevel@tonic-gate 		 */
5760Sstevel@tonic-gate 		if (ident == NULL && dnp->dn_kind == DT_NODE_INT &&
5770Sstevel@tonic-gate 		    dnp->dn_value == 0) {
5780Sstevel@tonic-gate 			dt_node_free(dnp);
5790Sstevel@tonic-gate 			goto done;
5800Sstevel@tonic-gate 		}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 		if (dt_node_is_posconst(dnp) == 0) {
5830Sstevel@tonic-gate 			xyerror(D_DECL_BFCONST, "positive integral constant "
5840Sstevel@tonic-gate 			    "expression expected as bit-field size\n");
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 		if (ctf_type_kind(dtt.dtt_ctfp, base) != CTF_K_INTEGER ||
5880Sstevel@tonic-gate 		    ctf_type_encoding(dtt.dtt_ctfp, base, &cte) == CTF_ERR ||
5890Sstevel@tonic-gate 		    IS_VOID(cte)) {
5900Sstevel@tonic-gate 			xyerror(D_DECL_BFTYPE, "invalid type for "
5910Sstevel@tonic-gate 			    "bit-field: %s\n", idname);
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 		if (dnp->dn_value > cte.cte_bits) {
5950Sstevel@tonic-gate 			xyerror(D_DECL_BFSIZE, "bit-field too big "
5960Sstevel@tonic-gate 			    "for type: %s\n", idname);
5970Sstevel@tonic-gate 		}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 		cte.cte_offset = 0;
6000Sstevel@tonic-gate 		cte.cte_bits = (uint_t)dnp->dn_value;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 		dtt.dtt_type = ctf_add_integer(dsp->ds_ctfp,
6030Sstevel@tonic-gate 		    CTF_ADD_NONROOT, ctf_type_name(dtt.dtt_ctfp,
6040Sstevel@tonic-gate 		    dtt.dtt_type, n, sizeof (n)), &cte);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 		if (dtt.dtt_type == CTF_ERR ||
6070Sstevel@tonic-gate 		    ctf_update(dsp->ds_ctfp) == CTF_ERR) {
6080Sstevel@tonic-gate 			xyerror(D_UNKNOWN, "failed to create type for "
6090Sstevel@tonic-gate 			    "member '%s': %s\n", idname,
6100Sstevel@tonic-gate 			    ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
6110Sstevel@tonic-gate 		}
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 		dtt.dtt_ctfp = dsp->ds_ctfp;
6140Sstevel@tonic-gate 		dt_node_free(dnp);
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	/*
6180Sstevel@tonic-gate 	 * If the member type is not defined in the same CTF container as the
6190Sstevel@tonic-gate 	 * one associated with the current scope (i.e. the container for the
6200Sstevel@tonic-gate 	 * struct or union itself) or its parent, copy the member type into
6210Sstevel@tonic-gate 	 * this container and reset dtt to refer to the copied type.
6220Sstevel@tonic-gate 	 */
6230Sstevel@tonic-gate 	if (dtt.dtt_ctfp != dsp->ds_ctfp &&
6240Sstevel@tonic-gate 	    dtt.dtt_ctfp != ctf_parent_file(dsp->ds_ctfp)) {
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 		dtt.dtt_type = ctf_add_type(dsp->ds_ctfp,
6270Sstevel@tonic-gate 		    dtt.dtt_ctfp, dtt.dtt_type);
6280Sstevel@tonic-gate 		dtt.dtt_ctfp = dsp->ds_ctfp;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 		if (dtt.dtt_type == CTF_ERR ||
6310Sstevel@tonic-gate 		    ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
6320Sstevel@tonic-gate 			xyerror(D_UNKNOWN, "failed to copy type of '%s': %s\n",
6330Sstevel@tonic-gate 			    idname, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
6340Sstevel@tonic-gate 		}
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	if (ctf_add_member(dsp->ds_ctfp, dsp->ds_type,
6380Sstevel@tonic-gate 	    ident, dtt.dtt_type) == CTF_ERR) {
6390Sstevel@tonic-gate 		xyerror(D_UNKNOWN, "failed to define member '%s': %s\n",
6400Sstevel@tonic-gate 		    idname, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate done:
6440Sstevel@tonic-gate 	free(ident);
6450Sstevel@tonic-gate 	yypcb->pcb_dstack.ds_ident = NULL;
6460Sstevel@tonic-gate 	dt_decl_reset();
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate /*ARGSUSED*/
6500Sstevel@tonic-gate static int
dt_decl_hasmembers(const char * name,int value,void * private)6510Sstevel@tonic-gate dt_decl_hasmembers(const char *name, int value, void *private)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	return (1); /* abort search and return true if a member exists */
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate dt_decl_t *
dt_decl_enum(char * name)6570Sstevel@tonic-gate dt_decl_enum(char *name)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate 	dt_decl_t *ddp = dt_decl_spec(CTF_K_ENUM, name);
6600Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
6610Sstevel@tonic-gate 	ctf_file_t *ctfp;
6620Sstevel@tonic-gate 	ctf_id_t type;
6630Sstevel@tonic-gate 	uint_t flag;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	if (yypcb->pcb_idepth != 0)
6660Sstevel@tonic-gate 		ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
6670Sstevel@tonic-gate 	else
6680Sstevel@tonic-gate 		ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if (yypcb->pcb_dstack.ds_next != NULL)
6710Sstevel@tonic-gate 		flag = CTF_ADD_NONROOT;
6720Sstevel@tonic-gate 	else
6730Sstevel@tonic-gate 		flag = CTF_ADD_ROOT;
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	(void) snprintf(n, sizeof (n), "enum %s", name ? name : "(anon)");
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR) {
6780Sstevel@tonic-gate 		if (ctf_enum_iter(ctfp, type, dt_decl_hasmembers, NULL))
6790Sstevel@tonic-gate 			xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);
6800Sstevel@tonic-gate 	} else if ((type = ctf_add_enum(ctfp, flag, name)) == CTF_ERR) {
6810Sstevel@tonic-gate 		xyerror(D_UNKNOWN, "failed to define %s: %s\n",
6820Sstevel@tonic-gate 		    n, ctf_errmsg(ctf_errno(ctfp)));
6830Sstevel@tonic-gate 	}
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	ddp->dd_ctfp = ctfp;
6860Sstevel@tonic-gate 	ddp->dd_type = type;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	dt_scope_push(ctfp, type);
6890Sstevel@tonic-gate 	return (ddp);
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate void
dt_decl_enumerator(char * s,dt_node_t * dnp)6930Sstevel@tonic-gate dt_decl_enumerator(char *s, dt_node_t *dnp)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate 	dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;
6960Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	dt_idnode_t *inp;
6990Sstevel@tonic-gate 	dt_ident_t *idp;
7000Sstevel@tonic-gate 	char *name;
7010Sstevel@tonic-gate 	int value;
7020Sstevel@tonic-gate 
703*13093SRoger.Faulkner@Oracle.COM 	name = strdupa(s);
7040Sstevel@tonic-gate 	free(s);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	if (dsp == NULL)
7070Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	assert(dsp->ds_decl->dd_kind == CTF_K_ENUM);
7100Sstevel@tonic-gate 	value = dsp->ds_enumval + 1; /* default is previous value plus one */
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	if (strchr(name, '`') != NULL) {
7130Sstevel@tonic-gate 		xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
7140Sstevel@tonic-gate 		    "an enumerator name (%s)\n", name);
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	/*
7180Sstevel@tonic-gate 	 * If the enumerator is being assigned a value, cook and check the node
7190Sstevel@tonic-gate 	 * and then free it after we get the value.  We also permit references
7200Sstevel@tonic-gate 	 * to identifiers which are previously defined enumerators in the type.
7210Sstevel@tonic-gate 	 */
7220Sstevel@tonic-gate 	if (dnp != NULL) {
7230Sstevel@tonic-gate 		if (dnp->dn_kind != DT_NODE_IDENT || ctf_enum_value(
7240Sstevel@tonic-gate 		    dsp->ds_ctfp, dsp->ds_type, dnp->dn_string, &value) != 0) {
7250Sstevel@tonic-gate 			dnp = dt_node_cook(dnp, DT_IDFLG_REF);
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 			if (dnp->dn_kind != DT_NODE_INT) {
7280Sstevel@tonic-gate 				xyerror(D_DECL_ENCONST, "enumerator '%s' must "
7290Sstevel@tonic-gate 				    "be assigned to an integral constant "
7300Sstevel@tonic-gate 				    "expression\n", name);
7310Sstevel@tonic-gate 			}
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 			if ((intmax_t)dnp->dn_value > INT_MAX ||
7340Sstevel@tonic-gate 			    (intmax_t)dnp->dn_value < INT_MIN) {
7350Sstevel@tonic-gate 				xyerror(D_DECL_ENOFLOW, "enumerator '%s' value "
7360Sstevel@tonic-gate 				    "overflows INT_MAX (%d)\n", name, INT_MAX);
7370Sstevel@tonic-gate 			}
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 			value = (int)dnp->dn_value;
7400Sstevel@tonic-gate 		}
7410Sstevel@tonic-gate 		dt_node_free(dnp);
7420Sstevel@tonic-gate 	}
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	if (ctf_add_enumerator(dsp->ds_ctfp, dsp->ds_type,
7450Sstevel@tonic-gate 	    name, value) == CTF_ERR || ctf_update(dsp->ds_ctfp) == CTF_ERR) {
7460Sstevel@tonic-gate 		xyerror(D_UNKNOWN, "failed to define enumerator '%s': %s\n",
7470Sstevel@tonic-gate 		    name, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	dsp->ds_enumval = value; /* save most recent value */
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	/*
7530Sstevel@tonic-gate 	 * If the enumerator name matches an identifier in the global scope,
7540Sstevel@tonic-gate 	 * flag this as an error.  We only do this for "D" enumerators to
7550Sstevel@tonic-gate 	 * prevent "C" header file enumerators from conflicting with the ever-
7560Sstevel@tonic-gate 	 * growing list of D built-in global variables and inlines.  If a "C"
7570Sstevel@tonic-gate 	 * enumerator conflicts with a global identifier, we add the enumerator
7580Sstevel@tonic-gate 	 * but do not insert a corresponding inline (i.e. the D variable wins).
7590Sstevel@tonic-gate 	 */
7600Sstevel@tonic-gate 	if (dt_idstack_lookup(&yypcb->pcb_globals, name) != NULL) {
7610Sstevel@tonic-gate 		if (dsp->ds_ctfp == dtp->dt_ddefs->dm_ctfp) {
7620Sstevel@tonic-gate 			xyerror(D_DECL_IDRED,
7630Sstevel@tonic-gate 			    "identifier redeclared: %s\n", name);
7640Sstevel@tonic-gate 		} else
7650Sstevel@tonic-gate 			return;
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	dt_dprintf("add global enumerator %s = %d\n", name, value);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	idp = dt_idhash_insert(dtp->dt_globals, name, DT_IDENT_ENUM,
7710Sstevel@tonic-gate 	    DT_IDFLG_INLINE | DT_IDFLG_REF, 0, _dtrace_defattr, 0,
7720Sstevel@tonic-gate 	    &dt_idops_inline, NULL, dtp->dt_gen);
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	if (idp == NULL)
7750Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	yyintprefix = 0;
7780Sstevel@tonic-gate 	yyintsuffix[0] = '\0';
7790Sstevel@tonic-gate 	yyintdecimal = 0;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	dnp = dt_node_int(value);
7820Sstevel@tonic-gate 	dt_node_type_assign(dnp, dsp->ds_ctfp, dsp->ds_type);
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
7850Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	/*
7880Sstevel@tonic-gate 	 * Remove the INT node from the node allocation list and store it in
7890Sstevel@tonic-gate 	 * din_list and din_root so it persists with and is freed by the ident.
7900Sstevel@tonic-gate 	 */
7910Sstevel@tonic-gate 	assert(yypcb->pcb_list == dnp);
7920Sstevel@tonic-gate 	yypcb->pcb_list = dnp->dn_link;
7930Sstevel@tonic-gate 	dnp->dn_link = NULL;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	bzero(inp, sizeof (dt_idnode_t));
7960Sstevel@tonic-gate 	inp->din_list = dnp;
7970Sstevel@tonic-gate 	inp->din_root = dnp;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	idp->di_iarg = inp;
8000Sstevel@tonic-gate 	idp->di_ctfp = dsp->ds_ctfp;
8010Sstevel@tonic-gate 	idp->di_type = dsp->ds_type;
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate /*
8050Sstevel@tonic-gate  * Look up the type corresponding to the specified decl stack.  The scoping of
8060Sstevel@tonic-gate  * the underlying type names is handled by dt_type_lookup().  We build up the
8070Sstevel@tonic-gate  * name from the specified string and prefixes and then lookup the type.  If
8080Sstevel@tonic-gate  * we fail, an errmsg is saved and the caller must abort with EDT_COMPILER.
8090Sstevel@tonic-gate  */
8100Sstevel@tonic-gate int
dt_decl_type(dt_decl_t * ddp,dtrace_typeinfo_t * tip)8110Sstevel@tonic-gate dt_decl_type(dt_decl_t *ddp, dtrace_typeinfo_t *tip)
8120Sstevel@tonic-gate {
8130Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	dt_module_t *dmp;
8160Sstevel@tonic-gate 	ctf_arinfo_t r;
8170Sstevel@tonic-gate 	ctf_id_t type;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
8200Sstevel@tonic-gate 	uint_t flag;
8210Sstevel@tonic-gate 	char *name;
8220Sstevel@tonic-gate 	int rv;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	/*
8250Sstevel@tonic-gate 	 * Based on our current #include depth and decl stack depth, determine
8260Sstevel@tonic-gate 	 * which dynamic CTF module and scope to use when adding any new types.
8270Sstevel@tonic-gate 	 */
8280Sstevel@tonic-gate 	dmp = yypcb->pcb_idepth ? dtp->dt_cdefs : dtp->dt_ddefs;
8290Sstevel@tonic-gate 	flag = yypcb->pcb_dstack.ds_next ? CTF_ADD_NONROOT : CTF_ADD_ROOT;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	/*
8320Sstevel@tonic-gate 	 * If we have already cached a CTF type for this decl, then we just
8330Sstevel@tonic-gate 	 * return the type information for the cached type.
8340Sstevel@tonic-gate 	 */
8350Sstevel@tonic-gate 	if (ddp->dd_ctfp != NULL &&
8360Sstevel@tonic-gate 	    (dmp = dt_module_lookup_by_ctf(dtp, ddp->dd_ctfp)) != NULL) {
8370Sstevel@tonic-gate 		tip->dtt_object = dmp->dm_name;
8380Sstevel@tonic-gate 		tip->dtt_ctfp = ddp->dd_ctfp;
8390Sstevel@tonic-gate 		tip->dtt_type = ddp->dd_type;
8400Sstevel@tonic-gate 		return (0);
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	/*
8440Sstevel@tonic-gate 	 * Currently CTF treats all function pointers identically.  We cache a
8450Sstevel@tonic-gate 	 * representative ID of kind CTF_K_FUNCTION and just return that type.
8460Sstevel@tonic-gate 	 * If we want to support full function declarations, dd_next refers to
8470Sstevel@tonic-gate 	 * the declaration of the function return type, and the parameter list
8480Sstevel@tonic-gate 	 * should be parsed and hung off a new pointer inside of this decl.
8490Sstevel@tonic-gate 	 */
8500Sstevel@tonic-gate 	if (ddp->dd_kind == CTF_K_FUNCTION) {
8510Sstevel@tonic-gate 		tip->dtt_object = dtp->dt_ddefs->dm_name;
8520Sstevel@tonic-gate 		tip->dtt_ctfp = DT_FUNC_CTFP(dtp);
8530Sstevel@tonic-gate 		tip->dtt_type = DT_FUNC_TYPE(dtp);
8540Sstevel@tonic-gate 		return (0);
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/*
8580Sstevel@tonic-gate 	 * If the decl is a pointer, resolve the rest of the stack by calling
8590Sstevel@tonic-gate 	 * dt_decl_type() recursively and then compute a pointer to the result.
8600Sstevel@tonic-gate 	 * Similar to the code above, we return a cached id for function ptrs.
8610Sstevel@tonic-gate 	 */
8620Sstevel@tonic-gate 	if (ddp->dd_kind == CTF_K_POINTER) {
8630Sstevel@tonic-gate 		if (ddp->dd_next->dd_kind == CTF_K_FUNCTION) {
8640Sstevel@tonic-gate 			tip->dtt_object = dtp->dt_ddefs->dm_name;
8650Sstevel@tonic-gate 			tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
8660Sstevel@tonic-gate 			tip->dtt_type = DT_FPTR_TYPE(dtp);
8670Sstevel@tonic-gate 			return (0);
8680Sstevel@tonic-gate 		}
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 		if ((rv = dt_decl_type(ddp->dd_next, tip)) == 0 &&
8710Sstevel@tonic-gate 		    (rv = dt_type_pointer(tip)) != 0) {
8720Sstevel@tonic-gate 			xywarn(D_UNKNOWN, "cannot find type: %s*: %s\n",
8730Sstevel@tonic-gate 			    dt_type_name(tip->dtt_ctfp, tip->dtt_type,
8740Sstevel@tonic-gate 			    n, sizeof (n)), ctf_errmsg(dtp->dt_ctferr));
8750Sstevel@tonic-gate 		}
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 		return (rv);
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	/*
8810Sstevel@tonic-gate 	 * If the decl is an array, we must find the base type and then call
8820Sstevel@tonic-gate 	 * dt_decl_type() recursively and then build an array of the result.
8830Sstevel@tonic-gate 	 * The C and D multi-dimensional array syntax requires that consecutive
8840Sstevel@tonic-gate 	 * array declarations be processed from right-to-left (i.e. top-down
8850Sstevel@tonic-gate 	 * from the perspective of the declaration stack).  For example, an
8860Sstevel@tonic-gate 	 * array declaration such as int x[3][5] is stored on the stack as:
8870Sstevel@tonic-gate 	 *
8880Sstevel@tonic-gate 	 * (bottom) NULL <- ( INT "int" ) <- ( ARR [3] ) <- ( ARR [5] ) (top)
8890Sstevel@tonic-gate 	 *
8900Sstevel@tonic-gate 	 * but means that x is declared to be an array of 3 objects each of
8910Sstevel@tonic-gate 	 * which is an array of 5 integers, or in CTF representation:
8920Sstevel@tonic-gate 	 *
8930Sstevel@tonic-gate 	 * type T1:( content=int, nelems=5 ) type T2:( content=T1, nelems=3 )
8940Sstevel@tonic-gate 	 *
8950Sstevel@tonic-gate 	 * For more details, refer to K&R[5.7] and ISO C 6.5.2.1.  Rather than
8960Sstevel@tonic-gate 	 * overcomplicate the implementation of dt_decl_type(), we push array
8970Sstevel@tonic-gate 	 * declarations down into the stack in dt_decl_array(), above, so that
8980Sstevel@tonic-gate 	 * by the time dt_decl_type() is called, the decl stack looks like:
8990Sstevel@tonic-gate 	 *
9000Sstevel@tonic-gate 	 * (bottom) NULL <- ( INT "int" ) <- ( ARR [5] ) <- ( ARR [3] ) (top)
9010Sstevel@tonic-gate 	 *
9020Sstevel@tonic-gate 	 * which permits a straightforward recursive descent of the decl stack
9030Sstevel@tonic-gate 	 * to build the corresponding CTF type tree in the appropriate order.
9040Sstevel@tonic-gate 	 */
9050Sstevel@tonic-gate 	if (ddp->dd_kind == CTF_K_ARRAY) {
9060Sstevel@tonic-gate 		/*
9070Sstevel@tonic-gate 		 * If the array decl has a parameter list associated with it,
9080Sstevel@tonic-gate 		 * this is an associative array declaration: return <DYN>.
9090Sstevel@tonic-gate 		 */
9100Sstevel@tonic-gate 		if (ddp->dd_node != NULL &&
9110Sstevel@tonic-gate 		    ddp->dd_node->dn_kind == DT_NODE_TYPE) {
9120Sstevel@tonic-gate 			tip->dtt_object = dtp->dt_ddefs->dm_name;
9130Sstevel@tonic-gate 			tip->dtt_ctfp = DT_DYN_CTFP(dtp);
9140Sstevel@tonic-gate 			tip->dtt_type = DT_DYN_TYPE(dtp);
9150Sstevel@tonic-gate 			return (0);
9160Sstevel@tonic-gate 		}
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 		if ((rv = dt_decl_type(ddp->dd_next, tip)) != 0)
9190Sstevel@tonic-gate 			return (rv);
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 		/*
9220Sstevel@tonic-gate 		 * If the array base type is not defined in the target
9230Sstevel@tonic-gate 		 * container or its parent, copy the type to the target
9240Sstevel@tonic-gate 		 * container and reset dtt_ctfp and dtt_type to the copy.
9250Sstevel@tonic-gate 		 */
9260Sstevel@tonic-gate 		if (tip->dtt_ctfp != dmp->dm_ctfp &&
9270Sstevel@tonic-gate 		    tip->dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 			tip->dtt_type = ctf_add_type(dmp->dm_ctfp,
9300Sstevel@tonic-gate 			    tip->dtt_ctfp, tip->dtt_type);
9310Sstevel@tonic-gate 			tip->dtt_ctfp = dmp->dm_ctfp;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 			if (tip->dtt_type == CTF_ERR ||
9340Sstevel@tonic-gate 			    ctf_update(tip->dtt_ctfp) == CTF_ERR) {
9350Sstevel@tonic-gate 				xywarn(D_UNKNOWN, "failed to copy type: %s\n",
9360Sstevel@tonic-gate 				    ctf_errmsg(ctf_errno(tip->dtt_ctfp)));
9370Sstevel@tonic-gate 				return (-1);
9380Sstevel@tonic-gate 			}
9390Sstevel@tonic-gate 		}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 		/*
9420Sstevel@tonic-gate 		 * The array index type is irrelevant in C and D: just set it
9430Sstevel@tonic-gate 		 * to "long" for all array types that we create on-the-fly.
9440Sstevel@tonic-gate 		 */
9450Sstevel@tonic-gate 		r.ctr_contents = tip->dtt_type;
9460Sstevel@tonic-gate 		r.ctr_index = ctf_lookup_by_name(tip->dtt_ctfp, "long");
9470Sstevel@tonic-gate 		r.ctr_nelems = ddp->dd_node ?
9480Sstevel@tonic-gate 		    (uint_t)ddp->dd_node->dn_value : 0;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 		tip->dtt_object = dmp->dm_name;
9510Sstevel@tonic-gate 		tip->dtt_ctfp = dmp->dm_ctfp;
9520Sstevel@tonic-gate 		tip->dtt_type = ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &r);
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 		if (tip->dtt_type == CTF_ERR ||
9550Sstevel@tonic-gate 		    ctf_update(tip->dtt_ctfp) == CTF_ERR) {
9560Sstevel@tonic-gate 			xywarn(D_UNKNOWN, "failed to create array type: %s\n",
9570Sstevel@tonic-gate 			    ctf_errmsg(ctf_errno(tip->dtt_ctfp)));
9580Sstevel@tonic-gate 			return (-1);
9590Sstevel@tonic-gate 		}
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 		return (0);
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	/*
9650Sstevel@tonic-gate 	 * Allocate space for the type name and enough space for the maximum
9660Sstevel@tonic-gate 	 * additional text ("unsigned long long \0" requires 20 more bytes).
9670Sstevel@tonic-gate 	 */
9680Sstevel@tonic-gate 	name = alloca(ddp->dd_name ? strlen(ddp->dd_name) + 20 : 20);
9690Sstevel@tonic-gate 	name[0] = '\0';
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	switch (ddp->dd_kind) {
9720Sstevel@tonic-gate 	case CTF_K_INTEGER:
9730Sstevel@tonic-gate 	case CTF_K_FLOAT:
9740Sstevel@tonic-gate 		if (ddp->dd_attr & DT_DA_SIGNED)
9750Sstevel@tonic-gate 			(void) strcat(name, "signed ");
9760Sstevel@tonic-gate 		if (ddp->dd_attr & DT_DA_UNSIGNED)
9770Sstevel@tonic-gate 			(void) strcat(name, "unsigned ");
9780Sstevel@tonic-gate 		if (ddp->dd_attr & DT_DA_SHORT)
9790Sstevel@tonic-gate 			(void) strcat(name, "short ");
9800Sstevel@tonic-gate 		if (ddp->dd_attr & DT_DA_LONG)
9810Sstevel@tonic-gate 			(void) strcat(name, "long ");
9820Sstevel@tonic-gate 		if (ddp->dd_attr & DT_DA_LONGLONG)
9830Sstevel@tonic-gate 			(void) strcat(name, "long long ");
9840Sstevel@tonic-gate 		if (ddp->dd_attr == 0 && ddp->dd_name == NULL)
9850Sstevel@tonic-gate 			(void) strcat(name, "int");
9860Sstevel@tonic-gate 		break;
9870Sstevel@tonic-gate 	case CTF_K_STRUCT:
9880Sstevel@tonic-gate 		(void) strcpy(name, "struct ");
9890Sstevel@tonic-gate 		break;
9900Sstevel@tonic-gate 	case CTF_K_UNION:
9910Sstevel@tonic-gate 		(void) strcpy(name, "union ");
9920Sstevel@tonic-gate 		break;
9930Sstevel@tonic-gate 	case CTF_K_ENUM:
9940Sstevel@tonic-gate 		(void) strcpy(name, "enum ");
9950Sstevel@tonic-gate 		break;
9960Sstevel@tonic-gate 	case CTF_K_TYPEDEF:
9970Sstevel@tonic-gate 		break;
9980Sstevel@tonic-gate 	default:
9990Sstevel@tonic-gate 		xywarn(D_UNKNOWN, "internal error -- "
10000Sstevel@tonic-gate 		    "bad decl kind %u\n", ddp->dd_kind);
10010Sstevel@tonic-gate 		return (-1);
10020Sstevel@tonic-gate 	}
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	/*
10050Sstevel@tonic-gate 	 * Add dd_name unless a short, long, or long long is explicitly
10060Sstevel@tonic-gate 	 * suffixed by int.  We use the C/CTF canonical names for integers.
10070Sstevel@tonic-gate 	 */
10080Sstevel@tonic-gate 	if (ddp->dd_name != NULL && (ddp->dd_kind != CTF_K_INTEGER ||
10090Sstevel@tonic-gate 	    (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG)) == 0))
10100Sstevel@tonic-gate 		(void) strcat(name, ddp->dd_name);
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	/*
10130Sstevel@tonic-gate 	 * Lookup the type.  If we find it, we're done.  Otherwise create a
10140Sstevel@tonic-gate 	 * forward tag for the type if it is a struct, union, or enum.  If
10150Sstevel@tonic-gate 	 * we can't find it and we can't create a tag, return failure.
10160Sstevel@tonic-gate 	 */
10170Sstevel@tonic-gate 	if ((rv = dt_type_lookup(name, tip)) == 0)
10180Sstevel@tonic-gate 		return (rv);
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	switch (ddp->dd_kind) {
10210Sstevel@tonic-gate 	case CTF_K_STRUCT:
10220Sstevel@tonic-gate 	case CTF_K_UNION:
10230Sstevel@tonic-gate 	case CTF_K_ENUM:
10240Sstevel@tonic-gate 		type = ctf_add_forward(dmp->dm_ctfp, flag,
10250Sstevel@tonic-gate 		    ddp->dd_name, ddp->dd_kind);
10260Sstevel@tonic-gate 		break;
10270Sstevel@tonic-gate 	default:
10280Sstevel@tonic-gate 		xywarn(D_UNKNOWN, "failed to resolve type %s: %s\n", name,
10290Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
10300Sstevel@tonic-gate 		return (rv);
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
10340Sstevel@tonic-gate 		xywarn(D_UNKNOWN, "failed to add forward tag for %s: %s\n",
10350Sstevel@tonic-gate 		    name, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
10360Sstevel@tonic-gate 		return (-1);
10370Sstevel@tonic-gate 	}
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	ddp->dd_ctfp = dmp->dm_ctfp;
10400Sstevel@tonic-gate 	ddp->dd_type = type;
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	tip->dtt_object = dmp->dm_name;
10430Sstevel@tonic-gate 	tip->dtt_ctfp = dmp->dm_ctfp;
10440Sstevel@tonic-gate 	tip->dtt_type = type;
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	return (0);
10470Sstevel@tonic-gate }
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate void
dt_scope_create(dt_scope_t * dsp)10500Sstevel@tonic-gate dt_scope_create(dt_scope_t *dsp)
10510Sstevel@tonic-gate {
10520Sstevel@tonic-gate 	dsp->ds_decl = NULL;
10530Sstevel@tonic-gate 	dsp->ds_next = NULL;
10540Sstevel@tonic-gate 	dsp->ds_ident = NULL;
10550Sstevel@tonic-gate 	dsp->ds_ctfp = NULL;
10560Sstevel@tonic-gate 	dsp->ds_type = CTF_ERR;
10570Sstevel@tonic-gate 	dsp->ds_class = DT_DC_DEFAULT;
10580Sstevel@tonic-gate 	dsp->ds_enumval = -1;
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate void
dt_scope_destroy(dt_scope_t * dsp)10620Sstevel@tonic-gate dt_scope_destroy(dt_scope_t *dsp)
10630Sstevel@tonic-gate {
10640Sstevel@tonic-gate 	dt_scope_t *nsp;
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	for (; dsp != NULL; dsp = nsp) {
10670Sstevel@tonic-gate 		dt_decl_free(dsp->ds_decl);
10680Sstevel@tonic-gate 		free(dsp->ds_ident);
10690Sstevel@tonic-gate 		nsp = dsp->ds_next;
10700Sstevel@tonic-gate 		if (dsp != &yypcb->pcb_dstack)
10710Sstevel@tonic-gate 			free(dsp);
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate }
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate void
dt_scope_push(ctf_file_t * ctfp,ctf_id_t type)10760Sstevel@tonic-gate dt_scope_push(ctf_file_t *ctfp, ctf_id_t type)
10770Sstevel@tonic-gate {
10780Sstevel@tonic-gate 	dt_scope_t *rsp = &yypcb->pcb_dstack;
10790Sstevel@tonic-gate 	dt_scope_t *dsp = malloc(sizeof (dt_scope_t));
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	if (dsp == NULL)
10820Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	dsp->ds_decl = rsp->ds_decl;
10850Sstevel@tonic-gate 	dsp->ds_next = rsp->ds_next;
10860Sstevel@tonic-gate 	dsp->ds_ident = rsp->ds_ident;
10870Sstevel@tonic-gate 	dsp->ds_ctfp = ctfp;
10880Sstevel@tonic-gate 	dsp->ds_type = type;
10890Sstevel@tonic-gate 	dsp->ds_class = rsp->ds_class;
10900Sstevel@tonic-gate 	dsp->ds_enumval = rsp->ds_enumval;
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	dt_scope_create(rsp);
10930Sstevel@tonic-gate 	rsp->ds_next = dsp;
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate dt_decl_t *
dt_scope_pop(void)10970Sstevel@tonic-gate dt_scope_pop(void)
10980Sstevel@tonic-gate {
10990Sstevel@tonic-gate 	dt_scope_t *rsp = &yypcb->pcb_dstack;
11000Sstevel@tonic-gate 	dt_scope_t *dsp = rsp->ds_next;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	if (dsp == NULL)
11030Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	if (dsp->ds_ctfp != NULL && ctf_update(dsp->ds_ctfp) == CTF_ERR) {
11060Sstevel@tonic-gate 		xyerror(D_UNKNOWN, "failed to update type definitions: %s\n",
11070Sstevel@tonic-gate 		    ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	dt_decl_free(rsp->ds_decl);
11110Sstevel@tonic-gate 	free(rsp->ds_ident);
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	rsp->ds_decl = dsp->ds_decl;
11140Sstevel@tonic-gate 	rsp->ds_next = dsp->ds_next;
11150Sstevel@tonic-gate 	rsp->ds_ident = dsp->ds_ident;
11160Sstevel@tonic-gate 	rsp->ds_ctfp = dsp->ds_ctfp;
11170Sstevel@tonic-gate 	rsp->ds_type = dsp->ds_type;
11180Sstevel@tonic-gate 	rsp->ds_class = dsp->ds_class;
11190Sstevel@tonic-gate 	rsp->ds_enumval = dsp->ds_enumval;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	free(dsp);
11220Sstevel@tonic-gate 	return (rsp->ds_decl);
11230Sstevel@tonic-gate }
1124