widl: Fix out-only temporary variable generation.
[wine/multimedia.git] / tools / widl / proxy.c
blobf060a23772195646594150842b1741b118410d1e
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
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 <assert.h>
32 #include <ctype.h>
33 #include <signal.h>
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
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)
48 static FILE* proxy;
49 static int indent = 0;
51 /* FIXME: support generation of stubless proxies */
53 static int print_proxy( const char *format, ... )
55 va_list va;
56 int i, r;
58 va_start( va, format );
59 if ( format[0] != '\n' )
60 for( i=0; i<indent; i++ )
61 fprintf( proxy, " " );
62 r = vfprintf( proxy, format, va );
63 va_end( va );
64 return r;
68 static type_t *get_base_type( var_t *arg )
70 type_t *t = arg->type;
71 while( (t->type == 0) && t->ref )
72 t = t->ref;
73 return t;
76 static void write_stubdescproto(void)
78 print_proxy( "extern const MIDL_STUB_DESC Object_StubDesc;\n");
79 print_proxy( "\n");
82 static void write_stubdesc(void)
84 print_proxy( "const MIDL_STUB_DESC Object_StubDesc = {\n");
85 print_proxy( " 0,\n");
86 print_proxy( " NdrOleAllocate,\n");
87 print_proxy( " NdrOleFree,\n");
88 print_proxy( " {0}, 0, 0, 0, 0,\n");
89 print_proxy( " 0 /* __MIDL_TypeFormatString.Format */\n");
90 print_proxy( "};\n");
91 print_proxy( "\n");
94 static void write_formatdesc( const char *str )
96 print_proxy( "typedef struct _MIDL_%s_FORMAT_STRING\n", str );
97 indent++;
98 print_proxy( "{\n");
99 print_proxy( "short Pad;\n");
100 print_proxy( "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
101 indent--;
102 print_proxy( "} MIDL_%s_FORMAT_STRING;\n", str);
103 print_proxy( "\n");
106 static void write_formatstringsdecl(void)
108 print_proxy( "#define TYPE_FORMAT_STRING_SIZE %d\n",1); /* FIXME */
109 print_proxy( "#define PROC_FORMAT_STRING_SIZE %d\n",1); /* FIXME */
110 fprintf(proxy, "\n");
111 write_formatdesc( "TYPE" );
112 write_formatdesc( "PROC" );
113 fprintf(proxy, "\n");
114 print_proxy( "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
115 print_proxy( "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
116 print_proxy( "\n");
119 static void write_formatstring( int proc )
121 const char *t, *n;
122 if( !proc )
124 t = "TYPE";
125 n = "Type";
127 else
129 t = "PROC";
130 n = "Proc";
132 print_proxy( "static const MIDL_%s_FORMAT_STRING __MIDL_%sFormatString =\n", t, n);
133 print_proxy( "{\n");
134 indent++;
135 print_proxy( "0,\n");
136 print_proxy( "{\n");
137 indent++;
138 print_proxy( "0\n");
139 indent--;
140 print_proxy( "}\n");
141 indent--;
142 print_proxy( "};\n");
143 print_proxy( "\n");
146 static void init_proxy(void)
148 if (proxy) return;
149 if(!(proxy = fopen(proxy_name, "w")))
150 error("Could not open %s for output\n", proxy_name);
151 print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
152 print_proxy( "\n");
153 print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
154 print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ 440\n");
155 print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
156 print_proxy( "\n");
157 print_proxy( "#include \"rpcproxy.h\"\n");
158 print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
159 print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
160 print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
161 print_proxy( "\n");
162 print_proxy( "#include \"%s\"\n", header_name);
163 print_proxy( "\n");
164 write_formatstringsdecl();
165 write_stubdescproto();
168 static void clear_output_vars( var_t *arg )
170 END_OF_LIST(arg);
171 while (arg) {
172 if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
173 print_proxy( "if(%s)\n", arg->name );
174 indent++;
175 print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name );
176 indent--;
178 arg = PREV_LINK(arg);
182 int is_pointer(var_t *arg)
184 if (arg->ptr_level)
185 return 1;
187 switch (ref_type(arg->type))
189 case RPC_FC_RP:
190 case RPC_FC_C_CSTRING:
191 case RPC_FC_C_WSTRING:
192 case RPC_FC_FP:
193 case RPC_FC_OP:
194 case RPC_FC_UP:
195 return 1;
198 return 0;
201 int cant_be_null(var_t *v)
203 /* Search backwards for the most recent pointer attribute. */
204 const attr_t *attrs = v->attrs;
205 const type_t *type = v->type;
207 if (! attrs && type)
209 attrs = type->attrs;
210 type = type->ref;
213 while (attrs)
215 int t = get_attrv(attrs, ATTR_POINTERTYPE);
217 if (t == RPC_FC_FP || t == RPC_FC_OP || t == RPC_FC_UP)
218 return 0;
220 if (t == RPC_FC_RP)
221 return 1;
223 if (type)
225 attrs = type->attrs;
226 type = type->ref;
228 else
229 attrs = NULL;
232 return 1; /* Default is RPC_FC_RP. */
235 static void proxy_check_pointers( var_t *arg )
237 END_OF_LIST(arg);
238 while (arg) {
239 if (is_pointer(arg) && cant_be_null(arg)) {
240 print_proxy( "if(!%s)\n", arg->name );
241 indent++;
242 print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
243 indent--;
245 arg = PREV_LINK(arg);
249 static void marshall_size_arg( var_t *arg )
251 int index = 0;
252 const type_t *type = get_base_type(arg);
253 expr_t *expr;
255 expr = get_attrp( arg->attrs, ATTR_SIZEIS );
256 if (expr)
258 print_proxy( "_StubMsg.MaxCount = ", arg->name );
259 write_expr(proxy, expr, 0);
260 fprintf(proxy, ";\n\n");
261 print_proxy( "NdrConformantArrayBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
262 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
263 return;
266 switch( type->type )
268 case RPC_FC_BYTE:
269 case RPC_FC_CHAR:
270 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 1, arg->name );
271 break;
273 case RPC_FC_WCHAR:
274 case RPC_FC_SHORT:
275 case RPC_FC_USHORT:
276 case RPC_FC_ENUM16:
277 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 2, arg->name );
278 break;
280 case RPC_FC_LONG:
281 case RPC_FC_ULONG:
282 case RPC_FC_ENUM32:
283 print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 4, arg->name );
284 break;
286 case RPC_FC_STRUCT:
287 print_proxy( "NdrSimpleStructBufferSize(&_StubMsg, (unsigned char*)%s, ", arg->name );
288 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
289 break;
291 case RPC_FC_C_CSTRING:
292 case RPC_FC_C_WSTRING:
293 case RPC_FC_CARRAY:
294 print_proxy( "NdrConformantArrayBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
295 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
296 break;
298 case RPC_FC_BOGUS_STRUCT:
299 print_proxy( "NdrComplexStructBufferSize(&_StubMsg, (unsigned char*)%s, ", arg->name );
300 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
301 break;
303 case RPC_FC_FP:
305 var_t temp;
306 memset( &temp, 0, sizeof temp );
307 temp.type = type->ref;
308 temp.name = arg->name; /* FIXME */
309 #if 0
310 print_proxy( "/* FIXME: %s use the right name for %s */\n", __FUNCTION__, arg->name );
311 #endif
312 marshall_size_arg( &temp );
314 break;
316 case RPC_FC_IP:
317 print_proxy( "NdrPointerBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
318 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
319 break;
321 default:
322 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
326 static void proxy_gen_marshall_size( var_t *arg )
328 print_proxy( "_StubMsg.BufferLength = 0U;\n" );
330 END_OF_LIST(arg);
331 while (arg) {
332 if (is_attr(arg->attrs, ATTR_IN))
334 marshall_size_arg( arg );
335 fprintf(proxy, "\n");
337 arg = PREV_LINK(arg);
341 static void marshall_copy_arg( var_t *arg )
343 int index = 0;
344 type_t *type = get_base_type(arg);
345 expr_t *expr;
347 expr = get_attrp( arg->attrs, ATTR_SIZEIS );
348 if (expr)
350 print_proxy( "_StubMsg.MaxCount = ", arg->name );
351 write_expr(proxy, expr, 0);
352 fprintf(proxy, ";\n\n");
353 print_proxy( "NdrConformantArrayMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name );
354 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
355 return;
358 switch( type->type )
360 case RPC_FC_BYTE:
361 case RPC_FC_CHAR:
362 case RPC_FC_WCHAR:
363 case RPC_FC_SHORT:
364 case RPC_FC_USHORT:
365 case RPC_FC_ENUM16:
366 case RPC_FC_LONG:
367 case RPC_FC_ULONG:
368 case RPC_FC_ENUM32:
369 print_proxy( "*(");
370 write_type(proxy, arg->type, arg, arg->tname);
371 fprintf(proxy, " *)_StubMsg.Buffer = %s;\n", arg->name );
372 print_proxy("_StubMsg.Buffer += sizeof(");
373 write_type(proxy, arg->type, arg, arg->tname);
374 fprintf(proxy, ");\n");
375 break;
377 case RPC_FC_STRUCT:
378 /* FIXME: add the format string, and set the index below */
379 print_proxy( "NdrSimpleStructMarshall(&_StubMsg, (unsigned char*)%s, ", arg->name );
380 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
381 break;
383 case RPC_FC_C_CSTRING:
384 case RPC_FC_C_WSTRING:
385 case RPC_FC_CARRAY:
386 break;
388 case RPC_FC_BOGUS_STRUCT:
389 print_proxy( "NdrComplexStructMarshall(&_StubMsg, (unsigned char*)%s, ", arg->name );
390 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
391 break;
393 case RPC_FC_FP:
395 var_t temp;
396 memset( &temp, 0, sizeof temp );
397 temp.type = type->ref;
398 temp.name = arg->name; /* FIXME */
399 #if 0
400 print_proxy( "/* FIXME: %s use the right name for %s */\n", __FUNCTION__, arg->name );
401 #endif
402 marshall_copy_arg( &temp );
404 break;
406 case RPC_FC_IP:
407 print_proxy( "NdrPointerMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name );
408 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
409 break;
411 default:
412 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
416 static void gen_marshall_copydata( var_t *arg )
418 END_OF_LIST(arg);
419 while (arg) {
420 if (is_attr(arg->attrs, ATTR_IN))
422 marshall_copy_arg( arg );
423 fprintf(proxy, "\n");
425 arg = PREV_LINK(arg);
429 static void gen_marshall( var_t *arg )
431 /* generated code to determine the size of the buffer required */
432 proxy_gen_marshall_size( arg );
434 /* generated code to allocate the buffer */
435 print_proxy( "NdrProxyGetBuffer(This, &_StubMsg);\n" );
437 /* generated code to copy the args into the buffer */
438 gen_marshall_copydata( arg );
440 print_proxy( "\n");
443 static void unmarshall_copy_arg( var_t *arg )
445 int index = 0;
446 type_t *type = get_base_type(arg);
447 expr_t *expr;
449 expr = get_attrp( arg->attrs, ATTR_SIZEIS );
450 if (expr)
452 print_proxy( "NdrConformantArrayUnmarshall( &_StubMsg, (unsigned char**)&%s, ", arg->name );
453 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
454 return;
457 switch( type->type )
459 case RPC_FC_BYTE:
460 case RPC_FC_CHAR:
461 case RPC_FC_WCHAR:
462 case RPC_FC_SHORT:
463 case RPC_FC_USHORT:
464 case RPC_FC_ENUM16:
465 case RPC_FC_LONG:
466 case RPC_FC_ULONG:
467 case RPC_FC_ENUM32:
468 print_proxy( "%s = *(", arg->name );
469 write_type(proxy, arg->type, arg, arg->tname);
470 fprintf(proxy," *)_StubMsg.Buffer;\n");
471 print_proxy("_StubMsg.Buffer += sizeof(");
472 write_type(proxy, arg->type, arg, arg->tname);
473 fprintf(proxy, ");\n");
474 break;
476 case RPC_FC_STRUCT:
477 print_proxy( "NdrSimpleStructUnmarshall(&_StubMsg, (unsigned char**)%s, ", arg->name );
478 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
479 break;
481 case RPC_FC_C_CSTRING:
482 case RPC_FC_C_WSTRING:
483 case RPC_FC_CARRAY:
484 print_proxy( "NdrConformantArrayUnmarshall( &_StubMsg, (unsigned char**)&%s, ", arg->name );
485 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
486 break;
488 case RPC_FC_BOGUS_STRUCT:
489 print_proxy( "NdrComplexStructUnmarshall(&_StubMsg, (unsigned char**)&%s, ", arg->name );
490 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0 );\n", index );
491 break;
493 case RPC_FC_FP:
495 var_t temp;
496 memset( &temp, 0, sizeof temp );
497 temp.type = type->ref;
498 temp.name = arg->name; /* FIXME */
499 #if 1
500 print_proxy( "/* FIXME: %s use the right name for %s */\n", __FUNCTION__, arg->name );
501 #endif
502 unmarshall_copy_arg( &temp );
504 break;
506 case RPC_FC_IP:
507 print_proxy( "NdrPointerUnmarshall(&_StubMsg, (unsigned char**)&%s, ", arg->name );
508 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
509 break;
511 default:
512 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
516 static void gen_unmarshall( var_t *arg )
518 END_OF_LIST(arg);
519 while (arg) {
520 if (is_attr(arg->attrs, ATTR_OUT))
522 unmarshall_copy_arg( arg );
523 fprintf(proxy, "\n");
525 arg = PREV_LINK(arg);
529 static void free_variable( var_t *arg )
531 var_t *constraint;
532 int index = 0; /* FIXME */
533 type_t *type;
534 expr_t *expr;
536 expr = get_attrp( arg->attrs, ATTR_SIZEIS );
537 if (expr)
539 print_proxy( "_StubMsg.MaxCount = ", arg->name );
540 write_expr(proxy, expr, 0);
541 fprintf(proxy, ";\n\n");
542 print_proxy( "NdrClearOutParameters( &_StubMsg, ");
543 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], ", index );
544 fprintf(proxy, "(void*)%s );\n", arg->name );
545 return;
548 type = get_base_type(arg);
549 switch( type->type )
551 case RPC_FC_BYTE:
552 case RPC_FC_CHAR:
553 case RPC_FC_WCHAR:
554 case RPC_FC_SHORT:
555 case RPC_FC_USHORT:
556 case RPC_FC_ENUM16:
557 case RPC_FC_LONG:
558 case RPC_FC_ULONG:
559 case RPC_FC_ENUM32:
560 case RPC_FC_STRUCT:
561 break;
563 case RPC_FC_FP:
564 case RPC_FC_IP:
565 constraint = get_attrp( arg->attrs, ATTR_IIDIS );
566 if( constraint )
567 print_proxy( "_StubMsg.MaxCount = (unsigned long) ( %s );\n",constraint->name);
568 print_proxy( "NdrClearOutParameters( &_StubMsg, ");
569 fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], ", index );
570 fprintf(proxy, "(void*)%s );\n", arg->name );
571 break;
573 default:
574 print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
578 static void proxy_free_variables( var_t *arg )
580 END_OF_LIST(arg);
581 while (arg) {
582 if (is_attr(arg->attrs, ATTR_OUT))
584 free_variable( arg );
585 fprintf(proxy, "\n");
587 arg = PREV_LINK(arg);
591 static void gen_proxy(type_t *iface, func_t *cur, int idx)
593 var_t *def = cur->def;
594 int has_ret = !is_void(def->type, def);
596 indent = 0;
597 write_type(proxy, def->type, def, def->tname);
598 print_proxy( " STDMETHODCALLTYPE %s_", iface->name);
599 write_name(proxy, def);
600 print_proxy( "_Proxy(\n");
601 write_args(proxy, cur->args, iface->name, 1, TRUE);
602 print_proxy( ")\n");
603 print_proxy( "{\n");
604 indent ++;
605 /* local variables */
606 if (has_ret) {
607 print_proxy( "" );
608 write_type(proxy, def->type, def, def->tname);
609 print_proxy( " _RetVal;\n");
611 print_proxy( "RPC_MESSAGE _Msg;\n" );
612 print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n" );
613 print_proxy( "\n");
615 /* FIXME: trace */
616 clear_output_vars( cur->args );
618 print_proxy( "RpcTryExcept\n" );
619 print_proxy( "{\n" );
620 indent++;
621 print_proxy( "NdrProxyInitialize(This, &_Msg, &_StubMsg, &Object_StubDesc, %d);\n", idx);
622 proxy_check_pointers( cur->args );
624 print_proxy( "RpcTryFinally\n" );
625 print_proxy( "{\n" );
626 indent++;
628 gen_marshall( cur->args );
630 print_proxy( "NdrProxySendReceive(This, &_StubMsg);\n" );
631 fprintf(proxy, "\n");
632 print_proxy("if ((_Msg.DataRepresentation&0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
633 indent++;
634 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[0]);\n" );
635 indent--;
636 fprintf(proxy, "\n");
638 gen_unmarshall( cur->args );
639 if (has_ret) {
641 * FIXME: We only need to round the buffer up if it could be unaligned...
642 * We should calculate how much buffer we used and output the following
643 * line only if necessary.
645 print_proxy( "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + 3) & ~ 0x3);\n");
647 print_proxy( "_RetVal = *(" );
648 write_type(proxy, def->type, def, def->tname);
649 fprintf(proxy, " *)_StubMsg.Buffer;\n");
650 print_proxy("_StubMsg.Buffer += sizeof(");
651 write_type(proxy, def->type, def, def->tname);
652 fprintf(proxy, ");\n");
655 indent--;
656 print_proxy( "}\n");
657 print_proxy( "RpcFinally\n" );
658 print_proxy( "{\n" );
659 indent++;
660 print_proxy( "NdrProxyFreeBuffer(This, &_StubMsg);\n" );
661 indent--;
662 print_proxy( "}\n");
663 print_proxy( "RpcEndFinally\n" );
664 indent--;
665 print_proxy( "}\n" );
666 print_proxy( "RpcExcept(_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
667 print_proxy( "{\n" );
668 if (has_ret) {
669 indent++;
670 proxy_free_variables( cur->args );
671 print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
672 indent--;
674 print_proxy( "}\n" );
675 print_proxy( "RpcEndExcept\n" );
677 if (has_ret) {
678 print_proxy( "return _RetVal;\n" );
680 indent--;
681 print_proxy( "}\n");
682 print_proxy( "\n");
685 static void stub_write_locals( var_t *arg )
687 int n = 0;
688 END_OF_LIST(arg);
689 while (arg) {
690 int outptr = is_attr(arg->attrs, ATTR_OUT)
691 && ! is_attr(arg->attrs, ATTR_IN);
693 /* create a temporary variable to store the output */
694 if (outptr) {
695 var_t temp;
696 memset( &temp, 0, sizeof temp );
697 temp.ptr_level = arg->ptr_level - 1; /* dereference once */
698 print_proxy("");
699 write_type(proxy, arg->type, &temp, arg->tname);
700 fprintf(proxy, " _M%d;\n",n++);
702 print_proxy("");
703 write_type(proxy, arg->type, arg, arg->tname);
704 fprintf(proxy, " ");
705 write_name(proxy, arg);
706 fprintf(proxy, ";\n");
707 arg = PREV_LINK(arg);
711 static void stub_unmarshall( var_t *arg )
713 int n = 0;
714 END_OF_LIST(arg);
715 while (arg) {
716 if (is_attr(arg->attrs, ATTR_IN))
718 unmarshall_copy_arg( arg );
719 fprintf(proxy,"\n");
721 else if (is_attr(arg->attrs, ATTR_OUT)) {
722 type_t *type = get_base_type(arg);
723 switch( type->type )
725 case RPC_FC_STRUCT:
726 print_proxy("MIDL_memset(");
727 write_name(proxy, arg);
728 fprintf(proxy,", 0, sizeof(");
729 write_type(proxy, arg->type, arg, arg->tname);
730 fprintf(proxy,"));\n");
731 break;
732 default:
733 print_proxy("");
734 write_name(proxy, arg);
735 fprintf(proxy," = &_M%d;\n", n);
736 print_proxy("MIDL_memset(&_M%d, 0, sizeof _M%d);\n", n, n);
737 ++n;
738 break;
741 arg = PREV_LINK(arg);
745 static void stub_gen_marshall_size( var_t *arg )
747 print_proxy( "_StubMsg.BufferLength = 0U;\n" );
749 END_OF_LIST(arg);
750 while (arg) {
751 if (is_attr(arg->attrs, ATTR_OUT))
752 marshall_size_arg( arg );
753 arg = PREV_LINK(arg);
757 static void stub_gen_marshall_copydata( var_t *arg )
759 END_OF_LIST(arg);
760 while (arg) {
761 if (is_attr(arg->attrs, ATTR_OUT))
762 marshall_copy_arg( arg );
763 arg = PREV_LINK(arg);
767 static void stub_genmarshall( var_t *args )
769 /* FIXME: size buffer */
770 stub_gen_marshall_size( args );
772 print_proxy("NdrStubGetBuffer(This, pRpcChannelBuffer, &_StubMsg);\n");
774 stub_gen_marshall_copydata( args );
777 static void gen_stub(type_t *iface, func_t *cur, const char *cas)
779 var_t *def = cur->def;
780 var_t *arg;
781 int has_ret = !is_void(def->type, def);
783 indent = 0;
784 print_proxy( "void __RPC_STUB %s_", iface->name);
785 write_name(proxy, def);
786 print_proxy( "_Stub(\n");
787 indent++;
788 print_proxy( "IRpcStubBuffer* This,\n");
789 print_proxy( "IRpcChannelBuffer* pRpcChannelBuffer,\n");
790 print_proxy( "PRPC_MESSAGE _Msg,\n");
791 print_proxy( "DWORD* _pdwStubPhase)\n");
792 indent--;
793 print_proxy( "{\n");
794 indent++;
795 /* local variables */
796 if (has_ret) {
797 print_proxy("");
798 write_type(proxy, def->type, def, def->tname);
799 fprintf(proxy, " _RetVal;\n");
801 print_proxy("%s * _This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n", iface->name, iface->name);
802 print_proxy("MIDL_STUB_MESSAGE _StubMsg;\n");
803 stub_write_locals( cur->args );
804 fprintf(proxy, "\n");
806 /* FIXME: trace */
808 print_proxy("NdrStubInitialize(_Msg, &_StubMsg, &Object_StubDesc, pRpcChannelBuffer);\n");
809 fprintf(proxy, "\n");
811 print_proxy("RpcTryFinally\n");
812 print_proxy("{\n");
813 indent++;
814 print_proxy("if ((_Msg->DataRepresentation&0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
815 indent++;
816 print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[0]);\n" );
817 indent--;
818 fprintf(proxy, "\n");
820 stub_unmarshall( cur->args );
821 fprintf(proxy, "\n");
823 print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
824 fprintf(proxy, "\n");
825 print_proxy("");
826 if (has_ret) fprintf(proxy, "_RetVal = ");
827 fprintf(proxy, "%s_", iface->name);
828 if (cas) fprintf(proxy, "%s_Stub", cas);
829 else write_name(proxy, def);
830 fprintf(proxy, "(_This");
831 arg = cur->args;
832 if (arg) {
833 END_OF_LIST(arg);
834 while (arg) {
835 fprintf(proxy, ", ");
836 write_name(proxy, arg);
837 arg = PREV_LINK(arg);
840 fprintf(proxy, ");\n");
841 fprintf(proxy, "\n");
842 print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
843 fprintf(proxy, "\n");
845 stub_genmarshall( cur->args );
846 fprintf(proxy, "\n");
848 if (has_ret) {
850 * FIXME: We only need to round the buffer up if it could be unaligned...
851 * We should calculate how much buffer we used and output the following
852 * line only if necessary.
854 print_proxy( "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + 3) & ~ 0x3);\n");
856 print_proxy( "*(" );
857 write_type(proxy, def->type, def, def->tname);
858 fprintf(proxy, " *)_StubMsg.Buffer = _RetVal;\n");
859 print_proxy("_StubMsg.Buffer += sizeof(");
860 write_type(proxy, def->type, def, def->tname);
861 fprintf(proxy, ");\n");
864 indent--;
865 print_proxy("}\n");
866 print_proxy("RpcFinally\n");
867 print_proxy("{\n");
868 print_proxy("}\n");
869 print_proxy("RpcEndFinally\n");
871 print_proxy("_Msg->BufferLength = _StubMsg.Buffer - (unsigned char *)_Msg->Buffer;\n");
872 indent--;
874 print_proxy("}\n");
875 print_proxy("\n");
878 static int write_proxy_methods(type_t *iface)
880 func_t *cur = iface->funcs;
881 int i = 0;
883 END_OF_LIST(cur);
885 if (iface->ref) i = write_proxy_methods(iface->ref);
886 while (cur) {
887 var_t *def = cur->def;
888 if (!is_callas(def->attrs)) {
889 if (i) fprintf(proxy, ",\n");
890 print_proxy( "%s_", iface->name);
891 write_name(proxy, def);
892 fprintf(proxy, "_Proxy");
893 i++;
895 cur = PREV_LINK(cur);
897 return i;
900 static int write_stub_methods(type_t *iface)
902 func_t *cur = iface->funcs;
903 int i = 0;
905 END_OF_LIST(cur);
907 if (iface->ref) i = write_stub_methods(iface->ref);
908 else return i; /* skip IUnknown */
909 while (cur) {
910 var_t *def = cur->def;
911 if (!is_local(def->attrs)) {
912 if (i) fprintf(proxy,",\n");
913 print_proxy( "%s_", iface->name);
914 write_name(proxy, def);
915 fprintf(proxy, "_Stub");
916 i++;
918 cur = PREV_LINK(cur);
920 return i;
923 static void write_proxy(type_t *iface)
925 int midx = -1, stubs;
926 func_t *cur = iface->funcs;
928 if (!cur) return;
930 END_OF_LIST(cur);
932 /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */
934 fprintf(proxy, "/*****************************************************************************\n");
935 fprintf(proxy, " * %s interface\n", iface->name);
936 fprintf(proxy, " */\n");
937 while (cur) {
938 const var_t *def = cur->def;
939 if (!is_local(def->attrs)) {
940 const var_t *cas = is_callas(def->attrs);
941 const char *cname = cas ? cas->name : NULL;
942 int idx = cur->idx;
943 if (cname) {
944 const func_t *m = iface->funcs;
945 while (m && strcmp(get_name(m->def), cname))
946 m = NEXT_LINK(m);
947 idx = m->idx;
949 gen_proxy(iface, cur, idx);
950 gen_stub(iface, cur, cname);
951 if (midx == -1) midx = idx;
952 else if (midx != idx) yyerror("method index mismatch in write_proxy");
953 midx++;
955 cur = PREV_LINK(cur);
958 /* proxy vtable */
959 print_proxy( "const CINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n", midx, iface->name);
960 print_proxy( "{\n");
961 indent++;
962 print_proxy( "{\n", iface->name);
963 indent++;
964 print_proxy( "&IID_%s,\n", iface->name);
965 indent--;
966 print_proxy( "},\n");
967 print_proxy( "{\n");
968 indent++;
969 write_proxy_methods(iface);
970 fprintf(proxy, "\n");
971 indent--;
972 print_proxy( "}\n");
973 indent--;
974 print_proxy( "};\n");
975 fprintf(proxy, "\n\n");
977 /* stub vtable */
978 print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
979 print_proxy( "{\n");
980 indent++;
981 stubs = write_stub_methods(iface);
982 fprintf(proxy, "\n");
983 indent--;
984 fprintf(proxy, "};\n");
985 print_proxy( "\n");
986 print_proxy( "const CInterfaceStubVtbl _%sStubVtbl =\n", iface->name);
987 print_proxy( "{\n");
988 indent++;
989 print_proxy( "{\n");
990 indent++;
991 print_proxy( "&IID_%s,\n", iface->name);
992 print_proxy( "0,\n");
993 print_proxy( "%d,\n", stubs+3);
994 print_proxy( "&%s_table[-3],\n", iface->name);
995 indent--;
996 print_proxy( "},\n", iface->name);
997 print_proxy( "{\n");
998 indent++;
999 print_proxy( "CStdStubBuffer_METHODS\n");
1000 indent--;
1001 print_proxy( "}\n");
1002 indent--;
1003 print_proxy( "};\n");
1004 print_proxy( "\n");
1007 void write_proxies(ifref_t *ifaces)
1009 ifref_t *lcur = ifaces;
1010 ifref_t *cur;
1011 char *file_id = proxy_token;
1012 int c;
1014 if (!do_proxies) return;
1015 if (!lcur) return;
1016 END_OF_LIST(lcur);
1018 init_proxy();
1019 if(!proxy) return;
1021 cur = lcur;
1022 while (cur) {
1023 if (is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
1024 write_proxy(cur->iface);
1025 cur = PREV_LINK(cur);
1028 if (!proxy) return;
1030 write_stubdesc();
1032 print_proxy( "#if !defined(__RPC_WIN32__)\n");
1033 print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
1034 print_proxy( "#endif\n");
1035 print_proxy( "\n");
1036 write_formatstring( 1 );
1037 write_formatstring( 0 );
1039 fprintf(proxy, "const CInterfaceProxyVtbl* _%s_ProxyVtblList[] =\n", file_id);
1040 fprintf(proxy, "{\n");
1041 cur = lcur;
1042 while (cur) {
1043 if(cur->iface->ref && cur->iface->funcs &&
1044 is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
1045 fprintf(proxy, " (CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", cur->iface->name);
1046 cur = PREV_LINK(cur);
1048 fprintf(proxy, " 0\n");
1049 fprintf(proxy, "};\n");
1050 fprintf(proxy, "\n");
1052 fprintf(proxy, "const CInterfaceStubVtbl* _%s_StubVtblList[] =\n", file_id);
1053 fprintf(proxy, "{\n");
1054 cur = lcur;
1055 while (cur) {
1056 if(cur->iface->ref && cur->iface->funcs &&
1057 is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
1058 fprintf(proxy, " (CInterfaceStubVtbl*)&_%sStubVtbl,\n", cur->iface->name);
1059 cur = PREV_LINK(cur);
1061 fprintf(proxy, " 0\n");
1062 fprintf(proxy, "};\n");
1063 fprintf(proxy, "\n");
1065 fprintf(proxy, "PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
1066 fprintf(proxy, "{\n");
1067 cur = lcur;
1068 while (cur) {
1069 if(cur->iface->ref && cur->iface->funcs &&
1070 is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
1071 fprintf(proxy, " \"%s\",\n", cur->iface->name);
1072 cur = PREV_LINK(cur);
1074 fprintf(proxy, " 0\n");
1075 fprintf(proxy, "};\n");
1076 fprintf(proxy, "\n");
1078 fprintf(proxy, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_%s, pIID, n)\n", file_id, file_id);
1079 fprintf(proxy, "\n");
1080 fprintf(proxy, "int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
1081 fprintf(proxy, "{\n");
1082 cur = lcur;
1083 c = 0;
1084 while (cur) {
1085 if(cur->iface->ref)
1087 fprintf(proxy, " if (!_%s_CHECK_IID(%d))\n", file_id, c);
1088 fprintf(proxy, " {\n");
1089 fprintf(proxy, " *pIndex = %d;\n", c);
1090 fprintf(proxy, " return 1;\n");
1091 fprintf(proxy, " }\n");
1092 c++;
1094 cur = PREV_LINK(cur);
1096 fprintf(proxy, " return 0;\n");
1097 fprintf(proxy, "}\n");
1098 fprintf(proxy, "\n");
1100 fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id);
1101 fprintf(proxy, "{\n");
1102 fprintf(proxy, " (PCInterfaceProxyVtblList*)&_%s_ProxyVtblList,\n", file_id);
1103 fprintf(proxy, " (PCInterfaceStubVtblList*)&_%s_StubVtblList,\n", file_id);
1104 fprintf(proxy, " (const PCInterfaceName*)&_%s_InterfaceNamesList,\n", file_id);
1105 fprintf(proxy, " 0,\n");
1106 fprintf(proxy, " &_%s_IID_Lookup,\n", file_id);
1107 fprintf(proxy, " %d,\n", c);
1108 fprintf(proxy, " 1,\n");
1109 fprintf(proxy, " 0,\n");
1110 fprintf(proxy, " 0,\n");
1111 fprintf(proxy, " 0,\n");
1112 fprintf(proxy, " 0\n");
1113 fprintf(proxy, "};\n");
1115 fclose(proxy);