Index: backend/cdef.h
===================================================================
--- backend/cdef.h	(revision 556)
+++ backend/cdef.h	(working copy)
@@ -293,6 +293,8 @@
 
 #define NEWSTATICDTOR           1       // support new style static destructors
 
+class Dsymbol;
+
 // For Shared Code Base
 #define TARGET_structBLOCK
 #define TARGET_structFUNC_S
@@ -300,7 +302,7 @@
 #define TARGET_structPARAM
 #define TARGET_structBLKLST
 #define TARGET_structELEM
-#define TARGET_structSYMBOL
+#define TARGET_structSYMBOL     Dsymbol* dsymbol;
 #define TARGET_INLINEFUNC_NAMES
 #define PASCAL pascal
 #define HINT int
@@ -994,7 +996,7 @@
          (symbol *)0,(symbol *)0,(symbol *)0,(dt_t *)0,(type *)0,{0},\
          SYMBOLZERO\
          UNIXFIELDS\
-         SCextern,(fl),(flags),0,0,0,0,0,0,0,{0},(regsaved),{name}}
+         SCextern,(fl),(flags),0,0,0,0,0,0,0,{0},(regsaved),0,{name}}
 
 /**********************************
  * Storage classes
Index: backend/cgcv.c
===================================================================
--- backend/cgcv.c	(revision 556)
+++ backend/cgcv.c	(working copy)
@@ -95,6 +95,9 @@
 STATIC void cv4_outsym(symbol *s);
 STATIC void cv4_func(Funcsym *s);
 
+// from tocvdebug.c
+unsigned cv_typeidx_from_symbol(Symbol* s);
+
 /******************************************
  * Return number of bytes consumed in OBJ file by a name.
  */
@@ -577,7 +580,7 @@
 
             p = t->Tparamtypes;
             for (u = 0; u < nparam; u++)
-            {   TOWORD(d->data + 4 + u * 2,cv4_typidx(p->Ptype));
+            {   TOWORD(d->data + 4 + u * 2,cv4_typidx(p->Psym, p->Ptype));
                 p = p->Pnext;
             }
         }
@@ -588,7 +591,7 @@
 
             p = t->Tparamtypes;
             for (u = 0; u < nparam; u++)
-            {   TOLONG(d->data + 6 + u * 4,cv4_typidx(p->Ptype));
+            {   TOLONG(d->data + 6 + u * 4,cv4_typidx(p->Psym, p->Ptype));
                 p = p->Pnext;
             }
         }
@@ -1571,7 +1574,7 @@
 
 STATIC unsigned cv4_symtypidx(symbol *s)
 {
-    return cv4_typidx(s->Stype);
+    return cv4_typidx(s, s->Stype);
 }
 
 #endif
@@ -1652,7 +1655,7 @@
  * Return CV4 type index for a type.
  */
 
