changeset 158:80f4806ffa13

overloadApply fixes
author korDen
date Sun, 19 Sep 2010 06:54:31 +0400
parents b7b61140701d
children 2e2ed2632ccc
files dmd/ClassDeclaration.d dmd/FuncDeclaration.d dmd/TraitsExp.d dmd/expression/Util.d
diffstat 4 files changed, 88 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/dmd/ClassDeclaration.d	Thu Sep 16 01:34:10 2010 +0200
+++ b/dmd/ClassDeclaration.d	Sun Sep 19 06:54:31 2010 +0400
@@ -73,7 +73,7 @@
 
 struct Param
 {
-	int isf(void*, FuncDeclaration fd2)
+	bool visit(FuncDeclaration fd2)
 	{
 		//printf("param = %p, fd = %p %s\n", param, fd, fd.toChars());
 		return fd is fd2;
@@ -972,7 +972,7 @@
 			foreach (s2; os.a)
 			{   
 				auto f2 = s2.isFuncDeclaration();
-				if (f2 && overloadApply(f2, &p.isf, &p))
+				if (f2 && overloadApply(f2, p))
 					return false;
 			}
 			return true;
@@ -981,7 +981,7 @@
 		{
 			FuncDeclaration fdstart = s.isFuncDeclaration();
 			//printf("%s fdstart = %p\n", s.kind(), fdstart);
-			return !overloadApply(fdstart, &p.isf, &p);
+			return !overloadApply(fdstart, p);
 		}
 	}
 }
--- a/dmd/FuncDeclaration.d	Thu Sep 16 01:34:10 2010 +0200
+++ b/dmd/FuncDeclaration.d	Sun Sep 19 06:54:31 2010 +0400
@@ -1865,7 +1865,7 @@
 		Param1 p;
 		p.t = t;
 		p.f = null;
-		overloadApply(this, &p.fp1, &p);
+		overloadApply(this, p);
 		return p.f;
 	}
 	
@@ -2938,10 +2938,10 @@
 	
 	FuncDeclaration isUnique()
 	{	
-		FuncDeclaration result = null;
-
-		overloadApply(this, &fpunique, &result);
-		return result;
+		Unique unique;
+		overloadApply(this, unique);
+		
+		return unique.f;
 	}
 	
 	/*******************************
--- a/dmd/TraitsExp.d	Thu Sep 16 01:34:10 2010 +0200
+++ b/dmd/TraitsExp.d	Sun Sep 19 06:54:31 2010 +0400
@@ -46,21 +46,25 @@
 {
 	Expression e1;
 	Expressions exps;
-	int fpvirtuals(void* param, FuncDeclaration f)
-	{   Pvirtuals p = *cast(Pvirtuals*)param;
+	
+	bool visit(FuncDeclaration f)
+	{
+		Pvirtuals* p = &this;
 
 		if (f.isVirtual())
-		{	Expression e;
+		{	
+			Expression e;
 
-			if (p.e1.op == TOK.TOKdotvar)
-			{   DotVarExp dve = cast(DotVarExp)p.e1;
+			if (p.e1.op == TOKdotvar)
+			{   
+				DotVarExp dve = cast(DotVarExp)p.e1;
 				e = new DotVarExp(Loc(0), dve.e1, f);
 			}
 			else
 				e = new DsymbolExp(Loc(0), f);
 			p.exps.push(e);
 		}
-		return 0;
+		return false;
 	}
 }
 
@@ -260,19 +264,22 @@
 				Expressions exps = new Expressions();
 				FuncDeclaration f_;
 				if (e.op == TOKvar)
-				{	VarExp ve = cast(VarExp)e;
+				{	
+					VarExp ve = cast(VarExp)e;
 					f_ = ve.var.isFuncDeclaration();
 				}
 				else if (e.op == TOKdotvar)
-				{	DotVarExp dve = cast(DotVarExp)e;
+				{	
+					DotVarExp dve = cast(DotVarExp)e;
 					f_ = dve.var.isFuncDeclaration();
 				}
 				else
 					f_ = null;
+					
 				Pvirtuals p;
 				p.exps = exps;
 				p.e1 = e;
-				overloadApply(f_, &p.fpvirtuals, &p);
+				overloadApply(f_, p);
 
 				TupleExp tup = new TupleExp(loc, exps);
 				return tup.semantic(sc);
--- a/dmd/expression/Util.d	Thu Sep 16 01:34:10 2010 +0200
+++ b/dmd/expression/Util.d	Sun Sep 19 06:54:31 2010 +0400
@@ -136,7 +136,7 @@
  *	0	continue
  *	1	done
  */
-int overloadApply(FuncDeclaration fstart, int delegate(void*, FuncDeclaration) dg, void* param)
+bool overloadApply(Visitor)(FuncDeclaration fstart, Visitor visitor)
 {
     FuncDeclaration f;
     Declaration d;
@@ -148,8 +148,8 @@
 
 		if (fa)
 		{
-			if (overloadApply(fa.funcalias, dg, param))
-				return 1;
+			if (overloadApply(fa.funcalias, visitor))
+				return true;
 			next = fa.overnext;
 		}
 		else
@@ -173,32 +173,37 @@
 					d.error("is aliased to a function");
 					break;		// BUG: should print error message?
 				}
-				if (dg(param, f))
-					return 1;
+				if (visitor.visit(f))
+					return true;
 
 				next = f.overnext;
 			}
 		}
     }
-    return 0;
+    return false;
 }
 
 /********************************************
  * If there are no overloads of function f, return that function,
  * otherwise return NULL.
  */
