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 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(ifref_list_t
*ifaces
)
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 write_formatstringsdecl(proxy
, indent
, ifaces
, need_proxy
);
114 write_stubdescproto();
117 static void clear_output_vars( const var_list_t
*args
)
122 LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
124 if (is_attr(arg
->attrs
, ATTR_OUT
) && !is_attr(arg
->attrs
, ATTR_IN
)) {
125 print_proxy( "if(%s)\n", arg
->name
);
127 print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg
->name
, arg
->name
);
133 int is_var_ptr(const var_t
*v
)
135 return is_ptr(v
->type
);
138 int cant_be_null(const var_t
*v
)
140 /* Search backwards for the most recent pointer attribute. */
141 const attr_list_t
*attrs
= v
->attrs
;
142 const type_t
*type
= v
->type
;
152 int t
= get_attrv(attrs
, ATTR_POINTERTYPE
);
154 if (t
== RPC_FC_FP
|| t
== RPC_FC_OP
|| t
== RPC_FC_UP
)
169 return 1; /* Default is RPC_FC_RP. */
172 static void proxy_check_pointers( const var_list_t
*args
)
177 LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
179 if (is_var_ptr(arg
) && cant_be_null(arg
)) {
180 print_proxy( "if(!%s)\n", arg
->name
);
182 print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
188 static void free_variable( const var_t
*arg
)
190 unsigned int type_offset
= arg
->type
->typestring_offset
;
192 type_t
*type
= arg
->type
;
193 expr_t
*size
= get_size_is_expr(type
, arg
->name
);
197 print_proxy( "_StubMsg.MaxCount = " );
198 write_expr(proxy
, size
, 0);
199 fprintf(proxy
, ";\n\n");
200 print_proxy( "NdrClearOutParameters( &_StubMsg, ");
201 fprintf(proxy
, "&__MIDL_TypeFormatString.Format[%u], ", type_offset
);
202 fprintf(proxy
, "(void*)%s );\n", arg
->name
);
222 iid
= get_attrp( arg
->attrs
, ATTR_IIDIS
);
225 print_proxy( "_StubMsg.MaxCount = (unsigned long) " );
226 write_expr(proxy
, iid
, 1);
227 print_proxy( ";\n\n" );
229 print_proxy( "NdrClearOutParameters( &_StubMsg, ");
230 fprintf(proxy
, "&__MIDL_TypeFormatString.Format[%u], ", type_offset
);
231 fprintf(proxy
, "(void*)%s );\n", arg
->name
);
235 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__
, arg
->name
, type
->type
);
239 static void proxy_free_variables( var_list_t
*args
)
244 LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
245 if (is_attr(arg
->attrs
, ATTR_OUT
))
247 free_variable( arg
);
248 fprintf(proxy
, "\n");
252 static void gen_proxy(type_t
*iface
, const func_t
*cur
, int idx
,
253 unsigned int proc_offset
)
255 var_t
*def
= cur
->def
;
256 int has_ret
= !is_void(def
->type
);
259 write_type_decl_left(proxy
, def
->type
);
260 print_proxy( " STDMETHODCALLTYPE %s_", iface
->name
);
261 write_name(proxy
, def
);
262 print_proxy( "_Proxy(\n");
263 write_args(proxy
, cur
->args
, iface
->name
, 1, TRUE
);
267 /* local variables */
270 write_type_decl_left(proxy
, def
->type
);
271 print_proxy( " _RetVal;\n");
273 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
274 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n" );
276 if (decl_indirect(def
->type
))
277 print_proxy("void *_p_%s = &%s;\n",
278 "_RetVal", "_RetVal");
283 clear_output_vars( cur
->args
);
285 print_proxy( "RpcTryExcept\n" );
286 print_proxy( "{\n" );
288 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &_StubMsg, &Object_StubDesc, %d);\n", idx
);
289 proxy_check_pointers( cur
->args
);
291 print_proxy( "RpcTryFinally\n" );
292 print_proxy( "{\n" );
295 write_remoting_arguments(proxy
, indent
, cur
, PASS_IN
, PHASE_BUFFERSIZE
);
297 print_proxy( "NdrProxyGetBuffer(This, &_StubMsg);\n" );
299 write_remoting_arguments(proxy
, indent
, cur
, PASS_IN
, PHASE_MARSHAL
);
301 print_proxy( "NdrProxySendReceive(This, &_StubMsg);\n" );
302 fprintf(proxy
, "\n");
303 print_proxy( "_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
304 print_proxy( "_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
306 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
308 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset
);
310 fprintf(proxy
, "\n");
312 write_remoting_arguments(proxy
, indent
, cur
, PASS_OUT
, PHASE_UNMARSHAL
);
316 if (decl_indirect(def
->type
))
317 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
318 else if (is_ptr(def
->type
) || is_array(def
->type
))
319 print_proxy("%s = 0;\n", "_RetVal");
320 write_remoting_arguments(proxy
, indent
, cur
, PASS_RETURN
, PHASE_UNMARSHAL
);
325 print_proxy( "RpcFinally\n" );
326 print_proxy( "{\n" );
328 print_proxy( "NdrProxyFreeBuffer(This, &_StubMsg);\n" );
331 print_proxy( "RpcEndFinally\n" );
333 print_proxy( "}\n" );
334 print_proxy( "RpcExcept(_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
335 print_proxy( "{\n" );
338 proxy_free_variables( cur
->args
);
339 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
342 print_proxy( "}\n" );
343 print_proxy( "RpcEndExcept\n" );
346 print_proxy( "return _RetVal;\n" );
353 static void gen_stub(type_t
*iface
, const func_t
*cur
, const char *cas
,
354 unsigned int proc_offset
)
356 var_t
*def
= cur
->def
;
358 int has_ret
= !is_void(def
->type
);
361 print_proxy( "void __RPC_STUB %s_", iface
->name
);
362 write_name(proxy
, def
);
363 print_proxy( "_Stub(\n");
365 print_proxy( "IRpcStubBuffer* This,\n");
366 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
367 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
368 print_proxy( "DWORD* _pdwStubPhase)\n");
372 print_proxy("%s * _This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n", iface
->name
, iface
->name
);
373 print_proxy("MIDL_STUB_MESSAGE _StubMsg;\n");
374 declare_stub_args( proxy
, indent
, cur
);
375 fprintf(proxy
, "\n");
379 print_proxy("NdrStubInitialize(_pRpcMessage, &_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
380 fprintf(proxy
, "\n");
382 write_parameters_init(proxy
, indent
, cur
);
384 print_proxy("RpcTryFinally\n");
387 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
389 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset
);
391 fprintf(proxy
, "\n");
393 write_remoting_arguments(proxy
, indent
, cur
, PASS_IN
, PHASE_UNMARSHAL
);
394 fprintf(proxy
, "\n");
396 assign_stub_out_args( proxy
, indent
, cur
);
398 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
399 fprintf(proxy
, "\n");
401 if (has_ret
) fprintf(proxy
, "_RetVal = ");
402 if (cas
) fprintf(proxy
, "%s_%s_Stub", iface
->name
, cas
);
405 fprintf(proxy
, "_This->lpVtbl->");
406 write_name(proxy
, def
);
408 fprintf(proxy
, "(_This");
412 LIST_FOR_EACH_ENTRY( arg
, cur
->args
, const var_t
, entry
)
414 fprintf(proxy
, ", ");
415 if (arg
->type
->declarray
)
417 write_name(proxy
, arg
);
420 fprintf(proxy
, ");\n");
421 fprintf(proxy
, "\n");
422 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
423 fprintf(proxy
, "\n");
425 write_remoting_arguments(proxy
, indent
, cur
, PASS_OUT
, PHASE_BUFFERSIZE
);
427 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &_StubMsg);\n");
429 write_remoting_arguments(proxy
, indent
, cur
, PASS_OUT
, PHASE_MARSHAL
);
430 fprintf(proxy
, "\n");
433 print_phase_basetype(proxy
, indent
, PHASE_MARSHAL
, PASS_RETURN
, def
, "_RetVal");
437 print_proxy("RpcFinally\n");
440 write_remoting_arguments(proxy
, indent
+1, cur
, PASS_OUT
, PHASE_FREE
);
443 print_proxy("RpcEndFinally\n");
445 print_proxy("_pRpcMessage->BufferLength = _StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
452 static int write_proxy_methods(type_t
*iface
)
457 if (iface
->ref
) i
= write_proxy_methods(iface
->ref
);
458 if (iface
->funcs
) LIST_FOR_EACH_ENTRY( cur
, iface
->funcs
, const func_t
, entry
) {
459 var_t
*def
= cur
->def
;
460 if (!is_callas(def
->attrs
)) {
461 if (i
) fprintf(proxy
, ",\n");
462 print_proxy( "%s_", iface
->name
);
463 write_name(proxy
, def
);
464 fprintf(proxy
, "_Proxy");
471 static int write_stub_methods(type_t
*iface
)
476 if (iface
->ref
) i
= write_stub_methods(iface
->ref
);
477 else return i
; /* skip IUnknown */
479 if (iface
->funcs
) LIST_FOR_EACH_ENTRY( cur
, iface
->funcs
, const func_t
, entry
) {
480 var_t
*def
= cur
->def
;
481 if (!is_local(def
->attrs
)) {
482 if (i
) fprintf(proxy
,",\n");
483 print_proxy( "%s_", iface
->name
);
484 write_name(proxy
, def
);
485 fprintf(proxy
, "_Stub");
492 static void write_proxy(type_t
*iface
, unsigned int *proc_offset
)
494 int midx
= -1, stubs
;
497 if (!iface
->funcs
) return;
499 /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
501 fprintf(proxy
, "/*****************************************************************************\n");
502 fprintf(proxy
, " * %s interface\n", iface
->name
);
503 fprintf(proxy
, " */\n");
504 LIST_FOR_EACH_ENTRY( cur
, iface
->funcs
, const func_t
, entry
)
506 const var_t
*def
= cur
->def
;
507 if (!is_local(def
->attrs
)) {
508 const var_t
*cas
= is_callas(def
->attrs
);
509 const char *cname
= cas
? cas
->name
: NULL
;
513 LIST_FOR_EACH_ENTRY( m
, iface
->funcs
, const func_t
, entry
)
514 if (!strcmp(m
->def
->name
, cname
))
520 gen_proxy(iface
, cur
, idx
, *proc_offset
);
521 gen_stub(iface
, cur
, cname
, *proc_offset
);
522 *proc_offset
+= get_size_procformatstring_func( cur
);
523 if (midx
== -1) midx
= idx
;
524 else if (midx
!= idx
) error("method index mismatch in write_proxy\n");
530 print_proxy( "static const CINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n", midx
, iface
->name
);
533 print_proxy( "{\n", iface
->name
);
535 print_proxy( "&IID_%s,\n", iface
->name
);
537 print_proxy( "},\n");
540 write_proxy_methods(iface
);
541 fprintf(proxy
, "\n");
545 print_proxy( "};\n");
546 fprintf(proxy
, "\n\n");
549 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface
->name
);
552 stubs
= write_stub_methods(iface
);
553 fprintf(proxy
, "\n");
555 fprintf(proxy
, "};\n");
557 print_proxy( "static const CInterfaceStubVtbl _%sStubVtbl =\n", iface
->name
);
562 print_proxy( "&IID_%s,\n", iface
->name
);
563 print_proxy( "0,\n");
564 print_proxy( "%d,\n", stubs
+3);
565 print_proxy( "&%s_table[-3],\n", iface
->name
);
567 print_proxy( "},\n", iface
->name
);
570 print_proxy( "CStdStubBuffer_METHODS\n");
574 print_proxy( "};\n");
578 static int does_any_iface(const ifref_list_t
*ifaces
, type_pred_t pred
)
583 LIST_FOR_EACH_ENTRY(ir
, ifaces
, ifref_t
, entry
)
590 int need_proxy(const type_t
*iface
)
592 return is_object(iface
->attrs
) && !is_local(iface
->attrs
);
595 int need_stub(const type_t
*iface
)
597 return !is_object(iface
->attrs
) && !is_local(iface
->attrs
);
600 int need_proxy_file(const ifref_list_t
*ifaces
)
602 return does_any_iface(ifaces
, need_proxy
);
605 int need_stub_files(const ifref_list_t
*ifaces
)
607 return does_any_iface(ifaces
, need_stub
);
610 void write_proxies(ifref_list_t
*ifaces
)
613 int expr_eval_routines
;
614 char *file_id
= proxy_token
;
616 unsigned int proc_offset
= 0;
618 if (!do_proxies
) return;
619 if (do_everything
&& !need_proxy_file(ifaces
)) return;
625 LIST_FOR_EACH_ENTRY( cur
, ifaces
, ifref_t
, entry
)
626 if (need_proxy(cur
->iface
))
627 write_proxy(cur
->iface
, &proc_offset
);
629 expr_eval_routines
= write_expr_eval_routines(proxy
, proxy_token
);
630 if (expr_eval_routines
)
631 write_expr_eval_routine_list(proxy
, proxy_token
);
632 write_user_quad_list(proxy
);
633 write_stubdesc(expr_eval_routines
);
635 print_proxy( "#if !defined(__RPC_WIN32__)\n");
636 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
637 print_proxy( "#endif\n");
639 write_procformatstring(proxy
, ifaces
, need_proxy
);
640 write_typeformatstring(proxy
, ifaces
, need_proxy
);
642 fprintf(proxy
, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\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
&& need_proxy(cur
->iface
))
647 fprintf(proxy
, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", cur
->iface
->name
);
649 fprintf(proxy
, " 0\n");
650 fprintf(proxy
, "};\n");
651 fprintf(proxy
, "\n");
653 fprintf(proxy
, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id
);
654 fprintf(proxy
, "{\n");
656 LIST_FOR_EACH_ENTRY( cur
, ifaces
, ifref_t
, entry
)
657 if(cur
->iface
->ref
&& cur
->iface
->funcs
&& need_proxy(cur
->iface
))
658 fprintf(proxy
, " (const CInterfaceStubVtbl*)&_%sStubVtbl,\n", cur
->iface
->name
);
659 fprintf(proxy
, " 0\n");
660 fprintf(proxy
, "};\n");
661 fprintf(proxy
, "\n");
663 fprintf(proxy
, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id
);
664 fprintf(proxy
, "{\n");
666 LIST_FOR_EACH_ENTRY( cur
, ifaces
, ifref_t
, entry
)
667 if(cur
->iface
->ref
&& cur
->iface
->funcs
&& need_proxy(cur
->iface
))
668 fprintf(proxy
, " \"%s\",\n", cur
->iface
->name
);
669 fprintf(proxy
, " 0\n");
670 fprintf(proxy
, "};\n");
671 fprintf(proxy
, "\n");
673 fprintf(proxy
, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_%s, pIID, n)\n", file_id
, file_id
);
674 fprintf(proxy
, "\n");
675 fprintf(proxy
, "int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id
);
676 fprintf(proxy
, "{\n");
679 LIST_FOR_EACH_ENTRY( cur
, ifaces
, ifref_t
, entry
)
680 if(cur
->iface
->ref
&& cur
->iface
->funcs
&& need_proxy(cur
->iface
))
682 fprintf(proxy
, " if (!_%s_CHECK_IID(%d))\n", file_id
, c
);
683 fprintf(proxy
, " {\n");
684 fprintf(proxy
, " *pIndex = %d;\n", c
);
685 fprintf(proxy
, " return 1;\n");
686 fprintf(proxy
, " }\n");
689 fprintf(proxy
, " return 0;\n");
690 fprintf(proxy
, "}\n");
691 fprintf(proxy
, "\n");
693 fprintf(proxy
, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id
);
694 fprintf(proxy
, "{\n");
695 fprintf(proxy
, " (const PCInterfaceProxyVtblList*)&_%s_ProxyVtblList,\n", file_id
);
696 fprintf(proxy
, " (const PCInterfaceStubVtblList*)&_%s_StubVtblList,\n", file_id
);
697 fprintf(proxy
, " _%s_InterfaceNamesList,\n", file_id
);
698 fprintf(proxy
, " 0,\n");
699 fprintf(proxy
, " &_%s_IID_Lookup,\n", file_id
);
700 fprintf(proxy
, " %d,\n", c
);
701 fprintf(proxy
, " 1,\n");
702 fprintf(proxy
, " 0,\n");
703 fprintf(proxy
, " 0,\n");
704 fprintf(proxy
, " 0,\n");
705 fprintf(proxy
, " 0\n");
706 fprintf(proxy
, "};\n");