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