push bf834a7eef2241618d351018da1587a7ae2466d1
[wine/hacks.git] / tools / widl / typelib.c
blob55a2e22c4fab4f04a7959e819de8fff32b5aa541
1 /*
2 * IDL Compiler
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
22 #include "config.h"
23 #include "wine/port.h"
24 #include "wine/wpp.h"
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>
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
38 #include "windef.h"
39 #include "winbase.h"
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 #include "typetree.h"
50 static typelib_t *typelib;
52 int is_ptr(const type_t *t)
54 unsigned char c = t->type;
55 return c == RPC_FC_RP
56 || c == RPC_FC_UP
57 || c == RPC_FC_FP
58 || c == RPC_FC_OP;
61 int is_array(const type_t *t)
63 switch (t->type)
65 case RPC_FC_SMFARRAY:
66 case RPC_FC_LGFARRAY:
67 case RPC_FC_SMVARRAY:
68 case RPC_FC_LGVARRAY:
69 case RPC_FC_CARRAY:
70 case RPC_FC_CVARRAY:
71 case RPC_FC_BOGUS_ARRAY:
72 return TRUE;
73 default:
74 return FALSE;
78 /* List of oleauto types that should be recognized by name.
79 * (most of) these seem to be intrinsic types in mktyplib.
80 * This table MUST be alphabetically sorted on the kw field.
82 static const struct oatype {
83 const char *kw;
84 unsigned short vt;
85 } oatypes[] = {
86 {"BSTR", VT_BSTR},
87 {"CURRENCY", VT_CY},
88 {"DATE", VT_DATE},
89 {"DECIMAL", VT_DECIMAL},
90 {"HRESULT", VT_HRESULT},
91 {"LPSTR", VT_LPSTR},
92 {"LPWSTR", VT_LPWSTR},
93 {"SCODE", VT_ERROR},
94 {"VARIANT", VT_VARIANT},
95 {"VARIANT_BOOL", VT_BOOL}
97 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
98 #define KWP(p) ((const struct oatype *)(p))
100 static int kw_cmp_func(const void *s1, const void *s2)
102 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
105 static unsigned short builtin_vt(const type_t *t)
107 const char *kw = t->name;
108 struct oatype key;
109 const struct oatype *kwp;
110 key.kw = kw;
111 #ifdef KW_BSEARCH
112 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
113 #else
115 unsigned int i;
116 for (kwp=NULL, i=0; i < NTYPES; i++)
117 if (!kw_cmp_func(&key, &oatypes[i])) {
118 kwp = &oatypes[i];
119 break;
122 #endif
123 if (kwp) {
124 return kwp->vt;
126 if (is_string_type (t->attrs, t))
128 unsigned char fc;
129 if (is_array(t))
130 fc = type_array_get_element(t)->type;
131 else
132 fc = type_pointer_get_ref(t)->type;
133 switch (fc)
135 case RPC_FC_CHAR: return VT_LPSTR;
136 case RPC_FC_WCHAR: return VT_LPWSTR;
137 default: break;
140 return 0;
143 static int match(const char*n, const char*m)
145 if (!n) return 0;
146 return !strcmp(n, m);
149 unsigned short get_type_vt(type_t *t)
151 unsigned short vt;
153 chat("get_type_vt: %p type->name %s\n", t, t->name);
154 if (t->name) {
155 vt = builtin_vt(t);
156 if (vt) return vt;
159 if (type_is_alias(t) && is_attr(t->attrs, ATTR_PUBLIC))
160 return VT_USERDEFINED;
162 switch (t->type) {
163 case RPC_FC_BYTE:
164 case RPC_FC_USMALL:
165 return VT_UI1;
166 case RPC_FC_CHAR:
167 case RPC_FC_SMALL:
168 return VT_I1;
169 case RPC_FC_WCHAR:
170 return VT_I2; /* mktyplib seems to parse wchar_t as short */
171 case RPC_FC_SHORT:
172 return VT_I2;
173 case RPC_FC_USHORT:
174 return VT_UI2;
175 case RPC_FC_LONG:
176 if (match(t->name, "int")) return VT_INT;
177 return VT_I4;
178 case RPC_FC_ULONG:
179 if (match(t->name, "int")) return VT_UINT;
180 return VT_UI4;
181 case RPC_FC_HYPER:
182 if (t->sign < 0) return VT_UI8;
183 if (match(t->name, "MIDL_uhyper")) return VT_UI8;
184 return VT_I8;
185 case RPC_FC_FLOAT:
186 return VT_R4;
187 case RPC_FC_DOUBLE:
188 return VT_R8;
189 case RPC_FC_RP:
190 case RPC_FC_UP:
191 case RPC_FC_OP:
192 case RPC_FC_FP:
193 case RPC_FC_SMFARRAY:
194 case RPC_FC_LGFARRAY:
195 case RPC_FC_SMVARRAY:
196 case RPC_FC_LGVARRAY:
197 case RPC_FC_CARRAY:
198 case RPC_FC_CVARRAY:
199 case RPC_FC_BOGUS_ARRAY:
200 if(t->ref)
202 if (match(t->ref->name, "SAFEARRAY"))
203 return VT_SAFEARRAY;
204 return VT_PTR;
207 error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
208 break;
209 case RPC_FC_IP:
210 if(match(t->name, "IUnknown"))
211 return VT_UNKNOWN;
212 if(match(t->name, "IDispatch"))
213 return VT_DISPATCH;
214 return VT_USERDEFINED;
216 case RPC_FC_ENUM16:
217 case RPC_FC_STRUCT:
218 case RPC_FC_PSTRUCT:
219 case RPC_FC_CSTRUCT:
220 case RPC_FC_CPSTRUCT:
221 case RPC_FC_CVSTRUCT:
222 case RPC_FC_BOGUS_STRUCT:
223 case RPC_FC_COCLASS:
224 case RPC_FC_MODULE:
225 return VT_USERDEFINED;
226 case 0:
227 return VT_VOID;
228 default:
229 error("get_type_vt: unknown type: 0x%02x\n", t->type);
231 return 0;
234 void start_typelib(typelib_t *typelib_type)
236 if (!do_typelib) return;
238 typelib = typelib_type;
239 typelib->filename = xstrdup(typelib_name);
242 void end_typelib(void)
244 if (!typelib) return;
246 create_msft_typelib(typelib);
249 static void tlb_read(int fd, void *buf, int count)
251 if(read(fd, buf, count) < count)
252 error("error while reading importlib.\n");
255 static void tlb_lseek(int fd, off_t offset)
257 if(lseek(fd, offset, SEEK_SET) == -1)
258 error("lseek failed\n");
261 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
263 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
264 tlb_read(fd, guid, sizeof(GUID));
267 static void read_msft_importlib(importlib_t *importlib, int fd)
269 MSFT_Header header;
270 MSFT_SegDir segdir;
271 int *typeinfo_offs;
272 int i;
274 importlib->allocated = 0;
276 tlb_lseek(fd, 0);
277 tlb_read(fd, &header, sizeof(header));
279 importlib->version = header.version;
281 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
282 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
283 tlb_read(fd, &segdir, sizeof(segdir));
285 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
287 importlib->ntypeinfos = header.nrtypeinfos;
288 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
290 for(i=0; i < importlib->ntypeinfos; i++) {
291 MSFT_TypeInfoBase base;
292 MSFT_NameIntro nameintro;
293 int len;
295 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
296 + typeinfo_offs[i]);
297 tlb_read(fd, &base, sizeof(base));
299 importlib->importinfos[i].importlib = importlib;
300 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
301 importlib->importinfos[i].offset = -1;
302 importlib->importinfos[i].id = i;
304 if(base.posguid != -1) {
305 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
306 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
308 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
310 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
311 tlb_read(fd, &nameintro, sizeof(nameintro));
313 len = nameintro.namelen & 0xff;
315 importlib->importinfos[i].name = xmalloc(len+1);
316 tlb_read(fd, importlib->importinfos[i].name, len);
317 importlib->importinfos[i].name[len] = 0;
320 free(typeinfo_offs);
323 static void read_importlib(importlib_t *importlib)
325 int fd;
326 INT magic;
327 char *file_name;
329 file_name = wpp_find_include(importlib->name, NULL);
330 if(file_name) {
331 fd = open(file_name, O_RDONLY | O_BINARY );
332 free(file_name);
333 }else {
334 fd = open(importlib->name, O_RDONLY | O_BINARY );
337 if(fd < 0)
338 error("Could not open importlib %s.\n", importlib->name);
340 tlb_read(fd, &magic, sizeof(magic));
342 switch(magic) {
343 case MSFT_MAGIC:
344 read_msft_importlib(importlib, fd);
345 break;
346 default:
347 error("Wrong or unsupported typelib magic %x\n", magic);
350 close(fd);
353 void add_importlib(const char *name)
355 importlib_t *importlib;
357 if(!typelib) return;
359 LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
360 if(!strcmp(name, importlib->name))
361 return;
363 chat("add_importlib: %s\n", name);
365 importlib = xmalloc(sizeof(*importlib));
366 memset( importlib, 0, sizeof(*importlib) );
367 importlib->name = xstrdup(name);
369 read_importlib(importlib);
370 list_add_head( &typelib->importlibs, &importlib->entry );