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
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.
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]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
25 * Use is subject to license terms.
28 #pragma ident "%Z%%M% %I% %E% SMI"
37 #include <dt_parser.h>
38 #include <dt_module.h>
42 dt_decl_check(dt_decl_t *ddp)
44 if (ddp->dd_kind == CTF_K_UNKNOWN)
45 return (ddp); /* nothing to check if the type is not yet set */
47 if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "char") == 0 &&
48 (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG))) {
49 xyerror(D_DECL_CHARATTR, "invalid type declaration: short and "
50 "long may not be used with char type\n");
53 if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "void") == 0 &&
54 (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG |
55 (DT_DA_SIGNED | DT_DA_UNSIGNED)))) {
56 xyerror(D_DECL_VOIDATTR, "invalid type declaration: attributes "
57 "may not be used with void type\n");
60 if (ddp->dd_kind != CTF_K_INTEGER &&
61 (ddp->dd_attr & (DT_DA_SIGNED | DT_DA_UNSIGNED))) {
62 xyerror(D_DECL_SIGNINT, "invalid type declaration: signed and "
63 "unsigned may only be used with integer type\n");
66 if (ddp->dd_kind != CTF_K_INTEGER && ddp->dd_kind != CTF_K_FLOAT &&
67 (ddp->dd_attr & (DT_DA_LONG | DT_DA_LONGLONG))) {
68 xyerror(D_DECL_LONGINT, "invalid type declaration: long and "
69 "long long may only be used with integer or "
70 "floating-point type\n");
77 dt_decl_alloc(ushort_t kind, char *name)
79 dt_decl_t *ddp = malloc(sizeof (dt_decl_t));
82 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
87 ddp->dd_type = CTF_ERR;
96 dt_decl_free(dt_decl_t *ddp)
100 for (; ddp != NULL; ddp = ndp) {
103 dt_node_list_free(&ddp->dd_node);
111 dt_scope_t *dsp = &yypcb->pcb_dstack;
112 dt_decl_t *ddp = dsp->ds_decl;
114 while (ddp->dd_next != NULL) {
115 dsp->ds_decl = ddp->dd_next;
123 dt_decl_push(dt_decl_t *ddp)
125 dt_scope_t *dsp = &yypcb->pcb_dstack;
126 dt_decl_t *top = dsp->ds_decl;
129 top->dd_kind == CTF_K_UNKNOWN && top->dd_name == NULL) {
130 top->dd_kind = CTF_K_INTEGER;
131 (void) dt_decl_check(top);
134 assert(ddp->dd_next == NULL);
144 dt_scope_t *dsp = &yypcb->pcb_dstack;
145 dt_decl_t *ddp = dt_decl_top();
149 dsp->ds_ident = NULL;
151 dsp->ds_type = CTF_ERR;
152 dsp->ds_class = DT_DC_DEFAULT;
153 dsp->ds_enumval = -1;
159 dt_decl_pop_param(char **idp)
161 dt_scope_t *dsp = &yypcb->pcb_dstack;
163 if (dsp->ds_class != DT_DC_DEFAULT && dsp->ds_class != DT_DC_REGISTER) {
164 xyerror(D_DECL_PARMCLASS, "inappropriate storage class "
165 "for function or associative array parameter\n");
168 if (idp != NULL && dt_decl_top() != NULL) {
169 *idp = dsp->ds_ident;
170 dsp->ds_ident = NULL;
173 return (dt_decl_pop());
179 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
182 longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);
184 if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {
185 ddp->dd_kind = CTF_K_INTEGER;
186 (void) dt_decl_check(ddp);
193 dt_decl_ident(char *name)
195 dt_scope_t *dsp = &yypcb->pcb_dstack;
196 dt_decl_t *ddp = dsp->ds_decl;
198 if (dsp->ds_ident != NULL) {
200 xyerror(D_DECL_IDENT, "old-style declaration or "
201 "incorrect type specified\n");
204 dsp->ds_ident = name;
207 ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));
213 dt_decl_class(dt_dclass_t class)
215 dt_scope_t *dsp = &yypcb->pcb_dstack;
217 if (dsp->ds_class != DT_DC_DEFAULT) {
218 xyerror(D_DECL_CLASS, "only one storage class allowed "
219 "in a declaration\n");
222 dsp->ds_class = class;
226 * Set the kind and name of the current declaration. If none is allocated,
227 * make a new decl and push it on to the top of our stack. If the name or kind
228 * is already set for the current decl, then we need to fail this declaration.
229 * This can occur because too many types were given (e.g. "int int"), etc.
232 dt_decl_spec(ushort_t kind, char *name)
234 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
237 return (dt_decl_push(dt_decl_alloc(kind, name)));
240 * If we already have a type name specified and we see another type
241 * name, this is an error if the declaration is a typedef. If the
242 * declaration is not a typedef, then the user may be trying to declare
243 * a variable whose name has been returned by lex as a TNAME token:
244 * call dt_decl_ident() as if the grammar's IDENT rule was matched.
246 if (ddp->dd_name != NULL && kind == CTF_K_TYPEDEF) {
247 if (yypcb->pcb_dstack.ds_class != DT_DC_TYPEDEF)
248 return (dt_decl_ident(name));
249 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n", name);
252 if (ddp->dd_name != NULL || ddp->dd_kind != CTF_K_UNKNOWN)
253 xyerror(D_DECL_COMBO, "invalid type combination\n");
258 return (dt_decl_check(ddp));
262 dt_decl_attr(ushort_t attr)
264 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
267 ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));
272 if (attr == DT_DA_LONG && (ddp->dd_attr & DT_DA_LONG)) {
273 ddp->dd_attr &= ~DT_DA_LONG;
274 attr = DT_DA_LONGLONG;
277 ddp->dd_attr |= attr;
278 return (dt_decl_check(ddp));
282 * Examine the list of formal parameters 'flist' and determine if the formal
283 * name fnp->dn_string is defined in this list (B_TRUE) or not (B_FALSE).
284 * If 'fnp' is in 'flist', do not search beyond 'fnp' itself in 'flist'.
287 dt_decl_protoform(dt_node_t *fnp, dt_node_t *flist)
291 for (dnp = flist; dnp != fnp && dnp != NULL; dnp = dnp->dn_list) {
292 if (dnp->dn_string != NULL &&
293 strcmp(dnp->dn_string, fnp->dn_string) == 0)
301 * Common code for parsing array, function, and probe definition prototypes.
302 * The prototype node list is specified as 'plist'. The formal prototype
303 * against which to compare the prototype is specified as 'flist'. If plist
304 * and flist are the same, we require that named parameters are unique. If
305 * plist and flist are different, we require that named parameters in plist
306 * match a name that is present in flist.
309 dt_decl_prototype(dt_node_t *plist,
310 dt_node_t *flist, const char *kind, uint_t flags)
312 char n[DT_TYPE_NAMELEN];
313 int is_void, v = 0, i = 1;
314 int form = plist != flist;
317 for (dnp = plist; dnp != NULL; dnp = dnp->dn_list, i++) {
319 if (dnp->dn_type == CTF_ERR && !(flags & DT_DP_VARARGS)) {
320 dnerror(dnp, D_DECL_PROTO_VARARGS, "%s prototype may "
321 "not use a variable-length argument list\n", kind);
324 if (dt_node_is_dynamic(dnp) && !(flags & DT_DP_DYNAMIC)) {
325 dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "
326 "use parameter of type %s: %s, parameter #%d\n",
327 kind, dt_node_type_name(dnp, n, sizeof (n)),
328 dnp->dn_string ? dnp->dn_string : "(anonymous)", i);
331 is_void = dt_node_is_void(dnp);
334 if (is_void && !(flags & DT_DP_VOID)) {
335 dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "
336 "use parameter of type %s: %s, parameter #%d\n",
337 kind, dt_node_type_name(dnp, n, sizeof (n)),
338 dnp->dn_string ? dnp->dn_string : "(anonymous)", i);
341 if (is_void && dnp->dn_string != NULL) {
342 dnerror(dnp, D_DECL_PROTO_NAME, "void parameter may "
343 "not have a name: %s\n", dnp->dn_string);
346 if (dnp->dn_string != NULL &&
347 dt_decl_protoform(dnp, flist) != form) {
348 dnerror(dnp, D_DECL_PROTO_FORM, "parameter is "
349 "%s declared in %s prototype: %s, parameter #%d\n",
350 form ? "not" : "already", kind, dnp->dn_string, i);
353 if (dnp->dn_string == NULL &&
354 !is_void && !(flags & DT_DP_ANON)) {
355 dnerror(dnp, D_DECL_PROTO_NAME, "parameter declaration "
356 "requires a name: parameter #%d\n", i);
360 if (v != 0 && plist->dn_list != NULL)
361 xyerror(D_DECL_PROTO_VOID, "void must be sole parameter\n");
363 return (v ? 0 : i - 1); /* return zero if sole parameter is 'void' */
367 dt_decl_array(dt_node_t *dnp)
369 dt_decl_t *ddp = dt_decl_push(dt_decl_alloc(CTF_K_ARRAY, NULL));
370 dt_scope_t *dsp = &yypcb->pcb_dstack;
371 dt_decl_t *ndp = ddp;
374 * After pushing the array on to the decl stack, scan ahead for multi-
375 * dimensional array declarations and push the current decl to the
376 * bottom to match the resulting CTF type tree and data layout. Refer
377 * to the comments in dt_decl_type() and ISO C 6.5.2.1 for more info.
379 while (ndp->dd_next != NULL && ndp->dd_next->dd_kind == CTF_K_ARRAY)
380 ndp = ndp->dd_next; /* skip to bottom-most array declaration */
383 if (dnp != NULL && dnp->dn_kind == DT_NODE_TYPE) {
384 xyerror(D_DECL_DYNOBJ,
385 "cannot declare array of associative arrays\n");
387 dsp->ds_decl = ddp->dd_next;
388 ddp->dd_next = ndp->dd_next;
392 if (ddp->dd_next->dd_name != NULL &&
393 strcmp(ddp->dd_next->dd_name, "void") == 0)
394 xyerror(D_DECL_VOIDOBJ, "cannot declare array of void\n");
396 if (dnp != NULL && dnp->dn_kind != DT_NODE_TYPE) {
397 dnp = ddp->dd_node = dt_node_cook(dnp, DT_IDFLG_REF);
399 if (dt_node_is_posconst(dnp) == 0) {
400 xyerror(D_DECL_ARRSUB, "positive integral constant "
401 "expression or tuple signature expected as "
402 "array declaration subscript\n");
405 if (dnp->dn_value > UINT_MAX)
406 xyerror(D_DECL_ARRBIG, "array dimension too big\n");
408 } else if (dnp != NULL) {
410 (void) dt_decl_prototype(dnp, dnp, "array", DT_DP_ANON);
417 * When a function is declared, we need to fudge the decl stack a bit if the
418 * declaration uses the function pointer (*)() syntax. In this case, the
419 * dt_decl_func() call occurs *after* the dt_decl_ptr() call, even though the
420 * resulting type is "pointer to function". To make the pointer land on top,
421 * we check to see if 'pdp' is non-NULL and a pointer. If it is, we search
422 * backward for a decl tagged with DT_DA_PAREN, and if one is found, the func
423 * decl is inserted behind this node in the decl list instead of at the top.
424 * In all cases, the func decl's dd_next pointer is set to the decl chain
425 * for the function's return type and the function parameter list is discarded.
428 dt_decl_func(dt_decl_t *pdp, dt_node_t *dnp)
430 dt_decl_t *ddp = dt_decl_alloc(CTF_K_FUNCTION, NULL);
434 (void) dt_decl_prototype(dnp, dnp, "function",
435 DT_DP_VARARGS | DT_DP_VOID | DT_DP_ANON);
437 if (pdp == NULL || pdp->dd_kind != CTF_K_POINTER)
438 return (dt_decl_push(ddp));
440 while (pdp->dd_next != NULL && !(pdp->dd_next->dd_attr & DT_DA_PAREN))
443 if (pdp->dd_next == NULL)
444 return (dt_decl_push(ddp));
446 ddp->dd_next = pdp->dd_next;
455 return (dt_decl_push(dt_decl_alloc(CTF_K_POINTER, NULL)));
459 dt_decl_sou(uint_t kind, char *name)
461 dt_decl_t *ddp = dt_decl_spec(kind, name);
462 char n[DT_TYPE_NAMELEN];
467 if (yypcb->pcb_idepth != 0)
468 ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
470 ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;
472 if (yypcb->pcb_dstack.ds_next != NULL)
473 flag = CTF_ADD_NONROOT;
477 (void) snprintf(n, sizeof (n), "%s %s",
478 kind == CTF_K_STRUCT ? "struct" : "union",
479 name == NULL ? "(anon)" : name);
481 if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR &&
482 ctf_type_kind(ctfp, type) != CTF_K_FORWARD)
483 xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);
485 if (kind == CTF_K_STRUCT)
486 type = ctf_add_struct(ctfp, flag, name);
488 type = ctf_add_union(ctfp, flag, name);
490 if (type == CTF_ERR || ctf_update(ctfp) == CTF_ERR) {
491 xyerror(D_UNKNOWN, "failed to define %s: %s\n",
492 n, ctf_errmsg(ctf_errno(ctfp)));
498 dt_scope_push(ctfp, type);
503 dt_decl_member(dt_node_t *dnp)
505 dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;
506 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
507 char *ident = yypcb->pcb_dstack.ds_ident;
509 const char *idname = ident ? ident : "(anon)";
510 char n[DT_TYPE_NAMELEN];
512 dtrace_typeinfo_t dtt;
519 longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
522 longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);
524 if (dnp == NULL && ident == NULL)
525 xyerror(D_DECL_MNAME, "member declaration requires a name\n");
527 if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {
528 ddp->dd_kind = CTF_K_INTEGER;
529 (void) dt_decl_check(ddp);
532 if (dt_decl_type(ddp, &dtt) != 0)
533 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
535 if (ident != NULL && strchr(ident, '`') != NULL) {
536 xyerror(D_DECL_SCOPE, "D scoping operator may not be used "
537 "in a member name (%s)\n", ident);
540 if (dtt.dtt_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
541 dtt.dtt_type == DT_DYN_TYPE(yypcb->pcb_hdl)) {
542 xyerror(D_DECL_DYNOBJ,
543 "cannot have dynamic member: %s\n", ident);
546 base = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
547 kind = ctf_type_kind(dtt.dtt_ctfp, base);
548 size = ctf_type_size(dtt.dtt_ctfp, base);
550 if (kind == CTF_K_FORWARD || ((kind == CTF_K_STRUCT ||
551 kind == CTF_K_UNION) && size == 0)) {
552 xyerror(D_DECL_INCOMPLETE, "incomplete struct/union/enum %s: "
553 "%s\n", dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
554 n, sizeof (n)), ident);
558 xyerror(D_DECL_VOIDOBJ, "cannot have void member: %s\n", ident);
561 * If a bit-field qualifier was part of the member declaration, create
562 * a new integer type of the same name and attributes as the base type
563 * and size equal to the specified number of bits. We reset 'dtt' to
564 * refer to this new bit-field type and continue on to add the member.
567 dnp = dt_node_cook(dnp, DT_IDFLG_REF);
570 * A bit-field member with no declarator is permitted to have
571 * size zero and indicates that no more fields are to be packed
572 * into the current storage unit. We ignore these directives
573 * as the underlying ctf code currently does so for all fields.
575 if (ident == NULL && dnp->dn_kind == DT_NODE_INT &&
576 dnp->dn_value == 0) {
581 if (dt_node_is_posconst(dnp) == 0) {
582 xyerror(D_DECL_BFCONST, "positive integral constant "
583 "expression expected as bit-field size\n");
586 if (ctf_type_kind(dtt.dtt_ctfp, base) != CTF_K_INTEGER ||
587 ctf_type_encoding(dtt.dtt_ctfp, base, &cte) == CTF_ERR ||
589 xyerror(D_DECL_BFTYPE, "invalid type for "
590 "bit-field: %s\n", idname);
593 if (dnp->dn_value > cte.cte_bits) {
594 xyerror(D_DECL_BFSIZE, "bit-field too big "
595 "for type: %s\n", idname);
599 cte.cte_bits = (uint_t)dnp->dn_value;
601 dtt.dtt_type = ctf_add_integer(dsp->ds_ctfp,
602 CTF_ADD_NONROOT, ctf_type_name(dtt.dtt_ctfp,
603 dtt.dtt_type, n, sizeof (n)), &cte);
605 if (dtt.dtt_type == CTF_ERR ||
606 ctf_update(dsp->ds_ctfp) == CTF_ERR) {
607 xyerror(D_UNKNOWN, "failed to create type for "
608 "member '%s': %s\n", idname,
609 ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
612 dtt.dtt_ctfp = dsp->ds_ctfp;
617 * If the member type is not defined in the same CTF container as the
618 * one associated with the current scope (i.e. the container for the
619 * struct or union itself) or its parent, copy the member type into
620 * this container and reset dtt to refer to the copied type.
622 if (dtt.dtt_ctfp != dsp->ds_ctfp &&
623 dtt.dtt_ctfp != ctf_parent_file(dsp->ds_ctfp)) {
625 dtt.dtt_type = ctf_add_type(dsp->ds_ctfp,
626 dtt.dtt_ctfp, dtt.dtt_type);
627 dtt.dtt_ctfp = dsp->ds_ctfp;
629 if (dtt.dtt_type == CTF_ERR ||
630 ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
631 xyerror(D_UNKNOWN, "failed to copy type of '%s': %s\n",
632 idname, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
636 if (ctf_add_member(dsp->ds_ctfp, dsp->ds_type,
637 ident, dtt.dtt_type) == CTF_ERR) {
638 xyerror(D_UNKNOWN, "failed to define member '%s': %s\n",
639 idname, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
644 yypcb->pcb_dstack.ds_ident = NULL;
650 dt_decl_hasmembers(const char *name, int value, void *private)
652 return (1); /* abort search and return true if a member exists */
656 dt_decl_enum(char *name)
658 dt_decl_t *ddp = dt_decl_spec(CTF_K_ENUM, name);
659 char n[DT_TYPE_NAMELEN];
664 if (yypcb->pcb_idepth != 0)
665 ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
667 ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;
669 if (yypcb->pcb_dstack.ds_next != NULL)
670 flag = CTF_ADD_NONROOT;
674 (void) snprintf(n, sizeof (n), "enum %s", name ? name : "(anon)");
676 if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR) {
677 if (ctf_enum_iter(ctfp, type, dt_decl_hasmembers, NULL))
678 xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);
679 } else if ((type = ctf_add_enum(ctfp, flag, name)) == CTF_ERR) {
680 xyerror(D_UNKNOWN, "failed to define %s: %s\n",
681 n, ctf_errmsg(ctf_errno(ctfp)));
687 dt_scope_push(ctfp, type);
692 dt_decl_enumerator(char *s, dt_node_t *dnp)
694 dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;
695 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
702 name = alloca(strlen(s) + 1);
703 (void) strcpy(name, s);
707 longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
709 assert(dsp->ds_decl->dd_kind == CTF_K_ENUM);
710 value = dsp->ds_enumval + 1; /* default is previous value plus one */
712 if (strchr(name, '`') != NULL) {
713 xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
714 "an enumerator name (%s)\n", name);
718 * If the enumerator is being assigned a value, cook and check the node
719 * and then free it after we get the value. We also permit references
720 * to identifiers which are previously defined enumerators in the type.
723 if (dnp->dn_kind != DT_NODE_IDENT || ctf_enum_value(
724 dsp->ds_ctfp, dsp->ds_type, dnp->dn_string, &value) != 0) {
725 dnp = dt_node_cook(dnp, DT_IDFLG_REF);
727 if (dnp->dn_kind != DT_NODE_INT) {
728 xyerror(D_DECL_ENCONST, "enumerator '%s' must "
729 "be assigned to an integral constant "
730 "expression\n", name);
733 if ((intmax_t)dnp->dn_value > INT_MAX ||
734 (intmax_t)dnp->dn_value < INT_MIN) {
735 xyerror(D_DECL_ENOFLOW, "enumerator '%s' value "
736 "overflows INT_MAX (%d)\n", name, INT_MAX);
739 value = (int)dnp->dn_value;
744 if (ctf_add_enumerator(dsp->ds_ctfp, dsp->ds_type,
745 name, value) == CTF_ERR || ctf_update(dsp->ds_ctfp) == CTF_ERR) {
746 xyerror(D_UNKNOWN, "failed to define enumerator '%s': %s\n",
747 name, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
750 dsp->ds_enumval = value; /* save most recent value */
753 * If the enumerator name matches an identifier in the global scope,
754 * flag this as an error. We only do this for "D" enumerators to
755 * prevent "C" header file enumerators from conflicting with the ever-
756 * growing list of D built-in global variables and inlines. If a "C"
757 * enumerator conflicts with a global identifier, we add the enumerator
758 * but do not insert a corresponding inline (i.e. the D variable wins).
760 if (dt_idstack_lookup(&yypcb->pcb_globals, name) != NULL) {
761 if (dsp->ds_ctfp == dtp->dt_ddefs->dm_ctfp) {
762 xyerror(D_DECL_IDRED,
763 "identifier redeclared: %s\n", name);
768 dt_dprintf("add global enumerator %s = %d\n", name, value);
770 idp = dt_idhash_insert(dtp->dt_globals, name, DT_IDENT_ENUM,
771 DT_IDFLG_INLINE | DT_IDFLG_REF, 0, _dtrace_defattr, 0,
772 &dt_idops_inline, NULL, dtp->dt_gen);
775 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
778 yyintsuffix[0] = '\0';
781 dnp = dt_node_int(value);
782 dt_node_type_assign(dnp, dsp->ds_ctfp, dsp->ds_type);
784 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
785 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
788 * Remove the INT node from the node allocation list and store it in
789 * din_list and din_root so it persists with and is freed by the ident.
791 assert(yypcb->pcb_list == dnp);
792 yypcb->pcb_list = dnp->dn_link;
795 bzero(inp, sizeof (dt_idnode_t));
800 idp->di_ctfp = dsp->ds_ctfp;
801 idp->di_type = dsp->ds_type;
805 * Look up the type corresponding to the specified decl stack. The scoping of
806 * the underlying type names is handled by dt_type_lookup(). We build up the
807 * name from the specified string and prefixes and then lookup the type. If
808 * we fail, an errmsg is saved and the caller must abort with EDT_COMPILER.
811 dt_decl_type(dt_decl_t *ddp, dtrace_typeinfo_t *tip)
813 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
819 char n[DT_TYPE_NAMELEN];
825 * Based on our current #include depth and decl stack depth, determine
826 * which dynamic CTF module and scope to use when adding any new types.
828 dmp = yypcb->pcb_idepth ? dtp->dt_cdefs : dtp->dt_ddefs;
829 flag = yypcb->pcb_dstack.ds_next ? CTF_ADD_NONROOT : CTF_ADD_ROOT;
832 * If we have already cached a CTF type for this decl, then we just
833 * return the type information for the cached type.
835 if (ddp->dd_ctfp != NULL &&
836 (dmp = dt_module_lookup_by_ctf(dtp, ddp->dd_ctfp)) != NULL) {
837 tip->dtt_object = dmp->dm_name;
838 tip->dtt_ctfp = ddp->dd_ctfp;
839 tip->dtt_type = ddp->dd_type;
844 * Currently CTF treats all function pointers identically. We cache a
845 * representative ID of kind CTF_K_FUNCTION and just return that type.
846 * If we want to support full function declarations, dd_next refers to
847 * the declaration of the function return type, and the parameter list
848 * should be parsed and hung off a new pointer inside of this decl.
850 if (ddp->dd_kind == CTF_K_FUNCTION) {
851 tip->dtt_object = dtp->dt_ddefs->dm_name;
852 tip->dtt_ctfp = DT_FUNC_CTFP(dtp);
853 tip->dtt_type = DT_FUNC_TYPE(dtp);
858 * If the decl is a pointer, resolve the rest of the stack by calling
859 * dt_decl_type() recursively and then compute a pointer to the result.
860 * Similar to the code above, we return a cached id for function ptrs.
862 if (ddp->dd_kind == CTF_K_POINTER) {
863 if (ddp->dd_next->dd_kind == CTF_K_FUNCTION) {
864 tip->dtt_object = dtp->dt_ddefs->dm_name;
865 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
866 tip->dtt_type = DT_FPTR_TYPE(dtp);
870 if ((rv = dt_decl_type(ddp->dd_next, tip)) == 0 &&
871 (rv = dt_type_pointer(tip)) != 0) {
872 xywarn(D_UNKNOWN, "cannot find type: %s*: %s\n",
873 dt_type_name(tip->dtt_ctfp, tip->dtt_type,
874 n, sizeof (n)), ctf_errmsg(dtp->dt_ctferr));
881 * If the decl is an array, we must find the base type and then call
882 * dt_decl_type() recursively and then build an array of the result.
883 * The C and D multi-dimensional array syntax requires that consecutive
884 * array declarations be processed from right-to-left (i.e. top-down
885 * from the perspective of the declaration stack). For example, an
886 * array declaration such as int x[3][5] is stored on the stack as:
888 * (bottom) NULL <- ( INT "int" ) <- ( ARR [3] ) <- ( ARR [5] ) (top)
890 * but means that x is declared to be an array of 3 objects each of
891 * which is an array of 5 integers, or in CTF representation:
893 * type T1:( content=int, nelems=5 ) type T2:( content=T1, nelems=3 )
895 * For more details, refer to K&R[5.7] and ISO C 6.5.2.1. Rather than
896 * overcomplicate the implementation of dt_decl_type(), we push array
897 * declarations down into the stack in dt_decl_array(), above, so that
898 * by the time dt_decl_type() is called, the decl stack looks like:
900 * (bottom) NULL <- ( INT "int" ) <- ( ARR [5] ) <- ( ARR [3] ) (top)
902 * which permits a straightforward recursive descent of the decl stack
903 * to build the corresponding CTF type tree in the appropriate order.
905 if (ddp->dd_kind == CTF_K_ARRAY) {
907 * If the array decl has a parameter list associated with it,
908 * this is an associative array declaration: return <DYN>.
910 if (ddp->dd_node != NULL &&
911 ddp->dd_node->dn_kind == DT_NODE_TYPE) {
912 tip->dtt_object = dtp->dt_ddefs->dm_name;
913 tip->dtt_ctfp = DT_DYN_CTFP(dtp);
914 tip->dtt_type = DT_DYN_TYPE(dtp);
918 if ((rv = dt_decl_type(ddp->dd_next, tip)) != 0)
922 * If the array base type is not defined in the target
923 * container or its parent, copy the type to the target
924 * container and reset dtt_ctfp and dtt_type to the copy.
926 if (tip->dtt_ctfp != dmp->dm_ctfp &&
927 tip->dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
929 tip->dtt_type = ctf_add_type(dmp->dm_ctfp,
930 tip->dtt_ctfp, tip->dtt_type);
931 tip->dtt_ctfp = dmp->dm_ctfp;
933 if (tip->dtt_type == CTF_ERR ||
934 ctf_update(tip->dtt_ctfp) == CTF_ERR) {
935 xywarn(D_UNKNOWN, "failed to copy type: %s\n",
936 ctf_errmsg(ctf_errno(tip->dtt_ctfp)));
942 * The array index type is irrelevant in C and D: just set it
943 * to "long" for all array types that we create on-the-fly.
945 r.ctr_contents = tip->dtt_type;
946 r.ctr_index = ctf_lookup_by_name(tip->dtt_ctfp, "long");
947 r.ctr_nelems = ddp->dd_node ?
948 (uint_t)ddp->dd_node->dn_value : 0;
950 tip->dtt_object = dmp->dm_name;
951 tip->dtt_ctfp = dmp->dm_ctfp;
952 tip->dtt_type = ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &r);
954 if (tip->dtt_type == CTF_ERR ||
955 ctf_update(tip->dtt_ctfp) == CTF_ERR) {
956 xywarn(D_UNKNOWN, "failed to create array type: %s\n",
957 ctf_errmsg(ctf_errno(tip->dtt_ctfp)));
965 * Allocate space for the type name and enough space for the maximum
966 * additional text ("unsigned long long \0" requires 20 more bytes).
968 name = alloca(ddp->dd_name ? strlen(ddp->dd_name) + 20 : 20);
971 switch (ddp->dd_kind) {
974 if (ddp->dd_attr & DT_DA_SIGNED)
975 (void) strcat(name, "signed ");
976 if (ddp->dd_attr & DT_DA_UNSIGNED)
977 (void) strcat(name, "unsigned ");
978 if (ddp->dd_attr & DT_DA_SHORT)
979 (void) strcat(name, "short ");
980 if (ddp->dd_attr & DT_DA_LONG)
981 (void) strcat(name, "long ");
982 if (ddp->dd_attr & DT_DA_LONGLONG)
983 (void) strcat(name, "long long ");
984 if (ddp->dd_attr == 0 && ddp->dd_name == NULL)
985 (void) strcat(name, "int");
988 (void) strcpy(name, "struct ");
991 (void) strcpy(name, "union ");
994 (void) strcpy(name, "enum ");
999 xywarn(D_UNKNOWN, "internal error -- "
1000 "bad decl kind %u\n", ddp->dd_kind);
1005 * Add dd_name unless a short, long, or long long is explicitly
1006 * suffixed by int. We use the C/CTF canonical names for integers.
1008 if (ddp->dd_name != NULL && (ddp->dd_kind != CTF_K_INTEGER ||
1009 (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG)) == 0))
1010 (void) strcat(name, ddp->dd_name);
1013 * Lookup the type. If we find it, we're done. Otherwise create a
1014 * forward tag for the type if it is a struct, union, or enum. If
1015 * we can't find it and we can't create a tag, return failure.
1017 if ((rv = dt_type_lookup(name, tip)) == 0)
1020 switch (ddp->dd_kind) {
1024 type = ctf_add_forward(dmp->dm_ctfp, flag,
1025 ddp->dd_name, ddp->dd_kind);
1028 xywarn(D_UNKNOWN, "failed to resolve type %s: %s\n", name,
1029 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1033 if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
1034 xywarn(D_UNKNOWN, "failed to add forward tag for %s: %s\n",
1035 name, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1039 ddp->dd_ctfp = dmp->dm_ctfp;
1040 ddp->dd_type = type;
1042 tip->dtt_object = dmp->dm_name;
1043 tip->dtt_ctfp = dmp->dm_ctfp;
1044 tip->dtt_type = type;
1050 dt_scope_create(dt_scope_t *dsp)
1052 dsp->ds_decl = NULL;
1053 dsp->ds_next = NULL;
1054 dsp->ds_ident = NULL;
1055 dsp->ds_ctfp = NULL;
1056 dsp->ds_type = CTF_ERR;
1057 dsp->ds_class = DT_DC_DEFAULT;
1058 dsp->ds_enumval = -1;
1062 dt_scope_destroy(dt_scope_t *dsp)
1066 for (; dsp != NULL; dsp = nsp) {
1067 dt_decl_free(dsp->ds_decl);
1068 free(dsp->ds_ident);
1070 if (dsp != &yypcb->pcb_dstack)
1076 dt_scope_push(ctf_file_t *ctfp, ctf_id_t type)
1078 dt_scope_t *rsp = &yypcb->pcb_dstack;
1079 dt_scope_t *dsp = malloc(sizeof (dt_scope_t));
1082 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1084 dsp->ds_decl = rsp->ds_decl;
1085 dsp->ds_next = rsp->ds_next;
1086 dsp->ds_ident = rsp->ds_ident;
1087 dsp->ds_ctfp = ctfp;
1088 dsp->ds_type = type;
1089 dsp->ds_class = rsp->ds_class;
1090 dsp->ds_enumval = rsp->ds_enumval;
1092 dt_scope_create(rsp);
1099 dt_scope_t *rsp = &yypcb->pcb_dstack;
1100 dt_scope_t *dsp = rsp->ds_next;
1103 longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
1105 if (dsp->ds_ctfp != NULL && ctf_update(dsp->ds_ctfp) == CTF_ERR) {
1106 xyerror(D_UNKNOWN, "failed to update type definitions: %s\n",
1107 ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
1110 dt_decl_free(rsp->ds_decl);
1111 free(rsp->ds_ident);
1113 rsp->ds_decl = dsp->ds_decl;
1114 rsp->ds_next = dsp->ds_next;
1115 rsp->ds_ident = dsp->ds_ident;
1116 rsp->ds_ctfp = dsp->ds_ctfp;
1117 rsp->ds_type = dsp->ds_type;
1118 rsp->ds_class = dsp->ds_class;
1119 rsp->ds_enumval = dsp->ds_enumval;
1122 return (rsp->ds_decl);