wnaspi32: Make winaspi.dll into a stand-alone 16-bit module.
[wine] / tools / widl / typetree.c
1 /*
2  * IDL Type Tree
3  *
4  * Copyright 2008 Robert Shearman
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "widl.h"
28 #include "utils.h"
29 #include "parser.h"
30 #include "typetree.h"
31 #include "header.h"
32
33 type_t *duptype(type_t *t, int dupname)
34 {
35   type_t *d = alloc_type();
36
37   *d = *t;
38   if (dupname && t->name)
39     d->name = xstrdup(t->name);
40
41   return d;
42 }
43
44 type_t *make_type(enum type_type type)
45 {
46     type_t *t = alloc_type();
47     t->name = NULL;
48     t->type_type = type;
49     t->attrs = NULL;
50     t->orig = NULL;
51     memset(&t->details, 0, sizeof(t->details));
52     t->typestring_offset = 0;
53     t->ptrdesc = 0;
54     t->ignore = (parse_only != 0);
55     t->defined = FALSE;
56     t->written = FALSE;
57     t->user_types_registered = FALSE;
58     t->tfswrite = FALSE;
59     t->checked = FALSE;
60     t->is_alias = FALSE;
61     t->typelib_idx = -1;
62     init_loc_info(&t->loc_info);
63     return t;
64 }
65
66 type_t *type_new_function(var_list_t *args)
67 {
68     type_t *t = make_type(TYPE_FUNCTION);
69     t->details.function = xmalloc(sizeof(*t->details.function));
70     t->details.function->args = args;
71     t->details.function->idx = -1;
72     return t;
73 }
74
75 type_t *type_new_pointer(unsigned char pointer_default, type_t *ref, attr_list_t *attrs)
76 {
77     type_t *t = make_type(TYPE_POINTER);
78     t->details.pointer.def_fc = pointer_default;
79     t->details.pointer.ref = ref;
80     t->attrs = attrs;
81     return t;
82 }
83
84 type_t *type_new_alias(type_t *t, const char *name)
85 {
86     type_t *a = duptype(t, 0);
87
88     a->name = xstrdup(name);
89     a->attrs = NULL;
90     a->orig = t;
91     a->is_alias = TRUE;
92     /* for pointer types */
93     a->details = t->details;
94     init_loc_info(&a->loc_info);
95
96     return a;
97 }
98
99 type_t *type_new_module(char *name)
100 {
101     type_t *type = get_type(TYPE_MODULE, name, 0);
102     if (type->type_type != TYPE_MODULE || type->defined)
103         error_loc("%s: redefinition error; original definition was at %s:%d\n",
104                   type->name, type->loc_info.input_name, type->loc_info.line_number);
105     type->name = name;
106     return type;
107 }
108
109 type_t *type_new_coclass(char *name)
110 {
111     type_t *type = get_type(TYPE_COCLASS, name, 0);
112     if (type->type_type != TYPE_COCLASS || type->defined)
113         error_loc("%s: redefinition error; original definition was at %s:%d\n",
114                   type->name, type->loc_info.input_name, type->loc_info.line_number);
115     type->name = name;
116     return type;
117 }
118
119
120 type_t *type_new_array(const char *name, type_t *element, int declptr,
121                        unsigned int dim, expr_t *size_is, expr_t *length_is,
122                        unsigned char ptr_default_fc)
123 {
124     type_t *t = make_type(TYPE_ARRAY);
125     if (name) t->name = xstrdup(name);
126     t->details.array.declptr = declptr;
127     t->details.array.length_is = length_is;
128     if (size_is)
129         t->details.array.size_is = size_is;
130     else
131         t->details.array.dim = dim;
132     t->details.array.elem = element;
133     t->details.array.ptr_def_fc = ptr_default_fc;
134     return t;
135 }
136
137 type_t *type_new_basic(enum type_basic_type basic_type)
138 {
139     type_t *t = make_type(TYPE_BASIC);
140     t->details.basic.type = basic_type;
141     t->details.basic.sign = 0;
142     return t;
143 }
144
145 type_t *type_new_int(enum type_basic_type basic_type, int sign)
146 {
147     static type_t *int_types[TYPE_BASIC_INT_MAX+1][3];
148
149     assert(basic_type <= TYPE_BASIC_INT_MAX);
150
151     /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
152     if (!int_types[basic_type][sign + 1])
153     {
154         int_types[basic_type][sign + 1] = type_new_basic(basic_type);
155         int_types[basic_type][sign + 1]->details.basic.sign = sign;
156     }
157     return int_types[basic_type][sign + 1];
158 }
159
160 type_t *type_new_void(void)
161 {
162     static type_t *void_type = NULL;
163     if (!void_type)
164         void_type = make_type(TYPE_VOID);
165     return void_type;
166 }
167
168 type_t *type_new_enum(const char *name, int defined, var_list_t *enums)
169 {
170     type_t *tag_type = name ? find_type(name, tsENUM) : NULL;
171     type_t *t = make_type(TYPE_ENUM);
172     t->name = name;
173
174     if (tag_type && tag_type->details.enumeration)
175         t->details.enumeration = tag_type->details.enumeration;
176     else if (defined)
177     {
178         t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
179         t->details.enumeration->enums = enums;
180         t->defined = TRUE;
181     }
182
183     if (name)
184     {
185         if (defined)
186             reg_type(t, name, tsENUM);
187         else
188             add_incomplete(t);
189     }
190     return t;
191 }
192
193 type_t *type_new_struct(char *name, int defined, var_list_t *fields)
194 {
195     type_t *tag_type = name ? find_type(name, tsSTRUCT) : NULL;
196     type_t *t = make_type(TYPE_STRUCT);
197     t->name = name;
198     if (tag_type && tag_type->details.structure)
199         t->details.structure = tag_type->details.structure;
200     else if (defined)
201     {
202         t->details.structure = xmalloc(sizeof(*t->details.structure));
203         t->details.structure->fields = fields;
204         t->defined = TRUE;
205     }
206     if (name)
207     {
208         if (defined)
209             reg_type(t, name, tsSTRUCT);
210         else
211             add_incomplete(t);
212     }
213     return t;
214 }
215
216 type_t *type_new_nonencapsulated_union(const char *name, int defined, var_list_t *fields)
217 {
218     type_t *tag_type = name ? find_type(name, tsUNION) : NULL;
219     type_t *t = make_type(TYPE_UNION);
220     t->name = name;
221     if (tag_type && tag_type->details.structure)
222         t->details.structure = tag_type->details.structure;
223     else if (defined)
224     {
225         t->details.structure = xmalloc(sizeof(*t->details.structure));
226         t->details.structure->fields = fields;
227         t->defined = TRUE;
228     }
229     if (name)
230     {
231         if (defined)
232             reg_type(t, name, tsUNION);
233         else
234             add_incomplete(t);
235     }
236     return t;
237 }
238
239 type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
240 {
241     type_t *t = get_type(TYPE_ENCAPSULATED_UNION, name, tsUNION);
242     if (!union_field) union_field = make_var( xstrdup("tagged_union") );
243     union_field->type = type_new_nonencapsulated_union(NULL, TRUE, cases);
244     t->details.structure = xmalloc(sizeof(*t->details.structure));
245     t->details.structure->fields = append_var( NULL, switch_field );
246     t->details.structure->fields = append_var( t->details.structure->fields, union_field );
247     t->defined = TRUE;
248     return t;
249 }
250
251 static int compute_method_indexes(type_t *iface)
252 {
253     int idx;
254     statement_t *stmt;
255
256     if (!iface->details.iface)
257         return 0;
258
259     if (type_iface_get_inherit(iface))
260         idx = compute_method_indexes(type_iface_get_inherit(iface));
261     else
262         idx = 0;
263
264     STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
265     {
266         var_t *func = stmt->u.var;
267         if (!is_callas(func->attrs))
268             func->type->details.function->idx = idx++;
269     }
270
271     return idx;
272 }
273
274 void type_interface_define(type_t *iface, type_t *inherit, statement_list_t *stmts)
275 {
276     iface->details.iface = xmalloc(sizeof(*iface->details.iface));
277     iface->details.iface->disp_props = NULL;
278     iface->details.iface->disp_methods = NULL;
279     iface->details.iface->stmts = stmts;
280     iface->details.iface->inherit = inherit;
281     iface->defined = TRUE;
282     compute_method_indexes(iface);
283 }
284
285 void type_dispinterface_define(type_t *iface, var_list_t *props, func_list_t *methods)
286 {
287     iface->details.iface = xmalloc(sizeof(*iface->details.iface));
288     iface->details.iface->disp_props = props;
289     iface->details.iface->disp_methods = methods;
290     iface->details.iface->stmts = NULL;
291     iface->details.iface->inherit = find_type("IDispatch", 0);
292     if (!iface->details.iface->inherit) error_loc("IDispatch is undefined\n");
293     iface->defined = TRUE;
294     compute_method_indexes(iface);
295 }
296
297 void type_dispinterface_define_from_iface(type_t *dispiface, type_t *iface)
298 {
299     type_dispinterface_define(dispiface, iface->details.iface->disp_props,
300                               iface->details.iface->disp_methods);
301 }
302
303 void type_module_define(type_t *module, statement_list_t *stmts)
304 {
305     if (module->details.module) error_loc("multiple definition error\n");
306     module->details.module = xmalloc(sizeof(*module->details.module));
307     module->details.module->stmts = stmts;
308     module->defined = TRUE;
309 }
310
311 type_t *type_coclass_define(type_t *coclass, ifref_list_t *ifaces)
312 {
313     coclass->details.coclass.ifaces = ifaces;
314     coclass->defined = TRUE;
315     return coclass;
316 }
317
318 int type_is_equal(const type_t *type1, const type_t *type2)
319 {
320     if (type_get_type_detect_alias(type1) != type_get_type_detect_alias(type2))
321         return FALSE;
322
323     if (type1->name && type2->name)
324         return !strcmp(type1->name, type2->name);
325     else if ((!type1->name && type2->name) || (type1->name && !type2->name))
326         return FALSE;
327
328     /* FIXME: do deep inspection of types to determine if they are equal */
329
330     return FALSE;
331 }