Anders Carlsson | 5b95592 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGClass.cpp - Emit LLVM Code for C++ classes ---------------------===// |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with C++ code generation of classes |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Devang Patel | d67ef0e | 2010-08-11 21:04:37 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7e1dff7 | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 17 | #include "clang/AST/EvaluatedExprVisitor.h" |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecordLayout.h" |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtCXX.h" |
Devang Patel | 3ee36af | 2011-02-22 20:55:26 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/CodeGenOptions.h" |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 21 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 25 | static CharUnits |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 26 | ComputeNonVirtualBaseClassOffset(ASTContext &Context, |
| 27 | const CXXRecordDecl *DerivedClass, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 28 | CastExpr::path_const_iterator Start, |
| 29 | CastExpr::path_const_iterator End) { |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 30 | CharUnits Offset = CharUnits::Zero(); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 31 | |
| 32 | const CXXRecordDecl *RD = DerivedClass; |
| 33 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 34 | for (CastExpr::path_const_iterator I = Start; I != End; ++I) { |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 35 | const CXXBaseSpecifier *Base = *I; |
| 36 | assert(!Base->isVirtual() && "Should not see virtual bases here!"); |
| 37 | |
| 38 | // Get the layout. |
| 39 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 40 | |
| 41 | const CXXRecordDecl *BaseDecl = |
| 42 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 43 | |
| 44 | // Add the offset. |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 45 | Offset += Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 46 | |
| 47 | RD = BaseDecl; |
| 48 | } |
| 49 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 50 | return Offset; |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 51 | } |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 52 | |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 53 | llvm::Constant * |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 54 | CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 55 | CastExpr::path_const_iterator PathBegin, |
| 56 | CastExpr::path_const_iterator PathEnd) { |
| 57 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 58 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 59 | CharUnits Offset = |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 60 | ComputeNonVirtualBaseClassOffset(getContext(), ClassDecl, |
| 61 | PathBegin, PathEnd); |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 62 | if (Offset.isZero()) |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 63 | return 0; |
| 64 | |
| 65 | const llvm::Type *PtrDiffTy = |
| 66 | Types.ConvertType(getContext().getPointerDiffType()); |
| 67 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 68 | return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity()); |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 71 | /// Gets the address of a direct base class within a complete object. |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 72 | /// This should only be used for (1) non-virtual bases or (2) virtual bases |
| 73 | /// when the type is known to be complete (e.g. in complete destructors). |
| 74 | /// |
| 75 | /// The object pointed to by 'This' is assumed to be non-null. |
| 76 | llvm::Value * |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 77 | CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(llvm::Value *This, |
| 78 | const CXXRecordDecl *Derived, |
| 79 | const CXXRecordDecl *Base, |
| 80 | bool BaseIsVirtual) { |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 81 | // 'this' must be a pointer (in some address space) to Derived. |
| 82 | assert(This->getType()->isPointerTy() && |
| 83 | cast<llvm::PointerType>(This->getType())->getElementType() |
| 84 | == ConvertType(Derived)); |
| 85 | |
| 86 | // Compute the offset of the virtual base. |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 87 | CharUnits Offset; |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 88 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 89 | if (BaseIsVirtual) |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 90 | Offset = Layout.getVBaseClassOffset(Base); |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 91 | else |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 92 | Offset = Layout.getBaseClassOffset(Base); |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 93 | |
| 94 | // Shift and cast down to the base type. |
| 95 | // TODO: for complete types, this should be possible with a GEP. |
| 96 | llvm::Value *V = This; |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 97 | if (Offset.isPositive()) { |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 98 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 99 | V = Builder.CreateBitCast(V, Int8PtrTy); |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 100 | V = Builder.CreateConstInBoundsGEP1_64(V, Offset.getQuantity()); |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 101 | } |
| 102 | V = Builder.CreateBitCast(V, ConvertType(Base)->getPointerTo()); |
| 103 | |
| 104 | return V; |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 105 | } |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 106 | |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 107 | static llvm::Value * |
| 108 | ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, llvm::Value *ThisPtr, |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 109 | CharUnits NonVirtual, llvm::Value *Virtual) { |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 110 | const llvm::Type *PtrDiffTy = |
| 111 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 112 | |
| 113 | llvm::Value *NonVirtualOffset = 0; |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 114 | if (!NonVirtual.isZero()) |
| 115 | NonVirtualOffset = llvm::ConstantInt::get(PtrDiffTy, |
| 116 | NonVirtual.getQuantity()); |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 117 | |
| 118 | llvm::Value *BaseOffset; |
| 119 | if (Virtual) { |
| 120 | if (NonVirtualOffset) |
| 121 | BaseOffset = CGF.Builder.CreateAdd(Virtual, NonVirtualOffset); |
| 122 | else |
| 123 | BaseOffset = Virtual; |
| 124 | } else |
| 125 | BaseOffset = NonVirtualOffset; |
| 126 | |
| 127 | // Apply the base offset. |
| 128 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 129 | ThisPtr = CGF.Builder.CreateBitCast(ThisPtr, Int8PtrTy); |
| 130 | ThisPtr = CGF.Builder.CreateGEP(ThisPtr, BaseOffset, "add.ptr"); |
| 131 | |
| 132 | return ThisPtr; |
| 133 | } |
| 134 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 135 | llvm::Value * |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 136 | CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value, |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 137 | const CXXRecordDecl *Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 138 | CastExpr::path_const_iterator PathBegin, |
| 139 | CastExpr::path_const_iterator PathEnd, |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 140 | bool NullCheckValue) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 141 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 142 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 143 | CastExpr::path_const_iterator Start = PathBegin; |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 144 | const CXXRecordDecl *VBase = 0; |
| 145 | |
| 146 | // Get the virtual base. |
| 147 | if ((*Start)->isVirtual()) { |
| 148 | VBase = |
| 149 | cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl()); |
| 150 | ++Start; |
| 151 | } |
| 152 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 153 | CharUnits NonVirtualOffset = |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 154 | ComputeNonVirtualBaseClassOffset(getContext(), VBase ? VBase : Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 155 | Start, PathEnd); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 156 | |
| 157 | // Get the base pointer type. |
| 158 | const llvm::Type *BasePtrTy = |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 159 | ConvertType((PathEnd[-1])->getType())->getPointerTo(); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 160 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 161 | if (NonVirtualOffset.isZero() && !VBase) { |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 162 | // Just cast back. |
| 163 | return Builder.CreateBitCast(Value, BasePtrTy); |
| 164 | } |
| 165 | |
| 166 | llvm::BasicBlock *CastNull = 0; |
| 167 | llvm::BasicBlock *CastNotNull = 0; |
| 168 | llvm::BasicBlock *CastEnd = 0; |
| 169 | |
| 170 | if (NullCheckValue) { |
| 171 | CastNull = createBasicBlock("cast.null"); |
| 172 | CastNotNull = createBasicBlock("cast.notnull"); |
| 173 | CastEnd = createBasicBlock("cast.end"); |
| 174 | |
Anders Carlsson | b924124 | 2011-04-11 00:30:07 +0000 | [diff] [blame] | 175 | llvm::Value *IsNull = Builder.CreateIsNull(Value); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 176 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 177 | EmitBlock(CastNotNull); |
| 178 | } |
| 179 | |
| 180 | llvm::Value *VirtualOffset = 0; |
| 181 | |
Anders Carlsson | 336a7dc | 2011-01-29 03:18:56 +0000 | [diff] [blame] | 182 | if (VBase) { |
| 183 | if (Derived->hasAttr<FinalAttr>()) { |
| 184 | VirtualOffset = 0; |
| 185 | |
| 186 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); |
| 187 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 188 | CharUnits VBaseOffset = Layout.getVBaseClassOffset(VBase); |
| 189 | NonVirtualOffset += VBaseOffset; |
Anders Carlsson | 336a7dc | 2011-01-29 03:18:56 +0000 | [diff] [blame] | 190 | } else |
| 191 | VirtualOffset = GetVirtualBaseClassOffset(Value, Derived, VBase); |
| 192 | } |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 193 | |
| 194 | // Apply the offsets. |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 195 | Value = ApplyNonVirtualAndVirtualOffset(*this, Value, |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 196 | NonVirtualOffset, |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 197 | VirtualOffset); |
| 198 | |
| 199 | // Cast back. |
| 200 | Value = Builder.CreateBitCast(Value, BasePtrTy); |
| 201 | |
| 202 | if (NullCheckValue) { |
| 203 | Builder.CreateBr(CastEnd); |
| 204 | EmitBlock(CastNull); |
| 205 | Builder.CreateBr(CastEnd); |
| 206 | EmitBlock(CastEnd); |
| 207 | |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 208 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 209 | PHI->addIncoming(Value, CastNotNull); |
| 210 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
| 211 | CastNull); |
| 212 | Value = PHI; |
| 213 | } |
| 214 | |
| 215 | return Value; |
| 216 | } |
| 217 | |
| 218 | llvm::Value * |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 219 | CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value, |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 220 | const CXXRecordDecl *Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 221 | CastExpr::path_const_iterator PathBegin, |
| 222 | CastExpr::path_const_iterator PathEnd, |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 223 | bool NullCheckValue) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 224 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 225 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 226 | QualType DerivedTy = |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 227 | getContext().getCanonicalType(getContext().getTagDeclType(Derived)); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 228 | const llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo(); |
| 229 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 230 | llvm::Value *NonVirtualOffset = |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 231 | CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd); |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 232 | |
| 233 | if (!NonVirtualOffset) { |
| 234 | // No offset, we can just cast back. |
| 235 | return Builder.CreateBitCast(Value, DerivedPtrTy); |
| 236 | } |
| 237 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 238 | llvm::BasicBlock *CastNull = 0; |
| 239 | llvm::BasicBlock *CastNotNull = 0; |
| 240 | llvm::BasicBlock *CastEnd = 0; |
| 241 | |
| 242 | if (NullCheckValue) { |
| 243 | CastNull = createBasicBlock("cast.null"); |
| 244 | CastNotNull = createBasicBlock("cast.notnull"); |
| 245 | CastEnd = createBasicBlock("cast.end"); |
| 246 | |
Anders Carlsson | b924124 | 2011-04-11 00:30:07 +0000 | [diff] [blame] | 247 | llvm::Value *IsNull = Builder.CreateIsNull(Value); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 248 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 249 | EmitBlock(CastNotNull); |
| 250 | } |
| 251 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 252 | // Apply the offset. |
| 253 | Value = Builder.CreatePtrToInt(Value, NonVirtualOffset->getType()); |
| 254 | Value = Builder.CreateSub(Value, NonVirtualOffset); |
| 255 | Value = Builder.CreateIntToPtr(Value, DerivedPtrTy); |
| 256 | |
| 257 | // Just cast. |
| 258 | Value = Builder.CreateBitCast(Value, DerivedPtrTy); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 259 | |
| 260 | if (NullCheckValue) { |
| 261 | Builder.CreateBr(CastEnd); |
| 262 | EmitBlock(CastNull); |
| 263 | Builder.CreateBr(CastEnd); |
| 264 | EmitBlock(CastEnd); |
| 265 | |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 266 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 267 | PHI->addIncoming(Value, CastNotNull); |
| 268 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
| 269 | CastNull); |
| 270 | Value = PHI; |
| 271 | } |
| 272 | |
| 273 | return Value; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 274 | } |
Anders Carlsson | 21c9ad9 | 2010-03-30 03:27:09 +0000 | [diff] [blame] | 275 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 276 | /// GetVTTParameter - Return the VTT parameter that should be passed to a |
| 277 | /// base constructor/destructor with virtual bases. |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 278 | static llvm::Value *GetVTTParameter(CodeGenFunction &CGF, GlobalDecl GD, |
| 279 | bool ForVirtualBase) { |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 280 | if (!CodeGenVTables::needsVTTParameter(GD)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 281 | // This constructor/destructor does not need a VTT parameter. |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(CGF.CurFuncDecl)->getParent(); |
| 286 | const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 287 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 288 | llvm::Value *VTT; |
| 289 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 290 | uint64_t SubVTTIndex; |
| 291 | |
| 292 | // If the record matches the base, this is the complete ctor/dtor |
| 293 | // variant calling the base variant in a class with virtual bases. |
| 294 | if (RD == Base) { |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 295 | assert(!CodeGenVTables::needsVTTParameter(CGF.CurGD) && |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 296 | "doing no-op VTT offset in base dtor/ctor?"); |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 297 | assert(!ForVirtualBase && "Can't have same class as virtual base!"); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 298 | SubVTTIndex = 0; |
| 299 | } else { |
Anders Carlsson | c11bb21 | 2010-05-02 23:53:25 +0000 | [diff] [blame] | 300 | const ASTRecordLayout &Layout = |
| 301 | CGF.getContext().getASTRecordLayout(RD); |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 302 | CharUnits BaseOffset = ForVirtualBase ? |
| 303 | Layout.getVBaseClassOffset(Base) : |
| 304 | Layout.getBaseClassOffset(Base); |
Anders Carlsson | c11bb21 | 2010-05-02 23:53:25 +0000 | [diff] [blame] | 305 | |
| 306 | SubVTTIndex = |
| 307 | CGF.CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset)); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 308 | assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!"); |
| 309 | } |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 310 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 311 | if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 312 | // A VTT parameter was passed to the constructor, use it. |
| 313 | VTT = CGF.LoadCXXVTT(); |
| 314 | VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex); |
| 315 | } else { |
| 316 | // We're the complete constructor, so get the VTT by name. |
Anders Carlsson | 1cbce12 | 2011-01-29 19:16:51 +0000 | [diff] [blame] | 317 | VTT = CGF.CGM.getVTables().GetAddrOfVTT(RD); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 318 | VTT = CGF.Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex); |
| 319 | } |
| 320 | |
| 321 | return VTT; |
| 322 | } |
| 323 | |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 324 | namespace { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 325 | /// Call the destructor for a direct base class. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 326 | struct CallBaseDtor : EHScopeStack::Cleanup { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 327 | const CXXRecordDecl *BaseClass; |
| 328 | bool BaseIsVirtual; |
| 329 | CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual) |
| 330 | : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {} |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 331 | |
| 332 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 333 | const CXXRecordDecl *DerivedClass = |
| 334 | cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent(); |
| 335 | |
| 336 | const CXXDestructorDecl *D = BaseClass->getDestructor(); |
| 337 | llvm::Value *Addr = |
| 338 | CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThis(), |
| 339 | DerivedClass, BaseClass, |
| 340 | BaseIsVirtual); |
| 341 | CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual, Addr); |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 342 | } |
| 343 | }; |
John McCall | 7e1dff7 | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 344 | |
| 345 | /// A visitor which checks whether an initializer uses 'this' in a |
| 346 | /// way which requires the vtable to be properly set. |
| 347 | struct DynamicThisUseChecker : EvaluatedExprVisitor<DynamicThisUseChecker> { |
| 348 | typedef EvaluatedExprVisitor<DynamicThisUseChecker> super; |
| 349 | |
| 350 | bool UsesThis; |
| 351 | |
| 352 | DynamicThisUseChecker(ASTContext &C) : super(C), UsesThis(false) {} |
| 353 | |
| 354 | // Black-list all explicit and implicit references to 'this'. |
| 355 | // |
| 356 | // Do we need to worry about external references to 'this' derived |
| 357 | // from arbitrary code? If so, then anything which runs arbitrary |
| 358 | // external code might potentially access the vtable. |
| 359 | void VisitCXXThisExpr(CXXThisExpr *E) { UsesThis = true; } |
| 360 | }; |
| 361 | } |
| 362 | |
| 363 | static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) { |
| 364 | DynamicThisUseChecker Checker(C); |
| 365 | Checker.Visit(const_cast<Expr*>(Init)); |
| 366 | return Checker.UsesThis; |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 369 | static void EmitBaseInitializer(CodeGenFunction &CGF, |
| 370 | const CXXRecordDecl *ClassDecl, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 371 | CXXCtorInitializer *BaseInit, |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 372 | CXXCtorType CtorType) { |
| 373 | assert(BaseInit->isBaseInitializer() && |
| 374 | "Must have base initializer!"); |
| 375 | |
| 376 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 377 | |
| 378 | const Type *BaseType = BaseInit->getBaseClass(); |
| 379 | CXXRecordDecl *BaseClassDecl = |
| 380 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| 381 | |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 382 | bool isBaseVirtual = BaseInit->isBaseVirtual(); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 383 | |
| 384 | // The base constructor doesn't construct virtual bases. |
| 385 | if (CtorType == Ctor_Base && isBaseVirtual) |
| 386 | return; |
| 387 | |
John McCall | 7e1dff7 | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 388 | // If the initializer for the base (other than the constructor |
| 389 | // itself) accesses 'this' in any way, we need to initialize the |
| 390 | // vtables. |
| 391 | if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit())) |
| 392 | CGF.InitializeVTablePointers(ClassDecl); |
| 393 | |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 394 | // We can pretend to be a complete class because it only matters for |
| 395 | // virtual bases, and we only do virtual bases for complete ctors. |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 396 | llvm::Value *V = |
| 397 | CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl, |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 398 | BaseClassDecl, |
| 399 | isBaseVirtual); |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 400 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 401 | AggValueSlot AggSlot = AggValueSlot::forAddr(V, Qualifiers(), |
| 402 | /*Lifetime*/ true); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 403 | |
| 404 | CGF.EmitAggExpr(BaseInit->getInit(), AggSlot); |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 405 | |
Anders Carlsson | 7a17851 | 2011-02-28 00:33:03 +0000 | [diff] [blame] | 406 | if (CGF.CGM.getLangOptions().Exceptions && |
Anders Carlsson | c1cfdf8 | 2011-02-20 00:20:27 +0000 | [diff] [blame] | 407 | !BaseClassDecl->hasTrivialDestructor()) |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 408 | CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl, |
| 409 | isBaseVirtual); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 412 | static void EmitAggMemberInitializer(CodeGenFunction &CGF, |
| 413 | LValue LHS, |
| 414 | llvm::Value *ArrayIndexVar, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 415 | CXXCtorInitializer *MemberInit, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 416 | QualType T, |
| 417 | unsigned Index) { |
| 418 | if (Index == MemberInit->getNumArrayIndices()) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 419 | CodeGenFunction::RunCleanupsScope Cleanups(CGF); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 420 | |
| 421 | llvm::Value *Dest = LHS.getAddress(); |
| 422 | if (ArrayIndexVar) { |
| 423 | // If we have an array index variable, load it and use it as an offset. |
| 424 | // Then, increment the value. |
| 425 | llvm::Value *ArrayIndex = CGF.Builder.CreateLoad(ArrayIndexVar); |
| 426 | Dest = CGF.Builder.CreateInBoundsGEP(Dest, ArrayIndex, "destaddress"); |
| 427 | llvm::Value *Next = llvm::ConstantInt::get(ArrayIndex->getType(), 1); |
| 428 | Next = CGF.Builder.CreateAdd(ArrayIndex, Next, "inc"); |
| 429 | CGF.Builder.CreateStore(Next, ArrayIndexVar); |
| 430 | } |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 431 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 432 | if (!CGF.hasAggregateLLVMType(T)) { |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 433 | LValue lvalue = CGF.MakeAddrLValue(Dest, T); |
| 434 | CGF.EmitScalarInit(MemberInit->getInit(), /*decl*/ 0, lvalue, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 435 | } else if (T->isAnyComplexType()) { |
| 436 | CGF.EmitComplexExprIntoAddr(MemberInit->getInit(), Dest, |
| 437 | LHS.isVolatileQualified()); |
| 438 | } else { |
| 439 | AggValueSlot Slot = AggValueSlot::forAddr(Dest, LHS.getQuals(), |
| 440 | /*Lifetime*/ true); |
| 441 | |
| 442 | CGF.EmitAggExpr(MemberInit->getInit(), Slot); |
| 443 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 444 | |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | const ConstantArrayType *Array = CGF.getContext().getAsConstantArrayType(T); |
| 449 | assert(Array && "Array initialization without the array type?"); |
| 450 | llvm::Value *IndexVar |
| 451 | = CGF.GetAddrOfLocalVar(MemberInit->getArrayIndex(Index)); |
| 452 | assert(IndexVar && "Array index variable not loaded"); |
| 453 | |
| 454 | // Initialize this index variable to zero. |
| 455 | llvm::Value* Zero |
| 456 | = llvm::Constant::getNullValue( |
| 457 | CGF.ConvertType(CGF.getContext().getSizeType())); |
| 458 | CGF.Builder.CreateStore(Zero, IndexVar); |
| 459 | |
| 460 | // Start the loop with a block that tests the condition. |
| 461 | llvm::BasicBlock *CondBlock = CGF.createBasicBlock("for.cond"); |
| 462 | llvm::BasicBlock *AfterFor = CGF.createBasicBlock("for.end"); |
| 463 | |
| 464 | CGF.EmitBlock(CondBlock); |
| 465 | |
| 466 | llvm::BasicBlock *ForBody = CGF.createBasicBlock("for.body"); |
| 467 | // Generate: if (loop-index < number-of-elements) fall to the loop body, |
| 468 | // otherwise, go to the block after the for-loop. |
| 469 | uint64_t NumElements = Array->getSize().getZExtValue(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 470 | llvm::Value *Counter = CGF.Builder.CreateLoad(IndexVar); |
Chris Lattner | 985f739 | 2010-05-06 06:35:23 +0000 | [diff] [blame] | 471 | llvm::Value *NumElementsPtr = |
| 472 | llvm::ConstantInt::get(Counter->getType(), NumElements); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 473 | llvm::Value *IsLess = CGF.Builder.CreateICmpULT(Counter, NumElementsPtr, |
| 474 | "isless"); |
| 475 | |
| 476 | // If the condition is true, execute the body. |
| 477 | CGF.Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 478 | |
| 479 | CGF.EmitBlock(ForBody); |
| 480 | llvm::BasicBlock *ContinueBlock = CGF.createBasicBlock("for.inc"); |
| 481 | |
| 482 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 483 | CodeGenFunction::RunCleanupsScope Cleanups(CGF); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 484 | |
| 485 | // Inside the loop body recurse to emit the inner loop or, eventually, the |
| 486 | // constructor call. |
| 487 | EmitAggMemberInitializer(CGF, LHS, ArrayIndexVar, MemberInit, |
| 488 | Array->getElementType(), Index + 1); |
| 489 | } |
| 490 | |
| 491 | CGF.EmitBlock(ContinueBlock); |
| 492 | |
| 493 | // Emit the increment of the loop counter. |
| 494 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 495 | Counter = CGF.Builder.CreateLoad(IndexVar); |
| 496 | NextVal = CGF.Builder.CreateAdd(Counter, NextVal, "inc"); |
| 497 | CGF.Builder.CreateStore(NextVal, IndexVar); |
| 498 | |
| 499 | // Finally, branch back up to the condition for the next iteration. |
| 500 | CGF.EmitBranch(CondBlock); |
| 501 | |
| 502 | // Emit the fall-through block. |
| 503 | CGF.EmitBlock(AfterFor, true); |
| 504 | } |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 505 | |
| 506 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 507 | struct CallMemberDtor : EHScopeStack::Cleanup { |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 508 | FieldDecl *Field; |
| 509 | CXXDestructorDecl *Dtor; |
| 510 | |
| 511 | CallMemberDtor(FieldDecl *Field, CXXDestructorDecl *Dtor) |
| 512 | : Field(Field), Dtor(Dtor) {} |
| 513 | |
| 514 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 515 | // FIXME: Is this OK for C++0x delegating constructors? |
| 516 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 517 | LValue LHS = CGF.EmitLValueForField(ThisPtr, Field, 0); |
| 518 | |
| 519 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false, |
| 520 | LHS.getAddress()); |
| 521 | } |
| 522 | }; |
| 523 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 524 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 525 | static void EmitMemberInitializer(CodeGenFunction &CGF, |
| 526 | const CXXRecordDecl *ClassDecl, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 527 | CXXCtorInitializer *MemberInit, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 528 | const CXXConstructorDecl *Constructor, |
| 529 | FunctionArgList &Args) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 530 | assert(MemberInit->isAnyMemberInitializer() && |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 531 | "Must have member initializer!"); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 532 | assert(MemberInit->getInit() && "Must have initializer!"); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 533 | |
| 534 | // non-static data member initializers. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 535 | FieldDecl *Field = MemberInit->getAnyMember(); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 536 | QualType FieldType = CGF.getContext().getCanonicalType(Field->getType()); |
| 537 | |
| 538 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
John McCall | a9976d3 | 2010-05-21 01:18:57 +0000 | [diff] [blame] | 539 | LValue LHS; |
Anders Carlsson | 06a2970 | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 540 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 541 | // If we are initializing an anonymous union field, drill down to the field. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 542 | if (MemberInit->isIndirectMemberInitializer()) { |
| 543 | LHS = CGF.EmitLValueForAnonRecordField(ThisPtr, |
| 544 | MemberInit->getIndirectMember(), 0); |
| 545 | FieldType = MemberInit->getIndirectMember()->getAnonField()->getType(); |
John McCall | a9976d3 | 2010-05-21 01:18:57 +0000 | [diff] [blame] | 546 | } else { |
| 547 | LHS = CGF.EmitLValueForFieldInitialization(ThisPtr, Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 550 | // FIXME: If there's no initializer and the CXXCtorInitializer |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 551 | // was implicitly generated, we shouldn't be zeroing memory. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 552 | if (FieldType->isArrayType() && !MemberInit->getInit()) { |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 553 | CGF.EmitNullInitialization(LHS.getAddress(), Field->getType()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 554 | } else if (!CGF.hasAggregateLLVMType(Field->getType())) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 555 | if (LHS.isSimple()) { |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 556 | CGF.EmitExprAsInit(MemberInit->getInit(), Field, LHS, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 557 | } else { |
| 558 | RValue RHS = RValue::get(CGF.EmitScalarExpr(MemberInit->getInit())); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 559 | CGF.EmitStoreThroughLValue(RHS, LHS); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 560 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 561 | } else if (MemberInit->getInit()->getType()->isAnyComplexType()) { |
| 562 | CGF.EmitComplexExprIntoAddr(MemberInit->getInit(), LHS.getAddress(), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 563 | LHS.isVolatileQualified()); |
| 564 | } else { |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 565 | llvm::Value *ArrayIndexVar = 0; |
| 566 | const ConstantArrayType *Array |
| 567 | = CGF.getContext().getAsConstantArrayType(FieldType); |
| 568 | if (Array && Constructor->isImplicit() && |
| 569 | Constructor->isCopyConstructor()) { |
| 570 | const llvm::Type *SizeTy |
| 571 | = CGF.ConvertType(CGF.getContext().getSizeType()); |
| 572 | |
| 573 | // The LHS is a pointer to the first object we'll be constructing, as |
| 574 | // a flat array. |
| 575 | QualType BaseElementTy = CGF.getContext().getBaseElementType(Array); |
| 576 | const llvm::Type *BasePtr = CGF.ConvertType(BaseElementTy); |
| 577 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 578 | llvm::Value *BaseAddrPtr = CGF.Builder.CreateBitCast(LHS.getAddress(), |
| 579 | BasePtr); |
Daniel Dunbar | 9f553f5 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 580 | LHS = CGF.MakeAddrLValue(BaseAddrPtr, BaseElementTy); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 581 | |
| 582 | // Create an array index that will be used to walk over all of the |
| 583 | // objects we're constructing. |
| 584 | ArrayIndexVar = CGF.CreateTempAlloca(SizeTy, "object.index"); |
| 585 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 586 | CGF.Builder.CreateStore(Zero, ArrayIndexVar); |
| 587 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 588 | // If we are copying an array of PODs or classes with trivial copy |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 589 | // constructors, perform a single aggregate copy. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 590 | const CXXRecordDecl *Record = BaseElementTy->getAsCXXRecordDecl(); |
| 591 | if (BaseElementTy.isPODType(CGF.getContext()) || |
| 592 | (Record && Record->hasTrivialCopyConstructor())) { |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 593 | // Find the source pointer. We knows it's the last argument because |
| 594 | // we know we're in a copy constructor. |
| 595 | unsigned SrcArgIndex = Args.size() - 1; |
| 596 | llvm::Value *SrcPtr |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 597 | = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex])); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 598 | LValue Src = CGF.EmitLValueForFieldInitialization(SrcPtr, Field, 0); |
| 599 | |
| 600 | // Copy the aggregate. |
| 601 | CGF.EmitAggregateCopy(LHS.getAddress(), Src.getAddress(), FieldType, |
| 602 | LHS.isVolatileQualified()); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | // Emit the block variables for the array indices, if any. |
| 607 | for (unsigned I = 0, N = MemberInit->getNumArrayIndices(); I != N; ++I) |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 608 | CGF.EmitAutoVarDecl(*MemberInit->getArrayIndex(I)); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | EmitAggMemberInitializer(CGF, LHS, ArrayIndexVar, MemberInit, FieldType, 0); |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 612 | |
Anders Carlsson | 7a17851 | 2011-02-28 00:33:03 +0000 | [diff] [blame] | 613 | if (!CGF.CGM.getLangOptions().Exceptions) |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 614 | return; |
| 615 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 616 | // FIXME: If we have an array of classes w/ non-trivial destructors, |
| 617 | // we need to destroy in reverse order of construction along the exception |
| 618 | // path. |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 619 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 620 | if (!RT) |
| 621 | return; |
| 622 | |
| 623 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 624 | if (!RD->hasTrivialDestructor()) |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 625 | CGF.EHStack.pushCleanup<CallMemberDtor>(EHCleanup, Field, |
| 626 | RD->getDestructor()); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 630 | /// Checks whether the given constructor is a valid subject for the |
| 631 | /// complete-to-base constructor delegation optimization, i.e. |
| 632 | /// emitting the complete constructor as a simple call to the base |
| 633 | /// constructor. |
| 634 | static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) { |
| 635 | |
| 636 | // Currently we disable the optimization for classes with virtual |
| 637 | // bases because (1) the addresses of parameter variables need to be |
| 638 | // consistent across all initializers but (2) the delegate function |
| 639 | // call necessarily creates a second copy of the parameter variable. |
| 640 | // |
| 641 | // The limiting example (purely theoretical AFAIK): |
| 642 | // struct A { A(int &c) { c++; } }; |
| 643 | // struct B : virtual A { |
| 644 | // B(int count) : A(count) { printf("%d\n", count); } |
| 645 | // }; |
| 646 | // ...although even this example could in principle be emitted as a |
| 647 | // delegation since the address of the parameter doesn't escape. |
| 648 | if (Ctor->getParent()->getNumVBases()) { |
| 649 | // TODO: white-list trivial vbase initializers. This case wouldn't |
| 650 | // be subject to the restrictions below. |
| 651 | |
| 652 | // TODO: white-list cases where: |
| 653 | // - there are no non-reference parameters to the constructor |
| 654 | // - the initializers don't access any non-reference parameters |
| 655 | // - the initializers don't take the address of non-reference |
| 656 | // parameters |
| 657 | // - etc. |
| 658 | // If we ever add any of the above cases, remember that: |
| 659 | // - function-try-blocks will always blacklist this optimization |
| 660 | // - we need to perform the constructor prologue and cleanup in |
| 661 | // EmitConstructorBody. |
| 662 | |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | // We also disable the optimization for variadic functions because |
| 667 | // it's impossible to "re-pass" varargs. |
| 668 | if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic()) |
| 669 | return false; |
| 670 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 671 | // FIXME: Decide if we can do a delegation of a delegating constructor. |
| 672 | if (Ctor->isDelegatingConstructor()) |
| 673 | return false; |
| 674 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 675 | return true; |
| 676 | } |
| 677 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 678 | /// EmitConstructorBody - Emits the body of the current constructor. |
| 679 | void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) { |
| 680 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl()); |
| 681 | CXXCtorType CtorType = CurGD.getCtorType(); |
| 682 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 683 | // Before we go any further, try the complete->base constructor |
| 684 | // delegation optimization. |
| 685 | if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor)) { |
Devang Patel | d67ef0e | 2010-08-11 21:04:37 +0000 | [diff] [blame] | 686 | if (CGDebugInfo *DI = getDebugInfo()) |
| 687 | DI->EmitStopPoint(Builder); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 688 | EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args); |
| 689 | return; |
| 690 | } |
| 691 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 692 | Stmt *Body = Ctor->getBody(); |
| 693 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 694 | // Enter the function-try-block before the constructor prologue if |
| 695 | // applicable. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 696 | bool IsTryBody = (Body && isa<CXXTryStmt>(Body)); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 697 | if (IsTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 698 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 699 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 700 | EHScopeStack::stable_iterator CleanupDepth = EHStack.stable_begin(); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 701 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 702 | // Emit the constructor prologue, i.e. the base and member |
| 703 | // initializers. |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 704 | EmitCtorPrologue(Ctor, CtorType, Args); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 705 | |
| 706 | // Emit the body of the statement. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 707 | if (IsTryBody) |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 708 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 709 | else if (Body) |
| 710 | EmitStmt(Body); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 711 | |
| 712 | // Emit any cleanup blocks associated with the member or base |
| 713 | // initializers, which includes (along the exceptional path) the |
| 714 | // destructors for those members and bases that were fully |
| 715 | // constructed. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 716 | PopCleanupBlocks(CleanupDepth); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 717 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 718 | if (IsTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 719 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 722 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 723 | /// base classes and non-static data members belonging to this constructor. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 724 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 725 | CXXCtorType CtorType, |
| 726 | FunctionArgList &Args) { |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 727 | if (CD->isDelegatingConstructor()) |
| 728 | return EmitDelegatingCXXConstructorCall(CD, Args); |
| 729 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 730 | const CXXRecordDecl *ClassDecl = CD->getParent(); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 731 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 732 | llvm::SmallVector<CXXCtorInitializer *, 8> MemberInitializers; |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 733 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 734 | for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
| 735 | E = CD->init_end(); |
| 736 | B != E; ++B) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 737 | CXXCtorInitializer *Member = (*B); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 738 | |
Sean Hunt | d49bd55 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 739 | if (Member->isBaseInitializer()) { |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 740 | EmitBaseInitializer(*this, ClassDecl, Member, CtorType); |
Sean Hunt | d49bd55 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 741 | } else { |
| 742 | assert(Member->isAnyMemberInitializer() && |
| 743 | "Delegating initializer on non-delegating constructor"); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 744 | MemberInitializers.push_back(Member); |
Sean Hunt | d49bd55 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 745 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 748 | InitializeVTablePointers(ClassDecl); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 749 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 750 | for (unsigned I = 0, E = MemberInitializers.size(); I != E; ++I) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 751 | EmitMemberInitializer(*this, ClassDecl, MemberInitializers[I], CD, Args); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 754 | static bool |
| 755 | FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field); |
| 756 | |
| 757 | static bool |
| 758 | HasTrivialDestructorBody(ASTContext &Context, |
| 759 | const CXXRecordDecl *BaseClassDecl, |
| 760 | const CXXRecordDecl *MostDerivedClassDecl) |
| 761 | { |
| 762 | // If the destructor is trivial we don't have to check anything else. |
| 763 | if (BaseClassDecl->hasTrivialDestructor()) |
| 764 | return true; |
| 765 | |
| 766 | if (!BaseClassDecl->getDestructor()->hasTrivialBody()) |
| 767 | return false; |
| 768 | |
| 769 | // Check fields. |
| 770 | for (CXXRecordDecl::field_iterator I = BaseClassDecl->field_begin(), |
| 771 | E = BaseClassDecl->field_end(); I != E; ++I) { |
| 772 | const FieldDecl *Field = *I; |
| 773 | |
| 774 | if (!FieldHasTrivialDestructorBody(Context, Field)) |
| 775 | return false; |
| 776 | } |
| 777 | |
| 778 | // Check non-virtual bases. |
| 779 | for (CXXRecordDecl::base_class_const_iterator I = |
| 780 | BaseClassDecl->bases_begin(), E = BaseClassDecl->bases_end(); |
| 781 | I != E; ++I) { |
| 782 | if (I->isVirtual()) |
| 783 | continue; |
| 784 | |
| 785 | const CXXRecordDecl *NonVirtualBase = |
| 786 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
| 787 | if (!HasTrivialDestructorBody(Context, NonVirtualBase, |
| 788 | MostDerivedClassDecl)) |
| 789 | return false; |
| 790 | } |
| 791 | |
| 792 | if (BaseClassDecl == MostDerivedClassDecl) { |
| 793 | // Check virtual bases. |
| 794 | for (CXXRecordDecl::base_class_const_iterator I = |
| 795 | BaseClassDecl->vbases_begin(), E = BaseClassDecl->vbases_end(); |
| 796 | I != E; ++I) { |
| 797 | const CXXRecordDecl *VirtualBase = |
| 798 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
| 799 | if (!HasTrivialDestructorBody(Context, VirtualBase, |
| 800 | MostDerivedClassDecl)) |
| 801 | return false; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | return true; |
| 806 | } |
| 807 | |
| 808 | static bool |
| 809 | FieldHasTrivialDestructorBody(ASTContext &Context, |
| 810 | const FieldDecl *Field) |
| 811 | { |
| 812 | QualType FieldBaseElementType = Context.getBaseElementType(Field->getType()); |
| 813 | |
| 814 | const RecordType *RT = FieldBaseElementType->getAs<RecordType>(); |
| 815 | if (!RT) |
| 816 | return true; |
| 817 | |
| 818 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 819 | return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl); |
| 820 | } |
| 821 | |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 822 | /// CanSkipVTablePointerInitialization - Check whether we need to initialize |
| 823 | /// any vtable pointers before calling this destructor. |
| 824 | static bool CanSkipVTablePointerInitialization(ASTContext &Context, |
Anders Carlsson | e3d6cf2 | 2011-05-16 04:08:36 +0000 | [diff] [blame] | 825 | const CXXDestructorDecl *Dtor) { |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 826 | if (!Dtor->hasTrivialBody()) |
| 827 | return false; |
| 828 | |
| 829 | // Check the fields. |
| 830 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
| 831 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 832 | E = ClassDecl->field_end(); I != E; ++I) { |
| 833 | const FieldDecl *Field = *I; |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 834 | |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 835 | if (!FieldHasTrivialDestructorBody(Context, Field)) |
| 836 | return false; |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | return true; |
| 840 | } |
| 841 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 842 | /// EmitDestructorBody - Emits the body of the current destructor. |
| 843 | void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) { |
| 844 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl()); |
| 845 | CXXDtorType DtorType = CurGD.getDtorType(); |
| 846 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 847 | // The call to operator delete in a deleting destructor happens |
| 848 | // outside of the function-try-block, which means it's always |
| 849 | // possible to delegate the destructor body to the complete |
| 850 | // destructor. Do so. |
| 851 | if (DtorType == Dtor_Deleting) { |
| 852 | EnterDtorCleanups(Dtor, Dtor_Deleting); |
| 853 | EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false, |
| 854 | LoadCXXThis()); |
| 855 | PopCleanupBlock(); |
| 856 | return; |
| 857 | } |
| 858 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 859 | Stmt *Body = Dtor->getBody(); |
| 860 | |
| 861 | // If the body is a function-try-block, enter the try before |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 862 | // anything else. |
| 863 | bool isTryBody = (Body && isa<CXXTryStmt>(Body)); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 864 | if (isTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 865 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 866 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 867 | // Enter the epilogue cleanups. |
| 868 | RunCleanupsScope DtorEpilogue(*this); |
| 869 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 870 | // If this is the complete variant, just invoke the base variant; |
| 871 | // the epilogue will destruct the virtual bases. But we can't do |
| 872 | // this optimization if the body is a function-try-block, because |
| 873 | // we'd introduce *two* handler blocks. |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 874 | switch (DtorType) { |
| 875 | case Dtor_Deleting: llvm_unreachable("already handled deleting case"); |
| 876 | |
| 877 | case Dtor_Complete: |
| 878 | // Enter the cleanup scopes for virtual bases. |
| 879 | EnterDtorCleanups(Dtor, Dtor_Complete); |
| 880 | |
| 881 | if (!isTryBody) { |
| 882 | EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false, |
| 883 | LoadCXXThis()); |
| 884 | break; |
| 885 | } |
| 886 | // Fallthrough: act like we're in the base variant. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 887 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 888 | case Dtor_Base: |
| 889 | // Enter the cleanup scopes for fields and non-virtual bases. |
| 890 | EnterDtorCleanups(Dtor, Dtor_Base); |
| 891 | |
| 892 | // Initialize the vtable pointers before entering the body. |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 893 | if (!CanSkipVTablePointerInitialization(getContext(), Dtor)) |
| 894 | InitializeVTablePointers(Dtor->getParent()); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 895 | |
| 896 | if (isTryBody) |
| 897 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 898 | else if (Body) |
| 899 | EmitStmt(Body); |
| 900 | else { |
| 901 | assert(Dtor->isImplicit() && "bodyless dtor not implicit"); |
| 902 | // nothing to do besides what's in the epilogue |
| 903 | } |
Fariborz Jahanian | 5abec14 | 2011-02-02 23:12:46 +0000 | [diff] [blame] | 904 | // -fapple-kext must inline any call to this dtor into |
| 905 | // the caller's body. |
| 906 | if (getContext().getLangOptions().AppleKext) |
| 907 | CurFn->addFnAttr(llvm::Attribute::AlwaysInline); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 908 | break; |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 909 | } |
| 910 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 911 | // Jump out through the epilogue cleanups. |
| 912 | DtorEpilogue.ForceCleanup(); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 913 | |
| 914 | // Exit the try if applicable. |
| 915 | if (isTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 916 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 917 | } |
| 918 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 919 | namespace { |
| 920 | /// Call the operator delete associated with the current destructor. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 921 | struct CallDtorDelete : EHScopeStack::Cleanup { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 922 | CallDtorDelete() {} |
| 923 | |
| 924 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 925 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl); |
| 926 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
| 927 | CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(), |
| 928 | CGF.getContext().getTagDeclType(ClassDecl)); |
| 929 | } |
| 930 | }; |
| 931 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame^] | 932 | class DestroyField : public EHScopeStack::Cleanup { |
| 933 | const FieldDecl *field; |
| 934 | CodeGenFunction::Destroyer &destroyer; |
| 935 | bool useEHCleanupForArray; |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 936 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame^] | 937 | public: |
| 938 | DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer, |
| 939 | bool useEHCleanupForArray) |
| 940 | : field(field), destroyer(*destroyer), |
| 941 | useEHCleanupForArray(useEHCleanupForArray) {} |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 942 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame^] | 943 | void Emit(CodeGenFunction &CGF, bool isForEH) { |
| 944 | // Find the address of the field. |
| 945 | llvm::Value *thisValue = CGF.LoadCXXThis(); |
| 946 | LValue LV = CGF.EmitLValueForField(thisValue, field, /*CVRQualifiers=*/0); |
| 947 | assert(LV.isSimple()); |
| 948 | |
| 949 | CGF.emitDestroy(LV.getAddress(), field->getType(), destroyer, |
| 950 | !isForEH && useEHCleanupForArray); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 951 | } |
| 952 | }; |
| 953 | } |
| 954 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 955 | /// EmitDtorEpilogue - Emit all code that comes at the end of class's |
| 956 | /// destructor. This is to call destructors on members and base classes |
| 957 | /// in reverse order of their construction. |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 958 | void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD, |
| 959 | CXXDtorType DtorType) { |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 960 | assert(!DD->isTrivial() && |
| 961 | "Should not emit dtor epilogue for trivial dtor!"); |
| 962 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 963 | // The deleting-destructor phase just needs to call the appropriate |
| 964 | // operator delete that Sema picked up. |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 965 | if (DtorType == Dtor_Deleting) { |
| 966 | assert(DD->getOperatorDelete() && |
| 967 | "operator delete missing - EmitDtorEpilogue"); |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 968 | EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 969 | return; |
| 970 | } |
| 971 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 972 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
| 973 | |
| 974 | // The complete-destructor phase just destructs all the virtual bases. |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 975 | if (DtorType == Dtor_Complete) { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 976 | |
| 977 | // We push them in the forward order so that they'll be popped in |
| 978 | // the reverse order. |
| 979 | for (CXXRecordDecl::base_class_const_iterator I = |
| 980 | ClassDecl->vbases_begin(), E = ClassDecl->vbases_end(); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 981 | I != E; ++I) { |
| 982 | const CXXBaseSpecifier &Base = *I; |
| 983 | CXXRecordDecl *BaseClassDecl |
| 984 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 985 | |
| 986 | // Ignore trivial destructors. |
| 987 | if (BaseClassDecl->hasTrivialDestructor()) |
| 988 | continue; |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 989 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 990 | EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, |
| 991 | BaseClassDecl, |
| 992 | /*BaseIsVirtual*/ true); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 993 | } |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 994 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 995 | return; |
| 996 | } |
| 997 | |
| 998 | assert(DtorType == Dtor_Base); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 999 | |
| 1000 | // Destroy non-virtual bases. |
| 1001 | for (CXXRecordDecl::base_class_const_iterator I = |
| 1002 | ClassDecl->bases_begin(), E = ClassDecl->bases_end(); I != E; ++I) { |
| 1003 | const CXXBaseSpecifier &Base = *I; |
| 1004 | |
| 1005 | // Ignore virtual bases. |
| 1006 | if (Base.isVirtual()) |
| 1007 | continue; |
| 1008 | |
| 1009 | CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl(); |
| 1010 | |
| 1011 | // Ignore trivial destructors. |
| 1012 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1013 | continue; |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1014 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1015 | EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, |
| 1016 | BaseClassDecl, |
| 1017 | /*BaseIsVirtual*/ false); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | // Destroy direct fields. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1021 | llvm::SmallVector<const FieldDecl *, 16> FieldDecls; |
| 1022 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 1023 | E = ClassDecl->field_end(); I != E; ++I) { |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame^] | 1024 | const FieldDecl *field = *I; |
| 1025 | QualType type = field->getType(); |
| 1026 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
| 1027 | if (!dtorKind) continue; |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1028 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame^] | 1029 | CleanupKind cleanupKind = getCleanupKind(dtorKind); |
| 1030 | EHStack.pushCleanup<DestroyField>(cleanupKind, field, |
| 1031 | getDestroyer(dtorKind), |
| 1032 | cleanupKind & EHCleanup); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1033 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1036 | /// EmitCXXAggrConstructorCall - This routine essentially creates a (nested) |
| 1037 | /// for-loop to call the default constructor on individual members of the |
| 1038 | /// array. |
| 1039 | /// 'D' is the default constructor for elements of the array, 'ArrayTy' is the |
| 1040 | /// array type and 'ArrayPtr' points to the beginning fo the array. |
| 1041 | /// It is assumed that all relevant checks have been made by the caller. |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1042 | /// |
| 1043 | /// \param ZeroInitialization True if each element should be zero-initialized |
| 1044 | /// before it is constructed. |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1045 | void |
| 1046 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1047 | const ConstantArrayType *ArrayTy, |
| 1048 | llvm::Value *ArrayPtr, |
| 1049 | CallExpr::const_arg_iterator ArgBeg, |
| 1050 | CallExpr::const_arg_iterator ArgEnd, |
| 1051 | bool ZeroInitialization) { |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1052 | |
| 1053 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 1054 | llvm::Value * NumElements = |
| 1055 | llvm::ConstantInt::get(SizeTy, |
| 1056 | getContext().getConstantArrayElementCount(ArrayTy)); |
| 1057 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1058 | EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr, ArgBeg, ArgEnd, |
| 1059 | ZeroInitialization); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | void |
| 1063 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 1064 | llvm::Value *NumElements, |
| 1065 | llvm::Value *ArrayPtr, |
| 1066 | CallExpr::const_arg_iterator ArgBeg, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1067 | CallExpr::const_arg_iterator ArgEnd, |
| 1068 | bool ZeroInitialization) { |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1069 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 1070 | |
| 1071 | // Create a temporary for the loop index and initialize it with 0. |
| 1072 | llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); |
| 1073 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 1074 | Builder.CreateStore(Zero, IndexPtr); |
| 1075 | |
| 1076 | // Start the loop with a block that tests the condition. |
| 1077 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1078 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1079 | |
| 1080 | EmitBlock(CondBlock); |
| 1081 | |
| 1082 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1083 | |
| 1084 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 1085 | // otherwise, go to the block after the for-loop. |
| 1086 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1087 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); |
| 1088 | // If the condition is true, execute the body. |
| 1089 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 1090 | |
| 1091 | EmitBlock(ForBody); |
| 1092 | |
| 1093 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1094 | // Inside the loop body, emit the constructor call on the array element. |
| 1095 | Counter = Builder.CreateLoad(IndexPtr); |
| 1096 | llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter, |
| 1097 | "arrayidx"); |
| 1098 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1099 | // Zero initialize the storage, if requested. |
| 1100 | if (ZeroInitialization) |
| 1101 | EmitNullInitialization(Address, |
| 1102 | getContext().getTypeDeclType(D->getParent())); |
| 1103 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1104 | // C++ [class.temporary]p4: |
| 1105 | // There are two contexts in which temporaries are destroyed at a different |
| 1106 | // point than the end of the full-expression. The first context is when a |
| 1107 | // default constructor is called to initialize an element of an array. |
| 1108 | // If the constructor has one or more default arguments, the destruction of |
| 1109 | // every temporary created in a default argument expression is sequenced |
| 1110 | // before the construction of the next array element, if any. |
| 1111 | |
| 1112 | // Keep track of the current number of live temporaries. |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1113 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1114 | RunCleanupsScope Scope(*this); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1115 | |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 1116 | EmitCXXConstructorCall(D, Ctor_Complete, /*ForVirtualBase=*/false, Address, |
Anders Carlsson | 24eb78e | 2010-05-02 23:01:10 +0000 | [diff] [blame] | 1117 | ArgBeg, ArgEnd); |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1118 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1119 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1120 | EmitBlock(ContinueBlock); |
| 1121 | |
| 1122 | // Emit the increment of the loop counter. |
| 1123 | llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); |
| 1124 | Counter = Builder.CreateLoad(IndexPtr); |
| 1125 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 1126 | Builder.CreateStore(NextVal, IndexPtr); |
| 1127 | |
| 1128 | // Finally, branch back up to the condition for the next iteration. |
| 1129 | EmitBranch(CondBlock); |
| 1130 | |
| 1131 | // Emit the fall-through block. |
| 1132 | EmitBlock(AfterFor, true); |
| 1133 | } |
| 1134 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1135 | void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF, |
| 1136 | llvm::Value *addr, |
| 1137 | QualType type) { |
| 1138 | const RecordType *rtype = type->castAs<RecordType>(); |
| 1139 | const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl()); |
| 1140 | const CXXDestructorDecl *dtor = record->getDestructor(); |
| 1141 | assert(!dtor->isTrivial()); |
| 1142 | CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false, |
| 1143 | addr); |
| 1144 | } |
| 1145 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1146 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 1147 | /// elements in reverse order of construction. |
| 1148 | void |
| 1149 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1150 | const ArrayType *Array, |
| 1151 | llvm::Value *This) { |
| 1152 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 1153 | assert(CA && "Do we support VLA for destruction ?"); |
| 1154 | uint64_t ElementCount = getContext().getConstantArrayElementCount(CA); |
| 1155 | |
| 1156 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 1157 | llvm::Value* ElementCountPtr = llvm::ConstantInt::get(SizeLTy, ElementCount); |
| 1158 | EmitCXXAggrDestructorCall(D, ElementCountPtr, This); |
| 1159 | } |
| 1160 | |
| 1161 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 1162 | /// elements in reverse order of construction. |
| 1163 | void |
| 1164 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1165 | llvm::Value *UpperCount, |
| 1166 | llvm::Value *This) { |
| 1167 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 1168 | llvm::Value *One = llvm::ConstantInt::get(SizeLTy, 1); |
| 1169 | |
| 1170 | // Create a temporary for the loop index and initialize it with count of |
| 1171 | // array elements. |
| 1172 | llvm::Value *IndexPtr = CreateTempAlloca(SizeLTy, "loop.index"); |
| 1173 | |
| 1174 | // Store the number of elements in the index pointer. |
| 1175 | Builder.CreateStore(UpperCount, IndexPtr); |
| 1176 | |
| 1177 | // Start the loop with a block that tests the condition. |
| 1178 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1179 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1180 | |
| 1181 | EmitBlock(CondBlock); |
| 1182 | |
| 1183 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1184 | |
| 1185 | // Generate: if (loop-index != 0 fall to the loop body, |
| 1186 | // otherwise, go to the block after the for-loop. |
| 1187 | llvm::Value* zeroConstant = |
| 1188 | llvm::Constant::getNullValue(SizeLTy); |
| 1189 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1190 | llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant, |
| 1191 | "isne"); |
| 1192 | // If the condition is true, execute the body. |
| 1193 | Builder.CreateCondBr(IsNE, ForBody, AfterFor); |
| 1194 | |
| 1195 | EmitBlock(ForBody); |
| 1196 | |
| 1197 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1198 | // Inside the loop body, emit the constructor call on the array element. |
| 1199 | Counter = Builder.CreateLoad(IndexPtr); |
| 1200 | Counter = Builder.CreateSub(Counter, One); |
| 1201 | llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 1202 | EmitCXXDestructorCall(D, Dtor_Complete, /*ForVirtualBase=*/false, Address); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1203 | |
| 1204 | EmitBlock(ContinueBlock); |
| 1205 | |
| 1206 | // Emit the decrement of the loop counter. |
| 1207 | Counter = Builder.CreateLoad(IndexPtr); |
| 1208 | Counter = Builder.CreateSub(Counter, One, "dec"); |
| 1209 | Builder.CreateStore(Counter, IndexPtr); |
| 1210 | |
| 1211 | // Finally, branch back up to the condition for the next iteration. |
| 1212 | EmitBranch(CondBlock); |
| 1213 | |
| 1214 | // Emit the fall-through block. |
| 1215 | EmitBlock(AfterFor, true); |
| 1216 | } |
| 1217 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1218 | void |
| 1219 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 1220 | CXXCtorType Type, bool ForVirtualBase, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1221 | llvm::Value *This, |
| 1222 | CallExpr::const_arg_iterator ArgBeg, |
| 1223 | CallExpr::const_arg_iterator ArgEnd) { |
Devang Patel | 3ee36af | 2011-02-22 20:55:26 +0000 | [diff] [blame] | 1224 | |
| 1225 | CGDebugInfo *DI = getDebugInfo(); |
| 1226 | if (DI && CGM.getCodeGenOpts().LimitDebugInfo) { |
| 1227 | // If debug info for this class has been emitted then this is the right time |
| 1228 | // to do so. |
| 1229 | const CXXRecordDecl *Parent = D->getParent(); |
| 1230 | DI->getOrCreateRecordType(CGM.getContext().getTypeDeclType(Parent), |
| 1231 | Parent->getLocation()); |
| 1232 | } |
| 1233 | |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1234 | if (D->isTrivial()) { |
| 1235 | if (ArgBeg == ArgEnd) { |
| 1236 | // Trivial default constructor, no codegen required. |
| 1237 | assert(D->isDefaultConstructor() && |
| 1238 | "trivial 0-arg ctor not a default ctor"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1239 | return; |
| 1240 | } |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1241 | |
| 1242 | assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor"); |
| 1243 | assert(D->isCopyConstructor() && "trivial 1-arg ctor not a copy ctor"); |
| 1244 | |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1245 | const Expr *E = (*ArgBeg); |
| 1246 | QualType Ty = E->getType(); |
| 1247 | llvm::Value *Src = EmitLValue(E).getAddress(); |
| 1248 | EmitAggregateCopy(This, Src, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1249 | return; |
| 1250 | } |
| 1251 | |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 1252 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(D, Type), ForVirtualBase); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1253 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 1254 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1255 | EmitCXXMemberCall(D, Callee, ReturnValueSlot(), This, VTT, ArgBeg, ArgEnd); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1258 | void |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1259 | CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, |
| 1260 | llvm::Value *This, llvm::Value *Src, |
| 1261 | CallExpr::const_arg_iterator ArgBeg, |
| 1262 | CallExpr::const_arg_iterator ArgEnd) { |
| 1263 | if (D->isTrivial()) { |
| 1264 | assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor"); |
| 1265 | assert(D->isCopyConstructor() && "trivial 1-arg ctor not a copy ctor"); |
| 1266 | EmitAggregateCopy(This, Src, (*ArgBeg)->getType()); |
| 1267 | return; |
| 1268 | } |
| 1269 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, |
| 1270 | clang::Ctor_Complete); |
| 1271 | assert(D->isInstance() && |
| 1272 | "Trying to emit a member call expr on a static method!"); |
| 1273 | |
| 1274 | const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>(); |
| 1275 | |
| 1276 | CallArgList Args; |
| 1277 | |
| 1278 | // Push the this ptr. |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1279 | Args.add(RValue::get(This), D->getThisType(getContext())); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1280 | |
| 1281 | |
| 1282 | // Push the src ptr. |
| 1283 | QualType QT = *(FPT->arg_type_begin()); |
| 1284 | const llvm::Type *t = CGM.getTypes().ConvertType(QT); |
| 1285 | Src = Builder.CreateBitCast(Src, t); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1286 | Args.add(RValue::get(Src), QT); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1287 | |
| 1288 | // Skip over first argument (Src). |
| 1289 | ++ArgBeg; |
| 1290 | CallExpr::const_arg_iterator Arg = ArgBeg; |
| 1291 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin()+1, |
| 1292 | E = FPT->arg_type_end(); I != E; ++I, ++Arg) { |
| 1293 | assert(Arg != ArgEnd && "Running over edge of argument list!"); |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1294 | EmitCallArg(Args, *Arg, *I); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1295 | } |
| 1296 | // Either we've emitted all the call args, or we have a call to a |
| 1297 | // variadic function. |
| 1298 | assert((Arg == ArgEnd || FPT->isVariadic()) && |
| 1299 | "Extra arguments in non-variadic function!"); |
| 1300 | // If we still have any arguments, emit them using the type of the argument. |
| 1301 | for (; Arg != ArgEnd; ++Arg) { |
| 1302 | QualType ArgType = Arg->getType(); |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1303 | EmitCallArg(Args, *Arg, ArgType); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | QualType ResultType = FPT->getResultType(); |
| 1307 | EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args, |
| 1308 | FPT->getExtInfo()), |
| 1309 | Callee, ReturnValueSlot(), Args, D); |
| 1310 | } |
| 1311 | |
| 1312 | void |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1313 | CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1314 | CXXCtorType CtorType, |
| 1315 | const FunctionArgList &Args) { |
| 1316 | CallArgList DelegateArgs; |
| 1317 | |
| 1318 | FunctionArgList::const_iterator I = Args.begin(), E = Args.end(); |
| 1319 | assert(I != E && "no parameters to constructor"); |
| 1320 | |
| 1321 | // this |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1322 | DelegateArgs.add(RValue::get(LoadCXXThis()), (*I)->getType()); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1323 | ++I; |
| 1324 | |
| 1325 | // vtt |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 1326 | if (llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(Ctor, CtorType), |
| 1327 | /*ForVirtualBase=*/false)) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1328 | QualType VoidPP = getContext().getPointerType(getContext().VoidPtrTy); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1329 | DelegateArgs.add(RValue::get(VTT), VoidPP); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1330 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 1331 | if (CodeGenVTables::needsVTTParameter(CurGD)) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1332 | assert(I != E && "cannot skip vtt parameter, already done with args"); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1333 | assert((*I)->getType() == VoidPP && "skipping parameter not of vtt type"); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1334 | ++I; |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | // Explicit arguments. |
| 1339 | for (; I != E; ++I) { |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1340 | const VarDecl *param = *I; |
| 1341 | EmitDelegateCallArg(DelegateArgs, param); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | EmitCall(CGM.getTypes().getFunctionInfo(Ctor, CtorType), |
| 1345 | CGM.GetAddrOfCXXConstructor(Ctor, CtorType), |
| 1346 | ReturnValueSlot(), DelegateArgs, Ctor); |
| 1347 | } |
| 1348 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 1349 | namespace { |
| 1350 | struct CallDelegatingCtorDtor : EHScopeStack::Cleanup { |
| 1351 | const CXXDestructorDecl *Dtor; |
| 1352 | llvm::Value *Addr; |
| 1353 | CXXDtorType Type; |
| 1354 | |
| 1355 | CallDelegatingCtorDtor(const CXXDestructorDecl *D, llvm::Value *Addr, |
| 1356 | CXXDtorType Type) |
| 1357 | : Dtor(D), Addr(Addr), Type(Type) {} |
| 1358 | |
| 1359 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 1360 | CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false, |
| 1361 | Addr); |
| 1362 | } |
| 1363 | }; |
| 1364 | } |
| 1365 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1366 | void |
| 1367 | CodeGenFunction::EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1368 | const FunctionArgList &Args) { |
| 1369 | assert(Ctor->isDelegatingConstructor()); |
| 1370 | |
| 1371 | llvm::Value *ThisPtr = LoadCXXThis(); |
| 1372 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1373 | AggValueSlot AggSlot = |
| 1374 | AggValueSlot::forAddr(ThisPtr, Qualifiers(), /*Lifetime*/ true); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1375 | |
| 1376 | EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1377 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 1378 | const CXXRecordDecl *ClassDecl = Ctor->getParent(); |
| 1379 | if (CGM.getLangOptions().Exceptions && !ClassDecl->hasTrivialDestructor()) { |
| 1380 | CXXDtorType Type = |
| 1381 | CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base; |
| 1382 | |
| 1383 | EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup, |
| 1384 | ClassDecl->getDestructor(), |
| 1385 | ThisPtr, Type); |
| 1386 | } |
| 1387 | } |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1388 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1389 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, |
| 1390 | CXXDtorType Type, |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 1391 | bool ForVirtualBase, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1392 | llvm::Value *This) { |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 1393 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(DD, Type), |
| 1394 | ForVirtualBase); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 1395 | llvm::Value *Callee = 0; |
| 1396 | if (getContext().getLangOptions().AppleKext) |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 1397 | Callee = BuildAppleKextVirtualDestructorCall(DD, Type, |
| 1398 | DD->getParent()); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 1399 | |
| 1400 | if (!Callee) |
| 1401 | Callee = CGM.GetAddrOfCXXDestructor(DD, Type); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1402 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1403 | EmitCXXMemberCall(DD, Callee, ReturnValueSlot(), This, VTT, 0, 0); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
John McCall | 291ae94 | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 1406 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1407 | struct CallLocalDtor : EHScopeStack::Cleanup { |
John McCall | 291ae94 | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 1408 | const CXXDestructorDecl *Dtor; |
| 1409 | llvm::Value *Addr; |
| 1410 | |
| 1411 | CallLocalDtor(const CXXDestructorDecl *D, llvm::Value *Addr) |
| 1412 | : Dtor(D), Addr(Addr) {} |
| 1413 | |
| 1414 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 1415 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
| 1416 | /*ForVirtualBase=*/false, Addr); |
| 1417 | } |
| 1418 | }; |
| 1419 | } |
| 1420 | |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 1421 | void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D, |
| 1422 | llvm::Value *Addr) { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1423 | EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr); |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1426 | void CodeGenFunction::PushDestructorCleanup(QualType T, llvm::Value *Addr) { |
| 1427 | CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl(); |
| 1428 | if (!ClassDecl) return; |
| 1429 | if (ClassDecl->hasTrivialDestructor()) return; |
| 1430 | |
| 1431 | const CXXDestructorDecl *D = ClassDecl->getDestructor(); |
John McCall | 642a75f | 2011-04-28 02:15:35 +0000 | [diff] [blame] | 1432 | assert(D && D->isUsed() && "destructor not marked as used!"); |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 1433 | PushDestructorCleanup(D, Addr); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1436 | llvm::Value * |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 1437 | CodeGenFunction::GetVirtualBaseClassOffset(llvm::Value *This, |
| 1438 | const CXXRecordDecl *ClassDecl, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1439 | const CXXRecordDecl *BaseClassDecl) { |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 1440 | llvm::Value *VTablePtr = GetVTablePtr(This, Int8PtrTy); |
Ken Dyck | 14c65ca | 2011-04-07 12:37:09 +0000 | [diff] [blame] | 1441 | CharUnits VBaseOffsetOffset = |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 1442 | CGM.getVTables().getVirtualBaseOffsetOffset(ClassDecl, BaseClassDecl); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1443 | |
| 1444 | llvm::Value *VBaseOffsetPtr = |
Ken Dyck | 14c65ca | 2011-04-07 12:37:09 +0000 | [diff] [blame] | 1445 | Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(), |
| 1446 | "vbase.offset.ptr"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1447 | const llvm::Type *PtrDiffTy = |
| 1448 | ConvertType(getContext().getPointerDiffType()); |
| 1449 | |
| 1450 | VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr, |
| 1451 | PtrDiffTy->getPointerTo()); |
| 1452 | |
| 1453 | llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); |
| 1454 | |
| 1455 | return VBaseOffset; |
| 1456 | } |
| 1457 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1458 | void |
| 1459 | CodeGenFunction::InitializeVTablePointer(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1460 | const CXXRecordDecl *NearestVBase, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1461 | CharUnits OffsetFromNearestVBase, |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1462 | llvm::Constant *VTable, |
| 1463 | const CXXRecordDecl *VTableClass) { |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1464 | const CXXRecordDecl *RD = Base.getBase(); |
| 1465 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1466 | // Compute the address point. |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1467 | llvm::Value *VTableAddressPoint; |
Anders Carlsson | 851853d | 2010-03-29 02:38:51 +0000 | [diff] [blame] | 1468 | |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1469 | // Check if we need to use a vtable from the VTT. |
Anders Carlsson | 851853d | 2010-03-29 02:38:51 +0000 | [diff] [blame] | 1470 | if (CodeGenVTables::needsVTTParameter(CurGD) && |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1471 | (RD->getNumVBases() || NearestVBase)) { |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1472 | // Get the secondary vpointer index. |
| 1473 | uint64_t VirtualPointerIndex = |
| 1474 | CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); |
| 1475 | |
| 1476 | /// Load the VTT. |
| 1477 | llvm::Value *VTT = LoadCXXVTT(); |
| 1478 | if (VirtualPointerIndex) |
| 1479 | VTT = Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); |
| 1480 | |
| 1481 | // And load the address point from the VTT. |
| 1482 | VTableAddressPoint = Builder.CreateLoad(VTT); |
| 1483 | } else { |
Anders Carlsson | 64c9eca | 2010-03-29 02:08:26 +0000 | [diff] [blame] | 1484 | uint64_t AddressPoint = CGM.getVTables().getAddressPoint(Base, VTableClass); |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1485 | VTableAddressPoint = |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1486 | Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint); |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1487 | } |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1488 | |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1489 | // Compute where to store the address point. |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1490 | llvm::Value *VirtualOffset = 0; |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 1491 | CharUnits NonVirtualOffset = CharUnits::Zero(); |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1492 | |
| 1493 | if (CodeGenVTables::needsVTTParameter(CurGD) && NearestVBase) { |
| 1494 | // We need to use the virtual base offset offset because the virtual base |
| 1495 | // might have a different offset in the most derived class. |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1496 | VirtualOffset = GetVirtualBaseClassOffset(LoadCXXThis(), VTableClass, |
| 1497 | NearestVBase); |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1498 | NonVirtualOffset = OffsetFromNearestVBase; |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1499 | } else { |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1500 | // We can just use the base offset in the complete class. |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1501 | NonVirtualOffset = Base.getBaseOffset(); |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1502 | } |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1503 | |
| 1504 | // Apply the offsets. |
| 1505 | llvm::Value *VTableField = LoadCXXThis(); |
| 1506 | |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 1507 | if (!NonVirtualOffset.isZero() || VirtualOffset) |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1508 | VTableField = ApplyNonVirtualAndVirtualOffset(*this, VTableField, |
| 1509 | NonVirtualOffset, |
| 1510 | VirtualOffset); |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1511 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1512 | // Finally, store the address point. |
| 1513 | const llvm::Type *AddressPointPtrTy = |
| 1514 | VTableAddressPoint->getType()->getPointerTo(); |
| 1515 | VTableField = Builder.CreateBitCast(VTableField, AddressPointPtrTy); |
| 1516 | Builder.CreateStore(VTableAddressPoint, VTableField); |
| 1517 | } |
| 1518 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1519 | void |
| 1520 | CodeGenFunction::InitializeVTablePointers(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1521 | const CXXRecordDecl *NearestVBase, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1522 | CharUnits OffsetFromNearestVBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1523 | bool BaseIsNonVirtualPrimaryBase, |
| 1524 | llvm::Constant *VTable, |
| 1525 | const CXXRecordDecl *VTableClass, |
| 1526 | VisitedVirtualBasesSetTy& VBases) { |
| 1527 | // If this base is a non-virtual primary base the address point has already |
| 1528 | // been set. |
| 1529 | if (!BaseIsNonVirtualPrimaryBase) { |
| 1530 | // Initialize the vtable pointer for this base. |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1531 | InitializeVTablePointer(Base, NearestVBase, OffsetFromNearestVBase, |
| 1532 | VTable, VTableClass); |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | const CXXRecordDecl *RD = Base.getBase(); |
| 1536 | |
| 1537 | // Traverse bases. |
| 1538 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1539 | E = RD->bases_end(); I != E; ++I) { |
| 1540 | CXXRecordDecl *BaseDecl |
| 1541 | = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1542 | |
| 1543 | // Ignore classes without a vtable. |
| 1544 | if (!BaseDecl->isDynamicClass()) |
| 1545 | continue; |
| 1546 | |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1547 | CharUnits BaseOffset; |
| 1548 | CharUnits BaseOffsetFromNearestVBase; |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1549 | bool BaseDeclIsNonVirtualPrimaryBase; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1550 | |
| 1551 | if (I->isVirtual()) { |
| 1552 | // Check if we've visited this virtual base before. |
| 1553 | if (!VBases.insert(BaseDecl)) |
| 1554 | continue; |
| 1555 | |
| 1556 | const ASTRecordLayout &Layout = |
| 1557 | getContext().getASTRecordLayout(VTableClass); |
| 1558 | |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1559 | BaseOffset = Layout.getVBaseClassOffset(BaseDecl); |
| 1560 | BaseOffsetFromNearestVBase = CharUnits::Zero(); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1561 | BaseDeclIsNonVirtualPrimaryBase = false; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1562 | } else { |
| 1563 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 1564 | |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1565 | BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1566 | BaseOffsetFromNearestVBase = |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1567 | OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1568 | BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1571 | InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset), |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1572 | I->isVirtual() ? BaseDecl : NearestVBase, |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1573 | BaseOffsetFromNearestVBase, |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1574 | BaseDeclIsNonVirtualPrimaryBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1575 | VTable, VTableClass, VBases); |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) { |
| 1580 | // Ignore classes without a vtable. |
Anders Carlsson | 0703690 | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 1581 | if (!RD->isDynamicClass()) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1582 | return; |
| 1583 | |
Anders Carlsson | 0703690 | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 1584 | // Get the VTable. |
| 1585 | llvm::Constant *VTable = CGM.getVTables().GetAddrOfVTable(RD); |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 1586 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1587 | // Initialize the vtable pointers for this class and all of its bases. |
| 1588 | VisitedVirtualBasesSetTy VBases; |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1589 | InitializeVTablePointers(BaseSubobject(RD, CharUnits::Zero()), |
| 1590 | /*NearestVBase=*/0, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1591 | /*OffsetFromNearestVBase=*/CharUnits::Zero(), |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1592 | /*BaseIsNonVirtualPrimaryBase=*/false, |
| 1593 | VTable, RD, VBases); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1594 | } |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 1595 | |
| 1596 | llvm::Value *CodeGenFunction::GetVTablePtr(llvm::Value *This, |
| 1597 | const llvm::Type *Ty) { |
| 1598 | llvm::Value *VTablePtrSrc = Builder.CreateBitCast(This, Ty->getPointerTo()); |
| 1599 | return Builder.CreateLoad(VTablePtrSrc, "vtable"); |
| 1600 | } |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 1601 | |
| 1602 | static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) { |
| 1603 | const Expr *E = Base; |
| 1604 | |
| 1605 | while (true) { |
| 1606 | E = E->IgnoreParens(); |
| 1607 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { |
| 1608 | if (CE->getCastKind() == CK_DerivedToBase || |
| 1609 | CE->getCastKind() == CK_UncheckedDerivedToBase || |
| 1610 | CE->getCastKind() == CK_NoOp) { |
| 1611 | E = CE->getSubExpr(); |
| 1612 | continue; |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | break; |
| 1617 | } |
| 1618 | |
| 1619 | QualType DerivedType = E->getType(); |
| 1620 | if (const PointerType *PTy = DerivedType->getAs<PointerType>()) |
| 1621 | DerivedType = PTy->getPointeeType(); |
| 1622 | |
| 1623 | return cast<CXXRecordDecl>(DerivedType->castAs<RecordType>()->getDecl()); |
| 1624 | } |
| 1625 | |
| 1626 | // FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do |
| 1627 | // quite what we want. |
| 1628 | static const Expr *skipNoOpCastsAndParens(const Expr *E) { |
| 1629 | while (true) { |
| 1630 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 1631 | E = PE->getSubExpr(); |
| 1632 | continue; |
| 1633 | } |
| 1634 | |
| 1635 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { |
| 1636 | if (CE->getCastKind() == CK_NoOp) { |
| 1637 | E = CE->getSubExpr(); |
| 1638 | continue; |
| 1639 | } |
| 1640 | } |
| 1641 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 1642 | if (UO->getOpcode() == UO_Extension) { |
| 1643 | E = UO->getSubExpr(); |
| 1644 | continue; |
| 1645 | } |
| 1646 | } |
| 1647 | return E; |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | /// canDevirtualizeMemberFunctionCall - Checks whether the given virtual member |
| 1652 | /// function call on the given expr can be devirtualized. |
| 1653 | /// expr can be devirtualized. |
| 1654 | static bool canDevirtualizeMemberFunctionCall(const Expr *Base, |
| 1655 | const CXXMethodDecl *MD) { |
| 1656 | // If the most derived class is marked final, we know that no subclass can |
| 1657 | // override this member function and so we can devirtualize it. For example: |
| 1658 | // |
| 1659 | // struct A { virtual void f(); } |
| 1660 | // struct B final : A { }; |
| 1661 | // |
| 1662 | // void f(B *b) { |
| 1663 | // b->f(); |
| 1664 | // } |
| 1665 | // |
| 1666 | const CXXRecordDecl *MostDerivedClassDecl = getMostDerivedClassDecl(Base); |
| 1667 | if (MostDerivedClassDecl->hasAttr<FinalAttr>()) |
| 1668 | return true; |
| 1669 | |
| 1670 | // If the member function is marked 'final', we know that it can't be |
| 1671 | // overridden and can therefore devirtualize it. |
| 1672 | if (MD->hasAttr<FinalAttr>()) |
| 1673 | return true; |
| 1674 | |
| 1675 | // Similarly, if the class itself is marked 'final' it can't be overridden |
| 1676 | // and we can therefore devirtualize the member function call. |
| 1677 | if (MD->getParent()->hasAttr<FinalAttr>()) |
| 1678 | return true; |
| 1679 | |
| 1680 | Base = skipNoOpCastsAndParens(Base); |
| 1681 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 1682 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 1683 | // This is a record decl. We know the type and can devirtualize it. |
| 1684 | return VD->getType()->isRecordType(); |
| 1685 | } |
| 1686 | |
| 1687 | return false; |
| 1688 | } |
| 1689 | |
| 1690 | // We can always devirtualize calls on temporary object expressions. |
| 1691 | if (isa<CXXConstructExpr>(Base)) |
| 1692 | return true; |
| 1693 | |
| 1694 | // And calls on bound temporaries. |
| 1695 | if (isa<CXXBindTemporaryExpr>(Base)) |
| 1696 | return true; |
| 1697 | |
| 1698 | // Check if this is a call expr that returns a record type. |
| 1699 | if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) |
| 1700 | return CE->getCallReturnType()->isRecordType(); |
| 1701 | |
| 1702 | // We can't devirtualize the call. |
| 1703 | return false; |
| 1704 | } |
| 1705 | |
| 1706 | static bool UseVirtualCall(ASTContext &Context, |
| 1707 | const CXXOperatorCallExpr *CE, |
| 1708 | const CXXMethodDecl *MD) { |
| 1709 | if (!MD->isVirtual()) |
| 1710 | return false; |
| 1711 | |
| 1712 | // When building with -fapple-kext, all calls must go through the vtable since |
| 1713 | // the kernel linker can do runtime patching of vtables. |
| 1714 | if (Context.getLangOptions().AppleKext) |
| 1715 | return true; |
| 1716 | |
| 1717 | return !canDevirtualizeMemberFunctionCall(CE->getArg(0), MD); |
| 1718 | } |
| 1719 | |
| 1720 | llvm::Value * |
| 1721 | CodeGenFunction::EmitCXXOperatorMemberCallee(const CXXOperatorCallExpr *E, |
| 1722 | const CXXMethodDecl *MD, |
| 1723 | llvm::Value *This) { |
| 1724 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 1725 | const llvm::Type *Ty = |
| 1726 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 1727 | FPT->isVariadic()); |
| 1728 | |
| 1729 | if (UseVirtualCall(getContext(), E, MD)) |
| 1730 | return BuildVirtualCall(MD, This, Ty); |
| 1731 | |
| 1732 | return CGM.GetAddrOfFunction(MD, Ty); |
| 1733 | } |