b6464354e68d786f0f11514c8f723e2802370d9e
[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 * This table MUST be alphabetically sorted on the kw field.
105 */
106 static const 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}
120 };
121 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
122 #define KWP(p) ((const struct oatype *)(p))
123
124 static int kw_cmp_func(const void *s1, const void *s2)
125 {
126 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
127 }
128
129 static unsigned short builtin_vt(const type_t *t)
130 {
131 const char *kw = t->name;
132 struct oatype key;
133 const struct oatype *kwp;
134 key.kw = kw;
135 #ifdef KW_BSEARCH
136 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
137 #else
138 {
139 unsigned int i;
140 for (kwp=NULL, i=0; i < NTYPES; i++)
141 if (!kw_cmp_func(&key, &oatypes[i])) {
142 kwp = &oatypes[i];
143 break;
144 }
145 }
146 #endif
147 if (kwp) {
148 return kwp->vt;
149 }
150 if (is_string_type (t->attrs, t))
151 switch (t->ref->type)
152 {
153 case RPC_FC_CHAR: return VT_LPSTR;
154 case RPC_FC_WCHAR: return VT_LPWSTR;
155 default: break;
156 }
157 return 0;
158 }
159
160 static int match(const char*n, const char*m)
161 {
162 if (!n) return 0;
163 return !strcmp(n, m);
164 }
165
166 unsigned short get_type_vt(type_t *t)
167 {
168 unsigned short vt;
169
170 chat("get_type_vt: %p type->name %s\n", t, t->name);
171 if (t->name) {
172 vt = builtin_vt(t);
173 if (vt) return vt;
174 }
175
176 if (t->kind == TKIND_ALIAS && is_attr(t->attrs, ATTR_PUBLIC))
177 return VT_USERDEFINED;
178
179 switch (t->type) {
180 case RPC_FC_BYTE:
181 case RPC_FC_USMALL:
182 return VT_UI1;
183 case RPC_FC_CHAR:
184 case RPC_FC_SMALL:
185 return VT_I1;
186 case RPC_FC_WCHAR:
187 return VT_I2; /* mktyplib seems to parse wchar_t as short */
188 case RPC_FC_SHORT:
189 return VT_I2;
190 case RPC_FC_USHORT:
191 return VT_UI2;
192 case RPC_FC_LONG:
193 if (match(t->name, "int")) return VT_INT;
194 return VT_I4;
195 case RPC_FC_ULONG:
196 if (match(t->name, "int")) return VT_UINT;
197 return VT_UI4;
198 case RPC_FC_HYPER:
199 if (t->sign < 0) return VT_UI8;
200 if (match(t->name, "MIDL_uhyper")) return VT_UI8;
201 return VT_I8;
202 case RPC_FC_FLOAT:
203 return VT_R4;
204 case RPC_FC_DOUBLE:
205 return VT_R8;
206 case RPC_FC_RP:
207 case RPC_FC_UP:
208 case RPC_FC_OP:
209 case RPC_FC_FP:
210 case RPC_FC_CARRAY:
211 case RPC_FC_CVARRAY:
212 if(t->ref)
213 {
214 if (match(t->ref->name, "SAFEARRAY"))
215 return VT_SAFEARRAY;
216 return VT_PTR;
217 }
218
219 error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
220 break;
221 case RPC_FC_IP:
222 if(match(t->name, "IUnknown"))
223 return VT_UNKNOWN;
224 if(match(t->name, "IDispatch"))
225 return VT_DISPATCH;
226 return VT_USERDEFINED;
227
228 case RPC_FC_ENUM16:
229 case RPC_FC_STRUCT:
230 case RPC_FC_PSTRUCT:
231 case RPC_FC_CSTRUCT:
232 case RPC_FC_CPSTRUCT:
233 case RPC_FC_CVSTRUCT:
234 case RPC_FC_BOGUS_STRUCT:
235 case RPC_FC_COCLASS:
236 return VT_USERDEFINED;
237 case 0:
238 return t->kind == TKIND_PRIMITIVE ? VT_VOID : VT_USERDEFINED;
239 default:
240 error("get_type_vt: unknown type: 0x%02x\n", t->type);
241 }
242 return 0;
243 }
244
245 void start_typelib(typelib_t *typelib_type)
246 {
247 in_typelib++;
248 if (!do_typelib) return;
249
250 typelib = typelib_type;
251 typelib->filename = xstrdup(typelib_name);
252 }
253
254 void end_typelib(void)
255 {
256 in_typelib--;
257 if (!typelib) return;
258
259 create_msft_typelib(typelib);
260 }
261
262 void add_typelib_entry(type_t *t)
263 {
264 typelib_entry_t *entry;
265 if (!typelib) return;
266
267 chat("add kind %i: %s\n", t->kind, t->name);
268 entry = xmalloc(sizeof(*entry));
269 entry->type = t;
270 list_add_tail( &typelib->entries, &entry->entry );
271 }
272
273 static void tlb_read(int fd, void *buf, int count)
274 {
275 if(read(fd, buf, count) < count)
276 error("error while reading importlib.\n");
277 }
278
279 static void tlb_lseek(int fd, off_t offset)
280 {
281 if(lseek(fd, offset, SEEK_SET) == -1)
282 error("lseek failed\n");
283 }
284
285 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
286 {
287 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
288 tlb_read(fd, guid, sizeof(GUID));
289 }
290
291 static void read_msft_importlib(importlib_t *importlib, int fd)
292 {
293 MSFT_Header header;
294 MSFT_SegDir segdir;
295 int *typeinfo_offs;
296 int i;
297
298 importlib->allocated = 0;
299
300 tlb_lseek(fd, 0);
301 tlb_read(fd, &header, sizeof(header));
302
303 importlib->version = header.version;
304
305 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
306 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
307 tlb_read(fd, &segdir, sizeof(segdir));
308
309 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
310
311 importlib->ntypeinfos = header.nrtypeinfos;
312 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
313
314 for(i=0; i < importlib->ntypeinfos; i++) {
315 MSFT_TypeInfoBase base;
316 MSFT_NameIntro nameintro;
317 int len;
318
319 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
320 + typeinfo_offs[i]);
321 tlb_read(fd, &base, sizeof(base));
322
323 importlib->importinfos[i].importlib = importlib;
324 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
325 importlib->importinfos[i].offset = -1;
326 importlib->importinfos[i].id = i;
327
328 if(base.posguid != -1) {
329 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
330 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
331 }
332 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
333
334 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
335 tlb_read(fd, &nameintro, sizeof(nameintro));
336
337 len = nameintro.namelen & 0xff;
338
339 importlib->importinfos[i].name = xmalloc(len+1);
340 tlb_read(fd, importlib->importinfos[i].name, len);
341 importlib->importinfos[i].name[len] = 0;
342 }
343
344 free(typeinfo_offs);
345 }
346
347 static void read_importlib(importlib_t *importlib)
348 {
349 int fd;
350 INT magic;
351 char *file_name;
352
353 file_name = wpp_find_include(importlib->name, NULL);
354 if(file_name) {
355 fd = open(file_name, O_RDONLY | O_BINARY);
356 free(file_name);
357 }else {
358 fd = open(importlib->name, O_RDONLY | O_BINARY);
359 }
360
361 if(fd < 0)
362 error("Could not open importlib %s.\n", importlib->name);
363
364 tlb_read(fd, &magic, sizeof(magic));
365
366 switch(magic) {
367 case MSFT_MAGIC:
368 read_msft_importlib(importlib, fd);
369 break;
370 default:
371 error("Wrong or unsupported typelib magic %x\n", magic);
372 };
373
374 close(fd);
375 }
376
377 void add_importlib(const char *name)
378 {
379 importlib_t *importlib;
380
381 if(!typelib) return;
382
383 LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
384 if(!strcmp(name, importlib->name))
385 return;
386
387 chat("add_importlib: %s\n", name);
388
389 importlib = xmalloc(sizeof(*importlib));
390 memset( importlib, 0, sizeof(*importlib) );
391 importlib->name = xstrdup(name);
392
393 read_importlib(importlib);
394 list_add_head( &typelib->importlibs, &importlib->entry );
395 }