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