msvcp140: Import __ExceptionPtrCopy implementation.
[wine.git] / tools / widl / proxy.c
bloba80aa5d77fe05b1e2374af352b3c2fdf523c0576
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( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
302 indent--;
304 print_proxy( "}\n" );
305 print_proxy( "RpcEndExcept\n" );
307 if (has_ret) {
308 print_proxy( "return _RetVal;\n" );
310 indent--;
311 print_proxy( "}\n");
312 print_proxy( "\n");
315 static void gen_stub(type_t *iface, const var_t *func, const char *cas,
316 unsigned int proc_offset)
318 const var_t *arg;
319 int has_ret = !is_void(type_function_get_rettype(func->declspec.type));
320 int has_full_pointer = is_full_pointer_function(func);
322 if (is_interpreted_func( iface, func )) return;
324 indent = 0;
325 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(func));
326 indent++;
327 print_proxy( "__DECL_EXCEPTION_FRAME\n" );
328 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
329 print_proxy( "%s * _This;\n", iface->name );
330 declare_stub_args( proxy, indent, func );
331 indent--;
332 print_proxy( "};\n\n" );
334 print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(func) );
335 print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(func) );
336 indent++;
337 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
338 if (has_full_pointer)
339 write_full_pointer_free(proxy, indent, func);
340 indent--;
341 print_proxy( "}\n\n" );
343 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
344 indent++;
345 print_proxy( "IRpcStubBuffer* This,\n");
346 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
347 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
348 print_proxy( "DWORD* _pdwStubPhase)\n");
349 indent--;
350 print_proxy( "{\n");
351 indent++;
352 print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
353 iface->name, get_name(func) );
355 print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
357 /* FIXME: trace */
359 print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
360 fprintf(proxy, "\n");
361 print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(func) );
363 write_parameters_init(proxy, indent, func, "__frame->");
365 print_proxy("RpcTryFinally\n");
366 print_proxy("{\n");
367 indent++;
368 if (has_full_pointer)
369 write_full_pointer_init(proxy, indent, func, TRUE);
370 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
371 indent++;
372 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
373 indent--;
374 fprintf(proxy, "\n");
376 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
377 fprintf(proxy, "\n");
379 assign_stub_out_args( proxy, indent, func, "__frame->" );
381 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
382 fprintf(proxy, "\n");
383 print_proxy( "%s", has_ret ? "__frame->_RetVal = " : "" );
384 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
385 else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(func));
386 fprintf(proxy, "(__frame->_This");
388 if (type_function_get_args(func->declspec.type))
390 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
391 fprintf(proxy, ", %s__frame->%s", is_array(arg->declspec.type) && !type_array_is_decl_as_ptr(arg->declspec.type) ? "*" :"" , arg->name);
393 fprintf(proxy, ");\n");
394 fprintf(proxy, "\n");
395 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
396 fprintf(proxy, "\n");
398 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
400 if (!is_void(type_function_get_rettype(func->declspec.type)))
401 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
403 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
405 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
406 fprintf(proxy, "\n");
408 /* marshall the return value */
409 if (!is_void(type_function_get_rettype(func->declspec.type)))
410 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
412 indent--;
413 print_proxy("}\n");
414 print_proxy("RpcFinally\n");
415 print_proxy("{\n");
416 indent++;
417 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(func) );
418 indent--;
419 print_proxy("}\n");
420 print_proxy("RpcEndFinally\n");
422 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
423 indent--;
425 print_proxy("}\n");
426 print_proxy("\n");
429 static void gen_stub_thunk( type_t *iface, const var_t *func, unsigned int proc_offset )
431 int has_ret = !is_void( type_function_get_rettype( func->declspec.type ));
432 const var_t *arg, *callas = is_callas( func->attrs );
433 const var_list_t *args = type_function_get_args( func->declspec.type );
435 indent = 0;
436 print_proxy( "void __RPC_API %s_%s_Thunk( PMIDL_STUB_MESSAGE pStubMsg )\n",
437 iface->name, get_name(func) );
438 print_proxy( "{\n");
439 indent++;
440 write_func_param_struct( proxy, iface, func->declspec.type,
441 "*pParamStruct = (struct _PARAM_STRUCT *)pStubMsg->StackTop", has_ret );
442 print_proxy( "%s%s_%s_Stub( pParamStruct->This",
443 has_ret ? "pParamStruct->_RetVal = " : "", iface->name, callas->name );
444 indent++;
445 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
447 fprintf( proxy, ",\n%*spParamStruct->%s", 4 * indent, "", arg->name );
449 fprintf( proxy, " );\n" );
450 indent--;
451 indent--;
452 print_proxy( "}\n\n");
455 int count_methods(const type_t *iface)
457 const statement_t *stmt;
458 int count = 0;
460 if (type_iface_get_inherit(iface))
461 count = count_methods(type_iface_get_inherit(iface));
463 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
464 const var_t *func = stmt->u.var;
465 if (!is_callas(func->attrs)) count++;
467 return count;
470 static const statement_t * get_callas_source(const type_t * iface, const var_t * def)
472 const statement_t * source;
473 STATEMENTS_FOR_EACH_FUNC( source, type_iface_get_stmts(iface)) {
474 const var_t * cas = is_callas(source->u.var->attrs );
475 if (cas && !strcmp(def->name, cas->name))
476 return source;
478 return NULL;
481 static void write_proxy_procformatstring_offsets( const type_t *iface, int skip )
483 const statement_t *stmt;
485 if (type_iface_get_inherit(iface))
486 write_proxy_procformatstring_offsets( type_iface_get_inherit(iface), need_delegation(iface));
487 else
488 return;
490 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
492 const var_t *func = stmt->u.var;
493 int missing = 0;
495 if (is_callas(func->attrs)) continue;
496 if (is_local(func->attrs))
498 const statement_t * callas_source = get_callas_source(iface, func);
499 if (!callas_source)
500 missing = 1;
501 else
502 func = callas_source->u.var;
504 if (skip || missing)
505 print_proxy( "(unsigned short)-1, /* %s::%s */\n", iface->name, get_name(func));
506 else
507 print_proxy( "%u, /* %s::%s */\n", func->procstring_offset, iface->name, get_name(func));
511 static int write_proxy_methods(type_t *iface, int skip)
513 const statement_t *stmt;
514 int i = 0;
516 if (type_iface_get_inherit(iface))
517 i = write_proxy_methods(type_iface_get_inherit(iface),
518 need_delegation(iface));
519 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
520 const var_t *func = stmt->u.var;
521 if (!is_callas(func->attrs)) {
522 if (skip || (is_local(func->attrs) && !get_callas_source(iface, func)))
523 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func));
524 else if (is_interpreted_func( iface, func ) &&
525 get_stub_mode() == MODE_Oif &&
526 !is_local( func->attrs ) &&
527 type_iface_get_inherit(iface))
528 print_proxy( "(void *)-1, /* %s::%s */\n", iface->name, get_name(func));
529 else
530 print_proxy( "%s_%s_Proxy,\n", iface->name, get_name(func));
531 i++;
534 return i;
537 static int write_stub_methods(type_t *iface, int skip)
539 const statement_t *stmt;
540 int i = 0;
542 if (type_iface_get_inherit(iface))
543 i = write_stub_methods(type_iface_get_inherit(iface), need_delegation(iface));
544 else
545 return i; /* skip IUnknown */
547 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
548 const var_t *func = stmt->u.var;
549 if (!is_callas(func->attrs)) {
550 int missing = 0;
551 const char * fname = get_name(func);
552 if(is_local(func->attrs)) {
553 const statement_t * callas_source = get_callas_source(iface, func);
554 if(!callas_source)
555 missing = 1;
556 else
557 fname = get_name(callas_source->u.var);
559 if (i) fprintf(proxy,",\n");
560 if (skip || missing) print_proxy("STUB_FORWARDING_FUNCTION");
561 else if (is_interpreted_func( iface, func ))
562 print_proxy( "(PRPC_STUB_FUNCTION)%s", get_stub_mode() == MODE_Oif ? "NdrStubCall2" : "NdrStubCall" );
563 else print_proxy( "%s_%s_Stub", iface->name, fname);
564 i++;
567 return i;
570 static void write_thunk_methods( type_t *iface, int skip )
572 const statement_t *stmt;
574 if (type_iface_get_inherit( iface ))
575 write_thunk_methods( type_iface_get_inherit(iface), need_delegation(iface) );
576 else
577 return; /* skip IUnknown */
579 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
581 var_t *func = stmt->u.var;
582 const statement_t * callas_source = NULL;
584 if (is_callas(func->attrs)) continue;
585 if (is_local(func->attrs)) callas_source = get_callas_source(iface, func);
587 if (!skip && callas_source && is_interpreted_func( iface, func ))
588 print_proxy( "%s_%s_Thunk,\n", iface->name, get_name(callas_source->u.var) );
589 else
590 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func) );
594 static void write_proxy(type_t *iface, unsigned int *proc_offset)
596 int count;
597 const statement_t *stmt;
598 int first_func = 1;
599 int needs_stub_thunks = 0;
600 int needs_inline_stubs = need_inline_stubs( iface ) || need_delegation( iface );
602 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
603 var_t *func = stmt->u.var;
604 if (first_func) {
605 fprintf(proxy, "/*****************************************************************************\n");
606 fprintf(proxy, " * %s interface\n", iface->name);
607 fprintf(proxy, " */\n");
608 first_func = 0;
610 if (!is_local(func->attrs)) {
611 const var_t *cas = is_callas(func->attrs);
612 const char *cname = cas ? cas->name : NULL;
613 int idx = func->func_idx;
614 if (cname) {
615 const statement_t *stmt2;
616 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface)) {
617 const var_t *m = stmt2->u.var;
618 if (!strcmp(m->name, cname))
620 idx = m->func_idx;
621 break;
625 func->procstring_offset = *proc_offset;
626 gen_proxy(iface, func, idx, *proc_offset);
627 gen_stub(iface, func, cname, *proc_offset);
628 if (cas && is_interpreted_func( iface, func ))
630 needs_stub_thunks = 1;
631 gen_stub_thunk(iface, func, *proc_offset);
633 *proc_offset += get_size_procformatstring_func( iface, func );
637 count = count_methods(iface);
639 print_proxy( "static const unsigned short %s_FormatStringOffsetTable[] =\n", iface->name );
640 print_proxy( "{\n" );
641 indent++;
642 write_proxy_procformatstring_offsets( iface, 0 );
643 indent--;
644 print_proxy( "};\n\n" );
646 /* proxy info */
647 if (get_stub_mode() == MODE_Oif)
649 print_proxy( "static const MIDL_STUBLESS_PROXY_INFO %s_ProxyInfo =\n", iface->name );
650 print_proxy( "{\n" );
651 indent++;
652 print_proxy( "&Object_StubDesc,\n" );
653 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
654 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
655 print_proxy( "0,\n" );
656 print_proxy( "0,\n" );
657 print_proxy( "0\n" );
658 indent--;
659 print_proxy( "};\n\n" );
662 /* proxy vtable */
663 print_proxy( "static %sCINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n",
664 (get_stub_mode() != MODE_Os || need_delegation_indirect(iface)) ? "" : "const ",
665 count, iface->name);
666 print_proxy( "{\n");
667 indent++;
668 print_proxy( "{\n");
669 indent++;
670 if (get_stub_mode() == MODE_Oif) print_proxy( "&%s_ProxyInfo,\n", iface->name );
671 print_proxy( "&IID_%s,\n", iface->name);
672 indent--;
673 print_proxy( "},\n");
674 print_proxy( "{\n");
675 indent++;
676 write_proxy_methods(iface, FALSE);
677 indent--;
678 print_proxy( "}\n");
679 indent--;
680 print_proxy( "};\n\n");
682 /* stub thunk table */
683 if (needs_stub_thunks)
685 print_proxy( "static const STUB_THUNK %s_StubThunkTable[] =\n", iface->name);
686 print_proxy( "{\n");
687 indent++;
688 write_thunk_methods( iface, 0 );
689 indent--;
690 print_proxy( "};\n\n");
693 /* server info */
694 print_proxy( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
695 print_proxy( "{\n" );
696 indent++;
697 print_proxy( "&Object_StubDesc,\n" );
698 print_proxy( "0,\n" );
699 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
700 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
701 if (needs_stub_thunks)
702 print_proxy( "&%s_StubThunkTable[-3],\n", iface->name );
703 else
704 print_proxy( "0,\n" );
705 print_proxy( "0,\n" );
706 print_proxy( "0,\n" );
707 print_proxy( "0\n" );
708 indent--;
709 print_proxy( "};\n\n" );
711 /* stub vtable */
712 if (needs_inline_stubs)
714 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
715 print_proxy( "{\n");
716 indent++;
717 write_stub_methods(iface, FALSE);
718 fprintf(proxy, "\n");
719 indent--;
720 fprintf(proxy, "};\n\n");
722 print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
723 need_delegation_indirect(iface) ? "" : "const ", iface->name);
724 print_proxy( "{\n");
725 indent++;
726 print_proxy( "{\n");
727 indent++;
728 print_proxy( "&IID_%s,\n", iface->name);
729 print_proxy( "&%s_ServerInfo,\n", iface->name );
730 print_proxy( "%d,\n", count);
731 if (needs_inline_stubs) print_proxy( "&%s_table[-3]\n", iface->name );
732 else print_proxy( "0\n" );
733 indent--;
734 print_proxy( "},\n");
735 print_proxy( "{\n");
736 indent++;
737 print_proxy( "%s_%s\n",
738 type_iface_get_async_iface(iface) == iface ? "CStdAsyncStubBuffer" : "CStdStubBuffer",
739 need_delegation_indirect(iface) ? "DELEGATING_METHODS" : "METHODS");
740 indent--;
741 print_proxy( "}\n");
742 indent--;
743 print_proxy( "};\n");
744 print_proxy( "\n");
747 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
749 const statement_t *stmt;
751 if (stmts)
752 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
754 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
756 if (pred(stmt->u.type))
757 return TRUE;
761 return FALSE;
764 int need_proxy(const type_t *iface)
766 if (!is_object( iface )) return 0;
767 if (is_local( iface->attrs )) return 0;
768 if (is_attr( iface->attrs, ATTR_DISPINTERFACE )) return 0;
769 return 1;
772 int need_stub(const type_t *iface)
774 return !is_object(iface) && !is_local(iface->attrs);
777 int need_proxy_file(const statement_list_t *stmts)
779 return does_any_iface(stmts, need_proxy);
782 int need_proxy_delegation(const statement_list_t *stmts)
784 return does_any_iface(stmts, need_delegation);
787 int need_inline_stubs(const type_t *iface)
789 const statement_t *stmt;
791 if (get_stub_mode() == MODE_Os) return 1;
793 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
795 const var_t *func = stmt->u.var;
796 if (is_local( func->attrs )) continue;
797 if (!is_interpreted_func( iface, func )) return 1;
799 return 0;
802 static int need_proxy_and_inline_stubs(const type_t *iface)
804 const statement_t *stmt;
806 if (!need_proxy( iface )) return 0;
807 if (get_stub_mode() == MODE_Os) return 1;
809 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
811 const var_t *func = stmt->u.var;
812 if (is_local( func->attrs )) continue;
813 if (!is_interpreted_func( iface, func )) return 1;
815 return 0;
818 int need_stub_files(const statement_list_t *stmts)
820 return does_any_iface(stmts, need_stub);
823 int need_inline_stubs_file(const statement_list_t *stmts)
825 return does_any_iface(stmts, need_inline_stubs);
828 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
830 const statement_t *stmt;
831 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
833 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
835 type_t *iface = stmt->u.type;
836 if (need_proxy(iface))
838 write_proxy(iface, proc_offset);
839 if (type_iface_get_async_iface(iface))
840 write_proxy(type_iface_get_async_iface(iface), proc_offset);
846 static int cmp_iid( const void *ptr1, const void *ptr2 )
848 const type_t * const *iface1 = ptr1;
849 const type_t * const *iface2 = ptr2;
850 const struct uuid *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
851 const struct uuid *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
852 return memcmp( uuid1, uuid2, sizeof(*uuid1) );
855 static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
857 const statement_t *stmt;
859 if (!stmts) return;
860 LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
862 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
864 type_t *iface = stmt->u.type;
865 if (type_iface_get_inherit(iface) && need_proxy(iface))
867 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
868 (*ifaces)[(*count)++] = iface;
869 if (type_iface_get_async_iface(iface))
871 iface = type_iface_get_async_iface(iface);
872 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
873 (*ifaces)[(*count)++] = iface;
880 static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
882 type_t **ifaces = NULL;
884 *count = 0;
885 build_iface_list( stmts, &ifaces, count );
886 qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
887 return ifaces;
890 static void write_proxy_routines(const statement_list_t *stmts)
892 int expr_eval_routines;
893 unsigned int proc_offset = 0;
894 char *file_id = proxy_token;
895 int i, count, have_baseiid = 0;
896 unsigned int table_version;
897 type_t **interfaces;
898 const type_t * delegate_to;
900 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
901 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ %u\n", get_stub_mode() == MODE_Oif ? 475 : 440);
902 print_proxy( "#endif\n");
903 print_proxy( "\n");
904 if (get_stub_mode() == MODE_Oif) print_proxy( "#define USE_STUBLESS_PROXY\n");
905 print_proxy( "#include \"rpcproxy.h\"\n");
906 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
907 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
908 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
909 print_proxy( "\n");
910 print_proxy( "#include \"%s\"\n", header_name);
911 print_proxy( "\n");
913 if (does_any_iface(stmts, need_proxy_and_inline_stubs))
915 write_exceptions( proxy );
916 print_proxy( "\n");
917 print_proxy( "struct __proxy_frame\n");
918 print_proxy( "{\n");
919 print_proxy( " __DECL_EXCEPTION_FRAME\n");
920 print_proxy( " MIDL_STUB_MESSAGE _StubMsg;\n");
921 print_proxy( " void *This;\n");
922 print_proxy( "};\n");
923 print_proxy( "\n");
924 print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
925 print_proxy( "{\n");
926 print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
927 print_proxy( "}\n");
928 print_proxy( "\n");
931 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
932 write_stubdescproto();
933 write_proxy_stmts(stmts, &proc_offset);
935 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
936 if (expr_eval_routines)
937 write_expr_eval_routine_list(proxy, proxy_token);
938 write_user_quad_list(proxy);
939 write_stubdesc(expr_eval_routines);
941 print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
942 print_proxy( "#error Invalid build platform for this proxy.\n");
943 print_proxy( "#endif\n");
944 print_proxy( "\n");
945 write_procformatstring(proxy, stmts, need_proxy);
946 write_typeformatstring(proxy, stmts, need_proxy);
948 interfaces = sort_interfaces(stmts, &count);
949 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
950 fprintf(proxy, "{\n");
951 for (i = 0; i < count; i++)
952 fprintf(proxy, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
953 fprintf(proxy, " 0\n");
954 fprintf(proxy, "};\n");
955 fprintf(proxy, "\n");
957 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
958 fprintf(proxy, "{\n");
959 for (i = 0; i < count; i++)
960 fprintf(proxy, " &_%sStubVtbl,\n", interfaces[i]->name);
961 fprintf(proxy, " 0\n");
962 fprintf(proxy, "};\n");
963 fprintf(proxy, "\n");
965 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
966 fprintf(proxy, "{\n");
967 for (i = 0; i < count; i++)
968 fprintf(proxy, " \"%s\",\n", interfaces[i]->name);
969 fprintf(proxy, " 0\n");
970 fprintf(proxy, "};\n");
971 fprintf(proxy, "\n");
973 for (i = 0; i < count; i++)
974 if ((have_baseiid = get_delegation_indirect( interfaces[i], NULL ))) break;
976 if (have_baseiid)
978 fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
979 fprintf(proxy, "{\n");
980 for (i = 0; i < count; i++)
982 if (get_delegation_indirect(interfaces[i], &delegate_to))
983 fprintf( proxy, " &IID_%s, /* %s */\n", delegate_to->name, interfaces[i]->name );
984 else
985 fprintf( proxy, " 0,\n" );
987 fprintf(proxy, " 0\n");
988 fprintf(proxy, "};\n");
989 fprintf(proxy, "\n");
992 fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
993 fprintf(proxy, "{\n");
994 fprintf(proxy, " int low = 0, high = %d;\n", count - 1);
995 fprintf(proxy, "\n");
996 fprintf(proxy, " while (low <= high)\n");
997 fprintf(proxy, " {\n");
998 fprintf(proxy, " int pos = (low + high) / 2;\n");
999 fprintf(proxy, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
1000 fprintf(proxy, " if (!res) { *pIndex = pos; return 1; }\n");
1001 fprintf(proxy, " if (res > 0) low = pos + 1;\n");
1002 fprintf(proxy, " else high = pos - 1;\n");
1003 fprintf(proxy, " }\n");
1004 fprintf(proxy, " return 0;\n");
1005 fprintf(proxy, "}\n");
1006 fprintf(proxy, "\n");
1008 table_version = get_stub_mode() == MODE_Oif ? 2 : 1;
1009 for (i = 0; i < count; i++)
1011 if (type_iface_get_async_iface(interfaces[i]) != interfaces[i]) continue;
1012 if (table_version != 6)
1014 fprintf(proxy, "static const IID *_AsyncInterfaceTable[] =\n");
1015 fprintf(proxy, "{\n");
1016 table_version = 6;
1018 fprintf(proxy, " &IID_%s,\n", interfaces[i]->name);
1019 fprintf(proxy, " (IID*)(LONG_PTR)-1,\n");
1021 if (table_version == 6)
1023 fprintf(proxy, " 0\n");
1024 fprintf(proxy, "};\n");
1025 fprintf(proxy, "\n");
1028 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo DECLSPEC_HIDDEN =\n", file_id);
1029 fprintf(proxy, "{\n");
1030 fprintf(proxy, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
1031 fprintf(proxy, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
1032 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
1033 if (have_baseiid) fprintf(proxy, " _%s_BaseIIDList,\n", file_id);
1034 else fprintf(proxy, " 0,\n");
1035 fprintf(proxy, " _%s_IID_Lookup,\n", file_id);
1036 fprintf(proxy, " %d,\n", count);
1037 fprintf(proxy, " %u,\n", table_version);
1038 fprintf(proxy, " %s,\n", table_version == 6 ? "_AsyncInterfaceTable" : "0");
1039 fprintf(proxy, " 0,\n");
1040 fprintf(proxy, " 0,\n");
1041 fprintf(proxy, " 0\n");
1042 fprintf(proxy, "};\n");
1045 void write_proxies(const statement_list_t *stmts)
1047 if (!do_proxies) return;
1048 if (do_everything && !need_proxy_file(stmts)) return;
1050 init_proxy(stmts);
1051 if(!proxy) return;
1053 write_proxy_routines( stmts );
1054 fclose(proxy);