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 | |
| 14 | #include "CodeGenFunction.h" |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 17 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | using namespace CodeGen; |
| 20 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 21 | static uint64_t |
| 22 | ComputeNonVirtualBaseClassOffset(ASTContext &Context, CXXBasePaths &Paths, |
| 23 | unsigned Start) { |
| 24 | uint64_t Offset = 0; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 25 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 26 | const CXXBasePath &Path = Paths.front(); |
| 27 | for (unsigned i = Start, e = Path.size(); i != e; ++i) { |
| 28 | const CXXBasePathElement& Element = Path[i]; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 29 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 30 | // Get the layout. |
| 31 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 32 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 33 | const CXXBaseSpecifier *BS = Element.Base; |
Mike Stump | a660440 | 2009-11-13 18:53:35 +0000 | [diff] [blame] | 34 | // FIXME: enable test3 from virt.cc to not abort. |
| 35 | if (BS->isVirtual()) |
| 36 | return 0; |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 37 | assert(!BS->isVirtual() && "Should not see virtual bases here!"); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 38 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 39 | const CXXRecordDecl *Base = |
| 40 | cast<CXXRecordDecl>(BS->getType()->getAs<RecordType>()->getDecl()); |
| 41 | |
| 42 | // Add the offset. |
| 43 | Offset += Layout.getBaseClassOffset(Base) / 8; |
| 44 | } |
| 45 | |
| 46 | return Offset; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 49 | llvm::Constant * |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 50 | CodeGenModule::GetCXXBaseClassOffset(const CXXRecordDecl *ClassDecl, |
| 51 | const CXXRecordDecl *BaseClassDecl) { |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 52 | if (ClassDecl == BaseClassDecl) |
| 53 | return 0; |
| 54 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 55 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 56 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
| 57 | if (!const_cast<CXXRecordDecl *>(ClassDecl)-> |
| 58 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClassDecl), Paths)) { |
| 59 | assert(false && "Class must be derived from the passed in base class!"); |
| 60 | return 0; |
| 61 | } |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 62 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 63 | uint64_t Offset = ComputeNonVirtualBaseClassOffset(getContext(), Paths, 0); |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 64 | if (!Offset) |
| 65 | return 0; |
| 66 | |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 67 | const llvm::Type *PtrDiffTy = |
| 68 | Types.ConvertType(getContext().getPointerDiffType()); |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 69 | |
| 70 | return llvm::ConstantInt::get(PtrDiffTy, Offset); |
| 71 | } |
| 72 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 73 | static llvm::Value *GetCXXBaseClassOffset(CodeGenFunction &CGF, |
| 74 | llvm::Value *BaseValue, |
| 75 | const CXXRecordDecl *ClassDecl, |
| 76 | const CXXRecordDecl *BaseClassDecl) { |
| 77 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 78 | /*RecordPaths=*/true, /*DetectVirtual=*/true); |
| 79 | if (!const_cast<CXXRecordDecl *>(ClassDecl)-> |
| 80 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClassDecl), Paths)) { |
| 81 | assert(false && "Class must be derived from the passed in base class!"); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | unsigned Start = 0; |
| 86 | llvm::Value *VirtualOffset = 0; |
| 87 | if (const RecordType *RT = Paths.getDetectedVirtual()) { |
| 88 | const CXXRecordDecl *VBase = cast<CXXRecordDecl>(RT->getDecl()); |
| 89 | |
| 90 | VirtualOffset = |
| 91 | CGF.GetVirtualCXXBaseClassOffset(BaseValue, ClassDecl, VBase); |
| 92 | |
| 93 | const CXXBasePath &Path = Paths.front(); |
| 94 | unsigned e = Path.size(); |
| 95 | for (Start = 0; Start != e; ++Start) { |
| 96 | const CXXBasePathElement& Element = Path[Start]; |
| 97 | |
| 98 | if (Element.Class == VBase) |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | uint64_t Offset = |
| 104 | ComputeNonVirtualBaseClassOffset(CGF.getContext(), Paths, Start); |
| 105 | |
| 106 | if (!Offset) |
| 107 | return VirtualOffset; |
| 108 | |
| 109 | const llvm::Type *PtrDiffTy = |
| 110 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 111 | llvm::Value *NonVirtualOffset = llvm::ConstantInt::get(PtrDiffTy, Offset); |
| 112 | |
| 113 | if (VirtualOffset) |
| 114 | return CGF.Builder.CreateAdd(VirtualOffset, NonVirtualOffset); |
| 115 | |
| 116 | return NonVirtualOffset; |
| 117 | } |
| 118 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 119 | llvm::Value * |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 120 | CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value, |
| 121 | const CXXRecordDecl *ClassDecl, |
| 122 | const CXXRecordDecl *BaseClassDecl, |
| 123 | bool NullCheckValue) { |
Anders Carlsson | dfd0330 | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 124 | QualType BTy = |
| 125 | getContext().getCanonicalType( |
| 126 | getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(BaseClassDecl))); |
| 127 | const llvm::Type *BasePtrTy = llvm::PointerType::getUnqual(ConvertType(BTy)); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 128 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 129 | if (ClassDecl == BaseClassDecl) { |
Anders Carlsson | dfd0330 | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 130 | // Just cast back. |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 131 | return Builder.CreateBitCast(Value, BasePtrTy); |
Anders Carlsson | dfd0330 | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 132 | } |
Eli Friedman | 4a5dc24 | 2009-11-10 22:48:10 +0000 | [diff] [blame] | 133 | |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 134 | llvm::BasicBlock *CastNull = 0; |
| 135 | llvm::BasicBlock *CastNotNull = 0; |
| 136 | llvm::BasicBlock *CastEnd = 0; |
| 137 | |
| 138 | if (NullCheckValue) { |
| 139 | CastNull = createBasicBlock("cast.null"); |
| 140 | CastNotNull = createBasicBlock("cast.notnull"); |
| 141 | CastEnd = createBasicBlock("cast.end"); |
| 142 | |
| 143 | llvm::Value *IsNull = |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 144 | Builder.CreateICmpEQ(Value, |
| 145 | llvm::Constant::getNullValue(Value->getType())); |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 146 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 147 | EmitBlock(CastNotNull); |
| 148 | } |
| 149 | |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 150 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Eli Friedman | 4a5dc24 | 2009-11-10 22:48:10 +0000 | [diff] [blame] | 151 | |
| 152 | llvm::Value *Offset = |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 153 | GetCXXBaseClassOffset(*this, Value, ClassDecl, BaseClassDecl); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 154 | |
Eli Friedman | 4a5dc24 | 2009-11-10 22:48:10 +0000 | [diff] [blame] | 155 | if (Offset) { |
| 156 | // Apply the offset. |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 157 | Value = Builder.CreateBitCast(Value, Int8PtrTy); |
| 158 | Value = Builder.CreateGEP(Value, Offset, "add.ptr"); |
Eli Friedman | 4a5dc24 | 2009-11-10 22:48:10 +0000 | [diff] [blame] | 159 | } |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 160 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 161 | // Cast back. |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 162 | Value = Builder.CreateBitCast(Value, BasePtrTy); |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 163 | |
| 164 | if (NullCheckValue) { |
| 165 | Builder.CreateBr(CastEnd); |
| 166 | EmitBlock(CastNull); |
| 167 | Builder.CreateBr(CastEnd); |
| 168 | EmitBlock(CastEnd); |
| 169 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 170 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType()); |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 171 | PHI->reserveOperandSpace(2); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 172 | PHI->addIncoming(Value, CastNotNull); |
| 173 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 174 | CastNull); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 175 | Value = PHI; |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 176 | } |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 177 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 178 | return Value; |
| 179 | } |
| 180 | |
| 181 | llvm::Value * |
| 182 | CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value, |
| 183 | const CXXRecordDecl *ClassDecl, |
| 184 | const CXXRecordDecl *DerivedClassDecl, |
| 185 | bool NullCheckValue) { |
| 186 | QualType DerivedTy = |
| 187 | getContext().getCanonicalType( |
| 188 | getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(DerivedClassDecl))); |
| 189 | const llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo(); |
| 190 | |
| 191 | if (ClassDecl == DerivedClassDecl) { |
| 192 | // Just cast back. |
| 193 | return Builder.CreateBitCast(Value, DerivedPtrTy); |
| 194 | } |
| 195 | |
| 196 | llvm::BasicBlock *CastNull = 0; |
| 197 | llvm::BasicBlock *CastNotNull = 0; |
| 198 | llvm::BasicBlock *CastEnd = 0; |
| 199 | |
| 200 | if (NullCheckValue) { |
| 201 | CastNull = createBasicBlock("cast.null"); |
| 202 | CastNotNull = createBasicBlock("cast.notnull"); |
| 203 | CastEnd = createBasicBlock("cast.end"); |
| 204 | |
| 205 | llvm::Value *IsNull = |
| 206 | Builder.CreateICmpEQ(Value, |
| 207 | llvm::Constant::getNullValue(Value->getType())); |
| 208 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 209 | EmitBlock(CastNotNull); |
| 210 | } |
| 211 | |
| 212 | llvm::Value *Offset = GetCXXBaseClassOffset(*this, Value, DerivedClassDecl, |
| 213 | ClassDecl); |
| 214 | if (Offset) { |
| 215 | // Apply the offset. |
| 216 | Value = Builder.CreatePtrToInt(Value, Offset->getType()); |
| 217 | Value = Builder.CreateSub(Value, Offset); |
| 218 | Value = Builder.CreateIntToPtr(Value, DerivedPtrTy); |
| 219 | } else { |
| 220 | // Just cast. |
| 221 | Value = Builder.CreateBitCast(Value, DerivedPtrTy); |
| 222 | } |
| 223 | |
| 224 | if (NullCheckValue) { |
| 225 | Builder.CreateBr(CastEnd); |
| 226 | EmitBlock(CastNull); |
| 227 | Builder.CreateBr(CastEnd); |
| 228 | EmitBlock(CastEnd); |
| 229 | |
| 230 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType()); |
| 231 | PHI->reserveOperandSpace(2); |
| 232 | PHI->addIncoming(Value, CastNotNull); |
| 233 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
| 234 | CastNull); |
| 235 | Value = PHI; |
| 236 | } |
| 237 | |
| 238 | return Value; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 239 | } |