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
40 static int indentation
= 0;
41 static int is_object_interface
= 0;
42 user_type_list_t user_type_list
= LIST_INIT(user_type_list
);
43 context_handle_list_t context_handle_list
= LIST_INIT(context_handle_list
);
44 generic_handle_list_t generic_handle_list
= LIST_INIT(generic_handle_list
);
46 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
);
48 static void write_apicontract_guard_start(FILE *header
, const expr_t
*expr
);
49 static void write_apicontract_guard_end(FILE *header
, const expr_t
*expr
);
51 static void indent(FILE *h
, int delta
)
54 if (delta
< 0) indentation
+= delta
;
55 for (c
=0; c
<indentation
; c
++) fprintf(h
, " ");
56 if (delta
> 0) indentation
+= delta
;
59 static void write_line(FILE *f
, int delta
, const char *fmt
, ...)
69 int is_ptrchain_attr(const var_t
*var
, enum attr_type t
)
71 if (is_attr(var
->attrs
, t
))
75 type_t
*type
= var
->declspec
.type
;
78 if (is_attr(type
->attrs
, t
))
80 else if (type_is_alias(type
))
81 type
= type_alias_get_aliasee_type(type
);
82 else if (is_ptr(type
))
83 type
= type_pointer_get_ref_type(type
);
89 int is_aliaschain_attr(const type_t
*type
, enum attr_type attr
)
91 const type_t
*t
= type
;
94 if (is_attr(t
->attrs
, attr
))
96 else if (type_is_alias(t
))
97 t
= type_alias_get_aliasee_type(t
);
102 int is_attr(const attr_list_t
*list
, enum attr_type t
)
105 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
106 if (attr
->type
== t
) return 1;
110 void *get_attrp(const attr_list_t
*list
, enum attr_type t
)
113 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
114 if (attr
->type
== t
) return attr
->u
.pval
;
118 unsigned int get_attrv(const attr_list_t
*list
, enum attr_type t
)
121 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
122 if (attr
->type
== t
) return attr
->u
.ival
;
126 static void write_guid(FILE *f
, const char *guid_prefix
, const char *name
, const UUID
*uuid
)
129 fprintf(f
, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
130 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
131 guid_prefix
, name
, uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0],
132 uuid
->Data4
[1], uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5],
133 uuid
->Data4
[6], uuid
->Data4
[7]);
136 static void write_uuid_decl(FILE *f
, type_t
*type
, const UUID
*uuid
)
138 char *name
= format_namespace(type
->namespace, "", "::", type
->name
, use_abi_namespace
? "ABI" : NULL
);
139 fprintf(f
, "#ifdef __CRT_UUID_DECL\n");
140 fprintf(f
, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
141 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
142 name
, uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
143 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6],
145 fprintf(f
, "#endif\n");
149 static const char *uuid_string(const UUID
*uuid
)
153 sprintf(buf
, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
154 uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1], uuid
->Data4
[2],
155 uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6], uuid
->Data4
[7]);
160 static void write_namespace_start(FILE *header
, struct namespace *namespace)
162 if(is_global_namespace(namespace)) {
163 if(use_abi_namespace
)
164 write_line(header
, 1, "namespace ABI {");
168 write_namespace_start(header
, namespace->parent
);
169 write_line(header
, 1, "namespace %s {", namespace->name
);
172 static void write_namespace_end(FILE *header
, struct namespace *namespace)
174 if(is_global_namespace(namespace)) {
175 if(use_abi_namespace
)
176 write_line(header
, -1, "}", namespace->name
);
180 write_line(header
, -1, "}", namespace->name
);
181 write_namespace_end(header
, namespace->parent
);
184 const char *get_name(const var_t
*v
)
188 if (is_attr( v
->attrs
, ATTR_EVENTADD
))
189 return buffer
= strmake( "add_%s", v
->name
);
190 if (is_attr( v
->attrs
, ATTR_EVENTREMOVE
))
191 return buffer
= strmake( "remove_%s", v
->name
);
192 if (is_attr( v
->attrs
, ATTR_PROPGET
))
193 return buffer
= strmake( "get_%s", v
->name
);
194 if (is_attr( v
->attrs
, ATTR_PROPPUT
))
195 return buffer
= strmake( "put_%s", v
->name
);
196 if (is_attr( v
->attrs
, ATTR_PROPPUTREF
))
197 return buffer
= strmake( "putref_%s", v
->name
);
202 static void write_fields(FILE *h
, var_list_t
*fields
, enum name_type name_type
)
204 unsigned nameless_struct_cnt
= 0, nameless_struct_i
= 0, nameless_union_cnt
= 0, nameless_union_i
= 0;
211 LIST_FOR_EACH_ENTRY( v
, fields
, var_t
, entry
) {
212 if (!v
|| !v
->declspec
.type
) continue;
214 switch(type_get_type_detect_alias(v
->declspec
.type
)) {
216 case TYPE_ENCAPSULATED_UNION
:
217 nameless_struct_cnt
++;
220 nameless_union_cnt
++;
227 LIST_FOR_EACH_ENTRY( v
, fields
, var_t
, entry
) {
228 expr_t
*contract
= get_attrp(v
->attrs
, ATTR_CONTRACT
);
229 if (!v
|| !v
->declspec
.type
) continue;
230 if (contract
) write_apicontract_guard_start(h
, contract
);
235 switch(type_get_type_detect_alias(v
->declspec
.type
)) {
237 case TYPE_ENCAPSULATED_UNION
:
239 fprintf(h
, "__C89_NAMELESS ");
240 if(nameless_struct_cnt
== 1) {
241 name
= "__C89_NAMELESSSTRUCTNAME";
242 }else if(nameless_struct_i
< 5 /* # of supporting macros */) {
243 sprintf(buf
, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i
);
250 fprintf(h
, "__C89_NAMELESS ");
251 if(nameless_union_cnt
== 1) {
252 name
= "__C89_NAMELESSUNIONNAME";
253 }else if(nameless_union_i
< 8 /* # of supporting macros */ ) {
254 sprintf(buf
, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i
);
262 write_type_v(h
, &v
->declspec
, TRUE
, v
->declonly
, name
, name_type
);
264 if (contract
) write_apicontract_guard_end(h
, contract
);
268 static void write_enums(FILE *h
, var_list_t
*enums
, const char *enum_name
)
272 LIST_FOR_EACH_ENTRY( v
, enums
, var_t
, entry
)
274 expr_t
*contract
= get_attrp(v
->attrs
, ATTR_CONTRACT
);
275 if (contract
) write_apicontract_guard_start(h
, contract
);
279 fprintf(h
, "%s", get_name(v
));
281 fprintf(h
, "%s_%s", enum_name
, get_name(v
));
284 write_expr(h
, v
->eval
, 0, 1, NULL
, NULL
, "");
287 if (list_next( enums
, &v
->entry
)) fprintf(h
, ",\n");
288 else fprintf(h
, "\n");
289 if (contract
) write_apicontract_guard_end(h
, contract
);
293 int needs_space_after(type_t
*t
)
295 return (type_is_alias(t
) ||
296 (!is_ptr(t
) && (!is_array(t
) || !type_array_is_decl_as_ptr(t
) || t
->name
)));
299 static int decl_needs_parens(const type_t
*t
)
301 if (type_is_alias(t
))
303 if (is_array(t
) && !type_array_is_decl_as_ptr(t
))
308 static void write_pointer_left(FILE *h
, type_t
*ref
)
310 if (needs_space_after(ref
))
312 if (decl_needs_parens(ref
))
314 if (type_get_type_detect_alias(ref
) == TYPE_FUNCTION
)
316 const char *callconv
= get_attrp(ref
->attrs
, ATTR_CALLCONV
);
317 if (!callconv
&& is_object_interface
) callconv
= "STDMETHODCALLTYPE";
318 if (callconv
) fprintf(h
, "%s ", callconv
);
323 void write_type_left(FILE *h
, const decl_spec_t
*ds
, enum name_type name_type
, int declonly
, int write_callconv
)
325 type_t
*t
= ds
->type
;
330 name
= type_get_name(t
, name_type
);
332 if (ds
->func_specifier
& FUNCTION_SPECIFIER_INLINE
)
333 fprintf(h
, "inline ");
335 if ((ds
->qualifier
& TYPE_QUALIFIER_CONST
) && (type_is_alias(t
) || !is_ptr(t
)))
336 fprintf(h
, "const ");
338 if (!winrt_mode
&& type_is_alias(t
)) fprintf(h
, "%s", t
->name
);
340 switch (type_get_type_detect_alias(t
)) {
342 if (!declonly
&& !t
->written
) {
344 if (name
) fprintf(h
, "enum %s {\n", name
);
345 else fprintf(h
, "enum {\n");
348 write_enums(h
, type_enum_get_values(t
), is_global_namespace(t
->namespace) ? NULL
: t
->name
);
352 else fprintf(h
, "enum %s", name
? name
: "");
355 case TYPE_ENCAPSULATED_UNION
:
356 if (!declonly
&& !t
->written
) {
358 if (name
) fprintf(h
, "struct %s {\n", name
);
359 else fprintf(h
, "struct {\n");
362 if (type_get_type(t
) != TYPE_STRUCT
)
363 write_fields(h
, type_encapsulated_union_get_fields(t
), name_type
);
365 write_fields(h
, type_struct_get_fields(t
), name_type
);
369 else fprintf(h
, "struct %s", name
? name
: "");
372 if (!declonly
&& !t
->written
) {
374 if (t
->name
) fprintf(h
, "union %s {\n", t
->name
);
375 else fprintf(h
, "union {\n");
378 write_fields(h
, type_union_get_cases(t
), name_type
);
382 else fprintf(h
, "union %s", t
->name
? t
->name
: "");
386 write_type_left(h
, type_pointer_get_ref(t
), name_type
, declonly
, FALSE
);
387 write_pointer_left(h
, type_pointer_get_ref_type(t
));
388 if (ds
->qualifier
& TYPE_QUALIFIER_CONST
) fprintf(h
, "const ");
392 if (t
->name
&& type_array_is_decl_as_ptr(t
))
393 fprintf(h
, "%s", t
->name
);
396 write_type_left(h
, type_array_get_element(t
), name_type
, declonly
, !type_array_is_decl_as_ptr(t
));
397 if (type_array_is_decl_as_ptr(t
))
398 write_pointer_left(h
, type_array_get_element_type(t
));
403 write_type_left(h
, type_function_get_ret(t
), name_type
, declonly
, TRUE
);
405 /* A pointer to a function has to write the calling convention inside
406 * the parentheses. There's no way to handle that here, so we have to
407 * use an extra parameter to tell us whether to write the calling
408 * convention or not. */
411 const char *callconv
= get_attrp(t
->attrs
, ATTR_CALLCONV
);
412 if (!callconv
&& is_object_interface
) callconv
= "STDMETHODCALLTYPE";
413 if (callconv
) fprintf(h
, " %s ", callconv
);
418 if (type_basic_get_type(t
) != TYPE_BASIC_INT32
&&
419 type_basic_get_type(t
) != TYPE_BASIC_INT64
&&
420 type_basic_get_type(t
) != TYPE_BASIC_LONG
&&
421 type_basic_get_type(t
) != TYPE_BASIC_HYPER
)
423 if (type_basic_get_sign(t
) < 0) fprintf(h
, "signed ");
424 else if (type_basic_get_sign(t
) > 0) fprintf(h
, "unsigned ");
426 switch (type_basic_get_type(t
))
428 case TYPE_BASIC_INT8
: fprintf(h
, "small"); break;
429 case TYPE_BASIC_INT16
: fprintf(h
, "short"); break;
430 case TYPE_BASIC_INT
: fprintf(h
, "int"); break;
431 case TYPE_BASIC_INT3264
: fprintf(h
, "__int3264"); break;
432 case TYPE_BASIC_BYTE
: fprintf(h
, "byte"); break;
433 case TYPE_BASIC_CHAR
: fprintf(h
, "char"); break;
434 case TYPE_BASIC_WCHAR
: fprintf(h
, "wchar_t"); break;
435 case TYPE_BASIC_FLOAT
: fprintf(h
, "float"); break;
436 case TYPE_BASIC_DOUBLE
: fprintf(h
, "double"); break;
437 case TYPE_BASIC_ERROR_STATUS_T
: fprintf(h
, "error_status_t"); break;
438 case TYPE_BASIC_HANDLE
: fprintf(h
, "handle_t"); break;
439 case TYPE_BASIC_INT32
:
440 if (type_basic_get_sign(t
) > 0)
441 fprintf(h
, "UINT32");
445 case TYPE_BASIC_LONG
:
446 if (type_basic_get_sign(t
) > 0)
451 case TYPE_BASIC_INT64
:
452 if (type_basic_get_sign(t
) > 0)
453 fprintf(h
, "UINT64");
457 case TYPE_BASIC_HYPER
:
458 if (type_basic_get_sign(t
) > 0)
459 fprintf(h
, "MIDL_uhyper");
468 fprintf(h
, "%s", name
);
470 case TYPE_RUNTIMECLASS
:
471 fprintf(h
, "%s", type_get_name(type_runtimeclass_get_default_iface(t
), name_type
));
478 const decl_spec_t ds
= {.type
= type_bitfield_get_field(t
)};
479 write_type_left(h
, &ds
, name_type
, declonly
, TRUE
);
484 const decl_spec_t
*ds
= type_alias_get_aliasee(t
);
485 int in_namespace
= ds
&& ds
->type
&& ds
->type
->namespace && !is_global_namespace(ds
->type
->namespace);
486 if (!in_namespace
) fprintf(h
, "%s", t
->name
);
487 else write_type_left(h
, ds
, name_type
, declonly
, write_callconv
);
490 case TYPE_APICONTRACT
:
491 case TYPE_PARAMETERIZED_TYPE
:
493 /* shouldn't be here */
500 void write_type_right(FILE *h
, type_t
*t
, int is_field
)
503 if (type_is_alias(t
)) return;
505 switch (type_get_type(t
))
509 type_t
*elem
= type_array_get_element_type(t
);
510 if (type_array_is_decl_as_ptr(t
))
512 if (decl_needs_parens(elem
))
517 if (is_conformant_array(t
))
518 fprintf(h
, "[%s]", is_field
? "1" : "");
520 fprintf(h
, "[%u]", type_array_get_dim(t
));
522 write_type_right(h
, elem
, FALSE
);
527 const var_list_t
*args
= type_function_get_args(t
);
529 if (args
) write_args(h
, args
, NULL
, 0, FALSE
, NAME_DEFAULT
);
533 write_type_right(h
, type_function_get_rettype(t
), FALSE
);
538 type_t
*ref
= type_pointer_get_ref_type(t
);
539 if (decl_needs_parens(ref
))
541 write_type_right(h
, ref
, FALSE
);
545 fprintf(h
, " : %u", type_bitfield_get_bits(t
)->cval
);
551 case TYPE_ENCAPSULATED_UNION
:
557 case TYPE_RUNTIMECLASS
:
559 case TYPE_APICONTRACT
:
560 case TYPE_PARAMETERIZED_TYPE
:
562 /* not supposed to be here */
568 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
)
570 type_t
*t
= ds
->type
;
574 if (t
) write_type_left(h
, ds
, name_type
, declonly
, TRUE
);
576 if (name
) fprintf(h
, "%s%s", !t
|| needs_space_after(t
) ? " " : "", name
);
579 write_type_right(h
, t
, is_field
);
582 static void write_type_definition(FILE *f
, type_t
*t
, int declonly
)
584 int in_namespace
= t
->namespace && !is_global_namespace(t
->namespace);
585 int save_written
= t
->written
;
586 decl_spec_t ds
= {.type
= t
};
587 expr_t
*contract
= get_attrp(t
->attrs
, ATTR_CONTRACT
);
589 if (contract
) write_apicontract_guard_start(f
, contract
);
591 fprintf(f
, "#ifdef __cplusplus\n");
592 fprintf(f
, "} /* extern \"C\" */\n");
593 write_namespace_start(f
, t
->namespace);
596 write_type_left(f
, &ds
, NAME_DEFAULT
, declonly
, TRUE
);
599 t
->written
= save_written
;
600 write_namespace_end(f
, t
->namespace);
601 fprintf(f
, "extern \"C\" {\n");
602 fprintf(f
, "#else\n");
603 write_type_left(f
, &ds
, NAME_C
, declonly
, TRUE
);
605 fprintf(f
, "#endif\n\n");
607 if (contract
) write_apicontract_guard_end(f
, contract
);
610 void write_type_decl(FILE *f
, const decl_spec_t
*t
, const char *name
)
612 write_type_v(f
, t
, FALSE
, TRUE
, name
, NAME_DEFAULT
);
615 void write_type_decl_left(FILE *f
, const decl_spec_t
*ds
)
617 write_type_left(f
, ds
, NAME_DEFAULT
, TRUE
, TRUE
);
620 static int user_type_registered(const char *name
)
623 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
624 if (!strcmp(name
, ut
->name
))
629 static int context_handle_registered(const char *name
)
631 context_handle_t
*ch
;
632 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
633 if (!strcmp(name
, ch
->name
))
638 static int generic_handle_registered(const char *name
)
640 generic_handle_t
*gh
;
641 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
642 if (!strcmp(name
, gh
->name
))
647 unsigned int get_context_handle_offset( const type_t
*type
)
649 context_handle_t
*ch
;
650 unsigned int index
= 0;
652 while (!is_attr( type
->attrs
, ATTR_CONTEXTHANDLE
))
654 if (type_is_alias( type
)) type
= type_alias_get_aliasee_type( type
);
655 else if (is_ptr( type
)) type
= type_pointer_get_ref_type( type
);
656 else error( "internal error: %s is not a context handle\n", type
->name
);
658 LIST_FOR_EACH_ENTRY( ch
, &context_handle_list
, context_handle_t
, entry
)
660 if (!strcmp( type
->name
, ch
->name
)) return index
;
663 error( "internal error: %s is not registered as a context handle\n", type
->name
);
667 unsigned int get_generic_handle_offset( const type_t
*type
)
669 generic_handle_t
*gh
;
670 unsigned int index
= 0;
672 while (!is_attr( type
->attrs
, ATTR_HANDLE
))
674 if (type_is_alias( type
)) type
= type_alias_get_aliasee_type( type
);
675 else if (is_ptr( type
)) type
= type_pointer_get_ref_type( type
);
676 else error( "internal error: %s is not a generic handle\n", type
->name
);
678 LIST_FOR_EACH_ENTRY( gh
, &generic_handle_list
, generic_handle_t
, entry
)
680 if (!strcmp( type
->name
, gh
->name
)) return index
;
683 error( "internal error: %s is not registered as a generic handle\n", type
->name
);
687 /* check for types which require additional prototypes to be generated in the
689 void check_for_additional_prototype_types(type_t
*type
)
693 const char *name
= type
->name
;
694 if (type
->user_types_registered
) break;
695 type
->user_types_registered
= 1;
696 if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
)) {
697 if (!context_handle_registered(name
))
699 context_handle_t
*ch
= xmalloc(sizeof(*ch
));
700 ch
->name
= xstrdup(name
);
701 list_add_tail(&context_handle_list
, &ch
->entry
);
703 /* don't carry on parsing fields within this type */
706 if ((type_get_type(type
) != TYPE_BASIC
||
707 type_basic_get_type(type
) != TYPE_BASIC_HANDLE
) &&
708 is_attr(type
->attrs
, ATTR_HANDLE
)) {
709 if (!generic_handle_registered(name
))
711 generic_handle_t
*gh
= xmalloc(sizeof(*gh
));
712 gh
->name
= xstrdup(name
);
713 list_add_tail(&generic_handle_list
, &gh
->entry
);
715 /* don't carry on parsing fields within this type */
718 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
)) {
719 if (!user_type_registered(name
))
721 user_type_t
*ut
= xmalloc(sizeof *ut
);
722 ut
->name
= xstrdup(name
);
723 list_add_tail(&user_type_list
, &ut
->entry
);
725 /* don't carry on parsing fields within this type as we are already
726 * using a wire marshaled type */
729 else if (type_is_complete(type
))
733 switch (type_get_type_detect_alias(type
))
736 vars
= type_enum_get_values(type
);
739 vars
= type_struct_get_fields(type
);
742 vars
= type_union_get_cases(type
);
748 if (vars
) LIST_FOR_EACH_ENTRY( v
, vars
, const var_t
, entry
)
749 check_for_additional_prototype_types(v
->declspec
.type
);
752 if (type_is_alias(type
))
753 type
= type_alias_get_aliasee_type(type
);
754 else if (is_ptr(type
))
755 type
= type_pointer_get_ref_type(type
);
756 else if (is_array(type
))
757 type
= type_array_get_element_type(type
);
763 static int write_serialize_function_decl(FILE *header
, const type_t
*type
)
765 write_serialize_functions(header
, type
, NULL
);
769 static int serializable_exists(FILE *header
, const type_t
*type
)
774 static int for_each_serializable(const statement_list_t
*stmts
, FILE *header
,
775 int (*proc
)(FILE*, const type_t
*))
777 statement_t
*stmt
, *iface_stmt
;
778 statement_list_t
*iface_stmts
;
781 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, statement_t
, entry
)
783 if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
786 iface_stmts
= type_iface_get_stmts(stmt
->u
.type
);
787 if (iface_stmts
) LIST_FOR_EACH_ENTRY( iface_stmt
, iface_stmts
, statement_t
, entry
)
789 if (iface_stmt
->type
!= STMT_TYPEDEF
) continue;
790 if (iface_stmt
->u
.type_list
) LIST_FOR_EACH_ENTRY(ref
, iface_stmt
->u
.type_list
, typeref_t
, entry
)
792 if (!is_attr(ref
->type
->attrs
, ATTR_ENCODE
)
793 && !is_attr(ref
->type
->attrs
, ATTR_DECODE
))
795 if (!proc(header
, ref
->type
))
804 static void write_user_types(FILE *header
)
807 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
809 const char *name
= ut
->name
;
810 fprintf(header
, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name
, name
);
811 fprintf(header
, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name
, name
);
812 fprintf(header
, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name
, name
);
813 fprintf(header
, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name
, name
);
817 static void write_context_handle_rundowns(FILE *header
)
819 context_handle_t
*ch
;
820 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
822 const char *name
= ch
->name
;
823 fprintf(header
, "void __RPC_USER %s_rundown(%s);\n", name
, name
);
827 static void write_generic_handle_routines(FILE *header
)
829 generic_handle_t
*gh
;
830 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
832 const char *name
= gh
->name
;
833 fprintf(header
, "handle_t __RPC_USER %s_bind(%s);\n", name
, name
);
834 fprintf(header
, "void __RPC_USER %s_unbind(%s, handle_t);\n", name
, name
);
838 static void write_typedef(FILE *header
, type_t
*type
, int declonly
)
840 type_t
*t
= type_alias_get_aliasee_type(type
);
841 if (winrt_mode
&& t
->namespace && !is_global_namespace(t
->namespace)) return;
842 fprintf(header
, "typedef ");
843 write_type_v(header
, type_alias_get_aliasee(type
), FALSE
, declonly
, type
->name
, NAME_DEFAULT
);
844 fprintf(header
, ";\n");
847 int is_const_decl(const var_t
*var
)
849 const decl_spec_t
*t
;
850 /* strangely, MIDL accepts a const attribute on any pointer in the
851 * declaration to mean that data isn't being instantiated. this appears
852 * to be a bug, but there is no benefit to being incompatible with MIDL,
853 * so we'll do the same thing */
854 for (t
= &var
->declspec
; ; )
856 if (t
->qualifier
& TYPE_QUALIFIER_CONST
)
858 else if (is_ptr(t
->type
))
859 t
= type_pointer_get_ref(t
->type
);
865 static void write_declaration(FILE *header
, const var_t
*v
)
867 if (is_const_decl(v
) && v
->eval
)
869 fprintf(header
, "#define %s (", v
->name
);
870 write_expr(header
, v
->eval
, 0, 1, NULL
, NULL
, "");
871 fprintf(header
, ")\n\n");
875 switch (v
->declspec
.stgclass
)
878 case STG_REGISTER
: /* ignored */
881 fprintf(header
, "static ");
884 fprintf(header
, "extern ");
887 write_type_v(header
, &v
->declspec
, FALSE
, v
->declonly
, v
->name
, NAME_DEFAULT
);
888 fprintf(header
, ";\n\n");
892 static void write_library(FILE *header
, const typelib_t
*typelib
)
894 const UUID
*uuid
= get_attrp(typelib
->attrs
, ATTR_UUID
);
895 fprintf(header
, "\n");
896 write_guid(header
, "LIBID", typelib
->name
, uuid
);
897 fprintf(header
, "\n");
901 const type_t
* get_explicit_generic_handle_type(const var_t
* var
)
904 for (t
= var
->declspec
.type
;
905 is_ptr(t
) || type_is_alias(t
);
906 t
= type_is_alias(t
) ? type_alias_get_aliasee_type(t
) : type_pointer_get_ref_type(t
))
907 if ((type_get_type_detect_alias(t
) != TYPE_BASIC
|| type_basic_get_type(t
) != TYPE_BASIC_HANDLE
) &&
908 is_attr(t
->attrs
, ATTR_HANDLE
))
913 const var_t
*get_func_handle_var( const type_t
*iface
, const var_t
*func
,
914 unsigned char *explicit_fc
, unsigned char *implicit_fc
)
917 const var_list_t
*args
= type_function_get_args( func
->declspec
.type
);
919 *explicit_fc
= *implicit_fc
= 0;
920 if (args
) LIST_FOR_EACH_ENTRY( var
, args
, const var_t
, entry
)
922 if (!is_attr( var
->attrs
, ATTR_IN
) && is_attr( var
->attrs
, ATTR_OUT
)) continue;
923 if (type_get_type( var
->declspec
.type
) == TYPE_BASIC
&& type_basic_get_type( var
->declspec
.type
) == TYPE_BASIC_HANDLE
)
925 *explicit_fc
= FC_BIND_PRIMITIVE
;
928 if (get_explicit_generic_handle_type( var
))
930 *explicit_fc
= FC_BIND_GENERIC
;
933 if (is_context_handle( var
->declspec
.type
))
935 *explicit_fc
= FC_BIND_CONTEXT
;
940 if ((var
= get_attrp( iface
->attrs
, ATTR_IMPLICIT_HANDLE
)))
942 if (type_get_type( var
->declspec
.type
) == TYPE_BASIC
&&
943 type_basic_get_type( var
->declspec
.type
) == TYPE_BASIC_HANDLE
)
944 *implicit_fc
= FC_BIND_PRIMITIVE
;
946 *implicit_fc
= FC_BIND_GENERIC
;
950 *implicit_fc
= FC_AUTO_HANDLE
;
954 int has_out_arg_or_return(const var_t
*func
)
958 if (!is_void(type_function_get_rettype(func
->declspec
.type
)))
961 if (!type_function_get_args(func
->declspec
.type
))
964 LIST_FOR_EACH_ENTRY( var
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
965 if (is_attr(var
->attrs
, ATTR_OUT
))
972 /********** INTERFACES **********/
974 int is_object(const type_t
*iface
)
977 if (type_is_defined(iface
) && type_iface_get_inherit(iface
))
979 if (iface
->attrs
) LIST_FOR_EACH_ENTRY( attr
, iface
->attrs
, const attr_t
, entry
)
980 if (attr
->type
== ATTR_OBJECT
|| attr
->type
== ATTR_ODL
) return 1;
984 int is_local(const attr_list_t
*a
)
986 return is_attr(a
, ATTR_LOCAL
);
989 const var_t
*is_callas(const attr_list_t
*a
)
991 return get_attrp(a
, ATTR_CALLAS
);
994 static int is_inherited_method(const type_t
*iface
, const var_t
*func
)
996 while ((iface
= type_iface_get_inherit(iface
)))
998 const statement_t
*stmt
;
999 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1001 const var_t
*funccmp
= stmt
->u
.var
;
1003 if (!is_callas(func
->attrs
))
1005 char inherit_name
[256];
1006 /* compare full name including property prefix */
1007 strcpy(inherit_name
, get_name(funccmp
));
1008 if (!strcmp(inherit_name
, get_name(func
))) return 1;
1016 static int is_override_method(const type_t
*iface
, const type_t
*child
, const var_t
*func
)
1023 const statement_t
*stmt
;
1024 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(child
))
1026 const var_t
*funccmp
= stmt
->u
.var
;
1028 if (!is_callas(func
->attrs
))
1030 char inherit_name
[256];
1031 /* compare full name including property prefix */
1032 strcpy(inherit_name
, get_name(funccmp
));
1033 if (!strcmp(inherit_name
, get_name(func
))) return 1;
1037 while ((child
= type_iface_get_inherit(child
)) && child
!= iface
);
1042 static int is_aggregate_return(const var_t
*func
)
1044 enum type_type type
= type_get_type(type_function_get_rettype(func
->declspec
.type
));
1045 return type
== TYPE_STRUCT
|| type
== TYPE_UNION
||
1046 type
== TYPE_COCLASS
|| type
== TYPE_INTERFACE
||
1047 type
== TYPE_RUNTIMECLASS
;
1050 static char *get_vtbl_entry_name(const type_t
*iface
, const var_t
*func
)
1052 static char buff
[255];
1053 if (is_inherited_method(iface
, func
))
1054 sprintf(buff
, "%s_%s", iface
->name
, get_name(func
));
1056 sprintf(buff
, "%s", get_name(func
));
1060 static void write_method_macro(FILE *header
, const type_t
*iface
, const type_t
*child
, const char *name
)
1062 const statement_t
*stmt
;
1063 int first_iface
= 1;
1065 if (type_iface_get_inherit(iface
))
1066 write_method_macro(header
, type_iface_get_inherit(iface
), child
, name
);
1068 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1070 const var_t
*func
= stmt
->u
.var
;
1074 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1078 if (is_override_method(iface
, child
, func
))
1081 if (!is_callas(func
->attrs
)) {
1084 fprintf(header
, "#define %s_%s(This", name
, get_name(func
));
1085 if (type_function_get_args(func
->declspec
.type
))
1086 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1087 fprintf(header
, ",%s", arg
->name
);
1088 fprintf(header
, ") ");
1090 if (is_aggregate_return(func
))
1092 fprintf(header
, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name
, get_name(func
));
1096 fprintf(header
, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface
, func
));
1097 if (type_function_get_args(func
->declspec
.type
))
1098 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1099 fprintf(header
, ",%s", arg
->name
);
1100 fprintf(header
, ")\n");
1105 void write_args(FILE *h
, const var_list_t
*args
, const char *name
, int method
, int do_indent
, enum name_type name_type
)
1116 fprintf(h
, "%s* This", name
);
1119 if (args
) LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
) {
1126 else fprintf(h
, ",");
1128 /* In theory we should be writing the definition using write_type_v(..., arg->declonly),
1129 * but that causes redefinition in e.g. proxy files. In fact MIDL disallows
1130 * defining UDTs inside of an argument list. */
1131 write_type_v(h
, &arg
->declspec
, FALSE
, TRUE
, arg
->name
, name_type
);
1133 const expr_t
*expr
= get_attrp(arg
->attrs
, ATTR_DEFAULTVALUE
);
1135 const var_t
*tail_arg
;
1137 /* Output default value only if all following arguments also have default value. */
1138 LIST_FOR_EACH_ENTRY_REV( tail_arg
, args
, const var_t
, entry
) {
1139 if(tail_arg
== arg
) {
1142 /* Fixup the expression type for a BSTR like midl does. */
1143 if (get_type_vt(arg
->declspec
.type
) == VT_BSTR
&& expr
->type
== EXPR_STRLIT
)
1146 bstr
.type
= EXPR_WSTRLIT
;
1151 write_expr( h
, expr
, 0, 1, NULL
, NULL
, "" );
1154 if(!get_attrp(tail_arg
->attrs
, ATTR_DEFAULTVALUE
))
1161 if (do_indent
) indentation
--;
1164 static void write_cpp_method_def(FILE *header
, const type_t
*iface
)
1166 const statement_t
*stmt
;
1168 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1170 const var_t
*func
= stmt
->u
.var
;
1171 if (!is_callas(func
->attrs
)) {
1172 const decl_spec_t
*ret
= type_function_get_ret(func
->declspec
.type
);
1173 const char *callconv
= get_attrp(func
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1174 const var_list_t
*args
= type_function_get_args(func
->declspec
.type
);
1177 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1179 if (is_aggregate_return(func
)) {
1180 fprintf(header
, "#ifdef WIDL_EXPLICIT_AGGREGATE_RETURNS\n");
1183 fprintf(header
, "virtual ");
1184 write_type_decl_left(header
, ret
);
1185 fprintf(header
, "* %s %s(\n", callconv
, get_name(func
));
1188 write_type_decl_left(header
, ret
);
1189 fprintf(header
, " *__ret");
1192 fprintf(header
, ",\n");
1193 write_args(header
, args
, iface
->name
, 2, TRUE
, NAME_DEFAULT
);
1195 fprintf(header
, ") = 0;\n");
1198 write_type_decl_left(header
, ret
);
1199 fprintf(header
, " %s %s(\n", callconv
, get_name(func
));
1200 write_args(header
, args
, iface
->name
, 2, TRUE
, NAME_DEFAULT
);
1201 fprintf(header
, ")\n");
1203 fprintf(header
, "{\n");
1206 write_type_decl_left(header
, ret
);
1207 fprintf(header
, " __ret;\n");
1209 fprintf(header
, "return *%s(&__ret", get_name(func
));
1211 LIST_FOR_EACH_ENTRY(arg
, args
, const var_t
, entry
)
1212 fprintf(header
, ", %s", arg
->name
);
1213 fprintf(header
, ");\n");
1216 fprintf(header
, "}\n");
1218 fprintf(header
, "#else\n");
1222 fprintf(header
, "virtual ");
1223 write_type_decl_left(header
, ret
);
1224 fprintf(header
, " %s %s(\n", callconv
, get_name(func
));
1225 write_args(header
, args
, iface
->name
, 2, TRUE
, NAME_DEFAULT
);
1226 fprintf(header
, ") = 0;\n");
1228 if (is_aggregate_return(func
))
1229 fprintf(header
, "#endif\n");
1230 fprintf(header
, "\n");
1235 static void write_inline_wrappers(FILE *header
, const type_t
*iface
, const type_t
*child
, const char *name
)
1237 const statement_t
*stmt
;
1238 int first_iface
= 1;
1240 if (type_iface_get_inherit(iface
))
1241 write_inline_wrappers(header
, type_iface_get_inherit(iface
), child
, name
);
1243 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1245 const var_t
*func
= stmt
->u
.var
;
1249 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1253 if (is_override_method(iface
, child
, func
))
1256 if (!is_callas(func
->attrs
)) {
1259 fprintf(header
, "static FORCEINLINE ");
1260 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1261 fprintf(header
, " %s_%s(", name
, get_name(func
));
1262 write_args(header
, type_function_get_args(func
->declspec
.type
), name
, 1, FALSE
, NAME_C
);
1263 fprintf(header
, ") {\n");
1265 if (!is_aggregate_return(func
)) {
1267 fprintf(header
, "%sThis->lpVtbl->%s(This",
1268 is_void(type_function_get_rettype(func
->declspec
.type
)) ? "" : "return ",
1269 get_vtbl_entry_name(iface
, func
));
1272 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1273 fprintf(header
, " __ret;\n");
1275 fprintf(header
, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface
, func
));
1277 if (type_function_get_args(func
->declspec
.type
))
1278 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1279 fprintf(header
, ",%s", arg
->name
);
1280 fprintf(header
, ");\n");
1282 fprintf(header
, "}\n");
1287 static void do_write_c_method_def(FILE *header
, const type_t
*iface
, const char *name
)
1289 const statement_t
*stmt
;
1290 int first_iface
= 1;
1292 if (type_iface_get_inherit(iface
))
1293 do_write_c_method_def(header
, type_iface_get_inherit(iface
), name
);
1295 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1297 const var_t
*func
= stmt
->u
.var
;
1300 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1303 if (!is_callas(func
->attrs
)) {
1304 const char *callconv
= get_attrp(func
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1305 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1307 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1308 if (is_aggregate_return(func
))
1309 fprintf(header
, " *");
1310 if (is_inherited_method(iface
, func
))
1311 fprintf(header
, " (%s *%s_%s)(\n", callconv
, iface
->name
, func
->name
);
1313 fprintf(header
, " (%s *%s)(\n", callconv
, get_name(func
));
1316 fprintf(header
, "%s *This", name
);
1317 if (is_aggregate_return(func
)) {
1318 fprintf(header
, ",\n");
1320 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1321 fprintf(header
, " *__ret");
1324 if (type_function_get_args(func
->declspec
.type
)) {
1325 fprintf(header
, ",\n");
1326 write_args(header
, type_function_get_args(func
->declspec
.type
), name
, 0, TRUE
, NAME_C
);
1328 fprintf(header
, ");\n");
1329 fprintf(header
, "\n");
1334 static void write_c_method_def(FILE *header
, const type_t
*iface
)
1336 do_write_c_method_def(header
, iface
, iface
->c_name
);
1339 static void write_c_disp_method_def(FILE *header
, const type_t
*iface
)
1341 do_write_c_method_def(header
, type_iface_get_inherit(iface
), iface
->c_name
);
1344 static void write_method_proto(FILE *header
, const type_t
*iface
)
1346 const statement_t
*stmt
;
1348 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1350 const var_t
*func
= stmt
->u
.var
;
1352 if (is_callas(func
->attrs
)) {
1353 const char *callconv
= get_attrp(func
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1354 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1355 /* proxy prototype */
1356 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1357 fprintf(header
, " %s %s_%s_Proxy(\n", callconv
, iface
->name
, get_name(func
));
1358 write_args(header
, type_function_get_args(func
->declspec
.type
), iface
->name
, 1, TRUE
, NAME_DEFAULT
);
1359 fprintf(header
, ");\n");
1360 /* stub prototype */
1361 fprintf(header
, "void __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(func
));
1362 fprintf(header
, " IRpcStubBuffer* This,\n");
1363 fprintf(header
, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1364 fprintf(header
, " PRPC_MESSAGE pRpcMessage,\n");
1365 fprintf(header
, " DWORD* pdwStubPhase);\n");
1370 static void write_locals(FILE *fp
, const type_t
*iface
, int body
)
1372 static const char comment
[]
1373 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1374 const statement_t
*stmt
;
1376 if (!is_object(iface
))
1379 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
)) {
1380 const var_t
*func
= stmt
->u
.var
;
1381 const var_t
*cas
= is_callas(func
->attrs
);
1384 const statement_t
*stmt2
= NULL
;
1385 STATEMENTS_FOR_EACH_FUNC(stmt2
, type_iface_get_stmts(iface
))
1386 if (!strcmp(get_name(stmt2
->u
.var
), cas
->name
))
1388 if (&stmt2
->entry
!= type_iface_get_stmts(iface
)) {
1389 const var_t
*m
= stmt2
->u
.var
;
1390 /* proxy prototype - use local prototype */
1391 write_type_decl_left(fp
, type_function_get_ret(m
->declspec
.type
));
1392 fprintf(fp
, " CALLBACK %s_%s_Proxy(\n", iface
->name
, get_name(m
));
1393 write_args(fp
, type_function_get_args(m
->declspec
.type
), iface
->name
, 1, TRUE
, NAME_DEFAULT
);
1396 const decl_spec_t
*rt
= type_function_get_ret(m
->declspec
.type
);
1397 fprintf(fp
, "\n{\n");
1398 fprintf(fp
, " %s\n", comment
);
1399 if (rt
->type
->name
&& strcmp(rt
->type
->name
, "HRESULT") == 0)
1400 fprintf(fp
, " return E_NOTIMPL;\n");
1401 else if (type_get_type(rt
->type
) != TYPE_VOID
) {
1403 write_type_decl(fp
, rt
, "rv");
1405 fprintf(fp
, " memset(&rv, 0, sizeof rv);\n");
1406 fprintf(fp
, " return rv;\n");
1408 fprintf(fp
, "}\n\n");
1412 /* stub prototype - use remotable prototype */
1413 write_type_decl_left(fp
, type_function_get_ret(func
->declspec
.type
));
1414 fprintf(fp
, " __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(m
));
1415 write_args(fp
, type_function_get_args(func
->declspec
.type
), iface
->name
, 1, TRUE
, NAME_DEFAULT
);
1418 /* Remotable methods must all return HRESULTs. */
1419 fprintf(fp
, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment
);
1424 error_loc("invalid call_as attribute (%s -> %s)\n", func
->name
, cas
->name
);
1429 static void write_local_stubs_stmts(FILE *local_stubs
, const statement_list_t
*stmts
)
1431 const statement_t
*stmt
;
1432 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1434 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1435 write_locals(local_stubs
, stmt
->u
.type
, TRUE
);
1439 void write_local_stubs(const statement_list_t
*stmts
)
1443 if (!local_stubs_name
) return;
1445 local_stubs
= fopen(local_stubs_name
, "w");
1447 error("Could not open %s for output\n", local_stubs_name
);
1450 fprintf(local_stubs
, "/* call_as/local stubs for %s */\n\n", input_name
);
1451 fprintf(local_stubs
, "#include <objbase.h>\n");
1452 fprintf(local_stubs
, "#include \"%s\"\n\n", header_name
);
1454 write_local_stubs_stmts(local_stubs
, stmts
);
1456 fclose(local_stubs
);
1459 static void write_function_proto(FILE *header
, const type_t
*iface
, const var_t
*fun
, const char *prefix
)
1461 const char *callconv
= get_attrp(fun
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1463 if (!callconv
) callconv
= "__cdecl";
1464 /* FIXME: do we need to handle call_as? */
1465 write_type_decl_left(header
, type_function_get_ret(fun
->declspec
.type
));
1466 fprintf(header
, " %s ", callconv
);
1467 fprintf(header
, "%s%s(\n", prefix
, get_name(fun
));
1468 if (type_function_get_args(fun
->declspec
.type
))
1469 write_args(header
, type_function_get_args(fun
->declspec
.type
), iface
->name
, 0, TRUE
, NAME_DEFAULT
);
1471 fprintf(header
, " void");
1472 fprintf(header
, ");\n\n");
1475 static void write_forward(FILE *header
, type_t
*iface
)
1477 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", iface
->c_name
);
1478 fprintf(header
, "#define __%s_FWD_DEFINED__\n", iface
->c_name
);
1479 fprintf(header
, "typedef interface %s %s;\n", iface
->c_name
, iface
->c_name
);
1480 fprintf(header
, "#ifdef __cplusplus\n");
1481 write_namespace_start(header
, iface
->namespace);
1482 write_line(header
, 0, "interface %s;", iface
->name
);
1483 write_namespace_end(header
, iface
->namespace);
1484 fprintf(header
, "#endif /* __cplusplus */\n");
1485 fprintf(header
, "#endif\n\n" );
1488 static char *format_apicontract_macro(const type_t
*type
)
1490 char *name
= format_namespace(type
->namespace, "", "_", type
->name
, NULL
);
1492 for (i
= strlen(name
); i
> 0; --i
) name
[i
- 1] = toupper(name
[i
- 1]);
1496 static void write_apicontract_guard_start(FILE *header
, const expr_t
*expr
)
1501 if (!winrt_mode
) return;
1502 type
= expr
->u
.tref
.type
;
1503 ver
= expr
->ref
->u
.lval
;
1504 name
= format_apicontract_macro(type
);
1505 fprintf(header
, "#if %s_VERSION >= %#x\n", name
, ver
);
1509 static void write_apicontract_guard_end(FILE *header
, const expr_t
*expr
)
1514 if (!winrt_mode
) return;
1515 type
= expr
->u
.tref
.type
;
1516 ver
= expr
->ref
->u
.lval
;
1517 name
= format_apicontract_macro(type
);
1518 fprintf(header
, "#endif /* %s_VERSION >= %#x */\n", name
, ver
);
1522 static void write_com_interface_start(FILE *header
, const type_t
*iface
)
1524 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
1525 expr_t
*contract
= get_attrp(iface
->attrs
, ATTR_CONTRACT
);
1526 fprintf(header
, "/*****************************************************************************\n");
1527 fprintf(header
, " * %s %sinterface\n", iface
->name
, dispinterface
? "disp" : "");
1528 fprintf(header
, " */\n");
1529 if (contract
) write_apicontract_guard_start(header
, contract
);
1530 fprintf(header
,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface
->c_name
, dispinterface
? "DISP" : "");
1531 fprintf(header
,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface
->c_name
, dispinterface
? "DISP" : "");
1534 static void write_com_interface_end(FILE *header
, type_t
*iface
)
1536 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
1537 const UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
1538 expr_t
*contract
= get_attrp(iface
->attrs
, ATTR_CONTRACT
);
1542 write_guid(header
, dispinterface
? "DIID" : "IID", iface
->c_name
, uuid
);
1545 fprintf(header
, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1546 if (!is_global_namespace(iface
->namespace)) {
1547 write_line(header
, 0, "} /* extern \"C\" */");
1548 write_namespace_start(header
, iface
->namespace);
1551 write_line(header
, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid
));
1555 fprintf(header
, "interface ");
1557 if (type_iface_get_inherit(iface
))
1559 fprintf(header
, "%s : public %s\n", iface
->name
,
1560 type_iface_get_inherit(iface
)->name
);
1561 write_line(header
, 1, "{");
1565 fprintf(header
, "%s\n", iface
->name
);
1566 write_line(header
, 1, "{\n");
1567 write_line(header
, 0, "BEGIN_INTERFACE\n");
1569 /* dispinterfaces don't have real functions, so don't write C++ functions for
1572 write_cpp_method_def(header
, iface
);
1573 if (!type_iface_get_inherit(iface
))
1574 write_line(header
, 0, "END_INTERFACE\n");
1575 write_line(header
, -1, "};");
1576 if (!is_global_namespace(iface
->namespace)) {
1577 write_namespace_end(header
, iface
->namespace);
1578 write_line(header
, 0, "extern \"C\" {");
1581 write_uuid_decl(header
, iface
, uuid
);
1582 fprintf(header
, "#else\n");
1584 write_line(header
, 1, "typedef struct %sVtbl {", iface
->c_name
);
1585 write_line(header
, 0, "BEGIN_INTERFACE\n");
1587 write_c_disp_method_def(header
, iface
);
1589 write_c_method_def(header
, iface
);
1590 write_line(header
, 0, "END_INTERFACE");
1591 write_line(header
, -1, "} %sVtbl;\n", iface
->c_name
);
1592 fprintf(header
, "interface %s {\n", iface
->c_name
);
1593 fprintf(header
, " CONST_VTBL %sVtbl* lpVtbl;\n", iface
->c_name
);
1594 fprintf(header
, "};\n\n");
1595 fprintf(header
, "#ifdef COBJMACROS\n");
1596 /* dispinterfaces don't have real functions, so don't write macros for them,
1597 * only for the interface this interface inherits from, i.e. IDispatch */
1598 fprintf(header
, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1599 type
= dispinterface
? type_iface_get_inherit(iface
) : iface
;
1600 write_method_macro(header
, type
, type
, iface
->c_name
);
1601 fprintf(header
, "#else\n");
1602 write_inline_wrappers(header
, type
, type
, iface
->c_name
);
1603 fprintf(header
, "#endif\n");
1604 fprintf(header
, "#endif\n");
1605 fprintf(header
, "\n");
1606 fprintf(header
, "#endif\n");
1607 fprintf(header
, "\n");
1608 /* dispinterfaces don't have real functions, so don't write prototypes for
1610 if (!dispinterface
&& !winrt_mode
)
1612 write_method_proto(header
, iface
);
1613 write_locals(header
, iface
, FALSE
);
1614 fprintf(header
, "\n");
1616 fprintf(header
, "#endif /* __%s_%sINTERFACE_DEFINED__ */\n", iface
->c_name
, dispinterface
? "DISP" : "");
1617 if (contract
) write_apicontract_guard_end(header
, contract
);
1618 fprintf(header
, "\n");
1621 static void write_rpc_interface_start(FILE *header
, const type_t
*iface
)
1623 unsigned int ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
1624 const var_t
*var
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
1625 expr_t
*contract
= get_attrp(iface
->attrs
, ATTR_CONTRACT
);
1627 fprintf(header
, "/*****************************************************************************\n");
1628 fprintf(header
, " * %s interface (v%d.%d)\n", iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1629 fprintf(header
, " */\n");
1630 if (contract
) write_apicontract_guard_start(header
, contract
);
1631 fprintf(header
,"#ifndef __%s_INTERFACE_DEFINED__\n", iface
->name
);
1632 fprintf(header
,"#define __%s_INTERFACE_DEFINED__\n\n", iface
->name
);
1635 fprintf(header
, "extern ");
1636 write_type_decl( header
, &var
->declspec
, var
->name
);
1637 fprintf(header
, ";\n");
1641 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client
, iface
->name
);
1642 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server
, iface
->name
);
1646 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1647 prefix_client
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1648 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1649 prefix_server
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1653 static void write_rpc_interface_end(FILE *header
, const type_t
*iface
)
1655 expr_t
*contract
= get_attrp(iface
->attrs
, ATTR_CONTRACT
);
1656 fprintf(header
, "\n#endif /* __%s_INTERFACE_DEFINED__ */\n", iface
->name
);
1657 if (contract
) write_apicontract_guard_end(header
, contract
);
1658 fprintf(header
, "\n");
1661 static void write_coclass(FILE *header
, type_t
*cocl
)
1663 const UUID
*uuid
= get_attrp(cocl
->attrs
, ATTR_UUID
);
1665 fprintf(header
, "/*****************************************************************************\n");
1666 fprintf(header
, " * %s coclass\n", cocl
->name
);
1667 fprintf(header
, " */\n\n");
1669 write_guid(header
, "CLSID", cocl
->name
, uuid
);
1670 fprintf(header
, "\n#ifdef __cplusplus\n");
1673 fprintf(header
, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid
), cocl
->name
);
1674 write_uuid_decl(header
, cocl
, uuid
);
1678 fprintf(header
, "class %s;\n", cocl
->name
);
1680 fprintf(header
, "#endif\n");
1681 fprintf(header
, "\n");
1684 static void write_coclass_forward(FILE *header
, type_t
*cocl
)
1686 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", cocl
->name
);
1687 fprintf(header
, "#define __%s_FWD_DEFINED__\n", cocl
->name
);
1688 fprintf(header
, "#ifdef __cplusplus\n");
1689 fprintf(header
, "typedef class %s %s;\n", cocl
->name
, cocl
->name
);
1690 fprintf(header
, "#else\n");
1691 fprintf(header
, "typedef struct %s %s;\n", cocl
->name
, cocl
->name
);
1692 fprintf(header
, "#endif /* defined __cplusplus */\n");
1693 fprintf(header
, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl
->name
);
1696 static void write_apicontract(FILE *header
, type_t
*apicontract
)
1698 char *name
= format_apicontract_macro(apicontract
);
1699 fprintf(header
, "#if !defined(%s_VERSION)\n", name
);
1700 fprintf(header
, "#define %s_VERSION %#x\n", name
, get_attrv(apicontract
->attrs
, ATTR_CONTRACTVERSION
));
1701 fprintf(header
, "#endif // defined(%s_VERSION)\n\n", name
);
1705 static void write_runtimeclass(FILE *header
, type_t
*runtimeclass
)
1707 expr_t
*contract
= get_attrp(runtimeclass
->attrs
, ATTR_CONTRACT
);
1708 char *name
, *c_name
;
1710 name
= format_namespace(runtimeclass
->namespace, "", ".", runtimeclass
->name
, NULL
);
1711 c_name
= format_namespace(runtimeclass
->namespace, "", "_", runtimeclass
->name
, NULL
);
1712 fprintf(header
, "/*\n");
1713 fprintf(header
, " * Class %s\n", name
);
1714 fprintf(header
, " */\n");
1715 if (contract
) write_apicontract_guard_start(header
, contract
);
1716 fprintf(header
, "#ifndef RUNTIMECLASS_%s_DEFINED\n", c_name
);
1717 fprintf(header
, "#define RUNTIMECLASS_%s_DEFINED\n", c_name
);
1718 fprintf(header
, "#if defined(_MSC_VER) || defined(__MINGW32__)\n");
1719 /* FIXME: MIDL generates extern const here but GCC warns if extern is initialized */
1720 fprintf(header
, "const DECLSPEC_SELECTANY WCHAR RuntimeClass_%s[] = L\"%s\";\n", c_name
, name
);
1721 fprintf(header
, "#else\n");
1722 fprintf(header
, "static const WCHAR RuntimeClass_%s[] = {", c_name
);
1723 for (i
= 0, len
= strlen(name
); i
< len
; ++i
) fprintf(header
, "'%c',", name
[i
]);
1724 fprintf(header
, "0};\n");
1725 fprintf(header
, "#endif\n");
1726 fprintf(header
, "#endif /* RUNTIMECLASS_%s_DEFINED */\n", c_name
);
1729 if (contract
) write_apicontract_guard_end(header
, contract
);
1730 fprintf(header
, "\n");
1733 static void write_runtimeclass_forward(FILE *header
, type_t
*runtimeclass
)
1735 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", runtimeclass
->c_name
);
1736 fprintf(header
, "#define __%s_FWD_DEFINED__\n", runtimeclass
->c_name
);
1737 fprintf(header
, "#ifdef __cplusplus\n");
1738 write_namespace_start(header
, runtimeclass
->namespace);
1739 write_line(header
, 0, "class %s;", runtimeclass
->name
);
1740 write_namespace_end(header
, runtimeclass
->namespace);
1741 fprintf(header
, "#else\n");
1742 fprintf(header
, "typedef struct %s %s;\n", runtimeclass
->c_name
, runtimeclass
->c_name
);
1743 fprintf(header
, "#endif /* defined __cplusplus */\n");
1744 fprintf(header
, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", runtimeclass
->c_name
);
1747 static void write_import(FILE *header
, const char *fname
)
1751 hname
= dup_basename(fname
, ".idl");
1752 p
= hname
+ strlen(hname
) - 2;
1753 if (p
<= hname
|| strcmp( p
, ".h" )) strcat(hname
, ".h");
1755 fprintf(header
, "#include <%s>\n", hname
);
1759 static void write_imports(FILE *header
, const statement_list_t
*stmts
)
1761 const statement_t
*stmt
;
1762 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1767 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1768 write_imports(header
, type_iface_get_stmts(stmt
->u
.type
));
1771 case STMT_IMPORTLIB
:
1772 /* not included in header */
1775 write_import(header
, stmt
->u
.str
);
1781 case STMT_DECLARATION
:
1782 /* not processed here */
1785 write_imports(header
, stmt
->u
.lib
->stmts
);
1791 static void write_forward_decls(FILE *header
, const statement_list_t
*stmts
)
1793 const statement_t
*stmt
;
1794 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1799 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1801 type_t
*iface
= stmt
->u
.type
;
1802 if (is_object(iface
) || is_attr(iface
->attrs
, ATTR_DISPINTERFACE
))
1804 write_forward(header
, iface
);
1805 if (type_iface_get_async_iface(iface
))
1806 write_forward(header
, type_iface_get_async_iface(iface
));
1809 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
1810 write_coclass_forward(header
, stmt
->u
.type
);
1811 else if (type_get_type(stmt
->u
.type
) == TYPE_RUNTIMECLASS
)
1812 write_runtimeclass_forward(header
, stmt
->u
.type
);
1815 case STMT_IMPORTLIB
:
1816 /* not included in header */
1823 case STMT_DECLARATION
:
1824 /* not processed here */
1827 write_forward_decls(header
, stmt
->u
.lib
->stmts
);
1833 static void write_header_stmts(FILE *header
, const statement_list_t
*stmts
, const type_t
*iface
, int ignore_funcs
)
1835 const statement_t
*stmt
;
1836 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1841 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1843 type_t
*iface
= stmt
->u
.type
;
1844 type_t
*async_iface
= type_iface_get_async_iface(iface
);
1845 if (is_object(iface
)) is_object_interface
++;
1846 if (is_attr(stmt
->u
.type
->attrs
, ATTR_DISPINTERFACE
) || is_object(stmt
->u
.type
))
1848 write_com_interface_start(header
, iface
);
1849 write_header_stmts(header
, type_iface_get_stmts(iface
), stmt
->u
.type
, TRUE
);
1850 write_com_interface_end(header
, iface
);
1853 write_com_interface_start(header
, async_iface
);
1854 write_com_interface_end(header
, async_iface
);
1859 write_rpc_interface_start(header
, iface
);
1860 write_header_stmts(header
, type_iface_get_stmts(iface
), iface
, FALSE
);
1861 write_rpc_interface_end(header
, iface
);
1863 if (is_object(iface
)) is_object_interface
--;
1865 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
1866 write_coclass(header
, stmt
->u
.type
);
1867 else if (type_get_type(stmt
->u
.type
) == TYPE_APICONTRACT
)
1868 write_apicontract(header
, stmt
->u
.type
);
1869 else if (type_get_type(stmt
->u
.type
) == TYPE_RUNTIMECLASS
)
1870 write_runtimeclass(header
, stmt
->u
.type
);
1871 else if (type_get_type(stmt
->u
.type
) != TYPE_PARAMETERIZED_TYPE
)
1872 write_type_definition(header
, stmt
->u
.type
, stmt
->declonly
);
1875 /* FIXME: shouldn't write out forward declarations for undefined
1876 * interfaces but a number of our IDL files depend on this */
1877 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
&& !stmt
->u
.type
->written
)
1878 write_forward(header
, stmt
->u
.type
);
1880 case STMT_IMPORTLIB
:
1883 /* not included in header */
1886 /* not processed here */
1891 if (stmt
->u
.type_list
) LIST_FOR_EACH_ENTRY(ref
, stmt
->u
.type_list
, typeref_t
, entry
)
1892 write_typedef(header
, ref
->type
, stmt
->declonly
);
1896 fprintf(header
, "#ifndef __%s_LIBRARY_DEFINED__\n", stmt
->u
.lib
->name
);
1897 fprintf(header
, "#define __%s_LIBRARY_DEFINED__\n", stmt
->u
.lib
->name
);
1898 write_library(header
, stmt
->u
.lib
);
1899 write_header_stmts(header
, stmt
->u
.lib
->stmts
, NULL
, FALSE
);
1900 fprintf(header
, "#endif /* __%s_LIBRARY_DEFINED__ */\n", stmt
->u
.lib
->name
);
1903 fprintf(header
, "%s\n", stmt
->u
.str
);
1905 case STMT_DECLARATION
:
1906 if (iface
&& type_get_type(stmt
->u
.var
->declspec
.type
) == TYPE_FUNCTION
)
1910 int prefixes_differ
= strcmp(prefix_client
, prefix_server
);
1912 if (prefixes_differ
)
1914 fprintf(header
, "/* client prototype */\n");
1915 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_client
);
1916 fprintf(header
, "/* server prototype */\n");
1918 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_server
);
1922 write_declaration(header
, stmt
->u
.var
);
1928 void write_header(const statement_list_t
*stmts
)
1932 if (!do_header
) return;
1934 if(!(header
= fopen(header_name
, "w"))) {
1935 error("Could not open %s for output\n", header_name
);
1938 fprintf(header
, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION
, input_name
);
1940 fprintf(header
, "#ifdef _WIN32\n");
1941 fprintf(header
, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
1942 fprintf(header
, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
1943 fprintf(header
, "#endif\n");
1944 fprintf(header
, "#include <rpc.h>\n" );
1945 fprintf(header
, "#include <rpcndr.h>\n" );
1946 if (!for_each_serializable(stmts
, NULL
, serializable_exists
))
1947 fprintf(header
, "#include <midles.h>\n" );
1948 fprintf(header
, "#endif\n\n");
1950 fprintf(header
, "#ifndef COM_NO_WINDOWS_H\n");
1951 fprintf(header
, "#include <windows.h>\n");
1952 fprintf(header
, "#include <ole2.h>\n");
1953 fprintf(header
, "#endif\n\n");
1955 fprintf(header
, "#ifndef __%s__\n", header_token
);
1956 fprintf(header
, "#define __%s__\n\n", header_token
);
1958 fprintf(header
, "/* Forward declarations */\n\n");
1959 write_forward_decls(header
, stmts
);
1961 fprintf(header
, "/* Headers for imported files */\n\n");
1962 write_imports(header
, stmts
);
1963 fprintf(header
, "\n");
1964 start_cplusplus_guard(header
);
1966 write_header_stmts(header
, stmts
, NULL
, FALSE
);
1968 fprintf(header
, "/* Begin additional prototypes for all interfaces */\n");
1969 fprintf(header
, "\n");
1970 for_each_serializable(stmts
, header
, write_serialize_function_decl
);
1971 write_user_types(header
);
1972 write_generic_handle_routines(header
);
1973 write_context_handle_rundowns(header
);
1974 fprintf(header
, "\n");
1975 fprintf(header
, "/* End additional prototypes */\n");
1976 fprintf(header
, "\n");
1978 end_cplusplus_guard(header
);
1979 fprintf(header
, "#endif /* __%s__ */\n", header_token
);