Skip to content

Commit

Permalink
Merge pull request #5113 from 9rnsr/reduce_dsymexp
Browse files Browse the repository at this point in the history
[optimize] Reduce DsymbolExp object creation
  • Loading branch information
WalterBright committed Sep 23, 2015
2 parents b0d77b5 + 0dc9bd7 commit 4fda426
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 51 deletions.
7 changes: 5 additions & 2 deletions src/aggregate.d
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,20 @@ public:
auto tiargs = new Objects();
tiargs.push(type);
auto ti = new TemplateInstance(loc, Type.rtinfo, tiargs);

Scope* sc3 = ti.tempdecl._scope.startCTFE();
sc3.tinst = sc.tinst;
sc3.minst = sc.minst;
if (isDeprecated())
sc3.stc |= STCdeprecated;

ti.semantic(sc3);
ti.semantic2(sc3);
ti.semantic3(sc3);
Expression e = new DsymbolExp(Loc(), ti.toAlias(), 0);
e = e.semantic(sc3);
auto e = DsymbolExp.resolve(Loc(), sc3, ti.toAlias(), false);

sc3.endCTFE();

e = e.ctfeInterpret();
getRTInfo = e;
}
Expand Down
73 changes: 34 additions & 39 deletions src/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -3555,6 +3555,7 @@ public:
t = (cast(TypePointer)t).next;
e = typeDotIdExp(loc, t, ident);
}
e = e.semantic(sc);
}
else
{
Expand All @@ -3564,6 +3565,7 @@ public:
if (d)
checkAccess(loc, sc, null, d);
}

/* If f is really a function template,
* then replace f with the function template declaration.
*/
Expand All @@ -3581,9 +3583,9 @@ public:
}
}
// Haven't done overload resolution yet, so pass 1
e = new DsymbolExp(loc, s, 1);
e = DsymbolExp.resolve(loc, sc, s, true);
}
return e.semantic(sc);
return e;
}
if (hasThis(sc))
{
Expand Down Expand Up @@ -3673,10 +3675,15 @@ public:
}

override Expression semantic(Scope* sc)
{
return resolve(loc ,sc, s, hasOverloads);
}

