X-Git-Url: http://drtracing.org/?a=blobdiff_plain;ds=sidebyside;f=gdb%2Fcp-name-parser.y;h=f257370c2e9abf43b00b690907e79fc25eace4f3;hb=3a93a0c2ef1629add02bfdb6efe81015819acd35;hp=b4597ec6c744dd51023169ae98d228ce606a3371;hpb=44742d57fbfe7f4041e72db9527e72f63abdbc5e;p=deliverable%2Fbinutils-gdb.git
diff --git a/gdb/cp-name-parser.y b/gdb/cp-name-parser.y
index b4597ec6c7..f257370c2e 100644
--- a/gdb/cp-name-parser.y
+++ b/gdb/cp-name-parser.y
@@ -1,25 +1,24 @@
/* YACC parser for C++ names, for GDB.
- Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
+ Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
+ Free Software Foundation, Inc.
Parts of the lexer are based on c-exp.y from GDB.
-This file is part of 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 2 of the License, or
-(at your option) any later version.
+ 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.
+ 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, write to the Free Software
-Foundation, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA. */
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see . */
/* Note that malloc's and realloc's in this file are transformed to
xmalloc and xrealloc respectively by the same sed command in the
@@ -31,6 +30,8 @@ Boston, MA 02110-1301, USA. */
%{
+#include "defs.h"
+
#include
#include
#include
@@ -39,6 +40,8 @@ Boston, MA 02110-1301, USA. */
#include "safe-ctype.h"
#include "libiberty.h"
#include "demangle.h"
+#include "cp-support.h"
+#include "gdb_assert.h"
/* Bison does not make it easy to create a parser without global
state, unfortunately. Here are all the global variables used
@@ -58,7 +61,7 @@ static const char *lexptr, *prev_lexptr, *error_lexptr, *global_errmsg;
struct demangle_info {
int used;
- struct demangle_info *prev, *next;
+ struct demangle_info *next;
struct demangle_component comps[ALLOC_CHUNK];
};
@@ -74,7 +77,6 @@ d_grab (void)
if (demangle_info->next == NULL)
{
more = malloc (sizeof (struct demangle_info));
- more->prev = demangle_info;
more->next = NULL;
demangle_info->next = more;
}
@@ -253,10 +255,6 @@ make_name (const char *name, int len)
int fold_flag;
} abstract;
int lval;
- struct {
- int val;
- struct demangle_component *type;
- } typed_val_int;
const char *opname;
}
@@ -311,14 +309,6 @@ make_name (const char *name, int len)
/* Non-C++ things we get from the demangler. */
%token DEMANGLER_SPECIAL
%token CONSTRUCTION_VTABLE CONSTRUCTION_IN
-%token GLOBAL
-
-%{
-enum {
- GLOBAL_CONSTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 20,
- GLOBAL_DESTRUCTORS = DEMANGLE_COMPONENT_LITERAL + 21
-};
-%}
/* Precedence declarations. */
@@ -428,10 +418,6 @@ demangler_special
d_right ($$) = NULL; }
| CONSTRUCTION_VTABLE start CONSTRUCTION_IN start
{ $$ = fill_comp (DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE, $2, $4); }
- | GLOBAL
- { $$ = make_empty ($1.val);
- d_left ($$) = $1.type;
- d_right ($$) = NULL; }
;
operator : OPERATOR NEW
@@ -497,7 +483,7 @@ operator : OPERATOR NEW
| OPERATOR ARROW
{ $$ = make_operator ("->", 2); }
| OPERATOR '(' ')'
- { $$ = make_operator ("()", 0); }
+ { $$ = make_operator ("()", 2); }
| OPERATOR '[' ']'
{ $$ = make_operator ("[]", 2); }
;
@@ -1460,7 +1446,7 @@ c_parse_backslash (int host_char, int *target_char)
after the zeros. A value of 0 does not mean end of string. */
static int
-parse_escape (const char **string_ptr)
+cp_parse_escape (const char **string_ptr)
{
int target_char;
int c = *(*string_ptr)++;
@@ -1481,7 +1467,7 @@ parse_escape (const char **string_ptr)
if (c == '?')
return 0177;
else if (c == '\\')
- target_char = parse_escape (string_ptr);
+ target_char = cp_parse_escape (string_ptr);
else
target_char = c;
@@ -1555,7 +1541,7 @@ yylex (void)
{
int c;
int namelen;
- const char *tokstart, *tokptr;
+ const char *tokstart;
retry:
prev_lexptr = lexptr;
@@ -1579,17 +1565,17 @@ yylex (void)
lexptr++;
c = *lexptr++;
if (c == '\\')
- c = parse_escape (&lexptr);
+ c = cp_parse_escape (&lexptr);
else if (c == '\'')
{
- yyerror ("empty character constant");
+ yyerror (_("empty character constant"));
return ERROR;
}
c = *lexptr++;
if (c != '\'')
{
- yyerror ("invalid character constant");
+ yyerror (_("invalid character constant"));
return ERROR;
}
@@ -1713,7 +1699,7 @@ yylex (void)
memcpy (err_copy, tokstart, p - tokstart);
err_copy[p - tokstart] = 0;
- yyerror ("invalid number");
+ yyerror (_("invalid number"));
return ERROR;
}
lexptr = p;
@@ -1789,14 +1775,14 @@ yylex (void)
case '"':
/* These can't occur in C++ names. */
- yyerror ("unexpected string literal");
+ yyerror (_("unexpected string literal"));
return ERROR;
}
if (!(c == '_' || c == '$' || ISALPHA (c)))
{
/* We must have come across a bad character (e.g. ';'). */
- yyerror ("invalid character");
+ yyerror (_("invalid character"));
return ERROR;
}
@@ -1859,23 +1845,23 @@ yylex (void)
{
const char *p;
lexptr = tokstart + 29;
- yylval.typed_val_int.val = GLOBAL_CONSTRUCTORS;
+ yylval.lval = DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS;
/* Find the end of the symbol. */
p = symbol_end (lexptr);
- yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
+ yylval.comp = make_name (lexptr, p - lexptr);
lexptr = p;
- return GLOBAL;
+ return DEMANGLER_SPECIAL;
}
if (strncmp (tokstart, "global destructors keyed to ", 28) == 0)
{
const char *p;
lexptr = tokstart + 28;
- yylval.typed_val_int.val = GLOBAL_DESTRUCTORS;
+ yylval.lval = DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS;
/* Find the end of the symbol. */
p = symbol_end (lexptr);
- yylval.typed_val_int.type = make_name (lexptr, p - lexptr);
+ yylval.comp = make_name (lexptr, p - lexptr);
lexptr = p;
- return GLOBAL;
+ return DEMANGLER_SPECIAL;
}
HANDLE_SPECIAL ("vtable for ", DEMANGLE_COMPONENT_VTABLE);
@@ -1949,20 +1935,14 @@ yyerror (char *msg)
generally allocate too many components, but the extra memory usage
doesn't hurt because the trees are temporary and the storage is
reused. More may be allocated later, by d_grab. */
-static void
+static struct demangle_info *
allocate_info (void)
{
- if (demangle_info == NULL)
- {
- demangle_info = malloc (sizeof (struct demangle_info));
- demangle_info->prev = NULL;
- demangle_info->next = NULL;
- }
- else
- while (demangle_info->prev)
- demangle_info = demangle_info->prev;
+ struct demangle_info *info = malloc (sizeof (struct demangle_info));
- demangle_info->used = 0;
+ info->next = NULL;
+ info->used = 0;
+ return info;
}
/* Convert RESULT to a string. The return value is allocated
@@ -1974,51 +1954,108 @@ allocate_info (void)
char *
cp_comp_to_string (struct demangle_component *result, int estimated_len)
{
- char *str, *prefix = NULL, *buf;
- size_t err = 0;
+ size_t err;
- if (result->type == GLOBAL_DESTRUCTORS)
- {
- result = d_left (result);
- prefix = "global destructors keyed to ";
- }
- else if (result->type == GLOBAL_CONSTRUCTORS)
+ return cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len,
+ &err);
+}
+
+/* A convenience function to allocate and initialize a new struct
+ demangled_parse_info. */
+
+struct demangle_parse_info *
+cp_new_demangle_parse_info (void)
+{
+ struct demangle_parse_info *info;
+
+ info = malloc (sizeof (struct demangle_parse_info));
+ info->info = NULL;
+ info->tree = NULL;
+ obstack_init (&info->obstack);
+
+ return info;
+}
+
+/* Free any memory associated with the given PARSE_INFO. */
+
+void
+cp_demangled_name_parse_free (struct demangle_parse_info *parse_info)
+{
+ struct demangle_info *info = parse_info->info;
+
+ /* Free any allocated chunks of memory for the parse. */
+ while (info != NULL)
{
- result = d_left (result);
- prefix = "global constructors keyed to ";
+ struct demangle_info *next = info->next;
+
+ free (info);
+ info = next;
}
- str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, estimated_len, &err);
- if (str == NULL)
- return NULL;
+ /* Free any memory allocated during typedef replacement. */
+ obstack_free (&parse_info->obstack, NULL);
+
+ /* Free the parser info. */
+ free (parse_info);
+}
- if (prefix == NULL)
- return str;
+/* Merge the two parse trees given by DEST and SRC. The parse tree
+ in SRC is attached to DEST at the node represented by TARGET.
+ SRC is then freed.
- buf = malloc (strlen (str) + strlen (prefix) + 1);
- strcpy (buf, prefix);
- strcat (buf, str);
- free (str);
- return (buf);
+ NOTE 1: Since there is no API to merge obstacks, this function does
+ even attempt to try it. Fortunately, we do not (yet?) need this ability.
+ The code will assert if SRC->obstack is not empty.
+
+ NOTE 2: The string from which SRC was parsed must not be freed, since
+ this function will place pointers to that string into DEST. */
+
+void
+cp_merge_demangle_parse_infos (struct demangle_parse_info *dest,
+ struct demangle_component *target,
+ struct demangle_parse_info *src)
+
+{
+ struct demangle_info *di;
+
+ /* Copy the SRC's parse data into DEST. */
+ *target = *src->tree;
+ di = dest->info;
+ while (di->next != NULL)
+ di = di->next;
+ di->next = src->info;
+
+ /* Clear the (pointer to) SRC's parse data so that it is not freed when
+ cp_demangled_parse_info_free is called. */
+ src->info = NULL;
+
+ /* Assert if the SRC obstack is not empty. */
+ gdb_assert (obstack_empty_p (&src->obstack));
+
+ /* Free SRC. */
+ cp_demangled_name_parse_free (src);
}
/* Convert a demangled name to a demangle_component tree. On success,
- the root of the new tree is returned; it is valid until the next
- call to this function and should not be freed. On error, NULL is
+ a structure containing the root of the new tree is returned; it must
+ be freed by calling cp_demangled_name_parse_free. On error, NULL is
returned, and an error message will be set in *ERRMSG (which does
not need to be freed). */
-struct demangle_component *
+struct demangle_parse_info *
cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
{
static char errbuf[60];
- struct demangle_component *result;
+ struct demangle_parse_info *result;
prev_lexptr = lexptr = demangled_name;
error_lexptr = NULL;
global_errmsg = NULL;
- allocate_info ();
+ demangle_info = allocate_info ();
+
+ result = cp_new_demangle_parse_info ();
+ result->info = demangle_info;
if (yyparse ())
{
@@ -2029,10 +2066,11 @@ cp_demangled_name_to_comp (const char *demangled_name, const char **errmsg)
strcat (errbuf, "'");
*errmsg = errbuf;
}
+ cp_demangled_name_parse_free (result);
return NULL;
}
- result = global_result;
+ result->tree = global_result;
global_result = NULL;
return result;
@@ -2046,17 +2084,6 @@ cp_print (struct demangle_component *result)
char *str;
size_t err = 0;
- if (result->type == GLOBAL_DESTRUCTORS)
- {
- result = d_left (result);
- fputs ("global destructors keyed to ", stdout);
- }
- else if (result->type == GLOBAL_CONSTRUCTORS)
- {
- result = d_left (result);
- fputs ("global constructors keyed to ", stdout);
- }
-
str = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, result, 64, &err);
if (str == NULL)
return;
@@ -2082,6 +2109,33 @@ trim_chars (char *lexptr, char **extra_chars)
return c;
}
+/* When this file is built as a standalone program, xmalloc comes from
+ libiberty --- in which case we have to provide xfree ourselves. */
+
+void
+xfree (void *ptr)
+{
+ if (ptr != NULL)
+ {
+ /* Literal `free' would get translated back to xfree again. */
+ CONCAT2 (fr,ee) (ptr);
+ }
+}
+
+/* GDB normally defines internal_error itself, but when this file is built
+ as a standalone program, we must also provide an implementation. */
+
+void
+internal_error (const char *file, int line, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start (ap, fmt);
+ fprintf (stderr, "%s:%d: internal error: ", file, line);
+ vfprintf (stderr, fmt, ap);
+ exit (1);
+}
+
int
main (int argc, char **argv)
{
@@ -2089,7 +2143,7 @@ main (int argc, char **argv)
char buf[65536];
int arg;
const char *errmsg;
- struct demangle_component *result;
+ struct demangle_parse_info *result;
arg = 1;
if (argv[arg] && strcmp (argv[arg], "--debug") == 0)
@@ -2108,7 +2162,7 @@ main (int argc, char **argv)
str2 = cplus_demangle (buf, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE);
if (str2 == NULL)
{
- /* printf ("Demangling error\n"); */
+ printf ("Demangling error\n");
if (c)
printf ("%s%c%s\n", buf, c, extra_chars);
else
@@ -2123,7 +2177,8 @@ main (int argc, char **argv)
continue;
}
- cp_print (result);
+ cp_print (result->tree);
+ cp_demangled_name_parse_free (result);
free (str2);
if (c)
@@ -2142,7 +2197,8 @@ main (int argc, char **argv)
fputc ('\n', stderr);
return 0;
}
- cp_print (result);
+ cp_print (result->tree);
+ cp_demangled_name_parse_free (result);
putchar ('\n');
}
return 0;