Sync to Wine-0.9.58
[reactos.git] / reactos / tools / widl / header.c
index ec5c8c7..0a28fd9 100644 (file)
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
 #include "config.h"
 
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif
 #include <string.h>
-#include <assert.h>
 #include <ctype.h>
-#include <signal.h>
 
 #include "widl.h"
 #include "utils.h"
 #include "header.h"
 
 static int indentation = 0;
+user_type_list_t user_type_list = LIST_INIT(user_type_list);
+static context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
 
-static void indent(int delta)
+static void indent(FILE *h, int delta)
 {
   int c;
   if (delta < 0) indentation += delta;
-  for (c=0; c<indentation; c++) fprintf(header, "    ");
+  for (c=0; c<indentation; c++) fprintf(h, "    ");
   if (delta > 0) indentation += delta;
 }
 
-int is_base_type(type_t *t)
+int is_ptrchain_attr(const var_t *var, enum attr_type t)
 {
-  return (t->type == RPC_FC_BYTE || t->type == RPC_FC_CHAR ||
-          t->type == RPC_FC_SMALL || t->type == RPC_FC_USMALL ||
-          t->type == RPC_FC_WCHAR || t->type == RPC_FC_SHORT ||
-          t->type == RPC_FC_USHORT || t->type == RPC_FC_LONG ||
-          t->type == RPC_FC_ULONG || t->type == RPC_FC_FLOAT ||
-          t->type == RPC_FC_HYPER || t->type == RPC_FC_DOUBLE ||
-          t->type == RPC_FC_ENUM16 || t->type == RPC_FC_ENUM32 ||
-          t->type == RPC_FC_IGNORE);
+    if (is_attr(var->attrs, t))
+        return 1;
+    else
+    {
+        type_t *type = var->type;
+        for (;;)
+        {
+            if (is_attr(type->attrs, t))
+                return 1;
+            else if (type->kind == TKIND_ALIAS)
+                type = type->orig;
+            else if (is_ptr(type))
+                type = type->ref;
+            else return 0;
+        }
+    }
 }
 
-int is_attr(attr_t *a, enum attr_type t)
+int is_attr(const attr_list_t *list, enum attr_type t)
 {
-  while (a) {
-    if (a->type == t) return 1;
-    a = NEXT_LINK(a);
-  }
-  return 0;
+    const attr_t *attr;
+    if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
+        if (attr->type == t) return 1;
+    return 0;
 }
 
-void *get_attrp(attr_t *a, enum attr_type t)
+void *get_attrp(const attr_list_t *list, enum attr_type t)
 {
-  while (a) {
-    if (a->type == t) return a->u.pval;
-    a = NEXT_LINK(a);
-  }
-  return NULL;
+    const attr_t *attr;
+    if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
+        if (attr->type == t) return attr->u.pval;
+    return NULL;
 }
 
-unsigned long get_attrv(attr_t *a, enum attr_type t)
+unsigned long get_attrv(const attr_list_t *list, enum attr_type t)
 {
-  while (a) {
-    if (a->type == t) return a->u.ival;
-    a = NEXT_LINK(a);
-  }
-  return 0;
+    const attr_t *attr;
+    if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
+        if (attr->type == t) return attr->u.ival;
+    return 0;
 }
 
-int is_void(type_t *t, var_t *v)
+int is_void(const type_t *t)
 {
-  if (v && v->ptr_level) return 0;
   if (!t->type && !t->ref) return 1;
   return 0;
 }
 
-static void write_pident(FILE *h, var_t *v)
+int is_conformant_array(const type_t *t)
 {
-  int c;
-  for (c=0; c<v->ptr_level; c++) {
-    fprintf(h, "*");
-  }
-  if (v->name) fprintf(h, "%s", v->name);
+    return t->type == RPC_FC_CARRAY
+        || t->type == RPC_FC_CVARRAY
+        || (t->type == RPC_FC_BOGUS_ARRAY && t->size_is);
+}
+
+void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
+{
+  if (!uuid) return;
+  fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
+        "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
+        guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
+        uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
+        uuid->Data4[6], uuid->Data4[7]);
 }
 
-void write_name(FILE *h, var_t *v)
+void write_name(FILE *h, const var_t *v)
 {
   if (is_attr( v->attrs, ATTR_PROPGET ))
     fprintf(h, "get_" );
   else if (is_attr( v->attrs, ATTR_PROPPUT ))
     fprintf(h, "put_" );
+  else if (is_attr( v->attrs, ATTR_PROPPUTREF ))
+    fprintf(h, "putref_" );
   fprintf(h, "%s", v->name);
 }
 
