api-ms-win-core-sysinfo-l1-1-0: Add stub dll.
[wine.git] / tools / widl / proxy.c
blobf4fe6f5ee3bb0e5d0cb26aefbd8f270a5949fb91
1 /*
2 * IDL Compiler
4 * Copyright 2002 Ove Kaaven
5 * Copyright 2004 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <ctype.h>
33 #include "widl.h"
34 #include "utils.h"
35 #include "parser.h"
36 #include "header.h"
37 #include "typegen.h"
38 #include "expr.h"
40 static FILE* proxy;
41 static int indent = 0;
43 static void print_proxy( const char *format, ... ) __attribute__((format (printf, 1, 2)));
44 static void print_proxy( const char *format, ... )
46 va_list va;
47 va_start( va, format );
48 print( proxy, indent, format, va );
49 va_end( va );
52 static void write_stubdescproto(void)
54 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
55 print_proxy( "\n");
58 static void write_stubdesc(int expr_eval_routines)
60 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc =\n{\n");
61 indent++;
62 print_proxy( "0,\n");
63 print_proxy( "NdrOleAllocate,\n");
64 print_proxy( "NdrOleFree,\n");
65 print_proxy( "{0}, 0, 0, %s, 0,\n", expr_eval_routines ? "ExprEvalRoutines" : "0");
66 print_proxy( "__MIDL_TypeFormatString.Format,\n");
67 print_proxy( "1, /* -error bounds_check flag */\n");
68 print_proxy( "0x%x, /* Ndr library version */\n", get_stub_mode() == MODE_Oif ? 0x50002 : 0x10001);
69 print_proxy( "0,\n");
70 print_proxy( "0x50100a4, /* MIDL Version 5.1.164 */\n");
71 print_proxy( "0,\n");
72 print_proxy("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
73 print_proxy( "0, /* notify & notify_flag routine table */\n");
74 print_proxy( "1, /* Flags */\n");
75 print_proxy( "0, /* Reserved3 */\n");
76 print_proxy( "0, /* Reserved4 */\n");
77 print_proxy( "0 /* Reserved5 */\n");
78 indent--;
79 print_proxy( "};\n");
80 print_proxy( "\n");
83 static void init_proxy(const statement_list_t *stmts)
85 if (proxy) return;
86 if(!(proxy = fopen(proxy_name, "w")))
87 error("Could not open %s for output\n", proxy_name);
88 print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
89 print_proxy( "\n");
90 print_proxy( "#define __midl_proxy\n");
91 print_proxy( "#include \"objbase.h\"\n");
92 print_proxy( "\n");
93 print_proxy( "#ifndef DECLSPEC_HIDDEN\n");
94 print_proxy( "#define DECLSPEC_HIDDEN\n");
95 print_proxy( "#endif\n");
96 print_proxy( "\n");
99 static void clear_output_vars( const var_list_t *args )
101 const var_t *arg;
103 if (!args) return;
104 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
106 if (is_attr(arg->attrs, ATTR_IN)) continue;
107 if (!is_attr(arg->attrs, ATTR_OUT)) continue;
108 if (is_ptr(arg->type))
110 if (type_get_type(type_pointer_get_ref(arg->type)) == TYPE_BASIC) continue;
111 if (type_get_type(type_pointer_get_ref(arg->type)) == TYPE_ENUM) continue;
113 print_proxy( "if (%s) MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name, arg->name );
117 static int need_delegation(const type_t *iface)
119 const type_t *parent = type_iface_get_inherit( iface );
120 return parent && type_iface_get_inherit(parent) && (parent->ignore || is_local( parent->attrs ));
123 static int get_delegation_indirect(const type_t *iface, const type_t ** delegate_to)
125 const type_t * cur_iface;
126 for (cur_iface = iface; cur_iface != NULL; cur_iface = type_iface_get_inherit(cur_iface))
127 if (need_delegation(cur_iface))
129 if(delegate_to)
130 *delegate_to = type_iface_get_inherit(cur_iface);
131 return 1;
133 return 0;
136 static int need_delegation_indirect(const type_t *iface)
138 return get_delegation_indirect(iface, NULL);
141 static void free_variable( const var_t *arg, const char *local_var_prefix )
143 unsigned int type_offset = arg->typestring_offset;
144 type_t *type = arg->type;
146 write_parameter_conf_or_var_exprs(proxy, indent, local_var_prefix, PHASE_FREE, arg, FALSE);
148 switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
150 case TGT_ENUM:
151 case TGT_BASIC:
152 break;
154 case TGT_STRUCT:
155 if (get_struct_fc(type) != RPC_FC_STRUCT)
156 print_proxy("/* FIXME: %s code for %s struct type 0x%x missing */\n", __FUNCTION__, arg->name, get_struct_fc(type) );
157 break;
159 case TGT_IFACE_POINTER:
160 case TGT_POINTER:
161 case TGT_ARRAY:
162 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
163 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
164 fprintf(proxy, "(void *)%s );\n", arg->name );
165 break;
167 default:
168 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
172 static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
174 const var_t *arg;
176 if (!args) return;
177 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
178 if (is_attr(arg->attrs, ATTR_OUT))
180 free_variable( arg, local_var_prefix );
181 fprintf(proxy, "\n");
185 static void gen_proxy(type_t *iface, const var_t *func, int idx,
186 unsigned int proc_offset)
188 var_t *retval = type_function_get_retval(func->type);
189 int has_ret = !is_void(retval->type);
190 int has_full_pointer = is_full_pointer_function(func);
191 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
192 const var_list_t *args = type_get_function_args(func->type);
193 if (!callconv) callconv = "STDMETHODCALLTYPE";
195 indent = 0;
196 if (is_interpreted_func( iface, func ))
198 if (get_stub_mode() == MODE_Oif && !is_callas( func->attrs )) return;
199 write_type_decl_left(proxy, retval->type);
200 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
201 write_args(proxy, args, iface->name, 1, TRUE);
202 print_proxy( ")\n");
203 write_client_call_routine( proxy, iface, func, "Object", proc_offset );
204 return;
206 print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
207 iface->name, get_name(func) );
208 print_proxy( "{\n");
209 indent++;
210 if (has_full_pointer) write_full_pointer_free(proxy, indent, func);
211 print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
212 indent--;
213 print_proxy( "}\n");
214 print_proxy( "\n");
216 write_type_decl_left(proxy, retval->type);
217 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
218 write_args(proxy, args, iface->name, 1, TRUE);
219 print_proxy( ")\n");
220 print_proxy( "{\n");
221 indent ++;
222 print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
223 /* local variables */
224 if (has_ret) {
225 print_proxy( "%s", "" );
226 write_type_decl(proxy, retval->type, retval->name);
227 fprintf( proxy, ";\n" );
229 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
230 if (has_ret) {
231 if (decl_indirect(retval->type))
232 print_proxy("void *_p_%s = &%s;\n", retval->name, retval->name);
234 print_proxy( "\n");
236 print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(func) );
237 print_proxy( "__frame->This = This;\n" );
239 if (has_full_pointer)
240 write_full_pointer_init(proxy, indent, func, FALSE);
242 /* FIXME: trace */
243 clear_output_vars( type_get_function_args(func->type) );
245 print_proxy( "RpcTryExcept\n" );
246 print_proxy( "{\n" );
247 indent++;
248 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx);
249 write_pointer_checks( proxy, indent, func );
251 print_proxy( "RpcTryFinally\n" );
252 print_proxy( "{\n" );
253 indent++;
255 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
257 print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
259 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_MARSHAL);
261 print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
262 fprintf(proxy, "\n");
263 print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
264 print_proxy( "__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
266 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
267 indent++;
268 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
269 indent--;
270 fprintf(proxy, "\n");
272 write_remoting_arguments(proxy, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
274 if (has_ret)
276 if (decl_indirect(retval->type))
277 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", retval->name, retval->name);
278 else if (is_ptr(retval->type) || is_array(retval->type))
279 print_proxy("%s = 0;\n", retval->name);
280 write_remoting_arguments(proxy, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
283 indent--;
284 print_proxy( "}\n");
285 print_proxy( "RpcFinally\n" );
286 print_proxy( "{\n" );
287 indent++;
288 print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(func) );
289 indent--;
290 print_proxy( "}\n");
291 print_proxy( "RpcEndFinally\n" );
292 indent--;
293 print_proxy( "}\n" );
294 print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
295 print_proxy( "{\n" );
296 if (has_ret) {
297 indent++;
298 proxy_free_variables( type_get_function_args(func->type), "" );
299 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
300 indent--;
302 print_proxy( "}\n" );
303 print_proxy( "RpcEndExcept\n" );
305 if (has_ret) {
306 print_proxy( "return _RetVal;\n" );
308 indent--;
309 print_proxy( "}\n");
310 print_proxy( "\n");
313 static void gen_stub(type_t *iface, const var_t *func, const char *cas,
314 unsigned int proc_offset)
316 const var_t *arg;
317 int has_ret = !is_void(type_function_get_rettype(func->type));
318 int has_full_pointer = is_full_pointer_function(func);
320 if (is_interpreted_func( iface, func )) return;
322 indent = 0;
323 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(func));
324 indent++;
325 print_proxy( "__DECL_EXCEPTION_FRAME\n" );
326 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
327 print_proxy( "%s * _This;\n", iface->name );
328 declare_stub_args( proxy, indent, func );
329 indent--;
330 print_proxy( "};\n\n" );
332 print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(func) );
333 print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(func) );
334 indent++;
335 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
336 if (has_full_pointer)
337 write_full_pointer_free(proxy, indent, func);
338 indent--;
339 print_proxy( "}\n\n" );
341 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
342 indent++;
343 print_proxy( "IRpcStubBuffer* This,\n");
344 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
345 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
346 print_proxy( "DWORD* _pdwStubPhase)\n");
347 indent--;
348 print_proxy( "{\n");
349 indent++;
350 print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
351 iface->name, get_name(func) );
353 print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
355 /* FIXME: trace */
357 print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
358 fprintf(proxy, "\n");
359 print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(func) );
361 write_parameters_init(proxy, indent, func, "__frame->");
363 print_proxy("RpcTryFinally\n");
364 print_proxy("{\n");
365 indent++;
366 if (has_full_pointer)
367 write_full_pointer_init(proxy, indent, func, TRUE);
368 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
369 indent++;
370 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
371 indent--;
372 fprintf(proxy, "\n");
374 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
375 fprintf(proxy, "\n");
377 assign_stub_out_args( proxy, indent, func, "__frame->" );
379 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
380 fprintf(proxy, "\n");
381 print_proxy( "%s", has_ret ? "__frame->_RetVal = " : "" );
382 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
383 else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(func));
384 fprintf(proxy, "(__frame->_This");
386 if (type_get_function_args(func->type))
388 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
389 fprintf(proxy, ", %s__frame->%s", is_array(arg->type) && !type_array_is_decl_as_ptr(arg->type) ? "*" :"" , arg->name);
391 fprintf(proxy, ");\n");
392 fprintf(proxy, "\n");
393 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
394 fprintf(proxy, "\n");
396 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
398 if (!is_void(type_function_get_rettype(func->type)))
399 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
401 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
403 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
404 fprintf(proxy, "\n");
406 /* marshall the return value */
407 if (!is_void(type_function_get_rettype(func->type)))
408 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
410 indent--;
411 print_proxy("}\n");
412 print_proxy("RpcFinally\n");
413 print_proxy("{\n");
414 indent++;
415 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(func) );
416 indent--;
417 print_proxy("}\n");
418 print_proxy("RpcEndFinally\n");
420 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
421 indent--;
423 print_proxy("}\n");
424 print_proxy("\n");
427 static void gen_stub_thunk( type_t *iface, const var_t *func, unsigned int proc_offset )
429 int has_ret = !is_void( type_function_get_rettype( func->type ));
430 const var_t *arg, *callas = is_callas( func->attrs );
431 const var_list_t *args = type_get_function_args( func->type );
433 indent = 0;
434 print_proxy( "void __RPC_API %s_%s_Thunk( PMIDL_STUB_MESSAGE pStubMsg )\n",
435 iface->name, get_name(func) );
436 print_proxy( "{\n");
437 indent++;
438 write_func_param_struct( proxy, iface, func->type,
439 "*pParamStruct = (struct _PARAM_STRUCT *)pStubMsg->StackTop", has_ret );
440 print_proxy( "%s%s_%s_Stub( pParamStruct->This",
441 has_ret ? "pParamStruct->_RetVal = " : "", iface->name, callas->name );
442 indent++;
443 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
445 fprintf( proxy, ",\n%*spParamStruct->%s", 4 * indent, "", arg->name );
447 fprintf( proxy, " );\n" );
448 indent--;
449 indent--;
450 print_proxy( "}\n\n");
453 int count_methods(const type_t *iface)
455 const statement_t *stmt;
456 int count = 0;
458 if (type_iface_get_inherit(iface))
459 count = count_methods(type_iface_get_inherit(iface));
461 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
462 const var_t *func = stmt->u.var;
463 if (!is_callas(func->attrs)) count++;
465 return count;
468 static const statement_t * get_callas_source(const type_t * iface, const var_t * def)
470 const statement_t * source;
471 STATEMENTS_FOR_EACH_FUNC( source, type_iface_get_stmts(iface)) {
472 const var_t * cas = is_callas(source->u.var->attrs );
473 if (cas && !strcmp(def->name, cas->name))
474 return source;
476 return NULL;
479 static void write_proxy_procformatstring_offsets( const type_t *iface, int skip )
481 const statement_t *stmt;
483 if (type_iface_get_inherit(iface))
484 write_proxy_procformatstring_offsets( type_iface_get_inherit(iface), need_delegation(iface));
485 else
486 return;
488 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
490 const var_t *func = stmt->u.var;
491 int missing = 0;
493 if (is_callas(func->attrs)) continue;
494 if (is_local(func->attrs))
496 const statement_t * callas_source = get_callas_source(iface, func);
497 if (!callas_source)
498 missing = 1;
499 else
500 func = callas_source->u.var;
502 if (skip || missing)
503 print_proxy( "(unsigned short)-1, /* %s::%s */\n", iface->name, get_name(func));
504 else
505 print_proxy( "%u, /* %s::%s */\n", func->procstring_offset, iface->name, get_name(func));
509 static int write_proxy_methods(type_t *iface, int skip)
511 const statement_t *stmt;
512 int i = 0;
514 if (type_iface_get_inherit(iface))
515 i = write_proxy_methods(type_iface_get_inherit(iface),
516 need_delegation(iface));
517 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
518 const var_t *func = stmt->u.var;
519 if (!is_callas(func->attrs)) {
520 if (skip || (is_local(func->attrs) && !get_callas_source(iface, func)))
521 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func));
522 else if (is_interpreted_func( iface, func ) &&
523 !is_local( func->attrs ) &&
524 type_iface_get_inherit(iface))
525 print_proxy( "(void *)-1, /* %s::%s */\n", iface->name, get_name(func));
526 else
527 print_proxy( "%s_%s_Proxy,\n", iface->name, get_name(func));
528 i++;
531 return i;
534 static int write_stub_methods(type_t *iface, int skip)
536 const statement_t *stmt;
537 int i = 0;
539 if (type_iface_get_inherit(iface))
540 i = write_stub_methods(type_iface_get_inherit(iface), need_delegation(iface));
541 else
542 return i; /* skip IUnknown */
544 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
545 const var_t *func = stmt->u.var;
546 if (!is_callas(func->attrs)) {
547 int missing = 0;
548 const char * fname = get_name(func);
549 if(is_local(func->attrs)) {
550 const statement_t * callas_source = get_callas_source(iface, func);
551 if(!callas_source)
552 missing = 1;
553 else
554 fname = get_name(callas_source->u.var);
556 if (i) fprintf(proxy,",\n");
557 if (skip || missing) print_proxy("STUB_FORWARDING_FUNCTION");
558 else if (is_interpreted_func( iface, func ))
559 print_proxy( "(PRPC_STUB_FUNCTION)%s", get_stub_mode() == MODE_Oif ? "NdrStubCall2" : "NdrStubCall" );
560 else print_proxy( "%s_%s_Stub", iface->name, fname);
561 i++;
564 return i;
567 static void write_thunk_methods( type_t *iface, int skip )
569 const statement_t *stmt;
571 if (type_iface_get_inherit( iface ))
572 write_thunk_methods( type_iface_get_inherit(iface), need_delegation(iface) );
573 else
574 return; /* skip IUnknown */
576 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
578 var_t *func = stmt->u.var;
579 const statement_t * callas_source = NULL;
581 if (is_callas(func->attrs)) continue;
582 if (is_local(func->attrs)) callas_source = get_callas_source(iface, func);
584 if (!skip && callas_source && is_interpreted_func( iface, func ))
585 print_proxy( "%s_%s_Thunk,\n", iface->name, get_name(callas_source->u.var) );
586 else
587 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func) );
591 static void write_proxy(type_t *iface, unsigned int *proc_offset)
593 int count;
594 const statement_t *stmt;
595 int first_func = 1;
596 int needs_stub_thunks = 0;
597 int needs_inline_stubs = need_inline_stubs( iface ) || need_delegation( iface );
599 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
600 var_t *func = stmt->u.var;
601 if (first_func) {
602 fprintf(proxy, "/*****************************************************************************\n");
603 fprintf(proxy, " * %s interface\n", iface->name);
604 fprintf(proxy, " */\n");
605 first_func = 0;
607 if (!is_local(func->attrs)) {
608 const var_t *cas = is_callas(func->attrs);
609 const char *cname = cas ? cas->name : NULL;
610 int idx = func->type->details.function->idx;
611 if (cname) {
612 const statement_t *stmt2;
613 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface)) {
614 const var_t *m = stmt2->u.var;
615 if (!strcmp(m->name, cname))
617 idx = m->type->details.function->idx;
618 break;
622 func->procstring_offset = *proc_offset;
623 gen_proxy(iface, func, idx, *proc_offset);
624 gen_stub(iface, func, cname, *proc_offset);
625 if (cas && is_interpreted_func( iface, func ))
627 needs_stub_thunks = 1;
628 gen_stub_thunk(iface, func, *proc_offset);
630 *proc_offset += get_size_procformatstring_func( iface, func );
634 count = count_methods(iface);
636 print_proxy( "static const unsigned short %s_FormatStringOffsetTable[] =\n", iface->name );
637 print_proxy( "{\n" );
638 indent++;
639 write_proxy_procformatstring_offsets( iface, 0 );
640 indent--;
641 print_proxy( "};\n\n" );
643 /* proxy info */
644 if (get_stub_mode() == MODE_Oif)
646 print_proxy( "static const MIDL_STUBLESS_PROXY_INFO %s_ProxyInfo =\n", iface->name );
647 print_proxy( "{\n" );
648 indent++;
649 print_proxy( "&Object_StubDesc,\n" );
650 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
651 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
652 print_proxy( "0,\n" );
653 print_proxy( "0,\n" );
654 print_proxy( "0\n" );
655 indent--;
656 print_proxy( "};\n\n" );
659 /* proxy vtable */
660 print_proxy( "static %sCINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n",
661 need_delegation_indirect(iface) ? "" : "const ", count, iface->name);
662 print_proxy( "{\n");
663 indent++;
664 print_proxy( "{\n");
665 indent++;
666 if (get_stub_mode() == MODE_Oif) 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( "CStdStubBuffer_%s\n", need_delegation_indirect(iface) ? "DELEGATING_METHODS" : "METHODS");
734 indent--;
735 print_proxy( "}\n");
736 indent--;
737 print_proxy( "};\n");
738 print_proxy( "\n");
741 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
743 const statement_t *stmt;
745 if (stmts)
746 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
748 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
750 if (pred(stmt->u.type))
751 return TRUE;
755 return FALSE;
758 int need_proxy(const type_t *iface)
760 if (!is_object( iface )) return 0;
761 if (is_local( iface->attrs )) return 0;
762 if (is_attr( iface->attrs, ATTR_DISPINTERFACE )) return 0;
763 return 1;
766 int need_stub(const type_t *iface)
768 return !is_object(iface) && !is_local(iface->attrs);
771 int need_proxy_file(const statement_list_t *stmts)
773 return does_any_iface(stmts, need_proxy);
776 int need_proxy_delegation(const statement_list_t *stmts)
778 return does_any_iface(stmts, need_delegation);
781 int need_inline_stubs(const type_t *iface)
783 const statement_t *stmt;
785 if (get_stub_mode() == MODE_Os) return 1;
787 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
789 const var_t *func = stmt->u.var;
790 if (is_local( func->attrs )) continue;
791 if (!is_interpreted_func( iface, func )) return 1;
793 return 0;
796 static int need_proxy_and_inline_stubs(const type_t *iface)
798 const statement_t *stmt;
800 if (!need_proxy( iface )) return 0;
801 if (get_stub_mode() == MODE_Os) return 1;
803 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
805 const var_t *func = stmt->u.var;
806 if (is_local( func->attrs )) continue;
807 if (!is_interpreted_func( iface, func )) return 1;
809 return 0;
812 int need_stub_files(const statement_list_t *stmts)
814 return does_any_iface(stmts, need_stub);
817 int need_inline_stubs_file(const statement_list_t *stmts)
819 return does_any_iface(stmts, need_inline_stubs);
822 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
824 const statement_t *stmt;
825 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
827 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
829 if (need_proxy(stmt->u.type))
830 write_proxy(stmt->u.type, proc_offset);
835 static int cmp_iid( const void *ptr1, const void *ptr2 )
837 const type_t * const *iface1 = ptr1;
838 const type_t * const *iface2 = ptr2;
839 const UUID *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
840 const UUID *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
841 return memcmp( uuid1, uuid2, sizeof(UUID) );
844 static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
846 const statement_t *stmt;
848 if (!stmts) return;
849 LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
851 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
853 type_t *iface = stmt->u.type;
854 if (type_iface_get_inherit(iface) && need_proxy(iface))
856 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
857 (*ifaces)[(*count)++] = iface;
863 static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
865 type_t **ifaces = NULL;
867 *count = 0;
868 build_iface_list( stmts, &ifaces, count );
869 qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
870 return ifaces;
873 static void write_proxy_routines(const statement_list_t *stmts)
875 int expr_eval_routines;
876 unsigned int proc_offset = 0;
877 char *file_id = proxy_token;
878 int i, count, have_baseiid = 0;
879 type_t **interfaces;
880 const type_t * delegate_to;
882 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
883 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ %u\n", get_stub_mode() == MODE_Oif ? 475 : 440);
884 print_proxy( "#endif\n");
885 print_proxy( "\n");
886 if (get_stub_mode() == MODE_Oif) print_proxy( "#define USE_STUBLESS_PROXY\n");
887 print_proxy( "#include \"rpcproxy.h\"\n");
888 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
889 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
890 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
891 print_proxy( "\n");
892 print_proxy( "#include \"%s\"\n", header_name);
893 print_proxy( "\n");
895 if (does_any_iface(stmts, need_proxy_and_inline_stubs))
897 write_exceptions( proxy );
898 print_proxy( "\n");
899 print_proxy( "struct __proxy_frame\n");
900 print_proxy( "{\n");
901 print_proxy( " __DECL_EXCEPTION_FRAME\n");
902 print_proxy( " MIDL_STUB_MESSAGE _StubMsg;\n");
903 print_proxy( " void *This;\n");
904 print_proxy( "};\n");
905 print_proxy( "\n");
906 print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
907 print_proxy( "{\n");
908 print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
909 print_proxy( "}\n");
910 print_proxy( "\n");
913 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
914 write_stubdescproto();
915 write_proxy_stmts(stmts, &proc_offset);
917 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
918 if (expr_eval_routines)
919 write_expr_eval_routine_list(proxy, proxy_token);
920 write_user_quad_list(proxy);
921 write_stubdesc(expr_eval_routines);
923 print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
924 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
925 print_proxy( "#endif\n");
926 print_proxy( "\n");
927 write_procformatstring(proxy, stmts, need_proxy);
928 write_typeformatstring(proxy, stmts, need_proxy);
930 interfaces = sort_interfaces(stmts, &count);
931 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
932 fprintf(proxy, "{\n");
933 for (i = 0; i < count; i++)
934 fprintf(proxy, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
935 fprintf(proxy, " 0\n");
936 fprintf(proxy, "};\n");
937 fprintf(proxy, "\n");
939 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
940 fprintf(proxy, "{\n");
941 for (i = 0; i < count; i++)
942 fprintf(proxy, " &_%sStubVtbl,\n", interfaces[i]->name);
943 fprintf(proxy, " 0\n");
944 fprintf(proxy, "};\n");
945 fprintf(proxy, "\n");
947 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
948 fprintf(proxy, "{\n");
949 for (i = 0; i < count; i++)
950 fprintf(proxy, " \"%s\",\n", interfaces[i]->name);
951 fprintf(proxy, " 0\n");
952 fprintf(proxy, "};\n");
953 fprintf(proxy, "\n");
955 for (i = 0; i < count; i++)
956 if ((have_baseiid = get_delegation_indirect( interfaces[i], NULL ))) break;
958 if (have_baseiid)
960 fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
961 fprintf(proxy, "{\n");
962 for (i = 0; i < count; i++)
964 if (get_delegation_indirect(interfaces[i], &delegate_to))
965 fprintf( proxy, " &IID_%s, /* %s */\n", delegate_to->name, interfaces[i]->name );
966 else
967 fprintf( proxy, " 0,\n" );
969 fprintf(proxy, " 0\n");
970 fprintf(proxy, "};\n");
971 fprintf(proxy, "\n");
974 fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
975 fprintf(proxy, "{\n");
976 fprintf(proxy, " int low = 0, high = %d;\n", count - 1);
977 fprintf(proxy, "\n");
978 fprintf(proxy, " while (low <= high)\n");
979 fprintf(proxy, " {\n");
980 fprintf(proxy, " int pos = (low + high) / 2;\n");
981 fprintf(proxy, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
982 fprintf(proxy, " if (!res) { *pIndex = pos; return 1; }\n");
983 fprintf(proxy, " if (res > 0) low = pos + 1;\n");
984 fprintf(proxy, " else high = pos - 1;\n");
985 fprintf(proxy, " }\n");
986 fprintf(proxy, " return 0;\n");
987 fprintf(proxy, "}\n");
988 fprintf(proxy, "\n");
990 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo DECLSPEC_HIDDEN =\n", file_id);
991 fprintf(proxy, "{\n");
992 fprintf(proxy, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
993 fprintf(proxy, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
994 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
995 if (have_baseiid) fprintf(proxy, " _%s_BaseIIDList,\n", file_id);
996 else fprintf(proxy, " 0,\n");
997 fprintf(proxy, " _%s_IID_Lookup,\n", file_id);
998 fprintf(proxy, " %d,\n", count);
999 fprintf(proxy, " %d,\n", get_stub_mode() == MODE_Oif ? 2 : 1);
1000 fprintf(proxy, " 0,\n");
1001 fprintf(proxy, " 0,\n");
1002 fprintf(proxy, " 0,\n");
1003 fprintf(proxy, " 0\n");
1004 fprintf(proxy, "};\n");
1007 void write_proxies(const statement_list_t *stmts)
1009 if (!do_proxies) return;
1010 if (do_everything && !need_proxy_file(stmts)) return;
1012 init_proxy(stmts);
1013 if(!proxy) return;
1015 if (do_win32 && do_win64)
1017 fprintf(proxy, "\n#ifndef _WIN64\n\n");
1018 pointer_size = 4;
1019 write_proxy_routines( stmts );
1020 fprintf(proxy, "\n#else /* _WIN64 */\n\n");
1021 pointer_size = 8;
1022 write_proxy_routines( stmts );
1023 fprintf(proxy, "\n#endif /* _WIN64 */\n");
1025 else if (do_win32)
1027 pointer_size = 4;
1028 write_proxy_routines( stmts );
1030 else if (do_win64)
1032 pointer_size = 8;
1033 write_proxy_routines( stmts );
1036 fclose(proxy);