msxml3: Fold in reset_output_buffer.
[wine/zf.git] / tools / widl / typetree.c
bloba18ffe1f4a5e2ef505a7a954bfb7d60cf3afabe2
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 ? type->c_name : type->name;
88 assert(0);
89 return NULL;
92 static char *append_namespace(char *ptr, struct namespace *namespace, const char *separator, const char *abi_prefix)
94 if(is_global_namespace(namespace)) {
95 if(!abi_prefix) return ptr;
96 strcpy(ptr, abi_prefix);
97 strcat(ptr, separator);
98 return ptr + strlen(ptr);
101 ptr = append_namespace(ptr, namespace->parent, separator, abi_prefix);
102 strcpy(ptr, namespace->name);
103 strcat(ptr, separator);
104 return ptr + strlen(ptr);
107 char *format_namespace(struct namespace *namespace, const char *prefix, const char *separator, const char *suffix,
108 const char *abi_prefix)
110 unsigned len = strlen(prefix) + strlen(suffix);
111 unsigned sep_len = strlen(separator);
112 struct namespace *iter;
113 char *ret, *ptr;
115 if(abi_prefix)
116 len += strlen(abi_prefix) + 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, abi_prefix);
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;
218 type_t *type_new_runtimeclass(char *name, struct namespace *namespace)
220 type_t *type = get_type(TYPE_RUNTIMECLASS, name, NULL, 0);
221 if (type->type_type != TYPE_RUNTIMECLASS || type->defined)
222 error_loc("%s: redefinition error; original definition was at %s:%d\n",
223 type->name, type->loc_info.input_name, type->loc_info.line_number);
224 type->name = name;
225 type->namespace = namespace;
226 return type;
229 type_t *type_new_array(const char *name, const decl_spec_t *element, int declptr,
230 unsigned int dim, expr_t *size_is, expr_t *length_is)
232 type_t *t = make_type(TYPE_ARRAY);
233 if (name) t->name = xstrdup(name);
234 t->details.array.declptr = declptr;
235 t->details.array.length_is = length_is;
236 if (size_is)
237 t->details.array.size_is = size_is;
238 else
239 t->details.array.dim = dim;
240 if (element)
241 t->details.array.elem = *element;
242 return t;
245 type_t *type_new_basic(enum type_basic_type basic_type)
247 type_t *t = make_type(TYPE_BASIC);
248 t->details.basic.type = basic_type;
249 t->details.basic.sign = 0;
250 return t;
253 type_t *type_new_int(enum type_basic_type basic_type, int sign)
255 static type_t *int_types[TYPE_BASIC_INT_MAX+1][3];
257 assert(basic_type <= TYPE_BASIC_INT_MAX);
259 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
260 if (!int_types[basic_type][sign + 1])
262 int_types[basic_type][sign + 1] = type_new_basic(basic_type);
263 int_types[basic_type][sign + 1]->details.basic.sign = sign;
265 return int_types[basic_type][sign + 1];
268 type_t *type_new_void(void)
270 static type_t *void_type = NULL;
271 if (!void_type)
272 void_type = make_type(TYPE_VOID);
273 return void_type;
276 type_t *type_new_enum(const char *name, struct namespace *namespace, int defined, var_list_t *enums)
278 type_t *t = NULL;
280 if (name)
281 t = find_type(name, namespace,tsENUM);
283 if (!t)
285 t = make_type(TYPE_ENUM);
286 t->name = name;
287 t->namespace = namespace;
288 if (name)
289 reg_type(t, name, namespace, tsENUM);
292 if (!t->defined && defined)
294 t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
295 t->details.enumeration->enums = enums;
296 t->defined = TRUE;
298 else if (defined)
299 error_loc("redefinition of enum %s\n", name);
301 return t;
304 type_t *type_new_struct(char *name, struct namespace *namespace, int defined, var_list_t *fields)
306 type_t *t = NULL;
308 if (name)
309 t = find_type(name, namespace, tsSTRUCT);
311 if (!t)
313 t = make_type(TYPE_STRUCT);
314 t->name = name;
315 t->namespace = namespace;
316 if (name)
317 reg_type(t, name, namespace, tsSTRUCT);
320 if (!t->defined && defined)
322 t->details.structure = xmalloc(sizeof(*t->details.structure));
323 t->details.structure->fields = fields;
324 t->defined = TRUE;
326 else if (defined)
327 error_loc("redefinition of struct %s\n", name);
329 return t;
332 type_t *type_new_nonencapsulated_union(const char *name, int defined, var_list_t *fields)
334 type_t *t = NULL;
336 if (name)
337 t = find_type(name, NULL, tsUNION);
339 if (!t)
341 t = make_type(TYPE_UNION);
342 t->name = name;
343 if (name)
344 reg_type(t, name, NULL, tsUNION);
347 if (!t->defined && defined)
349 t->details.structure = xmalloc(sizeof(*t->details.structure));
350 t->details.structure->fields = fields;
351 t->defined = TRUE;
353 else if (defined)
354 error_loc("redefinition of union %s\n", name);
356 return t;
359 type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
361 type_t *t = NULL;
363 if (name)
364 t = find_type(name, NULL, tsUNION);
366 if (!t)
368 t = make_type(TYPE_ENCAPSULATED_UNION);
369 t->name = name;
370 if (name)
371 reg_type(t, name, NULL, tsUNION);
373 t->type_type = TYPE_ENCAPSULATED_UNION;
375 if (!t->defined)
377 if (!union_field)
378 union_field = make_var(xstrdup("tagged_union"));
379 union_field->declspec.type = type_new_nonencapsulated_union(gen_name(), TRUE, cases);
381 t->details.structure = xmalloc(sizeof(*t->details.structure));
382 t->details.structure->fields = append_var(NULL, switch_field);
383 t->details.structure->fields = append_var(t->details.structure->fields, union_field);
384 t->defined = TRUE;
386 else
387 error_loc("redefinition of union %s\n", name);
389 return t;
392 static int is_valid_bitfield_type(const type_t *type)
394 switch (type_get_type(type))
396 case TYPE_ENUM:
397 return TRUE;
398 case TYPE_BASIC:
399 switch (type_basic_get_type(type))
401 case TYPE_BASIC_INT8:
402 case TYPE_BASIC_INT16:
403 case TYPE_BASIC_INT32:
404 case TYPE_BASIC_INT64:
405 case TYPE_BASIC_INT:
406 case TYPE_BASIC_INT3264:
407 case TYPE_BASIC_LONG:
408 case TYPE_BASIC_CHAR:
409 case TYPE_BASIC_HYPER:
410 case TYPE_BASIC_BYTE:
411 case TYPE_BASIC_WCHAR:
412 case TYPE_BASIC_ERROR_STATUS_T:
413 return TRUE;
414 case TYPE_BASIC_FLOAT:
415 case TYPE_BASIC_DOUBLE:
416 case TYPE_BASIC_HANDLE:
417 return FALSE;
419 return FALSE;
420 default:
421 return FALSE;
425 type_t *type_new_bitfield(type_t *field, const expr_t *bits)
427 type_t *t;
429 if (!is_valid_bitfield_type(field))
430 error_loc("bit-field has invalid type\n");
432 if (bits->cval < 0)
433 error_loc("negative width for bit-field\n");
435 /* FIXME: validate bits->cval <= memsize(field) * 8 */
437 t = make_type(TYPE_BITFIELD);
438 t->details.bitfield.field = field;
439 t->details.bitfield.bits = bits;
440 return t;
443 static unsigned int compute_method_indexes(type_t *iface)
445 unsigned int idx;
446 statement_t *stmt;
448 if (!iface->details.iface)
449 return 0;
451 if (type_iface_get_inherit(iface))
452 idx = compute_method_indexes(type_iface_get_inherit(iface));
453 else
454 idx = 0;
456 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
458 var_t *func = stmt->u.var;
459 if (!is_callas(func->attrs))
460 func->func_idx = idx++;
463 return idx;
466 void type_interface_define(type_t *iface, type_t *inherit, statement_list_t *stmts)
468 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
469 iface->details.iface->disp_props = NULL;
470 iface->details.iface->disp_methods = NULL;
471 iface->details.iface->stmts = stmts;
472 iface->details.iface->inherit = inherit;
473 iface->details.iface->disp_inherit = NULL;
474 iface->details.iface->async_iface = NULL;
475 iface->defined = TRUE;
476 compute_method_indexes(iface);
479 void type_dispinterface_define(type_t *iface, var_list_t *props, var_list_t *methods)
481 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
482 iface->details.iface->disp_props = props;
483 iface->details.iface->disp_methods = methods;
484 iface->details.iface->stmts = NULL;
485 iface->details.iface->inherit = find_type("IDispatch", NULL, 0);
486 if (!iface->details.iface->inherit) error_loc("IDispatch is undefined\n");
487 iface->details.iface->disp_inherit = NULL;
488 iface->details.iface->async_iface = NULL;
489 iface->defined = TRUE;
490 compute_method_indexes(iface);
493 void type_dispinterface_define_from_iface(type_t *dispiface, type_t *iface)
495 dispiface->details.iface = xmalloc(sizeof(*dispiface->details.iface));
496 dispiface->details.iface->disp_props = NULL;
497 dispiface->details.iface->disp_methods = NULL;
498 dispiface->details.iface->stmts = NULL;
499 dispiface->details.iface->inherit = find_type("IDispatch", NULL, 0);
500 if (!dispiface->details.iface->inherit) error_loc("IDispatch is undefined\n");
501 dispiface->details.iface->disp_inherit = iface;
502 dispiface->details.iface->async_iface = NULL;
503 dispiface->defined = TRUE;
504 compute_method_indexes(dispiface);
507 void type_module_define(type_t *module, statement_list_t *stmts)
509 if (module->details.module) error_loc("multiple definition error\n");
510 module->details.module = xmalloc(sizeof(*module->details.module));
511 module->details.module->stmts = stmts;
512 module->defined = TRUE;
515 type_t *type_coclass_define(type_t *coclass, ifref_list_t *ifaces)
517 coclass->details.coclass.ifaces = ifaces;
518 coclass->defined = TRUE;
519 return coclass;
522 type_t *type_runtimeclass_define(type_t *runtimeclass, ifref_list_t *ifaces)
524 runtimeclass->details.runtimeclass.ifaces = ifaces;
525 runtimeclass->defined = TRUE;
526 if (!type_runtimeclass_get_default_iface(runtimeclass))
527 error_loc("missing default interface on runtimeclass %s\n", runtimeclass->name);
528 return runtimeclass;
531 int type_is_equal(const type_t *type1, const type_t *type2)
533 if (type_get_type_detect_alias(type1) != type_get_type_detect_alias(type2))
534 return FALSE;
536 if (type1->name && type2->name)
537 return !strcmp(type1->name, type2->name);
538 else if ((!type1->name && type2->name) || (type1->name && !type2->name))
539 return FALSE;
541 /* FIXME: do deep inspection of types to determine if they are equal */
543 return FALSE;