widl: Factor out the finding of a registered type to reduce code duplication.
[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
49 int in_typelib = 0;
50
51 static typelib_t *typelib;
52
53 type_t *duptype(type_t *t, int dupname)
54 {
55   type_t *d = alloc_type();
56
57   *d = *t;
58   if (dupname && t->name)
59     d->name = xstrdup(t->name);
60
61   d->orig = t;
62   return d;
63 }
64
65 type_t *alias(type_t *t, const char *name)
66 {
67   type_t *a = duptype(t, 0);
68
69   a->name = xstrdup(name);
70   a->kind = TKIND_ALIAS;
71   a->attrs = NULL;
72   a->declarray = FALSE;
73
74   return a;
75 }
76
77 int is_ptr(const type_t *t)
78 {
79   unsigned char c = t->type;
80   return c == RPC_FC_RP
81       || c == RPC_FC_UP
82       || c == RPC_FC_FP
83       || c == RPC_FC_OP;
84 }
85
86 int is_array(const type_t *t)
87 {
88     switch (t->type)
89     {
90     case RPC_FC_SMFARRAY:
91     case RPC_FC_LGFARRAY:
92     case RPC_FC_SMVARRAY:
93     case RPC_FC_LGVARRAY:
94     case RPC_FC_CARRAY:
95     case RPC_FC_CVARRAY:
96     case RPC_FC_BOGUS_ARRAY:
97         return TRUE;
98     default:
99         return FALSE;
100     }
101 }
102
103 /* List of oleauto types that should be recognized by name.
104  * (most of) these seem to be intrinsic types in mktyplib.
105  * This table MUST be alphabetically sorted on the kw field.
106  */
107 static const struct oatype {
108   const char *kw;
109   unsigned short vt;
110 } oatypes[] = {
111   {"BSTR",          VT_BSTR},
112   {"CURRENCY",      VT_CY},
113   {"DATE",          VT_DATE},
114   {"DECIMAL",       VT_DECIMAL},
115   {"HRESULT",       VT_HRESULT},
116   {"LPSTR",         VT_LPSTR},
117   {"LPWSTR",        VT_LPWSTR},
118   {"SCODE",         VT_ERROR},
119   {"VARIANT",       VT_VARIANT},
120   {"VARIANT_BOOL",  VT_BOOL}
121 };
122 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
123 #define KWP(p) ((const struct oatype *)(p))
124
125 static int kw_cmp_func(const void *s1, const void *s2)
126 {
127         return strcmp(KWP(s1)->kw, KWP(s2)->kw);
128 }
129
130 static unsigned short builtin_vt(const type_t *t)
131 {
132   const char *kw = t->name;
133   struct oatype key;
134   const struct oatype *kwp;
135   key.kw = kw;
136 #ifdef KW_BSEARCH
137   kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
138 #else
139   {
140     unsigned int i;
141     for (kwp=NULL, i=0; i < NTYPES; i++)
142       if (!kw_cmp_func(&key, &oatypes[i])) {
143         kwp = &oatypes[i];
144         break;
145       }
146   }
147 #endif
148   if (kwp) {
149     return kwp->vt;
150   }
151   if (is_string_type (t->attrs, t))
152     switch (t->ref->type)
153       {
154       case RPC_FC_CHAR: return VT_LPSTR;
155       case RPC_FC_WCHAR: return VT_LPWSTR;
156       default: break;
157       }
158   return 0;
159 }
160
161 static int match(const char*n, const char*m)
162 {
163   if (!n) return 0;
164   return !strcmp(n, m);
165 }
166
167 unsigned short get_type_vt(type_t *t)
168 {
169   unsigned short vt;
170
171   chat("get_type_vt: %p type->name %s\n", t, t->name);
172   if (t->name) {
173     vt = builtin_vt(t);
174     if (vt) return vt;
175   }
176
177   if (t->kind == TKIND_ALIAS && is_attr(t->attrs, ATTR_PUBLIC))
178     return VT_USERDEFINED;
179
180   switch (t->type) {
181   case RPC_FC_BYTE:
182   case RPC_FC_USMALL:
183     return VT_UI1;
184   case RPC_FC_CHAR:
185   case RPC_FC_SMALL:
186     return VT_I1;
187   case RPC_FC_WCHAR:
188     return VT_I2; /* mktyplib seems to parse wchar_t as short */
189   case RPC_FC_SHORT:
190     return VT_I2;
191   case RPC_FC_USHORT:
192     return VT_UI2;
193   case RPC_FC_LONG:
194     if (match(t->name, "int")) return VT_INT;
195     return VT_I4;
196   case RPC_FC_ULONG:
197     if (match(t->name, "int")) return VT_UINT;
198     return VT_UI4;
199   case RPC_FC_HYPER:
200     if (t->sign < 0) return VT_UI8;
201     if (match(t->name, "MIDL_uhyper")) return VT_UI8;
202     return VT_I8;
203   case RPC_FC_FLOAT:
204     return VT_R4;
205   case RPC_FC_DOUBLE:
206     return VT_R8;
207   case RPC_FC_RP:
208   case RPC_FC_UP:
209   case RPC_FC_OP:
210   case RPC_FC_FP:
211   case RPC_FC_CARRAY:
212   case RPC_FC_CVARRAY:
213     if(t->ref)
214     {
215       if (match(t->ref->name, "SAFEARRAY"))
216         return VT_SAFEARRAY;
217       return VT_PTR;
218     }
219
220     error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
221     break;
222   case RPC_FC_IP:
223     if(match(t->name, "IUnknown"))
224       return VT_UNKNOWN;
225     if(match(t->name, "IDispatch"))
226       return VT_DISPATCH;
227     return VT_USERDEFINED;
228
229   case RPC_FC_ENUM16:
230   case RPC_FC_STRUCT:
231   case RPC_FC_PSTRUCT:
232   case RPC_FC_CSTRUCT:
233   case RPC_FC_CPSTRUCT:
234   case RPC_FC_CVSTRUCT:
235   case RPC_FC_BOGUS_STRUCT:
236   case RPC_FC_COCLASS:
237     return VT_USERDEFINED;
238   case 0:
239     return t->kind == TKIND_PRIMITIVE ? VT_VOID : VT_USERDEFINED;
240   default:
241     error("get_type_vt: unknown type: 0x%02x\n", t->type);
242   }
243   return 0;
244 }
245
246 void start_typelib(typelib_t *typelib_type)
247 {
248     in_typelib++;
249     if (!do_typelib) return;
250
251     typelib = typelib_type;
252     typelib->filename = xstrdup(typelib_name);
253 }
254
255 void end_typelib(void)
256 {
257     in_typelib--;
258     if (!typelib) return;
259
260     create_msft_typelib(typelib);
261 }
262
263 void add_typelib_entry(type_t *t)
264 {
265     typelib_entry_t *entry;
266     if (!typelib) return;
267
268     chat("add kind %i: %s\n", t->kind, t->name);
269     entry = xmalloc(sizeof(*entry));
270     entry->type = t;
271     list_add_tail( &typelib->entries, &entry->entry );
272 }
273
274 static void tlb_read(int fd, void *buf, int count)
275 {
276     if(read(fd, buf, count) < count)
277         error("error while reading importlib.\n");
278 }
279
280 static void tlb_lseek(int fd, off_t offset)
281 {
282     if(lseek(fd, offset, SEEK_SET) == -1)
283         error("lseek failed\n");
284 }
285
286 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
287 {
288     tlb_lseek(fd, segdir->pGuidTab.offset+offset);
289     tlb_read(fd, guid, sizeof(GUID));
290 }
291
292 static void read_msft_importlib(importlib_t *importlib, int fd)
293 {
294     MSFT_Header header;
295     MSFT_SegDir segdir;
296     int *typeinfo_offs;
297     int i;
298
299     importlib->allocated = 0;
300
301     tlb_lseek(fd, 0);
302     tlb_read(fd, &header, sizeof(header));
303
304     importlib->version = header.version;
305
306     typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
307     tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
308     tlb_read(fd, &segdir, sizeof(segdir));
309
310     msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
311
312     importlib->ntypeinfos = header.nrtypeinfos;
313     importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
314
315     for(i=0; i < importlib->ntypeinfos; i++) {
316         MSFT_TypeInfoBase base;
317         MSFT_NameIntro nameintro;
318         int len;
319
320         tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
321                  + typeinfo_offs[i]);
322         tlb_read(fd, &base, sizeof(base));
323
324         importlib->importinfos[i].importlib = importlib;
325         importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
326         importlib->importinfos[i].offset = -1;
327         importlib->importinfos[i].id = i;
328
329         if(base.posguid != -1) {
330             importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
331             msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
332         }
333         else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
334
335         tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
336         tlb_read(fd, &nameintro, sizeof(nameintro));
337
338         len = nameintro.namelen & 0xff;
339
340         importlib->importinfos[i].name = xmalloc(len+1);
341         tlb_read(fd, importlib->importinfos[i].name, len);
342         importlib->importinfos[i].name[len] = 0;
343     }
344
345     free(typeinfo_offs);
346 }
347
348 static void read_importlib(importlib_t *importlib)
349 {
350     int fd;
351     INT magic;
352     char *file_name;
353
354     file_name = wpp_find_include(importlib->name, NULL);
355     if(file_name) {
356         fd = open(file_name, O_RDONLY);
357         free(file_name);
358     }else {
359         fd = open(importlib->name, O_RDONLY);
360     }
361
362     if(fd < 0)
363         error("Could not open importlib %s.\n", importlib->name);
364
365     tlb_read(fd, &magic, sizeof(magic));
366
367     switch(magic) {
368     case MSFT_MAGIC:
369         read_msft_importlib(importlib, fd);
370         break;
371     default:
372         error("Wrong or unsupported typelib magic %x\n", magic);
373     };
374
375     close(fd);
376 }
377
378 void add_importlib(const char *name)
379 {
380     importlib_t *importlib;
381
382     if(!typelib) return;
383
384     LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
385         if(!strcmp(name, importlib->name))
386             return;
387
388     chat("add_importlib: %s\n", name);
389
390     importlib = xmalloc(sizeof(*importlib));
391     memset( importlib, 0, sizeof(*importlib) );
392     importlib->name = xstrdup(name);
393
394     read_importlib(importlib);
395     list_add_head( &typelib->importlibs, &importlib->entry );
396 }