4 * Copyright 2000 Jon Griffiths
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
27 /* Items that are swapped in arguments after the symbol structure
30 static const char *swap_after[] =
32 "\r", " ", /* Remove whitespace, normalise pointers and brackets */
41 "wchar_t", "WCHAR", /* Help with Unicode compliles */
44 "unsigned __int64", "__uint64", /* Wine doesn't cope with unsigned i64's */
49 /* Items containing these substrings are assumed to be wide character
50 * strings, unless they contain more that one '*'. A preceeding 'LP'
51 * counts as a '*', so 'LPWCSTR *' is a pointer, not a string
53 static const char *wide_strings[] =
58 /* Items containing these substrings are assumed to be wide characters,
59 * unless they contain one '*'. A preceeding 'LP' counts as a '*',
60 * so 'WCHAR *' is string, while 'LPWCHAR *' is a pointer
62 static const char *wide_chars[] =
67 /* Items containing these substrings are assumed to be ASCII character
70 static const char *ascii_strings[] =
76 /* Items containing these substrings are assumed to be ASCII characters,
79 static const char *ascii_chars[] =
84 /* Any type other than the following will produce a FIXME warning with -v
85 * when mapped to a long, to allow fixups
87 static const char *known_longs[] =
89 "char", "CHAR", "float", "int", "INT", "short", "SHORT", "long", "LONG",
90 "WCHAR", "BOOL", "bool", "INT16", "WORD", "DWORD", NULL
93 int symbol_init(parsed_symbol* sym, const char* name)
95 memset(sym, 0, sizeof(parsed_symbol));
96 sym->symbol = strdup(name);
100 /*******************************************************************
103 * Free the memory used by a symbol and initialise it
105 void symbol_clear(parsed_symbol *sym)
110 assert (sym->symbol);
114 if (sym->return_text)
115 free (sym->return_text);
117 if (sym->function_name)
118 free (sym->function_name);
120 for (i = sym->argc - 1; i >= 0; i--)
122 if (sym->arg_text [i])
123 free (sym->arg_text [i]);
124 if (sym->arg_name [i])
125 free (sym->arg_name [i]);
127 memset (sym, 0, sizeof (parsed_symbol));
131 /*******************************************************************
134 * Check if a symbol is a valid C identifier
136 int symbol_is_valid_c(const parsed_symbol *sym)
141 assert (sym->symbol);
147 if (!isalnum (*name) && *name != '_')
155 /*******************************************************************
156 * symbol_get_call_convention
158 * Return the calling convention of a symbol
160 const char *symbol_get_call_convention(const parsed_symbol *sym)
162 int call = sym->flags ? sym->flags : CALLING_CONVENTION;
165 assert (sym->symbol);
167 if (call & SYM_CDECL)
173 /*******************************************************************
174 * symbol_get_spec_type
176 * Get the .spec file text for a symbols argument
178 const char *symbol_get_spec_type (const parsed_symbol *sym, size_t arg)
180 assert (arg < sym->argc);
181 switch (sym->arg_type [arg])
183 case ARG_STRING: return "str";
184 case ARG_WIDE_STRING: return "wstr";
185 case ARG_POINTER: return "ptr";
186 case ARG_DOUBLE: return "double";
189 case ARG_LONG: return "long";
196 /*******************************************************************
199 * Get the ARG_ constant for a type string
201 int symbol_get_type (const char *string)
203 const char *iter = string;
207 while (*iter && isspace(*iter))
209 if (*iter == 'P' || *iter == 'H')
210 ptrs++; /* Win32 type pointer */
215 if (*iter == '*' || (*iter == 'L' && iter[1] == 'P')
216 || (*iter == '[' && iter[1] == ']'))
226 if (strstr (string, tab[-1]))
228 if (ptrs < 2) return ARG_WIDE_STRING;
229 else return ARG_POINTER;
233 if (strstr (string, tab[-1]))
235 if (!ptrs) return ARG_LONG;
236 else return ARG_WIDE_STRING;
240 if (strstr (string, tab[-1]))
242 if (ptrs < 2) return ARG_STRING;
243 else return ARG_POINTER;
247 if (strstr (string, tab[-1]))
249 if (!ptrs) return ARG_LONG;
251 if (!strstr (string, "unsigned")) /* unsigned char * => ptr */
257 return ARG_POINTER; /* Pointer to some other type */
260 if (strstr (string, "double"))
263 if (strstr (string, "void") || strstr (string, "VOID"))
266 if (strstr (string, "struct") || strstr (string, "union"))
267 return ARG_STRUCT; /* Struct by value, ugh */
275 if (strstr (string, tab[-1]))
280 /* Unknown types passed by value can be 'grep'ed out for fixup later */
282 printf ("/* FIXME: By value type: Assumed 'int' */ typedef int %s;\n",
289 /*******************************************************************
290 * symbol_clean_string
292 * Make a type string more Wine-friendly. Logically const :-)
294 void symbol_clean_string (const char *string)
296 const char **tab = swap_after;
297 char *str = (char *)string;
299 #define SWAP(i, p, x, y) do { i = p; while ((i = str_replace (i, x, y))); } while(0)
304 SWAP (p, str, tab [0], tab [1]);
307 if (str [strlen (str) - 1] == ' ')
308 str [strlen (str) - 1] = '\0'; /* no trailing space */
311 memmove (str, str + 1, strlen (str)); /* No leading spaces */