changeset 159:5acec6b2eef8 trunk

[svn r175] merged dmd 1.029
author ChristianK
date Thu, 01 May 2008 15:15:28 +0200
parents 287540c5f05e
children b77664331d06
files dmd/aggregate.h dmd/attrib.c dmd/cast.c dmd/class.c dmd/clone.c dmd/declaration.c dmd/declaration.h dmd/doc.c dmd/dsymbol.c dmd/dsymbol.h dmd/expression.c dmd/expression.h dmd/func.c dmd/interpret.c dmd/lexer.c dmd/lexer.h dmd/link.c dmd/mars.c dmd/mars.h dmd/mtype.c dmd/mtype.h dmd/parse.c dmd/scope.c dmd/scope.h dmd/statement.c dmd/template.c dmd/template.h
diffstat 27 files changed, 38676 insertions(+), 38030 deletions(-) [+]
line wrap: on
line diff
--- a/dmd/aggregate.h	Thu May 01 13:33:02 2008 +0200
+++ b/dmd/aggregate.h	Thu May 01 15:15:28 2008 +0200
@@ -1,259 +1,282 @@
-
-// Compiler implementation of the D programming language
-// Copyright (c) 1999-2006 by Digital Mars
-// All Rights Reserved
-// written by Walter Bright
-// http://www.digitalmars.com
-// License for redistribution is by either the Artistic License
-// in artistic.txt, or the GNU General Public License in gnu.txt.
-// See the included readme.txt for details.
-
-#ifndef DMD_AGGREGATE_H
-#define DMD_AGGREGATE_H
-
-#ifdef __DMC__
-#pragma once
-#endif /* __DMC__ */
-
-#include "root.h"
-#include "dsymbol.h"
-
-#include <vector>
-#include <set>
-#include <map>
-
-struct Identifier;
-struct Type;
-struct TypeFunction;
-struct Expression;
-struct FuncDeclaration;
-struct CtorDeclaration;
-struct DtorDeclaration;
-struct InvariantDeclaration;
-struct NewDeclaration;
-struct DeleteDeclaration;
-struct InterfaceDeclaration;
-struct ClassInfoDeclaration;
-struct VarDeclaration;
-struct dt_t;
-
-namespace llvm
-{
-    class Type;
-    class Value;
-    class Constant;
-    class ConstantStruct;
-    class GlobalVariable;
-}
-struct DUnion;
-
-struct AggregateDeclaration : ScopeDsymbol
-{
-    Type *type;
-    unsigned storage_class;
-    enum PROT protection;
-    Type *handle;		// 'this' type
-    unsigned structsize;	// size of struct
-    unsigned alignsize;		// size of struct for alignment purposes
-    unsigned structalign;	// struct member alignment in effect
-    int hasUnions;		// set if aggregate has overlapping fields
-    Array fields;		// VarDeclaration fields
-    unsigned sizeok;		// set when structsize contains valid data
-				// 0: no size
-				// 1: size is correct
-				// 2: cannot determine size; fwd referenced
-    int isdeprecated;		// !=0 if deprecated
-    Scope *scope;		// !=NULL means context to use
-
-    // Special member functions
-    InvariantDeclaration *inv;		// invariant
-    NewDeclaration *aggNew;		// allocator
-    DeleteDeclaration *aggDelete;	// deallocator
-
-#ifdef IN_GCC
-    Array methods;              // flat list of all methods for debug information
-#endif
-
-    AggregateDeclaration(Loc loc, Identifier *id);
-    void semantic2(Scope *sc);
-    void semantic3(Scope *sc);
-    void inlineScan();
-    unsigned size(Loc loc);
-    static void alignmember(unsigned salign, unsigned size, unsigned *poffset);
-    Type *getType();
-    void addField(Scope *sc, VarDeclaration *v);
-    int isDeprecated();		// is aggregate deprecated?
-
-    void emitComment(Scope *sc);
-    void toDocBuffer(OutBuffer *buf);
-
-    // For access checking
-    virtual PROT getAccess(Dsymbol *smember);	// determine access to smember
-    int isFriendOf(AggregateDeclaration *cd);
-    int hasPrivateAccess(Dsymbol *smember);	// does smember have private access to members of this class?
-    void accessCheck(Loc loc, Scope *sc, Dsymbol *smember);
-
-    enum PROT prot();
-
-    // Back end
-    Symbol *stag;		// tag symbol for debug data
-    Symbol *sinit;
-    Symbol *toInitializer();
-
-    AggregateDeclaration *isAggregateDeclaration() { return this; }
-};
-
-struct AnonymousAggregateDeclaration : AggregateDeclaration
-{
-    AnonymousAggregateDeclaration()
-	: AggregateDeclaration(0, NULL)
-    {
-    }
-
-    AnonymousAggregateDeclaration *isAnonymousAggregateDeclaration() { return this; }
-};
-
-struct StructDeclaration : AggregateDeclaration
-{
-    int zeroInit;		// !=0 if initialize with 0 fill
-
-    StructDeclaration(Loc loc, Identifier *id);
-    Dsymbol *syntaxCopy(Dsymbol *s);
-    void semantic(Scope *sc);
-    void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
-    char *mangle();
-    char *kind();
-    void toDocBuffer(OutBuffer *buf);
-
-    PROT getAccess(Dsymbol *smember);	// determine access to smember
-
-    void toObjFile();			// compile to .obj file
-    void toDt(dt_t **pdt);
-    void toDebug();			// to symbolic debug info
-
-    StructDeclaration *isStructDeclaration() { return this; }
-};
-
-struct UnionDeclaration : StructDeclaration
-{
-    UnionDeclaration(Loc loc, Identifier *id);
-    Dsymbol *syntaxCopy(Dsymbol *s);
-    char *kind();
-
-    UnionDeclaration *isUnionDeclaration() { return this; }
-};
-
-struct BaseClass
-{
-    Type *type;				// (before semantic processing)
-    enum PROT protection;		// protection for the base interface
-
-    ClassDeclaration *base;
-    int offset;				// 'this' pointer offset
-    Array vtbl;				// for interfaces: Array of FuncDeclaration's
-					// making up the vtbl[]
-
-    int baseInterfaces_dim;
-    BaseClass *baseInterfaces;		// if BaseClass is an interface, these
-					// are a copy of the InterfaceDeclaration::interfaces
-
-    BaseClass();
-    BaseClass(Type *type, enum PROT protection);
-
-    int fillVtbl(ClassDeclaration *cd, Array *vtbl, int newinstance);
-    void copyBaseInterfaces(BaseClasses *);
-};
-
-#define CLASSINFO_SIZE 	(0x3C+12)	// value of ClassInfo.size
-
-struct ClassDeclaration : AggregateDeclaration
-{
-    static ClassDeclaration *object;
-    static ClassDeclaration *classinfo;
-
-    ClassDeclaration *baseClass;	// NULL only if this is Object
-    CtorDeclaration *ctor;
-    CtorDeclaration *defaultCtor;	// default constructor
-    FuncDeclarations dtors;		// Array of destructors
-    FuncDeclaration *staticCtor;
-    FuncDeclaration *staticDtor;
-    Array vtbl;				// Array of FuncDeclaration's making up the vtbl[]
-    Array vtblFinal;			// More FuncDeclaration's that aren't in vtbl[]
-
-    BaseClasses baseclasses;		// Array of BaseClass's; first is super,
-					// rest are Interface's
-
-    int interfaces_dim;
-    BaseClass **interfaces;		// interfaces[interfaces_dim] for this class
-					// (does not include baseClass)
-
-    BaseClasses *vtblInterfaces;	// array of base interfaces that have
-					// their own vtbl[]
-
-    ClassInfoDeclaration *vclassinfo;	// the ClassInfo object for this ClassDeclaration
-    int com;				// !=0 if this is a COM class
-    int isauto;				// !=0 if this is an auto class
-    int isabstract;			// !=0 if abstract class
-
-    int isnested;			// !=0 if is nested
-    VarDeclaration *vthis;		// 'this' parameter if this class is nested
-
-    ClassDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses);
-    Dsymbol *syntaxCopy(Dsymbol *s);
-    void semantic(Scope *sc);
-    void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
-    int isBaseOf2(ClassDeclaration *cd);
-
-    #define OFFSET_RUNTIME 0x76543210
-    virtual int isBaseOf(ClassDeclaration *cd, int *poffset);
-
-    Dsymbol *search(Loc, Identifier *ident, int flags);
-    FuncDeclaration *findFunc(Identifier *ident, TypeFunction *tf);
-    void interfaceSemantic(Scope *sc);
-    int isNested();
-    int isCOMclass();
-    virtual int isCOMinterface();
-    int isAbstract();
-    virtual int vtblOffset();
-    char *kind();
-    char *mangle();
-    void toDocBuffer(OutBuffer *buf);
-
-    PROT getAccess(Dsymbol *smember);	// determine access to smember
-
-    void addLocalClass(ClassDeclarations *);
-
-    // Back end
-    void toObjFile();			// compile to .obj file
-    void toDebug();
-    unsigned baseVtblOffset(BaseClass *bc);
-    Symbol *toSymbol();
-    Symbol *toVtblSymbol();
-    void toDt(dt_t **pdt);
-    void toDt2(dt_t **pdt, ClassDeclaration *cd);
-
-    Symbol *vtblsym;
-
-    void offsetToIndex(Type* t, unsigned os, std::vector<unsigned>& result);
-
-    ClassDeclaration *isClassDeclaration() { return (ClassDeclaration *)this; }
-};
-
-struct InterfaceDeclaration : ClassDeclaration
-{
-    InterfaceDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses);
-    Dsymbol *syntaxCopy(Dsymbol *s);
-    void semantic(Scope *sc);
-    int isBaseOf(ClassDeclaration *cd, int *poffset);
-    int isBaseOf(BaseClass *bc, int *poffset);
-    char *kind();
-    int vtblOffset();
-    virtual int isCOMinterface();
-
-    void toObjFile();			// compile to .obj file
-    Symbol *toSymbol();
-
-    InterfaceDeclaration *isInterfaceDeclaration() { return this; }
-};
-
-#endif /* DMD_AGGREGATE_H */
+
+// Compiler implementation of the D programming language
+// Copyright (c) 1999-2008 by Digital Mars
+// All Rights Reserved
+// written by Walter Bright
+// http://www.digitalmars.com
+// License for redistribution is by either the Artistic License
+// in artistic.txt, or the GNU General Public License in gnu.txt.
+// See the included readme.txt for details.
+
+#ifndef DMD_AGGREGATE_H
+#define DMD_AGGREGATE_H
+
+#ifdef __DMC__
+#pragma once
+#endif /* __DMC__ */
+
+#include "root.h"
+#include "dsymbol.h"
+
+#include <vector>
+#include <set>
+#include <map>
+
+struct Identifier;
+struct Type;
+struct TypeFunction;
+struct Expression;
+struct FuncDeclaration;
+struct CtorDeclaration;
+struct DtorDeclaration;
+struct InvariantDeclaration;
+struct NewDeclaration;
+struct DeleteDeclaration;
+struct InterfaceDeclaration;
+struct ClassInfoDeclaration;
+struct VarDeclaration;
+struct dt_t;
+
+namespace llvm
+{
+    class Type;
+    class Value;
+    class Constant;
+    class ConstantStruct;
+    class GlobalVariable;
+}
+struct DUnion;
+
+struct AggregateDeclaration : ScopeDsymbol
+{
+    Type *type;
+    unsigned storage_class;
+    enum PROT protection;
+    Type *handle;		// 'this' type
+    unsigned structsize;	// size of struct
+    unsigned alignsize;		// size of struct for alignment purposes
+    unsigned structalign;	// struct member alignment in effect
+    int hasUnions;		// set if aggregate has overlapping fields
+    Array fields;		// VarDeclaration fields
+    unsigned sizeok;		// set when structsize contains valid data
+				// 0: no size
+				// 1: size is correct
+				// 2: cannot determine size; fwd referenced
+    int isdeprecated;		// !=0 if deprecated
+    Scope *scope;		// !=NULL means context to use
+    FuncDeclarations dtors;	// Array of destructors
+    FuncDeclaration *dtor;	// aggregate destructor
+
+    // Special member functions
+    InvariantDeclaration *inv;		// invariant
+    NewDeclaration *aggNew;		// allocator
+    DeleteDeclaration *aggDelete;	// deallocator
+
+#ifdef IN_GCC
+    Array methods;              // flat list of all methods for debug information
+#endif
+
+    AggregateDeclaration(Loc loc, Identifier *id);
+    void semantic2(Scope *sc);
+    void semantic3(Scope *sc);
+    void inlineScan();
+    unsigned size(Loc loc);
+    static void alignmember(unsigned salign, unsigned size, unsigned *poffset);
+    Type *getType();
+    void addField(Scope *sc, VarDeclaration *v);
+    int isDeprecated();		// is aggregate deprecated?
+    FuncDeclaration *buildDtor(Scope *sc);
+
+    void emitComment(Scope *sc);
+    void toDocBuffer(OutBuffer *buf);
+
+    // For access checking
+    virtual PROT getAccess(Dsymbol *smember);	// determine access to smember
+    int isFriendOf(AggregateDeclaration *cd);
+    int hasPrivateAccess(Dsymbol *smember);	// does smember have private access to members of this class?
+    void accessCheck(Loc loc, Scope *sc, Dsymbol *smember);
+
+    enum PROT prot();
+
+    // Back end
+    Symbol *stag;		// tag symbol for debug data
+    Symbol *sinit;
+    Symbol *toInitializer();
+
+    AggregateDeclaration *isAggregateDeclaration() { return this; }
+};
+
+struct AnonymousAggregateDeclaration : AggregateDeclaration
+{
+    AnonymousAggregateDeclaration()
+	: AggregateDeclaration(0, NULL)
+    {
+    }
+
+    AnonymousAggregateDeclaration *isAnonymousAggregateDeclaration() { return this; }
+};
+
+struct StructDeclaration : AggregateDeclaration
+{
+    int zeroInit;		// !=0 if initialize with 0 fill
+
+    StructDeclaration(Loc loc, Identifier *id);
+    Dsymbol *syntaxCopy(Dsymbol *s);
+    void semantic(Scope *sc);
+    void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
+    char *mangle();
+    char *kind();
+    Expression *cloneMembers();
+    void toDocBuffer(OutBuffer *buf);
+
+    PROT getAccess(Dsymbol *smember);	// determine access to smember
+
+    void toObjFile();			// compile to .obj file
+    void toDt(dt_t **pdt);
+    void toDebug();			// to symbolic debug info
+
+    StructDeclaration *isStructDeclaration() { return this; }
+};
+
+struct UnionDeclaration : StructDeclaration
+{
+    UnionDeclaration(Loc loc, Identifier *id);
+    Dsymbol *syntaxCopy(Dsymbol *s);
+    char *kind();
+
+    UnionDeclaration *isUnionDeclaration() { return this; }
+};
+
+struct BaseClass
+{
+    Type *type;				// (before semantic processing)
+    enum PROT protection;		// protection for the base interface
+
+    ClassDeclaration *base;
+    int offset;				// 'this' pointer offset
+    Array vtbl;				// for interfaces: Array of FuncDeclaration's
+					// making up the vtbl[]
+
+    int baseInterfaces_dim;
+    BaseClass *baseInterfaces;		// if BaseClass is an interface, these
+					// are a copy of the InterfaceDeclaration::interfaces
+
+    BaseClass();
+    BaseClass(Type *type, enum PROT protection);
+
+    int fillVtbl(ClassDeclaration *cd, Array *vtbl, int newinstance);
+    void copyBaseInterfaces(BaseClasses *);
+};
+
+#if V2
+#define CLASSINFO_SIZE 	(0x3C+16)	// value of ClassInfo.size
+#else
+#define CLASSINFO_SIZE 	(0x3C+12)	// value of ClassInfo.size
+#endif
+
+struct ClassDeclaration : AggregateDeclaration
+{
+    static ClassDeclaration *object;
+    static ClassDeclaration *classinfo;
+
+    ClassDeclaration *baseClass;	// NULL only if this is Object
+    CtorDeclaration *ctor;
+    CtorDeclaration *defaultCtor;	// default constructor
+    FuncDeclaration *staticCtor;
+    FuncDeclaration *staticDtor;
+    Array vtbl;				// Array of FuncDeclaration's making up the vtbl[]
+    Array vtblFinal;			// More FuncDeclaration's that aren't in vtbl[]
+
+    BaseClasses baseclasses;		// Array of BaseClass's; first is super,
+					// rest are Interface's
+
+    int interfaces_dim;
+    BaseClass **interfaces;		// interfaces[interfaces_dim] for this class
+					// (does not include baseClass)
+
+    BaseClasses *vtblInterfaces;	// array of base interfaces that have
+					// their own vtbl[]
+
+    ClassInfoDeclaration *vclassinfo;	// the ClassInfo object for this ClassDeclaration
+    int com;				// !=0 if this is a COM class (meaning
+					// it derives from IUnknown)
+    int isauto;				// !=0 if this is an auto class
+    int isabstract;			// !=0 if abstract class
+
+    int isnested;			// !=0 if is nested
+    VarDeclaration *vthis;		// 'this' parameter if this class is nested
+
+    int inuse;				// to prevent recursive attempts
+
+    ClassDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses);
+    Dsymbol *syntaxCopy(Dsymbol *s);
+    void semantic(Scope *sc);
+    void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
+    int isBaseOf2(ClassDeclaration *cd);
+
+    #define OFFSET_RUNTIME 0x76543210
+    virtual int isBaseOf(ClassDeclaration *cd, int *poffset);
+
+    Dsymbol *search(Loc, Identifier *ident, int flags);
+#if V2
+    int isFuncHidden(FuncDeclaration *fd);
+#endif
+    FuncDeclaration *findFunc(Identifier *ident, TypeFunction *tf);
+    void interfaceSemantic(Scope *sc);
+    int isNested();
+    int isCOMclass();
+    virtual int isCOMinterface();
+#if V2
+    virtual int isCPPinterface();
+#endif
+    int isAbstract();
+    virtual int vtblOffset();
+    char *kind();
+    char *mangle();
+    void toDocBuffer(OutBuffer *buf);
+
+    PROT getAccess(Dsymbol *smember);	// determine access to smember
+
+    void addLocalClass(ClassDeclarations *);
+
+    // Back end
+    void toObjFile();			// compile to .obj file
+    void toDebug();
+    unsigned baseVtblOffset(BaseClass *bc);
+    Symbol *toSymbol();
+    Symbol *toVtblSymbol();
+    void toDt(dt_t **pdt);
+    void toDt2(dt_t **pdt, ClassDeclaration *cd);
+
+    Symbol *vtblsym;
+
+    // llvm
+    void offsetToIndex(Type* t, unsigned os, std::vector<unsigned>& result);
+
+    ClassDeclaration *isClassDeclaration() { return (ClassDeclaration *)this; }
+};
+
+struct InterfaceDeclaration : ClassDeclaration
+{
+#if V2
+    int cpp;				// !=0 if this is a C++ interface
+#endif
+    InterfaceDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses);
+    Dsymbol *syntaxCopy(Dsymbol *s);
+    void semantic(Scope *sc);
+    int isBaseOf(ClassDeclaration *cd, int *poffset);
+    int isBaseOf(BaseClass *bc, int *poffset);
+    char *kind();
+    int vtblOffset();
+#if V2
+    int isCPPinterface();
+#endif
+    virtual int isCOMinterface();
+
+    void toObjFile();			// compile to .obj file
+    Symbol *toSymbol();
+
+    InterfaceDeclaration *isInterfaceDeclaration() { return this; }
+};
+
+#endif /* DMD_AGGREGATE_H */
--- a/dmd/attrib.c	Thu May 01 13:33:02 2008 +0200
+++ b/dmd/attrib.c	Thu May 01 15:15:28 2008 +0200
@@ -1,1292 +1,1319 @@
-
-// Compiler implementation of the D programming language
-// Copyright (c) 1999-2007 by Digital Mars
-// All Rights Reserved
-// written by Walter Bright
-// http://www.digitalmars.com
-// License for redistribution is by either the Artistic License
-// in artistic.txt, or the GNU General Public License in gnu.txt.
-// See the included readme.txt for details.
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-
-#if _WIN32 || IN_GCC || IN_LLVM
-#include "mem.h"
-#elif linux
-#include "../root/mem.h"
-#endif
-
-#include "init.h"
-#include "declaration.h"
-#include "attrib.h"
-#include "cond.h"
-#include "scope.h"
-#include "id.h"
-#include "expression.h"
-#include "dsymbol.h"
-#include "aggregate.h"
-#include "module.h"
-#include "parse.h"
-#include "template.h"
-
-#include "../gen/enums.h"
-#include "../gen/logger.h"
-
-extern void obj_includelib(char *name);
-
-
-/********************************* AttribDeclaration ****************************/
-
-AttribDeclaration::AttribDeclaration(Array *decl)
-	: Dsymbol()
-{
-    this->decl = decl;
-}
-
-Array *AttribDeclaration::include(Scope *sc, ScopeDsymbol *sd)
-{
-    return decl;
-}
-
-int AttribDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
-{
-    unsigned i;
-    int m = 0;
-    Array *d = include(sc, sd);
-
-    if (d)
-    {
-	for (i = 0; i < d->dim; i++)
-	{   Dsymbol *s;
-
-	    s = (Dsymbol *)d->data[i];
-	    m |= s->addMember(sc, sd, m | memnum);
-	}
-    }
-    return m;
-}
-
-void AttribDeclaration::semantic(Scope *sc)
-{
-    Array *d = include(sc, NULL);
-
-    //printf("\tAttribDeclaration::semantic '%s'\n",toChars());
-    if (d)
-    {
-	for (unsigned i = 0; i < d->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)d->data[i];
-
-	    s->semantic(sc);
-	}
-    }
-}
-
-void AttribDeclaration::semantic2(Scope *sc)
-{
-    unsigned i;
-    Array *d = include(sc, NULL);
-
-    if (d)
-    {
-	for (i = 0; i < d->dim; i++)
-	{   Dsymbol *s;
-
-	    s = (Dsymbol *)d->data[i];
-	    s->semantic2(sc);
-	}
-    }
-}
-
-void AttribDeclaration::semantic3(Scope *sc)
-{
-    unsigned i;
-    Array *d = include(sc, NULL);
-
-    if (d)
-    {
-	for (i = 0; i < d->dim; i++)
-	{   Dsymbol *s;
-
-	    s = (Dsymbol *)d->data[i];
-	    s->semantic3(sc);
-	}
-    }
-}
-
-void AttribDeclaration::inlineScan()
-{
-    unsigned i;
-    Array *d = include(NULL, NULL);
-
-    if (d)
-    {
-	for (i = 0; i < d->dim; i++)
-	{   Dsymbol *s;
-
-	    s = (Dsymbol *)d->data[i];
-	    //printf("AttribDeclaration::inlineScan %s\n", s->toChars());
-	    s->inlineScan();
-	}
-    }
-}
-
-void AttribDeclaration::addComment(unsigned char *comment)
-{
-    if (comment)
-    {
-	unsigned i;
-	Array *d = include(NULL, NULL);
-
-	if (d)
-	{
-	    for (i = 0; i < d->dim; i++)
-	    {   Dsymbol *s;
-
-		s = (Dsymbol *)d->data[i];
-		//printf("AttribDeclaration::addComment %s\n", s->toChars());
-		s->addComment(comment);
-	    }
-	}
-    }
-}
-
-void AttribDeclaration::emitComment(Scope *sc)
-{
-    //printf("AttribDeclaration::emitComment(sc = %p)\n", sc);
-
-    /* If generating doc comment, skip this because if we're inside
-     * a template, then include(NULL, NULL) will fail.
-     */
-//    if (sc->docbuf)
-//	return;
-
-    unsigned i;
-    Array *d = include(NULL, NULL);
-
-    if (d)
-    {
-	for (i = 0; i < d->dim; i++)
-	{   Dsymbol *s;
-
-	    s = (Dsymbol *)d->data[i];
-	    //printf("AttribDeclaration::emitComment %s\n", s->toChars());
-	    s->emitComment(sc);
-	}
-    }
-}
-
-void AttribDeclaration::toObjFile()
-{
-    unsigned i;
-    Array *d = include(NULL, NULL);
-
-    if (d)
-    {
-	for (i = 0; i < d->dim; i++)
-	{   Dsymbol *s;
-
-	    s = (Dsymbol *)d->data[i];
-	    s->toObjFile();
-	}
-    }
-}
-
-int AttribDeclaration::cvMember(unsigned char *p)
-{
-    unsigned i;
-    int nwritten = 0;
-    int n;
-    Array *d = include(NULL, NULL);
-
-    if (d)
-    {
-    for (i = 0; i < d->dim; i++)
-    {   Dsymbol *s;
-
-        s = (Dsymbol *)d->data[i];
-        n = s->cvMember(p);
-        if (p)
-        p += n;
-        nwritten += n;
-    }
-    }
-    return nwritten;
-}
-
-int AttribDeclaration::hasPointers()
-{
-    Array *d = include(NULL, NULL);
-
-    if (d)
-    {
-	for (size_t i = 0; i < d->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)d->data[i];
-	    if (s->hasPointers())
-		return 1;
-	}
-    }
-    return 0;
-}
-
-char *AttribDeclaration::kind()
-{
-    return "attribute";
-}
-
-int AttribDeclaration::oneMember(Dsymbol **ps)
-{
-    Array *d = include(NULL, NULL);
-
-    return Dsymbol::oneMembers(d, ps);
-}
-
-void AttribDeclaration::checkCtorConstInit()
-{
-    unsigned i;
-    Array *d = include(NULL, NULL);
-
-    if (d)
-    {
-	for (i = 0; i < d->dim; i++)
-	{   Dsymbol *s;
-
-	    s = (Dsymbol *)d->data[i];
-	    s->checkCtorConstInit();
-	}
-    }
-}
-
-/****************************************
- */
-
-void AttribDeclaration::addLocalClass(ClassDeclarations *aclasses)
-{   unsigned i;
-    Array *d = include(NULL, NULL);
-
-    if (d)
-    {
-	for (i = 0; i < d->dim; i++)
-	{   Dsymbol *s;
-
-	    s = (Dsymbol *)d->data[i];
-	    s->addLocalClass(aclasses);
-	}
-    }
-}
-
-
-void AttribDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    if (decl)
-    {
-	buf->writenl();
-	buf->writeByte('{');
-	buf->writenl();
-	for (unsigned i = 0; i < decl->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)decl->data[i];
-
-	    buf->writestring("    ");
-	    s->toCBuffer(buf, hgs);
-	}
-	buf->writeByte('}');
-    }
-    else
-	buf->writeByte(';');
-    buf->writenl();
-}
-
-/************************* StorageClassDeclaration ****************************/
-
-StorageClassDeclaration::StorageClassDeclaration(unsigned stc, Array *decl)
-	: AttribDeclaration(decl)
-{
-    this->stc = stc;
-}
-
-Dsymbol *StorageClassDeclaration::syntaxCopy(Dsymbol *s)
-{
-    StorageClassDeclaration *scd;
-
-    assert(!s);
-    scd = new StorageClassDeclaration(stc, Dsymbol::arraySyntaxCopy(decl));
-    return scd;
-}
-
-void StorageClassDeclaration::semantic(Scope *sc)
-{
-    if (decl)
-    {	unsigned stc_save = sc->stc;
-
-	if (stc & (STCauto | STCscope | STCstatic | STCextern))
-	    sc->stc &= ~(STCauto | STCscope | STCstatic | STCextern);
-	sc->stc |= stc;
-	for (unsigned i = 0; i < decl->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)decl->data[i];
-
-	    s->semantic(sc);
-	}
-	sc->stc = stc_save;
-    }
-    else
-	sc->stc = stc;
-}
-
-void StorageClassDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    struct SCstring
-    {
-	int stc;
-	enum TOK tok;
-    };
-
-    static SCstring table[] =
-    {
-	{ STCauto,         TOKauto },
-	{ STCscope,        TOKscope },
-	{ STCstatic,       TOKstatic },
-	{ STCextern,       TOKextern },
-	{ STCconst,        TOKconst },
-	{ STCfinal,        TOKfinal },
-	{ STCabstract,     TOKabstract },
-	{ STCsynchronized, TOKsynchronized },
-	{ STCdeprecated,   TOKdeprecated },
-	{ STCoverride,     TOKoverride },
-    };
-
-    int written = 0;
-    for (int i = 0; i < sizeof(table)/sizeof(table[0]); i++)
-    {
-	if (stc & table[i].stc)
-	{
-	    if (written)
-		buf->writeByte(' ');
-	    written = 1;
-	    buf->writestring(Token::toChars(table[i].tok));
-	}
-    }
-
-    AttribDeclaration::toCBuffer(buf, hgs);
-}
-
-/********************************* LinkDeclaration ****************************/
-
-LinkDeclaration::LinkDeclaration(enum LINK p, Array *decl)
-	: AttribDeclaration(decl)
-{
-    //printf("LinkDeclaration(linkage = %d, decl = %p)\n", p, decl);
-    linkage = p;
-}
-
-Dsymbol *LinkDeclaration::syntaxCopy(Dsymbol *s)
-{
-    LinkDeclaration *ld;
-
-    assert(!s);
-    ld = new LinkDeclaration(linkage, Dsymbol::arraySyntaxCopy(decl));
-    return ld;
-}
-
-void LinkDeclaration::semantic(Scope *sc)
-{
-    //printf("LinkDeclaration::semantic(linkage = %d, decl = %p)\n", linkage, decl);
-    if (decl)
-    {	enum LINK linkage_save = sc->linkage;
-
-	sc->linkage = linkage;
-	for (unsigned i = 0; i < decl->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)decl->data[i];
-
-	    s->semantic(sc);
-	}
-	sc->linkage = linkage_save;
-    }
-    else
-    {
-	sc->linkage = linkage;
-    }
-}
-
-void LinkDeclaration::semantic3(Scope *sc)
-{
-    //printf("LinkDeclaration::semantic3(linkage = %d, decl = %p)\n", linkage, decl);
-    if (decl)
-    {	enum LINK linkage_save = sc->linkage;
-
-	sc->linkage = linkage;
-	for (unsigned i = 0; i < decl->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)decl->data[i];
-
-	    s->semantic3(sc);
-	}
-	sc->linkage = linkage_save;
-    }
-    else
-    {
-	sc->linkage = linkage;
-    }
-}
-
-void LinkDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{   char *p;
-
-    switch (linkage)
-    {
-	case LINKd:		p = "D";		break;
-	case LINKc:		p = "C";		break;
-	case LINKcpp:		p = "C++";		break;
-	case LINKwindows:	p = "Windows";		break;
-	case LINKpascal:	p = "Pascal";		break;
-	default:
-	    assert(0);
-	    break;
-    }
-    buf->writestring("extern (");
-    buf->writestring(p);
-    buf->writestring(") ");
-    AttribDeclaration::toCBuffer(buf, hgs);
-}
-
-char *LinkDeclaration::toChars()
-{
-    return "extern ()";
-}
-
-/********************************* ProtDeclaration ****************************/
-
-ProtDeclaration::ProtDeclaration(enum PROT p, Array *decl)
-	: AttribDeclaration(decl)
-{
-    protection = p;
-    //printf("decl = %p\n", decl);
-}
-
-Dsymbol *ProtDeclaration::syntaxCopy(Dsymbol *s)
-{
-    ProtDeclaration *pd;
-
-    assert(!s);
-    pd = new ProtDeclaration(protection, Dsymbol::arraySyntaxCopy(decl));
-    return pd;
-}
-
-void ProtDeclaration::semantic(Scope *sc)
-{
-    if (decl)
-    {	enum PROT protection_save = sc->protection;
-	int explicitProtection_save = sc->explicitProtection;
-
-	sc->protection = protection;
-	sc->explicitProtection = 1;
-	for (unsigned i = 0; i < decl->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)decl->data[i];
-
-	    s->semantic(sc);
-	}
-	sc->protection = protection_save;
-	sc->explicitProtection = explicitProtection_save;
-    }
-    else
-    {	sc->protection = protection;
-	sc->explicitProtection = 1;
-    }
-}
-
-void ProtDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{   char *p;
-
-    switch (protection)
-    {
-	case PROTprivate:	p = "private";		break;
-	case PROTpackage:	p = "package";		break;
-	case PROTprotected:	p = "protected";	break;
-	case PROTpublic:	p = "public";		break;
-	case PROTexport:	p = "export";		break;
-	default:
-	    assert(0);
-	    break;
-    }
-    buf->writestring(p);
-    AttribDeclaration::toCBuffer(buf, hgs);
-}
-
-/********************************* AlignDeclaration ****************************/
-
-AlignDeclaration::AlignDeclaration(unsigned sa, Array *decl)
-	: AttribDeclaration(decl)
-{
-    salign = sa;
-}
-
-Dsymbol *AlignDeclaration::syntaxCopy(Dsymbol *s)
-{
-    AlignDeclaration *ad;
-
-    assert(!s);
-    ad = new AlignDeclaration(salign, Dsymbol::arraySyntaxCopy(decl));
-    return ad;
-}
-
-void AlignDeclaration::semantic(Scope *sc)
-{
-    //printf("\tAlignDeclaration::semantic '%s'\n",toChars());
-    if (decl)
-    {	unsigned salign_save = sc->structalign;
-
-	sc->structalign = salign;
-	for (unsigned i = 0; i < decl->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)decl->data[i];
-
-	    s->semantic(sc);
-	}
-	sc->structalign = salign_save;
-    }
-    else
-	sc->structalign = salign;
-}
-
-
-void AlignDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    buf->printf("align (%d)", salign);
-    AttribDeclaration::toCBuffer(buf, hgs);
-}
-
-/********************************* AnonDeclaration ****************************/
-
-AnonDeclaration::AnonDeclaration(Loc loc, int isunion, Array *decl)
-	: AttribDeclaration(decl)
-{
-    this->loc = loc;
-    this->isunion = isunion;
-    this->scope = NULL;
-    this->sem = 0;
-}
-
-Dsymbol *AnonDeclaration::syntaxCopy(Dsymbol *s)
-{
-    AnonDeclaration *ad;
-
-    assert(!s);
-    ad = new AnonDeclaration(loc, isunion, Dsymbol::arraySyntaxCopy(decl));
-    return ad;
-}
-
-void AnonDeclaration::semantic(Scope *sc)
-{
-    //printf("\tAnonDeclaration::semantic %s %p\n", isunion ? "union" : "struct", this);
-
-    Scope *scx = NULL;
-    if (scope)
-    {   sc = scope;
-	scx = scope;
-	scope = NULL;
-    }
-
-    assert(sc->parent);
-
-    Dsymbol *parent = sc->parent->pastMixin();
-    AggregateDeclaration *ad = parent->isAggregateDeclaration();
-
-    if (!ad || (!ad->isStructDeclaration() && !ad->isClassDeclaration()))
-    {
-	error("can only be a part of an aggregate");
-	return;
-    }
-
-    if (decl)
-    {
-	AnonymousAggregateDeclaration aad;
-	int adisunion;
-
-	if (sc->anonAgg)
-	{   ad = sc->anonAgg;
-	    adisunion = sc->inunion;
-	}
-	else
-	    adisunion = ad->isUnionDeclaration() != NULL;
-
-//	printf("\tsc->anonAgg = %p\n", sc->anonAgg);
-//	printf("\tad  = %p\n", ad);
-//	printf("\taad = %p\n", &aad);
-
-	sc = sc->push();
-	sc->anonAgg = &aad;
-	sc->stc &= ~(STCauto | STCscope | STCstatic);
-	sc->inunion = isunion;
-	sc->offset = 0;
-	sc->flags = 0;
-	aad.structalign = sc->structalign;
-	aad.parent = ad;
-
-	for (unsigned i = 0; i < decl->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)decl->data[i];
-
-	    s->semantic(sc);
-	    if (isunion)
-		sc->offset = 0;
-	    if (aad.sizeok == 2)
-	    {
-		break;
-	    }
-	}
-	sc = sc->pop();
-
-	// If failed due to forward references, unwind and try again later
-	if (aad.sizeok == 2)
-	{
-	    ad->sizeok = 2;
-	    //printf("\tsetting ad->sizeok %p to 2\n", ad);
-	    if (!sc->anonAgg)
-	    {
-		scope = scx ? scx : new Scope(*sc);
-		scope->setNoFree();
-		scope->module->addDeferredSemantic(this);
-	    }
-	    //printf("\tforward reference %p\n", this);
-	    return;
-	}
-	if (sem == 0)
-	{   Module::dprogress++;
-	    sem = 1;
-	    //printf("\tcompleted %p\n", this);
-	}
-	else
-	    ;//printf("\talready completed %p\n", this);
-
-	// 0 sized structs are set to 1 byte
-	if (aad.structsize == 0)
-	{
-	    aad.structsize = 1;
-	    aad.alignsize = 1;
-	}
-
-	// Align size of anonymous aggregate
-//printf("aad.structalign = %d, aad.alignsize = %d, sc->offset = %d\n", aad.structalign, aad.alignsize, sc->offset);
-	ad->alignmember(aad.structalign, aad.alignsize, &sc->offset);
-	//ad->structsize = sc->offset;
-//printf("sc->offset = %d\n", sc->offset);
-
-	// Add members of aad to ad
-	//printf("\tadding members of aad to '%s'\n", ad->toChars());
-	for (unsigned i = 0; i < aad.fields.dim; i++)
-	{
-	    VarDeclaration *v = (VarDeclaration *)aad.fields.data[i];
-
-	    v->offset += sc->offset;
-	    ad->fields.push(v);
-	}
-
-	// Add size of aad to ad
-	if (adisunion)
-	{
-	    if (aad.structsize > ad->structsize)
-		ad->structsize = aad.structsize;
-	    sc->offset = 0;
-	}
-	else
-	{
-	    ad->structsize = sc->offset + aad.structsize;
-	    sc->offset = ad->structsize;
-	}
-
-	if (ad->alignsize < aad.alignsize)
-	    ad->alignsize = aad.alignsize;
-    }
-}
-
-
-void AnonDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    buf->printf(isunion ? "union" : "struct");
-    buf->writestring("\n{\n");
-    if (decl)
-    {
-	for (unsigned i = 0; i < decl->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)decl->data[i];
-
-	    //buf->writestring("    ");
-	    s->toCBuffer(buf, hgs);
-	}
-    }
-    buf->writestring("}\n");
-}
-
-char *AnonDeclaration::kind()
-{
-    return (char *)(isunion ? "anonymous union" : "anonymous struct");
-}
-
-/********************************* PragmaDeclaration ****************************/
-
-PragmaDeclaration::PragmaDeclaration(Loc loc, Identifier *ident, Expressions *args, Array *decl)
-	: AttribDeclaration(decl)
-{
-    this->loc = loc;
-    this->ident = ident;
-    this->args = args;
-}
-
-Dsymbol *PragmaDeclaration::syntaxCopy(Dsymbol *s)
-{
-    PragmaDeclaration *pd;
-
-    assert(!s);
-    pd = new PragmaDeclaration(loc, ident,
-	Expression::arraySyntaxCopy(args), Dsymbol::arraySyntaxCopy(decl));
-    return pd;
-}
-
-void PragmaDeclaration::semantic(Scope *sc)
-{   // Should be merged with PragmaStatement
-
-#if IN_LLVM
-    int llvm_internal = 0;
-    char* llvm_str1 = NULL;
-
-#endif
-
-    //printf("\tPragmaDeclaration::semantic '%s'\n",toChars());
-    if (ident == Id::msg)
-    {
-	if (args)
-	{
-	    for (size_t i = 0; i < args->dim; i++)
-	    {
-		Expression *e = (Expression *)args->data[i];
-
-		e = e->semantic(sc);
-		e = e->optimize(WANTvalue | WANTinterpret);
-		if (e->op == TOKstring)
-		{
-		    StringExp *se = (StringExp *)e;
-		    fprintf(stdmsg, "%.*s", (int)se->len, se->string);
-		}
-		else
-		    error("string expected for message, not '%s'", e->toChars());
-	    }
-	    fprintf(stdmsg, "\n");
-	}
-	goto Lnodecl;
-    }
-    else if (ident == Id::lib)
-    {
-	if (!args || args->dim != 1)
-	    error("string expected for library name");
-	else
-	{
-	    Expression *e = (Expression *)args->data[0];
-
-	    e = e->semantic(sc);
-	    e = e->optimize(WANTvalue | WANTinterpret);
-	    args->data[0] = (void *)e;
-	    if (e->op != TOKstring)
-		error("string expected for library name, not '%s'", e->toChars());
-	    else if (global.params.verbose)
-	    {
-		StringExp *se = (StringExp *)e;
-		char *name = (char *)mem.malloc(se->len + 1);
-		memcpy(name, se->string, se->len);
-		name[se->len] = 0;
-		printf("library   %s\n", name);
-		mem.free(name);
-	    }
-	}
-	goto Lnodecl;
-    }
-#if IN_GCC
-    else if (ident == Id::GNU_asm)
-    {
-	if (! args || args->dim != 2)
-	    error("identifier and string expected for asm name");
-	else
-	{
-	    Expression *e;
-	    Declaration *d = NULL;
-	    StringExp *s = NULL;
-
-	    e = (Expression *)args->data[0];
-	    e = e->semantic(sc);
-	    if (e->op == TOKvar)
-	    {
-		d = ((VarExp *)e)->var;
-		if (! d->isFuncDeclaration() && ! d->isVarDeclaration())
-		    d = NULL;
-	    }
-	    if (!d)
-		error("first argument of GNU_asm must be a function or variable declaration");
-
-	    e = (Expression *)args->data[1];
-	    e = e->semantic(sc);
-	    e = e->optimize(WANTvalue);
-	    if (e->op == TOKstring && ((StringExp *)e)->sz == 1)
-		s = ((StringExp *)e);
-	    else
-		error("second argument of GNU_asm must be a char string");
-
-	    if (d && s)
-		d->c_ident = Lexer::idPool((char*) s->string);
-	}
-	goto Lnodecl;
-    }
-#endif
-#if IN_LLVM
-    else if (ident == Id::LLVM_internal)
-    {
-        if (!args || args->dim < 1 || args->dim > 2)
-            error("needs 1-3 parameters");
-        else if (!decl || decl->dim < 1)
-            error("must apply to at least one declaration");
-        else
-        {
-            Expression *e;
-            StringExp *s = NULL;
-
-            e = (Expression *)args->data[0];
-            e = e->semantic(sc);
-            e = e->optimize(WANTvalue);
-            if (e->op == TOKstring && (s = (StringExp *)e))
-            {
-                char* str = (char*)s->string;
-                if (strcmp(str,"intrinsic")==0) {
-                    llvm_internal = LLVMintrinsic;
-                    assert(args->dim == 2);
-                }
-                else if (strcmp(str,"va_start")==0) {
-                    llvm_internal = LLVMva_start;
-                    assert(args->dim == 1);
-                }
-                else if (strcmp(str,"va_arg")==0) {
-                    llvm_internal = LLVMva_arg;
-                    assert(args->dim == 1);
-                }
-                else if (strcmp(str,"va_intrinsic")==0) {
-                    llvm_internal = LLVMva_intrinsic;
-                    assert(args->dim == 2);
-                }
-                else if (strcmp(str,"notypeinfo")==0) {
-                    llvm_internal = LLVMnotypeinfo;
-                    assert(args->dim == 1);
-                }
-                else if (strcmp(str,"alloca")==0) {
-                    llvm_internal = LLVMalloca;
-                    assert(args->dim == 1);
-                }
-                else {
-                    error("unknown pragma command: %s", str);
-                }
-            }
-            else
-            error("1st argument must be a string");
-
-            if (llvm_internal)
-            switch (llvm_internal)
-            {
-            case LLVMintrinsic:
-            case LLVMva_intrinsic:
-                e = (Expression *)args->data[1];
-                e = e->semantic(sc);
-                e = e->optimize(WANTvalue);
-                if (e->op == TOKstring && (s = (StringExp *)e)) {
-                    llvm_str1 = (char*)s->string;
-                }
-                else
-                error("2nd argument must be a string");
-                break;
-
-            case LLVMva_arg:
-            case LLVMva_start:
-            case LLVMnotypeinfo:
-            case LLVMalloca:
-                break;
-
-            default:
-                assert(0);
-            }
-        }
-    }
-#endif
-    else
-	error("unrecognized pragma(%s)", ident->toChars());
-
-    if (decl)
-    {
-	for (unsigned i = 0; i < decl->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)decl->data[i];
-
-	    s->semantic(sc);
-        
-#if IN_LLVM
-        if (llvm_internal)
-        {
-            switch(llvm_internal)
-            {
-            case LLVMintrinsic:
-            case LLVMva_intrinsic:
-                if (FuncDeclaration* fd = s->isFuncDeclaration()) {
-                    fd->llvmInternal = llvm_internal;
-                    fd->llvmInternal1 = llvm_str1;
-                }
-                else {
-                    error("may only be used on function declarations");
-                    assert(0);
-                }
-                break;
-
-            case LLVMva_start:
-            case LLVMva_arg:
-                if (TemplateDeclaration* td = s->isTemplateDeclaration()) {
-                    td->llvmInternal = llvm_internal;
-                    assert(td->parameters->dim == 1);
-                    assert(!td->overnext);
-                    assert(!td->overroot);
-                    assert(td->onemember);
-                    Logger::println("template->onemember = %s", td->onemember->toChars());
-                }
-                else {
-                    error("can only be used on templates");
-                    assert(0);
-                }
-                break;
-
-            case LLVMnotypeinfo:
-                s->llvmInternal = llvm_internal;
-                break;
-
-            case LLVMalloca:
-                if (FuncDeclaration* fd = s->isFuncDeclaration()) {
-                    fd->llvmInternal = llvm_internal;
-                }
-                else {
-                    error("may only be used on function declarations");
-                    assert(0);
-                }
-                break;
-
-            default:
-                assert(0 && "invalid LLVM_internal pragma got through :/");
-            }
-        }
-        
-#endif
-    }
-    }
-    return;
-
-Lnodecl:
-    if (decl)
-	error("pragma is missing closing ';'");
-}
-
-int PragmaDeclaration::oneMember(Dsymbol **ps)
-{
-    *ps = NULL;
-    return TRUE;
-}
-
-char *PragmaDeclaration::kind()
-{
-    return "pragma";
-}
-
-void PragmaDeclaration::toObjFile()
-{
-    if (ident == Id::lib)
-    {
-	assert(args && args->dim == 1);
-
-	Expression *e = (Expression *)args->data[0];
-
-	assert(e->op == TOKstring);
-
-	StringExp *se = (StringExp *)e;
-	char *name = (char *)mem.malloc(se->len + 1);
-	memcpy(name, se->string, se->len);
-	name[se->len] = 0;
-	obj_includelib(name);
-    }
-    AttribDeclaration::toObjFile();
-}
-
-void PragmaDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    buf->printf("pragma(%s", ident->toChars());
-    if (args)
-    {
-	for (size_t i = 0; i < args->dim; i++)
-	{
-	    Expression *e = (Expression *)args->data[i];
-
-	    buf->writestring(", ");
-	    e->toCBuffer(buf, hgs);
-	}
-    }
-    buf->writestring(")");
-    AttribDeclaration::toCBuffer(buf, hgs);
-}
-
-
-/********************************* ConditionalDeclaration ****************************/
-
-ConditionalDeclaration::ConditionalDeclaration(Condition *condition, Array *decl, Array *elsedecl)
-	: AttribDeclaration(decl)
-{
-    //printf("ConditionalDeclaration::ConditionalDeclaration()\n");
-    this->condition = condition;
-    this->elsedecl = elsedecl;
-}
-
-Dsymbol *ConditionalDeclaration::syntaxCopy(Dsymbol *s)
-{
-    ConditionalDeclaration *dd;
-
-    assert(!s);
-    dd = new ConditionalDeclaration(condition->syntaxCopy(),
-	Dsymbol::arraySyntaxCopy(decl),
-	Dsymbol::arraySyntaxCopy(elsedecl));
-    return dd;
-}
-
-
-int ConditionalDeclaration::oneMember(Dsymbol **ps)
-{
-    //printf("ConditionalDeclaration::oneMember(), inc = %d\n", condition->inc);
-    if (condition->inc)
-    {
-	Array *d = condition->include(NULL, NULL) ? decl : elsedecl;
-	return Dsymbol::oneMembers(d, ps);
-    }
-    *ps = NULL;
-    return TRUE;
-}
-
-void ConditionalDeclaration::emitComment(Scope *sc)
-{
-    //printf("ConditionalDeclaration::emitComment(sc = %p)\n", sc);
-    if (condition->inc)
-    {
-	AttribDeclaration::emitComment(sc);
-    }
-}
-
-// Decide if 'then' or 'else' code should be included
-
-Array *ConditionalDeclaration::include(Scope *sc, ScopeDsymbol *sd)
-{
-    //printf("ConditionalDeclaration::include()\n");
-    assert(condition);
-    return condition->include(sc, sd) ? decl : elsedecl;
-}
-
-
-void ConditionalDeclaration::addComment(unsigned char *comment)
-{
-    /* Because addComment is called by the parser, if we called
-     * include() it would define a version before it was used.
-     * But it's no problem to drill down to both decl and elsedecl,
-     * so that's the workaround.
-     */
-
-    if (comment)
-    {
-	Array *d = decl;
-
-	for (int j = 0; j < 2; j++)
-	{
-	    if (d)
-	    {
-		for (unsigned i = 0; i < d->dim; i++)
-		{   Dsymbol *s;
-
-		    s = (Dsymbol *)d->data[i];
-		    //printf("ConditionalDeclaration::addComment %s\n", s->toChars());
-		    s->addComment(comment);
-		}
-	    }
-	    d = elsedecl;
-	}
-    }
-}
-
-void ConditionalDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    condition->toCBuffer(buf, hgs);
-    if (decl || elsedecl)
-    {
-	buf->writenl();
-	buf->writeByte('{');
-	buf->writenl();
-	if (decl)
-	{
-	    for (unsigned i = 0; i < decl->dim; i++)
-	    {
-		Dsymbol *s = (Dsymbol *)decl->data[i];
-
-		buf->writestring("    ");
-		s->toCBuffer(buf, hgs);
-	    }
-	}
-	buf->writeByte('}');
-	if (elsedecl)
-	{
-	    buf->writenl();
-	    buf->writestring("else");
-	    buf->writenl();
-	    buf->writeByte('{');
-	    buf->writenl();
-	    for (unsigned i = 0; i < elsedecl->dim; i++)
-	    {
-		Dsymbol *s = (Dsymbol *)elsedecl->data[i];
-
-		buf->writestring("    ");
-		s->toCBuffer(buf, hgs);
-	    }
-	    buf->writeByte('}');
-	}
-    }
-    else
-	buf->writeByte(':');
-    buf->writenl();
-}
-
-/***************************** StaticIfDeclaration ****************************/
-
-StaticIfDeclaration::StaticIfDeclaration(Condition *condition,
-	Array *decl, Array *elsedecl)
-	: ConditionalDeclaration(condition, decl, elsedecl)
-{
-    //printf("StaticIfDeclaration::StaticIfDeclaration()\n");
-    sd = NULL;
-    addisdone = 0;
-}
-
-
-Dsymbol *StaticIfDeclaration::syntaxCopy(Dsymbol *s)
-{
-    StaticIfDeclaration *dd;
-
-    assert(!s);
-    dd = new StaticIfDeclaration(condition->syntaxCopy(),
-	Dsymbol::arraySyntaxCopy(decl),
-	Dsymbol::arraySyntaxCopy(elsedecl));
-    return dd;
-}
-
-
-int StaticIfDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
-{
-    /* This is deferred until semantic(), so that
-     * expressions in the condition can refer to declarations
-     * in the same scope, such as:
-     *
-     * template Foo(int i)
-     * {
-     *     const int j = i + 1;
-     *     static if (j == 3)
-     *         const int k;
-     * }
-     */
-    this->sd = sd;
-    int m = 0;
-
-    if (memnum == 0)
-    {	m = AttribDeclaration::addMember(sc, sd, memnum);
-	addisdone = 1;
-    }
-    return m;
-}
-
-
-void StaticIfDeclaration::semantic(Scope *sc)
-{
-    Array *d = include(sc, sd);
-
-    //printf("\tStaticIfDeclaration::semantic '%s'\n",toChars());
-    if (d)
-    {
-	if (!addisdone)
-	{   AttribDeclaration::addMember(sc, sd, 1);
-	    addisdone = 1;
-	}
-
-	for (unsigned i = 0; i < d->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)d->data[i];
-
-	    s->semantic(sc);
-	}
-    }
-}
-
-char *StaticIfDeclaration::kind()
-{
-    return "static if";
-}
-
-
-/***************************** CompileDeclaration *****************************/
-
-CompileDeclaration::CompileDeclaration(Loc loc, Expression *exp)
-    : AttribDeclaration(NULL)
-{
-    this->exp = exp;
-    this->sd = NULL;
-}
-
-Dsymbol *CompileDeclaration::syntaxCopy(Dsymbol *s)
-{
-    //printf("CompileDeclaration::syntaxCopy('%s')\n", toChars());
-    CompileDeclaration *sc = new CompileDeclaration(loc, exp->syntaxCopy());
-    return sc;
-}
-
-int CompileDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
-{
-    this->sd = sd;
-    return memnum;
-}
-
-void CompileDeclaration::semantic(Scope *sc)
-{
-    //printf("CompileDeclaration::semantic()\n");
-    exp = exp->semantic(sc);
-    exp = resolveProperties(sc, exp);
-    exp = exp->optimize(WANTvalue | WANTinterpret);
-    if (exp->op != TOKstring)
-    {	error("argument to mixin must be a string, not (%s)", exp->toChars());
-	return;
-    }
-    StringExp *se = (StringExp *)exp;
-    se = se->toUTF8(sc);
-    Parser p(sc->module, (unsigned char *)se->string, se->len, 0);
-    p.loc = loc;
-    p.nextToken();
-    decl = p.parseDeclDefs(0);
-    if (p.token.value != TOKeof)
-    {
-	error("incomplete mixin declaration (%s)", se->toChars());
-    }
-
-    AttribDeclaration::addMember(sc, sd, 0);
-    AttribDeclaration::semantic(sc);
-}
-
-void CompileDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    buf->writestring("mixin(");
-    exp->toCBuffer(buf, hgs);
-    buf->writestring(");");
-    buf->writenl();
-}
+
+// Compiler implementation of the D programming language
+// Copyright (c) 1999-2007 by Digital Mars
+// All Rights Reserved
+// written by Walter Bright
+// http://www.digitalmars.com
+// License for redistribution is by either the Artistic License
+// in artistic.txt, or the GNU General Public License in gnu.txt.
+// See the included readme.txt for details.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#if _WIN32 || IN_GCC || IN_LLVM
+#include "mem.h"
+#elif linux
+#include "../root/mem.h"
+#endif
+
+#include "init.h"
+#include "declaration.h"
+#include "attrib.h"
+#include "cond.h"
+#include "scope.h"
+#include "id.h"
+#include "expression.h"
+#include "dsymbol.h"
+#include "aggregate.h"
+#include "module.h"
+#include "parse.h"
+#include "template.h"
+
+#include "../gen/enums.h"
+#include "../gen/logger.h"
+
+extern void obj_includelib(char *name);
+
+
+/********************************* AttribDeclaration ****************************/
+
+AttribDeclaration::AttribDeclaration(Array *decl)
+	: Dsymbol()
+{
+    this->decl = decl;
+}
+
+Array *AttribDeclaration::include(Scope *sc, ScopeDsymbol *sd)
+{
+    return decl;
+}
+
+int AttribDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
+{
+    unsigned i;
+    int m = 0;
+    Array *d = include(sc, sd);
+
+    if (d)
+    {
+	for (i = 0; i < d->dim; i++)
+	{   Dsymbol *s;
+
+	    s = (Dsymbol *)d->data[i];
+	    m |= s->addMember(sc, sd, m | memnum);
+	}
+    }
+    return m;
+}
+
+void AttribDeclaration::semantic(Scope *sc)
+{
+    Array *d = include(sc, NULL);
+
+    //printf("\tAttribDeclaration::semantic '%s'\n",toChars());
+    if (d)
+    {
+	for (unsigned i = 0; i < d->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)d->data[i];
+
+	    s->semantic(sc);
+	}
+    }
+}
+
+void AttribDeclaration::semantic2(Scope *sc)
+{
+    unsigned i;
+    Array *d = include(sc, NULL);
+
+    if (d)
+    {
+	for (i = 0; i < d->dim; i++)
+	{   Dsymbol *s;
+
+	    s = (Dsymbol *)d->data[i];
+	    s->semantic2(sc);
+	}
+    }
+}
+
+void AttribDeclaration::semantic3(Scope *sc)
+{
+    unsigned i;
+    Array *d = include(sc, NULL);
+
+    if (d)
+    {
+	for (i = 0; i < d->dim; i++)
+	{   Dsymbol *s;
+
+	    s = (Dsymbol *)d->data[i];
+	    s->semantic3(sc);
+	}
+    }
+}
+
+void AttribDeclaration::inlineScan()
+{
+    unsigned i;
+    Array *d = include(NULL, NULL);
+
+    if (d)
+    {
+	for (i = 0; i < d->dim; i++)
+	{   Dsymbol *s;
+
+	    s = (Dsymbol *)d->data[i];
+	    //printf("AttribDeclaration::inlineScan %s\n", s->toChars());
+	    s->inlineScan();
+	}
+    }
+}
+
+void AttribDeclaration::addComment(unsigned char *comment)
+{
+    if (comment)
+    {
+	unsigned i;
+	Array *d = include(NULL, NULL);
+
+	if (d)
+	{
+	    for (i = 0; i < d->dim; i++)
+	    {   Dsymbol *s;
+
+		s = (Dsymbol *)d->data[i];
+		//printf("AttribDeclaration::addComment %s\n", s->toChars());
+		s->addComment(comment);
+	    }
+	}
+    }
+}
+
+void AttribDeclaration::emitComment(Scope *sc)
+{
+    //printf("AttribDeclaration::emitComment(sc = %p)\n", sc);
+
+    /* If generating doc comment, skip this because if we're inside
+     * a template, then include(NULL, NULL) will fail.
+     */
+//    if (sc->docbuf)
+//	return;
+
+    unsigned i;
+    Array *d = include(NULL, NULL);
+
+    if (d)
+    {
+	for (i = 0; i < d->dim; i++)
+	{   Dsymbol *s;
+
+	    s = (Dsymbol *)d->data[i];
+	    //printf("AttribDeclaration::emitComment %s\n", s->toChars());
+	    s->emitComment(sc);
+	}
+    }
+}
+
+void AttribDeclaration::toObjFile()
+{
+    unsigned i;
+    Array *d = include(NULL, NULL);
+
+    if (d)
+    {
+	for (i = 0; i < d->dim; i++)
+	{   Dsymbol *s;
+
+	    s = (Dsymbol *)d->data[i];
+	    s->toObjFile();
+	}
+    }
+}
+
+int AttribDeclaration::cvMember(unsigned char *p)
+{
+    unsigned i;
+    int nwritten = 0;
+    int n;
+    Array *d = include(NULL, NULL);
+
+    if (d)
+    {
+	for (i = 0; i < d->dim; i++)
+	{   Dsymbol *s;
+
+	    s = (Dsymbol *)d->data[i];
+	    n = s->cvMember(p);
+	    if (p)
+		p += n;
+	    nwritten += n;
+	}
+    }
+    return nwritten;
+}
+
+int AttribDeclaration::hasPointers()
+{
+    Array *d = include(NULL, NULL);
+
+    if (d)
+    {
+	for (size_t i = 0; i < d->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)d->data[i];
+	    if (s->hasPointers())
+		return 1;
+	}
+    }
+    return 0;
+}
+
+char *AttribDeclaration::kind()
+{
+    return "attribute";
+}
+
+int AttribDeclaration::oneMember(Dsymbol **ps)
+{
+    Array *d = include(NULL, NULL);
+
+    return Dsymbol::oneMembers(d, ps);
+}
+
+void AttribDeclaration::checkCtorConstInit()
+{
+    unsigned i;
+    Array *d = include(NULL, NULL);
+
+    if (d)
+    {
+	for (i = 0; i < d->dim; i++)
+	{   Dsymbol *s;
+
+	    s = (Dsymbol *)d->data[i];
+	    s->checkCtorConstInit();
+	}
+    }
+}
+
+/****************************************
+ */
+
+void AttribDeclaration::addLocalClass(ClassDeclarations *aclasses)
+{   unsigned i;
+    Array *d = include(NULL, NULL);
+
+    if (d)
+    {
+	for (i = 0; i < d->dim; i++)
+	{   Dsymbol *s;
+
+	    s = (Dsymbol *)d->data[i];
+	    s->addLocalClass(aclasses);
+	}
+    }
+}
+
+
+void AttribDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    if (decl)
+    {
+	buf->writenl();
+	buf->writeByte('{');
+	buf->writenl();
+	for (unsigned i = 0; i < decl->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)decl->data[i];
+
+	    buf->writestring("    ");
+	    s->toCBuffer(buf, hgs);
+	}
+	buf->writeByte('}');
+    }
+    else
+	buf->writeByte(';');
+    buf->writenl();
+}
+
+/************************* StorageClassDeclaration ****************************/
+
+StorageClassDeclaration::StorageClassDeclaration(unsigned stc, Array *decl)
+	: AttribDeclaration(decl)
+{
+    this->stc = stc;
+}
+
+Dsymbol *StorageClassDeclaration::syntaxCopy(Dsymbol *s)
+{
+    StorageClassDeclaration *scd;
+
+    assert(!s);
+    scd = new StorageClassDeclaration(stc, Dsymbol::arraySyntaxCopy(decl));
+    return scd;
+}
+
+void StorageClassDeclaration::semantic(Scope *sc)
+{
+    if (decl)
+    {	unsigned stc_save = sc->stc;
+
+	if (stc & (STCauto | STCscope | STCstatic | STCextern))
+	    sc->stc &= ~(STCauto | STCscope | STCstatic | STCextern);
+	sc->stc |= stc;
+	for (unsigned i = 0; i < decl->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)decl->data[i];
+
+	    s->semantic(sc);
+	}
+	sc->stc = stc_save;
+    }
+    else
+	sc->stc = stc;
+}
+
+void StorageClassDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    struct SCstring
+    {
+	int stc;
+	enum TOK tok;
+    };
+
+    static SCstring table[] =
+    {
+	{ STCauto,         TOKauto },
+	{ STCscope,        TOKscope },
+	{ STCstatic,       TOKstatic },
+	{ STCextern,       TOKextern },
+	{ STCconst,        TOKconst },
+	{ STCfinal,        TOKfinal },
+	{ STCabstract,     TOKabstract },
+	{ STCsynchronized, TOKsynchronized },
+	{ STCdeprecated,   TOKdeprecated },
+	{ STCoverride,     TOKoverride },
+    };
+
+    int written = 0;
+    for (int i = 0; i < sizeof(table)/sizeof(table[0]); i++)
+    {
+	if (stc & table[i].stc)
+	{
+	    if (written)
+		buf->writeByte(' ');
+	    written = 1;
+	    buf->writestring(Token::toChars(table[i].tok));
+	}
+    }
+
+    AttribDeclaration::toCBuffer(buf, hgs);
+}
+
+/********************************* LinkDeclaration ****************************/
+
+LinkDeclaration::LinkDeclaration(enum LINK p, Array *decl)
+	: AttribDeclaration(decl)
+{
+    //printf("LinkDeclaration(linkage = %d, decl = %p)\n", p, decl);
+    linkage = p;
+}
+
+Dsymbol *LinkDeclaration::syntaxCopy(Dsymbol *s)
+{
+    LinkDeclaration *ld;
+
+    assert(!s);
+    ld = new LinkDeclaration(linkage, Dsymbol::arraySyntaxCopy(decl));
+    return ld;
+}
+
+void LinkDeclaration::semantic(Scope *sc)
+{
+    //printf("LinkDeclaration::semantic(linkage = %d, decl = %p)\n", linkage, decl);
+    if (decl)
+    {	enum LINK linkage_save = sc->linkage;
+
+	sc->linkage = linkage;
+	for (unsigned i = 0; i < decl->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)decl->data[i];
+
+	    s->semantic(sc);
+	}
+	sc->linkage = linkage_save;
+    }
+    else
+    {
+	sc->linkage = linkage;
+    }
+}
+
+void LinkDeclaration::semantic3(Scope *sc)
+{
+    //printf("LinkDeclaration::semantic3(linkage = %d, decl = %p)\n", linkage, decl);
+    if (decl)
+    {	enum LINK linkage_save = sc->linkage;
+
+	sc->linkage = linkage;
+	for (unsigned i = 0; i < decl->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)decl->data[i];
+
+	    s->semantic3(sc);
+	}
+	sc->linkage = linkage_save;
+    }
+    else
+    {
+	sc->linkage = linkage;
+    }
+}
+
+void LinkDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{   char *p;
+
+    switch (linkage)
+    {
+	case LINKd:		p = "D";		break;
+	case LINKc:		p = "C";		break;
+	case LINKcpp:		p = "C++";		break;
+	case LINKwindows:	p = "Windows";		break;
+	case LINKpascal:	p = "Pascal";		break;
+	default:
+	    assert(0);
+	    break;
+    }
+    buf->writestring("extern (");
+    buf->writestring(p);
+    buf->writestring(") ");
+    AttribDeclaration::toCBuffer(buf, hgs);
+}
+
+char *LinkDeclaration::toChars()
+{
+    return "extern ()";
+}
+
+/********************************* ProtDeclaration ****************************/
+
+ProtDeclaration::ProtDeclaration(enum PROT p, Array *decl)
+	: AttribDeclaration(decl)
+{
+    protection = p;
+    //printf("decl = %p\n", decl);
+}
+
+Dsymbol *ProtDeclaration::syntaxCopy(Dsymbol *s)
+{
+    ProtDeclaration *pd;
+
+    assert(!s);
+    pd = new ProtDeclaration(protection, Dsymbol::arraySyntaxCopy(decl));
+    return pd;
+}
+
+void ProtDeclaration::semantic(Scope *sc)
+{
+    if (decl)
+    {	enum PROT protection_save = sc->protection;
+	int explicitProtection_save = sc->explicitProtection;
+
+	sc->protection = protection;
+	sc->explicitProtection = 1;
+	for (unsigned i = 0; i < decl->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)decl->data[i];
+
+	    s->semantic(sc);
+	}
+	sc->protection = protection_save;
+	sc->explicitProtection = explicitProtection_save;
+    }
+    else
+    {	sc->protection = protection;
+	sc->explicitProtection = 1;
+    }
+}
+
+void ProtDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{   char *p;
+
+    switch (protection)
+    {
+	case PROTprivate:	p = "private";		break;
+	case PROTpackage:	p = "package";		break;
+	case PROTprotected:	p = "protected";	break;
+	case PROTpublic:	p = "public";		break;
+	case PROTexport:	p = "export";		break;
+	default:
+	    assert(0);
+	    break;
+    }
+    buf->writestring(p);
+    AttribDeclaration::toCBuffer(buf, hgs);
+}
+
+/********************************* AlignDeclaration ****************************/
+
+AlignDeclaration::AlignDeclaration(unsigned sa, Array *decl)
+	: AttribDeclaration(decl)
+{
+    salign = sa;
+}
+
+Dsymbol *AlignDeclaration::syntaxCopy(Dsymbol *s)
+{
+    AlignDeclaration *ad;
+
+    assert(!s);
+    ad = new AlignDeclaration(salign, Dsymbol::arraySyntaxCopy(decl));
+    return ad;
+}
+
+void AlignDeclaration::semantic(Scope *sc)
+{
+    //printf("\tAlignDeclaration::semantic '%s'\n",toChars());
+    if (decl)
+    {	unsigned salign_save = sc->structalign;
+
+	sc->structalign = salign;
+	for (unsigned i = 0; i < decl->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)decl->data[i];
+
+	    s->semantic(sc);
+	}
+	sc->structalign = salign_save;
+    }
+    else
+	sc->structalign = salign;
+}
+
+
+void AlignDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    buf->printf("align (%d)", salign);
+    AttribDeclaration::toCBuffer(buf, hgs);
+}
+
+/********************************* AnonDeclaration ****************************/
+
+AnonDeclaration::AnonDeclaration(Loc loc, int isunion, Array *decl)
+	: AttribDeclaration(decl)
+{
+    this->loc = loc;
+    this->isunion = isunion;
+    this->scope = NULL;
+    this->sem = 0;
+}
+
+Dsymbol *AnonDeclaration::syntaxCopy(Dsymbol *s)
+{
+    AnonDeclaration *ad;
+
+    assert(!s);
+    ad = new AnonDeclaration(loc, isunion, Dsymbol::arraySyntaxCopy(decl));
+    return ad;
+}
+
+void AnonDeclaration::semantic(Scope *sc)
+{
+    //printf("\tAnonDeclaration::semantic %s %p\n", isunion ? "union" : "struct", this);
+
+    Scope *scx = NULL;
+    if (scope)
+    {   sc = scope;
+	scx = scope;
+	scope = NULL;
+    }
+
+    assert(sc->parent);
+
+    Dsymbol *parent = sc->parent->pastMixin();
+    AggregateDeclaration *ad = parent->isAggregateDeclaration();
+
+    if (!ad || (!ad->isStructDeclaration() && !ad->isClassDeclaration()))
+    {
+	error("can only be a part of an aggregate");
+	return;
+    }
+
+    if (decl)
+    {
+	AnonymousAggregateDeclaration aad;
+	int adisunion;
+
+	if (sc->anonAgg)
+	{   ad = sc->anonAgg;
+	    adisunion = sc->inunion;
+	}
+	else
+	    adisunion = ad->isUnionDeclaration() != NULL;
+
+//	printf("\tsc->anonAgg = %p\n", sc->anonAgg);
+//	printf("\tad  = %p\n", ad);
+//	printf("\taad = %p\n", &aad);
+
+	sc = sc->push();
+	sc->anonAgg = &aad;
+	sc->stc &= ~(STCauto | STCscope | STCstatic);
+	sc->inunion = isunion;
+	sc->offset = 0;
+	sc->flags = 0;
+	aad.structalign = sc->structalign;
+	aad.parent = ad;
+
+	for (unsigned i = 0; i < decl->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)decl->data[i];
+
+	    s->semantic(sc);
+	    if (isunion)
+		sc->offset = 0;
+	    if (aad.sizeok == 2)
+	    {
+		break;
+	    }
+	}
+	sc = sc->pop();
+
+	// If failed due to forward references, unwind and try again later
+	if (aad.sizeok == 2)
+	{
+	    ad->sizeok = 2;
+	    //printf("\tsetting ad->sizeok %p to 2\n", ad);
+	    if (!sc->anonAgg)
+	    {
+		scope = scx ? scx : new Scope(*sc);
+		scope->setNoFree();
+		scope->module->addDeferredSemantic(this);
+	    }
+	    //printf("\tforward reference %p\n", this);
+	    return;
+	}
+	if (sem == 0)
+	{   Module::dprogress++;
+	    sem = 1;
+	    //printf("\tcompleted %p\n", this);
+	}
+	else
+	    ;//printf("\talready completed %p\n", this);
+
+	// 0 sized structs are set to 1 byte
+	if (aad.structsize == 0)
+	{
+	    aad.structsize = 1;
+	    aad.alignsize = 1;
+	}
+
+	// Align size of anonymous aggregate
+//printf("aad.structalign = %d, aad.alignsize = %d, sc->offset = %d\n", aad.structalign, aad.alignsize, sc->offset);
+	ad->alignmember(aad.structalign, aad.alignsize, &sc->offset);
+	//ad->structsize = sc->offset;
+//printf("sc->offset = %d\n", sc->offset);
+
+	// Add members of aad to ad
+	//printf("\tadding members of aad to '%s'\n", ad->toChars());
+	for (unsigned i = 0; i < aad.fields.dim; i++)
+	{
+	    VarDeclaration *v = (VarDeclaration *)aad.fields.data[i];
+
+	    v->offset += sc->offset;
+	    ad->fields.push(v);
+	}
+
+	// Add size of aad to ad
+	if (adisunion)
+	{
+	    if (aad.structsize > ad->structsize)
+		ad->structsize = aad.structsize;
+	    sc->offset = 0;
+	}
+	else
+	{
+	    ad->structsize = sc->offset + aad.structsize;
+	    sc->offset = ad->structsize;
+	}
+
+	if (ad->alignsize < aad.alignsize)
+	    ad->alignsize = aad.alignsize;
+    }
+}
+
+
+void AnonDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    buf->printf(isunion ? "union" : "struct");
+    buf->writestring("\n{\n");
+    if (decl)
+    {
+	for (unsigned i = 0; i < decl->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)decl->data[i];
+
+	    //buf->writestring("    ");
+	    s->toCBuffer(buf, hgs);
+	}
+    }
+    buf->writestring("}\n");
+}
+
+char *AnonDeclaration::kind()
+{
+    return (char *)(isunion ? "anonymous union" : "anonymous struct");
+}
+
+/********************************* PragmaDeclaration ****************************/
+
+PragmaDeclaration::PragmaDeclaration(Loc loc, Identifier *ident, Expressions *args, Array *decl)
+	: AttribDeclaration(decl)
+{
+    this->loc = loc;
+    this->ident = ident;
+    this->args = args;
+}
+
+Dsymbol *PragmaDeclaration::syntaxCopy(Dsymbol *s)
+{
+    PragmaDeclaration *pd;
+
+    assert(!s);
+    pd = new PragmaDeclaration(loc, ident,
+	Expression::arraySyntaxCopy(args), Dsymbol::arraySyntaxCopy(decl));
+    return pd;
+}
+
+void PragmaDeclaration::semantic(Scope *sc)
+{   // Should be merged with PragmaStatement
+
+#if IN_LLVM
+    int llvm_internal = 0;
+    char* llvm_str1 = NULL;
+
+#endif
+
+    //printf("\tPragmaDeclaration::semantic '%s'\n",toChars());
+    if (ident == Id::msg)
+    {
+	if (args)
+	{
+	    for (size_t i = 0; i < args->dim; i++)
+	    {
+		Expression *e = (Expression *)args->data[i];
+
+		e = e->semantic(sc);
+		e = e->optimize(WANTvalue | WANTinterpret);
+		if (e->op == TOKstring)
+		{
+		    StringExp *se = (StringExp *)e;
+		    fprintf(stdmsg, "%.*s", (int)se->len, se->string);
+		}
+		else
+		    error("string expected for message, not '%s'", e->toChars());
+	    }
+	    fprintf(stdmsg, "\n");
+	}
+	goto Lnodecl;
+    }
+    else if (ident == Id::lib)
+    {
+	if (!args || args->dim != 1)
+	    error("string expected for library name");
+	else
+	{
+	    Expression *e = (Expression *)args->data[0];
+
+	    e = e->semantic(sc);
+	    e = e->optimize(WANTvalue | WANTinterpret);
+	    args->data[0] = (void *)e;
+	    if (e->op != TOKstring)
+		error("string expected for library name, not '%s'", e->toChars());
+	    else if (global.params.verbose)
+	    {
+		StringExp *se = (StringExp *)e;
+		char *name = (char *)mem.malloc(se->len + 1);
+		memcpy(name, se->string, se->len);
+		name[se->len] = 0;
+		printf("library   %s\n", name);
+		mem.free(name);
+	    }
+	}
+	goto Lnodecl;
+    }
+#if IN_GCC
+    else if (ident == Id::GNU_asm)
+    {
+	if (! args || args->dim != 2)
+	    error("identifier and string expected for asm name");
+	else
+	{
+	    Expression *e;
+	    Declaration *d = NULL;
+	    StringExp *s = NULL;
+
+	    e = (Expression *)args->data[0];
+	    e = e->semantic(sc);
+	    if (e->op == TOKvar)
+	    {
+		d = ((VarExp *)e)->var;
+		if (! d->isFuncDeclaration() && ! d->isVarDeclaration())
+		    d = NULL;
+	    }
+	    if (!d)
+		error("first argument of GNU_asm must be a function or variable declaration");
+
+	    e = (Expression *)args->data[1];
+	    e = e->semantic(sc);
+	    e = e->optimize(WANTvalue);
+	    if (e->op == TOKstring && ((StringExp *)e)->sz == 1)
+		s = ((StringExp *)e);
+	    else
+		error("second argument of GNU_asm must be a char string");
+
+	    if (d && s)
+		d->c_ident = Lexer::idPool((char*) s->string);
+	}
+	goto Lnodecl;
+    }
+#endif
+#if IN_LLVM
+    else if (ident == Id::LLVM_internal)
+    {
+        if (!args || args->dim < 1 || args->dim > 2)
+            error("needs 1-3 parameters");
+        else if (!decl || decl->dim < 1)
+            error("must apply to at least one declaration");
+        else
+        {
+            Expression *e;
+            StringExp *s = NULL;
+
+            e = (Expression *)args->data[0];
+            e = e->semantic(sc);
+            e = e->optimize(WANTvalue);
+            if (e->op == TOKstring && (s = (StringExp *)e))
+            {
+                char* str = (char*)s->string;
+                if (strcmp(str,"intrinsic")==0) {
+                    llvm_internal = LLVMintrinsic;
+                    assert(args->dim == 2);
+                }
+                else if (strcmp(str,"va_start")==0) {
+                    llvm_internal = LLVMva_start;
+                    assert(args->dim == 1);
+                }
+                else if (strcmp(str,"va_arg")==0) {
+                    llvm_internal = LLVMva_arg;
+                    assert(args->dim == 1);
+                }
+                else if (strcmp(str,"va_intrinsic")==0) {
+                    llvm_internal = LLVMva_intrinsic;
+                    assert(args->dim == 2);
+                }
+                else if (strcmp(str,"notypeinfo")==0) {
+                    llvm_internal = LLVMnotypeinfo;
+                    assert(args->dim == 1);
+                }
+                else if (strcmp(str,"alloca")==0) {
+                    llvm_internal = LLVMalloca;
+                    assert(args->dim == 1);
+                }
+                else {
+                    error("unknown pragma command: %s", str);
+                }
+            }
+            else
+            error("1st argument must be a string");
+
+            if (llvm_internal)
+            switch (llvm_internal)
+            {
+            case LLVMintrinsic:
+            case LLVMva_intrinsic:
+                e = (Expression *)args->data[1];
+                e = e->semantic(sc);
+                e = e->optimize(WANTvalue);
+                if (e->op == TOKstring && (s = (StringExp *)e)) {
+                    llvm_str1 = (char*)s->string;
+                }
+                else
+                error("2nd argument must be a string");
+                break;
+
+            case LLVMva_arg:
+            case LLVMva_start:
+            case LLVMnotypeinfo:
+            case LLVMalloca:
+                break;
+
+            default:
+                assert(0);
+            }
+        }
+    }
+#endif
+    else if (global.params.ignoreUnsupportedPragmas)
+    {
+	if (global.params.verbose)
+	{
+	    /* Print unrecognized pragmas
+	     */
+	    printf("pragma    %s", ident->toChars());
+	    if (args)
+	    {
+		for (size_t i = 0; i < args->dim; i++)
+		{
+		    Expression *e = (Expression *)args->data[i];
+		    e = e->semantic(sc);
+		    e = e->optimize(WANTvalue | WANTinterpret);
+		    if (i == 0)
+			printf(" (");
+		    else
+			printf(",");
+		    printf("%s", e->toChars());
+		}
+		if (args->dim)
+		    printf(")");
+	    }
+	    printf("\n");
+	}
+	goto Lnodecl;
+    }
+    else
+	error("unrecognized pragma(%s)", ident->toChars());
+
+    if (decl)
+    {
+	for (unsigned i = 0; i < decl->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)decl->data[i];
+
+	    s->semantic(sc);
+        
+#if IN_LLVM
+        if (llvm_internal)
+        {
+            switch(llvm_internal)
+            {
+            case LLVMintrinsic:
+            case LLVMva_intrinsic:
+                if (FuncDeclaration* fd = s->isFuncDeclaration()) {
+                    fd->llvmInternal = llvm_internal;
+                    fd->llvmInternal1 = llvm_str1;
+                }
+                else {
+                    error("may only be used on function declarations");
+                    assert(0);
+                }
+                break;
+
+            case LLVMva_start:
+            case LLVMva_arg:
+                if (TemplateDeclaration* td = s->isTemplateDeclaration()) {
+                    td->llvmInternal = llvm_internal;
+                    assert(td->parameters->dim == 1);
+                    assert(!td->overnext);
+                    assert(!td->overroot);
+                    assert(td->onemember);
+                    Logger::println("template->onemember = %s", td->onemember->toChars());
+                }
+                else {
+                    error("can only be used on templates");
+                    assert(0);
+                }
+                break;
+
+            case LLVMnotypeinfo:
+                s->llvmInternal = llvm_internal;
+                break;
+
+            case LLVMalloca:
+                if (FuncDeclaration* fd = s->isFuncDeclaration()) {
+                    fd->llvmInternal = llvm_internal;
+                }
+                else {
+                    error("may only be used on function declarations");
+                    assert(0);
+                }
+                break;
+
+            default:
+                assert(0 && "invalid LLVM_internal pragma got through :/");
+            }
+        }
+        
+#endif
+    }
+    }
+    return;
+
+Lnodecl:
+    if (decl)
+	error("pragma is missing closing ';'");
+}
+
+int PragmaDeclaration::oneMember(Dsymbol **ps)
+{
+    *ps = NULL;
+    return TRUE;
+}
+
+char *PragmaDeclaration::kind()
+{
+    return "pragma";
+}
+
+void PragmaDeclaration::toObjFile()
+{
+    if (ident == Id::lib)
+    {
+	assert(args && args->dim == 1);
+
+	Expression *e = (Expression *)args->data[0];
+
+	assert(e->op == TOKstring);
+
+	StringExp *se = (StringExp *)e;
+	char *name = (char *)mem.malloc(se->len + 1);
+	memcpy(name, se->string, se->len);
+	name[se->len] = 0;
+	obj_includelib(name);
+    }
+    AttribDeclaration::toObjFile();
+}
+
+void PragmaDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    buf->printf("pragma(%s", ident->toChars());
+    if (args)
+    {
+	for (size_t i = 0; i < args->dim; i++)
+	{
+	    Expression *e = (Expression *)args->data[i];
+
+	    buf->writestring(", ");
+	    e->toCBuffer(buf, hgs);
+	}
+    }
+    buf->writestring(")");
+    AttribDeclaration::toCBuffer(buf, hgs);
+}
+
+
+/********************************* ConditionalDeclaration ****************************/
+
+ConditionalDeclaration::ConditionalDeclaration(Condition *condition, Array *decl, Array *elsedecl)
+	: AttribDeclaration(decl)
+{
+    //printf("ConditionalDeclaration::ConditionalDeclaration()\n");
+    this->condition = condition;
+    this->elsedecl = elsedecl;
+}
+
+Dsymbol *ConditionalDeclaration::syntaxCopy(Dsymbol *s)
+{
+    ConditionalDeclaration *dd;
+
+    assert(!s);
+    dd = new ConditionalDeclaration(condition->syntaxCopy(),
+	Dsymbol::arraySyntaxCopy(decl),
+	Dsymbol::arraySyntaxCopy(elsedecl));
+    return dd;
+}
+
+
+int ConditionalDeclaration::oneMember(Dsymbol **ps)
+{
+    //printf("ConditionalDeclaration::oneMember(), inc = %d\n", condition->inc);
+    if (condition->inc)
+    {
+	Array *d = condition->include(NULL, NULL) ? decl : elsedecl;
+	return Dsymbol::oneMembers(d, ps);
+    }
+    *ps = NULL;
+    return TRUE;
+}
+
+void ConditionalDeclaration::emitComment(Scope *sc)
+{
+    //printf("ConditionalDeclaration::emitComment(sc = %p)\n", sc);
+    if (condition->inc)
+    {
+	AttribDeclaration::emitComment(sc);
+    }
+}
+
+// Decide if 'then' or 'else' code should be included
+
+Array *ConditionalDeclaration::include(Scope *sc, ScopeDsymbol *sd)
+{
+    //printf("ConditionalDeclaration::include()\n");
+    assert(condition);
+    return condition->include(sc, sd) ? decl : elsedecl;
+}
+
+
+void ConditionalDeclaration::addComment(unsigned char *comment)
+{
+    /* Because addComment is called by the parser, if we called
+     * include() it would define a version before it was used.
+     * But it's no problem to drill down to both decl and elsedecl,
+     * so that's the workaround.
+     */
+
+    if (comment)
+    {
+	Array *d = decl;
+
+	for (int j = 0; j < 2; j++)
+	{
+	    if (d)
+	    {
+		for (unsigned i = 0; i < d->dim; i++)
+		{   Dsymbol *s;
+
+		    s = (Dsymbol *)d->data[i];
+		    //printf("ConditionalDeclaration::addComment %s\n", s->toChars());
+		    s->addComment(comment);
+		}
+	    }
+	    d = elsedecl;
+	}
+    }
+}
+
+void ConditionalDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    condition->toCBuffer(buf, hgs);
+    if (decl || elsedecl)
+    {
+	buf->writenl();
+	buf->writeByte('{');
+	buf->writenl();
+	if (decl)
+	{
+	    for (unsigned i = 0; i < decl->dim; i++)
+	    {
+		Dsymbol *s = (Dsymbol *)decl->data[i];
+
+		buf->writestring("    ");
+		s->toCBuffer(buf, hgs);
+	    }
+	}
+	buf->writeByte('}');
+	if (elsedecl)
+	{
+	    buf->writenl();
+	    buf->writestring("else");
+	    buf->writenl();
+	    buf->writeByte('{');
+	    buf->writenl();
+	    for (unsigned i = 0; i < elsedecl->dim; i++)
+	    {
+		Dsymbol *s = (Dsymbol *)elsedecl->data[i];
+
+		buf->writestring("    ");
+		s->toCBuffer(buf, hgs);
+	    }
+	    buf->writeByte('}');
+	}
+    }
+    else
+	buf->writeByte(':');
+    buf->writenl();
+}
+
+/***************************** StaticIfDeclaration ****************************/
+
+StaticIfDeclaration::StaticIfDeclaration(Condition *condition,
+	Array *decl, Array *elsedecl)
+	: ConditionalDeclaration(condition, decl, elsedecl)
+{
+    //printf("StaticIfDeclaration::StaticIfDeclaration()\n");
+    sd = NULL;
+    addisdone = 0;
+}
+
+
+Dsymbol *StaticIfDeclaration::syntaxCopy(Dsymbol *s)
+{
+    StaticIfDeclaration *dd;
+
+    assert(!s);
+    dd = new StaticIfDeclaration(condition->syntaxCopy(),
+	Dsymbol::arraySyntaxCopy(decl),
+	Dsymbol::arraySyntaxCopy(elsedecl));
+    return dd;
+}
+
+
+int StaticIfDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
+{
+    /* This is deferred until semantic(), so that
+     * expressions in the condition can refer to declarations
+     * in the same scope, such as:
+     *
+     * template Foo(int i)
+     * {
+     *     const int j = i + 1;
+     *     static if (j == 3)
+     *         const int k;
+     * }
+     */
+    this->sd = sd;
+    int m = 0;
+
+    if (memnum == 0)
+    {	m = AttribDeclaration::addMember(sc, sd, memnum);
+	addisdone = 1;
+    }
+    return m;
+}
+
+
+void StaticIfDeclaration::semantic(Scope *sc)
+{
+    Array *d = include(sc, sd);
+
+    //printf("\tStaticIfDeclaration::semantic '%s'\n",toChars());
+    if (d)
+    {
+	if (!addisdone)
+	{   AttribDeclaration::addMember(sc, sd, 1);
+	    addisdone = 1;
+	}
+
+	for (unsigned i = 0; i < d->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)d->data[i];
+
+	    s->semantic(sc);
+	}
+    }
+}
+
+char *StaticIfDeclaration::kind()
+{
+    return "static if";
+}
+
+
+/***************************** CompileDeclaration *****************************/
+
+CompileDeclaration::CompileDeclaration(Loc loc, Expression *exp)
+    : AttribDeclaration(NULL)
+{
+    this->exp = exp;
+    this->sd = NULL;
+}
+
+Dsymbol *CompileDeclaration::syntaxCopy(Dsymbol *s)
+{
+    //printf("CompileDeclaration::syntaxCopy('%s')\n", toChars());
+    CompileDeclaration *sc = new CompileDeclaration(loc, exp->syntaxCopy());
+    return sc;
+}
+
+int CompileDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
+{
+    this->sd = sd;
+    return memnum;
+}
+
+void CompileDeclaration::semantic(Scope *sc)
+{
+    //printf("CompileDeclaration::semantic()\n");
+    exp = exp->semantic(sc);
+    exp = resolveProperties(sc, exp);
+    exp = exp->optimize(WANTvalue | WANTinterpret);
+    if (exp->op != TOKstring)
+    {	error("argument to mixin must be a string, not (%s)", exp->toChars());
+	return;
+    }
+    StringExp *se = (StringExp *)exp;
+    se = se->toUTF8(sc);
+    Parser p(sc->module, (unsigned char *)se->string, se->len, 0);
+    p.loc = loc;
+    p.nextToken();
+    decl = p.parseDeclDefs(0);
+    if (p.token.value != TOKeof)
+    {
+	error("incomplete mixin declaration (%s)", se->toChars());
+    }
+
+    AttribDeclaration::addMember(sc, sd, 0);
+    AttribDeclaration::semantic(sc);
+}
+
+void CompileDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    buf->writestring("mixin(");
+    exp->toCBuffer(buf, hgs);
+    buf->writestring(");");
+    buf->writenl();
+}
--- a/dmd/cast.c	Thu May 01 13:33:02 2008 +0200
+++ b/dmd/cast.c	Thu May 01 15:15:28 2008 +0200
@@ -1,1454 +1,1455 @@
-
-// Copyright (c) 1999-2006 by Digital Mars
-// All Rights Reserved
-// written by Walter Bright
-// http://www.digitalmars.com
-// License for redistribution is by either the Artistic License
-// in artistic.txt, or the GNU General Public License in gnu.txt.
-// See the included readme.txt for details.
-
-#include <stdio.h>
-#include <assert.h>
-
-#if _WIN32 || IN_GCC || IN_LLVM
-#include "mem.h"
-#else
-#include "../root/mem.h"
-#endif
-
-#include "expression.h"
-#include "mtype.h"
-#include "utf.h"
-#include "declaration.h"
-#include "aggregate.h"
-
-/* ==================== implicitCast ====================== */
-
-/**************************************
- * Do an implicit cast.
- * Issue error if it can't be done.
- */
-
-Expression *Expression::implicitCastTo(Scope *sc, Type *t)
-{
-    //printf("implicitCastTo(%s) => %s\n", type->toChars(), t->toChars());
-    if (implicitConvTo(t))
-    {
-	if (global.params.warnings &&
-	    Type::impcnvWarn[type->toBasetype()->ty][t->toBasetype()->ty] &&
-	    op != TOKint64)
-	{
-	    Expression *e = optimize(WANTflags | WANTvalue);
-
-	    if (e->op == TOKint64)
-		return e->implicitCastTo(sc, t);
-
-	    fprintf(stdmsg, "warning - ");
-	    error("implicit conversion of expression (%s) of type %s to %s can cause loss of data",
-		toChars(), type->toChars(), t->toChars());
-	}
-	return castTo(sc, t);
-    }
-
-    Expression *e = optimize(WANTflags | WANTvalue);
-    if (e != this)
-	return e->implicitCastTo(sc, t);
-
-#if 0
-print();
-type->print();
-printf("to:\n");
-t->print();
-printf("%p %p type: %s to: %s\n", type->deco, t->deco, type->deco, t->deco);
-//printf("%p %p %p\n", type->next->arrayOf(), type, t);
-fflush(stdout);
-#endif
-    if (!t->deco)
-    {	/* Can happen with:
-	 *    enum E { One }
-	 *    class A
-	 *    { static void fork(EDG dg) { dg(E.One); }
-	 *	alias void delegate(E) EDG;
-	 *    }
-	 * Should eventually make it work.
-	 */
-	error("forward reference to type %s", t->toChars());
-    }
-    else if (t->reliesOnTident())
-	error("forward reference to type %s", t->reliesOnTident()->toChars());
-
-    error("cannot implicitly convert expression (%s) of type %s to %s",
-	toChars(), type->toChars(), t->toChars());
-    return castTo(sc, t);
-}
-
-/*******************************************
- * Return !=0 if we can implicitly convert this to type t.
- * Don't do the actual cast.
- */
-
-MATCH Expression::implicitConvTo(Type *t)
-{
-#if 0
-    printf("Expression::implicitConvTo(this=%s, type=%s, t=%s)\n",
-	toChars(), type->toChars(), t->toChars());
-#endif
-    if (!type)
-    {	error("%s is not an expression", toChars());
-	type = Type::terror;
-    }
-    if (t->ty == Tbit && isBit())
-	return MATCHconvert;
-    Expression *e = optimize(WANTvalue | WANTflags);
-    if (e != this)
-    {	//printf("optimzed to %s\n", e->toChars());
-	return e->implicitConvTo(t);
-    }
-    MATCH match = type->implicitConvTo(t);
-    if (match)
-	return match;
-#if 0
-    Type *tb = t->toBasetype();
-    if (tb->ty == Tdelegate)
-    {	TypeDelegate *td = (TypeDelegate *)tb;
-	TypeFunction *tf = (TypeFunction *)td->next;
-
-	if (!tf->varargs &&
-	    !(tf->arguments && tf->arguments->dim)
-	   )
-	{
-	    match = type->implicitConvTo(tf->next);
-	    if (match)
-		return match;
-	    if (tf->next->toBasetype()->ty == Tvoid)
-		return MATCHconvert;
-	}
-    }
-#endif
-    return MATCHnomatch;
-}
-
-
-MATCH IntegerExp::implicitConvTo(Type *t)
-{
-#if 0
-    printf("IntegerExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
-	toChars(), type->toChars(), t->toChars());
-#endif
-    if (type->equals(t))
-	return MATCHexact;
-
-    enum TY ty = type->toBasetype()->ty;
-    enum TY toty = t->toBasetype()->ty;
-
-    if (type->implicitConvTo(t) == MATCHnomatch && t->ty == Tenum)
-    {
-	return MATCHnomatch;
-    }
-
-    switch (ty)
-    {
-	case Tbit:
-	case Tbool:
-	    value &= 1;
-	    ty = Tint32;
-	    break;
-
-	case Tint8:
-	    value = (signed char)value;
-	    ty = Tint32;
-	    break;
-
-	case Tchar:
-	case Tuns8:
-	    value &= 0xFF;
-	    ty = Tint32;
-	    break;
-
-	case Tint16:
-	    value = (short)value;
-	    ty = Tint32;
-	    break;
-
-	case Tuns16:
-	case Twchar:
-	    value &= 0xFFFF;
-	    ty = Tint32;
-	    break;
-
-	case Tint32:
-	    value = (int)value;
-	    break;
-
-	case Tuns32:
-	case Tdchar:
-	    value &= 0xFFFFFFFF;
-	    ty = Tuns32;
-	    break;
-
-	default:
-	    break;
-    }
-
-    // Only allow conversion if no change in value
-    switch (toty)
-    {
-	case Tbit:
-	case Tbool:
-	    if ((value & 1) != value)
-		goto Lno;
-	    goto Lyes;
-
-	case Tint8:
-	    if ((signed char)value != value)
-		goto Lno;
-	    goto Lyes;
-
-	case Tchar:
-	case Tuns8:
-	    //printf("value = %llu %llu\n", (integer_t)(unsigned char)value, value);
-	    if ((unsigned char)value != value)
-		goto Lno;
-	    goto Lyes;
-
-	case Tint16:
-	    if ((short)value != value)
-		goto Lno;
-	    goto Lyes;
-
-	case Tuns16:
-	    if ((unsigned short)value != value)
-		goto Lno;
-	    goto Lyes;
-
-	case Tint32:
-	    if (ty == Tuns32)
-	    {
-	    }
-	    else if ((int)value != value)
-		goto Lno;
-	    goto Lyes;
-
-	case Tuns32:
-	    if (ty == Tint32)
-	    {
-	    }
-	    else if ((unsigned)value != value)
-		goto Lno;
-	    goto Lyes;
-
-	case Tdchar:
-	    if (value > 0x10FFFFUL)
-		goto Lno;
-	    goto Lyes;
-
-	case Twchar:
-	    if ((unsigned short)value != value)
-		goto Lno;
-	    goto Lyes;
-
-	case Tfloat32:
-	{
-	    volatile float f;
-	    if (type->isunsigned())
-	    {
-		f = (float)value;
-		if (f != value)
-		    goto Lno;
-	    }
-	    else
-	    {
-		f = (float)(long long)value;
-		if (f != (long long)value)
-		    goto Lno;
-	    }
-	    goto Lyes;
-	}
-
-	case Tfloat64:
-	{
-	    volatile double f;
-	    if (type->isunsigned())
-	    {
-		f = (double)value;
-		if (f != value)
-		    goto Lno;
-	    }
-	    else
-	    {
-		f = (double)(long long)value;
-		if (f != (long long)value)
-		    goto Lno;
-	    }
-	    goto Lyes;
-	}
-
-	case Tfloat80:
-	{
-	    volatile long double f;
-	    if (type->isunsigned())
-	    {
-		f = (long double)value;
-		if (f != value)
-		    goto Lno;
-	    }
-	    else
-	    {
-		f = (long double)(long long)value;
-		if (f != (long long)value)
-		    goto Lno;
-	    }
-	    goto Lyes;
-	}
-    }
-    return Expression::implicitConvTo(t);
-
-Lyes:
-    //printf("MATCHconvert\n");
-    return MATCHconvert;
-
-Lno:
-    //printf("MATCHnomatch\n");
-    return MATCHnomatch;
-}
-
-MATCH NullExp::implicitConvTo(Type *t)
-{
-#if 0
-    printf("NullExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
-	toChars(), type->toChars(), t->toChars());
-#endif
-    if (this->type->equals(t))
-	return MATCHexact;
-    // NULL implicitly converts to any pointer type or dynamic array
-    if (type->ty == Tpointer && type->next->ty == Tvoid)
-    {
-	if (t->ty == Ttypedef)
-	    t = ((TypeTypedef *)t)->sym->basetype;
-	if (t->ty == Tpointer || t->ty == Tarray ||
-	    t->ty == Taarray  || t->ty == Tclass ||
-	    t->ty == Tdelegate)
-	    return committed ? MATCHconvert : MATCHexact;
-    }
-    return Expression::implicitConvTo(t);
-}
-
-MATCH StringExp::implicitConvTo(Type *t)
-{   MATCH m;
-
-#if 0
-    printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n",
-	toChars(), committed, type->toChars(), t->toChars());
-#endif
-    if (!committed)
-    {
-    if (!committed && t->ty == Tpointer && t->next->ty == Tvoid)
-    {
-	return MATCHnomatch;
-    }
-    if (type->ty == Tsarray || type->ty == Tarray || type->ty == Tpointer)
-    {
-	if (type->next->ty == Tchar)
-	{
-	    switch (t->ty)
-	    {
-		case Tsarray:
-		    if (type->ty == Tsarray &&
-			((TypeSArray *)type)->dim->toInteger() !=
-			((TypeSArray *)t)->dim->toInteger())
-			return MATCHnomatch;
-		    goto L1;
-		case Tarray:
-		    goto L1;
-		case Tpointer:
-		L1:
-		    if (t->next->ty == Tchar)
-			return MATCHexact;
-		    else if (t->next->ty == Twchar)
-			return MATCHexact;
-		    else if (t->next->ty == Tdchar)
-			return MATCHexact;
-		    break;
-	    }
-	}
-    }
-    }
-    return Expression::implicitConvTo(t);
-#if 0
-    m = (MATCH)type->implicitConvTo(t);
-    if (m)
-    {
-	return m;
-    }
-
-    return MATCHnomatch;
-#endif
-}
-
-MATCH ArrayLiteralExp::implicitConvTo(Type *t)
-{   MATCH result = MATCHexact;
-
-    Type *typeb = type->toBasetype();
-    Type *tb = t->toBasetype();
-    if ((tb->ty == Tarray || tb->ty == Tsarray) &&
-	(typeb->ty == Tarray || typeb->ty == Tsarray))
-    {
-	if (tb->ty == Tsarray)
-	{   TypeSArray *tsa = (TypeSArray *)tb;
-	    if (elements->dim != tsa->dim->toInteger())
-		result = MATCHnomatch;
-	}
-
-	for (int i = 0; i < elements->dim; i++)
-	{   Expression *e = (Expression *)elements->data[i];
-	    MATCH m = (MATCH)e->implicitConvTo(tb->next);
-	    if (m < result)
-		result = m;			// remember worst match
-	    if (result == MATCHnomatch)
-		break;				// no need to check for worse
-	}
-	return result;
-    }
-    else
-	return Expression::implicitConvTo(t);
-}
-
-MATCH AssocArrayLiteralExp::implicitConvTo(Type *t)
-{   MATCH result = MATCHexact;
-
-    Type *typeb = type->toBasetype();
-    Type *tb = t->toBasetype();
-    if (tb->ty == Taarray && typeb->ty == Taarray)
-    {
-	for (size_t i = 0; i < keys->dim; i++)
-	{   Expression *e = (Expression *)keys->data[i];
-	    MATCH m = (MATCH)e->implicitConvTo(((TypeAArray *)tb)->key);
-	    if (m < result)
-		result = m;			// remember worst match
-	    if (result == MATCHnomatch)
-		break;				// no need to check for worse
-	    e = (Expression *)values->data[i];
-	    m = (MATCH)e->implicitConvTo(tb->next);
-	    if (m < result)
-		result = m;			// remember worst match
-	    if (result == MATCHnomatch)
-		break;				// no need to check for worse
-	}
-	return result;
-    }
-    else
-	return Expression::implicitConvTo(t);
-}
-
-MATCH AddrExp::implicitConvTo(Type *t)
-{
-#if 0
-    printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
-	toChars(), type->toChars(), t->toChars());
-#endif
-    MATCH result;
-
-    result = type->implicitConvTo(t);
-    //printf("\tresult = %d\n", result);
-
-    if (result == MATCHnomatch)
-    {
-	// Look for pointers to functions where the functions are overloaded.
-	VarExp *ve;
-	FuncDeclaration *f;
-
-	t = t->toBasetype();
-	if (type->ty == Tpointer && type->next->ty == Tfunction &&
-	    t->ty == Tpointer && t->next->ty == Tfunction &&
-	    e1->op == TOKvar)
-	{
-	    ve = (VarExp *)e1;
-	    f = ve->var->isFuncDeclaration();
-	    if (f && f->overloadExactMatch(t->next))
-		result = MATCHexact;
-	}
-    }
-    //printf("\tresult = %d\n", result);
-    return result;
-}
-
-MATCH SymOffExp::implicitConvTo(Type *t)
-{
-#if 0
-    printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
-	toChars(), type->toChars(), t->toChars());
-#endif
-    MATCH result;
-
-    result = type->implicitConvTo(t);
-    //printf("\tresult = %d\n", result);
-
-    if (result == MATCHnomatch)
-    {
-	// Look for pointers to functions where the functions are overloaded.
-	FuncDeclaration *f;
-
-	t = t->toBasetype();
-	if (type->ty == Tpointer && type->next->ty == Tfunction &&
-	    t->ty == Tpointer && t->next->ty == Tfunction)
-	{
-	    f = var->isFuncDeclaration();
-	    if (f && f->overloadExactMatch(t->next))
-		result = MATCHexact;
-	}
-    }
-    //printf("\tresult = %d\n", result);
-    return result;
-}
-
-MATCH DelegateExp::implicitConvTo(Type *t)
-{
-#if 0
-    printf("DelegateExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
-	toChars(), type->toChars(), t->toChars());
-#endif
-    MATCH result;
-
-    result = type->implicitConvTo(t);
-
-    if (result == 0)
-    {
-	// Look for pointers to functions where the functions are overloaded.
-	FuncDeclaration *f;
-
-	t = t->toBasetype();
-	if (type->ty == Tdelegate && type->next->ty == Tfunction &&
-	    t->ty == Tdelegate && t->next->ty == Tfunction)
-	{
-	    if (func && func->overloadExactMatch(t->next))
-		result = MATCHexact;
-	}
-    }
-    return result;
-}
-
-MATCH CondExp::implicitConvTo(Type *t)
-{
-    MATCH m1;
-    MATCH m2;
-
-    m1 = e1->implicitConvTo(t);
-    m2 = e2->implicitConvTo(t);
-
-    // Pick the worst match
-    return (m1 < m2) ? m1 : m2;
-}
-
-
-/* ==================== castTo ====================== */
-
-/**************************************
- * Do an explicit cast.
- */
-
-Expression *Expression::castTo(Scope *sc, Type *t)
-{
-    //printf("Expression::castTo(this=%s, t=%s)\n", toChars(), t->toChars());
-#if 0
-    printf("Expression::castTo(this=%s, type=%s, t=%s)\n",
-	toChars(), type->toChars(), t->toChars());
-#endif
-    if (type == t)
-	return this;
-    Expression *e = this;
-    Type *tb = t->toBasetype();
-    Type *typeb = type->toBasetype();
-    if (tb != typeb)
-    {
-	// Do (type *) cast of (type [dim])
-	if (tb->ty == Tpointer &&
-	    typeb->ty == Tsarray
-	   )
-	{
-	    //printf("Converting [dim] to *\n");
-
-	    if (typeb->size(loc) == 0)
-		e = new NullExp(loc);
-	    else
-		e = new AddrExp(loc, e);
-	}
-#if 0
-	else if (tb->ty == Tdelegate && type->ty != Tdelegate)
-	{
-	    TypeDelegate *td = (TypeDelegate *)tb;
-	    TypeFunction *tf = (TypeFunction *)td->nextOf();
-	    return toDelegate(sc, tf->nextOf());
-	}
-#endif
-	else
-	{
-	    e = new CastExp(loc, e, tb);
-	}
-    }
-    else
-    {
-	e = e->copy();	// because of COW for assignment to e->type
-    }
-    assert(e != this);
-    e->type = t;
-    //printf("Returning: %s\n", e->toChars());
-    return e;
-}
-
-
-Expression *RealExp::castTo(Scope *sc, Type *t)
-{   Expression *e = this;
-    if (type != t)
-    {
-	if ((type->isreal() && t->isreal()) ||
-	    (type->isimaginary() && t->isimaginary())
-	   )
-	{   e = copy();
-	    e->type = t;
-	}
-	else
-	    e = Expression::castTo(sc, t);
-    }
-    return e;
-}
-
-
-Expression *ComplexExp::castTo(Scope *sc, Type *t)
-{   Expression *e = this;
-    if (type != t)
-    {
-	if (type->iscomplex() && t->iscomplex())
-	{   e = copy();
-	    e->type = t;
-	}
-	else
-	    e = Expression::castTo(sc, t);
-    }
-    return e;
-}
-
-
-Expression *NullExp::castTo(Scope *sc, Type *t)
-{   NullExp *e;
-    Type *tb;
-
-    //printf("NullExp::castTo(t = %p)\n", t);
-    if (type == t)
-    {
-	committed = 1;
-	return this;
-    }
-    e = (NullExp *)copy();
-    e->committed = 1;
-    tb = t->toBasetype();
-    e->type = type->toBasetype();
-    if (tb != e->type)
-    {
-	// NULL implicitly converts to any pointer type or dynamic array
-	if (e->type->ty == Tpointer && e->type->nextOf()->ty == Tvoid &&
-	    (tb->ty == Tpointer || tb->ty == Tarray || tb->ty == Taarray ||
-	     tb->ty == Tdelegate))
-	{
-#if 0
-	    if (tb->ty == Tdelegate)
-	    {   TypeDelegate *td = (TypeDelegate *)tb;
-		TypeFunction *tf = (TypeFunction *)td->nextOf();
-
-		if (!tf->varargs &&
-		    !(tf->arguments && tf->arguments->dim)
-		   )
-		{
-		    return Expression::castTo(sc, t);
-		}
-	    }
-#endif
-	}
-	else
-	{
-	    return e->Expression::castTo(sc, t);
-	}
-    }
-    e->type = t;
-    return e;
-}
-
-Expression *StringExp::castTo(Scope *sc, Type *t)
-{
-    /* This follows copy-on-write; any changes to 'this'
-     * will result in a copy.
-     * The this->string member is considered immutable.
-     */
-    StringExp *se;
-    Type *tb;
-    int copied = 0;
-
-    //printf("StringExp::castTo(t = %s), '%s' committed = %d\n", t->toChars(), toChars(), committed);
-
-    if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid)
-    {
-	error("cannot convert string literal to void*");
-    }
-
-    se = this;
-    if (!committed)
-    {   se = (StringExp *)copy();
-	se->committed = 1;
-	copied = 1;
-    }
-
-    if (type == t)
-    {
-	return se;
-    }
-
-    tb = t->toBasetype();
-    //printf("\ttype = %s\n", type->toChars());
-    if (tb->ty == Tdelegate && type->toBasetype()->ty != Tdelegate)
-	return Expression::castTo(sc, t);
-
-    Type *typeb = type->toBasetype();
-    if (typeb == tb)
-    {
-	if (!copied)
-	{   se = (StringExp *)copy();
-	    copied = 1;
-	}
-	se->type = t;
-	return se;
-    }
-
-    if (tb->ty != Tsarray && tb->ty != Tarray && tb->ty != Tpointer)
-    {	if (!copied)
-	{   se = (StringExp *)copy();
-	    copied = 1;
-	}
-	goto Lcast;
-    }
-    if (typeb->ty != Tsarray && typeb->ty != Tarray && typeb->ty != Tpointer)
-    {	if (!copied)
-	{   se = (StringExp *)copy();
-	    copied = 1;
-	}
-	goto Lcast;
-    }
-
-    if (typeb->nextOf()->size() == tb->nextOf()->size())
-    {
-	if (!copied)
-	{   se = (StringExp *)copy();
-	    copied = 1;
-	}
-	if (tb->ty == Tsarray)
-	    goto L2;	// handle possible change in static array dimension
-	se->type = t;
-	return se;
-    }
-
-    if (committed)
-	goto Lcast;
-
-#define X(tf,tt)	((tf) * 256 + (tt))
-    {
-    OutBuffer buffer;
-    size_t newlen = 0;
-    int tfty = typeb->nextOf()->toBasetype()->ty;
-    int ttty = tb->nextOf()->toBasetype()->ty;
-    switch (X(tfty, ttty))
-    {
-	case X(Tchar, Tchar):
-	case X(Twchar,Twchar):
-	case X(Tdchar,Tdchar):
-	    break;
-
-	case X(Tchar, Twchar):
-	    for (size_t u = 0; u < len;)
-	    {	unsigned c;
-		char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
-		if (p)
-		    error("%s", p);
-		else
-		    buffer.writeUTF16(c);
-	    }
-	    newlen = buffer.offset / 2;
-	    buffer.writeUTF16(0);
-	    goto L1;
-
-	case X(Tchar, Tdchar):
-	    for (size_t u = 0; u < len;)
-	    {	unsigned c;
-		char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
-		if (p)
-		    error("%s", p);
-		buffer.write4(c);
-		newlen++;
-	    }
-	    buffer.write4(0);
-	    goto L1;
-
-	case X(Twchar,Tchar):
-	    for (size_t u = 0; u < len;)
-	    {	unsigned c;
-		char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
-		if (p)
-		    error("%s", p);
-		else
-		    buffer.writeUTF8(c);
-	    }
-	    newlen = buffer.offset;
-	    buffer.writeUTF8(0);
-	    goto L1;
-
-	case X(Twchar,Tdchar):
-	    for (size_t u = 0; u < len;)
-	    {	unsigned c;
-		char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
-		if (p)
-		    error("%s", p);
-		buffer.write4(c);
-		newlen++;
-	    }
-	    buffer.write4(0);
-	    goto L1;
-
-	case X(Tdchar,Tchar):
-	    for (size_t u = 0; u < len; u++)
-	    {
-		unsigned c = ((unsigned *)se->string)[u];
-		if (!utf_isValidDchar(c))
-		    error("invalid UCS-32 char \\U%08x", c);
-		else
-		    buffer.writeUTF8(c);
-		newlen++;
-	    }
-	    newlen = buffer.offset;
-	    buffer.writeUTF8(0);
-	    goto L1;
-
-	case X(Tdchar,Twchar):
-	    for (size_t u = 0; u < len; u++)
-	    {
-		unsigned c = ((unsigned *)se->string)[u];
-		if (!utf_isValidDchar(c))
-		    error("invalid UCS-32 char \\U%08x", c);
-		else
-		    buffer.writeUTF16(c);
-		newlen++;
-	    }
-	    newlen = buffer.offset / 2;
-	    buffer.writeUTF16(0);
-	    goto L1;
-
-	L1:
-	    if (!copied)
-	    {   se = (StringExp *)copy();
-		copied = 1;
-	    }
-	    se->string = buffer.extractData();
-	    se->len = newlen;
-	    se->sz = tb->nextOf()->size();
-	    break;
-
-	default:
-	    assert(typeb->nextOf()->size() != tb->nextOf()->size());
-	    goto Lcast;
-    }
-    }
-#undef X
-L2:
-    assert(copied);
-
-    // See if need to truncate or extend the literal
-    if (tb->ty == Tsarray)
-    {
-	int dim2 = ((TypeSArray *)tb)->dim->toInteger();
-
-	//printf("dim from = %d, to = %d\n", se->len, dim2);
-
-	// Changing dimensions
-	if (dim2 != se->len)
-	{
-	    // Copy when changing the string literal
-	    unsigned newsz = se->sz;
-	    void *s;
-	    int d;
-
-	    d = (dim2 < se->len) ? dim2 : se->len;
-	    s = (unsigned char *)mem.malloc((dim2 + 1) * newsz);
-	    memcpy(s, se->string, d * newsz);
-	    // Extend with 0, add terminating 0
-	    memset((char *)s + d * newsz, 0, (dim2 + 1 - d) * newsz);
-	    se->string = s;
-	    se->len = dim2;
-	}
-    }
-    se->type = t;
-    return se;
-
-Lcast:
-    Expression *e = new CastExp(loc, se, t);
-    e->type = t;	// so semantic() won't be run on e
-    return e;
-}
-
-Expression *AddrExp::castTo(Scope *sc, Type *t)
-{
-    Type *tb;
-
-#if 0
-    printf("AddrExp::castTo(this=%s, type=%s, t=%s)\n",
-	toChars(), type->toChars(), t->toChars());
-#endif
-    Expression *e = this;
-
-    tb = t->toBasetype();
-    type = type->toBasetype();
-    if (tb != type)
-    {
-	// Look for pointers to functions where the functions are overloaded.
-	VarExp *ve;
-	FuncDeclaration *f;
-
-	if (type->ty == Tpointer && type->next->ty == Tfunction &&
-	    tb->ty == Tpointer && tb->next->ty == Tfunction &&
-	    e1->op == TOKvar)
-	{
-	    ve = (VarExp *)e1;
-	    f = ve->var->isFuncDeclaration();
-	    if (f)
-	    {
-		f = f->overloadExactMatch(tb->next);
-		if (f)
-		{
-		    e = new VarExp(loc, f);
-		    e->type = f->type;
-		    e = new AddrExp(loc, e);
-		    e->type = t;
-		    return e;
-		}
-	    }
-	}
-	e = Expression::castTo(sc, t);
-    }
-    e->type = t;
-    return e;
-}
-
-
-Expression *TupleExp::castTo(Scope *sc, Type *t)
-{   TupleExp *e = (TupleExp *)copy();
-    e->exps = (Expressions *)exps->copy();
-    for (size_t i = 0; i < e->exps->dim; i++)
-    {   Expression *ex = (Expression *)e->exps->data[i];
-	ex = ex->castTo(sc, t);
-	e->exps->data[i] = (void *)ex;
-    }
-    return e;
-}
-
-
-Expression *ArrayLiteralExp::castTo(Scope *sc, Type *t)
-{
-#if 0
-    printf("ArrayLiteralExp::castTo(this=%s, type=%s, t=%s)\n",
-	toChars(), type->toChars(), t->toChars());
-#endif
-    if (type == t)
-	return this;
-    ArrayLiteralExp *e = this;
-    Type *typeb = type->toBasetype();
-    Type *tb = t->toBasetype();
-    if ((tb->ty == Tarray || tb->ty == Tsarray) &&
-	(typeb->ty == Tarray || typeb->ty == Tsarray) &&
-	tb->nextOf()->toBasetype()->ty != Tvoid)
-    {
-	if (tb->ty == Tsarray)
-	{   TypeSArray *tsa = (TypeSArray *)tb;
-	    if (elements->dim != tsa->dim->toInteger())
-		goto L1;
-	}
-
-	e = (ArrayLiteralExp *)copy();
-	e->elements = (Expressions *)elements->copy();
-	for (int i = 0; i < elements->dim; i++)
-	{   Expression *ex = (Expression *)elements->data[i];
-	    ex = ex->castTo(sc, tb->nextOf());
-	    e->elements->data[i] = (void *)ex;
-	}
-	e->type = t;
-	return e;
-    }
-    if (tb->ty == Tpointer && typeb->ty == Tsarray)
-    {
-	e = (ArrayLiteralExp *)copy();
-	e->type = typeb->nextOf()->pointerTo();
-    }
-L1:
-    return e->Expression::castTo(sc, t);
-}
-
-Expression *AssocArrayLiteralExp::castTo(Scope *sc, Type *t)
-{
-    if (type == t)
-	return this;
-    AssocArrayLiteralExp *e = this;
-    Type *typeb = type->toBasetype();
-    Type *tb = t->toBasetype();
-    if (tb->ty == Taarray && typeb->ty == Taarray &&
-	tb->nextOf()->toBasetype()->ty != Tvoid)
-    {
-	e = (AssocArrayLiteralExp *)copy();
-	e->keys = (Expressions *)keys->copy();
-	e->values = (Expressions *)values->copy();
-	assert(keys->dim == values->dim);
-	for (size_t i = 0; i < keys->dim; i++)
-	{   Expression *ex = (Expression *)values->data[i];
-	    ex = ex->castTo(sc, tb->nextOf());
-	    e->values->data[i] = (void *)ex;
-
-	    ex = (Expression *)keys->data[i];
-	    ex = ex->castTo(sc, ((TypeAArray *)tb)->index);
-	    e->keys->data[i] = (void *)ex;
-	}
-	e->type = t;
-	return e;
-    }
-L1:
-    return e->Expression::castTo(sc, t);
-}
-
-
-Expression *SymOffExp::castTo(Scope *sc, Type *t)
-{
-    Type *tb;
-
-#if 0
-    printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n",
-	toChars(), type->toChars(), t->toChars());
-#endif
-    Expression *e = this;
-
-    tb = t->toBasetype();
-    type = type->toBasetype();
-    if (tb != type)
-    {
-	// Look for pointers to functions where the functions are overloaded.
-	FuncDeclaration *f;
-
-	if (type->ty == Tpointer && type->next->ty == Tfunction &&
-	    tb->ty == Tpointer && tb->next->ty == Tfunction)
-	{
-	    f = var->isFuncDeclaration();
-	    if (f)
-	    {
-		f = f->overloadExactMatch(tb->next);
-		if (f)
-		{
-		    e = new SymOffExp(loc, f, 0);
-		    e->type = t;
-		    return e;
-		}
-	    }
-	}
-	e = Expression::castTo(sc, t);
-    }
-    e->type = t;
-    return e;
-}
-
-Expression *DelegateExp::castTo(Scope *sc, Type *t)
-{
-    Type *tb;
-#if 0
-    printf("DelegateExp::castTo(this=%s, type=%s, t=%s)\n",
-	toChars(), type->toChars(), t->toChars());
-#endif
-    Expression *e = this;
-    static char msg[] = "cannot form delegate due to covariant return type";
-
-    tb = t->toBasetype();
-    type = type->toBasetype();
-    if (tb != type)
-    {
-	// Look for delegates to functions where the functions are overloaded.
-	FuncDeclaration *f;
-
-	if (type->ty == Tdelegate && type->next->ty == Tfunction &&
-	    tb->ty == Tdelegate && tb->next->ty == Tfunction)
-	{
-	    if (func)
-	    {
-		f = func->overloadExactMatch(tb->next);
-		if (f)
-		{   int offset;
-		    if (f->tintro && f->tintro->next->isBaseOf(f->type->next, &offset) && offset)
-			error("%s", msg);
-		    e = new DelegateExp(loc, e1, f);
-		    e->type = t;
-		    return e;
-		}
-		if (func->tintro)
-		    error("%s", msg);
-	    }
-	}
-	e = Expression::castTo(sc, t);
-    }
-    else
-    {	int offset;
-
-	if (func->tintro && func->tintro->next->isBaseOf(func->type->next, &offset) && offset)
-	    error("%s", msg);
-    }
-    e->type = t;
-    return e;
-}
-
-Expression *CondExp::castTo(Scope *sc, Type *t)
-{
-    Expression *e = this;
-
-    if (type != t)
-    {
-	if (1 || e1->op == TOKstring || e2->op == TOKstring)
-	{   e = new CondExp(loc, econd, e1->castTo(sc, t), e2->castTo(sc, t));
-	    e->type = t;
-	}
-	else
-	    e = Expression::castTo(sc, t);
-    }
-    return e;
-}
-
-/* ==================== ====================== */
-
-/****************************************
- * Scale addition/subtraction to/from pointer.
- */
-
-Expression *BinExp::scaleFactor(Scope *sc)
-{   d_uns64 stride;
-    Type *t1b = e1->type->toBasetype();
-    Type *t2b = e2->type->toBasetype();
-
-    if (t1b->ty == Tpointer && t2b->isintegral())
-    {   // Need to adjust operator by the stride
-	// Replace (ptr + int) with (ptr + (int * stride))
-	Type *t = Type::tptrdiff_t;
-
-	stride = t1b->next->size();
-	if (!t->equals(t2b))
-	    e2 = e2->castTo(sc, t);
-    // LLVMDC: llvm uses typesafe pointer arithmetic
-    #if !IN_LLVM
-	if (t1b->next->isbit())
-	    // BUG: should add runtime check for misaligned offsets
-	    // This perhaps should be done by rewriting as &p[i]
-	    // and letting back end do it.
-	    e2 = new UshrExp(loc, e2, new IntegerExp(0, 3, t));
-	else
-	    e2 = new MulExp(loc, e2, new IntegerExp(0, stride, t));
-    #endif
-	e2->type = t;
-	type = e1->type;
-    }
-    else if (t2b->ty == Tpointer && t1b->isintegral())
-    {   // Need to adjust operator by the stride
-	// Replace (int + ptr) with (ptr + (int * stride))
-	Type *t = Type::tptrdiff_t;
-	Expression *e;
-
-	stride = t2b->next->size();
-	if (!t->equals(t1b))
-	    e = e1->castTo(sc, t);
-	else
-	    e = e1;
-    #if !IN_LLVM
-	if (t2b->next->isbit())
-	    // BUG: should add runtime check for misaligned offsets
-	    e = new UshrExp(loc, e, new IntegerExp(0, 3, t));
-	else
-	    e = new MulExp(loc, e, new IntegerExp(0, stride, t));
-    #endif
-	e->type = t;
-	type = e2->type;
-	e1 = e2;
-	e2 = e;
-    }
-    return this;
-}
-
-/************************************
- * Bring leaves to common type.
- */
-
-Expression *BinExp::typeCombine(Scope *sc)
-{
-    Type *t1;
-    Type *t2;
-    Type *t;
-    TY ty;
-
-    //printf("BinExp::typeCombine()\n");
-    //dump(0);
-
-    e1 = e1->integralPromotions(sc);
-    e2 = e2->integralPromotions(sc);
-
-    // BUG: do toBasetype()
-    t1 = e1->type;
-    t2 = e2->type;
-    assert(t1);
-
-    //if (t1) printf("\tt1 = %s\n", t1->toChars());
-    //if (t2) printf("\tt2 = %s\n", t2->toChars());
-#ifdef DEBUG
-    if (!t2) printf("\te2 = '%s'\n", e2->toChars());
-#endif
-    assert(t2);
-
-    Type *t1b = t1->toBasetype();
-    Type *t2b = t2->toBasetype();
-
-    ty = (TY)Type::impcnvResult[t1b->ty][t2b->ty];
-    if (ty != Terror)
-    {	TY ty1;
-	TY ty2;
-
-	ty1 = (TY)Type::impcnvType1[t1b->ty][t2b->ty];
-	ty2 = (TY)Type::impcnvType2[t1b->ty][t2b->ty];
-
-	if (t1b->ty == ty1)	// if no promotions
-	{
-	    if (t1 == t2)
-	    {
-		if (!type)
-		    type = t1;
-		return this;
-	    }
-
-	    if (t1b == t2b)
-	    {
-		if (!type)
-		    type = t1b;
-		return this;
-	    }
-	}
-
-	if (!type)
-	    type = Type::basic[ty];
-
-	t1 = Type::basic[ty1];
-	t2 = Type::basic[ty2];
-	e1 = e1->castTo(sc, t1);
-	e2 = e2->castTo(sc, t2);
-#if 0
-	if (type != Type::basic[ty])
-	{   t = type;
-	    type = Type::basic[ty];
-	    return castTo(sc, t);
-	}
-#endif
-	//printf("after typeCombine():\n");
-	//dump(0);
-	//printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
-	return this;
-    }
-
-    t = t1;
-    if (t1 == t2)
-    {
-	if ((t1->ty == Tstruct || t1->ty == Tclass) &&
-	    (op == TOKmin || op == TOKadd))
-	    goto Lincompatible;
-    }
-    else if (t1->isintegral() && t2->isintegral())
-    {
-	printf("t1 = %s, t2 = %s\n", t1->toChars(), t2->toChars());
-	int sz1 = t1->size();
-	int sz2 = t2->size();
-	int sign1 = t1->isunsigned() == 0;
-	int sign2 = t2->isunsigned() == 0;
-
-	if (sign1 == sign2)
-	{
-	    if (sz1 < sz2)
-		goto Lt2;
-	    else
-		goto Lt1;
-	}
-	if (!sign1)
-	{
-	    if (sz1 >= sz2)
-		goto Lt1;
-	    else
-		goto Lt2;
-	}
-	else
-	{
-	    if (sz2 >= sz1)
-		goto Lt2;
-	    else
-		goto Lt1;
-	}
-    }
-    else if (t1->ty == Tpointer && t2->ty == Tpointer)
-    {
-	// Bring pointers to compatible type
-	Type *t1n = t1->next;
-	Type *t2n = t2->next;
-
-//t1->print();
-//t2->print();
-//if (t1n == t2n) *(char *)0 = 0;
-	assert(t1n != t2n);
-	if (t1n->ty == Tvoid)		// pointers to void are always compatible
-	    t = t2;
-	else if (t2n->ty == Tvoid)
-	    ;
-	else if (t1n->ty == Tclass && t2n->ty == Tclass)
-	{   ClassDeclaration *cd1 = t1n->isClassHandle();
-	    ClassDeclaration *cd2 = t2n->isClassHandle();
-	    int offset;
-
-	    if (cd1->isBaseOf(cd2, &offset))
-	    {
-		if (offset)
-		    e2 = e2->castTo(sc, t);
-	    }
-	    else if (cd2->isBaseOf(cd1, &offset))
-	    {
-		t = t2;
-		if (offset)
-		    e1 = e1->castTo(sc, t);
-	    }
-	    else
-		goto Lincompatible;
-	}
-	else
-	    goto Lincompatible;
-    }
-    else if ((t1->ty == Tsarray || t1->ty == Tarray) &&
-	     e2->op == TOKnull && t2->ty == Tpointer && t2->next->ty == Tvoid)
-    {
-	goto Lx1;
-    }
-    else if ((t2->ty == Tsarray || t2->ty == Tarray) &&
-	     e1->op == TOKnull && t1->ty == Tpointer && t1->next->ty == Tvoid)
-    {
-	goto Lx2;
-    }
-    else if ((t1->ty == Tsarray || t1->ty == Tarray) && t1->implicitConvTo(t2))
-    {
-	goto Lt2;
-    }
-    else if ((t2->ty == Tsarray || t2->ty == Tarray) && t2->implicitConvTo(t1))
-    {
-	goto Lt1;
-    }
-    else if (t1->ty == Tclass || t2->ty == Tclass)
-    {	int i1;
-	int i2;
-
-	i1 = e2->implicitConvTo(t1);
-	i2 = e1->implicitConvTo(t2);
-
-	if (i1 && i2)
-	{
-	    // We have the case of class vs. void*, so pick class
-	    if (t1->ty == Tpointer)
-		i1 = 0;
-	    else if (t2->ty == Tpointer)
-		i2 = 0;
-	}
-
-	if (i2)
-	{
-	    goto Lt2;
-	}
-	else if (i1)
-	{
-	    goto Lt1;
-	}
-	else
-	    goto Lincompatible;
-    }
-    else if ((e1->op == TOKstring || e1->op == TOKnull) && e1->implicitConvTo(t2))
-    {
-	goto Lt2;
-    }
-//else if (e2->op == TOKstring) { printf("test2\n"); }
-    else if ((e2->op == TOKstring || e2->op == TOKnull) && e2->implicitConvTo(t1))
-    {
-	goto Lt1;
-    }
-    else if (t1->ty == Tsarray && t2->ty == Tsarray &&
-	     e2->implicitConvTo(t1->next->arrayOf()))
-    {
-     Lx1:
-	t = t1->next->arrayOf();
-	e1 = e1->castTo(sc, t);
-	e2 = e2->castTo(sc, t);
-    }
-    else if (t1->ty == Tsarray && t2->ty == Tsarray &&
-	     e1->implicitConvTo(t2->next->arrayOf()))
-    {
-     Lx2:
-	t = t2->next->arrayOf();
-	e1 = e1->castTo(sc, t);
-	e2 = e2->castTo(sc, t);
-    }
-    else
-    {
-     Lincompatible:
-	incompatibleTypes();
-    }
-Lret:
-    if (!type)
-	type = t;
-    //dump(0);
-    return this;
-
-
-Lt1:
-    e2 = e2->castTo(sc, t1);
-    t = t1;
-    goto Lret;
-
-Lt2:
-    e1 = e1->castTo(sc, t2);
-    t = t2;
-    goto Lret;
-}
-
-/***********************************
- * Do integral promotions (convertchk).
- * Don't convert <array of> to <pointer to>
- */
-
-Expression *Expression::integralPromotions(Scope *sc)
-{   Expression *e;
-
-    e = this;
-    switch (type->toBasetype()->ty)
-    {
-	case Tvoid:
-	    error("void has no value");
-	    break;
-
-	case Tint8:
-	case Tuns8:
-	case Tint16:
-	case Tuns16:
-	case Tbit:
-	case Tbool:
-	case Tchar:
-	case Twchar:
-	    e = e->castTo(sc, Type::tint32);
-	    break;
-
-	case Tdchar:
-	    e = e->castTo(sc, Type::tuns32);
-	    break;
-    }
-    return e;
-}
-
+
+// Copyright (c) 1999-2006 by Digital Mars
+// All Rights Reserved
+// written by Walter Bright
+// http://www.digitalmars.com
+// License for redistribution is by either the Artistic License
+// in artistic.txt, or the GNU General Public License in gnu.txt.
+// See the included readme.txt for details.
+
+#include <stdio.h>
+#include <assert.h>
+
+#if _WIN32 || IN_GCC || IN_LLVM
+#include "mem.h"
+#else
+#include "../root/mem.h"
+#endif
+
+#include "expression.h"
+#include "mtype.h"
+#include "utf.h"
+#include "declaration.h"
+#include "aggregate.h"
+
+/* ==================== implicitCast ====================== */
+
+/**************************************
+ * Do an implicit cast.
+ * Issue error if it can't be done.
+ */
+
+Expression *Expression::implicitCastTo(Scope *sc, Type *t)
+{
+    //printf("implicitCastTo(%s) => %s\n", type->toChars(), t->toChars());
+    if (implicitConvTo(t))
+    {
+	if (global.params.warnings &&
+	    Type::impcnvWarn[type->toBasetype()->ty][t->toBasetype()->ty] &&
+	    op != TOKint64)
+	{
+	    Expression *e = optimize(WANTflags | WANTvalue);
+
+	    if (e->op == TOKint64)
+		return e->implicitCastTo(sc, t);
+
+	    fprintf(stdmsg, "warning - ");
+	    error("implicit conversion of expression (%s) of type %s to %s can cause loss of data",
+		toChars(), type->toChars(), t->toChars());
+	}
+	return castTo(sc, t);
+    }
+
+    Expression *e = optimize(WANTflags | WANTvalue);
+    if (e != this)
+	return e->implicitCastTo(sc, t);
+
+#if 0
+print();
+type->print();
+printf("to:\n");
+t->print();
+printf("%p %p type: %s to: %s\n", type->deco, t->deco, type->deco, t->deco);
+//printf("%p %p %p\n", type->next->arrayOf(), type, t);
+fflush(stdout);
+#endif
+    if (!t->deco)
+    {	/* Can happen with:
+	 *    enum E { One }
+	 *    class A
+	 *    { static void fork(EDG dg) { dg(E.One); }
+	 *	alias void delegate(E) EDG;
+	 *    }
+	 * Should eventually make it work.
+	 */
+	error("forward reference to type %s", t->toChars());
+    }
+    else if (t->reliesOnTident())
+	error("forward reference to type %s", t->reliesOnTident()->toChars());
+
+    error("cannot implicitly convert expression (%s) of type %s to %s",
+	toChars(), type->toChars(), t->toChars());
+    return castTo(sc, t);
+}
+
+/*******************************************
+ * Return !=0 if we can implicitly convert this to type t.
+ * Don't do the actual cast.
+ */
+
+MATCH Expression::implicitConvTo(Type *t)
+{
+#if 0
+    printf("Expression::implicitConvTo(this=%s, type=%s, t=%s)\n",
+	toChars(), type->toChars(), t->toChars());
+#endif
+    if (!type)
+    {	error("%s is not an expression", toChars());
+	type = Type::terror;
+    }
+    if (t->ty == Tbit && isBit())
+	return MATCHconvert;
+    Expression *e = optimize(WANTvalue | WANTflags);
+    if (e != this)
+    {	//printf("optimzed to %s\n", e->toChars());
+	return e->implicitConvTo(t);
+    }
+    MATCH match = type->implicitConvTo(t);
+    if (match)
+	return match;
+#if 0
+    Type *tb = t->toBasetype();
+    if (tb->ty == Tdelegate)
+    {	TypeDelegate *td = (TypeDelegate *)tb;
+	TypeFunction *tf = (TypeFunction *)td->next;
+
+	if (!tf->varargs &&
+	    !(tf->arguments && tf->arguments->dim)
+	   )
+	{
+	    match = type->implicitConvTo(tf->next);
+	    if (match)
+		return match;
+	    if (tf->next->toBasetype()->ty == Tvoid)
+		return MATCHconvert;
+	}
+    }
+#endif
+    return MATCHnomatch;
+}
+
+
+MATCH IntegerExp::implicitConvTo(Type *t)
+{
+#if 0
+    printf("IntegerExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
+	toChars(), type->toChars(), t->toChars());
+#endif
+    if (type->equals(t))
+	return MATCHexact;
+
+    enum TY ty = type->toBasetype()->ty;
+    enum TY toty = t->toBasetype()->ty;
+
+    if (type->implicitConvTo(t) == MATCHnomatch && t->ty == Tenum)
+    {
+	return MATCHnomatch;
+    }
+
+    switch (ty)
+    {
+	case Tbit:
+	case Tbool:
+	    value &= 1;
+	    ty = Tint32;
+	    break;
+
+	case Tint8:
+	    value = (signed char)value;
+	    ty = Tint32;
+	    break;
+
+	case Tchar:
+	case Tuns8:
+	    value &= 0xFF;
+	    ty = Tint32;
+	    break;
+
+	case Tint16:
+	    value = (short)value;
+	    ty = Tint32;
+	    break;
+
+	case Tuns16:
+	case Twchar:
+	    value &= 0xFFFF;
+	    ty = Tint32;
+	    break;
+
+	case Tint32:
+	    value = (int)value;
+	    break;
+
+	case Tuns32:
+	case Tdchar:
+	    value &= 0xFFFFFFFF;
+	    ty = Tuns32;
+	    break;
+
+	default:
+	    break;
+    }
+
+    // Only allow conversion if no change in value
+    switch (toty)
+    {
+	case Tbit:
+	case Tbool:
+	    if ((value & 1) != value)
+		goto Lno;
+	    goto Lyes;
+
+	case Tint8:
+	    if ((signed char)value != value)
+		goto Lno;
+	    goto Lyes;
+
+	case Tchar:
+	case Tuns8:
+	    //printf("value = %llu %llu\n", (integer_t)(unsigned char)value, value);
+	    if ((unsigned char)value != value)
+		goto Lno;
+	    goto Lyes;
+
+	case Tint16:
+	    if ((short)value != value)
+		goto Lno;
+	    goto Lyes;
+
+	case Tuns16:
+	    if ((unsigned short)value != value)
+		goto Lno;
+	    goto Lyes;
+
+	case Tint32:
+	    if (ty == Tuns32)
+	    {
+	    }
+	    else if ((int)value != value)
+		goto Lno;
+	    goto Lyes;
+
+	case Tuns32:
+	    if (ty == Tint32)
+	    {
+	    }
+	    else if ((unsigned)value != value)
+		goto Lno;
+	    goto Lyes;
+
+	case Tdchar:
+	    if (value > 0x10FFFFUL)
+		goto Lno;
+	    goto Lyes;
+
+	case Twchar:
+	    if ((unsigned short)value != value)
+		goto Lno;
+	    goto Lyes;
+
+	case Tfloat32:
+	{
+	    volatile float f;
+	    if (type->isunsigned())
+	    {
+		f = (float)value;
+		if (f != value)
+		    goto Lno;
+	    }
+	    else
+	    {
+		f = (float)(long long)value;
+		if (f != (long long)value)
+		    goto Lno;
+	    }
+	    goto Lyes;
+	}
+
+	case Tfloat64:
+	{
+	    volatile double f;
+	    if (type->isunsigned())
+	    {
+		f = (double)value;
+		if (f != value)
+		    goto Lno;
+	    }
+	    else
+	    {
+		f = (double)(long long)value;
+		if (f != (long long)value)
+		    goto Lno;
+	    }
+	    goto Lyes;
+	}
+
+	case Tfloat80:
+	{
+	    volatile long double f;
+	    if (type->isunsigned())
+	    {
+		f = (long double)value;
+		if (f != value)
+		    goto Lno;
+	    }
+	    else
+	    {
+		f = (long double)(long long)value;
+		if (f != (long long)value)
+		    goto Lno;
+	    }
+	    goto Lyes;
+	}
+    }
+    return Expression::implicitConvTo(t);
+
+Lyes:
+    //printf("MATCHconvert\n");
+    return MATCHconvert;
+
+Lno:
+    //printf("MATCHnomatch\n");
+    return MATCHnomatch;
+}
+
+MATCH NullExp::implicitConvTo(Type *t)
+{
+#if 0
+    printf("NullExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
+	toChars(), type->toChars(), t->toChars());
+#endif
+    if (this->type->equals(t))
+	return MATCHexact;
+    // NULL implicitly converts to any pointer type or dynamic array
+    if (type->ty == Tpointer && type->next->ty == Tvoid)
+    {
+	if (t->ty == Ttypedef)
+	    t = ((TypeTypedef *)t)->sym->basetype;
+	if (t->ty == Tpointer || t->ty == Tarray ||
+	    t->ty == Taarray  || t->ty == Tclass ||
+	    t->ty == Tdelegate)
+	    return committed ? MATCHconvert : MATCHexact;
+    }
+    return Expression::implicitConvTo(t);
+}
+
+MATCH StringExp::implicitConvTo(Type *t)
+{   MATCH m;
+
+#if 0
+    printf("StringExp::implicitConvTo(this=%s, committed=%d, type=%s, t=%s)\n",
+	toChars(), committed, type->toChars(), t->toChars());
+#endif
+    if (!committed)
+    {
+    if (!committed && t->ty == Tpointer && t->next->ty == Tvoid)
+    {
+	return MATCHnomatch;
+    }
+    if (type->ty == Tsarray || type->ty == Tarray || type->ty == Tpointer)
+    {
+	if (type->next->ty == Tchar)
+	{
+	    switch (t->ty)
+	    {
+		case Tsarray:
+		    if (type->ty == Tsarray &&
+			((TypeSArray *)type)->dim->toInteger() !=
+			((TypeSArray *)t)->dim->toInteger())
+			return MATCHnomatch;
+		    goto L1;
+		case Tarray:
+		    goto L1;
+		case Tpointer:
+		L1:
+		    if (t->next->ty == Tchar)
+			return MATCHexact;
+		    else if (t->next->ty == Twchar)
+			return MATCHexact;
+		    else if (t->next->ty == Tdchar)
+			return MATCHexact;
+		    break;
+	    }
+	}
+    }
+    }
+    return Expression::implicitConvTo(t);
+#if 0
+    m = (MATCH)type->implicitConvTo(t);
+    if (m)
+    {
+	return m;
+    }
+
+    return MATCHnomatch;
+#endif
+}
+
+MATCH ArrayLiteralExp::implicitConvTo(Type *t)
+{   MATCH result = MATCHexact;
+
+    Type *typeb = type->toBasetype();
+    Type *tb = t->toBasetype();
+    if ((tb->ty == Tarray || tb->ty == Tsarray) &&
+	(typeb->ty == Tarray || typeb->ty == Tsarray))
+    {
+	if (tb->ty == Tsarray)
+	{   TypeSArray *tsa = (TypeSArray *)tb;
+	    if (elements->dim != tsa->dim->toInteger())
+		result = MATCHnomatch;
+	}
+
+	for (int i = 0; i < elements->dim; i++)
+	{   Expression *e = (Expression *)elements->data[i];
+	    MATCH m = (MATCH)e->implicitConvTo(tb->next);
+	    if (m < result)
+		result = m;			// remember worst match
+	    if (result == MATCHnomatch)
+		break;				// no need to check for worse
+	}
+	return result;
+    }
+    else
+	return Expression::implicitConvTo(t);
+}
+
+MATCH AssocArrayLiteralExp::implicitConvTo(Type *t)
+{   MATCH result = MATCHexact;
+
+    Type *typeb = type->toBasetype();
+    Type *tb = t->toBasetype();
+    if (tb->ty == Taarray && typeb->ty == Taarray)
+    {
+	for (size_t i = 0; i < keys->dim; i++)
+	{   Expression *e = (Expression *)keys->data[i];
+	    MATCH m = (MATCH)e->implicitConvTo(((TypeAArray *)tb)->key);
+	    if (m < result)
+		result = m;			// remember worst match
+	    if (result == MATCHnomatch)
+		break;				// no need to check for worse
+	    e = (Expression *)values->data[i];
+	    m = (MATCH)e->implicitConvTo(tb->next);
+	    if (m < result)
+		result = m;			// remember worst match
+	    if (result == MATCHnomatch)
+		break;				// no need to check for worse
+	}
+	return result;
+    }
+    else
+	return Expression::implicitConvTo(t);
+}
+
+MATCH AddrExp::implicitConvTo(Type *t)
+{
+#if 0
+    printf("AddrExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
+	toChars(), type->toChars(), t->toChars());
+#endif
+    MATCH result;
+
+    result = type->implicitConvTo(t);
+    //printf("\tresult = %d\n", result);
+
+    if (result == MATCHnomatch)
+    {
+	// Look for pointers to functions where the functions are overloaded.
+	VarExp *ve;
+	FuncDeclaration *f;
+
+	t = t->toBasetype();
+	if (type->ty == Tpointer && type->next->ty == Tfunction &&
+	    t->ty == Tpointer && t->next->ty == Tfunction &&
+	    e1->op == TOKvar)
+	{
+	    ve = (VarExp *)e1;
+	    f = ve->var->isFuncDeclaration();
+	    if (f && f->overloadExactMatch(t->next))
+		result = MATCHexact;
+	}
+    }
+    //printf("\tresult = %d\n", result);
+    return result;
+}
+
+MATCH SymOffExp::implicitConvTo(Type *t)
+{
+#if 0
+    printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
+	toChars(), type->toChars(), t->toChars());
+#endif
+    MATCH result;
+
+    result = type->implicitConvTo(t);
+    //printf("\tresult = %d\n", result);
+
+    if (result == MATCHnomatch)
+    {
+	// Look for pointers to functions where the functions are overloaded.
+	FuncDeclaration *f;
+
+	t = t->toBasetype();
+	if (type->ty == Tpointer && type->next->ty == Tfunction &&
+	    t->ty == Tpointer && t->next->ty == Tfunction)
+	{
+	    f = var->isFuncDeclaration();
+	    if (f && f->overloadExactMatch(t->next))
+		result = MATCHexact;
+	}
+    }
+    //printf("\tresult = %d\n", result);
+    return result;
+}
+
+MATCH DelegateExp::implicitConvTo(Type *t)
+{
+#if 0
+    printf("DelegateExp::implicitConvTo(this=%s, type=%s, t=%s)\n",
+	toChars(), type->toChars(), t->toChars());
+#endif
+    MATCH result;
+
+    result = type->implicitConvTo(t);
+
+    if (result == 0)
+    {
+	// Look for pointers to functions where the functions are overloaded.
+	FuncDeclaration *f;
+
+	t = t->toBasetype();
+	if (type->ty == Tdelegate && type->next->ty == Tfunction &&
+	    t->ty == Tdelegate && t->next->ty == Tfunction)
+	{
+	    if (func && func->overloadExactMatch(t->next))
+		result = MATCHexact;
+	}
+    }
+    return result;
+}
+
+MATCH CondExp::implicitConvTo(Type *t)
+{
+    MATCH m1;
+    MATCH m2;
+
+    m1 = e1->implicitConvTo(t);
+    m2 = e2->implicitConvTo(t);
+
+    // Pick the worst match
+    return (m1 < m2) ? m1 : m2;
+}
+
+
+/* ==================== castTo ====================== */
+
+/**************************************
+ * Do an explicit cast.
+ */
+
+Expression *Expression::castTo(Scope *sc, Type *t)
+{
+    //printf("Expression::castTo(this=%s, t=%s)\n", toChars(), t->toChars());
+#if 0
+    printf("Expression::castTo(this=%s, type=%s, t=%s)\n",
+	toChars(), type->toChars(), t->toChars());
+#endif
+    if (type == t)
+	return this;
+    Expression *e = this;
+    Type *tb = t->toBasetype();
+    Type *typeb = type->toBasetype();
+    if (tb != typeb)
+    {
+	// Do (type *) cast of (type [dim])
+	if (tb->ty == Tpointer &&
+	    typeb->ty == Tsarray
+	   )
+	{
+	    //printf("Converting [dim] to *\n");
+
+	    if (typeb->size(loc) == 0)
+		e = new NullExp(loc);
+	    else
+		e = new AddrExp(loc, e);
+	}
+#if 0
+	else if (tb->ty == Tdelegate && type->ty != Tdelegate)
+	{
+	    TypeDelegate *td = (TypeDelegate *)tb;
+	    TypeFunction *tf = (TypeFunction *)td->nextOf();
+	    return toDelegate(sc, tf->nextOf());
+	}
+#endif
+	else
+	{
+	    e = new CastExp(loc, e, tb);
+	}
+    }
+    else
+    {
+	e = e->copy();	// because of COW for assignment to e->type
+    }
+    assert(e != this);
+    e->type = t;
+    //printf("Returning: %s\n", e->toChars());
+    return e;
+}
+
+
+Expression *RealExp::castTo(Scope *sc, Type *t)
+{   Expression *e = this;
+    if (type != t)
+    {
+	if ((type->isreal() && t->isreal()) ||
+	    (type->isimaginary() && t->isimaginary())
+	   )
+	{   e = copy();
+	    e->type = t;
+	}
+	else
+	    e = Expression::castTo(sc, t);
+    }
+    return e;
+}
+
+
+Expression *ComplexExp::castTo(Scope *sc, Type *t)
+{   Expression *e = this;
+    if (type != t)
+    {
+	if (type->iscomplex() && t->iscomplex())
+	{   e = copy();
+	    e->type = t;
+	}
+	else
+	    e = Expression::castTo(sc, t);
+    }
+    return e;
+}
+
+
+Expression *NullExp::castTo(Scope *sc, Type *t)
+{   NullExp *e;
+    Type *tb;
+
+    //printf("NullExp::castTo(t = %p)\n", t);
+    if (type == t)
+    {
+	committed = 1;
+	return this;
+    }
+    e = (NullExp *)copy();
+    e->committed = 1;
+    tb = t->toBasetype();
+    e->type = type->toBasetype();
+    if (tb != e->type)
+    {
+	// NULL implicitly converts to any pointer type or dynamic array
+	if (e->type->ty == Tpointer && e->type->nextOf()->ty == Tvoid &&
+	    (tb->ty == Tpointer || tb->ty == Tarray || tb->ty == Taarray ||
+	     tb->ty == Tdelegate))
+	{
+#if 0
+	    if (tb->ty == Tdelegate)
+	    {   TypeDelegate *td = (TypeDelegate *)tb;
+		TypeFunction *tf = (TypeFunction *)td->nextOf();
+
+		if (!tf->varargs &&
+		    !(tf->arguments && tf->arguments->dim)
+		   )
+		{
+		    return Expression::castTo(sc, t);
+		}
+	    }
+#endif
+	}
+	else
+	{
+	    return e->Expression::castTo(sc, t);
+	}
+    }
+    e->type = t;
+    return e;
+}
+
+Expression *StringExp::castTo(Scope *sc, Type *t)
+{
+    /* This follows copy-on-write; any changes to 'this'
+     * will result in a copy.
+     * The this->string member is considered immutable.
+     */
+    StringExp *se;
+    Type *tb;
+    int copied = 0;
+
+    //printf("StringExp::castTo(t = %s), '%s' committed = %d\n", t->toChars(), toChars(), committed);
+
+    if (!committed && t->ty == Tpointer && t->nextOf()->ty == Tvoid)
+    {
+	error("cannot convert string literal to void*");
+    }
+
+    se = this;
+    if (!committed)
+    {   se = (StringExp *)copy();
+	se->committed = 1;
+	copied = 1;
+    }
+
+    if (type == t)
+    {
+	return se;
+    }
+
+    tb = t->toBasetype();
+    //printf("\ttype = %s\n", type->toChars());
+    if (tb->ty == Tdelegate && type->toBasetype()->ty != Tdelegate)
+	return Expression::castTo(sc, t);
+
+    Type *typeb = type->toBasetype();
+    if (typeb == tb)
+    {
+	if (!copied)
+	{   se = (StringExp *)copy();
+	    copied = 1;
+	}
+	se->type = t;
+	return se;
+    }
+
+    if (tb->ty != Tsarray && tb->ty != Tarray && tb->ty != Tpointer)
+    {	if (!copied)
+	{   se = (StringExp *)copy();
+	    copied = 1;
+	}
+	goto Lcast;
+    }
+    if (typeb->ty != Tsarray && typeb->ty != Tarray && typeb->ty != Tpointer)
+    {	if (!copied)
+	{   se = (StringExp *)copy();
+	    copied = 1;
+	}
+	goto Lcast;
+    }
+
+    if (typeb->nextOf()->size() == tb->nextOf()->size())
+    {
+	if (!copied)
+	{   se = (StringExp *)copy();
+	    copied = 1;
+	}
+	if (tb->ty == Tsarray)
+	    goto L2;	// handle possible change in static array dimension
+	se->type = t;
+	return se;
+    }
+
+    if (committed)
+	goto Lcast;
+
+#define X(tf,tt)	((tf) * 256 + (tt))
+    {
+    OutBuffer buffer;
+    size_t newlen = 0;
+    int tfty = typeb->nextOf()->toBasetype()->ty;
+    int ttty = tb->nextOf()->toBasetype()->ty;
+    switch (X(tfty, ttty))
+    {
+	case X(Tchar, Tchar):
+	case X(Twchar,Twchar):
+	case X(Tdchar,Tdchar):
+	    break;
+
+	case X(Tchar, Twchar):
+	    for (size_t u = 0; u < len;)
+	    {	unsigned c;
+		char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
+		if (p)
+		    error("%s", p);
+		else
+		    buffer.writeUTF16(c);
+	    }
+	    newlen = buffer.offset / 2;
+	    buffer.writeUTF16(0);
+	    goto L1;
+
+	case X(Tchar, Tdchar):
+	    for (size_t u = 0; u < len;)
+	    {	unsigned c;
+		char *p = utf_decodeChar((unsigned char *)se->string, len, &u, &c);
+		if (p)
+		    error("%s", p);
+		buffer.write4(c);
+		newlen++;
+	    }
+	    buffer.write4(0);
+	    goto L1;
+
+	case X(Twchar,Tchar):
+	    for (size_t u = 0; u < len;)
+	    {	unsigned c;
+		char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
+		if (p)
+		    error("%s", p);
+		else
+		    buffer.writeUTF8(c);
+	    }
+	    newlen = buffer.offset;
+	    buffer.writeUTF8(0);
+	    goto L1;
+
+	case X(Twchar,Tdchar):
+	    for (size_t u = 0; u < len;)
+	    {	unsigned c;
+		char *p = utf_decodeWchar((unsigned short *)se->string, len, &u, &c);
+		if (p)
+		    error("%s", p);
+		buffer.write4(c);
+		newlen++;
+	    }
+	    buffer.write4(0);
+	    goto L1;
+
+	case X(Tdchar,Tchar):
+	    for (size_t u = 0; u < len; u++)
+	    {
+		unsigned c = ((unsigned *)se->string)[u];
+		if (!utf_isValidDchar(c))
+		    error("invalid UCS-32 char \\U%08x", c);
+		else
+		    buffer.writeUTF8(c);
+		newlen++;
+	    }
+	    newlen = buffer.offset;
+	    buffer.writeUTF8(0);
+	    goto L1;
+
+	case X(Tdchar,Twchar):
+	    for (size_t u = 0; u < len; u++)
+	    {
+		unsigned c = ((unsigned *)se->string)[u];
+		if (!utf_isValidDchar(c))
+		    error("invalid UCS-32 char \\U%08x", c);
+		else
+		    buffer.writeUTF16(c);
+		newlen++;
+	    }
+	    newlen = buffer.offset / 2;
+	    buffer.writeUTF16(0);
+	    goto L1;
+
+	L1:
+	    if (!copied)
+	    {   se = (StringExp *)copy();
+		copied = 1;
+	    }
+	    se->string = buffer.extractData();
+	    se->len = newlen;
+	    se->sz = tb->nextOf()->size();
+	    break;
+
+	default:
+	    assert(typeb->nextOf()->size() != tb->nextOf()->size());
+	    goto Lcast;
+    }
+    }
+#undef X
+L2:
+    assert(copied);
+
+    // See if need to truncate or extend the literal
+    if (tb->ty == Tsarray)
+    {
+	int dim2 = ((TypeSArray *)tb)->dim->toInteger();
+
+	//printf("dim from = %d, to = %d\n", se->len, dim2);
+
+	// Changing dimensions
+	if (dim2 != se->len)
+	{
+	    // Copy when changing the string literal
+	    unsigned newsz = se->sz;
+	    void *s;
+	    int d;
+
+	    d = (dim2 < se->len) ? dim2 : se->len;
+	    s = (unsigned char *)mem.malloc((dim2 + 1) * newsz);
+	    memcpy(s, se->string, d * newsz);
+	    // Extend with 0, add terminating 0
+	    memset((char *)s + d * newsz, 0, (dim2 + 1 - d) * newsz);
+	    se->string = s;
+	    se->len = dim2;
+	}
+    }
+    se->type = t;
+    return se;
+
+Lcast:
+    Expression *e = new CastExp(loc, se, t);
+    e->type = t;	// so semantic() won't be run on e
+    return e;
+}
+
+Expression *AddrExp::castTo(Scope *sc, Type *t)
+{
+    Type *tb;
+
+#if 0
+    printf("AddrExp::castTo(this=%s, type=%s, t=%s)\n",
+	toChars(), type->toChars(), t->toChars());
+#endif
+    Expression *e = this;
+
+    tb = t->toBasetype();
+    type = type->toBasetype();
+    if (tb != type)
+    {
+	// Look for pointers to functions where the functions are overloaded.
+	VarExp *ve;
+	FuncDeclaration *f;
+
+	if (type->ty == Tpointer && type->next->ty == Tfunction &&
+	    tb->ty == Tpointer && tb->next->ty == Tfunction &&
+	    e1->op == TOKvar)
+	{
+	    ve = (VarExp *)e1;
+	    f = ve->var->isFuncDeclaration();
+	    if (f)
+	    {
+		f = f->overloadExactMatch(tb->next);
+		if (f)
+		{
+		    e = new VarExp(loc, f);
+		    e->type = f->type;
+		    e = new AddrExp(loc, e);
+		    e->type = t;
+		    return e;
+		}
+	    }
+	}
+	e = Expression::castTo(sc, t);
+    }
+    e->type = t;
+    return e;
+}
+
+
+Expression *TupleExp::castTo(Scope *sc, Type *t)
+{   TupleExp *e = (TupleExp *)copy();
+    e->exps = (Expressions *)exps->copy();
+    for (size_t i = 0; i < e->exps->dim; i++)
+    {   Expression *ex = (Expression *)e->exps->data[i];
+	ex = ex->castTo(sc, t);
+	e->exps->data[i] = (void *)ex;
+    }
+    return e;
+}
+
+
+Expression *ArrayLiteralExp::castTo(Scope *sc, Type *t)
+{
+#if 0
+    printf("ArrayLiteralExp::castTo(this=%s, type=%s, t=%s)\n",
+	toChars(), type->toChars(), t->toChars());
+#endif
+    if (type == t)
+	return this;
+    ArrayLiteralExp *e = this;
+    Type *typeb = type->toBasetype();
+    Type *tb = t->toBasetype();
+    if ((tb->ty == Tarray || tb->ty == Tsarray) &&
+	(typeb->ty == Tarray || typeb->ty == Tsarray) &&
+	// Not trying to convert non-void[] to void[]
+	!(tb->nextOf()->toBasetype()->ty == Tvoid && typeb->nextOf()->toBasetype()->ty != Tvoid))
+    {
+	if (tb->ty == Tsarray)
+	{   TypeSArray *tsa = (TypeSArray *)tb;
+	    if (elements->dim != tsa->dim->toInteger())
+		goto L1;
+	}
+
+	e = (ArrayLiteralExp *)copy();
+	e->elements = (Expressions *)elements->copy();
+	for (int i = 0; i < elements->dim; i++)
+	{   Expression *ex = (Expression *)elements->data[i];
+	    ex = ex->castTo(sc, tb->nextOf());
+	    e->elements->data[i] = (void *)ex;
+	}
+	e->type = t;
+	return e;
+    }
+    if (tb->ty == Tpointer && typeb->ty == Tsarray)
+    {
+	e = (ArrayLiteralExp *)copy();
+	e->type = typeb->nextOf()->pointerTo();
+    }
+L1:
+    return e->Expression::castTo(sc, t);
+}
+
+Expression *AssocArrayLiteralExp::castTo(Scope *sc, Type *t)
+{
+    if (type == t)
+	return this;
+    AssocArrayLiteralExp *e = this;
+    Type *typeb = type->toBasetype();
+    Type *tb = t->toBasetype();
+    if (tb->ty == Taarray && typeb->ty == Taarray &&
+	tb->nextOf()->toBasetype()->ty != Tvoid)
+    {
+	e = (AssocArrayLiteralExp *)copy();
+	e->keys = (Expressions *)keys->copy();
+	e->values = (Expressions *)values->copy();
+	assert(keys->dim == values->dim);
+	for (size_t i = 0; i < keys->dim; i++)
+	{   Expression *ex = (Expression *)values->data[i];
+	    ex = ex->castTo(sc, tb->nextOf());
+	    e->values->data[i] = (void *)ex;
+
+	    ex = (Expression *)keys->data[i];
+	    ex = ex->castTo(sc, ((TypeAArray *)tb)->index);
+	    e->keys->data[i] = (void *)ex;
+	}
+	e->type = t;
+	return e;
+    }
+L1:
+    return e->Expression::castTo(sc, t);
+}
+
+
+Expression *SymOffExp::castTo(Scope *sc, Type *t)
+{
+    Type *tb;
+
+#if 0
+    printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n",
+	toChars(), type->toChars(), t->toChars());
+#endif
+    Expression *e = this;
+
+    tb = t->toBasetype();
+    type = type->toBasetype();
+    if (tb != type)
+    {
+	// Look for pointers to functions where the functions are overloaded.
+	FuncDeclaration *f;
+
+	if (type->ty == Tpointer && type->next->ty == Tfunction &&
+	    tb->ty == Tpointer && tb->next->ty == Tfunction)
+	{
+	    f = var->isFuncDeclaration();
+	    if (f)
+	    {
+		f = f->overloadExactMatch(tb->next);
+		if (f)
+		{
+		    e = new SymOffExp(loc, f, 0);
+		    e->type = t;
+		    return e;
+		}
+	    }
+	}
+	e = Expression::castTo(sc, t);
+    }
+    e->type = t;
+    return e;
+}
+
+Expression *DelegateExp::castTo(Scope *sc, Type *t)
+{
+    Type *tb;
+#if 0
+    printf("DelegateExp::castTo(this=%s, type=%s, t=%s)\n",
+	toChars(), type->toChars(), t->toChars());
+#endif
+    Expression *e = this;
+    static char msg[] = "cannot form delegate due to covariant return type";
+
+    tb = t->toBasetype();
+    type = type->toBasetype();
+    if (tb != type)
+    {
+	// Look for delegates to functions where the functions are overloaded.
+	FuncDeclaration *f;
+
+	if (type->ty == Tdelegate && type->next->ty == Tfunction &&
+	    tb->ty == Tdelegate && tb->next->ty == Tfunction)
+	{
+	    if (func)
+	    {
+		f = func->overloadExactMatch(tb->next);
+		if (f)
+		{   int offset;
+		    if (f->tintro && f->tintro->next->isBaseOf(f->type->next, &offset) && offset)
+			error("%s", msg);
+		    e = new DelegateExp(loc, e1, f);
+		    e->type = t;
+		    return e;
+		}
+		if (func->tintro)
+		    error("%s", msg);
+	    }
+	}
+	e = Expression::castTo(sc, t);
+    }
+    else
+    {	int offset;
+
+	if (func->tintro && func->tintro->next->isBaseOf(func->type->next, &offset) && offset)
+	    error("%s", msg);
+    }
+    e->type = t;
+    return e;
+}
+
+Expression *CondExp::castTo(Scope *sc, Type *t)
+{
+    Expression *e = this;
+
+    if (type != t)
+    {
+	if (1 || e1->op == TOKstring || e2->op == TOKstring)
+	{   e = new CondExp(loc, econd, e1->castTo(sc, t), e2->castTo(sc, t));
+	    e->type = t;
+	}
+	else
+	    e = Expression::castTo(sc, t);
+    }
+    return e;
+}
+
+/* ==================== ====================== */
+
+/****************************************
+ * Scale addition/subtraction to/from pointer.
+ */
+
+Expression *BinExp::scaleFactor(Scope *sc)
+{   d_uns64 stride;
+    Type *t1b = e1->type->toBasetype();
+    Type *t2b = e2->type->toBasetype();
+
+    if (t1b->ty == Tpointer && t2b->isintegral())
+    {   // Need to adjust operator by the stride
+	// Replace (ptr + int) with (ptr + (int * stride))
+	Type *t = Type::tptrdiff_t;
+
+	stride = t1b->next->size();
+	if (!t->equals(t2b))
+	    e2 = e2->castTo(sc, t);
+    // LLVMDC: llvm uses typesafe pointer arithmetic
+    #if !IN_LLVM
+	if (t1b->next->isbit())
+	    // BUG: should add runtime check for misaligned offsets
+	    // This perhaps should be done by rewriting as &p[i]
+	    // and letting back end do it.
+	    e2 = new UshrExp(loc, e2, new IntegerExp(0, 3, t));
+	else
+	    e2 = new MulExp(loc, e2, new IntegerExp(0, stride, t));
+    #endif
+	e2->type = t;
+	type = e1->type;
+    }
+    else if (t2b->ty == Tpointer && t1b->isintegral())
+    {   // Need to adjust operator by the stride
+	// Replace (int + ptr) with (ptr + (int * stride))
+	Type *t = Type::tptrdiff_t;
+	Expression *e;
+
+	stride = t2b->next->size();
+	if (!t->equals(t1b))
+	    e = e1->castTo(sc, t);
+	else
+	    e = e1;
+    #if !IN_LLVM
+	if (t2b->next->isbit())
+	    // BUG: should add runtime check for misaligned offsets
+	    e = new UshrExp(loc, e, new IntegerExp(0, 3, t));
+	else
+	    e = new MulExp(loc, e, new IntegerExp(0, stride, t));
+    #endif
+	e->type = t;
+	type = e2->type;
+	e1 = e2;
+	e2 = e;
+    }
+    return this;
+}
+
+/************************************
+ * Bring leaves to common type.
+ */
+
+Expression *BinExp::typeCombine(Scope *sc)
+{
+    Type *t1;
+    Type *t2;
+    Type *t;
+    TY ty;
+
+    //printf("BinExp::typeCombine()\n");
+    //dump(0);
+
+    e1 = e1->integralPromotions(sc);
+    e2 = e2->integralPromotions(sc);
+
+    // BUG: do toBasetype()
+    t1 = e1->type;
+    t2 = e2->type;
+    assert(t1);
+
+    //if (t1) printf("\tt1 = %s\n", t1->toChars());
+    //if (t2) printf("\tt2 = %s\n", t2->toChars());
+#ifdef DEBUG
+    if (!t2) printf("\te2 = '%s'\n", e2->toChars());
+#endif
+    assert(t2);
+
+    Type *t1b = t1->toBasetype();
+    Type *t2b = t2->toBasetype();
+
+    ty = (TY)Type::impcnvResult[t1b->ty][t2b->ty];
+    if (ty != Terror)
+    {	TY ty1;
+	TY ty2;
+
+	ty1 = (TY)Type::impcnvType1[t1b->ty][t2b->ty];
+	ty2 = (TY)Type::impcnvType2[t1b->ty][t2b->ty];
+
+	if (t1b->ty == ty1)	// if no promotions
+	{
+	    if (t1 == t2)
+	    {
+		if (!type)
+		    type = t1;
+		return this;
+	    }
+
+	    if (t1b == t2b)
+	    {
+		if (!type)
+		    type = t1b;
+		return this;
+	    }
+	}
+
+	if (!type)
+	    type = Type::basic[ty];
+
+	t1 = Type::basic[ty1];
+	t2 = Type::basic[ty2];
+	e1 = e1->castTo(sc, t1);
+	e2 = e2->castTo(sc, t2);
+#if 0
+	if (type != Type::basic[ty])
+	{   t = type;
+	    type = Type::basic[ty];
+	    return castTo(sc, t);
+	}
+#endif
+	//printf("after typeCombine():\n");
+	//dump(0);
+	//printf("ty = %d, ty1 = %d, ty2 = %d\n", ty, ty1, ty2);
+	return this;
+    }
+
+    t = t1;
+    if (t1 == t2)
+    {
+	if ((t1->ty == Tstruct || t1->ty == Tclass) &&
+	    (op == TOKmin || op == TOKadd))
+	    goto Lincompatible;
+    }
+    else if (t1->isintegral() && t2->isintegral())
+    {
+	printf("t1 = %s, t2 = %s\n", t1->toChars(), t2->toChars());
+	int sz1 = t1->size();
+	int sz2 = t2->size();
+	int sign1 = t1->isunsigned() == 0;
+	int sign2 = t2->isunsigned() == 0;
+
+	if (sign1 == sign2)
+	{
+	    if (sz1 < sz2)
+		goto Lt2;
+	    else
+		goto Lt1;
+	}
+	if (!sign1)
+	{
+	    if (sz1 >= sz2)
+		goto Lt1;
+	    else
+		goto Lt2;
+	}
+	else
+	{
+	    if (sz2 >= sz1)
+		goto Lt2;
+	    else
+		goto Lt1;
+	}
+    }
+    else if (t1->ty == Tpointer && t2->ty == Tpointer)
+    {
+	// Bring pointers to compatible type
+	Type *t1n = t1->next;
+	Type *t2n = t2->next;
+
+//t1->print();
+//t2->print();
+//if (t1n == t2n) *(char *)0 = 0;
+	assert(t1n != t2n);
+	if (t1n->ty == Tvoid)		// pointers to void are always compatible
+	    t = t2;
+	else if (t2n->ty == Tvoid)
+	    ;
+	else if (t1n->ty == Tclass && t2n->ty == Tclass)
+	{   ClassDeclaration *cd1 = t1n->isClassHandle();
+	    ClassDeclaration *cd2 = t2n->isClassHandle();
+	    int offset;
+
+	    if (cd1->isBaseOf(cd2, &offset))
+	    {
+		if (offset)
+		    e2 = e2->castTo(sc, t);
+	    }
+	    else if (cd2->isBaseOf(cd1, &offset))
+	    {
+		t = t2;
+		if (offset)
+		    e1 = e1->castTo(sc, t);
+	    }
+	    else
+		goto Lincompatible;
+	}
+	else
+	    goto Lincompatible;
+    }
+    else if ((t1->ty == Tsarray || t1->ty == Tarray) &&
+	     e2->op == TOKnull && t2->ty == Tpointer && t2->next->ty == Tvoid)
+    {
+	goto Lx1;
+    }
+    else if ((t2->ty == Tsarray || t2->ty == Tarray) &&
+	     e1->op == TOKnull && t1->ty == Tpointer && t1->next->ty == Tvoid)
+    {
+	goto Lx2;
+    }
+    else if ((t1->ty == Tsarray || t1->ty == Tarray) && t1->implicitConvTo(t2))
+    {
+	goto Lt2;
+    }
+    else if ((t2->ty == Tsarray || t2->ty == Tarray) && t2->implicitConvTo(t1))
+    {
+	goto Lt1;
+    }
+    else if (t1->ty == Tclass || t2->ty == Tclass)
+    {	int i1;
+	int i2;
+
+	i1 = e2->implicitConvTo(t1);
+	i2 = e1->implicitConvTo(t2);
+
+	if (i1 && i2)
+	{
+	    // We have the case of class vs. void*, so pick class
+	    if (t1->ty == Tpointer)
+		i1 = 0;
+	    else if (t2->ty == Tpointer)
+		i2 = 0;
+	}
+
+	if (i2)
+	{
+	    goto Lt2;
+	}
+	else if (i1)
+	{
+	    goto Lt1;
+	}
+	else
+	    goto Lincompatible;
+    }
+    else if ((e1->op == TOKstring || e1->op == TOKnull) && e1->implicitConvTo(t2))
+    {
+	goto Lt2;
+    }
+//else if (e2->op == TOKstring) { printf("test2\n"); }
+    else if ((e2->op == TOKstring || e2->op == TOKnull) && e2->implicitConvTo(t1))
+    {
+	goto Lt1;
+    }
+    else if (t1->ty == Tsarray && t2->ty == Tsarray &&
+	     e2->implicitConvTo(t1->next->arrayOf()))
+    {
+     Lx1:
+	t = t1->next->arrayOf();
+	e1 = e1->castTo(sc, t);
+	e2 = e2->castTo(sc, t);
+    }
+    else if (t1->ty == Tsarray && t2->ty == Tsarray &&
+	     e1->implicitConvTo(t2->next->arrayOf()))
+    {
+     Lx2:
+	t = t2->next->arrayOf();
+	e1 = e1->castTo(sc, t);
+	e2 = e2->castTo(sc, t);
+    }
+    else
+    {
+     Lincompatible:
+	incompatibleTypes();
+    }
+Lret:
+    if (!type)
+	type = t;
+    //dump(0);
+    return this;
+
+
+Lt1:
+    e2 = e2->castTo(sc, t1);
+    t = t1;
+    goto Lret;
+
+Lt2:
+    e1 = e1->castTo(sc, t2);
+    t = t2;
+    goto Lret;
+}
+
+/***********************************
+ * Do integral promotions (convertchk).
+ * Don't convert <array of> to <pointer to>
+ */
+
+Expression *Expression::integralPromotions(Scope *sc)
+{   Expression *e;
+
+    e = this;
+    switch (type->toBasetype()->ty)
+    {
+	case Tvoid:
+	    error("void has no value");
+	    break;
+
+	case Tint8:
+	case Tuns8:
+	case Tint16:
+	case Tuns16:
+	case Tbit:
+	case Tbool:
+	case Tchar:
+	case Twchar:
+	    e = e->castTo(sc, Type::tint32);
+	    break;
+
+	case Tdchar:
+	    e = e->castTo(sc, Type::tuns32);
+	    break;
+    }
+    return e;
+}
+
--- a/dmd/class.c	Thu May 01 13:33:02 2008 +0200
+++ b/dmd/class.c	Thu May 01 15:15:28 2008 +0200
@@ -1,1346 +1,1352 @@
-
-// Compiler implementation of the D programming language
-// Copyright (c) 1999-2007 by Digital Mars
-// All Rights Reserved
-// written by Walter Bright
-// http://www.digitalmars.com
-// License for redistribution is by either the Artistic License
-// in artistic.txt, or the GNU General Public License in gnu.txt.
-// See the included readme.txt for details.
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-
-#include "root.h"
-#include "mem.h"
-
-#include "enum.h"
-#include "init.h"
-#include "attrib.h"
-#include "declaration.h"
-#include "aggregate.h"
-#include "id.h"
-#include "mtype.h"
-#include "scope.h"
-#include "module.h"
-#include "expression.h"
-#include "statement.h"
-
-/********************************* ClassDeclaration ****************************/
-
-ClassDeclaration *ClassDeclaration::classinfo;
-ClassDeclaration *ClassDeclaration::object;
-
-ClassDeclaration::ClassDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses)
-    : AggregateDeclaration(loc, id)
-{
-    static char msg[] = "only object.d can define this reserved class name";
-
-    if (baseclasses)
-	this->baseclasses = *baseclasses;
-    baseClass = NULL;
-
-    interfaces_dim = 0;
-    interfaces = NULL;
-
-    vtblInterfaces = NULL;
-
-    //printf("ClassDeclaration(%s), dim = %d\n", id->toChars(), this->baseclasses.dim);
-
-    // For forward references
-    type = new TypeClass(this);
-    handle = type;
-
-    ctor = NULL;
-    defaultCtor = NULL;
-    staticCtor = NULL;
-    staticDtor = NULL;
-
-    vtblsym = NULL;
-    vclassinfo = NULL;
-
-    if (id)
-    {	// Look for special class names
-
-	if (id == Id::__sizeof || id == Id::alignof || id == Id::mangleof)
-	    error("illegal class name");
-
-	// BUG: What if this is the wrong TypeInfo, i.e. it is nested?
-	if (id->toChars()[0] == 'T')
-	{
-	    if (id == Id::TypeInfo)
-	    {	if (Type::typeinfo)
-		    Type::typeinfo->error("%s", msg);
-		Type::typeinfo = this;
-	    }
-
-	    if (id == Id::TypeInfo_Class)
-	    {	if (Type::typeinfoclass)
-		    Type::typeinfoclass->error("%s", msg);
-		Type::typeinfoclass = this;
-	    }
-
-	    if (id == Id::TypeInfo_Interface)
-	    {	if (Type::typeinfointerface)
-		    Type::typeinfointerface->error("%s", msg);
-		Type::typeinfointerface = this;
-	    }
-
-	    if (id == Id::TypeInfo_Struct)
-	    {	if (Type::typeinfostruct)
-		    Type::typeinfostruct->error("%s", msg);
-		Type::typeinfostruct = this;
-	    }
-
-	    if (id == Id::TypeInfo_Typedef)
-	    {	if (Type::typeinfotypedef)
-		    Type::typeinfotypedef->error("%s", msg);
-		Type::typeinfotypedef = this;
-	    }
-
-	    if (id == Id::TypeInfo_Pointer)
-	    {	if (Type::typeinfopointer)
-		    Type::typeinfopointer->error("%s", msg);
-		Type::typeinfopointer = this;
-	    }
-
-	    if (id == Id::TypeInfo_Array)
-	    {	if (Type::typeinfoarray)
-		    Type::typeinfoarray->error("%s", msg);
-		Type::typeinfoarray = this;
-	    }
-
-	    if (id == Id::TypeInfo_StaticArray)
-	    {	//if (Type::typeinfostaticarray)
-		    //Type::typeinfostaticarray->error("%s", msg);
-		Type::typeinfostaticarray = this;
-	    }
-
-	    if (id == Id::TypeInfo_AssociativeArray)
-	    {	if (Type::typeinfoassociativearray)
-		    Type::typeinfoassociativearray->error("%s", msg);
-		Type::typeinfoassociativearray = this;
-	    }
-
-	    if (id == Id::TypeInfo_Enum)
-	    {	if (Type::typeinfoenum)
-		    Type::typeinfoenum->error("%s", msg);
-		Type::typeinfoenum = this;
-	    }
-
-	    if (id == Id::TypeInfo_Function)
-	    {	if (Type::typeinfofunction)
-		    Type::typeinfofunction->error("%s", msg);
-		Type::typeinfofunction = this;
-	    }
-
-	    if (id == Id::TypeInfo_Delegate)
-	    {	if (Type::typeinfodelegate)
-		    Type::typeinfodelegate->error("%s", msg);
-		Type::typeinfodelegate = this;
-	    }
-
-	    if (id == Id::TypeInfo_Tuple)
-	    {	if (Type::typeinfotypelist)
-		    Type::typeinfotypelist->error("%s", msg);
-		Type::typeinfotypelist = this;
-	    }
-
-#if V2
-	    if (id == Id::TypeInfo_Const)
-	    {	if (Type::typeinfoconst)
-		    Type::typeinfoconst->error("%s", msg);
-		Type::typeinfoconst = this;
-	    }
-
-	    if (id == Id::TypeInfo_Invariant)
-	    {	if (Type::typeinfoinvariant)
-		    Type::typeinfoinvariant->error("%s", msg);
-		Type::typeinfoinvariant = this;
-	    }
-#endif
-	}
-
-	if (id == Id::Object)
-	{   if (object)
-		object->error("%s", msg);
-	    object = this;
-	}
-
-	if (id == Id::ClassInfo)
-	{   if (classinfo)
-		classinfo->error("%s", msg);
-	    classinfo = this;
-	}
-
-	if (id == Id::ModuleInfo)
-	{   if (Module::moduleinfo)
-		Module::moduleinfo->error("%s", msg);
-	    Module::moduleinfo = this;
-	}
-    }
-
-    com = 0;
-    isauto = 0;
-    isabstract = 0;
-    isnested = 0;
-    vthis = NULL;
-}
-
-Dsymbol *ClassDeclaration::syntaxCopy(Dsymbol *s)
-{
-    ClassDeclaration *cd;
-
-    //printf("ClassDeclaration::syntaxCopy('%s')\n", toChars());
-    if (s)
-	cd = (ClassDeclaration *)s;
-    else
-	cd = new ClassDeclaration(loc, ident, NULL);
-
-    cd->storage_class |= storage_class;
-
-    cd->baseclasses.setDim(this->baseclasses.dim);
-    for (int i = 0; i < cd->baseclasses.dim; i++)
-    {
-	BaseClass *b = (BaseClass *)this->baseclasses.data[i];
-	BaseClass *b2 = new BaseClass(b->type->syntaxCopy(), b->protection);
-	cd->baseclasses.data[i] = b2;
-    }
-
-    ScopeDsymbol::syntaxCopy(cd);
-    return cd;
-}
-
-void ClassDeclaration::semantic(Scope *sc)
-{   int i;
-    unsigned offset;
-
-    //printf("ClassDeclaration::semantic(%s), type = %p, sizeok = %d, this = %p\n", toChars(), type, sizeok, this);
-    //printf("\tparent = %p, '%s'\n", sc->parent, sc->parent ? sc->parent->toChars() : "");
-    //printf("sc->stc = %x\n", sc->stc);
-
-    //{ static int n;  if (++n == 20) *(char*)0=0; }
-
-    if (!ident)		// if anonymous class
-    {	char *id = "__anonclass";
-
-	ident = Identifier::generateId(id);
-    }
-
-    if (!scope)
-    {
-	if (!parent && sc->parent && !sc->parent->isModule())
-	    parent = sc->parent;
-
-	type = type->semantic(loc, sc);
-	handle = handle->semantic(loc, sc);
-    }
-    if (!members)			// if forward reference
-    {	//printf("\tclass '%s' is forward referenced\n", toChars());
-	return;
-    }
-    if (symtab)
-    {	if (!scope)
-	{   //printf("\tsemantic for '%s' is already completed\n", toChars());
-	    return;		// semantic() already completed
-	}
-    }
-    else
-	symtab = new DsymbolTable();
-
-    Scope *scx = NULL;
-    if (scope)
-    {	sc = scope;
-	scx = scope;		// save so we don't make redundant copies
-	scope = NULL;
-    }
-#ifdef IN_GCC
-    methods.setDim(0);
-#endif
-
-    if (sc->stc & STCdeprecated)
-    {	//printf("test1: %s is deprecated\n", toChars());
-	isdeprecated = 1;
-    }
-
-    // Expand any tuples in baseclasses[]
-    for (i = 0; i < baseclasses.dim; )
-    {	BaseClass *b = (BaseClass *)baseclasses.data[i];
-	b->type = b->type->semantic(loc, sc);
-	Type *tb = b->type->toBasetype();
-
-	if (tb->ty == Ttuple)
-	{   TypeTuple *tup = (TypeTuple *)tb;
-	    enum PROT protection = b->protection;
-	    baseclasses.remove(i);
-	    size_t dim = Argument::dim(tup->arguments);
-	    for (size_t j = 0; j < dim; j++)
-	    {	Argument *arg = Argument::getNth(tup->arguments, j);
-		b = new BaseClass(arg->type, protection);
-		baseclasses.insert(i + j, b);
-	    }
-	}
-	else
-	    i++;
-    }
-
-    // See if there's a base class as first in baseclasses[]
-    if (baseclasses.dim)
-    {	TypeClass *tc;
-	BaseClass *b;
-	Type *tb;
-
-	b = (BaseClass *)baseclasses.data[0];
-	//b->type = b->type->semantic(loc, sc);
-	tb = b->type->toBasetype();
-	if (tb->ty != Tclass)
-	{   error("base type must be class or interface, not %s", b->type->toChars());
-	    baseclasses.remove(0);
-	}
-	else
-	{
-	    tc = (TypeClass *)(tb);
-	    if (tc->sym->isDeprecated())
-	    {
-		if (!isDeprecated())
-		{
-		    // Deriving from deprecated class makes this one deprecated too
-		    isdeprecated = 1;
-
-		    tc->checkDeprecated(loc, sc);
-		}
-	    }
-
-	    if (tc->sym->isInterfaceDeclaration())
-		;
-	    else
-	    {
-		for (ClassDeclaration *cdb = tc->sym; cdb; cdb = cdb->baseClass)
-		{
-		    if (cdb == this)
-		    {
-			error("circular inheritance");
-			baseclasses.remove(0);
-			goto L7;
-		    }
-		}
-		if (!tc->sym->symtab || tc->sym->scope || tc->sym->sizeok == 0)
-		{
-		    //error("forward reference of base class %s", baseClass->toChars());
-		    // Forward reference of base class, try again later
-		    //printf("\ttry later, forward reference of base class %s\n", tc->sym->toChars());
-		    scope = scx ? scx : new Scope(*sc);
-		    scope->setNoFree();
-		    scope->module->addDeferredSemantic(this);
-		    return;
-		}
-		else
-		{   baseClass = tc->sym;
-		    b->base = baseClass;
-		}
-	     L7: ;
-	    }
-	}
-    }
-
-    // Treat the remaining entries in baseclasses as interfaces
-    // Check for errors, handle forward references
-    for (i = (baseClass ? 1 : 0); i < baseclasses.dim; )
-    {	TypeClass *tc;
-	BaseClass *b;
-	Type *tb;
-
-	b = (BaseClass *)baseclasses.data[i];
-	b->type = b->type->semantic(loc, sc);
-	tb = b->type->toBasetype();
-	if (tb->ty == Tclass)
-	    tc = (TypeClass *)tb;
-	else
-	    tc = NULL;
-	if (!tc || !tc->sym->isInterfaceDeclaration())
-	{
-	    error("base type must be interface, not %s", b->type->toChars());
-	    baseclasses.remove(i);
-	    continue;
-	}
-	else
-	{
-	    if (tc->sym->isDeprecated())
-	    {
-		if (!isDeprecated())
-		{
-		    // Deriving from deprecated class makes this one deprecated too
-		    isdeprecated = 1;
-
-		    tc->checkDeprecated(loc, sc);
-		}
-	    }
-
-	    // Check for duplicate interfaces
-	    for (size_t j = (baseClass ? 1 : 0); j < i; j++)
-	    {
-		BaseClass *b2 = (BaseClass *)baseclasses.data[j];
-		if (b2->base == tc->sym)
-		    error("inherits from duplicate interface %s", b2->base->toChars());
-	    }
-
-	    b->base = tc->sym;
-	    if (!b->base->symtab || b->base->scope)
-	    {
-		//error("forward reference of base class %s", baseClass->toChars());
-		// Forward reference of base, try again later
-		//printf("\ttry later, forward reference of base %s\n", baseClass->toChars());
-		scope = scx ? scx : new Scope(*sc);
-		scope->setNoFree();
-		scope->module->addDeferredSemantic(this);
-		return;
-	    }
-	}
-	i++;
-    }
-
-
-    // If no base class, and this is not an Object, use Object as base class
-    if (!baseClass && ident != Id::Object)
-    {
-	// BUG: what if Object is redefined in an inner scope?
-	Type *tbase = new TypeIdentifier(0, Id::Object);
-	BaseClass *b;
-	TypeClass *tc;
-	Type *bt;
-
-	if (!object)
-	{
-	    error("missing or corrupt object.d");
-	    fatal();
-	}
-	bt = tbase->semantic(loc, sc)->toBasetype();
-	b = new BaseClass(bt, PROTpublic);
-	baseclasses.shift(b);
-	assert(b->type->ty == Tclass);
-	tc = (TypeClass *)(b->type);
-	baseClass = tc->sym;
-	assert(!baseClass->isInterfaceDeclaration());
-	b->base = baseClass;
-    }
-
-    interfaces_dim = baseclasses.dim;
-    interfaces = (BaseClass **)baseclasses.data;
-
-
-    if (baseClass)
-    {
-	if (baseClass->storage_class & STCfinal)
-	    error("cannot inherit from final class %s", baseClass->toChars());
-
-	interfaces_dim--;
-	interfaces++;
-
-	// Copy vtbl[] from base class
-	vtbl.setDim(baseClass->vtbl.dim);
-	memcpy(vtbl.data, baseClass->vtbl.data, sizeof(void *) * vtbl.dim);
-
-	// Inherit properties from base class
-	com = baseClass->isCOMclass();
-	isauto = baseClass->isauto;
-	vthis = baseClass->vthis;
-    }
-    else
-    {
-	// No base class, so this is the root of the class hierarchy
-	vtbl.setDim(0);
-	vtbl.push(this);		// leave room for classinfo as first member
-    }
-
-    protection = sc->protection;
-    storage_class |= sc->stc;
-
-    if (sizeok == 0)
-    {
-	interfaceSemantic(sc);
-
-	for (i = 0; i < members->dim; i++)
-	{
-	    Dsymbol *s = (Dsymbol *)members->data[i];
-	    s->addMember(sc, this, 1);
-	}
-
-	/* If this is a nested class, add the hidden 'this'
-	 * member which is a pointer to the enclosing scope.
-	 */
-	if (vthis)		// if inheriting from nested class
-	{   // Use the base class's 'this' member
-	    isnested = 1;
-	    if (storage_class & STCstatic)
-		error("static class cannot inherit from nested class %s", baseClass->toChars());
-	    if (toParent2() != baseClass->toParent2())
-		error("super class %s is nested within %s, not %s",
-			baseClass->toChars(),
-			baseClass->toParent2()->toChars(),
-			toParent2()->toChars());
-	}
-	else if (!(storage_class & STCstatic))
-	{   Dsymbol *s = toParent2();
-	    if (s)
-	    {
-		ClassDeclaration *cd = s->isClassDeclaration();
-		FuncDeclaration *fd = s->isFuncDeclaration();
-
-
-		if (cd || fd)
-		{   isnested = 1;
-		    Type *t;
-		    if (cd)
-			t = cd->type;
-		    else if (fd)
-		    {	AggregateDeclaration *ad = fd->isMember2();
-			if (ad)
-			    t = ad->handle;
-			else
-			{
-			    t = new TypePointer(Type::tvoid);
-			    t = t->semantic(0, sc);
-			}
-		    }
-		    else
-			assert(0);
-		    assert(!vthis);
-		    vthis = new ThisDeclaration(t);
-		    members->push(vthis);
-		}
-	    }
-	}
-    }
-
-    if (storage_class & (STCauto | STCscope))
-	isauto = 1;
-    if (storage_class & STCabstract)
-	isabstract = 1;
-
-    sc = sc->push(this);
-    sc->stc &= ~(STCfinal | STCauto | STCscope | STCstatic |
-		 STCabstract | STCdeprecated);
-    sc->parent = this;
-    sc->inunion = 0;
-
-    if (isCOMclass())
-	sc->linkage = LINKwindows;
-    sc->protection = PROTpublic;
-    sc->explicitProtection = 0;
-    sc->structalign = 8;
-    structalign = sc->structalign;
-    if (baseClass)
-    {	sc->offset = baseClass->structsize;
-	alignsize = baseClass->alignsize;
-//	if (isnested)
-//	    sc->offset += PTRSIZE;	// room for uplevel context pointer
-    }
-    else
-    {	sc->offset = 8;		// allow room for vptr[] and monitor
-	alignsize = 4;
-    }
-    structsize = sc->offset;
-    Scope scsave = *sc;
-    int members_dim = members->dim;
-    sizeok = 0;
-    for (i = 0; i < members_dim; i++)
-    {
-	Dsymbol *s = (Dsymbol *)members->data[i];
-	s->semantic(sc);
-    }
-
-    if (sizeok == 2)
-    {	// semantic() failed because of forward references.
-	// Unwind what we did, and defer it for later
-	fields.setDim(0);
-	structsize = 0;
-	alignsize = 0;
-	structalign = 0;
-
-	sc = sc->pop();
-
-	scope = scx ? scx : new Scope(*sc);
-	scope->setNoFree();
-	scope->module->addDeferredSemantic(this);
-
-	//printf("\tsemantic('%s') failed\n", toChars());
-	return;
-    }
-
-    //printf("\tsemantic('%s') successful\n", toChars());
-
-    structsize = sc->offset;
-    //members->print();
-
-    /* Look for special member functions.
-     * They must be in this class, not in a base class.
-     */
-    ctor = (CtorDeclaration *)search(0, Id::ctor, 0);
-    if (ctor && ctor->toParent() != this)
-	ctor = NULL;
-
-//    dtor = (DtorDeclaration *)search(Id::dtor, 0);
-//    if (dtor && dtor->toParent() != this)
-//	dtor = NULL;
-
-//    inv = (InvariantDeclaration *)search(Id::classInvariant, 0);
-//    if (inv && inv->toParent() != this)
-//	inv = NULL;
-
-    // Can be in base class
-    aggNew    = (NewDeclaration *)search(0, Id::classNew, 0);
-    aggDelete = (DeleteDeclaration *)search(0, Id::classDelete, 0);
-
-    // If this class has no constructor, but base class does, create
-    // a constructor:
-    //    this() { }
-    if (!ctor && baseClass && baseClass->ctor)
-    {
-	//printf("Creating default this(){} for class %s\n", toChars());
-	ctor = new CtorDeclaration(0, 0, NULL, 0);
-	ctor->fbody = new CompoundStatement(0, new Statements());
-	members->push(ctor);
-	ctor->addMember(sc, this, 1);
-	*sc = scsave;	// why? What about sc->nofree?
-	sc->offset = structsize;
-	ctor->semantic(sc);
-	defaultCtor = ctor;
-    }
-
-#if 0
-    if (baseClass)
-    {	if (!aggDelete)
-	    aggDelete = baseClass->aggDelete;
-	if (!aggNew)
-	    aggNew = baseClass->aggNew;
-    }
-#endif
-
-    // Allocate instance of each new interface
-    for (i = 0; i < vtblInterfaces->dim; i++)
-    {
-	BaseClass *b = (BaseClass *)vtblInterfaces->data[i];
-	unsigned thissize = PTRSIZE;
-
-	alignmember(structalign, thissize, &sc->offset);
-	assert(b->offset == 0);
-	b->offset = sc->offset;
-
-	// Take care of single inheritance offsets
-	while (b->baseInterfaces_dim)
-	{
-	    b = &b->baseInterfaces[0];
-	    b->offset = sc->offset;
-	}
-
-	sc->offset += thissize;
-	if (alignsize < thissize)
-	    alignsize = thissize;
-    }
-    structsize = sc->offset;
-    sizeok = 1;
-    Module::dprogress++;
-
-
-    sc->pop();
-
-#if 0 // Do not call until toObjfile() because of forward references
-    // Fill in base class vtbl[]s
-    for (i = 0; i < vtblInterfaces->dim; i++)
-    {
-	BaseClass *b = (BaseClass *)vtblInterfaces->data[i];
-
-	//b->fillVtbl(this, &b->vtbl, 1);
-    }
-#endif
-    //printf("-ClassDeclaration::semantic(%s), type = %p\n", toChars(), type);
-}
-
-void ClassDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    if (!isAnonymous())
-    {
-	buf->printf("%s ", kind());
-	buf->writestring(toChars());
-	if (baseclasses.dim)
-	    buf->writestring(" : ");
-    }
-    for (int i = 0; i < baseclasses.dim; i++)
-    {
-	BaseClass *b = (BaseClass *)baseclasses.data[i];
-
-	if (i)
-	    buf->writeByte(',');
-	//buf->writestring(b->base->ident->toChars());
-	b->type->toCBuffer(buf, NULL, hgs);
-    }
-    buf->writenl();
-    buf->writeByte('{');
-    buf->writenl();
-    for (int i = 0; i < members->dim; i++)
-    {
-	Dsymbol *s = (Dsymbol *)members->data[i];
-
-	buf->writestring("    ");
-	s->toCBuffer(buf, hgs);
-    }
-    buf->writestring("}");
-    buf->writenl();
-}
-
-#if 0
-void ClassDeclaration::defineRef(Dsymbol *s)
-{
-    ClassDeclaration *cd;
-
-    AggregateDeclaration::defineRef(s);
-    cd = s->isClassDeclaration();
-    baseType = cd->baseType;
-    cd->baseType = NULL;
-}
-#endif
-
-/*********************************************
- * Determine if 'this' is a base class of cd.
- * This is used to detect circular inheritance only.
- */
-
-int ClassDeclaration::isBaseOf2(ClassDeclaration *cd)
-{
-    if (!cd)
-	return 0;
-    //printf("ClassDeclaration::isBaseOf2(this = '%s', cd = '%s')\n", toChars(), cd->toChars());
-    for (int i = 0; i < cd->baseclasses.dim; i++)
-    {	BaseClass *b = (BaseClass *)cd->baseclasses.data[i];
-
-	if (b->base == this || isBaseOf2(b->base))
-	    return 1;
-    }
-    return 0;
-}
-
-/*******************************************
- * Determine if 'this' is a base class of cd.
- */
-
-int ClassDeclaration::isBaseOf(ClassDeclaration *cd, int *poffset)
-{
-    //printf("ClassDeclaration::isBaseOf(this = '%s', cd = '%s')\n", toChars(), cd->toChars());
-    if (poffset)
-	*poffset = 0;
-    while (cd)
-    {
-	if (this == cd->baseClass)
-	    return 1;
-
-	/* cd->baseClass might not be set if cd is forward referenced.
-	 */
-	if (!cd->baseClass && cd->baseclasses.dim && !cd->isInterfaceDeclaration())
-	{
-	    cd->error("base class is forward referenced by %s", toChars());
-	}
-
-	cd = cd->baseClass;
-    }
-    return 0;
-}
-
-Dsymbol *ClassDeclaration::search(Loc loc, Identifier *ident, int flags)
-{
-    Dsymbol *s;
-
-    //printf("%s.ClassDeclaration::search('%s')\n", toChars(), ident->toChars());
-    if (scope)
-	semantic(scope);
-
-    if (!members || !symtab || scope)
-    {	error("is forward referenced when looking for '%s'", ident->toChars());
-	//*(char*)0=0;
-	return NULL;
-    }
-
-    s = ScopeDsymbol::search(loc, ident, flags);
-    if (!s)
-    {
-	// Search bases classes in depth-first, left to right order
-
-	int i;
-
-	for (i = 0; i < baseclasses.dim; i++)
-	{
-	    BaseClass *b = (BaseClass *)baseclasses.data[i];
-
-	    if (b->base)
-	    {
-		if (!b->base->symtab)
-		    error("base %s is forward referenced", b->base->ident->toChars());
-		else
-		{
-		    s = b->base->search(loc, ident, flags);
-		    if (s == this)	// happens if s is nested in this and derives from this
-			s = NULL;
-		    else if (s)
-			break;
-		}
-	    }
-	}
-    }
-    return s;
-}
-
-/**********************************************************
- * fd is in the vtbl[] for this class.
- * Return 1 if function is hidden (not findable through search).
- */
-
-#if V2
-int isf(void *param, FuncDeclaration *fd)
-{
-    //printf("param = %p, fd = %p %s\n", param, fd, fd->toChars());
-    return param == fd;
-}
-
-int ClassDeclaration::isFuncHidden(FuncDeclaration *fd)
-{
-    //printf("ClassDeclaration::isFuncHidden(%s)\n", fd->toChars());
-    Dsymbol *s = search(0, fd->ident, 4|2);
-    if (!s)
-    {	//printf("not found\n");
-	/* Because, due to a hack, if there are multiple definitions
-	 * of fd->ident, NULL is returned.
-	 */
-	return 0;
-    }
-    FuncDeclaration *fdstart = s->toAlias()->isFuncDeclaration();
-    //printf("%s fdstart = %p\n", s->kind(), fdstart);
-    return !overloadApply(fdstart, &isf, fd);
-}
-#endif
-
-/****************
- * Find virtual function matching identifier and type.
- * Used to build virtual function tables for interface implementations.
- */
-
-FuncDeclaration *ClassDeclaration::findFunc(Identifier *ident, TypeFunction *tf)
-{
-    //printf("ClassDeclaration::findFunc(%s, %s) %s\n", ident->toChars(), tf->toChars(), toChars());
-
-    ClassDeclaration *cd = this;
-    Array *vtbl = &cd->vtbl;
-    while (1)
-    {
-	for (size_t i = 0; i < vtbl->dim; i++)
-	{
-	    FuncDeclaration *fd = (FuncDeclaration *)vtbl->data[i];
-
-	    //printf("\t[%d] = %s\n", i, fd->toChars());
-	    if (ident == fd->ident &&
-		//tf->equals(fd->type)
-		fd->type->covariant(tf) == 1
-	       )
-	    {   //printf("\t\tfound\n");
-		return fd;
-	    }
-	    //else printf("\t\t%d\n", fd->type->covariant(tf));
-	}
-	if (!cd)
-	    break;
-	vtbl = &cd->vtblFinal;
-	cd = cd->baseClass;
-    }
-
-    return NULL;
-}
-
-void ClassDeclaration::interfaceSemantic(Scope *sc)
-{   int i;
-
-    vtblInterfaces = new BaseClasses();
-    vtblInterfaces->reserve(interfaces_dim);
-
-    for (i = 0; i < interfaces_dim; i++)
-    {
-	BaseClass *b = interfaces[i];
-
-	// If this is an interface, and it derives from a COM interface,
-	// then this is a COM interface too.
-	if (b->base->isCOMinterface())
-	    com = 1;
-
-	vtblInterfaces->push(b);
-	b->copyBaseInterfaces(vtblInterfaces);
-    }
-}
-
-/****************************************
- */
-
-int ClassDeclaration::isCOMclass()
-{
-    return com;
-}
-
-int ClassDeclaration::isCOMinterface()
-{
-    return 0;
-}
-
-
-/****************************************
- */
-
-int ClassDeclaration::isAbstract()
-{
-    if (isabstract)
-	return TRUE;
-    for (int i = 1; i < vtbl.dim; i++)
-    {
-	FuncDeclaration *fd = ((Dsymbol *)vtbl.data[i])->isFuncDeclaration();
-
-	//printf("\tvtbl[%d] = %p\n", i, fd);
-	if (!fd || fd->isAbstract())
-	{
-	    isabstract |= 1;
-	    return TRUE;
-	}
-    }
-    return FALSE;
-}
-
-/****************************************
- * Returns !=0 if there's an extra member which is the 'this'
- * pointer to the enclosing context (enclosing class or function)
- */
-
-int ClassDeclaration::isNested()
-{
-    return isnested;
-}
-
-/****************************************
- * Determine if slot 0 of the vtbl[] is reserved for something else.
- * For class objects, yes, this is where the classinfo ptr goes.
- * For COM interfaces, no.
- * For non-COM interfaces, yes, this is where the Interface ptr goes.
- */
-
-int ClassDeclaration::vtblOffset()
-{
-    return 1;
-}
-
-/****************************************
- */
-
-char *ClassDeclaration::kind()
-{
-    return "class";
-}
-
-/****************************************
- */
-
-void ClassDeclaration::addLocalClass(ClassDeclarations *aclasses)
-{
-    aclasses->push(this);
-}
-
-/********************************* InterfaceDeclaration ****************************/
-
-InterfaceDeclaration::InterfaceDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses)
-    : ClassDeclaration(loc, id, baseclasses)
-{
-    com = 0;
-    if (id == Id::IUnknown)		// IUnknown is the root of all COM objects
-	com = 1;
-}
-
-Dsymbol *InterfaceDeclaration::syntaxCopy(Dsymbol *s)
-{
-    InterfaceDeclaration *id;
-
-    if (s)
-	id = (InterfaceDeclaration *)s;
-    else
-	id = new InterfaceDeclaration(loc, ident, NULL);
-
-    ClassDeclaration::syntaxCopy(id);
-    return id;
-}
-
-void InterfaceDeclaration::semantic(Scope *sc)
-{   int i;
-
-    //printf("InterfaceDeclaration::semantic(%s), type = %p\n", toChars(), type);
-    if (!scope)
-    {	type = type->semantic(loc, sc);
-	handle = handle->semantic(loc, sc);
-    }
-    if (!members)			// if forward reference
-    {	//printf("\tinterface '%s' is forward referenced\n", toChars());
-	return;
-    }
-    if (symtab)			// if already done
-    {	if (!scope)
-	    return;
-    }
-    else
-	symtab = new DsymbolTable();
-
-    Scope *scx = NULL;
-    if (scope)
-    {	sc = scope;
-	scx = scope;		// save so we don't make redundant copies
-	scope = NULL;
-    }
-
-    if (sc->stc & STCdeprecated)
-    {
-	isdeprecated = 1;
-    }
-
-    // Expand any tuples in baseclasses[]
-    for (i = 0; i < baseclasses.dim; )
-    {	BaseClass *b = (BaseClass *)baseclasses.data[0];
-	b->type = b->type->semantic(loc, sc);
-	Type *tb = b->type->toBasetype();
-
-	if (tb->ty == Ttuple)
-	{   TypeTuple *tup = (TypeTuple *)tb;
-	    enum PROT protection = b->protection;
-	    baseclasses.remove(i);
-	    size_t dim = Argument::dim(tup->arguments);
-	    for (size_t j = 0; j < dim; j++)
-	    {	Argument *arg = Argument::getNth(tup->arguments, j);
-		b = new BaseClass(arg->type, protection);
-		baseclasses.insert(i + j, b);
-	    }
-	}
-	else
-	    i++;
-    }
-
-    // Check for errors, handle forward references
-    for (i = 0; i < baseclasses.dim; )
-    {	TypeClass *tc;
-	BaseClass *b;
-	Type *tb;
-
-	b = (BaseClass *)baseclasses.data[i];
-	b->type = b->type->semantic(loc, sc);
-	tb = b->type->toBasetype();
-	if (tb->ty == Tclass)
-	    tc = (TypeClass *)tb;
-	else
-	    tc = NULL;
-	if (!tc || !tc->sym->isInterfaceDeclaration())
-	{
-	    error("base type must be interface, not %s", b->type->toChars());
-	    baseclasses.remove(i);
-	    continue;
-	}
-	else
-	{
-	    // Check for duplicate interfaces
-	    for (size_t j = 0; j < i; j++)
-	    {
-		BaseClass *b2 = (BaseClass *)baseclasses.data[j];
-		if (b2->base == tc->sym)
-		    error("inherits from duplicate interface %s", b2->base->toChars());
-	    }
-
-	    b->base = tc->sym;
-	    if (b->base == this || isBaseOf2(b->base))
-	    {
-		error("circular inheritance of interface");
-		baseclasses.remove(i);
-		continue;
-	    }
-	    if (!b->base->symtab || b->base->scope)
-	    {
-		//error("forward reference of base class %s", baseClass->toChars());
-		// Forward reference of base, try again later
-		//printf("\ttry later, forward reference of base %s\n", b->base->toChars());
-		scope = scx ? scx : new Scope(*sc);
-		scope->setNoFree();
-		scope->module->addDeferredSemantic(this);
-		return;
-	    }
-	}
-	i++;
-    }
-
-    interfaces_dim = baseclasses.dim;
-    interfaces = (BaseClass **)baseclasses.data;
-
-    interfaceSemantic(sc);
-
-    if (vtblOffset())
-	vtbl.push(this);		// leave room at vtbl[0] for classinfo
-
-    // Cat together the vtbl[]'s from base interfaces
-    for (i = 0; i < interfaces_dim; i++)
-    {	BaseClass *b = interfaces[i];
-
-	// Skip if b has already appeared
-	for (int k = 0; k < i; k++)
-	{
-	    if (b == interfaces[i])
-		goto Lcontinue;
-	}
-
-	// Copy vtbl[] from base class
-	if (b->base->vtblOffset())
-	{   int d = b->base->vtbl.dim;
-	    if (d > 1)
-	    {
-		vtbl.reserve(d - 1);
-		for (int j = 1; j < d; j++)
-		    vtbl.push(b->base->vtbl.data[j]);
-	    }
-	}
-	else
-	{
-	    vtbl.append(&b->base->vtbl);
-	}
-
-      Lcontinue:
-	;
-    }
-
-    for (i = 0; i < members->dim; i++)
-    {
-	Dsymbol *s = (Dsymbol *)members->data[i];
-	s->addMember(sc, this, 1);
-    }
-
-    sc = sc->push(this);
-    sc->parent = this;
-    if (isCOMinterface())
-	sc->linkage = LINKwindows;
-    sc->structalign = 8;
-    structalign = sc->structalign;
-    sc->offset = 8;
-    for (i = 0; i < members->dim; i++)
-    {
-	Dsymbol *s = (Dsymbol *)members->data[i];
-	s->semantic(sc);
-    }
-    //members->print();
-    sc->pop();
-    //printf("-InterfaceDeclaration::semantic(%s), type = %p\n", toChars(), type);
-}
-
-
-/*******************************************
- * Determine if 'this' is a base class of cd.
- * (Actually, if it is an interface supported by cd)
- * Output:
- *	*poffset	offset to start of class
- *			OFFSET_RUNTIME	must determine offset at runtime
- * Returns:
- *	0	not a base
- *	1	is a base
- */
-
-int InterfaceDeclaration::isBaseOf(ClassDeclaration *cd, int *poffset)
-{
-    unsigned j;
-
-    //printf("%s.InterfaceDeclaration::isBaseOf(cd = '%s')\n", toChars(), cd->toChars());
-    assert(!baseClass);
-    for (j = 0; j < cd->interfaces_dim; j++)
-    {
-	BaseClass *b = cd->interfaces[j];
-
-	//printf("\tbase %s\n", b->base->toChars());
-	if (this == b->base)
-	{
-	    //printf("\tfound at offset %d\n", b->offset);
-	    if (poffset)
-	    {	*poffset = b->offset;
-		if (j && cd->isInterfaceDeclaration())
-		    *poffset = OFFSET_RUNTIME;
-	    }
-	    return 1;
-	}
-	if (isBaseOf(b, poffset))
-	{   if (j && poffset && cd->isInterfaceDeclaration())
-		*poffset = OFFSET_RUNTIME;
-	    return 1;
-	}
-    }
-
-    if (cd->baseClass && isBaseOf(cd->baseClass, poffset))
-	return 1;
-
-    if (poffset)
-	*poffset = 0;
-    return 0;
-}
-
-
-int InterfaceDeclaration::isBaseOf(BaseClass *bc, int *poffset)
-{
-    //printf("%s.InterfaceDeclaration::isBaseOf(bc = '%s')\n", toChars(), bc->base->toChars());
-    for (unsigned j = 0; j < bc->baseInterfaces_dim; j++)
-    {
-	BaseClass *b = &bc->baseInterfaces[j];
-
-	if (this == b->base)
-	{
-	    if (poffset)
-	    {	*poffset = b->offset;
-	    }
-	    return 1;
-	}
-	if (isBaseOf(b, poffset))
-	{
-	    return 1;
-	}
-    }
-    if (poffset)
-	*poffset = 0;
-    return 0;
-}
-
-/****************************************
- * Determine if slot 0 of the vtbl[] is reserved for something else.
- * For class objects, yes, this is where the ClassInfo ptr goes.
- * For COM interfaces, no.
- * For non-COM interfaces, yes, this is where the Interface ptr goes.
- */
-
-int InterfaceDeclaration::vtblOffset()
-{
-    if (isCOMinterface())
-	return 0;
-    return 1;
-}
-
-int InterfaceDeclaration::isCOMinterface()
-{
-    return com;
-}
-
-/*******************************************
- */
-
-char *InterfaceDeclaration::kind()
-{
-    return "interface";
-}
-
-
-/******************************** BaseClass *****************************/
-
-BaseClass::BaseClass()
-{
-    memset(this, 0, sizeof(BaseClass));
-}
-
-BaseClass::BaseClass(Type *type, enum PROT protection)
-{
-    //printf("BaseClass(this = %p, '%s')\n", this, type->toChars());
-    this->type = type;
-    this->protection = protection;
-    base = NULL;
-    offset = 0;
-
-    baseInterfaces_dim = 0;
-    baseInterfaces = NULL;
-}
-
-/****************************************
- * Fill in vtbl[] for base class based on member functions of class cd.
- * Input:
- *	vtbl		if !=NULL, fill it in
- *	newinstance	!=0 means all entries must be filled in by members
- *			of cd, not members of any base classes of cd.
- * Returns:
- *	!=0 if any entries were filled in by members of cd (not exclusively
- *	by base classes)
- */
-
-int BaseClass::fillVtbl(ClassDeclaration *cd, Array *vtbl, int newinstance)
-{
-    ClassDeclaration *id = base;
-    int j;
-    int result = 0;
-
-    //printf("BaseClass::fillVtbl(this='%s', cd='%s')\n", base->toChars(), cd->toChars());
-    if (vtbl)
-	vtbl->setDim(base->vtbl.dim);
-
-    // first entry is ClassInfo reference
-    for (j = base->vtblOffset(); j < base->vtbl.dim; j++)
-    {
-	FuncDeclaration *ifd = ((Dsymbol *)base->vtbl.data[j])->isFuncDeclaration();
-	FuncDeclaration *fd;
-	TypeFunction *tf;
-
-	//printf("        vtbl[%d] is '%s'\n", j, ifd ? ifd->toChars() : "null");
-
-	assert(ifd);
-	// Find corresponding function in this class
-	tf = (ifd->type->ty == Tfunction) ? (TypeFunction *)(ifd->type) : NULL;
-	fd = cd->findFunc(ifd->ident, tf);
-	if (fd && !fd->isAbstract())
-	{
-	    //printf("            found\n");
-	    // Check that calling conventions match
-	    if (fd->linkage != ifd->linkage)
-		fd->error("linkage doesn't match interface function");
-
-	    // Check that it is current
-	    if (newinstance &&
-		fd->toParent() != cd &&
-		ifd->toParent() == base)
-		cd->error("interface function %s.%s is not implemented",
-		    id->toChars(), ifd->ident->toChars());
-
-	    if (fd->toParent() == cd)
-		result = 1;
-	}
-	else
-	{
-	    //printf("            not found\n");
-	    // BUG: should mark this class as abstract?
-	    if (!cd->isAbstract())
-		cd->error("interface function %s.%s isn't implemented",
-		    id->toChars(), ifd->ident->toChars());
-	    fd = NULL;
-	}
-	if (vtbl)
-	    vtbl->data[j] = fd;
-    }
-
-    return result;
-}
-
-void BaseClass::copyBaseInterfaces(BaseClasses *vtblInterfaces)
-{
-    //printf("+copyBaseInterfaces(), %s\n", base->toChars());
-//    if (baseInterfaces_dim)
-//	return;
-
-    baseInterfaces_dim = base->interfaces_dim;
-    baseInterfaces = (BaseClass *)mem.calloc(baseInterfaces_dim, sizeof(BaseClass));
-
-    //printf("%s.copyBaseInterfaces()\n", base->toChars());
-    for (int i = 0; i < baseInterfaces_dim; i++)
-    {
-	BaseClass *b = &baseInterfaces[i];
-	BaseClass *b2 = base->interfaces[i];
-
-	assert(b2->vtbl.dim == 0);	// should not be filled yet
-	memcpy(b, b2, sizeof(BaseClass));
-
-	if (i)				// single inheritance is i==0
-	    vtblInterfaces->push(b);	// only need for M.I.
-	b->copyBaseInterfaces(vtblInterfaces);
-    }
-    //printf("-copyBaseInterfaces\n");
-}
+
+// Compiler implementation of the D programming language
+// Copyright (c) 1999-2008 by Digital Mars
+// All Rights Reserved
+// written by Walter Bright
+// http://www.digitalmars.com
+// License for redistribution is by either the Artistic License
+// in artistic.txt, or the GNU General Public License in gnu.txt.
+// See the included readme.txt for details.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "root.h"
+#include "mem.h"
+
+#include "enum.h"
+#include "init.h"
+#include "attrib.h"
+#include "declaration.h"
+#include "aggregate.h"
+#include "id.h"
+#include "mtype.h"
+#include "scope.h"
+#include "module.h"
+#include "expression.h"
+#include "statement.h"
+
+/********************************* ClassDeclaration ****************************/
+
+ClassDeclaration *ClassDeclaration::classinfo;
+ClassDeclaration *ClassDeclaration::object;
+
+ClassDeclaration::ClassDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses)
+    : AggregateDeclaration(loc, id)
+{
+    static char msg[] = "only object.d can define this reserved class name";
+
+    if (baseclasses)
+	this->baseclasses = *baseclasses;
+    baseClass = NULL;
+
+    interfaces_dim = 0;
+    interfaces = NULL;
+
+    vtblInterfaces = NULL;
+
+    //printf("ClassDeclaration(%s), dim = %d\n", id->toChars(), this->baseclasses.dim);
+
+    // For forward references
+    type = new TypeClass(this);
+    handle = type;
+
+    ctor = NULL;
+    defaultCtor = NULL;
+    staticCtor = NULL;
+    staticDtor = NULL;
+
+    vtblsym = NULL;
+    vclassinfo = NULL;
+
+    if (id)
+    {	// Look for special class names
+
+	if (id == Id::__sizeof || id == Id::alignof || id == Id::mangleof)
+	    error("illegal class name");
+
+	// BUG: What if this is the wrong TypeInfo, i.e. it is nested?
+	if (id->toChars()[0] == 'T')
+	{
+	    if (id == Id::TypeInfo)
+	    {	if (Type::typeinfo)
+		    Type::typeinfo->error("%s", msg);
+		Type::typeinfo = this;
+	    }
+
+	    if (id == Id::TypeInfo_Class)
+	    {	if (Type::typeinfoclass)
+		    Type::typeinfoclass->error("%s", msg);
+		Type::typeinfoclass = this;
+	    }
+
+	    if (id == Id::TypeInfo_Interface)
+	    {	if (Type::typeinfointerface)
+		    Type::typeinfointerface->error("%s", msg);
+		Type::typeinfointerface = this;
+	    }
+
+	    if (id == Id::TypeInfo_Struct)
+	    {	if (Type::typeinfostruct)
+		    Type::typeinfostruct->error("%s", msg);
+		Type::typeinfostruct = this;
+	    }
+
+	    if (id == Id::TypeInfo_Typedef)
+	    {	if (Type::typeinfotypedef)
+		    Type::typeinfotypedef->error("%s", msg);
+		Type::typeinfotypedef = this;
+	    }
+
+	    if (id == Id::TypeInfo_Pointer)
+	    {	if (Type::typeinfopointer)
+		    Type::typeinfopointer->error("%s", msg);
+		Type::typeinfopointer = this;
+	    }
+
+	    if (id == Id::TypeInfo_Array)
+	    {	if (Type::typeinfoarray)
+		    Type::typeinfoarray->error("%s", msg);
+		Type::typeinfoarray = this;
+	    }
+
+	    if (id == Id::TypeInfo_StaticArray)
+	    {	//if (Type::typeinfostaticarray)
+		    //Type::typeinfostaticarray->error("%s", msg);
+		Type::typeinfostaticarray = this;
+	    }
+
+	    if (id == Id::TypeInfo_AssociativeArray)
+	    {	if (Type::typeinfoassociativearray)
+		    Type::typeinfoassociativearray->error("%s", msg);
+		Type::typeinfoassociativearray = this;
+	    }
+
+	    if (id == Id::TypeInfo_Enum)
+	    {	if (Type::typeinfoenum)
+		    Type::typeinfoenum->error("%s", msg);
+		Type::typeinfoenum = this;
+	    }
+
+	    if (id == Id::TypeInfo_Function)
+	    {	if (Type::typeinfofunction)
+		    Type::typeinfofunction->error("%s", msg);
+		Type::typeinfofunction = this;
+	    }
+
+	    if (id == Id::TypeInfo_Delegate)
+	    {	if (Type::typeinfodelegate)
+		    Type::typeinfodelegate->error("%s", msg);
+		Type::typeinfodelegate = this;
+	    }
+
+	    if (id == Id::TypeInfo_Tuple)
+	    {	if (Type::typeinfotypelist)
+		    Type::typeinfotypelist->error("%s", msg);
+		Type::typeinfotypelist = this;
+	    }
+
+#if V2
+	    if (id == Id::TypeInfo_Const)
+	    {	if (Type::typeinfoconst)
+		    Type::typeinfoconst->error("%s", msg);
+		Type::typeinfoconst = this;
+	    }
+
+	    if (id == Id::TypeInfo_Invariant)
+	    {	if (Type::typeinfoinvariant)
+		    Type::typeinfoinvariant->error("%s", msg);
+		Type::typeinfoinvariant = this;
+	    }
+#endif
+	}
+
+	if (id == Id::Object)
+	{   if (object)
+		object->error("%s", msg);
+	    object = this;
+	}
+
+	if (id == Id::ClassInfo)
+	{   if (classinfo)
+		classinfo->error("%s", msg);
+	    classinfo = this;
+	}
+
+	if (id == Id::ModuleInfo)
+	{   if (Module::moduleinfo)
+		Module::moduleinfo->error("%s", msg);
+	    Module::moduleinfo = this;
+	}
+    }
+
+    com = 0;
+    isauto = 0;
+    isabstract = 0;
+    isnested = 0;
+    vthis = NULL;
+    inuse = 0;
+}
+
+Dsymbol *ClassDeclaration::syntaxCopy(Dsymbol *s)
+{
+    ClassDeclaration *cd;
+
+    //printf("ClassDeclaration::syntaxCopy('%s')\n", toChars());
+    if (s)
+	cd = (ClassDeclaration *)s;
+    else
+	cd = new ClassDeclaration(loc, ident, NULL);
+
+    cd->storage_class |= storage_class;
+
+    cd->baseclasses.setDim(this->baseclasses.dim);
+    for (int i = 0; i < cd->baseclasses.dim; i++)
+    {
+	BaseClass *b = (BaseClass *)this->baseclasses.data[i];
+	BaseClass *b2 = new BaseClass(b->type->syntaxCopy(), b->protection);
+	cd->baseclasses.data[i] = b2;
+    }
+
+    ScopeDsymbol::syntaxCopy(cd);
+    return cd;
+}
+
+void ClassDeclaration::semantic(Scope *sc)
+{   int i;
+    unsigned offset;
+
+    //printf("ClassDeclaration::semantic(%s), type = %p, sizeok = %d, this = %p\n", toChars(), type, sizeok, this);
+    //printf("\tparent = %p, '%s'\n", sc->parent, sc->parent ? sc->parent->toChars() : "");
+    //printf("sc->stc = %x\n", sc->stc);
+
+    //{ static int n;  if (++n == 20) *(char*)0=0; }
+
+    if (!ident)		// if anonymous class
+    {	char *id = "__anonclass";
+
+	ident = Identifier::generateId(id);
+    }
+
+    if (!scope)
+    {
+	if (!parent && sc->parent && !sc->parent->isModule())
+	    parent = sc->parent;
+
+	type = type->semantic(loc, sc);
+	handle = handle->semantic(loc, sc);
+    }
+    if (!members)			// if forward reference
+    {	//printf("\tclass '%s' is forward referenced\n", toChars());
+	return;
+    }
+    if (symtab)
+    {	if (!scope)
+	{   //printf("\tsemantic for '%s' is already completed\n", toChars());
+	    return;		// semantic() already completed
+	}
+    }
+    else
+	symtab = new DsymbolTable();
+
+    Scope *scx = NULL;
+    if (scope)
+    {	sc = scope;
+	scx = scope;		// save so we don't make redundant copies
+	scope = NULL;
+    }
+#ifdef IN_GCC
+    methods.setDim(0);
+#endif
+
+    if (sc->stc & STCdeprecated)
+    {	//printf("test1: %s is deprecated\n", toChars());
+	isdeprecated = 1;
+    }
+
+    // Expand any tuples in baseclasses[]
+    for (i = 0; i < baseclasses.dim; )
+    {	BaseClass *b = (BaseClass *)baseclasses.data[i];
+	b->type = b->type->semantic(loc, sc);
+	Type *tb = b->type->toBasetype();
+
+	if (tb->ty == Ttuple)
+	{   TypeTuple *tup = (TypeTuple *)tb;
+	    enum PROT protection = b->protection;
+	    baseclasses.remove(i);
+	    size_t dim = Argument::dim(tup->arguments);
+	    for (size_t j = 0; j < dim; j++)
+	    {	Argument *arg = Argument::getNth(tup->arguments, j);
+		b = new BaseClass(arg->type, protection);
+		baseclasses.insert(i + j, b);
+	    }
+	}
+	else
+	    i++;
+    }
+
+    // See if there's a base class as first in baseclasses[]
+    if (baseclasses.dim)
+    {	TypeClass *tc;
+	BaseClass *b;
+	Type *tb;
+
+	b = (BaseClass *)baseclasses.data[0];
+	//b->type = b->type->semantic(loc, sc);
+	tb = b->type->toBasetype();
+	if (tb->ty != Tclass)
+	{   error("base type must be class or interface, not %s", b->type->toChars());
+	    baseclasses.remove(0);
+	}
+	else
+	{
+	    tc = (TypeClass *)(tb);
+	    if (tc->sym->isDeprecated())
+	    {
+		if (!isDeprecated())
+		{
+		    // Deriving from deprecated class makes this one deprecated too
+		    isdeprecated = 1;
+
+		    tc->checkDeprecated(loc, sc);
+		}
+	    }
+
+	    if (tc->sym->isInterfaceDeclaration())
+		;
+	    else
+	    {
+		for (ClassDeclaration *cdb = tc->sym; cdb; cdb = cdb->baseClass)
+		{
+		    if (cdb == this)
+		    {
+			error("circular inheritance");
+			baseclasses.remove(0);
+			goto L7;
+		    }
+		}
+		if (!tc->sym->symtab || tc->sym->scope || tc->sym->sizeok == 0)
+		{
+		    //error("forward reference of base class %s", baseClass->toChars());
+		    // Forward reference of base class, try again later
+		    //printf("\ttry later, forward reference of base class %s\n", tc->sym->toChars());
+		    scope = scx ? scx : new Scope(*sc);
+		    scope->setNoFree();
+		    scope->module->addDeferredSemantic(this);
+		    return;
+		}
+		else
+		{   baseClass = tc->sym;
+		    b->base = baseClass;
+		}
+	     L7: ;
+	    }
+	}
+    }
+
+    // Treat the remaining entries in baseclasses as interfaces
+    // Check for errors, handle forward references
+    for (i = (baseClass ? 1 : 0); i < baseclasses.dim; )
+    {	TypeClass *tc;
+	BaseClass *b;
+	Type *tb;
+
+	b = (BaseClass *)baseclasses.data[i];
+	b->type = b->type->semantic(loc, sc);
+	tb = b->type->toBasetype();
+	if (tb->ty == Tclass)
+	    tc = (TypeClass *)tb;
+	else
+	    tc = NULL;
+	if (!tc || !tc->sym->isInterfaceDeclaration())
+	{
+	    error("base type must be interface, not %s", b->type->toChars());
+	    baseclasses.remove(i);
+	    continue;
+	}
+	else
+	{
+	    if (tc->sym->isDeprecated())
+	    {
+		if (!isDeprecated())
+		{
+		    // Deriving from deprecated class makes this one deprecated too
+		    isdeprecated = 1;
+
+		    tc->checkDeprecated(loc, sc);
+		}
+	    }
+
+	    // Check for duplicate interfaces
+	    for (size_t j = (baseClass ? 1 : 0); j < i; j++)
+	    {
+		BaseClass *b2 = (BaseClass *)baseclasses.data[j];
+		if (b2->base == tc->sym)
+		    error("inherits from duplicate interface %s", b2->base->toChars());
+	    }
+
+	    b->base = tc->sym;
+	    if (!b->base->symtab || b->base->scope)
+	    {
+		//error("forward reference of base class %s", baseClass->toChars());
+		// Forward reference of base, try again later
+		//printf("\ttry later, forward reference of base %s\n", baseClass->toChars());
+		scope = scx ? scx : new Scope(*sc);
+		scope->setNoFree();
+		scope->module->addDeferredSemantic(this);
+		return;
+	    }
+	}
+	i++;
+    }
+
+
+    // If no base class, and this is not an Object, use Object as base class
+    if (!baseClass && ident != Id::Object)
+    {
+	// BUG: what if Object is redefined in an inner scope?
+	Type *tbase = new TypeIdentifier(0, Id::Object);
+	BaseClass *b;
+	TypeClass *tc;
+	Type *bt;
+
+	if (!object)
+	{
+	    error("missing or corrupt object.d");
+	    fatal();
+	}
+	bt = tbase->semantic(loc, sc)->toBasetype();
+	b = new BaseClass(bt, PROTpublic);
+	baseclasses.shift(b);
+	assert(b->type->ty == Tclass);
+	tc = (TypeClass *)(b->type);
+	baseClass = tc->sym;
+	assert(!baseClass->isInterfaceDeclaration());
+	b->base = baseClass;
+    }
+
+    interfaces_dim = baseclasses.dim;
+    interfaces = (BaseClass **)baseclasses.data;
+
+
+    if (baseClass)
+    {
+	if (baseClass->storage_class & STCfinal)
+	    error("cannot inherit from final class %s", baseClass->toChars());
+
+	interfaces_dim--;
+	interfaces++;
+
+	// Copy vtbl[] from base class
+	vtbl.setDim(baseClass->vtbl.dim);
+	memcpy(vtbl.data, baseClass->vtbl.data, sizeof(void *) * vtbl.dim);
+
+	// Inherit properties from base class
+	com = baseClass->isCOMclass();
+	isauto = baseClass->isauto;
+	vthis = baseClass->vthis;
+    }
+    else
+    {
+	// No base class, so this is the root of the class hierarchy
+	vtbl.setDim(0);
+	vtbl.push(this);		// leave room for classinfo as first member
+    }
+
+    protection = sc->protection;
+    storage_class |= sc->stc;
+
+    if (sizeok == 0)
+    {
+	interfaceSemantic(sc);
+
+	for (i = 0; i < members->dim; i++)
+	{
+	    Dsymbol *s = (Dsymbol *)members->data[i];
+	    s->addMember(sc, this, 1);
+	}
+
+	/* If this is a nested class, add the hidden 'this'
+	 * member which is a pointer to the enclosing scope.
+	 */
+	if (vthis)		// if inheriting from nested class
+	{   // Use the base class's 'this' member
+	    isnested = 1;
+	    if (storage_class & STCstatic)
+		error("static class cannot inherit from nested class %s", baseClass->toChars());
+	    if (toParent2() != baseClass->toParent2())
+		error("super class %s is nested within %s, not %s",
+			baseClass->toChars(),
+			baseClass->toParent2()->toChars(),
+			toParent2()->toChars());
+	}
+	else if (!(storage_class & STCstatic))
+	{   Dsymbol *s = toParent2();
+	    if (s)
+	    {
+		ClassDeclaration *cd = s->isClassDeclaration();
+		FuncDeclaration *fd = s->isFuncDeclaration();
+
+
+		if (cd || fd)
+		{   isnested = 1;
+		    Type *t;
+		    if (cd)
+			t = cd->type;
+		    else if (fd)
+		    {	AggregateDeclaration *ad = fd->isMember2();
+			if (ad)
+			    t = ad->handle;
+			else
+			{
+			    t = new TypePointer(Type::tvoid);
+			    t = t->semantic(0, sc);
+			}
+		    }
+		    else
+			assert(0);
+		    assert(!vthis);
+		    vthis = new ThisDeclaration(t);
+		    members->push(vthis);
+		}
+	    }
+	}
+    }
+
+    if (storage_class & (STCauto | STCscope))
+	isauto = 1;
+    if (storage_class & STCabstract)
+	isabstract = 1;
+
+    sc = sc->push(this);
+    sc->stc &= ~(STCfinal | STCauto | STCscope | STCstatic |
+		 STCabstract | STCdeprecated);
+    sc->parent = this;
+    sc->inunion = 0;
+
+    if (isCOMclass())
+	sc->linkage = LINKwindows;
+    sc->protection = PROTpublic;
+    sc->explicitProtection = 0;
+    sc->structalign = 8;
+    structalign = sc->structalign;
+    if (baseClass)
+    {	sc->offset = baseClass->structsize;
+	alignsize = baseClass->alignsize;
+//	if (isnested)
+//	    sc->offset += PTRSIZE;	// room for uplevel context pointer
+    }
+    else
+    {	sc->offset = 8;		// allow room for vptr[] and monitor
+	alignsize = 4;
+    }
+    structsize = sc->offset;
+    Scope scsave = *sc;
+    int members_dim = members->dim;
+    sizeok = 0;
+    for (i = 0; i < members_dim; i++)
+    {
+	Dsymbol *s = (Dsymbol *)members->data[i];
+	s->semantic(sc);
+    }
+
+    if (sizeok == 2)
+    {	// semantic() failed because of forward references.
+	// Unwind what we did, and defer it for later
+	fields.setDim(0);
+	structsize = 0;
+	alignsize = 0;
+	structalign = 0;
+
+	sc = sc->pop();
+
+	scope = scx ? scx : new Scope(*sc);
+	scope->setNoFree();
+	scope->module->addDeferredSemantic(this);
+
+	//printf("\tsemantic('%s') failed\n", toChars());
+	return;
+    }
+
+    //printf("\tsemantic('%s') successful\n", toChars());
+
+    structsize = sc->offset;
+    //members->print();
+
+    /* Look for special member functions.
+     * They must be in this class, not in a base class.
+     */
+    ctor = (CtorDeclaration *)search(0, Id::ctor, 0);
+    if (ctor && ctor->toParent() != this)
+	ctor = NULL;
+
+//    dtor = (DtorDeclaration *)search(Id::dtor, 0);
+//    if (dtor && dtor->toParent() != this)
+//	dtor = NULL;
+
+//    inv = (InvariantDeclaration *)search(Id::classInvariant, 0);
+//    if (inv && inv->toParent() != this)
+//	inv = NULL;
+
+    // Can be in base class
+    aggNew    = (NewDeclaration *)search(0, Id::classNew, 0);
+    aggDelete = (DeleteDeclaration *)search(0, Id::classDelete, 0);
+
+    // If this class has no constructor, but base class does, create
+    // a constructor:
+    //    this() { }
+    if (!ctor && baseClass && baseClass->ctor)
+    {
+	//printf("Creating default this(){} for class %s\n", toChars());
+	ctor = new CtorDeclaration(0, 0, NULL, 0);
+	ctor->fbody = new CompoundStatement(0, new Statements());
+	members->push(ctor);
+	ctor->addMember(sc, this, 1);
+	*sc = scsave;	// why? What about sc->nofree?
+	sc->offset = structsize;
+	ctor->semantic(sc);
+	defaultCtor = ctor;
+    }
+
+#if 0
+    if (baseClass)
+    {	if (!aggDelete)
+	    aggDelete = baseClass->aggDelete;
+	if (!aggNew)
+	    aggNew = baseClass->aggNew;
+    }
+#endif
+
+    // Allocate instance of each new interface
+    for (i = 0; i < vtblInterfaces->dim; i++)
+    {
+	BaseClass *b = (BaseClass *)vtblInterfaces->data[i];
+	unsigned thissize = PTRSIZE;
+
+	alignmember(structalign, thissize, &sc->offset);
+	assert(b->offset == 0);
+	b->offset = sc->offset;
+
+	// Take care of single inheritance offsets
+	while (b->baseInterfaces_dim)
+	{
+	    b = &b->baseInterfaces[0];
+	    b->offset = sc->offset;
+	}
+
+	sc->offset += thissize;
+	if (alignsize < thissize)
+	    alignsize = thissize;
+    }
+    structsize = sc->offset;
+    sizeok = 1;
+    Module::dprogress++;
+
+    dtor = buildDtor(sc);
+
+    sc->pop();
+
+#if 0 // Do not call until toObjfile() because of forward references
+    // Fill in base class vtbl[]s
+    for (i = 0; i < vtblInterfaces->dim; i++)
+    {
+	BaseClass *b = (BaseClass *)vtblInterfaces->data[i];
+
+	//b->fillVtbl(this, &b->vtbl, 1);
+    }
+#endif
+    //printf("-ClassDeclaration::semantic(%s), type = %p\n", toChars(), type);
+}
+
+void ClassDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    if (!isAnonymous())
+    {
+	buf->printf("%s ", kind());
+	buf->writestring(toChars());
+	if (baseclasses.dim)
+	    buf->writestring(" : ");
+    }
+    for (int i = 0; i < baseclasses.dim; i++)
+    {
+	BaseClass *b = (BaseClass *)baseclasses.data[i];
+
+	if (i)
+	    buf->writeByte(',');
+	//buf->writestring(b->base->ident->toChars());
+	b->type->toCBuffer(buf, NULL, hgs);
+    }
+    buf->writenl();
+    buf->writeByte('{');
+    buf->writenl();
+    for (int i = 0; i < members->dim; i++)
+    {
+	Dsymbol *s = (Dsymbol *)members->data[i];
+
+	buf->writestring("    ");
+	s->toCBuffer(buf, hgs);
+    }
+    buf->writestring("}");
+    buf->writenl();
+}
+
+#if 0
+void ClassDeclaration::defineRef(Dsymbol *s)
+{
+    ClassDeclaration *cd;
+
+    AggregateDeclaration::defineRef(s);
+    cd = s->isClassDeclaration();
+    baseType = cd->baseType;
+    cd->baseType = NULL;
+}
+#endif
+
+/*********************************************
+ * Determine if 'this' is a base class of cd.
+ * This is used to detect circular inheritance only.
+ */
+
+int ClassDeclaration::isBaseOf2(ClassDeclaration *cd)
+{
+    if (!cd)
+	return 0;
+    //printf("ClassDeclaration::isBaseOf2(this = '%s', cd = '%s')\n", toChars(), cd->toChars());
+    for (int i = 0; i < cd->baseclasses.dim; i++)
+    {	BaseClass *b = (BaseClass *)cd->baseclasses.data[i];
+
+	if (b->base == this || isBaseOf2(b->base))
+	    return 1;
+    }
+    return 0;
+}
+
+/*******************************************
+ * Determine if 'this' is a base class of cd.
+ */
+
+int ClassDeclaration::isBaseOf(ClassDeclaration *cd, int *poffset)
+{
+    //printf("ClassDeclaration::isBaseOf(this = '%s', cd = '%s')\n", toChars(), cd->toChars());
+    if (poffset)
+	*poffset = 0;
+    while (cd)
+    {
+	if (this == cd->baseClass)
+	    return 1;
+
+	/* cd->baseClass might not be set if cd is forward referenced.
+	 */
+	if (!cd->baseClass && cd->baseclasses.dim && !cd->isInterfaceDeclaration())
+	{
+	    cd->error("base class is forward referenced by %s", toChars());
+	}
+
+	cd = cd->baseClass;
+    }
+    return 0;
+}
+
+Dsymbol *ClassDeclaration::search(Loc loc, Identifier *ident, int flags)
+{
+    Dsymbol *s;
+
+    //printf("%s.ClassDeclaration::search('%s')\n", toChars(), ident->toChars());
+    if (scope)
+	semantic(scope);
+
+    if (!members || !symtab || scope)
+    {	error("is forward referenced when looking for '%s'", ident->toChars());
+	//*(char*)0=0;
+	return NULL;
+    }
+
+    s = ScopeDsymbol::search(loc, ident, flags);
+    if (!s)
+    {
+	// Search bases classes in depth-first, left to right order
+
+	int i;
+
+	for (i = 0; i < baseclasses.dim; i++)
+	{
+	    BaseClass *b = (BaseClass *)baseclasses.data[i];
+
+	    if (b->base)
+	    {
+		if (!b->base->symtab)
+		    error("base %s is forward referenced", b->base->ident->toChars());
+		else
+		{
+		    s = b->base->search(loc, ident, flags);
+		    if (s == this)	// happens if s is nested in this and derives from this
+			s = NULL;
+		    else if (s)
+			break;
+		}
+	    }
+	}
+    }
+    return s;
+}
+
+/**********************************************************
+ * fd is in the vtbl[] for this class.
+ * Return 1 if function is hidden (not findable through search).
+ */
+
+#if V2
+int isf(void *param, FuncDeclaration *fd)
+{
+    //printf("param = %p, fd = %p %s\n", param, fd, fd->toChars());
+    return param == fd;
+}
+
+int ClassDeclaration::isFuncHidden(FuncDeclaration *fd)
+{
+    //printf("ClassDeclaration::isFuncHidden(%s)\n", fd->toChars());
+    Dsymbol *s = search(0, fd->ident, 4|2);
+    if (!s)
+    {	//printf("not found\n");
+	/* Because, due to a hack, if there are multiple definitions
+	 * of fd->ident, NULL is returned.
+	 */
+	return 0;
+    }
+    FuncDeclaration *fdstart = s->toAlias()->isFuncDeclaration();
+    //printf("%s fdstart = %p\n", s->kind(), fdstart);
+    return !overloadApply(fdstart, &isf, fd);
+}
+#endif
+
+/****************
+ * Find virtual function matching identifier and type.
+ * Used to build virtual function tables for interface implementations.
+ */
+
+FuncDeclaration *ClassDeclaration::findFunc(Identifier *ident, TypeFunction *tf)
+{
+    //printf("ClassDeclaration::findFunc(%s, %s) %s\n", ident->toChars(), tf->toChars(), toChars());
+
+    ClassDeclaration *cd = this;
+    Array *vtbl = &cd->vtbl;
+    while (1)
+    {
+	for (size_t i = 0; i < vtbl->dim; i++)
+	{
+	    FuncDeclaration *fd = (FuncDeclaration *)vtbl->data[i];
+
+	    //printf("\t[%d] = %s\n", i, fd->toChars());
+	    if (ident == fd->ident &&
+		//tf->equals(fd->type)
+		fd->type->covariant(tf) == 1
+	       )
+	    {   //printf("\t\tfound\n");
+		return fd;
+	    }
+	    //else printf("\t\t%d\n", fd->type->covariant(tf));
+	}
+	if (!cd)
+	    break;
+	vtbl = &cd->vtblFinal;
+	cd = cd->baseClass;
+    }
+
+    return NULL;
+}
+
+void ClassDeclaration::interfaceSemantic(Scope *sc)
+{   int i;
+
+    vtblInterfaces = new BaseClasses();
+    vtblInterfaces->reserve(interfaces_dim);
+
+    for (i = 0; i < interfaces_dim; i++)
+    {
+	BaseClass *b = interfaces[i];
+
+	// If this is an interface, and it derives from a COM interface,
+	// then this is a COM interface too.
+	if (b->base->isCOMinterface())
+	    com = 1;
+
+	vtblInterfaces->push(b);
+	b->copyBaseInterfaces(vtblInterfaces);
+    }
+}
+
+/****************************************
+ */
+
+int ClassDeclaration::isCOMclass()
+{
+    return com;
+}
+
+int ClassDeclaration::isCOMinterface()
+{
+    return 0;
+}
+
+
+/****************************************
+ */
+
+int ClassDeclaration::isAbstract()
+{
+    if (isabstract)
+	return TRUE;
+    for (int i = 1; i < vtbl.dim; i++)
+    {
+	FuncDeclaration *fd = ((Dsymbol *)vtbl.data[i])->isFuncDeclaration();
+
+	//printf("\tvtbl[%d] = %p\n", i, fd);
+	if (!fd || fd->isAbstract())
+	{
+	    isabstract |= 1;
+	    return TRUE;
+	}
+    }
+    return FALSE;
+}
+
+/****************************************
+ * Returns !=0 if there's an extra member which is the 'this'
+ * pointer to the enclosing context (enclosing class or function)
+ */
+
+int ClassDeclaration::isNested()
+{
+    return isnested;
+}
+
+/****************************************
+ * Determine if slot 0 of the vtbl[] is reserved for something else.
+ * For class objects, yes, this is where the classinfo ptr goes.
+ * For COM interfaces, no.
+ * For non-COM interfaces, yes, this is where the Interface ptr goes.
+ */
+
+int ClassDeclaration::vtblOffset()
+{
+    return 1;
+}
+
+/****************************************
+ */
+
+char *ClassDeclaration::kind()
+{
+    return "class";
+}
+
+/****************************************
+ */
+
+void ClassDeclaration::addLocalClass(ClassDeclarations *aclasses)
+{
+    aclasses->push(this);
+}
+
+/********************************* InterfaceDeclaration ****************************/
+
+InterfaceDeclaration::InterfaceDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses)
+    : ClassDeclaration(loc, id, baseclasses)
+{
+    com = 0;
+    if (id == Id::IUnknown)		// IUnknown is the root of all COM objects
+	com = 1;
+}
+
+Dsymbol *InterfaceDeclaration::syntaxCopy(Dsymbol *s)
+{
+    InterfaceDeclaration *id;
+
+    if (s)
+	id = (InterfaceDeclaration *)s;
+    else
+	id = new InterfaceDeclaration(loc, ident, NULL);
+
+    ClassDeclaration::syntaxCopy(id);
+    return id;
+}
+
+void InterfaceDeclaration::semantic(Scope *sc)
+{   int i;
+
+    //printf("InterfaceDeclaration::semantic(%s), type = %p\n", toChars(), type);
+    if (inuse)
+	return;
+    if (!scope)
+    {	type = type->semantic(loc, sc);
+	handle = handle->semantic(loc, sc);
+    }
+    if (!members)			// if forward reference
+    {	//printf("\tinterface '%s' is forward referenced\n", toChars());
+	return;
+    }
+    if (symtab)			// if already done
+    {	if (!scope)
+	    return;
+    }
+    else
+	symtab = new DsymbolTable();
+
+    Scope *scx = NULL;
+    if (scope)
+    {	sc = scope;
+	scx = scope;		// save so we don't make redundant copies
+	scope = NULL;
+    }
+
+    if (sc->stc & STCdeprecated)
+    {
+	isdeprecated = 1;
+    }
+
+    // Expand any tuples in baseclasses[]
+    for (i = 0; i < baseclasses.dim; )
+    {	BaseClass *b = (BaseClass *)baseclasses.data[0];
+	b->type = b->type->semantic(loc, sc);
+	Type *tb = b->type->toBasetype();
+
+	if (tb->ty == Ttuple)
+	{   TypeTuple *tup = (TypeTuple *)tb;
+	    enum PROT protection = b->protection;
+	    baseclasses.remove(i);
+	    size_t dim = Argument::dim(tup->arguments);
+	    for (size_t j = 0; j < dim; j++)
+	    {	Argument *arg = Argument::getNth(tup->arguments, j);
+		b = new BaseClass(arg->type, protection);
+		baseclasses.insert(i + j, b);
+	    }
+	}
+	else
+	    i++;
+    }
+
+    // Check for errors, handle forward references
+    for (i = 0; i < baseclasses.dim; )
+    {	TypeClass *tc;
+	BaseClass *b;
+	Type *tb;
+
+	b = (BaseClass *)baseclasses.data[i];
+	b->type = b->type->semantic(loc, sc);
+	tb = b->type->toBasetype();
+	if (tb->ty == Tclass)
+	    tc = (TypeClass *)tb;
+	else
+	    tc = NULL;
+	if (!tc || !tc->sym->isInterfaceDeclaration())
+	{
+	    error("base type must be interface, not %s", b->type->toChars());
+	    baseclasses.remove(i);
+	    continue;
+	}
+	else
+	{
+	    // Check for duplicate interfaces
+	    for (size_t j = 0; j < i; j++)
+	    {
+		BaseClass *b2 = (BaseClass *)baseclasses.data[j];
+		if (b2->base == tc->sym)
+		    error("inherits from duplicate interface %s", b2->base->toChars());
+	    }
+
+	    b->base = tc->sym;
+	    if (b->base == this || isBaseOf2(b->base))
+	    {
+		error("circular inheritance of interface");
+		baseclasses.remove(i);
+		continue;
+	    }
+	    if (!b->base->symtab || b->base->scope || b->base->inuse)
+	    {
+		//error("forward reference of base class %s", baseClass->toChars());
+		// Forward reference of base, try again later
+		//printf("\ttry later, forward reference of base %s\n", b->base->toChars());
+		scope = scx ? scx : new Scope(*sc);
+		scope->setNoFree();
+		scope->module->addDeferredSemantic(this);
+		return;
+	    }
+	}
+	i++;
+    }
+
+    interfaces_dim = baseclasses.dim;
+    interfaces = (BaseClass **)baseclasses.data;
+
+    interfaceSemantic(sc);
+
+    if (vtblOffset())
+	vtbl.push(this);		// leave room at vtbl[0] for classinfo
+
+    // Cat together the vtbl[]'s from base interfaces
+    for (i = 0; i < interfaces_dim; i++)
+    {	BaseClass *b = interfaces[i];
+
+	// Skip if b has already appeared
+	for (int k = 0; k < i; k++)
+	{
+	    if (b == interfaces[i])
+		goto Lcontinue;
+	}
+
+	// Copy vtbl[] from base class
+	if (b->base->vtblOffset())
+	{   int d = b->base->vtbl.dim;
+	    if (d > 1)
+	    {
+		vtbl.reserve(d - 1);
+		for (int j = 1; j < d; j++)
+		    vtbl.push(b->base->vtbl.data[j]);
+	    }
+	}
+	else
+	{
+	    vtbl.append(&b->base->vtbl);
+	}
+
+      Lcontinue:
+	;
+    }
+
+    for (i = 0; i < members->dim; i++)
+    {
+	Dsymbol *s = (Dsymbol *)members->data[i];
+	s->addMember(sc, this, 1);
+    }
+
+    sc = sc->push(this);
+    sc->parent = this;
+    if (isCOMinterface())
+	sc->linkage = LINKwindows;
+    sc->structalign = 8;
+    structalign = sc->structalign;
+    sc->offset = 8;
+    inuse++;
+    for (i = 0; i < members->dim; i++)
+    {
+	Dsymbol *s = (Dsymbol *)members->data[i];
+	s->semantic(sc);
+    }
+    inuse--;
+    //members->print();
+    sc->pop();
+    //printf("-InterfaceDeclaration::semantic(%s), type = %p\n", toChars(), type);
+}
+
+
+/*******************************************
+ * Determine if 'this' is a base class of cd.
+ * (Actually, if it is an interface supported by cd)
+ * Output:
+ *	*poffset	offset to start of class
+ *			OFFSET_RUNTIME	must determine offset at runtime
+ * Returns:
+ *	0	not a base
+ *	1	is a base
+ */
+
+int InterfaceDeclaration::isBaseOf(ClassDeclaration *cd, int *poffset)
+{
+    unsigned j;
+
+    //printf("%s.InterfaceDeclaration::isBaseOf(cd = '%s')\n", toChars(), cd->toChars());
+    assert(!baseClass);
+    for (j = 0; j < cd->interfaces_dim; j++)
+    {
+	BaseClass *b = cd->interfaces[j];
+
+	//printf("\tbase %s\n", b->base->toChars());
+	if (this == b->base)
+	{
+	    //printf("\tfound at offset %d\n", b->offset);
+	    if (poffset)
+	    {	*poffset = b->offset;
+		if (j && cd->isInterfaceDeclaration())
+		    *poffset = OFFSET_RUNTIME;
+	    }
+	    return 1;
+	}
+	if (isBaseOf(b, poffset))
+	{   if (j && poffset && cd->isInterfaceDeclaration())
+		*poffset = OFFSET_RUNTIME;
+	    return 1;
+	}
+    }
+
+    if (cd->baseClass && isBaseOf(cd->baseClass, poffset))
+	return 1;
+
+    if (poffset)
+	*poffset = 0;
+    return 0;
+}
+
+
+int InterfaceDeclaration::isBaseOf(BaseClass *bc, int *poffset)
+{
+    //printf("%s.InterfaceDeclaration::isBaseOf(bc = '%s')\n", toChars(), bc->base->toChars());
+    for (unsigned j = 0; j < bc->baseInterfaces_dim; j++)
+    {
+	BaseClass *b = &bc->baseInterfaces[j];
+
+	if (this == b->base)
+	{
+	    if (poffset)
+	    {	*poffset = b->offset;
+	    }
+	    return 1;
+	}
+	if (isBaseOf(b, poffset))
+	{
+	    return 1;
+	}
+    }
+    if (poffset)
+	*poffset = 0;
+    return 0;
+}
+
+/****************************************
+ * Determine if slot 0 of the vtbl[] is reserved for something else.
+ * For class objects, yes, this is where the ClassInfo ptr goes.
+ * For COM interfaces, no.
+ * For non-COM interfaces, yes, this is where the Interface ptr goes.
+ */
+
+int InterfaceDeclaration::vtblOffset()
+{
+    if (isCOMinterface())
+	return 0;
+    return 1;
+}
+
+int InterfaceDeclaration::isCOMinterface()
+{
+    return com;
+}
+
+/*******************************************
+ */
+
+char *InterfaceDeclaration::kind()
+{
+    return "interface";
+}
+
+
+/******************************** BaseClass *****************************/
+
+BaseClass::BaseClass()
+{
+    memset(this, 0, sizeof(BaseClass));
+}
+
+BaseClass::BaseClass(Type *type, enum PROT protection)
+{
+    //printf("BaseClass(this = %p, '%s')\n", this, type->toChars());
+    this->type = type;
+    this->protection = protection;
+    base = NULL;
+    offset = 0;
+
+    baseInterfaces_dim = 0;
+    baseInterfaces = NULL;
+}
+
+/****************************************
+ * Fill in vtbl[] for base class based on member functions of class cd.
+ * Input:
+ *	vtbl		if !=NULL, fill it in
+ *	newinstance	!=0 means all entries must be filled in by members
+ *			of cd, not members of any base classes of cd.
+ * Returns:
+ *	!=0 if any entries were filled in by members of cd (not exclusively
+ *	by base classes)
+ */
+
+int BaseClass::fillVtbl(ClassDeclaration *cd, Array *vtbl, int newinstance)
+{
+    ClassDeclaration *id = base;
+    int j;
+    int result = 0;
+
+    //printf("BaseClass::fillVtbl(this='%s', cd='%s')\n", base->toChars(), cd->toChars());
+    if (vtbl)
+	vtbl->setDim(base->vtbl.dim);
+
+    // first entry is ClassInfo reference
+    for (j = base->vtblOffset(); j < base->vtbl.dim; j++)
+    {
+	FuncDeclaration *ifd = ((Dsymbol *)base->vtbl.data[j])->isFuncDeclaration();
+	FuncDeclaration *fd;
+	TypeFunction *tf;
+
+	//printf("        vtbl[%d] is '%s'\n", j, ifd ? ifd->toChars() : "null");
+
+	assert(ifd);
+	// Find corresponding function in this class
+	tf = (ifd->type->ty == Tfunction) ? (TypeFunction *)(ifd->type) : NULL;
+	fd = cd->findFunc(ifd->ident, tf);
+	if (fd && !fd->isAbstract())
+	{
+	    //printf("            found\n");
+	    // Check that calling conventions match
+	    if (fd->linkage != ifd->linkage)
+		fd->error("linkage doesn't match interface function");
+
+	    // Check that it is current
+	    if (newinstance &&
+		fd->toParent() != cd &&
+		ifd->toParent() == base)
+		cd->error("interface function %s.%s is not implemented",
+		    id->toChars(), ifd->ident->toChars());
+
+	    if (fd->toParent() == cd)
+		result = 1;
+	}
+	else
+	{
+	    //printf("            not found\n");
+	    // BUG: should mark this class as abstract?
+	    if (!cd->isAbstract())
+		cd->error("interface function %s.%s isn't implemented",
+		    id->toChars(), ifd->ident->toChars());
+	    fd = NULL;
+	}
+	if (vtbl)
+	    vtbl->data[j] = fd;
+    }
+
+    return result;
+}
+
+void BaseClass::copyBaseInterfaces(BaseClasses *vtblInterfaces)
+{
+    //printf("+copyBaseInterfaces(), %s\n", base->toChars());
+//    if (baseInterfaces_dim)
+//	return;
+
+    baseInterfaces_dim = base->interfaces_dim;
+    baseInterfaces = (BaseClass *)mem.calloc(baseInterfaces_dim, sizeof(BaseClass));
+
+    //printf("%s.copyBaseInterfaces()\n", base->toChars());
+    for (int i = 0; i < baseInterfaces_dim; i++)
+    {
+	BaseClass *b = &baseInterfaces[i];
+	BaseClass *b2 = base->interfaces[i];
+
+	assert(b2->vtbl.dim == 0);	// should not be filled yet
+	memcpy(b, b2, sizeof(BaseClass));
+
+	if (i)				// single inheritance is i==0
+	    vtblInterfaces->push(b);	// only need for M.I.
+	b->copyBaseInterfaces(vtblInterfaces);
+    }
+    //printf("-copyBaseInterfaces\n");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dmd/clone.c	Thu May 01 15:15:28 2008 +0200
@@ -0,0 +1,175 @@
+
+// Compiler implementation of the D programming language
+// Copyright (c) 1999-2008 by Digital Mars
+// All Rights Reserved
+// written by Walter Bright
+// http://www.digitalmars.com
+// License for redistribution is by either the Artistic License
+// in artistic.txt, or the GNU General Public License in gnu.txt.
+// See the included readme.txt for details.
+
+#include <stdio.h>
+#include <assert.h>
+
+#include "root.h"
+#include "aggregate.h"
+#include "scope.h"
+#include "mtype.h"
+#include "declaration.h"
+#include "module.h"
+#include "id.h"
+#include "expression.h"
+#include "statement.h"
+
+
+/*********************************
+ * Generate expression that calls opClone()
+ * for each member of the struct
+ * (can be NULL for members that don't need one)
+ */
+
+#if V2
+Expression *StructDeclaration::cloneMembers()
+{
+    Expression *e = NULL;
+
+    for (size_t i = 0; i < fields.dim; i++)
+    {
+	Dsymbol *s = (Dsymbol *)fields.data[i];
+	VarDeclaration *v = s->isVarDeclaration();
+	assert(v && v->storage_class & STCfield);
+	Type *tv = v->type->toBasetype();
+	size_t dim = 1;
+	while (tv->ty == Tsarray)
+	{   TypeSArray *ta = (TypeSArray *)tv;
+	    dim *= ((TypeSArray *)tv)->dim->toInteger();
+	    tv = tv->nextOf()->toBasetype();
+	}
+	if (tv->ty == Tstruct)
+	{   TypeStruct *ts = (TypeStruct *)tv;
+	    StructDeclaration *sd = ts->sym;
+	    if (sd->opclone)
+	    {	Expression *ex;
+
+		// this.v
+		ex = new ThisExp(0);
+		ex = new DotVarExp(0, ex, v, 0);
+
+		if (dim == 1)
+		{   // this.v.opClone()
+		    ex = new DotVarExp(0, ex, sd->opclone, 0);
+		    ex = new CallExp(0, ex);
+		}
+		else
+		{
+		    // _callOpClones(&this.v, opclone, dim)
+		    Expressions *args = new Expressions();
+		    args->push(new AddrExp(0, ex));
+		    args->push(new SymOffExp(0, sd->opclone, 0));
+		    args->push(new IntegerExp(dim));
+		    FuncDeclaration *ec = FuncDeclaration::genCfunc(Type::tvoid, "_callOpClones");
+		    ex = new CallExp(0, new VarExp(0, ec), args);
+		}
+		e = Expression::combine(e, ex);
+	    }
+	}
+    }
+    return e;
+}
+#endif
+
+/*****************************************
+ * Create inclusive destructor for struct by aggregating
+ * all the destructors in dtors[] with the destructors for
+ * all the members.
+ */
+
+FuncDeclaration *AggregateDeclaration::buildDtor(Scope *sc)
+{
+    //printf("StructDeclaration::buildDtor() %s\n", toChars());
+    Expression *e = NULL;
+
+#if V2
+    for (size_t i = 0; i < fields.dim; i++)
+    {
+	Dsymbol *s = (Dsymbol *)fields.data[i];
+	VarDeclaration *v = s->isVarDeclaration();
+	assert(v && v->storage_class & STCfield);
+	Type *tv = v->type->toBasetype();
+	size_t dim = 1;
+	while (tv->ty == Tsarray)
+	{   TypeSArray *ta = (TypeSArray *)tv;
+	    dim *= ((TypeSArray *)tv)->dim->toInteger();
+	    tv = tv->nextOf()->toBasetype();
+	}
+	if (tv->ty == Tstruct)
+	{   TypeStruct *ts = (TypeStruct *)tv;
+	    StructDeclaration *sd = ts->sym;
+	    if (sd->dtor)
+	    {	Expression *ex;
+
+		// this.v
+		ex = new ThisExp(0);
+		ex = new DotVarExp(0, ex, v, 0);
+
+		if (dim == 1)
+		{   // this.v.dtor()
+		    ex = new DotVarExp(0, ex, sd->dtor, 0);
+		    ex = new CallExp(0, ex);
+		}
+		else
+		{
+		    // Typeinfo.destroy(cast(void*)&this.v);
+		    Expression *ea = new AddrExp(0, ex);
+		    ea = new CastExp(0, ea, Type::tvoid->pointerTo());
+		    Expressions *args = new Expressions();
+		    args->push(ea);
+
+		    Expression *et = v->type->getTypeInfo(sc);
+		    et = new DotIdExp(0, et, Id::destroy);
+
+		    ex = new CallExp(0, et, args);
+		}
+		e = Expression::combine(ex, e);	// combine in reverse order
+	    }
+	}
+    }
+
+    /* Build our own "destructor" which executes e
+     */
+    if (e)
+    {	//printf("Building __fieldDtor()\n");
+	DtorDeclaration *dd = new DtorDeclaration(0, 0, Lexer::idPool("__fieldDtor"));
+	dd->fbody = new ExpStatement(0, e);
+	dtors.shift(dd);
+	members->push(dd);
+	dd->semantic(sc);
+    }
+#endif
+
+    switch (dtors.dim)
+    {
+	case 0:
+	    return NULL;
+
+	case 1:
+	    return (FuncDeclaration *)dtors.data[0];
+
+	default:
+	    e = NULL;
+	    for (size_t i = 0; i < dtors.dim; i++)
+	    {	FuncDeclaration *fd = (FuncDeclaration *)dtors.data[i];
+		Expression *ex = new ThisExp(0);
+		ex = new DotVarExp(0, ex, fd);
+		ex = new CallExp(0, ex);
+		e = Expression::combine(ex, e);
+	    }
+	    DtorDeclaration *dd = new DtorDeclaration(0, 0, Lexer::idPool("__aggrDtor"));
+	    dd->fbody = new ExpStatement(0, e);
+	    members->push(dd);
+	    dd->semantic(sc);
+	    return dd;
+    }
+}
+
+
--- a/dmd/declaration.c	Thu May 01 13:33:02 2008 +0200
+++ b/dmd/declaration.c	Thu May 01 15:15:28 2008 +0200
@@ -1,1308 +1,1318 @@
-
-// Compiler implementation of the D programming language
-// Copyright (c) 1999-2007 by Digital Mars
-// All Rights Reserved
-// written by Walter Bright
-// http://www.digitalmars.com
-// License for redistribution is by either the Artistic License
-// in artistic.txt, or the GNU General Public License in gnu.txt.
-// See the included readme.txt for details.
-
-#include <stdio.h>
-#include <assert.h>
-
-#include "init.h"
-#include "declaration.h"
-#include "attrib.h"
-#include "mtype.h"
-#include "template.h"
-#include "scope.h"
-#include "aggregate.h"
-#include "module.h"
-#include "id.h"
-#include "expression.h"
-#include "hdrgen.h"
-
-/********************************* Declaration ****************************/
-
-Declaration::Declaration(Identifier *id)
-    : Dsymbol(id)
-{
-    type = NULL;
-    originalType = NULL;
-    storage_class = STCundefined;
-    protection = PROTundefined;
-    linkage = LINKdefault;
-}
-
-void Declaration::semantic(Scope *sc)
-{
-}
-
-char *Declaration::kind()
-{
-    return "declaration";
-}
-
-unsigned Declaration::size(Loc loc)
-{
-    assert(type);
-    return type->size();
-}
-
-int Declaration::isStaticConstructor()
-{
-    return FALSE;
-}
-
-int Declaration::isStaticDestructor()
-{
-    return FALSE;
-}
-
-int Declaration::isDelete()
-{
-    return FALSE;
-}
-
-int Declaration::isDataseg()
-{
-    return FALSE;
-}
-
-int Declaration::isCodeseg()
-{
-    return FALSE;
-}
-
-enum PROT Declaration::prot()
-{
-    return protection;
-}
-
-/********************************* TupleDeclaration ****************************/
-
-TupleDeclaration::TupleDeclaration(Loc loc, Identifier *id, Objects *objects)
-    : Declaration(id)
-{
-    this->type = NULL;
-    this->objects = objects;
-    this->isexp = 0;
-    this->tupletype = NULL;
-}
-
-Dsymbol *TupleDeclaration::syntaxCopy(Dsymbol *s)
-{
-    assert(0);
-    return NULL;
-}
-
-char *TupleDeclaration::kind()
-{
-    return "tuple";
-}
-
-Type *TupleDeclaration::getType()
-{
-    /* If this tuple represents a type, return that type
-     */
-
-    //printf("TupleDeclaration::getType() %s\n", toChars());
-    if (isexp)
-	return NULL;
-    if (!tupletype)
-    {
-	/* It's only a type tuple if all the Object's are types
-	 */
-	for (size_t i = 0; i < objects->dim; i++)
-	{   Object *o = (Object *)objects->data[i];
-
-	    if (o->dyncast() != DYNCAST_TYPE)
-	    {
-		//printf("\tnot[%d], %p, %d\n", i, o, o->dyncast());
-		return NULL;
-	    }
-	}
-
-	/* We know it's a type tuple, so build the TypeTuple
-	 */
-	Arguments *args = new Arguments();
-	args->setDim(objects->dim);
-	OutBuffer buf;
-	for (size_t i = 0; i < objects->dim; i++)
-	{   Type *t = (Type *)objects->data[i];
-
-	    //printf("type = %s\n", t->toChars());
-#if 0
-	    buf.printf("_%s_%d", ident->toChars(), i);
-	    char *name = (char *)buf.extractData();
-	    Identifier *id = new Identifier(name, TOKidentifier);
-	    Argument *arg = new Argument(STCin, t, id, NULL);
-#else
-	    Argument *arg = new Argument(STCin, t, NULL, NULL);
-#endif
-	    args->data[i] = (void *)arg;
-	}
-
-	tupletype = new TypeTuple(args);
-    }
-
-    return tupletype;
-}
-
-int TupleDeclaration::needThis()
-{
-    //printf("TupleDeclaration::needThis(%s)\n", toChars());
-    for (size_t i = 0; i < objects->dim; i++)
-    {   Object *o = (Object *)objects->data[i];
-	if (o->dyncast() == DYNCAST_EXPRESSION)
-	{   Expression *e = (Expression *)o;
-	    if (e->op == TOKdsymbol)
-	    {	DsymbolExp *ve = (DsymbolExp *)e;
-		Declaration *d = ve->s->isDeclaration();
-		if (d && d->needThis())
-		{
-		    return 1;
-		}
-	    }
-	}
-    }
-    return 0;
-}
-
-/********************************* TypedefDeclaration ****************************/
-
-TypedefDeclaration::TypedefDeclaration(Loc loc, Identifier *id, Type *basetype, Initializer *init)
-    : Declaration(id)
-{
-    this->type = new TypeTypedef(this);
-    this->basetype = basetype->toBasetype();
-    this->init = init;
-#ifdef _DH
-    this->htype = NULL;
-    this->hbasetype = NULL;
-#endif
-    this->sem = 0;
-    this->inuse = 0;
-    this->loc = loc;
-    this->sinit = NULL;
-}
-
-Dsymbol *TypedefDeclaration::syntaxCopy(Dsymbol *s)
-{
-    Type *basetype = this->basetype->syntaxCopy();
-
-    Initializer *init = NULL;
-    if (this->init)
-	init = this->init->syntaxCopy();
-
-    assert(!s);
-    TypedefDeclaration *st;
-    st = new TypedefDeclaration(loc, ident, basetype, init);
-#ifdef _DH
-    // Syntax copy for header file
-    if (!htype)      // Don't overwrite original
-    {	if (type)    // Make copy for both old and new instances
-	{   htype = type->syntaxCopy();
-	    st->htype = type->syntaxCopy();
-	}
-    }
-    else            // Make copy of original for new instance
-        st->htype = htype->syntaxCopy();
-    if (!hbasetype)
-    {	if (basetype)
-	{   hbasetype = basetype->syntaxCopy();
-	    st->hbasetype = basetype->syntaxCopy();
-	}
-    }
-    else
-        st->hbasetype = hbasetype->syntaxCopy();
-#endif
-    return st;
-}
-
-void TypedefDeclaration::semantic(Scope *sc)
-{
-    //printf("TypedefDeclaration::semantic(%s) sem = %d\n", toChars(), sem);
-    if (sem == 0)
-    {	sem = 1;
-	basetype = basetype->semantic(loc, sc);
-	sem = 2;
-	type = type->semantic(loc, sc);
-	if (sc->parent->isFuncDeclaration() && init)
-	    semantic2(sc);
-    }
-    else if (sem == 1)
-    {
-	error("circular definition");
-    }
-}
-
-void TypedefDeclaration::semantic2(Scope *sc)
-{
-    //printf("TypedefDeclaration::semantic2(%s) sem = %d\n", toChars(), sem);
-    if (sem == 2)
-    {	sem = 3;
-	if (init)
-	{
-	    init = init->semantic(sc, basetype);
-
-	    ExpInitializer *ie = init->isExpInitializer();
-	    if (ie)
-	    {
-		if (ie->exp->type == basetype)
-		    ie->exp->type = type;
-	    }
-	}
-    }
-}
-
-char *TypedefDeclaration::kind()
-{
-    return "typedef";
-}
-
-Type *TypedefDeclaration::getType()
-{
-    return type;
-}
-
-void TypedefDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    buf->writestring("typedef ");
-    basetype->toCBuffer(buf, ident, hgs);
-    if (init)
-    {
-	buf->writestring(" = ");
-	init->toCBuffer(buf, hgs);
-    }
-    buf->writeByte(';');
-    buf->writenl();
-}
-
-/********************************* AliasDeclaration ****************************/
-
-AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Type *type)
-    : Declaration(id)
-{
-    //printf("AliasDeclaration(id = '%s', type = %p)\n", id->toChars(), type);
-    //printf("type = '%s'\n", type->toChars());
-    this->loc = loc;
-    this->type = type;
-    this->aliassym = NULL;
-#ifdef _DH
-    this->htype = NULL;
-    this->haliassym = NULL;
-#endif
-    this->overnext = NULL;
-    this->inSemantic = 0;
-    assert(type);
-}
-
-AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Dsymbol *s)
-    : Declaration(id)
-{
-    //printf("AliasDeclaration(id = '%s', s = %p)\n", id->toChars(), s);
-    assert(s != this);
-    this->loc = loc;
-    this->type = NULL;
-    this->aliassym = s;
-#ifdef _DH
-    this->htype = NULL;
-    this->haliassym = NULL;
-#endif
-    this->overnext = NULL;
-    this->inSemantic = 0;
-    assert(s);
-}
-
-Dsymbol *AliasDeclaration::syntaxCopy(Dsymbol *s)
-{
-    assert(!s);
-    AliasDeclaration *sa;
-    if (type)
-	sa = new AliasDeclaration(loc, ident, type->syntaxCopy());
-    else
-	sa = new AliasDeclaration(loc, ident, aliassym->syntaxCopy(NULL));
-#ifdef _DH
-    // Syntax copy for header file
-    if (!htype)	    // Don't overwrite original
-    {	if (type)	// Make copy for both old and new instances
-	{   htype = type->syntaxCopy();
-	    sa->htype = type->syntaxCopy();
-	}
-    }
-    else			// Make copy of original for new instance
-	sa->htype = htype->syntaxCopy();
-    if (!haliassym)
-    {	if (aliassym)
-	{   haliassym = aliassym->syntaxCopy(s);
-	    sa->haliassym = aliassym->syntaxCopy(s);
-	}
-    }
-    else
-	sa->haliassym = haliassym->syntaxCopy(s);
-#endif
-    return sa;
-}
-
-void AliasDeclaration::semantic(Scope *sc)
-{
-    //printf("AliasDeclaration::semantic() %s\n", toChars());
-    if (aliassym)
-    {
-	if (aliassym->isTemplateInstance())
-	    aliassym->semantic(sc);
-	return;
-    }
-    this->inSemantic = 1;
-
-    if (storage_class & STCconst)
-	error("cannot be const");
-
-    storage_class |= sc->stc & STCdeprecated;
-
-    // Given:
-    //	alias foo.bar.abc def;
-    // it is not knowable from the syntax whether this is an alias
-    // for a type or an alias for a symbol. It is up to the semantic()
-    // pass to distinguish.
-    // If it is a type, then type is set and getType() will return that
-    // type. If it is a symbol, then aliassym is set and type is NULL -
-    // toAlias() will return aliasssym.
-
-    Dsymbol *s;
-    Type *t;
-    Expression *e;
-
-    /* This section is needed because resolve() will:
-     *   const x = 3;
-     *   alias x y;
-     * try to alias y to 3.
-     */
-    s = type->toDsymbol(sc);
-    if (s)
-	goto L2;			// it's a symbolic alias
-
-    //printf("alias type is %s\n", type->toChars());
-    type->resolve(loc, sc, &e, &t, &s);
-    if (s)
-    {
-	goto L2;
-    }
-    else if (e)
-    {
-	// Try to convert Expression to Dsymbol
-        if (e->op == TOKvar)
-	{   s = ((VarExp *)e)->var;
-	    goto L2;
-	}
-        else if (e->op == TOKfunction)
-	{   s = ((FuncExp *)e)->fd;
-	    goto L2;
-	}
-        else
-	{   error("cannot alias an expression %s", e->toChars());
-	    t = e->type;
-	}
-    }
-    else if (t)
-	type = t;
-    if (overnext)
-	ScopeDsymbol::multiplyDefined(0, this, overnext);
-    this->inSemantic = 0;
-    return;
-
-  L2:
-    //printf("alias is a symbol %s %s\n", s->kind(), s->toChars());
-    type = NULL;
-    VarDeclaration *v = s->isVarDeclaration();
-    if (v && v->linkage == LINKdefault)
-    {
-	error("forward reference of %s", v->toChars());
-	s = NULL;
-    }
-    else
-    {
-	FuncDeclaration *f = s->toAlias()->isFuncDeclaration();
-	if (f)
-	{
-	    if (overnext)
-	    {
-		FuncAliasDeclaration *fa = new FuncAliasDeclaration(f);
-		if (!fa->overloadInsert(overnext))
-		    ScopeDsymbol::multiplyDefined(0, f, overnext);
-		overnext = NULL;
-		s = fa;
-		s->parent = sc->parent;
-	    }
-	}
-	if (overnext)
-	    ScopeDsymbol::multiplyDefined(0, s, overnext);
-	if (s == this)
-	{
-	    assert(global.errors);
-	    s = NULL;
-	}
-    }
-    aliassym = s;
-    this->inSemantic = 0;
-}
-
-int AliasDeclaration::overloadInsert(Dsymbol *s)
-{
-    /* Don't know yet what the aliased symbol is, so assume it can
-     * be overloaded and check later for correctness.
-     */
-
-    //printf("AliasDeclaration::overloadInsert('%s')\n", s->toChars());
-    if (overnext == NULL)
-    {	overnext = s;
-	return TRUE;
-    }
-    else
-    {
-	return overnext->overloadInsert(s);
-    }
-}
-
-char *AliasDeclaration::kind()
-{
-    return "alias";
-}
-
-Type *AliasDeclaration::getType()
-{
-    return type;
-}
-
-Dsymbol *AliasDeclaration::toAlias()
-{
-    //printf("AliasDeclaration::toAlias('%s', this = %p, aliassym = %p, kind = '%s')\n", toChars(), this, aliassym, aliassym ? aliassym->kind() : "");
-    assert(this != aliassym);
-    //static int count; if (++count == 10) *(char*)0=0;
-    if (inSemantic)
-    {	error("recursive alias declaration");
-//	return this;
-    }
-    Dsymbol *s = aliassym ? aliassym->toAlias() : this;
-    return s;
-}
-
-void AliasDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    buf->writestring("alias ");
-#if 0 && _DH
-    if (hgs->hdrgen)
-    {
-	if (haliassym)
-	{
-	    haliassym->toCBuffer(buf, hgs);
-	    buf->writeByte(' ');
-	    buf->writestring(ident->toChars());
-	}
-	else
-	    htype->toCBuffer(buf, ident, hgs);
-    }
-    else
-#endif
-    {
-	if (aliassym)
-	{
-	    aliassym->toCBuffer(buf, hgs);
-	    buf->writeByte(' ');
-	    buf->writestring(ident->toChars());
-	}
-	else
-	    type->toCBuffer(buf, ident, hgs);
-    }
-    buf->writeByte(';');
-    buf->writenl();
-}
-
-/********************************* VarDeclaration ****************************/
-
-VarDeclaration::VarDeclaration(Loc loc, Type *type, Identifier *id, Initializer *init)
-    : Declaration(id)
-{
-    //printf("VarDeclaration('%s')\n", id->toChars());
-#ifdef DEBUG
-    if (!type && !init)
-    {	printf("VarDeclaration('%s')\n", id->toChars());
-	//*(char*)0=0;
-    }
-#endif
-    assert(type || init);
-    this->type = type;
-    this->init = init;
-#ifdef _DH
-    this->htype = NULL;
-    this->hinit = NULL;
-#endif
-    this->loc = loc;
-    offset = 0;
-    noauto = 0;
-    nestedref = 0;
-    inuse = 0;
-    ctorinit = 0;
-    aliassym = NULL;
-    onstack = 0;
-    canassign = 0;
-    value = NULL;
-
-    // LLVMDC
-    needsStorage = false;
-}
-
-Dsymbol *VarDeclaration::syntaxCopy(Dsymbol *s)
-{
-    //printf("VarDeclaration::syntaxCopy(%s)\n", toChars());
-
-    VarDeclaration *sv;
-    if (s)
-    {	sv = (VarDeclaration *)s;
-    }
-    else
-    {
-	Initializer *init = NULL;
-	if (this->init)
-	{   init = this->init->syntaxCopy();
-	    //init->isExpInitializer()->exp->print();
-	    //init->isExpInitializer()->exp->dump(0);
-	}
-
-	sv = new VarDeclaration(loc, type ? type->syntaxCopy() : NULL, ident, init);
-	sv->storage_class = storage_class;
-    }
-#ifdef _DH
-    // Syntax copy for header file
-    if (!htype)      // Don't overwrite original
-    {	if (type)    // Make copy for both old and new instances
-	{   htype = type->syntaxCopy();
-	    sv->htype = type->syntaxCopy();
-	}
-    }
-    else            // Make copy of original for new instance
-        sv->htype = htype->syntaxCopy();
-    if (!hinit)
-    {	if (init)
-	{   hinit = init->syntaxCopy();
-	    sv->hinit = init->syntaxCopy();
-	}
-    }
-    else
-        sv->hinit = hinit->syntaxCopy();
-#endif
-    return sv;
-}
-
-void VarDeclaration::semantic(Scope *sc)
-{
-    //printf("VarDeclaration::semantic('%s', parent = '%s')\n", toChars(), sc->parent->toChars());
-    //printf("type = %s\n", type->toChars());
-    //printf("linkage = %d\n", sc->linkage);
-    //if (strcmp(toChars(), "mul") == 0) halt();
-
-    storage_class |= sc->stc;
-    if (storage_class & STCextern && init)
-	error("extern symbols cannot have initializers");
-
-    /* If auto type inference, do the inference
-     */
-    int inferred = 0;
-    if (!type)
-    {	inuse++;
-	type = init->inferType(sc);
-	inuse--;
-	inferred = 1;
-
-	/* This is a kludge to support the existing syntax for RAII
-	 * declarations.
-	 */
-	storage_class &= ~STCauto;
-	originalType = type;
-    }
-    else
-    {	if (!originalType)
-	    originalType = type;
-	type = type->semantic(loc, sc);
-    }
-
-    type->checkDeprecated(loc, sc);
-    linkage = sc->linkage;
-    this->parent = sc->parent;
-    //printf("this = %p, parent = %p, '%s'\n", this, parent, parent->toChars());
-    protection = sc->protection;
-    //printf("sc->stc = %x\n", sc->stc);
-    //printf("storage_class = %x\n", storage_class);
-
-    Dsymbol *parent = toParent();
-    FuncDeclaration *fd = parent->isFuncDeclaration();
-
-    Type *tb = type->toBasetype();
-    if (tb->ty == Tvoid && !(storage_class & STClazy))
-    {	error("voids have no value");
-	type = Type::terror;
-	tb = type;
-    }
-    if (tb->ty == Tfunction)
-    {	error("cannot be declared to be a function");
-	type = Type::terror;
-	tb = type;
-    }
-    if (tb->ty == Tstruct)
-    {	TypeStruct *ts = (TypeStruct *)tb;
-
-	if (!ts->sym->members)
-	{
-	    error("no definition of struct %s", ts->toChars());
-	}
-    }
-
-    if (tb->ty == Ttuple)
-    {   /* Instead, declare variables for each of the tuple elements
-	 * and add those.
-	 */
-	TypeTuple *tt = (TypeTuple *)tb;
-	size_t nelems = Argument::dim(tt->arguments);
-	Objects *exps = new Objects();
-	exps->setDim(nelems);
-
-	for (size_t i = 0; i < nelems; i++)
-	{   Argument *arg = Argument::getNth(tt->arguments, i);
-
-	    OutBuffer buf;
-	    buf.printf("_%s_field_%zu", ident->toChars(), i);
-	    buf.writeByte(0);
-	    char *name = (char *)buf.extractData();
-	    Identifier *id = new Identifier(name, TOKidentifier);
-
-	    VarDeclaration *v = new VarDeclaration(loc, arg->type, id, NULL);
-	    //printf("declaring field %s of type %s\n", v->toChars(), v->type->toChars());
-	    v->semantic(sc);
-
-	    if (sc->scopesym)
-	    {	//printf("adding %s to %s\n", v->toChars(), sc->scopesym->toChars());
-		if (sc->scopesym->members)
-		    sc->scopesym->members->push(v);
-	    }
-
-	    Expression *e = new DsymbolExp(loc, v);
-	    exps->data[i] = e;
-	}
-	TupleDeclaration *v2 = new TupleDeclaration(loc, ident, exps);
-	v2->isexp = 1;
-	aliassym = v2;
-	return;
-    }
-
-    if (storage_class & STCconst && !init && !fd)
-	// Initialize by constructor only
-	storage_class = (storage_class & ~STCconst) | STCctorinit;
-
-    if (isConst())
-    {
-    }
-    else if (isStatic())
-    {
-    }
-    else if (isSynchronized())
-    {
-	error("variable %s cannot be synchronized", toChars());
-    }
-    else if (isOverride())
-    {
-	error("override cannot be applied to variable");
-    }
-    else if (isAbstract())
-    {
-	error("abstract cannot be applied to variable");
-    }
-    else if (storage_class & STCtemplateparameter)
-    {
-    }
-    else
-    {
-	AggregateDeclaration *aad = sc->anonAgg;
-	if (!aad)
-	    aad = parent->isAggregateDeclaration();
-	if (aad)
-	{
-	    aad->addField(sc, this);
-	}
-
-	InterfaceDeclaration *id = parent->isInterfaceDeclaration();
-	if (id)
-	{
-	    error("field not allowed in interface");
-	}
-
-	TemplateInstance *ti = parent->isTemplateInstance();
-	if (ti)
-	{
-	    // Take care of nested templates
-	    while (1)
-	    {
-		TemplateInstance *ti2 = ti->tempdecl->parent->isTemplateInstance();
-		if (!ti2)
-		    break;
-		ti = ti2;
-	    }
-
-	    // If it's a member template
-	    AggregateDeclaration *ad = ti->tempdecl->isMember();
-	    if (ad && storage_class != STCundefined)
-	    {
-		error("cannot use template to add field to aggregate '%s'", ad->toChars());
-	    }
-	}
-    }
-
-    if (type->isauto() && !noauto)
-    {
-	if (storage_class & (STCfield | STCout | STCref | STCstatic) || !fd)
-	{
-	    error("globals, statics, fields, ref and out parameters cannot be auto");
-	}
-
-	if (!(storage_class & (STCauto | STCscope)))
-	{
-	    if (!(storage_class & STCparameter) && ident != Id::withSym)
-		error("reference to scope class must be scope");
-	}
-    }
-
-    if (!init && !sc->inunion && !isStatic() && !isConst() && fd &&
-	!(storage_class & (STCfield | STCin | STCforeach)) &&
-	type->size() != 0)
-    {
-	// Provide a default initializer
-	//printf("Providing default initializer for '%s'\n", toChars());
-	if (type->ty == Tstruct &&
-	    ((TypeStruct *)type)->sym->zeroInit == 1)
-	{   /* If a struct is all zeros, as a special case
-	     * set it's initializer to the integer 0.
-	     * In AssignExp::toElem(), we check for this and issue
-	     * a memset() to initialize the struct.
-	     * Must do same check in interpreter.
-	     */
-	    Expression *e = new IntegerExp(loc, 0, Type::tint32);
-	    Expression *e1;
-	    e1 = new VarExp(loc, this);
-	    e = new AssignExp(loc, e1, e);
-	    e->type = e1->type;
-	    init = new ExpInitializer(loc, e/*->type->defaultInit()*/);
-	    return;
-	}
-	else if (type->ty == Ttypedef)
-	{   TypeTypedef *td = (TypeTypedef *)type;
-	    if (td->sym->init)
-	    {	init = td->sym->init;
-		ExpInitializer *ie = init->isExpInitializer();
-		if (ie)
-		    // Make copy so we can modify it
-		    init = new ExpInitializer(ie->loc, ie->exp);
-	    }
-	    else
-		init = getExpInitializer();
-	}
-	else
-	{
-	    init = getExpInitializer();
-	}
-    }
-
-    if (init)
-    {
-	ArrayInitializer *ai = init->isArrayInitializer();
-	if (ai && tb->ty == Taarray)
-	{
-	    init = ai->toAssocArrayInitializer();
-	}
-
-	StructInitializer *si = init->isStructInitializer();
-	ExpInitializer *ei = init->isExpInitializer();
-
-	// See if we can allocate on the stack
-	if (ei && isScope() && ei->exp->op == TOKnew)
-	{   NewExp *ne = (NewExp *)ei->exp;
-	    if (!(ne->newargs && ne->newargs->dim))
-	    {	ne->onstack = 1;
-		onstack = 1;
-		if (type->isBaseOf(ne->newtype->semantic(loc, sc), NULL))
-		    onstack = 2;
-	    }
-	}
-
-	// If inside function, there is no semantic3() call
-	if (sc->func)
-	{
-	    // If local variable, use AssignExp to handle all the various
-	    // possibilities.
-	    if (fd && !isStatic() && !isConst() && !init->isVoidInitializer())
-	    {
-		Expression *e1;
-		Type *t;
-		int dim;
-
-		//printf("fd = '%s', var = '%s'\n", fd->toChars(), toChars());
-		if (!ei)
-		{
-		    Expression *e = init->toExpression();
-		    if (!e)
-		    {
-			init = init->semantic(sc, type);
-			e = init->toExpression();
-			if (!e)
-			{   error("is not a static and cannot have static initializer");
-			    return;
-			}
-		    }
-		    ei = new ExpInitializer(init->loc, e);
-		    init = ei;
-		}
-
-		e1 = new VarExp(loc, this);
-
-		t = type->toBasetype();
-		if (t->ty == Tsarray)
-		{
-		    ei->exp = ei->exp->semantic(sc);
-		    if (!ei->exp->implicitConvTo(type))
-		    {
-			dim = ((TypeSArray *)t)->dim->toInteger();
-			// If multidimensional static array, treat as one large array
-			while (1)
-			{
-			    t = t->nextOf()->toBasetype();
-			    if (t->ty != Tsarray)
-				break;
-			    dim *= ((TypeSArray *)t)->dim->toInteger();
-			    e1->type = new TypeSArray(t->nextOf(), new IntegerExp(0, dim, Type::tindex));
-			}
-		    }
-		    e1 = new SliceExp(loc, e1, NULL, NULL);
-		}
-		else if (t->ty == Tstruct)
-		{
-		    ei->exp = ei->exp->semantic(sc);
-		    if (!ei->exp->implicitConvTo(type))
-			ei->exp = new CastExp(loc, ei->exp, type);
-		}
-		ei->exp = new AssignExp(loc, e1, ei->exp);
-		ei->exp->op = TOKconstruct;
-		canassign++;
-		ei->exp = ei->exp->semantic(sc);
-		canassign--;
-		ei->exp->optimize(WANTvalue);
-	    }
-	    else
-	    {
-		init = init->semantic(sc, type);
-		if (fd && isConst() && !isStatic())
-		{   // Make it static
-		    storage_class |= STCstatic;
-		}
-	    }
-	}
-	else if (isConst() || isFinal())
-	{
-	    /* Because we may need the results of a const declaration in a
-	     * subsequent type, such as an array dimension, before semantic2()
-	     * gets ordinarily run, try to run semantic2() now.
-	     * Ignore failure.
-	     */
-
-	    if (!global.errors && !inferred)
-	    {
-		unsigned errors = global.errors;
-		global.gag++;
-		//printf("+gag\n");
-		Expression *e;
-		Initializer *i2 = init;
-		inuse++;
-		if (ei)
-		{
-		    e = ei->exp->syntaxCopy();
-		    e = e->semantic(sc);
-		    e = e->implicitCastTo(sc, type);
-		}
-		else if (si || ai)
-		{   i2 = init->syntaxCopy();
-		    i2 = i2->semantic(sc, type);
-		}
-		inuse--;
-		global.gag--;
-		//printf("-gag\n");
-		if (errors != global.errors)	// if errors happened
-		{
-		    if (global.gag == 0)
-			global.errors = errors;	// act as if nothing happened
-		}
-		else if (ei)
-		{
-		    e = e->optimize(WANTvalue | WANTinterpret);
-		    if (e->op == TOKint64 || e->op == TOKstring)
-		    {
-			ei->exp = e;		// no errors, keep result
-		    }
-		}
-		else
-		    init = i2;		// no errors, keep result
-	    }
-	}
-    }
-}
-
-ExpInitializer *VarDeclaration::getExpInitializer()
-{
-    ExpInitializer *ei;
-
-    if (init)
-	ei = init->isExpInitializer();
-    else
-    {
-	Expression *e = type->defaultInit();
-	if (e)
-	    ei = new ExpInitializer(loc, e);
-	else
-	    ei = NULL;
-    }
-    return ei;
-}
-
-void VarDeclaration::semantic2(Scope *sc)
-{
-    //printf("VarDeclaration::semantic2('%s')\n", toChars());
-    if (init && !toParent()->isFuncDeclaration())
-    {	inuse++;
-#if 0
-	ExpInitializer *ei = init->isExpInitializer();
-	if (ei)
-	{
-	    ei->exp->dump(0);
-	    printf("type = %p\n", ei->exp->type);
-	}
-#endif
-	init = init->semantic(sc, type);
-	inuse--;
-    }
-}
-
-char *VarDeclaration::kind()
-{
-    return "variable";
-}
-
-Dsymbol *VarDeclaration::toAlias()
-{
-    //printf("VarDeclaration::toAlias('%s', this = %p, aliassym = %p)\n", toChars(), this, aliassym);
-    assert(this != aliassym);
-    Dsymbol *s = aliassym ? aliassym->toAlias() : this;
-    return s;
-}
-
-void VarDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    if (storage_class & STCconst)
-	buf->writestring("const ");
-    if (storage_class & STCstatic)
-	buf->writestring("static ");
-    if (type)
-	type->toCBuffer(buf, ident, hgs);
-    else
-	buf->writestring(ident->toChars());
-    if (init)
-    {	buf->writestring(" = ");
-	init->toCBuffer(buf, hgs);
-    }
-    buf->writeByte(';');
-    buf->writenl();
-}
-
-int VarDeclaration::needThis()
-{
-    //printf("VarDeclaration::needThis(%s, x%x)\n", toChars(), storage_class);
-    return storage_class & STCfield;
-}
-
-int VarDeclaration::isImportedSymbol()
-{
-    if (protection == PROTexport && !init && (isStatic() || isConst() || parent->isModule()))
-	return TRUE;
-    return FALSE;
-}
-
-void VarDeclaration::checkCtorConstInit()
-{
-    if (ctorinit == 0 && isCtorinit() && !(storage_class & STCfield))
-	error("missing initializer in static constructor for const variable");
-}
-
-/************************************
- * Check to see if variable is a reference to an enclosing function
- * or not.
- */
-
-void VarDeclaration::checkNestedReference(Scope *sc, Loc loc)
-{
-    if (parent && !isDataseg() && parent != sc->parent)
-    {
-	FuncDeclaration *fdv = toParent()->isFuncDeclaration();
-	FuncDeclaration *fdthis = sc->parent->isFuncDeclaration();
-
-	if (fdv && fdthis)
-	{
-	    if (loc.filename)
-		fdthis->getLevel(loc, fdv);
-	    nestedref = 1;
-	    fdv->nestedFrameRef = 1;
-        fdv->nestedVars.insert(this);
-	    //printf("var %s in function %s is nested ref\n", toChars(), fdv->toChars());
-	}
-    }
-}
-
-/*******************************
- * Does symbol go into data segment?
- */
-
-int VarDeclaration::isDataseg()
-{
-#if 0
-    printf("VarDeclaration::isDataseg(%p, '%s')\n", this, toChars());
-    printf("%x, %p, %p\n", storage_class & (STCstatic | STCconst), parent->isModule(), parent->isTemplateInstance());
-    printf("parent = '%s'\n", parent->toChars());
-#endif
-    Dsymbol *parent = this->toParent();
-    if (!parent && !(storage_class & (STCstatic | STCconst)))
-    {	error("forward referenced");
-	type = Type::terror;
-	return 0;
-    }
-    return (storage_class & (STCstatic | STCconst) ||
-	   parent->isModule() ||
-	   parent->isTemplateInstance());
-}
-
-int VarDeclaration::hasPointers()
-{
-    return (!isDataseg() && type->hasPointers());
-}
-
-/******************************************
- * If a variable has an auto destructor call, return call for it.
- * Otherwise, return NULL.
- */
-
-Expression *VarDeclaration::callAutoDtor()
-{   Expression *e = NULL;
-
-    //printf("VarDeclaration::callAutoDtor() %s\n", toChars());
-    if (storage_class & (STCauto | STCscope) && !noauto)
-    {
-	for (ClassDeclaration *cd = type->isClassHandle();
-	     cd;
-	     cd = cd->baseClass)
-	{
-	    /* We can do better if there's a way with onstack
-	     * classes to determine if there's no way the monitor
-	     * could be set.
-	     */
-	    if (cd->isInterfaceDeclaration())
-		error("interface %s cannot be scope", cd->toChars());
-	    if (1 || onstack || cd->dtors.dim)	// if any destructors
-	    {
-		// delete this;
-		Expression *ec;
-
-		ec = new VarExp(loc, this);
-		e = new DeleteExp(loc, ec);
-		e->type = Type::tvoid;
-		break;
-	    }
-	}
-    }
-    return e;
-}
-
-
-/********************************* ClassInfoDeclaration ****************************/
-
-ClassInfoDeclaration::ClassInfoDeclaration(ClassDeclaration *cd)
-    : VarDeclaration(0, ClassDeclaration::classinfo->type, cd->ident, NULL)
-{
-    this->cd = cd;
-    storage_class = STCstatic;
-}
-
-Dsymbol *ClassInfoDeclaration::syntaxCopy(Dsymbol *s)
-{
-    assert(0);		// should never be produced by syntax
-    return NULL;
-}
-
-void ClassInfoDeclaration::semantic(Scope *sc)
-{
-}
-
-/********************************* ModuleInfoDeclaration ****************************/
-
-ModuleInfoDeclaration::ModuleInfoDeclaration(Module *mod)
-    : VarDeclaration(0, Module::moduleinfo->type, mod->ident, NULL)
-{
-    this->mod = mod;
-    storage_class = STCstatic;
-}
-
-Dsymbol *ModuleInfoDeclaration::syntaxCopy(Dsymbol *s)
-{
-    assert(0);		// should never be produced by syntax
-    return NULL;
-}
-
-void ModuleInfoDeclaration::semantic(Scope *sc)
-{
-}
-
-/********************************* TypeInfoDeclaration ****************************/
-
-TypeInfoDeclaration::TypeInfoDeclaration(Type *tinfo, int internal)
-    : VarDeclaration(0, Type::typeinfo->type, tinfo->getTypeInfoIdent(internal), NULL)
-{
-    this->tinfo = tinfo;
-    storage_class = STCstatic;
-    protection = PROTpublic;
-    linkage = LINKc;
-}
-
-Dsymbol *TypeInfoDeclaration::syntaxCopy(Dsymbol *s)
-{
-    assert(0);		// should never be produced by syntax
-    return NULL;
-}
-
-void TypeInfoDeclaration::semantic(Scope *sc)
-{
-    assert(linkage == LINKc);
-}
-
-/***************************** TypeInfoConstDeclaration **********************/
-
-#if V2
-TypeInfoConstDeclaration::TypeInfoConstDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-#endif
-
-/***************************** TypeInfoInvariantDeclaration **********************/
-
-#if V2
-TypeInfoInvariantDeclaration::TypeInfoInvariantDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-#endif
-
-/***************************** TypeInfoStructDeclaration **********************/
-
-TypeInfoStructDeclaration::TypeInfoStructDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-
-/***************************** TypeInfoClassDeclaration ***********************/
-
-TypeInfoClassDeclaration::TypeInfoClassDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-
-/***************************** TypeInfoInterfaceDeclaration *******************/
-
-TypeInfoInterfaceDeclaration::TypeInfoInterfaceDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-
-/***************************** TypeInfoTypedefDeclaration *********************/
-
-TypeInfoTypedefDeclaration::TypeInfoTypedefDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-
-/***************************** TypeInfoPointerDeclaration *********************/
-
-TypeInfoPointerDeclaration::TypeInfoPointerDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-
-/***************************** TypeInfoArrayDeclaration ***********************/
-
-TypeInfoArrayDeclaration::TypeInfoArrayDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-
-/***************************** TypeInfoStaticArrayDeclaration *****************/
-
-TypeInfoStaticArrayDeclaration::TypeInfoStaticArrayDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-
-/***************************** TypeInfoAssociativeArrayDeclaration ************/
-
-TypeInfoAssociativeArrayDeclaration::TypeInfoAssociativeArrayDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-
-/***************************** TypeInfoEnumDeclaration ***********************/
-
-TypeInfoEnumDeclaration::TypeInfoEnumDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-
-/***************************** TypeInfoFunctionDeclaration ********************/
-
-TypeInfoFunctionDeclaration::TypeInfoFunctionDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-
-/***************************** TypeInfoDelegateDeclaration ********************/
-
-TypeInfoDelegateDeclaration::TypeInfoDelegateDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-
-/***************************** TypeInfoTupleDeclaration **********************/
-
-TypeInfoTupleDeclaration::TypeInfoTupleDeclaration(Type *tinfo)
-    : TypeInfoDeclaration(tinfo, 0)
-{
-}
-
-/********************************* ThisDeclaration ****************************/
-
-// For the "this" parameter to member functions
-
-ThisDeclaration::ThisDeclaration(Type *t)
-   : VarDeclaration(0, t, Id::This, NULL)
-{
-    noauto = 1;
-}
-
-Dsymbol *ThisDeclaration::syntaxCopy(Dsymbol *s)
-{
-    assert(0);		// should never be produced by syntax
-    return NULL;
-}
-
-
+
+// Compiler implementation of the D programming language
+// Copyright (c) 1999-2007 by Digital Mars
+// All Rights Reserved
+// written by Walter Bright
+// http://www.digitalmars.com
+// License for redistribution is by either the Artistic License
+// in artistic.txt, or the GNU General Public License in gnu.txt.
+// See the included readme.txt for details.
+
+#include <stdio.h>
+#include <assert.h>
+
+#include "init.h"
+#include "declaration.h"
+#include "attrib.h"
+#include "mtype.h"
+#include "template.h"
+#include "scope.h"
+#include "aggregate.h"
+#include "module.h"
+#include "id.h"
+#include "expression.h"
+#include "hdrgen.h"
+
+/********************************* Declaration ****************************/
+
+Declaration::Declaration(Identifier *id)
+    : Dsymbol(id)
+{
+    type = NULL;
+    originalType = NULL;
+    storage_class = STCundefined;
+    protection = PROTundefined;
+    linkage = LINKdefault;
+}
+
+void Declaration::semantic(Scope *sc)
+{
+}
+
+char *Declaration::kind()
+{
+    return "declaration";
+}
+
+unsigned Declaration::size(Loc loc)
+{
+    assert(type);
+    return type->size();
+}
+
+int Declaration::isStaticConstructor()
+{
+    return FALSE;
+}
+
+int Declaration::isStaticDestructor()
+{
+    return FALSE;
+}
+
+int Declaration::isDelete()
+{
+    return FALSE;
+}
+
+int Declaration::isDataseg()
+{
+    return FALSE;
+}
+
+int Declaration::isCodeseg()
+{
+    return FALSE;
+}
+
+enum PROT Declaration::prot()
+{
+    return protection;
+}
+
+/********************************* TupleDeclaration ****************************/
+
+TupleDeclaration::TupleDeclaration(Loc loc, Identifier *id, Objects *objects)
+    : Declaration(id)
+{
+    this->type = NULL;
+    this->objects = objects;
+    this->isexp = 0;
+    this->tupletype = NULL;
+}
+
+Dsymbol *TupleDeclaration::syntaxCopy(Dsymbol *s)
+{
+    assert(0);
+    return NULL;
+}
+
+char *TupleDeclaration::kind()
+{
+    return "tuple";
+}
+
+Type *TupleDeclaration::getType()
+{
+    /* If this tuple represents a type, return that type
+     */
+
+    //printf("TupleDeclaration::getType() %s\n", toChars());
+    if (isexp)
+	return NULL;
+    if (!tupletype)
+    {
+	/* It's only a type tuple if all the Object's are types
+	 */
+	for (size_t i = 0; i < objects->dim; i++)
+	{   Object *o = (Object *)objects->data[i];
+
+	    if (o->dyncast() != DYNCAST_TYPE)
+	    {
+		//printf("\tnot[%d], %p, %d\n", i, o, o->dyncast());
+		return NULL;
+	    }
+	}
+
+	/* We know it's a type tuple, so build the TypeTuple
+	 */
+	Arguments *args = new Arguments();
+	args->setDim(objects->dim);
+	OutBuffer buf;
+	for (size_t i = 0; i < objects->dim; i++)
+	{   Type *t = (Type *)objects->data[i];
+
+	    //printf("type = %s\n", t->toChars());
+#if 0
+	    buf.printf("_%s_%d", ident->toChars(), i);
+	    char *name = (char *)buf.extractData();
+	    Identifier *id = new Identifier(name, TOKidentifier);
+	    Argument *arg = new Argument(STCin, t, id, NULL);
+#else
+	    Argument *arg = new Argument(STCin, t, NULL, NULL);
+#endif
+	    args->data[i] = (void *)arg;
+	}
+
+	tupletype = new TypeTuple(args);
+    }
+
+    return tupletype;
+}
+
+int TupleDeclaration::needThis()
+{
+    //printf("TupleDeclaration::needThis(%s)\n", toChars());
+    for (size_t i = 0; i < objects->dim; i++)
+    {   Object *o = (Object *)objects->data[i];
+	if (o->dyncast() == DYNCAST_EXPRESSION)
+	{   Expression *e = (Expression *)o;
+	    if (e->op == TOKdsymbol)
+	    {	DsymbolExp *ve = (DsymbolExp *)e;
+		Declaration *d = ve->s->isDeclaration();
+		if (d && d->needThis())
+		{
+		    return 1;
+		}
+	    }
+	}
+    }
+    return 0;
+}
+
+/********************************* TypedefDeclaration ****************************/
+
+TypedefDeclaration::TypedefDeclaration(Loc loc, Identifier *id, Type *basetype, Initializer *init)
+    : Declaration(id)
+{
+    this->type = new TypeTypedef(this);
+    this->basetype = basetype->toBasetype();
+    this->init = init;
+#ifdef _DH
+    this->htype = NULL;
+    this->hbasetype = NULL;
+#endif
+    this->sem = 0;
+    this->inuse = 0;
+    this->loc = loc;
+    this->sinit = NULL;
+}
+
+Dsymbol *TypedefDeclaration::syntaxCopy(Dsymbol *s)
+{
+    Type *basetype = this->basetype->syntaxCopy();
+
+    Initializer *init = NULL;
+    if (this->init)
+	init = this->init->syntaxCopy();
+
+    assert(!s);
+    TypedefDeclaration *st;
+    st = new TypedefDeclaration(loc, ident, basetype, init);
+#ifdef _DH
+    // Syntax copy for header file
+    if (!htype)      // Don't overwrite original
+    {	if (type)    // Make copy for both old and new instances
+	{   htype = type->syntaxCopy();
+	    st->htype = type->syntaxCopy();
+	}
+    }
+    else            // Make copy of original for new instance
+        st->htype = htype->syntaxCopy();
+    if (!hbasetype)
+    {	if (basetype)
+	{   hbasetype = basetype->syntaxCopy();
+	    st->hbasetype = basetype->syntaxCopy();
+	}
+    }
+    else
+        st->hbasetype = hbasetype->syntaxCopy();
+#endif
+    return st;
+}
+
+void TypedefDeclaration::semantic(Scope *sc)
+{
+    //printf("TypedefDeclaration::semantic(%s) sem = %d\n", toChars(), sem);
+    if (sem == 0)
+    {	sem = 1;
+	basetype = basetype->semantic(loc, sc);
+	sem = 2;
+	type = type->semantic(loc, sc);
+	if (sc->parent->isFuncDeclaration() && init)
+	    semantic2(sc);
+    }
+    else if (sem == 1)
+    {
+	error("circular definition");
+    }
+}
+
+void TypedefDeclaration::semantic2(Scope *sc)
+{
+    //printf("TypedefDeclaration::semantic2(%s) sem = %d\n", toChars(), sem);
+    if (sem == 2)
+    {	sem = 3;
+	if (init)
+	{
+	    init = init->semantic(sc, basetype);
+
+	    ExpInitializer *ie = init->isExpInitializer();
+	    if (ie)
+	    {
+		if (ie->exp->type == basetype)
+		    ie->exp->type = type;
+	    }
+	}
+    }
+}
+
+char *TypedefDeclaration::kind()
+{
+    return "typedef";
+}
+
+Type *TypedefDeclaration::getType()
+{
+    return type;
+}
+
+void TypedefDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    buf->writestring("typedef ");
+    basetype->toCBuffer(buf, ident, hgs);
+    if (init)
+    {
+	buf->writestring(" = ");
+	init->toCBuffer(buf, hgs);
+    }
+    buf->writeByte(';');
+    buf->writenl();
+}
+
+/********************************* AliasDeclaration ****************************/
+
+AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Type *type)
+    : Declaration(id)
+{
+    //printf("AliasDeclaration(id = '%s', type = %p)\n", id->toChars(), type);
+    //printf("type = '%s'\n", type->toChars());
+    this->loc = loc;
+    this->type = type;
+    this->aliassym = NULL;
+#ifdef _DH
+    this->htype = NULL;
+    this->haliassym = NULL;
+#endif
+    this->overnext = NULL;
+    this->inSemantic = 0;
+    assert(type);
+}
+
+AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Dsymbol *s)
+    : Declaration(id)
+{
+    //printf("AliasDeclaration(id = '%s', s = %p)\n", id->toChars(), s);
+    assert(s != this);
+    this->loc = loc;
+    this->type = NULL;
+    this->aliassym = s;
+#ifdef _DH
+    this->htype = NULL;
+    this->haliassym = NULL;
+#endif
+    this->overnext = NULL;
+    this->inSemantic = 0;
+    assert(s);
+}
+
+Dsymbol *AliasDeclaration::syntaxCopy(Dsymbol *s)
+{
+    assert(!s);
+    AliasDeclaration *sa;
+    if (type)
+	sa = new AliasDeclaration(loc, ident, type->syntaxCopy());
+    else
+	sa = new AliasDeclaration(loc, ident, aliassym->syntaxCopy(NULL));
+#ifdef _DH
+    // Syntax copy for header file
+    if (!htype)	    // Don't overwrite original
+    {	if (type)	// Make copy for both old and new instances
+	{   htype = type->syntaxCopy();
+	    sa->htype = type->syntaxCopy();
+	}
+    }
+    else			// Make copy of original for new instance
+	sa->htype = htype->syntaxCopy();
+    if (!haliassym)
+    {	if (aliassym)
+	{   haliassym = aliassym->syntaxCopy(s);
+	    sa->haliassym = aliassym->syntaxCopy(s);
+	}
+    }
+    else
+	sa->haliassym = haliassym->syntaxCopy(s);
+#endif
+    return sa;
+}
+
+void AliasDeclaration::semantic(Scope *sc)
+{
+    //printf("AliasDeclaration::semantic() %s\n", toChars());
+    if (aliassym)
+    {
+	if (aliassym->isTemplateInstance())
+	    aliassym->semantic(sc);
+	return;
+    }
+    this->inSemantic = 1;
+
+    if (storage_class & STCconst)
+	error("cannot be const");
+
+    storage_class |= sc->stc & STCdeprecated;
+
+    // Given:
+    //	alias foo.bar.abc def;
+    // it is not knowable from the syntax whether this is an alias
+    // for a type or an alias for a symbol. It is up to the semantic()
+    // pass to distinguish.
+    // If it is a type, then type is set and getType() will return that
+    // type. If it is a symbol, then aliassym is set and type is NULL -
+    // toAlias() will return aliasssym.
+
+    Dsymbol *s;
+    Type *t;
+    Expression *e;
+
+    /* This section is needed because resolve() will:
+     *   const x = 3;
+     *   alias x y;
+     * try to alias y to 3.
+     */
+    s = type->toDsymbol(sc);
+    if (s)
+	goto L2;			// it's a symbolic alias
+
+    //printf("alias type is %s\n", type->toChars());
+    type->resolve(loc, sc, &e, &t, &s);
+    if (s)
+    {
+	goto L2;
+    }
+    else if (e)
+    {
+	// Try to convert Expression to Dsymbol
+        if (e->op == TOKvar)
+	{   s = ((VarExp *)e)->var;
+	    goto L2;
+	}
+        else if (e->op == TOKfunction)
+	{   s = ((FuncExp *)e)->fd;
+	    goto L2;
+	}
+        else
+	{   error("cannot alias an expression %s", e->toChars());
+	    t = e->type;
+	}
+    }
+    else if (t)
+	type = t;
+    if (overnext)
+	ScopeDsymbol::multiplyDefined(0, this, overnext);
+    this->inSemantic = 0;
+    return;
+
+  L2:
+    //printf("alias is a symbol %s %s\n", s->kind(), s->toChars());
+    type = NULL;
+    VarDeclaration *v = s->isVarDeclaration();
+    if (v && v->linkage == LINKdefault)
+    {
+	error("forward reference of %s", v->toChars());
+	s = NULL;
+    }
+    else
+    {
+	FuncDeclaration *f = s->toAlias()->isFuncDeclaration();
+	if (f)
+	{
+	    if (overnext)
+	    {
+		FuncAliasDeclaration *fa = new FuncAliasDeclaration(f);
+		if (!fa->overloadInsert(overnext))
+		    ScopeDsymbol::multiplyDefined(0, f, overnext);
+		overnext = NULL;
+		s = fa;
+		s->parent = sc->parent;
+	    }
+	}
+	if (overnext)
+	    ScopeDsymbol::multiplyDefined(0, s, overnext);
+	if (s == this)
+	{
+	    assert(global.errors);
+	    s = NULL;
+	}
+    }
+    aliassym = s;
+    this->inSemantic = 0;
+}
+
+int AliasDeclaration::overloadInsert(Dsymbol *s)
+{
+    /* Don't know yet what the aliased symbol is, so assume it can
+     * be overloaded and check later for correctness.
+     */
+
+    //printf("AliasDeclaration::overloadInsert('%s')\n", s->toChars());
+    if (overnext == NULL)
+    {	overnext = s;
+	return TRUE;
+    }
+    else
+    {
+	return overnext->overloadInsert(s);
+    }
+}
+
+char *AliasDeclaration::kind()
+{
+    return "alias";
+}
+
+Type *AliasDeclaration::getType()
+{
+    return type;
+}
+
+Dsymbol *AliasDeclaration::toAlias()
+{
+    //printf("AliasDeclaration::toAlias('%s', this = %p, aliassym = %p, kind = '%s')\n", toChars(), this, aliassym, aliassym ? aliassym->kind() : "");
+    assert(this != aliassym);
+    //static int count; if (++count == 10) *(char*)0=0;
+    if (inSemantic)
+    {	error("recursive alias declaration");
+//	return this;
+    }
+    Dsymbol *s = aliassym ? aliassym->toAlias() : this;
+    return s;
+}
+
+void AliasDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    buf->writestring("alias ");
+#if 0 && _DH
+    if (hgs->hdrgen)
+    {
+	if (haliassym)
+	{
+	    haliassym->toCBuffer(buf, hgs);
+	    buf->writeByte(' ');
+	    buf->writestring(ident->toChars());
+	}
+	else
+	    htype->toCBuffer(buf, ident, hgs);
+    }
+    else
+#endif
+    {
+	if (aliassym)
+	{
+	    aliassym->toCBuffer(buf, hgs);
+	    buf->writeByte(' ');
+	    buf->writestring(ident->toChars());
+	}
+	else
+	    type->toCBuffer(buf, ident, hgs);
+    }
+    buf->writeByte(';');
+    buf->writenl();
+}
+
+/********************************* VarDeclaration ****************************/
+
+VarDeclaration::VarDeclaration(Loc loc, Type *type, Identifier *id, Initializer *init)
+    : Declaration(id)
+{
+    //printf("VarDeclaration('%s')\n", id->toChars());
+#ifdef DEBUG
+    if (!type && !init)
+    {	printf("VarDeclaration('%s')\n", id->toChars());
+	//*(char*)0=0;
+    }
+#endif
+    assert(type || init);
+    this->type = type;
+    this->init = init;
+#ifdef _DH
+    this->htype = NULL;
+    this->hinit = NULL;
+#endif
+    this->loc = loc;
+    offset = 0;
+    noauto = 0;
+    nestedref = 0;
+    inuse = 0;
+    ctorinit = 0;
+    aliassym = NULL;
+    onstack = 0;
+    canassign = 0;
+    value = NULL;
+
+    // LLVMDC
+    needsStorage = false;
+}
+
+Dsymbol *VarDeclaration::syntaxCopy(Dsymbol *s)
+{
+    //printf("VarDeclaration::syntaxCopy(%s)\n", toChars());
+
+    VarDeclaration *sv;
+    if (s)
+    {	sv = (VarDeclaration *)s;
+    }
+    else
+    {
+	Initializer *init = NULL;
+	if (this->init)
+	{   init = this->init->syntaxCopy();
+	    //init->isExpInitializer()->exp->print();
+	    //init->isExpInitializer()->exp->dump(0);
+	}
+
+	sv = new VarDeclaration(loc, type ? type->syntaxCopy() : NULL, ident, init);
+	sv->storage_class = storage_class;
+    }
+#ifdef _DH
+    // Syntax copy for header file
+    if (!htype)      // Don't overwrite original
+    {	if (type)    // Make copy for both old and new instances
+	{   htype = type->syntaxCopy();
+	    sv->htype = type->syntaxCopy();
+	}
+    }
+    else            // Make copy of original for new instance
+        sv->htype = htype->syntaxCopy();
+    if (!hinit)
+    {	if (init)
+	{   hinit = init->syntaxCopy();
+	    sv->hinit = init->syntaxCopy();
+	}
+    }
+    else
+        sv->hinit = hinit->syntaxCopy();
+#endif
+    return sv;
+}
+
+void VarDeclaration::semantic(Scope *sc)
+{
+    //printf("VarDeclaration::semantic('%s', parent = '%s')\n", toChars(), sc->parent->toChars());
+    //printf("type = %s\n", type->toChars());
+    //printf("linkage = %d\n", sc->linkage);
+    //if (strcmp(toChars(), "mul") == 0) halt();
+
+    storage_class |= sc->stc;
+    if (storage_class & STCextern && init)
+	error("extern symbols cannot have initializers");
+
+    /* If auto type inference, do the inference
+     */
+    int inferred = 0;
+    if (!type)
+    {	inuse++;
+	type = init->inferType(sc);
+	inuse--;
+	inferred = 1;
+
+	/* This is a kludge to support the existing syntax for RAII
+	 * declarations.
+	 */
+	storage_class &= ~STCauto;
+	originalType = type;
+    }
+    else
+    {	if (!originalType)
+	    originalType = type;
+	type = type->semantic(loc, sc);
+    }
+
+    type->checkDeprecated(loc, sc);
+    linkage = sc->linkage;
+    this->parent = sc->parent;
+    //printf("this = %p, parent = %p, '%s'\n", this, parent, parent->toChars());
+    protection = sc->protection;
+    //printf("sc->stc = %x\n", sc->stc);
+    //printf("storage_class = %x\n", storage_class);
+
+    Dsymbol *parent = toParent();
+    FuncDeclaration *fd = parent->isFuncDeclaration();
+
+    Type *tb = type->toBasetype();
+    if (tb->ty == Tvoid && !(storage_class & STClazy))
+    {	error("voids have no value");
+	type = Type::terror;
+	tb = type;
+    }
+    if (tb->ty == Tfunction)
+    {	error("cannot be declared to be a function");
+	type = Type::terror;
+	tb = type;
+    }
+    if (tb->ty == Tstruct)
+    {	TypeStruct *ts = (TypeStruct *)tb;
+
+	if (!ts->sym->members)
+	{
+	    error("no definition of struct %s", ts->toChars());
+	}
+    }
+
+    if (tb->ty == Ttuple)
+    {   /* Instead, declare variables for each of the tuple elements
+	 * and add those.
+	 */
+	TypeTuple *tt = (TypeTuple *)tb;
+	size_t nelems = Argument::dim(tt->arguments);
+	Objects *exps = new Objects();
+	exps->setDim(nelems);
+	Expression *ie = init ? init->toExpression() : NULL;
+
+	for (size_t i = 0; i < nelems; i++)
+	{   Argument *arg = Argument::getNth(tt->arguments, i);
+
+	    OutBuffer buf;
+	    buf.printf("_%s_field_%zu", ident->toChars(), i);
+	    buf.writeByte(0);
+	    char *name = (char *)buf.extractData();
+	    Identifier *id = new Identifier(name, TOKidentifier);
+
+	    Expression *einit = ie;
+	    if (ie && ie->op == TOKtuple)
+	    {	einit = (Expression *)((TupleExp *)ie)->exps->data[i];
+	    }
+	    Initializer *ti = init;
+	    if (einit)
+	    {	ti = new ExpInitializer(einit->loc, einit);
+	    }
+
+	    VarDeclaration *v = new VarDeclaration(loc, arg->type, id, ti);
+	    //printf("declaring field %s of type %s\n", v->toChars(), v->type->toChars());
+	    v->semantic(sc);
+
+	    if (sc->scopesym)
+	    {	//printf("adding %s to %s\n", v->toChars(), sc->scopesym->toChars());
+		if (sc->scopesym->members)
+		    sc->scopesym->members->push(v);
+	    }
+
+	    Expression *e = new DsymbolExp(loc, v);
+	    exps->data[i] = e;
+	}
+	TupleDeclaration *v2 = new TupleDeclaration(loc, ident, exps);
+	v2->isexp = 1;
+	aliassym = v2;
+	return;
+    }
+
+    if (storage_class & STCconst && !init && !fd)
+	// Initialize by constructor only
+	storage_class = (storage_class & ~STCconst) | STCctorinit;
+
+    if (isConst())
+    {
+    }
+    else if (isStatic())
+    {
+    }
+    else if (isSynchronized())
+    {
+	error("variable %s cannot be synchronized", toChars());
+    }
+    else if (isOverride())
+    {
+	error("override cannot be applied to variable");
+    }
+    else if (isAbstract())
+    {
+	error("abstract cannot be applied to variable");
+    }
+    else if (storage_class & STCtemplateparameter)
+    {
+    }
+    else
+    {
+	AggregateDeclaration *aad = sc->anonAgg;
+	if (!aad)
+	    aad = parent->isAggregateDeclaration();
+	if (aad)
+	{
+	    aad->addField(sc, this);
+	}
+
+	InterfaceDeclaration *id = parent->isInterfaceDeclaration();
+	if (id)
+	{
+	    error("field not allowed in interface");
+	}
+
+	TemplateInstance *ti = parent->isTemplateInstance();
+	if (ti)
+	{
+	    // Take care of nested templates
+	    while (1)
+	    {
+		TemplateInstance *ti2 = ti->tempdecl->parent->isTemplateInstance();
+		if (!ti2)
+		    break;
+		ti = ti2;
+	    }
+
+	    // If it's a member template
+	    AggregateDeclaration *ad = ti->tempdecl->isMember();
+	    if (ad && storage_class != STCundefined)
+	    {
+		error("cannot use template to add field to aggregate '%s'", ad->toChars());
+	    }
+	}
+    }
+
+    if (type->isauto() && !noauto)
+    {
+	if (storage_class & (STCfield | STCout | STCref | STCstatic) || !fd)
+	{
+	    error("globals, statics, fields, ref and out parameters cannot be auto");
+	}
+
+	if (!(storage_class & (STCauto | STCscope)))
+	{
+	    if (!(storage_class & STCparameter) && ident != Id::withSym)
+		error("reference to scope class must be scope");
+	}
+    }
+
+    if (!init && !sc->inunion && !isStatic() && !isConst() && fd &&
+	!(storage_class & (STCfield | STCin | STCforeach)) &&
+	type->size() != 0)
+    {
+	// Provide a default initializer
+	//printf("Providing default initializer for '%s'\n", toChars());
+	if (type->ty == Tstruct &&
+	    ((TypeStruct *)type)->sym->zeroInit == 1)
+	{   /* If a struct is all zeros, as a special case
+	     * set it's initializer to the integer 0.
+	     * In AssignExp::toElem(), we check for this and issue
+	     * a memset() to initialize the struct.
+	     * Must do same check in interpreter.
+	     */
+	    Expression *e = new IntegerExp(loc, 0, Type::tint32);
+	    Expression *e1;
+	    e1 = new VarExp(loc, this);
+	    e = new AssignExp(loc, e1, e);
+	    e->type = e1->type;
+	    init = new ExpInitializer(loc, e/*->type->defaultInit()*/);
+	    return;
+	}
+	else if (type->ty == Ttypedef)
+	{   TypeTypedef *td = (TypeTypedef *)type;
+	    if (td->sym->init)
+	    {	init = td->sym->init;
+		ExpInitializer *ie = init->isExpInitializer();
+		if (ie)
+		    // Make copy so we can modify it
+		    init = new ExpInitializer(ie->loc, ie->exp);
+	    }
+	    else
+		init = getExpInitializer();
+	}
+	else
+	{
+	    init = getExpInitializer();
+	}
+    }
+
+    if (init)
+    {
+	ArrayInitializer *ai = init->isArrayInitializer();
+	if (ai && tb->ty == Taarray)
+	{
+	    init = ai->toAssocArrayInitializer();
+	}
+
+	StructInitializer *si = init->isStructInitializer();
+	ExpInitializer *ei = init->isExpInitializer();
+
+	// See if we can allocate on the stack
+	if (ei && isScope() && ei->exp->op == TOKnew)
+	{   NewExp *ne = (NewExp *)ei->exp;
+	    if (!(ne->newargs && ne->newargs->dim))
+	    {	ne->onstack = 1;
+		onstack = 1;
+		if (type->isBaseOf(ne->newtype->semantic(loc, sc), NULL))
+		    onstack = 2;
+	    }
+	}
+
+	// If inside function, there is no semantic3() call
+	if (sc->func)
+	{
+	    // If local variable, use AssignExp to handle all the various
+	    // possibilities.
+	    if (fd && !isStatic() && !isConst() && !init->isVoidInitializer())
+	    {
+		Expression *e1;
+		Type *t;
+		int dim;
+
+		//printf("fd = '%s', var = '%s'\n", fd->toChars(), toChars());
+		if (!ei)
+		{
+		    Expression *e = init->toExpression();
+		    if (!e)
+		    {
+			init = init->semantic(sc, type);
+			e = init->toExpression();
+			if (!e)
+			{   error("is not a static and cannot have static initializer");
+			    return;
+			}
+		    }
+		    ei = new ExpInitializer(init->loc, e);
+		    init = ei;
+		}
+
+		e1 = new VarExp(loc, this);
+
+		t = type->toBasetype();
+		if (t->ty == Tsarray)
+		{
+		    ei->exp = ei->exp->semantic(sc);
+		    if (!ei->exp->implicitConvTo(type))
+		    {
+			dim = ((TypeSArray *)t)->dim->toInteger();
+			// If multidimensional static array, treat as one large array
+			while (1)
+			{
+			    t = t->nextOf()->toBasetype();
+			    if (t->ty != Tsarray)
+				break;
+			    dim *= ((TypeSArray *)t)->dim->toInteger();
+			    e1->type = new TypeSArray(t->nextOf(), new IntegerExp(0, dim, Type::tindex));
+			}
+		    }
+		    e1 = new SliceExp(loc, e1, NULL, NULL);
+		}
+		else if (t->ty == Tstruct)
+		{
+		    ei->exp = ei->exp->semantic(sc);
+		    if (!ei->exp->implicitConvTo(type))
+			ei->exp = new CastExp(loc, ei->exp, type);
+		}
+		ei->exp = new AssignExp(loc, e1, ei->exp);
+		ei->exp->op = TOKconstruct;
+		canassign++;
+		ei->exp = ei->exp->semantic(sc);
+		canassign--;
+		ei->exp->optimize(WANTvalue);
+	    }
+	    else
+	    {
+		init = init->semantic(sc, type);
+		if (fd && isConst() && !isStatic())
+		{   // Make it static
+		    storage_class |= STCstatic;
+		}
+	    }
+	}
+	else if (isConst() || isFinal())
+	{
+	    /* Because we may need the results of a const declaration in a
+	     * subsequent type, such as an array dimension, before semantic2()
+	     * gets ordinarily run, try to run semantic2() now.
+	     * Ignore failure.
+	     */
+
+	    if (!global.errors && !inferred)
+	    {
+		unsigned errors = global.errors;
+		global.gag++;
+		//printf("+gag\n");
+		Expression *e;
+		Initializer *i2 = init;
+		inuse++;
+		if (ei)
+		{
+		    e = ei->exp->syntaxCopy();
+		    e = e->semantic(sc);
+		    e = e->implicitCastTo(sc, type);
+		}
+		else if (si || ai)
+		{   i2 = init->syntaxCopy();
+		    i2 = i2->semantic(sc, type);
+		}
+		inuse--;
+		global.gag--;
+		//printf("-gag\n");
+		if (errors != global.errors)	// if errors happened
+		{
+		    if (global.gag == 0)
+			global.errors = errors;	// act as if nothing happened
+		}
+		else if (ei)
+		{
+		    e = e->optimize(WANTvalue | WANTinterpret);
+		    if (e->op == TOKint64 || e->op == TOKstring)
+		    {
+			ei->exp = e;		// no errors, keep result
+		    }
+		}
+		else
+		    init = i2;		// no errors, keep result
+	    }
+	}
+    }
+}
+
+ExpInitializer *VarDeclaration::getExpInitializer()
+{
+    ExpInitializer *ei;
+
+    if (init)
+	ei = init->isExpInitializer();
+    else
+    {
+	Expression *e = type->defaultInit();
+	if (e)
+	    ei = new ExpInitializer(loc, e);
+	else
+	    ei = NULL;
+    }
+    return ei;
+}
+
+void VarDeclaration::semantic2(Scope *sc)
+{
+    //printf("VarDeclaration::semantic2('%s')\n", toChars());
+    if (init && !toParent()->isFuncDeclaration())
+    {	inuse++;
+#if 0
+	ExpInitializer *ei = init->isExpInitializer();
+	if (ei)
+	{
+	    ei->exp->dump(0);
+	    printf("type = %p\n", ei->exp->type);
+	}
+#endif
+	init = init->semantic(sc, type);
+	inuse--;
+    }
+}
+
+char *VarDeclaration::kind()
+{
+    return "variable";
+}
+
+Dsymbol *VarDeclaration::toAlias()
+{
+    //printf("VarDeclaration::toAlias('%s', this = %p, aliassym = %p)\n", toChars(), this, aliassym);
+    assert(this != aliassym);
+    Dsymbol *s = aliassym ? aliassym->toAlias() : this;
+    return s;
+}
+
+void VarDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    if (storage_class & STCconst)
+	buf->writestring("const ");
+    if (storage_class & STCstatic)
+	buf->writestring("static ");
+    if (type)
+	type->toCBuffer(buf, ident, hgs);
+    else
+	buf->writestring(ident->toChars());
+    if (init)
+    {	buf->writestring(" = ");
+	init->toCBuffer(buf, hgs);
+    }
+    buf->writeByte(';');
+    buf->writenl();
+}
+
+int VarDeclaration::needThis()
+{
+    //printf("VarDeclaration::needThis(%