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