widl: Out parameters.
[wine] / tools / widl / header.c
1 /*
2  * IDL Compiler
3  *
4  * Copyright 2002 Ove Kaaven
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <string.h>
29 #include <assert.h>
30 #include <ctype.h>
31 #include <signal.h>
32
33 #include "windef.h"
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
38
39 static int indentation = 0;
40
41 static void indent(FILE *h, int delta)
42 {
43   int c;
44   if (delta < 0) indentation += delta;
45   for (c=0; c<indentation; c++) fprintf(h, "    ");
46   if (delta > 0) indentation += delta;
47 }
48
49 int is_attr(const attr_t *a, enum attr_type t)
50 {
51   while (a) {
52     if (a->type == t) return 1;
53     a = NEXT_LINK(a);
54   }
55   return 0;
56 }
57
58 void *get_attrp(const attr_t *a, enum attr_type t)
59 {
60   while (a) {
61     if (a->type == t) return a->u.pval;
62     a = NEXT_LINK(a);
63   }
64   return NULL;
65 }
66
67 unsigned long get_attrv(const attr_t *a, enum attr_type t)
68 {
69   while (a) {
70     if (a->type == t) return a->u.ival;
71     a = NEXT_LINK(a);
72   }
73   return 0;
74 }
75
76 int is_void(const type_t *t, const var_t *v)
77 {
78   if (v && v->ptr_level) return 0;
79   if (!t->type && !t->ref) return 1;
80   return 0;
81 }
82
83 static void write_guid(const char *guid_prefix, const char *name, UUID *uuid)
84 {
85   if (!uuid) return;
86   fprintf(header, "DEFINE_GUID(%s_%s, 0x%08lx, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
87         "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
88         guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
89         uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
90         uuid->Data4[6], uuid->Data4[7]);
91 }
92
93 static void write_pident(FILE *h, const var_t *v)
94 {
95   int c;
96   for (c=0; c<v->ptr_level; c++) {
97     fprintf(h, "*");
98   }
99   if (v->name) fprintf(h, "%s", v->name);
100 }
101
102 void write_name(FILE *h, const var_t *v)
103 {
104   if (is_attr( v->attrs, ATTR_PROPGET ))
105     fprintf(h, "get_" );
106   else if (is_attr( v->attrs, ATTR_PROPPUT ))
107     fprintf(h, "put_" );
108   else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
109     fprintf(h, "putref_" );
110   fprintf(h, "%s", v->name);
111 }
112
113 const char* get_name(const var_t *v)
114 {
115   return v->name;
116 }
117
118 void write_array(FILE *h, const expr_t *v, int field)
119 {
120   if (!v) return;
121   while (NEXT_LINK(v)) v = NEXT_LINK(v);
122   fprintf(h, "[");
123   while (v) {
124     if (v->is_const)
125       fprintf(h, "%ld", v->cval); /* statically sized array */
126     else
127       if (field) fprintf(h, "1"); /* dynamically sized array */
128     if (PREV_LINK(v))
129       fprintf(h, ", ");
130     v = PREV_LINK(v);
131   }
132   fprintf(h, "]");
133 }
134
135 static void write_field(FILE *h, var_t *v)
136 {
137   if (!v) return;
138   if (v->type) {
139     indent(h, 0);
140     write_type(h, v->type, NULL, v->tname);
141     if (get_name(v)) {
142       fprintf(h, " ");
143       write_pident(h, v);
144     }
145     else {
146       /* not all C/C++ compilers support anonymous structs and unions */
147       switch (v->type->type) {
148       case RPC_FC_STRUCT:
149       case RPC_FC_CVSTRUCT:
150       case RPC_FC_CPSTRUCT:
151       case RPC_FC_CSTRUCT:
152       case RPC_FC_PSTRUCT:
153       case RPC_FC_BOGUS_STRUCT:
154       case RPC_FC_ENCAPSULATED_UNION:
155         fprintf(h, " DUMMYSTRUCTNAME");
156         break;
157       case RPC_FC_NON_ENCAPSULATED_UNION:
158         fprintf(h, " DUMMYUNIONNAME");
159         break;
160       default:
161         /* ? */
162         break;
163       }
164     }
165     write_array(h, v->array, 1);
166     fprintf(h, ";\n");
167   }
168 }
169
170 static void write_fields(FILE *h, var_t *v)
171 {
172   var_t *first = v;
173   if (!v) return;
174   while (NEXT_LINK(v)) v = NEXT_LINK(v);
175   while (v) {
176     write_field(h, v);
177     if (v == first) break;
178     v = PREV_LINK(v);
179   }
180 }
181
182 static void write_enums(FILE *h, var_t *v)
183 {
184   if (!v) return;
185   while (NEXT_LINK(v)) v = NEXT_LINK(v);
186   while (v) {
187     if (get_name(v)) {
188       indent(h, 0);
189       write_name(h, v);
190       if (v->eval) {
191         fprintf(h, " = ");
192         write_expr(h, v->eval);
193       }
194     }
195     if (PREV_LINK(v))
196       fprintf(h, ",\n");
197     v = PREV_LINK(v);
198   }
199   fprintf(h, "\n");
200 }
201
202 void write_type(FILE *h, type_t *t, const var_t *v, const char *n)
203 {
204   int c;
205
206   if (n) fprintf(h, "%s", n);
207   else {
208     if (t->is_const) fprintf(h, "const ");
209     if (t->type) {
210       if (t->sign > 0) fprintf(h, "signed ");
211       else if (t->sign < 0) fprintf(h, "unsigned ");
212       switch (t->type) {
213       case RPC_FC_BYTE:
214         if (t->ref) fprintf(h, t->ref->name);
215         else fprintf(h, "byte");
216         break;
217       case RPC_FC_CHAR:
218         if (t->ref) fprintf(h, t->ref->name);
219         else fprintf(h, "char");
220         break;
221       case RPC_FC_WCHAR:
222         fprintf(h, "WCHAR");
223         break;
224       case RPC_FC_USMALL:
225       case RPC_FC_SMALL:
226         if (t->ref) fprintf(h, t->ref->name);
227         else fprintf(h, "small");
228         break;
229       case RPC_FC_USHORT:
230       case RPC_FC_SHORT:
231         if (t->ref) fprintf(h, t->ref->name);
232         else fprintf(h, "short");
233         break;
234       case RPC_FC_ULONG:
235       case RPC_FC_LONG:
236         if (t->ref) fprintf(h, t->ref->name);
237         else fprintf(h, "long");
238         break;
239       case RPC_FC_HYPER:
240         if (t->ref) fprintf(h, t->ref->name);
241         else fprintf(h, "hyper");
242         break;
243       case RPC_FC_FLOAT:
244         fprintf(h, "float");
245         break;
246       case RPC_FC_DOUBLE:
247         fprintf(h, "double");
248         break;
249       case RPC_FC_ENUM16:
250       case RPC_FC_ENUM32:
251         if (t->defined && !t->written) {
252           if (t->name) fprintf(h, "enum %s {\n", t->name);
253           else fprintf(h, "enum {\n");
254           t->written = TRUE;
255           indentation++;
256           write_enums(h, t->fields);
257           indent(h, -1);
258           fprintf(h, "}");
259         }
260         else fprintf(h, "enum %s", t->name);
261         break;
262       case RPC_FC_ERROR_STATUS_T:
263         if (t->ref) fprintf(h, t->ref->name);
264         else fprintf(h, "error_status_t");
265         break;
266       case RPC_FC_BIND_PRIMITIVE:
267         if (t->ref) fprintf(h, t->ref->name);
268         else fprintf(h, "handle_t");
269         break;
270       case RPC_FC_STRUCT:
271       case RPC_FC_CVSTRUCT:
272       case RPC_FC_CPSTRUCT:
273       case RPC_FC_CSTRUCT:
274       case RPC_FC_PSTRUCT:
275       case RPC_FC_BOGUS_STRUCT:
276       case RPC_FC_ENCAPSULATED_UNION:
277         if (t->defined && !t->written) {
278           if (t->name) fprintf(h, "struct %s {\n", t->name);
279           else fprintf(h, "struct {\n");
280           t->written = TRUE;
281           indentation++;
282           write_fields(h, t->fields);
283           indent(h, -1);
284           fprintf(h, "}");
285         }
286         else fprintf(h, "struct %s", t->name);
287         break;
288       case RPC_FC_NON_ENCAPSULATED_UNION:
289         if (t->defined && !t->written) {
290           if (t->name) fprintf(h, "union %s {\n", t->name);
291           else fprintf(h, "union {\n");
292           t->written = TRUE;
293           indentation++;
294           write_fields(h, t->fields);
295           indent(h, -1);
296           fprintf(h, "}");
297         }
298         else fprintf(h, "union %s", t->name);
299         break;
300       default:
301         fprintf(h, "(unknown-type:%d)", t->type);
302       }
303     }
304     else {
305       if (t->ref) {
306         write_type(h, t->ref, NULL, t->name);
307       }
308       else fprintf(h, "void");
309     }
310   }
311   if (v) {
312     for (c=0; c<v->ptr_level; c++) {
313       fprintf(h, "*");
314     }
315   }
316 }
317
318
319 struct user_type
320 {
321     struct user_type *next;
322     char name[1];
323 };
324
325 static struct user_type *user_type_list;
326
327 static int user_type_registered(const char *name)
328 {
329   struct user_type *ut;
330   for (ut = user_type_list; ut; ut = ut->next)
331     if (!strcmp(name, ut->name))
332         return 1;
333   return 0;
334 }
335
336 static void check_for_user_types(var_t *v)
337 {
338   while (v) {
339     type_t *type = v->type;
340     const char *name = v->tname;
341     for (type = v->type; type; type = type->ref) {
342       if (type->user_types_registered) continue;
343       type->user_types_registered = 1;
344       if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
345         if (!user_type_registered(name))
346         {
347           struct user_type *ut = xmalloc(sizeof(struct user_type) + strlen(name));
348           strcpy(ut->name, name);
349           ut->next = user_type_list;
350           user_type_list = ut;
351         }
352         /* don't carry on parsing fields within this type as we are already
353          * using a wire marshaled type */
354         break;
355       }
356       else if (type->fields)
357       {
358         var_t *fields = type->fields;
359         while (NEXT_LINK(fields)) fields = NEXT_LINK(fields);
360         check_for_user_types(fields);
361       }
362       /* the wire_marshal attribute is always at least one reference away
363        * from the name of the type, so update it after the rest of the
364        * processing above */
365       if (type->name) name = type->name;
366     }
367     v = PREV_LINK(v);
368   }
369 }
370
371 void write_user_types(void)
372 {
373   struct user_type *ut;
374   for (ut = user_type_list; ut; ut = ut->next)
375   {
376     const char *name = ut->name;
377     fprintf(header, "unsigned long   __RPC_USER %s_UserSize     (unsigned long *, unsigned long,   %s *);\n", name, name);
378     fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal  (unsigned long *, unsigned char *, %s *);\n", name, name);
379     fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(unsigned long *, unsigned char *, %s *);\n", name, name);
380     fprintf(header, "void            __RPC_USER %s_UserFree     (unsigned long *, %s *);\n", name, name);
381   }
382 }
383
384 void write_typedef(type_t *type, const var_t *names)
385 {
386   char *tname = names->tname;
387   const var_t *lname;
388   while (NEXT_LINK(names)) names = NEXT_LINK(names);
389   lname = names;
390   fprintf(header, "typedef ");
391   write_type(header, type, NULL, tname);
392   fprintf(header, " ");
393   while (names) {
394     write_pident(header, names);
395     if (PREV_LINK(names))
396       fprintf(header, ", ");
397     names = PREV_LINK(names);
398   }
399   fprintf(header, ";\n");
400 }
401
402 static void do_write_expr(FILE *h, expr_t *e, int p)
403 {
404   switch (e->type) {
405   case EXPR_VOID:
406     break;
407   case EXPR_NUM:
408     fprintf(h, "%ld", e->u.lval);
409     break;
410   case EXPR_HEXNUM:
411     fprintf(h, "0x%lx", e->u.lval);
412     break;
413   case EXPR_IDENTIFIER:
414     fprintf(h, "%s", e->u.sval);
415     break;
416   case EXPR_NEG:
417     fprintf(h, "-");
418     do_write_expr(h, e->ref, 1);
419     break;
420   case EXPR_NOT:
421     fprintf(h, "~");
422     do_write_expr(h, e->ref, 1);
423     break;
424   case EXPR_PPTR:
425     fprintf(h, "*");
426     do_write_expr(h, e->ref, 1);
427     break;
428   case EXPR_CAST:
429     fprintf(h, "(");
430     write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
431     fprintf(h, ")");
432     do_write_expr(h, e->ref, 1);
433     break;
434   case EXPR_SIZEOF:
435     fprintf(h, "sizeof(");
436     write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
437     fprintf(h, ")");
438     break;
439   case EXPR_SHL:
440   case EXPR_SHR:
441   case EXPR_MUL:
442   case EXPR_DIV:
443   case EXPR_ADD:
444   case EXPR_SUB:
445   case EXPR_AND:
446   case EXPR_OR:
447     if (p) fprintf(h, "(");
448     do_write_expr(h, e->ref, 1);
449     switch (e->type) {
450     case EXPR_SHL: fprintf(h, " << "); break;
451     case EXPR_SHR: fprintf(h, " >> "); break;
452     case EXPR_MUL: fprintf(h, " * "); break;
453     case EXPR_DIV: fprintf(h, " / "); break;
454     case EXPR_ADD: fprintf(h, " + "); break;
455     case EXPR_SUB: fprintf(h, " - "); break;
456     case EXPR_AND: fprintf(h, " & "); break;
457     case EXPR_OR:  fprintf(h, " | "); break;
458     default: break;
459     }
460     do_write_expr(h, e->u.ext, 1);
461     if (p) fprintf(h, ")");
462     break;
463   case EXPR_COND:
464     if (p) fprintf(h, "(");
465     do_write_expr(h, e->ref, 1);
466     fprintf(h, " ? ");
467     do_write_expr(h, e->u.ext, 1);
468     fprintf(h, " : ");
469     do_write_expr(h, e->ext2, 1);
470     if (p) fprintf(h, ")");
471     break;
472   }
473 }
474
475 void write_expr(FILE *h, expr_t *e)
476 {
477   do_write_expr(h, e, 0);
478 }
479
480 void write_constdef(const var_t *v)
481 {
482   fprintf(header, "#define %s (", get_name(v));
483   write_expr(header, v->eval);
484   fprintf(header, ")\n\n");
485 }
486
487 void write_externdef(const var_t *v)
488 {
489   fprintf(header, "extern const ");
490   write_type(header, v->type, NULL, v->tname);
491   if (get_name(v)) {
492     fprintf(header, " ");
493     write_pident(header, v);
494   }
495   fprintf(header, ";\n\n");
496 }
497
498 void write_library(const char *name, attr_t *attr) {
499   UUID *uuid = get_attrp(attr, ATTR_UUID);
500   fprintf(header, "\n");
501   write_guid("LIBID", name, uuid);
502   fprintf(header, "\n");
503 }
504
505
506 var_t* get_explicit_handle_var(func_t* func)
507 {
508     var_t* var;
509
510     if (!func->args)
511         return NULL;
512
513     var = func->args;
514     while (NEXT_LINK(var)) var = NEXT_LINK(var);
515     while (var)
516     {
517         if (var->type->type == RPC_FC_IGNORE)
518             return var;
519
520         var = PREV_LINK(var);
521     }
522
523     return NULL;
524 }
525
526
527 /********** INTERFACES **********/
528
529 int is_object(attr_t *a)
530 {
531   while (a) {
532     if (a->type == ATTR_OBJECT || a->type == ATTR_ODL) return 1;
533     a = NEXT_LINK(a);
534   }
535   return 0;
536 }
537
538 int is_local(attr_t *a)
539 {
540   return is_attr(a, ATTR_LOCAL);
541 }
542
543 var_t *is_callas(attr_t *a)
544 {
545   return get_attrp(a, ATTR_CALLAS);
546 }
547
548 static int write_method_macro(const type_t *iface, const char *name)
549 {
550   int idx;
551   func_t *cur = iface->funcs;
552
553   if (iface->ref) idx = write_method_macro(iface->ref, name);
554   else idx = 0;
555
556   if (!cur) return idx;
557   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
558
559   fprintf(header, "/*** %s methods ***/\n", iface->name);
560   while (cur) {
561     var_t *def = cur->def;
562     if (!is_callas(def->attrs)) {
563       var_t *arg = cur->args;
564       int argc = 0;
565       int c;
566       while (arg) {
567         arg = NEXT_LINK(arg);
568         argc++;
569       }
570
571       fprintf(header, "#define %s_", name);
572       write_name(header,def);
573       fprintf(header, "(p");
574       for (c=0; c<argc; c++)
575         fprintf(header, ",%c", c+'a');
576       fprintf(header, ") ");
577
578       fprintf(header, "(p)->lpVtbl->");
579       write_name(header, def);
580       fprintf(header, "(p");
581       for (c=0; c<argc; c++)
582         fprintf(header, ",%c", c+'a');
583       fprintf(header, ")\n");
584       if (cur->idx == -1) cur->idx = idx;
585       else if (cur->idx != idx) yyerror("BUG: method index mismatch in write_method_macro");
586       idx++;
587     }
588     cur = PREV_LINK(cur);
589   }
590   return idx;
591 }
592
593 void write_args(FILE *h, var_t *arg, const char *name, int method, int do_indent)
594 {
595   int count = 0;
596   if (arg) {
597     while (NEXT_LINK(arg))
598       arg = NEXT_LINK(arg);
599   }
600   if (do_indent)
601   {
602       indentation++;
603       indent(h, 0);
604   }
605   if (method == 1) {
606     fprintf(h, "%s* This", name);
607     count++;
608   }
609   while (arg) {
610     if (count) {
611         if (do_indent)
612         {
613             fprintf(h, ",\n");
614             indent(h, 0);
615         }
616         else fprintf(h, ",");
617     }
618     write_type(h, arg->type, arg, arg->tname);
619     if (arg->args)
620     {
621       fprintf(h, " (STDMETHODCALLTYPE *");
622       write_name(h,arg);
623       fprintf(h, ")(");
624       write_args(h, arg->args, NULL, 0, FALSE);
625       fprintf(h, ")");
626     }
627     else
628     {
629       fprintf(h, " ");
630       write_name(h, arg);
631     }
632     write_array(h, arg->array, 0);
633     arg = PREV_LINK(arg);
634     count++;
635   }
636   if (do_indent) indentation--;
637 }
638
639 static void write_cpp_method_def(const type_t *iface)
640 {
641   func_t *cur = iface->funcs;
642
643   if (!cur) return;
644   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
645   while (cur) {
646     var_t *def = cur->def;
647     if (!is_callas(def->attrs)) {
648       indent(header, 0);
649       fprintf(header, "virtual ");
650       write_type(header, def->type, def, def->tname);
651       fprintf(header, " STDMETHODCALLTYPE ");
652       write_name(header, def);
653       fprintf(header, "(\n");
654       write_args(header, cur->args, iface->name, 2, TRUE);
655       fprintf(header, ") = 0;\n");
656       fprintf(header, "\n");
657     }
658     cur = PREV_LINK(cur);
659   }
660 }
661
662 static void do_write_c_method_def(const type_t *iface, char *name)
663 {
664   func_t *cur = iface->funcs;
665
666   if (iface->ref) do_write_c_method_def(iface->ref, name);
667
668   if (!cur) return;
669   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
670   indent(header, 0);
671   fprintf(header, "/*** %s methods ***/\n", iface->name);
672   while (cur) {
673     var_t *def = cur->def;
674     if (!is_callas(def->attrs)) {
675       indent(header, 0);
676       write_type(header, def->type, def, def->tname);
677       fprintf(header, " (STDMETHODCALLTYPE *");
678       write_name(header, def);
679       fprintf(header, ")(\n");
680       write_args(header, cur->args, name, 1, TRUE);
681       fprintf(header, ");\n");
682       fprintf(header, "\n");
683     }
684     cur = PREV_LINK(cur);
685   }
686 }
687
688 static void write_c_method_def(const type_t *iface)
689 {
690   do_write_c_method_def(iface, iface->name);
691 }
692
693 static void write_c_disp_method_def(const type_t *iface)
694 {
695   do_write_c_method_def(iface->ref, iface->name);
696 }
697
698 static void write_method_proto(const type_t *iface)
699 {
700   func_t *cur = iface->funcs;
701
702   if (!cur) return;
703   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
704   while (cur) {
705     var_t *def = cur->def;
706     var_t *cas = is_callas(def->attrs);
707     var_t *args;
708     if (!is_local(def->attrs)) {
709       /* proxy prototype */
710       write_type(header, def->type, def, def->tname);
711       fprintf(header, " CALLBACK %s_", iface->name);
712       write_name(header, def);
713       fprintf(header, "_Proxy(\n");
714       write_args(header, cur->args, iface->name, 1, TRUE);
715       fprintf(header, ");\n");
716       /* stub prototype */
717       fprintf(header, "void __RPC_STUB %s_", iface->name);
718       write_name(header,def);
719       fprintf(header, "_Stub(\n");
720       fprintf(header, "    IRpcStubBuffer* This,\n");
721       fprintf(header, "    IRpcChannelBuffer* pRpcChannelBuffer,\n");
722       fprintf(header, "    PRPC_MESSAGE pRpcMessage,\n");
723       fprintf(header, "    DWORD* pdwStubPhase);\n");
724
725       args = cur->args;
726       if (args) {
727         while (NEXT_LINK(args))
728           args = NEXT_LINK(args);
729       }
730       check_for_user_types(args);
731     }
732     if (cas) {
733       func_t *m = iface->funcs;
734       while (m && strcmp(get_name(m->def), cas->name))
735         m = NEXT_LINK(m);
736       if (m) {
737         var_t *mdef = m->def;
738         /* proxy prototype - use local prototype */
739         write_type(header, mdef->type, mdef, mdef->tname);
740         fprintf(header, " CALLBACK %s_", iface->name);
741         write_name(header, mdef);
742         fprintf(header, "_Proxy(\n");
743         write_args(header, m->args, iface->name, 1, TRUE);
744         fprintf(header, ");\n");
745         /* stub prototype - use remotable prototype */
746         write_type(header, def->type, def, def->tname);
747         fprintf(header, " __RPC_STUB %s_", iface->name);
748         write_name(header, mdef);
749         fprintf(header, "_Stub(\n");
750         write_args(header, cur->args, iface->name, 1, TRUE);
751         fprintf(header, ");\n");
752       }
753       else {
754         yywarning("invalid call_as attribute (%s -> %s)\n", get_name(def), cas->name);
755       }
756     }
757
758     cur = PREV_LINK(cur);
759   }
760 }
761
762 static void write_function_proto(const type_t *iface)
763 {
764   char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
765   int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
766   var_t* explicit_handle_var;
767
768   func_t *cur = iface->funcs;
769   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
770   while (cur) {
771     var_t *def = cur->def;
772
773     /* check for a defined binding handle */
774     explicit_handle_var = get_explicit_handle_var(cur);
775     if (explicit_handle) {
776       if (!explicit_handle_var) {
777         error("%s() does not define an explicit binding handle!\n", def->name);
778         return;
779       }
780     } else if (implicit_handle) {
781       if (explicit_handle_var) {
782         error("%s() must not define a binding handle!\n", def->name);
783         return;
784       }
785     }
786
787     /* FIXME: do we need to handle call_as? */
788     write_type(header, def->type, def, def->tname);
789     fprintf(header, " ");
790     write_name(header, def);
791     fprintf(header, "(\n");
792     if (cur->args)
793       write_args(header, cur->args, iface->name, 0, TRUE);
794     else
795       fprintf(header, "    void");
796     fprintf(header, ");\n");
797
798     cur = PREV_LINK(cur);
799   }
800 }
801
802 void write_forward(type_t *iface)
803 {
804   /* C/C++ forwards should only be written for object interfaces, so if we
805    * have a full definition we only write one if we find [object] among the
806    * attributes - however, if we don't have a full definition at this point
807    * (i.e. this is an IDL forward), then we also assume that it is an object
808    * interface, since non-object interfaces shouldn't need forwards */
809   if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
810         && !iface->written) {
811     fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
812     fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
813     fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
814     fprintf(header, "#endif\n\n" );
815     iface->written = TRUE;
816   }
817 }
818
819 static void write_iface_guid(const type_t *iface)
820 {
821   UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
822   write_guid("IID", iface->name, uuid);
823
824
825 static void write_dispiface_guid(const type_t *iface)
826 {
827   UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
828   write_guid("DIID", iface->name, uuid);
829 }
830
831 static void write_coclass_guid(class_t *cocl)
832 {
833   UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
834   write_guid("CLSID", cocl->name, uuid);
835 }
836
837 static void write_com_interface(type_t *iface)
838 {
839   if (!iface->funcs && !iface->ref) {
840     yywarning("%s has no methods", iface->name);
841     return;
842   }
843
844   fprintf(header, "/*****************************************************************************\n");
845   fprintf(header, " * %s interface\n", iface->name);
846   fprintf(header, " */\n");
847   fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
848   fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
849   write_iface_guid(iface);
850   write_forward(iface);
851   /* C++ interface */
852   fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
853   if (iface->ref)
854   {
855       fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
856       fprintf(header, "{\n");
857       indentation++;
858       write_cpp_method_def(iface);
859       indentation--;
860       fprintf(header, "};\n");
861   }
862   else
863   {
864       fprintf(header, "interface %s\n", iface->name);
865       fprintf(header, "{\n");
866       fprintf(header, "    BEGIN_INTERFACE\n");
867       fprintf(header, "\n");
868       indentation++;
869       write_cpp_method_def(iface);
870       indentation--;
871       fprintf(header, "    END_INTERFACE\n");
872       fprintf(header, "};\n");
873   }
874   fprintf(header, "#else\n");
875   /* C interface */
876   fprintf(header, "typedef struct %sVtbl {\n", iface->name);
877   indentation++;
878   fprintf(header, "    BEGIN_INTERFACE\n");
879   fprintf(header, "\n");
880   write_c_method_def(iface);
881   indentation--;
882   fprintf(header, "    END_INTERFACE\n");
883   fprintf(header, "} %sVtbl;\n", iface->name);
884   fprintf(header, "interface %s {\n", iface->name);
885   fprintf(header, "    const %sVtbl* lpVtbl;\n", iface->name);
886   fprintf(header, "};\n");
887   fprintf(header, "\n");
888   fprintf(header, "#ifdef COBJMACROS\n");
889   write_method_macro(iface, iface->name);
890   fprintf(header, "#endif\n");
891   fprintf(header, "\n");
892   fprintf(header, "#endif\n");
893   fprintf(header, "\n");
894   write_method_proto(iface);
895   fprintf(header,"\n#endif  /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
896 }
897
898 static void write_rpc_interface(const type_t *iface)
899 {
900   unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
901   char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
902
903   if (!iface->funcs) return;
904
905   fprintf(header, "/*****************************************************************************\n");
906   fprintf(header, " * %s interface (v%d.%d)\n", iface->name, LOWORD(ver), HIWORD(ver));
907   fprintf(header, " */\n");
908   write_iface_guid(iface);
909   if (var) fprintf(header, "extern handle_t %s;\n", var);
910   fprintf(header, "extern RPC_IF_HANDLE %s_v%d_%d_c_ifspec;\n", iface->name, LOWORD(ver), HIWORD(ver));
911   fprintf(header, "extern RPC_IF_HANDLE %s_v%d_%d_s_ifspec;\n", iface->name, LOWORD(ver), HIWORD(ver));
912   write_function_proto(iface);
913   fprintf(header, "\n");
914
915   /* FIXME: server/client code */
916 }
917
918 void write_interface(type_t *iface)
919 {
920   if (is_object(iface->attrs))
921     write_com_interface(iface);
922   else
923     write_rpc_interface(iface);
924 }
925
926 void write_dispinterface(type_t *iface)
927 {
928   fprintf(header, "/*****************************************************************************\n");
929   fprintf(header, " * %s dispinterface\n", iface->name);
930   fprintf(header, " */\n");
931   fprintf(header,"#ifndef __%s_DISPINTERFACE_DEFINED__\n", iface->name);
932   fprintf(header,"#define __%s_DISPINTERFACE_DEFINED__\n\n", iface->name);
933   write_dispiface_guid(iface);
934   write_forward(iface);
935   /* C++ interface */
936   fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
937   fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
938   fprintf(header, "{\n");
939   fprintf(header, "};\n");
940   fprintf(header, "#else\n");
941   /* C interface */
942   fprintf(header, "typedef struct %sVtbl {\n", iface->name);
943   indentation++;
944   fprintf(header, "    BEGIN_INTERFACE\n");
945   fprintf(header, "\n");
946   write_c_disp_method_def(iface);
947   indentation--;
948   fprintf(header, "    END_INTERFACE\n");
949   fprintf(header, "} %sVtbl;\n", iface->name);
950   fprintf(header, "interface %s {\n", iface->name);
951   fprintf(header, "    const %sVtbl* lpVtbl;\n", iface->name);
952   fprintf(header, "};\n");
953   fprintf(header, "\n");
954   fprintf(header, "#ifdef COBJMACROS\n");
955   write_method_macro(iface->ref, iface->name);
956   fprintf(header, "#endif\n");
957   fprintf(header, "\n");
958   fprintf(header, "#endif\n");
959   fprintf(header, "\n");
960   fprintf(header,"#endif  /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
961 }
962
963 void write_coclass(class_t *cocl)
964 {
965   fprintf(header, "/*****************************************************************************\n");
966   fprintf(header, " * %s coclass\n", cocl->name);
967   fprintf(header, " */\n\n");
968   write_coclass_guid(cocl);
969   fprintf(header, "\n");
970 }