static Expression resolve(Loc loc, Scope *sc, Dsymbol s, bool hasOverloads)
{
static if (LOGSEMANTIC)
{
printf("DsymbolExp::semantic(%s %s)\n", s.kind(), s.toChars());
printf("DsymbolExp::resolve(%s %s)\n", s.kind(), s.toChars());
}
Lagain:
Expression e;
Expand All @@ -3691,11 +3698,11 @@ public:
else
{
if (!s.isFuncDeclaration()) // functions are checked after overloading
checkDeprecated(sc, s);
s.checkDeprecated(loc, sc);
s = s.toAlias();
//printf("s = '%s', s->kind = '%s', s->needThis() = %p\n", s->toChars(), s->kind(), s->needThis());
if (s != olds && !s.isFuncDeclaration())
checkDeprecated(sc, s);
s.checkDeprecated(loc, sc);
}
if (VarDeclaration v = s.isVarDeclaration())
{
Expand Down Expand Up @@ -3729,22 +3736,18 @@ public:
if (VarDeclaration v = s.isVarDeclaration())
{
//printf("Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars());
if (!type)
if (!v.type)
{
type = v.type;
if (!v.type)
{
error("forward reference of %s %s", s.kind(), s.toChars());
return new ErrorExp();
}
.error(loc, "forward reference of %s %s", s.kind(), s.toChars());
return new ErrorExp();
}
if ((v.storage_class & STCmanifest) && v._init)
{
// Detect recursive initializers.
// BUG: The check for speculative gagging is not correct
if (v.inuse && !global.gag)
{
error("circular initialization of %s", v.toChars());
.error(loc, "circular initialization of %s", v.toChars());
return new ErrorExp();
}
if (v._scope)
Expand All @@ -3757,7 +3760,7 @@ public:
e = v._init.toExpression(v.type);
if (!e)
{
error("cannot make expression out of initializer for %s", v.toChars());
.error(loc, "cannot make expression out of initializer for %s", v.toChars());
return new ErrorExp();
}
e = e.copy();
Expand All @@ -3766,7 +3769,7 @@ public:
return e;
}
e = new VarExp(loc, v);
e.type = type;
e.type = v.type;
e = e.semantic(sc);
return e.deref();
}
Expand All @@ -3784,7 +3787,7 @@ public:
if (!f.type.deco)
{
const(char)* trailMsg = f.inferRetType ? "inferred return type of function call " : "";
error("forward reference to %s'%s'", trailMsg, toChars());
.error(loc, "forward reference to %s'%s'", trailMsg, f.toChars());
return new ErrorExp();
}
FuncDeclaration fd = s.isFuncDeclaration();
Expand All @@ -3806,7 +3809,7 @@ public:
{
if (!imp.pkg)
{
error("forward reference of import %s", imp.toChars());
.error(loc, "forward reference of import %s", imp.toChars());
return new ErrorExp();
}
auto ie = new ScopeExp(loc, imp.pkg);
Expand Down Expand Up @@ -3864,7 +3867,7 @@ public:
e = e.semantic(sc);
return e;
}
error("%s '%s' is not a variable", s.kind(), s.toChars());
.error(loc, "%s '%s' is not a variable", s.kind(), s.toChars());
return new ErrorExp();
}

Expand Down Expand Up @@ -5142,8 +5145,7 @@ public:
else if (s)
{
//printf("s = %s %s\n", s->kind(), s->toChars());
e = new DsymbolExp(loc, s, s.hasOverloads());
e = e.semantic(sc);
e = DsymbolExp.resolve(loc, sc, s, s.hasOverloads());
}
else
assert(0);
Expand Down Expand Up @@ -5247,10 +5249,10 @@ public:
// Same as wthis.s
e = new VarExp(loc, withsym.withstate.wthis);
e = new DotVarExp(loc, e, s.isDeclaration());
e = e.semantic(sc);
}
else
e = new DsymbolExp(loc, s, s.hasOverloads());
e = e.semantic(sc);
e = DsymbolExp.resolve(loc, sc, s, s.hasOverloads());
//printf("-1ScopeExp::semantic()\n");
return e;
}
Expand Down Expand Up @@ -5308,9 +5310,7 @@ public:
if (!fd)
return Expression.toLvalue(sc, e);
assert(sc);
Expression ex = new DsymbolExp(loc, fd, 1);
ex = ex.semantic(sc);
return ex;
return DsymbolExp.resolve(loc, sc, fd, true);
}

