dplayx: Tests for CreatePlayer.
[wine/gsoc_dplay.git] / tools / widl / typelib.c
blobb30b323ed1615cb40d0c26da4faa08ca0d152c8e
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"
49 int in_typelib = 0;
51 static typelib_t *typelib;
53 type_t *duptype(type_t *t, int dupname)
55 type_t *d = alloc_type();
57 *d = *t;
58 if (dupname && t->name)
59 d->name = xstrdup(t->name);
61 d->orig = t;
62 return d;
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;
71 a->attrs = NULL;
72 a->declarray = FALSE;
73 init_loc_info(&a->loc_info);
75 return a;
78 int is_ptr(const type_t *t)
80 unsigned char c = t->type;
81 return c == RPC_FC_RP
82 || c == RPC_FC_UP
83 || c == RPC_FC_FP
84 || c == RPC_FC_OP;
87 int is_array(const type_t *t)
89 switch (t->type)
91 case RPC_FC_SMFARRAY:
92 case RPC_FC_LGFARRAY:
93 case RPC_FC_SMVARRAY:
94 case RPC_FC_LGVARRAY:
95 case RPC_FC_CARRAY:
96 case RPC_FC_CVARRAY:
97 case RPC_FC_BOGUS_ARRAY:
98 return TRUE;
99 default:
100 return FALSE;
104 /* List of oleauto types that should be recognized by name.
105 * (most of) these seem to be intrinsic types in mktyplib.
106 * This table MUST be alphabetically sorted on the kw field.
108 static const struct oatype {
109 const char *kw;
110 unsigned short vt;
111 } oatypes[] = {
112 {"BSTR", VT_BSTR},
113 {"CURRENCY", VT_CY},
114 {"DATE", VT_DATE},
115 {"DECIMAL", VT_DECIMAL},
116 {"HRESULT", VT_HRESULT},
117 {"LPSTR", VT_LPSTR},
118 {"LPWSTR", VT_LPWSTR},
119 {"SCODE", VT_ERROR},
120 {"VARIANT", VT_VARIANT},
121 {"VARIANT_BOOL", VT_BOOL}
123 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
124 #define KWP(p) ((const struct oatype *)(p))
126 static int kw_cmp_func(const void *s1, const void *s2)
128 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
131 static unsigned short builtin_vt(const type_t *t)
133 const char *kw = t->name;
134 struct oatype key;
135 const struct oatype *kwp;
136 key.kw = kw;
137 #ifdef KW_BSEARCH
138 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
139 #else
141 unsigned int i;
142 for (kwp=NULL, i=0; i < NTYPES; i++)
143 if (!kw_cmp_func(&key, &oatypes[i])) {
144 kwp = &oatypes[i];
145 break;
148 #endif
149 if (kwp) {
150 return kwp->vt;
152 if (is_string_type (t->attrs, t))
153 switch (t->ref->type)
155 case RPC_FC_CHAR: return VT_LPSTR;
156 case RPC_FC_WCHAR: return VT_LPWSTR;
157 default: break;
159 return 0;
162 static int match(const char*n, const char*m)
164 if (!n) return 0;
165 return !strcmp(n, m);
168 unsigned short get_type_vt(type_t *t)
170 unsigned short vt;
172 chat("get_type_vt: %p type->name %s\n", t, t->name);
173 if (t->name) {
174 vt = builtin_vt(t);
175 if (vt) return vt;
178 if (t->kind == TKIND_ALIAS && is_attr(t->attrs, ATTR_PUBLIC))
179 return VT_USERDEFINED;
181 switch (t->type) {
182 case RPC_FC_BYTE:
183 case RPC_FC_USMALL:
184 return VT_UI1;
185 case RPC_FC_CHAR:
186 case RPC_FC_SMALL:
187 return VT_I1;
188 case RPC_FC_WCHAR:
189 return VT_I2; /* mktyplib seems to parse wchar_t as short */
190 case RPC_FC_SHORT:
191 return VT_I2;
192 case RPC_FC_USHORT:
193 return VT_UI2;
194 case RPC_FC_LONG:
195 if (match(t->name, "int")) return VT_INT;
196 return VT_I4;
197 case RPC_FC_ULONG:
198 if (match(t->name, "int")) return VT_UINT;
199 return VT_UI4;
200 case RPC_FC_HYPER:
201 if (t->sign < 0) return VT_UI8;
202 if (match(t->name, "MIDL_uhyper")) return VT_UI8;
203 return VT_I8;
204 case RPC_FC_FLOAT:
205 return VT_R4;
206 case RPC_FC_DOUBLE:
207 return VT_R8;
208 case RPC_FC_RP:
209 case RPC_FC_UP:
210 case RPC_FC_OP:
211 case RPC_FC_FP:
212 case RPC_FC_CARRAY:
213 case RPC_FC_CVARRAY:
214 if(t->ref)
216 if (match(t->ref->name, "SAFEARRAY"))
217 return VT_SAFEARRAY;
218 return VT_PTR;
221 error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
222 break;
223 case RPC_FC_IP:
224 if(match(t->name, "IUnknown"))
225 return VT_UNKNOWN;
226 if(match(t->name, "IDispatch"))
227 return VT_DISPATCH;
228 return VT_USERDEFINED;
230 case RPC_FC_ENUM16:
231 case RPC_FC_STRUCT:
232 case RPC_FC_PSTRUCT:
233 case RPC_FC_CSTRUCT:
234 case RPC_FC_CPSTRUCT:
235 case RPC_FC_CVSTRUCT:
236 case RPC_FC_BOGUS_STRUCT:
237 case RPC_FC_COCLASS:
238 return VT_USERDEFINED;
239 case 0:
240 return t->kind == TKIND_PRIMITIVE ? VT_VOID : VT_USERDEFINED;
241 default:
242 error("get_type_vt: unknown type: 0x%02x\n", t->type);
244 return 0;
247 void start_typelib(typelib_t *typelib_type)
249 in_typelib++;
250 if (!do_typelib) return;
252 typelib = typelib_type;
253 typelib->filename = xstrdup(typelib_name);
256 void end_typelib(void)
258 in_typelib--;
259 if (!typelib) return;
261 create_msft_typelib(typelib);
264 void add_typelib_entry(type_t *t)
266 typelib_entry_t *entry;
267 if (!typelib) return;
269 chat("add kind %i: %s\n", t->kind, t->name);
270 entry = xmalloc(sizeof(*entry));
271 entry->type = t;
272 list_add_tail( &typelib->entries, &entry->entry );
275 static void tlb_read(int fd, void *buf, int count)
277 if(read(fd, buf, count) < count)
278 error("error while reading importlib.\n");
281 static void tlb_lseek(int fd, off_t offset)
283 if(lseek(fd, offset, SEEK_SET) == -1)
284 error("lseek failed\n");
287 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
289 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
290 tlb_read(fd, guid, sizeof(GUID));
293 static void read_msft_importlib(importlib_t *importlib, int fd)
295 MSFT_Header header;
296 MSFT_SegDir segdir;
297 int *typeinfo_offs;
298 int i;
300 importlib->allocated = 0;
302 tlb_lseek(fd, 0);
303 tlb_read(fd, &header, sizeof(header));
305 importlib->version = header.version;
307 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
308 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
309 tlb_read(fd, &segdir, sizeof(segdir));
311 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
313 importlib->ntypeinfos = header.nrtypeinfos;
314 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
316 for(i=0; i < importlib->ntypeinfos; i++) {
317 MSFT_TypeInfoBase base;
318 MSFT_NameIntro nameintro;
319 int len;
321 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
322 + typeinfo_offs[i]);
323 tlb_read(fd, &base, sizeof(base));
325 importlib->importinfos[i].importlib = importlib;
326 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
327 importlib->importinfos[i].offset = -1;
328 importlib->importinfos[i].id = i;
330 if(base.posguid != -1) {
331 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
332 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
334 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
336 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
337 tlb_read(fd, &nameintro, sizeof(nameintro));
339 len = nameintro.namelen & 0xff;
341 importlib->importinfos[i].name = xmalloc(len+1);
342 tlb_read(fd, importlib->importinfos[i].name, len);
343 importlib->importinfos[i].name[len] = 0;
346 free(typeinfo_offs);
349 static void read_importlib(importlib_t *importlib)
351 int fd;
352 INT magic;
353 char *file_name;
355 file_name = wpp_find_include(importlib->name, NULL);
356 if(file_name) {
357 fd = open(file_name, O_RDONLY);
358 free(file_name);
359 }else {
360 fd = open(importlib->name, O_RDONLY);
363 if(fd < 0)
364 error("Could not open importlib %s.\n", importlib->name);
366 tlb_read(fd, &magic, sizeof(magic));
368 switch(magic) {
369 case MSFT_MAGIC:
370 read_msft_importlib(importlib, fd);
371 break;
372 default:
373 error("Wrong or unsupported typelib magic %x\n", magic);
376 close(fd);
379 void add_importlib(const char *name)
381 importlib_t *importlib;
383 if(!typelib) return;
385 LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
386 if(!strcmp(name, importlib->name))
387 return;
389 chat("add_importlib: %s\n", name);
391 importlib = xmalloc(sizeof(*importlib));
392 memset( importlib, 0, sizeof(*importlib) );
393 importlib->name = xstrdup(name);
395 read_importlib(importlib);
396 list_add_head( &typelib->importlibs, &importlib->entry );