winmm: Fix a failing mixer test on 98 and ME.
[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 <ctype.h>
32
33 #include "widl.h"
34 #include "utils.h"
35 #include "parser.h"
36 #include "header.h"
37 #include "typegen.h"
38 #include "expr.h"
39
40 #define END_OF_LIST(list)       \
41   do {                          \
42     if (list) {                 \
43       while (NEXT_LINK(list))   \
44         list = NEXT_LINK(list); \
45     }                           \
46   } while(0)
47
48 static FILE* proxy;
49 static int indent = 0;
50
51 /* FIXME: support generation of stubless proxies */
52
53 static void print_proxy( const char *format, ... )
54 {
55   va_list va;
56   va_start( va, format );
57   print( proxy, indent, format, va );
58   va_end( va );
59 }
60
61 static void write_stubdescproto(void)
62 {
63   print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
64   print_proxy( "\n");
65 }
66
67 static void write_stubdesc(int expr_eval_routines)
68 {
69   print_proxy( "static const MIDL_STUB_DESC Object_StubDesc =\n{\n");
70   indent++;
71   print_proxy( "0,\n");
72   print_proxy( "NdrOleAllocate,\n");
73   print_proxy( "NdrOleFree,\n");
74   print_proxy( "{0}, 0, 0, %s, 0,\n", expr_eval_routines ? "ExprEvalRoutines" : "0");
75   print_proxy( "__MIDL_TypeFormatString.Format,\n");
76   print_proxy( "1, /* -error bounds_check flag */\n");
77   print_proxy( "0x10001, /* Ndr library version */\n");
78   print_proxy( "0,\n");
79   print_proxy( "0x50100a4, /* MIDL Version 5.1.164 */\n");
80   print_proxy( "0,\n");
81   print_proxy("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
82   print_proxy( "0,  /* notify & notify_flag routine table */\n");
83   print_proxy( "1,  /* Flags */\n");
84   print_proxy( "0,  /* Reserved3 */\n");
85   print_proxy( "0,  /* Reserved4 */\n");
86   print_proxy( "0   /* Reserved5 */\n");
87   indent--;
88   print_proxy( "};\n");
89   print_proxy( "\n");
90 }
91
92 static void init_proxy(const statement_list_t *stmts)
93 {
94   if (proxy) return;
95   if(!(proxy = fopen(proxy_name, "w")))
96     error("Could not open %s for output\n", proxy_name);
97   print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
98   print_proxy( "\n");
99   print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
100   print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ 440\n");
101   print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
102   print_proxy( "\n");
103   print_proxy( "#define __midl_proxy\n");
104   print_proxy( "#include \"objbase.h\"\n");
105   print_proxy( "#include \"rpcproxy.h\"\n");
106   print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
107   print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
108   print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
109   print_proxy( "\n");
110   print_proxy( "#include \"%s\"\n", header_name);
111   print_proxy( "\n");
112   write_exceptions( proxy );
113   print_proxy( "\n");
114   print_proxy( "struct __proxy_frame\n");
115   print_proxy( "{\n");
116   print_proxy( "    __DECL_EXCEPTION_FRAME;\n");
117   print_proxy( "    MIDL_STUB_MESSAGE _StubMsg;\n");
118   print_proxy( "    void             *This;\n");
119   print_proxy( "};\n");
120   print_proxy( "\n");
121   print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
122   print_proxy( "{\n");
123   print_proxy( "    return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
124   print_proxy( "}\n");
125   print_proxy( "\n");
126   write_formatstringsdecl(proxy, indent, stmts, need_proxy);
127   write_stubdescproto();
128 }
129
130 static void clear_output_vars( const var_list_t *args )
131 {
132   const var_t *arg;
133
134   if (!args) return;
135   LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
136   {
137     if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
138       print_proxy( "if(%s)\n", arg->name );
139       indent++;
140       print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name );
141       indent--;
142     }
143   }
144 }
145
146 int is_var_ptr(const var_t *v)
147 {
148   return is_ptr(v->type);
149 }
150
151 int cant_be_null(const var_t *v)
152 {
153   /* Search backwards for the most recent pointer attribute.  */
154   const attr_list_t *attrs = v->attrs;
155   const type_t *type = v->type;
156
157   /* context handles have their own checking so they can be null for the
158    * purposes of null ref pointer checking */
159   if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
160       return 0;
161
162   if (! attrs && type)
163   {
164     attrs = type->attrs;
165     type = type->ref;
166   }
167
168   while (attrs)
169   {
170     int t = get_attrv(attrs, ATTR_POINTERTYPE);
171
172     if (t == RPC_FC_FP || t == RPC_FC_OP || t == RPC_FC_UP)
173       return 0;
174
175     if (t == RPC_FC_RP)
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 1;                             /* Default is RPC_FC_RP.  */
188 }
189
190 static void proxy_check_pointers( const var_list_t *args )
191 {
192   const var_t *arg;
193
194   if (!args) return;
195   LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
196   {
197     if (is_var_ptr(arg) && cant_be_null(arg)) {
198         print_proxy( "if(!%s)\n", arg->name );
199         indent++;
200         print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
201         indent--;
202     }
203   }
204 }
205
206 static void free_variable( const var_t *arg, const char *local_var_prefix )
207 {
208   unsigned int type_offset = arg->type->typestring_offset;
209   expr_t *iid;
210   type_t *type = arg->type;
211   expr_t *size = get_size_is_expr(type, arg->name);
212
213   if (size)
214   {
215     print_proxy( "__frame->_StubMsg.MaxCount = " );
216     write_expr(proxy, size, 0, 1, NULL, NULL, local_var_prefix);
217     fprintf(proxy, ";\n\n");
218     print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
219     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
220     fprintf(proxy, "(void*)%s );\n", arg->name );
221     return;
222   }
223
224   switch( type->type )
225   {
226   case RPC_FC_BYTE:
227   case RPC_FC_CHAR:
228   case RPC_FC_WCHAR:
229   case RPC_FC_SHORT:
230   case RPC_FC_USHORT:
231   case RPC_FC_ENUM16:
232   case RPC_FC_LONG:
233   case RPC_FC_ULONG:
234   case RPC_FC_ENUM32:
235   case RPC_FC_STRUCT:
236     break;
237
238   case RPC_FC_FP:
239   case RPC_FC_IP:
240     iid = get_attrp( arg->attrs, ATTR_IIDIS );
241     if( iid )
242     {
243       print_proxy( "__frame->_StubMsg.MaxCount = (unsigned long) " );
244       write_expr(proxy, iid, 1, 1, NULL, NULL, local_var_prefix);
245       print_proxy( ";\n\n" );
246     }
247     print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
248     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
249     fprintf(proxy, "(void*)%s );\n", arg->name );
250     break;
251
252   default:
253     print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
254   }
255 }
256
257 static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
258 {
259   const var_t *arg;
260
261   if (!args) return;
262   LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
263     if (is_attr(arg->attrs, ATTR_OUT))
264     {
265       free_variable( arg, local_var_prefix );
266       fprintf(proxy, "\n");
267     }
268 }
269
270 static void gen_proxy(type_t *iface, const func_t *cur, int idx,
271                       unsigned int proc_offset)
272 {
273   var_t *def = cur->def;
274   int has_ret = !is_void(get_func_return_type(cur));
275   int has_full_pointer = is_full_pointer_function(cur);
276   const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
277   if (!callconv) callconv = "";
278
279   indent = 0;
280   print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
281                iface->name, get_name(def) );
282   print_proxy( "{\n");
283   indent++;
284   if (has_full_pointer) write_full_pointer_free(proxy, indent, cur);
285   print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
286   indent--;
287   print_proxy( "}\n");
288   print_proxy( "\n");
289
290   write_type_decl_left(proxy, get_func_return_type(cur));
291   print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(def));
292   write_args(proxy, cur->args, iface->name, 1, TRUE);
293   print_proxy( ")\n");
294   print_proxy( "{\n");
295   indent ++;
296   print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
297   /* local variables */
298   if (has_ret) {
299     print_proxy( "" );
300     write_type_decl_left(proxy, get_func_return_type(cur));
301     print_proxy( " _RetVal;\n");
302   }
303   print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
304   if (has_ret) {
305     if (decl_indirect(get_func_return_type(cur)))
306       print_proxy("void *_p_%s = &%s;\n",
307                  "_RetVal", "_RetVal");
308   }
309   print_proxy( "\n");
310
311   print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(def) );
312   print_proxy( "__frame->This = This;\n" );
313
314   if (has_full_pointer)
315     write_full_pointer_init(proxy, indent, cur, FALSE);
316
317   /* FIXME: trace */
318   clear_output_vars( cur->args );
319
320   print_proxy( "RpcTryExcept\n" );
321   print_proxy( "{\n" );
322   indent++;
323   print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx);
324   proxy_check_pointers( cur->args );
325
326   print_proxy( "RpcTryFinally\n" );
327   print_proxy( "{\n" );
328   indent++;
329
330   write_remoting_arguments(proxy, indent, cur, "", PASS_IN, PHASE_BUFFERSIZE);
331
332   print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
333
334   write_remoting_arguments(proxy, indent, cur, "", PASS_IN, PHASE_MARSHAL);
335
336   print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
337   fprintf(proxy, "\n");
338   print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
339   print_proxy( "__frame->_StubMsg.BufferEnd   = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
340
341   print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
342   indent++;
343   print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
344   indent--;
345   fprintf(proxy, "\n");
346
347   write_remoting_arguments(proxy, indent, cur, "", PASS_OUT, PHASE_UNMARSHAL);
348
349   if (has_ret)
350   {
351       if (decl_indirect(get_func_return_type(cur)))
352           print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
353       else if (is_ptr(get_func_return_type(cur)) || is_array(get_func_return_type(cur)))
354           print_proxy("%s = 0;\n", "_RetVal");
355       write_remoting_arguments(proxy, indent, cur, "", PASS_RETURN, PHASE_UNMARSHAL);
356   }
357
358   indent--;
359   print_proxy( "}\n");
360   print_proxy( "RpcFinally\n" );
361   print_proxy( "{\n" );
362   indent++;
363   print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(def) );
364   indent--;
365   print_proxy( "}\n");
366   print_proxy( "RpcEndFinally\n" );
367   indent--;
368   print_proxy( "}\n" );
369   print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
370   print_proxy( "{\n" );
371   if (has_ret) {
372     indent++;
373     proxy_free_variables( cur->args, "" );
374     print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
375     indent--;
376   }
377   print_proxy( "}\n" );
378   print_proxy( "RpcEndExcept\n" );
379
380   if (has_ret) {
381     print_proxy( "return _RetVal;\n" );
382   }
383   indent--;
384   print_proxy( "}\n");
385   print_proxy( "\n");
386 }
387
388 static void gen_stub(type_t *iface, const func_t *cur, const char *cas,
389                      unsigned int proc_offset)
390 {
391   var_t *def = cur->def;
392   const var_t *arg;
393   int has_ret = !is_void(get_func_return_type(cur));
394   int has_full_pointer = is_full_pointer_function(cur);
395
396   indent = 0;
397   print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(def));
398   indent++;
399   print_proxy( "__DECL_EXCEPTION_FRAME\n" );
400   print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
401   print_proxy( "%s * _This;\n", iface->name );
402   declare_stub_args( proxy, indent, cur );
403   indent--;
404   print_proxy( "};\n\n" );
405
406   print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(def) );
407   print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(def) );
408   indent++;
409   write_remoting_arguments(proxy, indent, cur, "__frame->", PASS_OUT, PHASE_FREE);
410   if (has_full_pointer)
411     write_full_pointer_free(proxy, indent, cur);
412   indent--;
413   print_proxy( "}\n\n" );
414
415   print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(def));
416   indent++;
417   print_proxy( "IRpcStubBuffer* This,\n");
418   print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
419   print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
420   print_proxy( "DWORD* _pdwStubPhase)\n");
421   indent--;
422   print_proxy( "{\n");
423   indent++;
424   print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
425                iface->name, get_name(def) );
426
427   print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
428
429   /* FIXME: trace */
430
431   print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
432   fprintf(proxy, "\n");
433   print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(def) );
434
435   write_parameters_init(proxy, indent, cur, "__frame->");
436
437   print_proxy("RpcTryFinally\n");
438   print_proxy("{\n");
439   indent++;
440   if (has_full_pointer)
441     write_full_pointer_init(proxy, indent, cur, TRUE);
442   print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
443   indent++;
444   print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
445   indent--;
446   fprintf(proxy, "\n");
447
448   write_remoting_arguments(proxy, indent, cur, "__frame->", PASS_IN, PHASE_UNMARSHAL);
449   fprintf(proxy, "\n");
450
451   assign_stub_out_args( proxy, indent, cur, "__frame->" );
452
453   print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
454   fprintf(proxy, "\n");
455   print_proxy("");
456   if (has_ret) fprintf(proxy, "__frame->_RetVal = ");
457   if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
458   else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(def));
459   fprintf(proxy, "(__frame->_This");
460
461   if (cur->args)
462   {
463       LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
464           fprintf(proxy, ", %s__frame->%s", arg->type->declarray ? "*" : "", arg->name);
465   }
466   fprintf(proxy, ");\n");
467   fprintf(proxy, "\n");
468   print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
469   fprintf(proxy, "\n");
470
471   write_remoting_arguments(proxy, indent, cur, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
472
473   if (!is_void(get_func_return_type(cur)))
474     write_remoting_arguments(proxy, indent, cur, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
475
476   print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
477
478   write_remoting_arguments(proxy, indent, cur, "__frame->", PASS_OUT, PHASE_MARSHAL);
479   fprintf(proxy, "\n");
480
481   /* marshall the return value */
482   if (!is_void(get_func_return_type(cur)))
483     write_remoting_arguments(proxy, indent, cur, "__frame->", PASS_RETURN, PHASE_MARSHAL);
484
485   indent--;
486   print_proxy("}\n");
487   print_proxy("RpcFinally\n");
488   print_proxy("{\n");
489   indent++;
490   print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(def) );
491   indent--;
492   print_proxy("}\n");
493   print_proxy("RpcEndFinally\n");
494
495   print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
496   indent--;
497
498   print_proxy("}\n");
499   print_proxy("\n");
500 }
501
502 static int write_proxy_methods(type_t *iface, int skip)
503 {
504   const func_t *cur;
505   int i = 0;
506
507   if (iface->ref) i = write_proxy_methods(iface->ref, iface->ref->ref != NULL);
508   if (iface->funcs) LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry ) {
509     var_t *def = cur->def;
510     if (!is_callas(def->attrs)) {
511       if (i) fprintf(proxy, ",\n");
512       if (skip) print_proxy( "0  /* %s_%s_Proxy */", iface->name, get_name(def));
513       else print_proxy( "%s_%s_Proxy", iface->name, get_name(def));
514       i++;
515     }
516   }
517   return i;
518 }
519
520 static int write_stub_methods(type_t *iface, int skip)
521 {
522   const func_t *cur;
523   int i = 0;
524
525   if (iface->ref) i = write_stub_methods(iface->ref, TRUE);
526   else return i; /* skip IUnknown */
527
528   if (iface->funcs) LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry ) {
529     var_t *def = cur->def;
530     if (!is_local(def->attrs)) {
531       if (i) fprintf(proxy,",\n");
532       if (skip) print_proxy("STUB_FORWARDING_FUNCTION");
533       else print_proxy( "%s_%s_Stub", iface->name, get_name(def));
534       i++;
535     }
536   }
537   return i;
538 }
539
540 static void write_proxy(type_t *iface, unsigned int *proc_offset)
541 {
542   int midx = -1, stubs;
543   const func_t *cur;
544
545   if (!iface->funcs) return;
546
547   /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
548
549   fprintf(proxy, "/*****************************************************************************\n");
550   fprintf(proxy, " * %s interface\n", iface->name);
551   fprintf(proxy, " */\n");
552   LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
553   {
554     const var_t *def = cur->def;
555     if (!is_local(def->attrs)) {
556       const var_t *cas = is_callas(def->attrs);
557       const char *cname = cas ? cas->name : NULL;
558       int idx = cur->idx;
559       if (cname) {
560           const func_t *m;
561           LIST_FOR_EACH_ENTRY( m, iface->funcs, const func_t, entry )
562               if (!strcmp(m->def->name, cname))
563               {
564                   idx = m->idx;
565                   break;
566               }
567       }
568       gen_proxy(iface, cur, idx, *proc_offset);
569       gen_stub(iface, cur, cname, *proc_offset);
570       *proc_offset += get_size_procformatstring_func( cur );
571       if (midx == -1) midx = idx;
572       else if (midx != idx) error("method index mismatch in write_proxy\n");
573       midx++;
574     }
575   }
576
577   /* proxy vtable */
578   print_proxy( "static const CINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n", midx, iface->name);
579   print_proxy( "{\n");
580   indent++;
581   print_proxy( "{\n", iface->name);
582   indent++;
583   print_proxy( "&IID_%s,\n", iface->name);
584   indent--;
585   print_proxy( "},\n");
586   print_proxy( "{\n");
587   indent++;
588   write_proxy_methods(iface, FALSE);
589   fprintf(proxy, "\n");
590   indent--;
591   print_proxy( "}\n");
592   indent--;
593   print_proxy( "};\n");
594   fprintf(proxy, "\n\n");
595
596   /* stub vtable */
597   print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
598   print_proxy( "{\n");
599   indent++;
600   stubs = write_stub_methods(iface, FALSE);
601   fprintf(proxy, "\n");
602   indent--;
603   fprintf(proxy, "};\n");
604   print_proxy( "\n");
605   print_proxy( "static const CInterfaceStubVtbl _%sStubVtbl =\n", iface->name);
606   print_proxy( "{\n");
607   indent++;
608   print_proxy( "{\n");
609   indent++;
610   print_proxy( "&IID_%s,\n", iface->name);
611   print_proxy( "0,\n");
612   print_proxy( "%d,\n", stubs+3);
613   print_proxy( "&%s_table[-3],\n", iface->name);
614   indent--;
615   print_proxy( "},\n", iface->name);
616   print_proxy( "{\n");
617   indent++;
618   print_proxy( "CStdStubBuffer_METHODS\n");
619   indent--;
620   print_proxy( "}\n");
621   indent--;
622   print_proxy( "};\n");
623   print_proxy( "\n");
624 }
625
626 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
627 {
628   const statement_t *stmt;
629
630   if (stmts)
631     LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
632     {
633       if (stmt->type == STMT_LIBRARY)
634       {
635           if (does_any_iface(stmt->u.lib->stmts, pred))
636               return TRUE;
637       }
638       else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
639       {
640         if (pred(stmt->u.type))
641           return TRUE;
642       }
643     }
644
645   return FALSE;
646 }
647
648 int need_proxy(const type_t *iface)
649 {
650   return is_object(iface->attrs) && !is_local(iface->attrs);
651 }
652
653 int need_stub(const type_t *iface)
654 {
655   return !is_object(iface->attrs) && !is_local(iface->attrs);
656 }
657
658 int need_proxy_file(const statement_list_t *stmts)
659 {
660   return does_any_iface(stmts, need_proxy);
661 }
662
663 int need_stub_files(const statement_list_t *stmts)
664 {
665   return does_any_iface(stmts, need_stub);
666 }
667
668 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
669 {
670   const statement_t *stmt;
671   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
672   {
673     if (stmt->type == STMT_LIBRARY)
674       write_proxy_stmts(stmt->u.lib->stmts, proc_offset);
675     else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
676     {
677       if (need_proxy(stmt->u.type))
678         write_proxy(stmt->u.type, proc_offset);
679     }
680   }
681 }
682
683 static void write_proxy_iface_name_format(const statement_list_t *stmts, const char *format)
684 {
685   const statement_t *stmt;
686   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
687   {
688     if (stmt->type == STMT_LIBRARY)
689       write_proxy_iface_name_format(stmt->u.lib->stmts, format);
690     else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
691     {
692       type_t *iface = stmt->u.type;
693       if (iface->ref && iface->funcs && need_proxy(iface))
694         fprintf(proxy, format, iface->name);
695     }
696   }
697 }
698
699 static void write_iid_lookup(const statement_list_t *stmts, const char *file_id, int *c)
700 {
701   const statement_t *stmt;
702   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
703   {
704     if (stmt->type == STMT_LIBRARY)
705       write_iid_lookup(stmt->u.lib->stmts, file_id, c);
706     else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
707     {
708       type_t *iface = stmt->u.type;
709       if(iface->ref && iface->funcs && need_proxy(iface))
710       {
711         fprintf(proxy, "    if (!_%s_CHECK_IID(%d))\n", file_id, *c);
712         fprintf(proxy, "    {\n");
713         fprintf(proxy, "        *pIndex = %d;\n", *c);
714         fprintf(proxy, "        return 1;\n");
715         fprintf(proxy, "    }\n");
716         (*c)++;
717       }
718     }
719   }
720 }
721
722 void write_proxies(const statement_list_t *stmts)
723 {
724   int expr_eval_routines;
725   char *file_id = proxy_token;
726   int c;
727   unsigned int proc_offset = 0;
728
729   if (!do_proxies) return;
730   if (do_everything && !need_proxy_file(stmts)) return;
731
732   init_proxy(stmts);
733   if(!proxy) return;
734
735   write_proxy_stmts(stmts, &proc_offset);
736
737   expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
738   if (expr_eval_routines)
739       write_expr_eval_routine_list(proxy, proxy_token);
740   write_user_quad_list(proxy);
741   write_stubdesc(expr_eval_routines);
742
743   print_proxy( "#if !defined(__RPC_WIN32__)\n");
744   print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
745   print_proxy( "#endif\n");
746   print_proxy( "\n");
747   write_procformatstring(proxy, stmts, need_proxy);
748   write_typeformatstring(proxy, stmts, need_proxy);
749
750   fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
751   fprintf(proxy, "{\n");
752   write_proxy_iface_name_format(stmts, "    (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n");
753   fprintf(proxy, "    0\n");
754   fprintf(proxy, "};\n");
755   fprintf(proxy, "\n");
756
757   fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
758   fprintf(proxy, "{\n");
759   write_proxy_iface_name_format(stmts, "    (const CInterfaceStubVtbl*)&_%sStubVtbl,\n");
760   fprintf(proxy, "    0\n");
761   fprintf(proxy, "};\n");
762   fprintf(proxy, "\n");
763
764   fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
765   fprintf(proxy, "{\n");
766   write_proxy_iface_name_format(stmts, "    \"%s\",\n");
767   fprintf(proxy, "    0\n");
768   fprintf(proxy, "};\n");
769   fprintf(proxy, "\n");
770
771   fprintf(proxy, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_%s, pIID, n)\n", file_id, file_id);
772   fprintf(proxy, "\n");
773   fprintf(proxy, "int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
774   fprintf(proxy, "{\n");
775   c = 0;
776   write_iid_lookup(stmts, file_id, &c);
777   fprintf(proxy, "    return 0;\n");
778   fprintf(proxy, "}\n");
779   fprintf(proxy, "\n");
780
781   fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id);
782   fprintf(proxy, "{\n");
783   fprintf(proxy, "    (const PCInterfaceProxyVtblList*)&_%s_ProxyVtblList,\n", file_id);
784   fprintf(proxy, "    (const PCInterfaceStubVtblList*)&_%s_StubVtblList,\n", file_id);
785   fprintf(proxy, "    _%s_InterfaceNamesList,\n", file_id);
786   fprintf(proxy, "    0,\n");
787   fprintf(proxy, "    &_%s_IID_Lookup,\n", file_id);
788   fprintf(proxy, "    %d,\n", c);
789   fprintf(proxy, "    1,\n");
790   fprintf(proxy, "    0,\n");
791   fprintf(proxy, "    0,\n");
792   fprintf(proxy, "    0,\n");
793   fprintf(proxy, "    0\n");
794   fprintf(proxy, "};\n");
795
796   fclose(proxy);
797 }