gdb/cp-name-parser.y - gdb

  1. /* YACC parser for C++ names, for GDB.

  2.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  3.    Parts of the lexer are based on c-exp.y from GDB.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. /* Note that malloc's and realloc's in this file are transformed to
  16.    xmalloc and xrealloc respectively by the same sed command in the
  17.    makefile that remaps any other malloc/realloc inserted by the parser
  18.    generator.  Doing this with #defines and trying to control the interaction
  19.    with include files (<malloc.h> and <stdlib.h> for example) just became
  20.    too messy, particularly when such includes can be inserted at random
  21.    times by the parser generator.  */

  22. %{

  23. #include "defs.h"

  24. #include <unistd.h>
  25. #include "safe-ctype.h"
  26. #include "demangle.h"
  27. #include "cp-support.h"

  28. /* Bison does not make it easy to create a parser without global
  29.    state, unfortunately.  Here are all the global variables used
  30.    in this parser.  */

  31. /* LEXPTR is the current pointer into our lex buffer.  PREV_LEXPTR
  32.    is the start of the last token lexed, only used for diagnostics.
  33.    ERROR_LEXPTR is the first place an error occurred.  GLOBAL_ERRMSG
  34.    is the first error message encountered.  */

  35. static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;

  36. /* The components built by the parser are allocated ahead of time,
  37.    and cached in this structure.  */

  38. #define ALLOC_CHUNK 100

  39. struct demangle_info {
  40.   int used;
  41.   struct demangle_info *next;
  42.   struct demangle_component comps[ALLOC_CHUNK];
  43. };

  44. static struct demangle_info *demangle_info;

  45. static struct demangle_component *
  46. d_grab (void)
  47. {
  48.   struct demangle_info *more;

  49.   if (demangle_info->used >= ALLOC_CHUNK)
  50.     {
  51.       if (demangle_info->next == NULL)
  52.         {
  53.           more = malloc (sizeof (struct demangle_info));
  54.           more->next = NULL;
  55.           demangle_info->next = more;
  56.         }
  57.       else
  58.         more = demangle_info->next;

  59.       more->used = 0;
  60.       demangle_info = more;
  61.     }
  62.   return &demangle_info->comps[demangle_info->used++];
  63. }

  64. /* The parse tree created by the parser is stored here after a successful
  65.    parse.  */

  66. static struct demangle_component *global_result;

  67. /* Prototypes for helper functions used when constructing the parse
  68.    tree.  */

  69. static struct demangle_component *d_qualify (struct demangle_component *, int,
  70.                                              int);

  71. static struct demangle_component *d_int_type (int);

  72. static struct demangle_component *d_unary (const char *,
  73.                                            struct demangle_component *);
  74. static struct demangle_component *d_binary (const char *,
  75.                                             struct demangle_component *,
  76.                                             struct demangle_component *);

  77. /* Flags passed to d_qualify.  */

  78. #define QUAL_CONST 1
  79. #define QUAL_RESTRICT 2
  80. #define QUAL_VOLATILE 4

  81. /* Flags passed to d_int_type.  */

  82. #define INT_CHAR        (1 << 0)
  83. #define INT_SHORT        (1 << 1)
  84. #define INT_LONG        (1 << 2)
  85. #define INT_LLONG        (1 << 3)

  86. #define INT_SIGNED        (1 << 4)
  87. #define INT_UNSIGNED        (1 << 5)

  88. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  89.    as well as gratuitiously global symbol names, so we can have multiple
  90.    yacc generated parsers in gdb.  Note that these are only the variables
  91.    produced by yacc.  If other parser generators (bison, byacc, etc) produce
  92.    additional global names that conflict at link time, then those parser
  93.    generators need to be fixed instead of adding those names to this list. */

  94. #define        yymaxdepth cpname_maxdepth
  95. #define        yyparse        cpname_parse
  96. #define        yylex        cpname_lex
  97. #define        yyerror        cpname_error
  98. #define        yylval        cpname_lval
  99. #define        yychar        cpname_char
  100. #define        yydebug        cpname_debug
  101. #define        yypact        cpname_pact
  102. #define        yyr1        cpname_r1
  103. #define        yyr2        cpname_r2
  104. #define        yydef        cpname_def
  105. #define        yychk        cpname_chk
  106. #define        yypgo        cpname_pgo
  107. #define        yyact        cpname_act
  108. #define        yyexca        cpname_exca
  109. #define yyerrflag cpname_errflag
  110. #define yynerrs        cpname_nerrs
  111. #define        yyps        cpname_ps
  112. #define        yypv        cpname_pv
  113. #define        yys        cpname_s
  114. #define        yy_yys        cpname_yys
  115. #define        yystate        cpname_state
  116. #define        yytmp        cpname_tmp
  117. #define        yyv        cpname_v
  118. #define        yy_yyv        cpname_yyv
  119. #define        yyval        cpname_val
  120. #define        yylloc        cpname_lloc
  121. #define yyreds        cpname_reds                /* With YYDEBUG defined */
  122. #define yytoks        cpname_toks                /* With YYDEBUG defined */
  123. #define yyname        cpname_name                /* With YYDEBUG defined */
  124. #define yyrule        cpname_rule                /* With YYDEBUG defined */
  125. #define yylhs        cpname_yylhs
  126. #define yylen        cpname_yylen
  127. #define yydefred cpname_yydefred
  128. #define yydgoto        cpname_yydgoto
  129. #define yysindex cpname_yysindex
  130. #define yyrindex cpname_yyrindex
  131. #define yygindex cpname_yygindex
  132. #define yytable         cpname_yytable
  133. #define yycheck         cpname_yycheck
  134. #define yyss        cpname_yyss
  135. #define yysslim        cpname_yysslim
  136. #define yyssp        cpname_yyssp
  137. #define yystacksize cpname_yystacksize
  138. #define yyvs        cpname_yyvs
  139. #define yyvsp        cpname_yyvsp

  140. int yyparse (void);
  141. static int yylex (void);
  142. static void yyerror (char *);

  143. /* Enable yydebug for the stand-alone parser.  */
  144. #ifdef TEST_CPNAMES
  145. # define YYDEBUG        1
  146. #endif

  147. /* Helper functions.  These wrap the demangler tree interface, handle
  148.    allocation from our global store, and return the allocated component.  */

  149. static struct demangle_component *
  150. fill_comp (enum demangle_component_type d_type, struct demangle_component *lhs,
  151.            struct demangle_component *rhs)
  152. {
  153.   struct demangle_component *ret = d_grab ();
  154.   int i;

  155.   i = cplus_demangle_fill_component (ret, d_type, lhs, rhs);
  156.   gdb_assert (i);

  157.   return ret;
  158. }

  159. static struct demangle_component *
  160. make_empty (enum demangle_component_type d_type)
  161. {
  162.   struct demangle_component *ret = d_grab ();
  163.   ret->type = d_type;
  164.   return ret;
  165. }

  166. static struct demangle_component *
  167. make_operator (const char *name, int args)
  168. {
  169.   struct demangle_component *ret = d_grab ();
  170.   int i;

  171.   i = cplus_demangle_fill_operator (ret, name, args);
  172.   gdb_assert (i);

  173.   return ret;
  174. }

  175. static struct demangle_component *
  176. make_dtor (enum gnu_v3_dtor_kinds kind, struct demangle_component *name)
  177. {
  178.   struct demangle_component *ret = d_grab ();
  179.   int i;

  180.   i = cplus_demangle_fill_dtor (ret, kind, name);
  181.   gdb_assert (i);

  182.   return ret;
  183. }

  184. static struct demangle_component *
  185. make_builtin_type (const char *name)
  186. {
  187.   struct demangle_component *ret = d_grab ();
  188.   int i;

  189.   i = cplus_demangle_fill_builtin_type (ret, name);
  190.   gdb_assert (i);

  191.   return ret;
  192. }

  193. static struct demangle_component *
  194. make_name (const char *name, int len)
  195. {
  196.   struct demangle_component *ret = d_grab ();
  197.   int i;

  198.   i = cplus_demangle_fill_name (ret, name, len);
  199.   gdb_assert (i);

  200.   return ret;
  201. }

  202. #define d_left(dc) (dc)->u.s_binary.left
  203. #define d_right(dc) (dc)->u.s_binary.right

  204. %}

  205. %union
  206.   {
  207.     struct demangle_component *comp;
  208.     struct nested {
  209.       struct demangle_component *comp;
  210.       struct demangle_component **last;
  211.     } nested;
  212.     struct {
  213.       struct demangle_component *comp, *last;
  214.     } nested1;
  215.     struct {
  216.       struct demangle_component *comp, **last;
  217.       struct nested fn;
  218.       struct demangle_component *start;
  219.       int fold_flag;
  220.     } abstract;
  221.     int lval;
  222.     const char *opname;
  223.   }

  224. %type <comp> exp exp1 type start start_opt operator colon_name
  225. %type <comp> unqualified_name colon_ext_name
  226. %type <comp> template template_arg
  227. %type <comp> builtin_type
  228. %type <comp> typespec_2 array_indicator
  229. %type <comp> colon_ext_only ext_only_name

  230. %type <comp> demangler_special function conversion_op
  231. %type <nested> conversion_op_name

  232. %type <abstract> abstract_declarator direct_abstract_declarator
  233. %type <abstract> abstract_declarator_fn
  234. %type <nested> declarator direct_declarator function_arglist

  235. %type <nested> declarator_1 direct_declarator_1

  236. %type <nested> template_params function_args
  237. %type <nested> ptr_operator

  238. %type <nested1> nested_name

  239. %type <lval> qualifier qualifiers qualifiers_opt

  240. %type <lval> int_part int_seq

  241. %token <comp> INT
  242. %token <comp> FLOAT

  243. %token <comp> NAME
  244. %type <comp> name

  245. %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
  246. %token TEMPLATE
  247. %token ERROR
  248. %token NEW DELETE OPERATOR
  249. %token STATIC_CAST REINTERPRET_CAST DYNAMIC_CAST

  250. /* Special type cases, put in to allow the parser to distinguish different
  251.    legal basetypes.  */
  252. %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD BOOL
  253. %token ELLIPSIS RESTRICT VOID FLOAT_KEYWORD CHAR WCHAR_T

  254. %token <opname> ASSIGN_MODIFY

  255. /* C++ */
  256. %token TRUEKEYWORD
  257. %token FALSEKEYWORD

  258. /* Non-C++ things we get from the demangler.  */
  259. %token <lval> DEMANGLER_SPECIAL
  260. %token CONSTRUCTION_VTABLE CONSTRUCTION_IN

  261. /* Precedence declarations.  */

  262. /* Give NAME lower precedence than COLONCOLON, so that nested_name will
  263.    associate greedily.  */
  264. %nonassoc NAME

  265. /* Give NEW and DELETE lower precedence than ']', because we can not
  266.    have an array of type operator new.  This causes NEW '[' to be
  267.    parsed as operator new[].  */
  268. %nonassoc NEW DELETE

  269. /* Give VOID higher precedence than NAME.  Then we can use %prec NAME
  270.    to prefer (VOID) to (function_args).  */
  271. %nonassoc VOID

  272. /* Give VOID lower precedence than ')' for similar reasons.  */
  273. %nonassoc ')'

  274. %left ','
  275. %right '=' ASSIGN_MODIFY
  276. %right '?'
  277. %left OROR
  278. %left ANDAND
  279. %left '|'
  280. %left '^'
  281. %left '&'
  282. %left EQUAL NOTEQUAL
  283. %left '<' '>' LEQ GEQ
  284. %left LSH RSH
  285. %left '@'
  286. %left '+' '-'
  287. %left '*' '/' '%'
  288. %right UNARY INCREMENT DECREMENT

  289. /* We don't need a precedence for '(' in this reduced grammar, and it
  290.    can mask some unpleasant bugs, so disable it for now.  */

  291. %right ARROW '.' '[' /* '(' */
  292. %left COLONCOLON


  293. %%

  294. result                :        start
  295.                         { global_result = $1; }
  296.                 ;

  297. start                :        type

  298.                 |        demangler_special

  299.                 |        function

  300.                 ;

  301. start_opt        :        /* */
  302.                         { $$ = NULL; }
  303.                 |        COLONCOLON start
  304.                         { $$ = $2; }
  305.                 ;

  306. function
  307.                 /* Function with a return type.  declarator_1 is used to prevent
  308.                    ambiguity with the next rule.  */
  309.                 :        typespec_2 declarator_1
  310.                         { $$ = $2.comp;
  311.                           *$2.last = $1;
  312.                         }

  313.                 /* Function without a return type.  We need to use typespec_2
  314.                    to prevent conflicts from qualifiers_opt - harmless.  The
  315.                    start_opt is used to handle "function-local" variables and
  316.                    types.  */
  317.                 |        typespec_2 function_arglist start_opt
  318.                         { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
  319.                           if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }
  320.                 |        colon_ext_only function_arglist start_opt
  321.                         { $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
  322.                           if ($3) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $3); }

  323.                 |        conversion_op_name start_opt
  324.                         { $$ = $1.comp;
  325.                           if ($2) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2); }
  326.                 |        conversion_op_name abstract_declarator_fn
  327.                         { if ($2.last)
  328.                             {
  329.                                /* First complete the abstract_declarator's type using
  330.                                   the typespec from the conversion_op_name.  */
  331.                               *$2.last = *$1.last;
  332.                               /* Then complete the conversion_op_name with the type.  */
  333.                               *$1.last = $2.comp;
  334.                             }
  335.                           /* If we have an arglist, build a function type.  */
  336.                           if ($2.fn.comp)
  337.                             $$ = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1.comp, $2.fn.comp);
  338.                           else
  339.                             $$ = $1.comp;
  340.                           if ($2.start) $$ = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$, $2.start);
  341.                         }
  342.                 ;

  343. demangler_special
  344.                 :        DEMANGLER_SPECIAL start
  345.                         { $$ = make_empty ($1);
  346.                           d_left ($$) = $2;
  347.                           d_right ($$) = NULL; }
  348.                 |        CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
  349.                         { $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
  350.                 ;

  351. operator        :        OPERATOR NEW
  352.                         {
  353.                           /* Match the whitespacing of cplus_demangle_operators.
  354.                              It would abort on unrecognized string otherwise.  */
  355.                           $$ = make_operator ("new", 3);
  356.                         }
  357.                 |        OPERATOR DELETE
  358.                         {
  359.                           /* Match the whitespacing of cplus_demangle_operators.
  360.                              It would abort on unrecognized string otherwise.  */
  361.                           $$ = make_operator ("delete ", 1);
  362.                         }
  363.                 |        OPERATOR NEW '[' ']'
  364.                         {
  365.                           /* Match the whitespacing of cplus_demangle_operators.
  366.                              It would abort on unrecognized string otherwise.  */
  367.                           $$ = make_operator ("new[]", 3);
  368.                         }
  369.                 |        OPERATOR DELETE '[' ']'
  370.                         {
  371.                           /* Match the whitespacing of cplus_demangle_operators.
  372.                              It would abort on unrecognized string otherwise.  */
  373.                           $$ = make_operator ("delete[] ", 1);
  374.                         }
  375.                 |        OPERATOR '+'
  376.                         { $$ = make_operator ("+", 2); }
  377.                 |        OPERATOR '-'
  378.                         { $$ = make_operator ("-", 2); }
  379.                 |        OPERATOR '*'
  380.                         { $$ = make_operator ("*", 2); }
  381.                 |        OPERATOR '/'
  382.                         { $$ = make_operator ("/", 2); }
  383.                 |        OPERATOR '%'
  384.                         { $$ = make_operator ("%", 2); }
  385.                 |        OPERATOR '^'
  386.                         { $$ = make_operator ("^", 2); }
  387.                 |        OPERATOR '&'
  388.                         { $$ = make_operator ("&", 2); }
  389.                 |        OPERATOR '|'
  390.                         { $$ = make_operator ("|", 2); }
  391.                 |        OPERATOR '~'
  392.                         { $$ = make_operator ("~", 1); }
  393.                 |        OPERATOR '!'
  394.                         { $$ = make_operator ("!", 1); }
  395.                 |        OPERATOR '='
  396.                         { $$ = make_operator ("=", 2); }
  397.                 |        OPERATOR '<'
  398.                         { $$ = make_operator ("<", 2); }
  399.                 |        OPERATOR '>'
  400.                         { $$ = make_operator (">", 2); }
  401.                 |        OPERATOR ASSIGN_MODIFY
  402.                         { $$ = make_operator ($2, 2); }
  403.                 |        OPERATOR LSH
  404.                         { $$ = make_operator ("<<", 2); }
  405.                 |        OPERATOR RSH
  406.                         { $$ = make_operator (">>", 2); }
  407.                 |        OPERATOR EQUAL
  408.                         { $$ = make_operator ("==", 2); }
  409.                 |        OPERATOR NOTEQUAL
  410.                         { $$ = make_operator ("!=", 2); }
  411.                 |        OPERATOR LEQ
  412.                         { $$ = make_operator ("<=", 2); }
  413.                 |        OPERATOR GEQ
  414.                         { $$ = make_operator (">=", 2); }
  415.                 |        OPERATOR ANDAND
  416.                         { $$ = make_operator ("&&", 2); }
  417.                 |        OPERATOR OROR
  418.                         { $$ = make_operator ("||", 2); }
  419.                 |        OPERATOR INCREMENT
  420.                         { $$ = make_operator ("++", 1); }
  421.                 |        OPERATOR DECREMENT
  422.                         { $$ = make_operator ("--", 1); }
  423.                 |        OPERATOR ','
  424.                         { $$ = make_operator (",", 2); }
  425.                 |        OPERATOR ARROW '*'
  426.                         { $$ = make_operator ("->*", 2); }
  427.                 |        OPERATOR ARROW
  428.                         { $$ = make_operator ("->", 2); }
  429.                 |        OPERATOR '(' ')'
  430.                         { $$ = make_operator ("()", 2); }
  431.                 |        OPERATOR '[' ']'
  432.                         { $$ = make_operator ("[]", 2); }
  433.                 ;

  434.                 /* Conversion operators.  We don't try to handle some of
  435.                    the wackier demangler output for function pointers,
  436.                    since it's not clear that it's parseable.  */
  437. conversion_op
  438.                 :        OPERATOR typespec_2
  439.                         { $$ = fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL); }
  440.                 ;

  441. conversion_op_name
  442.                 :        nested_name conversion_op
  443.                         { $$.comp = $1.comp;
  444.                           d_right ($1.last) = $2;
  445.                           $$.last = &d_left ($2);
  446.                         }
  447.                 |        conversion_op
  448.                         { $$.comp = $1;
  449.                           $$.last = &d_left ($1);
  450.                         }
  451.                 |        COLONCOLON nested_name conversion_op
  452.                         { $$.comp = $2.comp;
  453.                           d_right ($2.last) = $3;
  454.                           $$.last = &d_left ($3);
  455.                         }
  456.                 |        COLONCOLON conversion_op
  457.                         { $$.comp = $2;
  458.                           $$.last = &d_left ($2);
  459.                         }
  460.                 ;

  461. /* DEMANGLE_COMPONENT_NAME */
  462. /* This accepts certain invalid placements of '~'.  */
  463. unqualified_name:        operator
  464.                 |        operator '<' template_params '>'
  465.                         { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
  466.                 |        '~' NAME
  467.                         { $$ = make_dtor (gnu_v3_complete_object_dtor, $2); }
  468.                 ;

  469. /* This rule is used in name and nested_name, and expanded inline there
  470.    for efficiency.  */
  471. /*
  472. scope_id        :        NAME
  473.                 |        template
  474.                 ;
  475. */

  476. colon_name        :        name
  477.                 |        COLONCOLON name
  478.                         { $$ = $2; }
  479.                 ;

  480. /* DEMANGLE_COMPONENT_QUAL_NAME */
  481. /* DEMANGLE_COMPONENT_CTOR / DEMANGLE_COMPONENT_DTOR ? */
  482. name                :        nested_name NAME %prec NAME
  483.                         { $$ = $1.comp; d_right ($1.last) = $2; }
  484.                 |        NAME %prec NAME
  485.                 |        nested_name template %prec NAME
  486.                         { $$ = $1.comp; d_right ($1.last) = $2; }
  487.                 |        template %prec NAME
  488.                 ;

  489. colon_ext_name        :        colon_name
  490.                 |        colon_ext_only
  491.                 ;

  492. colon_ext_only        :        ext_only_name
  493.                 |        COLONCOLON ext_only_name
  494.                         { $$ = $2; }
  495.                 ;

  496. ext_only_name        :        nested_name unqualified_name
  497.                         { $$ = $1.comp; d_right ($1.last) = $2; }
  498.                 |        unqualified_name
  499.                 ;

  500. nested_name        :        NAME COLONCOLON
  501.                         { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
  502.                           d_left ($$.comp) = $1;
  503.                           d_right ($$.comp) = NULL;
  504.                           $$.last = $$.comp;
  505.                         }
  506.                 |        nested_name NAME COLONCOLON
  507.                         { $$.comp = $1.comp;
  508.                           d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
  509.                           $$.last = d_right ($1.last);
  510.                           d_left ($$.last) = $2;
  511.                           d_right ($$.last) = NULL;
  512.                         }
  513.                 |        template COLONCOLON
  514.                         { $$.comp = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
  515.                           d_left ($$.comp) = $1;
  516.                           d_right ($$.comp) = NULL;
  517.                           $$.last = $$.comp;
  518.                         }
  519.                 |        nested_name template COLONCOLON
  520.                         { $$.comp = $1.comp;
  521.                           d_right ($1.last) = make_empty (DEMANGLE_COMPONENT_QUAL_NAME);
  522.                           $$.last = d_right ($1.last);
  523.                           d_left ($$.last) = $2;
  524.                           d_right ($$.last) = NULL;
  525.                         }
  526.                 ;

  527. /* DEMANGLE_COMPONENT_TEMPLATE */
  528. /* DEMANGLE_COMPONENT_TEMPLATE_ARGLIST */
  529. template        :        NAME '<' template_params '>'
  530.                         { $$ = fill_comp (DEMANGLE_COMPONENT_TEMPLATE, $1, $3.comp); }
  531.                 ;

  532. template_params        :        template_arg
  533.                         { $$.comp = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $1, NULL);
  534.                         $$.last = &d_right ($$.comp); }
  535.                 |        template_params ',' template_arg
  536.                         { $$.comp = $1.comp;
  537.                           *$1.last = fill_comp (DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, $3, NULL);
  538.                           $$.last = &d_right (*$1.last);
  539.                         }
  540.                 ;

  541. /* "type" is inlined into template_arg and function_args.  */

  542. /* Also an integral constant-expression of integral type, and a
  543.    pointer to member (?) */
  544. template_arg        :        typespec_2
  545.                 |        typespec_2 abstract_declarator
  546.                         { $$ = $2.comp;
  547.                           *$2.last = $1;
  548.                         }
  549.                 |        '&' start
  550.                         { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
  551.                 |        '&' '(' start ')'
  552.                         { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
  553.                 |        exp
  554.                 ;

  555. function_args        :        typespec_2
  556.                         { $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $1, NULL);
  557.                           $$.last = &d_right ($$.comp);
  558.                         }
  559.                 |        typespec_2 abstract_declarator
  560.                         { *$2.last = $1;
  561.                           $$.comp = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $2.comp, NULL);
  562.                           $$.last = &d_right ($$.comp);
  563.                         }
  564.                 |        function_args ',' typespec_2
  565.                         { *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $3, NULL);
  566.                           $$.comp = $1.comp;
  567.                           $$.last = &d_right (*$1.last);
  568.                         }
  569.                 |        function_args ',' typespec_2 abstract_declarator
  570.                         { *$4.last = $3;
  571.                           *$1.last = fill_comp (DEMANGLE_COMPONENT_ARGLIST, $4.comp, NULL);
  572.                           $$.comp = $1.comp;
  573.                           $$.last = &d_right (*$1.last);
  574.                         }
  575.                 |        function_args ',' ELLIPSIS
  576.                         { *$1.last
  577.                             = fill_comp (DEMANGLE_COMPONENT_ARGLIST,
  578.                                            make_builtin_type ("..."),
  579.                                            NULL);
  580.                           $$.comp = $1.comp;
  581.                           $$.last = &d_right (*$1.last);
  582.                         }
  583.                 ;

  584. function_arglist:        '(' function_args ')' qualifiers_opt %prec NAME
  585.                         { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, $2.comp);
  586.                           $$.last = &d_left ($$.comp);
  587.                           $$.comp = d_qualify ($$.comp, $4, 1); }
  588.                 |        '(' VOID ')' qualifiers_opt
  589.                         { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
  590.                           $$.last = &d_left ($$.comp);
  591.                           $$.comp = d_qualify ($$.comp, $4, 1); }
  592.                 |        '(' ')' qualifiers_opt
  593.                         { $$.comp = fill_comp (DEMANGLE_COMPONENT_FUNCTION_TYPE, NULL, NULL);
  594.                           $$.last = &d_left ($$.comp);
  595.                           $$.comp = d_qualify ($$.comp, $3, 1); }
  596.                 ;

  597. /* Should do something about DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL */
  598. qualifiers_opt        :        /* epsilon */
  599.                         { $$ = 0; }
  600.                 |        qualifiers
  601.                 ;

  602. qualifier        :        RESTRICT
  603.                         { $$ = QUAL_RESTRICT; }
  604.                 |        VOLATILE_KEYWORD
  605.                         { $$ = QUAL_VOLATILE; }
  606.                 |        CONST_KEYWORD
  607.                         { $$ = QUAL_CONST; }
  608.                 ;

  609. qualifiers        :        qualifier
  610.                 |        qualifier qualifiers
  611.                         { $$ = $1 | $2; }
  612.                 ;

  613. /* This accepts all sorts of invalid constructions and produces
  614.    invalid output for them - an error would be better.  */

  615. int_part        :        INT_KEYWORD
  616.                         { $$ = 0; }
  617.                 |        SIGNED_KEYWORD
  618.                         { $$ = INT_SIGNED; }
  619.                 |        UNSIGNED
  620.                         { $$ = INT_UNSIGNED; }
  621.                 |        CHAR
  622.                         { $$ = INT_CHAR; }
  623.                 |        LONG
  624.                         { $$ = INT_LONG; }
  625.                 |        SHORT
  626.                         { $$ = INT_SHORT; }
  627.                 ;

  628. int_seq                :        int_part
  629.                 |        int_seq int_part
  630.                         { $$ = $1 | $2; if ($1 & $2 & INT_LONG) $$ = $1 | INT_LLONG; }
  631.                 ;

  632. builtin_type        :        int_seq
  633.                         { $$ = d_int_type ($1); }
  634.                 |        FLOAT_KEYWORD
  635.                         { $$ = make_builtin_type ("float"); }
  636.                 |        DOUBLE_KEYWORD
  637.                         { $$ = make_builtin_type ("double"); }
  638.                 |        LONG DOUBLE_KEYWORD
  639.                         { $$ = make_builtin_type ("long double"); }
  640.                 |        BOOL
  641.                         { $$ = make_builtin_type ("bool"); }
  642.                 |        WCHAR_T
  643.                         { $$ = make_builtin_type ("wchar_t"); }
  644.                 |        VOID
  645.                         { $$ = make_builtin_type ("void"); }
  646.                 ;

  647. ptr_operator        :        '*' qualifiers_opt
  648.                         { $$.comp = make_empty (DEMANGLE_COMPONENT_POINTER);
  649.                           $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
  650.                           $$.last = &d_left ($$.comp);
  651.                           $$.comp = d_qualify ($$.comp, $2, 0); }
  652.                 /* g++ seems to allow qualifiers after the reference?  */
  653.                 |        '&'
  654.                         { $$.comp = make_empty (DEMANGLE_COMPONENT_REFERENCE);
  655.                           $$.comp->u.s_binary.left = $$.comp->u.s_binary.right = NULL;
  656.                           $$.last = &d_left ($$.comp); }
  657.                 |        nested_name '*' qualifiers_opt
  658.                         { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
  659.                           $$.comp->u.s_binary.left = $1.comp;
  660.                           /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME.  */
  661.                           *$1.last = *d_left ($1.last);
  662.                           $$.comp->u.s_binary.right = NULL;
  663.                           $$.last = &d_right ($$.comp);
  664.                           $$.comp = d_qualify ($$.comp, $3, 0); }
  665.                 |        COLONCOLON nested_name '*' qualifiers_opt
  666.                         { $$.comp = make_empty (DEMANGLE_COMPONENT_PTRMEM_TYPE);
  667.                           $$.comp->u.s_binary.left = $2.comp;
  668.                           /* Convert the innermost DEMANGLE_COMPONENT_QUAL_NAME to a DEMANGLE_COMPONENT_NAME.  */
  669.                           *$2.last = *d_left ($2.last);
  670.                           $$.comp->u.s_binary.right = NULL;
  671.                           $$.last = &d_right ($$.comp);
  672.                           $$.comp = d_qualify ($$.comp, $4, 0); }
  673.                 ;

  674. array_indicator        :        '[' ']'
  675.                         { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
  676.                           d_left ($$) = NULL;
  677.                         }
  678.                 |        '[' INT ']'
  679.                         { $$ = make_empty (DEMANGLE_COMPONENT_ARRAY_TYPE);
  680.                           d_left ($$) = $2;
  681.                         }
  682.                 ;

  683. /* Details of this approach inspired by the G++ < 3.4 parser.  */

  684. /* This rule is only used in typespec_2, and expanded inline there for
  685.    efficiency.  */
  686. /*
  687. typespec        :        builtin_type
  688.                 |        colon_name
  689.                 ;
  690. */

  691. typespec_2        :        builtin_type qualifiers
  692.                         { $$ = d_qualify ($1, $2, 0); }
  693.                 |        builtin_type
  694.                 |        qualifiers builtin_type qualifiers
  695.                         { $$ = d_qualify ($2, $1 | $3, 0); }
  696.                 |        qualifiers builtin_type
  697.                         { $$ = d_qualify ($2, $1, 0); }

  698.                 |        name qualifiers
  699.                         { $$ = d_qualify ($1, $2, 0); }
  700.                 |        name
  701.                 |        qualifiers name qualifiers
  702.                         { $$ = d_qualify ($2, $1 | $3, 0); }
  703.                 |        qualifiers name
  704.                         { $$ = d_qualify ($2, $1, 0); }

  705.                 |        COLONCOLON name qualifiers
  706.                         { $$ = d_qualify ($2, $3, 0); }
  707.                 |        COLONCOLON name
  708.                         { $$ = $2; }
  709.                 |        qualifiers COLONCOLON name qualifiers
  710.                         { $$ = d_qualify ($3, $1 | $4, 0); }
  711.                 |        qualifiers COLONCOLON name
  712.                         { $$ = d_qualify ($3, $1, 0); }
  713.                 ;

  714. abstract_declarator
  715.                 :        ptr_operator
  716.                         { $$.comp = $1.comp; $$.last = $1.last;
  717.                           $$.fn.comp = NULL; $$.fn.last = NULL; }
  718.                 |        ptr_operator abstract_declarator
  719.                         { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL;
  720.                           if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
  721.                           *$$.last = $1.comp;
  722.                           $$.last = $1.last; }
  723.                 |        direct_abstract_declarator
  724.                         { $$.fn.comp = NULL; $$.fn.last = NULL;
  725.                           if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
  726.                         }
  727.                 ;

  728. direct_abstract_declarator
  729.                 :        '(' abstract_declarator ')'
  730.                         { $$ = $2; $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 1;
  731.                           if ($2.fn.comp) { $$.last = $2.fn.last; *$2.last = $2.fn.comp; }
  732.                         }
  733.                 |        direct_abstract_declarator function_arglist
  734.                         { $$.fold_flag = 0;
  735.                           if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
  736.                           if ($1.fold_flag)
  737.                             {
  738.                               *$$.last = $2.comp;
  739.                               $$.last = $2.last;
  740.                             }
  741.                           else
  742.                             $$.fn = $2;
  743.                         }
  744.                 |        direct_abstract_declarator array_indicator
  745.                         { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
  746.                           if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
  747.                           *$1.last = $2;
  748.                           $$.last = &d_right ($2);
  749.                         }
  750.                 |        array_indicator
  751.                         { $$.fn.comp = NULL; $$.fn.last = NULL; $$.fold_flag = 0;
  752.                           $$.comp = $1;
  753.                           $$.last = &d_right ($1);
  754.                         }
  755.                 /* G++ has the following except for () and (type).  Then
  756.                    (type) is handled in regcast_or_absdcl and () is handled
  757.                    in fcast_or_absdcl.

  758.                    However, this is only useful for function types, and
  759.                    generates reduce/reduce conflicts with direct_declarator.
  760.                    We're interested in pointer-to-function types, and in
  761.                    functions, but not in function types - so leave this
  762.                    out.  */
  763.                 /* |        function_arglist */
  764.                 ;

  765. abstract_declarator_fn
  766.                 :        ptr_operator
  767.                         { $$.comp = $1.comp; $$.last = $1.last;
  768.                           $$.fn.comp = NULL; $$.fn.last = NULL; $$.start = NULL; }
  769.                 |        ptr_operator abstract_declarator_fn
  770.                         { $$ = $2;
  771.                           if ($2.last)
  772.                             *$$.last = $1.comp;
  773.                           else
  774.                             $$.comp = $1.comp;
  775.                           $$.last = $1.last;
  776.                         }
  777.                 |        direct_abstract_declarator
  778.                         { $$.comp = $1.comp; $$.last = $1.last; $$.fn = $1.fn; $$.start = NULL; }
  779.                 |        direct_abstract_declarator function_arglist COLONCOLON start
  780.                         { $$.start = $4;
  781.                           if ($1.fn.comp) { $$.last = $1.fn.last; *$1.last = $1.fn.comp; }
  782.                           if ($1.fold_flag)
  783.                             {
  784.                               *$$.last = $2.comp;
  785.                               $$.last = $2.last;
  786.                             }
  787.                           else
  788.                             $$.fn = $2;
  789.                         }
  790.                 |        function_arglist start_opt
  791.                         { $$.fn = $1;
  792.                           $$.start = $2;
  793.                           $$.comp = NULL; $$.last = NULL;
  794.                         }
  795.                 ;

  796. type                :        typespec_2
  797.                 |        typespec_2 abstract_declarator
  798.                         { $$ = $2.comp;
  799.                           *$2.last = $1;
  800.                         }
  801.                 ;

  802. declarator        :        ptr_operator declarator
  803.                         { $$.comp = $2.comp;
  804.                           $$.last = $1.last;
  805.                           *$2.last = $1.comp; }
  806.                 |        direct_declarator
  807.                 ;

  808. direct_declarator
  809.                 :        '(' declarator ')'
  810.                         { $$ = $2; }
  811.                 |        direct_declarator function_arglist
  812.                         { $$.comp = $1.comp;
  813.                           *$1.last = $2.comp;
  814.                           $$.last = $2.last;
  815.                         }
  816.                 |        direct_declarator array_indicator
  817.                         { $$.comp = $1.comp;
  818.                           *$1.last = $2;
  819.                           $$.last = &d_right ($2);
  820.                         }
  821.                 |        colon_ext_name
  822.                         { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
  823.                           d_left ($$.comp) = $1;
  824.                           $$.last = &d_right ($$.comp);
  825.                         }
  826.                 ;

  827. /* These are similar to declarator and direct_declarator except that they
  828.    do not permit ( colon_ext_name ), which is ambiguous with a function
  829.    argument list.  They also don't permit a few other forms with redundant
  830.    parentheses around the colon_ext_name; any colon_ext_name in parentheses
  831.    must be followed by an argument list or an array indicator, or preceded
  832.    by a pointer.  */
  833. declarator_1        :        ptr_operator declarator_1
  834.                         { $$.comp = $2.comp;
  835.                           $$.last = $1.last;
  836.                           *$2.last = $1.comp; }
  837.                 |        colon_ext_name
  838.                         { $$.comp = make_empty (DEMANGLE_COMPONENT_TYPED_NAME);
  839.                           d_left ($$.comp) = $1;
  840.                           $$.last = &d_right ($$.comp);
  841.                         }
  842.                 |        direct_declarator_1

  843.                         /* Function local variable or type.  The typespec to
  844.                            our left is the type of the containing function.
  845.                            This should be OK, because function local types
  846.                            can not be templates, so the return types of their
  847.                            members will not be mangled.  If they are hopefully
  848.                            they'll end up to the right of the ::.  */
  849.                 |        colon_ext_name function_arglist COLONCOLON start
  850.                         { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
  851.                           $$.last = $2.last;
  852.                           $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
  853.                         }
  854.                 |        direct_declarator_1 function_arglist COLONCOLON start
  855.                         { $$.comp = $1.comp;
  856.                           *$1.last = $2.comp;
  857.                           $$.last = $2.last;
  858.                           $$.comp = fill_comp (DEMANGLE_COMPONENT_LOCAL_NAME, $$.comp, $4);
  859.                         }
  860.                 ;

  861. direct_declarator_1
  862.                 :        '(' ptr_operator declarator ')'
  863.                         { $$.comp = $3.comp;
  864.                           $$.last = $2.last;
  865.                           *$3.last = $2.comp; }
  866.                 |        direct_declarator_1 function_arglist
  867.                         { $$.comp = $1.comp;
  868.                           *$1.last = $2.comp;
  869.                           $$.last = $2.last;
  870.                         }
  871.                 |        direct_declarator_1 array_indicator
  872.                         { $$.comp = $1.comp;
  873.                           *$1.last = $2;
  874.                           $$.last = &d_right ($2);
  875.                         }
  876.                 |        colon_ext_name function_arglist
  877.                         { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2.comp);
  878.                           $$.last = $2.last;
  879.                         }
  880.                 |        colon_ext_name array_indicator
  881.                         { $$.comp = fill_comp (DEMANGLE_COMPONENT_TYPED_NAME, $1, $2);
  882.                           $$.last = &d_right ($2);
  883.                         }
  884.                 ;

  885. exp        :        '(' exp1 ')'
  886.                 { $$ = $2; }
  887.         ;

  888. /* Silly trick.  Only allow '>' when parenthesized, in order to
  889.    handle conflict with templates.  */
  890. exp1        :        exp
  891.         ;

  892. exp1        :        exp '>' exp
  893.                 { $$ = d_binary (">", $1, $3); }
  894.         ;

  895. /* References.  Not allowed everywhere in template parameters, only
  896.    at the top level, but treat them as expressions in case they are wrapped
  897.    in parentheses.  */
  898. exp1        :        '&' start
  899.                 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $2); }
  900.         |        '&' '(' start ')'
  901.                 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator ("&", 1), $3); }
  902.         ;

  903. /* Expressions, not including the comma operator.  */
  904. exp        :        '-' exp    %prec UNARY
  905.                 { $$ = d_unary ("-", $2); }
  906.         ;

  907. exp        :        '!' exp    %prec UNARY
  908.                 { $$ = d_unary ("!", $2); }
  909.         ;

  910. exp        :        '~' exp    %prec UNARY
  911.                 { $$ = d_unary ("~", $2); }
  912.         ;

  913. /* Casts.  First your normal C-style cast.  If exp is a LITERAL, just change
  914.    its type.  */

  915. exp        :        '(' type ')' exp  %prec UNARY
  916.                 { if ($4->type == DEMANGLE_COMPONENT_LITERAL
  917.                       || $4->type == DEMANGLE_COMPONENT_LITERAL_NEG)
  918.                     {
  919.                       $$ = $4;
  920.                       d_left ($4) = $2;
  921.                     }
  922.                   else
  923.                     $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
  924.                                       fill_comp (DEMANGLE_COMPONENT_CAST, $2, NULL),
  925.                                       $4);
  926.                 }
  927.         ;

  928. /* Mangling does not differentiate between these, so we don't need to
  929.    either.  */
  930. exp        :        STATIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
  931.                 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
  932.                                     fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
  933.                                     $6);
  934.                 }
  935.         ;

  936. exp        :        DYNAMIC_CAST '<' type '>' '(' exp1 ')' %prec UNARY
  937.                 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
  938.                                     fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
  939.                                     $6);
  940.                 }
  941.         ;

  942. exp        :        REINTERPRET_CAST '<' type '>' '(' exp1 ')' %prec UNARY
  943.                 { $$ = fill_comp (DEMANGLE_COMPONENT_UNARY,
  944.                                     fill_comp (DEMANGLE_COMPONENT_CAST, $3, NULL),
  945.                                     $6);
  946.                 }
  947.         ;

  948. /* Another form of C++-style cast is "type ( exp1 )".  This creates too many
  949.    conflicts to support.  For a while we supported the simpler
  950.    "typespec_2 ( exp1 )", but that conflicts with "& ( start )" as a
  951.    reference, deep within the wilderness of abstract declarators:
  952.    Qux<int(&(*))> vs Qux<int(&(var))>, a shift-reduce conflict at the
  953.    innermost left parenthesis.  So we do not support function-like casts.
  954.    Fortunately they never appear in demangler output.  */

  955. /* TO INVESTIGATE: ._0 style anonymous names; anonymous namespaces */

  956. /* Binary operators in order of decreasing precedence.  */

  957. exp        :        exp '*' exp
  958.                 { $$ = d_binary ("*", $1, $3); }
  959.         ;

  960. exp        :        exp '/' exp
  961.                 { $$ = d_binary ("/", $1, $3); }
  962.         ;

  963. exp        :        exp '%' exp
  964.                 { $$ = d_binary ("%", $1, $3); }
  965.         ;

  966. exp        :        exp '+' exp
  967.                 { $$ = d_binary ("+", $1, $3); }
  968.         ;

  969. exp        :        exp '-' exp
  970.                 { $$ = d_binary ("-", $1, $3); }
  971.         ;

  972. exp        :        exp LSH exp
  973.                 { $$ = d_binary ("<<", $1, $3); }
  974.         ;

  975. exp        :        exp RSH exp
  976.                 { $$ = d_binary (">>", $1, $3); }
  977.         ;

  978. exp        :        exp EQUAL exp
  979.                 { $$ = d_binary ("==", $1, $3); }
  980.         ;

  981. exp        :        exp NOTEQUAL exp
  982.                 { $$ = d_binary ("!=", $1, $3); }
  983.         ;

  984. exp        :        exp LEQ exp
  985.                 { $$ = d_binary ("<=", $1, $3); }
  986.         ;

  987. exp        :        exp GEQ exp
  988.                 { $$ = d_binary (">=", $1, $3); }
  989.         ;

  990. exp        :        exp '<' exp
  991.                 { $$ = d_binary ("<", $1, $3); }
  992.         ;

  993. exp        :        exp '&' exp
  994.                 { $$ = d_binary ("&", $1, $3); }
  995.         ;

  996. exp        :        exp '^' exp
  997.                 { $$ = d_binary ("^", $1, $3); }
  998.         ;

  999. exp        :        exp '|' exp
  1000.                 { $$ = d_binary ("|", $1, $3); }
  1001.         ;

  1002. exp        :        exp ANDAND exp
  1003.                 { $$ = d_binary ("&&", $1, $3); }
  1004.         ;

  1005. exp        :        exp OROR exp
  1006.                 { $$ = d_binary ("||", $1, $3); }
  1007.         ;

  1008. /* Not 100% sure these are necessary, but they're harmless.  */
  1009. exp        :        exp ARROW NAME
  1010.                 { $$ = d_binary ("->", $1, $3); }
  1011.         ;

  1012. exp        :        exp '.' NAME
  1013.                 { $$ = d_binary (".", $1, $3); }
  1014.         ;

  1015. exp        :        exp '?' exp ':' exp        %prec '?'
  1016.                 { $$ = fill_comp (DEMANGLE_COMPONENT_TRINARY, make_operator ("?", 3),
  1017.                                     fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG1, $1,
  1018.                                                  fill_comp (DEMANGLE_COMPONENT_TRINARY_ARG2, $3, $5)));
  1019.                 }
  1020.         ;

  1021. exp        :        INT
  1022.         ;

  1023. /* Not generally allowed.  */
  1024. exp        :        FLOAT
  1025.         ;

  1026. exp        :        SIZEOF '(' type ')'        %prec UNARY
  1027.                 {
  1028.                   /* Match the whitespacing of cplus_demangle_operators.
  1029.                      It would abort on unrecognized string otherwise.  */
  1030.                   $$ = d_unary ("sizeof ", $3);
  1031.                 }
  1032.         ;

  1033. /* C++.  */
  1034. exp     :       TRUEKEYWORD
  1035.                 { struct demangle_component *i;
  1036.                   i = make_name ("1", 1);
  1037.                   $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
  1038.                                     make_builtin_type ("bool"),
  1039.                                     i);
  1040.                 }
  1041.         ;

  1042. exp     :       FALSEKEYWORD
  1043.                 { struct demangle_component *i;
  1044.                   i = make_name ("0", 1);
  1045.                   $$ = fill_comp (DEMANGLE_COMPONENT_LITERAL,
  1046.                                     make_builtin_type ("bool"),
  1047.                                     i);
  1048.                 }
  1049.         ;

  1050. /* end of C++.  */

  1051. %%

  1052. /* Apply QUALIFIERS to LHS and return a qualified component.  IS_METHOD
  1053.    is set if LHS is a method, in which case the qualifiers are logically
  1054.    applied to "this".  We apply qualifiers in a consistent order; LHS
  1055.    may already be qualified; duplicate qualifiers are not created.  */

  1056. struct demangle_component *
  1057. d_qualify (struct demangle_component *lhs, int qualifiers, int is_method)
  1058. {
  1059.   struct demangle_component **inner_p;
  1060.   enum demangle_component_type type;

  1061.   /* For now the order is CONST (innermost), VOLATILE, RESTRICT.  */

  1062. #define HANDLE_QUAL(TYPE, MTYPE, QUAL)                                \
  1063.   if ((qualifiers & QUAL) && (type != TYPE) && (type != MTYPE))        \
  1064.     {                                                                \
  1065.       *inner_p = fill_comp (is_method ? MTYPE : TYPE,        \
  1066.                               *inner_p, NULL);                        \
  1067.       inner_p = &d_left (*inner_p);                                \
  1068.       type = (*inner_p)->type;                                        \
  1069.     }                                                                \
  1070.   else if (type == TYPE || type == MTYPE)                        \
  1071.     {                                                                \
  1072.       inner_p = &d_left (*inner_p);                                \
  1073.       type = (*inner_p)->type;                                        \
  1074.     }

  1075.   inner_p = &lhs;

  1076.   type = (*inner_p)->type;

  1077.   HANDLE_QUAL (DEMANGLE_COMPONENT_RESTRICT, DEMANGLE_COMPONENT_RESTRICT_THIS, QUAL_RESTRICT);
  1078.   HANDLE_QUAL (DEMANGLE_COMPONENT_VOLATILE, DEMANGLE_COMPONENT_VOLATILE_THIS, QUAL_VOLATILE);
  1079.   HANDLE_QUAL (DEMANGLE_COMPONENT_CONST, DEMANGLE_COMPONENT_CONST_THIS, QUAL_CONST);

  1080.   return lhs;
  1081. }

  1082. /* Return a builtin type corresponding to FLAGS.  */

  1083. static struct demangle_component *
  1084. d_int_type (int flags)
  1085. {
  1086.   const char *name;

  1087.   switch (flags)
  1088.     {
  1089.     case INT_SIGNED | INT_CHAR:
  1090.       name = "signed char";
  1091.       break;
  1092.     case INT_CHAR:
  1093.       name = "char";
  1094.       break;
  1095.     case INT_UNSIGNED | INT_CHAR:
  1096.       name = "unsigned char";
  1097.       break;
  1098.     case 0:
  1099.     case INT_SIGNED:
  1100.       name = "int";
  1101.       break;
  1102.     case INT_UNSIGNED:
  1103.       name = "unsigned int";
  1104.       break;
  1105.     case INT_LONG:
  1106.     case INT_SIGNED | INT_LONG:
  1107.       name = "long";
  1108.       break;
  1109.     case INT_UNSIGNED | INT_LONG:
  1110.       name = "unsigned long";
  1111.       break;
  1112.     case INT_SHORT:
  1113.     case INT_SIGNED | INT_SHORT:
  1114.       name = "short";
  1115.       break;
  1116.     case INT_UNSIGNED | INT_SHORT:
  1117.       name = "unsigned short";
  1118.       break;
  1119.     case INT_LLONG | INT_LONG:
  1120.     case INT_SIGNED | INT_LLONG | INT_LONG:
  1121.       name = "long long";
  1122.       break;
  1123.     case INT_UNSIGNED | INT_LLONG | INT_LONG:
  1124.       name = "unsigned long long";
  1125.       break;
  1126.     default:
  1127.       return NULL;
  1128.     }

  1129.   return make_builtin_type (name);
  1130. }

  1131. /* Wrapper to create a unary operation.  */

  1132. static struct demangle_component *
  1133. d_unary (const char *name, struct demangle_component *lhs)
  1134. {
  1135.   return fill_comp (DEMANGLE_COMPONENT_UNARY, make_operator (name, 1), lhs);
  1136. }

  1137. /* Wrapper to create a binary operation.  */

  1138. static struct demangle_component *
  1139. d_binary (const char *name, struct demangle_component *lhs, struct demangle_component *rhs)
  1140. {
  1141.   return fill_comp (DEMANGLE_COMPONENT_BINARY, make_operator (name, 2),
  1142.                       fill_comp (DEMANGLE_COMPONENT_BINARY_ARGS, lhs, rhs));
  1143. }

  1144. /* Find the end of a symbol name starting at LEXPTR.  */

  1145. static const char *
  1146. symbol_end (const char *lexptr)
  1147. {
  1148.   const char *p = lexptr;

  1149.   while (*p && (ISALNUM (*p) || *p == '_' || *p == '$' || *p == '.'))
  1150.     p++;

  1151.   return p;
  1152. }

  1153. /* Take care of parsing a number (anything that starts with a digit).
  1154.    The number starts at P and contains LEN characters.  Store the result in
  1155.    YYLVAL.  */

  1156. static int
  1157. parse_number (const char *p, int len, int parsed_float)
  1158. {
  1159.   int unsigned_p = 0;

  1160.   /* Number of "L" suffixes encountered.  */
  1161.   int long_p = 0;

  1162.   struct demangle_component *signed_type;
  1163.   struct demangle_component *unsigned_type;
  1164.   struct demangle_component *type, *name;
  1165.   enum demangle_component_type literal_type;

  1166.   if (p[0] == '-')
  1167.     {
  1168.       literal_type = DEMANGLE_COMPONENT_LITERAL_NEG;
  1169.       p++;
  1170.       len--;
  1171.     }
  1172.   else
  1173.     literal_type = DEMANGLE_COMPONENT_LITERAL;

  1174.   if (parsed_float)
  1175.     {
  1176.       /* It's a float since it contains a point or an exponent.  */
  1177.       char c;

  1178.       /* The GDB lexer checks the result of scanf at this point.  Not doing
  1179.          this leaves our error checking slightly weaker but only for invalid
  1180.          data.  */

  1181.       /* See if it has `f' or `l' suffix (float or long double).  */

  1182.       c = TOLOWER (p[len - 1]);

  1183.       if (c == 'f')
  1184.               {
  1185.                 len--;
  1186.                 type = make_builtin_type ("float");
  1187.               }
  1188.       else if (c == 'l')
  1189.         {
  1190.           len--;
  1191.           type = make_builtin_type ("long double");
  1192.         }
  1193.       else if (ISDIGIT (c) || c == '.')
  1194.         type = make_builtin_type ("double");
  1195.       else
  1196.         return ERROR;

  1197.       name = make_name (p, len);
  1198.       yylval.comp = fill_comp (literal_type, type, name);

  1199.       return FLOAT;
  1200.     }

  1201.   /* This treats 0x1 and 1 as different literals.  We also do not
  1202.      automatically generate unsigned types.  */

  1203.   long_p = 0;
  1204.   unsigned_p = 0;
  1205.   while (len > 0)
  1206.     {
  1207.       if (p[len - 1] == 'l' || p[len - 1] == 'L')
  1208.         {
  1209.           len--;
  1210.           long_p++;
  1211.           continue;
  1212.         }
  1213.       if (p[len - 1] == 'u' || p[len - 1] == 'U')
  1214.         {
  1215.           len--;
  1216.           unsigned_p++;
  1217.           continue;
  1218.         }
  1219.       break;
  1220.     }

  1221.   if (long_p == 0)
  1222.     {
  1223.       unsigned_type = make_builtin_type ("unsigned int");
  1224.       signed_type = make_builtin_type ("int");
  1225.     }
  1226.   else if (long_p == 1)
  1227.     {
  1228.       unsigned_type = make_builtin_type ("unsigned long");
  1229.       signed_type = make_builtin_type ("long");
  1230.     }
  1231.   else
  1232.     {
  1233.       unsigned_type = make_builtin_type ("unsigned long long");
  1234.       signed_type = make_builtin_type ("long long");
  1235.     }

  1236.    if (unsigned_p)
  1237.      type = unsigned_type;
  1238.    else
  1239.      type = signed_type;

  1240.    name = make_name (p, len);
  1241.    yylval.comp = fill_comp (literal_type, type, name);

  1242.    return INT;
  1243. }

  1244. static char backslashable[] = "abefnrtv";
  1245. static char represented[] = "\a\b\e\f\n\r\t\v";

  1246. /* Translate the backslash the way we would in the host character set.  */
  1247. static int
  1248. c_parse_backslash (int host_char, int *target_char)
  1249. {
  1250.   const char *ix;
  1251.   ix = strchr (backslashable, host_char);
  1252.   if (! ix)
  1253.     return 0;
  1254.   else
  1255.     *target_char = represented[ix - backslashable];
  1256.   return 1;
  1257. }

  1258. /* Parse a C escape sequence.  STRING_PTR points to a variable
  1259.    containing a pointer to the string to parse.  That pointer
  1260.    should point to the character after the \.  That pointer
  1261.    is updated past the characters we use.  The value of the
  1262.    escape sequence is returned.

  1263.    A negative value means the sequence \ newline was seen,
  1264.    which is supposed to be equivalent to nothing at all.

  1265.    If \ is followed by a null character, we return a negative
  1266.    value and leave the string pointer pointing at the null character.

  1267.    If \ is followed by 000, we return 0 and leave the string pointer
  1268.    after the zeros.  A value of 0 does not mean end of string.  */

  1269. static int
  1270. cp_parse_escape (const char **string_ptr)
  1271. {
  1272.   int target_char;
  1273.   int c = *(*string_ptr)++;
  1274.   if (c_parse_backslash (c, &target_char))
  1275.     return target_char;
  1276.   else
  1277.     switch (c)
  1278.       {
  1279.       case '\n':
  1280.         return -2;
  1281.       case 0:
  1282.         (*string_ptr)--;
  1283.         return 0;
  1284.       case '^':
  1285.         {
  1286.           c = *(*string_ptr)++;

  1287.           if (c == '?')
  1288.             return 0177;
  1289.           else if (c == '\\')
  1290.             target_char = cp_parse_escape (string_ptr);
  1291.           else
  1292.             target_char = c;

  1293.           /* Now target_char is something like `c', and we want to find
  1294.              its control-character equivalent.  */
  1295.           target_char = target_char & 037;

  1296.           return target_char;
  1297.         }

  1298.       case '0':
  1299.       case '1':
  1300.       case '2':
  1301.       case '3':
  1302.       case '4':
  1303.       case '5':
  1304.       case '6':
  1305.       case '7':
  1306.         {
  1307.           int i = c - '0';
  1308.           int count = 0;
  1309.           while (++count < 3)
  1310.             {
  1311.               c = (**string_ptr);
  1312.               if (c >= '0' && c <= '7')
  1313.                 {
  1314.                   (*string_ptr)++;
  1315.                   i *= 8;
  1316.                   i += c - '0';
  1317.                 }
  1318.               else
  1319.                 {
  1320.                   break;
  1321.                 }
  1322.             }
  1323.           return i;
  1324.         }
  1325.       default:
  1326.         return c;
  1327.       }
  1328. }

  1329. #define HANDLE_SPECIAL(string, comp)                                \
  1330.   if (strncmp (tokstart, string, sizeof (string) - 1) == 0)        \
  1331.     {                                                                \
  1332.       lexptr = tokstart + sizeof (string) - 1;                        \
  1333.       yylval.lval = comp;                                        \
  1334.       return DEMANGLER_SPECIAL;                                        \
  1335.     }

  1336. #define HANDLE_TOKEN2(string, token)                        \
  1337.   if (lexptr[1] == string[1])                                \
  1338.     {                                                        \
  1339.       lexptr += 2;                                        \
  1340.       yylval.opname = string;                                \
  1341.       return token;                                        \
  1342.     }

  1343. #define HANDLE_TOKEN3(string, token)                        \
  1344.   if (lexptr[1] == string[1] && lexptr[2] == string[2])        \
  1345.     {                                                        \
  1346.       lexptr += 3;                                        \
  1347.       yylval.opname = string;                                \
  1348.       return token;                                        \
  1349.     }

  1350. /* Read one token, getting characters through LEXPTR.  */

  1351. static int
  1352. yylex (void)
  1353. {
  1354.   int c;
  1355.   int namelen;
  1356.   const char *tokstart;

  1357. retry:
  1358.   prev_lexptr = lexptr;
  1359.   tokstart = lexptr;

  1360.   switch (c = *tokstart)
  1361.     {
  1362.     case 0:
  1363.       return 0;

  1364.     case ' ':
  1365.     case '\t':
  1366.     case '\n':
  1367.       lexptr++;
  1368.       goto retry;

  1369.     case '\'':
  1370.       /* We either have a character constant ('0' or '\177' for example)
  1371.          or we have a quoted symbol reference ('foo(int,int)' in C++
  1372.          for example). */
  1373.       lexptr++;
  1374.       c = *lexptr++;
  1375.       if (c == '\\')
  1376.         c = cp_parse_escape (&lexptr);
  1377.       else if (c == '\'')
  1378.         {
  1379.           yyerror (_("empty character constant"));
  1380.           return ERROR;
  1381.         }

  1382.       c = *lexptr++;
  1383.       if (c != '\'')
  1384.         {
  1385.           yyerror (_("invalid character constant"));
  1386.           return ERROR;
  1387.         }

  1388.       /* FIXME: We should refer to a canonical form of the character,
  1389.          presumably the same one that appears in manglings - the decimal
  1390.          representation.  But if that isn't in our input then we have to
  1391.          allocate memory for it somewhere.  */
  1392.       yylval.comp = fill_comp (DEMANGLE_COMPONENT_LITERAL,
  1393.                                  make_builtin_type ("char"),
  1394.                                  make_name (tokstart, lexptr - tokstart));

  1395.       return INT;

  1396.     case '(':
  1397.       if (strncmp (tokstart, "(anonymous namespace)", 21) == 0)
  1398.         {
  1399.           lexptr += 21;
  1400.           yylval.comp = make_name ("(anonymous namespace)",
  1401.                                      sizeof "(anonymous namespace)" - 1);
  1402.           return NAME;
  1403.         }
  1404.         /* FALL THROUGH */

  1405.     case ')':
  1406.     case ',':
  1407.       lexptr++;
  1408.       return c;

  1409.     case '.':
  1410.       if (lexptr[1] == '.' && lexptr[2] == '.')
  1411.         {
  1412.           lexptr += 3;
  1413.           return ELLIPSIS;
  1414.         }

  1415.       /* Might be a floating point number.  */
  1416.       if (lexptr[1] < '0' || lexptr[1] > '9')
  1417.         goto symbol;                /* Nope, must be a symbol. */

  1418.       goto try_number;

  1419.     case '-':
  1420.       HANDLE_TOKEN2 ("-=", ASSIGN_MODIFY);
  1421.       HANDLE_TOKEN2 ("--", DECREMENT);
  1422.       HANDLE_TOKEN2 ("->", ARROW);

  1423.       /* For construction vtables.  This is kind of hokey.  */
  1424.       if (strncmp (tokstart, "-in-", 4) == 0)
  1425.         {
  1426.           lexptr += 4;
  1427.           return CONSTRUCTION_IN;
  1428.         }

  1429.       if (lexptr[1] < '0' || lexptr[1] > '9')
  1430.         {
  1431.           lexptr++;
  1432.           return '-';
  1433.         }
  1434.       /* FALL THRU into number case.  */

  1435.     try_number:
  1436.     case '0':
  1437.     case '1':
  1438.     case '2':
  1439.     case '3':
  1440.     case '4':
  1441.     case '5':
  1442.     case '6':
  1443.     case '7':
  1444.     case '8':
  1445.     case '9':
  1446.       {
  1447.         /* It's a number.  */
  1448.         int got_dot = 0, got_e = 0, toktype;
  1449.         const char *p = tokstart;
  1450.         int hex = 0;

  1451.         if (c == '-')
  1452.           p++;

  1453.         if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  1454.           {
  1455.             p += 2;
  1456.             hex = 1;
  1457.           }
  1458.         else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
  1459.           {
  1460.             p += 2;
  1461.             hex = 0;
  1462.           }

  1463.         for (;; ++p)
  1464.           {
  1465.             /* This test includes !hex because 'e' is a valid hex digit
  1466.                and thus does not indicate a floating point number when
  1467.                the radix is hex.  */
  1468.             if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  1469.               got_dot = got_e = 1;
  1470.             /* This test does not include !hex, because a '.' always indicates
  1471.                a decimal floating point number regardless of the radix.

  1472.                NOTE drow/2005-03-09: This comment is not accurate in C99;
  1473.                however, it's not clear that all the floating point support
  1474.                in this file is doing any good here.  */
  1475.             else if (!got_dot && *p == '.')
  1476.               got_dot = 1;
  1477.             else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  1478.                      && (*p == '-' || *p == '+'))
  1479.               /* This is the sign of the exponent, not the end of the
  1480.                  number.  */
  1481.               continue;
  1482.             /* We will take any letters or digits.  parse_number will
  1483.                complain if past the radix, or if L or U are not final.  */
  1484.             else if (! ISALNUM (*p))
  1485.               break;
  1486.           }
  1487.         toktype = parse_number (tokstart, p - tokstart, got_dot|got_e);
  1488.         if (toktype == ERROR)
  1489.           {
  1490.             char *err_copy = (char *) alloca (p - tokstart + 1);

  1491.             memcpy (err_copy, tokstart, p - tokstart);
  1492.             err_copy[p - tokstart] = 0;
  1493.             yyerror (_("invalid number"));
  1494.             return ERROR;
  1495.           }
  1496.         lexptr = p;
  1497.         return toktype;
  1498.       }

  1499.     case '+':
  1500.       HANDLE_TOKEN2 ("+=", ASSIGN_MODIFY);
  1501.       HANDLE_TOKEN2 ("++", INCREMENT);
  1502.       lexptr++;
  1503.       return c;
  1504.     case '*':
  1505.       HANDLE_TOKEN2 ("*=", ASSIGN_MODIFY);
  1506.       lexptr++;
  1507.       return c;
  1508.     case '/':
  1509.       HANDLE_TOKEN2 ("/=", ASSIGN_MODIFY);
  1510.       lexptr++;
  1511.       return c;
  1512.     case '%':
  1513.       HANDLE_TOKEN2 ("%=", ASSIGN_MODIFY);
  1514.       lexptr++;
  1515.       return c;
  1516.     case '|':
  1517.       HANDLE_TOKEN2 ("|=", ASSIGN_MODIFY);
  1518.       HANDLE_TOKEN2 ("||", OROR);
  1519.       lexptr++;
  1520.       return c;
  1521.     case '&':
  1522.       HANDLE_TOKEN2 ("&=", ASSIGN_MODIFY);
  1523.       HANDLE_TOKEN2 ("&&", ANDAND);
  1524.       lexptr++;
  1525.       return c;
  1526.     case '^':
  1527.       HANDLE_TOKEN2 ("^=", ASSIGN_MODIFY);
  1528.       lexptr++;
  1529.       return c;
  1530.     case '!':
  1531.       HANDLE_TOKEN2 ("!=", NOTEQUAL);
  1532.       lexptr++;
  1533.       return c;
  1534.     case '<':
  1535.       HANDLE_TOKEN3 ("<<=", ASSIGN_MODIFY);
  1536.       HANDLE_TOKEN2 ("<=", LEQ);
  1537.       HANDLE_TOKEN2 ("<<", LSH);
  1538.       lexptr++;
  1539.       return c;
  1540.     case '>':
  1541.       HANDLE_TOKEN3 (">>=", ASSIGN_MODIFY);
  1542.       HANDLE_TOKEN2 (">=", GEQ);
  1543.       HANDLE_TOKEN2 (">>", RSH);
  1544.       lexptr++;
  1545.       return c;
  1546.     case '=':
  1547.       HANDLE_TOKEN2 ("==", EQUAL);
  1548.       lexptr++;
  1549.       return c;
  1550.     case ':':
  1551.       HANDLE_TOKEN2 ("::", COLONCOLON);
  1552.       lexptr++;
  1553.       return c;

  1554.     case '[':
  1555.     case ']':
  1556.     case '?':
  1557.     case '@':
  1558.     case '~':
  1559.     case '{':
  1560.     case '}':
  1561.     symbol:
  1562.       lexptr++;
  1563.       return c;

  1564.     case '"':
  1565.       /* These can't occur in C++ names.  */
  1566.       yyerror (_("unexpected string literal"));
  1567.       return ERROR;
  1568.     }

  1569.   if (!(c == '_' || c == '$' || ISALPHA (c)))
  1570.     {
  1571.       /* We must have come across a bad character (e.g. ';').  */
  1572.       yyerror (_("invalid character"));
  1573.       return ERROR;
  1574.     }

  1575.   /* It's a name.  See how long it is.  */
  1576.   namelen = 0;
  1577.   do
  1578.     c = tokstart[++namelen];
  1579.   while (ISALNUM (c) || c == '_' || c == '$');

  1580.   lexptr += namelen;

  1581.   /* Catch specific keywords.  Notice that some of the keywords contain
  1582.      spaces, and are sorted by the length of the first word.  They must
  1583.      all include a trailing space in the string comparison.  */
  1584.   switch (namelen)
  1585.     {
  1586.     case 16:
  1587.       if (strncmp (tokstart, "reinterpret_cast", 16) == 0)
  1588.         return REINTERPRET_CAST;
  1589.       break;
  1590.     case 12:
  1591.       if (strncmp (tokstart, "construction vtable for ", 24) == 0)
  1592.         {
  1593.           lexptr = tokstart + 24;
  1594.           return CONSTRUCTION_VTABLE;
  1595.         }
  1596.       if (strncmp (tokstart, "dynamic_cast", 12) == 0)
  1597.         return DYNAMIC_CAST;
  1598.       break;
  1599.     case 11:
  1600.       if (strncmp (tokstart, "static_cast", 11) == 0)
  1601.         return STATIC_CAST;
  1602.       break;
  1603.     case 9:
  1604.       HANDLE_SPECIAL ("covariant return thunk to ", DEMANGLE_COMPONENT_COVARIANT_THUNK);
  1605.       HANDLE_SPECIAL ("reference temporary for ", DEMANGLE_COMPONENT_REFTEMP);
  1606.       break;
  1607.     case 8:
  1608.       HANDLE_SPECIAL ("typeinfo for ", DEMANGLE_COMPONENT_TYPEINFO);
  1609.       HANDLE_SPECIAL ("typeinfo fn for ", DEMANGLE_COMPONENT_TYPEINFO_FN);
  1610.       HANDLE_SPECIAL ("typeinfo name for ", DEMANGLE_COMPONENT_TYPEINFO_NAME);
  1611.       if (strncmp (tokstart, "operator", 8) == 0)
  1612.         return OPERATOR;
  1613.       if (strncmp (tokstart, "restrict", 8) == 0)
  1614.         return RESTRICT;
  1615.       if (strncmp (tokstart, "unsigned", 8) == 0)
  1616.         return UNSIGNED;
  1617.       if (strncmp (tokstart, "template", 8) == 0)
  1618.         return TEMPLATE;
  1619.       if (strncmp (tokstart, "volatile", 8) == 0)
  1620.         return VOLATILE_KEYWORD;
  1621.       break;
  1622.     case 7:
  1623.       HANDLE_SPECIAL ("virtual thunk to ", DEMANGLE_COMPONENT_VIRTUAL_THUNK);
  1624.       if (strncmp (tokstart, "wchar_t", 7) == 0)
  1625.         return WCHAR_T;
  1626.       break;
  1627.     case 6:
  1628.       if (strncmp (tokstart, "global constructors keyed to ", 29) == 0)
  1629.         {
  1630.           const char *p;
  1631.           lexptr = tokstart + 29;
  1632.           yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
  1633.           /* Find the end of the symbol.  */
  1634.           p = symbol_end (lexptr);
  1635.           yylval.comp = make_name (lexptr, p - lexptr);
  1636.           lexptr = p;
  1637.           return DEMANGLER_SPECIAL;
  1638.         }
  1639.       if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
  1640.         {
  1641.           const char *p;
  1642.           lexptr = tokstart + 28;
  1643.           yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
  1644.           /* Find the end of the symbol.  */
  1645.           p = symbol_end (lexptr);
  1646.           yylval.comp = make_name (lexptr, p - lexptr);
  1647.           lexptr = p;
  1648.           return DEMANGLER_SPECIAL;
  1649.         }

  1650.       HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
  1651.       if (strncmp (tokstart, "delete", 6) == 0)
  1652.         return DELETE;
  1653.       if (strncmp (tokstart, "struct", 6) == 0)
  1654.         return STRUCT;
  1655.       if (strncmp (tokstart, "signed", 6) == 0)
  1656.         return SIGNED_KEYWORD;
  1657.       if (strncmp (tokstart, "sizeof", 6) == 0)
  1658.         return SIZEOF;
  1659.       if (strncmp (tokstart, "double", 6) == 0)
  1660.         return DOUBLE_KEYWORD;
  1661.       break;
  1662.     case 5:
  1663.       HANDLE_SPECIAL ("guard variable for ", DEMANGLE_COMPONENT_GUARD);
  1664.       if (strncmp (tokstart, "false", 5) == 0)
  1665.         return FALSEKEYWORD;
  1666.       if (strncmp (tokstart, "class", 5) == 0)
  1667.         return CLASS;
  1668.       if (strncmp (tokstart, "union", 5) == 0)
  1669.         return UNION;
  1670.       if (strncmp (tokstart, "float", 5) == 0)
  1671.         return FLOAT_KEYWORD;
  1672.       if (strncmp (tokstart, "short", 5) == 0)
  1673.         return SHORT;
  1674.       if (strncmp (tokstart, "const", 5) == 0)
  1675.         return CONST_KEYWORD;
  1676.       break;
  1677.     case 4:
  1678.       if (strncmp (tokstart, "void", 4) == 0)
  1679.         return VOID;
  1680.       if (strncmp (tokstart, "bool", 4) == 0)
  1681.         return BOOL;
  1682.       if (strncmp (tokstart, "char", 4) == 0)
  1683.         return CHAR;
  1684.       if (strncmp (tokstart, "enum", 4) == 0)
  1685.         return ENUM;
  1686.       if (strncmp (tokstart, "long", 4) == 0)
  1687.         return LONG;
  1688.       if (strncmp (tokstart, "true", 4) == 0)
  1689.         return TRUEKEYWORD;
  1690.       break;
  1691.     case 3:
  1692.       HANDLE_SPECIAL ("VTT for ", DEMANGLE_COMPONENT_VTT);
  1693.       HANDLE_SPECIAL ("non-virtual thunk to ", DEMANGLE_COMPONENT_THUNK);
  1694.       if (strncmp (tokstart, "new", 3) == 0)
  1695.         return NEW;
  1696.       if (strncmp (tokstart, "int", 3) == 0)
  1697.         return INT_KEYWORD;
  1698.       break;
  1699.     default:
  1700.       break;
  1701.     }

  1702.   yylval.comp = make_name (tokstart, namelen);
  1703.   return NAME;
  1704. }

  1705. static void
  1706. yyerror (char *msg)
  1707. {
  1708.   if (global_errmsg)
  1709.     return;

  1710.   error_lexptr = prev_lexptr;
  1711.   global_errmsg = msg ? msg : "parse error";
  1712. }

  1713. /* Allocate a chunk of the components we'll need to build a tree.  We
  1714.    generally allocate too many components, but the extra memory usage
  1715.    doesn't hurt because the trees are temporary and the storage is
  1716.    reused.  More may be allocated later, by d_grab.  */
  1717. static struct demangle_info *
  1718. allocate_info (void)
  1719. {
  1720.   struct demangle_info *info = malloc (sizeof (struct demangle_info));

  1721.   info->next = NULL;
  1722.   info->used = 0;
  1723.   return info;
  1724. }

  1725. /* Convert RESULT to a string.  The return value is allocated
  1726.    using xmalloc.  ESTIMATED_LEN is used only as a guide to the
  1727.    length of the result.  This functions handles a few cases that
  1728.    cplus_demangle_print does not, specifically the global destructor
  1729.    and constructor labels.  */

  1730. char *
  1731. cp_comp_to_string (struct demangle_component *result, int estimated_len)
  1732. {
  1733.   size_t err;

  1734.   return cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len,
  1735.                                &err);
  1736. }

  1737. /* A convenience function to allocate and initialize a new struct
  1738.    demangled_parse_info.  */

  1739. struct demangle_parse_info *
  1740. cp_new_demangle_parse_info (void)
  1741. {
  1742.   struct demangle_parse_info *info;

  1743.   info = malloc (sizeof (struct demangle_parse_info));
  1744.   info->info = NULL;
  1745.   info->tree = NULL;
  1746.   obstack_init (&info->obstack);

  1747.   return info;
  1748. }

  1749. /* Free any memory associated with the given PARSE_INFO.  */

  1750. void
  1751. cp_demangled_name_parse_free (struct demangle_parse_info *parse_info)
  1752. {
  1753.   struct demangle_info *info = parse_info->info;

  1754.   /* Free any allocated chunks of memory for the parse.  */
  1755.   while (info != NULL)
  1756.     {
  1757.       struct demangle_info *next = info->next;

  1758.       free (info);
  1759.       info = next;
  1760.     }

  1761.   /* Free any memory allocated during typedef replacement.  */
  1762.   obstack_free (&parse_info->obstack, NULL);

  1763.   /* Free the parser info.  */
  1764.   free (parse_info);
  1765. }

  1766. /* Merge the two parse trees given by DEST and SRC.  The parse tree
  1767.    in SRC is attached to DEST at the node represented by TARGET.
  1768.    SRC is then freed.

  1769.    NOTE 1: Since there is no API to merge obstacks, this function does
  1770.    even attempt to try it.  Fortunately, we do not (yet?) need this ability.
  1771.    The code will assert if SRC->obstack is not empty.

  1772.    NOTE 2: The string from which SRC was parsed must not be freed, since
  1773.    this function will place pointers to that string into DEST.  */

  1774. void
  1775. cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
  1776.                                struct demangle_component *target,
  1777.                                struct demangle_parse_info *src)

  1778. {
  1779.   struct demangle_info *di;

  1780.   /* Copy the SRC's parse data into DEST.  */
  1781.   *target = *src->tree;
  1782.   di = dest->info;
  1783.   while (di->next != NULL)
  1784.     di = di->next;
  1785.   di->next = src->info;

  1786.   /* Clear the (pointer to) SRC's parse data so that it is not freed when
  1787.      cp_demangled_parse_info_free is called.  */
  1788.   src->info = NULL;

  1789.   /* Free SRC.  */
  1790.   cp_demangled_name_parse_free (src);
  1791. }

  1792. /* Convert a demangled name to a demangle_component tree.  On success,
  1793.    a structure containing the root of the new tree is returned; it must
  1794.    be freed by calling cp_demangled_name_parse_free. On error, NULL is
  1795.    returned, and an error message will be set in *ERRMSG (which does
  1796.    not need to be freed).  */

  1797. struct demangle_parse_info *
  1798. cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
  1799. {
  1800.   static char errbuf[60];
  1801.   struct demangle_parse_info *result;

  1802.   prev_lexptr = lexptr = demangled_name;
  1803.   error_lexptr = NULL;
  1804.   global_errmsg = NULL;

  1805.   demangle_info = allocate_info ();

  1806.   result = cp_new_demangle_parse_info ();
  1807.   result->info = demangle_info;

  1808.   if (yyparse ())
  1809.     {
  1810.       if (global_errmsg && errmsg)
  1811.         {
  1812.           snprintf (errbuf, sizeof (errbuf) - 2, "%s, near `%s",
  1813.                     global_errmsg, error_lexptr);
  1814.           strcat (errbuf, "'");
  1815.           *errmsg = errbuf;
  1816.         }
  1817.       cp_demangled_name_parse_free (result);
  1818.       return NULL;
  1819.     }

  1820.   result->tree = global_result;
  1821.   global_result = NULL;

  1822.   return result;
  1823. }

  1824. #ifdef TEST_CPNAMES

  1825. static void
  1826. cp_print (struct demangle_component *result)
  1827. {
  1828.   char *str;
  1829.   size_t err = 0;

  1830.   str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
  1831.   if (str == NULL)
  1832.     return;

  1833.   fputs (str, stdout);

  1834.   free (str);
  1835. }

  1836. static char
  1837. trim_chars (char *lexptr, char **extra_chars)
  1838. {
  1839.   char *p = (char *) symbol_end (lexptr);
  1840.   char c = 0;

  1841.   if (*p)
  1842.     {
  1843.       c = *p;
  1844.       *p = 0;
  1845.       *extra_chars = p + 1;
  1846.     }

  1847.   return c;
  1848. }

  1849. /* When this file is built as a standalone program, xmalloc comes from
  1850.    libiberty --- in which case we have to provide xfree ourselves.  */

  1851. void
  1852. xfree (void *ptr)
  1853. {
  1854.   if (ptr != NULL)
  1855.     {
  1856.       /* Literal `free' would get translated back to xfree again.  */
  1857.       CONCAT2 (fr,ee) (ptr);
  1858.     }
  1859. }

  1860. /* GDB normally defines internal_error itself, but when this file is built
  1861.    as a standalone program, we must also provide an implementation.  */

  1862. void
  1863. internal_error (const char *file, int line, const char *fmt, ...)
  1864. {
  1865.   va_list ap;

  1866.   va_start (ap, fmt);
  1867.   fprintf (stderr, "%s:%d: internal error: ", file, line);
  1868.   vfprintf (stderr, fmt, ap);
  1869.   exit (1);
  1870. }

  1871. int
  1872. main (int argc, char **argv)
  1873. {
  1874.   char *str2, *extra_chars = "", c;
  1875.   char buf[65536];
  1876.   int arg;
  1877.   const char *errmsg;
  1878.   struct demangle_parse_info *result;

  1879.   arg = 1;
  1880.   if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
  1881.     {
  1882.       yydebug = 1;
  1883.       arg++;
  1884.     }

  1885.   if (argv[arg] == NULL)
  1886.     while (fgets (buf, 65536, stdin) != NULL)
  1887.       {
  1888.         int len;
  1889.         buf[strlen (buf) - 1] = 0;
  1890.         /* Use DMGL_VERBOSE to get expanded standard substitutions.  */
  1891.         c = trim_chars (buf, &extra_chars);
  1892.         str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
  1893.         if (str2 == NULL)
  1894.           {
  1895.             printf ("Demangling error\n");
  1896.             if (c)
  1897.               printf ("%s%c%s\n", buf, c, extra_chars);
  1898.             else
  1899.               printf ("%s\n", buf);
  1900.             continue;
  1901.           }
  1902.         result = cp_demangled_name_to_comp (str2, &errmsg);
  1903.         if (result == NULL)
  1904.           {
  1905.             fputs (errmsg, stderr);
  1906.             fputc ('\n', stderr);
  1907.             continue;
  1908.           }

  1909.         cp_print (result->tree);
  1910.         cp_demangled_name_parse_free (result);

  1911.         free (str2);
  1912.         if (c)
  1913.           {
  1914.             putchar (c);
  1915.             fputs (extra_chars, stdout);
  1916.           }
  1917.         putchar ('\n');
  1918.       }
  1919.   else
  1920.     {
  1921.       result = cp_demangled_name_to_comp (argv[arg], &errmsg);
  1922.       if (result == NULL)
  1923.         {
  1924.           fputs (errmsg, stderr);
  1925.           fputc ('\n', stderr);
  1926.           return 0;
  1927.         }
  1928.       cp_print (result->tree);
  1929.       cp_demangled_name_parse_free (result);
  1930.       putchar ('\n');
  1931.     }
  1932.   return 0;
  1933. }

  1934. #endif