Release 0.9.39.
[wine/dibdrv.git] / tools / widl / typelib.c
blob608a114717aaee9ba473844cd32cd68ad643f1a1
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 #include <unistd.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <signal.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 = xmalloc(sizeof *d);
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;
74 return a;
77 int is_ptr(const type_t *t)
79 unsigned char c = t->type;
80 return c == RPC_FC_RP
81 || c == RPC_FC_UP
82 || c == RPC_FC_FP
83 || c == RPC_FC_OP;
86 int is_array(const type_t *t)
88 switch (t->type)
90 case RPC_FC_SMFARRAY:
91 case RPC_FC_LGFARRAY:
92 case RPC_FC_SMVARRAY:
93 case RPC_FC_LGVARRAY:
94 case RPC_FC_CARRAY:
95 case RPC_FC_CVARRAY:
96 case RPC_FC_BOGUS_ARRAY:
97 return TRUE;
98 default:
99 return FALSE;
103 /* List of oleauto types that should be recognized by name.
104 * (most of) these seem to be intrinsic types in mktyplib. */
106 static struct oatype {
107 const char *kw;
108 unsigned short vt;
109 } oatypes[] = {
110 {"BSTR", VT_BSTR},
111 {"CURRENCY", VT_CY},
112 {"DATE", VT_DATE},
113 {"DECIMAL", VT_DECIMAL},
114 {"HRESULT", VT_HRESULT},
115 {"LPSTR", VT_LPSTR},
116 {"LPWSTR", VT_LPWSTR},
117 {"SCODE", VT_ERROR},
118 {"VARIANT", VT_VARIANT},
119 {"VARIANT_BOOL", VT_BOOL}
121 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
122 #define KWP(p) ((const struct oatype *)(p))
124 static int kw_cmp_func(const void *s1, const void *s2)
126 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
129 static unsigned short builtin_vt(const char *kw)
131 struct oatype key, *kwp;
132 key.kw = kw;
133 #ifdef KW_BSEARCH
134 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
135 #else
137 unsigned int i;
138 for (kwp=NULL, i=0; i < NTYPES; i++)
139 if (!kw_cmp_func(&key, &oatypes[i])) {
140 kwp = &oatypes[i];
141 break;
144 #endif
145 if (kwp) {
146 return kwp->vt;
148 return 0;
151 static int match(const char*n, const char*m)
153 if (!n) return 0;
154 return !strcmp(n, m);
157 unsigned short get_type_vt(type_t *t)
159 unsigned short vt;
161 chat("get_type_vt: %p type->name %s\n", t, t->name);
162 if (t->name) {
163 vt = builtin_vt(t->name);
164 if (vt) return vt;
167 if (t->kind == TKIND_ALIAS && t->attrs)
168 return VT_USERDEFINED;
170 switch (t->type) {
171 case RPC_FC_BYTE:
172 case RPC_FC_USMALL:
173 return VT_UI1;
174 case RPC_FC_CHAR:
175 case RPC_FC_SMALL:
176 return VT_I1;
177 case RPC_FC_WCHAR:
178 return VT_I2; /* mktyplib seems to parse wchar_t as short */
179 case RPC_FC_SHORT:
180 return VT_I2;
181 case RPC_FC_USHORT:
182 return VT_UI2;
183 case RPC_FC_LONG:
184 if (match(t->name, "int")) return VT_INT;
185 return VT_I4;
186 case RPC_FC_ULONG:
187 if (match(t->name, "int")) return VT_UINT;
188 return VT_UI4;
189 case RPC_FC_HYPER:
190 if (t->sign < 0) return VT_UI8;
191 if (match(t->name, "MIDL_uhyper")) return VT_UI8;
192 return VT_I8;
193 case RPC_FC_FLOAT:
194 return VT_R4;
195 case RPC_FC_DOUBLE:
196 return VT_R8;
197 case RPC_FC_RP:
198 case RPC_FC_UP:
199 case RPC_FC_OP:
200 case RPC_FC_FP:
201 if(t->ref)
203 if (match(t->ref->name, "SAFEARRAY"))
204 return VT_SAFEARRAY;
205 return VT_PTR;
208 error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
209 break;
210 case RPC_FC_IP:
211 if(match(t->name, "IUnknown"))
212 return VT_UNKNOWN;
213 if(match(t->name, "IDispatch"))
214 return VT_DISPATCH;
215 return VT_USERDEFINED;
217 case RPC_FC_ENUM16:
218 case RPC_FC_STRUCT:
219 case RPC_FC_PSTRUCT:
220 case RPC_FC_CSTRUCT:
221 case RPC_FC_CPSTRUCT:
222 case RPC_FC_CVSTRUCT:
223 case RPC_FC_BOGUS_STRUCT:
224 return VT_USERDEFINED;
225 case 0:
226 return t->kind == TKIND_PRIMITIVE ? VT_VOID : VT_USERDEFINED;
227 default:
228 error("get_type_vt: unknown type: 0x%02x\n", t->type);
230 return 0;
233 void start_typelib(char *name, attr_list_t *attrs)
235 in_typelib++;
236 if (!do_typelib) return;
238 typelib = xmalloc(sizeof(*typelib));
239 typelib->name = xstrdup(name);
240 typelib->filename = xstrdup(typelib_name);
241 typelib->attrs = attrs;
242 list_init( &typelib->entries );
243 list_init( &typelib->importlibs );
246 void end_typelib(void)
248 in_typelib--;
249 if (!typelib) return;
251 create_msft_typelib(typelib);
252 return;
255 void add_typelib_entry(type_t *t)
257 typelib_entry_t *entry;
258 if (!typelib) return;
260 chat("add kind %i: %s\n", t->kind, t->name);
261 entry = xmalloc(sizeof(*entry));
262 entry->type = t;
263 list_add_tail( &typelib->entries, &entry->entry );
266 static void tlb_read(int fd, void *buf, int count)
268 if(read(fd, buf, count) < count)
269 error("error while reading importlib.\n");
272 static void tlb_lseek(int fd, off_t offset)
274 if(lseek(fd, offset, SEEK_SET) == -1)
275 error("lseek failed\n");
278 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
280 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
281 tlb_read(fd, guid, sizeof(GUID));
284 static void read_msft_importlib(importlib_t *importlib, int fd)
286 MSFT_Header header;
287 MSFT_SegDir segdir;
288 int *typeinfo_offs;
289 int i;
291 importlib->allocated = 0;
293 tlb_lseek(fd, 0);
294 tlb_read(fd, &header, sizeof(header));
296 importlib->version = header.version;
298 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
299 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
300 tlb_read(fd, &segdir, sizeof(segdir));
302 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
304 importlib->ntypeinfos = header.nrtypeinfos;
305 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
307 for(i=0; i < importlib->ntypeinfos; i++) {
308 MSFT_TypeInfoBase base;
309 MSFT_NameIntro nameintro;
310 int len;
312 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
313 + typeinfo_offs[i]);
314 tlb_read(fd, &base, sizeof(base));
316 importlib->importinfos[i].importlib = importlib;
317 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
318 importlib->importinfos[i].offset = -1;
319 importlib->importinfos[i].id = i;
321 if(base.posguid != -1) {
322 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
323 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
325 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
327 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
328 tlb_read(fd, &nameintro, sizeof(nameintro));
330 len = nameintro.namelen & 0xff;
332 importlib->importinfos[i].name = xmalloc(len+1);
333 tlb_read(fd, importlib->importinfos[i].name, len);
334 importlib->importinfos[i].name[len] = 0;
337 free(typeinfo_offs);
340 static void read_importlib(importlib_t *importlib)
342 int fd;
343 INT magic;
344 char *file_name;
346 file_name = wpp_find_include(importlib->name, NULL);
347 if(file_name) {
348 fd = open(file_name, O_RDONLY);
349 free(file_name);
350 }else {
351 fd = open(importlib->name, O_RDONLY);
354 if(fd < 0)
355 error("Could not open importlib %s.\n", importlib->name);
357 tlb_read(fd, &magic, sizeof(magic));
359 switch(magic) {
360 case MSFT_MAGIC:
361 read_msft_importlib(importlib, fd);
362 break;
363 default:
364 error("Wrong or unsupported typelib magic %x\n", magic);
367 close(fd);
370 void add_importlib(const char *name)
372 importlib_t *importlib;
374 if(!typelib) return;
376 LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
377 if(!strcmp(name, importlib->name))
378 return;
380 chat("add_importlib: %s\n", name);
382 importlib = xmalloc(sizeof(*importlib));
383 memset( importlib, 0, sizeof(*importlib) );
384 importlib->name = xstrdup(name);
386 read_importlib(importlib);
387 list_add_head( &typelib->importlibs, &importlib->entry );