msxml3: Ignore XML property ResolveExternals.
[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, ... ) __attribute__((format (printf, 1, 2)));
54 static void print_proxy( const char *format, ... )
55 {
56   va_list va;
57   va_start( va, format );
58   print( proxy, indent, format, va );
59   va_end( va );
60 }
61
62 static void write_stubdescproto(void)
63 {
64   print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
65   print_proxy( "\n");
66 }
67
68 static void write_stubdesc(int expr_eval_routines)
69 {
70   print_proxy( "static const MIDL_STUB_DESC Object_StubDesc =\n{\n");
71   indent++;
72   print_proxy( "0,\n");
73   print_proxy( "NdrOleAllocate,\n");
74   print_proxy( "NdrOleFree,\n");
75   print_proxy( "{0}, 0, 0, %s, 0,\n", expr_eval_routines ? "ExprEvalRoutines" : "0");
76   print_proxy( "__MIDL_TypeFormatString.Format,\n");
77   print_proxy( "1, /* -error bounds_check flag */\n");
78   print_proxy( "0x10001, /* Ndr library version */\n");
79   print_proxy( "0,\n");
80   print_proxy( "0x50100a4, /* MIDL Version 5.1.164 */\n");
81   print_proxy( "0,\n");
82   print_proxy("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
83   print_proxy( "0,  /* notify & notify_flag routine table */\n");
84   print_proxy( "1,  /* Flags */\n");
85   print_proxy( "0,  /* Reserved3 */\n");
86   print_proxy( "0,  /* Reserved4 */\n");
87   print_proxy( "0   /* Reserved5 */\n");
88   indent--;
89   print_proxy( "};\n");
90   print_proxy( "\n");
91 }
92
93 static void init_proxy(const statement_list_t *stmts)
94 {
95   if (proxy) return;
96   if(!(proxy = fopen(proxy_name, "w")))
97     error("Could not open %s for output\n", proxy_name);
98   print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
99   print_proxy( "\n");
100   print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
101   print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ 440\n");
102   print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
103   print_proxy( "\n");
104   print_proxy( "#define __midl_proxy\n");
105   print_proxy( "#include \"objbase.h\"\n");
106   print_proxy( "#include \"rpcproxy.h\"\n");
107   print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
108   print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
109   print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
110   print_proxy( "\n");
111   print_proxy( "#include \"%s\"\n", header_name);
112   print_proxy( "\n");
113   print_proxy( "#ifndef DECLSPEC_HIDDEN\n");
114   print_proxy( "#define DECLSPEC_HIDDEN\n");
115   print_proxy( "#endif\n");
116   print_proxy( "\n");
117 }
118
119 static void clear_output_vars( const var_list_t *args )
120 {
121   const var_t *arg;
122
123   if (!args) return;
124   LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
125   {
126     if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
127       print_proxy( "if(%s)\n", arg->name );
128       indent++;
129       print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name );
130       indent--;
131     }
132   }
133 }
134
135 int is_var_ptr(const var_t *v)
136 {
137   return is_ptr(v->type);
138 }
139
140 int cant_be_null(const var_t *v)
141 {
142     switch (typegen_detect_type(v->type, v->attrs, TDT_IGNORE_STRINGS))
143     {
144     case TGT_ARRAY:
145     case TGT_POINTER:
146         return (get_pointer_fc(v->type, v->attrs, TRUE) == RPC_FC_RP);
147     case TGT_CTXT_HANDLE_POINTER:
148         return TRUE;
149     default:
150         return 0;
151     }
152
153 }
154
155 static int need_delegation(const type_t *iface)
156 {
157     return type_iface_get_inherit(iface) &&
158            type_iface_get_inherit(type_iface_get_inherit(iface)) &&
159            type_iface_get_inherit(iface)->ignore;
160 }
161
162 static int get_delegation_indirect(const type_t *iface, const type_t ** delegate_to)
163 {
164   const type_t * cur_iface;
165   for (cur_iface = iface; cur_iface != NULL; cur_iface = type_iface_get_inherit(cur_iface))
166     if (need_delegation(cur_iface))
167     {
168       if(delegate_to)
169         *delegate_to = type_iface_get_inherit(cur_iface);
170       return 1;
171     }
172   return 0;
173 }
174
175 static int need_delegation_indirect(const type_t *iface)
176 {
177   return get_delegation_indirect(iface, NULL);
178 }
179
180 static void proxy_check_pointers( const var_list_t *args )
181 {
182   const var_t *arg;
183
184   if (!args) return;
185   LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
186   {
187     if (is_var_ptr(arg) && cant_be_null(arg)) {
188         print_proxy( "if(!%s)\n", arg->name );
189         indent++;
190         print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
191         indent--;
192     }
193   }
194 }
195
196 static void free_variable( const var_t *arg, const char *local_var_prefix )
197 {
198   unsigned int type_offset = arg->type->typestring_offset;
199   expr_t *iid;
200   type_t *type = arg->type;
201   expr_t *size = get_size_is_expr(type, arg->name);
202
203   if (size)
204   {
205     print_proxy( "__frame->_StubMsg.MaxCount = " );
206     write_expr(proxy, size, 0, 1, NULL, NULL, local_var_prefix);
207     fprintf(proxy, ";\n\n");
208     print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
209     fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
210     fprintf(proxy, "(void*)%s );\n", arg->name );
211     return;
212   }
213
214   switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
215   {
216   case TGT_ENUM:
217   case TGT_BASIC:
218     break;
219
220   case TGT_STRUCT:
221     if (get_struct_fc(type) != RPC_FC_STRUCT)
222       print_proxy("/* FIXME: %s code for %s struct type 0x%x missing */\n", __FUNCTION__, arg->name, get_struct_fc(type) );
223     break;
224
225   case TGT_IFACE_POINTER:
226     iid = get_attrp( arg->attrs, ATTR_IIDIS );
227     if( iid )
228     {
229       print_proxy( "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
230       write_expr(proxy, iid, 1, 1, NULL, NULL, local_var_prefix);
231       print_proxy( ";\n\n" );
232     }
233     /* fall through */
234   case TGT_POINTER:
235     if (get_pointer_fc(type, arg->attrs, TRUE) == RPC_FC_FP)
236     {
237       print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
238       fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
239       fprintf(proxy, "(void*)%s );\n", arg->name );
240     }
241     else
242       print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
243     break;
244
245   default:
246     print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
247   }
248 }
249
250 static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
251 {
252   const var_t *arg;
253
254   if (!args) return;
255   LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
256     if (is_attr(arg->attrs, ATTR_OUT))
257     {
258       free_variable( arg, local_var_prefix );
259       fprintf(proxy, "\n");
260     }
261 }
262
263 static void gen_proxy(type_t *iface, const var_t *func, int idx,
264                       unsigned int proc_offset)
265 {
266   int has_ret = !is_void(type_function_get_rettype(func->type));
267   int has_full_pointer = is_full_pointer_function(func);
268   const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
269   if (!callconv) callconv = "STDMETHODCALLTYPE";
270
271   indent = 0;
272   print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
273                iface->name, get_name(func) );
274   print_proxy( "{\n");
275   indent++;
276   if (has_full_pointer) write_full_pointer_free(proxy, indent, func);
277   print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
278   indent--;
279   print_proxy( "}\n");
280   print_proxy( "\n");
281
282   write_type_decl_left(proxy, type_function_get_rettype(func->type));
283   print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
284   write_args(proxy, type_get_function_args(func->type), iface->name, 1, TRUE);
285   print_proxy( ")\n");
286   print_proxy( "{\n");
287   indent ++;
288   print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
289   /* local variables */
290   if (has_ret) {
291     print_proxy( "%s", "" );
292     write_type_decl_left(proxy, type_function_get_rettype(func->type));
293     print_proxy( " _RetVal;\n");
294   }
295   print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
296   if (has_ret) {
297     if (decl_indirect(type_function_get_rettype(func->type)))
298       print_proxy("void *_p_%s = &%s;\n",
299                  "_RetVal", "_RetVal");
300   }
301   print_proxy( "\n");
302
303   print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(func) );
304   print_proxy( "__frame->This = This;\n" );
305
306   if (has_full_pointer)
307     write_full_pointer_init(proxy, indent, func, FALSE);
308
309   /* FIXME: trace */
310   clear_output_vars( type_get_function_args(func->type) );
311
312   print_proxy( "RpcTryExcept\n" );
313   print_proxy( "{\n" );
314   indent++;
315   print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx);
316   proxy_check_pointers( type_get_function_args(func->type) );
317
318   print_proxy( "RpcTryFinally\n" );
319   print_proxy( "{\n" );
320   indent++;
321
322   write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
323
324   print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
325
326   write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_MARSHAL);
327
328   print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
329   fprintf(proxy, "\n");
330   print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
331   print_proxy( "__frame->_StubMsg.BufferEnd   = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
332
333   print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
334   indent++;
335   print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
336   indent--;
337   fprintf(proxy, "\n");
338
339   write_remoting_arguments(proxy, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
340
341   if (has_ret)
342   {
343       if (decl_indirect(type_function_get_rettype(func->type)))
344           print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
345       else if (is_ptr(type_function_get_rettype(func->type)) ||
346                is_array(type_function_get_rettype(func->type)))
347           print_proxy("%s = 0;\n", "_RetVal");
348       write_remoting_arguments(proxy, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
349   }
350
351   indent--;
352   print_proxy( "}\n");
353   print_proxy( "RpcFinally\n" );
354   print_proxy( "{\n" );
355   indent++;
356   print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(func) );
357   indent--;
358   print_proxy( "}\n");
359   print_proxy( "RpcEndFinally\n" );
360   indent--;
361   print_proxy( "}\n" );
362   print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
363   print_proxy( "{\n" );
364   if (has_ret) {
365     indent++;
366     proxy_free_variables( type_get_function_args(func->type), "" );
367     print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
368     indent--;
369   }
370   print_proxy( "}\n" );
371   print_proxy( "RpcEndExcept\n" );
372
373   if (has_ret) {
374     print_proxy( "return _RetVal;\n" );
375   }
376   indent--;
377   print_proxy( "}\n");
378   print_proxy( "\n");
379 }
380
381 static void gen_stub(type_t *iface, const var_t *func, const char *cas,
382                      unsigned int proc_offset)
383 {
384   const var_t *arg;
385   int has_ret = !is_void(type_function_get_rettype(func->type));
386   int has_full_pointer = is_full_pointer_function(func);
387
388   indent = 0;
389   print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(func));
390   indent++;
391   print_proxy( "__DECL_EXCEPTION_FRAME\n" );
392   print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
393   print_proxy( "%s * _This;\n", iface->name );
394   declare_stub_args( proxy, indent, func );
395   indent--;
396   print_proxy( "};\n\n" );
397
398   print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(func) );
399   print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(func) );
400   indent++;
401   write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
402   if (has_full_pointer)
403     write_full_pointer_free(proxy, indent, func);
404   indent--;
405   print_proxy( "}\n\n" );
406
407   print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
408   indent++;
409   print_proxy( "IRpcStubBuffer* This,\n");
410   print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
411   print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
412   print_proxy( "DWORD* _pdwStubPhase)\n");
413   indent--;
414   print_proxy( "{\n");
415   indent++;
416   print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
417                iface->name, get_name(func) );
418
419   print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
420
421   /* FIXME: trace */
422
423   print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
424   fprintf(proxy, "\n");
425   print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(func) );
426
427   write_parameters_init(proxy, indent, func, "__frame->");
428
429   print_proxy("RpcTryFinally\n");
430   print_proxy("{\n");
431   indent++;
432   if (has_full_pointer)
433     write_full_pointer_init(proxy, indent, func, TRUE);
434   print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
435   indent++;
436   print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
437   indent--;
438   fprintf(proxy, "\n");
439
440   write_remoting_arguments(proxy, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
441   fprintf(proxy, "\n");
442
443   assign_stub_out_args( proxy, indent, func, "__frame->" );
444
445   print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
446   fprintf(proxy, "\n");
447   print_proxy( "%s", has_ret ? "__frame->_RetVal = " : "" );
448   if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
449   else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(func));
450   fprintf(proxy, "(__frame->_This");
451
452   if (type_get_function_args(func->type))
453   {
454       LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
455           fprintf(proxy, ", %s__frame->%s", is_array(arg->type) && !type_array_is_decl_as_ptr(arg->type) ? "*" :"" , arg->name);
456   }
457   fprintf(proxy, ");\n");
458   fprintf(proxy, "\n");
459   print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
460   fprintf(proxy, "\n");
461
462   write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
463
464   if (!is_void(type_function_get_rettype(func->type)))
465     write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
466
467   print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
468
469   write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
470   fprintf(proxy, "\n");
471
472   /* marshall the return value */
473   if (!is_void(type_function_get_rettype(func->type)))
474     write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
475
476   indent--;
477   print_proxy("}\n");
478   print_proxy("RpcFinally\n");
479   print_proxy("{\n");
480   indent++;
481   print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(func) );
482   indent--;
483   print_proxy("}\n");
484   print_proxy("RpcEndFinally\n");
485
486   print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
487   indent--;
488
489   print_proxy("}\n");
490   print_proxy("\n");
491 }
492
493 int count_methods(const type_t *iface)
494 {
495     const statement_t *stmt;
496     int count = 0;
497
498     if (type_iface_get_inherit(iface))
499         count = count_methods(type_iface_get_inherit(iface));
500
501     STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
502         const var_t *func = stmt->u.var;
503         if (!is_callas(func->attrs)) count++;
504     }
505     return count;
506 }
507
508 static const statement_t * get_callas_source(const type_t * iface, const var_t * def)
509 {
510   const statement_t * source;
511   STATEMENTS_FOR_EACH_FUNC( source, type_iface_get_stmts(iface)) {
512     const var_t * cas = is_callas(source->u.var->attrs );
513     if (cas && !strcmp(def->name, cas->name))
514       return source;
515   }
516   return NULL;
517 }
518
519 static int write_proxy_methods(type_t *iface, int skip)
520 {
521   const statement_t *stmt;
522   int i = 0;
523
524   if (type_iface_get_inherit(iface))
525     i = write_proxy_methods(type_iface_get_inherit(iface),
526                             need_delegation(iface));
527   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
528     const var_t *func = stmt->u.var;
529     if (!is_callas(func->attrs)) {
530       if (i != func->type->details.function->idx )
531         error("widl internal error: method index mismatch\n");
532       if (i) fprintf(proxy, ",\n");
533       if (skip || (is_local(func->attrs) && !get_callas_source(iface, func)))
534            print_proxy( "0  /* %s_%s_Proxy */", iface->name, get_name(func));
535       else print_proxy( "%s_%s_Proxy", iface->name, get_name(func));
536       i++;
537     }
538   }
539   return i;
540 }
541
542 static int write_stub_methods(type_t *iface, int skip)
543 {
544   const statement_t *stmt;
545   int i = 0;
546
547   if (type_iface_get_inherit(iface))
548     i = write_stub_methods(type_iface_get_inherit(iface), need_delegation(iface));
549   else
550     return i; /* skip IUnknown */
551
552   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
553     const var_t *func = stmt->u.var;
554     if (!is_callas(func->attrs)) {
555       int missing = 0;
556       const char * fname = get_name(func);
557       if(is_local(func->attrs)) {
558         const statement_t * callas_source = get_callas_source(iface, func);
559         if(!callas_source)
560           missing = 1;
561         else
562           fname = get_name(callas_source->u.var);
563       }
564       if (i) fprintf(proxy,",\n");
565       if (skip || missing) print_proxy("STUB_FORWARDING_FUNCTION");
566       else print_proxy( "%s_%s_Stub", iface->name, fname);
567       i++;
568     }
569   }
570   return i;
571 }
572
573 static void write_proxy(type_t *iface, unsigned int *proc_offset)
574 {
575   int count;
576   const statement_t *stmt;
577   int first_func = 1;
578
579   STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
580     var_t *func = stmt->u.var;
581     if (first_func) {
582       fprintf(proxy, "/*****************************************************************************\n");
583       fprintf(proxy, " * %s interface\n", iface->name);
584       fprintf(proxy, " */\n");
585       first_func = 0;
586     }
587     if (!is_local(func->attrs)) {
588       const var_t *cas = is_callas(func->attrs);
589       const char *cname = cas ? cas->name : NULL;
590       int idx = func->type->details.function->idx;
591       if (cname) {
592           const statement_t *stmt2;
593           STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface)) {
594               const var_t *m = stmt2->u.var;
595               if (!strcmp(m->name, cname))
596               {
597                   idx = m->type->details.function->idx;
598                   break;
599               }
600           }
601       }
602       func->procstring_offset = *proc_offset;
603       gen_proxy(iface, func, idx, *proc_offset);
604       gen_stub(iface, func, cname, *proc_offset);
605       *proc_offset += get_size_procformatstring_func( func );
606     }
607   }
608
609   count = count_methods(iface);
610
611   write_procformatstring_offsets( proxy, iface );
612
613   /* proxy vtable */
614   print_proxy( "static %sCINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n",
615                need_delegation_indirect(iface) ? "" : "const ", count, iface->name);
616   print_proxy( "{\n");
617   indent++;
618   print_proxy( "{\n");
619   indent++;
620   print_proxy( "&IID_%s,\n", iface->name);
621   indent--;
622   print_proxy( "},\n");
623   print_proxy( "{\n");
624   indent++;
625   write_proxy_methods(iface, FALSE);
626   fprintf(proxy, "\n");
627   indent--;
628   print_proxy( "}\n");
629   indent--;
630   print_proxy( "};\n\n");
631
632   /* server info */
633   print_proxy( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
634   print_proxy( "{\n" );
635   indent++;
636   print_proxy( "&Object_StubDesc,\n" );
637   print_proxy( "0,\n" );
638   print_proxy( "__MIDL_ProcFormatString.Format,\n" );
639   print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
640   print_proxy( "0,\n" );
641   print_proxy( "0,\n" );
642   print_proxy( "0,\n" );
643   print_proxy( "0\n" );
644   indent--;
645   print_proxy( "};\n\n" );
646
647   /* stub vtable */
648   print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
649   print_proxy( "{\n");
650   indent++;
651   write_stub_methods(iface, FALSE);
652   fprintf(proxy, "\n");
653   indent--;
654   fprintf(proxy, "};\n");
655   print_proxy( "\n");
656   print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
657                need_delegation_indirect(iface) ? "" : "const ", iface->name);
658   print_proxy( "{\n");
659   indent++;
660   print_proxy( "{\n");
661   indent++;
662   print_proxy( "&IID_%s,\n", iface->name);
663   print_proxy( "&%s_ServerInfo,\n", iface->name );
664   print_proxy( "%d,\n", count);
665   print_proxy( "&%s_table[-3],\n", iface->name);
666   indent--;
667   print_proxy( "},\n");
668   print_proxy( "{\n");
669   indent++;
670   print_proxy( "CStdStubBuffer_%s\n", need_delegation_indirect(iface) ? "DELEGATING_METHODS" : "METHODS");
671   indent--;
672   print_proxy( "}\n");
673   indent--;
674   print_proxy( "};\n");
675   print_proxy( "\n");
676 }
677
678 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
679 {
680   const statement_t *stmt;
681
682   if (stmts)
683     LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
684     {
685       if (stmt->type == STMT_LIBRARY)
686       {
687           if (does_any_iface(stmt->u.lib->stmts, pred))
688               return TRUE;
689       }
690       else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
691       {
692         if (pred(stmt->u.type))
693           return TRUE;
694       }
695     }
696
697   return FALSE;
698 }
699
700 int need_proxy(const type_t *iface)
701 {
702     if (!is_object( iface )) return 0;
703     if (is_local( iface->attrs )) return 0;
704     if (is_attr( iface->attrs, ATTR_OLEAUTOMATION )) return 0;
705     if (is_attr( iface->attrs, ATTR_DISPINTERFACE )) return 0;
706     return 1;
707 }
708
709 int need_stub(const type_t *iface)
710 {
711   return !is_object(iface) && !is_local(iface->attrs);
712 }
713
714 int need_proxy_file(const statement_list_t *stmts)
715 {
716   return does_any_iface(stmts, need_proxy);
717 }
718
719 int need_stub_files(const statement_list_t *stmts)
720 {
721   return does_any_iface(stmts, need_stub);
722 }
723
724 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
725 {
726   const statement_t *stmt;
727   if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
728   {
729     if (stmt->type == STMT_LIBRARY)
730       write_proxy_stmts(stmt->u.lib->stmts, proc_offset);
731     else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
732     {
733       if (need_proxy(stmt->u.type))
734         write_proxy(stmt->u.type, proc_offset);
735     }
736   }
737 }
738
739 static int cmp_iid( const void *ptr1, const void *ptr2 )
740 {
741     const type_t * const *iface1 = ptr1;
742     const type_t * const *iface2 = ptr2;
743     const UUID *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
744     const UUID *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
745     return memcmp( uuid1, uuid2, sizeof(UUID) );
746 }
747
748 static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
749 {
750     const statement_t *stmt;
751
752     if (!stmts) return;
753     LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
754     {
755         if (stmt->type == STMT_LIBRARY)
756             build_iface_list(stmt->u.lib->stmts, ifaces, count);
757         else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
758         {
759             type_t *iface = stmt->u.type;
760             if (type_iface_get_inherit(iface) && need_proxy(iface))
761             {
762                 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
763                 (*ifaces)[(*count)++] = iface;
764             }
765         }
766     }
767 }
768
769 static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
770 {
771     type_t **ifaces = NULL;
772
773     *count = 0;
774     build_iface_list( stmts, &ifaces, count );
775     qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
776     return ifaces;
777 }
778
779 static void write_proxy_routines(const statement_list_t *stmts)
780 {
781   int expr_eval_routines;
782   unsigned int proc_offset = 0;
783   char *file_id = proxy_token;
784   int i, count, have_baseiid;
785   type_t **interfaces;
786   const type_t * delegate_to;
787
788   write_exceptions( proxy );
789   print_proxy( "\n");
790   print_proxy( "struct __proxy_frame\n");
791   print_proxy( "{\n");
792   print_proxy( "    __DECL_EXCEPTION_FRAME\n");
793   print_proxy( "    MIDL_STUB_MESSAGE _StubMsg;\n");
794   print_proxy( "    void             *This;\n");
795   print_proxy( "};\n");
796   print_proxy( "\n");
797   print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
798   print_proxy( "{\n");
799   print_proxy( "    return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
800   print_proxy( "}\n");
801   print_proxy( "\n");
802
803   write_formatstringsdecl(proxy, indent, stmts, need_proxy);
804   write_stubdescproto();
805   write_proxy_stmts(stmts, &proc_offset);
806
807   expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
808   if (expr_eval_routines)
809       write_expr_eval_routine_list(proxy, proxy_token);
810   write_user_quad_list(proxy);
811   write_stubdesc(expr_eval_routines);
812
813   print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
814   print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
815   print_proxy( "#endif\n");
816   print_proxy( "\n");
817   write_procformatstring(proxy, stmts, need_proxy);
818   write_typeformatstring(proxy, stmts, need_proxy);
819
820   interfaces = sort_interfaces(stmts, &count);
821   fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
822   fprintf(proxy, "{\n");
823   for (i = 0; i < count; i++)
824       fprintf(proxy, "    (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
825   fprintf(proxy, "    0\n");
826   fprintf(proxy, "};\n");
827   fprintf(proxy, "\n");
828
829   fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
830   fprintf(proxy, "{\n");
831   for (i = 0; i < count; i++)
832       fprintf(proxy, "    &_%sStubVtbl,\n", interfaces[i]->name);
833   fprintf(proxy, "    0\n");
834   fprintf(proxy, "};\n");
835   fprintf(proxy, "\n");
836
837   fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
838   fprintf(proxy, "{\n");
839   for (i = 0; i < count; i++)
840       fprintf(proxy, "    \"%s\",\n", interfaces[i]->name);
841   fprintf(proxy, "    0\n");
842   fprintf(proxy, "};\n");
843   fprintf(proxy, "\n");
844
845   if ((have_baseiid = does_any_iface(stmts, need_delegation_indirect)))
846   {
847       fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
848       fprintf(proxy, "{\n");
849       for (i = 0; i < count; i++)
850       {
851           if (get_delegation_indirect(interfaces[i], &delegate_to))
852               fprintf( proxy, "    &IID_%s,  /* %s */\n", delegate_to->name, interfaces[i]->name );
853           else
854               fprintf( proxy, "    0,\n" );
855       }
856       fprintf(proxy, "    0\n");
857       fprintf(proxy, "};\n");
858       fprintf(proxy, "\n");
859   }
860
861   fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
862   fprintf(proxy, "{\n");
863   fprintf(proxy, "    int low = 0, high = %d;\n", count - 1);
864   fprintf(proxy, "\n");
865   fprintf(proxy, "    while (low <= high)\n");
866   fprintf(proxy, "    {\n");
867   fprintf(proxy, "        int pos = (low + high) / 2;\n");
868   fprintf(proxy, "        int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
869   fprintf(proxy, "        if (!res) { *pIndex = pos; return 1; }\n");
870   fprintf(proxy, "        if (res > 0) low = pos + 1;\n");
871   fprintf(proxy, "        else high = pos - 1;\n");
872   fprintf(proxy, "    }\n");
873   fprintf(proxy, "    return 0;\n");
874   fprintf(proxy, "}\n");
875   fprintf(proxy, "\n");
876
877   fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo DECLSPEC_HIDDEN =\n", file_id);
878   fprintf(proxy, "{\n");
879   fprintf(proxy, "    (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
880   fprintf(proxy, "    (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
881   fprintf(proxy, "    _%s_InterfaceNamesList,\n", file_id);
882   if (have_baseiid) fprintf(proxy, "    _%s_BaseIIDList,\n", file_id);
883   else fprintf(proxy, "    0,\n");
884   fprintf(proxy, "    _%s_IID_Lookup,\n", file_id);
885   fprintf(proxy, "    %d,\n", count);
886   fprintf(proxy, "    1,\n");
887   fprintf(proxy, "    0,\n");
888   fprintf(proxy, "    0,\n");
889   fprintf(proxy, "    0,\n");
890   fprintf(proxy, "    0\n");
891   fprintf(proxy, "};\n");
892 }
893
894 void write_proxies(const statement_list_t *stmts)
895 {
896   if (!do_proxies) return;
897   if (do_everything && !need_proxy_file(stmts)) return;
898
899   init_proxy(stmts);
900   if(!proxy) return;
901
902   if (do_win32 && do_win64)
903   {
904       fprintf(proxy, "\n#ifndef _WIN64\n\n");
905       pointer_size = 4;
906       write_proxy_routines( stmts );
907       fprintf(proxy, "\n#else /* _WIN64 */\n\n");
908       pointer_size = 8;
909       write_proxy_routines( stmts );
910       fprintf(proxy, "\n#endif /* _WIN64 */\n");
911   }
912   else if (do_win32)
913   {
914       pointer_size = 4;
915       write_proxy_routines( stmts );
916   }
917   else if (do_win64)
918   {
919       pointer_size = 8;
920       write_proxy_routines( stmts );
921   }
922
923   fclose(proxy);
924 }