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 static void print_proxy( const char *format
, ... ) __attribute__((format (printf
, 1, 2)));
52 static void print_proxy( const char *format
, ... )
55 va_start( va
, format
);
56 print( proxy
, indent
, format
, va
);
60 static void write_stubdescproto(void)
62 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
66 static void write_stubdesc(int expr_eval_routines
)
68 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc =\n{\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);
78 print_proxy( "0x50100a4, /* MIDL Version 5.1.164 */\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");
91 static void init_proxy(const statement_list_t
*stmts
)
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
);
98 print_proxy( "#define __midl_proxy\n");
99 print_proxy( "#include \"objbase.h\"\n");
101 print_proxy( "#ifndef DECLSPEC_HIDDEN\n");
102 print_proxy( "#define DECLSPEC_HIDDEN\n");
103 print_proxy( "#endif\n");
107 static void clear_output_vars( const var_list_t
*args
)
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
))
138 *delegate_to
= type_iface_get_inherit(cur_iface
);
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
))
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
) );
167 case TGT_IFACE_POINTER
:
170 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
171 fprintf(proxy
, "&__MIDL_TypeFormatString.Format[%u], ", type_offset
);
172 fprintf(proxy
, "(void *)%s );\n", arg
->name
);
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
)
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";
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
);
211 write_client_call_routine( proxy
, iface
, func
, "Object", proc_offset
);
214 print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
215 iface
->name
, get_name(func
) );
218 if (has_full_pointer
) write_full_pointer_free(proxy
, indent
, func
);
219 print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\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
);
230 print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
231 /* local variables */
233 print_proxy( "%s", "" );
234 write_type_decl_left(proxy
, rettype
);
235 print_proxy( " _RetVal;\n");
237 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
239 if (decl_indirect(rettype
))
240 print_proxy("void *_p_%s = &%s;\n",
241 "_RetVal", "_RetVal");
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
);
252 clear_output_vars( type_get_function_args(func
->type
) );
254 print_proxy( "RpcTryExcept\n" );
255 print_proxy( "{\n" );
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" );
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");
277 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset
);
279 fprintf(proxy
, "\n");
281 write_remoting_arguments(proxy
, indent
, func
, "", PASS_OUT
, PHASE_UNMARSHAL
);
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
);
294 print_proxy( "RpcFinally\n" );
295 print_proxy( "{\n" );
297 print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface
->name
, get_name(func
) );
300 print_proxy( "RpcEndFinally\n" );
302 print_proxy( "}\n" );
303 print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
304 print_proxy( "{\n" );
307 proxy_free_variables( type_get_function_args(func
->type
), "" );
308 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
311 print_proxy( "}\n" );
312 print_proxy( "RpcEndExcept\n" );
315 print_proxy( "return _RetVal;\n" );
322 static void gen_stub(type_t
*iface
, const var_t
*func
, const char *cas
,
323 unsigned int proc_offset
)
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;
332 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface
->name
, get_name(func
));
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
);
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
) );
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
);
348 print_proxy( "}\n\n" );
350 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface
->name
, get_name(func
));
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");
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
);
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");
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");
379 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset
);
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
);
421 print_proxy("RpcFinally\n");
424 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface
->name
, get_name(func
) );
427 print_proxy("RpcEndFinally\n");
429 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\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
);
443 print_proxy( "void __RPC_API %s_%s_Thunk( PMIDL_STUB_MESSAGE pStubMsg )\n",
444 iface
->name
, get_name(func
) );
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
);
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" );
459 print_proxy( "}\n\n");
462 int count_methods(const type_t
*iface
)
464 const statement_t
*stmt
;
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
++;
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
))
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
));
497 STATEMENTS_FOR_EACH_FUNC( stmt
, type_iface_get_stmts(iface
) )
499 const var_t
*func
= stmt
->u
.var
;
502 if (is_callas(func
->attrs
)) continue;
503 if (is_local(func
->attrs
))
505 const statement_t
* callas_source
= get_callas_source(iface
, func
);
509 func
= callas_source
->u
.var
;
512 print_proxy( "(unsigned short)-1, /* %s::%s */\n", iface
->name
, get_name(func
));
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
;
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
));
536 print_proxy( "%s_%s_Proxy,\n", iface
->name
, get_name(func
));
543 static int write_stub_methods(type_t
*iface
, int skip
)
545 const statement_t
*stmt
;
548 if (type_iface_get_inherit(iface
))
549 i
= write_stub_methods(type_iface_get_inherit(iface
), need_delegation(iface
));
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
)) {
557 const char * fname
= get_name(func
);
558 if(is_local(func
->attrs
)) {
559 const statement_t
* callas_source
= get_callas_source(iface
, func
);
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
);
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
) );
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
) );
596 print_proxy( "0, /* %s::%s */\n", iface
->name
, get_name(func
) );
600 static void write_proxy(type_t
*iface
, unsigned int *proc_offset
)
603 const statement_t
*stmt
;
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
;
611 fprintf(proxy
, "/*****************************************************************************\n");
612 fprintf(proxy
, " * %s interface\n", iface
->name
);
613 fprintf(proxy
, " */\n");
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
;
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
;
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" );
648 write_proxy_procformatstring_offsets( iface
, 0 );
650 print_proxy( "};\n\n" );
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" );
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" );
665 print_proxy( "};\n\n" );
669 print_proxy( "static %sCINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n",
670 need_delegation_indirect(iface
) ? "" : "const ", count
, iface
->name
);
675 if (get_stub_mode() == MODE_Oif
) print_proxy( "&%s_ProxyInfo,\n", iface
->name
);
676 print_proxy( "&IID_%s,\n", iface
->name
);
678 print_proxy( "},\n");
681 write_proxy_methods(iface
, FALSE
);
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
);
693 write_thunk_methods( iface
, 0 );
695 print_proxy( "};\n\n");
699 print_proxy( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface
->name
);
700 print_proxy( "{\n" );
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
);
709 print_proxy( "0,\n" );
710 print_proxy( "0,\n" );
711 print_proxy( "0,\n" );
712 print_proxy( "0\n" );
714 print_proxy( "};\n\n" );
717 if (needs_inline_stubs
)
719 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface
->name
);
722 write_stub_methods(iface
, FALSE
);
723 fprintf(proxy
, "\n");
725 fprintf(proxy
, "};\n\n");
727 print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
728 need_delegation_indirect(iface
) ? "" : "const ", iface
->name
);
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" );
739 print_proxy( "},\n");
742 print_proxy( "CStdStubBuffer_%s\n", need_delegation_indirect(iface
) ? "DELEGATING_METHODS" : "METHODS");
746 print_proxy( "};\n");
750 static int does_any_iface(const statement_list_t
*stmts
, type_pred_t pred
)
752 const statement_t
*stmt
;
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
))
762 else if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
764 if (pred(stmt
->u
.type
))
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;
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;
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;
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
;
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
;
882 build_iface_list( stmts
, &ifaces
, count
);
883 qsort( ifaces
, *count
, sizeof(*ifaces
), cmp_iid
);
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;
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");
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");
906 print_proxy( "#include \"%s\"\n", header_name
);
909 if (does_any_iface(stmts
, need_proxy_and_inline_stubs
))
911 write_exceptions( proxy
);
913 print_proxy( "struct __proxy_frame\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");
920 print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
922 print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\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");
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;
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
);
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;
1029 if (do_win32
&& do_win64
)
1031 fprintf(proxy
, "\n#ifndef _WIN64\n\n");
1033 write_proxy_routines( stmts
);
1034 fprintf(proxy
, "\n#else /* _WIN64 */\n\n");
1036 write_proxy_routines( stmts
);
1037 fprintf(proxy
, "\n#endif /* _WIN64 */\n");
1042 write_proxy_routines( stmts
);
1047 write_proxy_routines( stmts
);