Fixed potential crash in fd_dump function.
[wine/multimedia.git] / tools / widl / typelib.c
blobaf3d687b06b9a32005e2cfd37c4523d0d28e0938
1 /*
2 * IDL Compiler
4 * Copyright 2004 Ove Kaaven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #include <signal.h>
31 #include "widl.h"
32 #include "utils.h"
33 #include "parser.h"
34 #include "header.h"
35 #include "typelib.h"
37 int in_typelib = 0;
39 static typelib_t *typelib;
41 /* List of oleauto types that should be recognized by name.
42 * (most of) these seem to be intrinsic types in mktyplib. */
44 static struct oatype {
45 const char *kw;
46 unsigned short vt;
47 } oatypes[] = {
48 {"BSTR", VT_BSTR},
49 {"CURRENCY", VT_CY},
50 {"DATE", VT_DATE},
51 {"DECIMAL", VT_DECIMAL},
52 {"HRESULT", VT_HRESULT},
53 {"LPSTR", VT_LPSTR},
54 {"LPWSTR", VT_LPWSTR},
55 {"SCODE", VT_ERROR},
56 {"VARIANT", VT_VARIANT}
58 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
59 #define KWP(p) ((const struct oatype *)(p))
61 static int kw_cmp_func(const void *s1, const void *s2)
63 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
66 static unsigned short builtin_vt(const char *kw)
68 struct oatype key, *kwp;
69 key.kw = kw;
70 #ifdef KW_BSEARCH
71 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
72 #else
74 int i;
75 for (kwp=NULL, i=0; i < NTYPES; i++)
76 if (!kw_cmp_func(&key, &oatypes[i])) {
77 kwp = &oatypes[i];
78 break;
81 #endif
82 if (kwp) {
83 return kwp->vt;
85 return 0;
88 static int match(const char*n, const char*m)
90 if (!n) return 0;
91 return !strcmp(n, m);
94 unsigned short get_type_vt(type_t *t)
96 unsigned short vt;
98 chat("get_type_vt: %p type->name %s\n", t, t->name);
99 if (t->name) {
100 vt = builtin_vt(t->name);
101 if (vt) return vt;
104 switch (t->type) {
105 case RPC_FC_BYTE:
106 case RPC_FC_USMALL:
107 return VT_UI1;
108 case RPC_FC_CHAR:
109 case RPC_FC_SMALL:
110 return VT_I1;
111 case RPC_FC_WCHAR:
112 return VT_I2; /* mktyplib seems to parse wchar_t as short */
113 case RPC_FC_SHORT:
114 return VT_I2;
115 case RPC_FC_USHORT:
116 return VT_UI2;
117 case RPC_FC_LONG:
118 if (t->ref && match(t->ref->name, "int")) return VT_INT;
119 return VT_I4;
120 case RPC_FC_ULONG:
121 if (t->ref && match(t->ref->name, "int")) return VT_UINT;
122 return VT_UI4;
123 case RPC_FC_HYPER:
124 if (t->sign < 0) return VT_UI8;
125 if (t->ref && match(t->ref->name, "MIDL_uhyper")) return VT_UI8;
126 return VT_I8;
127 case RPC_FC_FLOAT:
128 return VT_R4;
129 case RPC_FC_DOUBLE:
130 return VT_R8;
131 case RPC_FC_RP:
132 case RPC_FC_UP:
133 case RPC_FC_OP:
134 case RPC_FC_FP:
135 if(t->ref)
136 return VT_PTR;
138 error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
139 break;
140 case RPC_FC_IP:
141 if(match(t->name, "IUnknown"))
142 return VT_UNKNOWN;
143 if(match(t->name, "IDispatch"))
144 return VT_DISPATCH;
145 return VT_USERDEFINED;
147 case RPC_FC_STRUCT:
148 case RPC_FC_PSTRUCT:
149 return VT_USERDEFINED;
150 case 0:
151 if(t->attrs)
152 return VT_USERDEFINED;
153 return 0;
154 default:
155 error("get_type_vt: unknown-type: %d\n", t->type);
157 return 0;
160 unsigned short get_var_vt(var_t *v)
162 unsigned short vt;
164 chat("get_var_vt: %p tname %s\n", v, v->tname);
165 if (v->tname) {
166 vt = builtin_vt(v->tname);
167 if (vt) return vt;
170 return get_type_vt(v->type);
173 void start_typelib(char *name, attr_t *attrs)
175 in_typelib++;
176 if (!do_typelib) return;
178 typelib = xmalloc(sizeof(*typelib));
179 typelib->name = xstrdup(name);
180 typelib->filename = xstrdup(typelib_name);
181 typelib->attrs = attrs;
184 void end_typelib(void)
186 in_typelib--;
187 if (!typelib) return;
189 create_msft_typelib(typelib);
190 return;
193 void add_interface(type_t *iface)
195 typelib_entry_t *entry;
196 if (!typelib) return;
198 chat("add interface: %s\n", iface->name);
199 entry = xmalloc(sizeof(*entry));
200 entry->kind = TKIND_INTERFACE;
201 entry->u.interface = iface;
202 LINK(entry, typelib->entry);
203 typelib->entry = entry;
206 void add_coclass(class_t *cls)
208 ifref_t *lcur = cls->ifaces;
209 ifref_t *cur;
210 typelib_entry_t *entry;
212 if (lcur) {
213 while (NEXT_LINK(lcur)) lcur = NEXT_LINK(lcur);
216 if (!typelib) return;
218 /* install interfaces the coclass depends on */
219 cur = lcur;
220 while (cur) {
221 add_interface(cur->iface);
222 cur = PREV_LINK(cur);
224 entry = xmalloc(sizeof(*entry));
225 entry->kind = TKIND_COCLASS;
226 entry->u.class = cls;
227 LINK(entry, typelib->entry);
228 typelib->entry = entry;
231 void add_module(type_t *module)
233 typelib_entry_t *entry;
234 if (!typelib) return;
236 chat("add module: %s\n", module->name);
237 entry = xmalloc(sizeof(*entry));
238 entry->kind = TKIND_MODULE;
239 entry->u.module = module;
240 LINK(entry, typelib->entry);
241 typelib->entry = entry;
244 void add_struct(type_t *structure)
246 typelib_entry_t *entry;
247 if (!typelib) return;
249 chat("add struct: %s\n", structure->name);
250 entry = xmalloc(sizeof(*entry));
251 entry->kind = TKIND_RECORD;
252 entry->u.structure = structure;
253 LINK(entry, typelib->entry);
254 typelib->entry = entry;
257 void add_enum(type_t *enumeration)
259 typelib_entry_t *entry;
260 if (!typelib) return;
262 chat("add enum: %s\n", enumeration->name);
263 entry = xmalloc(sizeof(*entry));
264 entry->kind = TKIND_ENUM;
265 entry->u.enumeration = enumeration;
266 LINK(entry, typelib->entry);
267 typelib->entry = entry;
270 void add_typedef(type_t *tdef, var_t *name)
272 typelib_entry_t *entry;
273 if (!typelib) return;
275 entry = xmalloc(sizeof(*entry));
276 entry->kind = TKIND_ALIAS;
277 entry->u.tdef = xmalloc(sizeof(*entry->u.tdef));
278 memcpy(entry->u.tdef, name, sizeof(*name));
279 entry->u.tdef->type = tdef;
280 entry->u.tdef->name = xstrdup(name->name);
281 LINK(entry, typelib->entry);
282 typelib->entry = entry;