widl: Don't use recursion to implement write_procformatstring_type.
[wine] / 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     unsigned 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     return VT_USERDEFINED;
156   case 0:
157     if(t->attrs)
158         return VT_USERDEFINED;
159     return 0;
160   default:
161     error("get_type_vt: unknown type: 0x%02x\n", t->type);
162   }
163   return 0;
164 }
165
166 unsigned short get_var_vt(var_t *v)
167 {
168   unsigned short vt;
169
170   chat("get_var_vt: %p tname %s\n", v, v->tname);
171   if (v->tname) {
172     vt = builtin_vt(v->tname);
173     if (vt) return vt;
174   }
175
176   return get_type_vt(v->type);
177 }
178
179 void start_typelib(char *name, attr_t *attrs)
180 {
181     in_typelib++;
182     if (!do_typelib) return;
183
184     typelib = xmalloc(sizeof(*typelib));
185     typelib->name = xstrdup(name);
186     typelib->filename = xstrdup(typelib_name);
187     typelib->attrs = attrs;
188 }
189
190 void end_typelib(void)
191 {
192     in_typelib--;
193     if (!typelib) return;
194
195     create_msft_typelib(typelib);
196     return;
197 }
198
199 void add_interface(type_t *iface)
200 {
201     typelib_entry_t *entry;
202     if (!typelib) return;
203
204     chat("add interface: %s\n", iface->name);
205     entry = xmalloc(sizeof(*entry));
206     entry->kind = TKIND_INTERFACE;
207     entry->u.interface = iface;
208     LINK(entry, typelib->entry);
209     typelib->entry = entry;
210 }
211
212 void add_coclass(class_t *cls)
213 {
214     typelib_entry_t *entry;
215
216     if (!typelib) return;
217
218     chat("add coclass: %s\n", cls->name);
219
220     entry = xmalloc(sizeof(*entry));
221     entry->kind = TKIND_COCLASS;
222     entry->u.class = cls;
223     LINK(entry, typelib->entry);
224     typelib->entry = entry;
225 }
226
227 void add_module(type_t *module)
228 {
229     typelib_entry_t *entry;
230     if (!typelib) return;
231
232     chat("add module: %s\n", module->name);
233     entry = xmalloc(sizeof(*entry));
234     entry->kind = TKIND_MODULE;
235     entry->u.module = module;
236     LINK(entry, typelib->entry);
237     typelib->entry = entry;
238 }
239
240 void add_struct(type_t *structure)
241 {
242      typelib_entry_t *entry;
243      if (!typelib) return;
244
245      chat("add struct: %s\n", structure->name);
246      entry = xmalloc(sizeof(*entry));
247      entry->kind = TKIND_RECORD;
248      entry->u.structure = structure;
249      LINK(entry, typelib->entry);
250      typelib->entry = entry;
251 }
252
253 void add_enum(type_t *enumeration)
254 {
255      typelib_entry_t *entry;
256      if (!typelib) return;
257
258      chat("add enum: %s\n", enumeration->name);
259      entry = xmalloc(sizeof(*entry));
260      entry->kind = TKIND_ENUM;
261      entry->u.enumeration = enumeration;
262      LINK(entry, typelib->entry);
263      typelib->entry = entry;
264 }
265
266 void add_typedef(type_t *tdef, var_t *name)
267 {
268      typelib_entry_t *entry;
269      if (!typelib) return;
270
271      entry = xmalloc(sizeof(*entry));
272      entry->kind = TKIND_ALIAS;
273      entry->u.tdef = xmalloc(sizeof(*entry->u.tdef));
274      memcpy(entry->u.tdef, name, sizeof(*name));
275      entry->u.tdef->type = tdef;
276      entry->u.tdef->name = xstrdup(name->name);
277      LINK(entry, typelib->entry);
278      typelib->entry = entry;
279 }