push c6fcfc519a04d046be60ec60e33d075a2146cc03
[wine/hacks.git] / tools / widl / proxy.c
blob0dcb7378d0acf1a4556e7c36c05804001bb7d458
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 /* FIXME: support generation of stubless proxies */
53 static void print_proxy( const char *format, ... )
55 va_list va;
56 va_start( va, format );
57 print( proxy, indent, format, va );
58 va_end( va );
61 static void write_stubdescproto(void)
63 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
64 print_proxy( "\n");
67 static void write_stubdesc(int expr_eval_routines)
69 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc =\n{\n");
70 indent++;
71 print_proxy( "0,\n");
72 print_proxy( "NdrOleAllocate,\n");
73 print_proxy( "NdrOleFree,\n");
74 print_proxy( "{0}, 0, 0, %s, 0,\n", expr_eval_routines ? "ExprEvalRoutines" : "0");
75 print_proxy( "__MIDL_TypeFormatString.Format,\n");
76 print_proxy( "1, /* -error bounds_check flag */\n");
77 print_proxy( "0x10001, /* Ndr library version */\n");
78 print_proxy( "0,\n");
79 print_proxy( "0x50100a4, /* MIDL Version 5.1.164 */\n");
80 print_proxy( "0,\n");
81 print_proxy("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
82 print_proxy( "0, /* notify & notify_flag routine table */\n");
83 print_proxy( "1, /* Flags */\n");
84 print_proxy( "0, /* Reserved3 */\n");
85 print_proxy( "0, /* Reserved4 */\n");
86 print_proxy( "0 /* Reserved5 */\n");
87 indent--;
88 print_proxy( "};\n");
89 print_proxy( "\n");
92 static void init_proxy(const statement_list_t *stmts)
94 if (proxy) return;
95 if(!(proxy = fopen(proxy_name, "w")))
96 error("Could not open %s for output\n", proxy_name);
97 print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
98 print_proxy( "\n");
99 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
100 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ 440\n");
101 print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
102 print_proxy( "\n");
103 print_proxy( "#define __midl_proxy\n");
104 print_proxy( "#include \"objbase.h\"\n");
105 print_proxy( "#include \"rpcproxy.h\"\n");
106 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
107 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
108 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
109 print_proxy( "\n");
110 print_proxy( "#include \"%s\"\n", header_name);
111 print_proxy( "\n");
112 print_proxy( "#ifndef DECLSPEC_HIDDEN\n");
113 print_proxy( "#define DECLSPEC_HIDDEN\n");
114 print_proxy( "#endif\n");
115 print_proxy( "\n");
116 write_exceptions( proxy );
117 print_proxy( "\n");
118 print_proxy( "struct __proxy_frame\n");
119 print_proxy( "{\n");
120 print_proxy( " __DECL_EXCEPTION_FRAME\n");
121 print_proxy( " MIDL_STUB_MESSAGE _StubMsg;\n");
122 print_proxy( " void *This;\n");
123 print_proxy( "};\n");
124 print_proxy( "\n");
125 print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
126 print_proxy( "{\n");
127 print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
128 print_proxy( "}\n");
129 print_proxy( "\n");
132 static void clear_output_vars( const var_list_t *args )
134 const var_t *arg;
136 if (!args) return;
137 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
139 if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
140 print_proxy( "if(%s)\n", arg->name );
141 indent++;
142 print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name );
143 indent--;
148 int is_var_ptr(const var_t *v)
150 return is_ptr(v->type);
153 int cant_be_null(const var_t *v)
155 /* Search backwards for the most recent pointer attribute. */
156 const attr_list_t *attrs = v->attrs;
157 const type_t *type = v->type;
159 /* context handles have their own checking so they can be null for the
160 * purposes of null ref pointer checking */
161 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
162 return 0;
164 if (is_user_type(type))
165 return 0;
167 if (!attrs && is_ptr(type))
169 attrs = type->attrs;
170 type = type_pointer_get_ref(type);
173 while (attrs)
175 int t = get_attrv(attrs, ATTR_POINTERTYPE);
177 if (t == RPC_FC_FP || t == RPC_FC_OP || t == RPC_FC_UP)
178 return 0;
180 if (t == RPC_FC_RP)
181 return 1;
183 if (is_ptr(type))
185 attrs = type->attrs;
186 type = type_pointer_get_ref(type);
188 else
189 attrs = NULL;
192 return 1; /* Default is RPC_FC_RP. */
195 static int need_delegation(const type_t *iface)
197 return type_iface_get_inherit(iface) &&
198 type_iface_get_inherit(type_iface_get_inherit(iface)) &&
199 type_iface_get_inherit(iface)->ignore;
202 static int get_delegation_indirect(const type_t *iface, const type_t ** delegate_to)
204 const type_t * cur_iface;
205 for (cur_iface = iface; cur_iface != NULL; cur_iface = type_iface_get_inherit(cur_iface))
206 if (need_delegation(cur_iface))
208 if(delegate_to)
209 *delegate_to = type_iface_get_inherit(cur_iface);
210 return 1;
212 return 0;
215 static int need_delegation_indirect(const type_t *iface)
217 return get_delegation_indirect(iface, NULL);
220 static void proxy_check_pointers( const var_list_t *args )
222 const var_t *arg;
224 if (!args) return;
225 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
227 if (is_var_ptr(arg) && cant_be_null(arg)) {
228 print_proxy( "if(!%s)\n", arg->name );
229 indent++;
230 print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
231 indent--;
236 static void free_variable( const var_t *arg, const char *local_var_prefix )
238 unsigned int type_offset = arg->type->typestring_offset;
239 expr_t *iid;
240 type_t *type = arg->type;
241 expr_t *size = get_size_is_expr(type, arg->name);
243 if (size)
245 print_proxy( "__frame->_StubMsg.MaxCount = " );
246 write_expr(proxy, size, 0, 1, NULL, NULL, local_var_prefix);
247 fprintf(proxy, ";\n\n");
248 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
249 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
250 fprintf(proxy, "(void*)%s );\n", arg->name );
251 return;
254 switch( type->type )
256 case RPC_FC_BYTE:
257 case RPC_FC_CHAR:
258 case RPC_FC_WCHAR:
259 case RPC_FC_SHORT:
260 case RPC_FC_USHORT:
261 case RPC_FC_ENUM16:
262 case RPC_FC_LONG:
263 case RPC_FC_ULONG:
264 case RPC_FC_ENUM32:
265 case RPC_FC_STRUCT:
266 break;
268 case RPC_FC_FP:
269 case RPC_FC_IP:
270 iid = get_attrp( arg->attrs, ATTR_IIDIS );
271 if( iid )
273 print_proxy( "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
274 write_expr(proxy, iid, 1, 1, NULL, NULL, local_var_prefix);
275 print_proxy( ";\n\n" );
277 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
278 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
279 fprintf(proxy, "(void*)%s );\n", arg->name );
280 break;
282 default:
283 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
287 static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
289 const var_t *arg;
291 if (!args) return;
292 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
293 if (is_attr(arg->attrs, ATTR_OUT))
295 free_variable( arg, local_var_prefix );
296 fprintf(proxy, "\n");
300 static void gen_proxy(type_t *iface, const var_t *func, int idx,
301 unsigned int proc_offset)
303 int has_ret = !is_void(type_function_get_rettype(func->type));
304 int has_full_pointer = is_full_pointer_function(func);
305 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
306 if (!callconv) callconv = "";
308 indent = 0;
309 print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
310 iface->name, get_name(func) );
311 print_proxy( "{\n");
312 indent++;
313 if (has_full_pointer) write_full_pointer_free(proxy, indent, func);
314 print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
315 indent--;
316 print_proxy( "}\n");
317 print_proxy( "\n");
319 write_type_decl_left(proxy, type_function_get_rettype(func->type));
320 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
321 write_args(proxy, type_get_function_args(func->type), iface->name, 1, TRUE);
322 print_proxy( ")\n");
323 print_proxy( "{\n");
324 indent ++;
325 print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
326 /* local variables */
327 if (has_ret) {
328 print_proxy( "" );
329 write_type_decl_left(proxy, type_function_get_rettype(func->type));
330 print_proxy( " _RetVal;\n");
332 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
333 if (has_ret) {
334 if (decl_indirect(type_function_get_rettype(func->type)))
335 print_proxy("void *_p_%s = &%s;\n",
336 "_RetVal", "_RetVal");
338 print_proxy( "\n");
340 print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(func) );
341 print_proxy( "__frame->This = This;\n" );
343 if (has_full_pointer)
344 write_full_pointer_init(proxy, indent, func, FALSE);
346 /* FIXME: trace */
347 clear_output_vars( type_get_function_args(func->type) );
349 print_proxy( "RpcTryExcept\n" );
350 print_proxy( "{\n" );
351 indent++;
352 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx);
353 proxy_check_pointers( type_get_function_args(func->type) );
355 print_proxy( "RpcTryFinally\n" );
356 print_proxy( "{\n" );
357 indent++;
359 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
361 print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
363 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_MARSHAL);
365 print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
366 fprintf(proxy, "\n");
367 print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
368 print_proxy( "__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
370 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
371 indent++;
372 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
373 indent--;
374 fprintf(proxy, "\n");
376 write_remoting_arguments(proxy, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
378 if (has_ret)
380 if (decl_indirect(type_function_get_rettype(func->type)))
381 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
382 else if (is_ptr(type_function_get_rettype(func->type)) ||
383 is_array(type_function_get_rettype(func->type)))
384 print_proxy("%s = 0;\n", "_RetVal");
385 write_remoting_arguments(proxy, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
388 indent--;
389 print_proxy( "}\n");
390 print_proxy( "RpcFinally\n" );
391 print_proxy( "{\n" );
392 indent++;
393 print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(func) );
394 indent--;
395 print_proxy( "}\n");
396 print_proxy( "RpcEndFinally\n" );
397 indent--;
398 print_proxy( "}\n" );
399 print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
400 print_proxy( "{\n" );
401 if (has_ret) {
402 indent++;
403 proxy_free_variables( type_get_function_args(func->type), "" );
404 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
405 indent--;
407 print_proxy( "}\n" );
408 print_proxy( "RpcEndExcept\n" );
410 if (has_ret) {
411 print_proxy( "return _RetVal;\n" );
413 indent--;
414 print_proxy( "}\n");
415 print_proxy( "\n");
418 static void gen_stub(type_t *iface, const var_t *func, const char *cas,
419 unsigned int proc_offset)
421 const var_t *arg;
422 int has_ret = !is_void(type_function_get_rettype(func->type));
423 int has_full_pointer = is_full_pointer_function(func);
425 indent = 0;
426 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(func));
427 indent++;
428 print_proxy( "__DECL_EXCEPTION_FRAME\n" );
429 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
430 print_proxy( "%s * _This;\n", iface->name );
431 declare_stub_args( proxy, indent, func );
432 indent--;
433 print_proxy( "};\n\n" );
435 print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(func) );
436 print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(func) );
437 indent++;
438 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
439 if (has_full_pointer)
440 write_full_pointer_free(proxy, indent, func);
441 indent--;
442 print_proxy( "}\n\n" );
444 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
445 indent++;
446 print_proxy( "IRpcStubBuffer* This,\n");
447 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
448 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
449 print_proxy( "DWORD* _pdwStubPhase)\n");
450 indent--;
451 print_proxy( "{\n");
452 indent++;
453 print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
454 iface->name, get_name(func) );
456 print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
458 /* FIXME: trace */
460 print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
461 fprintf(proxy, "\n");
462 print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(func) );
464 write_parameters_init(proxy, indent, func, "__frame->");
466 print_proxy("RpcTryFinally\n");
467 print_proxy("{\n");
468 indent++;
469 if (has_full_pointer)
470 write_full_pointer_init(proxy, indent, func, TRUE);
471 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
472 indent++;
473 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
474 indent--;
475 fprintf(proxy, "\n");
477 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
478 fprintf(proxy, "\n");
480 assign_stub_out_args( proxy, indent, func, "__frame->" );
482 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
483 fprintf(proxy, "\n");
484 print_proxy("");
485 if (has_ret) fprintf(proxy, "__frame->_RetVal = ");
486 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
487 else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(func));
488 fprintf(proxy, "(__frame->_This");
490 if (type_get_function_args(func->type))
492 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
493 fprintf(proxy, ", %s__frame->%s", arg->type->declarray ? "*" : "", arg->name);
495 fprintf(proxy, ");\n");
496 fprintf(proxy, "\n");
497 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
498 fprintf(proxy, "\n");
500 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
502 if (!is_void(type_function_get_rettype(func->type)))
503 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
505 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
507 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
508 fprintf(proxy, "\n");
510 /* marshall the return value */
511 if (!is_void(type_function_get_rettype(func->type)))
512 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
514 indent--;
515 print_proxy("}\n");
516 print_proxy("RpcFinally\n");
517 print_proxy("{\n");
518 indent++;
519 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(func) );
520 indent--;
521 print_proxy("}\n");
522 print_proxy("RpcEndFinally\n");
524 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
525 indent--;
527 print_proxy("}\n");
528 print_proxy("\n");
531 static int count_methods(type_t *iface)
533 const statement_t *stmt;
534 int count = 0;
536 if (type_iface_get_inherit(iface))
537 count = count_methods(type_iface_get_inherit(iface));
539 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
540 const var_t *func = stmt->u.var;
541 if (!is_callas(func->attrs)) count++;
543 return count;
546 static const statement_t * get_callas_source(const type_t * iface, const var_t * def)
548 const statement_t * source;
549 STATEMENTS_FOR_EACH_FUNC( source, type_iface_get_stmts(iface)) {
550 const var_t * cas = is_callas(source->u.var->attrs );
551 if (cas && !strcmp(def->name, cas->name))
552 return source;
554 return NULL;
557 static int write_proxy_methods(type_t *iface, int skip)
559 const statement_t *stmt;
560 int i = 0;
562 if (type_iface_get_inherit(iface))
563 i = write_proxy_methods(type_iface_get_inherit(iface),
564 need_delegation(iface));
565 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
566 const var_t *func = stmt->u.var;
567 if (!is_callas(func->attrs)) {
568 if (i != func->type->details.function->idx )
569 error("widl internal error: method index mismatch\n");
570 if (i) fprintf(proxy, ",\n");
571 if (skip || (is_local(func->attrs) && !get_callas_source(iface, func)))
572 print_proxy( "0 /* %s_%s_Proxy */", iface->name, get_name(func));
573 else print_proxy( "%s_%s_Proxy", iface->name, get_name(func));
574 i++;
577 return i;
580 static int write_stub_methods(type_t *iface, int skip)
582 const statement_t *stmt;
583 int i = 0;
585 if (type_iface_get_inherit(iface))
586 i = write_stub_methods(type_iface_get_inherit(iface), need_delegation(iface));
587 else
588 return i; /* skip IUnknown */
590 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
591 const var_t *func = stmt->u.var;
592 if (!is_callas(func->attrs)) {
593 int missing = 0;
594 const char * fname = get_name(func);
595 if(is_local(func->attrs)) {
596 const statement_t * callas_source = get_callas_source(iface, func);
597 if(!callas_source)
598 missing = 1;
599 else
600 fname = get_name(callas_source->u.var);
602 if (i) fprintf(proxy,",\n");
603 if (skip || missing) print_proxy("STUB_FORWARDING_FUNCTION");
604 else print_proxy( "%s_%s_Stub", iface->name, fname);
605 i++;
608 return i;
611 static void write_proxy(type_t *iface, unsigned int *proc_offset)
613 int count;
614 const statement_t *stmt;
615 int first_func = 1;
617 /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
619 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
620 const var_t *func = stmt->u.var;
621 if (first_func) {
622 fprintf(proxy, "/*****************************************************************************\n");
623 fprintf(proxy, " * %s interface\n", iface->name);
624 fprintf(proxy, " */\n");
625 first_func = 0;
627 if (!is_local(func->attrs)) {
628 const var_t *cas = is_callas(func->attrs);
629 const char *cname = cas ? cas->name : NULL;
630 int idx = func->type->details.function->idx;
631 if (cname) {
632 const statement_t *stmt2;
633 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface)) {
634 const var_t *m = stmt2->u.var;
635 if (!strcmp(m->name, cname))
637 idx = m->type->details.function->idx;
638 break;
642 gen_proxy(iface, func, idx, *proc_offset);
643 gen_stub(iface, func, cname, *proc_offset);
644 *proc_offset += get_size_procformatstring_func( func );
648 count = count_methods(iface);
650 /* proxy vtable */
651 print_proxy( "static const CINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n", count, iface->name);
652 print_proxy( "{\n");
653 indent++;
654 print_proxy( "{\n", iface->name);
655 indent++;
656 print_proxy( "&IID_%s,\n", iface->name);
657 indent--;
658 print_proxy( "},\n");
659 print_proxy( "{\n");
660 indent++;
661 write_proxy_methods(iface, FALSE);
662 fprintf(proxy, "\n");
663 indent--;
664 print_proxy( "}\n");
665 indent--;
666 print_proxy( "};\n");
667 fprintf(proxy, "\n\n");
669 /* stub vtable */
670 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
671 print_proxy( "{\n");
672 indent++;
673 write_stub_methods(iface, FALSE);
674 fprintf(proxy, "\n");
675 indent--;
676 fprintf(proxy, "};\n");
677 print_proxy( "\n");
678 print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
679 need_delegation_indirect(iface) ? "" : "const ", iface->name);
680 print_proxy( "{\n");
681 indent++;
682 print_proxy( "{\n");
683 indent++;
684 print_proxy( "&IID_%s,\n", iface->name);
685 print_proxy( "0,\n");
686 print_proxy( "%d,\n", count);
687 print_proxy( "&%s_table[-3],\n", iface->name);
688 indent--;
689 print_proxy( "},\n", iface->name);
690 print_proxy( "{\n");
691 indent++;
692 print_proxy( "CStdStubBuffer_%s\n", need_delegation_indirect(iface) ? "DELEGATING_METHODS" : "METHODS");
693 indent--;
694 print_proxy( "}\n");
695 indent--;
696 print_proxy( "};\n");
697 print_proxy( "\n");
700 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
702 const statement_t *stmt;
704 if (stmts)
705 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
707 if (stmt->type == STMT_LIBRARY)
709 if (does_any_iface(stmt->u.lib->stmts, pred))
710 return TRUE;
712 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
714 if (pred(stmt->u.type))
715 return TRUE;
719 return FALSE;
722 int need_proxy(const type_t *iface)
724 return is_object(iface->attrs) && !is_local(iface->attrs);
727 int need_stub(const type_t *iface)
729 return !is_object(iface->attrs) && !is_local(iface->attrs);
732 int need_proxy_file(const statement_list_t *stmts)
734 return does_any_iface(stmts, need_proxy);
737 int need_stub_files(const statement_list_t *stmts)
739 return does_any_iface(stmts, need_stub);
742 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
744 const statement_t *stmt;
745 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
747 if (stmt->type == STMT_LIBRARY)
748 write_proxy_stmts(stmt->u.lib->stmts, proc_offset);
749 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
751 if (need_proxy(stmt->u.type))
752 write_proxy(stmt->u.type, proc_offset);
757 static int cmp_iid( const void *ptr1, const void *ptr2 )
759 const type_t * const *iface1 = ptr1;
760 const type_t * const *iface2 = ptr2;
761 const UUID *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
762 const UUID *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
763 return memcmp( uuid1, uuid2, sizeof(UUID) );
766 static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
768 const statement_t *stmt;
770 if (!stmts) return;
771 LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
773 if (stmt->type == STMT_LIBRARY)
774 build_iface_list(stmt->u.lib->stmts, ifaces, count);
775 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
777 type_t *iface = stmt->u.type;
778 if (type_iface_get_inherit(iface) && need_proxy(iface))
780 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(*ifaces) );
781 (*ifaces)[(*count)++] = iface;
787 static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
789 type_t **ifaces = NULL;
791 *count = 0;
792 build_iface_list( stmts, &ifaces, count );
793 qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
794 return ifaces;
797 static void write_proxy_routines(const statement_list_t *stmts)
799 int expr_eval_routines;
800 unsigned int proc_offset = 0;
802 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
803 write_stubdescproto();
804 write_proxy_stmts(stmts, &proc_offset);
806 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
807 if (expr_eval_routines)
808 write_expr_eval_routine_list(proxy, proxy_token);
809 write_user_quad_list(proxy);
810 write_stubdesc(expr_eval_routines);
812 print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
813 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
814 print_proxy( "#endif\n");
815 print_proxy( "\n");
816 write_procformatstring(proxy, stmts, need_proxy);
817 write_typeformatstring(proxy, stmts, need_proxy);
821 void write_proxies(const statement_list_t *stmts)
823 char *file_id = proxy_token;
824 int i, count, have_baseiid;
825 type_t **interfaces;
826 const type_t * delegate_to;
828 if (!do_proxies) return;
829 if (do_everything && !need_proxy_file(stmts)) return;
831 init_proxy(stmts);
832 if(!proxy) return;
834 if (do_win32 && do_win64)
836 fprintf(proxy, "\n#ifndef _WIN64\n\n");
837 pointer_size = 4;
838 write_proxy_routines( stmts );
839 fprintf(proxy, "\n#else /* _WIN64 */\n\n");
840 pointer_size = 8;
841 write_proxy_routines( stmts );
842 fprintf(proxy, "#endif /* _WIN64 */\n\n");
844 else if (do_win32)
846 pointer_size = 4;
847 write_proxy_routines( stmts );
849 else if (do_win64)
851 pointer_size = 8;
852 write_proxy_routines( stmts );
855 interfaces = sort_interfaces(stmts, &count);
856 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
857 fprintf(proxy, "{\n");
858 for (i = 0; i < count; i++)
859 fprintf(proxy, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
860 fprintf(proxy, " 0\n");
861 fprintf(proxy, "};\n");
862 fprintf(proxy, "\n");
864 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
865 fprintf(proxy, "{\n");
866 for (i = 0; i < count; i++)
867 fprintf(proxy, " &_%sStubVtbl,\n", interfaces[i]->name);
868 fprintf(proxy, " 0\n");
869 fprintf(proxy, "};\n");
870 fprintf(proxy, "\n");
872 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
873 fprintf(proxy, "{\n");
874 for (i = 0; i < count; i++)
875 fprintf(proxy, " \"%s\",\n", interfaces[i]->name);
876 fprintf(proxy, " 0\n");
877 fprintf(proxy, "};\n");
878 fprintf(proxy, "\n");
880 if ((have_baseiid = does_any_iface(stmts, need_delegation_indirect)))
882 fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
883 fprintf(proxy, "{\n");
884 for (i = 0; i < count; i++)
886 if (get_delegation_indirect(interfaces[i], &delegate_to))
887 fprintf( proxy, " &IID_%s, /* %s */\n", delegate_to->name, interfaces[i]->name );
888 else
889 fprintf( proxy, " 0,\n" );
891 fprintf(proxy, " 0\n");
892 fprintf(proxy, "};\n");
893 fprintf(proxy, "\n");
896 fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
897 fprintf(proxy, "{\n");
898 fprintf(proxy, " int low = 0, high = %d;\n", count - 1);
899 fprintf(proxy, "\n");
900 fprintf(proxy, " while (low <= high)\n");
901 fprintf(proxy, " {\n");
902 fprintf(proxy, " int pos = (low + high) / 2;\n");
903 fprintf(proxy, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
904 fprintf(proxy, " if (!res) { *pIndex = pos; return 1; }\n");
905 fprintf(proxy, " if (res > 0) low = pos + 1;\n");
906 fprintf(proxy, " else high = pos - 1;\n");
907 fprintf(proxy, " }\n");
908 fprintf(proxy, " return 0;\n");
909 fprintf(proxy, "}\n");
910 fprintf(proxy, "\n");
912 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo DECLSPEC_HIDDEN =\n", file_id);
913 fprintf(proxy, "{\n");
914 fprintf(proxy, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
915 fprintf(proxy, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
916 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
917 if (have_baseiid) fprintf(proxy, " _%s_BaseIIDList,\n", file_id);
918 else fprintf(proxy, " 0,\n");
919 fprintf(proxy, " _%s_IID_Lookup,\n", file_id);
920 fprintf(proxy, " %d,\n", count);
921 fprintf(proxy, " 1,\n");
922 fprintf(proxy, " 0,\n");
923 fprintf(proxy, " 0,\n");
924 fprintf(proxy, " 0,\n");
925 fprintf(proxy, " 0\n");
926 fprintf(proxy, "};\n");
928 fclose(proxy);