wined3d: Fix debug_d3dusagequery() to handle combinations of flags.
[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     const type_t *elem_type;
113     if (is_array(t))
114       elem_type = type_array_get_element(t);
115     else
116       elem_type = type_pointer_get_ref(t);
117     if (type_get_type(elem_type) == TYPE_BASIC)
118     {
119       switch (type_basic_get_type(elem_type))
120       {
121       case TYPE_BASIC_CHAR: return VT_LPSTR;
122       case TYPE_BASIC_WCHAR: return VT_LPWSTR;
123       default: break;
124       }
125     }
126   }
127   return 0;
128 }
129
130 static int match(const char*n, const char*m)
131 {
132   if (!n) return 0;
133   return !strcmp(n, m);
134 }
135
136 unsigned short get_type_vt(type_t *t)
137 {
138   unsigned short vt;
139
140   chat("get_type_vt: %p type->name %s\n", t, t->name);
141   if (t->name) {
142     vt = builtin_vt(t);
143     if (vt) return vt;
144   }
145
146   if (type_is_alias(t) && is_attr(t->attrs, ATTR_PUBLIC))
147     return VT_USERDEFINED;
148
149   switch (type_get_type(t)) {
150   case TYPE_BASIC:
151     switch (type_basic_get_type(t)) {
152     case TYPE_BASIC_BYTE:
153       return VT_UI1;
154     case TYPE_BASIC_CHAR:
155     case TYPE_BASIC_INT8:
156       if (type_basic_get_sign(t) > 0)
157         return VT_UI1;
158       else
159         return VT_I1;
160     case TYPE_BASIC_WCHAR:
161       return VT_I2; /* mktyplib seems to parse wchar_t as short */
162     case TYPE_BASIC_INT16:
163       if (type_basic_get_sign(t) > 0)
164         return VT_UI2;
165       else
166         return VT_I2;
167     case TYPE_BASIC_INT:
168       if (type_basic_get_sign(t) > 0)
169         return VT_UINT;
170       else
171         return VT_INT;
172     case TYPE_BASIC_INT32:
173     case TYPE_BASIC_ERROR_STATUS_T:
174       if (type_basic_get_sign(t) > 0)
175         return VT_UI4;
176       else
177         return VT_I4;
178     case TYPE_BASIC_INT64:
179     case TYPE_BASIC_HYPER:
180       if (type_basic_get_sign(t) > 0)
181         return VT_UI8;
182       else
183         return VT_I8;
184     case TYPE_BASIC_FLOAT:
185       return VT_R4;
186     case TYPE_BASIC_DOUBLE:
187       return VT_R8;
188     case TYPE_BASIC_HANDLE:
189       error("handles can't be used in typelibs\n");
190     }
191     break;
192
193   case TYPE_POINTER:
194     return VT_PTR;
195
196   case TYPE_ARRAY:
197     if (type_array_is_decl_as_ptr(t))
198     {
199       if (match(type_array_get_element(t)->name, "SAFEARRAY"))
200         return VT_SAFEARRAY;
201     }
202     else
203       error("get_type_vt: array types not supported\n");
204     return VT_PTR;
205
206   case TYPE_INTERFACE:
207     if(match(t->name, "IUnknown"))
208       return VT_UNKNOWN;
209     if(match(t->name, "IDispatch"))
210       return VT_DISPATCH;
211     return VT_USERDEFINED;
212
213   case TYPE_ENUM:
214   case TYPE_STRUCT:
215   case TYPE_COCLASS:
216   case TYPE_MODULE:
217   case TYPE_UNION:
218   case TYPE_ENCAPSULATED_UNION:
219     return VT_USERDEFINED;
220
221   case TYPE_VOID:
222     return VT_VOID;
223
224   case TYPE_ALIAS:
225     /* aliases should be filtered out by the type_get_type call above */
226     assert(0);
227     break;
228
229   case TYPE_FUNCTION:
230     error("get_type_vt: functions not supported\n");
231     break;
232   }
233   return 0;
234 }
235
236 void start_typelib(typelib_t *typelib_type)
237 {
238     if (!do_typelib) return;
239
240     typelib = typelib_type;
241     typelib->filename = xstrdup(typelib_name);
242 }
243
244 void end_typelib(void)
245 {
246     if (!typelib) return;
247
248     create_msft_typelib(typelib);
249 }
250
251 static void tlb_read(int fd, void *buf, int count)
252 {
253     if(read(fd, buf, count) < count)
254         error("error while reading importlib.\n");
255 }
256
257 static void tlb_lseek(int fd, off_t offset)
258 {
259     if(lseek(fd, offset, SEEK_SET) == -1)
260         error("lseek failed\n");
261 }
262
263 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
264 {
265     tlb_lseek(fd, segdir->pGuidTab.offset+offset);
266     tlb_read(fd, guid, sizeof(GUID));
267 }
268
269 static void read_msft_importlib(importlib_t *importlib, int fd)
270 {
271     MSFT_Header header;
272     MSFT_SegDir segdir;
273     int *typeinfo_offs;
274     int i;
275
276     importlib->allocated = 0;
277
278     tlb_lseek(fd, 0);
279     tlb_read(fd, &header, sizeof(header));
280
281     importlib->version = header.version;
282
283     typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
284     tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
285     tlb_read(fd, &segdir, sizeof(segdir));
286
287     msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
288
289     importlib->ntypeinfos = header.nrtypeinfos;
290     importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
291
292     for(i=0; i < importlib->ntypeinfos; i++) {
293         MSFT_TypeInfoBase base;
294         MSFT_NameIntro nameintro;
295         int len;
296
297         tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
298                  + typeinfo_offs[i]);
299         tlb_read(fd, &base, sizeof(base));
300
301         importlib->importinfos[i].importlib = importlib;
302         importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
303         importlib->importinfos[i].offset = -1;
304         importlib->importinfos[i].id = i;
305
306         if(base.posguid != -1) {
307             importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
308             msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
309         }
310         else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
311
312         tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
313         tlb_read(fd, &nameintro, sizeof(nameintro));
314
315         len = nameintro.namelen & 0xff;
316
317         importlib->importinfos[i].name = xmalloc(len+1);
318         tlb_read(fd, importlib->importinfos[i].name, len);
319         importlib->importinfos[i].name[len] = 0;
320     }
321
322     free(typeinfo_offs);
323 }
324
325 static void read_importlib(importlib_t *importlib)
326 {
327     int fd;
328     INT magic;
329     char *file_name;
330
331     file_name = wpp_find_include(importlib->name, NULL);
332     if(file_name) {
333         fd = open(file_name, O_RDONLY | O_BINARY );
334         free(file_name);
335     }else {
336         fd = open(importlib->name, O_RDONLY | O_BINARY );
337     }
338
339     if(fd < 0)
340         error("Could not open importlib %s.\n", importlib->name);
341
342     tlb_read(fd, &magic, sizeof(magic));
343
344     switch(magic) {
345     case MSFT_MAGIC:
346         read_msft_importlib(importlib, fd);
347         break;
348     default:
349         error("Wrong or unsupported typelib magic %x\n", magic);
350     };
351
352     close(fd);
353 }
354
355 void add_importlib(const char *name)
356 {
357     importlib_t *importlib;
358
359     if(!typelib) return;
360
361     LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
362         if(!strcmp(name, importlib->name))
363             return;
364
365     chat("add_importlib: %s\n", name);
366
367     importlib = xmalloc(sizeof(*importlib));
368     memset( importlib, 0, sizeof(*importlib) );
369     importlib->name = xstrdup(name);
370
371     read_importlib(importlib);
372     list_add_head( &typelib->importlibs, &importlib->entry );
373 }