[MKHIVE] Minor improvements.
[reactos.git] / sdk / tools / widl / proxy.c
1 /*
2 * IDL Compiler
3 *
4 * Copyright 2002 Ove Kaaven
5 * Copyright 2004 Mike McCormack
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
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>
32
33 #include "widl.h"
34 #include "utils.h"
35 #include "parser.h"
36 #include "header.h"
37 #include "typegen.h"
38 #include "expr.h"
39
40 static FILE* proxy;
41 static int indent = 0;
42
43 static void print_proxy( const char *format, ... ) __attribute__((format (printf, 1, 2)));
44 static void print_proxy( const char *format, ... )
45 {
46 va_list va;
47 va_start( va, format );
48 print( proxy, indent, format, va );
49 va_end( va );
50 }
51
52 static void write_stubdescproto(void)
53 {
54 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
55 print_proxy( "\n");
56 }
57
58 static void write_stubdesc(int expr_eval_routines)
59 {
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");
81 }
82
83 static void init_proxy(const statement_list_t *stmts)
84 {
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\n");
91
92 print_proxy( "#ifdef __REACTOS__\n");
93 print_proxy( "#define WIN32_NO_STATUS\n");
94 print_proxy( "#define WIN32_LEAN_AND_MEAN\n");
95 print_proxy( "#endif\n\n");
96
97 print_proxy( "#include \"objbase.h\"\n");
98 print_proxy( "\n");
99 print_proxy( "#ifndef DECLSPEC_HIDDEN\n");
100 print_proxy( "#define DECLSPEC_HIDDEN\n");
101 print_proxy( "#endif\n");
102 print_proxy( "\n");
103 }
104
105 static void clear_output_vars( const var_list_t *args )
106 {
107 const var_t *arg;
108
109 if (!args) return;
110 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
111 {
112 if (is_attr(arg->attrs, ATTR_IN)) continue;
113 if (!is_attr(arg->attrs, ATTR_OUT)) continue;
114 if (is_ptr(arg->type))
115 {
116 if (type_get_type(type_pointer_get_ref(arg->type)) == TYPE_BASIC) continue;
117 if (type_get_type(type_pointer_get_ref(arg->type)) == TYPE_ENUM) continue;
118 }
119 print_proxy( "if (%s) MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name, arg->name );
120 }
121 }
122
123 static int need_delegation(const type_t *iface)
124 {
125 const type_t *parent = type_iface_get_inherit( iface );
126 return parent && type_iface_get_inherit(parent) && (parent->ignore || is_local( parent->attrs ));
127 }
128
129 static int get_delegation_indirect(const type_t *iface, const type_t ** delegate_to)
130 {
131 const type_t * cur_iface;
132 for (cur_iface = iface; cur_iface != NULL; cur_iface = type_iface_get_inherit(cur_iface))
133 if (need_delegation(cur_iface))
134 {
135 if(delegate_to)
136 *delegate_to = type_iface_get_inherit(cur_iface);
137 return 1;
138 }
139 return 0;
140 }
141
142 static int need_delegation_indirect(const type_t *iface)
143 {
144 return get_delegation_indirect(iface, NULL);
145 }
146
147 static void free_variable( const var_t *arg, const char *local_var_prefix )
148 {
149 unsigned int type_offset = arg->typestring_offset;
150 type_t *type = arg->type;
151
152 write_parameter_conf_or_var_exprs(proxy, indent, local_var_prefix, PHASE_FREE, arg, FALSE);
153
154 switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
155 {
156 case TGT_ENUM:
157 case TGT_BASIC:
158 break;
159
160 case TGT_STRUCT:
161 if (get_struct_fc(type) != RPC_FC_STRUCT)
162 print_proxy("/* FIXME: %s code for %s struct type 0x%x missing */\n", __FUNCTION__, arg->name, get_struct_fc(type) );
163 break;
164
165 case TGT_IFACE_POINTER:
166 case TGT_POINTER:
167 case TGT_ARRAY:
168 print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
169 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
170 fprintf(proxy, "(void *)%s );\n", arg->name );
171 break;
172
173 default:
174 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
175 }
176 }
177
178 static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
179 {
180 const var_t *arg;
181
182 if (!args) return;
183 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
184 if (is_attr(arg->attrs, ATTR_OUT))
185 {
186 free_variable( arg, local_var_prefix );
187 fprintf(proxy, "\n");
188 }
189 }
190
191 static void gen_proxy(type_t *iface, const var_t *func, int idx,
192 unsigned int proc_offset)
193 {
194 var_t *retval = type_function_get_retval(func->type);
195 int has_ret = !is_void(retval->type);
196 int has_full_pointer = is_full_pointer_function(func);
197 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
198 const var_list_t *args = type_get_function_args(func->type);
199 if (!callconv) callconv = "STDMETHODCALLTYPE";
200
201 indent = 0;
202 if (is_interpreted_func( iface, func ))
203 {
204 if (get_stub_mode() == MODE_Oif && !is_callas( func->attrs )) return;
205 write_type_decl_left(proxy, retval->type);
206 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
207 write_args(proxy, args, iface->name, 1, TRUE);
208 print_proxy( ")\n");
209 write_client_call_routine( proxy, iface, func, "Object", proc_offset );
210 return;
211 }
212 print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
213 iface->name, get_name(func) );
214 print_proxy( "{\n");
215 indent++;
216 if (has_full_pointer) write_full_pointer_free(proxy, indent, func);
217 print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
218 indent--;
219 print_proxy( "}\n");
220 print_proxy( "\n");
221
222 write_type_decl_left(proxy, retval->type);
223 print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
224 write_args(proxy, args, iface->name, 1, TRUE);
225 print_proxy( ")\n");
226 print_proxy( "{\n");
227 indent ++;
228 print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
229 /* local variables */
230 if (has_ret) {
231 print_proxy( "%s", "" );
232 write_type_decl(proxy, retval->type, retval->name);
233 fprintf( proxy, ";\n" );
234 }
235 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
236 if (has_ret) {
237 if (decl_indirect(retval->type))
238 print_proxy("void *_p_%s = &%s;\n", retval->name, retval->name);
239 }
240 print_proxy( "\n");
241
242 print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(func) );
243 print_proxy( "__frame->This = This;\n" );
244
245 if (has_full_pointer)
246 write_full_pointer_init(proxy, indent, func, FALSE);
247
248 /* FIXME: trace */
249 clear_output_vars( type_get_function_args(func->type) );
250
251 print_proxy( "RpcTryExcept\n" );
252 print_proxy( "{\n" );
253 indent++;
254 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx);
255 write_pointer_checks( proxy, indent, func );
256
257 print_proxy( "RpcTryFinally\n" );
258 print_proxy( "{\n" );
259 indent++;
260
261 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
262
263 print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
264
265 write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_MARSHAL);
266
267 print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
268 fprintf(proxy, "\n");
269 print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
270 print_proxy( "__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
271
272 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
273 indent++;
274 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
275 indent--;
276 fprintf(proxy, "\n");
277
278 write_remoting_arguments(proxy, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
279
280 if (has_ret)
281 {
282 if (decl_indirect(retval->type))
283 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", retval->name, retval->name);
284 else if (is_ptr(retval->type) || is_array(retval->type))
285 print_proxy("%s = 0;\n", retval->name);
286 write_remoting_arguments(proxy, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
287 }
288
289 indent--;
290 print_proxy( "}\n");
291 print_proxy( "RpcFinally\n" );
292 print_proxy( "{\n" );
293 indent++;
294 print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(func) );
295 indent--;
296 print_proxy( "}\n");
297 print_proxy( "RpcEndFinally\n" );
298 indent--;
299 print_proxy( "}\n" );
300 print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
301 print_proxy( "{\n" );
302 if (has_ret) {
303 indent++;
304 proxy_free_variables( type_get_function_args(func->type), "" );
305 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
306 indent--;
307 }
308 print_proxy( "}\n" );
309 print_proxy( "RpcEndExcept\n" );
310
311 if (has_ret) {
312 print_proxy( "return _RetVal;\n" );
313 }
314 indent--;
315 print_proxy( "}\n");
316 print_proxy( "\n");
317 }
318
319 static void gen_stub(type_t *iface, const var_t *func, const char *cas,
320 unsigned int proc_offset)
321 {
322 const var_t *arg;
323 int has_ret = !is_void(type_function_get_rettype(func->type));
324 int has_full_pointer = is_full_pointer_function(func);
325
326 if (is_interpreted_func( iface, func )) return;
327
328 indent = 0;
329 print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(func));
330 indent++;
331 print_proxy( "__DECL_EXCEPTION_FRAME\n" );
332 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
333 print_proxy( "%s * _This;\n", iface->name );
334 declare_stub_args( proxy, indent, func );
335 indent--;
336 print_proxy( "};\n\n" );
337
338 print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(func) );
339 print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(func) );
340 indent++;
341 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
342 if (has_full_pointer)
343 write_full_pointer_free(proxy, indent, func);
344 indent--;
345 print_proxy( "}\n\n" );
346
347 print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
348 indent++;
349 print_proxy( "IRpcStubBuffer* This,\n");
350 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
351 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
352 print_proxy( "DWORD* _pdwStubPhase)\n");
353 indent--;
354 print_proxy( "{\n");
355 indent++;
356 print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
357 iface->name, get_name(func) );
358
359 print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
360
361 /* FIXME: trace */
362
363 print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
364 fprintf(proxy, "\n");
365 print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(func) );
366
367 write_parameters_init(proxy, indent, func, "__frame->");
368
369 print_proxy("RpcTryFinally\n");
370 print_proxy("{\n");
371 indent++;
372 if (has_full_pointer)
373 write_full_pointer_init(proxy, indent, func, TRUE);
374 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
375 indent++;
376 print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
377 indent--;
378 fprintf(proxy, "\n");
379
380 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
381 fprintf(proxy, "\n");
382
383 assign_stub_out_args( proxy, indent, func, "__frame->" );
384
385 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
386 fprintf(proxy, "\n");
387 print_proxy( "%s", has_ret ? "__frame->_RetVal = " : "" );
388 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
389 else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(func));
390 fprintf(proxy, "(__frame->_This");
391
392 if (type_get_function_args(func->type))
393 {
394 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), const var_t, entry )
395 fprintf(proxy, ", %s__frame->%s", is_array(arg->type) && !type_array_is_decl_as_ptr(arg->type) ? "*" :"" , arg->name);
396 }
397 fprintf(proxy, ");\n");
398 fprintf(proxy, "\n");
399 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
400 fprintf(proxy, "\n");
401
402 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
403
404 if (!is_void(type_function_get_rettype(func->type)))
405 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
406
407 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
408
409 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
410 fprintf(proxy, "\n");
411
412 /* marshall the return value */
413 if (!is_void(type_function_get_rettype(func->type)))
414 write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
415
416 indent--;
417 print_proxy("}\n");
418 print_proxy("RpcFinally\n");
419 print_proxy("{\n");
420 indent++;
421 print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(func) );
422 indent--;
423 print_proxy("}\n");
424 print_proxy("RpcEndFinally\n");
425
426 print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
427 indent--;
428
429 print_proxy("}\n");
430 print_proxy("\n");
431 }
432
433 static void gen_stub_thunk( type_t *iface, const var_t *func, unsigned int proc_offset )
434 {
435 int has_ret = !is_void( type_function_get_rettype( func->type ));
436 const var_t *arg, *callas = is_callas( func->attrs );
437 const var_list_t *args = type_get_function_args( func->type );
438
439 indent = 0;
440 print_proxy( "void __RPC_API %s_%s_Thunk( PMIDL_STUB_MESSAGE pStubMsg )\n",
441 iface->name, get_name(func) );
442 print_proxy( "{\n");
443 indent++;
444 write_func_param_struct( proxy, iface, func->type,
445 "*pParamStruct = (struct _PARAM_STRUCT *)pStubMsg->StackTop", has_ret );
446 print_proxy( "%s%s_%s_Stub( pParamStruct->This",
447 has_ret ? "pParamStruct->_RetVal = " : "", iface->name, callas->name );
448 indent++;
449 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
450 {
451 fprintf( proxy, ",\n%*spParamStruct->%s", 4 * indent, "", arg->name );
452 }
453 fprintf( proxy, " );\n" );
454 indent--;
455 indent--;
456 print_proxy( "}\n\n");
457 }
458
459 int count_methods(const type_t *iface)
460 {
461 const statement_t *stmt;
462 int count = 0;
463
464 if (type_iface_get_inherit(iface))
465 count = count_methods(type_iface_get_inherit(iface));
466
467 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
468 const var_t *func = stmt->u.var;
469 if (!is_callas(func->attrs)) count++;
470 }
471 return count;
472 }
473
474 static const statement_t * get_callas_source(const type_t * iface, const var_t * def)
475 {
476 const statement_t * source;
477 STATEMENTS_FOR_EACH_FUNC( source, type_iface_get_stmts(iface)) {
478 const var_t * cas = is_callas(source->u.var->attrs );
479 if (cas && !strcmp(def->name, cas->name))
480 return source;
481 }
482 return NULL;
483 }
484
485 #ifdef __REACTOS__ /* r57019 / c3be8a3 */
486 static int write_proxy_procformatstring_offsets( const type_t *iface, int skip )
487 #else
488 static void write_proxy_procformatstring_offsets( const type_t *iface, int skip )
489 #endif
490 {
491 const statement_t *stmt;
492 int i = 0;
493
494 if (type_iface_get_inherit(iface))
495 i = write_proxy_procformatstring_offsets( type_iface_get_inherit(iface), need_delegation(iface));
496 else
497 return 0;
498
499 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
500 {
501 const var_t *func = stmt->u.var;
502 int missing = 0;
503
504 if (is_callas(func->attrs)) continue;
505 if (is_local(func->attrs))
506 {
507 const statement_t * callas_source = get_callas_source(iface, func);
508 if (!callas_source)
509 missing = 1;
510 else
511 func = callas_source->u.var;
512 }
513 if (skip || missing)
514 print_proxy( "(unsigned short)-1, /* %s::%s */\n", iface->name, get_name(func));
515 else
516 print_proxy( "%u, /* %s::%s */\n", func->procstring_offset, iface->name, get_name(func));
517 i++;
518 }
519 #ifdef __REACTOS__ /* r57019 / c3be8a3 */
520 return i;
521 #endif
522 }
523
524 static int write_proxy_methods(type_t *iface, int skip)
525 {
526 const statement_t *stmt;
527 int i = 0;
528
529 if (type_iface_get_inherit(iface))
530 i = write_proxy_methods(type_iface_get_inherit(iface),
531 need_delegation(iface));
532 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
533 const var_t *func = stmt->u.var;
534 if (!is_callas(func->attrs)) {
535 if (skip || (is_local(func->attrs) && !get_callas_source(iface, func)))
536 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func));
537 else if (is_interpreted_func( iface, func ) &&
538 !is_local( func->attrs ) &&
539 type_iface_get_inherit(iface))
540 print_proxy( "(void *)-1, /* %s::%s */\n", iface->name, get_name(func));
541 else
542 print_proxy( "%s_%s_Proxy,\n", iface->name, get_name(func));
543 i++;
544 }
545 }
546 return i;
547 }
548
549 static int write_stub_methods(type_t *iface, int skip)
550 {
551 const statement_t *stmt;
552 int i = 0;
553
554 if (type_iface_get_inherit(iface))
555 i = write_stub_methods(type_iface_get_inherit(iface), need_delegation(iface));
556 else
557 return i; /* skip IUnknown */
558
559 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
560 const var_t *func = stmt->u.var;
561 if (!is_callas(func->attrs)) {
562 int missing = 0;
563 const char * fname = get_name(func);
564 if(is_local(func->attrs)) {
565 const statement_t * callas_source = get_callas_source(iface, func);
566 if(!callas_source)
567 missing = 1;
568 else
569 fname = get_name(callas_source->u.var);
570 }
571 if (i) fprintf(proxy,",\n");
572 if (skip || missing) print_proxy("STUB_FORWARDING_FUNCTION");
573 else if (is_interpreted_func( iface, func ))
574 print_proxy( "(PRPC_STUB_FUNCTION)%s", get_stub_mode() == MODE_Oif ? "NdrStubCall2" : "NdrStubCall" );
575 else print_proxy( "%s_%s_Stub", iface->name, fname);
576 i++;
577 }
578 }
579 return i;
580 }
581
582 static void write_thunk_methods( type_t *iface, int skip )
583 {
584 const statement_t *stmt;
585
586 if (type_iface_get_inherit( iface ))
587 write_thunk_methods( type_iface_get_inherit(iface), need_delegation(iface) );
588 else
589 return; /* skip IUnknown */
590
591 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
592 {
593 var_t *func = stmt->u.var;
594 const statement_t * callas_source = NULL;
595
596 if (is_callas(func->attrs)) continue;
597 if (is_local(func->attrs)) callas_source = get_callas_source(iface, func);
598
599 if (!skip && callas_source && is_interpreted_func( iface, func ))
600 print_proxy( "%s_%s_Thunk,\n", iface->name, get_name(callas_source->u.var) );
601 else
602 print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func) );
603 }
604 }
605
606 static void write_proxy(type_t *iface, unsigned int *proc_offset)
607 {
608 int count;
609 const statement_t *stmt;
610 int first_func = 1;
611 int needs_stub_thunks = 0;
612 int needs_inline_stubs = need_inline_stubs( iface ) || need_delegation( iface );
613
614 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
615 var_t *func = stmt->u.var;
616 if (first_func) {
617 fprintf(proxy, "/*****************************************************************************\n");
618 fprintf(proxy, " * %s interface\n", iface->name);
619 fprintf(proxy, " */\n");
620 first_func = 0;
621 }
622 if (!is_local(func->attrs)) {
623 const var_t *cas = is_callas(func->attrs);
624 const char *cname = cas ? cas->name : NULL;
625 int idx = func->type->details.function->idx;
626 if (cname) {
627 const statement_t *stmt2;
628 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface)) {
629 const var_t *m = stmt2->u.var;
630 if (!strcmp(m->name, cname))
631 {
632 idx = m->type->details.function->idx;
633 break;
634 }
635 }
636 }
637 func->procstring_offset = *proc_offset;
638 gen_proxy(iface, func, idx, *proc_offset);
639 gen_stub(iface, func, cname, *proc_offset);
640 if (cas && is_interpreted_func( iface, func ))
641 {
642 needs_stub_thunks = 1;
643 gen_stub_thunk(iface, func, *proc_offset);
644 }
645 *proc_offset += get_size_procformatstring_func( iface, func );
646 }
647 }
648
649 count = count_methods(iface);
650
651 print_proxy( "static const unsigned short %s_FormatStringOffsetTable[] =\n", iface->name );
652 print_proxy( "{\n" );
653 indent++;
654 #ifdef __REACTOS__ /* r57019 / c3be8a3 */
655 if (write_proxy_procformatstring_offsets( iface, 0 ) == 0)
656 {
657 print_proxy( "0\n" );
658 }
659 #else
660 write_proxy_procformatstring_offsets( iface, 0 );
661 #endif
662 indent--;
663 print_proxy( "};\n\n" );
664
665 /* proxy info */
666 if (get_stub_mode() == MODE_Oif)
667 {
668 print_proxy( "static const MIDL_STUBLESS_PROXY_INFO %s_ProxyInfo =\n", iface->name );
669 print_proxy( "{\n" );
670 indent++;
671 print_proxy( "&Object_StubDesc,\n" );
672 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
673 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
674 print_proxy( "0,\n" );
675 print_proxy( "0,\n" );
676 print_proxy( "0\n" );
677 indent--;
678 print_proxy( "};\n\n" );
679 }
680
681 /* proxy vtable */
682 print_proxy( "static %sCINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n",
683 need_delegation_indirect(iface) ? "" : "const ", count, iface->name);
684 print_proxy( "{\n");
685 indent++;
686 print_proxy( "{\n");
687 indent++;
688 if (get_stub_mode() == MODE_Oif) print_proxy( "&%s_ProxyInfo,\n", iface->name );
689 print_proxy( "&IID_%s,\n", iface->name);
690 indent--;
691 print_proxy( "},\n");
692 print_proxy( "{\n");
693 indent++;
694 write_proxy_methods(iface, FALSE);
695 indent--;
696 print_proxy( "}\n");
697 indent--;
698 print_proxy( "};\n\n");
699
700 /* stub thunk table */
701 if (needs_stub_thunks)
702 {
703 print_proxy( "static const STUB_THUNK %s_StubThunkTable[] =\n", iface->name);
704 print_proxy( "{\n");
705 indent++;
706 write_thunk_methods( iface, 0 );
707 indent--;
708 print_proxy( "};\n\n");
709 }
710
711 /* server info */
712 print_proxy( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
713 print_proxy( "{\n" );
714 indent++;
715 print_proxy( "&Object_StubDesc,\n" );
716 print_proxy( "0,\n" );
717 print_proxy( "__MIDL_ProcFormatString.Format,\n" );
718 print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
719 if (needs_stub_thunks)
720 print_proxy( "&%s_StubThunkTable[-3],\n", iface->name );
721 else
722 print_proxy( "0,\n" );
723 print_proxy( "0,\n" );
724 print_proxy( "0,\n" );
725 print_proxy( "0\n" );
726 indent--;
727 print_proxy( "};\n\n" );
728
729 /* stub vtable */
730 if (needs_inline_stubs)
731 {
732 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
733 print_proxy( "{\n");
734 indent++;
735 #ifdef __REACTOS__ /* r57019 / c3be8a3 */
736 if (write_stub_methods(iface, FALSE) == 0)
737 {
738 fprintf(proxy, "0");
739 }
740 #else
741 write_stub_methods(iface, FALSE);
742 #endif
743 fprintf(proxy, "\n");
744 indent--;
745 fprintf(proxy, "};\n\n");
746 }
747 print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
748 need_delegation_indirect(iface) ? "" : "const ", iface->name);
749 print_proxy( "{\n");
750 indent++;
751 print_proxy( "{\n");
752 indent++;
753 print_proxy( "&IID_%s,\n", iface->name);
754 print_proxy( "&%s_ServerInfo,\n", iface->name );
755 print_proxy( "%d,\n", count);
756 if (needs_inline_stubs) print_proxy( "&%s_table[-3]\n", iface->name );
757 else print_proxy( "0\n" );
758 indent--;
759 print_proxy( "},\n");
760 print_proxy( "{\n");
761 indent++;
762 print_proxy( "CStdStubBuffer_%s\n", need_delegation_indirect(iface) ? "DELEGATING_METHODS" : "METHODS");
763 indent--;
764 print_proxy( "}\n");
765 indent--;
766 print_proxy( "};\n");
767 print_proxy( "\n");
768 }
769
770 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
771 {
772 const statement_t *stmt;
773
774 if (stmts)
775 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
776 {
777 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
778 {
779 if (pred(stmt->u.type))
780 return TRUE;
781 }
782 }
783
784 return FALSE;
785 }
786
787 int need_proxy(const type_t *iface)
788 {
789 if (!is_object( iface )) return 0;
790 if (is_local( iface->attrs )) return 0;
791 if (is_attr( iface->attrs, ATTR_DISPINTERFACE )) return 0;
792 return 1;
793 }
794
795 int need_stub(const type_t *iface)
796 {
797 return !is_object(iface) && !is_local(iface->attrs);
798 }
799
800 int need_proxy_file(const statement_list_t *stmts)
801 {
802 return does_any_iface(stmts, need_proxy);
803 }
804
805 int need_proxy_delegation(const statement_list_t *stmts)
806 {
807 return does_any_iface(stmts, need_delegation);
808 }
809
810 int need_inline_stubs(const type_t *iface)
811 {
812 const statement_t *stmt;
813
814 if (get_stub_mode() == MODE_Os) return 1;
815
816 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
817 {
818 const var_t *func = stmt->u.var;
819 if (is_local( func->attrs )) continue;
820 if (!is_interpreted_func( iface, func )) return 1;
821 }
822 return 0;
823 }
824
825 static int need_proxy_and_inline_stubs(const type_t *iface)
826 {
827 const statement_t *stmt;
828
829 if (!need_proxy( iface )) return 0;
830 if (get_stub_mode() == MODE_Os) return 1;
831
832 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
833 {
834 const var_t *func = stmt->u.var;
835 if (is_local( func->attrs )) continue;
836 if (!is_interpreted_func( iface, func )) return 1;
837 }
838 return 0;
839 }
840
841 int need_stub_files(const statement_list_t *stmts)
842 {
843 return does_any_iface(stmts, need_stub);
844 }
845
846 int need_inline_stubs_file(const statement_list_t *stmts)
847 {
848 return does_any_iface(stmts, need_inline_stubs);
849 }
850
851 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
852 {
853 const statement_t *stmt;
854 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
855 {
856 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
857 {
858 if (need_proxy(stmt->u.type))
859 write_proxy(stmt->u.type, proc_offset);
860 }
861 }
862 }
863
864 static int cmp_iid( const void *ptr1, const void *ptr2 )
865 {
866 const type_t * const *iface1 = ptr1;
867 const type_t * const *iface2 = ptr2;
868 const UUID *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
869 const UUID *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
870 return memcmp( uuid1, uuid2, sizeof(UUID) );
871 }
872
873 static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
874 {
875 const statement_t *stmt;
876
877 if (!stmts) return;
878 LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
879 {
880 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
881 {
882 type_t *iface = stmt->u.type;
883 if (type_iface_get_inherit(iface) && need_proxy(iface))
884 {
885 *ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
886 (*ifaces)[(*count)++] = iface;
887 }
888 }
889 }
890 }
891
892 static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
893 {
894 type_t **ifaces = NULL;
895
896 *count = 0;
897 build_iface_list( stmts, &ifaces, count );
898 qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
899 return ifaces;
900 }
901
902 static void write_proxy_routines(const statement_list_t *stmts)
903 {
904 int expr_eval_routines;
905 unsigned int proc_offset = 0;
906 char *file_id = proxy_token;
907 int i, count, have_baseiid = 0;
908 type_t **interfaces;
909 const type_t * delegate_to;
910
911 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
912 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ %u\n", get_stub_mode() == MODE_Oif ? 475 : 440);
913 print_proxy( "#endif\n");
914 print_proxy( "\n");
915 if (get_stub_mode() == MODE_Oif) print_proxy( "#define USE_STUBLESS_PROXY\n");
916 print_proxy( "#include \"rpcproxy.h\"\n");
917 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
918 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
919 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
920 print_proxy( "\n");
921 print_proxy( "#include \"%s\"\n", header_name);
922 print_proxy( "\n");
923
924 if (does_any_iface(stmts, need_proxy_and_inline_stubs))
925 {
926 write_exceptions( proxy );
927 print_proxy( "\n");
928 print_proxy( "struct __proxy_frame\n");
929 print_proxy( "{\n");
930 print_proxy( " __DECL_EXCEPTION_FRAME\n");
931 print_proxy( " MIDL_STUB_MESSAGE _StubMsg;\n");
932 print_proxy( " void *This;\n");
933 print_proxy( "};\n");
934 print_proxy( "\n");
935 print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
936 print_proxy( "{\n");
937 print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
938 print_proxy( "}\n");
939 print_proxy( "\n");
940 }
941
942 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
943 write_stubdescproto();
944 write_proxy_stmts(stmts, &proc_offset);
945
946 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
947 if (expr_eval_routines)
948 write_expr_eval_routine_list(proxy, proxy_token);
949 write_user_quad_list(proxy);
950 write_stubdesc(expr_eval_routines);
951
952 print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
953 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
954 print_proxy( "#endif\n");
955 print_proxy( "\n");
956 write_procformatstring(proxy, stmts, need_proxy);
957 write_typeformatstring(proxy, stmts, need_proxy);
958
959 interfaces = sort_interfaces(stmts, &count);
960 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
961 fprintf(proxy, "{\n");
962 for (i = 0; i < count; i++)
963 fprintf(proxy, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
964 fprintf(proxy, " 0\n");
965 fprintf(proxy, "};\n");
966 fprintf(proxy, "\n");
967
968 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
969 fprintf(proxy, "{\n");
970 for (i = 0; i < count; i++)
971 fprintf(proxy, " &_%sStubVtbl,\n", interfaces[i]->name);
972 fprintf(proxy, " 0\n");
973 fprintf(proxy, "};\n");
974 fprintf(proxy, "\n");
975
976 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
977 fprintf(proxy, "{\n");
978 for (i = 0; i < count; i++)
979 fprintf(proxy, " \"%s\",\n", interfaces[i]->name);
980 fprintf(proxy, " 0\n");
981 fprintf(proxy, "};\n");
982 fprintf(proxy, "\n");
983
984 for (i = 0; i < count; i++)
985 if ((have_baseiid = get_delegation_indirect( interfaces[i], NULL ))) break;
986
987 if (have_baseiid)
988 {
989 fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
990 fprintf(proxy, "{\n");
991 for (i = 0; i < count; i++)
992 {
993 if (get_delegation_indirect(interfaces[i], &delegate_to))
994 fprintf( proxy, " &IID_%s, /* %s */\n", delegate_to->name, interfaces[i]->name );
995 else
996 fprintf( proxy, " 0,\n" );
997 }
998 fprintf(proxy, " 0\n");
999 fprintf(proxy, "};\n");
1000 fprintf(proxy, "\n");
1001 }
1002
1003 fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
1004 fprintf(proxy, "{\n");
1005 fprintf(proxy, " int low = 0, high = %d;\n", count - 1);
1006 fprintf(proxy, "\n");
1007 fprintf(proxy, " while (low <= high)\n");
1008 fprintf(proxy, " {\n");
1009 fprintf(proxy, " int pos = (low + high) / 2;\n");
1010 fprintf(proxy, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
1011 fprintf(proxy, " if (!res) { *pIndex = pos; return 1; }\n");
1012 fprintf(proxy, " if (res > 0) low = pos + 1;\n");
1013 fprintf(proxy, " else high = pos - 1;\n");
1014 fprintf(proxy, " }\n");
1015 fprintf(proxy, " return 0;\n");
1016 fprintf(proxy, "}\n");
1017 fprintf(proxy, "\n");
1018
1019 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo DECLSPEC_HIDDEN =\n", file_id);
1020 fprintf(proxy, "{\n");
1021 fprintf(proxy, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
1022 fprintf(proxy, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
1023 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
1024 if (have_baseiid) fprintf(proxy, " _%s_BaseIIDList,\n", file_id);
1025 else fprintf(proxy, " 0,\n");
1026 fprintf(proxy, " _%s_IID_Lookup,\n", file_id);
1027 fprintf(proxy, " %d,\n", count);
1028 fprintf(proxy, " %d,\n", get_stub_mode() == MODE_Oif ? 2 : 1);
1029 fprintf(proxy, " 0,\n");
1030 fprintf(proxy, " 0,\n");
1031 fprintf(proxy, " 0,\n");
1032 fprintf(proxy, " 0\n");
1033 fprintf(proxy, "};\n");
1034 }
1035
1036 void write_proxies(const statement_list_t *stmts)
1037 {
1038 if (!do_proxies) return;
1039 if (do_everything && !need_proxy_file(stmts)) return;
1040
1041 init_proxy(stmts);
1042 if(!proxy) return;
1043
1044 if (do_win32 && do_win64)
1045 {
1046 fprintf(proxy, "\n#ifndef _WIN64\n\n");
1047 pointer_size = 4;
1048 write_proxy_routines( stmts );
1049 fprintf(proxy, "\n#else /* _WIN64 */\n\n");
1050 pointer_size = 8;
1051 write_proxy_routines( stmts );
1052 fprintf(proxy, "\n#endif /* _WIN64 */\n");
1053 }
1054 else if (do_win32)
1055 {
1056 pointer_size = 4;
1057 write_proxy_routines( stmts );
1058 }
1059 else if (do_win64)
1060 {
1061 pointer_size = 8;
1062 write_proxy_routines( stmts );
1063 }
1064
1065 fclose(proxy);
1066 }