user32/tests: Fix some window test failures on various Windows platforms.
[wine] / tools / widl / typelib.c
1 /*
2  * IDL Compiler
3  *
4  * Copyright 2004 Ove Kaaven
5  * Copyright 2006 Jacek Caban for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24 #include "wine/wpp.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <ctype.h>
34
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37
38 #include "windef.h"
39 #include "winbase.h"
40
41 #include "widl.h"
42 #include "utils.h"
43 #include "parser.h"
44 #include "header.h"
45 #include "typelib.h"
46 #include "widltypes.h"
47 #include "typelib_struct.h"
48 #include "typetree.h"
49
50 static typelib_t *typelib;
51
52 int is_ptr(const type_t *t)
53 {
54     return type_get_type(t) == TYPE_POINTER;
55 }
56
57 int is_array(const type_t *t)
58 {
59     return type_get_type(t) == TYPE_ARRAY;
60 }
61
62 /* List of oleauto types that should be recognized by name.
63  * (most of) these seem to be intrinsic types in mktyplib.
64  * This table MUST be alphabetically sorted on the kw field.
65  */
66 static const struct oatype {
67   const char *kw;
68   unsigned short vt;
69 } oatypes[] = {
70   {"BSTR",          VT_BSTR},
71   {"CURRENCY",      VT_CY},
72   {"DATE",          VT_DATE},
73   {"DECIMAL",       VT_DECIMAL},
74   {"HRESULT",       VT_HRESULT},
75   {"LPSTR",         VT_LPSTR},
76   {"LPWSTR",        VT_LPWSTR},
77   {"SCODE",         VT_ERROR},
78   {"VARIANT",       VT_VARIANT},
79   {"VARIANT_BOOL",  VT_BOOL}
80 };
81 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
82 #define KWP(p) ((const struct oatype *)(p))
83
84 static int kw_cmp_func(const void *s1, const void *s2)
85 {
86         return strcmp(KWP(s1)->kw, KWP(s2)->kw);
87 }
88
89 static unsigned short builtin_vt(const type_t *t)
90 {
91   const char *kw = t->name;
92   struct oatype key;
93   const struct oatype *kwp;
94   key.kw = kw;
95 #ifdef KW_BSEARCH
96   kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
97 #else
98   {
99     unsigned int i;
100     for (kwp=NULL, i=0; i < NTYPES; i++)
101       if (!kw_cmp_func(&key, &oatypes[i])) {
102         kwp = &oatypes[i];
103         break;
104       }
105   }
106 #endif
107   if (kwp) {
108     return kwp->vt;
109   }
110   if (is_string_type (t->attrs, t))
111   {
112     unsigned char fc;
113     if (is_array(t))
114       fc = type_array_get_element(t)->type;
115     else
116       fc = type_pointer_get_ref(t)->type;
117     switch (fc)
118       {
119       case RPC_FC_CHAR: return VT_LPSTR;
120       case RPC_FC_WCHAR: return VT_LPWSTR;
121       default: break;
122       }
123   }
124   return 0;
125 }
126
127 static int match(const char*n, const char*m)
128 {
129   if (!n) return 0;
130   return !strcmp(n, m);
131 }
132
133 unsigned short get_type_vt(type_t *t)
134 {
135   unsigned short vt;
136
137   chat("get_type_vt: %p type->name %s\n", t, t->name);
138   if (t->name) {
139     vt = builtin_vt(t);
140     if (vt) return vt;
141   }
142
143   if (type_is_alias(t) && is_attr(t->attrs, ATTR_PUBLIC))
144     return VT_USERDEFINED;
145
146   switch (t->type) {
147   case RPC_FC_BYTE:
148   case RPC_FC_USMALL:
149     return VT_UI1;
150   case RPC_FC_CHAR:
151   case RPC_FC_SMALL:
152     return VT_I1;
153   case RPC_FC_WCHAR:
154     return VT_I2; /* mktyplib seems to parse wchar_t as short */
155   case RPC_FC_SHORT:
156     return VT_I2;
157   case RPC_FC_USHORT:
158     return VT_UI2;
159   case RPC_FC_LONG:
160     if (match(t->name, "int")) return VT_INT;
161     return VT_I4;
162   case RPC_FC_ULONG:
163     if (match(t->name, "int")) return VT_UINT;
164     return VT_UI4;
165   case RPC_FC_HYPER:
166     if (t->sign < 0) return VT_UI8;
167     if (match(t->name, "MIDL_uhyper")) return VT_UI8;
168     return VT_I8;
169   case RPC_FC_FLOAT:
170     return VT_R4;
171   case RPC_FC_DOUBLE:
172     return VT_R8;
173   case RPC_FC_RP:
174   case RPC_FC_UP:
175   case RPC_FC_OP:
176   case RPC_FC_FP:
177   case RPC_FC_SMFARRAY:
178   case RPC_FC_LGFARRAY:
179   case RPC_FC_SMVARRAY:
180   case RPC_FC_LGVARRAY:
181   case RPC_FC_CARRAY:
182   case RPC_FC_CVARRAY:
183   case RPC_FC_BOGUS_ARRAY:
184     if(t->ref)
185     {
186       if (match(t->ref->name, "SAFEARRAY"))
187         return VT_SAFEARRAY;
188       return VT_PTR;
189     }
190
191     error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
192     break;
193   case RPC_FC_IP:
194     if(match(t->name, "IUnknown"))
195       return VT_UNKNOWN;
196     if(match(t->name, "IDispatch"))
197       return VT_DISPATCH;
198     return VT_USERDEFINED;
199
200   case RPC_FC_ENUM16:
201   case RPC_FC_STRUCT:
202   case RPC_FC_PSTRUCT:
203   case RPC_FC_CSTRUCT:
204   case RPC_FC_CPSTRUCT:
205   case RPC_FC_CVSTRUCT:
206   case RPC_FC_BOGUS_STRUCT:
207   case RPC_FC_COCLASS:
208   case RPC_FC_MODULE:
209     return VT_USERDEFINED;
210   case 0:
211     return VT_VOID;
212   default:
213     error("get_type_vt: unknown type: 0x%02x\n", t->type);
214   }
215   return 0;
216 }
217
218 void start_typelib(typelib_t *typelib_type)
219 {
220     if (!do_typelib) return;
221
222     typelib = typelib_type;
223     typelib->filename = xstrdup(typelib_name);
224 }
225
226 void end_typelib(void)
227 {
228     if (!typelib) return;
229
230     create_msft_typelib(typelib);
231 }
232
233 static void tlb_read(int fd, void *buf, int count)
234 {
235     if(read(fd, buf, count) < count)
236         error("error while reading importlib.\n");
237 }
238
239 static void tlb_lseek(int fd, off_t offset)
240 {
241     if(lseek(fd, offset, SEEK_SET) == -1)
242         error("lseek failed\n");
243 }
244
245 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
246 {
247     tlb_lseek(fd, segdir->pGuidTab.offset+offset);
248     tlb_read(fd, guid, sizeof(GUID));
249 }
250
251 static void read_msft_importlib(importlib_t *importlib, int fd)
252 {
253     MSFT_Header header;
254     MSFT_SegDir segdir;
255     int *typeinfo_offs;
256     int i;
257
258     importlib->allocated = 0;
259
260     tlb_lseek(fd, 0);
261     tlb_read(fd, &header, sizeof(header));
262
263     importlib->version = header.version;
264
265     typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
266     tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
267     tlb_read(fd, &segdir, sizeof(segdir));
268
269     msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
270
271     importlib->ntypeinfos = header.nrtypeinfos;
272     importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
273
274     for(i=0; i < importlib->ntypeinfos; i++) {
275         MSFT_TypeInfoBase base;
276         MSFT_NameIntro nameintro;
277         int len;
278
279         tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
280                  + typeinfo_offs[i]);
281         tlb_read(fd, &base, sizeof(base));
282
283         importlib->importinfos[i].importlib = importlib;
284         importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
285         importlib->importinfos[i].offset = -1;
286         importlib->importinfos[i].id = i;
287
288         if(base.posguid != -1) {
289             importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
290             msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
291         }
292         else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
293
294         tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
295         tlb_read(fd, &nameintro, sizeof(nameintro));
296
297         len = nameintro.namelen & 0xff;
298
299         importlib->importinfos[i].name = xmalloc(len+1);
300         tlb_read(fd, importlib->importinfos[i].name, len);
301         importlib->importinfos[i].name[len] = 0;
302     }
303
304     free(typeinfo_offs);
305 }
306
307 static void read_importlib(importlib_t *importlib)
308 {
309     int fd;
310     INT magic;
311     char *file_name;
312
313     file_name = wpp_find_include(importlib->name, NULL);
314     if(file_name) {
315         fd = open(file_name, O_RDONLY | O_BINARY );
316         free(file_name);
317     }else {
318         fd = open(importlib->name, O_RDONLY | O_BINARY );
319     }
320
321     if(fd < 0)
322         error("Could not open importlib %s.\n", importlib->name);
323
324     tlb_read(fd, &magic, sizeof(magic));
325
326     switch(magic) {
327     case MSFT_MAGIC:
328         read_msft_importlib(importlib, fd);
329         break;
330     default:
331         error("Wrong or unsupported typelib magic %x\n", magic);
332     };
333
334     close(fd);
335 }
336
337 void add_importlib(const char *name)
338 {
339     importlib_t *importlib;
340
341     if(!typelib) return;
342
343     LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
344         if(!strcmp(name, importlib->name))
345             return;
346
347     chat("add_importlib: %s\n", name);
348
349     importlib = xmalloc(sizeof(*importlib));
350     memset( importlib, 0, sizeof(*importlib) );
351     importlib->name = xstrdup(name);
352
353     read_importlib(importlib);
354     list_add_head( &typelib->importlibs, &importlib->entry );
355 }