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 const char *get_name(const var_t *v)
135 static char buffer[256];
137 if (is_attr( v->attrs, ATTR_PROPGET ))
138 strcpy( buffer, "get_" );
139 else if (is_attr( v->attrs, ATTR_PROPPUT ))
140 strcpy( buffer, "put_" );
141 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
142 strcpy( buffer, "putref_" );
145 strcat( buffer, v->name );
149 static void write_field(FILE *h, var_t *v)
153 const char *name = v->name;
155 switch (v->type->type) {
157 case RPC_FC_CVSTRUCT:
158 case RPC_FC_CPSTRUCT:
161 case RPC_FC_BOGUS_STRUCT:
162 case RPC_FC_ENCAPSULATED_UNION:
163 name = "DUMMYSTRUCTNAME";
165 case RPC_FC_NON_ENCAPSULATED_UNION:
166 name = "DUMMYUNIONNAME";
174 write_type_def_or_decl(h, v->type, TRUE, "%s", name);
179 static void write_fields(FILE *h, var_list_t *fields)
183 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
186 static void write_enums(FILE *h, var_list_t *enums)
190 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
194 fprintf(h, "%s", get_name(v));
197 write_expr(h, v->eval, 0, 1, NULL, NULL);
200 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
205 int needs_space_after(type_t *t)
207 return (t->kind == TKIND_ALIAS
208 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
211 void write_type_left(FILE *h, type_t *t, int declonly)
215 if (is_attr(t->attrs, ATTR_CONST) &&
216 (t->kind == TKIND_ALIAS || t->declarray || !is_ptr(t)))
217 fprintf(h, "const ");
219 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
220 else if (t->declarray) write_type_left(h, t->ref, declonly);
222 if (t->sign > 0) fprintf(h, "signed ");
223 else if (t->sign < 0) fprintf(h, "unsigned ");
227 if (!declonly && t->defined && !t->written && !t->ignore) {
228 if (t->name) fprintf(h, "enum %s {\n", t->name);
229 else fprintf(h, "enum {\n");
232 write_enums(h, t->fields_or_args);
236 else fprintf(h, "enum %s", t->name ? t->name : "");
239 case RPC_FC_CVSTRUCT:
240 case RPC_FC_CPSTRUCT:
243 case RPC_FC_BOGUS_STRUCT:
244 case RPC_FC_ENCAPSULATED_UNION:
245 if (!declonly && t->defined && !t->written && !t->ignore) {
246 if (t->name) fprintf(h, "struct %s {\n", t->name);
247 else fprintf(h, "struct {\n");
250 write_fields(h, t->fields_or_args);
254 else fprintf(h, "struct %s", t->name ? t->name : "");
256 case RPC_FC_NON_ENCAPSULATED_UNION:
257 if (!declonly && t->defined && !t->written && !t->ignore) {
258 if (t->name) fprintf(h, "union %s {\n", t->name);
259 else fprintf(h, "union {\n");
262 write_fields(h, t->fields_or_args);
266 else fprintf(h, "union %s", t->name ? t->name : "");
274 case RPC_FC_BOGUS_ARRAY:
275 write_type_left(h, t->ref, declonly);
276 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
277 if (is_ptr(t) && is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
280 fprintf(h, "%s", t->name);
285 void write_type_right(FILE *h, type_t *t, int is_field)
290 if (is_conformant_array(t)) {
291 fprintf(h, "[%s]", is_field ? "1" : "");
294 for ( ; t->declarray; t = t->ref)
295 fprintf(h, "[%lu]", t->dim);
299 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
300 const char *fmt, va_list args)
307 for (pt = t; is_ptr(pt); pt = pt->ref, ptr_level++)
310 if (pt->type == RPC_FC_FUNCTION) {
312 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
313 if (!callconv) callconv = "";
314 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
315 write_type_left(h, pt->ref, declonly);
317 if (ptr_level) fputc('(', h);
318 fprintf(h, "%s ", callconv);
319 for (i = 0; i < ptr_level; i++)
322 write_type_left(h, t, declonly);
324 if (needs_space_after(t))
326 vfprintf(h, fmt, args);
328 if (pt->type == RPC_FC_FUNCTION) {
329 if (ptr_level) fputc(')', h);
331 write_args(h, pt->fields_or_args, NULL, 0, FALSE);
334 write_type_right(h, t, is_field);
337 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
341 write_type_v(f, t, field, FALSE, fmt, args);
345 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
349 write_type_v(f, t, FALSE, TRUE, fmt, args);
353 void write_type_decl_left(FILE *f, type_t *t)
355 write_type_left(f, t, TRUE);
358 static int user_type_registered(const char *name)
361 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
362 if (!strcmp(name, ut->name))
367 static int context_handle_registered(const char *name)
369 context_handle_t *ch;
370 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
371 if (!strcmp(name, ch->name))
376 static int generic_handle_registered(const char *name)
378 generic_handle_t *gh;
379 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
380 if (!strcmp(name, gh->name))
385 /* check for types which require additional prototypes to be generated in the
387 void check_for_additional_prototype_types(const var_list_t *list)
392 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
395 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
396 const char *name = type->name;
397 if (type->user_types_registered) continue;
398 type->user_types_registered = 1;
399 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
400 if (!context_handle_registered(name))
402 context_handle_t *ch = xmalloc(sizeof(*ch));
403 ch->name = xstrdup(name);
404 list_add_tail(&context_handle_list, &ch->entry);
406 /* don't carry on parsing fields within this type */
409 if (type->type != RPC_FC_BIND_PRIMITIVE && is_attr(type->attrs, ATTR_HANDLE)) {
410 if (!generic_handle_registered(name))
412 generic_handle_t *gh = xmalloc(sizeof(*gh));
413 gh->name = xstrdup(name);
414 list_add_tail(&generic_handle_list, &gh->entry);
416 /* don't carry on parsing fields within this type */
419 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
420 if (!user_type_registered(name))
422 user_type_t *ut = xmalloc(sizeof *ut);
423 ut->name = xstrdup(name);
424 list_add_tail(&user_type_list, &ut->entry);
426 /* don't carry on parsing fields within this type as we are already
427 * using a wire marshaled type */
432 check_for_additional_prototype_types(type->fields_or_args);
438 void write_user_types(void)
441 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
443 const char *name = ut->name;
444 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
445 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
446 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
447 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
451 void write_context_handle_rundowns(void)
453 context_handle_t *ch;
454 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
456 const char *name = ch->name;
457 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
461 void write_generic_handle_routines(void)
463 generic_handle_t *gh;
464 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
466 const char *name = gh->name;
467 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
468 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
472 void write_typedef(type_t *type)
474 fprintf(header, "typedef ");
475 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
476 fprintf(header, ";\n");
479 int is_const_decl(const var_t *var)
482 /* strangely, MIDL accepts a const attribute on any pointer in the
483 * declaration to mean that data isn't being instantiated. this appears
484 * to be a bug, but there is no benefit to being incompatible with MIDL,
485 * so we'll do the same thing */
486 for (t = var->type; ; )
488 if (is_attr(t->attrs, ATTR_CONST))
497 void write_declaration(const var_t *v, int is_in_interface)
499 if (is_const_decl(v) && v->eval)
501 fprintf(header, "#define %s (", v->name);
502 write_expr(header, v->eval, 0, 1, NULL, NULL);
503 fprintf(header, ")\n\n");
505 else if (v->type->type != RPC_FC_FUNCTION || !is_in_interface)
510 case STG_REGISTER: /* ignored */
513 fprintf(header, "static ");
516 fprintf(header, "extern ");
519 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
520 fprintf(header, ";\n\n");
524 void write_library(const typelib_t *typelib)
526 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
527 fprintf(header, "\n");
528 write_guid(header, "LIBID", typelib->name, uuid);
529 fprintf(header, "\n");
533 const var_t* get_explicit_handle_var(const func_t* func)
540 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
541 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
547 const type_t* get_explicit_generic_handle_type(const var_t* var)
550 for (t = var->type; is_ptr(t); t = t->ref)
551 if (t->type != RPC_FC_BIND_PRIMITIVE && is_attr(t->attrs, ATTR_HANDLE))
556 const var_t* get_explicit_generic_handle_var(const func_t* func)
563 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
564 if (get_explicit_generic_handle_type(var))
570 const var_t* get_context_handle_var(const func_t* func)
577 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
578 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
584 int has_out_arg_or_return(const func_t *func)
588 if (!is_void(get_func_return_type(func)))
594 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
595 if (is_attr(var->attrs, ATTR_OUT))
602 /********** INTERFACES **********/
604 int is_object(const attr_list_t *list)
607 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
608 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
612 int is_local(const attr_list_t *a)
614 return is_attr(a, ATTR_LOCAL);
617 const var_t *is_callas(const attr_list_t *a)
619 return get_attrp(a, ATTR_CALLAS);
622 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
626 if (iface->ref) write_method_macro(header, iface->ref, name);
628 if (!iface->funcs) return;
630 fprintf(header, "/*** %s methods ***/\n", iface->name);
631 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
633 var_t *def = cur->def;
634 if (!is_callas(def->attrs)) {
637 fprintf(header, "#define %s_%s(This", name, get_name(def));
639 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
640 fprintf(header, ",%s", arg->name);
641 fprintf(header, ") ");
643 fprintf(header, "(This)->lpVtbl->%s(This", get_name(def));
645 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
646 fprintf(header, ",%s", arg->name);
647 fprintf(header, ")\n");
652 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
663 fprintf(h, "%s* This", name);
666 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
673 else fprintf(h, ",");
675 write_type_decl(h, arg->type, "%s", arg->name);
678 if (do_indent) indentation--;
681 static void write_cpp_method_def(FILE *header, const type_t *iface)
685 if (!iface->funcs) return;
687 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
689 var_t *def = cur->def;
690 if (!is_callas(def->attrs)) {
691 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
692 if (!callconv) callconv = "";
694 fprintf(header, "virtual ");
695 write_type_decl_left(header, get_func_return_type(cur));
696 fprintf(header, " %s %s(\n", callconv, get_name(def));
697 write_args(header, cur->args, iface->name, 2, TRUE);
698 fprintf(header, ") = 0;\n");
699 fprintf(header, "\n");
704 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
708 if (iface->ref) do_write_c_method_def(header, iface->ref, name);
710 if (!iface->funcs) return;
712 fprintf(header, "/*** %s methods ***/\n", iface->name);
713 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
715 const var_t *def = cur->def;
716 if (!is_callas(def->attrs)) {
717 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
718 if (!callconv) callconv = "";
720 write_type_decl_left(header, get_func_return_type(cur));
721 fprintf(header, " (%s *%s)(\n", callconv, get_name(def));
722 write_args(header, cur->args, name, 1, TRUE);
723 fprintf(header, ");\n");
724 fprintf(header, "\n");
729 static void write_c_method_def(FILE *header, const type_t *iface)
731 do_write_c_method_def(header, iface, iface->name);
734 static void write_c_disp_method_def(FILE *header, const type_t *iface)
736 do_write_c_method_def(header, iface->ref, iface->name);
739 static void write_method_proto(FILE *header, const type_t *iface)
743 if (!iface->funcs) return;
744 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
746 const var_t *def = cur->def;
748 if (!is_local(def->attrs)) {
749 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
750 if (!callconv) callconv = "";
751 /* proxy prototype */
752 write_type_decl_left(header, get_func_return_type(cur));
753 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(def));
754 write_args(header, cur->args, iface->name, 1, TRUE);
755 fprintf(header, ");\n");
757 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(def));
758 fprintf(header, " IRpcStubBuffer* This,\n");
759 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
760 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
761 fprintf(header, " DWORD* pdwStubPhase);\n");
766 void write_locals(FILE *fp, const type_t *iface, int body)
768 static const char comment[]
769 = "/* WIDL-generated stub. You must provide an implementation for this. */";
770 const func_list_t *funcs = iface->funcs;
773 if (!is_object(iface->attrs) || !funcs)
776 LIST_FOR_EACH_ENTRY(cur, funcs, const func_t, entry) {
777 const var_t *def = cur->def;
778 const var_t *cas = is_callas(def->attrs);
782 LIST_FOR_EACH_ENTRY(m, iface->funcs, const func_t, entry)
783 if (!strcmp(m->def->name, cas->name))
785 if (&m->entry != iface->funcs) {
786 const var_t *mdef = m->def;
787 /* proxy prototype - use local prototype */
788 write_type_decl_left(fp, get_func_return_type(m));
789 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(mdef));
790 write_args(fp, m->args, iface->name, 1, TRUE);
793 type_t *rt = get_func_return_type(m);
794 fprintf(fp, "\n{\n");
795 fprintf(fp, " %s\n", comment);
796 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
797 fprintf(fp, " return E_NOTIMPL;\n");
800 write_type_decl(fp, rt, "rv");
802 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
803 fprintf(fp, " return rv;\n");
805 fprintf(fp, "}\n\n");
809 /* stub prototype - use remotable prototype */
810 write_type_decl_left(fp, get_func_return_type(cur));
811 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(mdef));
812 write_args(fp, cur->args, iface->name, 1, TRUE);
815 /* Remotable methods must all return HRESULTs. */
816 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
821 error_loc("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
826 static void write_function_proto(FILE *header, const type_t *iface, const func_t *fun, const char *prefix)
828 var_t *def = fun->def;
829 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
831 /* FIXME: do we need to handle call_as? */
832 write_type_decl_left(header, get_func_return_type(fun));
833 fprintf(header, " ");
834 if (callconv) fprintf(header, "%s ", callconv);
835 fprintf(header, "%s%s(\n", prefix, get_name(def));
837 write_args(header, fun->args, iface->name, 0, TRUE);
839 fprintf(header, " void");
840 fprintf(header, ");\n\n");
843 static void write_function_protos(FILE *header, const type_t *iface)
846 int prefixes_differ = strcmp(prefix_client, prefix_server);
848 if (!iface->funcs) return;
849 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
851 if (prefixes_differ) {
852 fprintf(header, "/* client prototype */\n");
853 write_function_proto(header, iface, cur, prefix_client);
854 fprintf(header, "/* server prototype */\n");
856 write_function_proto(header, iface, cur, prefix_server);
860 void write_forward(type_t *iface)
862 /* C/C++ forwards should only be written for object interfaces, so if we
863 * have a full definition we only write one if we find [object] among the
864 * attributes - however, if we don't have a full definition at this point
865 * (i.e. this is an IDL forward), then we also assume that it is an object
866 * interface, since non-object interfaces shouldn't need forwards */
867 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
868 && !iface->written) {
869 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
870 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
871 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
872 fprintf(header, "#endif\n\n" );
873 iface->written = TRUE;
877 static void write_iface_guid(FILE *header, const type_t *iface)
879 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
880 write_guid(header, "IID", iface->name, uuid);
883 static void write_dispiface_guid(FILE *header, const type_t *iface)
885 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
886 write_guid(header, "DIID", iface->name, uuid);
889 static void write_coclass_guid(FILE *header, const type_t *cocl)
891 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
892 write_guid(header, "CLSID", cocl->name, uuid);
895 static void write_com_interface_start(FILE *header, const type_t *iface)
897 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
898 fprintf(header, "/*****************************************************************************\n");
899 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
900 fprintf(header, " */\n");
901 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
902 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
905 static void write_com_interface_end(FILE *header, type_t *iface)
907 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
909 write_dispiface_guid(header, iface);
911 write_iface_guid(header, iface);
913 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
916 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
917 fprintf(header, "{\n");
921 fprintf(header, "interface %s\n", iface->name);
922 fprintf(header, "{\n");
923 fprintf(header, " BEGIN_INTERFACE\n");
924 fprintf(header, "\n");
926 /* dispinterfaces don't have real functions, so don't write C++ functions for
931 write_cpp_method_def(header, iface);
935 fprintf(header, " END_INTERFACE\n");
936 fprintf(header, "};\n");
937 fprintf(header, "#else\n");
939 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
941 fprintf(header, " BEGIN_INTERFACE\n");
942 fprintf(header, "\n");
944 write_c_disp_method_def(header, iface);
946 write_c_method_def(header, iface);
948 fprintf(header, " END_INTERFACE\n");
949 fprintf(header, "} %sVtbl;\n", iface->name);
950 fprintf(header, "interface %s {\n", iface->name);
951 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
952 fprintf(header, "};\n");
953 fprintf(header, "\n");
954 fprintf(header, "#ifdef COBJMACROS\n");
955 /* dispinterfaces don't have real functions, so don't write macros for them,
956 * only for the interface this interface inherits from, i.e. IDispatch */
957 write_method_macro(header, dispinterface ? iface->ref : iface, iface->name);
958 fprintf(header, "#endif\n");
959 fprintf(header, "\n");
960 fprintf(header, "#endif\n");
961 fprintf(header, "\n");
962 /* dispinterfaces don't have real functions, so don't write prototypes for
966 write_method_proto(header, iface);
967 write_locals(header, iface, FALSE);
968 fprintf(header, "\n");
970 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
973 static void write_rpc_interface_start(FILE *header, const type_t *iface)
975 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
976 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
977 static int allocate_written = 0;
979 if (!allocate_written)
981 allocate_written = 1;
982 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
983 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
986 fprintf(header, "/*****************************************************************************\n");
987 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
988 fprintf(header, " */\n");
989 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
990 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
991 if (var) fprintf(header, "extern handle_t %s;\n", var);
994 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
995 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
999 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1000 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1001 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1002 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1006 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1008 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1011 void write_interface(type_t *iface)
1013 if (is_attr(iface->attrs, ATTR_DISPINTERFACE) || is_object(iface->attrs))
1015 write_com_interface_start(header, iface);
1016 write_com_interface_end(header, iface);
1020 write_rpc_interface_start(header, iface);
1021 write_function_protos(header, iface);
1022 write_rpc_interface_end(header, iface);
1026 void write_coclass(type_t *cocl)
1028 fprintf(header, "/*****************************************************************************\n");
1029 fprintf(header, " * %s coclass\n", cocl->name);
1030 fprintf(header, " */\n\n");
1031 write_coclass_guid(header, cocl);
1032 fprintf(header, "\n");
1035 void write_coclass_forward(type_t *cocl)
1037 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1038 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1039 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1040 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1043 void write_import(const char *fname)
1047 hname = dup_basename(fname, ".idl");
1048 p = hname + strlen(hname) - 2;
1049 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1051 fprintf(header, "#include <%s>\n", hname);