gdi32: Add support for 16 bpp dibs.
[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 static 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 /* check for types which require additional prototypes to be generated in the
443  * header */
444 void check_for_additional_prototype_types(const var_list_t *list)
445 {
446   const var_t *v;
447
448   if (!list) return;
449   LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
450   {
451     type_t *type = v->type;
452     if (!type) continue;
453     for (;;) {
454       const char *name = type->name;
455       if (type->user_types_registered) break;
456       type->user_types_registered = 1;
457       if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
458         if (!context_handle_registered(name))
459         {
460           context_handle_t *ch = xmalloc(sizeof(*ch));
461           ch->name = xstrdup(name);
462           list_add_tail(&context_handle_list, &ch->entry);
463         }
464         /* don't carry on parsing fields within this type */
465         break;
466       }
467       if ((type_get_type(type) != TYPE_BASIC ||
468            type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
469           is_attr(type->attrs, ATTR_HANDLE)) {
470         if (!generic_handle_registered(name))
471         {
472           generic_handle_t *gh = xmalloc(sizeof(*gh));
473           gh->name = xstrdup(name);
474           list_add_tail(&generic_handle_list, &gh->entry);
475         }
476         /* don't carry on parsing fields within this type */
477         break;
478       }
479       if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
480         if (!user_type_registered(name))
481         {
482           user_type_t *ut = xmalloc(sizeof *ut);
483           ut->name = xstrdup(name);
484           list_add_tail(&user_type_list, &ut->entry);
485         }
486         /* don't carry on parsing fields within this type as we are already
487          * using a wire marshaled type */
488         break;
489       }
490       else if (type_is_complete(type))
491       {
492         var_list_t *vars;
493         switch (type_get_type_detect_alias(type))
494         {
495         case TYPE_ENUM:
496           vars = type_enum_get_values(type);
497           break;
498         case TYPE_STRUCT:
499           vars = type_struct_get_fields(type);
500           break;
501         case TYPE_UNION:
502           vars = type_union_get_cases(type);
503           break;
504         default:
505           vars = NULL;
506           break;
507         }
508         check_for_additional_prototype_types(vars);
509       }
510
511       if (type_is_alias(type))
512         type = type_alias_get_aliasee(type);
513       else if (is_ptr(type))
514         type = type_pointer_get_ref(type);
515       else if (is_array(type))
516         type = type_array_get_element(type);
517       else
518         break;
519     }
520   }
521 }
522
523 static void write_user_types(FILE *header)
524 {
525   user_type_t *ut;
526   LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
527   {
528     const char *name = ut->name;
529     fprintf(header, "ULONG           __RPC_USER %s_UserSize     (ULONG *, ULONG, %s *);\n", name, name);
530     fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal  (ULONG *, unsigned char *, %s *);\n", name, name);
531     fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
532     fprintf(header, "void            __RPC_USER %s_UserFree     (ULONG *, %s *);\n", name, name);
533   }
534 }
535
536 static void write_context_handle_rundowns(FILE *header)
537 {
538   context_handle_t *ch;
539   LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
540   {
541     const char *name = ch->name;
542     fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
543   }
544 }
545
546 static void write_generic_handle_routines(FILE *header)
547 {
548   generic_handle_t *gh;
549   LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
550   {
551     const char *name = gh->name;
552     fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
553     fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
554   }
555 }
556
557 static void write_typedef(FILE *header, type_t *type)
558 {
559   fprintf(header, "typedef ");
560   write_type_def_or_decl(header, type_alias_get_aliasee(type), FALSE, type->name);
561   fprintf(header, ";\n");
562 }
563
564 int is_const_decl(const var_t *var)
565 {
566   const type_t *t;
567   /* strangely, MIDL accepts a const attribute on any pointer in the
568   * declaration to mean that data isn't being instantiated. this appears
569   * to be a bug, but there is no benefit to being incompatible with MIDL,
570   * so we'll do the same thing */
571   for (t = var->type; ; )
572   {
573     if (is_attr(t->attrs, ATTR_CONST))
574       return TRUE;
575     else if (is_ptr(t))
576       t = type_pointer_get_ref(t);
577     else break;
578   }
579   return FALSE;
580 }
581
582 static void write_declaration(FILE *header, const var_t *v)
583 {
584   if (is_const_decl(v) && v->eval)
585   {
586     fprintf(header, "#define %s (", v->name);
587     write_expr(header, v->eval, 0, 1, NULL, NULL, "");
588     fprintf(header, ")\n\n");
589   }
590   else
591   {
592     switch (v->stgclass)
593     {
594       case STG_NONE:
595       case STG_REGISTER: /* ignored */
596         break;
597       case STG_STATIC:
598         fprintf(header, "static ");
599         break;
600       case STG_EXTERN:
601         fprintf(header, "extern ");
602         break;
603     }
604     write_type_def_or_decl(header, v->type, FALSE, v->name);
605     fprintf(header, ";\n\n");
606   }
607 }
608
609 static void write_library(FILE *header, const typelib_t *typelib)
610 {
611   const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
612   fprintf(header, "\n");
613   write_guid(header, "LIBID", typelib->name, uuid);
614   fprintf(header, "\n");
615 }
616
617
618 const var_t* get_explicit_handle_var(const var_t *func)
619 {
620     const var_t* var;
621
622     if (!type_get_function_args(func->type))
623         return NULL;
624
625     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
626     {
627         const type_t *type = var->type;
628         if (type_get_type(type) == TYPE_BASIC && type_basic_get_type(type) == TYPE_BASIC_HANDLE)
629             return var;
630     }
631
632     return NULL;
633 }
634
635 const type_t* get_explicit_generic_handle_type(const var_t* var)
636 {
637     const type_t *t;
638     for (t = var->type;
639          is_ptr(t) || type_is_alias(t);
640          t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
641         if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
642             is_attr(t->attrs, ATTR_HANDLE))
643             return t;
644     return NULL;
645 }
646
647 const var_t* get_explicit_generic_handle_var(const var_t *func)
648 {
649     const var_t* var;
650
651     if (!type_get_function_args(func->type))
652         return NULL;
653
654     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
655         if (get_explicit_generic_handle_type(var))
656             return var;
657
658     return NULL;
659 }
660
661 const var_t* get_context_handle_var(const var_t *func)
662 {
663     const var_t* var;
664
665     if (!type_get_function_args(func->type))
666         return NULL;
667
668     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
669         if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
670             return var;
671
672     return NULL;
673 }
674
675 int has_out_arg_or_return(const var_t *func)
676 {
677     const var_t *var;
678
679     if (!is_void(type_function_get_rettype(func->type)))
680         return 1;
681
682     if (!type_get_function_args(func->type))
683         return 0;
684
685     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
686         if (is_attr(var->attrs, ATTR_OUT))
687             return 1;
688
689     return 0;
690 }
691
692
693 /********** INTERFACES **********/
694
695 int is_object(const type_t *iface)
696 {
697     const attr_t *attr;
698     if (type_is_defined(iface) && type_iface_get_inherit(iface))
699         return 1;
700     if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
701         if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
702     return 0;
703 }
704
705 int is_local(const attr_list_t *a)
706 {
707   return is_attr(a, ATTR_LOCAL);
708 }
709
710 const var_t *is_callas(const attr_list_t *a)
711 {
712   return get_attrp(a, ATTR_CALLAS);
713 }
714
715 static void write_method_macro(FILE *header, const type_t *iface, const char *name)
716 {
717   const statement_t *stmt;
718   int first_iface = 1;
719
720   if (type_iface_get_inherit(iface))
721     write_method_macro(header, type_iface_get_inherit(iface), name);
722
723   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
724   {
725     const var_t *func = stmt->u.var;
726
727     if (first_iface)
728     {
729       fprintf(header, "/*** %s methods ***/\n", iface->name);
730       first_iface = 0;
731     }
732
733     if (!is_callas(func->attrs)) {
734       const var_t *arg;
735
736       fprintf(header, "#define %s_%s(This", name, get_name(func));
737       if (type_get_function_args(func->type))
738           LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
739               fprintf(header, ",%s", arg->name);
740       fprintf(header, ") ");
741
742       fprintf(header, "(This)->lpVtbl->%s(This", get_name(func));
743       if (type_get_function_args(func->type))
744           LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
745               fprintf(header, ",%s", arg->name);
746       fprintf(header, ")\n");
747     }
748   }
749 }
750
751 void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
752 {
753   const var_t *arg;
754   int count = 0;
755
756   if (do_indent)
757   {
758       indentation++;
759       indent(h, 0);
760   }
761   if (method == 1) {
762     fprintf(h, "%s* This", name);
763     count++;
764   }
765   if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
766     if (count) {
767         if (do_indent)
768         {
769             fprintf(h, ",\n");
770             indent(h, 0);
771         }
772         else fprintf(h, ",");
773     }
774     write_type_decl(h, arg->type, arg->name);
775     count++;
776   }
777   if (do_indent) indentation--;
778 }
779
780 static void write_cpp_method_def(FILE *header, const type_t *iface)
781 {
782   const statement_t *stmt;
783
784   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
785   {
786     const var_t *func = stmt->u.var;
787     if (!is_callas(func->attrs)) {
788       const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
789       if (!callconv) callconv = "STDMETHODCALLTYPE";
790       indent(header, 0);
791       fprintf(header, "virtual ");
792       write_type_decl_left(header, type_function_get_rettype(func->type));
793       fprintf(header, " %s %s(\n", callconv, get_name(func));
794       write_args(header, type_get_function_args(func->type), iface->name, 2, TRUE);
795       fprintf(header, ") = 0;\n");
796       fprintf(header, "\n");
797     }
798   }
799 }
800
801 static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
802 {
803   const statement_t *stmt;
804   int first_iface = 1;
805
806   if (type_iface_get_inherit(iface))
807     do_write_c_method_def(header, type_iface_get_inherit(iface), name);
808
809   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
810   {
811     const var_t *func = stmt->u.var;
812     if (first_iface) {
813       indent(header, 0);
814       fprintf(header, "/*** %s methods ***/\n", iface->name);
815       first_iface = 0;
816     }
817     if (!is_callas(func->attrs)) {
818       const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
819       if (!callconv) callconv = "STDMETHODCALLTYPE";
820       indent(header, 0);
821       write_type_decl_left(header, type_function_get_rettype(func->type));
822       fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
823       write_args(header, type_get_function_args(func->type), name, 1, TRUE);
824       fprintf(header, ");\n");
825       fprintf(header, "\n");
826     }
827   }
828 }
829
830 static void write_c_method_def(FILE *header, const type_t *iface)
831 {
832   do_write_c_method_def(header, iface, iface->name);
833 }
834
835 static void write_c_disp_method_def(FILE *header, const type_t *iface)
836 {
837   do_write_c_method_def(header, type_iface_get_inherit(iface), iface->name);
838 }
839
840 static void write_method_proto(FILE *header, const type_t *iface)
841 {
842   const statement_t *stmt;
843
844   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
845   {
846     const var_t *func = stmt->u.var;
847
848     if (!is_local(func->attrs)) {
849       const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
850       if (!callconv) callconv = "STDMETHODCALLTYPE";
851       /* proxy prototype */
852       write_type_decl_left(header, type_function_get_rettype(func->type));
853       fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
854       write_args(header, type_get_function_args(func->type), iface->name, 1, TRUE);
855       fprintf(header, ");\n");
856       /* stub prototype */
857       fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
858       fprintf(header, "    IRpcStubBuffer* This,\n");
859       fprintf(header, "    IRpcChannelBuffer* pRpcChannelBuffer,\n");
860       fprintf(header, "    PRPC_MESSAGE pRpcMessage,\n");
861       fprintf(header, "    DWORD* pdwStubPhase);\n");
862     }
863   }
864 }
865
866 static void write_locals(FILE *fp, const type_t *iface, int body)
867 {
868   static const char comment[]
869     = "/* WIDL-generated stub.  You must provide an implementation for this.  */";
870   const statement_t *stmt;
871
872   if (!is_object(iface))
873     return;
874
875   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
876     const var_t *func = stmt->u.var;
877     const var_t *cas = is_callas(func->attrs);
878
879     if (cas) {
880       const statement_t *stmt2 = NULL;
881       STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
882         if (!strcmp(stmt2->u.var->name, cas->name))
883           break;
884       if (&stmt2->entry != type_iface_get_stmts(iface)) {
885         const var_t *m = stmt2->u.var;
886         /* proxy prototype - use local prototype */
887         write_type_decl_left(fp, type_function_get_rettype(m->type));
888         fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
889         write_args(fp, type_get_function_args(m->type), iface->name, 1, TRUE);
890         fprintf(fp, ")");
891         if (body) {
892           type_t *rt = type_function_get_rettype(m->type);
893           fprintf(fp, "\n{\n");
894           fprintf(fp, "    %s\n", comment);
895           if (rt->name && strcmp(rt->name, "HRESULT") == 0)
896             fprintf(fp, "    return E_NOTIMPL;\n");
897           else if (type_get_type(rt) != TYPE_VOID) {
898             fprintf(fp, "    ");
899             write_type_decl(fp, rt, "rv");
900             fprintf(fp, ";\n");
901             fprintf(fp, "    memset(&rv, 0, sizeof rv);\n");
902             fprintf(fp, "    return rv;\n");
903           }
904           fprintf(fp, "}\n\n");
905         }
906         else
907           fprintf(fp, ";\n");
908         /* stub prototype - use remotable prototype */
909         write_type_decl_left(fp, type_function_get_rettype(func->type));
910         fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
911         write_args(fp, type_get_function_args(func->type), iface->name, 1, TRUE);
912         fprintf(fp, ")");
913         if (body)
914           /* Remotable methods must all return HRESULTs.  */
915           fprintf(fp, "\n{\n    %s\n    return E_NOTIMPL;\n}\n\n", comment);
916         else
917           fprintf(fp, ";\n");
918       }
919       else
920         error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
921     }
922   }
923 }
924
925 static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
926 {
927   const statement_t *stmt;
928   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
929   {
930     if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
931       write_locals(local_stubs, stmt->u.type, TRUE);
932     else if (stmt->type == STMT_LIBRARY)
933       write_local_stubs_stmts(local_stubs, stmt->u.lib->stmts);
934   }
935 }
936
937 void write_local_stubs(const statement_list_t *stmts)
938 {
939   FILE *local_stubs;
940
941   if (!local_stubs_name) return;
942
943   local_stubs = fopen(local_stubs_name, "w");
944   if (!local_stubs) {
945     error("Could not open %s for output\n", local_stubs_name);
946     return;
947   }
948   fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
949   fprintf(local_stubs, "#include <objbase.h>\n");
950   fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
951
952   write_local_stubs_stmts(local_stubs, stmts);
953
954   fclose(local_stubs);
955 }
956
957 static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
958 {
959   const char *callconv = get_attrp(fun->type->attrs, ATTR_CALLCONV);
960
961   /* FIXME: do we need to handle call_as? */
962   write_type_decl_left(header, type_function_get_rettype(fun->type));
963   fprintf(header, " ");
964   if (callconv) fprintf(header, "%s ", callconv);
965   fprintf(header, "%s%s(\n", prefix, get_name(fun));
966   if (type_get_function_args(fun->type))
967     write_args(header, type_get_function_args(fun->type), iface->name, 0, TRUE);
968   else
969     fprintf(header, "    void");
970   fprintf(header, ");\n\n");
971 }
972
973 static void write_forward(FILE *header, type_t *iface)
974 {
975   fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
976   fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
977   fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
978   fprintf(header, "#endif\n\n" );
979 }
980
981 static void write_iface_guid(FILE *header, const type_t *iface)
982 {
983   const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
984   write_guid(header, "IID", iface->name, uuid);
985
986
987 static void write_dispiface_guid(FILE *header, const type_t *iface)
988 {
989   const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
990   write_guid(header, "DIID", iface->name, uuid);
991 }
992
993 static void write_coclass_guid(FILE *header, const type_t *cocl)
994 {
995   const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
996   write_guid(header, "CLSID", cocl->name, uuid);
997 }
998
999 static void write_com_interface_start(FILE *header, const type_t *iface)
1000 {
1001   int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1002   fprintf(header, "/*****************************************************************************\n");
1003   fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
1004   fprintf(header, " */\n");
1005   fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->name, dispinterface ? "DISP" : "");
1006   fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->name, dispinterface ? "DISP" : "");
1007 }
1008
1009 static void write_com_interface_end(FILE *header, type_t *iface)
1010 {
1011   int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
1012   if (dispinterface)
1013     write_dispiface_guid(header, iface);
1014   else
1015     write_iface_guid(header, iface);
1016   /* C++ interface */
1017   fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
1018   if (type_iface_get_inherit(iface))
1019   {
1020     fprintf(header, "interface %s : public %s\n", iface->name,
1021             type_iface_get_inherit(iface)->name);
1022     fprintf(header, "{\n");
1023   }
1024   else
1025   {
1026     fprintf(header, "interface %s\n", iface->name);
1027     fprintf(header, "{\n");
1028     fprintf(header, "    BEGIN_INTERFACE\n");
1029     fprintf(header, "\n");
1030   }
1031   /* dispinterfaces don't have real functions, so don't write C++ functions for
1032    * them */
1033   if (!dispinterface)
1034   {
1035     indentation++;
1036     write_cpp_method_def(header, iface);
1037     indentation--;
1038   }
1039   if (!type_iface_get_inherit(iface))
1040     fprintf(header, "    END_INTERFACE\n");
1041   fprintf(header, "};\n");
1042   fprintf(header, "#else\n");
1043   /* C interface */
1044   fprintf(header, "typedef struct %sVtbl {\n", iface->name);
1045   indentation++;
1046   fprintf(header, "    BEGIN_INTERFACE\n");
1047   fprintf(header, "\n");
1048   if (dispinterface)
1049     write_c_disp_method_def(header, iface);
1050   else
1051     write_c_method_def(header, iface);
1052   indentation--;
1053   fprintf(header, "    END_INTERFACE\n");
1054   fprintf(header, "} %sVtbl;\n", iface->name);
1055   fprintf(header, "interface %s {\n", iface->name);
1056   fprintf(header, "    CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
1057   fprintf(header, "};\n");
1058   fprintf(header, "\n");
1059   fprintf(header, "#ifdef COBJMACROS\n");
1060   /* dispinterfaces don't have real functions, so don't write macros for them,
1061    * only for the interface this interface inherits from, i.e. IDispatch */
1062   write_method_macro(header, dispinterface ? type_iface_get_inherit(iface) : iface, iface->name);
1063   fprintf(header, "#endif\n");
1064   fprintf(header, "\n");
1065   fprintf(header, "#endif\n");
1066   fprintf(header, "\n");
1067   /* dispinterfaces don't have real functions, so don't write prototypes for
1068    * them */
1069   if (!dispinterface)
1070   {
1071     write_method_proto(header, iface);
1072     write_locals(header, iface, FALSE);
1073     fprintf(header, "\n");
1074   }
1075   fprintf(header,"#endif  /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->name, dispinterface ? "DISP" : "");
1076 }
1077
1078 static void write_rpc_interface_start(FILE *header, const type_t *iface)
1079 {
1080   unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
1081   const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
1082
1083   fprintf(header, "/*****************************************************************************\n");
1084   fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1085   fprintf(header, " */\n");
1086   fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
1087   fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
1088   if (var) fprintf(header, "extern handle_t %s;\n", var);
1089   if (old_names)
1090   {
1091       fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
1092       fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
1093   }
1094   else
1095   {
1096       fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
1097               prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1098       fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
1099               prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
1100   }
1101 }
1102
1103 static void write_rpc_interface_end(FILE *header, const type_t *iface)
1104 {
1105   fprintf(header,"\n#endif  /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
1106 }
1107
1108 static void write_coclass(FILE *header, type_t *cocl)
1109 {
1110   fprintf(header, "/*****************************************************************************\n");
1111   fprintf(header, " * %s coclass\n", cocl->name);
1112   fprintf(header, " */\n\n");
1113   write_coclass_guid(header, cocl);
1114   fprintf(header, "\n");
1115 }
1116
1117 static void write_coclass_forward(FILE *header, type_t *cocl)
1118 {
1119   fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
1120   fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
1121   fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
1122   fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
1123 }
1124
1125 static void write_import(FILE *header, const char *fname)
1126 {
1127   char *hname, *p;
1128
1129   hname = dup_basename(fname, ".idl");
1130   p = hname + strlen(hname) - 2;
1131   if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
1132
1133   fprintf(header, "#include <%s>\n", hname);
1134   free(hname);
1135 }
1136
1137 static void write_imports(FILE *header, const statement_list_t *stmts)
1138 {
1139   const statement_t *stmt;
1140   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1141   {
1142     switch (stmt->type)
1143     {
1144       case STMT_TYPE:
1145         if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1146           write_imports(header, type_iface_get_stmts(stmt->u.type));
1147         break;
1148       case STMT_TYPEREF:
1149       case STMT_IMPORTLIB:
1150         /* not included in header */
1151         break;
1152       case STMT_IMPORT:
1153         write_import(header, stmt->u.str);
1154         break;
1155       case STMT_TYPEDEF:
1156       case STMT_MODULE:
1157       case STMT_CPPQUOTE:
1158       case STMT_DECLARATION:
1159         /* not processed here */
1160         break;
1161       case STMT_LIBRARY:
1162         write_imports(header, stmt->u.lib->stmts);
1163         break;
1164     }
1165   }
1166 }
1167
1168 static void write_forward_decls(FILE *header, const statement_list_t *stmts)
1169 {
1170   const statement_t *stmt;
1171   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1172   {
1173     switch (stmt->type)
1174     {
1175       case STMT_TYPE:
1176         if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1177         {
1178           if (is_object(stmt->u.type) || is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE))
1179             write_forward(header, stmt->u.type);
1180         }
1181         else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1182           write_coclass_forward(header, stmt->u.type);
1183         break;
1184       case STMT_TYPEREF:
1185       case STMT_IMPORTLIB:
1186         /* not included in header */
1187         break;
1188       case STMT_IMPORT:
1189       case STMT_TYPEDEF:
1190       case STMT_MODULE:
1191       case STMT_CPPQUOTE:
1192       case STMT_DECLARATION:
1193         /* not processed here */
1194         break;
1195       case STMT_LIBRARY:
1196         write_forward_decls(header, stmt->u.lib->stmts);
1197         break;
1198     }
1199   }
1200 }
1201
1202 static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
1203 {
1204   const statement_t *stmt;
1205   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1206   {
1207     switch (stmt->type)
1208     {
1209       case STMT_TYPE:
1210         if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
1211         {
1212           type_t *iface = stmt->u.type;
1213           if (is_object(iface)) is_object_interface++;
1214           if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
1215           {
1216             write_com_interface_start(header, iface);
1217             write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
1218             write_com_interface_end(header, iface);
1219           }
1220           else
1221           {
1222             write_rpc_interface_start(header, iface);
1223             write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
1224             write_rpc_interface_end(header, iface);
1225           }
1226           if (is_object(iface)) is_object_interface++;
1227         }
1228         else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
1229           write_coclass(header, stmt->u.type);
1230         else
1231         {
1232           write_type_def_or_decl(header, stmt->u.type, FALSE, NULL);
1233           fprintf(header, ";\n\n");
1234         }
1235         break;
1236       case STMT_TYPEREF:
1237         /* FIXME: shouldn't write out forward declarations for undefined
1238         * interfaces but a number of our IDL files depend on this */
1239         if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
1240           write_forward(header, stmt->u.type);
1241         break;
1242       case STMT_IMPORTLIB:
1243       case STMT_MODULE:
1244         /* not included in header */
1245         break;
1246       case STMT_IMPORT:
1247         /* not processed here */
1248         break;
1249       case STMT_TYPEDEF:
1250       {
1251         const type_list_t *type_entry = stmt->u.type_list;
1252         for (; type_entry; type_entry = type_entry->next)
1253           write_typedef(header, type_entry->type);
1254         break;
1255       }
1256       case STMT_LIBRARY:
1257         write_library(header, stmt->u.lib);
1258         write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
1259         break;
1260       case STMT_CPPQUOTE:
1261         fprintf(header, "%s\n", stmt->u.str);
1262         break;
1263       case STMT_DECLARATION:
1264         if (iface && type_get_type(stmt->u.var->type) == TYPE_FUNCTION)
1265         {
1266           if (!ignore_funcs)
1267           {
1268             int prefixes_differ = strcmp(prefix_client, prefix_server);
1269
1270             if (prefixes_differ)
1271             {
1272               fprintf(header, "/* client prototype */\n");
1273               write_function_proto(header, iface, stmt->u.var, prefix_client);
1274               fprintf(header, "/* server prototype */\n");
1275             }
1276             write_function_proto(header, iface, stmt->u.var, prefix_server);
1277           }
1278         }
1279         else
1280           write_declaration(header, stmt->u.var);
1281         break;
1282     }
1283   }
1284 }
1285
1286 void write_header(const statement_list_t *stmts)
1287 {
1288   FILE *header;
1289
1290   if (!do_header) return;
1291
1292   if(!(header = fopen(header_name, "w"))) {
1293     error("Could not open %s for output\n", header_name);
1294     return;
1295   }
1296   fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
1297   fprintf(header, "#include <rpc.h>\n" );
1298   fprintf(header, "#include <rpcndr.h>\n\n" );
1299
1300   fprintf(header, "#ifndef __WIDL_%s\n", header_token);
1301   fprintf(header, "#define __WIDL_%s\n\n", header_token);
1302
1303   fprintf(header, "/* Forward declarations */\n\n");
1304   write_forward_decls(header, stmts);
1305
1306   fprintf(header, "/* Headers for imported files */\n\n");
1307   write_imports(header, stmts);
1308   fprintf(header, "\n");
1309   start_cplusplus_guard(header);
1310
1311   write_header_stmts(header, stmts, NULL, FALSE);
1312
1313   fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
1314   fprintf(header, "\n");
1315   write_user_types(header);
1316   write_generic_handle_routines(header);
1317   write_context_handle_rundowns(header);
1318   fprintf(header, "\n");
1319   fprintf(header, "/* End additional prototypes */\n");
1320   fprintf(header, "\n");
1321
1322   end_cplusplus_guard(header);
1323   fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
1324
1325   fclose(header);
1326 }