[MKHIVE] Remove key name in our custom registry tree; use cell index instead
[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 <typedefs.h>
39 #include "widl.h"
40 #include "utils.h"
41 #include "parser.h"
42 #include "header.h"
43 #include "typelib.h"
44 #include "widltypes.h"
45 #include "typelib_struct.h"
46 #include "typetree.h"
47
48 static typelib_t *typelib;
49
50 /* List of oleauto types that should be recognized by name.
51 * (most of) these seem to be intrinsic types in mktyplib.
52 * This table MUST be alphabetically sorted on the kw field.
53 */
54 static const struct oatype {
55 const char *kw;
56 unsigned short vt;
57 } oatypes[] = {
58 {"BSTR", VT_BSTR},
59 {"CURRENCY", VT_CY},
60 {"DATE", VT_DATE},
61 {"DECIMAL", VT_DECIMAL},
62 {"HRESULT", VT_HRESULT},
63 {"LPSTR", VT_LPSTR},
64 {"LPWSTR", VT_LPWSTR},
65 {"SCODE", VT_ERROR},
66 {"VARIANT", VT_VARIANT},
67 {"VARIANT_BOOL", VT_BOOL}
68 };
69 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
70 #define KWP(p) ((const struct oatype *)(p))
71
72 static int kw_cmp_func(const void *s1, const void *s2)
73 {
74 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
75 }
76
77 static unsigned short builtin_vt(const type_t *t)
78 {
79 const char *kw = t->name;
80 struct oatype key;
81 const struct oatype *kwp;
82 key.kw = kw;
83 #ifdef KW_BSEARCH
84 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
85 #else
86 {
87 unsigned int i;
88 for (kwp=NULL, i=0; i < NTYPES; i++)
89 if (!kw_cmp_func(&key, &oatypes[i])) {
90 kwp = &oatypes[i];
91 break;
92 }
93 }
94 #endif
95 if (kwp) {
96 return kwp->vt;
97 }
98 if (is_string_type (t->attrs, t))
99 {
100 const type_t *elem_type;
101 if (is_array(t))
102 elem_type = type_array_get_element(t);
103 else
104 elem_type = type_pointer_get_ref(t);
105 if (type_get_type(elem_type) == TYPE_BASIC)
106 {
107 switch (type_basic_get_type(elem_type))
108 {
109 case TYPE_BASIC_CHAR: return VT_LPSTR;
110 case TYPE_BASIC_WCHAR: return VT_LPWSTR;
111 default: break;
112 }
113 }
114 }
115 return 0;
116 }
117
118 static int match(const char*n, const char*m)
119 {
120 if (!n) return 0;
121 return !strcmp(n, m);
122 }
123
124 unsigned short get_type_vt(type_t *t)
125 {
126 unsigned short vt;
127
128 chat("get_type_vt: %p type->name %s\n", t, t->name);
129 if (t->name) {
130 vt = builtin_vt(t);
131 if (vt) return vt;
132 }
133
134 if (type_is_alias(t) && is_attr(t->attrs, ATTR_PUBLIC))
135 return VT_USERDEFINED;
136
137 switch (type_get_type(t)) {
138 case TYPE_BASIC:
139 switch (type_basic_get_type(t)) {
140 case TYPE_BASIC_BYTE:
141 return VT_UI1;
142 case TYPE_BASIC_CHAR:
143 case TYPE_BASIC_INT8:
144 if (type_basic_get_sign(t) > 0)
145 return VT_UI1;
146 else
147 return VT_I1;
148 case TYPE_BASIC_WCHAR:
149 return VT_I2; /* mktyplib seems to parse wchar_t as short */
150 case TYPE_BASIC_INT16:
151 if (type_basic_get_sign(t) > 0)
152 return VT_UI2;
153 else
154 return VT_I2;
155 case TYPE_BASIC_INT:
156 if (type_basic_get_sign(t) > 0)
157 return VT_UINT;
158 else
159 return VT_INT;
160 case TYPE_BASIC_INT32:
161 case TYPE_BASIC_ERROR_STATUS_T:
162 if (type_basic_get_sign(t) > 0)
163 return VT_UI4;
164 else
165 return VT_I4;
166 case TYPE_BASIC_INT64:
167 case TYPE_BASIC_HYPER:
168 if (type_basic_get_sign(t) > 0)
169 return VT_UI8;
170 else
171 return VT_I8;
172 case TYPE_BASIC_INT3264:
173 if (typelib_kind == SYS_WIN64)
174 {
175 if (type_basic_get_sign(t) > 0)
176 return VT_UI8;
177 else
178 return VT_I8;
179 }
180 else
181 {
182 if (type_basic_get_sign(t) > 0)
183 return VT_UI4;
184 else
185 return VT_I4;
186 }
187 case TYPE_BASIC_FLOAT:
188 return VT_R4;
189 case TYPE_BASIC_DOUBLE:
190 return VT_R8;
191 case TYPE_BASIC_HANDLE:
192 error("handles can't be used in typelibs\n");
193 }
194 break;
195
196 case TYPE_POINTER:
197 return VT_PTR;
198
199 case TYPE_ARRAY:
200 if (type_array_is_decl_as_ptr(t))
201 {
202 if (match(type_array_get_element(t)->name, "SAFEARRAY"))
203 return VT_SAFEARRAY;
204 }
205 else
206 error("get_type_vt: array types not supported\n");
207 return VT_PTR;
208
209 case TYPE_INTERFACE:
210 if(match(t->name, "IUnknown"))
211 return VT_UNKNOWN;
212 if(match(t->name, "IDispatch"))
213 return VT_DISPATCH;
214 return VT_USERDEFINED;
215
216 case TYPE_ENUM:
217 case TYPE_STRUCT:
218 case TYPE_COCLASS:
219 case TYPE_MODULE:
220 case TYPE_UNION:
221 case TYPE_ENCAPSULATED_UNION:
222 return VT_USERDEFINED;
223
224 case TYPE_VOID:
225 return VT_VOID;
226
227 case TYPE_ALIAS:
228 /* aliases should be filtered out by the type_get_type call above */
229 assert(0);
230 break;
231
232 case TYPE_FUNCTION:
233 error("get_type_vt: functions not supported\n");
234 break;
235
236 case TYPE_BITFIELD:
237 error("get_type_vt: bitfields not supported\n");
238 break;
239 }
240 return 0;
241 }
242
243 void start_typelib(typelib_t *typelib_type)
244 {
245 if (!do_typelib) return;
246 typelib = typelib_type;
247 }
248
249 void end_typelib(void)
250 {
251 if (!typelib) return;
252
253 create_msft_typelib(typelib);
254 }
255
256 static void tlb_read(int fd, void *buf, int count)
257 {
258 if(read(fd, buf, count) < count)
259 error("error while reading importlib.\n");
260 }
261
262 static void tlb_lseek(int fd, off_t offset)
263 {
264 if(lseek(fd, offset, SEEK_SET) == -1)
265 error("lseek failed\n");
266 }
267
268 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
269 {
270 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
271 tlb_read(fd, guid, sizeof(GUID));
272 }
273
274 static void read_msft_importlib(importlib_t *importlib, int fd)
275 {
276 MSFT_Header header;
277 MSFT_SegDir segdir;
278 int *typeinfo_offs;
279 int i;
280
281 importlib->allocated = 0;
282
283 tlb_lseek(fd, 0);
284 tlb_read(fd, &header, sizeof(header));
285
286 importlib->version = header.version;
287
288 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
289 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
290 tlb_read(fd, &segdir, sizeof(segdir));
291
292 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
293
294 importlib->ntypeinfos = header.nrtypeinfos;
295 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
296
297 for(i=0; i < importlib->ntypeinfos; i++) {
298 MSFT_TypeInfoBase base;
299 MSFT_NameIntro nameintro;
300 int len;
301
302 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
303 + typeinfo_offs[i]);
304 tlb_read(fd, &base, sizeof(base));
305
306 importlib->importinfos[i].importlib = importlib;
307 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
308 importlib->importinfos[i].offset = -1;
309 importlib->importinfos[i].id = i;
310
311 if(base.posguid != -1) {
312 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
313 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
314 }
315 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
316
317 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
318 tlb_read(fd, &nameintro, sizeof(nameintro));
319
320 len = nameintro.namelen & 0xff;
321
322 importlib->importinfos[i].name = xmalloc(len+1);
323 tlb_read(fd, importlib->importinfos[i].name, len);
324 importlib->importinfos[i].name[len] = 0;
325 }
326
327 free(typeinfo_offs);
328 }
329
330 static void read_importlib(importlib_t *importlib)
331 {
332 int fd;
333 INT magic;
334 char *file_name;
335
336 file_name = wpp_find_include(importlib->name, NULL);
337 if(file_name) {
338 fd = open(file_name, O_RDONLY | O_BINARY );
339 free(file_name);
340 }else {
341 fd = open(importlib->name, O_RDONLY | O_BINARY );
342 }
343
344 if(fd < 0)
345 error("Could not open importlib %s.\n", importlib->name);
346
347 tlb_read(fd, &magic, sizeof(magic));
348
349 switch(magic) {
350 case MSFT_MAGIC:
351 read_msft_importlib(importlib, fd);
352 break;
353 default:
354 error("Wrong or unsupported typelib magic %x\n", magic);
355 };
356
357 close(fd);
358 }
359
360 void add_importlib(const char *name)
361 {
362 importlib_t *importlib;
363
364 if(!typelib) return;
365
366 LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
367 if(!strcmp(name, importlib->name))
368 return;
369
370 chat("add_importlib: %s\n", name);
371
372 importlib = xmalloc(sizeof(*importlib));
373 memset( importlib, 0, sizeof(*importlib) );
374 importlib->name = xstrdup(name);
375
376 read_importlib(importlib);
377 list_add_head( &typelib->importlibs, &importlib->entry );
378 }