changeset 55:8c2c7b7579f8

Expression.toMangleBuffer Expression.toDelegate Expression.scanForNestedRef TypeStruct.deduceType TypeStruct.toChars DefaultInitExp wrong TOK bug fixed
author korDen
date Sat, 21 Aug 2010 13:28:16 +0400
parents f95140b40251
children 51605de93870
files dmd/DefaultInitExp.d dmd/Expression.d dmd/FileInitExp.d dmd/TypeStruct.d
diffstat 4 files changed, 98 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/dmd/DefaultInitExp.d	Sat Aug 21 12:51:02 2010 +0400
+++ b/dmd/DefaultInitExp.d	Sat Aug 21 13:28:16 2010 +0400
@@ -14,7 +14,7 @@
 
 	this(Loc loc, TOK subop, int size)
 	{
-		super(loc, subop, size);
+		super(loc, TOKdefault, size);
 		this.subop = subop;
 	}
 
--- a/dmd/Expression.d	Sat Aug 21 12:51:02 2010 +0400
+++ b/dmd/Expression.d	Sat Aug 21 13:28:16 2010 +0400
@@ -31,6 +31,12 @@
 import dmd.CommaExp;
 import dmd.NullExp;
 import dmd.AddrExp;
+import dmd.LINK;
+import dmd.FuncExp;
+import dmd.ReturnStatement;
+import dmd.Statement;
+import dmd.FuncLiteralDeclaration;
+import dmd.TypeFunction;
 import dmd.ErrorExp;
 import dmd.TypeStruct;
 import dmd.CastExp;
@@ -348,7 +354,11 @@
     
     void toMangleBuffer(OutBuffer buf)
 	{
+		error("expression %s is not a valid template value argument", toChars());
 		assert(false);
+version (DEBUG) {
+		dump(0);
+}
 	}
     
     int isLvalue()
@@ -772,14 +782,38 @@
 		return e;
 	}
     
+	/********************************************
+	 * Convert from expression to delegate that returns the expression,
+	 * i.e. convert:
+	 *	expr
+	 * to:
+	 *	t delegate() { return expr; }
+	 */
     Expression toDelegate(Scope sc, Type t)
 	{
-		assert(false);
+		//printf("Expression.toDelegate(t = %s) %s\n", t.toChars(), toChars());
+		TypeFunction tf = new TypeFunction(null, t, 0, LINKd);
+		FuncLiteralDeclaration fld = new FuncLiteralDeclaration(loc, loc, tf, TOKdelegate, null);
+		Expression e;
+	static if (true) {
+		sc = sc.push();
+		sc.parent = fld;		// set current function to be the delegate
+		e = this;
+		e.scanForNestedRef(sc);
+		sc = sc.pop();
+	} else {
+		e = this.syntaxCopy();
+	}
+		Statement s = new ReturnStatement(loc, e);
+		fld.fbody = s;
+		e = new FuncExp(loc, fld);
+		e = e.semantic(sc);
+		return e;
 	}
     
     void scanForNestedRef(Scope sc)
 	{
-		assert(false);
+		//printf("Expression.scanForNestedRef(%s)\n", toChars());
 	}
     
     Expression optimize(int result)
--- a/dmd/FileInitExp.d	Sat Aug 21 12:51:02 2010 +0400
+++ b/dmd/FileInitExp.d	Sat Aug 21 13:28:16 2010 +0400
@@ -24,7 +24,7 @@
 
 	Expression resolve(Loc loc, Scope sc)
 	{
-		//printf("FileInitExp::resolve() %s\n", toChars());
+		//printf("FileInitExp::resolve() %.*s\n", toChars());
 		string s = loc.filename ? loc.filename : sc.module_.ident.toChars();
 		Expression e = new StringExp(loc, s);
 		e = e.semantic(sc);
--- a/dmd/TypeStruct.d	Sat Aug 21 12:51:02 2010 +0400
+++ b/dmd/TypeStruct.d	Sat Aug 21 13:28:16 2010 +0400
@@ -1,6 +1,7 @@
 module dmd.TypeStruct;
 
 import dmd.Type;
+import dmd.TypeInstance;
 import dmd.StructDeclaration;
 import dmd.Declaration;
 import dmd.STC;
@@ -29,6 +30,7 @@
 import dmd.Identifier;
 import dmd.MATCH;
 import dmd.ArrayTypes;
+import dmd.DYNCAST;
 import dmd.TemplateInstance;
 import dmd.FuncDeclaration;
 import dmd.VarExp;
@@ -98,7 +100,15 @@
 	
     string toChars()
 	{
-		assert(false);
+		//printf("sym.parent: %s, deco = %s\n", sym.parent.toChars(), deco);
+		if (mod)
+			return Type.toChars();
+		TemplateInstance ti = sym.parent.isTemplateInstance();
+		if (ti && ti.toAlias() == sym)
+		{
+			return ti.toChars();
+		}
+		return sym.toChars();
 	}
 	
     Type syntaxCopy()
@@ -445,7 +455,55 @@
 	
     MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes)
 	{
-		assert(false);
+		//printf("TypeStruct.deduceType()\n");
+		//printf("\tthis.parent   = %s, ", sym.parent.toChars()); print();
+		//printf("\ttparam = %d, ", tparam.ty); tparam.print();
+
+		/* If this struct is a template struct, and we're matching
+		 * it against a template instance, convert the struct type
+		 * to a template instance, too, and try again.
+		 */
+		TemplateInstance ti = sym.parent.isTemplateInstance();
+
+		if (tparam && tparam.ty == Tinstance)
+		{
+			if (ti && ti.toAlias() == sym)
+			{
+				TypeInstance t = new TypeInstance(Loc(0), ti);
+				return t.deduceType(sc, tparam, parameters, dedtypes);
+			}
+
+			/* Match things like:
+			 *  S!(T).foo
+			 */
+			TypeInstance tpi = cast(TypeInstance)tparam;
+			if (tpi.idents.dim)
+			{   Identifier id = cast(Identifier)tpi.idents.data[tpi.idents.dim - 1];
+				if (id.dyncast() == DYNCAST.DYNCAST_IDENTIFIER && sym.ident.equals(id))
+				{
+					Type tparent = sym.parent.getType();
+					if (tparent)
+					{
+						/* Slice off the .foo in S!(T).foo
+						 */
+						tpi.idents.dim--;
+						MATCH m = tparent.deduceType(sc, tpi, parameters, dedtypes);
+						tpi.idents.dim++;
+						return m;
+					}
+				}
+			}
+		}
+
+		// Extra check
+		if (tparam && tparam.ty == Tstruct)
+		{
+			TypeStruct tp = cast(TypeStruct)tparam;
+
+			if (sym != tp.sym)
+				return MATCHnomatch;
+		}
+		return Type.deduceType(sc, tparam, parameters, dedtypes);
 	}
 	
     TypeInfoDeclaration getTypeInfoDeclaration()