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