1 //===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code to emit Aggregate Expr nodes as LLVM code.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "CGObjCRuntime.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/StmtVisitor.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Function.h"
22 #include "llvm/GlobalVariable.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Intrinsics.h"
25 using namespace clang;
26 using namespace CodeGen;
28 //===----------------------------------------------------------------------===//
29 // Aggregate Expression Emitter
30 //===----------------------------------------------------------------------===//
33 class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
40 bool RequiresGCollection;
42 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v,
43 bool ignore, bool isinit, bool requiresGCollection)
44 : CGF(cgf), Builder(CGF.Builder),
45 DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore),
46 IsInitializer(isinit), RequiresGCollection(requiresGCollection) {
49 //===--------------------------------------------------------------------===//
51 //===--------------------------------------------------------------------===//
53 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
54 /// represents a value lvalue, this method emits the address of the lvalue,
55 /// then loads the result into DestPtr.
56 void EmitAggLoadOfLValue(const Expr *E);
58 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
59 void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false);
60 void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false);
62 //===--------------------------------------------------------------------===//
64 //===--------------------------------------------------------------------===//
66 void VisitStmt(Stmt *S) {
67 CGF.ErrorUnsupported(S, "aggregate expression");
69 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
70 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
73 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
74 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
75 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
76 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
77 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
78 EmitAggLoadOfLValue(E);
80 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
81 EmitAggLoadOfLValue(E);
83 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
84 EmitAggLoadOfLValue(E);
86 void VisitPredefinedExpr(const PredefinedExpr *E) {
87 EmitAggLoadOfLValue(E);
91 void VisitCastExpr(CastExpr *E);
92 void VisitCallExpr(const CallExpr *E);
93 void VisitStmtExpr(const StmtExpr *E);
94 void VisitBinaryOperator(const BinaryOperator *BO);
95 void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
96 void VisitBinAssign(const BinaryOperator *E);
97 void VisitBinComma(const BinaryOperator *E);
98 void VisitUnaryAddrOf(const UnaryOperator *E);
100 void VisitObjCMessageExpr(ObjCMessageExpr *E);
101 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
102 EmitAggLoadOfLValue(E);
104 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
105 void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E);
107 void VisitConditionalOperator(const ConditionalOperator *CO);
108 void VisitChooseExpr(const ChooseExpr *CE);
109 void VisitInitListExpr(InitListExpr *E);
110 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
111 Visit(DAE->getExpr());
113 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
114 void VisitCXXConstructExpr(const CXXConstructExpr *E);
115 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
116 void VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
118 void VisitVAArgExpr(VAArgExpr *E);
120 void EmitInitializationToLValue(Expr *E, LValue Address);
121 void EmitNullInitializationToLValue(LValue Address, QualType T);
122 // case Expr::ChooseExprClass:
125 } // end anonymous namespace.
127 //===----------------------------------------------------------------------===//
129 //===----------------------------------------------------------------------===//
131 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
132 /// represents a value lvalue, this method emits the address of the lvalue,
133 /// then loads the result into DestPtr.
134 void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
135 LValue LV = CGF.EmitLValue(E);
136 EmitFinalDestCopy(E, LV);
139 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
140 void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
141 assert(Src.isAggregate() && "value must be aggregate value!");
143 // If the result is ignored, don't copy from the value.
145 if (!Src.isVolatileQualified() || (IgnoreResult && Ignore))
147 // If the source is volatile, we must read from it; to do that, we need
148 // some place to put it.
149 DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp");
152 if (RequiresGCollection) {
153 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
154 DestPtr, Src.getAggregateAddr(),
158 // If the result of the assignment is used, copy the LHS there also.
159 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
160 // from the source as well, as we can't eliminate it if either operand
161 // is volatile, unless copy has volatile for both source and destination..
162 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
163 VolatileDest|Src.isVolatileQualified());
166 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
167 void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
168 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
170 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
171 Src.isVolatileQualified()),
175 //===----------------------------------------------------------------------===//
177 //===----------------------------------------------------------------------===//
179 void AggExprEmitter::VisitCastExpr(CastExpr *E) {
180 switch (E->getCastKind()) {
181 default: assert(0 && "Unhandled cast kind!");
183 case CastExpr::CK_ToUnion: {
184 // GCC union extension
186 CGF.getContext().getPointerType(E->getSubExpr()->getType());
187 llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr,
188 CGF.ConvertType(PtrTy));
189 EmitInitializationToLValue(E->getSubExpr(),
190 LValue::MakeAddr(CastPtr, Qualifiers()));
194 // FIXME: Remove the CK_Unknown check here.
195 case CastExpr::CK_Unknown:
196 case CastExpr::CK_NoOp:
197 case CastExpr::CK_UserDefinedConversion:
198 case CastExpr::CK_ConstructorConversion:
199 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
201 "Implicit cast types must be compatible");
202 Visit(E->getSubExpr());
205 case CastExpr::CK_NullToMemberPointer: {
206 const llvm::Type *PtrDiffTy =
207 CGF.ConvertType(CGF.getContext().getPointerDiffType());
209 llvm::Value *NullValue = llvm::Constant::getNullValue(PtrDiffTy);
210 llvm::Value *Ptr = Builder.CreateStructGEP(DestPtr, 0, "ptr");
211 Builder.CreateStore(NullValue, Ptr, VolatileDest);
213 llvm::Value *Adj = Builder.CreateStructGEP(DestPtr, 1, "adj");
214 Builder.CreateStore(NullValue, Adj, VolatileDest);
219 case CastExpr::CK_BitCast: {
220 // This must be a member function pointer cast.
221 Visit(E->getSubExpr());
225 case CastExpr::CK_BaseToDerivedMemberPointer: {
226 QualType SrcType = E->getSubExpr()->getType();
228 llvm::Value *Src = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(SrcType),
230 CGF.EmitAggExpr(E->getSubExpr(), Src, SrcType.isVolatileQualified());
232 llvm::Value *SrcPtr = Builder.CreateStructGEP(Src, 0, "src.ptr");
233 SrcPtr = Builder.CreateLoad(SrcPtr);
235 llvm::Value *SrcAdj = Builder.CreateStructGEP(Src, 1, "src.adj");
236 SrcAdj = Builder.CreateLoad(SrcAdj);
238 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr");
239 Builder.CreateStore(SrcPtr, DstPtr, VolatileDest);
241 llvm::Value *DstAdj = Builder.CreateStructGEP(DestPtr, 1, "dst.adj");
243 // Now See if we need to update the adjustment.
244 const CXXRecordDecl *SrcDecl =
245 cast<CXXRecordDecl>(SrcType->getAs<MemberPointerType>()->
246 getClass()->getAs<RecordType>()->getDecl());
247 const CXXRecordDecl *DstDecl =
248 cast<CXXRecordDecl>(E->getType()->getAs<MemberPointerType>()->
249 getClass()->getAs<RecordType>()->getDecl());
251 llvm::Constant *Adj = CGF.CGM.GetCXXBaseClassOffset(DstDecl, SrcDecl);
253 SrcAdj = Builder.CreateAdd(SrcAdj, Adj, "adj");
255 Builder.CreateStore(SrcAdj, DstAdj, VolatileDest);
261 void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
262 if (E->getCallReturnType()->isReferenceType()) {
263 EmitAggLoadOfLValue(E);
267 RValue RV = CGF.EmitCallExpr(E);
268 EmitFinalDestCopy(E, RV);
271 void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
272 RValue RV = CGF.EmitObjCMessageExpr(E);
273 EmitFinalDestCopy(E, RV);
276 void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
277 RValue RV = CGF.EmitObjCPropertyGet(E);
278 EmitFinalDestCopy(E, RV);
281 void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr(
282 ObjCImplicitSetterGetterRefExpr *E) {
283 RValue RV = CGF.EmitObjCPropertyGet(E);
284 EmitFinalDestCopy(E, RV);
287 void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
288 CGF.EmitAnyExpr(E->getLHS(), 0, false, true);
289 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest,
290 /*IgnoreResult=*/false, IsInitializer);
293 void AggExprEmitter::VisitUnaryAddrOf(const UnaryOperator *E) {
294 // We have a member function pointer.
295 const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>();
297 assert(MPT->getPointeeType()->isFunctionProtoType() &&
298 "Unexpected member pointer type!");
300 const QualifiedDeclRefExpr *DRE = cast<QualifiedDeclRefExpr>(E->getSubExpr());
301 const CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl());
303 const llvm::Type *PtrDiffTy =
304 CGF.ConvertType(CGF.getContext().getPointerDiffType());
306 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr");
307 llvm::Value *FuncPtr;
309 if (MD->isVirtual()) {
311 CGF.CGM.getVtableInfo().getMethodVtableIndex(MD);
313 FuncPtr = llvm::ConstantInt::get(PtrDiffTy, Index + 1);
315 FuncPtr = llvm::ConstantExpr::getPtrToInt(CGF.CGM.GetAddrOfFunction(MD),
318 Builder.CreateStore(FuncPtr, DstPtr, VolatileDest);
320 llvm::Value *AdjPtr = Builder.CreateStructGEP(DestPtr, 1, "dst.adj");
322 // The adjustment will always be 0.
323 Builder.CreateStore(llvm::ConstantInt::get(PtrDiffTy, 0), AdjPtr,
327 void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
328 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
331 void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
332 if (E->getOpcode() == BinaryOperator::PtrMemD)
333 VisitPointerToDataMemberBinaryOperator(E);
335 CGF.ErrorUnsupported(E, "aggregate binary expression");
338 void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
339 const BinaryOperator *E) {
340 LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
341 EmitFinalDestCopy(E, LV);
344 void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
345 // For an assignment to work, the value on the right has
346 // to be compatible with the value on the left.
347 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
348 E->getRHS()->getType())
349 && "Invalid assignment");
350 LValue LHS = CGF.EmitLValue(E->getLHS());
352 // We have to special case property setters, otherwise we must have
353 // a simple lvalue (no aggregates inside vectors, bitfields).
354 if (LHS.isPropertyRef()) {
355 llvm::Value *AggLoc = DestPtr;
357 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
358 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
359 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
360 RValue::getAggregate(AggLoc, VolatileDest));
361 } else if (LHS.isKVCRef()) {
362 llvm::Value *AggLoc = DestPtr;
364 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
365 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
366 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
367 RValue::getAggregate(AggLoc, VolatileDest));
369 bool RequiresGCollection = false;
370 if (CGF.getContext().getLangOptions().NeXTRuntime) {
371 QualType LHSTy = E->getLHS()->getType();
372 if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAs<RecordType>())
373 RequiresGCollection = FDTTy->getDecl()->hasObjectMember();
375 // Codegen the RHS so that it stores directly into the LHS.
376 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified(),
377 false, false, RequiresGCollection);
378 EmitFinalDestCopy(E, LHS, true);
382 void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
383 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
384 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
385 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
387 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
388 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
390 CGF.PushConditionalTempDestruction();
391 CGF.EmitBlock(LHSBlock);
393 // Handle the GNU extension for missing LHS.
394 assert(E->getLHS() && "Must have LHS for aggregate value");
397 CGF.PopConditionalTempDestruction();
398 CGF.EmitBranch(ContBlock);
400 CGF.PushConditionalTempDestruction();
401 CGF.EmitBlock(RHSBlock);
404 CGF.PopConditionalTempDestruction();
405 CGF.EmitBranch(ContBlock);
407 CGF.EmitBlock(ContBlock);
410 void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
411 Visit(CE->getChosenSubExpr(CGF.getContext()));
414 void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
415 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
416 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
419 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
423 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, Qualifiers()));
426 void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
427 llvm::Value *Val = DestPtr;
430 // Create a temporary variable.
431 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
434 CGF.EmitAggExpr(E->getSubExpr(), Val, false);
436 Visit(E->getSubExpr());
438 // Don't make this a live temporary if we're emitting an initializer expr.
440 CGF.PushCXXTemporary(E->getTemporary(), Val);
444 AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
445 llvm::Value *Val = DestPtr;
448 // Create a temporary variable.
449 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
452 CGF.EmitCXXConstructExpr(Val, E);
455 void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
456 CGF.EmitCXXExprWithTemporaries(E, DestPtr, VolatileDest, IsInitializer);
459 void AggExprEmitter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
460 LValue lvalue = LValue::MakeAddr(DestPtr, Qualifiers());
461 EmitNullInitializationToLValue(lvalue, E->getType());
464 void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
465 // FIXME: Ignore result?
466 // FIXME: Are initializers affected by volatile?
467 if (isa<ImplicitValueInitExpr>(E)) {
468 EmitNullInitializationToLValue(LV, E->getType());
469 } else if (E->getType()->isComplexType()) {
470 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
471 } else if (CGF.hasAggregateLLVMType(E->getType())) {
472 CGF.EmitAnyExpr(E, LV.getAddress(), false);
474 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
478 void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
479 if (!CGF.hasAggregateLLVMType(T)) {
480 // For non-aggregates, we can store zero
481 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
482 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
484 // Otherwise, just memset the whole thing to zero. This is legal
485 // because in LLVM, all default initializers are guaranteed to have a
486 // bit pattern of all zeros.
487 // FIXME: That isn't true for member pointers!
488 // There's a potential optimization opportunity in combining
489 // memsets; that would be easy for arrays, but relatively
490 // difficult for structures with the current code.
491 CGF.EmitMemSetToZero(LV.getAddress(), T);
495 void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
497 // FIXME: Disabled while we figure out what to do about
498 // test/CodeGen/bitfield.c
500 // If we can, prefer a copy from a global; this is a lot less code for long
501 // globals, and it's easier for the current optimizers to analyze.
502 // FIXME: Should we really be doing this? Should we try to avoid cases where
503 // we emit a global with a lot of zeros? Should we try to avoid short
505 if (E->isConstantInitializer(CGF.getContext(), 0)) {
506 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
507 llvm::GlobalVariable* GV =
508 new llvm::GlobalVariable(C->getType(), true,
509 llvm::GlobalValue::InternalLinkage,
510 C, "", &CGF.CGM.getModule(), 0);
511 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
515 if (E->hadArrayRangeDesignator()) {
516 CGF.ErrorUnsupported(E, "GNU array range designator extension");
519 // Handle initialization of an array.
520 if (E->getType()->isArrayType()) {
521 const llvm::PointerType *APType =
522 cast<llvm::PointerType>(DestPtr->getType());
523 const llvm::ArrayType *AType =
524 cast<llvm::ArrayType>(APType->getElementType());
526 uint64_t NumInitElements = E->getNumInits();
528 if (E->getNumInits() > 0) {
529 QualType T1 = E->getType();
530 QualType T2 = E->getInit(0)->getType();
531 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
532 EmitAggLoadOfLValue(E->getInit(0));
537 uint64_t NumArrayElements = AType->getNumElements();
538 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
539 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
541 // FIXME: were we intentionally ignoring address spaces and GC attributes?
542 Qualifiers Quals = CGF.MakeQualifiers(ElementType);
544 for (uint64_t i = 0; i != NumArrayElements; ++i) {
545 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
546 if (i < NumInitElements)
547 EmitInitializationToLValue(E->getInit(i),
548 LValue::MakeAddr(NextVal, Quals));
550 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, Quals),
556 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
558 // Do struct initialization; this code just sets each individual member
559 // to the approprate value. This makes bitfield support automatic;
560 // the disadvantage is that the generated code is more difficult for
561 // the optimizer, especially with bitfields.
562 unsigned NumInitElements = E->getNumInits();
563 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
564 unsigned CurInitVal = 0;
566 if (E->getType()->isUnionType()) {
567 // Only initialize one field of a union. The field itself is
568 // specified by the initializer list.
569 if (!E->getInitializedFieldInUnion()) {
570 // Empty union; we have nothing to do.
573 // Make sure that it's really an empty and not a failure of
574 // semantic analysis.
575 for (RecordDecl::field_iterator Field = SD->field_begin(),
576 FieldEnd = SD->field_end();
577 Field != FieldEnd; ++Field)
578 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
584 FieldDecl *Field = E->getInitializedFieldInUnion();
585 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
587 if (NumInitElements) {
588 // Store the initializer into the field
589 EmitInitializationToLValue(E->getInit(0), FieldLoc);
591 // Default-initialize to null
592 EmitNullInitializationToLValue(FieldLoc, Field->getType());
598 // Here we iterate over the fields; this makes it simpler to both
599 // default-initialize fields and skip over unnamed fields.
600 for (RecordDecl::field_iterator Field = SD->field_begin(),
601 FieldEnd = SD->field_end();
602 Field != FieldEnd; ++Field) {
603 // We're done once we hit the flexible array member
604 if (Field->getType()->isIncompleteArrayType())
607 if (Field->isUnnamedBitfield())
611 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
612 // We never generate write-barries for initialized fields.
613 LValue::SetObjCNonGC(FieldLoc, true);
614 if (CurInitVal < NumInitElements) {
615 // Store the initializer into the field
616 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
618 // We're out of initalizers; default-initialize to null
619 EmitNullInitializationToLValue(FieldLoc, Field->getType());
624 //===----------------------------------------------------------------------===//
625 // Entry Points into this File
626 //===----------------------------------------------------------------------===//
628 /// EmitAggExpr - Emit the computation of the specified expression of aggregate
629 /// type. The result is computed into DestPtr. Note that if DestPtr is null,
630 /// the value of the aggregate expression is not needed. If VolatileDest is
631 /// true, DestPtr cannot be 0.
632 void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
633 bool VolatileDest, bool IgnoreResult,
635 bool RequiresGCollection) {
636 assert(E && hasAggregateLLVMType(E->getType()) &&
637 "Invalid aggregate expression to emit");
638 assert ((DestPtr != 0 || VolatileDest == false)
639 && "volatile aggregate can't be 0");
641 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer,
643 .Visit(const_cast<Expr*>(E));
646 void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
647 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
649 EmitMemSetToZero(DestPtr, Ty);
652 void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
653 llvm::Value *SrcPtr, QualType Ty,
655 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
657 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
658 // C99 6.5.16.1p3, which states "If the value being stored in an object is
659 // read from another object that overlaps in anyway the storage of the first
660 // object, then the overlap shall be exact and the two objects shall have
661 // qualified or unqualified versions of a compatible type."
663 // memcpy is not defined if the source and destination pointers are exactly
664 // equal, but other compilers do this optimization, and almost every memcpy
665 // implementation handles this case safely. If there is a libc that does not
666 // safely handle this, we can add a target hook.
667 const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
668 if (DestPtr->getType() != BP)
669 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
670 if (SrcPtr->getType() != BP)
671 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
673 // Get size and alignment info for this aggregate.
674 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
676 // FIXME: Handle variable sized types.
677 const llvm::Type *IntPtr =
678 llvm::IntegerType::get(VMContext, LLVMPointerWidth);
680 // FIXME: If we have a volatile struct, the optimizer can remove what might
681 // appear to be `extra' memory ops:
683 // volatile struct { int i; } a, b;
690 // we need to use a differnt call here. We use isVolatile to indicate when
691 // either the source or the destination is volatile.
692 Builder.CreateCall4(CGM.getMemCpyFn(),
694 // TypeInfo.first describes size in bits.
695 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
696 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),