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