ieframe: Widen toolbar buttons to accommodate Catalan translation.
[wine.git] / tools / widl / typelib.c
blob6ac748f4041cacec91dd47234c9f9ee4ca2de3cd
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"
47 static typelib_t *typelib;
49 /* List of oleauto types that should be recognized by name.
50 * (most of) these seem to be intrinsic types in mktyplib.
51 * This table MUST be alphabetically sorted on the kw field.
53 static const struct oatype {
54 const char *kw;
55 unsigned short vt;
56 } oatypes[] = {
57 {"BSTR", VT_BSTR},
58 {"CURRENCY", VT_CY},
59 {"DATE", VT_DATE},
60 {"DECIMAL", VT_DECIMAL},
61 {"HRESULT", VT_HRESULT},
62 {"LPSTR", VT_LPSTR},
63 {"LPWSTR", VT_LPWSTR},
64 {"SCODE", VT_ERROR},
65 {"VARIANT", VT_VARIANT},
66 {"VARIANT_BOOL", VT_BOOL}
68 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
69 #define KWP(p) ((const struct oatype *)(p))
71 static int kw_cmp_func(const void *s1, const void *s2)
73 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
76 static unsigned short builtin_vt(const type_t *t)
78 const char *kw = t->name;
79 struct oatype key;
80 const struct oatype *kwp;
81 key.kw = kw;
82 #ifdef KW_BSEARCH
83 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
84 #else
86 unsigned int i;
87 for (kwp=NULL, i=0; i < NTYPES; i++)
88 if (!kw_cmp_func(&key, &oatypes[i])) {
89 kwp = &oatypes[i];
90 break;
93 #endif
94 if (kwp) {
95 return kwp->vt;
97 if (is_string_type (t->attrs, t))
99 const type_t *elem_type;
100 if (is_array(t))
101 elem_type = type_array_get_element(t);
102 else
103 elem_type = type_pointer_get_ref(t);
104 if (type_get_type(elem_type) == TYPE_BASIC)
106 switch (type_basic_get_type(elem_type))
108 case TYPE_BASIC_CHAR: return VT_LPSTR;
109 case TYPE_BASIC_WCHAR: return VT_LPWSTR;
110 default: break;
114 return 0;
117 static int match(const char*n, const char*m)
119 if (!n) return 0;
120 return !strcmp(n, m);
123 unsigned short get_type_vt(type_t *t)
125 unsigned short vt;
127 chat("get_type_vt: %p type->name %s\n", t, t->name);
128 if (t->name) {
129 vt = builtin_vt(t);
130 if (vt) return vt;
133 if (type_is_alias(t) && is_attr(t->attrs, ATTR_PUBLIC))
134 return VT_USERDEFINED;
136 switch (type_get_type(t)) {
137 case TYPE_BASIC:
138 switch (type_basic_get_type(t)) {
139 case TYPE_BASIC_BYTE:
140 return VT_UI1;
141 case TYPE_BASIC_CHAR:
142 case TYPE_BASIC_INT8:
143 if (type_basic_get_sign(t) > 0)
144 return VT_UI1;
145 else
146 return VT_I1;
147 case TYPE_BASIC_WCHAR:
148 return VT_I2; /* mktyplib seems to parse wchar_t as short */
149 case TYPE_BASIC_INT16:
150 if (type_basic_get_sign(t) > 0)
151 return VT_UI2;
152 else
153 return VT_I2;
154 case TYPE_BASIC_INT:
155 if (type_basic_get_sign(t) > 0)
156 return VT_UINT;
157 else
158 return VT_INT;
159 case TYPE_BASIC_INT32:
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 (typelib_kind == SYS_WIN64)
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(t)->name, "SAFEARRAY"))
202 return VT_SAFEARRAY;
204 else
205 error("get_type_vt: array types not supported\n");
206 return VT_PTR;
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 return VT_USERDEFINED;
223 case TYPE_VOID:
224 return VT_VOID;
226 case TYPE_ALIAS:
227 /* aliases should be filtered out by the type_get_type call above */
228 assert(0);
229 break;
231 case TYPE_FUNCTION:
232 error("get_type_vt: functions not supported\n");
233 break;
235 case TYPE_BITFIELD:
236 error("get_type_vt: bitfields not supported\n");
237 break;
239 return 0;
242 void start_typelib(typelib_t *typelib_type)
244 if (!do_typelib) return;
245 typelib = typelib_type;
248 void end_typelib(void)
250 if (!typelib) return;
252 create_msft_typelib(typelib);
255 static void tlb_read(int fd, void *buf, int count)
257 if(read(fd, buf, count) < count)
258 error("error while reading importlib.\n");
261 static void tlb_lseek(int fd, off_t offset)
263 if(lseek(fd, offset, SEEK_SET) == -1)
264 error("lseek failed\n");
267 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
269 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
270 tlb_read(fd, guid, sizeof(GUID));
273 static void read_msft_importlib(importlib_t *importlib, int fd)
275 MSFT_Header header;
276 MSFT_SegDir segdir;
277 int *typeinfo_offs;
278 int i;
280 importlib->allocated = 0;
282 tlb_lseek(fd, 0);
283 tlb_read(fd, &header, sizeof(header));
285 importlib->version = header.version;
287 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
288 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
289 tlb_read(fd, &segdir, sizeof(segdir));
291 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
293 importlib->ntypeinfos = header.nrtypeinfos;
294 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
296 for(i=0; i < importlib->ntypeinfos; i++) {
297 MSFT_TypeInfoBase base;
298 MSFT_NameIntro nameintro;
299 int len;
301 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
302 + typeinfo_offs[i]);
303 tlb_read(fd, &base, sizeof(base));
305 importlib->importinfos[i].importlib = importlib;
306 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
307 importlib->importinfos[i].offset = -1;
308 importlib->importinfos[i].id = i;
310 if(base.posguid != -1) {
311 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
312 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
314 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
316 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
317 tlb_read(fd, &nameintro, sizeof(nameintro));
319 len = nameintro.namelen & 0xff;
321 importlib->importinfos[i].name = xmalloc(len+1);
322 tlb_read(fd, importlib->importinfos[i].name, len);
323 importlib->importinfos[i].name[len] = 0;
326 free(typeinfo_offs);
329 static void read_importlib(importlib_t *importlib)
331 int fd;
332 INT magic;
333 char *file_name;
335 file_name = wpp_find_include(importlib->name, NULL);
336 if(file_name) {
337 fd = open(file_name, O_RDONLY | O_BINARY );
338 free(file_name);
339 }else {
340 fd = open(importlib->name, O_RDONLY | O_BINARY );
343 if(fd < 0)
344 error("Could not open importlib %s.\n", importlib->name);
346 tlb_read(fd, &magic, sizeof(magic));
348 switch(magic) {
349 case MSFT_MAGIC:
350 read_msft_importlib(importlib, fd);
351 break;
352 default:
353 error("Wrong or unsupported typelib magic %x\n", magic);
356 close(fd);
359 void add_importlib(const char *name)
361 importlib_t *importlib;
363 if(!typelib) return;
365 LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
366 if(!strcmp(name, importlib->name))
367 return;
369 chat("add_importlib: %s\n", name);
371 importlib = xmalloc(sizeof(*importlib));
372 memset( importlib, 0, sizeof(*importlib) );
373 importlib->name = xstrdup(name);
375 read_importlib(importlib);
376 list_add_head( &typelib->importlibs, &importlib->entry );