Skip to content

Commit

Permalink
Move expression.resolveLoc to expressionsem
Browse files Browse the repository at this point in the history
  • Loading branch information
RazvanN7 committed Nov 7, 2023
1 parent f55bc3f commit 11e1b1a
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 101 deletions.
86 changes: 0 additions & 86 deletions compiler/src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -715,15 +715,6 @@ extern (C++) abstract class Expression : ASTNode
return this.toLvalue(sc, e);
}

/****************************************
* Resolve __FILE__, __LINE__, __MODULE__, __FUNCTION__, __PRETTY_FUNCTION__, __FILE_FULL_PATH__ to loc.
*/
Expression resolveLoc(const ref Loc loc, Scope* sc)
{
this.loc = loc;
return this;
}

/****************************************
* Check that the expression has a valid type.
* If not, generates an error "... has no type".
Expand Down Expand Up @@ -3297,12 +3288,6 @@ extern (C++) abstract class UnaExp : Expression

}

override final Expression resolveLoc(const ref Loc loc, Scope* sc)
{
e1 = e1.resolveLoc(loc, sc);
return this;
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -5086,13 +5071,6 @@ extern (C++) final class CatExp : BinExp
super(loc, EXP.concatenate, e1, e2);
}

override Expression resolveLoc(const ref Loc loc, Scope* sc)
{
e1 = e1.resolveLoc(loc, sc);
e2 = e2.resolveLoc(loc, sc);
return this;
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -5488,19 +5466,6 @@ extern (C++) final class FileInitExp : DefaultInitExp
super(loc, tok);
}

override Expression resolveLoc(const ref Loc loc, Scope* sc)
{
//printf("FileInitExp::resolve() %s\n", toChars());
const(char)* s;
if (op == EXP.fileFullPath)
s = FileName.toAbsolute(loc.isValid() ? loc.filename : sc._module.srcfile.toChars());
else
s = loc.isValid() ? loc.filename : sc._module.ident.toChars();

Expression e = new StringExp(loc, s.toDString());
return e.expressionSemantic(sc);
}

override void accept(Visitor v)
{
v.visit(this);
Expand All @@ -5517,12 +5482,6 @@ extern (C++) final class LineInitExp : DefaultInitExp
super(loc, EXP.line);
}

override Expression resolveLoc(const ref Loc loc, Scope* sc)
{
Expression e = new IntegerExp(loc, loc.linnum, Type.tint32);
return e.expressionSemantic(sc);
}

override void accept(Visitor v)
{
v.visit(this);
Expand All @@ -5539,13 +5498,6 @@ extern (C++) final class ModuleInitExp : DefaultInitExp
super(loc, EXP.moduleString);
}

override Expression resolveLoc(const ref Loc loc, Scope* sc)
{
const auto s = (sc.callsc ? sc.callsc : sc)._module.toPrettyChars().toDString();
Expression e = new StringExp(loc, s);
return e.expressionSemantic(sc);
}

override void accept(Visitor v)
{
v.visit(this);
Expand All @@ -5562,19 +5514,6 @@ extern (C++) final class FuncInitExp : DefaultInitExp
super(loc, EXP.functionString);
}

override Expression resolveLoc(const ref Loc loc, Scope* sc)
{
const(char)* s;
if (sc.callsc && sc.callsc.func)
s = sc.callsc.func.Dsymbol.toPrettyChars();
else if (sc.func)
s = sc.func.Dsymbol.toPrettyChars();
else
s = "";
Expression e = new StringExp(loc, s.toDString());
return e.expressionSemantic(sc);
}

override void accept(Visitor v)
{
v.visit(this);
Expand All @@ -5591,31 +5530,6 @@ extern (C++) final class PrettyFuncInitExp : DefaultInitExp
super(loc, EXP.prettyFunction);
}

override Expression resolveLoc(const ref Loc loc, Scope* sc)
{
FuncDeclaration fd = (sc.callsc && sc.callsc.func)
? sc.callsc.func
: sc.func;

const(char)* s;
if (fd)
{
const funcStr = fd.Dsymbol.toPrettyChars();
OutBuffer buf;
functionToBufferWithIdent(fd.type.isTypeFunction(), buf, funcStr, fd.isStatic);
s = buf.extractChars();
}
else
{
s = "";
}

Expression e = new StringExp(loc, s.toDString());
e = e.expressionSemantic(sc);
e.type = Type.tstring;
return e;
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down
8 changes: 1 addition & 7 deletions compiler/src/dmd/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct Symbol; // back end symbol
Expression *ctfeInterpret(Expression *e);
void expandTuples(Expressions *exps, Identifiers *names = nullptr);
StringExp *toUTF8(StringExp *se, Scope *sc);
Expression *resolveLoc(Expression *exp, const Loc &loc, Scope *sc);
MATCH implicitConvTo(Expression *e, Type *t);
Expression *toLvalue(Expression *_this, Scope *sc, Expression *e);

Expand Down Expand Up @@ -101,7 +102,6 @@ class Expression : public ASTNode
virtual StringExp *toStringExp();
virtual bool isLvalue();
virtual Expression *modifiableLvalue(Scope *sc, Expression *e);
virtual Expression *resolveLoc(const Loc &loc, Scope *sc);
virtual bool checkType();
virtual bool checkValue();
Expression *addressOf();
Expand Down Expand Up @@ -668,7 +668,6 @@ class UnaExp : public Expression
Expression *e1;

UnaExp *syntaxCopy() override;
Expression *resolveLoc(const Loc &loc, Scope *sc) override final;

void accept(Visitor *v) override { v->visit(this); }
};
Expand Down Expand Up @@ -1296,35 +1295,30 @@ class DefaultInitExp : public Expression
class FileInitExp final : public DefaultInitExp
{
public:
Expression *resolveLoc(const Loc &loc, Scope *sc) override;
void accept(Visitor *v) override { v->visit(this); }
};

class LineInitExp final : public DefaultInitExp
{
public:
Expression *resolveLoc(const Loc &loc, Scope *sc) override;
void accept(Visitor *v) override { v->visit(this); }
};

class ModuleInitExp final : public DefaultInitExp
{
public:
Expression *resolveLoc(const Loc &loc, Scope *sc) override;
void accept(Visitor *v) override { v->visit(this); }
};

class FuncInitExp final : public DefaultInitExp
{
public:
Expression *resolveLoc(const Loc &loc, Scope *sc) override;
void accept(Visitor *v) override { v->visit(this); }
};

class PrettyFuncInitExp final : public DefaultInitExp
{
public:
Expression *resolveLoc(const Loc &loc, Scope *sc) override;
void accept(Visitor *v) override { v->visit(this); }
};

Expand Down
100 changes: 100 additions & 0 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -14871,6 +14871,106 @@ bool checkSharedAccess(Expression e, Scope* sc, bool returnRef = false)
return check(e, returnRef);
}

/****************************************
* Resolve __FILE__, __LINE__, __MODULE__, __FUNCTION__, __PRETTY_FUNCTION__, __FILE_FULL_PATH__ to loc.
*/
Expression resolveLoc(Expression exp, const ref Loc loc, Scope* sc)
{
Expression visit(Expression exp)
{
if (auto unaExp = exp.isUnaExp())
{
unaExp.e1 = unaExp.e1.resolveLoc(loc, sc);
return unaExp;
}
exp.loc = loc;
return exp;
}

Expression visitCat(CatExp exp)
{
exp.e1 = exp.e1.resolveLoc(loc, sc);
exp.e2 = exp.e2.resolveLoc(loc, sc);
return exp;
}

Expression visitFileInit(FileInitExp exp)
{
//printf("FileInitExp::resolve() %s\n", exp.toChars());
const(char)* s;
if (exp.op == EXP.fileFullPath)
s = FileName.toAbsolute(loc.isValid() ? loc.filename : sc._module.srcfile.toChars());
else
s = loc.isValid() ? loc.filename : sc._module.ident.toChars();

Expression e = new StringExp(loc, s.toDString());
return e.expressionSemantic(sc);
}

Expression visitLineInit(LineInitExp _)
{
Expression e = new IntegerExp(loc, loc.linnum, Type.tint32);
return e.expressionSemantic(sc);
}

Expression visitModuleInit(ModuleInitExp _)
{
const auto s = (sc.callsc ? sc.callsc : sc)._module.toPrettyChars().toDString();
Expression e = new StringExp(loc, s);
return e.expressionSemantic(sc);
}

Expression visitFuncInit(FuncInitExp _)
{
const(char)* s;
if (sc.callsc && sc.callsc.func)
s = sc.callsc.func.Dsymbol.toPrettyChars();
else if (sc.func)
s = sc.func.Dsymbol.toPrettyChars();
else
s = "";
Expression e = new StringExp(loc, s.toDString());
return e.expressionSemantic(sc);
}

Expression visitPrettyFunc(PrettyFuncInitExp _)
{
FuncDeclaration fd = (sc.callsc && sc.callsc.func)
? sc.callsc.func
: sc.func;

const(char)* s;
if (fd)
{
const funcStr = fd.Dsymbol.toPrettyChars();
OutBuffer buf;
functionToBufferWithIdent(fd.type.isTypeFunction(), buf, funcStr, fd.isStatic);
s = buf.extractChars();
}
else
{
s = "";
}

Expression e = new StringExp(loc, s.toDString());
e = e.expressionSemantic(sc);
e.type = Type.tstring;
return e;
}

switch(exp.op)
{
default: return visit(exp);
case EXP.concatenate: return visitCat(exp.isCatExp());
case EXP.file:
case EXP.fileFullPath: return visitFileInit(exp.isFileInitExp());
case EXP.line: return visitLineInit(exp.isLineInitExp);
case EXP.moduleString: return visitModuleInit(exp.isModuleInitExp());
case EXP.functionString: return visitFuncInit(exp.isFuncInitExp());
case EXP.prettyFunction: return visitPrettyFunc(exp.isPrettyFuncInitExp());
}
}

