d3d10core: Implement d3d10_device_SetPredication().
[wine/multimedia.git] / tools / widl / typelib.c
blob8632fdfe2edc86434f63f6381c54dc7f3add3129
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 /* List of oleauto types that should be recognized by name.
53 * (most of) these seem to be intrinsic types in mktyplib.
54 * This table MUST be alphabetically sorted on the kw field.
56 static const struct oatype {
57 const char *kw;
58 unsigned short vt;
59 } oatypes[] = {
60 {"BSTR", VT_BSTR},
61 {"CURRENCY", VT_CY},
62 {"DATE", VT_DATE},
63 {"DECIMAL", VT_DECIMAL},
64 {"HRESULT", VT_HRESULT},
65 {"LPSTR", VT_LPSTR},
66 {"LPWSTR", VT_LPWSTR},
67 {"SCODE", VT_ERROR},
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 type_t *t)
81 const char *kw = t->name;
82 struct oatype key;
83 const struct oatype *kwp;
84 key.kw = kw;
85 #ifdef KW_BSEARCH
86 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
87 #else
89 unsigned int i;
90 for (kwp=NULL, i=0; i < NTYPES; i++)
91 if (!kw_cmp_func(&key, &oatypes[i])) {
92 kwp = &oatypes[i];
93 break;
96 #endif
97 if (kwp) {
98 return kwp->vt;
100 if (is_string_type (t->attrs, t))
102 const type_t *elem_type;
103 if (is_array(t))
104 elem_type = type_array_get_element(t);
105 else
106 elem_type = type_pointer_get_ref(t);
107 if (type_get_type(elem_type) == TYPE_BASIC)
109 switch (type_basic_get_type(elem_type))
111 case TYPE_BASIC_CHAR: return VT_LPSTR;
112 case TYPE_BASIC_WCHAR: return VT_LPWSTR;
113 default: break;
117 return 0;
120 static int match(const char*n, const char*m)
122 if (!n) return 0;
123 return !strcmp(n, m);
126 unsigned short get_type_vt(type_t *t)
128 unsigned short vt;
130 chat("get_type_vt: %p type->name %s\n", t, t->name);
131 if (t->name) {
132 vt = builtin_vt(t);
133 if (vt) return vt;
136 if (type_is_alias(t) && is_attr(t->attrs, ATTR_PUBLIC))
137 return VT_USERDEFINED;
139 switch (type_get_type(t)) {
140 case TYPE_BASIC:
141 switch (type_basic_get_type(t)) {
142 case TYPE_BASIC_BYTE:
143 return VT_UI1;
144 case TYPE_BASIC_CHAR:
145 case TYPE_BASIC_INT8:
146 if (type_basic_get_sign(t) > 0)
147 return VT_UI1;
148 else
149 return VT_I1;
150 case TYPE_BASIC_WCHAR:
151 return VT_I2; /* mktyplib seems to parse wchar_t as short */
152 case TYPE_BASIC_INT16:
153 if (type_basic_get_sign(t) > 0)
154 return VT_UI2;
155 else
156 return VT_I2;
157 case TYPE_BASIC_INT:
158 if (type_basic_get_sign(t) > 0)
159 return VT_UINT;
160 else
161 return VT_INT;
162 case TYPE_BASIC_INT32:
163 case TYPE_BASIC_ERROR_STATUS_T:
164 if (type_basic_get_sign(t) > 0)
165 return VT_UI4;
166 else
167 return VT_I4;
168 case TYPE_BASIC_INT64:
169 case TYPE_BASIC_HYPER:
170 if (type_basic_get_sign(t) > 0)
171 return VT_UI8;
172 else
173 return VT_I8;
174 case TYPE_BASIC_INT3264:
175 if (typelib_kind == SYS_WIN64)
177 if (type_basic_get_sign(t) > 0)
178 return VT_UI8;
179 else
180 return VT_I8;
182 else
184 if (type_basic_get_sign(t) > 0)
185 return VT_UI4;
186 else
187 return VT_I4;
189 case TYPE_BASIC_FLOAT:
190 return VT_R4;
191 case TYPE_BASIC_DOUBLE:
192 return VT_R8;
193 case TYPE_BASIC_HANDLE:
194 error("handles can't be used in typelibs\n");
196 break;
198 case TYPE_POINTER:
199 return VT_PTR;
201 case TYPE_ARRAY:
202 if (type_array_is_decl_as_ptr(t))
204 if (match(type_array_get_element(t)->name, "SAFEARRAY"))
205 return VT_SAFEARRAY;
207 else
208 error("get_type_vt: array types not supported\n");
209 return VT_PTR;
211 case TYPE_INTERFACE:
212 if(match(t->name, "IUnknown"))
213 return VT_UNKNOWN;
214 if(match(t->name, "IDispatch"))
215 return VT_DISPATCH;
216 return VT_USERDEFINED;
218 case TYPE_ENUM:
219 case TYPE_STRUCT:
220 case TYPE_COCLASS:
221 case TYPE_MODULE:
222 case TYPE_UNION:
223 case TYPE_ENCAPSULATED_UNION:
224 return VT_USERDEFINED;
226 case TYPE_VOID:
227 return VT_VOID;
229 case TYPE_ALIAS:
230 /* aliases should be filtered out by the type_get_type call above */
231 assert(0);
232 break;
234 case TYPE_FUNCTION:
235 error("get_type_vt: functions not supported\n");
236 break;
238 case TYPE_BITFIELD:
239 error("get_type_vt: bitfields not supported\n");
240 break;
242 return 0;
245 void start_typelib(typelib_t *typelib_type)
247 if (!do_typelib) return;
248 typelib = typelib_type;
251 void end_typelib(void)
253 if (!typelib) return;
255 create_msft_typelib(typelib);
258 static void tlb_read(int fd, void *buf, int count)
260 if(read(fd, buf, count) < count)
261 error("error while reading importlib.\n");
264 static void tlb_lseek(int fd, off_t offset)
266 if(lseek(fd, offset, SEEK_SET) == -1)
267 error("lseek failed\n");
270 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
272 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
273 tlb_read(fd, guid, sizeof(GUID));
276 static void read_msft_importlib(importlib_t *importlib, int fd)
278 MSFT_Header header;
279 MSFT_SegDir segdir;
280 int *typeinfo_offs;
281 int i;
283 importlib->allocated = 0;
285 tlb_lseek(fd, 0);
286 tlb_read(fd, &header, sizeof(header));
288 importlib->version = header.version;
290 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
291 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
292 tlb_read(fd, &segdir, sizeof(segdir));
294 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
296 importlib->ntypeinfos = header.nrtypeinfos;
297 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
299 for(i=0; i < importlib->ntypeinfos; i++) {
300 MSFT_TypeInfoBase base;
301 MSFT_NameIntro nameintro;
302 int len;
304 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
305 + typeinfo_offs[i]);
306 tlb_read(fd, &base, sizeof(base));
308 importlib->importinfos[i].importlib = importlib;
309 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
310 importlib->importinfos[i].offset = -1;
311 importlib->importinfos[i].id = i;
313 if(base.posguid != -1) {
314 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
315 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
317 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
319 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
320 tlb_read(fd, &nameintro, sizeof(nameintro));
322 len = nameintro.namelen & 0xff;
324 importlib->importinfos[i].name = xmalloc(len+1);
325 tlb_read(fd, importlib->importinfos[i].name, len);
326 importlib->importinfos[i].name[len] = 0;
329 free(typeinfo_offs);
332 static void read_importlib(importlib_t *importlib)
334 int fd;
335 INT magic;
336 char *file_name;
338 file_name = wpp_find_include(importlib->name, NULL);
339 if(file_name) {
340 fd = open(file_name, O_RDONLY | O_BINARY );
341 free(file_name);
342 }else {
343 fd = open(importlib->name, O_RDONLY | O_BINARY );
346 if(fd < 0)
347 error("Could not open importlib %s.\n", importlib->name);
349 tlb_read(fd, &magic, sizeof(magic));
351 switch(magic) {
352 case MSFT_MAGIC:
353 read_msft_importlib(importlib, fd);
354 break;
355 default:
356 error("Wrong or unsupported typelib magic %x\n", magic);
359 close(fd);
362 void add_importlib(const char *name)
364 importlib_t *importlib;
366 if(!typelib) return;
368 LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
369 if(!strcmp(name, importlib->name))
370 return;
372 chat("add_importlib: %s\n", name);
374 importlib = xmalloc(sizeof(*importlib));
375 memset( importlib, 0, sizeof(*importlib) );
376 importlib->name = xstrdup(name);
378 read_importlib(importlib);
379 list_add_head( &typelib->importlibs, &importlib->entry );