180708Sjake/* Demangler component interface functions.
280708Sjake   Copyright (C) 2004 Free Software Foundation, Inc.
380708Sjake   Written by Ian Lance Taylor <ian@wasabisystems.com>.
480708Sjake
580708Sjake   This file is part of the libiberty library, which is part of GCC.
680708Sjake
780708Sjake   This file is free software; you can redistribute it and/or modify
880708Sjake   it under the terms of the GNU General Public License as published by
980708Sjake   the Free Software Foundation; either version 2 of the License, or
1080708Sjake   (at your option) any later version.
1180708Sjake
1280708Sjake   In addition to the permissions in the GNU General Public License, the
1380708Sjake   Free Software Foundation gives you unlimited permission to link the
1480708Sjake   compiled version of this file into combinations with other programs,
1580708Sjake   and to distribute those combinations without any restriction coming
1680708Sjake   from the use of this file.  (The General Public License restrictions
1780708Sjake   do apply in other respects; for example, they cover modification of
1880708Sjake   the file, and distribution when not linked into a combined
1980708Sjake   executable.)
2080708Sjake
2180708Sjake   This program is distributed in the hope that it will be useful,
2280708Sjake   but WITHOUT ANY WARRANTY; without even the implied warranty of
2380708Sjake   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2480708Sjake   GNU General Public License for more details.
2580708Sjake
2680708Sjake   You should have received a copy of the GNU General Public License
2780708Sjake   along with this program; if not, write to the Free Software
2880708Sjake   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
2980708Sjake*/
3080708Sjake
3180708Sjake/* This file implements a few interface functions which are provided
3280708Sjake   for use with struct demangle_component trees.  These functions are
33196994Sphk   declared in demangle.h.  These functions are closely tied to the
34196994Sphk   demangler code in cp-demangle.c, and other interface functions can
35196994Sphk   be found in that file.  We put these functions in a separate file
3680708Sjake   because they are not needed by the demangler, and so we avoid
3780708Sjake   having them pulled in by programs which only need the
3880708Sjake   demangler.  */
3980708Sjake
40196994Sphk#ifdef HAVE_CONFIG_H
4180708Sjake#include "config.h"
42195808Smarius#endif
43195808Smarius
4480708Sjake#ifdef HAVE_STDLIB_H
4580708Sjake#include <stdlib.h>
4680708Sjake#endif
4780708Sjake#ifdef HAVE_STRING_H
4880708Sjake#include <string.h>
4980708Sjake#endif
5080708Sjake
5180708Sjake#include "ansidecl.h"
52177661Sjb#include "libiberty.h"
53224207Sattilio#include "demangle.h"
54224232Smarius#include "cp-demangle.h"
55224207Sattilio
5680708Sjake/* Fill in most component types.  */
5780708Sjake
58177661Sjbint
5980708Sjakecplus_demangle_fill_component (struct demangle_component *p,
6091336Sjake                               enum demangle_component_type type,
6191336Sjake                               struct demangle_component *left,
6291336Sjake                                struct demangle_component *right)
6380708Sjake{
6480708Sjake  if (p == NULL)
65195376Ssam    return 0;
66195376Ssam  switch (type)
67195376Ssam    {
68195376Ssam    case DEMANGLE_COMPONENT_QUAL_NAME:
69195376Ssam    case DEMANGLE_COMPONENT_LOCAL_NAME:
70195376Ssam    case DEMANGLE_COMPONENT_TYPED_NAME:
71195376Ssam    case DEMANGLE_COMPONENT_TEMPLATE:
7280708Sjake    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
73191278Srwatson    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
74191278Srwatson    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
75191278Srwatson    case DEMANGLE_COMPONENT_ARRAY_TYPE:
76191278Srwatson    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
77191309Srwatson    case DEMANGLE_COMPONENT_ARGLIST:
78191276Srwatson    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
79191276Srwatson    case DEMANGLE_COMPONENT_UNARY:
8080709Sjake    case DEMANGLE_COMPONENT_BINARY:
81120710Salc    case DEMANGLE_COMPONENT_BINARY_ARGS:
8280709Sjake    case DEMANGLE_COMPONENT_TRINARY:
8380708Sjake    case DEMANGLE_COMPONENT_TRINARY_ARG1:
8480709Sjake    case DEMANGLE_COMPONENT_TRINARY_ARG2:
85120710Salc    case DEMANGLE_COMPONENT_LITERAL:
8680709Sjake    case DEMANGLE_COMPONENT_LITERAL_NEG:
8780708Sjake      break;
8880709Sjake
89120710Salc      /* These component types only have one subtree.  */
9080709Sjake    case DEMANGLE_COMPONENT_VTABLE:
9180709Sjake    case DEMANGLE_COMPONENT_VTT:
9280709Sjake    case DEMANGLE_COMPONENT_TYPEINFO:
93120710Salc    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
9480709Sjake    case DEMANGLE_COMPONENT_TYPEINFO_FN:
9580709Sjake    case DEMANGLE_COMPONENT_THUNK:
96165324Skmacy    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
97165324Skmacy    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
98165324Skmacy    case DEMANGLE_COMPONENT_JAVA_CLASS:
99165324Skmacy    case DEMANGLE_COMPONENT_GUARD:
100165324Skmacy    case DEMANGLE_COMPONENT_REFTEMP:
101165324Skmacy    case DEMANGLE_COMPONENT_RESTRICT:
102165324Skmacy    case DEMANGLE_COMPONENT_VOLATILE:
103165324Skmacy    case DEMANGLE_COMPONENT_CONST:
10486226Stmm    case DEMANGLE_COMPONENT_RESTRICT_THIS:
10586226Stmm    case DEMANGLE_COMPONENT_VOLATILE_THIS:
10686226Stmm    case DEMANGLE_COMPONENT_CONST_THIS:
10780709Sjake    case DEMANGLE_COMPONENT_POINTER:
10880709Sjake    case DEMANGLE_COMPONENT_REFERENCE:
10980709Sjake    case DEMANGLE_COMPONENT_COMPLEX:
11086226Stmm    case DEMANGLE_COMPONENT_IMAGINARY:
11186226Stmm    case DEMANGLE_COMPONENT_VENDOR_TYPE:
11286226Stmm    case DEMANGLE_COMPONENT_CAST:
11380709Sjake      if (right != NULL)
114197316Salc	return 0;
115197316Salc      break;
116118239Speter
11788781Sjake    default:
118118239Speter      /* Other types do not use subtrees.  */
119116355Salc      return 0;
12091360Sjake    }
12183366Sjulian
122151344Skris  p->type = type;
123151344Skris  p->u.s_binary.left = left;
124151344Skris  p->u.s_binary.right = right;
125151344Skris
126151344Skris  return 1;
127151344Skris}
128151344Skris
129151344Skris/* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE.  */
13083366Sjulian
13180708Sjakeint
13280708Sjakecplus_demangle_fill_builtin_type (struct demangle_component *p,
13380708Sjake                                  const char *type_name)
13480709Sjake{
13580708Sjake  int len;
13680708Sjake  unsigned int i;
13780708Sjake
13880708Sjake  if (p == NULL || type_name == NULL)
13980708Sjake    return 0;
14080708Sjake  len = strlen (type_name);
14180708Sjake  for (i = 0; i < D_BUILTIN_TYPE_COUNT; ++i)
14280708Sjake    {
14380708Sjake      if (len == cplus_demangle_builtin_types[i].len
14480708Sjake	  && strcmp (type_name, cplus_demangle_builtin_types[i].name) == 0)
145196994Sphk	{
146	  p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
147	  p->u.s_builtin.type = &cplus_demangle_builtin_types[i];
148	  return 1;
149	}
150    }
151  return 0;
152}
153
154/* Fill in a DEMANGLE_COMPONENT_OPERATOR.  */
155
156int
157cplus_demangle_fill_operator (struct demangle_component *p,
158                              const char *opname, int args)
159{
160  int len;
161  unsigned int i;
162
163  if (p == NULL || opname == NULL)
164    return 0;
165  len = strlen (opname);
166  for (i = 0; cplus_demangle_operators[i].name != NULL; ++i)
167    {
168      if (len == cplus_demangle_operators[i].len
169	  && args == cplus_demangle_operators[i].args
170	  && strcmp (opname, cplus_demangle_operators[i].name) == 0)
171	{
172	  p->type = DEMANGLE_COMPONENT_OPERATOR;
173	  p->u.s_operator.op = &cplus_demangle_operators[i];
174	  return 1;
175	}
176    }
177  return 0;
178}
179
180/* Translate a mangled name into components.  */
181
182struct demangle_component *
183cplus_demangle_v3_components (const char *mangled, int options, void **mem)
184{
185  size_t len;
186  int type;
187  struct d_info di;
188  struct demangle_component *dc;
189
190  len = strlen (mangled);
191
192  if (mangled[0] == '_' && mangled[1] == 'Z')
193    type = 0;
194  else
195    {
196      if ((options & DMGL_TYPES) == 0)
197	return NULL;
198      type = 1;
199    }
200
201  cplus_demangle_init_info (mangled, options, len, &di);
202
203  di.comps = ((struct demangle_component *)
204	      malloc (di.num_comps * sizeof (struct demangle_component)));
205  di.subs = ((struct demangle_component **)
206	     malloc (di.num_subs * sizeof (struct demangle_component *)));
207  if (di.comps == NULL || di.subs == NULL)
208    {
209      if (di.comps != NULL)
210	free (di.comps);
211      if (di.subs != NULL)
212	free (di.subs);
213      return NULL;
214    }
215
216  if (! type)
217    dc = cplus_demangle_mangled_name (&di, 1);
218  else
219    dc = cplus_demangle_type (&di);
220
221  /* If DMGL_PARAMS is set, then if we didn't consume the entire
222     mangled string, then we didn't successfully demangle it.  */
223  if ((options & DMGL_PARAMS) != 0 && d_peek_char (&di) != '\0')
224    dc = NULL;
225
226  free (di.subs);
227
228  if (dc != NULL)
229    *mem = di.comps;
230  else
231    free (di.comps);
232
233  return dc;
234}
235