-unsigned cv4_typidx(type *t)
+unsigned cv4_typidx(Symbol* s, type *t)
 {   unsigned typidx;
     unsigned u;
     unsigned next;
@@ -1671,7 +1674,7 @@
     if (!t)
         return dttab4[TYint];           // assume int
     type_debug(t);
-    next = cv4_typidx(t->Tnext);
+    next = cv4_typidx(0, t->Tnext);
     tycv = t->Tty;
     tym = tybasic(tycv);
     tycv &= mTYconst | mTYvolatile | mTYimmutable;
@@ -1719,7 +1722,8 @@
         case TYwchar_t:
         case TYdchar:
             assert(dt);
-            typidx = dt;
+            if ((typidx = cv_typeidx_from_symbol(s)) == 0)
+                typidx = dt;
             break;
 
 #if JHANDLE
@@ -1749,7 +1753,7 @@
 #if 1
             // This is a hack to duplicate bugs in VC, so that the VC
             // debugger will work.
-            tymnext = t->Tnext->Tty;
+            tymnext = t->Tnext ? t->Tnext->Tty : 0;
             if (tymnext & (mTYconst | mTYimmutable | mTYvolatile) &&
                 !tycv &&
                 tyarithmetic(tymnext) &&
@@ -1807,7 +1811,7 @@
         Laarray:
             assert(config.fulltypes == CV4);
 #if MARS
-            key = cv4_typidx(t->Tkey);
+            key = cv4_typidx(0, t->Tkey);
 #if 1
             d = debtyp_alloc(12);
             TOWORD(d->data, LF_OEM);
@@ -1830,7 +1834,7 @@
             assert(config.fulltypes == CV4);
             tv = type_fake(TYnptr);
             tv->Tcount++;
-            key = cv4_typidx(tv);
+            key = cv4_typidx(0, tv);
             type_free(tv);
 #if 1
             d = debtyp_alloc(12);
@@ -1970,7 +1974,7 @@
 #endif
         case TYnullptr:
             tym = TYnptr;
-            next = cv4_typidx(tsvoid);  // rewrite as void*
+            next = cv4_typidx(0, tsvoid);  // rewrite as void*
             t = tspvoid;
             goto L1;
 
@@ -2147,7 +2151,7 @@
         unsigned fixoff;
         idx_t typidx;
 
-        typidx = cv4_typidx(t);
+        typidx = cv4_typidx(s, t);
         id = prettyident(s);
         len = strlen(id);
         debsym = (unsigned char __ss *) alloca(39 + IDOHD + len);
@@ -2681,7 +2685,7 @@
  * Return cv type index for a type.
  */
 
-unsigned cv_typidx(type *t)
+unsigned cv_typidx(Symbol*s, type *t)
 {   unsigned ti;
 
     //dbg_printf("cv_typidx(%p)\n",t);
@@ -2690,7 +2694,7 @@
         case CV4:
         case CVTDB:
         case CVSYM:
-            ti = cv4_typidx(t);
+            ti = cv4_typidx(s, t);
             break;
         default:
 #ifdef DEBUG
Index: backend/cgcv.h
===================================================================
--- backend/cgcv.h	(revision 556)
+++ backend/cgcv.h	(working copy)
@@ -19,7 +19,7 @@
 extern char *ftdbname;
 
 void cv_init ( void );
-unsigned cv_typidx ( type *t );
+unsigned cv_typidx ( Symbol *s, type *t );
 void cv_outsym ( Symbol *s );
 void cv_func ( Symbol *s );
 void cv_term ( void );
@@ -62,7 +62,7 @@
 void cv4_storenumeric(unsigned char *p,targ_size_t value);
 idx_t cv_debtyp ( debtyp_t *d );
 int cv_namestring ( unsigned char *p , const char *name );
-unsigned cv4_typidx(type *t);
+unsigned cv4_typidx(Symbol*s, type *t);
 idx_t cv4_arglist(type *t,unsigned *pnparam);
 unsigned char cv4_callconv(type *t);
 
Index: backend/cgobj.c
===================================================================
--- backend/cgobj.c	(revision 556)
+++ backend/cgobj.c	(working copy)
@@ -1794,7 +1794,7 @@
     outextdata();
     p = cextdef;
     p += insidx(p,obj.lnameidx++);
-    ti = (config.fulltypes == CVOLD) ? cv_typidx(s->Stype) : 0;
+    ti = (config.fulltypes == CVOLD) ? cv_typidx(s, s->Stype) : 0;
     p += instypidx(p,ti);
     objrecord(CEXTDEF,cextdef,p - cextdef);
     s->Sxtrnnum = ++obj.extidx;
@@ -2334,7 +2334,7 @@
     p += len;
     TOOFFSET(p,offset);
     p += intsize;
-    ti = (config.fulltypes == CVOLD) ? cv_typidx(s->Stype) : 0;
+    ti = (config.fulltypes == CVOLD) ? cv_typidx(s, s->Stype) : 0;
     reclen += instypidx(p,ti);
     obj.pubdatai += reclen;
 }
@@ -2452,7 +2452,7 @@
     outextdata();               // borrow the extdata[] storage
     i = obj_mangle(s,obj.extdata);
 
-    ti = (config.fulltypes == CVOLD) ? cv_typidx(s->Stype) : 0;
+    ti = (config.fulltypes == CVOLD) ? cv_typidx(s, s->Stype) : 0;
     i += instypidx(obj.extdata + i,ti);
 
   if (flag)                             // if in default data segment
Index: mtype.c
===================================================================
--- mtype.c	(revision 556)
+++ mtype.c	(working copy)
@@ -6087,6 +6087,7 @@
         : Type(Tenum)
 {
     this->sym = sym;
+    dbg_typidx = 0;
 }
 
 char *TypeEnum::toChars()
Index: mtype.h
===================================================================
--- mtype.h	(revision 556)
+++ mtype.h	(working copy)
@@ -712,6 +712,7 @@
 struct TypeEnum : Type
 {
     EnumDeclaration *sym;
+    unsigned dbg_typidx; // CV debug info type index
 
     TypeEnum(EnumDeclaration *sym);
     Type *syntaxCopy();
Index: tocsym.c
===================================================================
--- tocsym.c	(revision 556)
+++ tocsym.c	(working copy)
@@ -266,6 +266,7 @@
         }
         type_setmangle(&t, m);
         s->Stype = t;
+        s->dsymbol = this;
 
         csym = s;
     }
@@ -413,6 +414,7 @@
         t->Tcount++;
         s->Stype = t;
         //s->Sfielddef = this;
+        s->dsymbol = this;
 
         csym = s;
     }
@@ -515,6 +517,7 @@
         s = toSymbolX("__Class", SCextern, scc->Stype, "Z");
         s->Sfl = FLextern;
         s->Sflags |= SFLnodebug;
+        s->dsymbol = this;
         csym = s;
         slist_add(s);
     }
@@ -537,6 +540,7 @@
         s = toSymbolX("__Interface", SCextern, scc->Stype, "Z");
         s->Sfl = FLextern;
         s->Sflags |= SFLnodebug;
+        s->dsymbol = this;
         csym = s;
         slist_add(s);
     }
