dinput: Make EnumDevices and GetObjectInfo consistent about GUIDs.
[wine.git] / tools / widl / typetree.c
blob4359d924ba0e5be4edb68b3e822d3094cc9c9f46
1 /*
2 * IDL Type Tree
4 * Copyright 2008 Robert Shearman
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "widl.h"
28 #include "utils.h"
29 #include "parser.h"
30 #include "typetree.h"
31 #include "header.h"
33 type_t *duptype(type_t *t, int dupname)
35 type_t *d = alloc_type();
37 *d = *t;
38 if (dupname && t->name)
39 d->name = xstrdup(t->name);
41 return d;
44 type_t *make_type(enum type_type type)
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;
66 type_t *type_new_function(var_list_t *args)
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;
75 type_t *type_new_pointer(unsigned char pointer_default, type_t *ref, attr_list_t *attrs)
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;
84 type_t *type_new_alias(type_t *t, const char *name)
86 type_t *a = duptype(t, 0);
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);
96 return a;
99 type_t *type_new_module(char *name)
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;
109 type_t *type_new_coclass(char *name)
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;
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)
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;
137 type_t *type_new_basic(enum type_basic_type basic_type)
139 type_t *t = make_type(TYPE_BASIC);
140 t->details.basic.type = basic_type;
141 t->details.basic.sign = 0;
142 return t;
145 type_t *type_new_int(enum type_basic_type basic_type, int sign)
147 static type_t *int_types[TYPE_BASIC_INT_MAX+1][3];
149 assert(basic_type <= TYPE_BASIC_INT_MAX);
151 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
152 if (!int_types[basic_type][sign + 1])
154 int_types[basic_type][sign + 1] = type_new_basic(basic_type);
155 int_types[basic_type][sign + 1]->details.basic.sign = sign;
157 return int_types[basic_type][sign + 1];
160 type_t *type_new_void(void)
162 static type_t *void_type = NULL;
163 if (!void_type)
164 void_type = make_type(TYPE_VOID);
165 return void_type;
168 type_t *type_new_enum(const char *name, int defined, var_list_t *enums)
170 type_t *tag_type = name ? find_type(name, tsENUM) : NULL;
171 type_t *t = make_type(TYPE_ENUM);
172 t->name = name;
174 if (tag_type && tag_type->details.enumeration)
175 t->details.enumeration = tag_type->details.enumeration;
176 else if (defined)
178 t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
179 t->details.enumeration->enums = enums;
180 t->defined = TRUE;
183 if (name)
185 if (defined)
186 reg_type(t, name, tsENUM);
187 else
188 add_incomplete(t);
190 return t;
193 type_t *type_new_struct(char *name, int defined, var_list_t *fields)
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)
202 t->details.structure = xmalloc(sizeof(*t->details.structure));
203 t->details.structure->fields = fields;
204 t->defined = TRUE;
206 if (name)
208 if (defined)
209 reg_type(t, name, tsSTRUCT);
210 else
211 add_incomplete(t);
213 return t;
216 type_t *type_new_nonencapsulated_union(const char *name, int defined, var_list_t *fields)
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)
225 t->details.structure = xmalloc(sizeof(*t->details.structure));
226 t->details.structure->fields = fields;
227 t->defined = TRUE;
229 if (name)
231 if (defined)
232 reg_type(t, name, tsUNION);
233 else
234 add_incomplete(t);
236 return t;
239 type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
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;
251 static int compute_method_indexes(type_t *iface)
253 int idx;
254 statement_t *stmt;
256 if (!iface->details.iface)
257 return 0;
259 if (type_iface_get_inherit(iface))
260 idx = compute_method_indexes(type_iface_get_inherit(iface));
261 else
262 idx = 0;
264 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
266 var_t *func = stmt->u.var;
267 if (!is_callas(func->attrs))
268 func->type->details.function->idx = idx++;
271 return idx;
274 void type_interface_define(type_t *iface, type_t *inherit, statement_list_t *stmts)
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);
285 void type_dispinterface_define(type_t *iface, var_list_t *props, func_list_t *methods)
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);
297 void type_dispinterface_define_from_iface(type_t *dispiface, type_t *iface)
299 type_dispinterface_define(dispiface, iface->details.iface->disp_props,
300 iface->details.iface->disp_methods);
303 void type_module_define(type_t *module, statement_list_t *stmts)
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;
311 type_t *type_coclass_define(type_t *coclass, ifref_list_t *ifaces)
313 coclass->details.coclass.ifaces = ifaces;
314 coclass->defined = TRUE;
315 return coclass;
318 int type_is_equal(const type_t *type1, const type_t *type2)
320 if (type_get_type_detect_alias(type1) != type_get_type_detect_alias(type2))
321 return FALSE;
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;
328 /* FIXME: do deep inspection of types to determine if they are equal */
330 return FALSE;