wineps: Don't use CDECL for private functions.
[wine.git] / tools / widl / proxy.c
blob3d55cd73ee7fc59cfe2132eee615f57d4cce78a7
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", get_stub_mode() == MODE_Oif ? 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");
89 print_proxy( "#ifndef DECLSPEC_HIDDEN\n");
90 print_proxy( "#define DECLSPEC_HIDDEN\n");
91 print_proxy( "#endif\n");
92 print_proxy( "\n");
95 static void clear_output_vars( const var_list_t *args )
97 const var_t *arg;
99 if (!args) return;
100 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
102 if (is_attr(arg->attrs, ATTR_IN)) continue;
103 if (!is_attr(arg->attrs, ATTR_OUT)) continue;
104 if (is_ptr(arg->declspec.type))
106 if (type_get_type(type_pointer_get_ref_type(arg->declspec.type)) == TYPE_BASIC) continue;
107 if (type_get_type(type_pointer_get_ref_type(arg->declspec.type)) == TYPE_ENUM) continue;
109 print_proxy( "if (%s) MIDL_memset( %s, 0, ", arg->name, arg->name );
110 if (is_array(arg->declspec.type) && type_array_has_conformance(arg->declspec.type))
112 write_expr( proxy, type_array_get_conformance(arg->declspec.type), 1, 1, NULL, NULL, "" );
113 fprintf( proxy, " * " );
115 fprintf( proxy, "sizeof( *%s ));\n", arg->name );
119 static int need_delegation(const type_t *iface)
121 const type_t *parent = type_iface_get_inherit( iface );
122 return parent && type_iface_get_inherit(parent) && (parent->ignore || is_local( parent->attrs ));
125 static int get_delegation_indirect(const type_t *iface, const type_t ** delegate_to)
127 const type_t * cur_iface;
128 for (cur_iface = iface; cur_iface != NULL; cur_iface = type_iface_get_inherit(cur_iface))
129 if (need_delegation(cur_iface))
131 if(delegate_to)
132 *delegate_to = type_iface_get_inherit(cur_iface);
133 return 1;
135 return 0;
138 static int need_delegation_indirect(const type_t *iface)
140 return get_delegation_indirect(iface, NULL);
143 static void free_variable( const var_t *arg, const char *local_var_prefix )
145 unsigned int type_offset = arg->typestring_offset;
146 type_t *type = arg->declspec.type;
148 write_parameter_conf_or_var_exprs(proxy, indent, local_var_prefix, PHASE_FREE, arg, FALSE);
150 switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
152 case TGT_ENUM:
153 case TGT_BASIC:
154 break;
156 case TGT_STRUCT:
157 if (get_struct_fc(type) != FC_STRUCT)
158 print_proxy("/* FIXME: %s code for %s struct type 0x%x missing */\n", __FUNCTION__, arg->name, get_struct_fc(type) );
159 break;
161 case TGT_IFACE_POINTER:
162 case TGT_POINTER:
163 case TGT_ARRAY:
164 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
165 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
166 fprintf(proxy, "(void *)%s );\n", arg->name );
167 break;
169 default:
170 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
174 static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
176 const var_t *arg;
178 if (!args) return;
179 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
180 if (is_attr(arg->attrs, ATTR_OUT))
182 free_variable( arg, local_var_prefix );
183 fprintf(proxy, "\n");
187 static void gen_proxy(type_t *iface, const var_t *func, int idx,
188 unsigned int proc_offset)
190 var_t *retval = type_function_get_retval(func->declspec.type);
191 int has_ret = !is_void(retval->declspec.type);
192 int has_full_pointer = is_full_pointer_function(func);
193 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
194 const var_list_t *args = type_function_get_args(func->declspec.type);
195 if (!callconv) callconv = "STDMETHODCALLTYPE";
197 indent = 0;
198 if (is_interpreted_func( iface, func ))
200 if (get_stub_mode() == MODE_Oif && !is_callas( func->attrs )) return;
201 write_type_decl_left(proxy, &retval->declspec);
202 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
203 write_args(proxy, args, iface->name, 1, TRUE, NAME_DEFAULT);
204 print_proxy( ")\n");
205 write_client_call_routine( proxy, iface, func, "Object", proc_offset );
206 return;
208 print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
209 iface->name, get_name(func) );
210 print_proxy( "{\n");
211 indent++;
212 if (has_full_pointer) write_full_pointer_free(proxy, indent, func);
213 print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
214 indent--;
215 print_proxy( "}\n");
216 print_proxy( "\n");
218 write_type_decl_left(proxy, &retval->declspec);
219 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
220 write_args(proxy, args, iface->name, 1, TRUE, NAME_DEFAULT);
221 print_proxy( ")\n");
222 print_proxy( "{\n");
223 indent ++;
224 print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
225 /* local variables */
226 if (has_ret) {
227 print_proxy( "%s", "" );
228 write_type_decl(proxy, &retval->declspec, retval->name);
229 fprintf( proxy, ";\n" );
231 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
232 if (has_ret) {
233 if (decl_indirect(retval->declspec.type))
234 print_proxy("void *_p_%s = &%s;\n", retval->name, retval->name);
236 print_proxy( "\n");
238 print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(func) );
239 print_proxy( "__frame->This = This;\n" );
241 if (has_full_pointer)
242 write_full_pointer_init(proxy, indent, func, FALSE);
244 /* FIXME: trace */
245 clear_output_vars( type_function_get_args(func->declspec.type) );
247 print_proxy( "RpcTryExcept\n" );
248 print_proxy( "{\n" );
249 indent++;
250 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx);
251 write_pointer_checks( proxy, indent, func );
253 print_proxy( "RpcTryFinally\n" );
254 print_proxy( "{\n" );
255 indent++;
257 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
259 print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
261 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_MARSHAL);
263 print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
264 fprintf(proxy, "\n");
265 print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
266 print_proxy( "__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
268 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
269 indent++;
270 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
271 indent--;
272 fprintf(proxy, "\n");
274 write_remoting_arguments(proxy, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
276 if (has_ret)
278 if (decl_indirect(retval->declspec.type))
279 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", retval->name, retval->name);
280 else if (is_ptr(retval->declspec.type) || is_array(retval->declspec.type))
281 print_proxy("%s = 0;\n", retval->name);
282 write_remoting_arguments(proxy, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
285 indent--;
286 print_proxy( "}\n");
287 print_proxy( "RpcFinally\n" );
288 print_proxy( "{\n" );
289 indent++;
290 print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(func) );
291 indent--;
292 print_proxy( "}\n");
293 print_proxy( "RpcEndFinally\n" );
294 indent--;
295 print_proxy( "}\n" );
296 print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
297 print_proxy( "{\n" );
298 if (has_ret) {
299 indent++;
300 proxy_free_variables( type_function_get_args(func->declspec.type), "" );
301 print_proxy( "/* coverity[uninit_use_in_call:SUPPRESS] */\n" );
302 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
303 indent--;
305 print_proxy( "}\n" );
306 print_proxy( "RpcEndExcept\n" );
308 if (has_ret) {
309 print_proxy( "return _RetVal;\n" );
311 indent--;
312 print_proxy( "}\n");
313 print_proxy( "\n");
316 static void gen_stub(type_t *iface, const var_t *func, const char *cas,
317 unsigned int proc_offset)
319 const var_t *arg;
320 int has_ret = !is_void(type_function_get_rettype(func->declspec.type));
321 int has_full_pointer = is_full_pointer_function(func);
323 if (is_interpreted_func( iface, func )) return;
325 indent = 0;
326 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(func));
327 indent++;
328 print_proxy( "__DECL_EXCEPTION_FRAME\n" );
329 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
330 print_proxy( "%s * _This;\n", iface->name );
331 declare_stub_args( proxy, indent, func );
332 indent--;
333 print_proxy( "};\n\n" );
335 print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(func) );
336 print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(func) );
337 indent++;
338 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
339 if (has_full_pointer)
340 write_full_pointer_free(proxy, indent, func);
341 indent--;
342 print_proxy( "}\n\n" );
344 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
345 indent++;
346 print_proxy( "IRpcStubBuffer* This,\n");
347 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
348 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
349 print_proxy( "DWORD* _pdwStubPhase)\n");
350 indent--;
351 print_proxy( "{\n");
352 indent++;
353 print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
354 iface->name, get_name(func) );
356 print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
358 /* FIXME: trace */
360 print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
361 fprintf(proxy, "\n");
362 print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(func) );
364 write_parameters_init(proxy, indent, func, "__frame->");
366 print_proxy("RpcTryFinally\n");
367 print_proxy("{\n");
368 indent++;
369 if (has_full_pointer)
370 write_full_pointer_init(proxy, indent, func, TRUE);
371 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
372 indent++;
373 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
374 indent--;
375 fprintf(proxy, "\n");
377 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
378 fprintf(proxy, "\n");
380 assign_stub_out_args( proxy, indent, func, "__frame->" );
382 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
383 fprintf(proxy, "\n");
384 print_proxy( "%s", has_ret ? "__frame->_RetVal = " : "" );
385 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
386 else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(func));
387 fprintf(proxy, "(__frame->_This");
389 if (type_function_get_args(func->declspec.type))
391 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
392 fprintf(proxy, ", %s__frame->%s", is_array(arg->declspec.type) && !type_array_is_decl_as_ptr(arg->declspec.type) ? "*" :"" , arg->name);
394 fprintf(proxy, ");\n");
395 fprintf(proxy, "\n");
396 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
397 fprintf(proxy, "\n");
399 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
401 if (!is_void(type_function_get_rettype(func->declspec.type)))
402 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
404 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
406 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
407 fprintf(proxy, "\n");
409 /* marshall the return value */
410 if (!is_void(type_function_get_rettype(func->declspec.type)))
411 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
413 indent--;
414 print_proxy("}\n");
415 print_proxy("RpcFinally\n");
416 print_proxy("{\n");
417 indent++;
418 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(func) );
419 indent--;
420 print_proxy("}\n");
421 print_proxy("RpcEndFinally\n");
423 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
424 indent--;
426 print_proxy("}\n");
427 print_proxy("\n");
430 static void gen_stub_thunk( type_t *iface, const var_t *func, unsigned int proc_offset )
432 int has_ret = !is_void( type_function_get_rettype( func->declspec.type ));
433 const var_t *arg, *callas = is_callas( func->attrs );
434 const var_list_t *args = type_function_get_args( func->declspec.type );
436 indent = 0;
437 print_proxy( "void __RPC_API %s_%s_Thunk( PMIDL_STUB_MESSAGE pStubMsg )\n",
438 iface->name, get_name(func) );
439 print_proxy( "{\n");
440 indent++;
441 write_func_param_struct( proxy, iface, func->declspec.type,
442 "*pParamStruct = (struct _PARAM_STRUCT *)pStubMsg->StackTop", has_ret );
443 print_proxy( "%s%s_%s_Stub( pParamStruct->This",
444 has_ret ? "pParamStruct->_RetVal = " : "", iface->name, callas->name );
445 indent++;
446 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
448 fprintf( proxy, ",\n%*spParamStruct->%s", 4 * indent, "", arg->name );
450 fprintf( proxy, " );\n" );
451 indent--;
452 indent--;
453 print_proxy( "}\n\n");
456 int count_methods(const type_t *iface)
458 const statement_t *stmt;
459 int count = 0;
461 if (type_iface_get_inherit(iface))
462 count = count_methods(type_iface_get_inherit(iface));
464 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
465 const var_t *func = stmt->u.var;
466 if (!is_callas(func->attrs)) count++;
468 return count;
471 static const statement_t * get_callas_source(const type_t * iface, const var_t * def)
473 const statement_t * source;
474 STATEMENTS_FOR_EACH_FUNC( source, type_iface_get_stmts(iface)) {
475 const var_t * cas = is_callas(source->u.var->attrs );
476 if (cas && !strcmp(def->name, cas->name))
477 return source;
479 return NULL;
482 static void write_proxy_procformatstring_offsets( const type_t *iface, int skip )
484 const statement_t *stmt;
486 if (type_iface_get_inherit(iface))
487 write_proxy_procformatstring_offsets( type_iface_get_inherit(iface), need_delegation(iface));
488 else
489 return;
491 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
493 const var_t *func = stmt->u.var;
494 int missing = 0;
496 if (is_callas(func->attrs)) continue;
497 if (is_local(func->attrs))
499 const statement_t * callas_source = get_callas_source(iface, func);
500 if (!callas_source)
501 missing = 1;
502 else
503 func = callas_source->u.var;
505 if (skip || missing)
506 print_proxy( "(unsigned short)-1, /* %s::%s */\n", iface->name, get_name(func));
507 else
508 print_proxy( "%u, /* %s::%s */\n", func->procstring_offset, iface->name, get_name(func));
512 static int write_proxy_methods(type_t *iface, int skip)
514 const statement_t *stmt;
515 int i = 0;
517 if (type_iface_get_inherit(iface))
518 i = write_proxy_methods(type_iface_get_inherit(iface),
519 need_delegation(iface));
520 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
521 const var_t *func = stmt->u.var;
522 if (!is_callas(func->attrs)) {
523 if (skip || (is_local(func->attrs) && !get_callas_source(iface, func)))
524 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func));
525 else if (is_interpreted_func( iface, func ) &&
526 get_stub_mode() == MODE_Oif &&
527 !is_local( func->attrs ) &&
528 type_iface_get_inherit(iface))
529 print_proxy( "(void *)-1, /* %s::%s */\n", iface->name, get_name(func));
530 else
531 print_proxy( "%s_%s_Proxy,\n", iface->name, get_name(func));
532 i++;
535 return i;
538 static int write_stub_methods(type_t *iface, int skip)
540 const statement_t *stmt;
541 int i = 0;
543 if (type_iface_get_inherit(iface))
544 i = write_stub_methods(type_iface_get_inherit(iface), need_delegation(iface));
545 else
546 return i; /* skip IUnknown */
548 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
549 const var_t *func = stmt->u.var;
550 if (!is_callas(func->attrs)) {
551 int missing = 0;
552 const char * fname = get_name(func);
553 if(is_local(func->attrs)) {
554 const statement_t * callas_source = get_callas_source(iface, func);
555 if(!callas_source)
556 missing = 1;
557 else
558 fname = get_name(callas_source->u.var);
560 if (i) fprintf(proxy,",\n");
561 if (skip || missing) print_proxy("STUB_FORWARDING_FUNCTION");
562 else if (is_interpreted_func( iface, func ))
563 print_proxy( "(PRPC_STUB_FUNCTION)%s", get_stub_mode() == MODE_Oif ? "NdrStubCall2" : "NdrStubCall" );
564 else print_proxy( "%s_%s_Stub", iface->name, fname);
565 i++;
568 return i;
571 static void write_thunk_methods( type_t *iface, int skip )
573 const statement_t *stmt;
575 if (type_iface_get_inherit( iface ))
576 write_thunk_methods( type_iface_get_inherit(iface), need_delegation(iface) );
577 else
578 return; /* skip IUnknown */
580 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
582 var_t *func = stmt->u.var;
583 const statement_t * callas_source = NULL;
585 if (is_callas(func->attrs)) continue;
586 if (is_local(func->attrs)) callas_source = get_callas_source(iface, func);
588 if (!skip && callas_source && is_interpreted_func( iface, func ))
589 print_proxy( "%s_%s_Thunk,\n", iface->name, get_name(callas_source->u.var) );
590 else
591 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func) );
595 static void write_proxy(type_t *iface, unsigned int *proc_offset)
597 int count;
598 const statement_t *stmt;
599 int first_func = 1;
600 int needs_stub_thunks = 0;
601 int needs_inline_stubs = need_inline_stubs( iface ) || need_delegation( iface );
603 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
604 var_t *func = stmt->u.var;
605 if (first_func) {
606 fprintf(proxy, "/*****************************************************************************\n");
607 fprintf(proxy, " * %s interface\n", iface->name);
608 fprintf(proxy, " */\n");
609 first_func = 0;
611 if (!is_local(func->attrs)) {
612 const var_t *cas = is_callas(func->attrs);
613 const char *cname = cas ? cas->name : NULL;
614 int idx = func->func_idx;
615 if (cname) {
616 const statement_t *stmt2;
617 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface)) {
618 const var_t *m = stmt2->u.var;
619 if (!strcmp(m->name, cname))
621 idx = m->func_idx;
622 break;
626 func->procstring_offset = *proc_offset;
627 gen_proxy(iface, func, idx, *proc_offset);
628 gen_stub(iface, func, cname, *proc_offset);
629 if (cas && is_interpreted_func( iface, func ))
631 needs_stub_thunks = 1;
632 gen_stub_thunk(iface, func, *proc_offset);
634 *proc_offset += get_size_procformatstring_func( iface, func );
638 count = count_methods(iface);
640 print_proxy( "static const unsigned short %s_FormatStringOffsetTable[] =\n", iface->name );
641 print_proxy( "{\n" );
642 indent++;
643 write_proxy_procformatstring_offsets( iface, 0 );
644 indent--;
645 print_proxy( "};\n\n" );
647 /* proxy info */
648 if (get_stub_mode() == MODE_Oif)
650 print_proxy( "static const MIDL_STUBLESS_PROXY_INFO %s_ProxyInfo =\n", iface->name );
651 print_proxy( "{\n" );
652 indent++;
653 print_proxy( "&Object_StubDesc,\n" );
654 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
655 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
656 print_proxy( "0,\n" );
657 print_proxy( "0,\n" );
658 print_proxy( "0\n" );
659 indent--;
660 print_proxy( "};\n\n" );
663 /* proxy vtable */
664 print_proxy( "static %sCINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n",
665 (get_stub_mode() != MODE_Os || need_delegation_indirect(iface)) ? "" : "const ",
666 count, iface->name);
667 print_proxy( "{\n");
668 indent++;
669 print_proxy( "{\n");
670 indent++;
671 if (get_stub_mode() == MODE_Oif) print_proxy( "&%s_ProxyInfo,\n", iface->name );
672 print_proxy( "&IID_%s,\n", iface->name);
673 indent--;
674 print_proxy( "},\n");
675 print_proxy( "{\n");
676 indent++;
677 write_proxy_methods(iface, FALSE);
678 indent--;
679 print_proxy( "}\n");
680 indent--;
681 print_proxy( "};\n\n");
683 /* stub thunk table */
684 if (needs_stub_thunks)
686 print_proxy( "static const STUB_THUNK %s_StubThunkTable[] =\n", iface->name);
687 print_proxy( "{\n");
688 indent++;
689 write_thunk_methods( iface, 0 );
690 indent--;
691 print_proxy( "};\n\n");
694 /* server info */
695 print_proxy( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
696 print_proxy( "{\n" );
697 indent++;
698 print_proxy( "&Object_StubDesc,\n" );
699 print_proxy( "0,\n" );
700 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
701 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
702 if (needs_stub_thunks)
703 print_proxy( "&%s_StubThunkTable[-3],\n", iface->name );
704 else
705 print_proxy( "0,\n" );
706 print_proxy( "0,\n" );
707 print_proxy( "0,\n" );
708 print_proxy( "0\n" );
709 indent--;
710 print_proxy( "};\n\n" );
712 /* stub vtable */
713 if (needs_inline_stubs)
715 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
716 print_proxy( "{\n");
717 indent++;
718 write_stub_methods(iface, FALSE);
719 fprintf(proxy, "\n");
720 indent--;
721 fprintf(proxy, "};\n\n");
723 print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
724 need_delegation_indirect(iface) ? "" : "const ", iface->name);
725 print_proxy( "{\n");
726 indent++;
727 print_proxy( "{\n");
728 indent++;
729 print_proxy( "&IID_%s,\n", iface->name);
730 print_proxy( "&%s_ServerInfo,\n", iface->name );
731 print_proxy( "%d,\n", count);
732 if (needs_inline_stubs) print_proxy( "&%s_table[-3]\n", iface->name );
733 else print_proxy( "0\n" );
734 indent--;
735 print_proxy( "},\n");
736 print_proxy( "{\n");
737 indent++;
738 print_proxy( "%s_%s\n",
739 type_iface_get_async_iface(iface) == iface ? "CStdAsyncStubBuffer" : "CStdStubBuffer",
740 need_delegation_indirect(iface) ? "DELEGATING_METHODS" : "METHODS");
741 indent--;
742 print_proxy( "}\n");
743 indent--;
744 print_proxy( "};\n");
745 print_proxy( "\n");
748 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
750 const statement_t *stmt;
752 if (stmts)
753 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
755 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
757 if (pred(stmt->u.type))
758 return TRUE;
762 return FALSE;
765 int need_proxy(const type_t *iface)
767 if (!is_object( iface )) return 0;
768 if (is_local( iface->attrs )) return 0;
769 if (is_attr( iface->attrs, ATTR_DISPINTERFACE )) return 0;
770 return 1;
773 int need_stub(const type_t *iface)
775 return !is_object(iface) && !is_local(iface->attrs);
778 int need_proxy_file(const statement_list_t *stmts)
780 return does_any_iface(stmts, need_proxy);
783 int need_proxy_delegation(const statement_list_t *stmts)
785 return does_any_iface(stmts, need_delegation);
788 int need_inline_stubs(const type_t *iface)
790 const statement_t *stmt;
792 if (get_stub_mode() == MODE_Os) return 1;
794 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
796 const var_t *func = stmt->u.var;
797 if (is_local( func->attrs )) continue;
798 if (!is_interpreted_func( iface, func )) return 1;
800 return 0;
803 static int need_proxy_and_inline_stubs(const type_t *iface)
805 const statement_t *stmt;
807 if (!need_proxy( iface )) return 0;
808 if (get_stub_mode() == MODE_Os) return 1;
810 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
812 const var_t *func = stmt->u.var;
813 if (is_local( func->attrs )) continue;
814 if (!is_interpreted_func( iface, func )) return 1;
816 return 0;
819 int need_stub_files(const statement_list_t *stmts)
821 return does_any_iface(stmts, need_stub);
824 int need_inline_stubs_file(const statement_list_t *stmts)
826 return does_any_iface(stmts, need_inline_stubs);
829 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
831 const statement_t *stmt;
832 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
834 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
836 type_t *iface = stmt->u.type;
837 if (need_proxy(iface))
839 write_proxy(iface, proc_offset);
840 if (type_iface_get_async_iface(iface))
841 write_proxy(type_iface_get_async_iface(iface), proc_offset);
847 static int cmp_iid( const void *ptr1, const void *ptr2 )
849 const type_t * const *iface1 = ptr1;
850 const type_t * const *iface2 = ptr2;
851 const struct uuid *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
852 const struct uuid *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
853 return memcmp( uuid1, uuid2, sizeof(*uuid1) );
856 static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
858 const statement_t *stmt;
860 if (!stmts) return;
861 LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
863 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
865 type_t *iface = stmt->u.type;
866 if (type_iface_get_inherit(iface) && need_proxy(iface))
868 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
869 (*ifaces)[(*count)++] = iface;
870 if (type_iface_get_async_iface(iface))
872 iface = type_iface_get_async_iface(iface);
873 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
874 (*ifaces)[(*count)++] = iface;
881 static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
883 type_t **ifaces = NULL;
885 *count = 0;
886 build_iface_list( stmts, &ifaces, count );
887 qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
888 return ifaces;
891 static void write_proxy_routines(const statement_list_t *stmts)
893 int expr_eval_routines;
894 unsigned int proc_offset = 0;
895 char *file_id = proxy_token;
896 int i, count, have_baseiid = 0;
897 unsigned int table_version;
898 type_t **interfaces;
899 const type_t * delegate_to;
901 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
902 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ %u\n", get_stub_mode() == MODE_Oif ? 475 : 440);
903 print_proxy( "#endif\n");
904 print_proxy( "\n");
905 if (get_stub_mode() == MODE_Oif) print_proxy( "#define USE_STUBLESS_PROXY\n");
906 print_proxy( "#include \"rpcproxy.h\"\n");
907 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
908 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
909 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
910 print_proxy( "\n");
911 print_proxy( "#include \"%s\"\n", header_name);
912 print_proxy( "\n");
914 if (does_any_iface(stmts, need_proxy_and_inline_stubs))
916 write_exceptions( proxy );
917 print_proxy( "\n");
918 print_proxy( "struct __proxy_frame\n");
919 print_proxy( "{\n");
920 print_proxy( " __DECL_EXCEPTION_FRAME\n");
921 print_proxy( " MIDL_STUB_MESSAGE _StubMsg;\n");
922 print_proxy( " void *This;\n");
923 print_proxy( "};\n");
924 print_proxy( "\n");
925 print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
926 print_proxy( "{\n");
927 print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
928 print_proxy( "}\n");
929 print_proxy( "\n");
932 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
933 write_stubdescproto();
934 write_proxy_stmts(stmts, &proc_offset);
936 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
937 if (expr_eval_routines)
938 write_expr_eval_routine_list(proxy, proxy_token);
939 write_user_quad_list(proxy);
940 write_stubdesc(expr_eval_routines);
942 print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
943 print_proxy( "#error Invalid build platform for this proxy.\n");
944 print_proxy( "#endif\n");
945 print_proxy( "\n");
946 write_procformatstring(proxy, stmts, need_proxy);
947 write_typeformatstring(proxy, stmts, need_proxy);
949 interfaces = sort_interfaces(stmts, &count);
950 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
951 fprintf(proxy, "{\n");
952 for (i = 0; i < count; i++)
953 fprintf(proxy, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
954 fprintf(proxy, " 0\n");
955 fprintf(proxy, "};\n");
956 fprintf(proxy, "\n");
958 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
959 fprintf(proxy, "{\n");
960 for (i = 0; i < count; i++)
961 fprintf(proxy, " &_%sStubVtbl,\n", interfaces[i]->name);
962 fprintf(proxy, " 0\n");
963 fprintf(proxy, "};\n");
964 fprintf(proxy, "\n");
966 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
967 fprintf(proxy, "{\n");
968 for (i = 0; i < count; i++)
969 fprintf(proxy, " \"%s\",\n", interfaces[i]->name);
970 fprintf(proxy, " 0\n");
971 fprintf(proxy, "};\n");
972 fprintf(proxy, "\n");
974 for (i = 0; i < count; i++)
975 if ((have_baseiid = get_delegation_indirect( interfaces[i], NULL ))) break;
977 if (have_baseiid)
979 fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
980 fprintf(proxy, "{\n");
981 for (i = 0; i < count; i++)
983 if (get_delegation_indirect(interfaces[i], &delegate_to))
984 fprintf( proxy, " &IID_%s, /* %s */\n", delegate_to->name, interfaces[i]->name );
985 else
986 fprintf( proxy, " 0,\n" );
988 fprintf(proxy, " 0\n");
989 fprintf(proxy, "};\n");
990 fprintf(proxy, "\n");
993 fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
994 fprintf(proxy, "{\n");
995 fprintf(proxy, " int low = 0, high = %d;\n", count - 1);
996 fprintf(proxy, "\n");
997 fprintf(proxy, " while (low <= high)\n");
998 fprintf(proxy, " {\n");
999 fprintf(proxy, " int pos = (low + high) / 2;\n");
1000 fprintf(proxy, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
1001 fprintf(proxy, " if (!res) { *pIndex = pos; return 1; }\n");
1002 fprintf(proxy, " if (res > 0) low = pos + 1;\n");
1003 fprintf(proxy, " else high = pos - 1;\n");
1004 fprintf(proxy, " }\n");
1005 fprintf(proxy, " return 0;\n");
1006 fprintf(proxy, "}\n");
1007 fprintf(proxy, "\n");
1009 table_version = get_stub_mode() == MODE_Oif ? 2 : 1;
1010 for (i = 0; i < count; i++)
1012 if (type_iface_get_async_iface(interfaces[i]) != interfaces[i]) continue;
1013 if (table_version != 6)
1015 fprintf(proxy, "static const IID *_AsyncInterfaceTable[] =\n");
1016 fprintf(proxy, "{\n");
1017 table_version = 6;
1019 fprintf(proxy, " &IID_%s,\n", interfaces[i]->name);
1020 fprintf(proxy, " (IID*)(LONG_PTR)-1,\n");
1022 if (table_version == 6)
1024 fprintf(proxy, " 0\n");
1025 fprintf(proxy, "};\n");
1026 fprintf(proxy, "\n");
1029 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo DECLSPEC_HIDDEN =\n", file_id);
1030 fprintf(proxy, "{\n");
1031 fprintf(proxy, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
1032 fprintf(proxy, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
1033 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
1034 if (have_baseiid) fprintf(proxy, " _%s_BaseIIDList,\n", file_id);
1035 else fprintf(proxy, " 0,\n");
1036 fprintf(proxy, " _%s_IID_Lookup,\n", file_id);
1037 fprintf(proxy, " %d,\n", count);
1038 fprintf(proxy, " %u,\n", table_version);
1039 fprintf(proxy, " %s,\n", table_version == 6 ? "_AsyncInterfaceTable" : "0");
1040 fprintf(proxy, " 0,\n");
1041 fprintf(proxy, " 0,\n");
1042 fprintf(proxy, " 0\n");
1043 fprintf(proxy, "};\n");
1046 void write_proxies(const statement_list_t *stmts)
1048 if (!do_proxies) return;
1049 if (do_everything && !need_proxy_file(stmts)) return;
1051 init_proxy(stmts);
1052 if(!proxy) return;
1054 write_proxy_routines( stmts );
1055 fclose(proxy);