12a6b7db3Sskrll /* Demangler component interface functions.
2*f22f0ef4Schristos    Copyright (C) 2004-2022 Free Software Foundation, Inc.
32a6b7db3Sskrll    Written by Ian Lance Taylor <ian@wasabisystems.com>.
42a6b7db3Sskrll 
52a6b7db3Sskrll    This file is part of the libiberty library, which is part of GCC.
62a6b7db3Sskrll 
72a6b7db3Sskrll    This file is free software; you can redistribute it and/or modify
82a6b7db3Sskrll    it under the terms of the GNU General Public License as published by
92a6b7db3Sskrll    the Free Software Foundation; either version 2 of the License, or
102a6b7db3Sskrll    (at your option) any later version.
112a6b7db3Sskrll 
122a6b7db3Sskrll    In addition to the permissions in the GNU General Public License, the
132a6b7db3Sskrll    Free Software Foundation gives you unlimited permission to link the
142a6b7db3Sskrll    compiled version of this file into combinations with other programs,
152a6b7db3Sskrll    and to distribute those combinations without any restriction coming
162a6b7db3Sskrll    from the use of this file.  (The General Public License restrictions
172a6b7db3Sskrll    do apply in other respects; for example, they cover modification of
182a6b7db3Sskrll    the file, and distribution when not linked into a combined
192a6b7db3Sskrll    executable.)
202a6b7db3Sskrll 
212a6b7db3Sskrll    This program is distributed in the hope that it will be useful,
222a6b7db3Sskrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
232a6b7db3Sskrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
242a6b7db3Sskrll    GNU General Public License for more details.
252a6b7db3Sskrll 
262a6b7db3Sskrll    You should have received a copy of the GNU General Public License
272a6b7db3Sskrll    along with this program; if not, write to the Free Software
282a6b7db3Sskrll    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
292a6b7db3Sskrll */
302a6b7db3Sskrll 
312a6b7db3Sskrll /* This file implements a few interface functions which are provided
322a6b7db3Sskrll    for use with struct demangle_component trees.  These functions are
332a6b7db3Sskrll    declared in demangle.h.  These functions are closely tied to the
342a6b7db3Sskrll    demangler code in cp-demangle.c, and other interface functions can
352a6b7db3Sskrll    be found in that file.  We put these functions in a separate file
362a6b7db3Sskrll    because they are not needed by the demangler, and so we avoid
372a6b7db3Sskrll    having them pulled in by programs which only need the
382a6b7db3Sskrll    demangler.  */
392a6b7db3Sskrll 
402a6b7db3Sskrll #ifdef HAVE_CONFIG_H
412a6b7db3Sskrll #include "config.h"
422a6b7db3Sskrll #endif
432a6b7db3Sskrll 
442a6b7db3Sskrll #ifdef HAVE_STDLIB_H
452a6b7db3Sskrll #include <stdlib.h>
462a6b7db3Sskrll #endif
472a6b7db3Sskrll #ifdef HAVE_STRING_H
482a6b7db3Sskrll #include <string.h>
492a6b7db3Sskrll #endif
502a6b7db3Sskrll 
512a6b7db3Sskrll #include "ansidecl.h"
522a6b7db3Sskrll #include "libiberty.h"
532a6b7db3Sskrll #include "demangle.h"
542a6b7db3Sskrll #include "cp-demangle.h"
552a6b7db3Sskrll 
562a6b7db3Sskrll /* Fill in most component types.  */
572a6b7db3Sskrll 
582a6b7db3Sskrll int
cplus_demangle_fill_component(struct demangle_component * p,enum demangle_component_type type,struct demangle_component * left,struct demangle_component * right)592a6b7db3Sskrll cplus_demangle_fill_component (struct demangle_component *p,
602a6b7db3Sskrll                                enum demangle_component_type type,
612a6b7db3Sskrll                                struct demangle_component *left,
622a6b7db3Sskrll                                 struct demangle_component *right)
632a6b7db3Sskrll {
642a6b7db3Sskrll   if (p == NULL)
652a6b7db3Sskrll     return 0;
662a6b7db3Sskrll   switch (type)
672a6b7db3Sskrll     {
682a6b7db3Sskrll     case DEMANGLE_COMPONENT_QUAL_NAME:
692a6b7db3Sskrll     case DEMANGLE_COMPONENT_LOCAL_NAME:
702a6b7db3Sskrll     case DEMANGLE_COMPONENT_TYPED_NAME:
712a6b7db3Sskrll     case DEMANGLE_COMPONENT_TEMPLATE:
722a6b7db3Sskrll     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
732a6b7db3Sskrll     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
742a6b7db3Sskrll     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
752a6b7db3Sskrll     case DEMANGLE_COMPONENT_ARRAY_TYPE:
762a6b7db3Sskrll     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
772a6b7db3Sskrll     case DEMANGLE_COMPONENT_ARGLIST:
782a6b7db3Sskrll     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
792a6b7db3Sskrll     case DEMANGLE_COMPONENT_UNARY:
802a6b7db3Sskrll     case DEMANGLE_COMPONENT_BINARY:
812a6b7db3Sskrll     case DEMANGLE_COMPONENT_BINARY_ARGS:
822a6b7db3Sskrll     case DEMANGLE_COMPONENT_TRINARY:
832a6b7db3Sskrll     case DEMANGLE_COMPONENT_TRINARY_ARG1:
842a6b7db3Sskrll     case DEMANGLE_COMPONENT_TRINARY_ARG2:
852a6b7db3Sskrll     case DEMANGLE_COMPONENT_LITERAL:
862a6b7db3Sskrll     case DEMANGLE_COMPONENT_LITERAL_NEG:
872a6b7db3Sskrll       break;
882a6b7db3Sskrll 
892a6b7db3Sskrll       /* These component types only have one subtree.  */
902a6b7db3Sskrll     case DEMANGLE_COMPONENT_VTABLE:
912a6b7db3Sskrll     case DEMANGLE_COMPONENT_VTT:
922a6b7db3Sskrll     case DEMANGLE_COMPONENT_TYPEINFO:
932a6b7db3Sskrll     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
942a6b7db3Sskrll     case DEMANGLE_COMPONENT_TYPEINFO_FN:
952a6b7db3Sskrll     case DEMANGLE_COMPONENT_THUNK:
962a6b7db3Sskrll     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
972a6b7db3Sskrll     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
982a6b7db3Sskrll     case DEMANGLE_COMPONENT_JAVA_CLASS:
992a6b7db3Sskrll     case DEMANGLE_COMPONENT_GUARD:
1002a6b7db3Sskrll     case DEMANGLE_COMPONENT_REFTEMP:
1012a6b7db3Sskrll     case DEMANGLE_COMPONENT_RESTRICT:
1022a6b7db3Sskrll     case DEMANGLE_COMPONENT_VOLATILE:
1032a6b7db3Sskrll     case DEMANGLE_COMPONENT_CONST:
1042a6b7db3Sskrll     case DEMANGLE_COMPONENT_RESTRICT_THIS:
1052a6b7db3Sskrll     case DEMANGLE_COMPONENT_VOLATILE_THIS:
1062a6b7db3Sskrll     case DEMANGLE_COMPONENT_CONST_THIS:
1072a6b7db3Sskrll     case DEMANGLE_COMPONENT_POINTER:
1082a6b7db3Sskrll     case DEMANGLE_COMPONENT_REFERENCE:
10998f124a6Schristos     case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
1102a6b7db3Sskrll     case DEMANGLE_COMPONENT_COMPLEX:
1112a6b7db3Sskrll     case DEMANGLE_COMPONENT_IMAGINARY:
1122a6b7db3Sskrll     case DEMANGLE_COMPONENT_VENDOR_TYPE:
1132a6b7db3Sskrll     case DEMANGLE_COMPONENT_CAST:
11475f9f1baSchristos     case DEMANGLE_COMPONENT_CONVERSION:
1152a6b7db3Sskrll       if (right != NULL)
1162a6b7db3Sskrll 	return 0;
1172a6b7db3Sskrll       break;
1182a6b7db3Sskrll 
1192a6b7db3Sskrll     default:
1202a6b7db3Sskrll       /* Other types do not use subtrees.  */
1212a6b7db3Sskrll       return 0;
1222a6b7db3Sskrll     }
1232a6b7db3Sskrll 
1242a6b7db3Sskrll   p->type = type;
1252a6b7db3Sskrll   p->u.s_binary.left = left;
1262a6b7db3Sskrll   p->u.s_binary.right = right;
12798f124a6Schristos   p->d_printing = 0;
128f7172901Schristos   p->d_counting = 0;
1292a6b7db3Sskrll 
1302a6b7db3Sskrll   return 1;
1312a6b7db3Sskrll }
1322a6b7db3Sskrll 
1332a6b7db3Sskrll /* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE.  */
1342a6b7db3Sskrll 
1352a6b7db3Sskrll int
cplus_demangle_fill_builtin_type(struct demangle_component * p,const char * type_name)1362a6b7db3Sskrll cplus_demangle_fill_builtin_type (struct demangle_component *p,
1372a6b7db3Sskrll                                   const char *type_name)
1382a6b7db3Sskrll {
1392a6b7db3Sskrll   int len;
1402a6b7db3Sskrll   unsigned int i;
1412a6b7db3Sskrll 
1422a6b7db3Sskrll   if (p == NULL || type_name == NULL)
1432a6b7db3Sskrll     return 0;
1442a6b7db3Sskrll   len = strlen (type_name);
1452a6b7db3Sskrll   for (i = 0; i < D_BUILTIN_TYPE_COUNT; ++i)
1462a6b7db3Sskrll     {
1472a6b7db3Sskrll       if (len == cplus_demangle_builtin_types[i].len
1482a6b7db3Sskrll 	  && strcmp (type_name, cplus_demangle_builtin_types[i].name) == 0)
1492a6b7db3Sskrll 	{
1502a6b7db3Sskrll 	  p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
1512a6b7db3Sskrll 	  p->u.s_builtin.type = &cplus_demangle_builtin_types[i];
15298f124a6Schristos 	  p->d_printing = 0;
153f7172901Schristos 	  p->d_counting = 0;
1542a6b7db3Sskrll 	  return 1;
1552a6b7db3Sskrll 	}
1562a6b7db3Sskrll     }
1572a6b7db3Sskrll   return 0;
1582a6b7db3Sskrll }
1592a6b7db3Sskrll 
1602a6b7db3Sskrll /* Fill in a DEMANGLE_COMPONENT_OPERATOR.  */
1612a6b7db3Sskrll 
1622a6b7db3Sskrll int
cplus_demangle_fill_operator(struct demangle_component * p,const char * opname,int args)1632a6b7db3Sskrll cplus_demangle_fill_operator (struct demangle_component *p,
1642a6b7db3Sskrll                               const char *opname, int args)
1652a6b7db3Sskrll {
1662a6b7db3Sskrll   int len;
1672a6b7db3Sskrll   unsigned int i;
1682a6b7db3Sskrll 
1692a6b7db3Sskrll   if (p == NULL || opname == NULL)
1702a6b7db3Sskrll     return 0;
1712a6b7db3Sskrll   len = strlen (opname);
1722a6b7db3Sskrll   for (i = 0; cplus_demangle_operators[i].name != NULL; ++i)
1732a6b7db3Sskrll     {
1742a6b7db3Sskrll       if (len == cplus_demangle_operators[i].len
1752a6b7db3Sskrll 	  && args == cplus_demangle_operators[i].args
1762a6b7db3Sskrll 	  && strcmp (opname, cplus_demangle_operators[i].name) == 0)
1772a6b7db3Sskrll 	{
1782a6b7db3Sskrll 	  p->type = DEMANGLE_COMPONENT_OPERATOR;
1792a6b7db3Sskrll 	  p->u.s_operator.op = &cplus_demangle_operators[i];
18098f124a6Schristos 	  p->d_printing = 0;
181f7172901Schristos 	  p->d_counting = 0;
1822a6b7db3Sskrll 	  return 1;
1832a6b7db3Sskrll 	}
1842a6b7db3Sskrll     }
1852a6b7db3Sskrll   return 0;
1862a6b7db3Sskrll }
1872a6b7db3Sskrll 
1882a6b7db3Sskrll /* Translate a mangled name into components.  */
1892a6b7db3Sskrll 
1902a6b7db3Sskrll struct demangle_component *
cplus_demangle_v3_components(const char * mangled,int options,void ** mem)1912a6b7db3Sskrll cplus_demangle_v3_components (const char *mangled, int options, void **mem)
1922a6b7db3Sskrll {
1932a6b7db3Sskrll   size_t len;
1942a6b7db3Sskrll   int type;
1952a6b7db3Sskrll   struct d_info di;
1962a6b7db3Sskrll   struct demangle_component *dc;
1972a6b7db3Sskrll 
1982a6b7db3Sskrll   len = strlen (mangled);
1992a6b7db3Sskrll 
2002a6b7db3Sskrll   if (mangled[0] == '_' && mangled[1] == 'Z')
2012a6b7db3Sskrll     type = 0;
2022a6b7db3Sskrll   else
2032a6b7db3Sskrll     {
2042a6b7db3Sskrll       if ((options & DMGL_TYPES) == 0)
2052a6b7db3Sskrll 	return NULL;
2062a6b7db3Sskrll       type = 1;
2072a6b7db3Sskrll     }
2082a6b7db3Sskrll 
2092a6b7db3Sskrll   cplus_demangle_init_info (mangled, options, len, &di);
2102a6b7db3Sskrll 
2112a6b7db3Sskrll   di.comps = ((struct demangle_component *)
2122a6b7db3Sskrll 	      malloc (di.num_comps * sizeof (struct demangle_component)));
2132a6b7db3Sskrll   di.subs = ((struct demangle_component **)
2142a6b7db3Sskrll 	     malloc (di.num_subs * sizeof (struct demangle_component *)));
2152a6b7db3Sskrll   if (di.comps == NULL || di.subs == NULL)
2162a6b7db3Sskrll     {
2172a6b7db3Sskrll       free (di.comps);
2182a6b7db3Sskrll       free (di.subs);
2192a6b7db3Sskrll       return NULL;
2202a6b7db3Sskrll     }
2212a6b7db3Sskrll 
2222a6b7db3Sskrll   if (! type)
2232a6b7db3Sskrll     dc = cplus_demangle_mangled_name (&di, 1);
2242a6b7db3Sskrll   else
2252a6b7db3Sskrll     dc = cplus_demangle_type (&di);
2262a6b7db3Sskrll 
2272a6b7db3Sskrll   /* If DMGL_PARAMS is set, then if we didn't consume the entire
2282a6b7db3Sskrll      mangled string, then we didn't successfully demangle it.  */
2292a6b7db3Sskrll   if ((options & DMGL_PARAMS) != 0 && d_peek_char (&di) != '\0')
2302a6b7db3Sskrll     dc = NULL;
2312a6b7db3Sskrll 
2322a6b7db3Sskrll   free (di.subs);
2332a6b7db3Sskrll 
2342a6b7db3Sskrll   if (dc != NULL)
2352a6b7db3Sskrll     *mem = di.comps;
2362a6b7db3Sskrll   else
2372a6b7db3Sskrll     free (di.comps);
2382a6b7db3Sskrll 
2392a6b7db3Sskrll   return dc;
2402a6b7db3Sskrll }
241