@@ -560,6 +564,7 @@
         s = toSymbolX("__ModuleInfo", SCextern, scc->Stype, "Z");
         s->Sfl = FLextern;
         s->Sflags |= SFLnodebug;
+        s->dsymbol = this;
         csym = s;
         slist_add(s);
     }
Index: tocvdebug.c
===================================================================
--- tocvdebug.c	(revision 556)
+++ tocvdebug.c	(working copy)
@@ -91,7 +91,7 @@
         else
         {
             assert(ad->handle);
-            thisidx = cv4_typidx(ad->handle->toCtype());
+            thisidx = cv4_typidx(0, ad->handle->toCtype());
         }
 
         paramidx = cv4_arglist(t,&nparam);
@@ -100,8 +100,8 @@
         d = debtyp_alloc(18);
         p = d->data;
         TOWORD(p,LF_MFUNCTION);
-        TOWORD(p + 2,cv4_typidx(t->Tnext));
-        TOWORD(p + 4,cv4_typidx(ad->type->toCtype()));
+        TOWORD(p + 2,cv4_typidx(0, t->Tnext));
+        TOWORD(p + 4,cv4_typidx(0, ad->type->toCtype()));
         TOWORD(p + 6,thisidx);
         p[8] = call;
         p[9] = 0;                               // reserved
@@ -111,7 +111,7 @@
 
         return cv_debtyp(d);
     }
-    return cv4_typidx(t);
+    return cv4_typidx(0, t);
 }
 
 unsigned cv4_Denum(EnumDeclaration *e)
@@ -134,7 +134,7 @@
     len = 10;
     d = debtyp_alloc(len + cv_stringbytes(id));
     TOWORD(d->data,LF_ENUM);
-    TOWORD(d->data + 4,e->memtype ? cv4_typidx(e->memtype->toCtype()) : 0);
+    TOWORD(d->data + 4,e->memtype ? cv4_typidx(0, e->memtype->toCtype()) : 0);
     TOWORD(d->data + 8,property);
     len += cv_namestring(d->data + len,id);
 
@@ -237,7 +237,7 @@
 
         unsigned length;
         const char *id = toPrettyChars();
-        idx_t typidx = cv4_typidx(basetype->toCtype());
+        idx_t typidx = cv4_typidx(0, basetype->toCtype());
         unsigned len = strlen(id);
         unsigned char *debsym = (unsigned char *) alloca(39 + IDOHD + len);
 
@@ -278,6 +278,9 @@
 
         assert(length <= 40 + len);
         obj_write_bytes(SegData[DEBSYM],length,debsym);
+
+        assert(type->ty == Tenum);
+        ((TypeEnum*)type)->dbg_typidx = typidx;
     }
 }
 
@@ -563,7 +566,7 @@
         idx_t typidx;
         unsigned attribute;
 
-        typidx = cv4_typidx(bc->base->type->toCtype()->Tnext);
+        typidx = cv4_typidx(0, bc->base->type->toCtype()->Tnext);
 
         attribute = PROTtoATTR(bc->protection);
 
@@ -609,6 +612,19 @@
 //    return typidx;
 }
 
+unsigned cv_typeidx_from_symbol(Symbol* s)
+{
+    if(!s || !s->dsymbol)
+        return 0;
+    Declaration* decl = s->dsymbol->isDeclaration();
+    if(!decl)
+        return 0;
+    
+    if(decl->type->ty == Tenum)
+        return ((TypeEnum*)decl->type)->dbg_typidx;
+    
+    return 0;
+}
 
 /* ===================================================================== */
 
@@ -642,7 +658,7 @@
     else
     {
         TOWORD(p,LF_NESTTYPE);
-        typidx = cv4_typidx(basetype->toCtype());
+        typidx = cv4_typidx(0, basetype->toCtype());
         TOWORD(p + 2,typidx);
         nwritten = 4 + cv_namestring(p + 4, id);
     }
@@ -671,6 +687,9 @@
         typidx = cv4_Denum(this);
         TOWORD(p + 2,typidx);
         nwritten = 4 + cv_namestring(p + 4, id);
+
+        assert(type->ty == Tenum);
+        ((TypeEnum*)type)->dbg_typidx = typidx;
     }
     return nwritten;
 }
@@ -805,7 +824,10 @@
     else if (storage_class & STCfield)
     {
         TOWORD(p,LF_MEMBER);
-        typidx = cv_typidx(type->toCtype());
+        if(type->ty == Tenum)
+            typidx = ((TypeEnum*)type)->dbg_typidx;
+        else
+            typidx = cv_typidx(csym,type->toCtype());
         attribute = PROTtoATTR(prot());
         assert((attribute & ~3) == 0);
         TOWORD(p + 2,typidx);
@@ -817,7 +839,7 @@
     else if (isStatic())
     {
         TOWORD(p,LF_STMEMBER);
-        typidx = cv_typidx(type->toCtype());
+        typidx = cv_typidx(csym,type->toCtype());
         attribute = PROTtoATTR(prot());
         assert((attribute & ~3) == 0);
         TOWORD(p + 2,typidx);
