changeset 104:a1cf34da9ebe

+ TemplateDeclaration.toCBuffer + TypeExp.rvalue + TypeExp.toElem
author Trass3r
date Tue, 31 Aug 2010 16:35:41 +0200
parents 3ddadd0c4534
children 12c0c84d13fd
files dmd/TemplateDeclaration.d dmd/TypeExp.d
diffstat 2 files changed, 103 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/dmd/TemplateDeclaration.d	Tue Aug 31 17:07:01 2010 +0400
+++ b/dmd/TemplateDeclaration.d	Tue Aug 31 16:35:41 2010 +0200
@@ -53,35 +53,35 @@
 TemplateTupleParameter isVariadic(TemplateParameters parameters)
 {   
 	size_t dim = parameters.dim;
-    TemplateTupleParameter tp = null;
+	TemplateTupleParameter tp = null;
 
-    if (dim)
+	if (dim)
 		tp = (cast(TemplateParameter)parameters.data[dim - 1]).isTemplateTupleParameter();
 
-    return tp;
+	return tp;
 }
 
 void ObjectToCBuffer(OutBuffer buf, HdrGenState* hgs, Object oarg)
 {
-    //printf("ObjectToCBuffer()\n");
-    Type t = isType(oarg);
-    Expression e = isExpression(oarg);
-    Dsymbol s = isDsymbol(oarg);
-    Tuple v = isTuple(oarg);
-    if (t)
-    {	
+	//printf("ObjectToCBuffer()\n");
+	Type t = isType(oarg);
+	Expression e = isExpression(oarg);
+	Dsymbol s = isDsymbol(oarg);
+	Tuple v = isTuple(oarg);
+	if (t)
+	{	
 		//printf("\tt: %s ty = %d\n", t.toChars(), t.ty);
 		t.toCBuffer(buf, null, hgs);
-    }
-    else if (e)
+	}
+	else if (e)
 		e.toCBuffer(buf, hgs);
-    else if (s)
-    {
+	else if (s)
+	{
 		string p = s.ident ? s.ident.toChars() : s.toChars();
 		buf.writestring(p);
-    }
-    else if (v)
-    {
+	}
+	else if (v)
+	{
 		Objects args = v.objects;
 		for (size_t i = 0; i < args.dim; i++)
 		{
@@ -90,36 +90,36 @@
 			Object o = cast(Object)args.data[i];
 			ObjectToCBuffer(buf, hgs, o);
 		}
-    }
-    else if (!oarg)
-    {
+	}
+	else if (!oarg)
+	{
 		buf.writestring("null");
-    }
-    else
-    {
+	}
+	else
+	{
 		debug writef("bad Object = %p\n", oarg);
 		assert(0);
-    }
+	}
 }
 
 class TemplateDeclaration : ScopeDsymbol
 {
-    TemplateParameters parameters;	// array of TemplateParameter's
+	TemplateParameters parameters;	// array of TemplateParameter's
 
-    TemplateParameters origParameters;	// originals for Ddoc
-    Expression constraint;
-    Array instances;			// array of TemplateInstance's
+	TemplateParameters origParameters;	// originals for Ddoc
+	Expression constraint;
+	Array instances;			// array of TemplateInstance's
 
-    TemplateDeclaration overnext;	// next overloaded TemplateDeclaration
-    TemplateDeclaration overroot;	// first in overnext list
+	TemplateDeclaration overnext;	// next overloaded TemplateDeclaration
+	TemplateDeclaration overroot;	// first in overnext list
 
-    int semanticRun;			// 1 semantic() run
+	int semanticRun;			// 1 semantic() run
 
-    Dsymbol onemember;		// if !=NULL then one member of this template
+	Dsymbol onemember;		// if !=NULL then one member of this template
 
-    int literal;		// this template declaration is a literal
+	int literal;		// this template declaration is a literal
 
-    this(Loc loc, Identifier id, TemplateParameters parameters, Expression constraint, Dsymbols decldefs)
+	this(Loc loc, Identifier id, TemplateParameters parameters, Expression constraint, Dsymbols decldefs)
 	{	
 		super(id);
 		
@@ -150,7 +150,7 @@
 		instances = new Array();
 	}
 
-    override Dsymbol syntaxCopy(Dsymbol)
+	override Dsymbol syntaxCopy(Dsymbol)
 	{
 		//printf("TemplateDeclaration.syntaxCopy()\n");
 		TemplateDeclaration td;
@@ -177,7 +177,7 @@
 		return td;
 	}
 
-    override void semantic(Scope sc)
+	override void semantic(Scope sc)
 	{
 	version (LOG) {
 		printf("TemplateDeclaration.semantic(this = %p, id = '%s')\n", this, ident.toChars());
@@ -273,7 +273,7 @@
 	 * Overload existing TemplateDeclaration 'this' with the new one 's'.
 	 * Return !=0 if successful; i.e. no conflict.
 	 */
-    override bool overloadInsert(Dsymbol s)
+	override bool overloadInsert(Dsymbol s)
 	{
 		TemplateDeclaration *pf;
 		TemplateDeclaration f;
@@ -323,9 +323,49 @@
 		return true;
 	}
 
-    override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
+	override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
 	{
-		assert(false);
+static if (false) // Should handle template functions
+{
+		if (onemember && onemember.isFuncDeclaration())
+			buf.writestring("foo ");
+}
+		buf.writestring(kind());
+		buf.writeByte(' ');
+		buf.writestring(ident.toChars());
+		buf.writeByte('(');
+		for (int i = 0; i < parameters.dim; i++)
+		{
+			TemplateParameter tp = cast(TemplateParameter)parameters.data[i];
+			if (hgs.ddoc)
+				tp = cast(TemplateParameter)origParameters.data[i];
+			if (i)
+				buf.writeByte(',');
+			tp.toCBuffer(buf, hgs);
+		}
+		buf.writeByte(')');
+version(DMDV2)
+{
+		if (constraint)
+		{   buf.writestring(" if (");
+			constraint.toCBuffer(buf, hgs);
+			buf.writeByte(')');
+		}
+}
+
+		if (hgs.hdrgen)
+		{
+			hgs.tpltMember++;
+			buf.writenl();
+			buf.writebyte('{');
+			buf.writenl();
+			foreach (Dsymbol s; members)
+				s.toCBuffer(buf, hgs);
+
+			buf.writebyte('}');
+			buf.writenl();
+			hgs.tpltMember--;
+		}
 	}
 
 	override void toJsonBuffer(OutBuffer buf)
@@ -358,14 +398,14 @@
 		buf.writestring("}\n");
 	}
 
-    override string kind()
+	override string kind()
 	{
 		return (onemember && onemember.isAggregateDeclaration())
 			? onemember.kind()
 			: "template";
 	}
 
-    override string toChars()
+	override string toChars()
 	{
 		OutBuffer buf = new OutBuffer();
 		HdrGenState hgs;
@@ -393,12 +433,12 @@
 		return buf.extractString();
 	}
 
-    override void emitComment(Scope sc)
+	override void emitComment(Scope sc)
 	{
 		assert(false);
 	}
 	
-//    void toDocBuffer(OutBuffer *buf);
+//	void toDocBuffer(OutBuffer *buf);
 
 	/***************************************
 	 * Given that ti is an instance of this TemplateDeclaration,
@@ -411,7 +451,7 @@
 	 *	dedtypes	deduced arguments
 	 * Return match level.
 	 */
-    MATCH matchWithInstance(TemplateInstance ti, Objects dedtypes, int flag)
+	MATCH matchWithInstance(TemplateInstance ti, Objects dedtypes, int flag)
 	{
 		MATCH m;
 		int dedtypes_dim = dedtypes.dim;
@@ -573,9 +613,9 @@
 	 *	match	this is at least as specialized as td2
 	 *	0	td2 is more specialized than this
 	 */
-    MATCH leastAsSpecialized(TemplateDeclaration td2)
+	MATCH leastAsSpecialized(TemplateDeclaration td2)
 	{
-	    /* This works by taking the template parameters to this template
+		/* 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
@@ -642,7 +682,7 @@
 	 * Returns:
 	 *	match level
 	 */
-    MATCH deduceFunctionTemplateMatch(Loc loc, Objects targsi, Expression ethis, Expressions fargs, Objects dedargs)
+	MATCH deduceFunctionTemplateMatch(Loc loc, Objects targsi, Expression ethis, Expressions fargs, Objects dedargs)
 	{
 		size_t nfparams;
 		size_t nfargs;
@@ -1096,7 +1136,7 @@
 		//printf("\tnomatch\n");
 		return MATCHnomatch;
 	}
-    
+	
 	/*************************************************
 	 * Given function arguments, figure out which template function
 	 * to expand, and return that function.
@@ -1120,7 +1160,7 @@
 
 	static if (false) {
 		printf("TemplateDeclaration.deduceFunctionTemplate() %s\n", toChars());
-		printf("    targsi:\n");
+		printf("	targsi:\n");
 		if (targsi)
 		{	
 			for (int i = 0; i < targsi.dim; i++)
@@ -1129,7 +1169,7 @@
 				printf("\t%s\n", arg.toChars());
 			}
 		}
-		printf("    fargs:\n");
+		printf("	fargs:\n");
 		for (int i = 0; i < fargs.dim; i++)
 		{	
 			Expression arg = cast(Expression)fargs.data[i];
@@ -1248,7 +1288,7 @@
 	/**************************************************
 	 * Declare template parameter tp with value o, and install it in the scope sc.
 	 */
-    void declareParameter(Scope sc, TemplateParameter tp, Object o)
+	void declareParameter(Scope sc, TemplateParameter tp, Object o)
 	{
 		//printf("TemplateDeclaration.declareParameter('%s', o = %p)\n", tp.ident.toChars(), o);
 
@@ -1316,9 +1356,9 @@
 		s.semantic(sc);
 	}
 	
-    override TemplateDeclaration isTemplateDeclaration() { return this; }
+	override TemplateDeclaration isTemplateDeclaration() { return this; }
 
-    TemplateTupleParameter isVariadic()
+	TemplateTupleParameter isVariadic()
 	{
 		return .isVariadic(parameters);
 	}
@@ -1326,8 +1366,8 @@
 	/***********************************
 	 * We can overload templates.
 	 */
-    override bool isOverloadable()
+	override bool isOverloadable()
 	{
-		return 1;
+		return true;
 	}
 }
--- a/dmd/TypeExp.d	Tue Aug 31 17:07:01 2010 +0400
+++ b/dmd/TypeExp.d	Tue Aug 31 16:35:41 2010 +0200
@@ -1,6 +1,8 @@
 module dmd.TypeExp;
 
 import dmd.Expression;
+import dmd.backend.TYM;
+import dmd.backend.Util;
 import dmd.backend.elem;
 import dmd.Type;
 import dmd.OutBuffer;
@@ -41,7 +43,7 @@
 
 	override void rvalue()
 	{
-		assert(false);
+		error("type %s has no value", toChars());
 	}
 
 	override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
@@ -56,7 +58,11 @@
 
 	override elem* toElem(IRState* irs)
 	{
-		assert(false);
+		debug
+			writef("TypeExp.toElem()\n");
+
+		error("type %s is not an expression", toChars());
+		return el_long(TYint, 0);
 	}
 }