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