16ca2c52aSchristos /* Demangler component interface functions.
2*184b2d41Schristos Copyright (C) 2004-2020 Free Software Foundation, Inc.
36ca2c52aSchristos Written by Ian Lance Taylor <ian@wasabisystems.com>.
46ca2c52aSchristos
56ca2c52aSchristos This file is part of the libiberty library, which is part of GCC.
66ca2c52aSchristos
76ca2c52aSchristos This file is free software; you can redistribute it and/or modify
86ca2c52aSchristos it under the terms of the GNU General Public License as published by
96ca2c52aSchristos the Free Software Foundation; either version 2 of the License, or
106ca2c52aSchristos (at your option) any later version.
116ca2c52aSchristos
126ca2c52aSchristos In addition to the permissions in the GNU General Public License, the
136ca2c52aSchristos Free Software Foundation gives you unlimited permission to link the
146ca2c52aSchristos compiled version of this file into combinations with other programs,
156ca2c52aSchristos and to distribute those combinations without any restriction coming
166ca2c52aSchristos from the use of this file. (The General Public License restrictions
176ca2c52aSchristos do apply in other respects; for example, they cover modification of
186ca2c52aSchristos the file, and distribution when not linked into a combined
196ca2c52aSchristos executable.)
206ca2c52aSchristos
216ca2c52aSchristos This program is distributed in the hope that it will be useful,
226ca2c52aSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
236ca2c52aSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
246ca2c52aSchristos GNU General Public License for more details.
256ca2c52aSchristos
266ca2c52aSchristos You should have received a copy of the GNU General Public License
276ca2c52aSchristos along with this program; if not, write to the Free Software
286ca2c52aSchristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
296ca2c52aSchristos */
306ca2c52aSchristos
316ca2c52aSchristos /* This file implements a few interface functions which are provided
326ca2c52aSchristos for use with struct demangle_component trees. These functions are
336ca2c52aSchristos declared in demangle.h. These functions are closely tied to the
346ca2c52aSchristos demangler code in cp-demangle.c, and other interface functions can
356ca2c52aSchristos be found in that file. We put these functions in a separate file
366ca2c52aSchristos because they are not needed by the demangler, and so we avoid
376ca2c52aSchristos having them pulled in by programs which only need the
386ca2c52aSchristos demangler. */
396ca2c52aSchristos
406ca2c52aSchristos #ifdef HAVE_CONFIG_H
416ca2c52aSchristos #include "config.h"
426ca2c52aSchristos #endif
436ca2c52aSchristos
446ca2c52aSchristos #ifdef HAVE_STDLIB_H
456ca2c52aSchristos #include <stdlib.h>
466ca2c52aSchristos #endif
476ca2c52aSchristos #ifdef HAVE_STRING_H
486ca2c52aSchristos #include <string.h>
496ca2c52aSchristos #endif
506ca2c52aSchristos
516ca2c52aSchristos #include "ansidecl.h"
526ca2c52aSchristos #include "libiberty.h"
536ca2c52aSchristos #include "demangle.h"
546ca2c52aSchristos #include "cp-demangle.h"
556ca2c52aSchristos
566ca2c52aSchristos /* Fill in most component types. */
576ca2c52aSchristos
586ca2c52aSchristos int
cplus_demangle_fill_component(struct demangle_component * p,enum demangle_component_type type,struct demangle_component * left,struct demangle_component * right)596ca2c52aSchristos cplus_demangle_fill_component (struct demangle_component *p,
606ca2c52aSchristos enum demangle_component_type type,
616ca2c52aSchristos struct demangle_component *left,
626ca2c52aSchristos struct demangle_component *right)
636ca2c52aSchristos {
646ca2c52aSchristos if (p == NULL)
656ca2c52aSchristos return 0;
666ca2c52aSchristos switch (type)
676ca2c52aSchristos {
686ca2c52aSchristos case DEMANGLE_COMPONENT_QUAL_NAME:
696ca2c52aSchristos case DEMANGLE_COMPONENT_LOCAL_NAME:
706ca2c52aSchristos case DEMANGLE_COMPONENT_TYPED_NAME:
716ca2c52aSchristos case DEMANGLE_COMPONENT_TEMPLATE:
726ca2c52aSchristos case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
736ca2c52aSchristos case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
746ca2c52aSchristos case DEMANGLE_COMPONENT_FUNCTION_TYPE:
756ca2c52aSchristos case DEMANGLE_COMPONENT_ARRAY_TYPE:
766ca2c52aSchristos case DEMANGLE_COMPONENT_PTRMEM_TYPE:
776ca2c52aSchristos case DEMANGLE_COMPONENT_ARGLIST:
786ca2c52aSchristos case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
796ca2c52aSchristos case DEMANGLE_COMPONENT_UNARY:
806ca2c52aSchristos case DEMANGLE_COMPONENT_BINARY:
816ca2c52aSchristos case DEMANGLE_COMPONENT_BINARY_ARGS:
826ca2c52aSchristos case DEMANGLE_COMPONENT_TRINARY:
836ca2c52aSchristos case DEMANGLE_COMPONENT_TRINARY_ARG1:
846ca2c52aSchristos case DEMANGLE_COMPONENT_TRINARY_ARG2:
856ca2c52aSchristos case DEMANGLE_COMPONENT_LITERAL:
866ca2c52aSchristos case DEMANGLE_COMPONENT_LITERAL_NEG:
876ca2c52aSchristos break;
886ca2c52aSchristos
896ca2c52aSchristos /* These component types only have one subtree. */
906ca2c52aSchristos case DEMANGLE_COMPONENT_VTABLE:
916ca2c52aSchristos case DEMANGLE_COMPONENT_VTT:
926ca2c52aSchristos case DEMANGLE_COMPONENT_TYPEINFO:
936ca2c52aSchristos case DEMANGLE_COMPONENT_TYPEINFO_NAME:
946ca2c52aSchristos case DEMANGLE_COMPONENT_TYPEINFO_FN:
956ca2c52aSchristos case DEMANGLE_COMPONENT_THUNK:
966ca2c52aSchristos case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
976ca2c52aSchristos case DEMANGLE_COMPONENT_COVARIANT_THUNK:
986ca2c52aSchristos case DEMANGLE_COMPONENT_JAVA_CLASS:
996ca2c52aSchristos case DEMANGLE_COMPONENT_GUARD:
1006ca2c52aSchristos case DEMANGLE_COMPONENT_REFTEMP:
1016ca2c52aSchristos case DEMANGLE_COMPONENT_RESTRICT:
1026ca2c52aSchristos case DEMANGLE_COMPONENT_VOLATILE:
1036ca2c52aSchristos case DEMANGLE_COMPONENT_CONST:
1046ca2c52aSchristos case DEMANGLE_COMPONENT_RESTRICT_THIS:
1056ca2c52aSchristos case DEMANGLE_COMPONENT_VOLATILE_THIS:
1066ca2c52aSchristos case DEMANGLE_COMPONENT_CONST_THIS:
1076ca2c52aSchristos case DEMANGLE_COMPONENT_POINTER:
1086ca2c52aSchristos case DEMANGLE_COMPONENT_REFERENCE:
10915d8e94aSchristos case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
1106ca2c52aSchristos case DEMANGLE_COMPONENT_COMPLEX:
1116ca2c52aSchristos case DEMANGLE_COMPONENT_IMAGINARY:
1126ca2c52aSchristos case DEMANGLE_COMPONENT_VENDOR_TYPE:
1136ca2c52aSchristos case DEMANGLE_COMPONENT_CAST:
1146ca2c52aSchristos case DEMANGLE_COMPONENT_CONVERSION:
1156ca2c52aSchristos if (right != NULL)
1166ca2c52aSchristos return 0;
1176ca2c52aSchristos break;
1186ca2c52aSchristos
1196ca2c52aSchristos default:
1206ca2c52aSchristos /* Other types do not use subtrees. */
1216ca2c52aSchristos return 0;
1226ca2c52aSchristos }
1236ca2c52aSchristos
1246ca2c52aSchristos p->type = type;
1256ca2c52aSchristos p->u.s_binary.left = left;
1266ca2c52aSchristos p->u.s_binary.right = right;
12715d8e94aSchristos p->d_printing = 0;
128*184b2d41Schristos p->d_counting = 0;
1296ca2c52aSchristos
1306ca2c52aSchristos return 1;
1316ca2c52aSchristos }
1326ca2c52aSchristos
1336ca2c52aSchristos /* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE. */
1346ca2c52aSchristos
1356ca2c52aSchristos int
cplus_demangle_fill_builtin_type(struct demangle_component * p,const char * type_name)1366ca2c52aSchristos cplus_demangle_fill_builtin_type (struct demangle_component *p,
1376ca2c52aSchristos const char *type_name)
1386ca2c52aSchristos {
1396ca2c52aSchristos int len;
1406ca2c52aSchristos unsigned int i;
1416ca2c52aSchristos
1426ca2c52aSchristos if (p == NULL || type_name == NULL)
1436ca2c52aSchristos return 0;
1446ca2c52aSchristos len = strlen (type_name);
1456ca2c52aSchristos for (i = 0; i < D_BUILTIN_TYPE_COUNT; ++i)
1466ca2c52aSchristos {
1476ca2c52aSchristos if (len == cplus_demangle_builtin_types[i].len
1486ca2c52aSchristos && strcmp (type_name, cplus_demangle_builtin_types[i].name) == 0)
1496ca2c52aSchristos {
1506ca2c52aSchristos p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1516ca2c52aSchristos p->u.s_builtin.type = &cplus_demangle_builtin_types[i];
15215d8e94aSchristos p->d_printing = 0;
153*184b2d41Schristos p->d_counting = 0;
1546ca2c52aSchristos return 1;
1556ca2c52aSchristos }
1566ca2c52aSchristos }
1576ca2c52aSchristos return 0;
1586ca2c52aSchristos }
1596ca2c52aSchristos
1606ca2c52aSchristos /* Fill in a DEMANGLE_COMPONENT_OPERATOR. */
1616ca2c52aSchristos
1626ca2c52aSchristos int
cplus_demangle_fill_operator(struct demangle_component * p,const char * opname,int args)1636ca2c52aSchristos cplus_demangle_fill_operator (struct demangle_component *p,
1646ca2c52aSchristos const char *opname, int args)
1656ca2c52aSchristos {
1666ca2c52aSchristos int len;
1676ca2c52aSchristos unsigned int i;
1686ca2c52aSchristos
1696ca2c52aSchristos if (p == NULL || opname == NULL)
1706ca2c52aSchristos return 0;
1716ca2c52aSchristos len = strlen (opname);
1726ca2c52aSchristos for (i = 0; cplus_demangle_operators[i].name != NULL; ++i)
1736ca2c52aSchristos {
1746ca2c52aSchristos if (len == cplus_demangle_operators[i].len
1756ca2c52aSchristos && args == cplus_demangle_operators[i].args
1766ca2c52aSchristos && strcmp (opname, cplus_demangle_operators[i].name) == 0)
1776ca2c52aSchristos {
1786ca2c52aSchristos p->type = DEMANGLE_COMPONENT_OPERATOR;
1796ca2c52aSchristos p->u.s_operator.op = &cplus_demangle_operators[i];
18015d8e94aSchristos p->d_printing = 0;
181*184b2d41Schristos p->d_counting = 0;
1826ca2c52aSchristos return 1;
1836ca2c52aSchristos }
1846ca2c52aSchristos }
1856ca2c52aSchristos return 0;
1866ca2c52aSchristos }
1876ca2c52aSchristos
1886ca2c52aSchristos /* Translate a mangled name into components. */
1896ca2c52aSchristos
1906ca2c52aSchristos struct demangle_component *
cplus_demangle_v3_components(const char * mangled,int options,void ** mem)1916ca2c52aSchristos cplus_demangle_v3_components (const char *mangled, int options, void **mem)
1926ca2c52aSchristos {
1936ca2c52aSchristos size_t len;
1946ca2c52aSchristos int type;
1956ca2c52aSchristos struct d_info di;
1966ca2c52aSchristos struct demangle_component *dc;
1976ca2c52aSchristos
1986ca2c52aSchristos len = strlen (mangled);
1996ca2c52aSchristos
2006ca2c52aSchristos if (mangled[0] == '_' && mangled[1] == 'Z')
2016ca2c52aSchristos type = 0;
2026ca2c52aSchristos else
2036ca2c52aSchristos {
2046ca2c52aSchristos if ((options & DMGL_TYPES) == 0)
2056ca2c52aSchristos return NULL;
2066ca2c52aSchristos type = 1;
2076ca2c52aSchristos }
2086ca2c52aSchristos
2096ca2c52aSchristos cplus_demangle_init_info (mangled, options, len, &di);
2106ca2c52aSchristos
2116ca2c52aSchristos di.comps = ((struct demangle_component *)
2126ca2c52aSchristos malloc (di.num_comps * sizeof (struct demangle_component)));
2136ca2c52aSchristos di.subs = ((struct demangle_component **)
2146ca2c52aSchristos malloc (di.num_subs * sizeof (struct demangle_component *)));
2156ca2c52aSchristos if (di.comps == NULL || di.subs == NULL)
2166ca2c52aSchristos {
2176ca2c52aSchristos free (di.comps);
2186ca2c52aSchristos free (di.subs);
2196ca2c52aSchristos return NULL;
2206ca2c52aSchristos }
2216ca2c52aSchristos
2226ca2c52aSchristos if (! type)
2236ca2c52aSchristos dc = cplus_demangle_mangled_name (&di, 1);
2246ca2c52aSchristos else
2256ca2c52aSchristos dc = cplus_demangle_type (&di);
2266ca2c52aSchristos
2276ca2c52aSchristos /* If DMGL_PARAMS is set, then if we didn't consume the entire
2286ca2c52aSchristos mangled string, then we didn't successfully demangle it. */
2296ca2c52aSchristos if ((options & DMGL_PARAMS) != 0 && d_peek_char (&di) != '\0')
2306ca2c52aSchristos dc = NULL;
2316ca2c52aSchristos
2326ca2c52aSchristos free (di.subs);
2336ca2c52aSchristos
2346ca2c52aSchristos if (dc != NULL)
2356ca2c52aSchristos *mem = di.comps;
2366ca2c52aSchristos else
2376ca2c52aSchristos free (di.comps);
2386ca2c52aSchristos
2396ca2c52aSchristos return dc;
2406ca2c52aSchristos }
241