windows.media.speech: Add IIterator<Inspectable*>.
[wine.git] / tools / widl / typetree.h
blob8f5e1ebdac53f16e800f6d5e4ca357f22489c473
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 "widltypes.h"
22 #include <assert.h>
23 #include <stdio.h>
25 #include "utils.h"
27 #ifndef WIDL_TYPE_TREE_H
28 #define WIDL_TYPE_TREE_H
30 enum name_type {
31 NAME_DEFAULT,
32 NAME_C
35 attr_list_t *check_apicontract_attrs(const char *name, attr_list_t *attrs);
36 attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs);
37 attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs);
38 attr_list_t *check_interface_attrs(const char *name, attr_list_t *attrs);
39 attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs);
40 attr_list_t *check_runtimeclass_attrs(const char *name, attr_list_t *attrs);
42 type_t *find_parameterized_type(type_t *type, typeref_list_t *params);
44 type_t *type_new_function(var_list_t *args);
45 type_t *type_new_pointer(type_t *ref);
46 type_t *type_new_alias(const decl_spec_t *t, const char *name);
47 type_t *type_module_declare(char *name);
48 type_t *type_new_array(const char *name, const decl_spec_t *element, int declptr,
49 unsigned int dim, expr_t *size_is, expr_t *length_is);
50 type_t *type_new_basic(enum type_basic_type basic_type);
51 type_t *type_new_int(enum type_basic_type basic_type, int sign);
52 type_t *type_new_void(void);
53 type_t *type_coclass_declare(char *name);
54 type_t *type_new_enum(const char *name, struct namespace *namespace, int defined, var_list_t *enums);
55 type_t *type_new_struct(char *name, struct namespace *namespace, int defined, var_list_t *fields);
56 type_t *type_new_nonencapsulated_union(const char *name, int defined, var_list_t *fields);
57 type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases);
58 type_t *type_new_bitfield(type_t *field_type, const expr_t *bits);
59 type_t *type_runtimeclass_declare(char *name, struct namespace *namespace);
60 type_t *type_interface_declare(char *name, struct namespace *namespace);
61 type_t *type_interface_define(type_t *iface, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires);
62 type_t *type_dispinterface_declare(char *name);
63 type_t *type_dispinterface_define(type_t *iface, attr_list_t *attrs, var_list_t *props, var_list_t *methods);
64 type_t *type_dispinterface_define_from_iface(type_t *dispiface, attr_list_t *attrs, type_t *iface);
65 type_t *type_module_define(type_t* module, attr_list_t *attrs, statement_list_t *stmts);
66 type_t *type_coclass_define(type_t *coclass, attr_list_t *attrs, typeref_list_t *ifaces);
67 type_t *type_runtimeclass_define(type_t *runtimeclass, attr_list_t *attrs, typeref_list_t *ifaces);
68 type_t *type_apicontract_declare(char *name, struct namespace *namespace);
69 type_t *type_apicontract_define(type_t *apicontract, attr_list_t *attrs);
70 type_t *type_delegate_declare(char *name, struct namespace *namespace);
71 type_t *type_delegate_define(type_t *delegate, attr_list_t *attrs, statement_list_t *stmts);
72 type_t *type_parameterized_interface_declare(char *name, struct namespace *namespace, typeref_list_t *params);
73 type_t *type_parameterized_interface_define(type_t *type, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires);
74 type_t *type_parameterized_delegate_declare(char *name, struct namespace *namespace, typeref_list_t *params);
75 type_t *type_parameterized_delegate_define(type_t *type, attr_list_t *attrs, statement_list_t *stmts);
76 type_t *type_parameterized_type_specialize_partial(type_t *type, typeref_list_t *params);
77 type_t *type_parameterized_type_specialize_declare(type_t *type, typeref_list_t *params);
78 type_t *type_parameterized_type_specialize_define(type_t *type);
79 int type_is_equal(const type_t *type1, const type_t *type2);
80 const char *type_get_decl_name(const type_t *type, enum name_type name_type);
81 const char *type_get_name(const type_t *type, enum name_type name_type);
82 char *gen_name(void);
83 extern int is_attr(const attr_list_t *list, enum attr_type t);
85 typeref_t *make_typeref(type_t *type);
86 typeref_list_t *append_typeref(typeref_list_t *list, typeref_t *ref);
87 attr_t *make_attrp(enum attr_type type, void *val);
88 attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
90 /* FIXME: shouldn't need to export this */
91 type_t *duptype(type_t *t, int dupname);
93 /* un-alias the type until finding the non-alias type */
94 static inline type_t *type_get_real_type(const type_t *type)
96 if (type->type_type == TYPE_ALIAS)
97 return type_get_real_type(type->details.alias.aliasee.type);
98 else
99 return (type_t *)type;
102 static inline type_t *type_parameterized_type_get_real_type(const type_t *type)
104 if (type->type_type == TYPE_PARAMETERIZED_TYPE)
105 return type_parameterized_type_get_real_type(type->details.parameterized.type);
106 else
107 return (type_t *)type;
110 static inline enum type_type type_get_type(const type_t *type)
112 return type_get_type_detect_alias(type_get_real_type(type));
115 static inline enum type_basic_type type_basic_get_type(const type_t *type)
117 type = type_get_real_type(type);
118 assert(type_get_type(type) == TYPE_BASIC);
119 return type->details.basic.type;
122 static inline int type_basic_get_sign(const type_t *type)
124 type = type_get_real_type(type);
125 assert(type_get_type(type) == TYPE_BASIC);
126 return type->details.basic.sign;
129 static inline var_list_t *type_struct_get_fields(const type_t *type)
131 type = type_get_real_type(type);
132 assert(type_get_type(type) == TYPE_STRUCT);
133 return type->details.structure->fields;
136 static inline var_list_t *type_function_get_args(const type_t *type)
138 type = type_get_real_type(type);
139 assert(type_get_type(type) == TYPE_FUNCTION);
140 return type->details.function->args;
143 static inline var_t *type_function_get_retval(const type_t *type)
145 type = type_get_real_type(type);
146 assert(type_get_type(type) == TYPE_FUNCTION);
147 return type->details.function->retval;
150 static inline const decl_spec_t *type_function_get_ret(const type_t *type)
152 return &type_function_get_retval(type)->declspec;
155 static inline type_t *type_function_get_rettype(const type_t *type)
157 return type_function_get_retval(type)->declspec.type;
160 static inline var_list_t *type_enum_get_values(const type_t *type)
162 type = type_get_real_type(type);
163 assert(type_get_type(type) == TYPE_ENUM);
164 return type->details.enumeration->enums;
167 static inline var_t *type_union_get_switch_value(const type_t *type)
169 type = type_get_real_type(type);
170 assert(type_get_type(type) == TYPE_ENCAPSULATED_UNION);
171 return LIST_ENTRY(list_head(type->details.structure->fields), var_t, entry);
174 static inline var_list_t *type_encapsulated_union_get_fields(const type_t *type)
176 type = type_get_real_type(type);
177 assert(type_get_type(type) == TYPE_ENCAPSULATED_UNION);
178 return type->details.structure->fields;
181 static inline var_list_t *type_union_get_cases(const type_t *type)
183 enum type_type type_type;
185 type = type_get_real_type(type);
186 type_type = type_get_type(type);
188 assert(type_type == TYPE_UNION || type_type == TYPE_ENCAPSULATED_UNION);
189 if (type_type == TYPE_ENCAPSULATED_UNION)
191 const var_t *uv = LIST_ENTRY(list_tail(type->details.structure->fields), const var_t, entry);
192 return uv->declspec.type->details.structure->fields;
194 else
195 return type->details.structure->fields;
198 static inline statement_list_t *type_iface_get_stmts(const type_t *type)
200 type = type_get_real_type(type);
201 assert(type_get_type(type) == TYPE_INTERFACE);
202 return type->details.iface->stmts;
205 static inline type_t *type_iface_get_inherit(const type_t *type)
207 type = type_get_real_type(type);
208 assert(type_get_type(type) == TYPE_INTERFACE);
209 return type->details.iface->inherit;
212 static inline typeref_list_t *type_iface_get_requires(const type_t *type)
214 type = type_get_real_type(type);
215 assert(type_get_type(type) == TYPE_INTERFACE);
216 return type->details.iface->requires;
219 static inline type_t *type_iface_get_async_iface(const type_t *type)
221 type = type_get_real_type(type);
222 assert(type_get_type(type) == TYPE_INTERFACE);
223 return type->details.iface->async_iface;
226 static inline var_list_t *type_dispiface_get_props(const type_t *type)
228 type = type_get_real_type(type);
229 assert(type_get_type(type) == TYPE_INTERFACE);
230 return type->details.iface->disp_props;
233 static inline var_list_t *type_dispiface_get_methods(const type_t *type)
235 type = type_get_real_type(type);
236 assert(type_get_type(type) == TYPE_INTERFACE);
237 return type->details.iface->disp_methods;
240 static inline type_t *type_dispiface_get_inherit(const type_t *type)
242 type = type_get_real_type(type);
243 assert(type_get_type(type) == TYPE_INTERFACE);
244 return type->details.iface->disp_inherit;
247 static inline int type_is_defined(const type_t *type)
249 return type->defined;
252 static inline int type_is_complete(const type_t *type)
254 switch (type_get_type_detect_alias(type))
256 case TYPE_FUNCTION:
257 return (type->details.function != NULL);
258 case TYPE_INTERFACE:
259 return (type->details.iface != NULL);
260 case TYPE_ENUM:
261 return (type->details.enumeration != NULL);
262 case TYPE_UNION:
263 case TYPE_ENCAPSULATED_UNION:
264 case TYPE_STRUCT:
265 return (type->details.structure != NULL);
266 case TYPE_VOID:
267 case TYPE_BASIC:
268 case TYPE_ALIAS:
269 case TYPE_MODULE:
270 case TYPE_COCLASS:
271 case TYPE_POINTER:
272 case TYPE_ARRAY:
273 case TYPE_BITFIELD:
274 case TYPE_RUNTIMECLASS:
275 case TYPE_DELEGATE:
276 return TRUE;
277 case TYPE_APICONTRACT:
278 case TYPE_PARAMETERIZED_TYPE:
279 case TYPE_PARAMETER:
280 assert(0);
281 break;
283 return FALSE;
286 static inline int type_array_has_conformance(const type_t *type)
288 type = type_get_real_type(type);
289 assert(type_get_type(type) == TYPE_ARRAY);
290 return (type->details.array.size_is != NULL);
293 static inline int type_array_has_variance(const type_t *type)
295 type = type_get_real_type(type);
296 assert(type_get_type(type) == TYPE_ARRAY);
297 return (type->details.array.length_is != NULL);
300 static inline unsigned int type_array_get_dim(const type_t *type)
302 type = type_get_real_type(type);
303 assert(type_get_type(type) == TYPE_ARRAY);
304 return type->details.array.dim;
307 static inline expr_t *type_array_get_conformance(const type_t *type)
309 type = type_get_real_type(type);
310 assert(type_get_type(type) == TYPE_ARRAY);
311 return type->details.array.size_is;
314 static inline expr_t *type_array_get_variance(const type_t *type)
316 type = type_get_real_type(type);
317 assert(type_get_type(type) == TYPE_ARRAY);
318 return type->details.array.length_is;
321 static inline unsigned short type_array_get_ptr_tfsoff(const type_t *type)
323 type = type_get_real_type(type);
324 assert(type_get_type(type) == TYPE_ARRAY);
325 return type->details.array.ptr_tfsoff;
328 static inline void type_array_set_ptr_tfsoff(type_t *type, unsigned short ptr_tfsoff)
330 type = type_get_real_type(type);
331 assert(type_get_type(type) == TYPE_ARRAY);
332 type->details.array.ptr_tfsoff = ptr_tfsoff;
335 static inline const decl_spec_t *type_array_get_element(const type_t *type)
337 type = type_get_real_type(type);
338 assert(type_get_type(type) == TYPE_ARRAY);
339 return &type->details.array.elem;
342 static inline type_t *type_array_get_element_type(const type_t *type)
344 return type_array_get_element(type)->type;
347 static inline int type_array_is_decl_as_ptr(const type_t *type)
349 type = type_get_real_type(type);
350 assert(type_get_type(type) == TYPE_ARRAY);
351 return type->details.array.declptr;
354 static inline int type_is_alias(const type_t *type)
356 return type->type_type == TYPE_ALIAS;
359 static inline const decl_spec_t *type_alias_get_aliasee(const type_t *type)
361 assert(type_is_alias(type));
362 return &type->details.alias.aliasee;
365 static inline type_t *type_alias_get_aliasee_type(const type_t *type)
367 assert(type_is_alias(type));
368 return type->details.alias.aliasee.type;
371 static inline typeref_list_t *type_coclass_get_ifaces(const type_t *type)
373 type = type_get_real_type(type);
374 assert(type_get_type(type) == TYPE_COCLASS);
375 return type->details.coclass.ifaces;
378 static inline typeref_list_t *type_runtimeclass_get_ifaces(const type_t *type)
380 type = type_get_real_type(type);
381 assert(type_get_type(type) == TYPE_RUNTIMECLASS);
382 return type->details.runtimeclass.ifaces;
385 static inline type_t *type_runtimeclass_get_default_iface(const type_t *type, int check)
387 typeref_list_t *ifaces = type_runtimeclass_get_ifaces(type);
388 typeref_t *ref;
390 if (ifaces) LIST_FOR_EACH_ENTRY(ref, ifaces, typeref_t, entry)
391 if (is_attr(ref->attrs, ATTR_DEFAULT))
392 return ref->type;
394 if (!check) return NULL;
395 error_loc_info(&type->loc_info, "runtimeclass %s needs a default interface\n", type->name);
398 static inline type_t *type_delegate_get_iface(const type_t *type)
400 type = type_get_real_type(type);
401 assert(type_get_type(type) == TYPE_DELEGATE);
402 return type->details.delegate.iface;
405 static inline const decl_spec_t *type_pointer_get_ref(const type_t *type)
407 type = type_get_real_type(type);
408 assert(type_get_type(type) == TYPE_POINTER);
409 return &type->details.pointer.ref;
412 static inline type_t *type_pointer_get_ref_type(const type_t *type)
414 return type_pointer_get_ref(type)->type;
417 static inline type_t *type_pointer_get_root_type(type_t *type)
419 for (; type && type->type_type == TYPE_POINTER; type = type_pointer_get_ref_type(type)) {}
420 return type;
423 static inline type_t *type_bitfield_get_field(const type_t *type)
425 type = type_get_real_type(type);
426 assert(type_get_type(type) == TYPE_BITFIELD);
427 return type->details.bitfield.field;
430 static inline const expr_t *type_bitfield_get_bits(const type_t *type)
432 type = type_get_real_type(type);
433 assert(type_get_type(type) == TYPE_BITFIELD);
434 return type->details.bitfield.bits;
437 #endif /* WIDL_TYPE_TREE_H */