widl: Fix incorrect version number in generated code.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
84 {
85   if (!uuid) return;
86   fprintf(f, "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, 0);
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 && !t->ignore) {
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 && !t->ignore) {
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 && !t->ignore) {
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       case RPC_FC_FP:
301         if (t->ref) write_type(h, t->ref, NULL, t->name);
302         fprintf(h, "*");
303         break;
304       default:
305         fprintf(h, "(unknown-type:%d)", t->type);
306       }
307     }
308     else {
309       if (t->ref) {
310         write_type(h, t->ref, NULL, t->name);
311       }
312       else fprintf(h, "void");
313     }
314   }
315   if (v) {
316     for (c=0; c<v->ptr_level; c++) {
317       fprintf(h, "*");
318     }
319   }
320 }
321
322
323 struct user_type
324 {
325     struct user_type *next;
326     char name[1];
327 };
328
329 static struct user_type *user_type_list;
330
331 static int user_type_registered(const char *name)
332 {
333   struct user_type *ut;
334   for (ut = user_type_list; ut; ut = ut->next)
335     if (!strcmp(name, ut->name))
336         return 1;
337   return 0;
338 }
339
340 static void check_for_user_types(const var_t *v)
341 {
342   while (v) {
343     type_t *type = v->type;
344     const char *name = v->tname;
345     for (type = v->type; type; type = type->ref) {
346       if (type->user_types_registered) continue;
347       type->user_types_registered = 1;
348       if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
349         if (!user_type_registered(name))
350         {
351           struct user_type *ut = xmalloc(sizeof(struct user_type) + strlen(name));
352           strcpy(ut->name, name);
353           ut->next = user_type_list;
354           user_type_list = ut;
355         }
356         /* don't carry on parsing fields within this type as we are already
357          * using a wire marshaled type */
358         break;
359       }
360       else if (type->fields)
361       {
362         const var_t *fields = type->fields;
363         while (NEXT_LINK(fields)) fields = NEXT_LINK(fields);
364         check_for_user_types(fields);
365       }
366       /* the wire_marshal attribute is always at least one reference away
367        * from the name of the type, so update it after the rest of the
368        * processing above */
369       if (type->name) name = type->name;
370     }
371     v = PREV_LINK(v);
372   }
373 }
374
375 void write_user_types(void)
376 {
377   struct user_type *ut;
378   for (ut = user_type_list; ut; ut = ut->next)
379   {
380     const char *name = ut->name;
381     fprintf(header, "unsigned long   __RPC_USER %s_UserSize     (unsigned long *, unsigned long,   %s *);\n", name, name);
382     fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal  (unsigned long *, unsigned char *, %s *);\n", name, name);
383     fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(unsigned long *, unsigned char *, %s *);\n", name, name);
384     fprintf(header, "void            __RPC_USER %s_UserFree     (unsigned long *, %s *);\n", name, name);
385   }
386 }
387
388 void write_typedef(type_t *type, const var_t *names)
389 {
390   const char *tname = names->tname;
391   const var_t *lname;
392   while (NEXT_LINK(names)) names = NEXT_LINK(names);
393   lname = names;
394   fprintf(header, "typedef ");
395   write_type(header, type, NULL, tname);
396   fprintf(header, " ");
397   while (names) {
398     write_pident(header, names);
399     if (PREV_LINK(names))
400       fprintf(header, ", ");
401     names = PREV_LINK(names);
402   }
403   fprintf(header, ";\n");
404 }
405
406 void write_expr(FILE *h, const expr_t *e, int brackets)
407 {
408   switch (e->type) {
409   case EXPR_VOID:
410     break;
411   case EXPR_NUM:
412     fprintf(h, "%ld", e->u.lval);
413     break;
414   case EXPR_HEXNUM:
415     fprintf(h, "0x%lx", e->u.lval);
416     break;
417   case EXPR_TRUEFALSE:
418     if (e->u.lval == 0)
419       fprintf(h, "FALSE");
420     else
421       fprintf(h, "TRUE");
422     break;
423   case EXPR_IDENTIFIER:
424     fprintf(h, "%s", e->u.sval);
425     break;
426   case EXPR_NEG:
427     fprintf(h, "-");
428     write_expr(h, e->ref, 1);
429     break;
430   case EXPR_NOT:
431     fprintf(h, "~");
432     write_expr(h, e->ref, 1);
433     break;
434   case EXPR_PPTR:
435     fprintf(h, "*");
436     write_expr(h, e->ref, 1);
437     break;
438   case EXPR_CAST:
439     fprintf(h, "(");
440     write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
441     fprintf(h, ")");
442     write_expr(h, e->ref, 1);
443     break;
444   case EXPR_SIZEOF:
445     fprintf(h, "sizeof(");
446     write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
447     fprintf(h, ")");
448     break;
449   case EXPR_SHL:
450   case EXPR_SHR:
451   case EXPR_MUL:
452   case EXPR_DIV:
453   case EXPR_ADD:
454   case EXPR_SUB:
455   case EXPR_AND:
456   case EXPR_OR:
457     if (brackets) fprintf(h, "(");
458     write_expr(h, e->ref, 1);
459     switch (e->type) {
460     case EXPR_SHL: fprintf(h, " << "); break;
461     case EXPR_SHR: fprintf(h, " >> "); break;
462     case EXPR_MUL: fprintf(h, " * "); break;
463     case EXPR_DIV: fprintf(h, " / "); break;
464     case EXPR_ADD: fprintf(h, " + "); break;
465     case EXPR_SUB: fprintf(h, " - "); break;
466     case EXPR_AND: fprintf(h, " & "); break;
467     case EXPR_OR:  fprintf(h, " | "); break;
468     default: break;
469     }
470     write_expr(h, e->u.ext, 1);
471     if (brackets) fprintf(h, ")");
472     break;
473   case EXPR_COND:
474     if (brackets) fprintf(h, "(");
475     write_expr(h, e->ref, 1);
476     fprintf(h, " ? ");
477     write_expr(h, e->u.ext, 1);
478     fprintf(h, " : ");
479     write_expr(h, e->ext2, 1);
480     if (brackets) fprintf(h, ")");
481     break;
482   }
483 }
484
485 void write_constdef(const var_t *v)
486 {
487   fprintf(header, "#define %s (", get_name(v));
488   write_expr(header, v->eval, 0);
489   fprintf(header, ")\n\n");
490 }
491
492 void write_externdef(const var_t *v)
493 {
494   fprintf(header, "extern const ");
495   write_type(header, v->type, NULL, v->tname);
496   if (get_name(v)) {
497     fprintf(header, " ");
498     write_pident(header, v);
499   }
500   fprintf(header, ";\n\n");
501 }
502
503 void write_library(const char *name, const attr_t *attr) {
504   const UUID *uuid = get_attrp(attr, ATTR_UUID);
505   fprintf(header, "\n");
506   write_guid(header, "LIBID", name, uuid);
507   fprintf(header, "\n");
508 }
509
510
511 const var_t* get_explicit_handle_var(const func_t* func)
512 {
513     const var_t* var;
514
515     if (!func->args)
516         return NULL;
517
518     var = func->args;
519     while (NEXT_LINK(var)) var = NEXT_LINK(var);
520     while (var)
521     {
522         if (var->type->type == RPC_FC_BIND_PRIMITIVE)
523             return var;
524
525         var = PREV_LINK(var);
526     }
527
528     return NULL;
529 }
530
531 int has_out_arg_or_return(const func_t *func)
532 {
533     var_t *var;
534
535     if (!is_void(func->def->type, NULL))
536         return 1;
537
538     if (!func->args)
539         return 0;
540
541     var = func->args;
542     while (NEXT_LINK(var)) var = NEXT_LINK(var);
543     while (var)
544     {
545         if (is_attr(var->attrs, ATTR_OUT))
546             return 1;
547
548         var = PREV_LINK(var);
549     }
550     return 0;
551 }
552
553
554 /********** INTERFACES **********/
555
556 int is_object(const attr_t *a)
557 {
558   while (a) {
559     if (a->type == ATTR_OBJECT || a->type == ATTR_ODL) return 1;
560     a = NEXT_LINK(a);
561   }
562   return 0;
563 }
564
565 int is_local(const attr_t *a)
566 {
567   return is_attr(a, ATTR_LOCAL);
568 }
569
570 const var_t *is_callas(const attr_t *a)
571 {
572   return get_attrp(a, ATTR_CALLAS);
573 }
574
575 static int write_method_macro(const type_t *iface, const char *name)
576 {
577   int idx;
578   func_t *cur = iface->funcs;
579
580   if (iface->ref) idx = write_method_macro(iface->ref, name);
581   else idx = 0;
582
583   if (!cur) return idx;
584   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
585
586   fprintf(header, "/*** %s methods ***/\n", iface->name);
587   while (cur) {
588     var_t *def = cur->def;
589     if (!is_callas(def->attrs)) {
590       var_t *arg = cur->args;
591       int argc = 0;
592       int c;
593       while (arg) {
594         arg = NEXT_LINK(arg);
595         argc++;
596       }
597
598       fprintf(header, "#define %s_", name);
599       write_name(header,def);
600       fprintf(header, "(p");
601       for (c=0; c<argc; c++)
602         fprintf(header, ",%c", c+'a');
603       fprintf(header, ") ");
604
605       fprintf(header, "(p)->lpVtbl->");
606       write_name(header, def);
607       fprintf(header, "(p");
608       for (c=0; c<argc; c++)
609         fprintf(header, ",%c", c+'a');
610       fprintf(header, ")\n");
611       if (cur->idx == -1) cur->idx = idx;
612       else if (cur->idx != idx) yyerror("BUG: method index mismatch in write_method_macro");
613       idx++;
614     }
615     cur = PREV_LINK(cur);
616   }
617   return idx;
618 }
619
620 void write_args(FILE *h, var_t *arg, const char *name, int method, int do_indent)
621 {
622   int count = 0;
623   if (arg) {
624     while (NEXT_LINK(arg))
625       arg = NEXT_LINK(arg);
626   }
627   if (do_indent)
628   {
629       indentation++;
630       indent(h, 0);
631   }
632   if (method == 1) {
633     fprintf(h, "%s* This", name);
634     count++;
635   }
636   while (arg) {
637     if (count) {
638         if (do_indent)
639         {
640             fprintf(h, ",\n");
641             indent(h, 0);
642         }
643         else fprintf(h, ",");
644     }
645     write_type(h, arg->type, arg, arg->tname);
646     if (arg->args)
647     {
648       fprintf(h, " (STDMETHODCALLTYPE *");
649       write_name(h,arg);
650       fprintf(h, ")(");
651       write_args(h, arg->args, NULL, 0, FALSE);
652       fprintf(h, ")");
653     }
654     else
655     {
656       fprintf(h, " ");
657       write_name(h, arg);
658     }
659     write_array(h, arg->array, 0);
660     arg = PREV_LINK(arg);
661     count++;
662   }
663   if (do_indent) indentation--;
664 }
665
666 static void write_cpp_method_def(const type_t *iface)
667 {
668   func_t *cur = iface->funcs;
669
670   if (!cur) return;
671   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
672   while (cur) {
673     var_t *def = cur->def;
674     if (!is_callas(def->attrs)) {
675       indent(header, 0);
676       fprintf(header, "virtual ");
677       write_type(header, def->type, def, def->tname);
678       fprintf(header, " STDMETHODCALLTYPE ");
679       write_name(header, def);
680       fprintf(header, "(\n");
681       write_args(header, cur->args, iface->name, 2, TRUE);
682       fprintf(header, ") = 0;\n");
683       fprintf(header, "\n");
684     }
685     cur = PREV_LINK(cur);
686   }
687 }
688
689 static void do_write_c_method_def(const type_t *iface, const char *name)
690 {
691   const func_t *cur = iface->funcs;
692
693   if (iface->ref) do_write_c_method_def(iface->ref, name);
694
695   if (!cur) return;
696   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
697   indent(header, 0);
698   fprintf(header, "/*** %s methods ***/\n", iface->name);
699   while (cur) {
700     const var_t *def = cur->def;
701     if (!is_callas(def->attrs)) {
702       indent(header, 0);
703       write_type(header, def->type, def, def->tname);
704       fprintf(header, " (STDMETHODCALLTYPE *");
705       write_name(header, def);
706       fprintf(header, ")(\n");
707       write_args(header, cur->args, name, 1, TRUE);
708       fprintf(header, ");\n");
709       fprintf(header, "\n");
710     }
711     cur = PREV_LINK(cur);
712   }
713 }
714
715 static void write_c_method_def(const type_t *iface)
716 {
717   do_write_c_method_def(iface, iface->name);
718 }
719
720 static void write_c_disp_method_def(const type_t *iface)
721 {
722   do_write_c_method_def(iface->ref, iface->name);
723 }
724
725 static void write_method_proto(const type_t *iface)
726 {
727   const func_t *cur = iface->funcs;
728
729   if (!cur) return;
730   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
731   while (cur) {
732     const var_t *def = cur->def;
733     const var_t *cas = is_callas(def->attrs);
734     const var_t *args;
735     if (!is_local(def->attrs)) {
736       /* proxy prototype */
737       write_type(header, def->type, def, def->tname);
738       fprintf(header, " CALLBACK %s_", iface->name);
739       write_name(header, def);
740       fprintf(header, "_Proxy(\n");
741       write_args(header, cur->args, iface->name, 1, TRUE);
742       fprintf(header, ");\n");
743       /* stub prototype */
744       fprintf(header, "void __RPC_STUB %s_", iface->name);
745       write_name(header,def);
746       fprintf(header, "_Stub(\n");
747       fprintf(header, "    IRpcStubBuffer* This,\n");
748       fprintf(header, "    IRpcChannelBuffer* pRpcChannelBuffer,\n");
749       fprintf(header, "    PRPC_MESSAGE pRpcMessage,\n");
750       fprintf(header, "    DWORD* pdwStubPhase);\n");
751
752       args = cur->args;
753       if (args) {
754         while (NEXT_LINK(args))
755           args = NEXT_LINK(args);
756       }
757       check_for_user_types(args);
758     }
759     if (cas) {
760       const func_t *m = iface->funcs;
761       while (m && strcmp(get_name(m->def), cas->name))
762         m = NEXT_LINK(m);
763       if (m) {
764         const var_t *mdef = m->def;
765         /* proxy prototype - use local prototype */
766         write_type(header, mdef->type, mdef, mdef->tname);
767         fprintf(header, " CALLBACK %s_", iface->name);
768         write_name(header, mdef);
769         fprintf(header, "_Proxy(\n");
770         write_args(header, m->args, iface->name, 1, TRUE);
771         fprintf(header, ");\n");
772         /* stub prototype - use remotable prototype */
773         write_type(header, def->type, def, def->tname);
774         fprintf(header, " __RPC_STUB %s_", iface->name);
775         write_name(header, mdef);
776         fprintf(header, "_Stub(\n");
777         write_args(header, cur->args, iface->name, 1, TRUE);
778         fprintf(header, ");\n");
779       }
780       else {
781         yywarning("invalid call_as attribute (%s -> %s)\n", get_name(def), cas->name);
782       }
783     }
784
785     cur = PREV_LINK(cur);
786   }
787 }
788
789 static void write_function_proto(const type_t *iface)
790 {
791   const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
792   int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
793   const var_t* explicit_handle_var;
794
795   func_t *cur = iface->funcs;
796   while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
797   while (cur) {
798     var_t *def = cur->def;
799
800     /* check for a defined binding handle */
801     explicit_handle_var = get_explicit_handle_var(cur);
802     if (explicit_handle) {
803       if (!explicit_handle_var) {
804         error("%s() does not define an explicit binding handle!\n", def->name);
805         return;
806       }
807     } else if (implicit_handle) {
808       if (explicit_handle_var) {
809         error("%s() must not define a binding handle!\n", def->name);
810         return;
811       }
812     }
813
814     /* FIXME: do we need to handle call_as? */
815     write_type(header, def->type, def, def->tname);
816     fprintf(header, " ");
817     write_name(header, def);
818     fprintf(header, "(\n");
819     if (cur->args)
820       write_args(header, cur->args, iface->name, 0, TRUE);
821     else
822       fprintf(header, "    void");
823     fprintf(header, ");\n");
824
825     cur = PREV_LINK(cur);
826   }
827 }
828
829 void write_forward(type_t *iface)
830 {
831   /* C/C++ forwards should only be written for object interfaces, so if we
832    * have a full definition we only write one if we find [object] among the
833    * attributes - however, if we don't have a full definition at this point
834    * (i.e. this is an IDL forward), then we also assume that it is an object
835    * interface, since non-object interfaces shouldn't need forwards */
836   if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
837         && !iface->written) {
838     fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
839     fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
840     fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
841     fprintf(header, "#endif\n\n" );
842     iface->written = TRUE;
843   }
844 }
845
846 static void write_iface_guid(const type_t *iface)
847 {
848   const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
849   write_guid(header, "IID", iface->name, uuid);
850
851
852 static void write_dispiface_guid(const type_t *iface)
853 {
854   const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
855   write_guid(header, "DIID", iface->name, uuid);
856 }
857
858 static void write_coclass_guid(type_t *cocl)
859 {
860   const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
861   write_guid(header, "CLSID", cocl->name, uuid);
862 }
863
864 static void write_com_interface(type_t *iface)
865 {
866   if (!iface->funcs && !iface->ref) {
867     yywarning("%s has no methods", iface->name);
868     return;
869   }
870
871   fprintf(header, "/*****************************************************************************\n");
872   fprintf(header, " * %s interface\n", iface->name);
873   fprintf(header, " */\n");
874   fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
875   fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
876   write_iface_guid(iface);
877   write_forward(iface);
878   /* C++ interface */
879   fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
880   if (iface->ref)
881   {
882       fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
883       fprintf(header, "{\n");
884       indentation++;
885       write_cpp_method_def(iface);
886       indentation--;
887       fprintf(header, "};\n");
888   }
889   else
890   {
891       fprintf(header, "interface %s\n", iface->name);
892       fprintf(header, "{\n");
893       fprintf(header, "    BEGIN_INTERFACE\n");
894       fprintf(header, "\n");
895       indentation++;
896       write_cpp_method_def(iface);
897       indentation--;
898       fprintf(header, "    END_INTERFACE\n");
899       fprintf(header, "};\n");
900   }
901   fprintf(header, "#else\n");
902   /* C interface */
903   fprintf(header, "typedef struct %sVtbl {\n", iface->name);
904   indentation++;
905   fprintf(header, "    BEGIN_INTERFACE\n");
906   fprintf(header, "\n");
907   write_c_method_def(iface);
908   indentation--;
909   fprintf(header, "    END_INTERFACE\n");
910   fprintf(header, "} %sVtbl;\n", iface->name);
911   fprintf(header, "interface %s {\n", iface->name);
912   fprintf(header, "    const %sVtbl* lpVtbl;\n", iface->name);
913   fprintf(header, "};\n");
914   fprintf(header, "\n");
915   fprintf(header, "#ifdef COBJMACROS\n");
916   write_method_macro(iface, iface->name);
917   fprintf(header, "#endif\n");
918   fprintf(header, "\n");
919   fprintf(header, "#endif\n");
920   fprintf(header, "\n");
921   write_method_proto(iface);
922   fprintf(header,"\n#endif  /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
923 }
924
925 static void write_rpc_interface(const type_t *iface)
926 {
927   unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
928   const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
929   static int allocate_written = 0;
930
931   if (!allocate_written)
932   {
933     allocate_written = 1;
934     fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
935     fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
936   }
937
938   fprintf(header, "/*****************************************************************************\n");
939   fprintf(header, " * %s interface (v%d.%d)\n", iface->name, LOWORD(ver), HIWORD(ver));
940   fprintf(header, " */\n");
941   fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
942   fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
943   if (iface->funcs)
944   {
945     write_iface_guid(iface);
946     if (var) fprintf(header, "extern handle_t %s;\n", var);
947     if (old_names)
948     {
949         fprintf(header, "extern RPC_IF_HANDLE %s_ClientIfHandle;\n", iface->name);
950         fprintf(header, "extern RPC_IF_HANDLE %s_ServerIfHandle;\n", iface->name);
951     }
952     else
953     {
954         fprintf(header, "extern RPC_IF_HANDLE %s_v%d_%d_c_ifspec;\n", iface->name, LOWORD(ver), HIWORD(ver));
955         fprintf(header, "extern RPC_IF_HANDLE %s_v%d_%d_s_ifspec;\n", iface->name, LOWORD(ver), HIWORD(ver));
956     }
957     write_function_proto(iface);
958   }
959   fprintf(header,"\n#endif  /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
960
961   /* FIXME: server/client code */
962 }
963
964 void write_interface(type_t *iface)
965 {
966   if (is_object(iface->attrs))
967     write_com_interface(iface);
968   else
969     write_rpc_interface(iface);
970 }
971
972 void write_dispinterface(type_t *iface)
973 {
974   fprintf(header, "/*****************************************************************************\n");
975   fprintf(header, " * %s dispinterface\n", iface->name);
976   fprintf(header, " */\n");
977   fprintf(header,"#ifndef __%s_DISPINTERFACE_DEFINED__\n", iface->name);
978   fprintf(header,"#define __%s_DISPINTERFACE_DEFINED__\n\n", iface->name);
979   write_dispiface_guid(iface);
980   write_forward(iface);
981   /* C++ interface */
982   fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
983   fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
984   fprintf(header, "{\n");
985   fprintf(header, "};\n");
986   fprintf(header, "#else\n");
987   /* C interface */
988   fprintf(header, "typedef struct %sVtbl {\n", iface->name);
989   indentation++;
990   fprintf(header, "    BEGIN_INTERFACE\n");
991   fprintf(header, "\n");
992   write_c_disp_method_def(iface);
993   indentation--;
994   fprintf(header, "    END_INTERFACE\n");
995   fprintf(header, "} %sVtbl;\n", iface->name);
996   fprintf(header, "interface %s {\n", iface->name);
997   fprintf(header, "    const %sVtbl* lpVtbl;\n", iface->name);
998   fprintf(header, "};\n");
999   fprintf(header, "\n");
1000   fprintf(header, "#ifdef COBJMACROS\n");
1001   write_method_macro(iface->ref, iface->name);
1002   fprintf(header, "#endif\n");
1003   fprintf(header, "\n");
1004   fprintf(header, "#endif\n");
1005   fprintf(header, "\n");
1006   fprintf(header,"#endif  /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
1007 }
1008
1009 void write_coclass(type_t *cocl)
1010 {
1011   fprintf(header, "/*****************************************************************************\n");
1012   fprintf(header, " * %s coclass\n", cocl->name);
1013   fprintf(header, " */\n\n");
1014   write_coclass_guid(cocl);
1015   fprintf(header, "\n");
1016 }
1017
1018 void write_coclass_forward(type_t *cocl)
1019 {
1020   fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1021   fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1022   fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1023   fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1024 }