Sync to wine-0.9.61:
[reactos.git] / reactos / 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 #define END_OF_LIST(list) \
41 do { \
42 if (list) { \
43 while (NEXT_LINK(list)) \
44 list = NEXT_LINK(list); \
45 } \
46 } while(0)
47
48 static FILE* proxy;
49 static int indent = 0;
50
51 /* FIXME: support generation of stubless proxies */
52
53 static void print_proxy( const char *format, ... )
54 {
55 va_list va;
56 va_start( va, format );
57 print( proxy, indent, format, va );
58 va_end( va );
59 }
60
61 static void write_stubdescproto(void)
62 {
63 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
64 print_proxy( "\n");
65 }
66
67 static void write_stubdesc(int expr_eval_routines)
68 {
69 print_proxy( "static const MIDL_STUB_DESC Object_StubDesc =\n{\n");
70 indent++;
71 print_proxy( "0,\n");
72 print_proxy( "NdrOleAllocate,\n");
73 print_proxy( "NdrOleFree,\n");
74 print_proxy( "{0}, 0, 0, %s, 0,\n", expr_eval_routines ? "ExprEvalRoutines" : "0");
75 print_proxy( "__MIDL_TypeFormatString.Format,\n");
76 print_proxy( "1, /* -error bounds_check flag */\n");
77 print_proxy( "0x10001, /* Ndr library version */\n");
78 print_proxy( "0,\n");
79 print_proxy( "0x50100a4, /* MIDL Version 5.1.164 */\n");
80 print_proxy( "0,\n");
81 print_proxy("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
82 print_proxy( "0, /* notify & notify_flag routine table */\n");
83 print_proxy( "1, /* Flags */\n");
84 print_proxy( "0, /* Reserved3 */\n");
85 print_proxy( "0, /* Reserved4 */\n");
86 print_proxy( "0 /* Reserved5 */\n");
87 indent--;
88 print_proxy( "};\n");
89 print_proxy( "\n");
90 }
91
92 static void init_proxy(const statement_list_t *stmts)
93 {
94 if (proxy) return;
95 if(!(proxy = fopen(proxy_name, "w")))
96 error("Could not open %s for output\n", proxy_name);
97 print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
98 print_proxy( "\n");
99 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
100 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ 440\n");
101 print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
102 print_proxy( "\n");
103 print_proxy( "#define __midl_proxy\n");
104 print_proxy( "#include \"objbase.h\"\n");
105 print_proxy( "#include \"rpcproxy.h\"\n");
106 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
107 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
108 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
109 print_proxy( "\n");
110 print_proxy( "#include \"%s\"\n", header_name);
111 print_proxy( "\n");
112 write_formatstringsdecl(proxy, indent, stmts, need_proxy);
113 write_stubdescproto();
114 }
115
116 static void clear_output_vars( const var_list_t *args )
117 {
118 const var_t *arg;
119
120 if (!args) return;
121 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
122 {
123 if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
124 print_proxy( "if(%s)\n", arg->name );
125 indent++;
126 print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name );
127 indent--;
128 }
129 }
130 }
131
132 int is_var_ptr(const var_t *v)
133 {
134 return is_ptr(v->type);
135 }
136
137 int cant_be_null(const var_t *v)
138 {
139 /* Search backwards for the most recent pointer attribute. */
140 const attr_list_t *attrs = v->attrs;
141 const type_t *type = v->type;
142
143 if (! attrs && type)
144 {
145 attrs = type->attrs;
146 type = type->ref;
147 }
148
149 while (attrs)
150 {
151 int t = get_attrv(attrs, ATTR_POINTERTYPE);
152
153 if (t == RPC_FC_FP || t == RPC_FC_OP || t == RPC_FC_UP)
154 return 0;
155
156 if (t == RPC_FC_RP)
157 return 1;
158
159 if (type)
160 {
161 attrs = type->attrs;
162 type = type->ref;
163 }
164 else
165 attrs = NULL;
166 }
167
168 return 1; /* Default is RPC_FC_RP. */
169 }
170
171 static void proxy_check_pointers( const var_list_t *args )
172 {
173 const var_t *arg;
174
175 if (!args) return;
176 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
177 {
178 if (is_var_ptr(arg) && cant_be_null(arg)) {
179 print_proxy( "if(!%s)\n", arg->name );
180 indent++;
181 print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
182 indent--;
183 }
184 }
185 }
186
187 static void free_variable( const var_t *arg )
188 {
189 unsigned int type_offset = arg->type->typestring_offset;
190 expr_t *iid;
191 type_t *type = arg->type;
192 expr_t *size = get_size_is_expr(type, arg->name);
193
194 if (size)
195 {
196 print_proxy( "_StubMsg.MaxCount = " );
197 write_expr(proxy, size, 0, 1, NULL, NULL);
198 fprintf(proxy, ";\n\n");
199 print_proxy( "NdrClearOutParameters( &_StubMsg, ");
200 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
201 fprintf(proxy, "(void*)%s );\n", arg->name );
202 return;
203 }
204
205 switch( type->type )
206 {
207 case RPC_FC_BYTE:
208 case RPC_FC_CHAR:
209 case RPC_FC_WCHAR:
210 case RPC_FC_SHORT:
211 case RPC_FC_USHORT:
212 case RPC_FC_ENUM16:
213 case RPC_FC_LONG:
214 case RPC_FC_ULONG:
215 case RPC_FC_ENUM32:
216 case RPC_FC_STRUCT:
217 break;
218
219 case RPC_FC_FP:
220 case RPC_FC_IP:
221 iid = get_attrp( arg->attrs, ATTR_IIDIS );
222 if( iid )
223 {
224 print_proxy( "_StubMsg.MaxCount = (unsigned long) " );
225 write_expr(proxy, iid, 1, 1, NULL, NULL);
226 print_proxy( ";\n\n" );
227 }
228 print_proxy( "NdrClearOutParameters( &_StubMsg, ");
229 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
230 fprintf(proxy, "(void*)%s );\n", arg->name );
231 break;
232
233 default:
234 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
235 }
236 }
237
238 static void proxy_free_variables( var_list_t *args )
239 {
240 const var_t *arg;
241
242 if (!args) return;
243 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
244 if (is_attr(arg->attrs, ATTR_OUT))
245 {
246 free_variable( arg );
247 fprintf(proxy, "\n");
248 }
249 }
250
251 static void gen_proxy(type_t *iface, const func_t *cur, int idx,
252 unsigned int proc_offset)
253 {
254 var_t *def = cur->def;
255 int has_ret = !is_void(get_func_return_type(cur));
256 int has_full_pointer = is_full_pointer_function(cur);
257 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
258 if (!callconv) callconv = "";
259
260 indent = 0;
261 write_type_decl_left(proxy, get_func_return_type(cur));
262 print_proxy( " %s %s_", callconv, iface->name);
263 write_name(proxy, def);
264 print_proxy( "_Proxy(\n");
265 write_args(proxy, cur->args, iface->name, 1, TRUE);
266 print_proxy( ")\n");
267 print_proxy( "{\n");
268 indent ++;
269 /* local variables */
270 if (has_ret) {
271 print_proxy( "" );
272 write_type_decl_left(proxy, get_func_return_type(cur));
273
274 /* Initialize _RetVal properly in order to avoid compiler warnings */
275 if (is_ptr(get_func_return_type(cur)) || is_array(get_func_return_type(cur)))
276 print_proxy(" _RetVal = NULL;\n");
277 else
278 print_proxy(" _RetVal = 0;\n");
279 }
280 print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
281 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n" );
282 if (has_ret) {
283 if (decl_indirect(get_func_return_type(cur)))
284 print_proxy("void *_p_%s = &%s;\n",
285 "_RetVal", "_RetVal");
286 }
287 print_proxy( "\n");
288
289 if (has_full_pointer)
290 write_full_pointer_init(proxy, indent, cur, FALSE);
291
292 /* FIXME: trace */
293 clear_output_vars( cur->args );
294
295 print_proxy( "RpcTryExcept\n" );
296 print_proxy( "{\n" );
297 indent++;
298 print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &_StubMsg, &Object_StubDesc, %d);\n", idx);
299 proxy_check_pointers( cur->args );
300
301 print_proxy( "RpcTryFinally\n" );
302 print_proxy( "{\n" );
303 indent++;
304
305 write_remoting_arguments(proxy, indent, cur, PASS_IN, PHASE_BUFFERSIZE);
306
307 print_proxy( "NdrProxyGetBuffer(This, &_StubMsg);\n" );
308
309 write_remoting_arguments(proxy, indent, cur, PASS_IN, PHASE_MARSHAL);
310
311 print_proxy( "NdrProxySendReceive(This, &_StubMsg);\n" );
312 fprintf(proxy, "\n");
313 print_proxy( "_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
314 print_proxy( "_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
315
316 print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
317 indent++;
318 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
319 indent--;
320 fprintf(proxy, "\n");
321
322 write_remoting_arguments(proxy, indent, cur, PASS_OUT, PHASE_UNMARSHAL);
323
324 if (has_ret)
325 {
326 if (decl_indirect(get_func_return_type(cur)))
327 print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
328 else if (is_ptr(get_func_return_type(cur)) || is_array(get_func_return_type(cur)))
329 print_proxy("%s = 0;\n", "_RetVal");
330 write_remoting_arguments(proxy, indent, cur, PASS_RETURN, PHASE_UNMARSHAL);
331 }
332
333 indent--;
334 print_proxy( "}\n");
335 print_proxy( "RpcFinally\n" );
336 print_proxy( "{\n" );
337 indent++;
338 if (has_full_pointer)
339 write_full_pointer_free(proxy, indent, cur);
340 print_proxy( "NdrProxyFreeBuffer(This, &_StubMsg);\n" );
341 indent--;
342 print_proxy( "}\n");
343 print_proxy( "RpcEndFinally\n" );
344 indent--;
345 print_proxy( "}\n" );
346 print_proxy( "RpcExcept(_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
347 print_proxy( "{\n" );
348 if (has_ret) {
349 indent++;
350 proxy_free_variables( cur->args );
351 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
352 indent--;
353 }
354 print_proxy( "}\n" );
355 print_proxy( "RpcEndExcept\n" );
356
357 if (has_ret) {
358 print_proxy( "return _RetVal;\n" );
359 }
360 indent--;
361 print_proxy( "}\n");
362 print_proxy( "\n");
363 }
364
365 static void gen_stub(type_t *iface, const func_t *cur, const char *cas,
366 unsigned int proc_offset)
367 {
368 var_t *def = cur->def;
369 const var_t *arg;
370 int has_ret = !is_void(get_func_return_type(cur));
371 int has_full_pointer = is_full_pointer_function(cur);
372
373 indent = 0;
374 print_proxy( "void __RPC_STUB %s_", iface->name);
375 write_name(proxy, def);
376 print_proxy( "_Stub(\n");
377 indent++;
378 print_proxy( "IRpcStubBuffer* This,\n");
379 print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
380 print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
381 print_proxy( "DWORD* _pdwStubPhase)\n");
382 indent--;
383 print_proxy( "{\n");
384 indent++;
385 print_proxy("%s * _This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n", iface->name, iface->name);
386 print_proxy("MIDL_STUB_MESSAGE _StubMsg;\n");
387 declare_stub_args( proxy, indent, cur );
388 fprintf(proxy, "\n");
389
390 /* FIXME: trace */
391
392 print_proxy("NdrStubInitialize(_pRpcMessage, &_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
393 fprintf(proxy, "\n");
394
395 write_parameters_init(proxy, indent, cur);
396
397 print_proxy("RpcTryFinally\n");
398 print_proxy("{\n");
399 indent++;
400 if (has_full_pointer)
401 write_full_pointer_init(proxy, indent, cur, TRUE);
402 print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
403 indent++;
404 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
405 indent--;
406 fprintf(proxy, "\n");
407
408 write_remoting_arguments(proxy, indent, cur, PASS_IN, PHASE_UNMARSHAL);
409 fprintf(proxy, "\n");
410
411 assign_stub_out_args( proxy, indent, cur );
412
413 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
414 fprintf(proxy, "\n");
415 print_proxy("");
416 if (has_ret) fprintf(proxy, "_RetVal = ");
417 if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
418 else
419 {
420 fprintf(proxy, "_This->lpVtbl->");
421 write_name(proxy, def);
422 }
423 fprintf(proxy, "(_This");
424
425 if (cur->args)
426 {
427 LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
428 {
429 fprintf(proxy, ", ");
430 if (arg->type->declarray)
431 fprintf(proxy, "*");
432 write_name(proxy, arg);
433 }
434 }
435 fprintf(proxy, ");\n");
436 fprintf(proxy, "\n");
437 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
438 fprintf(proxy, "\n");
439
440 write_remoting_arguments(proxy, indent, cur, PASS_OUT, PHASE_BUFFERSIZE);
441
442 if (!is_void(get_func_return_type(cur)))
443 write_remoting_arguments(proxy, indent, cur, PASS_RETURN, PHASE_BUFFERSIZE);
444
445 print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &_StubMsg);\n");
446
447 write_remoting_arguments(proxy, indent, cur, PASS_OUT, PHASE_MARSHAL);
448 fprintf(proxy, "\n");
449
450 /* marshall the return value */
451 if (!is_void(get_func_return_type(cur)))
452 write_remoting_arguments(proxy, indent, cur, PASS_RETURN, PHASE_MARSHAL);
453
454 indent--;
455 print_proxy("}\n");
456 print_proxy("RpcFinally\n");
457 print_proxy("{\n");
458
459 write_remoting_arguments(proxy, indent+1, cur, PASS_OUT, PHASE_FREE);
460
461 if (has_full_pointer)
462 write_full_pointer_free(proxy, indent, cur);
463
464 print_proxy("}\n");
465 print_proxy("RpcEndFinally\n");
466
467 print_proxy("_pRpcMessage->BufferLength = _StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
468 indent--;
469
470 print_proxy("}\n");
471 print_proxy("\n");
472 }
473
474 static int write_proxy_methods(type_t *iface, int skip)
475 {
476 const func_t *cur;
477 int i = 0;
478
479 if (iface->ref) i = write_proxy_methods(iface->ref, iface->ref->ref != NULL);
480 if (iface->funcs) LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry ) {
481 var_t *def = cur->def;
482 if (!is_callas(def->attrs)) {
483 if (i) fprintf(proxy, ",\n");
484 print_proxy( "%s%s_", skip ? "0\t/* " : "", iface->name);
485 write_name(proxy, def);
486 fprintf(proxy, "_Proxy%s", skip ? " */" : "");
487 i++;
488 }
489 }
490 return i;
491 }
492
493 static int write_stub_methods(type_t *iface, int skip)
494 {
495 const func_t *cur;
496 int i = 0;
497
498 if (iface->ref) i = write_stub_methods(iface->ref, TRUE);
499 else return i; /* skip IUnknown */
500
501 if (iface->funcs) LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry ) {
502 var_t *def = cur->def;
503 if (!is_local(def->attrs)) {
504 if (skip)
505 print_proxy("STUB_FORWARDING_FUNCTION,\n");
506 else {
507 if (i) fprintf(proxy,",\n");
508 print_proxy( "%s_", iface->name);
509 write_name(proxy, def);
510 fprintf(proxy, "_Stub");
511 i++;
512 }
513 }
514 }
515 return i;
516 }
517
518 static void write_proxy(type_t *iface, unsigned int *proc_offset)
519 {
520 int midx = -1, stubs;
521 const func_t *cur;
522
523 if (!iface->funcs) return;
524
525 /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
526
527 fprintf(proxy, "/*****************************************************************************\n");
528 fprintf(proxy, " * %s interface\n", iface->name);
529 fprintf(proxy, " */\n");
530 LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
531 {
532 const var_t *def = cur->def;
533 if (!is_local(def->attrs)) {
534 const var_t *cas = is_callas(def->attrs);
535 const char *cname = cas ? cas->name : NULL;
536 int idx = cur->idx;
537 if (cname) {
538 const func_t *m;
539 LIST_FOR_EACH_ENTRY( m, iface->funcs, const func_t, entry )
540 if (!strcmp(m->def->name, cname))
541 {
542 idx = m->idx;
543 break;
544 }
545 }
546 gen_proxy(iface, cur, idx, *proc_offset);
547 gen_stub(iface, cur, cname, *proc_offset);
548 *proc_offset += get_size_procformatstring_func( cur );
549 if (midx == -1) midx = idx;
550 else if (midx != idx) error("method index mismatch in write_proxy\n");
551 midx++;
552 }
553 }
554
555 /* proxy vtable */
556 print_proxy( "static const CINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n", midx, iface->name);
557 print_proxy( "{\n");
558 indent++;
559 print_proxy( "{\n", iface->name);
560 indent++;
561 print_proxy( "&IID_%s,\n", iface->name);
562 indent--;
563 print_proxy( "},\n");
564 print_proxy( "{\n");
565 indent++;
566 write_proxy_methods(iface, FALSE);
567 fprintf(proxy, "\n");
568 indent--;
569 print_proxy( "}\n");
570 indent--;
571 print_proxy( "};\n");
572 fprintf(proxy, "\n\n");
573
574 /* stub vtable */
575 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
576 print_proxy( "{\n");
577 indent++;
578 stubs = write_stub_methods(iface, FALSE);
579 fprintf(proxy, "\n");
580 indent--;
581 fprintf(proxy, "};\n");
582 print_proxy( "\n");
583 print_proxy( "static const CInterfaceStubVtbl _%sStubVtbl =\n", iface->name);
584 print_proxy( "{\n");
585 indent++;
586 print_proxy( "{\n");
587 indent++;
588 print_proxy( "&IID_%s,\n", iface->name);
589 print_proxy( "0,\n");
590 print_proxy( "%d,\n", stubs+3);
591 print_proxy( "&%s_table[-3],\n", iface->name);
592 indent--;
593 print_proxy( "},\n", iface->name);
594 print_proxy( "{\n");
595 indent++;
596 print_proxy( "CStdStubBuffer_METHODS\n");
597 indent--;
598 print_proxy( "}\n");
599 indent--;
600 print_proxy( "};\n");
601 print_proxy( "\n");
602 }
603
604 static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
605 {
606 const statement_t *stmt;
607
608 if (stmts)
609 LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
610 {
611 if (stmt->type == STMT_LIBRARY)
612 {
613 if (does_any_iface(stmt->u.lib->stmts, pred))
614 return TRUE;
615 }
616 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
617 {
618 if (pred(stmt->u.type))
619 return TRUE;
620 }
621 }
622
623 return FALSE;
624 }
625
626 int need_proxy(const type_t *iface)
627 {
628 return is_object(iface->attrs) && !is_local(iface->attrs);
629 }
630
631 int need_stub(const type_t *iface)
632 {
633 return !is_object(iface->attrs) && !is_local(iface->attrs);
634 }
635
636 int need_proxy_file(const statement_list_t *stmts)
637 {
638 return does_any_iface(stmts, need_proxy);
639 }
640
641 int need_stub_files(const statement_list_t *stmts)
642 {
643 return does_any_iface(stmts, need_stub);
644 }
645
646 static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
647 {
648 const statement_t *stmt;
649 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
650 {
651 if (stmt->type == STMT_LIBRARY)
652 write_proxy_stmts(stmt->u.lib->stmts, proc_offset);
653 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
654 {
655 if (need_proxy(stmt->u.type))
656 write_proxy(stmt->u.type, proc_offset);
657 }
658 }
659 }
660
661 static void write_proxy_iface_name_format(const statement_list_t *stmts, const char *format)
662 {
663 const statement_t *stmt;
664 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
665 {
666 if (stmt->type == STMT_LIBRARY)
667 write_proxy_iface_name_format(stmt->u.lib->stmts, format);
668 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
669 {
670 type_t *iface = stmt->u.type;
671 if (iface->ref && iface->funcs && need_proxy(iface))
672 fprintf(proxy, format, iface->name);
673 }
674 }
675 }
676
677 static void write_iid_lookup(const statement_list_t *stmts, const char *file_id, int *c)
678 {
679 const statement_t *stmt;
680 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
681 {
682 if (stmt->type == STMT_LIBRARY)
683 write_iid_lookup(stmt->u.lib->stmts, file_id, c);
684 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
685 {
686 type_t *iface = stmt->u.type;
687 if(iface->ref && iface->funcs && need_proxy(iface))
688 {
689 fprintf(proxy, " if (!_%s_CHECK_IID(%d))\n", file_id, *c);
690 fprintf(proxy, " {\n");
691 fprintf(proxy, " *pIndex = %d;\n", *c);
692 fprintf(proxy, " return 1;\n");
693 fprintf(proxy, " }\n");
694 (*c)++;
695 }
696 }
697 }
698 }
699
700 void write_proxies(const statement_list_t *stmts)
701 {
702 int expr_eval_routines;
703 char *file_id = proxy_token;
704 int c;
705 unsigned int proc_offset = 0;
706
707 if (!do_proxies) return;
708 if (do_everything && !need_proxy_file(stmts)) return;
709
710 init_proxy(stmts);
711 if(!proxy) return;
712
713 write_proxy_stmts(stmts, &proc_offset);
714
715 expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
716 if (expr_eval_routines)
717 write_expr_eval_routine_list(proxy, proxy_token);
718 write_user_quad_list(proxy);
719 write_stubdesc(expr_eval_routines);
720
721 print_proxy( "#if !defined(__RPC_WIN32__)\n");
722 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
723 print_proxy( "#endif\n");
724 print_proxy( "\n");
725 write_procformatstring(proxy, stmts, need_proxy);
726 write_typeformatstring(proxy, stmts, need_proxy);
727
728 fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
729 fprintf(proxy, "{\n");
730 write_proxy_iface_name_format(stmts, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n");
731 fprintf(proxy, " 0\n");
732 fprintf(proxy, "};\n");
733 fprintf(proxy, "\n");
734
735 fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
736 fprintf(proxy, "{\n");
737 write_proxy_iface_name_format(stmts, " (const CInterfaceStubVtbl*)&_%sStubVtbl,\n");
738 fprintf(proxy, " 0\n");
739 fprintf(proxy, "};\n");
740 fprintf(proxy, "\n");
741
742 fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
743 fprintf(proxy, "{\n");
744 write_proxy_iface_name_format(stmts, " \"%s\",\n");
745 fprintf(proxy, " 0\n");
746 fprintf(proxy, "};\n");
747 fprintf(proxy, "\n");
748
749 fprintf(proxy, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_%s, pIID, n)\n", file_id, file_id);
750 fprintf(proxy, "\n");
751 fprintf(proxy, "int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
752 fprintf(proxy, "{\n");
753 c = 0;
754 write_iid_lookup(stmts, file_id, &c);
755 fprintf(proxy, " return 0;\n");
756 fprintf(proxy, "}\n");
757 fprintf(proxy, "\n");
758
759 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id);
760 fprintf(proxy, "{\n");
761 fprintf(proxy, " (const PCInterfaceProxyVtblList*)&_%s_ProxyVtblList,\n", file_id);
762 fprintf(proxy, " (const PCInterfaceStubVtblList*)&_%s_StubVtblList,\n", file_id);
763 fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
764 fprintf(proxy, " 0,\n");
765 fprintf(proxy, " &_%s_IID_Lookup,\n", file_id);
766 fprintf(proxy, " %d,\n", c);
767 fprintf(proxy, " 1,\n");
768 fprintf(proxy, " 0,\n");
769 fprintf(proxy, " 0,\n");
770 fprintf(proxy, " 0,\n");
771 fprintf(proxy, " 0\n");
772 fprintf(proxy, "};\n");
773
774 fclose(proxy);
775 }