winebuild: Add an option to use the C compiler to assemble files.
[wine.git] / tools / widl / proxy.c
blob0c0d7ceee5b51222c7841f446f7a5345cc6f3590
1 /*
2 * IDL Compiler
4 * Copyright 2002 Ove Kaaven
5 * Copyright 2004 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <ctype.h>
33 #include "widl.h"
34 #include "utils.h"
35 #include "parser.h"
36 #include "header.h"
37 #include "typegen.h"
38 #include "expr.h"
40 #define END_OF_LIST(list) \
41 do { \
42 if (list) { \
43 while (NEXT_LINK(list)) \
44 list = NEXT_LINK(list); \
45 } \
46 } while(0)
48 static FILE* proxy;
49 static int indent = 0;
51 static void print_proxy( const char *format, ... ) __attribute__((format (printf, 1, 2)));
52 static void print_proxy( const char *format, ... )
54 va_list va;
55 va_start( va, format );
56 print( proxy, indent, format, va );
57 va_end( va );
60 static void write_stubdescproto(void)
62 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
63 print_proxy( "\n");
66 static void write_stubdesc(int expr_eval_routines)
68 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc =\n{\n");
69 indent++;
70 print_proxy( "0,\n");
71 print_proxy( "NdrOleAllocate,\n");
72 print_proxy( "NdrOleFree,\n");
73 print_proxy( "{0}, 0, 0, %s, 0,\n", expr_eval_routines ? "ExprEvalRoutines" : "0");
74 print_proxy( "__MIDL_TypeFormatString.Format,\n");
75 print_proxy( "1, /* -error bounds_check flag */\n");
76 print_proxy( "0x%x, /* Ndr library version */\n", get_stub_mode() == MODE_Oif ? 0x50002 : 0x10001);
77 print_proxy( "0,\n");
78 print_proxy( "0x50100a4, /* MIDL Version 5.1.164 */\n");
79 print_proxy( "0,\n");
80 print_proxy("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
81 print_proxy( "0, /* notify & notify_flag routine table */\n");
82 print_proxy( "1, /* Flags */\n");
83 print_proxy( "0, /* Reserved3 */\n");
84 print_proxy( "0, /* Reserved4 */\n");
85 print_proxy( "0 /* Reserved5 */\n");
86 indent--;
87 print_proxy( "};\n");
88 print_proxy( "\n");
91 static void init_proxy(const statement_list_t *stmts)
93 if (proxy) return;
94 if(!(proxy = fopen(proxy_name, "w")))
95 error("Could not open %s for output\n", proxy_name);
96 print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
97 print_proxy( "\n");
98 print_proxy( "#define __midl_proxy\n");
99 print_proxy( "#include \"objbase.h\"\n");
100 print_proxy( "\n");
101 print_proxy( "#ifndef DECLSPEC_HIDDEN\n");
102 print_proxy( "#define DECLSPEC_HIDDEN\n");
103 print_proxy( "#endif\n");
104 print_proxy( "\n");
107 static void clear_output_vars( const var_list_t *args )
109 const var_t *arg;
111 if (!args) return;
112 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
114 if (is_attr(arg->attrs, ATTR_IN)) continue;
115 if (!is_attr(arg->attrs, ATTR_OUT)) continue;
116 if (is_ptr(arg->type))
118 if (type_get_type(type_pointer_get_ref(arg->type)) == TYPE_BASIC) continue;
119 if (type_get_type(type_pointer_get_ref(arg->type)) == TYPE_ENUM) continue;
121 print_proxy( "if (%s) MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name, arg->name );
125 static int need_delegation(const type_t *iface)
127 const type_t *parent = type_iface_get_inherit( iface );
128 return parent && type_iface_get_inherit(parent) && (parent->ignore || is_local( parent->attrs ));
131 static int get_delegation_indirect(const type_t *iface, const type_t ** delegate_to)
133 const type_t * cur_iface;
134 for (cur_iface = iface; cur_iface != NULL; cur_iface = type_iface_get_inherit(cur_iface))
135 if (need_delegation(cur_iface))
137 if(delegate_to)
138 *delegate_to = type_iface_get_inherit(cur_iface);
139 return 1;
141 return 0;
144 static int need_delegation_indirect(const type_t *iface)
146 return get_delegation_indirect(iface, NULL);
149 static void free_variable( const var_t *arg, const char *local_var_prefix )
151 unsigned int type_offset = arg->typestring_offset;
152 type_t *type = arg->type;
154 write_parameter_conf_or_var_exprs(proxy, indent, local_var_prefix, PHASE_FREE, arg, FALSE);
156 switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
158 case TGT_ENUM:
159 case TGT_BASIC:
160 break;
162 case TGT_STRUCT:
163 if (get_struct_fc(type) != RPC_FC_STRUCT)
164 print_proxy("/* FIXME: %s code for %s struct type 0x%x missing */\n", __FUNCTION__, arg->name, get_struct_fc(type) );
165 break;
167 case TGT_IFACE_POINTER:
168 case TGT_POINTER:
169 case TGT_ARRAY:
170 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
171 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
172 fprintf(proxy, "(void *)%s );\n", arg->name );
173 break;
175 default:
176 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
180 static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
182 const var_t *arg;
184 if (!args) return;
185 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
186 if (is_attr(arg->attrs, ATTR_OUT))
188 free_variable( arg, local_var_prefix );
189 fprintf(proxy, "\n");
193 static void gen_proxy(type_t *iface, const var_t *func, int idx,
194 unsigned int proc_offset)
196 var_t *retval = type_function_get_retval(func->type);
197 int has_ret = !is_void(retval->type);
198 int has_full_pointer = is_full_pointer_function(func);
199 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
200 const var_list_t *args = type_get_function_args(func->type);
201 if (!callconv) callconv = "STDMETHODCALLTYPE";
203 indent = 0;
204 if (is_interpreted_func( iface, func ))
206 if (get_stub_mode() == MODE_Oif && !is_callas( func->attrs )) return;
207 write_type_decl_left(proxy, retval->type);
208 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
209 write_args(proxy, args, iface->name, 1, TRUE);
210 print_proxy( ")\n");
211 write_client_call_routine( proxy, iface, func, "Object", proc_offset );
212 return;
214 print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
215 iface->name, get_name(func) );
216 print_proxy( "{\n");
217 indent++;
218 if (has_full_pointer) write_full_pointer_free(proxy, indent, func);
219 print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
220 indent--;
221 print_proxy( "}\n");
222 print_proxy( "\n");
224 write_type_decl_left(proxy, retval->type);
225 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
226 write_args(proxy, args, iface->name, 1, TRUE);
227 print_proxy( ")\n");
228 print_proxy( "{\n");
229 indent ++;
230 print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
231 /* local variables */
232 if (has_ret) {
233 print_proxy( "%s", "" );
234 write_type_decl(proxy, retval->type, retval->name);
235 fprintf( proxy, ";\n" );
237 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
238 if (has_ret) {
239 if (decl_indirect(retval->type))
240 print_proxy("void *_p_%s = &%s;\n", retval->name, retval->name);
242 print_proxy( "\n");
244 print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(func) );
245 print_proxy( "__frame->This = This;\n" );
247 if (has_full_pointer)
248 write_full_pointer_init(proxy, indent, func, FALSE);
250 /* FIXME: trace */
251 clear_output_vars( type_get_function_args(func->type) );
253 print_proxy( "RpcTryExcept\n" );
254 print_proxy( "{\n" );
255 indent++;
256 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx);
257 write_pointer_checks( proxy, indent, func );
259 print_proxy( "RpcTryFinally\n" );
260 print_proxy( "{\n" );
261 indent++;
263 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
265 print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
267 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_MARSHAL);
269 print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
270 fprintf(proxy, "\n");
271 print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
272 print_proxy( "__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
274 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
275 indent++;
276 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
277 indent--;
278 fprintf(proxy, "\n");
280 write_remoting_arguments(proxy, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
282 if (has_ret)
284 if (decl_indirect(retval->type))
285 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", retval->name, retval->name);
286 else if (is_ptr(retval->type) || is_array(retval->type))
287 print_proxy("%s = 0;\n", retval->name);
288 write_remoting_arguments(proxy, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
291 indent--;
292 print_proxy( "}\n");
293 print_proxy( "RpcFinally\n" );
294 print_proxy( "{\n" );
295 indent++;
296 print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(func) );
297 indent--;
298 print_proxy( "}\n");
299 print_proxy( "RpcEndFinally\n" );
300 indent--;
301 print_proxy( "}\n" );
302 print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
303 print_proxy( "{\n" );
304 if (has_ret) {
305 indent++;
306 proxy_free_variables( type_get_function_args(func->type), "" );
307 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
308 indent--;
310 print_proxy( "}\n" );
311 print_proxy( "RpcEndExcept\n" );
313 if (has_ret) {
314 print_proxy( "return _RetVal;\n" );
316 indent--;
317 print_proxy( "}\n");
318 print_proxy( "\n");
321 static void gen_stub(type_t *iface, const var_t *func, const char *cas,
322 unsigned int proc_offset)
324 const var_t *arg;
325 int has_ret = !is_void(type_function_get_rettype(func->type));
326 int has_full_pointer = is_full_pointer_function(func);
328 if (is_interpreted_func( iface, func )) return;
330 indent = 0;
331 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(func));
332 indent++;
333 print_proxy( "__DECL_EXCEPTION_FRAME\n" );
334 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
335 print_proxy( "%s * _This;\n", iface->name );
336 declare_stub_args( proxy, indent, func );
337 indent--;
338 print_proxy( "};\n\n" );
340 print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(func) );
341 print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(func) );
342 indent++;
343 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
344 if (has_full_pointer)
345 write_full_pointer_free(proxy, indent, func);
346 indent--;
347 print_proxy( "}\n\n" );
349 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
350 indent++;
351 print_proxy( "IRpcStubBuffer* This,\n");
352 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
353 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
354 print_proxy( "DWORD* _pdwStubPhase)\n");
355 indent--;
356 print_proxy( "{\n");
357 indent++;
358 print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
359 iface->name, get_name(func) );
361 print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
363 /* FIXME: trace */
365 print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
366 fprintf(proxy, "\n");
367 print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(func) );
369 write_parameters_init(proxy, indent, func, "__frame->");
371 print_proxy("RpcTryFinally\n");
372 print_proxy("{\n");
373 indent++;
374 if (has_full_pointer)
375 write_full_pointer_init(proxy, indent, func, TRUE);
376 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
377 indent++;
378 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
379 indent--;
380 fprintf(proxy, "\n");
382 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
383 fprintf(proxy, "\n");
385 assign_stub_out_args( proxy, indent, func, "__frame->" );
387 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
388 fprintf(proxy, "\n");
389 print_proxy( "%s", has_ret ? "__frame->_RetVal = " : "" );
390 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
391 else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(func));
392 fprintf(proxy, "(__frame->_This");
394 if (type_get_function_args(func->type))
396 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
397 fprintf(proxy, ", %s__frame->%s", is_array(arg->type) && !type_array_is_decl_as_ptr(arg->type) ? "*" :"" , arg->name);
399 fprintf(proxy, ");\n");
400 fprintf(proxy, "\n");
401 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
402 fprintf(proxy, "\n");
404 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
406 if (!is_void(type_function_get_rettype(func->type)))
407 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
409 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
411 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
412 fprintf(proxy, "\n");
414 /* marshall the return value */
415 if (!is_void(type_function_get_rettype(func->type)))
416 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
418 indent--;
419 print_proxy("}\n");
420 print_proxy("RpcFinally\n");
421 print_proxy("{\n");
422 indent++;
423 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(func) );
424 indent--;
425 print_proxy("}\n");
426 print_proxy("RpcEndFinally\n");
428 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
429 indent--;
431 print_proxy("}\n");
432 print_proxy("\n");
435 static void gen_stub_thunk( type_t *iface, const var_t *func, unsigned int proc_offset )
437 int has_ret = !is_void( type_function_get_rettype( func->type ));
438 const var_t *arg, *callas = is_callas( func->attrs );
439 const var_list_t *args = type_get_function_args( func->type );
441 indent = 0;
442 print_proxy( "void __RPC_API %s_%s_Thunk( PMIDL_STUB_MESSAGE pStubMsg )\n",
443 iface->name, get_name(func) );
444 print_proxy( "{\n");
445 indent++;
446 write_func_param_struct( proxy, iface, func->type,
447 "*pParamStruct = (struct _PARAM_STRUCT *)pStubMsg->StackTop", has_ret );
448 print_proxy( "%s%s_%s_Stub( pParamStruct->This",
449 has_ret ? "pParamStruct->_RetVal = " : "", iface->name, callas->name );
450 indent++;
451 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
453 fprintf( proxy, ",\n%*spParamStruct->%s", 4 * indent, "", arg->name );
455 fprintf( proxy, " );\n" );
456 indent--;
457 indent--;
458 print_proxy( "}\n\n");
461 int count_methods(const type_t *iface)
463 const statement_t *stmt;
464 int count = 0;
466 if (type_iface_get_inherit(iface))
467 count = count_methods(type_iface_get_inherit(iface));
469 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
470 const var_t *func = stmt->u.var;
471 if (!is_callas(func->attrs)) count++;
473 return count;
476 static const statement_t * get_callas_source(const type_t * iface, const var_t * def)
478 const statement_t * source;
479 STATEMENTS_FOR_EACH_FUNC( source, type_iface_get_stmts(iface)) {
480 const var_t * cas = is_callas(source->u.var->attrs );
481 if (cas && !strcmp(def->name, cas->name))
482 return source;
484 return NULL;
487 static void write_proxy_procformatstring_offsets( const type_t *iface, int skip )
489 const statement_t *stmt;
491 if (type_iface_get_inherit(iface))
492 write_proxy_procformatstring_offsets( type_iface_get_inherit(iface), need_delegation(iface));
493 else
494 return;
496 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
498 const var_t *func = stmt->u.var;
499 int missing = 0;
501 if (is_callas(func->attrs)) continue;
502 if (is_local(func->attrs))
504 const statement_t * callas_source = get_callas_source(iface, func);
505 if (!callas_source)
506 missing = 1;
507 else
508 func = callas_source->u.var;
510 if (skip || missing)
511 print_proxy( "(unsigned short)-1, /* %s::%s */\n", iface->name, get_name(func));
512 else
513 print_proxy( "%u, /* %s::%s */\n", func->procstring_offset, iface->name, get_name(func));
517 static int write_proxy_methods(type_t *iface, int skip)
519 const statement_t *stmt;
520 int i = 0;
522 if (type_iface_get_inherit(iface))
523 i = write_proxy_methods(type_iface_get_inherit(iface),
524 need_delegation(iface));
525 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
526 const var_t *func = stmt->u.var;
527 if (!is_callas(func->attrs)) {
528 if (skip || (is_local(func->attrs) && !get_callas_source(iface, func)))
529 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func));
530 else if (is_interpreted_func( iface, func ) &&
531 !is_local( func->attrs ) &&
532 type_iface_get_inherit(iface))
533 print_proxy( "(void *)-1, /* %s::%s */\n", iface->name, get_name(func));
534 else
535 print_proxy( "%s_%s_Proxy,\n", iface->name, get_name(func));
536 i++;
539 return i;
542 static int write_stub_methods(type_t *iface, int skip)
544 const statement_t *stmt;
545 int i = 0;
547 if (type_iface_get_inherit(iface))
548 i = write_stub_methods(type_iface_get_inherit(iface), need_delegation(iface));
549 else
550 return i; /* skip IUnknown */
552 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
553 const var_t *func = stmt->u.var;
554 if (!is_callas(func->attrs)) {
555 int missing = 0;
556 const char * fname = get_name(func);
557 if(is_local(func->attrs)) {
558 const statement_t * callas_source = get_callas_source(iface, func);
559 if(!callas_source)
560 missing = 1;
561 else
562 fname = get_name(callas_source->u.var);
564 if (i) fprintf(proxy,",\n");
565 if (skip || missing) print_proxy("STUB_FORWARDING_FUNCTION");
566 else if (is_interpreted_func( iface, func ))
567 print_proxy( "(PRPC_STUB_FUNCTION)%s", get_stub_mode() == MODE_Oif ? "NdrStubCall2" : "NdrStubCall" );
568 else print_proxy( "%s_%s_Stub", iface->name, fname);
569 i++;
572 return i;
575 static void write_thunk_methods( type_t *iface, int skip )
577 const statement_t *stmt;
579 if (type_iface_get_inherit( iface ))
580 write_thunk_methods( type_iface_get_inherit(iface), need_delegation(iface) );
581 else
582 return; /* skip IUnknown */
584 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
586 var_t *func = stmt->u.var;
587 const statement_t * callas_source = NULL;
589 if (is_callas(func->attrs)) continue;
590 if (is_local(func->attrs)) callas_source = get_callas_source(iface, func);
592 if (!skip && callas_source && is_interpreted_func( iface, func ))
593 print_proxy( "%s_%s_Thunk,\n", iface->name, get_name(callas_source->u.var) );
594 else
595 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func) );
599 static void write_proxy(type_t *iface, unsigned int *proc_offset)
601 int count;
602 const statement_t *stmt;
603 int first_func = 1;
604 int needs_stub_thunks = 0;
605 int needs_inline_stubs = need_inline_stubs( iface ) || need_delegation( iface );
607 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
608 var_t *func = stmt->u.var;
609 if (first_func) {
610 fprintf(proxy, "/*****************************************************************************\n");
611 fprintf(proxy, " * %s interface\n", iface->name);
612 fprintf(proxy, " */\n");
613 first_func = 0;
615 if (!is_local(func->attrs)) {
616 const var_t *cas = is_callas(func->attrs);
617 const char *cname = cas ? cas->name : NULL;
618 int idx = func->type->details.function->idx;
619 if (cname) {
620 const statement_t *stmt2;
621 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface)) {
622 const var_t *m = stmt2->u.var;
623 if (!strcmp(m->name, cname))
625 idx = m->type->details.function->idx;
626 break;
630 func->procstring_offset = *proc_offset;
631 gen_proxy(iface, func, idx, *proc_offset);
632 gen_stub(iface, func, cname, *proc_offset);
633 if (cas && is_interpreted_func( iface, func ))
635 needs_stub_thunks = 1;
636 gen_stub_thunk(iface, func, *proc_offset);
638 *proc_offset += get_size_procformatstring_func( iface, func );
642 count = count_methods(iface);
644 print_proxy( "static const unsigned short %s_FormatStringOffsetTable[] =\n", iface->name );
645 print_proxy( "{\n" );
646 indent++;
647 write_proxy_procformatstring_offsets( iface, 0 );
648 indent--;
649 print_proxy( "};\n\n" );
651 /* proxy info */
652 if (get_stub_mode() == MODE_Oif)
654 print_proxy( "static const MIDL_STUBLESS_PROXY_INFO %s_ProxyInfo =\n", iface->name );
655 print_proxy( "{\n" );
656 indent++;
657 print_proxy( "&Object_StubDesc,\n" );
658 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
659 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
660 print_proxy( "0,\n" );
661 print_proxy( "0,\n" );
662 print_proxy( "0\n" );
663 indent--;
664 print_proxy( "};\n\n" );
667 /* proxy vtable */
668 print_proxy( "static %sCINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n",
669 need_delegation_indirect(iface) ? "" : "const ", count, iface->name);
670 print_proxy( "{\n");
671 indent++;
672 print_proxy( "{\n");
673 indent++;
674 if (get_stub_mode() == MODE_Oif) print_proxy( "&%s_ProxyInfo,\n", iface->name );
675 print_proxy( "&IID_%s,\n", iface->name);
676 indent--;
677 print_proxy( "},\n");
678 print_proxy( "{\n");
679 indent++;
680 write_proxy_methods(iface, FALSE);
681 indent--;
682 print_proxy( "}\n");
683 indent--;
684 print_proxy( "};\n\n");
686 /* stub thunk table */
687 if (needs_stub_thunks)
689 print_proxy( "static const STUB_THUNK %s_StubThunkTable[] =\n", iface->name);
690 print_proxy( "{\n");
691 indent++;
692 write_thunk_methods( iface, 0 );
693 indent--;
694 print_proxy( "};\n\n");
697 /* server info */
698 print_proxy( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
699 print_proxy( "{\n" );
700 indent++;
701 print_proxy( "&Object_StubDesc,\n" );
702 print_proxy( "0,\n" );
703 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
704 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
705 if (needs_stub_thunks)
706 print_proxy( "&%s_StubThunkTable[-3],\n", iface->name );
707 else
708 print_proxy( "0,\n" );
709 print_proxy( "0,\n" );
710 print_proxy( "0,\n" );
711 print_proxy( "0\n" );
712 indent--;
713 print_proxy( "};\n\n" );
715 /* stub vtable */
716 if (needs_inline_stubs)
718 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
719 print_proxy( "{\n");
720 indent++;
721 write_stub_methods(iface, FALSE);
722 fprintf(proxy, "\n");
723 indent--;
724 fprintf(proxy, "};\n\n");
726 print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
727 need_delegation_indirect(iface) ? "" : "const ", iface->name);
728 print_proxy( "{\n");
729 indent++;
730 print_proxy( "{\n");
731 indent++;
732 print_proxy( "&IID_%s,\n", iface->name);
733 print_proxy( "&%s_ServerInfo,\n", iface->name );
734 print_proxy( "%d,\n", count);
735 if (needs_inline_stubs) print_proxy( "&%s_table[-3]\n", iface->name );
736 else print_proxy( "0\n" );
737 indent--;
738 print_proxy( "},\n");
739 print_proxy( "{\n");
740 indent++;
741 print_proxy( "CStdStubBuffer_%s\n", need_delegation_indirect(iface) ? "DELEGATING_METHODS" : "METHODS");
742 indent--;
743 print_proxy( "}\n");
744 indent--;
745 print_proxy( "};\n");
746 print_proxy( "\n");
749 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
751 const statement_t *stmt;
753 if (stmts)
754 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
756 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
758 if (pred(stmt->u.type))
759 return TRUE;
763 return FALSE;
766 int need_proxy(const type_t *iface)
768 if (!is_object( iface )) return 0;
769 if (is_local( iface->attrs )) return 0;
770 if (is_attr( iface->attrs, ATTR_DISPINTERFACE )) return 0;
771 return 1;
774 int need_stub(const type_t *iface)
776 return !is_object(iface) && !is_local(iface->attrs);
779 int need_proxy_file(const statement_list_t *stmts)
781 return does_any_iface(stmts, need_proxy);
784 int need_proxy_delegation(const statement_list_t *stmts)
786 return does_any_iface(stmts, need_delegation);
789 int need_inline_stubs(const type_t *iface)
791 const statement_t *stmt;
793 if (get_stub_mode() == MODE_Os) return 1;
795 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
797 const var_t *func = stmt->u.var;
798 if (is_local( func->attrs )) continue;
799 if (!is_interpreted_func( iface, func )) return 1;
801 return 0;
804 static int need_proxy_and_inline_stubs(const type_t *iface)
806 const statement_t *stmt;
808 if (!need_proxy( iface )) return 0;
809 if (get_stub_mode() == MODE_Os) return 1;
811 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
813 const var_t *func = stmt->u.var;
814 if (is_local( func->attrs )) continue;
815 if (!is_interpreted_func( iface, func )) return 1;
817 return 0;
820 int need_stub_files(const statement_list_t *stmts)
822 return does_any_iface(stmts, need_stub);
825 int need_inline_stubs_file(const statement_list_t *stmts)
827 return does_any_iface(stmts, need_inline_stubs);
830 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
832 const statement_t *stmt;
833 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
835 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
837 if (need_proxy(stmt->u.type))
838 write_proxy(stmt->u.type, proc_offset);
843 static int cmp_iid( const void *ptr1, const void *ptr2 )
845 const type_t * const *iface1 = ptr1;
846 const type_t * const *iface2 = ptr2;
847 const UUID *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
848 const UUID *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
849 return memcmp( uuid1, uuid2, sizeof(UUID) );
852 static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
854 const statement_t *stmt;
856 if (!stmts) return;
857 LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
859 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
861 type_t *iface = stmt->u.type;
862 if (type_iface_get_inherit(iface) && need_proxy(iface))
864 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
865 (*ifaces)[(*count)++] = iface;
871 static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
873 type_t **ifaces = NULL;
875 *count = 0;
876 build_iface_list( stmts, &ifaces, count );
877 qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
878 return ifaces;
881 static void write_proxy_routines(const statement_list_t *stmts)
883 int expr_eval_routines;
884 unsigned int proc_offset = 0;
885 char *file_id = proxy_token;
886 int i, count, have_baseiid = 0;
887 type_t **interfaces;
888 const type_t * delegate_to;
890 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
891 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ %u\n", get_stub_mode() == MODE_Oif ? 475 : 440);
892 print_proxy( "#endif\n");
893 print_proxy( "\n");
894 if (get_stub_mode() == MODE_Oif) print_proxy( "#define USE_STUBLESS_PROXY\n");
895 print_proxy( "#include \"rpcproxy.h\"\n");
896 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
897 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
898 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
899 print_proxy( "\n");
900 print_proxy( "#include \"%s\"\n", header_name);
901 print_proxy( "\n");
903 if (does_any_iface(stmts, need_proxy_and_inline_stubs))
905 write_exceptions( proxy );
906 print_proxy( "\n");
907 print_proxy( "struct __proxy_frame\n");
908 print_proxy( "{\n");
909 print_proxy( " __DECL_EXCEPTION_FRAME\n");
910 print_proxy( " MIDL_STUB_MESSAGE _StubMsg;\n");
911 print_proxy( " void *This;\n");
912 print_proxy( "};\n");
913 print_proxy( "\n");
914 print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
915 print_proxy( "{\n");
916 print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
917 print_proxy( "}\n");
918 print_proxy( "\n");
921 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
922 write_stubdescproto();
923 write_proxy_stmts(stmts, &proc_offset);
925 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
926 if (expr_eval_routines)
927 write_expr_eval_routine_list(proxy, proxy_token);
928 write_user_quad_list(proxy);
929 write_stubdesc(expr_eval_routines);
931 print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
932 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
933 print_proxy( "#endif\n");
934 print_proxy( "\n");
935 write_procformatstring(proxy, stmts, need_proxy);
936 write_typeformatstring(proxy, stmts, need_proxy);
938 interfaces = sort_interfaces(stmts, &count);
939 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
940 fprintf(proxy, "{\n");
941 for (i = 0; i < count; i++)
942 fprintf(proxy, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
943 fprintf(proxy, " 0\n");
944 fprintf(proxy, "};\n");
945 fprintf(proxy, "\n");
947 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
948 fprintf(proxy, "{\n");
949 for (i = 0; i < count; i++)
950 fprintf(proxy, " &_%sStubVtbl,\n", interfaces[i]->name);
951 fprintf(proxy, " 0\n");
952 fprintf(proxy, "};\n");
953 fprintf(proxy, "\n");
955 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
956 fprintf(proxy, "{\n");
957 for (i = 0; i < count; i++)
958 fprintf(proxy, " \"%s\",\n", interfaces[i]->name);
959 fprintf(proxy, " 0\n");
960 fprintf(proxy, "};\n");
961 fprintf(proxy, "\n");
963 for (i = 0; i < count; i++)
964 if ((have_baseiid = get_delegation_indirect( interfaces[i], NULL ))) break;
966 if (have_baseiid)
968 fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
969 fprintf(proxy, "{\n");
970 for (i = 0; i < count; i++)
972 if (get_delegation_indirect(interfaces[i], &delegate_to))
973 fprintf( proxy, " &IID_%s, /* %s */\n", delegate_to->name, interfaces[i]->name );
974 else
975 fprintf( proxy, " 0,\n" );
977 fprintf(proxy, " 0\n");
978 fprintf(proxy, "};\n");
979 fprintf(proxy, "\n");
982 fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
983 fprintf(proxy, "{\n");
984 fprintf(proxy, " int low = 0, high = %d;\n", count - 1);
985 fprintf(proxy, "\n");
986 fprintf(proxy, " while (low <= high)\n");
987 fprintf(proxy, " {\n");
988 fprintf(proxy, " int pos = (low + high) / 2;\n");
989 fprintf(proxy, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
990 fprintf(proxy, " if (!res) { *pIndex = pos; return 1; }\n");
991 fprintf(proxy, " if (res > 0) low = pos + 1;\n");
992 fprintf(proxy, " else high = pos - 1;\n");
993 fprintf(proxy, " }\n");
994 fprintf(proxy, " return 0;\n");
995 fprintf(proxy, "}\n");
996 fprintf(proxy, "\n");
998 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo DECLSPEC_HIDDEN =\n", file_id);
999 fprintf(proxy, "{\n");
1000 fprintf(proxy, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
1001 fprintf(proxy, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
1002 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
1003 if (have_baseiid) fprintf(proxy, " _%s_BaseIIDList,\n", file_id);
1004 else fprintf(proxy, " 0,\n");
1005 fprintf(proxy, " _%s_IID_Lookup,\n", file_id);
1006 fprintf(proxy, " %d,\n", count);
1007 fprintf(proxy, " %d,\n", get_stub_mode() == MODE_Oif ? 2 : 1);
1008 fprintf(proxy, " 0,\n");
1009 fprintf(proxy, " 0,\n");
1010 fprintf(proxy, " 0,\n");
1011 fprintf(proxy, " 0\n");
1012 fprintf(proxy, "};\n");
1015 void write_proxies(const statement_list_t *stmts)
1017 if (!do_proxies) return;
1018 if (do_everything && !need_proxy_file(stmts)) return;
1020 init_proxy(stmts);
1021 if(!proxy) return;
1023 if (do_win32 && do_win64)
1025 fprintf(proxy, "\n#ifndef _WIN64\n\n");
1026 pointer_size = 4;
1027 write_proxy_routines( stmts );
1028 fprintf(proxy, "\n#else /* _WIN64 */\n\n");
1029 pointer_size = 8;
1030 write_proxy_routines( stmts );
1031 fprintf(proxy, "\n#endif /* _WIN64 */\n");
1033 else if (do_win32)
1035 pointer_size = 4;
1036 write_proxy_routines( stmts );
1038 else if (do_win64)
1040 pointer_size = 8;
1041 write_proxy_routines( stmts );
1044 fclose(proxy);