Skip to content

Commit 478bdd8

Browse files
authored
[clang][bytecode] Save Constexpr bit in Function (#142793)
Rename isConstexpr to isValid, the former was always a bad name. Save a constexpr bit in Function so we don't have to access the decl in CheckCallable.
1 parent eca616f commit 478bdd8

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

clang/lib/AST/ByteCode/Context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
4949
if (!Run(Parent, Func))
5050
return false;
5151

52-
return Func->isConstexpr();
52+
return Func->isValid();
5353
}
5454

5555
bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {

clang/lib/AST/ByteCode/Function.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Function::Function(Program &P, FunctionDeclTy Source, unsigned ArgSize,
2727
if (const auto *F = dyn_cast<const FunctionDecl *>(Source)) {
2828
Variadic = F->isVariadic();
2929
Immediate = F->isImmediateFunction();
30+
Constexpr = F->isConstexpr() || F->hasAttr<MSConstexprAttr>();
3031
if (const auto *CD = dyn_cast<CXXConstructorDecl>(F)) {
3132
Virtual = CD->isVirtual();
3233
Kind = FunctionKind::Ctor;
@@ -48,6 +49,7 @@ Function::Function(Program &P, FunctionDeclTy Source, unsigned ArgSize,
4849
Variadic = false;
4950
Virtual = false;
5051
Immediate = false;
52+
Constexpr = false;
5153
}
5254
}
5355

clang/lib/AST/ByteCode/Function.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,13 @@ class Function final {
150150
/// Returns the source information at a given PC.
151151
SourceInfo getSource(CodePtr PC) const;
152152

153-
/// Checks if the function is valid to call in constexpr.
154-
bool isConstexpr() const { return IsValid || isLambdaStaticInvoker(); }
153+
/// Checks if the function is valid to call.
154+
bool isValid() const { return IsValid || isLambdaStaticInvoker(); }
155155

156156
/// Checks if the function is virtual.
157157
bool isVirtual() const { return Virtual; };
158158
bool isImmediate() const { return Immediate; }
159+
bool isConstexpr() const { return Constexpr; }
159160

160161
/// Checks if the function is a constructor.
161162
bool isConstructor() const { return Kind == FunctionKind::Ctor; }
@@ -303,6 +304,8 @@ class Function final {
303304
unsigned Virtual : 1;
304305
LLVM_PREFERRED_TYPE(bool)
305306
unsigned Immediate : 1;
307+
LLVM_PREFERRED_TYPE(bool)
308+
unsigned Constexpr : 1;
306309

307310
public:
308311
/// Dumps the disassembled bytecode to \c llvm::errs().

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -857,23 +857,22 @@ bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) {
857857
return false;
858858
}
859859

860-
// Bail out if the function declaration itself is invalid. We will
861-
// have produced a relevant diagnostic while parsing it, so just
862-
// note the problematic sub-expression.
863-
if (F->getDecl()->isInvalidDecl())
864-
return Invalid(S, OpPC);
865-
866860
if (S.checkingPotentialConstantExpression() && S.Current->getDepth() != 0)
867861
return false;
868862

869-
if (F->isConstexpr() && F->hasBody() &&
870-
(F->getDecl()->isConstexpr() || F->getDecl()->hasAttr<MSConstexprAttr>()))
863+
if (F->isValid() && F->hasBody() && F->isConstexpr())
871864
return true;
872865

873866
// Implicitly constexpr.
874867
if (F->isLambdaStaticInvoker())
875868
return true;
876869

870+
// Bail out if the function declaration itself is invalid. We will
871+
// have produced a relevant diagnostic while parsing it, so just
872+
// note the problematic sub-expression.
873+
if (F->getDecl()->isInvalidDecl())
874+
return Invalid(S, OpPC);
875+
877876
// Diagnose failed assertions specially.
878877
if (S.Current->getLocation(OpPC).isMacroID() &&
879878
F->getDecl()->getIdentifier()) {
@@ -923,7 +922,8 @@ bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) {
923922
// for a constant expression. It might be defined at the point we're
924923
// actually calling it.
925924
bool IsExtern = DiagDecl->getStorageClass() == SC_Extern;
926-
if (!DiagDecl->isDefined() && !IsExtern && DiagDecl->isConstexpr() &&
925+
bool IsDefined = F->isDefined();
926+
if (!IsDefined && !IsExtern && DiagDecl->isConstexpr() &&
927927
S.checkingPotentialConstantExpression())
928928
return false;
929929

0 commit comments

Comments
 (0)