gdi32/tests: Add some GetDIBits tests with zero lines and bits parameters.
[wine/multimedia.git] / tools / widl / proxy.c
blob699e073bc359f3683fd3178fb2a352a2989bb7de
1 /*
2 * IDL Compiler
4 * Copyright 2002 Ove Kaaven
5 * Copyright 2004 Mike McCormack
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
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>
33 #include "widl.h"
34 #include "utils.h"
35 #include "parser.h"
36 #include "header.h"
37 #include "typegen.h"
38 #include "expr.h"
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)
48 static FILE* proxy;
49 static int indent = 0;
51 static void print_proxy( const char *format, ... ) __attribute__((format (printf, 1, 2)));
52 static void print_proxy( const char *format, ... )
54 va_list va;
55 va_start( va, format );
56 print( proxy, indent, format, va );
57 va_end( va );
60 static void write_stubdescproto(void)
62 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
63 print_proxy( "\n");
66 static void write_stubdesc(int expr_eval_routines)
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");
91 static void init_proxy(const statement_list_t *stmts)
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");
107 static void clear_output_vars( const var_list_t *args )
109 const var_t *arg;
111 if (!args) return;
112 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
114 if (is_attr(arg->attrs, ATTR_IN)) continue;
115 if (!is_attr(arg->attrs, ATTR_OUT)) continue;
116 if (is_ptr(arg->type))
118 if (type_get_type(type_pointer_get_ref(arg->type)) == TYPE_BASIC) continue;
119 if (type_get_type(type_pointer_get_ref(arg->type)) == TYPE_ENUM) continue;
121 print_proxy( "if (%s) MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name, arg->name );
125 static int need_delegation(const type_t *iface)
127 const type_t *parent = type_iface_get_inherit( iface );
128 return parent && type_iface_get_inherit(parent) && (parent->ignore || is_local( parent->attrs ));
131 static int get_delegation_indirect(const type_t *iface, const type_t ** delegate_to)
133 const type_t * cur_iface;
134 for (cur_iface = iface; cur_iface != NULL; cur_iface = type_iface_get_inherit(cur_iface))
135 if (need_delegation(cur_iface))
137 if(delegate_to)
138 *delegate_to = type_iface_get_inherit(cur_iface);
139 return 1;
141 return 0;
144 static int need_delegation_indirect(const type_t *iface)
146 return get_delegation_indirect(iface, NULL);
149 static void free_variable( const var_t *arg, const char *local_var_prefix )
151 unsigned int type_offset = arg->type->typestring_offset;
152 type_t *type = arg->type;
154 write_parameter_conf_or_var_exprs(proxy, indent, local_var_prefix, PHASE_FREE, arg, FALSE);
156 switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
158 case TGT_ENUM:
159 case TGT_BASIC:
160 break;
162 case TGT_STRUCT:
163 if (get_struct_fc(type) != RPC_FC_STRUCT)
164 print_proxy("/* FIXME: %s code for %s struct type 0x%x missing */\n", __FUNCTION__, arg->name, get_struct_fc(type) );
165 break;
167 case TGT_IFACE_POINTER:
168 case TGT_POINTER:
169 case TGT_ARRAY:
170 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
171 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
172 fprintf(proxy, "(void *)%s );\n", arg->name );
173 break;
175 default:
176 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
180 static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
182 const var_t *arg;
184 if (!args) return;
185 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
186 if (is_attr(arg->attrs, ATTR_OUT))
188 free_variable( arg, local_var_prefix );
189 fprintf(proxy, "\n");
193 static void gen_proxy(type_t *iface, const var_t *func, int idx,
194 unsigned int proc_offset)
196 type_t *rettype = type_function_get_rettype(func->type);
197 int has_ret = !is_void(rettype);
198 int has_full_pointer = is_full_pointer_function(func);
199 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
200 const var_list_t *args = type_get_function_args(func->type);
201 if (!callconv) callconv = "STDMETHODCALLTYPE";
203 indent = 0;
204 if (is_interpreted_func( iface, func ))
206 if (get_stub_mode() == MODE_Oif && !is_callas( func->attrs )) return;
207 write_type_decl_left(proxy, type_function_get_rettype(func->type));
208 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
209 write_args(proxy, args, iface->name, 1, TRUE);
210 print_proxy( ")\n");
211 write_client_call_routine( proxy, iface, func, "Object", proc_offset );
212 return;
214 print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
215 iface->name, get_name(func) );
216 print_proxy( "{\n");
217 indent++;
218 if (has_full_pointer) write_full_pointer_free(proxy, indent, func);
219 print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
220 indent--;
221 print_proxy( "}\n");
222 print_proxy( "\n");
224 write_type_decl_left(proxy, rettype);
225 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
226 write_args(proxy, args, iface->name, 1, TRUE);
227 print_proxy( ")\n");
228 print_proxy( "{\n");
229 indent ++;
230 print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
231 /* local variables */
232 if (has_ret) {
233 print_proxy( "%s", "" );
234 write_type_decl_left(proxy, rettype);
235 print_proxy( " _RetVal;\n");
237 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
238 if (has_ret) {
239 if (decl_indirect(rettype))
240 print_proxy("void *_p_%s = &%s;\n",
241 "_RetVal", "_RetVal");
243 print_proxy( "\n");
245 print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(func) );
246 print_proxy( "__frame->This = This;\n" );
248 if (has_full_pointer)
249 write_full_pointer_init(proxy, indent, func, FALSE);
251 /* FIXME: trace */
252 clear_output_vars( type_get_function_args(func->type) );
254 print_proxy( "RpcTryExcept\n" );
255 print_proxy( "{\n" );
256 indent++;
257 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx);
258 write_pointer_checks( proxy, indent, func );
260 print_proxy( "RpcTryFinally\n" );
261 print_proxy( "{\n" );
262 indent++;
264 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
266 print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
268 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_MARSHAL);
270 print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
271 fprintf(proxy, "\n");
272 print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
273 print_proxy( "__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
275 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
276 indent++;
277 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
278 indent--;
279 fprintf(proxy, "\n");
281 write_remoting_arguments(proxy, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
283 if (has_ret)
285 if (decl_indirect(rettype))
286 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
287 else if (is_ptr(rettype) || is_array(rettype))
288 print_proxy("%s = 0;\n", "_RetVal");
289 write_remoting_arguments(proxy, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
292 indent--;
293 print_proxy( "}\n");
294 print_proxy( "RpcFinally\n" );
295 print_proxy( "{\n" );
296 indent++;
297 print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(func) );
298 indent--;
299 print_proxy( "}\n");
300 print_proxy( "RpcEndFinally\n" );
301 indent--;
302 print_proxy( "}\n" );
303 print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
304 print_proxy( "{\n" );
305 if (has_ret) {
306 indent++;
307 proxy_free_variables( type_get_function_args(func->type), "" );
308 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
309 indent--;
311 print_proxy( "}\n" );
312 print_proxy( "RpcEndExcept\n" );
314 if (has_ret) {
315 print_proxy( "return _RetVal;\n" );
317 indent--;
318 print_proxy( "}\n");
319 print_proxy( "\n");
322 static void gen_stub(type_t *iface, const var_t *func, const char *cas,
323 unsigned int proc_offset)
325 const var_t *arg;
326 int has_ret = !is_void(type_function_get_rettype(func->type));
327 int has_full_pointer = is_full_pointer_function(func);
329 if (is_interpreted_func( iface, func )) return;
331 indent = 0;
332 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(func));
333 indent++;
334 print_proxy( "__DECL_EXCEPTION_FRAME\n" );
335 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
336 print_proxy( "%s * _This;\n", iface->name );
337 declare_stub_args( proxy, indent, func );
338 indent--;
339 print_proxy( "};\n\n" );
341 print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(func) );
342 print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(func) );
343 indent++;
344 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
345 if (has_full_pointer)
346 write_full_pointer_free(proxy, indent, func);
347 indent--;
348 print_proxy( "}\n\n" );
350 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
351 indent++;
352 print_proxy( "IRpcStubBuffer* This,\n");
353 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
354 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
355 print_proxy( "DWORD* _pdwStubPhase)\n");
356 indent--;
357 print_proxy( "{\n");
358 indent++;
359 print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
360 iface->name, get_name(func) );
362 print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
364 /* FIXME: trace */
366 print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
367 fprintf(proxy, "\n");
368 print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(func) );
370 write_parameters_init(proxy, indent, func, "__frame->");
372 print_proxy("RpcTryFinally\n");
373 print_proxy("{\n");
374 indent++;
375 if (has_full_pointer)
376 write_full_pointer_init(proxy, indent, func, TRUE);
377 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
378 indent++;
379 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
380 indent--;
381 fprintf(proxy, "\n");
383 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
384 fprintf(proxy, "\n");
386 assign_stub_out_args( proxy, indent, func, "__frame->" );
388 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
389 fprintf(proxy, "\n");
390 print_proxy( "%s", has_ret ? "__frame->_RetVal = " : "" );
391 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
392 else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(func));
393 fprintf(proxy, "(__frame->_This");
395 if (type_get_function_args(func->type))
397 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
398 fprintf(proxy, ", %s__frame->%s", is_array(arg->type) && !type_array_is_decl_as_ptr(arg->type) ? "*" :"" , arg->name);
400 fprintf(proxy, ");\n");
401 fprintf(proxy, "\n");
402 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
403 fprintf(proxy, "\n");
405 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
407 if (!is_void(type_function_get_rettype(func->type)))
408 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
410 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
412 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
413 fprintf(proxy, "\n");
415 /* marshall the return value */
416 if (!is_void(type_function_get_rettype(func->type)))
417 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
419 indent--;
420 print_proxy("}\n");
421 print_proxy("RpcFinally\n");
422 print_proxy("{\n");
423 indent++;
424 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(func) );
425 indent--;
426 print_proxy("}\n");
427 print_proxy("RpcEndFinally\n");
429 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
430 indent--;
432 print_proxy("}\n");
433 print_proxy("\n");
436 static void gen_stub_thunk( type_t *iface, const var_t *func, unsigned int proc_offset )
438 int has_ret = !is_void( type_function_get_rettype( func->type ));
439 const var_t *arg, *callas = is_callas( func->attrs );
440 const var_list_t *args = type_get_function_args( func->type );
442 indent = 0;
443 print_proxy( "void __RPC_API %s_%s_Thunk( PMIDL_STUB_MESSAGE pStubMsg )\n",
444 iface->name, get_name(func) );
445 print_proxy( "{\n");
446 indent++;
447 write_func_param_struct( proxy, iface, func->type,
448 "*pParamStruct = (struct _PARAM_STRUCT *)pStubMsg->StackTop", has_ret );
449 print_proxy( "%s%s_%s_Stub( pParamStruct->This",
450 has_ret ? "pParamStruct->_RetVal = " : "", iface->name, callas->name );
451 indent++;
452 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
454 fprintf( proxy, ",\n%*spParamStruct->%s", 4 * indent, "", arg->name );
456 fprintf( proxy, " );\n" );
457 indent--;
458 indent--;
459 print_proxy( "}\n\n");
462 int count_methods(const type_t *iface)
464 const statement_t *stmt;
465 int count = 0;
467 if (type_iface_get_inherit(iface))
468 count = count_methods(type_iface_get_inherit(iface));
470 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
471 const var_t *func = stmt->u.var;
472 if (!is_callas(func->attrs)) count++;
474 return count;
477 static const statement_t * get_callas_source(const type_t * iface, const var_t * def)
479 const statement_t * source;
480 STATEMENTS_FOR_EACH_FUNC( source, type_iface_get_stmts(iface)) {
481 const var_t * cas = is_callas(source->u.var->attrs );
482 if (cas && !strcmp(def->name, cas->name))
483 return source;
485 return NULL;
488 static void write_proxy_procformatstring_offsets( const type_t *iface, int skip )
490 const statement_t *stmt;
492 if (type_iface_get_inherit(iface))
493 write_proxy_procformatstring_offsets( type_iface_get_inherit(iface), need_delegation(iface));
494 else
495 return;
497 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
499 const var_t *func = stmt->u.var;
500 int missing = 0;
502 if (is_callas(func->attrs)) continue;
503 if (is_local(func->attrs))
505 const statement_t * callas_source = get_callas_source(iface, func);
506 if (!callas_source)
507 missing = 1;
508 else
509 func = callas_source->u.var;
511 if (skip || missing)
512 print_proxy( "(unsigned short)-1, /* %s::%s */\n", iface->name, get_name(func));
513 else
514 print_proxy( "%u, /* %s::%s */\n", func->procstring_offset, iface->name, get_name(func));
518 static int write_proxy_methods(type_t *iface, int skip)
520 const statement_t *stmt;
521 int i = 0;
523 if (type_iface_get_inherit(iface))
524 i = write_proxy_methods(type_iface_get_inherit(iface),
525 need_delegation(iface));
526 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
527 const var_t *func = stmt->u.var;
528 if (!is_callas(func->attrs)) {
529 if (skip || (is_local(func->attrs) && !get_callas_source(iface, func)))
530 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func));
531 else if (is_interpreted_func( iface, func ) &&
532 !is_local( func->attrs ) &&
533 type_iface_get_inherit(iface))
534 print_proxy( "(void *)-1, /* %s::%s */\n", iface->name, get_name(func));
535 else
536 print_proxy( "%s_%s_Proxy,\n", iface->name, get_name(func));
537 i++;
540 return i;
543 static int write_stub_methods(type_t *iface, int skip)
545 const statement_t *stmt;
546 int i = 0;
548 if (type_iface_get_inherit(iface))
549 i = write_stub_methods(type_iface_get_inherit(iface), need_delegation(iface));
550 else
551 return i; /* skip IUnknown */
553 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
554 const var_t *func = stmt->u.var;
555 if (!is_callas(func->attrs)) {
556 int missing = 0;
557 const char * fname = get_name(func);
558 if(is_local(func->attrs)) {
559 const statement_t * callas_source = get_callas_source(iface, func);
560 if(!callas_source)
561 missing = 1;
562 else
563 fname = get_name(callas_source->u.var);
565 if (i) fprintf(proxy,",\n");
566 if (skip || missing) print_proxy("STUB_FORWARDING_FUNCTION");
567 else if (is_interpreted_func( iface, func ))
568 print_proxy( "(PRPC_STUB_FUNCTION)%s", get_stub_mode() == MODE_Oif ? "NdrStubCall2" : "NdrStubCall" );
569 else print_proxy( "%s_%s_Stub", iface->name, fname);
570 i++;
573 return i;
576 static void write_thunk_methods( type_t *iface, int skip )
578 const statement_t *stmt;
580 if (type_iface_get_inherit( iface ))
581 write_thunk_methods( type_iface_get_inherit(iface), need_delegation(iface) );
582 else
583 return; /* skip IUnknown */
585 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
587 var_t *func = stmt->u.var;
588 const statement_t * callas_source = NULL;
590 if (is_callas(func->attrs)) continue;
591 if (is_local(func->attrs)) callas_source = get_callas_source(iface, func);
593 if (!skip && callas_source && is_interpreted_func( iface, func ))
594 print_proxy( "%s_%s_Thunk,\n", iface->name, get_name(callas_source->u.var) );
595 else
596 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func) );
600 static void write_proxy(type_t *iface, unsigned int *proc_offset)
602 int count;
603 const statement_t *stmt;
604 int first_func = 1;
605 int needs_stub_thunks = 0;
606 int needs_inline_stubs = need_inline_stubs( iface ) || need_delegation( iface );
608 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
609 var_t *func = stmt->u.var;
610 if (first_func) {
611 fprintf(proxy, "/*****************************************************************************\n");
612 fprintf(proxy, " * %s interface\n", iface->name);
613 fprintf(proxy, " */\n");
614 first_func = 0;
616 if (!is_local(func->attrs)) {
617 const var_t *cas = is_callas(func->attrs);
618 const char *cname = cas ? cas->name : NULL;
619 int idx = func->type->details.function->idx;
620 if (cname) {
621 const statement_t *stmt2;
622 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface)) {
623 const var_t *m = stmt2->u.var;
624 if (!strcmp(m->name, cname))
626 idx = m->type->details.function->idx;
627 break;
631 func->procstring_offset = *proc_offset;
632 gen_proxy(iface, func, idx, *proc_offset);
633 gen_stub(iface, func, cname, *proc_offset);
634 if (cas && is_interpreted_func( iface, func ))
636 needs_stub_thunks = 1;
637 gen_stub_thunk(iface, func, *proc_offset);
639 *proc_offset += get_size_procformatstring_func( iface, func );
643 count = count_methods(iface);
645 print_proxy( "static const unsigned short %s_FormatStringOffsetTable[] =\n", iface->name );
646 print_proxy( "{\n" );
647 indent++;
648 write_proxy_procformatstring_offsets( iface, 0 );
649 indent--;
650 print_proxy( "};\n\n" );
652 /* proxy info */
653 if (get_stub_mode() == MODE_Oif)
655 print_proxy( "static const MIDL_STUBLESS_PROXY_INFO %s_ProxyInfo =\n", iface->name );
656 print_proxy( "{\n" );
657 indent++;
658 print_proxy( "&Object_StubDesc,\n" );
659 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
660 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
661 print_proxy( "0,\n" );
662 print_proxy( "0,\n" );
663 print_proxy( "0\n" );
664 indent--;
665 print_proxy( "};\n\n" );
668 /* proxy vtable */
669 print_proxy( "static %sCINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n",
670 need_delegation_indirect(iface) ? "" : "const ", count, iface->name);
671 print_proxy( "{\n");
672 indent++;
673 print_proxy( "{\n");
674 indent++;
675 if (get_stub_mode() == MODE_Oif) print_proxy( "&%s_ProxyInfo,\n", iface->name );
676 print_proxy( "&IID_%s,\n", iface->name);
677 indent--;
678 print_proxy( "},\n");
679 print_proxy( "{\n");
680 indent++;
681 write_proxy_methods(iface, FALSE);
682 indent--;
683 print_proxy( "}\n");
684 indent--;
685 print_proxy( "};\n\n");
687 /* stub thunk table */
688 if (needs_stub_thunks)
690 print_proxy( "static const STUB_THUNK %s_StubThunkTable[] =\n", iface->name);
691 print_proxy( "{\n");
692 indent++;
693 write_thunk_methods( iface, 0 );
694 indent--;
695 print_proxy( "};\n\n");
698 /* server info */
699 print_proxy( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
700 print_proxy( "{\n" );
701 indent++;
702 print_proxy( "&Object_StubDesc,\n" );
703 print_proxy( "0,\n" );
704 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
705 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
706 if (needs_stub_thunks)
707 print_proxy( "&%s_StubThunkTable[-3],\n", iface->name );
708 else
709 print_proxy( "0,\n" );
710 print_proxy( "0,\n" );
711 print_proxy( "0,\n" );
712 print_proxy( "0\n" );
713 indent--;
714 print_proxy( "};\n\n" );
716 /* stub vtable */
717 if (needs_inline_stubs)
719 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
720 print_proxy( "{\n");
721 indent++;
722 write_stub_methods(iface, FALSE);
723 fprintf(proxy, "\n");
724 indent--;
725 fprintf(proxy, "};\n\n");
727 print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
728 need_delegation_indirect(iface) ? "" : "const ", iface->name);
729 print_proxy( "{\n");
730 indent++;
731 print_proxy( "{\n");
732 indent++;
733 print_proxy( "&IID_%s,\n", iface->name);
734 print_proxy( "&%s_ServerInfo,\n", iface->name );
735 print_proxy( "%d,\n", count);
736 if (needs_inline_stubs) print_proxy( "&%s_table[-3]\n", iface->name );
737 else print_proxy( "0\n" );
738 indent--;
739 print_proxy( "},\n");
740 print_proxy( "{\n");
741 indent++;
742 print_proxy( "CStdStubBuffer_%s\n", need_delegation_indirect(iface) ? "DELEGATING_METHODS" : "METHODS");
743 indent--;
744 print_proxy( "}\n");
745 indent--;
746 print_proxy( "};\n");
747 print_proxy( "\n");
750 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
752 const statement_t *stmt;
754 if (stmts)
755 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
757 if (stmt->type == STMT_LIBRARY)
759 if (does_any_iface(stmt->u.lib->stmts, pred))
760 return TRUE;
762 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
764 if (pred(stmt->u.type))
765 return TRUE;
769 return FALSE;
772 int need_proxy(const type_t *iface)
774 if (!is_object( iface )) return 0;
775 if (is_local( iface->attrs )) return 0;
776 if (is_attr( iface->attrs, ATTR_OLEAUTOMATION )) return 0;
777 if (is_attr( iface->attrs, ATTR_DISPINTERFACE )) return 0;
778 return 1;
781 int need_stub(const type_t *iface)
783 return !is_object(iface) && !is_local(iface->attrs);
786 int need_proxy_file(const statement_list_t *stmts)
788 return does_any_iface(stmts, need_proxy);
791 int need_inline_stubs(const type_t *iface)
793 const statement_t *stmt;
795 if (get_stub_mode() == MODE_Os) return 1;
797 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
799 const var_t *func = stmt->u.var;
800 if (is_local( func->attrs )) continue;
801 if (!is_interpreted_func( iface, func )) return 1;
803 return 0;
806 static int need_proxy_and_inline_stubs(const type_t *iface)
808 const statement_t *stmt;
810 if (!need_proxy( iface )) return 0;
811 if (get_stub_mode() == MODE_Os) return 1;
813 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
815 const var_t *func = stmt->u.var;
816 if (is_local( func->attrs )) continue;
817 if (!is_interpreted_func( iface, func )) return 1;
819 return 0;
822 int need_stub_files(const statement_list_t *stmts)
824 return does_any_iface(stmts, need_stub);
827 int need_inline_stubs_file(const statement_list_t *stmts)
829 return does_any_iface(stmts, need_inline_stubs);
832 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
834 const statement_t *stmt;
835 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
837 if (stmt->type == STMT_LIBRARY)
838 write_proxy_stmts(stmt->u.lib->stmts, proc_offset);
839 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
841 if (need_proxy(stmt->u.type))
842 write_proxy(stmt->u.type, proc_offset);
847 static int cmp_iid( const void *ptr1, const void *ptr2 )
849 const type_t * const *iface1 = ptr1;
850 const type_t * const *iface2 = ptr2;
851 const UUID *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
852 const UUID *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
853 return memcmp( uuid1, uuid2, sizeof(UUID) );
856 static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
858 const statement_t *stmt;
860 if (!stmts) return;
861 LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
863 if (stmt->type == STMT_LIBRARY)
864 build_iface_list(stmt->u.lib->stmts, ifaces, count);
865 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
867 type_t *iface = stmt->u.type;
868 if (type_iface_get_inherit(iface) && need_proxy(iface))
870 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
871 (*ifaces)[(*count)++] = iface;
877 static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
879 type_t **ifaces = NULL;
881 *count = 0;
882 build_iface_list( stmts, &ifaces, count );
883 qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
884 return ifaces;
887 static void write_proxy_routines(const statement_list_t *stmts)
889 int expr_eval_routines;
890 unsigned int proc_offset = 0;
891 char *file_id = proxy_token;
892 int i, count, have_baseiid = 0;
893 type_t **interfaces;
894 const type_t * delegate_to;
896 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
897 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ %u\n", get_stub_mode() == MODE_Oif ? 475 : 440);
898 print_proxy( "#endif\n");
899 print_proxy( "\n");
900 if (get_stub_mode() == MODE_Oif) print_proxy( "#define USE_STUBLESS_PROXY\n");
901 print_proxy( "#include \"rpcproxy.h\"\n");
902 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
903 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
904 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
905 print_proxy( "\n");
906 print_proxy( "#include \"%s\"\n", header_name);
907 print_proxy( "\n");
909 if (does_any_iface(stmts, need_proxy_and_inline_stubs))
911 write_exceptions( proxy );
912 print_proxy( "\n");
913 print_proxy( "struct __proxy_frame\n");
914 print_proxy( "{\n");
915 print_proxy( " __DECL_EXCEPTION_FRAME\n");
916 print_proxy( " MIDL_STUB_MESSAGE _StubMsg;\n");
917 print_proxy( " void *This;\n");
918 print_proxy( "};\n");
919 print_proxy( "\n");
920 print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
921 print_proxy( "{\n");
922 print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
923 print_proxy( "}\n");
924 print_proxy( "\n");
927 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
928 write_stubdescproto();
929 write_proxy_stmts(stmts, &proc_offset);
931 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
932 if (expr_eval_routines)
933 write_expr_eval_routine_list(proxy, proxy_token);
934 write_user_quad_list(proxy);
935 write_stubdesc(expr_eval_routines);
937 print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
938 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
939 print_proxy( "#endif\n");
940 print_proxy( "\n");
941 write_procformatstring(proxy, stmts, need_proxy);
942 write_typeformatstring(proxy, stmts, need_proxy);
944 interfaces = sort_interfaces(stmts, &count);
945 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
946 fprintf(proxy, "{\n");
947 for (i = 0; i < count; i++)
948 fprintf(proxy, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
949 fprintf(proxy, " 0\n");
950 fprintf(proxy, "};\n");
951 fprintf(proxy, "\n");
953 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
954 fprintf(proxy, "{\n");
955 for (i = 0; i < count; i++)
956 fprintf(proxy, " &_%sStubVtbl,\n", interfaces[i]->name);
957 fprintf(proxy, " 0\n");
958 fprintf(proxy, "};\n");
959 fprintf(proxy, "\n");
961 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
962 fprintf(proxy, "{\n");
963 for (i = 0; i < count; i++)
964 fprintf(proxy, " \"%s\",\n", interfaces[i]->name);
965 fprintf(proxy, " 0\n");
966 fprintf(proxy, "};\n");
967 fprintf(proxy, "\n");
969 for (i = 0; i < count; i++)
970 if ((have_baseiid = get_delegation_indirect( interfaces[i], NULL ))) break;
972 if (have_baseiid)
974 fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
975 fprintf(proxy, "{\n");
976 for (i = 0; i < count; i++)
978 if (get_delegation_indirect(interfaces[i], &delegate_to))
979 fprintf( proxy, " &IID_%s, /* %s */\n", delegate_to->name, interfaces[i]->name );
980 else
981 fprintf( proxy, " 0,\n" );
983 fprintf(proxy, " 0\n");
984 fprintf(proxy, "};\n");
985 fprintf(proxy, "\n");
988 fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
989 fprintf(proxy, "{\n");
990 fprintf(proxy, " int low = 0, high = %d;\n", count - 1);
991 fprintf(proxy, "\n");
992 fprintf(proxy, " while (low <= high)\n");
993 fprintf(proxy, " {\n");
994 fprintf(proxy, " int pos = (low + high) / 2;\n");
995 fprintf(proxy, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
996 fprintf(proxy, " if (!res) { *pIndex = pos; return 1; }\n");
997 fprintf(proxy, " if (res > 0) low = pos + 1;\n");
998 fprintf(proxy, " else high = pos - 1;\n");
999 fprintf(proxy, " }\n");
1000 fprintf(proxy, " return 0;\n");
1001 fprintf(proxy, "}\n");
1002 fprintf(proxy, "\n");
1004 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo DECLSPEC_HIDDEN =\n", file_id);
1005 fprintf(proxy, "{\n");
1006 fprintf(proxy, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
1007 fprintf(proxy, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
1008 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
1009 if (have_baseiid) fprintf(proxy, " _%s_BaseIIDList,\n", file_id);
1010 else fprintf(proxy, " 0,\n");
1011 fprintf(proxy, " _%s_IID_Lookup,\n", file_id);
1012 fprintf(proxy, " %d,\n", count);
1013 fprintf(proxy, " %d,\n", get_stub_mode() == MODE_Oif ? 2 : 1);
1014 fprintf(proxy, " 0,\n");
1015 fprintf(proxy, " 0,\n");
1016 fprintf(proxy, " 0,\n");
1017 fprintf(proxy, " 0\n");
1018 fprintf(proxy, "};\n");
1021 void write_proxies(const statement_list_t *stmts)
1023 if (!do_proxies) return;
1024 if (do_everything && !need_proxy_file(stmts)) return;
1026 init_proxy(stmts);
1027 if(!proxy) return;
1029 if (do_win32 && do_win64)
1031 fprintf(proxy, "\n#ifndef _WIN64\n\n");
1032 pointer_size = 4;
1033 write_proxy_routines( stmts );
1034 fprintf(proxy, "\n#else /* _WIN64 */\n\n");
1035 pointer_size = 8;
1036 write_proxy_routines( stmts );
1037 fprintf(proxy, "\n#endif /* _WIN64 */\n");
1039 else if (do_win32)
1041 pointer_size = 4;
1042 write_proxy_routines( stmts );
1044 else if (do_win64)
1046 pointer_size = 8;
1047 write_proxy_routines( stmts );
1050 fclose(proxy);