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"
41 #define END_OF_LIST(list) \
44 while (NEXT_LINK(list)) \
45 list = NEXT_LINK(list); \
50 static int indent
= 0;
52 /* FIXME: support generation of stubless proxies */
54 static int print_proxy( const char *format
, ... )
59 va_start( va
, format
);
60 if ( format
[0] != '\n' )
61 for( i
=0; i
<indent
; i
++ )
62 fprintf( proxy
, " " );
63 r
= vfprintf( proxy
, format
, va
);
68 static void write_stubdescproto(void)
70 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
74 static void write_stubdesc(void)
76 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc =\n{\n");
79 print_proxy( "NdrOleAllocate,\n");
80 print_proxy( "NdrOleFree,\n");
81 print_proxy( "{0}, 0, 0, 0, 0,\n");
82 print_proxy( "__MIDL_TypeFormatString.Format,\n");
83 print_proxy( "1, /* -error bounds_check flag */\n");
84 print_proxy( "0x10001, /* Ndr library version */\n");
86 print_proxy( "0x50100a4, /* MIDL Version 5.1.164 */\n");
89 print_proxy( "0, /* notify & notify_flag routine table */\n");
90 print_proxy( "1, /* Flags */\n");
91 print_proxy( "0, /* Reserved3 */\n");
92 print_proxy( "0, /* Reserved4 */\n");
93 print_proxy( "0 /* Reserved5 */\n");
99 static void init_proxy(ifref_list_t
*ifaces
)
102 if(!(proxy
= fopen(proxy_name
, "w")))
103 error("Could not open %s for output\n", proxy_name
);
104 print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION
, input_name
);
106 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
107 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ 440\n");
108 print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
110 print_proxy( "#define __midl_proxy\n");
111 print_proxy( "#include \"objbase.h\"\n");
112 print_proxy( "#include \"rpcproxy.h\"\n");
113 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
114 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
115 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
117 print_proxy( "#include \"%s\"\n", header_name
);
119 write_formatstringsdecl(proxy
, indent
, ifaces
, 1);
120 write_stubdescproto();
123 static void clear_output_vars( const var_list_t
*args
)
128 LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
130 if (is_attr(arg
->attrs
, ATTR_OUT
) && !is_attr(arg
->attrs
, ATTR_IN
)) {
131 print_proxy( "if(%s)\n", arg
->name
);
133 print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg
->name
, arg
->name
);
139 int is_var_ptr(const var_t
*v
)
141 return is_ptr(v
->type
);
144 int cant_be_null(const var_t
*v
)
146 /* Search backwards for the most recent pointer attribute. */
147 const attr_list_t
*attrs
= v
->attrs
;
148 const type_t
*type
= v
->type
;
158 int t
= get_attrv(attrs
, ATTR_POINTERTYPE
);
160 if (t
== RPC_FC_FP
|| t
== RPC_FC_OP
|| t
== RPC_FC_UP
)
175 return 1; /* Default is RPC_FC_RP. */
178 static void proxy_check_pointers( const var_list_t
*args
)
183 LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
185 if (is_var_ptr(arg
) && cant_be_null(arg
)) {
186 print_proxy( "if(!%s)\n", arg
->name
);
188 print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
194 static void free_variable( const var_t
*arg
, unsigned int type_offset
)
200 expr
= get_attrp( arg
->attrs
, ATTR_SIZEIS
);
203 const expr_t
*size
= LIST_ENTRY( list_head(expr
), const expr_t
, entry
);
204 print_proxy( "_StubMsg.MaxCount = " );
205 write_expr(proxy
, size
, 0);
206 fprintf(proxy
, ";\n\n");
207 print_proxy( "NdrClearOutParameters( &_StubMsg, ");
208 fprintf(proxy
, "&__MIDL_TypeFormatString.Format[%u], ", type_offset
);
209 fprintf(proxy
, "(void*)%s );\n", arg
->name
);
230 constraint
= get_attrp( arg
->attrs
, ATTR_IIDIS
);
232 print_proxy( "_StubMsg.MaxCount = (unsigned long) ( %s );\n",constraint
->name
);
233 print_proxy( "NdrClearOutParameters( &_StubMsg, ");
234 fprintf(proxy
, "&__MIDL_TypeFormatString.Format[%u], ", type_offset
);
235 fprintf(proxy
, "(void*)%s );\n", arg
->name
);
239 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__
, arg
->name
, type
->type
);
243 static void proxy_free_variables( var_list_t
*args
, unsigned int type_offset
)
248 LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
251 size_t size_type
= get_size_typeformatstring_var(arg
, &start_offset
);
252 start_offset
+= type_offset
;
254 if (is_attr(arg
->attrs
, ATTR_OUT
))
256 free_variable( arg
, type_offset
);
257 fprintf(proxy
, "\n");
260 type_offset
+= size_type
;
264 static void gen_proxy(type_t
*iface
, const func_t
*cur
, int idx
,
265 unsigned int proc_offset
, unsigned int *type_offset
)
267 var_t
*def
= cur
->def
;
268 int has_ret
= !is_void(def
->type
);
272 write_type(proxy
, def
->type
);
273 print_proxy( " STDMETHODCALLTYPE %s_", iface
->name
);
274 write_name(proxy
, def
);
275 print_proxy( "_Proxy(\n");
276 write_args(proxy
, cur
->args
, iface
->name
, 1, TRUE
);
280 /* local variables */
283 write_type(proxy
, def
->type
);
284 print_proxy( " _RetVal;\n");
286 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
287 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n" );
291 clear_output_vars( cur
->args
);
293 print_proxy( "RpcTryExcept\n" );
294 print_proxy( "{\n" );
296 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &_StubMsg, &Object_StubDesc, %d);\n", idx
);
297 proxy_check_pointers( cur
->args
);
299 print_proxy( "RpcTryFinally\n" );
300 print_proxy( "{\n" );
303 offset
= *type_offset
;
304 write_remoting_arguments(proxy
, indent
, cur
, &offset
, PASS_IN
, PHASE_BUFFERSIZE
);
306 print_proxy( "NdrProxyGetBuffer(This, &_StubMsg);\n" );
308 offset
= *type_offset
;
309 write_remoting_arguments(proxy
, indent
, cur
, &offset
, PASS_IN
, PHASE_MARSHAL
);
311 print_proxy( "NdrProxySendReceive(This, &_StubMsg);\n" );
312 fprintf(proxy
, "\n");
313 print_proxy( "_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
314 print_proxy( "_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
316 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
318 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset
);
320 fprintf(proxy
, "\n");
322 offset
= *type_offset
;
323 write_remoting_arguments(proxy
, indent
, cur
, type_offset
, PASS_OUT
, PHASE_UNMARSHAL
);
326 print_phase_basetype(proxy
, indent
, PHASE_UNMARSHAL
, PASS_RETURN
, def
, "_RetVal");
330 print_proxy( "RpcFinally\n" );
331 print_proxy( "{\n" );
333 print_proxy( "NdrProxyFreeBuffer(This, &_StubMsg);\n" );
336 print_proxy( "RpcEndFinally\n" );
338 print_proxy( "}\n" );
339 print_proxy( "RpcExcept(_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
340 print_proxy( "{\n" );
343 proxy_free_variables( cur
->args
, offset
);
344 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
347 print_proxy( "}\n" );
348 print_proxy( "RpcEndExcept\n" );
351 print_proxy( "return _RetVal;\n" );
358 static void gen_stub(type_t
*iface
, const func_t
*cur
, const char *cas
,
359 unsigned int proc_offset
, unsigned int *type_offset
)
361 var_t
*def
= cur
->def
;
363 int has_ret
= !is_void(def
->type
);
367 print_proxy( "void __RPC_STUB %s_", iface
->name
);
368 write_name(proxy
, def
);
369 print_proxy( "_Stub(\n");
371 print_proxy( "IRpcStubBuffer* This,\n");
372 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
373 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
374 print_proxy( "DWORD* _pdwStubPhase)\n");
378 print_proxy("%s * _This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n", iface
->name
, iface
->name
);
379 print_proxy("MIDL_STUB_MESSAGE _StubMsg;\n");
380 declare_stub_args( proxy
, indent
, cur
);
381 fprintf(proxy
, "\n");
385 print_proxy("NdrStubInitialize(_pRpcMessage, &_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
386 fprintf(proxy
, "\n");
389 LIST_FOR_EACH_ENTRY( arg
, cur
->args
, const var_t
, entry
)
390 print_proxy("%s = 0;\n", arg
->name
);
392 print_proxy("RpcTryFinally\n");
395 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
397 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset
);
399 fprintf(proxy
, "\n");
401 offset
= *type_offset
;
402 write_remoting_arguments(proxy
, indent
, cur
, &offset
, PASS_IN
, PHASE_UNMARSHAL
);
403 fprintf(proxy
, "\n");
405 assign_stub_out_args( proxy
, indent
, cur
);
407 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
408 fprintf(proxy
, "\n");
410 if (has_ret
) fprintf(proxy
, "_RetVal = ");
411 if (cas
) fprintf(proxy
, "%s_%s_Stub", iface
->name
, cas
);
414 fprintf(proxy
, "_This->lpVtbl->");
415 write_name(proxy
, def
);
417 fprintf(proxy
, "(_This");
421 LIST_FOR_EACH_ENTRY( arg
, cur
->args
, const var_t
, entry
)
423 fprintf(proxy
, ", ");
426 write_name(proxy
, arg
);
429 fprintf(proxy
, ");\n");
430 fprintf(proxy
, "\n");
431 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
432 fprintf(proxy
, "\n");
434 offset
= *type_offset
;
435 write_remoting_arguments(proxy
, indent
, cur
, &offset
, PASS_OUT
, PHASE_BUFFERSIZE
);
437 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &_StubMsg);\n");
439 offset
= *type_offset
;
440 write_remoting_arguments(proxy
, indent
, cur
, &offset
, PASS_OUT
, PHASE_MARSHAL
);
441 fprintf(proxy
, "\n");
444 print_phase_basetype(proxy
, indent
, PHASE_MARSHAL
, PASS_RETURN
, def
, "_RetVal");
448 print_proxy("RpcFinally\n");
451 offset
= *type_offset
;
452 write_remoting_arguments(proxy
, indent
+1, cur
, &offset
, PASS_OUT
, PHASE_FREE
);
455 print_proxy("RpcEndFinally\n");
457 print_proxy("_pRpcMessage->BufferLength = _StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
464 static int write_proxy_methods(type_t
*iface
)
469 if (iface
->ref
) i
= write_proxy_methods(iface
->ref
);
470 if (iface
->funcs
) LIST_FOR_EACH_ENTRY( cur
, iface
->funcs
, const func_t
, entry
) {
471 var_t
*def
= cur
->def
;
472 if (!is_callas(def
->attrs
)) {
473 if (i
) fprintf(proxy
, ",\n");
474 print_proxy( "%s_", iface
->name
);
475 write_name(proxy
, def
);
476 fprintf(proxy
, "_Proxy");
483 static int write_stub_methods(type_t
*iface
)
488 if (iface
->ref
) i
= write_stub_methods(iface
->ref
);
489 else return i
; /* skip IUnknown */
491 if (iface
->funcs
) LIST_FOR_EACH_ENTRY( cur
, iface
->funcs
, const func_t
, entry
) {
492 var_t
*def
= cur
->def
;
493 if (!is_local(def
->attrs
)) {
494 if (i
) fprintf(proxy
,",\n");
495 print_proxy( "%s_", iface
->name
);
496 write_name(proxy
, def
);
497 fprintf(proxy
, "_Stub");
504 static void write_proxy(type_t
*iface
, unsigned int *proc_offset
, unsigned int *type_offset
)
506 int midx
= -1, stubs
;
510 if (!iface
->funcs
) return;
512 /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
514 fprintf(proxy
, "/*****************************************************************************\n");
515 fprintf(proxy
, " * %s interface\n", iface
->name
);
516 fprintf(proxy
, " */\n");
517 LIST_FOR_EACH_ENTRY( cur
, iface
->funcs
, const func_t
, entry
)
519 const var_t
*def
= cur
->def
;
520 if (!is_local(def
->attrs
)) {
521 const var_t
*cas
= is_callas(def
->attrs
);
522 const char *cname
= cas
? cas
->name
: NULL
;
526 LIST_FOR_EACH_ENTRY( m
, iface
->funcs
, const func_t
, entry
)
527 if (!strcmp(get_name(m
->def
), cname
))
533 offset
= *type_offset
;
534 gen_proxy(iface
, cur
, idx
, *proc_offset
, type_offset
);
535 gen_stub(iface
, cur
, cname
, *proc_offset
, &offset
);
536 *proc_offset
+= get_size_procformatstring_func( cur
);
537 if (midx
== -1) midx
= idx
;
538 else if (midx
!= idx
) parser_error("method index mismatch in write_proxy");
544 print_proxy( "static const CINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n", midx
, iface
->name
);
547 print_proxy( "{\n", iface
->name
);
549 print_proxy( "&IID_%s,\n", iface
->name
);
551 print_proxy( "},\n");
554 write_proxy_methods(iface
);
555 fprintf(proxy
, "\n");
559 print_proxy( "};\n");
560 fprintf(proxy
, "\n\n");
563 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface
->name
);
566 stubs
= write_stub_methods(iface
);
567 fprintf(proxy
, "\n");
569 fprintf(proxy
, "};\n");
571 print_proxy( "static const CInterfaceStubVtbl _%sStubVtbl =\n", iface
->name
);
576 print_proxy( "&IID_%s,\n", iface
->name
);
577 print_proxy( "0,\n");
578 print_proxy( "%d,\n", stubs
+3);
579 print_proxy( "&%s_table[-3],\n", iface
->name
);
581 print_proxy( "},\n", iface
->name
);
584 print_proxy( "CStdStubBuffer_METHODS\n");
588 print_proxy( "};\n");
592 void write_proxies(ifref_list_t
*ifaces
)
595 char *file_id
= proxy_token
;
597 unsigned int proc_offset
= 0, type_offset
= 0;
599 if (!do_proxies
) return;
600 if (do_everything
&& !ifaces
) return;
606 LIST_FOR_EACH_ENTRY( cur
, ifaces
, ifref_t
, entry
)
607 if (is_object(cur
->iface
->attrs
) && !is_local(cur
->iface
->attrs
))
608 write_proxy(cur
->iface
, &proc_offset
, &type_offset
);
612 print_proxy( "#if !defined(__RPC_WIN32__)\n");
613 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
614 print_proxy( "#endif\n");
616 write_procformatstring(proxy
, ifaces
, 1);
617 write_typeformatstring(proxy
, ifaces
, 1);
619 fprintf(proxy
, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id
);
620 fprintf(proxy
, "{\n");
622 LIST_FOR_EACH_ENTRY( cur
, ifaces
, ifref_t
, entry
)
623 if(cur
->iface
->ref
&& cur
->iface
->funcs
&&
624 is_object(cur
->iface
->attrs
) && !is_local(cur
->iface
->attrs
))
625 fprintf(proxy
, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", cur
->iface
->name
);
627 fprintf(proxy
, " 0\n");
628 fprintf(proxy
, "};\n");
629 fprintf(proxy
, "\n");
631 fprintf(proxy
, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id
);
632 fprintf(proxy
, "{\n");
634 LIST_FOR_EACH_ENTRY( cur
, ifaces
, ifref_t
, entry
)
635 if(cur
->iface
->ref
&& cur
->iface
->funcs
&&
636 is_object(cur
->iface
->attrs
) && !is_local(cur
->iface
->attrs
))
637 fprintf(proxy
, " (const CInterfaceStubVtbl*)&_%sStubVtbl,\n", cur
->iface
->name
);
638 fprintf(proxy
, " 0\n");
639 fprintf(proxy
, "};\n");
640 fprintf(proxy
, "\n");
642 fprintf(proxy
, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id
);
643 fprintf(proxy
, "{\n");
645 LIST_FOR_EACH_ENTRY( cur
, ifaces
, ifref_t
, entry
)
646 if(cur
->iface
->ref
&& cur
->iface
->funcs
&&
647 is_object(cur
->iface
->attrs
) && !is_local(cur
->iface
->attrs
))
648 fprintf(proxy
, " \"%s\",\n", cur
->iface
->name
);
649 fprintf(proxy
, " 0\n");
650 fprintf(proxy
, "};\n");
651 fprintf(proxy
, "\n");
653 fprintf(proxy
, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_%s, pIID, n)\n", file_id
, file_id
);
654 fprintf(proxy
, "\n");
655 fprintf(proxy
, "int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id
);
656 fprintf(proxy
, "{\n");
659 LIST_FOR_EACH_ENTRY( cur
, ifaces
, ifref_t
, entry
)
662 fprintf(proxy
, " if (!_%s_CHECK_IID(%d))\n", file_id
, c
);
663 fprintf(proxy
, " {\n");
664 fprintf(proxy
, " *pIndex = %d;\n", c
);
665 fprintf(proxy
, " return 1;\n");
666 fprintf(proxy
, " }\n");
669 fprintf(proxy
, " return 0;\n");
670 fprintf(proxy
, "}\n");
671 fprintf(proxy
, "\n");
673 fprintf(proxy
, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id
);
674 fprintf(proxy
, "{\n");
675 fprintf(proxy
, " (const PCInterfaceProxyVtblList*)&_%s_ProxyVtblList,\n", file_id
);
676 fprintf(proxy
, " (const PCInterfaceStubVtblList*)&_%s_StubVtblList,\n", file_id
);
677 fprintf(proxy
, " _%s_InterfaceNamesList,\n", file_id
);
678 fprintf(proxy
, " 0,\n");
679 fprintf(proxy
, " &_%s_IID_Lookup,\n", file_id
);
680 fprintf(proxy
, " %d,\n", c
);
681 fprintf(proxy
, " 1,\n");
682 fprintf(proxy
, " 0,\n");
683 fprintf(proxy
, " 0,\n");
684 fprintf(proxy
, " 0,\n");
685 fprintf(proxy
, " 0\n");
686 fprintf(proxy
, "};\n");