4 * Copyright 2002 Ove Kaaven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
39 static int indentation = 0;
40 static int is_object_interface = 0;
41 user_type_list_t user_type_list = LIST_INIT(user_type_list);
42 static context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
43 generic_handle_list_t generic_handle_list = LIST_INIT(generic_handle_list);
45 static void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name);
47 static void indent(FILE *h, int delta)
50 if (delta < 0) indentation += delta;
51 for (c=0; c<indentation; c++) fprintf(h, " ");
52 if (delta > 0) indentation += delta;
55 int is_ptrchain_attr(const var_t *var, enum attr_type t)
57 if (is_attr(var->attrs, t))
61 type_t *type = var->type;
64 if (is_attr(type->attrs, t))
66 else if (type_is_alias(type))
67 type = type_alias_get_aliasee(type);
68 else if (is_ptr(type))
69 type = type_pointer_get_ref(type);
75 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
77 const type_t *t = type;
80 if (is_attr(t->attrs, attr))
82 else if (type_is_alias(t))
83 t = type_alias_get_aliasee(t);
88 int is_attr(const attr_list_t *list, enum attr_type t)
91 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
92 if (attr->type == t) return 1;
96 void *get_attrp(const attr_list_t *list, enum attr_type t)
99 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
100 if (attr->type == t) return attr->u.pval;
104 unsigned int get_attrv(const attr_list_t *list, enum attr_type t)
107 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
108 if (attr->type == t) return attr->u.ival;
112 int is_void(const type_t *t)
114 return type_get_type(t) == TYPE_VOID;
117 int is_conformant_array(const type_t *t)
119 return is_array(t) && type_array_has_conformance(t);
122 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
125 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
126 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
127 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
128 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
129 uuid->Data4[6], uuid->Data4[7]);
132 const char *get_name(const var_t *v)
134 static char buffer[256];
136 if (is_attr( v->attrs, ATTR_PROPGET ))
137 strcpy( buffer, "get_" );
138 else if (is_attr( v->attrs, ATTR_PROPPUT ))
139 strcpy( buffer, "put_" );
140 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
141 strcpy( buffer, "putref_" );
144 strcat( buffer, v->name );
148 static void write_field(FILE *h, var_t *v)
153 write_type_def_or_decl(h, v->type, TRUE, v->name);
158 static void write_fields(FILE *h, var_list_t *fields)
162 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
165 static void write_enums(FILE *h, var_list_t *enums)
169 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
173 fprintf(h, "%s", get_name(v));
176 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
179 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
184 int needs_space_after(type_t *t)
186 return (type_is_alias(t) ||
187 (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
190 void write_type_left(FILE *h, type_t *t, int declonly)
194 if (is_attr(t->attrs, ATTR_CONST) &&
195 (type_is_alias(t) || !is_ptr(t)))
196 fprintf(h, "const ");
198 if (type_is_alias(t)) fprintf(h, "%s", t->name);
200 switch (type_get_type_detect_alias(t)) {
202 if (!declonly && t->defined && !t->written) {
203 if (t->name) fprintf(h, "enum %s {\n", t->name);
204 else fprintf(h, "enum {\n");
207 write_enums(h, type_enum_get_values(t));
211 else fprintf(h, "enum %s", t->name ? t->name : "");
214 case TYPE_ENCAPSULATED_UNION:
215 if (!declonly && t->defined && !t->written) {
216 if (t->name) fprintf(h, "struct %s {\n", t->name);
217 else fprintf(h, "struct {\n");
220 if (type_get_type(t) != TYPE_STRUCT)
221 write_fields(h, type_encapsulated_union_get_fields(t));
223 write_fields(h, type_struct_get_fields(t));
227 else fprintf(h, "struct %s", t->name ? t->name : "");
230 if (!declonly && t->defined && !t->written) {
231 if (t->name) fprintf(h, "union %s {\n", t->name);
232 else fprintf(h, "union {\n");
235 write_fields(h, type_union_get_cases(t));
239 else fprintf(h, "union %s", t->name ? t->name : "");
242 write_type_left(h, type_pointer_get_ref(t), declonly);
243 fprintf(h, "%s*", needs_space_after(type_pointer_get_ref(t)) ? " " : "");
244 if (is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
247 if (t->name && type_array_is_decl_as_ptr(t))
248 fprintf(h, "%s", t->name);
251 write_type_left(h, type_array_get_element(t), declonly);
252 if (type_array_is_decl_as_ptr(t))
253 fprintf(h, "%s*", needs_space_after(type_array_get_element(t)) ? " " : "");
257 if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
258 type_basic_get_type(t) != TYPE_BASIC_INT64 &&
259 type_basic_get_type(t) != TYPE_BASIC_HYPER)
261 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
262 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
264 switch (type_basic_get_type(t))
266 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
267 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
268 case TYPE_BASIC_INT: fprintf(h, "int"); break;
269 case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
270 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
271 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
272 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
273 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
274 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
275 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
276 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
277 case TYPE_BASIC_INT32:
278 if (type_basic_get_sign(t) > 0)
283 case TYPE_BASIC_INT64:
284 if (type_basic_get_sign(t) > 0)
285 fprintf(h, "UINT64");
289 case TYPE_BASIC_HYPER:
290 if (type_basic_get_sign(t) > 0)
291 fprintf(h, "MIDL_uhyper");
300 fprintf(h, "%s", t->name);
306 write_type_left(h, type_bitfield_get_field(t), declonly);
310 /* handled elsewhere */
317 void write_type_right(FILE *h, type_t *t, int is_field)
321 switch (type_get_type(t))
324 if (!type_array_is_decl_as_ptr(t))
326 if (is_conformant_array(t))
328 fprintf(h, "[%s]", is_field ? "1" : "");
329 t = type_array_get_element(t);
332 type_get_type(t) == TYPE_ARRAY && !type_array_is_decl_as_ptr(t);
333 t = type_array_get_element(t))
334 fprintf(h, "[%u]", type_array_get_dim(t));
338 fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
344 case TYPE_ENCAPSULATED_UNION:
356 static void write_type_v(FILE *h, type_t *t, int is_field, int declonly, const char *name)
364 for (pt = t; is_ptr(pt); pt = type_pointer_get_ref(pt), ptr_level++)
367 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
369 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
370 if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
371 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
372 write_type_left(h, type_function_get_rettype(pt), declonly);
374 if (ptr_level) fputc('(', h);
375 if (callconv) fprintf(h, "%s ", callconv);
376 for (i = 0; i < ptr_level; i++)
379 write_type_left(h, t, declonly);
382 if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
385 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
386 const var_list_t *args = type_function_get_args(pt);
388 if (ptr_level) fputc(')', h);
391 write_args(h, args, NULL, 0, FALSE);
396 write_type_right(h, t, is_field);
400 static void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name)
402 write_type_v(f, t, field, FALSE, name);
405 void write_type_decl(FILE *f, type_t *t, const char *name)
407 write_type_v(f, t, FALSE, TRUE, name);
410 void write_type_decl_left(FILE *f, type_t *t)
412 write_type_left(f, t, TRUE);
415 static int user_type_registered(const char *name)
418 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
419 if (!strcmp(name, ut->name))
424 static int context_handle_registered(const char *name)
426 context_handle_t *ch;
427 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
428 if (!strcmp(name, ch->name))
433 static int generic_handle_registered(const char *name)
435 generic_handle_t *gh;
436 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
437 if (!strcmp(name, gh->name))
442 /* check for types which require additional prototypes to be generated in the
444 void check_for_additional_prototype_types(const var_list_t *list)
449 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
451 type_t *type = v->type;
454 const char *name = type->name;
455 if (type->user_types_registered) break;
456 type->user_types_registered = 1;
457 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
458 if (!context_handle_registered(name))
460 context_handle_t *ch = xmalloc(sizeof(*ch));
461 ch->name = xstrdup(name);
462 list_add_tail(&context_handle_list, &ch->entry);
464 /* don't carry on parsing fields within this type */
467 if ((type_get_type(type) != TYPE_BASIC ||
468 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
469 is_attr(type->attrs, ATTR_HANDLE)) {
470 if (!generic_handle_registered(name))
472 generic_handle_t *gh = xmalloc(sizeof(*gh));
473 gh->name = xstrdup(name);
474 list_add_tail(&generic_handle_list, &gh->entry);
476 /* don't carry on parsing fields within this type */
479 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
480 if (!user_type_registered(name))
482 user_type_t *ut = xmalloc(sizeof *ut);
483 ut->name = xstrdup(name);
484 list_add_tail(&user_type_list, &ut->entry);
486 /* don't carry on parsing fields within this type as we are already
487 * using a wire marshaled type */
490 else if (type_is_complete(type))
493 switch (type_get_type_detect_alias(type))
496 vars = type_enum_get_values(type);
499 vars = type_struct_get_fields(type);
502 vars = type_union_get_cases(type);
508 check_for_additional_prototype_types(vars);
511 if (type_is_alias(type))
512 type = type_alias_get_aliasee(type);
513 else if (is_ptr(type))
514 type = type_pointer_get_ref(type);
515 else if (is_array(type))
516 type = type_array_get_element(type);
523 static void write_user_types(FILE *header)
526 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
528 const char *name = ut->name;
529 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
530 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
531 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
532 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
536 static void write_context_handle_rundowns(FILE *header)
538 context_handle_t *ch;
539 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
541 const char *name = ch->name;
542 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
546 static void write_generic_handle_routines(FILE *header)
548 generic_handle_t *gh;
549 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
551 const char *name = gh->name;
552 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
553 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
557 static void write_typedef(FILE *header, type_t *type)
559 fprintf(header, "typedef ");
560 write_type_def_or_decl(header, type_alias_get_aliasee(type), FALSE, type->name);
561 fprintf(header, ";\n");
564 int is_const_decl(const var_t *var)
567 /* strangely, MIDL accepts a const attribute on any pointer in the
568 * declaration to mean that data isn't being instantiated. this appears
569 * to be a bug, but there is no benefit to being incompatible with MIDL,
570 * so we'll do the same thing */
571 for (t = var->type; ; )
573 if (is_attr(t->attrs, ATTR_CONST))
576 t = type_pointer_get_ref(t);
582 static void write_declaration(FILE *header, const var_t *v)
584 if (is_const_decl(v) && v->eval)
586 fprintf(header, "#define %s (", v->name);
587 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
588 fprintf(header, ")\n\n");
595 case STG_REGISTER: /* ignored */
598 fprintf(header, "static ");
601 fprintf(header, "extern ");
604 write_type_def_or_decl(header, v->type, FALSE, v->name);
605 fprintf(header, ";\n\n");
609 static void write_library(FILE *header, const typelib_t *typelib)
611 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
612 fprintf(header, "\n");
613 write_guid(header, "LIBID", typelib->name, uuid);
614 fprintf(header, "\n");
618 const var_t* get_explicit_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 )
627 const type_t *type = var->type;
628 if (type_get_type(type) == TYPE_BASIC && type_basic_get_type(type) == TYPE_BASIC_HANDLE)
635 const type_t* get_explicit_generic_handle_type(const var_t* var)
639 is_ptr(t) || type_is_alias(t);
640 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
641 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
642 is_attr(t->attrs, ATTR_HANDLE))
647 const var_t* get_explicit_generic_handle_var(const var_t *func)
651 if (!type_get_function_args(func->type))
654 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
655 if (get_explicit_generic_handle_type(var))
661 const var_t* get_context_handle_var(const var_t *func)
665 if (!type_get_function_args(func->type))
668 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
669 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
675 int has_out_arg_or_return(const var_t *func)
679 if (!is_void(type_function_get_rettype(func->type)))
682 if (!type_get_function_args(func->type))
685 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
686 if (is_attr(var->attrs, ATTR_OUT))
693 /********** INTERFACES **********/
695 int is_object(const type_t *iface)
698 if (type_is_defined(iface) && type_iface_get_inherit(iface))
700 if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
701 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
705 int is_local(const attr_list_t *a)
707 return is_attr(a, ATTR_LOCAL);
710 const var_t *is_callas(const attr_list_t *a)
712 return get_attrp(a, ATTR_CALLAS);
715 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
717 const statement_t *stmt;
720 if (type_iface_get_inherit(iface))
721 write_method_macro(header, type_iface_get_inherit(iface), name);
723 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
725 const var_t *func = stmt->u.var;
729 fprintf(header, "/*** %s methods ***/\n", iface->name);
733 if (!is_callas(func->attrs)) {
736 fprintf(header, "#define %s_%s(This", name, get_name(func));
737 if (type_get_function_args(func->type))
738 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
739 fprintf(header, ",%s", arg->name);
740 fprintf(header, ") ");
742 fprintf(header, "(This)->lpVtbl->%s(This", get_name(func));
743 if (type_get_function_args(func->type))
744 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
745 fprintf(header, ",%s", arg->name);
746 fprintf(header, ")\n");
751 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
762 fprintf(h, "%s* This", name);
765 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
772 else fprintf(h, ",");
774 write_type_decl(h, arg->type, arg->name);
777 if (do_indent) indentation--;
780 static void write_cpp_method_def(FILE *header, const type_t *iface)
782 const statement_t *stmt;
784 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
786 const var_t *func = stmt->u.var;
787 if (!is_callas(func->attrs)) {
788 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
789 if (!callconv) callconv = "STDMETHODCALLTYPE";
791 fprintf(header, "virtual ");
792 write_type_decl_left(header, type_function_get_rettype(func->type));
793 fprintf(header, " %s %s(\n", callconv, get_name(func));
794 write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
795 fprintf(header, ") = 0;\n");
796 fprintf(header, "\n");
801 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
803 const statement_t *stmt;
806 if (type_iface_get_inherit(iface))
807 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
809 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
811 const var_t *func = stmt->u.var;
814 fprintf(header, "/*** %s methods ***/\n", iface->name);
817 if (!is_callas(func->attrs)) {
818 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
819 if (!callconv) callconv = "STDMETHODCALLTYPE";
821 write_type_decl_left(header, type_function_get_rettype(func->type));
822 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
823 write_args(header, type_get_function_args(func->type), name, 1, TRUE);
824 fprintf(header, ");\n");
825 fprintf(header, "\n");
830 static void write_c_method_def(FILE *header, const type_t *iface)
832 do_write_c_method_def(header, iface, iface->name);
835 static void write_c_disp_method_def(FILE *header, const type_t *iface)
837 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->name);
840 static void write_method_proto(FILE *header, const type_t *iface)
842 const statement_t *stmt;
844 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
846 const var_t *func = stmt->u.var;
848 if (!is_local(func->attrs)) {
849 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
850 if (!callconv) callconv = "STDMETHODCALLTYPE";
851 /* proxy prototype */
852 write_type_decl_left(header, type_function_get_rettype(func->type));
853 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
854 write_args(header, type_get_function_args(func->type), iface->name, 1, TRUE);
855 fprintf(header, ");\n");
857 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
858 fprintf(header, " IRpcStubBuffer* This,\n");
859 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
860 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
861 fprintf(header, " DWORD* pdwStubPhase);\n");
866 static void write_locals(FILE *fp, const type_t *iface, int body)
868 static const char comment[]
869 = "/* WIDL-generated stub. You must provide an implementation for this. */";
870 const statement_t *stmt;
872 if (!is_object(iface))
875 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
876 const var_t *func = stmt->u.var;
877 const var_t *cas = is_callas(func->attrs);
880 const statement_t *stmt2 = NULL;
881 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
882 if (!strcmp(stmt2->u.var->name, cas->name))
884 if (&stmt2->entry != type_iface_get_stmts(iface)) {
885 const var_t *m = stmt2->u.var;
886 /* proxy prototype - use local prototype */
887 write_type_decl_left(fp, type_function_get_rettype(m->type));
888 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
889 write_args(fp, type_get_function_args(m->type), iface->name, 1, TRUE);
892 type_t *rt = type_function_get_rettype(m->type);
893 fprintf(fp, "\n{\n");
894 fprintf(fp, " %s\n", comment);
895 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
896 fprintf(fp, " return E_NOTIMPL;\n");
897 else if (type_get_type(rt) != TYPE_VOID) {
899 write_type_decl(fp, rt, "rv");
901 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
902 fprintf(fp, " return rv;\n");
904 fprintf(fp, "}\n\n");
908 /* stub prototype - use remotable prototype */
909 write_type_decl_left(fp, type_function_get_rettype(func->type));
910 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
911 write_args(fp, type_get_function_args(func->type), iface->name, 1, TRUE);
914 /* Remotable methods must all return HRESULTs. */
915 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
920 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
925 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
927 const statement_t *stmt;
928 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
930 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
931 write_locals(local_stubs, stmt->u.type, TRUE);
932 else if (stmt->type == STMT_LIBRARY)
933 write_local_stubs_stmts(local_stubs, stmt->u.lib->stmts);
937 void write_local_stubs(const statement_list_t *stmts)
941 if (!local_stubs_name) return;
943 local_stubs = fopen(local_stubs_name, "w");
945 error("Could not open %s for output\n", local_stubs_name);
948 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
949 fprintf(local_stubs, "#include <objbase.h>\n");
950 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
952 write_local_stubs_stmts(local_stubs, stmts);
957 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
959 const char *callconv = get_attrp(fun->type->attrs, ATTR_CALLCONV);
961 /* FIXME: do we need to handle call_as? */
962 write_type_decl_left(header, type_function_get_rettype(fun->type));
963 fprintf(header, " ");
964 if (callconv) fprintf(header, "%s ", callconv);
965 fprintf(header, "%s%s(\n", prefix, get_name(fun));
966 if (type_get_function_args(fun->type))
967 write_args(header, type_get_function_args(fun->type), iface->name, 0, TRUE);
969 fprintf(header, " void");
970 fprintf(header, ");\n\n");
973 static void write_forward(FILE *header, type_t *iface)
975 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
976 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
977 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
978 fprintf(header, "#endif\n\n" );
981 static void write_iface_guid(FILE *header, const type_t *iface)
983 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
984 write_guid(header, "IID", iface->name, uuid);
987 static void write_dispiface_guid(FILE *header, const type_t *iface)
989 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
990 write_guid(header, "DIID", iface->name, uuid);
993 static void write_coclass_guid(FILE *header, const type_t *cocl)
995 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
996 write_guid(header, "CLSID", cocl->name, uuid);
999 static void write_com_interface_start(FILE *header, const type_t *iface)
1001 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1002 fprintf(header, "/*****************************************************************************\n");
1003 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1004 fprintf(header, " */\n");
1005 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
1006 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
1009 static void write_com_interface_end(FILE *header, type_t *iface)
1011 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1013 write_dispiface_guid(header, iface);
1015 write_iface_guid(header, iface);
1017 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1018 if (type_iface_get_inherit(iface))
1020 fprintf(header, "interface %s : public %s\n", iface->name,
1021 type_iface_get_inherit(iface)->name);
1022 fprintf(header, "{\n");
1026 fprintf(header, "interface %s\n", iface->name);
1027 fprintf(header, "{\n");
1028 fprintf(header, " BEGIN_INTERFACE\n");
1029 fprintf(header, "\n");
1031 /* dispinterfaces don't have real functions, so don't write C++ functions for
1036 write_cpp_method_def(header, iface);
1039 if (!type_iface_get_inherit(iface))
1040 fprintf(header, " END_INTERFACE\n");
1041 fprintf(header, "};\n");
1042 fprintf(header, "#else\n");
1044 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
1046 fprintf(header, " BEGIN_INTERFACE\n");
1047 fprintf(header, "\n");
1049 write_c_disp_method_def(header, iface);
1051 write_c_method_def(header, iface);
1053 fprintf(header, " END_INTERFACE\n");
1054 fprintf(header, "} %sVtbl;\n", iface->name);
1055 fprintf(header, "interface %s {\n", iface->name);
1056 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1057 fprintf(header, "};\n");
1058 fprintf(header, "\n");
1059 fprintf(header, "#ifdef COBJMACROS\n");
1060 /* dispinterfaces don't have real functions, so don't write macros for them,
1061 * only for the interface this interface inherits from, i.e. IDispatch */
1062 write_method_macro(header, dispinterface ? type_iface_get_inherit(iface) : iface, iface->name);
1063 fprintf(header, "#endif\n");
1064 fprintf(header, "\n");
1065 fprintf(header, "#endif\n");
1066 fprintf(header, "\n");
1067 /* dispinterfaces don't have real functions, so don't write prototypes for
1071 write_method_proto(header, iface);
1072 write_locals(header, iface, FALSE);
1073 fprintf(header, "\n");
1075 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
1078 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1080 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1081 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1083 fprintf(header, "/*****************************************************************************\n");
1084 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1085 fprintf(header, " */\n");
1086 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1087 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1088 if (var) fprintf(header, "extern handle_t %s;\n", var);
1091 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1092 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1096 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1097 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1098 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1099 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1103 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1105 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1108 static void write_coclass(FILE *header, type_t *cocl)
1110 fprintf(header, "/*****************************************************************************\n");
1111 fprintf(header, " * %s coclass\n", cocl->name);
1112 fprintf(header, " */\n\n");
1113 write_coclass_guid(header, cocl);
1114 fprintf(header, "\n");
1117 static void write_coclass_forward(FILE *header, type_t *cocl)
1119 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1120 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1121 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1122 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1125 static void write_import(FILE *header, const char *fname)
1129 hname = dup_basename(fname, ".idl");
1130 p = hname + strlen(hname) - 2;
1131 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1133 fprintf(header, "#include <%s>\n", hname);
1137 static void write_imports(FILE *header, const statement_list_t *stmts)
1139 const statement_t *stmt;
1140 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1145 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1146 write_imports(header, type_iface_get_stmts(stmt->u.type));
1149 case STMT_IMPORTLIB:
1150 /* not included in header */
1153 write_import(header, stmt->u.str);
1158 case STMT_DECLARATION:
1159 /* not processed here */
1162 write_imports(header, stmt->u.lib->stmts);
1168 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1170 const statement_t *stmt;
1171 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1176 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1178 if (is_object(stmt->u.type) || is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE))
1179 write_forward(header, stmt->u.type);
1181 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1182 write_coclass_forward(header, stmt->u.type);
1185 case STMT_IMPORTLIB:
1186 /* not included in header */
1192 case STMT_DECLARATION:
1193 /* not processed here */
1196 write_forward_decls(header, stmt->u.lib->stmts);
1202 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1204 const statement_t *stmt;
1205 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1210 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1212 type_t *iface = stmt->u.type;
1213 if (is_object(iface)) is_object_interface++;
1214 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
1216 write_com_interface_start(header, iface);
1217 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1218 write_com_interface_end(header, iface);
1222 write_rpc_interface_start(header, iface);
1223 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1224 write_rpc_interface_end(header, iface);
1226 if (is_object(iface)) is_object_interface++;
1228 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1229 write_coclass(header, stmt->u.type);
1232 write_type_def_or_decl(header, stmt->u.type, FALSE, NULL);
1233 fprintf(header, ";\n\n");
1237 /* FIXME: shouldn't write out forward declarations for undefined
1238 * interfaces but a number of our IDL files depend on this */
1239 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
1240 write_forward(header, stmt->u.type);
1242 case STMT_IMPORTLIB:
1244 /* not included in header */
1247 /* not processed here */
1251 const type_list_t *type_entry = stmt->u.type_list;
1252 for (; type_entry; type_entry = type_entry->next)
1253 write_typedef(header, type_entry->type);
1257 write_library(header, stmt->u.lib);
1258 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1261 fprintf(header, "%s\n", stmt->u.str);
1263 case STMT_DECLARATION:
1264 if (iface && type_get_type(stmt->u.var->type) == TYPE_FUNCTION)
1268 int prefixes_differ = strcmp(prefix_client, prefix_server);
1270 if (prefixes_differ)
1272 fprintf(header, "/* client prototype */\n");
1273 write_function_proto(header, iface, stmt->u.var, prefix_client);
1274 fprintf(header, "/* server prototype */\n");
1276 write_function_proto(header, iface, stmt->u.var, prefix_server);
1280 write_declaration(header, stmt->u.var);
1286 void write_header(const statement_list_t *stmts)
1290 if (!do_header) return;
1292 if(!(header = fopen(header_name, "w"))) {
1293 error("Could not open %s for output\n", header_name);
1296 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1297 fprintf(header, "#include <rpc.h>\n" );
1298 fprintf(header, "#include <rpcndr.h>\n\n" );
1300 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
1301 fprintf(header, "#define __WIDL_%s\n\n", header_token);
1303 fprintf(header, "/* Forward declarations */\n\n");
1304 write_forward_decls(header, stmts);
1306 fprintf(header, "/* Headers for imported files */\n\n");
1307 write_imports(header, stmts);
1308 fprintf(header, "\n");
1309 start_cplusplus_guard(header);
1311 write_header_stmts(header, stmts, NULL, FALSE);
1313 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1314 fprintf(header, "\n");
1315 write_user_types(header);
1316 write_generic_handle_routines(header);
1317 write_context_handle_rundowns(header);
1318 fprintf(header, "\n");
1319 fprintf(header, "/* End additional prototypes */\n");
1320 fprintf(header, "\n");
1322 end_cplusplus_guard(header);
1323 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);