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