widl: Fix the embedded pointer offsets in generated code when the containing structur...
[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_formatstringsdecl(proxy, indent, stmts, need_proxy);
113   write_stubdescproto();
114 }
115
116 static void clear_output_vars( const var_list_t *args )
117 {
118   const var_t *arg;
119
120   if (!args) return;
121   LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
122   {
123     if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
124       print_proxy( "if(%s)\n", arg->name );
125       indent++;
126       print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name );
127       indent--;
128     }
129   }
130 }
131
132 int is_var_ptr(const var_t *v)
133 {
134   return is_ptr(v->type);
135 }
136
137 int cant_be_null(const var_t *v)
138 {
139   /* Search backwards for the most recent pointer attribute.  */
140   const attr_list_t *attrs = v->attrs;
141   const type_t *type = v->type;
142
143   if (! attrs && type)
144   {
145     attrs = type->attrs;
146     type = type->ref;
147   }
148
149   while (attrs)
150   {
151     int t = get_attrv(attrs, ATTR_POINTERTYPE);
152
153     if (t == RPC_FC_FP || t == RPC_FC_OP || t == RPC_FC_UP)
154       return 0;
155
156     if (t == RPC_FC_RP)
157       return 1;
158
159     if (type)
160     {
161       attrs = type->attrs;
162       type = type->ref;
163     }
164     else
165       attrs = NULL;
166   }
167
168   return 1;                             /* Default is RPC_FC_RP.  */
169 }
170
171 static void proxy_check_pointers( const var_list_t *args )
172 {
173   const var_t *arg;
174
175   if (!args) return;
176   LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
177   {
178     if (is_var_ptr(arg) && cant_be_null(arg)) {
179         print_proxy( "if(!%s)\n", arg->name );
180         indent++;
181         print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
182         indent--;
183     }
184   }
185 }
186
187 static void free_variable( const var_t *arg )
188 {
189   unsigned int type_offset = arg->type->typestring_offset;
190   expr_t *iid;
191   type_t *type = arg->type;
192   expr_t *size = get_size_is_expr(type, arg->name);
193
194   if (size)
195   {
196     print_proxy( "_StubMsg.MaxCount = " );
197     write_expr(proxy, size, 0, 1, NULL, NULL);
198     fprintf(proxy, ";\n\n");
199     print_proxy( "NdrClearOutParameters( &_StubMsg, ");
200     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
201     fprintf(proxy, "(void*)%s );\n", arg->name );
202     return;
203   }
204
205   switch( type->type )
206   {
207   case RPC_FC_BYTE:
208   case RPC_FC_CHAR:
209   case RPC_FC_WCHAR:
210   case RPC_FC_SHORT:
211   case RPC_FC_USHORT:
212   case RPC_FC_ENUM16:
213   case RPC_FC_LONG:
214   case RPC_FC_ULONG:
215   case RPC_FC_ENUM32:
216   case RPC_FC_STRUCT:
217     break;
218
219   case RPC_FC_FP:
220   case RPC_FC_IP:
221     iid = get_attrp( arg->attrs, ATTR_IIDIS );
222     if( iid )
223     {
224       print_proxy( "_StubMsg.MaxCount = (unsigned long) " );
225       write_expr(proxy, iid, 1, 1, NULL, NULL);
226       print_proxy( ";\n\n" );
227     }
228     print_proxy( "NdrClearOutParameters( &_StubMsg, ");
229     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
230     fprintf(proxy, "(void*)%s );\n", arg->name );
231     break;
232
233   default:
234     print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
235   }
236 }
237
238 static void proxy_free_variables( var_list_t *args )
239 {
240   const var_t *arg;
241
242   if (!args) return;
243   LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
244     if (is_attr(arg->attrs, ATTR_OUT))
245     {
246       free_variable( arg );
247       fprintf(proxy, "\n");
248     }
249 }
250
251 static void gen_proxy(type_t *iface, const func_t *cur, int idx,
252                       unsigned int proc_offset)
253 {
254   var_t *def = cur->def;
255   int has_ret = !is_void(get_func_return_type(cur));
256   int has_full_pointer = is_full_pointer_function(cur);
257   const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
258   if (!callconv) callconv = "";
259
260   indent = 0;
261   write_type_decl_left(proxy, get_func_return_type(cur));
262   print_proxy( " %s %s_", callconv, iface->name);
263   write_name(proxy, def);
264   print_proxy( "_Proxy(\n");
265   write_args(proxy, cur->args, iface->name, 1, TRUE);
266   print_proxy( ")\n");
267   print_proxy( "{\n");
268   indent ++;
269   /* local variables */
270   if (has_ret) {
271     print_proxy( "" );
272     write_type_decl_left(proxy, get_func_return_type(cur));
273     print_proxy( " _RetVal;\n");
274   }
275   print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
276   print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n" );
277   if (has_ret) {
278     if (decl_indirect(get_func_return_type(cur)))
279       print_proxy("void *_p_%s = &%s;\n",
280                  "_RetVal", "_RetVal");
281   }
282   print_proxy( "\n");
283
284   if (has_full_pointer)
285     write_full_pointer_init(proxy, indent, cur, FALSE);
286
287   /* FIXME: trace */
288   clear_output_vars( cur->args );
289
290   print_proxy( "RpcTryExcept\n" );
291   print_proxy( "{\n" );
292   indent++;
293   print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &_StubMsg, &Object_StubDesc, %d);\n", idx);
294   proxy_check_pointers( cur->args );
295
296   print_proxy( "RpcTryFinally\n" );
297   print_proxy( "{\n" );
298   indent++;
299
300   write_remoting_arguments(proxy, indent, cur, PASS_IN, PHASE_BUFFERSIZE);
301
302   print_proxy( "NdrProxyGetBuffer(This, &_StubMsg);\n" );
303
304   write_remoting_arguments(proxy, indent, cur, PASS_IN, PHASE_MARSHAL);
305
306   print_proxy( "NdrProxySendReceive(This, &_StubMsg);\n" );
307   fprintf(proxy, "\n");
308   print_proxy( "_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
309   print_proxy( "_StubMsg.BufferEnd   = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
310
311   print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
312   indent++;
313   print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
314   indent--;
315   fprintf(proxy, "\n");
316
317   write_remoting_arguments(proxy, indent, cur, PASS_OUT, PHASE_UNMARSHAL);
318
319   if (has_ret)
320   {
321       if (decl_indirect(get_func_return_type(cur)))
322           print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
323       else if (is_ptr(get_func_return_type(cur)) || is_array(get_func_return_type(cur)))
324           print_proxy("%s = 0;\n", "_RetVal");
325       write_remoting_arguments(proxy, indent, cur, PASS_RETURN, PHASE_UNMARSHAL);
326   }
327
328   indent--;
329   print_proxy( "}\n");
330   print_proxy( "RpcFinally\n" );
331   print_proxy( "{\n" );
332   indent++;
333   if (has_full_pointer)
334     write_full_pointer_free(proxy, indent, cur);
335   print_proxy( "NdrProxyFreeBuffer(This, &_StubMsg);\n" );
336   indent--;
337   print_proxy( "}\n");
338   print_proxy( "RpcEndFinally\n" );
339   indent--;
340   print_proxy( "}\n" );
341   print_proxy( "RpcExcept(_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
342   print_proxy( "{\n" );
343   if (has_ret) {
344     indent++;
345     proxy_free_variables( cur->args );
346     print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
347     indent--;
348   }
349   print_proxy( "}\n" );
350   print_proxy( "RpcEndExcept\n" );
351
352   if (has_ret) {
353     print_proxy( "return _RetVal;\n" );
354   }
355   indent--;
356   print_proxy( "}\n");
357   print_proxy( "\n");
358 }
359
360 static void gen_stub(type_t *iface, const func_t *cur, const char *cas,
361                      unsigned int proc_offset)
362 {
363   var_t *def = cur->def;
364   const var_t *arg;
365   int has_ret = !is_void(get_func_return_type(cur));
366   int has_full_pointer = is_full_pointer_function(cur);
367
368   indent = 0;
369   print_proxy( "void __RPC_STUB %s_", iface->name);
370   write_name(proxy, def);
371   print_proxy( "_Stub(\n");
372   indent++;
373   print_proxy( "IRpcStubBuffer* This,\n");
374   print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
375   print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
376   print_proxy( "DWORD* _pdwStubPhase)\n");
377   indent--;
378   print_proxy( "{\n");
379   indent++;
380   print_proxy("%s * _This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n", iface->name, iface->name);
381   print_proxy("MIDL_STUB_MESSAGE _StubMsg;\n");
382   declare_stub_args( proxy, indent, cur );
383   fprintf(proxy, "\n");
384
385   /* FIXME: trace */
386
387   print_proxy("NdrStubInitialize(_pRpcMessage, &_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
388   fprintf(proxy, "\n");
389
390   write_parameters_init(proxy, indent, cur);
391
392   print_proxy("RpcTryFinally\n");
393   print_proxy("{\n");
394   indent++;
395   if (has_full_pointer)
396     write_full_pointer_init(proxy, indent, cur, TRUE);
397   print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
398   indent++;
399   print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
400   indent--;
401   fprintf(proxy, "\n");
402
403   write_remoting_arguments(proxy, indent, cur, PASS_IN, PHASE_UNMARSHAL);
404   fprintf(proxy, "\n");
405
406   assign_stub_out_args( proxy, indent, cur );
407
408   print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
409   fprintf(proxy, "\n");
410   print_proxy("");
411   if (has_ret) fprintf(proxy, "_RetVal = ");
412   if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
413   else
414   {
415       fprintf(proxy, "_This->lpVtbl->");
416       write_name(proxy, def);
417   }
418   fprintf(proxy, "(_This");
419
420   if (cur->args)
421   {
422       LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
423       {
424           fprintf(proxy, ", ");
425           if (arg->type->declarray)
426               fprintf(proxy, "*");
427           write_name(proxy, arg);
428       }
429   }
430   fprintf(proxy, ");\n");
431   fprintf(proxy, "\n");
432   print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
433   fprintf(proxy, "\n");
434
435   write_remoting_arguments(proxy, indent, cur, PASS_OUT, PHASE_BUFFERSIZE);
436
437   if (!is_void(get_func_return_type(cur)))
438     write_remoting_arguments(proxy, indent, cur, PASS_RETURN, PHASE_BUFFERSIZE);
439
440   print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &_StubMsg);\n");
441
442   write_remoting_arguments(proxy, indent, cur, PASS_OUT, PHASE_MARSHAL);
443   fprintf(proxy, "\n");
444
445   /* marshall the return value */
446   if (!is_void(get_func_return_type(cur)))
447     write_remoting_arguments(proxy, indent, cur, PASS_RETURN, PHASE_MARSHAL);
448
449   indent--;
450   print_proxy("}\n");
451   print_proxy("RpcFinally\n");
452   print_proxy("{\n");
453
454   write_remoting_arguments(proxy, indent+1, cur, PASS_OUT, PHASE_FREE);
455
456   if (has_full_pointer)
457     write_full_pointer_free(proxy, indent, cur);
458
459   print_proxy("}\n");
460   print_proxy("RpcEndFinally\n");
461
462   print_proxy("_pRpcMessage->BufferLength = _StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
463   indent--;
464
465   print_proxy("}\n");
466   print_proxy("\n");
467 }
468
469 static int write_proxy_methods(type_t *iface)
470 {
471   const func_t *cur;
472   int i = 0;
473
474   if (iface->ref) i = write_proxy_methods(iface->ref);
475   if (iface->funcs) LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry ) {
476     var_t *def = cur->def;
477     if (!is_callas(def->attrs)) {
478       if (i) fprintf(proxy, ",\n");
479       print_proxy( "%s_", iface->name);
480       write_name(proxy, def);
481       fprintf(proxy, "_Proxy");
482       i++;
483     }
484   }
485   return i;
486 }
487
488 static int write_stub_methods(type_t *iface)
489 {
490   const func_t *cur;
491   int i = 0;
492
493   if (iface->ref) i = write_stub_methods(iface->ref);
494   else return i; /* skip IUnknown */
495
496   if (iface->funcs) LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry ) {
497     var_t *def = cur->def;
498     if (!is_local(def->attrs)) {
499       if (i) fprintf(proxy,",\n");
500       print_proxy( "%s_", iface->name);
501       write_name(proxy, def);
502       fprintf(proxy, "_Stub");
503       i++;
504     }
505   }
506   return i;
507 }
508
509 static void write_proxy(type_t *iface, unsigned int *proc_offset)
510 {
511   int midx = -1, stubs;
512   const func_t *cur;
513
514   if (!iface->funcs) return;
515
516   /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
517
518   fprintf(proxy, "/*****************************************************************************\n");
519   fprintf(proxy, " * %s interface\n", iface->name);
520   fprintf(proxy, " */\n");
521   LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
522   {
523     const var_t *def = cur->def;
524     if (!is_local(def->attrs)) {
525       const var_t *cas = is_callas(def->attrs);
526       const char *cname = cas ? cas->name : NULL;
527       int idx = cur->idx;
528       if (cname) {
529           const func_t *m;
530           LIST_FOR_EACH_ENTRY( m, iface->funcs, const func_t, entry )
531               if (!strcmp(m->def->name, cname))
532               {
533                   idx = m->idx;
534                   break;
535               }
536       }
537       gen_proxy(iface, cur, idx, *proc_offset);
538       gen_stub(iface, cur, cname, *proc_offset);
539       *proc_offset += get_size_procformatstring_func( cur );
540       if (midx == -1) midx = idx;
541       else if (midx != idx) error("method index mismatch in write_proxy\n");
542       midx++;
543     }
544   }
545
546   /* proxy vtable */
547   print_proxy( "static const CINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n", midx, iface->name);
548   print_proxy( "{\n");
549   indent++;
550   print_proxy( "{\n", iface->name);
551   indent++;
552   print_proxy( "&IID_%s,\n", iface->name);
553   indent--;
554   print_proxy( "},\n");
555   print_proxy( "{\n");
556   indent++;
557   write_proxy_methods(iface);
558   fprintf(proxy, "\n");
559   indent--;
560   print_proxy( "}\n");
561   indent--;
562   print_proxy( "};\n");
563   fprintf(proxy, "\n\n");
564
565   /* stub vtable */
566   print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
567   print_proxy( "{\n");
568   indent++;
569   stubs = write_stub_methods(iface);
570   fprintf(proxy, "\n");
571   indent--;
572   fprintf(proxy, "};\n");
573   print_proxy( "\n");
574   print_proxy( "static const CInterfaceStubVtbl _%sStubVtbl =\n", iface->name);
575   print_proxy( "{\n");
576   indent++;
577   print_proxy( "{\n");
578   indent++;
579   print_proxy( "&IID_%s,\n", iface->name);
580   print_proxy( "0,\n");
581   print_proxy( "%d,\n", stubs+3);
582   print_proxy( "&%s_table[-3],\n", iface->name);
583   indent--;
584   print_proxy( "},\n", iface->name);
585   print_proxy( "{\n");
586   indent++;
587   print_proxy( "CStdStubBuffer_METHODS\n");
588   indent--;
589   print_proxy( "}\n");
590   indent--;
591   print_proxy( "};\n");
592   print_proxy( "\n");
593 }
594
595 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
596 {
597   const statement_t *stmt;
598
599   if (stmts)
600     LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
601     {
602       if (stmt->type == STMT_LIBRARY)
603       {
604           if (does_any_iface(stmt->u.lib->stmts, pred))
605               return TRUE;
606       }
607       else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
608       {
609         if (pred(stmt->u.type))
610           return TRUE;
611       }
612     }
613
614   return FALSE;
615 }
616
617 int need_proxy(const type_t *iface)
618 {
619   return is_object(iface->attrs) && !is_local(iface->attrs);
620 }
621
622 int need_stub(const type_t *iface)
623 {
624   return !is_object(iface->attrs) && !is_local(iface->attrs);
625 }
626
627 int need_proxy_file(const statement_list_t *stmts)
628 {
629   return does_any_iface(stmts, need_proxy);
630 }
631
632 int need_stub_files(const statement_list_t *stmts)
633 {
634   return does_any_iface(stmts, need_stub);
635 }
636
637 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
638 {
639   const statement_t *stmt;
640   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
641   {
642     if (stmt->type == STMT_LIBRARY)
643       write_proxy_stmts(stmt->u.lib->stmts, proc_offset);
644     else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
645     {
646       if (need_proxy(stmt->u.type))
647         write_proxy(stmt->u.type, proc_offset);
648     }
649   }
650 }
651
652 static void write_proxy_iface_name_format(const statement_list_t *stmts, const char *format)
653 {
654   const statement_t *stmt;
655   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
656   {
657     if (stmt->type == STMT_LIBRARY)
658       write_proxy_iface_name_format(stmt->u.lib->stmts, format);
659     else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
660     {
661       type_t *iface = stmt->u.type;
662       if (iface->ref && iface->funcs && need_proxy(iface))
663         fprintf(proxy, format, iface->name);
664     }
665   }
666 }
667
668 static void write_iid_lookup(const statement_list_t *stmts, const char *file_id, int *c)
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_iid_lookup(stmt->u.lib->stmts, file_id, c);
675     else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
676     {
677       type_t *iface = stmt->u.type;
678       if(iface->ref && iface->funcs && need_proxy(iface))
679       {
680         fprintf(proxy, "    if (!_%s_CHECK_IID(%d))\n", file_id, *c);
681         fprintf(proxy, "    {\n");
682         fprintf(proxy, "        *pIndex = %d;\n", *c);
683         fprintf(proxy, "        return 1;\n");
684         fprintf(proxy, "    }\n");
685         (*c)++;
686       }
687     }
688   }
689 }
690
691 void write_proxies(const statement_list_t *stmts)
692 {
693   int expr_eval_routines;
694   char *file_id = proxy_token;
695   int c;
696   unsigned int proc_offset = 0;
697
698   if (!do_proxies) return;
699   if (do_everything && !need_proxy_file(stmts)) return;
700
701   init_proxy(stmts);
702   if(!proxy) return;
703
704   write_proxy_stmts(stmts, &proc_offset);
705
706   expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
707   if (expr_eval_routines)
708       write_expr_eval_routine_list(proxy, proxy_token);
709   write_user_quad_list(proxy);
710   write_stubdesc(expr_eval_routines);
711
712   print_proxy( "#if !defined(__RPC_WIN32__)\n");
713   print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
714   print_proxy( "#endif\n");
715   print_proxy( "\n");
716   write_procformatstring(proxy, stmts, need_proxy);
717   write_typeformatstring(proxy, stmts, need_proxy);
718
719   fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
720   fprintf(proxy, "{\n");
721   write_proxy_iface_name_format(stmts, "    (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n");
722   fprintf(proxy, "    0\n");
723   fprintf(proxy, "};\n");
724   fprintf(proxy, "\n");
725
726   fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
727   fprintf(proxy, "{\n");
728   write_proxy_iface_name_format(stmts, "    (const CInterfaceStubVtbl*)&_%sStubVtbl,\n");
729   fprintf(proxy, "    0\n");
730   fprintf(proxy, "};\n");
731   fprintf(proxy, "\n");
732
733   fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
734   fprintf(proxy, "{\n");
735   write_proxy_iface_name_format(stmts, "    \"%s\",\n");
736   fprintf(proxy, "    0\n");
737   fprintf(proxy, "};\n");
738   fprintf(proxy, "\n");
739
740   fprintf(proxy, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_%s, pIID, n)\n", file_id, file_id);
741   fprintf(proxy, "\n");
742   fprintf(proxy, "int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
743   fprintf(proxy, "{\n");
744   c = 0;
745   write_iid_lookup(stmts, file_id, &c);
746   fprintf(proxy, "    return 0;\n");
747   fprintf(proxy, "}\n");
748   fprintf(proxy, "\n");
749
750   fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id);
751   fprintf(proxy, "{\n");
752   fprintf(proxy, "    (const PCInterfaceProxyVtblList*)&_%s_ProxyVtblList,\n", file_id);
753   fprintf(proxy, "    (const PCInterfaceStubVtblList*)&_%s_StubVtblList,\n", file_id);
754   fprintf(proxy, "    _%s_InterfaceNamesList,\n", file_id);
755   fprintf(proxy, "    0,\n");
756   fprintf(proxy, "    &_%s_IID_Lookup,\n", file_id);
757   fprintf(proxy, "    %d,\n", c);
758   fprintf(proxy, "    1,\n");
759   fprintf(proxy, "    0,\n");
760   fprintf(proxy, "    0,\n");
761   fprintf(proxy, "    0,\n");
762   fprintf(proxy, "    0\n");
763   fprintf(proxy, "};\n");
764
765   fclose(proxy);
766 }