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 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
36 #include <dt_parser.h>
37 #include <dt_module.h>
41 dt_decl_check(dt_decl_t *ddp)
43 if (ddp->dd_kind == CTF_K_UNKNOWN)
44 return (ddp); /* nothing to check if the type is not yet set */
46 if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "char") == 0 &&
47 (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG))) {
48 xyerror(D_DECL_CHARATTR, "invalid type declaration: short and "
49 "long may not be used with char type\n");
52 if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "void") == 0 &&
53 (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG |
54 (DT_DA_SIGNED | DT_DA_UNSIGNED)))) {
55 xyerror(D_DECL_VOIDATTR, "invalid type declaration: attributes "
56 "may not be used with void type\n");
59 if (ddp->dd_kind != CTF_K_INTEGER &&
60 (ddp->dd_attr & (DT_DA_SIGNED | DT_DA_UNSIGNED))) {
61 xyerror(D_DECL_SIGNINT, "invalid type declaration: signed and "
62 "unsigned may only be used with integer type\n");
65 if (ddp->dd_kind != CTF_K_INTEGER && ddp->dd_kind != CTF_K_FLOAT &&
66 (ddp->dd_attr & (DT_DA_LONG | DT_DA_LONGLONG))) {
67 xyerror(D_DECL_LONGINT, "invalid type declaration: long and "
68 "long long may only be used with integer or "
69 "floating-point type\n");
76 dt_decl_alloc(ushort_t kind, char *name)
78 dt_decl_t *ddp = malloc(sizeof (dt_decl_t));
81 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
86 ddp->dd_type = CTF_ERR;
95 dt_decl_free(dt_decl_t *ddp)
99 for (; ddp != NULL; ddp = ndp) {
102 dt_node_list_free(&ddp->dd_node);
110 dt_scope_t *dsp = &yypcb->pcb_dstack;
111 dt_decl_t *ddp = dsp->ds_decl;
113 while (ddp->dd_next != NULL) {
114 dsp->ds_decl = ddp->dd_next;
122 dt_decl_push(dt_decl_t *ddp)
124 dt_scope_t *dsp = &yypcb->pcb_dstack;
125 dt_decl_t *top = dsp->ds_decl;
128 top->dd_kind == CTF_K_UNKNOWN && top->dd_name == NULL) {
129 top->dd_kind = CTF_K_INTEGER;
130 (void) dt_decl_check(top);
133 assert(ddp->dd_next == NULL);
143 dt_scope_t *dsp = &yypcb->pcb_dstack;
144 dt_decl_t *ddp = dt_decl_top();
148 dsp->ds_ident = NULL;
150 dsp->ds_type = CTF_ERR;
151 dsp->ds_class = DT_DC_DEFAULT;
152 dsp->ds_enumval = -1;
158 dt_decl_pop_param(char **idp)
160 dt_scope_t *dsp = &yypcb->pcb_dstack;
162 if (dsp->ds_class != DT_DC_DEFAULT && dsp->ds_class != DT_DC_REGISTER) {
163 xyerror(D_DECL_PARMCLASS, "inappropriate storage class "
164 "for function or associative array parameter\n");
167 if (idp != NULL && dt_decl_top() != NULL) {
168 *idp = dsp->ds_ident;
169 dsp->ds_ident = NULL;
172 return (dt_decl_pop());
178 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
181 longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);
183 if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {
184 ddp->dd_kind = CTF_K_INTEGER;
185 (void) dt_decl_check(ddp);
192 dt_decl_ident(char *name)
194 dt_scope_t *dsp = &yypcb->pcb_dstack;
195 dt_decl_t *ddp = dsp->ds_decl;
197 if (dsp->ds_ident != NULL) {
199 xyerror(D_DECL_IDENT, "old-style declaration or "
200 "incorrect type specified\n");
203 dsp->ds_ident = name;
206 ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));
212 dt_decl_class(dt_dclass_t class)
214 dt_scope_t *dsp = &yypcb->pcb_dstack;
216 if (dsp->ds_class != DT_DC_DEFAULT) {
217 xyerror(D_DECL_CLASS, "only one storage class allowed "
218 "in a declaration\n");
221 dsp->ds_class = class;
225 * Set the kind and name of the current declaration. If none is allocated,
226 * make a new decl and push it on to the top of our stack. If the name or kind
227 * is already set for the current decl, then we need to fail this declaration.
228 * This can occur because too many types were given (e.g. "int int"), etc.
231 dt_decl_spec(ushort_t kind, char *name)
233 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
236 return (dt_decl_push(dt_decl_alloc(kind, name)));
239 * If we already have a type name specified and we see another type
240 * name, this is an error if the declaration is a typedef. If the
241 * declaration is not a typedef, then the user may be trying to declare
242 * a variable whose name has been returned by lex as a TNAME token:
243 * call dt_decl_ident() as if the grammar's IDENT rule was matched.
245 if (ddp->dd_name != NULL && kind == CTF_K_TYPEDEF) {
246 if (yypcb->pcb_dstack.ds_class != DT_DC_TYPEDEF)
247 return (dt_decl_ident(name));
248 xyerror(D_DECL_IDRED, "identifier redeclared: %s\n", name);
251 if (ddp->dd_name != NULL || ddp->dd_kind != CTF_K_UNKNOWN)
252 xyerror(D_DECL_COMBO, "invalid type combination\n");
257 if (name != NULL && strchr(name, '`') != NULL) {
258 xyerror(D_DECL_SCOPE, "D scoping operator may not be used "
262 return (dt_decl_check(ddp));
266 dt_decl_attr(ushort_t attr)
268 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
271 ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));
276 if (attr == DT_DA_LONG && (ddp->dd_attr & DT_DA_LONG)) {
277 ddp->dd_attr &= ~DT_DA_LONG;
278 attr = DT_DA_LONGLONG;
281 ddp->dd_attr |= attr;
282 return (dt_decl_check(ddp));
286 * Examine the list of formal parameters 'flist' and determine if the formal
287 * name fnp->dn_string is defined in this list (B_TRUE) or not (B_FALSE).
288 * If 'fnp' is in 'flist', do not search beyond 'fnp' itself in 'flist'.
291 dt_decl_protoform(dt_node_t *fnp, dt_node_t *flist)
295 for (dnp = flist; dnp != fnp && dnp != NULL; dnp = dnp->dn_list) {
296 if (dnp->dn_string != NULL &&
297 strcmp(dnp->dn_string, fnp->dn_string) == 0)
305 * Common code for parsing array, function, and probe definition prototypes.
306 * The prototype node list is specified as 'plist'. The formal prototype
307 * against which to compare the prototype is specified as 'flist'. If plist
308 * and flist are the same, we require that named parameters are unique. If
309 * plist and flist are different, we require that named parameters in plist
310 * match a name that is present in flist.
313 dt_decl_prototype(dt_node_t *plist,
314 dt_node_t *flist, const char *kind, uint_t flags)
316 char n[DT_TYPE_NAMELEN];
317 int is_void, v = 0, i = 1;
318 int form = plist != flist;
321 for (dnp = plist; dnp != NULL; dnp = dnp->dn_list, i++) {
323 if (dnp->dn_type == CTF_ERR && !(flags & DT_DP_VARARGS)) {
324 dnerror(dnp, D_DECL_PROTO_VARARGS, "%s prototype may "
325 "not use a variable-length argument list\n", kind);
328 if (dt_node_is_dynamic(dnp) && !(flags & DT_DP_DYNAMIC)) {
329 dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "
330 "use parameter of type %s: %s, parameter #%d\n",
331 kind, dt_node_type_name(dnp, n, sizeof (n)),
332 dnp->dn_string ? dnp->dn_string : "(anonymous)", i);
335 is_void = dt_node_is_void(dnp);
338 if (is_void && !(flags & DT_DP_VOID)) {
339 dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "
340 "use parameter of type %s: %s, parameter #%d\n",
341 kind, dt_node_type_name(dnp, n, sizeof (n)),
342 dnp->dn_string ? dnp->dn_string : "(anonymous)", i);
345 if (is_void && dnp->dn_string != NULL) {
346 dnerror(dnp, D_DECL_PROTO_NAME, "void parameter may "
347 "not have a name: %s\n", dnp->dn_string);
350 if (dnp->dn_string != NULL &&
351 dt_decl_protoform(dnp, flist) != form) {
352 dnerror(dnp, D_DECL_PROTO_FORM, "parameter is "
353 "%s declared in %s prototype: %s, parameter #%d\n",
354 form ? "not" : "already", kind, dnp->dn_string, i);
357 if (dnp->dn_string == NULL &&
358 !is_void && !(flags & DT_DP_ANON)) {
359 dnerror(dnp, D_DECL_PROTO_NAME, "parameter declaration "
360 "requires a name: parameter #%d\n", i);
364 if (v != 0 && plist->dn_list != NULL)
365 xyerror(D_DECL_PROTO_VOID, "void must be sole parameter\n");
367 return (v ? 0 : i - 1); /* return zero if sole parameter is 'void' */
371 dt_decl_array(dt_node_t *dnp)
373 dt_decl_t *ddp = dt_decl_push(dt_decl_alloc(CTF_K_ARRAY, NULL));
374 dt_scope_t *dsp = &yypcb->pcb_dstack;
375 dt_decl_t *ndp = ddp;
378 * After pushing the array on to the decl stack, scan ahead for multi-
379 * dimensional array declarations and push the current decl to the
380 * bottom to match the resulting CTF type tree and data layout. Refer
381 * to the comments in dt_decl_type() and ISO C 6.5.2.1 for more info.
383 while (ndp->dd_next != NULL && ndp->dd_next->dd_kind == CTF_K_ARRAY)
384 ndp = ndp->dd_next; /* skip to bottom-most array declaration */
387 if (dnp != NULL && dnp->dn_kind == DT_NODE_TYPE) {
388 xyerror(D_DECL_DYNOBJ,
389 "cannot declare array of associative arrays\n");
391 dsp->ds_decl = ddp->dd_next;
392 ddp->dd_next = ndp->dd_next;
396 if (ddp->dd_next->dd_name != NULL &&
397 strcmp(ddp->dd_next->dd_name, "void") == 0)
398 xyerror(D_DECL_VOIDOBJ, "cannot declare array of void\n");
400 if (dnp != NULL && dnp->dn_kind != DT_NODE_TYPE) {
401 dnp = ddp->dd_node = dt_node_cook(dnp, DT_IDFLG_REF);
403 if (dt_node_is_posconst(dnp) == 0) {
404 xyerror(D_DECL_ARRSUB, "positive integral constant "
405 "expression or tuple signature expected as "
406 "array declaration subscript\n");
409 if (dnp->dn_value > UINT_MAX)
410 xyerror(D_DECL_ARRBIG, "array dimension too big\n");
412 } else if (dnp != NULL) {
414 (void) dt_decl_prototype(dnp, dnp, "array", DT_DP_ANON);
421 * When a function is declared, we need to fudge the decl stack a bit if the
422 * declaration uses the function pointer (*)() syntax. In this case, the
423 * dt_decl_func() call occurs *after* the dt_decl_ptr() call, even though the
424 * resulting type is "pointer to function". To make the pointer land on top,
425 * we check to see if 'pdp' is non-NULL and a pointer. If it is, we search
426 * backward for a decl tagged with DT_DA_PAREN, and if one is found, the func
427 * decl is inserted behind this node in the decl list instead of at the top.
428 * In all cases, the func decl's dd_next pointer is set to the decl chain
429 * for the function's return type and the function parameter list is discarded.
432 dt_decl_func(dt_decl_t *pdp, dt_node_t *dnp)
434 dt_decl_t *ddp = dt_decl_alloc(CTF_K_FUNCTION, NULL);
438 (void) dt_decl_prototype(dnp, dnp, "function",
439 DT_DP_VARARGS | DT_DP_VOID | DT_DP_ANON);
441 if (pdp == NULL || pdp->dd_kind != CTF_K_POINTER)
442 return (dt_decl_push(ddp));
444 while (pdp->dd_next != NULL && !(pdp->dd_next->dd_attr & DT_DA_PAREN))
447 if (pdp->dd_next == NULL)
448 return (dt_decl_push(ddp));
450 ddp->dd_next = pdp->dd_next;
459 return (dt_decl_push(dt_decl_alloc(CTF_K_POINTER, NULL)));
463 dt_decl_sou(uint_t kind, char *name)
465 dt_decl_t *ddp = dt_decl_spec(kind, name);
466 char n[DT_TYPE_NAMELEN];
471 if (yypcb->pcb_idepth != 0)
472 ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
474 ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;
476 if (yypcb->pcb_dstack.ds_next != NULL)
477 flag = CTF_ADD_NONROOT;
481 (void) snprintf(n, sizeof (n), "%s %s",
482 kind == CTF_K_STRUCT ? "struct" : "union",
483 name == NULL ? "(anon)" : name);
485 if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR &&
486 ctf_type_kind(ctfp, type) != CTF_K_FORWARD)
487 xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);
489 if (kind == CTF_K_STRUCT)
490 type = ctf_add_struct(ctfp, flag, name);
492 type = ctf_add_union(ctfp, flag, name);
494 if (type == CTF_ERR || ctf_update(ctfp) == CTF_ERR) {
495 xyerror(D_UNKNOWN, "failed to define %s: %s\n",
496 n, ctf_errmsg(ctf_errno(ctfp)));
502 dt_scope_push(ctfp, type);
507 dt_decl_member(dt_node_t *dnp)
509 dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;
510 dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
511 char *ident = yypcb->pcb_dstack.ds_ident;
513 const char *idname = ident ? ident : "(anon)";
514 char n[DT_TYPE_NAMELEN];
516 dtrace_typeinfo_t dtt;
523 longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
526 longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);
528 if (dnp == NULL && ident == NULL)
529 xyerror(D_DECL_MNAME, "member declaration requires a name\n");
531 if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {
532 ddp->dd_kind = CTF_K_INTEGER;
533 (void) dt_decl_check(ddp);
536 if (dt_decl_type(ddp, &dtt) != 0)
537 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
539 if (ident != NULL && strchr(ident, '`') != NULL) {
540 xyerror(D_DECL_SCOPE, "D scoping operator may not be used "
541 "in a member name (%s)\n", ident);
544 if (dtt.dtt_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&
545 dtt.dtt_type == DT_DYN_TYPE(yypcb->pcb_hdl)) {
546 xyerror(D_DECL_DYNOBJ,
547 "cannot have dynamic member: %s\n", ident);
550 base = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);
551 kind = ctf_type_kind(dtt.dtt_ctfp, base);
552 size = ctf_type_size(dtt.dtt_ctfp, base);
554 if (kind == CTF_K_FORWARD || ((kind == CTF_K_STRUCT ||
555 kind == CTF_K_UNION) && size == 0)) {
556 xyerror(D_DECL_INCOMPLETE, "incomplete struct/union/enum %s: "
557 "%s\n", dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,
558 n, sizeof (n)), ident);
562 xyerror(D_DECL_VOIDOBJ, "cannot have void member: %s\n", ident);
565 * If a bit-field qualifier was part of the member declaration, create
566 * a new integer type of the same name and attributes as the base type
567 * and size equal to the specified number of bits. We reset 'dtt' to
568 * refer to this new bit-field type and continue on to add the member.
571 dnp = dt_node_cook(dnp, DT_IDFLG_REF);
574 * A bit-field member with no declarator is permitted to have
575 * size zero and indicates that no more fields are to be packed
576 * into the current storage unit. We ignore these directives
577 * as the underlying ctf code currently does so for all fields.
579 if (ident == NULL && dnp->dn_kind == DT_NODE_INT &&
580 dnp->dn_value == 0) {
585 if (dt_node_is_posconst(dnp) == 0) {
586 xyerror(D_DECL_BFCONST, "positive integral constant "
587 "expression expected as bit-field size\n");
590 if (ctf_type_kind(dtt.dtt_ctfp, base) != CTF_K_INTEGER ||
591 ctf_type_encoding(dtt.dtt_ctfp, base, &cte) == CTF_ERR ||
593 xyerror(D_DECL_BFTYPE, "invalid type for "
594 "bit-field: %s\n", idname);
597 if (dnp->dn_value > cte.cte_bits) {
598 xyerror(D_DECL_BFSIZE, "bit-field too big "
599 "for type: %s\n", idname);
603 cte.cte_bits = (uint_t)dnp->dn_value;
605 dtt.dtt_type = ctf_add_integer(dsp->ds_ctfp,
606 CTF_ADD_NONROOT, ctf_type_name(dtt.dtt_ctfp,
607 dtt.dtt_type, n, sizeof (n)), &cte);
609 if (dtt.dtt_type == CTF_ERR ||
610 ctf_update(dsp->ds_ctfp) == CTF_ERR) {
611 xyerror(D_UNKNOWN, "failed to create type for "
612 "member '%s': %s\n", idname,
613 ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
616 dtt.dtt_ctfp = dsp->ds_ctfp;
621 * If the member type is not defined in the same CTF container as the
622 * one associated with the current scope (i.e. the container for the
623 * struct or union itself) or its parent, copy the member type into
624 * this container and reset dtt to refer to the copied type.
626 if (dtt.dtt_ctfp != dsp->ds_ctfp &&
627 dtt.dtt_ctfp != ctf_parent_file(dsp->ds_ctfp)) {
629 dtt.dtt_type = ctf_add_type(dsp->ds_ctfp,
630 dtt.dtt_ctfp, dtt.dtt_type);
631 dtt.dtt_ctfp = dsp->ds_ctfp;
633 if (dtt.dtt_type == CTF_ERR ||
634 ctf_update(dtt.dtt_ctfp) == CTF_ERR) {
635 xyerror(D_UNKNOWN, "failed to copy type of '%s': %s\n",
636 idname, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));
640 if (ctf_add_member(dsp->ds_ctfp, dsp->ds_type,
641 ident, dtt.dtt_type) == CTF_ERR) {
642 xyerror(D_UNKNOWN, "failed to define member '%s': %s\n",
643 idname, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
648 yypcb->pcb_dstack.ds_ident = NULL;
654 dt_decl_hasmembers(const char *name, int value, void *private)
656 return (1); /* abort search and return true if a member exists */
660 dt_decl_enum(char *name)
662 dt_decl_t *ddp = dt_decl_spec(CTF_K_ENUM, name);
663 char n[DT_TYPE_NAMELEN];
668 if (yypcb->pcb_idepth != 0)
669 ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;
671 ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;
673 if (yypcb->pcb_dstack.ds_next != NULL)
674 flag = CTF_ADD_NONROOT;
678 (void) snprintf(n, sizeof (n), "enum %s", name ? name : "(anon)");
680 if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR) {
681 if (ctf_enum_iter(ctfp, type, dt_decl_hasmembers, NULL))
682 xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);
683 } else if ((type = ctf_add_enum(ctfp, flag, name)) == CTF_ERR) {
684 xyerror(D_UNKNOWN, "failed to define %s: %s\n",
685 n, ctf_errmsg(ctf_errno(ctfp)));
691 dt_scope_push(ctfp, type);
696 dt_decl_enumerator(char *s, dt_node_t *dnp)
698 dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;
699 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
706 name = alloca(strlen(s) + 1);
707 (void) strcpy(name, s);
711 longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
713 assert(dsp->ds_decl->dd_kind == CTF_K_ENUM);
714 value = dsp->ds_enumval + 1; /* default is previous value plus one */
716 if (strchr(name, '`') != NULL) {
717 xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "
718 "an enumerator name (%s)\n", name);
722 * If the enumerator is being assigned a value, cook and check the node
723 * and then free it after we get the value. We also permit references
724 * to identifiers which are previously defined enumerators in the type.
727 if (dnp->dn_kind != DT_NODE_IDENT || ctf_enum_value(
728 dsp->ds_ctfp, dsp->ds_type, dnp->dn_string, &value) != 0) {
729 dnp = dt_node_cook(dnp, DT_IDFLG_REF);
731 if (dnp->dn_kind != DT_NODE_INT) {
732 xyerror(D_DECL_ENCONST, "enumerator '%s' must "
733 "be assigned to an integral constant "
734 "expression\n", name);
737 if ((intmax_t)dnp->dn_value > INT_MAX ||
738 (intmax_t)dnp->dn_value < INT_MIN) {
739 xyerror(D_DECL_ENOFLOW, "enumerator '%s' value "
740 "overflows INT_MAX (%d)\n", name, INT_MAX);
743 value = (int)dnp->dn_value;
748 if (ctf_add_enumerator(dsp->ds_ctfp, dsp->ds_type,
749 name, value) == CTF_ERR || ctf_update(dsp->ds_ctfp) == CTF_ERR) {
750 xyerror(D_UNKNOWN, "failed to define enumerator '%s': %s\n",
751 name, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
754 dsp->ds_enumval = value; /* save most recent value */
757 * If the enumerator name matches an identifier in the global scope,
758 * flag this as an error. We only do this for "D" enumerators to
759 * prevent "C" header file enumerators from conflicting with the ever-
760 * growing list of D built-in global variables and inlines. If a "C"
761 * enumerator conflicts with a global identifier, we add the enumerator
762 * but do not insert a corresponding inline (i.e. the D variable wins).
764 if (dt_idstack_lookup(&yypcb->pcb_globals, name) != NULL) {
765 if (dsp->ds_ctfp == dtp->dt_ddefs->dm_ctfp) {
766 xyerror(D_DECL_IDRED,
767 "identifier redeclared: %s\n", name);
772 dt_dprintf("add global enumerator %s = %d\n", name, value);
774 idp = dt_idhash_insert(dtp->dt_globals, name, DT_IDENT_ENUM,
775 DT_IDFLG_INLINE | DT_IDFLG_REF, 0, _dtrace_defattr, 0,
776 &dt_idops_inline, NULL, dtp->dt_gen);
779 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
782 yyintsuffix[0] = '\0';
785 dnp = dt_node_int(value);
786 dt_node_type_assign(dnp, dsp->ds_ctfp, dsp->ds_type);
788 if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)
789 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
792 * Remove the INT node from the node allocation list and store it in
793 * din_list and din_root so it persists with and is freed by the ident.
795 assert(yypcb->pcb_list == dnp);
796 yypcb->pcb_list = dnp->dn_link;
799 bzero(inp, sizeof (dt_idnode_t));
804 idp->di_ctfp = dsp->ds_ctfp;
805 idp->di_type = dsp->ds_type;
809 * Look up the type corresponding to the specified decl stack. The scoping of
810 * the underlying type names is handled by dt_type_lookup(). We build up the
811 * name from the specified string and prefixes and then lookup the type. If
812 * we fail, an errmsg is saved and the caller must abort with EDT_COMPILER.
815 dt_decl_type(dt_decl_t *ddp, dtrace_typeinfo_t *tip)
817 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
823 char n[DT_TYPE_NAMELEN];
829 * Based on our current #include depth and decl stack depth, determine
830 * which dynamic CTF module and scope to use when adding any new types.
832 dmp = yypcb->pcb_idepth ? dtp->dt_cdefs : dtp->dt_ddefs;
833 flag = yypcb->pcb_dstack.ds_next ? CTF_ADD_NONROOT : CTF_ADD_ROOT;
836 * If we have already cached a CTF type for this decl, then we just
837 * return the type information for the cached type.
839 if (ddp->dd_ctfp != NULL &&
840 (dmp = dt_module_lookup_by_ctf(dtp, ddp->dd_ctfp)) != NULL) {
841 tip->dtt_object = dmp->dm_name;
842 tip->dtt_ctfp = ddp->dd_ctfp;
843 tip->dtt_type = ddp->dd_type;
848 * Currently CTF treats all function pointers identically. We cache a
849 * representative ID of kind CTF_K_FUNCTION and just return that type.
850 * If we want to support full function declarations, dd_next refers to
851 * the declaration of the function return type, and the parameter list
852 * should be parsed and hung off a new pointer inside of this decl.
854 if (ddp->dd_kind == CTF_K_FUNCTION) {
855 tip->dtt_object = dtp->dt_ddefs->dm_name;
856 tip->dtt_ctfp = DT_FUNC_CTFP(dtp);
857 tip->dtt_type = DT_FUNC_TYPE(dtp);
862 * If the decl is a pointer, resolve the rest of the stack by calling
863 * dt_decl_type() recursively and then compute a pointer to the result.
864 * Similar to the code above, we return a cached id for function ptrs.
866 if (ddp->dd_kind == CTF_K_POINTER) {
867 if (ddp->dd_next->dd_kind == CTF_K_FUNCTION) {
868 tip->dtt_object = dtp->dt_ddefs->dm_name;
869 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
870 tip->dtt_type = DT_FPTR_TYPE(dtp);
874 if ((rv = dt_decl_type(ddp->dd_next, tip)) == 0 &&
875 (rv = dt_type_pointer(tip)) != 0) {
876 xywarn(D_UNKNOWN, "cannot find type: %s*: %s\n",
877 dt_type_name(tip->dtt_ctfp, tip->dtt_type,
878 n, sizeof (n)), ctf_errmsg(dtp->dt_ctferr));
885 * If the decl is an array, we must find the base type and then call
886 * dt_decl_type() recursively and then build an array of the result.
887 * The C and D multi-dimensional array syntax requires that consecutive
888 * array declarations be processed from right-to-left (i.e. top-down
889 * from the perspective of the declaration stack). For example, an
890 * array declaration such as int x[3][5] is stored on the stack as:
892 * (bottom) NULL <- ( INT "int" ) <- ( ARR [3] ) <- ( ARR [5] ) (top)
894 * but means that x is declared to be an array of 3 objects each of
895 * which is an array of 5 integers, or in CTF representation:
897 * type T1:( content=int, nelems=5 ) type T2:( content=T1, nelems=3 )
899 * For more details, refer to K&R[5.7] and ISO C 6.5.2.1. Rather than
900 * overcomplicate the implementation of dt_decl_type(), we push array
901 * declarations down into the stack in dt_decl_array(), above, so that
902 * by the time dt_decl_type() is called, the decl stack looks like:
904 * (bottom) NULL <- ( INT "int" ) <- ( ARR [5] ) <- ( ARR [3] ) (top)
906 * which permits a straightforward recursive descent of the decl stack
907 * to build the corresponding CTF type tree in the appropriate order.
909 if (ddp->dd_kind == CTF_K_ARRAY) {
911 * If the array decl has a parameter list associated with it,
912 * this is an associative array declaration: return <DYN>.
914 if (ddp->dd_node != NULL &&
915 ddp->dd_node->dn_kind == DT_NODE_TYPE) {
916 tip->dtt_object = dtp->dt_ddefs->dm_name;
917 tip->dtt_ctfp = DT_DYN_CTFP(dtp);
918 tip->dtt_type = DT_DYN_TYPE(dtp);
922 if ((rv = dt_decl_type(ddp->dd_next, tip)) != 0)
926 * If the array base type is not defined in the target
927 * container or its parent, copy the type to the target
928 * container and reset dtt_ctfp and dtt_type to the copy.
930 if (tip->dtt_ctfp != dmp->dm_ctfp &&
931 tip->dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {
933 tip->dtt_type = ctf_add_type(dmp->dm_ctfp,
934 tip->dtt_ctfp, tip->dtt_type);
935 tip->dtt_ctfp = dmp->dm_ctfp;
937 if (tip->dtt_type == CTF_ERR ||
938 ctf_update(tip->dtt_ctfp) == CTF_ERR) {
939 xywarn(D_UNKNOWN, "failed to copy type: %s\n",
940 ctf_errmsg(ctf_errno(tip->dtt_ctfp)));
946 * The array index type is irrelevant in C and D: just set it
947 * to "long" for all array types that we create on-the-fly.
949 r.ctr_contents = tip->dtt_type;
950 r.ctr_index = ctf_lookup_by_name(tip->dtt_ctfp, "long");
951 r.ctr_nelems = ddp->dd_node ?
952 (uint_t)ddp->dd_node->dn_value : 0;
954 tip->dtt_object = dmp->dm_name;
955 tip->dtt_ctfp = dmp->dm_ctfp;
956 tip->dtt_type = ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &r);
958 if (tip->dtt_type == CTF_ERR ||
959 ctf_update(tip->dtt_ctfp) == CTF_ERR) {
960 xywarn(D_UNKNOWN, "failed to create array type: %s\n",
961 ctf_errmsg(ctf_errno(tip->dtt_ctfp)));
969 * Allocate space for the type name and enough space for the maximum
970 * additional text ("unsigned long long \0" requires 20 more bytes).
972 name = alloca(ddp->dd_name ? strlen(ddp->dd_name) + 20 : 20);
975 switch (ddp->dd_kind) {
978 if (ddp->dd_attr & DT_DA_SIGNED)
979 (void) strcat(name, "signed ");
980 if (ddp->dd_attr & DT_DA_UNSIGNED)
981 (void) strcat(name, "unsigned ");
982 if (ddp->dd_attr & DT_DA_SHORT)
983 (void) strcat(name, "short ");
984 if (ddp->dd_attr & DT_DA_LONG)
985 (void) strcat(name, "long ");
986 if (ddp->dd_attr & DT_DA_LONGLONG)
987 (void) strcat(name, "long long ");
988 if (ddp->dd_attr == 0 && ddp->dd_name == NULL)
989 (void) strcat(name, "int");
992 (void) strcpy(name, "struct ");
995 (void) strcpy(name, "union ");
998 (void) strcpy(name, "enum ");
1003 xywarn(D_UNKNOWN, "internal error -- "
1004 "bad decl kind %u\n", ddp->dd_kind);
1009 * Add dd_name unless a short, long, or long long is explicitly
1010 * suffixed by int. We use the C/CTF canonical names for integers.
1012 if (ddp->dd_name != NULL && (ddp->dd_kind != CTF_K_INTEGER ||
1013 (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG)) == 0))
1014 (void) strcat(name, ddp->dd_name);
1017 * Lookup the type. If we find it, we're done. Otherwise create a
1018 * forward tag for the type if it is a struct, union, or enum. If
1019 * we can't find it and we can't create a tag, return failure.
1021 if ((rv = dt_type_lookup(name, tip)) == 0)
1024 switch (ddp->dd_kind) {
1028 type = ctf_add_forward(dmp->dm_ctfp, flag,
1029 ddp->dd_name, ddp->dd_kind);
1032 xywarn(D_UNKNOWN, "failed to resolve type %s: %s\n", name,
1033 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1037 if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {
1038 xywarn(D_UNKNOWN, "failed to add forward tag for %s: %s\n",
1039 name, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1043 ddp->dd_ctfp = dmp->dm_ctfp;
1044 ddp->dd_type = type;
1046 tip->dtt_object = dmp->dm_name;
1047 tip->dtt_ctfp = dmp->dm_ctfp;
1048 tip->dtt_type = type;
1054 dt_scope_create(dt_scope_t *dsp)
1056 dsp->ds_decl = NULL;
1057 dsp->ds_next = NULL;
1058 dsp->ds_ident = NULL;
1059 dsp->ds_ctfp = NULL;
1060 dsp->ds_type = CTF_ERR;
1061 dsp->ds_class = DT_DC_DEFAULT;
1062 dsp->ds_enumval = -1;
1066 dt_scope_destroy(dt_scope_t *dsp)
1070 for (; dsp != NULL; dsp = nsp) {
1071 dt_decl_free(dsp->ds_decl);
1072 free(dsp->ds_ident);
1074 if (dsp != &yypcb->pcb_dstack)
1080 dt_scope_push(ctf_file_t *ctfp, ctf_id_t type)
1082 dt_scope_t *rsp = &yypcb->pcb_dstack;
1083 dt_scope_t *dsp = malloc(sizeof (dt_scope_t));
1086 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1088 dsp->ds_decl = rsp->ds_decl;
1089 dsp->ds_next = rsp->ds_next;
1090 dsp->ds_ident = rsp->ds_ident;
1091 dsp->ds_ctfp = ctfp;
1092 dsp->ds_type = type;
1093 dsp->ds_class = rsp->ds_class;
1094 dsp->ds_enumval = rsp->ds_enumval;
1096 dt_scope_create(rsp);
1103 dt_scope_t *rsp = &yypcb->pcb_dstack;
1104 dt_scope_t *dsp = rsp->ds_next;
1107 longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);
1109 if (dsp->ds_ctfp != NULL && ctf_update(dsp->ds_ctfp) == CTF_ERR) {
1110 xyerror(D_UNKNOWN, "failed to update type definitions: %s\n",
1111 ctf_errmsg(ctf_errno(dsp->ds_ctfp)));
1114 dt_decl_free(rsp->ds_decl);
1115 free(rsp->ds_ident);
1117 rsp->ds_decl = dsp->ds_decl;
1118 rsp->ds_next = dsp->ds_next;
1119 rsp->ds_ident = dsp->ds_ident;
1120 rsp->ds_ctfp = dsp->ds_ctfp;
1121 rsp->ds_type = dsp->ds_type;
1122 rsp->ds_class = dsp->ds_class;
1123 rsp->ds_enumval = dsp->ds_enumval;
1126 return (rsp->ds_decl);