crypt32: Don't claim to have queried an object as a serialized context when reading...
[wine/wine64.git] / tools / widl / proxy.c
blob95651fcfe73580ec149ea5f6595829107ac32232
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");
130 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
131 write_stubdescproto();
134 static void clear_output_vars( const var_list_t *args )
136 const var_t *arg;
138 if (!args) return;
139 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
141 if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
142 print_proxy( "if(%s)\n", arg->name );
143 indent++;
144 print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name );
145 indent--;
150 int is_var_ptr(const var_t *v)
152 return is_ptr(v->type);
155 int cant_be_null(const var_t *v)
157 /* Search backwards for the most recent pointer attribute. */
158 const attr_list_t *attrs = v->attrs;
159 const type_t *type = v->type;
161 /* context handles have their own checking so they can be null for the
162 * purposes of null ref pointer checking */
163 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
164 return 0;
166 if (! attrs && type)
168 attrs = type->attrs;
169 type = type->ref;
172 while (attrs)
174 int t = get_attrv(attrs, ATTR_POINTERTYPE);
176 if (t == RPC_FC_FP || t == RPC_FC_OP || t == RPC_FC_UP)
177 return 0;
179 if (t == RPC_FC_RP)
180 return 1;
182 if (type)
184 attrs = type->attrs;
185 type = type->ref;
187 else
188 attrs = NULL;
191 return 1; /* Default is RPC_FC_RP. */
194 static int need_delegation(const type_t *iface)
196 return iface->ref && iface->ref->ref && iface->ref->ignore;
199 static void proxy_check_pointers( const var_list_t *args )
201 const var_t *arg;
203 if (!args) return;
204 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
206 if (is_var_ptr(arg) && cant_be_null(arg)) {
207 print_proxy( "if(!%s)\n", arg->name );
208 indent++;
209 print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
210 indent--;
215 static void free_variable( const var_t *arg, const char *local_var_prefix )
217 unsigned int type_offset = arg->type->typestring_offset;
218 expr_t *iid;
219 type_t *type = arg->type;
220 expr_t *size = get_size_is_expr(type, arg->name);
222 if (size)
224 print_proxy( "__frame->_StubMsg.MaxCount = " );
225 write_expr(proxy, size, 0, 1, NULL, NULL, local_var_prefix);
226 fprintf(proxy, ";\n\n");
227 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
228 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
229 fprintf(proxy, "(void*)%s );\n", arg->name );
230 return;
233 switch( type->type )
235 case RPC_FC_BYTE:
236 case RPC_FC_CHAR:
237 case RPC_FC_WCHAR:
238 case RPC_FC_SHORT:
239 case RPC_FC_USHORT:
240 case RPC_FC_ENUM16:
241 case RPC_FC_LONG:
242 case RPC_FC_ULONG:
243 case RPC_FC_ENUM32:
244 case RPC_FC_STRUCT:
245 break;
247 case RPC_FC_FP:
248 case RPC_FC_IP:
249 iid = get_attrp( arg->attrs, ATTR_IIDIS );
250 if( iid )
252 print_proxy( "__frame->_StubMsg.MaxCount = (unsigned long) " );
253 write_expr(proxy, iid, 1, 1, NULL, NULL, local_var_prefix);
254 print_proxy( ";\n\n" );
256 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
257 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
258 fprintf(proxy, "(void*)%s );\n", arg->name );
259 break;
261 default:
262 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
266 static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
268 const var_t *arg;
270 if (!args) return;
271 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
272 if (is_attr(arg->attrs, ATTR_OUT))
274 free_variable( arg, local_var_prefix );
275 fprintf(proxy, "\n");
279 static void gen_proxy(type_t *iface, const func_t *cur, int idx,
280 unsigned int proc_offset)
282 var_t *def = cur->def;
283 int has_ret = !is_void(get_func_return_type(cur));
284 int has_full_pointer = is_full_pointer_function(cur);
285 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
286 if (!callconv) callconv = "";
288 indent = 0;
289 print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
290 iface->name, get_name(def) );
291 print_proxy( "{\n");
292 indent++;
293 if (has_full_pointer) write_full_pointer_free(proxy, indent, cur);
294 print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
295 indent--;
296 print_proxy( "}\n");
297 print_proxy( "\n");
299 write_type_decl_left(proxy, get_func_return_type(cur));
300 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(def));
301 write_args(proxy, cur->args, iface->name, 1, TRUE);
302 print_proxy( ")\n");
303 print_proxy( "{\n");
304 indent ++;
305 print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
306 /* local variables */
307 if (has_ret) {
308 print_proxy( "" );
309 write_type_decl_left(proxy, get_func_return_type(cur));
310 print_proxy( " _RetVal;\n");
312 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
313 if (has_ret) {
314 if (decl_indirect(get_func_return_type(cur)))
315 print_proxy("void *_p_%s = &%s;\n",
316 "_RetVal", "_RetVal");
318 print_proxy( "\n");
320 print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(def) );
321 print_proxy( "__frame->This = This;\n" );
323 if (has_full_pointer)
324 write_full_pointer_init(proxy, indent, cur, FALSE);
326 /* FIXME: trace */
327 clear_output_vars( cur->args );
329 print_proxy( "RpcTryExcept\n" );
330 print_proxy( "{\n" );
331 indent++;
332 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx);
333 proxy_check_pointers( cur->args );
335 print_proxy( "RpcTryFinally\n" );
336 print_proxy( "{\n" );
337 indent++;
339 write_remoting_arguments(proxy, indent, cur, "", PASS_IN, PHASE_BUFFERSIZE);
341 print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
343 write_remoting_arguments(proxy, indent, cur, "", PASS_IN, PHASE_MARSHAL);
345 print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
346 fprintf(proxy, "\n");
347 print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
348 print_proxy( "__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
350 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
351 indent++;
352 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
353 indent--;
354 fprintf(proxy, "\n");
356 write_remoting_arguments(proxy, indent, cur, "", PASS_OUT, PHASE_UNMARSHAL);
358 if (has_ret)
360 if (decl_indirect(get_func_return_type(cur)))
361 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
362 else if (is_ptr(get_func_return_type(cur)) || is_array(get_func_return_type(cur)))
363 print_proxy("%s = 0;\n", "_RetVal");
364 write_remoting_arguments(proxy, indent, cur, "", PASS_RETURN, PHASE_UNMARSHAL);
367 indent--;
368 print_proxy( "}\n");
369 print_proxy( "RpcFinally\n" );
370 print_proxy( "{\n" );
371 indent++;
372 print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(def) );
373 indent--;
374 print_proxy( "}\n");
375 print_proxy( "RpcEndFinally\n" );
376 indent--;
377 print_proxy( "}\n" );
378 print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
379 print_proxy( "{\n" );
380 if (has_ret) {
381 indent++;
382 proxy_free_variables( cur->args, "" );
383 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
384 indent--;
386 print_proxy( "}\n" );
387 print_proxy( "RpcEndExcept\n" );
389 if (has_ret) {
390 print_proxy( "return _RetVal;\n" );
392 indent--;
393 print_proxy( "}\n");
394 print_proxy( "\n");
397 static void gen_stub(type_t *iface, const func_t *cur, const char *cas,
398 unsigned int proc_offset)
400 var_t *def = cur->def;
401 const var_t *arg;
402 int has_ret = !is_void(get_func_return_type(cur));
403 int has_full_pointer = is_full_pointer_function(cur);
405 indent = 0;
406 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(def));
407 indent++;
408 print_proxy( "__DECL_EXCEPTION_FRAME\n" );
409 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
410 print_proxy( "%s * _This;\n", iface->name );
411 declare_stub_args( proxy, indent, cur );
412 indent--;
413 print_proxy( "};\n\n" );
415 print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(def) );
416 print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(def) );
417 indent++;
418 write_remoting_arguments(proxy, indent, cur, "__frame->", PASS_OUT, PHASE_FREE);
419 if (has_full_pointer)
420 write_full_pointer_free(proxy, indent, cur);
421 indent--;
422 print_proxy( "}\n\n" );
424 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(def));
425 indent++;
426 print_proxy( "IRpcStubBuffer* This,\n");
427 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
428 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
429 print_proxy( "DWORD* _pdwStubPhase)\n");
430 indent--;
431 print_proxy( "{\n");
432 indent++;
433 print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
434 iface->name, get_name(def) );
436 print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
438 /* FIXME: trace */
440 print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
441 fprintf(proxy, "\n");
442 print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(def) );
444 write_parameters_init(proxy, indent, cur, "__frame->");
446 print_proxy("RpcTryFinally\n");
447 print_proxy("{\n");
448 indent++;
449 if (has_full_pointer)
450 write_full_pointer_init(proxy, indent, cur, TRUE);
451 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
452 indent++;
453 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
454 indent--;
455 fprintf(proxy, "\n");
457 write_remoting_arguments(proxy, indent, cur, "__frame->", PASS_IN, PHASE_UNMARSHAL);
458 fprintf(proxy, "\n");
460 assign_stub_out_args( proxy, indent, cur, "__frame->" );
462 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
463 fprintf(proxy, "\n");
464 print_proxy("");
465 if (has_ret) fprintf(proxy, "__frame->_RetVal = ");
466 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
467 else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(def));
468 fprintf(proxy, "(__frame->_This");
470 if (cur->args)
472 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
473 fprintf(proxy, ", %s__frame->%s", arg->type->declarray ? "*" : "", arg->name);
475 fprintf(proxy, ");\n");
476 fprintf(proxy, "\n");
477 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
478 fprintf(proxy, "\n");
480 write_remoting_arguments(proxy, indent, cur, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
482 if (!is_void(get_func_return_type(cur)))
483 write_remoting_arguments(proxy, indent, cur, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
485 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
487 write_remoting_arguments(proxy, indent, cur, "__frame->", PASS_OUT, PHASE_MARSHAL);
488 fprintf(proxy, "\n");
490 /* marshall the return value */
491 if (!is_void(get_func_return_type(cur)))
492 write_remoting_arguments(proxy, indent, cur, "__frame->", PASS_RETURN, PHASE_MARSHAL);
494 indent--;
495 print_proxy("}\n");
496 print_proxy("RpcFinally\n");
497 print_proxy("{\n");
498 indent++;
499 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(def) );
500 indent--;
501 print_proxy("}\n");
502 print_proxy("RpcEndFinally\n");
504 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
505 indent--;
507 print_proxy("}\n");
508 print_proxy("\n");
511 static int count_methods(type_t *iface)
513 const func_t *cur;
514 int count = 0;
516 if (iface->ref) count = count_methods(iface->ref);
517 if (iface->funcs)
518 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
519 if (!is_callas(cur->def->attrs)) count++;
520 return count;
523 static int write_proxy_methods(type_t *iface, int skip)
525 const func_t *cur;
526 int i = 0;
528 if (iface->ref) i = write_proxy_methods(iface->ref, need_delegation(iface));
529 if (iface->funcs) LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry ) {
530 var_t *def = cur->def;
531 if (!is_callas(def->attrs)) {
532 if (i) fprintf(proxy, ",\n");
533 if (skip) print_proxy( "0 /* %s_%s_Proxy */", iface->name, get_name(def));
534 else print_proxy( "%s_%s_Proxy", iface->name, get_name(def));
535 i++;
538 return i;
541 static int write_stub_methods(type_t *iface, int skip)
543 const func_t *cur;
544 int i = 0;
546 if (iface->ref) i = write_stub_methods(iface->ref, need_delegation(iface));
547 else return i; /* skip IUnknown */
549 if (iface->funcs) LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry ) {
550 var_t *def = cur->def;
551 if (!is_local(def->attrs)) {
552 if (i) fprintf(proxy,",\n");
553 if (skip) print_proxy("STUB_FORWARDING_FUNCTION");
554 else print_proxy( "%s_%s_Stub", iface->name, get_name(def));
555 i++;
558 return i;
561 static void write_proxy(type_t *iface, unsigned int *proc_offset)
563 int midx = -1, count;
564 const func_t *cur;
566 /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
568 fprintf(proxy, "/*****************************************************************************\n");
569 fprintf(proxy, " * %s interface\n", iface->name);
570 fprintf(proxy, " */\n");
571 if (iface->funcs) LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
573 const var_t *def = cur->def;
574 if (!is_local(def->attrs)) {
575 const var_t *cas = is_callas(def->attrs);
576 const char *cname = cas ? cas->name : NULL;
577 int idx = cur->idx;
578 if (cname) {
579 const func_t *m;
580 LIST_FOR_EACH_ENTRY( m, iface->funcs, const func_t, entry )
581 if (!strcmp(m->def->name, cname))
583 idx = m->idx;
584 break;
587 gen_proxy(iface, cur, idx, *proc_offset);
588 gen_stub(iface, cur, cname, *proc_offset);
589 *proc_offset += get_size_procformatstring_func( cur );
590 if (midx == -1) midx = idx;
591 else if (midx != idx) error("method index mismatch in write_proxy\n");
592 midx++;
596 count = count_methods(iface);
597 if (midx != -1 && midx != count) error("invalid count %u/%u\n", count, midx);
599 /* proxy vtable */
600 print_proxy( "static const CINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n", count, iface->name);
601 print_proxy( "{\n");
602 indent++;
603 print_proxy( "{\n", iface->name);
604 indent++;
605 print_proxy( "&IID_%s,\n", iface->name);
606 indent--;
607 print_proxy( "},\n");
608 print_proxy( "{\n");
609 indent++;
610 write_proxy_methods(iface, FALSE);
611 fprintf(proxy, "\n");
612 indent--;
613 print_proxy( "}\n");
614 indent--;
615 print_proxy( "};\n");
616 fprintf(proxy, "\n\n");
618 /* stub vtable */
619 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
620 print_proxy( "{\n");
621 indent++;
622 write_stub_methods(iface, FALSE);
623 fprintf(proxy, "\n");
624 indent--;
625 fprintf(proxy, "};\n");
626 print_proxy( "\n");
627 print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
628 need_delegation(iface) ? "" : "const ", iface->name);
629 print_proxy( "{\n");
630 indent++;
631 print_proxy( "{\n");
632 indent++;
633 print_proxy( "&IID_%s,\n", iface->name);
634 print_proxy( "0,\n");
635 print_proxy( "%d,\n", count);
636 print_proxy( "&%s_table[-3],\n", iface->name);
637 indent--;
638 print_proxy( "},\n", iface->name);
639 print_proxy( "{\n");
640 indent++;
641 print_proxy( "CStdStubBuffer_%s\n", need_delegation(iface) ? "DELEGATING_METHODS" : "METHODS");
642 indent--;
643 print_proxy( "}\n");
644 indent--;
645 print_proxy( "};\n");
646 print_proxy( "\n");
649 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
651 const statement_t *stmt;
653 if (stmts)
654 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
656 if (stmt->type == STMT_LIBRARY)
658 if (does_any_iface(stmt->u.lib->stmts, pred))
659 return TRUE;
661 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
663 if (pred(stmt->u.type))
664 return TRUE;
668 return FALSE;
671 int need_proxy(const type_t *iface)
673 return is_object(iface->attrs) && !is_local(iface->attrs);
676 int need_stub(const type_t *iface)
678 return !is_object(iface->attrs) && !is_local(iface->attrs);
681 int need_proxy_file(const statement_list_t *stmts)
683 return does_any_iface(stmts, need_proxy);
686 int need_stub_files(const statement_list_t *stmts)
688 return does_any_iface(stmts, need_stub);
691 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
693 const statement_t *stmt;
694 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
696 if (stmt->type == STMT_LIBRARY)
697 write_proxy_stmts(stmt->u.lib->stmts, proc_offset);
698 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
700 if (need_proxy(stmt->u.type))
701 write_proxy(stmt->u.type, proc_offset);
706 static int cmp_iid( const void *ptr1, const void *ptr2 )
708 const type_t * const *iface1 = ptr1;
709 const type_t * const *iface2 = ptr2;
710 const UUID *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
711 const UUID *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
712 return memcmp( uuid1, uuid2, sizeof(UUID) );
715 static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
717 const statement_t *stmt;
719 if (!stmts) return;
720 LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
722 if (stmt->type == STMT_LIBRARY)
723 build_iface_list(stmt->u.lib->stmts, ifaces, count);
724 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
726 type_t *iface = stmt->u.type;
727 if (iface->ref && need_proxy(iface))
729 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(*ifaces) );
730 (*ifaces)[(*count)++] = iface;
736 static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
738 type_t **ifaces = NULL;
740 *count = 0;
741 build_iface_list( stmts, &ifaces, count );
742 qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
743 return ifaces;
746 void write_proxies(const statement_list_t *stmts)
748 int expr_eval_routines;
749 char *file_id = proxy_token;
750 int i, count, have_baseiid;
751 unsigned int proc_offset = 0;
752 type_t **interfaces;
754 if (!do_proxies) return;
755 if (do_everything && !need_proxy_file(stmts)) return;
757 init_proxy(stmts);
758 if(!proxy) return;
760 write_proxy_stmts(stmts, &proc_offset);
762 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
763 if (expr_eval_routines)
764 write_expr_eval_routine_list(proxy, proxy_token);
765 write_user_quad_list(proxy);
766 write_stubdesc(expr_eval_routines);
768 print_proxy( "#if !defined(__RPC_WIN32__)\n");
769 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
770 print_proxy( "#endif\n");
771 print_proxy( "\n");
772 write_procformatstring(proxy, stmts, need_proxy);
773 write_typeformatstring(proxy, stmts, need_proxy);
775 interfaces = sort_interfaces(stmts, &count);
776 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
777 fprintf(proxy, "{\n");
778 for (i = 0; i < count; i++)
779 fprintf(proxy, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
780 fprintf(proxy, " 0\n");
781 fprintf(proxy, "};\n");
782 fprintf(proxy, "\n");
784 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
785 fprintf(proxy, "{\n");
786 for (i = 0; i < count; i++)
787 fprintf(proxy, " &_%sStubVtbl,\n", interfaces[i]->name);
788 fprintf(proxy, " 0\n");
789 fprintf(proxy, "};\n");
790 fprintf(proxy, "\n");
792 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
793 fprintf(proxy, "{\n");
794 for (i = 0; i < count; i++)
795 fprintf(proxy, " \"%s\",\n", interfaces[i]->name);
796 fprintf(proxy, " 0\n");
797 fprintf(proxy, "};\n");
798 fprintf(proxy, "\n");
800 if ((have_baseiid = does_any_iface(stmts, need_delegation)))
802 fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
803 fprintf(proxy, "{\n");
804 for (i = 0; i < count; i++)
806 if (need_delegation(interfaces[i]))
807 fprintf( proxy, " &IID_%s, /* %s */\n", interfaces[i]->ref->name, interfaces[i]->name );
808 else
809 fprintf( proxy, " 0,\n" );
811 fprintf(proxy, " 0\n");
812 fprintf(proxy, "};\n");
813 fprintf(proxy, "\n");
816 fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
817 fprintf(proxy, "{\n");
818 fprintf(proxy, " int low = 0, high = %d;\n", count - 1);
819 fprintf(proxy, "\n");
820 fprintf(proxy, " while (low <= high)\n");
821 fprintf(proxy, " {\n");
822 fprintf(proxy, " int pos = (low + high) / 2;\n");
823 fprintf(proxy, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
824 fprintf(proxy, " if (!res) { *pIndex = pos; return 1; }\n");
825 fprintf(proxy, " if (res > 0) low = pos + 1;\n");
826 fprintf(proxy, " else high = pos - 1;\n");
827 fprintf(proxy, " }\n");
828 fprintf(proxy, " return 0;\n");
829 fprintf(proxy, "}\n");
830 fprintf(proxy, "\n");
832 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo DECLSPEC_HIDDEN =\n", file_id);
833 fprintf(proxy, "{\n");
834 fprintf(proxy, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
835 fprintf(proxy, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
836 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
837 if (have_baseiid) fprintf(proxy, " _%s_BaseIIDList,\n", file_id);
838 else fprintf(proxy, " 0,\n");
839 fprintf(proxy, " _%s_IID_Lookup,\n", file_id);
840 fprintf(proxy, " %d,\n", count);
841 fprintf(proxy, " 1,\n");
842 fprintf(proxy, " 0,\n");
843 fprintf(proxy, " 0,\n");
844 fprintf(proxy, " 0,\n");
845 fprintf(proxy, " 0\n");
846 fprintf(proxy, "};\n");
848 fclose(proxy);