From 4d8673b645ad86e496b886a0f80b60763f67071d Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Fri, 7 Aug 2009 23:22:37 +0000 Subject: [PATCH] Add CK_ToUnion and use it for aggregate expression codegen. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78429 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Expr.h | 5 ++++- lib/CodeGen/CGExprAgg.cpp | 17 ++++++++--------- lib/Sema/SemaExpr.cpp | 2 ++ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index e65a842d3f..0c884d88b7 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -1178,7 +1178,10 @@ public: CK_DerivedToBase, /// CK_Dynamic - Dynamic cast. - CK_Dynamic + CK_Dynamic, + + /// CK_ToUnion - Cast to union (GCC extension). + CK_ToUnion }; private: diff --git a/lib/CodeGen/CGExprAgg.cpp b/lib/CodeGen/CGExprAgg.cpp index ea0057e40f..b0d91aa916 100644 --- a/lib/CodeGen/CGExprAgg.cpp +++ b/lib/CodeGen/CGExprAgg.cpp @@ -86,8 +86,7 @@ public: } // Operators. - void VisitCStyleCastExpr(CStyleCastExpr *E); - void VisitImplicitCastExpr(ImplicitCastExpr *E); + void VisitCastExpr(CastExpr *E); void VisitCallExpr(const CallExpr *E); void VisitStmtExpr(const StmtExpr *E); void VisitBinaryOperator(const BinaryOperator *BO); @@ -166,9 +165,9 @@ void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) { // Visitor Methods //===----------------------------------------------------------------------===// -void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) { - // GCC union extension - if (E->getSubExpr()->getType()->isScalarType()) { +void AggExprEmitter::VisitCastExpr(CastExpr *E) { + if (E->getCastKind() == CastExpr::CK_ToUnion) { + // GCC union extension QualType PtrTy = CGF.getContext().getPointerType(E->getSubExpr()->getType()); llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr, @@ -178,10 +177,10 @@ void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) { return; } - Visit(E->getSubExpr()); -} - -void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) { + // FIXME: Remove the CK_Unknown check here. + assert((E->getCastKind() == CastExpr::CK_NoOp || + E->getCastKind() == CastExpr::CK_Unknown) && + "Only no-op casts allowed!"); assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), E->getType()) && "Implicit cast types must be compatible"); diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 684c2dafdb..6f75b59c30 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -2990,6 +2990,7 @@ bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, // FIXME: Check that the cast destination type is complete. Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) << castType << castExpr->getSourceRange(); + Kind = CastExpr::CK_NoOp; } else if (castType->isUnionType()) { // GCC cast to union extension RecordDecl *RD = castType->getAs()->getDecl(); @@ -3006,6 +3007,7 @@ bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, if (Field == FieldEnd) return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) << castExpr->getType() << castExpr->getSourceRange(); + Kind = CastExpr::CK_ToUnion; } else { // Reject any other conversions to non-scalar types. return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) -- 2.40.0