| 1 | /* YACC parser for C++ names, for GDB. |
| 2 | |
| 3 | Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009 |
| 4 | Free Software Foundation, Inc. |
| 5 | |
| 6 | Parts of the lexer are based on c-exp.y from GDB. |
| 7 | |
| 8 | This file is part of GDB. |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 3 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 22 | |
| 23 | /* Note that malloc's and realloc's in this file are transformed to |
| 24 | xmalloc and xrealloc respectively by the same sed command in the |
| 25 | makefile that remaps any other malloc/realloc inserted by the parser |
| 26 | generator. Doing this with #defines and trying to control the interaction |
| 27 | with include files (<malloc.h> and <stdlib.h> for example) just became |
| 28 | too messy, particularly when such includes can be inserted at random |
| 29 | times by the parser generator. */ |
| 30 | |
| 31 | %{ |
| 32 | |
| 33 | #include "defs.h" |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <unistd.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include "safe-ctype.h" |
| 41 | #include "libiberty.h" |
| 42 | #include "demangle.h" |
| 43 | #include "cp-support.h" |
| 44 | |
| 45 | /* Bison does not make it easy to create a parser without global |
| 46 | state, unfortunately. Here are all the global variables used |
| 47 | in this parser. */ |
| 48 | |
| 49 | /* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR |
| 50 | is the start of the last token lexed, only used for diagnostics. |
| 51 | ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG |
| 52 | is the first error message encountered. */ |
| 53 | |
| 54 | static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg; |
| 55 | |
| 56 | /* The components built by the parser are allocated ahead of time, |
| 57 | and cached in this structure. */ |
| 58 | |
| 59 | #define ALLOC_CHUNK 100 |
| 60 | |
| 61 | struct demangle_info { |
| 62 | int used; |
| 63 | struct demangle_info *prev, *next; |
| 64 | struct demangle_component comps[ALLOC_CHUNK]; |
| 65 | }; |
| 66 | |
| 67 | static struct demangle_info *demangle_info; |
| 68 | |
| 69 | static struct demangle_component * |
| 70 | d_grab (void) |
| 71 | { |
| 72 | struct demangle_info *more; |
| 73 | |
| 74 | if (demangle_info->used >= ALLOC_CHUNK) |
| 75 | { |
| 76 | if (demangle_info->next == NULL) |
| 77 | { |
| 78 | more = malloc (sizeof (struct demangle_info)); |
| 79 | more->prev = demangle_info; |
| 80 | more->next = NULL; |
| 81 | demangle_info->next = more; |
| 82 | } |
| 83 | else |
| 84 | more = demangle_info->next; |
| 85 | |
| 86 | more->used = 0; |
| 87 | demangle_info = more; |
| 88 | } |
| 89 | return &demangle_info->comps[demangle_info->used++]; |
| 90 | } |
| 91 | |
| 92 | /* The parse tree created by the parser is stored here after a successful |
| 93 | parse. */ |
| 94 | |
| 95 | static struct demangle_component *global_result; |
| 96 | |
| 97 | /* Prototypes for helper functions used when constructing the parse |
| 98 | tree. */ |
| 99 | |
| 100 | static struct demangle_component *d_qualify (struct demangle_component *, int, |
| 101 | int); |
| 102 | |
| 103 | static struct demangle_component *d_int_type (int); |
| 104 | |
| 105 | static struct demangle_component *d_unary (const char *, |
| 106 | struct demangle_component *); |
| 107 | static struct demangle_component *d_binary (const char *, |
| 108 | struct demangle_component *, |
| 109 | struct demangle_component *); |
| 110 | |
| 111 | /* Flags passed to d_qualify. */ |
| 112 | |
| 113 | #define QUAL_CONST 1 |
| 114 | #define QUAL_RESTRICT 2 |
| 115 | #define QUAL_VOLATILE 4 |
| 116 | |
| 117 | /* Flags passed to d_int_type. */ |
| 118 | |
| 119 | #define INT_CHAR (1 << 0) |
| 120 | #define INT_SHORT (1 << 1) |
| 121 | #define INT_LONG (1 << 2) |
| 122 | #define INT_LLONG (1 << 3) |
| 123 | |
| 124 | #define INT_SIGNED (1 << 4) |
| 125 | #define INT_UNSIGNED (1 << 5) |
| 126 | |
| 127 | /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc), |
| 128 | as well as gratuitiously global symbol names, so we can have multiple |
| 129 | yacc generated parsers in gdb. Note that these are only the variables |
| 130 | produced by yacc. If other parser generators (bison, byacc, etc) produce |
| 131 | additional global names that conflict at link time, then those parser |
| 132 | generators need to be fixed instead of adding those names to this list. */ |
| 133 | |
| 134 | #define yymaxdepth cpname_maxdepth |
| 135 | #define yyparse cpname_parse |
| 136 | #define yylex cpname_lex |
| 137 | #define yyerror cpname_error |
| 138 | #define yylval cpname_lval |
| 139 | #define yychar cpname_char |
| 140 | #define yydebug cpname_debug |
| 141 | #define yypact cpname_pact |
| 142 | #define yyr1 cpname_r1 |
| 143 | #define yyr2 cpname_r2 |
| 144 | #define yydef cpname_def |
| 145 | #define yychk cpname_chk |
| 146 | #define yypgo cpname_pgo |
| 147 | #define yyact cpname_act |
| 148 | #define yyexca cpname_exca |
| 149 | #define yyerrflag cpname_errflag |
| 150 | #define yynerrs cpname_nerrs |
| 151 | #define yyps cpname_ps |
| 152 | #define yypv cpname_pv |
| 153 | #define yys cpname_s |
| 154 | #define yy_yys cpname_yys |
| 155 | #define yystate cpname_state |
| 156 | #define yytmp cpname_tmp |
| 157 | #define yyv cpname_v |
| 158 | #define yy_yyv cpname_yyv |
| 159 | #define yyval cpname_val |
| 160 | #define yylloc cpname_lloc |
| 161 | #define yyreds cpname_reds /* With YYDEBUG defined */ |
| 162 | #define yytoks cpname_toks /* With YYDEBUG defined */ |
| 163 | #define yyname cpname_name /* With YYDEBUG defined */ |
| 164 | #define yyrule cpname_rule /* With YYDEBUG defined */ |
| 165 | #define yylhs cpname_yylhs |
| 166 | #define yylen cpname_yylen |
| 167 | #define yydefred cpname_yydefred |
| 168 | #define yydgoto cpname_yydgoto |
| 169 | #define yysindex cpname_yysindex |
| 170 | #define yyrindex cpname_yyrindex |
| 171 | #define yygindex cpname_yygindex |
| 172 | #define yytable cpname_yytable |
| 173 | #define yycheck cpname_yycheck |
| 174 | |
| 175 | int yyparse (void); |
| 176 | static int yylex (void); |
| 177 | static void yyerror (char *); |
| 178 | |
| 179 | /* Enable yydebug for the stand-alone parser. */ |
| 180 | #ifdef TEST_CPNAMES |
| 181 | # define YYDEBUG 1 |
| 182 | #endif |
| 183 | |
| 184 | /* Helper functions. These wrap the demangler tree interface, handle |
| 185 | allocation from our global store, and return the allocated component. */ |
| 186 | |
| 187 | static struct demangle_component * |
| 188 | fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs, |
| 189 | struct demangle_component *rhs) |
| 190 | { |
| 191 | struct demangle_component *ret = d_grab (); |
| 192 | cplus_demangle_fill_component (ret, d_type, lhs, rhs); |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static struct demangle_component * |
| 197 | make_empty (enum demangle_component_type d_type) |
| 198 | { |
| 199 | struct demangle_component *ret = d_grab (); |
| 200 | ret->type = d_type; |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | static struct demangle_component * |
| 205 | make_operator (const char *name, int args) |
| 206 | { |
| 207 | struct demangle_component *ret = d_grab (); |
| 208 | cplus_demangle_fill_operator (ret, name, args); |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | static struct demangle_component * |
| 213 | make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name) |
| 214 | { |
| 215 | struct demangle_component *ret = d_grab (); |
| 216 | cplus_demangle_fill_dtor (ret, kind, name); |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static struct demangle_component * |
| 221 | make_builtin_type (const char *name) |
| 222 | { |
| 223 | struct demangle_component *ret = d_grab (); |
| 224 | cplus_demangle_fill_builtin_type (ret, name); |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | static struct demangle_component * |
| 229 | make_name (const char *name, int len) |
| 230 | { |
| 231 | struct demangle_component *ret = d_grab (); |
| 232 | cplus_demangle_fill_name (ret, name, len); |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | #define d_left(dc) (dc)->u.s_binary.left |
| 237 | #define d_right(dc) (dc)->u.s_binary.right |
| 238 | |
| 239 | %} |
| 240 | |
| 241 | %union |
| 242 | { |
| 243 | struct demangle_component *comp; |
| 244 | struct nested { |
| 245 | struct demangle_component *comp; |
| 246 | struct demangle_component **last; |
| 247 | } nested; |
| 248 | struct { |
| 249 | struct demangle_component *comp, *last; |
| 250 | } nested1; |
| 251 | struct { |
| 252 | struct demangle_component *comp, **last; |
| 253 | struct nested fn; |
| 254 | struct demangle_component *start; |
| 255 | int fold_flag; |
| 256 | } abstract; |
| 257 | int lval; |
| 258 | struct { |
| 259 | int val; |
| 260 | struct demangle_component *type; |
| 261 | } typed_val_int; |
| 262 | const char *opname; |
| 263 | } |
| 264 | |
| 265 | %type <comp> exp exp1 type start start_opt operator colon_name |
| 266 | %type <comp> unqualified_name colon_ext_name |
| 267 | %type <comp> template template_arg |
| 268 | %type <comp> builtin_type |
| 269 | %type <comp> typespec_2 array_indicator |
| 270 | %type <comp> colon_ext_only ext_only_name |
| 271 | |
| 272 | %type <comp> demangler_special function conversion_op |
| 273 | %type <nested> conversion_op_name |
| 274 | |
| 275 | %type <abstract> abstract_declarator direct_abstract_declarator |
| 276 | %type <abstract> abstract_declarator_fn |
| 277 | %type <nested> declarator direct_declarator function_arglist |
| 278 | |
| 279 | %type <nested> declarator_1 direct_declarator_1 |
| 280 | |
| 281 | %type <nested> template_params function_args |
| 282 | %type <nested> ptr_operator |
| 283 | |
| 284 | %type <nested1> nested_name |
| 285 | |
| 286 | %type <lval> qualifier qualifiers qualifiers_opt |
| 287 | |
| 288 | %type <lval> int_part int_seq |
| 289 | |
| 290 | %token <comp> INT |
| 291 | %token <comp> FLOAT |
| 292 | |
| 293 | %token <comp> NAME |
| 294 | %type <comp> name |
| 295 | |
| 296 | %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON |
| 297 | %token TEMPLATE |
| 298 | %token ERROR |
| 299 | %token NEW DELETE OPERATOR |
| 300 | %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST |
| 301 | |
| 302 | /* Special type cases, put in to allow the parser to distinguish different |
| 303 | legal basetypes. */ |
| 304 | %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL |
| 305 | %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T |
| 306 | |
| 307 | %token <opname> ASSIGN_MODIFY |
| 308 | |
| 309 | /* C++ */ |
| 310 | %token TRUEKEYWORD |
| 311 | %token FALSEKEYWORD |
| 312 | |
| 313 | /* Non-C++ things we get from the demangler. */ |
| 314 | %token <lval> DEMANGLER_SPECIAL |
| 315 | %token CONSTRUCTION_VTABLE CONSTRUCTION_IN |
| 316 | %token <typed_val_int> GLOBAL |
| 317 | |
| 318 | %{ |
| 319 | enum { |
| 320 | GLOBAL_CONSTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 20, |
| 321 | GLOBAL_DESTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 21 |
| 322 | }; |
| 323 | %} |
| 324 | |
| 325 | /* Precedence declarations. */ |
| 326 | |
| 327 | /* Give NAME lower precedence than COLONCOLON, so that nested_name will |
| 328 | associate greedily. */ |
| 329 | %nonassoc NAME |
| 330 | |
| 331 | /* Give NEW and DELETE lower precedence than ']', because we can not |
| 332 | have an array of type operator new. This causes NEW '[' to be |
| 333 | parsed as operator new[]. */ |
| 334 | %nonassoc NEW DELETE |
| 335 | |
| 336 | /* Give VOID higher precedence than NAME. Then we can use %prec NAME |
| 337 | to prefer (VOID) to (function_args). */ |
| 338 | %nonassoc VOID |
| 339 | |
| 340 | /* Give VOID lower precedence than ')' for similar reasons. */ |
| 341 | %nonassoc ')' |
| 342 | |
| 343 | %left ',' |
| 344 | %right '=' ASSIGN_MODIFY |
| 345 | %right '?' |
| 346 | %left OROR |
| 347 | %left ANDAND |
| 348 | %left '|' |
| 349 | %left '^' |
| 350 | %left '&' |
| 351 | %left EQUAL NOTEQUAL |
| 352 | %left '<' '>' LEQ GEQ |
| 353 | %left LSH RSH |
| 354 | %left '@' |
| 355 | %left '+' '-' |
| 356 | %left '*' '/' '%' |
| 357 | %right UNARY INCREMENT DECREMENT |
| 358 | |
| 359 | /* We don't need a precedence for '(' in this reduced grammar, and it |
| 360 | can mask some unpleasant bugs, so disable it for now. */ |
| 361 | |
| 362 | %right ARROW '.' '[' /* '(' */ |
| 363 | %left COLONCOLON |
| 364 | |
| 365 | \f |
| 366 | %% |
| 367 | |
| 368 | result : start |
| 369 | { global_result = $1; } |
| 370 | ; |
| 371 | |
| 372 | start : type |
| 373 | |
| 374 | | demangler_special |
| 375 | |
| 376 | | function |
| 377 | |
| 378 | ; |
| 379 | |
| 380 | start_opt : /* */ |
| 381 | { $$ = NULL; } |
| 382 | | COLONCOLON start |
| 383 | { $$ = $2; } |
| 384 | ; |
| 385 | |
| 386 | function |
| 387 | /* Function with a return type. declarator_1 is used to prevent |
| 388 | ambiguity with the next rule. */ |
| 389 | : typespec_2 declarator_1 |
| 390 | { $$ = $2.comp; |
| 391 | *$2.last = $1; |
| 392 | } |
| 393 | |
| 394 | /* Function without a return type. We need to use typespec_2 |
| 395 | to prevent conflicts from qualifiers_opt - harmless. The |
| 396 | start_opt is used to handle "function-local" variables and |
| 397 | types. */ |
| 398 | | typespec_2 function_arglist start_opt |
| 399 | { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp); |
| 400 | if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); } |
| 401 | | colon_ext_only function_arglist start_opt |
| 402 | { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp); |
| 403 | if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); } |
| 404 | |
| 405 | | conversion_op_name start_opt |
| 406 | { $$ = $1.comp; |
| 407 | if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); } |
| 408 | | conversion_op_name abstract_declarator_fn |
| 409 | { if ($2.last) |
| 410 | { |
| 411 | /* First complete the abstract_declarator's type using |
| 412 | the typespec from the conversion_op_name. */ |
| 413 | *$2.last = *$1.last; |
| 414 | /* Then complete the conversion_op_name with the type. */ |
| 415 | *$1.last = $2.comp; |
| 416 | } |
| 417 | /* If we have an arglist, build a function type. */ |
| 418 | if ($2.fn.comp) |
| 419 | $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp); |
| 420 | else |
| 421 | $$ = $1.comp; |
| 422 | if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start); |
| 423 | } |
| 424 | ; |
| 425 | |
| 426 | demangler_special |
| 427 | : DEMANGLER_SPECIAL start |
| 428 | { $$ = make_empty ($1); |
| 429 | d_left ($$) = $2; |
| 430 | d_right ($$) = NULL; } |
| 431 | | CONSTRUCTION_VTABLE start CONSTRUCTION_IN start |
| 432 | { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); } |
| 433 | | GLOBAL |
| 434 | { $$ = make_empty ($1.val); |
| 435 | d_left ($$) = $1.type; |
| 436 | d_right ($$) = NULL; } |
| 437 | ; |
| 438 | |
| 439 | operator : OPERATOR NEW |
| 440 | { $$ = make_operator ("new", 1); } |
| 441 | | OPERATOR DELETE |
| 442 | { $$ = make_operator ("delete", 1); } |
| 443 | | OPERATOR NEW '[' ']' |
| 444 | { $$ = make_operator ("new[]", 1); } |
| 445 | | OPERATOR DELETE '[' ']' |
| 446 | { $$ = make_operator ("delete[]", 1); } |
| 447 | | OPERATOR '+' |
| 448 | { $$ = make_operator ("+", 2); } |
| 449 | | OPERATOR '-' |
| 450 | { $$ = make_operator ("-", 2); } |
| 451 | | OPERATOR '*' |
| 452 | { $$ = make_operator ("*", 2); } |
| 453 | | OPERATOR '/' |
| 454 | { $$ = make_operator ("/", 2); } |
| 455 | | OPERATOR '%' |
| 456 | { $$ = make_operator ("%", 2); } |
| 457 | | OPERATOR '^' |
| 458 | { $$ = make_operator ("^", 2); } |
| 459 | | OPERATOR '&' |
| 460 | { $$ = make_operator ("&", 2); } |
| 461 | | OPERATOR '|' |
| 462 | { $$ = make_operator ("|", 2); } |
| 463 | | OPERATOR '~' |
| 464 | { $$ = make_operator ("~", 1); } |
| 465 | | OPERATOR '!' |
| 466 | { $$ = make_operator ("!", 1); } |
| 467 | | OPERATOR '=' |
| 468 | { $$ = make_operator ("=", 2); } |
| 469 | | OPERATOR '<' |
| 470 | { $$ = make_operator ("<", 2); } |
| 471 | | OPERATOR '>' |
| 472 | { $$ = make_operator (">", 2); } |
| 473 | | OPERATOR ASSIGN_MODIFY |
| 474 | { $$ = make_operator ($2, 2); } |
| 475 | | OPERATOR LSH |
| 476 | { $$ = make_operator ("<<", 2); } |
| 477 | | OPERATOR RSH |
| 478 | { $$ = make_operator (">>", 2); } |
| 479 | | OPERATOR EQUAL |
| 480 | { $$ = make_operator ("==", 2); } |
| 481 | | OPERATOR NOTEQUAL |
| 482 | { $$ = make_operator ("!=", 2); } |
| 483 | | OPERATOR LEQ |
| 484 | { $$ = make_operator ("<=", 2); } |
| 485 | | OPERATOR GEQ |
| 486 | { $$ = make_operator (">=", 2); } |
| 487 | | OPERATOR ANDAND |
| 488 | { $$ = make_operator ("&&", 2); } |
| 489 | | OPERATOR OROR |
| 490 | { $$ = make_operator ("||", 2); } |
| 491 | | OPERATOR INCREMENT |
| 492 | { $$ = make_operator ("++", 1); } |
| 493 | | OPERATOR DECREMENT |
| 494 | { $$ = make_operator ("--", 1); } |
| 495 | | OPERATOR ',' |
| 496 | { $$ = make_operator (",", 2); } |
| 497 | | OPERATOR ARROW '*' |
| 498 | { $$ = make_operator ("->*", 2); } |
| 499 | | OPERATOR ARROW |
| 500 | { $$ = make_operator ("->", 2); } |
| 501 | | OPERATOR '(' ')' |
| 502 | { $$ = make_operator ("()", 2); } |
| 503 | | OPERATOR '[' ']' |
| 504 | { $$ = make_operator ("[]", 2); } |
| 505 | ; |
| 506 | |
| 507 | /* Conversion operators. We don't try to handle some of |
| 508 | the wackier demangler output for function pointers, |
| 509 | since it's not clear that it's parseable. */ |
| 510 | conversion_op |
| 511 | : OPERATOR typespec_2 |
| 512 | { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); } |
| 513 | ; |
| 514 | |
| 515 | conversion_op_name |
| 516 | : nested_name conversion_op |
| 517 | { $$.comp = $1.comp; |
| 518 | d_right ($1.last) = $2; |
| 519 | $$.last = &d_left ($2); |
| 520 | } |
| 521 | | conversion_op |
| 522 | { $$.comp = $1; |
| 523 | $$.last = &d_left ($1); |
| 524 | } |
| 525 | | COLONCOLON nested_name conversion_op |
| 526 | { $$.comp = $2.comp; |
| 527 | d_right ($2.last) = $3; |
| 528 | $$.last = &d_left ($3); |
| 529 | } |
| 530 | | COLONCOLON conversion_op |
| 531 | { $$.comp = $2; |
| 532 | $$.last = &d_left ($2); |
| 533 | } |
| 534 | ; |
| 535 | |
| 536 | /* DEMANGLE_COMPONENT_NAME */ |
| 537 | /* This accepts certain invalid placements of '~'. */ |
| 538 | unqualified_name: operator |
| 539 | | operator '<' template_params '>' |
| 540 | { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); } |
| 541 | | '~' NAME |
| 542 | { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); } |
| 543 | ; |
| 544 | |
| 545 | /* This rule is used in name and nested_name, and expanded inline there |
| 546 | for efficiency. */ |
| 547 | /* |
| 548 | scope_id : NAME |
| 549 | | template |
| 550 | ; |
| 551 | */ |
| 552 | |
| 553 | colon_name : name |
| 554 | | COLONCOLON name |
| 555 | { $$ = $2; } |
| 556 | ; |
| 557 | |
| 558 | /* DEMANGLE_COMPONENT_QUAL_NAME */ |
| 559 | /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */ |
| 560 | name : nested_name NAME %prec NAME |
| 561 | { $$ = $1.comp; d_right ($1.last) = $2; } |
| 562 | | NAME %prec NAME |
| 563 | | nested_name template %prec NAME |
| 564 | { $$ = $1.comp; d_right ($1.last) = $2; } |
| 565 | | template %prec NAME |
| 566 | ; |
| 567 | |
| 568 | colon_ext_name : colon_name |
| 569 | | colon_ext_only |
| 570 | ; |
| 571 | |
| 572 | colon_ext_only : ext_only_name |
| 573 | | COLONCOLON ext_only_name |
| 574 | { $$ = $2; } |
| 575 | ; |
| 576 | |
| 577 | ext_only_name : nested_name unqualified_name |
| 578 | { $$ = $1.comp; d_right ($1.last) = $2; } |
| 579 | | unqualified_name |
| 580 | ; |
| 581 | |
| 582 | nested_name : NAME COLONCOLON |
| 583 | { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME); |
| 584 | d_left ($$.comp) = $1; |
| 585 | d_right ($$.comp) = NULL; |
| 586 | $$.last = $$.comp; |
| 587 | } |
| 588 | | nested_name NAME COLONCOLON |
| 589 | { $$.comp = $1.comp; |
| 590 | d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME); |
| 591 | $$.last = d_right ($1.last); |
| 592 | d_left ($$.last) = $2; |
| 593 | d_right ($$.last) = NULL; |
| 594 | } |
| 595 | | template COLONCOLON |
| 596 | { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME); |
| 597 | d_left ($$.comp) = $1; |
| 598 | d_right ($$.comp) = NULL; |
| 599 | $$.last = $$.comp; |
| 600 | } |
| 601 | | nested_name template COLONCOLON |
| 602 | { $$.comp = $1.comp; |
| 603 | d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME); |
| 604 | $$.last = d_right ($1.last); |
| 605 | d_left ($$.last) = $2; |
| 606 | d_right ($$.last) = NULL; |
| 607 | } |
| 608 | ; |
| 609 | |
| 610 | /* DEMANGLE_COMPONENT_TEMPLATE */ |
| 611 | /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */ |
| 612 | template : NAME '<' template_params '>' |
| 613 | { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); } |
| 614 | ; |
| 615 | |
| 616 | template_params : template_arg |
| 617 | { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL); |
| 618 | $$.last = &d_right ($$.comp); } |
| 619 | | template_params ',' template_arg |
| 620 | { $$.comp = $1.comp; |
| 621 | *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL); |
| 622 | $$.last = &d_right (*$1.last); |
| 623 | } |
| 624 | ; |
| 625 | |
| 626 | /* "type" is inlined into template_arg and function_args. */ |
| 627 | |
| 628 | /* Also an integral constant-expression of integral type, and a |
| 629 | pointer to member (?) */ |
| 630 | template_arg : typespec_2 |
| 631 | | typespec_2 abstract_declarator |
| 632 | { $$ = $2.comp; |
| 633 | *$2.last = $1; |
| 634 | } |
| 635 | | '&' start |
| 636 | { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); } |
| 637 | | '&' '(' start ')' |
| 638 | { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); } |
| 639 | | exp |
| 640 | ; |
| 641 | |
| 642 | function_args : typespec_2 |
| 643 | { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL); |
| 644 | $$.last = &d_right ($$.comp); |
| 645 | } |
| 646 | | typespec_2 abstract_declarator |
| 647 | { *$2.last = $1; |
| 648 | $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL); |
| 649 | $$.last = &d_right ($$.comp); |
| 650 | } |
| 651 | | function_args ',' typespec_2 |
| 652 | { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL); |
| 653 | $$.comp = $1.comp; |
| 654 | $$.last = &d_right (*$1.last); |
| 655 | } |
| 656 | | function_args ',' typespec_2 abstract_declarator |
| 657 | { *$4.last = $3; |
| 658 | *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL); |
| 659 | $$.comp = $1.comp; |
| 660 | $$.last = &d_right (*$1.last); |
| 661 | } |
| 662 | | function_args ',' ELLIPSIS |
| 663 | { *$1.last |
| 664 | = fill_comp (DEMANGLE_COMPONENT_ARGLIST, |
| 665 | make_builtin_type ("..."), |
| 666 | NULL); |
| 667 | $$.comp = $1.comp; |
| 668 | $$.last = &d_right (*$1.last); |
| 669 | } |
| 670 | ; |
| 671 | |
| 672 | function_arglist: '(' function_args ')' qualifiers_opt %prec NAME |
| 673 | { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp); |
| 674 | $$.last = &d_left ($$.comp); |
| 675 | $$.comp = d_qualify ($$.comp, $4, 1); } |
| 676 | | '(' VOID ')' qualifiers_opt |
| 677 | { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL); |
| 678 | $$.last = &d_left ($$.comp); |
| 679 | $$.comp = d_qualify ($$.comp, $4, 1); } |
| 680 | | '(' ')' qualifiers_opt |
| 681 | { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL); |
| 682 | $$.last = &d_left ($$.comp); |
| 683 | $$.comp = d_qualify ($$.comp, $3, 1); } |
| 684 | ; |
| 685 | |
| 686 | /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */ |
| 687 | qualifiers_opt : /* epsilon */ |
| 688 | { $$ = 0; } |
| 689 | | qualifiers |
| 690 | ; |
| 691 | |
| 692 | qualifier : RESTRICT |
| 693 | { $$ = QUAL_RESTRICT; } |
| 694 | | VOLATILE_KEYWORD |
| 695 | { $$ = QUAL_VOLATILE; } |
| 696 | | CONST_KEYWORD |
| 697 | { $$ = QUAL_CONST; } |
| 698 | ; |
| 699 | |
| 700 | qualifiers : qualifier |
| 701 | | qualifier qualifiers |
| 702 | { $$ = $1 | $2; } |
| 703 | ; |
| 704 | |
| 705 | /* This accepts all sorts of invalid constructions and produces |
| 706 | invalid output for them - an error would be better. */ |
| 707 | |
| 708 | int_part : INT_KEYWORD |
| 709 | { $$ = 0; } |
| 710 | | SIGNED_KEYWORD |
| 711 | { $$ = INT_SIGNED; } |
| 712 | | UNSIGNED |
| 713 | { $$ = INT_UNSIGNED; } |
| 714 | | CHAR |
| 715 | { $$ = INT_CHAR; } |
| 716 | | LONG |
| 717 | { $$ = INT_LONG; } |
| 718 | | SHORT |
| 719 | { $$ = INT_SHORT; } |
| 720 | ; |
| 721 | |
| 722 | int_seq : int_part |
| 723 | | int_seq int_part |
| 724 | { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; } |
| 725 | ; |
| 726 | |
| 727 | builtin_type : int_seq |
| 728 | { $$ = d_int_type ($1); } |
| 729 | | FLOAT_KEYWORD |
| 730 | { $$ = make_builtin_type ("float"); } |
| 731 | | DOUBLE_KEYWORD |
| 732 | { $$ = make_builtin_type ("double"); } |
| 733 | | LONG DOUBLE_KEYWORD |
| 734 | { $$ = make_builtin_type ("long double"); } |
| 735 | | BOOL |
| 736 | { $$ = make_builtin_type ("bool"); } |
| 737 | | WCHAR_T |
| 738 | { $$ = make_builtin_type ("wchar_t"); } |
| 739 | | VOID |
| 740 | { $$ = make_builtin_type ("void"); } |
| 741 | ; |
| 742 | |
| 743 | ptr_operator : '*' qualifiers_opt |
| 744 | { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER); |
| 745 | $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL; |
| 746 | $$.last = &d_left ($$.comp); |
| 747 | $$.comp = d_qualify ($$.comp, $2, 0); } |
| 748 | /* g++ seems to allow qualifiers after the reference? */ |
| 749 | | '&' |
| 750 | { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE); |
| 751 | $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL; |
| 752 | $$.last = &d_left ($$.comp); } |
| 753 | | nested_name '*' qualifiers_opt |
| 754 | { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE); |
| 755 | $$.comp->u.s_binary.left = $1.comp; |
| 756 | /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */ |
| 757 | *$1.last = *d_left ($1.last); |
| 758 | $$.comp->u.s_binary.right = NULL; |
| 759 | $$.last = &d_right ($$.comp); |
| 760 | $$.comp = d_qualify ($$.comp, $3, 0); } |
| 761 | | COLONCOLON nested_name '*' qualifiers_opt |
| 762 | { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE); |
| 763 | $$.comp->u.s_binary.left = $2.comp; |
| 764 | /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */ |
| 765 | *$2.last = *d_left ($2.last); |
| 766 | $$.comp->u.s_binary.right = NULL; |
| 767 | $$.last = &d_right ($$.comp); |
| 768 | $$.comp = d_qualify ($$.comp, $4, 0); } |
| 769 | ; |
| 770 | |
| 771 | array_indicator : '[' ']' |
| 772 | { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE); |
| 773 | d_left ($$) = NULL; |
| 774 | } |
| 775 | | '[' INT ']' |
| 776 | { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE); |
| 777 | d_left ($$) = $2; |
| 778 | } |
| 779 | ; |
| 780 | |
| 781 | /* Details of this approach inspired by the G++ < 3.4 parser. */ |
| 782 | |
| 783 | /* This rule is only used in typespec_2, and expanded inline there for |
| 784 | efficiency. */ |
| 785 | /* |
| 786 | typespec : builtin_type |
| 787 | | colon_name |
| 788 | ; |
| 789 | */ |
| 790 | |
| 791 | typespec_2 : builtin_type qualifiers |
| 792 | { $$ = d_qualify ($1, $2, 0); } |
| 793 | | builtin_type |
| 794 | | qualifiers builtin_type qualifiers |
| 795 | { $$ = d_qualify ($2, $1 | $3, 0); } |
| 796 | | qualifiers builtin_type |
| 797 | { $$ = d_qualify ($2, $1, 0); } |
| 798 | |
| 799 | | name qualifiers |
| 800 | { $$ = d_qualify ($1, $2, 0); } |
| 801 | | name |
| 802 | | qualifiers name qualifiers |
| 803 | { $$ = d_qualify ($2, $1 | $3, 0); } |
| 804 | | qualifiers name |
| 805 | { $$ = d_qualify ($2, $1, 0); } |
| 806 | |
| 807 | | COLONCOLON name qualifiers |
| 808 | { $$ = d_qualify ($2, $3, 0); } |
| 809 | | COLONCOLON name |
| 810 | { $$ = $2; } |
| 811 | | qualifiers COLONCOLON name qualifiers |
| 812 | { $$ = d_qualify ($3, $1 | $4, 0); } |
| 813 | | qualifiers COLONCOLON name |
| 814 | { $$ = d_qualify ($3, $1, 0); } |
| 815 | ; |
| 816 | |
| 817 | abstract_declarator |
| 818 | : ptr_operator |
| 819 | { $$.comp = $1.comp; $$.last = $1.last; |
| 820 | $$.fn.comp = NULL; $$.fn.last = NULL; } |
| 821 | | ptr_operator abstract_declarator |
| 822 | { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; |
| 823 | if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; } |
| 824 | *$$.last = $1.comp; |
| 825 | $$.last = $1.last; } |
| 826 | | direct_abstract_declarator |
| 827 | { $$.fn.comp = NULL; $$.fn.last = NULL; |
| 828 | if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; } |
| 829 | } |
| 830 | ; |
| 831 | |
| 832 | direct_abstract_declarator |
| 833 | : '(' abstract_declarator ')' |
| 834 | { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1; |
| 835 | if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; } |
| 836 | } |
| 837 | | direct_abstract_declarator function_arglist |
| 838 | { $$.fold_flag = 0; |
| 839 | if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; } |
| 840 | if ($1.fold_flag) |
| 841 | { |
| 842 | *$$.last = $2.comp; |
| 843 | $$.last = $2.last; |
| 844 | } |
| 845 | else |
| 846 | $$.fn = $2; |
| 847 | } |
| 848 | | direct_abstract_declarator array_indicator |
| 849 | { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0; |
| 850 | if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; } |
| 851 | *$1.last = $2; |
| 852 | $$.last = &d_right ($2); |
| 853 | } |
| 854 | | array_indicator |
| 855 | { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0; |
| 856 | $$.comp = $1; |
| 857 | $$.last = &d_right ($1); |
| 858 | } |
| 859 | /* G++ has the following except for () and (type). Then |
| 860 | (type) is handled in regcast_or_absdcl and () is handled |
| 861 | in fcast_or_absdcl. |
| 862 | |
| 863 | However, this is only useful for function types, and |
| 864 | generates reduce/reduce conflicts with direct_declarator. |
| 865 | We're interested in pointer-to-function types, and in |
| 866 | functions, but not in function types - so leave this |
| 867 | out. */ |
| 868 | /* | function_arglist */ |
| 869 | ; |
| 870 | |
| 871 | abstract_declarator_fn |
| 872 | : ptr_operator |
| 873 | { $$.comp = $1.comp; $$.last = $1.last; |
| 874 | $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; } |
| 875 | | ptr_operator abstract_declarator_fn |
| 876 | { $$ = $2; |
| 877 | if ($2.last) |
| 878 | *$$.last = $1.comp; |
| 879 | else |
| 880 | $$.comp = $1.comp; |
| 881 | $$.last = $1.last; |
| 882 | } |
| 883 | | direct_abstract_declarator |
| 884 | { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; } |
| 885 | | direct_abstract_declarator function_arglist COLONCOLON start |
| 886 | { $$.start = $4; |
| 887 | if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; } |
| 888 | if ($1.fold_flag) |
| 889 | { |
| 890 | *$$.last = $2.comp; |
| 891 | $$.last = $2.last; |
| 892 | } |
| 893 | else |
| 894 | $$.fn = $2; |
| 895 | } |
| 896 | | function_arglist start_opt |
| 897 | { $$.fn = $1; |
| 898 | $$.start = $2; |
| 899 | $$.comp = NULL; $$.last = NULL; |
| 900 | } |
| 901 | ; |
| 902 | |
| 903 | type : typespec_2 |
| 904 | | typespec_2 abstract_declarator |
| 905 | { $$ = $2.comp; |
| 906 | *$2.last = $1; |
| 907 | } |
| 908 | ; |
| 909 | |
| 910 | declarator : ptr_operator declarator |
| 911 | { $$.comp = $2.comp; |
| 912 | $$.last = $1.last; |
| 913 | *$2.last = $1.comp; } |
| 914 | | direct_declarator |
| 915 | ; |
| 916 | |
| 917 | direct_declarator |
| 918 | : '(' declarator ')' |
| 919 | { $$ = $2; } |
| 920 | | direct_declarator function_arglist |
| 921 | { $$.comp = $1.comp; |
| 922 | *$1.last = $2.comp; |
| 923 | $$.last = $2.last; |
| 924 | } |
| 925 | | direct_declarator array_indicator |
| 926 | { $$.comp = $1.comp; |
| 927 | *$1.last = $2; |
| 928 | $$.last = &d_right ($2); |
| 929 | } |
| 930 | | colon_ext_name |
| 931 | { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME); |
| 932 | d_left ($$.comp) = $1; |
| 933 | $$.last = &d_right ($$.comp); |
| 934 | } |
| 935 | ; |
| 936 | |
| 937 | /* These are similar to declarator and direct_declarator except that they |
| 938 | do not permit ( colon_ext_name ), which is ambiguous with a function |
| 939 | argument list. They also don't permit a few other forms with redundant |
| 940 | parentheses around the colon_ext_name; any colon_ext_name in parentheses |
| 941 | must be followed by an argument list or an array indicator, or preceded |
| 942 | by a pointer. */ |
| 943 | declarator_1 : ptr_operator declarator_1 |
| 944 | { $$.comp = $2.comp; |
| 945 | $$.last = $1.last; |
| 946 | *$2.last = $1.comp; } |
| 947 | | colon_ext_name |
| 948 | { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME); |
| 949 | d_left ($$.comp) = $1; |
| 950 | $$.last = &d_right ($$.comp); |
| 951 | } |
| 952 | | direct_declarator_1 |
| 953 | |
| 954 | /* Function local variable or type. The typespec to |
| 955 | our left is the type of the containing function. |
| 956 | This should be OK, because function local types |
| 957 | can not be templates, so the return types of their |
| 958 | members will not be mangled. If they are hopefully |
| 959 | they'll end up to the right of the ::. */ |
| 960 | | colon_ext_name function_arglist COLONCOLON start |
| 961 | { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp); |
| 962 | $$.last = $2.last; |
| 963 | $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4); |
| 964 | } |
| 965 | | direct_declarator_1 function_arglist COLONCOLON start |
| 966 | { $$.comp = $1.comp; |
| 967 | *$1.last = $2.comp; |
| 968 | $$.last = $2.last; |
| 969 | $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4); |
| 970 | } |
| 971 | ; |
| 972 | |
| 973 | direct_declarator_1 |
| 974 | : '(' ptr_operator declarator ')' |
| 975 | { $$.comp = $3.comp; |
| 976 | $$.last = $2.last; |
| 977 | *$3.last = $2.comp; } |
| 978 | | direct_declarator_1 function_arglist |
| 979 | { $$.comp = $1.comp; |
| 980 | *$1.last = $2.comp; |
| 981 | $$.last = $2.last; |
| 982 | } |
| 983 | | direct_declarator_1 array_indicator |
| 984 | { $$.comp = $1.comp; |
| 985 | *$1.last = $2; |
| 986 | $$.last = &d_right ($2); |
| 987 | } |
| 988 | | colon_ext_name function_arglist |
| 989 | { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp); |
| 990 | $$.last = $2.last; |
| 991 | } |
| 992 | | colon_ext_name array_indicator |
| 993 | { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2); |
| 994 | $$.last = &d_right ($2); |
| 995 | } |
| 996 | ; |
| 997 | |
| 998 | exp : '(' exp1 ')' |
| 999 | { $$ = $2; } |
| 1000 | ; |
| 1001 | |
| 1002 | /* Silly trick. Only allow '>' when parenthesized, in order to |
| 1003 | handle conflict with templates. */ |
| 1004 | exp1 : exp |
| 1005 | ; |
| 1006 | |
| 1007 | exp1 : exp '>' exp |
| 1008 | { $$ = d_binary (">", $1, $3); } |
| 1009 | ; |
| 1010 | |
| 1011 | /* References. Not allowed everywhere in template parameters, only |
| 1012 | at the top level, but treat them as expressions in case they are wrapped |
| 1013 | in parentheses. */ |
| 1014 | exp1 : '&' start |
| 1015 | { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); } |
| 1016 | | '&' '(' start ')' |
| 1017 | { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); } |
| 1018 | ; |
| 1019 | |
| 1020 | /* Expressions, not including the comma operator. */ |
| 1021 | exp : '-' exp %prec UNARY |
| 1022 | { $$ = d_unary ("-", $2); } |
| 1023 | ; |
| 1024 | |
| 1025 | exp : '!' exp %prec UNARY |
| 1026 | { $$ = d_unary ("!", $2); } |
| 1027 | ; |
| 1028 | |
| 1029 | exp : '~' exp %prec UNARY |
| 1030 | { $$ = d_unary ("~", $2); } |
| 1031 | ; |
| 1032 | |
| 1033 | /* Casts. First your normal C-style cast. If exp is a LITERAL, just change |
| 1034 | its type. */ |
| 1035 | |
| 1036 | exp : '(' type ')' exp %prec UNARY |
| 1037 | { if ($4->type == DEMANGLE_COMPONENT_LITERAL |
| 1038 | || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG) |
| 1039 | { |
| 1040 | $$ = $4; |
| 1041 | d_left ($4) = $2; |
| 1042 | } |
| 1043 | else |
| 1044 | $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, |
| 1045 | fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL), |
| 1046 | $4); |
| 1047 | } |
| 1048 | ; |
| 1049 | |
| 1050 | /* Mangling does not differentiate between these, so we don't need to |
| 1051 | either. */ |
| 1052 | exp : STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY |
| 1053 | { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, |
| 1054 | fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL), |
| 1055 | $6); |
| 1056 | } |
| 1057 | ; |
| 1058 | |
| 1059 | exp : DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY |
| 1060 | { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, |
| 1061 | fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL), |
| 1062 | $6); |
| 1063 | } |
| 1064 | ; |
| 1065 | |
| 1066 | exp : REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY |
| 1067 | { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, |
| 1068 | fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL), |
| 1069 | $6); |
| 1070 | } |
| 1071 | ; |
| 1072 | |
| 1073 | /* Another form of C++-style cast is "type ( exp1 )". This creates too many |
| 1074 | conflicts to support. For a while we supported the simpler |
| 1075 | "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a |
| 1076 | reference, deep within the wilderness of abstract declarators: |
| 1077 | Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the |
| 1078 | innermost left parenthesis. So we do not support function-like casts. |
| 1079 | Fortunately they never appear in demangler output. */ |
| 1080 | |
| 1081 | /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */ |
| 1082 | |
| 1083 | /* Binary operators in order of decreasing precedence. */ |
| 1084 | |
| 1085 | exp : exp '*' exp |
| 1086 | { $$ = d_binary ("*", $1, $3); } |
| 1087 | ; |
| 1088 | |
| 1089 | exp : exp '/' exp |
| 1090 | { $$ = d_binary ("/", $1, $3); } |
| 1091 | ; |
| 1092 | |
| 1093 | exp : exp '%' exp |
| 1094 | { $$ = d_binary ("%", $1, $3); } |
| 1095 | ; |
| 1096 | |
| 1097 | exp : exp '+' exp |
| 1098 | { $$ = d_binary ("+", $1, $3); } |
| 1099 | ; |
| 1100 | |
| 1101 | exp : exp '-' exp |
| 1102 | { $$ = d_binary ("-", $1, $3); } |
| 1103 | ; |
| 1104 | |
| 1105 | exp : exp LSH exp |
| 1106 | { $$ = d_binary ("<<", $1, $3); } |
| 1107 | ; |
| 1108 | |
| 1109 | exp : exp RSH exp |
| 1110 | { $$ = d_binary (">>", $1, $3); } |
| 1111 | ; |
| 1112 | |
| 1113 | exp : exp EQUAL exp |
| 1114 | { $$ = d_binary ("==", $1, $3); } |
| 1115 | ; |
| 1116 | |
| 1117 | exp : exp NOTEQUAL exp |
| 1118 | { $$ = d_binary ("!=", $1, $3); } |
| 1119 | ; |
| 1120 | |
| 1121 | exp : exp LEQ exp |
| 1122 | { $$ = d_binary ("<=", $1, $3); } |
| 1123 | ; |
| 1124 | |
| 1125 | exp : exp GEQ exp |
| 1126 | { $$ = d_binary (">=", $1, $3); } |
| 1127 | ; |
| 1128 | |
| 1129 | exp : exp '<' exp |
| 1130 | { $$ = d_binary ("<", $1, $3); } |
| 1131 | ; |
| 1132 | |
| 1133 | exp : exp '&' exp |
| 1134 | { $$ = d_binary ("&", $1, $3); } |
| 1135 | ; |
| 1136 | |
| 1137 | exp : exp '^' exp |
| 1138 | { $$ = d_binary ("^", $1, $3); } |
| 1139 | ; |
| 1140 | |
| 1141 | exp : exp '|' exp |
| 1142 | { $$ = d_binary ("|", $1, $3); } |
| 1143 | ; |
| 1144 | |
| 1145 | exp : exp ANDAND exp |
| 1146 | { $$ = d_binary ("&&", $1, $3); } |
| 1147 | ; |
| 1148 | |
| 1149 | exp : exp OROR exp |
| 1150 | { $$ = d_binary ("||", $1, $3); } |
| 1151 | ; |
| 1152 | |
| 1153 | /* Not 100% sure these are necessary, but they're harmless. */ |
| 1154 | exp : exp ARROW NAME |
| 1155 | { $$ = d_binary ("->", $1, $3); } |
| 1156 | ; |
| 1157 | |
| 1158 | exp : exp '.' NAME |
| 1159 | { $$ = d_binary (".", $1, $3); } |
| 1160 | ; |
| 1161 | |
| 1162 | exp : exp '?' exp ':' exp %prec '?' |
| 1163 | { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3), |
| 1164 | fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1, |
| 1165 | fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5))); |
| 1166 | } |
| 1167 | ; |
| 1168 | |
| 1169 | exp : INT |
| 1170 | ; |
| 1171 | |
| 1172 | /* Not generally allowed. */ |
| 1173 | exp : FLOAT |
| 1174 | ; |
| 1175 | |
| 1176 | exp : SIZEOF '(' type ')' %prec UNARY |
| 1177 | { $$ = d_unary ("sizeof", $3); } |
| 1178 | ; |
| 1179 | |
| 1180 | /* C++. */ |
| 1181 | exp : TRUEKEYWORD |
| 1182 | { struct demangle_component *i; |
| 1183 | i = make_name ("1", 1); |
| 1184 | $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL, |
| 1185 | make_builtin_type ("bool"), |
| 1186 | i); |
| 1187 | } |
| 1188 | ; |
| 1189 | |
| 1190 | exp : FALSEKEYWORD |
| 1191 | { struct demangle_component *i; |
| 1192 | i = make_name ("0", 1); |
| 1193 | $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL, |
| 1194 | make_builtin_type ("bool"), |
| 1195 | i); |
| 1196 | } |
| 1197 | ; |
| 1198 | |
| 1199 | /* end of C++. */ |
| 1200 | |
| 1201 | %% |
| 1202 | |
| 1203 | /* Apply QUALIFIERS to LHS and return a qualified component. IS_METHOD |
| 1204 | is set if LHS is a method, in which case the qualifiers are logically |
| 1205 | applied to "this". We apply qualifiers in a consistent order; LHS |
| 1206 | may already be qualified; duplicate qualifiers are not created. */ |
| 1207 | |
| 1208 | struct demangle_component * |
| 1209 | d_qualify (struct demangle_component *lhs, int qualifiers, int is_method) |
| 1210 | { |
| 1211 | struct demangle_component **inner_p; |
| 1212 | enum demangle_component_type type; |
| 1213 | |
| 1214 | /* For now the order is CONST (innermost), VOLATILE, RESTRICT. */ |
| 1215 | |
| 1216 | #define HANDLE_QUAL(TYPE, MTYPE, QUAL) \ |
| 1217 | if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE)) \ |
| 1218 | { \ |
| 1219 | *inner_p = fill_comp (is_method ? MTYPE : TYPE, \ |
| 1220 | *inner_p, NULL); \ |
| 1221 | inner_p = &d_left (*inner_p); \ |
| 1222 | type = (*inner_p)->type; \ |
| 1223 | } \ |
| 1224 | else if (type == TYPE || type == MTYPE) \ |
| 1225 | { \ |
| 1226 | inner_p = &d_left (*inner_p); \ |
| 1227 | type = (*inner_p)->type; \ |
| 1228 | } |
| 1229 | |
| 1230 | inner_p = &lhs; |
| 1231 | |
| 1232 | type = (*inner_p)->type; |
| 1233 | |
| 1234 | HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT); |
| 1235 | HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE); |
| 1236 | HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST); |
| 1237 | |
| 1238 | return lhs; |
| 1239 | } |
| 1240 | |
| 1241 | /* Return a builtin type corresponding to FLAGS. */ |
| 1242 | |
| 1243 | static struct demangle_component * |
| 1244 | d_int_type (int flags) |
| 1245 | { |
| 1246 | const char *name; |
| 1247 | |
| 1248 | switch (flags) |
| 1249 | { |
| 1250 | case INT_SIGNED | INT_CHAR: |
| 1251 | name = "signed char"; |
| 1252 | break; |
| 1253 | case INT_CHAR: |
| 1254 | name = "char"; |
| 1255 | break; |
| 1256 | case INT_UNSIGNED | INT_CHAR: |
| 1257 | name = "unsigned char"; |
| 1258 | break; |
| 1259 | case 0: |
| 1260 | case INT_SIGNED: |
| 1261 | name = "int"; |
| 1262 | break; |
| 1263 | case INT_UNSIGNED: |
| 1264 | name = "unsigned int"; |
| 1265 | break; |
| 1266 | case INT_LONG: |
| 1267 | case INT_SIGNED | INT_LONG: |
| 1268 | name = "long"; |
| 1269 | break; |
| 1270 | case INT_UNSIGNED | INT_LONG: |
| 1271 | name = "unsigned long"; |
| 1272 | break; |
| 1273 | case INT_SHORT: |
| 1274 | case INT_SIGNED | INT_SHORT: |
| 1275 | name = "short"; |
| 1276 | break; |
| 1277 | case INT_UNSIGNED | INT_SHORT: |
| 1278 | name = "unsigned short"; |
| 1279 | break; |
| 1280 | case INT_LLONG | INT_LONG: |
| 1281 | case INT_SIGNED | INT_LLONG | INT_LONG: |
| 1282 | name = "long long"; |
| 1283 | break; |
| 1284 | case INT_UNSIGNED | INT_LLONG | INT_LONG: |
| 1285 | name = "unsigned long long"; |
| 1286 | break; |
| 1287 | default: |
| 1288 | return NULL; |
| 1289 | } |
| 1290 | |
| 1291 | return make_builtin_type (name); |
| 1292 | } |
| 1293 | |
| 1294 | /* Wrapper to create a unary operation. */ |
| 1295 | |
| 1296 | static struct demangle_component * |
| 1297 | d_unary (const char *name, struct demangle_component *lhs) |
| 1298 | { |
| 1299 | return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs); |
| 1300 | } |
| 1301 | |
| 1302 | /* Wrapper to create a binary operation. */ |
| 1303 | |
| 1304 | static struct demangle_component * |
| 1305 | d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs) |
| 1306 | { |
| 1307 | return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2), |
| 1308 | fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs)); |
| 1309 | } |
| 1310 | |
| 1311 | /* Find the end of a symbol name starting at LEXPTR. */ |
| 1312 | |
| 1313 | static const char * |
| 1314 | symbol_end (const char *lexptr) |
| 1315 | { |
| 1316 | const char *p = lexptr; |
| 1317 | |
| 1318 | while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.')) |
| 1319 | p++; |
| 1320 | |
| 1321 | return p; |
| 1322 | } |
| 1323 | |
| 1324 | /* Take care of parsing a number (anything that starts with a digit). |
| 1325 | The number starts at P and contains LEN characters. Store the result in |
| 1326 | YYLVAL. */ |
| 1327 | |
| 1328 | static int |
| 1329 | parse_number (const char *p, int len, int parsed_float) |
| 1330 | { |
| 1331 | int unsigned_p = 0; |
| 1332 | |
| 1333 | /* Number of "L" suffixes encountered. */ |
| 1334 | int long_p = 0; |
| 1335 | |
| 1336 | struct demangle_component *signed_type; |
| 1337 | struct demangle_component *unsigned_type; |
| 1338 | struct demangle_component *type, *name; |
| 1339 | enum demangle_component_type literal_type; |
| 1340 | |
| 1341 | if (p[0] == '-') |
| 1342 | { |
| 1343 | literal_type = DEMANGLE_COMPONENT_LITERAL_NEG; |
| 1344 | p++; |
| 1345 | len--; |
| 1346 | } |
| 1347 | else |
| 1348 | literal_type = DEMANGLE_COMPONENT_LITERAL; |
| 1349 | |
| 1350 | if (parsed_float) |
| 1351 | { |
| 1352 | /* It's a float since it contains a point or an exponent. */ |
| 1353 | char c; |
| 1354 | |
| 1355 | /* The GDB lexer checks the result of scanf at this point. Not doing |
| 1356 | this leaves our error checking slightly weaker but only for invalid |
| 1357 | data. */ |
| 1358 | |
| 1359 | /* See if it has `f' or `l' suffix (float or long double). */ |
| 1360 | |
| 1361 | c = TOLOWER (p[len - 1]); |
| 1362 | |
| 1363 | if (c == 'f') |
| 1364 | { |
| 1365 | len--; |
| 1366 | type = make_builtin_type ("float"); |
| 1367 | } |
| 1368 | else if (c == 'l') |
| 1369 | { |
| 1370 | len--; |
| 1371 | type = make_builtin_type ("long double"); |
| 1372 | } |
| 1373 | else if (ISDIGIT (c) || c == '.') |
| 1374 | type = make_builtin_type ("double"); |
| 1375 | else |
| 1376 | return ERROR; |
| 1377 | |
| 1378 | name = make_name (p, len); |
| 1379 | yylval.comp = fill_comp (literal_type, type, name); |
| 1380 | |
| 1381 | return FLOAT; |
| 1382 | } |
| 1383 | |
| 1384 | /* This treats 0x1 and 1 as different literals. We also do not |
| 1385 | automatically generate unsigned types. */ |
| 1386 | |
| 1387 | long_p = 0; |
| 1388 | unsigned_p = 0; |
| 1389 | while (len > 0) |
| 1390 | { |
| 1391 | if (p[len - 1] == 'l' || p[len - 1] == 'L') |
| 1392 | { |
| 1393 | len--; |
| 1394 | long_p++; |
| 1395 | continue; |
| 1396 | } |
| 1397 | if (p[len - 1] == 'u' || p[len - 1] == 'U') |
| 1398 | { |
| 1399 | len--; |
| 1400 | unsigned_p++; |
| 1401 | continue; |
| 1402 | } |
| 1403 | break; |
| 1404 | } |
| 1405 | |
| 1406 | if (long_p == 0) |
| 1407 | { |
| 1408 | unsigned_type = make_builtin_type ("unsigned int"); |
| 1409 | signed_type = make_builtin_type ("int"); |
| 1410 | } |
| 1411 | else if (long_p == 1) |
| 1412 | { |
| 1413 | unsigned_type = make_builtin_type ("unsigned long"); |
| 1414 | signed_type = make_builtin_type ("long"); |
| 1415 | } |
| 1416 | else |
| 1417 | { |
| 1418 | unsigned_type = make_builtin_type ("unsigned long long"); |
| 1419 | signed_type = make_builtin_type ("long long"); |
| 1420 | } |
| 1421 | |
| 1422 | if (unsigned_p) |
| 1423 | type = unsigned_type; |
| 1424 | else |
| 1425 | type = signed_type; |
| 1426 | |
| 1427 | name = make_name (p, len); |
| 1428 | yylval.comp = fill_comp (literal_type, type, name); |
| 1429 | |
| 1430 | return INT; |
| 1431 | } |
| 1432 | |
| 1433 | static char backslashable[] = "abefnrtv"; |
| 1434 | static char represented[] = "\a\b\e\f\n\r\t\v"; |
| 1435 | |
| 1436 | /* Translate the backslash the way we would in the host character set. */ |
| 1437 | static int |
| 1438 | c_parse_backslash (int host_char, int *target_char) |
| 1439 | { |
| 1440 | const char *ix; |
| 1441 | ix = strchr (backslashable, host_char); |
| 1442 | if (! ix) |
| 1443 | return 0; |
| 1444 | else |
| 1445 | *target_char = represented[ix - backslashable]; |
| 1446 | return 1; |
| 1447 | } |
| 1448 | |
| 1449 | /* Parse a C escape sequence. STRING_PTR points to a variable |
| 1450 | containing a pointer to the string to parse. That pointer |
| 1451 | should point to the character after the \. That pointer |
| 1452 | is updated past the characters we use. The value of the |
| 1453 | escape sequence is returned. |
| 1454 | |
| 1455 | A negative value means the sequence \ newline was seen, |
| 1456 | which is supposed to be equivalent to nothing at all. |
| 1457 | |
| 1458 | If \ is followed by a null character, we return a negative |
| 1459 | value and leave the string pointer pointing at the null character. |
| 1460 | |
| 1461 | If \ is followed by 000, we return 0 and leave the string pointer |
| 1462 | after the zeros. A value of 0 does not mean end of string. */ |
| 1463 | |
| 1464 | static int |
| 1465 | cp_parse_escape (const char **string_ptr) |
| 1466 | { |
| 1467 | int target_char; |
| 1468 | int c = *(*string_ptr)++; |
| 1469 | if (c_parse_backslash (c, &target_char)) |
| 1470 | return target_char; |
| 1471 | else |
| 1472 | switch (c) |
| 1473 | { |
| 1474 | case '\n': |
| 1475 | return -2; |
| 1476 | case 0: |
| 1477 | (*string_ptr)--; |
| 1478 | return 0; |
| 1479 | case '^': |
| 1480 | { |
| 1481 | c = *(*string_ptr)++; |
| 1482 | |
| 1483 | if (c == '?') |
| 1484 | return 0177; |
| 1485 | else if (c == '\\') |
| 1486 | target_char = cp_parse_escape (string_ptr); |
| 1487 | else |
| 1488 | target_char = c; |
| 1489 | |
| 1490 | /* Now target_char is something like `c', and we want to find |
| 1491 | its control-character equivalent. */ |
| 1492 | target_char = target_char & 037; |
| 1493 | |
| 1494 | return target_char; |
| 1495 | } |
| 1496 | |
| 1497 | case '0': |
| 1498 | case '1': |
| 1499 | case '2': |
| 1500 | case '3': |
| 1501 | case '4': |
| 1502 | case '5': |
| 1503 | case '6': |
| 1504 | case '7': |
| 1505 | { |
| 1506 | int i = c - '0'; |
| 1507 | int count = 0; |
| 1508 | while (++count < 3) |
| 1509 | { |
| 1510 | c = (**string_ptr); |
| 1511 | if (c >= '0' && c <= '7') |
| 1512 | { |
| 1513 | (*string_ptr)++; |
| 1514 | i *= 8; |
| 1515 | i += c - '0'; |
| 1516 | } |
| 1517 | else |
| 1518 | { |
| 1519 | break; |
| 1520 | } |
| 1521 | } |
| 1522 | return i; |
| 1523 | } |
| 1524 | default: |
| 1525 | return c; |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | #define HANDLE_SPECIAL(string, comp) \ |
| 1530 | if (strncmp (tokstart, string, sizeof (string) - 1) == 0) \ |
| 1531 | { \ |
| 1532 | lexptr = tokstart + sizeof (string) - 1; \ |
| 1533 | yylval.lval = comp; \ |
| 1534 | return DEMANGLER_SPECIAL; \ |
| 1535 | } |
| 1536 | |
| 1537 | #define HANDLE_TOKEN2(string, token) \ |
| 1538 | if (lexptr[1] == string[1]) \ |
| 1539 | { \ |
| 1540 | lexptr += 2; \ |
| 1541 | yylval.opname = string; \ |
| 1542 | return token; \ |
| 1543 | } |
| 1544 | |
| 1545 | #define HANDLE_TOKEN3(string, token) \ |
| 1546 | if (lexptr[1] == string[1] && lexptr[2] == string[2]) \ |
| 1547 | { \ |
| 1548 | lexptr += 3; \ |
| 1549 | yylval.opname = string; \ |
| 1550 | return token; \ |
| 1551 | } |
| 1552 | |
| 1553 | /* Read one token, getting characters through LEXPTR. */ |
| 1554 | |
| 1555 | static int |
| 1556 | yylex (void) |
| 1557 | { |
| 1558 | int c; |
| 1559 | int namelen; |
| 1560 | const char *tokstart, *tokptr; |
| 1561 | |
| 1562 | retry: |
| 1563 | prev_lexptr = lexptr; |
| 1564 | tokstart = lexptr; |
| 1565 | |
| 1566 | switch (c = *tokstart) |
| 1567 | { |
| 1568 | case 0: |
| 1569 | return 0; |
| 1570 | |
| 1571 | case ' ': |
| 1572 | case '\t': |
| 1573 | case '\n': |
| 1574 | lexptr++; |
| 1575 | goto retry; |
| 1576 | |
| 1577 | case '\'': |
| 1578 | /* We either have a character constant ('0' or '\177' for example) |
| 1579 | or we have a quoted symbol reference ('foo(int,int)' in C++ |
| 1580 | for example). */ |
| 1581 | lexptr++; |
| 1582 | c = *lexptr++; |
| 1583 | if (c == '\\') |
| 1584 | c = cp_parse_escape (&lexptr); |
| 1585 | else if (c == '\'') |
| 1586 | { |
| 1587 | yyerror ("empty character constant"); |
| 1588 | return ERROR; |
| 1589 | } |
| 1590 | |
| 1591 | c = *lexptr++; |
| 1592 | if (c != '\'') |
| 1593 | { |
| 1594 | yyerror ("invalid character constant"); |
| 1595 | return ERROR; |
| 1596 | } |
| 1597 | |
| 1598 | /* FIXME: We should refer to a canonical form of the character, |
| 1599 | presumably the same one that appears in manglings - the decimal |
| 1600 | representation. But if that isn't in our input then we have to |
| 1601 | allocate memory for it somewhere. */ |
| 1602 | yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL, |
| 1603 | make_builtin_type ("char"), |
| 1604 | make_name (tokstart, lexptr - tokstart)); |
| 1605 | |
| 1606 | return INT; |
| 1607 | |
| 1608 | case '(': |
| 1609 | if (strncmp (tokstart, "(anonymous namespace)", 21) == 0) |
| 1610 | { |
| 1611 | lexptr += 21; |
| 1612 | yylval.comp = make_name ("(anonymous namespace)", |
| 1613 | sizeof "(anonymous namespace)" - 1); |
| 1614 | return NAME; |
| 1615 | } |
| 1616 | /* FALL THROUGH */ |
| 1617 | |
| 1618 | case ')': |
| 1619 | case ',': |
| 1620 | lexptr++; |
| 1621 | return c; |
| 1622 | |
| 1623 | case '.': |
| 1624 | if (lexptr[1] == '.' && lexptr[2] == '.') |
| 1625 | { |
| 1626 | lexptr += 3; |
| 1627 | return ELLIPSIS; |
| 1628 | } |
| 1629 | |
| 1630 | /* Might be a floating point number. */ |
| 1631 | if (lexptr[1] < '0' || lexptr[1] > '9') |
| 1632 | goto symbol; /* Nope, must be a symbol. */ |
| 1633 | |
| 1634 | goto try_number; |
| 1635 | |
| 1636 | case '-': |
| 1637 | HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY); |
| 1638 | HANDLE_TOKEN2 ("--", DECREMENT); |
| 1639 | HANDLE_TOKEN2 ("->", ARROW); |
| 1640 | |
| 1641 | /* For construction vtables. This is kind of hokey. */ |
| 1642 | if (strncmp (tokstart, "-in-", 4) == 0) |
| 1643 | { |
| 1644 | lexptr += 4; |
| 1645 | return CONSTRUCTION_IN; |
| 1646 | } |
| 1647 | |
| 1648 | if (lexptr[1] < '0' || lexptr[1] > '9') |
| 1649 | { |
| 1650 | lexptr++; |
| 1651 | return '-'; |
| 1652 | } |
| 1653 | /* FALL THRU into number case. */ |
| 1654 | |
| 1655 | try_number: |
| 1656 | case '0': |
| 1657 | case '1': |
| 1658 | case '2': |
| 1659 | case '3': |
| 1660 | case '4': |
| 1661 | case '5': |
| 1662 | case '6': |
| 1663 | case '7': |
| 1664 | case '8': |
| 1665 | case '9': |
| 1666 | { |
| 1667 | /* It's a number. */ |
| 1668 | int got_dot = 0, got_e = 0, toktype; |
| 1669 | const char *p = tokstart; |
| 1670 | int hex = 0; |
| 1671 | |
| 1672 | if (c == '-') |
| 1673 | p++; |
| 1674 | |
| 1675 | if (c == '0' && (p[1] == 'x' || p[1] == 'X')) |
| 1676 | { |
| 1677 | p += 2; |
| 1678 | hex = 1; |
| 1679 | } |
| 1680 | else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D')) |
| 1681 | { |
| 1682 | p += 2; |
| 1683 | hex = 0; |
| 1684 | } |
| 1685 | |
| 1686 | for (;; ++p) |
| 1687 | { |
| 1688 | /* This test includes !hex because 'e' is a valid hex digit |
| 1689 | and thus does not indicate a floating point number when |
| 1690 | the radix is hex. */ |
| 1691 | if (!hex && !got_e && (*p == 'e' || *p == 'E')) |
| 1692 | got_dot = got_e = 1; |
| 1693 | /* This test does not include !hex, because a '.' always indicates |
| 1694 | a decimal floating point number regardless of the radix. |
| 1695 | |
| 1696 | NOTE drow/2005-03-09: This comment is not accurate in C99; |
| 1697 | however, it's not clear that all the floating point support |
| 1698 | in this file is doing any good here. */ |
| 1699 | else if (!got_dot && *p == '.') |
| 1700 | got_dot = 1; |
| 1701 | else if (got_e && (p[-1] == 'e' || p[-1] == 'E') |
| 1702 | && (*p == '-' || *p == '+')) |
| 1703 | /* This is the sign of the exponent, not the end of the |
| 1704 | number. */ |
| 1705 | continue; |
| 1706 | /* We will take any letters or digits. parse_number will |
| 1707 | complain if past the radix, or if L or U are not final. */ |
| 1708 | else if (! ISALNUM (*p)) |
| 1709 | break; |
| 1710 | } |
| 1711 | toktype = parse_number (tokstart, p - tokstart, got_dot|got_e); |
| 1712 | if (toktype == ERROR) |
| 1713 | { |
| 1714 | char *err_copy = (char *) alloca (p - tokstart + 1); |
| 1715 | |
| 1716 | memcpy (err_copy, tokstart, p - tokstart); |
| 1717 | err_copy[p - tokstart] = 0; |
| 1718 | yyerror ("invalid number"); |
| 1719 | return ERROR; |
| 1720 | } |
| 1721 | lexptr = p; |
| 1722 | return toktype; |
| 1723 | } |
| 1724 | |
| 1725 | case '+': |
| 1726 | HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY); |
| 1727 | HANDLE_TOKEN2 ("++", INCREMENT); |
| 1728 | lexptr++; |
| 1729 | return c; |
| 1730 | case '*': |
| 1731 | HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY); |
| 1732 | lexptr++; |
| 1733 | return c; |
| 1734 | case '/': |
| 1735 | HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY); |
| 1736 | lexptr++; |
| 1737 | return c; |
| 1738 | case '%': |
| 1739 | HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY); |
| 1740 | lexptr++; |
| 1741 | return c; |
| 1742 | case '|': |
| 1743 | HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY); |
| 1744 | HANDLE_TOKEN2 ("||", OROR); |
| 1745 | lexptr++; |
| 1746 | return c; |
| 1747 | case '&': |
| 1748 | HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY); |
| 1749 | HANDLE_TOKEN2 ("&&", ANDAND); |
| 1750 | lexptr++; |
| 1751 | return c; |
| 1752 | case '^': |
| 1753 | HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY); |
| 1754 | lexptr++; |
| 1755 | return c; |
| 1756 | case '!': |
| 1757 | HANDLE_TOKEN2 ("!=", NOTEQUAL); |
| 1758 | lexptr++; |
| 1759 | return c; |
| 1760 | case '<': |
| 1761 | HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY); |
| 1762 | HANDLE_TOKEN2 ("<=", LEQ); |
| 1763 | HANDLE_TOKEN2 ("<<", LSH); |
| 1764 | lexptr++; |
| 1765 | return c; |
| 1766 | case '>': |
| 1767 | HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY); |
| 1768 | HANDLE_TOKEN2 (">=", GEQ); |
| 1769 | HANDLE_TOKEN2 (">>", RSH); |
| 1770 | lexptr++; |
| 1771 | return c; |
| 1772 | case '=': |
| 1773 | HANDLE_TOKEN2 ("==", EQUAL); |
| 1774 | lexptr++; |
| 1775 | return c; |
| 1776 | case ':': |
| 1777 | HANDLE_TOKEN2 ("::", COLONCOLON); |
| 1778 | lexptr++; |
| 1779 | return c; |
| 1780 | |
| 1781 | case '[': |
| 1782 | case ']': |
| 1783 | case '?': |
| 1784 | case '@': |
| 1785 | case '~': |
| 1786 | case '{': |
| 1787 | case '}': |
| 1788 | symbol: |
| 1789 | lexptr++; |
| 1790 | return c; |
| 1791 | |
| 1792 | case '"': |
| 1793 | /* These can't occur in C++ names. */ |
| 1794 | yyerror ("unexpected string literal"); |
| 1795 | return ERROR; |
| 1796 | } |
| 1797 | |
| 1798 | if (!(c == '_' || c == '$' || ISALPHA (c))) |
| 1799 | { |
| 1800 | /* We must have come across a bad character (e.g. ';'). */ |
| 1801 | yyerror ("invalid character"); |
| 1802 | return ERROR; |
| 1803 | } |
| 1804 | |
| 1805 | /* It's a name. See how long it is. */ |
| 1806 | namelen = 0; |
| 1807 | do |
| 1808 | c = tokstart[++namelen]; |
| 1809 | while (ISALNUM (c) || c == '_' || c == '$'); |
| 1810 | |
| 1811 | lexptr += namelen; |
| 1812 | |
| 1813 | /* Catch specific keywords. Notice that some of the keywords contain |
| 1814 | spaces, and are sorted by the length of the first word. They must |
| 1815 | all include a trailing space in the string comparison. */ |
| 1816 | switch (namelen) |
| 1817 | { |
| 1818 | case 16: |
| 1819 | if (strncmp (tokstart, "reinterpret_cast", 16) == 0) |
| 1820 | return REINTERPRET_CAST; |
| 1821 | break; |
| 1822 | case 12: |
| 1823 | if (strncmp (tokstart, "construction vtable for ", 24) == 0) |
| 1824 | { |
| 1825 | lexptr = tokstart + 24; |
| 1826 | return CONSTRUCTION_VTABLE; |
| 1827 | } |
| 1828 | if (strncmp (tokstart, "dynamic_cast", 12) == 0) |
| 1829 | return DYNAMIC_CAST; |
| 1830 | break; |
| 1831 | case 11: |
| 1832 | if (strncmp (tokstart, "static_cast", 11) == 0) |
| 1833 | return STATIC_CAST; |
| 1834 | break; |
| 1835 | case 9: |
| 1836 | HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK); |
| 1837 | HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP); |
| 1838 | break; |
| 1839 | case 8: |
| 1840 | HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO); |
| 1841 | HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN); |
| 1842 | HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME); |
| 1843 | if (strncmp (tokstart, "operator", 8) == 0) |
| 1844 | return OPERATOR; |
| 1845 | if (strncmp (tokstart, "restrict", 8) == 0) |
| 1846 | return RESTRICT; |
| 1847 | if (strncmp (tokstart, "unsigned", 8) == 0) |
| 1848 | return UNSIGNED; |
| 1849 | if (strncmp (tokstart, "template", 8) == 0) |
| 1850 | return TEMPLATE; |
| 1851 | if (strncmp (tokstart, "volatile", 8) == 0) |
| 1852 | return VOLATILE_KEYWORD; |
| 1853 | break; |
| 1854 | case 7: |
| 1855 | HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK); |
| 1856 | if (strncmp (tokstart, "wchar_t", 7) == 0) |
| 1857 | return WCHAR_T; |
| 1858 | break; |
| 1859 | case 6: |
| 1860 | if (strncmp (tokstart, "global constructors keyed to ", 29) == 0) |
| 1861 | { |
| 1862 | const char *p; |
| 1863 | lexptr = tokstart + 29; |
| 1864 | yylval.typed_val_int.val = GLOBAL_CONSTRUCTORS; |
| 1865 | /* Find the end of the symbol. */ |
| 1866 | p = symbol_end (lexptr); |
| 1867 | yylval.typed_val_int.type = make_name (lexptr, p - lexptr); |
| 1868 | lexptr = p; |
| 1869 | return GLOBAL; |
| 1870 | } |
| 1871 | if (strncmp (tokstart, "global destructors keyed to ", 28) == 0) |
| 1872 | { |
| 1873 | const char *p; |
| 1874 | lexptr = tokstart + 28; |
| 1875 | yylval.typed_val_int.val = GLOBAL_DESTRUCTORS; |
| 1876 | /* Find the end of the symbol. */ |
| 1877 | p = symbol_end (lexptr); |
| 1878 | yylval.typed_val_int.type = make_name (lexptr, p - lexptr); |
| 1879 | lexptr = p; |
| 1880 | return GLOBAL; |
| 1881 | } |
| 1882 | |
| 1883 | HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE); |
| 1884 | if (strncmp (tokstart, "delete", 6) == 0) |
| 1885 | return DELETE; |
| 1886 | if (strncmp (tokstart, "struct", 6) == 0) |
| 1887 | return STRUCT; |
| 1888 | if (strncmp (tokstart, "signed", 6) == 0) |
| 1889 | return SIGNED_KEYWORD; |
| 1890 | if (strncmp (tokstart, "sizeof", 6) == 0) |
| 1891 | return SIZEOF; |
| 1892 | if (strncmp (tokstart, "double", 6) == 0) |
| 1893 | return DOUBLE_KEYWORD; |
| 1894 | break; |
| 1895 | case 5: |
| 1896 | HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD); |
| 1897 | if (strncmp (tokstart, "false", 5) == 0) |
| 1898 | return FALSEKEYWORD; |
| 1899 | if (strncmp (tokstart, "class", 5) == 0) |
| 1900 | return CLASS; |
| 1901 | if (strncmp (tokstart, "union", 5) == 0) |
| 1902 | return UNION; |
| 1903 | if (strncmp (tokstart, "float", 5) == 0) |
| 1904 | return FLOAT_KEYWORD; |
| 1905 | if (strncmp (tokstart, "short", 5) == 0) |
| 1906 | return SHORT; |
| 1907 | if (strncmp (tokstart, "const", 5) == 0) |
| 1908 | return CONST_KEYWORD; |
| 1909 | break; |
| 1910 | case 4: |
| 1911 | if (strncmp (tokstart, "void", 4) == 0) |
| 1912 | return VOID; |
| 1913 | if (strncmp (tokstart, "bool", 4) == 0) |
| 1914 | return BOOL; |
| 1915 | if (strncmp (tokstart, "char", 4) == 0) |
| 1916 | return CHAR; |
| 1917 | if (strncmp (tokstart, "enum", 4) == 0) |
| 1918 | return ENUM; |
| 1919 | if (strncmp (tokstart, "long", 4) == 0) |
| 1920 | return LONG; |
| 1921 | if (strncmp (tokstart, "true", 4) == 0) |
| 1922 | return TRUEKEYWORD; |
| 1923 | break; |
| 1924 | case 3: |
| 1925 | HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT); |
| 1926 | HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK); |
| 1927 | if (strncmp (tokstart, "new", 3) == 0) |
| 1928 | return NEW; |
| 1929 | if (strncmp (tokstart, "int", 3) == 0) |
| 1930 | return INT_KEYWORD; |
| 1931 | break; |
| 1932 | default: |
| 1933 | break; |
| 1934 | } |
| 1935 | |
| 1936 | yylval.comp = make_name (tokstart, namelen); |
| 1937 | return NAME; |
| 1938 | } |
| 1939 | |
| 1940 | static void |
| 1941 | yyerror (char *msg) |
| 1942 | { |
| 1943 | if (global_errmsg) |
| 1944 | return; |
| 1945 | |
| 1946 | error_lexptr = prev_lexptr; |
| 1947 | global_errmsg = msg ? msg : "parse error"; |
| 1948 | } |
| 1949 | |
| 1950 | /* Allocate a chunk of the components we'll need to build a tree. We |
| 1951 | generally allocate too many components, but the extra memory usage |
| 1952 | doesn't hurt because the trees are temporary and the storage is |
| 1953 | reused. More may be allocated later, by d_grab. */ |
| 1954 | static void |
| 1955 | allocate_info (void) |
| 1956 | { |
| 1957 | if (demangle_info == NULL) |
| 1958 | { |
| 1959 | demangle_info = malloc (sizeof (struct demangle_info)); |
| 1960 | demangle_info->prev = NULL; |
| 1961 | demangle_info->next = NULL; |
| 1962 | } |
| 1963 | else |
| 1964 | while (demangle_info->prev) |
| 1965 | demangle_info = demangle_info->prev; |
| 1966 | |
| 1967 | demangle_info->used = 0; |
| 1968 | } |
| 1969 | |
| 1970 | /* Convert RESULT to a string. The return value is allocated |
| 1971 | using xmalloc. ESTIMATED_LEN is used only as a guide to the |
| 1972 | length of the result. This functions handles a few cases that |
| 1973 | cplus_demangle_print does not, specifically the global destructor |
| 1974 | and constructor labels. */ |
| 1975 | |
| 1976 | char * |
| 1977 | cp_comp_to_string (struct demangle_component *result, int estimated_len) |
| 1978 | { |
| 1979 | char *str, *prefix = NULL, *buf; |
| 1980 | size_t err = 0; |
| 1981 | |
| 1982 | if (result->type == GLOBAL_DESTRUCTORS) |
| 1983 | { |
| 1984 | result = d_left (result); |
| 1985 | prefix = "global destructors keyed to "; |
| 1986 | } |
| 1987 | else if (result->type == GLOBAL_CONSTRUCTORS) |
| 1988 | { |
| 1989 | result = d_left (result); |
| 1990 | prefix = "global constructors keyed to "; |
| 1991 | } |
| 1992 | |
| 1993 | str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len, &err); |
| 1994 | if (str == NULL) |
| 1995 | return NULL; |
| 1996 | |
| 1997 | if (prefix == NULL) |
| 1998 | return str; |
| 1999 | |
| 2000 | buf = malloc (strlen (str) + strlen (prefix) + 1); |
| 2001 | strcpy (buf, prefix); |
| 2002 | strcat (buf, str); |
| 2003 | free (str); |
| 2004 | return (buf); |
| 2005 | } |
| 2006 | |
| 2007 | /* Convert a demangled name to a demangle_component tree. On success, |
| 2008 | the root of the new tree is returned; it is valid until the next |
| 2009 | call to this function and should not be freed. On error, NULL is |
| 2010 | returned, and an error message will be set in *ERRMSG (which does |
| 2011 | not need to be freed). */ |
| 2012 | |
| 2013 | struct demangle_component * |
| 2014 | cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg) |
| 2015 | { |
| 2016 | static char errbuf[60]; |
| 2017 | struct demangle_component *result; |
| 2018 | |
| 2019 | prev_lexptr = lexptr = demangled_name; |
| 2020 | error_lexptr = NULL; |
| 2021 | global_errmsg = NULL; |
| 2022 | |
| 2023 | allocate_info (); |
| 2024 | |
| 2025 | if (yyparse ()) |
| 2026 | { |
| 2027 | if (global_errmsg && errmsg) |
| 2028 | { |
| 2029 | snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s", |
| 2030 | global_errmsg, error_lexptr); |
| 2031 | strcat (errbuf, "'"); |
| 2032 | *errmsg = errbuf; |
| 2033 | } |
| 2034 | return NULL; |
| 2035 | } |
| 2036 | |
| 2037 | result = global_result; |
| 2038 | global_result = NULL; |
| 2039 | |
| 2040 | return result; |
| 2041 | } |
| 2042 | |
| 2043 | #ifdef TEST_CPNAMES |
| 2044 | |
| 2045 | static void |
| 2046 | cp_print (struct demangle_component *result) |
| 2047 | { |
| 2048 | char *str; |
| 2049 | size_t err = 0; |
| 2050 | |
| 2051 | if (result->type == GLOBAL_DESTRUCTORS) |
| 2052 | { |
| 2053 | result = d_left (result); |
| 2054 | fputs ("global destructors keyed to ", stdout); |
| 2055 | } |
| 2056 | else if (result->type == GLOBAL_CONSTRUCTORS) |
| 2057 | { |
| 2058 | result = d_left (result); |
| 2059 | fputs ("global constructors keyed to ", stdout); |
| 2060 | } |
| 2061 | |
| 2062 | str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err); |
| 2063 | if (str == NULL) |
| 2064 | return; |
| 2065 | |
| 2066 | fputs (str, stdout); |
| 2067 | |
| 2068 | free (str); |
| 2069 | } |
| 2070 | |
| 2071 | static char |
| 2072 | trim_chars (char *lexptr, char **extra_chars) |
| 2073 | { |
| 2074 | char *p = (char *) symbol_end (lexptr); |
| 2075 | char c = 0; |
| 2076 | |
| 2077 | if (*p) |
| 2078 | { |
| 2079 | c = *p; |
| 2080 | *p = 0; |
| 2081 | *extra_chars = p + 1; |
| 2082 | } |
| 2083 | |
| 2084 | return c; |
| 2085 | } |
| 2086 | |
| 2087 | /* When this file is built as a standalone program, xmalloc comes from |
| 2088 | libiberty --- in which case we have to provide xfree ourselves. */ |
| 2089 | |
| 2090 | void |
| 2091 | xfree (void *ptr) |
| 2092 | { |
| 2093 | if (ptr != NULL) |
| 2094 | free (ptr); |
| 2095 | } |
| 2096 | |
| 2097 | int |
| 2098 | main (int argc, char **argv) |
| 2099 | { |
| 2100 | char *str2, *extra_chars = "", c; |
| 2101 | char buf[65536]; |
| 2102 | int arg; |
| 2103 | const char *errmsg; |
| 2104 | struct demangle_component *result; |
| 2105 | |
| 2106 | arg = 1; |
| 2107 | if (argv[arg] && strcmp (argv[arg], "--debug") == 0) |
| 2108 | { |
| 2109 | yydebug = 1; |
| 2110 | arg++; |
| 2111 | } |
| 2112 | |
| 2113 | if (argv[arg] == NULL) |
| 2114 | while (fgets (buf, 65536, stdin) != NULL) |
| 2115 | { |
| 2116 | int len; |
| 2117 | buf[strlen (buf) - 1] = 0; |
| 2118 | /* Use DMGL_VERBOSE to get expanded standard substitutions. */ |
| 2119 | c = trim_chars (buf, &extra_chars); |
| 2120 | str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE); |
| 2121 | if (str2 == NULL) |
| 2122 | { |
| 2123 | /* printf ("Demangling error\n"); */ |
| 2124 | if (c) |
| 2125 | printf ("%s%c%s\n", buf, c, extra_chars); |
| 2126 | else |
| 2127 | printf ("%s\n", buf); |
| 2128 | continue; |
| 2129 | } |
| 2130 | result = cp_demangled_name_to_comp (str2, &errmsg); |
| 2131 | if (result == NULL) |
| 2132 | { |
| 2133 | fputs (errmsg, stderr); |
| 2134 | fputc ('\n', stderr); |
| 2135 | continue; |
| 2136 | } |
| 2137 | |
| 2138 | cp_print (result); |
| 2139 | |
| 2140 | free (str2); |
| 2141 | if (c) |
| 2142 | { |
| 2143 | putchar (c); |
| 2144 | fputs (extra_chars, stdout); |
| 2145 | } |
| 2146 | putchar ('\n'); |
| 2147 | } |
| 2148 | else |
| 2149 | { |
| 2150 | result = cp_demangled_name_to_comp (argv[arg], &errmsg); |
| 2151 | if (result == NULL) |
| 2152 | { |
| 2153 | fputs (errmsg, stderr); |
| 2154 | fputc ('\n', stderr); |
| 2155 | return 0; |
| 2156 | } |
| 2157 | cp_print (result); |
| 2158 | putchar ('\n'); |
| 2159 | } |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
| 2163 | #endif |