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->kind == TKIND_ALIAS)
67 else if (is_ptr(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 (t->kind == TKIND_ALIAS)
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 if (!t->type && !t->ref) return 1;
117 int is_conformant_array(const type_t *t)
119 return t->type == RPC_FC_CARRAY
120 || t->type == RPC_FC_CVARRAY
121 || (t->type == RPC_FC_BOGUS_ARRAY && t->size_is);
124 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
127 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
128 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
129 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
130 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
131 uuid->Data4[6], uuid->Data4[7]);
134 const char *get_name(const var_t *v)
136 static char buffer[256];
138 if (is_attr( v->attrs, ATTR_PROPGET ))
139 strcpy( buffer, "get_" );
140 else if (is_attr( v->attrs, ATTR_PROPPUT ))
141 strcpy( buffer, "put_" );
142 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
143 strcpy( buffer, "putref_" );
146 strcat( buffer, v->name );
150 static void write_field(FILE *h, var_t *v)
155 write_type_def_or_decl(h, v->type, TRUE, "%s", v->name);
160 static void write_fields(FILE *h, var_list_t *fields)
164 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
167 static void write_enums(FILE *h, var_list_t *enums)
171 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
175 fprintf(h, "%s", get_name(v));
178 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
181 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
186 int needs_space_after(type_t *t)
188 return (t->kind == TKIND_ALIAS
189 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
192 void write_type_left(FILE *h, type_t *t, int declonly)
196 if (is_attr(t->attrs, ATTR_CONST) &&
197 (t->kind == TKIND_ALIAS || t->declarray || !is_ptr(t)))
198 fprintf(h, "const ");
200 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
201 else if (t->declarray) write_type_left(h, t->ref, declonly);
203 if (t->sign > 0) fprintf(h, "signed ");
204 else if (t->sign < 0) fprintf(h, "unsigned ");
208 if (!declonly && t->defined && !t->written && !t->ignore) {
209 if (t->name) fprintf(h, "enum %s {\n", t->name);
210 else fprintf(h, "enum {\n");
213 write_enums(h, type_enum_get_values(t));
217 else fprintf(h, "enum %s", t->name ? t->name : "");
220 case RPC_FC_CVSTRUCT:
221 case RPC_FC_CPSTRUCT:
224 case RPC_FC_BOGUS_STRUCT:
225 case RPC_FC_ENCAPSULATED_UNION:
226 if (!declonly && t->defined && !t->written && !t->ignore) {
227 if (t->name) fprintf(h, "struct %s {\n", t->name);
228 else fprintf(h, "struct {\n");
231 if (t->type == RPC_FC_ENCAPSULATED_UNION)
232 write_fields(h, type_encapsulated_union_get_fields(t));
234 write_fields(h, type_struct_get_fields(t));
238 else fprintf(h, "struct %s", t->name ? t->name : "");
240 case RPC_FC_NON_ENCAPSULATED_UNION:
241 if (!declonly && t->defined && !t->written && !t->ignore) {
242 if (t->name) fprintf(h, "union %s {\n", t->name);
243 else fprintf(h, "union {\n");
246 write_fields(h, type_union_get_cases(t));
250 else fprintf(h, "union %s", t->name ? t->name : "");
258 case RPC_FC_BOGUS_ARRAY:
259 write_type_left(h, t->ref, declonly);
260 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
261 if (is_ptr(t) && is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
264 fprintf(h, "%s", t->name);
269 void write_type_right(FILE *h, type_t *t, int is_field)
274 if (is_conformant_array(t)) {
275 fprintf(h, "[%s]", is_field ? "1" : "");
278 for ( ; t->declarray; t = t->ref)
279 fprintf(h, "[%lu]", t->dim);
283 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
284 const char *fmt, va_list args)
291 for (pt = t; is_ptr(pt); pt = pt->ref, ptr_level++)
294 if (pt->type == RPC_FC_FUNCTION) {
296 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
297 if (!callconv) callconv = "";
298 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
299 write_type_left(h, pt->ref, declonly);
301 if (ptr_level) fputc('(', h);
302 fprintf(h, "%s ", callconv);
303 for (i = 0; i < ptr_level; i++)
306 write_type_left(h, t, declonly);
308 if (needs_space_after(t))
310 vfprintf(h, fmt, args);
312 if (pt->type == RPC_FC_FUNCTION) {
313 if (ptr_level) fputc(')', h);
315 write_args(h, type_function_get_args(pt), NULL, 0, FALSE);
318 write_type_right(h, t, is_field);
321 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
325 write_type_v(f, t, field, FALSE, fmt, args);
329 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
333 write_type_v(f, t, FALSE, TRUE, fmt, args);
337 void write_type_decl_left(FILE *f, type_t *t)
339 write_type_left(f, t, TRUE);
342 static int user_type_registered(const char *name)
345 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
346 if (!strcmp(name, ut->name))
351 static int context_handle_registered(const char *name)
353 context_handle_t *ch;
354 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
355 if (!strcmp(name, ch->name))
360 static int generic_handle_registered(const char *name)
362 generic_handle_t *gh;
363 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
364 if (!strcmp(name, gh->name))
369 /* check for types which require additional prototypes to be generated in the
371 void check_for_additional_prototype_types(const var_list_t *list)
376 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
379 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
380 const char *name = type->name;
381 if (type->user_types_registered) continue;
382 type->user_types_registered = 1;
383 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
384 if (!context_handle_registered(name))
386 context_handle_t *ch = xmalloc(sizeof(*ch));
387 ch->name = xstrdup(name);
388 list_add_tail(&context_handle_list, &ch->entry);
390 /* don't carry on parsing fields within this type */
393 if (type->type != RPC_FC_BIND_PRIMITIVE && is_attr(type->attrs, ATTR_HANDLE)) {
394 if (!generic_handle_registered(name))
396 generic_handle_t *gh = xmalloc(sizeof(*gh));
397 gh->name = xstrdup(name);
398 list_add_tail(&generic_handle_list, &gh->entry);
400 /* don't carry on parsing fields within this type */
403 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
404 if (!user_type_registered(name))
406 user_type_t *ut = xmalloc(sizeof *ut);
407 ut->name = xstrdup(name);
408 list_add_tail(&user_type_list, &ut->entry);
410 /* don't carry on parsing fields within this type as we are already
411 * using a wire marshaled type */
416 var_list_t *vars = NULL;
417 if (type->type == RPC_FC_ENUM16 || type->type == RPC_FC_ENUM32)
418 vars = type_enum_get_values(type);
419 else if (is_struct(type->type))
420 vars = type_struct_get_fields(type);
421 else if (is_union(type->type))
422 vars = type_union_get_cases(type);
423 check_for_additional_prototype_types(vars);
429 void write_user_types(void)
432 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
434 const char *name = ut->name;
435 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
436 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
437 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
438 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
442 void write_context_handle_rundowns(void)
444 context_handle_t *ch;
445 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
447 const char *name = ch->name;
448 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
452 void write_generic_handle_routines(void)
454 generic_handle_t *gh;
455 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
457 const char *name = gh->name;
458 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
459 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
463 void write_typedef(type_t *type)
465 fprintf(header, "typedef ");
466 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
467 fprintf(header, ";\n");
470 int is_const_decl(const var_t *var)
473 /* strangely, MIDL accepts a const attribute on any pointer in the
474 * declaration to mean that data isn't being instantiated. this appears
475 * to be a bug, but there is no benefit to being incompatible with MIDL,
476 * so we'll do the same thing */
477 for (t = var->type; ; )
479 if (is_attr(t->attrs, ATTR_CONST))
488 void write_declaration(const var_t *v, int is_in_interface)
490 if (is_const_decl(v) && v->eval)
492 fprintf(header, "#define %s (", v->name);
493 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
494 fprintf(header, ")\n\n");
496 else if (v->type->type != RPC_FC_FUNCTION || !is_in_interface)
501 case STG_REGISTER: /* ignored */
504 fprintf(header, "static ");
507 fprintf(header, "extern ");
510 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
511 fprintf(header, ";\n\n");
515 void write_library(const typelib_t *typelib)
517 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
518 fprintf(header, "\n");
519 write_guid(header, "LIBID", typelib->name, uuid);
520 fprintf(header, "\n");
524 const var_t* get_explicit_handle_var(const func_t* func)
531 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
532 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
538 const type_t* get_explicit_generic_handle_type(const var_t* var)
541 for (t = var->type; is_ptr(t); t = t->ref)
542 if (t->type != RPC_FC_BIND_PRIMITIVE && is_attr(t->attrs, ATTR_HANDLE))
547 const var_t* get_explicit_generic_handle_var(const func_t* func)
554 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
555 if (get_explicit_generic_handle_type(var))
561 const var_t* get_context_handle_var(const func_t* func)
568 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
569 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
575 int has_out_arg_or_return(const func_t *func)
579 if (!is_void(get_func_return_type(func)))
585 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
586 if (is_attr(var->attrs, ATTR_OUT))
593 /********** INTERFACES **********/
595 int is_object(const attr_list_t *list)
598 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
599 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
603 int is_local(const attr_list_t *a)
605 return is_attr(a, ATTR_LOCAL);
608 const var_t *is_callas(const attr_list_t *a)
610 return get_attrp(a, ATTR_CALLAS);
613 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
617 if (iface->ref) write_method_macro(header, iface->ref, name);
619 if (!iface->funcs) return;
621 fprintf(header, "/*** %s methods ***/\n", iface->name);
622 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
624 var_t *def = cur->def;
625 if (!is_callas(def->attrs)) {
628 fprintf(header, "#define %s_%s(This", name, get_name(def));
630 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
631 fprintf(header, ",%s", arg->name);
632 fprintf(header, ") ");
634 fprintf(header, "(This)->lpVtbl->%s(This", get_name(def));
636 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
637 fprintf(header, ",%s", arg->name);
638 fprintf(header, ")\n");
643 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
654 fprintf(h, "%s* This", name);
657 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
664 else fprintf(h, ",");
666 write_type_decl(h, arg->type, "%s", arg->name);
669 if (do_indent) indentation--;
672 static void write_cpp_method_def(FILE *header, const type_t *iface)
676 if (!iface->funcs) return;
678 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
680 var_t *def = cur->def;
681 if (!is_callas(def->attrs)) {
682 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
683 if (!callconv) callconv = "";
685 fprintf(header, "virtual ");
686 write_type_decl_left(header, get_func_return_type(cur));
687 fprintf(header, " %s %s(\n", callconv, get_name(def));
688 write_args(header, cur->args, iface->name, 2, TRUE);
689 fprintf(header, ") = 0;\n");
690 fprintf(header, "\n");
695 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
699 if (iface->ref) do_write_c_method_def(header, iface->ref, name);
701 if (!iface->funcs) return;
703 fprintf(header, "/*** %s methods ***/\n", iface->name);
704 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
706 const var_t *def = cur->def;
707 if (!is_callas(def->attrs)) {
708 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
709 if (!callconv) callconv = "";
711 write_type_decl_left(header, get_func_return_type(cur));
712 fprintf(header, " (%s *%s)(\n", callconv, get_name(def));
713 write_args(header, cur->args, name, 1, TRUE);
714 fprintf(header, ");\n");
715 fprintf(header, "\n");
720 static void write_c_method_def(FILE *header, const type_t *iface)
722 do_write_c_method_def(header, iface, iface->name);
725 static void write_c_disp_method_def(FILE *header, const type_t *iface)
727 do_write_c_method_def(header, iface->ref, iface->name);
730 static void write_method_proto(FILE *header, const type_t *iface)
734 if (!iface->funcs) return;
735 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
737 const var_t *def = cur->def;
739 if (!is_local(def->attrs)) {
740 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
741 if (!callconv) callconv = "";
742 /* proxy prototype */
743 write_type_decl_left(header, get_func_return_type(cur));
744 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(def));
745 write_args(header, cur->args, iface->name, 1, TRUE);
746 fprintf(header, ");\n");
748 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(def));
749 fprintf(header, " IRpcStubBuffer* This,\n");
750 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
751 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
752 fprintf(header, " DWORD* pdwStubPhase);\n");
757 void write_locals(FILE *fp, const type_t *iface, int body)
759 static const char comment[]
760 = "/* WIDL-generated stub. You must provide an implementation for this. */";
761 const func_list_t *funcs = iface->funcs;
764 if (!is_object(iface->attrs) || !funcs)
767 LIST_FOR_EACH_ENTRY(cur, funcs, const func_t, entry) {
768 const var_t *def = cur->def;
769 const var_t *cas = is_callas(def->attrs);
773 LIST_FOR_EACH_ENTRY(m, iface->funcs, const func_t, entry)
774 if (!strcmp(m->def->name, cas->name))
776 if (&m->entry != iface->funcs) {
777 const var_t *mdef = m->def;
778 /* proxy prototype - use local prototype */
779 write_type_decl_left(fp, get_func_return_type(m));
780 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(mdef));
781 write_args(fp, m->args, iface->name, 1, TRUE);
784 type_t *rt = get_func_return_type(m);
785 fprintf(fp, "\n{\n");
786 fprintf(fp, " %s\n", comment);
787 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
788 fprintf(fp, " return E_NOTIMPL;\n");
791 write_type_decl(fp, rt, "rv");
793 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
794 fprintf(fp, " return rv;\n");
796 fprintf(fp, "}\n\n");
800 /* stub prototype - use remotable prototype */
801 write_type_decl_left(fp, get_func_return_type(cur));
802 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(mdef));
803 write_args(fp, cur->args, iface->name, 1, TRUE);
806 /* Remotable methods must all return HRESULTs. */
807 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
812 error_loc("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
817 static void write_function_proto(FILE *header, const type_t *iface, const func_t *fun, const char *prefix)
819 var_t *def = fun->def;
820 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
822 /* FIXME: do we need to handle call_as? */
823 write_type_decl_left(header, get_func_return_type(fun));
824 fprintf(header, " ");
825 if (callconv) fprintf(header, "%s ", callconv);
826 fprintf(header, "%s%s(\n", prefix, get_name(def));
828 write_args(header, fun->args, iface->name, 0, TRUE);
830 fprintf(header, " void");
831 fprintf(header, ");\n\n");
834 static void write_function_protos(FILE *header, const type_t *iface)
837 int prefixes_differ = strcmp(prefix_client, prefix_server);
839 if (!iface->funcs) return;
840 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
842 if (prefixes_differ) {
843 fprintf(header, "/* client prototype */\n");
844 write_function_proto(header, iface, cur, prefix_client);
845 fprintf(header, "/* server prototype */\n");
847 write_function_proto(header, iface, cur, prefix_server);
851 void write_forward(type_t *iface)
853 /* C/C++ forwards should only be written for object interfaces, so if we
854 * have a full definition we only write one if we find [object] among the
855 * attributes - however, if we don't have a full definition at this point
856 * (i.e. this is an IDL forward), then we also assume that it is an object
857 * interface, since non-object interfaces shouldn't need forwards */
858 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
859 && !iface->written) {
860 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
861 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
862 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
863 fprintf(header, "#endif\n\n" );
864 iface->written = TRUE;
868 static void write_iface_guid(FILE *header, const type_t *iface)
870 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
871 write_guid(header, "IID", iface->name, uuid);
874 static void write_dispiface_guid(FILE *header, const type_t *iface)
876 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
877 write_guid(header, "DIID", iface->name, uuid);
880 static void write_coclass_guid(FILE *header, const type_t *cocl)
882 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
883 write_guid(header, "CLSID", cocl->name, uuid);
886 static void write_com_interface_start(FILE *header, const type_t *iface)
888 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
889 fprintf(header, "/*****************************************************************************\n");
890 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
891 fprintf(header, " */\n");
892 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
893 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
896 static void write_com_interface_end(FILE *header, type_t *iface)
898 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
900 write_dispiface_guid(header, iface);
902 write_iface_guid(header, iface);
904 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
907 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
908 fprintf(header, "{\n");
912 fprintf(header, "interface %s\n", iface->name);
913 fprintf(header, "{\n");
914 fprintf(header, " BEGIN_INTERFACE\n");
915 fprintf(header, "\n");
917 /* dispinterfaces don't have real functions, so don't write C++ functions for
922 write_cpp_method_def(header, iface);
926 fprintf(header, " END_INTERFACE\n");
927 fprintf(header, "};\n");
928 fprintf(header, "#else\n");
930 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
932 fprintf(header, " BEGIN_INTERFACE\n");
933 fprintf(header, "\n");
935 write_c_disp_method_def(header, iface);
937 write_c_method_def(header, iface);
939 fprintf(header, " END_INTERFACE\n");
940 fprintf(header, "} %sVtbl;\n", iface->name);
941 fprintf(header, "interface %s {\n", iface->name);
942 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
943 fprintf(header, "};\n");
944 fprintf(header, "\n");
945 fprintf(header, "#ifdef COBJMACROS\n");
946 /* dispinterfaces don't have real functions, so don't write macros for them,
947 * only for the interface this interface inherits from, i.e. IDispatch */
948 write_method_macro(header, dispinterface ? iface->ref : iface, iface->name);
949 fprintf(header, "#endif\n");
950 fprintf(header, "\n");
951 fprintf(header, "#endif\n");
952 fprintf(header, "\n");
953 /* dispinterfaces don't have real functions, so don't write prototypes for
957 write_method_proto(header, iface);
958 write_locals(header, iface, FALSE);
959 fprintf(header, "\n");
961 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
964 static void write_rpc_interface_start(FILE *header, const type_t *iface)
966 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
967 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
968 static int allocate_written = 0;
970 if (!allocate_written)
972 allocate_written = 1;
973 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
974 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
977 fprintf(header, "/*****************************************************************************\n");
978 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
979 fprintf(header, " */\n");
980 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
981 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
982 if (var) fprintf(header, "extern handle_t %s;\n", var);
985 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
986 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
990 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
991 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
992 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
993 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
997 static void write_rpc_interface_end(FILE *header, const type_t *iface)
999 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1002 void write_interface(type_t *iface)
1004 if (is_attr(iface->attrs, ATTR_DISPINTERFACE) || is_object(iface->attrs))
1006 write_com_interface_start(header, iface);
1007 write_com_interface_end(header, iface);
1011 write_rpc_interface_start(header, iface);
1012 write_function_protos(header, iface);
1013 write_rpc_interface_end(header, iface);
1017 void write_coclass(type_t *cocl)
1019 fprintf(header, "/*****************************************************************************\n");
1020 fprintf(header, " * %s coclass\n", cocl->name);
1021 fprintf(header, " */\n\n");
1022 write_coclass_guid(header, cocl);
1023 fprintf(header, "\n");
1026 void write_coclass_forward(type_t *cocl)
1028 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1029 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1030 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1031 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1034 void write_import(const char *fname)
1038 hname = dup_basename(fname, ".idl");
1039 p = hname + strlen(hname) - 2;
1040 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1042 fprintf(header, "#include <%s>\n", hname);