xref: /freebsd-src/cddl/contrib/opensolaris/lib/libdtrace/common/dt_decl.c (revision 246e7a2b6494cd991b08ac669ed761ecea0cc98c)
16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License, Version 1.0 only
66ff6d951SJohn Birrell  * (the "License").  You may not use this file except in compliance
76ff6d951SJohn Birrell  * with the License.
86ff6d951SJohn Birrell  *
96ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
106ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
116ff6d951SJohn Birrell  * See the License for the specific language governing permissions
126ff6d951SJohn Birrell  * and limitations under the License.
136ff6d951SJohn Birrell  *
146ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
156ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
166ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
176ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
186ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
196ff6d951SJohn Birrell  *
206ff6d951SJohn Birrell  * CDDL HEADER END
216ff6d951SJohn Birrell  */
226ff6d951SJohn Birrell /*
236ff6d951SJohn Birrell  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*8e648814SRui Paulo  * Copyright (c) 2013 by Delphix. All rights reserved.
25*8e648814SRui Paulo  * Copyright (c) 2013 Joyent, Inc. All rights reserved.
266ff6d951SJohn Birrell  * Use is subject to license terms.
276ff6d951SJohn Birrell  */
286ff6d951SJohn Birrell 
296ff6d951SJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
306ff6d951SJohn Birrell 
316ff6d951SJohn Birrell #include <strings.h>
326ff6d951SJohn Birrell #include <stdlib.h>
336ff6d951SJohn Birrell #include <limits.h>
346ff6d951SJohn Birrell #include <alloca.h>
356ff6d951SJohn Birrell #include <assert.h>
366ff6d951SJohn Birrell 
376ff6d951SJohn Birrell #include <dt_decl.h>
386ff6d951SJohn Birrell #include <dt_parser.h>
396ff6d951SJohn Birrell #include <dt_module.h>
406ff6d951SJohn Birrell #include <dt_impl.h>
416ff6d951SJohn Birrell 
426ff6d951SJohn Birrell static dt_decl_t *
dt_decl_check(dt_decl_t * ddp)436ff6d951SJohn Birrell dt_decl_check(dt_decl_t *ddp)
446ff6d951SJohn Birrell {
456ff6d951SJohn Birrell 	if (ddp->dd_kind == CTF_K_UNKNOWN)
466ff6d951SJohn Birrell 		return (ddp); /* nothing to check if the type is not yet set */
476ff6d951SJohn Birrell 
486ff6d951SJohn Birrell 	if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "char") == 0 &&
496ff6d951SJohn Birrell 	    (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG))) {
506ff6d951SJohn Birrell 		xyerror(D_DECL_CHARATTR, "invalid type declaration: short and "
516ff6d951SJohn Birrell 		    "long may not be used with char type\n");
526ff6d951SJohn Birrell 	}
536ff6d951SJohn Birrell 
546ff6d951SJohn Birrell 	if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "void") == 0 &&
556ff6d951SJohn Birrell 	    (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG |
566ff6d951SJohn Birrell 	    (DT_DA_SIGNED | DT_DA_UNSIGNED)))) {
576ff6d951SJohn Birrell 		xyerror(D_DECL_VOIDATTR, "invalid type declaration: attributes "
586ff6d951SJohn Birrell 		    "may not be used with void type\n");
596ff6d951SJohn Birrell 	}
606ff6d951SJohn Birrell 
616ff6d951SJohn Birrell 	if (ddp->dd_kind != CTF_K_INTEGER &&
626ff6d951SJohn Birrell 	    (ddp->dd_attr & (DT_DA_SIGNED | DT_DA_UNSIGNED))) {
636ff6d951SJohn Birrell 		xyerror(D_DECL_SIGNINT, "invalid type declaration: signed and "
646ff6d951SJohn Birrell 		    "unsigned may only be used with integer type\n");
656ff6d951SJohn Birrell 	}
666ff6d951SJohn Birrell 
676ff6d951SJohn Birrell 	if (ddp->dd_kind != CTF_K_INTEGER && ddp->dd_kind != CTF_K_FLOAT &&
686ff6d951SJohn Birrell 	    (ddp->dd_attr & (DT_DA_LONG | DT_DA_LONGLONG))) {
696ff6d951SJohn Birrell 		xyerror(D_DECL_LONGINT, "invalid type declaration: long and "
706ff6d951SJohn Birrell 		    "long long may only be used with integer or "
716ff6d951SJohn Birrell 		    "floating-point type\n");
726ff6d951SJohn Birrell 	}
736ff6d951SJohn Birrell 
746ff6d951SJohn Birrell 	return (ddp);
756ff6d951SJohn Birrell }
766ff6d951SJohn Birrell 
776ff6d951SJohn Birrell dt_decl_t *
dt_decl_alloc(ushort_t kind,char * name)786ff6d951SJohn Birrell dt_decl_alloc(ushort_t kind, char *name)
796ff6d951SJohn Birrell {
806ff6d951SJohn Birrell 	dt_decl_t *ddp = malloc(sizeof (dt_decl_t));
816ff6d951SJohn Birrell 
826ff6d951SJohn Birrell 	if (ddp == NULL)
836ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
846ff6d951SJohn Birrell 
856ff6d951SJohn Birrell 	ddp->dd_kind = kind;
866ff6d951SJohn Birrell 	ddp->dd_attr = 0;
876ff6d951SJohn Birrell 	ddp->dd_ctfp = NULL;
886ff6d951SJohn Birrell 	ddp->dd_type = CTF_ERR;
896ff6d951SJohn Birrell 	ddp->dd_name = name;
906ff6d951SJohn Birrell 	ddp->dd_node = NULL;
916ff6d951SJohn Birrell 	ddp->dd_next = NULL;
926ff6d951SJohn Birrell 
936ff6d951SJohn Birrell 	return (ddp);
946ff6d951SJohn Birrell }
956ff6d951SJohn Birrell 
966ff6d951SJohn Birrell void
dt_decl_free(dt_decl_t * ddp)976ff6d951SJohn Birrell dt_decl_free(dt_decl_t *ddp)
986ff6d951SJohn Birrell {
996ff6d951SJohn Birrell 	dt_decl_t *ndp;
1006ff6d951SJohn Birrell 
1016ff6d951SJohn Birrell 	for (; ddp != NULL; ddp = ndp) {
1026ff6d951SJohn Birrell 		ndp = ddp->dd_next;
1036ff6d951SJohn Birrell 		free(ddp->dd_name);
1046ff6d951SJohn Birrell 		dt_node_list_free(&ddp->dd_node);
1056ff6d951SJohn Birrell 		free(ddp);
1066ff6d951SJohn Birrell 	}
1076ff6d951SJohn Birrell }
1086ff6d951SJohn Birrell 
1096ff6d951SJohn Birrell void
dt_decl_reset(void)1106ff6d951SJohn Birrell dt_decl_reset(void)
1116ff6d951SJohn Birrell {
1126ff6d951SJohn Birrell 	dt_scope_t *dsp = &yypcb->pcb_dstack;
1136ff6d951SJohn Birrell 	dt_decl_t *ddp = dsp->ds_decl;
1146ff6d951SJohn Birrell 
1156ff6d951SJohn Birrell 	while (ddp->dd_next != NULL) {
1166ff6d951SJohn Birrell 		dsp->ds_decl = ddp->dd_next;
1176ff6d951SJohn Birrell 		ddp->dd_next = NULL;
1186ff6d951SJohn Birrell 		dt_decl_free(ddp);
1196ff6d951SJohn Birrell 		ddp = dsp->ds_decl;
1206ff6d951SJohn Birrell 	}
1216ff6d951SJohn Birrell }
1226ff6d951SJohn Birrell 
1236ff6d951SJohn Birrell dt_decl_t *
dt_decl_push(dt_decl_t * ddp)1246ff6d951SJohn Birrell dt_decl_push(dt_decl_t *ddp)
1256ff6d951SJohn Birrell {
1266ff6d951SJohn Birrell 	dt_scope_t *dsp = &yypcb->pcb_dstack;
1276ff6d951SJohn Birrell 	dt_decl_t *top = dsp->ds_decl;
1286ff6d951SJohn Birrell 
1296ff6d951SJohn Birrell 	if (top != NULL &&
1306ff6d951SJohn Birrell 	    top->dd_kind == CTF_K_UNKNOWN && top->dd_name == NULL) {
1316ff6d951SJohn Birrell 		top->dd_kind = CTF_K_INTEGER;
1326ff6d951SJohn Birrell 		(void) dt_decl_check(top);
1336ff6d951SJohn Birrell 	}
1346ff6d951SJohn Birrell 
1356ff6d951SJohn Birrell 	assert(ddp->dd_next == NULL);
1366ff6d951SJohn Birrell 	ddp->dd_next = top;
1376ff6d951SJohn Birrell 	dsp->ds_decl = ddp;
1386ff6d951SJohn Birrell 
1396ff6d951SJohn Birrell 	return (ddp);
1406ff6d951SJohn Birrell }
1416ff6d951SJohn Birrell 
1426ff6d951SJohn Birrell dt_decl_t *
dt_decl_pop(void)1436ff6d951SJohn Birrell dt_decl_pop(void)
1446ff6d951SJohn Birrell {
1456ff6d951SJohn Birrell 	dt_scope_t *dsp = &yypcb->pcb_dstack;
1466ff6d951SJohn Birrell 	dt_decl_t *ddp = dt_decl_top();
1476ff6d951SJohn Birrell 
1486ff6d951SJohn Birrell 	dsp->ds_decl = NULL;
1496ff6d951SJohn Birrell 	free(dsp->ds_ident);
1506ff6d951SJohn Birrell 	dsp->ds_ident = NULL;
1516ff6d951SJohn Birrell 	dsp->ds_ctfp = NULL;
1526ff6d951SJohn Birrell 	dsp->ds_type = CTF_ERR;
1536ff6d951SJohn Birrell 	dsp->ds_class = DT_DC_DEFAULT;
1546ff6d951SJohn Birrell 	dsp->ds_enumval = -1;
1556ff6d951SJohn Birrell 
1566ff6d951SJohn Birrell 	return (ddp);
1576ff6d951SJohn Birrell }
1586ff6d951SJohn Birrell 
1596ff6d951SJohn Birrell dt_decl_t *
dt_decl_pop_param(char ** idp)1606ff6d951SJohn Birrell dt_decl_pop_param(char **idp)
1616ff6d951SJohn Birrell {
1626ff6d951SJohn Birrell 	dt_scope_t *dsp = &yypcb->pcb_dstack;
1636ff6d951SJohn Birrell 
1646ff6d951SJohn Birrell 	if (dsp->ds_class != DT_DC_DEFAULT && dsp->ds_class != DT_DC_REGISTER) {
1656ff6d951SJohn Birrell 		xyerror(D_DECL_PARMCLASS, "inappropriate storage class "
1666ff6d951SJohn Birrell 		    "for function or associative array parameter\n");
1676ff6d951SJohn Birrell 	}
1686ff6d951SJohn Birrell 
1696ff6d951SJohn Birrell 	if (idp != NULL && dt_decl_top() != NULL) {
1706ff6d951SJohn Birrell 		*idp = dsp->ds_ident;
1716ff6d951SJohn Birrell 		dsp->ds_ident = NULL;
1726ff6d951SJohn Birrell 	}
1736ff6d951SJohn Birrell 
1746ff6d951SJohn Birrell 	return (dt_decl_pop());
1756ff6d951SJohn Birrell }
1766ff6d951SJohn Birrell 
1776ff6d951SJohn Birrell dt_decl_t *
dt_decl_top(void)1786ff6d951SJohn Birrell dt_decl_top(void)
1796ff6d951SJohn Birrell {
1806ff6d951SJohn Birrell 	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
1816ff6d951SJohn Birrell 
1826ff6d951SJohn Birrell 	if (ddp == NULL)
1836ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);
1846ff6d951SJohn Birrell 
1856ff6d951SJohn Birrell 	if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {
1866ff6d951SJohn Birrell 		ddp->dd_kind = CTF_K_INTEGER;
1876ff6d951SJohn Birrell 		(void) dt_decl_check(ddp);
1886ff6d951SJohn Birrell 	}
1896ff6d951SJohn Birrell 
1906ff6d951SJohn Birrell 	return (ddp);
1916ff6d951SJohn Birrell }
1926ff6d951SJohn Birrell 
1936ff6d951SJohn Birrell dt_decl_t *
dt_decl_ident(char * name)1946ff6d951SJohn Birrell dt_decl_ident(char *name)
1956ff6d951SJohn Birrell {
1966ff6d951SJohn Birrell 	dt_scope_t *dsp = &yypcb->pcb_dstack;
1976ff6d951SJohn Birrell 	dt_decl_t *ddp = dsp->ds_decl;
1986ff6d951SJohn Birrell 
1996ff6d951SJohn Birrell 	if (dsp->ds_ident != NULL) {
2006ff6d951SJohn Birrell 		free(name);
2016ff6d951SJohn Birrell 		xyerror(D_DECL_IDENT, "old-style declaration or "
2026ff6d951SJohn Birrell 		    "incorrect type specified\n");
2036ff6d951SJohn Birrell 	}
2046ff6d951SJohn Birrell 
2056ff6d951SJohn Birrell 	dsp->ds_ident = name;
2066ff6d951SJohn Birrell 
2076ff6d951SJohn Birrell 	if (ddp == NULL)
2086ff6d951SJohn Birrell 		ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));
2096ff6d951SJohn Birrell 
2106ff6d951SJohn Birrell 	return (ddp);
2116ff6d951SJohn Birrell }
2126ff6d951SJohn Birrell 
2136ff6d951SJohn Birrell void
dt_decl_class(dt_dclass_t class)2146ff6d951SJohn Birrell dt_decl_class(dt_dclass_t class)
2156ff6d951SJohn Birrell {
2166ff6d951SJohn Birrell 	dt_scope_t *dsp = &yypcb->pcb_dstack;
2176ff6d951SJohn Birrell 
2186ff6d951SJohn Birrell 	if (dsp->ds_class != DT_DC_DEFAULT) {
2196ff6d951SJohn Birrell 		xyerror(D_DECL_CLASS, "only one storage class allowed "
2206ff6d951SJohn Birrell 		    "in a declaration\n");
2216ff6d951SJohn Birrell 	}
2226ff6d951SJohn Birrell 
2236ff6d951SJohn Birrell 	dsp->ds_class = class;
2246ff6d951SJohn Birrell }
2256ff6d951SJohn Birrell 
2266ff6d951SJohn Birrell /*
2276ff6d951SJohn Birrell  * Set the kind and name of the current declaration.  If none is allocated,
2286ff6d951SJohn Birrell  * make a new decl and push it on to the top of our stack.  If the name or kind
2296ff6d951SJohn Birrell  * is already set for the current decl, then we need to fail this declaration.
2306ff6d951SJohn Birrell  * This can occur because too many types were given (e.g. "int int"), etc.
2316ff6d951SJohn Birrell  */
2326ff6d951SJohn Birrell dt_decl_t *
dt_decl_spec(ushort_t kind,char * name)2336ff6d951SJohn Birrell dt_decl_spec(ushort_t kind, char *name)
2346ff6d951SJohn Birrell {
2356ff6d951SJohn Birrell 	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
2366ff6d951SJohn Birrell 
2376ff6d951SJohn Birrell 	if (ddp == NULL)
2386ff6d951SJohn Birrell 		return (dt_decl_push(dt_decl_alloc(kind, name)));
2396ff6d951SJohn Birrell 
2406ff6d951SJohn Birrell 	/*
2416ff6d951SJohn Birrell 	 * If we already have a type name specified and we see another type
2426ff6d951SJohn Birrell 	 * name, this is an error if the declaration is a typedef.  If the
2436ff6d951SJohn Birrell 	 * declaration is not a typedef, then the user may be trying to declare
2446ff6d951SJohn Birrell 	 * a variable whose name has been returned by lex as a TNAME token:
2456ff6d951SJohn Birrell 	 * call dt_decl_ident() as if the grammar's IDENT rule was matched.
2466ff6d951SJohn Birrell 	 */
2476ff6d951SJohn Birrell 	if (ddp->dd_name != NULL && kind == CTF_K_TYPEDEF) {
2486ff6d951SJohn Birrell 		if (yypcb->pcb_dstack.ds_class != DT_DC_TYPEDEF)
2496ff6d951SJohn Birrell 			return (dt_decl_ident(name));
2506ff6d951SJohn Birrell 		xyerror(D_DECL_IDRED, "identifier redeclared: %s\n", name);
2516ff6d951SJohn Birrell 	}
2526ff6d951SJohn Birrell 
2536ff6d951SJohn Birrell 	if (ddp->dd_name != NULL || ddp->dd_kind != CTF_K_UNKNOWN)
2546ff6d951SJohn Birrell 		xyerror(D_DECL_COMBO, "invalid type combination\n");
2556ff6d951SJohn Birrell 
2566ff6d951SJohn Birrell 	ddp->dd_kind = kind;
2576ff6d951SJohn Birrell 	ddp->dd_name = name;
2586ff6d951SJohn Birrell 
2596ff6d951SJohn Birrell 	return (dt_decl_check(ddp));
2606ff6d951SJohn Birrell }
2616ff6d951SJohn Birrell 
2626ff6d951SJohn Birrell dt_decl_t *
dt_decl_attr(ushort_t attr)2636ff6d951SJohn Birrell dt_decl_attr(ushort_t attr)
2646ff6d951SJohn Birrell {
2656ff6d951SJohn Birrell 	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
2666ff6d951SJohn Birrell 
2676ff6d951SJohn Birrell 	if (ddp == NULL) {
2686ff6d951SJohn Birrell 		ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));
2696ff6d951SJohn Birrell 		ddp->dd_attr = attr;
2706ff6d951SJohn Birrell 		return (ddp);
2716ff6d951SJohn Birrell 	}
2726ff6d951SJohn Birrell 
2736ff6d951SJohn Birrell 	if (attr == DT_DA_LONG && (ddp->dd_attr & DT_DA_LONG)) {
2746ff6d951SJohn Birrell 		ddp->dd_attr &= ~DT_DA_LONG;
2756ff6d951SJohn Birrell 		attr = DT_DA_LONGLONG;
2766ff6d951SJohn Birrell 	}
2776ff6d951SJohn Birrell 
2786ff6d951SJohn Birrell 	ddp->dd_attr |= attr;
2796ff6d951SJohn Birrell 	return (dt_decl_check(ddp));
2806ff6d951SJohn Birrell }
2816ff6d951SJohn Birrell 
2826ff6d951SJohn Birrell /*
2836ff6d951SJohn Birrell  * Examine the list of formal parameters 'flist' and determine if the formal
2846ff6d951SJohn Birrell  * name fnp->dn_string is defined in this list (B_TRUE) or not (B_FALSE).
2856ff6d951SJohn Birrell  * If 'fnp' is in 'flist', do not search beyond 'fnp' itself in 'flist'.
2866ff6d951SJohn Birrell  */
2876ff6d951SJohn Birrell static int
dt_decl_protoform(dt_node_t * fnp,dt_node_t * flist)2886ff6d951SJohn Birrell dt_decl_protoform(dt_node_t *fnp, dt_node_t *flist)
2896ff6d951SJohn Birrell {
2906ff6d951SJohn Birrell 	dt_node_t *dnp;
2916ff6d951SJohn Birrell 
2926ff6d951SJohn Birrell 	for (dnp = flist; dnp != fnp && dnp != NULL; dnp = dnp->dn_list) {
2936ff6d951SJohn Birrell 		if (dnp->dn_string != NULL &&
2946ff6d951SJohn Birrell 		    strcmp(dnp->dn_string, fnp->dn_string) == 0)
2956ff6d951SJohn Birrell 			return (B_TRUE);
2966ff6d951SJohn Birrell 	}
2976ff6d951SJohn Birrell 
2986ff6d951SJohn Birrell 	return (B_FALSE);
2996ff6d951SJohn Birrell }
3006ff6d951SJohn Birrell 
3016ff6d951SJohn Birrell /*
3026ff6d951SJohn Birrell  * Common code for parsing array, function, and probe definition prototypes.
3036ff6d951SJohn Birrell  * The prototype node list is specified as 'plist'.  The formal prototype
3046ff6d951SJohn Birrell  * against which to compare the prototype is specified as 'flist'.  If plist
3056ff6d951SJohn Birrell  * and flist are the same, we require that named parameters are unique.  If
3066ff6d951SJohn Birrell  * plist and flist are different, we require that named parameters in plist
3076ff6d951SJohn Birrell  * match a name that is present in flist.
3086ff6d951SJohn Birrell  */
3096ff6d951SJohn Birrell int
dt_decl_prototype(dt_node_t * plist,dt_node_t * flist,const char * kind,uint_t flags)3106ff6d951SJohn Birrell dt_decl_prototype(dt_node_t *plist,
3116ff6d951SJohn Birrell     dt_node_t *flist, const char *kind, uint_t flags)
3126ff6d951SJohn Birrell {
3136ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN];
3146ff6d951SJohn Birrell 	int is_void, v = 0, i = 1;
3156ff6d951SJohn Birrell 	int form = plist != flist;
3166ff6d951SJohn Birrell 	dt_node_t *dnp;
3176ff6d951SJohn Birrell 
3186ff6d951SJohn Birrell 	for (dnp = plist; dnp != NULL; dnp = dnp->dn_list, i++) {
3196ff6d951SJohn Birrell 
3206ff6d951SJohn Birrell 		if (dnp->dn_type == CTF_ERR && !(flags & DT_DP_VARARGS)) {
3216ff6d951SJohn Birrell 			dnerror(dnp, D_DECL_PROTO_VARARGS, "%s prototype may "
3226ff6d951SJohn Birrell 			    "not use a variable-length argument list\n", kind);
3236ff6d951SJohn Birrell 		}
3246ff6d951SJohn Birrell 
3256ff6d951SJohn Birrell 		if (dt_node_is_dynamic(dnp) && !(flags & DT_DP_DYNAMIC)) {
3266ff6d951SJohn Birrell 			dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "
3276ff6d951SJohn Birrell 			    "use parameter of type %s: %s, parameter #%d\n",
3286ff6d951SJohn Birrell 			    kind, dt_node_type_name(dnp, n, sizeof (n)),
3296ff6d951SJohn Birrell 			    dnp->dn_string ? dnp->dn_string : "(anonymous)", i);
3306ff6d951SJohn Birrell 		}
3316ff6d951SJohn Birrell 
3326ff6d951SJohn Birrell 		is_void = dt_node_is_void(dnp);
3336ff6d951SJohn Birrell 		v += is_void;
3346ff6d951SJohn Birrell 
3356ff6d951SJohn Birrell 		if (is_void && !(flags & DT_DP_VOID)) {
3366ff6d951SJohn Birrell 			dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "
3376ff6d951SJohn Birrell 			    "use parameter of type %s: %s, parameter #%d\n",
3386ff6d951SJohn Birrell 			    kind, dt_node_type_name(dnp, n, sizeof (n)),
3396ff6d951SJohn Birrell 			    dnp->dn_string ? dnp->dn_string : "(anonymous)", i);
3406ff6d951SJohn Birrell 		}
3416ff6d951SJohn Birrell 
3426ff6d951SJohn Birrell 		if (is_void && dnp->dn_string != NULL) {
3436ff6d951SJohn Birrell 			dnerror(dnp, D_DECL_PROTO_NAME, "void parameter may "
3446ff6d951SJohn Birrell 			    "not have a name: %s\n", dnp->dn_string);
3456ff6d951SJohn Birrell 		}
3466ff6d951SJohn Birrell 
3476ff6d951SJohn Birrell 		if (dnp->dn_string != NULL &&
3486ff6d951SJohn Birrell 		    dt_decl_protoform(dnp, flist) != form) {
3496ff6d951SJohn Birrell 			dnerror(dnp, D_DECL_PROTO_FORM, "parameter is "
3506ff6d951SJohn Birrell 			    "%s declared in %s prototype: %s, parameter #%d\n",
3516ff6d951SJohn Birrell 			    form ? "not" : "already", kind, dnp->dn_string, i);
3526ff6d951SJohn Birrell 		}
3536ff6d951SJohn Birrell 
3546ff6d951SJohn Birrell 		if (dnp->dn_string == NULL &&
3556ff6d951SJohn Birrell 		    !is_void && !(flags & DT_DP_ANON)) {
3566ff6d951SJohn Birrell 			dnerror(dnp, D_DECL_PROTO_NAME, "parameter declaration "
3576ff6d951SJohn Birrell 			    "requires a name: parameter #%d\n", i);
3586ff6d951SJohn Birrell 		}
3596ff6d951SJohn Birrell 	}
3606ff6d951SJohn Birrell 
3616ff6d951SJohn Birrell 	if (v != 0 && plist->dn_list != NULL)
3626ff6d951SJohn Birrell 		xyerror(D_DECL_PROTO_VOID, "void must be sole parameter\n");
3636ff6d951SJohn Birrell 
3646ff6d951SJohn Birrell 	return (v ? 0 : i - 1); /* return zero if sole parameter is 'void' */
3656ff6d951SJohn Birrell }
3666ff6d951SJohn Birrell 
3676ff6d951SJohn Birrell dt_decl_t *
dt_decl_array(dt_node_t * dnp)3686ff6d951SJohn Birrell dt_decl_array(dt_node_t *dnp)
3696ff6d951SJohn Birrell {
3706ff6d951SJohn Birrell 	dt_decl_t *ddp = dt_decl_push(dt_decl_alloc(CTF_K_ARRAY, NULL));
3716ff6d951SJohn Birrell 	dt_scope_t *dsp = &yypcb->pcb_dstack;
3726ff6d951SJohn Birrell 	dt_decl_t *ndp = ddp;
3736ff6d951SJohn Birrell 
3746ff6d951SJohn Birrell 	/*
3756ff6d951SJohn Birrell 	 * After pushing the array on to the decl stack, scan ahead for multi-
3766ff6d951SJohn Birrell 	 * dimensional array declarations and push the current decl to the
3776ff6d951SJohn Birrell 	 * bottom to match the resulting CTF type tree and data layout.  Refer
3786ff6d951SJohn Birrell 	 * to the comments in dt_decl_type() and ISO C 6.5.2.1 for more info.
3796ff6d951SJohn Birrell 	 */
3806ff6d951SJohn Birrell 	while (ndp->dd_next != NULL && ndp->dd_next->dd_kind == CTF_K_ARRAY)
3816ff6d951SJohn Birrell 		ndp = ndp->dd_next; /* skip to bottom-most array declaration */
3826ff6d951SJohn Birrell 
3836ff6d951SJohn Birrell 	if (ndp != ddp) {
3846ff6d951SJohn Birrell 		if (dnp != NULL && dnp->dn_kind == DT_NODE_TYPE) {
3856ff6d951SJohn Birrell 			xyerror(D_DECL_DYNOBJ,
3866ff6d951SJohn Birrell 			    "cannot declare array of associative arrays\n");
3876ff6d951SJohn Birrell 		}
3886ff6d951SJohn Birrell 		dsp->ds_decl = ddp->dd_next;
3896ff6d951SJohn Birrell 		ddp->dd_next = ndp->dd_next;
3906ff6d951SJohn Birrell 		ndp->dd_next = ddp;
3916ff6d951SJohn Birrell 	}
3926ff6d951SJohn Birrell 
3936ff6d951SJohn Birrell 	if (ddp->dd_next->dd_name != NULL &&
3946ff6d951SJohn Birrell 	    strcmp(ddp->dd_next->dd_name, "void") == 0)
3956ff6d951SJohn Birrell 		xyerror(D_DECL_VOIDOBJ, "cannot declare array of void\n");
3966ff6d951SJohn Birrell 
3976ff6d951SJohn Birrell 	if (dnp != NULL && dnp->dn_kind != DT_NODE_TYPE) {
3986ff6d951SJohn Birrell 		dnp = ddp->dd_node = dt_node_cook(dnp, DT_IDFLG_REF);
3996ff6d951SJohn Birrell 
4006ff6d951SJohn Birrell 		if (dt_node_is_posconst(dnp) == 0) {
4016ff6d951SJohn Birrell 			xyerror(D_DECL_ARRSUB, "positive integral constant "
4026ff6d951SJohn Birrell 			    "expression or tuple signature expected as "
4036ff6d951SJohn Birrell 			    "array declaration subscript\n");
4046ff6d951SJohn Birrell 		}
4056ff6d951SJohn Birrell 
4066ff6d951SJohn Birrell 		if (dnp->dn_value > UINT_MAX)
4076ff6d951SJohn Birrell 			xyerror(D_DECL_ARRBIG, "array dimension too big\n");
4086ff6d951SJohn Birrell 
4096ff6d951SJohn Birrell 	} else if (dnp != NULL) {
4106ff6d951SJohn Birrell 		ddp->dd_node = dnp;
4116ff6d951SJohn Birrell 		(void) dt_decl_prototype(dnp, dnp, "array", DT_DP_ANON);
4126ff6d951SJohn Birrell 	}
4136ff6d951SJohn Birrell 
4146ff6d951SJohn Birrell 	return (ddp);
4156ff6d951SJohn Birrell }
4166ff6d951SJohn Birrell 
4176ff6d951SJohn Birrell /*
4186ff6d951SJohn Birrell  * When a function is declared, we need to fudge the decl stack a bit if the
4196ff6d951SJohn Birrell  * declaration uses the function pointer (*)() syntax.  In this case, the
4206ff6d951SJohn Birrell  * dt_decl_func() call occurs *after* the dt_decl_ptr() call, even though the
4216ff6d951SJohn Birrell  * resulting type is "pointer to function".  To make the pointer land on top,
4226ff6d951SJohn Birrell  * we check to see if 'pdp' is non-NULL and a pointer.  If it is, we search
4236ff6d951SJohn Birrell  * backward for a decl tagged with DT_DA_PAREN, and if one is found, the func
4246ff6d951SJohn Birrell  * decl is inserted behind this node in the decl list instead of at the top.
4256ff6d951SJohn Birrell  * In all cases, the func decl's dd_next pointer is set to the decl chain
4266ff6d951SJohn Birrell  * for the function's return type and the function parameter list is discarded.
4276ff6d951SJohn Birrell  */
4286ff6d951SJohn Birrell dt_decl_t *
dt_decl_func(dt_decl_t * pdp,dt_node_t * dnp)4296ff6d951SJohn Birrell dt_decl_func(dt_decl_t *pdp, dt_node_t *dnp)
4306ff6d951SJohn Birrell {
4316ff6d951SJohn Birrell 	dt_decl_t *ddp = dt_decl_alloc(CTF_K_FUNCTION, NULL);
4326ff6d951SJohn Birrell 
4336ff6d951SJohn Birrell 	ddp->dd_node = dnp;
4346ff6d951SJohn Birrell 
4356ff6d951SJohn Birrell 	(void) dt_decl_prototype(dnp, dnp, "function",
4366ff6d951SJohn Birrell 	    DT_DP_VARARGS | DT_DP_VOID | DT_DP_ANON);
4376ff6d951SJohn Birrell 
4386ff6d951SJohn Birrell 	if (pdp == NULL || pdp->dd_kind != CTF_K_POINTER)
4396ff6d951SJohn Birrell 		return (dt_decl_push(ddp));
4406ff6d951SJohn Birrell 
4416ff6d951SJohn Birrell 	while (pdp->dd_next != NULL && !(pdp->dd_next->dd_attr & DT_DA_PAREN))
4426ff6d951SJohn Birrell 		pdp = pdp->dd_next;
4436ff6d951SJohn Birrell 
4446ff6d951SJohn Birrell 	if (pdp->dd_next == NULL)
4456ff6d951SJohn Birrell 		return (dt_decl_push(ddp));
4466ff6d951SJohn Birrell 
4476ff6d951SJohn Birrell 	ddp->dd_next = pdp->dd_next;
4486ff6d951SJohn Birrell 	pdp->dd_next = ddp;
4496ff6d951SJohn Birrell 
4506ff6d951SJohn Birrell 	return (pdp);
4516ff6d951SJohn Birrell }
4526ff6d951SJohn Birrell 
4536ff6d951SJohn Birrell dt_decl_t *
dt_decl_ptr(void)4546ff6d951SJohn Birrell dt_decl_ptr(void)
4556ff6d951SJohn Birrell {
4566ff6d951SJohn Birrell 	return (dt_decl_push(dt_decl_alloc(CTF_K_POINTER, NULL)));
4576ff6d951SJohn Birrell }
4586ff6d951SJohn Birrell 
4596ff6d951SJohn Birrell dt_decl_t *
dt_decl_sou(uint_t kind,char * name)4606ff6d951SJohn Birrell dt_decl_sou(uint_t kind, char *name)
4616ff6d951SJohn Birrell {
4626ff6d951SJohn Birrell 	dt_decl_t *ddp = dt_decl_spec(kind, name);
4636ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN];
4646ff6d951SJohn Birrell 	ctf_file_t *ctfp;
4656ff6d951SJohn Birrell 	ctf_id_t type;
4666ff6d951SJohn Birrell 	uint_t flag;
4676ff6d951SJohn Birrell 
4686ff6d951SJohn Birrell 	if (yypcb->pcb_idepth != 0)
4696ff6d951SJohn Birrell 		ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
4706ff6d951SJohn Birrell 	else
4716ff6d951SJohn Birrell 		ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;
4726ff6d951SJohn Birrell 
4736ff6d951SJohn Birrell 	if (yypcb->pcb_dstack.ds_next != NULL)
4746ff6d951SJohn Birrell 		flag = CTF_ADD_NONROOT;
4756ff6d951SJohn Birrell 	else
4766ff6d951SJohn Birrell 		flag = CTF_ADD_ROOT;
4776ff6d951SJohn Birrell 
4786ff6d951SJohn Birrell 	(void) snprintf(n, sizeof (n), "%s %s",
4796ff6d951SJohn Birrell 	    kind == CTF_K_STRUCT ? "struct" : "union",
4806ff6d951SJohn Birrell 	    name == NULL ? "(anon)" : name);
4816ff6d951SJohn Birrell 
4826ff6d951SJohn Birrell 	if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR &&
4836ff6d951SJohn Birrell 	    ctf_type_kind(ctfp, type) != CTF_K_FORWARD)
4846ff6d951SJohn Birrell 		xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);
4856ff6d951SJohn Birrell 
4866ff6d951SJohn Birrell 	if (kind == CTF_K_STRUCT)
4876ff6d951SJohn Birrell 		type = ctf_add_struct(ctfp, flag, name);
4886ff6d951SJohn Birrell 	else
4896ff6d951SJohn Birrell 		type = ctf_add_union(ctfp, flag, name);
4906ff6d951SJohn Birrell 
4916ff6d951SJohn Birrell 	if (type == CTF_ERR || ctf_update(ctfp) == CTF_ERR) {
4926ff6d951SJohn Birrell 		xyerror(D_UNKNOWN, "failed to define %s: %s\n",
4936ff6d951SJohn Birrell 		    n, ctf_errmsg(ctf_errno(ctfp)));
4946ff6d951SJohn Birrell 	}
4956ff6d951SJohn Birrell 
4966ff6d951SJohn Birrell 	ddp->dd_ctfp = ctfp;
4976ff6d951SJohn Birrell 	ddp->dd_type = type;
4986ff6d951SJohn Birrell 
4996ff6d951SJohn Birrell 	dt_scope_push(ctfp, type);
5006ff6d951SJohn Birrell 	return (ddp);
5016ff6d951SJohn Birrell }
5026ff6d951SJohn Birrell 
5036ff6d951SJohn Birrell void
dt_decl_member(dt_node_t * dnp)5046ff6d951SJohn Birrell dt_decl_member(dt_node_t *dnp)
5056ff6d951SJohn Birrell {
5066ff6d951SJohn Birrell 	dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;
5076ff6d951SJohn Birrell 	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
5086ff6d951SJohn Birrell 	char *ident = yypcb->pcb_dstack.ds_ident;
5096ff6d951SJohn Birrell 
5106ff6d951SJohn Birrell 	const char *idname = ident ? ident : "(anon)";
5116ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN];
5126ff6d951SJohn Birrell 
5136ff6d951SJohn Birrell 	dtrace_typeinfo_t dtt;
5146ff6d951SJohn Birrell 	ctf_encoding_t cte;
5156ff6d951SJohn Birrell 	ctf_id_t base;
5166ff6d951SJohn Birrell 	uint_t kind;
5176ff6d951SJohn Birrell 	ssize_t size;
5186ff6d951SJohn Birrell 
5196ff6d951SJohn Birrell 	if (dsp == NULL)
5206ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
5216ff6d951SJohn Birrell 
5226ff6d951SJohn Birrell 	if (ddp == NULL)
5236ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);
5246ff6d951SJohn Birrell 
5256ff6d951SJohn Birrell 	if (dnp == NULL && ident == NULL)
5266ff6d951SJohn Birrell 		xyerror(D_DECL_MNAME, "member declaration requires a name\n");
5276ff6d951SJohn Birrell 
5286ff6d951SJohn Birrell 	if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {
5296ff6d951SJohn Birrell 		ddp->dd_kind = CTF_K_INTEGER;
5306ff6d951SJohn Birrell 		(void) dt_decl_check(ddp);
5316ff6d951SJohn Birrell 	}
5326ff6d951SJohn Birrell 
5336ff6d951SJohn Birrell 	if (dt_decl_type(ddp, &dtt) != 0)
5346ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
5356ff6d951SJohn Birrell 
5366ff6d951SJohn Birrell 	if (ident != NULL && strchr(ident, '`') != NULL) {
5376ff6d951SJohn Birrell 		xyerror(D_DECL_SCOPE, "D scoping operator may not be used "
5386ff6d951SJohn Birrell 		    "in a member name (%s)\n", ident);
5396ff6d951SJohn Birrell 	}
5406ff6d951SJohn Birrell 
5416ff6d951SJohn Birrell 	if (dtt.dtt_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
5426ff6d951SJohn Birrell 	    dtt.dtt_type == DT_DYN_TYPE(yypcb->pcb_hdl)) {
5436ff6d951SJohn Birrell 		xyerror(D_DECL_DYNOBJ,
5446ff6d951SJohn Birrell 		    "cannot have dynamic member: %s\n", ident);
5456ff6d951SJohn Birrell 	}
5466ff6d951SJohn Birrell 
5476ff6d951SJohn Birrell 	base = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
5486ff6d951SJohn Birrell 	kind = ctf_type_kind(dtt.dtt_ctfp, base);
5496ff6d951SJohn Birrell 	size = ctf_type_size(dtt.dtt_ctfp, base);
5506ff6d951SJohn Birrell 
5516ff6d951SJohn Birrell 	if (kind == CTF_K_FORWARD || ((kind == CTF_K_STRUCT ||
5526ff6d951SJohn Birrell 	    kind == CTF_K_UNION) && size == 0)) {
5536ff6d951SJohn Birrell 		xyerror(D_DECL_INCOMPLETE, "incomplete struct/union/enum %s: "
5546ff6d951SJohn Birrell 		    "%s\n", dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
5556ff6d951SJohn Birrell 		    n, sizeof (n)), ident);
5566ff6d951SJohn Birrell 	}
5576ff6d951SJohn Birrell 
5586ff6d951SJohn Birrell 	if (size == 0)
5596ff6d951SJohn Birrell 		xyerror(D_DECL_VOIDOBJ, "cannot have void member: %s\n", ident);
5606ff6d951SJohn Birrell 
5616ff6d951SJohn Birrell 	/*
5626ff6d951SJohn Birrell 	 * If a bit-field qualifier was part of the member declaration, create
5636ff6d951SJohn Birrell 	 * a new integer type of the same name and attributes as the base type
5646ff6d951SJohn Birrell 	 * and size equal to the specified number of bits.  We reset 'dtt' to
5656ff6d951SJohn Birrell 	 * refer to this new bit-field type and continue on to add the member.
5666ff6d951SJohn Birrell 	 */
5676ff6d951SJohn Birrell 	if (dnp != NULL) {
5686ff6d951SJohn Birrell 		dnp = dt_node_cook(dnp, DT_IDFLG_REF);
5696ff6d951SJohn Birrell 
5706ff6d951SJohn Birrell 		/*
5716ff6d951SJohn Birrell 		 * A bit-field member with no declarator is permitted to have
5726ff6d951SJohn Birrell 		 * size zero and indicates that no more fields are to be packed
5736ff6d951SJohn Birrell 		 * into the current storage unit.  We ignore these directives
5746ff6d951SJohn Birrell 		 * as the underlying ctf code currently does so for all fields.
5756ff6d951SJohn Birrell 		 */
5766ff6d951SJohn Birrell 		if (ident == NULL && dnp->dn_kind == DT_NODE_INT &&
5776ff6d951SJohn Birrell 		    dnp->dn_value == 0) {
5786ff6d951SJohn Birrell 			dt_node_free(dnp);
5796ff6d951SJohn Birrell 			goto done;
5806ff6d951SJohn Birrell 		}
5816ff6d951SJohn Birrell 
5826ff6d951SJohn Birrell 		if (dt_node_is_posconst(dnp) == 0) {
5836ff6d951SJohn Birrell 			xyerror(D_DECL_BFCONST, "positive integral constant "
5846ff6d951SJohn Birrell 			    "expression expected as bit-field size\n");
5856ff6d951SJohn Birrell 		}
5866ff6d951SJohn Birrell 
5876ff6d951SJohn Birrell 		if (ctf_type_kind(dtt.dtt_ctfp, base) != CTF_K_INTEGER ||
5886ff6d951SJohn Birrell 		    ctf_type_encoding(dtt.dtt_ctfp, base, &cte) == CTF_ERR ||
5896ff6d951SJohn Birrell 		    IS_VOID(cte)) {
5906ff6d951SJohn Birrell 			xyerror(D_DECL_BFTYPE, "invalid type for "
5916ff6d951SJohn Birrell 			    "bit-field: %s\n", idname);
5926ff6d951SJohn Birrell 		}
5936ff6d951SJohn Birrell 
5946ff6d951SJohn Birrell 		if (dnp->dn_value > cte.cte_bits) {
5956ff6d951SJohn Birrell 			xyerror(D_DECL_BFSIZE, "bit-field too big "
5966ff6d951SJohn Birrell 			    "for type: %s\n", idname);
5976ff6d951SJohn Birrell 		}
5986ff6d951SJohn Birrell 
5996ff6d951SJohn Birrell 		cte.cte_offset = 0;
6006ff6d951SJohn Birrell 		cte.cte_bits = (uint_t)dnp->dn_value;
6016ff6d951SJohn Birrell 
6026ff6d951SJohn Birrell 		dtt.dtt_type = ctf_add_integer(dsp->ds_ctfp,
6036ff6d951SJohn Birrell 		    CTF_ADD_NONROOT, ctf_type_name(dtt.dtt_ctfp,
6046ff6d951SJohn Birrell 		    dtt.dtt_type, n, sizeof (n)), &cte);
6056ff6d951SJohn Birrell 
6066ff6d951SJohn Birrell 		if (dtt.dtt_type == CTF_ERR ||
6076ff6d951SJohn Birrell 		    ctf_update(dsp->ds_ctfp) == CTF_ERR) {
6086ff6d951SJohn Birrell 			xyerror(D_UNKNOWN, "failed to create type for "
6096ff6d951SJohn Birrell 			    "member '%s': %s\n", idname,
6106ff6d951SJohn Birrell 			    ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
6116ff6d951SJohn Birrell 		}
6126ff6d951SJohn Birrell 
6136ff6d951SJohn Birrell 		dtt.dtt_ctfp = dsp->ds_ctfp;
6146ff6d951SJohn Birrell 		dt_node_free(dnp);
6156ff6d951SJohn Birrell 	}
6166ff6d951SJohn Birrell 
6176ff6d951SJohn Birrell 	/*
6186ff6d951SJohn Birrell 	 * If the member type is not defined in the same CTF container as the
6196ff6d951SJohn Birrell 	 * one associated with the current scope (i.e. the container for the
6206ff6d951SJohn Birrell 	 * struct or union itself) or its parent, copy the member type into
6216ff6d951SJohn Birrell 	 * this container and reset dtt to refer to the copied type.
6226ff6d951SJohn Birrell 	 */
6236ff6d951SJohn Birrell 	if (dtt.dtt_ctfp != dsp->ds_ctfp &&
6246ff6d951SJohn Birrell 	    dtt.dtt_ctfp != ctf_parent_file(dsp->ds_ctfp)) {
6256ff6d951SJohn Birrell 
6266ff6d951SJohn Birrell 		dtt.dtt_type = ctf_add_type(dsp->ds_ctfp,
6276ff6d951SJohn Birrell 		    dtt.dtt_ctfp, dtt.dtt_type);
6286ff6d951SJohn Birrell 		dtt.dtt_ctfp = dsp->ds_ctfp;
6296ff6d951SJohn Birrell 
6306ff6d951SJohn Birrell 		if (dtt.dtt_type == CTF_ERR ||
6316ff6d951SJohn Birrell 		    ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
6326ff6d951SJohn Birrell 			xyerror(D_UNKNOWN, "failed to copy type of '%s': %s\n",
6336ff6d951SJohn Birrell 			    idname, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
6346ff6d951SJohn Birrell 		}
6356ff6d951SJohn Birrell 	}
6366ff6d951SJohn Birrell 
6376ff6d951SJohn Birrell 	if (ctf_add_member(dsp->ds_ctfp, dsp->ds_type,
6386ff6d951SJohn Birrell 	    ident, dtt.dtt_type) == CTF_ERR) {
6396ff6d951SJohn Birrell 		xyerror(D_UNKNOWN, "failed to define member '%s': %s\n",
6406ff6d951SJohn Birrell 		    idname, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
6416ff6d951SJohn Birrell 	}
6426ff6d951SJohn Birrell 
6436ff6d951SJohn Birrell done:
6446ff6d951SJohn Birrell 	free(ident);
6456ff6d951SJohn Birrell 	yypcb->pcb_dstack.ds_ident = NULL;
6466ff6d951SJohn Birrell 	dt_decl_reset();
6476ff6d951SJohn Birrell }
6486ff6d951SJohn Birrell 
6496ff6d951SJohn Birrell /*ARGSUSED*/
6506ff6d951SJohn Birrell static int
dt_decl_hasmembers(const char * name,int value,void * private)6516ff6d951SJohn Birrell dt_decl_hasmembers(const char *name, int value, void *private)
6526ff6d951SJohn Birrell {
6536ff6d951SJohn Birrell 	return (1); /* abort search and return true if a member exists */
6546ff6d951SJohn Birrell }
6556ff6d951SJohn Birrell 
6566ff6d951SJohn Birrell dt_decl_t *
dt_decl_enum(char * name)6576ff6d951SJohn Birrell dt_decl_enum(char *name)
6586ff6d951SJohn Birrell {
6596ff6d951SJohn Birrell 	dt_decl_t *ddp = dt_decl_spec(CTF_K_ENUM, name);
6606ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN];
6616ff6d951SJohn Birrell 	ctf_file_t *ctfp;
6626ff6d951SJohn Birrell 	ctf_id_t type;
6636ff6d951SJohn Birrell 	uint_t flag;
6646ff6d951SJohn Birrell 
6656ff6d951SJohn Birrell 	if (yypcb->pcb_idepth != 0)
6666ff6d951SJohn Birrell 		ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
6676ff6d951SJohn Birrell 	else
6686ff6d951SJohn Birrell 		ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;
6696ff6d951SJohn Birrell 
6706ff6d951SJohn Birrell 	if (yypcb->pcb_dstack.ds_next != NULL)
6716ff6d951SJohn Birrell 		flag = CTF_ADD_NONROOT;
6726ff6d951SJohn Birrell 	else
6736ff6d951SJohn Birrell 		flag = CTF_ADD_ROOT;
6746ff6d951SJohn Birrell 
6756ff6d951SJohn Birrell 	(void) snprintf(n, sizeof (n), "enum %s", name ? name : "(anon)");
6766ff6d951SJohn Birrell 
6776ff6d951SJohn Birrell 	if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR) {
6786ff6d951SJohn Birrell 		if (ctf_enum_iter(ctfp, type, dt_decl_hasmembers, NULL))
6796ff6d951SJohn Birrell 			xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);
6806ff6d951SJohn Birrell 	} else if ((type = ctf_add_enum(ctfp, flag, name)) == CTF_ERR) {
6816ff6d951SJohn Birrell 		xyerror(D_UNKNOWN, "failed to define %s: %s\n",
6826ff6d951SJohn Birrell 		    n, ctf_errmsg(ctf_errno(ctfp)));
6836ff6d951SJohn Birrell 	}
6846ff6d951SJohn Birrell 
6856ff6d951SJohn Birrell 	ddp->dd_ctfp = ctfp;
6866ff6d951SJohn Birrell 	ddp->dd_type = type;
6876ff6d951SJohn Birrell 
6886ff6d951SJohn Birrell 	dt_scope_push(ctfp, type);
6896ff6d951SJohn Birrell 	return (ddp);
6906ff6d951SJohn Birrell }
6916ff6d951SJohn Birrell 
6926ff6d951SJohn Birrell void
dt_decl_enumerator(char * s,dt_node_t * dnp)6936ff6d951SJohn Birrell dt_decl_enumerator(char *s, dt_node_t *dnp)
6946ff6d951SJohn Birrell {
6956ff6d951SJohn Birrell 	dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;
6966ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
6976ff6d951SJohn Birrell 
6986ff6d951SJohn Birrell 	dt_idnode_t *inp;
6996ff6d951SJohn Birrell 	dt_ident_t *idp;
7006ff6d951SJohn Birrell 	char *name;
7016ff6d951SJohn Birrell 	int value;
7026ff6d951SJohn Birrell 
7036ff6d951SJohn Birrell 	name = alloca(strlen(s) + 1);
7046ff6d951SJohn Birrell 	(void) strcpy(name, s);
7056ff6d951SJohn Birrell 	free(s);
7066ff6d951SJohn Birrell 
7076ff6d951SJohn Birrell 	if (dsp == NULL)
7086ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
7096ff6d951SJohn Birrell 
7106ff6d951SJohn Birrell 	assert(dsp->ds_decl->dd_kind == CTF_K_ENUM);
7116ff6d951SJohn Birrell 	value = dsp->ds_enumval + 1; /* default is previous value plus one */
7126ff6d951SJohn Birrell 
7136ff6d951SJohn Birrell 	if (strchr(name, '`') != NULL) {
7146ff6d951SJohn Birrell 		xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
7156ff6d951SJohn Birrell 		    "an enumerator name (%s)\n", name);
7166ff6d951SJohn Birrell 	}
7176ff6d951SJohn Birrell 
7186ff6d951SJohn Birrell 	/*
7196ff6d951SJohn Birrell 	 * If the enumerator is being assigned a value, cook and check the node
7206ff6d951SJohn Birrell 	 * and then free it after we get the value.  We also permit references
7216ff6d951SJohn Birrell 	 * to identifiers which are previously defined enumerators in the type.
7226ff6d951SJohn Birrell 	 */
7236ff6d951SJohn Birrell 	if (dnp != NULL) {
7246ff6d951SJohn Birrell 		if (dnp->dn_kind != DT_NODE_IDENT || ctf_enum_value(
7256ff6d951SJohn Birrell 		    dsp->ds_ctfp, dsp->ds_type, dnp->dn_string, &value) != 0) {
7266ff6d951SJohn Birrell 			dnp = dt_node_cook(dnp, DT_IDFLG_REF);
7276ff6d951SJohn Birrell 
7286ff6d951SJohn Birrell 			if (dnp->dn_kind != DT_NODE_INT) {
7296ff6d951SJohn Birrell 				xyerror(D_DECL_ENCONST, "enumerator '%s' must "
7306ff6d951SJohn Birrell 				    "be assigned to an integral constant "
7316ff6d951SJohn Birrell 				    "expression\n", name);
7326ff6d951SJohn Birrell 			}
7336ff6d951SJohn Birrell 
7346ff6d951SJohn Birrell 			if ((intmax_t)dnp->dn_value > INT_MAX ||
7356ff6d951SJohn Birrell 			    (intmax_t)dnp->dn_value < INT_MIN) {
7366ff6d951SJohn Birrell 				xyerror(D_DECL_ENOFLOW, "enumerator '%s' value "
7376ff6d951SJohn Birrell 				    "overflows INT_MAX (%d)\n", name, INT_MAX);
7386ff6d951SJohn Birrell 			}
7396ff6d951SJohn Birrell 
7406ff6d951SJohn Birrell 			value = (int)dnp->dn_value;
7416ff6d951SJohn Birrell 		}
7426ff6d951SJohn Birrell 		dt_node_free(dnp);
7436ff6d951SJohn Birrell 	}
7446ff6d951SJohn Birrell 
7456ff6d951SJohn Birrell 	if (ctf_add_enumerator(dsp->ds_ctfp, dsp->ds_type,
7466ff6d951SJohn Birrell 	    name, value) == CTF_ERR || ctf_update(dsp->ds_ctfp) == CTF_ERR) {
7476ff6d951SJohn Birrell 		xyerror(D_UNKNOWN, "failed to define enumerator '%s': %s\n",
7486ff6d951SJohn Birrell 		    name, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
7496ff6d951SJohn Birrell 	}
7506ff6d951SJohn Birrell 
7516ff6d951SJohn Birrell 	dsp->ds_enumval = value; /* save most recent value */
7526ff6d951SJohn Birrell 
7536ff6d951SJohn Birrell 	/*
7546ff6d951SJohn Birrell 	 * If the enumerator name matches an identifier in the global scope,
7556ff6d951SJohn Birrell 	 * flag this as an error.  We only do this for "D" enumerators to
7566ff6d951SJohn Birrell 	 * prevent "C" header file enumerators from conflicting with the ever-
7576ff6d951SJohn Birrell 	 * growing list of D built-in global variables and inlines.  If a "C"
7586ff6d951SJohn Birrell 	 * enumerator conflicts with a global identifier, we add the enumerator
7596ff6d951SJohn Birrell 	 * but do not insert a corresponding inline (i.e. the D variable wins).
7606ff6d951SJohn Birrell 	 */
7616ff6d951SJohn Birrell 	if (dt_idstack_lookup(&yypcb->pcb_globals, name) != NULL) {
7626ff6d951SJohn Birrell 		if (dsp->ds_ctfp == dtp->dt_ddefs->dm_ctfp) {
7636ff6d951SJohn Birrell 			xyerror(D_DECL_IDRED,
7646ff6d951SJohn Birrell 			    "identifier redeclared: %s\n", name);
7656ff6d951SJohn Birrell 		} else
7666ff6d951SJohn Birrell 			return;
7676ff6d951SJohn Birrell 	}
7686ff6d951SJohn Birrell 
7696ff6d951SJohn Birrell 	dt_dprintf("add global enumerator %s = %d\n", name, value);
7706ff6d951SJohn Birrell 
7716ff6d951SJohn Birrell 	idp = dt_idhash_insert(dtp->dt_globals, name, DT_IDENT_ENUM,
7726ff6d951SJohn Birrell 	    DT_IDFLG_INLINE | DT_IDFLG_REF, 0, _dtrace_defattr, 0,
7736ff6d951SJohn Birrell 	    &dt_idops_inline, NULL, dtp->dt_gen);
7746ff6d951SJohn Birrell 
7756ff6d951SJohn Birrell 	if (idp == NULL)
7766ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
7776ff6d951SJohn Birrell 
7786ff6d951SJohn Birrell 	yyintprefix = 0;
7796ff6d951SJohn Birrell 	yyintsuffix[0] = '\0';
7806ff6d951SJohn Birrell 	yyintdecimal = 0;
7816ff6d951SJohn Birrell 
7826ff6d951SJohn Birrell 	dnp = dt_node_int(value);
783*8e648814SRui Paulo 	dt_node_type_assign(dnp, dsp->ds_ctfp, dsp->ds_type, B_FALSE);
7846ff6d951SJohn Birrell 
7856ff6d951SJohn Birrell 	if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
7866ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
7876ff6d951SJohn Birrell 
7886ff6d951SJohn Birrell 	/*
7896ff6d951SJohn Birrell 	 * Remove the INT node from the node allocation list and store it in
7906ff6d951SJohn Birrell 	 * din_list and din_root so it persists with and is freed by the ident.
7916ff6d951SJohn Birrell 	 */
7926ff6d951SJohn Birrell 	assert(yypcb->pcb_list == dnp);
7936ff6d951SJohn Birrell 	yypcb->pcb_list = dnp->dn_link;
7946ff6d951SJohn Birrell 	dnp->dn_link = NULL;
7956ff6d951SJohn Birrell 
7966ff6d951SJohn Birrell 	bzero(inp, sizeof (dt_idnode_t));
7976ff6d951SJohn Birrell 	inp->din_list = dnp;
7986ff6d951SJohn Birrell 	inp->din_root = dnp;
7996ff6d951SJohn Birrell 
8006ff6d951SJohn Birrell 	idp->di_iarg = inp;
8016ff6d951SJohn Birrell 	idp->di_ctfp = dsp->ds_ctfp;
8026ff6d951SJohn Birrell 	idp->di_type = dsp->ds_type;
8036ff6d951SJohn Birrell }
8046ff6d951SJohn Birrell 
8056ff6d951SJohn Birrell /*
8066ff6d951SJohn Birrell  * Look up the type corresponding to the specified decl stack.  The scoping of
8076ff6d951SJohn Birrell  * the underlying type names is handled by dt_type_lookup().  We build up the
8086ff6d951SJohn Birrell  * name from the specified string and prefixes and then lookup the type.  If
8096ff6d951SJohn Birrell  * we fail, an errmsg is saved and the caller must abort with EDT_COMPILER.
8106ff6d951SJohn Birrell  */
8116ff6d951SJohn Birrell int
dt_decl_type(dt_decl_t * ddp,dtrace_typeinfo_t * tip)8126ff6d951SJohn Birrell dt_decl_type(dt_decl_t *ddp, dtrace_typeinfo_t *tip)
8136ff6d951SJohn Birrell {
8146ff6d951SJohn Birrell 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
8156ff6d951SJohn Birrell 
8166ff6d951SJohn Birrell 	dt_module_t *dmp;
8176ff6d951SJohn Birrell 	ctf_arinfo_t r;
8186ff6d951SJohn Birrell 	ctf_id_t type;
8196ff6d951SJohn Birrell 
8206ff6d951SJohn Birrell 	char n[DT_TYPE_NAMELEN];
8216ff6d951SJohn Birrell 	uint_t flag;
8226ff6d951SJohn Birrell 	char *name;
8236ff6d951SJohn Birrell 	int rv;
8246ff6d951SJohn Birrell 
825*8e648814SRui Paulo 	tip->dtt_flags = 0;
826*8e648814SRui Paulo 
8276ff6d951SJohn Birrell 	/*
8286ff6d951SJohn Birrell 	 * Based on our current #include depth and decl stack depth, determine
8296ff6d951SJohn Birrell 	 * which dynamic CTF module and scope to use when adding any new types.
8306ff6d951SJohn Birrell 	 */
8316ff6d951SJohn Birrell 	dmp = yypcb->pcb_idepth ? dtp->dt_cdefs : dtp->dt_ddefs;
8326ff6d951SJohn Birrell 	flag = yypcb->pcb_dstack.ds_next ? CTF_ADD_NONROOT : CTF_ADD_ROOT;
8336ff6d951SJohn Birrell 
834*8e648814SRui Paulo 	if (ddp->dd_attr & DT_DA_USER)
835*8e648814SRui Paulo 		tip->dtt_flags = DTT_FL_USER;
836*8e648814SRui Paulo 
8376ff6d951SJohn Birrell 	/*
8386ff6d951SJohn Birrell 	 * If we have already cached a CTF type for this decl, then we just
8396ff6d951SJohn Birrell 	 * return the type information for the cached type.
8406ff6d951SJohn Birrell 	 */
8416ff6d951SJohn Birrell 	if (ddp->dd_ctfp != NULL &&
8426ff6d951SJohn Birrell 	    (dmp = dt_module_lookup_by_ctf(dtp, ddp->dd_ctfp)) != NULL) {
8436ff6d951SJohn Birrell 		tip->dtt_object = dmp->dm_name;
8446ff6d951SJohn Birrell 		tip->dtt_ctfp = ddp->dd_ctfp;
8456ff6d951SJohn Birrell 		tip->dtt_type = ddp->dd_type;
8466ff6d951SJohn Birrell 		return (0);
8476ff6d951SJohn Birrell 	}
8486ff6d951SJohn Birrell 
8496ff6d951SJohn Birrell 	/*
8506ff6d951SJohn Birrell 	 * Currently CTF treats all function pointers identically.  We cache a
8516ff6d951SJohn Birrell 	 * representative ID of kind CTF_K_FUNCTION and just return that type.
8526ff6d951SJohn Birrell 	 * If we want to support full function declarations, dd_next refers to
8536ff6d951SJohn Birrell 	 * the declaration of the function return type, and the parameter list
8546ff6d951SJohn Birrell 	 * should be parsed and hung off a new pointer inside of this decl.
8556ff6d951SJohn Birrell 	 */
8566ff6d951SJohn Birrell 	if (ddp->dd_kind == CTF_K_FUNCTION) {
8576ff6d951SJohn Birrell 		tip->dtt_object = dtp->dt_ddefs->dm_name;
8586ff6d951SJohn Birrell 		tip->dtt_ctfp = DT_FUNC_CTFP(dtp);
8596ff6d951SJohn Birrell 		tip->dtt_type = DT_FUNC_TYPE(dtp);
8606ff6d951SJohn Birrell 		return (0);
8616ff6d951SJohn Birrell 	}
8626ff6d951SJohn Birrell 
8636ff6d951SJohn Birrell 	/*
8646ff6d951SJohn Birrell 	 * If the decl is a pointer, resolve the rest of the stack by calling
8656ff6d951SJohn Birrell 	 * dt_decl_type() recursively and then compute a pointer to the result.
8666ff6d951SJohn Birrell 	 * Similar to the code above, we return a cached id for function ptrs.
8676ff6d951SJohn Birrell 	 */
8686ff6d951SJohn Birrell 	if (ddp->dd_kind == CTF_K_POINTER) {
8696ff6d951SJohn Birrell 		if (ddp->dd_next->dd_kind == CTF_K_FUNCTION) {
8706ff6d951SJohn Birrell 			tip->dtt_object = dtp->dt_ddefs->dm_name;
8716ff6d951SJohn Birrell 			tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
8726ff6d951SJohn Birrell 			tip->dtt_type = DT_FPTR_TYPE(dtp);
8736ff6d951SJohn Birrell 			return (0);
8746ff6d951SJohn Birrell 		}
8756ff6d951SJohn Birrell 
8766ff6d951SJohn Birrell 		if ((rv = dt_decl_type(ddp->dd_next, tip)) == 0 &&
8776ff6d951SJohn Birrell 		    (rv = dt_type_pointer(tip)) != 0) {
8786ff6d951SJohn Birrell 			xywarn(D_UNKNOWN, "cannot find type: %s*: %s\n",
8796ff6d951SJohn Birrell 			    dt_type_name(tip->dtt_ctfp, tip->dtt_type,
8806ff6d951SJohn Birrell 			    n, sizeof (n)), ctf_errmsg(dtp->dt_ctferr));
8816ff6d951SJohn Birrell 		}
8826ff6d951SJohn Birrell 
8836ff6d951SJohn Birrell 		return (rv);
8846ff6d951SJohn Birrell 	}
8856ff6d951SJohn Birrell 
8866ff6d951SJohn Birrell 	/*
8876ff6d951SJohn Birrell 	 * If the decl is an array, we must find the base type and then call
8886ff6d951SJohn Birrell 	 * dt_decl_type() recursively and then build an array of the result.
8896ff6d951SJohn Birrell 	 * The C and D multi-dimensional array syntax requires that consecutive
8906ff6d951SJohn Birrell 	 * array declarations be processed from right-to-left (i.e. top-down
8916ff6d951SJohn Birrell 	 * from the perspective of the declaration stack).  For example, an
8926ff6d951SJohn Birrell 	 * array declaration such as int x[3][5] is stored on the stack as:
8936ff6d951SJohn Birrell 	 *
8946ff6d951SJohn Birrell 	 * (bottom) NULL <- ( INT "int" ) <- ( ARR [3] ) <- ( ARR [5] ) (top)
8956ff6d951SJohn Birrell 	 *
8966ff6d951SJohn Birrell 	 * but means that x is declared to be an array of 3 objects each of
8976ff6d951SJohn Birrell 	 * which is an array of 5 integers, or in CTF representation:
8986ff6d951SJohn Birrell 	 *
8996ff6d951SJohn Birrell 	 * type T1:( content=int, nelems=5 ) type T2:( content=T1, nelems=3 )
9006ff6d951SJohn Birrell 	 *
9016ff6d951SJohn Birrell 	 * For more details, refer to K&R[5.7] and ISO C 6.5.2.1.  Rather than
9026ff6d951SJohn Birrell 	 * overcomplicate the implementation of dt_decl_type(), we push array
9036ff6d951SJohn Birrell 	 * declarations down into the stack in dt_decl_array(), above, so that
9046ff6d951SJohn Birrell 	 * by the time dt_decl_type() is called, the decl stack looks like:
9056ff6d951SJohn Birrell 	 *
9066ff6d951SJohn Birrell 	 * (bottom) NULL <- ( INT "int" ) <- ( ARR [5] ) <- ( ARR [3] ) (top)
9076ff6d951SJohn Birrell 	 *
9086ff6d951SJohn Birrell 	 * which permits a straightforward recursive descent of the decl stack
9096ff6d951SJohn Birrell 	 * to build the corresponding CTF type tree in the appropriate order.
9106ff6d951SJohn Birrell 	 */
9116ff6d951SJohn Birrell 	if (ddp->dd_kind == CTF_K_ARRAY) {
9126ff6d951SJohn Birrell 		/*
9136ff6d951SJohn Birrell 		 * If the array decl has a parameter list associated with it,
9146ff6d951SJohn Birrell 		 * this is an associative array declaration: return <DYN>.
9156ff6d951SJohn Birrell 		 */
9166ff6d951SJohn Birrell 		if (ddp->dd_node != NULL &&
9176ff6d951SJohn Birrell 		    ddp->dd_node->dn_kind == DT_NODE_TYPE) {
9186ff6d951SJohn Birrell 			tip->dtt_object = dtp->dt_ddefs->dm_name;
9196ff6d951SJohn Birrell 			tip->dtt_ctfp = DT_DYN_CTFP(dtp);
9206ff6d951SJohn Birrell 			tip->dtt_type = DT_DYN_TYPE(dtp);
9216ff6d951SJohn Birrell 			return (0);
9226ff6d951SJohn Birrell 		}
9236ff6d951SJohn Birrell 
9246ff6d951SJohn Birrell 		if ((rv = dt_decl_type(ddp->dd_next, tip)) != 0)
9256ff6d951SJohn Birrell 			return (rv);
9266ff6d951SJohn Birrell 
9276ff6d951SJohn Birrell 		/*
9286ff6d951SJohn Birrell 		 * If the array base type is not defined in the target
9296ff6d951SJohn Birrell 		 * container or its parent, copy the type to the target
9306ff6d951SJohn Birrell 		 * container and reset dtt_ctfp and dtt_type to the copy.
9316ff6d951SJohn Birrell 		 */
9326ff6d951SJohn Birrell 		if (tip->dtt_ctfp != dmp->dm_ctfp &&
9336ff6d951SJohn Birrell 		    tip->dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
9346ff6d951SJohn Birrell 
9356ff6d951SJohn Birrell 			tip->dtt_type = ctf_add_type(dmp->dm_ctfp,
9366ff6d951SJohn Birrell 			    tip->dtt_ctfp, tip->dtt_type);
9376ff6d951SJohn Birrell 			tip->dtt_ctfp = dmp->dm_ctfp;
9386ff6d951SJohn Birrell 
9396ff6d951SJohn Birrell 			if (tip->dtt_type == CTF_ERR ||
9406ff6d951SJohn Birrell 			    ctf_update(tip->dtt_ctfp) == CTF_ERR) {
9416ff6d951SJohn Birrell 				xywarn(D_UNKNOWN, "failed to copy type: %s\n",
9426ff6d951SJohn Birrell 				    ctf_errmsg(ctf_errno(tip->dtt_ctfp)));
9436ff6d951SJohn Birrell 				return (-1);
9446ff6d951SJohn Birrell 			}
9456ff6d951SJohn Birrell 		}
9466ff6d951SJohn Birrell 
9476ff6d951SJohn Birrell 		/*
9486ff6d951SJohn Birrell 		 * The array index type is irrelevant in C and D: just set it
9496ff6d951SJohn Birrell 		 * to "long" for all array types that we create on-the-fly.
9506ff6d951SJohn Birrell 		 */
9516ff6d951SJohn Birrell 		r.ctr_contents = tip->dtt_type;
9526ff6d951SJohn Birrell 		r.ctr_index = ctf_lookup_by_name(tip->dtt_ctfp, "long");
9536ff6d951SJohn Birrell 		r.ctr_nelems = ddp->dd_node ?
9546ff6d951SJohn Birrell 		    (uint_t)ddp->dd_node->dn_value : 0;
9556ff6d951SJohn Birrell 
9566ff6d951SJohn Birrell 		tip->dtt_object = dmp->dm_name;
9576ff6d951SJohn Birrell 		tip->dtt_ctfp = dmp->dm_ctfp;
9586ff6d951SJohn Birrell 		tip->dtt_type = ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &r);
9596ff6d951SJohn Birrell 
9606ff6d951SJohn Birrell 		if (tip->dtt_type == CTF_ERR ||
9616ff6d951SJohn Birrell 		    ctf_update(tip->dtt_ctfp) == CTF_ERR) {
9626ff6d951SJohn Birrell 			xywarn(D_UNKNOWN, "failed to create array type: %s\n",
9636ff6d951SJohn Birrell 			    ctf_errmsg(ctf_errno(tip->dtt_ctfp)));
9646ff6d951SJohn Birrell 			return (-1);
9656ff6d951SJohn Birrell 		}
9666ff6d951SJohn Birrell 
9676ff6d951SJohn Birrell 		return (0);
9686ff6d951SJohn Birrell 	}
9696ff6d951SJohn Birrell 
9706ff6d951SJohn Birrell 	/*
9716ff6d951SJohn Birrell 	 * Allocate space for the type name and enough space for the maximum
9726ff6d951SJohn Birrell 	 * additional text ("unsigned long long \0" requires 20 more bytes).
9736ff6d951SJohn Birrell 	 */
9746ff6d951SJohn Birrell 	name = alloca(ddp->dd_name ? strlen(ddp->dd_name) + 20 : 20);
9756ff6d951SJohn Birrell 	name[0] = '\0';
9766ff6d951SJohn Birrell 
9776ff6d951SJohn Birrell 	switch (ddp->dd_kind) {
9786ff6d951SJohn Birrell 	case CTF_K_INTEGER:
9796ff6d951SJohn Birrell 	case CTF_K_FLOAT:
9806ff6d951SJohn Birrell 		if (ddp->dd_attr & DT_DA_SIGNED)
9816ff6d951SJohn Birrell 			(void) strcat(name, "signed ");
9826ff6d951SJohn Birrell 		if (ddp->dd_attr & DT_DA_UNSIGNED)
9836ff6d951SJohn Birrell 			(void) strcat(name, "unsigned ");
9846ff6d951SJohn Birrell 		if (ddp->dd_attr & DT_DA_SHORT)
9856ff6d951SJohn Birrell 			(void) strcat(name, "short ");
9866ff6d951SJohn Birrell 		if (ddp->dd_attr & DT_DA_LONG)
9876ff6d951SJohn Birrell 			(void) strcat(name, "long ");
9886ff6d951SJohn Birrell 		if (ddp->dd_attr & DT_DA_LONGLONG)
9896ff6d951SJohn Birrell 			(void) strcat(name, "long long ");
9906ff6d951SJohn Birrell 		if (ddp->dd_attr == 0 && ddp->dd_name == NULL)
9916ff6d951SJohn Birrell 			(void) strcat(name, "int");
9926ff6d951SJohn Birrell 		break;
9936ff6d951SJohn Birrell 	case CTF_K_STRUCT:
9946ff6d951SJohn Birrell 		(void) strcpy(name, "struct ");
9956ff6d951SJohn Birrell 		break;
9966ff6d951SJohn Birrell 	case CTF_K_UNION:
9976ff6d951SJohn Birrell 		(void) strcpy(name, "union ");
9986ff6d951SJohn Birrell 		break;
9996ff6d951SJohn Birrell 	case CTF_K_ENUM:
10006ff6d951SJohn Birrell 		(void) strcpy(name, "enum ");
10016ff6d951SJohn Birrell 		break;
10026ff6d951SJohn Birrell 	case CTF_K_TYPEDEF:
10036ff6d951SJohn Birrell 		break;
10046ff6d951SJohn Birrell 	default:
10056ff6d951SJohn Birrell 		xywarn(D_UNKNOWN, "internal error -- "
10066ff6d951SJohn Birrell 		    "bad decl kind %u\n", ddp->dd_kind);
10076ff6d951SJohn Birrell 		return (-1);
10086ff6d951SJohn Birrell 	}
10096ff6d951SJohn Birrell 
10106ff6d951SJohn Birrell 	/*
10116ff6d951SJohn Birrell 	 * Add dd_name unless a short, long, or long long is explicitly
10126ff6d951SJohn Birrell 	 * suffixed by int.  We use the C/CTF canonical names for integers.
10136ff6d951SJohn Birrell 	 */
10146ff6d951SJohn Birrell 	if (ddp->dd_name != NULL && (ddp->dd_kind != CTF_K_INTEGER ||
10156ff6d951SJohn Birrell 	    (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG)) == 0))
10166ff6d951SJohn Birrell 		(void) strcat(name, ddp->dd_name);
10176ff6d951SJohn Birrell 
10186ff6d951SJohn Birrell 	/*
10196ff6d951SJohn Birrell 	 * Lookup the type.  If we find it, we're done.  Otherwise create a
10206ff6d951SJohn Birrell 	 * forward tag for the type if it is a struct, union, or enum.  If
10216ff6d951SJohn Birrell 	 * we can't find it and we can't create a tag, return failure.
10226ff6d951SJohn Birrell 	 */
10236ff6d951SJohn Birrell 	if ((rv = dt_type_lookup(name, tip)) == 0)
10246ff6d951SJohn Birrell 		return (rv);
10256ff6d951SJohn Birrell 
10266ff6d951SJohn Birrell 	switch (ddp->dd_kind) {
10276ff6d951SJohn Birrell 	case CTF_K_STRUCT:
10286ff6d951SJohn Birrell 	case CTF_K_UNION:
10296ff6d951SJohn Birrell 	case CTF_K_ENUM:
10306ff6d951SJohn Birrell 		type = ctf_add_forward(dmp->dm_ctfp, flag,
10316ff6d951SJohn Birrell 		    ddp->dd_name, ddp->dd_kind);
10326ff6d951SJohn Birrell 		break;
10336ff6d951SJohn Birrell 	default:
10346ff6d951SJohn Birrell 		xywarn(D_UNKNOWN, "failed to resolve type %s: %s\n", name,
10356ff6d951SJohn Birrell 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
10366ff6d951SJohn Birrell 		return (rv);
10376ff6d951SJohn Birrell 	}
10386ff6d951SJohn Birrell 
10396ff6d951SJohn Birrell 	if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
10406ff6d951SJohn Birrell 		xywarn(D_UNKNOWN, "failed to add forward tag for %s: %s\n",
10416ff6d951SJohn Birrell 		    name, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
10426ff6d951SJohn Birrell 		return (-1);
10436ff6d951SJohn Birrell 	}
10446ff6d951SJohn Birrell 
10456ff6d951SJohn Birrell 	ddp->dd_ctfp = dmp->dm_ctfp;
10466ff6d951SJohn Birrell 	ddp->dd_type = type;
10476ff6d951SJohn Birrell 
10486ff6d951SJohn Birrell 	tip->dtt_object = dmp->dm_name;
10496ff6d951SJohn Birrell 	tip->dtt_ctfp = dmp->dm_ctfp;
10506ff6d951SJohn Birrell 	tip->dtt_type = type;
10516ff6d951SJohn Birrell 
10526ff6d951SJohn Birrell 	return (0);
10536ff6d951SJohn Birrell }
10546ff6d951SJohn Birrell 
10556ff6d951SJohn Birrell void
dt_scope_create(dt_scope_t * dsp)10566ff6d951SJohn Birrell dt_scope_create(dt_scope_t *dsp)
10576ff6d951SJohn Birrell {
10586ff6d951SJohn Birrell 	dsp->ds_decl = NULL;
10596ff6d951SJohn Birrell 	dsp->ds_next = NULL;
10606ff6d951SJohn Birrell 	dsp->ds_ident = NULL;
10616ff6d951SJohn Birrell 	dsp->ds_ctfp = NULL;
10626ff6d951SJohn Birrell 	dsp->ds_type = CTF_ERR;
10636ff6d951SJohn Birrell 	dsp->ds_class = DT_DC_DEFAULT;
10646ff6d951SJohn Birrell 	dsp->ds_enumval = -1;
10656ff6d951SJohn Birrell }
10666ff6d951SJohn Birrell 
10676ff6d951SJohn Birrell void
dt_scope_destroy(dt_scope_t * dsp)10686ff6d951SJohn Birrell dt_scope_destroy(dt_scope_t *dsp)
10696ff6d951SJohn Birrell {
10706ff6d951SJohn Birrell 	dt_scope_t *nsp;
10716ff6d951SJohn Birrell 
10726ff6d951SJohn Birrell 	for (; dsp != NULL; dsp = nsp) {
10736ff6d951SJohn Birrell 		dt_decl_free(dsp->ds_decl);
10746ff6d951SJohn Birrell 		free(dsp->ds_ident);
10756ff6d951SJohn Birrell 		nsp = dsp->ds_next;
10766ff6d951SJohn Birrell 		if (dsp != &yypcb->pcb_dstack)
10776ff6d951SJohn Birrell 			free(dsp);
10786ff6d951SJohn Birrell 	}
10796ff6d951SJohn Birrell }
10806ff6d951SJohn Birrell 
10816ff6d951SJohn Birrell void
dt_scope_push(ctf_file_t * ctfp,ctf_id_t type)10826ff6d951SJohn Birrell dt_scope_push(ctf_file_t *ctfp, ctf_id_t type)
10836ff6d951SJohn Birrell {
10846ff6d951SJohn Birrell 	dt_scope_t *rsp = &yypcb->pcb_dstack;
10856ff6d951SJohn Birrell 	dt_scope_t *dsp = malloc(sizeof (dt_scope_t));
10866ff6d951SJohn Birrell 
10876ff6d951SJohn Birrell 	if (dsp == NULL)
10886ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
10896ff6d951SJohn Birrell 
10906ff6d951SJohn Birrell 	dsp->ds_decl = rsp->ds_decl;
10916ff6d951SJohn Birrell 	dsp->ds_next = rsp->ds_next;
10926ff6d951SJohn Birrell 	dsp->ds_ident = rsp->ds_ident;
10936ff6d951SJohn Birrell 	dsp->ds_ctfp = ctfp;
10946ff6d951SJohn Birrell 	dsp->ds_type = type;
10956ff6d951SJohn Birrell 	dsp->ds_class = rsp->ds_class;
10966ff6d951SJohn Birrell 	dsp->ds_enumval = rsp->ds_enumval;
10976ff6d951SJohn Birrell 
10986ff6d951SJohn Birrell 	dt_scope_create(rsp);
10996ff6d951SJohn Birrell 	rsp->ds_next = dsp;
11006ff6d951SJohn Birrell }
11016ff6d951SJohn Birrell 
11026ff6d951SJohn Birrell dt_decl_t *
dt_scope_pop(void)11036ff6d951SJohn Birrell dt_scope_pop(void)
11046ff6d951SJohn Birrell {
11056ff6d951SJohn Birrell 	dt_scope_t *rsp = &yypcb->pcb_dstack;
11066ff6d951SJohn Birrell 	dt_scope_t *dsp = rsp->ds_next;
11076ff6d951SJohn Birrell 
11086ff6d951SJohn Birrell 	if (dsp == NULL)
11096ff6d951SJohn Birrell 		longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
11106ff6d951SJohn Birrell 
11116ff6d951SJohn Birrell 	if (dsp->ds_ctfp != NULL && ctf_update(dsp->ds_ctfp) == CTF_ERR) {
11126ff6d951SJohn Birrell 		xyerror(D_UNKNOWN, "failed to update type definitions: %s\n",
11136ff6d951SJohn Birrell 		    ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
11146ff6d951SJohn Birrell 	}
11156ff6d951SJohn Birrell 
11166ff6d951SJohn Birrell 	dt_decl_free(rsp->ds_decl);
11176ff6d951SJohn Birrell 	free(rsp->ds_ident);
11186ff6d951SJohn Birrell 
11196ff6d951SJohn Birrell 	rsp->ds_decl = dsp->ds_decl;
11206ff6d951SJohn Birrell 	rsp->ds_next = dsp->ds_next;
11216ff6d951SJohn Birrell 	rsp->ds_ident = dsp->ds_ident;
11226ff6d951SJohn Birrell 	rsp->ds_ctfp = dsp->ds_ctfp;
11236ff6d951SJohn Birrell 	rsp->ds_type = dsp->ds_type;
11246ff6d951SJohn Birrell 	rsp->ds_class = dsp->ds_class;
11256ff6d951SJohn Birrell 	rsp->ds_enumval = dsp->ds_enumval;
11266ff6d951SJohn Birrell 
11276ff6d951SJohn Birrell 	free(dsp);
11286ff6d951SJohn Birrell 	return (rsp->ds_decl);
11296ff6d951SJohn Birrell }
1130