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 /* shouldn't be here */
498 void write_type_right(FILE *h
, type_t
*t
, int is_field
)
501 if (type_is_alias(t
)) return;
503 switch (type_get_type(t
))
507 type_t
*elem
= type_array_get_element_type(t
);
508 if (type_array_is_decl_as_ptr(t
))
510 if (decl_needs_parens(elem
))
515 if (is_conformant_array(t
))
516 fprintf(h
, "[%s]", is_field
? "1" : "");
518 fprintf(h
, "[%u]", type_array_get_dim(t
));
520 write_type_right(h
, elem
, FALSE
);
525 const var_list_t
*args
= type_function_get_args(t
);
527 if (args
) write_args(h
, args
, NULL
, 0, FALSE
, NAME_DEFAULT
);
531 write_type_right(h
, type_function_get_rettype(t
), FALSE
);
536 type_t
*ref
= type_pointer_get_ref_type(t
);
537 if (decl_needs_parens(ref
))
539 write_type_right(h
, ref
, FALSE
);
543 fprintf(h
, " : %u", type_bitfield_get_bits(t
)->cval
);
549 case TYPE_ENCAPSULATED_UNION
:
555 case TYPE_RUNTIMECLASS
:
557 case TYPE_APICONTRACT
:
558 /* not supposed to be here */
564 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
)
566 type_t
*t
= ds
->type
;
570 if (t
) write_type_left(h
, ds
, name_type
, declonly
, TRUE
);
572 if (name
) fprintf(h
, "%s%s", !t
|| needs_space_after(t
) ? " " : "", name
);
575 write_type_right(h
, t
, is_field
);
578 static void write_type_definition(FILE *f
, type_t
*t
, int declonly
)
580 int in_namespace
= t
->namespace && !is_global_namespace(t
->namespace);
581 int save_written
= t
->written
;
582 decl_spec_t ds
= {.type
= t
};
583 expr_t
*contract
= get_attrp(t
->attrs
, ATTR_CONTRACT
);
585 if (contract
) write_apicontract_guard_start(f
, contract
);
587 fprintf(f
, "#ifdef __cplusplus\n");
588 fprintf(f
, "} /* extern \"C\" */\n");
589 write_namespace_start(f
, t
->namespace);
592 write_type_left(f
, &ds
, NAME_DEFAULT
, declonly
, TRUE
);
595 t
->written
= save_written
;
596 write_namespace_end(f
, t
->namespace);
597 fprintf(f
, "extern \"C\" {\n");
598 fprintf(f
, "#else\n");
599 write_type_left(f
, &ds
, NAME_C
, declonly
, TRUE
);
601 fprintf(f
, "#endif\n\n");
603 if (contract
) write_apicontract_guard_end(f
, contract
);
606 void write_type_decl(FILE *f
, const decl_spec_t
*t
, const char *name
)
608 write_type_v(f
, t
, FALSE
, TRUE
, name
, NAME_DEFAULT
);
611 void write_type_decl_left(FILE *f
, const decl_spec_t
*ds
)
613 write_type_left(f
, ds
, NAME_DEFAULT
, TRUE
, TRUE
);
616 static int user_type_registered(const char *name
)
619 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
620 if (!strcmp(name
, ut
->name
))
625 static int context_handle_registered(const char *name
)
627 context_handle_t
*ch
;
628 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
629 if (!strcmp(name
, ch
->name
))
634 static int generic_handle_registered(const char *name
)
636 generic_handle_t
*gh
;
637 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
638 if (!strcmp(name
, gh
->name
))
643 unsigned int get_context_handle_offset( const type_t
*type
)
645 context_handle_t
*ch
;
646 unsigned int index
= 0;
648 while (!is_attr( type
->attrs
, ATTR_CONTEXTHANDLE
))
650 if (type_is_alias( type
)) type
= type_alias_get_aliasee_type( type
);
651 else if (is_ptr( type
)) type
= type_pointer_get_ref_type( type
);
652 else error( "internal error: %s is not a context handle\n", type
->name
);
654 LIST_FOR_EACH_ENTRY( ch
, &context_handle_list
, context_handle_t
, entry
)
656 if (!strcmp( type
->name
, ch
->name
)) return index
;
659 error( "internal error: %s is not registered as a context handle\n", type
->name
);
663 unsigned int get_generic_handle_offset( const type_t
*type
)
665 generic_handle_t
*gh
;
666 unsigned int index
= 0;
668 while (!is_attr( type
->attrs
, ATTR_HANDLE
))
670 if (type_is_alias( type
)) type
= type_alias_get_aliasee_type( type
);
671 else if (is_ptr( type
)) type
= type_pointer_get_ref_type( type
);
672 else error( "internal error: %s is not a generic handle\n", type
->name
);
674 LIST_FOR_EACH_ENTRY( gh
, &generic_handle_list
, generic_handle_t
, entry
)
676 if (!strcmp( type
->name
, gh
->name
)) return index
;
679 error( "internal error: %s is not registered as a generic handle\n", type
->name
);
683 /* check for types which require additional prototypes to be generated in the
685 void check_for_additional_prototype_types(type_t
*type
)
689 const char *name
= type
->name
;
690 if (type
->user_types_registered
) break;
691 type
->user_types_registered
= 1;
692 if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
)) {
693 if (!context_handle_registered(name
))
695 context_handle_t
*ch
= xmalloc(sizeof(*ch
));
696 ch
->name
= xstrdup(name
);
697 list_add_tail(&context_handle_list
, &ch
->entry
);
699 /* don't carry on parsing fields within this type */
702 if ((type_get_type(type
) != TYPE_BASIC
||
703 type_basic_get_type(type
) != TYPE_BASIC_HANDLE
) &&
704 is_attr(type
->attrs
, ATTR_HANDLE
)) {
705 if (!generic_handle_registered(name
))
707 generic_handle_t
*gh
= xmalloc(sizeof(*gh
));
708 gh
->name
= xstrdup(name
);
709 list_add_tail(&generic_handle_list
, &gh
->entry
);
711 /* don't carry on parsing fields within this type */
714 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
)) {
715 if (!user_type_registered(name
))
717 user_type_t
*ut
= xmalloc(sizeof *ut
);
718 ut
->name
= xstrdup(name
);
719 list_add_tail(&user_type_list
, &ut
->entry
);
721 /* don't carry on parsing fields within this type as we are already
722 * using a wire marshaled type */
725 else if (type_is_complete(type
))
729 switch (type_get_type_detect_alias(type
))
732 vars
= type_enum_get_values(type
);
735 vars
= type_struct_get_fields(type
);
738 vars
= type_union_get_cases(type
);
744 if (vars
) LIST_FOR_EACH_ENTRY( v
, vars
, const var_t
, entry
)
745 check_for_additional_prototype_types(v
->declspec
.type
);
748 if (type_is_alias(type
))
749 type
= type_alias_get_aliasee_type(type
);
750 else if (is_ptr(type
))
751 type
= type_pointer_get_ref_type(type
);
752 else if (is_array(type
))
753 type
= type_array_get_element_type(type
);
759 static int write_serialize_function_decl(FILE *header
, const type_t
*type
)
761 write_serialize_functions(header
, type
, NULL
);
765 static int serializable_exists(FILE *header
, const type_t
*type
)
770 static int for_each_serializable(const statement_list_t
*stmts
, FILE *header
,
771 int (*proc
)(FILE*, const type_t
*))
773 statement_t
*stmt
, *iface_stmt
;
774 statement_list_t
*iface_stmts
;
775 const type_list_t
*type_entry
;
777 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, statement_t
, entry
)
779 if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
782 iface_stmts
= type_iface_get_stmts(stmt
->u
.type
);
783 if (iface_stmts
) LIST_FOR_EACH_ENTRY( iface_stmt
, iface_stmts
, statement_t
, entry
)
785 if (iface_stmt
->type
!= STMT_TYPEDEF
) continue;
786 for (type_entry
= iface_stmt
->u
.type_list
; type_entry
; type_entry
= type_entry
->next
)
788 if (!is_attr(type_entry
->type
->attrs
, ATTR_ENCODE
)
789 && !is_attr(type_entry
->type
->attrs
, ATTR_DECODE
))
791 if (!proc(header
, type_entry
->type
))
800 static void write_user_types(FILE *header
)
803 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
805 const char *name
= ut
->name
;
806 fprintf(header
, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name
, name
);
807 fprintf(header
, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name
, name
);
808 fprintf(header
, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name
, name
);
809 fprintf(header
, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name
, name
);
813 static void write_context_handle_rundowns(FILE *header
)
815 context_handle_t
*ch
;
816 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
818 const char *name
= ch
->name
;
819 fprintf(header
, "void __RPC_USER %s_rundown(%s);\n", name
, name
);
823 static void write_generic_handle_routines(FILE *header
)
825 generic_handle_t
*gh
;
826 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
828 const char *name
= gh
->name
;
829 fprintf(header
, "handle_t __RPC_USER %s_bind(%s);\n", name
, name
);
830 fprintf(header
, "void __RPC_USER %s_unbind(%s, handle_t);\n", name
, name
);
834 static void write_typedef(FILE *header
, type_t
*type
, int declonly
)
836 type_t
*t
= type_alias_get_aliasee_type(type
);
837 if (winrt_mode
&& t
->namespace && !is_global_namespace(t
->namespace)) return;
838 fprintf(header
, "typedef ");
839 write_type_v(header
, type_alias_get_aliasee(type
), FALSE
, declonly
, type
->name
, NAME_DEFAULT
);
840 fprintf(header
, ";\n");
843 int is_const_decl(const var_t
*var
)
845 const decl_spec_t
*t
;
846 /* strangely, MIDL accepts a const attribute on any pointer in the
847 * declaration to mean that data isn't being instantiated. this appears
848 * to be a bug, but there is no benefit to being incompatible with MIDL,
849 * so we'll do the same thing */
850 for (t
= &var
->declspec
; ; )
852 if (t
->qualifier
& TYPE_QUALIFIER_CONST
)
854 else if (is_ptr(t
->type
))
855 t
= type_pointer_get_ref(t
->type
);
861 static void write_declaration(FILE *header
, const var_t
*v
)
863 if (is_const_decl(v
) && v
->eval
)
865 fprintf(header
, "#define %s (", v
->name
);
866 write_expr(header
, v
->eval
, 0, 1, NULL
, NULL
, "");
867 fprintf(header
, ")\n\n");
871 switch (v
->declspec
.stgclass
)
874 case STG_REGISTER
: /* ignored */
877 fprintf(header
, "static ");
880 fprintf(header
, "extern ");
883 write_type_v(header
, &v
->declspec
, FALSE
, v
->declonly
, v
->name
, NAME_DEFAULT
);
884 fprintf(header
, ";\n\n");
888 static void write_library(FILE *header
, const typelib_t
*typelib
)
890 const UUID
*uuid
= get_attrp(typelib
->attrs
, ATTR_UUID
);
891 fprintf(header
, "\n");
892 write_guid(header
, "LIBID", typelib
->name
, uuid
);
893 fprintf(header
, "\n");
897 const type_t
* get_explicit_generic_handle_type(const var_t
* var
)
900 for (t
= var
->declspec
.type
;
901 is_ptr(t
) || type_is_alias(t
);
902 t
= type_is_alias(t
) ? type_alias_get_aliasee_type(t
) : type_pointer_get_ref_type(t
))
903 if ((type_get_type_detect_alias(t
) != TYPE_BASIC
|| type_basic_get_type(t
) != TYPE_BASIC_HANDLE
) &&
904 is_attr(t
->attrs
, ATTR_HANDLE
))
909 const var_t
*get_func_handle_var( const type_t
*iface
, const var_t
*func
,
910 unsigned char *explicit_fc
, unsigned char *implicit_fc
)
913 const var_list_t
*args
= type_function_get_args( func
->declspec
.type
);
915 *explicit_fc
= *implicit_fc
= 0;
916 if (args
) LIST_FOR_EACH_ENTRY( var
, args
, const var_t
, entry
)
918 if (!is_attr( var
->attrs
, ATTR_IN
) && is_attr( var
->attrs
, ATTR_OUT
)) continue;
919 if (type_get_type( var
->declspec
.type
) == TYPE_BASIC
&& type_basic_get_type( var
->declspec
.type
) == TYPE_BASIC_HANDLE
)
921 *explicit_fc
= FC_BIND_PRIMITIVE
;
924 if (get_explicit_generic_handle_type( var
))
926 *explicit_fc
= FC_BIND_GENERIC
;
929 if (is_context_handle( var
->declspec
.type
))
931 *explicit_fc
= FC_BIND_CONTEXT
;
936 if ((var
= get_attrp( iface
->attrs
, ATTR_IMPLICIT_HANDLE
)))
938 if (type_get_type( var
->declspec
.type
) == TYPE_BASIC
&&
939 type_basic_get_type( var
->declspec
.type
) == TYPE_BASIC_HANDLE
)
940 *implicit_fc
= FC_BIND_PRIMITIVE
;
942 *implicit_fc
= FC_BIND_GENERIC
;
946 *implicit_fc
= FC_AUTO_HANDLE
;
950 int has_out_arg_or_return(const var_t
*func
)
954 if (!is_void(type_function_get_rettype(func
->declspec
.type
)))
957 if (!type_function_get_args(func
->declspec
.type
))
960 LIST_FOR_EACH_ENTRY( var
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
961 if (is_attr(var
->attrs
, ATTR_OUT
))
968 /********** INTERFACES **********/
970 int is_object(const type_t
*iface
)
973 if (type_is_defined(iface
) && type_iface_get_inherit(iface
))
975 if (iface
->attrs
) LIST_FOR_EACH_ENTRY( attr
, iface
->attrs
, const attr_t
, entry
)
976 if (attr
->type
== ATTR_OBJECT
|| attr
->type
== ATTR_ODL
) return 1;
980 int is_local(const attr_list_t
*a
)
982 return is_attr(a
, ATTR_LOCAL
);
985 const var_t
*is_callas(const attr_list_t
*a
)
987 return get_attrp(a
, ATTR_CALLAS
);
990 static int is_inherited_method(const type_t
*iface
, const var_t
*func
)
992 while ((iface
= type_iface_get_inherit(iface
)))
994 const statement_t
*stmt
;
995 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
997 const var_t
*funccmp
= stmt
->u
.var
;
999 if (!is_callas(func
->attrs
))
1001 char inherit_name
[256];
1002 /* compare full name including property prefix */
1003 strcpy(inherit_name
, get_name(funccmp
));
1004 if (!strcmp(inherit_name
, get_name(func
))) return 1;
1012 static int is_override_method(const type_t
*iface
, const type_t
*child
, const var_t
*func
)
1019 const statement_t
*stmt
;
1020 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(child
))
1022 const var_t
*funccmp
= stmt
->u
.var
;
1024 if (!is_callas(func
->attrs
))
1026 char inherit_name
[256];
1027 /* compare full name including property prefix */
1028 strcpy(inherit_name
, get_name(funccmp
));
1029 if (!strcmp(inherit_name
, get_name(func
))) return 1;
1033 while ((child
= type_iface_get_inherit(child
)) && child
!= iface
);
1038 static int is_aggregate_return(const var_t
*func
)
1040 enum type_type type
= type_get_type(type_function_get_rettype(func
->declspec
.type
));
1041 return type
== TYPE_STRUCT
|| type
== TYPE_UNION
||
1042 type
== TYPE_COCLASS
|| type
== TYPE_INTERFACE
||
1043 type
== TYPE_RUNTIMECLASS
;
1046 static char *get_vtbl_entry_name(const type_t
*iface
, const var_t
*func
)
1048 static char buff
[255];
1049 if (is_inherited_method(iface
, func
))
1050 sprintf(buff
, "%s_%s", iface
->name
, get_name(func
));
1052 sprintf(buff
, "%s", get_name(func
));
1056 static void write_method_macro(FILE *header
, const type_t
*iface
, const type_t
*child
, const char *name
)
1058 const statement_t
*stmt
;
1059 int first_iface
= 1;
1061 if (type_iface_get_inherit(iface
))
1062 write_method_macro(header
, type_iface_get_inherit(iface
), child
, name
);
1064 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1066 const var_t
*func
= stmt
->u
.var
;
1070 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1074 if (is_override_method(iface
, child
, func
))
1077 if (!is_callas(func
->attrs
)) {
1080 fprintf(header
, "#define %s_%s(This", name
, get_name(func
));
1081 if (type_function_get_args(func
->declspec
.type
))
1082 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1083 fprintf(header
, ",%s", arg
->name
);
1084 fprintf(header
, ") ");
1086 if (is_aggregate_return(func
))
1088 fprintf(header
, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name
, get_name(func
));
1092 fprintf(header
, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface
, func
));
1093 if (type_function_get_args(func
->declspec
.type
))
1094 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1095 fprintf(header
, ",%s", arg
->name
);
1096 fprintf(header
, ")\n");
1101 void write_args(FILE *h
, const var_list_t
*args
, const char *name
, int method
, int do_indent
, enum name_type name_type
)
1112 fprintf(h
, "%s* This", name
);
1115 if (args
) LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
) {
1122 else fprintf(h
, ",");
1124 /* In theory we should be writing the definition using write_type_v(..., arg->declonly),
1125 * but that causes redefinition in e.g. proxy files. In fact MIDL disallows
1126 * defining UDTs inside of an argument list. */
1127 write_type_v(h
, &arg
->declspec
, FALSE
, TRUE
, arg
->name
, name_type
);
1129 const expr_t
*expr
= get_attrp(arg
->attrs
, ATTR_DEFAULTVALUE
);
1131 const var_t
*tail_arg
;
1133 /* Output default value only if all following arguments also have default value. */
1134 LIST_FOR_EACH_ENTRY_REV( tail_arg
, args
, const var_t
, entry
) {
1135 if(tail_arg
== arg
) {
1138 /* Fixup the expression type for a BSTR like midl does. */
1139 if (get_type_vt(arg
->declspec
.type
) == VT_BSTR
&& expr
->type
== EXPR_STRLIT
)
1142 bstr
.type
= EXPR_WSTRLIT
;
1147 write_expr( h
, expr
, 0, 1, NULL
, NULL
, "" );
1150 if(!get_attrp(tail_arg
->attrs
, ATTR_DEFAULTVALUE
))
1157 if (do_indent
) indentation
--;
1160 static void write_cpp_method_def(FILE *header
, const type_t
*iface
)
1162 const statement_t
*stmt
;
1164 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1166 const var_t
*func
= stmt
->u
.var
;
1167 if (!is_callas(func
->attrs
)) {
1168 const decl_spec_t
*ret
= type_function_get_ret(func
->declspec
.type
);
1169 const char *callconv
= get_attrp(func
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1170 const var_list_t
*args
= type_function_get_args(func
->declspec
.type
);
1173 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1175 if (is_aggregate_return(func
)) {
1176 fprintf(header
, "#ifdef WIDL_EXPLICIT_AGGREGATE_RETURNS\n");
1179 fprintf(header
, "virtual ");
1180 write_type_decl_left(header
, ret
);
1181 fprintf(header
, "* %s %s(\n", callconv
, get_name(func
));
1184 write_type_decl_left(header
, ret
);
1185 fprintf(header
, " *__ret");
1188 fprintf(header
, ",\n");
1189 write_args(header
, args
, iface
->name
, 2, TRUE
, NAME_DEFAULT
);
1191 fprintf(header
, ") = 0;\n");
1194 write_type_decl_left(header
, ret
);
1195 fprintf(header
, " %s %s(\n", callconv
, get_name(func
));
1196 write_args(header
, args
, iface
->name
, 2, TRUE
, NAME_DEFAULT
);
1197 fprintf(header
, ")\n");
1199 fprintf(header
, "{\n");
1202 write_type_decl_left(header
, ret
);
1203 fprintf(header
, " __ret;\n");
1205 fprintf(header
, "return *%s(&__ret", get_name(func
));
1207 LIST_FOR_EACH_ENTRY(arg
, args
, const var_t
, entry
)
1208 fprintf(header
, ", %s", arg
->name
);
1209 fprintf(header
, ");\n");
1212 fprintf(header
, "}\n");
1214 fprintf(header
, "#else\n");
1218 fprintf(header
, "virtual ");
1219 write_type_decl_left(header
, ret
);
1220 fprintf(header
, " %s %s(\n", callconv
, get_name(func
));
1221 write_args(header
, args
, iface
->name
, 2, TRUE
, NAME_DEFAULT
);
1222 fprintf(header
, ") = 0;\n");
1224 if (is_aggregate_return(func
))
1225 fprintf(header
, "#endif\n");
1226 fprintf(header
, "\n");
1231 static void write_inline_wrappers(FILE *header
, const type_t
*iface
, const type_t
*child
, const char *name
)
1233 const statement_t
*stmt
;
1234 int first_iface
= 1;
1236 if (type_iface_get_inherit(iface
))
1237 write_inline_wrappers(header
, type_iface_get_inherit(iface
), child
, name
);
1239 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1241 const var_t
*func
= stmt
->u
.var
;
1245 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1249 if (is_override_method(iface
, child
, func
))
1252 if (!is_callas(func
->attrs
)) {
1255 fprintf(header
, "static FORCEINLINE ");
1256 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1257 fprintf(header
, " %s_%s(", name
, get_name(func
));
1258 write_args(header
, type_function_get_args(func
->declspec
.type
), name
, 1, FALSE
, NAME_C
);
1259 fprintf(header
, ") {\n");
1261 if (!is_aggregate_return(func
)) {
1263 fprintf(header
, "%sThis->lpVtbl->%s(This",
1264 is_void(type_function_get_rettype(func
->declspec
.type
)) ? "" : "return ",
1265 get_vtbl_entry_name(iface
, func
));
1268 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1269 fprintf(header
, " __ret;\n");
1271 fprintf(header
, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface
, func
));
1273 if (type_function_get_args(func
->declspec
.type
))
1274 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1275 fprintf(header
, ",%s", arg
->name
);
1276 fprintf(header
, ");\n");
1278 fprintf(header
, "}\n");
1283 static void do_write_c_method_def(FILE *header
, const type_t
*iface
, const char *name
)
1285 const statement_t
*stmt
;
1286 int first_iface
= 1;
1288 if (type_iface_get_inherit(iface
))
1289 do_write_c_method_def(header
, type_iface_get_inherit(iface
), name
);
1291 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1293 const var_t
*func
= stmt
->u
.var
;
1296 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1299 if (!is_callas(func
->attrs
)) {
1300 const char *callconv
= get_attrp(func
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1301 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1303 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1304 if (is_aggregate_return(func
))
1305 fprintf(header
, " *");
1306 if (is_inherited_method(iface
, func
))
1307 fprintf(header
, " (%s *%s_%s)(\n", callconv
, iface
->name
, func
->name
);
1309 fprintf(header
, " (%s *%s)(\n", callconv
, get_name(func
));
1312 fprintf(header
, "%s *This", name
);
1313 if (is_aggregate_return(func
)) {
1314 fprintf(header
, ",\n");
1316 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1317 fprintf(header
, " *__ret");
1320 if (type_function_get_args(func
->declspec
.type
)) {
1321 fprintf(header
, ",\n");
1322 write_args(header
, type_function_get_args(func
->declspec
.type
), name
, 0, TRUE
, NAME_C
);
1324 fprintf(header
, ");\n");
1325 fprintf(header
, "\n");
1330 static void write_c_method_def(FILE *header
, const type_t
*iface
)
1332 do_write_c_method_def(header
, iface
, iface
->c_name
);
1335 static void write_c_disp_method_def(FILE *header
, const type_t
*iface
)
1337 do_write_c_method_def(header
, type_iface_get_inherit(iface
), iface
->c_name
);
1340 static void write_method_proto(FILE *header
, const type_t
*iface
)
1342 const statement_t
*stmt
;
1344 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1346 const var_t
*func
= stmt
->u
.var
;
1348 if (is_callas(func
->attrs
)) {
1349 const char *callconv
= get_attrp(func
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1350 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1351 /* proxy prototype */
1352 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1353 fprintf(header
, " %s %s_%s_Proxy(\n", callconv
, iface
->name
, get_name(func
));
1354 write_args(header
, type_function_get_args(func
->declspec
.type
), iface
->name
, 1, TRUE
, NAME_DEFAULT
);
1355 fprintf(header
, ");\n");
1356 /* stub prototype */
1357 fprintf(header
, "void __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(func
));
1358 fprintf(header
, " IRpcStubBuffer* This,\n");
1359 fprintf(header
, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1360 fprintf(header
, " PRPC_MESSAGE pRpcMessage,\n");
1361 fprintf(header
, " DWORD* pdwStubPhase);\n");
1366 static void write_locals(FILE *fp
, const type_t
*iface
, int body
)
1368 static const char comment
[]
1369 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1370 const statement_t
*stmt
;
1372 if (!is_object(iface
))
1375 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
)) {
1376 const var_t
*func
= stmt
->u
.var
;
1377 const var_t
*cas
= is_callas(func
->attrs
);
1380 const statement_t
*stmt2
= NULL
;
1381 STATEMENTS_FOR_EACH_FUNC(stmt2
, type_iface_get_stmts(iface
))
1382 if (!strcmp(get_name(stmt2
->u
.var
), cas
->name
))
1384 if (&stmt2
->entry
!= type_iface_get_stmts(iface
)) {
1385 const var_t
*m
= stmt2
->u
.var
;
1386 /* proxy prototype - use local prototype */
1387 write_type_decl_left(fp
, type_function_get_ret(m
->declspec
.type
));
1388 fprintf(fp
, " CALLBACK %s_%s_Proxy(\n", iface
->name
, get_name(m
));
1389 write_args(fp
, type_function_get_args(m
->declspec
.type
), iface
->name
, 1, TRUE
, NAME_DEFAULT
);
1392 const decl_spec_t
*rt
= type_function_get_ret(m
->declspec
.type
);
1393 fprintf(fp
, "\n{\n");
1394 fprintf(fp
, " %s\n", comment
);
1395 if (rt
->type
->name
&& strcmp(rt
->type
->name
, "HRESULT") == 0)
1396 fprintf(fp
, " return E_NOTIMPL;\n");
1397 else if (type_get_type(rt
->type
) != TYPE_VOID
) {
1399 write_type_decl(fp
, rt
, "rv");
1401 fprintf(fp
, " memset(&rv, 0, sizeof rv);\n");
1402 fprintf(fp
, " return rv;\n");
1404 fprintf(fp
, "}\n\n");
1408 /* stub prototype - use remotable prototype */
1409 write_type_decl_left(fp
, type_function_get_ret(func
->declspec
.type
));
1410 fprintf(fp
, " __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(m
));
1411 write_args(fp
, type_function_get_args(func
->declspec
.type
), iface
->name
, 1, TRUE
, NAME_DEFAULT
);
1414 /* Remotable methods must all return HRESULTs. */
1415 fprintf(fp
, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment
);
1420 error_loc("invalid call_as attribute (%s -> %s)\n", func
->name
, cas
->name
);
1425 static void write_local_stubs_stmts(FILE *local_stubs
, const statement_list_t
*stmts
)
1427 const statement_t
*stmt
;
1428 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1430 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1431 write_locals(local_stubs
, stmt
->u
.type
, TRUE
);
1435 void write_local_stubs(const statement_list_t
*stmts
)
1439 if (!local_stubs_name
) return;
1441 local_stubs
= fopen(local_stubs_name
, "w");
1443 error("Could not open %s for output\n", local_stubs_name
);
1446 fprintf(local_stubs
, "/* call_as/local stubs for %s */\n\n", input_name
);
1447 fprintf(local_stubs
, "#include <objbase.h>\n");
1448 fprintf(local_stubs
, "#include \"%s\"\n\n", header_name
);
1450 write_local_stubs_stmts(local_stubs
, stmts
);
1452 fclose(local_stubs
);
1455 static void write_function_proto(FILE *header
, const type_t
*iface
, const var_t
*fun
, const char *prefix
)
1457 const char *callconv
= get_attrp(fun
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1459 if (!callconv
) callconv
= "__cdecl";
1460 /* FIXME: do we need to handle call_as? */
1461 write_type_decl_left(header
, type_function_get_ret(fun
->declspec
.type
));
1462 fprintf(header
, " %s ", callconv
);
1463 fprintf(header
, "%s%s(\n", prefix
, get_name(fun
));
1464 if (type_function_get_args(fun
->declspec
.type
))
1465 write_args(header
, type_function_get_args(fun
->declspec
.type
), iface
->name
, 0, TRUE
, NAME_DEFAULT
);
1467 fprintf(header
, " void");
1468 fprintf(header
, ");\n\n");
1471 static void write_forward(FILE *header
, type_t
*iface
)
1473 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", iface
->c_name
);
1474 fprintf(header
, "#define __%s_FWD_DEFINED__\n", iface
->c_name
);
1475 fprintf(header
, "typedef interface %s %s;\n", iface
->c_name
, iface
->c_name
);
1476 fprintf(header
, "#ifdef __cplusplus\n");
1477 write_namespace_start(header
, iface
->namespace);
1478 write_line(header
, 0, "interface %s;", iface
->name
);
1479 write_namespace_end(header
, iface
->namespace);
1480 fprintf(header
, "#endif /* __cplusplus */\n");
1481 fprintf(header
, "#endif\n\n" );
1484 static char *format_apicontract_macro(const type_t
*type
)
1486 char *name
= format_namespace(type
->namespace, "", "_", type
->name
, NULL
);
1488 for (i
= strlen(name
); i
> 0; --i
) name
[i
- 1] = toupper(name
[i
- 1]);
1492 static void write_apicontract_guard_start(FILE *header
, const expr_t
*expr
)
1497 if (!winrt_mode
) return;
1498 type
= expr
->u
.tref
.type
;
1499 ver
= expr
->ref
->u
.lval
;
1500 name
= format_apicontract_macro(type
);
1501 fprintf(header
, "#if %s_VERSION >= %#x\n", name
, ver
);
1505 static void write_apicontract_guard_end(FILE *header
, const expr_t
*expr
)
1510 if (!winrt_mode
) return;
1511 type
= expr
->u
.tref
.type
;
1512 ver
= expr
->ref
->u
.lval
;
1513 name
= format_apicontract_macro(type
);
1514 fprintf(header
, "#endif /* %s_VERSION >= %#x */\n", name
, ver
);
1518 static void write_com_interface_start(FILE *header
, const type_t
*iface
)
1520 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
1521 expr_t
*contract
= get_attrp(iface
->attrs
, ATTR_CONTRACT
);
1522 fprintf(header
, "/*****************************************************************************\n");
1523 fprintf(header
, " * %s %sinterface\n", iface
->name
, dispinterface
? "disp" : "");
1524 fprintf(header
, " */\n");
1525 if (contract
) write_apicontract_guard_start(header
, contract
);
1526 fprintf(header
,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface
->c_name
, dispinterface
? "DISP" : "");
1527 fprintf(header
,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface
->c_name
, dispinterface
? "DISP" : "");
1530 static void write_com_interface_end(FILE *header
, type_t
*iface
)
1532 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
1533 const UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
1534 expr_t
*contract
= get_attrp(iface
->attrs
, ATTR_CONTRACT
);
1538 write_guid(header
, dispinterface
? "DIID" : "IID", iface
->c_name
, uuid
);
1541 fprintf(header
, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1542 if (!is_global_namespace(iface
->namespace)) {
1543 write_line(header
, 0, "} /* extern \"C\" */");
1544 write_namespace_start(header
, iface
->namespace);
1547 write_line(header
, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid
));
1551 fprintf(header
, "interface ");
1553 if (type_iface_get_inherit(iface
))
1555 fprintf(header
, "%s : public %s\n", iface
->name
,
1556 type_iface_get_inherit(iface
)->name
);
1557 write_line(header
, 1, "{");
1561 fprintf(header
, "%s\n", iface
->name
);
1562 write_line(header
, 1, "{\n");
1563 write_line(header
, 0, "BEGIN_INTERFACE\n");
1565 /* dispinterfaces don't have real functions, so don't write C++ functions for
1568 write_cpp_method_def(header
, iface
);
1569 if (!type_iface_get_inherit(iface
))
1570 write_line(header
, 0, "END_INTERFACE\n");
1571 write_line(header
, -1, "};");
1572 if (!is_global_namespace(iface
->namespace)) {
1573 write_namespace_end(header
, iface
->namespace);
1574 write_line(header
, 0, "extern \"C\" {");
1577 write_uuid_decl(header
, iface
, uuid
);
1578 fprintf(header
, "#else\n");
1580 write_line(header
, 1, "typedef struct %sVtbl {", iface
->c_name
);
1581 write_line(header
, 0, "BEGIN_INTERFACE\n");
1583 write_c_disp_method_def(header
, iface
);
1585 write_c_method_def(header
, iface
);
1586 write_line(header
, 0, "END_INTERFACE");
1587 write_line(header
, -1, "} %sVtbl;\n", iface
->c_name
);
1588 fprintf(header
, "interface %s {\n", iface
->c_name
);
1589 fprintf(header
, " CONST_VTBL %sVtbl* lpVtbl;\n", iface
->c_name
);
1590 fprintf(header
, "};\n\n");
1591 fprintf(header
, "#ifdef COBJMACROS\n");
1592 /* dispinterfaces don't have real functions, so don't write macros for them,
1593 * only for the interface this interface inherits from, i.e. IDispatch */
1594 fprintf(header
, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1595 type
= dispinterface
? type_iface_get_inherit(iface
) : iface
;
1596 write_method_macro(header
, type
, type
, iface
->c_name
);
1597 fprintf(header
, "#else\n");
1598 write_inline_wrappers(header
, type
, type
, iface
->c_name
);
1599 fprintf(header
, "#endif\n");
1600 fprintf(header
, "#endif\n");
1601 fprintf(header
, "\n");
1602 fprintf(header
, "#endif\n");
1603 fprintf(header
, "\n");
1604 /* dispinterfaces don't have real functions, so don't write prototypes for
1606 if (!dispinterface
&& !winrt_mode
)
1608 write_method_proto(header
, iface
);
1609 write_locals(header
, iface
, FALSE
);
1610 fprintf(header
, "\n");
1612 fprintf(header
, "#endif /* __%s_%sINTERFACE_DEFINED__ */\n", iface
->c_name
, dispinterface
? "DISP" : "");
1613 if (contract
) write_apicontract_guard_end(header
, contract
);
1614 fprintf(header
, "\n");
1617 static void write_rpc_interface_start(FILE *header
, const type_t
*iface
)
1619 unsigned int ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
1620 const var_t
*var
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
1621 expr_t
*contract
= get_attrp(iface
->attrs
, ATTR_CONTRACT
);
1623 fprintf(header
, "/*****************************************************************************\n");
1624 fprintf(header
, " * %s interface (v%d.%d)\n", iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1625 fprintf(header
, " */\n");
1626 if (contract
) write_apicontract_guard_start(header
, contract
);
1627 fprintf(header
,"#ifndef __%s_INTERFACE_DEFINED__\n", iface
->name
);
1628 fprintf(header
,"#define __%s_INTERFACE_DEFINED__\n\n", iface
->name
);
1631 fprintf(header
, "extern ");
1632 write_type_decl( header
, &var
->declspec
, var
->name
);
1633 fprintf(header
, ";\n");
1637 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client
, iface
->name
);
1638 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server
, iface
->name
);
1642 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1643 prefix_client
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1644 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1645 prefix_server
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1649 static void write_rpc_interface_end(FILE *header
, const type_t
*iface
)
1651 expr_t
*contract
= get_attrp(iface
->attrs
, ATTR_CONTRACT
);
1652 fprintf(header
, "\n#endif /* __%s_INTERFACE_DEFINED__ */\n", iface
->name
);
1653 if (contract
) write_apicontract_guard_end(header
, contract
);
1654 fprintf(header
, "\n");
1657 static void write_coclass(FILE *header
, type_t
*cocl
)
1659 const UUID
*uuid
= get_attrp(cocl
->attrs
, ATTR_UUID
);
1661 fprintf(header
, "/*****************************************************************************\n");
1662 fprintf(header
, " * %s coclass\n", cocl
->name
);
1663 fprintf(header
, " */\n\n");
1665 write_guid(header
, "CLSID", cocl
->name
, uuid
);
1666 fprintf(header
, "\n#ifdef __cplusplus\n");
1669 fprintf(header
, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid
), cocl
->name
);
1670 write_uuid_decl(header
, cocl
, uuid
);
1674 fprintf(header
, "class %s;\n", cocl
->name
);
1676 fprintf(header
, "#endif\n");
1677 fprintf(header
, "\n");
1680 static void write_coclass_forward(FILE *header
, type_t
*cocl
)
1682 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", cocl
->name
);
1683 fprintf(header
, "#define __%s_FWD_DEFINED__\n", cocl
->name
);
1684 fprintf(header
, "#ifdef __cplusplus\n");
1685 fprintf(header
, "typedef class %s %s;\n", cocl
->name
, cocl
->name
);
1686 fprintf(header
, "#else\n");
1687 fprintf(header
, "typedef struct %s %s;\n", cocl
->name
, cocl
->name
);
1688 fprintf(header
, "#endif /* defined __cplusplus */\n");
1689 fprintf(header
, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl
->name
);
1692 static void write_apicontract(FILE *header
, type_t
*apicontract
)
1694 char *name
= format_apicontract_macro(apicontract
);
1695 fprintf(header
, "#if !defined(%s_VERSION)\n", name
);
1696 fprintf(header
, "#define %s_VERSION %#x\n", name
, get_attrv(apicontract
->attrs
, ATTR_CONTRACTVERSION
));
1697 fprintf(header
, "#endif // defined(%s_VERSION)\n\n", name
);
1701 static void write_runtimeclass(FILE *header
, type_t
*runtimeclass
)
1703 expr_t
*contract
= get_attrp(runtimeclass
->attrs
, ATTR_CONTRACT
);
1704 char *name
, *c_name
;
1705 name
= format_namespace(runtimeclass
->namespace, "", ".", runtimeclass
->name
, NULL
);
1706 c_name
= format_namespace(runtimeclass
->namespace, "", "_", runtimeclass
->name
, NULL
);
1707 fprintf(header
, "/*\n");
1708 fprintf(header
, " * Class %s\n", name
);
1709 fprintf(header
, " */\n");
1710 if (contract
) write_apicontract_guard_start(header
, contract
);
1711 fprintf(header
, "#ifndef RUNTIMECLASS_%s_DEFINED\n", c_name
);
1712 fprintf(header
, "#define RUNTIMECLASS_%s_DEFINED\n", c_name
);
1713 fprintf(header
, "#endif /* RUNTIMECLASS_%s_DEFINED */\n", c_name
);
1716 if (contract
) write_apicontract_guard_end(header
, contract
);
1717 fprintf(header
, "\n");
1720 static void write_runtimeclass_forward(FILE *header
, type_t
*runtimeclass
)
1722 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", runtimeclass
->c_name
);
1723 fprintf(header
, "#define __%s_FWD_DEFINED__\n", runtimeclass
->c_name
);
1724 fprintf(header
, "#ifdef __cplusplus\n");
1725 write_namespace_start(header
, runtimeclass
->namespace);
1726 write_line(header
, 0, "class %s;", runtimeclass
->name
);
1727 write_namespace_end(header
, runtimeclass
->namespace);
1728 fprintf(header
, "#else\n");
1729 fprintf(header
, "typedef struct %s %s;\n", runtimeclass
->c_name
, runtimeclass
->c_name
);
1730 fprintf(header
, "#endif /* defined __cplusplus */\n");
1731 fprintf(header
, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", runtimeclass
->c_name
);
1734 static void write_import(FILE *header
, const char *fname
)
1738 hname
= dup_basename(fname
, ".idl");
1739 p
= hname
+ strlen(hname
) - 2;
1740 if (p
<= hname
|| strcmp( p
, ".h" )) strcat(hname
, ".h");
1742 fprintf(header
, "#include <%s>\n", hname
);
1746 static void write_imports(FILE *header
, const statement_list_t
*stmts
)
1748 const statement_t
*stmt
;
1749 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1754 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1755 write_imports(header
, type_iface_get_stmts(stmt
->u
.type
));
1758 case STMT_IMPORTLIB
:
1759 /* not included in header */
1762 write_import(header
, stmt
->u
.str
);
1768 case STMT_DECLARATION
:
1769 /* not processed here */
1772 write_imports(header
, stmt
->u
.lib
->stmts
);
1778 static void write_forward_decls(FILE *header
, const statement_list_t
*stmts
)
1780 const statement_t
*stmt
;
1781 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1786 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1788 type_t
*iface
= stmt
->u
.type
;
1789 if (is_object(iface
) || is_attr(iface
->attrs
, ATTR_DISPINTERFACE
))
1791 write_forward(header
, iface
);
1792 if (type_iface_get_async_iface(iface
))
1793 write_forward(header
, type_iface_get_async_iface(iface
));
1796 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
1797 write_coclass_forward(header
, stmt
->u
.type
);
1798 else if (type_get_type(stmt
->u
.type
) == TYPE_RUNTIMECLASS
)
1799 write_runtimeclass_forward(header
, stmt
->u
.type
);
1802 case STMT_IMPORTLIB
:
1803 /* not included in header */
1810 case STMT_DECLARATION
:
1811 /* not processed here */
1814 write_forward_decls(header
, stmt
->u
.lib
->stmts
);
1820 static void write_header_stmts(FILE *header
, const statement_list_t
*stmts
, const type_t
*iface
, int ignore_funcs
)
1822 const statement_t
*stmt
;
1823 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1828 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1830 type_t
*iface
= stmt
->u
.type
;
1831 type_t
*async_iface
= type_iface_get_async_iface(iface
);
1832 if (is_object(iface
)) is_object_interface
++;
1833 if (is_attr(stmt
->u
.type
->attrs
, ATTR_DISPINTERFACE
) || is_object(stmt
->u
.type
))
1835 write_com_interface_start(header
, iface
);
1836 write_header_stmts(header
, type_iface_get_stmts(iface
), stmt
->u
.type
, TRUE
);
1837 write_com_interface_end(header
, iface
);
1840 write_com_interface_start(header
, async_iface
);
1841 write_com_interface_end(header
, async_iface
);
1846 write_rpc_interface_start(header
, iface
);
1847 write_header_stmts(header
, type_iface_get_stmts(iface
), iface
, FALSE
);
1848 write_rpc_interface_end(header
, iface
);
1850 if (is_object(iface
)) is_object_interface
--;
1852 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
1853 write_coclass(header
, stmt
->u
.type
);
1854 else if (type_get_type(stmt
->u
.type
) == TYPE_APICONTRACT
)
1855 write_apicontract(header
, stmt
->u
.type
);
1856 else if (type_get_type(stmt
->u
.type
) == TYPE_RUNTIMECLASS
)
1857 write_runtimeclass(header
, stmt
->u
.type
);
1860 write_type_definition(header
, stmt
->u
.type
, stmt
->declonly
);
1864 /* FIXME: shouldn't write out forward declarations for undefined
1865 * interfaces but a number of our IDL files depend on this */
1866 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
&& !stmt
->u
.type
->written
)
1867 write_forward(header
, stmt
->u
.type
);
1869 case STMT_IMPORTLIB
:
1872 /* not included in header */
1875 /* not processed here */
1879 const type_list_t
*type_entry
= stmt
->u
.type_list
;
1880 for (; type_entry
; type_entry
= type_entry
->next
)
1881 write_typedef(header
, type_entry
->type
, stmt
->declonly
);
1885 fprintf(header
, "#ifndef __%s_LIBRARY_DEFINED__\n", stmt
->u
.lib
->name
);
1886 fprintf(header
, "#define __%s_LIBRARY_DEFINED__\n", stmt
->u
.lib
->name
);
1887 write_library(header
, stmt
->u
.lib
);
1888 write_header_stmts(header
, stmt
->u
.lib
->stmts
, NULL
, FALSE
);
1889 fprintf(header
, "#endif /* __%s_LIBRARY_DEFINED__ */\n", stmt
->u
.lib
->name
);
1892 fprintf(header
, "%s\n", stmt
->u
.str
);
1894 case STMT_DECLARATION
:
1895 if (iface
&& type_get_type(stmt
->u
.var
->declspec
.type
) == TYPE_FUNCTION
)
1899 int prefixes_differ
= strcmp(prefix_client
, prefix_server
);
1901 if (prefixes_differ
)
1903 fprintf(header
, "/* client prototype */\n");
1904 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_client
);
1905 fprintf(header
, "/* server prototype */\n");
1907 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_server
);
1911 write_declaration(header
, stmt
->u
.var
);
1917 void write_header(const statement_list_t
*stmts
)
1921 if (!do_header
) return;
1923 if(!(header
= fopen(header_name
, "w"))) {
1924 error("Could not open %s for output\n", header_name
);
1927 fprintf(header
, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION
, input_name
);
1929 fprintf(header
, "#ifdef _WIN32\n");
1930 fprintf(header
, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
1931 fprintf(header
, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
1932 fprintf(header
, "#endif\n");
1933 fprintf(header
, "#include <rpc.h>\n" );
1934 fprintf(header
, "#include <rpcndr.h>\n" );
1935 if (!for_each_serializable(stmts
, NULL
, serializable_exists
))
1936 fprintf(header
, "#include <midles.h>\n" );
1937 fprintf(header
, "#endif\n\n");
1939 fprintf(header
, "#ifndef COM_NO_WINDOWS_H\n");
1940 fprintf(header
, "#include <windows.h>\n");
1941 fprintf(header
, "#include <ole2.h>\n");
1942 fprintf(header
, "#endif\n\n");
1944 fprintf(header
, "#ifndef __%s__\n", header_token
);
1945 fprintf(header
, "#define __%s__\n\n", header_token
);
1947 fprintf(header
, "/* Forward declarations */\n\n");
1948 write_forward_decls(header
, stmts
);
1950 fprintf(header
, "/* Headers for imported files */\n\n");
1951 write_imports(header
, stmts
);
1952 fprintf(header
, "\n");
1953 start_cplusplus_guard(header
);
1955 write_header_stmts(header
, stmts
, NULL
, FALSE
);
1957 fprintf(header
, "/* Begin additional prototypes for all interfaces */\n");
1958 fprintf(header
, "\n");
1959 for_each_serializable(stmts
, header
, write_serialize_function_decl
);
1960 write_user_types(header
);
1961 write_generic_handle_routines(header
);
1962 write_context_handle_rundowns(header
);
1963 fprintf(header
, "\n");
1964 fprintf(header
, "/* End additional prototypes */\n");
1965 fprintf(header
, "\n");
1967 end_cplusplus_guard(header
);
1968 fprintf(header
, "#endif /* __%s__ */\n", header_token
);