17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 523a1cceaSRoger A. Faulkner * Common Development and Distribution License (the "License"). 623a1cceaSRoger A. Faulkner * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate 2223a1cceaSRoger A. Faulkner /* 2323a1cceaSRoger A. Faulkner * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 247266c4fdSPaul Dagnelie * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 25*7fd79137SRobert Mustacchi * Copyright (c) 2015 Joyent, Inc. All rights reserved. 2623a1cceaSRoger A. Faulkner */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <strings.h> 297c478bd9Sstevel@tonic-gate #include <stdlib.h> 307c478bd9Sstevel@tonic-gate #include <limits.h> 317c478bd9Sstevel@tonic-gate #include <alloca.h> 327c478bd9Sstevel@tonic-gate #include <assert.h> 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include <dt_decl.h> 357c478bd9Sstevel@tonic-gate #include <dt_parser.h> 367c478bd9Sstevel@tonic-gate #include <dt_module.h> 377c478bd9Sstevel@tonic-gate #include <dt_impl.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate static dt_decl_t * 407c478bd9Sstevel@tonic-gate dt_decl_check(dt_decl_t *ddp) 417c478bd9Sstevel@tonic-gate { 427c478bd9Sstevel@tonic-gate if (ddp->dd_kind == CTF_K_UNKNOWN) 437c478bd9Sstevel@tonic-gate return (ddp); /* nothing to check if the type is not yet set */ 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "char") == 0 && 467c478bd9Sstevel@tonic-gate (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG))) { 477c478bd9Sstevel@tonic-gate xyerror(D_DECL_CHARATTR, "invalid type declaration: short and " 487c478bd9Sstevel@tonic-gate "long may not be used with char type\n"); 497c478bd9Sstevel@tonic-gate } 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "void") == 0 && 527c478bd9Sstevel@tonic-gate (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG | 537c478bd9Sstevel@tonic-gate (DT_DA_SIGNED | DT_DA_UNSIGNED)))) { 547c478bd9Sstevel@tonic-gate xyerror(D_DECL_VOIDATTR, "invalid type declaration: attributes " 557c478bd9Sstevel@tonic-gate "may not be used with void type\n"); 567c478bd9Sstevel@tonic-gate } 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate if (ddp->dd_kind != CTF_K_INTEGER && 597c478bd9Sstevel@tonic-gate (ddp->dd_attr & (DT_DA_SIGNED | DT_DA_UNSIGNED))) { 607c478bd9Sstevel@tonic-gate xyerror(D_DECL_SIGNINT, "invalid type declaration: signed and " 617c478bd9Sstevel@tonic-gate "unsigned may only be used with integer type\n"); 627c478bd9Sstevel@tonic-gate } 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate if (ddp->dd_kind != CTF_K_INTEGER && ddp->dd_kind != CTF_K_FLOAT && 657c478bd9Sstevel@tonic-gate (ddp->dd_attr & (DT_DA_LONG | DT_DA_LONGLONG))) { 667c478bd9Sstevel@tonic-gate xyerror(D_DECL_LONGINT, "invalid type declaration: long and " 677c478bd9Sstevel@tonic-gate "long long may only be used with integer or " 687c478bd9Sstevel@tonic-gate "floating-point type\n"); 697c478bd9Sstevel@tonic-gate } 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate return (ddp); 727c478bd9Sstevel@tonic-gate } 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate dt_decl_t * 757c478bd9Sstevel@tonic-gate dt_decl_alloc(ushort_t kind, char *name) 767c478bd9Sstevel@tonic-gate { 777c478bd9Sstevel@tonic-gate dt_decl_t *ddp = malloc(sizeof (dt_decl_t)); 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate if (ddp == NULL) 807c478bd9Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate ddp->dd_kind = kind; 837c478bd9Sstevel@tonic-gate ddp->dd_attr = 0; 847c478bd9Sstevel@tonic-gate ddp->dd_ctfp = NULL; 857c478bd9Sstevel@tonic-gate ddp->dd_type = CTF_ERR; 867c478bd9Sstevel@tonic-gate ddp->dd_name = name; 877c478bd9Sstevel@tonic-gate ddp->dd_node = NULL; 887c478bd9Sstevel@tonic-gate ddp->dd_next = NULL; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate return (ddp); 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate void 947c478bd9Sstevel@tonic-gate dt_decl_free(dt_decl_t *ddp) 957c478bd9Sstevel@tonic-gate { 967c478bd9Sstevel@tonic-gate dt_decl_t *ndp; 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate for (; ddp != NULL; ddp = ndp) { 997c478bd9Sstevel@tonic-gate ndp = ddp->dd_next; 1007c478bd9Sstevel@tonic-gate free(ddp->dd_name); 1017c478bd9Sstevel@tonic-gate dt_node_list_free(&ddp->dd_node); 1027c478bd9Sstevel@tonic-gate free(ddp); 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate void 1077c478bd9Sstevel@tonic-gate dt_decl_reset(void) 1087c478bd9Sstevel@tonic-gate { 1097c478bd9Sstevel@tonic-gate dt_scope_t *dsp = &yypcb->pcb_dstack; 1107c478bd9Sstevel@tonic-gate dt_decl_t *ddp = dsp->ds_decl; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate while (ddp->dd_next != NULL) { 1137c478bd9Sstevel@tonic-gate dsp->ds_decl = ddp->dd_next; 1147c478bd9Sstevel@tonic-gate ddp->dd_next = NULL; 1157c478bd9Sstevel@tonic-gate dt_decl_free(ddp); 1167c478bd9Sstevel@tonic-gate ddp = dsp->ds_decl; 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate dt_decl_t * 1217c478bd9Sstevel@tonic-gate dt_decl_push(dt_decl_t *ddp) 1227c478bd9Sstevel@tonic-gate { 1237c478bd9Sstevel@tonic-gate dt_scope_t *dsp = &yypcb->pcb_dstack; 1247c478bd9Sstevel@tonic-gate dt_decl_t *top = dsp->ds_decl; 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate if (top != NULL && 1277c478bd9Sstevel@tonic-gate top->dd_kind == CTF_K_UNKNOWN && top->dd_name == NULL) { 1287c478bd9Sstevel@tonic-gate top->dd_kind = CTF_K_INTEGER; 1297c478bd9Sstevel@tonic-gate (void) dt_decl_check(top); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate assert(ddp->dd_next == NULL); 1337c478bd9Sstevel@tonic-gate ddp->dd_next = top; 1347c478bd9Sstevel@tonic-gate dsp->ds_decl = ddp; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate return (ddp); 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate dt_decl_t * 1407c478bd9Sstevel@tonic-gate dt_decl_pop(void) 1417c478bd9Sstevel@tonic-gate { 1427c478bd9Sstevel@tonic-gate dt_scope_t *dsp = &yypcb->pcb_dstack; 1437c478bd9Sstevel@tonic-gate dt_decl_t *ddp = dt_decl_top(); 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate dsp->ds_decl = NULL; 1467c478bd9Sstevel@tonic-gate free(dsp->ds_ident); 1477c478bd9Sstevel@tonic-gate dsp->ds_ident = NULL; 1487c478bd9Sstevel@tonic-gate dsp->ds_ctfp = NULL; 1497c478bd9Sstevel@tonic-gate dsp->ds_type = CTF_ERR; 1507c478bd9Sstevel@tonic-gate dsp->ds_class = DT_DC_DEFAULT; 1517c478bd9Sstevel@tonic-gate dsp->ds_enumval = -1; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate return (ddp); 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate dt_decl_t * 1577c478bd9Sstevel@tonic-gate dt_decl_pop_param(char **idp) 1587c478bd9Sstevel@tonic-gate { 1597c478bd9Sstevel@tonic-gate dt_scope_t *dsp = &yypcb->pcb_dstack; 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate if (dsp->ds_class != DT_DC_DEFAULT && dsp->ds_class != DT_DC_REGISTER) { 1627c478bd9Sstevel@tonic-gate xyerror(D_DECL_PARMCLASS, "inappropriate storage class " 1637c478bd9Sstevel@tonic-gate "for function or associative array parameter\n"); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate if (idp != NULL && dt_decl_top() != NULL) { 1677c478bd9Sstevel@tonic-gate *idp = dsp->ds_ident; 1687c478bd9Sstevel@tonic-gate dsp->ds_ident = NULL; 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate return (dt_decl_pop()); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate dt_decl_t * 1757c478bd9Sstevel@tonic-gate dt_decl_top(void) 1767c478bd9Sstevel@tonic-gate { 1777c478bd9Sstevel@tonic-gate dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate if (ddp == NULL) 1807c478bd9Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NODECL); 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) { 1837c478bd9Sstevel@tonic-gate ddp->dd_kind = CTF_K_INTEGER; 1847c478bd9Sstevel@tonic-gate (void) dt_decl_check(ddp); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate return (ddp); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate dt_decl_t * 1917c478bd9Sstevel@tonic-gate dt_decl_ident(char *name) 1927c478bd9Sstevel@tonic-gate { 1937c478bd9Sstevel@tonic-gate dt_scope_t *dsp = &yypcb->pcb_dstack; 1947c478bd9Sstevel@tonic-gate dt_decl_t *ddp = dsp->ds_decl; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate if (dsp->ds_ident != NULL) { 1977c478bd9Sstevel@tonic-gate free(name); 1987c478bd9Sstevel@tonic-gate xyerror(D_DECL_IDENT, "old-style declaration or " 1997c478bd9Sstevel@tonic-gate "incorrect type specified\n"); 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate dsp->ds_ident = name; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate if (ddp == NULL) 2057c478bd9Sstevel@tonic-gate ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL)); 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate return (ddp); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate void 2117c478bd9Sstevel@tonic-gate dt_decl_class(dt_dclass_t class) 2127c478bd9Sstevel@tonic-gate { 2137c478bd9Sstevel@tonic-gate dt_scope_t *dsp = &yypcb->pcb_dstack; 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate if (dsp->ds_class != DT_DC_DEFAULT) { 2167c478bd9Sstevel@tonic-gate xyerror(D_DECL_CLASS, "only one storage class allowed " 2177c478bd9Sstevel@tonic-gate "in a declaration\n"); 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate dsp->ds_class = class; 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /* 2247c478bd9Sstevel@tonic-gate * Set the kind and name of the current declaration. If none is allocated, 2257c478bd9Sstevel@tonic-gate * make a new decl and push it on to the top of our stack. If the name or kind 2267c478bd9Sstevel@tonic-gate * is already set for the current decl, then we need to fail this declaration. 2277c478bd9Sstevel@tonic-gate * This can occur because too many types were given (e.g. "int int"), etc. 2287c478bd9Sstevel@tonic-gate */ 2297c478bd9Sstevel@tonic-gate dt_decl_t * 2307c478bd9Sstevel@tonic-gate dt_decl_spec(ushort_t kind, char *name) 2317c478bd9Sstevel@tonic-gate { 2327c478bd9Sstevel@tonic-gate dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate if (ddp == NULL) 2357c478bd9Sstevel@tonic-gate return (dt_decl_push(dt_decl_alloc(kind, name))); 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate /* 2387c478bd9Sstevel@tonic-gate * If we already have a type name specified and we see another type 2397c478bd9Sstevel@tonic-gate * name, this is an error if the declaration is a typedef. If the 2407c478bd9Sstevel@tonic-gate * declaration is not a typedef, then the user may be trying to declare 2417c478bd9Sstevel@tonic-gate * a variable whose name has been returned by lex as a TNAME token: 2427c478bd9Sstevel@tonic-gate * call dt_decl_ident() as if the grammar's IDENT rule was matched. 2437c478bd9Sstevel@tonic-gate */ 2447c478bd9Sstevel@tonic-gate if (ddp->dd_name != NULL && kind == CTF_K_TYPEDEF) { 2457c478bd9Sstevel@tonic-gate if (yypcb->pcb_dstack.ds_class != DT_DC_TYPEDEF) 2467c478bd9Sstevel@tonic-gate return (dt_decl_ident(name)); 2477c478bd9Sstevel@tonic-gate xyerror(D_DECL_IDRED, "identifier redeclared: %s\n", name); 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate if (ddp->dd_name != NULL || ddp->dd_kind != CTF_K_UNKNOWN) 2517c478bd9Sstevel@tonic-gate xyerror(D_DECL_COMBO, "invalid type combination\n"); 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate ddp->dd_kind = kind; 2547c478bd9Sstevel@tonic-gate ddp->dd_name = name; 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate return (dt_decl_check(ddp)); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate dt_decl_t * 2607c478bd9Sstevel@tonic-gate dt_decl_attr(ushort_t attr) 2617c478bd9Sstevel@tonic-gate { 2627c478bd9Sstevel@tonic-gate dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl; 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate if (ddp == NULL) { 2657c478bd9Sstevel@tonic-gate ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL)); 2667c478bd9Sstevel@tonic-gate ddp->dd_attr = attr; 2677c478bd9Sstevel@tonic-gate return (ddp); 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate 2707266c4fdSPaul Dagnelie if ((attr & DT_DA_LONG) && (ddp->dd_attr & DT_DA_LONGLONG)) { 2717266c4fdSPaul Dagnelie xyerror(D_DECL_COMBO, "the attribute 'long' may only " 2727266c4fdSPaul Dagnelie "be used at most twice in a declaration"); 2737266c4fdSPaul Dagnelie } 2747266c4fdSPaul Dagnelie 2757266c4fdSPaul Dagnelie if ((attr & DT_DA_SHORT) && (ddp->dd_attr & DT_DA_SHORT)) { 2767266c4fdSPaul Dagnelie xyerror(D_DECL_COMBO, "the attribute 'short' may only be " 2777266c4fdSPaul Dagnelie "used at most once in a declaration"); 2787266c4fdSPaul Dagnelie } 2797266c4fdSPaul Dagnelie 2807266c4fdSPaul Dagnelie if ((attr & DT_DA_SIGNED) && (ddp->dd_attr & DT_DA_SIGNED)) { 2817266c4fdSPaul Dagnelie xyerror(D_DECL_COMBO, "the attribute 'signed' may only be " 2827266c4fdSPaul Dagnelie "used at most once in a declaration"); 2837266c4fdSPaul Dagnelie } 2847266c4fdSPaul Dagnelie 2857266c4fdSPaul Dagnelie if ((attr & DT_DA_UNSIGNED) && (ddp->dd_attr & DT_DA_UNSIGNED)) { 2867266c4fdSPaul Dagnelie xyerror(D_DECL_COMBO, "the attribute 'unsigned' may only be " 2877266c4fdSPaul Dagnelie "used at most once in a declaration"); 2887266c4fdSPaul Dagnelie } 2897266c4fdSPaul Dagnelie 2907c478bd9Sstevel@tonic-gate if (attr == DT_DA_LONG && (ddp->dd_attr & DT_DA_LONG)) { 2917c478bd9Sstevel@tonic-gate ddp->dd_attr &= ~DT_DA_LONG; 2927c478bd9Sstevel@tonic-gate attr = DT_DA_LONGLONG; 2937c478bd9Sstevel@tonic-gate } 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate ddp->dd_attr |= attr; 2967c478bd9Sstevel@tonic-gate return (dt_decl_check(ddp)); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate /* 3007c478bd9Sstevel@tonic-gate * Examine the list of formal parameters 'flist' and determine if the formal 3017c478bd9Sstevel@tonic-gate * name fnp->dn_string is defined in this list (B_TRUE) or not (B_FALSE). 3027c478bd9Sstevel@tonic-gate * If 'fnp' is in 'flist', do not search beyond 'fnp' itself in 'flist'. 3037c478bd9Sstevel@tonic-gate */ 3047c478bd9Sstevel@tonic-gate static int 3057c478bd9Sstevel@tonic-gate dt_decl_protoform(dt_node_t *fnp, dt_node_t *flist) 3067c478bd9Sstevel@tonic-gate { 3077c478bd9Sstevel@tonic-gate dt_node_t *dnp; 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate for (dnp = flist; dnp != fnp && dnp != NULL; dnp = dnp->dn_list) { 3107c478bd9Sstevel@tonic-gate if (dnp->dn_string != NULL && 3117c478bd9Sstevel@tonic-gate strcmp(dnp->dn_string, fnp->dn_string) == 0) 3127c478bd9Sstevel@tonic-gate return (B_TRUE); 3137c478bd9Sstevel@tonic-gate } 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate return (B_FALSE); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate /* 3197c478bd9Sstevel@tonic-gate * Common code for parsing array, function, and probe definition prototypes. 3207c478bd9Sstevel@tonic-gate * The prototype node list is specified as 'plist'. The formal prototype 3217c478bd9Sstevel@tonic-gate * against which to compare the prototype is specified as 'flist'. If plist 3227c478bd9Sstevel@tonic-gate * and flist are the same, we require that named parameters are unique. If 3237c478bd9Sstevel@tonic-gate * plist and flist are different, we require that named parameters in plist 3247c478bd9Sstevel@tonic-gate * match a name that is present in flist. 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate int 3277c478bd9Sstevel@tonic-gate dt_decl_prototype(dt_node_t *plist, 3287c478bd9Sstevel@tonic-gate dt_node_t *flist, const char *kind, uint_t flags) 3297c478bd9Sstevel@tonic-gate { 3307c478bd9Sstevel@tonic-gate char n[DT_TYPE_NAMELEN]; 3317c478bd9Sstevel@tonic-gate int is_void, v = 0, i = 1; 3327c478bd9Sstevel@tonic-gate int form = plist != flist; 3337c478bd9Sstevel@tonic-gate dt_node_t *dnp; 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate for (dnp = plist; dnp != NULL; dnp = dnp->dn_list, i++) { 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate if (dnp->dn_type == CTF_ERR && !(flags & DT_DP_VARARGS)) { 3387c478bd9Sstevel@tonic-gate dnerror(dnp, D_DECL_PROTO_VARARGS, "%s prototype may " 3397c478bd9Sstevel@tonic-gate "not use a variable-length argument list\n", kind); 3407c478bd9Sstevel@tonic-gate } 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate if (dt_node_is_dynamic(dnp) && !(flags & DT_DP_DYNAMIC)) { 3437c478bd9Sstevel@tonic-gate dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not " 3447c478bd9Sstevel@tonic-gate "use parameter of type %s: %s, parameter #%d\n", 3457c478bd9Sstevel@tonic-gate kind, dt_node_type_name(dnp, n, sizeof (n)), 3467c478bd9Sstevel@tonic-gate dnp->dn_string ? dnp->dn_string : "(anonymous)", i); 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate is_void = dt_node_is_void(dnp); 3507c478bd9Sstevel@tonic-gate v += is_void; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate if (is_void && !(flags & DT_DP_VOID)) { 3537c478bd9Sstevel@tonic-gate dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not " 3547c478bd9Sstevel@tonic-gate "use parameter of type %s: %s, parameter #%d\n", 3557c478bd9Sstevel@tonic-gate kind, dt_node_type_name(dnp, n, sizeof (n)), 3567c478bd9Sstevel@tonic-gate dnp->dn_string ? dnp->dn_string : "(anonymous)", i); 3577c478bd9Sstevel@tonic-gate } 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate if (is_void && dnp->dn_string != NULL) { 3607c478bd9Sstevel@tonic-gate dnerror(dnp, D_DECL_PROTO_NAME, "void parameter may " 3617c478bd9Sstevel@tonic-gate "not have a name: %s\n", dnp->dn_string); 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate if (dnp->dn_string != NULL && 3657c478bd9Sstevel@tonic-gate dt_decl_protoform(dnp, flist) != form) { 3667c478bd9Sstevel@tonic-gate dnerror(dnp, D_DECL_PROTO_FORM, "parameter is " 3677c478bd9Sstevel@tonic-gate "%s declared in %s prototype: %s, parameter #%d\n", 3687c478bd9Sstevel@tonic-gate form ? "not" : "already", kind, dnp->dn_string, i); 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate if (dnp->dn_string == NULL && 3727c478bd9Sstevel@tonic-gate !is_void && !(flags & DT_DP_ANON)) { 3737c478bd9Sstevel@tonic-gate dnerror(dnp, D_DECL_PROTO_NAME, "parameter declaration " 3747c478bd9Sstevel@tonic-gate "requires a name: parameter #%d\n", i); 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate if (v != 0 && plist->dn_list != NULL) 3797c478bd9Sstevel@tonic-gate xyerror(D_DECL_PROTO_VOID, "void must be sole parameter\n"); 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate return (v ? 0 : i - 1); /* return zero if sole parameter is 'void' */ 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate dt_decl_t * 3857c478bd9Sstevel@tonic-gate dt_decl_array(dt_node_t *dnp) 3867c478bd9Sstevel@tonic-gate { 3877c478bd9Sstevel@tonic-gate dt_decl_t *ddp = dt_decl_push(dt_decl_alloc(CTF_K_ARRAY, NULL)); 3887c478bd9Sstevel@tonic-gate dt_scope_t *dsp = &yypcb->pcb_dstack; 3897c478bd9Sstevel@tonic-gate dt_decl_t *ndp = ddp; 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate /* 3927c478bd9Sstevel@tonic-gate * After pushing the array on to the decl stack, scan ahead for multi- 3937c478bd9Sstevel@tonic-gate * dimensional array declarations and push the current decl to the 3947c478bd9Sstevel@tonic-gate * bottom to match the resulting CTF type tree and data layout. Refer 3957c478bd9Sstevel@tonic-gate * to the comments in dt_decl_type() and ISO C 6.5.2.1 for more info. 3967c478bd9Sstevel@tonic-gate */ 3977c478bd9Sstevel@tonic-gate while (ndp->dd_next != NULL && ndp->dd_next->dd_kind == CTF_K_ARRAY) 3987c478bd9Sstevel@tonic-gate ndp = ndp->dd_next; /* skip to bottom-most array declaration */ 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate if (ndp != ddp) { 4017c478bd9Sstevel@tonic-gate if (dnp != NULL && dnp->dn_kind == DT_NODE_TYPE) { 4027c478bd9Sstevel@tonic-gate xyerror(D_DECL_DYNOBJ, 4037c478bd9Sstevel@tonic-gate "cannot declare array of associative arrays\n"); 4047c478bd9Sstevel@tonic-gate } 4057c478bd9Sstevel@tonic-gate dsp->ds_decl = ddp->dd_next; 4067c478bd9Sstevel@tonic-gate ddp->dd_next = ndp->dd_next; 4077c478bd9Sstevel@tonic-gate ndp->dd_next = ddp; 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate if (ddp->dd_next->dd_name != NULL && 4117c478bd9Sstevel@tonic-gate strcmp(ddp->dd_next->dd_name, "void") == 0) 4127c478bd9Sstevel@tonic-gate xyerror(D_DECL_VOIDOBJ, "cannot declare array of void\n"); 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate if (dnp != NULL && dnp->dn_kind != DT_NODE_TYPE) { 4157c478bd9Sstevel@tonic-gate dnp = ddp->dd_node = dt_node_cook(dnp, DT_IDFLG_REF); 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate if (dt_node_is_posconst(dnp) == 0) { 4187c478bd9Sstevel@tonic-gate xyerror(D_DECL_ARRSUB, "positive integral constant " 4197c478bd9Sstevel@tonic-gate "expression or tuple signature expected as " 4207c478bd9Sstevel@tonic-gate "array declaration subscript\n"); 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate if (dnp->dn_value > UINT_MAX) 4247c478bd9Sstevel@tonic-gate xyerror(D_DECL_ARRBIG, "array dimension too big\n"); 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate } else if (dnp != NULL) { 4277c478bd9Sstevel@tonic-gate ddp->dd_node = dnp; 4287c478bd9Sstevel@tonic-gate (void) dt_decl_prototype(dnp, dnp, "array", DT_DP_ANON); 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate return (ddp); 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate /* 4357c478bd9Sstevel@tonic-gate * When a function is declared, we need to fudge the decl stack a bit if the 4367c478bd9Sstevel@tonic-gate * declaration uses the function pointer (*)() syntax. In this case, the 4377c478bd9Sstevel@tonic-gate * dt_decl_func() call occurs *after* the dt_decl_ptr() call, even though the 4387c478bd9Sstevel@tonic-gate * resulting type is "pointer to function". To make the pointer land on top, 4397c478bd9Sstevel@tonic-gate * we check to see if 'pdp' is non-NULL and a pointer. If it is, we search 4407c478bd9Sstevel@tonic-gate * backward for a decl tagged with DT_DA_PAREN, and if one is found, the func 4417c478bd9Sstevel@tonic-gate * decl is inserted behind this node in the decl list instead of at the top. 4427c478bd9Sstevel@tonic-gate * In all cases, the func decl's dd_next pointer is set to the decl chain 4437c478bd9Sstevel@tonic-gate * for the function's return type and the function parameter list is discarded. 4447c478bd9Sstevel@tonic-gate */ 4457c478bd9Sstevel@tonic-gate dt_decl_t * 4467c478bd9Sstevel@tonic-gate dt_decl_func(dt_decl_t *pdp, dt_node_t *dnp) 4477c478bd9Sstevel@tonic-gate { 4487c478bd9Sstevel@tonic-gate dt_decl_t *ddp = dt_decl_alloc(CTF_K_FUNCTION, NULL); 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate ddp->dd_node = dnp; 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate (void) dt_decl_prototype(dnp, dnp, "function", 4537c478bd9Sstevel@tonic-gate DT_DP_VARARGS | DT_DP_VOID | DT_DP_ANON); 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate if (pdp == NULL || pdp->dd_kind != CTF_K_POINTER) 4567c478bd9Sstevel@tonic-gate return (dt_decl_push(ddp)); 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate while (pdp->dd_next != NULL && !(pdp->dd_next->dd_attr & DT_DA_PAREN)) 4597c478bd9Sstevel@tonic-gate pdp = pdp->dd_next; 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate if (pdp->dd_next == NULL) 4627c478bd9Sstevel@tonic-gate return (dt_decl_push(ddp)); 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate ddp->dd_next = pdp->dd_next; 4657c478bd9Sstevel@tonic-gate pdp->dd_next = ddp; 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate return (pdp); 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate dt_decl_t * 4717c478bd9Sstevel@tonic-gate dt_decl_ptr(void) 4727c478bd9Sstevel@tonic-gate { 4737c478bd9Sstevel@tonic-gate return (dt_decl_push(dt_decl_alloc(CTF_K_POINTER, NULL))); 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate dt_decl_t * 4777c478bd9Sstevel@tonic-gate dt_decl_sou(uint_t kind, char *name) 4787c478bd9Sstevel@tonic-gate { 4797c478bd9Sstevel@tonic-gate dt_decl_t *ddp = dt_decl_spec(kind, name); 4807c478bd9Sstevel@tonic-gate char n[DT_TYPE_NAMELEN]; 4817c478bd9Sstevel@tonic-gate ctf_file_t *ctfp; 4827c478bd9Sstevel@tonic-gate ctf_id_t type; 4837c478bd9Sstevel@tonic-gate uint_t flag; 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate if (yypcb->pcb_idepth != 0) 4867c478bd9Sstevel@tonic-gate ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp; 4877c478bd9Sstevel@tonic-gate else 4887c478bd9Sstevel@tonic-gate ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp; 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate if (yypcb->pcb_dstack.ds_next != NULL) 4917c478bd9Sstevel@tonic-gate flag = CTF_ADD_NONROOT; 4927c478bd9Sstevel@tonic-gate else 4937c478bd9Sstevel@tonic-gate flag = CTF_ADD_ROOT; 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate (void) snprintf(n, sizeof (n), "%s %s", 4967c478bd9Sstevel@tonic-gate kind == CTF_K_STRUCT ? "struct" : "union", 4977c478bd9Sstevel@tonic-gate name == NULL ? "(anon)" : name); 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR && 5007c478bd9Sstevel@tonic-gate ctf_type_kind(ctfp, type) != CTF_K_FORWARD) 5017c478bd9Sstevel@tonic-gate xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n); 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate if (kind == CTF_K_STRUCT) 5047c478bd9Sstevel@tonic-gate type = ctf_add_struct(ctfp, flag, name); 5057c478bd9Sstevel@tonic-gate else 5067c478bd9Sstevel@tonic-gate type = ctf_add_union(ctfp, flag, name); 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate if (type == CTF_ERR || ctf_update(ctfp) == CTF_ERR) { 5097c478bd9Sstevel@tonic-gate xyerror(D_UNKNOWN, "failed to define %s: %s\n", 5107c478bd9Sstevel@tonic-gate n, ctf_errmsg(ctf_errno(ctfp))); 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate ddp->dd_ctfp = ctfp; 5147c478bd9Sstevel@tonic-gate ddp->dd_type = type; 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate dt_scope_push(ctfp, type); 5177c478bd9Sstevel@tonic-gate return (ddp); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate void 5217c478bd9Sstevel@tonic-gate dt_decl_member(dt_node_t *dnp) 5227c478bd9Sstevel@tonic-gate { 5237c478bd9Sstevel@tonic-gate dt_scope_t *dsp = yypcb->pcb_dstack.ds_next; 5247c478bd9Sstevel@tonic-gate dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl; 5257c478bd9Sstevel@tonic-gate char *ident = yypcb->pcb_dstack.ds_ident; 5267c478bd9Sstevel@tonic-gate 5277c478bd9Sstevel@tonic-gate const char *idname = ident ? ident : "(anon)"; 5287c478bd9Sstevel@tonic-gate char n[DT_TYPE_NAMELEN]; 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate dtrace_typeinfo_t dtt; 5317c478bd9Sstevel@tonic-gate ctf_encoding_t cte; 5327c478bd9Sstevel@tonic-gate ctf_id_t base; 5337c478bd9Sstevel@tonic-gate uint_t kind; 5347c478bd9Sstevel@tonic-gate ssize_t size; 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate if (dsp == NULL) 5377c478bd9Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE); 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate if (ddp == NULL) 5407c478bd9Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NODECL); 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate if (dnp == NULL && ident == NULL) 5437c478bd9Sstevel@tonic-gate xyerror(D_DECL_MNAME, "member declaration requires a name\n"); 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) { 5467c478bd9Sstevel@tonic-gate ddp->dd_kind = CTF_K_INTEGER; 5477c478bd9Sstevel@tonic-gate (void) dt_decl_check(ddp); 5487c478bd9Sstevel@tonic-gate } 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate if (dt_decl_type(ddp, &dtt) != 0) 5517c478bd9Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER); 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate if (ident != NULL && strchr(ident, '`') != NULL) { 5547c478bd9Sstevel@tonic-gate xyerror(D_DECL_SCOPE, "D scoping operator may not be used " 5557c478bd9Sstevel@tonic-gate "in a member name (%s)\n", ident); 5567c478bd9Sstevel@tonic-gate } 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate if (dtt.dtt_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) && 5597c478bd9Sstevel@tonic-gate dtt.dtt_type == DT_DYN_TYPE(yypcb->pcb_hdl)) { 5607c478bd9Sstevel@tonic-gate xyerror(D_DECL_DYNOBJ, 5617c478bd9Sstevel@tonic-gate "cannot have dynamic member: %s\n", ident); 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate base = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type); 5657c478bd9Sstevel@tonic-gate kind = ctf_type_kind(dtt.dtt_ctfp, base); 5667c478bd9Sstevel@tonic-gate size = ctf_type_size(dtt.dtt_ctfp, base); 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate if (kind == CTF_K_FORWARD || ((kind == CTF_K_STRUCT || 5697c478bd9Sstevel@tonic-gate kind == CTF_K_UNION) && size == 0)) { 5707c478bd9Sstevel@tonic-gate xyerror(D_DECL_INCOMPLETE, "incomplete struct/union/enum %s: " 5717c478bd9Sstevel@tonic-gate "%s\n", dt_type_name(dtt.dtt_ctfp, dtt.dtt_type, 5727c478bd9Sstevel@tonic-gate n, sizeof (n)), ident); 5737c478bd9Sstevel@tonic-gate } 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate if (size == 0) 5767c478bd9Sstevel@tonic-gate xyerror(D_DECL_VOIDOBJ, "cannot have void member: %s\n", ident); 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate /* 5797c478bd9Sstevel@tonic-gate * If a bit-field qualifier was part of the member declaration, create 5807c478bd9Sstevel@tonic-gate * a new integer type of the same name and attributes as the base type 5817c478bd9Sstevel@tonic-gate * and size equal to the specified number of bits. We reset 'dtt' to 5827c478bd9Sstevel@tonic-gate * refer to this new bit-field type and continue on to add the member. 5837c478bd9Sstevel@tonic-gate */ 5847c478bd9Sstevel@tonic-gate if (dnp != NULL) { 5857c478bd9Sstevel@tonic-gate dnp = dt_node_cook(dnp, DT_IDFLG_REF); 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate /* 5887c478bd9Sstevel@tonic-gate * A bit-field member with no declarator is permitted to have 5897c478bd9Sstevel@tonic-gate * size zero and indicates that no more fields are to be packed 5907c478bd9Sstevel@tonic-gate * into the current storage unit. We ignore these directives 5917c478bd9Sstevel@tonic-gate * as the underlying ctf code currently does so for all fields. 5927c478bd9Sstevel@tonic-gate */ 5937c478bd9Sstevel@tonic-gate if (ident == NULL && dnp->dn_kind == DT_NODE_INT && 5947c478bd9Sstevel@tonic-gate dnp->dn_value == 0) { 5957c478bd9Sstevel@tonic-gate dt_node_free(dnp); 5967c478bd9Sstevel@tonic-gate goto done; 5977c478bd9Sstevel@tonic-gate } 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate if (dt_node_is_posconst(dnp) == 0) { 6007c478bd9Sstevel@tonic-gate xyerror(D_DECL_BFCONST, "positive integral constant " 6017c478bd9Sstevel@tonic-gate "expression expected as bit-field size\n"); 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate if (ctf_type_kind(dtt.dtt_ctfp, base) != CTF_K_INTEGER || 6057c478bd9Sstevel@tonic-gate ctf_type_encoding(dtt.dtt_ctfp, base, &cte) == CTF_ERR || 6067c478bd9Sstevel@tonic-gate IS_VOID(cte)) { 6077c478bd9Sstevel@tonic-gate xyerror(D_DECL_BFTYPE, "invalid type for " 6087c478bd9Sstevel@tonic-gate "bit-field: %s\n", idname); 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate if (dnp->dn_value > cte.cte_bits) { 6127c478bd9Sstevel@tonic-gate xyerror(D_DECL_BFSIZE, "bit-field too big " 6137c478bd9Sstevel@tonic-gate "for type: %s\n", idname); 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate cte.cte_offset = 0; 6177c478bd9Sstevel@tonic-gate cte.cte_bits = (uint_t)dnp->dn_value; 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate dtt.dtt_type = ctf_add_integer(dsp->ds_ctfp, 6207c478bd9Sstevel@tonic-gate CTF_ADD_NONROOT, ctf_type_name(dtt.dtt_ctfp, 6217c478bd9Sstevel@tonic-gate dtt.dtt_type, n, sizeof (n)), &cte); 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate if (dtt.dtt_type == CTF_ERR || 6247c478bd9Sstevel@tonic-gate ctf_update(dsp->ds_ctfp) == CTF_ERR) { 6257c478bd9Sstevel@tonic-gate xyerror(D_UNKNOWN, "failed to create type for " 6267c478bd9Sstevel@tonic-gate "member '%s': %s\n", idname, 6277c478bd9Sstevel@tonic-gate ctf_errmsg(ctf_errno(dsp->ds_ctfp))); 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate dtt.dtt_ctfp = dsp->ds_ctfp; 6317c478bd9Sstevel@tonic-gate dt_node_free(dnp); 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate /* 6357c478bd9Sstevel@tonic-gate * If the member type is not defined in the same CTF container as the 6367c478bd9Sstevel@tonic-gate * one associated with the current scope (i.e. the container for the 6377c478bd9Sstevel@tonic-gate * struct or union itself) or its parent, copy the member type into 6387c478bd9Sstevel@tonic-gate * this container and reset dtt to refer to the copied type. 6397c478bd9Sstevel@tonic-gate */ 6407c478bd9Sstevel@tonic-gate if (dtt.dtt_ctfp != dsp->ds_ctfp && 6417c478bd9Sstevel@tonic-gate dtt.dtt_ctfp != ctf_parent_file(dsp->ds_ctfp)) { 6427c478bd9Sstevel@tonic-gate 6437c478bd9Sstevel@tonic-gate dtt.dtt_type = ctf_add_type(dsp->ds_ctfp, 6447c478bd9Sstevel@tonic-gate dtt.dtt_ctfp, dtt.dtt_type); 6457c478bd9Sstevel@tonic-gate dtt.dtt_ctfp = dsp->ds_ctfp; 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate if (dtt.dtt_type == CTF_ERR || 6487c478bd9Sstevel@tonic-gate ctf_update(dtt.dtt_ctfp) == CTF_ERR) { 6497c478bd9Sstevel@tonic-gate xyerror(D_UNKNOWN, "failed to copy type of '%s': %s\n", 6507c478bd9Sstevel@tonic-gate idname, ctf_errmsg(ctf_errno(dtt.dtt_ctfp))); 6517c478bd9Sstevel@tonic-gate } 6527c478bd9Sstevel@tonic-gate } 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate if (ctf_add_member(dsp->ds_ctfp, dsp->ds_type, 655*7fd79137SRobert Mustacchi ident, dtt.dtt_type, ULONG_MAX) == CTF_ERR) { 6567c478bd9Sstevel@tonic-gate xyerror(D_UNKNOWN, "failed to define member '%s': %s\n", 6577c478bd9Sstevel@tonic-gate idname, ctf_errmsg(ctf_errno(dsp->ds_ctfp))); 6587c478bd9Sstevel@tonic-gate } 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate done: 6617c478bd9Sstevel@tonic-gate free(ident); 6627c478bd9Sstevel@tonic-gate yypcb->pcb_dstack.ds_ident = NULL; 6637c478bd9Sstevel@tonic-gate dt_decl_reset(); 6647c478bd9Sstevel@tonic-gate } 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 6677c478bd9Sstevel@tonic-gate static int 6687c478bd9Sstevel@tonic-gate dt_decl_hasmembers(const char *name, int value, void *private) 6697c478bd9Sstevel@tonic-gate { 6707c478bd9Sstevel@tonic-gate return (1); /* abort search and return true if a member exists */ 6717c478bd9Sstevel@tonic-gate } 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate dt_decl_t * 6747c478bd9Sstevel@tonic-gate dt_decl_enum(char *name) 6757c478bd9Sstevel@tonic-gate { 6767c478bd9Sstevel@tonic-gate dt_decl_t *ddp = dt_decl_spec(CTF_K_ENUM, name); 6777c478bd9Sstevel@tonic-gate char n[DT_TYPE_NAMELEN]; 6787c478bd9Sstevel@tonic-gate ctf_file_t *ctfp; 6797c478bd9Sstevel@tonic-gate ctf_id_t type; 6807c478bd9Sstevel@tonic-gate uint_t flag; 6817c478bd9Sstevel@tonic-gate 6827c478bd9Sstevel@tonic-gate if (yypcb->pcb_idepth != 0) 6837c478bd9Sstevel@tonic-gate ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp; 6847c478bd9Sstevel@tonic-gate else 6857c478bd9Sstevel@tonic-gate ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp; 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate if (yypcb->pcb_dstack.ds_next != NULL) 6887c478bd9Sstevel@tonic-gate flag = CTF_ADD_NONROOT; 6897c478bd9Sstevel@tonic-gate else 6907c478bd9Sstevel@tonic-gate flag = CTF_ADD_ROOT; 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate (void) snprintf(n, sizeof (n), "enum %s", name ? name : "(anon)"); 6937c478bd9Sstevel@tonic-gate 6947c478bd9Sstevel@tonic-gate if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR) { 6957c478bd9Sstevel@tonic-gate if (ctf_enum_iter(ctfp, type, dt_decl_hasmembers, NULL)) 6967c478bd9Sstevel@tonic-gate xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n); 6977c478bd9Sstevel@tonic-gate } else if ((type = ctf_add_enum(ctfp, flag, name)) == CTF_ERR) { 6987c478bd9Sstevel@tonic-gate xyerror(D_UNKNOWN, "failed to define %s: %s\n", 6997c478bd9Sstevel@tonic-gate n, ctf_errmsg(ctf_errno(ctfp))); 7007c478bd9Sstevel@tonic-gate } 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate ddp->dd_ctfp = ctfp; 7037c478bd9Sstevel@tonic-gate ddp->dd_type = type; 7047c478bd9Sstevel@tonic-gate 7057c478bd9Sstevel@tonic-gate dt_scope_push(ctfp, type); 7067c478bd9Sstevel@tonic-gate return (ddp); 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate void 7107c478bd9Sstevel@tonic-gate dt_decl_enumerator(char *s, dt_node_t *dnp) 7117c478bd9Sstevel@tonic-gate { 7127c478bd9Sstevel@tonic-gate dt_scope_t *dsp = yypcb->pcb_dstack.ds_next; 7137c478bd9Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl; 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate dt_idnode_t *inp; 7167c478bd9Sstevel@tonic-gate dt_ident_t *idp; 7177c478bd9Sstevel@tonic-gate char *name; 7187c478bd9Sstevel@tonic-gate int value; 7197c478bd9Sstevel@tonic-gate 72023a1cceaSRoger A. Faulkner name = strdupa(s); 7217c478bd9Sstevel@tonic-gate free(s); 7227c478bd9Sstevel@tonic-gate 7237c478bd9Sstevel@tonic-gate if (dsp == NULL) 7247c478bd9Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE); 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate assert(dsp->ds_decl->dd_kind == CTF_K_ENUM); 7277c478bd9Sstevel@tonic-gate value = dsp->ds_enumval + 1; /* default is previous value plus one */ 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate if (strchr(name, '`') != NULL) { 7307c478bd9Sstevel@tonic-gate xyerror(D_DECL_SCOPE, "D scoping operator may not be used in " 7317c478bd9Sstevel@tonic-gate "an enumerator name (%s)\n", name); 7327c478bd9Sstevel@tonic-gate } 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate /* 7357c478bd9Sstevel@tonic-gate * If the enumerator is being assigned a value, cook and check the node 7367c478bd9Sstevel@tonic-gate * and then free it after we get the value. We also permit references 7377c478bd9Sstevel@tonic-gate * to identifiers which are previously defined enumerators in the type. 7387c478bd9Sstevel@tonic-gate */ 7397c478bd9Sstevel@tonic-gate if (dnp != NULL) { 7407c478bd9Sstevel@tonic-gate if (dnp->dn_kind != DT_NODE_IDENT || ctf_enum_value( 7417c478bd9Sstevel@tonic-gate dsp->ds_ctfp, dsp->ds_type, dnp->dn_string, &value) != 0) { 7427c478bd9Sstevel@tonic-gate dnp = dt_node_cook(dnp, DT_IDFLG_REF); 7437c478bd9Sstevel@tonic-gate 7447c478bd9Sstevel@tonic-gate if (dnp->dn_kind != DT_NODE_INT) { 7457c478bd9Sstevel@tonic-gate xyerror(D_DECL_ENCONST, "enumerator '%s' must " 7467c478bd9Sstevel@tonic-gate "be assigned to an integral constant " 7477c478bd9Sstevel@tonic-gate "expression\n", name); 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate 7507c478bd9Sstevel@tonic-gate if ((intmax_t)dnp->dn_value > INT_MAX || 7517c478bd9Sstevel@tonic-gate (intmax_t)dnp->dn_value < INT_MIN) { 7527c478bd9Sstevel@tonic-gate xyerror(D_DECL_ENOFLOW, "enumerator '%s' value " 7537c478bd9Sstevel@tonic-gate "overflows INT_MAX (%d)\n", name, INT_MAX); 7547c478bd9Sstevel@tonic-gate } 7557c478bd9Sstevel@tonic-gate 7567c478bd9Sstevel@tonic-gate value = (int)dnp->dn_value; 7577c478bd9Sstevel@tonic-gate } 7587c478bd9Sstevel@tonic-gate dt_node_free(dnp); 7597c478bd9Sstevel@tonic-gate } 7607c478bd9Sstevel@tonic-gate 7617c478bd9Sstevel@tonic-gate if (ctf_add_enumerator(dsp->ds_ctfp, dsp->ds_type, 7627c478bd9Sstevel@tonic-gate name, value) == CTF_ERR || ctf_update(dsp->ds_ctfp) == CTF_ERR) { 7637c478bd9Sstevel@tonic-gate xyerror(D_UNKNOWN, "failed to define enumerator '%s': %s\n", 7647c478bd9Sstevel@tonic-gate name, ctf_errmsg(ctf_errno(dsp->ds_ctfp))); 7657c478bd9Sstevel@tonic-gate } 7667c478bd9Sstevel@tonic-gate 7677c478bd9Sstevel@tonic-gate dsp->ds_enumval = value; /* save most recent value */ 7687c478bd9Sstevel@tonic-gate 7697c478bd9Sstevel@tonic-gate /* 7707c478bd9Sstevel@tonic-gate * If the enumerator name matches an identifier in the global scope, 7717c478bd9Sstevel@tonic-gate * flag this as an error. We only do this for "D" enumerators to 7727c478bd9Sstevel@tonic-gate * prevent "C" header file enumerators from conflicting with the ever- 7737c478bd9Sstevel@tonic-gate * growing list of D built-in global variables and inlines. If a "C" 7747c478bd9Sstevel@tonic-gate * enumerator conflicts with a global identifier, we add the enumerator 7757c478bd9Sstevel@tonic-gate * but do not insert a corresponding inline (i.e. the D variable wins). 7767c478bd9Sstevel@tonic-gate */ 7777c478bd9Sstevel@tonic-gate if (dt_idstack_lookup(&yypcb->pcb_globals, name) != NULL) { 7787c478bd9Sstevel@tonic-gate if (dsp->ds_ctfp == dtp->dt_ddefs->dm_ctfp) { 7797c478bd9Sstevel@tonic-gate xyerror(D_DECL_IDRED, 7807c478bd9Sstevel@tonic-gate "identifier redeclared: %s\n", name); 7817c478bd9Sstevel@tonic-gate } else 7827c478bd9Sstevel@tonic-gate return; 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate 7857c478bd9Sstevel@tonic-gate dt_dprintf("add global enumerator %s = %d\n", name, value); 7867c478bd9Sstevel@tonic-gate 7877c478bd9Sstevel@tonic-gate idp = dt_idhash_insert(dtp->dt_globals, name, DT_IDENT_ENUM, 7887c478bd9Sstevel@tonic-gate DT_IDFLG_INLINE | DT_IDFLG_REF, 0, _dtrace_defattr, 0, 7897c478bd9Sstevel@tonic-gate &dt_idops_inline, NULL, dtp->dt_gen); 7907c478bd9Sstevel@tonic-gate 7917c478bd9Sstevel@tonic-gate if (idp == NULL) 7927c478bd9Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate yyintprefix = 0; 7957c478bd9Sstevel@tonic-gate yyintsuffix[0] = '\0'; 7967c478bd9Sstevel@tonic-gate yyintdecimal = 0; 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate dnp = dt_node_int(value); 799a386cc11SRobert Mustacchi dt_node_type_assign(dnp, dsp->ds_ctfp, dsp->ds_type, B_FALSE); 8007c478bd9Sstevel@tonic-gate 8017c478bd9Sstevel@tonic-gate if ((inp = malloc(sizeof (dt_idnode_t))) == NULL) 8027c478bd9Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 8037c478bd9Sstevel@tonic-gate 8047c478bd9Sstevel@tonic-gate /* 8057c478bd9Sstevel@tonic-gate * Remove the INT node from the node allocation list and store it in 8067c478bd9Sstevel@tonic-gate * din_list and din_root so it persists with and is freed by the ident. 8077c478bd9Sstevel@tonic-gate */ 8087c478bd9Sstevel@tonic-gate assert(yypcb->pcb_list == dnp); 8097c478bd9Sstevel@tonic-gate yypcb->pcb_list = dnp->dn_link; 8107c478bd9Sstevel@tonic-gate dnp->dn_link = NULL; 8117c478bd9Sstevel@tonic-gate 8127c478bd9Sstevel@tonic-gate bzero(inp, sizeof (dt_idnode_t)); 8137c478bd9Sstevel@tonic-gate inp->din_list = dnp; 8147c478bd9Sstevel@tonic-gate inp->din_root = dnp; 8157c478bd9Sstevel@tonic-gate 8167c478bd9Sstevel@tonic-gate idp->di_iarg = inp; 8177c478bd9Sstevel@tonic-gate idp->di_ctfp = dsp->ds_ctfp; 8187c478bd9Sstevel@tonic-gate idp->di_type = dsp->ds_type; 8197c478bd9Sstevel@tonic-gate } 8207c478bd9Sstevel@tonic-gate 8217c478bd9Sstevel@tonic-gate /* 8227c478bd9Sstevel@tonic-gate * Look up the type corresponding to the specified decl stack. The scoping of 8237c478bd9Sstevel@tonic-gate * the underlying type names is handled by dt_type_lookup(). We build up the 8247c478bd9Sstevel@tonic-gate * name from the specified string and prefixes and then lookup the type. If 8257c478bd9Sstevel@tonic-gate * we fail, an errmsg is saved and the caller must abort with EDT_COMPILER. 8267c478bd9Sstevel@tonic-gate */ 8277c478bd9Sstevel@tonic-gate int 8287c478bd9Sstevel@tonic-gate dt_decl_type(dt_decl_t *ddp, dtrace_typeinfo_t *tip) 8297c478bd9Sstevel@tonic-gate { 8307c478bd9Sstevel@tonic-gate dtrace_hdl_t *dtp = yypcb->pcb_hdl; 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate dt_module_t *dmp; 8337c478bd9Sstevel@tonic-gate ctf_arinfo_t r; 8347c478bd9Sstevel@tonic-gate ctf_id_t type; 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate char n[DT_TYPE_NAMELEN]; 8377c478bd9Sstevel@tonic-gate uint_t flag; 8387c478bd9Sstevel@tonic-gate char *name; 8397c478bd9Sstevel@tonic-gate int rv; 8407c478bd9Sstevel@tonic-gate 841a386cc11SRobert Mustacchi tip->dtt_flags = 0; 842a386cc11SRobert Mustacchi 8437c478bd9Sstevel@tonic-gate /* 8447c478bd9Sstevel@tonic-gate * Based on our current #include depth and decl stack depth, determine 8457c478bd9Sstevel@tonic-gate * which dynamic CTF module and scope to use when adding any new types. 8467c478bd9Sstevel@tonic-gate */ 8477c478bd9Sstevel@tonic-gate dmp = yypcb->pcb_idepth ? dtp->dt_cdefs : dtp->dt_ddefs; 8487c478bd9Sstevel@tonic-gate flag = yypcb->pcb_dstack.ds_next ? CTF_ADD_NONROOT : CTF_ADD_ROOT; 8497c478bd9Sstevel@tonic-gate 850a386cc11SRobert Mustacchi if (ddp->dd_attr & DT_DA_USER) 851a386cc11SRobert Mustacchi tip->dtt_flags = DTT_FL_USER; 852a386cc11SRobert Mustacchi 8537c478bd9Sstevel@tonic-gate /* 8547c478bd9Sstevel@tonic-gate * If we have already cached a CTF type for this decl, then we just 8557c478bd9Sstevel@tonic-gate * return the type information for the cached type. 8567c478bd9Sstevel@tonic-gate */ 8577c478bd9Sstevel@tonic-gate if (ddp->dd_ctfp != NULL && 8587c478bd9Sstevel@tonic-gate (dmp = dt_module_lookup_by_ctf(dtp, ddp->dd_ctfp)) != NULL) { 8597c478bd9Sstevel@tonic-gate tip->dtt_object = dmp->dm_name; 8607c478bd9Sstevel@tonic-gate tip->dtt_ctfp = ddp->dd_ctfp; 8617c478bd9Sstevel@tonic-gate tip->dtt_type = ddp->dd_type; 8627c478bd9Sstevel@tonic-gate return (0); 8637c478bd9Sstevel@tonic-gate } 8647c478bd9Sstevel@tonic-gate 8657c478bd9Sstevel@tonic-gate /* 8667c478bd9Sstevel@tonic-gate * Currently CTF treats all function pointers identically. We cache a 8677c478bd9Sstevel@tonic-gate * representative ID of kind CTF_K_FUNCTION and just return that type. 8687c478bd9Sstevel@tonic-gate * If we want to support full function declarations, dd_next refers to 8697c478bd9Sstevel@tonic-gate * the declaration of the function return type, and the parameter list 8707c478bd9Sstevel@tonic-gate * should be parsed and hung off a new pointer inside of this decl. 8717c478bd9Sstevel@tonic-gate */ 8727c478bd9Sstevel@tonic-gate if (ddp->dd_kind == CTF_K_FUNCTION) { 8737c478bd9Sstevel@tonic-gate tip->dtt_object = dtp->dt_ddefs->dm_name; 8747c478bd9Sstevel@tonic-gate tip->dtt_ctfp = DT_FUNC_CTFP(dtp); 8757c478bd9Sstevel@tonic-gate tip->dtt_type = DT_FUNC_TYPE(dtp); 8767c478bd9Sstevel@tonic-gate return (0); 8777c478bd9Sstevel@tonic-gate } 8787c478bd9Sstevel@tonic-gate 8797c478bd9Sstevel@tonic-gate /* 8807c478bd9Sstevel@tonic-gate * If the decl is a pointer, resolve the rest of the stack by calling 8817c478bd9Sstevel@tonic-gate * dt_decl_type() recursively and then compute a pointer to the result. 8827c478bd9Sstevel@tonic-gate * Similar to the code above, we return a cached id for function ptrs. 8837c478bd9Sstevel@tonic-gate */ 8847c478bd9Sstevel@tonic-gate if (ddp->dd_kind == CTF_K_POINTER) { 8857c478bd9Sstevel@tonic-gate if (ddp->dd_next->dd_kind == CTF_K_FUNCTION) { 8867c478bd9Sstevel@tonic-gate tip->dtt_object = dtp->dt_ddefs->dm_name; 8877c478bd9Sstevel@tonic-gate tip->dtt_ctfp = DT_FPTR_CTFP(dtp); 8887c478bd9Sstevel@tonic-gate tip->dtt_type = DT_FPTR_TYPE(dtp); 8897c478bd9Sstevel@tonic-gate return (0); 8907c478bd9Sstevel@tonic-gate } 8917c478bd9Sstevel@tonic-gate 8927c478bd9Sstevel@tonic-gate if ((rv = dt_decl_type(ddp->dd_next, tip)) == 0 && 8937c478bd9Sstevel@tonic-gate (rv = dt_type_pointer(tip)) != 0) { 8947c478bd9Sstevel@tonic-gate xywarn(D_UNKNOWN, "cannot find type: %s*: %s\n", 8957c478bd9Sstevel@tonic-gate dt_type_name(tip->dtt_ctfp, tip->dtt_type, 8967c478bd9Sstevel@tonic-gate n, sizeof (n)), ctf_errmsg(dtp->dt_ctferr)); 8977c478bd9Sstevel@tonic-gate } 8987c478bd9Sstevel@tonic-gate 8997c478bd9Sstevel@tonic-gate return (rv); 9007c478bd9Sstevel@tonic-gate } 9017c478bd9Sstevel@tonic-gate 9027c478bd9Sstevel@tonic-gate /* 9037c478bd9Sstevel@tonic-gate * If the decl is an array, we must find the base type and then call 9047c478bd9Sstevel@tonic-gate * dt_decl_type() recursively and then build an array of the result. 9057c478bd9Sstevel@tonic-gate * The C and D multi-dimensional array syntax requires that consecutive 9067c478bd9Sstevel@tonic-gate * array declarations be processed from right-to-left (i.e. top-down 9077c478bd9Sstevel@tonic-gate * from the perspective of the declaration stack). For example, an 9087c478bd9Sstevel@tonic-gate * array declaration such as int x[3][5] is stored on the stack as: 9097c478bd9Sstevel@tonic-gate * 9107c478bd9Sstevel@tonic-gate * (bottom) NULL <- ( INT "int" ) <- ( ARR [3] ) <- ( ARR [5] ) (top) 9117c478bd9Sstevel@tonic-gate * 9127c478bd9Sstevel@tonic-gate * but means that x is declared to be an array of 3 objects each of 9137c478bd9Sstevel@tonic-gate * which is an array of 5 integers, or in CTF representation: 9147c478bd9Sstevel@tonic-gate * 9157c478bd9Sstevel@tonic-gate * type T1:( content=int, nelems=5 ) type T2:( content=T1, nelems=3 ) 9167c478bd9Sstevel@tonic-gate * 9177c478bd9Sstevel@tonic-gate * For more details, refer to K&R[5.7] and ISO C 6.5.2.1. Rather than 9187c478bd9Sstevel@tonic-gate * overcomplicate the implementation of dt_decl_type(), we push array 9197c478bd9Sstevel@tonic-gate * declarations down into the stack in dt_decl_array(), above, so that 9207c478bd9Sstevel@tonic-gate * by the time dt_decl_type() is called, the decl stack looks like: 9217c478bd9Sstevel@tonic-gate * 9227c478bd9Sstevel@tonic-gate * (bottom) NULL <- ( INT "int" ) <- ( ARR [5] ) <- ( ARR [3] ) (top) 9237c478bd9Sstevel@tonic-gate * 9247c478bd9Sstevel@tonic-gate * which permits a straightforward recursive descent of the decl stack 9257c478bd9Sstevel@tonic-gate * to build the corresponding CTF type tree in the appropriate order. 9267c478bd9Sstevel@tonic-gate */ 9277c478bd9Sstevel@tonic-gate if (ddp->dd_kind == CTF_K_ARRAY) { 9287c478bd9Sstevel@tonic-gate /* 9297c478bd9Sstevel@tonic-gate * If the array decl has a parameter list associated with it, 9307c478bd9Sstevel@tonic-gate * this is an associative array declaration: return <DYN>. 9317c478bd9Sstevel@tonic-gate */ 9327c478bd9Sstevel@tonic-gate if (ddp->dd_node != NULL && 9337c478bd9Sstevel@tonic-gate ddp->dd_node->dn_kind == DT_NODE_TYPE) { 9347c478bd9Sstevel@tonic-gate tip->dtt_object = dtp->dt_ddefs->dm_name; 9357c478bd9Sstevel@tonic-gate tip->dtt_ctfp = DT_DYN_CTFP(dtp); 9367c478bd9Sstevel@tonic-gate tip->dtt_type = DT_DYN_TYPE(dtp); 9377c478bd9Sstevel@tonic-gate return (0); 9387c478bd9Sstevel@tonic-gate } 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate if ((rv = dt_decl_type(ddp->dd_next, tip)) != 0) 9417c478bd9Sstevel@tonic-gate return (rv); 9427c478bd9Sstevel@tonic-gate 9437c478bd9Sstevel@tonic-gate /* 9447c478bd9Sstevel@tonic-gate * If the array base type is not defined in the target 9457c478bd9Sstevel@tonic-gate * container or its parent, copy the type to the target 9467c478bd9Sstevel@tonic-gate * container and reset dtt_ctfp and dtt_type to the copy. 9477c478bd9Sstevel@tonic-gate */ 9487c478bd9Sstevel@tonic-gate if (tip->dtt_ctfp != dmp->dm_ctfp && 9497c478bd9Sstevel@tonic-gate tip->dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) { 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate tip->dtt_type = ctf_add_type(dmp->dm_ctfp, 9527c478bd9Sstevel@tonic-gate tip->dtt_ctfp, tip->dtt_type); 9537c478bd9Sstevel@tonic-gate tip->dtt_ctfp = dmp->dm_ctfp; 9547c478bd9Sstevel@tonic-gate 9557c478bd9Sstevel@tonic-gate if (tip->dtt_type == CTF_ERR || 9567c478bd9Sstevel@tonic-gate ctf_update(tip->dtt_ctfp) == CTF_ERR) { 9577c478bd9Sstevel@tonic-gate xywarn(D_UNKNOWN, "failed to copy type: %s\n", 9587c478bd9Sstevel@tonic-gate ctf_errmsg(ctf_errno(tip->dtt_ctfp))); 9597c478bd9Sstevel@tonic-gate return (-1); 9607c478bd9Sstevel@tonic-gate } 9617c478bd9Sstevel@tonic-gate } 9627c478bd9Sstevel@tonic-gate 9637c478bd9Sstevel@tonic-gate /* 9647c478bd9Sstevel@tonic-gate * The array index type is irrelevant in C and D: just set it 9657c478bd9Sstevel@tonic-gate * to "long" for all array types that we create on-the-fly. 9667c478bd9Sstevel@tonic-gate */ 9677c478bd9Sstevel@tonic-gate r.ctr_contents = tip->dtt_type; 9687c478bd9Sstevel@tonic-gate r.ctr_index = ctf_lookup_by_name(tip->dtt_ctfp, "long"); 9697c478bd9Sstevel@tonic-gate r.ctr_nelems = ddp->dd_node ? 9707c478bd9Sstevel@tonic-gate (uint_t)ddp->dd_node->dn_value : 0; 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate tip->dtt_object = dmp->dm_name; 9737c478bd9Sstevel@tonic-gate tip->dtt_ctfp = dmp->dm_ctfp; 9747c478bd9Sstevel@tonic-gate tip->dtt_type = ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &r); 9757c478bd9Sstevel@tonic-gate 9767c478bd9Sstevel@tonic-gate if (tip->dtt_type == CTF_ERR || 9777c478bd9Sstevel@tonic-gate ctf_update(tip->dtt_ctfp) == CTF_ERR) { 9787c478bd9Sstevel@tonic-gate xywarn(D_UNKNOWN, "failed to create array type: %s\n", 9797c478bd9Sstevel@tonic-gate ctf_errmsg(ctf_errno(tip->dtt_ctfp))); 9807c478bd9Sstevel@tonic-gate return (-1); 9817c478bd9Sstevel@tonic-gate } 9827c478bd9Sstevel@tonic-gate 9837c478bd9Sstevel@tonic-gate return (0); 9847c478bd9Sstevel@tonic-gate } 9857c478bd9Sstevel@tonic-gate 9867c478bd9Sstevel@tonic-gate /* 9877c478bd9Sstevel@tonic-gate * Allocate space for the type name and enough space for the maximum 9887c478bd9Sstevel@tonic-gate * additional text ("unsigned long long \0" requires 20 more bytes). 9897c478bd9Sstevel@tonic-gate */ 9907c478bd9Sstevel@tonic-gate name = alloca(ddp->dd_name ? strlen(ddp->dd_name) + 20 : 20); 9917c478bd9Sstevel@tonic-gate name[0] = '\0'; 9927c478bd9Sstevel@tonic-gate 9937c478bd9Sstevel@tonic-gate switch (ddp->dd_kind) { 9947c478bd9Sstevel@tonic-gate case CTF_K_INTEGER: 9957c478bd9Sstevel@tonic-gate case CTF_K_FLOAT: 9967c478bd9Sstevel@tonic-gate if (ddp->dd_attr & DT_DA_SIGNED) 9977c478bd9Sstevel@tonic-gate (void) strcat(name, "signed "); 9987c478bd9Sstevel@tonic-gate if (ddp->dd_attr & DT_DA_UNSIGNED) 9997c478bd9Sstevel@tonic-gate (void) strcat(name, "unsigned "); 10007c478bd9Sstevel@tonic-gate if (ddp->dd_attr & DT_DA_SHORT) 10017c478bd9Sstevel@tonic-gate (void) strcat(name, "short "); 10027c478bd9Sstevel@tonic-gate if (ddp->dd_attr & DT_DA_LONG) 10037c478bd9Sstevel@tonic-gate (void) strcat(name, "long "); 10047c478bd9Sstevel@tonic-gate if (ddp->dd_attr & DT_DA_LONGLONG) 10057c478bd9Sstevel@tonic-gate (void) strcat(name, "long long "); 10067c478bd9Sstevel@tonic-gate if (ddp->dd_attr == 0 && ddp->dd_name == NULL) 10077c478bd9Sstevel@tonic-gate (void) strcat(name, "int"); 10087c478bd9Sstevel@tonic-gate break; 10097c478bd9Sstevel@tonic-gate case CTF_K_STRUCT: 10107c478bd9Sstevel@tonic-gate (void) strcpy(name, "struct "); 10117c478bd9Sstevel@tonic-gate break; 10127c478bd9Sstevel@tonic-gate case CTF_K_UNION: 10137c478bd9Sstevel@tonic-gate (void) strcpy(name, "union "); 10147c478bd9Sstevel@tonic-gate break; 10157c478bd9Sstevel@tonic-gate case CTF_K_ENUM: 10167c478bd9Sstevel@tonic-gate (void) strcpy(name, "enum "); 10177c478bd9Sstevel@tonic-gate break; 10187c478bd9Sstevel@tonic-gate case CTF_K_TYPEDEF: 10197c478bd9Sstevel@tonic-gate break; 10207c478bd9Sstevel@tonic-gate default: 10217c478bd9Sstevel@tonic-gate xywarn(D_UNKNOWN, "internal error -- " 10227c478bd9Sstevel@tonic-gate "bad decl kind %u\n", ddp->dd_kind); 10237c478bd9Sstevel@tonic-gate return (-1); 10247c478bd9Sstevel@tonic-gate } 10257c478bd9Sstevel@tonic-gate 10267c478bd9Sstevel@tonic-gate /* 10277c478bd9Sstevel@tonic-gate * Add dd_name unless a short, long, or long long is explicitly 10287c478bd9Sstevel@tonic-gate * suffixed by int. We use the C/CTF canonical names for integers. 10297c478bd9Sstevel@tonic-gate */ 10307c478bd9Sstevel@tonic-gate if (ddp->dd_name != NULL && (ddp->dd_kind != CTF_K_INTEGER || 10317c478bd9Sstevel@tonic-gate (ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG)) == 0)) 10327c478bd9Sstevel@tonic-gate (void) strcat(name, ddp->dd_name); 10337c478bd9Sstevel@tonic-gate 10347c478bd9Sstevel@tonic-gate /* 10357c478bd9Sstevel@tonic-gate * Lookup the type. If we find it, we're done. Otherwise create a 10367c478bd9Sstevel@tonic-gate * forward tag for the type if it is a struct, union, or enum. If 10377c478bd9Sstevel@tonic-gate * we can't find it and we can't create a tag, return failure. 10387c478bd9Sstevel@tonic-gate */ 10397c478bd9Sstevel@tonic-gate if ((rv = dt_type_lookup(name, tip)) == 0) 10407c478bd9Sstevel@tonic-gate return (rv); 10417c478bd9Sstevel@tonic-gate 10427c478bd9Sstevel@tonic-gate switch (ddp->dd_kind) { 10437c478bd9Sstevel@tonic-gate case CTF_K_STRUCT: 10447c478bd9Sstevel@tonic-gate case CTF_K_UNION: 10457c478bd9Sstevel@tonic-gate case CTF_K_ENUM: 10467c478bd9Sstevel@tonic-gate type = ctf_add_forward(dmp->dm_ctfp, flag, 10477c478bd9Sstevel@tonic-gate ddp->dd_name, ddp->dd_kind); 10487c478bd9Sstevel@tonic-gate break; 10497c478bd9Sstevel@tonic-gate default: 10507c478bd9Sstevel@tonic-gate xywarn(D_UNKNOWN, "failed to resolve type %s: %s\n", name, 10517c478bd9Sstevel@tonic-gate dtrace_errmsg(dtp, dtrace_errno(dtp))); 10527c478bd9Sstevel@tonic-gate return (rv); 10537c478bd9Sstevel@tonic-gate } 10547c478bd9Sstevel@tonic-gate 10557c478bd9Sstevel@tonic-gate if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) { 10567c478bd9Sstevel@tonic-gate xywarn(D_UNKNOWN, "failed to add forward tag for %s: %s\n", 10577c478bd9Sstevel@tonic-gate name, ctf_errmsg(ctf_errno(dmp->dm_ctfp))); 10587c478bd9Sstevel@tonic-gate return (-1); 10597c478bd9Sstevel@tonic-gate } 10607c478bd9Sstevel@tonic-gate 10617c478bd9Sstevel@tonic-gate ddp->dd_ctfp = dmp->dm_ctfp; 10627c478bd9Sstevel@tonic-gate ddp->dd_type = type; 10637c478bd9Sstevel@tonic-gate 10647c478bd9Sstevel@tonic-gate tip->dtt_object = dmp->dm_name; 10657c478bd9Sstevel@tonic-gate tip->dtt_ctfp = dmp->dm_ctfp; 10667c478bd9Sstevel@tonic-gate tip->dtt_type = type; 10677c478bd9Sstevel@tonic-gate 10687c478bd9Sstevel@tonic-gate return (0); 10697c478bd9Sstevel@tonic-gate } 10707c478bd9Sstevel@tonic-gate 10717c478bd9Sstevel@tonic-gate void 10727c478bd9Sstevel@tonic-gate dt_scope_create(dt_scope_t *dsp) 10737c478bd9Sstevel@tonic-gate { 10747c478bd9Sstevel@tonic-gate dsp->ds_decl = NULL; 10757c478bd9Sstevel@tonic-gate dsp->ds_next = NULL; 10767c478bd9Sstevel@tonic-gate dsp->ds_ident = NULL; 10777c478bd9Sstevel@tonic-gate dsp->ds_ctfp = NULL; 10787c478bd9Sstevel@tonic-gate dsp->ds_type = CTF_ERR; 10797c478bd9Sstevel@tonic-gate dsp->ds_class = DT_DC_DEFAULT; 10807c478bd9Sstevel@tonic-gate dsp->ds_enumval = -1; 10817c478bd9Sstevel@tonic-gate } 10827c478bd9Sstevel@tonic-gate 10837c478bd9Sstevel@tonic-gate void 10847c478bd9Sstevel@tonic-gate dt_scope_destroy(dt_scope_t *dsp) 10857c478bd9Sstevel@tonic-gate { 10867c478bd9Sstevel@tonic-gate dt_scope_t *nsp; 10877c478bd9Sstevel@tonic-gate 10887c478bd9Sstevel@tonic-gate for (; dsp != NULL; dsp = nsp) { 10897c478bd9Sstevel@tonic-gate dt_decl_free(dsp->ds_decl); 10907c478bd9Sstevel@tonic-gate free(dsp->ds_ident); 10917c478bd9Sstevel@tonic-gate nsp = dsp->ds_next; 10927c478bd9Sstevel@tonic-gate if (dsp != &yypcb->pcb_dstack) 10937c478bd9Sstevel@tonic-gate free(dsp); 10947c478bd9Sstevel@tonic-gate } 10957c478bd9Sstevel@tonic-gate } 10967c478bd9Sstevel@tonic-gate 10977c478bd9Sstevel@tonic-gate void 10987c478bd9Sstevel@tonic-gate dt_scope_push(ctf_file_t *ctfp, ctf_id_t type) 10997c478bd9Sstevel@tonic-gate { 11007c478bd9Sstevel@tonic-gate dt_scope_t *rsp = &yypcb->pcb_dstack; 11017c478bd9Sstevel@tonic-gate dt_scope_t *dsp = malloc(sizeof (dt_scope_t)); 11027c478bd9Sstevel@tonic-gate 11037c478bd9Sstevel@tonic-gate if (dsp == NULL) 11047c478bd9Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 11057c478bd9Sstevel@tonic-gate 11067c478bd9Sstevel@tonic-gate dsp->ds_decl = rsp->ds_decl; 11077c478bd9Sstevel@tonic-gate dsp->ds_next = rsp->ds_next; 11087c478bd9Sstevel@tonic-gate dsp->ds_ident = rsp->ds_ident; 11097c478bd9Sstevel@tonic-gate dsp->ds_ctfp = ctfp; 11107c478bd9Sstevel@tonic-gate dsp->ds_type = type; 11117c478bd9Sstevel@tonic-gate dsp->ds_class = rsp->ds_class; 11127c478bd9Sstevel@tonic-gate dsp->ds_enumval = rsp->ds_enumval; 11137c478bd9Sstevel@tonic-gate 11147c478bd9Sstevel@tonic-gate dt_scope_create(rsp); 11157c478bd9Sstevel@tonic-gate rsp->ds_next = dsp; 11167c478bd9Sstevel@tonic-gate } 11177c478bd9Sstevel@tonic-gate 11187c478bd9Sstevel@tonic-gate dt_decl_t * 11197c478bd9Sstevel@tonic-gate dt_scope_pop(void) 11207c478bd9Sstevel@tonic-gate { 11217c478bd9Sstevel@tonic-gate dt_scope_t *rsp = &yypcb->pcb_dstack; 11227c478bd9Sstevel@tonic-gate dt_scope_t *dsp = rsp->ds_next; 11237c478bd9Sstevel@tonic-gate 11247c478bd9Sstevel@tonic-gate if (dsp == NULL) 11257c478bd9Sstevel@tonic-gate longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE); 11267c478bd9Sstevel@tonic-gate 11277c478bd9Sstevel@tonic-gate if (dsp->ds_ctfp != NULL && ctf_update(dsp->ds_ctfp) == CTF_ERR) { 11287c478bd9Sstevel@tonic-gate xyerror(D_UNKNOWN, "failed to update type definitions: %s\n", 11297c478bd9Sstevel@tonic-gate ctf_errmsg(ctf_errno(dsp->ds_ctfp))); 11307c478bd9Sstevel@tonic-gate } 11317c478bd9Sstevel@tonic-gate 11327c478bd9Sstevel@tonic-gate dt_decl_free(rsp->ds_decl); 11337c478bd9Sstevel@tonic-gate free(rsp->ds_ident); 11347c478bd9Sstevel@tonic-gate 11357c478bd9Sstevel@tonic-gate rsp->ds_decl = dsp->ds_decl; 11367c478bd9Sstevel@tonic-gate rsp->ds_next = dsp->ds_next; 11377c478bd9Sstevel@tonic-gate rsp->ds_ident = dsp->ds_ident; 11387c478bd9Sstevel@tonic-gate rsp->ds_ctfp = dsp->ds_ctfp; 11397c478bd9Sstevel@tonic-gate rsp->ds_type = dsp->ds_type; 11407c478bd9Sstevel@tonic-gate rsp->ds_class = dsp->ds_class; 11417c478bd9Sstevel@tonic-gate rsp->ds_enumval = dsp->ds_enumval; 11427c478bd9Sstevel@tonic-gate 11437c478bd9Sstevel@tonic-gate free(dsp); 11447c478bd9Sstevel@tonic-gate return (rsp->ds_decl); 11457c478bd9Sstevel@tonic-gate } 1146