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" |
| 20 | #include "clang/AST/Decl.h" |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 27 | void |
| 28 | CodeGenFunction::GenerateStaticCXXBlockVarDeclInit(const VarDecl &D, |
| 29 | llvm::GlobalVariable *GV) { |
| 30 | // FIXME: This should use __cxa_guard_{acquire,release}? |
| 31 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 32 | assert(!getContext().getLangOptions().ThreadsafeStatics && |
| 33 | "thread safe statics are currently not supported!"); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 34 | |
Anders Carlsson | 283a062 | 2009-04-13 18:03:33 +0000 | [diff] [blame] | 35 | llvm::SmallString<256> GuardVName; |
| 36 | llvm::raw_svector_ostream GuardVOut(GuardVName); |
| 37 | mangleGuardVariable(&D, getContext(), GuardVOut); |
| 38 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 39 | // Create the guard variable. |
| 40 | llvm::GlobalValue *GuardV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 41 | new llvm::GlobalVariable(CGM.getModule(), llvm::Type::Int64Ty, false, |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 42 | GV->getLinkage(), |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 43 | VMContext.getNullValue(llvm::Type::Int64Ty), |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 44 | GuardVName.c_str()); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 45 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 46 | // Load the first byte of the guard variable. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 47 | const llvm::Type *PtrTy = VMContext.getPointerType(llvm::Type::Int8Ty, 0); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 48 | llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy), |
| 49 | "tmp"); |
| 50 | |
| 51 | // Compare it against 0. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 52 | llvm::Value *nullValue = VMContext.getNullValue(llvm::Type::Int8Ty); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 53 | llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool"); |
| 54 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 55 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 56 | llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 57 | |
| 58 | // If the guard variable is 0, jump to the initializer code. |
| 59 | Builder.CreateCondBr(ICmp, InitBlock, EndBlock); |
| 60 | |
| 61 | EmitBlock(InitBlock); |
| 62 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 63 | const Expr *Init = D.getInit(); |
| 64 | if (!hasAggregateLLVMType(Init->getType())) { |
| 65 | llvm::Value *V = EmitScalarExpr(Init); |
| 66 | Builder.CreateStore(V, GV, D.getType().isVolatileQualified()); |
| 67 | } else if (Init->getType()->isAnyComplexType()) { |
| 68 | EmitComplexExprIntoAddr(Init, GV, D.getType().isVolatileQualified()); |
| 69 | } else { |
| 70 | EmitAggExpr(Init, GV, D.getType().isVolatileQualified()); |
| 71 | } |
| 72 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 73 | Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::Int8Ty, 1), |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 74 | Builder.CreateBitCast(GuardV, PtrTy)); |
| 75 | |
| 76 | EmitBlock(EndBlock); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 79 | RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, |
| 80 | llvm::Value *Callee, |
| 81 | llvm::Value *This, |
| 82 | CallExpr::const_arg_iterator ArgBeg, |
| 83 | CallExpr::const_arg_iterator ArgEnd) { |
| 84 | assert(MD->isInstance() && |
| 85 | "Trying to emit a member call expr on a static method!"); |
| 86 | |
| 87 | const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); |
| 88 | |
| 89 | CallArgList Args; |
| 90 | |
| 91 | // Push the this ptr. |
| 92 | Args.push_back(std::make_pair(RValue::get(This), |
| 93 | MD->getThisType(getContext()))); |
| 94 | |
| 95 | // And the rest of the call args |
| 96 | EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); |
| 97 | |
| 98 | QualType ResultType = MD->getType()->getAsFunctionType()->getResultType(); |
| 99 | return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), |
| 100 | Callee, Args, MD); |
| 101 | } |
| 102 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 103 | RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) { |
| 104 | const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()); |
| 105 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 106 | |
Anders Carlsson | e9918d2 | 2009-04-08 20:31:57 +0000 | [diff] [blame] | 107 | const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 108 | const llvm::Type *Ty = |
Anders Carlsson | e9918d2 | 2009-04-08 20:31:57 +0000 | [diff] [blame] | 109 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 110 | FPT->isVariadic()); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 111 | llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 112 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 113 | llvm::Value *This; |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 114 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 115 | if (ME->isArrow()) |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 116 | This = EmitScalarExpr(ME->getBase()); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 117 | else { |
| 118 | LValue BaseLV = EmitLValue(ME->getBase()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 119 | This = BaseLV.getAddress(); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 122 | return EmitCXXMemberCall(MD, Callee, This, |
| 123 | CE->arg_begin(), CE->arg_end()); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 124 | } |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 125 | |
Anders Carlsson | 0f29463 | 2009-05-27 04:18:27 +0000 | [diff] [blame] | 126 | RValue |
| 127 | CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, |
| 128 | const CXXMethodDecl *MD) { |
| 129 | assert(MD->isInstance() && |
| 130 | "Trying to emit a member call expr on a static method!"); |
| 131 | |
| 132 | |
| 133 | const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); |
| 134 | const llvm::Type *Ty = |
| 135 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 136 | FPT->isVariadic()); |
| 137 | llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty); |
| 138 | |
| 139 | llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); |
| 140 | |
| 141 | return EmitCXXMemberCall(MD, Callee, This, |
| 142 | E->arg_begin() + 1, E->arg_end()); |
| 143 | } |
| 144 | |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 145 | llvm::Value *CodeGenFunction::LoadCXXThis() { |
| 146 | assert(isa<CXXMethodDecl>(CurFuncDecl) && |
| 147 | "Must be in a C++ member function decl to load 'this'"); |
| 148 | assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() && |
| 149 | "Must be in a C++ member function decl to load 'this'"); |
| 150 | |
| 151 | // FIXME: What if we're inside a block? |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 152 | // ans: See how CodeGenFunction::LoadObjCSelf() uses |
| 153 | // CodeGenFunction::BlockForwardSelf() for how to do this. |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 154 | return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this"); |
| 155 | } |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 156 | |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 157 | void |
| 158 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 159 | CXXCtorType Type, |
| 160 | llvm::Value *This, |
| 161 | CallExpr::const_arg_iterator ArgBeg, |
| 162 | CallExpr::const_arg_iterator ArgEnd) { |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 163 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 164 | |
| 165 | EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 168 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D, |
| 169 | CXXDtorType Type, |
| 170 | llvm::Value *This) { |
| 171 | llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type); |
| 172 | |
| 173 | EmitCXXMemberCall(D, Callee, This, 0, 0); |
| 174 | } |
| 175 | |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 176 | void |
Anders Carlsson | 31ccf37 | 2009-05-03 17:47:16 +0000 | [diff] [blame] | 177 | CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest, |
| 178 | const CXXConstructExpr *E) { |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 179 | assert(Dest && "Must have a destination!"); |
| 180 | |
| 181 | const CXXRecordDecl *RD = |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 182 | cast<CXXRecordDecl>(E->getType()->getAsRecordType()->getDecl()); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 183 | if (RD->hasTrivialConstructor()) |
| 184 | return; |
| 185 | |
| 186 | // Call the constructor. |
| 187 | EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest, |
| 188 | E->arg_begin(), E->arg_end()); |
| 189 | } |
| 190 | |
Anders Carlsson | a00703d | 2009-05-31 01:40:14 +0000 | [diff] [blame] | 191 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 192 | if (E->isArray()) { |
| 193 | ErrorUnsupported(E, "new[] expression"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 194 | return VMContext.getUndef(ConvertType(E->getType())); |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | QualType AllocType = E->getAllocatedType(); |
| 198 | FunctionDecl *NewFD = E->getOperatorNew(); |
| 199 | const FunctionProtoType *NewFTy = NewFD->getType()->getAsFunctionProtoType(); |
| 200 | |
| 201 | CallArgList NewArgs; |
| 202 | |
| 203 | // The allocation size is the first argument. |
| 204 | QualType SizeTy = getContext().getSizeType(); |
| 205 | llvm::Value *AllocSize = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 206 | llvm::ConstantInt::get(ConvertType(SizeTy), |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 207 | getContext().getTypeSize(AllocType) / 8); |
| 208 | |
| 209 | NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy)); |
| 210 | |
| 211 | // Emit the rest of the arguments. |
| 212 | // FIXME: Ideally, this should just use EmitCallArgs. |
| 213 | CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin(); |
| 214 | |
| 215 | // First, use the types from the function type. |
| 216 | // We start at 1 here because the first argument (the allocation size) |
| 217 | // has already been emitted. |
| 218 | for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) { |
| 219 | QualType ArgType = NewFTy->getArgType(i); |
| 220 | |
| 221 | assert(getContext().getCanonicalType(ArgType.getNonReferenceType()). |
| 222 | getTypePtr() == |
| 223 | getContext().getCanonicalType(NewArg->getType()).getTypePtr() && |
| 224 | "type mismatch in call argument!"); |
| 225 | |
| 226 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 227 | ArgType)); |
| 228 | |
| 229 | } |
| 230 | |
| 231 | // Either we've emitted all the call args, or we have a call to a |
| 232 | // variadic function. |
| 233 | assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) && |
| 234 | "Extra arguments in non-variadic function!"); |
| 235 | |
| 236 | // If we still have any arguments, emit them using the type of the argument. |
| 237 | for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end(); |
| 238 | NewArg != NewArgEnd; ++NewArg) { |
| 239 | QualType ArgType = NewArg->getType(); |
| 240 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 241 | ArgType)); |
| 242 | } |
| 243 | |
| 244 | // Emit the call to new. |
| 245 | RValue RV = |
| 246 | EmitCall(CGM.getTypes().getFunctionInfo(NewFTy->getResultType(), NewArgs), |
| 247 | CGM.GetAddrOfFunction(GlobalDecl(NewFD)), |
| 248 | NewArgs, NewFD); |
| 249 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 250 | // If an allocation function is declared with an empty exception specification |
| 251 | // it returns null to indicate failure to allocate storage. [expr.new]p13. |
| 252 | // (We don't need to check for null when there's no new initializer and |
| 253 | // we're allocating a POD type). |
| 254 | bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() && |
| 255 | !(AllocType->isPODType() && !E->hasInitializer()); |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 256 | |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 257 | llvm::BasicBlock *NewNull = 0; |
| 258 | llvm::BasicBlock *NewNotNull = 0; |
| 259 | llvm::BasicBlock *NewEnd = 0; |
| 260 | |
| 261 | llvm::Value *NewPtr = RV.getScalarVal(); |
| 262 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 263 | if (NullCheckResult) { |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 264 | NewNull = createBasicBlock("new.null"); |
| 265 | NewNotNull = createBasicBlock("new.notnull"); |
| 266 | NewEnd = createBasicBlock("new.end"); |
| 267 | |
| 268 | llvm::Value *IsNull = |
| 269 | Builder.CreateICmpEQ(NewPtr, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 270 | VMContext.getNullValue(NewPtr->getType()), |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 271 | "isnull"); |
| 272 | |
| 273 | Builder.CreateCondBr(IsNull, NewNull, NewNotNull); |
| 274 | EmitBlock(NewNotNull); |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 277 | NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType())); |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 278 | |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 279 | if (AllocType->isPODType()) { |
Anders Carlsson | 215bd20 | 2009-06-01 00:26:14 +0000 | [diff] [blame] | 280 | if (E->getNumConstructorArgs() > 0) { |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 281 | assert(E->getNumConstructorArgs() == 1 && |
| 282 | "Can only have one argument to initializer of POD type."); |
| 283 | |
| 284 | const Expr *Init = E->getConstructorArg(0); |
| 285 | |
Anders Carlsson | 3923e95 | 2009-05-31 21:07:58 +0000 | [diff] [blame] | 286 | if (!hasAggregateLLVMType(AllocType)) |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 287 | Builder.CreateStore(EmitScalarExpr(Init), NewPtr); |
Anders Carlsson | 3923e95 | 2009-05-31 21:07:58 +0000 | [diff] [blame] | 288 | else if (AllocType->isAnyComplexType()) |
| 289 | EmitComplexExprIntoAddr(Init, NewPtr, AllocType.isVolatileQualified()); |
Anders Carlsson | 627a3e5 | 2009-05-31 21:12:26 +0000 | [diff] [blame] | 290 | else |
| 291 | EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified()); |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 292 | } |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 293 | } else { |
| 294 | // Call the constructor. |
| 295 | CXXConstructorDecl *Ctor = E->getConstructor(); |
Anders Carlsson | 6d0ffad | 2009-05-31 20:56:36 +0000 | [diff] [blame] | 296 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 297 | EmitCXXConstructorCall(Ctor, Ctor_Complete, NewPtr, |
| 298 | E->constructor_arg_begin(), |
| 299 | E->constructor_arg_end()); |
Anders Carlsson | ed4e367 | 2009-05-31 20:21:44 +0000 | [diff] [blame] | 300 | } |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 301 | |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 302 | if (NullCheckResult) { |
| 303 | Builder.CreateBr(NewEnd); |
| 304 | EmitBlock(NewNull); |
| 305 | Builder.CreateBr(NewEnd); |
| 306 | EmitBlock(NewEnd); |
| 307 | |
| 308 | llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType()); |
| 309 | PHI->reserveOperandSpace(2); |
| 310 | PHI->addIncoming(NewPtr, NewNotNull); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 311 | PHI->addIncoming(VMContext.getNullValue(NewPtr->getType()), NewNull); |
Anders Carlsson | f110853 | 2009-06-01 00:05:16 +0000 | [diff] [blame] | 312 | |
| 313 | NewPtr = PHI; |
| 314 | } |
| 315 | |
Anders Carlsson | d3fd6ba | 2009-05-31 21:53:59 +0000 | [diff] [blame] | 316 | return NewPtr; |
Anders Carlsson | a00703d | 2009-05-31 01:40:14 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 319 | static bool canGenerateCXXstructor(const CXXRecordDecl *RD, |
| 320 | ASTContext &Context) { |
Anders Carlsson | 59d8e0f | 2009-04-15 21:02:13 +0000 | [diff] [blame] | 321 | // The class has base classes - we don't support that right now. |
| 322 | if (RD->getNumBases() > 0) |
| 323 | return false; |
| 324 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 325 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 326 | I != E; ++I) { |
Anders Carlsson | 59d8e0f | 2009-04-15 21:02:13 +0000 | [diff] [blame] | 327 | // We don't support ctors for fields that aren't POD. |
| 328 | if (!I->getType()->isPODType()) |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | return true; |
| 333 | } |
| 334 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 335 | void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 336 | if (!canGenerateCXXstructor(D->getParent(), getContext())) { |
Anders Carlsson | 59d8e0f | 2009-04-15 21:02:13 +0000 | [diff] [blame] | 337 | ErrorUnsupported(D, "C++ constructor", true); |
| 338 | return; |
| 339 | } |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 340 | |
Anders Carlsson | 2a131fb | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 341 | EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 342 | EmitGlobal(GlobalDecl(D, Ctor_Base)); |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 343 | } |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 344 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 345 | void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, |
| 346 | CXXCtorType Type) { |
| 347 | |
| 348 | llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type); |
| 349 | |
| 350 | CodeGenFunction(*this).GenerateCode(D, Fn); |
| 351 | |
| 352 | SetFunctionDefinitionAttributes(D, Fn); |
| 353 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 354 | } |
| 355 | |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 356 | llvm::Function * |
| 357 | CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, |
| 358 | CXXCtorType Type) { |
| 359 | const llvm::FunctionType *FTy = |
| 360 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); |
| 361 | |
| 362 | const char *Name = getMangledCXXCtorName(D, Type); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 363 | return cast<llvm::Function>( |
| 364 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 365 | } |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 366 | |
| 367 | const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, |
| 368 | CXXCtorType Type) { |
| 369 | llvm::SmallString<256> Name; |
| 370 | llvm::raw_svector_ostream Out(Name); |
| 371 | mangleCXXCtor(D, Type, Context, Out); |
| 372 | |
| 373 | Name += '\0'; |
| 374 | return UniqueMangledName(Name.begin(), Name.end()); |
| 375 | } |
| 376 | |
| 377 | void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { |
| 378 | if (!canGenerateCXXstructor(D->getParent(), getContext())) { |
| 379 | ErrorUnsupported(D, "C++ destructor", true); |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | EmitCXXDestructor(D, Dtor_Complete); |
| 384 | EmitCXXDestructor(D, Dtor_Base); |
| 385 | } |
| 386 | |
| 387 | void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, |
| 388 | CXXDtorType Type) { |
| 389 | llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type); |
| 390 | |
| 391 | CodeGenFunction(*this).GenerateCode(D, Fn); |
| 392 | |
| 393 | SetFunctionDefinitionAttributes(D, Fn); |
| 394 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 395 | } |
| 396 | |
| 397 | llvm::Function * |
| 398 | CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, |
| 399 | CXXDtorType Type) { |
| 400 | const llvm::FunctionType *FTy = |
| 401 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); |
| 402 | |
| 403 | const char *Name = getMangledCXXDtorName(D, Type); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 404 | return cast<llvm::Function>( |
| 405 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, |
| 409 | CXXDtorType Type) { |
| 410 | llvm::SmallString<256> Name; |
| 411 | llvm::raw_svector_ostream Out(Name); |
| 412 | mangleCXXDtor(D, Type, Context, Out); |
| 413 | |
| 414 | Name += '\0'; |
| 415 | return UniqueMangledName(Name.begin(), Name.end()); |
| 416 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 417 | |
| 418 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 419 | /// base classes and non-static data members belonging to this constructor. |
| 420 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) { |
| 421 | for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
| 422 | E = CD->init_end(); |
| 423 | B != E; ++B) { |
| 424 | CXXBaseOrMemberInitializer *Member = (*B); |
| 425 | if (Member->isBaseInitializer()) { |
| 426 | // FIXME. Added base initialilzers here. |
| 427 | assert(false && "FIXME. base initialization unsupported"); |
| 428 | } |
| 429 | else { |
| 430 | // non-static data member initilaizers. |
| 431 | FieldDecl *Field = Member->getMember(); |
| 432 | QualType FieldType = getContext().getCanonicalType((Field)->getType()); |
| 433 | assert(!getContext().getAsArrayType(FieldType) |
| 434 | && "FIXME. Field arrays initialization unsupported"); |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 435 | |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 436 | llvm::Value *LoadOfThis = LoadCXXThis(); |
| 437 | LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0); |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 438 | if (FieldType->getAsRecordType()) { |
| 439 | |
| 440 | assert(Member->getConstructor() && |
| 441 | "EmitCtorPrologue - no constructor to initialize member"); |
| 442 | EmitCXXConstructorCall(Member->getConstructor(), |
| 443 | Ctor_Complete, LHS.getAddress(), |
| 444 | Member->const_arg_begin(), |
| 445 | Member->const_arg_end()); |
| 446 | continue; |
| 447 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 448 | |
| 449 | assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only"); |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 450 | Expr *RhsExpr = *Member->arg_begin(); |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 451 | llvm::Value *RHS = EmitScalarExpr(RhsExpr, true); |
| 452 | if (LHS.isBitfield()) |
| 453 | EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, FieldType, 0); |
| 454 | else |
| 455 | EmitStoreThroughLValue(RValue::get(RHS), LHS, FieldType); |
| 456 | } |
| 457 | } |
| 458 | } |