WIDL: improve proxy generation code (version 3)

Mike McCormack mike at codeweavers.com
Sun Sep 5 11:41:32 CDT 2004


Hi All,

This patch allows the proxy code to build without errors or warnings. 
It supercedes the two previous patches I sent.  It needs to be compiled 
without __WINESRC__ defined.  The proxy code now looks mostly right, the 
stub code is not yet complete.  The format strings are not yet generated.

Mike


ChangeLog:
* make the widl generated proxy-stub code compilable
* generate marshalling and unmarshalling code for proxies
* generate exception handling constructs

-------------- next part --------------
Index: tools/widl/proxy.c
===================================================================
RCS file: /home/wine/wine/tools/widl/proxy.c,v
retrieving revision 1.10
diff -u -r1.10 proxy.c
--- tools/widl/proxy.c	2 Sep 2004 20:07:07 -0000	1.10
+++ tools/widl/proxy.c	5 Sep 2004 15:03:47 -0000
@@ -2,6 +2,7 @@
  * IDL Compiler
  *
  * Copyright 2002 Ove Kaaven
+ * Copyright 2004 Mike McCormack
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -36,30 +37,82 @@
 #include "header.h"
 
 static FILE* proxy;
+static int indent = 0;
 
 /* FIXME: support generation of stubless proxies */
 
+int print_proxy( char *format, ... )
+{
+  va_list va;
+  int i, r;
+
+  va_start( va, format );
+  for( i=0; i<indent; i++ )
+    fprintf( proxy, "    " );
+  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");
+}
+
 static void write_stubdesc(void)
 {
-  fprintf(proxy, "const MIDL_STUB_DESC Object_StubDesc = {\n");
-  fprintf(proxy, "    0,\n");
-  fprintf(proxy, "    NdrOleAllocate,\n");
-  fprintf(proxy, "    NdrOleFree,\n");
-  fprintf(proxy, "    {0}, 0, 0, 0, 0,\n");
-  fprintf(proxy, "    0 /* __MIDL_TypeFormatString.Format */\n");
-  fprintf(proxy, "};\n");
-  fprintf(proxy, "\n");
+  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");
+}
+
+static void write_formatdesc( char *str )
+{
+  print_proxy( "typedef struct _MIDL_%s_FORMAT_STRING {\n", str );
+  print_proxy( "    short Pad;\n");
+  print_proxy( "    unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
+  print_proxy( "} MIDL_%s_FORMAT_STRING;\n", str);
+  print_proxy( "\n");
+}
+
+static void write_formatstringsdecl(void)
+{
+  print_proxy( "#define TYPE_FORMAT_STRING_SIZE %d\n",1); /* FIXME */
+  print_proxy( "#define PROC_FORMAT_STRING_SIZE %d\n",1); /* FIXME */
+  print_proxy( "\n");
+  write_formatdesc( "TYPE" );
+  write_formatdesc( "PROC" );
+  print_proxy( "extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
+  print_proxy( "extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
+  print_proxy( "\n");
 }
 
 static void init_proxy(void)
 {
   if (proxy) return;
   proxy = fopen(proxy_name, "w");
-  fprintf(proxy, "/*** Autogenerated by WIDL %s - Do not edit ***/\n", WIDL_FULLVERSION);
-  fprintf(proxy, "#include \"rpcproxy.h\"\n");
-  fprintf(proxy, "#include \"%s\"\n", header_name);
-  fprintf(proxy, "\n");
-  write_stubdesc();
+  print_proxy( "/*** Autogenerated by WIDL %s - Do not edit ***/\n", WIDL_FULLVERSION);
+  print_proxy( "\n");
+  print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
+  print_proxy( "#define __REDQ_RPCPROXY_H_VERSION__ 440\n");
+  print_proxy( "#endif /* __REDQ_RPCPROXY_H_VERSION__ */\n");
+  print_proxy( "\n");
+  print_proxy( "#include \"rpcproxy.h\"\n");
+  print_proxy( "\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");
+  write_formatstringsdecl();
+  write_stubdescproto();
 }
 
 static void clear_output_vars( var_t *arg )
@@ -69,9 +122,9 @@
       arg = NEXT_LINK(arg);
   }
   while (arg) {
-    if (is_attr(arg->attrs, ATTR_OUT)) {
-      fprintf( proxy, "    if(%s)\n", arg->name );
-      fprintf( proxy, "        MIDL_memset( %s, 0, sizeof( ", arg->name );
+    if (is_attr(arg->attrs, ATTR_OUT) && !is_attr(arg->attrs, ATTR_IN)) {
+      print_proxy( "if(%s)\n", arg->name );
+      print_proxy( "    MIDL_memset( %s, 0, sizeof( ", arg->name );
       write_type(proxy, arg->type, arg, arg->tname);
       fprintf( proxy, " ));\n" );
     }
@@ -79,49 +132,221 @@
   }
 }
 
+static void gen_marshall_size( var_t *arg )
+{
+  print_proxy( "_StubMsg.BufferLength = 0U;\n" );
+
+  if (arg) {
+    while (NEXT_LINK(arg))
+      arg = NEXT_LINK(arg);
+  }
+  while (arg) {
+    if (is_attr(arg->attrs, ATTR_IN)) {
+      int index = 0;
+      type_t *type = arg->type;
+
+      /* find the base type */
+      while( type->ref )
+        type = type->ref;
+
+      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_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;
+
+      default:
+        printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
+      }
+    }
+    arg = PREV_LINK(arg);
+  }
+}
+
+static void gen_marshall_copydata( var_t *arg )
+{
+  if (arg) {
+    while (NEXT_LINK(arg))
+      arg = NEXT_LINK(arg);
+  }
+  while (arg) {
+    if (is_attr(arg->attrs, ATTR_IN)) {
+      int index = 0;
+      type_t *type = arg->type;
+
+      /* find the base type */
+      while( type->ref )
+        type = type->ref;
+
+      switch( type->type )
+      {
+      case RPC_FC_BYTE:
+      case RPC_FC_CHAR:
+      case RPC_FC_SHORT:
+      case RPC_FC_USHORT:
+      case RPC_FC_ENUM16:
+      case RPC_FC_LONG:
+      case RPC_FC_ULONG:
+      case RPC_FC_ENUM32:
+        print_proxy( "*((");
+        write_type(proxy, arg->type, arg, arg->tname);
+        fprintf(proxy,"*)_StubMsg.Buffer)++ = %s;\n", arg->name );
+        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;
+
+      default:
+        printf("FIXME: arg %s has unknown type %d\n", arg->name, type->type );
+      }
+    }
+    arg = PREV_LINK(arg);
+  }
+}
+
+static void gen_marshall( var_t *arg )
+{
+  /* generated code to determine the size of the buffer required */
+  gen_marshall_size( arg );
+
+  /* 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");
+}
+
+static void gen_unmarshall( var_t *arg )
+{
+  if (arg) {
+    while (NEXT_LINK(arg))
+      arg = NEXT_LINK(arg);
+  }
+  while (arg) {
+    if (is_attr(arg->attrs, ATTR_OUT)) {
+      int index = 0;
+      /* FIXME: add the format string, and set the index below */
+      print_proxy( "NdrPointerUnmarshall(&_StubMsg, (unsigned char**)&%s, ", arg->name );
+      fprintf(proxy, "&__MIDL_TypeFormatString.Format[%d], 0);\n", index );
+    }
+    arg = PREV_LINK(arg);
+  }
+}
+
 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);
 
