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
38 typedef struct _user_type_t generic_handle_t;
40 static int indentation = 0;
41 user_type_list_t user_type_list = LIST_INIT(user_type_list);
42 static context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
43 static struct list generic_handle_list = LIST_INIT(generic_handle_list);
45 static void indent(FILE *h, int delta)
48 if (delta < 0) indentation += delta;
49 for (c=0; c<indentation; c++) fprintf(h, " ");
50 if (delta > 0) indentation += delta;
53 int is_ptrchain_attr(const var_t *var, enum attr_type t)
55 if (is_attr(var->attrs, t))
59 type_t *type = var->type;
62 if (is_attr(type->attrs, t))
64 else if (type->kind == TKIND_ALIAS)
66 else if (is_ptr(type))
73 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
75 const type_t *t = type;
78 if (is_attr(t->attrs, attr))
80 else if (t->kind == TKIND_ALIAS)
86 int is_attr(const attr_list_t *list, enum attr_type t)
89 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
90 if (attr->type == t) return 1;
94 void *get_attrp(const attr_list_t *list, enum attr_type t)
97 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
98 if (attr->type == t) return attr->u.pval;
102 unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
105 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
106 if (attr->type == t) return attr->u.ival;
110 int is_void(const type_t *t)
112 if (!t->type && !t->ref) return 1;
116 int is_conformant_array(const type_t *t)
118 return t->type == RPC_FC_CARRAY
119 || t->type == RPC_FC_CVARRAY
120 || (t->type == RPC_FC_BOGUS_ARRAY && t->size_is);
123 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 void write_name(FILE *h, const var_t *v)
135 if (is_attr( v->attrs, ATTR_PROPGET ))
137 else if (is_attr( v->attrs, ATTR_PROPPUT ))
139 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
140 fprintf(h, "putref_" );
141 fprintf(h, "%s", v->name);
144 void write_prefix_name(FILE *h, const char *prefix, const var_t *v)
146 fprintf(h, "%s", prefix);
150 static void write_field(FILE *h, var_t *v)
154 const char *name = v->name;
156 switch (v->type->type) {
158 case RPC_FC_CVSTRUCT:
159 case RPC_FC_CPSTRUCT:
162 case RPC_FC_BOGUS_STRUCT:
163 case RPC_FC_ENCAPSULATED_UNION:
164 name = "DUMMYSTRUCTNAME";
166 case RPC_FC_NON_ENCAPSULATED_UNION:
167 name = "DUMMYUNIONNAME";
175 write_type_def_or_decl(h, v->type, TRUE, "%s", 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 )
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 (t->kind == TKIND_ALIAS
209 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
212 void write_type_left(FILE *h, type_t *t, int declonly)
216 if (is_attr(t->attrs, ATTR_CONST) &&
217 (t->kind == TKIND_ALIAS || t->declarray || !is_ptr(t)))
218 fprintf(h, "const ");
220 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
221 else if (t->declarray) write_type_left(h, t->ref, declonly);
223 if (t->sign > 0) fprintf(h, "signed ");
224 else if (t->sign < 0) fprintf(h, "unsigned ");
228 if (!declonly && t->defined && !t->written && !t->ignore) {
229 if (t->name) fprintf(h, "enum %s {\n", t->name);
230 else fprintf(h, "enum {\n");
233 write_enums(h, t->fields_or_args);
237 else fprintf(h, "enum %s", t->name ? t->name : "");
240 case RPC_FC_CVSTRUCT:
241 case RPC_FC_CPSTRUCT:
244 case RPC_FC_BOGUS_STRUCT:
245 case RPC_FC_ENCAPSULATED_UNION:
246 if (!declonly && t->defined && !t->written && !t->ignore) {
247 if (t->name) fprintf(h, "struct %s {\n", t->name);
248 else fprintf(h, "struct {\n");
251 write_fields(h, t->fields_or_args);
255 else fprintf(h, "struct %s", t->name ? t->name : "");
257 case RPC_FC_NON_ENCAPSULATED_UNION:
258 if (!declonly && t->defined && !t->written && !t->ignore) {
259 if (t->name) fprintf(h, "union %s {\n", t->name);
260 else fprintf(h, "union {\n");
263 write_fields(h, t->fields_or_args);
267 else fprintf(h, "union %s", t->name ? t->name : "");
275 case RPC_FC_BOGUS_ARRAY:
276 write_type_left(h, t->ref, declonly);
277 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
278 if (is_ptr(t) && is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
281 fprintf(h, "%s", t->name);
286 void write_type_right(FILE *h, type_t *t, int is_field)
291 if (is_conformant_array(t)) {
292 fprintf(h, "[%s]", is_field ? "1" : "");
295 for ( ; t->declarray; t = t->ref)
296 fprintf(h, "[%lu]", t->dim);
300 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
301 const char *fmt, va_list args)
308 for (pt = t; is_ptr(pt); pt = pt->ref, ptr_level++)
311 if (pt->type == RPC_FC_FUNCTION) {
313 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
314 if (!callconv) callconv = "";
315 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
316 write_type_left(h, pt->ref, declonly);
318 if (ptr_level) fputc('(', h);
319 fprintf(h, "%s ", callconv);
320 for (i = 0; i < ptr_level; i++)
323 write_type_left(h, t, declonly);
325 if (needs_space_after(t))
327 vfprintf(h, fmt, args);
329 if (pt->type == RPC_FC_FUNCTION) {
330 if (ptr_level) fputc(')', h);
332 write_args(h, pt->fields_or_args, NULL, 0, FALSE);
335 write_type_right(h, t, is_field);
338 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
342 write_type_v(f, t, field, FALSE, fmt, args);
346 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
350 write_type_v(f, t, FALSE, TRUE, fmt, args);
354 void write_type_decl_left(FILE *f, type_t *t)
356 write_type_left(f, t, TRUE);
359 static int user_type_registered(const char *name)
362 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
363 if (!strcmp(name, ut->name))
368 static int context_handle_registered(const char *name)
370 context_handle_t *ch;
371 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
372 if (!strcmp(name, ch->name))
377 static int generic_handle_registered(const char *name)
379 generic_handle_t *gh;
380 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
381 if (!strcmp(name, gh->name))
386 /* check for types which require additional prototypes to be generated in the
388 void check_for_additional_prototype_types(const var_list_t *list)
393 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
396 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
397 const char *name = type->name;
398 if (type->user_types_registered) continue;
399 type->user_types_registered = 1;
400 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
401 if (!context_handle_registered(name))
403 context_handle_t *ch = xmalloc(sizeof(*ch));
404 ch->name = xstrdup(name);
405 list_add_tail(&context_handle_list, &ch->entry);
407 /* don't carry on parsing fields within this type */
410 if (type->type != RPC_FC_BIND_PRIMITIVE && is_attr(type->attrs, ATTR_HANDLE)) {
411 if (!generic_handle_registered(name))
413 generic_handle_t *gh = xmalloc(sizeof(*gh));
414 gh->name = xstrdup(name);
415 list_add_tail(&generic_handle_list, &gh->entry);
417 /* don't carry on parsing fields within this type */
420 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
421 if (!user_type_registered(name))
423 user_type_t *ut = xmalloc(sizeof *ut);
424 ut->name = xstrdup(name);
425 list_add_tail(&user_type_list, &ut->entry);
427 /* don't carry on parsing fields within this type as we are already
428 * using a wire marshaled type */
433 check_for_additional_prototype_types(type->fields_or_args);
439 void write_user_types(void)
442 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
444 const char *name = ut->name;
445 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
446 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
447 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
448 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
452 void write_context_handle_rundowns(void)
454 context_handle_t *ch;
455 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
457 const char *name = ch->name;
458 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
462 void write_generic_handle_routines(void)
464 generic_handle_t *gh;
465 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
467 const char *name = gh->name;
468 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
469 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
473 void write_typedef(type_t *type)
475 fprintf(header, "typedef ");
476 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
477 fprintf(header, ";\n");
480 int is_const_decl(const var_t *var)
483 /* strangely, MIDL accepts a const attribute on any pointer in the
484 * declaration to mean that data isn't being instantiated. this appears
485 * to be a bug, but there is no benefit to being incompatible with MIDL,
486 * so we'll do the same thing */
487 for (t = var->type; ; )
489 if (is_attr(t->attrs, ATTR_CONST))
498 void write_declaration(const var_t *v, int is_in_interface)
500 if (is_const_decl(v) && v->eval)
502 fprintf(header, "#define %s (", v->name);
503 write_expr(header, v->eval, 0, 1, NULL, NULL);
504 fprintf(header, ")\n\n");
506 else if (v->type->type != RPC_FC_FUNCTION || !is_in_interface)
511 case STG_REGISTER: /* ignored */
514 fprintf(header, "static ");
517 fprintf(header, "extern ");
520 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
521 fprintf(header, ";\n\n");
525 void write_library(const typelib_t *typelib)
527 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
528 fprintf(header, "\n");
529 write_guid(header, "LIBID", typelib->name, uuid);
530 fprintf(header, "\n");
534 const var_t* get_explicit_handle_var(const func_t* func)
541 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
542 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
548 const type_t* get_explicit_generic_handle_type(const var_t* var)
551 for (t = var->type; is_ptr(t); t = t->ref)
552 if (t->type != RPC_FC_BIND_PRIMITIVE && is_attr(t->attrs, ATTR_HANDLE))
557 const var_t* get_explicit_generic_handle_var(const func_t* func)
564 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
565 if (get_explicit_generic_handle_type(var))
571 const var_t* get_context_handle_var(const func_t* func)
578 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
579 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
585 int has_out_arg_or_return(const func_t *func)
589 if (!is_void(get_func_return_type(func)))
595 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
596 if (is_attr(var->attrs, ATTR_OUT))
603 /********** INTERFACES **********/
605 int is_object(const attr_list_t *list)
608 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
609 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
613 int is_local(const attr_list_t *a)
615 return is_attr(a, ATTR_LOCAL);
618 const var_t *is_callas(const attr_list_t *a)
620 return get_attrp(a, ATTR_CALLAS);
623 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
627 if (iface->ref) write_method_macro(header, iface->ref, name);
629 if (!iface->funcs) return;
631 fprintf(header, "/*** %s methods ***/\n", iface->name);
632 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
634 var_t *def = cur->def;
635 if (!is_callas(def->attrs)) {
638 fprintf(header, "#define %s_", name);
639 write_name(header,def);
640 fprintf(header, "(This");
642 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
643 fprintf(header, ",%s", arg->name);
644 fprintf(header, ") ");
646 fprintf(header, "(This)->lpVtbl->");
647 write_name(header, def);
648 fprintf(header, "(This");
650 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
651 fprintf(header, ",%s", arg->name);
652 fprintf(header, ")\n");
657 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
668 fprintf(h, "%s* This", name);
671 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
678 else fprintf(h, ",");
680 write_type_decl(h, arg->type, "%s", arg->name);
683 if (do_indent) indentation--;
686 static void write_cpp_method_def(FILE *header, const type_t *iface)
690 if (!iface->funcs) return;
692 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
694 var_t *def = cur->def;
695 if (!is_callas(def->attrs)) {
696 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
697 if (!callconv) callconv = "";
699 fprintf(header, "virtual ");
700 write_type_decl_left(header, get_func_return_type(cur));
701 fprintf(header, " %s ", callconv);
702 write_name(header, def);
703 fprintf(header, "(\n");
704 write_args(header, cur->args, iface->name, 2, TRUE);
705 fprintf(header, ") = 0;\n");
706 fprintf(header, "\n");
711 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
715 if (iface->ref) do_write_c_method_def(header, iface->ref, name);
717 if (!iface->funcs) return;
719 fprintf(header, "/*** %s methods ***/\n", iface->name);
720 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
722 const var_t *def = cur->def;
723 if (!is_callas(def->attrs)) {
724 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
725 if (!callconv) callconv = "";
727 write_type_decl_left(header, get_func_return_type(cur));
728 fprintf(header, " (%s *", callconv);
729 write_name(header, def);
730 fprintf(header, ")(\n");
731 write_args(header, cur->args, name, 1, TRUE);
732 fprintf(header, ");\n");
733 fprintf(header, "\n");
738 static void write_c_method_def(FILE *header, const type_t *iface)
740 do_write_c_method_def(header, iface, iface->name);
743 static void write_c_disp_method_def(FILE *header, const type_t *iface)
745 do_write_c_method_def(header, iface->ref, iface->name);
748 static void write_method_proto(FILE *header, const type_t *iface)
752 if (!iface->funcs) return;
753 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
755 const var_t *def = cur->def;
757 if (!is_local(def->attrs)) {
758 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
759 if (!callconv) callconv = "";
760 /* proxy prototype */
761 write_type_decl_left(header, get_func_return_type(cur));
762 fprintf(header, " %s %s_", callconv, iface->name);
763 write_name(header, def);
764 fprintf(header, "_Proxy(\n");
765 write_args(header, cur->args, iface->name, 1, TRUE);
766 fprintf(header, ");\n");
768 fprintf(header, "void __RPC_STUB %s_", iface->name);
769 write_name(header,def);
770 fprintf(header, "_Stub(\n");
771 fprintf(header, " IRpcStubBuffer* This,\n");
772 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
773 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
774 fprintf(header, " DWORD* pdwStubPhase);\n");
779 void write_locals(FILE *fp, const type_t *iface, int body)
781 static const char comment[]
782 = "/* WIDL-generated stub. You must provide an implementation for this. */";
783 const func_list_t *funcs = iface->funcs;
786 if (!is_object(iface->attrs) || !funcs)
789 LIST_FOR_EACH_ENTRY(cur, funcs, const func_t, entry) {
790 const var_t *def = cur->def;
791 const var_t *cas = is_callas(def->attrs);
795 LIST_FOR_EACH_ENTRY(m, iface->funcs, const func_t, entry)
796 if (!strcmp(m->def->name, cas->name))
798 if (&m->entry != iface->funcs) {
799 const var_t *mdef = m->def;
800 /* proxy prototype - use local prototype */
801 write_type_decl_left(fp, get_func_return_type(m));
802 fprintf(fp, " CALLBACK %s_", iface->name);
803 write_name(fp, mdef);
804 fprintf(fp, "_Proxy(\n");
805 write_args(fp, m->args, iface->name, 1, TRUE);
808 type_t *rt = get_func_return_type(m);
809 fprintf(fp, "\n{\n");
810 fprintf(fp, " %s\n", comment);
811 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
812 fprintf(fp, " return E_NOTIMPL;\n");
815 write_type_decl(fp, rt, "rv");
817 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
818 fprintf(fp, " return rv;\n");
820 fprintf(fp, "}\n\n");
824 /* stub prototype - use remotable prototype */
825 write_type_decl_left(fp, get_func_return_type(cur));
826 fprintf(fp, " __RPC_STUB %s_", iface->name);
827 write_name(fp, mdef);
828 fprintf(fp, "_Stub(\n");
829 write_args(fp, cur->args, iface->name, 1, TRUE);
832 /* Remotable methods must all return HRESULTs. */
833 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
838 error_loc("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
843 static void write_function_proto(FILE *header, const type_t *iface, const func_t *fun, const char *prefix)
845 var_t *def = fun->def;
846 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
848 /* FIXME: do we need to handle call_as? */
849 write_type_decl_left(header, get_func_return_type(fun));
850 fprintf(header, " ");
851 if (callconv) fprintf(header, "%s ", callconv);
852 write_prefix_name(header, prefix, def);
853 fprintf(header, "(\n");
855 write_args(header, fun->args, iface->name, 0, TRUE);
857 fprintf(header, " void");
858 fprintf(header, ");\n\n");
861 static void write_function_protos(FILE *header, const type_t *iface)
864 int prefixes_differ = strcmp(prefix_client, prefix_server);
866 if (!iface->funcs) return;
867 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
869 if (prefixes_differ) {
870 fprintf(header, "/* client prototype */\n");
871 write_function_proto(header, iface, cur, prefix_client);
872 fprintf(header, "/* server prototype */\n");
874 write_function_proto(header, iface, cur, prefix_server);
878 void write_forward(type_t *iface)
880 /* C/C++ forwards should only be written for object interfaces, so if we
881 * have a full definition we only write one if we find [object] among the
882 * attributes - however, if we don't have a full definition at this point
883 * (i.e. this is an IDL forward), then we also assume that it is an object
884 * interface, since non-object interfaces shouldn't need forwards */
885 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
886 && !iface->written) {
887 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
888 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
889 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
890 fprintf(header, "#endif\n\n" );
891 iface->written = TRUE;
895 static void write_iface_guid(FILE *header, const type_t *iface)
897 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
898 write_guid(header, "IID", iface->name, uuid);
901 static void write_dispiface_guid(FILE *header, const type_t *iface)
903 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
904 write_guid(header, "DIID", iface->name, uuid);
907 static void write_coclass_guid(FILE *header, const type_t *cocl)
909 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
910 write_guid(header, "CLSID", cocl->name, uuid);
913 static void write_com_interface_start(FILE *header, const type_t *iface)
915 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
916 fprintf(header, "/*****************************************************************************\n");
917 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
918 fprintf(header, " */\n");
919 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
920 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
923 static void write_com_interface_end(FILE *header, type_t *iface)
925 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
927 write_dispiface_guid(header, iface);
929 write_iface_guid(header, iface);
931 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
934 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
935 fprintf(header, "{\n");
939 fprintf(header, "interface %s\n", iface->name);
940 fprintf(header, "{\n");
941 fprintf(header, " BEGIN_INTERFACE\n");
942 fprintf(header, "\n");
944 /* dispinterfaces don't have real functions, so don't write C++ functions for
949 write_cpp_method_def(header, iface);
953 fprintf(header, " END_INTERFACE\n");
954 fprintf(header, "};\n");
955 fprintf(header, "#else\n");
957 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
959 fprintf(header, " BEGIN_INTERFACE\n");
960 fprintf(header, "\n");
962 write_c_disp_method_def(header, iface);
964 write_c_method_def(header, iface);
966 fprintf(header, " END_INTERFACE\n");
967 fprintf(header, "} %sVtbl;\n", iface->name);
968 fprintf(header, "interface %s {\n", iface->name);
969 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
970 fprintf(header, "};\n");
971 fprintf(header, "\n");
972 fprintf(header, "#ifdef COBJMACROS\n");
973 /* dispinterfaces don't have real functions, so don't write macros for them,
974 * only for the interface this interface inherits from, i.e. IDispatch */
975 write_method_macro(header, dispinterface ? iface->ref : iface, iface->name);
976 fprintf(header, "#endif\n");
977 fprintf(header, "\n");
978 fprintf(header, "#endif\n");
979 fprintf(header, "\n");
980 /* dispinterfaces don't have real functions, so don't write prototypes for
984 write_method_proto(header, iface);
985 write_locals(header, iface, FALSE);
986 fprintf(header, "\n");
988 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
991 static void write_rpc_interface_start(FILE *header, const type_t *iface)
993 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
994 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
995 static int allocate_written = 0;
997 if (!allocate_written)
999 allocate_written = 1;
1000 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
1001 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
1004 fprintf(header, "/*****************************************************************************\n");
1005 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1006 fprintf(header, " */\n");
1007 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1008 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1009 if (var) fprintf(header, "extern handle_t %s;\n", var);
1012 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1013 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1017 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1018 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1019 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1020 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1024 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1026 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1029 void write_interface(type_t *iface)
1031 if (is_attr(iface->attrs, ATTR_DISPINTERFACE) || is_object(iface->attrs))
1033 write_com_interface_start(header, iface);
1034 write_com_interface_end(header, iface);
1038 write_rpc_interface_start(header, iface);
1039 write_function_protos(header, iface);
1040 write_rpc_interface_end(header, iface);
1044 void write_coclass(type_t *cocl)
1046 fprintf(header, "/*****************************************************************************\n");
1047 fprintf(header, " * %s coclass\n", cocl->name);
1048 fprintf(header, " */\n\n");
1049 write_coclass_guid(header, cocl);
1050 fprintf(header, "\n");
1053 void write_coclass_forward(type_t *cocl)
1055 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1056 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1057 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1058 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1061 void write_import(const char *fname)
1065 hname = dup_basename(fname, ".idl");
1066 p = hname + strlen(hname) - 2;
1067 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1069 fprintf(header, "#include <%s>\n", hname);