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