symbol.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Symbol functions
  3. *
  4. * Copyright 2000 Jon Griffiths
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include "winedump.h"
  22. /* Items that are swapped in arguments after the symbol structure
  23. * has been populated
  24. */
  25. static const char * const swap_after[] =
  26. {
  27. "\r", " ", /* Remove whitespace, normalise pointers and brackets */
  28. "\t", " ",
  29. " ", " ",
  30. " * ", " *",
  31. "* *", "**",
  32. "* ", "*",
  33. " ,", ",",
  34. "( ", "(",
  35. " )", ")",
  36. "wchar_t", "WCHAR", /* Help with Unicode compiles */
  37. "wctype_t", "WCHAR",
  38. "wint_t", "WCHAR",
  39. NULL, NULL
  40. };
  41. /* Items containing these substrings are assumed to be wide character
  42. * strings, unless they contain more that one '*'. A preceding 'LP'
  43. * counts as a '*', so 'LPWCSTR *' is a pointer, not a string
  44. */
  45. static const char * const wide_strings[] =
  46. {
  47. "WSTR", "WCSTR", NULL
  48. };
  49. /* Items containing these substrings are assumed to be wide characters,
  50. * unless they contain one '*'. A preceding 'LP' counts as a '*',
  51. * so 'WCHAR *' is string, while 'LPWCHAR *' is a pointer
  52. */
  53. static const char * const wide_chars[] =
  54. {
  55. "WCHAR", NULL
  56. };
  57. /* Items containing these substrings are assumed to be ASCII character
  58. * strings, as above
  59. */
  60. static const char * const ascii_strings[] =
  61. {
  62. "STR", "CSTR", NULL
  63. };
  64. /* Items containing these substrings are assumed to be ASCII characters,
  65. * as above
  66. */
  67. static const char * const ascii_chars[] =
  68. {
  69. "CHAR", "char", NULL
  70. };
  71. /* Any type other than the following will produce a FIXME warning with -v
  72. * when mapped to a long, to allow fixups
  73. */
  74. static const char * const known_longs[] =
  75. {
  76. "char", "CHAR", "float", "int", "INT", "short", "SHORT", "long", "LONG",
  77. "WCHAR", "BOOL", "bool", "INT16", "WORD", "DWORD", NULL
  78. };
  79. void symbol_init(parsed_symbol* sym, const char* name)
  80. {
  81. memset(sym, 0, sizeof(parsed_symbol));
  82. sym->symbol = xstrdup(name);
  83. }
  84. /*******************************************************************
  85. * symbol_clear
  86. *
  87. * Free the memory used by a symbol and initialise it
  88. */
  89. void symbol_clear(parsed_symbol *sym)
  90. {
  91. int i;
  92. assert (sym);
  93. assert (sym->symbol);
  94. free (sym->symbol);
  95. free (sym->return_text);
  96. free (sym->function_name);
  97. for (i = sym->argc - 1; i >= 0; i--)
  98. {
  99. free (sym->arg_text [i]);
  100. free (sym->arg_name [i]);
  101. }
  102. memset (sym, 0, sizeof (parsed_symbol));
  103. }
  104. /*******************************************************************
  105. * symbol_is_valid_c
  106. *
  107. * Check if a symbol is a valid C identifier
  108. */
  109. BOOL symbol_is_valid_c(const parsed_symbol *sym)
  110. {
  111. char *name;
  112. assert (sym);
  113. assert (sym->symbol);
  114. name = sym->symbol;
  115. while (*name)
  116. {
  117. if (!isalnum (*name) && *name != '_')
  118. return FALSE;
  119. name++;
  120. }
  121. return TRUE;
  122. }
  123. /*******************************************************************
  124. * symbol_get_call_convention
  125. *
  126. * Return the calling convention of a symbol
  127. */
  128. const char *symbol_get_call_convention(const parsed_symbol *sym)
  129. {
  130. int call = sym->flags ? sym->flags : CALLING_CONVENTION;
  131. assert (sym);
  132. assert (sym->symbol);
  133. if (call & SYM_CDECL)
  134. return "cdecl";
  135. return "stdcall";
  136. }
  137. /*******************************************************************
  138. * symbol_get_spec_type
  139. *
  140. * Get the .spec file text for a symbol's argument
  141. */
  142. const char *symbol_get_spec_type (const parsed_symbol *sym, size_t arg)
  143. {
  144. assert (arg < sym->argc);
  145. switch (sym->arg_type [arg])
  146. {
  147. case ARG_STRING: return "str";
  148. case ARG_WIDE_STRING: return "wstr";
  149. case ARG_POINTER: return "ptr";
  150. case ARG_DOUBLE: return "double";
  151. case ARG_STRUCT:
  152. case ARG_FLOAT:
  153. case ARG_LONG: return "long";
  154. }
  155. assert (0);
  156. return NULL;
  157. }
  158. /*******************************************************************
  159. * symbol_get_type
  160. *
  161. * Get the ARG_ constant for a type string
  162. */
  163. int symbol_get_type (const char *string)
  164. {
  165. const char *iter = string;
  166. const char * const *tab;
  167. int ptrs = 0;
  168. while (*iter && isspace(*iter))
  169. iter++;
  170. if (*iter == 'P' || *iter == 'H')
  171. ptrs++; /* Win32 type pointer */
  172. iter = string;
  173. while (*iter)
  174. {
  175. if (*iter == '*' || (*iter == 'L' && iter[1] == 'P')
  176. || (*iter == '[' && iter[1] == ']'))
  177. ptrs++;
  178. if (ptrs > 1)
  179. return ARG_POINTER;
  180. iter++;
  181. }
  182. /* 0 or 1 pointer */
  183. tab = wide_strings;
  184. while (*tab++)
  185. if (strstr (string, tab[-1]))
  186. {
  187. if (ptrs < 2) return ARG_WIDE_STRING;
  188. else return ARG_POINTER;
  189. }
  190. tab = wide_chars;
  191. while (*tab++)
  192. if (strstr (string, tab[-1]))
  193. {
  194. if (!ptrs) return ARG_LONG;
  195. else return ARG_WIDE_STRING;
  196. }
  197. tab = ascii_strings;
  198. while (*tab++)
  199. if (strstr (string, tab[-1]))
  200. {
  201. if (ptrs < 2) return ARG_STRING;
  202. else return ARG_POINTER;
  203. }
  204. tab = ascii_chars;
  205. while (*tab++)
  206. if (strstr (string, tab[-1]))
  207. {
  208. if (!ptrs) return ARG_LONG;
  209. else {
  210. if (!strstr (string, "unsigned")) /* unsigned char * => ptr */
  211. return ARG_STRING;
  212. }
  213. }
  214. if (ptrs)
  215. return ARG_POINTER; /* Pointer to some other type */
  216. /* No pointers */
  217. if (strstr (string, "double"))
  218. return ARG_DOUBLE;
  219. if (strstr (string, "float") || strstr (string, "FLOAT"))
  220. return ARG_FLOAT;
  221. if (strstr (string, "void") || strstr (string, "VOID"))
  222. return ARG_VOID;
  223. if (strstr (string, "struct") || strstr (string, "union"))
  224. return ARG_STRUCT; /* Struct by value, ugh */
  225. if (VERBOSE)
  226. {
  227. BOOL known = FALSE;
  228. tab = known_longs;
  229. while (*tab++)
  230. if (strstr (string, tab[-1]))
  231. {
  232. known = TRUE;
  233. break;
  234. }
  235. /* Unknown types passed by value can be 'grep'ed out for fixup later */
  236. if (!known)
  237. printf ("/* FIXME: By value type: Assumed 'int' */ typedef int %s;\n",
  238. string);
  239. }
  240. return ARG_LONG;
  241. }
  242. /*******************************************************************
  243. * symbol_clean_string
  244. *
  245. * Make a type string more Wine-friendly. Logically const :-)
  246. */
  247. void symbol_clean_string (char *str)
  248. {
  249. const char * const *tab = swap_after;
  250. #define SWAP(i, p, x, y) do { i = p; while ((i = str_replace (i, x, y))); } while(0)
  251. while (tab [0])
  252. {
  253. char *p;
  254. SWAP (p, str, tab [0], tab [1]);
  255. tab += 2;
  256. }
  257. if (str [strlen (str) - 1] == ' ')
  258. str [strlen (str) - 1] = '\0'; /* no trailing space */
  259. if (*str == ' ')
  260. memmove (str, str + 1, strlen (str)); /* No leading spaces */
  261. }