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 = |
| 41 | new llvm::GlobalVariable(llvm::Type::Int64Ty, false, |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +0000 | [diff] [blame] | 42 | GV->getLinkage(), |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 43 | llvm::Constant::getNullValue(llvm::Type::Int64Ty), |
Anders Carlsson | 283a062 | 2009-04-13 18:03:33 +0000 | [diff] [blame] | 44 | GuardVName.c_str(), |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 45 | &CGM.getModule()); |
| 46 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 47 | // Load the first byte of the guard variable. |
Daniel Dunbar | 0096acf | 2009-02-25 19:24:29 +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. |
| 53 | llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::Int8Ty); |
| 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 | |
| 74 | Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::Int8Ty, 1), |
| 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(); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 109 | const llvm::Type *Ty = |
Anders Carlsson | e9918d2 | 2009-04-08 20:31:57 +0000 | [diff] [blame] | 110 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 111 | FPT->isVariadic()); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 112 | llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 113 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 114 | llvm::Value *This; |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 115 | |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 116 | if (ME->isArrow()) |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 117 | This = EmitScalarExpr(ME->getBase()); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 118 | else { |
| 119 | LValue BaseLV = EmitLValue(ME->getBase()); |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 120 | This = BaseLV.getAddress(); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 123 | return EmitCXXMemberCall(MD, Callee, This, |
| 124 | CE->arg_begin(), CE->arg_end()); |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 125 | } |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 126 | |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 127 | llvm::Value *CodeGenFunction::LoadCXXThis() { |
| 128 | assert(isa<CXXMethodDecl>(CurFuncDecl) && |
| 129 | "Must be in a C++ member function decl to load 'this'"); |
| 130 | assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() && |
| 131 | "Must be in a C++ member function decl to load 'this'"); |
| 132 | |
| 133 | // FIXME: What if we're inside a block? |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 134 | // ans: See how CodeGenFunction::LoadObjCSelf() uses |
| 135 | // CodeGenFunction::BlockForwardSelf() for how to do this. |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 136 | return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this"); |
| 137 | } |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 138 | |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 139 | void |
| 140 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 141 | CXXCtorType Type, |
| 142 | llvm::Value *This, |
| 143 | CallExpr::const_arg_iterator ArgBeg, |
| 144 | CallExpr::const_arg_iterator ArgEnd) { |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 145 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 146 | |
| 147 | EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 150 | void |
Anders Carlsson | 31ccf37 | 2009-05-03 17:47:16 +0000 | [diff] [blame] | 151 | CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest, |
| 152 | const CXXConstructExpr *E) { |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 153 | assert(Dest && "Must have a destination!"); |
| 154 | |
| 155 | const CXXRecordDecl *RD = |
| 156 | cast<CXXRecordDecl>(E->getType()->getAsRecordType()->getDecl()); |
| 157 | if (RD->hasTrivialConstructor()) |
| 158 | return; |
| 159 | |
| 160 | // Call the constructor. |
| 161 | EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest, |
| 162 | E->arg_begin(), E->arg_end()); |
| 163 | } |
| 164 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 165 | static bool canGenerateCXXstructor(const CXXRecordDecl *RD, |
| 166 | ASTContext &Context) { |
Anders Carlsson | 59d8e0f | 2009-04-15 21:02:13 +0000 | [diff] [blame] | 167 | // The class has base classes - we don't support that right now. |
| 168 | if (RD->getNumBases() > 0) |
| 169 | return false; |
| 170 | |
| 171 | for (CXXRecordDecl::field_iterator I = RD->field_begin(Context), |
| 172 | E = RD->field_end(Context); I != E; ++I) { |
| 173 | // We don't support ctors for fields that aren't POD. |
| 174 | if (!I->getType()->isPODType()) |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | return true; |
| 179 | } |
| 180 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 181 | void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 182 | if (!canGenerateCXXstructor(D->getParent(), getContext())) { |
Anders Carlsson | 59d8e0f | 2009-04-15 21:02:13 +0000 | [diff] [blame] | 183 | ErrorUnsupported(D, "C++ constructor", true); |
| 184 | return; |
| 185 | } |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 186 | |
Anders Carlsson | 2a131fb | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 187 | EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 188 | EmitGlobal(GlobalDecl(D, Ctor_Base)); |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 189 | } |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 190 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 191 | void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, |
| 192 | CXXCtorType Type) { |
| 193 | |
| 194 | llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type); |
| 195 | |
| 196 | CodeGenFunction(*this).GenerateCode(D, Fn); |
| 197 | |
| 198 | SetFunctionDefinitionAttributes(D, Fn); |
| 199 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 200 | } |
| 201 | |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 202 | llvm::Function * |
| 203 | CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, |
| 204 | CXXCtorType Type) { |
| 205 | const llvm::FunctionType *FTy = |
| 206 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); |
| 207 | |
| 208 | const char *Name = getMangledCXXCtorName(D, Type); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 209 | return cast<llvm::Function>( |
| 210 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 211 | } |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 212 | |
| 213 | const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, |
| 214 | CXXCtorType Type) { |
| 215 | llvm::SmallString<256> Name; |
| 216 | llvm::raw_svector_ostream Out(Name); |
| 217 | mangleCXXCtor(D, Type, Context, Out); |
| 218 | |
| 219 | Name += '\0'; |
| 220 | return UniqueMangledName(Name.begin(), Name.end()); |
| 221 | } |
| 222 | |
| 223 | void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { |
| 224 | if (!canGenerateCXXstructor(D->getParent(), getContext())) { |
| 225 | ErrorUnsupported(D, "C++ destructor", true); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | EmitCXXDestructor(D, Dtor_Complete); |
| 230 | EmitCXXDestructor(D, Dtor_Base); |
| 231 | } |
| 232 | |
| 233 | void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, |
| 234 | CXXDtorType Type) { |
| 235 | llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type); |
| 236 | |
| 237 | CodeGenFunction(*this).GenerateCode(D, Fn); |
| 238 | |
| 239 | SetFunctionDefinitionAttributes(D, Fn); |
| 240 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 241 | } |
| 242 | |
| 243 | llvm::Function * |
| 244 | CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, |
| 245 | CXXDtorType Type) { |
| 246 | const llvm::FunctionType *FTy = |
| 247 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); |
| 248 | |
| 249 | const char *Name = getMangledCXXDtorName(D, Type); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 250 | return cast<llvm::Function>( |
| 251 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, |
| 255 | CXXDtorType Type) { |
| 256 | llvm::SmallString<256> Name; |
| 257 | llvm::raw_svector_ostream Out(Name); |
| 258 | mangleCXXDtor(D, Type, Context, Out); |
| 259 | |
| 260 | Name += '\0'; |
| 261 | return UniqueMangledName(Name.begin(), Name.end()); |
| 262 | } |
| 263 | |