+  indent = 0;
   write_type(proxy, def->type, def, def->tname);
-  fprintf(proxy, " CALLBACK %s_", iface->name);
+  print_proxy( " CALLBACK %s_", iface->name);
   write_name(proxy, def);
-  fprintf(proxy, "_Proxy(\n");
+  print_proxy( "_Proxy(\n");
   write_args(proxy, cur->args, iface->name, 1, TRUE);
-  fprintf(proxy, ")\n");
-  fprintf(proxy, "{\n");
+  print_proxy( ")\n");
+  print_proxy( "{\n");
+  indent ++;
   /* local variables */
   if (has_ret) {
-    fprintf(proxy, "    ");
+    print_proxy( "" );
     write_type(proxy, def->type, def, def->tname);
-    fprintf(proxy, " _Ret;\n");
+    print_proxy( " _Ret;\n");
   }
-  fprintf(proxy, "    RPC_MESSAGE _Msg;\n");
-  fprintf(proxy, "    MIDL_STUB_MESSAGE _StubMsg;\n");
-  fprintf(proxy, "\n");
+  print_proxy( "RPC_MESSAGE _Msg;\n" );
+  print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n" );
+  print_proxy( "\n");
 
   /* FIXME: trace */
   clear_output_vars( cur->args );
 
-  fprintf(proxy, "    NdrProxyInitialize(This, &_Msg, &_StubMsg, &Object_StubDesc, %d);\n", idx);
+  print_proxy( "RpcTryExcept {\n" );
+  indent++;
+  print_proxy( "NdrProxyInitialize(This, &_Msg, &_StubMsg, &Object_StubDesc, %d);\n", idx);
 
