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