override bool checkValue()
Expand Down Expand Up @@ -6558,10 +6558,10 @@ public:
}
if (ea)
{
Dsymbol sym = getDsymbol(ea);
if (sym)
ea = new DsymbolExp(loc, sym);
ea = ea.semantic(sc);
if (auto sym = getDsymbol(ea))
ea = DsymbolExp.resolve(loc, sc, sym, false);
else
ea = ea.semantic(sc);
ea = resolveProperties(sc, ea);
ta = ea.type;
if (ea.op == TOKtype)
Expand Down Expand Up @@ -8166,8 +8166,7 @@ public:
if (fd.isNested() || fd.isFuncLiteralDeclaration())
{
// (e1, fd)
Expression e = new DsymbolExp(loc, fd);
e = e.semantic(sc);
auto e = DsymbolExp.resolve(loc, sc, fd, false);
return Expression.combine(e1, e);
}
type = fd.type;
Expand Down Expand Up @@ -8456,8 +8455,7 @@ public:
}
if (e1.op == TOKtype)
{
e = new DsymbolExp(loc, s);
e = e.semantic(sc);
e = DsymbolExp.resolve(loc, sc, s, false);
return e;
}
e = new ScopeExp(loc, ti);
Expand Down Expand Up @@ -8513,8 +8511,7 @@ public:
{
// This should *really* be moved to ScopeExp::semantic()
ScopeExp se = cast(ScopeExp)de.e2;
de.e2 = new DsymbolExp(loc, se.sds);
de.e2 = de.e2.semantic(sc);
de.e2 = DsymbolExp.resolve(loc, sc, se.sds, false);
}
if (de.e2.op == TOKtemplate)
{
Expand Down Expand Up @@ -8880,8 +8877,7 @@ public:
{
// Perhaps this should be moved to ScopeExp::semantic()
ScopeExp se = cast(ScopeExp)e1;
e1 = new DsymbolExp(loc, se.sds);
e1 = e1.semantic(sc);
e1 = DsymbolExp.resolve(loc, sc, se.sds, false);
}
else if (e1.op == TOKsymoff && (cast(SymOffExp)e1).hasOverloads)
{
Expand All @@ -8902,8 +8898,7 @@ public:
{
// This should *really* be moved to ScopeExp::semantic()
ScopeExp se = cast(ScopeExp)de.e2;
de.e2 = new DsymbolExp(loc, se.sds);
de.e2 = de.e2.semantic(sc);
de.e2 = DsymbolExp.resolve(loc, sc, se.sds, false);
}
if (de.e2.op == TOKtemplate)
{
Expand Down
1 change: 1 addition & 0 deletions src/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ class DsymbolExp : public Expression

DsymbolExp(Loc loc, Dsymbol *s, bool hasOverloads = false);
Expression *semantic(Scope *sc);
static Expression resolve(Loc loc, Scope *sc, Dsymbol *s, bool hasOverloads);
bool isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
void accept(Visitor *v) { v->visit(this); }
Expand Down
2 changes: 1 addition & 1 deletion src/func.d
Original file line number Diff line number Diff line change
Expand Up @@ -2087,7 +2087,7 @@ public:
if (isStatic())
{
// The monitor is in the ClassInfo
vsync = new DotIdExp(loc, new DsymbolExp(loc, cd), Id.classinfo);
vsync = new DotIdExp(loc, DsymbolExp.resolve(loc, sc2, cd, false), Id.classinfo);
}
else
{
Expand Down
14 changes: 6 additions & 8 deletions src/mtype.d
Original file line number Diff line number Diff line change
Expand Up @@ -6829,8 +6829,8 @@ public:
if (tindex)
eindex = new TypeExp(loc, tindex);
else if (sindex)
eindex = new DsymbolExp(loc, sindex);
Expression e = new IndexExp(loc, new DsymbolExp(loc, s), eindex);
eindex = DsymbolExp.resolve(loc, sc, sindex, false);
Expression e = new IndexExp(loc, DsymbolExp.resolve(loc, sc, s, false), eindex);
e = e.semantic(sc);
if (e.op == TOKerror)
*pt = Type.terror;
Expand All @@ -6844,7 +6844,7 @@ public:
if (tindex)
tindex.resolve(loc, sc, &eindex, &tindex, &sindex);
if (sindex)
eindex = new DsymbolExp(loc, sindex);
eindex = DsymbolExp.resolve(loc, sc, sindex, false);
if (!eindex)
{
.error(loc, "index is %s not an expression", oindex.toChars());
Expand Down Expand Up @@ -7014,7 +7014,7 @@ public:
VarDeclaration v = s.isVarDeclaration();
FuncDeclaration f = s.isFuncDeclaration();
if (intypeid || !v && !f)
e = new DsymbolExp(loc, s);
e = DsymbolExp.resolve(loc, sc, s, false);
else
e = new VarExp(loc, s.isDeclaration());
resolveExprType(loc, sc, e, i, pe, pt);
Expand Down Expand Up @@ -8013,8 +8013,7 @@ public:
}
if (s.isImport() || s.isModule() || s.isPackage())
{
e = new DsymbolExp(e.loc, s, 0);
e = e.semantic(sc);
e = DsymbolExp.resolve(e.loc, sc, s, false);
return e;
}
OverloadSet o = s.isOverloadSet();
Expand Down Expand Up @@ -8868,8 +8867,7 @@ public:
}
if (s.isImport() || s.isModule() || s.isPackage())
{
e = new DsymbolExp(e.loc, s, 0);
e = e.semantic(sc);
e = DsymbolExp.resolve(e.loc, sc, s, false);
return e;
}
OverloadSet o = s.isOverloadSet();
Expand Down
2 changes: 1 addition & 1 deletion src/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ extern (C++) Expression semanticTraits(TraitsExp e, Scope* sc)
return ex.semantic(sc);
}
}
return (new DsymbolExp(e.loc, s)).semantic(sc);
return DsymbolExp.resolve(e.loc, sc, s, false);
}
else if (e.ident == Id.hasMember || e.ident == Id.getMember || e.ident == Id.getOverloads || e.ident == Id.getVirtualMethods || e.ident == Id.getVirtualFunctions)
{
Expand Down

0 comments on commit 4fda426

Please sign in to comment.