Skip to content

Commit

Permalink
Move Expression.checkValue to expressionsem.d (#20797)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel authored Jan 28, 2025
1 parent acaa88a commit 17ad9f9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 80 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dmd/dtemplate.d
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import dmd.dsymbolsem : dsymbolSemantic, checkDeprecated, aliasSemantic, search,
import dmd.errors;
import dmd.errorsink;
import dmd.expression;
import dmd.expressionsem : resolveLoc, expressionSemantic, resolveProperties;
import dmd.expressionsem : resolveLoc, expressionSemantic, resolveProperties, checkValue;
import dmd.func;
import dmd.funcsem : functionSemantic, leastAsSpecialized, overloadApply;
import dmd.globals;
Expand Down
65 changes: 0 additions & 65 deletions compiler/src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -522,25 +522,6 @@ extern (C++) abstract class Expression : ASTNode
return false;
}

/****************************************
* Check that the expression has a valid value.
* If not, generates an error "... has no value".
* Returns:
* true if the expression is not valid or has void type.
*/
bool checkValue()
{
if (type && type.toBasetype().ty == Tvoid)
{
error(loc, "expression `%s` is `void` and has no value", toChars());
//print(); assert(0);
if (!global.gag)
type = Type.terror;
return true;
}
return false;
}

/******************************
* Take address of expression.
*/
Expand Down Expand Up @@ -2360,12 +2341,6 @@ extern (C++) final class TypeExp : Expression
return true;
}

override bool checkValue()
{
error(loc, "type `%s` has no value", toChars());
return true;
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -2419,12 +2394,6 @@ extern (C++) final class ScopeExp : Expression
return false;
}

override bool checkValue()
{
error(loc, "%s `%s` has no value", sds.kind(), sds.toChars());
return true;
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -2458,12 +2427,6 @@ extern (C++) final class TemplateExp : Expression
return true;
}

override bool checkValue()
{
error(loc, "%s `%s` has no value", td.kind(), toChars());
return true;
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -2746,16 +2709,6 @@ extern (C++) final class FuncExp : Expression
return false;
}

override bool checkValue()
{
if (td)
{
error(loc, "template lambda has no value");
return true;
}
return false;
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -3179,12 +3132,6 @@ extern (C++) final class DotTemplateExp : UnaExp
return true;
}

override bool checkValue()
{
error(loc, "%s `%s` has no value", td.kind(), toChars());
return true;
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -3263,18 +3210,6 @@ extern (C++) final class DotTemplateInstanceExp : UnaExp
return false;
}

override bool checkValue()
{
if (ti.tempdecl &&
ti.semantictiargsdone &&
ti.semanticRun == PASS.initial)

error(loc, "partial %s `%s` has no value", ti.kind(), toChars());
else
error(loc, "%s `%s` has no value", ti.kind(), ti.toChars());
return true;
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down
7 changes: 0 additions & 7 deletions compiler/src/dmd/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ class Expression : public ASTNode
virtual StringExp *toStringExp();
virtual bool isLvalue();
virtual bool checkType();
virtual bool checkValue();
Expression *addressOf();
Expression *deref();

Expand Down Expand Up @@ -485,7 +484,6 @@ class TypeExp final : public Expression
public:
TypeExp *syntaxCopy() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor *v) override { v->visit(this); }
};

Expand All @@ -496,7 +494,6 @@ class ScopeExp final : public Expression

ScopeExp *syntaxCopy() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor *v) override { v->visit(this); }
};

Expand All @@ -508,7 +505,6 @@ class TemplateExp final : public Expression

bool isLvalue() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor *v) override { v->visit(this); }
};

Expand Down Expand Up @@ -608,7 +604,6 @@ class FuncExp final : public Expression
FuncExp *syntaxCopy() override;
const char *toChars() const override;
bool checkType() override;
bool checkValue() override;

void accept(Visitor *v) override { v->visit(this); }
};
Expand Down Expand Up @@ -752,7 +747,6 @@ class DotTemplateExp final : public UnaExp
TemplateDeclaration *td;

