Store interfaces, structs, coclasses and modules that are to be
[wine/wine64.git] / tools / widl / typelib.c
blobe9a28cefb5641ae69934af274b8460ea3263e990
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 /* Copied from wtypes.h. Not included directly because that would create a
40 * circular dependency (after all, wtypes.h is generated by widl...) */
42 enum VARENUM {
43 VT_EMPTY = 0,
44 VT_NULL = 1,
45 VT_I2 = 2,
46 VT_I4 = 3,
47 VT_R4 = 4,
48 VT_R8 = 5,
49 VT_CY = 6,
50 VT_DATE = 7,
51 VT_BSTR = 8,
52 VT_DISPATCH = 9,
53 VT_ERROR = 10,
54 VT_BOOL = 11,
55 VT_VARIANT = 12,
56 VT_UNKNOWN = 13,
57 VT_DECIMAL = 14,
58 VT_I1 = 16,
59 VT_UI1 = 17,
60 VT_UI2 = 18,
61 VT_UI4 = 19,
62 VT_I8 = 20,
63 VT_UI8 = 21,
64 VT_INT = 22,
65 VT_UINT = 23,
66 VT_VOID = 24,
67 VT_HRESULT = 25,
68 VT_PTR = 26,
69 VT_SAFEARRAY = 27,
70 VT_CARRAY = 28,
71 VT_USERDEFINED = 29,
72 VT_LPSTR = 30,
73 VT_LPWSTR = 31,
74 VT_RECORD = 36,
75 VT_FILETIME = 64,
76 VT_BLOB = 65,
77 VT_STREAM = 66,
78 VT_STORAGE = 67,
79 VT_STREAMED_OBJECT = 68,
80 VT_STORED_OBJECT = 69,
81 VT_BLOB_OBJECT = 70,
82 VT_CF = 71,
83 VT_CLSID = 72,
84 VT_BSTR_BLOB = 0xfff,
85 VT_VECTOR = 0x1000,
86 VT_ARRAY = 0x2000,
87 VT_BYREF = 0x4000,
88 VT_RESERVED = 0x8000,
89 VT_ILLEGAL = 0xffff,
90 VT_ILLEGALMASKED = 0xfff,
91 VT_TYPEMASK = 0xfff
93 static typelib_t *typelib;
95 /* List of oleauto types that should be recognized by name.
96 * (most of) these seem to be intrinsic types in mktyplib. */
98 static struct oatype {
99 const char *kw;
100 unsigned short vt;
101 } oatypes[] = {
102 {"BSTR", VT_BSTR},
103 {"CURRENCY", VT_CY},
104 {"DATE", VT_DATE},
105 {"DECIMAL", VT_DECIMAL},
106 {"HRESULT", VT_HRESULT},
107 {"LPSTR", VT_LPSTR},
108 {"LPWSTR", VT_LPWSTR},
109 {"SCODE", VT_ERROR},
110 {"VARIANT", VT_VARIANT}
112 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
113 #define KWP(p) ((const struct oatype *)(p))
115 static int kw_cmp_func(const void *s1, const void *s2)
117 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
120 static unsigned short builtin_vt(const char *kw)
122 struct oatype key, *kwp;
123 key.kw = kw;
124 #ifdef KW_BSEARCH
125 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
126 #else
128 int i;
129 for (kwp=NULL, i=0; i < NTYPES; i++)
130 if (!kw_cmp_func(&key, &oatypes[i])) {
131 kwp = &oatypes[i];
132 break;
135 #endif
136 if (kwp) {
137 return kwp->vt;
139 return 0;
142 static int match(const char*n, const char*m)
144 if (!n) return 0;
145 return !strcmp(n, m);
148 unsigned short get_type_vt(type_t *t)
150 unsigned short vt;
152 if (t->name) {
153 vt = builtin_vt(t->name);
154 if (vt) return vt;
157 switch (t->type) {
158 case RPC_FC_BYTE:
159 case RPC_FC_USMALL:
160 return VT_UI1;
161 case RPC_FC_CHAR:
162 case RPC_FC_SMALL:
163 return VT_I1;
164 case RPC_FC_WCHAR:
165 return VT_I2; /* mktyplib seems to parse wchar_t as short */
166 case RPC_FC_SHORT:
167 return VT_I2;
168 case RPC_FC_USHORT:
169 return VT_UI2;
170 case RPC_FC_LONG:
171 if (t->ref && match(t->ref->name, "int")) return VT_INT;
172 return VT_I4;
173 case RPC_FC_ULONG:
174 if (t->ref && match(t->ref->name, "int")) return VT_UINT;
175 return VT_UI4;
176 case RPC_FC_HYPER:
177 if (t->sign < 0) return VT_UI8;
178 if (t->ref && match(t->ref->name, "MIDL_uhyper")) return VT_UI8;
179 return VT_I8;
180 case RPC_FC_FLOAT:
181 return VT_R4;
182 case RPC_FC_DOUBLE:
183 return VT_R8;
184 case RPC_FC_RP:
185 case RPC_FC_UP:
186 case RPC_FC_OP:
187 case RPC_FC_FP:
188 /* it's a pointer... */
189 if (t->ref && t->ref->type == RPC_FC_IP) {
190 /* it's to an interface, which one? */
191 if (match(t->ref->name, "IDispatch"))
192 return VT_DISPATCH;
193 if (match(t->ref->name, "IUnknown"))
194 return VT_UNKNOWN;
196 /* FIXME: should we recurse and add a VT_BYREF? */
197 /* Or just return VT_PTR? */
198 error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
199 break;
200 default:
201 error("get_type_vt: unknown-type: %d\n", t->type);
203 return 0;
206 unsigned short get_var_vt(var_t *v)
208 unsigned short vt;
210 if (v->tname) {
211 vt = builtin_vt(v->tname);
212 if (vt) return vt;
215 return get_type_vt(v->type);
218 void start_typelib(char *name, attr_t *attrs)
220 in_typelib++;
221 if (!do_everything && !typelib_only) return;
223 typelib = xmalloc(sizeof(*typelib));
224 typelib->name = xstrdup(name);
225 typelib->filename = xstrdup(typelib_name);
226 typelib->attrs = attrs;
229 void end_typelib(void)
231 in_typelib--;
232 if (!typelib) return;
234 /* create_msft_typelib(typelib);*/
235 return;
238 void add_interface(type_t *iface)
240 typelib_entry_t *entry;
241 if (!typelib) return;
243 chat("add interface: %s\n", iface->name);
244 entry = xmalloc(sizeof(*entry));
245 entry->kind = TKIND_INTERFACE;
246 entry->u.interface = iface;
247 LINK(entry, typelib->entry);
248 typelib->entry = entry;
251 void add_coclass(class_t *cls)
253 ifref_t *lcur = cls->ifaces;
254 ifref_t *cur;
255 typelib_entry_t *entry;
257 if (lcur) {
258 while (NEXT_LINK(lcur)) lcur = NEXT_LINK(lcur);
261 if (!typelib) return;
263 /* install interfaces the coclass depends on */
264 cur = lcur;
265 while (cur) {
266 add_interface(cur->iface);
267 cur = PREV_LINK(cur);
269 entry = xmalloc(sizeof(*entry));
270 entry->kind = TKIND_COCLASS;
271 entry->u.class = cls;
272 LINK(entry, typelib->entry);
273 typelib->entry = entry;
276 void add_module(type_t *module)
278 typelib_entry_t *entry;
279 if (!typelib) return;
281 chat("add module: %s\n", module->name);
282 entry = xmalloc(sizeof(*entry));
283 entry->kind = TKIND_MODULE;
284 entry->u.module = module;
285 LINK(entry, typelib->entry);
286 typelib->entry = entry;
289 void add_struct(type_t *structure)
291 typelib_entry_t *entry;
292 if (!typelib) return;
294 chat("add struct: %s\n", structure->name);
295 entry = xmalloc(sizeof(*entry));
296 entry->kind = TKIND_RECORD;
297 entry->u.structure = structure;
298 LINK(entry, typelib->entry);
299 typelib->entry = entry;