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