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_def_or_decl(FILE *f
, type_t
*t
, int field
, const char *name
);
48 static void indent(FILE *h
, int delta
)
51 if (delta
< 0) indentation
+= delta
;
52 for (c
=0; c
<indentation
; c
++) fprintf(h
, " ");
53 if (delta
> 0) indentation
+= delta
;
56 static void write_line(FILE *f
, int delta
, const char *fmt
, ...)
66 int is_ptrchain_attr(const var_t
*var
, enum attr_type t
)
68 if (is_attr(var
->attrs
, t
))
72 type_t
*type
= var
->type
;
75 if (is_attr(type
->attrs
, t
))
77 else if (type_is_alias(type
))
78 type
= type_alias_get_aliasee(type
);
79 else if (is_ptr(type
))
80 type
= type_pointer_get_ref(type
);
86 int is_aliaschain_attr(const type_t
*type
, enum attr_type attr
)
88 const type_t
*t
= type
;
91 if (is_attr(t
->attrs
, attr
))
93 else if (type_is_alias(t
))
94 t
= type_alias_get_aliasee(t
);
99 int is_attr(const attr_list_t
*list
, enum attr_type t
)
102 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
103 if (attr
->type
== t
) return 1;
107 void *get_attrp(const attr_list_t
*list
, enum attr_type t
)
110 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
111 if (attr
->type
== t
) return attr
->u
.pval
;
115 unsigned int get_attrv(const attr_list_t
*list
, enum attr_type t
)
118 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
119 if (attr
->type
== t
) return attr
->u
.ival
;
123 static void write_guid(FILE *f
, const char *guid_prefix
, const char *name
, const UUID
*uuid
)
126 fprintf(f
, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
127 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
128 guid_prefix
, name
, uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0],
129 uuid
->Data4
[1], uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5],
130 uuid
->Data4
[6], uuid
->Data4
[7]);
133 static void write_uuid_decl(FILE *f
, type_t
*type
, const UUID
*uuid
)
135 char *name
= format_namespace(type
->namespace, "", "::", type
->name
);
136 fprintf(f
, "#ifdef __CRT_UUID_DECL\n");
137 fprintf(f
, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
138 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
139 name
, uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
140 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6],
142 fprintf(f
, "#endif\n");
146 static const char *uuid_string(const UUID
*uuid
)
150 sprintf(buf
, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
151 uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1], uuid
->Data4
[2],
152 uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6], uuid
->Data4
[7]);
157 static void write_namespace_start(FILE *header
, struct namespace *namespace)
159 if(is_global_namespace(namespace)) {
160 if(use_abi_namespace
)
161 write_line(header
, 1, "namespace ABI {");
165 write_namespace_start(header
, namespace->parent
);
166 write_line(header
, 1, "namespace %s {", namespace->name
);
169 static void write_namespace_end(FILE *header
, struct namespace *namespace)
171 if(is_global_namespace(namespace)) {
172 if(use_abi_namespace
)
173 write_line(header
, -1, "}", namespace->name
);
177 write_line(header
, -1, "}", namespace->name
);
178 write_namespace_end(header
, namespace->parent
);
181 const char *get_name(const var_t
*v
)
183 static char buffer
[256];
185 if (is_attr( v
->attrs
, ATTR_PROPGET
))
186 strcpy( buffer
, "get_" );
187 else if (is_attr( v
->attrs
, ATTR_PROPPUT
))
188 strcpy( buffer
, "put_" );
189 else if (is_attr( v
->attrs
, ATTR_PROPPUTREF
))
190 strcpy( buffer
, "putref_" );
193 strcat( buffer
, v
->name
);
197 static void write_fields(FILE *h
, var_list_t
*fields
)
199 unsigned nameless_struct_cnt
= 0, nameless_struct_i
= 0, nameless_union_cnt
= 0, nameless_union_i
= 0;
206 LIST_FOR_EACH_ENTRY( v
, fields
, var_t
, entry
) {
207 if (!v
|| !v
->type
) continue;
209 switch(type_get_type_detect_alias(v
->type
)) {
211 case TYPE_ENCAPSULATED_UNION
:
212 nameless_struct_cnt
++;
215 nameless_union_cnt
++;
222 LIST_FOR_EACH_ENTRY( v
, fields
, var_t
, entry
) {
223 if (!v
|| !v
->type
) continue;
228 switch(type_get_type_detect_alias(v
->type
)) {
230 case TYPE_ENCAPSULATED_UNION
:
232 fprintf(h
, "__C89_NAMELESS ");
233 if(nameless_struct_cnt
== 1) {
234 name
= "__C89_NAMELESSSTRUCTNAME";
235 }else if(nameless_struct_i
< 5 /* # of supporting macros */) {
236 sprintf(buf
, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i
);
243 fprintf(h
, "__C89_NAMELESS ");
244 if(nameless_union_cnt
== 1) {
245 name
= "__C89_NAMELESSUNIONNAME";
246 }else if(nameless_union_i
< 8 /* # of supporting macros */ ) {
247 sprintf(buf
, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i
);
255 write_type_def_or_decl(h
, v
->type
, TRUE
, name
);
260 static void write_enums(FILE *h
, var_list_t
*enums
, const char *enum_name
)
264 LIST_FOR_EACH_ENTRY( v
, enums
, var_t
, entry
)
269 fprintf(h
, "%s", get_name(v
));
271 fprintf(h
, "%s_%s", enum_name
, get_name(v
));
274 write_expr(h
, v
->eval
, 0, 1, NULL
, NULL
, "");
277 if (list_next( enums
, &v
->entry
)) fprintf(h
, ",\n");
282 int needs_space_after(type_t
*t
)
284 return (type_is_alias(t
) ||
285 (!is_ptr(t
) && (!is_array(t
) || !type_array_is_decl_as_ptr(t
) || t
->name
)));
288 static void write_pointer_left(FILE *h
, type_t
*ref
)
290 if (needs_space_after(ref
))
292 if (!type_is_alias(ref
) && is_array(ref
) && !type_array_is_decl_as_ptr(ref
))
297 void write_type_left(FILE *h
, type_t
*t
, enum name_type name_type
, int declonly
)
303 name
= type_get_name(t
, name_type
);
305 if (is_attr(t
->attrs
, ATTR_CONST
) &&
306 (type_is_alias(t
) || !is_ptr(t
)))
307 fprintf(h
, "const ");
309 if (type_is_alias(t
)) fprintf(h
, "%s", t
->name
);
311 switch (type_get_type_detect_alias(t
)) {
313 if (!declonly
&& t
->defined
&& !t
->written
) {
314 if (name
) fprintf(h
, "enum %s {\n", name
);
315 else fprintf(h
, "enum {\n");
318 write_enums(h
, type_enum_get_values(t
), is_global_namespace(t
->namespace) ? NULL
: t
->name
);
322 else fprintf(h
, "enum %s", name
? name
: "");
325 case TYPE_ENCAPSULATED_UNION
:
326 if (!declonly
&& t
->defined
&& !t
->written
) {
327 if (name
) fprintf(h
, "struct %s {\n", name
);
328 else fprintf(h
, "struct {\n");
331 if (type_get_type(t
) != TYPE_STRUCT
)
332 write_fields(h
, type_encapsulated_union_get_fields(t
));
334 write_fields(h
, type_struct_get_fields(t
));
338 else fprintf(h
, "struct %s", name
? name
: "");
341 if (!declonly
&& t
->defined
&& !t
->written
) {
342 if (t
->name
) fprintf(h
, "union %s {\n", t
->name
);
343 else fprintf(h
, "union {\n");
346 write_fields(h
, type_union_get_cases(t
));
350 else fprintf(h
, "union %s", t
->name
? t
->name
: "");
354 write_type_left(h
, type_pointer_get_ref(t
), name_type
, declonly
);
355 write_pointer_left(h
, type_pointer_get_ref(t
));
356 if (is_attr(t
->attrs
, ATTR_CONST
)) fprintf(h
, "const ");
360 if (t
->name
&& type_array_is_decl_as_ptr(t
))
361 fprintf(h
, "%s", t
->name
);
364 write_type_left(h
, type_array_get_element(t
), name_type
, declonly
);
365 if (type_array_is_decl_as_ptr(t
))
366 write_pointer_left(h
, type_array_get_element(t
));
370 if (type_basic_get_type(t
) != TYPE_BASIC_INT32
&&
371 type_basic_get_type(t
) != TYPE_BASIC_INT64
&&
372 type_basic_get_type(t
) != TYPE_BASIC_LONG
&&
373 type_basic_get_type(t
) != TYPE_BASIC_HYPER
)
375 if (type_basic_get_sign(t
) < 0) fprintf(h
, "signed ");
376 else if (type_basic_get_sign(t
) > 0) fprintf(h
, "unsigned ");
378 switch (type_basic_get_type(t
))
380 case TYPE_BASIC_INT8
: fprintf(h
, "small"); break;
381 case TYPE_BASIC_INT16
: fprintf(h
, "short"); break;
382 case TYPE_BASIC_INT
: fprintf(h
, "int"); break;
383 case TYPE_BASIC_INT3264
: fprintf(h
, "__int3264"); break;
384 case TYPE_BASIC_BYTE
: fprintf(h
, "byte"); break;
385 case TYPE_BASIC_CHAR
: fprintf(h
, "char"); break;
386 case TYPE_BASIC_WCHAR
: fprintf(h
, "wchar_t"); break;
387 case TYPE_BASIC_FLOAT
: fprintf(h
, "float"); break;
388 case TYPE_BASIC_DOUBLE
: fprintf(h
, "double"); break;
389 case TYPE_BASIC_ERROR_STATUS_T
: fprintf(h
, "error_status_t"); break;
390 case TYPE_BASIC_HANDLE
: fprintf(h
, "handle_t"); break;
391 case TYPE_BASIC_INT32
:
392 if (type_basic_get_sign(t
) > 0)
393 fprintf(h
, "UINT32");
397 case TYPE_BASIC_LONG
:
398 if (type_basic_get_sign(t
) > 0)
403 case TYPE_BASIC_INT64
:
404 if (type_basic_get_sign(t
) > 0)
405 fprintf(h
, "UINT64");
409 case TYPE_BASIC_HYPER
:
410 if (type_basic_get_sign(t
) > 0)
411 fprintf(h
, "MIDL_uhyper");
420 fprintf(h
, "%s", t
->name
);
426 write_type_left(h
, type_bitfield_get_field(t
), name_type
, declonly
);
430 /* handled elsewhere */
437 void write_type_right(FILE *h
, type_t
*t
, int is_field
)
440 if (type_is_alias(t
)) return;
442 switch (type_get_type(t
))
446 type_t
*elem
= type_array_get_element(t
);
447 if (type_array_is_decl_as_ptr(t
))
449 if (!type_is_alias(elem
) && is_array(elem
) && !type_array_is_decl_as_ptr(elem
))
454 if (is_conformant_array(t
))
455 fprintf(h
, "[%s]", is_field
? "1" : "");
457 fprintf(h
, "[%u]", type_array_get_dim(t
));
459 write_type_right(h
, elem
, FALSE
);
464 type_t
*ref
= type_pointer_get_ref(t
);
465 if (!type_is_alias(ref
) && is_array(ref
) && !type_array_is_decl_as_ptr(ref
))
467 write_type_right(h
, ref
, FALSE
);
471 fprintf(h
, " : %u", type_bitfield_get_bits(t
)->cval
);
477 case TYPE_ENCAPSULATED_UNION
:
488 static void write_type_v(FILE *h
, type_t
*t
, int is_field
, int declonly
, const char *name
)
496 for (pt
= t
; is_ptr(pt
); pt
= type_pointer_get_ref(pt
), ptr_level
++)
499 if (type_get_type_detect_alias(pt
) == TYPE_FUNCTION
) {
501 const char *callconv
= get_attrp(pt
->attrs
, ATTR_CALLCONV
);
502 if (!callconv
&& is_object_interface
) callconv
= "STDMETHODCALLTYPE";
503 if (is_attr(pt
->attrs
, ATTR_INLINE
)) fprintf(h
, "inline ");
504 write_type_left(h
, type_function_get_rettype(pt
), NAME_DEFAULT
, declonly
);
506 if (ptr_level
) fputc('(', h
);
507 if (callconv
) fprintf(h
, "%s ", callconv
);
508 for (i
= 0; i
< ptr_level
; i
++)
511 write_type_left(h
, t
, NAME_DEFAULT
, declonly
);
514 if (name
) fprintf(h
, "%s%s", !t
|| needs_space_after(t
) ? " " : "", name
);
517 if (type_get_type_detect_alias(pt
) == TYPE_FUNCTION
) {
518 const var_list_t
*args
= type_function_get_args(pt
);
520 if (ptr_level
) fputc(')', h
);
523 write_args(h
, args
, NULL
, 0, FALSE
);
528 write_type_right(h
, t
, is_field
);
532 static void write_type_def_or_decl(FILE *f
, type_t
*t
, int field
, const char *name
)
534 write_type_v(f
, t
, field
, FALSE
, name
);
537 static void write_type_definition(FILE *f
, type_t
*t
)
539 int in_namespace
= t
->namespace && !is_global_namespace(t
->namespace);
540 int save_written
= t
->written
;
543 fprintf(f
, "#ifdef __cplusplus\n");
544 fprintf(f
, "} /* extern \"C\" */\n");
545 write_namespace_start(f
, t
->namespace);
548 write_type_left(f
, t
, NAME_DEFAULT
, FALSE
);
551 t
->written
= save_written
;
552 write_namespace_end(f
, t
->namespace);
553 fprintf(f
, "extern \"C\" {\n");
554 fprintf(f
, "#else\n");
555 write_type_left(f
, t
, NAME_C
, FALSE
);
557 fprintf(f
, "#endif\n\n");
561 void write_type_decl(FILE *f
, type_t
*t
, const char *name
)
563 write_type_v(f
, t
, FALSE
, TRUE
, name
);
566 void write_type_decl_left(FILE *f
, type_t
*t
)
568 write_type_left(f
, t
, NAME_DEFAULT
, TRUE
);
571 static int user_type_registered(const char *name
)
574 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
575 if (!strcmp(name
, ut
->name
))
580 static int context_handle_registered(const char *name
)
582 context_handle_t
*ch
;
583 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
584 if (!strcmp(name
, ch
->name
))
589 static int generic_handle_registered(const char *name
)
591 generic_handle_t
*gh
;
592 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
593 if (!strcmp(name
, gh
->name
))
598 unsigned int get_context_handle_offset( const type_t
*type
)
600 context_handle_t
*ch
;
601 unsigned int index
= 0;
603 while (!is_attr( type
->attrs
, ATTR_CONTEXTHANDLE
))
605 if (type_is_alias( type
)) type
= type_alias_get_aliasee( type
);
606 else if (is_ptr( type
)) type
= type_pointer_get_ref( type
);
607 else error( "internal error: %s is not a context handle\n", type
->name
);
609 LIST_FOR_EACH_ENTRY( ch
, &context_handle_list
, context_handle_t
, entry
)
611 if (!strcmp( type
->name
, ch
->name
)) return index
;
614 error( "internal error: %s is not registered as a context handle\n", type
->name
);
618 unsigned int get_generic_handle_offset( const type_t
*type
)
620 generic_handle_t
*gh
;
621 unsigned int index
= 0;
623 while (!is_attr( type
->attrs
, ATTR_HANDLE
))
625 if (type_is_alias( type
)) type
= type_alias_get_aliasee( type
);
626 else if (is_ptr( type
)) type
= type_pointer_get_ref( type
);
627 else error( "internal error: %s is not a generic handle\n", type
->name
);
629 LIST_FOR_EACH_ENTRY( gh
, &generic_handle_list
, generic_handle_t
, entry
)
631 if (!strcmp( type
->name
, gh
->name
)) return index
;
634 error( "internal error: %s is not registered as a generic handle\n", type
->name
);
638 /* check for types which require additional prototypes to be generated in the
640 void check_for_additional_prototype_types(const var_list_t
*list
)
645 LIST_FOR_EACH_ENTRY( v
, list
, const var_t
, entry
)
647 type_t
*type
= v
->type
;
650 const char *name
= type
->name
;
651 if (type
->user_types_registered
) break;
652 type
->user_types_registered
= 1;
653 if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
)) {
654 if (!context_handle_registered(name
))
656 context_handle_t
*ch
= xmalloc(sizeof(*ch
));
657 ch
->name
= xstrdup(name
);
658 list_add_tail(&context_handle_list
, &ch
->entry
);
660 /* don't carry on parsing fields within this type */
663 if ((type_get_type(type
) != TYPE_BASIC
||
664 type_basic_get_type(type
) != TYPE_BASIC_HANDLE
) &&
665 is_attr(type
->attrs
, ATTR_HANDLE
)) {
666 if (!generic_handle_registered(name
))
668 generic_handle_t
*gh
= xmalloc(sizeof(*gh
));
669 gh
->name
= xstrdup(name
);
670 list_add_tail(&generic_handle_list
, &gh
->entry
);
672 /* don't carry on parsing fields within this type */
675 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
)) {
676 if (!user_type_registered(name
))
678 user_type_t
*ut
= xmalloc(sizeof *ut
);
679 ut
->name
= xstrdup(name
);
680 list_add_tail(&user_type_list
, &ut
->entry
);
682 /* don't carry on parsing fields within this type as we are already
683 * using a wire marshaled type */
686 else if (type_is_complete(type
))
689 switch (type_get_type_detect_alias(type
))
692 vars
= type_enum_get_values(type
);
695 vars
= type_struct_get_fields(type
);
698 vars
= type_union_get_cases(type
);
704 check_for_additional_prototype_types(vars
);
707 if (type_is_alias(type
))
708 type
= type_alias_get_aliasee(type
);
709 else if (is_ptr(type
))
710 type
= type_pointer_get_ref(type
);
711 else if (is_array(type
))
712 type
= type_array_get_element(type
);
719 static int write_serialize_function_decl(FILE *header
, const type_t
*type
)
721 write_serialize_functions(header
, type
, NULL
);
725 static int serializable_exists(FILE *header
, const type_t
*type
)
730 static int for_each_serializable(const statement_list_t
*stmts
, FILE *header
,
731 int (*proc
)(FILE*, const type_t
*))
733 statement_t
*stmt
, *iface_stmt
;
734 statement_list_t
*iface_stmts
;
735 const type_list_t
*type_entry
;
737 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, statement_t
, entry
)
739 if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
742 iface_stmts
= type_iface_get_stmts(stmt
->u
.type
);
743 if (iface_stmts
) LIST_FOR_EACH_ENTRY( iface_stmt
, iface_stmts
, statement_t
, entry
)
745 if (iface_stmt
->type
!= STMT_TYPEDEF
) continue;
746 for (type_entry
= iface_stmt
->u
.type_list
; type_entry
; type_entry
= type_entry
->next
)
748 if (!is_attr(type_entry
->type
->attrs
, ATTR_ENCODE
)
749 && !is_attr(type_entry
->type
->attrs
, ATTR_DECODE
))
751 if (!proc(header
, type_entry
->type
))
760 static void write_user_types(FILE *header
)
763 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
765 const char *name
= ut
->name
;
766 fprintf(header
, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name
, name
);
767 fprintf(header
, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name
, name
);
768 fprintf(header
, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name
, name
);
769 fprintf(header
, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name
, name
);
773 static void write_context_handle_rundowns(FILE *header
)
775 context_handle_t
*ch
;
776 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
778 const char *name
= ch
->name
;
779 fprintf(header
, "void __RPC_USER %s_rundown(%s);\n", name
, name
);
783 static void write_generic_handle_routines(FILE *header
)
785 generic_handle_t
*gh
;
786 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
788 const char *name
= gh
->name
;
789 fprintf(header
, "handle_t __RPC_USER %s_bind(%s);\n", name
, name
);
790 fprintf(header
, "void __RPC_USER %s_unbind(%s, handle_t);\n", name
, name
);
794 static void write_typedef(FILE *header
, type_t
*type
)
796 fprintf(header
, "typedef ");
797 write_type_def_or_decl(header
, type_alias_get_aliasee(type
), FALSE
, type
->name
);
798 fprintf(header
, ";\n");
801 int is_const_decl(const var_t
*var
)
804 /* strangely, MIDL accepts a const attribute on any pointer in the
805 * declaration to mean that data isn't being instantiated. this appears
806 * to be a bug, but there is no benefit to being incompatible with MIDL,
807 * so we'll do the same thing */
808 for (t
= var
->type
; ; )
810 if (is_attr(t
->attrs
, ATTR_CONST
))
813 t
= type_pointer_get_ref(t
);
819 static void write_declaration(FILE *header
, const var_t
*v
)
821 if (is_const_decl(v
) && v
->eval
)
823 fprintf(header
, "#define %s (", v
->name
);
824 write_expr(header
, v
->eval
, 0, 1, NULL
, NULL
, "");
825 fprintf(header
, ")\n\n");
832 case STG_REGISTER
: /* ignored */
835 fprintf(header
, "static ");
838 fprintf(header
, "extern ");
841 write_type_def_or_decl(header
, v
->type
, FALSE
, v
->name
);
842 fprintf(header
, ";\n\n");
846 static void write_library(FILE *header
, const typelib_t
*typelib
)
848 const UUID
*uuid
= get_attrp(typelib
->attrs
, ATTR_UUID
);
849 fprintf(header
, "\n");
850 write_guid(header
, "LIBID", typelib
->name
, uuid
);
851 fprintf(header
, "\n");
855 const type_t
* get_explicit_generic_handle_type(const var_t
* var
)
859 is_ptr(t
) || type_is_alias(t
);
860 t
= type_is_alias(t
) ? type_alias_get_aliasee(t
) : type_pointer_get_ref(t
))
861 if ((type_get_type_detect_alias(t
) != TYPE_BASIC
|| type_basic_get_type(t
) != TYPE_BASIC_HANDLE
) &&
862 is_attr(t
->attrs
, ATTR_HANDLE
))
867 const var_t
*get_func_handle_var( const type_t
*iface
, const var_t
*func
,
868 unsigned char *explicit_fc
, unsigned char *implicit_fc
)
871 const var_list_t
*args
= type_get_function_args( func
->type
);
873 *explicit_fc
= *implicit_fc
= 0;
874 if (args
) LIST_FOR_EACH_ENTRY( var
, args
, const var_t
, entry
)
876 if (!is_attr( var
->attrs
, ATTR_IN
) && is_attr( var
->attrs
, ATTR_OUT
)) continue;
877 if (type_get_type( var
->type
) == TYPE_BASIC
&& type_basic_get_type( var
->type
) == TYPE_BASIC_HANDLE
)
879 *explicit_fc
= FC_BIND_PRIMITIVE
;
882 if (get_explicit_generic_handle_type( var
))
884 *explicit_fc
= FC_BIND_GENERIC
;
887 if (is_context_handle( var
->type
))
889 *explicit_fc
= FC_BIND_CONTEXT
;
894 if ((var
= get_attrp( iface
->attrs
, ATTR_IMPLICIT_HANDLE
)))
896 if (type_get_type( var
->type
) == TYPE_BASIC
&&
897 type_basic_get_type( var
->type
) == TYPE_BASIC_HANDLE
)
898 *implicit_fc
= FC_BIND_PRIMITIVE
;
900 *implicit_fc
= FC_BIND_GENERIC
;
904 *implicit_fc
= FC_AUTO_HANDLE
;
908 int has_out_arg_or_return(const var_t
*func
)
912 if (!is_void(type_function_get_rettype(func
->type
)))
915 if (!type_get_function_args(func
->type
))
918 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
919 if (is_attr(var
->attrs
, ATTR_OUT
))
926 /********** INTERFACES **********/
928 int is_object(const type_t
*iface
)
931 if (type_is_defined(iface
) && type_iface_get_inherit(iface
))
933 if (iface
->attrs
) LIST_FOR_EACH_ENTRY( attr
, iface
->attrs
, const attr_t
, entry
)
934 if (attr
->type
== ATTR_OBJECT
|| attr
->type
== ATTR_ODL
) return 1;
938 int is_local(const attr_list_t
*a
)
940 return is_attr(a
, ATTR_LOCAL
);
943 const var_t
*is_callas(const attr_list_t
*a
)
945 return get_attrp(a
, ATTR_CALLAS
);
948 static int is_inherited_method(const type_t
*iface
, const var_t
*func
)
950 while ((iface
= type_iface_get_inherit(iface
)))
952 const statement_t
*stmt
;
953 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
955 const var_t
*funccmp
= stmt
->u
.var
;
957 if (!is_callas(func
->attrs
))
959 char inherit_name
[256];
960 /* compare full name including property prefix */
961 strcpy(inherit_name
, get_name(funccmp
));
962 if (!strcmp(inherit_name
, get_name(func
))) return 1;
970 static int is_override_method(const type_t
*iface
, const type_t
*child
, const var_t
*func
)
977 const statement_t
*stmt
;
978 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(child
))
980 const var_t
*funccmp
= stmt
->u
.var
;
982 if (!is_callas(func
->attrs
))
984 char inherit_name
[256];
985 /* compare full name including property prefix */
986 strcpy(inherit_name
, get_name(funccmp
));
987 if (!strcmp(inherit_name
, get_name(func
))) return 1;
991 while ((child
= type_iface_get_inherit(child
)) && child
!= iface
);
996 static int is_aggregate_return(const var_t
*func
)
998 enum type_type type
= type_get_type(type_function_get_rettype(func
->type
));
999 return type
== TYPE_STRUCT
|| type
== TYPE_UNION
||
1000 type
== TYPE_COCLASS
|| type
== TYPE_INTERFACE
;
1003 static char *get_vtbl_entry_name(const type_t
*iface
, const var_t
*func
)
1005 static char buff
[255];
1006 if (is_inherited_method(iface
, func
))
1007 sprintf(buff
, "%s_%s", iface
->name
, get_name(func
));
1009 sprintf(buff
, "%s", get_name(func
));
1013 static void write_method_macro(FILE *header
, const type_t
*iface
, const type_t
*child
, const char *name
)
1015 const statement_t
*stmt
;
1016 int first_iface
= 1;
1018 if (type_iface_get_inherit(iface
))
1019 write_method_macro(header
, type_iface_get_inherit(iface
), child
, name
);
1021 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1023 const var_t
*func
= stmt
->u
.var
;
1027 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1031 if (is_override_method(iface
, child
, func
))
1034 if (!is_callas(func
->attrs
)) {
1037 fprintf(header
, "#define %s_%s(This", name
, get_name(func
));
1038 if (type_get_function_args(func
->type
))
1039 LIST_FOR_EACH_ENTRY( arg
, type_get_function_args(func
->type
), const var_t
, entry
)
1040 fprintf(header
, ",%s", arg
->name
);
1041 fprintf(header
, ") ");
1043 if (is_aggregate_return(func
))
1045 fprintf(header
, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name
, get_name(func
));
1049 fprintf(header
, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface
, func
));
1050 if (type_get_function_args(func
->type
))
1051 LIST_FOR_EACH_ENTRY( arg
, type_get_function_args(func
->type
), const var_t
, entry
)
1052 fprintf(header
, ",%s", arg
->name
);
1053 fprintf(header
, ")\n");
1058 void write_args(FILE *h
, const var_list_t
*args
, const char *name
, int method
, int do_indent
)
1069 fprintf(h
, "%s* This", name
);
1072 if (args
) LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
) {
1079 else fprintf(h
, ",");
1081 write_type_decl(h
, arg
->type
, arg
->name
);
1083 const expr_t
*expr
= get_attrp(arg
->attrs
, ATTR_DEFAULTVALUE
);
1085 const var_t
*tail_arg
;
1087 /* Output default value only if all following arguments also have default value. */
1088 LIST_FOR_EACH_ENTRY_REV( tail_arg
, args
, const var_t
, entry
) {
1089 if(tail_arg
== arg
) {
1092 /* Fixup the expression type for a BSTR like midl does. */
1093 if (get_type_vt(arg
->type
) == VT_BSTR
&& expr
->type
== EXPR_STRLIT
)
1096 bstr
.type
= EXPR_WSTRLIT
;
1101 write_expr( h
, expr
, 0, 1, NULL
, NULL
, "" );
1104 if(!get_attrp(tail_arg
->attrs
, ATTR_DEFAULTVALUE
))
1111 if (do_indent
) indentation
--;
1114 static void write_cpp_method_def(FILE *header
, const type_t
*iface
)
1116 const statement_t
*stmt
;
1118 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1120 const var_t
*func
= stmt
->u
.var
;
1121 if (!is_callas(func
->attrs
)) {
1122 const char *callconv
= get_attrp(func
->type
->attrs
, ATTR_CALLCONV
);
1123 const var_list_t
*args
= type_get_function_args(func
->type
);
1126 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1128 if (is_aggregate_return(func
)) {
1129 fprintf(header
, "#ifdef WIDL_EXPLICIT_AGGREGATE_RETURNS\n");
1132 fprintf(header
, "virtual ");
1133 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
1134 fprintf(header
, "* %s %s(\n", callconv
, get_name(func
));
1137 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
1138 fprintf(header
, " *__ret");
1141 fprintf(header
, ",\n");
1142 write_args(header
, args
, iface
->name
, 2, TRUE
);
1144 fprintf(header
, ") = 0;\n");
1147 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
1148 fprintf(header
, " %s %s(\n", callconv
, get_name(func
));
1149 write_args(header
, args
, iface
->name
, 2, TRUE
);
1150 fprintf(header
, ")\n");
1152 fprintf(header
, "{\n");
1155 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
1156 fprintf(header
, " __ret;\n");
1158 fprintf(header
, "return *%s(&__ret", get_name(func
));
1160 LIST_FOR_EACH_ENTRY(arg
, args
, const var_t
, entry
)
1161 fprintf(header
, ", %s", arg
->name
);
1162 fprintf(header
, ");\n");
1165 fprintf(header
, "}\n");
1167 fprintf(header
, "#else\n");
1171 fprintf(header
, "virtual ");
1172 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
1173 fprintf(header
, " %s %s(\n", callconv
, get_name(func
));
1174 write_args(header
, args
, iface
->name
, 2, TRUE
);
1175 fprintf(header
, ") = 0;\n");
1177 if (is_aggregate_return(func
))
1178 fprintf(header
, "#endif\n");
1179 fprintf(header
, "\n");
1184 static void write_inline_wrappers(FILE *header
, const type_t
*iface
, const type_t
*child
, const char *name
)
1186 const statement_t
*stmt
;
1187 int first_iface
= 1;
1189 if (type_iface_get_inherit(iface
))
1190 write_inline_wrappers(header
, type_iface_get_inherit(iface
), child
, name
);
1192 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1194 const var_t
*func
= stmt
->u
.var
;
1198 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1202 if (is_override_method(iface
, child
, func
))
1205 if (!is_callas(func
->attrs
)) {
1209 fprintf(header
, "FORCEINLINE ");
1211 fprintf(header
, "static FORCEINLINE ");
1213 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
1214 fprintf(header
, " %s_%s(", name
, get_name(func
));
1215 write_args(header
, type_get_function_args(func
->type
), name
, 1, FALSE
);
1216 fprintf(header
, ") {\n");
1218 if (!is_aggregate_return(func
)) {
1220 fprintf(header
, "%sThis->lpVtbl->%s(This",
1221 is_void(type_function_get_rettype(func
->type
)) ? "" : "return ",
1222 get_vtbl_entry_name(iface
, func
));
1225 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
1226 fprintf(header
, " __ret;\n");
1228 fprintf(header
, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface
, func
));
1230 if (type_get_function_args(func
->type
))
1231 LIST_FOR_EACH_ENTRY( arg
, type_get_function_args(func
->type
), const var_t
, entry
)
1232 fprintf(header
, ",%s", arg
->name
);
1233 fprintf(header
, ");\n");
1235 fprintf(header
, "}\n");
1240 static void do_write_c_method_def(FILE *header
, const type_t
*iface
, const char *name
)
1242 const statement_t
*stmt
;
1243 int first_iface
= 1;
1245 if (type_iface_get_inherit(iface
))
1246 do_write_c_method_def(header
, type_iface_get_inherit(iface
), name
);
1247 #ifdef __REACTOS__ /* r59312 / 3ab1571 */
1248 else if (type_iface_get_stmts(iface
) == NULL
)
1250 fprintf(header
, "#ifndef __cplusplus\n");
1252 fprintf(header
, "char dummy;\n");
1253 fprintf(header
, "#endif\n");
1254 fprintf(header
, "\n");
1259 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1261 const var_t
*func
= stmt
->u
.var
;
1264 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1267 if (!is_callas(func
->attrs
)) {
1268 const char *callconv
= get_attrp(func
->type
->attrs
, ATTR_CALLCONV
);
1269 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1271 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
1272 if (is_aggregate_return(func
))
1273 fprintf(header
, " *");
1274 if (is_inherited_method(iface
, func
))
1275 fprintf(header
, " (%s *%s_%s)(\n", callconv
, iface
->name
, func
->name
);
1277 fprintf(header
, " (%s *%s)(\n", callconv
, get_name(func
));
1280 fprintf(header
, "%s *This", name
);
1281 if (is_aggregate_return(func
)) {
1282 fprintf(header
, ",\n");
1284 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
1285 fprintf(header
, " *__ret");
1288 if (type_get_function_args(func
->type
)) {
1289 fprintf(header
, ",\n");
1290 write_args(header
, type_get_function_args(func
->type
), name
, 0, TRUE
);
1292 fprintf(header
, ");\n");
1293 fprintf(header
, "\n");
1298 static void write_c_method_def(FILE *header
, const type_t
*iface
)
1300 do_write_c_method_def(header
, iface
, iface
->c_name
);
1303 static void write_c_disp_method_def(FILE *header
, const type_t
*iface
)
1305 do_write_c_method_def(header
, type_iface_get_inherit(iface
), iface
->c_name
);
1308 static void write_method_proto(FILE *header
, const type_t
*iface
)
1310 const statement_t
*stmt
;
1312 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1314 const var_t
*func
= stmt
->u
.var
;
1316 if (is_callas(func
->attrs
)) {
1317 const char *callconv
= get_attrp(func
->type
->attrs
, ATTR_CALLCONV
);
1318 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1319 /* proxy prototype */
1320 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
1321 fprintf(header
, " %s %s_%s_Proxy(\n", callconv
, iface
->name
, get_name(func
));
1322 write_args(header
, type_get_function_args(func
->type
), iface
->name
, 1, TRUE
);
1323 fprintf(header
, ");\n");
1324 /* stub prototype */
1325 fprintf(header
, "void __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(func
));
1326 fprintf(header
, " IRpcStubBuffer* This,\n");
1327 fprintf(header
, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1328 fprintf(header
, " PRPC_MESSAGE pRpcMessage,\n");
1329 fprintf(header
, " DWORD* pdwStubPhase);\n");
1334 static void write_locals(FILE *fp
, const type_t
*iface
, int body
)
1336 static const char comment
[]
1337 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1338 const statement_t
*stmt
;
1340 if (!is_object(iface
))
1343 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
)) {
1344 const var_t
*func
= stmt
->u
.var
;
1345 const var_t
*cas
= is_callas(func
->attrs
);
1348 const statement_t
*stmt2
= NULL
;
1349 STATEMENTS_FOR_EACH_FUNC(stmt2
, type_iface_get_stmts(iface
))
1350 if (!strcmp(get_name(stmt2
->u
.var
), cas
->name
))
1352 if (&stmt2
->entry
!= type_iface_get_stmts(iface
)) {
1353 const var_t
*m
= stmt2
->u
.var
;
1354 /* proxy prototype - use local prototype */
1355 write_type_decl_left(fp
, type_function_get_rettype(m
->type
));
1356 fprintf(fp
, " CALLBACK %s_%s_Proxy(\n", iface
->name
, get_name(m
));
1357 write_args(fp
, type_get_function_args(m
->type
), iface
->name
, 1, TRUE
);
1360 type_t
*rt
= type_function_get_rettype(m
->type
);
1361 fprintf(fp
, "\n{\n");
1362 fprintf(fp
, " %s\n", comment
);
1363 if (rt
->name
&& strcmp(rt
->name
, "HRESULT") == 0)
1364 fprintf(fp
, " return E_NOTIMPL;\n");
1365 else if (type_get_type(rt
) != TYPE_VOID
) {
1367 write_type_decl(fp
, rt
, "rv");
1369 fprintf(fp
, " memset(&rv, 0, sizeof rv);\n");
1370 fprintf(fp
, " return rv;\n");
1372 fprintf(fp
, "}\n\n");
1376 /* stub prototype - use remotable prototype */
1377 write_type_decl_left(fp
, type_function_get_rettype(func
->type
));
1378 fprintf(fp
, " __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(m
));
1379 write_args(fp
, type_get_function_args(func
->type
), iface
->name
, 1, TRUE
);
1382 /* Remotable methods must all return HRESULTs. */
1383 fprintf(fp
, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment
);
1388 error_loc("invalid call_as attribute (%s -> %s)\n", func
->name
, cas
->name
);
1393 static void write_local_stubs_stmts(FILE *local_stubs
, const statement_list_t
*stmts
)
1395 const statement_t
*stmt
;
1396 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1398 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1399 write_locals(local_stubs
, stmt
->u
.type
, TRUE
);
1403 void write_local_stubs(const statement_list_t
*stmts
)
1407 if (!local_stubs_name
) return;
1409 local_stubs
= fopen(local_stubs_name
, "w");
1411 error("Could not open %s for output\n", local_stubs_name
);
1414 fprintf(local_stubs
, "/* call_as/local stubs for %s */\n\n", input_name
);
1415 fprintf(local_stubs
, "#include <objbase.h>\n");
1416 fprintf(local_stubs
, "#include \"%s\"\n\n", header_name
);
1418 write_local_stubs_stmts(local_stubs
, stmts
);
1420 fclose(local_stubs
);
1423 static void write_function_proto(FILE *header
, const type_t
*iface
, const var_t
*fun
, const char *prefix
)
1425 const char *callconv
= get_attrp(fun
->type
->attrs
, ATTR_CALLCONV
);
1427 if (!callconv
) callconv
= "__cdecl";
1428 /* FIXME: do we need to handle call_as? */
1429 write_type_decl_left(header
, type_function_get_rettype(fun
->type
));
1430 fprintf(header
, " %s ", callconv
);
1431 fprintf(header
, "%s%s(\n", prefix
, get_name(fun
));
1432 if (type_get_function_args(fun
->type
))
1433 write_args(header
, type_get_function_args(fun
->type
), iface
->name
, 0, TRUE
);
1435 fprintf(header
, " void");
1436 fprintf(header
, ");\n\n");
1439 static void write_forward(FILE *header
, type_t
*iface
)
1441 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", iface
->c_name
);
1442 fprintf(header
, "#define __%s_FWD_DEFINED__\n", iface
->c_name
);
1443 fprintf(header
, "typedef interface %s %s;\n", iface
->c_name
, iface
->c_name
);
1444 fprintf(header
, "#ifdef __cplusplus\n");
1445 write_namespace_start(header
, iface
->namespace);
1446 write_line(header
, 0, "interface %s;", iface
->name
);
1447 write_namespace_end(header
, iface
->namespace);
1448 fprintf(header
, "#endif /* __cplusplus */\n");
1449 fprintf(header
, "#endif\n\n" );
1452 static void write_com_interface_start(FILE *header
, const type_t
*iface
)
1454 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
1455 fprintf(header
, "/*****************************************************************************\n");
1456 fprintf(header
, " * %s %sinterface\n", iface
->name
, dispinterface
? "disp" : "");
1457 fprintf(header
, " */\n");
1458 fprintf(header
,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface
->c_name
, dispinterface
? "DISP" : "");
1459 fprintf(header
,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface
->c_name
, dispinterface
? "DISP" : "");
1462 static void write_com_interface_end(FILE *header
, type_t
*iface
)
1464 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
1465 const UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
1469 write_guid(header
, dispinterface
? "DIID" : "IID", iface
->c_name
, uuid
);
1472 fprintf(header
, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1473 if (!is_global_namespace(iface
->namespace)) {
1474 write_line(header
, 0, "} /* extern \"C\" */");
1475 write_namespace_start(header
, iface
->namespace);
1478 write_line(header
, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid
));
1482 fprintf(header
, "interface ");
1484 if (type_iface_get_inherit(iface
))
1486 fprintf(header
, "%s : public %s\n", iface
->name
,
1487 type_iface_get_inherit(iface
)->name
);
1488 write_line(header
, 1, "{");
1492 fprintf(header
, "%s\n", iface
->name
);
1493 write_line(header
, 1, "{\n");
1494 write_line(header
, 0, "BEGIN_INTERFACE\n");
1496 /* dispinterfaces don't have real functions, so don't write C++ functions for
1499 write_cpp_method_def(header
, iface
);
1500 if (!type_iface_get_inherit(iface
))
1501 write_line(header
, 0, "END_INTERFACE\n");
1502 write_line(header
, -1, "};");
1503 if (!is_global_namespace(iface
->namespace)) {
1504 write_namespace_end(header
, iface
->namespace);
1505 write_line(header
, 0, "extern \"C\" {");
1508 write_uuid_decl(header
, iface
, uuid
);
1509 fprintf(header
, "#else\n");
1511 write_line(header
, 1, "typedef struct %sVtbl {", iface
->c_name
);
1512 write_line(header
, 0, "BEGIN_INTERFACE\n");
1514 write_c_disp_method_def(header
, iface
);
1516 write_c_method_def(header
, iface
);
1517 write_line(header
, 0, "END_INTERFACE");
1518 write_line(header
, -1, "} %sVtbl;\n", iface
->c_name
);
1519 fprintf(header
, "interface %s {\n", iface
->c_name
);
1520 fprintf(header
, " CONST_VTBL %sVtbl* lpVtbl;\n", iface
->c_name
);
1521 fprintf(header
, "};\n\n");
1522 fprintf(header
, "#ifdef COBJMACROS\n");
1523 /* dispinterfaces don't have real functions, so don't write macros for them,
1524 * only for the interface this interface inherits from, i.e. IDispatch */
1525 fprintf(header
, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1526 type
= dispinterface
? type_iface_get_inherit(iface
) : iface
;
1527 write_method_macro(header
, type
, type
, iface
->c_name
);
1528 fprintf(header
, "#else\n");
1529 write_inline_wrappers(header
, type
, type
, iface
->c_name
);
1530 fprintf(header
, "#endif\n");
1531 fprintf(header
, "#endif\n");
1532 fprintf(header
, "\n");
1533 fprintf(header
, "#endif\n");
1534 fprintf(header
, "\n");
1535 /* dispinterfaces don't have real functions, so don't write prototypes for
1537 if (!dispinterface
&& !winrt_mode
)
1539 write_method_proto(header
, iface
);
1540 write_locals(header
, iface
, FALSE
);
1541 fprintf(header
, "\n");
1543 fprintf(header
,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface
->c_name
, dispinterface
? "DISP" : "");
1546 static void write_rpc_interface_start(FILE *header
, const type_t
*iface
)
1548 unsigned int ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
1549 const var_t
*var
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
1551 fprintf(header
, "/*****************************************************************************\n");
1552 fprintf(header
, " * %s interface (v%d.%d)\n", iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1553 fprintf(header
, " */\n");
1554 fprintf(header
,"#ifndef __%s_INTERFACE_DEFINED__\n", iface
->name
);
1555 fprintf(header
,"#define __%s_INTERFACE_DEFINED__\n\n", iface
->name
);
1558 fprintf(header
, "extern ");
1559 write_type_decl( header
, var
->type
, var
->name
);
1560 fprintf(header
, ";\n");
1564 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client
, iface
->name
);
1565 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server
, iface
->name
);
1569 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1570 prefix_client
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1571 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1572 prefix_server
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1576 static void write_rpc_interface_end(FILE *header
, const type_t
*iface
)
1578 fprintf(header
,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface
->name
);
1581 static void write_coclass(FILE *header
, type_t
*cocl
)
1583 const UUID
*uuid
= get_attrp(cocl
->attrs
, ATTR_UUID
);
1585 fprintf(header
, "/*****************************************************************************\n");
1586 fprintf(header
, " * %s coclass\n", cocl
->name
);
1587 fprintf(header
, " */\n\n");
1589 write_guid(header
, "CLSID", cocl
->name
, uuid
);
1590 fprintf(header
, "\n#ifdef __cplusplus\n");
1593 fprintf(header
, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid
), cocl
->name
);
1594 write_uuid_decl(header
, cocl
, uuid
);
1598 fprintf(header
, "class %s;\n", cocl
->name
);
1600 fprintf(header
, "#endif\n");
1601 fprintf(header
, "\n");
1604 static void write_coclass_forward(FILE *header
, type_t
*cocl
)
1606 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", cocl
->name
);
1607 fprintf(header
, "#define __%s_FWD_DEFINED__\n", cocl
->name
);
1608 fprintf(header
, "#ifdef __cplusplus\n");
1609 fprintf(header
, "typedef class %s %s;\n", cocl
->name
, cocl
->name
);
1610 fprintf(header
, "#else\n");
1611 fprintf(header
, "typedef struct %s %s;\n", cocl
->name
, cocl
->name
);
1612 fprintf(header
, "#endif /* defined __cplusplus */\n");
1613 fprintf(header
, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl
->name
);
1616 static void write_import(FILE *header
, const char *fname
)
1620 hname
= dup_basename(fname
, ".idl");
1621 p
= hname
+ strlen(hname
) - 2;
1622 if (p
<= hname
|| strcmp( p
, ".h" )) strcat(hname
, ".h");
1624 fprintf(header
, "#include <%s>\n", hname
);
1628 static void write_imports(FILE *header
, const statement_list_t
*stmts
)
1630 const statement_t
*stmt
;
1631 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1636 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1637 write_imports(header
, type_iface_get_stmts(stmt
->u
.type
));
1640 case STMT_IMPORTLIB
:
1641 /* not included in header */
1644 write_import(header
, stmt
->u
.str
);
1650 case STMT_DECLARATION
:
1651 /* not processed here */
1654 write_imports(header
, stmt
->u
.lib
->stmts
);
1660 static void write_forward_decls(FILE *header
, const statement_list_t
*stmts
)
1662 const statement_t
*stmt
;
1663 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1668 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1670 type_t
*iface
= stmt
->u
.type
;
1671 if (is_object(iface
) || is_attr(iface
->attrs
, ATTR_DISPINTERFACE
))
1673 write_forward(header
, iface
);
1674 if (iface
->details
.iface
->async_iface
)
1675 write_forward(header
, iface
->details
.iface
->async_iface
);
1678 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
1679 write_coclass_forward(header
, stmt
->u
.type
);
1682 case STMT_IMPORTLIB
:
1683 /* not included in header */
1690 case STMT_DECLARATION
:
1691 /* not processed here */
1694 write_forward_decls(header
, stmt
->u
.lib
->stmts
);
1700 static void write_header_stmts(FILE *header
, const statement_list_t
*stmts
, const type_t
*iface
, int ignore_funcs
)
1702 const statement_t
*stmt
;
1703 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1708 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1710 type_t
*iface
= stmt
->u
.type
;
1711 type_t
*async_iface
= iface
->details
.iface
->async_iface
;
1712 if (is_object(iface
)) is_object_interface
++;
1713 if (is_attr(stmt
->u
.type
->attrs
, ATTR_DISPINTERFACE
) || is_object(stmt
->u
.type
))
1715 write_com_interface_start(header
, iface
);
1716 write_header_stmts(header
, type_iface_get_stmts(iface
), stmt
->u
.type
, TRUE
);
1717 write_com_interface_end(header
, iface
);
1720 write_com_interface_start(header
, async_iface
);
1721 write_com_interface_end(header
, async_iface
);
1726 write_rpc_interface_start(header
, iface
);
1727 write_header_stmts(header
, type_iface_get_stmts(iface
), iface
, FALSE
);
1728 write_rpc_interface_end(header
, iface
);
1730 if (is_object(iface
)) is_object_interface
--;
1732 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
1733 write_coclass(header
, stmt
->u
.type
);
1736 write_type_definition(header
, stmt
->u
.type
);
1740 /* FIXME: shouldn't write out forward declarations for undefined
1741 * interfaces but a number of our IDL files depend on this */
1742 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
&& !stmt
->u
.type
->written
)
1743 write_forward(header
, stmt
->u
.type
);
1745 case STMT_IMPORTLIB
:
1748 /* not included in header */
1751 /* not processed here */
1755 const type_list_t
*type_entry
= stmt
->u
.type_list
;
1756 for (; type_entry
; type_entry
= type_entry
->next
)
1757 write_typedef(header
, type_entry
->type
);
1761 write_library(header
, stmt
->u
.lib
);
1762 write_header_stmts(header
, stmt
->u
.lib
->stmts
, NULL
, FALSE
);
1765 fprintf(header
, "%s\n", stmt
->u
.str
);
1767 case STMT_DECLARATION
:
1768 if (iface
&& type_get_type(stmt
->u
.var
->type
) == TYPE_FUNCTION
)
1772 int prefixes_differ
= strcmp(prefix_client
, prefix_server
);
1774 if (prefixes_differ
)
1776 fprintf(header
, "/* client prototype */\n");
1777 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_client
);
1778 fprintf(header
, "/* server prototype */\n");
1780 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_server
);
1784 write_declaration(header
, stmt
->u
.var
);
1790 void write_header(const statement_list_t
*stmts
)
1794 if (!do_header
) return;
1796 if(!(header
= fopen(header_name
, "w"))) {
1797 error("Could not open %s for output\n", header_name
);
1800 fprintf(header
, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION
, input_name
);
1802 fprintf(header
, "#ifdef _WIN32\n");
1803 fprintf(header
, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
1804 fprintf(header
, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
1805 fprintf(header
, "#endif\n\n");
1807 fprintf(header
, "#ifdef __REACTOS__\n");
1808 fprintf(header
, "#define WIN32_LEAN_AND_MEAN\n");
1809 fprintf(header
, "#endif\n\n");
1811 fprintf(header
, "#include <rpc.h>\n" );
1812 fprintf(header
, "#include <rpcndr.h>\n" );
1813 if (!for_each_serializable(stmts
, NULL
, serializable_exists
))
1814 fprintf(header
, "#include <midles.h>\n" );
1815 fprintf(header
, "#endif\n\n");
1817 fprintf(header
, "#ifndef COM_NO_WINDOWS_H\n");
1818 fprintf(header
, "#include <windows.h>\n");
1819 fprintf(header
, "#include <ole2.h>\n");
1820 fprintf(header
, "#endif\n\n");
1822 fprintf(header
, "#ifndef __%s__\n", header_token
);
1823 fprintf(header
, "#define __%s__\n\n", header_token
);
1825 fprintf(header
, "/* Forward declarations */\n\n");
1826 write_forward_decls(header
, stmts
);
1828 fprintf(header
, "/* Headers for imported files */\n\n");
1829 write_imports(header
, stmts
);
1830 fprintf(header
, "\n");
1831 start_cplusplus_guard(header
);
1833 write_header_stmts(header
, stmts
, NULL
, FALSE
);
1835 fprintf(header
, "/* Begin additional prototypes for all interfaces */\n");
1836 fprintf(header
, "\n");
1837 for_each_serializable(stmts
, header
, write_serialize_function_decl
);
1838 write_user_types(header
);
1839 write_generic_handle_routines(header
);
1840 write_context_handle_rundowns(header
);
1841 fprintf(header
, "\n");
1842 fprintf(header
, "/* End additional prototypes */\n");
1843 fprintf(header
, "\n");
1845 end_cplusplus_guard(header
);
1846 fprintf(header
, "#endif /* __%s__ */\n", header_token
);