-char* get_name(var_t *v)
-{
-  return v->name;
-}
-
-static void write_array(FILE *h, expr_t *v, int field)
+void write_prefix_name(FILE *h, const char *prefix, const var_t *v)
 {
-  if (!v) return;
-  while (NEXT_LINK(v)) v = NEXT_LINK(v);
-  fprintf(h, "[");
-  while (v) {
-    if (v->is_const)
-      fprintf(h, "%ld", v->cval); /* statically sized array */
-    else
-      if (field) fprintf(h, "1"); /* dynamically sized array */
-    if (PREV_LINK(v))
-      fprintf(h, ", ");
-    v = PREV_LINK(v);
-  }
-  fprintf(h, "]");
+  fprintf(h, "%s", prefix);
+  write_name(h, v);
 }
 
 static void write_field(FILE *h, var_t *v)
 {
   if (!v) return;
   if (v->type) {
-    indent(0);
-    write_type(h, v->type, NULL, v->tname);
-    if (get_name(v)) {
-      fprintf(h, " ");
-      write_pident(h, v);
-    }
-    else {
-      /* not all C/C++ compilers support anonymous structs and unions */
+    const char *name = v->name;
+    if (name == NULL) {
       switch (v->type->type) {
       case RPC_FC_STRUCT:
       case RPC_FC_CVSTRUCT:
@@ -151,115 +144,78 @@ static void write_field(FILE *h, var_t *v)
       case RPC_FC_PSTRUCT:
       case RPC_FC_BOGUS_STRUCT:
       case RPC_FC_ENCAPSULATED_UNION:
-        fprintf(h, " DUMMYSTRUCTNAME");
+        name = "DUMMYSTRUCTNAME";
         break;
       case RPC_FC_NON_ENCAPSULATED_UNION:
-        fprintf(h, " DUMMYUNIONNAME");
+        name = "DUMMYUNIONNAME";
         break;
       default:
         /* ? */
         break;
       }
     }
-    write_array(h, v->array, 1);
+    indent(h, 0);
+    write_type_def_or_decl(h, v->type, TRUE, "%s", name);
     fprintf(h, ";\n");
   }
 }
 
-static void write_fields(FILE *h, var_t *v)
+static void write_fields(FILE *h, var_list_t *fields)
 {
-  var_t *first = v;
-  if (!v) return;
-  while (NEXT_LINK(v)) v = NEXT_LINK(v);
-  while (v) {
-    write_field(h, v);
-    if (v == first) break;
-    v = PREV_LINK(v);
-  }
+    var_t *v;
+    if (!fields) return;
+    LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) write_field(h, v);
 }
 
