changeset 1358:78038e540342

Fix overload resolution issue in dmd bug 313/314 fix.
author Christian Kamm <kamm incasoftware de>
date Sat, 16 May 2009 12:17:33 +0200
parents 48747003a5de
children 015e00affbb9 194852badf8c
files dmd/cast.c dmd/class.c dmd/declaration.c dmd/declaration.h dmd/dsymbol.c dmd/expression.c dmd/expression.h dmd/func.c dmd/import.c dmd/opover.c dmd/statement.c dmd/struct.c gen/typinf.cpp
diffstat 13 files changed, 74 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/dmd/cast.c	Fri May 15 17:17:20 2009 +0200
+++ b/dmd/cast.c	Sat May 16 12:17:33 2009 +0200
@@ -523,7 +523,7 @@
 	{
 	    ve = (VarExp *)e1;
 	    f = ve->var->isFuncDeclaration();
-	    if (f && f->overloadExactMatch(t->next))
+	    if (f && f->overloadExactMatch(t->next, m))
 		result = MATCHexact;
 	}
     }
@@ -552,7 +552,7 @@
 	    t->ty == Tpointer && t->next->ty == Tfunction)
 	{
 	    f = var->isFuncDeclaration();
-	    if (f && f->overloadExactMatch(t->next))
+	    if (f && f->overloadExactMatch(t->next, m))
 		result = MATCHexact;
 	}
     }
@@ -579,7 +579,7 @@
 	if (type->ty == Tdelegate && type->nextOf()->ty == Tfunction &&
 	    t->ty == Tdelegate && t->nextOf()->ty == Tfunction)
 	{
-	    if (func && func->overloadExactMatch(t->nextOf()))
+	    if (func && func->overloadExactMatch(t->nextOf(), m))
 		result = MATCHexact;
 	}
     }
