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 |
| 29 | CodeGenFunction::GenerateStaticCXXBlockVarDeclInit(const VarDecl &D, |
| 30 | llvm::GlobalVariable *GV) { |
| 31 | // FIXME: This should use __cxa_guard_{acquire,release}? |
| 32 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 33 | assert(!getContext().getLangOptions().ThreadsafeStatics && |
| 34 | "thread safe statics are currently not supported!"); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 35 | |
Anders Carlsson | 283a062 | 2009-04-13 18:03:33 +0000 | [diff] [blame] | 36 | llvm::SmallString<256> GuardVName; |
| 37 | llvm::raw_svector_ostream GuardVOut(GuardVName); |
| 38 | mangleGuardVariable(&D, getContext(), GuardVOut); |
| 39 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 40 | // Create the guard variable. |
| 41 | llvm::GlobalValue *GuardV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 42 | new llvm::GlobalVariable(CGM.getModule(), llvm::Type::Int64Ty, false, |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 43 | GV->getLinkage(), |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 44 | llvm::Constant::getNullValue(llvm::Type::Int64Ty), |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 45 | GuardVName.c_str()); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 46 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 47 | // Load the first byte of the guard variable. |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 48 | const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::Int8Ty, 0); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 49 | llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy), |
| 50 | "tmp"); |
| 51 | |
| 52 | // Compare it against 0. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 53 | llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::Int8Ty); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 54 | llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool"); |
| 55 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 56 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 57 | llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 58 | |
| 59 | // If the guard variable is 0, jump to the initializer code. |
| 60 | Builder.CreateCondBr(ICmp, InitBlock, EndBlock); |
| 61 | |
| 62 | EmitBlock(InitBlock); |
| 63 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 64 | const Expr *Init = D.getInit(); |
| 65 | if (!hasAggregateLLVMType(Init->getType())) { |
| 66 | llvm::Value *V = EmitScalarExpr(Init); |
| 67 | Builder.CreateStore(V, GV, D.getType().isVolatileQualified()); |
| 68 | } else if (Init->getType()->isAnyComplexType()) { |
| 69 | EmitComplexExprIntoAddr(Init, GV, D.getType().isVolatileQualified()); |
| 70 | } else { |
| 71 | EmitAggExpr(Init, GV, D.getType().isVolatileQualified()); |
| 72 | } |
| 73 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 74 | Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::Int8Ty, 1), |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 75 | Builder.CreateBitCast(GuardV, PtrTy)); |
| 76 | |
| 77 | EmitBlock(EndBlock); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 80 | RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, |
| 81 | llvm::Value *Callee, |
| 82 | llvm::Value *This, |
| 83 | CallExpr::const_arg_iterator ArgBeg, |
| 84 | CallExpr::const_arg_iterator ArgEnd) { |
| 85 | assert(MD->isInstance() && |
| 86 | "Trying to emit a member call expr on a static method!"); |
| 87 | |
| 88 | const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); |
| 89 | |
| 90 | CallArgList Args; |
| 91 | |
| 92 | // Push the this ptr. |
| 93 | Args.push_back(std::make_pair(RValue::get(This), |
| 94 | MD->getThisType(getContext()))); |
| 95 | |
| 96 | // And the rest of the call args |
| 97 | EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); |
| 98 | |
| 99 | QualType ResultType = MD->getType()->getAsFunctionType()->getResultType(); |
| 100 | return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), |
| 101 | Callee, Args, MD); |
| 102 | } |
| 103 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 104 | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) { |
| 105 | const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()); |
| 106 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 107 | |
Anders Carlsson | e9918d2 | 2009-04-08 20:31:57 +0000 | [diff] [blame] | 108 | const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); |
Mike Stump | 7116da1 | 2009-07-30 21:47:44 +0000 | [diff] [blame] | 109 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 110 | if (MD->isVirtual()) { |
Mike Stump | 7116da1 | 2009-07-30 21:47:44 +0000 | [diff] [blame] | 111 | ErrorUnsupported(CE, "virtual dispatch"); |
| 112 | } |
| 113 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 114 | const llvm::Type *Ty = |
Anders Carlsson | e9918d2 | 2009-04-08 20:31:57 +0000 | [diff] [blame] | 115 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 116 | FPT->isVariadic()); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 117 | llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 118 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 119 | llvm::Value *This; |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 120 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 121 | if (ME->isArrow()) |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 122 | This = EmitScalarExpr(ME->getBase()); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 123 | else { |
| 124 | LValue BaseLV = EmitLValue(ME->getBase()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 125 | This = BaseLV.getAddress(); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 128 | return EmitCXXMemberCall(MD, Callee, This, |
| 129 | CE->arg_begin(), CE->arg_end()); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 130 | } |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 131 | |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 132 | RValue |
| 133 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 134 | const CXXMethodDecl *MD) { |
| 135 | assert(MD->isInstance() && |
| 136 | "Trying to emit a member call expr on a static method!"); |
| 137 | |
| 138 | |
| 139 | const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); |
| 140 | const llvm::Type *Ty = |
| 141 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 142 | FPT->isVariadic()); |
| 143 | llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty); |
| 144 | |
| 145 | llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); |
| 146 | |
| 147 | return EmitCXXMemberCall(MD, Callee, This, |
| 148 | E->arg_begin() + 1, E->arg_end()); |
| 149 | } |
| 150 | |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 151 | llvm::Value *CodeGenFunction::LoadCXXThis() { |
| 152 | assert(isa<CXXMethodDecl>(CurFuncDecl) && |
| 153 | "Must be in a C++ member function decl to load 'this'"); |
| 154 | assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() && |
| 155 | "Must be in a C++ member function decl to load 'this'"); |
| 156 | |
| 157 | // FIXME: What if we're inside a block? |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 158 | // ans: See how CodeGenFunction::LoadObjCSelf() uses |
| 159 | // CodeGenFunction::BlockForwardSelf() for how to do this. |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 160 | return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this"); |
| 161 | } |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 162 | |
Fariborz Jahanian | c238a79 | 2009-07-30 00:10:25 +0000 | [diff] [blame] | 163 | static bool |
| 164 | GetNestedPaths(llvm::SmallVectorImpl<const CXXRecordDecl *> &NestedBasePaths, |
| 165 | const CXXRecordDecl *ClassDecl, |
| 166 | const CXXRecordDecl *BaseClassDecl) { |
Fariborz Jahanian | c238a79 | 2009-07-30 00:10:25 +0000 | [diff] [blame] | 167 | for (CXXRecordDecl::base_class_const_iterator i = ClassDecl->bases_begin(), |
| 168 | e = ClassDecl->bases_end(); i != e; ++i) { |
| 169 | if (i->isVirtual()) |
| 170 | continue; |
| 171 | const CXXRecordDecl *Base = |
Mike Stump | 104ffaa | 2009-08-04 21:58:42 +0000 | [diff] [blame] | 172 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | c238a79 | 2009-07-30 00:10:25 +0000 | [diff] [blame] | 173 | if (Base == BaseClassDecl) { |
| 174 | NestedBasePaths.push_back(BaseClassDecl); |
| 175 | return true; |
| 176 | } |
| 177 | } |
| 178 | // BaseClassDecl not an immediate base of ClassDecl. |
| 179 | for (CXXRecordDecl::base_class_const_iterator i = ClassDecl->bases_begin(), |
| 180 | e = ClassDecl->bases_end(); i != e; ++i) { |
| 181 | if (i->isVirtual()) |
| 182 | continue; |
| 183 | const CXXRecordDecl *Base = |
| 184 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 185 | if (GetNestedPaths(NestedBasePaths, Base, BaseClassDecl)) { |
| 186 | NestedBasePaths.push_back(Base); |
| 187 | return true; |
| 188 | } |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
Fariborz Jahanian | 9e809e7 | 2009-07-28 17:38:28 +0000 | [diff] [blame] | 193 | llvm::Value *CodeGenFunction::AddressCXXOfBaseClass(llvm::Value *BaseValue, |
Fariborz Jahanian | 6d0bdaa | 2009-07-28 18:09:28 +0000 | [diff] [blame] | 194 | const CXXRecordDecl *ClassDecl, |
| 195 | const CXXRecordDecl *BaseClassDecl) { |
Fariborz Jahanian | 9e809e7 | 2009-07-28 17:38:28 +0000 | [diff] [blame] | 196 | if (ClassDecl == BaseClassDecl) |
| 197 | return BaseValue; |
| 198 | |
Fariborz Jahanian | c238a79 | 2009-07-30 00:10:25 +0000 | [diff] [blame] | 199 | llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 200 | llvm::SmallVector<const CXXRecordDecl *, 16> NestedBasePaths; |
| 201 | GetNestedPaths(NestedBasePaths, ClassDecl, BaseClassDecl); |
| 202 | assert(NestedBasePaths.size() > 0 && |
| 203 | "AddressCXXOfBaseClass - inheritence path failed"); |
| 204 | NestedBasePaths.push_back(ClassDecl); |
| 205 | uint64_t Offset = 0; |
| 206 | |
Fariborz Jahanian | 9e809e7 | 2009-07-28 17:38:28 +0000 | [diff] [blame] | 207 | // Accessing a member of the base class. Must add delata to |
| 208 | // the load of 'this'. |
Fariborz Jahanian | c238a79 | 2009-07-30 00:10:25 +0000 | [diff] [blame] | 209 | for (unsigned i = NestedBasePaths.size()-1; i > 0; i--) { |
| 210 | const CXXRecordDecl *DerivedClass = NestedBasePaths[i]; |
| 211 | const CXXRecordDecl *BaseClass = NestedBasePaths[i-1]; |
| 212 | const ASTRecordLayout &Layout = |
| 213 | getContext().getASTRecordLayout(DerivedClass); |
| 214 | Offset += Layout.getBaseClassOffset(BaseClass) / 8; |
| 215 | } |
Fariborz Jahanian | 5a8503b | 2009-07-29 15:54:56 +0000 | [diff] [blame] | 216 | llvm::Value *OffsetVal = |
| 217 | llvm::ConstantInt::get( |
| 218 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), Offset); |
Fariborz Jahanian | 9e809e7 | 2009-07-28 17:38:28 +0000 | [diff] [blame] | 219 | BaseValue = Builder.CreateBitCast(BaseValue, I8Ptr); |
| 220 | BaseValue = Builder.CreateGEP(BaseValue, OffsetVal, "add.ptr"); |
| 221 | QualType BTy = |
| 222 | getContext().getCanonicalType( |
Fariborz Jahanian | 6d0bdaa | 2009-07-28 18:09:28 +0000 | [diff] [blame] | 223 | getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(BaseClassDecl))); |
Fariborz Jahanian | 9e809e7 | 2009-07-28 17:38:28 +0000 | [diff] [blame] | 224 | const llvm::Type *BasePtr = ConvertType(BTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 225 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Fariborz Jahanian | 9e809e7 | 2009-07-28 17:38:28 +0000 | [diff] [blame] | 226 | BaseValue = Builder.CreateBitCast(BaseValue, BasePtr); |
| 227 | return BaseValue; |
| 228 | } |
| 229 | |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 230 | void |
| 231 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 232 | CXXCtorType Type, |
| 233 | llvm::Value *This, |
| 234 | CallExpr::const_arg_iterator ArgBeg, |
| 235 | CallExpr::const_arg_iterator ArgEnd) { |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 236 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 237 | |
| 238 | EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 241 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D, |
| 242 | CXXDtorType Type, |
| 243 | llvm::Value *This) { |
| 244 | llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type); |
| 245 | |
| 246 | EmitCXXMemberCall(D, Callee, This, 0, 0); |
| 247 | } |
| 248 | |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 249 | void |
Anders Carlsson | 31ccf37 | 2009-05-03 17:47:16 +0000 | [diff] [blame] | 250 | CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest, |
| 251 | const CXXConstructExpr *E) { |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 252 | assert(Dest && "Must have a destination!"); |
| 253 | |
| 254 | const CXXRecordDecl *RD = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 255 | cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 256 | if (RD->hasTrivialConstructor()) |
| 257 | return; |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 258 | |
| 259 | // Code gen optimization to eliminate copy constructor and return |
| 260 | // its first argument instead. |
Fariborz Jahanian | 1cf9ff8 | 2009-08-06 19:12:38 +0000 | [diff] [blame] | 261 | if (E->isElidable()) { |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 262 | CXXConstructExpr::const_arg_iterator i = E->arg_begin(); |
Fariborz Jahanian | 1cf9ff8 | 2009-08-06 19:12:38 +0000 | [diff] [blame] | 263 | EmitAggExpr((*i), Dest, false); |
| 264 | return; |
Fariborz Jahanian | 6904cbb | 2009-08-06 01:02:49 +0000 | [diff] [blame] | 265 | } |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 266 | // Call the constructor. |
| 267 | EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest, |
| 268 | E->arg_begin(), E->arg_end()); |
| 269 | } |
| 270 | |
Anders Carlsson | a00703d | 2009-05-31 01:40:14 +0000 | [diff] [blame] | 271 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 272 | if (E->isArray()) { |
| 273 | ErrorUnsupported(E, "new[] expression"); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 274 | return llvm::UndefValue::get(ConvertType(E->getType())); |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | QualType AllocType = E->getAllocatedType(); |
| 278 | FunctionDecl *NewFD = E->getOperatorNew(); |
| 279 | const FunctionProtoType *NewFTy = NewFD->getType()->getAsFunctionProtoType(); |
| 280 | |
| 281 | CallArgList NewArgs; |
| 282 | |
| 283 | // The allocation size is the first argument. |
| 284 | QualType SizeTy = getContext().getSizeType(); |
| 285 | llvm::Value *AllocSize = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 286 | llvm::ConstantInt::get(ConvertType(SizeTy), |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 287 | getContext().getTypeSize(AllocType) / 8); |
| 288 | |
| 289 | NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy)); |
| 290 | |
| 291 | // Emit the rest of the arguments. |
| 292 | // FIXME: Ideally, this should just use EmitCallArgs. |
| 293 | CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin(); |
| 294 | |
| 295 | // First, use the types from the function type. |
| 296 | // We start at 1 here because the first argument (the allocation size) |
| 297 | // has already been emitted. |
| 298 | for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) { |
| 299 | QualType ArgType = NewFTy->getArgType(i); |
| 300 | |
| 301 | assert(getContext().getCanonicalType(ArgType.getNonReferenceType()). |
| 302 | getTypePtr() == |
| 303 | getContext().getCanonicalType(NewArg->getType()).getTypePtr() && |
| 304 | "type mismatch in call argument!"); |
| 305 | |
| 306 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 307 | ArgType)); |
| 308 | |
| 309 | } |
| 310 | |
| 311 | // Either we've emitted all the call args, or we have a call to a |
| 312 | // variadic function. |
| 313 | assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) && |
| 314 | "Extra arguments in non-variadic function!"); |
| 315 | |
| 316 | // If we still have any arguments, emit them using the type of the argument. |
| 317 | for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end(); |
| 318 | NewArg != NewArgEnd; ++NewArg) { |
| 319 | QualType ArgType = NewArg->getType(); |
| 320 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 321 | ArgType)); |
| 322 | } |
| 323 | |
| 324 | // Emit the call to new. |
| 325 | RValue RV = |
| 326 | EmitCall(CGM.getTypes().getFunctionInfo(NewFTy->getResultType(), NewArgs), |
| 327 | CGM.GetAddrOfFunction(GlobalDecl(NewFD)), |
| 328 | NewArgs, NewFD); |
| 329 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 330 | // If an allocation function is declared with an empty exception specification |
| 331 | // it returns null to indicate failure to allocate storage. [expr.new]p13. |
| 332 | // (We don't need to check for null when there's no new initializer and |
| 333 | // we're allocating a POD type). |
| 334 | bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() && |
| 335 | !(AllocType->isPODType() && !E->hasInitializer()); |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 336 | |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 337 | llvm::BasicBlock *NewNull = 0; |
| 338 | llvm::BasicBlock *NewNotNull = 0; |
| 339 | llvm::BasicBlock *NewEnd = 0; |
| 340 | |
| 341 | llvm::Value *NewPtr = RV.getScalarVal(); |
| 342 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 343 | if (NullCheckResult) { |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 344 | NewNull = createBasicBlock("new.null"); |
| 345 | NewNotNull = createBasicBlock("new.notnull"); |
| 346 | NewEnd = createBasicBlock("new.end"); |
| 347 | |
| 348 | llvm::Value *IsNull = |
| 349 | Builder.CreateICmpEQ(NewPtr, |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 350 | llvm::Constant::getNullValue(NewPtr->getType()), |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 351 | "isnull"); |
| 352 | |
| 353 | Builder.CreateCondBr(IsNull, NewNull, NewNotNull); |
| 354 | EmitBlock(NewNotNull); |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 357 | NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType())); |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 358 | |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 359 | if (AllocType->isPODType()) { |
Anders Carlsson | 215bd20 | 2009-06-01 00:26:14 +0000 | [diff] [blame] | 360 | if (E->getNumConstructorArgs() > 0) { |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 361 | assert(E->getNumConstructorArgs() == 1 && |
| 362 | "Can only have one argument to initializer of POD type."); |
| 363 | |
| 364 | const Expr *Init = E->getConstructorArg(0); |
| 365 | |
Anders Carlsson | 3923e95 | 2009-05-31 21:07:58 +0000 | [diff] [blame] | 366 | if (!hasAggregateLLVMType(AllocType)) |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 367 | Builder.CreateStore(EmitScalarExpr(Init), NewPtr); |
Anders Carlsson | 3923e95 | 2009-05-31 21:07:58 +0000 | [diff] [blame] | 368 | else if (AllocType->isAnyComplexType()) |
| 369 | EmitComplexExprIntoAddr(Init, NewPtr, AllocType.isVolatileQualified()); |
Anders Carlsson | 627a3e5 | 2009-05-31 21:12:26 +0000 | [diff] [blame] | 370 | else |
| 371 | EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified()); |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 372 | } |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 373 | } else { |
| 374 | // Call the constructor. |
| 375 | CXXConstructorDecl *Ctor = E->getConstructor(); |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 376 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 377 | EmitCXXConstructorCall(Ctor, Ctor_Complete, NewPtr, |
| 378 | E->constructor_arg_begin(), |
| 379 | E->constructor_arg_end()); |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 380 | } |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 381 | |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 382 | if (NullCheckResult) { |
| 383 | Builder.CreateBr(NewEnd); |
| 384 | EmitBlock(NewNull); |
| 385 | Builder.CreateBr(NewEnd); |
| 386 | EmitBlock(NewEnd); |
| 387 | |
| 388 | llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType()); |
| 389 | PHI->reserveOperandSpace(2); |
| 390 | PHI->addIncoming(NewPtr, NewNotNull); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 391 | PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()), NewNull); |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 392 | |
| 393 | NewPtr = PHI; |
| 394 | } |
| 395 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 396 | return NewPtr; |
Anders Carlsson | a00703d | 2009-05-31 01:40:14 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 399 | static bool canGenerateCXXstructor(const CXXRecordDecl *RD, |
| 400 | ASTContext &Context) { |
Anders Carlsson | 59d8e0f | 2009-04-15 21:02:13 +0000 | [diff] [blame] | 401 | // The class has base classes - we don't support that right now. |
| 402 | if (RD->getNumBases() > 0) |
| 403 | return false; |
| 404 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 405 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 406 | I != E; ++I) { |
Anders Carlsson | 59d8e0f | 2009-04-15 21:02:13 +0000 | [diff] [blame] | 407 | // We don't support ctors for fields that aren't POD. |
| 408 | if (!I->getType()->isPODType()) |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | return true; |
| 413 | } |
| 414 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 415 | void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 416 | if (!canGenerateCXXstructor(D->getParent(), getContext())) { |
Anders Carlsson | 59d8e0f | 2009-04-15 21:02:13 +0000 | [diff] [blame] | 417 | ErrorUnsupported(D, "C++ constructor", true); |
| 418 | return; |
| 419 | } |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 420 | |
Anders Carlsson | 2a131fb | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 421 | EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 422 | EmitGlobal(GlobalDecl(D, Ctor_Base)); |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 423 | } |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 424 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 425 | void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, |
| 426 | CXXCtorType Type) { |
| 427 | |
| 428 | llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type); |
| 429 | |
| 430 | CodeGenFunction(*this).GenerateCode(D, Fn); |
| 431 | |
| 432 | SetFunctionDefinitionAttributes(D, Fn); |
| 433 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 434 | } |
| 435 | |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 436 | llvm::Function * |
| 437 | CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, |
| 438 | CXXCtorType Type) { |
| 439 | const llvm::FunctionType *FTy = |
| 440 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); |
| 441 | |
| 442 | const char *Name = getMangledCXXCtorName(D, Type); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 443 | return cast<llvm::Function>( |
| 444 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 445 | } |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 446 | |
| 447 | const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, |
| 448 | CXXCtorType Type) { |
| 449 | llvm::SmallString<256> Name; |
| 450 | llvm::raw_svector_ostream Out(Name); |
| 451 | mangleCXXCtor(D, Type, Context, Out); |
| 452 | |
| 453 | Name += '\0'; |
| 454 | return UniqueMangledName(Name.begin(), Name.end()); |
| 455 | } |
| 456 | |
| 457 | void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { |
| 458 | if (!canGenerateCXXstructor(D->getParent(), getContext())) { |
| 459 | ErrorUnsupported(D, "C++ destructor", true); |
| 460 | return; |
| 461 | } |
| 462 | |
| 463 | EmitCXXDestructor(D, Dtor_Complete); |
| 464 | EmitCXXDestructor(D, Dtor_Base); |
| 465 | } |
| 466 | |
| 467 | void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, |
| 468 | CXXDtorType Type) { |
| 469 | llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type); |
| 470 | |
| 471 | CodeGenFunction(*this).GenerateCode(D, Fn); |
| 472 | |
| 473 | SetFunctionDefinitionAttributes(D, Fn); |
| 474 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 475 | } |
| 476 | |
| 477 | llvm::Function * |
| 478 | CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, |
| 479 | CXXDtorType Type) { |
| 480 | const llvm::FunctionType *FTy = |
| 481 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); |
| 482 | |
| 483 | const char *Name = getMangledCXXDtorName(D, Type); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 484 | return cast<llvm::Function>( |
| 485 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, |
| 489 | CXXDtorType Type) { |
| 490 | llvm::SmallString<256> Name; |
| 491 | llvm::raw_svector_ostream Out(Name); |
| 492 | mangleCXXDtor(D, Type, Context, Out); |
| 493 | |
| 494 | Name += '\0'; |
| 495 | return UniqueMangledName(Name.begin(), Name.end()); |
| 496 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 497 | |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 498 | llvm::Constant *CodeGenFunction::GenerateRtti(const CXXRecordDecl *RD) { |
| 499 | llvm::Type *Ptr8Ty; |
| 500 | Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0); |
Mike Stump | cb1b5d3 | 2009-08-04 20:06:48 +0000 | [diff] [blame] | 501 | llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty); |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 502 | |
| 503 | if (!getContext().getLangOptions().Rtti) |
Mike Stump | cb1b5d3 | 2009-08-04 20:06:48 +0000 | [diff] [blame] | 504 | return Rtti; |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 505 | |
| 506 | llvm::SmallString<256> OutName; |
| 507 | llvm::raw_svector_ostream Out(OutName); |
| 508 | QualType ClassTy; |
| 509 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 510 | // away const? mutable? |
| 511 | ClassTy = getContext().getTagDeclType(const_cast<CXXRecordDecl*>(RD)); |
| 512 | mangleCXXRtti(ClassTy, getContext(), Out); |
| 513 | const char *Name = OutName.c_str(); |
| 514 | llvm::GlobalVariable::LinkageTypes linktype; |
| 515 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 516 | std::vector<llvm::Constant *> info; |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame^] | 517 | // assert (0 && "FIXME: implement rtti descriptor"); |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 518 | // FIXME: descriptor |
| 519 | info.push_back(llvm::Constant::getNullValue(Ptr8Ty)); |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame^] | 520 | // assert (0 && "FIXME: implement rtti ts"); |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 521 | // FIXME: TS |
| 522 | info.push_back(llvm::Constant::getNullValue(Ptr8Ty)); |
| 523 | |
| 524 | llvm::Constant *C; |
| 525 | llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size()); |
| 526 | C = llvm::ConstantArray::get(type, info); |
Mike Stump | cb1b5d3 | 2009-08-04 20:06:48 +0000 | [diff] [blame] | 527 | Rtti = new llvm::GlobalVariable(CGM.getModule(), type, true, linktype, C, |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 528 | Name); |
Mike Stump | cb1b5d3 | 2009-08-04 20:06:48 +0000 | [diff] [blame] | 529 | Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty); |
| 530 | return Rtti; |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 533 | void CodeGenFunction::GenerateVtableForBase(const CXXRecordDecl *RD, |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame^] | 534 | const CXXRecordDecl *Class, |
| 535 | llvm::Constant *rtti, |
| 536 | std::vector<llvm::Constant *> &methods, |
| 537 | bool isPrimary) { |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 538 | typedef CXXRecordDecl::method_iterator meth_iter; |
| 539 | llvm::Type *Ptr8Ty; |
| 540 | Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0); |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame^] | 541 | llvm::Constant *m = llvm::Constant::getNullValue(Ptr8Ty); |
| 542 | |
| 543 | if (RD && !RD->isDynamicClass()) |
| 544 | return; |
| 545 | if (RD) { |
| 546 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Class); |
| 547 | int64_t BaseOffset = -(Layout.getBaseClassOffset(RD) / 8); |
| 548 | m = llvm::ConstantInt::get(llvm::Type::Int64Ty, BaseOffset); |
| 549 | m = llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty); |
| 550 | } |
| 551 | methods.push_back(m); |
| 552 | methods.push_back(rtti); |
| 553 | |
| 554 | if (RD) |
| 555 | for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
| 556 | ++mi) { |
| 557 | if (mi->isVirtual()) { |
| 558 | m = CGM.GetAddrOfFunction(GlobalDecl(*mi)); |
| 559 | m = llvm::ConstantExpr::getBitCast(m, Ptr8Ty); |
| 560 | methods.push_back(m); |
| 561 | } |
| 562 | } |
| 563 | if (!isPrimary) |
| 564 | return; |
| 565 | |
| 566 | // And add the virtuals for the class to the primary vtable. |
| 567 | RD = Class; |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 568 | for (meth_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
| 569 | ++mi) { |
| 570 | if (mi->isVirtual()) { |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 571 | m = CGM.GetAddrOfFunction(GlobalDecl(*mi)); |
| 572 | m = llvm::ConstantExpr::getBitCast(m, Ptr8Ty); |
| 573 | methods.push_back(m); |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 578 | llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) { |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 579 | llvm::SmallString<256> OutName; |
| 580 | llvm::raw_svector_ostream Out(OutName); |
| 581 | QualType ClassTy; |
| 582 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 583 | // away const? mutable? |
| 584 | ClassTy = getContext().getTagDeclType(const_cast<CXXRecordDecl*>(RD)); |
| 585 | mangleCXXVtable(ClassTy, getContext(), Out); |
| 586 | const char *Name = OutName.c_str(); |
Mike Stump | 82b5696 | 2009-07-31 21:43:43 +0000 | [diff] [blame] | 587 | llvm::GlobalVariable::LinkageTypes linktype; |
| 588 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 589 | std::vector<llvm::Constant *> methods; |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 590 | typedef CXXRecordDecl::method_iterator meth_iter; |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 591 | llvm::Type *Ptr8Ty; |
| 592 | Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 593 | int64_t Offset = 0; |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame^] | 594 | llvm::Constant *rtti = GenerateRtti(RD); |
| 595 | |
| 596 | Offset += LLVMPointerWidth; |
| 597 | Offset += LLVMPointerWidth; |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 598 | |
| 599 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 600 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
Mike Stump | 104ffaa | 2009-08-04 21:58:42 +0000 | [diff] [blame] | 601 | |
Mike Stump | 70101ce | 2009-08-06 18:05:22 +0000 | [diff] [blame] | 602 | // The primary base comes first. |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame^] | 603 | GenerateVtableForBase(PrimaryBase, RD, rtti, methods, true); |
Mike Stump | 104ffaa | 2009-08-04 21:58:42 +0000 | [diff] [blame] | 604 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 605 | e = RD->bases_end(); i != e; ++i) { |
| 606 | if (i->isVirtual()) |
| 607 | continue; |
| 608 | const CXXRecordDecl *Base = |
| 609 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 610 | if (PrimaryBase != Base) { |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame^] | 611 | GenerateVtableForBase(Base, RD, rtti, methods); |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 612 | } |
| 613 | } |
Mike Stump | 70101ce | 2009-08-06 18:05:22 +0000 | [diff] [blame] | 614 | |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 615 | // FIXME: finish layout for virtual bases |
| 616 | // FIXME: audit indirect virtual bases |
| 617 | for (CXXRecordDecl::base_class_const_iterator i = RD->vbases_begin(), |
| 618 | e = RD->vbases_end(); i != e; ++i) { |
| 619 | const CXXRecordDecl *Base = |
| 620 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 621 | if (Base != PrimaryBase) |
Mike Stump | e1be2b1 | 2009-08-06 21:49:36 +0000 | [diff] [blame^] | 622 | GenerateVtableForBase(Base, RD, rtti, methods); |
Mike Stump | 8a12b56 | 2009-08-06 15:50:11 +0000 | [diff] [blame] | 623 | } |
Mike Stump | 104ffaa | 2009-08-04 21:58:42 +0000 | [diff] [blame] | 624 | |
Mike Stump | 82b5696 | 2009-07-31 21:43:43 +0000 | [diff] [blame] | 625 | llvm::Constant *C; |
| 626 | llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size()); |
| 627 | C = llvm::ConstantArray::get(type, methods); |
| 628 | llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true, |
| 629 | linktype, C, Name); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 630 | vtable = Builder.CreateBitCast(vtable, Ptr8Ty); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 631 | vtable = Builder.CreateGEP(vtable, |
| 632 | llvm::ConstantInt::get(llvm::Type::Int64Ty, |
Mike Stump | 6f37633 | 2009-08-05 22:37:18 +0000 | [diff] [blame] | 633 | Offset/8)); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 634 | return vtable; |
| 635 | } |
| 636 | |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 637 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 638 | /// base classes and non-static data members belonging to this constructor. |
| 639 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) { |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 640 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); |
Mike Stump | eb19fa9 | 2009-08-06 13:41:24 +0000 | [diff] [blame] | 641 | // FIXME: Add vbase initialization |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 642 | llvm::Value *LoadOfThis = 0; |
Fariborz Jahanian | 6d0bdaa | 2009-07-28 18:09:28 +0000 | [diff] [blame] | 643 | |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 644 | for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 645 | E = CD->init_end(); |
| 646 | B != E; ++B) { |
| 647 | CXXBaseOrMemberInitializer *Member = (*B); |
| 648 | if (Member->isBaseInitializer()) { |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 649 | LoadOfThis = LoadCXXThis(); |
Fariborz Jahanian | 6d0bdaa | 2009-07-28 18:09:28 +0000 | [diff] [blame] | 650 | Type *BaseType = Member->getBaseClass(); |
| 651 | CXXRecordDecl *BaseClassDecl = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 652 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 6d0bdaa | 2009-07-28 18:09:28 +0000 | [diff] [blame] | 653 | llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl, |
| 654 | BaseClassDecl); |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 655 | EmitCXXConstructorCall(Member->getConstructor(), |
| 656 | Ctor_Complete, V, |
| 657 | Member->const_arg_begin(), |
| 658 | Member->const_arg_end()); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 659 | } else { |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 660 | // non-static data member initilaizers. |
| 661 | FieldDecl *Field = Member->getMember(); |
| 662 | QualType FieldType = getContext().getCanonicalType((Field)->getType()); |
| 663 | assert(!getContext().getAsArrayType(FieldType) |
| 664 | && "FIXME. Field arrays initialization unsupported"); |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 665 | |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 666 | LoadOfThis = LoadCXXThis(); |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 667 | LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 668 | if (FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 669 | |
| 670 | assert(Member->getConstructor() && |
| 671 | "EmitCtorPrologue - no constructor to initialize member"); |
| 672 | EmitCXXConstructorCall(Member->getConstructor(), |
| 673 | Ctor_Complete, LHS.getAddress(), |
| 674 | Member->const_arg_begin(), |
| 675 | Member->const_arg_end()); |
| 676 | continue; |
| 677 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 678 | |
| 679 | assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only"); |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 680 | Expr *RhsExpr = *Member->arg_begin(); |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 681 | llvm::Value *RHS = EmitScalarExpr(RhsExpr, true); |
| 682 | if (LHS.isBitfield()) |
| 683 | EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, FieldType, 0); |
| 684 | else |
| 685 | EmitStoreThroughLValue(RValue::get(RHS), LHS, FieldType); |
| 686 | } |
| 687 | } |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 688 | |
| 689 | // Initialize the vtable pointer |
Mike Stump | b502d83 | 2009-08-05 22:59:44 +0000 | [diff] [blame] | 690 | if (ClassDecl->isDynamicClass()) { |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 691 | if (!LoadOfThis) |
| 692 | LoadOfThis = LoadCXXThis(); |
| 693 | llvm::Value *VtableField; |
| 694 | llvm::Type *Ptr8Ty, *PtrPtr8Ty; |
| 695 | Ptr8Ty = llvm::PointerType::get(llvm::Type::Int8Ty, 0); |
| 696 | PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0); |
| 697 | VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty); |
| 698 | llvm::Value *vtable = GenerateVtable(ClassDecl); |
| 699 | Builder.CreateStore(vtable, VtableField); |
| 700 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 701 | } |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 702 | |
| 703 | /// EmitDtorEpilogue - Emit all code that comes at the end of class's |
| 704 | /// destructor. This is to call destructors on members and base classes |
| 705 | /// in reverse order of their construction. |
| 706 | void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD) { |
| 707 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext()); |
| 708 | assert(!ClassDecl->isPolymorphic() && |
| 709 | "FIXME. polymorphic destruction not supported"); |
| 710 | (void)ClassDecl; // prevent warning. |
| 711 | |
| 712 | for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(), |
| 713 | *E = DD->destr_end(); B != E; ++B) { |
| 714 | uintptr_t BaseOrMember = (*B); |
| 715 | if (DD->isMemberToDestroy(BaseOrMember)) { |
| 716 | FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember); |
| 717 | QualType FieldType = getContext().getCanonicalType((FD)->getType()); |
| 718 | assert(!getContext().getAsArrayType(FieldType) |
| 719 | && "FIXME. Field arrays destruction unsupported"); |
| 720 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 721 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 722 | if (FieldClassDecl->hasTrivialDestructor()) |
| 723 | continue; |
| 724 | llvm::Value *LoadOfThis = LoadCXXThis(); |
| 725 | LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0); |
| 726 | EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 727 | Dtor_Complete, LHS.getAddress()); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 728 | } else { |
Fariborz Jahanian | 426cc38 | 2009-07-30 17:49:11 +0000 | [diff] [blame] | 729 | const RecordType *RT = |
| 730 | DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>(); |
| 731 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 732 | if (BaseClassDecl->hasTrivialDestructor()) |
| 733 | continue; |
| 734 | llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(), |
| 735 | ClassDecl,BaseClassDecl); |
| 736 | EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), |
| 737 | Dtor_Complete, V); |
| 738 | } |
| 739 | } |
| 740 | } |