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