4 * Copyright 2004 Ove Kaaven
5 * Copyright 2006 Jacek Caban for CodeWeavers
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.
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.
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
23 #include "wine/port.h"
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
46 #include "widltypes.h"
47 #include "typelib_struct.h"
51 static typelib_t *typelib;
53 type_t *duptype(type_t *t, int dupname)
55 type_t *d = xmalloc(sizeof *d);
58 if (dupname && t->name)
59 d->name = xstrdup(t->name);
65 type_t *alias(type_t *t, const char *name)
67 type_t *a = duptype(t, 0);
69 a->name = xstrdup(name);
70 a->kind = TKIND_ALIAS;
76 int is_ptr(const type_t *t)
78 unsigned char c = t->type;
85 /* List of oleauto types that should be recognized by name.
86 * (most of) these seem to be intrinsic types in mktyplib. */
88 static struct oatype {
95 {"DECIMAL", VT_DECIMAL},
96 {"HRESULT", VT_HRESULT},
98 {"LPWSTR", VT_LPWSTR},
100 {"VARIANT", VT_VARIANT},
101 {"VARIANT_BOOL", VT_BOOL}
103 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
104 #define KWP(p) ((const struct oatype *)(p))
106 static int kw_cmp_func(const void *s1, const void *s2)
108 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
111 static unsigned short builtin_vt(const char *kw)
113 struct oatype key, *kwp;
116 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
120 for (kwp=NULL, i=0; i < NTYPES; i++)
121 if (!kw_cmp_func(&key, &oatypes[i])) {
133 static int match(const char*n, const char*m)
136 return !strcmp(n, m);
139 unsigned short get_type_vt(type_t *t)
143 chat("get_type_vt: %p type->name %s\n", t, t->name);
145 vt = builtin_vt(t->name);
149 if (t->kind == TKIND_ALIAS && t->attrs)
150 return VT_USERDEFINED;
160 return VT_I2; /* mktyplib seems to parse wchar_t as short */
166 if (match(t->name, "int")) return VT_INT;
169 if (match(t->name, "int")) return VT_UINT;
172 if (t->sign < 0) return VT_UI8;
173 if (match(t->name, "MIDL_uhyper")) return VT_UI8;
186 error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
189 if(match(t->name, "IUnknown"))
191 if(match(t->name, "IDispatch"))
193 return VT_USERDEFINED;
199 case RPC_FC_CPSTRUCT:
200 case RPC_FC_CVSTRUCT:
201 case RPC_FC_BOGUS_STRUCT:
202 return VT_USERDEFINED;
204 return t->kind == TKIND_PRIMITIVE ? VT_VOID : VT_USERDEFINED;
206 error("get_type_vt: unknown type: 0x%02x\n", t->type);
211 unsigned short get_var_vt(var_t *v)
215 chat("get_var_vt: %p tname %s\n", v, v->tname);
217 vt = builtin_vt(v->tname);
221 return get_type_vt(v->type);
224 void start_typelib(char *name, attr_list_t *attrs)
227 if (!do_typelib) return;
229 typelib = xmalloc(sizeof(*typelib));
230 typelib->name = xstrdup(name);
231 typelib->filename = xstrdup(typelib_name);
232 typelib->attrs = attrs;
233 list_init( &typelib->entries );
234 list_init( &typelib->importlibs );
237 void end_typelib(void)
240 if (!typelib) return;
242 create_msft_typelib(typelib);
246 void add_typelib_entry(type_t *t)
248 typelib_entry_t *entry;
249 if (!typelib) return;
251 chat("add kind %i: %s\n", t->kind, t->name);
252 entry = xmalloc(sizeof(*entry));
254 list_add_tail( &typelib->entries, &entry->entry );
257 static void tlb_read(int fd, void *buf, int count)
259 if(read(fd, buf, count) < count)
260 error("error while reading importlib.\n");
263 static void tlb_lseek(int fd, off_t offset)
265 if(lseek(fd, offset, SEEK_SET) == -1)
266 error("lseek failed\n");
269 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
271 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
272 tlb_read(fd, guid, sizeof(GUID));
275 static void read_msft_importlib(importlib_t *importlib, int fd)
282 importlib->allocated = 0;
285 tlb_read(fd, &header, sizeof(header));
287 importlib->version = header.version;
289 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
290 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
291 tlb_read(fd, &segdir, sizeof(segdir));
293 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
295 importlib->ntypeinfos = header.nrtypeinfos;
296 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
298 for(i=0; i < importlib->ntypeinfos; i++) {
299 MSFT_TypeInfoBase base;
300 MSFT_NameIntro nameintro;
303 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
305 tlb_read(fd, &base, sizeof(base));
307 importlib->importinfos[i].importlib = importlib;
308 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
309 importlib->importinfos[i].offset = -1;
310 importlib->importinfos[i].id = i;
312 if(base.posguid != -1) {
313 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
314 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
316 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
318 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
319 tlb_read(fd, &nameintro, sizeof(nameintro));
321 len = nameintro.namelen & 0xff;
323 importlib->importinfos[i].name = xmalloc(len+1);
324 tlb_read(fd, importlib->importinfos[i].name, len);
325 importlib->importinfos[i].name[len] = 0;
331 static void read_importlib(importlib_t *importlib)
337 file_name = wpp_find_include(importlib->name, NULL);
339 fd = open(file_name, O_RDONLY);
342 fd = open(importlib->name, O_RDONLY);
346 error("Could not open importlib %s.\n", importlib->name);
348 tlb_read(fd, &magic, sizeof(magic));
352 read_msft_importlib(importlib, fd);
355 error("Wrong or unsupported typelib magic %x\n", magic);
361 void add_importlib(const char *name)
363 importlib_t *importlib;
367 LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
368 if(!strcmp(name, importlib->name))
371 chat("add_importlib: %s\n", name);
373 importlib = xmalloc(sizeof(*importlib));
374 memset( importlib, 0, sizeof(*importlib) );
375 importlib->name = xstrdup(name);
377 read_importlib(importlib);
378 list_add_head( &typelib->importlibs, &importlib->entry );