push b0b97fcd59eff07f047585692fa36859b459324f
[wine/hacks.git] / tools / widl / proxy.c
blobc5c79c32ec8cd4e5c3b011653cb673018cc8fd71
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 USE_COMPILER_EXCEPTIONS\n");
113 print_proxy( "\n");
114 print_proxy( "#include \"wine/exception.h\"\n");
115 print_proxy( "#undef RpcTryExcept\n");
116 print_proxy( "#undef RpcExcept\n");
117 print_proxy( "#undef RpcEndExcept\n");
118 print_proxy( "#undef RpcExceptionCode\n");
119 print_proxy( "\n");
120 print_proxy( "struct __proxy_frame\n");
121 print_proxy( "{\n");
122 print_proxy( " EXCEPTION_REGISTRATION_RECORD frame;\n");
123 print_proxy( " sigjmp_buf jmp;\n");
124 print_proxy( " DWORD code;\n");
125 print_proxy( " MIDL_STUB_MESSAGE *stub;\n");
126 print_proxy( "};\n");
127 print_proxy( "\n");
128 print_proxy( "static DWORD __proxy_exception_handler( EXCEPTION_RECORD *record,\n");
129 print_proxy( " EXCEPTION_REGISTRATION_RECORD *frame,\n");
130 print_proxy( " CONTEXT *context,\n");
131 print_proxy( " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
132 print_proxy( "{\n");
133 print_proxy( " struct __proxy_frame *proxy_frame = (struct __proxy_frame *)frame;\n");
134 print_proxy( "\n");
135 print_proxy( " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
136 print_proxy( " return ExceptionContinueSearch;\n");
137 print_proxy( " if (proxy_frame->stub->dwStubPhase == PROXY_SENDRECEIVE)\n");
138 print_proxy( " return ExceptionContinueSearch;\n");
139 print_proxy( "\n");
140 print_proxy( " proxy_frame->code = record->ExceptionCode;\n");
141 print_proxy( " __wine_rtl_unwind( frame, record );\n");
142 print_proxy( " __wine_pop_frame( frame );\n");
143 print_proxy( " siglongjmp( proxy_frame->jmp, 1 );\n");
144 print_proxy( "}\n");
145 print_proxy( "\n");
146 print_proxy( "#define RpcTryExcept \\\n");
147 print_proxy( " do { \\\n");
148 print_proxy( " struct __proxy_frame __proxy_frame; \\\n");
149 print_proxy( " __proxy_frame.frame.Handler = __proxy_exception_handler; \\\n");
150 print_proxy( " __proxy_frame.stub = &_StubMsg; \\\n");
151 print_proxy( " if (!sigsetjmp( __proxy_frame.jmp, 0 )) \\\n");
152 print_proxy( " { \\\n");
153 print_proxy( " __wine_push_frame( &__proxy_frame.frame ); \\\n");
154 print_proxy( " {\n");
155 print_proxy( "\n");
156 print_proxy( "#define RpcExcept(expr) \\\n");
157 print_proxy( " } \\\n");
158 print_proxy( " __wine_pop_frame( &__proxy_frame.frame ); \\\n");
159 print_proxy( " } \\\n");
160 print_proxy( " else \\\n");
161 print_proxy( " {\n");
162 print_proxy( "\n");
163 print_proxy( "#define RpcEndExcept \\\n");
164 print_proxy( " } \\\n");
165 print_proxy( " } while(0);\n");
166 print_proxy( "\n");
167 print_proxy( "#define RpcExceptionCode() (__proxy_frame.code)\n");
168 print_proxy( "\n");
169 print_proxy( "#endif /* USE_COMPILER_EXCEPTIONS */\n");
170 print_proxy( "\n");
171 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
172 write_stubdescproto();
175 static void clear_output_vars( const var_list_t *args )
177 const var_t *arg;
179 if (!args) return;
180 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
182 if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
183 print_proxy( "if(%s)\n", arg->name );
184 indent++;
185 print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name );
186 indent--;
191 int is_var_ptr(const var_t *v)
193 return is_ptr(v->type);
196 int cant_be_null(const var_t *v)
198 /* Search backwards for the most recent pointer attribute. */
199 const attr_list_t *attrs = v->attrs;
200 const type_t *type = v->type;
202 /* context handles have their own checking so they can be null for the
203 * purposes of null ref pointer checking */
204 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
205 return 0;
207 if (! attrs && type)
209 attrs = type->attrs;
210 type = type->ref;
213 while (attrs)
215 int t = get_attrv(attrs, ATTR_POINTERTYPE);
217 if (t == RPC_FC_FP || t == RPC_FC_OP || t == RPC_FC_UP)
218 return 0;
220 if (t == RPC_FC_RP)
221 return 1;
223 if (type)
225 attrs = type->attrs;
226 type = type->ref;
228 else
229 attrs = NULL;
232 return 1; /* Default is RPC_FC_RP. */
235 static void proxy_check_pointers( const var_list_t *args )
237 const var_t *arg;
239 if (!args) return;
240 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
242 if (is_var_ptr(arg) && cant_be_null(arg)) {
243 print_proxy( "if(!%s)\n", arg->name );
244 indent++;
245 print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
246 indent--;
251 static void free_variable( const var_t *arg )
253 unsigned int type_offset = arg->type->typestring_offset;
254 expr_t *iid;
255 type_t *type = arg->type;
256 expr_t *size = get_size_is_expr(type, arg->name);
258 if (size)
260 print_proxy( "_StubMsg.MaxCount = " );
261 write_expr(proxy, size, 0, 1, NULL, NULL);
262 fprintf(proxy, ";\n\n");
263 print_proxy( "NdrClearOutParameters( &_StubMsg, ");
264 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
265 fprintf(proxy, "(void*)%s );\n", arg->name );
266 return;
269 switch( type->type )
271 case RPC_FC_BYTE:
272 case RPC_FC_CHAR:
273 case RPC_FC_WCHAR:
274 case RPC_FC_SHORT:
275 case RPC_FC_USHORT:
276 case RPC_FC_ENUM16:
277 case RPC_FC_LONG:
278 case RPC_FC_ULONG:
279 case RPC_FC_ENUM32:
280 case RPC_FC_STRUCT:
281 break;
283 case RPC_FC_FP:
284 case RPC_FC_IP:
285 iid = get_attrp( arg->attrs, ATTR_IIDIS );
286 if( iid )
288 print_proxy( "_StubMsg.MaxCount = (unsigned long) " );
289 write_expr(proxy, iid, 1, 1, NULL, NULL);
290 print_proxy( ";\n\n" );
292 print_proxy( "NdrClearOutParameters( &_StubMsg, ");
293 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
294 fprintf(proxy, "(void*)%s );\n", arg->name );
295 break;
297 default:
298 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
302 static void proxy_free_variables( var_list_t *args )
304 const var_t *arg;
306 if (!args) return;
307 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
308 if (is_attr(arg->attrs, ATTR_OUT))
310 free_variable( arg );
311 fprintf(proxy, "\n");
315 static void gen_proxy(type_t *iface, const func_t *cur, int idx,
316 unsigned int proc_offset)
318 var_t *def = cur->def;
319 int has_ret = !is_void(get_func_return_type(cur));
320 int has_full_pointer = is_full_pointer_function(cur);
321 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
322 if (!callconv) callconv = "";
324 indent = 0;
325 write_type_decl_left(proxy, get_func_return_type(cur));
326 print_proxy( " %s %s_", callconv, iface->name);
327 write_name(proxy, def);
328 print_proxy( "_Proxy(\n");
329 write_args(proxy, cur->args, iface->name, 1, TRUE);
330 print_proxy( ")\n");
331 print_proxy( "{\n");
332 indent ++;
333 /* local variables */
334 if (has_ret) {
335 print_proxy( "" );
336 write_type_decl_left(proxy, get_func_return_type(cur));
337 print_proxy( " _RetVal;\n");
339 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
340 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n" );
341 if (has_ret) {
342 if (decl_indirect(get_func_return_type(cur)))
343 print_proxy("void *_p_%s = &%s;\n",
344 "_RetVal", "_RetVal");
346 print_proxy( "\n");
348 if (has_full_pointer)
349 write_full_pointer_init(proxy, indent, cur, FALSE);
351 /* FIXME: trace */
352 clear_output_vars( cur->args );
354 print_proxy( "RpcTryExcept\n" );
355 print_proxy( "{\n" );
356 indent++;
357 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &_StubMsg, &Object_StubDesc, %d);\n", idx);
358 proxy_check_pointers( cur->args );
360 print_proxy( "RpcTryFinally\n" );
361 print_proxy( "{\n" );
362 indent++;
364 write_remoting_arguments(proxy, indent, cur, PASS_IN, PHASE_BUFFERSIZE);
366 print_proxy( "NdrProxyGetBuffer(This, &_StubMsg);\n" );
368 write_remoting_arguments(proxy, indent, cur, PASS_IN, PHASE_MARSHAL);
370 print_proxy( "NdrProxySendReceive(This, &_StubMsg);\n" );
371 fprintf(proxy, "\n");
372 print_proxy( "_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
373 print_proxy( "_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
375 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
376 indent++;
377 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
378 indent--;
379 fprintf(proxy, "\n");
381 write_remoting_arguments(proxy, indent, cur, PASS_OUT, PHASE_UNMARSHAL);
383 if (has_ret)
385 if (decl_indirect(get_func_return_type(cur)))
386 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
387 else if (is_ptr(get_func_return_type(cur)) || is_array(get_func_return_type(cur)))
388 print_proxy("%s = 0;\n", "_RetVal");
389 write_remoting_arguments(proxy, indent, cur, PASS_RETURN, PHASE_UNMARSHAL);
392 indent--;
393 print_proxy( "}\n");
394 print_proxy( "RpcFinally\n" );
395 print_proxy( "{\n" );
396 indent++;
397 if (has_full_pointer)
398 write_full_pointer_free(proxy, indent, cur);
399 print_proxy( "NdrProxyFreeBuffer(This, &_StubMsg);\n" );
400 indent--;
401 print_proxy( "}\n");
402 print_proxy( "RpcEndFinally\n" );
403 indent--;
404 print_proxy( "}\n" );
405 print_proxy( "RpcExcept(_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
406 print_proxy( "{\n" );
407 if (has_ret) {
408 indent++;
409 proxy_free_variables( cur->args );
410 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
411 indent--;
413 print_proxy( "}\n" );
414 print_proxy( "RpcEndExcept\n" );
416 if (has_ret) {
417 print_proxy( "return _RetVal;\n" );
419 indent--;
420 print_proxy( "}\n");
421 print_proxy( "\n");
424 static void gen_stub(type_t *iface, const func_t *cur, const char *cas,
425 unsigned int proc_offset)
427 var_t *def = cur->def;
428 const var_t *arg;
429 int has_ret = !is_void(get_func_return_type(cur));
430 int has_full_pointer = is_full_pointer_function(cur);
432 indent = 0;
433 print_proxy( "void __RPC_STUB %s_", iface->name);
434 write_name(proxy, def);
435 print_proxy( "_Stub(\n");
436 indent++;
437 print_proxy( "IRpcStubBuffer* This,\n");
438 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
439 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
440 print_proxy( "DWORD* _pdwStubPhase)\n");
441 indent--;
442 print_proxy( "{\n");
443 indent++;
444 print_proxy("%s * _This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n", iface->name, iface->name);
445 print_proxy("MIDL_STUB_MESSAGE _StubMsg;\n");
446 declare_stub_args( proxy, indent, cur );
447 fprintf(proxy, "\n");
449 /* FIXME: trace */
451 print_proxy("NdrStubInitialize(_pRpcMessage, &_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
452 fprintf(proxy, "\n");
454 write_parameters_init(proxy, indent, cur);
456 print_proxy("RpcTryFinally\n");
457 print_proxy("{\n");
458 indent++;
459 if (has_full_pointer)
460 write_full_pointer_init(proxy, indent, cur, TRUE);
461 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
462 indent++;
463 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
464 indent--;
465 fprintf(proxy, "\n");
467 write_remoting_arguments(proxy, indent, cur, PASS_IN, PHASE_UNMARSHAL);
468 fprintf(proxy, "\n");
470 assign_stub_out_args( proxy, indent, cur );
472 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
473 fprintf(proxy, "\n");
474 print_proxy("");
475 if (has_ret) fprintf(proxy, "_RetVal = ");
476 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
477 else
479 fprintf(proxy, "_This->lpVtbl->");
480 write_name(proxy, def);
482 fprintf(proxy, "(_This");
484 if (cur->args)
486 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
488 fprintf(proxy, ", ");
489 if (arg->type->declarray)
490 fprintf(proxy, "*");
491 write_name(proxy, arg);
494 fprintf(proxy, ");\n");
495 fprintf(proxy, "\n");
496 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
497 fprintf(proxy, "\n");
499 write_remoting_arguments(proxy, indent, cur, PASS_OUT, PHASE_BUFFERSIZE);
501 if (!is_void(get_func_return_type(cur)))
502 write_remoting_arguments(proxy, indent, cur, PASS_RETURN, PHASE_BUFFERSIZE);
504 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &_StubMsg);\n");
506 write_remoting_arguments(proxy, indent, cur, PASS_OUT, PHASE_MARSHAL);
507 fprintf(proxy, "\n");
509 /* marshall the return value */
510 if (!is_void(get_func_return_type(cur)))
511 write_remoting_arguments(proxy, indent, cur, PASS_RETURN, PHASE_MARSHAL);
513 indent--;
514 print_proxy("}\n");
515 print_proxy("RpcFinally\n");
516 print_proxy("{\n");
518 write_remoting_arguments(proxy, indent+1, cur, PASS_OUT, PHASE_FREE);
520 if (has_full_pointer)
521 write_full_pointer_free(proxy, indent, cur);
523 print_proxy("}\n");
524 print_proxy("RpcEndFinally\n");
526 print_proxy("_pRpcMessage->BufferLength = _StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
527 indent--;
529 print_proxy("}\n");
530 print_proxy("\n");
533 static int write_proxy_methods(type_t *iface)
535 const func_t *cur;
536 int i = 0;
538 if (iface->ref) i = write_proxy_methods(iface->ref);
539 if (iface->funcs) LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry ) {
540 var_t *def = cur->def;
541 if (!is_callas(def->attrs)) {
542 if (i) fprintf(proxy, ",\n");
543 print_proxy( "%s_", iface->name);
544 write_name(proxy, def);
545 fprintf(proxy, "_Proxy");
546 i++;
549 return i;
552 static int write_stub_methods(type_t *iface)
554 const func_t *cur;
555 int i = 0;
557 if (iface->ref) i = write_stub_methods(iface->ref);
558 else return i; /* skip IUnknown */
560 if (iface->funcs) LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry ) {
561 var_t *def = cur->def;
562 if (!is_local(def->attrs)) {
563 if (i) fprintf(proxy,",\n");
564 print_proxy( "%s_", iface->name);
565 write_name(proxy, def);
566 fprintf(proxy, "_Stub");
567 i++;
570 return i;
573 static void write_proxy(type_t *iface, unsigned int *proc_offset)
575 int midx = -1, stubs;
576 const func_t *cur;
578 if (!iface->funcs) return;
580 /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
582 fprintf(proxy, "/*****************************************************************************\n");
583 fprintf(proxy, " * %s interface\n", iface->name);
584 fprintf(proxy, " */\n");
585 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
587 const var_t *def = cur->def;
588 if (!is_local(def->attrs)) {
589 const var_t *cas = is_callas(def->attrs);
590 const char *cname = cas ? cas->name : NULL;
591 int idx = cur->idx;
592 if (cname) {
593 const func_t *m;
594 LIST_FOR_EACH_ENTRY( m, iface->funcs, const func_t, entry )
595 if (!strcmp(m->def->name, cname))
597 idx = m->idx;
598 break;
601 gen_proxy(iface, cur, idx, *proc_offset);
602 gen_stub(iface, cur, cname, *proc_offset);
603 *proc_offset += get_size_procformatstring_func( cur );
604 if (midx == -1) midx = idx;
605 else if (midx != idx) error("method index mismatch in write_proxy\n");
606 midx++;
610 /* proxy vtable */
611 print_proxy( "static const CINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n", midx, iface->name);
612 print_proxy( "{\n");
613 indent++;
614 print_proxy( "{\n", iface->name);
615 indent++;
616 print_proxy( "&IID_%s,\n", iface->name);
617 indent--;
618 print_proxy( "},\n");
619 print_proxy( "{\n");
620 indent++;
621 write_proxy_methods(iface);
622 fprintf(proxy, "\n");
623 indent--;
624 print_proxy( "}\n");
625 indent--;
626 print_proxy( "};\n");
627 fprintf(proxy, "\n\n");
629 /* stub vtable */
630 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
631 print_proxy( "{\n");
632 indent++;
633 stubs = write_stub_methods(iface);
634 fprintf(proxy, "\n");
635 indent--;
636 fprintf(proxy, "};\n");
637 print_proxy( "\n");
638 print_proxy( "static const CInterfaceStubVtbl _%sStubVtbl =\n", iface->name);
639 print_proxy( "{\n");
640 indent++;
641 print_proxy( "{\n");
642 indent++;
643 print_proxy( "&IID_%s,\n", iface->name);
644 print_proxy( "0,\n");
645 print_proxy( "%d,\n", stubs+3);
646 print_proxy( "&%s_table[-3],\n", iface->name);
647 indent--;
648 print_proxy( "},\n", iface->name);
649 print_proxy( "{\n");
650 indent++;
651 print_proxy( "CStdStubBuffer_METHODS\n");
652 indent--;
653 print_proxy( "}\n");
654 indent--;
655 print_proxy( "};\n");
656 print_proxy( "\n");
659 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
661 const statement_t *stmt;
663 if (stmts)
664 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
666 if (stmt->type == STMT_LIBRARY)
668 if (does_any_iface(stmt->u.lib->stmts, pred))
669 return TRUE;
671 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
673 if (pred(stmt->u.type))
674 return TRUE;
678 return FALSE;
681 int need_proxy(const type_t *iface)
683 return is_object(iface->attrs) && !is_local(iface->attrs);
686 int need_stub(const type_t *iface)
688 return !is_object(iface->attrs) && !is_local(iface->attrs);
691 int need_proxy_file(const statement_list_t *stmts)
693 return does_any_iface(stmts, need_proxy);
696 int need_stub_files(const statement_list_t *stmts)
698 return does_any_iface(stmts, need_stub);
701 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
703 const statement_t *stmt;
704 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
706 if (stmt->type == STMT_LIBRARY)
707 write_proxy_stmts(stmt->u.lib->stmts, proc_offset);
708 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
710 if (need_proxy(stmt->u.type))
711 write_proxy(stmt->u.type, proc_offset);
716 static void write_proxy_iface_name_format(const statement_list_t *stmts, const char *format)
718 const statement_t *stmt;
719 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
721 if (stmt->type == STMT_LIBRARY)
722 write_proxy_iface_name_format(stmt->u.lib->stmts, format);
723 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
725 type_t *iface = stmt->u.type;
726 if (iface->ref && iface->funcs && need_proxy(iface))
727 fprintf(proxy, format, iface->name);
732 static void write_iid_lookup(const statement_list_t *stmts, const char *file_id, int *c)
734 const statement_t *stmt;
735 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
737 if (stmt->type == STMT_LIBRARY)
738 write_iid_lookup(stmt->u.lib->stmts, file_id, c);
739 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
741 type_t *iface = stmt->u.type;
742 if(iface->ref && iface->funcs && need_proxy(iface))
744 fprintf(proxy, " if (!_%s_CHECK_IID(%d))\n", file_id, *c);
745 fprintf(proxy, " {\n");
746 fprintf(proxy, " *pIndex = %d;\n", *c);
747 fprintf(proxy, " return 1;\n");
748 fprintf(proxy, " }\n");
749 (*c)++;
755 void write_proxies(const statement_list_t *stmts)
757 int expr_eval_routines;
758 char *file_id = proxy_token;
759 int c;
760 unsigned int proc_offset = 0;
762 if (!do_proxies) return;
763 if (do_everything && !need_proxy_file(stmts)) return;
765 init_proxy(stmts);
766 if(!proxy) return;
768 write_proxy_stmts(stmts, &proc_offset);
770 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
771 if (expr_eval_routines)
772 write_expr_eval_routine_list(proxy, proxy_token);
773 write_user_quad_list(proxy);
774 write_stubdesc(expr_eval_routines);
776 print_proxy( "#if !defined(__RPC_WIN32__)\n");
777 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
778 print_proxy( "#endif\n");
779 print_proxy( "\n");
780 write_procformatstring(proxy, stmts, need_proxy);
781 write_typeformatstring(proxy, stmts, need_proxy);
783 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
784 fprintf(proxy, "{\n");
785 write_proxy_iface_name_format(stmts, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n");
786 fprintf(proxy, " 0\n");
787 fprintf(proxy, "};\n");
788 fprintf(proxy, "\n");
790 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
791 fprintf(proxy, "{\n");
792 write_proxy_iface_name_format(stmts, " (const CInterfaceStubVtbl*)&_%sStubVtbl,\n");
793 fprintf(proxy, " 0\n");
794 fprintf(proxy, "};\n");
795 fprintf(proxy, "\n");
797 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
798 fprintf(proxy, "{\n");
799 write_proxy_iface_name_format(stmts, " \"%s\",\n");
800 fprintf(proxy, " 0\n");
801 fprintf(proxy, "};\n");
802 fprintf(proxy, "\n");
804 fprintf(proxy, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_%s, pIID, n)\n", file_id, file_id);
805 fprintf(proxy, "\n");
806 fprintf(proxy, "int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
807 fprintf(proxy, "{\n");
808 c = 0;
809 write_iid_lookup(stmts, file_id, &c);
810 fprintf(proxy, " return 0;\n");
811 fprintf(proxy, "}\n");
812 fprintf(proxy, "\n");
814 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id);
815 fprintf(proxy, "{\n");
816 fprintf(proxy, " (const PCInterfaceProxyVtblList*)&_%s_ProxyVtblList,\n", file_id);
817 fprintf(proxy, " (const PCInterfaceStubVtblList*)&_%s_StubVtblList,\n", file_id);
818 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
819 fprintf(proxy, " 0,\n");
820 fprintf(proxy, " &_%s_IID_Lookup,\n", file_id);
821 fprintf(proxy, " %d,\n", c);
822 fprintf(proxy, " 1,\n");
823 fprintf(proxy, " 0,\n");
824 fprintf(proxy, " 0,\n");
825 fprintf(proxy, " 0,\n");
826 fprintf(proxy, " 0\n");
827 fprintf(proxy, "};\n");
829 fclose(proxy);