-static int fpunique(void* param, FuncDeclaration f)
+struct Unique
 {
-	FuncDeclaration* pf = cast(FuncDeclaration *)param;
-
-    if (*pf)
-    {   *pf is null;
-        return 1;               // ambiguous, done
-    }
-    else
-    {   *pf = f;
-        return 0;
-    }
+	bool visit(FuncDeclaration f)
+	{
+		if (this.f)
+		{	
+			this.f = null;
+			return true;		// ambiguous, done
+		}
+		else
+		{	
+			this.f = f;
+			return false;
+		}
+	}
+	
+	FuncDeclaration f;
 }
 
 /********************************************
@@ -216,9 +221,11 @@
 }
     Expressions arguments;
 	
-	int fp2(void* param, FuncDeclaration f)
+	bool visit(FuncDeclaration f)
 	{
-        auto p = cast(Param2*)param;
+		Param2* p = &this;
+		Match* m = p.m;
+		Expressions arguments = p.arguments;
 		MATCH match;
 
 		if (f != m.lastf)		// skip duplicates
@@ -226,15 +233,15 @@
 			m.anyf = f;
 			TypeFunction tf = cast(TypeFunction)f.type;
 
-	        int property = (tf.isproperty) ? 1 : 2;
-	        if (p.property == 0)
-	            p.property = property;
-	        else if (p.property != property)
-	            error(f.loc, "cannot overload both property and non-property functions");
+			int property = (tf.isproperty) ? 1 : 2;
+			if (p.property == 0)
+				p.property = property;
+			else if (p.property != property)
+				error(f.loc, "cannot overload both property and non-property functions");
 
-			match = tf.callMatch(f.needThis() ? ethis : null, arguments);
+			match = cast(MATCH) tf.callMatch(f.needThis() ? p.ethis : null, arguments);
 			//printf("test: match = %d\n", match);
-			if (match != MATCH.MATCHnomatch)
+			if (match != MATCHnomatch)
 			{
 				if (match > m.last)
 					goto LfIsBetter;
@@ -249,7 +256,7 @@
 				else if (f.overrides(m.lastf))
 					goto LfIsBetter;
 
-version(DMDV2) {
+version (DMDV2) {
 				/* Try to disambiguate using template-style partial ordering rules.
 				 * In essence, if f() and g() are ambiguous, if f() can call g(),
 				 * but g() cannot call f(), then pick f().
@@ -265,23 +272,22 @@
 						goto LlastIsBetter;
 				}
 }
-
 			Lambiguous:
 				m.nextf = f;
 				m.count++;
-				return 0;
+				return false;
 
 			LfIsBetter:
 				m.last = match;
 				m.lastf = f;
 				m.count = 1;
-				return 0;
+				return false;
 
 			LlastIsBetter:
-				return 0;
+				return false;
 			}
 		}
-		return 0;
+		return false;
 	}
 }
 
@@ -290,31 +296,34 @@
     Type t;		// type to match
     FuncDeclaration f;	// return value
 	
-	int fp1(void*, FuncDeclaration f)
+	bool visit(FuncDeclaration f)
 	{   
+		Param1* p = &this;
+		Type t = p.t;
+
 		if (t.equals(f.type))
 		{	
-			this.f = f;
-			return 1;
+			p.f = f;
+			return true;
 		}
 
 	version (DMDV2) {
-        /* Allow covariant matches, as long as the return type
-         * is just a const conversion.
-         * This allows things like pure functions to match with an impure function type.
-         */
+		/* Allow covariant matches, as long as the return type
+		 * is just a const conversion.
+		 * This allows things like pure functions to match with an impure function type.
+		 */
 		if (t.ty == Tfunction)
 		{   
 			TypeFunction tf = cast(TypeFunction)f.type;
 			if (tf.covariant(t) == 1 &&
 				tf.nextOf().implicitConvTo(t.nextOf()) >= MATCHconst)
 			{
-				this.f = f;
-				return 1;
+				p.f = f;
+				return true;
 			}
 		}
 	}
-		return 0;
+		return false;
 	}
 }
 
@@ -325,7 +334,8 @@
     p.ethis = ethis;
     p.property = 0;
     p.arguments = arguments;
-    overloadApply(fstart, &p.fp2, &p);
+	
+    overloadApply(fstart, p);
 }
 
 void templateResolve(Match* m, TemplateDeclaration td, Scope sc, Loc loc, Objects targsi, Expression ethis, Expressions arguments)
@@ -1580,16 +1590,15 @@
 	 * analogous to func.overloadResolveX().
 	 */
 
-	int fp3(void*, FuncDeclaration f)
+	bool visit(FuncDeclaration f)
 	{
-		auto tf = cast(TypeFunction)f.type;
-		if (inferApplyArgTypesY(tf, arguments) == 1)
-			return 0;
-
+		Parameters arguments = this.arguments;
+		TypeFunction tf = cast(TypeFunction)f.type;
+		if (inferApplyArgTypesY(tf, arguments))
+			return false;
 		if (arguments.dim == 0)
-			return 1;
-
-		return 0;
+			return true;
+		return false;
 	}
 	
 	Parameters arguments;
@@ -1599,7 +1608,7 @@
 {
 	Param3 p3;
 	p3.arguments = arguments;
-    overloadApply(fstart, &p3.fp3, cast(void*)arguments);
+    overloadApply(fstart, p3);
 }
 
 /******************************
@@ -1609,7 +1618,7 @@
  *	1 no match for this function
  */
 
-int inferApplyArgTypesY(TypeFunction tf, Parameters arguments)
+bool inferApplyArgTypesY(TypeFunction tf, Parameters arguments)
 {   
 	size_t nparams;
     Parameter p;
@@ -1653,10 +1662,10 @@
     }
 
   Lmatch:
-    return 0;
+    return false;
 
   Lnomatch:
-    return 1;
+    return true;
 }
 
 /**************************************************