-static void write_enums(FILE *h, var_t *v)
+static void write_enums(FILE *h, var_list_t *enums)
 {
-  if (!v) return;
-  while (NEXT_LINK(v)) v = NEXT_LINK(v);
-  while (v) {
-    if (get_name(v)) {
-      indent(0);
+  var_t *v;
+  if (!enums) return;
+  LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
+  {
+    if (v->name) {
+      indent(h, 0);
       write_name(h, v);
       if (v->eval) {
         fprintf(h, " = ");
-        write_expr(h, v->eval);
+        write_expr(h, v->eval, 0);
       }
     }
-    if (PREV_LINK(v))
-      fprintf(h, ",\n");
-    v = PREV_LINK(v);
+    if (list_next( enums, &v->entry )) fprintf(h, ",\n");
   }
   fprintf(h, "\n");
 }
 
-void write_type(FILE *h, type_t *t, var_t *v, char *n)
+int needs_space_after(type_t *t)
 {
-  int c;
+  return (t->kind == TKIND_ALIAS
+          || (!is_ptr(t) && (!is_conformant_array(t) || t->declarray)));
+}
 
-  if (n) fprintf(h, "%s", n);
+void write_type_left(FILE *h, type_t *t, int declonly)
+{
+  if (!h) return;
+
+  if (t->is_const) fprintf(h, "const ");
+
+  if (t->kind == TKIND_ALIAS) fprintf(h, "%s", t->name);
+  else if (t->declarray) write_type_left(h, t->ref, declonly);
   else {
-    if (t->is_const) fprintf(h, "const ");
-    if (t->type) {
-      if (t->sign > 0) fprintf(h, "signed ");
-      else if (t->sign < 0) fprintf(h, "unsigned ");
-      switch (t->type) {
-      case RPC_FC_BYTE:
-        if (t->ref) fprintf(h, t->ref->name);
-        else fprintf(h, "byte");
-        break;
-      case RPC_FC_CHAR:
-        if (t->ref) fprintf(h, t->ref->name);
-        else fprintf(h, "char");
-        break;
-      case RPC_FC_WCHAR:
-        fprintf(h, "wchar_t");
-        break;
-      case RPC_FC_USHORT:
-      case RPC_FC_SHORT:
-        if (t->ref) fprintf(h, t->ref->name);
-        else fprintf(h, "short");
-        break;
-      case RPC_FC_ULONG:
-      case RPC_FC_LONG:
-        if (t->ref) fprintf(h, t->ref->name);
-        else fprintf(h, "long");
-        break;
-      case RPC_FC_HYPER:
-        if (t->ref) fprintf(h, t->ref->name);
-        else fprintf(h, "hyper");
-        break;
-      case RPC_FC_FLOAT:
-        fprintf(h, "float");
-        break;
-      case RPC_FC_DOUBLE:
-        fprintf(h, "double");
-        break;
+    if (t->sign > 0) fprintf(h, "signed ");
+    else if (t->sign < 0) fprintf(h, "unsigned ");
+    switch (t->type) {
       case RPC_FC_ENUM16:
       case RPC_FC_ENUM32:
-        if (t->defined && !t->written) {
+        if (!declonly && t->defined && !t->written && !t->ignore) {
           if (t->name) fprintf(h, "enum %s {\n", t->name);
           else fprintf(h, "enum {\n");
           t->written = TRUE;
           indentation++;
           write_enums(h, t->fields);
-          indent(-1);
+          indent(h, -1);
           fprintf(h, "}");
         }
-        else fprintf(h, "enum %s", t->name);
-        break;
-      case RPC_FC_ERROR_STATUS_T:
-        if (t->ref) fprintf(h, t->ref->name);
-        else fprintf(h, "error_status_t");
-        break;
-      case RPC_FC_IGNORE:
-        if (t->ref) fprintf(h, t->ref->name);
-        else fprintf(h, "handle_t");
+        else fprintf(h, "enum %s", t->name ? t->name : "");
         break;
       case RPC_FC_STRUCT:
       case RPC_FC_CVSTRUCT:
@@ -268,116 +224,227 @@ void write_type(FILE *h, type_t *t, var_t *v, char *n)
       case RPC_FC_PSTRUCT:
       case RPC_FC_BOGUS_STRUCT:
       case RPC_FC_ENCAPSULATED_UNION:
-        if (t->defined && !t->written) {
+        if (!declonly && t->defined && !t->written && !t->ignore) {
           if (t->name) fprintf(h, "struct %s {\n", t->name);
           else fprintf(h, "struct {\n");
           t->written = TRUE;
           indentation++;
           write_fields(h, t->fields);
-          indent(-1);
+          indent(h, -1);
           fprintf(h, "}");
         }
-        else fprintf(h, "struct %s", t->name);
+        else fprintf(h, "struct %s", t->name ? t->name : "");
         break;
       case RPC_FC_NON_ENCAPSULATED_UNION:
-        if (t->defined && !t->written) {
+        if (!declonly && t->defined && !t->written && !t->ignore) {
           if (t->name) fprintf(h, "union %s {\n", t->name);
           else fprintf(h, "union {\n");
           t->written = TRUE;
           indentation++;
           write_fields(h, t->fields);
-          indent(-1);
+          indent(h, -1);
           fprintf(h, "}");
         }
-        else fprintf(h, "union %s", t->name);
+        else fprintf(h, "union %s", t->name ? t->name : "");
+        break;
+      case RPC_FC_RP:
+      case RPC_FC_UP:
+      case RPC_FC_FP:
+      case RPC_FC_OP:
+      case RPC_FC_CARRAY:
+      case RPC_FC_CVARRAY:
+      case RPC_FC_BOGUS_ARRAY:
+        write_type_left(h, t->ref, declonly);
+        fprintf(h, "%s*", needs_space_after(t->ref) ? " " : "");
         break;
       default:
-        fprintf(h, "(unknown-type:%d)", t->type);
-      }
-    }
-    else {
-      if (t->ref) {
-        write_type(h, t->ref, NULL, t->name);
-      }
-      else fprintf(h, "void");
+        fprintf(h, "%s", t->name);
     }
   }
-  if (v) {
-    for (c=0; c<v->ptr_level; c++) {
-      fprintf(h, "*");
+}
+
+void write_type_right(FILE *h, type_t *t, int is_field)
+{
+  if (!h) return;
+
+  if (t->declarray) {
+    if (is_conformant_array(t)) {
+      fprintf(h, "[%s]", is_field ? "1" : "");
+      t = t->ref;
     }
+    for ( ; t->declarray; t = t->ref)
+      fprintf(h, "[%lu]", t->dim);
   }
 }
 
-void write_typedef(type_t *type, var_t *names)
+void write_type_v(FILE *h, type_t *t, int is_field, int declonly,
+                  const char *fmt, va_list args)
 {
-  char *tname = names->tname;
-  var_t *lname;
-  while (NEXT_LINK(names)) names = NEXT_LINK(names);
-  lname = names;
-  fprintf(header, "typedef ");
-  write_type(header, type, NULL, tname);
-  fprintf(header, " ");
-  while (names) {
-    write_pident(header, names);
-    if (PREV_LINK(names))
-      fprintf(header, ", ");
-    names = PREV_LINK(names);
+  if (!h) return;
+
+  write_type_left(h, t, declonly);
+  if (fmt) {
+    if (needs_space_after(t))
+      fprintf(h, " ");
+    vfprintf(h, fmt, args);
   }
-  fprintf(header, ";\n");
+  write_type_right(h, t, is_field);
+}
+
+void write_type_def_or_decl(FILE *f, type_t *t, int field, const char *fmt, ...)
+{
+  va_list args;
+  va_start(args, fmt);
+  write_type_v(f, t, field, FALSE, fmt, args);
+  va_end(args);
+}
+
+void write_type_decl(FILE *f, type_t *t, const char *fmt, ...)
+{
+  va_list args;
+  va_start(args, fmt);
+  write_type_v(f, t, FALSE, TRUE, fmt, args);
+  va_end(args);
+}
+
+void write_type_decl_left(FILE *f, type_t *t)
+{
+  write_type_left(f, t, TRUE);
+}
+
+static int user_type_registered(const char *name)
+{
+  user_type_t *ut;
+  LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
+    if (!strcmp(name, ut->name))
+      return 1;
+  return 0;
+}
+
+static int context_handle_registered(const char *name)
+{
+  context_handle_t *ch;
+  LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
+    if (!strcmp(name, ch->name))
+      return 1;
+  return 0;
+}
+
+void check_for_user_types_and_context_handles(const var_list_t *list)
+{
+  const var_t *v;
 
-  if (get_attrp(type->attrs, ATTR_WIREMARSHAL)) {
-    names = lname;
-    while (names) {
-      char *name = get_name(names);
-      fprintf(header, "unsigned long   __RPC_USER %s_UserSize     (unsigned long *, unsigned long,   %s *);\n", name, name);
-      fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal  (unsigned long *, unsigned char *, %s *);\n", name, name);
-      fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(unsigned long *, unsigned char *, %s *);\n", name, name);
-      fprintf(header, "void            __RPC_USER %s_UserFree     (unsigned long *, %s *);\n", name, name);
-      if (PREV_LINK(names))
-        fprintf(header, ", ");
-      names = PREV_LINK(names);
+  if (!list) return;
+  LIST_FOR_EACH_ENTRY( v, list, const var_t, entry )
+  {
+    type_t *type;
+    for (type = v->type; type; type = type->kind == TKIND_ALIAS ? type->orig : type->ref) {
+      const char *name = type->name;
+      if (type->user_types_registered) continue;
+      type->user_types_registered = 1;
+      if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
+        if (!context_handle_registered(name))
+        {
+          context_handle_t *ch = xmalloc(sizeof(*ch));
+          ch->name = xstrdup(name);
+          list_add_tail(&context_handle_list, &ch->entry);
+        }
+        /* don't carry on parsing fields within this type */
+        break;
+      }
+      if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
+        if (!user_type_registered(name))
+        {
+          user_type_t *ut = xmalloc(sizeof *ut);
+          ut->name = xstrdup(name);
+          list_add_tail(&user_type_list, &ut->entry);
+        }
+        /* don't carry on parsing fields within this type as we are already
+         * using a wire marshaled type */
+        break;
+      }
+      else
+      {
+        check_for_user_types_and_context_handles(type->fields);
+      }
     }
   }
+}
 
-  fprintf(header, "\n");
+void write_user_types(void)
+{
+  user_type_t *ut;
+  LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
+  {
+    const char *name = ut->name;
+    fprintf(header, "ULONG           __RPC_USER %s_UserSize     (ULONG *, ULONG, %s *);\n", name, name);
+    fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal  (ULONG *, unsigned char *, %s *);\n", name, name);
+    fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
+    fprintf(header, "void            __RPC_USER %s_UserFree     (ULONG *, %s *);\n", name, name);
+  }
+}
+
+void write_context_handle_rundowns(void)
+{
+  context_handle_t *ch;
+  LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
+  {
+    const char *name = ch->name;
+    fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
+  }
 }
 
-static void do_write_expr(FILE *h, expr_t *e, int p)
+void write_typedef(type_t *type)
+{
+  fprintf(header, "typedef ");
+  write_type_def_or_decl(header, type->orig, FALSE, "%s", type->name);
+  fprintf(header, ";\n");
+}
+
+void write_expr(FILE *h, const expr_t *e, int brackets)
 {
   switch (e->type) {
   case EXPR_VOID:
     break;
   case EXPR_NUM:
-    fprintf(h, "%ld", e->u.lval);
+    fprintf(h, "%lu", e->u.lval);
     break;
   case EXPR_HEXNUM:
     fprintf(h, "0x%lx", e->u.lval);
     break;
+  case EXPR_DOUBLE:
+    fprintf(h, "%#.15g", e->u.dval);
+    break;
+  case EXPR_TRUEFALSE:
+    if (e->u.lval == 0)
+      fprintf(h, "FALSE");
+    else
+      fprintf(h, "TRUE");
+    break;
   case EXPR_IDENTIFIER:
     fprintf(h, "%s", e->u.sval);
     break;
   case EXPR_NEG:
     fprintf(h, "-");
-    do_write_expr(h, e->ref, 1);
+    write_expr(h, e->ref, 1);
     break;
   case EXPR_NOT:
     fprintf(h, "~");
-    do_write_expr(h, e->ref, 1);
+    write_expr(h, e->ref, 1);
     break;
   case EXPR_PPTR:
     fprintf(h, "*");
-    do_write_expr(h, e->ref, 1);
+    write_expr(h, e->ref, 1);
     break;
   case EXPR_CAST:
     fprintf(h, "(");
-    write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
+    write_type_decl(h, e->u.tref, NULL);
     fprintf(h, ")");
-    do_write_expr(h, e->ref, 1);
+    write_expr(h, e->ref, 1);
     break;
   case EXPR_SIZEOF:
     fprintf(h, "sizeof(");
-    write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
+    write_type_decl(h, e->u.tref, NULL);
     fprintf(h, ")");
     break;
   case EXPR_SHL:
@@ -388,8 +455,8 @@ static void do_write_expr(FILE *h, expr_t *e, int p)
   case EXPR_SUB:
   case EXPR_AND:
   case EXPR_OR:
-    if (p) fprintf(h, "(");
-    do_write_expr(h, e->ref, 1);
+    if (brackets) fprintf(h, "(");
+    write_expr(h, e->ref, 1);
     switch (e->type) {
     case EXPR_SHL: fprintf(h, " << "); break;
     case EXPR_SHR: fprintf(h, " >> "); break;
@@ -401,167 +468,160 @@ static void do_write_expr(FILE *h, expr_t *e, int p)
     case EXPR_OR:  fprintf(h, " | "); break;
     default: break;
     }
-    do_write_expr(h, e->u.ext, 1);
-    if (p) fprintf(h, ")");
+    write_expr(h, e->u.ext, 1);
+    if (brackets) fprintf(h, ")");
     break;
   case EXPR_COND:
-    if (p) fprintf(h, "(");
-    do_write_expr(h, e->ref, 1);
+    if (brackets) fprintf(h, "(");
+    write_expr(h, e->ref, 1);
     fprintf(h, " ? ");
-    do_write_expr(h, e->u.ext, 1);
+    write_expr(h, e->u.ext, 1);
     fprintf(h, " : ");
-    do_write_expr(h, e->ext2, 1);
-    if (p) fprintf(h, ")");
+    write_expr(h, e->ext2, 1);
+    if (brackets) fprintf(h, ")");
+    break;
+  case EXPR_ADDRESSOF:
+    fprintf(h, "&");
+    write_expr(h, e->ref, 1);
     break;
   }
 }
 
-void write_expr(FILE *h, expr_t *e)
+void write_constdef(const var_t *v)
 {
-  do_write_expr(h, e, 0);
-}
-
-void write_constdef(var_t *v)
-{
-  fprintf(header, "#define %s (", get_name(v));
-  write_expr(header, v->eval);
+  fprintf(header, "#define %s (", v->name);
+  write_expr(header, v->eval, 0);
   fprintf(header, ")\n\n");
 }
 
-void write_externdef(var_t *v)
+void write_externdef(const var_t *v)
 {
   fprintf(header, "extern const ");
-  write_type(header, v->type, NULL, v->tname);
-  if (get_name(v)) {
-    fprintf(header, " ");
-    write_pident(header, v);
-  }
+  write_type_def_or_decl(header, v->type, FALSE, "%s", v->name);
   fprintf(header, ";\n\n");
 }
 
+void write_library(const char *name, const attr_list_t *attr)
+{
+  const UUID *uuid = get_attrp(attr, ATTR_UUID);
+  fprintf(header, "\n");
+  write_guid(header, "LIBID", name, uuid);
+  fprintf(header, "\n");
+}
+
 
-var_t* get_explicit_handle_var(func_t* func)
+const var_t* get_explicit_handle_var(const func_t* func)
 {
-    var_t* var;
+    const var_t* var;
 
     if (!func->args)
         return NULL;
 
-    var = func->args;
-    while (NEXT_LINK(var)) var = NEXT_LINK(var);
-    while (var)
-    {
-        if (var->type->type == RPC_FC_IGNORE)
+    LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
+        if (var->type->type == RPC_FC_BIND_PRIMITIVE)
             return var;
 
-        var = PREV_LINK(var);
-    }
-
     return NULL;
 }
 
+int has_out_arg_or_return(const func_t *func)
+{
+    const var_t *var;
+
+    if (!is_void(func->def->type))
+        return 1;
+
+    if (!func->args)
+        return 0;
+
+    LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
+        if (is_attr(var->attrs, ATTR_OUT))
+            return 1;
+
+    return 0;
+}
+
 
 /********** INTERFACES **********/
 
-int is_object(attr_t *a)
+int is_object(const attr_list_t *list)
 {
-  while (a) {
-    if (a->type == ATTR_OBJECT || a->type == ATTR_ODL) return 1;
-    a = NEXT_LINK(a);
-  }
-  return 0;
+    const attr_t *attr;
+    if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
+        if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
+    return 0;
 }
 
-int is_local(attr_t *a)
+int is_local(const attr_list_t *a)
 {
   return is_attr(a, ATTR_LOCAL);
 }
 
-var_t *is_callas(attr_t *a)
+const var_t *is_callas(const attr_list_t *a)
 {
   return get_attrp(a, ATTR_CALLAS);
 }
 
-static int write_method_macro(type_t *iface, char *name)
+static void write_method_macro(const type_t *iface, const char *name)
 {
-  int idx;
-  func_t *cur = iface->funcs;
+  const func_t *cur;
 
-  if (iface->ref) idx = write_method_macro(iface->ref, name);
-  else idx = 0;
+  if (iface->ref) write_method_macro(iface->ref, name);
 
-  if (!cur) return idx;
-  while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
+  if (!iface->funcs) return;
 
   fprintf(header, "/*** %s methods ***/\n", iface->name);
-  while (cur) {
+  LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
+  {
     var_t *def = cur->def;
     if (!is_callas(def->attrs)) {
-      var_t *arg = cur->args;
-      int argc = 0;
-      int c;
-      while (arg) {
-       arg = NEXT_LINK(arg);
-       argc++;
-      }
+      const var_t *arg;
 
       fprintf(header, "#define %s_", name);
       write_name(header,def);
-      fprintf(header, "(p");
-      for (c=0; c<argc; c++)
-       fprintf(header, ",%c", c+'a');
+      fprintf(header, "(This");
+      if (cur->args)
+          LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
+              fprintf(header, ",%s", arg->name);
       fprintf(header, ") ");
 
-      fprintf(header, "(p)->lpVtbl->");
+      fprintf(header, "(This)->lpVtbl->");
       write_name(header, def);
-      fprintf(header, "(p");
-      for (c=0; c<argc; c++)
-       fprintf(header, ",%c", c+'a');
+      fprintf(header, "(This");
+      if (cur->args)
+          LIST_FOR_EACH_ENTRY( arg, cur->args, const var_t, entry )
+              fprintf(header, ",%s", arg->name);
       fprintf(header, ")\n");
-      if (cur->idx == -1) cur->idx = idx;
-      else if (cur->idx != idx) yyerror("BUG: method index mismatch in write_method_macro");
-      idx++;
     }
-    cur = PREV_LINK(cur);
   }
-  return idx;
 }
 
-void write_args(FILE *h, var_t *arg, char *name, int method, int do_indent)
+void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
 {
+  const var_t *arg;
   int count = 0;
-  if (arg) {
-    while (NEXT_LINK(arg))
-      arg = NEXT_LINK(arg);
-  }
+
   if (do_indent)
   {
-      if (h == header) {
-          indentation++;
-          indent(0);
-      } else fprintf(h, "    ");
+      indentation++;
+      indent(h, 0);
   }
   if (method == 1) {
     fprintf(h, "%s* This", name);
     count++;
   }
-  if (arg == NULL && method == 0) {
-    fprintf(h, "void");
-    return;
-  }
-  while (arg) {
+  if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
     if (count) {
         if (do_indent)
         {
             fprintf(h, ",\n");
-            if (h == header) indent(0);
-            else fprintf(h, "    ");
+            indent(h, 0);
         }
         else fprintf(h, ",");
     }
-    write_type(h, arg->type, arg, arg->tname);
     if (arg->args)
     {
+      write_type_decl_left(h, arg->type);
       fprintf(h, " (STDMETHODCALLTYPE *");
       write_name(h,arg);
       fprintf(h, ")(");
@@ -569,29 +629,25 @@ void write_args(FILE *h, var_t *arg, char *name, int method, int do_indent)
       fprintf(h, ")");
     }
     else
-    {
-      fprintf(h, " ");
-      write_name(h, arg);
-    }
-    write_array(h, arg->array, 0);
-    arg = PREV_LINK(arg);
+      write_type_decl(h, arg->type, "%s", arg->name);
     count++;
   }
-  if (do_indent && h == header) indentation--;
+  if (do_indent) indentation--;
 }
 
-static void write_cpp_method_def(type_t *iface)
+static void write_cpp_method_def(const type_t *iface)
 {
-  func_t *cur = iface->funcs;
+  const func_t *cur;
 
-  if (!cur) return;
-  while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
-  while (cur) {
+  if (!iface->funcs) return;
+
+  LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
+  {
     var_t *def = cur->def;
     if (!is_callas(def->attrs)) {
-      indent(0);
+      indent(header, 0);
       fprintf(header, "virtual ");
-      write_type(header, def->type, def, def->tname);
+      write_type_decl_left(header, def->type);
       fprintf(header, " STDMETHODCALLTYPE ");
       write_name(header, def);
       fprintf(header, "(\n");
@@ -599,25 +655,24 @@ static void write_cpp_method_def(type_t *iface)
       fprintf(header, ") = 0;\n");
       fprintf(header, "\n");
     }
-    cur = PREV_LINK(cur);
   }
 }
 
-static void do_write_c_method_def(type_t *iface, char *name)
+static void do_write_c_method_def(const type_t *iface, const char *name)
 {
-  func_t *cur = iface->funcs;
+  const func_t *cur;
 
   if (iface->ref) do_write_c_method_def(iface->ref, name);
 
-  if (!cur) return;
-  while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
-  indent(0);
+  if (!iface->funcs) return;
+  indent(header, 0);
   fprintf(header, "/*** %s methods ***/\n", iface->name);
-  while (cur) {
-    var_t *def = cur->def;
+  LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
+  {
+    const var_t *def = cur->def;
     if (!is_callas(def->attrs)) {
-      indent(0);
-      write_type(header, def->type, def, def->tname);
+      indent(header, 0);
+      write_type_decl_left(header, def->type);
       fprintf(header, " (STDMETHODCALLTYPE *");
       write_name(header, def);
       fprintf(header, ")(\n");
@@ -625,32 +680,31 @@ static void do_write_c_method_def(type_t *iface, char *name)
       fprintf(header, ");\n");
       fprintf(header, "\n");
     }
-    cur = PREV_LINK(cur);
   }
 }
 
-static void write_c_method_def(type_t *iface)
+static void write_c_method_def(const type_t *iface)
 {
   do_write_c_method_def(iface, iface->name);
 }
 
-static void write_c_disp_method_def(type_t *iface)
+static void write_c_disp_method_def(const type_t *iface)
 {
   do_write_c_method_def(iface->ref, iface->name);
 }
 
-static void write_method_proto(type_t *iface)
+static void write_method_proto(const type_t *iface)
 {
-  func_t *cur = iface->funcs;
+  const func_t *cur;
+
+  if (!iface->funcs) return;
+  LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
+  {
+    const var_t *def = cur->def;
 
-  if (!cur) return;
-  while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
-  while (cur) {
-    var_t *def = cur->def;
-    var_t *cas = is_callas(def->attrs);
     if (!is_local(def->attrs)) {
       /* proxy prototype */
-      write_type(header, def->type, def, def->tname);
+      write_type_decl_left(header, def->type);
       fprintf(header, " CALLBACK %s_", iface->name);
       write_name(header, def);
       fprintf(header, "_Proxy(\n");
@@ -660,49 +714,105 @@ static void write_method_proto(type_t *iface)
       fprintf(header, "void __RPC_STUB %s_", iface->name);
       write_name(header,def);
       fprintf(header, "_Stub(\n");
-      fprintf(header, "    struct IRpcStubBuffer* This,\n");
-      fprintf(header, "    struct IRpcChannelBuffer* pRpcChannelBuffer,\n");
+      fprintf(header, "    IRpcStubBuffer* This,\n");
+      fprintf(header, "    IRpcChannelBuffer* pRpcChannelBuffer,\n");
       fprintf(header, "    PRPC_MESSAGE pRpcMessage,\n");
       fprintf(header, "    DWORD* pdwStubPhase);\n");
     }
+  }
+}
+
+void write_locals(FILE *fp, const type_t *iface, int body)
+{
+  static const char comment[]
+    = "/* WIDL-generated stub.  You must provide an implementation for this.  */";
+  const func_list_t *funcs = iface->funcs;
+  const func_t *cur;
+
+  if (!is_object(iface->attrs) || !funcs)
+    return;
+
+  LIST_FOR_EACH_ENTRY(cur, funcs, const func_t, entry) {
+    const var_t *def = cur->def;
+    const var_t *cas = is_callas(def->attrs);
+
     if (cas) {
-      func_t *m = iface->funcs;
-      while (m && strcmp(get_name(m->def), cas->name))
-        m = NEXT_LINK(m);
-      if (m) {
-        var_t *mdef = m->def;
+      const func_t *m;
+      LIST_FOR_EACH_ENTRY(m, iface->funcs, const func_t, entry)
+        if (!strcmp(m->def->name, cas->name))
+          break;
+      if (&m->entry != iface->funcs) {
+        const var_t *mdef = m->def;
         /* proxy prototype - use local prototype */
-        write_type(header, mdef->type, mdef, mdef->tname);
-        fprintf(header, " CALLBACK %s_", iface->name);
-        write_name(header, mdef);
-        fprintf(header, "_Proxy(\n");
-        write_args(header, m->args, iface->name, 1, TRUE);
-        fprintf(header, ");\n");
+        write_type_decl_left(fp, mdef->type);
+        fprintf(fp, " CALLBACK %s_", iface->name);
+        write_name(fp, mdef);
+        fprintf(fp, "_Proxy(\n");
+        write_args(fp, m->args, iface->name, 1, TRUE);
+        fprintf(fp, ")");
+        if (body) {
+          type_t *rt = mdef->type;
+          fprintf(fp, "\n{\n");
+          fprintf(fp, "    %s\n", comment);
+          if (rt->name && strcmp(rt->name, "HRESULT") == 0)
+            fprintf(fp, "    return E_NOTIMPL;\n");
+          else if (rt->type) {
+            fprintf(fp, "    ");
+            write_type_decl(fp, rt, "rv");
+            fprintf(fp, ";\n");
+            fprintf(fp, "    memset(&rv, 0, sizeof rv);\n");
+            fprintf(fp, "    return rv;\n");
+          }
+          fprintf(fp, "}\n\n");
+        }
+        else
+          fprintf(fp, ";\n");
         /* stub prototype - use remotable prototype */
-        write_type(header, def->type, def, def->tname);
-        fprintf(header, " __RPC_STUB %s_", iface->name);
-        write_name(header, mdef);
-        fprintf(header, "_Stub(\n");
-        write_args(header, cur->args, iface->name, 1, TRUE);
-        fprintf(header, ");\n");
-      }
-      else {
-        yywarning("invalid call_as attribute (%s -> %s)\n", get_name(def), cas->name);
+        write_type_decl_left(fp, def->type);
+        fprintf(fp, " __RPC_STUB %s_", iface->name);
+        write_name(fp, mdef);
+        fprintf(fp, "_Stub(\n");
+        write_args(fp, cur->args, iface->name, 1, TRUE);
+        fprintf(fp, ")");
+        if (body)
+          /* Remotable methods must all return HRESULTs.  */
+          fprintf(fp, "\n{\n    %s\n    return E_NOTIMPL;\n}\n\n", comment);
+        else
+          fprintf(fp, ";\n");
       }
+      else
+        error_loc("invalid call_as attribute (%s -> %s)\n", def->name, cas->name);
     }
-
-    cur = PREV_LINK(cur);
   }
 }
 
-static void write_function_proto(type_t *iface)
+static void write_function_proto(const type_t *iface, const func_t *fun, const char *prefix)
 {
+  var_t *def = fun->def;
+
+  /* FIXME: do we need to handle call_as? */
+  write_type_decl_left(header, def->type);
+  fprintf(header, " ");
+  write_prefix_name(header, prefix, def);
+  fprintf(header, "(\n");
+  if (fun->args)
+    write_args(header, fun->args, iface->name, 0, TRUE);
+  else
+    fprintf(header, "    void");
+  fprintf(header, ");\n");
+}
+
+static void write_function_protos(const type_t *iface)
+{
+  const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
   int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
-  var_t* explicit_handle_var;
+  const var_t* explicit_handle_var;
+  const func_t *cur;
+  int prefixes_differ = strcmp(prefix_client, prefix_server);
 
-  func_t *cur = iface->funcs;
-  while (NEXT_LINK(cur)) cur = NEXT_LINK(cur);
-  while (cur) {
+  if (!iface->funcs) return;
+  LIST_FOR_EACH_ENTRY( cur, iface->funcs, const func_t, entry )
+  {
     var_t *def = cur->def;
 
     /* check for a defined binding handle */
@@ -712,22 +822,19 @@ static void write_function_proto(type_t *iface)
         error("%s() does not define an explicit binding handle!\n", def->name);
         return;
       }
-    } else {
+    } else if (implicit_handle) {
       if (explicit_handle_var) {
         error("%s() must not define a binding handle!\n", def->name);
         return;
       }
     }
 
-    /* FIXME: do we need to handle call_as? */
-    write_type(header, def->type, def, def->tname);
-    fprintf(header, " ");
-    write_name(header, def);
-    fprintf(header, "(\n");
-    write_args(header, cur->args, iface->name, 0, TRUE);
-    fprintf(header, ");\n");
-
-    cur = PREV_LINK(cur);
+    if (prefixes_differ) {
+      fprintf(header, "/* client prototype */\n");
+      write_function_proto(iface, cur, prefix_client);
+      fprintf(header, "/* server prototype */\n");
+    }
+    write_function_proto(iface, cur, prefix_server);
   }
 }
 
@@ -740,44 +847,36 @@ void write_forward(type_t *iface)
    * interface, since non-object interfaces shouldn't need forwards */
   if ((!iface->defined || is_object(iface->attrs) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
         && !iface->written) {
-    fprintf(header,"#ifndef __%s_FWD_DEFINED__\n", iface->name);
-    fprintf(header,"#define __%s_FWD_DEFINED__\n", iface->name);
-    fprintf(header, "typedef struct %s %s;\n", iface->name, iface->name);
+    fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->name);
+    fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->name);
+    fprintf(header, "typedef interface %s %s;\n", iface->name, iface->name);
     fprintf(header, "#endif\n\n" );
     iface->written = TRUE;
   }
 }
 
-void write_guid(const char *guid_prefix, const char *name, UUID *uuid)
+static void write_iface_guid(const type_t *iface)
 {
-  if (!uuid) return;
-  fprintf(header, "DEFINE_GUID(%s_%s, 0x%08lx, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
-          guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
-          uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
-}
-
-void write_iface_guid(type_t *iface)
-{
-  UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
-  write_guid("IID", iface->name, uuid);
+  const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
+  write_guid(header, "IID", iface->name, uuid);
 } 
 
-void write_dispiface_guid(type_t *iface)
+static void write_dispiface_guid(const type_t *iface)
 {
-  UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
-  write_guid("DIID", iface->name, uuid);
+  const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
+  write_guid(header, "DIID", iface->name, uuid);
 }
 
-void write_coclass_guid(class_t *cocl)
+static void write_coclass_guid(type_t *cocl)
 {
-  UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
-  write_guid("CLSID", cocl->name, uuid);
+  const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
+  write_guid(header, "CLSID", cocl->name, uuid);
 }
 
-void write_com_interface(type_t *iface)
+static void write_com_interface(type_t *iface)
 {
   if (!iface->funcs && !iface->ref) {
-    yywarning("%s has no methods", iface->name);
+    parser_warning("%s has no methods\n", iface->name);
     return;
   }
 
@@ -792,7 +891,7 @@ void write_com_interface(type_t *iface)
   fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
   if (iface->ref)
   {
-      fprintf(header, "struct %s : public %s\n", iface->name, iface->ref->name);
+      fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
       fprintf(header, "{\n");
       indentation++;
       write_cpp_method_def(iface);
@@ -801,7 +900,7 @@ void write_com_interface(type_t *iface)
   }
   else
   {
-      fprintf(header, "struct %s\n", iface->name);
+      fprintf(header, "interface %s\n", iface->name);
       fprintf(header, "{\n");
       fprintf(header, "    BEGIN_INTERFACE\n");
       fprintf(header, "\n");
@@ -813,17 +912,16 @@ void write_com_interface(type_t *iface)
   }
   fprintf(header, "#else\n");
   /* C interface */
-  fprintf(header, "typedef struct %sVtbl %sVtbl;\n", iface->name, iface->name);
-  fprintf(header, "struct %s {\n", iface->name);
-  fprintf(header, "    const %sVtbl* lpVtbl;\n", iface->name);
-  fprintf(header, "};\n");
-  fprintf(header, "struct %sVtbl {\n", iface->name);
+  fprintf(header, "typedef struct %sVtbl {\n", iface->name);
   indentation++;
   fprintf(header, "    BEGIN_INTERFACE\n");
   fprintf(header, "\n");
   write_c_method_def(iface);
   indentation--;
   fprintf(header, "    END_INTERFACE\n");
+  fprintf(header, "} %sVtbl;\n", iface->name);
+  fprintf(header, "interface %s {\n", iface->name);
+  fprintf(header, "    CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
   fprintf(header, "};\n");
   fprintf(header, "\n");
   fprintf(header, "#ifdef COBJMACROS\n");
@@ -833,36 +931,47 @@ void write_com_interface(type_t *iface)
   fprintf(header, "#endif\n");
   fprintf(header, "\n");
   write_method_proto(iface);
+  write_locals(header, iface, FALSE);
   fprintf(header,"\n#endif  /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
 }
 
-void write_rpc_interface(type_t *iface)
+static void write_rpc_interface(const type_t *iface)
 {
-  unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
-  char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
+  unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
+  const char *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
+  static int allocate_written = 0;
 
-  if (!iface->funcs) return;
+  if (!allocate_written)
+  {
+    allocate_written = 1;
+    fprintf(header, "void * __RPC_USER MIDL_user_allocate(size_t);\n");
+    fprintf(header, "void __RPC_USER MIDL_user_free(void *);\n\n");
+  }
 
   fprintf(header, "/*****************************************************************************\n");
-  fprintf(header, " * %s interface (v%d.%d)\n", iface->name, LOWORD(ver), HIWORD(ver));
+  fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
   fprintf(header, " */\n");
-  write_iface_guid(iface);
-  if (var)
-  {
-    fprintf(header, "extern handle_t %s;\n", var);
-  }
-  if (old_names)
-  {
-    fprintf(header, "extern RPC_IF_HANDLE %s_ClientIfHandle;\n", iface->name);
-    fprintf(header, "extern RPC_IF_HANDLE %s_ServerIfHandle;\n", iface->name);
-  }
-  else
+  fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
+  fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
+  if (iface->funcs)
   {
-    fprintf(header, "extern RPC_IF_HANDLE %s_v%d_%d_c_ifspec;\n", iface->name, LOWORD(ver), HIWORD(ver));
-    fprintf(header, "extern RPC_IF_HANDLE %s_v%d_%d_s_ifspec;\n", iface->name, LOWORD(ver), HIWORD(ver));
+    write_iface_guid(iface);
+    if (var) fprintf(header, "extern handle_t %s;\n", var);
+    if (old_names)
+    {
+        fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
+        fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
+    }
+    else
+    {
+        fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
+                prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
+        fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
+                prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
+    }
+    write_function_protos(iface);
   }
-  write_function_proto(iface);
-  fprintf(header, "\n");
+  fprintf(header,"\n#endif  /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
 
   /* FIXME: server/client code */
 }
@@ -886,22 +995,21 @@ void write_dispinterface(type_t *iface)
   write_forward(iface);
   /* C++ interface */
   fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
-  fprintf(header, "struct %s : public %s\n", iface->name, iface->ref->name);
+  fprintf(header, "interface %s : public %s\n", iface->name, iface->ref->name);
   fprintf(header, "{\n");
   fprintf(header, "};\n");
   fprintf(header, "#else\n");
   /* C interface */
-  fprintf(header, "typedef struct %sVtbl %sVtbl;\n", iface->name, iface->name);
-  fprintf(header, "struct %s {\n", iface->name);
-  fprintf(header, "    const %sVtbl* lpVtbl;\n", iface->name);
-  fprintf(header, "};\n");
-  fprintf(header, "struct %sVtbl {\n", iface->name);
+  fprintf(header, "typedef struct %sVtbl {\n", iface->name);
   indentation++;
   fprintf(header, "    BEGIN_INTERFACE\n");
   fprintf(header, "\n");
   write_c_disp_method_def(iface);
   indentation--;
   fprintf(header, "    END_INTERFACE\n");
+  fprintf(header, "} %sVtbl;\n", iface->name);
+  fprintf(header, "interface %s {\n", iface->name);
+  fprintf(header, "    CONST_VTBL %sVtbl* lpVtbl;\n", iface->name);
   fprintf(header, "};\n");
   fprintf(header, "\n");
   fprintf(header, "#ifdef COBJMACROS\n");
@@ -913,7 +1021,7 @@ void write_dispinterface(type_t *iface)
   fprintf(header,"#endif  /* __%s_DISPINTERFACE_DEFINED__ */\n\n", iface->name);
 }
 
-void write_coclass(class_t *cocl)
+void write_coclass(type_t *cocl)
 {
   fprintf(header, "/*****************************************************************************\n");
   fprintf(header, " * %s coclass\n", cocl->name);
@@ -921,3 +1029,11 @@ void write_coclass(class_t *cocl)
   write_coclass_guid(cocl);
   fprintf(header, "\n");
 }
+
+void write_coclass_forward(type_t *cocl)
+{
+  fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
+  fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
+  fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
+  fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
+}