Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1 | //===--- CGCXXExpr.cpp - Emit LLVM Code for C++ expressions ---------------===// |
| 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 code generation of C++ expressions |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | using namespace clang; |
| 16 | using namespace CodeGen; |
| 17 | |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 18 | static uint64_t CalculateCookiePadding(ASTContext &Ctx, const CXXNewExpr *E) { |
Anders Carlsson | 6ac5fc4 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 19 | if (!E->isArray()) |
| 20 | return 0; |
| 21 | |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 22 | QualType T = E->getAllocatedType(); |
| 23 | |
| 24 | const RecordType *RT = T->getAs<RecordType>(); |
| 25 | if (!RT) |
| 26 | return 0; |
| 27 | |
| 28 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 29 | if (!RD) |
| 30 | return 0; |
| 31 | |
| 32 | // Check if the class has a trivial destructor. |
| 33 | if (RD->hasTrivialDestructor()) { |
| 34 | // FIXME: Check for a two-argument delete. |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | // Padding is the maximum of sizeof(size_t) and alignof(T) |
| 39 | return std::max(Ctx.getTypeSize(Ctx.getSizeType()), |
Anders Carlsson | 6ac5fc4 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 40 | static_cast<uint64_t>(Ctx.getTypeAlign(T))) / 8; |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF, |
| 44 | const CXXNewExpr *E, |
| 45 | llvm::Value *& NumElements) { |
| 46 | QualType Type = E->getAllocatedType(); |
| 47 | uint64_t TypeSizeInBytes = CGF.getContext().getTypeSize(Type) / 8; |
| 48 | const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); |
| 49 | |
| 50 | if (!E->isArray()) |
| 51 | return llvm::ConstantInt::get(SizeTy, TypeSizeInBytes); |
| 52 | |
| 53 | uint64_t CookiePadding = CalculateCookiePadding(CGF.getContext(), E); |
| 54 | |
| 55 | Expr::EvalResult Result; |
| 56 | if (E->getArraySize()->Evaluate(Result, CGF.getContext()) && |
| 57 | !Result.HasSideEffects && Result.Val.isInt()) { |
| 58 | |
| 59 | uint64_t AllocSize = |
| 60 | Result.Val.getInt().getZExtValue() * TypeSizeInBytes + CookiePadding; |
| 61 | |
| 62 | NumElements = |
| 63 | llvm::ConstantInt::get(SizeTy, Result.Val.getInt().getZExtValue()); |
| 64 | |
| 65 | return llvm::ConstantInt::get(SizeTy, AllocSize); |
| 66 | } |
| 67 | |
| 68 | // Emit the array size expression. |
| 69 | NumElements = CGF.EmitScalarExpr(E->getArraySize()); |
| 70 | |
| 71 | // Multiply with the type size. |
| 72 | llvm::Value *V = |
| 73 | CGF.Builder.CreateMul(NumElements, |
| 74 | llvm::ConstantInt::get(SizeTy, TypeSizeInBytes)); |
| 75 | |
| 76 | // And add the cookie padding if necessary. |
| 77 | if (CookiePadding) |
| 78 | V = CGF.Builder.CreateAdd(V, llvm::ConstantInt::get(SizeTy, CookiePadding)); |
| 79 | |
| 80 | return V; |
| 81 | } |
| 82 | |
| 83 | static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, |
| 84 | llvm::Value *NewPtr, |
| 85 | llvm::Value *NumElements) { |
| 86 | QualType AllocType = E->getAllocatedType(); |
| 87 | |
| 88 | if (!E->isArray()) { |
| 89 | if (CXXConstructorDecl *Ctor = E->getConstructor()) { |
| 90 | CGF.EmitCXXConstructorCall(Ctor, Ctor_Complete, NewPtr, |
| 91 | E->constructor_arg_begin(), |
| 92 | E->constructor_arg_end()); |
| 93 | |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | // We have a POD type. |
| 98 | if (E->getNumConstructorArgs() == 0) |
| 99 | return; |
| 100 | |
| 101 | assert(E->getNumConstructorArgs() == 1 && |
| 102 | "Can only have one argument to initializer of POD type."); |
| 103 | |
| 104 | const Expr *Init = E->getConstructorArg(0); |
| 105 | |
| 106 | if (!CGF.hasAggregateLLVMType(AllocType)) |
| 107 | CGF.Builder.CreateStore(CGF.EmitScalarExpr(Init), NewPtr); |
| 108 | else if (AllocType->isAnyComplexType()) |
| 109 | CGF.EmitComplexExprIntoAddr(Init, NewPtr, |
| 110 | AllocType.isVolatileQualified()); |
| 111 | else |
| 112 | CGF.EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified()); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | if (CXXConstructorDecl *Ctor = E->getConstructor()) |
| 117 | CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr); |
| 118 | } |
| 119 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 120 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 121 | QualType AllocType = E->getAllocatedType(); |
| 122 | FunctionDecl *NewFD = E->getOperatorNew(); |
| 123 | const FunctionProtoType *NewFTy = NewFD->getType()->getAs<FunctionProtoType>(); |
| 124 | |
| 125 | CallArgList NewArgs; |
| 126 | |
| 127 | // The allocation size is the first argument. |
| 128 | QualType SizeTy = getContext().getSizeType(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 129 | |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 130 | llvm::Value *NumElements = 0; |
| 131 | llvm::Value *AllocSize = EmitCXXNewAllocSize(*this, E, NumElements); |
| 132 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 133 | NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy)); |
| 134 | |
| 135 | // Emit the rest of the arguments. |
| 136 | // FIXME: Ideally, this should just use EmitCallArgs. |
| 137 | CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin(); |
| 138 | |
| 139 | // First, use the types from the function type. |
| 140 | // We start at 1 here because the first argument (the allocation size) |
| 141 | // has already been emitted. |
| 142 | for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) { |
| 143 | QualType ArgType = NewFTy->getArgType(i); |
| 144 | |
| 145 | assert(getContext().getCanonicalType(ArgType.getNonReferenceType()). |
| 146 | getTypePtr() == |
| 147 | getContext().getCanonicalType(NewArg->getType()).getTypePtr() && |
| 148 | "type mismatch in call argument!"); |
| 149 | |
| 150 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 151 | ArgType)); |
| 152 | |
| 153 | } |
| 154 | |
| 155 | // Either we've emitted all the call args, or we have a call to a |
| 156 | // variadic function. |
| 157 | assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) && |
| 158 | "Extra arguments in non-variadic function!"); |
| 159 | |
| 160 | // If we still have any arguments, emit them using the type of the argument. |
| 161 | for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end(); |
| 162 | NewArg != NewArgEnd; ++NewArg) { |
| 163 | QualType ArgType = NewArg->getType(); |
| 164 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 165 | ArgType)); |
| 166 | } |
| 167 | |
| 168 | // Emit the call to new. |
| 169 | RValue RV = |
| 170 | EmitCall(CGM.getTypes().getFunctionInfo(NewFTy->getResultType(), NewArgs), |
| 171 | CGM.GetAddrOfFunction(NewFD), NewArgs, NewFD); |
| 172 | |
| 173 | // If an allocation function is declared with an empty exception specification |
| 174 | // it returns null to indicate failure to allocate storage. [expr.new]p13. |
| 175 | // (We don't need to check for null when there's no new initializer and |
| 176 | // we're allocating a POD type). |
| 177 | bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() && |
| 178 | !(AllocType->isPODType() && !E->hasInitializer()); |
| 179 | |
| 180 | llvm::BasicBlock *NewNull = 0; |
| 181 | llvm::BasicBlock *NewNotNull = 0; |
| 182 | llvm::BasicBlock *NewEnd = 0; |
| 183 | |
| 184 | llvm::Value *NewPtr = RV.getScalarVal(); |
| 185 | |
| 186 | if (NullCheckResult) { |
| 187 | NewNull = createBasicBlock("new.null"); |
| 188 | NewNotNull = createBasicBlock("new.notnull"); |
| 189 | NewEnd = createBasicBlock("new.end"); |
| 190 | |
| 191 | llvm::Value *IsNull = |
| 192 | Builder.CreateICmpEQ(NewPtr, |
| 193 | llvm::Constant::getNullValue(NewPtr->getType()), |
| 194 | "isnull"); |
| 195 | |
| 196 | Builder.CreateCondBr(IsNull, NewNull, NewNotNull); |
| 197 | EmitBlock(NewNotNull); |
| 198 | } |
| 199 | |
Anders Carlsson | 6ac5fc4 | 2009-09-23 18:59:48 +0000 | [diff] [blame] | 200 | if (uint64_t CookiePadding = CalculateCookiePadding(getContext(), E)) { |
| 201 | uint64_t CookieOffset = |
| 202 | CookiePadding - getContext().getTypeSize(SizeTy) / 8; |
| 203 | |
| 204 | llvm::Value *NumElementsPtr = |
| 205 | Builder.CreateConstInBoundsGEP1_64(NewPtr, CookieOffset); |
| 206 | |
| 207 | NumElementsPtr = Builder.CreateBitCast(NumElementsPtr, |
| 208 | ConvertType(SizeTy)->getPointerTo()); |
| 209 | Builder.CreateStore(NumElements, NumElementsPtr); |
| 210 | |
| 211 | // Now add the padding to the new ptr. |
| 212 | NewPtr = Builder.CreateConstInBoundsGEP1_64(NewPtr, CookiePadding); |
| 213 | } |
| 214 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 215 | NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType())); |
| 216 | |
Anders Carlsson | a4d4c01 | 2009-09-23 16:07:23 +0000 | [diff] [blame] | 217 | EmitNewInitializer(*this, E, NewPtr, NumElements); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 218 | |
| 219 | if (NullCheckResult) { |
| 220 | Builder.CreateBr(NewEnd); |
Eli Friedman | 7f1de45 | 2009-11-10 22:39:09 +0000 | [diff] [blame] | 221 | NewNotNull = Builder.GetInsertBlock(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 222 | EmitBlock(NewNull); |
| 223 | Builder.CreateBr(NewEnd); |
| 224 | EmitBlock(NewEnd); |
| 225 | |
| 226 | llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType()); |
| 227 | PHI->reserveOperandSpace(2); |
| 228 | PHI->addIncoming(NewPtr, NewNotNull); |
| 229 | PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()), NewNull); |
| 230 | |
| 231 | NewPtr = PHI; |
| 232 | } |
| 233 | |
| 234 | return NewPtr; |
| 235 | } |
| 236 | |
| 237 | void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { |
Fariborz Jahanian | 72c2153 | 2009-11-13 19:27:47 +0000 | [diff] [blame] | 238 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 239 | // Get at the argument before we performed the implicit conversion |
| 240 | // to void*. |
| 241 | const Expr *Arg = E->getArgument(); |
| 242 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) { |
| 243 | if (ICE->getCastKind() != CastExpr::CK_UserDefinedConversion && |
| 244 | ICE->getType()->isVoidPointerType()) |
| 245 | Arg = ICE->getSubExpr(); |
Douglas Gregor | d69dd78 | 2009-10-01 05:49:51 +0000 | [diff] [blame] | 246 | else |
| 247 | break; |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 251 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 252 | llvm::Value *Ptr = EmitScalarExpr(Arg); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 253 | |
| 254 | // Null check the pointer. |
| 255 | llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); |
| 256 | llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); |
| 257 | |
| 258 | llvm::Value *IsNull = |
| 259 | Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()), |
| 260 | "isnull"); |
| 261 | |
| 262 | Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); |
| 263 | EmitBlock(DeleteNotNull); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 264 | |
| 265 | bool ShouldCallDelete = true; |
| 266 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 267 | // Call the destructor if necessary. |
| 268 | if (const RecordType *RT = DeleteTy->getAs<RecordType>()) { |
| 269 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 270 | if (!RD->hasTrivialDestructor()) { |
| 271 | const CXXDestructorDecl *Dtor = RD->getDestructor(getContext()); |
Fariborz Jahanian | 72c2153 | 2009-11-13 19:27:47 +0000 | [diff] [blame] | 272 | if (E->isArrayForm()) { |
| 273 | QualType SizeTy = getContext().getSizeType(); |
| 274 | uint64_t CookiePadding = std::max(getContext().getTypeSize(SizeTy), |
| 275 | static_cast<uint64_t>(getContext().getTypeAlign(DeleteTy))) / 8; |
| 276 | if (CookiePadding) { |
| 277 | llvm::Type *Ptr8Ty = |
| 278 | llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
| 279 | uint64_t CookieOffset = |
| 280 | CookiePadding - getContext().getTypeSize(SizeTy) / 8; |
| 281 | llvm::Value *AllocatedObjectPtr = |
| 282 | Builder.CreateConstInBoundsGEP1_64( |
| 283 | Builder.CreateBitCast(Ptr, Ptr8Ty), -CookiePadding); |
| 284 | llvm::Value *NumElementsPtr = |
| 285 | Builder.CreateConstInBoundsGEP1_64(AllocatedObjectPtr, |
| 286 | CookieOffset); |
Fariborz Jahanian | 6fb98d9 | 2009-11-13 21:20:14 +0000 | [diff] [blame] | 287 | NumElementsPtr = Builder.CreateBitCast(NumElementsPtr, |
| 288 | ConvertType(SizeTy)->getPointerTo()); |
Fariborz Jahanian | 72c2153 | 2009-11-13 19:27:47 +0000 | [diff] [blame] | 289 | |
Fariborz Jahanian | 72c2153 | 2009-11-13 19:27:47 +0000 | [diff] [blame] | 290 | llvm::Value *NumElements = |
| 291 | Builder.CreateLoad(NumElementsPtr); |
Fariborz Jahanian | 6fb98d9 | 2009-11-13 21:20:14 +0000 | [diff] [blame] | 292 | NumElements = |
| 293 | Builder.CreateIntCast(NumElements, |
| 294 | llvm::Type::getInt64Ty(VMContext), false, |
| 295 | "count.tmp"); |
Fariborz Jahanian | 72c2153 | 2009-11-13 19:27:47 +0000 | [diff] [blame] | 296 | EmitCXXAggrDestructorCall(Dtor, NumElements, Ptr); |
| 297 | Ptr = AllocatedObjectPtr; |
| 298 | } |
| 299 | } |
| 300 | else if (Dtor->isVirtual()) { |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 301 | const llvm::Type *Ty = |
| 302 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(Dtor), |
| 303 | /*isVariadic=*/false); |
| 304 | |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 305 | llvm::Value *Callee = BuildVirtualCall(Dtor, Dtor_Deleting, Ptr, Ty); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 306 | EmitCXXMemberCall(Dtor, Callee, Ptr, 0, 0); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 307 | |
| 308 | // The dtor took care of deleting the object. |
| 309 | ShouldCallDelete = false; |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 310 | } else |
| 311 | EmitCXXDestructorCall(Dtor, Dtor_Complete, Ptr); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 316 | if (ShouldCallDelete) { |
| 317 | // Call delete. |
| 318 | FunctionDecl *DeleteFD = E->getOperatorDelete(); |
| 319 | const FunctionProtoType *DeleteFTy = |
| 320 | DeleteFD->getType()->getAs<FunctionProtoType>(); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 321 | |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 322 | CallArgList DeleteArgs; |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 323 | |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 324 | QualType ArgTy = DeleteFTy->getArgType(0); |
| 325 | llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); |
| 326 | DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy)); |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 327 | |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 328 | // Emit the call to delete. |
| 329 | EmitCall(CGM.getTypes().getFunctionInfo(DeleteFTy->getResultType(), |
| 330 | DeleteArgs), |
| 331 | CGM.GetAddrOfFunction(DeleteFD), |
| 332 | DeleteArgs, DeleteFD); |
| 333 | } |
| 334 | |
Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 335 | EmitBlock(DeleteEnd); |
| 336 | } |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 337 | |
| 338 | llvm::Value * CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { |
| 339 | QualType Ty = E->getType(); |
| 340 | const llvm::Type *LTy = ConvertType(Ty)->getPointerTo(); |
| 341 | if (E->isTypeOperand()) { |
| 342 | QualType Ty = E->getTypeOperand(); |
Mike Stump | 8055632 | 2009-11-15 20:30:39 +0000 | [diff] [blame^] | 343 | CanQualType CanTy = CGM.getContext().getCanonicalType(Ty); |
| 344 | Ty = CanTy.getUnqualifiedType().getNonReferenceType(); |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 345 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 346 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 347 | if (RD->isPolymorphic()) |
| 348 | return Builder.CreateBitCast(CGM.GenerateRttiRef(RD), LTy); |
| 349 | return Builder.CreateBitCast(CGM.GenerateRtti(RD), LTy); |
| 350 | } |
| 351 | // FIXME: return the rtti for the non-class static type. |
| 352 | ErrorUnsupported(E, "typeid expression"); |
| 353 | return 0; |
| 354 | } |
| 355 | Expr *subE = E->getExprOperand(); |
| 356 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 357 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 358 | if (RD->isPolymorphic()) { |
| 359 | // FIXME: if subE is an lvalue do |
| 360 | LValue Obj = EmitLValue(subE); |
| 361 | llvm::Value *This = Obj.getAddress(); |
Mike Stump | f549e89 | 2009-11-15 16:52:53 +0000 | [diff] [blame] | 362 | LTy = LTy->getPointerTo()->getPointerTo(); |
| 363 | llvm::Value *V = Builder.CreateBitCast(This, LTy); |
| 364 | // We need to do a zero check for *p, unless it has NonNullAttr. |
| 365 | // FIXME: PointerType->hasAttr<NonNullAttr>() |
| 366 | bool CanBeZero = false; |
| 367 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(subE)) |
| 368 | if (UO->getOpcode() == UnaryOperator::Deref) |
| 369 | CanBeZero = true; |
| 370 | if (CanBeZero) { |
| 371 | llvm::BasicBlock *NonZeroBlock = createBasicBlock(); |
| 372 | llvm::BasicBlock *ZeroBlock = createBasicBlock(); |
| 373 | |
| 374 | llvm::Value *Zero = llvm::Constant::getNullValue(LTy); |
| 375 | Builder.CreateCondBr(Builder.CreateICmpNE(V, Zero), |
| 376 | NonZeroBlock, ZeroBlock); |
| 377 | EmitBlock(ZeroBlock); |
| 378 | /// Call __cxa_bad_typeid |
| 379 | const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); |
| 380 | const llvm::FunctionType *FTy; |
| 381 | FTy = llvm::FunctionType::get(ResultType, false); |
| 382 | llvm::Value *F = CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); |
| 383 | Builder.CreateCall(F); |
| 384 | Builder.CreateUnreachable(); |
| 385 | EmitBlock(NonZeroBlock); |
| 386 | } |
Mike Stump | c2e84ae | 2009-11-15 08:09:41 +0000 | [diff] [blame] | 387 | V = Builder.CreateLoad(V, "vtable"); |
| 388 | V = Builder.CreateConstInBoundsGEP1_64(V, -1ULL); |
| 389 | V = Builder.CreateLoad(V); |
| 390 | return V; |
| 391 | } |
| 392 | return CGM.GenerateRtti(RD); |
| 393 | } |
| 394 | // FIXME: return rtti for the non-class static type. |
| 395 | ErrorUnsupported(E, "typeid expression"); |
| 396 | return 0; |
| 397 | } |