widl: xmalloc shouldn't initialize to zero, do that explicitly where needed.
[wine.git] / tools / widl / typelib.c
blob52180705858e8c222594497420529f332789b9a0
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;
73 return a;
76 /* List of oleauto types that should be recognized by name.
77 * (most of) these seem to be intrinsic types in mktyplib. */
79 static struct oatype {
80 const char *kw;
81 unsigned short vt;
82 } oatypes[] = {
83 {"BSTR", VT_BSTR},
84 {"CURRENCY", VT_CY},
85 {"DATE", VT_DATE},
86 {"DECIMAL", VT_DECIMAL},
87 {"HRESULT", VT_HRESULT},
88 {"LPSTR", VT_LPSTR},
89 {"LPWSTR", VT_LPWSTR},
90 {"SCODE", VT_ERROR},
91 {"VARIANT", VT_VARIANT},
92 {"VARIANT_BOOL", VT_BOOL}
94 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
95 #define KWP(p) ((const struct oatype *)(p))
97 static int kw_cmp_func(const void *s1, const void *s2)
99 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
102 static unsigned short builtin_vt(const char *kw)
104 struct oatype key, *kwp;
105 key.kw = kw;
106 #ifdef KW_BSEARCH
107 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
108 #else
110 unsigned int i;
111 for (kwp=NULL, i=0; i < NTYPES; i++)
112 if (!kw_cmp_func(&key, &oatypes[i])) {
113 kwp = &oatypes[i];
114 break;
117 #endif
118 if (kwp) {
119 return kwp->vt;
121 return 0;
124 static int match(const char*n, const char*m)
126 if (!n) return 0;
127 return !strcmp(n, m);
130 unsigned short get_type_vt(type_t *t)
132 unsigned short vt;
134 chat("get_type_vt: %p type->name %s\n", t, t->name);
135 if (t->name) {
136 vt = builtin_vt(t->name);
137 if (vt) return vt;
140 switch (t->type) {
141 case RPC_FC_BYTE:
142 case RPC_FC_USMALL:
143 return VT_UI1;
144 case RPC_FC_CHAR:
145 case RPC_FC_SMALL:
146 return VT_I1;
147 case RPC_FC_WCHAR:
148 return VT_I2; /* mktyplib seems to parse wchar_t as short */
149 case RPC_FC_SHORT:
150 return VT_I2;
151 case RPC_FC_USHORT:
152 return VT_UI2;
153 case RPC_FC_LONG:
154 if (match(t->name, "int")) return VT_INT;
155 return VT_I4;
156 case RPC_FC_ULONG:
157 if (match(t->name, "int")) return VT_UINT;
158 return VT_UI4;
159 case RPC_FC_HYPER:
160 if (t->sign < 0) return VT_UI8;
161 if (match(t->name, "MIDL_uhyper")) return VT_UI8;
162 return VT_I8;
163 case RPC_FC_FLOAT:
164 return VT_R4;
165 case RPC_FC_DOUBLE:
166 return VT_R8;
167 case RPC_FC_RP:
168 case RPC_FC_UP:
169 case RPC_FC_OP:
170 case RPC_FC_FP:
171 if(t->ref)
172 return VT_PTR;
174 error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
175 break;
176 case RPC_FC_IP:
177 if(match(t->name, "IUnknown"))
178 return VT_UNKNOWN;
179 if(match(t->name, "IDispatch"))
180 return VT_DISPATCH;
181 return VT_USERDEFINED;
183 case RPC_FC_ENUM16:
184 case RPC_FC_STRUCT:
185 case RPC_FC_PSTRUCT:
186 case RPC_FC_CSTRUCT:
187 case RPC_FC_CPSTRUCT:
188 case RPC_FC_CVSTRUCT:
189 case RPC_FC_BOGUS_STRUCT:
190 return VT_USERDEFINED;
191 case 0:
192 if(t->attrs)
193 return VT_USERDEFINED;
194 return 0;
195 default:
196 error("get_type_vt: unknown type: 0x%02x\n", t->type);
198 return 0;
201 unsigned short get_var_vt(var_t *v)
203 unsigned short vt;
205 chat("get_var_vt: %p tname %s\n", v, v->tname);
206 if (v->tname) {
207 vt = builtin_vt(v->tname);
208 if (vt) return vt;
211 return get_type_vt(v->type);
214 void start_typelib(char *name, attr_t *attrs)
216 in_typelib++;
217 if (!do_typelib) return;
219 typelib = xmalloc(sizeof(*typelib));
220 typelib->name = xstrdup(name);
221 typelib->filename = xstrdup(typelib_name);
222 typelib->attrs = attrs;
223 typelib->entry = NULL;
224 typelib->importlibs = NULL;
227 void end_typelib(void)
229 in_typelib--;
230 if (!typelib) return;
232 create_msft_typelib(typelib);
233 return;
236 void add_interface(type_t *iface)
238 typelib_entry_t *entry;
239 if (!typelib) return;
241 chat("add interface: %s\n", iface->name);
242 entry = xmalloc(sizeof(*entry));
243 entry->kind = TKIND_INTERFACE;
244 entry->u.interface = iface;
245 LINK(entry, typelib->entry);
246 typelib->entry = entry;
249 void add_coclass(type_t *cls)
251 typelib_entry_t *entry;
253 if (!typelib) return;
255 chat("add coclass: %s\n", cls->name);
257 entry = xmalloc(sizeof(*entry));
258 entry->kind = TKIND_COCLASS;
259 entry->u.class = cls;
260 LINK(entry, typelib->entry);
261 typelib->entry = entry;
264 void add_module(type_t *module)
266 typelib_entry_t *entry;
267 if (!typelib) return;
269 chat("add module: %s\n", module->name);
270 entry = xmalloc(sizeof(*entry));
271 entry->kind = TKIND_MODULE;
272 entry->u.module = module;
273 LINK(entry, typelib->entry);
274 typelib->entry = entry;
277 void add_struct(type_t *structure)
279 typelib_entry_t *entry;
280 if (!typelib) return;
282 chat("add struct: %s\n", structure->name);
283 entry = xmalloc(sizeof(*entry));
284 entry->kind = TKIND_RECORD;
285 entry->u.structure = structure;
286 LINK(entry, typelib->entry);
287 typelib->entry = entry;
290 void add_enum(type_t *enumeration)
292 typelib_entry_t *entry;
293 if (!typelib) return;
295 chat("add enum: %s\n", enumeration->name);
296 entry = xmalloc(sizeof(*entry));
297 entry->kind = TKIND_ENUM;
298 entry->u.enumeration = enumeration;
299 LINK(entry, typelib->entry);
300 typelib->entry = entry;
303 void add_typedef(type_t *tdef, var_t *name)
305 typelib_entry_t *entry;
306 if (!typelib) return;
308 chat("add typedef: %s\n", name->name);
309 entry = xmalloc(sizeof(*entry));
310 entry->kind = TKIND_ALIAS;
311 entry->u.tdef = xmalloc(sizeof(*entry->u.tdef));
312 memcpy(entry->u.tdef, name, sizeof(*name));
313 entry->u.tdef->type = tdef;
314 entry->u.tdef->name = xstrdup(name->name);
315 LINK(entry, typelib->entry);
316 typelib->entry = entry;
319 static void tlb_read(int fd, void *buf, size_t count)
321 if(read(fd, buf, count) < count)
322 error("error while reading importlib.\n");
325 static void tlb_lseek(int fd, off_t offset)
327 if(lseek(fd, offset, SEEK_SET) == -1)
328 error("lseek failed\n");
331 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
333 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
334 tlb_read(fd, guid, sizeof(GUID));
337 static void read_msft_importlib(importlib_t *importlib, int fd)
339 MSFT_Header header;
340 MSFT_SegDir segdir;
341 int *typeinfo_offs;
342 int i;
344 importlib->allocated = 0;
346 tlb_lseek(fd, 0);
347 tlb_read(fd, &header, sizeof(header));
349 importlib->version = header.version;
351 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
352 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
353 tlb_read(fd, &segdir, sizeof(segdir));
355 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
357 importlib->ntypeinfos = header.nrtypeinfos;
358 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
360 for(i=0; i < importlib->ntypeinfos; i++) {
361 MSFT_TypeInfoBase base;
362 MSFT_NameIntro nameintro;
363 int len;
365 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
366 + typeinfo_offs[i]);
367 tlb_read(fd, &base, sizeof(base));
369 importlib->importinfos[i].importlib = importlib;
370 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
371 importlib->importinfos[i].offset = -1;
372 importlib->importinfos[i].id = i;
374 if(base.posguid != -1) {
375 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
376 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
378 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
380 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
381 tlb_read(fd, &nameintro, sizeof(nameintro));
383 len = nameintro.namelen & 0xff;
385 importlib->importinfos[i].name = xmalloc(len+1);
386 tlb_read(fd, importlib->importinfos[i].name, len);
387 importlib->importinfos[i].name[len] = 0;
390 free(typeinfo_offs);
393 static void read_importlib(importlib_t *importlib)
395 int fd;
396 INT magic;
397 char *file_name;
399 file_name = wpp_find_include(importlib->name, NULL);
400 if(file_name) {
401 fd = open(file_name, O_RDONLY);
402 free(file_name);
403 }else {
404 fd = open(importlib->name, O_RDONLY);
407 if(fd < 0)
408 error("Could not open importlib %s.\n", importlib->name);
410 tlb_read(fd, &magic, sizeof(magic));
412 switch(magic) {
413 case MSFT_MAGIC:
414 read_msft_importlib(importlib, fd);
415 break;
416 default:
417 error("Wrong or unsupported typelib magic %x\n", magic);
420 close(fd);
423 void add_importlib(const char *name)
425 importlib_t *importlib;
427 if(!typelib) return;
429 for(importlib = typelib->importlibs; importlib; importlib = NEXT_LINK(importlib)) {
430 if(!strcmp(name, importlib->name))
431 return;
434 chat("add_importlib: %s\n", name);
436 importlib = xmalloc(sizeof(*importlib));
437 memset( importlib, 0, sizeof(*importlib) );
438 importlib->name = xstrdup(name);
440 read_importlib(importlib);
442 LINK(importlib, typelib->importlibs);
443 typelib->importlibs = importlib;