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
);
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
->declspec
.type
;
75 if (is_attr(type
->attrs
, t
))
77 else if (type_is_alias(type
))
78 type
= type_alias_get_aliasee_type(type
);
79 else if (is_ptr(type
))
80 type
= type_pointer_get_ref_type(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_type(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
)
185 if (is_attr( v
->attrs
, ATTR_PROPGET
))
186 return buffer
= strmake( "get_%s", v
->name
);
187 if (is_attr( v
->attrs
, ATTR_PROPPUT
))
188 return buffer
= strmake( "put_%s", v
->name
);
189 if (is_attr( v
->attrs
, ATTR_PROPPUTREF
))
190 return buffer
= strmake( "putref_%s", v
->name
);
195 static void write_fields(FILE *h
, var_list_t
*fields
)
197 unsigned nameless_struct_cnt
= 0, nameless_struct_i
= 0, nameless_union_cnt
= 0, nameless_union_i
= 0;
204 LIST_FOR_EACH_ENTRY( v
, fields
, var_t
, entry
) {
205 if (!v
|| !v
->declspec
.type
) continue;
207 switch(type_get_type_detect_alias(v
->declspec
.type
)) {
209 case TYPE_ENCAPSULATED_UNION
:
210 nameless_struct_cnt
++;
213 nameless_union_cnt
++;
220 LIST_FOR_EACH_ENTRY( v
, fields
, var_t
, entry
) {
221 if (!v
|| !v
->declspec
.type
) continue;
226 switch(type_get_type_detect_alias(v
->declspec
.type
)) {
228 case TYPE_ENCAPSULATED_UNION
:
230 fprintf(h
, "__C89_NAMELESS ");
231 if(nameless_struct_cnt
== 1) {
232 name
= "__C89_NAMELESSSTRUCTNAME";
233 }else if(nameless_struct_i
< 5 /* # of supporting macros */) {
234 sprintf(buf
, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i
);
241 fprintf(h
, "__C89_NAMELESS ");
242 if(nameless_union_cnt
== 1) {
243 name
= "__C89_NAMELESSUNIONNAME";
244 }else if(nameless_union_i
< 8 /* # of supporting macros */ ) {
245 sprintf(buf
, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i
);
253 write_type_v(h
, &v
->declspec
, TRUE
, v
->declonly
, name
);
258 static void write_enums(FILE *h
, var_list_t
*enums
, const char *enum_name
)
262 LIST_FOR_EACH_ENTRY( v
, enums
, var_t
, entry
)
267 fprintf(h
, "%s", get_name(v
));
269 fprintf(h
, "%s_%s", enum_name
, get_name(v
));
272 write_expr(h
, v
->eval
, 0, 1, NULL
, NULL
, "");
275 if (list_next( enums
, &v
->entry
)) fprintf(h
, ",\n");
280 int needs_space_after(type_t
*t
)
282 return (type_is_alias(t
) ||
283 (!is_ptr(t
) && (!is_array(t
) || !type_array_is_decl_as_ptr(t
) || t
->name
)));
286 int decl_needs_parens(const type_t
*t
)
288 if (type_is_alias(t
))
290 if (is_array(t
) && !type_array_is_decl_as_ptr(t
))
295 static void write_pointer_left(FILE *h
, type_t
*ref
)
297 if (needs_space_after(ref
))
299 if (decl_needs_parens(ref
))
301 if (type_get_type_detect_alias(ref
) == TYPE_FUNCTION
)
303 const char *callconv
= get_attrp(ref
->attrs
, ATTR_CALLCONV
);
304 if (!callconv
&& is_object_interface
) callconv
= "STDMETHODCALLTYPE";
305 if (callconv
) fprintf(h
, "%s ", callconv
);
310 void write_type_left(FILE *h
, const decl_spec_t
*ds
, enum name_type name_type
, int declonly
, int write_callconv
)
312 type_t
*t
= ds
->type
;
317 name
= type_get_name(t
, name_type
);
319 if (ds
->func_specifier
& FUNCTION_SPECIFIER_INLINE
)
320 fprintf(h
, "inline ");
322 if ((ds
->qualifier
& TYPE_QUALIFIER_CONST
) && (type_is_alias(t
) || !is_ptr(t
)))
323 fprintf(h
, "const ");
325 if (type_is_alias(t
)) fprintf(h
, "%s", t
->name
);
327 switch (type_get_type_detect_alias(t
)) {
329 if (!declonly
&& !t
->written
) {
331 if (name
) fprintf(h
, "enum %s {\n", name
);
332 else fprintf(h
, "enum {\n");
335 write_enums(h
, type_enum_get_values(t
), is_global_namespace(t
->namespace) ? NULL
: t
->name
);
339 else fprintf(h
, "enum %s", name
? name
: "");
342 case TYPE_ENCAPSULATED_UNION
:
343 if (!declonly
&& !t
->written
) {
345 if (name
) fprintf(h
, "struct %s {\n", name
);
346 else fprintf(h
, "struct {\n");
349 if (type_get_type(t
) != TYPE_STRUCT
)
350 write_fields(h
, type_encapsulated_union_get_fields(t
));
352 write_fields(h
, type_struct_get_fields(t
));
356 else fprintf(h
, "struct %s", name
? name
: "");
359 if (!declonly
&& !t
->written
) {
361 if (t
->name
) fprintf(h
, "union %s {\n", t
->name
);
362 else fprintf(h
, "union {\n");
365 write_fields(h
, type_union_get_cases(t
));
369 else fprintf(h
, "union %s", t
->name
? t
->name
: "");
373 write_type_left(h
, type_pointer_get_ref(t
), name_type
, declonly
, FALSE
);
374 write_pointer_left(h
, type_pointer_get_ref_type(t
));
375 if (ds
->qualifier
& TYPE_QUALIFIER_CONST
) fprintf(h
, "const ");
379 if (t
->name
&& type_array_is_decl_as_ptr(t
))
380 fprintf(h
, "%s", t
->name
);
383 write_type_left(h
, type_array_get_element(t
), name_type
, declonly
, !type_array_is_decl_as_ptr(t
));
384 if (type_array_is_decl_as_ptr(t
))
385 write_pointer_left(h
, type_array_get_element_type(t
));
390 write_type_left(h
, type_function_get_ret(t
), name_type
, declonly
, TRUE
);
392 /* A pointer to a function has to write the calling convention inside
393 * the parentheses. There's no way to handle that here, so we have to
394 * use an extra parameter to tell us whether to write the calling
395 * convention or not. */
398 const char *callconv
= get_attrp(t
->attrs
, ATTR_CALLCONV
);
399 if (!callconv
&& is_object_interface
) callconv
= "STDMETHODCALLTYPE";
400 if (callconv
) fprintf(h
, " %s ", callconv
);
405 if (type_basic_get_type(t
) != TYPE_BASIC_INT32
&&
406 type_basic_get_type(t
) != TYPE_BASIC_INT64
&&
407 type_basic_get_type(t
) != TYPE_BASIC_LONG
&&
408 type_basic_get_type(t
) != TYPE_BASIC_HYPER
)
410 if (type_basic_get_sign(t
) < 0) fprintf(h
, "signed ");
411 else if (type_basic_get_sign(t
) > 0) fprintf(h
, "unsigned ");
413 switch (type_basic_get_type(t
))
415 case TYPE_BASIC_INT8
: fprintf(h
, "small"); break;
416 case TYPE_BASIC_INT16
: fprintf(h
, "short"); break;
417 case TYPE_BASIC_INT
: fprintf(h
, "int"); break;
418 case TYPE_BASIC_INT3264
: fprintf(h
, "__int3264"); break;
419 case TYPE_BASIC_BYTE
: fprintf(h
, "byte"); break;
420 case TYPE_BASIC_CHAR
: fprintf(h
, "char"); break;
421 case TYPE_BASIC_WCHAR
: fprintf(h
, "wchar_t"); break;
422 case TYPE_BASIC_FLOAT
: fprintf(h
, "float"); break;
423 case TYPE_BASIC_DOUBLE
: fprintf(h
, "double"); break;
424 case TYPE_BASIC_ERROR_STATUS_T
: fprintf(h
, "error_status_t"); break;
425 case TYPE_BASIC_HANDLE
: fprintf(h
, "handle_t"); break;
426 case TYPE_BASIC_INT32
:
427 if (type_basic_get_sign(t
) > 0)
428 fprintf(h
, "UINT32");
432 case TYPE_BASIC_LONG
:
433 if (type_basic_get_sign(t
) > 0)
438 case TYPE_BASIC_INT64
:
439 if (type_basic_get_sign(t
) > 0)
440 fprintf(h
, "UINT64");
444 case TYPE_BASIC_HYPER
:
445 if (type_basic_get_sign(t
) > 0)
446 fprintf(h
, "MIDL_uhyper");
455 fprintf(h
, "%s", t
->name
);
462 const decl_spec_t ds
= {.type
= type_bitfield_get_field(t
)};
463 write_type_left(h
, &ds
, name_type
, declonly
, TRUE
);
467 /* handled elsewhere */
474 void write_type_right(FILE *h
, type_t
*t
, int is_field
)
477 if (type_is_alias(t
)) return;
479 switch (type_get_type(t
))
483 type_t
*elem
= type_array_get_element_type(t
);
484 if (type_array_is_decl_as_ptr(t
))
486 if (decl_needs_parens(elem
))
491 if (is_conformant_array(t
))
492 fprintf(h
, "[%s]", is_field
? "1" : "");
494 fprintf(h
, "[%u]", type_array_get_dim(t
));
496 write_type_right(h
, elem
, FALSE
);
501 const var_list_t
*args
= type_function_get_args(t
);
504 write_args(h
, args
, NULL
, 0, FALSE
);
508 write_type_right(h
, type_function_get_rettype(t
), FALSE
);
513 type_t
*ref
= type_pointer_get_ref_type(t
);
514 if (decl_needs_parens(ref
))
516 write_type_right(h
, ref
, FALSE
);
520 fprintf(h
, " : %u", type_bitfield_get_bits(t
)->cval
);
526 case TYPE_ENCAPSULATED_UNION
:
536 static void write_type_v(FILE *h
, const decl_spec_t
*ds
, int is_field
, int declonly
, const char *name
)
538 type_t
*t
= ds
->type
;
543 write_type_left(h
, ds
, NAME_DEFAULT
, declonly
, TRUE
);
545 if (name
) fprintf(h
, "%s%s", !t
|| needs_space_after(t
) ? " " : "", name
);
548 write_type_right(h
, t
, is_field
);
551 static void write_type_definition(FILE *f
, type_t
*t
, int declonly
)
553 int in_namespace
= t
->namespace && !is_global_namespace(t
->namespace);
554 int save_written
= t
->written
;
555 decl_spec_t ds
= {.type
= t
};
558 fprintf(f
, "#ifdef __cplusplus\n");
559 fprintf(f
, "} /* extern \"C\" */\n");
560 write_namespace_start(f
, t
->namespace);
563 write_type_left(f
, &ds
, NAME_DEFAULT
, declonly
, TRUE
);
566 t
->written
= save_written
;
567 write_namespace_end(f
, t
->namespace);
568 fprintf(f
, "extern \"C\" {\n");
569 fprintf(f
, "#else\n");
570 write_type_left(f
, &ds
, NAME_C
, declonly
, TRUE
);
572 fprintf(f
, "#endif\n\n");
576 void write_type_decl(FILE *f
, const decl_spec_t
*t
, const char *name
)
578 write_type_v(f
, t
, FALSE
, TRUE
, name
);
581 void write_type_decl_left(FILE *f
, const decl_spec_t
*ds
)
583 write_type_left(f
, ds
, NAME_DEFAULT
, TRUE
, TRUE
);
586 static int user_type_registered(const char *name
)
589 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
590 if (!strcmp(name
, ut
->name
))
595 static int context_handle_registered(const char *name
)
597 context_handle_t
*ch
;
598 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
599 if (!strcmp(name
, ch
->name
))
604 static int generic_handle_registered(const char *name
)
606 generic_handle_t
*gh
;
607 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
608 if (!strcmp(name
, gh
->name
))
613 unsigned int get_context_handle_offset( const type_t
*type
)
615 context_handle_t
*ch
;
616 unsigned int index
= 0;
618 while (!is_attr( type
->attrs
, ATTR_CONTEXTHANDLE
))
620 if (type_is_alias( type
)) type
= type_alias_get_aliasee_type( type
);
621 else if (is_ptr( type
)) type
= type_pointer_get_ref_type( type
);
622 else error( "internal error: %s is not a context handle\n", type
->name
);
624 LIST_FOR_EACH_ENTRY( ch
, &context_handle_list
, context_handle_t
, entry
)
626 if (!strcmp( type
->name
, ch
->name
)) return index
;
629 error( "internal error: %s is not registered as a context handle\n", type
->name
);
633 unsigned int get_generic_handle_offset( const type_t
*type
)
635 generic_handle_t
*gh
;
636 unsigned int index
= 0;
638 while (!is_attr( type
->attrs
, ATTR_HANDLE
))
640 if (type_is_alias( type
)) type
= type_alias_get_aliasee_type( type
);
641 else if (is_ptr( type
)) type
= type_pointer_get_ref_type( type
);
642 else error( "internal error: %s is not a generic handle\n", type
->name
);
644 LIST_FOR_EACH_ENTRY( gh
, &generic_handle_list
, generic_handle_t
, entry
)
646 if (!strcmp( type
->name
, gh
->name
)) return index
;
649 error( "internal error: %s is not registered as a generic handle\n", type
->name
);
653 /* check for types which require additional prototypes to be generated in the
655 void check_for_additional_prototype_types(type_t
*type
)
659 const char *name
= type
->name
;
660 if (type
->user_types_registered
) break;
661 type
->user_types_registered
= 1;
662 if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
)) {
663 if (!context_handle_registered(name
))
665 context_handle_t
*ch
= xmalloc(sizeof(*ch
));
666 ch
->name
= xstrdup(name
);
667 list_add_tail(&context_handle_list
, &ch
->entry
);
669 /* don't carry on parsing fields within this type */
672 if ((type_get_type(type
) != TYPE_BASIC
||
673 type_basic_get_type(type
) != TYPE_BASIC_HANDLE
) &&
674 is_attr(type
->attrs
, ATTR_HANDLE
)) {
675 if (!generic_handle_registered(name
))
677 generic_handle_t
*gh
= xmalloc(sizeof(*gh
));
678 gh
->name
= xstrdup(name
);
679 list_add_tail(&generic_handle_list
, &gh
->entry
);
681 /* don't carry on parsing fields within this type */
684 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
)) {
685 if (!user_type_registered(name
))
687 user_type_t
*ut
= xmalloc(sizeof *ut
);
688 ut
->name
= xstrdup(name
);
689 list_add_tail(&user_type_list
, &ut
->entry
);
691 /* don't carry on parsing fields within this type as we are already
692 * using a wire marshaled type */
695 else if (type_is_complete(type
))
699 switch (type_get_type_detect_alias(type
))
702 vars
= type_enum_get_values(type
);
705 vars
= type_struct_get_fields(type
);
708 vars
= type_union_get_cases(type
);
714 if (vars
) LIST_FOR_EACH_ENTRY( v
, vars
, const var_t
, entry
)
715 check_for_additional_prototype_types(v
->declspec
.type
);
718 if (type_is_alias(type
))
719 type
= type_alias_get_aliasee_type(type
);
720 else if (is_ptr(type
))
721 type
= type_pointer_get_ref_type(type
);
722 else if (is_array(type
))
723 type
= type_array_get_element_type(type
);
729 static int write_serialize_function_decl(FILE *header
, const type_t
*type
)
731 write_serialize_functions(header
, type
, NULL
);
735 static int serializable_exists(FILE *header
, const type_t
*type
)
740 static int for_each_serializable(const statement_list_t
*stmts
, FILE *header
,
741 int (*proc
)(FILE*, const type_t
*))
743 statement_t
*stmt
, *iface_stmt
;
744 statement_list_t
*iface_stmts
;
745 const type_list_t
*type_entry
;
747 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, statement_t
, entry
)
749 if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
752 iface_stmts
= type_iface_get_stmts(stmt
->u
.type
);
753 if (iface_stmts
) LIST_FOR_EACH_ENTRY( iface_stmt
, iface_stmts
, statement_t
, entry
)
755 if (iface_stmt
->type
!= STMT_TYPEDEF
) continue;
756 for (type_entry
= iface_stmt
->u
.type_list
; type_entry
; type_entry
= type_entry
->next
)
758 if (!is_attr(type_entry
->type
->attrs
, ATTR_ENCODE
)
759 && !is_attr(type_entry
->type
->attrs
, ATTR_DECODE
))
761 if (!proc(header
, type_entry
->type
))
770 static void write_user_types(FILE *header
)
773 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
775 const char *name
= ut
->name
;
776 fprintf(header
, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name
, name
);
777 fprintf(header
, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name
, name
);
778 fprintf(header
, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name
, name
);
779 fprintf(header
, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name
, name
);
783 static void write_context_handle_rundowns(FILE *header
)
785 context_handle_t
*ch
;
786 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
788 const char *name
= ch
->name
;
789 fprintf(header
, "void __RPC_USER %s_rundown(%s);\n", name
, name
);
793 static void write_generic_handle_routines(FILE *header
)
795 generic_handle_t
*gh
;
796 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
798 const char *name
= gh
->name
;
799 fprintf(header
, "handle_t __RPC_USER %s_bind(%s);\n", name
, name
);
800 fprintf(header
, "void __RPC_USER %s_unbind(%s, handle_t);\n", name
, name
);
804 static void write_typedef(FILE *header
, type_t
*type
, int declonly
)
806 fprintf(header
, "typedef ");
807 write_type_v(header
, type_alias_get_aliasee(type
), FALSE
, declonly
, type
->name
);
808 fprintf(header
, ";\n");
811 int is_const_decl(const var_t
*var
)
813 const decl_spec_t
*t
;
814 /* strangely, MIDL accepts a const attribute on any pointer in the
815 * declaration to mean that data isn't being instantiated. this appears
816 * to be a bug, but there is no benefit to being incompatible with MIDL,
817 * so we'll do the same thing */
818 for (t
= &var
->declspec
; ; )
820 if (t
->qualifier
& TYPE_QUALIFIER_CONST
)
822 else if (is_ptr(t
->type
))
823 t
= type_pointer_get_ref(t
->type
);
829 static void write_declaration(FILE *header
, const var_t
*v
)
831 if (is_const_decl(v
) && v
->eval
)
833 fprintf(header
, "#define %s (", v
->name
);
834 write_expr(header
, v
->eval
, 0, 1, NULL
, NULL
, "");
835 fprintf(header
, ")\n\n");
839 switch (v
->declspec
.stgclass
)
842 case STG_REGISTER
: /* ignored */
845 fprintf(header
, "static ");
848 fprintf(header
, "extern ");
851 write_type_v(header
, &v
->declspec
, FALSE
, v
->declonly
, v
->name
);
852 fprintf(header
, ";\n\n");
856 static void write_library(FILE *header
, const typelib_t
*typelib
)
858 const UUID
*uuid
= get_attrp(typelib
->attrs
, ATTR_UUID
);
859 fprintf(header
, "\n");
860 write_guid(header
, "LIBID", typelib
->name
, uuid
);
861 fprintf(header
, "\n");
865 const type_t
* get_explicit_generic_handle_type(const var_t
* var
)
868 for (t
= var
->declspec
.type
;
869 is_ptr(t
) || type_is_alias(t
);
870 t
= type_is_alias(t
) ? type_alias_get_aliasee_type(t
) : type_pointer_get_ref_type(t
))
871 if ((type_get_type_detect_alias(t
) != TYPE_BASIC
|| type_basic_get_type(t
) != TYPE_BASIC_HANDLE
) &&
872 is_attr(t
->attrs
, ATTR_HANDLE
))
877 const var_t
*get_func_handle_var( const type_t
*iface
, const var_t
*func
,
878 unsigned char *explicit_fc
, unsigned char *implicit_fc
)
881 const var_list_t
*args
= type_function_get_args( func
->declspec
.type
);
883 *explicit_fc
= *implicit_fc
= 0;
884 if (args
) LIST_FOR_EACH_ENTRY( var
, args
, const var_t
, entry
)
886 if (!is_attr( var
->attrs
, ATTR_IN
) && is_attr( var
->attrs
, ATTR_OUT
)) continue;
887 if (type_get_type( var
->declspec
.type
) == TYPE_BASIC
&& type_basic_get_type( var
->declspec
.type
) == TYPE_BASIC_HANDLE
)
889 *explicit_fc
= FC_BIND_PRIMITIVE
;
892 if (get_explicit_generic_handle_type( var
))
894 *explicit_fc
= FC_BIND_GENERIC
;
897 if (is_context_handle( var
->declspec
.type
))
899 *explicit_fc
= FC_BIND_CONTEXT
;
904 if ((var
= get_attrp( iface
->attrs
, ATTR_IMPLICIT_HANDLE
)))
906 if (type_get_type( var
->declspec
.type
) == TYPE_BASIC
&&
907 type_basic_get_type( var
->declspec
.type
) == TYPE_BASIC_HANDLE
)
908 *implicit_fc
= FC_BIND_PRIMITIVE
;
910 *implicit_fc
= FC_BIND_GENERIC
;
914 *implicit_fc
= FC_AUTO_HANDLE
;
918 int has_out_arg_or_return(const var_t
*func
)
922 if (!is_void(type_function_get_rettype(func
->declspec
.type
)))
925 if (!type_function_get_args(func
->declspec
.type
))
928 LIST_FOR_EACH_ENTRY( var
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
929 if (is_attr(var
->attrs
, ATTR_OUT
))
936 /********** INTERFACES **********/
938 int is_object(const type_t
*iface
)
941 if (type_is_defined(iface
) && type_iface_get_inherit(iface
))
943 if (iface
->attrs
) LIST_FOR_EACH_ENTRY( attr
, iface
->attrs
, const attr_t
, entry
)
944 if (attr
->type
== ATTR_OBJECT
|| attr
->type
== ATTR_ODL
) return 1;
948 int is_local(const attr_list_t
*a
)
950 return is_attr(a
, ATTR_LOCAL
);
953 const var_t
*is_callas(const attr_list_t
*a
)
955 return get_attrp(a
, ATTR_CALLAS
);
958 static int is_inherited_method(const type_t
*iface
, const var_t
*func
)
960 while ((iface
= type_iface_get_inherit(iface
)))
962 const statement_t
*stmt
;
963 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
965 const var_t
*funccmp
= stmt
->u
.var
;
967 if (!is_callas(func
->attrs
))
969 char inherit_name
[256];
970 /* compare full name including property prefix */
971 strcpy(inherit_name
, get_name(funccmp
));
972 if (!strcmp(inherit_name
, get_name(func
))) return 1;
980 static int is_override_method(const type_t
*iface
, const type_t
*child
, const var_t
*func
)
987 const statement_t
*stmt
;
988 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(child
))
990 const var_t
*funccmp
= stmt
->u
.var
;
992 if (!is_callas(func
->attrs
))
994 char inherit_name
[256];
995 /* compare full name including property prefix */
996 strcpy(inherit_name
, get_name(funccmp
));
997 if (!strcmp(inherit_name
, get_name(func
))) return 1;
1001 while ((child
= type_iface_get_inherit(child
)) && child
!= iface
);
1006 static int is_aggregate_return(const var_t
*func
)
1008 enum type_type type
= type_get_type(type_function_get_rettype(func
->declspec
.type
));
1009 return type
== TYPE_STRUCT
|| type
== TYPE_UNION
||
1010 type
== TYPE_COCLASS
|| type
== TYPE_INTERFACE
;
1013 static char *get_vtbl_entry_name(const type_t
*iface
, const var_t
*func
)
1015 static char buff
[255];
1016 if (is_inherited_method(iface
, func
))
1017 sprintf(buff
, "%s_%s", iface
->name
, get_name(func
));
1019 sprintf(buff
, "%s", get_name(func
));
1023 static void write_method_macro(FILE *header
, const type_t
*iface
, const type_t
*child
, const char *name
)
1025 const statement_t
*stmt
;
1026 int first_iface
= 1;
1028 if (type_iface_get_inherit(iface
))
1029 write_method_macro(header
, type_iface_get_inherit(iface
), child
, name
);
1031 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1033 const var_t
*func
= stmt
->u
.var
;
1037 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1041 if (is_override_method(iface
, child
, func
))
1044 if (!is_callas(func
->attrs
)) {
1047 fprintf(header
, "#define %s_%s(This", name
, get_name(func
));
1048 if (type_function_get_args(func
->declspec
.type
))
1049 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1050 fprintf(header
, ",%s", arg
->name
);
1051 fprintf(header
, ") ");
1053 if (is_aggregate_return(func
))
1055 fprintf(header
, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name
, get_name(func
));
1059 fprintf(header
, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface
, func
));
1060 if (type_function_get_args(func
->declspec
.type
))
1061 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1062 fprintf(header
, ",%s", arg
->name
);
1063 fprintf(header
, ")\n");
1068 void write_args(FILE *h
, const var_list_t
*args
, const char *name
, int method
, int do_indent
)
1079 fprintf(h
, "%s* This", name
);
1082 if (args
) LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
) {
1089 else fprintf(h
, ",");
1091 /* In theory we should be writing the definition using write_type_v(..., arg->declonly),
1092 * but that causes redefinition in e.g. proxy files. In fact MIDL disallows
1093 * defining UDTs inside of an argument list. */
1094 write_type_decl(h
, &arg
->declspec
, arg
->name
);
1096 const expr_t
*expr
= get_attrp(arg
->attrs
, ATTR_DEFAULTVALUE
);
1098 const var_t
*tail_arg
;
1100 /* Output default value only if all following arguments also have default value. */
1101 LIST_FOR_EACH_ENTRY_REV( tail_arg
, args
, const var_t
, entry
) {
1102 if(tail_arg
== arg
) {
1105 /* Fixup the expression type for a BSTR like midl does. */
1106 if (get_type_vt(arg
->declspec
.type
) == VT_BSTR
&& expr
->type
== EXPR_STRLIT
)
1109 bstr
.type
= EXPR_WSTRLIT
;
1114 write_expr( h
, expr
, 0, 1, NULL
, NULL
, "" );
1117 if(!get_attrp(tail_arg
->attrs
, ATTR_DEFAULTVALUE
))
1124 if (do_indent
) indentation
--;
1127 static void write_cpp_method_def(FILE *header
, const type_t
*iface
)
1129 const statement_t
*stmt
;
1131 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1133 const var_t
*func
= stmt
->u
.var
;
1134 if (!is_callas(func
->attrs
)) {
1135 const decl_spec_t
*ret
= type_function_get_ret(func
->declspec
.type
);
1136 const char *callconv
= get_attrp(func
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1137 const var_list_t
*args
= type_function_get_args(func
->declspec
.type
);
1140 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1142 if (is_aggregate_return(func
)) {
1143 fprintf(header
, "#ifdef WIDL_EXPLICIT_AGGREGATE_RETURNS\n");
1146 fprintf(header
, "virtual ");
1147 write_type_decl_left(header
, ret
);
1148 fprintf(header
, "* %s %s(\n", callconv
, get_name(func
));
1151 write_type_decl_left(header
, ret
);
1152 fprintf(header
, " *__ret");
1155 fprintf(header
, ",\n");
1156 write_args(header
, args
, iface
->name
, 2, TRUE
);
1158 fprintf(header
, ") = 0;\n");
1161 write_type_decl_left(header
, ret
);
1162 fprintf(header
, " %s %s(\n", callconv
, get_name(func
));
1163 write_args(header
, args
, iface
->name
, 2, TRUE
);
1164 fprintf(header
, ")\n");
1166 fprintf(header
, "{\n");
1169 write_type_decl_left(header
, ret
);
1170 fprintf(header
, " __ret;\n");
1172 fprintf(header
, "return *%s(&__ret", get_name(func
));
1174 LIST_FOR_EACH_ENTRY(arg
, args
, const var_t
, entry
)
1175 fprintf(header
, ", %s", arg
->name
);
1176 fprintf(header
, ");\n");
1179 fprintf(header
, "}\n");
1181 fprintf(header
, "#else\n");
1185 fprintf(header
, "virtual ");
1186 write_type_decl_left(header
, ret
);
1187 fprintf(header
, " %s %s(\n", callconv
, get_name(func
));
1188 write_args(header
, args
, iface
->name
, 2, TRUE
);
1189 fprintf(header
, ") = 0;\n");
1191 if (is_aggregate_return(func
))
1192 fprintf(header
, "#endif\n");
1193 fprintf(header
, "\n");
1198 static void write_inline_wrappers(FILE *header
, const type_t
*iface
, const type_t
*child
, const char *name
)
1200 const statement_t
*stmt
;
1201 int first_iface
= 1;
1203 if (type_iface_get_inherit(iface
))
1204 write_inline_wrappers(header
, type_iface_get_inherit(iface
), child
, name
);
1206 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1208 const var_t
*func
= stmt
->u
.var
;
1212 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1216 if (is_override_method(iface
, child
, func
))
1219 if (!is_callas(func
->attrs
)) {
1222 fprintf(header
, "static FORCEINLINE ");
1223 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1224 fprintf(header
, " %s_%s(", name
, get_name(func
));
1225 write_args(header
, type_function_get_args(func
->declspec
.type
), name
, 1, FALSE
);
1226 fprintf(header
, ") {\n");
1228 if (!is_aggregate_return(func
)) {
1230 fprintf(header
, "%sThis->lpVtbl->%s(This",
1231 is_void(type_function_get_rettype(func
->declspec
.type
)) ? "" : "return ",
1232 get_vtbl_entry_name(iface
, func
));
1235 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1236 fprintf(header
, " __ret;\n");
1238 fprintf(header
, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface
, func
));
1240 if (type_function_get_args(func
->declspec
.type
))
1241 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), const var_t
, entry
)
1242 fprintf(header
, ",%s", arg
->name
);
1243 fprintf(header
, ");\n");
1245 fprintf(header
, "}\n");
1250 static void do_write_c_method_def(FILE *header
, const type_t
*iface
, const char *name
)
1252 const statement_t
*stmt
;
1253 int first_iface
= 1;
1255 if (type_iface_get_inherit(iface
))
1256 do_write_c_method_def(header
, type_iface_get_inherit(iface
), name
);
1258 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1260 const var_t
*func
= stmt
->u
.var
;
1263 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
1266 if (!is_callas(func
->attrs
)) {
1267 const char *callconv
= get_attrp(func
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1268 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1270 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1271 if (is_aggregate_return(func
))
1272 fprintf(header
, " *");
1273 if (is_inherited_method(iface
, func
))
1274 fprintf(header
, " (%s *%s_%s)(\n", callconv
, iface
->name
, func
->name
);
1276 fprintf(header
, " (%s *%s)(\n", callconv
, get_name(func
));
1279 fprintf(header
, "%s *This", name
);
1280 if (is_aggregate_return(func
)) {
1281 fprintf(header
, ",\n");
1283 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1284 fprintf(header
, " *__ret");
1287 if (type_function_get_args(func
->declspec
.type
)) {
1288 fprintf(header
, ",\n");
1289 write_args(header
, type_function_get_args(func
->declspec
.type
), name
, 0, TRUE
);
1291 fprintf(header
, ");\n");
1292 fprintf(header
, "\n");
1297 static void write_c_method_def(FILE *header
, const type_t
*iface
)
1299 do_write_c_method_def(header
, iface
, iface
->c_name
);
1302 static void write_c_disp_method_def(FILE *header
, const type_t
*iface
)
1304 do_write_c_method_def(header
, type_iface_get_inherit(iface
), iface
->c_name
);
1307 static void write_method_proto(FILE *header
, const type_t
*iface
)
1309 const statement_t
*stmt
;
1311 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
1313 const var_t
*func
= stmt
->u
.var
;
1315 if (is_callas(func
->attrs
)) {
1316 const char *callconv
= get_attrp(func
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1317 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
1318 /* proxy prototype */
1319 write_type_decl_left(header
, type_function_get_ret(func
->declspec
.type
));
1320 fprintf(header
, " %s %s_%s_Proxy(\n", callconv
, iface
->name
, get_name(func
));
1321 write_args(header
, type_function_get_args(func
->declspec
.type
), iface
->name
, 1, TRUE
);
1322 fprintf(header
, ");\n");
1323 /* stub prototype */
1324 fprintf(header
, "void __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(func
));
1325 fprintf(header
, " IRpcStubBuffer* This,\n");
1326 fprintf(header
, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
1327 fprintf(header
, " PRPC_MESSAGE pRpcMessage,\n");
1328 fprintf(header
, " DWORD* pdwStubPhase);\n");
1333 static void write_locals(FILE *fp
, const type_t
*iface
, int body
)
1335 static const char comment
[]
1336 = "/* WIDL-generated stub. You must provide an implementation for this. */";
1337 const statement_t
*stmt
;
1339 if (!is_object(iface
))
1342 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
)) {
1343 const var_t
*func
= stmt
->u
.var
;
1344 const var_t
*cas
= is_callas(func
->attrs
);
1347 const statement_t
*stmt2
= NULL
;
1348 STATEMENTS_FOR_EACH_FUNC(stmt2
, type_iface_get_stmts(iface
))
1349 if (!strcmp(get_name(stmt2
->u
.var
), cas
->name
))
1351 if (&stmt2
->entry
!= type_iface_get_stmts(iface
)) {
1352 const var_t
*m
= stmt2
->u
.var
;
1353 /* proxy prototype - use local prototype */
1354 write_type_decl_left(fp
, type_function_get_ret(m
->declspec
.type
));
1355 fprintf(fp
, " CALLBACK %s_%s_Proxy(\n", iface
->name
, get_name(m
));
1356 write_args(fp
, type_function_get_args(m
->declspec
.type
), iface
->name
, 1, TRUE
);
1359 const decl_spec_t
*rt
= type_function_get_ret(m
->declspec
.type
);
1360 fprintf(fp
, "\n{\n");
1361 fprintf(fp
, " %s\n", comment
);
1362 if (rt
->type
->name
&& strcmp(rt
->type
->name
, "HRESULT") == 0)
1363 fprintf(fp
, " return E_NOTIMPL;\n");
1364 else if (type_get_type(rt
->type
) != TYPE_VOID
) {
1366 write_type_decl(fp
, rt
, "rv");
1368 fprintf(fp
, " memset(&rv, 0, sizeof rv);\n");
1369 fprintf(fp
, " return rv;\n");
1371 fprintf(fp
, "}\n\n");
1375 /* stub prototype - use remotable prototype */
1376 write_type_decl_left(fp
, type_function_get_ret(func
->declspec
.type
));
1377 fprintf(fp
, " __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(m
));
1378 write_args(fp
, type_function_get_args(func
->declspec
.type
), iface
->name
, 1, TRUE
);
1381 /* Remotable methods must all return HRESULTs. */
1382 fprintf(fp
, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment
);
1387 error_loc("invalid call_as attribute (%s -> %s)\n", func
->name
, cas
->name
);
1392 static void write_local_stubs_stmts(FILE *local_stubs
, const statement_list_t
*stmts
)
1394 const statement_t
*stmt
;
1395 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1397 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1398 write_locals(local_stubs
, stmt
->u
.type
, TRUE
);
1402 void write_local_stubs(const statement_list_t
*stmts
)
1406 if (!local_stubs_name
) return;
1408 local_stubs
= fopen(local_stubs_name
, "w");
1410 error("Could not open %s for output\n", local_stubs_name
);
1413 fprintf(local_stubs
, "/* call_as/local stubs for %s */\n\n", input_name
);
1414 fprintf(local_stubs
, "#include <objbase.h>\n");
1415 fprintf(local_stubs
, "#include \"%s\"\n\n", header_name
);
1417 write_local_stubs_stmts(local_stubs
, stmts
);
1419 fclose(local_stubs
);
1422 static void write_function_proto(FILE *header
, const type_t
*iface
, const var_t
*fun
, const char *prefix
)
1424 const char *callconv
= get_attrp(fun
->declspec
.type
->attrs
, ATTR_CALLCONV
);
1426 if (!callconv
) callconv
= "__cdecl";
1427 /* FIXME: do we need to handle call_as? */
1428 write_type_decl_left(header
, type_function_get_ret(fun
->declspec
.type
));
1429 fprintf(header
, " %s ", callconv
);
1430 fprintf(header
, "%s%s(\n", prefix
, get_name(fun
));
1431 if (type_function_get_args(fun
->declspec
.type
))
1432 write_args(header
, type_function_get_args(fun
->declspec
.type
), iface
->name
, 0, TRUE
);
1434 fprintf(header
, " void");
1435 fprintf(header
, ");\n\n");
1438 static void write_forward(FILE *header
, type_t
*iface
)
1440 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", iface
->c_name
);
1441 fprintf(header
, "#define __%s_FWD_DEFINED__\n", iface
->c_name
);
1442 fprintf(header
, "typedef interface %s %s;\n", iface
->c_name
, iface
->c_name
);
1443 fprintf(header
, "#ifdef __cplusplus\n");
1444 write_namespace_start(header
, iface
->namespace);
1445 write_line(header
, 0, "interface %s;", iface
->name
);
1446 write_namespace_end(header
, iface
->namespace);
1447 fprintf(header
, "#endif /* __cplusplus */\n");
1448 fprintf(header
, "#endif\n\n" );
1451 static void write_com_interface_start(FILE *header
, const type_t
*iface
)
1453 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
1454 fprintf(header
, "/*****************************************************************************\n");
1455 fprintf(header
, " * %s %sinterface\n", iface
->name
, dispinterface
? "disp" : "");
1456 fprintf(header
, " */\n");
1457 fprintf(header
,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface
->c_name
, dispinterface
? "DISP" : "");
1458 fprintf(header
,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface
->c_name
, dispinterface
? "DISP" : "");
1461 static void write_com_interface_end(FILE *header
, type_t
*iface
)
1463 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
1464 const UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
1468 write_guid(header
, dispinterface
? "DIID" : "IID", iface
->c_name
, uuid
);
1471 fprintf(header
, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1472 if (!is_global_namespace(iface
->namespace)) {
1473 write_line(header
, 0, "} /* extern \"C\" */");
1474 write_namespace_start(header
, iface
->namespace);
1477 write_line(header
, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid
));
1481 fprintf(header
, "interface ");
1483 if (type_iface_get_inherit(iface
))
1485 fprintf(header
, "%s : public %s\n", iface
->name
,
1486 type_iface_get_inherit(iface
)->name
);
1487 write_line(header
, 1, "{");
1491 fprintf(header
, "%s\n", iface
->name
);
1492 write_line(header
, 1, "{\n");
1493 write_line(header
, 0, "BEGIN_INTERFACE\n");
1495 /* dispinterfaces don't have real functions, so don't write C++ functions for
1498 write_cpp_method_def(header
, iface
);
1499 if (!type_iface_get_inherit(iface
))
1500 write_line(header
, 0, "END_INTERFACE\n");
1501 write_line(header
, -1, "};");
1502 if (!is_global_namespace(iface
->namespace)) {
1503 write_namespace_end(header
, iface
->namespace);
1504 write_line(header
, 0, "extern \"C\" {");
1507 write_uuid_decl(header
, iface
, uuid
);
1508 fprintf(header
, "#else\n");
1510 write_line(header
, 1, "typedef struct %sVtbl {", iface
->c_name
);
1511 write_line(header
, 0, "BEGIN_INTERFACE\n");
1513 write_c_disp_method_def(header
, iface
);
1515 write_c_method_def(header
, iface
);
1516 write_line(header
, 0, "END_INTERFACE");
1517 write_line(header
, -1, "} %sVtbl;\n", iface
->c_name
);
1518 fprintf(header
, "interface %s {\n", iface
->c_name
);
1519 fprintf(header
, " CONST_VTBL %sVtbl* lpVtbl;\n", iface
->c_name
);
1520 fprintf(header
, "};\n\n");
1521 fprintf(header
, "#ifdef COBJMACROS\n");
1522 /* dispinterfaces don't have real functions, so don't write macros for them,
1523 * only for the interface this interface inherits from, i.e. IDispatch */
1524 fprintf(header
, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
1525 type
= dispinterface
? type_iface_get_inherit(iface
) : iface
;
1526 write_method_macro(header
, type
, type
, iface
->c_name
);
1527 fprintf(header
, "#else\n");
1528 write_inline_wrappers(header
, type
, type
, iface
->c_name
);
1529 fprintf(header
, "#endif\n");
1530 fprintf(header
, "#endif\n");
1531 fprintf(header
, "\n");
1532 fprintf(header
, "#endif\n");
1533 fprintf(header
, "\n");
1534 /* dispinterfaces don't have real functions, so don't write prototypes for
1536 if (!dispinterface
&& !winrt_mode
)
1538 write_method_proto(header
, iface
);
1539 write_locals(header
, iface
, FALSE
);
1540 fprintf(header
, "\n");
1542 fprintf(header
,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface
->c_name
, dispinterface
? "DISP" : "");
1545 static void write_rpc_interface_start(FILE *header
, const type_t
*iface
)
1547 unsigned int ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
1548 const var_t
*var
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
1550 fprintf(header
, "/*****************************************************************************\n");
1551 fprintf(header
, " * %s interface (v%d.%d)\n", iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1552 fprintf(header
, " */\n");
1553 fprintf(header
,"#ifndef __%s_INTERFACE_DEFINED__\n", iface
->name
);
1554 fprintf(header
,"#define __%s_INTERFACE_DEFINED__\n\n", iface
->name
);
1557 fprintf(header
, "extern ");
1558 write_type_decl( header
, &var
->declspec
, var
->name
);
1559 fprintf(header
, ";\n");
1563 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client
, iface
->name
);
1564 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server
, iface
->name
);
1568 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1569 prefix_client
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1570 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1571 prefix_server
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1575 static void write_rpc_interface_end(FILE *header
, const type_t
*iface
)
1577 fprintf(header
,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface
->name
);
1580 static void write_coclass(FILE *header
, type_t
*cocl
)
1582 const UUID
*uuid
= get_attrp(cocl
->attrs
, ATTR_UUID
);
1584 fprintf(header
, "/*****************************************************************************\n");
1585 fprintf(header
, " * %s coclass\n", cocl
->name
);
1586 fprintf(header
, " */\n\n");
1588 write_guid(header
, "CLSID", cocl
->name
, uuid
);
1589 fprintf(header
, "\n#ifdef __cplusplus\n");
1592 fprintf(header
, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid
), cocl
->name
);
1593 write_uuid_decl(header
, cocl
, uuid
);
1597 fprintf(header
, "class %s;\n", cocl
->name
);
1599 fprintf(header
, "#endif\n");
1600 fprintf(header
, "\n");
1603 static void write_coclass_forward(FILE *header
, type_t
*cocl
)
1605 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", cocl
->name
);
1606 fprintf(header
, "#define __%s_FWD_DEFINED__\n", cocl
->name
);
1607 fprintf(header
, "#ifdef __cplusplus\n");
1608 fprintf(header
, "typedef class %s %s;\n", cocl
->name
, cocl
->name
);
1609 fprintf(header
, "#else\n");
1610 fprintf(header
, "typedef struct %s %s;\n", cocl
->name
, cocl
->name
);
1611 fprintf(header
, "#endif /* defined __cplusplus */\n");
1612 fprintf(header
, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl
->name
);
1615 static void write_import(FILE *header
, const char *fname
)
1619 hname
= dup_basename(fname
, ".idl");
1620 p
= hname
+ strlen(hname
) - 2;
1621 if (p
<= hname
|| strcmp( p
, ".h" )) strcat(hname
, ".h");
1623 fprintf(header
, "#include <%s>\n", hname
);
1627 static void write_imports(FILE *header
, const statement_list_t
*stmts
)
1629 const statement_t
*stmt
;
1630 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1635 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1636 write_imports(header
, type_iface_get_stmts(stmt
->u
.type
));
1639 case STMT_IMPORTLIB
:
1640 /* not included in header */
1643 write_import(header
, stmt
->u
.str
);
1649 case STMT_DECLARATION
:
1650 /* not processed here */
1653 write_imports(header
, stmt
->u
.lib
->stmts
);
1659 static void write_forward_decls(FILE *header
, const statement_list_t
*stmts
)
1661 const statement_t
*stmt
;
1662 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1667 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1669 type_t
*iface
= stmt
->u
.type
;
1670 if (is_object(iface
) || is_attr(iface
->attrs
, ATTR_DISPINTERFACE
))
1672 write_forward(header
, iface
);
1673 if (type_iface_get_async_iface(iface
))
1674 write_forward(header
, type_iface_get_async_iface(iface
));
1677 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
1678 write_coclass_forward(header
, stmt
->u
.type
);
1681 case STMT_IMPORTLIB
:
1682 /* not included in header */
1689 case STMT_DECLARATION
:
1690 /* not processed here */
1693 write_forward_decls(header
, stmt
->u
.lib
->stmts
);
1699 static void write_header_stmts(FILE *header
, const statement_list_t
*stmts
, const type_t
*iface
, int ignore_funcs
)
1701 const statement_t
*stmt
;
1702 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1707 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1709 type_t
*iface
= stmt
->u
.type
;
1710 type_t
*async_iface
= type_iface_get_async_iface(iface
);
1711 if (is_object(iface
)) is_object_interface
++;
1712 if (is_attr(stmt
->u
.type
->attrs
, ATTR_DISPINTERFACE
) || is_object(stmt
->u
.type
))
1714 write_com_interface_start(header
, iface
);
1715 write_header_stmts(header
, type_iface_get_stmts(iface
), stmt
->u
.type
, TRUE
);
1716 write_com_interface_end(header
, iface
);
1719 write_com_interface_start(header
, async_iface
);
1720 write_com_interface_end(header
, async_iface
);
1725 write_rpc_interface_start(header
, iface
);
1726 write_header_stmts(header
, type_iface_get_stmts(iface
), iface
, FALSE
);
1727 write_rpc_interface_end(header
, iface
);
1729 if (is_object(iface
)) is_object_interface
--;
1731 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
1732 write_coclass(header
, stmt
->u
.type
);
1735 write_type_definition(header
, stmt
->u
.type
, stmt
->declonly
);
1739 /* FIXME: shouldn't write out forward declarations for undefined
1740 * interfaces but a number of our IDL files depend on this */
1741 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
&& !stmt
->u
.type
->written
)
1742 write_forward(header
, stmt
->u
.type
);
1744 case STMT_IMPORTLIB
:
1747 /* not included in header */
1750 /* not processed here */
1754 const type_list_t
*type_entry
= stmt
->u
.type_list
;
1755 for (; type_entry
; type_entry
= type_entry
->next
)
1756 write_typedef(header
, type_entry
->type
, stmt
->declonly
);
1760 write_library(header
, stmt
->u
.lib
);
1761 write_header_stmts(header
, stmt
->u
.lib
->stmts
, NULL
, FALSE
);
1764 fprintf(header
, "%s\n", stmt
->u
.str
);
1766 case STMT_DECLARATION
:
1767 if (iface
&& type_get_type(stmt
->u
.var
->declspec
.type
) == TYPE_FUNCTION
)
1771 int prefixes_differ
= strcmp(prefix_client
, prefix_server
);
1773 if (prefixes_differ
)
1775 fprintf(header
, "/* client prototype */\n");
1776 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_client
);
1777 fprintf(header
, "/* server prototype */\n");
1779 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_server
);
1783 write_declaration(header
, stmt
->u
.var
);
1789 void write_header(const statement_list_t
*stmts
)
1793 if (!do_header
) return;
1795 if(!(header
= fopen(header_name
, "w"))) {
1796 error("Could not open %s for output\n", header_name
);
1799 fprintf(header
, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION
, input_name
);
1801 fprintf(header
, "#ifdef _WIN32\n");
1802 fprintf(header
, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
1803 fprintf(header
, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
1804 fprintf(header
, "#endif\n");
1805 fprintf(header
, "#include <rpc.h>\n" );
1806 fprintf(header
, "#include <rpcndr.h>\n" );
1807 if (!for_each_serializable(stmts
, NULL
, serializable_exists
))
1808 fprintf(header
, "#include <midles.h>\n" );
1809 fprintf(header
, "#endif\n\n");
1811 fprintf(header
, "#ifndef COM_NO_WINDOWS_H\n");
1812 fprintf(header
, "#include <windows.h>\n");
1813 fprintf(header
, "#include <ole2.h>\n");
1814 fprintf(header
, "#endif\n\n");
1816 fprintf(header
, "#ifndef __%s__\n", header_token
);
1817 fprintf(header
, "#define __%s__\n\n", header_token
);
1819 fprintf(header
, "/* Forward declarations */\n\n");
1820 write_forward_decls(header
, stmts
);
1822 fprintf(header
, "/* Headers for imported files */\n\n");
1823 write_imports(header
, stmts
);
1824 fprintf(header
, "\n");
1825 start_cplusplus_guard(header
);
1827 write_header_stmts(header
, stmts
, NULL
, FALSE
);
1829 fprintf(header
, "/* Begin additional prototypes for all interfaces */\n");
1830 fprintf(header
, "\n");
1831 for_each_serializable(stmts
, header
, write_serialize_function_decl
);
1832 write_user_types(header
);
1833 write_generic_handle_routines(header
);
1834 write_context_handle_rundowns(header
);
1835 fprintf(header
, "\n");
1836 fprintf(header
, "/* End additional prototypes */\n");
1837 fprintf(header
, "\n");
1839 end_cplusplus_guard(header
);
1840 fprintf(header
, "#endif /* __%s__ */\n", header_token
);