forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcp-name-parser.y
2243 lines (1934 loc) · 56.3 KB
/
cp-name-parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* YACC parser for C++ names, for GDB.
Copyright (C) 2003-2015 Free Software Foundation, Inc.
Parts of the lexer are based on c-exp.y from GDB.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* Note that malloc's and realloc's in this file are transformed to
xmalloc and xrealloc respectively by the same sed command in the
makefile that remaps any other malloc/realloc inserted by the parser
generator. Doing this with #defines and trying to control the interaction
with include files (<malloc.h> and <stdlib.h> for example) just became
too messy, particularly when such includes can be inserted at random
times by the parser generator. */
%{
#include "defs.h"
#include <unistd.h>
#include "safe-ctype.h"
#include "demangle.h"
#include "cp-support.h"
/* Bison does not make it easy to create a parser without global
state, unfortunately. Here are all the global variables used
in this parser. */
/* LEXPTR is the current pointer into our lex buffer. PREV_LEXPTR
is the start of the last token lexed, only used for diagnostics.
ERROR_LEXPTR is the first place an error occurred. GLOBAL_ERRMSG
is the first error message encountered. */
static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
/* The components built by the parser are allocated ahead of time,
and cached in this structure. */
#define ALLOC_CHUNK 100
struct demangle_info {
int used;
struct demangle_info *next;
struct demangle_component comps[ALLOC_CHUNK];
};
static struct demangle_info *demangle_info;
static struct demangle_component *
d_grab (void)
{
struct demangle_info *more;
if (demangle_info->used >= ALLOC_CHUNK)
{
if (demangle_info->next == NULL)
{
more = malloc (sizeof (struct demangle_info));
more->next = NULL;
demangle_info->next = more;
}
else
more = demangle_info->next;
more->used = 0;
demangle_info = more;
}
return &demangle_info->comps[demangle_info->used++];
}
/* The parse tree created by the parser is stored here after a successful
parse. */
static struct demangle_component *global_result;
/* Prototypes for helper functions used when constructing the parse
tree. */
static struct demangle_component *d_qualify (struct demangle_component *, int,
int);
static struct demangle_component *d_int_type (int);
static struct demangle_component *d_unary (const char *,
struct demangle_component *);
static struct demangle_component *d_binary (const char *,
struct demangle_component *,
struct demangle_component *);
/* Flags passed to d_qualify. */
#define QUAL_CONST 1
#define QUAL_RESTRICT 2
#define QUAL_VOLATILE 4
/* Flags passed to d_int_type. */
#define INT_CHAR (1 << 0)
#define INT_SHORT (1 << 1)
#define INT_LONG (1 << 2)
#define INT_LLONG (1 << 3)
#define INT_SIGNED (1 << 4)
#define INT_UNSIGNED (1 << 5)
/* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
as well as gratuitiously global symbol names, so we can have multiple
yacc generated parsers in gdb. Note that these are only the variables
produced by yacc. If other parser generators (bison, byacc, etc) produce
additional global names that conflict at link time, then those parser
generators need to be fixed instead of adding those names to this list. */
#define yymaxdepth cpname_maxdepth
#define yyparse cpname_parse
#define yylex cpname_lex
#define yyerror cpname_error
#define yylval cpname_lval
#define yychar cpname_char
#define yydebug cpname_debug
#define yypact cpname_pact
#define yyr1 cpname_r1
#define yyr2 cpname_r2
#define yydef cpname_def
#define yychk cpname_chk
#define yypgo cpname_pgo
#define yyact cpname_act
#define yyexca cpname_exca
#define yyerrflag cpname_errflag
#define yynerrs cpname_nerrs
#define yyps cpname_ps
#define yypv cpname_pv
#define yys cpname_s
#define yy_yys cpname_yys
#define yystate cpname_state
#define yytmp cpname_tmp
#define yyv cpname_v
#define yy_yyv cpname_yyv
#define yyval cpname_val
#define yylloc cpname_lloc
#define yyreds cpname_reds /* With YYDEBUG defined */
#define yytoks cpname_toks /* With YYDEBUG defined */
#define yyname cpname_name /* With YYDEBUG defined */
#define yyrule cpname_rule /* With YYDEBUG defined */
#define yylhs cpname_yylhs
#define yylen cpname_yylen
#define yydefred cpname_yydefred
#define yydgoto cpname_yydgoto
#define yysindex cpname_yysindex
#define yyrindex cpname_yyrindex
#define yygindex cpname_yygindex
#define yytable cpname_yytable
#define yycheck cpname_yycheck
#define yyss cpname_yyss
#define yysslim cpname_yysslim
#define yyssp cpname_yyssp
#define yystacksize cpname_yystacksize
#define yyvs cpname_yyvs
#define yyvsp cpname_yyvsp
int yyparse (void);
static int yylex (void);
static void yyerror (char *);
/* Enable yydebug for the stand-alone parser. */
#ifdef TEST_CPNAMES
# define YYDEBUG 1
#endif
/* Helper functions. These wrap the demangler tree interface, handle
allocation from our global store, and return the allocated component. */
static struct demangle_component *
fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
struct demangle_component *rhs)
{
struct demangle_component *ret = d_grab ();
int i;
i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
gdb_assert (i);
return ret;
}
static struct demangle_component *
make_empty (enum demangle_component_type d_type)
{
struct demangle_component *ret = d_grab ();
ret->type = d_type;
return ret;
}
static struct demangle_component *
make_operator (const char *name, int args)
{
struct demangle_component *ret = d_grab ();
int i;
i = cplus_demangle_fill_operator (ret, name, args);
gdb_assert (i);
return ret;
}
static struct demangle_component *
make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
{
struct demangle_component *ret = d_grab ();
int i;
i = cplus_demangle_fill_dtor (ret, kind, name);
gdb_assert (i);
return ret;
}
static struct demangle_component *
make_builtin_type (const char *name)
{
struct demangle_component *ret = d_grab ();
int i;
i = cplus_demangle_fill_builtin_type (ret, name);
gdb_assert (i);
return ret;
}
static struct demangle_component *
make_name (const char *name, int len)
{
struct demangle_component *ret = d_grab ();
int i;
i = cplus_demangle_fill_name (ret, name, len);
gdb_assert (i);
return ret;
}
#define d_left(dc) (dc)->u.s_binary.left
#define d_right(dc) (dc)->u.s_binary.right
%}
%union
{
struct demangle_component *comp;
struct nested {
struct demangle_component *comp;
struct demangle_component **last;
} nested;
struct {
struct demangle_component *comp, *last;
} nested1;
struct {
struct demangle_component *comp, **last;
struct nested fn;
struct demangle_component *start;
int fold_flag;
} abstract;
int lval;
const char *opname;
}
%type <comp> exp exp1 type start start_opt oper colon_name
%type <comp> unqualified_name colon_ext_name
%type <comp> templ template_arg
%type <comp> builtin_type
%type <comp> typespec_2 array_indicator
%type <comp> colon_ext_only ext_only_name
%type <comp> demangler_special function conversion_op
%type <nested> conversion_op_name
%type <abstract> abstract_declarator direct_abstract_declarator
%type <abstract> abstract_declarator_fn
%type <nested> declarator direct_declarator function_arglist
%type <nested> declarator_1 direct_declarator_1
%type <nested> template_params function_args
%type <nested> ptr_operator
%type <nested1> nested_name
%type <lval> qualifier qualifiers qualifiers_opt
%type <lval> int_part int_seq
%token <comp> INT
%token <comp> FLOAT
%token <comp> NAME
%type <comp> name
%token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
%token TEMPLATE
%token ERROR
%token NEW DELETE OPERATOR
%token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST
/* Special type cases, put in to allow the parser to distinguish different
legal basetypes. */
%token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
%token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T
%token <opname> ASSIGN_MODIFY
/* C++ */
%token TRUEKEYWORD
%token FALSEKEYWORD
/* Non-C++ things we get from the demangler. */
%token <lval> DEMANGLER_SPECIAL
%token CONSTRUCTION_VTABLE CONSTRUCTION_IN
/* Precedence declarations. */
/* Give NAME lower precedence than COLONCOLON, so that nested_name will
associate greedily. */
%nonassoc NAME
/* Give NEW and DELETE lower precedence than ']', because we can not
have an array of type operator new. This causes NEW '[' to be
parsed as operator new[]. */
%nonassoc NEW DELETE
/* Give VOID higher precedence than NAME. Then we can use %prec NAME
to prefer (VOID) to (function_args). */
%nonassoc VOID
/* Give VOID lower precedence than ')' for similar reasons. */
%nonassoc ')'
%left ','
%right '=' ASSIGN_MODIFY
%right '?'
%left OROR
%left ANDAND
%left '|'
%left '^'
%left '&'
%left EQUAL NOTEQUAL
%left '<' '>' LEQ GEQ
%left LSH RSH
%left '@'
%left '+' '-'
%left '*' '/' '%'
%right UNARY INCREMENT DECREMENT
/* We don't need a precedence for '(' in this reduced grammar, and it
can mask some unpleasant bugs, so disable it for now. */
%right ARROW '.' '[' /* '(' */
%left COLONCOLON
%%
result : start
{ global_result = $1; }
;
start : type
| demangler_special
| function
;
start_opt : /* */
{ $$ = NULL; }
| COLONCOLON start
{ $$ = $2; }
;
function
/* Function with a return type. declarator_1 is used to prevent
ambiguity with the next rule. */
: typespec_2 declarator_1
{ $$ = $2.comp;
*$2.last = $1;
}
/* Function without a return type. We need to use typespec_2
to prevent conflicts from qualifiers_opt - harmless. The
start_opt is used to handle "function-local" variables and
types. */
| typespec_2 function_arglist start_opt
{ $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
| colon_ext_only function_arglist start_opt
{ $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
| conversion_op_name start_opt
{ $$ = $1.comp;
if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
| conversion_op_name abstract_declarator_fn
{ if ($2.last)
{
/* First complete the abstract_declarator's type using
the typespec from the conversion_op_name. */
*$2.last = *$1.last;
/* Then complete the conversion_op_name with the type. */
*$1.last = $2.comp;
}
/* If we have an arglist, build a function type. */
if ($2.fn.comp)
$$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
else
$$ = $1.comp;
if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
}
;
demangler_special
: DEMANGLER_SPECIAL start
{ $$ = make_empty ((enum demangle_component_type) $1);
d_left ($$) = $2;
d_right ($$) = NULL; }
| CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
{ $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
;
oper : OPERATOR NEW
{
/* Match the whitespacing of cplus_demangle_operators.
It would abort on unrecognized string otherwise. */
$$ = make_operator ("new", 3);
}
| OPERATOR DELETE
{
/* Match the whitespacing of cplus_demangle_operators.
It would abort on unrecognized string otherwise. */
$$ = make_operator ("delete ", 1);
}
| OPERATOR NEW '[' ']'
{
/* Match the whitespacing of cplus_demangle_operators.
It would abort on unrecognized string otherwise. */
$$ = make_operator ("new[]", 3);
}
| OPERATOR DELETE '[' ']'
{
/* Match the whitespacing of cplus_demangle_operators.
It would abort on unrecognized string otherwise. */
$$ = make_operator ("delete[] ", 1);
}
| OPERATOR '+'
{ $$ = make_operator ("+", 2); }
| OPERATOR '-'
{ $$ = make_operator ("-", 2); }
| OPERATOR '*'
{ $$ = make_operator ("*", 2); }
| OPERATOR '/'
{ $$ = make_operator ("/", 2); }
| OPERATOR '%'
{ $$ = make_operator ("%", 2); }
| OPERATOR '^'
{ $$ = make_operator ("^", 2); }
| OPERATOR '&'
{ $$ = make_operator ("&", 2); }
| OPERATOR '|'
{ $$ = make_operator ("|", 2); }
| OPERATOR '~'
{ $$ = make_operator ("~", 1); }
| OPERATOR '!'
{ $$ = make_operator ("!", 1); }
| OPERATOR '='
{ $$ = make_operator ("=", 2); }
| OPERATOR '<'
{ $$ = make_operator ("<", 2); }
| OPERATOR '>'
{ $$ = make_operator (">", 2); }
| OPERATOR ASSIGN_MODIFY
{ $$ = make_operator ($2, 2); }
| OPERATOR LSH
{ $$ = make_operator ("<<", 2); }
| OPERATOR RSH
{ $$ = make_operator (">>", 2); }
| OPERATOR EQUAL
{ $$ = make_operator ("==", 2); }
| OPERATOR NOTEQUAL
{ $$ = make_operator ("!=", 2); }
| OPERATOR LEQ
{ $$ = make_operator ("<=", 2); }
| OPERATOR GEQ
{ $$ = make_operator (">=", 2); }
| OPERATOR ANDAND
{ $$ = make_operator ("&&", 2); }
| OPERATOR OROR
{ $$ = make_operator ("||", 2); }
| OPERATOR INCREMENT
{ $$ = make_operator ("++", 1); }
| OPERATOR DECREMENT
{ $$ = make_operator ("--", 1); }
| OPERATOR ','
{ $$ = make_operator (",", 2); }
| OPERATOR ARROW '*'
{ $$ = make_operator ("->*", 2); }
| OPERATOR ARROW
{ $$ = make_operator ("->", 2); }
| OPERATOR '(' ')'
{ $$ = make_operator ("()", 2); }
| OPERATOR '[' ']'
{ $$ = make_operator ("[]", 2); }
;
/* Conversion operators. We don't try to handle some of
the wackier demangler output for function pointers,
since it's not clear that it's parseable. */
conversion_op
: OPERATOR typespec_2
{ $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
;
conversion_op_name
: nested_name conversion_op
{ $$.comp = $1.comp;
d_right ($1.last) = $2;
$$.last = &d_left ($2);
}
| conversion_op
{ $$.comp = $1;
$$.last = &d_left ($1);
}
| COLONCOLON nested_name conversion_op
{ $$.comp = $2.comp;
d_right ($2.last) = $3;
$$.last = &d_left ($3);
}
| COLONCOLON conversion_op
{ $$.comp = $2;
$$.last = &d_left ($2);
}
;
/* DEMANGLE_COMPONENT_NAME */
/* This accepts certain invalid placements of '~'. */
unqualified_name: oper
| oper '<' template_params '>'
{ $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
| '~' NAME
{ $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
;
/* This rule is used in name and nested_name, and expanded inline there
for efficiency. */
/*
scope_id : NAME
| template
;
*/
colon_name : name
| COLONCOLON name
{ $$ = $2; }
;
/* DEMANGLE_COMPONENT_QUAL_NAME */
/* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
name : nested_name NAME %prec NAME
{ $$ = $1.comp; d_right ($1.last) = $2; }
| NAME %prec NAME
| nested_name templ %prec NAME
{ $$ = $1.comp; d_right ($1.last) = $2; }
| templ %prec NAME
;
colon_ext_name : colon_name
| colon_ext_only
;
colon_ext_only : ext_only_name
| COLONCOLON ext_only_name
{ $$ = $2; }
;
ext_only_name : nested_name unqualified_name
{ $$ = $1.comp; d_right ($1.last) = $2; }
| unqualified_name
;
nested_name : NAME COLONCOLON
{ $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
d_left ($$.comp) = $1;
d_right ($$.comp) = NULL;
$$.last = $$.comp;
}
| nested_name NAME COLONCOLON
{ $$.comp = $1.comp;
d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
$$.last = d_right ($1.last);
d_left ($$.last) = $2;
d_right ($$.last) = NULL;
}
| templ COLONCOLON
{ $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
d_left ($$.comp) = $1;
d_right ($$.comp) = NULL;
$$.last = $$.comp;
}
| nested_name templ COLONCOLON
{ $$.comp = $1.comp;
d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
$$.last = d_right ($1.last);
d_left ($$.last) = $2;
d_right ($$.last) = NULL;
}
;
/* DEMANGLE_COMPONENT_TEMPLATE */
/* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
templ : NAME '<' template_params '>'
{ $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
;
template_params : template_arg
{ $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
$$.last = &d_right ($$.comp); }
| template_params ',' template_arg
{ $$.comp = $1.comp;
*$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
$$.last = &d_right (*$1.last);
}
;
/* "type" is inlined into template_arg and function_args. */
/* Also an integral constant-expression of integral type, and a
pointer to member (?) */
template_arg : typespec_2
| typespec_2 abstract_declarator
{ $$ = $2.comp;
*$2.last = $1;
}
| '&' start
{ $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
| '&' '(' start ')'
{ $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
| exp
;
function_args : typespec_2
{ $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
$$.last = &d_right ($$.comp);
}
| typespec_2 abstract_declarator
{ *$2.last = $1;
$$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
$$.last = &d_right ($$.comp);
}
| function_args ',' typespec_2
{ *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
$$.comp = $1.comp;
$$.last = &d_right (*$1.last);
}
| function_args ',' typespec_2 abstract_declarator
{ *$4.last = $3;
*$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
$$.comp = $1.comp;
$$.last = &d_right (*$1.last);
}
| function_args ',' ELLIPSIS
{ *$1.last
= fill_comp (DEMANGLE_COMPONENT_ARGLIST,
make_builtin_type ("..."),
NULL);
$$.comp = $1.comp;
$$.last = &d_right (*$1.last);
}
;
function_arglist: '(' function_args ')' qualifiers_opt %prec NAME
{ $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
$$.last = &d_left ($$.comp);
$$.comp = d_qualify ($$.comp, $4, 1); }
| '(' VOID ')' qualifiers_opt
{ $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
$$.last = &d_left ($$.comp);
$$.comp = d_qualify ($$.comp, $4, 1); }
| '(' ')' qualifiers_opt
{ $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
$$.last = &d_left ($$.comp);
$$.comp = d_qualify ($$.comp, $3, 1); }
;
/* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
qualifiers_opt : /* epsilon */
{ $$ = 0; }
| qualifiers
;
qualifier : RESTRICT
{ $$ = QUAL_RESTRICT; }
| VOLATILE_KEYWORD
{ $$ = QUAL_VOLATILE; }
| CONST_KEYWORD
{ $$ = QUAL_CONST; }
;
qualifiers : qualifier
| qualifier qualifiers
{ $$ = $1 | $2; }
;
/* This accepts all sorts of invalid constructions and produces
invalid output for them - an error would be better. */
int_part : INT_KEYWORD
{ $$ = 0; }
| SIGNED_KEYWORD
{ $$ = INT_SIGNED; }
| UNSIGNED
{ $$ = INT_UNSIGNED; }
| CHAR
{ $$ = INT_CHAR; }
| LONG
{ $$ = INT_LONG; }
| SHORT
{ $$ = INT_SHORT; }
;
int_seq : int_part
| int_seq int_part
{ $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
;
builtin_type : int_seq
{ $$ = d_int_type ($1); }
| FLOAT_KEYWORD
{ $$ = make_builtin_type ("float"); }
| DOUBLE_KEYWORD
{ $$ = make_builtin_type ("double"); }
| LONG DOUBLE_KEYWORD
{ $$ = make_builtin_type ("long double"); }
| BOOL
{ $$ = make_builtin_type ("bool"); }
| WCHAR_T
{ $$ = make_builtin_type ("wchar_t"); }
| VOID
{ $$ = make_builtin_type ("void"); }
;
ptr_operator : '*' qualifiers_opt
{ $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
$$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
$$.last = &d_left ($$.comp);
$$.comp = d_qualify ($$.comp, $2, 0); }
/* g++ seems to allow qualifiers after the reference? */
| '&'
{ $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
$$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
$$.last = &d_left ($$.comp); }
| nested_name '*' qualifiers_opt
{ $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
$$.comp->u.s_binary.left = $1.comp;
/* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
*$1.last = *d_left ($1.last);
$$.comp->u.s_binary.right = NULL;
$$.last = &d_right ($$.comp);
$$.comp = d_qualify ($$.comp, $3, 0); }
| COLONCOLON nested_name '*' qualifiers_opt
{ $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
$$.comp->u.s_binary.left = $2.comp;
/* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME. */
*$2.last = *d_left ($2.last);
$$.comp->u.s_binary.right = NULL;
$$.last = &d_right ($$.comp);
$$.comp = d_qualify ($$.comp, $4, 0); }
;
array_indicator : '[' ']'
{ $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
d_left ($$) = NULL;
}
| '[' INT ']'
{ $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
d_left ($$) = $2;
}
;
/* Details of this approach inspired by the G++ < 3.4 parser. */
/* This rule is only used in typespec_2, and expanded inline there for
efficiency. */
/*
typespec : builtin_type
| colon_name
;
*/
typespec_2 : builtin_type qualifiers
{ $$ = d_qualify ($1, $2, 0); }
| builtin_type
| qualifiers builtin_type qualifiers
{ $$ = d_qualify ($2, $1 | $3, 0); }
| qualifiers builtin_type
{ $$ = d_qualify ($2, $1, 0); }
| name qualifiers
{ $$ = d_qualify ($1, $2, 0); }
| name
| qualifiers name qualifiers
{ $$ = d_qualify ($2, $1 | $3, 0); }
| qualifiers name
{ $$ = d_qualify ($2, $1, 0); }
| COLONCOLON name qualifiers
{ $$ = d_qualify ($2, $3, 0); }
| COLONCOLON name
{ $$ = $2; }
| qualifiers COLONCOLON name qualifiers
{ $$ = d_qualify ($3, $1 | $4, 0); }
| qualifiers COLONCOLON name
{ $$ = d_qualify ($3, $1, 0); }
;
abstract_declarator
: ptr_operator
{ $$.comp = $1.comp; $$.last = $1.last;
$$.fn.comp = NULL; $$.fn.last = NULL; }
| ptr_operator abstract_declarator
{ $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
*$$.last = $1.comp;
$$.last = $1.last; }
| direct_abstract_declarator
{ $$.fn.comp = NULL; $$.fn.last = NULL;
if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
}
;
direct_abstract_declarator
: '(' abstract_declarator ')'
{ $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
}
| direct_abstract_declarator function_arglist
{ $$.fold_flag = 0;
if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
if ($1.fold_flag)
{
*$$.last = $2.comp;
$$.last = $2.last;
}
else
$$.fn = $2;
}
| direct_abstract_declarator array_indicator
{ $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
*$1.last = $2;
$$.last = &d_right ($2);
}
| array_indicator
{ $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
$$.comp = $1;
$$.last = &d_right ($1);
}
/* G++ has the following except for () and (type). Then
(type) is handled in regcast_or_absdcl and () is handled
in fcast_or_absdcl.
However, this is only useful for function types, and
generates reduce/reduce conflicts with direct_declarator.
We're interested in pointer-to-function types, and in
functions, but not in function types - so leave this
out. */
/* | function_arglist */
;
abstract_declarator_fn
: ptr_operator
{ $$.comp = $1.comp; $$.last = $1.last;
$$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
| ptr_operator abstract_declarator_fn
{ $$ = $2;
if ($2.last)
*$$.last = $1.comp;
else
$$.comp = $1.comp;
$$.last = $1.last;
}
| direct_abstract_declarator
{ $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
| direct_abstract_declarator function_arglist COLONCOLON start
{ $$.start = $4;
if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
if ($1.fold_flag)
{
*$$.last = $2.comp;
$$.last = $2.last;
}
else
$$.fn = $2;
}
| function_arglist start_opt
{ $$.fn = $1;
$$.start = $2;
$$.comp = NULL; $$.last = NULL;
}
;
type : typespec_2
| typespec_2 abstract_declarator
{ $$ = $2.comp;
*$2.last = $1;
}
;
declarator : ptr_operator declarator
{ $$.comp = $2.comp;
$$.last = $1.last;
*$2.last = $1.comp; }
| direct_declarator
;
direct_declarator
: '(' declarator ')'
{ $$ = $2; }
| direct_declarator function_arglist
{ $$.comp = $1.comp;
*$1.last = $2.comp;
$$.last = $2.last;
}
| direct_declarator array_indicator
{ $$.comp = $1.comp;
*$1.last = $2;
$$.last = &d_right ($2);
}
| colon_ext_name
{ $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
d_left ($$.comp) = $1;
$$.last = &d_right ($$.comp);
}
;
/* These are similar to declarator and direct_declarator except that they
do not permit ( colon_ext_name ), which is ambiguous with a function
argument list. They also don't permit a few other forms with redundant
parentheses around the colon_ext_name; any colon_ext_name in parentheses
must be followed by an argument list or an array indicator, or preceded
by a pointer. */
declarator_1 : ptr_operator declarator_1
{ $$.comp = $2.comp;
$$.last = $1.last;
*$2.last = $1.comp; }
| colon_ext_name
{ $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
d_left ($$.comp) = $1;
$$.last = &d_right ($$.comp);
}
| direct_declarator_1
/* Function local variable or type. The typespec to
our left is the type of the containing function.
This should be OK, because function local types
can not be templates, so the return types of their
members will not be mangled. If they are hopefully
they'll end up to the right of the ::. */
| colon_ext_name function_arglist COLONCOLON start
{ $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
$$.last = $2.last;
$$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
}
| direct_declarator_1 function_arglist COLONCOLON start
{ $$.comp = $1.comp;
*$1.last = $2.comp;
$$.last = $2.last;
$$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
}
;
direct_declarator_1
: '(' ptr_operator declarator ')'
{ $$.comp = $3.comp;
$$.last = $2.last;
*$3.last = $2.comp; }
| direct_declarator_1 function_arglist
{ $$.comp = $1.comp;
*$1.last = $2.comp;
$$.last = $2.last;