Output NdrConvert, NdrClearOutParameters and IID constraints.
[wine.git] / tools / widl / proxy.c
blobf17feba8ac8d1757a80eb21d709e0076081922be
1 /*
2 * IDL Compiler
4 * Copyright 2002 Ove Kaaven
5 * Copyright 2004 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <assert.h>
31 #include <ctype.h>
32 #include <signal.h>
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
39 static FILE* proxy;
40 static int indent = 0;
42 /* FIXME: support generation of stubless proxies */
44 int print_proxy( char *format, ... )
46 va_list va;
47 int i, r;
49 va_start( va, format );
50 for( i=0; i<indent; i++ )
51 fprintf( proxy, " " );
52 r = vfprintf( proxy, format, va );
53 va_end( va );
54 return r;
57 static type_t *get_base_type( type_t *type )
59 while( type->ref )
60 type = type->ref;
61 return type;
64 static void write_stubdescproto(void)
66 print_proxy( "extern const MIDL_STUB_DESC Object_StubDesc;\n");
67 print_proxy( "\n");
70 static void write_stubdesc(void)
72 print_proxy( "const MIDL_STUB_DESC Object_StubDesc = {\n");
73 print_proxy( " 0,\n");
74 print_proxy( " NdrOleAllocate,\n");
75 print_proxy( " NdrOleFree,\n");
76 print_proxy( " {0}, 0, 0, 0, 0,\n");
77 print_proxy( " 0 /* __MIDL_TypeFormatString.Format */\n");
78 print_proxy( "};\n");
79 print_proxy( "\n");
82 static void write_formatdesc( char *str )
84 print_proxy( "typedef struct _MIDL_%s_FORMAT_STRING {\n", str );
85 print_proxy( " short Pad;\n");
86 print_proxy( " unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
87 print_proxy( "} MIDL_%s_FORMAT_STRING;\n", str);
88 print_proxy( "\n");
91 static void write_formatstringsdecl(void)
93 print_proxy( "#define TYPE_FORMAT_STRING_SIZE %d\n",1); /* FIXME */
94 print_proxy( "#define PROC_FORMAT_STRING_SIZE %d\n",1); /* FIXME */
95 print_proxy( "\n");
96 write_formatdesc( "TYPE" );
97 write_formatdesc( "PROC" );
98 print_proxy( "extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
99 print_proxy( "extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
100 print_proxy( "\n");
103 static void write_formatstring( int proc )
105 const char *t, *n;
106 if( !proc )
108 t = "TYPE";
109 n = "Type";
111 else
113 t = "PROC";
114 n = "Proc";
116 print_proxy( "static const MIDL_%s_FORMAT_STRING __MIDL_%sFormatString =\n", t, n);
117 print_proxy( "{\n");
118 indent++;
119 print_proxy( "0,\n");
120 print_proxy( "{\n");
121 indent++;
122 print_proxy( "0\n");
123 indent--;
124 print_proxy( "}\n");
125 indent--;
126 print_proxy( "};\n");
127 print_proxy( "\n");
130 static void init_proxy(void)
132 if (proxy) return;
133 proxy = fopen(proxy_name, "w");
134 print_proxy( "/*** Autogenerated by WIDL %s - Do not edit ***/\n", WIDL_FULLVERSION);
135 print_proxy( "\n");
136 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
137 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ 440\n");
138 print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
139 print_proxy( "\n");
140 print_proxy( "#include \"rpcproxy.h\"\n");
141 print_proxy( "\n");
142 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
143 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
144 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
145 print_proxy( "\n");
146 print_proxy( "#include \"%s\"\n", header_name);
147 print_proxy( "\n");
148 write_formatstringsdecl();
149 write_stubdescproto();
152 static void clear_output_vars( var_t *arg )
154 if (arg) {
155 while (NEXT_LINK(arg))
156 arg = NEXT_LINK(arg);
158 while (arg) {
159 if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
160 print_proxy( "if(%s)\n", arg->name );
161 indent++;
162 print_proxy( "MIDL_memset( %s, 0, sizeof( ", arg->name );
163 indent--;
164 write_type(proxy, arg->type, arg, arg->tname);
165 fprintf( proxy, " ));\n" );
167 arg = PREV_LINK(arg);
171 static void proxy_gen_marshall_size( var_t *arg )
173 print_proxy( "_StubMsg.BufferLength = 0U;\n" );
175 if (arg) {
176 while (NEXT_LINK(arg))
177 arg = NEXT_LINK(arg);
179 while (arg) {
180 if (is_attr(arg->attrs, ATTR_IN)) {
181 int index = 0;
182 type_t *type = get_base_type(arg->type);
184 switch( type->type )
186 case RPC_FC_BYTE:
187 case RPC_FC_CHAR:
188 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 1, arg->name );
189 break;
191 case RPC_FC_SHORT:
192 case RPC_FC_USHORT:
193 case RPC_FC_ENUM16:
194 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 2, arg->name );
195 break;
197 case RPC_FC_LONG:
198 case RPC_FC_ULONG:
199 case RPC_FC_ENUM32:
200 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 4, arg->name );
201 break;
203 case RPC_FC_STRUCT:
204 print_proxy( "NdrSimpleStructBufferSize(&_StubMsg, (unsigned char*)%s, ", arg->name );
205 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
206 break;
208 case RPC_FC_IP:
209 print_proxy( "NdrPointerBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
210 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
211 break;
213 default:
214 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
217 arg = PREV_LINK(arg);
221 static void gen_marshall_copydata( var_t *arg )
223 if (arg) {
224 while (NEXT_LINK(arg))
225 arg = NEXT_LINK(arg);
227 while (arg) {
228 if (is_attr(arg->attrs, ATTR_IN)) {
229 int index = 0;
230 type_t *type = get_base_type(arg->type);
232 switch( type->type )
234 case RPC_FC_BYTE:
235 case RPC_FC_CHAR:
236 case RPC_FC_SHORT:
237 case RPC_FC_USHORT:
238 case RPC_FC_ENUM16:
239 case RPC_FC_LONG:
240 case RPC_FC_ULONG:
241 case RPC_FC_ENUM32:
242 print_proxy( "*((");
243 write_type(proxy, arg->type, arg, arg->tname);
244 fprintf(proxy,"*)_StubMsg.Buffer)++ = %s;\n", arg->name );
245 break;
247 case RPC_FC_STRUCT:
248 /* FIXME: add the format string, and set the index below */
249 print_proxy( "NdrSimpleStructMarshall(&_StubMsg, (unsigned char*)%s, ", arg->name );
250 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
251 break;
253 case RPC_FC_IP:
254 print_proxy( "NdrPointerMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name );
255 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
256 break;
258 default:
259 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
262 arg = PREV_LINK(arg);
266 static void gen_marshall( var_t *arg )
268 /* generated code to determine the size of the buffer required */
269 proxy_gen_marshall_size( arg );
271 /* generated code to allocate the buffer */
272 print_proxy( "NdrProxyGetBuffer(This, &_StubMsg);\n" );
274 /* generated code to copy the args into the buffer */
275 gen_marshall_copydata( arg );
277 print_proxy( "\n");
280 static void gen_unmarshall( var_t *arg )
282 if (arg) {
283 while (NEXT_LINK(arg))
284 arg = NEXT_LINK(arg);
286 while (arg) {
287 if (is_attr(arg->attrs, ATTR_IN)) {
288 int index = 0;
289 print_proxy( "NdrPointerUnmarshall(&_StubMsg, (unsigned char**)&%s, ", arg->name );
290 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
292 arg = PREV_LINK(arg);
296 static void proxy_free_variables( var_t *arg )
298 if (arg) {
299 while (NEXT_LINK(arg))
300 arg = NEXT_LINK(arg);
302 while (arg) {
303 if (is_attr(arg->attrs, ATTR_OUT)) {
304 var_t *constraint;
305 int index = 0; /* FIXME */
306 type_t *type = get_base_type(arg->type);
308 switch( type->type )
310 case RPC_FC_BYTE:
311 case RPC_FC_CHAR:
312 case RPC_FC_SHORT:
313 case RPC_FC_USHORT:
314 case RPC_FC_ENUM16:
315 case RPC_FC_LONG:
316 case RPC_FC_ULONG:
317 case RPC_FC_ENUM32:
318 case RPC_FC_STRUCT:
319 break;
321 case RPC_FC_IP:
322 constraint = get_attrp( arg->attrs, ATTR_IIDIS );
323 if( constraint )
324 print_proxy( "_StubMsg.MaxCount = (unsigned long) ( %s );\n",constraint->name);
325 print_proxy( "NdrClearOutParameters( &_StubMsg, ");
326 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], ", index );
327 fprintf(proxy, "(void*)%s );\n", arg->name );
328 break;
330 default:
331 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
334 arg = PREV_LINK(arg);
338 static void gen_proxy(type_t *iface, func_t *cur, int idx)
340 var_t *def = cur->def;
341 int has_ret = !is_void(def->type, def);
343 indent = 0;
344 write_type(proxy, def->type, def, def->tname);
345 print_proxy( " CALLBACK %s_", iface->name);
346 write_name(proxy, def);
347 print_proxy( "_Proxy(\n");
348 write_args(proxy, cur->args, iface->name, 1, TRUE);
349 print_proxy( ")\n");
350 print_proxy( "{\n");
351 indent ++;
352 /* local variables */
353 if (has_ret) {
354 print_proxy( "" );
355 write_type(proxy, def->type, def, def->tname);
356 print_proxy( " _RetVal;\n");
358 print_proxy( "RPC_MESSAGE _Msg;\n" );
359 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n" );
360 print_proxy( "\n");
362 /* FIXME: trace */
363 clear_output_vars( cur->args );
365 print_proxy( "RpcTryExcept\n" );
366 print_proxy( "{\n" );
367 indent++;
368 print_proxy( "NdrProxyInitialize(This, &_Msg, &_StubMsg, &Object_StubDesc, %d);\n", idx);
370 print_proxy( "RpcTryFinally\n" );
371 print_proxy( "{\n" );
372 indent++;
374 gen_marshall( cur->args );
376 print_proxy( "NdrProxySendReceive(This, &_StubMsg);\n" );
377 fprintf(proxy, "\n");
378 print_proxy("if ((_Msg.DataRepresentation&0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
379 indent++;
380 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[0]);\n" );
381 indent--;
382 fprintf(proxy, "\n");
384 gen_unmarshall( cur->args );
385 if (has_ret) {
387 * FIXME: We only need to round the buffer up if it could be unaligned...
388 * We should calculate how much buffer we used and output the following
389 * line only if necessary.
391 print_proxy( "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + 3) & ~ 0x3);\n");
393 print_proxy( "_RetVal = *((" );
394 write_type(proxy, def->type, def, def->tname);
395 fprintf(proxy, "*)_StubMsg.Buffer)++;\n");
398 indent--;
399 print_proxy( "}\n");
400 print_proxy( "RpcFinally\n" );
401 print_proxy( "{\n" );
402 indent++;
403 print_proxy( "NdrProxyFreeBuffer(This, &_StubMsg);\n" );
404 indent--;
405 print_proxy( "}\n");
406 print_proxy( "RpcEndFinally\n" );
407 indent--;
408 print_proxy( "}\n" );
409 print_proxy( "RpcExcept(_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
410 print_proxy( "{\n" );
411 if (has_ret) {
412 indent++;
413 proxy_free_variables( cur->args );
414 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
415 indent--;
417 print_proxy( "}\n" );
418 print_proxy( "RpcEndExcept\n" );
420 if (has_ret) {
421 print_proxy( "return _RetVal;\n" );
423 indent--;
424 print_proxy( "}\n");
425 print_proxy( "\n");
428 static void stub_write_locals( var_t *arg )
430 int n = 0;
431 while (arg) {
432 int outptr = is_attr(arg->attrs, ATTR_OUT);
434 /* create a temporary variable to store the output */
435 if (outptr) {
436 var_t temp;
437 memset( &temp, 0, sizeof temp );
438 temp.ptr_level = arg->ptr_level - 1; /* dereference once */
439 print_proxy("");
440 write_type(proxy, arg->type, &temp, arg->tname);
441 fprintf(proxy, " _M%d;\n",n++);
443 print_proxy("");
444 write_type(proxy, arg->type, arg, arg->tname);
445 fprintf(proxy, " ");
446 write_name(proxy, arg);
447 /* if (outptr) fprintf(proxy, " = &_M%d;\n",n++); */
448 fprintf(proxy, ";\n");
449 arg = NEXT_LINK(arg);
453 static void stub_unmarshall( var_t *arg )
455 int n = 0;
456 if (arg) {
457 while (NEXT_LINK(arg))
458 arg = NEXT_LINK(arg);
460 while (arg) {
461 if (is_attr(arg->attrs, ATTR_IN)) {
462 int index = 0;
463 type_t *type = get_base_type(arg->type);
465 switch( type->type )
467 case RPC_FC_BYTE:
468 case RPC_FC_CHAR:
469 case RPC_FC_SHORT:
470 case RPC_FC_USHORT:
471 case RPC_FC_ENUM16:
472 case RPC_FC_LONG:
473 case RPC_FC_ULONG:
474 case RPC_FC_ENUM32:
475 print_proxy( "%s = *((", arg->name);
476 write_type(proxy, arg->type, arg, arg->tname);
477 fprintf(proxy,"*)_StubMsg.Buffer)++;\n");
478 break;
480 case RPC_FC_STRUCT:
481 /* FIXME: add the format string, and set the index below */
482 print_proxy( "NdrSimpleStructUnmarshall(&_StubMsg, (unsigned char**)&%s, ", arg->name );
483 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d],0);\n", index );
484 break;
486 default:
487 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
490 else if (is_attr(arg->attrs, ATTR_OUT)) {
491 print_proxy("");
492 write_name(proxy, arg);
493 fprintf(proxy," = &_M%d;\n", n);
494 print_proxy("_M%d = 0;\n", n++);
496 arg = PREV_LINK(arg);
500 static void stub_gen_marshall_size( var_t *arg )
502 print_proxy( "_StubMsg.BufferLength = 0U;\n" );
504 if (arg) {
505 while (NEXT_LINK(arg))
506 arg = NEXT_LINK(arg);
508 while (arg) {
509 if (is_attr(arg->attrs, ATTR_OUT)) {
510 int index = 0;
511 var_t *constraint;
512 type_t *type = get_base_type(arg->type);
514 switch( type->type )
516 case RPC_FC_BYTE:
517 case RPC_FC_CHAR:
518 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 1, arg->name );
519 break;
521 case RPC_FC_SHORT:
522 case RPC_FC_USHORT:
523 case RPC_FC_ENUM16:
524 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 2, arg->name );
525 break;
527 case RPC_FC_LONG:
528 case RPC_FC_ULONG:
529 case RPC_FC_ENUM32:
530 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 4, arg->name );
531 break;
533 case RPC_FC_STRUCT:
534 print_proxy( "NdrSimpleStructBufferSize(&_StubMsg, (unsigned char*)%s, ", arg->name );
535 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
536 break;
538 case RPC_FC_IP:
539 constraint = get_attrp( arg->attrs, ATTR_IIDIS );
540 if( constraint )
541 print_proxy( "_StubMsg.MaxCount = (unsigned long) ( %s );\n",constraint->name);
542 print_proxy( "NdrPointerBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
543 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
544 break;
546 default:
547 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
550 arg = PREV_LINK(arg);
554 static void stub_gen_marshall_copydata( var_t *arg )
556 if (arg) {
557 while (NEXT_LINK(arg))
558 arg = NEXT_LINK(arg);
560 while (arg) {
561 if (is_attr(arg->attrs, ATTR_OUT)) {
562 var_t *constraint;
563 int index = 0;
564 type_t *type = get_base_type(arg->type);
566 switch( type->type )
568 case RPC_FC_BYTE:
569 case RPC_FC_CHAR:
570 case RPC_FC_SHORT:
571 case RPC_FC_USHORT:
572 case RPC_FC_ENUM16:
573 case RPC_FC_LONG:
574 case RPC_FC_ULONG:
575 case RPC_FC_ENUM32:
576 print_proxy( "*((");
577 write_type(proxy, arg->type, arg, arg->tname);
578 fprintf(proxy,"*)_StubMsg.Buffer)++ = %s;\n", arg->name );
579 break;
581 case RPC_FC_STRUCT:
582 /* FIXME: add the format string, and set the index below */
583 print_proxy( "NdrSimpleStructMarshall(&_StubMsg, (unsigned char*)%s, ", arg->name );
584 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
585 break;
587 case RPC_FC_IP:
588 constraint = get_attrp( arg->attrs, ATTR_IIDIS );
589 if( constraint )
590 print_proxy( "_StubMsg.MaxCount = (unsigned long) ( %s );\n",constraint->name);
591 print_proxy( "NdrPointerMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name );
592 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
593 break;
595 default:
596 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
599 arg = PREV_LINK(arg);
603 static void stub_genmarshall( var_t *args )
605 /* FIXME: size buffer */
606 stub_gen_marshall_size( args );
608 print_proxy("NdrStubGetBuffer(This, pRpcChannelBuffer, &_StubMsg);\n");
610 stub_gen_marshall_copydata( args );
613 static void gen_stub(type_t *iface, func_t *cur, char *cas)
615 var_t *def = cur->def;
616 var_t *arg;
617 int has_ret = !is_void(def->type, def);
619 indent = 0;
620 print_proxy( "void __RPC_STUB %s_", iface->name);
621 write_name(proxy, def);
622 print_proxy( "_Stub(\n");
623 indent++;
624 print_proxy( "IRpcStubBuffer* This,\n");
625 print_proxy( "IRpcChannelBuffer* pRpcChannelBuffer,\n");
626 print_proxy( "PRPC_MESSAGE _Msg,\n");
627 print_proxy( "DWORD* _pdwStubPhase)\n");
628 indent--;
629 print_proxy( "{\n");
630 indent++;
631 /* local variables */
632 if (has_ret) {
633 print_proxy("");
634 write_type(proxy, def->type, def, def->tname);
635 fprintf(proxy, " _RetVal;\n");
637 print_proxy("%s* _This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n", iface->name, iface->name);
638 print_proxy("MIDL_STUB_MESSAGE _StubMsg;\n");
639 stub_write_locals( cur->args );
640 fprintf(proxy, "\n");
642 /* FIXME: trace */
644 print_proxy("NdrStubInitialize(_Msg, &_StubMsg, &Object_StubDesc, pRpcChannelBuffer);\n");
645 fprintf(proxy, "\n");
647 print_proxy("RpcTryFinally\n");
648 print_proxy("{\n");
649 indent++;
650 print_proxy("if ((_Msg->DataRepresentation&0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
651 indent++;
652 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[0]);\n" );
653 indent--;
654 fprintf(proxy, "\n");
656 stub_unmarshall( cur->args );
657 fprintf(proxy, "\n");
659 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
660 fprintf(proxy, "\n");
661 print_proxy("");
662 if (has_ret) fprintf(proxy, "_RetVal = ");
663 fprintf(proxy, "%s_", iface->name);
664 if (cas) fprintf(proxy, "%s_Stub", cas);
665 else write_name(proxy, def);
666 fprintf(proxy, "(_This");
667 arg = cur->args;
668 if (arg) {
669 while (NEXT_LINK(arg)) arg = NEXT_LINK(arg);
670 while (arg) {
671 fprintf(proxy, ", ");
672 write_name(proxy, arg);
673 arg = PREV_LINK(arg);
676 fprintf(proxy, ");\n");
677 fprintf(proxy, "\n");
678 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
679 fprintf(proxy, "\n");
681 stub_genmarshall( cur->args );
682 fprintf(proxy, "\n");
684 if (has_ret) {
686 * FIXME: We only need to round the buffer up if it could be unaligned...
687 * We should calculate how much buffer we used and output the following
688 * line only if necessary.
690 print_proxy( "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + 3) & ~ 0x3);\n");
692 print_proxy( "*((" );
693 write_type(proxy, def->type, def, def->tname);
694 fprintf(proxy, "*)_StubMsg.Buffer)++ = _RetVal;\n");
697 indent--;
698 print_proxy("}\n");
699 print_proxy("RpcFinally\n");
700 print_proxy("{\n");
701 print_proxy("}\n");
702 print_proxy("RpcEndFinally\n");
704 print_proxy("_Msg->BufferLength = ((long)_StubMsg.Buffer - (long)_Msg->Buffer);\n");
705 indent--;
707 print_proxy("}\n");
708 print_proxy("\n");
711 static int write_proxy_methods(type_t *iface)
713 func_t *cur = iface->funcs;
714 int i = 0;
715 while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
717 if (iface->ref) i = write_proxy_methods(iface->ref);
718 while (cur) {
719 var_t *def = cur->def;
720 if (!is_callas(def->attrs)) {
721 if (i) fprintf(proxy, ",\n");
722 print_proxy( "%s_", iface->name);
723 write_name(proxy, def);
724 fprintf(proxy, "_Proxy");
725 i++;
727 cur = PREV_LINK(cur);
729 return i;
732 static int write_stub_methods(type_t *iface)
734 func_t *cur = iface->funcs;
735 int i = 0;
736 while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
738 if (iface->ref) i = write_stub_methods(iface->ref);
739 else return i; /* skip IUnknown */
740 while (cur) {
741 var_t *def = cur->def;
742 if (!is_local(def->attrs)) {
743 if (i) fprintf(proxy,",\n");
744 print_proxy( "%s_", iface->name);
745 write_name(proxy, def);
746 fprintf(proxy, "_Stub");
747 i++;
749 cur = PREV_LINK(cur);
751 return i;
754 static void write_proxy(type_t *iface)
756 int midx = -1, stubs;
757 func_t *cur = iface->funcs;
759 if (!cur) return;
760 if (!do_everything) return;
762 while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
764 /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
766 init_proxy();
768 fprintf(proxy, "/*****************************************************************************\n");
769 fprintf(proxy, " * %s interface\n", iface->name);
770 fprintf(proxy, " */\n");
771 while (cur) {
772 var_t *def = cur->def;
773 if (!is_local(def->attrs)) {
774 var_t *cas = is_callas(def->attrs);
775 char *cname = cas ? cas->name : NULL;
776 int idx = cur->idx;
777 if (cname) {
778 func_t *m = iface->funcs;
779 while (m && strcmp(get_name(m->def), cname))
780 m = NEXT_LINK(m);
781 idx = m->idx;
783 gen_proxy(iface, cur, idx);
784 gen_stub(iface, cur, cname);
785 if (midx == -1) midx = idx;
786 else if (midx != idx) yyerror("method index mismatch in write_proxy");
787 midx++;
789 cur = PREV_LINK(cur);
792 write_stubdesc();
794 /* proxy vtable */
795 print_proxy( "const CINTERFACE_PROXY_VTABLE(%d) %sProxyVtbl =\n", midx, iface->name);
796 print_proxy( "{\n");
797 indent++;
798 print_proxy( "{&IID_%s},{\n", iface->name);
799 write_proxy_methods(iface);
800 fprintf(proxy, "}\n");
801 indent--;
802 print_proxy( "};\n");
803 print_proxy( "\n");
805 /* stub vtable */
806 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
807 print_proxy( "{\n");
808 indent++;
809 stubs = write_stub_methods(iface);
810 fprintf(proxy, "\n");
811 indent--;
812 fprintf(proxy, "};\n");
813 print_proxy( "\n");
814 print_proxy( "const CInterfaceStubVtbl %sStubVtbl = {\n", iface->name);
815 indent++;
816 print_proxy( "{&IID_%s,\n", iface->name);
817 print_proxy( " 0,\n");
818 print_proxy( " %d,\n", stubs+3);
819 print_proxy( " &%s_table[-3]},\n", iface->name);
820 print_proxy( "{CStdStubBuffer_METHODS}\n");
821 indent--;
822 print_proxy( "};\n");
823 print_proxy( "\n");
824 print_proxy( "#if !defined(__RPC_WIN32__)\n");
825 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
826 print_proxy( "#endif\n");
827 print_proxy( "\n");
828 write_formatstring( 1 );
829 write_formatstring( 0 );
832 void write_proxies(ifref_t *ifaces)
834 ifref_t *lcur = ifaces;
835 ifref_t *cur;
836 char *file_id = proxy_token;
837 int c;
839 if (!lcur) return;
840 while (NEXT_LINK(lcur)) lcur = NEXT_LINK(lcur);
842 cur = lcur;
843 while (cur) {
844 if (is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
845 write_proxy(cur->iface);
846 cur = PREV_LINK(cur);
849 if (!proxy) return;
851 fprintf(proxy, "const CInterfaceProxyVtbl* _%s_ProxyVtblList[] =\n", file_id);
852 fprintf(proxy, "{\n");
853 cur = lcur;
854 while (cur) {
855 if(cur->iface->ref)
856 fprintf(proxy, " (CInterfaceProxyVtbl*)&%sProxyVtbl,\n", cur->iface->name);
857 cur = PREV_LINK(cur);
859 fprintf(proxy, " 0\n");
860 fprintf(proxy, "};\n");
861 fprintf(proxy, "\n");
863 fprintf(proxy, "const CInterfaceStubVtbl* _%s_StubVtblList[] =\n", file_id);
864 fprintf(proxy, "{\n");
865 cur = lcur;
866 while (cur) {
867 if(cur->iface->ref)
868 fprintf(proxy, " (CInterfaceStubVtbl*)&%sStubVtbl,\n", cur->iface->name);
869 cur = PREV_LINK(cur);
871 fprintf(proxy, " 0\n");
872 fprintf(proxy, "};\n");
873 fprintf(proxy, "\n");
875 fprintf(proxy, "const PCInterfaceName _%s_InterfaceNamesList[] =\n", file_id);
876 fprintf(proxy, "{\n");
877 cur = lcur;
878 while (cur) {
879 if(cur->iface->ref)
880 fprintf(proxy, " \"%s\",\n", cur->iface->name);
881 cur = PREV_LINK(cur);
883 fprintf(proxy, " 0\n");
884 fprintf(proxy, "};\n");
885 fprintf(proxy, "\n");
887 fprintf(proxy, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_%s, pIID, n)\n", file_id, file_id);
888 fprintf(proxy, "\n");
889 fprintf(proxy, "int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
890 fprintf(proxy, "{\n");
891 cur = lcur;
892 c = 0;
893 while (cur) {
894 if(cur->iface->ref)
896 fprintf(proxy, " if (!_%s_CHECK_IID(%d)) {\n", file_id, c);
897 fprintf(proxy, " *pIndex = %d;\n", c);
898 fprintf(proxy, " return 1;\n");
899 fprintf(proxy, " }\n");
900 c++;
902 cur = PREV_LINK(cur);
904 fprintf(proxy, " return 0;\n");
905 fprintf(proxy, "}\n");
906 fprintf(proxy, "\n");
908 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id);
909 fprintf(proxy, "{\n");
910 fprintf(proxy, " (PCInterfaceProxyVtblList*)&_%s_ProxyVtblList,\n", file_id);
911 fprintf(proxy, " (PCInterfaceStubVtblList*)&_%s_StubVtblList,\n", file_id);
912 fprintf(proxy, " (const PCInterfaceName*)&_%s_InterfaceNamesList,\n", file_id);
913 fprintf(proxy, " 0,\n");
914 fprintf(proxy, " &_%s_IID_Lookup,\n", file_id);
915 fprintf(proxy, " %d,\n", c);
916 fprintf(proxy, " 1,\n");
917 fprintf(proxy, " 0,\n");
918 fprintf(proxy, " 0,\n");
919 fprintf(proxy, " 0,\n");
920 fprintf(proxy, " 0\n");
921 fprintf(proxy, "};\n");
923 fclose(proxy);