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" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Decl.h" |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 25 | |
| 26 | // FIXME: Name mangling should be moved to a separate class. |
| 27 | |
| 28 | static void mangleDeclContextInternal(const DeclContext *D, std::string &S) |
| 29 | { |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 30 | // FIXME: Should ObjcMethodDecl have the TranslationUnitDecl as its parent? |
Eli Friedman | e8e3205 | 2008-12-16 20:06:41 +0000 | [diff] [blame] | 31 | assert((!D->getParent() || isa<TranslationUnitDecl>(D->getParent())) && |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 32 | "Only one level of decl context mangling is currently supported!"); |
| 33 | |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 34 | if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 35 | S += llvm::utostr(FD->getIdentifier()->getLength()); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 36 | S += FD->getIdentifier()->getName(); |
| 37 | |
| 38 | if (FD->param_size() == 0) |
| 39 | S += 'v'; |
| 40 | else |
| 41 | assert(0 && "mangling of types not supported yet!"); |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 42 | } else if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 43 | |
| 44 | // FIXME: This should really use GetNameForMethod from CGObjCMac. |
| 45 | std::string Name; |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 46 | Name += MD->isInstanceMethod() ? '-' : '+'; |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 47 | Name += '['; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 48 | Name += MD->getClassInterface()->getNameAsString(); |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 49 | Name += ' '; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 50 | Name += MD->getSelector().getAsString(); |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 51 | Name += ']'; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 52 | S += llvm::utostr(Name.length()); |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 53 | S += Name; |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 54 | } else |
| 55 | assert(0 && "Unsupported decl type!"); |
| 56 | } |
| 57 | |
| 58 | static void mangleVarDeclInternal(const VarDecl &D, std::string &S) |
| 59 | { |
| 60 | S += 'Z'; |
| 61 | mangleDeclContextInternal(D.getDeclContext(), S); |
| 62 | S += 'E'; |
| 63 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 64 | S += llvm::utostr(D.getIdentifier()->getLength()); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 65 | S += D.getIdentifier()->getName(); |
| 66 | } |
| 67 | |
| 68 | static std::string mangleVarDecl(const VarDecl& D) |
| 69 | { |
| 70 | std::string S = "_Z"; |
| 71 | |
| 72 | mangleVarDeclInternal(D, S); |
| 73 | |
| 74 | return S; |
| 75 | } |
| 76 | |
| 77 | static std::string mangleGuardVariable(const VarDecl& D) |
| 78 | { |
| 79 | std::string S = "_ZGV"; |
| 80 | |
| 81 | mangleVarDeclInternal(D, S); |
| 82 | |
| 83 | return S; |
| 84 | } |
| 85 | |
| 86 | llvm::GlobalValue * |
| 87 | CodeGenFunction::GenerateStaticCXXBlockVarDecl(const VarDecl &D) |
| 88 | { |
| 89 | assert(!getContext().getLangOptions().ThreadsafeStatics && |
| 90 | "thread safe statics are currently not supported!"); |
| 91 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType()); |
| 92 | |
| 93 | // FIXME: If the function is inline, the linkage should be weak. |
| 94 | llvm::GlobalValue::LinkageTypes linkage = llvm::GlobalValue::InternalLinkage; |
| 95 | |
| 96 | // Create the guard variable. |
| 97 | llvm::GlobalValue *GuardV = |
| 98 | new llvm::GlobalVariable(llvm::Type::Int64Ty, false, |
| 99 | linkage, |
| 100 | llvm::Constant::getNullValue(llvm::Type::Int64Ty), |
| 101 | mangleGuardVariable(D), |
| 102 | &CGM.getModule()); |
| 103 | |
| 104 | // FIXME: Address space. |
| 105 | const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::Int8Ty, 0); |
| 106 | |
| 107 | // Load the first byte of the guard variable. |
| 108 | llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy), |
| 109 | "tmp"); |
| 110 | |
| 111 | // Compare it against 0. |
| 112 | llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::Int8Ty); |
| 113 | llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool"); |
| 114 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 115 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 116 | llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 117 | |
| 118 | // If the guard variable is 0, jump to the initializer code. |
| 119 | Builder.CreateCondBr(ICmp, InitBlock, EndBlock); |
| 120 | |
| 121 | EmitBlock(InitBlock); |
| 122 | |
| 123 | llvm::GlobalValue *GV = |
| 124 | new llvm::GlobalVariable(LTy, false, |
| 125 | llvm::GlobalValue::InternalLinkage, |
| 126 | llvm::Constant::getNullValue(LTy), |
| 127 | mangleVarDecl(D), |
| 128 | &CGM.getModule(), 0, |
| 129 | D.getType().getAddressSpace()); |
| 130 | |
| 131 | const Expr *Init = D.getInit(); |
| 132 | if (!hasAggregateLLVMType(Init->getType())) { |
| 133 | llvm::Value *V = EmitScalarExpr(Init); |
| 134 | Builder.CreateStore(V, GV, D.getType().isVolatileQualified()); |
| 135 | } else if (Init->getType()->isAnyComplexType()) { |
| 136 | EmitComplexExprIntoAddr(Init, GV, D.getType().isVolatileQualified()); |
| 137 | } else { |
| 138 | EmitAggExpr(Init, GV, D.getType().isVolatileQualified()); |
| 139 | } |
| 140 | |
| 141 | Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::Int8Ty, 1), |
| 142 | Builder.CreateBitCast(GuardV, PtrTy)); |
| 143 | |
| 144 | EmitBlock(EndBlock); |
| 145 | return GV; |
| 146 | } |
| 147 | |