From 17ad9f98a1cda1fb1fc5ec0d9e4544f3646b3c50 Mon Sep 17 00:00:00 2001 From: Dennis Date: Tue, 28 Jan 2025 23:33:28 +0100 Subject: [PATCH] Move Expression.checkValue to expressionsem.d (#20797) --- compiler/src/dmd/dtemplate.d | 2 +- compiler/src/dmd/expression.d | 65 ------------------------------ compiler/src/dmd/expression.h | 7 ---- compiler/src/dmd/expressionsem.d | 69 ++++++++++++++++++++++++++++++++ compiler/src/dmd/frontend.h | 7 ---- 5 files changed, 70 insertions(+), 80 deletions(-) diff --git a/compiler/src/dmd/dtemplate.d b/compiler/src/dmd/dtemplate.d index dd9f3dac6b04..38298c04681b 100644 --- a/compiler/src/dmd/dtemplate.d +++ b/compiler/src/dmd/dtemplate.d @@ -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; diff --git a/compiler/src/dmd/expression.d b/compiler/src/dmd/expression.d index 55c12b50331b..101e5872439a 100644 --- a/compiler/src/dmd/expression.d +++ b/compiler/src/dmd/expression.d @@ -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); diff --git a/compiler/src/dmd/expression.h b/compiler/src/dmd/expression.h index 5aa0029a7c0d..1ee669b24cb0 100644 --- a/compiler/src/dmd/expression.h +++ b/compiler/src/dmd/expression.h @@ -101,7 +101,6 @@ class Expression : public ASTNode 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 @@ class ScopeExp final : public Expression ScopeExp *syntaxCopy() override; bool checkType() override; - bool checkValue() override; void accept(Visitor *v) override { v->visit(this); } }; @@ -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); } }; @@ -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); } }; @@ -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); } }; @@ -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); } }; diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index 22b72646e5d0..526ec6e34113 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -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. diff --git a/compiler/src/dmd/frontend.h b/compiler/src/dmd/frontend.h index ad7afc3ab8e2..1fd1f1769705 100644 --- a/compiler/src/dmd/frontend.h +++ b/compiler/src/dmd/frontend.h @@ -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(); @@ -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 @@ class DotTemplateInstanceExp final : public UnaExp TemplateInstance* ti; DotTemplateInstanceExp* syntaxCopy() override; bool checkType() override; - bool checkValue() override; void accept(Visitor* v) override; }; @@ -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; }; @@ -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; }; @@ -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; }; @@ -3609,7 +3603,6 @@ class TypeExp final : public Expression public: TypeExp* syntaxCopy() override; bool checkType() override; - bool checkValue() override; void accept(Visitor* v) override; };