widl: Add support for generating new-style interpreted stubs for proxies.
[wine/multimedia.git] / tools / widl / proxy.c
blobe889423e68afed4525cf4a092fcf9e1eee12e2b0
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 static void print_proxy( const char *format, ... ) __attribute__((format (printf, 1, 2)));
52 static void print_proxy( const char *format, ... )
54 va_list va;
55 va_start( va, format );
56 print( proxy, indent, format, va );
57 va_end( va );
60 static void write_stubdescproto(void)
62 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
63 print_proxy( "\n");
66 static void write_stubdesc(int expr_eval_routines)
68 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc =\n{\n");
69 indent++;
70 print_proxy( "0,\n");
71 print_proxy( "NdrOleAllocate,\n");
72 print_proxy( "NdrOleFree,\n");
73 print_proxy( "{0}, 0, 0, %s, 0,\n", expr_eval_routines ? "ExprEvalRoutines" : "0");
74 print_proxy( "__MIDL_TypeFormatString.Format,\n");
75 print_proxy( "1, /* -error bounds_check flag */\n");
76 print_proxy( "0x%x, /* Ndr library version */\n", stub_mode == MODE_Oif ? 0x50002 : 0x10001);
77 print_proxy( "0,\n");
78 print_proxy( "0x50100a4, /* MIDL Version 5.1.164 */\n");
79 print_proxy( "0,\n");
80 print_proxy("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
81 print_proxy( "0, /* notify & notify_flag routine table */\n");
82 print_proxy( "1, /* Flags */\n");
83 print_proxy( "0, /* Reserved3 */\n");
84 print_proxy( "0, /* Reserved4 */\n");
85 print_proxy( "0 /* Reserved5 */\n");
86 indent--;
87 print_proxy( "};\n");
88 print_proxy( "\n");
91 static void init_proxy(const statement_list_t *stmts)
93 if (proxy) return;
94 if(!(proxy = fopen(proxy_name, "w")))
95 error("Could not open %s for output\n", proxy_name);
96 print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
97 print_proxy( "\n");
98 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
99 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ 440\n");
100 print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
101 print_proxy( "\n");
102 print_proxy( "#define __midl_proxy\n");
103 if (stub_mode == MODE_Oif) print_proxy( "#define USE_STUBLESS_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");
118 static void clear_output_vars( const var_list_t *args )
120 const var_t *arg;
122 if (!args) return;
123 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
125 if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
126 print_proxy( "if(%s)\n", arg->name );
127 indent++;
128 print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name );
129 indent--;
134 int is_var_ptr(const var_t *v)
136 return is_ptr(v->type);
139 int cant_be_null(const var_t *v)
141 switch (typegen_detect_type(v->type, v->attrs, TDT_IGNORE_STRINGS))
143 case TGT_ARRAY:
144 case TGT_POINTER:
145 return (get_pointer_fc(v->type, v->attrs, TRUE) == RPC_FC_RP);
146 case TGT_CTXT_HANDLE_POINTER:
147 return TRUE;
148 default:
149 return 0;
154 static int need_delegation(const type_t *iface)
156 return type_iface_get_inherit(iface) &&
157 type_iface_get_inherit(type_iface_get_inherit(iface)) &&
158 type_iface_get_inherit(iface)->ignore;
161 static int get_delegation_indirect(const type_t *iface, const type_t ** delegate_to)
163 const type_t * cur_iface;
164 for (cur_iface = iface; cur_iface != NULL; cur_iface = type_iface_get_inherit(cur_iface))
165 if (need_delegation(cur_iface))
167 if(delegate_to)
168 *delegate_to = type_iface_get_inherit(cur_iface);
169 return 1;
171 return 0;
174 static int need_delegation_indirect(const type_t *iface)
176 return get_delegation_indirect(iface, NULL);
179 static void proxy_check_pointers( const var_list_t *args )
181 const var_t *arg;
183 if (!args) return;
184 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
186 if (is_var_ptr(arg) && cant_be_null(arg)) {
187 print_proxy( "if(!%s)\n", arg->name );
188 indent++;
189 print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
190 indent--;
195 static void free_variable( const var_t *arg, const char *local_var_prefix )
197 unsigned int type_offset = arg->type->typestring_offset;
198 expr_t *iid;
199 type_t *type = arg->type;
200 expr_t *size = get_size_is_expr(type, arg->name);
202 if (size)
204 print_proxy( "__frame->_StubMsg.MaxCount = " );
205 write_expr(proxy, size, 0, 1, NULL, NULL, local_var_prefix);
206 fprintf(proxy, ";\n\n");
207 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
208 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
209 fprintf(proxy, "(void*)%s );\n", arg->name );
210 return;
213 switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
215 case TGT_ENUM:
216 case TGT_BASIC:
217 break;
219 case TGT_STRUCT:
220 if (get_struct_fc(type) != RPC_FC_STRUCT)
221 print_proxy("/* FIXME: %s code for %s struct type 0x%x missing */\n", __FUNCTION__, arg->name, get_struct_fc(type) );
222 break;
224 case TGT_IFACE_POINTER:
225 iid = get_attrp( arg->attrs, ATTR_IIDIS );
226 if( iid )
228 print_proxy( "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
229 write_expr(proxy, iid, 1, 1, NULL, NULL, local_var_prefix);
230 print_proxy( ";\n\n" );
232 /* fall through */
233 case TGT_POINTER:
234 if (get_pointer_fc(type, arg->attrs, TRUE) == RPC_FC_FP)
236 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
237 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
238 fprintf(proxy, "(void*)%s );\n", arg->name );
240 else
241 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
242 break;
244 default:
245 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
249 static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
251 const var_t *arg;
253 if (!args) return;
254 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
255 if (is_attr(arg->attrs, ATTR_OUT))
257 free_variable( arg, local_var_prefix );
258 fprintf(proxy, "\n");
262 static void gen_proxy(type_t *iface, const var_t *func, int idx,
263 unsigned int proc_offset)
265 type_t *rettype = type_function_get_rettype(func->type);
266 int has_ret = !is_void(rettype);
267 int has_full_pointer = is_full_pointer_function(func);
268 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
269 const var_list_t *args = type_get_function_args(func->type);
270 if (!callconv) callconv = "STDMETHODCALLTYPE";
272 indent = 0;
273 if (is_interpreted_func( iface, func ))
275 if (stub_mode == MODE_Oif && !is_callas( func->attrs )) return;
276 write_type_decl_left(proxy, type_function_get_rettype(func->type));
277 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
278 write_args(proxy, args, iface->name, 1, TRUE);
279 print_proxy( ")\n");
280 print_proxy( "{\n");
281 indent++;
282 if (has_ret) print_proxy( "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
283 print_proxy( "%s%s( &Object_StubDesc, &__MIDL_ProcFormatString.Format[%u], ",
284 has_ret ? "_RetVal = " : "",
285 stub_mode == MODE_Oif ? "NdrClientCall2" : "NdrClientCall",
286 proc_offset );
287 fprintf( proxy, "(unsigned char *)&This );\n" );
288 if (has_ret)
290 print_proxy( "return (" );
291 write_type_decl_left(proxy, rettype);
292 fprintf( proxy, ")*(LONG_PTR *)&_RetVal;\n" );
294 indent--;
295 print_proxy( "}\n\n");
296 return;
298 print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
299 iface->name, get_name(func) );
300 print_proxy( "{\n");
301 indent++;
302 if (has_full_pointer) write_full_pointer_free(proxy, indent, func);
303 print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
304 indent--;
305 print_proxy( "}\n");
306 print_proxy( "\n");
308 write_type_decl_left(proxy, rettype);
309 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
310 write_args(proxy, args, iface->name, 1, TRUE);
311 print_proxy( ")\n");
312 print_proxy( "{\n");
313 indent ++;
314 print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
315 /* local variables */
316 if (has_ret) {
317 print_proxy( "%s", "" );
318 write_type_decl_left(proxy, rettype);
319 print_proxy( " _RetVal;\n");
321 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
322 if (has_ret) {
323 if (decl_indirect(rettype))
324 print_proxy("void *_p_%s = &%s;\n",
325 "_RetVal", "_RetVal");
327 print_proxy( "\n");
329 print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(func) );
330 print_proxy( "__frame->This = This;\n" );
332 if (has_full_pointer)
333 write_full_pointer_init(proxy, indent, func, FALSE);
335 /* FIXME: trace */
336 clear_output_vars( type_get_function_args(func->type) );
338 print_proxy( "RpcTryExcept\n" );
339 print_proxy( "{\n" );
340 indent++;
341 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx);
342 proxy_check_pointers( type_get_function_args(func->type) );
344 print_proxy( "RpcTryFinally\n" );
345 print_proxy( "{\n" );
346 indent++;
348 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
350 print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
352 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_MARSHAL);
354 print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
355 fprintf(proxy, "\n");
356 print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
357 print_proxy( "__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
359 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
360 indent++;
361 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
362 indent--;
363 fprintf(proxy, "\n");
365 write_remoting_arguments(proxy, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
367 if (has_ret)
369 if (decl_indirect(rettype))
370 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
371 else if (is_ptr(rettype) || is_array(rettype))
372 print_proxy("%s = 0;\n", "_RetVal");
373 write_remoting_arguments(proxy, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
376 indent--;
377 print_proxy( "}\n");
378 print_proxy( "RpcFinally\n" );
379 print_proxy( "{\n" );
380 indent++;
381 print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(func) );
382 indent--;
383 print_proxy( "}\n");
384 print_proxy( "RpcEndFinally\n" );
385 indent--;
386 print_proxy( "}\n" );
387 print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
388 print_proxy( "{\n" );
389 if (has_ret) {
390 indent++;
391 proxy_free_variables( type_get_function_args(func->type), "" );
392 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
393 indent--;
395 print_proxy( "}\n" );
396 print_proxy( "RpcEndExcept\n" );
398 if (has_ret) {
399 print_proxy( "return _RetVal;\n" );
401 indent--;
402 print_proxy( "}\n");
403 print_proxy( "\n");
406 static void gen_stub(type_t *iface, const var_t *func, const char *cas,
407 unsigned int proc_offset)
409 const var_t *arg;
410 int has_ret = !is_void(type_function_get_rettype(func->type));
411 int has_full_pointer = is_full_pointer_function(func);
413 if (is_interpreted_func( iface, func )) return;
415 indent = 0;
416 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(func));
417 indent++;
418 print_proxy( "__DECL_EXCEPTION_FRAME\n" );
419 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
420 print_proxy( "%s * _This;\n", iface->name );
421 declare_stub_args( proxy, indent, func );
422 indent--;
423 print_proxy( "};\n\n" );
425 print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(func) );
426 print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(func) );
427 indent++;
428 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
429 if (has_full_pointer)
430 write_full_pointer_free(proxy, indent, func);
431 indent--;
432 print_proxy( "}\n\n" );
434 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
435 indent++;
436 print_proxy( "IRpcStubBuffer* This,\n");
437 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
438 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
439 print_proxy( "DWORD* _pdwStubPhase)\n");
440 indent--;
441 print_proxy( "{\n");
442 indent++;
443 print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
444 iface->name, get_name(func) );
446 print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
448 /* FIXME: trace */
450 print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
451 fprintf(proxy, "\n");
452 print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(func) );
454 write_parameters_init(proxy, indent, func, "__frame->");
456 print_proxy("RpcTryFinally\n");
457 print_proxy("{\n");
458 indent++;
459 if (has_full_pointer)
460 write_full_pointer_init(proxy, indent, func, TRUE);
461 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
462 indent++;
463 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
464 indent--;
465 fprintf(proxy, "\n");
467 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
468 fprintf(proxy, "\n");
470 assign_stub_out_args( proxy, indent, func, "__frame->" );
472 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
473 fprintf(proxy, "\n");
474 print_proxy( "%s", has_ret ? "__frame->_RetVal = " : "" );
475 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
476 else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(func));
477 fprintf(proxy, "(__frame->_This");
479 if (type_get_function_args(func->type))
481 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
482 fprintf(proxy, ", %s__frame->%s", is_array(arg->type) && !type_array_is_decl_as_ptr(arg->type) ? "*" :"" , arg->name);
484 fprintf(proxy, ");\n");
485 fprintf(proxy, "\n");
486 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
487 fprintf(proxy, "\n");
489 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
491 if (!is_void(type_function_get_rettype(func->type)))
492 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
494 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
496 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
497 fprintf(proxy, "\n");
499 /* marshall the return value */
500 if (!is_void(type_function_get_rettype(func->type)))
501 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
503 indent--;
504 print_proxy("}\n");
505 print_proxy("RpcFinally\n");
506 print_proxy("{\n");
507 indent++;
508 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(func) );
509 indent--;
510 print_proxy("}\n");
511 print_proxy("RpcEndFinally\n");
513 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
514 indent--;
516 print_proxy("}\n");
517 print_proxy("\n");
520 static void gen_stub_thunk( type_t *iface, const var_t *func, unsigned int proc_offset )
522 int has_ret = !is_void( type_function_get_rettype( func->type ));
523 const var_t *arg, *callas = is_callas( func->attrs );
524 const var_list_t *args = type_get_function_args( func->type );
526 indent = 0;
527 print_proxy( "void __RPC_API %s_%s_Thunk( PMIDL_STUB_MESSAGE pStubMsg )\n",
528 iface->name, get_name(func) );
529 print_proxy( "{\n");
530 indent++;
531 write_func_param_struct( proxy, iface, func->type, "pParamStruct" );
532 print_proxy( "%s%s_%s_Stub( pParamStruct->This",
533 has_ret ? "pParamStruct->_RetVal = " : "", iface->name, callas->name );
534 indent++;
535 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
537 fprintf( proxy, ",\n%*spParamStruct->%s", 4 * indent, "", arg->name );
539 fprintf( proxy, " );\n" );
540 indent--;
541 indent--;
542 print_proxy( "}\n\n");
545 int count_methods(const type_t *iface)
547 const statement_t *stmt;
548 int count = 0;
550 if (type_iface_get_inherit(iface))
551 count = count_methods(type_iface_get_inherit(iface));
553 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
554 const var_t *func = stmt->u.var;
555 if (!is_callas(func->attrs)) count++;
557 return count;
560 static const statement_t * get_callas_source(const type_t * iface, const var_t * def)
562 const statement_t * source;
563 STATEMENTS_FOR_EACH_FUNC( source, type_iface_get_stmts(iface)) {
564 const var_t * cas = is_callas(source->u.var->attrs );
565 if (cas && !strcmp(def->name, cas->name))
566 return source;
568 return NULL;
571 static int write_proxy_methods(type_t *iface, int skip)
573 const statement_t *stmt;
574 int i = 0;
576 if (type_iface_get_inherit(iface))
577 i = write_proxy_methods(type_iface_get_inherit(iface),
578 need_delegation(iface));
579 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
580 const var_t *func = stmt->u.var;
581 if (!is_callas(func->attrs)) {
582 int missing = 0;
583 const char * fname = get_name(func);
584 if (is_local(func->attrs)) {
585 const statement_t * callas_source = get_callas_source(iface, func);
586 if(!callas_source)
587 missing = 1;
588 else
589 fname = get_name(callas_source->u.var);
591 if (skip || missing) print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func));
592 else if (is_interpreted_func( iface, func ))
594 if (!is_local( func->attrs ) && type_iface_get_inherit(iface))
595 print_proxy( "(void *)-1, /* %s::%s */\n", iface->name, get_name(func));
596 else
597 print_proxy( "%s_%s_Proxy,\n", iface->name, fname);
599 else print_proxy( "%s_%s_Proxy,\n", iface->name, get_name(func));
600 i++;
603 return i;
606 static int write_stub_methods(type_t *iface, int skip)
608 const statement_t *stmt;
609 int i = 0;
611 if (type_iface_get_inherit(iface))
612 i = write_stub_methods(type_iface_get_inherit(iface), need_delegation(iface));
613 else
614 return i; /* skip IUnknown */
616 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
617 const var_t *func = stmt->u.var;
618 if (!is_callas(func->attrs)) {
619 int missing = 0;
620 const char * fname = get_name(func);
621 if(is_local(func->attrs)) {
622 const statement_t * callas_source = get_callas_source(iface, func);
623 if(!callas_source)
624 missing = 1;
625 else
626 fname = get_name(callas_source->u.var);
628 if (i) fprintf(proxy,",\n");
629 if (skip || missing) print_proxy("STUB_FORWARDING_FUNCTION");
630 else if (is_interpreted_func( iface, func ))
631 print_proxy( "(PRPC_STUB_FUNCTION)%s", stub_mode == MODE_Oif ? "NdrStubCall2" : "NdrStubCall" );
632 else print_proxy( "%s_%s_Stub", iface->name, fname);
633 i++;
636 return i;
639 static void write_thunk_methods( type_t *iface )
641 const statement_t *stmt;
642 int i = 0;
644 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
646 var_t *func = stmt->u.var;
647 const var_t *cas = is_callas(func->attrs);
649 if (is_local( func->attrs )) continue;
650 if (i) fprintf(proxy, ",\n");
651 if (cas && is_interpreted_func( iface, func ))
652 print_proxy( "%s_%s_Thunk", iface->name, func->name );
653 else
654 print_proxy( "0" );
655 i++;
657 fputc( '\n', proxy );
660 static void write_proxy(type_t *iface, unsigned int *proc_offset)
662 int count;
663 const statement_t *stmt;
664 int first_func = 1;
665 int needs_stub_thunks = 0;
666 int needs_inline_stubs = need_inline_stubs( iface ) || need_delegation( iface );
668 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
669 var_t *func = stmt->u.var;
670 if (first_func) {
671 fprintf(proxy, "/*****************************************************************************\n");
672 fprintf(proxy, " * %s interface\n", iface->name);
673 fprintf(proxy, " */\n");
674 first_func = 0;
676 if (!is_local(func->attrs)) {
677 const var_t *cas = is_callas(func->attrs);
678 const char *cname = cas ? cas->name : NULL;
679 int idx = func->type->details.function->idx;
680 if (cname) {
681 const statement_t *stmt2;
682 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface)) {
683 const var_t *m = stmt2->u.var;
684 if (!strcmp(m->name, cname))
686 idx = m->type->details.function->idx;
687 break;
691 func->procstring_offset = *proc_offset;
692 gen_proxy(iface, func, idx, *proc_offset);
693 gen_stub(iface, func, cname, *proc_offset);
694 if (cas && is_interpreted_func( iface, func ))
696 needs_stub_thunks = 1;
697 gen_stub_thunk(iface, func, *proc_offset);
699 *proc_offset += get_size_procformatstring_func( iface, func );
703 count = count_methods(iface);
705 write_procformatstring_offsets( proxy, iface );
707 /* proxy info */
708 if (stub_mode == MODE_Oif)
710 print_proxy( "static const MIDL_STUBLESS_PROXY_INFO %s_ProxyInfo =\n", iface->name );
711 print_proxy( "{\n" );
712 indent++;
713 print_proxy( "&Object_StubDesc,\n" );
714 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
715 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
716 print_proxy( "0,\n" );
717 print_proxy( "0,\n" );
718 print_proxy( "0\n" );
719 indent--;
720 print_proxy( "};\n\n" );
723 /* proxy vtable */
724 print_proxy( "static %sCINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n",
725 need_delegation_indirect(iface) ? "" : "const ", count, iface->name);
726 print_proxy( "{\n");
727 indent++;
728 print_proxy( "{\n");
729 indent++;
730 if (stub_mode == MODE_Oif) print_proxy( "&%s_ProxyInfo,\n", iface->name );
731 print_proxy( "&IID_%s,\n", iface->name);
732 indent--;
733 print_proxy( "},\n");
734 print_proxy( "{\n");
735 indent++;
736 write_proxy_methods(iface, FALSE);
737 indent--;
738 print_proxy( "}\n");
739 indent--;
740 print_proxy( "};\n\n");
742 /* stub thunk table */
743 if (needs_stub_thunks)
745 print_proxy( "static const STUB_THUNK %s_StubThunkTable[] =\n", iface->name);
746 print_proxy( "{\n");
747 indent++;
748 write_thunk_methods( iface );
749 indent--;
750 print_proxy( "};\n\n");
753 /* server info */
754 print_proxy( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
755 print_proxy( "{\n" );
756 indent++;
757 print_proxy( "&Object_StubDesc,\n" );
758 print_proxy( "0,\n" );
759 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
760 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
761 if (needs_stub_thunks)
762 print_proxy( "&%s_StubThunkTable[-3],\n", iface->name );
763 else
764 print_proxy( "0,\n" );
765 print_proxy( "0,\n" );
766 print_proxy( "0,\n" );
767 print_proxy( "0\n" );
768 indent--;
769 print_proxy( "};\n\n" );
771 /* stub vtable */
772 if (needs_inline_stubs)
774 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
775 print_proxy( "{\n");
776 indent++;
777 write_stub_methods(iface, FALSE);
778 fprintf(proxy, "\n");
779 indent--;
780 fprintf(proxy, "};\n\n");
782 print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
783 need_delegation_indirect(iface) ? "" : "const ", iface->name);
784 print_proxy( "{\n");
785 indent++;
786 print_proxy( "{\n");
787 indent++;
788 print_proxy( "&IID_%s,\n", iface->name);
789 print_proxy( "&%s_ServerInfo,\n", iface->name );
790 print_proxy( "%d,\n", count);
791 if (needs_inline_stubs) print_proxy( "&%s_table[-3]\n", iface->name );
792 else print_proxy( "0\n" );
793 indent--;
794 print_proxy( "},\n");
795 print_proxy( "{\n");
796 indent++;
797 print_proxy( "CStdStubBuffer_%s\n", need_delegation_indirect(iface) ? "DELEGATING_METHODS" : "METHODS");
798 indent--;
799 print_proxy( "}\n");
800 indent--;
801 print_proxy( "};\n");
802 print_proxy( "\n");
805 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
807 const statement_t *stmt;
809 if (stmts)
810 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
812 if (stmt->type == STMT_LIBRARY)
814 if (does_any_iface(stmt->u.lib->stmts, pred))
815 return TRUE;
817 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
819 if (pred(stmt->u.type))
820 return TRUE;
824 return FALSE;
827 int need_proxy(const type_t *iface)
829 if (!is_object( iface )) return 0;
830 if (is_local( iface->attrs )) return 0;
831 if (is_attr( iface->attrs, ATTR_OLEAUTOMATION )) return 0;
832 if (is_attr( iface->attrs, ATTR_DISPINTERFACE )) return 0;
833 return 1;
836 int need_stub(const type_t *iface)
838 return !is_object(iface) && !is_local(iface->attrs);
841 int need_proxy_file(const statement_list_t *stmts)
843 return does_any_iface(stmts, need_proxy);
846 int need_inline_stubs(const type_t *iface)
848 const statement_t *stmt;
850 if (stub_mode == MODE_Os) return 1;
852 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
854 const var_t *func = stmt->u.var;
855 if (!is_interpreted_func( iface, func )) return 1;
857 return 0;
860 int need_stub_files(const statement_list_t *stmts)
862 return does_any_iface(stmts, need_stub);
865 int need_inline_stubs_file(const statement_list_t *stmts)
867 return does_any_iface(stmts, need_inline_stubs);
870 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
872 const statement_t *stmt;
873 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
875 if (stmt->type == STMT_LIBRARY)
876 write_proxy_stmts(stmt->u.lib->stmts, proc_offset);
877 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
879 if (need_proxy(stmt->u.type))
880 write_proxy(stmt->u.type, proc_offset);
885 static int cmp_iid( const void *ptr1, const void *ptr2 )
887 const type_t * const *iface1 = ptr1;
888 const type_t * const *iface2 = ptr2;
889 const UUID *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
890 const UUID *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
891 return memcmp( uuid1, uuid2, sizeof(UUID) );
894 static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
896 const statement_t *stmt;
898 if (!stmts) return;
899 LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
901 if (stmt->type == STMT_LIBRARY)
902 build_iface_list(stmt->u.lib->stmts, ifaces, count);
903 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
905 type_t *iface = stmt->u.type;
906 if (type_iface_get_inherit(iface) && need_proxy(iface))
908 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
909 (*ifaces)[(*count)++] = iface;
915 static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
917 type_t **ifaces = NULL;
919 *count = 0;
920 build_iface_list( stmts, &ifaces, count );
921 qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
922 return ifaces;
925 static void write_proxy_routines(const statement_list_t *stmts)
927 int expr_eval_routines;
928 unsigned int proc_offset = 0;
929 char *file_id = proxy_token;
930 int i, count, have_baseiid;
931 type_t **interfaces;
932 const type_t * delegate_to;
934 if (need_inline_stubs_file( stmts ))
936 write_exceptions( proxy );
937 print_proxy( "\n");
938 print_proxy( "struct __proxy_frame\n");
939 print_proxy( "{\n");
940 print_proxy( " __DECL_EXCEPTION_FRAME\n");
941 print_proxy( " MIDL_STUB_MESSAGE _StubMsg;\n");
942 print_proxy( " void *This;\n");
943 print_proxy( "};\n");
944 print_proxy( "\n");
945 print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
946 print_proxy( "{\n");
947 print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
948 print_proxy( "}\n");
949 print_proxy( "\n");
952 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
953 write_stubdescproto();
954 write_proxy_stmts(stmts, &proc_offset);
956 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
957 if (expr_eval_routines)
958 write_expr_eval_routine_list(proxy, proxy_token);
959 write_user_quad_list(proxy);
960 write_stubdesc(expr_eval_routines);
962 print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
963 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
964 print_proxy( "#endif\n");
965 print_proxy( "\n");
966 write_procformatstring(proxy, stmts, need_proxy);
967 write_typeformatstring(proxy, stmts, need_proxy);
969 interfaces = sort_interfaces(stmts, &count);
970 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
971 fprintf(proxy, "{\n");
972 for (i = 0; i < count; i++)
973 fprintf(proxy, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
974 fprintf(proxy, " 0\n");
975 fprintf(proxy, "};\n");
976 fprintf(proxy, "\n");
978 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
979 fprintf(proxy, "{\n");
980 for (i = 0; i < count; i++)
981 fprintf(proxy, " &_%sStubVtbl,\n", interfaces[i]->name);
982 fprintf(proxy, " 0\n");
983 fprintf(proxy, "};\n");
984 fprintf(proxy, "\n");
986 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
987 fprintf(proxy, "{\n");
988 for (i = 0; i < count; i++)
989 fprintf(proxy, " \"%s\",\n", interfaces[i]->name);
990 fprintf(proxy, " 0\n");
991 fprintf(proxy, "};\n");
992 fprintf(proxy, "\n");
994 if ((have_baseiid = does_any_iface(stmts, need_delegation_indirect)))
996 fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
997 fprintf(proxy, "{\n");
998 for (i = 0; i < count; i++)
1000 if (get_delegation_indirect(interfaces[i], &delegate_to))
1001 fprintf( proxy, " &IID_%s, /* %s */\n", delegate_to->name, interfaces[i]->name );
1002 else
1003 fprintf( proxy, " 0,\n" );
1005 fprintf(proxy, " 0\n");
1006 fprintf(proxy, "};\n");
1007 fprintf(proxy, "\n");
1010 fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
1011 fprintf(proxy, "{\n");
1012 fprintf(proxy, " int low = 0, high = %d;\n", count - 1);
1013 fprintf(proxy, "\n");
1014 fprintf(proxy, " while (low <= high)\n");
1015 fprintf(proxy, " {\n");
1016 fprintf(proxy, " int pos = (low + high) / 2;\n");
1017 fprintf(proxy, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
1018 fprintf(proxy, " if (!res) { *pIndex = pos; return 1; }\n");
1019 fprintf(proxy, " if (res > 0) low = pos + 1;\n");
1020 fprintf(proxy, " else high = pos - 1;\n");
1021 fprintf(proxy, " }\n");
1022 fprintf(proxy, " return 0;\n");
1023 fprintf(proxy, "}\n");
1024 fprintf(proxy, "\n");
1026 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo DECLSPEC_HIDDEN =\n", file_id);
1027 fprintf(proxy, "{\n");
1028 fprintf(proxy, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
1029 fprintf(proxy, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
1030 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
1031 if (have_baseiid) fprintf(proxy, " _%s_BaseIIDList,\n", file_id);
1032 else fprintf(proxy, " 0,\n");
1033 fprintf(proxy, " _%s_IID_Lookup,\n", file_id);
1034 fprintf(proxy, " %d,\n", count);
1035 fprintf(proxy, " %d,\n", stub_mode == MODE_Oif ? 2 : 1);
1036 fprintf(proxy, " 0,\n");
1037 fprintf(proxy, " 0,\n");
1038 fprintf(proxy, " 0,\n");
1039 fprintf(proxy, " 0\n");
1040 fprintf(proxy, "};\n");
1043 void write_proxies(const statement_list_t *stmts)
1045 if (!do_proxies) return;
1046 if (do_everything && !need_proxy_file(stmts)) return;
1048 init_proxy(stmts);
1049 if(!proxy) return;
1051 if (do_win32 && do_win64)
1053 fprintf(proxy, "\n#ifndef _WIN64\n\n");
1054 pointer_size = 4;
1055 write_proxy_routines( stmts );
1056 fprintf(proxy, "\n#else /* _WIN64 */\n\n");
1057 pointer_size = 8;
1058 write_proxy_routines( stmts );
1059 fprintf(proxy, "\n#endif /* _WIN64 */\n");
1061 else if (do_win32)
1063 pointer_size = 4;
1064 write_proxy_routines( stmts );
1066 else if (do_win64)
1068 pointer_size = 8;
1069 write_proxy_routines( stmts );
1072 fclose(proxy);