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 = alloc_type();
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;
77 int is_ptr(const type_t *t)
79 unsigned char c = t->type;
86 int is_array(const type_t *t)
96 case RPC_FC_BOGUS_ARRAY:
103 /* List of oleauto types that should be recognized by name.
104 * (most of) these seem to be intrinsic types in mktyplib. */
106 static const struct oatype {
113 {"DECIMAL", VT_DECIMAL},
114 {"HRESULT", VT_HRESULT},
116 {"LPWSTR", VT_LPWSTR},
118 {"VARIANT", VT_VARIANT},
119 {"VARIANT_BOOL", VT_BOOL}
121 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
122 #define KWP(p) ((const struct oatype *)(p))
124 static int kw_cmp_func(const void *s1, const void *s2)
126 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
129 static unsigned short builtin_vt(const type_t *t)
131 const char *kw = t->name;
133 const struct oatype *kwp;
136 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
140 for (kwp=NULL, i=0; i < NTYPES; i++)
141 if (!kw_cmp_func(&key, &oatypes[i])) {
150 if (is_string_type (t->attrs, t))
151 switch (t->ref->type)
153 case RPC_FC_CHAR: return VT_LPSTR;
154 case RPC_FC_WCHAR: return VT_LPWSTR;
160 static int match(const char*n, const char*m)
163 return !strcmp(n, m);
166 unsigned short get_type_vt(type_t *t)
170 chat("get_type_vt: %p type->name %s\n", t, t->name);
176 if (t->kind == TKIND_ALIAS && t->attrs)
177 return VT_USERDEFINED;
187 return VT_I2; /* mktyplib seems to parse wchar_t as short */
193 if (match(t->name, "int")) return VT_INT;
196 if (match(t->name, "int")) return VT_UINT;
199 if (t->sign < 0) return VT_UI8;
200 if (match(t->name, "MIDL_uhyper")) return VT_UI8;
214 if (match(t->ref->name, "SAFEARRAY"))
219 error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
222 if(match(t->name, "IUnknown"))
224 if(match(t->name, "IDispatch"))
226 return VT_USERDEFINED;
232 case RPC_FC_CPSTRUCT:
233 case RPC_FC_CVSTRUCT:
234 case RPC_FC_BOGUS_STRUCT:
235 return VT_USERDEFINED;
237 return t->kind == TKIND_PRIMITIVE ? VT_VOID : VT_USERDEFINED;
239 error("get_type_vt: unknown type: 0x%02x\n", t->type);
244 void start_typelib(char *name, const attr_list_t *attrs)
247 if (!do_typelib) return;
249 typelib = xmalloc(sizeof(*typelib));
250 typelib->name = xstrdup(name);
251 typelib->filename = xstrdup(typelib_name);
252 typelib->attrs = attrs;
253 list_init( &typelib->entries );
254 list_init( &typelib->importlibs );
256 if (is_attr(attrs, ATTR_POINTERDEFAULT))
257 pointer_default = get_attrv(attrs, ATTR_POINTERDEFAULT);
260 void end_typelib(void)
263 if (!typelib) return;
265 create_msft_typelib(typelib);
266 pointer_default = RPC_FC_UP;
270 void add_typelib_entry(type_t *t)
272 typelib_entry_t *entry;
273 if (!typelib) return;
275 chat("add kind %i: %s\n", t->kind, t->name);
276 entry = xmalloc(sizeof(*entry));
278 list_add_tail( &typelib->entries, &entry->entry );
281 static void tlb_read(int fd, void *buf, int count)
283 if(read(fd, buf, count) < count)
284 error("error while reading importlib.\n");
287 static void tlb_lseek(int fd, off_t offset)
289 if(lseek(fd, offset, SEEK_SET) == -1)
290 error("lseek failed\n");
293 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
295 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
296 tlb_read(fd, guid, sizeof(GUID));
299 static void read_msft_importlib(importlib_t *importlib, int fd)
306 importlib->allocated = 0;
309 tlb_read(fd, &header, sizeof(header));
311 importlib->version = header.version;
313 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
314 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
315 tlb_read(fd, &segdir, sizeof(segdir));
317 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
319 importlib->ntypeinfos = header.nrtypeinfos;
320 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
322 for(i=0; i < importlib->ntypeinfos; i++) {
323 MSFT_TypeInfoBase base;
324 MSFT_NameIntro nameintro;
327 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
329 tlb_read(fd, &base, sizeof(base));
331 importlib->importinfos[i].importlib = importlib;
332 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
333 importlib->importinfos[i].offset = -1;
334 importlib->importinfos[i].id = i;
336 if(base.posguid != -1) {
337 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
338 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
340 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
342 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
343 tlb_read(fd, &nameintro, sizeof(nameintro));
345 len = nameintro.namelen & 0xff;
347 importlib->importinfos[i].name = xmalloc(len+1);
348 tlb_read(fd, importlib->importinfos[i].name, len);
349 importlib->importinfos[i].name[len] = 0;
355 static void read_importlib(importlib_t *importlib)
361 file_name = wpp_find_include(importlib->name, NULL);
363 fd = open(file_name, O_RDONLY);
366 fd = open(importlib->name, O_RDONLY);
370 error("Could not open importlib %s.\n", importlib->name);
372 tlb_read(fd, &magic, sizeof(magic));
376 read_msft_importlib(importlib, fd);
379 error("Wrong or unsupported typelib magic %x\n", magic);
385 void add_importlib(const char *name)
387 importlib_t *importlib;
391 LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
392 if(!strcmp(name, importlib->name))
395 chat("add_importlib: %s\n", name);
397 importlib = xmalloc(sizeof(*importlib));
398 memset( importlib, 0, sizeof(*importlib) );
399 importlib->name = xstrdup(name);
401 read_importlib(importlib);
402 list_add_head( &typelib->importlibs, &importlib->entry );