From 0d729105ecb50a7e3cbe6e57c29149edfa5cf05a Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Mon, 13 Aug 2012 20:08:14 +0000 Subject: [PATCH] Factor out computation of whether a typeid's expression is potentially evaluated into a CXXTypeid member function. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@161779 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/ExprCXX.h | 4 ++++ lib/AST/ASTContext.cpp | 2 +- lib/AST/Expr.cpp | 20 ++++---------------- lib/AST/ExprCXX.cpp | 15 +++++++++++++++ lib/AST/ExprConstant.cpp | 3 +++ lib/CodeGen/CGExprCXX.cpp | 12 +++--------- lib/Sema/SemaExprCXX.cpp | 2 +- 7 files changed, 31 insertions(+), 27 deletions(-) diff --git a/include/clang/AST/ExprCXX.h b/include/clang/AST/ExprCXX.h index 7a1ca21e92..ecfa9e2136 100644 --- a/include/clang/AST/ExprCXX.h +++ b/include/clang/AST/ExprCXX.h @@ -499,6 +499,10 @@ public: Operand = (TypeSourceInfo*)0; } + /// Determine whether this typeid has a type operand which is potentially + /// evaluated, per C++11 [expr.typeid]p3. + bool isPotentiallyEvaluated() const; + bool isTypeOperand() const { return Operand.is(); } /// \brief Retrieves the type operand of this typeid() expression after diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 1426a29f17..ad48dffb4d 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -3177,7 +3177,7 @@ QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { if (Canon) { // We already have a "canonical" version of an equivalent, dependent // decltype type. Use that as our canonical type. - dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, + dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, QualType((DecltypeType*)Canon, 0)); } else { // Build a new, canonical typeof(expr) type. diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 8ae7a2ddc3..24361efafa 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -2757,22 +2757,10 @@ bool Expr::HasSideEffects(const ASTContext &Ctx) const { break; } - case CXXTypeidExprClass: { - // A typeid expression has side-effects if it can throw. - const CXXTypeidExpr *TE = cast(this); - if (TE->isTypeOperand()) - return false; - const CXXRecordDecl *RD = - TE->getExprOperand()->getType()->getAsCXXRecordDecl(); - if (!RD || !RD->isPolymorphic() || - !TE->getExprOperand()-> - Classify(const_cast(Ctx)).isGLValue()) - // Not a glvalue of polymorphic class type: the expression is an - // unevaluated operand. - return false; - // Might throw. - return true; - } + case CXXTypeidExprClass: + // typeid might throw if its subexpression is potentially-evaluated, so has + // side-effects in that case whether or not its subexpression does. + return cast(this)->isPotentiallyEvaluated(); case CXXConstructExprClass: case CXXTemporaryObjectExprClass: { diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp index ec9c978f3c..b290fb423a 100644 --- a/lib/AST/ExprCXX.cpp +++ b/lib/AST/ExprCXX.cpp @@ -24,6 +24,21 @@ using namespace clang; // Child Iterators for iterating over subexpressions/substatements //===----------------------------------------------------------------------===// +bool CXXTypeidExpr::isPotentiallyEvaluated() const { + if (isTypeOperand()) + return false; + + // C++11 [expr.typeid]p3: + // When typeid is applied to an expression other than a glvalue of + // polymorphic class type, [...] the expression is an unevaluated operand. + const Expr *E = getExprOperand(); + if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl()) + if (RD->isPolymorphic() && E->isGLValue()) + return true; + + return false; +} + QualType CXXTypeidExpr::getTypeOperand() const { assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); return Operand.get()->getType().getNonReferenceType() diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index 567002cf82..06c41a2e7e 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -2881,6 +2881,9 @@ bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { if (E->isTypeOperand()) return Success(E); CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl(); + // FIXME: The standard says "a typeid expression whose operand is of a + // polymorphic class type" is not a constant expression, but it probably + // means "a typeid expression whose operand is potentially evaluated". if (RD && RD->isPolymorphic()) { Info.Diag(E, diag::note_constexpr_typeid_polymorphic) << E->getExprOperand()->getType() diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp index 79ebe515b0..7c2c9f1ecb 100644 --- a/lib/CodeGen/CGExprCXX.cpp +++ b/lib/CodeGen/CGExprCXX.cpp @@ -1643,15 +1643,9 @@ llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { // polymorphic class type, the result refers to a std::type_info object // representing the type of the most derived object (that is, the dynamic // type) to which the glvalue refers. - if (E->getExprOperand()->isGLValue()) { - if (const RecordType *RT = - E->getExprOperand()->getType()->getAs()) { - const CXXRecordDecl *RD = cast(RT->getDecl()); - if (RD->isPolymorphic()) - return EmitTypeidFromVTable(*this, E->getExprOperand(), - StdTypeInfoPtrTy); - } - } + if (E->isPotentiallyEvaluated()) + return EmitTypeidFromVTable(*this, E->getExprOperand(), + StdTypeInfoPtrTy); QualType OperandTy = E->getExprOperand()->getType(); return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy), diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 37450e24c0..1a3cff5bd7 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -333,7 +333,7 @@ ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, // When typeid is applied to an expression other than an glvalue of a // polymorphic class type [...] [the] expression is an unevaluated // operand. [...] - if (RecordD->isPolymorphic() && E->Classify(Context).isGLValue()) { + if (RecordD->isPolymorphic() && E->isGLValue()) { // The subexpression is potentially evaluated; switch the context // and recheck the subexpression. ExprResult Result = TranformToPotentiallyEvaluated(E); -- 2.40.0