-  /* FIXME: size buffer */
+  print_proxy( "RpcTryFinally {\n" );
+  indent++;
 
-  fprintf(proxy, "    NdrProxyGetBuffer(This, &_StubMsg);\n");
+  gen_marshall( cur->args );
 
-  /* FIXME: marshall */
+  print_proxy( "NdrProxySendReceive(This, &_StubMsg);\n" );
+  print_proxy( "\n");
 
-  fprintf(proxy, "    NdrProxySendReceive(This, &_StubMsg);\n");
+  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");
 
-  /* FIXME: unmarshall */
+    print_proxy( "_Ret = *((" );
+    write_type(proxy, def->type, def, def->tname);
+    fprintf(proxy, "*)_StubMsg.Buffer)++;\n");
+  }
 
-  fprintf(proxy, "    NdrProxyFreeBuffer(This, &_StubMsg);\n");
+  indent--;
+  print_proxy( "}\n");
+  print_proxy( "RpcFinally {\n" );
+  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++;
+    print_proxy( "_Ret = NdrProxyErrorHandler(RpcExceptionCode());\n" );
+    indent--;
+  }
+  print_proxy( "}\n" );
+  print_proxy( "RpcEndExcept\n" );
 
   if (has_ret) {
-    fprintf(proxy, "    return _Ret;\n");
+    print_proxy( "return _Ret;\n" );
+  }
+  indent--;
+  print_proxy( "}\n");
+  print_proxy( "\n");
+}
+
+static void stub_clear_output_vars( var_t *arg )
+{
+  while (arg) {
+    print_proxy("(");
+    write_type(proxy, arg->type, arg, arg->tname);
+    fprintf(proxy, ") ");
+    write_name(proxy, arg);
+    fprintf(proxy, " = 0;\n");
+    arg = NEXT_LINK(arg);
   }
-  fprintf(proxy, "}\n");
   fprintf(proxy, "\n");
 }
 
@@ -131,25 +356,29 @@
   var_t *arg;
   int has_ret = !is_void(def->type, def);
 
-  fprintf(proxy, "void __RPC_STUB %s_", iface->name);
+  indent = 0;
+  print_proxy( "void __RPC_STUB %s_", iface->name);
   write_name(proxy, def);
-  fprintf(proxy, "_Stub(\n");
-  fprintf(proxy, "    IRpcStubBuffer* This,\n");
-  fprintf(proxy, "    IRpcChannelBuffer* pRpcChannelBuffer,\n");
-  fprintf(proxy, "    PRPC_MESSAGE pRpcMessage,\n");
-  fprintf(proxy, "    DWORD* pdwStubPhase)\n");
-  fprintf(proxy, "{\n");
+  print_proxy( "_Stub(\n");
+  indent++;
+  print_proxy( "IRpcStubBuffer* This,\n");
+  print_proxy( "IRpcChannelBuffer* pRpcChannelBuffer,\n");
+  print_proxy( "PRPC_MESSAGE pRpcMessage,\n");
+  print_proxy( "DWORD* pdwStubPhase)\n");
+  indent--;
+  print_proxy( "{\n");
+  indent++;
   /* local variables */
   if (has_ret) {
-    fprintf(proxy, "    ");
+    print_proxy("");
     write_type(proxy, def->type, def, def->tname);
     fprintf(proxy, " _Ret;\n");
   }
-  fprintf(proxy, "    %s* _This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n", iface->name, iface->name);
-  fprintf(proxy, "    MIDL_STUB_MESSAGE _StubMsg;\n");
+  print_proxy("%s* _This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n", iface->name, iface->name);
+  print_proxy("MIDL_STUB_MESSAGE _StubMsg;\n");
   arg = cur->args;
   while (arg) {
-    fprintf(proxy, "    ");
+    print_proxy("");
     write_type(proxy, arg->type, arg, arg->tname);
     fprintf(proxy, " ");
     write_name(proxy, arg);
@@ -159,14 +388,17 @@
   fprintf(proxy, "\n");
 
   /* FIXME: trace */
-  /* FIXME: clear output vars? */
 
-  fprintf(proxy, "    NdrStubInitialize(pRpcMessage, &_StubMsg, &Object_StubDesc, pRpcChannelBuffer);\n");
+  print_proxy("NdrStubInitialize(pRpcMessage, &_StubMsg, &Object_StubDesc, pRpcChannelBuffer);\n");
+  fprintf(proxy, "\n");
+
+  /* clear output vars */
+  stub_clear_output_vars( cur->args );
 
   /* FIXME: unmarshall */
 
-  fprintf(proxy, "    *pdwStubPhase = STUB_CALL_SERVER;\n");
-  fprintf(proxy, "    ");
+  print_proxy("*pdwStubPhase = STUB_CALL_SERVER;\n");
+  print_proxy("");
   if (has_ret) fprintf(proxy, "_Ret = ");
   fprintf(proxy, "%s_", iface->name);
   if (cas) fprintf(proxy, "%s_Stub", cas);
@@ -182,16 +414,17 @@
     }
   }
   fprintf(proxy, ");\n");