/************************************************
* Destructors are attached to VarDeclarations.
* Hence, if expression returns a temp that needs a destructor,
Expand Down
8 changes: 0 additions & 8 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,6 @@ class Expression : public ASTNode
virtual StringExp* toStringExp();
virtual bool isLvalue();
virtual Expression* modifiableLvalue(Scope* sc, Expression* e);
virtual Expression* resolveLoc(const Loc& loc, Scope* sc);
virtual bool checkType();
virtual bool checkValue();
Expression* addressOf();
Expand Down Expand Up @@ -7393,7 +7392,6 @@ class UnaExp : public Expression
Expression* e1;
UnaExp* syntaxCopy() override;
void setNoderefOperand();
Expression* resolveLoc(const Loc& loc, Scope* sc) final override;
void accept(Visitor* v) override;
};

Expand Down Expand Up @@ -7837,7 +7835,6 @@ class CatExp final : public BinExp
{
public:
Expression* lowering;
Expression* resolveLoc(const Loc& loc, Scope* sc) override;
void accept(Visitor* v) override;
};

Expand Down Expand Up @@ -7956,35 +7953,30 @@ class DefaultInitExp : public Expression
class FileInitExp final : public DefaultInitExp
{
public:
Expression* resolveLoc(const Loc& loc, Scope* sc) override;
void accept(Visitor* v) override;
};

class LineInitExp final : public DefaultInitExp
{
public:
Expression* resolveLoc(const Loc& loc, Scope* sc) override;
void accept(Visitor* v) override;
};

class ModuleInitExp final : public DefaultInitExp
{
public:
Expression* resolveLoc(const Loc& loc, Scope* sc) override;
void accept(Visitor* v) override;
};

class FuncInitExp final : public DefaultInitExp
{
public:
Expression* resolveLoc(const Loc& loc, Scope* sc) override;
void accept(Visitor* v) override;
};

class PrettyFuncInitExp final : public DefaultInitExp
{
public:
Expression* resolveLoc(const Loc& loc, Scope* sc) override;
void accept(Visitor* v) override;
};

Expand Down

0 comments on commit 11e1b1a

Please sign in to comment.