Move Expression.checkValue to expressionsem.d (#20797)

This commit is contained in:
Dennis 2025-01-28 23:33:28 +01:00 committed by GitHub
parent acaa88abee
commit 17ad9f98a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 70 additions and 80 deletions

View file

@ -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;

View file

@ -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.
*/
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);

View file

@ -101,7 +101,6 @@ public:
virtual StringExp *toStringExp();
virtual bool isLvalue();
virtual bool checkType();
virtual bool checkValue();
Expression *addressOf();
Expression *deref();
@ -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); }
};
@ -496,7 +494,6 @@ public:
ScopeExp *syntaxCopy() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor *v) override { v->visit(this); }
};
@ -508,7 +505,6 @@ public:
bool isLvalue() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor *v) override { v->visit(this); }
};
@ -608,7 +604,6 @@ public:
FuncExp *syntaxCopy() override;
const char *toChars() const override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor *v) override { v->visit(this); }
};
@ -752,7 +747,6 @@ public:
TemplateDeclaration *td;
bool checkType() override;
bool checkValue() override;
void accept(Visitor *v) override { v->visit(this); }
};
@ -773,7 +767,6 @@ public:
DotTemplateInstanceExp *syntaxCopy() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor *v) override { v->visit(this); }
};

View file

@ -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.

View file

@ -2242,7 +2242,6 @@ public:
virtual StringExp* toStringExp();
virtual bool isLvalue();
virtual bool checkType();
virtual bool checkValue();
Expression* addressOf();
Expression* deref();
int32_t isConst();
@ -2749,7 +2748,6 @@ class DotTemplateExp final : public UnaExp
public:
TemplateDeclaration* td;
bool checkType() override;
bool checkValue() override;
void accept(Visitor* v) override;
};
@ -2759,7 +2757,6 @@ public:
TemplateInstance* ti;
DotTemplateInstanceExp* syntaxCopy() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor* v) override;
};
@ -3048,7 +3045,6 @@ public:
FuncExp* syntaxCopy() override;
const char* toChars() const override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor* v) override;
};
@ -3366,7 +3362,6 @@ public:
ScopeDsymbol* sds;
ScopeExp* syntaxCopy() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor* v) override;
};
@ -3565,7 +3560,6 @@ public:
FuncDeclaration* fd;
bool isLvalue() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor* v) override;
};
@ -3609,7 +3603,6 @@ class TypeExp final : public Expression
public:
TypeExp* syntaxCopy() override;
bool checkType() override;
bool checkValue() override;
void accept(Visitor* v) override;
};