cp-demint.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Demangler component interface functions.
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <ian@wasabisystems.com>.
  4. This file is part of the libiberty library, which is part of GCC.
  5. This file is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. In addition to the permissions in the GNU General Public License, the
  10. Free Software Foundation gives you unlimited permission to link the
  11. compiled version of this file into combinations with other programs,
  12. and to distribute those combinations without any restriction coming
  13. from the use of this file. (The General Public License restrictions
  14. do apply in other respects; for example, they cover modification of
  15. the file, and distribution when not linked into a combined
  16. executable.)
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. /* This file implements a few interface functions which are provided
  26. for use with struct demangle_component trees. These functions are
  27. declared in demangle.h. These functions are closely tied to the
  28. demangler code in cp-demangle.c, and other interface functions can
  29. be found in that file. We put these functions in a separate file
  30. because they are not needed by the demangler, and so we avoid
  31. having them pulled in by programs which only need the
  32. demangler. */
  33. #ifdef HAVE_CONFIG_H
  34. #include "config.h"
  35. #endif
  36. #ifdef HAVE_STDLIB_H
  37. #include <stdlib.h>
  38. #endif
  39. #ifdef HAVE_STRING_H
  40. #include <string.h>
  41. #endif
  42. #include "ansidecl.h"
  43. #include "libiberty.h"
  44. #include "demangle.h"
  45. #include "cp-demangle.h"
  46. /* Fill in most component types. */
  47. int
  48. cplus_demangle_fill_component (struct demangle_component *p,
  49. enum demangle_component_type type,
  50. struct demangle_component *left,
  51. struct demangle_component *right)
  52. {
  53. if (p == NULL)
  54. return 0;
  55. switch (type)
  56. {
  57. case DEMANGLE_COMPONENT_QUAL_NAME:
  58. case DEMANGLE_COMPONENT_LOCAL_NAME:
  59. case DEMANGLE_COMPONENT_TYPED_NAME:
  60. case DEMANGLE_COMPONENT_TEMPLATE:
  61. case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
  62. case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
  63. case DEMANGLE_COMPONENT_FUNCTION_TYPE:
  64. case DEMANGLE_COMPONENT_ARRAY_TYPE:
  65. case DEMANGLE_COMPONENT_PTRMEM_TYPE:
  66. case DEMANGLE_COMPONENT_ARGLIST:
  67. case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
  68. case DEMANGLE_COMPONENT_UNARY:
  69. case DEMANGLE_COMPONENT_BINARY:
  70. case DEMANGLE_COMPONENT_BINARY_ARGS:
  71. case DEMANGLE_COMPONENT_TRINARY:
  72. case DEMANGLE_COMPONENT_TRINARY_ARG1:
  73. case DEMANGLE_COMPONENT_TRINARY_ARG2:
  74. case DEMANGLE_COMPONENT_LITERAL:
  75. case DEMANGLE_COMPONENT_LITERAL_NEG:
  76. break;
  77. /* These component types only have one subtree. */
  78. case DEMANGLE_COMPONENT_VTABLE:
  79. case DEMANGLE_COMPONENT_VTT:
  80. case DEMANGLE_COMPONENT_TYPEINFO:
  81. case DEMANGLE_COMPONENT_TYPEINFO_NAME:
  82. case DEMANGLE_COMPONENT_TYPEINFO_FN:
  83. case DEMANGLE_COMPONENT_THUNK:
  84. case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
  85. case DEMANGLE_COMPONENT_COVARIANT_THUNK:
  86. case DEMANGLE_COMPONENT_JAVA_CLASS:
  87. case DEMANGLE_COMPONENT_GUARD:
  88. case DEMANGLE_COMPONENT_REFTEMP:
  89. case DEMANGLE_COMPONENT_RESTRICT:
  90. case DEMANGLE_COMPONENT_VOLATILE:
  91. case DEMANGLE_COMPONENT_CONST:
  92. case DEMANGLE_COMPONENT_RESTRICT_THIS:
  93. case DEMANGLE_COMPONENT_VOLATILE_THIS:
  94. case DEMANGLE_COMPONENT_CONST_THIS:
  95. case DEMANGLE_COMPONENT_POINTER:
  96. case DEMANGLE_COMPONENT_REFERENCE:
  97. case DEMANGLE_COMPONENT_COMPLEX:
  98. case DEMANGLE_COMPONENT_IMAGINARY:
  99. case DEMANGLE_COMPONENT_VENDOR_TYPE:
  100. case DEMANGLE_COMPONENT_CAST:
  101. if (right != NULL)
  102. return 0;
  103. break;
  104. default:
  105. /* Other types do not use subtrees. */
  106. return 0;
  107. }
  108. p->type = type;
  109. p->u.s_binary.left = left;
  110. p->u.s_binary.right = right;
  111. return 1;
  112. }
  113. /* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE. */
  114. int
  115. cplus_demangle_fill_builtin_type (struct demangle_component *p,
  116. const char *type_name)
  117. {
  118. int len;
  119. unsigned int i;
  120. if (p == NULL || type_name == NULL)
  121. return 0;
  122. len = strlen (type_name);
  123. for (i = 0; i < D_BUILTIN_TYPE_COUNT; ++i)
  124. {
  125. if (len == cplus_demangle_builtin_types[i].len
  126. && strcmp (type_name, cplus_demangle_builtin_types[i].name) == 0)
  127. {
  128. p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
  129. p->u.s_builtin.type = &cplus_demangle_builtin_types[i];
  130. return 1;
  131. }
  132. }
  133. return 0;
  134. }
  135. /* Fill in a DEMANGLE_COMPONENT_OPERATOR. */
  136. int
  137. cplus_demangle_fill_operator (struct demangle_component *p,
  138. const char *opname, int args)
  139. {
  140. int len;
  141. unsigned int i;
  142. if (p == NULL || opname == NULL)
  143. return 0;
  144. len = strlen (opname);
  145. for (i = 0; cplus_demangle_operators[i].name != NULL; ++i)
  146. {
  147. if (len == cplus_demangle_operators[i].len
  148. && args == cplus_demangle_operators[i].args
  149. && strcmp (opname, cplus_demangle_operators[i].name) == 0)
  150. {
  151. p->type = DEMANGLE_COMPONENT_OPERATOR;
  152. p->u.s_operator.op = &cplus_demangle_operators[i];
  153. return 1;
  154. }
  155. }
  156. return 0;
  157. }
  158. /* Translate a mangled name into components. */
  159. struct demangle_component *
  160. cplus_demangle_v3_components (const char *mangled, int options, void **mem)
  161. {
  162. size_t len;
  163. int type;
  164. struct d_info di;
  165. struct demangle_component *dc;
  166. len = strlen (mangled);
  167. if (mangled[0] == '_' && mangled[1] == 'Z')
  168. type = 0;
  169. else
  170. {
  171. if ((options & DMGL_TYPES) == 0)
  172. return NULL;
  173. type = 1;
  174. }
  175. cplus_demangle_init_info (mangled, options, len, &di);
  176. di.comps = ((struct demangle_component *)
  177. malloc (di.num_comps * sizeof (struct demangle_component)));
  178. di.subs = ((struct demangle_component **)
  179. malloc (di.num_subs * sizeof (struct demangle_component *)));
  180. if (di.comps == NULL || di.subs == NULL)
  181. {
  182. free (di.comps);
  183. free (di.subs);
  184. return NULL;
  185. }
  186. if (! type)
  187. dc = cplus_demangle_mangled_name (&di, 1);
  188. else
  189. dc = cplus_demangle_type (&di);
  190. /* If DMGL_PARAMS is set, then if we didn't consume the entire
  191. mangled string, then we didn't successfully demangle it. */
  192. if ((options & DMGL_PARAMS) != 0 && d_peek_char (&di) != '\0')
  193. dc = NULL;
  194. free (di.subs);
  195. if (dc != NULL)
  196. *mem = di.comps;
  197. else
  198. free (di.comps);
  199. return dc;
  200. }