4 * Copyright 2002 Ove Kaaven
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
37 static int indentation
= 0;
38 static int is_object_interface
= 0;
39 user_type_list_t user_type_list
= LIST_INIT(user_type_list
);
40 context_handle_list_t context_handle_list
= LIST_INIT(context_handle_list
);
41 generic_handle_list_t generic_handle_list
= LIST_INIT(generic_handle_list
);
43 static void write_type_v(FILE *f
, const decl_spec_t
*t
, int is_field
, int declonly
, const char *name
, enum name_type name_type
);
45 static void write_apicontract_guard_start(FILE *header
, const expr_t
*expr
);
46 static void write_apicontract_guard_end(FILE *header
, const expr_t
*expr
);
48 static void write_widl_using_macros(FILE *header
, type_t
*iface
);
50 static void indent(FILE *h
, int delta
)
53 if (delta
< 0) indentation
+= delta
;
54 for (c
=0; c
<indentation
; c
++) fprintf(h
, " ");
55 if (delta
> 0) indentation
+= delta
;
58 static void write_line(FILE *f
, int delta
, const char *fmt
, ...)
68 int is_ptrchain_attr(const var_t
*var
, enum attr_type t
)
70 if (is_attr(var
->attrs
, t
))
74 type_t
*type
= var
->declspec
.type
;
77 if (is_attr(type
->attrs
, t
))
79 else if (type_is_alias(type
))
80 type
= type_alias_get_aliasee_type(type
);
81 else if (is_ptr(type
))
82 type
= type_pointer_get_ref_type(type
);
88 int is_aliaschain_attr(const type_t
*type
, enum attr_type attr
)
90 const type_t
*t
= type
;
93 if (is_attr(t
->attrs
, attr
))
95 else if (type_is_alias(t
))
96 t
= type_alias_get_aliasee_type(t
);
101 int is_attr(const attr_list_t
*list
, enum attr_type t
)
104 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
105 if (attr
->type
== t
) return 1;
109 void *get_attrp(const attr_list_t
*list
, enum attr_type t
)
112 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
113 if (attr
->type
== t
) return attr
->u
.pval
;
117 unsigned int get_attrv(const attr_list_t
*list
, enum attr_type t
)
120 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
121 if (attr
->type
== t
) return attr
->u
.ival
;
125 static char *format_parameterized_type_args(const type_t
*type
, const char *prefix
, const char *suffix
)
127 typeref_list_t
*params
;
129 size_t len
= 0, pos
= 0;
132 params
= type
->details
.parameterized
.params
;
133 if (params
) LIST_FOR_EACH_ENTRY(ref
, params
, typeref_t
, entry
)
135 assert(ref
->type
->type_type
!= TYPE_POINTER
);
136 pos
+= strappend(&buf
, &len
, pos
, "%s%s%s", prefix
, ref
->type
->name
, suffix
);
137 if (list_next(params
, &ref
->entry
)) pos
+= strappend(&buf
, &len
, pos
, ", ");
140 if (!buf
) return xstrdup("");
144 static void write_guid(FILE *f
, const char *guid_prefix
, const char *name
, const struct uuid
*uuid
)
147 fprintf(f
, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
148 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
149 guid_prefix
, name
, uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0],
150 uuid
->Data4
[1], uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5],
151 uuid
->Data4
[6], uuid
->Data4
[7]);
154 static void write_uuid_decl(FILE *f
, type_t
*type
, const struct uuid
*uuid
)
156 fprintf(f
, "#ifdef __CRT_UUID_DECL\n");
157 fprintf(f
, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
158 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
159 type
->c_name
, uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
160 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6],
162 fprintf(f
, "#endif\n");
165 static const char *uuid_string(const struct uuid
*uuid
)
169 sprintf(buf
, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
170 uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1], uuid
->Data4
[2],
171 uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6], uuid
->Data4
[7]);
176 static void write_namespace_start(FILE *header
, struct namespace *namespace)
178 if(is_global_namespace(namespace)) {
179 if(use_abi_namespace
)
180 write_line(header
, 1, "namespace ABI {");
184 write_namespace_start(header
, namespace->parent
);
185 write_line(header
, 1, "namespace %s {", namespace->name
);
188 static void write_namespace_end(FILE *header
, struct namespace *namespace)
190 if(is_global_namespace(namespace)) {
191 if(use_abi_namespace
)
192 write_line(header
, -1, "}");
196 write_line(header
, -1, "}");
197 write_namespace_end(header
, namespace->parent
);
200 const char *get_name(const var_t
*v
)
204 if (is_attr( v
->attrs
, ATTR_EVENTADD
))
205 return buffer
= strmake( "add_%s", v
->name
);
206 if (is_attr( v
->attrs
, ATTR_EVENTREMOVE
))
207 return buffer
= strmake( "remove_%s", v
->name
);
208 if (is_attr( v
->attrs
, ATTR_PROPGET
))
209 return buffer
= strmake( "get_%s", v
->name
);
210 if (is_attr( v
->attrs
, ATTR_PROPPUT
))
211 return buffer
= strmake( "put_%s", v
->name
);
212 if (is_attr( v
->attrs
, ATTR_PROPPUTREF
))
213 return buffer
= strmake( "putref_%s", v
->name
);
218 static void write_fields(FILE *h
, var_list_t
*fields
, enum name_type name_type
)
220 unsigned nameless_struct_cnt
= 0, nameless_struct_i
= 0, nameless_union_cnt
= 0, nameless_union_i
= 0;
227 LIST_FOR_EACH_ENTRY( v
, fields
, var_t
, entry
) {
228 if (!v
|| !v
->declspec
.type
) continue;
230 switch(type_get_type_detect_alias(v
->declspec
.type
)) {
232 case TYPE_ENCAPSULATED_UNION
:
233 nameless_struct_cnt
++;
236 nameless_union_cnt
++;
243 LIST_FOR_EACH_ENTRY( v
, fields
, var_t
, entry
) {
244 expr_t
*contract
= get_attrp(v
->attrs
, ATTR_CONTRACT
);
245 if (!v
|| !v
->declspec
.type
) continue;
246 if (contract
) write_apicontract_guard_start(h
, contract
);
251 switch(type_get_type_detect_alias(v
->declspec
.type
)) {
253 case TYPE_ENCAPSULATED_UNION
:
255 fprintf(h
, "__C89_NAMELESS ");
256 if(nameless_struct_cnt
== 1) {
257 name
= "__C89_NAMELESSSTRUCTNAME";
258 }else if(nameless_struct_i
< 5 /* # of supporting macros */) {
259 sprintf(buf
, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i
);
266 fprintf(h
, "__C89_NAMELESS ");
267 if(nameless_union_cnt
== 1) {
268 name
= "__C89_NAMELESSUNIONNAME";
269 }else if(nameless_union_i
< 8 /* # of supporting macros */ ) {
270 sprintf(buf
, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i
);
278 write_type_v(h
, &v
->declspec
, TRUE
, v
->declonly
, name
, name_type
);
280 if (contract
) write_apicontract_guard_end(h
, contract
);
284 static void write_enums(FILE *h
, var_list_t
*enums
, const char *enum_name
)
288 LIST_FOR_EACH_ENTRY( v
, enums
, var_t
, entry
)
290 expr_t
*contract
= get_attrp(v
->attrs
, ATTR_CONTRACT
);
291 if (contract
) write_apicontract_guard_start(h
, contract
);
295 fprintf(h
, "%s", get_name(v
));
297 fprintf(h
, "%s_%s", enum_name
, get_name(v
));
300 write_expr(h
, v
->eval
, 0, 1, NULL
, NULL
, "");
303 if (list_next( enums
, &v
->entry
)) fprintf(h
, ",\n");
304 else fprintf(h
, "\n");
305 if (contract
) write_apicontract_guard_end(h
, contract
);
309 int needs_space_after(type_t
*t
)
311 return (type_is_alias(t
) ||
312 (!is_ptr(t
) && (!is_array(t
) || !type_array_is_decl_as_ptr(t
) || t
->name
)));
315 static int decl_needs_parens(const type_t
*t
)
317 if (type_is_alias(t
))
319 if (is_array(t
) && !type_array_is_decl_as_ptr(t
))
324 static void write_pointer_left(FILE *h
, type_t
*ref
)
326 if (needs_space_after(ref
))
328 if (decl_needs_parens(ref
))
330 if (type_get_type_detect_alias(ref
) == TYPE_FUNCTION
)
332 const char *callconv
= get_attrp(ref
->attrs
, ATTR_CALLCONV
);
333 if (!callconv
&& is_object_interface
) callconv
= "STDMETHODCALLTYPE";
334 if (callconv
) fprintf(h
, "%s ", callconv
);
339 void write_type_left(FILE *h
, const decl_spec_t
*ds
, enum name_type name_type
, int declonly
, int write_callconv
)
341 type_t
*t
= ds
->type
;
342 const char *decl_name
, *name
;
347 decl_name
= type_get_decl_name(t
, name_type
);
348 name
= type_get_name(t
, name_type
);
350 if (ds
->func_specifier
& FUNCTION_SPECIFIER_INLINE
)
351 fprintf(h
, "inline ");
353 if ((ds
->qualifier
& TYPE_QUALIFIER_CONST
) && (type_is_alias(t
) || !is_ptr(t
)))
354 fprintf(h
, "const ");
356 if (!winrt_mode
&& type_is_alias(t
)) fprintf(h
, "%s", t
->name
);
358 switch (type_get_type_detect_alias(t
)) {
360 if (declonly
) fprintf(h
, "enum %s", decl_name
? decl_name
: "");
361 else if (!t
->written
) {
363 if (decl_name
) fprintf(h
, "enum %s {\n", decl_name
);
364 else fprintf(h
, "enum {\n");
367 write_enums(h
, type_enum_get_values(t
), is_global_namespace(t
->namespace) ? NULL
: t
->name
);
371 else if (winrt_mode
&& name_type
== NAME_DEFAULT
&& name
) fprintf(h
, "%s", name
);
372 else fprintf(h
, "enum %s", name
? name
: "");
375 case TYPE_ENCAPSULATED_UNION
:
376 if (declonly
) fprintf(h
, "struct %s", decl_name
? decl_name
: "");
377 else if (!t
->written
) {
379 if (decl_name
) fprintf(h
, "struct %s {\n", decl_name
);
380 else fprintf(h
, "struct {\n");
383 if (type_get_type(t
) != TYPE_STRUCT
)
384 write_fields(h
, type_encapsulated_union_get_fields(t
), name_type
);
386 write_fields(h
, type_struct_get_fields(t
), name_type
);
390 else if (winrt_mode
&& name_type
== NAME_DEFAULT
&& name
) fprintf(h
, "%s", name
);
391 else fprintf(h
, "struct %s", name
? name
: "");
394 if (declonly
) fprintf(h
, "union %s", decl_name
? decl_name
: "");
395 else if (!t
->written
) {
397 if (decl_name
) fprintf(h
, "union %s {\n", decl_name
);
398 else fprintf(h
, "union {\n");
401 write_fields(h
, type_union_get_cases(t
), name_type
);
405 else if (winrt_mode
&& name_type
== NAME_DEFAULT
&& name
) fprintf(h
, "%s", name
);
406 else fprintf(h
, "union %s", name
? name
: "");
410 write_type_left(h
, type_pointer_get_ref(t
), name_type
, declonly
, FALSE
);
411 write_pointer_left(h
, type_pointer_get_ref_type(t
));
412 if (ds
->qualifier
& TYPE_QUALIFIER_CONST
) fprintf(h
, "const ");
416 if (t
->name
&& type_array_is_decl_as_ptr(t
))
417 fprintf(h
, "%s", t
->name
);
420 write_type_left(h
, type_array_get_element(t
), name_type
, declonly
, !type_array_is_decl_as_ptr(t
));
421 if (type_array_is_decl_as_ptr(t
))
422 write_pointer_left(h
, type_array_get_element_type(t
));
427 write_type_left(h
, type_function_get_ret(t
), name_type
, declonly
, TRUE
);
429 /* A pointer to a function has to write the calling convention inside
430 * the parentheses. There's no way to handle that here, so we have to
431 * use an extra parameter to tell us whether to write the calling
432 * convention or not. */
435 const char *callconv
= get_attrp(t
->attrs
, ATTR_CALLCONV
);
436 if (!callconv
&& is_object_interface
) callconv
= "STDMETHODCALLTYPE";
437 if (callconv
) fprintf(h
, " %s ", callconv
);
442 if (type_basic_get_type(t
) != TYPE_BASIC_INT32
&&
443 type_basic_get_type(t
) != TYPE_BASIC_INT64
&&
444 type_basic_get_type(t
) != TYPE_BASIC_LONG
&&
445 type_basic_get_type(t
) != TYPE_BASIC_HYPER
)
447 if (type_basic_get_sign(t
) < 0) fprintf(h
, "signed ");
448 else if (type_basic_get_sign(t
) > 0) fprintf(h
, "unsigned ");
450 switch (type_basic_get_type(t
))
452 case TYPE_BASIC_INT8
: fprintf(h
, "small"); break;
453 case TYPE_BASIC_INT16
: fprintf(h
, "short"); break;
454 case TYPE_BASIC_INT
: fprintf(h
, "int"); break;
455 case TYPE_BASIC_INT3264
: fprintf(h
, "__int3264"); break;
456 case TYPE_BASIC_BYTE
: fprintf(h
, "byte"); break;
457 case TYPE_BASIC_CHAR
: fprintf(h
, "char"); break;
458 case TYPE_BASIC_WCHAR
: fprintf(h
, "wchar_t"); break;
459 case TYPE_BASIC_FLOAT
: fprintf(h
, "float"); break;
460 case TYPE_BASIC_DOUBLE
: fprintf(h
, "double"); break;
461 case TYPE_BASIC_ERROR_STATUS_T
: fprintf(h
, "error_status_t"); break;
462 case TYPE_BASIC_HANDLE
: fprintf(h
, "handle_t"); break;
463 case TYPE_BASIC_INT32
:
464 if (type_basic_get_sign(t
) > 0)
465 fprintf(h
, "UINT32");
469 case TYPE_BASIC_LONG
:
470 if (type_basic_get_sign(t
) > 0)
475 case TYPE_BASIC_INT64
:
476 if (type_basic_get_sign(t
) > 0)
477 fprintf(h
, "UINT64");
481 case TYPE_BASIC_HYPER
:
482 if (type_basic_get_sign(t
) > 0)
483 fprintf(h
, "MIDL_uhyper");
492 fprintf(h
, "%s", type_get_name(t
, name_type
));
494 case TYPE_RUNTIMECLASS
:
495 fprintf(h
, "%s", type_get_name(type_runtimeclass_get_default_iface(t
, TRUE
), name_type
));
498 fprintf(h
, "%s", type_get_name(type_delegate_get_iface(t
), name_type
));
505 const decl_spec_t ds
= {.type
= type_bitfield_get_field(t
)};
506 write_type_left(h
, &ds
, name_type
, declonly
, TRUE
);
511 const decl_spec_t
*ds
= type_alias_get_aliasee(t
);
512 int in_namespace
= ds
&& ds
->type
&& ds
->type
->namespace && !is_global_namespace(ds
->type
->namespace);
513 if (!in_namespace
) fprintf(h
, "%s", t
->name
);
514 else write_type_left(h
, ds
, name_type
, declonly
, write_callconv
);
517 case TYPE_PARAMETERIZED_TYPE
:
519 type_t
*iface
= type_parameterized_type_get_real_type(t
);
520 if (type_get_type(iface
) == TYPE_DELEGATE
) iface
= type_delegate_get_iface(iface
);
521 args
= format_parameterized_type_args(t
, "", "_logical");
522 fprintf(h
, "%s<%s>", iface
->name
, args
);
527 fprintf(h
, "%s_abi", t
->name
);
529 case TYPE_APICONTRACT
:
530 /* shouldn't be here */
537 void write_type_right(FILE *h
, type_t
*t
, int is_field
)
540 if (type_is_alias(t
)) return;
542 switch (type_get_type(t
))
546 type_t
*elem
= type_array_get_element_type(t
);
547 if (type_array_is_decl_as_ptr(t
))
549 if (decl_needs_parens(elem
))
554 if (is_conformant_array(t
))
555 fprintf(h
, "[%s]", is_field
? "1" : "");
557 fprintf(h
, "[%u]", type_array_get_dim(t
));
559 write_type_right(h
, elem
, FALSE
);
564 const var_list_t
*args
= type_function_get_args(t
);
566 if (args
) write_args(h
, args
, NULL
, 0, FALSE
, NAME_DEFAULT
);
570 write_type_right(h
, type_function_get_rettype(t
), FALSE
);
575 type_t
*ref
= type_pointer_get_ref_type(t
);
576 if (decl_needs_parens(ref
))
578 write_type_right(h
, ref
, FALSE
);
582 fprintf(h
, " : %u", type_bitfield_get_bits(t
)->cval
);
588 case TYPE_ENCAPSULATED_UNION
:
594 case TYPE_RUNTIMECLASS
:
596 case TYPE_PARAMETERIZED_TYPE
:
599 case TYPE_APICONTRACT
:
600 /* not supposed to be here */
606 static void write_type_v(FILE *h
, const decl_spec_t
*ds
, int is_field
, int declonly
, const char *name
, enum name_type name_type
)
608 type_t
*t
= ds
->type
;
612 if (t
) write_type_left(h
, ds
, name_type
, declonly
, TRUE
);
614 if (name
) fprintf(h
, "%s%s", !t
|| needs_space_after(t
) ? " " : "", name
);
617 write_type_right(h
, t
, is_field
);
620 static void write_type_definition(FILE *f
, type_t
*t
, int declonly
)
622 int in_namespace
= t
->namespace && !is_global_namespace(t
->namespace);
623 int save_written
= t
->written
;
624 decl_spec_t ds
= {.type
= t
};
625 expr_t
*contract
= get_attrp(t
->attrs
, ATTR_CONTRACT
);
627 if (contract
) write_apicontract_guard_start(f
, contract
);
629 fprintf(f
, "#ifdef __cplusplus\n");
630 fprintf(f
, "} /* extern \"C\" */\n");
631 write_namespace_start(f
, t
->namespace);
634 write_type_left(f
, &ds
, NAME_DEFAULT
, declonly
, TRUE
);
637 t
->written
= save_written
;
638 write_namespace_end(f
, t
->namespace);
639 fprintf(f
, "extern \"C\" {\n");
640 fprintf(f
, "#else\n");
641 write_type_left(f
, &ds
, NAME_C
, declonly
, TRUE
);
643 if (winrt_mode
) write_widl_using_macros(f
, t
);
644 fprintf(f
, "#endif\n\n");
646 if (contract
) write_apicontract_guard_end(f
, contract
);
649 void write_type_decl(FILE *f
, const decl_spec_t
*t
, const char *name
)
651 write_type_v(f
, t
, FALSE
, TRUE
, name
, NAME_DEFAULT
);
654 void write_type_decl_left(FILE *f
, const decl_spec_t
*ds
)
656 write_type_left(f
, ds
, NAME_DEFAULT
, TRUE
, TRUE
);
659 static int user_type_registered(const char *name
)
662 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
663 if (!strcmp(name
, ut
->name
))
668 static int context_handle_registered(const char *name
)
670 context_handle_t
*ch
;
671 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
672 if (!strcmp(name
, ch
->name
))
677 static int generic_handle_registered(const char *name
)
679 generic_handle_t
*gh
;
680 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
681 if (!strcmp(name
, gh
->name
))
686 unsigned int get_context_handle_offset( const type_t
*type
)
688 context_handle_t
*ch
;
689 unsigned int index
= 0;
691 while (!is_attr( type
->attrs
, ATTR_CONTEXTHANDLE
))
693 if (type_is_alias( type
)) type
= type_alias_get_aliasee_type( type
);
694 else if (is_ptr( type
)) type
= type_pointer_get_ref_type( type
);
695 else error( "internal error: %s is not a context handle\n", type
->name
);
697 LIST_FOR_EACH_ENTRY( ch
, &context_handle_list
, context_handle_t
, entry
)
699 if (!strcmp( type
->name
, ch
->name
)) return index
;
702 error( "internal error: %s is not registered as a context handle\n", type
->name
);
706 unsigned int get_generic_handle_offset( const type_t
*type
)
708 generic_handle_t
*gh
;
709 unsigned int index
= 0;
711 while (!is_attr( type
->attrs
, ATTR_HANDLE
))
713 if (type_is_alias( type
)) type
= type_alias_get_aliasee_type( type
);
714 else if (is_ptr( type
)) type
= type_pointer_get_ref_type( type
);
715 else error( "internal error: %s is not a generic handle\n", type
->name
);
717 LIST_FOR_EACH_ENTRY( gh
, &generic_handle_list
, generic_handle_t
, entry
)
719 if (!strcmp( type
->name
, gh
->name
)) return index
;
722 error( "internal error: %s is not registered as a generic handle\n", type
->name
);
726 /* check for types which require additional prototypes to be generated in the
728 void check_for_additional_prototype_types(type_t
*type
)
732 const char *name
= type
->name
;
733 if (type
->user_types_registered
) break;
734 type
->user_types_registered
= 1;
735 if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
)) {
736 if (!context_handle_registered(name
))
738 context_handle_t
*ch
= xmalloc(sizeof(*ch
));
739 ch
->name
= xstrdup(name
);
740 list_add_tail(&context_handle_list
, &ch
->entry
);
742 /* don't carry on parsing fields within this type */
745 if ((type_get_type(type
) != TYPE_BASIC
||
746 type_basic_get_type(type
) != TYPE_BASIC_HANDLE
) &&
747 is_attr(type
->attrs
, ATTR_HANDLE
)) {
748 if (!generic_handle_registered(name
))
750 generic_handle_t
*gh
= xmalloc(sizeof(*gh
));
751 gh
->name
= xstrdup(name
);
752 list_add_tail(&generic_handle_list
, &gh
->entry
);
754 /* don't carry on parsing fields within this type */
757 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
)) {
758 if (!user_type_registered(name
))
760 user_type_t
*ut
= xmalloc(sizeof *ut
);
761 ut
->name
= xstrdup(name
);
762 list_add_tail(&user_type_list
, &ut
->entry
);
764 /* don't carry on parsing fields within this type as we are already
765 * using a wire marshaled type */
768 else if (type_is_complete(type
))
772 switch (type_get_type_detect_alias(type
))
775 vars
= type_enum_get_values(type
);
778 vars
= type_struct_get_fields(type
);
781 vars
= type_union_get_cases(type
);
787 if (vars
) LIST_FOR_EACH_ENTRY( v
, vars
, const var_t
, entry
)
788 check_for_additional_prototype_types(v
->declspec
.type
);
791 if (type_is_alias(type
))
792 type
= type_alias_get_aliasee_type(type
);
793 else if (is_ptr(type
))
794 type
= type_pointer_get_ref_type(type
);
795 else if (is_array(type
))
796 type
= type_array_get_element_type(type
);
802 static int write_serialize_function_decl(FILE *header
, const type_t
*type
)
804 write_serialize_functions(header
, type
, NULL
);
808 static int serializable_exists(FILE *header
, const type_t
*type
)
813 static int for_each_serializable(const statement_list_t
*stmts
, FILE *header
,
814 int (*proc
)(FILE*, const type_t
*))
816 statement_t
*stmt
, *iface_stmt
;
817 statement_list_t
*iface_stmts
;
820 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, statement_t
, entry
)
822 if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
825 iface_stmts
= type_iface_get_stmts(stmt
->u
.type
);
826 if (iface_stmts
) LIST_FOR_EACH_ENTRY( iface_stmt
, iface_stmts
, statement_t
, entry
)
828 if (iface_stmt
->type
!= STMT_TYPEDEF
) continue;
829 if (iface_stmt
->u
.type_list
) LIST_FOR_EACH_ENTRY(ref
, iface_stmt
->u
.type_list
, typeref_t
, entry
)
831 if (!is_attr(ref
->type
->attrs
, ATTR_ENCODE
)
832 && !is_attr(ref
->type
->attrs
, ATTR_DECODE
))
834 if (!proc(header
, ref
->type
))
843 static void write_user_types(FILE *header
)
846 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
848 const char *name
= ut
->name
;
849 fprintf(header
, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name
, name
);
850 fprintf(header
, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name
, name
);
851 fprintf(header
, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name
, name
);
852 fprintf(header
, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name
, name
);
856 static void write_context_handle_rundowns(FILE *header
)
858 context_handle_t
*ch
;
859 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
861 const char *name
= ch
->name
;
862 fprintf(header
, "void __RPC_USER %s_rundown(%s);\n", name
, name
);
866 static void write_generic_handle_routines(FILE *header
)
868 generic_handle_t
*gh
;
869 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
871 const char *name
= gh
->name
;
872 fprintf(header
, "handle_t __RPC_USER %s_bind(%s);\n", name
, name
);
873 fprintf(header
, "void __RPC_USER %s_unbind(%s, handle_t);\n", name
, name
);
877 static void write_typedef(FILE *header
, type_t
*type
, int declonly
)
879 type_t
*t
= type_alias_get_aliasee_type(type
);
880 if (winrt_mode
&& t
->namespace && !is_global_namespace(t
->namespace))
882 fprintf(header
, "#ifndef __cplusplus\n");
883 fprintf(header
, "typedef ");
884 write_type_v(header
, type_alias_get_aliasee(type
), FALSE
, declonly
, type
->c_name
, NAME_C
);
885 fprintf(header
, ";\n");
886 if (type_get_type_detect_alias(t
) != TYPE_ENUM
)
888 fprintf(header
, "#else /* __cplusplus */\n");
889 write_namespace_start(header
, t
->namespace);
891 fprintf(header
, "typedef ");
892 write_type_v(header
, type_alias_get_aliasee(type
), FALSE
, TRUE
, type
->name
, NAME_DEFAULT
);
893 fprintf(header
, ";\n");
894 write_namespace_end(header
, t
->namespace);
896 fprintf(header
, "#endif /* __cplusplus */\n\n");
900 fprintf(header
, "typedef ");
901 write_type_v(header
, type_alias_get_aliasee(type
), FALSE
, declonly
, type
->name
, NAME_DEFAULT
);
902 fprintf(header
, ";\n");
906 int is_const_decl(const var_t
*var
)
908 const decl_spec_t
*t
;
909 /* strangely, MIDL accepts a const attribute on any pointer in the
910 * declaration to mean that data isn't being instantiated. this appears
911 * to be a bug, but there is no benefit to being incompatible with MIDL,
912 * so we'll do the same thing */
913 for (t
= &var
->declspec
; ; )
915 if (t
->qualifier
& TYPE_QUALIFIER_CONST
)
917 else if (is_ptr(t
->type
))
918 t
= type_pointer_get_ref(t
->type
);
924 static void write_declaration(FILE *header
, const var_t
*v
)
926 if (is_const_decl(v
) && v
->eval
)
928 fprintf(header
, "#define %s (", v
->name
);
929 write_expr(header
, v
->eval
, 0, 1, NULL
, NULL
, "");
930 fprintf(header
, ")\n\n");
934 switch (v
->declspec
.stgclass
)
937 case STG_REGISTER
: /* ignored */
940 fprintf(header
, "static ");
943 fprintf(header
, "extern ");
946 write_type_v(header
, &v
->declspec
, FALSE
, v
->declonly
, v
->name
, NAME_DEFAULT
);
947 fprintf(header
, ";\n\n");
951 static void write_library(FILE *header
, const typelib_t
*typelib
)
953 const struct uuid
*uuid
= get_attrp(typelib
->attrs
, ATTR_UUID
);
954 fprintf(header
, "\n");
955 write_guid(header
, "LIBID", typelib
->name
, uuid
);
956 fprintf(header
, "\n");
960 const type_t
* get_explicit_generic_handle_type(const var_t
* var
)
963 for (t
= var
->declspec
.type
;
964 is_ptr(t
) || type_is_alias(t
);
965 t
= type_is_alias(t
) ? type_alias_get_aliasee_type(t
) : type_pointer_get_ref_type(t
))
966 if ((type_get_type_detect_alias(t
) != TYPE_BASIC
|| type_basic_get_type(t
) != TYPE_BASIC_HANDLE
) &&
967 is_attr(t
->attrs
, ATTR_HANDLE
))
972 const var_t
*get_func_handle_var( const type_t
*iface
, const var_t
*func
,
973 unsigned char *explicit_fc
, unsigned char *implicit_fc
)
976 const var_list_t
*args
= type_function_get_args( func
->declspec
.type
);
978 *explicit_fc
= *implicit_fc
= 0;
979 if (args
) LIST_FOR_EACH_ENTRY( var
, args
, const var_t
, entry
)
981 if (!is_attr( var
->attrs
, ATTR_IN
) && is_attr( var
->attrs
, ATTR_OUT
)) continue;
982 if (type_get_type( var
->declspec
.type
) == TYPE_BASIC
&& type_basic_get_type( var
->declspec
.type
) == TYPE_BASIC_HANDLE
)
984 *explicit_fc
= FC_BIND_PRIMITIVE
;
987 if (get_explicit_generic_handle_type( var
))
989 *explicit_fc
= FC_BIND_GENERIC
;
992 if (is_context_handle( var
->declspec
.type
))
994 *explicit_fc
= FC_BIND_CONTEXT
;
999 if ((var
= get_attrp( iface
->attrs
, ATTR_IMPLICIT_HANDLE
)))
1001 if (type_get_type( var
->declspec
.type
) == TYPE_BASIC
&&
1002 type_basic_get_type( var
->declspec
.type
) == TYPE_BASIC_HANDLE
)
1003 *implicit_fc
= FC_BIND_PRIMITIVE
;
1005 *implicit_fc
= FC_BIND_GENERIC
;
1009 *implicit_fc
= FC_AUTO_HANDLE
;
1013 int has_out_arg_or_return(const var_t
*func
)
1017 if (!is_void(type_function_get_rettype(func
->declspec
.type
)))
1020 if (!type_function_get_args(func
->declspec
.type
))
1023 LIST_FOR_EACH_ENTRY( var
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1024 if (is_attr(var
->attrs
, ATTR_OUT
))
1031 /********** INTERFACES **********/
1033 int is_object(const type_t
*iface
)
1036 if (type_is_defined(iface
) && (type_get_type(iface
) == TYPE_DELEGATE
|| type_iface_get_inherit(iface
)))
1038 if (iface
->attrs
) LIST_FOR_EACH_ENTRY( attr
, iface
->attrs
, const attr_t
, entry
)
1039 if (attr
->type
== ATTR_OBJECT
|| attr
->type
== ATTR_ODL
) return 1;
1043 int is_local(const attr_list_t
*a
)
1045 return is_attr(a
, ATTR_LOCAL
);
1048 const var_t
*is_callas(const attr_list_t
*a
)
1050 return get_attrp(a
, ATTR_CALLAS
);
1053 static int is_inherited_method(const type_t
*iface
, const var_t
*func
)
1055 while ((iface
= type_iface_get_inherit(iface
)))
1057 const statement_t
*stmt
;
1058 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1060 const var_t
*funccmp
= stmt
->u
.var
;
1062 if (!is_callas(func
->attrs
))
1064 char inherit_name
[256];
1065 /* compare full name including property prefix */
1066 strcpy(inherit_name
, get_name(funccmp
));
1067 if (!strcmp(inherit_name
, get_name(func
))) return 1;
1075 static int is_override_method(const type_t
*iface
, const type_t
*child
, const var_t
*func
)
1082 const statement_t
*stmt
;
1083 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(child
))
1085 const var_t
*funccmp
= stmt
->u
.var
;
1087 if (!is_callas(func
->attrs
))
1089 char inherit_name
[256];
1090 /* compare full name including property prefix */
1091 strcpy(inherit_name
, get_name(funccmp
));
1092 if (!strcmp(inherit_name
, get_name(func
))) return 1;
1096 while ((child
= type_iface_get_inherit(child
)) && child
!= iface
);
1101 static int is_aggregate_return(const var_t
*func
)
1103 enum type_type type
= type_get_type(type_function_get_rettype(func
->declspec
.type
));
1104 return type
== TYPE_STRUCT
|| type
== TYPE_UNION
||
1105 type
== TYPE_COCLASS
|| type
== TYPE_INTERFACE
||
1106 type
== TYPE_RUNTIMECLASS
;
1109 static char *get_vtbl_entry_name(const type_t
*iface
, const var_t
*func
)
1111 static char buff
[255];
1112 if (is_inherited_method(iface
, func
))
1113 sprintf(buff
, "%s_%s", iface
->name
, get_name(func
));
1115 sprintf(buff
, "%s", get_name(func
));
1119 static void write_method_macro(FILE *header
, const type_t
*iface
, const type_t
*child
, const char *name
)
1121 const statement_t
*stmt
;
1122 int first_iface
= 1;
1124 if (type_iface_get_inherit(iface
))
1125 write_method_macro(header
, type_iface_get_inherit(iface
), child
, name
);
1127 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1129 const var_t
*func
= stmt
->u
.var
;
1133 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1137 if (is_override_method(iface
, child
, func
))
1140 if (!is_callas(func
->attrs
)) {
1143 fprintf(header
, "#define %s_%s(This", name
, get_name(func
));
1144 if (type_function_get_args(func
->declspec
.type
))
1145 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1146 fprintf(header
, ",%s", arg
->name
);
1147 fprintf(header
, ") ");
1149 if (is_aggregate_return(func
))
1151 fprintf(header
, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name
, get_name(func
));
1155 fprintf(header
, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface
, func
));
1156 if (type_function_get_args(func
->declspec
.type
))
1157 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1158 fprintf(header
, ",%s", arg
->name
);
1159 fprintf(header
, ")\n");
1164 void write_args(FILE *h
, const var_list_t
*args
, const char *name
, int method
, int do_indent
, enum name_type name_type
)
1175 fprintf(h
, "%s* This", name
);
1178 if (args
) LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
) {
1185 else fprintf(h
, ",");
1187 /* In theory we should be writing the definition using write_type_v(..., arg->declonly),
1188 * but that causes redefinition in e.g. proxy files. In fact MIDL disallows
1189 * defining UDTs inside of an argument list. */
1190 write_type_v(h
, &arg
->declspec
, FALSE
, TRUE
, arg
->name
, name_type
);
1192 const expr_t
*expr
= get_attrp(arg
->attrs
, ATTR_DEFAULTVALUE
);
1194 const var_t
*tail_arg
;
1196 /* Output default value only if all following arguments also have default value. */
1197 LIST_FOR_EACH_ENTRY_REV( tail_arg
, args
, const var_t
, entry
) {
1198 if(tail_arg
== arg
) {
1201 /* Fixup the expression type for a BSTR like midl does. */
1202 if (get_type_vt(arg
->declspec
.type
) == VT_BSTR
&& expr
->type
== EXPR_STRLIT
)
1205 bstr
.type
= EXPR_WSTRLIT
;
1210 write_expr( h
, expr
, 0, 1, NULL
, NULL
, "" );
1213 if(!get_attrp(tail_arg
->attrs
, ATTR_DEFAULTVALUE
))
1220 if (do_indent
) indentation
--;
1223 static void write_cpp_method_def(FILE *header
, const type_t
*iface
)
1225 const statement_t
*stmt
;
1227 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1229 const var_t
*func
= stmt
->u
.var
;
1230 if (!is_callas(func
->attrs
)) {
1231 const decl_spec_t
*ret
= type_function_get_ret(func
->declspec
.type
);
1232 const char *callconv
= get_attrp(func
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1233 const var_list_t
*args
= type_function_get_args(func
->declspec
.type
);
1236 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1238 if (is_aggregate_return(func
)) {
1239 fprintf(header
, "#ifdef WIDL_EXPLICIT_AGGREGATE_RETURNS\n");
1242 fprintf(header
, "virtual ");
1243 write_type_decl_left(header
, ret
);
1244 fprintf(header
, "* %s %s(\n", callconv
, get_name(func
));
1247 write_type_decl_left(header
, ret
);
1248 fprintf(header
, " *__ret");
1251 fprintf(header
, ",\n");
1252 write_args(header
, args
, iface
->name
, 2, TRUE
, NAME_DEFAULT
);
1254 fprintf(header
, ") = 0;\n");
1257 write_type_decl_left(header
, ret
);
1258 fprintf(header
, " %s %s(\n", callconv
, get_name(func
));
1259 write_args(header
, args
, iface
->name
, 2, TRUE
, NAME_DEFAULT
);
1260 fprintf(header
, ")\n");
1262 fprintf(header
, "{\n");
1265 write_type_decl_left(header
, ret
);
1266 fprintf(header
, " __ret;\n");
1268 fprintf(header
, "return *%s(&__ret", get_name(func
));
1270 LIST_FOR_EACH_ENTRY(arg
, args
, const var_t
, entry
)
1271 fprintf(header
, ", %s", arg
->name
);
1272 fprintf(header
, ");\n");
1275 fprintf(header
, "}\n");
1277 fprintf(header
, "#else\n");
1281 fprintf(header
, "virtual ");
1282 write_type_decl_left(header
, ret
);
1283 fprintf(header
, " %s %s(\n", callconv
, get_name(func
));
1284 write_args(header
, args
, iface
->name
, 2, TRUE
, NAME_DEFAULT
);
1285 fprintf(header
, ") = 0;\n");
1287 if (is_aggregate_return(func
))
1288 fprintf(header
, "#endif\n");
1289 fprintf(header
, "\n");
1294 static void write_inline_wrappers(FILE *header
, const type_t
*iface
, const type_t
*child
, const char *name
)
1296 const statement_t
*stmt
;
1297 int first_iface
= 1;
1299 if (type_iface_get_inherit(iface
))
1300 write_inline_wrappers(header
, type_iface_get_inherit(iface
), child
, name
);
1302 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1304 const var_t
*func
= stmt
->u
.var
;
1308 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1312 if (is_override_method(iface
, child
, func
))
1315 if (!is_callas(func
->attrs
)) {
1318 fprintf(header
, "static __WIDL_INLINE ");
1319 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1320 fprintf(header
, " %s_%s(", name
, get_name(func
));
1321 write_args(header
, type_function_get_args(func
->declspec
.type
), name
, 1, FALSE
, NAME_C
);
1322 fprintf(header
, ") {\n");
1324 if (!is_aggregate_return(func
)) {
1326 fprintf(header
, "%sThis->lpVtbl->%s(This",
1327 is_void(type_function_get_rettype(func
->declspec
.type
)) ? "" : "return ",
1328 get_vtbl_entry_name(iface
, func
));
1331 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1332 fprintf(header
, " __ret;\n");
1334 fprintf(header
, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface
, func
));
1336 if (type_function_get_args(func
->declspec
.type
))
1337 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1338 fprintf(header
, ",%s", arg
->name
);
1339 fprintf(header
, ");\n");
1341 fprintf(header
, "}\n");
1346 static void do_write_c_method_def(FILE *header
, const type_t
*iface
, const char *name
)
1348 const statement_t
*stmt
;
1349 int first_iface
= 1;
1351 if (type_iface_get_inherit(iface
))
1352 do_write_c_method_def(header
, type_iface_get_inherit(iface
), name
);
1354 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1356 const var_t
*func
= stmt
->u
.var
;
1359 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1362 if (!is_callas(func
->attrs
)) {
1363 const char *callconv
= get_attrp(func
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1364 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1366 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1367 if (is_aggregate_return(func
))
1368 fprintf(header
, " *");
1369 if (is_inherited_method(iface
, func
))
1370 fprintf(header
, " (%s *%s_%s)(\n", callconv
, iface
->name
, func
->name
);
1372 fprintf(header
, " (%s *%s)(\n", callconv
, get_name(func
));
1375 fprintf(header
, "%s *This", name
);
1376 if (is_aggregate_return(func
)) {
1377 fprintf(header
, ",\n");
1379 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1380 fprintf(header
, " *__ret");
1383 if (type_function_get_args(func
->declspec
.type
)) {
1384 fprintf(header
, ",\n");
1385 write_args(header
, type_function_get_args(func
->declspec
.type
), name
, 0, TRUE
, NAME_C
);
1387 fprintf(header
, ");\n");
1388 fprintf(header
, "\n");
1393 static void write_c_method_def(FILE *header
, const type_t
*iface
)
1395 do_write_c_method_def(header
, iface
, iface
->c_name
);
1398 static void write_c_disp_method_def(FILE *header
, const type_t
*iface
)
1400 do_write_c_method_def(header
, type_iface_get_inherit(iface
), iface
->c_name
);
1403 static void write_method_proto(FILE *header
, const type_t
*iface
)
1405 const statement_t
*stmt
;
1407 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1409 const var_t
*func
= stmt
->u
.var
;
1411 if (is_callas(func
->attrs
)) {
1412 const char *callconv
= get_attrp(func
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1413 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1414 /* proxy prototype */
1415 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1416 fprintf(header
, " %s %s_%s_Proxy(\n", callconv
, iface
->name
, get_name(func
));
1417 write_args(header
, type_function_get_args(func
->declspec
.type
), iface
->name
, 1, TRUE
, NAME_DEFAULT
);
1418 fprintf(header
, ");\n");
1419 /* stub prototype */
1420 fprintf(header
, "void __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(func
));
1421 fprintf(header
, " IRpcStubBuffer* This,\n");
1422 fprintf(header
, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1423 fprintf(header
, " PRPC_MESSAGE pRpcMessage,\n");
1424 fprintf(header
, " DWORD* pdwStubPhase);\n");
1429 static void write_locals(FILE *fp
, const type_t
*iface
, int body
)
1431 static const char comment
[]
1432 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1433 const statement_t
*stmt
;
1435 if (!is_object(iface
))
1438 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
)) {
1439 const var_t
*func
= stmt
->u
.var
;
1440 const var_t
*cas
= is_callas(func
->attrs
);
1443 const statement_t
*stmt2
= NULL
;
1444 STATEMENTS_FOR_EACH_FUNC(stmt2
, type_iface_get_stmts(iface
))
1445 if (!strcmp(get_name(stmt2
->u
.var
), cas
->name
))
1447 if (&stmt2
->entry
!= type_iface_get_stmts(iface
)) {
1448 const var_t
*m
= stmt2
->u
.var
;
1449 /* proxy prototype - use local prototype */
1450 write_type_decl_left(fp
, type_function_get_ret(m
->declspec
.type
));
1451 fprintf(fp
, " CALLBACK %s_%s_Proxy(\n", iface
->name
, get_name(m
));
1452 write_args(fp
, type_function_get_args(m
->declspec
.type
), iface
->name
, 1, TRUE
, NAME_DEFAULT
);
1455 const decl_spec_t
*rt
= type_function_get_ret(m
->declspec
.type
);
1456 fprintf(fp
, "\n{\n");
1457 fprintf(fp
, " %s\n", comment
);
1458 if (rt
->type
->name
&& strcmp(rt
->type
->name
, "HRESULT") == 0)
1459 fprintf(fp
, " return E_NOTIMPL;\n");
1460 else if (type_get_type(rt
->type
) != TYPE_VOID
) {
1462 write_type_decl(fp
, rt
, "rv");
1464 fprintf(fp
, " memset(&rv, 0, sizeof rv);\n");
1465 fprintf(fp
, " return rv;\n");
1467 fprintf(fp
, "}\n\n");
1471 /* stub prototype - use remotable prototype */
1472 write_type_decl_left(fp
, type_function_get_ret(func
->declspec
.type
));
1473 fprintf(fp
, " __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(m
));
1474 write_args(fp
, type_function_get_args(func
->declspec
.type
), iface
->name
, 1, TRUE
, NAME_DEFAULT
);
1477 /* Remotable methods must all return HRESULTs. */
1478 fprintf(fp
, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment
);
1483 error_loc("invalid call_as attribute (%s -> %s)\n", func
->name
, cas
->name
);
1488 static void write_local_stubs_stmts(FILE *local_stubs
, const statement_list_t
*stmts
)
1490 const statement_t
*stmt
;
1491 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1493 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1494 write_locals(local_stubs
, stmt
->u
.type
, TRUE
);
1498 void write_local_stubs(const statement_list_t
*stmts
)
1502 if (!local_stubs_name
) return;
1504 local_stubs
= fopen(local_stubs_name
, "w");
1506 error("Could not open %s for output\n", local_stubs_name
);
1509 fprintf(local_stubs
, "/* call_as/local stubs for %s */\n\n", input_name
);
1510 fprintf(local_stubs
, "#include <objbase.h>\n");
1511 fprintf(local_stubs
, "#include \"%s\"\n\n", header_name
);
1513 write_local_stubs_stmts(local_stubs
, stmts
);
1515 fclose(local_stubs
);
1518 static void write_function_proto(FILE *header
, const type_t
*iface
, const var_t
*fun
, const char *prefix
)
1520 const char *callconv
= get_attrp(fun
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1522 if (!callconv
) callconv
= "__cdecl";
1523 /* FIXME: do we need to handle call_as? */
1524 write_type_decl_left(header
, type_function_get_ret(fun
->declspec
.type
));
1525 fprintf(header
, " %s ", callconv
);
1526 fprintf(header
, "%s%s(\n", prefix
, get_name(fun
));
1527 if (type_function_get_args(fun
->declspec
.type
))
1528 write_args(header
, type_function_get_args(fun
->declspec
.type
), iface
->name
, 0, TRUE
, NAME_DEFAULT
);
1530 fprintf(header
, " void");
1531 fprintf(header
, ");\n\n");
1534 static void write_parameterized_type_forward(FILE *header
, type_t
*type
)
1536 type_t
*iface
= type
->details
.parameterized
.type
;
1539 if (type_get_type(iface
) == TYPE_DELEGATE
) iface
= type_delegate_get_iface(iface
);
1541 fprintf(header
, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1542 write_namespace_start(header
, type
->namespace);
1544 args
= format_parameterized_type_args(type
, "class ", "");
1545 write_line(header
, 0, "template <%s>", args
);
1546 write_line(header
, 0, "struct %s_impl;\n", iface
->name
);
1548 write_line(header
, 0, "template <%s>", args
);
1550 args
= format_parameterized_type_args(type
, "", "");
1551 write_line(header
, 0, "struct %s : %s_impl<%s> {};", iface
->name
, iface
->name
, args
);
1554 write_namespace_end(header
, type
->namespace);
1555 fprintf(header
, "#endif\n\n" );
1558 static void write_parameterized_implementation(FILE *header
, type_t
*type
, int declonly
)
1560 const statement_t
*stmt
;
1561 typeref_list_t
*params
= type
->details
.parameterized
.params
;
1563 type_t
*iface
= type
->details
.parameterized
.type
, *base
;
1566 fprintf(header
, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1567 write_line(header
, 0, "} /* extern \"C\" */");
1568 write_namespace_start(header
, type
->namespace);
1570 if (type_get_type(iface
) == TYPE_DELEGATE
) iface
= type_delegate_get_iface(iface
);
1571 base
= type_iface_get_inherit(iface
);
1573 args
= format_parameterized_type_args(type
, "class ", "");
1574 write_line(header
, 0, "template <%s>", args
);
1576 write_line(header
, 0, "struct %s_impl%s", iface
->name
, base
? strmake(" : %s", base
->name
) : "");
1577 write_line(header
, 0, "{");
1579 write_line(header
, 1, "private:");
1580 if (params
) LIST_FOR_EACH_ENTRY(ref
, params
, typeref_t
, entry
)
1582 write_line(header
, 0, "typedef typename Windows::Foundation::Internal::GetAbiType<%s>::type %s_abi;", ref
->type
->name
, ref
->type
->name
);
1583 write_line(header
, 0, "typedef typename Windows::Foundation::Internal::GetLogicalType<%s>::type %s_logical;", ref
->type
->name
, ref
->type
->name
);
1587 write_line(header
, 1, "public:");
1588 if (params
) LIST_FOR_EACH_ENTRY(ref
, params
, typeref_t
, entry
)
1589 write_line(header
, 0, "typedef %s %s_complex;", ref
->type
->name
, ref
->type
->name
);
1591 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1593 const var_t
*func
= stmt
->u
.var
;
1594 if (is_callas(func
->attrs
)) continue;
1596 fprintf(header
, "virtual ");
1597 write_type_decl_left(header
, &func
->declspec
);
1598 fprintf(header
, "%s(", get_name(func
));
1599 write_args(header
, type_function_get_args(func
->declspec
.type
), NULL
, 0, 0, NAME_DEFAULT
);
1600 fprintf(header
, ") = 0;\n");
1603 write_line(header
, -1, "};");
1605 write_namespace_end(header
, type
->namespace);
1606 write_line(header
, 0, "extern \"C\" {");
1607 write_line(header
, 0, "#endif\n");
1610 static void write_forward(FILE *header
, type_t
*iface
)
1612 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", iface
->c_name
);
1613 fprintf(header
, "#define __%s_FWD_DEFINED__\n", iface
->c_name
);
1614 fprintf(header
, "typedef interface %s %s;\n", iface
->c_name
, iface
->c_name
);
1615 fprintf(header
, "#ifdef __cplusplus\n");
1616 if (iface
->namespace && !is_global_namespace(iface
->namespace))
1617 fprintf(header
, "#define %s %s\n", iface
->c_name
, iface
->qualified_name
);
1618 if (!iface
->impl_name
)
1620 write_namespace_start(header
, iface
->namespace);
1621 write_line(header
, 0, "interface %s;", iface
->name
);
1622 write_namespace_end(header
, iface
->namespace);
1624 fprintf(header
, "#endif /* __cplusplus */\n");
1625 fprintf(header
, "#endif\n\n" );
1628 static char *format_apicontract_macro(const type_t
*type
)
1630 char *name
= format_namespace(type
->namespace, "", "_", type
->name
, NULL
);
1632 for (i
= strlen(name
); i
> 0; --i
) name
[i
- 1] = toupper(name
[i
- 1]);
1636 static void write_apicontract_guard_start(FILE *header
, const expr_t
*expr
)
1641 if (!winrt_mode
) return;
1642 type
= expr
->u
.tref
.type
;
1643 ver
= expr
->ref
->u
.lval
;
1644 name
= format_apicontract_macro(type
);
1645 fprintf(header
, "#if %s_VERSION >= %#x\n", name
, ver
);
1649 static void write_apicontract_guard_end(FILE *header
, const expr_t
*expr
)
1654 if (!winrt_mode
) return;
1655 type
= expr
->u
.tref
.type
;
1656 ver
= expr
->ref
->u
.lval
;
1657 name
= format_apicontract_macro(type
);
1658 fprintf(header
, "#endif /* %s_VERSION >= %#x */\n", name
, ver
);
1662 static void write_com_interface_start(FILE *header
, const type_t
*iface
)
1664 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
1665 expr_t
*contract
= get_attrp(iface
->attrs
, ATTR_CONTRACT
);
1666 fprintf(header
, "/*****************************************************************************\n");
1667 fprintf(header
, " * %s %sinterface\n", iface
->name
, dispinterface
? "disp" : "");
1668 fprintf(header
, " */\n");
1669 if (contract
) write_apicontract_guard_start(header
, contract
);
1670 fprintf(header
,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface
->c_name
, dispinterface
? "DISP" : "");
1671 fprintf(header
,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface
->c_name
, dispinterface
? "DISP" : "");
1674 static void write_widl_using_method_macros(FILE *header
, const type_t
*iface
, const type_t
*top_iface
)
1676 const statement_t
*stmt
;
1677 const char *name
= top_iface
->short_name
? top_iface
->short_name
: top_iface
->name
;
1679 if (type_iface_get_inherit(iface
)) write_widl_using_method_macros(header
, type_iface_get_inherit(iface
), top_iface
);
1681 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1683 const var_t
*func
= stmt
->u
.var
;
1684 const char *func_name
;
1686 if (is_override_method(iface
, top_iface
, func
)) continue;
1687 if (is_callas(func
->attrs
)) continue;
1689 func_name
= get_name(func
);
1690 fprintf(header
, "#define %s_%s %s_%s\n", name
, func_name
, top_iface
->c_name
, func_name
);
1694 static void write_widl_using_macros(FILE *header
, type_t
*iface
)
1696 const struct uuid
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
1697 const char *name
= iface
->short_name
? iface
->short_name
: iface
->name
;
1700 if (!strcmp(iface
->name
, iface
->c_name
)) return;
1702 macro
= format_namespace(iface
->namespace, "WIDL_using_", "_", NULL
, NULL
);
1703 fprintf(header
, "#ifdef %s\n", macro
);
1705 if (uuid
) fprintf(header
, "#define IID_%s IID_%s\n", name
, iface
->c_name
);
1706 if (iface
->type_type
== TYPE_INTERFACE
) fprintf(header
, "#define %sVtbl %sVtbl\n", name
, iface
->c_name
);
1707 fprintf(header
, "#define %s %s\n", name
, iface
->c_name
);
1709 if (iface
->type_type
== TYPE_INTERFACE
) write_widl_using_method_macros(header
, iface
, iface
);
1711 fprintf(header
, "#endif /* %s */\n", macro
);
1715 static void write_com_interface_end(FILE *header
, type_t
*iface
)
1717 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
1718 const struct uuid
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
1719 expr_t
*contract
= get_attrp(iface
->attrs
, ATTR_CONTRACT
);
1723 write_guid(header
, dispinterface
? "DIID" : "IID", iface
->c_name
, uuid
);
1726 fprintf(header
, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1727 if (!is_global_namespace(iface
->namespace)) {
1728 write_line(header
, 0, "} /* extern \"C\" */");
1729 write_namespace_start(header
, iface
->namespace);
1732 if (strchr(iface
->name
, '<')) write_line(header
, 0, "template<>");
1733 write_line(header
, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid
));
1737 if (strchr(iface
->name
, '<')) fprintf(header
, "template<> struct ");
1738 else fprintf(header
, "interface ");
1740 if (iface
->impl_name
)
1742 fprintf(header
, "%s : %s\n", iface
->name
, iface
->impl_name
);
1743 write_line(header
, 1, "{");
1745 else if (type_iface_get_inherit(iface
))
1747 fprintf(header
, "%s : public %s\n", iface
->name
,
1748 type_iface_get_inherit(iface
)->name
);
1749 write_line(header
, 1, "{");
1753 fprintf(header
, "%s\n", iface
->name
);
1754 write_line(header
, 1, "{\n");
1755 write_line(header
, 0, "BEGIN_INTERFACE\n");
1757 /* dispinterfaces don't have real functions, so don't write C++ functions for
1759 if (!dispinterface
&& !iface
->impl_name
)
1760 write_cpp_method_def(header
, iface
);
1761 if (!type_iface_get_inherit(iface
) && !iface
->impl_name
)
1762 write_line(header
, 0, "END_INTERFACE\n");
1763 write_line(header
, -1, "};");
1764 if (!is_global_namespace(iface
->namespace)) {
1765 write_namespace_end(header
, iface
->namespace);
1766 write_line(header
, 0, "extern \"C\" {");
1769 write_uuid_decl(header
, iface
, uuid
);
1770 fprintf(header
, "#else\n");
1772 write_line(header
, 1, "typedef struct %sVtbl {", iface
->c_name
);
1773 write_line(header
, 0, "BEGIN_INTERFACE\n");
1775 write_c_disp_method_def(header
, iface
);
1777 write_c_method_def(header
, iface
);
1778 write_line(header
, 0, "END_INTERFACE");
1779 write_line(header
, -1, "} %sVtbl;\n", iface
->c_name
);
1780 fprintf(header
, "interface %s {\n", iface
->c_name
);
1781 fprintf(header
, " CONST_VTBL %sVtbl* lpVtbl;\n", iface
->c_name
);
1782 fprintf(header
, "};\n\n");
1783 fprintf(header
, "#ifdef COBJMACROS\n");
1784 /* dispinterfaces don't have real functions, so don't write macros for them,
1785 * only for the interface this interface inherits from, i.e. IDispatch */
1786 fprintf(header
, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1787 type
= dispinterface
? type_iface_get_inherit(iface
) : iface
;
1788 write_method_macro(header
, type
, type
, iface
->c_name
);
1789 fprintf(header
, "#else\n");
1790 write_inline_wrappers(header
, type
, type
, iface
->c_name
);
1791 fprintf(header
, "#endif\n");
1792 if (winrt_mode
) write_widl_using_macros(header
, iface
);
1793 fprintf(header
, "#endif\n");
1794 fprintf(header
, "\n");
1795 fprintf(header
, "#endif\n");
1796 fprintf(header
, "\n");
1797 /* dispinterfaces don't have real functions, so don't write prototypes for
1799 if (!dispinterface
&& !winrt_mode
)
1801 write_method_proto(header
, iface
);
1802 write_locals(header
, iface
, FALSE
);
1803 fprintf(header
, "\n");
1805 fprintf(header
, "#endif /* __%s_%sINTERFACE_DEFINED__ */\n", iface
->c_name
, dispinterface
? "DISP" : "");
1806 if (contract
) write_apicontract_guard_end(header
, contract
);
1807 fprintf(header
, "\n");
1810 static void write_rpc_interface_start(FILE *header
, const type_t
*iface
)
1812 unsigned int ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
1813 const var_t
*var
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
1814 expr_t
*contract
= get_attrp(iface
->attrs
, ATTR_CONTRACT
);
1816 fprintf(header
, "/*****************************************************************************\n");
1817 fprintf(header
, " * %s interface (v%d.%d)\n", iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1818 fprintf(header
, " */\n");
1819 if (contract
) write_apicontract_guard_start(header
, contract
);
1820 fprintf(header
,"#ifndef __%s_INTERFACE_DEFINED__\n", iface
->name
);
1821 fprintf(header
,"#define __%s_INTERFACE_DEFINED__\n\n", iface
->name
);
1824 fprintf(header
, "extern ");
1825 write_type_decl( header
, &var
->declspec
, var
->name
);
1826 fprintf(header
, ";\n");
1830 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client
, iface
->name
);
1831 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server
, iface
->name
);
1835 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1836 prefix_client
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1837 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1838 prefix_server
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1842 static void write_rpc_interface_end(FILE *header
, const type_t
*iface
)
1844 expr_t
*contract
= get_attrp(iface
->attrs
, ATTR_CONTRACT
);
1845 fprintf(header
, "\n#endif /* __%s_INTERFACE_DEFINED__ */\n", iface
->name
);
1846 if (contract
) write_apicontract_guard_end(header
, contract
);
1847 fprintf(header
, "\n");
1850 static void write_coclass(FILE *header
, type_t
*cocl
)
1852 const struct uuid
*uuid
= get_attrp(cocl
->attrs
, ATTR_UUID
);
1854 fprintf(header
, "/*****************************************************************************\n");
1855 fprintf(header
, " * %s coclass\n", cocl
->name
);
1856 fprintf(header
, " */\n\n");
1858 write_guid(header
, "CLSID", cocl
->name
, uuid
);
1859 fprintf(header
, "\n#ifdef __cplusplus\n");
1862 fprintf(header
, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid
), cocl
->name
);
1863 write_uuid_decl(header
, cocl
, uuid
);
1867 fprintf(header
, "class %s;\n", cocl
->name
);
1869 fprintf(header
, "#endif\n");
1870 fprintf(header
, "\n");
1873 static void write_coclass_forward(FILE *header
, type_t
*cocl
)
1875 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", cocl
->name
);
1876 fprintf(header
, "#define __%s_FWD_DEFINED__\n", cocl
->name
);
1877 fprintf(header
, "#ifdef __cplusplus\n");
1878 fprintf(header
, "typedef class %s %s;\n", cocl
->name
, cocl
->name
);
1879 fprintf(header
, "#else\n");
1880 fprintf(header
, "typedef struct %s %s;\n", cocl
->name
, cocl
->name
);
1881 fprintf(header
, "#endif /* defined __cplusplus */\n");
1882 fprintf(header
, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl
->name
);
1885 static void write_apicontract(FILE *header
, type_t
*apicontract
)
1887 char *name
= format_apicontract_macro(apicontract
);
1888 fprintf(header
, "#if !defined(%s_VERSION)\n", name
);
1889 fprintf(header
, "#define %s_VERSION %#x\n", name
, get_attrv(apicontract
->attrs
, ATTR_CONTRACTVERSION
));
1890 fprintf(header
, "#endif // defined(%s_VERSION)\n\n", name
);
1894 static void write_runtimeclass(FILE *header
, type_t
*runtimeclass
)
1896 expr_t
*contract
= get_attrp(runtimeclass
->attrs
, ATTR_CONTRACT
);
1897 char *name
, *c_name
;
1899 name
= format_namespace(runtimeclass
->namespace, "", ".", runtimeclass
->name
, NULL
);
1900 c_name
= format_namespace(runtimeclass
->namespace, "", "_", runtimeclass
->name
, NULL
);
1901 fprintf(header
, "/*\n");
1902 fprintf(header
, " * Class %s\n", name
);
1903 fprintf(header
, " */\n");
1904 if (contract
) write_apicontract_guard_start(header
, contract
);
1905 fprintf(header
, "#ifndef RUNTIMECLASS_%s_DEFINED\n", c_name
);
1906 fprintf(header
, "#define RUNTIMECLASS_%s_DEFINED\n", c_name
);
1907 fprintf(header
, "#if !defined(_MSC_VER) && !defined(__MINGW32__)\n");
1908 fprintf(header
, "static const WCHAR RuntimeClass_%s[] = {", c_name
);
1909 for (i
= 0, len
= strlen(name
); i
< len
; ++i
) fprintf(header
, "'%c',", name
[i
]);
1910 fprintf(header
, "0};\n");
1911 fprintf(header
, "#elif defined(__GNUC__) && !defined(__cplusplus)\n");
1912 /* FIXME: MIDL generates extern const here but GCC warns if extern is initialized */
1913 fprintf(header
, "const DECLSPEC_SELECTANY WCHAR RuntimeClass_%s[] = L\"%s\";\n", c_name
, name
);
1914 fprintf(header
, "#else\n");
1915 fprintf(header
, "extern const DECLSPEC_SELECTANY WCHAR RuntimeClass_%s[] = {", c_name
);
1916 for (i
= 0, len
= strlen(name
); i
< len
; ++i
) fprintf(header
, "'%c',", name
[i
]);
1917 fprintf(header
, "0};\n");
1918 fprintf(header
, "#endif\n");
1919 fprintf(header
, "#endif /* RUNTIMECLASS_%s_DEFINED */\n", c_name
);
1922 if (contract
) write_apicontract_guard_end(header
, contract
);
1923 fprintf(header
, "\n");
1926 static void write_runtimeclass_forward(FILE *header
, type_t
*runtimeclass
)
1928 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", runtimeclass
->c_name
);
1929 fprintf(header
, "#define __%s_FWD_DEFINED__\n", runtimeclass
->c_name
);
1930 fprintf(header
, "#ifdef __cplusplus\n");
1931 write_namespace_start(header
, runtimeclass
->namespace);
1932 write_line(header
, 0, "class %s;", runtimeclass
->name
);
1933 write_namespace_end(header
, runtimeclass
->namespace);
1934 fprintf(header
, "#else\n");
1935 fprintf(header
, "typedef struct %s %s;\n", runtimeclass
->c_name
, runtimeclass
->c_name
);
1936 fprintf(header
, "#endif /* defined __cplusplus */\n");
1937 fprintf(header
, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", runtimeclass
->c_name
);
1940 static void write_import(FILE *header
, const char *fname
)
1942 char *hname
= replace_extension( get_basename(fname
), ".idl", "" );
1944 if (!strendswith( hname
, ".h" )) hname
= strmake( "%s.h", hname
);
1945 fprintf(header
, "#include <%s>\n", hname
);
1949 static void write_imports(FILE *header
, const statement_list_t
*stmts
)
1951 const statement_t
*stmt
;
1952 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1957 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1958 write_imports(header
, type_iface_get_stmts(stmt
->u
.type
));
1961 case STMT_IMPORTLIB
:
1962 /* not included in header */
1965 write_import(header
, stmt
->u
.str
);
1971 case STMT_DECLARATION
:
1972 /* not processed here */
1975 write_imports(header
, stmt
->u
.lib
->stmts
);
1981 static void write_forward_decls(FILE *header
, const statement_list_t
*stmts
)
1983 const statement_t
*stmt
;
1984 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1989 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
|| type_get_type(stmt
->u
.type
) == TYPE_DELEGATE
)
1991 type_t
*iface
= stmt
->u
.type
;
1992 if (type_get_type(iface
) == TYPE_DELEGATE
) iface
= type_delegate_get_iface(iface
);
1993 if (is_object(iface
) || is_attr(iface
->attrs
, ATTR_DISPINTERFACE
))
1995 write_forward(header
, iface
);
1996 if (type_iface_get_async_iface(iface
))
1997 write_forward(header
, type_iface_get_async_iface(iface
));
2000 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
2001 write_coclass_forward(header
, stmt
->u
.type
);
2002 else if (type_get_type(stmt
->u
.type
) == TYPE_RUNTIMECLASS
)
2003 write_runtimeclass_forward(header
, stmt
->u
.type
);
2004 else if (type_get_type(stmt
->u
.type
) == TYPE_PARAMETERIZED_TYPE
)
2005 write_parameterized_type_forward(header
, stmt
->u
.type
);
2008 case STMT_IMPORTLIB
:
2009 /* not included in header */
2016 case STMT_DECLARATION
:
2017 /* not processed here */
2020 write_forward_decls(header
, stmt
->u
.lib
->stmts
);
2026 static void write_header_stmts(FILE *header
, const statement_list_t
*stmts
, const type_t
*iface
, int ignore_funcs
)
2028 const statement_t
*stmt
;
2029 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
2034 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
|| type_get_type(stmt
->u
.type
) == TYPE_DELEGATE
)
2036 type_t
*iface
= stmt
->u
.type
, *async_iface
;
2037 if (type_get_type(stmt
->u
.type
) == TYPE_DELEGATE
) iface
= type_delegate_get_iface(iface
);
2038 async_iface
= type_iface_get_async_iface(iface
);
2039 if (is_object(iface
)) is_object_interface
++;
2040 if (is_attr(stmt
->u
.type
->attrs
, ATTR_DISPINTERFACE
) || is_object(stmt
->u
.type
))
2042 write_com_interface_start(header
, iface
);
2043 write_header_stmts(header
, type_iface_get_stmts(iface
), stmt
->u
.type
, TRUE
);
2044 write_com_interface_end(header
, iface
);
2047 write_com_interface_start(header
, async_iface
);
2048 write_com_interface_end(header
, async_iface
);
2053 write_rpc_interface_start(header
, iface
);
2054 write_header_stmts(header
, type_iface_get_stmts(iface
), iface
, FALSE
);
2055 write_rpc_interface_end(header
, iface
);
2057 if (is_object(iface
)) is_object_interface
--;
2059 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
2060 write_coclass(header
, stmt
->u
.type
);
2061 else if (type_get_type(stmt
->u
.type
) == TYPE_APICONTRACT
)
2062 write_apicontract(header
, stmt
->u
.type
);
2063 else if (type_get_type(stmt
->u
.type
) == TYPE_RUNTIMECLASS
)
2064 write_runtimeclass(header
, stmt
->u
.type
);
2065 else if (type_get_type(stmt
->u
.type
) != TYPE_PARAMETERIZED_TYPE
)
2066 write_type_definition(header
, stmt
->u
.type
, stmt
->declonly
);
2069 is_object_interface
++;
2070 write_parameterized_implementation(header
, stmt
->u
.type
, stmt
->declonly
);
2071 is_object_interface
--;
2075 /* FIXME: shouldn't write out forward declarations for undefined
2076 * interfaces but a number of our IDL files depend on this */
2077 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
&& !stmt
->u
.type
->written
)
2078 write_forward(header
, stmt
->u
.type
);
2080 case STMT_IMPORTLIB
:
2083 /* not included in header */
2086 /* not processed here */
2091 if (stmt
->u
.type_list
) LIST_FOR_EACH_ENTRY(ref
, stmt
->u
.type_list
, typeref_t
, entry
)
2092 write_typedef(header
, ref
->type
, stmt
->declonly
);
2096 fprintf(header
, "#ifndef __%s_LIBRARY_DEFINED__\n", stmt
->u
.lib
->name
);
2097 fprintf(header
, "#define __%s_LIBRARY_DEFINED__\n", stmt
->u
.lib
->name
);
2098 write_library(header
, stmt
->u
.lib
);
2099 write_header_stmts(header
, stmt
->u
.lib
->stmts
, NULL
, FALSE
);
2100 fprintf(header
, "#endif /* __%s_LIBRARY_DEFINED__ */\n", stmt
->u
.lib
->name
);
2103 fprintf(header
, "%s\n", stmt
->u
.str
);
2105 case STMT_DECLARATION
:
2106 if (iface
&& type_get_type(stmt
->u
.var
->declspec
.type
) == TYPE_FUNCTION
)
2110 int prefixes_differ
= strcmp(prefix_client
, prefix_server
);
2112 if (prefixes_differ
)
2114 fprintf(header
, "/* client prototype */\n");
2115 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_client
);
2116 fprintf(header
, "/* server prototype */\n");
2118 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_server
);
2122 write_declaration(header
, stmt
->u
.var
);
2128 void write_header(const statement_list_t
*stmts
)
2132 if (!do_header
) return;
2134 if(!(header
= fopen(header_name
, "w"))) {
2135 error("Could not open %s for output\n", header_name
);
2138 fprintf(header
, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION
, input_name
);
2140 fprintf(header
, "#ifdef _WIN32\n");
2141 fprintf(header
, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
2142 fprintf(header
, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
2143 fprintf(header
, "#endif\n");
2144 fprintf(header
, "#include <rpc.h>\n" );
2145 fprintf(header
, "#include <rpcndr.h>\n" );
2146 if (!for_each_serializable(stmts
, NULL
, serializable_exists
))
2147 fprintf(header
, "#include <midles.h>\n" );
2148 fprintf(header
, "#endif\n\n");
2150 fprintf(header
, "#ifndef COM_NO_WINDOWS_H\n");
2151 fprintf(header
, "#include <windows.h>\n");
2152 fprintf(header
, "#include <ole2.h>\n");
2153 fprintf(header
, "#endif\n\n");
2155 fprintf(header
, "#ifndef __%s__\n", header_token
);
2156 fprintf(header
, "#define __%s__\n\n", header_token
);
2158 fprintf(header
, "#ifndef __WIDL_INLINE\n");
2159 fprintf(header
, "#if defined(__cplusplus) || defined(_MSC_VER)\n");
2160 fprintf(header
, "#define __WIDL_INLINE inline\n");
2161 fprintf(header
, "#elif defined(__GNUC__)\n");
2162 fprintf(header
, "#define __WIDL_INLINE __inline__\n");
2163 fprintf(header
, "#endif\n");
2164 fprintf(header
, "#endif\n\n");
2166 fprintf(header
, "/* Forward declarations */\n\n");
2167 write_forward_decls(header
, stmts
);
2169 fprintf(header
, "/* Headers for imported files */\n\n");
2170 write_imports(header
, stmts
);
2171 fprintf(header
, "\n");
2172 start_cplusplus_guard(header
);
2174 write_header_stmts(header
, stmts
, NULL
, FALSE
);
2176 fprintf(header
, "/* Begin additional prototypes for all interfaces */\n");
2177 fprintf(header
, "\n");
2178 for_each_serializable(stmts
, header
, write_serialize_function_decl
);
2179 write_user_types(header
);
2180 write_generic_handle_routines(header
);
2181 write_context_handle_rundowns(header
);
2182 fprintf(header
, "\n");
2183 fprintf(header
, "/* End additional prototypes */\n");
2184 fprintf(header
, "\n");
2186 end_cplusplus_guard(header
);
2187 fprintf(header
, "#endif /* __%s__ */\n", header_token
);