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