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 typedef struct _user_type_t generic_handle_t
;
41 static int indentation
= 0;
42 user_type_list_t user_type_list
= LIST_INIT(user_type_list
);
43 static context_handle_list_t context_handle_list
= LIST_INIT(context_handle_list
);
44 static struct list generic_handle_list
= LIST_INIT(generic_handle_list
);
46 static void indent(FILE *h
, int delta
)
49 if (delta
< 0) indentation
+= delta
;
50 for (c
=0; c
<indentation
; c
++) fprintf(h
, " ");
51 if (delta
> 0) indentation
+= delta
;
54 int is_ptrchain_attr(const var_t
*var
, enum attr_type t
)
56 if (is_attr(var
->attrs
, t
))
60 type_t
*type
= var
->type
;
63 if (is_attr(type
->attrs
, t
))
65 else if (type_is_alias(type
))
66 type
= type_alias_get_aliasee(type
);
67 else if (is_ptr(type
))
68 type
= type_pointer_get_ref(type
);
74 int is_aliaschain_attr(const type_t
*type
, enum attr_type attr
)
76 const type_t
*t
= type
;
79 if (is_attr(t
->attrs
, attr
))
81 else if (type_is_alias(t
))
82 t
= type_alias_get_aliasee(t
);
87 int is_attr(const attr_list_t
*list
, enum attr_type t
)
90 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
91 if (attr
->type
== t
) return 1;
95 void *get_attrp(const attr_list_t
*list
, enum attr_type t
)
98 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
99 if (attr
->type
== t
) return attr
->u
.pval
;
103 unsigned long get_attrv(const attr_list_t
*list
, enum attr_type t
)
106 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
107 if (attr
->type
== t
) return attr
->u
.ival
;
111 int is_void(const type_t
*t
)
113 return type_get_type(t
) == TYPE_VOID
;
116 int is_conformant_array(const type_t
*t
)
118 return is_array(t
) && type_array_has_conformance(t
);
121 void write_guid(FILE *f
, const char *guid_prefix
, const char *name
, const UUID
*uuid
)
124 fprintf(f
, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
125 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
126 guid_prefix
, name
, uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0],
127 uuid
->Data4
[1], uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5],
128 uuid
->Data4
[6], uuid
->Data4
[7]);
131 const char *get_name(const var_t
*v
)
133 static char buffer
[256];
135 if (is_attr( v
->attrs
, ATTR_PROPGET
))
136 strcpy( buffer
, "get_" );
137 else if (is_attr( v
->attrs
, ATTR_PROPPUT
))
138 strcpy( buffer
, "put_" );
139 else if (is_attr( v
->attrs
, ATTR_PROPPUTREF
))
140 strcpy( buffer
, "putref_" );
143 strcat( buffer
, v
->name
);
147 static void write_field(FILE *h
, var_t
*v
)
152 write_type_def_or_decl(h
, v
->type
, TRUE
, v
->name
);
157 static void write_fields(FILE *h
, var_list_t
*fields
)
161 LIST_FOR_EACH_ENTRY( v
, fields
, var_t
, entry
) write_field(h
, v
);
164 static void write_enums(FILE *h
, var_list_t
*enums
)
168 LIST_FOR_EACH_ENTRY( v
, enums
, var_t
, entry
)
172 fprintf(h
, "%s", get_name(v
));
175 write_expr(h
, v
->eval
, 0, 1, NULL
, NULL
, "");
178 if (list_next( enums
, &v
->entry
)) fprintf(h
, ",\n");
183 int needs_space_after(type_t
*t
)
185 return (type_is_alias(t
) ||
186 (!is_ptr(t
) && (!is_array(t
) || !type_array_is_decl_as_ptr(t
) || t
->name
)));
189 void write_type_left(FILE *h
, type_t
*t
, int declonly
)
193 if (is_attr(t
->attrs
, ATTR_CONST
) &&
194 (type_is_alias(t
) || !is_ptr(t
)))
195 fprintf(h
, "const ");
197 if (type_is_alias(t
)) fprintf(h
, "%s", t
->name
);
199 switch (type_get_type_detect_alias(t
)) {
201 if (!declonly
&& t
->defined
&& !t
->written
) {
202 if (t
->name
) fprintf(h
, "enum %s {\n", t
->name
);
203 else fprintf(h
, "enum {\n");
206 write_enums(h
, type_enum_get_values(t
));
210 else fprintf(h
, "enum %s", t
->name
? t
->name
: "");
213 case TYPE_ENCAPSULATED_UNION
:
214 if (!declonly
&& t
->defined
&& !t
->written
) {
215 if (t
->name
) fprintf(h
, "struct %s {\n", t
->name
);
216 else fprintf(h
, "struct {\n");
219 if (type_get_type(t
) != TYPE_STRUCT
)
220 write_fields(h
, type_encapsulated_union_get_fields(t
));
222 write_fields(h
, type_struct_get_fields(t
));
226 else fprintf(h
, "struct %s", t
->name
? t
->name
: "");
229 if (!declonly
&& t
->defined
&& !t
->written
) {
230 if (t
->name
) fprintf(h
, "union %s {\n", t
->name
);
231 else fprintf(h
, "union {\n");
234 write_fields(h
, type_union_get_cases(t
));
238 else fprintf(h
, "union %s", t
->name
? t
->name
: "");
241 write_type_left(h
, type_pointer_get_ref(t
), declonly
);
242 fprintf(h
, "%s*", needs_space_after(type_pointer_get_ref(t
)) ? " " : "");
243 if (is_attr(t
->attrs
, ATTR_CONST
)) fprintf(h
, "const ");
246 if (t
->name
&& type_array_is_decl_as_ptr(t
))
247 fprintf(h
, "%s", t
->name
);
250 write_type_left(h
, type_array_get_element(t
), declonly
);
251 if (type_array_is_decl_as_ptr(t
))
252 fprintf(h
, "%s*", needs_space_after(type_array_get_element(t
)) ? " " : "");
256 if (type_basic_get_type(t
) != TYPE_BASIC_INT32
&&
257 type_basic_get_type(t
) != TYPE_BASIC_HYPER
)
259 if (type_basic_get_sign(t
) < 0) fprintf(h
, "signed ");
260 else if (type_basic_get_sign(t
) > 0) fprintf(h
, "unsigned ");
262 switch (type_basic_get_type(t
))
264 case TYPE_BASIC_INT8
: fprintf(h
, "small"); break;
265 case TYPE_BASIC_INT16
: fprintf(h
, "short"); break;
266 case TYPE_BASIC_INT
: fprintf(h
, "int"); break;
267 case TYPE_BASIC_INT64
: fprintf(h
, "__int64"); break;
268 case TYPE_BASIC_BYTE
: fprintf(h
, "byte"); break;
269 case TYPE_BASIC_CHAR
: fprintf(h
, "char"); break;
270 case TYPE_BASIC_WCHAR
: fprintf(h
, "wchar_t"); break;
271 case TYPE_BASIC_FLOAT
: fprintf(h
, "float"); break;
272 case TYPE_BASIC_DOUBLE
: fprintf(h
, "double"); break;
273 case TYPE_BASIC_ERROR_STATUS_T
: fprintf(h
, "error_status_t"); break;
274 case TYPE_BASIC_HANDLE
: fprintf(h
, "handle_t"); break;
275 case TYPE_BASIC_INT32
:
276 if (type_basic_get_sign(t
) > 0)
281 case TYPE_BASIC_HYPER
:
282 if (type_basic_get_sign(t
) > 0)
283 fprintf(h
, "MIDL_uhyper");
292 fprintf(h
, "%s", t
->name
);
299 /* handled elsewhere */
306 void write_type_right(FILE *h
, type_t
*t
, int is_field
)
310 if (type_get_type(t
) == TYPE_ARRAY
&& !type_array_is_decl_as_ptr(t
)) {
311 if (is_conformant_array(t
)) {
312 fprintf(h
, "[%s]", is_field
? "1" : "");
313 t
= type_array_get_element(t
);
316 type_get_type(t
) == TYPE_ARRAY
&& !type_array_is_decl_as_ptr(t
);
317 t
= type_array_get_element(t
))
318 fprintf(h
, "[%u]", type_array_get_dim(t
));
322 static void write_type_v(FILE *h
, type_t
*t
, int is_field
, int declonly
, const char *name
)
329 for (pt
= t
; is_ptr(pt
); pt
= type_pointer_get_ref(pt
), ptr_level
++)
332 if (type_get_type_detect_alias(pt
) == TYPE_FUNCTION
) {
334 const char *callconv
= get_attrp(pt
->attrs
, ATTR_CALLCONV
);
335 if (!callconv
) callconv
= "";
336 if (is_attr(pt
->attrs
, ATTR_INLINE
)) fprintf(h
, "inline ");
337 write_type_left(h
, type_function_get_rettype(pt
), declonly
);
339 if (ptr_level
) fputc('(', h
);
340 fprintf(h
, "%s ", callconv
);
341 for (i
= 0; i
< ptr_level
; i
++)
344 write_type_left(h
, t
, declonly
);
346 if (name
) fprintf(h
, "%s%s", needs_space_after(t
) ? " " : "", name
);
348 if (type_get_type_detect_alias(pt
) == TYPE_FUNCTION
) {
349 if (ptr_level
) fputc(')', h
);
351 write_args(h
, type_function_get_args(pt
), NULL
, 0, FALSE
);
354 write_type_right(h
, t
, is_field
);
357 void write_type_def_or_decl(FILE *f
, type_t
*t
, int field
, const char *name
)
359 write_type_v(f
, t
, field
, FALSE
, name
);
362 void write_type_decl(FILE *f
, type_t
*t
, const char *name
)
364 write_type_v(f
, t
, FALSE
, TRUE
, name
);
367 void write_type_decl_left(FILE *f
, type_t
*t
)
369 write_type_left(f
, t
, TRUE
);
372 static int user_type_registered(const char *name
)
375 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
376 if (!strcmp(name
, ut
->name
))
381 static int context_handle_registered(const char *name
)
383 context_handle_t
*ch
;
384 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
385 if (!strcmp(name
, ch
->name
))
390 static int generic_handle_registered(const char *name
)
392 generic_handle_t
*gh
;
393 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
394 if (!strcmp(name
, gh
->name
))
399 /* check for types which require additional prototypes to be generated in the
401 void check_for_additional_prototype_types(const var_list_t
*list
)
406 LIST_FOR_EACH_ENTRY( v
, list
, const var_t
, entry
)
408 type_t
*type
= v
->type
;
411 const char *name
= type
->name
;
412 if (type
->user_types_registered
) break;
413 type
->user_types_registered
= 1;
414 if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
)) {
415 if (!context_handle_registered(name
))
417 context_handle_t
*ch
= xmalloc(sizeof(*ch
));
418 ch
->name
= xstrdup(name
);
419 list_add_tail(&context_handle_list
, &ch
->entry
);
421 /* don't carry on parsing fields within this type */
424 if ((type_get_type(type
) != TYPE_BASIC
||
425 type_basic_get_type(type
) != TYPE_BASIC_HANDLE
) &&
426 is_attr(type
->attrs
, ATTR_HANDLE
)) {
427 if (!generic_handle_registered(name
))
429 generic_handle_t
*gh
= xmalloc(sizeof(*gh
));
430 gh
->name
= xstrdup(name
);
431 list_add_tail(&generic_handle_list
, &gh
->entry
);
433 /* don't carry on parsing fields within this type */
436 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
)) {
437 if (!user_type_registered(name
))
439 user_type_t
*ut
= xmalloc(sizeof *ut
);
440 ut
->name
= xstrdup(name
);
441 list_add_tail(&user_type_list
, &ut
->entry
);
443 /* don't carry on parsing fields within this type as we are already
444 * using a wire marshaled type */
447 else if (type_is_complete(type
))
450 switch (type_get_type_detect_alias(type
))
453 vars
= type_enum_get_values(type
);
456 vars
= type_struct_get_fields(type
);
459 vars
= type_union_get_cases(type
);
465 check_for_additional_prototype_types(vars
);
468 if (type_is_alias(type
))
469 type
= type_alias_get_aliasee(type
);
470 else if (is_ptr(type
))
471 type
= type_pointer_get_ref(type
);
472 else if (is_array(type
))
473 type
= type_array_get_element(type
);
480 static void write_user_types(FILE *header
)
483 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
485 const char *name
= ut
->name
;
486 fprintf(header
, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name
, name
);
487 fprintf(header
, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name
, name
);
488 fprintf(header
, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name
, name
);
489 fprintf(header
, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name
, name
);
493 static void write_context_handle_rundowns(FILE *header
)
495 context_handle_t
*ch
;
496 LIST_FOR_EACH_ENTRY(ch
, &context_handle_list
, context_handle_t
, entry
)
498 const char *name
= ch
->name
;
499 fprintf(header
, "void __RPC_USER %s_rundown(%s);\n", name
, name
);
503 static void write_generic_handle_routines(FILE *header
)
505 generic_handle_t
*gh
;
506 LIST_FOR_EACH_ENTRY(gh
, &generic_handle_list
, generic_handle_t
, entry
)
508 const char *name
= gh
->name
;
509 fprintf(header
, "handle_t __RPC_USER %s_bind(%s);\n", name
, name
);
510 fprintf(header
, "void __RPC_USER %s_unbind(%s, handle_t);\n", name
, name
);
514 static void write_typedef(FILE *header
, type_t
*type
)
516 fprintf(header
, "typedef ");
517 write_type_def_or_decl(header
, type_alias_get_aliasee(type
), FALSE
, type
->name
);
518 fprintf(header
, ";\n");
521 int is_const_decl(const var_t
*var
)
524 /* strangely, MIDL accepts a const attribute on any pointer in the
525 * declaration to mean that data isn't being instantiated. this appears
526 * to be a bug, but there is no benefit to being incompatible with MIDL,
527 * so we'll do the same thing */
528 for (t
= var
->type
; ; )
530 if (is_attr(t
->attrs
, ATTR_CONST
))
533 t
= type_pointer_get_ref(t
);
539 static void write_declaration(FILE *header
, const var_t
*v
)
541 if (is_const_decl(v
) && v
->eval
)
543 fprintf(header
, "#define %s (", v
->name
);
544 write_expr(header
, v
->eval
, 0, 1, NULL
, NULL
, "");
545 fprintf(header
, ")\n\n");
552 case STG_REGISTER
: /* ignored */
555 fprintf(header
, "static ");
558 fprintf(header
, "extern ");
561 write_type_def_or_decl(header
, v
->type
, FALSE
, v
->name
);
562 fprintf(header
, ";\n\n");
566 static void write_library(FILE *header
, const typelib_t
*typelib
)
568 const UUID
*uuid
= get_attrp(typelib
->attrs
, ATTR_UUID
);
569 fprintf(header
, "\n");
570 write_guid(header
, "LIBID", typelib
->name
, uuid
);
571 fprintf(header
, "\n");
575 const var_t
* get_explicit_handle_var(const var_t
*func
)
579 if (!type_get_function_args(func
->type
))
582 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
584 const type_t
*type
= var
->type
;
585 if (type_get_type(type
) == TYPE_BASIC
&& type_basic_get_type(type
) == TYPE_BASIC_HANDLE
)
592 const type_t
* get_explicit_generic_handle_type(const var_t
* var
)
596 is_ptr(t
) || type_is_alias(t
);
597 t
= type_is_alias(t
) ? type_alias_get_aliasee(t
) : type_pointer_get_ref(t
))
598 if ((type_get_type_detect_alias(t
) != TYPE_BASIC
|| type_basic_get_type(t
) != TYPE_BASIC_HANDLE
) &&
599 is_attr(t
->attrs
, ATTR_HANDLE
))
604 const var_t
* get_explicit_generic_handle_var(const var_t
*func
)
608 if (!type_get_function_args(func
->type
))
611 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
612 if (get_explicit_generic_handle_type(var
))
618 const var_t
* get_context_handle_var(const var_t
*func
)
622 if (!type_get_function_args(func
->type
))
625 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
626 if (is_attr(var
->attrs
, ATTR_IN
) && is_context_handle(var
->type
))
632 int has_out_arg_or_return(const var_t
*func
)
636 if (!is_void(type_function_get_rettype(func
->type
)))
639 if (!type_get_function_args(func
->type
))
642 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
643 if (is_attr(var
->attrs
, ATTR_OUT
))
650 /********** INTERFACES **********/
652 int is_object(const attr_list_t
*list
)
655 if (list
) LIST_FOR_EACH_ENTRY( attr
, list
, const attr_t
, entry
)
656 if (attr
->type
== ATTR_OBJECT
|| attr
->type
== ATTR_ODL
) return 1;
660 int is_local(const attr_list_t
*a
)
662 return is_attr(a
, ATTR_LOCAL
);
665 const var_t
*is_callas(const attr_list_t
*a
)
667 return get_attrp(a
, ATTR_CALLAS
);
670 static void write_method_macro(FILE *header
, const type_t
*iface
, const char *name
)
672 const statement_t
*stmt
;
675 if (type_iface_get_inherit(iface
))
676 write_method_macro(header
, type_iface_get_inherit(iface
), name
);
678 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
680 const var_t
*func
= stmt
->u
.var
;
684 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
688 if (!is_callas(func
->attrs
)) {
691 fprintf(header
, "#define %s_%s(This", name
, get_name(func
));
692 if (type_get_function_args(func
->type
))
693 LIST_FOR_EACH_ENTRY( arg
, type_get_function_args(func
->type
), const var_t
, entry
)
694 fprintf(header
, ",%s", arg
->name
);
695 fprintf(header
, ") ");
697 fprintf(header
, "(This)->lpVtbl->%s(This", get_name(func
));
698 if (type_get_function_args(func
->type
))
699 LIST_FOR_EACH_ENTRY( arg
, type_get_function_args(func
->type
), const var_t
, entry
)
700 fprintf(header
, ",%s", arg
->name
);
701 fprintf(header
, ")\n");
706 void write_args(FILE *h
, const var_list_t
*args
, const char *name
, int method
, int do_indent
)
717 fprintf(h
, "%s* This", name
);
720 if (args
) LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
) {
727 else fprintf(h
, ",");
729 write_type_decl(h
, arg
->type
, arg
->name
);
732 if (do_indent
) indentation
--;
735 static void write_cpp_method_def(FILE *header
, const type_t
*iface
)
737 const statement_t
*stmt
;
739 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
741 const var_t
*func
= stmt
->u
.var
;
742 if (!is_callas(func
->attrs
)) {
743 const char *callconv
= get_attrp(func
->type
->attrs
, ATTR_CALLCONV
);
744 if (!callconv
) callconv
= "";
746 fprintf(header
, "virtual ");
747 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
748 fprintf(header
, " %s %s(\n", callconv
, get_name(func
));
749 write_args(header
, type_get_function_args(func
->type
), iface
->name
, 2, TRUE
);
750 fprintf(header
, ") = 0;\n");
751 fprintf(header
, "\n");
756 static void do_write_c_method_def(FILE *header
, const type_t
*iface
, const char *name
)
758 const statement_t
*stmt
;
761 if (type_iface_get_inherit(iface
))
762 do_write_c_method_def(header
, type_iface_get_inherit(iface
), name
);
764 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
766 const var_t
*func
= stmt
->u
.var
;
769 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
772 if (!is_callas(func
->attrs
)) {
773 const char *callconv
= get_attrp(func
->type
->attrs
, ATTR_CALLCONV
);
774 if (!callconv
) callconv
= "";
776 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
777 fprintf(header
, " (%s *%s)(\n", callconv
, get_name(func
));
778 write_args(header
, type_get_function_args(func
->type
), name
, 1, TRUE
);
779 fprintf(header
, ");\n");
780 fprintf(header
, "\n");
785 static void write_c_method_def(FILE *header
, const type_t
*iface
)
787 do_write_c_method_def(header
, iface
, iface
->name
);
790 static void write_c_disp_method_def(FILE *header
, const type_t
*iface
)
792 do_write_c_method_def(header
, type_iface_get_inherit(iface
), iface
->name
);
795 static void write_method_proto(FILE *header
, const type_t
*iface
)
797 const statement_t
*stmt
;
799 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
))
801 const var_t
*func
= stmt
->u
.var
;
803 if (!is_local(func
->attrs
)) {
804 const char *callconv
= get_attrp(func
->type
->attrs
, ATTR_CALLCONV
);
805 if (!callconv
) callconv
= "";
806 /* proxy prototype */
807 write_type_decl_left(header
, type_function_get_rettype(func
->type
));
808 fprintf(header
, " %s %s_%s_Proxy(\n", callconv
, iface
->name
, get_name(func
));
809 write_args(header
, type_get_function_args(func
->type
), iface
->name
, 1, TRUE
);
810 fprintf(header
, ");\n");
812 fprintf(header
, "void __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(func
));
813 fprintf(header
, " IRpcStubBuffer* This,\n");
814 fprintf(header
, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
815 fprintf(header
, " PRPC_MESSAGE pRpcMessage,\n");
816 fprintf(header
, " DWORD* pdwStubPhase);\n");
821 static void write_locals(FILE *fp
, const type_t
*iface
, int body
)
823 static const char comment
[]
824 = "/* WIDL-generated stub. You must provide an implementation for this. */";
825 const statement_t
*stmt
;
827 if (!is_object(iface
->attrs
))
830 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
)) {
831 const var_t
*func
= stmt
->u
.var
;
832 const var_t
*cas
= is_callas(func
->attrs
);
835 const statement_t
*stmt2
= NULL
;
836 STATEMENTS_FOR_EACH_FUNC(stmt2
, type_iface_get_stmts(iface
))
837 if (!strcmp(stmt2
->u
.var
->name
, cas
->name
))
839 if (&stmt2
->entry
!= type_iface_get_stmts(iface
)) {
840 const var_t
*m
= stmt2
->u
.var
;
841 /* proxy prototype - use local prototype */
842 write_type_decl_left(fp
, type_function_get_rettype(m
->type
));
843 fprintf(fp
, " CALLBACK %s_%s_Proxy(\n", iface
->name
, get_name(m
));
844 write_args(fp
, type_get_function_args(m
->type
), iface
->name
, 1, TRUE
);
847 type_t
*rt
= type_function_get_rettype(m
->type
);
848 fprintf(fp
, "\n{\n");
849 fprintf(fp
, " %s\n", comment
);
850 if (rt
->name
&& strcmp(rt
->name
, "HRESULT") == 0)
851 fprintf(fp
, " return E_NOTIMPL;\n");
852 else if (type_get_type(rt
) != TYPE_VOID
) {
854 write_type_decl(fp
, rt
, "rv");
856 fprintf(fp
, " memset(&rv, 0, sizeof rv);\n");
857 fprintf(fp
, " return rv;\n");
859 fprintf(fp
, "}\n\n");
863 /* stub prototype - use remotable prototype */
864 write_type_decl_left(fp
, type_function_get_rettype(func
->type
));
865 fprintf(fp
, " __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(m
));
866 write_args(fp
, type_get_function_args(func
->type
), iface
->name
, 1, TRUE
);
869 /* Remotable methods must all return HRESULTs. */
870 fprintf(fp
, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment
);
875 error_loc("invalid call_as attribute (%s -> %s)\n", func
->name
, cas
->name
);
880 static void write_local_stubs_stmts(FILE *local_stubs
, const statement_list_t
*stmts
)
882 const statement_t
*stmt
;
883 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
885 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
886 write_locals(local_stubs
, stmt
->u
.type
, TRUE
);
887 else if (stmt
->type
== STMT_LIBRARY
)
888 write_local_stubs_stmts(local_stubs
, stmt
->u
.lib
->stmts
);
892 void write_local_stubs(const statement_list_t
*stmts
)
896 if (!local_stubs_name
) return;
898 local_stubs
= fopen(local_stubs_name
, "w");
900 error("Could not open %s for output\n", local_stubs_name
);
903 fprintf(local_stubs
, "/* call_as/local stubs for %s */\n\n", input_name
);
904 fprintf(local_stubs
, "#include <objbase.h>\n");
905 fprintf(local_stubs
, "#include \"%s\"\n\n", header_name
);
907 write_local_stubs_stmts(local_stubs
, stmts
);
912 static void write_function_proto(FILE *header
, const type_t
*iface
, const var_t
*fun
, const char *prefix
)
914 const char *callconv
= get_attrp(fun
->type
->attrs
, ATTR_CALLCONV
);
916 /* FIXME: do we need to handle call_as? */
917 write_type_decl_left(header
, type_function_get_rettype(fun
->type
));
918 fprintf(header
, " ");
919 if (callconv
) fprintf(header
, "%s ", callconv
);
920 fprintf(header
, "%s%s(\n", prefix
, get_name(fun
));
921 if (type_get_function_args(fun
->type
))
922 write_args(header
, type_get_function_args(fun
->type
), iface
->name
, 0, TRUE
);
924 fprintf(header
, " void");
925 fprintf(header
, ");\n\n");
928 static void write_forward(FILE *header
, type_t
*iface
)
930 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", iface
->name
);
931 fprintf(header
, "#define __%s_FWD_DEFINED__\n", iface
->name
);
932 fprintf(header
, "typedef interface %s %s;\n", iface
->name
, iface
->name
);
933 fprintf(header
, "#endif\n\n" );
936 static void write_iface_guid(FILE *header
, const type_t
*iface
)
938 const UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
939 write_guid(header
, "IID", iface
->name
, uuid
);
942 static void write_dispiface_guid(FILE *header
, const type_t
*iface
)
944 const UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
945 write_guid(header
, "DIID", iface
->name
, uuid
);
948 static void write_coclass_guid(FILE *header
, const type_t
*cocl
)
950 const UUID
*uuid
= get_attrp(cocl
->attrs
, ATTR_UUID
);
951 write_guid(header
, "CLSID", cocl
->name
, uuid
);
954 static void write_com_interface_start(FILE *header
, const type_t
*iface
)
956 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
957 fprintf(header
, "/*****************************************************************************\n");
958 fprintf(header
, " * %s %sinterface\n", iface
->name
, dispinterface
? "disp" : "");
959 fprintf(header
, " */\n");
960 fprintf(header
,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface
->name
, dispinterface
? "DISP" : "");
961 fprintf(header
,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface
->name
, dispinterface
? "DISP" : "");
964 static void write_com_interface_end(FILE *header
, type_t
*iface
)
966 int dispinterface
= is_attr(iface
->attrs
, ATTR_DISPINTERFACE
);
968 write_dispiface_guid(header
, iface
);
970 write_iface_guid(header
, iface
);
972 fprintf(header
, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
973 if (type_iface_get_inherit(iface
))
975 fprintf(header
, "interface %s : public %s\n", iface
->name
,
976 type_iface_get_inherit(iface
)->name
);
977 fprintf(header
, "{\n");
981 fprintf(header
, "interface %s\n", iface
->name
);
982 fprintf(header
, "{\n");
983 fprintf(header
, " BEGIN_INTERFACE\n");
984 fprintf(header
, "\n");
986 /* dispinterfaces don't have real functions, so don't write C++ functions for
991 write_cpp_method_def(header
, iface
);
994 if (!type_iface_get_inherit(iface
))
995 fprintf(header
, " END_INTERFACE\n");
996 fprintf(header
, "};\n");
997 fprintf(header
, "#else\n");
999 fprintf(header
, "typedef struct %sVtbl {\n", iface
->name
);
1001 fprintf(header
, " BEGIN_INTERFACE\n");
1002 fprintf(header
, "\n");
1004 write_c_disp_method_def(header
, iface
);
1006 write_c_method_def(header
, iface
);
1008 fprintf(header
, " END_INTERFACE\n");
1009 fprintf(header
, "} %sVtbl;\n", iface
->name
);
1010 fprintf(header
, "interface %s {\n", iface
->name
);
1011 fprintf(header
, " CONST_VTBL %sVtbl* lpVtbl;\n", iface
->name
);
1012 fprintf(header
, "};\n");
1013 fprintf(header
, "\n");
1014 fprintf(header
, "#ifdef COBJMACROS\n");
1015 /* dispinterfaces don't have real functions, so don't write macros for them,
1016 * only for the interface this interface inherits from, i.e. IDispatch */
1017 write_method_macro(header
, dispinterface
? type_iface_get_inherit(iface
) : iface
, iface
->name
);
1018 fprintf(header
, "#endif\n");
1019 fprintf(header
, "\n");
1020 fprintf(header
, "#endif\n");
1021 fprintf(header
, "\n");
1022 /* dispinterfaces don't have real functions, so don't write prototypes for
1026 write_method_proto(header
, iface
);
1027 write_locals(header
, iface
, FALSE
);
1028 fprintf(header
, "\n");
1030 fprintf(header
,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface
->name
, dispinterface
? "DISP" : "");
1033 static void write_rpc_interface_start(FILE *header
, const type_t
*iface
)
1035 unsigned int ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
1036 const char *var
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
1037 static int allocate_written
= 0;
1039 if (!allocate_written
)
1041 allocate_written
= 1;
1042 fprintf(header
, "void * __RPC_USER MIDL_user_allocate(SIZE_T);\n");
1043 fprintf(header
, "void __RPC_USER MIDL_user_free(void *);\n\n");
1046 fprintf(header
, "/*****************************************************************************\n");
1047 fprintf(header
, " * %s interface (v%d.%d)\n", iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1048 fprintf(header
, " */\n");
1049 fprintf(header
,"#ifndef __%s_INTERFACE_DEFINED__\n", iface
->name
);
1050 fprintf(header
,"#define __%s_INTERFACE_DEFINED__\n\n", iface
->name
);
1051 if (var
) fprintf(header
, "extern handle_t %s;\n", var
);
1054 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client
, iface
->name
);
1055 fprintf(header
, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server
, iface
->name
);
1059 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1060 prefix_client
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1061 fprintf(header
, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1062 prefix_server
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
1066 static void write_rpc_interface_end(FILE *header
, const type_t
*iface
)
1068 fprintf(header
,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface
->name
);
1071 static void write_coclass(FILE *header
, type_t
*cocl
)
1073 fprintf(header
, "/*****************************************************************************\n");
1074 fprintf(header
, " * %s coclass\n", cocl
->name
);
1075 fprintf(header
, " */\n\n");
1076 write_coclass_guid(header
, cocl
);
1077 fprintf(header
, "\n");
1080 static void write_coclass_forward(FILE *header
, type_t
*cocl
)
1082 fprintf(header
, "#ifndef __%s_FWD_DEFINED__\n", cocl
->name
);
1083 fprintf(header
, "#define __%s_FWD_DEFINED__\n", cocl
->name
);
1084 fprintf(header
, "typedef struct %s %s;\n", cocl
->name
, cocl
->name
);
1085 fprintf(header
, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl
->name
);
1088 static void write_import(FILE *header
, const char *fname
)
1092 hname
= dup_basename(fname
, ".idl");
1093 p
= hname
+ strlen(hname
) - 2;
1094 if (p
<= hname
|| strcmp( p
, ".h" )) strcat(hname
, ".h");
1096 fprintf(header
, "#include <%s>\n", hname
);
1100 static void write_imports(FILE *header
, const statement_list_t
*stmts
)
1102 const statement_t
*stmt
;
1103 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1108 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1109 write_imports(header
, type_iface_get_stmts(stmt
->u
.type
));
1112 case STMT_IMPORTLIB
:
1113 /* not included in header */
1116 write_import(header
, stmt
->u
.str
);
1121 case STMT_DECLARATION
:
1122 /* not processed here */
1125 write_imports(header
, stmt
->u
.lib
->stmts
);
1131 static void write_forward_decls(FILE *header
, const statement_list_t
*stmts
)
1133 const statement_t
*stmt
;
1134 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1139 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1141 if (is_object(stmt
->u
.type
->attrs
) || is_attr(stmt
->u
.type
->attrs
, ATTR_DISPINTERFACE
))
1142 write_forward(header
, stmt
->u
.type
);
1144 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
1145 write_coclass_forward(header
, stmt
->u
.type
);
1148 case STMT_IMPORTLIB
:
1149 /* not included in header */
1155 case STMT_DECLARATION
:
1156 /* not processed here */
1159 write_forward_decls(header
, stmt
->u
.lib
->stmts
);
1165 static void write_header_stmts(FILE *header
, const statement_list_t
*stmts
, const type_t
*iface
, int ignore_funcs
)
1167 const statement_t
*stmt
;
1168 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1173 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1175 type_t
*iface
= stmt
->u
.type
;
1176 if (is_attr(stmt
->u
.type
->attrs
, ATTR_DISPINTERFACE
) || is_object(stmt
->u
.type
->attrs
))
1178 write_com_interface_start(header
, iface
);
1179 write_header_stmts(header
, type_iface_get_stmts(iface
), stmt
->u
.type
, TRUE
);
1180 write_com_interface_end(header
, iface
);
1184 write_rpc_interface_start(header
, iface
);
1185 write_header_stmts(header
, type_iface_get_stmts(iface
), iface
, FALSE
);
1186 write_rpc_interface_end(header
, iface
);
1189 else if (type_get_type(stmt
->u
.type
) == TYPE_COCLASS
)
1190 write_coclass(header
, stmt
->u
.type
);
1193 write_type_def_or_decl(header
, stmt
->u
.type
, FALSE
, NULL
);
1194 fprintf(header
, ";\n\n");
1198 /* FIXME: shouldn't write out forward declarations for undefined
1199 * interfaces but a number of our IDL files depend on this */
1200 if (type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
&& !stmt
->u
.type
->written
)
1201 write_forward(header
, stmt
->u
.type
);
1203 case STMT_IMPORTLIB
:
1205 /* not included in header */
1208 /* not processed here */
1212 const type_list_t
*type_entry
= stmt
->u
.type_list
;
1213 for (; type_entry
; type_entry
= type_entry
->next
)
1214 write_typedef(header
, type_entry
->type
);
1218 write_library(header
, stmt
->u
.lib
);
1219 write_header_stmts(header
, stmt
->u
.lib
->stmts
, NULL
, FALSE
);
1222 fprintf(header
, "%s\n", stmt
->u
.str
);
1224 case STMT_DECLARATION
:
1225 if (iface
&& type_get_type(stmt
->u
.var
->type
) == TYPE_FUNCTION
)
1229 int prefixes_differ
= strcmp(prefix_client
, prefix_server
);
1231 if (prefixes_differ
)
1233 fprintf(header
, "/* client prototype */\n");
1234 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_client
);
1235 fprintf(header
, "/* server prototype */\n");
1237 write_function_proto(header
, iface
, stmt
->u
.var
, prefix_server
);
1241 write_declaration(header
, stmt
->u
.var
);
1247 void write_header(const statement_list_t
*stmts
)
1251 if (!do_header
) return;
1253 if(!(header
= fopen(header_name
, "w"))) {
1254 error("Could not open %s for output\n", header_name
);
1257 fprintf(header
, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION
, input_name
);
1258 fprintf(header
, "#include <rpc.h>\n" );
1259 fprintf(header
, "#include <rpcndr.h>\n\n" );
1261 fprintf(header
, "#ifndef __WIDL_%s\n", header_token
);
1262 fprintf(header
, "#define __WIDL_%s\n\n", header_token
);
1263 start_cplusplus_guard(header
);
1265 fprintf(header
, "/* Headers for imported files */\n\n");
1266 write_imports(header
, stmts
);
1267 fprintf(header
, "\n");
1269 /* FIXME: should be before imported file includes */
1270 fprintf(header
, "/* Forward declarations */\n\n");
1271 write_forward_decls(header
, stmts
);
1272 fprintf(header
, "\n");
1274 write_header_stmts(header
, stmts
, NULL
, FALSE
);
1276 fprintf(header
, "/* Begin additional prototypes for all interfaces */\n");
1277 fprintf(header
, "\n");
1278 write_user_types(header
);
1279 write_generic_handle_routines(header
);
1280 write_context_handle_rundowns(header
);
1281 fprintf(header
, "\n");
1282 fprintf(header
, "/* End additional prototypes */\n");
1283 fprintf(header
, "\n");
1285 end_cplusplus_guard(header
);
1286 fprintf(header
, "#endif /* __WIDL_%s */\n", header_token
);