Release 20040914.
[wine/gsoc-2012-control.git] / tools / widl / proxy.c
blob58fc2c12036d555fb4fc01e0932c3f4891ac971b
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 #define END_OF_LIST(list) \
40 do { \
41 if (list) { \
42 while (NEXT_LINK(list)) \
43 list = NEXT_LINK(list); \
44 } \
45 } while(0)
47 static FILE* proxy;
48 static int indent = 0;
50 /* FIXME: support generation of stubless proxies */
52 int print_proxy( char *format, ... )
54 va_list va;
55 int i, r;
57 va_start( va, format );
58 for( i=0; i<indent; i++ )
59 fprintf( proxy, " " );
60 r = vfprintf( proxy, format, va );
61 va_end( va );
62 return r;
65 static type_t *get_base_type( type_t *type )
67 while( (type->type == 0) && type->ref )
68 type = type->ref;
69 return type;
72 static void write_stubdescproto(void)
74 print_proxy( "extern const MIDL_STUB_DESC Object_StubDesc;\n");
75 print_proxy( "\n");
78 static void write_stubdesc(void)
80 print_proxy( "const MIDL_STUB_DESC Object_StubDesc = {\n");
81 print_proxy( " 0,\n");
82 print_proxy( " NdrOleAllocate,\n");
83 print_proxy( " NdrOleFree,\n");
84 print_proxy( " {0}, 0, 0, 0, 0,\n");
85 print_proxy( " 0 /* __MIDL_TypeFormatString.Format */\n");
86 print_proxy( "};\n");
87 print_proxy( "\n");
90 static void write_formatdesc( char *str )
92 print_proxy( "typedef struct _MIDL_%s_FORMAT_STRING {\n", str );
93 print_proxy( " short Pad;\n");
94 print_proxy( " unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
95 print_proxy( "} MIDL_%s_FORMAT_STRING;\n", str);
96 print_proxy( "\n");
99 static void write_formatstringsdecl(void)
101 print_proxy( "#define TYPE_FORMAT_STRING_SIZE %d\n",1); /* FIXME */
102 print_proxy( "#define PROC_FORMAT_STRING_SIZE %d\n",1); /* FIXME */
103 print_proxy( "\n");
104 write_formatdesc( "TYPE" );
105 write_formatdesc( "PROC" );
106 print_proxy( "extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
107 print_proxy( "extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
108 print_proxy( "\n");
111 static void write_formatstring( int proc )
113 const char *t, *n;
114 if( !proc )
116 t = "TYPE";
117 n = "Type";
119 else
121 t = "PROC";
122 n = "Proc";
124 print_proxy( "static const MIDL_%s_FORMAT_STRING __MIDL_%sFormatString =\n", t, n);
125 print_proxy( "{\n");
126 indent++;
127 print_proxy( "0,\n");
128 print_proxy( "{\n");
129 indent++;
130 print_proxy( "0\n");
131 indent--;
132 print_proxy( "}\n");
133 indent--;
134 print_proxy( "};\n");
135 print_proxy( "\n");
138 static void init_proxy(void)
140 if (proxy) return;
141 proxy = fopen(proxy_name, "w");
142 print_proxy( "/*** Autogenerated by WIDL %s - Do not edit ***/\n", WIDL_FULLVERSION);
143 print_proxy( "\n");
144 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
145 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ 440\n");
146 print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
147 print_proxy( "\n");
148 print_proxy( "#include \"rpcproxy.h\"\n");
149 print_proxy( "\n");
150 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
151 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
152 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
153 print_proxy( "\n");
154 print_proxy( "#include \"%s\"\n", header_name);
155 print_proxy( "\n");
156 write_formatstringsdecl();
157 write_stubdescproto();
160 static void clear_output_vars( var_t *arg )
162 END_OF_LIST(arg);
163 while (arg) {
164 if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
165 print_proxy( "if(%s)\n", arg->name );
166 indent++;
167 print_proxy( "MIDL_memset( %s, 0, sizeof( ", arg->name );
168 indent--;
169 write_type(proxy, arg->type, arg, arg->tname);
170 fprintf( proxy, " ));\n" );
172 arg = PREV_LINK(arg);
176 static int is_pointer(var_t *arg)
178 if (arg->ptr_level)
179 return 1;
180 if (arg->type->type == RPC_FC_FP )
181 return 1;
182 return 0;
185 static void proxy_check_pointers( var_t *arg )
187 END_OF_LIST(arg);
188 while (arg) {
189 if (is_pointer(arg)) {
190 print_proxy( "if(!%s)\n", arg->name );
191 indent++;
192 print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
193 indent--;
195 arg = PREV_LINK(arg);
199 static void proxy_gen_marshall_size( var_t *arg )
201 print_proxy( "_StubMsg.BufferLength = 0U;\n" );
203 END_OF_LIST(arg);
204 while (arg) {
205 if (is_attr(arg->attrs, ATTR_IN)) {
206 int index = 0;
207 type_t *type = get_base_type(arg->type);
209 switch( type->type )
211 case RPC_FC_BYTE:
212 case RPC_FC_CHAR:
213 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 1, arg->name );
214 break;
216 case RPC_FC_WCHAR:
217 case RPC_FC_SHORT:
218 case RPC_FC_USHORT:
219 case RPC_FC_ENUM16:
220 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 2, arg->name );
221 break;
223 case RPC_FC_LONG:
224 case RPC_FC_ULONG:
225 case RPC_FC_ENUM32:
226 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 4, arg->name );
227 break;
229 case RPC_FC_STRUCT:
230 print_proxy( "NdrSimpleStructBufferSize(&_StubMsg, (unsigned char*)%s, ", arg->name );
231 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
232 break;
234 case RPC_FC_FP:
235 print_proxy( "NdrConformantArrayBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
236 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
237 break;
239 case RPC_FC_IP:
240 print_proxy( "NdrPointerBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
241 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
242 break;
244 default:
245 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
248 arg = PREV_LINK(arg);
252 static void gen_marshall_copydata( var_t *arg )
254 END_OF_LIST(arg);
255 while (arg) {
256 if (is_attr(arg->attrs, ATTR_IN)) {
257 int index = 0;
258 type_t *type = get_base_type(arg->type);
260 switch( type->type )
262 case RPC_FC_BYTE:
263 case RPC_FC_CHAR:
264 case RPC_FC_WCHAR:
265 case RPC_FC_SHORT:
266 case RPC_FC_USHORT:
267 case RPC_FC_ENUM16:
268 case RPC_FC_LONG:
269 case RPC_FC_ULONG:
270 case RPC_FC_ENUM32:
271 print_proxy( "*((");
272 write_type(proxy, arg->type, arg, arg->tname);
273 fprintf(proxy,"*)_StubMsg.Buffer)++ = %s;\n", arg->name );
274 break;
276 case RPC_FC_STRUCT:
277 /* FIXME: add the format string, and set the index below */
278 print_proxy( "NdrSimpleStructMarshall(&_StubMsg, (unsigned char*)%s, ", arg->name );
279 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
280 break;
282 case RPC_FC_FP:
283 print_proxy( "NdrConformantArrayMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name );
284 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
285 break;
287 case RPC_FC_IP:
288 print_proxy( "NdrPointerMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name );
289 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
290 break;
292 default:
293 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
296 arg = PREV_LINK(arg);
300 static void gen_marshall( var_t *arg )
302 /* generated code to determine the size of the buffer required */
303 proxy_gen_marshall_size( arg );
305 /* generated code to allocate the buffer */
306 print_proxy( "NdrProxyGetBuffer(This, &_StubMsg);\n" );
308 /* generated code to copy the args into the buffer */
309 gen_marshall_copydata( arg );
311 print_proxy( "\n");
314 static void gen_unmarshall( var_t *arg )
316 END_OF_LIST(arg);
317 while (arg) {
318 if (is_attr(arg->attrs, ATTR_OUT)) {
319 int index = 0;
320 type_t *type = get_base_type(arg->type);
322 switch( type->type )
324 case RPC_FC_BYTE:
325 case RPC_FC_CHAR:
326 case RPC_FC_WCHAR:
327 case RPC_FC_SHORT:
328 case RPC_FC_USHORT:
329 case RPC_FC_ENUM16:
330 case RPC_FC_LONG:
331 case RPC_FC_ULONG:
332 case RPC_FC_ENUM32:
333 print_proxy( "%s = *((", arg->name );
334 write_type(proxy, arg->type, arg, arg->tname);
335 fprintf(proxy,"*)_StubMsg.Buffer)++;\n");
336 break;
338 case RPC_FC_STRUCT:
339 print_proxy( "NdrSimpleStructUnmarshall(&_StubMsg, (unsigned char**)%s, ", arg->name );
340 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
341 break;
343 case RPC_FC_FP:
344 print_proxy( "NdrSimpleStructUnmarshall(&_StubMsg, (unsigned char**)%s, ", arg->name );
345 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
346 break;
348 case RPC_FC_IP:
349 print_proxy( "NdrPointerUnmarshall(&_StubMsg, (unsigned char**)&%s, ", arg->name );
350 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
351 break;
353 default:
354 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
356 arg = PREV_LINK(arg);
360 static void proxy_free_variables( var_t *arg )
362 END_OF_LIST(arg);
363 while (arg) {
364 if (is_attr(arg->attrs, ATTR_OUT)) {
365 var_t *constraint;
366 int index = 0; /* FIXME */
367 type_t *type = get_base_type(arg->type);
369 switch( type->type )
371 case RPC_FC_BYTE:
372 case RPC_FC_CHAR:
373 case RPC_FC_WCHAR:
374 case RPC_FC_SHORT:
375 case RPC_FC_USHORT:
376 case RPC_FC_ENUM16:
377 case RPC_FC_LONG:
378 case RPC_FC_ULONG:
379 case RPC_FC_ENUM32:
380 case RPC_FC_STRUCT:
381 break;
383 case RPC_FC_FP:
384 case RPC_FC_IP:
385 constraint = get_attrp( arg->attrs, ATTR_IIDIS );
386 if( constraint )
387 print_proxy( "_StubMsg.MaxCount = (unsigned long) ( %s );\n",constraint->name);
388 print_proxy( "NdrClearOutParameters( &_StubMsg, ");
389 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], ", index );
390 fprintf(proxy, "(void*)%s );\n", arg->name );
391 break;
393 default:
394 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
397 arg = PREV_LINK(arg);
401 static void gen_proxy(type_t *iface, func_t *cur, int idx)
403 var_t *def = cur->def;
404 int has_ret = !is_void(def->type, def);
406 indent = 0;
407 write_type(proxy, def->type, def, def->tname);
408 print_proxy( " STDMETHODCALLTYPE %s_", iface->name);
409 write_name(proxy, def);
410 print_proxy( "_Proxy(\n");
411 write_args(proxy, cur->args, iface->name, 1, TRUE);
412 print_proxy( ")\n");
413 print_proxy( "{\n");
414 indent ++;
415 /* local variables */
416 if (has_ret) {
417 print_proxy( "" );
418 write_type(proxy, def->type, def, def->tname);
419 print_proxy( " _RetVal;\n");
421 print_proxy( "RPC_MESSAGE _Msg;\n" );
422 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n" );
423 print_proxy( "\n");
425 /* FIXME: trace */
426 clear_output_vars( cur->args );
428 print_proxy( "RpcTryExcept\n" );
429 print_proxy( "{\n" );
430 indent++;
431 print_proxy( "NdrProxyInitialize(This, &_Msg, &_StubMsg, &Object_StubDesc, %d);\n", idx);
432 proxy_check_pointers( cur->args );
434 print_proxy( "RpcTryFinally\n" );
435 print_proxy( "{\n" );
436 indent++;
438 gen_marshall( cur->args );
440 print_proxy( "NdrProxySendReceive(This, &_StubMsg);\n" );
441 fprintf(proxy, "\n");
442 print_proxy("if ((_Msg.DataRepresentation&0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
443 indent++;
444 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[0]);\n" );
445 indent--;
446 fprintf(proxy, "\n");
448 gen_unmarshall( cur->args );
449 if (has_ret) {
451 * FIXME: We only need to round the buffer up if it could be unaligned...
452 * We should calculate how much buffer we used and output the following
453 * line only if necessary.
455 print_proxy( "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + 3) & ~ 0x3);\n");
457 print_proxy( "_RetVal = *((" );
458 write_type(proxy, def->type, def, def->tname);
459 fprintf(proxy, "*)_StubMsg.Buffer)++;\n");
462 indent--;
463 print_proxy( "}\n");
464 print_proxy( "RpcFinally\n" );
465 print_proxy( "{\n" );
466 indent++;
467 print_proxy( "NdrProxyFreeBuffer(This, &_StubMsg);\n" );
468 indent--;
469 print_proxy( "}\n");
470 print_proxy( "RpcEndFinally\n" );
471 indent--;
472 print_proxy( "}\n" );
473 print_proxy( "RpcExcept(_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
474 print_proxy( "{\n" );
475 if (has_ret) {
476 indent++;
477 proxy_free_variables( cur->args );
478 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
479 indent--;
481 print_proxy( "}\n" );
482 print_proxy( "RpcEndExcept\n" );
484 if (has_ret) {
485 print_proxy( "return _RetVal;\n" );
487 indent--;
488 print_proxy( "}\n");
489 print_proxy( "\n");
492 static void stub_write_locals( var_t *arg )
494 int n = 0;
495 while (arg) {
496 int outptr = is_attr(arg->attrs, ATTR_OUT);
498 /* create a temporary variable to store the output */
499 if (outptr) {
500 var_t temp;
501 memset( &temp, 0, sizeof temp );
502 temp.ptr_level = arg->ptr_level - 1; /* dereference once */
503 print_proxy("");
504 write_type(proxy, arg->type, &temp, arg->tname);
505 fprintf(proxy, " _M%d;\n",n++);
507 print_proxy("");
508 write_type(proxy, arg->type, arg, arg->tname);
509 fprintf(proxy, " ");
510 write_name(proxy, arg);
511 fprintf(proxy, ";\n");
512 arg = NEXT_LINK(arg);
516 static void stub_unmarshall( var_t *arg )
518 int n = 0;
519 END_OF_LIST(arg);
520 while (arg) {
521 type_t *type = get_base_type(arg->type);
522 if (is_attr(arg->attrs, ATTR_IN)) {
523 int index = 0;
525 switch( type->type )
527 case RPC_FC_BYTE:
528 case RPC_FC_CHAR:
529 case RPC_FC_WCHAR:
530 case RPC_FC_SHORT:
531 case RPC_FC_USHORT:
532 case RPC_FC_ENUM16:
533 case RPC_FC_LONG:
534 case RPC_FC_ULONG:
535 case RPC_FC_ENUM32:
536 print_proxy( "%s = *((", arg->name);
537 write_type(proxy, arg->type, arg, arg->tname);
538 fprintf(proxy,"*)_StubMsg.Buffer)++;\n");
539 break;
541 case RPC_FC_FP:
542 case RPC_FC_STRUCT:
543 /* FIXME: add the format string, and set the index below */
544 print_proxy( "NdrSimpleStructUnmarshall(&_StubMsg, (unsigned char**)&%s, ", arg->name );
545 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
546 break;
548 default:
549 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
552 else if (is_attr(arg->attrs, ATTR_OUT)) {
553 switch( type->type )
555 case RPC_FC_STRUCT:
556 print_proxy("MIDL_memset(");
557 write_name(proxy, arg);
558 fprintf(proxy,", 0, sizeof(");
559 write_type(proxy, arg->type, arg, arg->tname);
560 fprintf(proxy,"));\n");
561 break;
562 default:
563 print_proxy("");
564 write_name(proxy, arg);
565 fprintf(proxy," = &_M%d;\n", n);
566 print_proxy("_M%d = 0;\n", n++);
567 break;
570 arg = PREV_LINK(arg);
574 static void stub_gen_marshall_size( var_t *arg )
576 print_proxy( "_StubMsg.BufferLength = 0U;\n" );
578 END_OF_LIST(arg);
579 while (arg) {
580 if (is_attr(arg->attrs, ATTR_OUT)) {
581 int index = 0;
582 var_t *constraint;
583 type_t *type = get_base_type(arg->type);
585 switch( type->type )
587 case RPC_FC_BYTE:
588 case RPC_FC_CHAR:
589 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 1, arg->name );
590 break;
592 case RPC_FC_WCHAR:
593 case RPC_FC_SHORT:
594 case RPC_FC_USHORT:
595 case RPC_FC_ENUM16:
596 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 2, arg->name );
597 break;
599 case RPC_FC_LONG:
600 case RPC_FC_ULONG:
601 case RPC_FC_ENUM32:
602 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 4, arg->name );
603 break;
605 case RPC_FC_FP:
606 print_proxy( "/*FIXME %s*/\n", arg->name );
607 break;
608 case RPC_FC_STRUCT:
609 print_proxy( "NdrSimpleStructBufferSize(&_StubMsg, (unsigned char*)%s, ", arg->name );
610 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
611 break;
613 case RPC_FC_IP:
614 constraint = get_attrp( arg->attrs, ATTR_IIDIS );
615 if( constraint )
616 print_proxy( "_StubMsg.MaxCount = (unsigned long) ( %s );\n",constraint->name);
617 print_proxy( "NdrPointerBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
618 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
619 break;
621 default:
622 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
625 arg = PREV_LINK(arg);
629 static void stub_gen_marshall_copydata( var_t *arg )
631 END_OF_LIST(arg);
632 while (arg) {
633 if (is_attr(arg->attrs, ATTR_OUT)) {
634 var_t *constraint;
635 int index = 0;
636 type_t *type = get_base_type(arg->type);
638 if( arg->array )
640 fprintf(stderr,"FIXME: param %s is an array\n", arg->name);
642 else switch( type->type )
644 case RPC_FC_BYTE:
645 case RPC_FC_CHAR:
646 case RPC_FC_WCHAR:
647 case RPC_FC_SHORT:
648 case RPC_FC_USHORT:
649 case RPC_FC_ENUM16:
650 case RPC_FC_LONG:
651 case RPC_FC_ULONG:
652 case RPC_FC_ENUM32:
653 print_proxy( "*((");
654 write_type(proxy, arg->type, arg, arg->tname);
655 fprintf(proxy,"*)_StubMsg.Buffer)++ = %s;\n", arg->name );
656 break;
658 case RPC_FC_STRUCT:
659 /* FIXME: add the format string, and set the index below */
660 print_proxy( "NdrSimpleStructMarshall(&_StubMsg, (unsigned char*)%s, ", arg->name );
661 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
662 break;
664 case RPC_FC_FP:
665 print_proxy( "NdrConformantArrayBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
666 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
667 break;
669 case RPC_FC_IP:
670 constraint = get_attrp( arg->attrs, ATTR_IIDIS );
671 if( constraint )
672 print_proxy( "_StubMsg.MaxCount = (unsigned long) ( %s );\n",constraint->name);
673 print_proxy( "NdrPointerMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name );
674 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
675 break;
677 default:
678 printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
681 arg = PREV_LINK(arg);
685 static void stub_genmarshall( var_t *args )
687 /* FIXME: size buffer */
688 stub_gen_marshall_size( args );
690 print_proxy("NdrStubGetBuffer(This, pRpcChannelBuffer, &_StubMsg);\n");
692 stub_gen_marshall_copydata( args );
695 static void gen_stub(type_t *iface, func_t *cur, char *cas)
697 var_t *def = cur->def;
698 var_t *arg;
699 int has_ret = !is_void(def->type, def);
701 indent = 0;
702 print_proxy( "void __RPC_STUB %s_", iface->name);
703 write_name(proxy, def);
704 print_proxy( "_Stub(\n");
705 indent++;
706 print_proxy( "IRpcStubBuffer* This,\n");
707 print_proxy( "IRpcChannelBuffer* pRpcChannelBuffer,\n");
708 print_proxy( "PRPC_MESSAGE _Msg,\n");
709 print_proxy( "DWORD* _pdwStubPhase)\n");
710 indent--;
711 print_proxy( "{\n");
712 indent++;
713 /* local variables */
714 if (has_ret) {
715 print_proxy("");
716 write_type(proxy, def->type, def, def->tname);
717 fprintf(proxy, " _RetVal;\n");
719 print_proxy("%s * _This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n", iface->name, iface->name);
720 print_proxy("MIDL_STUB_MESSAGE _StubMsg;\n");
721 stub_write_locals( cur->args );
722 fprintf(proxy, "\n");
724 /* FIXME: trace */
726 print_proxy("NdrStubInitialize(_Msg, &_StubMsg, &Object_StubDesc, pRpcChannelBuffer);\n");
727 fprintf(proxy, "\n");
729 print_proxy("RpcTryFinally\n");
730 print_proxy("{\n");
731 indent++;
732 print_proxy("if ((_Msg->DataRepresentation&0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
733 indent++;
734 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[0]);\n" );
735 indent--;
736 fprintf(proxy, "\n");
738 stub_unmarshall( cur->args );
739 fprintf(proxy, "\n");
741 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
742 fprintf(proxy, "\n");
743 print_proxy("");
744 if (has_ret) fprintf(proxy, "_RetVal = ");
745 fprintf(proxy, "%s_", iface->name);
746 if (cas) fprintf(proxy, "%s_Stub", cas);
747 else write_name(proxy, def);
748 fprintf(proxy, "(_This");
749 arg = cur->args;
750 if (arg) {
751 END_OF_LIST(arg);
752 while (arg) {
753 fprintf(proxy, ", ");
754 write_name(proxy, arg);
755 arg = PREV_LINK(arg);
758 fprintf(proxy, ");\n");
759 fprintf(proxy, "\n");
760 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
761 fprintf(proxy, "\n");
763 stub_genmarshall( cur->args );
764 fprintf(proxy, "\n");
766 if (has_ret) {
768 * FIXME: We only need to round the buffer up if it could be unaligned...
769 * We should calculate how much buffer we used and output the following
770 * line only if necessary.
772 print_proxy( "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + 3) & ~ 0x3);\n");
774 print_proxy( "*((" );
775 write_type(proxy, def->type, def, def->tname);
776 fprintf(proxy, "*)_StubMsg.Buffer)++ = _RetVal;\n");
779 indent--;
780 print_proxy("}\n");
781 print_proxy("RpcFinally\n");
782 print_proxy("{\n");
783 print_proxy("}\n");
784 print_proxy("RpcEndFinally\n");
786 print_proxy("_Msg->BufferLength = ((long)_StubMsg.Buffer - (long)_Msg->Buffer);\n");
787 indent--;
789 print_proxy("}\n");
790 print_proxy("\n");
793 static int write_proxy_methods(type_t *iface)
795 func_t *cur = iface->funcs;
796 int i = 0;
798 END_OF_LIST(cur);
800 if (iface->ref) i = write_proxy_methods(iface->ref);
801 while (cur) {
802 var_t *def = cur->def;
803 if (!is_callas(def->attrs)) {
804 if (i) fprintf(proxy, ",\n");
805 print_proxy( "%s_", iface->name);
806 write_name(proxy, def);
807 fprintf(proxy, "_Proxy");
808 i++;
810 cur = PREV_LINK(cur);
812 return i;
815 static int write_stub_methods(type_t *iface)
817 func_t *cur = iface->funcs;
818 int i = 0;
820 END_OF_LIST(cur);
822 if (iface->ref) i = write_stub_methods(iface->ref);
823 else return i; /* skip IUnknown */
824 while (cur) {
825 var_t *def = cur->def;
826 if (!is_local(def->attrs)) {
827 if (i) fprintf(proxy,",\n");
828 print_proxy( "%s_", iface->name);
829 write_name(proxy, def);
830 fprintf(proxy, "_Stub");
831 i++;
833 cur = PREV_LINK(cur);
835 return i;
838 static void write_proxy(type_t *iface)
840 int midx = -1, stubs;
841 func_t *cur = iface->funcs;
843 if (!cur) return;
845 END_OF_LIST(cur);
847 /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
849 fprintf(proxy, "/*****************************************************************************\n");
850 fprintf(proxy, " * %s interface\n", iface->name);
851 fprintf(proxy, " */\n");
852 while (cur) {
853 var_t *def = cur->def;
854 if (!is_local(def->attrs)) {
855 var_t *cas = is_callas(def->attrs);
856 char *cname = cas ? cas->name : NULL;
857 int idx = cur->idx;
858 if (cname) {
859 func_t *m = iface->funcs;
860 while (m && strcmp(get_name(m->def), cname))
861 m = NEXT_LINK(m);
862 idx = m->idx;
864 gen_proxy(iface, cur, idx);
865 gen_stub(iface, cur, cname);
866 if (midx == -1) midx = idx;
867 else if (midx != idx) yyerror("method index mismatch in write_proxy");
868 midx++;
870 cur = PREV_LINK(cur);
873 /* proxy vtable */
874 print_proxy( "const CINTERFACE_PROXY_VTABLE(%d) %sProxyVtbl =\n", midx, iface->name);
875 print_proxy( "{\n");
876 indent++;
877 print_proxy( "{&IID_%s},{\n", iface->name);
878 write_proxy_methods(iface);
879 fprintf(proxy, "}\n");
880 indent--;
881 print_proxy( "};\n");
882 print_proxy( "\n");
884 /* stub vtable */
885 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
886 print_proxy( "{\n");
887 indent++;
888 stubs = write_stub_methods(iface);
889 fprintf(proxy, "\n");
890 indent--;
891 fprintf(proxy, "};\n");
892 print_proxy( "\n");
893 print_proxy( "const CInterfaceStubVtbl %sStubVtbl = {\n", iface->name);
894 indent++;
895 print_proxy( "{&IID_%s,\n", iface->name);
896 print_proxy( " 0,\n");
897 print_proxy( " %d,\n", stubs+3);
898 print_proxy( " &%s_table[-3]},\n", iface->name);
899 print_proxy( "{CStdStubBuffer_METHODS}\n");
900 indent--;
901 print_proxy( "};\n");
902 print_proxy( "\n");
905 void write_proxies(ifref_t *ifaces)
907 ifref_t *lcur = ifaces;
908 ifref_t *cur;
909 char *file_id = proxy_token;
910 int c;
912 if (!do_everything) return;
913 if (!lcur) return;
914 END_OF_LIST(lcur);
916 init_proxy();
918 cur = lcur;
919 while (cur) {
920 if (is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
921 write_proxy(cur->iface);
922 cur = PREV_LINK(cur);
925 if (!proxy) return;
927 write_stubdesc();
929 print_proxy( "#if !defined(__RPC_WIN32__)\n");
930 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
931 print_proxy( "#endif\n");
932 print_proxy( "\n");
933 write_formatstring( 1 );
934 write_formatstring( 0 );
936 fprintf(proxy, "const CInterfaceProxyVtbl* _%s_ProxyVtblList[] =\n", file_id);
937 fprintf(proxy, "{\n");
938 cur = lcur;
939 while (cur) {
940 if(cur->iface->ref && cur->iface->funcs &&
941 is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
942 fprintf(proxy, " (CInterfaceProxyVtbl*)&%sProxyVtbl,\n", cur->iface->name);
943 cur = PREV_LINK(cur);
945 fprintf(proxy, " 0\n");
946 fprintf(proxy, "};\n");
947 fprintf(proxy, "\n");
949 fprintf(proxy, "const CInterfaceStubVtbl* _%s_StubVtblList[] =\n", file_id);
950 fprintf(proxy, "{\n");
951 cur = lcur;
952 while (cur) {
953 if(cur->iface->ref && cur->iface->funcs &&
954 is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
955 fprintf(proxy, " (CInterfaceStubVtbl*)&%sStubVtbl,\n", cur->iface->name);
956 cur = PREV_LINK(cur);
958 fprintf(proxy, " 0\n");
959 fprintf(proxy, "};\n");
960 fprintf(proxy, "\n");
962 fprintf(proxy, "const PCInterfaceName _%s_InterfaceNamesList[] =\n", file_id);
963 fprintf(proxy, "{\n");
964 cur = lcur;
965 while (cur) {
966 if(cur->iface->ref && cur->iface->funcs &&
967 is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
968 fprintf(proxy, " \"%s\",\n", cur->iface->name);
969 cur = PREV_LINK(cur);
971 fprintf(proxy, " 0\n");
972 fprintf(proxy, "};\n");
973 fprintf(proxy, "\n");
975 fprintf(proxy, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_%s, pIID, n)\n", file_id, file_id);
976 fprintf(proxy, "\n");
977 fprintf(proxy, "int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
978 fprintf(proxy, "{\n");
979 cur = lcur;
980 c = 0;
981 while (cur) {
982 if(cur->iface->ref)
984 fprintf(proxy, " if (!_%s_CHECK_IID(%d)) {\n", file_id, c);
985 fprintf(proxy, " *pIndex = %d;\n", c);
986 fprintf(proxy, " return 1;\n");
987 fprintf(proxy, " }\n");
988 c++;
990 cur = PREV_LINK(cur);
992 fprintf(proxy, " return 0;\n");
993 fprintf(proxy, "}\n");
994 fprintf(proxy, "\n");
996 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id);
997 fprintf(proxy, "{\n");
998 fprintf(proxy, " (PCInterfaceProxyVtblList*)&_%s_ProxyVtblList,\n", file_id);
999 fprintf(proxy, " (PCInterfaceStubVtblList*)&_%s_StubVtblList,\n", file_id);
1000 fprintf(proxy, " (const PCInterfaceName*)&_%s_InterfaceNamesList,\n", file_id);
1001 fprintf(proxy, " 0,\n");
1002 fprintf(proxy, " &_%s_IID_Lookup,\n", file_id);
1003 fprintf(proxy, " %d,\n", c);
1004 fprintf(proxy, " 1,\n");
1005 fprintf(proxy, " 0,\n");
1006 fprintf(proxy, " 0,\n");
1007 fprintf(proxy, " 0,\n");
1008 fprintf(proxy, " 0\n");
1009 fprintf(proxy, "};\n");
1011 fclose(proxy);