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