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