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