diff dmd/TemplateDeclaration.d @ 51:b7d29f613539

StaticAssertStatement.syntaxCopy IfStatement.syntaxCopy CompoundDeclarationStatement.syntaxCopy VoidInitializer.syntaxCopy TypeAArray.syntaxCopy TypeTypeof.syntaxCopy TypeAArray.resolve TypeSArray.deduceType TypeAArray.deduceType TypeAArray.implicitConvTo TemplateDeclaration.leastAsSpecialized TemplateTypeParameter.dummyArg TypeIdentifier.deduceType TemplateTypeParameter.syntaxCopy Lexer.hexStringConstant Lexer.delimitedStringConstant GotoDefaultStatement.ctor CaseRangeStatement.ctor Type.castMod StorageClassDeclaration.syntaxCopy TemplateDeclaration.syntaxCopy
author korDen
date Sat, 21 Aug 2010 11:17:42 +0400
parents adf6f7f216ea
children cab4c37afb89
line wrap: on
line diff
--- a/dmd/TemplateDeclaration.d	Sat Aug 21 10:38:26 2010 +0400
+++ b/dmd/TemplateDeclaration.d	Sat Aug 21 11:17:42 2010 +0400
@@ -151,7 +151,29 @@
 
     Dsymbol syntaxCopy(Dsymbol)
 	{
-		assert(false);
+		//printf("TemplateDeclaration.syntaxCopy()\n");
+		TemplateDeclaration td;
+		TemplateParameters p;
+		Array d;
+
+		p = null;
+		if (parameters)
+		{
+			p = new TemplateParameters();
+			p.setDim(parameters.dim);
+			for (int i = 0; i < p.dim; i++)
+			{   
+				TemplateParameter tp = cast(TemplateParameter)parameters.data[i];
+				p.data[i] = cast(void*)tp.syntaxCopy();
+			}
+		}
+		
+		Expression e = null;
+		if (constraint)
+			e = constraint.syntaxCopy();
+		d = Dsymbol.arraySyntaxCopy(members);
+		td = new TemplateDeclaration(loc, ident, p, e, d);
+		return td;
 	}
 
     void semantic(Scope sc)
@@ -514,9 +536,67 @@
 		return m;
 	}
 	
+	/********************************************
+	 * Determine partial specialization order of 'this' vs td2.
+	 * Returns:
+	 *	match	this is at least as specialized as td2
+	 *	0	td2 is more specialized than this
+	 */
     MATCH leastAsSpecialized(TemplateDeclaration td2)
 	{
-		assert(false);
+	    /* This works by taking the template parameters to this template
+		 * declaration and feeding them to td2 as if it were a template
+		 * instance.
+		 * If it works, then this template is at least as specialized
+		 * as td2.
+		 */
+
+		scope TemplateInstance ti = new TemplateInstance(Loc(0), ident);	// create dummy template instance
+		scope Objects dedtypes = new Objects();
+
+version (LOG_LEASTAS) {
+		printf("%s.leastAsSpecialized(%s)\n", toChars(), td2.toChars());
+}
+
+		// Set type arguments to dummy template instance to be types
+		// generated from the parameters to this template declaration
+		ti.tiargs = new Objects();
+		ti.tiargs.setDim(parameters.dim);
+		for (int i = 0; i < ti.tiargs.dim; i++)
+		{
+			TemplateParameter tp = cast(TemplateParameter)parameters.data[i];
+
+			void* p = tp.dummyArg();
+			if (p)
+				ti.tiargs.data[i] = p;
+			else
+				ti.tiargs.setDim(i);
+		}
+
+		// Temporary Array to hold deduced types
+		//dedtypes.setDim(parameters.dim);
+		dedtypes.setDim(td2.parameters.dim);
+
+		// Attempt a type deduction
+		MATCH m = td2.matchWithInstance(ti, dedtypes, 1);
+		if (m)
+		{
+			/* A non-variadic template is more specialized than a
+			 * variadic one.
+			 */
+			if (isVariadic() && !td2.isVariadic())
+				goto L1;
+
+version (LOG_LEASTAS) {
+			printf("  matches %d, so is least as specialized\n", m);
+}
+			return m;
+		}
+	  L1:
+version (LOG_LEASTAS) {
+		printf("  doesn't match, so is not as specialized\n");
+}
+		return MATCHnomatch;
 	}
 
 	/*************************************************