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 /* List of oleauto types that should be recognized by name.
77 * (most of) these seem to be intrinsic types in mktyplib. */
79 static struct oatype
{
86 {"DECIMAL", VT_DECIMAL
},
87 {"HRESULT", VT_HRESULT
},
89 {"LPWSTR", VT_LPWSTR
},
91 {"VARIANT", VT_VARIANT
},
92 {"VARIANT_BOOL", VT_BOOL
}
94 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
95 #define KWP(p) ((const struct oatype *)(p))
97 static int kw_cmp_func(const void *s1
, const void *s2
)
99 return strcmp(KWP(s1
)->kw
, KWP(s2
)->kw
);
102 static unsigned short builtin_vt(const char *kw
)
104 struct oatype key
, *kwp
;
107 kwp
= bsearch(&key
, oatypes
, NTYPES
, sizeof(oatypes
[0]), kw_cmp_func
);
111 for (kwp
=NULL
, i
=0; i
< NTYPES
; i
++)
112 if (!kw_cmp_func(&key
, &oatypes
[i
])) {
124 static int match(const char*n
, const char*m
)
127 return !strcmp(n
, m
);
130 unsigned short get_type_vt(type_t
*t
)
134 chat("get_type_vt: %p type->name %s\n", t
, t
->name
);
136 vt
= builtin_vt(t
->name
);
148 return VT_I2
; /* mktyplib seems to parse wchar_t as short */
154 if (match(t
->name
, "int")) return VT_INT
;
157 if (match(t
->name
, "int")) return VT_UINT
;
160 if (t
->sign
< 0) return VT_UI8
;
161 if (match(t
->name
, "MIDL_uhyper")) return VT_UI8
;
174 error("get_type_vt: unknown-deref-type: %d\n", t
->ref
->type
);
177 if(match(t
->name
, "IUnknown"))
179 if(match(t
->name
, "IDispatch"))
181 return VT_USERDEFINED
;
187 case RPC_FC_CPSTRUCT
:
188 case RPC_FC_CVSTRUCT
:
189 case RPC_FC_BOGUS_STRUCT
:
190 return VT_USERDEFINED
;
193 return VT_USERDEFINED
;
196 error("get_type_vt: unknown type: 0x%02x\n", t
->type
);
201 unsigned short get_var_vt(var_t
*v
)
205 chat("get_var_vt: %p tname %s\n", v
, v
->tname
);
207 vt
= builtin_vt(v
->tname
);
211 return get_type_vt(v
->type
);
214 void start_typelib(char *name
, attr_t
*attrs
)
217 if (!do_typelib
) return;
219 typelib
= xmalloc(sizeof(*typelib
));
220 typelib
->name
= xstrdup(name
);
221 typelib
->filename
= xstrdup(typelib_name
);
222 typelib
->attrs
= attrs
;
223 typelib
->entry
= NULL
;
224 typelib
->importlibs
= NULL
;
227 void end_typelib(void)
230 if (!typelib
) return;
232 create_msft_typelib(typelib
);
236 void add_interface(type_t
*iface
)
238 typelib_entry_t
*entry
;
239 if (!typelib
) return;
241 chat("add interface: %s\n", iface
->name
);
242 entry
= xmalloc(sizeof(*entry
));
243 entry
->kind
= TKIND_INTERFACE
;
244 entry
->u
.interface
= iface
;
245 LINK(entry
, typelib
->entry
);
246 typelib
->entry
= entry
;
249 void add_coclass(type_t
*cls
)
251 typelib_entry_t
*entry
;
253 if (!typelib
) return;
255 chat("add coclass: %s\n", cls
->name
);
257 entry
= xmalloc(sizeof(*entry
));
258 entry
->kind
= TKIND_COCLASS
;
259 entry
->u
.class = cls
;
260 LINK(entry
, typelib
->entry
);
261 typelib
->entry
= entry
;
264 void add_module(type_t
*module
)
266 typelib_entry_t
*entry
;
267 if (!typelib
) return;
269 chat("add module: %s\n", module
->name
);
270 entry
= xmalloc(sizeof(*entry
));
271 entry
->kind
= TKIND_MODULE
;
272 entry
->u
.module
= module
;
273 LINK(entry
, typelib
->entry
);
274 typelib
->entry
= entry
;
277 void add_struct(type_t
*structure
)
279 typelib_entry_t
*entry
;
280 if (!typelib
) return;
282 chat("add struct: %s\n", structure
->name
);
283 entry
= xmalloc(sizeof(*entry
));
284 entry
->kind
= TKIND_RECORD
;
285 entry
->u
.structure
= structure
;
286 LINK(entry
, typelib
->entry
);
287 typelib
->entry
= entry
;
290 void add_enum(type_t
*enumeration
)
292 typelib_entry_t
*entry
;
293 if (!typelib
) return;
295 chat("add enum: %s\n", enumeration
->name
);
296 entry
= xmalloc(sizeof(*entry
));
297 entry
->kind
= TKIND_ENUM
;
298 entry
->u
.enumeration
= enumeration
;
299 LINK(entry
, typelib
->entry
);
300 typelib
->entry
= entry
;
303 void add_typedef(type_t
*tdef
, var_t
*name
)
305 typelib_entry_t
*entry
;
306 if (!typelib
) return;
308 chat("add typedef: %s\n", name
->name
);
309 entry
= xmalloc(sizeof(*entry
));
310 entry
->kind
= TKIND_ALIAS
;
311 entry
->u
.tdef
= xmalloc(sizeof(*entry
->u
.tdef
));
312 memcpy(entry
->u
.tdef
, name
, sizeof(*name
));
313 entry
->u
.tdef
->type
= tdef
;
314 entry
->u
.tdef
->name
= xstrdup(name
->name
);
315 LINK(entry
, typelib
->entry
);
316 typelib
->entry
= entry
;
319 static void tlb_read(int fd
, void *buf
, size_t count
)
321 if(read(fd
, buf
, count
) < count
)
322 error("error while reading importlib.\n");
325 static void tlb_lseek(int fd
, off_t offset
)
327 if(lseek(fd
, offset
, SEEK_SET
) == -1)
328 error("lseek failed\n");
331 static void msft_read_guid(int fd
, MSFT_SegDir
*segdir
, int offset
, GUID
*guid
)
333 tlb_lseek(fd
, segdir
->pGuidTab
.offset
+offset
);
334 tlb_read(fd
, guid
, sizeof(GUID
));
337 static void read_msft_importlib(importlib_t
*importlib
, int fd
)
344 importlib
->allocated
= 0;
347 tlb_read(fd
, &header
, sizeof(header
));
349 importlib
->version
= header
.version
;
351 typeinfo_offs
= xmalloc(header
.nrtypeinfos
*sizeof(INT
));
352 tlb_read(fd
, typeinfo_offs
, header
.nrtypeinfos
*sizeof(INT
));
353 tlb_read(fd
, &segdir
, sizeof(segdir
));
355 msft_read_guid(fd
, &segdir
, header
.posguid
, &importlib
->guid
);
357 importlib
->ntypeinfos
= header
.nrtypeinfos
;
358 importlib
->importinfos
= xmalloc(importlib
->ntypeinfos
*sizeof(importinfo_t
));
360 for(i
=0; i
< importlib
->ntypeinfos
; i
++) {
361 MSFT_TypeInfoBase base
;
362 MSFT_NameIntro nameintro
;
365 tlb_lseek(fd
, sizeof(MSFT_Header
) + header
.nrtypeinfos
*sizeof(INT
) + sizeof(MSFT_SegDir
)
367 tlb_read(fd
, &base
, sizeof(base
));
369 importlib
->importinfos
[i
].importlib
= importlib
;
370 importlib
->importinfos
[i
].flags
= (base
.typekind
&0xf)<<24;
371 importlib
->importinfos
[i
].offset
= -1;
372 importlib
->importinfos
[i
].id
= i
;
374 if(base
.posguid
!= -1) {
375 importlib
->importinfos
[i
].flags
|= MSFT_IMPINFO_OFFSET_IS_GUID
;
376 msft_read_guid(fd
, &segdir
, base
.posguid
, &importlib
->importinfos
[i
].guid
);
378 else memset( &importlib
->importinfos
[i
].guid
, 0, sizeof(importlib
->importinfos
[i
].guid
));
380 tlb_lseek(fd
, segdir
.pNametab
.offset
+ base
.NameOffset
);
381 tlb_read(fd
, &nameintro
, sizeof(nameintro
));
383 len
= nameintro
.namelen
& 0xff;
385 importlib
->importinfos
[i
].name
= xmalloc(len
+1);
386 tlb_read(fd
, importlib
->importinfos
[i
].name
, len
);
387 importlib
->importinfos
[i
].name
[len
] = 0;
393 static void read_importlib(importlib_t
*importlib
)
399 file_name
= wpp_find_include(importlib
->name
, NULL
);
401 fd
= open(file_name
, O_RDONLY
);
404 fd
= open(importlib
->name
, O_RDONLY
);
408 error("Could not open importlib %s.\n", importlib
->name
);
410 tlb_read(fd
, &magic
, sizeof(magic
));
414 read_msft_importlib(importlib
, fd
);
417 error("Wrong or unsupported typelib magic %x\n", magic
);
423 void add_importlib(const char *name
)
425 importlib_t
*importlib
;
429 for(importlib
= typelib
->importlibs
; importlib
; importlib
= NEXT_LINK(importlib
)) {
430 if(!strcmp(name
, importlib
->name
))
434 chat("add_importlib: %s\n", name
);
436 importlib
= xmalloc(sizeof(*importlib
));
437 memset( importlib
, 0, sizeof(*importlib
) );
438 importlib
->name
= xstrdup(name
);
440 read_importlib(importlib
);
442 LINK(importlib
, typelib
->importlibs
);
443 typelib
->importlibs
= importlib
;