widl: Include objbase.h so that generated proxy file compiles on Wine.
[wine] / tools / widl / proxy.c
1 /*
2  * IDL Compiler
3  *
4  * Copyright 2002 Ove Kaaven
5  * Copyright 2004 Mike McCormack
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <signal.h>
34
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "typegen.h"
40
41 #define END_OF_LIST(list)       \
42   do {                          \
43     if (list) {                 \
44       while (NEXT_LINK(list))   \
45         list = NEXT_LINK(list); \
46     }                           \
47   } while(0)
48
49 static FILE* proxy;
50 static int indent = 0;
51
52 /* FIXME: support generation of stubless proxies */
53
54 static int print_proxy( const char *format, ... )
55 {
56   va_list va;
57   int i, r;
58
59   va_start( va, format );
60   if ( format[0] != '\n' )
61     for( i=0; i<indent; i++ )
62       fprintf( proxy, "    " );
63   r = vfprintf( proxy, format, va );
64   va_end( va );
65   return r;
66 }
67
68 static void write_stubdescproto(void)
69 {
70   print_proxy( "extern const MIDL_STUB_DESC Object_StubDesc;\n");
71   print_proxy( "\n");
72 }
73
74 static void write_stubdesc(void)
75 {
76   print_proxy( "const MIDL_STUB_DESC Object_StubDesc = {\n");
77   print_proxy( "    0,\n");
78   print_proxy( "    NdrOleAllocate,\n");
79   print_proxy( "    NdrOleFree,\n");
80   print_proxy( "    {0}, 0, 0, 0, 0,\n");
81   print_proxy( "    0 /* __MIDL_TypeFormatString.Format */\n");
82   print_proxy( "};\n");
83   print_proxy( "\n");
84 }
85
86 static void init_proxy(ifref_t *ifaces)
87 {
88   if (proxy) return;
89   if(!(proxy = fopen(proxy_name, "w")))
90     error("Could not open %s for output\n", proxy_name);
91   print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
92   print_proxy( "\n");
93   print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
94   print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ 440\n");
95   print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
96   print_proxy( "\n");
97   print_proxy( "#include \"objbase.h\"\n");
98   print_proxy( "#include \"rpcproxy.h\"\n");
99   print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
100   print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
101   print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
102   print_proxy( "\n");
103   print_proxy( "#include \"%s\"\n", header_name);
104   print_proxy( "\n");
105   write_formatstringsdecl(proxy, indent, ifaces, 1);
106   write_stubdescproto();
107 }
108
109 static void clear_output_vars( var_t *arg )
110 {
111   END_OF_LIST(arg);
112   while (arg) {
113     if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
114       print_proxy( "if(%s)\n", arg->name );
115       indent++;
116       print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name );
117       indent--;
118     }
119     arg = PREV_LINK(arg);
120   }
121 }
122
123 int is_var_ptr(var_t *v)
124 {
125   return v->ptr_level || is_ptr(v->type);
126 }
127
128 int cant_be_null(var_t *v)
129 {
130   /* Search backwards for the most recent pointer attribute.  */
131   const attr_t *attrs = v->attrs;
132   const type_t *type = v->type;
133
134   if (! attrs && type)
135   {
136     attrs = type->attrs;
137     type = type->ref;
138   }
139
140   while (attrs)
141   {
142     int t = get_attrv(attrs, ATTR_POINTERTYPE);
143
144     if (t == RPC_FC_FP || t == RPC_FC_OP || t == RPC_FC_UP)
145       return 0;
146
147     if (t == RPC_FC_RP)
148       return 1;
149
150     if (type)
151     {
152       attrs = type->attrs;
153       type = type->ref;
154     }
155     else
156       attrs = NULL;
157   }
158
159   return 1;                             /* Default is RPC_FC_RP.  */
160 }
161
162 static int is_user_derived(var_t *v)
163 {
164   const attr_t *attrs = v->attrs;
165   const type_t *type = v->type;
166
167   if (! attrs && type)
168   {
169     attrs = type->attrs;
170     type = type->ref;
171   }
172
173   while (attrs)
174   {
175     if (is_attr(attrs, ATTR_WIREMARSHAL))
176       return 1;
177
178     if (type)
179     {
180       attrs = type->attrs;
181       type = type->ref;
182     }
183     else
184       attrs = NULL;
185   }
186
187   return 0;
188 }
189
190 static void proxy_check_pointers( var_t *arg )
191 {
192   END_OF_LIST(arg);
193   while (arg) {
194     if (is_var_ptr(arg) && cant_be_null(arg)) {
195         print_proxy( "if(!%s)\n", arg->name );
196         indent++;
197         print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
198         indent--;
199     }
200     arg = PREV_LINK(arg);
201   }
202 }
203
204 static void marshall_size_arg( var_t *arg )
205 {
206   int index = 0;
207   const type_t *type = arg->type;
208   expr_t *expr;
209
210   expr = get_attrp( arg->attrs, ATTR_SIZEIS );
211   if (expr)
212   {
213     print_proxy( "_StubMsg.MaxCount = ", arg->name );
214     write_expr(proxy, expr, 0);
215     fprintf(proxy, ";\n\n");
216     print_proxy( "NdrConformantArrayBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
217     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
218     return;
219   }
220
221   if (is_user_derived(arg))
222   {
223     print_proxy("NdrUserMarshalBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name);
224     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index);
225     return;
226   }
227
228   if (is_string_type(arg->attrs, arg->ptr_level, arg->array))
229   {
230     print_proxy("NdrConformantStringBufferSize( &_StubMsg, (unsigned char*)s, ", arg->name);
231     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index);
232   }
233
234   switch( type->type )
235   {
236   case RPC_FC_BYTE:
237   case RPC_FC_CHAR:
238     print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 1, arg->name );
239     break;
240
241   case RPC_FC_WCHAR:
242   case RPC_FC_SHORT:
243   case RPC_FC_USHORT:
244   case RPC_FC_ENUM16:
245     print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 2, arg->name );
246     break;
247
248   case RPC_FC_LONG:
249   case RPC_FC_ULONG:
250   case RPC_FC_ENUM32:
251     print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 4, arg->name );
252     break;
253       
254   case RPC_FC_STRUCT:
255     print_proxy( "NdrSimpleStructBufferSize(&_StubMsg, (unsigned char*)%s, ", arg->name );
256     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
257     break;
258
259   case RPC_FC_CARRAY:
260     print_proxy( "NdrConformantArrayBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
261     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
262     break;
263
264   case RPC_FC_BOGUS_STRUCT:
265     print_proxy( "NdrComplexStructBufferSize(&_StubMsg, (unsigned char*)%s, ", arg->name );
266     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
267     break;
268
269   case RPC_FC_FP:
270     {
271       var_t temp;
272       memset( &temp, 0, sizeof temp );
273       temp.type = type->ref;
274       temp.name = arg->name; /* FIXME */
275 #if 0
276       print_proxy( "/* FIXME: %s use the right name for %s */\n", __FUNCTION__, arg->name );
277 #endif
278       marshall_size_arg( &temp );
279     }
280     break;
281
282   case RPC_FC_IP:
283     print_proxy( "NdrPointerBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
284     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
285     break;
286
287   default:
288     print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
289   }
290 }
291
292 static void proxy_gen_marshall_size( var_t *arg )
293 {
294   print_proxy( "_StubMsg.BufferLength = 0U;\n" );
295
296   END_OF_LIST(arg);
297   while (arg) {
298     if (is_attr(arg->attrs, ATTR_IN)) 
299     {
300       marshall_size_arg( arg );
301       fprintf(proxy, "\n");
302     }
303     arg = PREV_LINK(arg);
304   }
305 }
306
307 static void marshall_copy_arg( var_t *arg )
308 {
309   int index = 0;
310   type_t *type = arg->type;
311   expr_t *expr;
312
313   expr = get_attrp( arg->attrs, ATTR_SIZEIS );
314   if (expr)
315   {
316     print_proxy( "_StubMsg.MaxCount = ", arg->name );
317     write_expr(proxy, expr, 0);
318     fprintf(proxy, ";\n\n");
319     print_proxy( "NdrConformantArrayMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name );
320     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
321     return;
322   }
323
324   if (is_user_derived(arg))
325   {
326     print_proxy("NdrUserMarshalMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name);
327     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index);
328     return;
329   }
330
331   if (is_string_type(arg->attrs, arg->ptr_level, arg->array))
332   {
333     print_proxy("NdrConformantStringMarshall( &_StubMsg, (unsigned char*)s, ", arg->name);
334     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index);
335   }
336
337   switch( type->type )
338   {
339   case RPC_FC_BYTE:
340   case RPC_FC_CHAR:
341   case RPC_FC_WCHAR:
342   case RPC_FC_SHORT:
343   case RPC_FC_USHORT:
344   case RPC_FC_ENUM16:
345   case RPC_FC_LONG:
346   case RPC_FC_ULONG:
347   case RPC_FC_ENUM32:
348     print_proxy( "*(");
349     write_type(proxy, arg->type, arg, arg->tname);
350     fprintf(proxy, " *)_StubMsg.Buffer = %s;\n", arg->name );
351     print_proxy("_StubMsg.Buffer += sizeof(");
352     write_type(proxy, arg->type, arg, arg->tname);
353     fprintf(proxy, ");\n");
354     break;
355       
356   case RPC_FC_STRUCT:
357     /* FIXME: add the format string, and set the index below */
358     print_proxy( "NdrSimpleStructMarshall(&_StubMsg, (unsigned char*)%s, ", arg->name );
359     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
360     break;
361
362   case RPC_FC_CARRAY:
363     break;
364
365   case RPC_FC_BOGUS_STRUCT:
366     print_proxy( "NdrComplexStructMarshall(&_StubMsg, (unsigned char*)%s, ", arg->name );
367     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
368     break;
369
370   case RPC_FC_FP:
371     {
372       var_t temp;
373       memset( &temp, 0, sizeof temp );
374       temp.type = type->ref;
375       temp.name = arg->name; /* FIXME */
376 #if 0
377       print_proxy( "/* FIXME: %s use the right name for %s */\n", __FUNCTION__, arg->name );
378 #endif
379       marshall_copy_arg( &temp );
380     }
381     break;
382
383   case RPC_FC_IP:
384     print_proxy( "NdrPointerMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name );
385     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
386     break;
387
388   default:
389     print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
390   }
391 }
392
393 static void gen_marshall_copydata( var_t *arg )
394 {
395   END_OF_LIST(arg);
396   while (arg) {
397     if (is_attr(arg->attrs, ATTR_IN)) 
398     {
399       marshall_copy_arg( arg );
400       fprintf(proxy, "\n");
401     }
402     arg = PREV_LINK(arg);
403   }
404 }
405
406 static void gen_marshall( var_t *arg )
407 {
408   /* generated code to determine the size of the buffer required */
409   proxy_gen_marshall_size( arg );
410
411   /* generated code to allocate the buffer */
412   print_proxy( "NdrProxyGetBuffer(This, &_StubMsg);\n" );
413
414   /* generated code to copy the args into the buffer */
415   gen_marshall_copydata( arg );
416
417   print_proxy( "\n");
418 }
419
420 static void unmarshall_copy_arg( var_t *arg )
421 {
422   int index = 0;
423   type_t *type = arg->type;
424   expr_t *expr;
425
426   expr = get_attrp( arg->attrs, ATTR_SIZEIS );
427   if (expr)
428   {
429     print_proxy( "NdrConformantArrayUnmarshall( &_StubMsg, (unsigned char**)&%s, ", arg->name );
430     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
431     return;
432   }
433
434   if (is_user_derived(arg))
435   {
436     print_proxy("NdrUserMarshalUnmarshall( &_StubMsg, (unsigned char**)&%s, ", arg->name);
437     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0 );\n", index);
438     return;
439   }
440
441   if (is_string_type(arg->attrs, arg->ptr_level, arg->array))
442   {
443     print_proxy("NdrConformantStringUnmarshall( &_StubMsg, (unsigned char**)&s, ", arg->name);
444     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0 );\n", index);
445   }
446
447   switch( type->type )
448   {
449   case RPC_FC_BYTE:
450   case RPC_FC_CHAR:
451   case RPC_FC_WCHAR:
452   case RPC_FC_SHORT:
453   case RPC_FC_USHORT:
454   case RPC_FC_ENUM16:
455   case RPC_FC_LONG:
456   case RPC_FC_ULONG:
457   case RPC_FC_ENUM32:
458     print_proxy( "%s = *(", arg->name );
459     write_type(proxy, arg->type, arg, arg->tname);
460     fprintf(proxy," *)_StubMsg.Buffer;\n");
461     print_proxy("_StubMsg.Buffer += sizeof(");
462     write_type(proxy, arg->type, arg, arg->tname);
463     fprintf(proxy, ");\n");
464     break;
465       
466   case RPC_FC_STRUCT:
467     print_proxy( "NdrSimpleStructUnmarshall(&_StubMsg, (unsigned char**)%s, ", arg->name );
468     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
469     break;
470
471   case RPC_FC_CARRAY:
472     print_proxy( "NdrConformantArrayUnmarshall( &_StubMsg, (unsigned char**)&%s, ", arg->name );
473     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
474     break;
475
476   case RPC_FC_BOGUS_STRUCT:
477     print_proxy( "NdrComplexStructUnmarshall(&_StubMsg, (unsigned char**)&%s, ", arg->name );
478     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0 );\n", index );
479     break;
480
481   case RPC_FC_FP:
482     {
483       var_t temp;
484       memset( &temp, 0, sizeof temp );
485       temp.type = type->ref;
486       temp.name = arg->name; /* FIXME */
487 #if 1
488       print_proxy( "/* FIXME: %s use the right name for %s */\n", __FUNCTION__, arg->name );
489 #endif
490       unmarshall_copy_arg( &temp );
491     }
492     break;
493
494   case RPC_FC_IP:
495     print_proxy( "NdrPointerUnmarshall(&_StubMsg, (unsigned char**)&%s, ", arg->name );
496     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
497     break;
498
499   default:
500     print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
501   }
502 }
503
504 static void gen_unmarshall( var_t *arg )
505 {
506   END_OF_LIST(arg);
507   while (arg) {
508     if (is_attr(arg->attrs, ATTR_OUT)) 
509     {
510       unmarshall_copy_arg( arg );
511       fprintf(proxy, "\n");
512     }
513     arg = PREV_LINK(arg);
514   }
515 }
516
517 static void free_variable( var_t *arg )
518 {
519   var_t *constraint;
520   int index = 0; /* FIXME */
521   type_t *type;
522   expr_t *expr;
523
524   expr = get_attrp( arg->attrs, ATTR_SIZEIS );
525   if (expr)
526   {
527     print_proxy( "_StubMsg.MaxCount = ", arg->name );
528     write_expr(proxy, expr, 0);
529     fprintf(proxy, ";\n\n");
530     print_proxy( "NdrClearOutParameters( &_StubMsg, ");
531     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], ", index );
532     fprintf(proxy, "(void*)%s );\n", arg->name );
533     return;
534   }
535
536   type = arg->type;
537   switch( type->type )
538   {
539   case RPC_FC_BYTE:
540   case RPC_FC_CHAR:
541   case RPC_FC_WCHAR:
542   case RPC_FC_SHORT:
543   case RPC_FC_USHORT:
544   case RPC_FC_ENUM16:
545   case RPC_FC_LONG:
546   case RPC_FC_ULONG:
547   case RPC_FC_ENUM32:
548   case RPC_FC_STRUCT:
549     break;
550
551   case RPC_FC_FP:
552   case RPC_FC_IP:
553     constraint = get_attrp( arg->attrs, ATTR_IIDIS );
554     if( constraint )
555       print_proxy( "_StubMsg.MaxCount = (unsigned long) ( %s );\n",constraint->name);
556     print_proxy( "NdrClearOutParameters( &_StubMsg, ");
557     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], ", index );
558     fprintf(proxy, "(void*)%s );\n", arg->name );
559     break;
560
561   default:
562     print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
563   }
564 }
565
566 static void proxy_free_variables( var_t *arg )
567 {
568   END_OF_LIST(arg);
569   while (arg) {
570     if (is_attr(arg->attrs, ATTR_OUT)) 
571     {
572       free_variable( arg );
573       fprintf(proxy, "\n");
574     }
575     arg = PREV_LINK(arg);
576   }
577 }
578
579 static void gen_proxy(type_t *iface, func_t *cur, int idx)
580 {
581   var_t *def = cur->def;
582   int has_ret = !is_void(def->type, def);
583
584   indent = 0;
585   write_type(proxy, def->type, def, def->tname);
586   print_proxy( " STDMETHODCALLTYPE %s_", iface->name);
587   write_name(proxy, def);
588   print_proxy( "_Proxy(\n");
589   write_args(proxy, cur->args, iface->name, 1, TRUE);
590   print_proxy( ")\n");
591   print_proxy( "{\n");
592   indent ++;
593   /* local variables */
594   if (has_ret) {
595     print_proxy( "" );
596     write_type(proxy, def->type, def, def->tname);
597     print_proxy( " _RetVal;\n");
598   }
599   print_proxy( "RPC_MESSAGE _Msg;\n" );
600   print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n" );
601   print_proxy( "\n");
602
603   /* FIXME: trace */
604   clear_output_vars( cur->args );
605
606   print_proxy( "RpcTryExcept\n" );
607   print_proxy( "{\n" );
608   indent++;
609   print_proxy( "NdrProxyInitialize(This, &_Msg, &_StubMsg, &Object_StubDesc, %d);\n", idx);
610   proxy_check_pointers( cur->args );
611
612   print_proxy( "RpcTryFinally\n" );
613   print_proxy( "{\n" );
614   indent++;
615
616   gen_marshall( cur->args );
617
618   print_proxy( "NdrProxySendReceive(This, &_StubMsg);\n" );
619   fprintf(proxy, "\n");
620   print_proxy("if ((_Msg.DataRepresentation&0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
621   indent++;
622   print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[0]);\n" );
623   indent--;
624   fprintf(proxy, "\n");
625
626   gen_unmarshall( cur->args );
627   if (has_ret) {
628     /* 
629      * FIXME: We only need to round the buffer up if it could be unaligned...
630      *    We should calculate how much buffer we used and output the following
631      *    line only if necessary.
632      */
633     print_proxy( "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + 3) & ~ 0x3);\n");
634
635     print_proxy( "_RetVal = *(" );
636     write_type(proxy, def->type, def, def->tname);
637     fprintf(proxy, " *)_StubMsg.Buffer;\n");
638     print_proxy("_StubMsg.Buffer += sizeof(");
639     write_type(proxy, def->type, def, def->tname);
640     fprintf(proxy, ");\n");
641   }
642
643   indent--;
644   print_proxy( "}\n");
645   print_proxy( "RpcFinally\n" );
646   print_proxy( "{\n" );
647   indent++;
648   print_proxy( "NdrProxyFreeBuffer(This, &_StubMsg);\n" );
649   indent--;
650   print_proxy( "}\n");
651   print_proxy( "RpcEndFinally\n" );
652   indent--;
653   print_proxy( "}\n" );
654   print_proxy( "RpcExcept(_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
655   print_proxy( "{\n" );
656   if (has_ret) {
657     indent++;
658     proxy_free_variables( cur->args );
659     print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
660     indent--;
661   }
662   print_proxy( "}\n" );
663   print_proxy( "RpcEndExcept\n" );
664
665   if (has_ret) {
666     print_proxy( "return _RetVal;\n" );
667   }
668   indent--;
669   print_proxy( "}\n");
670   print_proxy( "\n");
671 }
672
673 static void stub_write_locals( var_t *arg )
674 {
675   int n = 0;
676   END_OF_LIST(arg);
677   while (arg) {
678     int outptr = is_attr(arg->attrs, ATTR_OUT)
679                  && ! is_attr(arg->attrs, ATTR_IN);
680
681     /* create a temporary variable to store the output */
682     if (outptr) {
683       var_t temp;
684       memset( &temp, 0, sizeof temp );
685       temp.ptr_level = arg->ptr_level - 1; /* dereference once */
686       print_proxy("");
687       write_type(proxy, arg->type, &temp, arg->tname);
688       fprintf(proxy, " _M%d;\n",n++);
689     }
690     print_proxy("");
691     write_type(proxy, arg->type, arg, arg->tname);
692     fprintf(proxy, " ");
693     write_name(proxy, arg);
694     fprintf(proxy, ";\n");
695     arg = PREV_LINK(arg);
696   }
697 }
698
699 static void stub_unmarshall( var_t *arg )
700 {
701   int n = 0;
702   END_OF_LIST(arg);
703   while (arg) {
704     if (is_attr(arg->attrs, ATTR_IN))
705     {
706       unmarshall_copy_arg( arg );
707       fprintf(proxy,"\n");
708     }
709     else if (is_attr(arg->attrs, ATTR_OUT)) {
710       type_t *type = arg->type;
711       switch( type->type )
712       {
713       case RPC_FC_STRUCT:
714         print_proxy("MIDL_memset(");
715         write_name(proxy, arg);
716         fprintf(proxy,", 0, sizeof(");
717         write_type(proxy, arg->type, arg, arg->tname);
718         fprintf(proxy,"));\n");
719         break;
720       default:
721         print_proxy("");
722         write_name(proxy, arg);
723         fprintf(proxy," = &_M%d;\n", n);
724         print_proxy("MIDL_memset(&_M%d, 0, sizeof _M%d);\n", n, n);
725         ++n;
726         break;
727       }
728     }
729     arg = PREV_LINK(arg);
730   }
731 }
732
733 static void stub_gen_marshall_size( var_t *arg )
734 {
735   print_proxy( "_StubMsg.BufferLength = 0U;\n" );
736
737   END_OF_LIST(arg);
738   while (arg) {
739     if (is_attr(arg->attrs, ATTR_OUT))
740       marshall_size_arg( arg );
741     arg = PREV_LINK(arg);
742   }
743 }
744
745 static void stub_gen_marshall_copydata( var_t *arg )
746 {
747   END_OF_LIST(arg);
748   while (arg) {
749     if (is_attr(arg->attrs, ATTR_OUT))
750       marshall_copy_arg( arg );
751     arg = PREV_LINK(arg);
752   }
753 }
754
755 static void stub_genmarshall( var_t *args )
756 {
757   /* FIXME: size buffer */
758   stub_gen_marshall_size( args );
759
760   print_proxy("NdrStubGetBuffer(This, pRpcChannelBuffer, &_StubMsg);\n");
761
762   stub_gen_marshall_copydata( args );
763 }
764
765 static void gen_stub(type_t *iface, func_t *cur, const char *cas)
766 {
767   var_t *def = cur->def;
768   var_t *arg;
769   int has_ret = !is_void(def->type, def);
770
771   indent = 0;
772   print_proxy( "void __RPC_STUB %s_", iface->name);
773   write_name(proxy, def);
774   print_proxy( "_Stub(\n");
775   indent++;
776   print_proxy( "IRpcStubBuffer* This,\n");
777   print_proxy( "IRpcChannelBuffer* pRpcChannelBuffer,\n");
778   print_proxy( "PRPC_MESSAGE _Msg,\n");
779   print_proxy( "DWORD* _pdwStubPhase)\n");
780   indent--;
781   print_proxy( "{\n");
782   indent++;
783   /* local variables */
784   if (has_ret) {
785     print_proxy("");
786     write_type(proxy, def->type, def, def->tname);
787     fprintf(proxy, " _RetVal;\n");
788   }
789   print_proxy("%s * _This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n", iface->name, iface->name);
790   print_proxy("MIDL_STUB_MESSAGE _StubMsg;\n");
791   stub_write_locals( cur->args );
792   fprintf(proxy, "\n");
793
794   /* FIXME: trace */
795
796   print_proxy("NdrStubInitialize(_Msg, &_StubMsg, &Object_StubDesc, pRpcChannelBuffer);\n");
797   fprintf(proxy, "\n");
798
799   print_proxy("RpcTryFinally\n");
800   print_proxy("{\n");
801   indent++;
802   print_proxy("if ((_Msg->DataRepresentation&0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
803   indent++;
804   print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[0]);\n" );
805   indent--;
806   fprintf(proxy, "\n");
807
808   stub_unmarshall( cur->args );
809   fprintf(proxy, "\n");
810
811   print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
812   fprintf(proxy, "\n");
813   print_proxy("");
814   if (has_ret) fprintf(proxy, "_RetVal = ");
815   fprintf(proxy, "%s_", iface->name);
816   if (cas) fprintf(proxy, "%s_Stub", cas);
817   else write_name(proxy, def);
818   fprintf(proxy, "(_This");
819   arg = cur->args;
820   if (arg) {
821     END_OF_LIST(arg);
822     while (arg) {
823       fprintf(proxy, ", ");
824       write_name(proxy, arg);
825       arg = PREV_LINK(arg);
826     }
827   }
828   fprintf(proxy, ");\n");
829   fprintf(proxy, "\n");
830   print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
831   fprintf(proxy, "\n");
832
833   stub_genmarshall( cur->args );
834   fprintf(proxy, "\n");
835
836   if (has_ret) {
837     /* 
838      * FIXME: We only need to round the buffer up if it could be unaligned...
839      *    We should calculate how much buffer we used and output the following
840      *    line only if necessary.
841      */
842     print_proxy( "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + 3) & ~ 0x3);\n");
843
844     print_proxy( "*(" );
845     write_type(proxy, def->type, def, def->tname);
846     fprintf(proxy, " *)_StubMsg.Buffer = _RetVal;\n");
847     print_proxy("_StubMsg.Buffer += sizeof(");
848     write_type(proxy, def->type, def, def->tname);
849     fprintf(proxy, ");\n");
850   }
851
852   indent--;
853   print_proxy("}\n");
854   print_proxy("RpcFinally\n");
855   print_proxy("{\n");
856   print_proxy("}\n");
857   print_proxy("RpcEndFinally\n");
858
859   print_proxy("_Msg->BufferLength = _StubMsg.Buffer - (unsigned char *)_Msg->Buffer;\n");
860   indent--;
861
862   print_proxy("}\n");
863   print_proxy("\n");
864 }
865
866 static int write_proxy_methods(type_t *iface)
867 {
868   func_t *cur = iface->funcs;
869   int i = 0;
870
871   END_OF_LIST(cur);
872
873   if (iface->ref) i = write_proxy_methods(iface->ref);
874   while (cur) {
875     var_t *def = cur->def;
876     if (!is_callas(def->attrs)) {
877       if (i) fprintf(proxy, ",\n");
878       print_proxy( "%s_", iface->name);
879       write_name(proxy, def);
880       fprintf(proxy, "_Proxy");
881       i++;
882     }
883     cur = PREV_LINK(cur);
884   }
885   return i;
886 }
887
888 static int write_stub_methods(type_t *iface)
889 {
890   func_t *cur = iface->funcs;
891   int i = 0;
892
893   END_OF_LIST(cur);
894
895   if (iface->ref) i = write_stub_methods(iface->ref);
896   else return i; /* skip IUnknown */
897   while (cur) {
898     var_t *def = cur->def;
899     if (!is_local(def->attrs)) {
900       if (i) fprintf(proxy,",\n");
901       print_proxy( "%s_", iface->name);
902       write_name(proxy, def);
903       fprintf(proxy, "_Stub");
904       i++;
905     }
906     cur = PREV_LINK(cur);
907   }
908   return i;
909 }
910
911 static void write_proxy(type_t *iface)
912 {
913   int midx = -1, stubs;
914   func_t *cur = iface->funcs;
915
916   if (!cur) return;
917
918   END_OF_LIST(cur);
919
920   /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
921
922   fprintf(proxy, "/*****************************************************************************\n");
923   fprintf(proxy, " * %s interface\n", iface->name);
924   fprintf(proxy, " */\n");
925   while (cur) {
926     const var_t *def = cur->def;
927     if (!is_local(def->attrs)) {
928       const var_t *cas = is_callas(def->attrs);
929       const char *cname = cas ? cas->name : NULL;
930       int idx = cur->idx;
931       if (cname) {
932         const func_t *m = iface->funcs;
933         while (m && strcmp(get_name(m->def), cname))
934           m = NEXT_LINK(m);
935         idx = m->idx;
936       }
937       gen_proxy(iface, cur, idx);
938       gen_stub(iface, cur, cname);
939       if (midx == -1) midx = idx;
940       else if (midx != idx) parser_error("method index mismatch in write_proxy");
941       midx++;
942     }
943     cur = PREV_LINK(cur);
944   }
945
946   /* proxy vtable */
947   print_proxy( "const CINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n", midx, iface->name);
948   print_proxy( "{\n");
949   indent++;
950   print_proxy( "{\n", iface->name);
951   indent++;
952   print_proxy( "&IID_%s,\n", iface->name);
953   indent--;
954   print_proxy( "},\n");
955   print_proxy( "{\n");
956   indent++;
957   write_proxy_methods(iface);
958   fprintf(proxy, "\n");
959   indent--;
960   print_proxy( "}\n");
961   indent--;
962   print_proxy( "};\n");
963   fprintf(proxy, "\n\n");
964
965   /* stub vtable */
966   print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
967   print_proxy( "{\n");
968   indent++;
969   stubs = write_stub_methods(iface);
970   fprintf(proxy, "\n");
971   indent--;
972   fprintf(proxy, "};\n");
973   print_proxy( "\n");
974   print_proxy( "const CInterfaceStubVtbl _%sStubVtbl =\n", iface->name);
975   print_proxy( "{\n");
976   indent++;
977   print_proxy( "{\n");
978   indent++;
979   print_proxy( "&IID_%s,\n", iface->name);
980   print_proxy( "0,\n");
981   print_proxy( "%d,\n", stubs+3);
982   print_proxy( "&%s_table[-3],\n", iface->name);
983   indent--;
984   print_proxy( "},\n", iface->name);
985   print_proxy( "{\n");
986   indent++;
987   print_proxy( "CStdStubBuffer_METHODS\n");
988   indent--;
989   print_proxy( "}\n");
990   indent--;
991   print_proxy( "};\n");
992   print_proxy( "\n");
993 }
994
995 void write_proxies(ifref_t *ifaces)
996 {
997   ifref_t *lcur = ifaces;
998   ifref_t *cur;
999   char *file_id = proxy_token;
1000   int c;
1001
1002   if (!do_proxies) return;
1003   if (do_everything && !ifaces) return;
1004
1005   init_proxy(ifaces);
1006   if(!proxy) return;
1007
1008   END_OF_LIST(lcur);
1009   cur = lcur;
1010   while (cur) {
1011     if (is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
1012       write_proxy(cur->iface);
1013     cur = PREV_LINK(cur);
1014   }
1015
1016   write_stubdesc();
1017
1018   print_proxy( "#if !defined(__RPC_WIN32__)\n");
1019   print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
1020   print_proxy( "#endif\n");
1021   print_proxy( "\n");
1022   write_procformatstring(proxy, ifaces, 1);
1023   write_typeformatstring(proxy, ifaces, 1);
1024
1025   fprintf(proxy, "const CInterfaceProxyVtbl* _%s_ProxyVtblList[] =\n", file_id);
1026   fprintf(proxy, "{\n");
1027   cur = lcur;
1028   while (cur) {
1029     if(cur->iface->ref && cur->iface->funcs &&
1030        is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
1031       fprintf(proxy, "    (CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", cur->iface->name);
1032     cur = PREV_LINK(cur);
1033   }
1034   fprintf(proxy, "    0\n");
1035   fprintf(proxy, "};\n");
1036   fprintf(proxy, "\n");
1037
1038   fprintf(proxy, "const CInterfaceStubVtbl* _%s_StubVtblList[] =\n", file_id);
1039   fprintf(proxy, "{\n");
1040   cur = lcur;
1041   while (cur) {
1042     if(cur->iface->ref && cur->iface->funcs &&
1043        is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
1044       fprintf(proxy, "    (CInterfaceStubVtbl*)&_%sStubVtbl,\n", cur->iface->name);
1045     cur = PREV_LINK(cur);
1046   }
1047   fprintf(proxy, "    0\n");
1048   fprintf(proxy, "};\n");
1049   fprintf(proxy, "\n");
1050
1051   fprintf(proxy, "PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
1052   fprintf(proxy, "{\n");
1053   cur = lcur;
1054   while (cur) {
1055     if(cur->iface->ref && cur->iface->funcs &&
1056        is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
1057       fprintf(proxy, "    \"%s\",\n", cur->iface->name);
1058     cur = PREV_LINK(cur);
1059   }
1060   fprintf(proxy, "    0\n");
1061   fprintf(proxy, "};\n");
1062   fprintf(proxy, "\n");
1063
1064   fprintf(proxy, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_%s, pIID, n)\n", file_id, file_id);
1065   fprintf(proxy, "\n");
1066   fprintf(proxy, "int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
1067   fprintf(proxy, "{\n");
1068   cur = lcur;
1069   c = 0;
1070   while (cur) {
1071     if(cur->iface->ref)
1072     {
1073       fprintf(proxy, "    if (!_%s_CHECK_IID(%d))\n", file_id, c);
1074       fprintf(proxy, "    {\n");
1075       fprintf(proxy, "        *pIndex = %d;\n", c);
1076       fprintf(proxy, "        return 1;\n");
1077       fprintf(proxy, "    }\n");
1078       c++;
1079     }
1080     cur = PREV_LINK(cur);
1081   }
1082   fprintf(proxy, "    return 0;\n");
1083   fprintf(proxy, "}\n");
1084   fprintf(proxy, "\n");
1085
1086   fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id);
1087   fprintf(proxy, "{\n");
1088   fprintf(proxy, "    (PCInterfaceProxyVtblList*)&_%s_ProxyVtblList,\n", file_id);
1089   fprintf(proxy, "    (PCInterfaceStubVtblList*)&_%s_StubVtblList,\n", file_id);
1090   fprintf(proxy, "    (const PCInterfaceName*)&_%s_InterfaceNamesList,\n", file_id);
1091   fprintf(proxy, "    0,\n");
1092   fprintf(proxy, "    &_%s_IID_Lookup,\n", file_id);
1093   fprintf(proxy, "    %d,\n", c);
1094   fprintf(proxy, "    1,\n");
1095   fprintf(proxy, "    0,\n");
1096   fprintf(proxy, "    0,\n");
1097   fprintf(proxy, "    0,\n");
1098   fprintf(proxy, "    0\n");
1099   fprintf(proxy, "};\n");
1100
1101   fclose(proxy);
1102 }