widl: Use the local function as proxy entry for callas interpreted functions.
[wine/multimedia.git] / tools / widl / proxy.c
blob32a5054d41b1efac32b4c6e2cebb24eb6c146708
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_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--;
123 int is_var_ptr(const var_t *v)
125 return is_ptr(v->type);
128 int cant_be_null(const var_t *v)
130 switch (typegen_detect_type(v->type, v->attrs, TDT_IGNORE_STRINGS))
132 case TGT_ARRAY:
133 case TGT_POINTER:
134 return (get_pointer_fc(v->type, v->attrs, TRUE) == RPC_FC_RP);
135 case TGT_CTXT_HANDLE_POINTER:
136 return TRUE;
137 default:
138 return 0;
143 static int need_delegation(const type_t *iface)
145 return type_iface_get_inherit(iface) &&
146 type_iface_get_inherit(type_iface_get_inherit(iface)) &&
147 type_iface_get_inherit(iface)->ignore;
150 static int get_delegation_indirect(const type_t *iface, const type_t ** delegate_to)
152 const type_t * cur_iface;
153 for (cur_iface = iface; cur_iface != NULL; cur_iface = type_iface_get_inherit(cur_iface))
154 if (need_delegation(cur_iface))
156 if(delegate_to)
157 *delegate_to = type_iface_get_inherit(cur_iface);
158 return 1;
160 return 0;
163 static int need_delegation_indirect(const type_t *iface)
165 return get_delegation_indirect(iface, NULL);
168 static void proxy_check_pointers( const var_list_t *args )
170 const var_t *arg;
172 if (!args) return;
173 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
175 if (is_var_ptr(arg) && cant_be_null(arg)) {
176 print_proxy( "if(!%s)\n", arg->name );
177 indent++;
178 print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
179 indent--;
184 static void free_variable( const var_t *arg, const char *local_var_prefix )
186 unsigned int type_offset = arg->type->typestring_offset;
187 expr_t *iid;
188 type_t *type = arg->type;
189 expr_t *size = get_size_is_expr(type, arg->name);
191 if (size)
193 print_proxy( "__frame->_StubMsg.MaxCount = " );
194 write_expr(proxy, size, 0, 1, NULL, NULL, local_var_prefix);
195 fprintf(proxy, ";\n\n");
196 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
197 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
198 fprintf(proxy, "(void*)%s );\n", arg->name );
199 return;
202 switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
204 case TGT_ENUM:
205 case TGT_BASIC:
206 break;
208 case TGT_STRUCT:
209 if (get_struct_fc(type) != RPC_FC_STRUCT)
210 print_proxy("/* FIXME: %s code for %s struct type 0x%x missing */\n", __FUNCTION__, arg->name, get_struct_fc(type) );
211 break;
213 case TGT_IFACE_POINTER:
214 iid = get_attrp( arg->attrs, ATTR_IIDIS );
215 if( iid )
217 print_proxy( "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
218 write_expr(proxy, iid, 1, 1, NULL, NULL, local_var_prefix);
219 print_proxy( ";\n\n" );
221 /* fall through */
222 case TGT_POINTER:
223 if (get_pointer_fc(type, arg->attrs, TRUE) == RPC_FC_FP)
225 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
226 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
227 fprintf(proxy, "(void*)%s );\n", arg->name );
229 else
230 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
231 break;
233 default:
234 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
238 static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
240 const var_t *arg;
242 if (!args) return;
243 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
244 if (is_attr(arg->attrs, ATTR_OUT))
246 free_variable( arg, local_var_prefix );
247 fprintf(proxy, "\n");
251 static void gen_proxy(type_t *iface, const var_t *func, int idx,
252 unsigned int proc_offset)
254 type_t *rettype = type_function_get_rettype(func->type);
255 int has_ret = !is_void(rettype);
256 int has_full_pointer = is_full_pointer_function(func);
257 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
258 const var_list_t *args = type_get_function_args(func->type);
259 if (!callconv) callconv = "STDMETHODCALLTYPE";
261 indent = 0;
262 if (is_interpreted_func( iface, func ))
264 if (get_stub_mode() == MODE_Oif && !is_callas( func->attrs )) return;
265 write_type_decl_left(proxy, type_function_get_rettype(func->type));
266 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
267 write_args(proxy, args, iface->name, 1, TRUE);
268 print_proxy( ")\n");
269 write_client_call_routine( proxy, iface, func, "Object", proc_offset );
270 return;
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");
282 write_type_decl_left(proxy, rettype);
283 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
284 write_args(proxy, args, 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, rettype);
293 print_proxy( " _RetVal;\n");
295 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
296 if (has_ret) {
297 if (decl_indirect(rettype))
298 print_proxy("void *_p_%s = &%s;\n",
299 "_RetVal", "_RetVal");
301 print_proxy( "\n");
303 print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(func) );
304 print_proxy( "__frame->This = This;\n" );
306 if (has_full_pointer)
307 write_full_pointer_init(proxy, indent, func, FALSE);
309 /* FIXME: trace */
310 clear_output_vars( type_get_function_args(func->type) );
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) );
318 print_proxy( "RpcTryFinally\n" );
319 print_proxy( "{\n" );
320 indent++;
322 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
324 print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
326 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_MARSHAL);
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" );
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");
339 write_remoting_arguments(proxy, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
341 if (has_ret)
343 if (decl_indirect(rettype))
344 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
345 else if (is_ptr(rettype) || is_array(rettype))
346 print_proxy("%s = 0;\n", "_RetVal");
347 write_remoting_arguments(proxy, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
350 indent--;
351 print_proxy( "}\n");
352 print_proxy( "RpcFinally\n" );
353 print_proxy( "{\n" );
354 indent++;
355 print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(func) );
356 indent--;
357 print_proxy( "}\n");
358 print_proxy( "RpcEndFinally\n" );
359 indent--;
360 print_proxy( "}\n" );
361 print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
362 print_proxy( "{\n" );
363 if (has_ret) {
364 indent++;
365 proxy_free_variables( type_get_function_args(func->type), "" );
366 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
367 indent--;
369 print_proxy( "}\n" );
370 print_proxy( "RpcEndExcept\n" );
372 if (has_ret) {
373 print_proxy( "return _RetVal;\n" );
375 indent--;
376 print_proxy( "}\n");
377 print_proxy( "\n");
380 static void gen_stub(type_t *iface, const var_t *func, const char *cas,
381 unsigned int proc_offset)
383 const var_t *arg;
384 int has_ret = !is_void(type_function_get_rettype(func->type));
385 int has_full_pointer = is_full_pointer_function(func);
387 if (is_interpreted_func( iface, func )) return;
389 indent = 0;
390 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(func));
391 indent++;
392 print_proxy( "__DECL_EXCEPTION_FRAME\n" );
393 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
394 print_proxy( "%s * _This;\n", iface->name );
395 declare_stub_args( proxy, indent, func );
396 indent--;
397 print_proxy( "};\n\n" );
399 print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(func) );
400 print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(func) );
401 indent++;
402 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
403 if (has_full_pointer)
404 write_full_pointer_free(proxy, indent, func);
405 indent--;
406 print_proxy( "}\n\n" );
408 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
409 indent++;
410 print_proxy( "IRpcStubBuffer* This,\n");
411 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
412 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
413 print_proxy( "DWORD* _pdwStubPhase)\n");
414 indent--;
415 print_proxy( "{\n");
416 indent++;
417 print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
418 iface->name, get_name(func) );
420 print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
422 /* FIXME: trace */
424 print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
425 fprintf(proxy, "\n");
426 print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(func) );
428 write_parameters_init(proxy, indent, func, "__frame->");
430 print_proxy("RpcTryFinally\n");
431 print_proxy("{\n");
432 indent++;
433 if (has_full_pointer)
434 write_full_pointer_init(proxy, indent, func, TRUE);
435 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
436 indent++;
437 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
438 indent--;
439 fprintf(proxy, "\n");
441 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
442 fprintf(proxy, "\n");
444 assign_stub_out_args( proxy, indent, func, "__frame->" );
446 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
447 fprintf(proxy, "\n");
448 print_proxy( "%s", has_ret ? "__frame->_RetVal = " : "" );
449 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
450 else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(func));
451 fprintf(proxy, "(__frame->_This");
453 if (type_get_function_args(func->type))
455 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
456 fprintf(proxy, ", %s__frame->%s", is_array(arg->type) && !type_array_is_decl_as_ptr(arg->type) ? "*" :"" , arg->name);
458 fprintf(proxy, ");\n");
459 fprintf(proxy, "\n");
460 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
461 fprintf(proxy, "\n");
463 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
465 if (!is_void(type_function_get_rettype(func->type)))
466 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
468 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
470 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
471 fprintf(proxy, "\n");
473 /* marshall the return value */
474 if (!is_void(type_function_get_rettype(func->type)))
475 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
477 indent--;
478 print_proxy("}\n");
479 print_proxy("RpcFinally\n");
480 print_proxy("{\n");
481 indent++;
482 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(func) );
483 indent--;
484 print_proxy("}\n");
485 print_proxy("RpcEndFinally\n");
487 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
488 indent--;
490 print_proxy("}\n");
491 print_proxy("\n");
494 static void gen_stub_thunk( type_t *iface, const var_t *func, unsigned int proc_offset )
496 int has_ret = !is_void( type_function_get_rettype( func->type ));
497 const var_t *arg, *callas = is_callas( func->attrs );
498 const var_list_t *args = type_get_function_args( func->type );
500 indent = 0;
501 print_proxy( "void __RPC_API %s_%s_Thunk( PMIDL_STUB_MESSAGE pStubMsg )\n",
502 iface->name, get_name(func) );
503 print_proxy( "{\n");
504 indent++;
505 write_func_param_struct( proxy, iface, func->type,
506 "*pParamStruct = (struct _PARAM_STRUCT *)pStubMsg->StackTop", has_ret );
507 print_proxy( "%s%s_%s_Stub( pParamStruct->This",
508 has_ret ? "pParamStruct->_RetVal = " : "", iface->name, callas->name );
509 indent++;
510 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
512 fprintf( proxy, ",\n%*spParamStruct->%s", 4 * indent, "", arg->name );
514 fprintf( proxy, " );\n" );
515 indent--;
516 indent--;
517 print_proxy( "}\n\n");
520 int count_methods(const type_t *iface)
522 const statement_t *stmt;
523 int count = 0;
525 if (type_iface_get_inherit(iface))
526 count = count_methods(type_iface_get_inherit(iface));
528 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
529 const var_t *func = stmt->u.var;
530 if (!is_callas(func->attrs)) count++;
532 return count;
535 static const statement_t * get_callas_source(const type_t * iface, const var_t * def)
537 const statement_t * source;
538 STATEMENTS_FOR_EACH_FUNC( source, type_iface_get_stmts(iface)) {
539 const var_t * cas = is_callas(source->u.var->attrs );
540 if (cas && !strcmp(def->name, cas->name))
541 return source;
543 return NULL;
546 static int write_proxy_methods(type_t *iface, int skip)
548 const statement_t *stmt;
549 int i = 0;
551 if (type_iface_get_inherit(iface))
552 i = write_proxy_methods(type_iface_get_inherit(iface),
553 need_delegation(iface));
554 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
555 const var_t *func = stmt->u.var;
556 if (!is_callas(func->attrs)) {
557 if (skip || (is_local(func->attrs) && !get_callas_source(iface, func)))
558 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func));
559 else if (is_interpreted_func( iface, func ) &&
560 !is_local( func->attrs ) &&
561 type_iface_get_inherit(iface))
562 print_proxy( "(void *)-1, /* %s::%s */\n", iface->name, get_name(func));
563 else
564 print_proxy( "%s_%s_Proxy,\n", iface->name, get_name(func));
565 i++;
568 return i;
571 static int write_stub_methods(type_t *iface, int skip)
573 const statement_t *stmt;
574 int i = 0;
576 if (type_iface_get_inherit(iface))
577 i = write_stub_methods(type_iface_get_inherit(iface), need_delegation(iface));
578 else
579 return i; /* skip IUnknown */
581 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
582 const var_t *func = stmt->u.var;
583 if (!is_callas(func->attrs)) {
584 int missing = 0;
585 const char * fname = get_name(func);
586 if(is_local(func->attrs)) {
587 const statement_t * callas_source = get_callas_source(iface, func);
588 if(!callas_source)
589 missing = 1;
590 else
591 fname = get_name(callas_source->u.var);
593 if (i) fprintf(proxy,",\n");
594 if (skip || missing) print_proxy("STUB_FORWARDING_FUNCTION");
595 else if (is_interpreted_func( iface, func ))
596 print_proxy( "(PRPC_STUB_FUNCTION)%s", get_stub_mode() == MODE_Oif ? "NdrStubCall2" : "NdrStubCall" );
597 else print_proxy( "%s_%s_Stub", iface->name, fname);
598 i++;
601 return i;
604 static void write_thunk_methods( type_t *iface )
606 const statement_t *stmt;
607 int i = 0;
609 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
611 var_t *func = stmt->u.var;
612 const var_t *cas = is_callas(func->attrs);
614 if (is_local( func->attrs )) continue;
615 if (i) fprintf(proxy, ",\n");
616 if (cas && is_interpreted_func( iface, func ))
617 print_proxy( "%s_%s_Thunk", iface->name, func->name );
618 else
619 print_proxy( "0" );
620 i++;
622 fputc( '\n', proxy );
625 static void write_proxy(type_t *iface, unsigned int *proc_offset)
627 int count;
628 const statement_t *stmt;
629 int first_func = 1;
630 int needs_stub_thunks = 0;
631 int needs_inline_stubs = need_inline_stubs( iface ) || need_delegation( iface );
633 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
634 var_t *func = stmt->u.var;
635 if (first_func) {
636 fprintf(proxy, "/*****************************************************************************\n");
637 fprintf(proxy, " * %s interface\n", iface->name);
638 fprintf(proxy, " */\n");
639 first_func = 0;
641 if (!is_local(func->attrs)) {
642 const var_t *cas = is_callas(func->attrs);
643 const char *cname = cas ? cas->name : NULL;
644 int idx = func->type->details.function->idx;
645 if (cname) {
646 const statement_t *stmt2;
647 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface)) {
648 const var_t *m = stmt2->u.var;
649 if (!strcmp(m->name, cname))
651 idx = m->type->details.function->idx;
652 break;
656 func->procstring_offset = *proc_offset;
657 gen_proxy(iface, func, idx, *proc_offset);
658 gen_stub(iface, func, cname, *proc_offset);
659 if (cas && is_interpreted_func( iface, func ))
661 needs_stub_thunks = 1;
662 gen_stub_thunk(iface, func, *proc_offset);
664 *proc_offset += get_size_procformatstring_func( iface, func );
668 count = count_methods(iface);
670 write_procformatstring_offsets( proxy, iface );
672 /* proxy info */
673 if (get_stub_mode() == MODE_Oif)
675 print_proxy( "static const MIDL_STUBLESS_PROXY_INFO %s_ProxyInfo =\n", iface->name );
676 print_proxy( "{\n" );
677 indent++;
678 print_proxy( "&Object_StubDesc,\n" );
679 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
680 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
681 print_proxy( "0,\n" );
682 print_proxy( "0,\n" );
683 print_proxy( "0\n" );
684 indent--;
685 print_proxy( "};\n\n" );
688 /* proxy vtable */
689 print_proxy( "static %sCINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n",
690 need_delegation_indirect(iface) ? "" : "const ", count, iface->name);
691 print_proxy( "{\n");
692 indent++;
693 print_proxy( "{\n");
694 indent++;
695 if (get_stub_mode() == MODE_Oif) print_proxy( "&%s_ProxyInfo,\n", iface->name );
696 print_proxy( "&IID_%s,\n", iface->name);
697 indent--;
698 print_proxy( "},\n");
699 print_proxy( "{\n");
700 indent++;
701 write_proxy_methods(iface, FALSE);
702 indent--;
703 print_proxy( "}\n");
704 indent--;
705 print_proxy( "};\n\n");
707 /* stub thunk table */
708 if (needs_stub_thunks)
710 print_proxy( "static const STUB_THUNK %s_StubThunkTable[] =\n", iface->name);
711 print_proxy( "{\n");
712 indent++;
713 write_thunk_methods( iface );
714 indent--;
715 print_proxy( "};\n\n");
718 /* server info */
719 print_proxy( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
720 print_proxy( "{\n" );
721 indent++;
722 print_proxy( "&Object_StubDesc,\n" );
723 print_proxy( "0,\n" );
724 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
725 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
726 if (needs_stub_thunks)
727 print_proxy( "&%s_StubThunkTable[-3],\n", iface->name );
728 else
729 print_proxy( "0,\n" );
730 print_proxy( "0,\n" );
731 print_proxy( "0,\n" );
732 print_proxy( "0\n" );
733 indent--;
734 print_proxy( "};\n\n" );
736 /* stub vtable */
737 if (needs_inline_stubs)
739 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
740 print_proxy( "{\n");
741 indent++;
742 write_stub_methods(iface, FALSE);
743 fprintf(proxy, "\n");
744 indent--;
745 fprintf(proxy, "};\n\n");
747 print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
748 need_delegation_indirect(iface) ? "" : "const ", iface->name);
749 print_proxy( "{\n");
750 indent++;
751 print_proxy( "{\n");
752 indent++;
753 print_proxy( "&IID_%s,\n", iface->name);
754 print_proxy( "&%s_ServerInfo,\n", iface->name );
755 print_proxy( "%d,\n", count);
756 if (needs_inline_stubs) print_proxy( "&%s_table[-3]\n", iface->name );
757 else print_proxy( "0\n" );
758 indent--;
759 print_proxy( "},\n");
760 print_proxy( "{\n");
761 indent++;
762 print_proxy( "CStdStubBuffer_%s\n", need_delegation_indirect(iface) ? "DELEGATING_METHODS" : "METHODS");
763 indent--;
764 print_proxy( "}\n");
765 indent--;
766 print_proxy( "};\n");
767 print_proxy( "\n");
770 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
772 const statement_t *stmt;
774 if (stmts)
775 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
777 if (stmt->type == STMT_LIBRARY)
779 if (does_any_iface(stmt->u.lib->stmts, pred))
780 return TRUE;
782 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
784 if (pred(stmt->u.type))
785 return TRUE;
789 return FALSE;
792 int need_proxy(const type_t *iface)
794 if (!is_object( iface )) return 0;
795 if (is_local( iface->attrs )) return 0;
796 if (is_attr( iface->attrs, ATTR_OLEAUTOMATION )) return 0;
797 if (is_attr( iface->attrs, ATTR_DISPINTERFACE )) return 0;
798 return 1;
801 int need_stub(const type_t *iface)
803 return !is_object(iface) && !is_local(iface->attrs);
806 int need_proxy_file(const statement_list_t *stmts)
808 return does_any_iface(stmts, need_proxy);
811 int need_inline_stubs(const type_t *iface)
813 const statement_t *stmt;
815 if (get_stub_mode() == MODE_Os) return 1;
817 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
819 const var_t *func = stmt->u.var;
820 if (is_local( func->attrs )) continue;
821 if (!is_interpreted_func( iface, func )) return 1;
823 return 0;
826 static int need_proxy_and_inline_stubs(const type_t *iface)
828 const statement_t *stmt;
830 if (!need_proxy( iface )) return 0;
831 if (get_stub_mode() == MODE_Os) return 1;
833 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
835 const var_t *func = stmt->u.var;
836 if (is_local( func->attrs )) continue;
837 if (!is_interpreted_func( iface, func )) return 1;
839 return 0;
842 int need_stub_files(const statement_list_t *stmts)
844 return does_any_iface(stmts, need_stub);
847 int need_inline_stubs_file(const statement_list_t *stmts)
849 return does_any_iface(stmts, need_inline_stubs);
852 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
854 const statement_t *stmt;
855 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
857 if (stmt->type == STMT_LIBRARY)
858 write_proxy_stmts(stmt->u.lib->stmts, proc_offset);
859 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
861 if (need_proxy(stmt->u.type))
862 write_proxy(stmt->u.type, proc_offset);
867 static int cmp_iid( const void *ptr1, const void *ptr2 )
869 const type_t * const *iface1 = ptr1;
870 const type_t * const *iface2 = ptr2;
871 const UUID *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
872 const UUID *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
873 return memcmp( uuid1, uuid2, sizeof(UUID) );
876 static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
878 const statement_t *stmt;
880 if (!stmts) return;
881 LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
883 if (stmt->type == STMT_LIBRARY)
884 build_iface_list(stmt->u.lib->stmts, ifaces, count);
885 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
887 type_t *iface = stmt->u.type;
888 if (type_iface_get_inherit(iface) && need_proxy(iface))
890 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
891 (*ifaces)[(*count)++] = iface;
897 static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
899 type_t **ifaces = NULL;
901 *count = 0;
902 build_iface_list( stmts, &ifaces, count );
903 qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
904 return ifaces;
907 static void write_proxy_routines(const statement_list_t *stmts)
909 int expr_eval_routines;
910 unsigned int proc_offset = 0;
911 char *file_id = proxy_token;
912 int i, count, have_baseiid;
913 type_t **interfaces;
914 const type_t * delegate_to;
916 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
917 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ %u\n", get_stub_mode() == MODE_Oif ? 475 : 440);
918 print_proxy( "#endif\n");
919 print_proxy( "\n");
920 if (get_stub_mode() == MODE_Oif) print_proxy( "#define USE_STUBLESS_PROXY\n");
921 print_proxy( "#include \"rpcproxy.h\"\n");
922 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
923 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
924 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
925 print_proxy( "\n");
926 print_proxy( "#include \"%s\"\n", header_name);
927 print_proxy( "\n");
929 if (does_any_iface(stmts, need_proxy_and_inline_stubs))
931 write_exceptions( proxy );
932 print_proxy( "\n");
933 print_proxy( "struct __proxy_frame\n");
934 print_proxy( "{\n");
935 print_proxy( " __DECL_EXCEPTION_FRAME\n");
936 print_proxy( " MIDL_STUB_MESSAGE _StubMsg;\n");
937 print_proxy( " void *This;\n");
938 print_proxy( "};\n");
939 print_proxy( "\n");
940 print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
941 print_proxy( "{\n");
942 print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
943 print_proxy( "}\n");
944 print_proxy( "\n");
947 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
948 write_stubdescproto();
949 write_proxy_stmts(stmts, &proc_offset);
951 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
952 if (expr_eval_routines)
953 write_expr_eval_routine_list(proxy, proxy_token);
954 write_user_quad_list(proxy);
955 write_stubdesc(expr_eval_routines);
957 print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
958 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
959 print_proxy( "#endif\n");
960 print_proxy( "\n");
961 write_procformatstring(proxy, stmts, need_proxy);
962 write_typeformatstring(proxy, stmts, need_proxy);
964 interfaces = sort_interfaces(stmts, &count);
965 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
966 fprintf(proxy, "{\n");
967 for (i = 0; i < count; i++)
968 fprintf(proxy, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
969 fprintf(proxy, " 0\n");
970 fprintf(proxy, "};\n");
971 fprintf(proxy, "\n");
973 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
974 fprintf(proxy, "{\n");
975 for (i = 0; i < count; i++)
976 fprintf(proxy, " &_%sStubVtbl,\n", interfaces[i]->name);
977 fprintf(proxy, " 0\n");
978 fprintf(proxy, "};\n");
979 fprintf(proxy, "\n");
981 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
982 fprintf(proxy, "{\n");
983 for (i = 0; i < count; i++)
984 fprintf(proxy, " \"%s\",\n", interfaces[i]->name);
985 fprintf(proxy, " 0\n");
986 fprintf(proxy, "};\n");
987 fprintf(proxy, "\n");
989 if ((have_baseiid = does_any_iface(stmts, need_delegation_indirect)))
991 fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
992 fprintf(proxy, "{\n");
993 for (i = 0; i < count; i++)
995 if (get_delegation_indirect(interfaces[i], &delegate_to))
996 fprintf( proxy, " &IID_%s, /* %s */\n", delegate_to->name, interfaces[i]->name );
997 else
998 fprintf( proxy, " 0,\n" );
1000 fprintf(proxy, " 0\n");
1001 fprintf(proxy, "};\n");
1002 fprintf(proxy, "\n");
1005 fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
1006 fprintf(proxy, "{\n");
1007 fprintf(proxy, " int low = 0, high = %d;\n", count - 1);
1008 fprintf(proxy, "\n");
1009 fprintf(proxy, " while (low <= high)\n");
1010 fprintf(proxy, " {\n");
1011 fprintf(proxy, " int pos = (low + high) / 2;\n");
1012 fprintf(proxy, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
1013 fprintf(proxy, " if (!res) { *pIndex = pos; return 1; }\n");
1014 fprintf(proxy, " if (res > 0) low = pos + 1;\n");
1015 fprintf(proxy, " else high = pos - 1;\n");
1016 fprintf(proxy, " }\n");
1017 fprintf(proxy, " return 0;\n");
1018 fprintf(proxy, "}\n");
1019 fprintf(proxy, "\n");
1021 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo DECLSPEC_HIDDEN =\n", file_id);
1022 fprintf(proxy, "{\n");
1023 fprintf(proxy, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
1024 fprintf(proxy, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
1025 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
1026 if (have_baseiid) fprintf(proxy, " _%s_BaseIIDList,\n", file_id);
1027 else fprintf(proxy, " 0,\n");
1028 fprintf(proxy, " _%s_IID_Lookup,\n", file_id);
1029 fprintf(proxy, " %d,\n", count);
1030 fprintf(proxy, " %d,\n", get_stub_mode() == MODE_Oif ? 2 : 1);
1031 fprintf(proxy, " 0,\n");
1032 fprintf(proxy, " 0,\n");
1033 fprintf(proxy, " 0,\n");
1034 fprintf(proxy, " 0\n");
1035 fprintf(proxy, "};\n");
1038 void write_proxies(const statement_list_t *stmts)
1040 if (!do_proxies) return;
1041 if (do_everything && !need_proxy_file(stmts)) return;
1043 init_proxy(stmts);
1044 if(!proxy) return;
1046 if (do_win32 && do_win64)
1048 fprintf(proxy, "\n#ifndef _WIN64\n\n");
1049 pointer_size = 4;
1050 write_proxy_routines( stmts );
1051 fprintf(proxy, "\n#else /* _WIN64 */\n\n");
1052 pointer_size = 8;
1053 write_proxy_routines( stmts );
1054 fprintf(proxy, "\n#endif /* _WIN64 */\n");
1056 else if (do_win32)
1058 pointer_size = 4;
1059 write_proxy_routines( stmts );
1061 else if (do_win64)
1063 pointer_size = 8;
1064 write_proxy_routines( stmts );
1067 fclose(proxy);