proxy.c 28.7 KB
Newer Older
1 2 3 4
/*
 * IDL Compiler
 *
 * Copyright 2002 Ove Kaaven
5
 * Copyright 2004 Mike McCormack
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 26

#include <stdio.h>
#include <stdlib.h>
27 28 29
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
30 31 32 33 34 35 36 37 38
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <signal.h>

#include "widl.h"
#include "utils.h"
#include "parser.h"
#include "header.h"
39
#include "typegen.h"
40

41 42 43 44 45 46 47 48
#define END_OF_LIST(list)       \
  do {                          \
    if (list) {                 \
      while (NEXT_LINK(list))   \
        list = NEXT_LINK(list); \
    }                           \
  } while(0)

49
static FILE* proxy;
50
static int indent = 0;
51

52 53
/* FIXME: support generation of stubless proxies */

54
static int print_proxy( const char *format, ... )
55 56 57 58 59
{
  va_list va;
  int i, r;

  va_start( va, format );
60 61 62
  if ( format[0] != '\n' )
    for( i=0; i<indent; i++ )
      fprintf( proxy, "    " );
63 64 65 66 67 68 69 70 71 72 73
  r = vfprintf( proxy, format, va );
  va_end( va );
  return r;
}

static void write_stubdescproto(void)
{
  print_proxy( "extern const MIDL_STUB_DESC Object_StubDesc;\n");
  print_proxy( "\n");
}

74 75
static void write_stubdesc(void)
{
76 77 78 79 80 81 82 83 84 85
  print_proxy( "const MIDL_STUB_DESC Object_StubDesc = {\n");
  print_proxy( "    0,\n");
  print_proxy( "    NdrOleAllocate,\n");
  print_proxy( "    NdrOleFree,\n");
  print_proxy( "    {0}, 0, 0, 0, 0,\n");
  print_proxy( "    0 /* __MIDL_TypeFormatString.Format */\n");
  print_proxy( "};\n");
  print_proxy( "\n");
}

86
static void init_proxy(ifref_t *ifaces)
87 88
{
  if (proxy) return;
89 90
  if(!(proxy = fopen(proxy_name, "w")))
    error("Could not open %s for output\n", proxy_name);
91
  print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
92 93
  print_proxy( "\n");
  print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
94
  print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ 440\n");
95 96 97 98 99 100 101 102 103
  print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
  print_proxy( "\n");
  print_proxy( "#include \"rpcproxy.h\"\n");
  print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
  print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
  print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
  print_proxy( "\n");
  print_proxy( "#include \"%s\"\n", header_name);
  print_proxy( "\n");
104
  write_formatstringsdecl(proxy, indent, ifaces, 1);
105
  write_stubdescproto();
106 107
}

108 109
static void clear_output_vars( var_t *arg )
{
110
  END_OF_LIST(arg);
111
  while (arg) {
112 113
    if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
      print_proxy( "if(%s)\n", arg->name );
114
      indent++;
115
      print_proxy( "MIDL_memset( %s, 0, sizeof( *%s ));\n", arg->name, arg->name );
116
      indent--;
117 118 119 120 121
    }
    arg = PREV_LINK(arg);
  }
}

