Factor out computation of whether a typeid's expression is potentially

evaluated into a CXXTypeid member function. No functionality change.

llvm-svn: 161779
This commit is contained in:
Richard Smith 2012-08-13 20:08:14 +00:00
parent 0bb7f23cfc
commit ef8bf4368e
7 changed files with 31 additions and 27 deletions

View File

@ -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<TypeSourceInfo *>(); }
/// \brief Retrieves the type operand of this typeid() expression after

View File

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

View File

@ -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<CXXTypeidExpr>(this);
if (TE->isTypeOperand())
return false;
const CXXRecordDecl *RD =
TE->getExprOperand()->getType()->getAsCXXRecordDecl();
if (!RD || !RD->isPolymorphic() ||
!TE->getExprOperand()->
Classify(const_cast<ASTContext&>(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<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
case CXXConstructExprClass:
case CXXTemporaryObjectExprClass: {

View File

@ -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<TypeSourceInfo *>()->getType().getNonReferenceType()

View File

@ -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()

View File

@ -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<RecordType>()) {
const CXXRecordDecl *RD = cast<CXXRecordDecl>(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),

View File

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