widl: Generate class forward declaration for coclasses.
[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 <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <ctype.h>
31
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
36 #include "expr.h"
37 #include "typetree.h"
38
39 static int indentation = 0;
40 static int is_object_interface = 0;
41 user_type_list_t user_type_list = LIST_INIT(user_type_list);
42 context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
43 generic_handle_list_t generic_handle_list = LIST_INIT(generic_handle_list);
44
45 static void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name);
46
47 static void indent(FILE *h, int delta)
48 {
49   int c;
50   if (delta < 0) indentation += delta;
51   for (c=0; c<indentation; c++) fprintf(h, "    ");
52   if (delta > 0) indentation += delta;
53 }
54
55 int is_ptrchain_attr(const var_t *var, enum attr_type t)
56 {
57     if (is_attr(var->attrs, t))
58         return 1;
59     else
60     {
61         type_t *type = var->type;
62         for (;;)
63         {
64             if (is_attr(type->attrs, t))
65                 return 1;
66             else if (type_is_alias(type))
67                 type = type_alias_get_aliasee(type);
68             else if (is_ptr(type))
69                 type = type_pointer_get_ref(type);
70             else return 0;
71         }
72     }
73 }
74
75 int is_aliaschain_attr(const type_t *type, enum attr_type attr)
76 {
77     const type_t *t = type;
78     for (;;)
79     {
80         if (is_attr(t->attrs, attr))
81             return 1;
82         else if (type_is_alias(t))
83             t = type_alias_get_aliasee(t);
84         else return 0;
85     }
86 }
87
88 int is_attr(const attr_list_t *list, enum attr_type t)
89 {
90     const attr_t *attr;
91     if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
92         if (attr->type == t) return 1;
93     return 0;
94 }
95
96 void *get_attrp(const attr_list_t *list, enum attr_type t)
97 {
98     const attr_t *attr;
99     if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
100         if (attr->type == t) return attr->u.pval;
101     return NULL;
102 }
103
104 unsigned int get_attrv(const attr_list_t *list, enum attr_type t)
105 {
106     const attr_t *attr;
107     if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
108         if (attr->type == t) return attr->u.ival;
109     return 0;
110 }
111
112 int is_void(const type_t *t)
113 {
114     return type_get_type(t) == TYPE_VOID;
115 }
116
117 int is_conformant_array(const type_t *t)
118 {
119     return is_array(t) && type_array_has_conformance(t);
120 }
121
122 void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
123 {
124   if (!uuid) return;
125   fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
126         "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
127         guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
128         uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
129         uuid->Data4[6], uuid->Data4[7]);
130 }
131
132 const char *get_name(const var_t *v)
133 {
134     static char buffer[256];
135
136     if (is_attr( v->attrs, ATTR_PROPGET ))
137         strcpy( buffer, "get_" );
138     else if (is_attr( v->attrs, ATTR_PROPPUT ))
139         strcpy( buffer, "put_" );
140     else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
141         strcpy( buffer, "putref_" );
142     else
143         buffer[0] = 0;
144     strcat( buffer, v->name );
145     return buffer;
146 }
147
148 static void write_field(FILE *h, var_t *v)
149 {
150   if (!v) return;
151   if (v->type) {
152     indent(h, 0);
153     write_type_def_or_decl(h, v->type, TRUE, v->name);
154     fprintf(h, ";\n");
155   }
156 }
157
158 static void write_fields(FILE *h, var_list_t *fields)
159 {
160     var_t *v;
161     if (!fields) return;
162     LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
163 }
164
165 static void write_enums(FILE *h, var_list_t *enums)
166 {
167   var_t *v;
168   if (!enums) return;
169   LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
170   {
171     if (v->name) {
172       indent(h, 0);
173       fprintf(h, "%s", get_name(v));
174       if (v->eval) {
175         fprintf(h, " = ");
176         write_expr(h, v->eval, 0, 1, NULL, NULL, "");
177       }
178     }
179     if (list_next( enums, &v->entry )) fprintf(h, ",\n");
180   }
181   fprintf(h, "\n");
182 }
183
184 int needs_space_after(type_t *t)
185 {
186   return (type_is_alias(t) ||
187           (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
188 }
189
190 void write_type_left(FILE *h, type_t *t, int declonly)
191 {
192   if (!h) return;
193
194   if (is_attr(t->attrs, ATTR_CONST) &&
195       (type_is_alias(t) || !is_ptr(t)))
196     fprintf(h, "const ");
197
198   if (type_is_alias(t)) fprintf(h, "%s", t->name);
199   else {
200     switch (type_get_type_detect_alias(t)) {
201       case TYPE_ENUM:
202         if (!declonly && t->defined && !t->written) {
203           if (t->name) fprintf(h, "enum %s {\n", t->name);
204           else fprintf(h, "enum {\n");
205           t->written = TRUE;
206           indentation++;
207           write_enums(h, type_enum_get_values(t));
208           indent(h, -1);
209           fprintf(h, "}");
210         }
211         else fprintf(h, "enum %s", t->name ? t->name : "");
212         break;
213       case TYPE_STRUCT:
214       case TYPE_ENCAPSULATED_UNION:
215         if (!declonly && t->defined && !t->written) {
216           if (t->name) fprintf(h, "struct %s {\n", t->name);
217           else fprintf(h, "struct {\n");
218           t->written = TRUE;
219           indentation++;
220           if (type_get_type(t) != TYPE_STRUCT)
221             write_fields(h, type_encapsulated_union_get_fields(t));
222           else
223             write_fields(h, type_struct_get_fields(t));
224           indent(h, -1);
225           fprintf(h, "}");
226         }
227         else fprintf(h, "struct %s", t->name ? t->name : "");
228         break;
229       case TYPE_UNION:
230         if (!declonly && t->defined && !t->written) {
231           if (t->name) fprintf(h, "union %s {\n", t->name);
232           else fprintf(h, "union {\n");
233           t->written = TRUE;
234           indentation++;
235           write_fields(h, type_union_get_cases(t));
236           indent(h, -1);
237           fprintf(h, "}");
238         }
239         else fprintf(h, "union %s", t->name ? t->name : "");
240         break;
241       case TYPE_POINTER:
242         write_type_left(h, type_pointer_get_ref(t), declonly);
243         fprintf(h, "%s*", needs_space_after(type_pointer_get_ref(t)) ? " " : "");
244         if (is_attr(t->attrs, ATTR_CONST)) fprintf(h, "const ");
245         break;
246       case TYPE_ARRAY:
247         if (t->name && type_array_is_decl_as_ptr(t))
248           fprintf(h, "%s", t->name);
249         else
250         {
251           write_type_left(h, type_array_get_element(t), declonly);
252           if (type_array_is_decl_as_ptr(t))
253             fprintf(h, "%s*", needs_space_after(type_array_get_element(t)) ? " " : "");
254         }
255         break;
256       case TYPE_BASIC:
257         if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
258             type_basic_get_type(t) != TYPE_BASIC_INT64 &&
259             type_basic_get_type(t) != TYPE_BASIC_HYPER)
260         {
261           if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
262           else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
263         }
264         switch (type_basic_get_type(t))
265         {
266         case TYPE_BASIC_INT8: fprintf(h, "small"); break;
267         case TYPE_BASIC_INT16: fprintf(h, "short"); break;
268         case TYPE_BASIC_INT: fprintf(h, "int"); break;
269         case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
270         case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
271         case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
272         case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
273         case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
274         case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
275         case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
276         case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
277         case TYPE_BASIC_INT32:
278           if (type_basic_get_sign(t) > 0)
279             fprintf(h, "ULONG");
280           else
281             fprintf(h, "LONG");
282           break;
283         case TYPE_BASIC_INT64:
284           if (type_basic_get_sign(t) > 0)
285             fprintf(h, "UINT64");
286           else
287             fprintf(h, "INT64");
288           break;
289         case TYPE_BASIC_HYPER:
290           if (type_basic_get_sign(t) > 0)
291             fprintf(h, "MIDL_uhyper");
292           else
293             fprintf(h, "hyper");
294           break;
295         }
296         break;
297       case TYPE_INTERFACE:
298       case TYPE_MODULE:
299       case TYPE_COCLASS:
300         fprintf(h, "%s", t->name);
301         break;
302       case TYPE_VOID:
303         fprintf(h, "void");
304         break;
305       case TYPE_BITFIELD:
306         write_type_left(h, type_bitfield_get_field(t), declonly);
307         break;
308       case TYPE_ALIAS:
309       case TYPE_FUNCTION:
310         /* handled elsewhere */
311         assert(0);
312         break;
313     }
314   }
315 }
316
317 void write_type_right(FILE *h, type_t *t, int is_field)
318 {
319   if (!h) return;
320
321   switch (type_get_type(t))
322   {
323   case TYPE_ARRAY:
324     if (!type_array_is_decl_as_ptr(t))
325     {
326       if (is_conformant_array(t))
327       {
328         fprintf(h, "[%s]", is_field ? "1" : "");
329         t = type_array_get_element(t);
330       }
331       for ( ;
332            type_get_type(t) == TYPE_ARRAY && !type_array_is_decl_as_ptr(t);
333            t = type_array_get_element(t))
334         fprintf(h, "[%u]", type_array_get_dim(t));
335     }
336     break;
337   case TYPE_BITFIELD:
338     fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
339     break;
340   case TYPE_VOID:
341   case TYPE_BASIC:
342   case TYPE_ENUM:
343   case TYPE_STRUCT:
344   case TYPE_ENCAPSULATED_UNION:
345   case TYPE_UNION:
346   case TYPE_ALIAS:
347   case TYPE_MODULE:
348   case TYPE_COCLASS:
349   case TYPE_FUNCTION:
350   case TYPE_INTERFACE:
351   case TYPE_POINTER:
352     break;
353   }
354 }
355
356 static void write_type_v(FILE *h, type_t *t, int is_field, int declonly, const char *name)
357 {
358   type_t *pt = NULL;
359   int ptr_level = 0;
360
361   if (!h) return;
362
363   if (t) {
364     for (pt = t; is_ptr(pt); pt = type_pointer_get_ref(pt), ptr_level++)
365       ;
366
367     if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
368       int i;
369       const char *callconv = get_attrp(pt->attrs, ATTR_CALLCONV);
370       if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
371       if (is_attr(pt->attrs, ATTR_INLINE)) fprintf(h, "inline ");
372       write_type_left(h, type_function_get_rettype(pt), declonly);
373       fputc(' ', h);
374       if (ptr_level) fputc('(', h);
375       if (callconv) fprintf(h, "%s ", callconv);
376       for (i = 0; i < ptr_level; i++)
377         fputc('*', h);
378     } else
379       write_type_left(h, t, declonly);
380   }
381
382   if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
383
384   if (t) {
385     if (type_get_type_detect_alias(pt) == TYPE_FUNCTION) {
386       const var_list_t *args = type_function_get_args(pt);
387
388       if (ptr_level) fputc(')', h);
389       fputc('(', h);
390       if (args)
391           write_args(h, args, NULL, 0, FALSE);
392       else
393           fprintf(h, "void");
394       fputc(')', h);
395     } else
396       write_type_right(h, t, is_field);
397   }
398 }
399
400 static void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *name)
401 {
402   write_type_v(f, t, field, FALSE, name);
403 }
404
405 void write_type_decl(FILE *f, type_t *t, const char *name)
406 {
407   write_type_v(f, t, FALSE, TRUE, name);
408 }
409
410 void write_type_decl_left(FILE *f, type_t *t)
411 {
412   write_type_left(f, t, TRUE);
413 }
414
415 static int user_type_registered(const char *name)
416 {
417   user_type_t *ut;
418   LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
419     if (!strcmp(name, ut->name))
420       return 1;
421   return 0;
422 }
423
424 static int context_handle_registered(const char *name)
425 {
426   context_handle_t *ch;
427   LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
428     if (!strcmp(name, ch->name))
429       return 1;
430   return 0;
431 }
432
433 static int generic_handle_registered(const char *name)
434 {
435   generic_handle_t *gh;
436   LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
437     if (!strcmp(name, gh->name))
438       return 1;
439   return 0;
440 }
441
442 unsigned int get_context_handle_offset( const type_t *type )
443 {
444     context_handle_t *ch;
445     unsigned int index = 0;
446
447     while (!is_attr( type->attrs, ATTR_CONTEXTHANDLE ))
448     {
449         if (type_is_alias( type )) type = type_alias_get_aliasee( type );
450         else if (is_ptr( type )) type = type_pointer_get_ref( type );
451         else error( "internal error: %s is not a context handle\n", type->name );
452     }
453     LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
454     {
455         if (!strcmp( type->name, ch->name )) return index;
456         index++;
457     }
458     error( "internal error: %s is not registered as a context handle\n", type->name );
459     return index;
460 }
461
462 unsigned int get_generic_handle_offset( const type_t *type )
463 {
464     generic_handle_t *gh;
465     unsigned int index = 0;
466
467     while (!is_attr( type->attrs, ATTR_HANDLE ))
468     {
469         if (type_is_alias( type )) type = type_alias_get_aliasee( type );
470         else if (is_ptr( type )) type = type_pointer_get_ref( type );
471         else error( "internal error: %s is not a generic handle\n", type->name );
472     }
473     LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
474     {
475         if (!strcmp( type->name, gh->name )) return index;
476         index++;
477     }
478     error( "internal error: %s is not registered as a generic handle\n", type->name );
479     return index;
480 }
481
482 /* check for types which require additional prototypes to be generated in the
483  * header */
484 void check_for_additional_prototype_types(const var_list_t *list)
485 {
486   const var_t *v;
487
488   if (!list) return;
489   LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
490   {
491     type_t *type = v->type;
492     if (!type) continue;
493     for (;;) {
494       const char *name = type->name;
495       if (type->user_types_registered) break;
496       type->user_types_registered = 1;
497       if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
498         if (!context_handle_registered(name))
499         {
500           context_handle_t *ch = xmalloc(sizeof(*ch));
501           ch->name = xstrdup(name);
502           list_add_tail(&context_handle_list, &ch->entry);
503         }
504         /* don't carry on parsing fields within this type */
505         break;
506       }
507       if ((type_get_type(type) != TYPE_BASIC ||
508            type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
509           is_attr(type->attrs, ATTR_HANDLE)) {
510         if (!generic_handle_registered(name))
511         {
512           generic_handle_t *gh = xmalloc(sizeof(*gh));
513           gh->name = xstrdup(name);
514           list_add_tail(&generic_handle_list, &gh->entry);
515         }
516         /* don't carry on parsing fields within this type */
517         break;
518       }
519       if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
520         if (!user_type_registered(name))
521         {
522           user_type_t *ut = xmalloc(sizeof *ut);
523           ut->name = xstrdup(name);
524           list_add_tail(&user_type_list, &ut->entry);
525         }
526         /* don't carry on parsing fields within this type as we are already
527          * using a wire marshaled type */
528         break;
529       }
530       else if (type_is_complete(type))
531       {
532         var_list_t *vars;
533         switch (type_get_type_detect_alias(type))
534         {
535         case TYPE_ENUM:
536           vars = type_enum_get_values(type);
537           break;
538         case TYPE_STRUCT:
539           vars = type_struct_get_fields(type);
540           break;
541         case TYPE_UNION:
542           vars = type_union_get_cases(type);
543           break;
544         default:
545           vars = NULL;
546           break;
547         }
548         check_for_additional_prototype_types(vars);
549       }
550
551       if (type_is_alias(type))
552         type = type_alias_get_aliasee(type);
553       else if (is_ptr(type))
554         type = type_pointer_get_ref(type);
555       else if (is_array(type))
556         type = type_array_get_element(type);
557       else
558         break;
559     }
560   }
561 }
562
563 static void write_user_types(FILE *header)
564 {
565   user_type_t *ut;
566   LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
567   {
568     const char *name = ut->name;
569     fprintf(header, "ULONG           __RPC_USER %s_UserSize     (ULONG *, ULONG, %s *);\n", name, name);
570     fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal  (ULONG *, unsigned char *, %s *);\n", name, name);
571     fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
572     fprintf(header, "void            __RPC_USER %s_UserFree     (ULONG *, %s *);\n", name, name);
573   }
574 }
575
576 static void write_context_handle_rundowns(FILE *header)
577 {
578   context_handle_t *ch;
579   LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
580   {
581     const char *name = ch->name;
582     fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
583   }
584 }
585
586 static void write_generic_handle_routines(FILE *header)
587 {
588   generic_handle_t *gh;
589   LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
590   {
591     const char *name = gh->name;
592     fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
593     fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
594   }
595 }
596
597 static void write_typedef(FILE *header, type_t *type)
598 {
599   fprintf(header, "typedef ");
600   write_type_def_or_decl(header, type_alias_get_aliasee(type), FALSE, type->name);
601   fprintf(header, ";\n");
602 }
603
604 int is_const_decl(const var_t *var)
605 {
606   const type_t *t;
607   /* strangely, MIDL accepts a const attribute on any pointer in the
608   * declaration to mean that data isn't being instantiated. this appears
609   * to be a bug, but there is no benefit to being incompatible with MIDL,
610   * so we'll do the same thing */
611   for (t = var->type; ; )
612   {
613     if (is_attr(t->attrs, ATTR_CONST))
614       return TRUE;
615     else if (is_ptr(t))
616       t = type_pointer_get_ref(t);
617     else break;
618   }
619   return FALSE;
620 }
621
622 static void write_declaration(FILE *header, const var_t *v)
623 {
624   if (is_const_decl(v) && v->eval)
625   {
626     fprintf(header, "#define %s (", v->name);
627     write_expr(header, v->eval, 0, 1, NULL, NULL, "");
628     fprintf(header, ")\n\n");
629   }
630   else
631   {
632     switch (v->stgclass)
633     {
634       case STG_NONE:
635       case STG_REGISTER: /* ignored */
636         break;
637       case STG_STATIC:
638         fprintf(header, "static ");
639         break;
640       case STG_EXTERN:
641         fprintf(header, "extern ");
642         break;
643     }
644     write_type_def_or_decl(header, v->type, FALSE, v->name);
645     fprintf(header, ";\n\n");
646   }
647 }
648
649 static void write_library(FILE *header, const typelib_t *typelib)
650 {
651   const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
652   fprintf(header, "\n");
653   write_guid(header, "LIBID", typelib->name, uuid);
654   fprintf(header, "\n");
655 }
656
657
658 const type_t* get_explicit_generic_handle_type(const var_t* var)
659 {
660     const type_t *t;
661     for (t = var->type;
662          is_ptr(t) || type_is_alias(t);
663          t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
664         if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
665             is_attr(t->attrs, ATTR_HANDLE))
666             return t;
667     return NULL;
668 }
669
670 const var_t *get_func_handle_var( const type_t *iface, const var_t *func,
671                                   unsigned char *explicit_fc, unsigned char *implicit_fc )
672 {
673     const var_t *var;
674     const var_list_t *args = type_get_function_args( func->type );
675
676     *explicit_fc = *implicit_fc = 0;
677     if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
678     {
679         if (!is_attr( var->attrs, ATTR_IN ) && is_attr( var->attrs, ATTR_OUT )) continue;
680         if (type_get_type( var->type ) == TYPE_BASIC && type_basic_get_type( var->type ) == TYPE_BASIC_HANDLE)
681         {
682             *explicit_fc = RPC_FC_BIND_PRIMITIVE;
683             return var;
684         }
685         if (get_explicit_generic_handle_type( var ))
686         {
687             *explicit_fc = RPC_FC_BIND_GENERIC;
688             return var;
689         }
690         if (is_context_handle( var->type ))
691         {
692             *explicit_fc = RPC_FC_BIND_CONTEXT;
693             return var;
694         }
695     }
696
697     if ((var = get_attrp( iface->attrs, ATTR_IMPLICIT_HANDLE )))
698     {
699         if (type_get_type( var->type ) == TYPE_BASIC &&
700             type_basic_get_type( var->type ) == TYPE_BASIC_HANDLE)
701             *implicit_fc = RPC_FC_BIND_PRIMITIVE;
702         else
703             *implicit_fc = RPC_FC_BIND_GENERIC;
704         return var;
705     }
706
707     *implicit_fc = RPC_FC_AUTO_HANDLE;
708     return NULL;
709 }
710
711 int has_out_arg_or_return(const var_t *func)
712 {
713     const var_t *var;
714
715     if (!is_void(type_function_get_rettype(func->type)))
716         return 1;
717
718     if (!type_get_function_args(func->type))
719         return 0;
720
721     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
722         if (is_attr(var->attrs, ATTR_OUT))
723             return 1;
724
725     return 0;
726 }
727
728
729 /********** INTERFACES **********/
730
731 int is_object(const type_t *iface)
732 {
733     const attr_t *attr;
734     if (type_is_defined(iface) && type_iface_get_inherit(iface))
735         return 1;
736     if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
737         if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
738     return 0;
739 }
740
741 int is_local(const attr_list_t *a)
742 {
743   return is_attr(a, ATTR_LOCAL);
744 }
745
746 const var_t *is_callas(const attr_list_t *a)
747 {
748   return get_attrp(a, ATTR_CALLAS);
749 }
750
751 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
752 {
753   const statement_t *stmt;
754   int first_iface = 1;
755
756   if (type_iface_get_inherit(iface))
757     write_method_macro(header, type_iface_get_inherit(iface), name);
758
759   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
760   {
761     const var_t *func = stmt->u.var;
762
763     if (first_iface)
764     {
765       fprintf(header, "/*** %s methods ***/\n", iface->name);
766       first_iface = 0;
767     }
768
769     if (!is_callas(func->attrs)) {
770       const var_t *arg;
771
772       fprintf(header, "#define %s_%s(This", name, get_name(func));
773       if (type_get_function_args(func->type))
774           LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
775               fprintf(header, ",%s", arg->name);
776       fprintf(header, ") ");
777
778       fprintf(header, "(This)->lpVtbl->%s(This", get_name(func));
779       if (type_get_function_args(func->type))
780           LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
781               fprintf(header, ",%s", arg->name);
782       fprintf(header, ")\n");
783     }
784   }
785 }
786
787 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
788 {
789   const var_t *arg;
790   int count = 0;
791
792   if (do_indent)
793   {
794       indentation++;
795       indent(h, 0);
796   }
797   if (method == 1) {
798     fprintf(h, "%s* This", name);
799     count++;
800   }
801   if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
802     if (count) {
803         if (do_indent)
804         {
805             fprintf(h, ",\n");
806             indent(h, 0);
807         }
808         else fprintf(h, ",");
809     }
810     write_type_decl(h, arg->type, arg->name);
811     count++;
812   }
813   if (do_indent) indentation--;
814 }
815
816 static void write_cpp_method_def(FILE *header, const type_t *iface)
817 {
818   const statement_t *stmt;
819
820   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
821   {
822     const var_t *func = stmt->u.var;
823     if (!is_callas(func->attrs)) {
824       const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
825       if (!callconv) callconv = "STDMETHODCALLTYPE";
826       indent(header, 0);
827       fprintf(header, "virtual ");
828       write_type_decl_left(header, type_function_get_rettype(func->type));
829       fprintf(header, " %s %s(\n", callconv, get_name(func));
830       write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
831       fprintf(header, ") = 0;\n");
832       fprintf(header, "\n");
833     }
834   }
835 }
836
837 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
838 {
839   const statement_t *stmt;
840   int first_iface = 1;
841
842   if (type_iface_get_inherit(iface))
843     do_write_c_method_def(header, type_iface_get_inherit(iface), name);
844
845   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
846   {
847     const var_t *func = stmt->u.var;
848     if (first_iface) {
849       indent(header, 0);
850       fprintf(header, "/*** %s methods ***/\n", iface->name);
851       first_iface = 0;
852     }
853     if (!is_callas(func->attrs)) {
854       const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
855       if (!callconv) callconv = "STDMETHODCALLTYPE";
856       indent(header, 0);
857       write_type_decl_left(header, type_function_get_rettype(func->type));
858       fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
859       write_args(header, type_get_function_args(func->type), name, 1, TRUE);
860       fprintf(header, ");\n");
861       fprintf(header, "\n");
862     }
863   }
864 }
865
866 static void write_c_method_def(FILE *header, const type_t *iface)
867 {
868   do_write_c_method_def(header, iface, iface->name);
869 }
870
871 static void write_c_disp_method_def(FILE *header, const type_t *iface)
872 {
873   do_write_c_method_def(header, type_iface_get_inherit(iface), iface->name);
874 }
875
876 static void write_method_proto(FILE *header, const type_t *iface)
877 {
878   const statement_t *stmt;
879
880   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
881   {
882     const var_t *func = stmt->u.var;
883
884     if (!is_local(func->attrs)) {
885       const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
886       if (!callconv) callconv = "STDMETHODCALLTYPE";
887       /* proxy prototype */
888       write_type_decl_left(header, type_function_get_rettype(func->type));
889       fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
890       write_args(header, type_get_function_args(func->type), iface->name, 1, TRUE);
891       fprintf(header, ");\n");
892       /* stub prototype */
893       fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
894       fprintf(header, "    IRpcStubBuffer* This,\n");
895       fprintf(header, "    IRpcChannelBuffer* pRpcChannelBuffer,\n");
896       fprintf(header, "    PRPC_MESSAGE pRpcMessage,\n");
897       fprintf(header, "    DWORD* pdwStubPhase);\n");
898     }
899   }
900 }
901
902 static void write_locals(FILE *fp, const type_t *iface, int body)
903 {
904   static const char comment[]
905     = "/* WIDL-generated stub.  You must provide an implementation for this.  */";
906   const statement_t *stmt;
907
908   if (!is_object(iface))
909     return;
910
911   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
912     const var_t *func = stmt->u.var;
913     const var_t *cas = is_callas(func->attrs);
914
915     if (cas) {
916       const statement_t *stmt2 = NULL;
917       STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
918         if (!strcmp(stmt2->u.var->name, cas->name))
919           break;
920       if (&stmt2->entry != type_iface_get_stmts(iface)) {
921         const var_t *m = stmt2->u.var;
922         /* proxy prototype - use local prototype */
923         write_type_decl_left(fp, type_function_get_rettype(m->type));
924         fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
925         write_args(fp, type_get_function_args(m->type), iface->name, 1, TRUE);
926         fprintf(fp, ")");
927         if (body) {
928           type_t *rt = type_function_get_rettype(m->type);
929           fprintf(fp, "\n{\n");
930           fprintf(fp, "    %s\n", comment);
931           if (rt->name && strcmp(rt->name, "HRESULT") == 0)
932             fprintf(fp, "    return E_NOTIMPL;\n");
933           else if (type_get_type(rt) != TYPE_VOID) {
934             fprintf(fp, "    ");
935             write_type_decl(fp, rt, "rv");
936             fprintf(fp, ";\n");
937             fprintf(fp, "    memset(&rv, 0, sizeof rv);\n");
938             fprintf(fp, "    return rv;\n");
939           }
940           fprintf(fp, "}\n\n");
941         }
942         else
943           fprintf(fp, ";\n");
944         /* stub prototype - use remotable prototype */
945         write_type_decl_left(fp, type_function_get_rettype(func->type));
946         fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
947         write_args(fp, type_get_function_args(func->type), iface->name, 1, TRUE);
948         fprintf(fp, ")");
949         if (body)
950           /* Remotable methods must all return HRESULTs.  */
951           fprintf(fp, "\n{\n    %s\n    return E_NOTIMPL;\n}\n\n", comment);
952         else
953           fprintf(fp, ";\n");
954       }
955       else
956         error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
957     }
958   }
959 }
960
961 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
962 {
963   const statement_t *stmt;
964   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
965   {
966     if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
967       write_locals(local_stubs, stmt->u.type, TRUE);
968     else if (stmt->type == STMT_LIBRARY)
969       write_local_stubs_stmts(local_stubs, stmt->u.lib->stmts);
970   }
971 }
972
973 void write_local_stubs(const statement_list_t *stmts)
974 {
975   FILE *local_stubs;
976
977   if (!local_stubs_name) return;
978
979   local_stubs = fopen(local_stubs_name, "w");
980   if (!local_stubs) {
981     error("Could not open %s for output\n", local_stubs_name);
982     return;
983   }
984   fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
985   fprintf(local_stubs, "#include <objbase.h>\n");
986   fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
987
988   write_local_stubs_stmts(local_stubs, stmts);
989
990   fclose(local_stubs);
991 }
992
993 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
994 {
995   const char *callconv = get_attrp(fun->type->attrs, ATTR_CALLCONV);
996
997   if (!callconv) callconv = "__cdecl";
998   /* FIXME: do we need to handle call_as? */
999   write_type_decl_left(header, type_function_get_rettype(fun->type));
1000   fprintf(header, " %s ", callconv);
1001   fprintf(header, "%s%s(\n", prefix, get_name(fun));
1002   if (type_get_function_args(fun->type))
1003     write_args(header, type_get_function_args(fun->type), iface->name, 0, TRUE);
1004   else
1005     fprintf(header, "    void");
1006   fprintf(header, ");\n\n");
1007 }
1008
1009 static void write_forward(FILE *header, type_t *iface)
1010 {
1011   fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
1012   fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
1013   fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
1014   fprintf(header, "#endif\n\n" );
1015 }
1016
1017 static void write_iface_guid(FILE *header, const type_t *iface)
1018 {
1019   const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1020   write_guid(header, "IID", iface->name, uuid);
1021
1022
1023 static void write_dispiface_guid(FILE *header, const type_t *iface)
1024 {
1025   const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1026   write_guid(header, "DIID", iface->name, uuid);
1027 }
1028
1029 static void write_coclass_guid(FILE *header, const type_t *cocl)
1030 {
1031   const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
1032   write_guid(header, "CLSID", cocl->name, uuid);
1033 }
1034
1035 static void write_com_interface_start(FILE *header, const type_t *iface)
1036 {
1037   int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1038   fprintf(header, "/*****************************************************************************\n");
1039   fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1040   fprintf(header, " */\n");
1041   fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
1042   fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
1043 }
1044
1045 static void write_com_interface_end(FILE *header, type_t *iface)
1046 {
1047   int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1048   if (dispinterface)
1049     write_dispiface_guid(header, iface);
1050   else
1051     write_iface_guid(header, iface);
1052   /* C++ interface */
1053   fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1054   if (type_iface_get_inherit(iface))
1055   {
1056     fprintf(header, "interface %s : public %s\n", iface->name,
1057             type_iface_get_inherit(iface)->name);
1058     fprintf(header, "{\n");
1059   }
1060   else
1061   {
1062     fprintf(header, "interface %s\n", iface->name);
1063     fprintf(header, "{\n");
1064     fprintf(header, "    BEGIN_INTERFACE\n");
1065     fprintf(header, "\n");
1066   }
1067   /* dispinterfaces don't have real functions, so don't write C++ functions for
1068    * them */
1069   if (!dispinterface)
1070   {
1071     indentation++;
1072     write_cpp_method_def(header, iface);
1073     indentation--;
1074   }
1075   if (!type_iface_get_inherit(iface))
1076     fprintf(header, "    END_INTERFACE\n");
1077   fprintf(header, "};\n");
1078   fprintf(header, "#else\n");
1079   /* C interface */
1080   fprintf(header, "typedef struct %sVtbl {\n", iface->name);
1081   indentation++;
1082   fprintf(header, "    BEGIN_INTERFACE\n");
1083   fprintf(header, "\n");
1084   if (dispinterface)
1085     write_c_disp_method_def(header, iface);
1086   else
1087     write_c_method_def(header, iface);
1088   indentation--;
1089   fprintf(header, "    END_INTERFACE\n");
1090   fprintf(header, "} %sVtbl;\n", iface->name);
1091   fprintf(header, "interface %s {\n", iface->name);
1092   fprintf(header, "    CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1093   fprintf(header, "};\n");
1094   fprintf(header, "\n");
1095   fprintf(header, "#ifdef COBJMACROS\n");
1096   /* dispinterfaces don't have real functions, so don't write macros for them,
1097    * only for the interface this interface inherits from, i.e. IDispatch */
1098   write_method_macro(header, dispinterface ? type_iface_get_inherit(iface) : iface, iface->name);
1099   fprintf(header, "#endif\n");
1100   fprintf(header, "\n");
1101   fprintf(header, "#endif\n");
1102   fprintf(header, "\n");
1103   /* dispinterfaces don't have real functions, so don't write prototypes for
1104    * them */
1105   if (!dispinterface)
1106   {
1107     write_method_proto(header, iface);
1108     write_locals(header, iface, FALSE);
1109     fprintf(header, "\n");
1110   }
1111   fprintf(header,"#endif  /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
1112 }
1113
1114 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1115 {
1116   unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1117   const var_t *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1118
1119   fprintf(header, "/*****************************************************************************\n");
1120   fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1121   fprintf(header, " */\n");
1122   fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1123   fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1124   if (var)
1125   {
1126       fprintf(header, "extern ");
1127       write_type_decl( header, var->type, var->name );
1128       fprintf(header, ";\n");
1129   }
1130   if (old_names)
1131   {
1132       fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1133       fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1134   }
1135   else
1136   {
1137       fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1138               prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1139       fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1140               prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1141   }
1142 }
1143
1144 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1145 {
1146   fprintf(header,"\n#endif  /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1147 }
1148
1149 static void write_coclass(FILE *header, type_t *cocl)
1150 {
1151   fprintf(header, "/*****************************************************************************\n");
1152   fprintf(header, " * %s coclass\n", cocl->name);
1153   fprintf(header, " */\n\n");
1154   write_coclass_guid(header, cocl);
1155   fprintf(header, "\n#ifdef __cplusplus\n");
1156   fprintf(header, "class %s;\n", cocl->name);
1157   fprintf(header, "#endif\n");
1158   fprintf(header, "\n");
1159 }
1160
1161 static void write_coclass_forward(FILE *header, type_t *cocl)
1162 {
1163   fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1164   fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1165   fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1166   fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1167 }
1168
1169 static void write_import(FILE *header, const char *fname)
1170 {
1171   char *hname, *p;
1172
1173   hname = dup_basename(fname, ".idl");
1174   p = hname + strlen(hname) - 2;
1175   if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1176
1177   fprintf(header, "#include <%s>\n", hname);
1178   free(hname);
1179 }
1180
1181 static void write_imports(FILE *header, const statement_list_t *stmts)
1182 {
1183   const statement_t *stmt;
1184   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1185   {
1186     switch (stmt->type)
1187     {
1188       case STMT_TYPE:
1189         if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1190           write_imports(header, type_iface_get_stmts(stmt->u.type));
1191         break;
1192       case STMT_TYPEREF:
1193       case STMT_IMPORTLIB:
1194         /* not included in header */
1195         break;
1196       case STMT_IMPORT:
1197         write_import(header, stmt->u.str);
1198         break;
1199       case STMT_TYPEDEF:
1200       case STMT_MODULE:
1201       case STMT_CPPQUOTE:
1202       case STMT_DECLARATION:
1203         /* not processed here */
1204         break;
1205       case STMT_LIBRARY:
1206         write_imports(header, stmt->u.lib->stmts);
1207         break;
1208     }
1209   }
1210 }
1211
1212 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1213 {
1214   const statement_t *stmt;
1215   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1216   {
1217     switch (stmt->type)
1218     {
1219       case STMT_TYPE:
1220         if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1221         {
1222           if (is_object(stmt->u.type) || is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE))
1223             write_forward(header, stmt->u.type);
1224         }
1225         else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1226           write_coclass_forward(header, stmt->u.type);
1227         break;
1228       case STMT_TYPEREF:
1229       case STMT_IMPORTLIB:
1230         /* not included in header */
1231         break;
1232       case STMT_IMPORT:
1233       case STMT_TYPEDEF:
1234       case STMT_MODULE:
1235       case STMT_CPPQUOTE:
1236       case STMT_DECLARATION:
1237         /* not processed here */
1238         break;
1239       case STMT_LIBRARY:
1240         write_forward_decls(header, stmt->u.lib->stmts);
1241         break;
1242     }
1243   }
1244 }
1245
1246 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1247 {
1248   const statement_t *stmt;
1249   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1250   {
1251     switch (stmt->type)
1252     {
1253       case STMT_TYPE:
1254         if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1255         {
1256           type_t *iface = stmt->u.type;
1257           if (is_object(iface)) is_object_interface++;
1258           if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
1259           {
1260             write_com_interface_start(header, iface);
1261             write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1262             write_com_interface_end(header, iface);
1263           }
1264           else
1265           {
1266             write_rpc_interface_start(header, iface);
1267             write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1268             write_rpc_interface_end(header, iface);
1269           }
1270           if (is_object(iface)) is_object_interface++;
1271         }
1272         else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1273           write_coclass(header, stmt->u.type);
1274         else
1275         {
1276           write_type_def_or_decl(header, stmt->u.type, FALSE, NULL);
1277           fprintf(header, ";\n\n");
1278         }
1279         break;
1280       case STMT_TYPEREF:
1281         /* FIXME: shouldn't write out forward declarations for undefined
1282         * interfaces but a number of our IDL files depend on this */
1283         if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
1284           write_forward(header, stmt->u.type);
1285         break;
1286       case STMT_IMPORTLIB:
1287       case STMT_MODULE:
1288         /* not included in header */
1289         break;
1290       case STMT_IMPORT:
1291         /* not processed here */
1292         break;
1293       case STMT_TYPEDEF:
1294       {
1295         const type_list_t *type_entry = stmt->u.type_list;
1296         for (; type_entry; type_entry = type_entry->next)
1297           write_typedef(header, type_entry->type);
1298         break;
1299       }
1300       case STMT_LIBRARY:
1301         write_library(header, stmt->u.lib);
1302         write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1303         break;
1304       case STMT_CPPQUOTE:
1305         fprintf(header, "%s\n", stmt->u.str);
1306         break;
1307       case STMT_DECLARATION:
1308         if (iface && type_get_type(stmt->u.var->type) == TYPE_FUNCTION)
1309         {
1310           if (!ignore_funcs)
1311           {
1312             int prefixes_differ = strcmp(prefix_client, prefix_server);
1313
1314             if (prefixes_differ)
1315             {
1316               fprintf(header, "/* client prototype */\n");
1317               write_function_proto(header, iface, stmt->u.var, prefix_client);
1318               fprintf(header, "/* server prototype */\n");
1319             }
1320             write_function_proto(header, iface, stmt->u.var, prefix_server);
1321           }
1322         }
1323         else
1324           write_declaration(header, stmt->u.var);
1325         break;
1326     }
1327   }
1328 }
1329
1330 void write_header(const statement_list_t *stmts)
1331 {
1332   FILE *header;
1333
1334   if (!do_header) return;
1335
1336   if(!(header = fopen(header_name, "w"))) {
1337     error("Could not open %s for output\n", header_name);
1338     return;
1339   }
1340   fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1341   fprintf(header, "#include <rpc.h>\n" );
1342   fprintf(header, "#include <rpcndr.h>\n\n" );
1343
1344   fprintf(header, "#if !defined(COM_NO_WINDOWS_H) && !defined(__WINESRC__)\n");
1345   fprintf(header, "#include <windows.h>\n");
1346   fprintf(header, "#include <ole2.h>\n");
1347   fprintf(header, "#endif\n\n");
1348
1349   fprintf(header, "#ifndef __WIDL_%s\n", header_token);
1350   fprintf(header, "#define __WIDL_%s\n\n", header_token);
1351
1352   fprintf(header, "/* Forward declarations */\n\n");
1353   write_forward_decls(header, stmts);
1354
1355   fprintf(header, "/* Headers for imported files */\n\n");
1356   write_imports(header, stmts);
1357   fprintf(header, "\n");
1358   start_cplusplus_guard(header);
1359
1360   write_header_stmts(header, stmts, NULL, FALSE);
1361
1362   fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1363   fprintf(header, "\n");
1364   write_user_types(header);
1365   write_generic_handle_routines(header);
1366   write_context_handle_rundowns(header);
1367   fprintf(header, "\n");
1368   fprintf(header, "/* End additional prototypes */\n");
1369   fprintf(header, "\n");
1370
1371   end_cplusplus_guard(header);
1372   fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
1373
1374   fclose(header);
1375 }