diff dmd/TypeSlice.d @ 8:d42cd5917df4

wysiwyg strings, alias this, templates, TypeSlice implementation
author dkoroskin <>
date Mon, 14 Dec 2009 17:43:43 +0300
parents 10317f0c89a5
children 38a17310d587
line wrap: on
line diff
--- a/dmd/TypeSlice.d	Mon Dec 14 14:50:03 2009 +0300
+++ b/dmd/TypeSlice.d	Mon Dec 14 17:43:43 2009 +0300
@@ -10,6 +10,12 @@
 import dmd.OutBuffer;
 import dmd.HdrGenState;
 import dmd.TY;
+import dmd.TypeTuple;
+import dmd.WANT;
+import dmd.ArrayTypes;
+import dmd.Argument;
+
+import dmd.type.Util;
 
 class TypeSlice : TypeNext
 {
@@ -18,8 +24,10 @@
 
     this(Type next, Expression lwr, Expression upr)
 	{
-		super(TY.init, null);
-		assert(false);
+		super(TY.Tslice, next);
+		//printf("TypeSlice[%s .. %s]\n", lwr.toChars(), upr.toChars());
+		this.lwr = lwr;
+		this.upr = upr;
 	}
 	
 version (DumbClone) {
@@ -32,12 +40,49 @@
 	
     Type syntaxCopy()
 	{
-		assert(false);
+		Type t = new TypeSlice(next.syntaxCopy(), lwr.syntaxCopy(), upr.syntaxCopy());
+		t.mod = mod;
+		return t;
 	}
 	
     Type semantic(Loc loc, Scope sc)
 	{
-		assert(false);
+		//printf("TypeSlice.semantic() %s\n", toChars());
+		next = next.semantic(loc, sc);
+		transitive();
+		//printf("next: %s\n", next.toChars());
+
+		Type tbn = next.toBasetype();
+		if (tbn.ty != Ttuple)
+		{	
+			error(loc, "can only slice tuple types, not %s", tbn.toChars());
+			return Type.terror;
+		}
+		TypeTuple tt = cast(TypeTuple)tbn;
+
+		lwr = semanticLength(sc, tbn, lwr);
+		lwr = lwr.optimize(WANTvalue);
+		ulong i1 = lwr.toUInteger();
+
+		upr = semanticLength(sc, tbn, upr);
+		upr = upr.optimize(WANTvalue);
+		ulong i2 = upr.toUInteger();
+
+		if (!(i1 <= i2 && i2 <= tt.arguments.dim))
+		{	
+			error(loc, "slice [%ju..%ju] is out of range of [0..%u]", i1, i2, tt.arguments.dim);
+			return Type.terror;
+		}
+
+		Arguments args = new Arguments;
+		args.reserve(cast(size_t)(i2 - i1));
+		for (size_t i = cast(size_t)i1; i < cast(size_t)i2; i++)
+		{	
+			Argument arg = cast(Argument)tt.arguments.data[i];
+			args.push(cast(void*)arg);
+		}
+
+		return new TypeTuple(args);
 	}
 	
     void resolve(Loc loc, Scope sc, Expression* pe, Type* pt, Dsymbol* ps)