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 /* List of oleauto types that should be recognized by name.
54 * (most of) these seem to be intrinsic types in mktyplib. */
56 static struct oatype {
63 {"DECIMAL", VT_DECIMAL},
64 {"HRESULT", VT_HRESULT},
66 {"LPWSTR", VT_LPWSTR},
68 {"VARIANT", VT_VARIANT},
69 {"VARIANT_BOOL", VT_BOOL}
71 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
72 #define KWP(p) ((const struct oatype *)(p))
74 static int kw_cmp_func(const void *s1, const void *s2)
76 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
79 static unsigned short builtin_vt(const char *kw)
81 struct oatype key, *kwp;
84 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
88 for (kwp=NULL, i=0; i < NTYPES; i++)
89 if (!kw_cmp_func(&key, &oatypes[i])) {
101 static int match(const char*n, const char*m)
104 return !strcmp(n, m);
107 unsigned short get_type_vt(type_t *t)
111 chat("get_type_vt: %p type->name %s\n", t, t->name);
113 vt = builtin_vt(t->name);
125 return VT_I2; /* mktyplib seems to parse wchar_t as short */
131 if (t->ref && match(t->ref->name, "int")) return VT_INT;
134 if (t->ref && match(t->ref->name, "int")) return VT_UINT;
137 if (t->sign < 0) return VT_UI8;
138 if (t->ref && match(t->ref->name, "MIDL_uhyper")) return VT_UI8;
151 error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
154 if(match(t->name, "IUnknown"))
156 if(match(t->name, "IDispatch"))
158 return VT_USERDEFINED;
164 case RPC_FC_CPSTRUCT:
165 case RPC_FC_CVSTRUCT:
166 case RPC_FC_BOGUS_STRUCT:
167 return VT_USERDEFINED;
170 return VT_USERDEFINED;
173 error("get_type_vt: unknown type: 0x%02x\n", t->type);
178 unsigned short get_var_vt(var_t *v)
182 chat("get_var_vt: %p tname %s\n", v, v->tname);
184 vt = builtin_vt(v->tname);
188 return get_type_vt(v->type);
191 void start_typelib(char *name, attr_t *attrs)
194 if (!do_typelib) return;
196 typelib = xmalloc(sizeof(*typelib));
197 typelib->name = xstrdup(name);
198 typelib->filename = xstrdup(typelib_name);
199 typelib->attrs = attrs;
202 void end_typelib(void)
205 if (!typelib) return;
207 create_msft_typelib(typelib);
211 void add_interface(type_t *iface)
213 typelib_entry_t *entry;
214 if (!typelib) return;
216 chat("add interface: %s\n", iface->name);
217 entry = xmalloc(sizeof(*entry));
218 entry->kind = TKIND_INTERFACE;
219 entry->u.interface = iface;
220 LINK(entry, typelib->entry);
221 typelib->entry = entry;
224 void add_coclass(class_t *cls)
226 typelib_entry_t *entry;
228 if (!typelib) return;
230 chat("add coclass: %s\n", cls->name);
232 entry = xmalloc(sizeof(*entry));
233 entry->kind = TKIND_COCLASS;
234 entry->u.class = cls;
235 LINK(entry, typelib->entry);
236 typelib->entry = entry;
239 void add_module(type_t *module)
241 typelib_entry_t *entry;
242 if (!typelib) return;
244 chat("add module: %s\n", module->name);
245 entry = xmalloc(sizeof(*entry));
246 entry->kind = TKIND_MODULE;
247 entry->u.module = module;
248 LINK(entry, typelib->entry);
249 typelib->entry = entry;
252 void add_struct(type_t *structure)
254 typelib_entry_t *entry;
255 if (!typelib) return;
257 chat("add struct: %s\n", structure->name);
258 entry = xmalloc(sizeof(*entry));
259 entry->kind = TKIND_RECORD;
260 entry->u.structure = structure;
261 LINK(entry, typelib->entry);
262 typelib->entry = entry;
265 void add_enum(type_t *enumeration)
267 typelib_entry_t *entry;
268 if (!typelib) return;
270 chat("add enum: %s\n", enumeration->name);
271 entry = xmalloc(sizeof(*entry));
272 entry->kind = TKIND_ENUM;
273 entry->u.enumeration = enumeration;
274 LINK(entry, typelib->entry);
275 typelib->entry = entry;
278 void add_typedef(type_t *tdef, var_t *name)
280 typelib_entry_t *entry;
281 if (!typelib) return;
283 chat("add typedef: %s\n", name->name);
284 entry = xmalloc(sizeof(*entry));
285 entry->kind = TKIND_ALIAS;
286 entry->u.tdef = xmalloc(sizeof(*entry->u.tdef));
287 memcpy(entry->u.tdef, name, sizeof(*name));
288 entry->u.tdef->type = tdef;
289 entry->u.tdef->name = xstrdup(name->name);
290 LINK(entry, typelib->entry);
291 typelib->entry = entry;
294 static void tlb_read(int fd, void *buf, size_t count)
296 if(read(fd, buf, count) < count)
297 error("error while reading importlib.\n");
300 static void tlb_lseek(int fd, off_t offset)
302 if(lseek(fd, offset, SEEK_SET) == -1)
303 error("lseek failed\n");
306 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
308 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
309 tlb_read(fd, guid, sizeof(GUID));
312 static void read_msft_importlib(importlib_t *importlib, int fd)
319 importlib->allocated = 0;
322 tlb_read(fd, &header, sizeof(header));
324 importlib->version = header.version;
326 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
327 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
328 tlb_read(fd, &segdir, sizeof(segdir));
330 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
332 importlib->ntypeinfos = header.nrtypeinfos;
333 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
335 for(i=0; i < importlib->ntypeinfos; i++) {
336 MSFT_TypeInfoBase base;
337 MSFT_NameIntro nameintro;
340 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
342 tlb_read(fd, &base, sizeof(base));
344 importlib->importinfos[i].importlib = importlib;
345 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
346 importlib->importinfos[i].offset = -1;
347 importlib->importinfos[i].id = i;
349 if(base.posguid != -1) {
350 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
351 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
354 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
355 tlb_read(fd, &nameintro, sizeof(nameintro));
357 len = nameintro.namelen & 0xff;
359 importlib->importinfos[i].name = xmalloc(len+1);
360 tlb_read(fd, importlib->importinfos[i].name, len);
361 importlib->importinfos[i].name[len] = 0;
367 static void read_importlib(importlib_t *importlib)
373 file_name = wpp_find_include(importlib->name, NULL);
375 fd = open(file_name, O_RDONLY);
378 fd = open(importlib->name, O_RDONLY);
382 error("Could not open importlib %s.\n", importlib->name);
384 tlb_read(fd, &magic, sizeof(magic));
388 read_msft_importlib(importlib, fd);
391 error("Wrong or unsupported typelib magic %x\n", magic);
397 void add_importlib(const char *name)
399 importlib_t *importlib;
403 for(importlib = typelib->importlibs; importlib; importlib = NEXT_LINK(importlib)) {
404 if(!strcmp(name, importlib->name))
408 chat("add_importlib: %s\n", name);
410 importlib = xmalloc(sizeof(*importlib));
411 importlib->name = xstrdup(name);
413 read_importlib(importlib);
415 LINK(importlib, typelib->importlibs);
416 typelib->importlibs = importlib;