Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 1 | //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===// |
| 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. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | // We might split this into multiple files if it gets too unwieldy |
| 15 | |
| 16 | #include "CodeGenFunction.h" |
| 17 | #include "CodeGenModule.h" |
Anders Carlsson | 283a062 | 2009-04-13 18:03:33 +0000 | [diff] [blame] | 18 | #include "Mangle.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 28 | void |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 29 | CodeGenFunction::EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor, |
| 30 | llvm::Constant *DeclPtr) { |
| 31 | // FIXME: This is ABI dependent and we use the Itanium ABI. |
| 32 | |
| 33 | const llvm::Type *Int8PtrTy = |
| 34 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 35 | |
| 36 | std::vector<const llvm::Type *> Params; |
| 37 | Params.push_back(Int8PtrTy); |
| 38 | |
| 39 | // Get the destructor function type |
| 40 | const llvm::Type *DtorFnTy = |
| 41 | llvm::FunctionType::get(llvm::Type::VoidTy, Params, false); |
| 42 | DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); |
| 43 | |
| 44 | Params.clear(); |
| 45 | Params.push_back(DtorFnTy); |
| 46 | Params.push_back(Int8PtrTy); |
| 47 | Params.push_back(Int8PtrTy); |
| 48 | |
| 49 | // Get the __cxa_atexit function type |
| 50 | // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); |
| 51 | const llvm::FunctionType *AtExitFnTy = |
| 52 | llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); |
| 53 | |
| 54 | llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, |
| 55 | "__cxa_atexit"); |
| 56 | |
| 57 | llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, |
| 58 | "__dso_handle"); |
| 59 | |
| 60 | llvm::Constant *DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete); |
| 61 | |
| 62 | llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), |
| 63 | llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), |
| 64 | llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; |
| 65 | Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args)); |
| 66 | } |
| 67 | |
| 68 | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, |
| 69 | llvm::Constant *DeclPtr) { |
| 70 | assert(D.hasGlobalStorage() && |
| 71 | "VarDecl must have global storage!"); |
| 72 | |
| 73 | const Expr *Init = D.getInit(); |
| 74 | QualType T = D.getType(); |
| 75 | |
| 76 | if (T->isReferenceType()) { |
| 77 | ErrorUnsupported(Init, "Global variable that binds to a reference"); |
| 78 | } else if (!hasAggregateLLVMType(T)) { |
| 79 | llvm::Value *V = EmitScalarExpr(Init); |
| 80 | EmitStoreOfScalar(V, DeclPtr, T.isVolatileQualified(), T); |
| 81 | } else if (T->isAnyComplexType()) { |
| 82 | EmitComplexExprIntoAddr(Init, DeclPtr, T.isVolatileQualified()); |
| 83 | } else { |
| 84 | EmitAggExpr(Init, DeclPtr, T.isVolatileQualified()); |
| 85 | |
| 86 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 87 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 88 | if (!RD->hasTrivialDestructor()) |
| 89 | EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 94 | void |
| 95 | CodeGenModule::EmitCXXGlobalInitFunc() { |
| 96 | if (CXXGlobalInits.empty()) |
| 97 | return; |
| 98 | |
| 99 | const llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::VoidTy, |
| 100 | false); |
| 101 | |
| 102 | // Create our global initialization function. |
| 103 | // FIXME: Should this be tweakable by targets? |
| 104 | llvm::Function *Fn = |
| 105 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 106 | "__cxx_global_initialization", &TheModule); |
| 107 | |
| 108 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
Benjamin Kramer | 10c40ee | 2009-08-08 23:43:26 +0000 | [diff] [blame^] | 109 | &CXXGlobalInits[0], |
Anders Carlsson | 89ed31d | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 110 | CXXGlobalInits.size()); |
| 111 | AddGlobalCtor(Fn); |
| 112 | } |
| 113 | |
| 114 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 115 | const VarDecl **Decls, |
| 116 | unsigned NumDecls) { |
| 117 | StartFunction(0, getContext().VoidTy, Fn, FunctionArgList(), |
| 118 | SourceLocation()); |
| 119 | |
| 120 | for (unsigned i = 0; i != NumDecls; ++i) { |
| 121 | const VarDecl *D = Decls[i]; |
| 122 | |
| 123 | llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D); |
| 124 | EmitCXXGlobalVarDeclInit(*D, DeclPtr); |
| 125 | } |
| 126 | FinishFunction(); |
| 127 | } |
| 128 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 129 | void |
| 130 | CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, |
| 131 | llvm::GlobalVariable *GV) { |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 132 | // FIXME: This should use __cxa_guard_{acquire,release}? |
| 133 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 134 | assert(!getContext().getLangOptions().ThreadsafeStatics && |
| 135 | "thread safe statics are currently not supported!"); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 136 | |
Anders Carlsson | 283a062 | 2009-04-13 18:03:33 +0000 | [diff] [blame] | 137 | llvm::SmallString<256> GuardVName; |
| 138 | llvm::raw_svector_ostream GuardVOut(GuardVName); |
| 139 | mangleGuardVariable(&D, getContext(), GuardVOut); |
| 140 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 141 | // Create the guard variable. |
| 142 | llvm::GlobalValue *GuardV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 143 | new llvm::GlobalVariable(CGM.getModule(), llvm::Type::Int64Ty, false, |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 144 | GV->getLinkage(), |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 145 | llvm::Constant::getNullValue(llvm::Type::Int64Ty), |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 146 | GuardVName.c_str()); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 147 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 148 | // Load the first byte of the guard variable. |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 149 | const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::Int8Ty, 0); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 150 | llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy), |
| 151 | "tmp"); |
| 152 | |
| 153 | // Compare it against 0. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 154 | llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::Int8Ty); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 155 | llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool"); |
| 156 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 157 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 158 | llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 159 | |
| 160 | // If the guard variable is 0, jump to the initializer code. |
| 161 | Builder.CreateCondBr(ICmp, InitBlock, EndBlock); |
| 162 | |
| 163 | EmitBlock(InitBlock); |
| 164 | |
Anders Carlsson | 3b2e16b | 2009-08-08 21:45:14 +0000 | [diff] [blame] | 165 | EmitCXXGlobalVarDeclInit(D, GV); |
| 166 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 167 | Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::Int8Ty, 1), |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 168 | Builder.CreateBitCast(GuardV, PtrTy)); |
| 169 | |
| 170 | EmitBlock(EndBlock); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 173 | RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, |
| 174 | llvm::Value *Callee, |
| 175 | llvm::Value *This, |
| 176 | CallExpr::const_arg_iterator ArgBeg, |
| 177 | CallExpr::const_arg_iterator ArgEnd) { |
| 178 | assert(MD->isInstance() && |
| 179 | "Trying to emit a member call expr on a static method!"); |
| 180 | |
| 181 | const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); |
| 182 | |
| 183 | CallArgList Args; |
| 184 | |
| 185 | // Push the this ptr. |
| 186 | Args.push_back(std::make_pair(RValue::get(This), |
| 187 | MD->getThisType(getContext()))); |
| 188 | |
| 189 | // And the rest of the call args |
| 190 | EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); |
| 191 | |
| 192 | QualType ResultType = MD->getType()->getAsFunctionType()->getResultType(); |
| 193 | return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), |
| 194 | Callee, Args, MD); |
| 195 | } |
| 196 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 197 | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) { |
| 198 | const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()); |
| 199 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 200 | |
Anders Carlsson | e9918d2 | 2009-04-08 20:31:57 +0000 | [diff] [blame] | 201 | const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); |
Mike Stump | 7116da1 | 2009-07-30 21:47:44 +0000 | [diff] [blame] | 202 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 203 | if (MD->isVirtual()) { |
Mike Stump | 7116da1 | 2009-07-30 21:47:44 +0000 | [diff] [blame] | 204 | ErrorUnsupported(CE, "virtual dispatch"); |
| 205 | } |
| 206 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 207 | const llvm::Type *Ty = |
Anders Carlsson | e9918d2 | 2009-04-08 20:31:57 +0000 | [diff] [blame] | 208 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 209 | FPT->isVariadic()); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 210 | llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 211 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 212 | llvm::Value *This; |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 213 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 214 | if (ME->isArrow()) |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 215 | This = EmitScalarExpr(ME->getBase()); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 216 | else { |
| 217 | LValue BaseLV = EmitLValue(ME->getBase()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 218 | This = BaseLV.getAddress(); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 221 | return EmitCXXMemberCall(MD, Callee, This, |
| 222 | CE->arg_begin(), CE->arg_end()); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 223 | } |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 224 | |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 225 | RValue |
| 226 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 227 | const CXXMethodDecl *MD) { |
| 228 | assert(MD->isInstance() && |
| 229 | "Trying to emit a member call expr on a static method!"); |
| 230 | |
| 231 | |
| 232 | const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); |
| 233 | const llvm::Type *Ty = |
| 234 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 235 | FPT->isVariadic()); |
| 236 | llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty); |
| 237 | |
| 238 | llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); |
| 239 | |
| 240 | return EmitCXXMemberCall(MD, Callee, This, |
| 241 | E->arg_begin() + 1, E->arg_end()); |
| 242 | } |
| 243 | |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 244 | llvm::Value *CodeGenFunction::LoadCXXThis() { |
| 245 | assert(isa<CXXMethodDecl>(CurFuncDecl) && |
| 246 | "Must be in a C++ member function decl to load 'this'"); |
| 247 | assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() && |
| 248 | "Must be in a C++ member function decl to load 'this'"); |
| 249 | |
| 250 | // FIXME: What if we're inside a block? |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 251 | // ans: See how CodeGenFunction::LoadObjCSelf() uses |
| 252 | // CodeGenFunction::BlockForwardSelf() for how to do this. |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 253 | return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this"); |
| 254 | } |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 255 | |
Fariborz Jahanian | c238a79 | 2009-07-30 00:10:25 +0000 | [diff] [blame] | 256 | static bool |
| 257 | GetNestedPaths(llvm::SmallVectorImpl<const CXXRecordDecl *> &NestedBasePaths, |
| 258 | const CXXRecordDecl *ClassDecl, |
| 259 | const CXXRecordDecl *BaseClassDecl) { |
Fariborz Jahanian | c238a79 | 2009-07-30 00:10:25 +0000 | [diff] [blame] | 260 | for (CXXRecordDecl::base_class_const_iterator i = ClassDecl->bases_begin(), |
| 261 | e = ClassDecl->bases_end(); i != e; ++i) { |
| 262 | if (i->isVirtual()) |
| 263 | continue; |
| 264 | const CXXRecordDecl *Base = |
Mike Stump | 104ffaa | 2009-08-04 21:58:42 +0000 | [diff] [blame] | 265 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | c238a79 | 2009-07-30 00:10:25 +0000 | [diff] [blame] | 266 | if (Base == BaseClassDecl) { |
| 267 | NestedBasePaths.push_back(BaseClassDecl); |
| 268 | return true; |
| 269 | } |
| 270 | } |
| 271 | // BaseClassDecl not an immediate base of ClassDecl. |
| 272 | for (CXXRecordDecl::base_class_const_iterator i = ClassDecl->bases_begin(), |
| 273 | e = ClassDecl->bases_end(); i != e; ++i) { |
| 274 | if (i->isVirtual()) |
| 275 | continue; |
| 276 | const CXXRecordDecl *Base = |
| 277 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 278 | if (GetNestedPaths(NestedBasePaths, Base, BaseClassDecl)) { |
| 279 | NestedBasePaths.push_back(Base); |
| 280 | return true; |
| 281 | } |
| 282 | } |
| 283 | return false; |
| 284 | } |
| 285 | |
Fariborz Jahanian | 9e809e7 | 2009-07-28 17:38:28 +0000 | [diff] [blame] | 286 | llvm::Value *CodeGenFunction::AddressCXXOfBaseClass(llvm::Value *BaseValue, |
Fariborz Jahanian | 6d0bdaa | 2009-07-28 18:09:28 +0000 | [diff] [blame] | 287 | const CXXRecordDecl *ClassDecl, |
| 288 | const CXXRecordDecl *BaseClassDecl) { |
Fariborz Jahanian | 9e809e7 | 2009-07-28 17:38:28 +0000 | [diff] [blame] | 289 | if (ClassDecl == BaseClassDecl) |
| 290 | return BaseValue; |
| 291 | |
Fariborz Jahanian | c238a79 | 2009-07-30 00:10:25 +0000 | [diff] [blame] | 292 | llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 293 | llvm::SmallVector<const CXXRecordDecl *, 16> NestedBasePaths; |
| 294 | GetNestedPaths(NestedBasePaths, ClassDecl, BaseClassDecl); |
| 295 | assert(NestedBasePaths.size() > 0 && |
| 296 | "AddressCXXOfBaseClass - inheritence path failed"); |
| 297 | NestedBasePaths.push_back(ClassDecl); |
| 298 | uint64_t Offset = 0; |
| 299 | |
Fariborz Jahanian | 9e809e7 | 2009-07-28 17:38:28 +0000 | [diff] [blame] | 300 | // Accessing a member of the base class. Must add delata to |
| 301 | // the load of 'this'. |
Fariborz Jahanian | c238a79 | 2009-07-30 00:10:25 +0000 | [diff] [blame] | 302 | for (unsigned i = NestedBasePaths.size()-1; i > 0; i--) { |
| 303 | const CXXRecordDecl *DerivedClass = NestedBasePaths[i]; |
| 304 | const CXXRecordDecl *BaseClass = NestedBasePaths[i-1]; |
| 305 | const ASTRecordLayout &Layout = |
| 306 | getContext().getASTRecordLayout(DerivedClass); |
| 307 | Offset += Layout.getBaseClassOffset(BaseClass) / 8; |
| 308 | } |
Fariborz Jahanian | 5a8503b | 2009-07-29 15:54:56 +0000 | [diff] [blame] | 309 | llvm::Value *OffsetVal = |
| 310 | llvm::ConstantInt::get( |
| 311 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), Offset); |
Fariborz Jahanian | 9e809e7 | 2009-07-28 17:38:28 +0000 | [diff] [blame] | 312 | BaseValue = Builder.CreateBitCast(BaseValue, I8Ptr); |
| 313 | BaseValue = Builder.CreateGEP(BaseValue, OffsetVal, "add.ptr"); |
| 314 | QualType BTy = |
| 315 | getContext().getCanonicalType( |
Fariborz Jahanian | 6d0bdaa | 2009-07-28 18:09:28 +0000 | [diff] [blame] | 316 | getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(BaseClassDecl))); |
Fariborz Jahanian | 9e809e7 | 2009-07-28 17:38:28 +0000 | [diff] [blame] | 317 | const llvm::Type *BasePtr = ConvertType(BTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 318 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Fariborz Jahanian | 9e809e7 | 2009-07-28 17:38:28 +0000 | [diff] [blame] | 319 | BaseValue = Builder.CreateBitCast(BaseValue, BasePtr); |
| 320 | return BaseValue; |
| 321 | } |
| 322 | |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 323 | void |
| 324 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 325 | CXXCtorType Type, |
| 326 | llvm::Value *This, |
| 327 | CallExpr::const_arg_iterator ArgBeg, |
| 328 | CallExpr::const_arg_iterator ArgEnd) { |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 329 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 330 | |
| 331 | EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 334 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D, |
| 335 | CXXDtorType Type, |
| 336 | llvm::Value *This) { |
| 337 | llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type); |
| 338 | |
| 339 | EmitCXXMemberCall(D, Callee, This, 0, 0); |
| 340 | } |
| 341 | |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 342 | void |
Anders Carlsson | 31ccf37 | 2009-05-03 17:47:16 +0000 | [diff] [blame] | 343 | CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest, |
| 344 | const CXXConstructExpr *E) { |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 345 | assert(Dest && "Must have a destination!"); |
| 346 | |
| 347 | const CXXRecordDecl *RD = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 348 | cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 349 | if (RD->hasTrivialConstructor()) |
| 350 | return; |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 351 | |
| 352 | // Code gen optimization to eliminate copy constructor and return |
| 353 | // its first argument instead. |
Fariborz Jahanian | 1cf9ff8 | 2009-08-06 19:12:38 +0000 | [diff] [blame] | 354 | if (E->isElidable()) { |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 355 | CXXConstructExpr::const_arg_iterator i = E->arg_begin(); |
Fariborz Jahanian | 1cf9ff8 | 2009-08-06 19:12:38 +0000 | [diff] [blame] | 356 | EmitAggExpr((*i), Dest, false); |
| 357 | return; |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 358 | } |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 359 | // Call the constructor. |
| 360 | EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest, |
| 361 | E->arg_begin(), E->arg_end()); |
| 362 | } |
| 363 | |
Anders Carlsson | a00703d | 2009-05-31 01:40:14 +0000 | [diff] [blame] | 364 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 365 | if (E->isArray()) { |
| 366 | ErrorUnsupported(E, "new[] expression"); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 367 | return llvm::UndefValue::get(ConvertType(E->getType())); |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | QualType AllocType = E->getAllocatedType(); |
| 371 | FunctionDecl *NewFD = E->getOperatorNew(); |
| 372 | const FunctionProtoType *NewFTy = NewFD->getType()->getAsFunctionProtoType(); |
| 373 | |
| 374 | CallArgList NewArgs; |
| 375 | |
| 376 | // The allocation size is the first argument. |
| 377 | QualType SizeTy = getContext().getSizeType(); |
| 378 | llvm::Value *AllocSize = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 379 | llvm::ConstantInt::get(ConvertType(SizeTy), |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 380 | getContext().getTypeSize(AllocType) / 8); |
| 381 | |
| 382 | NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy)); |
| 383 | |
| 384 | // Emit the rest of the arguments. |
| 385 | // FIXME: Ideally, this should just use EmitCallArgs. |
| 386 | CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin(); |
| 387 | |
| 388 | // First, use the types from the function type. |
| 389 | // We start at 1 here because the first argument (the allocation size) |
| 390 | // has already been emitted. |
| 391 | for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) { |
| 392 | QualType ArgType = NewFTy->getArgType(i); |
| 393 | |
| 394 | assert(getContext().getCanonicalType(ArgType.getNonReferenceType()). |
| 395 | getTypePtr() == |
| 396 | getContext().getCanonicalType(NewArg->getType()).getTypePtr() && |
| 397 | "type mismatch in call argument!"); |
| 398 | |
| 399 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 400 | ArgType)); |
| 401 | |
| 402 | } |
| 403 | |
| 404 | // Either we've emitted all the call args, or we have a call to a |
| 405 | // variadic function. |
| 406 | assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) && |
| 407 | "Extra arguments in non-variadic function!"); |
| 408 | |
| 409 | // If we still have any arguments, emit them using the type of the argument. |
| 410 | for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end(); |
| 411 | NewArg != NewArgEnd; ++NewArg) { |
| 412 | QualType ArgType = NewArg->getType(); |
| 413 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 414 | ArgType)); |
| 415 | } |
| 416 | |
| 417 | // Emit the call to new. |
| 418 | RValue RV = |
| 419 | EmitCall(CGM.getTypes().getFunctionInfo(NewFTy->getResultType(), NewArgs), |
| 420 | CGM.GetAddrOfFunction(GlobalDecl(NewFD)), |
| 421 | NewArgs, NewFD); |
| 422 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 423 | // If an allocation function is declared with an empty exception specification |
| 424 | // it returns null to indicate failure to allocate storage. [expr.new]p13. |
| 425 | // (We don't need to check for null when there's no new initializer and |
| 426 | // we're allocating a POD type). |
| 427 | bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() && |
| 428 | !(AllocType->isPODType() && !E->hasInitializer()); |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 429 | |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 430 | llvm::BasicBlock *NewNull = 0; |
| 431 | llvm::BasicBlock *NewNotNull = 0; |
| 432 | llvm::BasicBlock *NewEnd = 0; |
| 433 | |
| 434 | llvm::Value *NewPtr = RV.getScalarVal(); |
| 435 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 436 | if (NullCheckResult) { |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 437 | NewNull = createBasicBlock("new.null"); |
| 438 | NewNotNull = createBasicBlock("new.notnull"); |
| 439 | NewEnd = createBasicBlock("new.end"); |
| 440 | |
| 441 | llvm::Value *IsNull = |
| 442 | Builder.CreateICmpEQ(NewPtr, |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 443 | llvm::Constant::getNullValue(NewPtr->getType()), |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 444 | "isnull"); |
| 445 | |
| 446 | Builder.CreateCondBr(IsNull, NewNull, NewNotNull); |
| 447 | EmitBlock(NewNotNull); |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 450 | NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType())); |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 451 | |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 452 | if (AllocType->isPODType()) { |
Anders Carlsson | 215bd20 | 2009-06-01 00:26:14 +0000 | [diff] [blame] | 453 | if (E->getNumConstructorArgs() > 0) { |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 454 | assert(E->getNumConstructorArgs() == 1 && |
| 455 | "Can only have one argument to initializer of POD type."); |
| 456 | |
| 457 | const Expr *Init = E->getConstructorArg(0); |
| 458 | |
Anders Carlsson | 3923e95 | 2009-05-31 21:07:58 +0000 | [diff] [blame] | 459 | if (!hasAggregateLLVMType(AllocType)) |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 460 | Builder.CreateStore(EmitScalarExpr(Init), NewPtr); |
Anders Carlsson | 3923e95 | 2009-05-31 21:07:58 +0000 | [diff] [blame] | 461 | else if (AllocType->isAnyComplexType()) |
| 462 | EmitComplexExprIntoAddr(Init, NewPtr, AllocType.isVolatileQualified()); |
Anders Carlsson | 627a3e5 | 2009-05-31 21:12:26 +0000 | [diff] [blame] | 463 | else |
| 464 | EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified()); |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 465 | } |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 466 | } else { |
| 467 | // Call the constructor. |
| 468 | CXXConstructorDecl *Ctor = E->getConstructor(); |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 469 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 470 | EmitCXXConstructorCall(Ctor, Ctor_Complete, NewPtr, |
| 471 | E->constructor_arg_begin(), |
| 472 | E->constructor_arg_end()); |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 473 | } |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 474 | |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 475 | if (NullCheckResult) { |
| 476 | Builder.CreateBr(NewEnd); |
| 477 | EmitBlock(NewNull); |
| 478 | Builder.CreateBr(NewEnd); |
| 479 | EmitBlock(NewEnd); |
| 480 | |
| 481 | llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType()); |
| 482 | PHI->reserveOperandSpace(2); |
| 483 | PHI->addIncoming(NewPtr, NewNotNull); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 484 | PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()), NewNull); |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 485 | |
| 486 | NewPtr = PHI; |
| 487 | } |
| 488 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 489 | return NewPtr; |
Anders Carlsson | a00703d | 2009-05-31 01:40:14 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 492 | static bool canGenerateCXXstructor(const CXXRecordDecl *RD, |
| 493 | ASTContext &Context) { |
Anders Carlsson | 59d8e0f | 2009-04-15 21:02:13 +0000 | [diff] [blame] | 494 | // The class has base classes - we don't support that right now. |
| 495 | if (RD->getNumBases() > 0) |
| 496 | return false; |
| 497 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 498 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 499 | I != E; ++I) { |
Anders Carlsson | 59d8e0f | 2009-04-15 21:02:13 +0000 | [diff] [blame] | 500 | // We don't support ctors for fields that aren't POD. |
| 501 | if (!I->getType()->isPODType()) |
| 502 | return false; |
| 503 | } |
| 504 | |
| 505 | return true; |
| 506 | } |
| 507 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 508 | void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 509 | if (!canGenerateCXXstructor(D->getParent(), getContext())) { |
Anders Carlsson | 59d8e0f | 2009-04-15 21:02:13 +0000 | [diff] [blame] | 510 | ErrorUnsupported(D, "C++ constructor", true); |
| 511 | return; |
| 512 | } |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 513 | |
Anders Carlsson | 2a131fb | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 514 | EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 515 | EmitGlobal(GlobalDecl(D, Ctor_Base)); |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 516 | } |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 517 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 518 | void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, |
| 519 | CXXCtorType Type) { |
| 520 | |
| 521 | llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type); |
| 522 | |
| 523 | CodeGenFunction(*this).GenerateCode(D, Fn); |
| 524 | |
| 525 | SetFunctionDefinitionAttributes(D, Fn); |
| 526 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 527 | } |
| 528 | |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 529 | llvm::Function * |
| 530 | CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, |
| 531 | CXXCtorType Type) { |
| 532 | const llvm::FunctionType *FTy = |
| 533 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); |
| 534 | |
| 535 | const char *Name = getMangledCXXCtorName(D, Type); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 536 | return cast<llvm::Function>( |
| 537 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 538 | } |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 539 | |
| 540 | const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, |
| 541 | CXXCtorType Type) { |
| 542 | llvm::SmallString<256> Name; |
| 543 | llvm::raw_svector_ostream Out(Name); |
| 544 | mangleCXXCtor(D, Type, Context, Out); |
| 545 | |
| 546 | Name += '\0'; |
| 547 | return UniqueMangledName(Name.begin(), Name.end()); |
| 548 | } |
| 549 | |
| 550 | void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { |
| 551 | if (!canGenerateCXXstructor(D->getParent(), getContext())) { |
| 552 | ErrorUnsupported(D, "C++ destructor", true); |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | EmitCXXDestructor(D, Dtor_Complete); |
| 557 | EmitCXXDestructor(D, Dtor_Base); |
| 558 | } |
| 559 | |
| 560 | void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, |
| 561 | CXXDtorType Type) { |
| 562 | llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type); |
| 563 | |
| 564 | CodeGenFunction(*this).GenerateCode(D, Fn); |
| 565 | |
| 566 | SetFunctionDefinitionAttributes(D, Fn); |
| 567 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 568 | } |
| 569 | |
| 570 | llvm::Function * |
| 571 | CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, |
| 572 | CXXDtorType Type) { |
| 573 | const llvm::FunctionType *FTy = |
| 574 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); |
| 575 | |
| 576 | const char *Name = getMangledCXXDtorName(D, Type); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 577 | return cast<llvm::Function>( |
| 578 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, |
| 582 | CXXDtorType Type) { |
| 583 | llvm::SmallString<256> Name; |
| 584 | llvm::raw_svector_ostream Out(Name); |
| 585 | mangleCXXDtor(D, Type, Context, Out); |
| 586 | |
| 587 | Name += '\0'; |
| 588 | return UniqueMangledName(Name.begin(), Name.end()); |
| 589 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 590 | |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 591 | llvm::Constant *CodeGenFunction::GenerateRtti(const CXXRecordDecl *RD) { |
| 592 | llvm::Type *Ptr8Ty; |
| 593 | Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0); |
Mike Stump | cb1b5d3 | 2009-08-04 20:06:48 +0000 | [diff] [blame] | 594 | llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty); |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 595 | |
| 596 | if (!getContext().getLangOptions().Rtti) |
Mike Stump | cb1b5d3 | 2009-08-04 20:06:48 +0000 | [diff] [blame] | 597 | return Rtti; |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 598 | |
| 599 | llvm::SmallString<256> OutName; |
| 600 | llvm::raw_svector_ostream Out(OutName); |
| 601 | QualType ClassTy; |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 602 | ClassTy = getContext().getTagDeclType(RD); |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 603 | mangleCXXRtti(ClassTy, getContext(), Out); |
| 604 | const char *Name = OutName.c_str(); |
| 605 | llvm::GlobalVariable::LinkageTypes linktype; |
| 606 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 607 | std::vector<llvm::Constant *> info; |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame] | 608 | // assert (0 && "FIXME: implement rtti descriptor"); |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 609 | // FIXME: descriptor |
| 610 | info.push_back(llvm::Constant::getNullValue(Ptr8Ty)); |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame] | 611 | // assert (0 && "FIXME: implement rtti ts"); |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 612 | // FIXME: TS |
| 613 | info.push_back(llvm::Constant::getNullValue(Ptr8Ty)); |
| 614 | |
| 615 | llvm::Constant *C; |
| 616 | llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size()); |
| 617 | C = llvm::ConstantArray::get(type, info); |
Mike Stump | cb1b5d3 | 2009-08-04 20:06:48 +0000 | [diff] [blame] | 618 | Rtti = new llvm::GlobalVariable(CGM.getModule(), type, true, linktype, C, |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 619 | Name); |
Mike Stump | cb1b5d3 | 2009-08-04 20:06:48 +0000 | [diff] [blame] | 620 | Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty); |
| 621 | return Rtti; |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 624 | void CodeGenFunction::GenerateVtableForBase(const CXXRecordDecl *RD, |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame] | 625 | const CXXRecordDecl *Class, |
| 626 | llvm::Constant *rtti, |
| 627 | std::vector<llvm::Constant *> &methods, |
Mike Stump | 02cf1e2 | 2009-08-06 23:48:32 +0000 | [diff] [blame] | 628 | bool isPrimary, |
| 629 | bool ForVirtualBase) { |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 630 | typedef CXXRecordDecl::method_iterator meth_iter; |
| 631 | llvm::Type *Ptr8Ty; |
| 632 | Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0); |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame] | 633 | llvm::Constant *m = llvm::Constant::getNullValue(Ptr8Ty); |
| 634 | |
| 635 | if (RD && !RD->isDynamicClass()) |
| 636 | return; |
Mike Stump | 02cf1e2 | 2009-08-06 23:48:32 +0000 | [diff] [blame] | 637 | |
| 638 | if (RD && ForVirtualBase) |
| 639 | for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
| 640 | ++mi) { |
| 641 | if (mi->isVirtual()) { |
| 642 | // FIXME: vcall: offset for virtual base for this function |
| 643 | m = llvm::Constant::getNullValue(Ptr8Ty); |
| 644 | methods.push_back(m); |
| 645 | } |
| 646 | } |
| 647 | if (isPrimary && ForVirtualBase) |
| 648 | for (meth_iter mi = Class->method_begin(), |
| 649 | me = Class->method_end(); mi != me; ++mi) { |
| 650 | if (mi->isVirtual()) { |
| 651 | // FIXME: vcall: offset for virtual base for this function |
| 652 | m = llvm::Constant::getNullValue(Ptr8Ty); |
| 653 | methods.push_back(m); |
| 654 | } |
| 655 | } |
| 656 | |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame] | 657 | if (RD) { |
| 658 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Class); |
| 659 | int64_t BaseOffset = -(Layout.getBaseClassOffset(RD) / 8); |
| 660 | m = llvm::ConstantInt::get(llvm::Type::Int64Ty, BaseOffset); |
| 661 | m = llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty); |
| 662 | } |
| 663 | methods.push_back(m); |
| 664 | methods.push_back(rtti); |
| 665 | |
| 666 | if (RD) |
| 667 | for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
| 668 | ++mi) { |
| 669 | if (mi->isVirtual()) { |
| 670 | m = CGM.GetAddrOfFunction(GlobalDecl(*mi)); |
| 671 | m = llvm::ConstantExpr::getBitCast(m, Ptr8Ty); |
| 672 | methods.push_back(m); |
| 673 | } |
| 674 | } |
| 675 | if (!isPrimary) |
| 676 | return; |
| 677 | |
| 678 | // And add the virtuals for the class to the primary vtable. |
Mike Stump | 02cf1e2 | 2009-08-06 23:48:32 +0000 | [diff] [blame] | 679 | for (meth_iter mi = Class->method_begin(), me = Class->method_end(); mi != me; |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 680 | ++mi) { |
| 681 | if (mi->isVirtual()) { |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 682 | m = CGM.GetAddrOfFunction(GlobalDecl(*mi)); |
| 683 | m = llvm::ConstantExpr::getBitCast(m, Ptr8Ty); |
| 684 | methods.push_back(m); |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 689 | llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) { |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 690 | llvm::SmallString<256> OutName; |
| 691 | llvm::raw_svector_ostream Out(OutName); |
| 692 | QualType ClassTy; |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 693 | ClassTy = getContext().getTagDeclType(RD); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 694 | mangleCXXVtable(ClassTy, getContext(), Out); |
| 695 | const char *Name = OutName.c_str(); |
Mike Stump | 82b5696 | 2009-07-31 21:43:43 +0000 | [diff] [blame] | 696 | llvm::GlobalVariable::LinkageTypes linktype; |
| 697 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 698 | std::vector<llvm::Constant *> methods; |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 699 | typedef CXXRecordDecl::method_iterator meth_iter; |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 700 | llvm::Type *Ptr8Ty; |
| 701 | Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 702 | int64_t Offset = 0; |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame] | 703 | llvm::Constant *rtti = GenerateRtti(RD); |
| 704 | |
| 705 | Offset += LLVMPointerWidth; |
| 706 | Offset += LLVMPointerWidth; |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 707 | |
| 708 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 709 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
Mike Stump | e6e1d60 | 2009-08-07 20:14:05 +0000 | [diff] [blame] | 710 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
Mike Stump | 104ffaa | 2009-08-04 21:58:42 +0000 | [diff] [blame] | 711 | |
Mike Stump | 57cf2f3 | 2009-08-07 21:54:03 +0000 | [diff] [blame] | 712 | // The virtual base offsets come first. |
| 713 | for (CXXRecordDecl::reverse_base_class_const_iterator i = RD->vbases_rbegin(), |
| 714 | e = RD->vbases_rend(); i != e; ++i) { |
| 715 | const CXXRecordDecl *Base = |
| 716 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 717 | int64_t BaseOffset = Layout.getBaseClassOffset(Base) / 8; |
| 718 | llvm::Constant *m; |
| 719 | m = llvm::ConstantInt::get(llvm::Type::Int64Ty, BaseOffset); |
| 720 | m = llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty); |
| 721 | methods.push_back(m); |
| 722 | } |
| 723 | |
Mike Stump | 70101ce | 2009-08-06 18:05:22 +0000 | [diff] [blame] | 724 | // The primary base comes first. |
Mike Stump | 928f150 | 2009-08-07 19:00:50 +0000 | [diff] [blame] | 725 | GenerateVtableForBase(PrimaryBase, RD, rtti, methods, true, |
| 726 | PrimaryBaseWasVirtual); |
Mike Stump | 104ffaa | 2009-08-04 21:58:42 +0000 | [diff] [blame] | 727 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 728 | e = RD->bases_end(); i != e; ++i) { |
| 729 | if (i->isVirtual()) |
| 730 | continue; |
| 731 | const CXXRecordDecl *Base = |
| 732 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 733 | if (PrimaryBase != Base) { |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame] | 734 | GenerateVtableForBase(Base, RD, rtti, methods); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 735 | } |
| 736 | } |
Mike Stump | 70101ce | 2009-08-06 18:05:22 +0000 | [diff] [blame] | 737 | |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 738 | // FIXME: finish layout for virtual bases |
| 739 | // FIXME: audit indirect virtual bases |
| 740 | for (CXXRecordDecl::base_class_const_iterator i = RD->vbases_begin(), |
| 741 | e = RD->vbases_end(); i != e; ++i) { |
| 742 | const CXXRecordDecl *Base = |
| 743 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 744 | if (Base != PrimaryBase) |
Mike Stump | 02cf1e2 | 2009-08-06 23:48:32 +0000 | [diff] [blame] | 745 | GenerateVtableForBase(Base, RD, rtti, methods, false, true); |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 746 | } |
Mike Stump | 104ffaa | 2009-08-04 21:58:42 +0000 | [diff] [blame] | 747 | |
Mike Stump | 82b5696 | 2009-07-31 21:43:43 +0000 | [diff] [blame] | 748 | llvm::Constant *C; |
| 749 | llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size()); |
| 750 | C = llvm::ConstantArray::get(type, methods); |
| 751 | llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true, |
| 752 | linktype, C, Name); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 753 | vtable = Builder.CreateBitCast(vtable, Ptr8Ty); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 754 | vtable = Builder.CreateGEP(vtable, |
| 755 | llvm::ConstantInt::get(llvm::Type::Int64Ty, |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 756 | Offset/8)); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 757 | return vtable; |
| 758 | } |
| 759 | |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 760 | /// EmitClassMemberwiseCopy - This routine generates code to copy a class |
| 761 | /// object from SrcValue to DestValue. Copying can be either a bitwise copy |
| 762 | /// of via a copy constructor call. |
| 763 | void CodeGenFunction::EmitClassMemberwiseCopy( |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 764 | llvm::Value *Dest, llvm::Value *Src, |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 765 | const CXXRecordDecl *ClassDecl, |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 766 | const CXXRecordDecl *BaseClassDecl, QualType Ty) { |
| 767 | if (ClassDecl) { |
| 768 | Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl); |
| 769 | Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ; |
| 770 | } |
| 771 | if (BaseClassDecl->hasTrivialCopyConstructor()) { |
| 772 | EmitAggregateCopy(Dest, Src, Ty); |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 773 | return; |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 776 | if (CXXConstructorDecl *BaseCopyCtor = |
Fariborz Jahanian | 80e4b9e | 2009-08-08 00:59:58 +0000 | [diff] [blame] | 777 | BaseClassDecl->getCopyConstructor(getContext(), 0)) { |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 778 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, |
| 779 | Ctor_Complete); |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 780 | CallArgList CallArgs; |
| 781 | // Push the this (Dest) ptr. |
| 782 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 783 | BaseCopyCtor->getThisType(getContext()))); |
| 784 | |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 785 | // Push the Src ptr. |
| 786 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 787 | BaseCopyCtor->getThisType(getContext()))); |
| 788 | QualType ResultType = |
| 789 | BaseCopyCtor->getType()->getAsFunctionType()->getResultType(); |
| 790 | EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), |
| 791 | Callee, CallArgs, BaseCopyCtor); |
| 792 | } |
| 793 | } |
| 794 | |
Fariborz Jahanian | 8c241a2 | 2009-08-08 19:31:03 +0000 | [diff] [blame] | 795 | /// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 796 | /// constructor, in accordance with section 12.8 (p7 and p8) of C++03 |
| 797 | /// The implicitly-defined copy constructor for class X performs a memberwise |
| 798 | /// copy of its subobjects. The order of copying is the same as the order |
| 799 | /// of initialization of bases and members in a user-defined constructor |
| 800 | /// Each subobject is copied in the manner appropriate to its type: |
| 801 | /// if the subobject is of class type, the copy constructor for the class is |
| 802 | /// used; |
| 803 | /// if the subobject is an array, each element is copied, in the manner |
| 804 | /// appropriate to the element type; |
| 805 | /// if the subobject is of scalar type, the built-in assignment operator is |
| 806 | /// used. |
| 807 | /// Virtual base class subobjects shall be copied only once by the |
| 808 | /// implicitly-defined copy constructor |
| 809 | |
Fariborz Jahanian | 8c241a2 | 2009-08-08 19:31:03 +0000 | [diff] [blame] | 810 | void CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *CD, |
| 811 | const FunctionDecl *FD, |
| 812 | llvm::Function *Fn, |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 813 | const FunctionArgList &Args) { |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 814 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); |
| 815 | assert(!ClassDecl->hasUserDeclaredCopyConstructor() && |
Fariborz Jahanian | 8c241a2 | 2009-08-08 19:31:03 +0000 | [diff] [blame] | 816 | "SynthesizeCXXCopyConstructor - copy constructor has definition already"); |
| 817 | StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation()); |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 818 | |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 819 | FunctionArgList::const_iterator i = Args.begin(); |
| 820 | const VarDecl *ThisArg = i->first; |
| 821 | llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); |
| 822 | llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); |
| 823 | const VarDecl *SrcArg = (i+1)->first; |
| 824 | llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); |
| 825 | llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); |
| 826 | |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 827 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 828 | Base != ClassDecl->bases_end(); ++Base) { |
| 829 | // FIXME. copy constrution of virtual base NYI |
| 830 | if (Base->isVirtual()) |
| 831 | continue; |
Fariborz Jahanian | ca28361 | 2009-08-07 23:51:33 +0000 | [diff] [blame] | 832 | |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 833 | CXXRecordDecl *BaseClassDecl |
| 834 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 835 | EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, |
| 836 | Base->getType()); |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 839 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 840 | FieldEnd = ClassDecl->field_end(); |
| 841 | Field != FieldEnd; ++Field) { |
| 842 | QualType FieldType = getContext().getCanonicalType((*Field)->getType()); |
| 843 | |
| 844 | // FIXME. How about copying arrays! |
| 845 | assert(!getContext().getAsArrayType(FieldType) && |
| 846 | "FIXME. Copying arrays NYI"); |
| 847 | |
| 848 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 849 | CXXRecordDecl *FieldClassDecl |
| 850 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 851 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); |
| 852 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 853 | |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 854 | EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(), |
Fariborz Jahanian | 942f4f3 | 2009-08-08 23:32:22 +0000 | [diff] [blame] | 855 | 0 /*ClassDecl*/, FieldClassDecl, FieldType); |
Fariborz Jahanian | 1e4edd5 | 2009-08-08 00:15:41 +0000 | [diff] [blame] | 856 | continue; |
| 857 | } |
| 858 | // FIXME. Do a built-in assignment of scalar data members. |
| 859 | } |
Fariborz Jahanian | 8c241a2 | 2009-08-08 19:31:03 +0000 | [diff] [blame] | 860 | FinishFunction(); |
Fariborz Jahanian | 97a9375 | 2009-08-07 20:22:40 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 864 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 865 | /// base classes and non-static data members belonging to this constructor. |
| 866 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) { |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 867 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); |
Mike Stump | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 868 | // FIXME: Add vbase initialization |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 869 | llvm::Value *LoadOfThis = 0; |
Fariborz Jahanian | 6d0bdaa | 2009-07-28 18:09:28 +0000 | [diff] [blame] | 870 | |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 871 | for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 872 | E = CD->init_end(); |
| 873 | B != E; ++B) { |
| 874 | CXXBaseOrMemberInitializer *Member = (*B); |
| 875 | if (Member->isBaseInitializer()) { |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 876 | LoadOfThis = LoadCXXThis(); |
Fariborz Jahanian | 6d0bdaa | 2009-07-28 18:09:28 +0000 | [diff] [blame] | 877 | Type *BaseType = Member->getBaseClass(); |
| 878 | CXXRecordDecl *BaseClassDecl = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 879 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 6d0bdaa | 2009-07-28 18:09:28 +0000 | [diff] [blame] | 880 | llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl, |
| 881 | BaseClassDecl); |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 882 | EmitCXXConstructorCall(Member->getConstructor(), |
| 883 | Ctor_Complete, V, |
| 884 | Member->const_arg_begin(), |
| 885 | Member->const_arg_end()); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 886 | } else { |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 887 | // non-static data member initilaizers. |
| 888 | FieldDecl *Field = Member->getMember(); |
| 889 | QualType FieldType = getContext().getCanonicalType((Field)->getType()); |
| 890 | assert(!getContext().getAsArrayType(FieldType) |
| 891 | && "FIXME. Field arrays initialization unsupported"); |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 892 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 893 | LoadOfThis = LoadCXXThis(); |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 894 | LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 895 | if (FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 896 | |
| 897 | assert(Member->getConstructor() && |
| 898 | "EmitCtorPrologue - no constructor to initialize member"); |
| 899 | EmitCXXConstructorCall(Member->getConstructor(), |
| 900 | Ctor_Complete, LHS.getAddress(), |
| 901 | Member->const_arg_begin(), |
| 902 | Member->const_arg_end()); |
| 903 | continue; |
| 904 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 905 | |
| 906 | assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only"); |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 907 | Expr *RhsExpr = *Member->arg_begin(); |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 908 | llvm::Value *RHS = EmitScalarExpr(RhsExpr, true); |
| 909 | if (LHS.isBitfield()) |
| 910 | EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, FieldType, 0); |
| 911 | else |
| 912 | EmitStoreThroughLValue(RValue::get(RHS), LHS, FieldType); |
| 913 | } |
| 914 | } |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 915 | |
| 916 | // Initialize the vtable pointer |
Mike Stump | b502d83 | 2009-08-05 22:59:44 +0000 | [diff] [blame] | 917 | if (ClassDecl->isDynamicClass()) { |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 918 | if (!LoadOfThis) |
| 919 | LoadOfThis = LoadCXXThis(); |
| 920 | llvm::Value *VtableField; |
| 921 | llvm::Type *Ptr8Ty, *PtrPtr8Ty; |
| 922 | Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0); |
| 923 | PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0); |
| 924 | VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty); |
| 925 | llvm::Value *vtable = GenerateVtable(ClassDecl); |
| 926 | Builder.CreateStore(vtable, VtableField); |
| 927 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 928 | } |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 929 | |
| 930 | /// EmitDtorEpilogue - Emit all code that comes at the end of class's |
| 931 | /// destructor. This is to call destructors on members and base classes |
| 932 | /// in reverse order of their construction. |
| 933 | void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD) { |
| 934 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext()); |
| 935 | assert(!ClassDecl->isPolymorphic() && |
| 936 | "FIXME. polymorphic destruction not supported"); |
| 937 | (void)ClassDecl; // prevent warning. |
| 938 | |
| 939 | for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(), |
| 940 | *E = DD->destr_end(); B != E; ++B) { |
| 941 | uintptr_t BaseOrMember = (*B); |
| 942 | if (DD->isMemberToDestroy(BaseOrMember)) { |
| 943 | FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember); |
| 944 | QualType FieldType = getContext().getCanonicalType((FD)->getType()); |
| 945 | assert(!getContext().getAsArrayType(FieldType) |
| 946 | && "FIXME. Field arrays destruction unsupported"); |
| 947 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 948 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 949 | if (FieldClassDecl->hasTrivialDestructor()) |
| 950 | continue; |
| 951 | llvm::Value *LoadOfThis = LoadCXXThis(); |
| 952 | LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0); |
| 953 | EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 954 | Dtor_Complete, LHS.getAddress()); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 955 | } else { |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 956 | const RecordType *RT = |
| 957 | DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>(); |
| 958 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 959 | if (BaseClassDecl->hasTrivialDestructor()) |
| 960 | continue; |
| 961 | llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(), |
| 962 | ClassDecl,BaseClassDecl); |
| 963 | EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), |
| 964 | Dtor_Complete, V); |
| 965 | } |
| 966 | } |
| 967 | } |