-  fprintf(proxy, "    *pdwStubPhase = STUB_MARSHAL;\n");
+  print_proxy("*pdwStubPhase = STUB_MARSHAL;\n");
 
   /* FIXME: size buffer */
 
-  fprintf(proxy, "    NdrStubGetBuffer(This, pRpcChannelBuffer, &_StubMsg);\n");
+  print_proxy("NdrStubGetBuffer(This, pRpcChannelBuffer, &_StubMsg);\n");
 
   /* FIXME: marshall */
+  indent--;
 
-  fprintf(proxy, "}\n");
-  fprintf(proxy, "\n");
+  print_proxy("}\n");
+  print_proxy("\n");
 }
 
 static int write_proxy_methods(type_t *iface)
@@ -275,6 +508,8 @@
     cur = PREV_LINK(cur);
   }
 
+  write_stubdesc();
+
   /* proxy vtable */
   fprintf(proxy, "const CINTERFACE_PROXY_VTABLE(%d) %sProxyVtbl = {\n", midx, iface->name);
   fprintf(proxy, "    {&IID_%s},\n", iface->name);
@@ -322,7 +557,8 @@
   fprintf(proxy, "const CInterfaceProxyVtbl* _%s_ProxyVtblList[] = {\n", file_id);
   cur = lcur;
   while (cur) {
-    fprintf(proxy, "    (CInterfaceProxyVtbl*)&%sProxyVtbl,\n", cur->iface->name);
+    if(cur->iface->ref)
+      fprintf(proxy, "    (CInterfaceProxyVtbl*)&%sProxyVtbl,\n", cur->iface->name);
     cur = PREV_LINK(cur);
   }
   fprintf(proxy, "    0\n");
@@ -332,7 +568,8 @@
   fprintf(proxy, "const CInterfaceStubVtbl* _%s_StubVtblList[] = {\n", file_id);
   cur = lcur;
   while (cur) {
-    fprintf(proxy, "    (CInterfaceStubVtbl*)&%sStubVtbl,\n", cur->iface->name);
+    if(cur->iface->ref)
+      fprintf(proxy, "    (CInterfaceStubVtbl*)&%sStubVtbl,\n", cur->iface->name);
     cur = PREV_LINK(cur);
   }
   fprintf(proxy, "    0\n");
@@ -342,26 +579,30 @@
   fprintf(proxy, "const PCInterfaceName _%s_InterfaceNamesList[] = {\n", file_id);
   cur = lcur;
   while (cur) {
-    fprintf(proxy, "    \"%s\",\n", cur->iface->name);
+    if(cur->iface->ref)
+      fprintf(proxy, "    \"%s\",\n", cur->iface->name);
     cur = PREV_LINK(cur);
   }
   fprintf(proxy, "    0\n");
   fprintf(proxy, "};\n");
   fprintf(proxy, "\n");
 
-  fprintf(proxy, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_XXX, pIID, n)\n", file_id);
+  fprintf(proxy, "#define _%s_CHECK_IID(n) IID_GENERIC_CHECK_IID(_%s, pIID, n)\n", file_id, file_id);
   fprintf(proxy, "\n");
   fprintf(proxy, "int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
   fprintf(proxy, "{\n");
   cur = lcur;
   c = 0;
   while (cur) {
-    fprintf(proxy, "    if (!_%s_CHECK_IID(%d)) {\n", file_id, c);
-    fprintf(proxy, "        *pIndex = %d\n", c);
-    fprintf(proxy, "        return 1;\n");
-    fprintf(proxy, "    }\n");
+    if(cur->iface->ref)
+    {
+      fprintf(proxy, "    if (!_%s_CHECK_IID(%d)) {\n", file_id, c);
+      fprintf(proxy, "        *pIndex = %d;\n", c);
+      fprintf(proxy, "        return 1;\n");
+      fprintf(proxy, "    }\n");
+      c++;
+    }
     cur = PREV_LINK(cur);
-    c++;
   }
   fprintf(proxy, "    return 0;\n");
   fprintf(proxy, "}\n");


More information about the wine-patches mailing list