widl: Don't store the default pointer type in the type_t structure.
[wine.git] / tools / widl / typetree.c
bloba066125b16776d77bab1fe6ebb31a11e74639ea4
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->namespace = NULL;
49 t->type_type = type;
50 t->attrs = NULL;
51 t->c_name = NULL;
52 memset(&t->details, 0, sizeof(t->details));
53 t->typestring_offset = 0;
54 t->ptrdesc = 0;
55 t->ignore = (parse_only != 0);
56 t->defined = FALSE;
57 t->written = FALSE;
58 t->user_types_registered = FALSE;
59 t->tfswrite = FALSE;
60 t->checked = FALSE;
61 t->typelib_idx = -1;
62 init_loc_info(&t->loc_info);
63 return t;
66 static const var_t *find_arg(const var_list_t *args, const char *name)
68 const var_t *arg;
70 if (args) LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
72 if (arg->name && !strcmp(name, arg->name))
73 return arg;
76 return NULL;
79 const char *type_get_name(const type_t *type, enum name_type name_type)
81 switch(name_type) {
82 case NAME_DEFAULT:
83 return type->name;
84 case NAME_C:
85 return type->c_name;
88 assert(0);
89 return NULL;
92 static char *append_namespace(char *ptr, struct namespace *namespace, const char *separator)
94 if(is_global_namespace(namespace)) {
95 if(!use_abi_namespace)
96 return ptr;
97 strcpy(ptr, "ABI");
98 strcat(ptr, separator);
99 return ptr + strlen(ptr);
102 ptr = append_namespace(ptr, namespace->parent, separator);
103 strcpy(ptr, namespace->name);
104 strcat(ptr, separator);
105 return ptr + strlen(ptr);
108 char *format_namespace(struct namespace *namespace, const char *prefix, const char *separator, const char *suffix)
110 unsigned len = strlen(prefix) + strlen(suffix);
111 unsigned sep_len = strlen(separator);
112 struct namespace *iter;
113 char *ret, *ptr;
115 if(use_abi_namespace && !is_global_namespace(namespace))
116 len += 3 /* strlen("ABI") */ + sep_len;
118 for(iter = namespace; !is_global_namespace(iter); iter = iter->parent)
119 len += strlen(iter->name) + sep_len;
121 ret = xmalloc(len+1);
122 strcpy(ret, prefix);
123 ptr = append_namespace(ret + strlen(ret), namespace, separator);
124 strcpy(ptr, suffix);
126 return ret;
129 type_t *type_new_function(var_list_t *args)
131 var_t *arg;
132 type_t *t;
133 unsigned int i = 0;
135 if (args)
137 arg = LIST_ENTRY(list_head(args), var_t, entry);
138 if (list_count(args) == 1 && !arg->name && arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
140 list_remove(&arg->entry);
141 free(arg);
142 free(args);
143 args = NULL;
146 if (args) LIST_FOR_EACH_ENTRY(arg, args, var_t, entry)
148 if (arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
149 error_loc("argument '%s' has void type\n", arg->name);
150 if (!arg->name)
152 if (i > 26 * 26)
153 error_loc("too many unnamed arguments\n");
154 else
156 int unique;
159 char name[3];
160 name[0] = i > 26 ? 'a' + i / 26 : 'a' + i;
161 name[1] = i > 26 ? 'a' + i % 26 : 0;
162 name[2] = 0;
163 unique = !find_arg(args, name);
164 if (unique)
165 arg->name = xstrdup(name);
166 i++;
167 } while (!unique);
172 t = make_type(TYPE_FUNCTION);
173 t->details.function = xmalloc(sizeof(*t->details.function));
174 t->details.function->args = args;
175 t->details.function->retval = make_var(xstrdup("_RetVal"));
176 return t;
179 type_t *type_new_pointer(type_t *ref)
181 type_t *t = make_type(TYPE_POINTER);
182 t->details.pointer.ref.type = ref;
183 return t;
186 type_t *type_new_alias(const decl_spec_t *t, const char *name)
188 type_t *a = make_type(TYPE_ALIAS);
190 a->name = xstrdup(name);
191 a->attrs = NULL;
192 a->details.alias.aliasee = *t;
193 init_loc_info(&a->loc_info);
195 return a;
198 type_t *type_new_module(char *name)
200 type_t *type = get_type(TYPE_MODULE, name, NULL, 0);
201 if (type->type_type != TYPE_MODULE || type->defined)
202 error_loc("%s: redefinition error; original definition was at %s:%d\n",
203 type->name, type->loc_info.input_name, type->loc_info.line_number);
204 type->name = name;
205 return type;
208 type_t *type_new_coclass(char *name)
210 type_t *type = get_type(TYPE_COCLASS, name, NULL, 0);
211 if (type->type_type != TYPE_COCLASS || type->defined)
212 error_loc("%s: redefinition error; original definition was at %s:%d\n",
213 type->name, type->loc_info.input_name, type->loc_info.line_number);
214 type->name = name;
215 return type;
219 type_t *type_new_array(const char *name, const decl_spec_t *element, int declptr,
220 unsigned int dim, expr_t *size_is, expr_t *length_is)
222 type_t *t = make_type(TYPE_ARRAY);
223 if (name) t->name = xstrdup(name);
224 t->details.array.declptr = declptr;
225 t->details.array.length_is = length_is;
226 if (size_is)
227 t->details.array.size_is = size_is;
228 else
229 t->details.array.dim = dim;
230 if (element)
231 t->details.array.elem = *element;
232 return t;
235 type_t *type_new_basic(enum type_basic_type basic_type)
237 type_t *t = make_type(TYPE_BASIC);
238 t->details.basic.type = basic_type;
239 t->details.basic.sign = 0;
240 return t;
243 type_t *type_new_int(enum type_basic_type basic_type, int sign)
245 static type_t *int_types[TYPE_BASIC_INT_MAX+1][3];
247 assert(basic_type <= TYPE_BASIC_INT_MAX);
249 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
250 if (!int_types[basic_type][sign + 1])
252 int_types[basic_type][sign + 1] = type_new_basic(basic_type);
253 int_types[basic_type][sign + 1]->details.basic.sign = sign;
255 return int_types[basic_type][sign + 1];
258 type_t *type_new_void(void)
260 static type_t *void_type = NULL;
261 if (!void_type)
262 void_type = make_type(TYPE_VOID);
263 return void_type;
266 type_t *type_new_enum(const char *name, struct namespace *namespace, int defined, var_list_t *enums)
268 type_t *tag_type = name ? find_type(name, namespace, tsENUM) : NULL;
269 type_t *t = make_type(TYPE_ENUM);
270 t->name = name;
271 t->namespace = namespace;
273 if (tag_type && tag_type->details.enumeration)
274 t->details.enumeration = tag_type->details.enumeration;
275 else if (defined)
277 t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
278 t->details.enumeration->enums = enums;
279 t->defined = TRUE;
282 if (name)
284 if (defined)
285 reg_type(t, name, namespace, tsENUM);
286 else
287 add_incomplete(t);
289 return t;
292 type_t *type_new_struct(char *name, struct namespace *namespace, int defined, var_list_t *fields)
294 type_t *tag_type = name ? find_type(name, namespace, tsSTRUCT) : NULL;
295 type_t *t;
297 /* avoid creating duplicate typelib type entries */
298 if (tag_type && do_typelib) return tag_type;
300 t = make_type(TYPE_STRUCT);
301 t->name = name;
302 t->namespace = namespace;
304 if (tag_type && tag_type->details.structure)
305 t->details.structure = tag_type->details.structure;
306 else if (defined)
308 t->details.structure = xmalloc(sizeof(*t->details.structure));
309 t->details.structure->fields = fields;
310 t->defined = TRUE;
312 if (name)
314 if (defined)
315 reg_type(t, name, namespace, tsSTRUCT);
316 else
317 add_incomplete(t);
319 return t;
322 type_t *type_new_nonencapsulated_union(const char *name, int defined, var_list_t *fields)
324 type_t *tag_type = name ? find_type(name, NULL, tsUNION) : NULL;
325 type_t *t = make_type(TYPE_UNION);
326 t->name = name;
327 if (tag_type && tag_type->details.structure)
328 t->details.structure = tag_type->details.structure;
329 else if (defined)
331 t->details.structure = xmalloc(sizeof(*t->details.structure));
332 t->details.structure->fields = fields;
333 t->defined = TRUE;
335 if (name)
337 if (defined)
338 reg_type(t, name, NULL, tsUNION);
339 else
340 add_incomplete(t);
342 return t;
345 type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
347 type_t *t = get_type(TYPE_ENCAPSULATED_UNION, name, NULL, tsUNION);
348 if (!union_field) union_field = make_var( xstrdup("tagged_union") );
349 union_field->declspec.type = type_new_nonencapsulated_union(NULL, TRUE, cases);
350 t->details.structure = xmalloc(sizeof(*t->details.structure));
351 t->details.structure->fields = append_var( NULL, switch_field );
352 t->details.structure->fields = append_var( t->details.structure->fields, union_field );
353 t->defined = TRUE;
354 return t;
357 static int is_valid_bitfield_type(const type_t *type)
359 switch (type_get_type(type))
361 case TYPE_ENUM:
362 return TRUE;
363 case TYPE_BASIC:
364 switch (type_basic_get_type(type))
366 case TYPE_BASIC_INT8:
367 case TYPE_BASIC_INT16:
368 case TYPE_BASIC_INT32:
369 case TYPE_BASIC_INT64:
370 case TYPE_BASIC_INT:
371 case TYPE_BASIC_INT3264:
372 case TYPE_BASIC_LONG:
373 case TYPE_BASIC_CHAR:
374 case TYPE_BASIC_HYPER:
375 case TYPE_BASIC_BYTE:
376 case TYPE_BASIC_WCHAR:
377 case TYPE_BASIC_ERROR_STATUS_T:
378 return TRUE;
379 case TYPE_BASIC_FLOAT:
380 case TYPE_BASIC_DOUBLE:
381 case TYPE_BASIC_HANDLE:
382 return FALSE;
384 return FALSE;
385 default:
386 return FALSE;
390 type_t *type_new_bitfield(type_t *field, const expr_t *bits)
392 type_t *t;
394 if (!is_valid_bitfield_type(field))
395 error_loc("bit-field has invalid type\n");
397 if (bits->cval < 0)
398 error_loc("negative width for bit-field\n");
400 /* FIXME: validate bits->cval <= memsize(field) * 8 */
402 t = make_type(TYPE_BITFIELD);
403 t->details.bitfield.field = field;
404 t->details.bitfield.bits = bits;
405 return t;
408 static unsigned int compute_method_indexes(type_t *iface)
410 unsigned int idx;
411 statement_t *stmt;
413 if (!iface->details.iface)
414 return 0;
416 if (type_iface_get_inherit(iface))
417 idx = compute_method_indexes(type_iface_get_inherit(iface));
418 else
419 idx = 0;
421 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
423 var_t *func = stmt->u.var;
424 if (!is_callas(func->attrs))
425 func->func_idx = idx++;
428 return idx;
431 void type_interface_define(type_t *iface, type_t *inherit, statement_list_t *stmts)
433 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
434 iface->details.iface->disp_props = NULL;
435 iface->details.iface->disp_methods = NULL;
436 iface->details.iface->stmts = stmts;
437 iface->details.iface->inherit = inherit;
438 iface->details.iface->disp_inherit = NULL;
439 iface->details.iface->async_iface = NULL;
440 iface->defined = TRUE;
441 compute_method_indexes(iface);
444 void type_dispinterface_define(type_t *iface, var_list_t *props, var_list_t *methods)
446 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
447 iface->details.iface->disp_props = props;
448 iface->details.iface->disp_methods = methods;
449 iface->details.iface->stmts = NULL;
450 iface->details.iface->inherit = find_type("IDispatch", NULL, 0);
451 if (!iface->details.iface->inherit) error_loc("IDispatch is undefined\n");
452 iface->details.iface->disp_inherit = NULL;
453 iface->details.iface->async_iface = NULL;
454 iface->defined = TRUE;
455 compute_method_indexes(iface);
458 void type_dispinterface_define_from_iface(type_t *dispiface, type_t *iface)
460 dispiface->details.iface = xmalloc(sizeof(*dispiface->details.iface));
461 dispiface->details.iface->disp_props = NULL;
462 dispiface->details.iface->disp_methods = NULL;
463 dispiface->details.iface->stmts = NULL;
464 dispiface->details.iface->inherit = find_type("IDispatch", NULL, 0);
465 if (!dispiface->details.iface->inherit) error_loc("IDispatch is undefined\n");
466 dispiface->details.iface->disp_inherit = iface;
467 dispiface->details.iface->async_iface = NULL;
468 dispiface->defined = TRUE;
469 compute_method_indexes(dispiface);
472 void type_module_define(type_t *module, statement_list_t *stmts)
474 if (module->details.module) error_loc("multiple definition error\n");
475 module->details.module = xmalloc(sizeof(*module->details.module));
476 module->details.module->stmts = stmts;
477 module->defined = TRUE;
480 type_t *type_coclass_define(type_t *coclass, ifref_list_t *ifaces)
482 coclass->details.coclass.ifaces = ifaces;
483 coclass->defined = TRUE;
484 return coclass;
487 int type_is_equal(const type_t *type1, const type_t *type2)
489 if (type_get_type_detect_alias(type1) != type_get_type_detect_alias(type2))
490 return FALSE;
492 if (type1->name && type2->name)
493 return !strcmp(type1->name, type2->name);
494 else if ((!type1->name && type2->name) || (type1->name && !type2->name))
495 return FALSE;
497 /* FIXME: do deep inspection of types to determine if they are equal */
499 return FALSE;