@@ -975,7 +975,7 @@
 	    f = ve->var->isFuncDeclaration();
 	    if (f)
 	    {
-		f = f->overloadExactMatch(tb->next);
+		f = f->overloadExactMatch(tb->next, m);
 		if (f)
 		{
 		    e = new VarExp(loc, f);
@@ -1099,7 +1099,7 @@
 	    f = var->isFuncDeclaration();
 	    if (f)
 	    {
-		f = f->overloadExactMatch(tb->next);
+		f = f->overloadExactMatch(tb->next, m);
 		if (f)
 		{
 		    e = new SymOffExp(loc, f, 0);
@@ -1136,7 +1136,7 @@
 	{
 	    if (func)
 	    {
-		f = func->overloadExactMatch(tb->next);
+		f = func->overloadExactMatch(tb->next, m);
 		if (f)
 		{   int offset;
 		    if (f->tintro && f->tintro->next->isBaseOf(f->type->next, &offset) && offset)
--- a/dmd/class.c	Fri May 15 17:17:20 2009 +0200
+++ b/dmd/class.c	Sat May 16 12:17:33 2009 +0200
@@ -840,7 +840,7 @@
     }
     FuncDeclaration *fdstart = s->toAlias()->isFuncDeclaration();
     //printf("%s fdstart = %p\n", s->kind(), fdstart);
-    return !overloadApply(fdstart, &isf, fd);
+    return !overloadApply(getModule(), fdstart, &isf, fd);
 }
 #endif
 
--- a/dmd/declaration.c	Fri May 15 17:17:20 2009 +0200
+++ b/dmd/declaration.c	Sat May 16 12:17:33 2009 +0200
@@ -369,6 +369,7 @@
 #endif
     this->overnext = NULL;
     this->inSemantic = 0;
+    this->importprot = PROTundefined;
     assert(type);
 }
 
@@ -501,15 +502,16 @@
 	FuncDeclaration *f = s->toAlias()->isFuncDeclaration();
 	if (f)
 	{
+	    FuncAliasDeclaration *fa = new FuncAliasDeclaration(f);
+	    fa->importprot = importprot;
 	    if (overnext)
 	    {
-		FuncAliasDeclaration *fa = new FuncAliasDeclaration(f);
 		if (!fa->overloadInsert(overnext))
 		    ScopeDsymbol::multiplyDefined(0, f, overnext);
 		overnext = NULL;
-		s = fa;
-		s->parent = sc->parent;
 	    }
+	    s = fa;
+	    s->parent = sc->parent;
 	}
 	if (overnext)
 	    ScopeDsymbol::multiplyDefined(0, s, overnext);
--- a/dmd/declaration.h	Fri May 15 17:17:20 2009 +0200
+++ b/dmd/declaration.h	Sat May 16 12:17:33 2009 +0200
@@ -87,8 +87,8 @@
     FuncDeclaration *anyf;	// pick a func, any func, to use for error recovery
 };
 
-void overloadResolveX(Match *m, FuncDeclaration *f, Expressions *arguments);
-int overloadApply(FuncDeclaration *fstart,
+void overloadResolveX(Match *m, FuncDeclaration *f, Expressions *arguments, Module* from);
+int overloadApply(Module* from, FuncDeclaration *fstart,
 	int (*fp)(void *, FuncDeclaration *),
 	void *param);
 
@@ -220,6 +220,7 @@
     Dsymbol *aliassym;
     Dsymbol *overnext;		// next in overload list
     int inSemantic;
+    PROT importprot;	// if generated by import, store its protection
 
     AliasDeclaration(Loc loc, Identifier *ident, Type *type);
     AliasDeclaration(Loc loc, Identifier *ident, Dsymbol *s);
@@ -670,8 +671,8 @@
     int overrides(FuncDeclaration *fd);
     int findVtblIndex(Array *vtbl, int dim);
     int overloadInsert(Dsymbol *s);
-    FuncDeclaration *overloadExactMatch(Type *t);
-    FuncDeclaration *overloadResolve(Loc loc, Expressions *arguments);
+    FuncDeclaration *overloadExactMatch(Type *t, Module* from);
+    FuncDeclaration *overloadResolve(Loc loc, Expressions *arguments, Module* from);
     LabelDsymbol *searchLabel(Identifier *ident);
     AggregateDeclaration *isThis();
     AggregateDeclaration *isMember2();
@@ -744,7 +745,8 @@
 struct FuncAliasDeclaration : FuncDeclaration
 {
     FuncDeclaration *funcalias;
-
+    PROT importprot;	// if generated by import, store its protection
+    
     FuncAliasDeclaration(FuncDeclaration *funcalias);
 
     FuncAliasDeclaration *isFuncAliasDeclaration() { return this; }
--- a/dmd/dsymbol.c	Fri May 15 17:17:20 2009 +0200
+++ b/dmd/dsymbol.c	Sat May 16 12:17:33 2009 +0200
@@ -685,9 +685,13 @@
     // Look in symbols declared in this module
     Dsymbol *s = symtab ? symtab->lookup(ident) : NULL;
 
-    // hide private nonlocal symbols
-    if (flags & 1 && s && s->prot() == PROTprivate)
-	s = NULL;
+    // hide the aliases generated by selective or renamed private imports
+    if (s && flags & 1)
+        if (AliasDeclaration* ad = s->isAliasDeclaration())
+	    // may be a private alias to a function that is overloaded. these
+	    // are sorted out during overload resolution, accept them here
+	    if (ad->importprot == PROTprivate && !ad->aliassym->isFuncAliasDeclaration())
+		s = NULL;
 
     if (s)
     {
--- a/dmd/expression.c	Fri May 15 17:17:20 2009 +0200
+++ b/dmd/expression.c	Sat May 16 12:17:33 2009 +0200
@@ -3621,7 +3621,7 @@
 	if (f)
 	{
 	    assert(f);
-	    f = f->overloadResolve(loc, arguments);
+	    f = f->overloadResolve(loc, arguments, sc->module);
 	    checkDeprecated(sc, f);
 	    member = f->isCtorDeclaration();
 	    assert(member);
@@ -3649,7 +3649,7 @@
 		newargs = new Expressions();
 	    newargs->shift(e);
 
-	    f = cd->aggNew->overloadResolve(loc, newargs);
+	    f = cd->aggNew->overloadResolve(loc, newargs, sc->module);
 	    allocator = f->isNewDeclaration();
 	    assert(allocator);
 
@@ -3682,7 +3682,7 @@
 		newargs = new Expressions();
 	    newargs->shift(e);
 
-	    f = f->overloadResolve(loc, newargs);
+	    f = f->overloadResolve(loc, newargs, sc->module);
 	    allocator = f->isNewDeclaration();
 	    assert(allocator);
 
@@ -3870,6 +3870,7 @@
     assert(var);
     this->var = var;
     this->offset = offset;
+    m = NULL;
     VarDeclaration *v = var->isVarDeclaration();
     if (v && v->needThis())
 	error("need 'this' for address of %s", v->toChars());
@@ -3881,6 +3882,7 @@
     printf("SymOffExp::semantic('%s')\n", toChars());
 #endif
     //var->semantic(sc);
+    m = sc->module;
     if (!type)
 	type = var->type->pointerTo();
     VarDeclaration *v = var->isVarDeclaration();
@@ -5858,6 +5860,7 @@
 	: UnaExp(loc, TOKdelegate, sizeof(DelegateExp), e)
 {
     this->func = f;
+    m = NULL;
 }
 
 Expression *DelegateExp::semantic(Scope *sc)
@@ -5867,6 +5870,7 @@
 #endif
     if (!type)
     {
+	m = sc->module;
 	e1 = e1->semantic(sc);
     // LDC we need a copy as we store the LLVM tpye in TypeFunction, and delegate/members have different types for 'this'
 	type = new TypeDelegate(func->type->syntaxCopy());
@@ -6202,7 +6206,7 @@
 
 	    f = dve->var->isFuncDeclaration();
 	    assert(f);
-	    f = f->overloadResolve(loc, arguments);
+	    f = f->overloadResolve(loc, arguments, sc->module);
 
 	    ad = f->toParent()->isAggregateDeclaration();
 	}
@@ -6303,7 +6307,7 @@
 		    sc->callSuper |= CSXany_ctor | CSXsuper_ctor;
 		}
 
-		f = f->overloadResolve(loc, arguments);
+		f = f->overloadResolve(loc, arguments, sc->module);
 		checkDeprecated(sc, f);
 #if DMDV2
 		checkPurity(sc, f);
@@ -6343,7 +6347,7 @@
 	    }
 
 	    f = cd->ctor;
-	    f = f->overloadResolve(loc, arguments);
+	    f = f->overloadResolve(loc, arguments, sc->module);
 	    checkDeprecated(sc, f);
 #if DMDV2
 	    checkPurity(sc, f);
@@ -6437,7 +6441,7 @@
 	    }
 	}
 
-	f = f->overloadResolve(loc, arguments);
+	f = f->overloadResolve(loc, arguments, sc->module);
 	checkDeprecated(sc, f);
 #if DMDV2
 	checkPurity(sc, f);
@@ -6581,6 +6585,7 @@
 AddrExp::AddrExp(Loc loc, Expression *e)
 	: UnaExp(loc, TOKaddress, sizeof(AddrExp), e)
 {
+    m = NULL;
 }
 
 Expression *AddrExp::semantic(Scope *sc)
@@ -6590,6 +6595,7 @@
 #endif
     if (!type)
     {
+	m = sc->module;
 	UnaExp::semantic(sc);
 	e1 = e1->toLvalue(sc, NULL);
 	if (!e1->type)
--- a/dmd/expression.h	Fri May 15 17:17:20 2009 +0200
+++ b/dmd/expression.h	Sat May 16 12:17:33 2009 +0200
@@ -73,7 +73,7 @@
 Expression *resolveProperties(Scope *sc, Expression *e);
 void accessCheck(Loc loc, Scope *sc, Expression *e, Declaration *d);
 Dsymbol *search_function(AggregateDeclaration *ad, Identifier *funcid);
-void inferApplyArgTypes(enum TOK op, Arguments *arguments, Expression *aggr);
+void inferApplyArgTypes(enum TOK op, Arguments *arguments, Expression *aggr, Module* from);
 void argExpTypesToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs);
 void argsToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs);
 void expandTuples(Expressions *exps);
@@ -634,6 +634,7 @@
 {
     Declaration *var;
     unsigned offset;
+    Module* m;	// starting point for overload resolution
 
     SymOffExp(Loc loc, Declaration *var, unsigned offset);
     Expression *semantic(Scope *sc);
@@ -969,6 +970,7 @@
 struct DelegateExp : UnaExp
 {
     FuncDeclaration *func;
+    Module* m;	// starting point for overload resolution
 
     DelegateExp(Loc loc, Expression *e, FuncDeclaration *func);
     Expression *semantic(Scope *sc);
@@ -1036,6 +1038,8 @@
 
 struct AddrExp : UnaExp
 {
+    Module* m;	// starting point for overload resolution
+
     AddrExp(Loc loc, Expression *e);
     Expression *semantic(Scope *sc);
 #if IN_DMD
--- a/dmd/func.c	Fri May 15 17:17:20 2009 +0200
+++ b/dmd/func.c	Sat May 16 12:17:33 2009 +0200
@@ -326,7 +326,7 @@
 		    if (s)
 		    {
 			FuncDeclaration *f = s->isFuncDeclaration();
-			f = f->overloadExactMatch(type);
+			f = f->overloadExactMatch(type, getModule());
 			if (f && f->isFinal() && f->prot() != PROTprivate)
 			    error("cannot override final function %s", f->toPrettyChars());
 		    }
@@ -1530,7 +1530,7 @@
  *	1	done
  */
 
-int overloadApply(FuncDeclaration *fstart,
+int overloadApply(Module* from, FuncDeclaration *fstart,
 	int (*fp)(void *, FuncDeclaration *),
 	void *param)
 {
@@ -1543,8 +1543,9 @@
 
 	if (fa)
 	{
-	    if (overloadApply(fa->funcalias, fp, param))
-		return 1;
+	    if (fa->getModule() == from || fa->importprot != PROTprivate)
+		if (overloadApply(from, fa->funcalias, fp, param))
+		    return 1;
 	    next = fa->overnext;
 	}
 	else
@@ -1613,12 +1614,12 @@
     return 0;
 }
 
-FuncDeclaration *FuncDeclaration::overloadExactMatch(Type *t)
+FuncDeclaration *FuncDeclaration::overloadExactMatch(Type *t, Module* from)
 {
     Param1 p;
     p.t = t;
     p.f = NULL;
-    overloadApply(this, &fp1, &p);
+    overloadApply(from, this, &fp1, &p);
     return p.f;
 }
 
@@ -1721,12 +1722,12 @@
 }
 
 
-void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments)
+void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments, Module* from)
 {
     Param2 p;
     p.m = m;
     p.arguments = arguments;
-    overloadApply(fstart, &fp2, &p);
+    overloadApply(from, fstart, &fp2, &p);
 }
 
 #if 0
@@ -1811,7 +1812,7 @@
 }
 #endif
 
-FuncDeclaration *FuncDeclaration::overloadResolve(Loc loc, Expressions *arguments)
+FuncDeclaration *FuncDeclaration::overloadResolve(Loc loc, Expressions *arguments, Module* from)
 {
     TypeFunction *tf;
     Match m;
@@ -1834,7 +1835,7 @@
 
     memset(&m, 0, sizeof(m));
     m.last = MATCHnomatch;
-    overloadResolveX(&m, this, arguments);
+    overloadResolveX(&m, this, arguments, from);
 
     if (m.count == 1)		// exactly one match
     {
@@ -2253,6 +2254,7 @@
 {
     assert(funcalias != this);
     this->funcalias = funcalias;
+    importprot = PROTundefined;
 }
 
 const char *FuncAliasDeclaration::kind()
--- a/dmd/import.c	Fri May 15 17:17:20 2009 +0200
+++ b/dmd/import.c	Sat May 16 12:17:33 2009 +0200
@@ -163,8 +163,8 @@
 	    if (!mod->search(loc, (Identifier *)names.data[i], 0))
 		error("%s not found", ((Identifier *)names.data[i])->toChars());
 
+	    ad->importprot = protection;
 	    ad->semantic(sc);
-	    ad->protection = protection;
 	}
 	sc = sc->pop();
     }
--- a/dmd/opover.c	Fri May 15 17:17:20 2009 +0200
+++ b/dmd/opover.c	Sat May 16 12:17:33 2009 +0200
@@ -28,9 +28,10 @@
 #include "declaration.h"
 #include "aggregate.h"
 #include "template.h"
+#include "scope.h"
 
 static Expression *build_overload(Loc loc, Scope *sc, Expression *ethis, Expression *earg, Identifier *id);
-static void inferApplyArgTypesX(FuncDeclaration *fstart, Arguments *arguments);
+static void inferApplyArgTypesX(Module* from, FuncDeclaration *fstart, Arguments *arguments);
 static int inferApplyArgTypesY(TypeFunction *tf, Arguments *arguments);
 static void templateResolve(Match *m, TemplateDeclaration *td, Scope *sc, Loc loc, Objects *targsi, Expressions *arguments);
 
@@ -263,7 +264,7 @@
 	    fd = s->isFuncDeclaration();
 	    if (fd)
 	    {
-		overloadResolveX(&m, fd, &args2);
+		overloadResolveX(&m, fd, &args2, sc->module);
 	    }
 	    else
 	    {   td = s->isTemplateDeclaration();
@@ -278,7 +279,7 @@
 	    fd = s_r->isFuncDeclaration();
 	    if (fd)
 	    {
-		overloadResolveX(&m, fd, &args1);
+		overloadResolveX(&m, fd, &args1, sc->module);
 	    }
 	    else
 	    {   td = s_r->isTemplateDeclaration();
@@ -352,7 +353,7 @@
 		fd = s_r->isFuncDeclaration();
 		if (fd)
 		{
-		    overloadResolveX(&m, fd, &args2);
+		    overloadResolveX(&m, fd, &args2, sc->module);
 		}
 		else
 		{   td = s_r->isTemplateDeclaration();
@@ -366,7 +367,7 @@
 		fd = s->isFuncDeclaration();
 		if (fd)
 		{
-		    overloadResolveX(&m, fd, &args1);
+		    overloadResolveX(&m, fd, &args1, sc->module);
 		}
 		else
 		{   td = s->isTemplateDeclaration();
@@ -482,7 +483,7 @@
  * them from the aggregate type.
  */
 
-void inferApplyArgTypes(enum TOK op, Arguments *arguments, Expression *aggr)
+void inferApplyArgTypes(enum TOK op, Arguments *arguments, Expression *aggr, Module* from)
 {
     if (!arguments || !arguments->dim)
 	return;
@@ -571,7 +572,7 @@
 	    {
 		fd = s->isFuncDeclaration();
 		if (fd) 
-		    inferApplyArgTypesX(fd, arguments);
+		    inferApplyArgTypesX(from, fd, arguments);
 	    }
 	    break;
 	}
@@ -583,7 +584,7 @@
 
 		fd = de->func->isFuncDeclaration();
 		if (fd)
-		    inferApplyArgTypesX(fd, arguments);
+		    inferApplyArgTypesX(from, fd, arguments);
 	    }
 	    else
 	    {
@@ -613,9 +614,9 @@
     return 0;
 }
 
-static void inferApplyArgTypesX(FuncDeclaration *fstart, Arguments *arguments)
+static void inferApplyArgTypesX(Module* from, FuncDeclaration *fstart, Arguments *arguments)
 {
-    overloadApply(fstart, &fp3, arguments);
+    overloadApply(from, fstart, &fp3, arguments);
 }
 
 #if 0
--- a/dmd/statement.c	Fri May 15 17:17:20 2009 +0200
+++ b/dmd/statement.c	Sat May 16 12:17:33 2009 +0200
@@ -1277,7 +1277,7 @@
 	return this;
     }
 
-    inferApplyArgTypes(op, arguments, aggr);
+    inferApplyArgTypes(op, arguments, aggr, sc->module);
 
     /* Check for inference errors
      */
--- a/dmd/struct.c	Fri May 15 17:17:20 2009 +0200
+++ b/dmd/struct.c	Sat May 16 12:17:33 2009 +0200
@@ -346,9 +346,9 @@
 	Dsymbol *s = search_function(this, id);
 	FuncDeclaration *fdx = s ? s->isFuncDeclaration() : NULL;
 	if (fdx)
-	{   FuncDeclaration *fd = fdx->overloadExactMatch(tfeqptr);
+	{   FuncDeclaration *fd = fdx->overloadExactMatch(tfeqptr, getModule());
 	    if (!fd)
-	    {	fd = fdx->overloadExactMatch(tfeq);
+	    {	fd = fdx->overloadExactMatch(tfeq, getModule());
 		if (fd)
 		{   // Create the thunk, fdptr
 		    FuncDeclaration *fdptr = new FuncDeclaration(loc, loc, fdx->ident, STCundefined, tfeqptr);
--- a/gen/typinf.cpp	Fri May 15 17:17:20 2009 +0200
+++ b/gen/typinf.cpp	Sat May 16 12:17:33 2009 +0200
@@ -749,7 +749,7 @@
     fdx = s ? s->isFuncDeclaration() : NULL;
     if (fdx)
     {
-        fd = fdx->overloadExactMatch(tftohash);
+        fd = fdx->overloadExactMatch(tftohash, getModule());
         if (fd) {
             fd->codegen(Type::sir);
             assert(fd->ir.irFunc->func != 0);
@@ -775,7 +775,7 @@
         ptty = isaPointer(stype->getElementType(5+i));
         if (fdx)
         {
-            fd = fdx->overloadExactMatch(tfeqptr);
+            fd = fdx->overloadExactMatch(tfeqptr, getModule());
             if (fd) {
                 fd->codegen(Type::sir);
                 assert(fd->ir.irFunc->func != 0);
@@ -803,7 +803,7 @@
     fdx = s ? s->isFuncDeclaration() : NULL;
     if (fdx)
     {
-        fd = fdx->overloadExactMatch(tftostring);
+        fd = fdx->overloadExactMatch(tftostring, getModule());
         if (fd) {
             fd->codegen(Type::sir);
             assert(fd->ir.irFunc->func != 0);