Reorder the virtual table definition so it's defined before it is used
[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(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(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(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(type_t *t, 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, 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, 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 char* get_name(var_t *v)
114 {
115   return v->name;
116 }
117
118 static void write_array(FILE *h, 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, 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_t");
223         break;
224       case RPC_FC_USHORT:
225       case RPC_FC_SHORT:
226         if (t->ref) fprintf(h, t->ref->name);
227         else fprintf(h, "short");
228         break;
229       case RPC_FC_ULONG:
230       case RPC_FC_LONG:
231         if (t->ref) fprintf(h, t->ref->name);
232         else fprintf(h, "long");
233         break;
234       case RPC_FC_HYPER:
235         if (t->ref) fprintf(h, t->ref->name);
236         else fprintf(h, "hyper");
237         break;
238       case RPC_FC_FLOAT:
239         fprintf(h, "float");
240         break;
241       case RPC_FC_DOUBLE:
242         fprintf(h, "double");
243         break;
244       case RPC_FC_ENUM16:
245       case RPC_FC_ENUM32:
246         if (t->defined && !t->written) {
247           if (t->name) fprintf(h, "enum %s {\n", t->name);
248           else fprintf(h, "enum {\n");
249           t->written = TRUE;
250           indentation++;
251           write_enums(h, t->fields);
252           indent(h, -1);
253           fprintf(h, "}");
254         }
255         else fprintf(h, "enum %s", t->name);
256         break;
257       case RPC_FC_ERROR_STATUS_T:
258         if (t->ref) fprintf(h, t->ref->name);
259         else fprintf(h, "error_status_t");
260         break;
261       case RPC_FC_BIND_PRIMITIVE:
262         if (t->ref) fprintf(h, t->ref->name);
263         else fprintf(h, "handle_t");
264         break;
265       case RPC_FC_STRUCT:
266       case RPC_FC_CVSTRUCT:
267       case RPC_FC_CPSTRUCT:
268       case RPC_FC_CSTRUCT:
269       case RPC_FC_PSTRUCT:
270       case RPC_FC_BOGUS_STRUCT:
271       case RPC_FC_ENCAPSULATED_UNION:
272         if (t->defined && !t->written) {
273           if (t->name) fprintf(h, "struct %s {\n", t->name);
274           else fprintf(h, "struct {\n");
275           t->written = TRUE;
276           indentation++;
277           write_fields(h, t->fields);
278           indent(h, -1);
279           fprintf(h, "}");
280         }
281         else fprintf(h, "struct %s", t->name);
282         break;
283       case RPC_FC_NON_ENCAPSULATED_UNION:
284         if (t->defined && !t->written) {
285           if (t->name) fprintf(h, "union %s {\n", t->name);
286           else fprintf(h, "union {\n");
287           t->written = TRUE;
288           indentation++;
289           write_fields(h, t->fields);
290           indent(h, -1);
291           fprintf(h, "}");
292         }
293         else fprintf(h, "union %s", t->name);
294         break;
295       default:
296         fprintf(h, "(unknown-type:%d)", t->type);
297       }
298     }
299     else {
300       if (t->ref) {
301         write_type(h, t->ref, NULL, t->name);
302       }
303       else fprintf(h, "void");
304     }
305   }
306   if (v) {
307     for (c=0; c<v->ptr_level; c++) {
308       fprintf(h, "*");
309     }
310   }
311 }
312
313
314 struct user_type
315 {
316     struct user_type *next;
317     char name[1];
318 };
319
320 static struct user_type *user_type_list;
321
322 static int user_type_registered(const char *name)
323 {
324   struct user_type *ut;
325   for (ut = user_type_list; ut; ut = ut->next)
326     if (!strcmp(name, ut->name))
327         return 1;
328   return 0;
329 }
330
331 static void check_for_user_types(var_t *v)
332 {
333   while (v) {
334     type_t *type = v->type;
335     const char *name = v->tname;
336     for (type = v->type; type; type = type->ref) {
337       if (type->user_types_registered) continue;
338       type->user_types_registered = 1;
339       if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
340         if (!user_type_registered(name))
341         {
342           struct user_type *ut = xmalloc(sizeof(struct user_type) + strlen(name));
343           strcpy(ut->name, name);
344           ut->next = user_type_list;
345           user_type_list = ut;
346         }
347         /* don't carry on parsing fields within this type as we are already
348          * using a wire marshaled type */
349         break;
350       }
351       else if (type->fields)
352       {
353         var_t *fields = type->fields;
354         while (NEXT_LINK(fields)) fields = NEXT_LINK(fields);
355         check_for_user_types(fields);
356       }
357       /* the wire_marshal attribute is always at least one reference away
358        * from the name of the type, so update it after the rest of the
359        * processing above */
360       if (type->name) name = type->name;
361     }
362     v = PREV_LINK(v);
363   }
364 }
365
366 void write_user_types(void)
367 {
368   struct user_type *ut;
369   for (ut = user_type_list; ut; ut = ut->next)
370   {
371     const char *name = ut->name;
372     fprintf(header, "unsigned long   __RPC_USER %s_UserSize     (unsigned long *, unsigned long,   %s *);\n", name, name);
373     fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal  (unsigned long *, unsigned char *, %s *);\n", name, name);
374     fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(unsigned long *, unsigned char *, %s *);\n", name, name);
375     fprintf(header, "void            __RPC_USER %s_UserFree     (unsigned long *, %s *);\n", name, name);
376   }
377 }
378
379 void write_typedef(type_t *type, var_t *names)
380 {
381   char *tname = names->tname;
382   var_t *lname;
383   while (NEXT_LINK(names)) names = NEXT_LINK(names);
384   lname = names;
385   fprintf(header, "typedef ");
386   write_type(header, type, NULL, tname);
387   fprintf(header, " ");
388   while (names) {
389     write_pident(header, names);
390     if (PREV_LINK(names))
391       fprintf(header, ", ");
392     names = PREV_LINK(names);
393   }
394   fprintf(header, ";\n");
395 }
396
397 static void do_write_expr(FILE *h, expr_t *e, int p)
398 {
399   switch (e->type) {
400   case EXPR_VOID:
401     break;
402   case EXPR_NUM:
403     fprintf(h, "%ld", e->u.lval);
404     break;
405   case EXPR_HEXNUM:
406     fprintf(h, "0x%lx", e->u.lval);
407     break;
408   case EXPR_IDENTIFIER:
409     fprintf(h, "%s", e->u.sval);
410     break;
411   case EXPR_NEG:
412     fprintf(h, "-");
413     do_write_expr(h, e->ref, 1);
414     break;
415   case EXPR_NOT:
416     fprintf(h, "~");
417     do_write_expr(h, e->ref, 1);
418     break;
419   case EXPR_PPTR:
420     fprintf(h, "*");
421     do_write_expr(h, e->ref, 1);
422     break;
423   case EXPR_CAST:
424     fprintf(h, "(");
425     write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
426     fprintf(h, ")");
427     do_write_expr(h, e->ref, 1);
428     break;
429   case EXPR_SIZEOF:
430     fprintf(h, "sizeof(");
431     write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
432     fprintf(h, ")");
433     break;
434   case EXPR_SHL:
435   case EXPR_SHR:
436   case EXPR_MUL:
437   case EXPR_DIV:
438   case EXPR_ADD:
439   case EXPR_SUB:
440   case EXPR_AND:
441   case EXPR_OR:
442     if (p) fprintf(h, "(");
443     do_write_expr(h, e->ref, 1);
444     switch (e->type) {
445     case EXPR_SHL: fprintf(h, " << "); break;
446     case EXPR_SHR: fprintf(h, " >> "); break;
447     case EXPR_MUL: fprintf(h, " * "); break;
448     case EXPR_DIV: fprintf(h, " / "); break;
449     case EXPR_ADD: fprintf(h, " + "); break;
450     case EXPR_SUB: fprintf(h, " - "); break;
451     case EXPR_AND: fprintf(h, " & "); break;
452     case EXPR_OR:  fprintf(h, " | "); break;
453     default: break;
454     }
455     do_write_expr(h, e->u.ext, 1);
456     if (p) fprintf(h, ")");
457     break;
458   case EXPR_COND:
459     if (p) fprintf(h, "(");
460     do_write_expr(h, e->ref, 1);
461     fprintf(h, " ? ");
462     do_write_expr(h, e->u.ext, 1);
463     fprintf(h, " : ");
464     do_write_expr(h, e->ext2, 1);
465     if (p) fprintf(h, ")");
466     break;
467   }
468 }
469
470 void write_expr(FILE *h, expr_t *e)
471 {
472   do_write_expr(h, e, 0);
473 }
474
475 void write_constdef(var_t *v)
476 {
477   fprintf(header, "#define %s (", get_name(v));
478   write_expr(header, v->eval);
479   fprintf(header, ")\n\n");
480 }
481
482 void write_externdef(var_t *v)
483 {
484   fprintf(header, "extern const ");
485   write_type(header, v->type, NULL, v->tname);
486   if (get_name(v)) {
487     fprintf(header, " ");
488     write_pident(header, v);
489   }
490   fprintf(header, ";\n\n");
491 }
492
493 void write_library(const char *name, attr_t *attr) {
494   UUID *uuid = get_attrp(attr, ATTR_UUID);
495   fprintf(header, "\n");
496   write_guid("LIBID", name, uuid);
497   fprintf(header, "\n");
498 }
499
500 /********** INTERFACES **********/
501
502 int is_object(attr_t *a)
503 {
504   while (a) {
505     if (a->type == ATTR_OBJECT || a->type == ATTR_ODL) return 1;
506     a = NEXT_LINK(a);
507   }
508   return 0;
509 }
510
511 int is_local(attr_t *a)
512 {
513   return is_attr(a, ATTR_LOCAL);
514 }
515
516 var_t *is_callas(attr_t *a)
517 {
518   return get_attrp(a, ATTR_CALLAS);
519 }
520
521 static int write_method_macro(type_t *iface, char *name)
522 {
523   int idx;
524   func_t *cur = iface->funcs;
525
526   if (iface->ref) idx = write_method_macro(iface->ref, name);
527   else idx = 0;
528
529   if (!cur) return idx;
530   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
531
532   fprintf(header, "/*** %s methods ***/\n", iface->name);
533   while (cur) {
534     var_t *def = cur->def;
535     if (!is_callas(def->attrs)) {
536       var_t *arg = cur->args;
537       int argc = 0;
538       int c;
539       while (arg) {
540         arg = NEXT_LINK(arg);
541         argc++;
542       }
543
544       fprintf(header, "#define %s_", name);
545       write_name(header,def);
546       fprintf(header, "(p");
547       for (c=0; c<argc; c++)
548         fprintf(header, ",%c", c+'a');
549       fprintf(header, ") ");
550
551       fprintf(header, "(p)->lpVtbl->");
552       write_name(header, def);
553       fprintf(header, "(p");
554       for (c=0; c<argc; c++)
555         fprintf(header, ",%c", c+'a');
556       fprintf(header, ")\n");
557       if (cur->idx == -1) cur->idx = idx;
558       else if (cur->idx != idx) yyerror("BUG: method index mismatch in write_method_macro");
559       idx++;
560     }
561     cur = PREV_LINK(cur);
562   }
563   return idx;
564 }
565
566 void write_args(FILE *h, var_t *arg, const char *name, int method, int do_indent)
567 {
568   int count = 0;
569   if (arg) {
570     while (NEXT_LINK(arg))
571       arg = NEXT_LINK(arg);
572   }
573   if (do_indent)
574   {
575       indentation++;
576       indent(h, 0);
577   }
578   if (method == 1) {
579     fprintf(h, "%s* This", name);
580     count++;
581   }
582   while (arg) {
583     if (count) {
584         if (do_indent)
585         {
586             fprintf(h, ",\n");
587             indent(h, 0);
588         }
589         else fprintf(h, ",");
590     }
591     write_type(h, arg->type, arg, arg->tname);
592     if (arg->args)
593     {
594       fprintf(h, " (STDMETHODCALLTYPE *");
595       write_name(h,arg);
596       fprintf(h, ")(");
597       write_args(h, arg->args, NULL, 0, FALSE);
598       fprintf(h, ")");
599     }
600     else
601     {
602       fprintf(h, " ");
603       write_name(h, arg);
604     }
605     write_array(h, arg->array, 0);
606     arg = PREV_LINK(arg);
607     count++;
608   }
609   if (do_indent) indentation--;
610 }
611
612 static void write_cpp_method_def(type_t *iface)
613 {
614   func_t *cur = iface->funcs;
615
616   if (!cur) return;
617   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
618   while (cur) {
619     var_t *def = cur->def;
620     if (!is_callas(def->attrs)) {
621       indent(header, 0);
622       fprintf(header, "virtual ");
623       write_type(header, def->type, def, def->tname);
624       fprintf(header, " STDMETHODCALLTYPE ");
625       write_name(header, def);
626       fprintf(header, "(\n");
627       write_args(header, cur->args, iface->name, 2, TRUE);
628       fprintf(header, ") = 0;\n");
629       fprintf(header, "\n");
630     }
631     cur = PREV_LINK(cur);
632   }
633 }
634
635 static void do_write_c_method_def(type_t *iface, char *name)
636 {
637   func_t *cur = iface->funcs;
638
639   if (iface->ref) do_write_c_method_def(iface->ref, name);
640
641   if (!cur) return;
642   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
643   indent(header, 0);
644   fprintf(header, "/*** %s methods ***/\n", iface->name);
645   while (cur) {
646     var_t *def = cur->def;
647     if (!is_callas(def->attrs)) {
648       indent(header, 0);
649       write_type(header, def->type, def, def->tname);
650       fprintf(header, " (STDMETHODCALLTYPE *");
651       write_name(header, def);
652       fprintf(header, ")(\n");
653       write_args(header, cur->args, name, 1, TRUE);
654       fprintf(header, ");\n");
655       fprintf(header, "\n");
656     }
657     cur = PREV_LINK(cur);
658   }
659 }
660
661 static void write_c_method_def(type_t *iface)
662 {
663   do_write_c_method_def(iface, iface->name);
664 }
665
666 static void write_c_disp_method_def(type_t *iface)
667 {
668   do_write_c_method_def(iface->ref, iface->name);
669 }
670
671 static void write_method_proto(type_t *iface)
672 {
673   func_t *cur = iface->funcs;
674
675   if (!cur) return;
676   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
677   while (cur) {
678     var_t *def = cur->def;
679     var_t *cas = is_callas(def->attrs);
680     var_t *args;
681     if (!is_local(def->attrs)) {
682       /* proxy prototype */
683       write_type(header, def->type, def, def->tname);
684       fprintf(header, " CALLBACK %s_", iface->name);
685       write_name(header, def);
686       fprintf(header, "_Proxy(\n");
687       write_args(header, cur->args, iface->name, 1, TRUE);
688       fprintf(header, ");\n");
689       /* stub prototype */
690       fprintf(header, "void __RPC_STUB %s_", iface->name);
691       write_name(header,def);
692       fprintf(header, "_Stub(\n");
693       fprintf(header, "    interface IRpcStubBuffer* This,\n");
694       fprintf(header, "    interface IRpcChannelBuffer* pRpcChannelBuffer,\n");
695       fprintf(header, "    PRPC_MESSAGE pRpcMessage,\n");
696       fprintf(header, "    DWORD* pdwStubPhase);\n");
697
698       args = cur->args;
699       if (args) {
700         while (NEXT_LINK(args))
701           args = NEXT_LINK(args);
702       }
703       check_for_user_types(args);
704     }
705     if (cas) {
706       func_t *m = iface->funcs;
707       while (m && strcmp(get_name(m->def), cas->name))
708         m = NEXT_LINK(m);
709       if (m) {
710         var_t *mdef = m->def;
711         /* proxy prototype - use local prototype */
712         write_type(header, mdef->type, mdef, mdef->tname);
713         fprintf(header, " CALLBACK %s_", iface->name);
714         write_name(header, mdef);
715         fprintf(header, "_Proxy(\n");
716         write_args(header, m->args, iface->name, 1, TRUE);
717         fprintf(header, ");\n");
718         /* stub prototype - use remotable prototype */
719         write_type(header, def->type, def, def->tname);
720         fprintf(header, " __RPC_STUB %s_", iface->name);
721         write_name(header, mdef);
722         fprintf(header, "_Stub(\n");
723         write_args(header, cur->args, iface->name, 1, TRUE);
724         fprintf(header, ");\n");
725       }
726       else {
727         yywarning("invalid call_as attribute (%s -> %s)\n", get_name(def), cas->name);
728       }
729     }
730
731     cur = PREV_LINK(cur);
732   }
733 }
734
735 static void write_function_proto(type_t *iface)
736 {
737   func_t *cur = iface->funcs;
738   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
739   while (cur) {
740     var_t *def = cur->def;
741     /* FIXME: do we need to handle call_as? */
742     write_type(header, def->type, def, def->tname);
743     fprintf(header, " ");
744     write_name(header, def);
745     fprintf(header, "(\n");
746     if (cur->args)
747       write_args(header, cur->args, iface->name, 0, TRUE);
748     else
749       fprintf(header, "    void");
750     fprintf(header, ");\n");
751
752     cur = PREV_LINK(cur);
753   }
754 }
755
756 void write_forward(type_t *iface)
757 {
758   /* C/C++ forwards should only be written for object interfaces, so if we
759    * have a full definition we only write one if we find [object] among the
760    * attributes - however, if we don't have a full definition at this point
761    * (i.e. this is an IDL forward), then we also assume that it is an object
762    * interface, since non-object interfaces shouldn't need forwards */
763   if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
764         && !iface->written) {
765     fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
766     fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
767     fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
768     fprintf(header, "#endif\n\n" );
769     iface->written = TRUE;
770   }
771 }
772
773 static void write_iface_guid(type_t *iface)
774 {
775   UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
776   write_guid("IID", iface->name, uuid);
777
778
779 static void write_dispiface_guid(type_t *iface)
780 {
781   UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
782   write_guid("DIID", iface->name, uuid);
783 }
784
785 static void write_coclass_guid(class_t *cocl)
786 {
787   UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
788   write_guid("CLSID", cocl->name, uuid);
789 }
790
791 static void write_com_interface(type_t *iface)
792 {
793   if (!iface->funcs && !iface->ref) {
794     yywarning("%s has no methods", iface->name);
795     return;
796   }
797
798   fprintf(header, "/*****************************************************************************\n");
799   fprintf(header, " * %s interface\n", iface->name);
800   fprintf(header, " */\n");
801   fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
802   fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
803   write_iface_guid(iface);
804   write_forward(iface);
805   /* C++ interface */
806   fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
807   if (iface->ref)
808   {
809       fprintf(header, "%s : public %s\n", iface->name, iface->ref->name);
810       fprintf(header, "{\n");
811       indentation++;
812       write_cpp_method_def(iface);
813       indentation--;
814       fprintf(header, "};\n");
815   }
816   else
817   {
818       fprintf(header, "%s\n", iface->name);
819       fprintf(header, "{\n");
820       fprintf(header, "    BEGIN_INTERFACE\n");
821       fprintf(header, "\n");
822       indentation++;
823       write_cpp_method_def(iface);
824       indentation--;
825       fprintf(header, "    END_INTERFACE\n");
826       fprintf(header, "};\n");
827   }
828   fprintf(header, "#else\n");
829   /* C interface */
830   fprintf(header, "typedef struct %sVtbl {\n", iface->name);
831   indentation++;
832   fprintf(header, "    BEGIN_INTERFACE\n");
833   fprintf(header, "\n");
834   write_c_method_def(iface);
835   indentation--;
836   fprintf(header, "    END_INTERFACE\n");
837   fprintf(header, "} %sVtbl;\n", iface->name);
838   fprintf(header, "interface %s {\n", iface->name);
839   fprintf(header, "    const %sVtbl* lpVtbl;\n", iface->name);
840   fprintf(header, "};\n");
841   fprintf(header, "\n");
842   fprintf(header, "#ifdef COBJMACROS\n");
843   write_method_macro(iface, iface->name);
844   fprintf(header, "#endif\n");
845   fprintf(header, "\n");
846   fprintf(header, "#endif\n");
847   fprintf(header, "\n");
848   write_method_proto(iface);
849   fprintf(header,"\n#endif  /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
850 }
851
852 static void write_rpc_interface(type_t *iface)
853 {
854   unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
855   char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
856
857   if (!iface->funcs) return;
858
859   fprintf(header, "/*****************************************************************************\n");
860   fprintf(header, " * %s interface (v%d.%d)\n", iface->name, LOWORD(ver), HIWORD(ver));
861   fprintf(header, " */\n");
862   write_iface_guid(iface);
863   if (var) fprintf(header, "extern handle_t %s;\n", var);
864   fprintf(header, "extern RPC_IF_HANDLE %s_v%d_%d_c_ifspec;\n", iface->name, LOWORD(ver), HIWORD(ver));
865   fprintf(header, "extern RPC_IF_HANDLE %s_v%d_%d_s_ifspec;\n", iface->name, LOWORD(ver), HIWORD(ver));
866   write_function_proto(iface);
867   fprintf(header, "\n");
868
869   /* FIXME: server/client code */
870 }
871
872 void write_interface(type_t *iface)
873 {
874   if (is_object(iface->attrs))
875     write_com_interface(iface);
876   else
877     write_rpc_interface(iface);
878 }
879
880 void write_dispinterface(type_t *iface)
881 {
882   fprintf(header, "/*****************************************************************************\n");
883   fprintf(header, " * %s dispinterface\n", iface->name);
884   fprintf(header, " */\n");
885   fprintf(header,"#ifndef __%s_DISPINTERFACE_DEFINED__\n", iface->name);
886   fprintf(header,"#define __%s_DISPINTERFACE_DEFINED__\n\n", iface->name);
887   write_dispiface_guid(iface);
888   write_forward(iface);
889   /* C++ interface */
890   fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
891   fprintf(header, "%s : public %s\n", iface->name, iface->ref->name);
892   fprintf(header, "{\n");
893   fprintf(header, "};\n");
894   fprintf(header, "#else\n");
895   /* C interface */
896   fprintf(header, "typedef struct %sVtbl {\n", iface->name);
897   indentation++;
898   fprintf(header, "    BEGIN_INTERFACE\n");
899   fprintf(header, "\n");
900   write_c_disp_method_def(iface);
901   indentation--;
902   fprintf(header, "    END_INTERFACE\n");
903   fprintf(header, "} %sVtbl;\n", iface->name);
904   fprintf(header, "interface %s {\n", iface->name);
905   fprintf(header, "    const %sVtbl* lpVtbl;\n", iface->name);
906   fprintf(header, "};\n");
907   fprintf(header, "\n");
908   fprintf(header, "#ifdef COBJMACROS\n");
909   write_method_macro(iface->ref, iface->name);
910   fprintf(header, "#endif\n");
911   fprintf(header, "\n");
912   fprintf(header, "#endif\n");
913   fprintf(header, "\n");
914   fprintf(header,"#endif  /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
915 }
916
917 void write_coclass(class_t *cocl)
918 {
919   fprintf(header, "/*****************************************************************************\n");
920   fprintf(header, " * %s coclass\n", cocl->name);
921   fprintf(header, " */\n\n");
922   write_coclass_guid(cocl);
923   fprintf(header, "\n");
924 }