Change oldnames option from '-o' to '--oldnames'.
[reactos.git] / reactos / tools / widl / typelib.c
1 /*
2 * IDL Compiler
3 *
4 * Copyright 2004 Ove Kaaven
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22
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>
30
31 #include "widl.h"
32 #include "utils.h"
33 #include "parser.h"
34 #include "header.h"
35 #include "typelib.h"
36
37 int in_typelib = 0;
38
39 static typelib_t *typelib;
40
41 /* List of oleauto types that should be recognized by name.
42 * (most of) these seem to be intrinsic types in mktyplib. */
43
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},
57 {"VARIANT_BOOL", VT_BOOL}
58 };
59 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
60 #define KWP(p) ((const struct oatype *)(p))
61
62 static int kw_cmp_func(const void *s1, const void *s2)
63 {
64 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
65 }
66
67 static unsigned short builtin_vt(const char *kw)
68 {
69 struct oatype key, *kwp;
70 key.kw = kw;
71 #ifdef KW_BSEARCH
72 kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
73 #else
74 {
75 int i;
76 for (kwp=NULL, i=0; i < NTYPES; i++)
77 if (!kw_cmp_func(&key, &oatypes[i])) {
78 kwp = &oatypes[i];
79 break;
80 }
81 }
82 #endif
83 if (kwp) {
84 return kwp->vt;
85 }
86 return 0;
87 }
88
89 static int match(const char*n, const char*m)
90 {
91 if (!n) return 0;
92 return !strcmp(n, m);
93 }
94
95 unsigned short get_type_vt(type_t *t)
96 {
97 unsigned short vt;
98
99 chat("get_type_vt: %p type->name %s\n", t, t->name);
100 if (t->name) {
101 vt = builtin_vt(t->name);
102 if (vt) return vt;
103 }
104
105 switch (t->type) {
106 case RPC_FC_BYTE:
107 case RPC_FC_USMALL:
108 return VT_UI1;
109 case RPC_FC_CHAR:
110 case RPC_FC_SMALL:
111 return VT_I1;
112 case RPC_FC_WCHAR:
113 return VT_I2; /* mktyplib seems to parse wchar_t as short */
114 case RPC_FC_SHORT:
115 return VT_I2;
116 case RPC_FC_USHORT:
117 return VT_UI2;
118 case RPC_FC_LONG:
119 if (t->ref && match(t->ref->name, "int")) return VT_INT;
120 return VT_I4;
121 case RPC_FC_ULONG:
122 if (t->ref && match(t->ref->name, "int")) return VT_UINT;
123 return VT_UI4;
124 case RPC_FC_HYPER:
125 if (t->sign < 0) return VT_UI8;
126 if (t->ref && match(t->ref->name, "MIDL_uhyper")) return VT_UI8;
127 return VT_I8;
128 case RPC_FC_FLOAT:
129 return VT_R4;
130 case RPC_FC_DOUBLE:
131 return VT_R8;
132 case RPC_FC_RP:
133 case RPC_FC_UP:
134 case RPC_FC_OP:
135 case RPC_FC_FP:
136 if(t->ref)
137 return VT_PTR;
138
139 error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
140 break;
141 case RPC_FC_IP:
142 if(match(t->name, "IUnknown"))
143 return VT_UNKNOWN;
144 if(match(t->name, "IDispatch"))
145 return VT_DISPATCH;
146 return VT_USERDEFINED;
147
148 case RPC_FC_ENUM16:
149 case RPC_FC_STRUCT:
150 case RPC_FC_PSTRUCT:
151 case RPC_FC_CSTRUCT:
152 case RPC_FC_CPSTRUCT:
153 case RPC_FC_CVSTRUCT:
154 case RPC_FC_BOGUS_STRUCT:
155
156 return VT_USERDEFINED;
157 case 0:
158 if(t->attrs)
159 return VT_USERDEFINED;
160 return 0;
161 default:
162 error("get_type_vt: unknown type: 0x%02x\n", t->type);
163 }
164 return 0;
165 }
166
167 unsigned short get_var_vt(var_t *v)
168 {
169 unsigned short vt;
170
171 chat("get_var_vt: %p tname %s\n", v, v->tname);
172 if (v->tname) {
173 vt = builtin_vt(v->tname);
174 if (vt) return vt;
175 }
176
177 return get_type_vt(v->type);
178 }
179
180 void start_typelib(char *name, attr_t *attrs)
181 {
182 in_typelib++;
183 if (!do_typelib) return;
184
185 typelib = xmalloc(sizeof(*typelib));
186 typelib->name = xstrdup(name);
187 typelib->filename = xstrdup(typelib_name);
188 typelib->attrs = attrs;
189 }
190
191 void end_typelib(void)
192 {
193 in_typelib--;
194 if (!typelib) return;
195
196 create_msft_typelib(typelib);
197 return;
198 }
199
200 void add_interface(type_t *iface)
201 {
202 typelib_entry_t *entry;
203 if (!typelib) return;
204
205 chat("add interface: %s\n", iface->name);
206 entry = xmalloc(sizeof(*entry));
207 entry->kind = TKIND_INTERFACE;
208 entry->u.interface = iface;
209 LINK(entry, typelib->entry);
210 typelib->entry = entry;
211 }
212
213 void add_coclass(class_t *cls)
214 {
215 typelib_entry_t *entry;
216
217 if (!typelib) return;
218
219 chat("add coclass: %s\n", cls->name);
220
221 entry = xmalloc(sizeof(*entry));
222 entry->kind = TKIND_COCLASS;
223 entry->u.class = cls;
224 LINK(entry, typelib->entry);
225 typelib->entry = entry;
226 }
227
228 void add_module(type_t *module)
229 {
230 typelib_entry_t *entry;
231 if (!typelib) return;
232
233 chat("add module: %s\n", module->name);
234 entry = xmalloc(sizeof(*entry));
235 entry->kind = TKIND_MODULE;
236 entry->u.module = module;
237 LINK(entry, typelib->entry);
238 typelib->entry = entry;
239 }
240
241 void add_struct(type_t *structure)
242 {
243 typelib_entry_t *entry;
244 if (!typelib) return;
245
246 chat("add struct: %s\n", structure->name);
247 entry = xmalloc(sizeof(*entry));
248 entry->kind = TKIND_RECORD;
249 entry->u.structure = structure;
250 LINK(entry, typelib->entry);
251 typelib->entry = entry;
252 }
253
254 void add_enum(type_t *enumeration)
255 {
256 typelib_entry_t *entry;
257 if (!typelib) return;
258
259 chat("add enum: %s\n", enumeration->name);
260 entry = xmalloc(sizeof(*entry));
261 entry->kind = TKIND_ENUM;
262 entry->u.enumeration = enumeration;
263 LINK(entry, typelib->entry);
264 typelib->entry = entry;
265 }
266
267 void add_typedef(type_t *tdef, var_t *name)
268 {
269 typelib_entry_t *entry;
270 if (!typelib) return;
271
272 entry = xmalloc(sizeof(*entry));
273 entry->kind = TKIND_ALIAS;
274 entry->u.tdef = xmalloc(sizeof(*entry->u.tdef));
275 memcpy(entry->u.tdef, name, sizeof(*name));
276 entry->u.tdef->type = tdef;
277 entry->u.tdef->name = xstrdup(name->name);
278 LINK(entry, typelib->entry);
279 typelib->entry = entry;
280 }