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
33 type_t
*duptype(type_t
*t
, int dupname
)
35 type_t
*d
= alloc_type();
38 if (dupname
&& t
->name
)
39 d
->name
= xstrdup(t
->name
);
44 type_t
*make_type(enum type_type type
)
46 type_t
*t
= alloc_type();
52 memset(&t
->details
, 0, sizeof(t
->details
));
53 t
->typestring_offset
= 0;
55 t
->ignore
= (parse_only
!= 0);
58 t
->user_types_registered
= FALSE
;
62 init_loc_info(&t
->loc_info
);
66 static const var_t
*find_arg(const var_list_t
*args
, const char *name
)
70 if (args
) LIST_FOR_EACH_ENTRY(arg
, args
, const var_t
, entry
)
72 if (arg
->name
&& !strcmp(name
, arg
->name
))
79 const char *type_get_name(const type_t
*type
, enum name_type name_type
)
85 return type
->c_name
? type
->c_name
: type
->name
;
92 static size_t append_namespace(char **buf
, size_t *len
, size_t pos
, struct namespace *namespace, const char *separator
, const char *abi_prefix
)
94 int nested
= namespace && !is_global_namespace(namespace);
95 const char *name
= nested
? namespace->name
: abi_prefix
;
98 if (nested
) n
+= append_namespace(buf
, len
, pos
+ n
, namespace->parent
, separator
, abi_prefix
);
99 n
+= strappend(buf
, len
, pos
+ n
, "%s%s", name
, separator
);
103 char *format_namespace(struct namespace *namespace, const char *prefix
, const char *separator
, const char *suffix
, const char *abi_prefix
)
105 size_t len
= 0, pos
= 0;
107 pos
+= strappend(&buf
, &len
, pos
, "%s", prefix
);
108 pos
+= append_namespace(&buf
, &len
, pos
, namespace, separator
, abi_prefix
);
109 pos
+= strappend(&buf
, &len
, pos
, "%s", suffix
);
113 type_t
*type_new_function(var_list_t
*args
)
121 arg
= LIST_ENTRY(list_head(args
), var_t
, entry
);
122 if (list_count(args
) == 1 && !arg
->name
&& arg
->declspec
.type
&& type_get_type(arg
->declspec
.type
) == TYPE_VOID
)
124 list_remove(&arg
->entry
);
130 if (args
) LIST_FOR_EACH_ENTRY(arg
, args
, var_t
, entry
)
132 if (arg
->declspec
.type
&& type_get_type(arg
->declspec
.type
) == TYPE_VOID
)
133 error_loc("argument '%s' has void type\n", arg
->name
);
137 error_loc("too many unnamed arguments\n");
144 name
[0] = i
> 26 ? 'a' + i
/ 26 : 'a' + i
;
145 name
[1] = i
> 26 ? 'a' + i
% 26 : 0;
147 unique
= !find_arg(args
, name
);
149 arg
->name
= xstrdup(name
);
156 t
= make_type(TYPE_FUNCTION
);
157 t
->details
.function
= xmalloc(sizeof(*t
->details
.function
));
158 t
->details
.function
->args
= args
;
159 t
->details
.function
->retval
= make_var(xstrdup("_RetVal"));
163 type_t
*type_new_pointer(type_t
*ref
)
165 type_t
*t
= make_type(TYPE_POINTER
);
166 t
->details
.pointer
.ref
.type
= ref
;
170 type_t
*type_new_alias(const decl_spec_t
*t
, const char *name
)
172 type_t
*a
= make_type(TYPE_ALIAS
);
174 a
->name
= xstrdup(name
);
176 a
->details
.alias
.aliasee
= *t
;
177 init_loc_info(&a
->loc_info
);
182 type_t
*type_new_array(const char *name
, const decl_spec_t
*element
, int declptr
,
183 unsigned int dim
, expr_t
*size_is
, expr_t
*length_is
)
185 type_t
*t
= make_type(TYPE_ARRAY
);
186 if (name
) t
->name
= xstrdup(name
);
187 t
->details
.array
.declptr
= declptr
;
188 t
->details
.array
.length_is
= length_is
;
190 t
->details
.array
.size_is
= size_is
;
192 t
->details
.array
.dim
= dim
;
194 t
->details
.array
.elem
= *element
;
198 type_t
*type_new_basic(enum type_basic_type basic_type
)
200 type_t
*t
= make_type(TYPE_BASIC
);
201 t
->details
.basic
.type
= basic_type
;
202 t
->details
.basic
.sign
= 0;
206 type_t
*type_new_int(enum type_basic_type basic_type
, int sign
)
208 static type_t
*int_types
[TYPE_BASIC_INT_MAX
+1][3];
210 assert(basic_type
<= TYPE_BASIC_INT_MAX
);
212 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
213 if (!int_types
[basic_type
][sign
+ 1])
215 int_types
[basic_type
][sign
+ 1] = type_new_basic(basic_type
);
216 int_types
[basic_type
][sign
+ 1]->details
.basic
.sign
= sign
;
218 return int_types
[basic_type
][sign
+ 1];
221 type_t
*type_new_void(void)
223 static type_t
*void_type
= NULL
;
225 void_type
= make_type(TYPE_VOID
);
229 type_t
*type_new_enum(const char *name
, struct namespace *namespace, int defined
, var_list_t
*enums
)
234 t
= find_type(name
, namespace,tsENUM
);
238 t
= make_type(TYPE_ENUM
);
240 t
->namespace = namespace;
242 reg_type(t
, name
, namespace, tsENUM
);
245 if (!t
->defined
&& defined
)
247 t
->details
.enumeration
= xmalloc(sizeof(*t
->details
.enumeration
));
248 t
->details
.enumeration
->enums
= enums
;
252 error_loc("redefinition of enum %s\n", name
);
257 type_t
*type_new_struct(char *name
, struct namespace *namespace, int defined
, var_list_t
*fields
)
262 t
= find_type(name
, namespace, tsSTRUCT
);
266 t
= make_type(TYPE_STRUCT
);
268 t
->namespace = namespace;
270 reg_type(t
, name
, namespace, tsSTRUCT
);
273 if (!t
->defined
&& defined
)
275 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
276 t
->details
.structure
->fields
= fields
;
280 error_loc("redefinition of struct %s\n", name
);
285 type_t
*type_new_nonencapsulated_union(const char *name
, int defined
, var_list_t
*fields
)
290 t
= find_type(name
, NULL
, tsUNION
);
294 t
= make_type(TYPE_UNION
);
297 reg_type(t
, name
, NULL
, tsUNION
);
300 if (!t
->defined
&& defined
)
302 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
303 t
->details
.structure
->fields
= fields
;
307 error_loc("redefinition of union %s\n", name
);
312 type_t
*type_new_encapsulated_union(char *name
, var_t
*switch_field
, var_t
*union_field
, var_list_t
*cases
)
317 t
= find_type(name
, NULL
, tsUNION
);
321 t
= make_type(TYPE_ENCAPSULATED_UNION
);
324 reg_type(t
, name
, NULL
, tsUNION
);
326 t
->type_type
= TYPE_ENCAPSULATED_UNION
;
331 union_field
= make_var(xstrdup("tagged_union"));
332 union_field
->declspec
.type
= type_new_nonencapsulated_union(gen_name(), TRUE
, cases
);
334 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
335 t
->details
.structure
->fields
= append_var(NULL
, switch_field
);
336 t
->details
.structure
->fields
= append_var(t
->details
.structure
->fields
, union_field
);
340 error_loc("redefinition of union %s\n", name
);
345 static int is_valid_bitfield_type(const type_t
*type
)
347 switch (type_get_type(type
))
352 switch (type_basic_get_type(type
))
354 case TYPE_BASIC_INT8
:
355 case TYPE_BASIC_INT16
:
356 case TYPE_BASIC_INT32
:
357 case TYPE_BASIC_INT64
:
359 case TYPE_BASIC_INT3264
:
360 case TYPE_BASIC_LONG
:
361 case TYPE_BASIC_CHAR
:
362 case TYPE_BASIC_HYPER
:
363 case TYPE_BASIC_BYTE
:
364 case TYPE_BASIC_WCHAR
:
365 case TYPE_BASIC_ERROR_STATUS_T
:
367 case TYPE_BASIC_FLOAT
:
368 case TYPE_BASIC_DOUBLE
:
369 case TYPE_BASIC_HANDLE
:
378 type_t
*type_new_bitfield(type_t
*field
, const expr_t
*bits
)
382 if (!is_valid_bitfield_type(field
))
383 error_loc("bit-field has invalid type\n");
386 error_loc("negative width for bit-field\n");
388 /* FIXME: validate bits->cval <= memsize(field) * 8 */
390 t
= make_type(TYPE_BITFIELD
);
391 t
->details
.bitfield
.field
= field
;
392 t
->details
.bitfield
.bits
= bits
;
396 static unsigned int compute_method_indexes(type_t
*iface
)
401 if (!iface
->details
.iface
)
404 if (type_iface_get_inherit(iface
))
405 idx
= compute_method_indexes(type_iface_get_inherit(iface
));
409 STATEMENTS_FOR_EACH_FUNC( stmt
, type_iface_get_stmts(iface
) )
411 var_t
*func
= stmt
->u
.var
;
412 if (!is_callas(func
->attrs
))
413 func
->func_idx
= idx
++;
419 type_t
*type_interface_declare(char *name
, struct namespace *namespace)
421 type_t
*type
= get_type(TYPE_INTERFACE
, name
, namespace, 0);
422 if (type_get_type_detect_alias(type
) != TYPE_INTERFACE
)
423 error_loc("interface %s previously not declared an interface at %s:%d\n",
424 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
428 type_t
*type_interface_define(type_t
*iface
, attr_list_t
*attrs
, type_t
*inherit
, statement_list_t
*stmts
, ifref_list_t
*requires
)
431 error_loc("interface %s already defined at %s:%d\n",
432 iface
->name
, iface
->loc_info
.input_name
, iface
->loc_info
.line_number
);
433 if (iface
== inherit
)
434 error_loc("interface %s can't inherit from itself\n",
436 iface
->attrs
= check_interface_attrs(iface
->name
, attrs
);
437 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
438 iface
->details
.iface
->disp_props
= NULL
;
439 iface
->details
.iface
->disp_methods
= NULL
;
440 iface
->details
.iface
->stmts
= stmts
;
441 iface
->details
.iface
->inherit
= inherit
;
442 iface
->details
.iface
->disp_inherit
= NULL
;
443 iface
->details
.iface
->async_iface
= NULL
;
444 iface
->details
.iface
->requires
= requires
;
445 iface
->defined
= TRUE
;
446 compute_method_indexes(iface
);
450 type_t
*type_dispinterface_declare(char *name
)
452 type_t
*type
= get_type(TYPE_INTERFACE
, name
, NULL
, 0);
453 if (type_get_type_detect_alias(type
) != TYPE_INTERFACE
)
454 error_loc("dispinterface %s previously not declared a dispinterface at %s:%d\n",
455 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
459 type_t
*type_dispinterface_define(type_t
*iface
, attr_list_t
*attrs
, var_list_t
*props
, var_list_t
*methods
)
462 error_loc("dispinterface %s already defined at %s:%d\n",
463 iface
->name
, iface
->loc_info
.input_name
, iface
->loc_info
.line_number
);
464 iface
->attrs
= check_dispiface_attrs(iface
->name
, attrs
);
465 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
466 iface
->details
.iface
->disp_props
= props
;
467 iface
->details
.iface
->disp_methods
= methods
;
468 iface
->details
.iface
->stmts
= NULL
;
469 iface
->details
.iface
->inherit
= find_type("IDispatch", NULL
, 0);
470 if (!iface
->details
.iface
->inherit
) error_loc("IDispatch is undefined\n");
471 iface
->details
.iface
->disp_inherit
= NULL
;
472 iface
->details
.iface
->async_iface
= NULL
;
473 iface
->details
.iface
->requires
= NULL
;
474 iface
->defined
= TRUE
;
475 compute_method_indexes(iface
);
479 type_t
*type_dispinterface_define_from_iface(type_t
*dispiface
, attr_list_t
*attrs
, type_t
*iface
)
481 if (dispiface
->defined
)
482 error_loc("dispinterface %s already defined at %s:%d\n",
483 dispiface
->name
, dispiface
->loc_info
.input_name
, dispiface
->loc_info
.line_number
);
484 dispiface
->attrs
= check_dispiface_attrs(dispiface
->name
, attrs
);
485 dispiface
->details
.iface
= xmalloc(sizeof(*dispiface
->details
.iface
));
486 dispiface
->details
.iface
->disp_props
= NULL
;
487 dispiface
->details
.iface
->disp_methods
= NULL
;
488 dispiface
->details
.iface
->stmts
= NULL
;
489 dispiface
->details
.iface
->inherit
= find_type("IDispatch", NULL
, 0);
490 if (!dispiface
->details
.iface
->inherit
) error_loc("IDispatch is undefined\n");
491 dispiface
->details
.iface
->disp_inherit
= iface
;
492 dispiface
->details
.iface
->async_iface
= NULL
;
493 dispiface
->details
.iface
->requires
= NULL
;
494 dispiface
->defined
= TRUE
;
495 compute_method_indexes(dispiface
);
499 type_t
*type_module_declare(char *name
)
501 type_t
*type
= get_type(TYPE_MODULE
, name
, NULL
, 0);
502 if (type_get_type_detect_alias(type
) != TYPE_MODULE
)
503 error_loc("module %s previously not declared a module at %s:%d\n",
504 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
508 type_t
*type_module_define(type_t
* module
, attr_list_t
*attrs
, statement_list_t
*stmts
)
511 error_loc("module %s already defined at %s:%d\n",
512 module
->name
, module
->loc_info
.input_name
, module
->loc_info
.line_number
);
513 module
->attrs
= check_module_attrs(module
->name
, attrs
);
514 module
->details
.module
= xmalloc(sizeof(*module
->details
.module
));
515 module
->details
.module
->stmts
= stmts
;
516 module
->defined
= TRUE
;
520 type_t
*type_coclass_declare(char *name
)
522 type_t
*type
= get_type(TYPE_COCLASS
, name
, NULL
, 0);
523 if (type_get_type_detect_alias(type
) != TYPE_COCLASS
)
524 error_loc("coclass %s previously not declared a coclass at %s:%d\n",
525 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
529 type_t
*type_coclass_define(type_t
*coclass
, attr_list_t
*attrs
, ifref_list_t
*ifaces
)
531 if (coclass
->defined
)
532 error_loc("coclass %s already defined at %s:%d\n",
533 coclass
->name
, coclass
->loc_info
.input_name
, coclass
->loc_info
.line_number
);
534 coclass
->attrs
= check_coclass_attrs(coclass
->name
, attrs
);
535 coclass
->details
.coclass
.ifaces
= ifaces
;
536 coclass
->defined
= TRUE
;
540 type_t
*type_runtimeclass_declare(char *name
, struct namespace *namespace)
542 type_t
*type
= get_type(TYPE_RUNTIMECLASS
, name
, namespace, 0);
543 if (type_get_type_detect_alias(type
) != TYPE_RUNTIMECLASS
)
544 error_loc("runtimeclass %s previously not declared a runtimeclass at %s:%d\n",
545 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
549 type_t
*type_runtimeclass_define(type_t
*runtimeclass
, attr_list_t
*attrs
, ifref_list_t
*ifaces
)
551 ifref_t
*ifref
, *required
, *tmp
;
552 ifref_list_t
*requires
;
554 if (runtimeclass
->defined
)
555 error_loc("runtimeclass %s already defined at %s:%d\n",
556 runtimeclass
->name
, runtimeclass
->loc_info
.input_name
, runtimeclass
->loc_info
.line_number
);
557 runtimeclass
->attrs
= check_runtimeclass_attrs(runtimeclass
->name
, attrs
);
558 runtimeclass
->details
.runtimeclass
.ifaces
= ifaces
;
559 runtimeclass
->defined
= TRUE
;
560 if (!type_runtimeclass_get_default_iface(runtimeclass
))
561 error_loc("missing default interface on runtimeclass %s\n", runtimeclass
->name
);
563 LIST_FOR_EACH_ENTRY(ifref
, ifaces
, ifref_t
, entry
)
565 /* FIXME: this should probably not be allowed, here or in coclass, */
566 /* but for now there's too many places in Wine IDL where it is to */
567 /* even print a warning. */
568 if (!(ifref
->type
->defined
)) continue;
569 if (!(requires
= type_iface_get_requires(ifref
->type
))) continue;
570 LIST_FOR_EACH_ENTRY(required
, requires
, ifref_t
, entry
)
574 LIST_FOR_EACH_ENTRY(tmp
, ifaces
, ifref_t
, entry
)
575 if ((found
= type_is_equal(tmp
->type
, required
->type
))) break;
578 error_loc("interface '%s' also requires interface '%s', "
579 "but runtimeclass '%s' does not implement it.\n",
580 ifref
->type
->name
, required
->type
->name
, runtimeclass
->name
);
587 type_t
*type_apicontract_declare(char *name
, struct namespace *namespace)
589 type_t
*type
= get_type(TYPE_APICONTRACT
, name
, namespace, 0);
590 if (type_get_type_detect_alias(type
) != TYPE_APICONTRACT
)
591 error_loc("apicontract %s previously not declared a apicontract at %s:%d\n",
592 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
596 type_t
*type_apicontract_define(type_t
*apicontract
, attr_list_t
*attrs
)
598 if (apicontract
->defined
)
599 error_loc("apicontract %s already defined at %s:%d\n",
600 apicontract
->name
, apicontract
->loc_info
.input_name
, apicontract
->loc_info
.line_number
);
601 apicontract
->attrs
= check_apicontract_attrs(apicontract
->name
, attrs
);
602 apicontract
->defined
= TRUE
;
606 type_t
*type_parameterized_interface_declare(char *name
, struct namespace *namespace, type_list_t
*params
)
608 type_t
*type
= get_type(TYPE_PARAMETERIZED_TYPE
, name
, namespace, 0);
609 if (type_get_type_detect_alias(type
) != TYPE_PARAMETERIZED_TYPE
)
610 error_loc("pinterface %s previously not declared a pinterface at %s:%d\n",
611 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
612 type
->details
.parameterized
.type
= make_type(TYPE_INTERFACE
);
613 type
->details
.parameterized
.params
= params
;
617 type_t
*type_parameterized_interface_define(type_t
*type
, attr_list_t
*attrs
, type_t
*inherit
, statement_list_t
*stmts
, ifref_list_t
*requires
)
621 error_loc("pinterface %s already defined at %s:%d\n",
622 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
624 /* The parameterized type UUID is actually a PIID that is then used as a seed to generate
625 * a new type GUID with the rules described in:
626 * https://docs.microsoft.com/en-us/uwp/winrt-cref/winrt-type-system#parameterized-types
627 * TODO: store type signatures for generated interfaces, and generate their GUIDs
629 type
->attrs
= check_interface_attrs(type
->name
, attrs
);
631 iface
= type
->details
.parameterized
.type
;
632 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
633 iface
->details
.iface
->disp_props
= NULL
;
634 iface
->details
.iface
->disp_methods
= NULL
;
635 iface
->details
.iface
->stmts
= stmts
;
636 iface
->details
.iface
->inherit
= inherit
;
637 iface
->details
.iface
->disp_inherit
= NULL
;
638 iface
->details
.iface
->async_iface
= NULL
;
639 iface
->details
.iface
->requires
= requires
;
641 type
->defined
= TRUE
;
645 int type_is_equal(const type_t
*type1
, const type_t
*type2
)
649 if (type_get_type_detect_alias(type1
) != type_get_type_detect_alias(type2
))
651 if (type1
->namespace != type2
->namespace)
654 if (type1
->name
&& type2
->name
)
655 return !strcmp(type1
->name
, type2
->name
);
656 else if ((!type1
->name
&& type2
->name
) || (type1
->name
&& !type2
->name
))
659 /* FIXME: do deep inspection of types to determine if they are equal */