widl: Add support for interfaces to SLTG typelib generator.
[wine.git] / tools / widl / proxy.c
blob9982efc635e0fe513bb78076bc074d36aed34a55
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"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
29 #include "widl.h"
30 #include "utils.h"
31 #include "parser.h"
32 #include "header.h"
33 #include "typegen.h"
34 #include "expr.h"
36 static FILE* proxy;
37 static int indent = 0;
39 static void print_proxy( const char *format, ... ) __attribute__((format (printf, 1, 2)));
40 static void print_proxy( const char *format, ... )
42 va_list va;
43 va_start( va, format );
44 print( proxy, indent, format, va );
45 va_end( va );
48 static void write_stubdescproto(void)
50 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
51 print_proxy( "\n");
54 static void write_stubdesc(int expr_eval_routines)
56 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc =\n{\n");
57 indent++;
58 print_proxy( "0,\n");
59 print_proxy( "NdrOleAllocate,\n");
60 print_proxy( "NdrOleFree,\n");
61 print_proxy( "{0}, 0, 0, %s, 0,\n", expr_eval_routines ? "ExprEvalRoutines" : "0");
62 print_proxy( "__MIDL_TypeFormatString.Format,\n");
63 print_proxy( "1, /* -error bounds_check flag */\n");
64 print_proxy( "0x%x, /* Ndr library version */\n", interpreted_mode ? 0x50002 : 0x10001);
65 print_proxy( "0,\n");
66 print_proxy( "0x50200ca, /* MIDL Version 5.2.202 */\n");
67 print_proxy( "0,\n");
68 print_proxy("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
69 print_proxy( "0, /* notify & notify_flag routine table */\n");
70 print_proxy( "1, /* Flags */\n");
71 print_proxy( "0, /* Reserved3 */\n");
72 print_proxy( "0, /* Reserved4 */\n");
73 print_proxy( "0 /* Reserved5 */\n");
74 indent--;
75 print_proxy( "};\n");
76 print_proxy( "\n");
79 static void init_proxy(const statement_list_t *stmts)
81 if (proxy) return;
82 if(!(proxy = fopen(proxy_name, "w")))
83 error("Could not open %s for output\n", proxy_name);
84 print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
85 print_proxy( "\n");
86 print_proxy( "#define __midl_proxy\n");
87 print_proxy( "#include \"objbase.h\"\n");
88 print_proxy( "\n");
91 static void clear_output_vars( const var_list_t *args )
93 const var_t *arg;
95 if (!args) return;
96 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
98 if (is_attr(arg->attrs, ATTR_IN)) continue;
99 if (!is_attr(arg->attrs, ATTR_OUT)) continue;
100 if (is_ptr(arg->declspec.type))
102 if (type_get_type(type_pointer_get_ref_type(arg->declspec.type)) == TYPE_BASIC) continue;
103 if (type_get_type(type_pointer_get_ref_type(arg->declspec.type)) == TYPE_ENUM) continue;
105 print_proxy( "if (%s) MIDL_memset( %s, 0, ", arg->name, arg->name );
106 if (is_array(arg->declspec.type) && type_array_has_conformance(arg->declspec.type))
108 write_expr( proxy, type_array_get_conformance(arg->declspec.type), 1, 1, NULL, NULL, "" );
109 fprintf( proxy, " * " );
111 fprintf( proxy, "sizeof( *%s ));\n", arg->name );
115 static int need_delegation(const type_t *iface)
117 const type_t *parent = type_iface_get_inherit( iface );
118 return parent && type_iface_get_inherit(parent) && (parent->ignore || is_local( parent->attrs ));
121 static int get_delegation_indirect(const type_t *iface, const type_t ** delegate_to)
123 const type_t * cur_iface;
124 for (cur_iface = iface; cur_iface != NULL; cur_iface = type_iface_get_inherit(cur_iface))
125 if (need_delegation(cur_iface))
127 if(delegate_to)
128 *delegate_to = type_iface_get_inherit(cur_iface);
129 return 1;
131 return 0;
134 static int need_delegation_indirect(const type_t *iface)
136 return get_delegation_indirect(iface, NULL);
139 static void free_variable( const var_t *arg, const char *local_var_prefix )
141 unsigned int type_offset = arg->typestring_offset;
142 type_t *type = arg->declspec.type;
144 write_parameter_conf_or_var_exprs(proxy, indent, local_var_prefix, PHASE_FREE, arg, FALSE);
146 switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
148 case TGT_ENUM:
149 case TGT_BASIC:
150 break;
152 case TGT_STRUCT:
153 if (get_struct_fc(type) != FC_STRUCT)
154 print_proxy("/* FIXME: %s code for %s struct type 0x%x missing */\n", __FUNCTION__, arg->name, get_struct_fc(type) );
155 break;
157 case TGT_IFACE_POINTER:
158 case TGT_POINTER:
159 case TGT_ARRAY:
160 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
161 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
162 fprintf(proxy, "(void *)%s );\n", arg->name );
163 break;
165 default:
166 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
170 static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
172 const var_t *arg;
174 if (!args) return;
175 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
176 if (is_attr(arg->attrs, ATTR_OUT))
178 free_variable( arg, local_var_prefix );
179 fprintf(proxy, "\n");
183 static void gen_proxy(type_t *iface, const var_t *func, int idx,
184 unsigned int proc_offset)
186 var_t *retval = type_function_get_retval(func->declspec.type);
187 int has_ret = !is_void(retval->declspec.type);
188 int has_full_pointer = is_full_pointer_function(func);
189 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
190 const var_list_t *args = type_function_get_args(func->declspec.type);
191 if (!callconv) callconv = "STDMETHODCALLTYPE";
193 indent = 0;
194 if (is_interpreted_func( iface, func ))
196 if (!is_callas( func->attrs )) return;
197 write_type_decl_left(proxy, &retval->declspec);
198 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
199 write_args(proxy, args, iface->name, 1, TRUE, NAME_DEFAULT);
200 print_proxy( ")\n");
201 write_client_call_routine( proxy, iface, func, "Object", proc_offset );
202 return;
204 print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
205 iface->name, get_name(func) );
206 print_proxy( "{\n");
207 indent++;
208 if (has_full_pointer) write_full_pointer_free(proxy, indent, func);
209 print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
210 indent--;
211 print_proxy( "}\n");
212 print_proxy( "\n");
214 write_type_decl_left(proxy, &retval->declspec);
215 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
216 write_args(proxy, args, iface->name, 1, TRUE, NAME_DEFAULT);
217 print_proxy( ")\n");
218 print_proxy( "{\n");
219 indent ++;
220 print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
221 /* local variables */
222 if (has_ret) {
223 print_proxy( "%s", "" );
224 write_type_decl(proxy, &retval->declspec, retval->name);
225 fprintf( proxy, ";\n" );
227 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
228 if (has_ret) {
229 if (decl_indirect(retval->declspec.type))
230 print_proxy("void *_p_%s = &%s;\n", retval->name, retval->name);
232 print_proxy( "\n");
234 print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(func) );
235 print_proxy( "__frame->This = This;\n" );
237 if (has_full_pointer)
238 write_full_pointer_init(proxy, indent, func, FALSE);
240 /* FIXME: trace */
241 clear_output_vars( type_function_get_args(func->declspec.type) );
243 print_proxy( "RpcTryExcept\n" );
244 print_proxy( "{\n" );
245 indent++;
246 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx);
247 write_pointer_checks( proxy, indent, func );
249 print_proxy( "RpcTryFinally\n" );
250 print_proxy( "{\n" );
251 indent++;
253 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
255 print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
257 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_MARSHAL);
259 print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
260 fprintf(proxy, "\n");
261 print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
262 print_proxy( "__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
264 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
265 indent++;
266 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
267 indent--;
268 fprintf(proxy, "\n");
270 write_remoting_arguments(proxy, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
272 if (has_ret)
274 if (decl_indirect(retval->declspec.type))
275 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", retval->name, retval->name);
276 else if (is_ptr(retval->declspec.type) || is_array(retval->declspec.type))
277 print_proxy("%s = 0;\n", retval->name);
278 write_remoting_arguments(proxy, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
281 indent--;
282 print_proxy( "}\n");
283 print_proxy( "RpcFinally\n" );
284 print_proxy( "{\n" );
285 indent++;
286 print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(func) );
287 indent--;
288 print_proxy( "}\n");
289 print_proxy( "RpcEndFinally\n" );
290 indent--;
291 print_proxy( "}\n" );
292 print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
293 print_proxy( "{\n" );
294 if (has_ret) {
295 indent++;
296 proxy_free_variables( type_function_get_args(func->declspec.type), "" );
297 print_proxy( "/* coverity[uninit_use_in_call:SUPPRESS] */\n" );
298 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
299 indent--;
301 print_proxy( "}\n" );
302 print_proxy( "RpcEndExcept\n" );
304 if (has_ret) {
305 print_proxy( "return _RetVal;\n" );
307 indent--;
308 print_proxy( "}\n");
309 print_proxy( "\n");
312 static void gen_stub(type_t *iface, const var_t *func, const char *cas,
313 unsigned int proc_offset)
315 const var_t *arg;
316 int has_ret = !is_void(type_function_get_rettype(func->declspec.type));
317 int has_full_pointer = is_full_pointer_function(func);
319 if (is_interpreted_func( iface, func )) return;
321 indent = 0;
322 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(func));
323 indent++;
324 print_proxy( "__DECL_EXCEPTION_FRAME\n" );
325 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
326 print_proxy( "%s * _This;\n", iface->name );
327 declare_stub_args( proxy, indent, func );
328 indent--;
329 print_proxy( "};\n\n" );
331 print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(func) );
332 print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(func) );
333 indent++;
334 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
335 if (has_full_pointer)
336 write_full_pointer_free(proxy, indent, func);
337 indent--;
338 print_proxy( "}\n\n" );
340 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
341 indent++;
342 print_proxy( "IRpcStubBuffer* This,\n");
343 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
344 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
345 print_proxy( "DWORD* _pdwStubPhase)\n");
346 indent--;
347 print_proxy( "{\n");
348 indent++;
349 print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
350 iface->name, get_name(func) );
352 print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
354 /* FIXME: trace */
356 print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
357 fprintf(proxy, "\n");
358 print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(func) );
360 write_parameters_init(proxy, indent, func, "__frame->");
362 print_proxy("RpcTryFinally\n");
363 print_proxy("{\n");
364 indent++;
365 if (has_full_pointer)
366 write_full_pointer_init(proxy, indent, func, TRUE);
367 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
368 indent++;
369 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
370 indent--;
371 fprintf(proxy, "\n");
373 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
374 fprintf(proxy, "\n");
376 assign_stub_out_args( proxy, indent, func, "__frame->" );
378 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
379 fprintf(proxy, "\n");
380 print_proxy( "%s", has_ret ? "__frame->_RetVal = " : "" );
381 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
382 else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(func));
383 fprintf(proxy, "(__frame->_This");
385 if (type_function_get_args(func->declspec.type))
387 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
388 fprintf(proxy, ", %s__frame->%s", is_array(arg->declspec.type) && !type_array_is_decl_as_ptr(arg->declspec.type) ? "*" :"" , arg->name);
390 fprintf(proxy, ");\n");
391 fprintf(proxy, "\n");
392 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
393 fprintf(proxy, "\n");
395 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
397 if (!is_void(type_function_get_rettype(func->declspec.type)))
398 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
400 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
402 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
403 fprintf(proxy, "\n");
405 /* marshall the return value */
406 if (!is_void(type_function_get_rettype(func->declspec.type)))
407 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
409 indent--;
410 print_proxy("}\n");
411 print_proxy("RpcFinally\n");
412 print_proxy("{\n");
413 indent++;
414 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(func) );
415 indent--;
416 print_proxy("}\n");
417 print_proxy("RpcEndFinally\n");
419 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
420 indent--;
422 print_proxy("}\n");
423 print_proxy("\n");
426 static void gen_stub_thunk( type_t *iface, const var_t *func, unsigned int proc_offset )
428 int has_ret = !is_void( type_function_get_rettype( func->declspec.type ));
429 const var_t *arg, *callas = is_callas( func->attrs );
430 const var_list_t *args = type_function_get_args( func->declspec.type );
432 indent = 0;
433 print_proxy( "void __RPC_API %s_%s_Thunk( PMIDL_STUB_MESSAGE pStubMsg )\n",
434 iface->name, get_name(func) );
435 print_proxy( "{\n");
436 indent++;
437 write_func_param_struct( proxy, iface, func->declspec.type,
438 "*pParamStruct = (struct _PARAM_STRUCT *)pStubMsg->StackTop", has_ret );
439 print_proxy( "%s%s_%s_Stub( pParamStruct->This",
440 has_ret ? "pParamStruct->_RetVal = " : "", iface->name, callas->name );
441 indent++;
442 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
444 fprintf( proxy, ",\n%*spParamStruct->%s", 4 * indent, "", arg->name );
446 fprintf( proxy, " );\n" );
447 indent--;
448 indent--;
449 print_proxy( "}\n\n");
452 int count_methods(const type_t *iface)
454 const statement_t *stmt;
455 int count = 0;
457 if (type_iface_get_inherit(iface))
458 count = count_methods(type_iface_get_inherit(iface));
460 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
461 const var_t *func = stmt->u.var;
462 if (!is_callas(func->attrs)) count++;
464 return count;
467 const statement_t *get_callas_source(const type_t *iface, const var_t *def)
469 const statement_t * source;
470 STATEMENTS_FOR_EACH_FUNC( source, type_iface_get_stmts(iface)) {
471 const var_t * cas = is_callas(source->u.var->attrs );
472 if (cas && !strcmp(def->name, cas->name))
473 return source;
475 return NULL;
478 static void write_proxy_procformatstring_offsets( const type_t *iface, int skip )
480 const statement_t *stmt;
482 if (type_iface_get_inherit(iface))
483 write_proxy_procformatstring_offsets( type_iface_get_inherit(iface), need_delegation(iface));
484 else
485 return;
487 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
489 const var_t *func = stmt->u.var;
490 int missing = 0;
492 if (is_callas(func->attrs)) continue;
493 if (is_local(func->attrs))
495 const statement_t * callas_source = get_callas_source(iface, func);
496 if (!callas_source)
497 missing = 1;
498 else
499 func = callas_source->u.var;
501 if (skip || missing)
502 print_proxy( "(unsigned short)-1, /* %s::%s */\n", iface->name, get_name(func));
503 else
504 print_proxy( "%u, /* %s::%s */\n", func->procstring_offset, iface->name, get_name(func));
508 static int write_proxy_methods(type_t *iface, int skip)
510 const statement_t *stmt;
511 int i = 0;
513 if (type_iface_get_inherit(iface))
514 i = write_proxy_methods(type_iface_get_inherit(iface),
515 need_delegation(iface));
516 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
517 const var_t *func = stmt->u.var;
518 if (!is_callas(func->attrs)) {
519 if (skip || (is_local(func->attrs) && !get_callas_source(iface, func)))
520 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func));
521 else if (is_interpreted_func( iface, func ) &&
522 !is_local( func->attrs ) &&
523 type_iface_get_inherit(iface))
524 print_proxy( "(void *)-1, /* %s::%s */\n", iface->name, get_name(func));
525 else
526 print_proxy( "%s_%s_Proxy,\n", iface->name, get_name(func));
527 i++;
530 return i;
533 static int write_stub_methods(type_t *iface, int skip)
535 const statement_t *stmt;
536 int i = 0;
538 if (type_iface_get_inherit(iface))
539 i = write_stub_methods(type_iface_get_inherit(iface), need_delegation(iface));
540 else
541 return i; /* skip IUnknown */
543 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
544 const var_t *func = stmt->u.var;
545 if (!is_callas(func->attrs)) {
546 int missing = 0;
547 const char * fname = get_name(func);
548 if(is_local(func->attrs)) {
549 const statement_t * callas_source = get_callas_source(iface, func);
550 if(!callas_source)
551 missing = 1;
552 else
553 fname = get_name(callas_source->u.var);
555 if (i) fprintf(proxy,",\n");
556 if (skip || missing) print_proxy("STUB_FORWARDING_FUNCTION");
557 else if (is_interpreted_func( iface, func ))
558 print_proxy( "(PRPC_STUB_FUNCTION)%s", interpreted_mode ? "NdrStubCall2" : "NdrStubCall" );
559 else print_proxy( "%s_%s_Stub", iface->name, fname);
560 i++;
563 return i;
566 static void write_thunk_methods( type_t *iface, int skip )
568 const statement_t *stmt;
570 if (type_iface_get_inherit( iface ))
571 write_thunk_methods( type_iface_get_inherit(iface), need_delegation(iface) );
572 else
573 return; /* skip IUnknown */
575 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
577 var_t *func = stmt->u.var;
578 const statement_t * callas_source = NULL;
580 if (is_callas(func->attrs)) continue;
581 if (is_local(func->attrs)) callas_source = get_callas_source(iface, func);
583 if (!skip && callas_source && is_interpreted_func( iface, func ))
584 print_proxy( "%s_%s_Thunk,\n", iface->name, get_name(callas_source->u.var) );
585 else
586 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func) );
590 static void write_proxy(type_t *iface, unsigned int *proc_offset)
592 int count;
593 const statement_t *stmt;
594 int first_func = 1;
595 int needs_stub_thunks = 0;
596 int needs_inline_stubs = need_inline_stubs( iface ) || need_delegation( iface );
598 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
599 var_t *func = stmt->u.var;
600 if (first_func) {
601 fprintf(proxy, "/*****************************************************************************\n");
602 fprintf(proxy, " * %s interface\n", iface->name);
603 fprintf(proxy, " */\n");
604 first_func = 0;
606 if (!is_local(func->attrs)) {
607 const var_t *cas = is_callas(func->attrs);
608 const char *cname = cas ? cas->name : NULL;
609 int idx = func->func_idx;
610 if (cname) {
611 const statement_t *stmt2;
612 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface)) {
613 const var_t *m = stmt2->u.var;
614 if (!strcmp(m->name, cname))
616 idx = m->func_idx;
617 break;
621 func->procstring_offset = *proc_offset;
622 gen_proxy(iface, func, idx, *proc_offset);
623 gen_stub(iface, func, cname, *proc_offset);
624 if (cas && is_interpreted_func( iface, func ))
626 needs_stub_thunks = 1;
627 gen_stub_thunk(iface, func, *proc_offset);
629 *proc_offset += get_size_procformatstring_func( iface, func );
633 count = count_methods(iface);
635 print_proxy( "static const unsigned short %s_FormatStringOffsetTable[] =\n", iface->name );
636 print_proxy( "{\n" );
637 indent++;
638 write_proxy_procformatstring_offsets( iface, 0 );
639 indent--;
640 print_proxy( "};\n\n" );
642 /* proxy info */
643 if (interpreted_mode)
645 print_proxy( "static const MIDL_STUBLESS_PROXY_INFO %s_ProxyInfo =\n", iface->name );
646 print_proxy( "{\n" );
647 indent++;
648 print_proxy( "&Object_StubDesc,\n" );
649 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
650 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
651 print_proxy( "0,\n" );
652 print_proxy( "0,\n" );
653 print_proxy( "0\n" );
654 indent--;
655 print_proxy( "};\n\n" );
658 /* proxy vtable */
659 print_proxy( "static %sCINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n",
660 (interpreted_mode || need_delegation_indirect(iface)) ? "" : "const ",
661 count, iface->name);
662 print_proxy( "{\n");
663 indent++;
664 print_proxy( "{\n");
665 indent++;
666 if (interpreted_mode) print_proxy( "&%s_ProxyInfo,\n", iface->name );
667 print_proxy( "&IID_%s,\n", iface->name);
668 indent--;
669 print_proxy( "},\n");
670 print_proxy( "{\n");
671 indent++;
672 write_proxy_methods(iface, FALSE);
673 indent--;
674 print_proxy( "}\n");
675 indent--;
676 print_proxy( "};\n\n");
678 /* stub thunk table */
679 if (needs_stub_thunks)
681 print_proxy( "static const STUB_THUNK %s_StubThunkTable[] =\n", iface->name);
682 print_proxy( "{\n");
683 indent++;
684 write_thunk_methods( iface, 0 );
685 indent--;
686 print_proxy( "};\n\n");
689 /* server info */
690 print_proxy( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
691 print_proxy( "{\n" );
692 indent++;
693 print_proxy( "&Object_StubDesc,\n" );
694 print_proxy( "0,\n" );
695 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
696 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
697 if (needs_stub_thunks)
698 print_proxy( "&%s_StubThunkTable[-3],\n", iface->name );
699 else
700 print_proxy( "0,\n" );
701 print_proxy( "0,\n" );
702 print_proxy( "0,\n" );
703 print_proxy( "0\n" );
704 indent--;
705 print_proxy( "};\n\n" );
707 /* stub vtable */
708 if (needs_inline_stubs)
710 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
711 print_proxy( "{\n");
712 indent++;
713 write_stub_methods(iface, FALSE);
714 fprintf(proxy, "\n");
715 indent--;
716 fprintf(proxy, "};\n\n");
718 print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
719 need_delegation_indirect(iface) ? "" : "const ", iface->name);
720 print_proxy( "{\n");
721 indent++;
722 print_proxy( "{\n");
723 indent++;
724 print_proxy( "&IID_%s,\n", iface->name);
725 print_proxy( "&%s_ServerInfo,\n", iface->name );
726 print_proxy( "%d,\n", count);
727 if (needs_inline_stubs) print_proxy( "&%s_table[-3]\n", iface->name );
728 else print_proxy( "0\n" );
729 indent--;
730 print_proxy( "},\n");
731 print_proxy( "{\n");
732 indent++;
733 print_proxy( "%s_%s\n",
734 type_iface_get_async_iface(iface) == iface ? "CStdAsyncStubBuffer" : "CStdStubBuffer",
735 need_delegation_indirect(iface) ? "DELEGATING_METHODS" : "METHODS");
736 indent--;
737 print_proxy( "}\n");
738 indent--;
739 print_proxy( "};\n");
740 print_proxy( "\n");
743 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
745 const statement_t *stmt;
747 if (stmts)
748 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
750 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
752 if (pred(stmt->u.type))
753 return TRUE;
757 return FALSE;
760 int need_proxy(const type_t *iface)
762 if (!is_object( iface )) return 0;
763 if (is_local( iface->attrs )) return 0;
764 if (is_attr( iface->attrs, ATTR_DISPINTERFACE )) return 0;
765 return 1;
768 int need_stub(const type_t *iface)
770 return !is_object(iface) && !is_local(iface->attrs);
773 int need_proxy_file(const statement_list_t *stmts)
775 return does_any_iface(stmts, need_proxy);
778 int need_proxy_delegation(const statement_list_t *stmts)
780 return does_any_iface(stmts, need_delegation);
783 int need_inline_stubs(const type_t *iface)
785 const statement_t *stmt;
787 if (!interpreted_mode) return 1;
789 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
791 const var_t *func = stmt->u.var;
792 if (is_local( func->attrs )) continue;
793 if (!is_interpreted_func( iface, func )) return 1;
795 return 0;
798 static int need_proxy_and_inline_stubs(const type_t *iface)
800 const statement_t *stmt;
802 if (!need_proxy( iface )) return 0;
803 if (!interpreted_mode) return 1;
805 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
807 const var_t *func = stmt->u.var;
808 if (is_local( func->attrs )) continue;
809 if (!is_interpreted_func( iface, func )) return 1;
811 return 0;
814 int need_stub_files(const statement_list_t *stmts)
816 return does_any_iface(stmts, need_stub);
819 int need_inline_stubs_file(const statement_list_t *stmts)
821 return does_any_iface(stmts, need_inline_stubs);
824 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
826 const statement_t *stmt;
827 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
829 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
831 type_t *iface = stmt->u.type;
832 if (need_proxy(iface))
834 write_proxy(iface, proc_offset);
835 if (type_iface_get_async_iface(iface))
836 write_proxy(type_iface_get_async_iface(iface), proc_offset);
842 static int cmp_iid( const void *ptr1, const void *ptr2 )
844 const type_t * const *iface1 = ptr1;
845 const type_t * const *iface2 = ptr2;
846 const struct uuid *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
847 const struct uuid *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
848 return memcmp( uuid1, uuid2, sizeof(*uuid1) );
851 static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
853 const statement_t *stmt;
855 if (!stmts) return;
856 LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
858 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
860 type_t *iface = stmt->u.type;
861 if (type_iface_get_inherit(iface) && need_proxy(iface))
863 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
864 (*ifaces)[(*count)++] = iface;
865 if (type_iface_get_async_iface(iface))
867 iface = type_iface_get_async_iface(iface);
868 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
869 (*ifaces)[(*count)++] = iface;
876 static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
878 type_t **ifaces = NULL;
880 *count = 0;
881 build_iface_list( stmts, &ifaces, count );
882 qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
883 return ifaces;
886 static void write_proxy_routines(const statement_list_t *stmts)
888 int expr_eval_routines;
889 unsigned int proc_offset = 0;
890 char *file_id = proxy_token;
891 int i, count, have_baseiid = 0;
892 unsigned int table_version;
893 type_t **interfaces;
894 const type_t * delegate_to;
896 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
897 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ %u\n", interpreted_mode ? 475 : 440);
898 print_proxy( "#endif\n");
899 print_proxy( "\n");
900 if (interpreted_mode) print_proxy( "#define USE_STUBLESS_PROXY\n");
901 print_proxy( "#include \"rpcproxy.h\"\n");
902 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
903 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
904 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
905 print_proxy( "\n");
906 print_proxy( "#include \"%s\"\n", header_name);
907 print_proxy( "\n");
909 if (does_any_iface(stmts, need_proxy_and_inline_stubs))
911 write_exceptions( proxy );
912 print_proxy( "\n");
913 print_proxy( "struct __proxy_frame\n");
914 print_proxy( "{\n");
915 print_proxy( " __DECL_EXCEPTION_FRAME\n");
916 print_proxy( " MIDL_STUB_MESSAGE _StubMsg;\n");
917 print_proxy( " void *This;\n");
918 print_proxy( "};\n");
919 print_proxy( "\n");
920 print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
921 print_proxy( "{\n");
922 print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
923 print_proxy( "}\n");
924 print_proxy( "\n");
927 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
928 write_stubdescproto();
929 write_proxy_stmts(stmts, &proc_offset);
931 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
932 if (expr_eval_routines)
933 write_expr_eval_routine_list(proxy, proxy_token);
934 write_user_quad_list(proxy);
935 write_stubdesc(expr_eval_routines);
937 print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
938 print_proxy( "#error Invalid build platform for this proxy.\n");
939 print_proxy( "#endif\n");
940 print_proxy( "\n");
941 write_procformatstring(proxy, stmts, need_proxy);
942 write_typeformatstring(proxy, stmts, need_proxy);
944 interfaces = sort_interfaces(stmts, &count);
945 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
946 fprintf(proxy, "{\n");
947 for (i = 0; i < count; i++)
948 fprintf(proxy, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
949 fprintf(proxy, " 0\n");
950 fprintf(proxy, "};\n");
951 fprintf(proxy, "\n");
953 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
954 fprintf(proxy, "{\n");
955 for (i = 0; i < count; i++)
956 fprintf(proxy, " &_%sStubVtbl,\n", interfaces[i]->name);
957 fprintf(proxy, " 0\n");
958 fprintf(proxy, "};\n");
959 fprintf(proxy, "\n");
961 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
962 fprintf(proxy, "{\n");
963 for (i = 0; i < count; i++)
964 fprintf(proxy, " \"%s\",\n", interfaces[i]->name);
965 fprintf(proxy, " 0\n");
966 fprintf(proxy, "};\n");
967 fprintf(proxy, "\n");
969 for (i = 0; i < count; i++)
970 if ((have_baseiid = get_delegation_indirect( interfaces[i], NULL ))) break;
972 if (have_baseiid)
974 fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
975 fprintf(proxy, "{\n");
976 for (i = 0; i < count; i++)
978 if (get_delegation_indirect(interfaces[i], &delegate_to))
979 fprintf( proxy, " &IID_%s, /* %s */\n", delegate_to->name, interfaces[i]->name );
980 else
981 fprintf( proxy, " 0,\n" );
983 fprintf(proxy, " 0\n");
984 fprintf(proxy, "};\n");
985 fprintf(proxy, "\n");
988 fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
989 fprintf(proxy, "{\n");
990 fprintf(proxy, " int low = 0, high = %d;\n", count - 1);
991 fprintf(proxy, "\n");
992 fprintf(proxy, " while (low <= high)\n");
993 fprintf(proxy, " {\n");
994 fprintf(proxy, " int pos = (low + high) / 2;\n");
995 fprintf(proxy, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
996 fprintf(proxy, " if (!res) { *pIndex = pos; return 1; }\n");
997 fprintf(proxy, " if (res > 0) low = pos + 1;\n");
998 fprintf(proxy, " else high = pos - 1;\n");
999 fprintf(proxy, " }\n");
1000 fprintf(proxy, " return 0;\n");
1001 fprintf(proxy, "}\n");
1002 fprintf(proxy, "\n");
1004 table_version = interpreted_mode ? 2 : 1;
1005 for (i = 0; i < count; i++)
1007 if (type_iface_get_async_iface(interfaces[i]) != interfaces[i]) continue;
1008 if (table_version != 6)
1010 fprintf(proxy, "static const IID *_AsyncInterfaceTable[] =\n");
1011 fprintf(proxy, "{\n");
1012 table_version = 6;
1014 fprintf(proxy, " &IID_%s,\n", interfaces[i]->name);
1015 fprintf(proxy, " (IID*)(LONG_PTR)-1,\n");
1017 if (table_version == 6)
1019 fprintf(proxy, " 0\n");
1020 fprintf(proxy, "};\n");
1021 fprintf(proxy, "\n");
1024 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id);
1025 fprintf(proxy, "{\n");
1026 fprintf(proxy, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
1027 fprintf(proxy, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
1028 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
1029 if (have_baseiid) fprintf(proxy, " _%s_BaseIIDList,\n", file_id);
1030 else fprintf(proxy, " 0,\n");
1031 fprintf(proxy, " _%s_IID_Lookup,\n", file_id);
1032 fprintf(proxy, " %d,\n", count);
1033 fprintf(proxy, " %u,\n", table_version);
1034 fprintf(proxy, " %s,\n", table_version == 6 ? "_AsyncInterfaceTable" : "0");
1035 fprintf(proxy, " 0,\n");
1036 fprintf(proxy, " 0,\n");
1037 fprintf(proxy, " 0\n");
1038 fprintf(proxy, "};\n");
1041 void write_proxies(const statement_list_t *stmts)
1043 if (!do_proxies) return;
1044 if (do_everything && !need_proxy_file(stmts)) return;
1046 init_proxy(stmts);
1047 if(!proxy) return;
1049 write_proxy_routines( stmts );
1050 fclose(proxy);