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
39 static int indentation
= 0;
40 static int is_object_interface
= 0;
41 user_type_list_t user_type_list
= LIST_INIT(user_type_list
);
42 context_handle_list_t context_handle_list
= LIST_INIT(context_handle_list
);
43 generic_handle_list_t generic_handle_list
= LIST_INIT(generic_handle_list
);
45 static void write_type_def_or_decl(FILE *f
, type_t
*t
, int field
, const char *name
);
47 static void indent(FILE *h
, int delta
)
50 if (delta
< 0) indentation
+= delta
;
51 for (c
=0; c
<indentation
; c
++) fprintf(h
, " ");
52 if (delta
> 0) indentation
+= delta
;
55 int is_ptrchain_attr(const var_t
*var
, enum attr_type t
)
57 if (is_attr(var
->attrs
, t
))
61 type_t
*type
= var
->type
;
64 if (is_attr(type
->attrs
, t
))
66 else if (type_is_alias(type
))
67 type
= type_alias_get_aliasee(type
);
68 else if (is_ptr(type
))
69 type
= type_pointer_get_ref(type
);
75 int is_aliaschain_attr(const type_t
*type
, enum attr_type attr
)
77 const type_t
*t
= type
;
80 if (is_attr(t
->attrs
, attr
))
82 else if (type_is_alias(t
))
83 t
= type_alias_get_aliasee(t
);
88 int is_attr(const attr_list_t
*list
, enum attr_type t
)
91 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
92 if (attr
->type
== t
) return 1;
96 void *get_attrp(const attr_list_t
*list
, enum attr_type t
)
99 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
100 if (attr
->type
== t
) return attr
->u
.pval
;
104 unsigned int get_attrv(const attr_list_t
*list
, enum attr_type t
)
107 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
108 if (attr
->type
== t
) return attr
->u
.ival
;
112 int is_void(const type_t
*t
)
114 return type_get_type(t
) == TYPE_VOID
;
117 int is_conformant_array(const type_t
*t
)
119 return is_array(t
) && type_array_has_conformance(t
);
122 void write_guid(FILE *f
, const char *guid_prefix
, const char *name
, const UUID
*uuid
)
125 fprintf(f
, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
126 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
127 guid_prefix
, name
, uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0],
128 uuid
->Data4
[1], uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5],
129 uuid
->Data4
[6], uuid
->Data4
[7]);
132 static void write_uuid_decl(FILE *f
, const char *name
, const UUID
*uuid
)
134 fprintf(f
, "#ifdef __CRT_UUID_DECL\n");
135 fprintf(f
, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
136 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
137 name
, uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
138 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6],
140 fprintf(f
, "#endif\n");
143 static const char *uuid_string(const UUID
*uuid
)
147 sprintf(buf
, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
148 uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1], uuid
->Data4
[2],
149 uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6], uuid
->Data4
[7]);
154 const char *get_name(const var_t
*v
)
156 static char buffer
[256];
158 if (is_attr( v
->attrs
, ATTR_PROPGET
))
159 strcpy( buffer
, "get_" );
160 else if (is_attr( v
->attrs
, ATTR_PROPPUT
))
161 strcpy( buffer
, "put_" );
162 else if (is_attr( v
->attrs
, ATTR_PROPPUTREF
))
163 strcpy( buffer
, "putref_" );
166 strcat( buffer
, v
->name
);
170 static void write_field(FILE *h
, var_t
*v
)
175 write_type_def_or_decl(h
, v
->type
, TRUE
, v
->name
);
180 static void write_fields(FILE *h
, var_list_t
*fields
)
184 LIST_FOR_EACH_ENTRY( v
, fields
, var_t
, entry
) write_field(h
, v
);
187 static void write_enums(FILE *h
, var_list_t
*enums
)
191 LIST_FOR_EACH_ENTRY( v
, enums
, var_t
, entry
)
195 fprintf(h
, "%s", get_name(v
));
198 write_expr(h
, v
->eval
, 0, 1, NULL
, NULL
, "");
201 if (list_next( enums
, &v
->entry
)) fprintf(h
, ",\n");
206 int needs_space_after(type_t
*t
)
208 return (type_is_alias(t
) ||
209 (!is_ptr(t
) && (!is_array(t
) || !type_array_is_decl_as_ptr(t
) || t
->name
)));
212 void write_type_left(FILE *h
, type_t
*t
, int declonly
)
216 if (is_attr(t
->attrs
, ATTR_CONST
) &&
217 (type_is_alias(t
) || !is_ptr(t
)))
218 fprintf(h
, "const ");
220 if (type_is_alias(t
)) fprintf(h
, "%s", t
->name
);
222 switch (type_get_type_detect_alias(t
)) {
224 if (!declonly
&& t
->defined
&& !t
->written
) {
225 if (t
->name
) fprintf(h
, "enum %s {\n", t
->name
);
226 else fprintf(h
, "enum {\n");
229 write_enums(h
, type_enum_get_values(t
));
233 else fprintf(h
, "enum %s", t
->name
? t
->name
: "");
236 case TYPE_ENCAPSULATED_UNION
:
237 if (!declonly
&& t
->defined
&& !t
->written
) {
238 if (t
->name
) fprintf(h
, "struct %s {\n", t
->name
);
239 else fprintf(h
, "struct {\n");
242 if (type_get_type(t
) != TYPE_STRUCT
)
243 write_fields(h
, type_encapsulated_union_get_fields(t
));
245 write_fields(h
, type_struct_get_fields(t
));
249 else fprintf(h
, "struct %s", t
->name
? t
->name
: "");
252 if (!declonly
&& t
->defined
&& !t
->written
) {
253 if (t
->name
) fprintf(h
, "union %s {\n", t
->name
);
254 else fprintf(h
, "union {\n");
257 write_fields(h
, type_union_get_cases(t
));
261 else fprintf(h
, "union %s", t
->name
? t
->name
: "");
264 write_type_left(h
, type_pointer_get_ref(t
), declonly
);
265 fprintf(h
, "%s*", needs_space_after(type_pointer_get_ref(t
)) ? " " : "");
266 if (is_attr(t
->attrs
, ATTR_CONST
)) fprintf(h
, "const ");
269 if (t
->name
&& type_array_is_decl_as_ptr(t
))
270 fprintf(h
, "%s", t
->name
);
273 write_type_left(h
, type_array_get_element(t
), declonly
);
274 if (type_array_is_decl_as_ptr(t
))
275 fprintf(h
, "%s*", needs_space_after(type_array_get_element(t
)) ? " " : "");
279 if (type_basic_get_type(t
) != TYPE_BASIC_INT32
&&
280 type_basic_get_type(t
) != TYPE_BASIC_INT64
&&
281 type_basic_get_type(t
) != TYPE_BASIC_HYPER
)
283 if (type_basic_get_sign(t
) < 0) fprintf(h
, "signed ");
284 else if (type_basic_get_sign(t
) > 0) fprintf(h
, "unsigned ");
286 switch (type_basic_get_type(t
))
288 case TYPE_BASIC_INT8
: fprintf(h
, "small"); break;
289 case TYPE_BASIC_INT16
: fprintf(h
, "short"); break;
290 case TYPE_BASIC_INT
: fprintf(h
, "int"); break;
291 case TYPE_BASIC_INT3264
: fprintf(h
, "__int3264"); break;
292 case TYPE_BASIC_BYTE
: fprintf(h
, "byte"); break;
293 case TYPE_BASIC_CHAR
: fprintf(h
, "char"); break;
294 case TYPE_BASIC_WCHAR
: fprintf(h
, "wchar_t"); break;
295 case TYPE_BASIC_FLOAT
: fprintf(h
, "float"); break;
296 case TYPE_BASIC_DOUBLE
: fprintf(h
, "double"); break;
297 case TYPE_BASIC_ERROR_STATUS_T
: fprintf(h
, "error_status_t"); break;
298 case TYPE_BASIC_HANDLE
: fprintf(h
, "handle_t"); break;
299 case TYPE_BASIC_INT32
:
300 if (type_basic_get_sign(t
) > 0)
305 case TYPE_BASIC_INT64
:
306 if (type_basic_get_sign(t
) > 0)
307 fprintf(h
, "UINT64");
311 case TYPE_BASIC_HYPER
:
312 if (type_basic_get_sign(t
) > 0)
313 fprintf(h
, "MIDL_uhyper");
322 fprintf(h
, "%s", t
->name
);
328 write_type_left(h
, type_bitfield_get_field(t
), declonly
);
332 /* handled elsewhere */
339 void write_type_right(FILE *h
, type_t
*t
, int is_field
)
343 switch (type_get_type(t
))
346 if (!type_array_is_decl_as_ptr(t
))
348 if (is_conformant_array(t
))
350 fprintf(h
, "[%s]", is_field
? "1" : "");
351 t
= type_array_get_element(t
);
354 type_get_type(t
) == TYPE_ARRAY
&& !type_array_is_decl_as_ptr(t
);
355 t
= type_array_get_element(t
))
356 fprintf(h
, "[%u]", type_array_get_dim(t
));
360 fprintf(h
, " : %u", type_bitfield_get_bits(t
)->cval
);
366 case TYPE_ENCAPSULATED_UNION
:
378 static void write_type_v(FILE *h
, type_t
*t
, int is_field
, int declonly
, const char *name
)
386 for (pt
= t
; is_ptr(pt
); pt
= type_pointer_get_ref(pt
), ptr_level
++)
389 if (type_get_type_detect_alias(pt
) == TYPE_FUNCTION
) {
391 const char *callconv
= get_attrp(pt
->attrs
, ATTR_CALLCONV
);
392 if (!callconv
&& is_object_interface
) callconv
= "STDMETHODCALLTYPE";
393 if (is_attr(pt
->attrs
, ATTR_INLINE
)) fprintf(h
, "inline ");
394 write_type_left(h
, type_function_get_rettype(pt
), declonly
);
396 if (ptr_level
) fputc('(', h
);
397 if (callconv
) fprintf(h
, "%s ", callconv
);
398 for (i
= 0; i
< ptr_level
; i
++)
401 write_type_left(h
, t
, declonly
);
404 if (name
) fprintf(h
, "%s%s", !t
|| needs_space_after(t
) ? " " : "", name
);
407 if (type_get_type_detect_alias(pt
) == TYPE_FUNCTION
) {
408 const var_list_t
*args
= type_function_get_args(pt
);
410 if (ptr_level
) fputc(')', h
);
413 write_args(h
, args
, NULL
, 0, FALSE
);
418 write_type_right(h
, t
, is_field
);
422 static void write_type_def_or_decl(FILE *f
, type_t
*t
, int field
, const char *name
)
424 write_type_v(f
, t
, field
, FALSE
, name
);
427 void write_type_decl(FILE *f
, type_t
*t
, const char *name
)
429 write_type_v(f
, t
, FALSE
, TRUE
, name
);
432 void write_type_decl_left(FILE *f
, type_t
*t
)
434 write_type_left(f
, t
, TRUE
);
437 static int user_type_registered(const char *name
)
440 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
441 if (!strcmp(name
, ut
->name
))
446 static int context_handle_registered(const char *name
)
448 context_handle_t
*ch
;
449 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
450 if (!strcmp(name
, ch
->name
))
455 static int generic_handle_registered(const char *name
)
457 generic_handle_t
*gh
;
458 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
459 if (!strcmp(name
, gh
->name
))
464 unsigned int get_context_handle_offset( const type_t
*type
)
466 context_handle_t
*ch
;
467 unsigned int index
= 0;
469 while (!is_attr( type
->attrs
, ATTR_CONTEXTHANDLE
))
471 if (type_is_alias( type
)) type
= type_alias_get_aliasee( type
);
472 else if (is_ptr( type
)) type
= type_pointer_get_ref( type
);
473 else error( "internal error: %s is not a context handle\n", type
->name
);
475 LIST_FOR_EACH_ENTRY( ch
, &context_handle_list
, context_handle_t
, entry
)
477 if (!strcmp( type
->name
, ch
->name
)) return index
;
480 error( "internal error: %s is not registered as a context handle\n", type
->name
);
484 unsigned int get_generic_handle_offset( const type_t
*type
)
486 generic_handle_t
*gh
;
487 unsigned int index
= 0;
489 while (!is_attr( type
->attrs
, ATTR_HANDLE
))
491 if (type_is_alias( type
)) type
= type_alias_get_aliasee( type
);
492 else if (is_ptr( type
)) type
= type_pointer_get_ref( type
);
493 else error( "internal error: %s is not a generic handle\n", type
->name
);
495 LIST_FOR_EACH_ENTRY( gh
, &generic_handle_list
, generic_handle_t
, entry
)
497 if (!strcmp( type
->name
, gh
->name
)) return index
;
500 error( "internal error: %s is not registered as a generic handle\n", type
->name
);
504 /* check for types which require additional prototypes to be generated in the
506 void check_for_additional_prototype_types(const var_list_t
*list
)
511 LIST_FOR_EACH_ENTRY( v
, list
, const var_t
, entry
)
513 type_t
*type
= v
->type
;
516 const char *name
= type
->name
;
517 if (type
->user_types_registered
) break;
518 type
->user_types_registered
= 1;
519 if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
)) {
520 if (!context_handle_registered(name
))
522 context_handle_t
*ch
= xmalloc(sizeof(*ch
));
523 ch
->name
= xstrdup(name
);
524 list_add_tail(&context_handle_list
, &ch
->entry
);
526 /* don't carry on parsing fields within this type */
529 if ((type_get_type(type
) != TYPE_BASIC
||
530 type_basic_get_type(type
) != TYPE_BASIC_HANDLE
) &&
531 is_attr(type
->attrs
, ATTR_HANDLE
)) {
532 if (!generic_handle_registered(name
))
534 generic_handle_t
*gh
= xmalloc(sizeof(*gh
));
535 gh
->name
= xstrdup(name
);
536 list_add_tail(&generic_handle_list
, &gh
->entry
);
538 /* don't carry on parsing fields within this type */
541 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
)) {
542 if (!user_type_registered(name
))
544 user_type_t
*ut
= xmalloc(sizeof *ut
);
545 ut
->name
= xstrdup(name
);
546 list_add_tail(&user_type_list
, &ut
->entry
);
548 /* don't carry on parsing fields within this type as we are already
549 * using a wire marshaled type */
552 else if (type_is_complete(type
))
555 switch (type_get_type_detect_alias(type
))
558 vars
= type_enum_get_values(type
);
561 vars
= type_struct_get_fields(type
);
564 vars
= type_union_get_cases(type
);
570 check_for_additional_prototype_types(vars
);
573 if (type_is_alias(type
))
574 type
= type_alias_get_aliasee(type
);
575 else if (is_ptr(type
))
576 type
= type_pointer_get_ref(type
);
577 else if (is_array(type
))
578 type
= type_array_get_element(type
);
585 static void write_user_types(FILE *header
)
588 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
590 const char *name
= ut
->name
;
591 fprintf(header
, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name
, name
);
592 fprintf(header
, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name
, name
);
593 fprintf(header
, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name
, name
);
594 fprintf(header
, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name
, name
);
598 static void write_context_handle_rundowns(FILE *header
)
600 context_handle_t
*ch
;
601 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
603 const char *name
= ch
->name
;
604 fprintf(header
, "void __RPC_USER %s_rundown(%s);\n", name
, name
);
608 static void write_generic_handle_routines(FILE *header
)
610 generic_handle_t
*gh
;
611 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
613 const char *name
= gh
->name
;
614 fprintf(header
, "handle_t __RPC_USER %s_bind(%s);\n", name
, name
);
615 fprintf(header
, "void __RPC_USER %s_unbind(%s, handle_t);\n", name
, name
);
619 static void write_typedef(FILE *header
, type_t
*type
)
621 fprintf(header
, "typedef ");
622 write_type_def_or_decl(header
, type_alias_get_aliasee(type
), FALSE
, type
->name
);
623 fprintf(header
, ";\n");
626 int is_const_decl(const var_t
*var
)
629 /* strangely, MIDL accepts a const attribute on any pointer in the
630 * declaration to mean that data isn't being instantiated. this appears
631 * to be a bug, but there is no benefit to being incompatible with MIDL,
632 * so we'll do the same thing */
633 for (t
= var
->type
; ; )
635 if (is_attr(t
->attrs
, ATTR_CONST
))
638 t
= type_pointer_get_ref(t
);
644 static void write_declaration(FILE *header
, const var_t
*v
)
646 if (is_const_decl(v
) && v
->eval
)
648 fprintf(header
, "#define %s (", v
->name
);
649 write_expr(header
, v
->eval
, 0, 1, NULL
, NULL
, "");
650 fprintf(header
, ")\n\n");
657 case STG_REGISTER
: /* ignored */
660 fprintf(header
, "static ");
663 fprintf(header
, "extern ");
666 write_type_def_or_decl(header
, v
->type
, FALSE
, v
->name
);
667 fprintf(header
, ";\n\n");
671 static void write_library(FILE *header
, const typelib_t
*typelib
)
673 const UUID
*uuid
= get_attrp(typelib
->attrs
, ATTR_UUID
);
674 fprintf(header
, "\n");
675 write_guid(header
, "LIBID", typelib
->name
, uuid
);
676 fprintf(header
, "\n");
680 const type_t
* get_explicit_generic_handle_type(const var_t
* var
)
684 is_ptr(t
) || type_is_alias(t
);
685 t
= type_is_alias(t
) ? type_alias_get_aliasee(t
) : type_pointer_get_ref(t
))
686 if ((type_get_type_detect_alias(t
) != TYPE_BASIC
|| type_basic_get_type(t
) != TYPE_BASIC_HANDLE
) &&
687 is_attr(t
->attrs
, ATTR_HANDLE
))
692 const var_t
*get_func_handle_var( const type_t
*iface
, const var_t
*func
,
693 unsigned char *explicit_fc
, unsigned char *implicit_fc
)
696 const var_list_t
*args
= type_get_function_args( func
->type
);
698 *explicit_fc
= *implicit_fc
= 0;
699 if (args
) LIST_FOR_EACH_ENTRY( var
, args
, const var_t
, entry
)
701 if (!is_attr( var
->attrs
, ATTR_IN
) && is_attr( var
->attrs
, ATTR_OUT
)) continue;
702 if (type_get_type( var
->type
) == TYPE_BASIC
&& type_basic_get_type( var
->type
) == TYPE_BASIC_HANDLE
)
704 *explicit_fc
= RPC_FC_BIND_PRIMITIVE
;
707 if (get_explicit_generic_handle_type( var
))
709 *explicit_fc
= RPC_FC_BIND_GENERIC
;
712 if (is_context_handle( var
->type
))
714 *explicit_fc
= RPC_FC_BIND_CONTEXT
;
719 if ((var
= get_attrp( iface
->attrs
, ATTR_IMPLICIT_HANDLE
)))
721 if (type_get_type( var
->type
) == TYPE_BASIC
&&
722 type_basic_get_type( var
->type
) == TYPE_BASIC_HANDLE
)
723 *implicit_fc
= RPC_FC_BIND_PRIMITIVE
;
725 *implicit_fc
= RPC_FC_BIND_GENERIC
;
729 *implicit_fc
= RPC_FC_AUTO_HANDLE
;
733 int has_out_arg_or_return(const var_t
*func
)
737 if (!is_void(type_function_get_rettype(func
->type
)))
740 if (!type_get_function_args(func
->type
))
743 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
744 if (is_attr(var
->attrs
, ATTR_OUT
))
751 /********** INTERFACES **********/
753 int is_object(const type_t
*iface
)
756 if (type_is_defined(iface
) && type_iface_get_inherit(iface
))
758 if (iface
->attrs
) LIST_FOR_EACH_ENTRY( attr
, iface
->attrs
, const attr_t
, entry
)
759 if (attr
->type
== ATTR_OBJECT
|| attr
->type
== ATTR_ODL
) return 1;
763 int is_local(const attr_list_t
*a
)
765 return is_attr(a
, ATTR_LOCAL
);
768 const var_t
*is_callas(const attr_list_t
*a
)
770 return get_attrp(a
, ATTR_CALLAS
);
773 static void write_method_macro(FILE *header
, const type_t
*iface
, const char *name
)
775 const statement_t
*stmt
;
778 if (type_iface_get_inherit(iface
))
779 write_method_macro(header
, type_iface_get_inherit(iface
), name
);
781 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
783 const var_t
*func
= stmt
->u
.var
;
787 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
791 if (!is_callas(func
->attrs
)) {
794 fprintf(header
, "#define %s_%s(This", name
, get_name(func
));
795 if (type_get_function_args(func
->type
))
796 LIST_FOR_EACH_ENTRY( arg
, type_get_function_args(func
->type
), const var_t
, entry
)
797 fprintf(header
, ",%s", arg
->name
);
798 fprintf(header
, ") ");
800 fprintf(header
, "(This)->lpVtbl->%s(This", get_name(func
));
801 if (type_get_function_args(func
->type
))
802 LIST_FOR_EACH_ENTRY( arg
, type_get_function_args(func
->type
), const var_t
, entry
)
803 fprintf(header
, ",%s", arg
->name
);
804 fprintf(header
, ")\n");
809 void write_args(FILE *h
, const var_list_t
*args
, const char *name
, int method
, int do_indent
)
820 fprintf(h
, "%s* This", name
);
823 if (args
) LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
) {
830 else fprintf(h
, ",");
832 write_type_decl(h
, arg
->type
, arg
->name
);
835 if (do_indent
) indentation
--;
838 static void write_cpp_method_def(FILE *header
, const type_t
*iface
)
840 const statement_t
*stmt
;
842 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
844 const var_t
*func
= stmt
->u
.var
;
845 if (!is_callas(func
->attrs
)) {
846 const char *callconv
= get_attrp(func
->type
->attrs
, ATTR_CALLCONV
);
847 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
849 fprintf(header
, "virtual ");
850 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
851 fprintf(header
, " %s %s(\n", callconv
, get_name(func
));
852 write_args(header
, type_get_function_args(func
->type
), iface
->name
, 2, TRUE
);
853 fprintf(header
, ") = 0;\n");
854 fprintf(header
, "\n");
859 static void do_write_c_method_def(FILE *header
, const type_t
*iface
, const char *name
)
861 const statement_t
*stmt
;
864 if (type_iface_get_inherit(iface
))
865 do_write_c_method_def(header
, type_iface_get_inherit(iface
), name
);
867 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
869 const var_t
*func
= stmt
->u
.var
;
872 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
875 if (!is_callas(func
->attrs
)) {
876 const char *callconv
= get_attrp(func
->type
->attrs
, ATTR_CALLCONV
);
877 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
879 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
880 fprintf(header
, " (%s *%s)(\n", callconv
, get_name(func
));
881 write_args(header
, type_get_function_args(func
->type
), name
, 1, TRUE
);
882 fprintf(header
, ");\n");
883 fprintf(header
, "\n");
888 static void write_c_method_def(FILE *header
, const type_t
*iface
)
890 do_write_c_method_def(header
, iface
, iface
->name
);
893 static void write_c_disp_method_def(FILE *header
, const type_t
*iface
)
895 do_write_c_method_def(header
, type_iface_get_inherit(iface
), iface
->name
);
898 static void write_method_proto(FILE *header
, const type_t
*iface
)
900 const statement_t
*stmt
;
902 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
904 const var_t
*func
= stmt
->u
.var
;
906 if (!is_local(func
->attrs
)) {
907 const char *callconv
= get_attrp(func
->type
->attrs
, ATTR_CALLCONV
);
908 if (!callconv
) callconv
= "STDMETHODCALLTYPE";
909 /* proxy prototype */
910 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
911 fprintf(header
, " %s %s_%s_Proxy(\n", callconv
, iface
->name
, get_name(func
));
912 write_args(header
, type_get_function_args(func
->type
), iface
->name
, 1, TRUE
);
913 fprintf(header
, ");\n");
915 fprintf(header
, "void __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(func
));
916 fprintf(header
, " IRpcStubBuffer* This,\n");
917 fprintf(header
, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
918 fprintf(header
, " PRPC_MESSAGE pRpcMessage,\n");
919 fprintf(header
, " DWORD* pdwStubPhase);\n");
924 static void write_locals(FILE *fp
, const type_t
*iface
, int body
)
926 static const char comment
[]
927 = "/* WIDL-generated stub. You must provide an implementation for this. */";
928 const statement_t
*stmt
;
930 if (!is_object(iface
))
933 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
)) {
934 const var_t
*func
= stmt
->u
.var
;
935 const var_t
*cas
= is_callas(func
->attrs
);
938 const statement_t
*stmt2
= NULL
;
939 STATEMENTS_FOR_EACH_FUNC(stmt2
, type_iface_get_stmts(iface
))
940 if (!strcmp(stmt2
->u
.var
->name
, cas
->name
))
942 if (&stmt2
->entry
!= type_iface_get_stmts(iface
)) {
943 const var_t
*m
= stmt2
->u
.var
;
944 /* proxy prototype - use local prototype */
945 write_type_decl_left(fp
, type_function_get_rettype(m
->type
));
946 fprintf(fp
, " CALLBACK %s_%s_Proxy(\n", iface
->name
, get_name(m
));
947 write_args(fp
, type_get_function_args(m
->type
), iface
->name
, 1, TRUE
);
950 type_t
*rt
= type_function_get_rettype(m
->type
);
951 fprintf(fp
, "\n{\n");
952 fprintf(fp
, " %s\n", comment
);
953 if (rt
->name
&& strcmp(rt
->name
, "HRESULT") == 0)
954 fprintf(fp
, " return E_NOTIMPL;\n");
955 else if (type_get_type(rt
) != TYPE_VOID
) {
957 write_type_decl(fp
, rt
, "rv");
959 fprintf(fp
, " memset(&rv, 0, sizeof rv);\n");
960 fprintf(fp
, " return rv;\n");
962 fprintf(fp
, "}\n\n");
966 /* stub prototype - use remotable prototype */
967 write_type_decl_left(fp
, type_function_get_rettype(func
->type
));
968 fprintf(fp
, " __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(m
));
969 write_args(fp
, type_get_function_args(func
->type
), iface
->name
, 1, TRUE
);
972 /* Remotable methods must all return HRESULTs. */
973 fprintf(fp
, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment
);
978 error_loc("invalid call_as attribute (%s -> %s)\n", func
->name
, cas
->name
);
983 static void write_local_stubs_stmts(FILE *local_stubs
, const statement_list_t
*stmts
)
985 const statement_t
*stmt
;
986 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
988 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
989 write_locals(local_stubs
, stmt
->u
.type
, TRUE
);
993 void write_local_stubs(const statement_list_t
*stmts
)
997 if (!local_stubs_name
) return;
999 local_stubs
= fopen(local_stubs_name
, "w");
1001 error("Could not open %s for output\n", local_stubs_name
);
1004 fprintf(local_stubs
, "/* call_as/local stubs for %s */\n\n", input_name
);
1005 fprintf(local_stubs
, "#include <objbase.h>\n");
1006 fprintf(local_stubs
, "#include \"%s\"\n\n", header_name
);
1008 write_local_stubs_stmts(local_stubs
, stmts
);
1010 fclose(local_stubs
);
1013 static void write_function_proto(FILE *header
, const type_t
*iface
, const var_t
*fun
, const char *prefix
)
1015 const char *callconv
= get_attrp(fun
->type
->attrs
, ATTR_CALLCONV
);
1017 if (!callconv
) callconv
= "__cdecl";
1018 /* FIXME: do we need to handle call_as? */
1019 write_type_decl_left(header
, type_function_get_rettype(fun
->type
));
1020 fprintf(header
, " %s ", callconv
);
1021 fprintf(header
, "%s%s(\n", prefix
, get_name(fun
));
1022 if (type_get_function_args(fun
->type
))
1023 write_args(header
, type_get_function_args(fun
->type
), iface
->name
, 0, TRUE
);
1025 fprintf(header
, " void");
1026 fprintf(header
, ");\n\n");
1029 static void write_forward(FILE *header
, type_t
*iface
)
1031 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", iface
->name
);
1032 fprintf(header
, "#define __%s_FWD_DEFINED__\n", iface
->name
);
1033 fprintf(header
, "typedef interface %s %s;\n", iface
->name
, iface
->name
);
1034 fprintf(header
, "#endif\n\n" );
1037 static void write_com_interface_start(FILE *header
, const type_t
*iface
)
1039 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
1040 fprintf(header
, "/*****************************************************************************\n");
1041 fprintf(header
, " * %s %sinterface\n", iface
->name
, dispinterface
? "disp" : "");
1042 fprintf(header
, " */\n");
1043 fprintf(header
,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface
->name
, dispinterface
? "DISP" : "");
1044 fprintf(header
,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface
->name
, dispinterface
? "DISP" : "");
1047 static void write_com_interface_end(FILE *header
, type_t
*iface
)
1049 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
1050 const UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
1053 write_guid(header
, dispinterface
? "DIID" : "IID", iface
->name
, uuid
);
1056 fprintf(header
, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1058 fprintf(header
, "MIDL_INTERFACE(\"%s\")\n", uuid_string(uuid
));
1060 fprintf(header
, "interface ");
1061 if (type_iface_get_inherit(iface
))
1063 fprintf(header
, "%s : public %s\n", iface
->name
,
1064 type_iface_get_inherit(iface
)->name
);
1065 fprintf(header
, "{\n");
1069 fprintf(header
, "%s\n", iface
->name
);
1070 fprintf(header
, "{\n");
1071 fprintf(header
, " BEGIN_INTERFACE\n");
1072 fprintf(header
, "\n");
1074 /* dispinterfaces don't have real functions, so don't write C++ functions for
1079 write_cpp_method_def(header
, iface
);
1082 if (!type_iface_get_inherit(iface
))
1083 fprintf(header
, " END_INTERFACE\n");
1084 fprintf(header
, "};\n");
1086 write_uuid_decl(header
, iface
->name
, uuid
);
1087 fprintf(header
, "#else\n");
1089 fprintf(header
, "typedef struct %sVtbl {\n", iface
->name
);
1091 fprintf(header
, " BEGIN_INTERFACE\n");
1092 fprintf(header
, "\n");
1094 write_c_disp_method_def(header
, iface
);
1096 write_c_method_def(header
, iface
);
1098 fprintf(header
, " END_INTERFACE\n");
1099 fprintf(header
, "} %sVtbl;\n", iface
->name
);
1100 fprintf(header
, "interface %s {\n", iface
->name
);
1101 fprintf(header
, " CONST_VTBL %sVtbl* lpVtbl;\n", iface
->name
);
1102 fprintf(header
, "};\n");
1103 fprintf(header
, "\n");
1104 fprintf(header
, "#ifdef COBJMACROS\n");
1105 /* dispinterfaces don't have real functions, so don't write macros for them,
1106 * only for the interface this interface inherits from, i.e. IDispatch */
1107 write_method_macro(header
, dispinterface
? type_iface_get_inherit(iface
) : iface
, iface
->name
);
1108 fprintf(header
, "#endif\n");
1109 fprintf(header
, "\n");
1110 fprintf(header
, "#endif\n");
1111 fprintf(header
, "\n");
1112 /* dispinterfaces don't have real functions, so don't write prototypes for
1116 write_method_proto(header
, iface
);
1117 write_locals(header
, iface
, FALSE
);
1118 fprintf(header
, "\n");
1120 fprintf(header
,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface
->name
, dispinterface
? "DISP" : "");
1123 static void write_rpc_interface_start(FILE *header
, const type_t
*iface
)
1125 unsigned int ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
1126 const var_t
*var
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
1128 fprintf(header
, "/*****************************************************************************\n");
1129 fprintf(header
, " * %s interface (v%d.%d)\n", iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1130 fprintf(header
, " */\n");
1131 fprintf(header
,"#ifndef __%s_INTERFACE_DEFINED__\n", iface
->name
);
1132 fprintf(header
,"#define __%s_INTERFACE_DEFINED__\n\n", iface
->name
);
1135 fprintf(header
, "extern ");
1136 write_type_decl( header
, var
->type
, var
->name
);
1137 fprintf(header
, ";\n");
1141 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client
, iface
->name
);
1142 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server
, iface
->name
);
1146 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1147 prefix_client
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1148 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1149 prefix_server
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1153 static void write_rpc_interface_end(FILE *header
, const type_t
*iface
)
1155 fprintf(header
,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface
->name
);
1158 static void write_coclass(FILE *header
, type_t
*cocl
)
1160 const UUID
*uuid
= get_attrp(cocl
->attrs
, ATTR_UUID
);
1162 fprintf(header
, "/*****************************************************************************\n");
1163 fprintf(header
, " * %s coclass\n", cocl
->name
);
1164 fprintf(header
, " */\n\n");
1166 write_guid(header
, "CLSID", cocl
->name
, uuid
);
1167 fprintf(header
, "\n#ifdef __cplusplus\n");
1170 fprintf(header
, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid
), cocl
->name
);
1171 write_uuid_decl(header
, cocl
->name
, uuid
);
1175 fprintf(header
, "class %s;\n", cocl
->name
);
1177 fprintf(header
, "#endif\n");
1178 fprintf(header
, "\n");
1181 static void write_coclass_forward(FILE *header
, type_t
*cocl
)
1183 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", cocl
->name
);
1184 fprintf(header
, "#define __%s_FWD_DEFINED__\n", cocl
->name
);
1185 fprintf(header
, "typedef struct %s %s;\n", cocl
->name
, cocl
->name
);
1186 fprintf(header
, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl
->name
);
1189 static void write_import(FILE *header
, const char *fname
)
1193 hname
= dup_basename(fname
, ".idl");
1194 p
= hname
+ strlen(hname
) - 2;
1195 if (p
<= hname
|| strcmp( p
, ".h" )) strcat(hname
, ".h");
1197 fprintf(header
, "#include <%s>\n", hname
);
1201 static void write_imports(FILE *header
, const statement_list_t
*stmts
)
1203 const statement_t
*stmt
;
1204 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1209 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1210 write_imports(header
, type_iface_get_stmts(stmt
->u
.type
));
1213 case STMT_IMPORTLIB
:
1214 /* not included in header */
1217 write_import(header
, stmt
->u
.str
);
1222 case STMT_DECLARATION
:
1223 /* not processed here */
1226 write_imports(header
, stmt
->u
.lib
->stmts
);
1232 static void write_forward_decls(FILE *header
, const statement_list_t
*stmts
)
1234 const statement_t
*stmt
;
1235 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1240 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1242 if (is_object(stmt
->u
.type
) || is_attr(stmt
->u
.type
->attrs
, ATTR_DISPINTERFACE
))
1243 write_forward(header
, stmt
->u
.type
);
1245 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
1246 write_coclass_forward(header
, stmt
->u
.type
);
1249 case STMT_IMPORTLIB
:
1250 /* not included in header */
1256 case STMT_DECLARATION
:
1257 /* not processed here */
1260 write_forward_decls(header
, stmt
->u
.lib
->stmts
);
1266 static void write_header_stmts(FILE *header
, const statement_list_t
*stmts
, const type_t
*iface
, int ignore_funcs
)
1268 const statement_t
*stmt
;
1269 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1274 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1276 type_t
*iface
= stmt
->u
.type
;
1277 if (is_object(iface
)) is_object_interface
++;
1278 if (is_attr(stmt
->u
.type
->attrs
, ATTR_DISPINTERFACE
) || is_object(stmt
->u
.type
))
1280 write_com_interface_start(header
, iface
);
1281 write_header_stmts(header
, type_iface_get_stmts(iface
), stmt
->u
.type
, TRUE
);
1282 write_com_interface_end(header
, iface
);
1286 write_rpc_interface_start(header
, iface
);
1287 write_header_stmts(header
, type_iface_get_stmts(iface
), iface
, FALSE
);
1288 write_rpc_interface_end(header
, iface
);
1290 if (is_object(iface
)) is_object_interface
--;
1292 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
1293 write_coclass(header
, stmt
->u
.type
);
1296 write_type_def_or_decl(header
, stmt
->u
.type
, FALSE
, NULL
);
1297 fprintf(header
, ";\n\n");
1301 /* FIXME: shouldn't write out forward declarations for undefined
1302 * interfaces but a number of our IDL files depend on this */
1303 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
&& !stmt
->u
.type
->written
)
1304 write_forward(header
, stmt
->u
.type
);
1306 case STMT_IMPORTLIB
:
1308 /* not included in header */
1311 /* not processed here */
1315 const type_list_t
*type_entry
= stmt
->u
.type_list
;
1316 for (; type_entry
; type_entry
= type_entry
->next
)
1317 write_typedef(header
, type_entry
->type
);
1321 write_library(header
, stmt
->u
.lib
);
1322 write_header_stmts(header
, stmt
->u
.lib
->stmts
, NULL
, FALSE
);
1325 fprintf(header
, "%s\n", stmt
->u
.str
);
1327 case STMT_DECLARATION
:
1328 if (iface
&& type_get_type(stmt
->u
.var
->type
) == TYPE_FUNCTION
)
1332 int prefixes_differ
= strcmp(prefix_client
, prefix_server
);
1334 if (prefixes_differ
)
1336 fprintf(header
, "/* client prototype */\n");
1337 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_client
);
1338 fprintf(header
, "/* server prototype */\n");
1340 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_server
);
1344 write_declaration(header
, stmt
->u
.var
);
1350 void write_header(const statement_list_t
*stmts
)
1354 if (!do_header
) return;
1356 if(!(header
= fopen(header_name
, "w"))) {
1357 error("Could not open %s for output\n", header_name
);
1360 fprintf(header
, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION
, input_name
);
1362 fprintf(header
, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
1363 fprintf(header
, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
1364 fprintf(header
, "#endif\n\n");
1366 fprintf(header
, "#include <rpc.h>\n" );
1367 fprintf(header
, "#include <rpcndr.h>\n\n" );
1369 fprintf(header
, "#ifndef COM_NO_WINDOWS_H\n");
1370 fprintf(header
, "#include <windows.h>\n");
1371 fprintf(header
, "#include <ole2.h>\n");
1372 fprintf(header
, "#endif\n\n");
1374 fprintf(header
, "#ifndef __%s__\n", header_token
);
1375 fprintf(header
, "#define __%s__\n\n", header_token
);
1377 fprintf(header
, "/* Forward declarations */\n\n");
1378 write_forward_decls(header
, stmts
);
1380 fprintf(header
, "/* Headers for imported files */\n\n");
1381 write_imports(header
, stmts
);
1382 fprintf(header
, "\n");
1383 start_cplusplus_guard(header
);
1385 write_header_stmts(header
, stmts
, NULL
, FALSE
);
1387 fprintf(header
, "/* Begin additional prototypes for all interfaces */\n");
1388 fprintf(header
, "\n");
1389 write_user_types(header
);
1390 write_generic_handle_routines(header
);
1391 write_context_handle_rundowns(header
);
1392 fprintf(header
, "\n");
1393 fprintf(header
, "/* End additional prototypes */\n");
1394 fprintf(header
, "\n");
1396 end_cplusplus_guard(header
);
1397 fprintf(header
, "#endif /* __%s__ */\n", header_token
);