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