122
int is_var_ptr(var_t *v)
123
{
124
  return v->ptr_level || is_ptr(v->type);
125 126
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
int cant_be_null(var_t *v)
{
  /* Search backwards for the most recent pointer attribute.  */
  const attr_t *attrs = v->attrs;
  const type_t *type = v->type;

  if (! attrs && type)
  {
    attrs = type->attrs;
    type = type->ref;
  }

  while (attrs)
  {
    int t = get_attrv(attrs, ATTR_POINTERTYPE);

    if (t == RPC_FC_FP || t == RPC_FC_OP || t == RPC_FC_UP)
      return 0;

    if (t == RPC_FC_RP)
      return 1;

    if (type)
    {
      attrs = type->attrs;
      type = type->ref;
    }
    else
      attrs = NULL;
  }

  return 1;                             /* Default is RPC_FC_RP.  */
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
static int is_user_derived(var_t *v)
{
  const attr_t *attrs = v->attrs;
  const type_t *type = v->type;

  if (! attrs && type)
  {
    attrs = type->attrs;
    type = type->ref;
  }

  while (attrs)
  {
    if (is_attr(attrs, ATTR_WIREMARSHAL))
      return 1;

    if (type)
    {
      attrs = type->attrs;
      type = type->ref;
    }
    else
      attrs = NULL;
  }

  return 0;
}

189 190 191 192
static void proxy_check_pointers( var_t *arg )
{
  END_OF_LIST(arg);
  while (arg) {
193
    if (is_var_ptr(arg) && cant_be_null(arg)) {
194 195 196 197 198 199 200 201 202
        print_proxy( "if(!%s)\n", arg->name );
        indent++;
        print_proxy( "RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
        indent--;
    }
    arg = PREV_LINK(arg);
  }
}

203 204 205
static void marshall_size_arg( var_t *arg )
{
  int index = 0;
206
  const type_t *type = arg->type;
207 208 209 210 211 212
  expr_t *expr;

  expr = get_attrp( arg->attrs, ATTR_SIZEIS );
  if (expr)
  {
    print_proxy( "_StubMsg.MaxCount = ", arg->name );
213
    write_expr(proxy, expr, 0);
214 215 216 217 218 219
    fprintf(proxy, ";\n\n");
    print_proxy( "NdrConformantArrayBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
    return;
  }

220 221 222 223 224 225 226
  if (is_user_derived(arg))
  {
    print_proxy("NdrUserMarshalBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name);
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index);
    return;
  }

227 228 229 230 231 232
  if (is_string_type(arg->attrs, arg->ptr_level, arg->array))
  {
    print_proxy("NdrConformantStringBufferSize( &_StubMsg, (unsigned char*)s, ", arg->name);
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index);
  }

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
  switch( type->type )
  {
  case RPC_FC_BYTE:
  case RPC_FC_CHAR:
    print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 1, arg->name );
    break;

  case RPC_FC_WCHAR:
  case RPC_FC_SHORT:
  case RPC_FC_USHORT:
  case RPC_FC_ENUM16:
    print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 2, arg->name );
    break;

  case RPC_FC_LONG:
  case RPC_FC_ULONG:
  case RPC_FC_ENUM32:
    print_proxy( "_StubMsg.BufferLength += %d; /* %s */\n", 4, arg->name );
    break;
      
  case RPC_FC_STRUCT:
    print_proxy( "NdrSimpleStructBufferSize(&_StubMsg, (unsigned char*)%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
    break;

  case RPC_FC_CARRAY:
    print_proxy( "NdrConformantArrayBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
    break;

  case RPC_FC_BOGUS_STRUCT:
    print_proxy( "NdrComplexStructBufferSize(&_StubMsg, (unsigned char*)%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
    break;

  case RPC_FC_FP:
    {
      var_t temp;
      memset( &temp, 0, sizeof temp );
      temp.type = type->ref;
      temp.name = arg->name; /* FIXME */
#if 0
275
      print_proxy( "/* FIXME: %s use the right name for %s */\n", __FUNCTION__, arg->name );
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
#endif
      marshall_size_arg( &temp );
    }
    break;

  case RPC_FC_IP:
    print_proxy( "NdrPointerBufferSize( &_StubMsg, (unsigned char*)%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
    break;

  default:
    print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
  }
}

291
static void proxy_gen_marshall_size( var_t *arg )
292 293 294
{
  print_proxy( "_StubMsg.BufferLength = 0U;\n" );

295
  END_OF_LIST(arg);
296
  while (arg) {
297 298 299 300 301 302 303 304
    if (is_attr(arg->attrs, ATTR_IN)) 
    {
      marshall_size_arg( arg );
      fprintf(proxy, "\n");
    }
    arg = PREV_LINK(arg);
  }
}
305

306 307 308
static void marshall_copy_arg( var_t *arg )
{
  int index = 0;
309
  type_t *type = arg->type;
310
  expr_t *expr;
311

312 313 314 315
  expr = get_attrp( arg->attrs, ATTR_SIZEIS );
  if (expr)
  {
    print_proxy( "_StubMsg.MaxCount = ", arg->name );
316
    write_expr(proxy, expr, 0);
317 318 319 320 321
    fprintf(proxy, ";\n\n");
    print_proxy( "NdrConformantArrayMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
    return;
  }
322

323 324 325 326 327 328 329
  if (is_user_derived(arg))
  {
    print_proxy("NdrUserMarshalMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name);
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index);
    return;
  }

330 331 332 333 334 335
  if (is_string_type(arg->attrs, arg->ptr_level, arg->array))
  {
    print_proxy("NdrConformantStringMarshall( &_StubMsg, (unsigned char*)s, ", arg->name);
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index);
  }

336 337 338 339 340 341 342 343 344 345 346
  switch( type->type )
  {
  case RPC_FC_BYTE:
  case RPC_FC_CHAR:
  case RPC_FC_WCHAR:
  case RPC_FC_SHORT:
  case RPC_FC_USHORT:
  case RPC_FC_ENUM16:
  case RPC_FC_LONG:
  case RPC_FC_ULONG:
  case RPC_FC_ENUM32:
347
    print_proxy( "*(");
348
    write_type(proxy, arg->type, arg, arg->tname);
349
    fprintf(proxy, " *)_StubMsg.Buffer = %s;\n", arg->name );
350
    print_proxy("_StubMsg.Buffer += sizeof(");
351 352
    write_type(proxy, arg->type, arg, arg->tname);
    fprintf(proxy, ");\n");
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    break;
      
  case RPC_FC_STRUCT:
    /* FIXME: add the format string, and set the index below */
    print_proxy( "NdrSimpleStructMarshall(&_StubMsg, (unsigned char*)%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
    break;

  case RPC_FC_CARRAY:
    break;

  case RPC_FC_BOGUS_STRUCT:
    print_proxy( "NdrComplexStructMarshall(&_StubMsg, (unsigned char*)%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d] );\n", index );
    break;

  case RPC_FC_FP:
    {
      var_t temp;
      memset( &temp, 0, sizeof temp );
      temp.type = type->ref;
      temp.name = arg->name; /* FIXME */
#if 0
376
      print_proxy( "/* FIXME: %s use the right name for %s */\n", __FUNCTION__, arg->name );
377 378 379 380
#endif
      marshall_copy_arg( &temp );
    }
    break;
381

382 383 384 385
  case RPC_FC_IP:
    print_proxy( "NdrPointerMarshall( &_StubMsg, (unsigned char*)%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d]);\n", index );
    break;
386

387 388
  default:
    print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
389 390 391 392 393
  }
}

static void gen_marshall_copydata( var_t *arg )
{
394
  END_OF_LIST(arg);
395
  while (arg) {
396 397 398 399
    if (is_attr(arg->attrs, ATTR_IN)) 
    {
      marshall_copy_arg( arg );
      fprintf(proxy, "\n");
400 401 402 403 404 405 406 407
    }
    arg = PREV_LINK(arg);
  }
}

static void gen_marshall( var_t *arg )
{
  /* generated code to determine the size of the buffer required */
408
  proxy_gen_marshall_size( arg );
409 410 411 412 413 414 415 416 417 418

  /* generated code to allocate the buffer */
  print_proxy( "NdrProxyGetBuffer(This, &_StubMsg);\n" );

  /* generated code to copy the args into the buffer */
  gen_marshall_copydata( arg );

  print_proxy( "\n");
}

419
static void unmarshall_copy_arg( var_t *arg )
420
{
421
  int index = 0;
422
  type_t *type = arg->type;
423
  expr_t *expr;
424

425 426 427
  expr = get_attrp( arg->attrs, ATTR_SIZEIS );
  if (expr)
  {
428 429
    print_proxy( "NdrConformantArrayUnmarshall( &_StubMsg, (unsigned char**)&%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
430 431
    return;
  }
432

433 434 435 436 437 438 439
  if (is_user_derived(arg))
  {
    print_proxy("NdrUserMarshalUnmarshall( &_StubMsg, (unsigned char**)&%s, ", arg->name);
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0 );\n", index);
    return;
  }

440 441 442 443 444 445
  if (is_string_type(arg->attrs, arg->ptr_level, arg->array))
  {
    print_proxy("NdrConformantStringUnmarshall( &_StubMsg, (unsigned char**)&s, ", arg->name);
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0 );\n", index);
  }

446 447 448 449 450 451 452 453 454 455 456
  switch( type->type )
  {
  case RPC_FC_BYTE:
  case RPC_FC_CHAR:
  case RPC_FC_WCHAR:
  case RPC_FC_SHORT:
  case RPC_FC_USHORT:
  case RPC_FC_ENUM16:
  case RPC_FC_LONG:
  case RPC_FC_ULONG:
  case RPC_FC_ENUM32:
457 458 459
    print_proxy( "%s = *(", arg->name );
    write_type(proxy, arg->type, arg, arg->tname);
    fprintf(proxy," *)_StubMsg.Buffer;\n");
460
    print_proxy("_StubMsg.Buffer += sizeof(");
461
    write_type(proxy, arg->type, arg, arg->tname);
462
    fprintf(proxy, ");\n");
463 464 465 466 467 468 469 470
    break;
      
  case RPC_FC_STRUCT:
    print_proxy( "NdrSimpleStructUnmarshall(&_StubMsg, (unsigned char**)%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
    break;

  case RPC_FC_CARRAY:
471 472
    print_proxy( "NdrConformantArrayUnmarshall( &_StubMsg, (unsigned char**)&%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
473 474 475
    break;

  case RPC_FC_BOGUS_STRUCT:
476
    print_proxy( "NdrComplexStructUnmarshall(&_StubMsg, (unsigned char**)&%s, ", arg->name );
477 478 479 480 481 482 483 484 485 486
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0 );\n", index );
    break;

  case RPC_FC_FP:
    {
      var_t temp;
      memset( &temp, 0, sizeof temp );
      temp.type = type->ref;
      temp.name = arg->name; /* FIXME */
#if 1
487
      print_proxy( "/* FIXME: %s use the right name for %s */\n", __FUNCTION__, arg->name );
488 489 490 491
#endif
      unmarshall_copy_arg( &temp );
    }
    break;
492

493 494 495 496
  case RPC_FC_IP:
    print_proxy( "NdrPointerUnmarshall(&_StubMsg, (unsigned char**)&%s, ", arg->name );
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
    break;
497

498 499
  default:
    print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
500 501 502
  }
}

503
static void gen_unmarshall( var_t *arg )
504
{
505
  END_OF_LIST(arg);
506
  while (arg) {
507 508 509 510 511 512 513 514
    if (is_attr(arg->attrs, ATTR_OUT)) 
    {
      unmarshall_copy_arg( arg );
      fprintf(proxy, "\n");
    }
    arg = PREV_LINK(arg);
  }
}
515

516 517 518 519 520 521
static void free_variable( var_t *arg )
{
  var_t *constraint;
  int index = 0; /* FIXME */
  type_t *type;
  expr_t *expr;
522

523 524 525 526
  expr = get_attrp( arg->attrs, ATTR_SIZEIS );
  if (expr)
  {
    print_proxy( "_StubMsg.MaxCount = ", arg->name );
527
    write_expr(proxy, expr, 0);
528 529 530 531 532 533
    fprintf(proxy, ";\n\n");
    print_proxy( "NdrClearOutParameters( &_StubMsg, ");
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], ", index );
    fprintf(proxy, "(void*)%s );\n", arg->name );
    return;
  }
534

535
  type = arg->type;
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  switch( type->type )
  {
  case RPC_FC_BYTE:
  case RPC_FC_CHAR:
  case RPC_FC_WCHAR:
  case RPC_FC_SHORT:
  case RPC_FC_USHORT:
  case RPC_FC_ENUM16:
  case RPC_FC_LONG:
  case RPC_FC_ULONG:
  case RPC_FC_ENUM32:
  case RPC_FC_STRUCT:
    break;

  case RPC_FC_FP:
  case RPC_FC_IP:
    constraint = get_attrp( arg->attrs, ATTR_IIDIS );
    if( constraint )
      print_proxy( "_StubMsg.MaxCount = (unsigned long) ( %s );\n",constraint->name);
    print_proxy( "NdrClearOutParameters( &_StubMsg, ");
    fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], ", index );
    fprintf(proxy, "(void*)%s );\n", arg->name );
    break;

  default:
    print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type->type );
  }
}

static void proxy_free_variables( var_t *arg )
{
  END_OF_LIST(arg);
  while (arg) {
    if (is_attr(arg->attrs, ATTR_OUT)) 
    {
      free_variable( arg );
      fprintf(proxy, "\n");
573 574 575 576 577
    }
    arg = PREV_LINK(arg);
  }
}

578 579 580 581 582
static void gen_proxy(type_t *iface, func_t *cur, int idx)
{
  var_t *def = cur->def;
  int has_ret = !is_void(def->type, def);

583
  indent = 0;
584
  write_type(proxy, def->type, def, def->tname);
585
  print_proxy( " STDMETHODCALLTYPE %s_", iface->name);
586
  write_name(proxy, def);
587
  print_proxy( "_Proxy(\n");
588
  write_args(proxy, cur->args, iface->name, 1, TRUE);
589 590 591
  print_proxy( ")\n");
  print_proxy( "{\n");
  indent ++;
592 593
  /* local variables */
  if (has_ret) {
594
    print_proxy( "" );
595
    write_type(proxy, def->type, def, def->tname);
596
    print_proxy( " _RetVal;\n");
597
  }
598 599 600
  print_proxy( "RPC_MESSAGE _Msg;\n" );
  print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n" );
  print_proxy( "\n");
601 602

  /* FIXME: trace */
603
  clear_output_vars( cur->args );
604

605 606
  print_proxy( "RpcTryExcept\n" );
  print_proxy( "{\n" );
607 608
  indent++;
  print_proxy( "NdrProxyInitialize(This, &_Msg, &_StubMsg, &Object_StubDesc, %d);\n", idx);
609
  proxy_check_pointers( cur->args );
610

611 612
  print_proxy( "RpcTryFinally\n" );
  print_proxy( "{\n" );
613
  indent++;
614

615
  gen_marshall( cur->args );
616

617
  print_proxy( "NdrProxySendReceive(This, &_StubMsg);\n" );
618 619 620 621 622 623
  fprintf(proxy, "\n");
  print_proxy("if ((_Msg.DataRepresentation&0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
  indent++;
  print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[0]);\n" );
  indent--;
  fprintf(proxy, "\n");
624

625 626 627 628 629 630 631 632 633
  gen_unmarshall( cur->args );
  if (has_ret) {
    /* 
     * FIXME: We only need to round the buffer up if it could be unaligned...
     *    We should calculate how much buffer we used and output the following
     *    line only if necessary.
     */
    print_proxy( "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + 3) & ~ 0x3);\n");

634
    print_proxy( "_RetVal = *(" );
635
    write_type(proxy, def->type, def, def->tname);
636
    fprintf(proxy, " *)_StubMsg.Buffer;\n");
637
    print_proxy("_StubMsg.Buffer += sizeof(");
638 639
    write_type(proxy, def->type, def, def->tname);
    fprintf(proxy, ");\n");
640
  }
641

642 643
  indent--;
  print_proxy( "}\n");
644 645
  print_proxy( "RpcFinally\n" );
  print_proxy( "{\n" );
646 647 648 649 650 651 652 653 654 655 656
  indent++;
  print_proxy( "NdrProxyFreeBuffer(This, &_StubMsg);\n" );
  indent--;
  print_proxy( "}\n");
  print_proxy( "RpcEndFinally\n" );
  indent--;
  print_proxy( "}\n" );
  print_proxy( "RpcExcept(_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
  print_proxy( "{\n" );
  if (has_ret) {
    indent++;
657 658
    proxy_free_variables( cur->args );
    print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
659 660 661 662
    indent--;
  }
  print_proxy( "}\n" );
  print_proxy( "RpcEndExcept\n" );
663 664

  if (has_ret) {
665
    print_proxy( "return _RetVal;\n" );
666 667 668 669 670 671
  }
  indent--;
  print_proxy( "}\n");
  print_proxy( "\n");
}

672
static void stub_write_locals( var_t *arg )
673
{
674
  int n = 0;
675
  END_OF_LIST(arg);
676
  while (arg) {
677 678
    int outptr = is_attr(arg->attrs, ATTR_OUT)
                 && ! is_attr(arg->attrs, ATTR_IN);
679 680 681 682 683 684 685 686 687 688 689

    /* create a temporary variable to store the output */
    if (outptr) {
      var_t temp;
      memset( &temp, 0, sizeof temp );
      temp.ptr_level = arg->ptr_level - 1; /* dereference once */
      print_proxy("");
      write_type(proxy, arg->type, &temp, arg->tname);
      fprintf(proxy, " _M%d;\n",n++);
    }
    print_proxy("");
690
    write_type(proxy, arg->type, arg, arg->tname);
691
    fprintf(proxy, " ");
692
    write_name(proxy, arg);
693
    fprintf(proxy, ";\n");
694
    arg = PREV_LINK(arg);
695 696 697
  }
}

698 699
static void stub_unmarshall( var_t *arg )
{
700
  int n = 0;
701
  END_OF_LIST(arg);
702
  while (arg) {
703 704 705 706
    if (is_attr(arg->attrs, ATTR_IN))
    {
      unmarshall_copy_arg( arg );
      fprintf(proxy,"\n");
707
    }
708
    else if (is_attr(arg->attrs, ATTR_OUT)) {
709
      type_t *type = arg->type;
710 711 712 713 714 715 716 717 718 719 720 721 722
      switch( type->type )
      {
      case RPC_FC_STRUCT:
        print_proxy("MIDL_memset(");
        write_name(proxy, arg);
        fprintf(proxy,", 0, sizeof(");
        write_type(proxy, arg->type, arg, arg->tname);
        fprintf(proxy,"));\n");
        break;
      default:
        print_proxy("");
        write_name(proxy, arg);
        fprintf(proxy," = &_M%d;\n", n);
723 724
        print_proxy("MIDL_memset(&_M%d, 0, sizeof _M%d);\n", n, n);
        ++n;
725 726
        break;
      }
727
    }
728 729 730 731 732 733 734 735
    arg = PREV_LINK(arg);
  }
}

static void stub_gen_marshall_size( var_t *arg )
{
  print_proxy( "_StubMsg.BufferLength = 0U;\n" );

736
  END_OF_LIST(arg);
737
  while (arg) {
738 739
    if (is_attr(arg->attrs, ATTR_OUT))
      marshall_size_arg( arg );
740 741 742 743 744 745
    arg = PREV_LINK(arg);
  }
}

static void stub_gen_marshall_copydata( var_t *arg )
{
746
  END_OF_LIST(arg);
747
  while (arg) {
748 749
    if (is_attr(arg->attrs, ATTR_OUT))
      marshall_copy_arg( arg );
750 751 752 753 754 755 756 757 758 759 760 761 762 763
    arg = PREV_LINK(arg);
  }
}

static void stub_genmarshall( var_t *args )
{
  /* FIXME: size buffer */
  stub_gen_marshall_size( args );

  print_proxy("NdrStubGetBuffer(This, pRpcChannelBuffer, &_StubMsg);\n");

  stub_gen_marshall_copydata( args );
}

764
static void gen_stub(type_t *iface, func_t *cur, const char *cas)
765 766 767 768 769
{
  var_t *def = cur->def;
  var_t *arg;
  int has_ret = !is_void(def->type, def);

770 771
  indent = 0;
  print_proxy( "void __RPC_STUB %s_", iface->name);
772
  write_name(proxy, def);
773 774 775 776
  print_proxy( "_Stub(\n");
  indent++;
  print_proxy( "IRpcStubBuffer* This,\n");
  print_proxy( "IRpcChannelBuffer* pRpcChannelBuffer,\n");
777
  print_proxy( "PRPC_MESSAGE _Msg,\n");
778
  print_proxy( "DWORD* _pdwStubPhase)\n");
779 780 781
  indent--;
  print_proxy( "{\n");
  indent++;
782 783
  /* local variables */
  if (has_ret) {
784
    print_proxy("");
785
    write_type(proxy, def->type, def, def->tname);
786
    fprintf(proxy, " _RetVal;\n");
787
  }
788
  print_proxy("%s * _This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n", iface->name, iface->name);
789
  print_proxy("MIDL_STUB_MESSAGE _StubMsg;\n");
790
  stub_write_locals( cur->args );
791 792 793 794
  fprintf(proxy, "\n");

  /* FIXME: trace */

795
  print_proxy("NdrStubInitialize(_Msg, &_StubMsg, &Object_StubDesc, pRpcChannelBuffer);\n");
796 797
  fprintf(proxy, "\n");

798 799 800
  print_proxy("RpcTryFinally\n");
  print_proxy("{\n");
  indent++;
801 802 803 804 805
  print_proxy("if ((_Msg->DataRepresentation&0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
  indent++;
  print_proxy("NdrConvert( &_StubMsg, &__MIDL_ProcFormatString.Format[0]);\n" );
  indent--;
  fprintf(proxy, "\n");
806

807
  stub_unmarshall( cur->args );
808
  fprintf(proxy, "\n");
809

810 811
  print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
  fprintf(proxy, "\n");
812
  print_proxy("");
813
  if (has_ret) fprintf(proxy, "_RetVal = ");
814 815 816 817 818 819
  fprintf(proxy, "%s_", iface->name);
  if (cas) fprintf(proxy, "%s_Stub", cas);
  else write_name(proxy, def);
  fprintf(proxy, "(_This");
  arg = cur->args;
  if (arg) {
820
    END_OF_LIST(arg);
821 822 823 824 825 826 827
    while (arg) {
      fprintf(proxy, ", ");
      write_name(proxy, arg);
      arg = PREV_LINK(arg);
    }
  }
  fprintf(proxy, ");\n");
828 829 830
  fprintf(proxy, "\n");
  print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
  fprintf(proxy, "\n");
831

832
  stub_genmarshall( cur->args );
833
  fprintf(proxy, "\n");
834

835 836 837 838 839 840 841 842
  if (has_ret) {
    /* 
     * FIXME: We only need to round the buffer up if it could be unaligned...
     *    We should calculate how much buffer we used and output the following
     *    line only if necessary.
     */
    print_proxy( "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + 3) & ~ 0x3);\n");

843 844 845
    print_proxy( "*(" );
    write_type(proxy, def->type, def, def->tname);
    fprintf(proxy, " *)_StubMsg.Buffer = _RetVal;\n");
846
    print_proxy("_StubMsg.Buffer += sizeof(");
847
    write_type(proxy, def->type, def, def->tname);
848
    fprintf(proxy, ");\n");
849 850 851 852 853 854 855 856
  }

  indent--;
  print_proxy("}\n");
  print_proxy("RpcFinally\n");
  print_proxy("{\n");
  print_proxy("}\n");
  print_proxy("RpcEndFinally\n");
857

858
  print_proxy("_Msg->BufferLength = _StubMsg.Buffer - (unsigned char *)_Msg->Buffer;\n");
859
  indent--;
860

861 862
  print_proxy("}\n");
  print_proxy("\n");
863 864 865 866 867 868
}

static int write_proxy_methods(type_t *iface)
{
  func_t *cur = iface->funcs;
  int i = 0;
869 870

  END_OF_LIST(cur);
871 872 873 874 875

  if (iface->ref) i = write_proxy_methods(iface->ref);
  while (cur) {
    var_t *def = cur->def;
    if (!is_callas(def->attrs)) {
876 877
      if (i) fprintf(proxy, ",\n");
      print_proxy( "%s_", iface->name);
878 879 880 881 882 883 884 885 886 887 888 889 890
      write_name(proxy, def);
      fprintf(proxy, "_Proxy");
      i++;
    }
    cur = PREV_LINK(cur);
  }
  return i;
}

static int write_stub_methods(type_t *iface)
{
  func_t *cur = iface->funcs;
  int i = 0;
891 892

  END_OF_LIST(cur);
893 894 895 896 897 898

  if (iface->ref) i = write_stub_methods(iface->ref);
  else return i; /* skip IUnknown */
  while (cur) {
    var_t *def = cur->def;
    if (!is_local(def->attrs)) {
899 900
      if (i) fprintf(proxy,",\n");
      print_proxy( "%s_", iface->name);
901 902 903 904 905 906 907 908 909
      write_name(proxy, def);
      fprintf(proxy, "_Stub");
      i++;
    }
    cur = PREV_LINK(cur);
  }
  return i;
}

910
static void write_proxy(type_t *iface)
911 912 913 914
{
  int midx = -1, stubs;
  func_t *cur = iface->funcs;

915
  if (!cur) return;
916

917
  END_OF_LIST(cur);
918 919 920 921 922 923 924

  /* FIXME: check for [oleautomation], shouldn't generate proxies/stubs if specified */

  fprintf(proxy, "/*****************************************************************************\n");
  fprintf(proxy, " * %s interface\n", iface->name);
  fprintf(proxy, " */\n");
  while (cur) {
925
    const var_t *def = cur->def;
926
    if (!is_local(def->attrs)) {
927
      const var_t *cas = is_callas(def->attrs);
928
      const char *cname = cas ? cas->name : NULL;
929 930
      int idx = cur->idx;
      if (cname) {
931
        const func_t *m = iface->funcs;
932 933 934 935 936 937 938
        while (m && strcmp(get_name(m->def), cname))
          m = NEXT_LINK(m);
        idx = m->idx;
      }
      gen_proxy(iface, cur, idx);
      gen_stub(iface, cur, cname);
      if (midx == -1) midx = idx;
939
      else if (midx != idx) parser_error("method index mismatch in write_proxy");
940 941 942 943 944 945
      midx++;
    }
    cur = PREV_LINK(cur);
  }

  /* proxy vtable */
946 947 948 949 950 951 952 953
  print_proxy( "const CINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n", midx, iface->name);
  print_proxy( "{\n");
  indent++;
  print_proxy( "{\n", iface->name);
  indent++;
  print_proxy( "&IID_%s,\n", iface->name);
  indent--;
  print_proxy( "},\n");
954 955
  print_proxy( "{\n");
  indent++;
956
  write_proxy_methods(iface);
957 958 959
  fprintf(proxy, "\n");
  indent--;
  print_proxy( "}\n");
960 961
  indent--;
  print_proxy( "};\n");
962
  fprintf(proxy, "\n\n");
963 964

  /* stub vtable */
965 966 967
  print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
  print_proxy( "{\n");
  indent++;
968 969
  stubs = write_stub_methods(iface);
  fprintf(proxy, "\n");
970
  indent--;
971
  fprintf(proxy, "};\n");
972
  print_proxy( "\n");
973 974 975 976
  print_proxy( "const CInterfaceStubVtbl _%sStubVtbl =\n", iface->name);
  print_proxy( "{\n");
  indent++;
  print_proxy( "{\n");
977
  indent++;
978 979 980 981 982 983 984 985 986 987 988
  print_proxy( "&IID_%s,\n", iface->name);
  print_proxy( "0,\n");
  print_proxy( "%d,\n", stubs+3);
  print_proxy( "&%s_table[-3],\n", iface->name);
  indent--;
  print_proxy( "},\n", iface->name);
  print_proxy( "{\n");
  indent++;
  print_proxy( "CStdStubBuffer_METHODS\n");
  indent--;
  print_proxy( "}\n");
989 990 991
  indent--;
  print_proxy( "};\n");
  print_proxy( "\n");
992 993
}

994
void write_proxies(ifref_t *ifaces)
995
{
996 997
  ifref_t *lcur = ifaces;
  ifref_t *cur;
998
  char *file_id = proxy_token;
999 1000
  int c;

Huw Davies's avatar
Huw Davies committed
1001
  if (!do_proxies) return;
1002
  if (!lcur) return;
1003 1004
  END_OF_LIST(lcur);

1005
  init_proxy(ifaces);
1006
  if(!proxy) return;
1007

1008 1009 1010 1011 1012 1013 1014 1015 1016
  cur = lcur;
  while (cur) {
    if (is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
      write_proxy(cur->iface);
    cur = PREV_LINK(cur);
  }

  if (!proxy) return;

1017 1018 1019 1020 1021 1022
  write_stubdesc();

  print_proxy( "#if !defined(__RPC_WIN32__)\n");
  print_proxy( "#error Currently only Wine and WIN32 are supported.\n");
  print_proxy( "#endif\n");
  print_proxy( "\n");
1023 1024
  write_procformatstring(proxy, ifaces, 1);
  write_typeformatstring(proxy, ifaces, 1);
1025

1026 1027
  fprintf(proxy, "const CInterfaceProxyVtbl* _%s_ProxyVtblList[] =\n", file_id);
  fprintf(proxy, "{\n");
1028 1029
  cur = lcur;
  while (cur) {
1030 1031
    if(cur->iface->ref && cur->iface->funcs &&
       is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
1032
      fprintf(proxy, "    (CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", cur->iface->name);
1033 1034 1035 1036 1037 1038
    cur = PREV_LINK(cur);
  }
  fprintf(proxy, "    0\n");
  fprintf(proxy, "};\n");
  fprintf(proxy, "\n");

1039 1040
  fprintf(proxy, "const CInterfaceStubVtbl* _%s_StubVtblList[] =\n", file_id);
  fprintf(proxy, "{\n");
1041 1042
  cur = lcur;
  while (cur) {
1043 1044
    if(cur->iface->ref && cur->iface->funcs &&
       is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
1045
      fprintf(proxy, "    (CInterfaceStubVtbl*)&_%sStubVtbl,\n", cur->iface->name);
1046 1047 1048 1049 1050 1051
    cur = PREV_LINK(cur);
  }
  fprintf(proxy, "    0\n");
  fprintf(proxy, "};\n");
  fprintf(proxy, "\n");

1052
  fprintf(proxy, "PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
1053
  fprintf(proxy, "{\n");
1054 1055
  cur = lcur;
  while (cur) {
1056 1057
    if(cur->iface->ref && cur->iface->funcs &&
       is_object(cur->iface->attrs) && !is_local(cur->iface->attrs))
1058
      fprintf(proxy, "    \"%s\",\n", cur->iface->name);
1059 1060 1061 1062 1063 1064
    cur = PREV_LINK(cur);
  }
  fprintf(proxy, "    0\n");
  fprintf(proxy, "};\n");
  fprintf(proxy, "\n");

1065
  fprintf(proxy, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_%s, pIID, n)\n", file_id, file_id);
1066
  fprintf(proxy, "\n");
1067
  fprintf(proxy, "int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
1068 1069 1070 1071
  fprintf(proxy, "{\n");
  cur = lcur;
  c = 0;
  while (cur) {
1072 1073
    if(cur->iface->ref)
    {
1074 1075
      fprintf(proxy, "    if (!_%s_CHECK_IID(%d))\n", file_id, c);
      fprintf(proxy, "    {\n");
1076 1077 1078 1079 1080
      fprintf(proxy, "        *pIndex = %d;\n", c);
      fprintf(proxy, "        return 1;\n");
      fprintf(proxy, "    }\n");
      c++;
    }
1081 1082 1083 1084 1085 1086
    cur = PREV_LINK(cur);
  }
  fprintf(proxy, "    return 0;\n");
  fprintf(proxy, "}\n");
  fprintf(proxy, "\n");

1087 1088
  fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id);
  fprintf(proxy, "{\n");
1089 1090 1091
  fprintf(proxy, "    (PCInterfaceProxyVtblList*)&_%s_ProxyVtblList,\n", file_id);
  fprintf(proxy, "    (PCInterfaceStubVtblList*)&_%s_StubVtblList,\n", file_id);
  fprintf(proxy, "    (const PCInterfaceName*)&_%s_InterfaceNamesList,\n", file_id);
1092
  fprintf(proxy, "    0,\n");
1093
  fprintf(proxy, "    &_%s_IID_Lookup,\n", file_id);
1094
  fprintf(proxy, "    %d,\n", c);
1095 1096 1097 1098 1099
  fprintf(proxy, "    1,\n");
  fprintf(proxy, "    0,\n");
  fprintf(proxy, "    0,\n");
  fprintf(proxy, "    0,\n");
  fprintf(proxy, "    0\n");
1100 1101 1102 1103
  fprintf(proxy, "};\n");

  fclose(proxy);
}