bool checkType() override;
bool checkValue() override;
void accept(Visitor *v) override { v->visit(this); }
};

Expand All @@ -773,7 +767,6 @@ class DotTemplateInstanceExp final : public UnaExp

DotTemplateInstanceExp *syntaxCopy() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor *v) override { v->visit(this); }
};

Expand Down
69 changes: 69 additions & 0 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -14829,6 +14829,75 @@ private bool checkArithmeticBin(BinExp e)
return (e.e1.checkArithmetic(e.op) || e.e2.checkArithmetic(e.op));
}

/****************************************
* Check that the expression has a valid value.
* If not, generates an error "... has no value".`
*
* Params:
* e = expression to check
*
* Returns:
* `true` if the expression is not valid or has `void` type.
*/
bool checkValue(Expression e)
{
if (auto te = e.isTypeExp())
{
error(e.loc, "type `%s` has no value", e.toChars());
return true;
}

if (auto dtie = e.isDotTemplateInstanceExp())
{
if (dtie.ti.tempdecl &&
dtie.ti.semantictiargsdone &&
dtie.ti.semanticRun == PASS.initial)

error(e.loc, "partial %s `%s` has no value", dtie.ti.kind(), e.toChars());
else
error(e.loc, "%s `%s` has no value", dtie.ti.kind(), dtie.ti.toChars());
return true;
}

if (auto se = e.isScopeExp())
{
error(e.loc, "%s `%s` has no value", se.sds.kind(), se.sds.toChars());
return true;
}

if (auto te = e.isTemplateExp())
{
error(e.loc, "%s `%s` has no value", te.td.kind(), te.toChars());
return true;
}

if (auto fe = e.isFuncExp())
{
if (fe.td)
{
error(e.loc, "template lambda has no value");
return true;
}
return false;
}

if (auto dte = e.isDotTemplateExp())
{
error(e.loc, "%s `%s` has no value", dte.td.kind(), e.toChars());
return true;
}

if (e.type && e.type.toBasetype().ty == Tvoid)
{
error(e.loc, "expression `%s` is `void` and has no value", e.toChars());
//print(); assert(0);
if (!global.gag)
e.type = Type.terror;
return true;
}
return false;
}

/***************************************
* If expression is shared, check that we can access it.
* Give error message if not.
Expand Down
7 changes: 0 additions & 7 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,6 @@ class Expression : public ASTNode
virtual StringExp* toStringExp();
virtual bool isLvalue();
virtual bool checkType();
virtual bool checkValue();
Expression* addressOf();
Expression* deref();
int32_t isConst();
Expand Down Expand Up @@ -2749,7 +2748,6 @@ class DotTemplateExp final : public UnaExp
public:
TemplateDeclaration* td;
bool checkType() override;
bool checkValue() override;
void accept(Visitor* v) override;
};

Expand All @@ -2759,7 +2757,6 @@ class DotTemplateInstanceExp final : public UnaExp
TemplateInstance* ti;
DotTemplateInstanceExp* syntaxCopy() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor* v) override;
};

Expand Down Expand Up @@ -3048,7 +3045,6 @@ class FuncExp final : public Expression
FuncExp* syntaxCopy() override;
const char* toChars() const override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor* v) override;
};

Expand Down Expand Up @@ -3366,7 +3362,6 @@ class ScopeExp final : public Expression
ScopeDsymbol* sds;
ScopeExp* syntaxCopy() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor* v) override;
};

Expand Down Expand Up @@ -3565,7 +3560,6 @@ class TemplateExp final : public Expression
FuncDeclaration* fd;
bool isLvalue() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor* v) override;
};

Expand Down Expand Up @@ -3609,7 +3603,6 @@ class TypeExp final : public Expression
public:
TypeExp* syntaxCopy() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor* v) override;
};

Expand Down

0 comments on commit 17ad9f9

Please sign in to comment.