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