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