cp-demint.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Demangler component interface functions.
  2. Copyright (C) 2004-2022 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_RVALUE_REFERENCE:
  98. case DEMANGLE_COMPONENT_COMPLEX:
  99. case DEMANGLE_COMPONENT_IMAGINARY:
  100. case DEMANGLE_COMPONENT_VENDOR_TYPE:
  101. case DEMANGLE_COMPONENT_CAST:
  102. case DEMANGLE_COMPONENT_CONVERSION:
  103. if (right != NULL)
  104. return 0;
  105. break;
  106. default:
  107. /* Other types do not use subtrees. */
  108. return 0;
  109. }
  110. p->type = type;
  111. p->u.s_binary.left = left;
  112. p->u.s_binary.right = right;
  113. p->d_printing = 0;
  114. p->d_counting = 0;
  115. return 1;
  116. }
  117. /* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE. */
  118. int
  119. cplus_demangle_fill_builtin_type (struct demangle_component *p,
  120. const char *type_name)
  121. {
  122. int len;
  123. unsigned int i;
  124. if (p == NULL || type_name == NULL)
  125. return 0;
  126. len = strlen (type_name);
  127. for (i = 0; i < D_BUILTIN_TYPE_COUNT; ++i)
  128. {
  129. if (len == cplus_demangle_builtin_types[i].len
  130. && strcmp (type_name, cplus_demangle_builtin_types[i].name) == 0)
  131. {
  132. p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
  133. p->u.s_builtin.type = &cplus_demangle_builtin_types[i];
  134. p->d_printing = 0;
  135. p->d_counting = 0;
  136. return 1;
  137. }
  138. }
  139. return 0;
  140. }
  141. /* Fill in a DEMANGLE_COMPONENT_OPERATOR. */
  142. int
  143. cplus_demangle_fill_operator (struct demangle_component *p,
  144. const char *opname, int args)
  145. {
  146. int len;
  147. unsigned int i;
  148. if (p == NULL || opname == NULL)
  149. return 0;
  150. len = strlen (opname);
  151. for (i = 0; cplus_demangle_operators[i].name != NULL; ++i)
  152. {
  153. if (len == cplus_demangle_operators[i].len
  154. && args == cplus_demangle_operators[i].args
  155. && strcmp (opname, cplus_demangle_operators[i].name) == 0)
  156. {
  157. p->type = DEMANGLE_COMPONENT_OPERATOR;
  158. p->u.s_operator.op = &cplus_demangle_operators[i];
  159. p->d_printing = 0;
  160. p->d_counting = 0;
  161. return 1;
  162. }
  163. }
  164. return 0;
  165. }
  166. /* Translate a mangled name into components. */
  167. struct demangle_component *
  168. cplus_demangle_v3_components (const char *mangled, int options, void **mem)
  169. {
  170. size_t len;
  171. int type;
  172. struct d_info di;
  173. struct demangle_component *dc;
  174. len = strlen (mangled);
  175. if (mangled[0] == '_' && mangled[1] == 'Z')
  176. type = 0;
  177. else
  178. {
  179. if ((options & DMGL_TYPES) == 0)
  180. return NULL;
  181. type = 1;
  182. }
  183. cplus_demangle_init_info (mangled, options, len, &di);
  184. di.comps = ((struct demangle_component *)
  185. malloc (di.num_comps * sizeof (struct demangle_component)));
  186. di.subs = ((struct demangle_component **)
  187. malloc (di.num_subs * sizeof (struct demangle_component *)));
  188. if (di.comps == NULL || di.subs == NULL)
  189. {
  190. free (di.comps);
  191. free (di.subs);
  192. return NULL;
  193. }
  194. if (! type)
  195. dc = cplus_demangle_mangled_name (&di, 1);
  196. else
  197. dc = cplus_demangle_type (&di);
  198. /* If DMGL_PARAMS is set, then if we didn't consume the entire
  199. mangled string, then we didn't successfully demangle it. */
  200. if ((options & DMGL_PARAMS) != 0 && d_peek_char (&di) != '\0')
  201. dc = NULL;
  202. free (di.subs);
  203. if (dc != NULL)
  204. *mem = di.comps;
  205. else
  206. free (di.comps);
  207. return dc;
  208. }