Fix overload resolution issue in dmd bug 313/314 fix.

This commit is contained in:
Christian Kamm
2009-05-16 12:17:33 +02:00
parent 62e8384da0
commit 53c3ac0313
13 changed files with 74 additions and 53 deletions

View File

@@ -523,7 +523,7 @@ MATCH AddrExp::implicitConvTo(Type *t)
{
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 @@ MATCH SymOffExp::implicitConvTo(Type *t)
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 @@ MATCH DelegateExp::implicitConvTo(Type *t)
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 @@ Expression *AddrExp::castTo(Scope *sc, Type *t)
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 @@ Expression *SymOffExp::castTo(Scope *sc, Type *t)
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 @@ Expression *DelegateExp::castTo(Scope *sc, Type *t)
{
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)

View File

@@ -840,7 +840,7 @@ int ClassDeclaration::isFuncHidden(FuncDeclaration *fd)
}
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

View File

@@ -369,6 +369,7 @@ AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Type *type)
#endif
this->overnext = NULL;
this->inSemantic = 0;
this->importprot = PROTundefined;
assert(type);
}
@@ -501,15 +502,16 @@ void AliasDeclaration::semantic(Scope *sc)
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);

View File

@@ -87,8 +87,8 @@ struct Match
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 @@ struct AliasDeclaration : Declaration
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 @@ struct FuncDeclaration : Declaration
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 FuncDeclaration : Declaration
struct FuncAliasDeclaration : FuncDeclaration
{
FuncDeclaration *funcalias;
PROT importprot; // if generated by import, store its protection
FuncAliasDeclaration(FuncDeclaration *funcalias);
FuncAliasDeclaration *isFuncAliasDeclaration() { return this; }

View File

@@ -685,9 +685,13 @@ Dsymbol *ScopeDsymbol::search(Loc loc, Identifier *ident, int flags)
// 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)
{

View File

@@ -3621,7 +3621,7 @@ Lagain:
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 @@ Lagain:
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 @@ Lagain:
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 @@ SymOffExp::SymOffExp(Loc loc, Declaration *var, unsigned offset)
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 @@ Expression *SymOffExp::semantic(Scope *sc)
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 @@ DelegateExp::DelegateExp(Loc loc, Expression *e, FuncDeclaration *f)
: UnaExp(loc, TOKdelegate, sizeof(DelegateExp), e)
{
this->func = f;
m = NULL;
}
Expression *DelegateExp::semantic(Scope *sc)
@@ -5867,6 +5870,7 @@ Expression *DelegateExp::semantic(Scope *sc)
#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 @@ Lagain:
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 @@ Lagain:
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 @@ Lagain:
}
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 @@ Lagain:
}
}
f = f->overloadResolve(loc, arguments);
f = f->overloadResolve(loc, arguments, sc->module);
checkDeprecated(sc, f);
#if DMDV2
checkPurity(sc, f);
@@ -6581,6 +6585,7 @@ void CallExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
AddrExp::AddrExp(Loc loc, Expression *e)
: UnaExp(loc, TOKaddress, sizeof(AddrExp), e)
{
m = NULL;
}
Expression *AddrExp::semantic(Scope *sc)
@@ -6590,6 +6595,7 @@ Expression *AddrExp::semantic(Scope *sc)
#endif
if (!type)
{
m = sc->module;
UnaExp::semantic(sc);
e1 = e1->toLvalue(sc, NULL);
if (!e1->type)

View File

@@ -73,7 +73,7 @@ void initPrecedence();
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 @@ struct SymOffExp : Expression
{
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 DotTemplateInstanceExp : UnaExp
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 CallExp : UnaExp
struct AddrExp : UnaExp
{
Module* m; // starting point for overload resolution
AddrExp(Loc loc, Expression *e);
Expression *semantic(Scope *sc);
#if IN_DMD

View File

@@ -326,7 +326,7 @@ void FuncDeclaration::semantic(Scope *sc)
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 @@ int FuncDeclaration::overloadInsert(Dsymbol *s)
* 1 done
*/
int overloadApply(FuncDeclaration *fstart,
int overloadApply(Module* from, FuncDeclaration *fstart,
int (*fp)(void *, FuncDeclaration *),
void *param)
{
@@ -1543,8 +1543,9 @@ int overloadApply(FuncDeclaration *fstart,
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 @@ int fp1(void *param, FuncDeclaration *f)
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 @@ int fp2(void *param, FuncDeclaration *f)
}
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 @@ void overloadResolveX(Match *m, FuncDeclaration *fstart, Expressions *arguments)
}
#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 @@ if (arguments)
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 @@ FuncAliasDeclaration::FuncAliasDeclaration(FuncDeclaration *funcalias)
{
assert(funcalias != this);
this->funcalias = funcalias;
importprot = PROTundefined;
}
const char *FuncAliasDeclaration::kind()

View File

@@ -163,8 +163,8 @@ void Import::semantic(Scope *sc)
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();
}

View File

@@ -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 @@ Expression *BinExp::op_overload(Scope *sc)
fd = s->isFuncDeclaration();
if (fd)
{
overloadResolveX(&m, fd, &args2);
overloadResolveX(&m, fd, &args2, sc->module);
}
else
{ td = s->isTemplateDeclaration();
@@ -278,7 +279,7 @@ Expression *BinExp::op_overload(Scope *sc)
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 @@ Expression *BinExp::op_overload(Scope *sc)
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 @@ Expression *BinExp::op_overload(Scope *sc)
fd = s->isFuncDeclaration();
if (fd)
{
overloadResolveX(&m, fd, &args1);
overloadResolveX(&m, fd, &args1, sc->module);
}
else
{ td = s->isTemplateDeclaration();
@@ -482,7 +483,7 @@ Dsymbol *search_function(AggregateDeclaration *ad, Identifier *funcid)
* 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 @@ void inferApplyArgTypes(enum TOK op, Arguments *arguments, Expression *aggr)
{
fd = s->isFuncDeclaration();
if (fd)
inferApplyArgTypesX(fd, arguments);
inferApplyArgTypesX(from, fd, arguments);
}
break;
}
@@ -583,7 +584,7 @@ void inferApplyArgTypes(enum TOK op, Arguments *arguments, Expression *aggr)
fd = de->func->isFuncDeclaration();
if (fd)
inferApplyArgTypesX(fd, arguments);
inferApplyArgTypesX(from, fd, arguments);
}
else
{
@@ -613,9 +614,9 @@ int fp3(void *param, FuncDeclaration *f)
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

View File

@@ -1277,7 +1277,7 @@ Statement *ForeachStatement::semantic(Scope *sc)
return this;
}
inferApplyArgTypes(op, arguments, aggr);
inferApplyArgTypes(op, arguments, aggr, sc->module);
/* Check for inference errors
*/

View File

@@ -346,9 +346,9 @@ void StructDeclaration::semantic(Scope *sc)
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);

View File

@@ -749,7 +749,7 @@ void TypeInfoStructDeclaration::llvmDefine()
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 @@ void TypeInfoStructDeclaration::llvmDefine()
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 @@ void TypeInfoStructDeclaration::llvmDefine()
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);