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