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