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 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 unsigned int get_context_handle_offset( const type_t *type )
444 context_handle_t *ch;
445 unsigned int index = 0;
447 while (!is_attr( type->attrs, ATTR_CONTEXTHANDLE ))
449 if (type_is_alias( type )) type = type_alias_get_aliasee( type );
450 else if (is_ptr( type )) type = type_pointer_get_ref( type );
451 else error( "internal error: %s is not a context handle\n", type->name );
453 LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
455 if (!strcmp( type->name, ch->name )) return index;
458 error( "internal error: %s is not registered as a context handle\n", type->name );
462 unsigned int get_generic_handle_offset( const type_t *type )
464 generic_handle_t *gh;
465 unsigned int index = 0;
467 while (!is_attr( type->attrs, ATTR_HANDLE ))
469 if (type_is_alias( type )) type = type_alias_get_aliasee( type );
470 else if (is_ptr( type )) type = type_pointer_get_ref( type );
471 else error( "internal error: %s is not a generic handle\n", type->name );
473 LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
475 if (!strcmp( type->name, gh->name )) return index;
478 error( "internal error: %s is not registered as a generic handle\n", type->name );
482 /* check for types which require additional prototypes to be generated in the
484 void check_for_additional_prototype_types(const var_list_t *list)
489 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
491 type_t *type = v->type;
494 const char *name = type->name;
495 if (type->user_types_registered) break;
496 type->user_types_registered = 1;
497 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
498 if (!context_handle_registered(name))
500 context_handle_t *ch = xmalloc(sizeof(*ch));
501 ch->name = xstrdup(name);
502 list_add_tail(&context_handle_list, &ch->entry);
504 /* don't carry on parsing fields within this type */
507 if ((type_get_type(type) != TYPE_BASIC ||
508 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
509 is_attr(type->attrs, ATTR_HANDLE)) {
510 if (!generic_handle_registered(name))
512 generic_handle_t *gh = xmalloc(sizeof(*gh));
513 gh->name = xstrdup(name);
514 list_add_tail(&generic_handle_list, &gh->entry);
516 /* don't carry on parsing fields within this type */
519 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
520 if (!user_type_registered(name))
522 user_type_t *ut = xmalloc(sizeof *ut);
523 ut->name = xstrdup(name);
524 list_add_tail(&user_type_list, &ut->entry);
526 /* don't carry on parsing fields within this type as we are already
527 * using a wire marshaled type */
530 else if (type_is_complete(type))
533 switch (type_get_type_detect_alias(type))
536 vars = type_enum_get_values(type);
539 vars = type_struct_get_fields(type);
542 vars = type_union_get_cases(type);
548 check_for_additional_prototype_types(vars);
551 if (type_is_alias(type))
552 type = type_alias_get_aliasee(type);
553 else if (is_ptr(type))
554 type = type_pointer_get_ref(type);
555 else if (is_array(type))
556 type = type_array_get_element(type);
563 static void write_user_types(FILE *header)
566 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
568 const char *name = ut->name;
569 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
570 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
571 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
572 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
576 static void write_context_handle_rundowns(FILE *header)
578 context_handle_t *ch;
579 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
581 const char *name = ch->name;
582 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
586 static void write_generic_handle_routines(FILE *header)
588 generic_handle_t *gh;
589 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
591 const char *name = gh->name;
592 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
593 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
597 static void write_typedef(FILE *header, type_t *type)
599 fprintf(header, "typedef ");
600 write_type_def_or_decl(header, type_alias_get_aliasee(type), FALSE, type->name);
601 fprintf(header, ";\n");
604 int is_const_decl(const var_t *var)
607 /* strangely, MIDL accepts a const attribute on any pointer in the
608 * declaration to mean that data isn't being instantiated. this appears
609 * to be a bug, but there is no benefit to being incompatible with MIDL,
610 * so we'll do the same thing */
611 for (t = var->type; ; )
613 if (is_attr(t->attrs, ATTR_CONST))
616 t = type_pointer_get_ref(t);
622 static void write_declaration(FILE *header, const var_t *v)
624 if (is_const_decl(v) && v->eval)
626 fprintf(header, "#define %s (", v->name);
627 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
628 fprintf(header, ")\n\n");
635 case STG_REGISTER: /* ignored */
638 fprintf(header, "static ");
641 fprintf(header, "extern ");
644 write_type_def_or_decl(header, v->type, FALSE, v->name);
645 fprintf(header, ";\n\n");
649 static void write_library(FILE *header, const typelib_t *typelib)
651 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
652 fprintf(header, "\n");
653 write_guid(header, "LIBID", typelib->name, uuid);
654 fprintf(header, "\n");
658 const type_t* get_explicit_generic_handle_type(const var_t* var)
662 is_ptr(t) || type_is_alias(t);
663 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
664 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
665 is_attr(t->attrs, ATTR_HANDLE))
670 const var_t *get_func_handle_var( const type_t *iface, const var_t *func,
671 unsigned char *explicit_fc, unsigned char *implicit_fc )
674 const var_list_t *args = type_get_function_args( func->type );
676 *explicit_fc = *implicit_fc = 0;
677 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
679 if (!is_attr( var->attrs, ATTR_IN ) && is_attr( var->attrs, ATTR_OUT )) continue;
680 if (type_get_type( var->type ) == TYPE_BASIC && type_basic_get_type( var->type ) == TYPE_BASIC_HANDLE)
682 *explicit_fc = RPC_FC_BIND_PRIMITIVE;
685 if (get_explicit_generic_handle_type( var ))
687 *explicit_fc = RPC_FC_BIND_GENERIC;
690 if (is_context_handle( var->type ))
692 *explicit_fc = RPC_FC_BIND_CONTEXT;
697 if ((var = get_attrp( iface->attrs, ATTR_IMPLICIT_HANDLE )))
699 if (type_get_type( var->type ) == TYPE_BASIC &&
700 type_basic_get_type( var->type ) == TYPE_BASIC_HANDLE)
701 *implicit_fc = RPC_FC_BIND_PRIMITIVE;
703 *implicit_fc = RPC_FC_BIND_GENERIC;
707 *implicit_fc = RPC_FC_AUTO_HANDLE;
711 int has_out_arg_or_return(const var_t *func)
715 if (!is_void(type_function_get_rettype(func->type)))
718 if (!type_get_function_args(func->type))
721 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
722 if (is_attr(var->attrs, ATTR_OUT))
729 /********** INTERFACES **********/
731 int is_object(const type_t *iface)
734 if (type_is_defined(iface) && type_iface_get_inherit(iface))
736 if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
737 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
741 int is_local(const attr_list_t *a)
743 return is_attr(a, ATTR_LOCAL);
746 const var_t *is_callas(const attr_list_t *a)
748 return get_attrp(a, ATTR_CALLAS);
751 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
753 const statement_t *stmt;
756 if (type_iface_get_inherit(iface))
757 write_method_macro(header, type_iface_get_inherit(iface), name);
759 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
761 const var_t *func = stmt->u.var;
765 fprintf(header, "/*** %s methods ***/\n", iface->name);
769 if (!is_callas(func->attrs)) {
772 fprintf(header, "#define %s_%s(This", name, get_name(func));
773 if (type_get_function_args(func->type))
774 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
775 fprintf(header, ",%s", arg->name);
776 fprintf(header, ") ");
778 fprintf(header, "(This)->lpVtbl->%s(This", get_name(func));
779 if (type_get_function_args(func->type))
780 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
781 fprintf(header, ",%s", arg->name);
782 fprintf(header, ")\n");
787 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
798 fprintf(h, "%s* This", name);
801 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
808 else fprintf(h, ",");
810 write_type_decl(h, arg->type, arg->name);
813 if (do_indent) indentation--;
816 static void write_cpp_method_def(FILE *header, const type_t *iface)
818 const statement_t *stmt;
820 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
822 const var_t *func = stmt->u.var;
823 if (!is_callas(func->attrs)) {
824 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
825 if (!callconv) callconv = "STDMETHODCALLTYPE";
827 fprintf(header, "virtual ");
828 write_type_decl_left(header, type_function_get_rettype(func->type));
829 fprintf(header, " %s %s(\n", callconv, get_name(func));
830 write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
831 fprintf(header, ") = 0;\n");
832 fprintf(header, "\n");
837 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
839 const statement_t *stmt;
842 if (type_iface_get_inherit(iface))
843 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
845 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
847 const var_t *func = stmt->u.var;
850 fprintf(header, "/*** %s methods ***/\n", iface->name);
853 if (!is_callas(func->attrs)) {
854 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
855 if (!callconv) callconv = "STDMETHODCALLTYPE";
857 write_type_decl_left(header, type_function_get_rettype(func->type));
858 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
859 write_args(header, type_get_function_args(func->type), name, 1, TRUE);
860 fprintf(header, ");\n");
861 fprintf(header, "\n");
866 static void write_c_method_def(FILE *header, const type_t *iface)
868 do_write_c_method_def(header, iface, iface->name);
871 static void write_c_disp_method_def(FILE *header, const type_t *iface)
873 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->name);
876 static void write_method_proto(FILE *header, const type_t *iface)
878 const statement_t *stmt;
880 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
882 const var_t *func = stmt->u.var;
884 if (!is_local(func->attrs)) {
885 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
886 if (!callconv) callconv = "STDMETHODCALLTYPE";
887 /* proxy prototype */
888 write_type_decl_left(header, type_function_get_rettype(func->type));
889 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
890 write_args(header, type_get_function_args(func->type), iface->name, 1, TRUE);
891 fprintf(header, ");\n");
893 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
894 fprintf(header, " IRpcStubBuffer* This,\n");
895 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
896 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
897 fprintf(header, " DWORD* pdwStubPhase);\n");
902 static void write_locals(FILE *fp, const type_t *iface, int body)
904 static const char comment[]
905 = "/* WIDL-generated stub. You must provide an implementation for this. */";
906 const statement_t *stmt;
908 if (!is_object(iface))
911 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
912 const var_t *func = stmt->u.var;
913 const var_t *cas = is_callas(func->attrs);
916 const statement_t *stmt2 = NULL;
917 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
918 if (!strcmp(stmt2->u.var->name, cas->name))
920 if (&stmt2->entry != type_iface_get_stmts(iface)) {
921 const var_t *m = stmt2->u.var;
922 /* proxy prototype - use local prototype */
923 write_type_decl_left(fp, type_function_get_rettype(m->type));
924 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
925 write_args(fp, type_get_function_args(m->type), iface->name, 1, TRUE);
928 type_t *rt = type_function_get_rettype(m->type);
929 fprintf(fp, "\n{\n");
930 fprintf(fp, " %s\n", comment);
931 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
932 fprintf(fp, " return E_NOTIMPL;\n");
933 else if (type_get_type(rt) != TYPE_VOID) {
935 write_type_decl(fp, rt, "rv");
937 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
938 fprintf(fp, " return rv;\n");
940 fprintf(fp, "}\n\n");
944 /* stub prototype - use remotable prototype */
945 write_type_decl_left(fp, type_function_get_rettype(func->type));
946 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
947 write_args(fp, type_get_function_args(func->type), iface->name, 1, TRUE);
950 /* Remotable methods must all return HRESULTs. */
951 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
956 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
961 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
963 const statement_t *stmt;
964 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
966 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
967 write_locals(local_stubs, stmt->u.type, TRUE);
968 else if (stmt->type == STMT_LIBRARY)
969 write_local_stubs_stmts(local_stubs, stmt->u.lib->stmts);
973 void write_local_stubs(const statement_list_t *stmts)
977 if (!local_stubs_name) return;
979 local_stubs = fopen(local_stubs_name, "w");
981 error("Could not open %s for output\n", local_stubs_name);
984 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
985 fprintf(local_stubs, "#include <objbase.h>\n");
986 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
988 write_local_stubs_stmts(local_stubs, stmts);
993 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
995 const char *callconv = get_attrp(fun->type->attrs, ATTR_CALLCONV);
997 if (!callconv) callconv = "__cdecl";
998 /* FIXME: do we need to handle call_as? */
999 write_type_decl_left(header, type_function_get_rettype(fun->type));
1000 fprintf(header, " %s ", callconv);
1001 fprintf(header, "%s%s(\n", prefix, get_name(fun));
1002 if (type_get_function_args(fun->type))
1003 write_args(header, type_get_function_args(fun->type), iface->name, 0, TRUE);
1005 fprintf(header, " void");
1006 fprintf(header, ");\n\n");
1009 static void write_forward(FILE *header, type_t *iface)
1011 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
1012 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
1013 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
1014 fprintf(header, "#endif\n\n" );
1017 static void write_iface_guid(FILE *header, const type_t *iface)
1019 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1020 write_guid(header, "IID", iface->name, uuid);
1023 static void write_dispiface_guid(FILE *header, const type_t *iface)
1025 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1026 write_guid(header, "DIID", iface->name, uuid);
1029 static void write_coclass_guid(FILE *header, const type_t *cocl)
1031 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
1032 write_guid(header, "CLSID", cocl->name, uuid);
1035 static void write_com_interface_start(FILE *header, const type_t *iface)
1037 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1038 fprintf(header, "/*****************************************************************************\n");
1039 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1040 fprintf(header, " */\n");
1041 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
1042 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
1045 static void write_com_interface_end(FILE *header, type_t *iface)
1047 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1049 write_dispiface_guid(header, iface);
1051 write_iface_guid(header, iface);
1053 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1054 if (type_iface_get_inherit(iface))
1056 fprintf(header, "interface %s : public %s\n", iface->name,
1057 type_iface_get_inherit(iface)->name);
1058 fprintf(header, "{\n");
1062 fprintf(header, "interface %s\n", iface->name);
1063 fprintf(header, "{\n");
1064 fprintf(header, " BEGIN_INTERFACE\n");
1065 fprintf(header, "\n");
1067 /* dispinterfaces don't have real functions, so don't write C++ functions for
1072 write_cpp_method_def(header, iface);
1075 if (!type_iface_get_inherit(iface))
1076 fprintf(header, " END_INTERFACE\n");
1077 fprintf(header, "};\n");
1078 fprintf(header, "#else\n");
1080 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
1082 fprintf(header, " BEGIN_INTERFACE\n");
1083 fprintf(header, "\n");
1085 write_c_disp_method_def(header, iface);
1087 write_c_method_def(header, iface);
1089 fprintf(header, " END_INTERFACE\n");
1090 fprintf(header, "} %sVtbl;\n", iface->name);
1091 fprintf(header, "interface %s {\n", iface->name);
1092 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1093 fprintf(header, "};\n");
1094 fprintf(header, "\n");
1095 fprintf(header, "#ifdef COBJMACROS\n");
1096 /* dispinterfaces don't have real functions, so don't write macros for them,
1097 * only for the interface this interface inherits from, i.e. IDispatch */
1098 write_method_macro(header, dispinterface ? type_iface_get_inherit(iface) : iface, iface->name);
1099 fprintf(header, "#endif\n");
1100 fprintf(header, "\n");
1101 fprintf(header, "#endif\n");
1102 fprintf(header, "\n");
1103 /* dispinterfaces don't have real functions, so don't write prototypes for
1107 write_method_proto(header, iface);
1108 write_locals(header, iface, FALSE);
1109 fprintf(header, "\n");
1111 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
1114 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1116 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1117 const var_t *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1119 fprintf(header, "/*****************************************************************************\n");
1120 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1121 fprintf(header, " */\n");
1122 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1123 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1126 fprintf(header, "extern ");
1127 write_type_decl( header, var->type, var->name );
1128 fprintf(header, ";\n");
1132 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1133 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1137 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1138 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1139 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1140 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1144 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1146 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1149 static void write_coclass(FILE *header, type_t *cocl)
1151 fprintf(header, "/*****************************************************************************\n");
1152 fprintf(header, " * %s coclass\n", cocl->name);
1153 fprintf(header, " */\n\n");
1154 write_coclass_guid(header, cocl);
1155 fprintf(header, "\n");
1158 static void write_coclass_forward(FILE *header, type_t *cocl)
1160 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1161 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1162 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1163 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1166 static void write_import(FILE *header, const char *fname)
1170 hname = dup_basename(fname, ".idl");
1171 p = hname + strlen(hname) - 2;
1172 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1174 fprintf(header, "#include <%s>\n", hname);
1178 static void write_imports(FILE *header, const statement_list_t *stmts)
1180 const statement_t *stmt;
1181 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1186 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1187 write_imports(header, type_iface_get_stmts(stmt->u.type));
1190 case STMT_IMPORTLIB:
1191 /* not included in header */
1194 write_import(header, stmt->u.str);
1199 case STMT_DECLARATION:
1200 /* not processed here */
1203 write_imports(header, stmt->u.lib->stmts);
1209 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1211 const statement_t *stmt;
1212 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1217 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1219 if (is_object(stmt->u.type) || is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE))
1220 write_forward(header, stmt->u.type);
1222 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1223 write_coclass_forward(header, stmt->u.type);
1226 case STMT_IMPORTLIB:
1227 /* not included in header */
1233 case STMT_DECLARATION:
1234 /* not processed here */
1237 write_forward_decls(header, stmt->u.lib->stmts);
1243 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1245 const statement_t *stmt;
1246 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1251 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1253 type_t *iface = stmt->u.type;
1254 if (is_object(iface)) is_object_interface++;
1255 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
1257 write_com_interface_start(header, iface);
1258 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1259 write_com_interface_end(header, iface);
1263 write_rpc_interface_start(header, iface);
1264 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1265 write_rpc_interface_end(header, iface);
1267 if (is_object(iface)) is_object_interface++;
1269 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1270 write_coclass(header, stmt->u.type);
1273 write_type_def_or_decl(header, stmt->u.type, FALSE, NULL);
1274 fprintf(header, ";\n\n");
1278 /* FIXME: shouldn't write out forward declarations for undefined
1279 * interfaces but a number of our IDL files depend on this */
1280 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
1281 write_forward(header, stmt->u.type);
1283 case STMT_IMPORTLIB:
1285 /* not included in header */
1288 /* not processed here */
1292 const type_list_t *type_entry = stmt->u.type_list;
1293 for (; type_entry; type_entry = type_entry->next)
1294 write_typedef(header, type_entry->type);
1298 write_library(header, stmt->u.lib);
1299 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1302 fprintf(header, "%s\n", stmt->u.str);
1304 case STMT_DECLARATION:
1305 if (iface && type_get_type(stmt->u.var->type) == TYPE_FUNCTION)
1309 int prefixes_differ = strcmp(prefix_client, prefix_server);
1311 if (prefixes_differ)
1313 fprintf(header, "/* client prototype */\n");
1314 write_function_proto(header, iface, stmt->u.var, prefix_client);
1315 fprintf(header, "/* server prototype */\n");
1317 write_function_proto(header, iface, stmt->u.var, prefix_server);
1321 write_declaration(header, stmt->u.var);
1327 void write_header(const statement_list_t *stmts)
1331 if (!do_header) return;
1333 if(!(header = fopen(header_name, "w"))) {
1334 error("Could not open %s for output\n", header_name);
1337 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1338 fprintf(header, "#include <rpc.h>\n" );
1339 fprintf(header, "#include <rpcndr.h>\n\n" );
1341 fprintf(header, "#if !defined(COM_NO_WINDOWS_H) && !defined(__WINESRC__)\n");
1342 fprintf(header, "#include <windows.h>\n");
1343 fprintf(header, "#include <ole2.h>\n");
1344 fprintf(header, "#endif\n\n");
1346 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
1347 fprintf(header, "#define __WIDL_%s\n\n", header_token);
1349 fprintf(header, "/* Forward declarations */\n\n");
1350 write_forward_decls(header, stmts);
1352 fprintf(header, "/* Headers for imported files */\n\n");
1353 write_imports(header, stmts);
1354 fprintf(header, "\n");
1355 start_cplusplus_guard(header);
1357 write_header_stmts(header, stmts, NULL, FALSE);
1359 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1360 fprintf(header, "\n");
1361 write_user_types(header);
1362 write_generic_handle_routines(header);
1363 write_context_handle_rundowns(header);
1364 fprintf(header, "\n");
1365 fprintf(header, "/* End additional prototypes */\n");
1366 fprintf(header, "\n");
1368 end_cplusplus_guard(header);
1369 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);