winevulkan: Update to VK spec version 1.2.188.
[wine.git] / tools / widl / typelib.c
blob8b2a24013672b94f8a0530d70577db97eb86ac1d
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 #include "windef.h"
36 #include "winbase.h"
38 #include "widl.h"
39 #include "utils.h"
40 #include "parser.h"
41 #include "header.h"
42 #include "typelib.h"
43 #include "widltypes.h"
44 #include "typelib_struct.h"
45 #include "typetree.h"
48 /* List of oleauto types that should be recognized by name.
49 * (most of) these seem to be intrinsic types in mktyplib.
50 * This table MUST be alphabetically sorted on the kw field.
52 static const struct oatype {
53 const char *kw;
54 unsigned short vt;
55 } oatypes[] = {
56 {"BSTR", VT_BSTR},
57 {"CURRENCY", VT_CY},
58 {"DATE", VT_DATE},
59 {"DECIMAL", VT_DECIMAL},
60 {"HRESULT", VT_HRESULT},
61 {"LPSTR", VT_LPSTR},
62 {"LPWSTR", VT_LPWSTR},
63 {"SCODE", VT_ERROR},
64 {"VARIANT", VT_VARIANT},
65 {"VARIANT_BOOL", VT_BOOL}
67 #define KWP(p) ((const struct oatype *)(p))
69 static int kw_cmp_func(const void *s1, const void *s2)
71 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
74 static unsigned short builtin_vt(const type_t *t)
76 const char *kw = t->name;
77 struct oatype key;
78 const struct oatype *kwp;
79 key.kw = kw;
80 #ifdef KW_BSEARCH
81 kwp = bsearch(&key, oatypes, ARRAY_SIZE(oatypes), sizeof(oatypes[0]), kw_cmp_func);
82 #else
84 unsigned int i;
85 for (kwp = NULL, i = 0; i < ARRAY_SIZE(oatypes); i++)
86 if (!kw_cmp_func(&key, &oatypes[i])) {
87 kwp = &oatypes[i];
88 break;
91 #endif
92 if (kwp) {
93 return kwp->vt;
95 if (is_string_type (t->attrs, t))
97 const type_t *elem_type;
98 if (is_array(t))
99 elem_type = type_array_get_element_type(t);
100 else
101 elem_type = type_pointer_get_ref_type(t);
102 if (type_get_type(elem_type) == TYPE_BASIC)
104 switch (type_basic_get_type(elem_type))
106 case TYPE_BASIC_CHAR: return VT_LPSTR;
107 case TYPE_BASIC_WCHAR: return VT_LPWSTR;
108 default: break;
112 return 0;
115 static int match(const char*n, const char*m)
117 if (!n) return 0;
118 return !strcmp(n, m);
121 unsigned short get_type_vt(type_t *t)
123 unsigned short vt;
125 chat("get_type_vt: %p type->name %s\n", t, t->name);
126 if (t->name) {
127 vt = builtin_vt(t);
128 if (vt) return vt;
131 if (type_is_alias(t) &&
132 (is_attr(t->attrs, ATTR_PUBLIC) || is_attr(t->attrs, ATTR_WIREMARSHAL)))
133 return VT_USERDEFINED;
135 switch (type_get_type(t)) {
136 case TYPE_BASIC:
137 switch (type_basic_get_type(t)) {
138 case TYPE_BASIC_BYTE:
139 return VT_UI1;
140 case TYPE_BASIC_CHAR:
141 case TYPE_BASIC_INT8:
142 if (type_basic_get_sign(t) > 0)
143 return VT_UI1;
144 else
145 return VT_I1;
146 case TYPE_BASIC_WCHAR:
147 return VT_I2; /* mktyplib seems to parse wchar_t as short */
148 case TYPE_BASIC_INT16:
149 if (type_basic_get_sign(t) > 0)
150 return VT_UI2;
151 else
152 return VT_I2;
153 case TYPE_BASIC_INT:
154 if (type_basic_get_sign(t) > 0)
155 return VT_UINT;
156 else
157 return VT_INT;
158 case TYPE_BASIC_INT32:
159 case TYPE_BASIC_LONG:
160 case TYPE_BASIC_ERROR_STATUS_T:
161 if (type_basic_get_sign(t) > 0)
162 return VT_UI4;
163 else
164 return VT_I4;
165 case TYPE_BASIC_INT64:
166 case TYPE_BASIC_HYPER:
167 if (type_basic_get_sign(t) > 0)
168 return VT_UI8;
169 else
170 return VT_I8;
171 case TYPE_BASIC_INT3264:
172 if (pointer_size == 8)
174 if (type_basic_get_sign(t) > 0)
175 return VT_UI8;
176 else
177 return VT_I8;
179 else
181 if (type_basic_get_sign(t) > 0)
182 return VT_UI4;
183 else
184 return VT_I4;
186 case TYPE_BASIC_FLOAT:
187 return VT_R4;
188 case TYPE_BASIC_DOUBLE:
189 return VT_R8;
190 case TYPE_BASIC_HANDLE:
191 error("handles can't be used in typelibs\n");
193 break;
195 case TYPE_POINTER:
196 return VT_PTR;
198 case TYPE_ARRAY:
199 if (type_array_is_decl_as_ptr(t))
201 if (match(type_array_get_element_type(t)->name, "SAFEARRAY"))
202 return VT_SAFEARRAY;
203 return VT_PTR;
205 else
206 return VT_CARRAY;
208 case TYPE_INTERFACE:
209 if(match(t->name, "IUnknown"))
210 return VT_UNKNOWN;
211 if(match(t->name, "IDispatch"))
212 return VT_DISPATCH;
213 return VT_USERDEFINED;
215 case TYPE_ENUM:
216 case TYPE_STRUCT:
217 case TYPE_COCLASS:
218 case TYPE_MODULE:
219 case TYPE_UNION:
220 case TYPE_ENCAPSULATED_UNION:
221 case TYPE_RUNTIMECLASS:
222 case TYPE_DELEGATE:
223 return VT_USERDEFINED;
225 case TYPE_VOID:
226 return VT_VOID;
228 case TYPE_ALIAS:
229 case TYPE_APICONTRACT:
230 case TYPE_PARAMETERIZED_TYPE:
231 case TYPE_PARAMETER:
232 /* not supposed to be here */
233 assert(0);
234 break;
236 case TYPE_FUNCTION:
237 error("get_type_vt: functions not supported\n");
238 break;
240 case TYPE_BITFIELD:
241 error("get_type_vt: bitfields not supported\n");
242 break;
244 return 0;
247 static void tlb_read(int fd, void *buf, int count)
249 if(read(fd, buf, count) < count)
250 error("error while reading importlib.\n");
253 static void tlb_lseek(int fd, off_t offset)
255 if(lseek(fd, offset, SEEK_SET) == -1)
256 error("lseek failed\n");
259 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
261 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
262 tlb_read(fd, guid, sizeof(GUID));
265 static void read_msft_importlib(importlib_t *importlib, int fd)
267 MSFT_Header header;
268 MSFT_SegDir segdir;
269 int *typeinfo_offs;
270 int i;
272 importlib->allocated = 0;
274 tlb_lseek(fd, 0);
275 tlb_read(fd, &header, sizeof(header));
277 importlib->version = header.version;
279 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
280 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
281 tlb_read(fd, &segdir, sizeof(segdir));
283 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
285 importlib->ntypeinfos = header.nrtypeinfos;
286 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
288 for(i=0; i < importlib->ntypeinfos; i++) {
289 MSFT_TypeInfoBase base;
290 MSFT_NameIntro nameintro;
291 int len;
293 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
294 + typeinfo_offs[i]);
295 tlb_read(fd, &base, sizeof(base));
297 importlib->importinfos[i].importlib = importlib;
298 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
299 importlib->importinfos[i].offset = -1;
300 importlib->importinfos[i].id = i;
302 if(base.posguid != -1) {
303 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
304 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
306 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
308 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
309 tlb_read(fd, &nameintro, sizeof(nameintro));
311 len = nameintro.namelen & 0xff;
313 importlib->importinfos[i].name = xmalloc(len+1);
314 tlb_read(fd, importlib->importinfos[i].name, len);
315 importlib->importinfos[i].name[len] = 0;
318 free(typeinfo_offs);
321 static int open_typelib(const char *name)
323 char *file_name;
324 int fd;
326 file_name = wpp_find_include(name, NULL);
327 if(!file_name)
328 return open(name, O_RDONLY | O_BINARY );
330 fd = open(file_name, O_RDONLY | O_BINARY );
331 free(file_name);
332 return fd;
335 static void read_importlib(importlib_t *importlib)
337 int fd;
338 INT magic;
340 fd = open_typelib(importlib->name);
342 /* widl extension: if importlib name has no .tlb extension, try using .tlb */
343 if(fd < 0) {
344 const char *p = strrchr(importlib->name, '.');
345 size_t len = p ? p - importlib->name : strlen(importlib->name);
346 if(strcmp(importlib->name + len, ".tlb")) {
347 char *tlb_name = xmalloc(len + 5);
348 memcpy(tlb_name, importlib->name, len);
349 strcpy(tlb_name + len, ".tlb");
350 fd = open_typelib(tlb_name);
351 free(tlb_name);
355 if(fd < 0)
356 error("Could not find importlib %s.\n", importlib->name);
358 tlb_read(fd, &magic, sizeof(magic));
360 switch(magic) {
361 case MSFT_MAGIC:
362 read_msft_importlib(importlib, fd);
363 break;
364 default:
365 error("Wrong or unsupported typelib magic %x\n", magic);
368 close(fd);
371 void add_importlib(const char *name, typelib_t *typelib)
373 importlib_t *importlib;
375 if(!typelib) return;
377 LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
378 if(!strcmp(name, importlib->name))
379 return;
381 chat("add_importlib: %s\n", name);
383 importlib = xmalloc(sizeof(*importlib));
384 memset( importlib, 0, sizeof(*importlib) );
385 importlib->name = xstrdup(name);
387 read_importlib(importlib);
388 list_add_head( &typelib->importlibs, &importlib->entry );