4 * Copyright 2002 Ove Kaaven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
39 typedef struct _user_type_t generic_handle_t;
41 static int indentation = 0;
42 user_type_list_t user_type_list = LIST_INIT(user_type_list);
43 static context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
44 static struct list generic_handle_list = LIST_INIT(generic_handle_list);
46 static void indent(FILE *h, int delta)
49 if (delta < 0) indentation += delta;
50 for (c=0; c<indentation; c++) fprintf(h, " ");
51 if (delta > 0) indentation += delta;
54 int is_ptrchain_attr(const var_t *var, enum attr_type t)
56 if (is_attr(var->attrs, t))
60 type_t *type = var->type;
63 if (is_attr(type->attrs, t))
65 else if (type_is_alias(type))
66 type = type_alias_get_aliasee(type);
67 else if (is_ptr(type))
68 type = type_pointer_get_ref(type);
74 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
76 const type_t *t = type;
79 if (is_attr(t->attrs, attr))
81 else if (type_is_alias(t))
82 t = type_alias_get_aliasee(t);
87 int is_attr(const attr_list_t *list, enum attr_type t)
90 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
91 if (attr->type == t) return 1;
95 void *get_attrp(const attr_list_t *list, enum attr_type t)
98 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
99 if (attr->type == t) return attr->u.pval;
103 unsigned int get_attrv(const attr_list_t *list, enum attr_type t)
106 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
107 if (attr->type == t) return attr->u.ival;
111 int is_void(const type_t *t)
113 return type_get_type(t) == TYPE_VOID;
116 int is_conformant_array(const type_t *t)
118 return is_array(t) && type_array_has_conformance(t);
121 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
124 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
125 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
126 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
127 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
128 uuid->Data4[6], uuid->Data4[7]);
131 const char *get_name(const var_t *v)
133 static char buffer[256];
135 if (is_attr( v->attrs, ATTR_PROPGET ))
136 strcpy( buffer, "get_" );
137 else if (is_attr( v->attrs, ATTR_PROPPUT ))
138 strcpy( buffer, "put_" );
139 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
140 strcpy( buffer, "putref_" );
143 strcat( buffer, v->name );
147 static void write_field(FILE *h, var_t *v)
152 write_type_def_or_decl(h, v->type, TRUE, v->name);
157 static void write_fields(FILE *h, var_list_t *fields)
161 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
164 static void write_enums(FILE *h, var_list_t *enums)
168 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
172 fprintf(h, "%s", get_name(v));
175 write_expr(h, v->eval, 0, 1, NULL, NULL, "");
178 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
183 int needs_space_after(type_t *t)
185 return (type_is_alias(t) ||
186 (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
189 void write_type_left(FILE *h, type_t *t, int declonly)
193 if (is_attr(t->attrs, ATTR_CONST) &&
194 (type_is_alias(t) || !is_ptr(t)))
195 fprintf(h, "const ");
197 if (type_is_alias(t)) fprintf(h, "%s", t->name);
199 switch (type_get_type_detect_alias(t)) {
201 if (!declonly && t->defined && !t->written) {
202 if (t->name) fprintf(h, "enum %s {\n", t->name);
203 else fprintf(h, "enum {\n");
206 write_enums(h, type_enum_get_values(t));
210 else fprintf(h, "enum %s", t->name ? t->name : "");
213 case TYPE_ENCAPSULATED_UNION:
214 if (!declonly && t->defined && !t->written) {
215 if (t->name) fprintf(h, "struct %s {\n", t->name);
216 else fprintf(h, "struct {\n");
219 if (type_get_type(t) != TYPE_STRUCT)
220 write_fields(h, type_encapsulated_union_get_fields(t));
222 write_fields(h, type_struct_get_fields(t));
226 else fprintf(h, "struct %s", t->name ? t->name : "");
229 if (!declonly && t->defined && !t->written) {
230 if (t->name) fprintf(h, "union %s {\n", t->name);
231 else fprintf(h, "union {\n");
234 write_fields(h, type_union_get_cases(t));
238 else fprintf(h, "union %s", t->name ? t->name : "");
241 write_type_left(h, type_pointer_get_ref(t), declonly);
242 fprintf(h, "%s*", needs_space_after(type_pointer_get_ref(t)) ? " " : "");
243 if (is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
246 if (t->name && type_array_is_decl_as_ptr(t))
247 fprintf(h, "%s", t->name);
250 write_type_left(h, type_array_get_element(t), declonly);
251 if (type_array_is_decl_as_ptr(t))
252 fprintf(h, "%s*", needs_space_after(type_array_get_element(t)) ? " " : "");
256 if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
257 type_basic_get_type(t) != TYPE_BASIC_INT64 &&
258 type_basic_get_type(t) != TYPE_BASIC_HYPER)
260 if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
261 else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
263 switch (type_basic_get_type(t))
265 case TYPE_BASIC_INT8: fprintf(h, "small"); break;
266 case TYPE_BASIC_INT16: fprintf(h, "short"); break;
267 case TYPE_BASIC_INT: fprintf(h, "int"); break;
268 case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
269 case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
270 case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
271 case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
272 case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
273 case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
274 case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
275 case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
276 case TYPE_BASIC_INT32:
277 if (type_basic_get_sign(t) > 0)
282 case TYPE_BASIC_INT64:
283 if (type_basic_get_sign(t) > 0)
284 fprintf(h, "UINT64");
288 case TYPE_BASIC_HYPER:
289 if (type_basic_get_sign(t) > 0)
290 fprintf(h, "MIDL_uhyper");
299 fprintf(h, "%s", t->name);
305 write_type_left(h, type_bitfield_get_field(t), declonly);
309 /* handled elsewhere */
316 void write_type_right(FILE *h, type_t *t, int is_field)
320 switch (type_get_type(t))
323 if (!type_array_is_decl_as_ptr(t))
325 if (is_conformant_array(t))
327 fprintf(h, "[%s]", is_field ? "1" : "");
328 t = type_array_get_element(t);
331 type_get_type(t) == TYPE_ARRAY && !type_array_is_decl_as_ptr(t);
332 t = type_array_get_element(t))
333 fprintf(h, "[%u]", type_array_get_dim(t));
337 fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
343 case TYPE_ENCAPSULATED_UNION:
355 static void write_type_v(FILE *h, type_t *t, int is_field, int declonly, const char *name)
363 for (pt = t; is_ptr(pt); pt = type_pointer_get_ref(pt), ptr_level++)
366 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
368 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
369 if (!callconv) callconv = "";
370 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
371 write_type_left(h, type_function_get_rettype(pt), declonly);
373 if (ptr_level) fputc('(', h);
374 fprintf(h, "%s ", callconv);
375 for (i = 0; i < ptr_level; i++)
378 write_type_left(h, t, declonly);
381 if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
384 if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
385 const var_list_t *args = type_function_get_args(pt);
387 if (ptr_level) fputc(')', h);
390 write_args(h, args, NULL, 0, FALSE);
395 write_type_right(h, t, is_field);
399 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name)
401 write_type_v(f, t, field, FALSE, name);
404 void write_type_decl(FILE *f, type_t *t, const char *name)
406 write_type_v(f, t, FALSE, TRUE, name);
409 void write_type_decl_left(FILE *f, type_t *t)
411 write_type_left(f, t, TRUE);
414 static int user_type_registered(const char *name)
417 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
418 if (!strcmp(name, ut->name))
423 static int context_handle_registered(const char *name)
425 context_handle_t *ch;
426 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
427 if (!strcmp(name, ch->name))
432 static int generic_handle_registered(const char *name)
434 generic_handle_t *gh;
435 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
436 if (!strcmp(name, gh->name))
441 /* check for types which require additional prototypes to be generated in the
443 void check_for_additional_prototype_types(const var_list_t *list)
448 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
450 type_t *type = v->type;
453 const char *name = type->name;
454 if (type->user_types_registered) break;
455 type->user_types_registered = 1;
456 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
457 if (!context_handle_registered(name))
459 context_handle_t *ch = xmalloc(sizeof(*ch));
460 ch->name = xstrdup(name);
461 list_add_tail(&context_handle_list, &ch->entry);
463 /* don't carry on parsing fields within this type */
466 if ((type_get_type(type) != TYPE_BASIC ||
467 type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
468 is_attr(type->attrs, ATTR_HANDLE)) {
469 if (!generic_handle_registered(name))
471 generic_handle_t *gh = xmalloc(sizeof(*gh));
472 gh->name = xstrdup(name);
473 list_add_tail(&generic_handle_list, &gh->entry);
475 /* don't carry on parsing fields within this type */
478 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
479 if (!user_type_registered(name))
481 user_type_t *ut = xmalloc(sizeof *ut);
482 ut->name = xstrdup(name);
483 list_add_tail(&user_type_list, &ut->entry);
485 /* don't carry on parsing fields within this type as we are already
486 * using a wire marshaled type */
489 else if (type_is_complete(type))
492 switch (type_get_type_detect_alias(type))
495 vars = type_enum_get_values(type);
498 vars = type_struct_get_fields(type);
501 vars = type_union_get_cases(type);
507 check_for_additional_prototype_types(vars);
510 if (type_is_alias(type))
511 type = type_alias_get_aliasee(type);
512 else if (is_ptr(type))
513 type = type_pointer_get_ref(type);
514 else if (is_array(type))
515 type = type_array_get_element(type);
522 static void write_user_types(FILE *header)
525 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
527 const char *name = ut->name;
528 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
529 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
530 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
531 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
535 static void write_context_handle_rundowns(FILE *header)
537 context_handle_t *ch;
538 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
540 const char *name = ch->name;
541 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
545 static void write_generic_handle_routines(FILE *header)
547 generic_handle_t *gh;
548 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
550 const char *name = gh->name;
551 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
552 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
556 static void write_typedef(FILE *header, type_t *type)
558 fprintf(header, "typedef ");
559 write_type_def_or_decl(header, type_alias_get_aliasee(type), FALSE, type->name);
560 fprintf(header, ";\n");
563 int is_const_decl(const var_t *var)
566 /* strangely, MIDL accepts a const attribute on any pointer in the
567 * declaration to mean that data isn't being instantiated. this appears
568 * to be a bug, but there is no benefit to being incompatible with MIDL,
569 * so we'll do the same thing */
570 for (t = var->type; ; )
572 if (is_attr(t->attrs, ATTR_CONST))
575 t = type_pointer_get_ref(t);
581 static void write_declaration(FILE *header, const var_t *v)
583 if (is_const_decl(v) && v->eval)
585 fprintf(header, "#define %s (", v->name);
586 write_expr(header, v->eval, 0, 1, NULL, NULL, "");
587 fprintf(header, ")\n\n");
594 case STG_REGISTER: /* ignored */
597 fprintf(header, "static ");
600 fprintf(header, "extern ");
603 write_type_def_or_decl(header, v->type, FALSE, v->name);
604 fprintf(header, ";\n\n");
608 static void write_library(FILE *header, const typelib_t *typelib)
610 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
611 fprintf(header, "\n");
612 write_guid(header, "LIBID", typelib->name, uuid);
613 fprintf(header, "\n");
617 const var_t* get_explicit_handle_var(const var_t *func)
621 if (!type_get_function_args(func->type))
624 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
626 const type_t *type = var->type;
627 if (type_get_type(type) == TYPE_BASIC && type_basic_get_type(type) == TYPE_BASIC_HANDLE)
634 const type_t* get_explicit_generic_handle_type(const var_t* var)
638 is_ptr(t) || type_is_alias(t);
639 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
640 if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
641 is_attr(t->attrs, ATTR_HANDLE))
646 const var_t* get_explicit_generic_handle_var(const var_t *func)
650 if (!type_get_function_args(func->type))
653 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
654 if (get_explicit_generic_handle_type(var))
660 const var_t* get_context_handle_var(const var_t *func)
664 if (!type_get_function_args(func->type))
667 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
668 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
674 int has_out_arg_or_return(const var_t *func)
678 if (!is_void(type_function_get_rettype(func->type)))
681 if (!type_get_function_args(func->type))
684 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
685 if (is_attr(var->attrs, ATTR_OUT))
692 /********** INTERFACES **********/
694 int is_object(const type_t *iface)
697 if (type_is_defined(iface) && type_iface_get_inherit(iface))
699 if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
700 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
704 int is_local(const attr_list_t *a)
706 return is_attr(a, ATTR_LOCAL);
709 const var_t *is_callas(const attr_list_t *a)
711 return get_attrp(a, ATTR_CALLAS);
714 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
716 const statement_t *stmt;
719 if (type_iface_get_inherit(iface))
720 write_method_macro(header, type_iface_get_inherit(iface), name);
722 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
724 const var_t *func = stmt->u.var;
728 fprintf(header, "/*** %s methods ***/\n", iface->name);
732 if (!is_callas(func->attrs)) {
735 fprintf(header, "#define %s_%s(This", name, get_name(func));
736 if (type_get_function_args(func->type))
737 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
738 fprintf(header, ",%s", arg->name);
739 fprintf(header, ") ");
741 fprintf(header, "(This)->lpVtbl->%s(This", get_name(func));
742 if (type_get_function_args(func->type))
743 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
744 fprintf(header, ",%s", arg->name);
745 fprintf(header, ")\n");
750 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
761 fprintf(h, "%s* This", name);
764 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
771 else fprintf(h, ",");
773 write_type_decl(h, arg->type, arg->name);
776 if (do_indent) indentation--;
779 static void write_cpp_method_def(FILE *header, const type_t *iface)
781 const statement_t *stmt;
783 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
785 const var_t *func = stmt->u.var;
786 if (!is_callas(func->attrs)) {
787 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
788 if (!callconv) callconv = "";
790 fprintf(header, "virtual ");
791 write_type_decl_left(header, type_function_get_rettype(func->type));
792 fprintf(header, " %s %s(\n", callconv, get_name(func));
793 write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
794 fprintf(header, ") = 0;\n");
795 fprintf(header, "\n");
800 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
802 const statement_t *stmt;
805 if (type_iface_get_inherit(iface))
806 do_write_c_method_def(header, type_iface_get_inherit(iface), name);
808 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
810 const var_t *func = stmt->u.var;
813 fprintf(header, "/*** %s methods ***/\n", iface->name);
816 if (!is_callas(func->attrs)) {
817 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
818 if (!callconv) callconv = "";
820 write_type_decl_left(header, type_function_get_rettype(func->type));
821 fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
822 write_args(header, type_get_function_args(func->type), name, 1, TRUE);
823 fprintf(header, ");\n");
824 fprintf(header, "\n");
829 static void write_c_method_def(FILE *header, const type_t *iface)
831 do_write_c_method_def(header, iface, iface->name);
834 static void write_c_disp_method_def(FILE *header, const type_t *iface)
836 do_write_c_method_def(header, type_iface_get_inherit(iface), iface->name);
839 static void write_method_proto(FILE *header, const type_t *iface)
841 const statement_t *stmt;
843 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
845 const var_t *func = stmt->u.var;
847 if (!is_local(func->attrs)) {
848 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
849 if (!callconv) callconv = "";
850 /* proxy prototype */
851 write_type_decl_left(header, type_function_get_rettype(func->type));
852 fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
853 write_args(header, type_get_function_args(func->type), iface->name, 1, TRUE);
854 fprintf(header, ");\n");
856 fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
857 fprintf(header, " IRpcStubBuffer* This,\n");
858 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
859 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
860 fprintf(header, " DWORD* pdwStubPhase);\n");
865 static void write_locals(FILE *fp, const type_t *iface, int body)
867 static const char comment[]
868 = "/* WIDL-generated stub. You must provide an implementation for this. */";
869 const statement_t *stmt;
871 if (!is_object(iface))
874 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
875 const var_t *func = stmt->u.var;
876 const var_t *cas = is_callas(func->attrs);
879 const statement_t *stmt2 = NULL;
880 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
881 if (!strcmp(stmt2->u.var->name, cas->name))
883 if (&stmt2->entry != type_iface_get_stmts(iface)) {
884 const var_t *m = stmt2->u.var;
885 /* proxy prototype - use local prototype */
886 write_type_decl_left(fp, type_function_get_rettype(m->type));
887 fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
888 write_args(fp, type_get_function_args(m->type), iface->name, 1, TRUE);
891 type_t *rt = type_function_get_rettype(m->type);
892 fprintf(fp, "\n{\n");
893 fprintf(fp, " %s\n", comment);
894 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
895 fprintf(fp, " return E_NOTIMPL;\n");
896 else if (type_get_type(rt) != TYPE_VOID) {
898 write_type_decl(fp, rt, "rv");
900 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
901 fprintf(fp, " return rv;\n");
903 fprintf(fp, "}\n\n");
907 /* stub prototype - use remotable prototype */
908 write_type_decl_left(fp, type_function_get_rettype(func->type));
909 fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
910 write_args(fp, type_get_function_args(func->type), iface->name, 1, TRUE);
913 /* Remotable methods must all return HRESULTs. */
914 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
919 error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
924 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
926 const statement_t *stmt;
927 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
929 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
930 write_locals(local_stubs, stmt->u.type, TRUE);
931 else if (stmt->type == STMT_LIBRARY)
932 write_local_stubs_stmts(local_stubs, stmt->u.lib->stmts);
936 void write_local_stubs(const statement_list_t *stmts)
940 if (!local_stubs_name) return;
942 local_stubs = fopen(local_stubs_name, "w");
944 error("Could not open %s for output\n", local_stubs_name);
947 fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
948 fprintf(local_stubs, "#include <objbase.h>\n");
949 fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
951 write_local_stubs_stmts(local_stubs, stmts);
956 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
958 const char *callconv = get_attrp(fun->type->attrs, ATTR_CALLCONV);
960 /* FIXME: do we need to handle call_as? */
961 write_type_decl_left(header, type_function_get_rettype(fun->type));
962 fprintf(header, " ");
963 if (callconv) fprintf(header, "%s ", callconv);
964 fprintf(header, "%s%s(\n", prefix, get_name(fun));
965 if (type_get_function_args(fun->type))
966 write_args(header, type_get_function_args(fun->type), iface->name, 0, TRUE);
968 fprintf(header, " void");
969 fprintf(header, ");\n\n");
972 static void write_forward(FILE *header, type_t *iface)
974 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
975 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
976 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
977 fprintf(header, "#endif\n\n" );
980 static void write_iface_guid(FILE *header, const type_t *iface)
982 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
983 write_guid(header, "IID", iface->name, uuid);
986 static void write_dispiface_guid(FILE *header, const type_t *iface)
988 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
989 write_guid(header, "DIID", iface->name, uuid);
992 static void write_coclass_guid(FILE *header, const type_t *cocl)
994 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
995 write_guid(header, "CLSID", cocl->name, uuid);
998 static void write_com_interface_start(FILE *header, const type_t *iface)
1000 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1001 fprintf(header, "/*****************************************************************************\n");
1002 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1003 fprintf(header, " */\n");
1004 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
1005 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
1008 static void write_com_interface_end(FILE *header, type_t *iface)
1010 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1012 write_dispiface_guid(header, iface);
1014 write_iface_guid(header, iface);
1016 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1017 if (type_iface_get_inherit(iface))
1019 fprintf(header, "interface %s : public %s\n", iface->name,
1020 type_iface_get_inherit(iface)->name);
1021 fprintf(header, "{\n");
1025 fprintf(header, "interface %s\n", iface->name);
1026 fprintf(header, "{\n");
1027 fprintf(header, " BEGIN_INTERFACE\n");
1028 fprintf(header, "\n");
1030 /* dispinterfaces don't have real functions, so don't write C++ functions for
1035 write_cpp_method_def(header, iface);
1038 if (!type_iface_get_inherit(iface))
1039 fprintf(header, " END_INTERFACE\n");
1040 fprintf(header, "};\n");
1041 fprintf(header, "#else\n");
1043 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
1045 fprintf(header, " BEGIN_INTERFACE\n");
1046 fprintf(header, "\n");
1048 write_c_disp_method_def(header, iface);
1050 write_c_method_def(header, iface);
1052 fprintf(header, " END_INTERFACE\n");
1053 fprintf(header, "} %sVtbl;\n", iface->name);
1054 fprintf(header, "interface %s {\n", iface->name);
1055 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1056 fprintf(header, "};\n");
1057 fprintf(header, "\n");
1058 fprintf(header, "#ifdef COBJMACROS\n");
1059 /* dispinterfaces don't have real functions, so don't write macros for them,
1060 * only for the interface this interface inherits from, i.e. IDispatch */
1061 write_method_macro(header, dispinterface ? type_iface_get_inherit(iface) : iface, iface->name);
1062 fprintf(header, "#endif\n");
1063 fprintf(header, "\n");
1064 fprintf(header, "#endif\n");
1065 fprintf(header, "\n");
1066 /* dispinterfaces don't have real functions, so don't write prototypes for
1070 write_method_proto(header, iface);
1071 write_locals(header, iface, FALSE);
1072 fprintf(header, "\n");
1074 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
1077 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1079 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1080 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1081 static int allocate_written = 0;
1083 if (!allocate_written)
1085 allocate_written = 1;
1086 fprintf(header, "void * __RPC_USER MIDL_user_allocate(SIZE_T);\n");
1087 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
1090 fprintf(header, "/*****************************************************************************\n");
1091 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1092 fprintf(header, " */\n");
1093 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1094 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1095 if (var) fprintf(header, "extern handle_t %s;\n", var);
1098 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1099 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1103 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1104 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1105 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1106 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1110 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1112 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1115 static void write_coclass(FILE *header, type_t *cocl)
1117 fprintf(header, "/*****************************************************************************\n");
1118 fprintf(header, " * %s coclass\n", cocl->name);
1119 fprintf(header, " */\n\n");
1120 write_coclass_guid(header, cocl);
1121 fprintf(header, "\n");
1124 static void write_coclass_forward(FILE *header, type_t *cocl)
1126 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1127 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1128 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1129 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1132 static void write_import(FILE *header, const char *fname)
1136 hname = dup_basename(fname, ".idl");
1137 p = hname + strlen(hname) - 2;
1138 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1140 fprintf(header, "#include <%s>\n", hname);
1144 static void write_imports(FILE *header, const statement_list_t *stmts)
1146 const statement_t *stmt;
1147 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1152 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1153 write_imports(header, type_iface_get_stmts(stmt->u.type));
1156 case STMT_IMPORTLIB:
1157 /* not included in header */
1160 write_import(header, stmt->u.str);
1165 case STMT_DECLARATION:
1166 /* not processed here */
1169 write_imports(header, stmt->u.lib->stmts);
1175 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1177 const statement_t *stmt;
1178 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1183 if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1185 if (is_object(stmt->u.type) || is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE))
1186 write_forward(header, stmt->u.type);
1188 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1189 write_coclass_forward(header, stmt->u.type);
1192 case STMT_IMPORTLIB:
1193 /* not included in header */
1199 case STMT_DECLARATION:
1200 /* not processed here */
1203 write_forward_decls(header, stmt->u.lib->stmts);
1209 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
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 type_t *iface = stmt->u.type;
1220 if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
1222 write_com_interface_start(header, iface);
1223 write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1224 write_com_interface_end(header, iface);
1228 write_rpc_interface_start(header, iface);
1229 write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1230 write_rpc_interface_end(header, iface);
1233 else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1234 write_coclass(header, stmt->u.type);
1237 write_type_def_or_decl(header, stmt->u.type, FALSE, NULL);
1238 fprintf(header, ";\n\n");
1242 /* FIXME: shouldn't write out forward declarations for undefined
1243 * interfaces but a number of our IDL files depend on this */
1244 if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
1245 write_forward(header, stmt->u.type);
1247 case STMT_IMPORTLIB:
1249 /* not included in header */
1252 /* not processed here */
1256 const type_list_t *type_entry = stmt->u.type_list;
1257 for (; type_entry; type_entry = type_entry->next)
1258 write_typedef(header, type_entry->type);
1262 write_library(header, stmt->u.lib);
1263 write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1266 fprintf(header, "%s\n", stmt->u.str);
1268 case STMT_DECLARATION:
1269 if (iface && type_get_type(stmt->u.var->type) == TYPE_FUNCTION)
1273 int prefixes_differ = strcmp(prefix_client, prefix_server);
1275 if (prefixes_differ)
1277 fprintf(header, "/* client prototype */\n");
1278 write_function_proto(header, iface, stmt->u.var, prefix_client);
1279 fprintf(header, "/* server prototype */\n");
1281 write_function_proto(header, iface, stmt->u.var, prefix_server);
1285 write_declaration(header, stmt->u.var);
1291 void write_header(const statement_list_t *stmts)
1295 if (!do_header) return;
1297 if(!(header = fopen(header_name, "w"))) {
1298 error("Could not open %s for output\n", header_name);
1301 fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1302 fprintf(header, "#include <rpc.h>\n" );
1303 fprintf(header, "#include <rpcndr.h>\n\n" );
1305 fprintf(header, "#ifndef __WIDL_%s\n", header_token);
1306 fprintf(header, "#define __WIDL_%s\n\n", header_token);
1307 start_cplusplus_guard(header);
1309 fprintf(header, "/* Headers for imported files */\n\n");
1310 write_imports(header, stmts);
1311 fprintf(header, "\n");
1313 /* FIXME: should be before imported file includes */
1314 fprintf(header, "/* Forward declarations */\n\n");
1315 write_forward_decls(header, stmts);
1316 fprintf(header, "\n");
1318 write_header_stmts(header, stmts, NULL, FALSE);
1320 fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1321 fprintf(header, "\n");
1322 write_user_types(header);
1323 write_generic_handle_routines(header);
1324 write_context_handle_rundowns(header);
1325 fprintf(header, "\n");
1326 fprintf(header, "/* End additional prototypes */\n");
1327 fprintf(header, "\n");
1329 end_cplusplus_guard(header);
1330 fprintf(header, "#endif /* __WIDL_%s */\n", header_token);