symbol.c 7.0 KB

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