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
23 #include "wine/port.h"
40 #define END_OF_LIST(list) \
43 while (NEXT_LINK(list)) \
44 list = NEXT_LINK(list); \
49 static int indent
= 0;
51 /* FIXME: support generation of stubless proxies */
53 static void print_proxy( const char *format
, ... ) __attribute__((format (printf
, 1, 2)));
54 static void print_proxy( const char *format
, ... )
57 va_start( va
, format
);
58 print( proxy
, indent
, format
, va
);
62 static void write_stubdescproto(void)
64 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
68 static void write_stubdesc(int expr_eval_routines
)
70 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc =\n{\n");
73 print_proxy( "NdrOleAllocate,\n");
74 print_proxy( "NdrOleFree,\n");
75 print_proxy( "{0}, 0, 0, %s, 0,\n", expr_eval_routines
? "ExprEvalRoutines" : "0");
76 print_proxy( "__MIDL_TypeFormatString.Format,\n");
77 print_proxy( "1, /* -error bounds_check flag */\n");
78 print_proxy( "0x10001, /* Ndr library version */\n");
80 print_proxy( "0x50100a4, /* MIDL Version 5.1.164 */\n");
82 print_proxy("%s,\n", list_empty(&user_type_list
) ? "0" : "UserMarshalRoutines");
83 print_proxy( "0, /* notify & notify_flag routine table */\n");
84 print_proxy( "1, /* Flags */\n");
85 print_proxy( "0, /* Reserved3 */\n");
86 print_proxy( "0, /* Reserved4 */\n");
87 print_proxy( "0 /* Reserved5 */\n");
93 static void init_proxy(const statement_list_t
*stmts
)
96 if(!(proxy
= fopen(proxy_name
, "w")))
97 error("Could not open %s for output\n", proxy_name
);
98 print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION
, input_name
);
100 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
101 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ 440\n");
102 print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
104 print_proxy( "#define __midl_proxy\n");
105 print_proxy( "#include \"objbase.h\"\n");
106 print_proxy( "#include \"rpcproxy.h\"\n");
107 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
108 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
109 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
111 print_proxy( "#include \"%s\"\n", header_name
);
113 print_proxy( "#ifndef DECLSPEC_HIDDEN\n");
114 print_proxy( "#define DECLSPEC_HIDDEN\n");
115 print_proxy( "#endif\n");
117 write_exceptions( proxy
);
119 print_proxy( "struct __proxy_frame\n");
121 print_proxy( " __DECL_EXCEPTION_FRAME\n");
122 print_proxy( " MIDL_STUB_MESSAGE _StubMsg;\n");
123 print_proxy( " void *This;\n");
124 print_proxy( "};\n");
126 print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
128 print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
133 static void clear_output_vars( const var_list_t
*args
)
138 LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
140 if (is_attr(arg
->attrs
, ATTR_OUT
) && !is_attr(arg
->attrs
, ATTR_IN
)) {
141 print_proxy( "if(%s)\n", arg
->name
);
143 print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg
->name
, arg
->name
);
149 int is_var_ptr(const var_t
*v
)
151 return is_ptr(v
->type
);
154 int cant_be_null(const var_t
*v
)
156 switch (typegen_detect_type(v
->type
, v
->attrs
, TDT_IGNORE_STRINGS
))
159 /* FIXME: work out pointer type */
162 return (get_pointer_fc(v
->type
) == RPC_FC_RP
);
163 case TGT_CTXT_HANDLE_POINTER
:
171 static int need_delegation(const type_t
*iface
)
173 return type_iface_get_inherit(iface
) &&
174 type_iface_get_inherit(type_iface_get_inherit(iface
)) &&
175 type_iface_get_inherit(iface
)->ignore
;
178 static int get_delegation_indirect(const type_t
*iface
, const type_t
** delegate_to
)
180 const type_t
* cur_iface
;
181 for (cur_iface
= iface
; cur_iface
!= NULL
; cur_iface
= type_iface_get_inherit(cur_iface
))
182 if (need_delegation(cur_iface
))
185 *delegate_to
= type_iface_get_inherit(cur_iface
);
191 static int need_delegation_indirect(const type_t
*iface
)
193 return get_delegation_indirect(iface
, NULL
);
196 static void proxy_check_pointers( const var_list_t
*args
)
201 LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
203 if (is_var_ptr(arg
) && cant_be_null(arg
)) {
204 print_proxy( "if(!%s)\n", arg
->name
);
206 print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
212 static void free_variable( const var_t
*arg
, const char *local_var_prefix
)
214 unsigned int type_offset
= arg
->type
->typestring_offset
;
216 type_t
*type
= arg
->type
;
217 expr_t
*size
= get_size_is_expr(type
, arg
->name
);
221 print_proxy( "__frame->_StubMsg.MaxCount = " );
222 write_expr(proxy
, size
, 0, 1, NULL
, NULL
, local_var_prefix
);
223 fprintf(proxy
, ";\n\n");
224 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
225 fprintf(proxy
, "&__MIDL_TypeFormatString.Format[%u], ", type_offset
);
226 fprintf(proxy
, "(void*)%s );\n", arg
->name
);
230 switch (typegen_detect_type(type
, arg
->attrs
, TDT_IGNORE_STRINGS
))
237 if (get_struct_fc(type
) != RPC_FC_STRUCT
)
238 print_proxy("/* FIXME: %s code for %s struct type 0x%x missing */\n", __FUNCTION__
, arg
->name
, get_struct_fc(type
) );
241 case TGT_IFACE_POINTER
:
242 iid
= get_attrp( arg
->attrs
, ATTR_IIDIS
);
245 print_proxy( "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
246 write_expr(proxy
, iid
, 1, 1, NULL
, NULL
, local_var_prefix
);
247 print_proxy( ";\n\n" );
251 if (get_pointer_fc(type
) == RPC_FC_FP
)
253 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
254 fprintf(proxy
, "&__MIDL_TypeFormatString.Format[%u], ", type_offset
);
255 fprintf(proxy
, "(void*)%s );\n", arg
->name
);
258 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__
, arg
->name
, type_get_type(type
) );
262 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__
, arg
->name
, type_get_type(type
) );
266 static void proxy_free_variables( var_list_t
*args
, const char *local_var_prefix
)
271 LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
272 if (is_attr(arg
->attrs
, ATTR_OUT
))
274 free_variable( arg
, local_var_prefix
);
275 fprintf(proxy
, "\n");
279 static void gen_proxy(type_t
*iface
, const var_t
*func
, int idx
,
280 unsigned int proc_offset
)
282 int has_ret
= !is_void(type_function_get_rettype(func
->type
));
283 int has_full_pointer
= is_full_pointer_function(func
);
284 const char *callconv
= get_attrp(func
->type
->attrs
, ATTR_CALLCONV
);
285 if (!callconv
) callconv
= "";
288 print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
289 iface
->name
, get_name(func
) );
292 if (has_full_pointer
) write_full_pointer_free(proxy
, indent
, func
);
293 print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
298 write_type_decl_left(proxy
, type_function_get_rettype(func
->type
));
299 print_proxy( " %s %s_%s_Proxy(\n", callconv
, iface
->name
, get_name(func
));
300 write_args(proxy
, type_get_function_args(func
->type
), iface
->name
, 1, TRUE
);
304 print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
305 /* local variables */
307 print_proxy( "%s", "" );
308 write_type_decl_left(proxy
, type_function_get_rettype(func
->type
));
309 print_proxy( " _RetVal;\n");
311 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
313 if (decl_indirect(type_function_get_rettype(func
->type
)))
314 print_proxy("void *_p_%s = &%s;\n",
315 "_RetVal", "_RetVal");
319 print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface
->name
, get_name(func
) );
320 print_proxy( "__frame->This = This;\n" );
322 if (has_full_pointer
)
323 write_full_pointer_init(proxy
, indent
, func
, FALSE
);
326 clear_output_vars( type_get_function_args(func
->type
) );
328 print_proxy( "RpcTryExcept\n" );
329 print_proxy( "{\n" );
331 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx
);
332 proxy_check_pointers( type_get_function_args(func
->type
) );
334 print_proxy( "RpcTryFinally\n" );
335 print_proxy( "{\n" );
338 write_remoting_arguments(proxy
, indent
, func
, "", PASS_IN
, PHASE_BUFFERSIZE
);
340 print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
342 write_remoting_arguments(proxy
, indent
, func
, "", PASS_IN
, PHASE_MARSHAL
);
344 print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
345 fprintf(proxy
, "\n");
346 print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
347 print_proxy( "__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
349 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
351 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset
);
353 fprintf(proxy
, "\n");
355 write_remoting_arguments(proxy
, indent
, func
, "", PASS_OUT
, PHASE_UNMARSHAL
);
359 if (decl_indirect(type_function_get_rettype(func
->type
)))
360 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
361 else if (is_ptr(type_function_get_rettype(func
->type
)) ||
362 is_array(type_function_get_rettype(func
->type
)))
363 print_proxy("%s = 0;\n", "_RetVal");
364 write_remoting_arguments(proxy
, indent
, func
, "", PASS_RETURN
, PHASE_UNMARSHAL
);
369 print_proxy( "RpcFinally\n" );
370 print_proxy( "{\n" );
372 print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface
->name
, get_name(func
) );
375 print_proxy( "RpcEndFinally\n" );
377 print_proxy( "}\n" );
378 print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
379 print_proxy( "{\n" );
382 proxy_free_variables( type_get_function_args(func
->type
), "" );
383 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
386 print_proxy( "}\n" );
387 print_proxy( "RpcEndExcept\n" );
390 print_proxy( "return _RetVal;\n" );
397 static void gen_stub(type_t
*iface
, const var_t
*func
, const char *cas
,
398 unsigned int proc_offset
)
401 int has_ret
= !is_void(type_function_get_rettype(func
->type
));
402 int has_full_pointer
= is_full_pointer_function(func
);
405 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface
->name
, get_name(func
));
407 print_proxy( "__DECL_EXCEPTION_FRAME\n" );
408 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
409 print_proxy( "%s * _This;\n", iface
->name
);
410 declare_stub_args( proxy
, indent
, func
);
412 print_proxy( "};\n\n" );
414 print_proxy( "static void __finally_%s_%s_Stub(", iface
->name
, get_name(func
) );
415 print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface
->name
, get_name(func
) );
417 write_remoting_arguments(proxy
, indent
, func
, "__frame->", PASS_OUT
, PHASE_FREE
);
418 if (has_full_pointer
)
419 write_full_pointer_free(proxy
, indent
, func
);
421 print_proxy( "}\n\n" );
423 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(func
));
425 print_proxy( "IRpcStubBuffer* This,\n");
426 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
427 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
428 print_proxy( "DWORD* _pdwStubPhase)\n");
432 print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
433 iface
->name
, get_name(func
) );
435 print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface
->name
);
439 print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
440 fprintf(proxy
, "\n");
441 print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface
->name
, get_name(func
) );
443 write_parameters_init(proxy
, indent
, func
, "__frame->");
445 print_proxy("RpcTryFinally\n");
448 if (has_full_pointer
)
449 write_full_pointer_init(proxy
, indent
, func
, TRUE
);
450 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
452 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset
);
454 fprintf(proxy
, "\n");
456 write_remoting_arguments(proxy
, indent
, func
, "__frame->", PASS_IN
, PHASE_UNMARSHAL
);
457 fprintf(proxy
, "\n");
459 assign_stub_out_args( proxy
, indent
, func
, "__frame->" );
461 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
462 fprintf(proxy
, "\n");
463 print_proxy( "%s", has_ret
? "__frame->_RetVal = " : "" );
464 if (cas
) fprintf(proxy
, "%s_%s_Stub", iface
->name
, cas
);
465 else fprintf(proxy
, "__frame->_This->lpVtbl->%s", get_name(func
));
466 fprintf(proxy
, "(__frame->_This");
468 if (type_get_function_args(func
->type
))
470 LIST_FOR_EACH_ENTRY( arg
, type_get_function_args(func
->type
), const var_t
, entry
)
471 fprintf(proxy
, ", %s__frame->%s", arg
->type
->declarray
? "*" : "", arg
->name
);
473 fprintf(proxy
, ");\n");
474 fprintf(proxy
, "\n");
475 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
476 fprintf(proxy
, "\n");
478 write_remoting_arguments(proxy
, indent
, func
, "__frame->", PASS_OUT
, PHASE_BUFFERSIZE
);
480 if (!is_void(type_function_get_rettype(func
->type
)))
481 write_remoting_arguments(proxy
, indent
, func
, "__frame->", PASS_RETURN
, PHASE_BUFFERSIZE
);
483 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
485 write_remoting_arguments(proxy
, indent
, func
, "__frame->", PASS_OUT
, PHASE_MARSHAL
);
486 fprintf(proxy
, "\n");
488 /* marshall the return value */
489 if (!is_void(type_function_get_rettype(func
->type
)))
490 write_remoting_arguments(proxy
, indent
, func
, "__frame->", PASS_RETURN
, PHASE_MARSHAL
);
494 print_proxy("RpcFinally\n");
497 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface
->name
, get_name(func
) );
500 print_proxy("RpcEndFinally\n");
502 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
509 static int count_methods(type_t
*iface
)
511 const statement_t
*stmt
;
514 if (type_iface_get_inherit(iface
))
515 count
= count_methods(type_iface_get_inherit(iface
));
517 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
)) {
518 const var_t
*func
= stmt
->u
.var
;
519 if (!is_callas(func
->attrs
)) count
++;
524 static const statement_t
* get_callas_source(const type_t
* iface
, const var_t
* def
)
526 const statement_t
* source
;
527 STATEMENTS_FOR_EACH_FUNC( source
, type_iface_get_stmts(iface
)) {
528 const var_t
* cas
= is_callas(source
->u
.var
->attrs
);
529 if (cas
&& !strcmp(def
->name
, cas
->name
))
535 static int write_proxy_methods(type_t
*iface
, int skip
)
537 const statement_t
*stmt
;
540 if (type_iface_get_inherit(iface
))
541 i
= write_proxy_methods(type_iface_get_inherit(iface
),
542 need_delegation(iface
));
543 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
)) {
544 const var_t
*func
= stmt
->u
.var
;
545 if (!is_callas(func
->attrs
)) {
546 if (i
!= func
->type
->details
.function
->idx
)
547 error("widl internal error: method index mismatch\n");
548 if (i
) fprintf(proxy
, ",\n");
549 if (skip
|| (is_local(func
->attrs
) && !get_callas_source(iface
, func
)))
550 print_proxy( "0 /* %s_%s_Proxy */", iface
->name
, get_name(func
));
551 else print_proxy( "%s_%s_Proxy", iface
->name
, get_name(func
));
558 static int write_stub_methods(type_t
*iface
, int skip
)
560 const statement_t
*stmt
;
563 if (type_iface_get_inherit(iface
))
564 i
= write_stub_methods(type_iface_get_inherit(iface
), need_delegation(iface
));
566 return i
; /* skip IUnknown */
568 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
)) {
569 const var_t
*func
= stmt
->u
.var
;
570 if (!is_callas(func
->attrs
)) {
572 const char * fname
= get_name(func
);
573 if(is_local(func
->attrs
)) {
574 const statement_t
* callas_source
= get_callas_source(iface
, func
);
578 fname
= get_name(callas_source
->u
.var
);
580 if (i
) fprintf(proxy
,",\n");
581 if (skip
|| missing
) print_proxy("STUB_FORWARDING_FUNCTION");
582 else print_proxy( "%s_%s_Stub", iface
->name
, fname
);
589 static void write_proxy(type_t
*iface
, unsigned int *proc_offset
)
592 const statement_t
*stmt
;
595 /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
597 STATEMENTS_FOR_EACH_FUNC(stmt
, type_iface_get_stmts(iface
)) {
598 const var_t
*func
= stmt
->u
.var
;
600 fprintf(proxy
, "/*****************************************************************************\n");
601 fprintf(proxy
, " * %s interface\n", iface
->name
);
602 fprintf(proxy
, " */\n");
605 if (!is_local(func
->attrs
)) {
606 const var_t
*cas
= is_callas(func
->attrs
);
607 const char *cname
= cas
? cas
->name
: NULL
;
608 int idx
= func
->type
->details
.function
->idx
;
610 const statement_t
*stmt2
;
611 STATEMENTS_FOR_EACH_FUNC(stmt2
, type_iface_get_stmts(iface
)) {
612 const var_t
*m
= stmt2
->u
.var
;
613 if (!strcmp(m
->name
, cname
))
615 idx
= m
->type
->details
.function
->idx
;
620 gen_proxy(iface
, func
, idx
, *proc_offset
);
621 gen_stub(iface
, func
, cname
, *proc_offset
);
622 *proc_offset
+= get_size_procformatstring_func( func
);
626 count
= count_methods(iface
);
629 print_proxy( "static const CINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n", count
, iface
->name
);
634 print_proxy( "&IID_%s,\n", iface
->name
);
636 print_proxy( "},\n");
639 write_proxy_methods(iface
, FALSE
);
640 fprintf(proxy
, "\n");
644 print_proxy( "};\n");
645 fprintf(proxy
, "\n\n");
648 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface
->name
);
651 write_stub_methods(iface
, FALSE
);
652 fprintf(proxy
, "\n");
654 fprintf(proxy
, "};\n");
656 print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
657 need_delegation_indirect(iface
) ? "" : "const ", iface
->name
);
662 print_proxy( "&IID_%s,\n", iface
->name
);
663 print_proxy( "0,\n");
664 print_proxy( "%d,\n", count
);
665 print_proxy( "&%s_table[-3],\n", iface
->name
);
667 print_proxy( "},\n");
670 print_proxy( "CStdStubBuffer_%s\n", need_delegation_indirect(iface
) ? "DELEGATING_METHODS" : "METHODS");
674 print_proxy( "};\n");
678 static int does_any_iface(const statement_list_t
*stmts
, type_pred_t pred
)
680 const statement_t
*stmt
;
683 LIST_FOR_EACH_ENTRY(stmt
, stmts
, const statement_t
, entry
)
685 if (stmt
->type
== STMT_LIBRARY
)
687 if (does_any_iface(stmt
->u
.lib
->stmts
, pred
))
690 else if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
692 if (pred(stmt
->u
.type
))
700 int need_proxy(const type_t
*iface
)
702 return is_object(iface
->attrs
) && !is_local(iface
->attrs
);
705 int need_stub(const type_t
*iface
)
707 return !is_object(iface
->attrs
) && !is_local(iface
->attrs
);
710 int need_proxy_file(const statement_list_t
*stmts
)
712 return does_any_iface(stmts
, need_proxy
);
715 int need_stub_files(const statement_list_t
*stmts
)
717 return does_any_iface(stmts
, need_stub
);
720 static void write_proxy_stmts(const statement_list_t
*stmts
, unsigned int *proc_offset
)
722 const statement_t
*stmt
;
723 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
725 if (stmt
->type
== STMT_LIBRARY
)
726 write_proxy_stmts(stmt
->u
.lib
->stmts
, proc_offset
);
727 else if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
729 if (need_proxy(stmt
->u
.type
))
730 write_proxy(stmt
->u
.type
, proc_offset
);
735 static int cmp_iid( const void *ptr1
, const void *ptr2
)
737 const type_t
* const *iface1
= ptr1
;
738 const type_t
* const *iface2
= ptr2
;
739 const UUID
*uuid1
= get_attrp( (*iface1
)->attrs
, ATTR_UUID
);
740 const UUID
*uuid2
= get_attrp( (*iface2
)->attrs
, ATTR_UUID
);
741 return memcmp( uuid1
, uuid2
, sizeof(UUID
) );
744 static void build_iface_list( const statement_list_t
*stmts
, type_t
**ifaces
[], int *count
)
746 const statement_t
*stmt
;
749 LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
751 if (stmt
->type
== STMT_LIBRARY
)
752 build_iface_list(stmt
->u
.lib
->stmts
, ifaces
, count
);
753 else if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
755 type_t
*iface
= stmt
->u
.type
;
756 if (type_iface_get_inherit(iface
) && need_proxy(iface
))
758 *ifaces
= xrealloc( *ifaces
, (*count
+ 1) * sizeof(*ifaces
) );
759 (*ifaces
)[(*count
)++] = iface
;
765 static type_t
**sort_interfaces( const statement_list_t
*stmts
, int *count
)
767 type_t
**ifaces
= NULL
;
770 build_iface_list( stmts
, &ifaces
, count
);
771 qsort( ifaces
, *count
, sizeof(*ifaces
), cmp_iid
);
775 static void write_proxy_routines(const statement_list_t
*stmts
)
777 int expr_eval_routines
;
778 unsigned int proc_offset
= 0;
780 write_formatstringsdecl(proxy
, indent
, stmts
, need_proxy
);
781 write_stubdescproto();
782 write_proxy_stmts(stmts
, &proc_offset
);
784 expr_eval_routines
= write_expr_eval_routines(proxy
, proxy_token
);
785 if (expr_eval_routines
)
786 write_expr_eval_routine_list(proxy
, proxy_token
);
787 write_user_quad_list(proxy
);
788 write_stubdesc(expr_eval_routines
);
790 print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size
== 8 ? 64 : 32);
791 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
792 print_proxy( "#endif\n");
794 write_procformatstring(proxy
, stmts
, need_proxy
);
795 write_typeformatstring(proxy
, stmts
, need_proxy
);
799 void write_proxies(const statement_list_t
*stmts
)
801 char *file_id
= proxy_token
;
802 int i
, count
, have_baseiid
;
804 const type_t
* delegate_to
;
806 if (!do_proxies
) return;
807 if (do_everything
&& !need_proxy_file(stmts
)) return;
812 if (do_win32
&& do_win64
)
814 fprintf(proxy
, "\n#ifndef _WIN64\n\n");
816 write_proxy_routines( stmts
);
817 fprintf(proxy
, "\n#else /* _WIN64 */\n\n");
819 write_proxy_routines( stmts
);
820 fprintf(proxy
, "#endif /* _WIN64 */\n\n");
825 write_proxy_routines( stmts
);
830 write_proxy_routines( stmts
);
833 interfaces
= sort_interfaces(stmts
, &count
);
834 fprintf(proxy
, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id
);
835 fprintf(proxy
, "{\n");
836 for (i
= 0; i
< count
; i
++)
837 fprintf(proxy
, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces
[i
]->name
);
838 fprintf(proxy
, " 0\n");
839 fprintf(proxy
, "};\n");
840 fprintf(proxy
, "\n");
842 fprintf(proxy
, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id
);
843 fprintf(proxy
, "{\n");
844 for (i
= 0; i
< count
; i
++)
845 fprintf(proxy
, " &_%sStubVtbl,\n", interfaces
[i
]->name
);
846 fprintf(proxy
, " 0\n");
847 fprintf(proxy
, "};\n");
848 fprintf(proxy
, "\n");
850 fprintf(proxy
, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id
);
851 fprintf(proxy
, "{\n");
852 for (i
= 0; i
< count
; i
++)
853 fprintf(proxy
, " \"%s\",\n", interfaces
[i
]->name
);
854 fprintf(proxy
, " 0\n");
855 fprintf(proxy
, "};\n");
856 fprintf(proxy
, "\n");
858 if ((have_baseiid
= does_any_iface(stmts
, need_delegation_indirect
)))
860 fprintf(proxy
, "static const IID * _%s_BaseIIDList[] =\n", file_id
);
861 fprintf(proxy
, "{\n");
862 for (i
= 0; i
< count
; i
++)
864 if (get_delegation_indirect(interfaces
[i
], &delegate_to
))
865 fprintf( proxy
, " &IID_%s, /* %s */\n", delegate_to
->name
, interfaces
[i
]->name
);
867 fprintf( proxy
, " 0,\n" );
869 fprintf(proxy
, " 0\n");
870 fprintf(proxy
, "};\n");
871 fprintf(proxy
, "\n");
874 fprintf(proxy
, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id
);
875 fprintf(proxy
, "{\n");
876 fprintf(proxy
, " int low = 0, high = %d;\n", count
- 1);
877 fprintf(proxy
, "\n");
878 fprintf(proxy
, " while (low <= high)\n");
879 fprintf(proxy
, " {\n");
880 fprintf(proxy
, " int pos = (low + high) / 2;\n");
881 fprintf(proxy
, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id
);
882 fprintf(proxy
, " if (!res) { *pIndex = pos; return 1; }\n");
883 fprintf(proxy
, " if (res > 0) low = pos + 1;\n");
884 fprintf(proxy
, " else high = pos - 1;\n");
885 fprintf(proxy
, " }\n");
886 fprintf(proxy
, " return 0;\n");
887 fprintf(proxy
, "}\n");
888 fprintf(proxy
, "\n");
890 fprintf(proxy
, "const ExtendedProxyFileInfo %s_ProxyFileInfo DECLSPEC_HIDDEN =\n", file_id
);
891 fprintf(proxy
, "{\n");
892 fprintf(proxy
, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id
);
893 fprintf(proxy
, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id
);
894 fprintf(proxy
, " _%s_InterfaceNamesList,\n", file_id
);
895 if (have_baseiid
) fprintf(proxy
, " _%s_BaseIIDList,\n", file_id
);
896 else fprintf(proxy
, " 0,\n");
897 fprintf(proxy
, " _%s_IID_Lookup,\n", file_id
);
898 fprintf(proxy
, " %d,\n", count
);
899 fprintf(proxy
, " 1,\n");
900 fprintf(proxy
, " 0,\n");
901 fprintf(proxy
, " 0,\n");
902 fprintf(proxy
, " 0,\n");
903 fprintf(proxy
, " 0\n");
904 fprintf(proxy
, "};\n");