2007-08-24 10:22:53 +08:00
|
|
|
//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
|
2007-08-11 04:13:28 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-11 04:13:28 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Aggregate Expr nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
2007-08-11 08:04:45 +08:00
|
|
|
#include "CodeGenModule.h"
|
2009-07-08 09:18:33 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-04-17 08:06:03 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2007-08-11 08:04:45 +08:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
2007-10-27 01:44:44 +08:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2008-04-05 02:42:16 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
2007-08-11 04:13:28 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
2007-08-11 08:04:45 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Aggregate Expression Emitter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-11-29 03:45:26 +08:00
|
|
|
class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
|
2007-08-21 12:25:47 +08:00
|
|
|
CodeGenFunction &CGF;
|
2008-11-01 09:53:16 +08:00
|
|
|
CGBuilderTy &Builder;
|
2010-09-15 18:14:12 +08:00
|
|
|
AggValueSlot Dest;
|
2009-05-27 06:03:21 +08:00
|
|
|
bool IgnoreResult;
|
2010-05-22 09:48:05 +08:00
|
|
|
|
|
|
|
ReturnValueSlot getReturnValueSlot() const {
|
2010-05-23 06:13:32 +08:00
|
|
|
// If the destination slot requires garbage collection, we can't
|
|
|
|
// use the real return value slot, because we have to use the GC
|
|
|
|
// API.
|
2010-09-16 11:13:23 +08:00
|
|
|
if (Dest.requiresGCollection()) return ReturnValueSlot();
|
2010-05-23 06:13:32 +08:00
|
|
|
|
2010-09-15 18:14:12 +08:00
|
|
|
return ReturnValueSlot(Dest.getAddr(), Dest.isVolatile());
|
|
|
|
}
|
|
|
|
|
|
|
|
AggValueSlot EnsureSlot(QualType T) {
|
|
|
|
if (!Dest.isIgnored()) return Dest;
|
|
|
|
return CGF.CreateAggTemp(T, "agg.tmp.ensured");
|
2010-05-22 09:48:05 +08:00
|
|
|
}
|
2010-05-23 06:13:32 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
public:
|
2010-09-15 18:14:12 +08:00
|
|
|
AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest,
|
2010-09-16 08:20:07 +08:00
|
|
|
bool ignore)
|
2010-09-15 18:14:12 +08:00
|
|
|
: CGF(cgf), Builder(CGF.Builder), Dest(Dest),
|
2010-09-16 08:20:07 +08:00
|
|
|
IgnoreResult(ignore) {
|
2007-08-21 12:25:47 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:59:27 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Utilities
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
/// EmitAggLoadOfLValue - Given an expression with aggregate type that
|
|
|
|
/// represents a value lvalue, this method emits the address of the lvalue,
|
|
|
|
/// then loads the result into DestPtr.
|
|
|
|
void EmitAggLoadOfLValue(const Expr *E);
|
2008-05-20 01:51:16 +08:00
|
|
|
|
2009-05-24 04:28:01 +08:00
|
|
|
/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
|
2009-05-27 06:03:21 +08:00
|
|
|
void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false);
|
|
|
|
void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false);
|
2009-05-24 04:28:01 +08:00
|
|
|
|
2010-05-23 06:13:32 +08:00
|
|
|
void EmitGCMove(const Expr *E, RValue Src);
|
|
|
|
|
|
|
|
bool TypeRequiresGCollection(QualType T);
|
|
|
|
|
2007-08-21 12:59:27 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
void VisitStmt(Stmt *S) {
|
2008-08-16 08:56:44 +08:00
|
|
|
CGF.ErrorUnsupported(S, "aggregate expression");
|
2007-08-21 12:25:47 +08:00
|
|
|
}
|
|
|
|
void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
|
2009-01-27 17:03:41 +08:00
|
|
|
void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
|
2007-08-21 12:25:47 +08:00
|
|
|
|
|
|
|
// l-values.
|
2007-12-14 10:04:12 +08:00
|
|
|
void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
|
|
|
|
void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
|
|
|
|
void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
|
2010-01-05 02:47:06 +08:00
|
|
|
void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
|
2009-04-22 07:00:09 +08:00
|
|
|
void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
2009-09-09 23:08:12 +08:00
|
|
|
EmitAggLoadOfLValue(E);
|
2009-04-22 07:00:09 +08:00
|
|
|
}
|
2007-12-14 10:04:12 +08:00
|
|
|
void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
|
|
|
|
EmitAggLoadOfLValue(E);
|
|
|
|
}
|
2009-04-22 07:00:09 +08:00
|
|
|
void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
|
2009-09-09 23:08:12 +08:00
|
|
|
EmitAggLoadOfLValue(E);
|
2009-04-22 07:00:09 +08:00
|
|
|
}
|
|
|
|
void VisitPredefinedExpr(const PredefinedExpr *E) {
|
2009-09-09 23:08:12 +08:00
|
|
|
EmitAggLoadOfLValue(E);
|
2009-04-22 07:00:09 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
// Operators.
|
2009-08-08 07:22:37 +08:00
|
|
|
void VisitCastExpr(CastExpr *E);
|
2007-11-01 06:04:46 +08:00
|
|
|
void VisitCallExpr(const CallExpr *E);
|
2007-09-01 06:54:14 +08:00
|
|
|
void VisitStmtExpr(const StmtExpr *E);
|
2007-08-21 12:25:47 +08:00
|
|
|
void VisitBinaryOperator(const BinaryOperator *BO);
|
2009-10-23 06:57:31 +08:00
|
|
|
void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
|
2007-08-21 12:43:17 +08:00
|
|
|
void VisitBinAssign(const BinaryOperator *E);
|
2008-05-20 15:56:31 +08:00
|
|
|
void VisitBinComma(const BinaryOperator *E);
|
2007-08-21 12:25:47 +08:00
|
|
|
|
2008-06-25 01:04:18 +08:00
|
|
|
void VisitObjCMessageExpr(ObjCMessageExpr *E);
|
2008-08-23 18:51:21 +08:00
|
|
|
void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
|
|
|
|
EmitAggLoadOfLValue(E);
|
|
|
|
}
|
2008-08-27 14:57:25 +08:00
|
|
|
void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
void VisitConditionalOperator(const ConditionalOperator *CO);
|
2009-07-09 02:33:14 +08:00
|
|
|
void VisitChooseExpr(const ChooseExpr *CE);
|
2007-10-27 01:44:44 +08:00
|
|
|
void VisitInitListExpr(InitListExpr *E);
|
2009-12-16 14:57:54 +08:00
|
|
|
void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
|
2008-04-08 12:40:51 +08:00
|
|
|
void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
|
|
|
|
Visit(DAE->getExpr());
|
|
|
|
}
|
2009-05-31 07:23:33 +08:00
|
|
|
void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
|
2009-05-04 01:47:16 +08:00
|
|
|
void VisitCXXConstructExpr(const CXXConstructExpr *E);
|
2009-05-19 12:48:36 +08:00
|
|
|
void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
|
2010-07-08 14:14:04 +08:00
|
|
|
void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
|
2009-11-18 08:40:12 +08:00
|
|
|
void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); }
|
2009-05-19 12:48:36 +08:00
|
|
|
|
2008-05-27 23:51:49 +08:00
|
|
|
void VisitVAArgExpr(VAArgExpr *E);
|
2008-04-05 02:42:16 +08:00
|
|
|
|
2010-02-04 01:33:16 +08:00
|
|
|
void EmitInitializationToLValue(Expr *E, LValue Address, QualType T);
|
2008-04-05 02:42:16 +08:00
|
|
|
void EmitNullInitializationToLValue(LValue Address, QualType T);
|
2007-08-21 12:25:47 +08:00
|
|
|
// case Expr::ChooseExprClass:
|
2009-12-10 03:24:08 +08:00
|
|
|
void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); }
|
2007-08-21 12:25:47 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2007-08-21 12:59:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utilities
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-08-21 12:25:47 +08:00
|
|
|
|
2007-08-11 08:04:45 +08:00
|
|
|
/// EmitAggLoadOfLValue - Given an expression with aggregate type that
|
|
|
|
/// represents a value lvalue, this method emits the address of the lvalue,
|
|
|
|
/// then loads the result into DestPtr.
|
2007-08-21 12:25:47 +08:00
|
|
|
void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
|
|
|
|
LValue LV = CGF.EmitLValue(E);
|
2009-05-24 04:28:01 +08:00
|
|
|
EmitFinalDestCopy(E, LV);
|
|
|
|
}
|
|
|
|
|
2010-05-23 06:13:32 +08:00
|
|
|
/// \brief True if the given aggregate type requires special GC API calls.
|
|
|
|
bool AggExprEmitter::TypeRequiresGCollection(QualType T) {
|
|
|
|
// Only record types have members that might require garbage collection.
|
|
|
|
const RecordType *RecordTy = T->getAs<RecordType>();
|
|
|
|
if (!RecordTy) return false;
|
|
|
|
|
|
|
|
// Don't mess with non-trivial C++ types.
|
|
|
|
RecordDecl *Record = RecordTy->getDecl();
|
|
|
|
if (isa<CXXRecordDecl>(Record) &&
|
|
|
|
(!cast<CXXRecordDecl>(Record)->hasTrivialCopyConstructor() ||
|
|
|
|
!cast<CXXRecordDecl>(Record)->hasTrivialDestructor()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check whether the type has an object member.
|
|
|
|
return Record->hasObjectMember();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Perform the final move to DestPtr if RequiresGCollection is set.
|
|
|
|
///
|
|
|
|
/// The idea is that you do something like this:
|
|
|
|
/// RValue Result = EmitSomething(..., getReturnValueSlot());
|
|
|
|
/// EmitGCMove(E, Result);
|
|
|
|
/// If GC doesn't interfere, this will cause the result to be emitted
|
|
|
|
/// directly into the return value slot. If GC does interfere, a final
|
|
|
|
/// move will be performed.
|
|
|
|
void AggExprEmitter::EmitGCMove(const Expr *E, RValue Src) {
|
2010-09-16 11:13:23 +08:00
|
|
|
if (Dest.requiresGCollection()) {
|
2010-06-16 06:44:06 +08:00
|
|
|
std::pair<uint64_t, unsigned> TypeInfo =
|
|
|
|
CGF.getContext().getTypeInfo(E->getType());
|
|
|
|
unsigned long size = TypeInfo.first/8;
|
|
|
|
const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
|
|
|
|
llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size);
|
2010-09-15 18:14:12 +08:00
|
|
|
CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, Dest.getAddr(),
|
2010-05-23 06:13:32 +08:00
|
|
|
Src.getAggregateAddr(),
|
2010-06-16 06:44:06 +08:00
|
|
|
SizeVal);
|
|
|
|
}
|
2010-05-23 06:13:32 +08:00
|
|
|
}
|
|
|
|
|
2009-05-24 04:28:01 +08:00
|
|
|
/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
|
2009-05-27 06:03:21 +08:00
|
|
|
void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
|
2009-05-24 04:28:01 +08:00
|
|
|
assert(Src.isAggregate() && "value must be aggregate value!");
|
|
|
|
|
2010-09-15 18:14:12 +08:00
|
|
|
// If Dest is ignored, then we're evaluating an aggregate expression
|
2010-08-25 10:50:31 +08:00
|
|
|
// in a context (like an expression statement) that doesn't care
|
|
|
|
// about the result. C says that an lvalue-to-rvalue conversion is
|
|
|
|
// performed in these cases; C++ says that it is not. In either
|
|
|
|
// case, we don't actually need to do anything unless the value is
|
|
|
|
// volatile.
|
2010-09-15 18:14:12 +08:00
|
|
|
if (Dest.isIgnored()) {
|
2010-08-25 10:50:31 +08:00
|
|
|
if (!Src.isVolatileQualified() ||
|
|
|
|
CGF.CGM.getLangOptions().CPlusPlus ||
|
|
|
|
(IgnoreResult && Ignore))
|
2009-05-24 06:01:27 +08:00
|
|
|
return;
|
2010-10-23 06:05:03 +08:00
|
|
|
|
2009-05-27 06:03:21 +08:00
|
|
|
// If the source is volatile, we must read from it; to do that, we need
|
|
|
|
// some place to put it.
|
2010-09-15 18:14:12 +08:00
|
|
|
Dest = CGF.CreateAggTemp(E->getType(), "agg.tmp");
|
2009-05-24 06:01:27 +08:00
|
|
|
}
|
2007-08-11 08:04:45 +08:00
|
|
|
|
2010-09-16 11:13:23 +08:00
|
|
|
if (Dest.requiresGCollection()) {
|
2010-06-16 06:44:06 +08:00
|
|
|
std::pair<uint64_t, unsigned> TypeInfo =
|
|
|
|
CGF.getContext().getTypeInfo(E->getType());
|
|
|
|
unsigned long size = TypeInfo.first/8;
|
|
|
|
const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
|
|
|
|
llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size);
|
2009-09-01 03:33:16 +08:00
|
|
|
CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
|
2010-09-15 18:14:12 +08:00
|
|
|
Dest.getAddr(),
|
|
|
|
Src.getAggregateAddr(),
|
|
|
|
SizeVal);
|
2009-09-01 03:33:16 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-05-24 04:28:01 +08:00
|
|
|
// If the result of the assignment is used, copy the LHS there also.
|
|
|
|
// FIXME: Pass VolatileDest as well. I think we also need to merge volatile
|
|
|
|
// from the source as well, as we can't eliminate it if either operand
|
|
|
|
// is volatile, unless copy has volatile for both source and destination..
|
2010-09-15 18:14:12 +08:00
|
|
|
CGF.EmitAggregateCopy(Dest.getAddr(), Src.getAggregateAddr(), E->getType(),
|
|
|
|
Dest.isVolatile()|Src.isVolatileQualified());
|
2009-05-24 04:28:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
|
2009-05-27 06:03:21 +08:00
|
|
|
void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
|
2009-05-24 04:28:01 +08:00
|
|
|
assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
|
|
|
|
|
|
|
|
EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
|
2009-05-27 06:03:21 +08:00
|
|
|
Src.isVolatileQualified()),
|
|
|
|
Ignore);
|
2007-08-11 08:04:45 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:59:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-08 07:22:37 +08:00
|
|
|
void AggExprEmitter::VisitCastExpr(CastExpr *E) {
|
2010-09-15 18:14:12 +08:00
|
|
|
if (Dest.isIgnored() && E->getCastKind() != CK_Dynamic) {
|
2010-03-08 07:24:59 +08:00
|
|
|
Visit(E->getSubExpr());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-29 09:23:39 +08:00
|
|
|
switch (E->getCastKind()) {
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_Dynamic: {
|
2010-05-15 05:31:02 +08:00
|
|
|
assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
|
|
|
|
LValue LV = CGF.EmitCheckedLValue(E->getSubExpr());
|
|
|
|
// FIXME: Do we also need to handle property references here?
|
|
|
|
if (LV.isSimple())
|
|
|
|
CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E));
|
|
|
|
else
|
|
|
|
CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast");
|
|
|
|
|
2010-09-15 18:14:12 +08:00
|
|
|
if (!Dest.isIgnored())
|
|
|
|
CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination");
|
2010-05-15 05:31:02 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ToUnion: {
|
2009-08-08 07:22:37 +08:00
|
|
|
// GCC union extension
|
2010-08-21 11:15:20 +08:00
|
|
|
QualType Ty = E->getSubExpr()->getType();
|
|
|
|
QualType PtrTy = CGF.getContext().getPointerType(Ty);
|
2010-09-15 18:14:12 +08:00
|
|
|
llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(),
|
2009-06-04 04:45:06 +08:00
|
|
|
CGF.ConvertType(PtrTy));
|
2010-08-21 11:15:20 +08:00
|
|
|
EmitInitializationToLValue(E->getSubExpr(), CGF.MakeAddrLValue(CastPtr, Ty),
|
|
|
|
Ty);
|
2009-09-29 09:23:39 +08:00
|
|
|
break;
|
2009-01-16 04:14:33 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_DerivedToBase:
|
|
|
|
case CK_BaseToDerived:
|
|
|
|
case CK_UncheckedDerivedToBase: {
|
2010-05-22 13:17:18 +08:00
|
|
|
assert(0 && "cannot perform hierarchy conversion in EmitAggExpr: "
|
|
|
|
"should have been unpacked before we got here");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_NoOp:
|
2010-12-01 12:43:34 +08:00
|
|
|
case CK_LValueToRValue:
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_UserDefinedConversion:
|
|
|
|
case CK_ConstructorConversion:
|
2009-09-29 09:23:39 +08:00
|
|
|
assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
|
|
|
|
E->getType()) &&
|
|
|
|
"Implicit cast types must be compatible");
|
|
|
|
Visit(E->getSubExpr());
|
|
|
|
break;
|
2010-12-01 12:43:34 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_LValueBitCast:
|
2010-12-01 12:43:34 +08:00
|
|
|
llvm_unreachable("should not be emitting lvalue bitcast as rvalue");
|
2010-07-14 07:17:26 +08:00
|
|
|
break;
|
2010-12-01 12:43:34 +08:00
|
|
|
|
|
|
|
case CK_Dependent:
|
|
|
|
case CK_BitCast:
|
|
|
|
case CK_ArrayToPointerDecay:
|
|
|
|
case CK_FunctionToPointerDecay:
|
|
|
|
case CK_NullToPointer:
|
|
|
|
case CK_NullToMemberPointer:
|
|
|
|
case CK_BaseToDerivedMemberPointer:
|
|
|
|
case CK_DerivedToBaseMemberPointer:
|
|
|
|
case CK_MemberPointerToBoolean:
|
|
|
|
case CK_IntegralToPointer:
|
|
|
|
case CK_PointerToIntegral:
|
|
|
|
case CK_PointerToBoolean:
|
|
|
|
case CK_ToVoid:
|
|
|
|
case CK_VectorSplat:
|
|
|
|
case CK_IntegralCast:
|
|
|
|
case CK_IntegralToBoolean:
|
|
|
|
case CK_IntegralToFloating:
|
|
|
|
case CK_FloatingToIntegral:
|
|
|
|
case CK_FloatingToBoolean:
|
|
|
|
case CK_FloatingCast:
|
|
|
|
case CK_AnyPointerToObjCPointerCast:
|
|
|
|
case CK_AnyPointerToBlockPointerCast:
|
|
|
|
case CK_ObjCObjectLValueCast:
|
|
|
|
case CK_FloatingRealToComplex:
|
|
|
|
case CK_FloatingComplexToReal:
|
|
|
|
case CK_FloatingComplexToBoolean:
|
|
|
|
case CK_FloatingComplexCast:
|
|
|
|
case CK_FloatingComplexToIntegralComplex:
|
|
|
|
case CK_IntegralRealToComplex:
|
|
|
|
case CK_IntegralComplexToReal:
|
|
|
|
case CK_IntegralComplexToBoolean:
|
|
|
|
case CK_IntegralComplexCast:
|
|
|
|
case CK_IntegralComplexToFloatingComplex:
|
|
|
|
llvm_unreachable("cast kind invalid for aggregate types");
|
2009-09-29 09:23:39 +08:00
|
|
|
}
|
2008-01-14 14:28:57 +08:00
|
|
|
}
|
|
|
|
|
2008-07-27 06:37:01 +08:00
|
|
|
void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
|
2009-05-28 00:45:02 +08:00
|
|
|
if (E->getCallReturnType()->isReferenceType()) {
|
|
|
|
EmitAggLoadOfLValue(E);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-23 06:13:32 +08:00
|
|
|
RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot());
|
|
|
|
EmitGCMove(E, RV);
|
2008-01-31 13:38:29 +08:00
|
|
|
}
|
2008-07-27 06:37:01 +08:00
|
|
|
|
|
|
|
void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
|
2010-05-23 06:13:32 +08:00
|
|
|
RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot());
|
|
|
|
EmitGCMove(E, RV);
|
2008-06-25 01:04:18 +08:00
|
|
|
}
|
2008-01-31 13:38:29 +08:00
|
|
|
|
2008-08-27 14:57:25 +08:00
|
|
|
void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
|
2010-05-23 06:13:32 +08:00
|
|
|
RValue RV = CGF.EmitObjCPropertyGet(E, getReturnValueSlot());
|
|
|
|
EmitGCMove(E, RV);
|
2008-11-23 02:39:36 +08:00
|
|
|
}
|
|
|
|
|
2008-07-27 06:37:01 +08:00
|
|
|
void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
|
2010-09-15 18:14:12 +08:00
|
|
|
CGF.EmitAnyExpr(E->getLHS(), AggValueSlot::ignored(), true);
|
|
|
|
Visit(E->getRHS());
|
2008-05-20 15:56:31 +08:00
|
|
|
}
|
|
|
|
|
2007-09-01 06:54:14 +08:00
|
|
|
void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
|
2010-09-15 18:14:12 +08:00
|
|
|
CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest);
|
2007-09-01 06:54:14 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)
|
2009-10-23 06:57:31 +08:00
|
|
|
VisitPointerToDataMemberBinaryOperator(E);
|
|
|
|
else
|
|
|
|
CGF.ErrorUnsupported(E, "aggregate binary expression");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
|
|
|
|
const BinaryOperator *E) {
|
|
|
|
LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
|
|
|
|
EmitFinalDestCopy(E, LV);
|
2007-08-21 12:59:27 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:43:17 +08:00
|
|
|
void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
|
2008-02-11 09:09:17 +08:00
|
|
|
// For an assignment to work, the value on the right has
|
|
|
|
// to be compatible with the value on the left.
|
2009-05-29 07:04:00 +08:00
|
|
|
assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
|
|
|
|
E->getRHS()->getType())
|
2008-02-11 09:09:17 +08:00
|
|
|
&& "Invalid assignment");
|
2007-08-21 12:25:47 +08:00
|
|
|
LValue LHS = CGF.EmitLValue(E->getLHS());
|
2007-08-11 08:04:45 +08:00
|
|
|
|
2008-08-30 13:35:15 +08:00
|
|
|
// We have to special case property setters, otherwise we must have
|
|
|
|
// a simple lvalue (no aggregates inside vectors, bitfields).
|
|
|
|
if (LHS.isPropertyRef()) {
|
2010-09-15 18:14:12 +08:00
|
|
|
AggValueSlot Slot = EnsureSlot(E->getRHS()->getType());
|
|
|
|
CGF.EmitAggExpr(E->getRHS(), Slot);
|
|
|
|
CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(), Slot.asRValue());
|
2009-07-31 06:28:39 +08:00
|
|
|
} else if (LHS.isKVCRef()) {
|
2010-09-15 18:14:12 +08:00
|
|
|
AggValueSlot Slot = EnsureSlot(E->getRHS()->getType());
|
|
|
|
CGF.EmitAggExpr(E->getRHS(), Slot);
|
|
|
|
CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(), Slot.asRValue());
|
2008-08-30 13:35:15 +08:00
|
|
|
} else {
|
2010-09-16 08:20:07 +08:00
|
|
|
bool GCollection = false;
|
2010-05-23 06:13:32 +08:00
|
|
|
if (CGF.getContext().getLangOptions().getGCMode())
|
2010-09-16 08:20:07 +08:00
|
|
|
GCollection = TypeRequiresGCollection(E->getLHS()->getType());
|
2010-05-23 06:13:32 +08:00
|
|
|
|
2008-08-30 13:35:15 +08:00
|
|
|
// Codegen the RHS so that it stores directly into the LHS.
|
2010-09-16 08:20:07 +08:00
|
|
|
AggValueSlot LHSSlot = AggValueSlot::forLValue(LHS, true,
|
|
|
|
GCollection);
|
|
|
|
CGF.EmitAggExpr(E->getRHS(), LHSSlot, false);
|
2009-05-27 06:03:21 +08:00
|
|
|
EmitFinalDestCopy(E, LHS, true);
|
2008-08-30 13:35:15 +08:00
|
|
|
}
|
2007-08-11 08:04:45 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
|
2009-12-25 14:17:05 +08:00
|
|
|
if (!E->getLHS()) {
|
|
|
|
CGF.ErrorUnsupported(E, "conditional operator with missing LHS");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
|
|
|
|
llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
|
|
|
|
llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-25 14:17:05 +08:00
|
|
|
CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-05 01:18:07 +08:00
|
|
|
CGF.BeginConditionalBranch();
|
2007-08-21 12:25:47 +08:00
|
|
|
CGF.EmitBlock(LHSBlock);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-11-17 08:07:33 +08:00
|
|
|
// Save whether the destination's lifetime is externally managed.
|
|
|
|
bool DestLifetimeManaged = Dest.isLifetimeExternallyManaged();
|
2007-08-11 08:04:45 +08:00
|
|
|
|
2007-08-21 13:02:10 +08:00
|
|
|
Visit(E->getLHS());
|
2010-02-05 01:18:07 +08:00
|
|
|
CGF.EndConditionalBranch();
|
2008-11-11 17:41:28 +08:00
|
|
|
CGF.EmitBranch(ContBlock);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-05 01:18:07 +08:00
|
|
|
CGF.BeginConditionalBranch();
|
2007-08-21 12:25:47 +08:00
|
|
|
CGF.EmitBlock(RHSBlock);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-11-17 08:07:33 +08:00
|
|
|
// If the result of an agg expression is unused, then the emission
|
|
|
|
// of the LHS might need to create a destination slot. That's fine
|
|
|
|
// with us, and we can safely emit the RHS into the same slot, but
|
|
|
|
// we shouldn't claim that its lifetime is externally managed.
|
|
|
|
Dest.setLifetimeExternallyManaged(DestLifetimeManaged);
|
|
|
|
|
2007-08-21 13:02:10 +08:00
|
|
|
Visit(E->getRHS());
|
2010-02-05 01:18:07 +08:00
|
|
|
CGF.EndConditionalBranch();
|
2008-11-11 17:41:28 +08:00
|
|
|
CGF.EmitBranch(ContBlock);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
CGF.EmitBlock(ContBlock);
|
2007-08-11 08:04:45 +08:00
|
|
|
}
|
2007-08-21 12:59:27 +08:00
|
|
|
|
2009-07-09 02:33:14 +08:00
|
|
|
void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
|
|
|
|
Visit(CE->getChosenSubExpr(CGF.getContext()));
|
|
|
|
}
|
|
|
|
|
2008-05-27 23:51:49 +08:00
|
|
|
void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
|
2009-02-12 06:25:55 +08:00
|
|
|
llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
|
2008-11-04 13:30:00 +08:00
|
|
|
llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
|
|
|
|
|
2009-01-10 05:09:38 +08:00
|
|
|
if (!ArgPtr) {
|
2008-11-04 13:30:00 +08:00
|
|
|
CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
|
2009-01-10 05:09:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-21 11:15:20 +08:00
|
|
|
EmitFinalDestCopy(VE, CGF.MakeAddrLValue(ArgPtr, VE->getType()));
|
2008-05-27 23:51:49 +08:00
|
|
|
}
|
|
|
|
|
2009-05-31 07:23:33 +08:00
|
|
|
void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
|
2010-09-15 18:14:12 +08:00
|
|
|
// Ensure that we have a slot, but if we already do, remember
|
|
|
|
// whether its lifetime was externally managed.
|
|
|
|
bool WasManaged = Dest.isLifetimeExternallyManaged();
|
|
|
|
Dest = EnsureSlot(E->getType());
|
|
|
|
Dest.setLifetimeExternallyManaged();
|
|
|
|
|
|
|
|
Visit(E->getSubExpr());
|
|
|
|
|
|
|
|
// Set up the temporary's destructor if its lifetime wasn't already
|
|
|
|
// being managed.
|
|
|
|
if (!WasManaged)
|
|
|
|
CGF.EmitCXXTemporary(E->getTemporary(), Dest.getAddr());
|
2009-05-31 07:23:33 +08:00
|
|
|
}
|
|
|
|
|
2009-04-17 08:06:03 +08:00
|
|
|
void
|
2009-05-04 01:47:16 +08:00
|
|
|
AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
|
2010-09-15 18:14:12 +08:00
|
|
|
AggValueSlot Slot = EnsureSlot(E->getType());
|
|
|
|
CGF.EmitCXXConstructExpr(E, Slot);
|
2009-05-19 12:48:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
|
2010-09-15 18:14:12 +08:00
|
|
|
CGF.EmitCXXExprWithTemporaries(E, Dest);
|
2009-04-17 08:06:03 +08:00
|
|
|
}
|
|
|
|
|
2010-07-08 14:14:04 +08:00
|
|
|
void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
|
2010-09-15 18:14:12 +08:00
|
|
|
QualType T = E->getType();
|
|
|
|
AggValueSlot Slot = EnsureSlot(T);
|
|
|
|
EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T), T);
|
2009-12-16 14:57:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
|
2010-09-15 18:14:12 +08:00
|
|
|
QualType T = E->getType();
|
|
|
|
AggValueSlot Slot = EnsureSlot(T);
|
|
|
|
EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T), T);
|
2009-10-18 23:18:11 +08:00
|
|
|
}
|
|
|
|
|
2010-02-04 01:33:16 +08:00
|
|
|
void
|
|
|
|
AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV, QualType T) {
|
2009-05-29 23:46:01 +08:00
|
|
|
// FIXME: Ignore result?
|
2008-04-05 02:42:16 +08:00
|
|
|
// FIXME: Are initializers affected by volatile?
|
2009-01-30 01:44:32 +08:00
|
|
|
if (isa<ImplicitValueInitExpr>(E)) {
|
2010-02-04 01:33:16 +08:00
|
|
|
EmitNullInitializationToLValue(LV, T);
|
2010-02-04 03:13:55 +08:00
|
|
|
} else if (T->isReferenceType()) {
|
2010-06-27 00:35:32 +08:00
|
|
|
RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
|
2010-02-04 03:13:55 +08:00
|
|
|
CGF.EmitStoreThroughLValue(RV, LV, T);
|
2010-02-04 01:33:16 +08:00
|
|
|
} else if (T->isAnyComplexType()) {
|
2009-01-30 01:44:32 +08:00
|
|
|
CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
|
2010-02-04 01:33:16 +08:00
|
|
|
} else if (CGF.hasAggregateLLVMType(T)) {
|
2010-09-15 18:14:12 +08:00
|
|
|
CGF.EmitAggExpr(E, AggValueSlot::forAddr(LV.getAddress(), false, true));
|
2008-05-12 23:06:05 +08:00
|
|
|
} else {
|
2010-02-04 01:33:16 +08:00
|
|
|
CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, T);
|
2008-04-05 02:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
2008-02-19 06:44:02 +08:00
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
|
|
|
|
if (!CGF.hasAggregateLLVMType(T)) {
|
|
|
|
// For non-aggregates, we can store zero
|
2009-08-01 04:28:54 +08:00
|
|
|
llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
|
2008-08-06 13:32:55 +08:00
|
|
|
CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
|
2008-04-05 02:42:16 +08:00
|
|
|
} else {
|
|
|
|
// There's a potential optimization opportunity in combining
|
|
|
|
// memsets; that would be easy for arrays, but relatively
|
|
|
|
// difficult for structures with the current code.
|
2010-05-23 01:35:42 +08:00
|
|
|
CGF.EmitNullInitialization(LV.getAddress(), T);
|
2008-04-05 02:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
2008-02-19 06:44:02 +08:00
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
|
2008-12-02 09:17:45 +08:00
|
|
|
#if 0
|
2009-12-04 09:30:56 +08:00
|
|
|
// FIXME: Assess perf here? Figure out what cases are worth optimizing here
|
|
|
|
// (Length of globals? Chunks of zeroed-out space?).
|
2008-12-02 09:17:45 +08:00
|
|
|
//
|
2009-05-16 15:57:57 +08:00
|
|
|
// If we can, prefer a copy from a global; this is a lot less code for long
|
|
|
|
// globals, and it's easier for the current optimizers to analyze.
|
2009-12-04 09:30:56 +08:00
|
|
|
if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
|
2008-11-30 10:11:09 +08:00
|
|
|
llvm::GlobalVariable* GV =
|
2009-12-04 09:30:56 +08:00
|
|
|
new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
|
|
|
|
llvm::GlobalValue::InternalLinkage, C, "");
|
2010-08-21 11:15:20 +08:00
|
|
|
EmitFinalDestCopy(E, CGF.MakeAddrLValue(GV, E->getType()));
|
2008-11-30 10:11:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-12-02 09:17:45 +08:00
|
|
|
#endif
|
2010-09-06 08:11:41 +08:00
|
|
|
if (E->hadArrayRangeDesignator())
|
2009-01-30 03:42:23 +08:00
|
|
|
CGF.ErrorUnsupported(E, "GNU array range designator extension");
|
|
|
|
|
2010-09-15 18:14:12 +08:00
|
|
|
llvm::Value *DestPtr = Dest.getAddr();
|
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
// Handle initialization of an array.
|
|
|
|
if (E->getType()->isArrayType()) {
|
|
|
|
const llvm::PointerType *APType =
|
|
|
|
cast<llvm::PointerType>(DestPtr->getType());
|
|
|
|
const llvm::ArrayType *AType =
|
|
|
|
cast<llvm::ArrayType>(APType->getElementType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
uint64_t NumInitElements = E->getNumInits();
|
2008-05-20 01:51:16 +08:00
|
|
|
|
2008-07-27 06:37:01 +08:00
|
|
|
if (E->getNumInits() > 0) {
|
|
|
|
QualType T1 = E->getType();
|
|
|
|
QualType T2 = E->getInit(0)->getType();
|
2009-05-29 07:04:00 +08:00
|
|
|
if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
|
2008-07-27 06:37:01 +08:00
|
|
|
EmitAggLoadOfLValue(E->getInit(0));
|
|
|
|
return;
|
|
|
|
}
|
2008-05-20 01:51:16 +08:00
|
|
|
}
|
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
uint64_t NumArrayElements = AType->getNumElements();
|
2008-08-04 15:31:14 +08:00
|
|
|
QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
|
2009-01-29 05:54:33 +08:00
|
|
|
ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-25 03:53:00 +08:00
|
|
|
// FIXME: were we intentionally ignoring address spaces and GC attributes?
|
2008-06-14 07:01:12 +08:00
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
for (uint64_t i = 0; i != NumArrayElements; ++i) {
|
|
|
|
llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
|
2010-08-21 11:08:16 +08:00
|
|
|
LValue LV = CGF.MakeAddrLValue(NextVal, ElementType);
|
2008-04-05 02:42:16 +08:00
|
|
|
if (i < NumInitElements)
|
2010-08-21 11:08:16 +08:00
|
|
|
EmitInitializationToLValue(E->getInit(i), LV, ElementType);
|
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
else
|
2010-08-21 11:08:16 +08:00
|
|
|
EmitNullInitializationToLValue(LV, ElementType);
|
2008-04-05 02:42:16 +08:00
|
|
|
}
|
2008-02-19 06:44:02 +08:00
|
|
|
return;
|
2008-04-05 02:42:16 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
assert(E->getType()->isRecordType() && "Only support structs/unions here!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
// Do struct initialization; this code just sets each individual member
|
|
|
|
// to the approprate value. This makes bitfield support automatic;
|
|
|
|
// the disadvantage is that the generated code is more difficult for
|
|
|
|
// the optimizer, especially with bitfields.
|
|
|
|
unsigned NumInitElements = E->getNumInits();
|
2009-07-30 05:53:49 +08:00
|
|
|
RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
|
2010-09-06 08:13:11 +08:00
|
|
|
|
2009-01-30 00:53:55 +08:00
|
|
|
if (E->getType()->isUnionType()) {
|
|
|
|
// Only initialize one field of a union. The field itself is
|
|
|
|
// specified by the initializer list.
|
|
|
|
if (!E->getInitializedFieldInUnion()) {
|
|
|
|
// Empty union; we have nothing to do.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-30 00:53:55 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// Make sure that it's really an empty and not a failure of
|
|
|
|
// semantic analysis.
|
2009-06-30 10:36:12 +08:00
|
|
|
for (RecordDecl::field_iterator Field = SD->field_begin(),
|
|
|
|
FieldEnd = SD->field_end();
|
2009-01-30 00:53:55 +08:00
|
|
|
Field != FieldEnd; ++Field)
|
|
|
|
assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: volatility
|
|
|
|
FieldDecl *Field = E->getInitializedFieldInUnion();
|
2010-02-04 03:13:55 +08:00
|
|
|
LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0);
|
2009-01-30 00:53:55 +08:00
|
|
|
|
|
|
|
if (NumInitElements) {
|
|
|
|
// Store the initializer into the field
|
2010-02-04 01:33:16 +08:00
|
|
|
EmitInitializationToLValue(E->getInit(0), FieldLoc, Field->getType());
|
2009-01-30 00:53:55 +08:00
|
|
|
} else {
|
|
|
|
// Default-initialize to null
|
|
|
|
EmitNullInitializationToLValue(FieldLoc, Field->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
// Here we iterate over the fields; this makes it simpler to both
|
|
|
|
// default-initialize fields and skip over unnamed fields.
|
2010-09-06 08:13:11 +08:00
|
|
|
unsigned CurInitVal = 0;
|
2009-06-30 10:36:12 +08:00
|
|
|
for (RecordDecl::field_iterator Field = SD->field_begin(),
|
|
|
|
FieldEnd = SD->field_end();
|
2008-12-12 00:49:14 +08:00
|
|
|
Field != FieldEnd; ++Field) {
|
|
|
|
// We're done once we hit the flexible array member
|
|
|
|
if (Field->getType()->isIncompleteArrayType())
|
|
|
|
break;
|
|
|
|
|
2009-01-29 07:36:17 +08:00
|
|
|
if (Field->isUnnamedBitfield())
|
2008-04-05 02:42:16 +08:00
|
|
|
continue;
|
2009-01-29 07:36:17 +08:00
|
|
|
|
2008-06-14 07:01:12 +08:00
|
|
|
// FIXME: volatility
|
2010-02-04 03:13:55 +08:00
|
|
|
LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, *Field, 0);
|
2009-05-28 03:54:11 +08:00
|
|
|
// We never generate write-barries for initialized fields.
|
2010-08-21 11:22:38 +08:00
|
|
|
FieldLoc.setNonGC(true);
|
2008-04-05 02:42:16 +08:00
|
|
|
if (CurInitVal < NumInitElements) {
|
2010-03-09 05:08:07 +08:00
|
|
|
// Store the initializer into the field.
|
|
|
|
EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc,
|
2010-02-04 01:33:16 +08:00
|
|
|
Field->getType());
|
2008-04-05 02:42:16 +08:00
|
|
|
} else {
|
|
|
|
// We're out of initalizers; default-initialize to null
|
2008-12-12 00:49:14 +08:00
|
|
|
EmitNullInitializationToLValue(FieldLoc, Field->getType());
|
2008-04-05 02:42:16 +08:00
|
|
|
}
|
2008-02-20 03:27:31 +08:00
|
|
|
}
|
2007-10-27 01:44:44 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:59:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Entry Points into this File
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-27 02:57:45 +08:00
|
|
|
/// EmitAggExpr - Emit the computation of the specified expression of aggregate
|
|
|
|
/// type. The result is computed into DestPtr. Note that if DestPtr is null,
|
|
|
|
/// the value of the aggregate expression is not needed. If VolatileDest is
|
|
|
|
/// true, DestPtr cannot be 0.
|
2010-09-15 18:14:12 +08:00
|
|
|
///
|
|
|
|
/// \param IsInitializer - true if this evaluation is initializing an
|
|
|
|
/// object whose lifetime is already being managed.
|
2010-02-06 03:38:31 +08:00
|
|
|
//
|
|
|
|
// FIXME: Take Qualifiers object.
|
2010-09-15 18:14:12 +08:00
|
|
|
void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot,
|
2010-09-16 08:20:07 +08:00
|
|
|
bool IgnoreResult) {
|
2007-08-21 12:59:27 +08:00
|
|
|
assert(E && hasAggregateLLVMType(E->getType()) &&
|
|
|
|
"Invalid aggregate expression to emit");
|
2010-09-15 18:14:12 +08:00
|
|
|
assert((Slot.getAddr() != 0 || Slot.isIgnored())
|
|
|
|
&& "slot has bits but no address");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-09-16 08:20:07 +08:00
|
|
|
AggExprEmitter(*this, Slot, IgnoreResult)
|
2009-05-27 06:03:21 +08:00
|
|
|
.Visit(const_cast<Expr*>(E));
|
2007-08-21 12:59:27 +08:00
|
|
|
}
|
2008-09-10 04:49:46 +08:00
|
|
|
|
2010-02-06 03:38:31 +08:00
|
|
|
LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
|
|
|
|
assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!");
|
2010-02-09 10:48:28 +08:00
|
|
|
llvm::Value *Temp = CreateMemTemp(E->getType());
|
2010-08-21 11:15:20 +08:00
|
|
|
LValue LV = MakeAddrLValue(Temp, E->getType());
|
2010-09-15 18:14:12 +08:00
|
|
|
AggValueSlot Slot
|
|
|
|
= AggValueSlot::forAddr(Temp, LV.isVolatileQualified(), false);
|
|
|
|
EmitAggExpr(E, Slot);
|
2010-08-21 11:15:20 +08:00
|
|
|
return LV;
|
2010-02-06 03:38:31 +08:00
|
|
|
}
|
|
|
|
|
2008-09-10 04:49:46 +08:00
|
|
|
void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
|
2009-05-24 06:29:41 +08:00
|
|
|
llvm::Value *SrcPtr, QualType Ty,
|
|
|
|
bool isVolatile) {
|
2008-09-10 04:49:46 +08:00
|
|
|
assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-03 09:20:20 +08:00
|
|
|
if (getContext().getLangOptions().CPlusPlus) {
|
|
|
|
if (const RecordType *RT = Ty->getAs<RecordType>()) {
|
2010-05-20 23:39:01 +08:00
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
assert((Record->hasTrivialCopyConstructor() ||
|
2010-05-21 00:46:55 +08:00
|
|
|
Record->hasTrivialCopyAssignment()) &&
|
2010-05-20 23:39:01 +08:00
|
|
|
"Trying to aggregate-copy a type without a trivial copy "
|
|
|
|
"constructor or assignment operator");
|
2010-05-20 23:48:29 +08:00
|
|
|
// Ignore empty classes in C++.
|
2010-05-20 23:39:01 +08:00
|
|
|
if (Record->isEmpty())
|
2010-05-03 09:20:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-01 02:31:01 +08:00
|
|
|
// Aggregate assignment turns into llvm.memcpy. This is almost valid per
|
2009-03-01 02:18:58 +08:00
|
|
|
// C99 6.5.16.1p3, which states "If the value being stored in an object is
|
|
|
|
// read from another object that overlaps in anyway the storage of the first
|
|
|
|
// object, then the overlap shall be exact and the two objects shall have
|
|
|
|
// qualified or unqualified versions of a compatible type."
|
|
|
|
//
|
2009-03-01 02:31:01 +08:00
|
|
|
// memcpy is not defined if the source and destination pointers are exactly
|
2009-03-01 02:18:58 +08:00
|
|
|
// equal, but other compilers do this optimization, and almost every memcpy
|
|
|
|
// implementation handles this case safely. If there is a libc that does not
|
|
|
|
// safely handle this, we can add a target hook.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-10 04:49:46 +08:00
|
|
|
// Get size and alignment info for this aggregate.
|
|
|
|
std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-10 04:49:46 +08:00
|
|
|
// FIXME: Handle variable sized types.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-23 12:13:59 +08:00
|
|
|
// FIXME: If we have a volatile struct, the optimizer can remove what might
|
|
|
|
// appear to be `extra' memory ops:
|
|
|
|
//
|
|
|
|
// volatile struct { int i; } a, b;
|
|
|
|
//
|
|
|
|
// int main() {
|
|
|
|
// a = b;
|
|
|
|
// a = b;
|
|
|
|
// }
|
|
|
|
//
|
2010-04-04 11:10:52 +08:00
|
|
|
// we need to use a different call here. We use isVolatile to indicate when
|
2009-05-27 06:03:21 +08:00
|
|
|
// either the source or the destination is volatile.
|
2010-04-04 11:10:52 +08:00
|
|
|
|
|
|
|
const llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
|
2010-07-08 08:07:45 +08:00
|
|
|
const llvm::Type *DBP =
|
|
|
|
llvm::Type::getInt8PtrTy(VMContext, DPT->getAddressSpace());
|
|
|
|
DestPtr = Builder.CreateBitCast(DestPtr, DBP, "tmp");
|
2010-04-04 11:10:52 +08:00
|
|
|
|
|
|
|
const llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
|
2010-07-08 08:07:45 +08:00
|
|
|
const llvm::Type *SBP =
|
|
|
|
llvm::Type::getInt8PtrTy(VMContext, SPT->getAddressSpace());
|
|
|
|
SrcPtr = Builder.CreateBitCast(SrcPtr, SBP, "tmp");
|
2010-04-04 11:10:52 +08:00
|
|
|
|
2010-06-16 06:44:06 +08:00
|
|
|
if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
|
|
|
|
RecordDecl *Record = RecordTy->getDecl();
|
|
|
|
if (Record->hasObjectMember()) {
|
|
|
|
unsigned long size = TypeInfo.first/8;
|
|
|
|
const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
|
|
|
|
llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size);
|
|
|
|
CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
|
|
|
|
SizeVal);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (getContext().getAsArrayType(Ty)) {
|
|
|
|
QualType BaseType = getContext().getBaseElementType(Ty);
|
|
|
|
if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) {
|
|
|
|
if (RecordTy->getDecl()->hasObjectMember()) {
|
|
|
|
unsigned long size = TypeInfo.first/8;
|
|
|
|
const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
|
|
|
|
llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size);
|
|
|
|
CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
|
|
|
|
SizeVal);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-04 11:10:52 +08:00
|
|
|
Builder.CreateCall5(CGM.getMemCpyFn(DestPtr->getType(), SrcPtr->getType(),
|
2010-06-27 15:15:29 +08:00
|
|
|
IntPtrTy),
|
2008-09-10 04:49:46 +08:00
|
|
|
DestPtr, SrcPtr,
|
|
|
|
// TypeInfo.first describes size in bits.
|
2010-06-27 15:15:29 +08:00
|
|
|
llvm::ConstantInt::get(IntPtrTy, TypeInfo.first/8),
|
2010-07-08 08:07:45 +08:00
|
|
|
Builder.getInt32(TypeInfo.second/8),
|
|
|
|
Builder.getInt1(isVolatile));
|
2008-09-10 04:49:46 +08:00
|
|
|
}
|