changeset 336:aaade6ded589 trunk

[svn r357] Merged DMD 1.033
author lindquist
date Sat, 12 Jul 2008 19:38:31 +0200
parents 17b844102023
children d03d748a9b5f
files dmd/access.c dmd/aggregate.h dmd/attrib.c dmd/attrib.h dmd/cast.c dmd/class.c dmd/clone.c dmd/constfold.c dmd/declaration.c dmd/declaration.h dmd/doc.c dmd/dsymbol.c dmd/dsymbol.h dmd/enum.c dmd/enum.h dmd/expression.c dmd/expression.h dmd/func.c dmd/id.c dmd/id.h dmd/idgen.c dmd/import.c dmd/import.h dmd/init.c dmd/inline.c dmd/interpret.c dmd/lexer.c dmd/lexer.h dmd/mars.c dmd/mars.h dmd/module.c dmd/module.h dmd/mtype.c dmd/mtype.h dmd/optimize.c dmd/parse.c dmd/parse.h dmd/root.c dmd/root.h dmd/statement.c dmd/statement.h dmd/staticassert.c dmd/staticassert.h dmd/stringtable.c dmd/stringtable.h dmd/struct.c dmd/template.c dmd/template.h dmd/version.c dmd/version.h gen/asmstmt.cpp gen/classes.cpp gen/dvalue.cpp gen/functions.cpp gen/structs.cpp gen/toir.cpp gen/toobj.cpp gen/typinf.cpp premake.lua
diffstat 59 files changed, 18377 insertions(+), 17003 deletions(-) [+]
line wrap: on
line diff
--- a/dmd/access.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/access.c	Sat Jul 12 19:38:31 2008 +0200
@@ -1,423 +1,424 @@
-
-// 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 <stdlib.h>
-#include <assert.h>
-
-#include "root.h"
-#include "mem.h"
-
-#include "enum.h"
-#include "aggregate.h"
-#include "init.h"
-#include "attrib.h"
-#include "scope.h"
-#include "id.h"
-#include "mtype.h"
-#include "declaration.h"
-#include "aggregate.h"
-#include "expression.h"
-#include "module.h"
-
-#define LOG 0
-
-/* Code to do access checks
- */
-
-int hasPackageAccess(Scope *sc, Dsymbol *s);
-
-/****************************************
- * Return PROT access for Dsymbol smember in this declaration.
- */
-
-enum PROT AggregateDeclaration::getAccess(Dsymbol *smember)
-{
-    return PROTpublic;
-}
-
-enum PROT StructDeclaration::getAccess(Dsymbol *smember)
-{
-    enum PROT access_ret = PROTnone;
-
-#if LOG
-    printf("+StructDeclaration::getAccess(this = '%s', smember = '%s')\n",
-	toChars(), smember->toChars());
-#endif
-    if (smember->toParent() == this)
-    {
-	access_ret = smember->prot();
-    }
-    else if (smember->isDeclaration()->isStatic())
-    {
-	access_ret = smember->prot();
-    }
-    return access_ret;
-}
-
-enum PROT ClassDeclaration::getAccess(Dsymbol *smember)
-{
-    enum PROT access_ret = PROTnone;
-
-#if LOG
-    printf("+ClassDeclaration::getAccess(this = '%s', smember = '%s')\n",
-	toChars(), smember->toChars());
-#endif
-    if (smember->toParent() == this)
-    {
-	access_ret = smember->prot();
-    }
-    else
-    {
-	enum PROT access;
-	int i;
-
-	if (smember->isDeclaration()->isStatic())
-	{
-	    access_ret = smember->prot();
-	}
-
-	for (i = 0; i < baseclasses.dim; i++)
-	{   BaseClass *b = (BaseClass *)baseclasses.data[i];
-
-	    access = b->base->getAccess(smember);
-	    switch (access)
-	    {
-		case PROTnone:
-		    break;
-
-		case PROTprivate:
-		    access = PROTnone;	// private members of base class not accessible
-		    break;
-
-		case PROTpackage:
-		case PROTprotected:
-		case PROTpublic:
-		case PROTexport:
-		    // If access is to be tightened
-		    if (b->protection < access)
-			access = b->protection;
-
-		    // Pick path with loosest access
-		    if (access > access_ret)
-			access_ret = access;
-		    break;
-
-		default:
-		    assert(0);
-	    }
-	}
-    }
-#if LOG
-    printf("-ClassDeclaration::getAccess(this = '%s', smember = '%s') = %d\n",
-	toChars(), smember->toChars(), access_ret);
-#endif
-    return access_ret;
-}
-
-/********************************************************
- * Helper function for ClassDeclaration::accessCheck()
- * Returns:
- *	0	no access
- * 	1	access
- */
-
-static int accessCheckX(
-	Dsymbol *smember,
-	Dsymbol *sfunc,
-	AggregateDeclaration *dthis,
-	AggregateDeclaration *cdscope)
-{
-    assert(dthis);
-
-#if 0
-    printf("accessCheckX for %s.%s in function %s() in scope %s\n",
-	dthis->toChars(), smember->toChars(),
-	sfunc ? sfunc->toChars() : "NULL",
-	cdscope ? cdscope->toChars() : "NULL");
-#endif
-    if (dthis->hasPrivateAccess(sfunc) ||
-	dthis->isFriendOf(cdscope))
-    {
-	if (smember->toParent() == dthis)
-	    return 1;
-	else
-	{
-	    ClassDeclaration *cdthis = dthis->isClassDeclaration();
-	    if (cdthis)
-	    {
-		for (int i = 0; i < cdthis->baseclasses.dim; i++)
-		{   BaseClass *b = (BaseClass *)cdthis->baseclasses.data[i];
-		    enum PROT access;
-
-		    access = b->base->getAccess(smember);
-		    if (access >= PROTprotected ||
-			accessCheckX(smember, sfunc, b->base, cdscope)
-		       )
-			return 1;
-
-		}
-	    }
-	}
-    }
-    else
-    {
-	if (smember->toParent() != dthis)
-	{
-	    ClassDeclaration *cdthis = dthis->isClassDeclaration();
-	    if (cdthis)
-	    {
-		for (int i = 0; i < cdthis->baseclasses.dim; i++)
-		{   BaseClass *b = (BaseClass *)cdthis->baseclasses.data[i];
-
-		    if (accessCheckX(smember, sfunc, b->base, cdscope))
-			return 1;
-		}
-	    }
-	}
-    }
-    return 0;
-}
-
-/*******************************
- * Do access check for member of this class, this class being the
- * type of the 'this' pointer used to access smember.
- */
-
-void AggregateDeclaration::accessCheck(Loc loc, Scope *sc, Dsymbol *smember)
-{
-    int result;
-
-    FuncDeclaration *f = sc->func;
-    AggregateDeclaration *cdscope = sc->getStructClassScope();
-    enum PROT access;
-
-#if LOG
-    printf("AggregateDeclaration::accessCheck() for %s.%s in function %s() in scope %s\n",
-	toChars(), smember->toChars(),
-	f ? f->toChars() : NULL,
-	cdscope ? cdscope->toChars() : NULL);
-#endif
-
-    Dsymbol *smemberparent = smember->toParent();
-    if (!smemberparent || !smemberparent->isAggregateDeclaration())
-    {
-#if LOG
-	printf("not an aggregate member\n");
-#endif
-	return;				// then it is accessible
-    }
-
-    // BUG: should enable this check
-    //assert(smember->parent->isBaseOf(this, NULL));
-
-    if (smemberparent == this)
-    {	enum PROT access = smember->prot();
-
-	result = access >= PROTpublic ||
-		hasPrivateAccess(f) ||
-		isFriendOf(cdscope) ||
-		(access == PROTpackage && hasPackageAccess(sc, this));
-#if LOG
-	printf("result1 = %d\n", result);
-#endif
-    }
-    else if ((access = this->getAccess(smember)) >= PROTpublic)
-    {
-	result = 1;
-#if LOG
-	printf("result2 = %d\n", result);
-#endif
-    }
-    else if (access == PROTpackage && hasPackageAccess(sc, this))
-    {
-	result = 1;
-#if LOG
-	printf("result3 = %d\n", result);
-#endif
-    }
-    else
-    {
-	result = accessCheckX(smember, f, this, cdscope);
-#if LOG
-	printf("result4 = %d\n", result);
-#endif
-    }
-    if (!result)
-    {
-	error(loc, "member %s is not accessible", smember->toChars());
-    }
-}
-
-/****************************************
- * Determine if this is the same or friend of cd.
- */
-
-int AggregateDeclaration::isFriendOf(AggregateDeclaration *cd)
-{
-#if LOG
-    printf("AggregateDeclaration::isFriendOf(this = '%s', cd = '%s')\n", toChars(), cd ? cd->toChars() : "null");
-#endif
-    if (this == cd)
-	return 1;
-
-    // Friends if both are in the same module
-    //if (toParent() == cd->toParent())
-    if (cd && getModule() == cd->getModule())
-    {
-#if LOG
-	printf("\tin same module\n");
-#endif
-	return 1;
-    }
-
-#if LOG
-    printf("\tnot friend\n");
-#endif
-    return 0;
-}
-
-/****************************************
- * Determine if scope sc has package level access to s.
- */
-
-int hasPackageAccess(Scope *sc, Dsymbol *s)
-{
-#if LOG
-    printf("hasPackageAccess(s = '%s', sc = '%p')\n", s->toChars(), sc);
-#endif
-
-    for (; s; s = s->parent)
-    {
-	if (s->isPackage() && !s->isModule())
-	    break;
-    }
-#if LOG
-    if (s)
-	printf("\tthis is in package '%s'\n", s->toChars());
-#endif
-
-    if (s && s == sc->module->parent)
-    {
-#if LOG
-	printf("\ts is in same package as sc\n");
-#endif
-	return 1;
-    }
-
-
-#if LOG
-    printf("\tno package access\n");
-#endif
-    return 0;
-}
-
-/**********************************
- * Determine if smember has access to private members of this declaration.
- */
-
-int AggregateDeclaration::hasPrivateAccess(Dsymbol *smember)
-{
-    if (smember)
-    {	AggregateDeclaration *cd = NULL;
-	Dsymbol *smemberparent = smember->toParent();
-	if (smemberparent)
-	    cd = smemberparent->isAggregateDeclaration();
-
-#if LOG
-	printf("AggregateDeclaration::hasPrivateAccess(class %s, member %s)\n",
-		toChars(), smember->toChars());
-#endif
-
-	if (this == cd)		// smember is a member of this class
-	{
-#if LOG
-	    printf("\tyes 1\n");
-#endif
-	    return 1;		// so we get private access
-	}
-
-	// If both are members of the same module, grant access
-	while (1)
-	{   Dsymbol *sp = smember->toParent();
-	    if (sp->isFuncDeclaration() && smember->isFuncDeclaration())
-		smember = sp;
-	    else
-		break;
-	}
-	if (!cd && toParent() == smember->toParent())
-	{
-#if LOG
-	    printf("\tyes 2\n");
-#endif
-	    return 1;
-	}
-	if (!cd && getModule() == smember->getModule())
-	{
-#if LOG
-	    printf("\tyes 3\n");
-#endif
-	    return 1;
-	}
-    }
-#if LOG
-    printf("\tno\n");
-#endif
-    return 0;
-}
-
-/****************************************
- * Check access to d for expression e.d
- */
-
-void accessCheck(Loc loc, Scope *sc, Expression *e, Declaration *d)
-{
-#if LOG
-    if (e)
-    {	printf("accessCheck(%s . %s)\n", e->toChars(), d->toChars());
-	printf("\te->type = %s\n", e->type->toChars());
-    }
-    else
-    {
-	//printf("accessCheck(%s)\n", d->toChars());
-    }
-#endif
-    if (!e)
-    {
-	if (d->prot() == PROTprivate && d->getModule() != sc->module ||
-	    d->prot() == PROTpackage && !hasPackageAccess(sc, d))
-
-	    error(loc, "%s %s.%s is not accessible from %s",
-		d->kind(), d->getModule()->toChars(), d->toChars(), sc->module->toChars());
-    }
-    else if (e->type->ty == Tclass)
-    {   // Do access check
-	ClassDeclaration *cd;
-
-	cd = (ClassDeclaration *)(((TypeClass *)e->type)->sym);
-#if 1
-	if (e->op == TOKsuper)
-	{   ClassDeclaration *cd2;
-
-	    cd2 = sc->func->toParent()->isClassDeclaration();
-	    if (cd2)
-		cd = cd2;
-	}
-#endif
-	cd->accessCheck(loc, sc, d);
-    }
-    else if (e->type->ty == Tstruct)
-    {   // Do access check
-	StructDeclaration *cd;
-
-	cd = (StructDeclaration *)(((TypeStruct *)e->type)->sym);
-	cd->accessCheck(loc, sc, d);
-    }
-}
+
+// 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 <stdlib.h>
+#include <assert.h>
+
+#include "root.h"
+#include "mem.h"
+
+#include "enum.h"
+#include "aggregate.h"
+#include "init.h"
+#include "attrib.h"
+#include "scope.h"
+#include "id.h"
+#include "mtype.h"
+#include "declaration.h"
+#include "aggregate.h"
+#include "expression.h"
+#include "module.h"
+
+#define LOG 0
+
+/* Code to do access checks
+ */
+
+int hasPackageAccess(Scope *sc, Dsymbol *s);
+
+/****************************************
+ * Return PROT access for Dsymbol smember in this declaration.
+ */
+
+enum PROT AggregateDeclaration::getAccess(Dsymbol *smember)
+{
+    return PROTpublic;
+}
+
+enum PROT StructDeclaration::getAccess(Dsymbol *smember)
+{
+    enum PROT access_ret = PROTnone;
+
+#if LOG
+    printf("+StructDeclaration::getAccess(this = '%s', smember = '%s')\n",
+	toChars(), smember->toChars());
+#endif
+    if (smember->toParent() == this)
+    {
+	access_ret = smember->prot();
+    }
+    else if (smember->isDeclaration()->isStatic())
+    {
+	access_ret = smember->prot();
+    }
+    return access_ret;
+}
+
+enum PROT ClassDeclaration::getAccess(Dsymbol *smember)
+{
+    enum PROT access_ret = PROTnone;
+
+#if LOG
+    printf("+ClassDeclaration::getAccess(this = '%s', smember = '%s')\n",
+	toChars(), smember->toChars());
+#endif
+    if (smember->toParent() == this)
+    {
+	access_ret = smember->prot();
+    }
+    else
+    {
+	enum PROT access;
+	int i;
+
+	if (smember->isDeclaration()->isStatic())
+	{
+	    access_ret = smember->prot();
+	}
+
+	for (i = 0; i < baseclasses.dim; i++)
+	{   BaseClass *b = (BaseClass *)baseclasses.data[i];
+
+	    access = b->base->getAccess(smember);
+	    switch (access)
+	    {
+		case PROTnone:
+		    break;
+
+		case PROTprivate:
+		    access = PROTnone;	// private members of base class not accessible
+		    break;
+
+		case PROTpackage:
+		case PROTprotected:
+		case PROTpublic:
+		case PROTexport:
+		    // If access is to be tightened
+		    if (b->protection < access)
+			access = b->protection;
+
+		    // Pick path with loosest access
+		    if (access > access_ret)
+			access_ret = access;
+		    break;
+
+		default:
+		    assert(0);
+	    }
+	}
+    }
+#if LOG
+    printf("-ClassDeclaration::getAccess(this = '%s', smember = '%s') = %d\n",
+	toChars(), smember->toChars(), access_ret);
+#endif
+    return access_ret;
+}
+
+/********************************************************
+ * Helper function for ClassDeclaration::accessCheck()
+ * Returns:
+ *	0	no access
+ * 	1	access
+ */
+
+static int accessCheckX(
+	Dsymbol *smember,
+	Dsymbol *sfunc,
+	AggregateDeclaration *dthis,
+	AggregateDeclaration *cdscope)
+{
+    assert(dthis);
+
+#if 0
+    printf("accessCheckX for %s.%s in function %s() in scope %s\n",
+	dthis->toChars(), smember->toChars(),
+	sfunc ? sfunc->toChars() : "NULL",
+	cdscope ? cdscope->toChars() : "NULL");
+#endif
+    if (dthis->hasPrivateAccess(sfunc) ||
+	dthis->isFriendOf(cdscope))
+    {
+	if (smember->toParent() == dthis)
+	    return 1;
+	else
+	{
+	    ClassDeclaration *cdthis = dthis->isClassDeclaration();
+	    if (cdthis)
+	    {
+		for (int i = 0; i < cdthis->baseclasses.dim; i++)
+		{   BaseClass *b = (BaseClass *)cdthis->baseclasses.data[i];
+		    enum PROT access;
+
+		    access = b->base->getAccess(smember);
+		    if (access >= PROTprotected ||
+			accessCheckX(smember, sfunc, b->base, cdscope)
+		       )
+			return 1;
+
+		}
+	    }
+	}
+    }
+    else
+    {
+	if (smember->toParent() != dthis)
+	{
+	    ClassDeclaration *cdthis = dthis->isClassDeclaration();
+	    if (cdthis)
+	    {
+		for (int i = 0; i < cdthis->baseclasses.dim; i++)
+		{   BaseClass *b = (BaseClass *)cdthis->baseclasses.data[i];
+
+		    if (accessCheckX(smember, sfunc, b->base, cdscope))
+			return 1;
+		}
+	    }
+	}
+    }
+    return 0;
+}
+
+/*******************************
+ * Do access check for member of this class, this class being the
+ * type of the 'this' pointer used to access smember.
+ */
+
+void AggregateDeclaration::accessCheck(Loc loc, Scope *sc, Dsymbol *smember)
+{
+    int result;
+
+    FuncDeclaration *f = sc->func;
+    AggregateDeclaration *cdscope = sc->getStructClassScope();
+    enum PROT access;
+
+#if LOG
+    printf("AggregateDeclaration::accessCheck() for %s.%s in function %s() in scope %s\n",
+	toChars(), smember->toChars(),
+	f ? f->toChars() : NULL,
+	cdscope ? cdscope->toChars() : NULL);
+#endif
+
+    Dsymbol *smemberparent = smember->toParent();
+    if (!smemberparent || !smemberparent->isAggregateDeclaration())
+    {
+#if LOG
+	printf("not an aggregate member\n");
+#endif
+	return;				// then it is accessible
+    }
+
+    // BUG: should enable this check
+    //assert(smember->parent->isBaseOf(this, NULL));
+
+    if (smemberparent == this)
+    {	enum PROT access = smember->prot();
+
+	result = access >= PROTpublic ||
+		hasPrivateAccess(f) ||
+		isFriendOf(cdscope) ||
+		(access == PROTpackage && hasPackageAccess(sc, this));
+#if LOG
+	printf("result1 = %d\n", result);
+#endif
+    }
+    else if ((access = this->getAccess(smember)) >= PROTpublic)
+    {
+	result = 1;
+#if LOG
+	printf("result2 = %d\n", result);
+#endif
+    }
+    else if (access == PROTpackage && hasPackageAccess(sc, this))
+    {
+	result = 1;
+#if LOG
+	printf("result3 = %d\n", result);
+#endif
+    }
+    else
+    {
+	result = accessCheckX(smember, f, this, cdscope);
+#if LOG
+	printf("result4 = %d\n", result);
+#endif
+    }
+    if (!result)
+    {
+	error(loc, "member %s is not accessible", smember->toChars());
+halt();
+    }
+}
+
+/****************************************   
+ * Determine if this is the same or friend of cd.
+ */
+
+int AggregateDeclaration::isFriendOf(AggregateDeclaration *cd)
+{
+#if LOG
+    printf("AggregateDeclaration::isFriendOf(this = '%s', cd = '%s')\n", toChars(), cd ? cd->toChars() : "null");
+#endif
+    if (this == cd)
+	return 1;
+
+    // Friends if both are in the same module
+    //if (toParent() == cd->toParent())
+    if (cd && getModule() == cd->getModule())
+    {
+#if LOG
+	printf("\tin same module\n");
+#endif
+	return 1;
+    }
+
+#if LOG
+    printf("\tnot friend\n");
+#endif
+    return 0;
+}
+
+/****************************************
+ * Determine if scope sc has package level access to s.
+ */
+
+int hasPackageAccess(Scope *sc, Dsymbol *s)
+{
+#if LOG
+    printf("hasPackageAccess(s = '%s', sc = '%p')\n", s->toChars(), sc);
+#endif
+
+    for (; s; s = s->parent)
+    {
+	if (s->isPackage() && !s->isModule())
+	    break;
+    }
+#if LOG
+    if (s)
+	printf("\tthis is in package '%s'\n", s->toChars());
+#endif
+
+    if (s && s == sc->module->parent)
+    {
+#if LOG
+	printf("\ts is in same package as sc\n");
+#endif
+	return 1;
+    }
+
+
+#if LOG
+    printf("\tno package access\n");
+#endif
+    return 0;
+}
+
+/**********************************
+ * Determine if smember has access to private members of this declaration.
+ */
+
+int AggregateDeclaration::hasPrivateAccess(Dsymbol *smember)
+{
+    if (smember)
+    {	AggregateDeclaration *cd = NULL;
+	Dsymbol *smemberparent = smember->toParent();
+	if (smemberparent)
+	    cd = smemberparent->isAggregateDeclaration();
+
+#if LOG
+	printf("AggregateDeclaration::hasPrivateAccess(class %s, member %s)\n",
+		toChars(), smember->toChars());
+#endif
+
+	if (this == cd)		// smember is a member of this class
+	{
+#if LOG
+	    printf("\tyes 1\n");
+#endif
+	    return 1;		// so we get private access
+	}
+
+	// If both are members of the same module, grant access
+	while (1)
+	{   Dsymbol *sp = smember->toParent();
+	    if (sp->isFuncDeclaration() && smember->isFuncDeclaration())
+		smember = sp;
+	    else
+		break;
+	}
+	if (!cd && toParent() == smember->toParent())
+	{
+#if LOG
+	    printf("\tyes 2\n");
+#endif
+	    return 1;
+	}
+	if (!cd && getModule() == smember->getModule())
+	{
+#if LOG
+	    printf("\tyes 3\n");
+#endif
+	    return 1;
+	}
+    }
+#if LOG
+    printf("\tno\n");
+#endif
+    return 0;
+}
+
+/****************************************
+ * Check access to d for expression e.d
+ */
+
+void accessCheck(Loc loc, Scope *sc, Expression *e, Declaration *d)
+{
+#if LOG
+    if (e)
+    {	printf("accessCheck(%s . %s)\n", e->toChars(), d->toChars());
+	printf("\te->type = %s\n", e->type->toChars());
+    }
+    else
+    {
+	//printf("accessCheck(%s)\n", d->toChars());
+    }
+#endif
+    if (!e)
+    {
+	if (d->prot() == PROTprivate && d->getModule() != sc->module ||
+	    d->prot() == PROTpackage && !hasPackageAccess(sc, d))
+
+	    error(loc, "%s %s.%s is not accessible from %s",
+		d->kind(), d->getModule()->toChars(), d->toChars(), sc->module->toChars());
+    }
+    else if (e->type->ty == Tclass)
+    {   // Do access check
+	ClassDeclaration *cd;
+
+	cd = (ClassDeclaration *)(((TypeClass *)e->type)->sym);
+#if 1
+	if (e->op == TOKsuper)
+	{   ClassDeclaration *cd2;
+
+	    cd2 = sc->func->toParent()->isClassDeclaration();
+	    if (cd2)
+		cd = cd2;
+	}
+#endif
+	cd->accessCheck(loc, sc, d);
+    }
+    else if (e->type->ty == Tstruct)
+    {   // Do access check
+	StructDeclaration *cd;
+
+	cd = (StructDeclaration *)(((TypeStruct *)e->type)->sym);
+	cd->accessCheck(loc, sc, d);
+    }
+}
--- a/dmd/aggregate.h	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/aggregate.h	Sat Jul 12 19:38:31 2008 +0200
@@ -64,14 +64,15 @@
 				// 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
 
+    FuncDeclarations dtors;	// Array of destructors
+    FuncDeclaration *dtor;	// aggregate destructor
+
 #ifdef IN_GCC
     Array methods;              // flat list of all methods for debug information
 #endif
@@ -119,19 +120,26 @@
 struct StructDeclaration : AggregateDeclaration
 {
     int zeroInit;		// !=0 if initialize with 0 fill
+#if DMDV2
+    int hasIdentityAssign;	// !=0 if has identity opAssign
+    FuncDeclaration *cpctor;	// generated copy-constructor, if any
+
+    FuncDeclarations postblits;	// Array of postblit functions
+    FuncDeclaration *postblit;	// aggregate postblit
+#endif
 
     StructDeclaration(Loc loc, Identifier *id);
     Dsymbol *syntaxCopy(Dsymbol *s);
     void semantic(Scope *sc);
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
     char *mangle();
-    char *kind();
+    const char *kind();
     Expression *cloneMembers();
     void toDocBuffer(OutBuffer *buf);
 
     PROT getAccess(Dsymbol *smember);	// determine access to smember
 
-    void toObjFile();			// compile to .obj file
+    void toObjFile(int multiobj);			// compile to .obj file
     void toDt(dt_t **pdt);
     void toDebug();			// to symbolic debug info
 
@@ -142,7 +150,7 @@
 {
     UnionDeclaration(Loc loc, Identifier *id);
     Dsymbol *syntaxCopy(Dsymbol *s);
-    char *kind();
+    const char *kind();
 
     UnionDeclaration *isUnionDeclaration() { return this; }
 };
@@ -168,7 +176,7 @@
     void copyBaseInterfaces(BaseClasses *);
 };
 
-#if V2
+#if DMDV2
 #define CLASSINFO_SIZE 	(0x3C+16)	// value of ClassInfo.size
 #else
 #define CLASSINFO_SIZE 	(0x3C+12)	// value of ClassInfo.size
@@ -218,7 +226,7 @@
     virtual int isBaseOf(ClassDeclaration *cd, int *poffset);
 
     Dsymbol *search(Loc, Identifier *ident, int flags);
-#if V2
+#if DMDV2
     int isFuncHidden(FuncDeclaration *fd);
 #endif
     FuncDeclaration *findFunc(Identifier *ident, TypeFunction *tf);
@@ -226,12 +234,12 @@
     int isNested();
     int isCOMclass();
     virtual int isCOMinterface();
-#if V2
+#if DMDV2
     virtual int isCPPinterface();
 #endif
     int isAbstract();
     virtual int vtblOffset();
-    char *kind();
+    const char *kind();
     char *mangle();
     void toDocBuffer(OutBuffer *buf);
 
@@ -240,7 +248,7 @@
     void addLocalClass(ClassDeclarations *);
 
     // Back end
-    void toObjFile();			// compile to .obj file
+    void toObjFile(int multiobj);			// compile to .obj file
     void toDebug();
     unsigned baseVtblOffset(BaseClass *bc);
     Symbol *toSymbol();
@@ -258,7 +266,7 @@
 
 struct InterfaceDeclaration : ClassDeclaration
 {
-#if V2
+#if DMDV2
     int cpp;				// !=0 if this is a C++ interface
 #endif
     InterfaceDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses);
@@ -266,14 +274,14 @@
     void semantic(Scope *sc);
     int isBaseOf(ClassDeclaration *cd, int *poffset);
     int isBaseOf(BaseClass *bc, int *poffset);
-    char *kind();
+    const char *kind();
     int vtblOffset();
-#if V2
+#if DMDV2
     int isCPPinterface();
 #endif
     virtual int isCOMinterface();
 
-    void toObjFile();			// compile to .obj file
+    void toObjFile(int multiobj);			// compile to .obj file
     Symbol *toSymbol();
 
     InterfaceDeclaration *isInterfaceDeclaration() { return this; }
--- a/dmd/attrib.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/attrib.c	Sat Jul 12 19:38:31 2008 +0200
@@ -1,6 +1,6 @@
 
 // Compiler implementation of the D programming language
-// Copyright (c) 1999-2007 by Digital Mars
+// Copyright (c) 1999-2008 by Digital Mars
 // All Rights Reserved
 // written by Walter Bright
 // http://www.digitalmars.com
@@ -35,6 +35,7 @@
 #include "../gen/logger.h"
 
 extern void obj_includelib(char *name);
+void obj_startaddress(Symbol *s);
 
 
 /********************************* AttribDeclaration ****************************/
@@ -52,16 +53,13 @@
 
 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];
+	for (unsigned i = 0; i < d->dim; i++)
+	{   Dsymbol *s = (Dsymbol *)d->data[i];
 	    m |= s->addMember(sc, sd, m | memnum);
 	}
     }
@@ -72,7 +70,7 @@
 {
     Array *d = include(sc, NULL);
 
-    //printf("\tAttribDeclaration::semantic '%s'\n",toChars());
+    //printf("\tAttribDeclaration::semantic '%s', d = %p\n",toChars(), d);
     if (d)
     {
 	for (unsigned i = 0; i < d->dim; i++)
@@ -86,15 +84,12 @@
 
 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];
+	for (unsigned i = 0; i < d->dim; i++)
+	{   Dsymbol *s = (Dsymbol *)d->data[i];
 	    s->semantic2(sc);
 	}
     }
@@ -102,15 +97,12 @@
 
 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];
+	for (unsigned i = 0; i < d->dim; i++)
+	{   Dsymbol *s = (Dsymbol *)d->data[i];
 	    s->semantic3(sc);
 	}
     }
@@ -118,15 +110,12 @@
 
 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];
+	for (unsigned i = 0; i < d->dim; i++)
+	{   Dsymbol *s = (Dsymbol *)d->data[i];
 	    //printf("AttribDeclaration::inlineScan %s\n", s->toChars());
 	    s->inlineScan();
 	}
@@ -137,15 +126,12 @@
 {
     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];
+	    for (unsigned i = 0; i < d->dim; i++)
+	    {   Dsymbol *s = (Dsymbol *)d->data[i];
 		//printf("AttribDeclaration::addComment %s\n", s->toChars());
 		s->addComment(comment);
 	    }
@@ -163,50 +149,41 @@
 //    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];
+	for (unsigned i = 0; i < d->dim; i++)
+	{   Dsymbol *s = (Dsymbol *)d->data[i];
 	    //printf("AttribDeclaration::emitComment %s\n", s->toChars());
 	    s->emitComment(sc);
 	}
     }
 }
 
-void AttribDeclaration::toObjFile()
+void AttribDeclaration::toObjFile(int multiobj)
 {
-    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();
+	for (unsigned i = 0; i < d->dim; i++)
+	{   Dsymbol *s = (Dsymbol *)d->data[i];
+	    s->toObjFile(multiobj);
 	}
     }
 }
 
 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];
+	for (unsigned i = 0; i < d->dim; i++)
+	{   Dsymbol *s = (Dsymbol *)d->data[i];
 	    n = s->cvMember(p);
 	    if (p)
 		p += n;
@@ -232,7 +209,7 @@
     return 0;
 }
 
-char *AttribDeclaration::kind()
+const char *AttribDeclaration::kind()
 {
     return "attribute";
 }
@@ -246,15 +223,12 @@
 
 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];
+	for (unsigned i = 0; i < d->dim; i++)
+	{   Dsymbol *s = (Dsymbol *)d->data[i];
 	    s->checkCtorConstInit();
 	}
     }
@@ -264,15 +238,13 @@
  */
 
 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];
+	for (unsigned i = 0; i < d->dim; i++)
+	{   Dsymbol *s = (Dsymbol *)d->data[i];
 	    s->addLocalClass(aclasses);
 	}
     }
@@ -723,7 +695,7 @@
     buf->writestring("}\n");
 }
 
-char *AnonDeclaration::kind()
+const char *AnonDeclaration::kind()
 {
     return (char *)(isunion ? "anonymous union" : "anonymous struct");
 }
@@ -1023,12 +995,12 @@
     return TRUE;
 }
 
-char *PragmaDeclaration::kind()
+const char *PragmaDeclaration::kind()
 {
     return "pragma";
 }
 
-void PragmaDeclaration::toObjFile()
+void PragmaDeclaration::toObjFile(int multiobj)
 {
     if (ident == Id::lib)
     {
@@ -1044,7 +1016,7 @@
 	name[se->len] = 0;
 	obj_includelib(name);
     }
-    AttribDeclaration::toObjFile();
+    AttribDeclaration::toObjFile(multiobj);
 }
 
 void PragmaDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
@@ -1214,6 +1186,7 @@
 
 int StaticIfDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
 {
+    //printf("StaticIfDeclaration::addMember() '%s'\n",toChars());
     /* This is deferred until semantic(), so that
      * expressions in the condition can refer to declarations
      * in the same scope, such as:
@@ -1240,7 +1213,7 @@
 {
     Array *d = include(sc, sd);
 
-    //printf("\tStaticIfDeclaration::semantic '%s'\n",toChars());
+    //printf("\tStaticIfDeclaration::semantic '%s', d = %p\n",toChars(), d);
     if (d)
     {
 	if (!addisdone)
@@ -1257,7 +1230,7 @@
     }
 }
 
-char *StaticIfDeclaration::kind()
+const char *StaticIfDeclaration::kind()
 {
     return "static if";
 }
@@ -1268,8 +1241,10 @@
 CompileDeclaration::CompileDeclaration(Loc loc, Expression *exp)
     : AttribDeclaration(NULL)
 {
+    this->loc = loc;
     this->exp = exp;
     this->sd = NULL;
+    this->compiled = 0;
 }
 
 Dsymbol *CompileDeclaration::syntaxCopy(Dsymbol *s)
@@ -1281,32 +1256,50 @@
 
 int CompileDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
 {
+    //printf("CompileDeclaration::addMember(sc = %p)\n", sc);
     this->sd = sd;
+    if (memnum == 0)
+    {	/* No members yet, so parse the mixin now
+	 */
+	compileIt(sc);
+	memnum |= AttribDeclaration::addMember(sc, sd, memnum);
+	compiled = 1;
+    }
     return memnum;
 }
 
+void CompileDeclaration::compileIt(Scope *sc)
+{
+    //printf("CompileDeclaration::compileIt()\n");
+    exp = exp->semantic(sc);
+    exp = resolveProperties(sc, exp);
+    exp = exp->optimize(WANTvalue | WANTinterpret);
+    if (exp->op != TOKstring)
+    {	exp->error("argument to mixin must be a string, not (%s)", exp->toChars());
+    }
+    else
+    {
+	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)
+	    exp->error("incomplete mixin declaration (%s)", se->toChars());
+    }
+}
+
 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;
+
+    if (!compiled)
+    {
+	compileIt(sc);
+	AttribDeclaration::addMember(sc, sd, 0);
+	compiled = 1;
     }
-    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);
 }
 
--- a/dmd/attrib.h	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/attrib.h	Sat Jul 12 19:38:31 2008 +0200
@@ -42,7 +42,7 @@
     void inlineScan();
     void addComment(unsigned char *comment);
     void emitComment(Scope *sc);
-    char *kind();
+    const char *kind();
     int oneMember(Dsymbol **ps);
     int hasPointers();
     void checkCtorConstInit();
@@ -50,7 +50,7 @@
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
     AttribDeclaration *isAttribDeclaration() { return this; }
 
-    void toObjFile();			// compile to .obj file
+    void toObjFile(int multiobj);			// compile to .obj file
     int cvMember(unsigned char *p);
 };
 
@@ -106,7 +106,7 @@
     Dsymbol *syntaxCopy(Dsymbol *s);
     void semantic(Scope *sc);
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
-    char *kind();
+    const char *kind();
 };
 
 struct PragmaDeclaration : AttribDeclaration
@@ -118,8 +118,8 @@
     void semantic(Scope *sc);
     int oneMember(Dsymbol **ps);
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
-    char *kind();
-    void toObjFile();			// compile to .obj file
+    const char *kind();
+    void toObjFile(int multiobj);			// compile to .obj file
 };
 
 struct ConditionalDeclaration : AttribDeclaration
@@ -145,7 +145,7 @@
     Dsymbol *syntaxCopy(Dsymbol *s);
     int addMember(Scope *sc, ScopeDsymbol *s, int memnum);
     void semantic(Scope *sc);
-    char *kind();
+    const char *kind();
 };
 
 // Mixin declarations
@@ -155,10 +155,12 @@
     Expression *exp;
 
     ScopeDsymbol *sd;
+    int compiled;
 
     CompileDeclaration(Loc loc, Expression *exp);
     Dsymbol *syntaxCopy(Dsymbol *s);
     int addMember(Scope *sc, ScopeDsymbol *sd, int memnum);
+    void compileIt(Scope *sc);
     void semantic(Scope *sc);
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
 };
--- a/dmd/cast.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/cast.c	Sat Jul 12 19:38:31 2008 +0200
@@ -1,5 +1,5 @@
 
-// Copyright (c) 1999-2006 by Digital Mars
+// Copyright (c) 1999-2008 by Digital Mars
 // All Rights Reserved
 // written by Walter Bright
 // http://www.digitalmars.com
@@ -1345,31 +1345,52 @@
 	goto Lt1;
     }
     else if (t1->ty == Tclass || t2->ty == Tclass)
-    {	int i1;
-	int i2;
+    {
+	while (1)
+	{
+	    int i1 = e2->implicitConvTo(t1);
+	    int i2 = e1->implicitConvTo(t2);
 
-	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 (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 if (t1->ty == Tclass && t2->ty == Tclass)
+	    {	TypeClass *tc1 = (TypeClass *)t1;
+		TypeClass *tc2 = (TypeClass *)t2;
+
+		/* Pick 'tightest' type
+		 */
+		ClassDeclaration *cd1 = tc1->sym->baseClass;
+		ClassDeclaration *cd2 = tc1->sym->baseClass;
+
+		if (cd1 && cd2)
+		{   t1 = cd1->type;
+		    t2 = cd2->type;
+		}
+		else if (cd1)
+		    t1 = cd1->type;
+		else if (cd2)
+		    t2 = cd2->type;
+		else
+		    goto Lincompatible;
+	    }
+	    else
+		goto Lincompatible;
 	}
-
-	if (i2)
-	{
-	    goto Lt2;
-	}
-	else if (i1)
-	{
-	    goto Lt1;
-	}
-	else
-	    goto Lincompatible;
     }
     else if ((e1->op == TOKstring || e1->op == TOKnull) && e1->implicitConvTo(t2))
     {
--- a/dmd/class.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/class.c	Sat Jul 12 19:38:31 2008 +0200
@@ -147,7 +147,7 @@
 		Type::typeinfotypelist = this;
 	    }
 
-#if V2
+#if DMDV2
 	    if (id == Id::TypeInfo_Const)
 	    {	if (Type::typeinfoconst)
 		    Type::typeinfoconst->error("%s", msg);
@@ -261,7 +261,7 @@
 #endif
 
     if (sc->stc & STCdeprecated)
-    {	//printf("test1: %s is deprecated\n", toChars());
+    {
 	isdeprecated = 1;
     }
 
@@ -302,6 +302,7 @@
 	else
 	{
 	    tc = (TypeClass *)(tb);
+
 	    if (tc->sym->isDeprecated())
 	    {
 		if (!isDeprecated())
@@ -565,7 +566,7 @@
 	scope->setNoFree();
 	scope->module->addDeferredSemantic(this);
 
-	//printf("\tsemantic('%s') failed\n", toChars());
+	//printf("\tsemantic('%s') failed due to forward references\n", toChars());
 	return;
     }
 
@@ -599,7 +600,7 @@
     if (!ctor && baseClass && baseClass->ctor)
     {
 	//printf("Creating default this(){} for class %s\n", toChars());
-	ctor = new CtorDeclaration(0, 0, NULL, 0);
+	ctor = new CtorDeclaration(loc, 0, NULL, 0);
 	ctor->fbody = new CompoundStatement(0, new Statements());
 	members->push(ctor);
 	ctor->addMember(sc, this, 1);
@@ -796,7 +797,7 @@
  * Return 1 if function is hidden (not findable through search).
  */
 
-#if V2
+#if DMDV2
 int isf(void *param, FuncDeclaration *fd)
 {
     //printf("param = %p, fd = %p %s\n", param, fd, fd->toChars());
@@ -857,12 +858,11 @@
 }
 
 void ClassDeclaration::interfaceSemantic(Scope *sc)
-{   int i;
-
+{
     vtblInterfaces = new BaseClasses();
     vtblInterfaces->reserve(interfaces_dim);
 
-    for (i = 0; i < interfaces_dim; i++)
+    for (size_t i = 0; i < interfaces_dim; i++)
     {
 	BaseClass *b = interfaces[i];
 
@@ -911,6 +911,7 @@
     return FALSE;
 }
 
+
 /****************************************
  * Returns !=0 if there's an extra member which is the 'this'
  * pointer to the enclosing context (enclosing class or function)
@@ -936,7 +937,7 @@
 /****************************************
  */
 
-char *ClassDeclaration::kind()
+const char *ClassDeclaration::kind()
 {
     return "class";
 }
@@ -1234,7 +1235,7 @@
 /*******************************************
  */
 
-char *InterfaceDeclaration::kind()
+const char *InterfaceDeclaration::kind()
 {
     return "interface";
 }
--- a/dmd/clone.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/clone.c	Sat Jul 12 19:38:31 2008 +0200
@@ -28,7 +28,7 @@
  * (can be NULL for members that don't need one)
  */
 
-#if V2
+#if DMDV2
 Expression *StructDeclaration::cloneMembers()
 {
     Expression *e = NULL;
@@ -89,7 +89,7 @@
     //printf("StructDeclaration::buildDtor() %s\n", toChars());
     Expression *e = NULL;
 
-#if V2
+#if DMDV2
     for (size_t i = 0; i < fields.dim; i++)
     {
 	Dsymbol *s = (Dsymbol *)fields.data[i];
--- a/dmd/constfold.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/constfold.c	Sat Jul 12 19:38:31 2008 +0200
@@ -1,1506 +1,1512 @@
-
-// 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 <math.h>
-
-#if __DMC__
-#include <complex.h>
-#endif
-
-#include "mem.h"
-#include "root.h"
-
-#include "mtype.h"
-#include "expression.h"
-#include "aggregate.h"
-#include "declaration.h"
-
-#ifdef IN_GCC
-#include "d-gcc-real.h"
-
-/* %% fix? */
-extern "C" bool real_isnan (const real_t *);
-#endif
-
-static real_t zero;	// work around DMC bug for now
-
-#define LOG 0
-
-Expression *expType(Type *type, Expression *e)
-{
-    if (type != e->type)
-    {
-	e = e->copy();
-	e->type = type;
-    }
-    return e;
-}
-
-/* ================================== isConst() ============================== */
-
-int Expression::isConst()
-{
-    //printf("Expression::isConst(): %s\n", toChars());
-    return 0;
-}
-
-int IntegerExp::isConst()
-{
-    return 1;
-}
-
-int RealExp::isConst()
-{
-    return 1;
-}
-
-int ComplexExp::isConst()
-{
-    return 1;
-}
-
-int SymOffExp::isConst()
-{
-    return 2;
-}
-
-/* =============================== constFold() ============================== */
-
-/* The constFold() functions were redundant with the optimize() ones,
- * and so have been folded in with them.
- */
-
-/* ========================================================================== */
-
-Expression *Neg(Type *type, Expression *e1)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    if (e1->type->isreal())
-    {
-	e = new RealExp(loc, -e1->toReal(), type);
-    }
-    else if (e1->type->isimaginary())
-    {
-	e = new RealExp(loc, -e1->toImaginary(), type);
-    }
-    else if (e1->type->iscomplex())
-    {
-	e = new ComplexExp(loc, -e1->toComplex(), type);
-    }
-    else
-	e = new IntegerExp(loc, -e1->toInteger(), type);
-    return e;
-}
-
-Expression *Com(Type *type, Expression *e1)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    e = new IntegerExp(loc, ~e1->toInteger(), type);
-    return e;
-}
-
-Expression *Not(Type *type, Expression *e1)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    e = new IntegerExp(loc, e1->isBool(0), type);
-    return e;
-}
-
-Expression *Bool(Type *type, Expression *e1)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    e = new IntegerExp(loc, e1->isBool(1), type);
-    return e;
-}
-
-Expression *Add(Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-#if LOG
-    printf("Add(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
-#endif
-    if (type->isreal())
-    {
-	e = new RealExp(loc, e1->toReal() + e2->toReal(), type);
-    }
-    else if (type->isimaginary())
-    {
-	e = new RealExp(loc, e1->toImaginary() + e2->toImaginary(), type);
-    }
-    else if (type->iscomplex())
-    {
-	// This rigamarole is necessary so that -0.0 doesn't get
-	// converted to +0.0 by doing an extraneous add with +0.0
-	complex_t c1;
-	real_t r1;
-	real_t i1;
-
-	complex_t c2;
-	real_t r2;
-	real_t i2;
-
-	complex_t v;
-	int x;
-
-	if (e1->type->isreal())
-	{   r1 = e1->toReal();
-	    x = 0;
-	}
-	else if (e1->type->isimaginary())
-	{   i1 = e1->toImaginary();
-	    x = 3;
-	}
-	else
-	{   c1 = e1->toComplex();
-	    x = 6;
-	}
-
-	if (e2->type->isreal())
-	{   r2 = e2->toReal();
-	}
-	else if (e2->type->isimaginary())
-	{   i2 = e2->toImaginary();
-	    x += 1;
-	}
-	else
-	{   c2 = e2->toComplex();
-	    x += 2;
-	}
-
-	switch (x)
-	{
-#if __DMC__
-	    case 0+0:	v = (complex_t) (r1 + r2);	break;
-	    case 0+1:	v = r1 + i2 * I;		break;
-	    case 0+2:	v = r1 + c2;			break;
-	    case 3+0:	v = i1 * I + r2;		break;
-	    case 3+1:	v = (complex_t) ((i1 + i2) * I); break;
-	    case 3+2:	v = i1 * I + c2;		break;
-	    case 6+0:	v = c1 + r2;			break;
-	    case 6+1:	v = c1 + i2 * I;		break;
-	    case 6+2:	v = c1 + c2;			break;
-#else
-	    case 0+0:	v = complex_t(r1 + r2, 0);	break;
-	    case 0+1:	v = complex_t(r1, i2);		break;
-	    case 0+2:	v = complex_t(r1 + creall(c2), cimagl(c2));	break;
-	    case 3+0:	v = complex_t(r2, i1);		break;
-	    case 3+1:	v = complex_t(0, i1 + i2);	break;
-	    case 3+2:	v = complex_t(creall(c2), i1 + cimagl(c2));	break;
-	    case 6+0:	v = complex_t(creall(c1) + r2, cimagl(c2));	break;
-	    case 6+1:	v = complex_t(creall(c1), cimagl(c1) + i2);	break;
-	    case 6+2:	v = c1 + c2;			break;
-#endif
-	    default: assert(0);
-	}
-	e = new ComplexExp(loc, v, type);
-    }
-    else if (e1->op == TOKsymoff)
-    {
-	SymOffExp *soe = (SymOffExp *)e1;
-	e = new SymOffExp(loc, soe->var, soe->offset + e2->toInteger());
-	e->type = type;
-    }
-    else if (e2->op == TOKsymoff)
-    {
-	SymOffExp *soe = (SymOffExp *)e2;
-	e = new SymOffExp(loc, soe->var, soe->offset + e1->toInteger());
-	e->type = type;
-    }
-    else
-	e = new IntegerExp(loc, e1->toInteger() + e2->toInteger(), type);
-    return e;
-}
-
-
-Expression *Min(Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    if (type->isreal())
-    {
-	e = new RealExp(loc, e1->toReal() - e2->toReal(), type);
-    }
-    else if (type->isimaginary())
-    {
-	e = new RealExp(loc, e1->toImaginary() - e2->toImaginary(), type);
-    }
-    else if (type->iscomplex())
-    {
-	// This rigamarole is necessary so that -0.0 doesn't get
-	// converted to +0.0 by doing an extraneous add with +0.0
-	complex_t c1;
-	real_t r1;
-	real_t i1;
-
-	complex_t c2;
-	real_t r2;
-	real_t i2;
-
-	complex_t v;
-	int x;
-
-	if (e1->type->isreal())
-	{   r1 = e1->toReal();
-	    x = 0;
-	}
-	else if (e1->type->isimaginary())
-	{   i1 = e1->toImaginary();
-	    x = 3;
-	}
-	else
-	{   c1 = e1->toComplex();
-	    x = 6;
-	}
-
-	if (e2->type->isreal())
-	{   r2 = e2->toReal();
-	}
-	else if (e2->type->isimaginary())
-	{   i2 = e2->toImaginary();
-	    x += 1;
-	}
-	else
-	{   c2 = e2->toComplex();
-	    x += 2;
-	}
-
-	switch (x)
-	{
-#if __DMC__
-	    case 0+0:	v = (complex_t) (r1 - r2);	break;
-	    case 0+1:	v = r1 - i2 * I;		break;
-	    case 0+2:	v = r1 - c2;			break;
-	    case 3+0:	v = i1 * I - r2;		break;
-	    case 3+1:	v = (complex_t) ((i1 - i2) * I); break;
-	    case 3+2:	v = i1 * I - c2;		break;
-	    case 6+0:	v = c1 - r2;			break;
-	    case 6+1:	v = c1 - i2 * I;		break;
-	    case 6+2:	v = c1 - c2;			break;
-#else
-	    case 0+0:	v = complex_t(r1 - r2, 0);	break;
-	    case 0+1:	v = complex_t(r1, -i2);		break;
-	    case 0+2:	v = complex_t(r1 - creall(c2), -cimagl(c2));	break;
-	    case 3+0:	v = complex_t(-r2, i1);		break;
-	    case 3+1:	v = complex_t(0, i1 - i2);	break;
-	    case 3+2:	v = complex_t(-creall(c2), i1 - cimagl(c2));	break;
-	    case 6+0:	v = complex_t(creall(c1) - r2, cimagl(c1));	break;
-	    case 6+1:	v = complex_t(creall(c1), cimagl(c1) - i2);	break;
-	    case 6+2:	v = c1 - c2;			break;
-#endif
-	    default: assert(0);
-	}
-	e = new ComplexExp(loc, v, type);
-    }
-    else if (e1->op == TOKsymoff)
-    {
-	SymOffExp *soe = (SymOffExp *)e1;
-	e = new SymOffExp(loc, soe->var, soe->offset - e2->toInteger());
-	e->type = type;
-    }
-    else
-    {
-	e = new IntegerExp(loc, e1->toInteger() - e2->toInteger(), type);
-    }
-    return e;
-}
-
-Expression *Mul(Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    if (type->isfloating())
-    {   complex_t c;
-#ifdef IN_GCC
-	real_t r;
-#else
-	d_float80 r;
-#endif
-
-	if (e1->type->isreal())
-	{
-#if __DMC__
-	    c = e1->toReal() * e2->toComplex();
-#else
-	    r = e1->toReal();
-	    c = e2->toComplex();
-	    c = complex_t(r * creall(c), r * cimagl(c));
-#endif
-	}
-	else if (e1->type->isimaginary())
-	{
-#if __DMC__
-	    c = e1->toImaginary() * I * e2->toComplex();
-#else
-	    r = e1->toImaginary();
-	    c = e2->toComplex();
-	    c = complex_t(-r * cimagl(c), r * creall(c));
-#endif
-	}
-	else if (e2->type->isreal())
-	{
-#if __DMC__
-	    c = e2->toReal() * e1->toComplex();
-#else
-	    r = e2->toReal();
-	    c = e1->toComplex();
-	    c = complex_t(r * creall(c), r * cimagl(c));
-#endif
-	}
-	else if (e2->type->isimaginary())
-	{
-#if __DMC__
-	    c = e1->toComplex() * e2->toImaginary() * I;
-#else
-	    r = e2->toImaginary();
-	    c = e1->toComplex();
-	    c = complex_t(-r * cimagl(c), r * creall(c));
-#endif
-	}
-	else
-	    c = e1->toComplex() * e2->toComplex();
-
-	if (type->isreal())
-	    e = new RealExp(loc, creall(c), type);
-	else if (type->isimaginary())
-	    e = new RealExp(loc, cimagl(c), type);
-	else if (type->iscomplex())
-	    e = new ComplexExp(loc, c, type);
-	else
-	    assert(0);
-    }
-    else
-    {
-	e = new IntegerExp(loc, e1->toInteger() * e2->toInteger(), type);
-    }
-    return e;
-}
-
-Expression *Div(Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    if (type->isfloating())
-    {   complex_t c;
-#ifdef IN_GCC
-	real_t r;
-#else
-	d_float80 r;
-#endif
-
-	//e1->type->print();
-	//e2->type->print();
-	if (e2->type->isreal())
-	{
-	    if (e1->type->isreal())
-	    {
-		e = new RealExp(loc, e1->toReal() / e2->toReal(), type);
-		return e;
-	    }
-#if __DMC__
-	    //r = e2->toReal();
-	    //c = e1->toComplex();
-	    //printf("(%Lg + %Lgi) / %Lg\n", creall(c), cimagl(c), r);
-
-	    c = e1->toComplex() / e2->toReal();
-#else
-	    r = e2->toReal();
-	    c = e1->toComplex();
-	    c = complex_t(creall(c) / r, cimagl(c) / r);
-#endif
-	}
-	else if (e2->type->isimaginary())
-	{
-#if __DMC__
-	    //r = e2->toImaginary();
-	    //c = e1->toComplex();
-	    //printf("(%Lg + %Lgi) / %Lgi\n", creall(c), cimagl(c), r);
-
-	    c = e1->toComplex() / (e2->toImaginary() * I);
-#else
-	    r = e2->toImaginary();
-	    c = e1->toComplex();
-	    c = complex_t(cimagl(c) / r, -creall(c) / r);
-#endif
-	}
-	else
-	{
-	    c = e1->toComplex() / e2->toComplex();
-	}
-
-	if (type->isreal())
-	    e = new RealExp(loc, creall(c), type);
-	else if (type->isimaginary())
-	    e = new RealExp(loc, cimagl(c), type);
-	else if (type->iscomplex())
-	    e = new ComplexExp(loc, c, type);
-	else
-	    assert(0);
-    }
-    else
-    {   sinteger_t n1;
-	sinteger_t n2;
-	sinteger_t n;
-
-	n1 = e1->toInteger();
-	n2 = e2->toInteger();
-	if (n2 == 0)
-	{   e2->error("divide by 0");
-	    e2 = new IntegerExp(0, 1, e2->type);
-	    n2 = 1;
-	}
-	if (e1->type->isunsigned() || e2->type->isunsigned())
-	    n = ((d_uns64) n1) / ((d_uns64) n2);
-	else
-	    n = n1 / n2;
-	e = new IntegerExp(loc, n, type);
-    }
-    return e;
-}
-
-Expression *Mod(Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    if (type->isfloating())
-    {
-	complex_t c;
-
-	if (e2->type->isreal())
-	{   real_t r2 = e2->toReal();
-
-#ifdef __DMC__
-	    c = fmodl(e1->toReal(), r2) + fmodl(e1->toImaginary(), r2) * I;
-#elif defined(IN_GCC)
-	    c = complex_t(e1->toReal() % r2, e1->toImaginary() % r2);
-#else
-	    c = complex_t(fmodl(e1->toReal(), r2), fmodl(e1->toImaginary(), r2));
-#endif
-	}
-	else if (e2->type->isimaginary())
-	{   real_t i2 = e2->toImaginary();
-
-#ifdef __DMC__
-	    c = fmodl(e1->toReal(), i2) + fmodl(e1->toImaginary(), i2) * I;
-#elif defined(IN_GCC)
-	    c = complex_t(e1->toReal() % i2, e1->toImaginary() % i2);
-#else
-	    c = complex_t(fmodl(e1->toReal(), i2), fmodl(e1->toImaginary(), i2));
-#endif
-	}
-	else
-	    assert(0);
-
-	if (type->isreal())
-	    e = new RealExp(loc, creall(c), type);
-	else if (type->isimaginary())
-	    e = new RealExp(loc, cimagl(c), type);
-	else if (type->iscomplex())
-	    e = new ComplexExp(loc, c, type);
-	else
-	    assert(0);
-    }
-    else
-    {   sinteger_t n1;
-	sinteger_t n2;
-	sinteger_t n;
-
-	n1 = e1->toInteger();
-	n2 = e2->toInteger();
-	if (n2 == 0)
-	{   e2->error("divide by 0");
-	    e2 = new IntegerExp(0, 1, e2->type);
-	    n2 = 1;
-	}
-	if (e1->type->isunsigned() || e2->type->isunsigned())
-	    n = ((d_uns64) n1) % ((d_uns64) n2);
-	else
-	    n = n1 % n2;
-	e = new IntegerExp(loc, n, type);
-    }
-    return e;
-}
-
-Expression *Shl(Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    e = new IntegerExp(loc, e1->toInteger() << e2->toInteger(), type);
-    return e;
-}
-
-Expression *Shr(Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-    unsigned count;
-    integer_t value;
-
-    value = e1->toInteger();
-    count = e2->toInteger();
-    switch (e1->type->toBasetype()->ty)
-    {
-	case Tint8:
-		value = (d_int8)(value) >> count;
-		break;
-
-	case Tuns8:
-		value = (d_uns8)(value) >> count;
-		break;
-
-	case Tint16:
-		value = (d_int16)(value) >> count;
-		break;
-
-	case Tuns16:
-		value = (d_uns16)(value) >> count;
-		break;
-
-	case Tint32:
-		value = (d_int32)(value) >> count;
-		break;
-
-	case Tuns32:
-		value = (d_uns32)(value) >> count;
-		break;
-
-	case Tint64:
-		value = (d_int64)(value) >> count;
-		break;
-
-	case Tuns64:
-		value = (d_uns64)(value) >> count;
-		break;
-
-	default:
-		assert(0);
-    }
-    e = new IntegerExp(loc, value, type);
-    return e;
-}
-
-Expression *Ushr(Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-    unsigned count;
-    integer_t value;
-
-    value = e1->toInteger();
-    count = e2->toInteger();
-    switch (e1->type->toBasetype()->ty)
-    {
-	case Tint8:
-	case Tuns8:
-		assert(0);		// no way to trigger this
-		value = (value & 0xFF) >> count;
-		break;
-
-	case Tint16:
-	case Tuns16:
-		assert(0);		// no way to trigger this
-		value = (value & 0xFFFF) >> count;
-		break;
-
-	case Tint32:
-	case Tuns32:
-		value = (value & 0xFFFFFFFF) >> count;
-		break;
-
-	case Tint64:
-	case Tuns64:
-		value = (d_uns64)(value) >> count;
-		break;
-
-	default:
-		assert(0);
-    }
-    e = new IntegerExp(loc, value, type);
-    return e;
-}
-
-Expression *And(Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    e = new IntegerExp(loc, e1->toInteger() & e2->toInteger(), type);
-    return e;
-}
-
-Expression *Or(Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    e = new IntegerExp(loc, e1->toInteger() | e2->toInteger(), type);
-    return e;
-}
-
-Expression *Xor(Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    e = new IntegerExp(loc, e1->toInteger() ^ e2->toInteger(), type);
-    return e;
-}
-
-/* Also returns EXP_CANT_INTERPRET if cannot be computed.
- */
-Expression *Equal(enum TOK op, Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-    int cmp;
-    real_t r1;
-    real_t r2;
-
-    //printf("Equal(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
-
-    assert(op == TOKequal || op == TOKnotequal);
-
-    if (e1->op == TOKnull)
-    {
-	if (e2->op == TOKnull)
-	    cmp = 1;
-	else if (e2->op == TOKstring)
-	{   StringExp *es2 = (StringExp *)e2;
-	    cmp = (0 == es2->len);
-	}
-	else if (e2->op == TOKarrayliteral)
-	{   ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
-	    cmp = !es2->elements || (0 == es2->elements->dim);
-	}
-	else
-	    return EXP_CANT_INTERPRET;
-    }
-    else if (e2->op == TOKnull)
-    {
-	if (e1->op == TOKstring)
-	{   StringExp *es1 = (StringExp *)e1;
-	    cmp = (0 == es1->len);
-	}
-	else if (e1->op == TOKarrayliteral)
-	{   ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
-	    cmp = !es1->elements || (0 == es1->elements->dim);
-	}
-	else
-	    return EXP_CANT_INTERPRET;
-    }
-    else if (e1->op == TOKstring && e2->op == TOKstring)
-    {	StringExp *es1 = (StringExp *)e1;
-	StringExp *es2 = (StringExp *)e2;
-
-	assert(es1->sz == es2->sz);
-	if (es1->len == es2->len &&
-	    memcmp(es1->string, es2->string, es1->sz * es1->len) == 0)
-	    cmp = 1;
-	else
-	    cmp = 0;
-    }
-    else if (e1->op == TOKarrayliteral && e2->op == TOKarrayliteral)
-    {   ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
-	ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
-
-	if ((!es1->elements || !es1->elements->dim) &&
-	    (!es2->elements || !es2->elements->dim))
-	    cmp = 1;		// both arrays are empty
-	else if (!es1->elements || !es2->elements)
-	    cmp = 0;
-	else if (es1->elements->dim != es2->elements->dim)
-	    cmp = 0;
-	else
-	{
-	    for (size_t i = 0; i < es1->elements->dim; i++)
-	    {   Expression *ee1 = (Expression *)es1->elements->data[i];
-		Expression *ee2 = (Expression *)es2->elements->data[i];
-
-		Expression *v = Equal(TOKequal, Type::tint32, ee1, ee2);
-		if (v == EXP_CANT_INTERPRET)
-		    return EXP_CANT_INTERPRET;
-		cmp = v->toInteger();
-		if (cmp == 0)
-		    break;
-	    }
-	}
-    }
-    else if (e1->op == TOKarrayliteral && e2->op == TOKstring)
-    {	// Swap operands and use common code
-	Expression *e = e1;
-	e1 = e2;
-	e2 = e;
-	goto Lsa;
-    }
-    else if (e1->op == TOKstring && e2->op == TOKarrayliteral)
-    {
-     Lsa:
-	StringExp *es1 = (StringExp *)e1;
-	ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
-	size_t dim1 = es1->len;
-	size_t dim2 = es2->elements ? es2->elements->dim : 0;
-	if (dim1 != dim2)
-	    cmp = 0;
-	else
-	{
-	    for (size_t i = 0; i < dim1; i++)
-	    {
-		uinteger_t c = es1->charAt(i);
-		Expression *ee2 = (Expression *)es2->elements->data[i];
-		if (ee2->isConst() != 1)
-		    return EXP_CANT_INTERPRET;
-		cmp = (c == ee2->toInteger());
-		if (cmp == 0)
-		    break;
-	    }
-	}
-    }
-    else if (e1->op == TOKstructliteral && e2->op == TOKstructliteral)
-    {   StructLiteralExp *es1 = (StructLiteralExp *)e1;
-	StructLiteralExp *es2 = (StructLiteralExp *)e2;
-
-	if (es1->sd != es2->sd)
-	    cmp = 0;
-	else if ((!es1->elements || !es1->elements->dim) &&
-	    (!es2->elements || !es2->elements->dim))
-	    cmp = 1;		// both arrays are empty
-	else if (!es1->elements || !es2->elements)
-	    cmp = 0;
-	else if (es1->elements->dim != es2->elements->dim)
-	    cmp = 0;
-	else
-	{
-	    cmp = 1;
-	    for (size_t i = 0; i < es1->elements->dim; i++)
-	    {   Expression *ee1 = (Expression *)es1->elements->data[i];
-		Expression *ee2 = (Expression *)es2->elements->data[i];
-
-		if (ee1 == ee2)
-		    continue;
-		if (!ee1 || !ee2)
-		{   cmp = 0;
-		    break;
-		}
-		Expression *v = Equal(TOKequal, Type::tint32, ee1, ee2);
-		if (v == EXP_CANT_INTERPRET)
-		    return EXP_CANT_INTERPRET;
-		cmp = v->toInteger();
-		if (cmp == 0)
-		    break;
-	    }
-	}
-    }
-#if 0 // Should handle this
-    else if (e1->op == TOKarrayliteral && e2->op == TOKstring)
-    {
-    }
-#endif
-    else if (e1->isConst() != 1 || e2->isConst() != 1)
-	return EXP_CANT_INTERPRET;
-    else if (e1->type->isreal())
-    {
-	r1 = e1->toReal();
-	r2 = e2->toReal();
-	goto L1;
-    }
-    else if (e1->type->isimaginary())
-    {
-	r1 = e1->toImaginary();
-	r2 = e2->toImaginary();
-     L1:
-#if __DMC__
-	cmp = (r1 == r2);
-#else
-	if (isnan(r1) || isnan(r2))	// if unordered
-	{
-	    cmp = 0;
-	}
-	else
-	{
-	    cmp = (r1 == r2);
-	}
-#endif
-    }
-    else if (e1->type->iscomplex())
-    {
-	cmp = e1->toComplex() == e2->toComplex();
-    }
-    else if (e1->type->isintegral())
-    {
-	cmp = (e1->toInteger() == e2->toInteger());
-    }
-    else
-	return EXP_CANT_INTERPRET;
-    if (op == TOKnotequal)
-	cmp ^= 1;
-    e = new IntegerExp(loc, cmp, type);
-    return e;
-}
-
-Expression *Identity(enum TOK op, Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-    int cmp;
-
-    if (e1->op == TOKnull && e2->op == TOKnull)
-    {
-	cmp = 1;
-    }
-    else if (e1->op == TOKsymoff && e2->op == TOKsymoff)
-    {
-	SymOffExp *es1 = (SymOffExp *)e1;
-	SymOffExp *es2 = (SymOffExp *)e2;
-
-	cmp = (es1->var == es2->var && es1->offset == es2->offset);
-    }
-    else if (e1->isConst() == 1 && e2->isConst() == 1)
-	return Equal((op == TOKidentity) ? TOKequal : TOKnotequal,
-		type, e1, e2);
-    else
-	assert(0);
-    if (op == TOKnotidentity)
-	cmp ^= 1;
-    return new IntegerExp(loc, cmp, type);
-}
-
-
-Expression *Cmp(enum TOK op, Type *type, Expression *e1, Expression *e2)
-{   Expression *e;
-    Loc loc = e1->loc;
-    integer_t n;
-    real_t r1;
-    real_t r2;
-
-    if (e1->type->isreal())
-    {
-	r1 = e1->toReal();
-	r2 = e2->toReal();
-	goto L1;
-    }
-    else if (e1->type->isimaginary())
-    {
-	r1 = e1->toImaginary();
-	r2 = e2->toImaginary();
-     L1:
-#if __DMC__
-	// DMC is the only compiler I know of that handles NAN arguments
-	// correctly in comparisons.
-	switch (op)
-	{
-	    case TOKlt:	   n = r1 <  r2;	break;
-	    case TOKle:	   n = r1 <= r2;	break;
-	    case TOKgt:	   n = r1 >  r2;	break;
-	    case TOKge:	   n = r1 >= r2;	break;
-
-	    case TOKleg:   n = r1 <>=  r2;	break;
-	    case TOKlg:	   n = r1 <>   r2;	break;
-	    case TOKunord: n = r1 !<>= r2;	break;
-	    case TOKue:	   n = r1 !<>  r2;	break;
-	    case TOKug:	   n = r1 !<=  r2;	break;
-	    case TOKuge:   n = r1 !<   r2;	break;
-	    case TOKul:	   n = r1 !>=  r2;	break;
-	    case TOKule:   n = r1 !>   r2;	break;
-
-	    default:
-		assert(0);
-	}
-#else
-	// Don't rely on compiler, handle NAN arguments separately
-#if IN_GCC
-	if (real_isnan(&r1) || real_isnan(&r2))	// if unordered
-#else
-	if (isnan(r1) || isnan(r2))	// if unordered
-#endif
-	{
-	    switch (op)
-	    {
-		case TOKlt:	n = 0;	break;
-		case TOKle:	n = 0;	break;
-		case TOKgt:	n = 0;	break;
-		case TOKge:	n = 0;	break;
-
-		case TOKleg:	n = 0;	break;
-		case TOKlg:	n = 0;	break;
-		case TOKunord:	n = 1;	break;
-		case TOKue:	n = 1;	break;
-		case TOKug:	n = 1;	break;
-		case TOKuge:	n = 1;	break;
-		case TOKul:	n = 1;	break;
-		case TOKule:	n = 1;	break;
-
-		default:
-		    assert(0);
-	    }
-	}
-	else
-	{
-	    switch (op)
-	    {
-		case TOKlt:	n = r1 <  r2;	break;
-		case TOKle:	n = r1 <= r2;	break;
-		case TOKgt:	n = r1 >  r2;	break;
-		case TOKge:	n = r1 >= r2;	break;
-
-		case TOKleg:	n = 1;		break;
-		case TOKlg:	n = r1 != r2;	break;
-		case TOKunord:	n = 0;		break;
-		case TOKue:	n = r1 == r2;	break;
-		case TOKug:	n = r1 >  r2;	break;
-		case TOKuge:	n = r1 >= r2;	break;
-		case TOKul:	n = r1 <  r2;	break;
-		case TOKule:	n = r1 <= r2;	break;
-
-		default:
-		    assert(0);
-	    }
-	}
-#endif
-    }
-    else if (e1->type->iscomplex())
-    {
-	assert(0);
-    }
-    else
-    {   sinteger_t n1;
-	sinteger_t n2;
-
-	n1 = e1->toInteger();
-	n2 = e2->toInteger();
-	if (e1->type->isunsigned() || e2->type->isunsigned())
-	{
-	    switch (op)
-	    {
-		case TOKlt:	n = ((d_uns64) n1) <  ((d_uns64) n2);	break;
-		case TOKle:	n = ((d_uns64) n1) <= ((d_uns64) n2);	break;
-		case TOKgt:	n = ((d_uns64) n1) >  ((d_uns64) n2);	break;
-		case TOKge:	n = ((d_uns64) n1) >= ((d_uns64) n2);	break;
-
-		case TOKleg:	n = 1;					break;
-		case TOKlg:	n = ((d_uns64) n1) != ((d_uns64) n2);	break;
-		case TOKunord:	n = 0;					break;
-		case TOKue:	n = ((d_uns64) n1) == ((d_uns64) n2);	break;
-		case TOKug:	n = ((d_uns64) n1) >  ((d_uns64) n2);	break;
-		case TOKuge:	n = ((d_uns64) n1) >= ((d_uns64) n2);	break;
-		case TOKul:	n = ((d_uns64) n1) <  ((d_uns64) n2);	break;
-		case TOKule:	n = ((d_uns64) n1) <= ((d_uns64) n2);	break;
-
-		default:
-		    assert(0);
-	    }
-	}
-	else
-	{
-	    switch (op)
-	    {
-		case TOKlt:	n = n1 <  n2;	break;
-		case TOKle:	n = n1 <= n2;	break;
-		case TOKgt:	n = n1 >  n2;	break;
-		case TOKge:	n = n1 >= n2;	break;
-
-		case TOKleg:	n = 1;		break;
-		case TOKlg:	n = n1 != n2;	break;
-		case TOKunord:	n = 0;		break;
-		case TOKue:	n = n1 == n2;	break;
-		case TOKug:	n = n1 >  n2;	break;
-		case TOKuge:	n = n1 >= n2;	break;
-		case TOKul:	n = n1 <  n2;	break;
-		case TOKule:	n = n1 <= n2;	break;
-
-		default:
-		    assert(0);
-	    }
-	}
-    }
-    e = new IntegerExp(loc, n, type);
-    return e;
-}
-
-/* Also returns EXP_CANT_INTERPRET if cannot be computed.
- *  to:	type to cast to
- *  type: type to paint the result
- */
-
-Expression *Cast(Type *type, Type *to, Expression *e1)
-{   Expression *e = EXP_CANT_INTERPRET;
-    Loc loc = e1->loc;
-
-    //printf("Cast(type = %s, to = %s, e1 = %s)\n", type->toChars(), to->toChars(), e1->toChars());
-    //printf("e1->type = %s\n", e1->type->toChars());
-    if (type->equals(e1->type) && to->equals(type))
-	return e1;
-
-    if (e1->isConst() != 1)
-	return EXP_CANT_INTERPRET;
-
-    Type *tb = to->toBasetype();
-    if (tb->ty == Tbool)
-	e = new IntegerExp(loc, e1->toInteger() != 0, type);
-    else if (type->isintegral())
-    {
-	if (e1->type->isfloating())
-	{   integer_t result;
-	    real_t r = e1->toReal();
-
-	    switch (type->toBasetype()->ty)
-	    {
-		case Tint8:	result = (d_int8)r;	break;
-		case Tchar:
-		case Tuns8:	result = (d_uns8)r;	break;
-		case Tint16:	result = (d_int16)r;	break;
-		case Twchar:
-		case Tuns16:	result = (d_uns16)r;	break;
-		case Tint32:	result = (d_int32)r;	break;
-		case Tdchar:
-		case Tuns32:	result = (d_uns32)r;	break;
-		case Tint64:	result = (d_int64)r;	break;
-		case Tuns64:	result = (d_uns64)r;	break;
-		default:
-		    assert(0);
-	    }
-
-	    e = new IntegerExp(loc, result, type);
-	}
-	else if (type->isunsigned())
-	    e = new IntegerExp(loc, e1->toUInteger(), type);
-	else
-	    e = new IntegerExp(loc, e1->toInteger(), type);
-    }
-    else if (tb->isreal())
-    {   real_t value = e1->toReal();
-
-	e = new RealExp(loc, value, type);
-    }
-    else if (tb->isimaginary())
-    {   real_t value = e1->toImaginary();
-
-	e = new RealExp(loc, value, type);
-    }
-    else if (tb->iscomplex())
-    {   complex_t value = e1->toComplex();
-
-	e = new ComplexExp(loc, value, type);
-    }
-    else if (tb->isscalar())
-	e = new IntegerExp(loc, e1->toInteger(), type);
-    else if (tb->ty == Tvoid)
-	e = EXP_CANT_INTERPRET;
-    else if (tb->ty == Tstruct && e1->op == TOKint64)
-    {	// Struct = 0;
-	StructDeclaration *sd = tb->toDsymbol(NULL)->isStructDeclaration();
-	assert(sd);
-	Expressions *elements = new Expressions;
-	for (size_t i = 0; i < sd->fields.dim; i++)
-	{   Dsymbol *s = (Dsymbol *)sd->fields.data[i];
-	    VarDeclaration *v = s->isVarDeclaration();
-	    assert(v);
-
-	    Expression *exp = new IntegerExp(0);
-	    exp = Cast(v->type, v->type, exp);
-	    if (exp == EXP_CANT_INTERPRET)
-		return exp;
-	    elements->push(exp);
-	}
-	e = new StructLiteralExp(loc, sd, elements);
-	e->type = type;
-    }
-    else
-    {
-	error(loc, "cannot cast %s to %s", e1->type->toChars(), type->toChars());
-	e = new IntegerExp(loc, 0, type);
-    }
-    return e;
-}
-
-
-Expression *ArrayLength(Type *type, Expression *e1)
-{   Expression *e;
-    Loc loc = e1->loc;
-
-    if (e1->op == TOKstring)
-    {	StringExp *es1 = (StringExp *)e1;
-
-	e = new IntegerExp(loc, es1->len, type);
-    }
-    else if (e1->op == TOKarrayliteral)
-    {	ArrayLiteralExp *ale = (ArrayLiteralExp *)e1;
-	size_t dim;
-
-	dim = ale->elements ? ale->elements->dim : 0;
-	e = new IntegerExp(loc, dim, type);
-    }
-    else if (e1->op == TOKassocarrayliteral)
-    {	AssocArrayLiteralExp *ale = (AssocArrayLiteralExp *)e1;
-	size_t dim = ale->keys->dim;
-
-	e = new IntegerExp(loc, dim, type);
-    }
-    else
-	e = EXP_CANT_INTERPRET;
-    return e;
-}
-
-/* Also return EXP_CANT_INTERPRET if this fails
- */
-Expression *Index(Type *type, Expression *e1, Expression *e2)
-{   Expression *e = EXP_CANT_INTERPRET;
-    Loc loc = e1->loc;
-
-    //printf("Index(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
-    assert(e1->type);
-    if (e1->op == TOKstring && e2->op == TOKint64)
-    {	StringExp *es1 = (StringExp *)e1;
-	uinteger_t i = e2->toInteger();
-
-	if (i >= es1->len)
-        e1->error("string index %llu is out of bounds [0 .. %"PRIuSIZE"]", i, es1->len);
-	else
-	{   unsigned value = es1->charAt(i);
-	    e = new IntegerExp(loc, value, type);
-	}
-    }
-    else if (e1->type->toBasetype()->ty == Tsarray && e2->op == TOKint64)
-    {	TypeSArray *tsa = (TypeSArray *)e1->type->toBasetype();
-	uinteger_t length = tsa->dim->toInteger();
-	uinteger_t i = e2->toInteger();
-
-	if (i >= length)
-	{
-        e2->error("array index %llu is out of bounds %s[0 .. %llu]", i, e1->toChars(), length);
-	}
-	else if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2))
-	{   ArrayLiteralExp *ale = (ArrayLiteralExp *)e1;
-	    e = (Expression *)ale->elements->data[i];
-	    e->type = type;
-	}
-    }
-    else if (e1->type->toBasetype()->ty == Tarray && e2->op == TOKint64)
-    {
-	uinteger_t i = e2->toInteger();
-
-	if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2))
-	{   ArrayLiteralExp *ale = (ArrayLiteralExp *)e1;
-	    if (i >= ale->elements->dim)
-	    {
-            e2->error("array index %llu is out of bounds %s[0 .. %u]", i, e1->toChars(), ale->elements->dim);
-	    }
-	    else
-	    {	e = (Expression *)ale->elements->data[i];
-		e->type = type;
-	    }
-	}
-    }
-    else if (e1->op == TOKassocarrayliteral && !e1->checkSideEffect(2))
-    {
-	AssocArrayLiteralExp *ae = (AssocArrayLiteralExp *)e1;
-	/* Search the keys backwards, in case there are duplicate keys
-	 */
-	for (size_t i = ae->keys->dim; i;)
-	{
-	    i--;
-	    Expression *ekey = (Expression *)ae->keys->data[i];
-	    Expression *ex = Equal(TOKequal, Type::tbool, ekey, e2);
-	    if (ex == EXP_CANT_INTERPRET)
-		return ex;
-	    if (ex->isBool(TRUE))
-	    {	e = (Expression *)ae->values->data[i];
-		e->type = type;
-		break;
-	    }
-	}
-    }
-    return e;
-}
-
-/* Also return EXP_CANT_INTERPRET if this fails
- */
-Expression *Slice(Type *type, Expression *e1, Expression *lwr, Expression *upr)
-{   Expression *e = EXP_CANT_INTERPRET;
-    Loc loc = e1->loc;
-
-#if LOG
-    printf("Slice()\n");
-    if (lwr)
-    {	printf("\te1 = %s\n", e1->toChars());
-	printf("\tlwr = %s\n", lwr->toChars());
-	printf("\tupr = %s\n", upr->toChars());
-    }
-#endif
-    if (e1->op == TOKstring && lwr->op == TOKint64 && upr->op == TOKint64)
-    {	StringExp *es1 = (StringExp *)e1;
-	uinteger_t ilwr = lwr->toInteger();
-	uinteger_t iupr = upr->toInteger();
-
-	if (iupr > es1->len || ilwr > iupr)
-        e1->error("string slice [%llu .. %llu] is out of bounds", ilwr, iupr);
-	else
-	{   integer_t value;
-	    void *s;
-	    size_t len = iupr - ilwr;
-	    int sz = es1->sz;
-	    StringExp *es;
-
-	    s = mem.malloc((len + 1) * sz);
-	    memcpy((unsigned char *)s, (unsigned char *)es1->string + ilwr * sz, len * sz);
-	    memset((unsigned char *)s + len * sz, 0, sz);
-
-	    es = new StringExp(loc, s, len, es1->postfix);
-	    es->sz = sz;
-	    es->committed = 1;
-	    es->type = type;
-	    e = es;
-	}
-    }
-    else if (e1->op == TOKarrayliteral &&
-	    lwr->op == TOKint64 && upr->op == TOKint64 &&
-	    !e1->checkSideEffect(2))
-    {	ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
-	uinteger_t ilwr = lwr->toInteger();
-	uinteger_t iupr = upr->toInteger();
-
-	if (iupr > es1->elements->dim || ilwr > iupr)
-        e1->error("array slice [%llu .. %llu] is out of bounds", ilwr, iupr);
-	else
-	{
-	    Expressions *elements = new Expressions();
-	    elements->setDim(iupr - ilwr);
-	    memcpy(elements->data,
-		   es1->elements->data + ilwr,
-		   (iupr - ilwr) * sizeof(es1->elements->data[0]));
-	    e = new ArrayLiteralExp(e1->loc, elements);
-	    e->type = type;
-	}
-    }
-    return e;
-}
-
-/* Also return EXP_CANT_INTERPRET if this fails
- */
-Expression *Cat(Type *type, Expression *e1, Expression *e2)
-{   Expression *e = EXP_CANT_INTERPRET;
-    Loc loc = e1->loc;
-    Type *t;
-
-    //printf("Cat(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
-
-    if (e1->op == TOKnull && (e2->op == TOKint64 || e2->op == TOKstructliteral))
-    {	e = e2;
-	goto L2;
-    }
-    else if ((e1->op == TOKint64 || e1->op == TOKstructliteral) && e2->op == TOKnull)
-    {	e = e1;
-     L2:
-	Type *tn = e->type->toBasetype();
-	if (tn->ty == Tchar || tn->ty == Twchar || tn->ty == Tdchar)
-	{
-	    // Create a StringExp
-	    void *s;
-	    StringExp *es;
-	    size_t len = 1;
-	    int sz = tn->size();
-	    integer_t v = e->toInteger();
-
-	    s = mem.malloc((len + 1) * sz);
-	    memcpy((unsigned char *)s, &v, sz);
-
-	    // Add terminating 0
-	    memset((unsigned char *)s + len * sz, 0, sz);
-
-	    es = new StringExp(loc, s, len);
-	    es->sz = sz;
-	    es->committed = 1;
-	    e = es;
-	}
-	else
-	{   // Create an ArrayLiteralExp
-	    Expressions *elements = new Expressions();
-	    elements->push(e);
-	    e = new ArrayLiteralExp(e->loc, elements);
-	}
-	e->type = type;
-	return e;
-    }
-    else if (e1->op == TOKstring && e2->op == TOKstring)
-    {
-	// Concatenate the strings
-	void *s;
-	StringExp *es1 = (StringExp *)e1;
-	StringExp *es2 = (StringExp *)e2;
-	StringExp *es;
-	Type *t;
-	size_t len = es1->len + es2->len;
-	int sz = es1->sz;
-
-	assert(sz == es2->sz);
-	s = mem.malloc((len + 1) * sz);
-	memcpy(s, es1->string, es1->len * sz);
-	memcpy((unsigned char *)s + es1->len * sz, es2->string, es2->len * sz);
-
-	// Add terminating 0
-	memset((unsigned char *)s + len * sz, 0, sz);
-
-	es = new StringExp(loc, s, len);
-	es->sz = sz;
-	es->committed = es1->committed | es2->committed;
-	if (es1->committed)
-	    t = es1->type;
-	else
-	    t = es2->type;
-	es->type = type;
-	e = es;
-    }
-    else if (e1->op == TOKstring && e2->op == TOKint64)
-    {
-	// Concatenate the strings
-	void *s;
-	StringExp *es1 = (StringExp *)e1;
-	StringExp *es;
-	Type *t;
-	size_t len = es1->len + 1;
-	int sz = es1->sz;
-	integer_t v = e2->toInteger();
-
-	s = mem.malloc((len + 1) * sz);
-	memcpy(s, es1->string, es1->len * sz);
-	memcpy((unsigned char *)s + es1->len * sz, &v, sz);
-
-	// Add terminating 0
-	memset((unsigned char *)s + len * sz, 0, sz);
-
-	es = new StringExp(loc, s, len);
-	es->sz = sz;
-	es->committed = es1->committed;
-	t = es1->type;
-	es->type = type;
-	e = es;
-    }
-    else if (e1->op == TOKint64 && e2->op == TOKstring)
-    {
-	// Concatenate the strings
-	void *s;
-	StringExp *es2 = (StringExp *)e2;
-	StringExp *es;
-	Type *t;
-	size_t len = 1 + es2->len;
-	int sz = es2->sz;
-	integer_t v = e1->toInteger();
-
-	s = mem.malloc((len + 1) * sz);
-	memcpy((unsigned char *)s, &v, sz);
-	memcpy((unsigned char *)s + sz, es2->string, es2->len * sz);
-
-	// Add terminating 0
-	memset((unsigned char *)s + len * sz, 0, sz);
-
-	es = new StringExp(loc, s, len);
-	es->sz = sz;
-	es->committed = es2->committed;
-	t = es2->type;
-	es->type = type;
-	e = es;
-    }
-    else if (e1->op == TOKarrayliteral && e2->op == TOKarrayliteral &&
-	e1->type->equals(e2->type))
-    {
-	// Concatenate the arrays
-	ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
-	ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
-
-	es1 = new ArrayLiteralExp(es1->loc, (Expressions *)es1->elements->copy());
-	es1->elements->insert(es1->elements->dim, es2->elements);
-	e = es1;
-
-	if (type->toBasetype()->ty == Tsarray)
-	{
-	    e->type = new TypeSArray(e1->type->toBasetype()->next, new IntegerExp(0, es1->elements->dim, Type::tindex));
-	    e->type = e->type->semantic(loc, NULL);
-	}
-	else
-	    e->type = type;
-    }
-    else if (e1->op == TOKarrayliteral &&
-	e1->type->toBasetype()->nextOf()->equals(e2->type))
-    {
-	ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
-
-	es1 = new ArrayLiteralExp(es1->loc, (Expressions *)es1->elements->copy());
-	es1->elements->push(e2);
-	e = es1;
-
-	if (type->toBasetype()->ty == Tsarray)
-	{
-	    e->type = new TypeSArray(e2->type, new IntegerExp(0, es1->elements->dim, Type::tindex));
-	    e->type = e->type->semantic(loc, NULL);
-	}
-	else
-	    e->type = type;
-    }
-    else if (e2->op == TOKarrayliteral &&
-	e2->type->toBasetype()->nextOf()->equals(e1->type))
-    {
-	ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
-
-	es2 = new ArrayLiteralExp(es2->loc, (Expressions *)es2->elements->copy());
-	es2->elements->shift(e1);
-	e = es2;
-
-	if (type->toBasetype()->ty == Tsarray)
-	{
-	    e->type = new TypeSArray(e1->type, new IntegerExp(0, es2->elements->dim, Type::tindex));
-	    e->type = e->type->semantic(loc, NULL);
-	}
-	else
-	    e->type = type;
-    }
-    else if (e1->op == TOKnull && e2->op == TOKstring)
-    {
-	t = e1->type;
-	e = e2;
-	goto L1;
-    }
-    else if (e1->op == TOKstring && e2->op == TOKnull)
-    {	e = e1;
-	t = e2->type;
-      L1:
-	Type *tb = t->toBasetype();
-	if (tb->ty == Tarray && tb->nextOf()->equals(e->type))
-	{   Expressions *expressions = new Expressions();
-	    expressions->push(e);
-	    e = new ArrayLiteralExp(loc, expressions);
-	    e->type = t;
-	}
-	if (!e->type->equals(type))
-	{   StringExp *se = (StringExp *)e->copy();
-	    e = se->castTo(NULL, type);
-	}
-    }
-    return e;
-}
-
-Expression *Ptr(Type *type, Expression *e1)
-{
-    //printf("Ptr(e1 = %s)\n", e1->toChars());
-    if (e1->op == TOKadd)
-    {	AddExp *ae = (AddExp *)e1;
-	if (ae->e1->op == TOKaddress && ae->e2->op == TOKint64)
-	{   AddrExp *ade = (AddrExp *)ae->e1;
-	    if (ade->e1->op == TOKstructliteral)
-	    {	StructLiteralExp *se = (StructLiteralExp *)ade->e1;
-		unsigned offset = ae->e2->toInteger();
-		Expression *e = se->getField(type, offset);
-		if (!e)
-		    e = EXP_CANT_INTERPRET;
-		return e;
-	    }
-	}
-    }
-    return EXP_CANT_INTERPRET;
-}
-
+
+// 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 <math.h>
+
+#if __DMC__
+#include <complex.h>
+#endif
+
+#include "mem.h"
+#include "root.h"
+
+#include "mtype.h"
+#include "expression.h"
+#include "aggregate.h"
+#include "declaration.h"
+
+#ifdef IN_GCC
+#include "d-gcc-real.h"
+
+/* %% fix? */
+extern "C" bool real_isnan (const real_t *);
+#endif
+
+static real_t zero;	// work around DMC bug for now
+
+#define LOG 0
+
+Expression *expType(Type *type, Expression *e)
+{
+    if (type != e->type)
+    {
+	e = e->copy();
+	e->type = type;
+    }
+    return e;
+}
+
+/* ================================== isConst() ============================== */
+
+int Expression::isConst()
+{
+    //printf("Expression::isConst(): %s\n", toChars());
+    return 0;
+}
+
+int IntegerExp::isConst()
+{
+    return 1;
+}
+
+int RealExp::isConst()
+{
+    return 1;
+}
+
+int ComplexExp::isConst()
+{
+    return 1;
+}
+
+int SymOffExp::isConst()
+{
+    return 2;
+}
+
+/* =============================== constFold() ============================== */
+
+/* The constFold() functions were redundant with the optimize() ones,
+ * and so have been folded in with them.
+ */
+
+/* ========================================================================== */
+
+Expression *Neg(Type *type, Expression *e1)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    if (e1->type->isreal())
+    {
+	e = new RealExp(loc, -e1->toReal(), type);
+    }
+    else if (e1->type->isimaginary())
+    {
+	e = new RealExp(loc, -e1->toImaginary(), type);
+    }
+    else if (e1->type->iscomplex())
+    {
+	e = new ComplexExp(loc, -e1->toComplex(), type);
+    }
+    else
+	e = new IntegerExp(loc, -e1->toInteger(), type);
+    return e;
+}
+
+Expression *Com(Type *type, Expression *e1)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    e = new IntegerExp(loc, ~e1->toInteger(), type);
+    return e;
+}
+
+Expression *Not(Type *type, Expression *e1)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    e = new IntegerExp(loc, e1->isBool(0), type);
+    return e;
+}
+
+Expression *Bool(Type *type, Expression *e1)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    e = new IntegerExp(loc, e1->isBool(1), type);
+    return e;
+}
+
+Expression *Add(Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+#if LOG
+    printf("Add(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
+#endif
+    if (type->isreal())
+    {
+	e = new RealExp(loc, e1->toReal() + e2->toReal(), type);
+    }
+    else if (type->isimaginary())
+    {
+	e = new RealExp(loc, e1->toImaginary() + e2->toImaginary(), type);
+    }
+    else if (type->iscomplex())
+    {
+	// This rigamarole is necessary so that -0.0 doesn't get
+	// converted to +0.0 by doing an extraneous add with +0.0
+	complex_t c1;
+	real_t r1;
+	real_t i1;
+
+	complex_t c2;
+	real_t r2;
+	real_t i2;
+
+	complex_t v;
+	int x;
+
+	if (e1->type->isreal())
+	{   r1 = e1->toReal();
+	    x = 0;
+	}
+	else if (e1->type->isimaginary())
+	{   i1 = e1->toImaginary();
+	    x = 3;
+	}
+	else
+	{   c1 = e1->toComplex();
+	    x = 6;
+	}
+
+	if (e2->type->isreal())
+	{   r2 = e2->toReal();
+	}
+	else if (e2->type->isimaginary())
+	{   i2 = e2->toImaginary();
+	    x += 1;
+	}
+	else
+	{   c2 = e2->toComplex();
+	    x += 2;
+	}
+
+	switch (x)
+	{
+#if __DMC__
+	    case 0+0:	v = (complex_t) (r1 + r2);	break;
+	    case 0+1:	v = r1 + i2 * I;		break;
+	    case 0+2:	v = r1 + c2;			break;
+	    case 3+0:	v = i1 * I + r2;		break;
+	    case 3+1:	v = (complex_t) ((i1 + i2) * I); break;
+	    case 3+2:	v = i1 * I + c2;		break;
+	    case 6+0:	v = c1 + r2;			break;
+	    case 6+1:	v = c1 + i2 * I;		break;
+	    case 6+2:	v = c1 + c2;			break;
+#else
+	    case 0+0:	v = complex_t(r1 + r2, 0);	break;
+	    case 0+1:	v = complex_t(r1, i2);		break;
+	    case 0+2:	v = complex_t(r1 + creall(c2), cimagl(c2));	break;
+	    case 3+0:	v = complex_t(r2, i1);		break;
+	    case 3+1:	v = complex_t(0, i1 + i2);	break;
+	    case 3+2:	v = complex_t(creall(c2), i1 + cimagl(c2));	break;
+	    case 6+0:	v = complex_t(creall(c1) + r2, cimagl(c2));	break;
+	    case 6+1:	v = complex_t(creall(c1), cimagl(c1) + i2);	break;
+	    case 6+2:	v = c1 + c2;			break;
+#endif
+	    default: assert(0);
+	}
+	e = new ComplexExp(loc, v, type);
+    }
+    else if (e1->op == TOKsymoff)
+    {
+	SymOffExp *soe = (SymOffExp *)e1;
+	e = new SymOffExp(loc, soe->var, soe->offset + e2->toInteger());
+	e->type = type;
+    }
+    else if (e2->op == TOKsymoff)
+    {
+	SymOffExp *soe = (SymOffExp *)e2;
+	e = new SymOffExp(loc, soe->var, soe->offset + e1->toInteger());
+	e->type = type;
+    }
+    else
+	e = new IntegerExp(loc, e1->toInteger() + e2->toInteger(), type);
+    return e;
+}
+
+
+Expression *Min(Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    if (type->isreal())
+    {
+	e = new RealExp(loc, e1->toReal() - e2->toReal(), type);
+    }
+    else if (type->isimaginary())
+    {
+	e = new RealExp(loc, e1->toImaginary() - e2->toImaginary(), type);
+    }
+    else if (type->iscomplex())
+    {
+	// This rigamarole is necessary so that -0.0 doesn't get
+	// converted to +0.0 by doing an extraneous add with +0.0
+	complex_t c1;
+	real_t r1;
+	real_t i1;
+
+	complex_t c2;
+	real_t r2;
+	real_t i2;
+
+	complex_t v;
+	int x;
+
+	if (e1->type->isreal())
+	{   r1 = e1->toReal();
+	    x = 0;
+	}
+	else if (e1->type->isimaginary())
+	{   i1 = e1->toImaginary();
+	    x = 3;
+	}
+	else
+	{   c1 = e1->toComplex();
+	    x = 6;
+	}
+
+	if (e2->type->isreal())
+	{   r2 = e2->toReal();
+	}
+	else if (e2->type->isimaginary())
+	{   i2 = e2->toImaginary();
+	    x += 1;
+	}
+	else
+	{   c2 = e2->toComplex();
+	    x += 2;
+	}
+
+	switch (x)
+	{
+#if __DMC__
+	    case 0+0:	v = (complex_t) (r1 - r2);	break;
+	    case 0+1:	v = r1 - i2 * I;		break;
+	    case 0+2:	v = r1 - c2;			break;
+	    case 3+0:	v = i1 * I - r2;		break;
+	    case 3+1:	v = (complex_t) ((i1 - i2) * I); break;
+	    case 3+2:	v = i1 * I - c2;		break;
+	    case 6+0:	v = c1 - r2;			break;
+	    case 6+1:	v = c1 - i2 * I;		break;
+	    case 6+2:	v = c1 - c2;			break;
+#else
+	    case 0+0:	v = complex_t(r1 - r2, 0);	break;
+	    case 0+1:	v = complex_t(r1, -i2);		break;
+	    case 0+2:	v = complex_t(r1 - creall(c2), -cimagl(c2));	break;
+	    case 3+0:	v = complex_t(-r2, i1);		break;
+	    case 3+1:	v = complex_t(0, i1 - i2);	break;
+	    case 3+2:	v = complex_t(-creall(c2), i1 - cimagl(c2));	break;
+	    case 6+0:	v = complex_t(creall(c1) - r2, cimagl(c1));	break;
+	    case 6+1:	v = complex_t(creall(c1), cimagl(c1) - i2);	break;
+	    case 6+2:	v = c1 - c2;			break;
+#endif
+	    default: assert(0);
+	}
+	e = new ComplexExp(loc, v, type);
+    }
+    else if (e1->op == TOKsymoff)
+    {
+	SymOffExp *soe = (SymOffExp *)e1;
+	e = new SymOffExp(loc, soe->var, soe->offset - e2->toInteger());
+	e->type = type;
+    }
+    else
+    {
+	e = new IntegerExp(loc, e1->toInteger() - e2->toInteger(), type);
+    }
+    return e;
+}
+
+Expression *Mul(Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    if (type->isfloating())
+    {   complex_t c;
+#ifdef IN_GCC
+	real_t r;
+#else
+	d_float80 r;
+#endif
+
+	if (e1->type->isreal())
+	{
+#if __DMC__
+	    c = e1->toReal() * e2->toComplex();
+#else
+	    r = e1->toReal();
+	    c = e2->toComplex();
+	    c = complex_t(r * creall(c), r * cimagl(c));
+#endif
+	}
+	else if (e1->type->isimaginary())
+	{
+#if __DMC__
+	    c = e1->toImaginary() * I * e2->toComplex();
+#else
+	    r = e1->toImaginary();
+	    c = e2->toComplex();
+	    c = complex_t(-r * cimagl(c), r * creall(c));
+#endif
+	}
+	else if (e2->type->isreal())
+	{
+#if __DMC__
+	    c = e2->toReal() * e1->toComplex();
+#else
+	    r = e2->toReal();
+	    c = e1->toComplex();
+	    c = complex_t(r * creall(c), r * cimagl(c));
+#endif
+	}
+	else if (e2->type->isimaginary())
+	{
+#if __DMC__
+	    c = e1->toComplex() * e2->toImaginary() * I;
+#else
+	    r = e2->toImaginary();
+	    c = e1->toComplex();
+	    c = complex_t(-r * cimagl(c), r * creall(c));
+#endif
+	}
+	else
+	    c = e1->toComplex() * e2->toComplex();
+
+	if (type->isreal())
+	    e = new RealExp(loc, creall(c), type);
+	else if (type->isimaginary())
+	    e = new RealExp(loc, cimagl(c), type);
+	else if (type->iscomplex())
+	    e = new ComplexExp(loc, c, type);
+	else
+	    assert(0);
+    }
+    else
+    {
+	e = new IntegerExp(loc, e1->toInteger() * e2->toInteger(), type);
+    }
+    return e;
+}
+
+Expression *Div(Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    if (type->isfloating())
+    {   complex_t c;
+#ifdef IN_GCC
+	real_t r;
+#else
+	d_float80 r;
+#endif
+
+	//e1->type->print();
+	//e2->type->print();
+	if (e2->type->isreal())
+	{
+	    if (e1->type->isreal())
+	    {
+		e = new RealExp(loc, e1->toReal() / e2->toReal(), type);
+		return e;
+	    }
+#if __DMC__
+	    //r = e2->toReal();
+	    //c = e1->toComplex();
+	    //printf("(%Lg + %Lgi) / %Lg\n", creall(c), cimagl(c), r);
+
+	    c = e1->toComplex() / e2->toReal();
+#else
+	    r = e2->toReal();
+	    c = e1->toComplex();
+	    c = complex_t(creall(c) / r, cimagl(c) / r);
+#endif
+	}
+	else if (e2->type->isimaginary())
+	{
+#if __DMC__
+	    //r = e2->toImaginary();
+	    //c = e1->toComplex();
+	    //printf("(%Lg + %Lgi) / %Lgi\n", creall(c), cimagl(c), r);
+
+	    c = e1->toComplex() / (e2->toImaginary() * I);
+#else
+	    r = e2->toImaginary();
+	    c = e1->toComplex();
+	    c = complex_t(cimagl(c) / r, -creall(c) / r);
+#endif
+	}
+	else
+	{
+	    c = e1->toComplex() / e2->toComplex();
+	}
+
+	if (type->isreal())
+	    e = new RealExp(loc, creall(c), type);
+	else if (type->isimaginary())
+	    e = new RealExp(loc, cimagl(c), type);
+	else if (type->iscomplex())
+	    e = new ComplexExp(loc, c, type);
+	else
+	    assert(0);
+    }
+    else
+    {   sinteger_t n1;
+	sinteger_t n2;
+	sinteger_t n;
+
+	n1 = e1->toInteger();
+	n2 = e2->toInteger();
+	if (n2 == 0)
+	{   e2->error("divide by 0");
+	    e2 = new IntegerExp(0, 1, e2->type);
+	    n2 = 1;
+	}
+	if (e1->type->isunsigned() || e2->type->isunsigned())
+	    n = ((d_uns64) n1) / ((d_uns64) n2);
+	else
+	    n = n1 / n2;
+	e = new IntegerExp(loc, n, type);
+    }
+    return e;
+}
+
+Expression *Mod(Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    if (type->isfloating())
+    {
+	complex_t c;
+
+	if (e2->type->isreal())
+	{   real_t r2 = e2->toReal();
+
+#ifdef __DMC__
+	    c = fmodl(e1->toReal(), r2) + fmodl(e1->toImaginary(), r2) * I;
+#elif defined(IN_GCC)
+	    c = complex_t(e1->toReal() % r2, e1->toImaginary() % r2);
+#else
+	    c = complex_t(fmodl(e1->toReal(), r2), fmodl(e1->toImaginary(), r2));
+#endif
+	}
+	else if (e2->type->isimaginary())
+	{   real_t i2 = e2->toImaginary();
+
+#ifdef __DMC__
+	    c = fmodl(e1->toReal(), i2) + fmodl(e1->toImaginary(), i2) * I;
+#elif defined(IN_GCC)
+	    c = complex_t(e1->toReal() % i2, e1->toImaginary() % i2);
+#else
+	    c = complex_t(fmodl(e1->toReal(), i2), fmodl(e1->toImaginary(), i2));
+#endif
+	}
+	else
+	    assert(0);
+
+	if (type->isreal())
+	    e = new RealExp(loc, creall(c), type);
+	else if (type->isimaginary())
+	    e = new RealExp(loc, cimagl(c), type);
+	else if (type->iscomplex())
+	    e = new ComplexExp(loc, c, type);
+	else
+	    assert(0);
+    }
+    else
+    {   sinteger_t n1;
+	sinteger_t n2;
+	sinteger_t n;
+
+	n1 = e1->toInteger();
+	n2 = e2->toInteger();
+	if (n2 == 0)
+	{   e2->error("divide by 0");
+	    e2 = new IntegerExp(0, 1, e2->type);
+	    n2 = 1;
+	}
+	if (e1->type->isunsigned() || e2->type->isunsigned())
+	    n = ((d_uns64) n1) % ((d_uns64) n2);
+	else
+	    n = n1 % n2;
+	e = new IntegerExp(loc, n, type);
+    }
+    return e;
+}
+
+Expression *Shl(Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    e = new IntegerExp(loc, e1->toInteger() << e2->toInteger(), type);
+    return e;
+}
+
+Expression *Shr(Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+    unsigned count;
+    integer_t value;
+
+    value = e1->toInteger();
+    count = e2->toInteger();
+    switch (e1->type->toBasetype()->ty)
+    {
+	case Tint8:
+		value = (d_int8)(value) >> count;
+		break;
+
+	case Tuns8:
+		value = (d_uns8)(value) >> count;
+		break;
+
+	case Tint16:
+		value = (d_int16)(value) >> count;
+		break;
+
+	case Tuns16:
+		value = (d_uns16)(value) >> count;
+		break;
+
+	case Tint32:
+		value = (d_int32)(value) >> count;
+		break;
+
+	case Tuns32:
+		value = (d_uns32)(value) >> count;
+		break;
+
+	case Tint64:
+		value = (d_int64)(value) >> count;
+		break;
+
+	case Tuns64:
+		value = (d_uns64)(value) >> count;
+		break;
+
+	default:
+		assert(0);
+    }
+    e = new IntegerExp(loc, value, type);
+    return e;
+}
+
+Expression *Ushr(Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+    unsigned count;
+    integer_t value;
+
+    value = e1->toInteger();
+    count = e2->toInteger();
+    switch (e1->type->toBasetype()->ty)
+    {
+	case Tint8:
+	case Tuns8:
+		assert(0);		// no way to trigger this
+		value = (value & 0xFF) >> count;
+		break;
+
+	case Tint16:
+	case Tuns16:
+		assert(0);		// no way to trigger this
+		value = (value & 0xFFFF) >> count;
+		break;
+
+	case Tint32:
+	case Tuns32:
+		value = (value & 0xFFFFFFFF) >> count;
+		break;
+
+	case Tint64:
+	case Tuns64:
+		value = (d_uns64)(value) >> count;
+		break;
+
+	default:
+		assert(0);
+    }
+    e = new IntegerExp(loc, value, type);
+    return e;
+}
+
+Expression *And(Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    e = new IntegerExp(loc, e1->toInteger() & e2->toInteger(), type);
+    return e;
+}
+
+Expression *Or(Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    e = new IntegerExp(loc, e1->toInteger() | e2->toInteger(), type);
+    return e;
+}
+
+Expression *Xor(Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    e = new IntegerExp(loc, e1->toInteger() ^ e2->toInteger(), type);
+    return e;
+}
+
+/* Also returns EXP_CANT_INTERPRET if cannot be computed.
+ */
+Expression *Equal(enum TOK op, Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+    int cmp;
+    real_t r1;
+    real_t r2;
+
+    //printf("Equal(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
+
+    assert(op == TOKequal || op == TOKnotequal);
+
+    if (e1->op == TOKnull)
+    {
+	if (e2->op == TOKnull)
+	    cmp = 1;
+	else if (e2->op == TOKstring)
+	{   StringExp *es2 = (StringExp *)e2;
+	    cmp = (0 == es2->len);
+	}
+	else if (e2->op == TOKarrayliteral)
+	{   ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
+	    cmp = !es2->elements || (0 == es2->elements->dim);
+	}
+	else
+	    return EXP_CANT_INTERPRET;
+    }
+    else if (e2->op == TOKnull)
+    {
+	if (e1->op == TOKstring)
+	{   StringExp *es1 = (StringExp *)e1;
+	    cmp = (0 == es1->len);
+	}
+	else if (e1->op == TOKarrayliteral)
+	{   ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
+	    cmp = !es1->elements || (0 == es1->elements->dim);
+	}
+	else
+	    return EXP_CANT_INTERPRET;
+    }
+    else if (e1->op == TOKstring && e2->op == TOKstring)
+    {	StringExp *es1 = (StringExp *)e1;
+	StringExp *es2 = (StringExp *)e2;
+
+	assert(es1->sz == es2->sz);
+	if (es1->len == es2->len &&
+	    memcmp(es1->string, es2->string, es1->sz * es1->len) == 0)
+	    cmp = 1;
+	else
+	    cmp = 0;
+    }
+    else if (e1->op == TOKarrayliteral && e2->op == TOKarrayliteral)
+    {   ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
+	ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
+
+	if ((!es1->elements || !es1->elements->dim) &&
+	    (!es2->elements || !es2->elements->dim))
+	    cmp = 1;		// both arrays are empty
+	else if (!es1->elements || !es2->elements)
+	    cmp = 0;
+	else if (es1->elements->dim != es2->elements->dim)
+	    cmp = 0;
+	else
+	{
+	    for (size_t i = 0; i < es1->elements->dim; i++)
+	    {   Expression *ee1 = (Expression *)es1->elements->data[i];
+		Expression *ee2 = (Expression *)es2->elements->data[i];
+
+		Expression *v = Equal(TOKequal, Type::tint32, ee1, ee2);
+		if (v == EXP_CANT_INTERPRET)
+		    return EXP_CANT_INTERPRET;
+		cmp = v->toInteger();
+		if (cmp == 0)
+		    break;
+	    }
+	}
+    }
+    else if (e1->op == TOKarrayliteral && e2->op == TOKstring)
+    {	// Swap operands and use common code
+	Expression *e = e1;
+	e1 = e2;
+	e2 = e;
+	goto Lsa;
+    }
+    else if (e1->op == TOKstring && e2->op == TOKarrayliteral)
+    {
+     Lsa:
+	StringExp *es1 = (StringExp *)e1;
+	ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
+	size_t dim1 = es1->len;
+	size_t dim2 = es2->elements ? es2->elements->dim : 0;
+	if (dim1 != dim2)
+	    cmp = 0;
+	else
+	{
+	    for (size_t i = 0; i < dim1; i++)
+	    {
+		uinteger_t c = es1->charAt(i);
+		Expression *ee2 = (Expression *)es2->elements->data[i];
+		if (ee2->isConst() != 1)
+		    return EXP_CANT_INTERPRET;
+		cmp = (c == ee2->toInteger());
+		if (cmp == 0)
+		    break;
+	    }
+	}
+    }
+    else if (e1->op == TOKstructliteral && e2->op == TOKstructliteral)
+    {   StructLiteralExp *es1 = (StructLiteralExp *)e1;
+	StructLiteralExp *es2 = (StructLiteralExp *)e2;
+
+	if (es1->sd != es2->sd)
+	    cmp = 0;
+	else if ((!es1->elements || !es1->elements->dim) &&
+	    (!es2->elements || !es2->elements->dim))
+	    cmp = 1;		// both arrays are empty
+	else if (!es1->elements || !es2->elements)
+	    cmp = 0;
+	else if (es1->elements->dim != es2->elements->dim)
+	    cmp = 0;
+	else
+	{
+	    cmp = 1;
+	    for (size_t i = 0; i < es1->elements->dim; i++)
+	    {   Expression *ee1 = (Expression *)es1->elements->data[i];
+		Expression *ee2 = (Expression *)es2->elements->data[i];
+
+		if (ee1 == ee2)
+		    continue;
+		if (!ee1 || !ee2)
+		{   cmp = 0;
+		    break;
+		}
+		Expression *v = Equal(TOKequal, Type::tint32, ee1, ee2);
+		if (v == EXP_CANT_INTERPRET)
+		    return EXP_CANT_INTERPRET;
+		cmp = v->toInteger();
+		if (cmp == 0)
+		    break;
+	    }
+	}
+    }
+#if 0 // Should handle this
+    else if (e1->op == TOKarrayliteral && e2->op == TOKstring)
+    {
+    }
+#endif
+    else if (e1->isConst() != 1 || e2->isConst() != 1)
+	return EXP_CANT_INTERPRET;
+    else if (e1->type->isreal())
+    {
+	r1 = e1->toReal();
+	r2 = e2->toReal();
+	goto L1;
+    }
+    else if (e1->type->isimaginary())
+    {
+	r1 = e1->toImaginary();
+	r2 = e2->toImaginary();
+     L1:
+#if __DMC__
+	cmp = (r1 == r2);
+#else
+	if (isnan(r1) || isnan(r2))	// if unordered
+	{
+	    cmp = 0;
+	}
+	else
+	{
+	    cmp = (r1 == r2);
+	}
+#endif
+    }
+    else if (e1->type->iscomplex())
+    {
+	cmp = e1->toComplex() == e2->toComplex();
+    }
+    else if (e1->type->isintegral())
+    {
+	cmp = (e1->toInteger() == e2->toInteger());
+    }
+    else
+	return EXP_CANT_INTERPRET;
+    if (op == TOKnotequal)
+	cmp ^= 1;
+    e = new IntegerExp(loc, cmp, type);
+    return e;
+}
+
+Expression *Identity(enum TOK op, Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+    int cmp;
+
+    if (e1->op == TOKnull && e2->op == TOKnull)
+    {
+	cmp = 1;
+    }
+    else if (e1->op == TOKsymoff && e2->op == TOKsymoff)
+    {
+	SymOffExp *es1 = (SymOffExp *)e1;
+	SymOffExp *es2 = (SymOffExp *)e2;
+
+	cmp = (es1->var == es2->var && es1->offset == es2->offset);
+    }
+    else if (e1->isConst() == 1 && e2->isConst() == 1)
+	return Equal((op == TOKidentity) ? TOKequal : TOKnotequal,
+		type, e1, e2);
+    else
+	assert(0);
+    if (op == TOKnotidentity)
+	cmp ^= 1;
+    return new IntegerExp(loc, cmp, type);
+}
+
+
+Expression *Cmp(enum TOK op, Type *type, Expression *e1, Expression *e2)
+{   Expression *e;
+    Loc loc = e1->loc;
+    integer_t n;
+    real_t r1;
+    real_t r2;
+
+    if (e1->type->isreal())
+    {
+	r1 = e1->toReal();
+	r2 = e2->toReal();
+	goto L1;
+    }
+    else if (e1->type->isimaginary())
+    {
+	r1 = e1->toImaginary();
+	r2 = e2->toImaginary();
+     L1:
+#if __DMC__
+	// DMC is the only compiler I know of that handles NAN arguments
+	// correctly in comparisons.
+	switch (op)
+	{
+	    case TOKlt:	   n = r1 <  r2;	break;
+	    case TOKle:	   n = r1 <= r2;	break;
+	    case TOKgt:	   n = r1 >  r2;	break;
+	    case TOKge:	   n = r1 >= r2;	break;
+
+	    case TOKleg:   n = r1 <>=  r2;	break;
+	    case TOKlg:	   n = r1 <>   r2;	break;
+	    case TOKunord: n = r1 !<>= r2;	break;
+	    case TOKue:	   n = r1 !<>  r2;	break;
+	    case TOKug:	   n = r1 !<=  r2;	break;
+	    case TOKuge:   n = r1 !<   r2;	break;
+	    case TOKul:	   n = r1 !>=  r2;	break;
+	    case TOKule:   n = r1 !>   r2;	break;
+
+	    default:
+		assert(0);
+	}
+#else
+	// Don't rely on compiler, handle NAN arguments separately
+#if IN_GCC
+	if (real_isnan(&r1) || real_isnan(&r2))	// if unordered
+#else
+	if (isnan(r1) || isnan(r2))	// if unordered
+#endif
+	{
+	    switch (op)
+	    {
+		case TOKlt:	n = 0;	break;
+		case TOKle:	n = 0;	break;
+		case TOKgt:	n = 0;	break;
+		case TOKge:	n = 0;	break;
+
+		case TOKleg:	n = 0;	break;
+		case TOKlg:	n = 0;	break;
+		case TOKunord:	n = 1;	break;
+		case TOKue:	n = 1;	break;
+		case TOKug:	n = 1;	break;
+		case TOKuge:	n = 1;	break;
+		case TOKul:	n = 1;	break;
+		case TOKule:	n = 1;	break;
+
+		default:
+		    assert(0);
+	    }
+	}
+	else
+	{
+	    switch (op)
+	    {
+		case TOKlt:	n = r1 <  r2;	break;
+		case TOKle:	n = r1 <= r2;	break;
+		case TOKgt:	n = r1 >  r2;	break;
+		case TOKge:	n = r1 >= r2;	break;
+
+		case TOKleg:	n = 1;		break;
+		case TOKlg:	n = r1 != r2;	break;
+		case TOKunord:	n = 0;		break;
+		case TOKue:	n = r1 == r2;	break;
+		case TOKug:	n = r1 >  r2;	break;
+		case TOKuge:	n = r1 >= r2;	break;
+		case TOKul:	n = r1 <  r2;	break;
+		case TOKule:	n = r1 <= r2;	break;
+
+		default:
+		    assert(0);
+	    }
+	}
+#endif
+    }
+    else if (e1->type->iscomplex())
+    {
+	assert(0);
+    }
+    else
+    {   sinteger_t n1;
+	sinteger_t n2;
+
+	n1 = e1->toInteger();
+	n2 = e2->toInteger();
+	if (e1->type->isunsigned() || e2->type->isunsigned())
+	{
+	    switch (op)
+	    {
+		case TOKlt:	n = ((d_uns64) n1) <  ((d_uns64) n2);	break;
+		case TOKle:	n = ((d_uns64) n1) <= ((d_uns64) n2);	break;
+		case TOKgt:	n = ((d_uns64) n1) >  ((d_uns64) n2);	break;
+		case TOKge:	n = ((d_uns64) n1) >= ((d_uns64) n2);	break;
+
+		case TOKleg:	n = 1;					break;
+		case TOKlg:	n = ((d_uns64) n1) != ((d_uns64) n2);	break;
+		case TOKunord:	n = 0;					break;
+		case TOKue:	n = ((d_uns64) n1) == ((d_uns64) n2);	break;
+		case TOKug:	n = ((d_uns64) n1) >  ((d_uns64) n2);	break;
+		case TOKuge:	n = ((d_uns64) n1) >= ((d_uns64) n2);	break;
+		case TOKul:	n = ((d_uns64) n1) <  ((d_uns64) n2);	break;
+		case TOKule:	n = ((d_uns64) n1) <= ((d_uns64) n2);	break;
+
+		default:
+		    assert(0);
+	    }
+	}
+	else
+	{
+	    switch (op)
+	    {
+		case TOKlt:	n = n1 <  n2;	break;
+		case TOKle:	n = n1 <= n2;	break;
+		case TOKgt:	n = n1 >  n2;	break;
+		case TOKge:	n = n1 >= n2;	break;
+
+		case TOKleg:	n = 1;		break;
+		case TOKlg:	n = n1 != n2;	break;
+		case TOKunord:	n = 0;		break;
+		case TOKue:	n = n1 == n2;	break;
+		case TOKug:	n = n1 >  n2;	break;
+		case TOKuge:	n = n1 >= n2;	break;
+		case TOKul:	n = n1 <  n2;	break;
+		case TOKule:	n = n1 <= n2;	break;
+
+		default:
+		    assert(0);
+	    }
+	}
+    }
+    e = new IntegerExp(loc, n, type);
+    return e;
+}
+
+/* Also returns EXP_CANT_INTERPRET if cannot be computed.
+ *  to:	type to cast to
+ *  type: type to paint the result
+ */
+
+Expression *Cast(Type *type, Type *to, Expression *e1)
+{   Expression *e = EXP_CANT_INTERPRET;
+    Loc loc = e1->loc;
+
+    //printf("Cast(type = %s, to = %s, e1 = %s)\n", type->toChars(), to->toChars(), e1->toChars());
+    //printf("e1->type = %s\n", e1->type->toChars());
+    if (type->equals(e1->type) && to->equals(type))
+	return e1;
+
+    if (e1->isConst() != 1)
+	return EXP_CANT_INTERPRET;
+
+    Type *tb = to->toBasetype();
+    if (tb->ty == Tbool)
+	e = new IntegerExp(loc, e1->toInteger() != 0, type);
+    else if (type->isintegral())
+    {
+	if (e1->type->isfloating())
+	{   integer_t result;
+	    real_t r = e1->toReal();
+
+	    switch (type->toBasetype()->ty)
+	    {
+		case Tint8:	result = (d_int8)r;	break;
+		case Tchar:
+		case Tuns8:	result = (d_uns8)r;	break;
+		case Tint16:	result = (d_int16)r;	break;
+		case Twchar:
+		case Tuns16:	result = (d_uns16)r;	break;
+		case Tint32:	result = (d_int32)r;	break;
+		case Tdchar:
+		case Tuns32:	result = (d_uns32)r;	break;
+		case Tint64:	result = (d_int64)r;	break;
+		case Tuns64:	result = (d_uns64)r;	break;
+		default:
+		    assert(0);
+	    }
+
+	    e = new IntegerExp(loc, result, type);
+	}
+	else if (type->isunsigned())
+	    e = new IntegerExp(loc, e1->toUInteger(), type);
+	else
+	    e = new IntegerExp(loc, e1->toInteger(), type);
+    }
+    else if (tb->isreal())
+    {   real_t value = e1->toReal();
+
+	e = new RealExp(loc, value, type);
+    }
+    else if (tb->isimaginary())
+    {   real_t value = e1->toImaginary();
+
+	e = new RealExp(loc, value, type);
+    }
+    else if (tb->iscomplex())
+    {   complex_t value = e1->toComplex();
+
+	e = new ComplexExp(loc, value, type);
+    }
+    else if (tb->isscalar())
+	e = new IntegerExp(loc, e1->toInteger(), type);
+    else if (tb->ty == Tvoid)
+	e = EXP_CANT_INTERPRET;
+    else if (tb->ty == Tstruct && e1->op == TOKint64)
+    {	// Struct = 0;
+	StructDeclaration *sd = tb->toDsymbol(NULL)->isStructDeclaration();
+	assert(sd);
+	Expressions *elements = new Expressions;
+	for (size_t i = 0; i < sd->fields.dim; i++)
+	{   Dsymbol *s = (Dsymbol *)sd->fields.data[i];
+	    VarDeclaration *v = s->isVarDeclaration();
+	    assert(v);
+
+	    Expression *exp = new IntegerExp(0);
+	    exp = Cast(v->type, v->type, exp);
+	    if (exp == EXP_CANT_INTERPRET)
+		return exp;
+	    elements->push(exp);
+	}
+	e = new StructLiteralExp(loc, sd, elements);
+	e->type = type;
+    }
+    else
+    {
+	error(loc, "cannot cast %s to %s", e1->type->toChars(), type->toChars());
+	e = new IntegerExp(loc, 0, type);
+    }
+    return e;
+}
+
+
+Expression *ArrayLength(Type *type, Expression *e1)
+{   Expression *e;
+    Loc loc = e1->loc;
+
+    if (e1->op == TOKstring)
+    {	StringExp *es1 = (StringExp *)e1;
+
+	e = new IntegerExp(loc, es1->len, type);
+    }
+    else if (e1->op == TOKarrayliteral)
+    {	ArrayLiteralExp *ale = (ArrayLiteralExp *)e1;
+	size_t dim;
+
+	dim = ale->elements ? ale->elements->dim : 0;
+	e = new IntegerExp(loc, dim, type);
+    }
+    else if (e1->op == TOKassocarrayliteral)
+    {	AssocArrayLiteralExp *ale = (AssocArrayLiteralExp *)e1;
+	size_t dim = ale->keys->dim;
+
+	e = new IntegerExp(loc, dim, type);
+    }
+    else
+	e = EXP_CANT_INTERPRET;
+    return e;
+}
+
+/* Also return EXP_CANT_INTERPRET if this fails
+ */
+Expression *Index(Type *type, Expression *e1, Expression *e2)
+{   Expression *e = EXP_CANT_INTERPRET;
+    Loc loc = e1->loc;
+
+    //printf("Index(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
+    assert(e1->type);
+    if (e1->op == TOKstring && e2->op == TOKint64)
+    {	StringExp *es1 = (StringExp *)e1;
+	uinteger_t i = e2->toInteger();
+
+	if (i >= es1->len)
+        e1->error("string index %llu is out of bounds [0 .. %"PRIuSIZE"]", i, es1->len);
+	else
+	{   unsigned value = es1->charAt(i);
+	    e = new IntegerExp(loc, value, type);
+	}
+    }
+    else if (e1->type->toBasetype()->ty == Tsarray && e2->op == TOKint64)
+    {	TypeSArray *tsa = (TypeSArray *)e1->type->toBasetype();
+	uinteger_t length = tsa->dim->toInteger();
+	uinteger_t i = e2->toInteger();
+
+	if (i >= length)
+	{
+        e2->error("array index %llu is out of bounds %s[0 .. %llu]", i, e1->toChars(), length);
+	}
+	else if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2))
+	{   ArrayLiteralExp *ale = (ArrayLiteralExp *)e1;
+	    e = (Expression *)ale->elements->data[i];
+	    e->type = type;
+	}
+    }
+    else if (e1->type->toBasetype()->ty == Tarray && e2->op == TOKint64)
+    {
+	uinteger_t i = e2->toInteger();
+
+	if (e1->op == TOKarrayliteral && !e1->checkSideEffect(2))
+	{   ArrayLiteralExp *ale = (ArrayLiteralExp *)e1;
+	    if (i >= ale->elements->dim)
+	    {
+            e2->error("array index %llu is out of bounds %s[0 .. %u]", i, e1->toChars(), ale->elements->dim);
+	    }
+	    else
+	    {	e = (Expression *)ale->elements->data[i];
+		e->type = type;
+	    }
+	}
+    }
+    else if (e1->op == TOKassocarrayliteral && !e1->checkSideEffect(2))
+    {
+	AssocArrayLiteralExp *ae = (AssocArrayLiteralExp *)e1;
+	/* Search the keys backwards, in case there are duplicate keys
+	 */
+	for (size_t i = ae->keys->dim; i;)
+	{
+	    i--;
+	    Expression *ekey = (Expression *)ae->keys->data[i];
+	    Expression *ex = Equal(TOKequal, Type::tbool, ekey, e2);
+	    if (ex == EXP_CANT_INTERPRET)
+		return ex;
+	    if (ex->isBool(TRUE))
+	    {	e = (Expression *)ae->values->data[i];
+		e->type = type;
+		break;
+	    }
+	}
+    }
+    return e;
+}
+
+/* Also return EXP_CANT_INTERPRET if this fails
+ */
+Expression *Slice(Type *type, Expression *e1, Expression *lwr, Expression *upr)
+{   Expression *e = EXP_CANT_INTERPRET;
+    Loc loc = e1->loc;
+
+#if LOG
+    printf("Slice()\n");
+    if (lwr)
+    {	printf("\te1 = %s\n", e1->toChars());
+	printf("\tlwr = %s\n", lwr->toChars());
+	printf("\tupr = %s\n", upr->toChars());
+    }
+#endif
+    if (e1->op == TOKstring && lwr->op == TOKint64 && upr->op == TOKint64)
+    {	StringExp *es1 = (StringExp *)e1;
+	uinteger_t ilwr = lwr->toInteger();
+	uinteger_t iupr = upr->toInteger();
+
+	if (iupr > es1->len || ilwr > iupr)
+        e1->error("string slice [%llu .. %llu] is out of bounds", ilwr, iupr);
+	else
+	{   integer_t value;
+	    void *s;
+	    size_t len = iupr - ilwr;
+	    int sz = es1->sz;
+	    StringExp *es;
+
+	    s = mem.malloc((len + 1) * sz);
+	    memcpy((unsigned char *)s, (unsigned char *)es1->string + ilwr * sz, len * sz);
+	    memset((unsigned char *)s + len * sz, 0, sz);
+
+	    es = new StringExp(loc, s, len, es1->postfix);
+	    es->sz = sz;
+	    es->committed = 1;
+	    es->type = type;
+	    e = es;
+	}
+    }
+    else if (e1->op == TOKarrayliteral &&
+	    lwr->op == TOKint64 && upr->op == TOKint64 &&
+	    !e1->checkSideEffect(2))
+    {	ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
+	uinteger_t ilwr = lwr->toInteger();
+	uinteger_t iupr = upr->toInteger();
+
+	if (iupr > es1->elements->dim || ilwr > iupr)
+        e1->error("array slice [%llu .. %llu] is out of bounds", ilwr, iupr);
+	else
+	{
+	    Expressions *elements = new Expressions();
+	    elements->setDim(iupr - ilwr);
+	    memcpy(elements->data,
+		   es1->elements->data + ilwr,
+		   (iupr - ilwr) * sizeof(es1->elements->data[0]));
+	    e = new ArrayLiteralExp(e1->loc, elements);
+	    e->type = type;
+	}
+    }
+    return e;
+}
+
+/* Also return EXP_CANT_INTERPRET if this fails
+ */
+Expression *Cat(Type *type, Expression *e1, Expression *e2)
+{   Expression *e = EXP_CANT_INTERPRET;
+    Loc loc = e1->loc;
+    Type *t;
+
+    //printf("Cat(e1 = %s, e2 = %s)\n", e1->toChars(), e2->toChars());
+
+    if (e1->op == TOKnull && (e2->op == TOKint64 || e2->op == TOKstructliteral))
+    {	e = e2;
+	goto L2;
+    }
+    else if ((e1->op == TOKint64 || e1->op == TOKstructliteral) && e2->op == TOKnull)
+    {	e = e1;
+     L2:
+	Type *tn = e->type->toBasetype();
+	if (tn->ty == Tchar || tn->ty == Twchar || tn->ty == Tdchar)
+	{
+	    // Create a StringExp
+	    void *s;
+	    StringExp *es;
+	    size_t len = 1;
+	    int sz = tn->size();
+	    integer_t v = e->toInteger();
+
+	    s = mem.malloc((len + 1) * sz);
+	    memcpy((unsigned char *)s, &v, sz);
+
+	    // Add terminating 0
+	    memset((unsigned char *)s + len * sz, 0, sz);
+
+	    es = new StringExp(loc, s, len);
+	    es->sz = sz;
+	    es->committed = 1;
+	    e = es;
+	}
+	else
+	{   // Create an ArrayLiteralExp
+	    Expressions *elements = new Expressions();
+	    elements->push(e);
+	    e = new ArrayLiteralExp(e->loc, elements);
+	}
+	e->type = type;
+	return e;
+    }
+    else if (e1->op == TOKstring && e2->op == TOKstring)
+    {
+	// Concatenate the strings
+	void *s;
+	StringExp *es1 = (StringExp *)e1;
+	StringExp *es2 = (StringExp *)e2;
+	StringExp *es;
+	Type *t;
+	size_t len = es1->len + es2->len;
+	int sz = es1->sz;
+
+	assert(sz == es2->sz);
+	s = mem.malloc((len + 1) * sz);
+	memcpy(s, es1->string, es1->len * sz);
+	memcpy((unsigned char *)s + es1->len * sz, es2->string, es2->len * sz);
+
+	// Add terminating 0
+	memset((unsigned char *)s + len * sz, 0, sz);
+
+	es = new StringExp(loc, s, len);
+	es->sz = sz;
+	es->committed = es1->committed | es2->committed;
+	if (es1->committed)
+	    t = es1->type;
+	else
+	    t = es2->type;
+	es->type = type;
+	e = es;
+    }
+    else if (e1->op == TOKstring && e2->op == TOKint64)
+    {
+	// Concatenate the strings
+	void *s;
+	StringExp *es1 = (StringExp *)e1;
+	StringExp *es;
+	Type *t;
+	size_t len = es1->len + 1;
+	int sz = es1->sz;
+	integer_t v = e2->toInteger();
+
+	s = mem.malloc((len + 1) * sz);
+	memcpy(s, es1->string, es1->len * sz);
+	memcpy((unsigned char *)s + es1->len * sz, &v, sz);
+
+	// Add terminating 0
+	memset((unsigned char *)s + len * sz, 0, sz);
+
+	es = new StringExp(loc, s, len);
+	es->sz = sz;
+	es->committed = es1->committed;
+	t = es1->type;
+	es->type = type;
+	e = es;
+    }
+    else if (e1->op == TOKint64 && e2->op == TOKstring)
+    {
+	// Concatenate the strings
+	void *s;
+	StringExp *es2 = (StringExp *)e2;
+	StringExp *es;
+	Type *t;
+	size_t len = 1 + es2->len;
+	int sz = es2->sz;
+	integer_t v = e1->toInteger();
+
+	s = mem.malloc((len + 1) * sz);
+	memcpy((unsigned char *)s, &v, sz);
+	memcpy((unsigned char *)s + sz, es2->string, es2->len * sz);
+
+	// Add terminating 0
+	memset((unsigned char *)s + len * sz, 0, sz);
+
+	es = new StringExp(loc, s, len);
+	es->sz = sz;
+	es->committed = es2->committed;
+	t = es2->type;
+	es->type = type;
+	e = es;
+    }
+    else if (e1->op == TOKarrayliteral && e2->op == TOKarrayliteral &&
+	e1->type->equals(e2->type))
+    {
+	// Concatenate the arrays
+	ArrayLiteralExp *es1 = (ArrayLiteralExp *)e1;
+	ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
+
+	es1 = new ArrayLiteralExp(es1->loc, (Expressions *)es1->elements->copy());
+	es1->elements->insert(es1->elements->dim, es2->elements);
+	e = es1;
+
+	if (type->toBasetype()->ty == Tsarray)
+	{
+	    e->type = new TypeSArray(e1->type->toBasetype()->next, new IntegerExp(0, es1->elements->dim, Type::tindex));
+	    e->type = e->type->semantic(loc, NULL);
+	}
+	else
+	    e->type = type;
+    }
+    else if ((e1->op == TOKarrayliteral || e1->op == TOKnull) &&
+	e1->type->toBasetype()->nextOf()->equals(e2->type))
+    {
+	ArrayLiteralExp *es1;
+	if (e1->op == TOKarrayliteral)
+	{   es1 = (ArrayLiteralExp *)e1;
+	    es1 = new ArrayLiteralExp(es1->loc, (Expressions *)es1->elements->copy());
+	    es1->elements->push(e2);
+	}
+	else
+	{
+	    es1 = new ArrayLiteralExp(e1->loc, e2);
+	}
+	e = es1;
+
+	if (type->toBasetype()->ty == Tsarray)
+	{
+	    e->type = new TypeSArray(e2->type, new IntegerExp(0, es1->elements->dim, Type::tindex));
+	    e->type = e->type->semantic(loc, NULL);
+	}
+	else
+	    e->type = type;
+    }
+    else if (e2->op == TOKarrayliteral &&
+	e2->type->toBasetype()->nextOf()->equals(e1->type))
+    {
+	ArrayLiteralExp *es2 = (ArrayLiteralExp *)e2;
+
+	es2 = new ArrayLiteralExp(es2->loc, (Expressions *)es2->elements->copy());
+	es2->elements->shift(e1);
+	e = es2;
+
+	if (type->toBasetype()->ty == Tsarray)
+	{
+	    e->type = new TypeSArray(e1->type, new IntegerExp(0, es2->elements->dim, Type::tindex));
+	    e->type = e->type->semantic(loc, NULL);
+	}
+	else
+	    e->type = type;
+    }
+    else if (e1->op == TOKnull && e2->op == TOKstring)
+    {
+	t = e1->type;
+	e = e2;
+	goto L1;
+    }
+    else if (e1->op == TOKstring && e2->op == TOKnull)
+    {	e = e1;
+	t = e2->type;
+      L1:
+	Type *tb = t->toBasetype();
+	if (tb->ty == Tarray && tb->nextOf()->equals(e->type))
+	{   Expressions *expressions = new Expressions();
+	    expressions->push(e);
+	    e = new ArrayLiteralExp(loc, expressions);
+	    e->type = t;
+	}
+	if (!e->type->equals(type))
+	{   StringExp *se = (StringExp *)e->copy();
+	    e = se->castTo(NULL, type);
+	}
+    }
+    return e;
+}
+
+Expression *Ptr(Type *type, Expression *e1)
+{
+    //printf("Ptr(e1 = %s)\n", e1->toChars());
+    if (e1->op == TOKadd)
+    {	AddExp *ae = (AddExp *)e1;
+	if (ae->e1->op == TOKaddress && ae->e2->op == TOKint64)
+	{   AddrExp *ade = (AddrExp *)ae->e1;
+	    if (ade->e1->op == TOKstructliteral)
+	    {	StructLiteralExp *se = (StructLiteralExp *)ade->e1;
+		unsigned offset = ae->e2->toInteger();
+		Expression *e = se->getField(type, offset);
+		if (!e)
+		    e = EXP_CANT_INTERPRET;
+		return e;
+	    }
+	}
+    }
+    return EXP_CANT_INTERPRET;
+}
+
--- a/dmd/declaration.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/declaration.c	Sat Jul 12 19:38:31 2008 +0200
@@ -1,6 +1,6 @@
 
 // Compiler implementation of the D programming language
-// Copyright (c) 1999-2007 by Digital Mars
+// Copyright (c) 1999-2008 by Digital Mars
 // All Rights Reserved
 // written by Walter Bright
 // http://www.digitalmars.com
@@ -39,7 +39,7 @@
 {
 }
 
-char *Declaration::kind()
+const char *Declaration::kind()
 {
     return "declaration";
 }
@@ -80,6 +80,76 @@
     return protection;
 }
 
+/*************************************
+ * Check to see if declaration can be modified in this context (sc).
+ * Issue error if not.
+ */
+
+#if DMDV2
+void Declaration::checkModify(Loc loc, Scope *sc, Type *t)
+{
+    if (sc->incontract && isParameter())
+	error(loc, "cannot modify parameter '%s' in contract", toChars());
+
+    if (isCtorinit())
+    {	// It's only modifiable if inside the right constructor
+	Dsymbol *s = sc->func;
+	while (1)
+	{
+	    FuncDeclaration *fd = NULL;
+	    if (s)
+		fd = s->isFuncDeclaration();
+	    if (fd &&
+		((fd->isCtorDeclaration() && storage_class & STCfield) ||
+		 (fd->isStaticCtorDeclaration() && !(storage_class & STCfield))) &&
+		fd->toParent() == toParent()
+	       )
+	    {
+		VarDeclaration *v = isVarDeclaration();
+		assert(v);
+		v->ctorinit = 1;
+		//printf("setting ctorinit\n");
+	    }
+	    else
+	    {
+		if (s)
+		{   s = s->toParent2();
+		    continue;
+		}
+		else
+		{
+		    const char *p = isStatic() ? "static " : "";
+		    error(loc, "can only initialize %sconst %s inside %sconstructor",
+			p, toChars(), p);
+		}
+	    }
+	    break;
+	}
+    }
+    else
+    {
+	VarDeclaration *v = isVarDeclaration();
+	if (v && v->canassign == 0)
+	{
+	    char *p = NULL;
+	    if (isConst())
+		p = "const";
+	    else if (isInvariant())
+		p = "invariant";
+	    else if (storage_class & STCmanifest)
+		p = "manifest constant";
+	    else if (!t->isAssignable())
+		p = "struct with immutable members";
+	    if (p)
+	    {	error(loc, "cannot modify %s", p);
+		halt();
+	    }
+	}
+    }
+}
+#endif
+
+
 /********************************* TupleDeclaration ****************************/
 
 TupleDeclaration::TupleDeclaration(Loc loc, Identifier *id, Objects *objects)
@@ -97,7 +167,7 @@
     return NULL;
 }
 
-char *TupleDeclaration::kind()
+const char *TupleDeclaration::kind()
 {
     return "tuple";
 }
@@ -231,6 +301,7 @@
 	type = type->semantic(loc, sc);
 	if (sc->parent->isFuncDeclaration() && init)
 	    semantic2(sc);
+	storage_class |= sc->stc & STCdeprecated;
     }
     else if (sem == 1)
     {
@@ -257,7 +328,7 @@
     }
 }
 
-char *TypedefDeclaration::kind()
+const char *TypedefDeclaration::kind()
 {
     return "typedef";
 }
@@ -318,6 +389,7 @@
 
 Dsymbol *AliasDeclaration::syntaxCopy(Dsymbol *s)
 {
+    //printf("AliasDeclaration::syntaxCopy()\n");
     assert(!s);
     AliasDeclaration *sa;
     if (type)
@@ -466,7 +538,7 @@
     }
 }
 
-char *AliasDeclaration::kind()
+const char *AliasDeclaration::kind()
 {
     return "alias";
 }
@@ -601,7 +673,9 @@
 void VarDeclaration::semantic(Scope *sc)
 {
     //printf("VarDeclaration::semantic('%s', parent = '%s')\n", toChars(), sc->parent->toChars());
-    //printf("type = %s\n", type->toChars());
+    //printf(" type = %s\n", type ? type->toChars() : "null");
+    //printf(" stc = x%x\n", sc->stc);
+    //printf(" storage_class = x%x\n", storage_class);
     //printf("linkage = %d\n", sc->linkage);
     //if (strcmp(toChars(), "mul") == 0) halt();
 
@@ -629,6 +703,7 @@
 	    originalType = type;
 	type = type->semantic(loc, sc);
     }
+    //printf(" semantic type = %s\n", type ? type->toChars() : "null");
 
     type->checkDeprecated(loc, sc);
     linkage = sc->linkage;
@@ -636,7 +711,7 @@
     //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);
+    //printf("storage_class = x%x\n", storage_class);
 
     Dsymbol *parent = toParent();
     FuncDeclaration *fd = parent->isFuncDeclaration();
@@ -749,6 +824,8 @@
 	    error("field not allowed in interface");
 	}
 
+	/* Templates cannot add fields to aggregates
+	 */
 	TemplateInstance *ti = parent->isTemplateInstance();
 	if (ti)
 	{
@@ -853,10 +930,6 @@
 	    // possibilities.
 	    if (fd && !isStatic() && !isConst() && !init->isVoidInitializer())
 	    {
-		Expression *e1;
-		Type *t;
-		int dim;
-
 		//printf("fd = '%s', var = '%s'\n", fd->toChars(), toChars());
 		if (!ei)
 		{
@@ -874,15 +947,15 @@
 		    init = ei;
 		}
 
-		e1 = new VarExp(loc, this);
+		Expression *e1 = new VarExp(loc, this);
 
-		t = type->toBasetype();
+		Type *t = type->toBasetype();
 		if (t->ty == Tsarray)
 		{
 		    ei->exp = ei->exp->semantic(sc);
 		    if (!ei->exp->implicitConvTo(type))
 		    {
-			dim = ((TypeSArray *)t)->dim->toInteger();
+			int dim = ((TypeSArray *)t)->dim->toInteger();
 			// If multidimensional static array, treat as one large array
 			while (1)
 			{
@@ -974,7 +1047,7 @@
 	ei = init->isExpInitializer();
     else
     {
-	Expression *e = type->defaultInit();
+	Expression *e = type->defaultInit(loc);
 	if (e)
 	    ei = new ExpInitializer(loc, e);
 	else
@@ -1001,7 +1074,7 @@
     }
 }
 
-char *VarDeclaration::kind()
+const char *VarDeclaration::kind()
 {
     return "variable";
 }
@@ -1020,6 +1093,17 @@
 	buf->writestring("const ");
     if (storage_class & STCstatic)
 	buf->writestring("static ");
+    if (storage_class & STCauto)
+	buf->writestring("auto ");
+#if DMDV2
+    if (storage_class & STCmanifest)
+	buf->writestring("manifest ");
+    if (storage_class & STCinvariant)
+	buf->writestring("invariant ");
+    if (storage_class & STCtls)
+	buf->writestring("__thread ");
+#endif
+
     if (type)
 	type->toCBuffer(buf, ident, hgs);
     else
@@ -1052,15 +1136,18 @@
 }
 
 /************************************
- * Check to see if variable is a reference to an enclosing function
- * or not.
+ * Check to see if this variable is actually in an enclosing function
+ * rather than the current one.
  */
 
 void VarDeclaration::checkNestedReference(Scope *sc, Loc loc)
 {
+    //printf("VarDeclaration::checkNestedReference() %s\n", toChars());
     if (parent && !isDataseg() && parent != sc->parent)
     {
+	// The function that this variable is in
 	FuncDeclaration *fdv = toParent()->isFuncDeclaration();
+	// The current function
 	FuncDeclaration *fdthis = sc->parent->isFuncDeclaration();
 
 	if (fdv && fdthis)
@@ -1077,6 +1164,7 @@
 
 /*******************************
  * Does symbol go into data segment?
+ * Includes extern variables.
  */
 
 int VarDeclaration::isDataseg()
@@ -1201,7 +1289,7 @@
 
 /***************************** TypeInfoConstDeclaration **********************/
 
-#if V2
+#if DMDV2
 TypeInfoConstDeclaration::TypeInfoConstDeclaration(Type *tinfo)
     : TypeInfoDeclaration(tinfo, 0)
 {
@@ -1210,7 +1298,7 @@
 
 /***************************** TypeInfoInvariantDeclaration **********************/
 
-#if V2
+#if DMDV2
 TypeInfoInvariantDeclaration::TypeInfoInvariantDeclaration(Type *tinfo)
     : TypeInfoDeclaration(tinfo, 0)
 {
--- a/dmd/declaration.h	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/declaration.h	Sat Jul 12 19:38:31 2008 +0200
@@ -73,6 +73,7 @@
     STCnothrow	    = 0x2000000,	// never throws exceptions
     STCpure	    = 0x4000000,	// pure function
     STCtls	    = 0x8000000,	// thread local
+    STCalias	    = 0x10000000,	// alias parameter
 };
 
 struct Match
@@ -101,7 +102,7 @@
 
     Declaration(Identifier *id);
     void semantic(Scope *sc);
-    char *kind();
+    const char *kind();
     unsigned size(Loc loc);
     void checkModify(Loc loc, Scope *sc, Type *t);
 
@@ -150,7 +151,7 @@
 
     TupleDeclaration(Loc loc, Identifier *ident, Objects *objects);
     Dsymbol *syntaxCopy(Dsymbol *);
-    char *kind();
+    const char *kind();
     Type *getType();
     int needThis();
 
@@ -174,7 +175,7 @@
     void semantic(Scope *sc);
     void semantic2(Scope *sc);
     char *mangle();
-    char *kind();
+    const char *kind();
     Type *getType();
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
 #ifdef _DH
@@ -184,7 +185,7 @@
 
     void toDocBuffer(OutBuffer *buf);
 
-    void toObjFile();			// compile to .obj file
+    void toObjFile(int multiobj);			// compile to .obj file
     void toDebug();
     int cvMember(unsigned char *p);
 
@@ -207,7 +208,7 @@
     Dsymbol *syntaxCopy(Dsymbol *);
     void semantic(Scope *sc);
     int overloadInsert(Dsymbol *s);
-    char *kind();
+    const char *kind();
     Type *getType();
     Dsymbol *toAlias();
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
@@ -242,7 +243,7 @@
     Dsymbol *syntaxCopy(Dsymbol *);
     void semantic(Scope *sc);
     void semantic2(Scope *sc);
-    char *kind();
+    const char *kind();
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
 #ifdef _DH
     Type *htype;
@@ -259,7 +260,7 @@
     Dsymbol *toAlias();
 
     Symbol *toSymbol();
-    void toObjFile();			// compile to .obj file
+    void toObjFile(int multiobj);			// compile to .obj file
     int cvMember(unsigned char *p);
 
     // Eliminate need for dynamic_cast
@@ -325,7 +326,7 @@
     void emitComment(Scope *sc);
 
     Symbol *toSymbol();
-    void toObjFile();			// compile to .obj file
+    void toObjFile(int multiobj);			// compile to .obj file
     virtual void toDt(dt_t **pdt);
 
     virtual TypeInfoDeclaration* isTypeInfoDeclaration() { return this; }
@@ -467,6 +468,32 @@
     void llvmDefine();
 };
 
+#if DMDV2
+struct TypeInfoConstDeclaration : TypeInfoDeclaration
+{
+    TypeInfoConstDeclaration(Type *tinfo);
+
+    void toDt(dt_t **pdt);
+
+    // LLVMDC
+    void llvmDeclare();
+    void llvmDefine();
+};
+
+struct TypeInfoInvariantDeclaration : TypeInfoDeclaration
+{
+    TypeInfoInvariantDeclaration(Type *tinfo);
+
+    void toDt(dt_t **pdt);
+
+    // LLVMDC
+    void llvmDeclare();
+    void llvmDefine();
+};
+#endif
+
+/**************************************************************/
+
 struct ThisDeclaration : VarDeclaration
 {
     ThisDeclaration(Type *t);
@@ -481,8 +508,7 @@
 };
 
 /**************************************************************/
-
-#if V2
+#if DMDV2
 
 enum BUILTIN
 {
@@ -548,7 +574,7 @@
     VarDeclaration *nrvo_var;		// variable to replace with shidden
     Symbol *shidden;			// hidden pointer passed to function
 
-#if V2
+#if DMDV2
     enum BUILTIN builtin;		// set if this is a known, builtin
 					// function we can evaluate at compile
 					// time
@@ -597,7 +623,7 @@
     void inlineScan();
     int canInline(int hasthis, int hdrscan = 0);
     Expression *doInline(InlineScanState *iss, Expression *ethis, Array *arguments);
-    char *kind();
+    const char *kind();
     void toDocBuffer(OutBuffer *buf);
 
     static FuncDeclaration *genCfunc(Type *treturn, char *name);
@@ -605,7 +631,7 @@
 
     Symbol *toSymbol();
     Symbol *toThunkSymbol(int offset);	// thunk version
-    void toObjFile();			// compile to .obj file
+    void toObjFile(int multiobj);			// compile to .obj file
     int cvMember(unsigned char *p);
 
     FuncDeclaration *isFuncDeclaration() { return this; }
@@ -627,7 +653,7 @@
     FuncAliasDeclaration(FuncDeclaration *funcalias);
 
     FuncAliasDeclaration *isFuncAliasDeclaration() { return this; }
-    char *kind();
+    const char *kind();
     Symbol *toSymbol();
 };
 
@@ -640,9 +666,10 @@
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
     Dsymbol *syntaxCopy(Dsymbol *);
     int isNested();
+    int isVirtual();
 
     FuncLiteralDeclaration *isFuncLiteralDeclaration() { return this; }
-    char *kind();
+    const char *kind();
 };
 
 struct CtorDeclaration : FuncDeclaration
@@ -653,7 +680,7 @@
     Dsymbol *syntaxCopy(Dsymbol *);
     void semantic(Scope *sc);
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
-    char *kind();
+    const char *kind();
     char *toChars();
     int isVirtual();
     int addPreInvariant();
@@ -663,6 +690,24 @@
     CtorDeclaration *isCtorDeclaration() { return this; }
 };
 
+#if DMDV2
+struct PostBlitDeclaration : FuncDeclaration
+{
+    PostBlitDeclaration(Loc loc, Loc endloc);
+    PostBlitDeclaration(Loc loc, Loc endloc, Identifier *id);
+    Dsymbol *syntaxCopy(Dsymbol *);
+    void semantic(Scope *sc);
+    void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
+    int isVirtual();
+    int addPreInvariant();
+    int addPostInvariant();
+    int overloadInsert(Dsymbol *s);
+    void emitComment(Scope *sc);
+
+    PostBlitDeclaration *isPostBlitDeclaration() { return this; }
+};
+#endif
+
 struct DtorDeclaration : FuncDeclaration
 {
     DtorDeclaration(Loc loc, Loc endloc);
@@ -696,7 +741,8 @@
 };
 
 struct StaticDtorDeclaration : FuncDeclaration
-{
+{   VarDeclaration *vgate;	// 'gate' variable
+
     StaticDtorDeclaration(Loc loc, Loc endloc);
     Dsymbol *syntaxCopy(Dsymbol *);
     void semantic(Scope *sc);
@@ -748,7 +794,7 @@
     Dsymbol *syntaxCopy(Dsymbol *);
     void semantic(Scope *sc);
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
-    char *kind();
+    const char *kind();
     int isVirtual();
     int addPreInvariant();
     int addPostInvariant();
@@ -764,7 +810,7 @@
     Dsymbol *syntaxCopy(Dsymbol *);
     void semantic(Scope *sc);
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
-    char *kind();
+    const char *kind();
     int isDelete();
     int isVirtual();
     int addPreInvariant();
--- a/dmd/doc.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/doc.c	Sat Jul 12 19:38:31 2008 +0200
@@ -400,7 +400,7 @@
 
 void ScopeDsymbol::emitMemberComments(Scope *sc)
 {
-    //printf("ScopeDsymbol::emitMemberComments()\n");
+    //printf("ScopeDsymbol::emitMemberComments() %s\n", toChars());
     OutBuffer *buf = sc->docbuf;
 
     if (members)
@@ -417,8 +417,9 @@
 	else if (isTemplateDeclaration())
 	    m = "$(DDOC_TEMPLATE_MEMBERS \n";
 
-	// BUG: if no members are actually printed, we should not emit DDOC_MEMBERS
+	unsigned offset1 = buf->offset;		// save starting offset
 	buf->writestring(m);
+	unsigned offset2 = buf->offset;		// to see if we write anything
 	sc = sc->push(this);
 	for (int i = 0; i < members->dim; i++)
 	{
@@ -427,7 +428,14 @@
 	    s->emitComment(sc);
 	}
 	sc->pop();
-	buf->writestring(")\n");
+	if (buf->offset == offset2)
+	{
+	    /* Didn't write out any members, so back out last write
+	     */
+	    buf->offset = offset1;
+	}
+	else
+	    buf->writestring(")\n");
     }
 }
 
@@ -448,7 +456,9 @@
 
 void Dsymbol::emitComment(Scope *sc)		   { }
 void InvariantDeclaration::emitComment(Scope *sc)  { }
-//void PostBlitDeclaration::emitComment(Scope *sc)   { }
+#if DMDV2
+void PostBlitDeclaration::emitComment(Scope *sc)   { }
+#endif
 void DtorDeclaration::emitComment(Scope *sc)	   { }
 void StaticCtorDeclaration::emitComment(Scope *sc) { }
 void StaticDtorDeclaration::emitComment(Scope *sc) { }
@@ -525,12 +535,8 @@
     //printf("TemplateDeclaration::emitComment() '%s', kind = %s\n", toChars(), kind());
     if (prot() == PROTprivate)
 	return;
-    if (!comment)
-	return;
 
-    OutBuffer *buf = sc->docbuf;
-    DocComment *dc = DocComment::parse(sc, this, comment);
-    unsigned o;
+    unsigned char *com = comment;
     int hasmembers = 1;
 
     Dsymbol *ss = this;
@@ -542,12 +548,22 @@
 	{
 	    ss = onemember->isFuncDeclaration();
 	    if (ss)
-		hasmembers = 0;
+	    {	hasmembers = 0;
+		if (com != ss->comment)
+		    com = Lexer::combineComments(com, ss->comment);
+	    }
 	    else
 		ss = this;
 	}
     }
 
+    if (!com)
+	return;
+
+    OutBuffer *buf = sc->docbuf;
+    DocComment *dc = DocComment::parse(sc, this, com);
+    unsigned o;
+
     if (!dc)
     {
 	ss->emitDitto(sc);
@@ -667,7 +683,7 @@
 	    buf->writestring("static ");
 	if (d->isConst())
 	    buf->writestring("const ");
-#if V2
+#if DMDV2
 	if (d->isInvariant())
 	    buf->writestring("invariant ");
 #endif
@@ -743,7 +759,9 @@
 	if (parent &&
 	    (td = parent->isTemplateDeclaration()) != NULL &&
 	    td->onemember == this)
-	{   HdrGenState hgs;
+	{   /* It's a function template
+	     */
+	    HdrGenState hgs;
 	    unsigned o = buf->offset;
 	    TypeFunction *tf = (TypeFunction *)type;
 
@@ -1581,7 +1599,13 @@
      */
     if (f && f->type)
     {
-	TypeFunction *tf = (TypeFunction *)f->type;
+	TypeFunction *tf;
+	if (f->originalType)
+	{
+	    tf = (TypeFunction *)f->originalType;
+	}
+	else
+	    tf = (TypeFunction *)f->type;
 
 	if (tf->parameters)
 	{
--- a/dmd/dsymbol.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/dsymbol.c	Sat Jul 12 19:38:31 2008 +0200
@@ -1,6 +1,6 @@
 
 // Compiler implementation of the D programming language
-// Copyright (c) 1999-2007 by Digital Mars
+// Copyright (c) 1999-2008 by Digital Mars
 // All Rights Reserved
 // written by Walter Bright
 // http://www.digitalmars.com
@@ -201,7 +201,7 @@
     return loc.toChars();
 }
 
-char *Dsymbol::kind()
+const char *Dsymbol::kind()
 {
     return "symbol";
 }
@@ -244,6 +244,16 @@
     return s;
 }
 
+TemplateInstance *Dsymbol::inTemplateInstance()
+{
+    for (Dsymbol *parent = this->parent; parent; parent = parent->parent)
+    {
+	TemplateInstance *ti = parent->isTemplateInstance();
+	if (ti)
+	    return ti;
+    }
+    return NULL;
+}
 
 int Dsymbol::isAnonymous()
 {
@@ -270,6 +280,16 @@
     // Most Dsymbols have no further semantic analysis needed
 }
 
+/*********************************************
+ * Search for ident as member of s.
+ * Input:
+ *	flags:	1	don't find private members
+ *		2	don't give error messages
+ *		4	return NULL if ambiguous
+ * Returns:
+ *	NULL if not found
+ */
+
 Dsymbol *Dsymbol::search(Loc loc, Identifier *ident, int flags)
 {
     //printf("Dsymbol::search(this=%p,%s, ident='%s')\n", this, toChars(), ident->toChars());
@@ -389,7 +409,9 @@
 
 AggregateDeclaration *Dsymbol::isMember()	// is this a member of an AggregateDeclaration?
 {
+    //printf("Dsymbol::isMember() %s\n", toChars());
     Dsymbol *parent = toParent();
+    //printf("parent is %s %s\n", parent->kind(), parent->toChars());
     return parent ? parent->isAggregateDeclaration() : NULL;
 }
 
@@ -502,6 +524,10 @@
 	{
 	    if (sc->scopesym && sc->scopesym->isDeprecated())
 		return;
+
+	    // If inside a StorageClassDeclaration that is deprecated
+	    if (sc->stc & STCdeprecated)
+		return;
 	}
 
 	error(loc, "is deprecated");
@@ -617,12 +643,11 @@
 }
 
 Dsymbol *ScopeDsymbol::search(Loc loc, Identifier *ident, int flags)
-{   Dsymbol *s;
-    int i;
+{
+    //printf("%s->ScopeDsymbol::search(ident='%s', flags=x%x)\n", toChars(), ident->toChars(), flags);
 
-    //printf("%s->ScopeDsymbol::search(ident='%s', flags=x%x)\n", toChars(), ident->toChars(), flags);
     // Look in symbols declared in this module
-    s = symtab ? symtab->lookup(ident) : NULL;
+    Dsymbol *s = symtab ? symtab->lookup(ident) : NULL;
     if (s)
     {
 	//printf("\ts = '%s.%s'\n",toChars(),s->toChars());
@@ -630,7 +655,7 @@
     else if (imports)
     {
 	// Look in imported modules
-	for (i = 0; i < imports->dim; i++)
+	for (int i = 0; i < imports->dim; i++)
 	{   ScopeDsymbol *ss = (ScopeDsymbol *)imports->data[i];
 	    Dsymbol *s2;
 
@@ -639,6 +664,8 @@
 		continue;
 
 	    //printf("\tscanning import '%s', prots = %d, isModule = %p, isImport = %p\n", ss->toChars(), prots[i], ss->isModule(), ss->isImport());
+	    /* Don't find private members if ss is a module
+	     */
 	    s2 = ss->search(loc, ident, ss->isModule() ? 1 : 0);
 	    if (!s)
 		s = s2;
@@ -646,6 +673,10 @@
 	    {
 		if (s->toAlias() == s2->toAlias())
 		{
+		    /* After following aliases, we found the same symbol,
+		     * so it's not an ambiguity.
+		     * But if one alias is deprecated, prefer the other.
+		     */
 		    if (s->isDeprecated())
 			s = s2;
 		}
@@ -768,7 +799,7 @@
     return sprev;
 }
 
-char *ScopeDsymbol::kind()
+const char *ScopeDsymbol::kind()
 {
     return "ScopeDsymbol";
 }
@@ -780,7 +811,7 @@
  * Returns NULL if not found
  */
 
-#if V2
+#if DMDV2
 FuncDeclaration *ScopeDsymbol::findGetMembers()
 {
     Dsymbol *s = search_function(this, Id::getmembers);
--- a/dmd/dsymbol.h	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/dsymbol.h	Sat Jul 12 19:38:31 2008 +0200
@@ -1,6 +1,6 @@
 
 // Compiler implementation of the D programming language
-// Copyright (c) 1999-2007 by Digital Mars
+// Copyright (c) 1999-2008 by Digital Mars
 // All Rights Reserved
 // written by Walter Bright
 // http://www.digitalmars.com
@@ -123,12 +123,13 @@
     Dsymbol *pastMixin();
     Dsymbol *toParent();
     Dsymbol *toParent2();
+    TemplateInstance *inTemplateInstance();
 
     int dyncast() { return DYNCAST_DSYMBOL; }	// kludge for template.isSymbol()
 
     static Array *arraySyntaxCopy(Array *a);
 
-    virtual char *kind();
+    virtual const char *kind();
     virtual Dsymbol *toAlias();			// resolve real symbol
     virtual int addMember(Scope *sc, ScopeDsymbol *s, int memnum);
     virtual void semantic(Scope *sc);
@@ -150,7 +151,7 @@
     virtual AggregateDeclaration *isThis();	// is a 'this' required to access the member
     virtual ClassDeclaration *isClassMember();	// are we a member of a class?
     virtual int isExport();			// is Dsymbol exported?
-    virtual int isImportedSymbol();			// is Dsymbol imported?
+    virtual int isImportedSymbol();		// is Dsymbol imported?
     virtual int isDeprecated();			// is Dsymbol deprecated?
     virtual LabelDsymbol *isLabel();		// is this a LabelDsymbol?
     virtual AggregateDeclaration *isMember();	// is this symbol a member of an AggregateDeclaration?
@@ -172,7 +173,7 @@
     // Backend
 
     virtual Symbol *toSymbol();			// to backend symbol
-    virtual void toObjFile();			// compile to .obj file
+    virtual void toObjFile(int multiobj);			// compile to .obj file
     virtual int cvMember(unsigned char *p);	// emit cv debug info for member
 
     Symbol *toImport();				// to backend import symbol
@@ -247,7 +248,7 @@
     void defineRef(Dsymbol *s);
     static void multiplyDefined(Loc loc, Dsymbol *s1, Dsymbol *s2);
     Dsymbol *nameCollision(Dsymbol *s);
-    char *kind();
+    const char *kind();
 
     void emitMemberComments(Scope *sc);
 
--- a/dmd/enum.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/enum.c	Sat Jul 12 19:38:31 2008 +0200
@@ -1,5 +1,5 @@
 
-// Copyright (c) 1999-2006 by Digital Mars
+// Copyright (c) 1999-2008 by Digital Mars
 // All Rights Reserved
 // written by Walter Bright
 // http://www.digitalmars.com
@@ -14,6 +14,7 @@
 #include "enum.h"
 #include "mtype.h"
 #include "scope.h"
+#include "declaration.h"
 
 /********************************* EnumDeclaration ****************************/
 
@@ -27,6 +28,7 @@
     minval = 0;
     defaultval = 0;
     sinit = NULL;
+    isdeprecated = 0;
 }
 
 Dsymbol *EnumDeclaration::syntaxCopy(Dsymbol *s)
@@ -57,6 +59,9 @@
 	return;
     if (!memtype)
 	memtype = Type::tint32;
+    if (sc->stc & STCdeprecated)
+	isdeprecated = 1;
+
     parent = sc->scopesym;
     memtype = memtype->semantic(loc, sc);
 
@@ -265,11 +270,16 @@
     return type;
 }
 
-char *EnumDeclaration::kind()
+const char *EnumDeclaration::kind()
 {
     return "enum";
 }
 
+int EnumDeclaration::isDeprecated()
+{
+    return isdeprecated;
+}
+
 /********************************* EnumMember ****************************/
 
 EnumMember::EnumMember(Loc loc, Identifier *id, Expression *value)
@@ -306,7 +316,7 @@
     }
 }
 
-char *EnumMember::kind()
+const char *EnumMember::kind()
 {
     return "enum member";
 }
--- a/dmd/enum.h	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/enum.h	Sat Jul 12 19:38:31 2008 +0200
@@ -1,6 +1,6 @@
 
 // Compiler implementation of the D programming language
-// Copyright (c) 1999-2006 by Digital Mars
+// Copyright (c) 1999-2008 by Digital Mars
 // All Rights Reserved
 // written by Walter Bright
 // http://www.digitalmars.com
@@ -30,9 +30,19 @@
 {
     Type *type;			// the TypeEnum
     Type *memtype;		// type of the members
+
+#if DMDV1
     integer_t maxval;
     integer_t minval;
     integer_t defaultval;	// default initializer
+#else
+    Expression *maxval;
+    Expression *minval;
+    Expression *defaultval;	// default initializer
+
+    Scope *scope;		// !=NULL means context to use
+#endif
+    int isdeprecated;
 
     EnumDeclaration(Loc loc, Identifier *id, Type *memtype);
     Dsymbol *syntaxCopy(Dsymbol *s);
@@ -40,14 +50,18 @@
     int oneMember(Dsymbol **ps);
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
     Type *getType();
-    char *kind();
+    const char *kind();
+#if DMDV2
+    Dsymbol *search(Loc, Identifier *ident, int flags);
+#endif
+    int isDeprecated();			// is Dsymbol deprecated?
 
     void emitComment(Scope *sc);
     void toDocBuffer(OutBuffer *buf);
 
     EnumDeclaration *isEnumDeclaration() { return this; }
 
-    void toObjFile();			// compile to .obj file
+    void toObjFile(int multiobj);			// compile to .obj file
     void toDebug();
     int cvMember(unsigned char *p);
 
@@ -63,7 +77,7 @@
     EnumMember(Loc loc, Identifier *id, Expression *value);
     Dsymbol *syntaxCopy(Dsymbol *s);
     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
-    char *kind();
+    const char *kind();
 
     void emitComment(Scope *sc);
     void toDocBuffer(OutBuffer *buf);
--- a/dmd/expression.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/expression.c	Sat Jul 12 19:38:31 2008 +0200
@@ -111,6 +111,9 @@
     precedence[TOKassert] = PREC_primary;
     precedence[TOKfunction] = PREC_primary;
     precedence[TOKvar] = PREC_primary;
+#if DMDV2
+    precedence[TOKdefault] = PREC_primary;
+#endif
 
     // post
     precedence[TOKdotti] = PREC_primary;
@@ -195,6 +198,89 @@
     precedence[TOKcomma] = PREC_expr;
 }
 
+/*************************************************************
+ * Given var, we need to get the
+ * right 'this' pointer if var is in an outer class, but our
+ * existing 'this' pointer is in an inner class.
+ * Input:
+ *	e1	existing 'this'
+ *	ad	struct or class we need the correct 'this' for
+ *	var	the specific member of ad we're accessing
+ */
+
+Expression *getRightThis(Loc loc, Scope *sc, AggregateDeclaration *ad,
+	Expression *e1, Declaration *var)
+{
+    //printf("\ngetRightThis(e1 = %s, ad = %s, var = %s)\n", e1->toChars(), ad->toChars(), var->toChars());
+ L1:
+    Type *t = e1->type->toBasetype();
+    //printf("e1->type = %s, var->type = %s\n", e1->type->toChars(), var->type->toChars());
+
+    /* If e1 is not the 'this' pointer for ad
+     */
+    if (ad &&
+	!(t->ty == Tpointer && t->nextOf()->ty == Tstruct &&
+	  ((TypeStruct *)t->nextOf())->sym == ad)
+	&&
+	!(t->ty == Tstruct &&
+	  ((TypeStruct *)t)->sym == ad)
+       )
+    {
+	ClassDeclaration *cd = ad->isClassDeclaration();
+	ClassDeclaration *tcd = t->isClassHandle();
+
+	/* e1 is the right this if ad is a base class of e1
+	 */
+	if (!cd || !tcd ||
+	    !(tcd == cd || cd->isBaseOf(tcd, NULL))
+	   )
+	{
+	    /* Only classes can be inner classes with an 'outer'
+	     * member pointing to the enclosing class instance
+	     */
+	    if (tcd && tcd->isNested())
+	    {   /* e1 is the 'this' pointer for an inner class: tcd.
+		 * Rewrite it as the 'this' pointer for the outer class.
+		 */
+
+		e1 = new DotVarExp(loc, e1, tcd->vthis);
+		e1->type = tcd->vthis->type;
+		// Do not call checkNestedRef()
+		//e1 = e1->semantic(sc);
+
+		// Skip up over nested functions, and get the enclosing
+		// class type.
+		int n = 0;
+		Dsymbol *s;
+		for (s = tcd->toParent();
+		     s && s->isFuncDeclaration();
+		     s = s->toParent())
+		{   FuncDeclaration *f = s->isFuncDeclaration();
+		    if (f->vthis)
+		    {
+			//printf("rewriting e1 to %s's this\n", f->toChars());
+			n++;
+			e1 = new VarExp(loc, f->vthis);
+		    }
+		}
+		if (s && s->isClassDeclaration())
+		{   e1->type = s->isClassDeclaration()->type;
+		    if (n > 1)
+			e1 = e1->semantic(sc);
+		}
+		else
+		    e1 = e1->semantic(sc);
+		goto L1;
+	    }
+	    /* Can't find a path from e1 to ad
+	     */
+	    e1->error("this for %s needs to be type %s not type %s",
+		var->toChars(), ad->toChars(), t->toChars());
+	}
+    }
+    return e1;
+}
+
 /*****************************************
  * Determine if 'this' is available.
  * If it is, return the FuncDeclaration that has it.
@@ -383,6 +469,32 @@
     }
 }
 
+/*********************************************
+ * Call copy constructor for struct value argument.
+ */
+#if DMDV2
+Expression *callCpCtor(Loc loc, Scope *sc, Expression *e)
+{
+    Type *tb = e->type->toBasetype();
+    assert(tb->ty == Tstruct);
+    StructDeclaration *sd = ((TypeStruct *)tb)->sym;
+    if (sd->cpctor)
+    {
+	/* Create a variable tmp, and replace the argument e with:
+	 *	(tmp = e),tmp
+	 * and let AssignExp() handle the construction.
+	 * This is not the most efficent, ideally tmp would be constructed
+	 * directly onto the stack.
+	 */
+	Identifier *idtmp = Lexer::uniqueId("__tmp");
+	VarDeclaration *tmp = new VarDeclaration(loc, tb, idtmp, new ExpInitializer(0, e));
+	Expression *ae = new DeclarationExp(loc, tmp);
+	e = new CommaExp(loc, ae, new VarExp(loc, tmp));
+	e = e->semantic(sc);
+    }
+    return e;
+}
+#endif
 
 /****************************************
  * Now that we know the exact type of the function we're calling,
@@ -432,7 +544,15 @@
             error(loc, "expected %"PRIuSIZE" arguments, not %"PRIuSIZE, nparams, nargs);
 		    break;
 		}
-		arg = p->defaultArg->copy();
+		arg = p->defaultArg;
+#if DMDV2
+		if (arg->op == TOKdefault)
+		{   DefaultInitExp *de = (DefaultInitExp *)arg;
+		    arg = de->resolve(loc, sc);
+		}
+		else
+#endif
+		    arg = arg->copy();
 		arguments->push(arg);
 		nargs++;
 	    }
@@ -572,7 +692,7 @@
 	    tb = arg->type->toBasetype();
 	    if (tb->ty == Tsarray)
 	    {	TypeSArray *ts = (TypeSArray *)tb;
-		Type *ta = tb->next->arrayOf();
+		Type *ta = ts->next->arrayOf();
 		if (ts->size(arg->loc) == 0)
 		{   arg = new NullExp(arg->loc);
 		    arg->type = ta;
@@ -580,7 +700,19 @@
 		else
 		    arg = arg->castTo(sc, ta);
 	    }
-
+#if DMDV2
+	    if (tb->ty == Tstruct)
+	    {
+		arg = callCpCtor(loc, sc, arg);
+	    }
+
+	    // Give error for overloaded function addresses
+	    if (arg->op == TOKsymoff)
+	    {	SymOffExp *se = (SymOffExp *)arg;
+		if (se->hasOverloads && !se->var->isFuncDeclaration()->isUnique())
+		    arg->error("function %s is overloaded", arg->toChars());
+	    }
+#endif
 	    arg->rvalue();
 	}
 	arg = arg->optimize(WANTvalue);
@@ -609,6 +741,7 @@
 
 void expToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e, enum PREC pr)
 {
+    //if (precedence[e->op] == 0) e->dump(0);
     if (precedence[e->op] < pr)
     {
 	buf->writeByte('(');
@@ -706,7 +839,7 @@
 Expression *Expression::semantic(Scope *sc)
 {
 #if LOGSEMANTIC
-    printf("Expression::semantic()\n");
+    printf("Expression::semantic() %s\n", toChars());
 #endif
     if (type)
 	type = type->semantic(loc, sc);
@@ -747,6 +880,7 @@
 	dump(0);
 	halt();
 #endif
+	type = Type::tint32;
     }
 }
 
@@ -939,7 +1073,7 @@
 	    e = new NullExp(loc);
 	else
 	    e = new AddrExp(loc, this);
-	e->type = tb->next->pointerTo();
+	e->type = ts->next->pointerTo();
     }
     return e;
 }
@@ -970,7 +1104,7 @@
     {	Expression *e;
 
 	e = new PtrExp(loc, this);
-	e->type = type->next;
+	e->type = ((TypeReference *)type)->next;
 	return e;
     }
     return this;
@@ -994,6 +1128,18 @@
     return FALSE;
 }
 
+/********************************
+ * Can this expression throw an exception?
+ * Valid only after semantic() pass.
+ */
+
+int Expression::canThrow()
+{
+    return TRUE;
+}
+
+
+
 Expressions *Expression::arraySyntaxCopy(Expressions *exps)
 {   Expressions *a = NULL;
 
@@ -1019,6 +1165,7 @@
     //printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type->toChars() : "");
     if (type && !type->isscalar())
     {
+	//printf("%s, loc = %d\n", toChars(), loc.linnum);
 	error("integral constant must be scalar type, not %s", type->toChars());
 	type = Type::terror;
     }
@@ -1093,9 +1240,13 @@
 	    }
 
 	    default:
-		print();
-		type->print();
-		assert(0);
+		/* This can happen if errors, such as
+		 * the type is painted on like in fromConstInitializer().
+		 */
+		if (!global.errors)
+		{   type->print();
+		    assert(0);
+		}
 		break;
 	}
 	break;
@@ -1246,10 +1397,17 @@
 		goto L3;
 
 	    default:
+		/* This can happen if errors, such as
+		 * the type is painted on like in fromConstInitializer().
+		 */
+		if (!global.errors)
+		{
 #ifdef DEBUG
-		t->print();
+		    t->print();
 #endif
-		assert(0);
+		    assert(0);
+		}
+		break;
 	}
     }
     else if (v & 0x8000000000000000LL)
@@ -1993,7 +2151,8 @@
 	fd->nestedFrameRef = 1;
     }
 #endif
-    sc->callSuper |= CSXthis;
+    if (!sc->intypeof)
+	sc->callSuper |= CSXthis;
     return this;
 
 Lerr:
@@ -2102,7 +2261,8 @@
     }
 #endif
 
-    sc->callSuper |= CSXsuper;
+    if (!sc->intypeof)
+	sc->callSuper |= CSXsuper;
     return this;
 
 
@@ -3396,6 +3556,7 @@
 {
 #if LOGSEMANTIC
     printf("NewAnonClassExp::semantic() %s\n", toChars());
+    //printf("thisexp = %p\n", thisexp);
     //printf("type: %s\n", type->toChars());
 #endif
 
@@ -3982,6 +4143,8 @@
 #endif
     typeidType = typeidType->semantic(loc, sc);
     e = typeidType->getTypeInfo(sc);
+    if (e->loc.linnum == 0)
+	e->loc = loc;		// so there's at least some line number info
     return e;
 }
 
@@ -4258,7 +4421,7 @@
 	    buf->writestring(" == ");
 	tspec->toCBuffer(buf, NULL, hgs);
     }
-#if V2
+#if DMDV2
     if (parameters)
     {	// First parameter is already output, so start with second
 	for (int i = 1; i < parameters->dim; i++)
@@ -4830,6 +4993,16 @@
 		return new TypeExp(loc, t);
 	    }
 
+	    TupleDeclaration *tup = s->isTupleDeclaration();
+	    if (tup)
+	    {
+		if (eleft)
+		    error("cannot have e.tuple");
+		e = new TupleExp(loc, tup);
+		e = e->semantic(sc);
+		return e;
+	    }
+
 	    ScopeDsymbol *sds = s->isScopeDsymbol();
 	    if (sds)
 	    {
@@ -4973,55 +5146,19 @@
 	if (!var->isFuncDeclaration())	// for functions, do checks after overload resolution
 	{
 	    AggregateDeclaration *ad = var->toParent()->isAggregateDeclaration();
-	L1:
-	    Type *t = e1->type->toBasetype();
-
-	    if (ad &&
-		!(t->ty == Tpointer && t->next->ty == Tstruct &&
-		  ((TypeStruct *)t->next)->sym == ad)
-		&&
-		!(t->ty == Tstruct &&
-		  ((TypeStruct *)t)->sym == ad)
-	       )
-	    {
-		ClassDeclaration *cd = ad->isClassDeclaration();
-		ClassDeclaration *tcd = t->isClassHandle();
-
-		if (!cd || !tcd ||
-		    !(tcd == cd || cd->isBaseOf(tcd, NULL))
-		   )
-		{
-		    if (tcd && tcd->isNested())
-		    {	// Try again with outer scope
-
-			e1 = new DotVarExp(loc, e1, tcd->vthis);
-			e1 = e1->semantic(sc);
-
-			// Skip over nested functions, and get the enclosing
-			// class type.
-			Dsymbol *s = tcd->toParent();
-			while (s && s->isFuncDeclaration())
-			{   FuncDeclaration *f = s->isFuncDeclaration();
-			    if (f->vthis)
-			    {
-				e1 = new VarExp(loc, f->vthis);
-			    }
-			    s = s->toParent();
-			}
-			if (s && s->isClassDeclaration())
-			    e1->type = s->isClassDeclaration()->type;
-
-			e1 = e1->semantic(sc);
-			goto L1;
-		    }
-#ifdef DEBUG
-		    printf("2: ");
-#endif
-		    error("this for %s needs to be type %s not type %s",
-			var->toChars(), ad->toChars(), t->toChars());
+	    e1 = getRightThis(loc, sc, ad, e1, var);
+	    if (!sc->noaccesscheck)
+		accessCheck(loc, sc, e1, var);
+
+	    VarDeclaration *v = var->isVarDeclaration();
+	    if (v && v->isConst())
+	    {	ExpInitializer *ei = v->getExpInitializer();
+		if (ei)
+		{   Expression *e = ei->exp->copy();
+		    e = e->semantic(sc);
+		    return e;
 		}
 	    }
-	    accessCheck(loc, sc, e1, var);
 	}
     }
     //printf("-DotVarExp::semantic('%s')\n", toChars());
@@ -5211,7 +5348,7 @@
     return e;
 
 Lerr:
-    return new IntegerExp(0);
+    return new IntegerExp(loc, 0, Type::tint32);
 }
 
 void DotTemplateInstanceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
@@ -5239,44 +5376,9 @@
 	e1 = e1->semantic(sc);
 	type = new TypeDelegate(func->type);
 	type = type->semantic(loc, sc);
-//-----------------
-	/* For func, we need to get the
-	 * right 'this' pointer if func is in an outer class, but our
-	 * existing 'this' pointer is in an inner class.
-	 * This code is analogous to that used for variables
-	 * in DotVarExp::semantic().
-	 */
 	AggregateDeclaration *ad = func->toParent()->isAggregateDeclaration();
-    L10:
-	Type *t = e1->type;
-	if (func->needThis() && ad &&
-	    !(t->ty == Tpointer && t->next->ty == Tstruct &&
-	      ((TypeStruct *)t->next)->sym == ad) &&
-	    !(t->ty == Tstruct && ((TypeStruct *)t)->sym == ad)
-	   )
-	{
-	    ClassDeclaration *cd = ad->isClassDeclaration();
-	    ClassDeclaration *tcd = t->isClassHandle();
-
-	    if (!cd || !tcd ||
-		!(tcd == cd || cd->isBaseOf(tcd, NULL))
-	       )
-	    {
-		if (tcd && tcd->isNested())
-		{   // Try again with outer scope
-
-		    e1 = new DotVarExp(loc, e1, tcd->vthis);
-		    e1 = e1->semantic(sc);
-		    goto L10;
-		}
-#ifdef DEBUG
-		printf("3: ");
-#endif
-		error("this for %s needs to be type %s not type %s",
-		    func->toChars(), ad->toChars(), t->toChars());
-	    }
-	}
-//-----------------
+	if (func->needThis())
+	    e1 = getRightThis(loc, sc, ad, e1, func);
     }
     return this;
 }
@@ -5535,7 +5637,7 @@
 	DotTemplateExp *dte;
 	AggregateDeclaration *ad;
 	UnaExp *ue = (UnaExp *)(e1);
-        
+
     	if (e1->op == TOKdotvar)
         {   // Do overload resolution
 	    dve = (DotVarExp *)(e1);
@@ -5560,40 +5662,20 @@
 	    }
 	    ad = td->toParent()->isAggregateDeclaration();
 	}	
-	/* Now that we have the right function f, we need to get the
-	 * right 'this' pointer if f is in an outer class, but our
-	 * existing 'this' pointer is in an inner class.
-	 * This code is analogous to that used for variables
-	 * in DotVarExp::semantic().
+	if (f->needThis())
+	{
+	    ue->e1 = getRightThis(loc, sc, ad, ue->e1, f);
+	}
+
+	/* Cannot call public functions from inside invariant
+	 * (because then the invariant would have infinite recursion)
 	 */
-    L10:
-	Type *t = ue->e1->type->toBasetype();
-	if (f->needThis() && ad &&
-	    !(t->ty == Tpointer && t->next->ty == Tstruct &&
-	      ((TypeStruct *)t->next)->sym == ad) &&
-	    !(t->ty == Tstruct && ((TypeStruct *)t)->sym == ad)
+	if (sc->func && sc->func->isInvariantDeclaration() &&
+	    ue->e1->op == TOKthis &&
+	    f->addPostInvariant()
 	   )
 	{
-	    ClassDeclaration *cd = ad->isClassDeclaration();
-	    ClassDeclaration *tcd = t->isClassHandle();
-
-	    if (!cd || !tcd ||
-		!(tcd == cd || cd->isBaseOf(tcd, NULL))
-	       )
-	    {
-		if (tcd && tcd->isNested())
-		{   // Try again with outer scope
-
-		    ue->e1 = new DotVarExp(loc, ue->e1, tcd->vthis);
-		    ue->e1 = ue->e1->semantic(sc);
-		    goto L10;
-		}
-#ifdef DEBUG
-		printf("1: ");
-#endif
-		error("this for %s needs to be type %s not type %s",
-		    f->toChars(), ad->toChars(), t->toChars());
-	    }
+	    error("cannot call public/export function %s from invariant", f->toChars());
 	}
 
 	checkDeprecated(sc, f);
@@ -5647,6 +5729,44 @@
 	    }
 	    else
 	    {
+		if (!sc->intypeof)
+		{
+#if 0
+		    if (sc->callSuper & (CSXthis | CSXsuper))
+			error("reference to this before super()");
+#endif
+		    if (sc->noctor || sc->callSuper & CSXlabel)
+			error("constructor calls not allowed in loops or after labels");
+		    if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor))
+			error("multiple constructor calls");
+		    sc->callSuper |= CSXany_ctor | CSXsuper_ctor;
+		}
+
+		f = f->overloadResolve(loc, arguments);
+		checkDeprecated(sc, f);
+		e1 = new DotVarExp(e1->loc, e1, f);
+		e1 = e1->semantic(sc);
+		t1 = e1->type;
+	    }
+	}
+    }
+    else if (e1->op == TOKthis)
+    {
+	// same class constructor call
+	ClassDeclaration *cd = NULL;
+
+	if (sc->func)
+	    cd = sc->func->toParent()->isClassDeclaration();
+	if (!cd || !sc->func->isCtorDeclaration())
+	{
+	    error("class constructor call must be in a constructor");
+	    type = Type::terror;
+	    return this;
+	}
+	else
+	{
+	    if (!sc->intypeof)
+	    {
 #if 0
 		if (sc->callSuper & (CSXthis | CSXsuper))
 		    error("reference to this before super()");
@@ -5655,40 +5775,8 @@
 		    error("constructor calls not allowed in loops or after labels");
 		if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor))
 		    error("multiple constructor calls");
-		sc->callSuper |= CSXany_ctor | CSXsuper_ctor;
-
-		f = f->overloadResolve(loc, arguments);
-		checkDeprecated(sc, f);
-		e1 = new DotVarExp(e1->loc, e1, f);
-		e1 = e1->semantic(sc);
-		t1 = e1->type;
+		sc->callSuper |= CSXany_ctor | CSXthis_ctor;
 	    }
-	}
-    }
-    else if (e1->op == TOKthis)
-    {
-	// same class constructor call
-	ClassDeclaration *cd = NULL;
-
-	if (sc->func)
-	    cd = sc->func->toParent()->isClassDeclaration();
-	if (!cd || !sc->func->isCtorDeclaration())
-	{
-	    error("class constructor call must be in a constructor");
-	    type = Type::terror;
-	    return this;
-	}
-	else
-	{
-#if 0
-	    if (sc->callSuper & (CSXthis | CSXsuper))
-		error("reference to this before super()");
-#endif
-	    if (sc->noctor || sc->callSuper & CSXlabel)
-		error("constructor calls not allowed in loops or after labels");
-	    if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor))
-		error("multiple constructor calls");
-	    sc->callSuper |= CSXany_ctor | CSXthis_ctor;
 
 	    f = cd->ctor;
 	    f = f->overloadResolve(loc, arguments);
--- a/dmd/expression.h	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/expression.h	Sat Jul 12 19:38:31 2008 +0200
@@ -1,6 +1,6 @@
 
 // Compiler implementation of the D programming language
-// Copyright (c) 1999-2007 by Digital Mars
+// Copyright (c) 1999-2008 by Digital Mars
 // All Rights Reserved
 // written by Walter Bright
 // http://www.digitalmars.com
@@ -140,6 +140,7 @@
     virtual int isBool(int result);
     virtual int isBit();
     virtual int checkSideEffect(int flag);
+    virtual int canThrow();
 
     virtual int inlineCost(InlineCostState *ics);
     virtual Expression *doInline(InlineDoState *ids);
--- a/dmd/func.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/func.c	Sat Jul 12 19:38:31 2008 +0200
@@ -103,6 +103,7 @@
     StructDeclaration *sd;
     ClassDeclaration *cd;
     InterfaceDeclaration *id;
+    Dsymbol *pd;
 
 #if 0
     printf("FuncDeclaration::semantic(sc = %p, this = %p, '%s', linkage = %d)\n", sc, this, toPrettyChars(), sc->linkage);
@@ -208,6 +209,18 @@
 	    error("function body is not abstract in interface %s", id->toChars());
     }
 
+    /* Template member functions aren't virtual:
+     *   interface TestInterface { void tpl(T)(); }
+     * and so won't work in interfaces
+     */
+    if ((pd = toParent()) != NULL &&
+	pd->isTemplateInstance() &&
+	(pd = toParent2()) != NULL &&
+	(id = pd->isInterfaceDeclaration()) != NULL)
+    {
+	error("template member function not allowed in interface %s", id->toChars());
+    }
+
     cd = parent->isClassDeclaration();
     if (cd)
     {	int vi;
@@ -306,7 +319,7 @@
 		if (fdv->isFinal())
 		    error("cannot override final function %s", fdv->toPrettyChars());
 
-#if V2
+#if DMDV2
 		if (!isOverride() && global.params.warnings)
 		    warning("%s: overrides base class function %s, but is not marked with 'override'", locToChars() fdv->toPrettyChars());
 #endif
@@ -322,7 +335,7 @@
 #if !BREAKABI
 			&& !isDtorDeclaration()
 #endif
-#if V2
+#if DMDV2
 			&& !isPostBlitDeclaration()
 #endif
 			)
@@ -978,7 +991,8 @@
 		f = (TypeFunction *)type;
 	    }
 
-	    int offend = fbody ? fbody->fallOffEnd() : TRUE;
+	    int offend = fbody ? fbody->blockExit() & BEfallthru : TRUE;
+	    //int offend = fbody ? fbody->fallOffEnd() : TRUE;
 
 	    if (isStaticCtorDeclaration())
 	    {	/* It's a static constructor. Ensure that all
@@ -1471,7 +1485,7 @@
 	return 1;
     }
 
-#if V2
+#if DMDV2
     /* Allow covariant matches, if it's just a const conversion
      * of the return type
      */
@@ -1777,6 +1791,7 @@
     }
     return (LabelDsymbol *)s;
 }
+
 /****************************************
  * If non-static member function that has a 'this' pointer,
  * return the aggregate it is a member of.
@@ -2031,17 +2046,18 @@
     return fd;
 }
 
-char *FuncDeclaration::kind()
+const char *FuncDeclaration::kind()
 {
     return "function";
 }
+
 /*******************************
  * Look at all the variables in this function that are referenced
  * by nested functions, and determine if a closure needs to be
  * created for them.
  */
 
-#if V2
+#if DMDV2
 int FuncDeclaration::needsClosure()
 {
     /* Need a closure for all the closureVars[] if any of the
@@ -2051,7 +2067,11 @@
      * 1) is a virtual function
      * 2) has its address taken
      * 3) has a parent that escapes
-     * escapes.
+     *
+     * Note that since a non-virtual function can be called by
+     * a virtual one, if that non-virtual function accesses a closure
+     * var, the closure still has to be taken. Hence, we check for isThis()
+     * instead of isVirtual(). (thanks to David Friedman)
      */
 
     //printf("FuncDeclaration::needsClosure() %s\n", toChars());
@@ -2065,14 +2085,14 @@
 	    assert(f != this);
 
 	    //printf("\t\tf = %s, %d, %d\n", f->toChars(), f->isVirtual(), f->tookAddressOf);
-	    if (f->isVirtual() || f->tookAddressOf)
+	    if (f->isThis() || f->tookAddressOf)
 		goto Lyes;	// assume f escapes this function's scope
 
 	    // Look to see if any parents of f that are below this escape
 	    for (Dsymbol *s = f->parent; s != this; s = s->parent)
 	    {
 		f = s->isFuncDeclaration();
-		if (f && (f->isVirtual() || f->tookAddressOf))
+		if (f && (f->isThis() || f->tookAddressOf))
 		    goto Lyes;
 	    }
 	}
@@ -2097,7 +2117,7 @@
     this->funcalias = funcalias;
 }
 
-char *FuncAliasDeclaration::kind()
+const char *FuncAliasDeclaration::kind()
 {
     return "function alias";
 }
@@ -2142,7 +2162,12 @@
     return (tok == TOKdelegate);
 }
 
-char *FuncLiteralDeclaration::kind()
+int FuncLiteralDeclaration::isVirtual()
+{
+    return FALSE;
+}
+
+const char *FuncLiteralDeclaration::kind()
 {
     // GCC requires the (char*) casts
     return (tok == TOKdelegate) ? (char*)"delegate" : (char*)"function";
@@ -2214,6 +2239,8 @@
     else
 	tret = cd->type; //->referenceTo();
     type = new TypeFunction(arguments, tret, varargs, LINKd);
+    if (!originalType)
+	originalType = type;
 
     sc->flags |= SCOPEctor;
     type = type->semantic(loc, sc);
@@ -2240,7 +2267,7 @@
 	cd->defaultCtor = this;
 }
 
-char *CtorDeclaration::kind()
+const char *CtorDeclaration::kind()
 {
     return "constructor";
 }
@@ -2377,6 +2404,34 @@
 
     type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd);
 
+    /* If the static ctor appears within a template instantiation,
+     * it could get called multiple times by the module constructors
+     * for different modules. Thus, protect it with a gate.
+     */
+    if (inTemplateInstance())
+    {
+	/* Add this prefix to the function:
+	 *	static int gate;
+	 *	if (++gate != 1) return;
+	 * Note that this is not thread safe; should not have threads
+	 * during static construction.
+	 */
+	Identifier *id = Lexer::idPool("__gate");
+	VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL);
+	v->storage_class = STCstatic;
+	Statements *sa = new Statements();
+	Statement *s = new DeclarationStatement(0, v);
+	sa->push(s);
+	Expression *e = new IdentifierExp(0, id);
+	e = new AddAssignExp(0, e, new IntegerExp(1));
+	e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(1));
+	s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL);
+	sa->push(s);
+	if (fbody)
+	    sa->push(fbody);
+	fbody = new CompoundStatement(0, sa);
+    }
+
     FuncDeclaration::semantic(sc);
 
     // We're going to need ModuleInfo
@@ -2432,6 +2487,7 @@
     : FuncDeclaration(loc, endloc,
       Identifier::generateId("_staticDtor"), STCstatic, NULL)
 {
+    vgate = NULL;
 }
 
 Dsymbol *StaticDtorDeclaration::syntaxCopy(Dsymbol *s)
@@ -2455,6 +2511,36 @@
     }
     type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd);
 
+    /* If the static ctor appears within a template instantiation,
+     * it could get called multiple times by the module constructors
+     * for different modules. Thus, protect it with a gate.
+     */
+    if (inTemplateInstance())
+    {
+	/* Add this prefix to the function:
+	 *	static int gate;
+	 *	if (--gate != 0) return;
+	 * Increment gate during constructor execution.
+	 * Note that this is not thread safe; should not have threads
+	 * during static destruction.
+	 */
+	Identifier *id = Lexer::idPool("__gate");
+	VarDeclaration *v = new VarDeclaration(0, Type::tint32, id, NULL);
+	v->storage_class = STCstatic;
+	Statements *sa = new Statements();
+	Statement *s = new DeclarationStatement(0, v);
+	sa->push(s);
+	Expression *e = new IdentifierExp(0, id);
+	e = new AddAssignExp(0, e, new IntegerExp(-1));
+	e = new EqualExp(TOKnotequal, 0, e, new IntegerExp(1));
+	s = new IfStatement(0, NULL, e, new ReturnStatement(0, NULL), NULL);
+	sa->push(s);
+	if (fbody)
+	    sa->push(fbody);
+	fbody = new CompoundStatement(0, sa);
+	vgate = v;
+    }
+
     FuncDeclaration::semantic(sc);
 
     // We're going to need ModuleInfo
@@ -2583,12 +2669,7 @@
 
 static Identifier *unitTestId()
 {
-    static int n;
-    char buffer[10 + sizeof(n)*3 + 1];
-
-    sprintf(buffer,"__unittest%d", n);
-    n++;
-    return Lexer::idPool(buffer);
+    return Lexer::uniqueId("__unittest");
 }
 
 UnitTestDeclaration::UnitTestDeclaration(Loc loc, Loc endloc)
@@ -2713,7 +2794,7 @@
     FuncDeclaration::semantic(sc);
 }
 
-char *NewDeclaration::kind()
+const char *NewDeclaration::kind()
 {
     return "allocator";
 }
@@ -2797,7 +2878,7 @@
     FuncDeclaration::semantic(sc);
 }
 
-char *DeleteDeclaration::kind()
+const char *DeleteDeclaration::kind()
 {
     return "deallocator";
 }
--- a/dmd/id.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/id.c	Sat Jul 12 19:38:31 2008 +0200
@@ -40,6 +40,8 @@
 Identifier *Id::empty;
 Identifier *Id::p;
 Identifier *Id::coverage;
+Identifier *Id::__vptr;
+Identifier *Id::__monitor;
 Identifier *Id::TypeInfo;
 Identifier *Id::TypeInfo_Class;
 Identifier *Id::TypeInfo_Interface;
@@ -213,6 +215,8 @@
     empty = Lexer::idPool("");
     p = Lexer::idPool("p");
     coverage = Lexer::idPool("__coverage");
+    __vptr = Lexer::idPool("__vptr");
+    __monitor = Lexer::idPool("__monitor");
     TypeInfo = Lexer::idPool("TypeInfo");
     TypeInfo_Class = Lexer::idPool("TypeInfo_Class");
     TypeInfo_Interface = Lexer::idPool("TypeInfo_Interface");
--- a/dmd/id.h	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/id.h	Sat Jul 12 19:38:31 2008 +0200
@@ -42,6 +42,8 @@
     static Identifier *empty;
     static Identifier *p;
     static Identifier *coverage;
+    static Identifier *__vptr;
+    static Identifier *__monitor;
     static Identifier *TypeInfo;
     static Identifier *TypeInfo_Class;
     static Identifier *TypeInfo_Interface;
--- a/dmd/idgen.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/idgen.c	Sat Jul 12 19:38:31 2008 +0200
@@ -1,302 +1,304 @@
-
-// 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.
-
-// Program to generate string files in d data structures.
-// Saves much tedious typing, and eliminates typo problems.
-// Generates:
-//	id.h
-//	id.c
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
-#include <assert.h>
-
-struct Msgtable
-{
-	char *ident;	// name to use in DMD source
-	char *name;	// name in D executable
-};
-
-Msgtable msgtable[] =
-{
-    { "IUnknown" },
-    { "Object" },
-    { "object" },
-    { "max" },
-    { "min" },
-    { "This", "this" },
-    { "ctor", "_ctor" },
-    { "dtor", "_dtor" },
-    { "classInvariant", "__invariant" },
-    { "unitTest", "_unitTest" },
-    { "init" },
-    { "size" },
-    { "__sizeof", "sizeof" },
-    { "alignof" },
-    { "mangleof" },
-    { "stringof" },
-    { "tupleof" },
-    { "length" },
-    { "remove" },
-    { "ptr" },
-    { "funcptr" },
-    { "dollar", "__dollar" },
-    { "offset" },
-    { "offsetof" },
-    { "ModuleInfo" },
-    { "ClassInfo" },
-    { "classinfo" },
-    { "typeinfo" },
-    { "outer" },
-    { "Exception" },
-    { "withSym", "__withSym" },
-    { "result", "__result" },
-    { "returnLabel", "__returnLabel" },
-    { "delegate" },
-    { "line" },
-    { "empty", "" },
-    { "p" },
-    { "coverage", "__coverage" },
-
-    { "TypeInfo" },
-    { "TypeInfo_Class" },
-    { "TypeInfo_Interface" },
-    { "TypeInfo_Struct" },
-    { "TypeInfo_Enum" },
-    { "TypeInfo_Typedef" },
-    { "TypeInfo_Pointer" },
-    { "TypeInfo_Array" },
-    { "TypeInfo_StaticArray" },
-    { "TypeInfo_AssociativeArray" },
-    { "TypeInfo_Function" },
-    { "TypeInfo_Delegate" },
-    { "TypeInfo_Tuple" },
-    { "TypeInfo_Const" },
-    { "TypeInfo_Invariant" },
-    { "elements" },
-    { "_arguments_typeinfo" },
-    { "_arguments" },
-    { "_argptr" },
-    { "_match" },
-
-    { "LINE", "__LINE__" },
-    { "FILE", "__FILE__" },
-    { "DATE", "__DATE__" },
-    { "TIME", "__TIME__" },
-    { "TIMESTAMP", "__TIMESTAMP__" },
-    { "VENDOR", "__VENDOR__" },
-    { "VERSIONX", "__VERSION__" },
-
-    { "nan" },
-    { "infinity" },
-    { "dig" },
-    { "epsilon" },
-    { "mant_dig" },
-    { "max_10_exp" },
-    { "max_exp" },
-    { "min_10_exp" },
-    { "min_exp" },
-    { "re" },
-    { "im" },
-
-    { "C" },
-    { "D" },
-    { "Windows" },
-    { "Pascal" },
-    { "System" },
-
-    { "exit" },
-    { "success" },
-    { "failure" },
-
-    { "keys" },
-    { "values" },
-    { "rehash" },
-
-    { "sort" },
-    { "reverse" },
-    { "dup" },
-    { "idup" },
-
-    // For inline assembler
-    { "___out", "out" },
-    { "___in", "in" },
-    { "__int", "int" },
-    { "__dollar", "$" },
-    { "__LOCAL_SIZE" },
-
-    // For operator overloads
-    { "uadd",	 "opPos" },
-    { "neg",     "opNeg" },
-    { "com",     "opCom" },
-    { "add",     "opAdd" },
-    { "add_r",   "opAdd_r" },
-    { "sub",     "opSub" },
-    { "sub_r",   "opSub_r" },
-    { "mul",     "opMul" },
-    { "mul_r",   "opMul_r" },
-    { "div",     "opDiv" },
-    { "div_r",   "opDiv_r" },
-    { "mod",     "opMod" },
-    { "mod_r",   "opMod_r" },
-    { "eq",      "opEquals" },
-    { "cmp",     "opCmp" },
-    { "iand",    "opAnd" },
-    { "iand_r",  "opAnd_r" },
-    { "ior",     "opOr" },
-    { "ior_r",   "opOr_r" },
-    { "ixor",    "opXor" },
-    { "ixor_r",  "opXor_r" },
-    { "shl",     "opShl" },
-    { "shl_r",   "opShl_r" },
-    { "shr",     "opShr" },
-    { "shr_r",   "opShr_r" },
-    { "ushr",    "opUShr" },
-    { "ushr_r",  "opUShr_r" },
-    { "cat",     "opCat" },
-    { "cat_r",   "opCat_r" },
-    { "assign",  "opAssign" },
-    { "addass",  "opAddAssign" },
-    { "subass",  "opSubAssign" },
-    { "mulass",  "opMulAssign" },
-    { "divass",  "opDivAssign" },
-    { "modass",  "opModAssign" },
-    { "andass",  "opAndAssign" },
-    { "orass",   "opOrAssign" },
-    { "xorass",  "opXorAssign" },
-    { "shlass",  "opShlAssign" },
-    { "shrass",  "opShrAssign" },
-    { "ushrass", "opUShrAssign" },
-    { "catass",  "opCatAssign" },
-    { "postinc", "opPostInc" },
-    { "postdec", "opPostDec" },
-    { "index",	 "opIndex" },
-    { "indexass", "opIndexAssign" },
-    { "slice",	 "opSlice" },
-    { "sliceass", "opSliceAssign" },
-    { "call",	 "opCall" },
-    { "cast",	 "opCast" },
-    { "match",	 "opMatch" },
-    { "next",	 "opNext" },
-    { "opIn" },
-    { "opIn_r" },
-
-    { "classNew", "new" },
-    { "classDelete", "delete" },
-
-    // For foreach
-    { "apply", "opApply" },
-    { "applyReverse", "opApplyReverse" },
-
-    { "adDup", "_adDupT" },
-    { "adReverse", "_adReverse" },
-
-    // For internal functions
-    { "aaLen", "_aaLen" },
-    { "aaKeys", "_aaKeys" },
-    { "aaValues", "_aaValues" },
-    { "aaRehash", "_aaRehash" },
-
-    // For pragma's
-    { "lib" },
-    { "msg" },
-    { "GNU_asm" },
-    { "LLVM_intrinsic" },
-    { "LLVM_internal" },
-
-    // For toHash/toString
-    { "tohash", "toHash" },
-    { "tostring", "toString" },
-
-    // Special functions
-    { "alloca" },
-    { "main" },
-    { "WinMain" },
-    { "DllMain" },
-};
-
-
-int main()
-{
-    FILE *fp;
-    unsigned i;
-
-    {
-	fp = fopen("id.h","w");
-	if (!fp)
-	{   printf("can't open id.h\n");
-	    exit(EXIT_FAILURE);
-	}
-
-	fprintf(fp, "// File generated by idgen.c\n");
-#if __DMC__
-	fprintf(fp, "#pragma once\n");
-#endif
-	fprintf(fp, "#ifndef DMD_ID_H\n");
-	fprintf(fp, "#define DMD_ID_H 1\n");
-	fprintf(fp, "struct Identifier;\n");
-	fprintf(fp, "struct Id\n");
-	fprintf(fp, "{\n");
-
-	for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
-	{   char *id = msgtable[i].ident;
-
-	    fprintf(fp,"    static Identifier *%s;\n", id);
-	}
-
-	fprintf(fp, "    static void initialize();\n");
-	fprintf(fp, "};\n");
-	fprintf(fp, "#endif\n");
-
-	fclose(fp);
-    }
-
-    {
-	fp = fopen("id.c","w");
-	if (!fp)
-	{   printf("can't open id.c\n");
-	    exit(EXIT_FAILURE);
-	}
-
-	fprintf(fp, "// File generated by idgen.c\n");
-	fprintf(fp, "#include \"id.h\"\n");
-	fprintf(fp, "#include \"identifier.h\"\n");
-	fprintf(fp, "#include \"lexer.h\"\n");
-
-	for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
-	{   char *id = msgtable[i].ident;
-	    char *p = msgtable[i].name;
-
-	    if (!p)
-		p = id;
-	    fprintf(fp,"Identifier *Id::%s;\n", id);
-	}
-
-	fprintf(fp, "void Id::initialize()\n");
-	fprintf(fp, "{\n");
-
-	for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
-	{   char *id = msgtable[i].ident;
-	    char *p = msgtable[i].name;
-
-	    if (!p)
-		p = id;
-	    fprintf(fp,"    %s = Lexer::idPool(\"%s\");\n", id, p);
-	}
-
-	fprintf(fp, "}\n");
-
-	fclose(fp);
-    }
-
-    return EXIT_SUCCESS;
-}
+
+// 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.
+
+// Program to generate string files in d data structures.
+// Saves much tedious typing, and eliminates typo problems.
+// Generates:
+//	id.h
+//	id.c
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <assert.h>
+
+struct Msgtable
+{
+	char *ident;	// name to use in DMD source
+	char *name;	// name in D executable
+};
+
+Msgtable msgtable[] =
+{
+    { "IUnknown" },
+    { "Object" },
+    { "object" },
+    { "max" },
+    { "min" },
+    { "This", "this" },
+    { "ctor", "_ctor" },
+    { "dtor", "_dtor" },
+    { "classInvariant", "__invariant" },
+    { "unitTest", "_unitTest" },
+    { "init" },
+    { "size" },
+    { "__sizeof", "sizeof" },
+    { "alignof" },
+    { "mangleof" },
+    { "stringof" },
+    { "tupleof" },
+    { "length" },
+    { "remove" },
+    { "ptr" },
+    { "funcptr" },
+    { "dollar", "__dollar" },
+    { "offset" },
+    { "offsetof" },
+    { "ModuleInfo" },
+    { "ClassInfo" },
+    { "classinfo" },
+    { "typeinfo" },
+    { "outer" },
+    { "Exception" },
+    { "withSym", "__withSym" },
+    { "result", "__result" },
+    { "returnLabel", "__returnLabel" },
+    { "delegate" },
+    { "line" },
+    { "empty", "" },
+    { "p" },
+    { "coverage", "__coverage" },
+    { "__vptr" },
+    { "__monitor" },
+
+    { "TypeInfo" },
+    { "TypeInfo_Class" },
+    { "TypeInfo_Interface" },
+    { "TypeInfo_Struct" },
+    { "TypeInfo_Enum" },
+    { "TypeInfo_Typedef" },
+    { "TypeInfo_Pointer" },
+    { "TypeInfo_Array" },
+    { "TypeInfo_StaticArray" },
+    { "TypeInfo_AssociativeArray" },
+    { "TypeInfo_Function" },
+    { "TypeInfo_Delegate" },
+    { "TypeInfo_Tuple" },
+    { "TypeInfo_Const" },
+    { "TypeInfo_Invariant" },
+    { "elements" },
+    { "_arguments_typeinfo" },
+    { "_arguments" },
+    { "_argptr" },
+    { "_match" },
+
+    { "LINE", "__LINE__" },
+    { "FILE", "__FILE__" },
+    { "DATE", "__DATE__" },
+    { "TIME", "__TIME__" },
+    { "TIMESTAMP", "__TIMESTAMP__" },
+    { "VENDOR", "__VENDOR__" },
+    { "VERSIONX", "__VERSION__" },
+
+    { "nan" },
+    { "infinity" },
+    { "dig" },
+    { "epsilon" },
+    { "mant_dig" },
+    { "max_10_exp" },
+    { "max_exp" },
+    { "min_10_exp" },
+    { "min_exp" },
+    { "re" },
+    { "im" },
+
+    { "C" },
+    { "D" },
+    { "Windows" },
+    { "Pascal" },
+    { "System" },
+
+    { "exit" },
+    { "success" },
+    { "failure" },
+
+    { "keys" },
+    { "values" },
+    { "rehash" },
+
+    { "sort" },
+    { "reverse" },
+    { "dup" },
+    { "idup" },
+
+    // For inline assembler
+    { "___out", "out" },
+    { "___in", "in" },
+    { "__int", "int" },
+    { "__dollar", "$" },
+    { "__LOCAL_SIZE" },
+
+    // For operator overloads
+    { "uadd",	 "opPos" },
+    { "neg",     "opNeg" },
+    { "com",     "opCom" },
+    { "add",     "opAdd" },
+    { "add_r",   "opAdd_r" },
+    { "sub",     "opSub" },
+    { "sub_r",   "opSub_r" },
+    { "mul",     "opMul" },
+    { "mul_r",   "opMul_r" },
+    { "div",     "opDiv" },
+    { "div_r",   "opDiv_r" },
+    { "mod",     "opMod" },
+    { "mod_r",   "opMod_r" },
+    { "eq",      "opEquals" },
+    { "cmp",     "opCmp" },
+    { "iand",    "opAnd" },
+    { "iand_r",  "opAnd_r" },
+    { "ior",     "opOr" },
+    { "ior_r",   "opOr_r" },
+    { "ixor",    "opXor" },
+    { "ixor_r",  "opXor_r" },
+    { "shl",     "opShl" },
+    { "shl_r",   "opShl_r" },
+    { "shr",     "opShr" },
+    { "shr_r",   "opShr_r" },
+    { "ushr",    "opUShr" },
+    { "ushr_r",  "opUShr_r" },
+    { "cat",     "opCat" },
+    { "cat_r",   "opCat_r" },
+    { "assign",  "opAssign" },
+    { "addass",  "opAddAssign" },
+    { "subass",  "opSubAssign" },
+    { "mulass",  "opMulAssign" },
+    { "divass",  "opDivAssign" },
+    { "modass",  "opModAssign" },
+    { "andass",  "opAndAssign" },
+    { "orass",   "opOrAssign" },
+    { "xorass",  "opXorAssign" },
+    { "shlass",  "opShlAssign" },
+    { "shrass",  "opShrAssign" },
+    { "ushrass", "opUShrAssign" },
+    { "catass",  "opCatAssign" },
+    { "postinc", "opPostInc" },
+    { "postdec", "opPostDec" },
+    { "index",	 "opIndex" },
+    { "indexass", "opIndexAssign" },
+    { "slice",	 "opSlice" },
+    { "sliceass", "opSliceAssign" },
+    { "call",	 "opCall" },
+    { "cast",	 "opCast" },
+    { "match",	 "opMatch" },
+    { "next",	 "opNext" },
+    { "opIn" },
+    { "opIn_r" },
+
+    { "classNew", "new" },
+    { "classDelete", "delete" },
+
+    // For foreach
+    { "apply", "opApply" },
+    { "applyReverse", "opApplyReverse" },
+
+    { "adDup", "_adDupT" },
+    { "adReverse", "_adReverse" },
+
+    // For internal functions
+    { "aaLen", "_aaLen" },
+    { "aaKeys", "_aaKeys" },
+    { "aaValues", "_aaValues" },
+    { "aaRehash", "_aaRehash" },
+
+    // For pragma's
+    { "lib" },
+    { "msg" },
+    { "GNU_asm" },
+    { "LLVM_intrinsic" },
+    { "LLVM_internal" },
+
+    // For toHash/toString
+    { "tohash", "toHash" },
+    { "tostring", "toString" },
+
+    // Special functions
+    { "alloca" },
+    { "main" },
+    { "WinMain" },
+    { "DllMain" },
+};
+
+
+int main()
+{
+    FILE *fp;
+    unsigned i;
+
+    {
+	fp = fopen("id.h","w");
+	if (!fp)
+	{   printf("can't open id.h\n");
+	    exit(EXIT_FAILURE);
+	}
+
+	fprintf(fp, "// File generated by idgen.c\n");
+#if __DMC__
+	fprintf(fp, "#pragma once\n");
+#endif
+	fprintf(fp, "#ifndef DMD_ID_H\n");
+	fprintf(fp, "#define DMD_ID_H 1\n");
+	fprintf(fp, "struct Identifier;\n");
+	fprintf(fp, "struct Id\n");
+	fprintf(fp, "{\n");
+
+	for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
+	{   char *id = msgtable[i].ident;
+
+	    fprintf(fp,"    static Identifier *%s;\n", id);
+	}
+
+	fprintf(fp, "    static void initialize();\n");
+	fprintf(fp, "};\n");
+	fprintf(fp, "#endif\n");
+
+	fclose(fp);
+    }
+
+    {
+	fp = fopen("id.c","w");
+	if (!fp)
+	{   printf("can't open id.c\n");
+	    exit(EXIT_FAILURE);
+	}
+
+	fprintf(fp, "// File generated by idgen.c\n");
+	fprintf(fp, "#include \"id.h\"\n");
+	fprintf(fp, "#include \"identifier.h\"\n");
+	fprintf(fp, "#include \"lexer.h\"\n");
+
+	for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
+	{   char *id = msgtable[i].ident;
+	    char *p = msgtable[i].name;
+
+	    if (!p)
+		p = id;
+	    fprintf(fp,"Identifier *Id::%s;\n", id);
+	}
+
+	fprintf(fp, "void Id::initialize()\n");
+	fprintf(fp, "{\n");
+
+	for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
+	{   char *id = msgtable[i].ident;
+	    char *p = msgtable[i].name;
+
+	    if (!p)
+		p = id;
+	    fprintf(fp,"    %s = Lexer::idPool(\"%s\");\n", id, p);
+	}
+
+	fprintf(fp, "}\n");
+
+	fclose(fp);
+    }
+
+    return EXIT_SUCCESS;
+}
--- a/dmd/import.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/import.c	Sat Jul 12 19:38:31 2008 +0200
@@ -55,7 +55,7 @@
     aliases.push(alias);
 }
 
-char *Import::kind()
+const char *Import::kind()
 {
     return isstatic ? (char *)"static import" : (char *)"import";
 }
--- a/dmd/import.h	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/import.h	Sat Jul 12 19:38:31 2008 +0200
@@ -48,7 +48,7 @@
 	int isstatic);
     void addAlias(Identifier *name, Identifier *alias);
 
-    char *kind();
+    const char *kind();
     Dsymbol *syntaxCopy(Dsymbol *s);	// copy only syntax trees
     void load(Scope *sc);
     void semantic(Scope *sc);
--- a/dmd/init.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/init.c	Sat Jul 12 19:38:31 2008 +0200
@@ -1,584 +1,586 @@
-
-// 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.
-
-#include <stdio.h>
-#include <assert.h>
-
-#include "mars.h"
-#include "init.h"
-#include "expression.h"
-#include "statement.h"
-#include "identifier.h"
-#include "declaration.h"
-#include "aggregate.h"
-#include "scope.h"
-#include "mtype.h"
-#include "hdrgen.h"
-
-/********************************** Initializer *******************************/
-
-Initializer::Initializer(Loc loc)
-{
-    this->loc = loc;
-}
-
-Initializer *Initializer::syntaxCopy()
-{
-    return this;
-}
-
-Initializer *Initializer::semantic(Scope *sc, Type *t)
-{
-    return this;
-}
-
-Type *Initializer::inferType(Scope *sc)
-{
-    error(loc, "cannot infer type from initializer");
-    return Type::terror;
-}
-
-Initializers *Initializer::arraySyntaxCopy(Initializers *ai)
-{   Initializers *a = NULL;
-
-    if (ai)
-    {
-	a = new Initializers();
-	a->setDim(ai->dim);
-	for (int i = 0; i < a->dim; i++)
-	{   Initializer *e = (Initializer *)ai->data[i];
-
-	    e = e->syntaxCopy();
-	    a->data[i] = e;
-	}
-    }
-    return a;
-}
-
-char *Initializer::toChars()
-{   OutBuffer *buf;
-    HdrGenState hgs;
-
-    memset(&hgs, 0, sizeof(hgs));
-    buf = new OutBuffer();
-    toCBuffer(buf, &hgs);
-    return buf->toChars();
-}
-
-/********************************** VoidInitializer ***************************/
-
-VoidInitializer::VoidInitializer(Loc loc)
-    : Initializer(loc)
-{
-    type = NULL;
-}
-
-
-Initializer *VoidInitializer::syntaxCopy()
-{
-    return new VoidInitializer(loc);
-}
-
-
-Initializer *VoidInitializer::semantic(Scope *sc, Type *t)
-{
-    //printf("VoidInitializer::semantic(t = %p)\n", t);
-    type = t;
-    return this;
-}
-
-
-Expression *VoidInitializer::toExpression()
-{
-    error(loc, "void initializer has no value");
-    return new IntegerExp(0);
-}
-
-
-void VoidInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    buf->writestring("void");
-}
-
-
-/********************************** StructInitializer *************************/
-
-StructInitializer::StructInitializer(Loc loc)
-    : Initializer(loc)
-{
-    ad = NULL;
-}
-
-Initializer *StructInitializer::syntaxCopy()
-{
-    StructInitializer *ai = new StructInitializer(loc);
-
-    assert(field.dim == value.dim);
-    ai->field.setDim(field.dim);
-    ai->value.setDim(value.dim);
-    for (int i = 0; i < field.dim; i++)
-    {    
-	ai->field.data[i] = field.data[i];
-
-	Initializer *init = (Initializer *)value.data[i];
-	init = init->syntaxCopy();
-	ai->value.data[i] = init;
-    }
-    return ai;
-}
-
-void StructInitializer::addInit(Identifier *field, Initializer *value)
-{
-    //printf("StructInitializer::addInit(field = %p, value = %p)\n", field, value);
-    this->field.push(field);
-    this->value.push(value);
-}
-
-Initializer *StructInitializer::semantic(Scope *sc, Type *t)
-{
-    TypeStruct *ts;
-    int errors = 0;
-
-    //printf("StructInitializer::semantic(t = %s) %s\n", t->toChars(), toChars());
-    vars.setDim(field.dim);
-    t = t->toBasetype();
-    if (t->ty == Tstruct)
-    {	unsigned i;
-	unsigned fieldi = 0;
-
-	ts = (TypeStruct *)t;
-	ad = ts->sym;
-	for (i = 0; i < field.dim; i++)
-	{
-	    Identifier *id = (Identifier *)field.data[i];
-	    Initializer *val = (Initializer *)value.data[i];
-	    Dsymbol *s;
-	    VarDeclaration *v;
-
-	    if (id == NULL)
-	    {
-		if (fieldi >= ad->fields.dim)
-		{   error(loc, "too many initializers for %s", ad->toChars());
-		    continue;
-		}
-		else
-		{
-		    s = (Dsymbol *)ad->fields.data[fieldi];
-		}
-	    }
-	    else
-	    {
-		//s = ad->symtab->lookup(id);
-		s = ad->search(loc, id, 0);
-		if (!s)
-		{
-		    error(loc, "'%s' is not a member of '%s'", id->toChars(), t->toChars());
-		    continue;
-		}
-
-		// Find out which field index it is
-		for (fieldi = 0; 1; fieldi++)
-		{
-		    if (fieldi >= ad->fields.dim)
-		    {
-			s->error("is not a per-instance initializable field");
-			break;
-		    }
-		    if (s == (Dsymbol *)ad->fields.data[fieldi])
-			break;
-		}
-	    }
-	    if (s && (v = s->isVarDeclaration()) != NULL)
-	    {
-		val = val->semantic(sc, v->type);
-		value.data[i] = (void *)val;
-		vars.data[i] = (void *)v;
-	    }
-	    else
-	    {	error(loc, "%s is not a field of %s", id ? id->toChars() : s->toChars(), ad->toChars());
-		errors = 1;
-	    }
-	    fieldi++;
-	}
-    }
-    else if (t->ty == Tdelegate && value.dim == 0)
-    {	/* Rewrite as empty delegate literal { }
-	 */
-	Arguments *arguments = new Arguments;
-	Type *tf = new TypeFunction(arguments, NULL, 0, LINKd);
-	FuncLiteralDeclaration *fd = new FuncLiteralDeclaration(loc, 0, tf, TOKdelegate, NULL);
-	fd->fbody = new CompoundStatement(loc, new Statements());
-	fd->endloc = loc;
-	Expression *e = new FuncExp(loc, fd);
-	ExpInitializer *ie = new ExpInitializer(loc, e);
-	return ie->semantic(sc, t);
-    }
-    else
-    {
-	error(loc, "a struct is not a valid initializer for a %s", t->toChars());
-	errors = 1;
-    }
-    if (errors)
-    {
-	field.setDim(0);
-	value.setDim(0);
-	vars.setDim(0);
-    }
-    return this;
-}
-
-
-/***************************************
- * This works by transforming a struct initializer into
- * a struct literal. In the future, the two should be the
- * same thing.
- */
-Expression *StructInitializer::toExpression()
-{   Expression *e;
-
-    //printf("StructInitializer::toExpression() %s\n", toChars());
-    if (!ad)				// if fwd referenced
-    {
-	return NULL;
-    }
-    StructDeclaration *sd = ad->isStructDeclaration();
-    if (!sd)
-	return NULL;
-    Expressions *elements = new Expressions();
-    for (size_t i = 0; i < value.dim; i++)
-    {
-	if (field.data[i])
-	    goto Lno;
-	Initializer *iz = (Initializer *)value.data[i];
-	if (!iz)
-	    goto Lno;
-	Expression *ex = iz->toExpression();
-	if (!ex)
-	    goto Lno;
-	elements->push(ex);
-    }
-    e = new StructLiteralExp(loc, sd, elements);
-    e->type = sd->type;
-    return e;
-
-Lno:
-    delete elements;
-    //error(loc, "struct initializers as expressions are not allowed");
-    return NULL;
-}
-
-
-void StructInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    //printf("StructInitializer::toCBuffer()\n");
-    buf->writebyte('{');
-    for (int i = 0; i < field.dim; i++)
-    {
-        if (i > 0)
-	    buf->writebyte(',');
-        Identifier *id = (Identifier *)field.data[i];
-        if (id)
-        {
-            buf->writestring(id->toChars());
-            buf->writebyte(':');
-        }
-        Initializer *iz = (Initializer *)value.data[i];
-        if (iz)
-            iz->toCBuffer(buf, hgs);
-    }
-    buf->writebyte('}');
-}
-
-/********************************** ArrayInitializer ************************************/
-
-ArrayInitializer::ArrayInitializer(Loc loc)
-    : Initializer(loc)
-{
-    dim = 0;
-    type = NULL;
-    sem = 0;
-}
-
-Initializer *ArrayInitializer::syntaxCopy()
-{
-    //printf("ArrayInitializer::syntaxCopy()\n");
-
-    ArrayInitializer *ai = new ArrayInitializer(loc);
-
-    assert(index.dim == value.dim);
-    ai->index.setDim(index.dim);
-    ai->value.setDim(value.dim);
-    for (int i = 0; i < ai->value.dim; i++)
-    {	Expression *e = (Expression *)index.data[i];
-	if (e)
-	    e = e->syntaxCopy();
-	ai->index.data[i] = e;
-
-	Initializer *init = (Initializer *)value.data[i];
-	init = init->syntaxCopy();
-	ai->value.data[i] = init;
-    }
-    return ai;
-}
-
-void ArrayInitializer::addInit(Expression *index, Initializer *value)
-{
-    this->index.push(index);
-    this->value.push(value);
-    dim = 0;
-    type = NULL;
-}
-
-Initializer *ArrayInitializer::semantic(Scope *sc, Type *t)
-{   unsigned i;
-    unsigned length;
-
-    //printf("ArrayInitializer::semantic(%s)\n", t->toChars());
-    if (sem)				// if semantic() already run
-	return this;
-    sem = 1;
-    type = t;
-    t = t->toBasetype();
-    switch (t->ty)
-    {
-	case Tpointer:
-	case Tsarray:
-	case Tarray:
-	    break;
-
-	default:
-	    error(loc, "cannot use array to initialize %s", type->toChars());
-	    return this;
-    }
-
-    length = 0;
-    for (i = 0; i < index.dim; i++)
-    {	Expression *idx;
-	Initializer *val;
-
-	idx = (Expression *)index.data[i];
-	if (idx)
-	{   idx = idx->semantic(sc);
-	    idx = idx->optimize(WANTvalue | WANTinterpret);
-	    index.data[i] = (void *)idx;
-	    length = idx->toInteger();
-	}
-
-	val = (Initializer *)value.data[i];
-	val = val->semantic(sc, t->next);
-	value.data[i] = (void *)val;
-	length++;
-	if (length == 0)
-	    error("array dimension overflow");
-	if (length > dim)
-	    dim = length;
-    }
-    unsigned long amax = 0x80000000;
-    if ((unsigned long) dim * t->next->size() >= amax)
-	error(loc, "array dimension %u exceeds max of %llu", dim, amax / t->next->size());
-    return this;
-}
-
-/********************************
- * If possible, convert array initializer to array literal.
- */
-
-Expression *ArrayInitializer::toExpression()
-{   Expressions *elements;
-    Expression *e;
-
-    //printf("ArrayInitializer::toExpression()\n");
-    //static int i; if (++i == 2) halt();
-    elements = new Expressions();
-    for (size_t i = 0; i < value.dim; i++)
-    {
-	if (index.data[i])
-	    goto Lno;
-	Initializer *iz = (Initializer *)value.data[i];
-	if (!iz)
-	    goto Lno;
-	Expression *ex = iz->toExpression();
-	if (!ex)
-	    goto Lno;
-	elements->push(ex);
-    }
-    e = new ArrayLiteralExp(loc, elements);
-    e->type = type;
-    return e;
-
-Lno:
-    delete elements;
-    error(loc, "array initializers as expressions are not allowed");
-    return NULL;
-}
-
-
-/********************************
- * If possible, convert array initializer to associative array initializer.
- */
-
-Initializer *ArrayInitializer::toAssocArrayInitializer()
-{   Expressions *keys;
-    Expressions *values;
-    Expression *e;
-
-    //printf("ArrayInitializer::toAssocArrayInitializer()\n");
-    //static int i; if (++i == 2) halt();
-    keys = new Expressions();
-    keys->setDim(value.dim);
-    values = new Expressions();
-    values->setDim(value.dim);
-
-    for (size_t i = 0; i < value.dim; i++)
-    {
-	e = (Expression *)index.data[i];
-	if (!e)
-	    goto Lno;
-	keys->data[i] = (void *)e;
-
-	Initializer *iz = (Initializer *)value.data[i];
-	if (!iz)
-	    goto Lno;
-	e = iz->toExpression();
-	if (!e)
-	    goto Lno;
-	values->data[i] = (void *)e;
-    }
-    e = new AssocArrayLiteralExp(loc, keys, values);
-    return new ExpInitializer(loc, e);
-
-Lno:
-    delete keys;
-    delete values;
-    error(loc, "not an associative array initializer");
-    return this;
-}
-
-
-Type *ArrayInitializer::inferType(Scope *sc)
-{
-    for (size_t i = 0; i < value.dim; i++)
-    {
-	if (index.data[i])
-	    goto Lno;
-    }
-    if (value.dim)
-    {
-	Initializer *iz = (Initializer *)value.data[0];
-	if (iz)
-	{   Type *t = iz->inferType(sc);
-	    t = new TypeSArray(t, new IntegerExp(value.dim));
-	    t = t->semantic(loc, sc);
-	    return t;
-	}
-    }
-
-Lno:
-    error(loc, "cannot infer type from this array initializer");
-    return Type::terror;
-}
-
-
-void ArrayInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    buf->writebyte('[');
-    for (int i = 0; i < index.dim; i++)
-    {
-        if (i > 0)
-	    buf->writebyte(',');
-        Expression *ex = (Expression *)index.data[i];
-        if (ex)
-        {
-            ex->toCBuffer(buf, hgs);
-            buf->writebyte(':');
-        }
-        Initializer *iz = (Initializer *)value.data[i];
-        if (iz)
-            iz->toCBuffer(buf, hgs);
-    }
-    buf->writebyte(']');
-}
-
-
-/********************************** ExpInitializer ************************************/
-
-ExpInitializer::ExpInitializer(Loc loc, Expression *exp)
-    : Initializer(loc)
-{
-    this->exp = exp;
-}
-
-Initializer *ExpInitializer::syntaxCopy()
-{
-    return new ExpInitializer(loc, exp->syntaxCopy());
-}
-
-Initializer *ExpInitializer::semantic(Scope *sc, Type *t)
-{
-    //printf("ExpInitializer::semantic(%s), type = %s\n", exp->toChars(), t->toChars());
-    exp = exp->semantic(sc);
-    Type *tb = t->toBasetype();
-
-    /* Look for case of initializing a static array with a too-short
-     * string literal, such as:
-     *	char[5] foo = "abc";
-     * Allow this by doing an explicit cast, which will lengthen the string
-     * literal.
-     */
-    if (exp->op == TOKstring && tb->ty == Tsarray && exp->type->ty == Tsarray)
-    {	StringExp *se = (StringExp *)exp;
-
-	if (!se->committed && se->type->ty == Tsarray &&
-	    ((TypeSArray *)se->type)->dim->toInteger() <
-	    ((TypeSArray *)t)->dim->toInteger())
-	{
-	    exp = se->castTo(sc, t);
-	    goto L1;
-	}
-    }
-
-    // Look for the case of statically initializing an array
-    // with a single member.
-    if (tb->ty == Tsarray &&
-	!tb->next->equals(exp->type->toBasetype()->next) &&
-	exp->implicitConvTo(tb->next)
-       )
-    {
-	t = tb->next;
-    }
-
-    exp = exp->implicitCastTo(sc, t);
-L1:
-    exp = exp->optimize(WANTvalue | WANTinterpret);
-    //printf("-ExpInitializer::semantic(): "); exp->print();
-    return this;
-}
-
-Type *ExpInitializer::inferType(Scope *sc)
-{
-    //printf("ExpInitializer::inferType() %s\n", toChars());
-    exp = exp->semantic(sc);
-    exp = resolveProperties(sc, exp);
-    return exp->type;
-}
-
-Expression *ExpInitializer::toExpression()
-{
-    return exp;
-}
-
-
-void ExpInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
-{
-    exp->toCBuffer(buf, hgs);
-}
-
-
-
+
+// 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 "mars.h"
+#include "init.h"
+#include "expression.h"
+#include "statement.h"
+#include "identifier.h"
+#include "declaration.h"
+#include "aggregate.h"
+#include "scope.h"
+#include "mtype.h"
+#include "hdrgen.h"
+
+/********************************** Initializer *******************************/
+
+Initializer::Initializer(Loc loc)
+{
+    this->loc = loc;
+}
+
+Initializer *Initializer::syntaxCopy()
+{
+    return this;
+}
+
+Initializer *Initializer::semantic(Scope *sc, Type *t)
+{
+    return this;
+}
+
+Type *Initializer::inferType(Scope *sc)
+{
+    error(loc, "cannot infer type from initializer");
+    return Type::terror;
+}
+
+Initializers *Initializer::arraySyntaxCopy(Initializers *ai)
+{   Initializers *a = NULL;
+
+    if (ai)
+    {
+	a = new Initializers();
+	a->setDim(ai->dim);
+	for (int i = 0; i < a->dim; i++)
+	{   Initializer *e = (Initializer *)ai->data[i];
+
+	    e = e->syntaxCopy();
+	    a->data[i] = e;
+	}
+    }
+    return a;
+}
+
+char *Initializer::toChars()
+{   OutBuffer *buf;
+    HdrGenState hgs;
+
+    memset(&hgs, 0, sizeof(hgs));
+    buf = new OutBuffer();
+    toCBuffer(buf, &hgs);
+    return buf->toChars();
+}
+
+/********************************** VoidInitializer ***************************/
+
+VoidInitializer::VoidInitializer(Loc loc)
+    : Initializer(loc)
+{
+    type = NULL;
+}
+
+
+Initializer *VoidInitializer::syntaxCopy()
+{
+    return new VoidInitializer(loc);
+}
+
+
+Initializer *VoidInitializer::semantic(Scope *sc, Type *t)
+{
+    //printf("VoidInitializer::semantic(t = %p)\n", t);
+    type = t;
+    return this;
+}
+
+
+Expression *VoidInitializer::toExpression()
+{
+    error(loc, "void initializer has no value");
+    return new IntegerExp(0);
+}
+
+
+void VoidInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    buf->writestring("void");
+}
+
+
+/********************************** StructInitializer *************************/
+
+StructInitializer::StructInitializer(Loc loc)
+    : Initializer(loc)
+{
+    ad = NULL;
+}
+
+Initializer *StructInitializer::syntaxCopy()
+{
+    StructInitializer *ai = new StructInitializer(loc);
+
+    assert(field.dim == value.dim);
+    ai->field.setDim(field.dim);
+    ai->value.setDim(value.dim);
+    for (int i = 0; i < field.dim; i++)
+    {    
+	ai->field.data[i] = field.data[i];
+
+	Initializer *init = (Initializer *)value.data[i];
+	init = init->syntaxCopy();
+	ai->value.data[i] = init;
+    }
+    return ai;
+}
+
+void StructInitializer::addInit(Identifier *field, Initializer *value)
+{
+    //printf("StructInitializer::addInit(field = %p, value = %p)\n", field, value);
+    this->field.push(field);
+    this->value.push(value);
+}
+
+Initializer *StructInitializer::semantic(Scope *sc, Type *t)
+{
+    TypeStruct *ts;
+    int errors = 0;
+
+    //printf("StructInitializer::semantic(t = %s) %s\n", t->toChars(), toChars());
+    vars.setDim(field.dim);
+    t = t->toBasetype();
+    if (t->ty == Tstruct)
+    {	unsigned i;
+	unsigned fieldi = 0;
+
+	ts = (TypeStruct *)t;
+	ad = ts->sym;
+	for (i = 0; i < field.dim; i++)
+	{
+	    Identifier *id = (Identifier *)field.data[i];
+	    Initializer *val = (Initializer *)value.data[i];
+	    Dsymbol *s;
+	    VarDeclaration *v;
+
+	    if (id == NULL)
+	    {
+		if (fieldi >= ad->fields.dim)
+		{   error(loc, "too many initializers for %s", ad->toChars());
+		    field.remove(i);
+		    i--;
+		    continue;
+		}
+		else
+		{
+		    s = (Dsymbol *)ad->fields.data[fieldi];
+		}
+	    }
+	    else
+	    {
+		//s = ad->symtab->lookup(id);
+		s = ad->search(loc, id, 0);
+		if (!s)
+		{
+		    error(loc, "'%s' is not a member of '%s'", id->toChars(), t->toChars());
+		    continue;
+		}
+
+		// Find out which field index it is
+		for (fieldi = 0; 1; fieldi++)
+		{
+		    if (fieldi >= ad->fields.dim)
+		    {
+			s->error("is not a per-instance initializable field");
+			break;
+		    }
+		    if (s == (Dsymbol *)ad->fields.data[fieldi])
+			break;
+		}
+	    }
+	    if (s && (v = s->isVarDeclaration()) != NULL)
+	    {
+		val = val->semantic(sc, v->type);
+		value.data[i] = (void *)val;
+		vars.data[i] = (void *)v;
+	    }
+	    else
+	    {	error(loc, "%s is not a field of %s", id ? id->toChars() : s->toChars(), ad->toChars());
+		errors = 1;
+	    }
+	    fieldi++;
+	}
+    }
+    else if (t->ty == Tdelegate && value.dim == 0)
+    {	/* Rewrite as empty delegate literal { }
+	 */
+	Arguments *arguments = new Arguments;
+	Type *tf = new TypeFunction(arguments, NULL, 0, LINKd);
+	FuncLiteralDeclaration *fd = new FuncLiteralDeclaration(loc, 0, tf, TOKdelegate, NULL);
+	fd->fbody = new CompoundStatement(loc, new Statements());
+	fd->endloc = loc;
+	Expression *e = new FuncExp(loc, fd);
+	ExpInitializer *ie = new ExpInitializer(loc, e);
+	return ie->semantic(sc, t);
+    }
+    else
+    {
+	error(loc, "a struct is not a valid initializer for a %s", t->toChars());
+	errors = 1;
+    }
+    if (errors)
+    {
+	field.setDim(0);
+	value.setDim(0);
+	vars.setDim(0);
+    }
+    return this;
+}
+
+
+/***************************************
+ * This works by transforming a struct initializer into
+ * a struct literal. In the future, the two should be the
+ * same thing.
+ */
+Expression *StructInitializer::toExpression()
+{   Expression *e;
+
+    //printf("StructInitializer::toExpression() %s\n", toChars());
+    if (!ad)				// if fwd referenced
+    {
+	return NULL;
+    }
+    StructDeclaration *sd = ad->isStructDeclaration();
+    if (!sd)
+	return NULL;
+    Expressions *elements = new Expressions();
+    for (size_t i = 0; i < value.dim; i++)
+    {
+	if (field.data[i])
+	    goto Lno;
+	Initializer *iz = (Initializer *)value.data[i];
+	if (!iz)
+	    goto Lno;
+	Expression *ex = iz->toExpression();
+	if (!ex)
+	    goto Lno;
+	elements->push(ex);
+    }
+    e = new StructLiteralExp(loc, sd, elements);
+    e->type = sd->type;
+    return e;
+
+Lno:
+    delete elements;
+    //error(loc, "struct initializers as expressions are not allowed");
+    return NULL;
+}
+
+
+void StructInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    //printf("StructInitializer::toCBuffer()\n");
+    buf->writebyte('{');
+    for (int i = 0; i < field.dim; i++)
+    {
+        if (i > 0)
+	    buf->writebyte(',');
+        Identifier *id = (Identifier *)field.data[i];
+        if (id)
+        {
+            buf->writestring(id->toChars());
+            buf->writebyte(':');
+        }
+        Initializer *iz = (Initializer *)value.data[i];
+        if (iz)
+            iz->toCBuffer(buf, hgs);
+    }
+    buf->writebyte('}');
+}
+
+/********************************** ArrayInitializer ************************************/
+
+ArrayInitializer::ArrayInitializer(Loc loc)
+    : Initializer(loc)
+{
+    dim = 0;
+    type = NULL;
+    sem = 0;
+}
+
+Initializer *ArrayInitializer::syntaxCopy()
+{
+    //printf("ArrayInitializer::syntaxCopy()\n");
+
+    ArrayInitializer *ai = new ArrayInitializer(loc);
+
+    assert(index.dim == value.dim);
+    ai->index.setDim(index.dim);
+    ai->value.setDim(value.dim);
+    for (int i = 0; i < ai->value.dim; i++)
+    {	Expression *e = (Expression *)index.data[i];
+	if (e)
+	    e = e->syntaxCopy();
+	ai->index.data[i] = e;
+
+	Initializer *init = (Initializer *)value.data[i];
+	init = init->syntaxCopy();
+	ai->value.data[i] = init;
+    }
+    return ai;
+}
+
+void ArrayInitializer::addInit(Expression *index, Initializer *value)
+{
+    this->index.push(index);
+    this->value.push(value);
+    dim = 0;
+    type = NULL;
+}
+
+Initializer *ArrayInitializer::semantic(Scope *sc, Type *t)
+{   unsigned i;
+    unsigned length;
+
+    //printf("ArrayInitializer::semantic(%s)\n", t->toChars());
+    if (sem)				// if semantic() already run
+	return this;
+    sem = 1;
+    type = t;
+    t = t->toBasetype();
+    switch (t->ty)
+    {
+	case Tpointer:
+	case Tsarray:
+	case Tarray:
+	    break;
+
+	default:
+	    error(loc, "cannot use array to initialize %s", type->toChars());
+	    return this;
+    }
+
+    length = 0;
+    for (i = 0; i < index.dim; i++)
+    {	Expression *idx;
+	Initializer *val;
+
+	idx = (Expression *)index.data[i];
+	if (idx)
+	{   idx = idx->semantic(sc);
+	    idx = idx->optimize(WANTvalue | WANTinterpret);
+	    index.data[i] = (void *)idx;
+	    length = idx->toInteger();
+	}
+
+	val = (Initializer *)value.data[i];
+	val = val->semantic(sc, t->next);
+	value.data[i] = (void *)val;
+	length++;
+	if (length == 0)
+	    error(loc, "array dimension overflow");
+	if (length > dim)
+	    dim = length;
+    }
+    unsigned long amax = 0x80000000;
+    if ((unsigned long) dim * t->next->size() >= amax)
+	error(loc, "array dimension %u exceeds max of %llu", dim, amax / t->next->size());
+    return this;
+}
+
+/********************************
+ * If possible, convert array initializer to array literal.
+ */
+
+Expression *ArrayInitializer::toExpression()
+{   Expressions *elements;
+    Expression *e;
+
+    //printf("ArrayInitializer::toExpression()\n");
+    //static int i; if (++i == 2) halt();
+    elements = new Expressions();
+    for (size_t i = 0; i < value.dim; i++)
+    {
+	if (index.data[i])
+	    goto Lno;
+	Initializer *iz = (Initializer *)value.data[i];
+	if (!iz)
+	    goto Lno;
+	Expression *ex = iz->toExpression();
+	if (!ex)
+	    goto Lno;
+	elements->push(ex);
+    }
+    e = new ArrayLiteralExp(loc, elements);
+    e->type = type;
+    return e;
+
+Lno:
+    delete elements;
+    error(loc, "array initializers as expressions are not allowed");
+    return NULL;
+}
+
+
+/********************************
+ * If possible, convert array initializer to associative array initializer.
+ */
+
+Initializer *ArrayInitializer::toAssocArrayInitializer()
+{   Expressions *keys;
+    Expressions *values;
+    Expression *e;
+
+    //printf("ArrayInitializer::toAssocArrayInitializer()\n");
+    //static int i; if (++i == 2) halt();
+    keys = new Expressions();
+    keys->setDim(value.dim);
+    values = new Expressions();
+    values->setDim(value.dim);
+
+    for (size_t i = 0; i < value.dim; i++)
+    {
+	e = (Expression *)index.data[i];
+	if (!e)
+	    goto Lno;
+	keys->data[i] = (void *)e;
+
+	Initializer *iz = (Initializer *)value.data[i];
+	if (!iz)
+	    goto Lno;
+	e = iz->toExpression();
+	if (!e)
+	    goto Lno;
+	values->data[i] = (void *)e;
+    }
+    e = new AssocArrayLiteralExp(loc, keys, values);
+    return new ExpInitializer(loc, e);
+
+Lno:
+    delete keys;
+    delete values;
+    error(loc, "not an associative array initializer");
+    return this;
+}
+
+
+Type *ArrayInitializer::inferType(Scope *sc)
+{
+    for (size_t i = 0; i < value.dim; i++)
+    {
+	if (index.data[i])
+	    goto Lno;
+    }
+    if (value.dim)
+    {
+	Initializer *iz = (Initializer *)value.data[0];
+	if (iz)
+	{   Type *t = iz->inferType(sc);
+	    t = new TypeSArray(t, new IntegerExp(value.dim));
+	    t = t->semantic(loc, sc);
+	    return t;
+	}
+    }
+
+Lno:
+    error(loc, "cannot infer type from this array initializer");
+    return Type::terror;
+}
+
+
+void ArrayInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    buf->writebyte('[');
+    for (int i = 0; i < index.dim; i++)
+    {
+        if (i > 0)
+	    buf->writebyte(',');
+        Expression *ex = (Expression *)index.data[i];
+        if (ex)
+        {
+            ex->toCBuffer(buf, hgs);
+            buf->writebyte(':');
+        }
+        Initializer *iz = (Initializer *)value.data[i];
+        if (iz)
+            iz->toCBuffer(buf, hgs);
+    }
+    buf->writebyte(']');
+}
+
+
+/********************************** ExpInitializer ************************************/
+
+ExpInitializer::ExpInitializer(Loc loc, Expression *exp)
+    : Initializer(loc)
+{
+    this->exp = exp;
+}
+
+Initializer *ExpInitializer::syntaxCopy()
+{
+    return new ExpInitializer(loc, exp->syntaxCopy());
+}
+
+Initializer *ExpInitializer::semantic(Scope *sc, Type *t)
+{
+    //printf("ExpInitializer::semantic(%s), type = %s\n", exp->toChars(), t->toChars());
+    exp = exp->semantic(sc);
+    Type *tb = t->toBasetype();
+
+    /* Look for case of initializing a static array with a too-short
+     * string literal, such as:
+     *	char[5] foo = "abc";
+     * Allow this by doing an explicit cast, which will lengthen the string
+     * literal.
+     */
+    if (exp->op == TOKstring && tb->ty == Tsarray && exp->type->ty == Tsarray)
+    {	StringExp *se = (StringExp *)exp;
+
+	if (!se->committed && se->type->ty == Tsarray &&
+	    ((TypeSArray *)se->type)->dim->toInteger() <
+	    ((TypeSArray *)t)->dim->toInteger())
+	{
+	    exp = se->castTo(sc, t);
+	    goto L1;
+	}
+    }
+
+    // Look for the case of statically initializing an array
+    // with a single member.
+    if (tb->ty == Tsarray &&
+	!tb->next->equals(exp->type->toBasetype()->next) &&
+	exp->implicitConvTo(tb->next)
+       )
+    {
+	t = tb->next;
+    }
+
+    exp = exp->implicitCastTo(sc, t);
+L1:
+    exp = exp->optimize(WANTvalue | WANTinterpret);
+    //printf("-ExpInitializer::semantic(): "); exp->print();
+    return this;
+}
+
+Type *ExpInitializer::inferType(Scope *sc)
+{
+    //printf("ExpInitializer::inferType() %s\n", toChars());
+    exp = exp->semantic(sc);
+    exp = resolveProperties(sc, exp);
+    return exp->type;
+}
+
+Expression *ExpInitializer::toExpression()
+{
+    return exp;
+}
+
+
+void ExpInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
+{
+    exp->toCBuffer(buf, hgs);
+}
+
+
+
--- a/dmd/inline.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/inline.c	Sat Jul 12 19:38:31 2008 +0200
@@ -860,7 +860,7 @@
 }
 
 
-#if V2
+#if DMDV2
 Statement *ForeachRangeStatement::inlineScan(InlineScanState *iss)
 {
     lwr = lwr->inlineScan(iss);
@@ -1290,7 +1290,7 @@
 #endif
 	isSynchronized() ||
 	isImportedSymbol() ||
-#if V2
+#if DMDV2
 	closureVars.dim ||	// no nested references to this frame
 #else
 	nestedFrameRef ||	// no nested references to this frame
--- a/dmd/interpret.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/interpret.c	Sat Jul 12 19:38:31 2008 +0200
@@ -729,7 +729,7 @@
     return e;
 }
 
-#if V2
+#if DMDV2
 Expression *ForeachRangeStatement::interpret(InterState *istate)
 {
 #if LOG
@@ -989,7 +989,7 @@
     SymbolDeclaration *s = d->isSymbolDeclaration();
     if (v)
     {
-#if V2
+#if DMDV2
 	if ((v->isConst() || v->isInvariant()) && v->init && !v->value)
 #else
 	if (v->isConst() && v->init)
@@ -1045,7 +1045,7 @@
 	    else if (v->init->isVoidInitializer())
 		e = NULL;
 	}
-#if V2
+#if DMDV2
 	else if (s == v && (v->isConst() || v->isInvariant()) && v->init)
 #else
 	else if (s == v && v->isConst() && v->init)
@@ -1855,7 +1855,7 @@
 	FuncDeclaration *fd = ((VarExp *)e1)->var->isFuncDeclaration();
 	if (fd)
 	{
-#if V2
+#if DMDV2
 	    enum BUILTIN b = fd->isBuiltin();
 	    if (b)
 	    {	Expressions args;
--- a/dmd/lexer.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/lexer.c	Sat Jul 12 19:38:31 2008 +0200
@@ -568,7 +568,7 @@
 		t->value = hexStringConstant(t);
 		return;
 
-#if V2
+#if DMDV2
 	    case 'q':
 		if (p[1] == '"')
 		{
@@ -627,7 +627,7 @@
 	    case 'a':  	case 'b':   case 'c':   case 'd':   case 'e':
 	    case 'f':  	case 'g':   case 'h':   case 'i':   case 'j':
 	    case 'k':  	            case 'm':   case 'n':   case 'o':
-#if V2
+#if DMDV2
 	    case 'p':  	/*case 'q': case 'r':*/ case 's':   case 't':
 #else
 	    case 'p':  	case 'q': /*case 'r':*/ case 's':   case 't':
@@ -677,6 +677,7 @@
 			sprintf(timestamp, "%.24s", p);
 		    }
 
+#if DMDV1
 		    if (mod && id == Id::FILE)
 		    {
 			t->ustring = (unsigned char *)(loc.filename ? loc.filename : mod->ident->toChars());
@@ -687,7 +688,9 @@
 			t->value = TOKint64v;
 			t->uns64value = loc.linnum;
 		    }
-		    else if (id == Id::DATE)
+		    else
+#endif
+		    if (id == Id::DATE)
 		    {
 			t->ustring = (unsigned char *)date;
 			goto Lstring;
@@ -730,7 +733,7 @@
 			t->value = TOKint64v;
 			t->uns64value = major * 1000 + minor;
 		    }
-#if V2
+#if DMDV2
 		    else if (id == Id::EOFX)
 		    {
 			t->value = TOKeof;
@@ -1476,7 +1479,7 @@
 }
 
 
-#if V2
+#if DMDV2
 /**************************************
  * Lex delimited strings:
  *	q"(foo(xxx))"   // "foo(xxx)"
@@ -2917,11 +2920,14 @@
     // Added after 1.0
     {	"ref",		TOKref		},
     {	"macro",	TOKmacro	},
-#if V2
+#if DMDV2
     {	"pure",		TOKpure		},
     {	"nothrow",	TOKnothrow	},
+    {	"__thread",	TOKtls		},
     {	"__traits",	TOKtraits	},
     {	"__overloadset", TOKoverloadset	},
+    {	"__FILE__",	TOKfile		},
+    {	"__LINE__",	TOKline		},
 #endif
 };
 
@@ -2974,7 +2980,7 @@
     Token::tochars[TOKxorass]		= "^=";
     Token::tochars[TOKassign]		= "=";
     Token::tochars[TOKconstruct]	= "=";
-#if V2
+#if DMDV2
     Token::tochars[TOKblit]		= "=";
 #endif
     Token::tochars[TOKlt]		= "<";
--- a/dmd/lexer.h	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/lexer.h	Sat Jul 12 19:38:31 2008 +0200
@@ -150,10 +150,14 @@
 	// Added after 1.0
 	TOKref,
 	TOKmacro,
-#if V2
+#if DMDV2
 	TOKtraits,
 	TOKoverloadset,
 	TOKpure,
+	TOKnothrow,
+	TOKtls,
+	TOKline,
+	TOKfile,
 #endif
 
 	TOKMAX
@@ -273,7 +277,7 @@
     unsigned escapeSequence();
     TOK wysiwygStringConstant(Token *t, int tc);
     TOK hexStringConstant(Token *t);
-#if V2
+#if DMDV2
     TOK delimitedStringConstant(Token *t);
     TOK tokenStringConstant(Token *t);
 #endif
--- a/dmd/mars.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/mars.c	Sat Jul 12 19:38:31 2008 +0200
@@ -1043,7 +1043,7 @@
 	if (global.params.verbose)
 	    printf("code      %s\n", m->toChars());
 	if (global.params.obj)
-	    m->genobjfile();
+	    m->genobjfile(0);
 	if (global.errors)
 	    m->deleteObjFile();
 	else
--- a/dmd/mars.h	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/mars.h	Sat Jul 12 19:38:31 2008 +0200
@@ -32,7 +32,7 @@
 /* Changes for the GDC compiler by David Friedman */
 #endif
 
-#define V2	0	// Version 2.0 features
+#define DMDV2	0	// Version 2.0 features
 #define BREAKABI 1	// 0 if not ready to break the ABI just yet
 
 struct Array;
@@ -300,7 +300,7 @@
 {
     MATCHnomatch,	// no match
     MATCHconvert,	// match with conversions
-#if V2
+#if DMDV2
     MATCHconst,		// match with conversion to const
 #endif
     MATCHexact		// exact match
--- a/dmd/module.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/module.c	Sat Jul 12 19:38:31 2008 +0200
@@ -1,983 +1,982 @@
-
-// 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 _MSC_VER || __MINGW32__
-#include <malloc.h>
-#endif
-
-#if IN_GCC
-#include "gdc_alloca.h"
-#endif
-
-#include "mem.h"
-
-#include "mars.h"
-#include "module.h"
-#include "parse.h"
-#include "scope.h"
-#include "identifier.h"
-#include "id.h"
-#include "import.h"
-#include "dsymbol.h"
-#include "hdrgen.h"
-#include "lexer.h"
-
-#define MARS 1
-#include "html.h"
-
-#ifdef IN_GCC
-#include "d-dmd-gcc.h"
-#endif
-
-ClassDeclaration *Module::moduleinfo;
-
-Module *Module::rootModule;
-DsymbolTable *Module::modules;
-Array Module::amodules;
-
-Array Module::deferred;	// deferred Dsymbol's needing semantic() run on them
-unsigned Module::dprogress;
-
-void Module::init()
-{
-    modules = new DsymbolTable();
-}
-
-Module::Module(char *filename, Identifier *ident, int doDocComment, int doHdrGen)
-	: Package(ident)
-{
-    FileName *srcfilename;
-    FileName *cfilename;
-    FileName *hfilename;
-    FileName *objfilename;
-    FileName *llfilename;
-    FileName *bcfilename;
-    FileName *symfilename;
-
-//    printf("Module::Module(filename = '%s', ident = '%s')\n", filename, ident->toChars());
-    this->arg = filename;
-    md = NULL;
-    errors = 0;
-    numlines = 0;
-    members = NULL;
-    isHtml = 0;
-    isDocFile = 0;
-    needmoduleinfo = 0;
-#ifdef IN_GCC
-    strictlyneedmoduleinfo = 0;
-#endif
-    insearch = 0;
-    searchCacheIdent = NULL;
-    searchCacheSymbol = NULL;
-    searchCacheFlags = 0;
-    semanticstarted = 0;
-    semanticdone = 0;
-    decldefs = NULL;
-    vmoduleinfo = NULL;
-    massert = NULL;
-    marray = NULL;
-    sictor = NULL;
-    sctor = NULL;
-    sdtor = NULL;
-    stest = NULL;
-    sfilename = NULL;
-    root = 0;
-    importedFrom = NULL;
-    srcfile = NULL;
-    docfile = NULL;
-
-    debuglevel = 0;
-    debugids = NULL;
-    debugidsNot = NULL;
-    versionlevel = 0;
-    versionids = NULL;
-    versionidsNot = NULL;
-
-    macrotable = NULL;
-    escapetable = NULL;
-    cov = NULL;
-    covb = NULL;
-
-    srcfilename = FileName::defaultExt(filename, global.mars_ext);
-    if (!srcfilename->equalsExt(global.mars_ext))
-    {
-	if (srcfilename->equalsExt("html") ||
-	    srcfilename->equalsExt("htm")  ||
-	    srcfilename->equalsExt("xhtml"))
-	    isHtml = 1;
-	else
-	{   error("source file name '%s' must have .%s extension", srcfilename->toChars(), global.mars_ext);
-	    fatal();
-	}
-    }
-
-    char *argobj;
-    if (global.params.objname)
-	argobj = global.params.objname;
-    else if (global.params.preservePaths)
-	argobj = filename;
-    else
-	argobj = FileName::name(filename);
-    if (!FileName::absolute(argobj))
-    {
-	argobj = FileName::combine(global.params.objdir, argobj);
-    }
-
-    if (global.params.objname)
-	objfilename = new FileName(argobj, 0);
-    else
-    objfilename = FileName::forceExt(argobj, global.obj_ext);
-
-    llfilename = FileName::forceExt(argobj, global.ll_ext);
-    bcfilename = FileName::forceExt(argobj, global.bc_ext);
-
-    symfilename = FileName::forceExt(filename, global.sym_ext);
-
-    srcfile = new File(srcfilename);
-
-    if (doDocComment)
-    {
-	setDocfile();
-    }
-
-    if (doHdrGen)
-    {
-	setHdrfile();
-    }
-
-    objfile = new File(objfilename);
-    bcfile = new File(bcfilename);
-    llfile = new File(llfilename);
-    symfile = new File(symfilename);
-}
-
-void Module::setDocfile()
-{
-    FileName *docfilename;
-    char *argdoc;
-
-    if (global.params.docname)
-	argdoc = global.params.docname;
-    else if (global.params.preservePaths)
-	argdoc = (char *)arg;
-    else
-	argdoc = FileName::name((char *)arg);
-    if (!FileName::absolute(argdoc))
-    {	//FileName::ensurePathExists(global.params.docdir);
-	argdoc = FileName::combine(global.params.docdir, argdoc);
-    }
-    if (global.params.docname)
-	docfilename = new FileName(argdoc, 0);
-    else
-	docfilename = FileName::forceExt(argdoc, global.doc_ext);
-
-    if (docfilename->equals(srcfile->name))
-    {   error("Source file and documentation file have same name '%s'", srcfile->name->str);
-	fatal();
-    }
-
-    docfile = new File(docfilename);
-}
-
-void Module::setHdrfile()
-{
-    FileName *hdrfilename;
-    char *arghdr;
-
-    if (global.params.hdrname)
-	arghdr = global.params.hdrname;
-    else if (global.params.preservePaths)
-	arghdr = (char *)arg;
-    else
-	arghdr = FileName::name((char *)arg);
-    if (!FileName::absolute(arghdr))
-    {	//FileName::ensurePathExists(global.params.hdrdir);
-	arghdr = FileName::combine(global.params.hdrdir, arghdr);
-    }
-    if (global.params.hdrname)
-	hdrfilename = new FileName(arghdr, 0);
-    else
-	hdrfilename = FileName::forceExt(arghdr, global.hdr_ext);
-
-    if (hdrfilename->equals(srcfile->name))
-    {   error("Source file and 'header' file have same name '%s'", srcfile->name->str);
-	fatal();
-    }
-
-    hdrfile = new File(hdrfilename);
-}
-
-void Module::deleteObjFile()
-{
-    if (global.params.obj)
-	objfile->remove();
-    //if (global.params.llvmBC)
-    bcfile->remove();
-    if (docfile)
-	docfile->remove();
-}
-
-Module::~Module()
-{
-}
-
-char *Module::kind()
-{
-    return "module";
-}
-
-Module *Module::load(Loc loc, Array *packages, Identifier *ident)
-{   Module *m;
-    char *filename;
-
-    //printf("Module::load(ident = '%s')\n", ident->toChars());
-
-    // Build module filename by turning:
-    //	foo.bar.baz
-    // into:
-    //	foo\bar\baz
-    filename = ident->toChars();
-    if (packages && packages->dim)
-    {
-	OutBuffer buf;
-	int i;
-
-	for (i = 0; i < packages->dim; i++)
-	{   Identifier *pid = (Identifier *)packages->data[i];
-
-	    buf.writestring(pid->toChars());
-#if _WIN32
-	    buf.writeByte('\\');
-#else
-	    buf.writeByte('/');
-#endif
-	}
-	buf.writestring(filename);
-	buf.writeByte(0);
-	filename = (char *)buf.extractData();
-    }
-
-    m = new Module(filename, ident, 0, 0);
-    m->loc = loc;
-
-    /* Search along global.path for .di file, then .d file.
-     */
-    char *result = NULL;
-    FileName *fdi = FileName::forceExt(filename, global.hdr_ext);
-    FileName *fd  = FileName::forceExt(filename, global.mars_ext);
-    char *sdi = fdi->toChars();
-    char *sd  = fd->toChars();
-
-    if (FileName::exists(sdi))
-	result = sdi;
-    else if (FileName::exists(sd))
-	result = sd;
-    else if (FileName::absolute(filename))
-	;
-    else if (!global.path)
-	;
-    else
-    {
-	for (size_t i = 0; i < global.path->dim; i++)
-	{
-	    char *p = (char *)global.path->data[i];
-	    char *n = FileName::combine(p, sdi);
-	    if (FileName::exists(n))
-	    {	result = n;
-		break;
-	    }
-	    mem.free(n);
-	    n = FileName::combine(p, sd);
-	    if (FileName::exists(n))
-	    {	result = n;
-		break;
-	    }
-	    mem.free(n);
-	}
-    }
-    if (result)
-	m->srcfile = new File(result);
-
-    if (global.params.verbose)
-    {
-	printf("import    ");
-	if (packages)
-	{
-	    for (size_t i = 0; i < packages->dim; i++)
-	    {   Identifier *pid = (Identifier *)packages->data[i];
-		printf("%s.", pid->toChars());
-	    }
-	}
-	printf("%s\t(%s)\n", ident->toChars(), m->srcfile->toChars());
-    }
-
-    m->read(loc);
-    m->parse();
-
-#ifdef IN_GCC
-    d_gcc_magic_module(m);
-#endif
-
-    return m;
-}
-
-void Module::read(Loc loc)
-{
-    //printf("Module::read('%s') file '%s'\n", toChars(), srcfile->toChars());
-    if (srcfile->read())
-    {	error(loc, "cannot read file '%s'", srcfile->toChars());
-	fatal();
-    }
-}
-
-inline unsigned readwordLE(unsigned short *p)
-{
-#if __I86__
-    return *p;
-#else
-    return (((unsigned char *)p)[1] << 8) | ((unsigned char *)p)[0];
-#endif
-}
-
-inline unsigned readwordBE(unsigned short *p)
-{
-    return (((unsigned char *)p)[0] << 8) | ((unsigned char *)p)[1];
-}
-
-inline unsigned readlongLE(unsigned *p)
-{
-#if __I86__
-    return *p;
-#else
-    return ((unsigned char *)p)[0] |
-	(((unsigned char *)p)[1] << 8) |
-	(((unsigned char *)p)[2] << 16) |
-	(((unsigned char *)p)[3] << 24);
-#endif
-}
-
-inline unsigned readlongBE(unsigned *p)
-{
-    return ((unsigned char *)p)[3] |
-	(((unsigned char *)p)[2] << 8) |
-	(((unsigned char *)p)[1] << 16) |
-	(((unsigned char *)p)[0] << 24);
-}
-
-#if IN_GCC
-void Module::parse(bool dump_source)
-#else
-void Module::parse()
-#endif
-{   char *srcname;
-    unsigned char *buf;
-    unsigned buflen;
-    unsigned le;
-    unsigned bom;
-
-    //printf("Module::parse()\n");
-
-    srcname = srcfile->name->toChars();
-    //printf("Module::parse(srcname = '%s')\n", srcname);
-
-    buf = srcfile->buffer;
-    buflen = srcfile->len;
-
-    if (buflen >= 2)
-    {
-	/* Convert all non-UTF-8 formats to UTF-8.
-	 * BOM : http://www.unicode.org/faq/utf_bom.html
-	 * 00 00 FE FF	UTF-32BE, big-endian
-	 * FF FE 00 00	UTF-32LE, little-endian
-	 * FE FF	UTF-16BE, big-endian
-	 * FF FE	UTF-16LE, little-endian
-	 * EF BB BF	UTF-8
-	 */
-
-	bom = 1;		// assume there's a BOM
-	if (buf[0] == 0xFF && buf[1] == 0xFE)
-	{
-	    if (buflen >= 4 && buf[2] == 0 && buf[3] == 0)
-	    {	// UTF-32LE
-		le = 1;
-
-	    Lutf32:
-		OutBuffer dbuf;
-		unsigned *pu = (unsigned *)(buf);
-		unsigned *pumax = &pu[buflen / 4];
-
-		if (buflen & 3)
-		{   error("odd length of UTF-32 char source %u", buflen);
-		    fatal();
-		}
-
-		dbuf.reserve(buflen / 4);
-		for (pu += bom; pu < pumax; pu++)
-		{   unsigned u;
-
-		    u = le ? readlongLE(pu) : readlongBE(pu);
-		    if (u & ~0x7F)
-		    {
-			if (u > 0x10FFFF)
-			{   error("UTF-32 value %08x greater than 0x10FFFF", u);
-			    fatal();
-			}
-			dbuf.writeUTF8(u);
-		    }
-		    else
-			dbuf.writeByte(u);
-		}
-		dbuf.writeByte(0);		// add 0 as sentinel for scanner
-		buflen = dbuf.offset - 1;	// don't include sentinel in count
-		buf = (unsigned char *) dbuf.extractData();
-	    }
-	    else
-	    {   // UTF-16LE (X86)
-		// Convert it to UTF-8
-		le = 1;
-
-	    Lutf16:
-		OutBuffer dbuf;
-		unsigned short *pu = (unsigned short *)(buf);
-		unsigned short *pumax = &pu[buflen / 2];
-
-		if (buflen & 1)
-		{   error("odd length of UTF-16 char source %u", buflen);
-		    fatal();
-		}
-
-		dbuf.reserve(buflen / 2);
-		for (pu += bom; pu < pumax; pu++)
-		{   unsigned u;
-
-		    u = le ? readwordLE(pu) : readwordBE(pu);
-		    if (u & ~0x7F)
-		    {	if (u >= 0xD800 && u <= 0xDBFF)
-			{   unsigned u2;
-
-			    if (++pu > pumax)
-			    {   error("surrogate UTF-16 high value %04x at EOF", u);
-				fatal();
-			    }
-			    u2 = le ? readwordLE(pu) : readwordBE(pu);
-			    if (u2 < 0xDC00 || u2 > 0xDFFF)
-			    {   error("surrogate UTF-16 low value %04x out of range", u2);
-				fatal();
-			    }
-			    u = (u - 0xD7C0) << 10;
-			    u |= (u2 - 0xDC00);
-			}
-			else if (u >= 0xDC00 && u <= 0xDFFF)
-			{   error("unpaired surrogate UTF-16 value %04x", u);
-			    fatal();
-			}
-			else if (u == 0xFFFE || u == 0xFFFF)
-			{   error("illegal UTF-16 value %04x", u);
-			    fatal();
-			}
-			dbuf.writeUTF8(u);
-		    }
-		    else
-			dbuf.writeByte(u);
-		}
-		dbuf.writeByte(0);		// add 0 as sentinel for scanner
-		buflen = dbuf.offset - 1;	// don't include sentinel in count
-		buf = (unsigned char *) dbuf.extractData();
-	    }
-	}
-	else if (buf[0] == 0xFE && buf[1] == 0xFF)
-	{   // UTF-16BE
-	    le = 0;
-	    goto Lutf16;
-	}
-	else if (buflen >= 4 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF)
-	{   // UTF-32BE
-	    le = 0;
-	    goto Lutf32;
-	}
-	else if (buflen >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF)
-	{   // UTF-8
-
-	    buf += 3;
-	    buflen -= 3;
-	}
-	else
-	{
-	    /* There is no BOM. Make use of Arcane Jill's insight that
-	     * the first char of D source must be ASCII to
-	     * figure out the encoding.
-	     */
-
-	    bom = 0;
-	    if (buflen >= 4)
-	    {   if (buf[1] == 0 && buf[2] == 0 && buf[3] == 0)
-		{   // UTF-32LE
-		    le = 1;
-		    goto Lutf32;
-		}
-		else if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0)
-		{   // UTF-32BE
-		    le = 0;
-		    goto Lutf32;
-		}
-	    }
-	    if (buflen >= 2)
-	    {
-		if (buf[1] == 0)
-		{   // UTF-16LE
-		    le = 1;
-		    goto Lutf16;
-		}
-		else if (buf[0] == 0)
-		{   // UTF-16BE
-		    le = 0;
-		    goto Lutf16;
-		}
-	    }
-
-	    // It's UTF-8
-	    if (buf[0] >= 0x80)
-	    {	error("source file must start with BOM or ASCII character, not \\x%02X", buf[0]);
-		fatal();
-	    }
-	}
-    }
-
-#ifdef IN_GCC
-    // dump utf-8 encoded source
-    if (dump_source)
-    {	// %% srcname could contain a path ...
-	d_gcc_dump_source(srcname, "utf-8", buf, buflen);
-    }
-#endif
-
-    /* If it starts with the string "Ddoc", then it's a documentation
-     * source file.
-     */
-    if (buflen >= 4 && memcmp(buf, "Ddoc", 4) == 0)
-    {
-	comment = buf + 4;
-	isDocFile = 1;
-	if (!docfile)
-	    setDocfile();
-	return;
-    }
-    if (isHtml)
-    {
-	OutBuffer *dbuf = new OutBuffer();
-	Html h(srcname, buf, buflen);
-	h.extractCode(dbuf);
-	buf = dbuf->data;
-	buflen = dbuf->offset;
-#ifdef IN_GCC
-	// dump extracted source
-	if (dump_source)
-	    d_gcc_dump_source(srcname, "d.utf-8", buf, buflen);
-#endif
-    }
-    Parser p(this, buf, buflen, docfile != NULL);
-    p.nextToken();
-    members = p.parseModule();
-    md = p.md;
-    numlines = p.loc.linnum;
-
-    DsymbolTable *dst;
-
-    if (md)
-    {	this->ident = md->id;
-	dst = Package::resolve(md->packages, &this->parent, NULL);
-    }
-    else
-    {
-	dst = modules;
-
-	/* Check to see if module name is a valid identifier
-	 */
-	if (!Lexer::isValidIdentifier(this->ident->toChars()))
-	    error("has non-identifier characters in filename, use module declaration instead");
-    }
-
-    // Update global list of modules
-    if (!dst->insert(this))
-    {
-	if (md)
-	    error(loc, "is in multiple packages %s", md->toChars());
-	else
-	    error(loc, "is in multiple defined");
-    }
-    else
-    {
-	amodules.push(this);
-    }
-}
-
-void Module::semantic()
-{   int i;
-
-    if (semanticstarted)
-	return;
-
-    //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
-    semanticstarted = 1;
-
-    // Note that modules get their own scope, from scratch.
-    // This is so regardless of where in the syntax a module
-    // gets imported, it is unaffected by context.
-    Scope *sc = Scope::createGlobal(this);	// create root scope
-
-    //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage);
-
-    // Add import of "object" if this module isn't "object"
-    if (ident != Id::object)
-    {
-	Import *im = new Import(0, NULL, Id::object, NULL, 0);
-	members->shift(im);
-    }
-
-    // Add all symbols into module's symbol table
-    symtab = new DsymbolTable();
-    for (i = 0; i < members->dim; i++)
-    {	Dsymbol *s;
-
-	s = (Dsymbol *)members->data[i];
-	s->addMember(NULL, sc->scopesym, 1);
-    }
-
-    // Pass 1 semantic routines: do public side of the definition
-    for (i = 0; i < members->dim; i++)
-    {	Dsymbol *s;
-
-	s = (Dsymbol *)members->data[i];
-	//printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars());
-	s->semantic(sc);
-	runDeferredSemantic();
-    }
-
-    sc = sc->pop();
-    sc->pop();
-    semanticdone = semanticstarted;
-    //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
-}
-
-void Module::semantic2()
-{   int i;
-
-    if (deferred.dim)
-    {
-	for (int i = 0; i < deferred.dim; i++)
-	{
-	    Dsymbol *sd = (Dsymbol *)deferred.data[i];
-
-	    sd->error("unable to resolve forward reference in definition");
-	}
-	return;
-    }
-    //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent);
-    if (semanticstarted >= 2)
-	return;
-    assert(semanticstarted == 1);
-    semanticstarted = 2;
-
-    // Note that modules get their own scope, from scratch.
-    // This is so regardless of where in the syntax a module
-    // gets imported, it is unaffected by context.
-    Scope *sc = Scope::createGlobal(this);	// create root scope
-    //printf("Module = %p\n", sc.scopesym);
-
-    // Pass 2 semantic routines: do initializers and function bodies
-    for (i = 0; i < members->dim; i++)
-    {	Dsymbol *s;
-
-	s = (Dsymbol *)members->data[i];
-	s->semantic2(sc);
-    }
-
-    sc = sc->pop();
-    sc->pop();
-    semanticdone = semanticstarted;
-    //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent);
-}
-
-void Module::semantic3()
-{   int i;
-
-    //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent);
-    if (semanticstarted >= 3)
-	return;
-    assert(semanticstarted == 2);
-    semanticstarted = 3;
-
-    // Note that modules get their own scope, from scratch.
-    // This is so regardless of where in the syntax a module
-    // gets imported, it is unaffected by context.
-    Scope *sc = Scope::createGlobal(this);	// create root scope
-    //printf("Module = %p\n", sc.scopesym);
-
-    // Pass 3 semantic routines: do initializers and function bodies
-    for (i = 0; i < members->dim; i++)
-    {	Dsymbol *s;
-
-	s = (Dsymbol *)members->data[i];
-	//printf("Module %s: %s.semantic3()\n", toChars(), s->toChars());
-	s->semantic3(sc);
-    }
-
-    sc = sc->pop();
-    sc->pop();
-    semanticdone = semanticstarted;
-}
-
-void Module::inlineScan()
-{   int i;
-
-    if (semanticstarted >= 4)
-	return;
-    assert(semanticstarted == 3);
-    semanticstarted = 4;
-
-    // Note that modules get their own scope, from scratch.
-    // This is so regardless of where in the syntax a module
-    // gets imported, it is unaffected by context.
-    //printf("Module = %p\n", sc.scopesym);
-
-    for (i = 0; i < members->dim; i++)
-    {	Dsymbol *s;
-
-	s = (Dsymbol *)members->data[i];
-	//if (global.params.verbose)
-	    //printf("inline scan symbol %s\n", s->toChars());
-
-	s->inlineScan();
-    }
-    semanticdone = semanticstarted;
-}
-
-/****************************************************
- */
-
-void Module::gensymfile()
-{
-    OutBuffer buf;
-    HdrGenState hgs;
-    int i;
-
-    //printf("Module::gensymfile()\n");
-
-    buf.printf("// Sym file generated from '%s'", srcfile->toChars());
-    buf.writenl();
-
-    for (i = 0; i < members->dim; i++)
-    {
-	Dsymbol *s;
-
-	s = (Dsymbol *)members->data[i];
-	s->toCBuffer(&buf, &hgs);
-    }
-
-    // Transfer image to file
-    symfile->setbuffer(buf.data, buf.offset);
-    buf.data = NULL;
-
-    symfile->writev();
-}
-
-/**********************************
- * Determine if we need to generate an instance of ModuleInfo
- * for this Module.
- */
-
-int Module::needModuleInfo()
-{
-    return needmoduleinfo || global.params.cov;
-}
-
-Dsymbol *Module::search(Loc loc, Identifier *ident, int flags)
-{
-    /* Since modules can be circularly referenced,
-     * need to stop infinite recursive searches.
-     */
-
-    //printf("%s Module::search('%s', flags = %d) insearch = %d\n", toChars(), ident->toChars(), flags, insearch);
-    Dsymbol *s;
-    if (insearch)
-	s = NULL;
-    else if (searchCacheIdent == ident && searchCacheFlags == flags)
-	s = searchCacheSymbol;
-    else
-    {
-	insearch = 1;
-	s = ScopeDsymbol::search(loc, ident, flags);
-	insearch = 0;
-
-	searchCacheIdent = ident;
-	searchCacheSymbol = s;
-	searchCacheFlags = flags;
-    }
-    return s;
-}
-
-/*******************************************
- * Can't run semantic on s now, try again later.
- */
-
-void Module::addDeferredSemantic(Dsymbol *s)
-{
-    // Don't add it if it is already there
-    for (int i = 0; i < deferred.dim; i++)
-    {
-	Dsymbol *sd = (Dsymbol *)deferred.data[i];
-
-	if (sd == s)
-	    return;
-    }
-
-    //printf("Module::addDeferredSemantic('%s')\n", s->toChars());
-    deferred.push(s);
-}
-
-
-/******************************************
- * Run semantic() on deferred symbols.
- */
-
-void Module::runDeferredSemantic()
-{
-    size_t len;
-
-    static int nested;
-    if (nested)
-	return;
-    //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
-    nested++;
-
-    do
-    {
-	dprogress = 0;
-	len = deferred.dim;
-	if (!len)
-	    break;
-
-	Dsymbol **todo;
-	Dsymbol *tmp;
-	if (len == 1)
-	{
-	    todo = &tmp;
-	}
-	else
-	{
-	    todo = (Dsymbol **)alloca(len * sizeof(Dsymbol *));
-	    assert(todo);
-	}
-	memcpy(todo, deferred.data, len * sizeof(Dsymbol *));
-	deferred.setDim(0);
-
-	for (int i = 0; i < len; i++)
-	{
-	    Dsymbol *s = todo[i];
-
-	    s->semantic(NULL);
-	    //printf("deferred: %s, parent = %s\n", s->toChars(), s->parent->toChars());
-	}
-	//printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress);
-    } while (deferred.dim < len || dprogress);	// while making progress
-    nested--;
-    //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
-}
-
-/* =========================== ModuleDeclaration ===================== */
-
-ModuleDeclaration::ModuleDeclaration(Array *packages, Identifier *id)
-{
-    this->packages = packages;
-    this->id = id;
-}
-
-char *ModuleDeclaration::toChars()
-{
-    OutBuffer buf;
-    int i;
-
-    if (packages && packages->dim)
-    {
-	for (i = 0; i < packages->dim; i++)
-	{   Identifier *pid = (Identifier *)packages->data[i];
-
-	    buf.writestring(pid->toChars());
-	    buf.writeByte('.');
-	}
-    }
-    buf.writestring(id->toChars());
-    buf.writeByte(0);
-    return (char *)buf.extractData();
-}
-
-/* =========================== Package ===================== */
-
-Package::Package(Identifier *ident)
-	: ScopeDsymbol(ident)
-{
-}
-
-
-char *Package::kind()
-{
-    return "package";
-}
-
-
-DsymbolTable *Package::resolve(Array *packages, Dsymbol **pparent, Package **ppkg)
-{
-    DsymbolTable *dst = Module::modules;
-    Dsymbol *parent = NULL;
-
-    //printf("Package::resolve()\n");
-    if (ppkg)
-	*ppkg = NULL;
-
-    if (packages)
-    {   int i;
-
-	for (i = 0; i < packages->dim; i++)
-	{   Identifier *pid = (Identifier *)packages->data[i];
-	    Dsymbol *p;
-
-	    p = dst->lookup(pid);
-	    if (!p)
-	    {
-		p = new Package(pid);
-		dst->insert(p);
-		p->parent = parent;
-		((ScopeDsymbol *)p)->symtab = new DsymbolTable();
-	    }
-	    else
-	    {
-		assert(p->isPackage());
-		if (p->isModule())
-		{   p->error("module and package have the same name");
-		    fatal();
-		    break;
-		}
-	    }
-	    parent = p;
-	    dst = ((Package *)p)->symtab;
-	    if (ppkg && !*ppkg)
-		*ppkg = (Package *)p;
-	}
-	if (pparent)
-	{
-	    *pparent = parent;
-	}
-    }
-    return dst;
-}
+
+// 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 _MSC_VER || __MINGW32__
+#include <malloc.h>
+#endif
+
+#if IN_GCC
+#include "gdc_alloca.h"
+#endif
+
+#include "mem.h"
+
+#include "mars.h"
+#include "module.h"
+#include "parse.h"
+#include "scope.h"
+#include "identifier.h"
+#include "id.h"
+#include "import.h"
+#include "dsymbol.h"
+#include "hdrgen.h"
+#include "lexer.h"
+
+#define MARS 1
+#include "html.h"
+
+#ifdef IN_GCC
+#include "d-dmd-gcc.h"
+#endif
+
+ClassDeclaration *Module::moduleinfo;
+
+Module *Module::rootModule;
+DsymbolTable *Module::modules;
+Array Module::amodules;
+
+Array Module::deferred;	// deferred Dsymbol's needing semantic() run on them
+unsigned Module::dprogress;
+
+void Module::init()
+{
+    modules = new DsymbolTable();
+}
+
+Module::Module(char *filename, Identifier *ident, int doDocComment, int doHdrGen)
+	: Package(ident)
+{
+    FileName *srcfilename;
+    FileName *cfilename;
+    FileName *hfilename;
+    FileName *objfilename;
+    FileName *llfilename;
+    FileName *bcfilename;
+    FileName *symfilename;
+
+//    printf("Module::Module(filename = '%s', ident = '%s')\n", filename, ident->toChars());
+    this->arg = filename;
+    md = NULL;
+    errors = 0;
+    numlines = 0;
+    members = NULL;
+    isHtml = 0;
+    isDocFile = 0;
+    needmoduleinfo = 0;
+#ifdef IN_GCC
+    strictlyneedmoduleinfo = 0;
+#endif
+    insearch = 0;
+    searchCacheIdent = NULL;
+    searchCacheSymbol = NULL;
+    searchCacheFlags = 0;
+    semanticstarted = 0;
+    semanticdone = 0;
+    decldefs = NULL;
+    vmoduleinfo = NULL;
+    massert = NULL;
+    marray = NULL;
+    sictor = NULL;
+    sctor = NULL;
+    sdtor = NULL;
+    stest = NULL;
+    sfilename = NULL;
+    root = 0;
+    importedFrom = NULL;
+    srcfile = NULL;
+    docfile = NULL;
+
+    debuglevel = 0;
+    debugids = NULL;
+    debugidsNot = NULL;
+    versionlevel = 0;
+    versionids = NULL;
+    versionidsNot = NULL;
+
+    macrotable = NULL;
+    escapetable = NULL;
+    doppelganger = 0;
+    cov = NULL;
+    covb = NULL;
+
+    srcfilename = FileName::defaultExt(filename, global.mars_ext);
+    if (!srcfilename->equalsExt(global.mars_ext) &&
+        !srcfilename->equalsExt("dd"))
+    {
+	if (srcfilename->equalsExt("html") ||
+	    srcfilename->equalsExt("htm")  ||
+	    srcfilename->equalsExt("xhtml"))
+	    isHtml = 1;
+	else
+	{   error("source file name '%s' must have .%s extension", srcfilename->toChars(), global.mars_ext);
+	    fatal();
+	}
+    }
+
+    char *argobj;
+    if (global.params.objname)
+	argobj = global.params.objname;
+    else if (global.params.preservePaths)
+	argobj = filename;
+    else
+	argobj = FileName::name(filename);
+    if (!FileName::absolute(argobj))
+    {
+	argobj = FileName::combine(global.params.objdir, argobj);
+    }
+
+    if (global.params.objname)
+	objfilename = new FileName(argobj, 0);
+    else
+    objfilename = FileName::forceExt(argobj, global.obj_ext);
+
+    llfilename = FileName::forceExt(argobj, global.ll_ext);
+    bcfilename = FileName::forceExt(argobj, global.bc_ext);
+
+    symfilename = FileName::forceExt(filename, global.sym_ext);
+
+    srcfile = new File(srcfilename);
+
+    if (doDocComment)
+    {
+	setDocfile();
+    }
+
+    if (doHdrGen)
+    {
+	setHdrfile();
+    }
+
+    objfile = new File(objfilename);
+    bcfile = new File(bcfilename);
+    llfile = new File(llfilename);
+    symfile = new File(symfilename);
+}
+
+void Module::setDocfile()
+{
+    FileName *docfilename;
+    char *argdoc;
+
+    if (global.params.docname)
+	argdoc = global.params.docname;
+    else if (global.params.preservePaths)
+	argdoc = (char *)arg;
+    else
+	argdoc = FileName::name((char *)arg);
+    if (!FileName::absolute(argdoc))
+    {	//FileName::ensurePathExists(global.params.docdir);
+	argdoc = FileName::combine(global.params.docdir, argdoc);
+    }
+    if (global.params.docname)
+	docfilename = new FileName(argdoc, 0);
+    else
+	docfilename = FileName::forceExt(argdoc, global.doc_ext);
+
+    if (docfilename->equals(srcfile->name))
+    {   error("Source file and documentation file have same name '%s'", srcfile->name->str);
+	fatal();
+    }
+
+    docfile = new File(docfilename);
+}
+
+void Module::setHdrfile()
+{
+    FileName *hdrfilename;
+    char *arghdr;
+
+    if (global.params.hdrname)
+	arghdr = global.params.hdrname;
+    else if (global.params.preservePaths)
+	arghdr = (char *)arg;
+    else
+	arghdr = FileName::name((char *)arg);
+    if (!FileName::absolute(arghdr))
+    {	//FileName::ensurePathExists(global.params.hdrdir);
+	arghdr = FileName::combine(global.params.hdrdir, arghdr);
+    }
+    if (global.params.hdrname)
+	hdrfilename = new FileName(arghdr, 0);
+    else
+	hdrfilename = FileName::forceExt(arghdr, global.hdr_ext);
+
+    if (hdrfilename->equals(srcfile->name))
+    {   error("Source file and 'header' file have same name '%s'", srcfile->name->str);
+	fatal();
+    }
+
+    hdrfile = new File(hdrfilename);
+}
+
+void Module::deleteObjFile()
+{
+    if (global.params.obj)
+	objfile->remove();
+    //if (global.params.llvmBC)
+    bcfile->remove();
+    if (docfile)
+	docfile->remove();
+}
+
+Module::~Module()
+{
+}
+
+const char *Module::kind()
+{
+    return "module";
+}
+
+Module *Module::load(Loc loc, Array *packages, Identifier *ident)
+{   Module *m;
+    char *filename;
+
+    //printf("Module::load(ident = '%s')\n", ident->toChars());
+
+    // Build module filename by turning:
+    //	foo.bar.baz
+    // into:
+    //	foo\bar\baz
+    filename = ident->toChars();
+    if (packages && packages->dim)
+    {
+	OutBuffer buf;
+	int i;
+
+	for (i = 0; i < packages->dim; i++)
+	{   Identifier *pid = (Identifier *)packages->data[i];
+
+	    buf.writestring(pid->toChars());
+#if _WIN32
+	    buf.writeByte('\\');
+#else
+	    buf.writeByte('/');
+#endif
+	}
+	buf.writestring(filename);
+	buf.writeByte(0);
+	filename = (char *)buf.extractData();
+    }
+
+    m = new Module(filename, ident, 0, 0);
+    m->loc = loc;
+
+    /* Search along global.path for .di file, then .d file.
+     */
+    char *result = NULL;
+    FileName *fdi = FileName::forceExt(filename, global.hdr_ext);
+    FileName *fd  = FileName::forceExt(filename, global.mars_ext);
+    char *sdi = fdi->toChars();
+    char *sd  = fd->toChars();
+
+    if (FileName::exists(sdi))
+	result = sdi;
+    else if (FileName::exists(sd))
+	result = sd;
+    else if (FileName::absolute(filename))
+	;
+    else if (!global.path)
+	;
+    else
+    {
+	for (size_t i = 0; i < global.path->dim; i++)
+	{
+	    char *p = (char *)global.path->data[i];
+	    char *n = FileName::combine(p, sdi);
+	    if (FileName::exists(n))
+	    {	result = n;
+		break;
+	    }
+	    mem.free(n);
+	    n = FileName::combine(p, sd);
+	    if (FileName::exists(n))
+	    {	result = n;
+		break;
+	    }
+	    mem.free(n);
+	}
+    }
+    if (result)
+	m->srcfile = new File(result);
+
+    if (global.params.verbose)
+    {
+	printf("import    ");
+	if (packages)
+	{
+	    for (size_t i = 0; i < packages->dim; i++)
+	    {   Identifier *pid = (Identifier *)packages->data[i];
+		printf("%s.", pid->toChars());
+	    }
+	}
+	printf("%s\t(%s)\n", ident->toChars(), m->srcfile->toChars());
+    }
+
+    m->read(loc);
+    m->parse();
+
+#ifdef IN_GCC
+    d_gcc_magic_module(m);
+#endif
+
+    return m;
+}
+
+void Module::read(Loc loc)
+{
+    //printf("Module::read('%s') file '%s'\n", toChars(), srcfile->toChars());
+    if (srcfile->read())
+    {	error(loc, "cannot read file '%s'", srcfile->toChars());
+	fatal();
+    }
+}
+
+inline unsigned readwordLE(unsigned short *p)
+{
+#if __I86__
+    return *p;
+#else
+    return (((unsigned char *)p)[1] << 8) | ((unsigned char *)p)[0];
+#endif
+}
+
+inline unsigned readwordBE(unsigned short *p)
+{
+    return (((unsigned char *)p)[0] << 8) | ((unsigned char *)p)[1];
+}
+
+inline unsigned readlongLE(unsigned *p)
+{
+#if __I86__
+    return *p;
+#else
+    return ((unsigned char *)p)[0] |
+	(((unsigned char *)p)[1] << 8) |
+	(((unsigned char *)p)[2] << 16) |
+	(((unsigned char *)p)[3] << 24);
+#endif
+}
+
+inline unsigned readlongBE(unsigned *p)
+{
+    return ((unsigned char *)p)[3] |
+	(((unsigned char *)p)[2] << 8) |
+	(((unsigned char *)p)[1] << 16) |
+	(((unsigned char *)p)[0] << 24);
+}
+
+#if IN_GCC
+void Module::parse(bool dump_source)
+#else
+void Module::parse()
+#endif
+{   char *srcname;
+    unsigned char *buf;
+    unsigned buflen;
+    unsigned le;
+    unsigned bom;
+
+    //printf("Module::parse()\n");
+
+    srcname = srcfile->name->toChars();
+    //printf("Module::parse(srcname = '%s')\n", srcname);
+
+    buf = srcfile->buffer;
+    buflen = srcfile->len;
+
+    if (buflen >= 2)
+    {
+	/* Convert all non-UTF-8 formats to UTF-8.
+	 * BOM : http://www.unicode.org/faq/utf_bom.html
+	 * 00 00 FE FF	UTF-32BE, big-endian
+	 * FF FE 00 00	UTF-32LE, little-endian
+	 * FE FF	UTF-16BE, big-endian
+	 * FF FE	UTF-16LE, little-endian
+	 * EF BB BF	UTF-8
+	 */
+
+	bom = 1;		// assume there's a BOM
+	if (buf[0] == 0xFF && buf[1] == 0xFE)
+	{
+	    if (buflen >= 4 && buf[2] == 0 && buf[3] == 0)
+	    {	// UTF-32LE
+		le = 1;
+
+	    Lutf32:
+		OutBuffer dbuf;
+		unsigned *pu = (unsigned *)(buf);
+		unsigned *pumax = &pu[buflen / 4];
+
+		if (buflen & 3)
+		{   error("odd length of UTF-32 char source %u", buflen);
+		    fatal();
+		}
+
+		dbuf.reserve(buflen / 4);
+		for (pu += bom; pu < pumax; pu++)
+		{   unsigned u;
+
+		    u = le ? readlongLE(pu) : readlongBE(pu);
+		    if (u & ~0x7F)
+		    {
+			if (u > 0x10FFFF)
+			{   error("UTF-32 value %08x greater than 0x10FFFF", u);
+			    fatal();
+			}
+			dbuf.writeUTF8(u);
+		    }
+		    else
+			dbuf.writeByte(u);
+		}
+		dbuf.writeByte(0);		// add 0 as sentinel for scanner
+		buflen = dbuf.offset - 1;	// don't include sentinel in count
+		buf = (unsigned char *) dbuf.extractData();
+	    }
+	    else
+	    {   // UTF-16LE (X86)
+		// Convert it to UTF-8
+		le = 1;
+
+	    Lutf16:
+		OutBuffer dbuf;
+		unsigned short *pu = (unsigned short *)(buf);
+		unsigned short *pumax = &pu[buflen / 2];
+
+		if (buflen & 1)
+		{   error("odd length of UTF-16 char source %u", buflen);
+		    fatal();
+		}
+
+		dbuf.reserve(buflen / 2);
+		for (pu += bom; pu < pumax; pu++)
+		{   unsigned u;
+
+		    u = le ? readwordLE(pu) : readwordBE(pu);
+		    if (u & ~0x7F)
+		    {	if (u >= 0xD800 && u <= 0xDBFF)
+			{   unsigned u2;
+
+			    if (++pu > pumax)
+			    {   error("surrogate UTF-16 high value %04x at EOF", u);
+				fatal();
+			    }
+			    u2 = le ? readwordLE(pu) : readwordBE(pu);
+			    if (u2 < 0xDC00 || u2 > 0xDFFF)
+			    {   error("surrogate UTF-16 low value %04x out of range", u2);
+				fatal();
+			    }
+			    u = (u - 0xD7C0) << 10;
+			    u |= (u2 - 0xDC00);
+			}
+			else if (u >= 0xDC00 && u <= 0xDFFF)
+			{   error("unpaired surrogate UTF-16 value %04x", u);
+			    fatal();
+			}
+			else if (u == 0xFFFE || u == 0xFFFF)
+			{   error("illegal UTF-16 value %04x", u);
+			    fatal();
+			}
+			dbuf.writeUTF8(u);
+		    }
+		    else
+			dbuf.writeByte(u);
+		}
+		dbuf.writeByte(0);		// add 0 as sentinel for scanner
+		buflen = dbuf.offset - 1;	// don't include sentinel in count
+		buf = (unsigned char *) dbuf.extractData();
+	    }
+	}
+	else if (buf[0] == 0xFE && buf[1] == 0xFF)
+	{   // UTF-16BE
+	    le = 0;
+	    goto Lutf16;
+	}
+	else if (buflen >= 4 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF)
+	{   // UTF-32BE
+	    le = 0;
+	    goto Lutf32;
+	}
+	else if (buflen >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF)
+	{   // UTF-8
+
+	    buf += 3;
+	    buflen -= 3;
+	}
+	else
+	{
+	    /* There is no BOM. Make use of Arcane Jill's insight that
+	     * the first char of D source must be ASCII to
+	     * figure out the encoding.
+	     */
+
+	    bom = 0;
+	    if (buflen >= 4)
+	    {   if (buf[1] == 0 && buf[2] == 0 && buf[3] == 0)
+		{   // UTF-32LE
+		    le = 1;
+		    goto Lutf32;
+		}
+		else if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0)
+		{   // UTF-32BE
+		    le = 0;
+		    goto Lutf32;
+		}
+	    }
+	    if (buflen >= 2)
+	    {
+		if (buf[1] == 0)
+		{   // UTF-16LE
+		    le = 1;
+		    goto Lutf16;
+		}
+		else if (buf[0] == 0)
+		{   // UTF-16BE
+		    le = 0;
+		    goto Lutf16;
+		}
+	    }
+
+	    // It's UTF-8
+	    if (buf[0] >= 0x80)
+	    {	error("source file must start with BOM or ASCII character, not \\x%02X", buf[0]);
+		fatal();
+	    }
+	}
+    }
+
+#ifdef IN_GCC
+    // dump utf-8 encoded source
+    if (dump_source)
+    {	// %% srcname could contain a path ...
+	d_gcc_dump_source(srcname, "utf-8", buf, buflen);
+    }
+#endif
+
+    /* If it starts with the string "Ddoc", then it's a documentation
+     * source file.
+     */
+    if (buflen >= 4 && memcmp(buf, "Ddoc", 4) == 0)
+    {
+	comment = buf + 4;
+	isDocFile = 1;
+	if (!docfile)
+	    setDocfile();
+	return;
+    }
+    if (isHtml)
+    {
+	OutBuffer *dbuf = new OutBuffer();
+	Html h(srcname, buf, buflen);
+	h.extractCode(dbuf);
+	buf = dbuf->data;
+	buflen = dbuf->offset;
+#ifdef IN_GCC
+	// dump extracted source
+	if (dump_source)
+	    d_gcc_dump_source(srcname, "d.utf-8", buf, buflen);
+#endif
+    }
+    Parser p(this, buf, buflen, docfile != NULL);
+    p.nextToken();
+    members = p.parseModule();
+    md = p.md;
+    numlines = p.loc.linnum;
+
+    DsymbolTable *dst;
+
+    if (md)
+    {	this->ident = md->id;
+	dst = Package::resolve(md->packages, &this->parent, NULL);
+    }
+    else
+    {
+	dst = modules;
+
+	/* Check to see if module name is a valid identifier
+	 */
+	if (!Lexer::isValidIdentifier(this->ident->toChars()))
+	    error("has non-identifier characters in filename, use module declaration instead");
+    }
+
+    // Update global list of modules
+    if (!dst->insert(this))
+    {
+	if (md)
+	    error(loc, "is in multiple packages %s", md->toChars());
+	else
+	    error(loc, "is in multiple defined");
+    }
+    else
+    {
+	amodules.push(this);
+    }
+}
+
+void Module::semantic()
+{   int i;
+
+    if (semanticstarted)
+	return;
+
+    //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
+    semanticstarted = 1;
+
+    // Note that modules get their own scope, from scratch.
+    // This is so regardless of where in the syntax a module
+    // gets imported, it is unaffected by context.
+    Scope *sc = Scope::createGlobal(this);	// create root scope
+
+    //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage);
+
+    // Add import of "object" if this module isn't "object"
+    if (ident != Id::object)
+    {
+	Import *im = new Import(0, NULL, Id::object, NULL, 0);
+	members->shift(im);
+    }
+
+    // Add all symbols into module's symbol table
+    symtab = new DsymbolTable();
+    for (i = 0; i < members->dim; i++)
+    {	Dsymbol *s;
+
+	s = (Dsymbol *)members->data[i];
+	s->addMember(NULL, sc->scopesym, 1);
+    }
+
+    // Pass 1 semantic routines: do public side of the definition
+    for (i = 0; i < members->dim; i++)
+    {	Dsymbol *s;
+
+	s = (Dsymbol *)members->data[i];
+	//printf("\tModule('%s'): '%s'.semantic()\n", toChars(), s->toChars());
+	s->semantic(sc);
+	runDeferredSemantic();
+    }
+
+    sc = sc->pop();
+    sc->pop();
+    semanticdone = semanticstarted;
+    //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent);
+}
+
+void Module::semantic2()
+{   int i;
+
+    if (deferred.dim)
+    {
+	for (int i = 0; i < deferred.dim; i++)
+	{
+	    Dsymbol *sd = (Dsymbol *)deferred.data[i];
+
+	    sd->error("unable to resolve forward reference in definition");
+	}
+	return;
+    }
+    //printf("Module::semantic2('%s'): parent = %p\n", toChars(), parent);
+    if (semanticstarted >= 2)
+	return;
+    assert(semanticstarted == 1);
+    semanticstarted = 2;
+
+    // Note that modules get their own scope, from scratch.
+    // This is so regardless of where in the syntax a module
+    // gets imported, it is unaffected by context.
+    Scope *sc = Scope::createGlobal(this);	// create root scope
+    //printf("Module = %p\n", sc.scopesym);
+
+    // Pass 2 semantic routines: do initializers and function bodies
+    for (i = 0; i < members->dim; i++)
+    {	Dsymbol *s;
+
+	s = (Dsymbol *)members->data[i];
+	s->semantic2(sc);
+    }
+
+    sc = sc->pop();
+    sc->pop();
+    semanticdone = semanticstarted;
+    //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), parent);
+}
+
+void Module::semantic3()
+{   int i;
+
+    //printf("Module::semantic3('%s'): parent = %p\n", toChars(), parent);
+    if (semanticstarted >= 3)
+	return;
+    assert(semanticstarted == 2);
+    semanticstarted = 3;
+
+    // Note that modules get their own scope, from scratch.
+    // This is so regardless of where in the syntax a module
+    // gets imported, it is unaffected by context.
+    Scope *sc = Scope::createGlobal(this);	// create root scope
+    //printf("Module = %p\n", sc.scopesym);
+
+    // Pass 3 semantic routines: do initializers and function bodies
+    for (i = 0; i < members->dim; i++)
+    {	Dsymbol *s;
+
+	s = (Dsymbol *)members->data[i];
+	//printf("Module %s: %s.semantic3()\n", toChars(), s->toChars());
+	s->semantic3(sc);
+    }
+
+    sc = sc->pop();
+    sc->pop();
+    semanticdone = semanticstarted;
+}
+
+void Module::inlineScan()
+{   int i;
+
+    if (semanticstarted >= 4)
+	return;
+    assert(semanticstarted == 3);
+    semanticstarted = 4;
+
+    // Note that modules get their own scope, from scratch.
+    // This is so regardless of where in the syntax a module
+    // gets imported, it is unaffected by context.
+    //printf("Module = %p\n", sc.scopesym);
+
+    for (i = 0; i < members->dim; i++)
+    {	Dsymbol *s;
+
+	s = (Dsymbol *)members->data[i];
+	//if (global.params.verbose)
+	    //printf("inline scan symbol %s\n", s->toChars());
+
+	s->inlineScan();
+    }
+    semanticdone = semanticstarted;
+}
+
+/****************************************************
+ */
+
+void Module::gensymfile()
+{
+    OutBuffer buf;
+    HdrGenState hgs;
+
+    //printf("Module::gensymfile()\n");
+
+    buf.printf("// Sym file generated from '%s'", srcfile->toChars());
+    buf.writenl();
+
+    for (int i = 0; i < members->dim; i++)
+    {	Dsymbol *s = (Dsymbol *)members->data[i];
+
+	s->toCBuffer(&buf, &hgs);
+    }
+
+    // Transfer image to file
+    symfile->setbuffer(buf.data, buf.offset);
+    buf.data = NULL;
+
+    symfile->writev();
+}
+
+/**********************************
+ * Determine if we need to generate an instance of ModuleInfo
+ * for this Module.
+ */
+
+int Module::needModuleInfo()
+{
+    return needmoduleinfo || global.params.cov;
+}
+
+Dsymbol *Module::search(Loc loc, Identifier *ident, int flags)
+{
+    /* Since modules can be circularly referenced,
+     * need to stop infinite recursive searches.
+     */
+
+    //printf("%s Module::search('%s', flags = %d) insearch = %d\n", toChars(), ident->toChars(), flags, insearch);
+    Dsymbol *s;
+    if (insearch)
+	s = NULL;
+    else if (searchCacheIdent == ident && searchCacheFlags == flags)
+	s = searchCacheSymbol;
+    else
+    {
+	insearch = 1;
+	s = ScopeDsymbol::search(loc, ident, flags);
+	insearch = 0;
+
+	searchCacheIdent = ident;
+	searchCacheSymbol = s;
+	searchCacheFlags = flags;
+    }
+    return s;
+}
+
+/*******************************************
+ * Can't run semantic on s now, try again later.
+ */
+
+void Module::addDeferredSemantic(Dsymbol *s)
+{
+    // Don't add it if it is already there
+    for (int i = 0; i < deferred.dim; i++)
+    {
+	Dsymbol *sd = (Dsymbol *)deferred.data[i];
+
+	if (sd == s)
+	    return;
+    }
+
+    //printf("Module::addDeferredSemantic('%s')\n", s->toChars());
+    deferred.push(s);
+}
+
+
+/******************************************
+ * Run semantic() on deferred symbols.
+ */
+
+void Module::runDeferredSemantic()
+{
+    size_t len;
+
+    static int nested;
+    if (nested)
+	return;
+    //if (deferred.dim) printf("+Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
+    nested++;
+
+    do
+    {
+	dprogress = 0;
+	len = deferred.dim;
+	if (!len)
+	    break;
+
+	Dsymbol **todo;
+	Dsymbol *tmp;
+	if (len == 1)
+	{
+	    todo = &tmp;
+	}
+	else
+	{
+	    todo = (Dsymbol **)alloca(len * sizeof(Dsymbol *));
+	    assert(todo);
+	}
+	memcpy(todo, deferred.data, len * sizeof(Dsymbol *));
+	deferred.setDim(0);
+
+	for (int i = 0; i < len; i++)
+	{
+	    Dsymbol *s = todo[i];
+
+	    s->semantic(NULL);
+	    //printf("deferred: %s, parent = %s\n", s->toChars(), s->parent->toChars());
+	}
+	//printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress);
+    } while (deferred.dim < len || dprogress);	// while making progress
+    nested--;
+    //printf("-Module::runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim);
+}
+
+/* =========================== ModuleDeclaration ===================== */
+
+ModuleDeclaration::ModuleDeclaration(Array *packages, Identifier *id)
+{
+    this->packages = packages;
+    this->id = id;
+}
+
+char *ModuleDeclaration::toChars()
+{
+    OutBuffer buf;
+    int i;
+
+    if (packages && packages->dim)
+    {
+	for (i = 0; i < packages->dim; i++)
+	{   Identifier *pid = (Identifier *)packages->data[i];
+
+	    buf.writestring(pid->toChars());
+	    buf.writeByte('.');
+	}
+    }
+    buf.writestring(id->toChars());
+    buf.writeByte(0);
+    return (char *)buf.extractData();
+}
+
+/* =========================== Package ===================== */
+
+Package::Package(Identifier *ident)
+	: ScopeDsymbol(ident)
+{
+}
+
+
+const char *Package::kind()
+{
+    return "package";
+}
+
+
+DsymbolTable *Package::resolve(Array *packages, Dsymbol **pparent, Package **ppkg)
+{
+    DsymbolTable *dst = Module::modules;
+    Dsymbol *parent = NULL;
+
+    //printf("Package::resolve()\n");
+    if (ppkg)
+	*ppkg = NULL;
+
+    if (packages)
+    {   int i;
+
+	for (i = 0; i < packages->dim; i++)
+	{   Identifier *pid = (Identifier *)packages->data[i];
+	    Dsymbol *p;
+
+	    p = dst->lookup(pid);
+	    if (!p)
+	    {
+		p = new Package(pid);
+		dst->insert(p);
+		p->parent = parent;
+		((ScopeDsymbol *)p)->symtab = new DsymbolTable();
+	    }
+	    else
+	    {
+		assert(p->isPackage());
+		if (p->isModule())
+		{   p->error("module and package have the same name");
+		    fatal();
+		    break;
+		}
+	    }
+	    parent = p;
+	    dst = ((Package *)p)->symtab;
+	    if (ppkg && !*ppkg)
+		*ppkg = (Package *)p;
+	}
+	if (pparent)
+	{
+	    *pparent = parent;
+	}
+    }
+    return dst;
+}
--- a/dmd/module.h	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/module.h	Sat Jul 12 19:38:31 2008 +0200
@@ -1,185 +1,187 @@
-
-// Compiler implementation of the D programming language
-// Copyright (c) 1999-2005 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_MODULE_H
-#define DMD_MODULE_H
-
-#ifdef __DMC__
-#pragma once
-#endif /* __DMC__ */
-
-#include "root.h"
-#include "dsymbol.h"
-
-struct ModuleInfoDeclaration;
-struct ClassDeclaration;
-struct ModuleDeclaration;
-struct Macro;
-struct Escape;
-struct VarDeclaration;
-
-// Back end
-#if IN_LLVM
-struct DValue;
-typedef DValue elem;
-#else
-#ifdef IN_GCC
-union tree_node; typedef union tree_node elem;
-#else
-struct elem;
-#endif
-#endif
-
-struct Package : ScopeDsymbol
-{
-    Package(Identifier *ident);
-    char *kind();
-
-    static DsymbolTable *resolve(Array *packages, Dsymbol **pparent, Package **ppkg);
-
-    Package *isPackage() { return this; }
-
-    virtual void semantic(Scope *sc) { }
-};
-
-struct Module : Package
-{
-    static Module *rootModule;
-    static DsymbolTable *modules;	// symbol table of all modules
-    static Array amodules;		// array of all modules
-    static Array deferred;	// deferred Dsymbol's needing semantic() run on them
-    static unsigned dprogress;	// progress resolving the deferred list
-    static void init();
-
-    static ClassDeclaration *moduleinfo;
-
-
-    const char *arg;	// original argument name
-    ModuleDeclaration *md; // if !NULL, the contents of the ModuleDeclaration declaration
-    File *srcfile;	// input source file
-    File *objfile;	// output .obj file
-    File *bcfile;  // output .bc file
-    File *llfile;  // output .ll file
-    File *hdrfile;	// 'header' file
-    File *symfile;	// output symbol file
-    File *docfile;	// output documentation file
-    unsigned errors;	// if any errors in file
-    unsigned numlines;	// number of lines in source file
-    int isHtml;		// if it is an HTML file
-    int isDocFile;	// if it is a documentation input file, not D source
-    int needmoduleinfo;
-#ifdef IN_GCC
-    int strictlyneedmoduleinfo;
-#endif
-
-    int insearch;
-    Identifier *searchCacheIdent;
-    Dsymbol *searchCacheSymbol;	// cached value of search
-    int searchCacheFlags;	// cached flags
-
-    int semanticstarted;	// has semantic() been started?
-    int semanticdone;		// has semantic() been done?
-    int root;			// != 0 if this is a 'root' module,
-				// i.e. a module that will be taken all the
-				// way to an object file
-    Module *importedFrom;	// module from command line we're imported from,
-				// i.e. a module that will be taken all the
-				// way to an object file
-
-    Array *decldefs;		// top level declarations for this Module
-
-    Array aimports;		// all imported modules
-
-    ModuleInfoDeclaration *vmoduleinfo;
-
-    unsigned debuglevel;	// debug level
-    Array *debugids;		// debug identifiers
-    Array *debugidsNot;		// forward referenced debug identifiers
-
-    unsigned versionlevel;	// version level
-    Array *versionids;		// version identifiers
-    Array *versionidsNot;	// forward referenced version identifiers
-
-    Macro *macrotable;		// document comment macros
-    Escape *escapetable;	// document comment escapes
-
-    Module(char *arg, Identifier *ident, int doDocComment, int doHdrGen);
-    ~Module();
-
-    static Module *load(Loc loc, Array *packages, Identifier *ident);
-
-    void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
-    char *kind();
-    void setDocfile();	// set docfile member
-    void read(Loc loc);	// read file
-#if IN_GCC
-    void parse(bool dump_source = false);	// syntactic parse
-#else
-    void parse();	// syntactic parse
-#endif
-    void semantic();	// semantic analysis
-    void semantic2();	// pass 2 semantic analysis
-    void semantic3();	// pass 3 semantic analysis
-    void inlineScan();	// scan for functions to inline
-    void setHdrfile();	// set hdrfile member
-#ifdef _DH
-    void genhdrfile();  // generate D import file
-#endif
-    void genobjfile();
-    void gensymfile();
-    void gendocfile();
-    int needModuleInfo();
-    Dsymbol *search(Loc loc, Identifier *ident, int flags);
-    void deleteObjFile();
-    void addDeferredSemantic(Dsymbol *s);
-    void runDeferredSemantic();
-
-    // Back end
-
-    Symbol *cov;		// private uint[] __coverage;
-    unsigned *covb;		// bit array of valid code line numbers
-
-    Symbol *sictor;		// module order independent constructor
-    Symbol *sctor;		// module constructor
-    Symbol *sdtor;		// module destructor
-    Symbol *stest;		// module unit test
-
-    Symbol *sfilename;		// symbol for filename
-
-    Symbol *massert;		// module assert function
-    Symbol *toModuleAssert();	// get module assert function
-
-    Symbol *marray;		// module array bounds function
-    Symbol *toModuleArray();	// get module array bounds function
-
-
-    static Symbol *gencritsec();
-    elem *toEfilename();
-    elem *toEmodulename();
-
-    Symbol *toSymbol();
-    void genmoduleinfo();
-
-    // LLVMDC
-    Module *isModule() { return this; }
-};
-
-
-struct ModuleDeclaration
-{
-    Identifier *id;
-    Array *packages;		// array of Identifier's representing packages
-
-    ModuleDeclaration(Array *packages, Identifier *id);
-
-    char *toChars();
-};
-
-#endif /* DMD_MODULE_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_MODULE_H
+#define DMD_MODULE_H
+
+#ifdef __DMC__
+#pragma once
+#endif /* __DMC__ */
+
+#include "root.h"
+#include "dsymbol.h"
+
+struct ModuleInfoDeclaration;
+struct ClassDeclaration;
+struct ModuleDeclaration;
+struct Macro;
+struct Escape;
+struct VarDeclaration;
+struct Library;
+
+// Back end
+#if IN_LLVM
+struct DValue;
+typedef DValue elem;
+#else
+#ifdef IN_GCC
+union tree_node; typedef union tree_node elem;
+#else
+struct elem;
+#endif
+#endif
+
+struct Package : ScopeDsymbol
+{
+    Package(Identifier *ident);
+    const char *kind();
+
+    static DsymbolTable *resolve(Array *packages, Dsymbol **pparent, Package **ppkg);
+
+    Package *isPackage() { return this; }
+
+    virtual void semantic(Scope *sc) { }
+};
+
+struct Module : Package
+{
+    static Module *rootModule;
+    static DsymbolTable *modules;	// symbol table of all modules
+    static Array amodules;		// array of all modules
+    static Array deferred;	// deferred Dsymbol's needing semantic() run on them
+    static unsigned dprogress;	// progress resolving the deferred list
+    static void init();
+
+    static ClassDeclaration *moduleinfo;
+
+
+    const char *arg;	// original argument name
+    ModuleDeclaration *md; // if !NULL, the contents of the ModuleDeclaration declaration
+    File *srcfile;	// input source file
+    File *objfile;	// output .obj file
+    File *bcfile;  // output .bc file
+    File *llfile;  // output .ll file
+    File *hdrfile;	// 'header' file
+    File *symfile;	// output symbol file
+    File *docfile;	// output documentation file
+    unsigned errors;	// if any errors in file
+    unsigned numlines;	// number of lines in source file
+    int isHtml;		// if it is an HTML file
+    int isDocFile;	// if it is a documentation input file, not D source
+    int needmoduleinfo;
+#ifdef IN_GCC
+    int strictlyneedmoduleinfo;
+#endif
+
+    int insearch;
+    Identifier *searchCacheIdent;
+    Dsymbol *searchCacheSymbol;	// cached value of search
+    int searchCacheFlags;	// cached flags
+
+    int semanticstarted;	// has semantic() been started?
+    int semanticdone;		// has semantic() been done?
+    int root;			// != 0 if this is a 'root' module,
+				// i.e. a module that will be taken all the
+				// way to an object file
+    Module *importedFrom;	// module from command line we're imported from,
+				// i.e. a module that will be taken all the
+				// way to an object file
+
+    Array *decldefs;		// top level declarations for this Module
+
+    Array aimports;		// all imported modules
+
+    ModuleInfoDeclaration *vmoduleinfo;
+
+    unsigned debuglevel;	// debug level
+    Array *debugids;		// debug identifiers
+    Array *debugidsNot;		// forward referenced debug identifiers
+
+    unsigned versionlevel;	// version level
+    Array *versionids;		// version identifiers
+    Array *versionidsNot;	// forward referenced version identifiers
+
+    Macro *macrotable;		// document comment macros
+    Escape *escapetable;	// document comment escapes
+
+    Module(char *arg, Identifier *ident, int doDocComment, int doHdrGen);
+    ~Module();
+
+    static Module *load(Loc loc, Array *packages, Identifier *ident);
+
+    void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
+    const char *kind();
+    void setDocfile();	// set docfile member
+    void read(Loc loc);	// read file
+#if IN_GCC
+    void parse(bool dump_source = false);	// syntactic parse
+#else
+    void parse();	// syntactic parse
+#endif
+    void semantic();	// semantic analysis
+    void semantic2();	// pass 2 semantic analysis
+    void semantic3();	// pass 3 semantic analysis
+    void inlineScan();	// scan for functions to inline
+    void setHdrfile();	// set hdrfile member
+#ifdef _DH
+    void genhdrfile();  // generate D import file
+#endif
+    void genobjfile(int multiobj);
+    void gensymfile();
+    void gendocfile();
+    int needModuleInfo();
+    Dsymbol *search(Loc loc, Identifier *ident, int flags);
+    void deleteObjFile();
+    void addDeferredSemantic(Dsymbol *s);
+    void runDeferredSemantic();
+
+    // Back end
+
+    int doppelganger;		// sub-module
+    Symbol *cov;		// private uint[] __coverage;
+    unsigned *covb;		// bit array of valid code line numbers
+
+    Symbol *sictor;		// module order independent constructor
+    Symbol *sctor;		// module constructor
+    Symbol *sdtor;		// module destructor
+    Symbol *stest;		// module unit test
+
+    Symbol *sfilename;		// symbol for filename
+
+    Symbol *massert;		// module assert function
+    Symbol *toModuleAssert();	// get module assert function
+
+    Symbol *marray;		// module array bounds function
+    Symbol *toModuleArray();	// get module array bounds function
+
+
+    static Symbol *gencritsec();
+    elem *toEfilename();
+    elem *toEmodulename();
+
+    Symbol *toSymbol();
+    void genmoduleinfo();
+
+    // LLVMDC
+    Module *isModule() { return this; }
+};
+
+
+struct ModuleDeclaration
+{
+    Identifier *id;
+    Array *packages;		// array of Identifier's representing packages
+
+    ModuleDeclaration(Array *packages, Identifier *id);
+
+    char *toChars();
+};
+
+#endif /* DMD_MODULE_H */
--- a/dmd/mtype.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/mtype.c	Sat Jul 12 19:38:31 2008 +0200
@@ -115,7 +115,7 @@
     this->mod = 0;
     this->next = next;
     this->deco = NULL;
-#if V2
+#if DMDV2
     this->cto = NULL;
     this->ito = NULL;
 #endif
@@ -2226,6 +2226,35 @@
     return merge();
 }
 
+void TypeAArray::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps)
+{
+    //printf("TypeAArray::resolve() %s\n", toChars());
+
+    // Deal with the case where we thought the index was a type, but
+    // in reality it was an expression.
+    if (index->ty == Tident || index->ty == Tinstance || index->ty == Tsarray)
+    {
+	Expression *e;
+	Type *t;
+	Dsymbol *s;
+
+	index->resolve(loc, sc, &e, &t, &s);
+	if (e)
+	{   // It was an expression -
+	    // Rewrite as a static array
+
+	    TypeSArray *tsa = new TypeSArray(next, e);
+	    return tsa->resolve(loc, sc, pe, pt, ps);
+	}
+	else if (t)
+	    index = t;
+	else
+	    index->error(loc, "index is not a type or an expression");
+    }
+    Type::resolve(loc, sc, pe, pt, ps);
+}
+
+
 Expression *TypeAArray::dotExp(Scope *sc, Expression *e, Identifier *ident)
 {
 #if LOGDOTEXP
@@ -3151,6 +3180,7 @@
     if (s)
     {
 	//printf("\t1: s = '%s' %p, kind = '%s'\n",s->toChars(), s, s->kind());
+	s->checkDeprecated(loc, sc);		// check for deprecated aliases
 	s = s->toAlias();
 	//printf("\t2: s = '%s' %p, kind = '%s'\n",s->toChars(), s, s->kind());
 	for (i = 0; i < idents.dim; i++)
@@ -3548,7 +3578,7 @@
 {
     Type *t;
 
-    t = semantic(0, sc);
+    t = semantic(loc, sc);
     if (t == this)
 	return NULL;
     return t->toDsymbol(sc);
@@ -3624,6 +3654,10 @@
 	sc->intypeof++;
 	exp = exp->semantic(sc);
 	sc->intypeof--;
+	if (exp->op == TOKtype)
+	{
+	    error(loc, "argument %s to typeof is not an expression", exp->toChars());
+	}
 	t = exp->type;
 	if (!t)
 	{
@@ -4174,6 +4208,7 @@
     {
 	/* Create a TupleExp
 	 */
+	e = e->semantic(sc);	// do this before turning on noaccesscheck
 	Expressions *exps = new Expressions;
 	exps->reserve(sym->fields.dim);
 	for (size_t i = 0; i < sym->fields.dim; i++)
@@ -4182,7 +4217,10 @@
 	    exps->push(fe);
 	}
 	e = new TupleExp(e->loc, exps);
+	sc = sc->push();
+	sc->noaccesscheck = 1;
 	e = e->semantic(sc);
+	sc->pop();
 	return e;
     }
 
@@ -4206,6 +4244,8 @@
 	//return getProperty(e->loc, ident);
 	return Type::dotExp(sc, e, ident);
     }
+    if (!s->isFuncDeclaration())	// because of overloading
+	s->checkDeprecated(e->loc, sc);
     s = s->toAlias();
 
     v = s->isVarDeclaration();
@@ -4459,6 +4499,7 @@
     {
 	/* Create a TupleExp
 	 */
+	e = e->semantic(sc);	// do this before turning on noaccesscheck
 	Expressions *exps = new Expressions;
 	exps->reserve(sym->fields.dim);
 	for (size_t i = 0; i < sym->fields.dim; i++)
@@ -4467,7 +4508,10 @@
 	    exps->push(fe);
 	}
 	e = new TupleExp(e->loc, exps);
+	sc = sc->push();
+	sc->noaccesscheck = 1;
 	e = e->semantic(sc);
+	sc->pop();
 	return e;
     }
 
@@ -4541,6 +4585,7 @@
             e = new PtrExp(e->loc, e);
             e->type = ct->next->next->next;
         }
+        }
 
 #else
 
@@ -4565,9 +4610,31 @@
 		    e->type = t->pointerTo();
 		}
 		e = new PtrExp(e->loc, e, t);
-
-#endif
-	    }
+        }
+
+#endif // !LLVMDC
+
+	    return e;
+	}
+
+	if (ident == Id::__vptr)
+	{   /* The pointer to the vtbl[]
+	     * *cast(void***)e
+	     */
+	    e = e->castTo(sc, tvoidptr->pointerTo()->pointerTo());
+	    e = new PtrExp(e->loc, e);
+	    e = e->semantic(sc);
+	    return e;
+	}
+
+	if (ident == Id::__monitor)
+	{   /* The handle to the monitor (call it a void*)
+	     * *(cast(void**)e + 1)
+	     */
+	    e = e->castTo(sc, tvoidptr->pointerTo());
+	    e = new AddExp(e->loc, e, new IntegerExp(1));
+	    e = new PtrExp(e->loc, e);
+	    e = e->semantic(sc);
 	    return e;
 	}
 
@@ -4587,6 +4654,8 @@
 	    return Type::dotExp(sc, e, ident);
 	}
     }
+    if (!s->isFuncDeclaration())	// because of overloading
+	s->checkDeprecated(e->loc, sc);
     s = s->toAlias();
     v = s->isVarDeclaration();
     if (v && v->isConst())
--- a/dmd/mtype.h	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/mtype.h	Sat Jul 12 19:38:31 2008 +0200
@@ -355,6 +355,7 @@
     Type *syntaxCopy();
     d_uns64 size(Loc loc);
     Type *semantic(Loc loc, Scope *sc);
+    void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps);
     void toDecoBuffer(OutBuffer *buf);
     void toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod);
     Expression *dotExp(Scope *sc, Expression *e, Identifier *ident);
--- a/dmd/optimize.c	Sat Jul 12 17:04:36 2008 +0200
+++ b/dmd/optimize.c	Sat Jul 12 19:38:31 2008 +0200
@@ -1,752 +1,752 @@
-
-// 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 <ctype.h>
-#include <assert.h>
-#include <math.h>
-
-#if __DMC__
-#include <complex.h>
-#endif
-
-#include "lexer.h"
-#include "mtype.h"
-#include "expression.h"
-#include "declaration.h"
-#include "aggregate.h"
-#include "init.h"
-
-
-#ifdef IN_GCC
-#include "d-gcc-real.h"
-
-/* %% fix? */
-extern "C" bool real_isnan (const real_t *);
-#endif
-
-static real_t zero;	// work around DMC bug for now
-
-
-/*************************************
- * If expression is a variable with a const initializer,
- * return that initializer.
- */
-
-Expression *fromConstInitializer(Expression *e1)
-{
-    //printf("fromConstInitializer(%s)\n", e1->toChars());
-    if (e1->op == TOKvar)
-    {	VarExp *ve = (VarExp *)e1;
-	VarDeclaration *v = ve->var->isVarDeclaration();
-	if (v && v->isConst() && v->init)
-	{   Expression *ei = v->init->toExpression();
-	    if (ei && ei->type)
-		e1 = ei;
-	}
-    }
-    return e1;
-}
-
-
-Expression *Expression::optimize(int result)
-{
-    //printf("Expression::optimize(result = x%x) %s\n", result, toChars());
-    return this;
-}
-
-Expression *VarExp::optimize(int result)
-{
-    if (result & WANTinterpret)
-    {
-	return fromConstInitializer(this);
-    }
-    return this;
-}
-
-Expression *TupleExp::optimize(int result)
-{
-    for (size_t i = 0; i < exps->dim; i++)
-    {   Expression *e = (Expression *)exps->data[i];
-
-	e = e->optimize(WANTvalue | (result & WANTinterpret));
-	exps->data[i] = (void *)e;
-    }
-    return this;
-}
-
-Expression *ArrayLiteralExp::optimize(int result)
-{
-    if (elements)
-    {
-	for (size_t i = 0; i < elements->dim; i++)
-	{   Expression *e = (Expression *)elements->data[i];
-
-	    e = e->optimize(WANTvalue | (result & WANTinterpret));
-	    elements->data[i] = (void *)e;
-	}
-    }
-    return this;
-}
-
-Expression *AssocArrayLiteralExp::optimize(int result)
-{
-    assert(keys->dim == values->dim);
-    for (size_t i = 0; i < keys->dim; i++)
-    {   Expression *e = (Expression *)keys->data[i];
-
-	e = e->optimize(WANTvalue | (result & WANTinterpret));
-	keys->data[i] = (void *)e;
-
-	e = (Expression *)values->data[i];
-	e = e->optimize(WANTvalue | (result & WANTinterpret));
-	values->data[i] = (void *)e;
-    }
-    return this;
-}
-
-Expression *StructLiteralExp::optimize(int result)
-{
-    if (elements)
-    {
-	for (size_t i = 0; i < elements->dim; i++)
-	{   Expression *e = (Expression *)elements->data[i];
-	    if (!e)
-		continue;
-	    e = e->optimize(WANTvalue | (result & WANTinterpret));
-	    elements->data[i] = (void *)e;
-	}
-    }
-    return this;
-}
-
-Expression *TypeExp::optimize(int result)
-{
-    return this;
-}
-
-Expression *UnaExp::optimize(int result)
-{
-    e1 = e1->optimize(result);
-    return this;
-}
-
-Expression *NegExp::optimize(int result)
-{   Expression *e;
-
-    e1 = e1->optimize(result);
-    if (e1->isConst() == 1)
-    {
-	e = Neg(type, e1);
-    }
-    else
-	e = this;
-    return e;
-}
-
-Expression *ComExp::optimize(int result)
-{   Expression *e;
-
-    e1 = e1->optimize(result);
-    if (e1->isConst() == 1)
-    {
-	e = Com(type, e1);
-    }
-    else
-	e = this;
-    return e;
-}
-
-Expression *NotExp::optimize(int result)
-{   Expression *e;
-
-    e1 = e1->optimize(result);
-    if (e1->isConst() == 1)
-    {
-	e = Not(type, e1);
-    }
-    else
-	e = this;
-    return e;
-}
-
-Expression *BoolExp::optimize(int result)
-{   Expression *e;
-
-    e1 = e1->optimize(result);
-    if (e1->isConst() == 1)
-    {
-	e = Bool(type, e1);
-    }
-    else
-	e = this;
-    return e;
-}
-
-Expression *AddrExp::optimize(int result)
-{   Expression *e;
-
-    //printf("AddrExp::optimize(result = %d) %s\n", result, toChars());
-    e1 = e1->optimize(result);
-    // Convert &*ex to ex
-    if (e1->op == TOKstar)
-    {	Expression *ex;
-
-	ex = ((PtrExp *)e1)->e1;
-	if (type->equals(ex->type))
-	    e = ex;
-	else
-	{
-	    e = ex->copy();
-	    e->type = type;
-	}
-	return e;
-    }
-#if !IN_LLVM
-    if (e1->op == TOKvar)
-    {	VarExp *ve = (VarExp *)e1;
-	if (!ve->var->isOut() && !ve->var->isRef() &&
-	    !ve->var->isImportedSymbol())
-	{
-	    e = new SymOffExp(loc, ve->var, 0);
-	    e->type = type;
-	    return e;
-	}
-    }
-    if (e1->op == TOKindex)
-    {	// Convert &array[n] to &array+n
-	IndexExp *ae = (IndexExp *)e1;
-
-	if (ae->e2->op == TOKint64 && ae->e1->op == TOKvar)
-	{
-	    integer_t index = ae->e2->toInteger();
-	    VarExp *ve = (VarExp *)ae->e1;
-	    if (ve->type->ty == Tsarray && ve->type->next->ty != Tbit
-		&& !ve->var->isImportedSymbol())
-	    {
-		TypeSArray *ts = (TypeSArray *)ve->type;
-		integer_t dim = ts->dim->toInteger();
-		if (index < 0 || index >= dim)
-		    error("array index %lld is out of bounds [0..%lld]", index, dim);
-		e = new SymOffExp(loc, ve->var, index * ts->next->size());
-		e->type = type;
-		return e;
-	    }
-	}
-    }
-#endif
-    return this;
-}