widl: Use type_get_type to determine the types of types during typelib generation.
[wine/hacks.git] / tools / widl / typelib.c
blobd6a2e2eb4a1b4a52227a90edea4abb71571d4d96
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 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <ctype.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"
48 #include "typetree.h"
50 static typelib_t *typelib;
52 int is_ptr(const type_t *t)
54 return type_get_type(t) == TYPE_POINTER;
57 int is_array(const type_t *t)
59 return type_get_type(t) == TYPE_ARRAY;
62 /* List of oleauto types that should be recognized by name.
63 * (most of) these seem to be intrinsic types in mktyplib.
64 * This table MUST be alphabetically sorted on the kw field.
66 static const struct oatype {
67 const char *kw;
68 unsigned short vt;
69 } oatypes[] = {
70 {"BSTR", VT_BSTR},
71 {"CURRENCY", VT_CY},
72 {"DATE", VT_DATE},
73 {"DECIMAL", VT_DECIMAL},
74 {"HRESULT", VT_HRESULT},
75 {"LPSTR", VT_LPSTR},
76 {"LPWSTR", VT_LPWSTR},
77 {"SCODE", VT_ERROR},
78 {"VARIANT", VT_VARIANT},
79 {"VARIANT_BOOL", VT_BOOL}
81 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
82 #define KWP(p) ((const struct oatype *)(p))
84 static int kw_cmp_func(const void *s1, const void *s2)
86 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
89 static unsigned short builtin_vt(const type_t *t)
91 const char *kw = t->name;
92 struct oatype key;
93 const struct oatype *kwp;
94 key.kw = kw;
95 #ifdef KW_BSEARCH
96 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
97 #else
99 unsigned int i;
100 for (kwp=NULL, i=0; i < NTYPES; i++)
101 if (!kw_cmp_func(&key, &oatypes[i])) {
102 kwp = &oatypes[i];
103 break;
106 #endif
107 if (kwp) {
108 return kwp->vt;
110 if (is_string_type (t->attrs, t))
112 const type_t *elem_type;
113 if (is_array(t))
114 elem_type = type_array_get_element(t);
115 else
116 elem_type = type_pointer_get_ref(t);
117 if (type_get_type(elem_type) == TYPE_BASIC)
119 switch (type_basic_get_fc(elem_type))
121 case RPC_FC_CHAR: return VT_LPSTR;
122 case RPC_FC_WCHAR: return VT_LPWSTR;
123 default: break;
127 return 0;
130 static int match(const char*n, const char*m)
132 if (!n) return 0;
133 return !strcmp(n, m);
136 unsigned short get_type_vt(type_t *t)
138 unsigned short vt;
140 chat("get_type_vt: %p type->name %s\n", t, t->name);
141 if (t->name) {
142 vt = builtin_vt(t);
143 if (vt) return vt;
146 if (type_is_alias(t) && is_attr(t->attrs, ATTR_PUBLIC))
147 return VT_USERDEFINED;
149 switch (type_get_type(t)) {
150 case TYPE_BASIC:
151 switch (type_basic_get_fc(t)) {
152 case RPC_FC_BYTE:
153 case RPC_FC_USMALL:
154 return VT_UI1;
155 case RPC_FC_CHAR:
156 case RPC_FC_SMALL:
157 return VT_I1;
158 case RPC_FC_WCHAR:
159 return VT_I2; /* mktyplib seems to parse wchar_t as short */
160 case RPC_FC_SHORT:
161 return VT_I2;
162 case RPC_FC_USHORT:
163 return VT_UI2;
164 case RPC_FC_LONG:
165 if (match(t->name, "int")) return VT_INT;
166 return VT_I4;
167 case RPC_FC_ULONG:
168 if (match(t->name, "int")) return VT_UINT;
169 return VT_UI4;
170 case RPC_FC_HYPER:
171 if (t->sign < 0) return VT_UI8;
172 if (match(t->name, "MIDL_uhyper")) return VT_UI8;
173 return VT_I8;
174 case RPC_FC_FLOAT:
175 return VT_R4;
176 case RPC_FC_DOUBLE:
177 return VT_R8;
178 default:
179 error("get_type_vt: unknown basic type: 0x%02x\n", type_basic_get_fc(t));
181 break;
183 case TYPE_POINTER:
184 if (match(type_pointer_get_ref(t)->name, "SAFEARRAY"))
185 return VT_SAFEARRAY;
186 return VT_PTR;
188 case TYPE_ARRAY:
189 if (t->declarray)
190 error("get_type_vt: array types not supported\n");
191 return VT_PTR;
193 case TYPE_INTERFACE:
194 if(match(t->name, "IUnknown"))
195 return VT_UNKNOWN;
196 if(match(t->name, "IDispatch"))
197 return VT_DISPATCH;
198 return VT_USERDEFINED;
200 case TYPE_ENUM:
201 case TYPE_STRUCT:
202 case TYPE_COCLASS:
203 case TYPE_MODULE:
204 case TYPE_UNION:
205 case TYPE_ENCAPSULATED_UNION:
206 return VT_USERDEFINED;
208 case TYPE_VOID:
209 return VT_VOID;
211 case TYPE_ALIAS:
212 /* aliases should be filtered out by the type_get_type call above */
213 assert(0);
214 break;
216 case TYPE_FUNCTION:
217 error("get_type_vt: functions not supported\n");
218 break;
220 return 0;
223 void start_typelib(typelib_t *typelib_type)
225 if (!do_typelib) return;
227 typelib = typelib_type;
228 typelib->filename = xstrdup(typelib_name);
231 void end_typelib(void)
233 if (!typelib) return;
235 create_msft_typelib(typelib);
238 static void tlb_read(int fd, void *buf, int count)
240 if(read(fd, buf, count) < count)
241 error("error while reading importlib.\n");
244 static void tlb_lseek(int fd, off_t offset)
246 if(lseek(fd, offset, SEEK_SET) == -1)
247 error("lseek failed\n");
250 static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
252 tlb_lseek(fd, segdir->pGuidTab.offset+offset);
253 tlb_read(fd, guid, sizeof(GUID));
256 static void read_msft_importlib(importlib_t *importlib, int fd)
258 MSFT_Header header;
259 MSFT_SegDir segdir;
260 int *typeinfo_offs;
261 int i;
263 importlib->allocated = 0;
265 tlb_lseek(fd, 0);
266 tlb_read(fd, &header, sizeof(header));
268 importlib->version = header.version;
270 typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
271 tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
272 tlb_read(fd, &segdir, sizeof(segdir));
274 msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
276 importlib->ntypeinfos = header.nrtypeinfos;
277 importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
279 for(i=0; i < importlib->ntypeinfos; i++) {
280 MSFT_TypeInfoBase base;
281 MSFT_NameIntro nameintro;
282 int len;
284 tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
285 + typeinfo_offs[i]);
286 tlb_read(fd, &base, sizeof(base));
288 importlib->importinfos[i].importlib = importlib;
289 importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
290 importlib->importinfos[i].offset = -1;
291 importlib->importinfos[i].id = i;
293 if(base.posguid != -1) {
294 importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
295 msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
297 else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
299 tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
300 tlb_read(fd, &nameintro, sizeof(nameintro));
302 len = nameintro.namelen & 0xff;
304 importlib->importinfos[i].name = xmalloc(len+1);
305 tlb_read(fd, importlib->importinfos[i].name, len);
306 importlib->importinfos[i].name[len] = 0;
309 free(typeinfo_offs);
312 static void read_importlib(importlib_t *importlib)
314 int fd;
315 INT magic;
316 char *file_name;
318 file_name = wpp_find_include(importlib->name, NULL);
319 if(file_name) {
320 fd = open(file_name, O_RDONLY | O_BINARY );
321 free(file_name);
322 }else {
323 fd = open(importlib->name, O_RDONLY | O_BINARY );
326 if(fd < 0)
327 error("Could not open importlib %s.\n", importlib->name);
329 tlb_read(fd, &magic, sizeof(magic));
331 switch(magic) {
332 case MSFT_MAGIC:
333 read_msft_importlib(importlib, fd);
334 break;
335 default:
336 error("Wrong or unsupported typelib magic %x\n", magic);
339 close(fd);
342 void add_importlib(const char *name)
344 importlib_t *importlib;
346 if(!typelib) return;
348 LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
349 if(!strcmp(name, importlib->name))
350 return;
352 chat("add_importlib: %s\n", name);
354 importlib = xmalloc(sizeof(*importlib));
355 memset( importlib, 0, sizeof(*importlib) );
356 importlib->name = xstrdup(name);
358 read_importlib(importlib);
359 list_add_head( &typelib->importlibs, &importlib->entry );