1169695Skan/* Demangler component interface functions. 2169695Skan Copyright (C) 2004 Free Software Foundation, Inc. 3169695Skan Written by Ian Lance Taylor <ian@wasabisystems.com>. 4169695Skan 5169695Skan This file is part of the libiberty library, which is part of GCC. 6169695Skan 7169695Skan This file is free software; you can redistribute it and/or modify 8169695Skan it under the terms of the GNU General Public License as published by 9169695Skan the Free Software Foundation; either version 2 of the License, or 10169695Skan (at your option) any later version. 11169695Skan 12169695Skan In addition to the permissions in the GNU General Public License, the 13169695Skan Free Software Foundation gives you unlimited permission to link the 14169695Skan compiled version of this file into combinations with other programs, 15169695Skan and to distribute those combinations without any restriction coming 16169695Skan from the use of this file. (The General Public License restrictions 17169695Skan do apply in other respects; for example, they cover modification of 18169695Skan the file, and distribution when not linked into a combined 19169695Skan executable.) 20169695Skan 21169695Skan This program is distributed in the hope that it will be useful, 22169695Skan but WITHOUT ANY WARRANTY; without even the implied warranty of 23169695Skan MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24169695Skan GNU General Public License for more details. 25169695Skan 26169695Skan You should have received a copy of the GNU General Public License 27169695Skan along with this program; if not, write to the Free Software 28169695Skan Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 29169695Skan*/ 30169695Skan 31169695Skan/* This file implements a few interface functions which are provided 32169695Skan for use with struct demangle_component trees. These functions are 33169695Skan declared in demangle.h. These functions are closely tied to the 34169695Skan demangler code in cp-demangle.c, and other interface functions can 35169695Skan be found in that file. We put these functions in a separate file 36169695Skan because they are not needed by the demangler, and so we avoid 37169695Skan having them pulled in by programs which only need the 38169695Skan demangler. */ 39169695Skan 40169695Skan#ifdef HAVE_CONFIG_H 41169695Skan#include "config.h" 42169695Skan#endif 43169695Skan 44169695Skan#ifdef HAVE_STDLIB_H 45169695Skan#include <stdlib.h> 46169695Skan#endif 47169695Skan#ifdef HAVE_STRING_H 48169695Skan#include <string.h> 49169695Skan#endif 50169695Skan 51169695Skan#include "ansidecl.h" 52169695Skan#include "libiberty.h" 53169695Skan#include "demangle.h" 54169695Skan#include "cp-demangle.h" 55169695Skan 56169695Skan/* Fill in most component types. */ 57169695Skan 58169695Skanint 59169695Skancplus_demangle_fill_component (struct demangle_component *p, 60169695Skan enum demangle_component_type type, 61169695Skan struct demangle_component *left, 62169695Skan struct demangle_component *right) 63169695Skan{ 64169695Skan if (p == NULL) 65169695Skan return 0; 66169695Skan switch (type) 67169695Skan { 68169695Skan case DEMANGLE_COMPONENT_QUAL_NAME: 69169695Skan case DEMANGLE_COMPONENT_LOCAL_NAME: 70169695Skan case DEMANGLE_COMPONENT_TYPED_NAME: 71169695Skan case DEMANGLE_COMPONENT_TEMPLATE: 72169695Skan case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE: 73169695Skan case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 74169695Skan case DEMANGLE_COMPONENT_FUNCTION_TYPE: 75169695Skan case DEMANGLE_COMPONENT_ARRAY_TYPE: 76169695Skan case DEMANGLE_COMPONENT_PTRMEM_TYPE: 77169695Skan case DEMANGLE_COMPONENT_ARGLIST: 78169695Skan case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: 79169695Skan case DEMANGLE_COMPONENT_UNARY: 80169695Skan case DEMANGLE_COMPONENT_BINARY: 81169695Skan case DEMANGLE_COMPONENT_BINARY_ARGS: 82169695Skan case DEMANGLE_COMPONENT_TRINARY: 83169695Skan case DEMANGLE_COMPONENT_TRINARY_ARG1: 84169695Skan case DEMANGLE_COMPONENT_TRINARY_ARG2: 85169695Skan case DEMANGLE_COMPONENT_LITERAL: 86169695Skan case DEMANGLE_COMPONENT_LITERAL_NEG: 87169695Skan break; 88169695Skan 89169695Skan /* These component types only have one subtree. */ 90169695Skan case DEMANGLE_COMPONENT_VTABLE: 91169695Skan case DEMANGLE_COMPONENT_VTT: 92169695Skan case DEMANGLE_COMPONENT_TYPEINFO: 93169695Skan case DEMANGLE_COMPONENT_TYPEINFO_NAME: 94169695Skan case DEMANGLE_COMPONENT_TYPEINFO_FN: 95169695Skan case DEMANGLE_COMPONENT_THUNK: 96169695Skan case DEMANGLE_COMPONENT_VIRTUAL_THUNK: 97169695Skan case DEMANGLE_COMPONENT_COVARIANT_THUNK: 98169695Skan case DEMANGLE_COMPONENT_JAVA_CLASS: 99169695Skan case DEMANGLE_COMPONENT_GUARD: 100169695Skan case DEMANGLE_COMPONENT_REFTEMP: 101169695Skan case DEMANGLE_COMPONENT_RESTRICT: 102169695Skan case DEMANGLE_COMPONENT_VOLATILE: 103169695Skan case DEMANGLE_COMPONENT_CONST: 104169695Skan case DEMANGLE_COMPONENT_RESTRICT_THIS: 105169695Skan case DEMANGLE_COMPONENT_VOLATILE_THIS: 106169695Skan case DEMANGLE_COMPONENT_CONST_THIS: 107169695Skan case DEMANGLE_COMPONENT_POINTER: 108169695Skan case DEMANGLE_COMPONENT_REFERENCE: 109169695Skan case DEMANGLE_COMPONENT_COMPLEX: 110169695Skan case DEMANGLE_COMPONENT_IMAGINARY: 111169695Skan case DEMANGLE_COMPONENT_VENDOR_TYPE: 112169695Skan case DEMANGLE_COMPONENT_CAST: 113169695Skan if (right != NULL) 114169695Skan return 0; 115169695Skan break; 116169695Skan 117169695Skan default: 118169695Skan /* Other types do not use subtrees. */ 119169695Skan return 0; 120169695Skan } 121169695Skan 122169695Skan p->type = type; 123169695Skan p->u.s_binary.left = left; 124169695Skan p->u.s_binary.right = right; 125169695Skan 126169695Skan return 1; 127169695Skan} 128169695Skan 129169695Skan/* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE. */ 130169695Skan 131169695Skanint 132169695Skancplus_demangle_fill_builtin_type (struct demangle_component *p, 133169695Skan const char *type_name) 134169695Skan{ 135169695Skan int len; 136169695Skan unsigned int i; 137169695Skan 138169695Skan if (p == NULL || type_name == NULL) 139169695Skan return 0; 140169695Skan len = strlen (type_name); 141169695Skan for (i = 0; i < D_BUILTIN_TYPE_COUNT; ++i) 142169695Skan { 143169695Skan if (len == cplus_demangle_builtin_types[i].len 144169695Skan && strcmp (type_name, cplus_demangle_builtin_types[i].name) == 0) 145169695Skan { 146169695Skan p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE; 147169695Skan p->u.s_builtin.type = &cplus_demangle_builtin_types[i]; 148169695Skan return 1; 149169695Skan } 150169695Skan } 151169695Skan return 0; 152169695Skan} 153169695Skan 154169695Skan/* Fill in a DEMANGLE_COMPONENT_OPERATOR. */ 155169695Skan 156169695Skanint 157169695Skancplus_demangle_fill_operator (struct demangle_component *p, 158169695Skan const char *opname, int args) 159169695Skan{ 160169695Skan int len; 161169695Skan unsigned int i; 162169695Skan 163169695Skan if (p == NULL || opname == NULL) 164169695Skan return 0; 165169695Skan len = strlen (opname); 166169695Skan for (i = 0; cplus_demangle_operators[i].name != NULL; ++i) 167169695Skan { 168169695Skan if (len == cplus_demangle_operators[i].len 169169695Skan && args == cplus_demangle_operators[i].args 170169695Skan && strcmp (opname, cplus_demangle_operators[i].name) == 0) 171169695Skan { 172169695Skan p->type = DEMANGLE_COMPONENT_OPERATOR; 173169695Skan p->u.s_operator.op = &cplus_demangle_operators[i]; 174169695Skan return 1; 175169695Skan } 176169695Skan } 177169695Skan return 0; 178169695Skan} 179169695Skan 180169695Skan/* Translate a mangled name into components. */ 181169695Skan 182169695Skanstruct demangle_component * 183169695Skancplus_demangle_v3_components (const char *mangled, int options, void **mem) 184169695Skan{ 185169695Skan size_t len; 186169695Skan int type; 187169695Skan struct d_info di; 188169695Skan struct demangle_component *dc; 189169695Skan 190169695Skan len = strlen (mangled); 191169695Skan 192169695Skan if (mangled[0] == '_' && mangled[1] == 'Z') 193169695Skan type = 0; 194169695Skan else 195169695Skan { 196169695Skan if ((options & DMGL_TYPES) == 0) 197169695Skan return NULL; 198169695Skan type = 1; 199169695Skan } 200169695Skan 201169695Skan cplus_demangle_init_info (mangled, options, len, &di); 202169695Skan 203169695Skan di.comps = ((struct demangle_component *) 204169695Skan malloc (di.num_comps * sizeof (struct demangle_component))); 205169695Skan di.subs = ((struct demangle_component **) 206169695Skan malloc (di.num_subs * sizeof (struct demangle_component *))); 207169695Skan if (di.comps == NULL || di.subs == NULL) 208169695Skan { 209169695Skan if (di.comps != NULL) 210169695Skan free (di.comps); 211169695Skan if (di.subs != NULL) 212169695Skan free (di.subs); 213169695Skan return NULL; 214169695Skan } 215169695Skan 216169695Skan if (! type) 217169695Skan dc = cplus_demangle_mangled_name (&di, 1); 218169695Skan else 219169695Skan dc = cplus_demangle_type (&di); 220169695Skan 221169695Skan /* If DMGL_PARAMS is set, then if we didn't consume the entire 222169695Skan mangled string, then we didn't successfully demangle it. */ 223169695Skan if ((options & DMGL_PARAMS) != 0 && d_peek_char (&di) != '\0') 224169695Skan dc = NULL; 225169695Skan 226169695Skan free (di.subs); 227169695Skan 228169695Skan if (dc != NULL) 229169695Skan *mem = di.comps; 230169695Skan else 231169695Skan free (di.comps); 232169695Skan 233169695Skan return dc; 234169695Skan} 235