43d16ec2760be3df7e990b7a21ed4f30514e7f4d
[reactos.git] / reactos / tools / widl / typelib.c
1 /*
2 * IDL Compiler
3 *
4 * Copyright 2004 Ove Kaaven
5 * Copyright 2006 Jacek Caban for CodeWeavers
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24 #include "wine/wpp.h"
25
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>
34
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37
38 #include <host/typedefs.h>
39
40 #include "widl.h"
41 #include "utils.h"
42 #include "parser.h"
43 #include "header.h"
44 #include "typelib.h"
45 #include "widltypes.h"
46 #include "typelib_struct.h"
47
48 int in_typelib = 0;
49
50 static typelib_t *typelib;
51
52 type_t *duptype(type_t *t, int dupname)
53 {
54 type_t *d = alloc_type();
55
56 *d = *t;
57 if (dupname && t->name)
58 d->name = xstrdup(t->name);
59
60 d->orig = t;
61 return d;
62 }
63
64 type_t *alias(type_t *t, const char *name)
65 {
66 type_t *a = duptype(t, 0);
67
68 a->name = xstrdup(name);
69 a->kind = TKIND_ALIAS;
70 a->attrs = NULL;
71 a->declarray = FALSE;
72
73 return a;
74 }
75
76 int is_ptr(const type_t *t)
77 {
78 unsigned char c = t->type;
79 return c == RPC_FC_RP
80 || c == RPC_FC_UP
81 || c == RPC_FC_FP
82 || c == RPC_FC_OP;
83 }
84
85 int is_array(const type_t *t)
86 {
87 switch (t->type)
88 {
89 case RPC_FC_SMFARRAY:
90 case RPC_FC_LGFARRAY:
91 case RPC_FC_SMVARRAY:
92 case RPC_FC_LGVARRAY:
93 case RPC_FC_CARRAY:
94 case RPC_FC_CVARRAY:
95 case RPC_FC_BOGUS_ARRAY:
96 return TRUE;
97 default:
98 return FALSE;
99 }
100 }
101
102 /* List of oleauto types that should be recognized by name.
103 * (most of) these seem to be intrinsic types in mktyplib. */
104
105 static const struct oatype {
106 const char *kw;
107 unsigned short vt;
108 } oatypes[] = {
109 {"BSTR", VT_BSTR},
110 {"CURRENCY", VT_CY},
111 {"DATE", VT_DATE},
112 {"DECIMAL", VT_DECIMAL},
113 {"HRESULT", VT_HRESULT},
114 {"LPSTR", VT_LPSTR},
115 {"LPWSTR", VT_LPWSTR},
116 {"SCODE", VT_ERROR},
117 {"VARIANT", VT_VARIANT},
118 {"VARIANT_BOOL", VT_BOOL}
119 };
120 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
121 #define KWP(p) ((const struct oatype *)(p))
122
123 static int kw_cmp_func(const void *s1, const void *s2)
124 {
125 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
126 }
127
128 static unsigned short builtin_vt(const type_t *t)
129 {
130 const char *kw = t->name;
131 struct oatype key;
132 const struct oatype *kwp;
133 key.kw = kw;
134 #ifdef KW_BSEARCH
135 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
136 #else
137 {
138 unsigned int i;
139 for (kwp=NULL, i=0; i < NTYPES; i++)
140 if (!kw_cmp_func(&key, &oatypes[i])) {
141 kwp = &oatypes[i];
142 break;
143 }
144 }
145 #endif
146 if (kwp) {
147 return kwp->vt;
148 }
149 if (is_string_type (t->attrs, t))
150 switch (t->ref->type)
151 {
152 case RPC_FC_CHAR: return VT_LPSTR;
153 case RPC_FC_WCHAR: return VT_LPWSTR;
154 default: break;
155 }
156 return 0;
157 }
158
159 static int match(const char*n, const char*m)
160 {
161 if (!n) return 0;
162 return !strcmp(n, m);
163 }
164
165 unsigned short get_type_vt(type_t *t)
166 {
167 unsigned short vt;
168
169 chat("get_type_vt: %p type->name %s\n", t, t->name);
170 if (t->name) {
171 vt = builtin_vt(t);
172 if (vt) return vt;
173 }
174
175 if (t->kind == TKIND_ALIAS && t->attrs)
176 return VT_USERDEFINED;
177
178 switch (t->type) {
179 case RPC_FC_BYTE:
180 case RPC_FC_USMALL:
181 return VT_UI1;
182 case RPC_FC_CHAR:
183 case RPC_FC_SMALL:
184 return VT_I1;
185 case RPC_FC_WCHAR:
186 return VT_I2; /* mktyplib seems to parse wchar_t as short */
187 case RPC_FC_SHORT:
188 return VT_I2;
189 case RPC_FC_USHORT:
190 return VT_UI2;
191 case RPC_FC_LONG:
192 if (match(t->name, "int")) return VT_INT;
193 return VT_I4;
194 case RPC_FC_ULONG:
195 if (match(t->name, "int")) return VT_UINT;
196 return VT_UI4;
197 case RPC_FC_HYPER:
198 if (t->sign < 0) return VT_UI8;
199 if (match(t->name, "MIDL_uhyper")) return VT_UI8;
200 return VT_I8;
201 case RPC_FC_FLOAT:
202 return VT_R4;
203 case RPC_FC_DOUBLE:
204 return VT_R8;
205 case RPC_FC_RP:
206 case RPC_FC_UP:
207 case RPC_FC_OP:
208 case RPC_FC_FP:
209 case RPC_FC_CARRAY:
210 case RPC_FC_CVARRAY:
211 if(t->ref)
212 {
213 if (match(t->ref->name, "SAFEARRAY"))
214 return VT_SAFEARRAY;
215 return VT_PTR;
216 }
217
218 error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
219 break;
220 case RPC_FC_IP:
221 if(match(t->name, "IUnknown"))
222 return VT_UNKNOWN;
223 if(match(t->name, "IDispatch"))
224 return VT_DISPATCH;
225 return VT_USERDEFINED;
226
227 case RPC_FC_ENUM16:
228 case RPC_FC_STRUCT:
229 case RPC_FC_PSTRUCT:
230 case RPC_FC_CSTRUCT:
231 case RPC_FC_CPSTRUCT:
232 case RPC_FC_CVSTRUCT:
233 case RPC_FC_BOGUS_STRUCT:
234 return VT_USERDEFINED;
235 case 0:
236 return t->kind == TKIND_PRIMITIVE ? VT_VOID : VT_USERDEFINED;
237 default:
238 error("get_type_vt: unknown type: 0x%02x\n", t->type);
239 }
240 return 0;
241 }
242
243 void start_typelib(char *name, attr_list_t *attrs)
244 {
245 in_typelib++;
246 if (!do_typelib) return;
247
248 typelib = xmalloc(sizeof(*typelib));
249 typelib->name = xstrdup(name);
250 typelib->filename = xstrdup(typelib_name);
251 typelib->attrs = attrs;
252 list_init( &typelib->entries );
253 list_init( &typelib->importlibs );
254
255 if (is_attr(attrs, ATTR_POINTERDEFAULT))
256 pointer_default = get_attrv(attrs, ATTR_POINTERDEFAULT);
257 }
258
259 void end_typelib(void)
260 {
261 in_typelib--;
262 if (!typelib) return;
263
264 create_msft_typelib(typelib);
265 pointer_default = RPC_FC_UP;
266 return;
267 }
268
269 void add_typelib_entry(type_t *t)
270 {
271 typelib_entry_t *entry;
272 if (!typelib) return;
273
274 chat("add kind %i: %s\n", t->kind, t->name);
275 entry = xmalloc(sizeof(*entry));
276 entry->type = t;
277 list_add_tail( &typelib->entries, &entry->entry );
278 }
279
280 static void tlb_read(int fd, void *buf, int count)
281 {
282 if(read(fd, buf, count) < count)
283 error("error while reading importlib.\n");
284 }
285
286 static void tlb_lseek(int fd, off_t offset)
287 {
288 if(lseek(fd, offset, SEEK_SET) == -1)
289 error("lseek failed\n");
290 }
291
292 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
293 {
294 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
295 tlb_read(fd, guid, sizeof(GUID));
296 }
297
298 static void read_msft_importlib(importlib_t *importlib, int fd)
299 {
300 MSFT_Header header;
301 MSFT_SegDir segdir;
302 int *typeinfo_offs;
303 int i;
304
305 importlib->allocated = 0;
306
307 tlb_lseek(fd, 0);
308 tlb_read(fd, &header, sizeof(header));
309
310 importlib->version = header.version;
311
312 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
313 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
314 tlb_read(fd, &segdir, sizeof(segdir));
315
316 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
317
318 importlib->ntypeinfos = header.nrtypeinfos;
319 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
320
321 for(i=0; i < importlib->ntypeinfos; i++) {
322 MSFT_TypeInfoBase base;
323 MSFT_NameIntro nameintro;
324 int len;
325
326 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
327 + typeinfo_offs[i]);
328 tlb_read(fd, &base, sizeof(base));
329
330 importlib->importinfos[i].importlib = importlib;
331 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
332 importlib->importinfos[i].offset = -1;
333 importlib->importinfos[i].id = i;
334
335 if(base.posguid != -1) {
336 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
337 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
338 }
339 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
340
341 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
342 tlb_read(fd, &nameintro, sizeof(nameintro));
343
344 len = nameintro.namelen & 0xff;
345
346 importlib->importinfos[i].name = xmalloc(len+1);
347 tlb_read(fd, importlib->importinfos[i].name, len);
348 importlib->importinfos[i].name[len] = 0;
349 }
350
351 free(typeinfo_offs);
352 }
353
354 static void read_importlib(importlib_t *importlib)
355 {
356 int fd;
357 INT magic;
358 char *file_name;
359
360 file_name = wpp_find_include(importlib->name, NULL);
361 if(file_name) {
362 fd = open(file_name, O_RDONLY | O_BINARY);
363 free(file_name);
364 }else {
365 fd = open(importlib->name, O_RDONLY | O_BINARY);
366 }
367
368 if(fd < 0)
369 error("Could not open importlib %s.\n", importlib->name);
370
371 tlb_read(fd, &magic, sizeof(magic));
372
373 switch(magic) {
374 case MSFT_MAGIC:
375 read_msft_importlib(importlib, fd);
376 break;
377 default:
378 error("Wrong or unsupported typelib magic %x\n", magic);
379 };
380
381 close(fd);
382 }
383
384 void add_importlib(const char *name)
385 {
386 importlib_t *importlib;
387
388 if(!typelib) return;
389
390 LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
391 if(!strcmp(name, importlib->name))
392 return;
393
394 chat("add_importlib: %s\n", name);
395
396 importlib = xmalloc(sizeof(*importlib));
397 memset( importlib, 0, sizeof(*importlib) );
398 importlib->name = xstrdup(name);
399
400 read_importlib(importlib);
401 list_add_head( &typelib->importlibs, &importlib->entry );
402 }