dplayx: Tests for GetCaps.
[wine/gsoc_dplay.git] / tools / widl / header.c
bloba3f7b01b8b25284494258114f3df0bbf991ad709
1 /*
2 * IDL Compiler
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
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <ctype.h>
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
36 #include "expr.h"
38 typedef struct _user_type_t generic_handle_t;
40 static int indentation = 0;
41 user_type_list_t user_type_list = LIST_INIT(user_type_list);
42 static context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
43 static struct list generic_handle_list = LIST_INIT(generic_handle_list);
45 static void indent(FILE *h, int delta)
47 int c;
48 if (delta < 0) indentation += delta;
49 for (c=0; c<indentation; c++) fprintf(h, " ");
50 if (delta > 0) indentation += delta;
53 int is_ptrchain_attr(const var_t *var, enum attr_type t)
55 if (is_attr(var->attrs, t))
56 return 1;
57 else
59 type_t *type = var->type;
60 for (;;)
62 if (is_attr(type->attrs, t))
63 return 1;
64 else if (type->kind == TKIND_ALIAS)
65 type = type->orig;
66 else if (is_ptr(type))
67 type = type->ref;
68 else return 0;
73 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
75 const type_t *t = type;
76 for (;;)
78 if (is_attr(t->attrs, attr))
79 return 1;
80 else if (t->kind == TKIND_ALIAS)
81 t = t->orig;
82 else return 0;
86 int is_attr(const attr_list_t *list, enum attr_type t)
88 const attr_t *attr;
89 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
90 if (attr->type == t) return 1;
91 return 0;
94 void *get_attrp(const attr_list_t *list, enum attr_type t)
96 const attr_t *attr;
97 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
98 if (attr->type == t) return attr->u.pval;
99 return NULL;
102 unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
104 const attr_t *attr;
105 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
106 if (attr->type == t) return attr->u.ival;
107 return 0;
110 int is_void(const type_t *t)
112 if (!t->type && !t->ref) return 1;
113 return 0;
116 int is_conformant_array(const type_t *t)
118 return t->type == RPC_FC_CARRAY
119 || t->type == RPC_FC_CVARRAY
120 || (t->type == RPC_FC_BOGUS_ARRAY && t->size_is);
123 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
125 if (!uuid) return;
126 fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
127 "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
128 guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
129 uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
130 uuid->Data4[6], uuid->Data4[7]);
133 void write_name(FILE *h, const var_t *v)
135 if (is_attr( v->attrs, ATTR_PROPGET ))
136 fprintf(h, "get_" );
137 else if (is_attr( v->attrs, ATTR_PROPPUT ))
138 fprintf(h, "put_" );
139 else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
140 fprintf(h, "putref_" );
141 fprintf(h, "%s", v->name);
144 void write_prefix_name(FILE *h, const char *prefix, const var_t *v)
146 fprintf(h, "%s", prefix);
147 write_name(h, v);
150 static void write_field(FILE *h, var_t *v)
152 if (!v) return;
153 if (v->type) {
154 const char *name = v->name;
155 if (name == NULL) {
156 switch (v->type->type) {
157 case RPC_FC_STRUCT:
158 case RPC_FC_CVSTRUCT:
159 case RPC_FC_CPSTRUCT:
160 case RPC_FC_CSTRUCT:
161 case RPC_FC_PSTRUCT:
162 case RPC_FC_BOGUS_STRUCT:
163 case RPC_FC_ENCAPSULATED_UNION:
164 name = "DUMMYSTRUCTNAME";
165 break;
166 case RPC_FC_NON_ENCAPSULATED_UNION:
167 name = "DUMMYUNIONNAME";
168 break;
169 default:
170 /* ? */
171 break;
174 indent(h, 0);
175 write_type_def_or_decl(h, v->type, TRUE, "%s", name);
176 fprintf(h, ";\n");
180 static void write_fields(FILE *h, var_list_t *fields)
182 var_t *v;
183 if (!fields) return;
184 LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
187 static void write_enums(FILE *h, var_list_t *enums)
189 var_t *v;
190 if (!enums) return;
191 LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
193 if (v->name) {
194 indent(h, 0);
195 write_name(h, v);
196 if (v->eval) {
197 fprintf(h, " = ");
198 write_expr(h, v->eval, 0, 1, NULL, NULL);
201 if (list_next( enums, &v->entry )) fprintf(h, ",\n");
203 fprintf(h, "\n");
206 int needs_space_after(type_t *t)
208 return (t->kind == TKIND_ALIAS
209 || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
212 void write_type_left(FILE *h, type_t *t, int declonly)
214 if (!h) return;
216 if (is_attr(t->attrs, ATTR_CONST) &&
217 (t->kind == TKIND_ALIAS || t->declarray || !is_ptr(t)))
218 fprintf(h, "const ");
220 if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
221 else if (t->declarray) write_type_left(h, t->ref, declonly);
222 else {
223 if (t->sign > 0) fprintf(h, "signed ");
224 else if (t->sign < 0) fprintf(h, "unsigned ");
225 switch (t->type) {
226 case RPC_FC_ENUM16:
227 case RPC_FC_ENUM32:
228 if (!declonly && t->defined && !t->written && !t->ignore) {
229 if (t->name) fprintf(h, "enum %s {\n", t->name);
230 else fprintf(h, "enum {\n");
231 t->written = TRUE;
232 indentation++;
233 write_enums(h, t->fields_or_args);
234 indent(h, -1);
235 fprintf(h, "}");
237 else fprintf(h, "enum %s", t->name ? t->name : "");
238 break;
239 case RPC_FC_STRUCT:
240 case RPC_FC_CVSTRUCT:
241 case RPC_FC_CPSTRUCT:
242 case RPC_FC_CSTRUCT:
243 case RPC_FC_PSTRUCT:
244 case RPC_FC_BOGUS_STRUCT:
245 case RPC_FC_ENCAPSULATED_UNION:
246 if (!declonly && t->defined && !t->written && !t->ignore) {
247 if (t->name) fprintf(h, "struct %s {\n", t->name);
248 else fprintf(h, "struct {\n");
249 t->written = TRUE;
250 indentation++;
251 write_fields(h, t->fields_or_args);
252 indent(h, -1);
253 fprintf(h, "}");
255 else fprintf(h, "struct %s", t->name ? t->name : "");
256 break;
257 case RPC_FC_NON_ENCAPSULATED_UNION:
258 if (!declonly && t->defined && !t->written && !t->ignore) {
259 if (t->name) fprintf(h, "union %s {\n", t->name);
260 else fprintf(h, "union {\n");
261 t->written = TRUE;
262 indentation++;
263 write_fields(h, t->fields_or_args);
264 indent(h, -1);
265 fprintf(h, "}");
267 else fprintf(h, "union %s", t->name ? t->name : "");
268 break;
269 case RPC_FC_RP:
270 case RPC_FC_UP:
271 case RPC_FC_FP:
272 case RPC_FC_OP:
273 case RPC_FC_CARRAY:
274 case RPC_FC_CVARRAY:
275 case RPC_FC_BOGUS_ARRAY:
276 write_type_left(h, t->ref, declonly);
277 fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
278 if (is_ptr(t) && is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
279 break;
280 default:
281 fprintf(h, "%s", t->name);
286 void write_type_right(FILE *h, type_t *t, int is_field)
288 if (!h) return;
290 if (t->declarray) {
291 if (is_conformant_array(t)) {
292 fprintf(h, "[%s]", is_field ? "1" : "");
293 t = t->ref;
295 for ( ; t->declarray; t = t->ref)
296 fprintf(h, "[%lu]", t->dim);
300 void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
301 const char *fmt, va_list args)
303 type_t *pt;
304 int ptr_level = 0;
306 if (!h) return;
308 for (pt = t; is_ptr(pt); pt = pt->ref, ptr_level++)
311 if (pt->type == RPC_FC_FUNCTION) {
312 int i;
313 const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
314 if (!callconv) callconv = "";
315 if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
316 write_type_left(h, pt->ref, declonly);
317 fputc(' ', h);
318 if (ptr_level) fputc('(', h);
319 fprintf(h, "%s ", callconv);
320 for (i = 0; i < ptr_level; i++)
321 fputc('*', h);
322 } else
323 write_type_left(h, t, declonly);
324 if (fmt) {
325 if (needs_space_after(t))
326 fputc(' ', h);
327 vfprintf(h, fmt, args);
329 if (pt->type == RPC_FC_FUNCTION) {
330 if (ptr_level) fputc(')', h);
331 fputc('(', h);
332 write_args(h, pt->fields_or_args, NULL, 0, FALSE);
333 fputc(')', h);
334 } else
335 write_type_right(h, t, is_field);
338 void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
340 va_list args;
341 va_start(args, fmt);
342 write_type_v(f, t, field, FALSE, fmt, args);
343 va_end(args);
346 void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
348 va_list args;
349 va_start(args, fmt);
350 write_type_v(f, t, FALSE, TRUE, fmt, args);
351 va_end(args);
354 void write_type_decl_left(FILE *f, type_t *t)
356 write_type_left(f, t, TRUE);
359 static int user_type_registered(const char *name)
361 user_type_t *ut;
362 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
363 if (!strcmp(name, ut->name))
364 return 1;
365 return 0;
368 static int context_handle_registered(const char *name)
370 context_handle_t *ch;
371 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
372 if (!strcmp(name, ch->name))
373 return 1;
374 return 0;
377 static int generic_handle_registered(const char *name)
379 generic_handle_t *gh;
380 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
381 if (!strcmp(name, gh->name))
382 return 1;
383 return 0;
386 /* check for types which require additional prototypes to be generated in the
387 * header */
388 void check_for_additional_prototype_types(const var_list_t *list)
390 const var_t *v;
392 if (!list) return;
393 LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
395 type_t *type;
396 for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
397 const char *name = type->name;
398 if (type->user_types_registered) continue;
399 type->user_types_registered = 1;
400 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
401 if (!context_handle_registered(name))
403 context_handle_t *ch = xmalloc(sizeof(*ch));
404 ch->name = xstrdup(name);
405 list_add_tail(&context_handle_list, &ch->entry);
407 /* don't carry on parsing fields within this type */
408 break;
410 if (type->type != RPC_FC_BIND_PRIMITIVE && is_attr(type->attrs, ATTR_HANDLE)) {
411 if (!generic_handle_registered(name))
413 generic_handle_t *gh = xmalloc(sizeof(*gh));
414 gh->name = xstrdup(name);
415 list_add_tail(&generic_handle_list, &gh->entry);
417 /* don't carry on parsing fields within this type */
418 break;
420 if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
421 if (!user_type_registered(name))
423 user_type_t *ut = xmalloc(sizeof *ut);
424 ut->name = xstrdup(name);
425 list_add_tail(&user_type_list, &ut->entry);
427 /* don't carry on parsing fields within this type as we are already
428 * using a wire marshaled type */
429 break;
431 else
433 check_for_additional_prototype_types(type->fields_or_args);
439 void write_user_types(void)
441 user_type_t *ut;
442 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
444 const char *name = ut->name;
445 fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
446 fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
447 fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
448 fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
452 void write_context_handle_rundowns(void)
454 context_handle_t *ch;
455 LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
457 const char *name = ch->name;
458 fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
462 void write_generic_handle_routines(void)
464 generic_handle_t *gh;
465 LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
467 const char *name = gh->name;
468 fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
469 fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
473 void write_typedef(type_t *type)
475 fprintf(header, "typedef ");
476 write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
477 fprintf(header, ";\n");
480 int is_const_decl(const var_t *var)
482 const type_t *t;
483 /* strangely, MIDL accepts a const attribute on any pointer in the
484 * declaration to mean that data isn't being instantiated. this appears
485 * to be a bug, but there is no benefit to being incompatible with MIDL,
486 * so we'll do the same thing */
487 for (t = var->type; ; )
489 if (is_attr(t->attrs, ATTR_CONST))
490 return TRUE;
491 else if (is_ptr(t))
492 t = t->ref;
493 else break;
495 return FALSE;
498 void write_declaration(const var_t *v, int is_in_interface)
500 if (is_const_decl(v) && v->eval)
502 fprintf(header, "#define %s (", v->name);
503 write_expr(header, v->eval, 0, 1, NULL, NULL);
504 fprintf(header, ")\n\n");
506 else if (v->type->type != RPC_FC_FUNCTION || !is_in_interface)
508 switch (v->stgclass)
510 case STG_NONE:
511 case STG_REGISTER: /* ignored */
512 break;
513 case STG_STATIC:
514 fprintf(header, "static ");
515 break;
516 case STG_EXTERN:
517 fprintf(header, "extern ");
518 break;
520 write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
521 fprintf(header, ";\n\n");
525 void write_library(const typelib_t *typelib)
527 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
528 fprintf(header, "\n");
529 write_guid(header, "LIBID", typelib->name, uuid);
530 fprintf(header, "\n");
534 const var_t* get_explicit_handle_var(const func_t* func)
536 const var_t* var;
538 if (!func->args)
539 return NULL;
541 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
542 if (var->type->type == RPC_FC_BIND_PRIMITIVE)
543 return var;
545 return NULL;
548 const type_t* get_explicit_generic_handle_type(const var_t* var)
550 const type_t *t;
551 for (t = var->type; is_ptr(t); t = t->ref)
552 if (t->type != RPC_FC_BIND_PRIMITIVE && is_attr(t->attrs, ATTR_HANDLE))
553 return t;
554 return NULL;
557 const var_t* get_explicit_generic_handle_var(const func_t* func)
559 const var_t* var;
561 if (!func->args)
562 return NULL;
564 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
565 if (get_explicit_generic_handle_type(var))
566 return var;
568 return NULL;
571 const var_t* get_context_handle_var(const func_t* func)
573 const var_t* var;
575 if (!func->args)
576 return NULL;
578 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
579 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
580 return var;
582 return NULL;
585 int has_out_arg_or_return(const func_t *func)
587 const var_t *var;
589 if (!is_void(get_func_return_type(func)))
590 return 1;
592 if (!func->args)
593 return 0;
595 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
596 if (is_attr(var->attrs, ATTR_OUT))
597 return 1;
599 return 0;
603 /********** INTERFACES **********/
605 int is_object(const attr_list_t *list)
607 const attr_t *attr;
608 if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
609 if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
610 return 0;
613 int is_local(const attr_list_t *a)
615 return is_attr(a, ATTR_LOCAL);
618 const var_t *is_callas(const attr_list_t *a)
620 return get_attrp(a, ATTR_CALLAS);
623 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
625 const func_t *cur;
627 if (iface->ref) write_method_macro(header, iface->ref, name);
629 if (!iface->funcs) return;
631 fprintf(header, "/*** %s methods ***/\n", iface->name);
632 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
634 var_t *def = cur->def;
635 if (!is_callas(def->attrs)) {
636 const var_t *arg;
638 fprintf(header, "#define %s_", name);
639 write_name(header,def);
640 fprintf(header, "(This");
641 if (cur->args)
642 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
643 fprintf(header, ",%s", arg->name);
644 fprintf(header, ") ");
646 fprintf(header, "(This)->lpVtbl->");
647 write_name(header, def);
648 fprintf(header, "(This");
649 if (cur->args)
650 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
651 fprintf(header, ",%s", arg->name);
652 fprintf(header, ")\n");
657 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
659 const var_t *arg;
660 int count = 0;
662 if (do_indent)
664 indentation++;
665 indent(h, 0);
667 if (method == 1) {
668 fprintf(h, "%s* This", name);
669 count++;
671 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
672 if (count) {
673 if (do_indent)
675 fprintf(h, ",\n");
676 indent(h, 0);
678 else fprintf(h, ",");
680 write_type_decl(h, arg->type, "%s", arg->name);
681 count++;
683 if (do_indent) indentation--;
686 static void write_cpp_method_def(FILE *header, const type_t *iface)
688 const func_t *cur;
690 if (!iface->funcs) return;
692 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
694 var_t *def = cur->def;
695 if (!is_callas(def->attrs)) {
696 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
697 if (!callconv) callconv = "";
698 indent(header, 0);
699 fprintf(header, "virtual ");
700 write_type_decl_left(header, get_func_return_type(cur));
701 fprintf(header, " %s ", callconv);
702 write_name(header, def);
703 fprintf(header, "(\n");
704 write_args(header, cur->args, iface->name, 2, TRUE);
705 fprintf(header, ") = 0;\n");
706 fprintf(header, "\n");
711 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
713 const func_t *cur;
715 if (iface->ref) do_write_c_method_def(header, iface->ref, name);
717 if (!iface->funcs) return;
718 indent(header, 0);
719 fprintf(header, "/*** %s methods ***/\n", iface->name);
720 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
722 const var_t *def = cur->def;
723 if (!is_callas(def->attrs)) {
724 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
725 if (!callconv) callconv = "";
726 indent(header, 0);
727 write_type_decl_left(header, get_func_return_type(cur));
728 fprintf(header, " (%s *", callconv);
729 write_name(header, def);
730 fprintf(header, ")(\n");
731 write_args(header, cur->args, name, 1, TRUE);
732 fprintf(header, ");\n");
733 fprintf(header, "\n");
738 static void write_c_method_def(FILE *header, const type_t *iface)
740 do_write_c_method_def(header, iface, iface->name);
743 static void write_c_disp_method_def(FILE *header, const type_t *iface)
745 do_write_c_method_def(header, iface->ref, iface->name);
748 static void write_method_proto(FILE *header, const type_t *iface)
750 const func_t *cur;
752 if (!iface->funcs) return;
753 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
755 const var_t *def = cur->def;
757 if (!is_local(def->attrs)) {
758 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
759 if (!callconv) callconv = "";
760 /* proxy prototype */
761 write_type_decl_left(header, get_func_return_type(cur));
762 fprintf(header, " %s %s_", callconv, iface->name);
763 write_name(header, def);
764 fprintf(header, "_Proxy(\n");
765 write_args(header, cur->args, iface->name, 1, TRUE);
766 fprintf(header, ");\n");
767 /* stub prototype */
768 fprintf(header, "void __RPC_STUB %s_", iface->name);
769 write_name(header,def);
770 fprintf(header, "_Stub(\n");
771 fprintf(header, " IRpcStubBuffer* This,\n");
772 fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
773 fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
774 fprintf(header, " DWORD* pdwStubPhase);\n");
779 void write_locals(FILE *fp, const type_t *iface, int body)
781 static const char comment[]
782 = "/* WIDL-generated stub. You must provide an implementation for this. */";
783 const func_list_t *funcs = iface->funcs;
784 const func_t *cur;
786 if (!is_object(iface->attrs) || !funcs)
787 return;
789 LIST_FOR_EACH_ENTRY(cur, funcs, const func_t, entry) {
790 const var_t *def = cur->def;
791 const var_t *cas = is_callas(def->attrs);
793 if (cas) {
794 const func_t *m;
795 LIST_FOR_EACH_ENTRY(m, iface->funcs, const func_t, entry)
796 if (!strcmp(m->def->name, cas->name))
797 break;
798 if (&m->entry != iface->funcs) {
799 const var_t *mdef = m->def;
800 /* proxy prototype - use local prototype */
801 write_type_decl_left(fp, get_func_return_type(m));
802 fprintf(fp, " CALLBACK %s_", iface->name);
803 write_name(fp, mdef);
804 fprintf(fp, "_Proxy(\n");
805 write_args(fp, m->args, iface->name, 1, TRUE);
806 fprintf(fp, ")");
807 if (body) {
808 type_t *rt = get_func_return_type(m);
809 fprintf(fp, "\n{\n");
810 fprintf(fp, " %s\n", comment);
811 if (rt->name && strcmp(rt->name, "HRESULT") == 0)
812 fprintf(fp, " return E_NOTIMPL;\n");
813 else if (rt->type) {
814 fprintf(fp, " ");
815 write_type_decl(fp, rt, "rv");
816 fprintf(fp, ";\n");
817 fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
818 fprintf(fp, " return rv;\n");
820 fprintf(fp, "}\n\n");
822 else
823 fprintf(fp, ";\n");
824 /* stub prototype - use remotable prototype */
825 write_type_decl_left(fp, get_func_return_type(cur));
826 fprintf(fp, " __RPC_STUB %s_", iface->name);
827 write_name(fp, mdef);
828 fprintf(fp, "_Stub(\n");
829 write_args(fp, cur->args, iface->name, 1, TRUE);
830 fprintf(fp, ")");
831 if (body)
832 /* Remotable methods must all return HRESULTs. */
833 fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
834 else
835 fprintf(fp, ";\n");
837 else
838 error_loc("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
843 static void write_function_proto(FILE *header, const type_t *iface, const func_t *fun, const char *prefix)
845 var_t *def = fun->def;
846 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
848 /* FIXME: do we need to handle call_as? */
849 write_type_decl_left(header, get_func_return_type(fun));
850 fprintf(header, " ");
851 if (callconv) fprintf(header, "%s ", callconv);
852 write_prefix_name(header, prefix, def);
853 fprintf(header, "(\n");
854 if (fun->args)
855 write_args(header, fun->args, iface->name, 0, TRUE);
856 else
857 fprintf(header, " void");
858 fprintf(header, ");\n\n");
861 static void write_function_protos(FILE *header, const type_t *iface)
863 const func_t *cur;
864 int prefixes_differ = strcmp(prefix_client, prefix_server);
866 if (!iface->funcs) return;
867 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
869 if (prefixes_differ) {
870 fprintf(header, "/* client prototype */\n");
871 write_function_proto(header, iface, cur, prefix_client);
872 fprintf(header, "/* server prototype */\n");
874 write_function_proto(header, iface, cur, prefix_server);
878 void write_forward(type_t *iface)
880 /* C/C++ forwards should only be written for object interfaces, so if we
881 * have a full definition we only write one if we find [object] among the
882 * attributes - however, if we don't have a full definition at this point
883 * (i.e. this is an IDL forward), then we also assume that it is an object
884 * interface, since non-object interfaces shouldn't need forwards */
885 if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
886 && !iface->written) {
887 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
888 fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
889 fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
890 fprintf(header, "#endif\n\n" );
891 iface->written = TRUE;
895 static void write_iface_guid(FILE *header, const type_t *iface)
897 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
898 write_guid(header, "IID", iface->name, uuid);
901 static void write_dispiface_guid(FILE *header, const type_t *iface)
903 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
904 write_guid(header, "DIID", iface->name, uuid);
907 static void write_coclass_guid(FILE *header, const type_t *cocl)
909 const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
910 write_guid(header, "CLSID", cocl->name, uuid);
913 static void write_com_interface_start(FILE *header, const type_t *iface)
915 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
916 fprintf(header, "/*****************************************************************************\n");
917 fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
918 fprintf(header, " */\n");
919 fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
920 fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
923 static void write_com_interface_end(FILE *header, type_t *iface)
925 int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
926 if (dispinterface)
927 write_dispiface_guid(header, iface);
928 else
929 write_iface_guid(header, iface);
930 /* C++ interface */
931 fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
932 if (iface->ref)
934 fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
935 fprintf(header, "{\n");
937 else
939 fprintf(header, "interface %s\n", iface->name);
940 fprintf(header, "{\n");
941 fprintf(header, " BEGIN_INTERFACE\n");
942 fprintf(header, "\n");
944 /* dispinterfaces don't have real functions, so don't write C++ functions for
945 * them */
946 if (!dispinterface)
948 indentation++;
949 write_cpp_method_def(header, iface);
950 indentation--;
952 if (!iface->ref)
953 fprintf(header, " END_INTERFACE\n");
954 fprintf(header, "};\n");
955 fprintf(header, "#else\n");
956 /* C interface */
957 fprintf(header, "typedef struct %sVtbl {\n", iface->name);
958 indentation++;
959 fprintf(header, " BEGIN_INTERFACE\n");
960 fprintf(header, "\n");
961 if (dispinterface)
962 write_c_disp_method_def(header, iface);
963 else
964 write_c_method_def(header, iface);
965 indentation--;
966 fprintf(header, " END_INTERFACE\n");
967 fprintf(header, "} %sVtbl;\n", iface->name);
968 fprintf(header, "interface %s {\n", iface->name);
969 fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
970 fprintf(header, "};\n");
971 fprintf(header, "\n");
972 fprintf(header, "#ifdef COBJMACROS\n");
973 /* dispinterfaces don't have real functions, so don't write macros for them,
974 * only for the interface this interface inherits from, i.e. IDispatch */
975 write_method_macro(header, dispinterface ? iface->ref : iface, iface->name);
976 fprintf(header, "#endif\n");
977 fprintf(header, "\n");
978 fprintf(header, "#endif\n");
979 fprintf(header, "\n");
980 /* dispinterfaces don't have real functions, so don't write prototypes for
981 * them */
982 if (!dispinterface)
984 write_method_proto(header, iface);
985 write_locals(header, iface, FALSE);
986 fprintf(header, "\n");
988 fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
991 static void write_rpc_interface_start(FILE *header, const type_t *iface)
993 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
994 const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
995 static int allocate_written = 0;
997 if (!allocate_written)
999 allocate_written = 1;
1000 fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
1001 fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
1004 fprintf(header, "/*****************************************************************************\n");
1005 fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1006 fprintf(header, " */\n");
1007 fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1008 fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1009 if (var) fprintf(header, "extern handle_t %s;\n", var);
1010 if (old_names)
1012 fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1013 fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1015 else
1017 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1018 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1019 fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1020 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1024 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1026 fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1029 void write_interface(type_t *iface)
1031 if (is_attr(iface->attrs, ATTR_DISPINTERFACE) || is_object(iface->attrs))
1033 write_com_interface_start(header, iface);
1034 write_com_interface_end(header, iface);
1036 else
1038 write_rpc_interface_start(header, iface);
1039 write_function_protos(header, iface);
1040 write_rpc_interface_end(header, iface);
1044 void write_coclass(type_t *cocl)
1046 fprintf(header, "/*****************************************************************************\n");
1047 fprintf(header, " * %s coclass\n", cocl->name);
1048 fprintf(header, " */\n\n");
1049 write_coclass_guid(header, cocl);
1050 fprintf(header, "\n");
1053 void write_coclass_forward(type_t *cocl)
1055 fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1056 fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1057 fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1058 fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1061 void write_import(const char *fname)
1063 char *hname, *p;
1065 hname = dup_basename(fname, ".idl");
1066 p = hname + strlen(hname) - 2;
1067 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1069 fprintf(header, "#include <%s>\n", hname);
1070 free(hname);