Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-module state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenModule.h" |
Chris Lattner | 984fac5 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 15 | #include "CGDebugInfo.h" |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 16 | #include "CodeGenFunction.h" |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 17 | #include "CGCall.h" |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Douglas Gregor | 5fec5b0 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 19 | #include "Mangle.h" |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 20 | #include "TargetInfo.h" |
Chandler Carruth | bc55fe2 | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 21 | #include "clang/CodeGen/CodeGenOptions.h" |
Chris Lattner | 2ccb73b | 2007-06-16 00:16:26 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | b8c18fa | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 25 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Builtins.h" |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 27 | #include "clang/Basic/Diagnostic.h" |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 28 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 29cae66 | 2009-04-01 15:50:34 +0000 | [diff] [blame] | 30 | #include "clang/Basic/ConvertUTF.h" |
Nate Begeman | aca747a | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 31 | #include "llvm/CallingConv.h" |
Chris Lattner | 1eec660 | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 32 | #include "llvm/Module.h" |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 33 | #include "llvm/Intrinsics.h" |
Chris Lattner | 5e124bf | 2009-12-28 21:44:41 +0000 | [diff] [blame] | 34 | #include "llvm/LLVMContext.h" |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 15275e5 | 2009-11-07 09:22:46 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 37 | using namespace clang; |
| 38 | using namespace CodeGen; |
| 39 | |
| 40 | |
Chandler Carruth | bc55fe2 | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 41 | CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO, |
Chris Lattner | c8dbe1e | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 42 | llvm::Module &M, const llvm::TargetData &TD, |
Chris Lattner | 984fac5 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 43 | Diagnostic &diags) |
| 44 | : BlockModule(C, M, TD, Types, *this), Context(C), |
Chandler Carruth | bc55fe2 | 2009-11-12 17:24:48 +0000 | [diff] [blame] | 45 | Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M), |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 46 | TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags), |
| 47 | Types(C, M, TD, getTargetCodeGenInfo().getABIInfo()), |
| 48 | MangleCtx(C), VtableInfo(*this), Runtime(0), |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 49 | MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0), |
| 50 | VMContext(M.getContext()) { |
Daniel Dunbar | 8d48059 | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 3637652 | 2009-03-21 07:12:05 +0000 | [diff] [blame] | 52 | if (!Features.ObjC1) |
| 53 | Runtime = 0; |
| 54 | else if (!Features.NeXTRuntime) |
| 55 | Runtime = CreateGNUObjCRuntime(*this); |
| 56 | else if (Features.ObjCNonFragileABI) |
| 57 | Runtime = CreateMacNonFragileABIObjCRuntime(*this); |
| 58 | else |
| 59 | Runtime = CreateMacObjCRuntime(*this); |
Sanjiv Gupta | 15cb669 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 60 | |
| 61 | // If debug info generation is enabled, create the CGDebugInfo object. |
Anders Carlsson | 3efc6e6 | 2009-12-06 18:00:51 +0000 | [diff] [blame] | 62 | DebugInfo = CodeGenOpts.DebugInfo ? new CGDebugInfo(*this) : 0; |
Chris Lattner | a087ff9 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | CodeGenModule::~CodeGenModule() { |
Ted Kremenek | 2c674f6 | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 66 | delete Runtime; |
| 67 | delete DebugInfo; |
| 68 | } |
| 69 | |
| 70 | void CodeGenModule::Release() { |
Chris Lattner | a5ae54a | 2009-03-22 21:21:57 +0000 | [diff] [blame] | 71 | EmitDeferred(); |
Eli Friedman | 5866fe3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 72 | EmitCXXGlobalInitFunc(); |
Daniel Dunbar | 8d48059 | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 73 | if (Runtime) |
| 74 | if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) |
| 75 | AddGlobalCtor(ObjCInitFunction); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 76 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 77 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Nate Begeman | 7fab578 | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 78 | EmitAnnotations(); |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 79 | EmitLLVMUsed(); |
Daniel Dunbar | 23fd462 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 82 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 83 | /// specified stmt yet. |
Daniel Dunbar | f2cf6d1 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 84 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, |
| 85 | bool OmitOnError) { |
| 86 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 87 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Daniel Dunbar | fe2fb0a | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 89 | "cannot compile this %0 yet"); |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 90 | std::string Msg = Type; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 91 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) |
| 92 | << Msg << S->getSourceRange(); |
Chris Lattner | d45aa2a | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 93 | } |
Chris Lattner | 41af818 | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 94 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 95 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 96 | /// specified decl yet. |
Daniel Dunbar | f2cf6d1 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 97 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, |
| 98 | bool OmitOnError) { |
| 99 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 100 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Daniel Dunbar | fe2fb0a | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 102 | "cannot compile this %0 yet"); |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 103 | std::string Msg = Type; |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 104 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | LangOptions::VisibilityMode |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 108 | CodeGenModule::getDeclVisibilityMode(const Decl *D) const { |
| 109 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 110 | if (VD->getStorageClass() == VarDecl::PrivateExtern) |
| 111 | return LangOptions::Hidden; |
| 112 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 113 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) { |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 114 | switch (attr->getVisibility()) { |
| 115 | default: assert(0 && "Unknown visibility!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | case VisibilityAttr::DefaultVisibility: |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 117 | return LangOptions::Default; |
| 118 | case VisibilityAttr::HiddenVisibility: |
| 119 | return LangOptions::Hidden; |
| 120 | case VisibilityAttr::ProtectedVisibility: |
| 121 | return LangOptions::Protected; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return getLangOptions().getVisibilityMode(); |
| 126 | } |
| 127 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 129 | const Decl *D) const { |
| 130 | // Internal definitions always have default visibility. |
Chris Lattner | 6a0f907 | 2009-04-14 05:27:13 +0000 | [diff] [blame] | 131 | if (GV->hasLocalLinkage()) { |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 132 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 133 | return; |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 134 | } |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 135 | |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 136 | switch (getDeclVisibilityMode(D)) { |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 137 | default: assert(0 && "Unknown visibility!"); |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 138 | case LangOptions::Default: |
| 139 | return GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 140 | case LangOptions::Hidden: |
| 141 | return GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 142 | case LangOptions::Protected: |
| 143 | return GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
Dan Gohman | 75d69da | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Anders Carlsson | dae1abc | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 147 | const char *CodeGenModule::getMangledName(const GlobalDecl &GD) { |
Anders Carlsson | 38988d7 | 2009-09-10 23:38:47 +0000 | [diff] [blame] | 148 | const NamedDecl *ND = cast<NamedDecl>(GD.getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Anders Carlsson | dae1abc | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 150 | if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) |
| 151 | return getMangledCXXCtorName(D, GD.getCtorType()); |
| 152 | if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) |
| 153 | return getMangledCXXDtorName(D, GD.getDtorType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Anders Carlsson | dae1abc | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 155 | return getMangledName(ND); |
| 156 | } |
| 157 | |
Douglas Gregor | 5fec5b0 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 158 | /// \brief Retrieves the mangled name for the given declaration. |
| 159 | /// |
| 160 | /// If the given declaration requires a mangled name, returns an |
Chris Lattner | 725a26b | 2009-03-21 06:31:09 +0000 | [diff] [blame] | 161 | /// const char* containing the mangled name. Otherwise, returns |
| 162 | /// the unmangled name. |
Douglas Gregor | 5fec5b0 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 163 | /// |
Douglas Gregor | 5f361c9 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 164 | const char *CodeGenModule::getMangledName(const NamedDecl *ND) { |
Daniel Dunbar | e949e6c | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 165 | if (!getMangleContext().shouldMangleDeclName(ND)) { |
Chris Lattner | 725a26b | 2009-03-21 06:31:09 +0000 | [diff] [blame] | 166 | assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); |
Chris Lattner | 3637652 | 2009-03-21 07:12:05 +0000 | [diff] [blame] | 167 | return ND->getNameAsCString(); |
Chris Lattner | 725a26b | 2009-03-21 06:31:09 +0000 | [diff] [blame] | 168 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Douglas Gregor | 5f361c9 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 170 | llvm::SmallString<256> Name; |
Daniel Dunbar | e949e6c | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 171 | getMangleContext().mangleName(ND, Name); |
Douglas Gregor | 5f361c9 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 172 | Name += '\0'; |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 173 | return UniqueMangledName(Name.begin(), Name.end()); |
| 174 | } |
| 175 | |
| 176 | const char *CodeGenModule::UniqueMangledName(const char *NameStart, |
| 177 | const char *NameEnd) { |
| 178 | assert(*(NameEnd - 1) == '\0' && "Mangled name must be null terminated!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 180 | return MangledNames.GetOrCreateValue(NameStart, NameEnd).getKeyData(); |
Douglas Gregor | 5fec5b0 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 183 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 184 | /// main() runs. |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 185 | void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 186 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 187 | GlobalCtors.push_back(std::make_pair(Ctor, Priority)); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 190 | /// AddGlobalDtor - Add a function to the list that will be called |
| 191 | /// when the module is unloaded. |
| 192 | void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 193 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 194 | GlobalDtors.push_back(std::make_pair(Dtor, Priority)); |
| 195 | } |
| 196 | |
| 197 | void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { |
| 198 | // Ctor function type is void()*. |
| 199 | llvm::FunctionType* CtorFTy = |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 201 | std::vector<const llvm::Type*>(), |
| 202 | false); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 203 | llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 204 | |
| 205 | // Get the type of a ctor entry, { i32, void ()* }. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | llvm::StructType* CtorStructTy = |
| 207 | llvm::StructType::get(VMContext, llvm::Type::getInt32Ty(VMContext), |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 208 | llvm::PointerType::getUnqual(CtorFTy), NULL); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 209 | |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 210 | // Construct the constructor and destructor arrays. |
| 211 | std::vector<llvm::Constant*> Ctors; |
| 212 | for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
| 213 | std::vector<llvm::Constant*> S; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 215 | I->second, false)); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 216 | S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 217 | Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 218 | } |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 219 | |
| 220 | if (!Ctors.empty()) { |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 221 | llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 222 | new llvm::GlobalVariable(TheModule, AT, false, |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 223 | llvm::GlobalValue::AppendingLinkage, |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 224 | llvm::ConstantArray::get(AT, Ctors), |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 225 | GlobalName); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 226 | } |
Chris Lattner | 5ff2d5d | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Nate Begeman | 7fab578 | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 229 | void CodeGenModule::EmitAnnotations() { |
| 230 | if (Annotations.empty()) |
| 231 | return; |
| 232 | |
| 233 | // Create a new global variable for the ConstantStruct in the Module. |
| 234 | llvm::Constant *Array = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 235 | llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), |
Nate Begeman | 7fab578 | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 236 | Annotations.size()), |
| 237 | Annotations); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | llvm::GlobalValue *gv = |
| 239 | new llvm::GlobalVariable(TheModule, Array->getType(), false, |
| 240 | llvm::GlobalValue::AppendingLinkage, Array, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 241 | "llvm.global.annotations"); |
Nate Begeman | 7fab578 | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 242 | gv->setSection("llvm.metadata"); |
| 243 | } |
| 244 | |
Chris Lattner | 02e987f | 2009-04-14 16:44:36 +0000 | [diff] [blame] | 245 | static CodeGenModule::GVALinkage |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | GetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD, |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 247 | const LangOptions &Features) { |
John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 248 | // Everything located semantically within an anonymous namespace is |
| 249 | // always internal. |
| 250 | if (FD->isInAnonymousNamespace()) |
| 251 | return CodeGenModule::GVA_Internal; |
| 252 | |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 253 | // "static" functions get internal linkage. |
| 254 | if (FD->getStorageClass() == FunctionDecl::Static && !isa<CXXMethodDecl>(FD)) |
| 255 | return CodeGenModule::GVA_Internal; |
| 256 | |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 257 | // The kind of external linkage this function will have, if it is not |
| 258 | // inline or static. |
| 259 | CodeGenModule::GVALinkage External = CodeGenModule::GVA_StrongExternal; |
Anders Carlsson | 781161d | 2009-12-10 22:25:34 +0000 | [diff] [blame] | 260 | if (Context.getLangOptions().CPlusPlus) { |
| 261 | TemplateSpecializationKind TSK = FD->getTemplateSpecializationKind(); |
| 262 | |
| 263 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 264 | // If a function has been explicitly instantiated, then it should |
| 265 | // always have strong external linkage. |
| 266 | return CodeGenModule::GVA_StrongExternal; |
| 267 | } |
| 268 | |
| 269 | if (TSK == TSK_ImplicitInstantiation) |
| 270 | External = CodeGenModule::GVA_TemplateInstantiation; |
| 271 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 273 | if (!FD->isInlined()) |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 274 | return External; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 276 | if (!Features.CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
| 277 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 278 | // externally visible. |
| 279 | if (FD->isInlineDefinitionExternallyVisible()) |
| 280 | return External; |
| 281 | |
| 282 | // C99 inline semantics, where the symbol is not externally visible. |
| 283 | return CodeGenModule::GVA_C99Inline; |
Chris Lattner | f8dc073 | 2009-04-22 00:03:30 +0000 | [diff] [blame] | 284 | } |
Chris Lattner | bae0e68 | 2009-04-14 20:25:53 +0000 | [diff] [blame] | 285 | |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 286 | // C++0x [temp.explicit]p9: |
| 287 | // [ Note: The intent is that an inline function that is the subject of |
| 288 | // an explicit instantiation declaration will still be implicitly |
| 289 | // instantiated when used so that the body can be considered for |
| 290 | // inlining, but that no out-of-line copy of the inline function would be |
| 291 | // generated in the translation unit. -- end note ] |
| 292 | if (FD->getTemplateSpecializationKind() |
| 293 | == TSK_ExplicitInstantiationDeclaration) |
| 294 | return CodeGenModule::GVA_C99Inline; |
| 295 | |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 296 | return CodeGenModule::GVA_CXXInline; |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /// SetFunctionDefinitionAttributes - Set attributes for a global. |
Daniel Dunbar | 29eff88 | 2009-04-14 06:19:49 +0000 | [diff] [blame] | 300 | /// |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 301 | /// FIXME: This is currently only done for aliases and functions, but not for |
| 302 | /// variables (these details are set in EmitGlobalVarDefinition for variables). |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 303 | void CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D, |
| 304 | llvm::GlobalValue *GV) { |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 305 | GVALinkage Linkage = GetLinkageForFunction(getContext(), D, Features); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 306 | |
Daniel Dunbar | 81a3f1b | 2009-04-14 07:19:20 +0000 | [diff] [blame] | 307 | if (Linkage == GVA_Internal) { |
Chris Lattner | 73920ce | 2009-04-14 05:33:52 +0000 | [diff] [blame] | 308 | GV->setLinkage(llvm::Function::InternalLinkage); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 309 | } else if (D->hasAttr<DLLExportAttr>()) { |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 310 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 311 | } else if (D->hasAttr<WeakAttr>()) { |
Chris Lattner | 3e031f4 | 2009-04-14 06:04:17 +0000 | [diff] [blame] | 312 | GV->setLinkage(llvm::Function::WeakAnyLinkage); |
Chris Lattner | bae0e68 | 2009-04-14 20:25:53 +0000 | [diff] [blame] | 313 | } else if (Linkage == GVA_C99Inline) { |
| 314 | // In C99 mode, 'inline' functions are guaranteed to have a strong |
| 315 | // definition somewhere else, so we can use available_externally linkage. |
Chris Lattner | 256c950 | 2009-04-14 06:27:57 +0000 | [diff] [blame] | 316 | GV->setLinkage(llvm::Function::AvailableExternallyLinkage); |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 317 | } else if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) { |
Chris Lattner | 02e987f | 2009-04-14 16:44:36 +0000 | [diff] [blame] | 318 | // In C++, the compiler has to emit a definition in every translation unit |
| 319 | // that references the function. We should use linkonce_odr because |
| 320 | // a) if all references in this translation unit are optimized away, we |
| 321 | // don't need to codegen it. b) if the function persists, it needs to be |
| 322 | // merged with other definitions. c) C++ has the ODR, so we know the |
| 323 | // definition is dependable. |
| 324 | GV->setLinkage(llvm::Function::LinkOnceODRLinkage); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 325 | } else { |
Chris Lattner | bae0e68 | 2009-04-14 20:25:53 +0000 | [diff] [blame] | 326 | assert(Linkage == GVA_StrongExternal); |
| 327 | // Otherwise, we have strong external linkage. |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 328 | GV->setLinkage(llvm::Function::ExternalLinkage); |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 329 | } |
Nuno Lopes | b6f7953 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 330 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 331 | SetCommonAttributes(D, GV); |
Nuno Lopes | b6f7953 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Daniel Dunbar | aeddffc | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 334 | void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | const CGFunctionInfo &Info, |
Daniel Dunbar | aeddffc | 2009-04-14 07:08:30 +0000 | [diff] [blame] | 336 | llvm::Function *F) { |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 337 | unsigned CallingConv; |
Devang Patel | 322300d | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 338 | AttributeListType AttributeList; |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 339 | ConstructAttributeList(Info, D, AttributeList, CallingConv); |
Devang Patel | 322300d | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 340 | F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 341 | AttributeList.size())); |
| 342 | F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 345 | void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, |
| 346 | llvm::Function *F) { |
Daniel Dunbar | 0f3403c | 2009-03-02 04:58:03 +0000 | [diff] [blame] | 347 | if (!Features.Exceptions && !Features.ObjCNonFragileABI) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | F->addFnAttr(llvm::Attribute::NoUnwind); |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 349 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 350 | if (D->hasAttr<AlwaysInlineAttr>()) |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 351 | F->addFnAttr(llvm::Attribute::AlwaysInline); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Mike Stump | 3722f58 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 353 | if (D->hasAttr<NoInlineAttr>()) |
Anders Carlsson | f96954c | 2009-02-19 19:22:11 +0000 | [diff] [blame] | 354 | F->addFnAttr(llvm::Attribute::NoInline); |
Mike Stump | c5e153c | 2009-10-05 21:58:44 +0000 | [diff] [blame] | 355 | |
Anders Carlsson | 0d82fa6 | 2009-11-16 16:56:03 +0000 | [diff] [blame] | 356 | if (Features.getStackProtectorMode() == LangOptions::SSPOn) |
| 357 | F->addFnAttr(llvm::Attribute::StackProtect); |
| 358 | else if (Features.getStackProtectorMode() == LangOptions::SSPReq) |
| 359 | F->addFnAttr(llvm::Attribute::StackProtectReq); |
| 360 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 361 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) { |
| 362 | unsigned width = Context.Target.getCharWidth(); |
| 363 | F->setAlignment(AA->getAlignment() / width); |
| 364 | while ((AA = AA->getNext<AlignedAttr>())) |
| 365 | F->setAlignment(std::max(F->getAlignment(), AA->getAlignment() / width)); |
| 366 | } |
Mike Stump | 3472ae5 | 2009-10-05 22:49:20 +0000 | [diff] [blame] | 367 | // C++ ABI requires 2-byte alignment for member functions. |
Mike Stump | 916c006 | 2009-10-05 23:08:21 +0000 | [diff] [blame] | 368 | if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) |
| 369 | F->setAlignment(2); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | void CodeGenModule::SetCommonAttributes(const Decl *D, |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 373 | llvm::GlobalValue *GV) { |
| 374 | setGlobalVisibility(GV, D); |
| 375 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 376 | if (D->hasAttr<UsedAttr>()) |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 377 | AddUsedGlobal(GV); |
| 378 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 379 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 380 | GV->setSection(SA->getName()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 381 | |
| 382 | getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Daniel Dunbar | c3e7cff | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 385 | void CodeGenModule::SetInternalFunctionAttributes(const Decl *D, |
| 386 | llvm::Function *F, |
| 387 | const CGFunctionInfo &FI) { |
| 388 | SetLLVMFunctionAttributes(D, FI, F); |
| 389 | SetLLVMFunctionAttributesForDefinition(D, F); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 390 | |
| 391 | F->setLinkage(llvm::Function::InternalLinkage); |
| 392 | |
Daniel Dunbar | c3e7cff | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 393 | SetCommonAttributes(D, F); |
Daniel Dunbar | 449a339 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, |
Eli Friedman | 895771a | 2009-05-26 01:22:57 +0000 | [diff] [blame] | 397 | llvm::Function *F, |
| 398 | bool IsIncompleteFunction) { |
| 399 | if (!IsIncompleteFunction) |
| 400 | SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 402 | // Only a few attributes are set on declarations; these may later be |
| 403 | // overridden by a definition. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 405 | if (FD->hasAttr<DLLImportAttr>()) { |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 406 | F->setLinkage(llvm::Function::DLLImportLinkage); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | } else if (FD->hasAttr<WeakAttr>() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 408 | FD->hasAttr<WeakImportAttr>()) { |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 409 | // "extern_weak" is overloaded in LLVM; we probably should have |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | // separate linkage types for this. |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 411 | F->setLinkage(llvm::Function::ExternalWeakLinkage); |
| 412 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | F->setLinkage(llvm::Function::ExternalLinkage); |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 416 | if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 417 | F->setSection(SA->getName()); |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 420 | void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | assert(!GV->isDeclaration() && |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 422 | "Only globals with definition can force usage."); |
Chris Lattner | f41e87f | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 423 | LLVMUsed.push_back(GV); |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void CodeGenModule::EmitLLVMUsed() { |
| 427 | // Don't create llvm.used if there is no need. |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 428 | if (LLVMUsed.empty()) |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 429 | return; |
| 430 | |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 431 | const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Chris Lattner | f41e87f | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 433 | // Convert LLVMUsed to what ConstantArray needs. |
| 434 | std::vector<llvm::Constant*> UsedArray; |
| 435 | UsedArray.resize(LLVMUsed.size()); |
| 436 | for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | UsedArray[i] = |
| 438 | llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 439 | i8PTy); |
Chris Lattner | f41e87f | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 440 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Fariborz Jahanian | 248c719 | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 442 | if (UsedArray.empty()) |
| 443 | return; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 444 | llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | |
| 446 | llvm::GlobalVariable *GV = |
| 447 | new llvm::GlobalVariable(getModule(), ATy, false, |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 448 | llvm::GlobalValue::AppendingLinkage, |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 449 | llvm::ConstantArray::get(ATy, UsedArray), |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 450 | "llvm.used"); |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 451 | |
| 452 | GV->setSection("llvm.metadata"); |
| 453 | } |
| 454 | |
| 455 | void CodeGenModule::EmitDeferred() { |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 456 | // Emit code for any potentially referenced deferred decls. Since a |
| 457 | // previously unused static decl may become used during the generation of code |
| 458 | // for a static function, iterate until no changes are made. |
| 459 | while (!DeferredDeclsToEmit.empty()) { |
Anders Carlsson | dae1abc | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 460 | GlobalDecl D = DeferredDeclsToEmit.back(); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 461 | DeferredDeclsToEmit.pop_back(); |
| 462 | |
| 463 | // The mangled name for the decl must have been emitted in GlobalDeclMap. |
| 464 | // Look it up to see if it was defined with a stronger definition (e.g. an |
| 465 | // extern inline function with a strong function redefinition). If so, |
| 466 | // just ignore the deferred decl. |
| 467 | llvm::GlobalValue *CGRef = GlobalDeclMap[getMangledName(D)]; |
| 468 | assert(CGRef && "Deferred decl wasn't referenced?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 470 | if (!CGRef->isDeclaration()) |
| 471 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 473 | // Otherwise, emit the definition and move on to the next one. |
| 474 | EmitGlobalDefinition(D); |
| 475 | } |
Chris Lattner | bed3144 | 2007-05-28 01:07:47 +0000 | [diff] [blame] | 476 | } |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 477 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 479 | /// annotation information for a given GlobalValue. The annotation struct is |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the |
| 481 | /// GlobalValue being annotated. The second field is the constant string |
| 482 | /// created from the AnnotateAttr's annotation. The third field is a constant |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 483 | /// string containing the name of the translation unit. The fourth field is |
| 484 | /// the line number in the file of the annotated value declaration. |
| 485 | /// |
| 486 | /// FIXME: this does not unique the annotation string constants, as llvm-gcc |
| 487 | /// appears to. |
| 488 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 490 | const AnnotateAttr *AA, |
| 491 | unsigned LineNo) { |
| 492 | llvm::Module *M = &getModule(); |
| 493 | |
| 494 | // get [N x i8] constants for the annotation string, and the filename string |
| 495 | // which are the 2nd and 3rd elements of the global annotation structure. |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 496 | const llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | llvm::Constant *anno = llvm::ConstantArray::get(VMContext, |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 498 | AA->getAnnotation(), true); |
| 499 | llvm::Constant *unit = llvm::ConstantArray::get(VMContext, |
| 500 | M->getModuleIdentifier(), |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 501 | true); |
| 502 | |
| 503 | // Get the two global values corresponding to the ConstantArrays we just |
| 504 | // created to hold the bytes of the strings. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | llvm::GlobalValue *annoGV = |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 506 | new llvm::GlobalVariable(*M, anno->getType(), false, |
| 507 | llvm::GlobalValue::PrivateLinkage, anno, |
| 508 | GV->getName()); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 509 | // translation unit name string, emitted into the llvm.metadata section. |
| 510 | llvm::GlobalValue *unitGV = |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 511 | new llvm::GlobalVariable(*M, unit->getType(), false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | llvm::GlobalValue::PrivateLinkage, unit, |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 513 | ".str"); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 514 | |
Daniel Dunbar | 346892a | 2009-04-14 22:41:13 +0000 | [diff] [blame] | 515 | // Create the ConstantStruct for the global annotation. |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 516 | llvm::Constant *Fields[4] = { |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 517 | llvm::ConstantExpr::getBitCast(GV, SBP), |
| 518 | llvm::ConstantExpr::getBitCast(annoGV, SBP), |
| 519 | llvm::ConstantExpr::getBitCast(unitGV, SBP), |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 520 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo) |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 521 | }; |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 522 | return llvm::ConstantStruct::get(VMContext, Fields, 4, false); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 525 | bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { |
Daniel Dunbar | 128a138 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 526 | // Never defer when EmitAllDecls is specified or the decl has |
| 527 | // attribute used. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 528 | if (Features.EmitAllDecls || Global->hasAttr<UsedAttr>()) |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 529 | return false; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 530 | |
| 531 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 532 | // Constructors and destructors should never be deferred. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | if (FD->hasAttr<ConstructorAttr>() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 534 | FD->hasAttr<DestructorAttr>()) |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 535 | return false; |
| 536 | |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 537 | // The key function for a class must never be deferred. |
| 538 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Global)) { |
| 539 | const CXXRecordDecl *RD = MD->getParent(); |
| 540 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 541 | const CXXMethodDecl *KeyFunction = getContext().getKeyFunction(RD); |
Douglas Gregor | a318efd | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 542 | if (KeyFunction && |
| 543 | KeyFunction->getCanonicalDecl() == MD->getCanonicalDecl()) |
Eli Friedman | f2c79b6 | 2009-12-08 03:56:49 +0000 | [diff] [blame] | 544 | return false; |
| 545 | } |
| 546 | } |
| 547 | |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 548 | GVALinkage Linkage = GetLinkageForFunction(getContext(), FD, Features); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Chris Lattner | 92028da | 2009-04-14 06:44:48 +0000 | [diff] [blame] | 550 | // static, static inline, always_inline, and extern inline functions can |
Chris Lattner | 02e987f | 2009-04-14 16:44:36 +0000 | [diff] [blame] | 551 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
Chris Lattner | bae0e68 | 2009-04-14 20:25:53 +0000 | [diff] [blame] | 552 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
Eli Friedman | 2e06e8b | 2009-12-25 05:29:40 +0000 | [diff] [blame] | 553 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Chris Lattner | 92028da | 2009-04-14 06:44:48 +0000 | [diff] [blame] | 554 | return true; |
Chris Lattner | 92028da | 2009-04-14 06:44:48 +0000 | [diff] [blame] | 555 | return false; |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 556 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 92028da | 2009-04-14 06:44:48 +0000 | [diff] [blame] | 558 | const VarDecl *VD = cast<VarDecl>(Global); |
| 559 | assert(VD->isFileVarDecl() && "Invalid decl"); |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 560 | |
Anders Carlsson | a18ed9b | 2009-10-08 17:28:59 +0000 | [diff] [blame] | 561 | // We never want to defer structs that have non-trivial constructors or |
| 562 | // destructors. |
| 563 | |
| 564 | // FIXME: Handle references. |
| 565 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 566 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 567 | if (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()) |
| 568 | return false; |
| 569 | } |
| 570 | } |
| 571 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 572 | // Static data may be deferred, but out-of-line static data members |
| 573 | // cannot be. |
Fariborz Jahanian | 4127b8e | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 574 | if (VD->isInAnonymousNamespace()) |
| 575 | return true; |
Eli Friedman | 8a5f75e | 2009-11-26 02:52:12 +0000 | [diff] [blame] | 576 | if (VD->getLinkage() == VarDecl::InternalLinkage) { |
Fariborz Jahanian | 4127b8e | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 577 | // Initializer has side effects? |
| 578 | if (VD->getInit() && VD->getInit()->HasSideEffects(Context)) |
| 579 | return false; |
| 580 | return !(VD->isStaticDataMember() && VD->isOutOfLine()); |
| 581 | } |
| 582 | return false; |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 585 | void CodeGenModule::EmitGlobal(GlobalDecl GD) { |
Anders Carlsson | 38988d7 | 2009-09-10 23:38:47 +0000 | [diff] [blame] | 586 | const ValueDecl *Global = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 588 | // If this is an alias definition (which otherwise looks like a declaration) |
| 589 | // emit it now. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 590 | if (Global->hasAttr<AliasAttr>()) |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 591 | return EmitAliasDefinition(Global); |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 593 | // Ignore declarations, they will be emitted on their first use. |
Daniel Dunbar | 4e004ed | 2009-03-19 08:27:24 +0000 | [diff] [blame] | 594 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 595 | // Forward declarations are emitted lazily on first use. |
| 596 | if (!FD->isThisDeclarationADefinition()) |
| 597 | return; |
Daniel Dunbar | 08b26a0 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 598 | } else { |
| 599 | const VarDecl *VD = cast<VarDecl>(Global); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 600 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
| 601 | |
Anders Carlsson | a30e175 | 2009-12-04 23:50:01 +0000 | [diff] [blame] | 602 | if (getLangOptions().CPlusPlus && !VD->getInit()) { |
| 603 | // In C++, if this is marked "extern", defer code generation. |
| 604 | if (VD->getStorageClass() == VarDecl::Extern || VD->isExternC()) |
| 605 | return; |
| 606 | |
| 607 | // If this is a declaration of an explicit specialization of a static |
| 608 | // data member in a class template, don't emit it. |
| 609 | if (VD->isStaticDataMember() && |
| 610 | VD->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 611 | return; |
| 612 | } |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 613 | |
| 614 | // In C, if this isn't a definition, defer code generation. |
| 615 | if (!getLangOptions().CPlusPlus && !VD->getInit()) |
| 616 | return; |
Nate Begeman | 8e8d498 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 619 | // Defer code generation when possible if this is a static definition, inline |
| 620 | // function etc. These we only want to emit if they are used. |
Daniel Dunbar | 6b8720e | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 621 | if (MayDeferGeneration(Global)) { |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 622 | // If the value has already been used, add it directly to the |
| 623 | // DeferredDeclsToEmit list. |
Anders Carlsson | dae1abc | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 624 | const char *MangledName = getMangledName(GD); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 625 | if (GlobalDeclMap.count(MangledName)) |
Anders Carlsson | dae1abc | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 626 | DeferredDeclsToEmit.push_back(GD); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 627 | else { |
| 628 | // Otherwise, remember that we saw a deferred decl with this name. The |
| 629 | // first use of the mangled name will cause it to move into |
| 630 | // DeferredDeclsToEmit. |
Anders Carlsson | dae1abc | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 631 | DeferredDecls[MangledName] = GD; |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 632 | } |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 633 | return; |
| 634 | } |
| 635 | |
| 636 | // Otherwise emit the definition. |
Anders Carlsson | dae1abc | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 637 | EmitGlobalDefinition(GD); |
Nate Begeman | 8e8d498 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 640 | void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) { |
Anders Carlsson | 38988d7 | 2009-09-10 23:38:47 +0000 | [diff] [blame] | 641 | const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | |
Anders Carlsson | 29295bf | 2009-10-27 14:32:27 +0000 | [diff] [blame] | 643 | PrettyStackTraceDecl CrashInfo((ValueDecl *)D, D->getLocation(), |
| 644 | Context.getSourceManager(), |
| 645 | "Generating code for declaration"); |
| 646 | |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 647 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 648 | getVtableInfo().MaybeEmitVtable(GD); |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 649 | if (MD->isVirtual() && MD->isOutOfLine() && |
| 650 | (!isa<CXXDestructorDecl>(D) || GD.getDtorType() != Dtor_Base)) { |
Eli Friedman | 31bc3ad | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 651 | if (isa<CXXDestructorDecl>(D)) { |
| 652 | GlobalDecl CanonGD(cast<CXXDestructorDecl>(D->getCanonicalDecl()), |
| 653 | GD.getDtorType()); |
| 654 | BuildThunksForVirtual(CanonGD); |
| 655 | } else { |
| 656 | BuildThunksForVirtual(MD->getCanonicalDecl()); |
| 657 | } |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 658 | } |
| 659 | } |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 660 | |
Anders Carlsson | dae1abc | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 661 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) |
| 662 | EmitCXXConstructor(CD, GD.getCtorType()); |
Anders Carlsson | 0a63741 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 663 | else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) |
| 664 | EmitCXXDestructor(DD, GD.getDtorType()); |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 665 | else if (isa<FunctionDecl>(D)) |
| 666 | EmitGlobalFunctionDefinition(GD); |
Anders Carlsson | dae1abc | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 667 | else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 668 | EmitGlobalVarDefinition(VD); |
Anders Carlsson | dae1abc | 2009-05-05 04:44:02 +0000 | [diff] [blame] | 669 | else { |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 670 | assert(0 && "Invalid argument to EmitGlobalDefinition()"); |
| 671 | } |
| 672 | } |
| 673 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 674 | /// GetOrCreateLLVMFunction - If the specified mangled name is not in the |
| 675 | /// module, create and return an llvm Function with the specified type. If there |
| 676 | /// is something in the module with the specified name, return it potentially |
| 677 | /// bitcasted to the right type. |
| 678 | /// |
| 679 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 680 | /// to set the attributes on the function when it is first created. |
| 681 | llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName, |
| 682 | const llvm::Type *Ty, |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 683 | GlobalDecl D) { |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 684 | // Lookup the entry, lazily creating it if necessary. |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 685 | llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; |
| 686 | if (Entry) { |
| 687 | if (Entry->getType()->getElementType() == Ty) |
| 688 | return Entry; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 690 | // Make sure the result is of the correct type. |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 691 | const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 692 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 693 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | |
Eli Friedman | cc522d9 | 2009-11-09 05:07:37 +0000 | [diff] [blame] | 695 | // This function doesn't have a complete type (for example, the return |
| 696 | // type is an incomplete struct). Use a fake type instead, and make |
| 697 | // sure not to try to set attributes. |
| 698 | bool IsIncompleteFunction = false; |
| 699 | if (!isa<llvm::FunctionType>(Ty)) { |
| 700 | Ty = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 701 | std::vector<const llvm::Type*>(), false); |
| 702 | IsIncompleteFunction = true; |
| 703 | } |
| 704 | llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), |
| 705 | llvm::Function::ExternalLinkage, |
| 706 | "", &getModule()); |
| 707 | F->setName(MangledName); |
| 708 | if (D.getDecl()) |
| 709 | SetFunctionAttributes(cast<FunctionDecl>(D.getDecl()), F, |
| 710 | IsIncompleteFunction); |
| 711 | Entry = F; |
| 712 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 713 | // This is the first use or definition of a mangled name. If there is a |
| 714 | // deferred decl with this name, remember that we need to emit it at the end |
| 715 | // of the file. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | llvm::DenseMap<const char*, GlobalDecl>::iterator DDI = |
Chris Lattner | eb7466d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 717 | DeferredDecls.find(MangledName); |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 718 | if (DDI != DeferredDecls.end()) { |
| 719 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 720 | // list, and remove it from DeferredDecls (since we don't need it anymore). |
| 721 | DeferredDeclsToEmit.push_back(DDI->second); |
| 722 | DeferredDecls.erase(DDI); |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 723 | } else if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl())) { |
Chris Lattner | d035ebd | 2009-05-12 21:02:27 +0000 | [diff] [blame] | 724 | // If this the first reference to a C++ inline function in a class, queue up |
| 725 | // the deferred function body for emission. These are not seen as |
| 726 | // top-level declarations. |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 727 | if (FD->isThisDeclarationADefinition() && MayDeferGeneration(FD)) |
| 728 | DeferredDeclsToEmit.push_back(D); |
Fariborz Jahanian | 6f14c73 | 2009-07-30 23:22:00 +0000 | [diff] [blame] | 729 | // A called constructor which has no definition or declaration need be |
| 730 | // synthesized. |
| 731 | else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
Eli Friedman | 84a7e34 | 2009-11-26 07:40:08 +0000 | [diff] [blame] | 732 | if (CD->isImplicit()) |
| 733 | DeferredDeclsToEmit.push_back(D); |
| 734 | } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) { |
| 735 | if (DD->isImplicit()) |
| 736 | DeferredDeclsToEmit.push_back(D); |
| 737 | } else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 738 | if (MD->isCopyAssignment() && MD->isImplicit()) |
Fariborz Jahanian | 6f14c73 | 2009-07-30 23:22:00 +0000 | [diff] [blame] | 739 | DeferredDeclsToEmit.push_back(D); |
| 740 | } |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 741 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 743 | return F; |
| 744 | } |
| 745 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 746 | /// GetAddrOfFunction - Return the address of the given function. If Ty is |
| 747 | /// non-null, then this function will use the specified type if it has to |
| 748 | /// create it (this occurs when we see a definition of the function). |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 749 | llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 750 | const llvm::Type *Ty) { |
| 751 | // If there was no specific requested type, just convert it now. |
| 752 | if (!Ty) |
Anders Carlsson | 38988d7 | 2009-09-10 23:38:47 +0000 | [diff] [blame] | 753 | Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType()); |
Douglas Gregor | d94105a | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 754 | return GetOrCreateLLVMFunction(getMangledName(GD), Ty, GD); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 755 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 756 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 757 | /// CreateRuntimeFunction - Create a new runtime function with the specified |
| 758 | /// type and name. |
| 759 | llvm::Constant * |
| 760 | CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, |
| 761 | const char *Name) { |
| 762 | // Convert Name to be a uniqued string from the IdentifierInfo table. |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 763 | Name = getContext().Idents.get(Name).getNameStart(); |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 764 | return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl()); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 765 | } |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 766 | |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 767 | static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D) { |
| 768 | if (!D->getType().isConstant(Context)) |
| 769 | return false; |
| 770 | if (Context.getLangOptions().CPlusPlus && |
| 771 | Context.getBaseElementType(D->getType())->getAs<RecordType>()) { |
| 772 | // FIXME: We should do something fancier here! |
| 773 | return false; |
| 774 | } |
| 775 | return true; |
| 776 | } |
| 777 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 778 | /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, |
| 779 | /// create and return an llvm GlobalVariable with the specified type. If there |
| 780 | /// is something in the module with the specified name, return it potentially |
| 781 | /// bitcasted to the right type. |
| 782 | /// |
| 783 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 784 | /// to set the attributes on the global when it is first created. |
| 785 | llvm::Constant *CodeGenModule::GetOrCreateLLVMGlobal(const char *MangledName, |
| 786 | const llvm::PointerType*Ty, |
| 787 | const VarDecl *D) { |
Daniel Dunbar | 829e988 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 788 | // Lookup the entry, lazily creating it if necessary. |
Chris Lattner | a9cb626 | 2009-03-21 08:06:59 +0000 | [diff] [blame] | 789 | llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 790 | if (Entry) { |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 791 | if (Entry->getType() == Ty) |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 792 | return Entry; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 794 | // Make sure the result is of the correct type. |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 795 | return llvm::ConstantExpr::getBitCast(Entry, Ty); |
Daniel Dunbar | dec798b | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 796 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 798 | // This is the first use or definition of a mangled name. If there is a |
| 799 | // deferred decl with this name, remember that we need to emit it at the end |
| 800 | // of the file. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | llvm::DenseMap<const char*, GlobalDecl>::iterator DDI = |
Chris Lattner | 4547094 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 802 | DeferredDecls.find(MangledName); |
| 803 | if (DDI != DeferredDecls.end()) { |
| 804 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 805 | // list, and remove it from DeferredDecls (since we don't need it anymore). |
| 806 | DeferredDeclsToEmit.push_back(DDI->second); |
| 807 | DeferredDecls.erase(DDI); |
| 808 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | |
| 810 | llvm::GlobalVariable *GV = |
| 811 | new llvm::GlobalVariable(getModule(), Ty->getElementType(), false, |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 812 | llvm::GlobalValue::ExternalLinkage, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | 0, "", 0, |
Eli Friedman | 4f85674 | 2009-04-19 21:05:03 +0000 | [diff] [blame] | 814 | false, Ty->getAddressSpace()); |
Chris Lattner | 4ff71de | 2009-03-22 00:12:30 +0000 | [diff] [blame] | 815 | GV->setName(MangledName); |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 816 | |
| 817 | // Handle things which are present even on external declarations. |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 818 | if (D) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 819 | // FIXME: This code is overly simple and should be merged with other global |
| 820 | // handling. |
Eli Friedman | b095e15 | 2009-12-11 21:23:03 +0000 | [diff] [blame] | 821 | GV->setConstant(DeclIsConstantGlobal(Context, D)); |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 822 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 823 | // FIXME: Merge with other attribute handling code. |
| 824 | if (D->getStorageClass() == VarDecl::PrivateExtern) |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 825 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 826 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | if (D->hasAttr<WeakAttr>() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 828 | D->hasAttr<WeakImportAttr>()) |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 829 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Eli Friedman | 4f85674 | 2009-04-19 21:05:03 +0000 | [diff] [blame] | 830 | |
| 831 | GV->setThreadLocal(D->isThreadSpecified()); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 832 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 833 | |
Chris Lattner | 3bfce18 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 834 | return Entry = GV; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 837 | |
| 838 | /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the |
| 839 | /// given global variable. If Ty is non-null and if the global doesn't exist, |
| 840 | /// then it will be greated with the specified type instead of whatever the |
| 841 | /// normal requested type would be. |
| 842 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
| 843 | const llvm::Type *Ty) { |
| 844 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 845 | QualType ASTTy = D->getType(); |
| 846 | if (Ty == 0) |
| 847 | Ty = getTypes().ConvertTypeForMem(ASTTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 848 | |
| 849 | const llvm::PointerType *PTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 850 | llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 851 | return GetOrCreateLLVMGlobal(getMangledName(D), PTy, D); |
| 852 | } |
| 853 | |
| 854 | /// CreateRuntimeVariable - Create a new runtime global variable with the |
| 855 | /// specified type and name. |
| 856 | llvm::Constant * |
| 857 | CodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, |
| 858 | const char *Name) { |
| 859 | // Convert Name to be a uniqued string from the IdentifierInfo table. |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 860 | Name = getContext().Idents.get(Name).getNameStart(); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 861 | return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 864 | void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { |
| 865 | assert(!D->getInit() && "Cannot emit definite definitions here!"); |
| 866 | |
Douglas Gregor | fa9ab53 | 2009-04-21 19:28:58 +0000 | [diff] [blame] | 867 | if (MayDeferGeneration(D)) { |
| 868 | // If we have not seen a reference to this variable yet, place it |
| 869 | // into the deferred declarations table to be emitted if needed |
| 870 | // later. |
| 871 | const char *MangledName = getMangledName(D); |
| 872 | if (GlobalDeclMap.count(MangledName) == 0) { |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 873 | DeferredDecls[MangledName] = D; |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 874 | return; |
Douglas Gregor | fa9ab53 | 2009-04-21 19:28:58 +0000 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
| 878 | // The tentative definition is the only definition. |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 879 | EmitGlobalVarDefinition(D); |
| 880 | } |
| 881 | |
Douglas Gregor | ccecc1b | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 882 | llvm::GlobalVariable::LinkageTypes |
| 883 | CodeGenModule::getVtableLinkage(const CXXRecordDecl *RD) { |
Douglas Gregor | 2a34df3 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 884 | if (RD->isInAnonymousNamespace() || !RD->hasLinkage()) |
| 885 | return llvm::GlobalVariable::InternalLinkage; |
| 886 | |
| 887 | if (const CXXMethodDecl *KeyFunction |
| 888 | = RD->getASTContext().getKeyFunction(RD)) { |
| 889 | // If this class has a key function, use that to determine the linkage of |
| 890 | // the vtable. |
Douglas Gregor | ccecc1b | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 891 | const FunctionDecl *Def = 0; |
| 892 | if (KeyFunction->getBody(Def)) |
| 893 | KeyFunction = cast<CXXMethodDecl>(Def); |
Douglas Gregor | 2a34df3 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 894 | |
Douglas Gregor | ccecc1b | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 895 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 896 | case TSK_Undeclared: |
| 897 | case TSK_ExplicitSpecialization: |
| 898 | if (KeyFunction->isInlined()) |
| 899 | return llvm::GlobalVariable::WeakODRLinkage; |
| 900 | |
| 901 | return llvm::GlobalVariable::ExternalLinkage; |
| 902 | |
| 903 | case TSK_ImplicitInstantiation: |
| 904 | case TSK_ExplicitInstantiationDefinition: |
| 905 | return llvm::GlobalVariable::WeakODRLinkage; |
| 906 | |
| 907 | case TSK_ExplicitInstantiationDeclaration: |
| 908 | // FIXME: Use available_externally linkage. However, this currently |
| 909 | // breaks LLVM's build due to undefined symbols. |
| 910 | // return llvm::GlobalVariable::AvailableExternallyLinkage; |
| 911 | return llvm::GlobalVariable::WeakODRLinkage; |
| 912 | } |
Douglas Gregor | 2a34df3 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | switch (RD->getTemplateSpecializationKind()) { |
| 916 | case TSK_Undeclared: |
| 917 | case TSK_ExplicitSpecialization: |
| 918 | case TSK_ImplicitInstantiation: |
| 919 | case TSK_ExplicitInstantiationDefinition: |
Douglas Gregor | ccecc1b | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 920 | return llvm::GlobalVariable::WeakODRLinkage; |
Douglas Gregor | 2a34df3 | 2010-01-06 22:00:56 +0000 | [diff] [blame] | 921 | |
| 922 | case TSK_ExplicitInstantiationDeclaration: |
| 923 | // FIXME: Use available_externally linkage. However, this currently |
| 924 | // breaks LLVM's build due to undefined symbols. |
| 925 | // return llvm::GlobalVariable::AvailableExternallyLinkage; |
| 926 | return llvm::GlobalVariable::WeakODRLinkage; |
Douglas Gregor | ccecc1b | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | // Silence GCC warning. |
| 930 | return llvm::GlobalVariable::WeakODRLinkage; |
| 931 | } |
| 932 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 933 | static CodeGenModule::GVALinkage |
| 934 | GetLinkageForVariable(ASTContext &Context, const VarDecl *VD) { |
| 935 | // Everything located semantically within an anonymous namespace is |
| 936 | // always internal. |
| 937 | if (VD->isInAnonymousNamespace()) |
| 938 | return CodeGenModule::GVA_Internal; |
| 939 | |
| 940 | // Handle linkage for static data members. |
| 941 | if (VD->isStaticDataMember()) { |
| 942 | switch (VD->getTemplateSpecializationKind()) { |
| 943 | case TSK_Undeclared: |
| 944 | case TSK_ExplicitSpecialization: |
| 945 | case TSK_ExplicitInstantiationDefinition: |
| 946 | return CodeGenModule::GVA_StrongExternal; |
| 947 | |
| 948 | case TSK_ExplicitInstantiationDeclaration: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 949 | llvm_unreachable("Variable should not be instantiated"); |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 950 | // Fall through to treat this like any other instantiation. |
| 951 | |
| 952 | case TSK_ImplicitInstantiation: |
| 953 | return CodeGenModule::GVA_TemplateInstantiation; |
| 954 | } |
| 955 | } |
Eli Friedman | 8a5f75e | 2009-11-26 02:52:12 +0000 | [diff] [blame] | 956 | |
| 957 | if (VD->getLinkage() == VarDecl::InternalLinkage) |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 958 | return CodeGenModule::GVA_Internal; |
| 959 | |
| 960 | return CodeGenModule::GVA_StrongExternal; |
| 961 | } |
| 962 | |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 963 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { |
Chris Lattner | 9d3b0e0 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 964 | llvm::Constant *Init = 0; |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 965 | QualType ASTTy = D->getType(); |
Eli Friedman | 5866fe3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 966 | bool NonConstInit = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | |
Chris Lattner | 9d3b0e0 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 968 | if (D->getInit() == 0) { |
Eli Friedman | 6859a1b | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 969 | // This is a tentative definition; tentative definitions are |
Daniel Dunbar | 7dd749e | 2009-04-15 22:08:45 +0000 | [diff] [blame] | 970 | // implicitly initialized with { 0 }. |
| 971 | // |
| 972 | // Note that tentative definitions are only emitted at the end of |
| 973 | // a translation unit, so they should never have incomplete |
| 974 | // type. In addition, EmitTentativeDefinition makes sure that we |
| 975 | // never attempt to emit a tentative definition if a real one |
| 976 | // exists. A use may still exists, however, so we still may need |
| 977 | // to do a RAUW. |
| 978 | assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); |
Anders Carlsson | f18318c | 2009-08-02 21:18:22 +0000 | [diff] [blame] | 979 | Init = EmitNullConstant(D->getType()); |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 980 | } else { |
Anders Carlsson | 80f97ab | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 981 | Init = EmitConstantExpr(D->getInit(), D->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | |
Eli Friedman | 719ed1a | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 983 | if (!Init) { |
Eli Friedman | 719ed1a | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 984 | QualType T = D->getInit()->getType(); |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 985 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 5866fe3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 986 | EmitCXXGlobalVarDeclInitFunc(D); |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 987 | Init = EmitNullConstant(T); |
Eli Friedman | 5866fe3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 988 | NonConstInit = true; |
Anders Carlsson | b8be93f | 2009-08-08 23:24:23 +0000 | [diff] [blame] | 989 | } else { |
| 990 | ErrorUnsupported(D, "static initializer"); |
| 991 | Init = llvm::UndefValue::get(getTypes().ConvertType(T)); |
| 992 | } |
Eli Friedman | 719ed1a | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 993 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 994 | } |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 995 | |
Chris Lattner | 64c5593 | 2009-03-21 08:13:05 +0000 | [diff] [blame] | 996 | const llvm::Type* InitType = Init->getType(); |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 997 | llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 998 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 999 | // Strip off a bitcast if we got one back. |
| 1000 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 1001 | assert(CE->getOpcode() == llvm::Instruction::BitCast || |
| 1002 | // all zero index gep. |
| 1003 | CE->getOpcode() == llvm::Instruction::GetElementPtr); |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1004 | Entry = CE->getOperand(0); |
| 1005 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1006 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1007 | // Entry is now either a Function or GlobalVariable. |
| 1008 | llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1010 | // We have a definition after a declaration with the wrong type. |
| 1011 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 1012 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 1013 | // (which will be a definition). |
| 1014 | // |
| 1015 | // This happens if there is a prototype for a global (e.g. |
| 1016 | // "extern int x[];") and then a definition of a different type (e.g. |
| 1017 | // "int x[10];"). This also happens when an initializer has a different type |
| 1018 | // from the type of the global (this happens with unions). |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1019 | if (GV == 0 || |
| 1020 | GV->getType()->getElementType() != InitType || |
| 1021 | GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1023 | // Remove the old entry from GlobalDeclMap so that we'll create a new one. |
| 1024 | GlobalDeclMap.erase(getMangledName(D)); |
Daniel Dunbar | b2f4cdb | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1026 | // Make a new global with the correct type, this is now guaranteed to work. |
| 1027 | GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1028 | GV->takeName(cast<llvm::GlobalValue>(Entry)); |
| 1029 | |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1030 | // Replace all uses of the old global with the new global |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | llvm::Constant *NewPtrForOldDecl = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1032 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1033 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | c18d9d5 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 1034 | |
| 1035 | // Erase the old global, since it is no longer used. |
Chris Lattner | 149927c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 1036 | cast<llvm::GlobalValue>(Entry)->eraseFromParent(); |
Chris Lattner | 9d3b0e0 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 1037 | } |
Devang Patel | 19c2b9a | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 1038 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1039 | if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1040 | SourceManager &SM = Context.getSourceManager(); |
| 1041 | AddAnnotation(EmitAnnotateAttr(GV, AA, |
Chris Lattner | 8a42586 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 1042 | SM.getInstantiationLineNumber(D->getLocation()))); |
Nate Begeman | faae081 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Chris Lattner | d14bfa9 | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 1045 | GV->setInitializer(Init); |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1046 | |
| 1047 | // If it is safe to mark the global 'constant', do so now. |
| 1048 | GV->setConstant(false); |
Eli Friedman | 5866fe3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 1049 | if (!NonConstInit && DeclIsConstantGlobal(Context, D)) |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1050 | GV->setConstant(true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | |
Eli Friedman | 97e070e | 2009-02-27 04:11:37 +0000 | [diff] [blame] | 1052 | GV->setAlignment(getContext().getDeclAlignInBytes(D)); |
Eli Friedman | 174d9c2 | 2008-05-29 11:10:27 +0000 | [diff] [blame] | 1053 | |
Chris Lattner | d14bfa9 | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 1054 | // Set the llvm linkage type as appropriate. |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1055 | GVALinkage Linkage = GetLinkageForVariable(getContext(), D); |
Douglas Gregor | 819c3dd | 2009-10-14 21:36:34 +0000 | [diff] [blame] | 1056 | if (Linkage == GVA_Internal) |
Chris Lattner | bc22b5b | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 1057 | GV->setLinkage(llvm::Function::InternalLinkage); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1058 | else if (D->hasAttr<DLLImportAttr>()) |
Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1059 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1060 | else if (D->hasAttr<DLLExportAttr>()) |
Chris Lattner | 8496639 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 1061 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1062 | else if (D->hasAttr<WeakAttr>()) { |
| 1063 | if (GV->isConstant()) |
| 1064 | GV->setLinkage(llvm::GlobalVariable::WeakODRLinkage); |
| 1065 | else |
| 1066 | GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1067 | } else if (Linkage == GVA_TemplateInstantiation) |
| 1068 | GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); |
Daniel Dunbar | 2c11cd1 | 2009-11-30 08:40:34 +0000 | [diff] [blame] | 1069 | else if (!getLangOptions().CPlusPlus && !CodeGenOpts.NoCommon && |
Chris Lattner | c0693bc | 2009-08-05 04:56:58 +0000 | [diff] [blame] | 1070 | !D->hasExternalStorage() && !D->getInit() && |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1071 | !D->getAttr<SectionAttr>()) { |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1072 | GV->setLinkage(llvm::GlobalVariable::CommonLinkage); |
Chris Lattner | f49573d | 2009-08-05 05:20:29 +0000 | [diff] [blame] | 1073 | // common vars aren't constant even if declared const. |
| 1074 | GV->setConstant(false); |
| 1075 | } else |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1076 | GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); |
Daniel Dunbar | d272cca | 2009-04-10 20:26:50 +0000 | [diff] [blame] | 1077 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1078 | SetCommonAttributes(D, GV); |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1079 | |
Sanjiv Gupta | 158143a | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1080 | // Emit global variable debug information. |
Chris Lattner | 64c5593 | 2009-03-21 08:13:05 +0000 | [diff] [blame] | 1081 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | b9fd902 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 1082 | DI->setLocation(D->getLocation()); |
Sanjiv Gupta | 158143a | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 1083 | DI->EmitGlobalVariable(GV, D); |
| 1084 | } |
Chris Lattner | d14bfa9 | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 1085 | } |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 1086 | |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1087 | /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we |
| 1088 | /// implement a function with no prototype, e.g. "int foo() {}". If there are |
| 1089 | /// existing call uses of the old function in the module, this adjusts them to |
| 1090 | /// call the new function directly. |
| 1091 | /// |
| 1092 | /// This is not just a cleanup: the always_inline pass requires direct calls to |
| 1093 | /// functions to be able to inline them. If there is a bitcast in the way, it |
| 1094 | /// won't inline them. Instcombine normally deletes these calls, but it isn't |
| 1095 | /// run at -O0. |
| 1096 | static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, |
| 1097 | llvm::Function *NewFn) { |
| 1098 | // If we're redefining a global as a function, don't transform it. |
| 1099 | llvm::Function *OldFn = dyn_cast<llvm::Function>(Old); |
| 1100 | if (OldFn == 0) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1102 | const llvm::Type *NewRetTy = NewFn->getReturnType(); |
| 1103 | llvm::SmallVector<llvm::Value*, 4> ArgList; |
| 1104 | |
| 1105 | for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end(); |
| 1106 | UI != E; ) { |
| 1107 | // TODO: Do invokes ever occur in C code? If so, we should handle them too. |
Chris Lattner | 0261e5c | 2009-06-04 16:47:43 +0000 | [diff] [blame] | 1108 | unsigned OpNo = UI.getOperandNo(); |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1109 | llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*UI++); |
Chris Lattner | 0261e5c | 2009-06-04 16:47:43 +0000 | [diff] [blame] | 1110 | if (!CI || OpNo != 0) continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1112 | // If the return types don't match exactly, and if the call isn't dead, then |
| 1113 | // we can't transform this call. |
| 1114 | if (CI->getType() != NewRetTy && !CI->use_empty()) |
| 1115 | continue; |
| 1116 | |
| 1117 | // If the function was passed too few arguments, don't transform. If extra |
| 1118 | // arguments were passed, we silently drop them. If any of the types |
| 1119 | // mismatch, we don't transform. |
| 1120 | unsigned ArgNo = 0; |
| 1121 | bool DontTransform = false; |
| 1122 | for (llvm::Function::arg_iterator AI = NewFn->arg_begin(), |
| 1123 | E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) { |
| 1124 | if (CI->getNumOperands()-1 == ArgNo || |
| 1125 | CI->getOperand(ArgNo+1)->getType() != AI->getType()) { |
| 1126 | DontTransform = true; |
| 1127 | break; |
| 1128 | } |
| 1129 | } |
| 1130 | if (DontTransform) |
| 1131 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1133 | // Okay, we can transform this. Create the new call instruction and copy |
| 1134 | // over the required information. |
| 1135 | ArgList.append(CI->op_begin()+1, CI->op_begin()+1+ArgNo); |
| 1136 | llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList.begin(), |
| 1137 | ArgList.end(), "", CI); |
| 1138 | ArgList.clear(); |
Benjamin Kramer | dde0fee | 2009-10-05 13:47:21 +0000 | [diff] [blame] | 1139 | if (!NewCall->getType()->isVoidTy()) |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1140 | NewCall->takeName(CI); |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1141 | NewCall->setAttributes(CI->getAttributes()); |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 1142 | NewCall->setCallingConv(CI->getCallingConv()); |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1143 | |
| 1144 | // Finally, remove the old call, replacing any uses with the new one. |
Chris Lattner | 734351d | 2009-10-14 05:49:21 +0000 | [diff] [blame] | 1145 | if (!CI->use_empty()) |
| 1146 | CI->replaceAllUsesWith(NewCall); |
Devang Patel | 7468489 | 2009-10-13 17:02:04 +0000 | [diff] [blame] | 1147 | |
Devang Patel | f33cfaf | 2009-10-14 17:03:29 +0000 | [diff] [blame] | 1148 | // Copy any custom metadata attached with CI. |
Chris Lattner | 3f8e5dd | 2009-12-29 07:49:13 +0000 | [diff] [blame] | 1149 | if (llvm::MDNode *DbgNode = CI->getMetadata("dbg")) |
| 1150 | NewCall->setMetadata("dbg", DbgNode); |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1151 | CI->eraseFromParent(); |
| 1152 | } |
| 1153 | } |
| 1154 | |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1155 | |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1156 | void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) { |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 1157 | const llvm::FunctionType *Ty; |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1158 | const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 1160 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1161 | bool isVariadic = D->getType()->getAs<FunctionProtoType>()->isVariadic(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 1163 | Ty = getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), isVariadic); |
| 1164 | } else { |
| 1165 | Ty = cast<llvm::FunctionType>(getTypes().ConvertType(D->getType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 1167 | // As a special case, make sure that definitions of K&R function |
| 1168 | // "type foo()" aren't declared as varargs (which forces the backend |
| 1169 | // to do unnecessary work). |
| 1170 | if (D->getType()->isFunctionNoProtoType()) { |
| 1171 | assert(Ty->isVarArg() && "Didn't lower type as expected"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1172 | // Due to stret, the lowered function could have arguments. |
| 1173 | // Just create the same type as was lowered by ConvertType |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 1174 | // but strip off the varargs bit. |
| 1175 | std::vector<const llvm::Type*> Args(Ty->param_begin(), Ty->param_end()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1176 | Ty = llvm::FunctionType::get(Ty->getReturnType(), Args, false); |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 1177 | } |
Chris Lattner | 75acb0c | 2009-03-22 19:35:37 +0000 | [diff] [blame] | 1178 | } |
Daniel Dunbar | f0acf7b | 2009-02-19 07:15:39 +0000 | [diff] [blame] | 1179 | |
Chris Lattner | eb7466d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 1180 | // Get or create the prototype for the function. |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1181 | llvm::Constant *Entry = GetAddrOfFunction(GD, Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1183 | // Strip off a bitcast if we got one back. |
| 1184 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
| 1185 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
| 1186 | Entry = CE->getOperand(0); |
| 1187 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
| 1189 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1190 | if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1191 | llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1192 | |
Daniel Dunbar | 99d2835 | 2009-03-09 23:53:08 +0000 | [diff] [blame] | 1193 | // If the types mismatch then we have to rewrite the definition. |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1194 | assert(OldFn->isDeclaration() && |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1195 | "Shouldn't replace non-declaration"); |
Chris Lattner | 832323e | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 1196 | |
Chris Lattner | 5eaee56 | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 1197 | // F is the Function* for the one with the wrong type, we must make a new |
| 1198 | // Function* and update everything that used F (a declaration) with the new |
| 1199 | // Function* (which will be a definition). |
| 1200 | // |
| 1201 | // This happens if there is a prototype for a function |
| 1202 | // (e.g. "int f()") and then a definition of a different type |
| 1203 | // (e.g. "int f(int x)"). Start by making a new function of the |
| 1204 | // correct type, RAUW, then steal the name. |
Chris Lattner | 832323e | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 1205 | GlobalDeclMap.erase(getMangledName(D)); |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1206 | llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty)); |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1207 | NewFn->takeName(OldFn); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1209 | // If this is an implementation of a function without a prototype, try to |
| 1210 | // replace any existing uses of the function (which may be calls) with uses |
| 1211 | // of the new function |
Chris Lattner | eb7466d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 1212 | if (D->getType()->isFunctionNoProtoType()) { |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1213 | ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn); |
Chris Lattner | eb7466d | 2009-05-12 20:58:15 +0000 | [diff] [blame] | 1214 | OldFn->removeDeadConstantUsers(); |
| 1215 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | |
Chris Lattner | 5eaee56 | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 1217 | // Replace uses of F with the Function we will endow with a body. |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1218 | if (!Entry->use_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | llvm::Constant *NewPtrForOldDecl = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1220 | llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1221 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
| 1222 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | |
Chris Lattner | 5eaee56 | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 1224 | // Ok, delete the old function now, which is dead. |
Chris Lattner | 36797ab | 2009-05-05 06:16:31 +0000 | [diff] [blame] | 1225 | OldFn->eraseFromParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1227 | Entry = NewFn; |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1228 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1229 | |
Chris Lattner | a85d68e | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 1230 | llvm::Function *Fn = cast<llvm::Function>(Entry); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1231 | |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1232 | CodeGenFunction(*this).GenerateCode(D, Fn); |
Daniel Dunbar | 74aa7e1 | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 1233 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1234 | SetFunctionDefinitionAttributes(D, Fn); |
| 1235 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1237 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1238 | AddGlobalCtor(Fn, CA->getPriority()); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1239 | if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) |
Daniel Dunbar | 0beedc1 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 1240 | AddGlobalDtor(Fn, DA->getPriority()); |
Daniel Dunbar | 9c42652 | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1243 | void CodeGenModule::EmitAliasDefinition(const ValueDecl *D) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1244 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1245 | assert(AA && "Not an alias?"); |
| 1246 | |
| 1247 | const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1248 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1249 | // Unique the name through the identifier table. |
| 1250 | const char *AliaseeName = AA->getAliasee().c_str(); |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1251 | AliaseeName = getContext().Idents.get(AliaseeName).getNameStart(); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1252 | |
| 1253 | // Create a reference to the named value. This ensures that it is emitted |
| 1254 | // if a deferred decl. |
| 1255 | llvm::Constant *Aliasee; |
| 1256 | if (isa<llvm::FunctionType>(DeclTy)) |
Chris Lattner | e0be0df | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 1257 | Aliasee = GetOrCreateLLVMFunction(AliaseeName, DeclTy, GlobalDecl()); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1258 | else |
| 1259 | Aliasee = GetOrCreateLLVMGlobal(AliaseeName, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1260 | llvm::PointerType::getUnqual(DeclTy), 0); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Create the new alias itself, but don't set a name yet. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1263 | llvm::GlobalValue *GA = |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1264 | new llvm::GlobalAlias(Aliasee->getType(), |
| 1265 | llvm::Function::ExternalLinkage, |
| 1266 | "", Aliasee, &getModule()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1268 | // See if there is already something with the alias' name in the module. |
| 1269 | const char *MangledName = getMangledName(D); |
| 1270 | llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1271 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1272 | if (Entry && !Entry->isDeclaration()) { |
| 1273 | // If there is a definition in the module, then it wins over the alias. |
| 1274 | // This is dubious, but allow it to be safe. Just ignore the alias. |
| 1275 | GA->eraseFromParent(); |
| 1276 | return; |
| 1277 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1279 | if (Entry) { |
| 1280 | // If there is a declaration in the module, then we had an extern followed |
| 1281 | // by the alias, as in: |
| 1282 | // extern int test6(); |
| 1283 | // ... |
| 1284 | // int test6() __attribute__((alias("test7"))); |
| 1285 | // |
| 1286 | // Remove it and replace uses of it with the alias. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1287 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1288 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1289 | Entry->getType())); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1290 | Entry->eraseFromParent(); |
| 1291 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1292 | |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1293 | // Now we know that there is no conflict, set the name. |
| 1294 | Entry = GA; |
| 1295 | GA->setName(MangledName); |
| 1296 | |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1297 | // Set attributes which are particular to an alias; this is a |
| 1298 | // specialization of the attributes which may be set on a global |
| 1299 | // variable/function. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1300 | if (D->hasAttr<DLLExportAttr>()) { |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1301 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1302 | // The dllexport attribute is ignored for undefined symbols. |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1303 | if (FD->getBody()) |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1304 | GA->setLinkage(llvm::Function::DLLExportLinkage); |
| 1305 | } else { |
| 1306 | GA->setLinkage(llvm::Function::DLLExportLinkage); |
| 1307 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | } else if (D->hasAttr<WeakAttr>() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1309 | D->hasAttr<WeakImportAttr>()) { |
Daniel Dunbar | 3893257 | 2009-04-14 08:05:55 +0000 | [diff] [blame] | 1310 | GA->setLinkage(llvm::Function::WeakAnyLinkage); |
| 1311 | } |
| 1312 | |
| 1313 | SetCommonAttributes(D, GA); |
Chris Lattner | 5404169 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Chris Lattner | e64911a | 2009-03-22 21:56:56 +0000 | [diff] [blame] | 1316 | /// getBuiltinLibFunction - Given a builtin id for a function like |
| 1317 | /// "__builtin_fabsf", return a Function* for "fabsf". |
Daniel Dunbar | ff0553e | 2009-09-14 04:33:21 +0000 | [diff] [blame] | 1318 | llvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, |
| 1319 | unsigned BuiltinID) { |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1320 | assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1322 | "isn't a lib fn"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1324 | // Get the name, skip over the __builtin_ prefix (if necessary). |
| 1325 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID); |
| 1326 | if (Context.BuiltinInfo.isLibFunction(BuiltinID)) |
| 1327 | Name += 10; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | const llvm::FunctionType *Ty = |
Eli Friedman | 4f678f3 | 2009-12-09 03:05:59 +0000 | [diff] [blame] | 1330 | cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType())); |
Chris Lattner | 1eec660 | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 1331 | |
Chris Lattner | e64911a | 2009-03-22 21:56:56 +0000 | [diff] [blame] | 1332 | // Unique the name through the identifier table. |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1333 | Name = getContext().Idents.get(Name).getNameStart(); |
Daniel Dunbar | ff0553e | 2009-09-14 04:33:21 +0000 | [diff] [blame] | 1334 | return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl(FD)); |
Chris Lattner | 1eec660 | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Chris Lattner | b8be97e | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 1337 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, |
| 1338 | unsigned NumTys) { |
| 1339 | return llvm::Intrinsic::getDeclaration(&getModule(), |
| 1340 | (llvm::Intrinsic::ID)IID, Tys, NumTys); |
| 1341 | } |
Chris Lattner | 1eec660 | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 1342 | |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 1343 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 1344 | if (MemCpyFn) return MemCpyFn; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1345 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(VMContext); |
Chris Lattner | 0f137df | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 1346 | return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); |
Chris Lattner | 09153c0 | 2007-06-22 18:48:09 +0000 | [diff] [blame] | 1347 | } |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1348 | |
Eli Friedman | df649f3 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 1349 | llvm::Function *CodeGenModule::getMemMoveFn() { |
| 1350 | if (MemMoveFn) return MemMoveFn; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1351 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(VMContext); |
Chris Lattner | 0f137df | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 1352 | return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); |
Eli Friedman | df649f3 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Lauro Ramos Venancio | 91fdb9e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 1355 | llvm::Function *CodeGenModule::getMemSetFn() { |
| 1356 | if (MemSetFn) return MemSetFn; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1357 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(VMContext); |
Chris Lattner | 0f137df | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 1358 | return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); |
Lauro Ramos Venancio | 91fdb9e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 1359 | } |
Chris Lattner | b8be97e | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 1360 | |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1361 | static llvm::StringMapEntry<llvm::Constant*> & |
| 1362 | GetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map, |
| 1363 | const StringLiteral *Literal, |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1364 | bool TargetIsLSB, |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1365 | bool &IsUTF16, |
| 1366 | unsigned &StringLength) { |
Daniel Dunbar | 5de27da | 2009-09-22 03:27:52 +0000 | [diff] [blame] | 1367 | unsigned NumBytes = Literal->getByteLength(); |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1368 | |
Daniel Dunbar | b879c3c | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 1369 | // Check for simple case. |
| 1370 | if (!Literal->containsNonAsciiOrNull()) { |
| 1371 | StringLength = NumBytes; |
| 1372 | return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(), |
| 1373 | StringLength)); |
| 1374 | } |
| 1375 | |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1376 | // Otherwise, convert the UTF8 literals into a byte string. |
| 1377 | llvm::SmallVector<UTF16, 128> ToBuf(NumBytes); |
| 1378 | const UTF8 *FromPtr = (UTF8 *)Literal->getStrData(); |
| 1379 | UTF16 *ToPtr = &ToBuf[0]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | |
| 1381 | ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1382 | &ToPtr, ToPtr + NumBytes, |
| 1383 | strictConversion); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1385 | // Check for conversion failure. |
| 1386 | if (Result != conversionOK) { |
| 1387 | // FIXME: Have Sema::CheckObjCString() validate the UTF-8 string and remove |
| 1388 | // this duplicate code. |
| 1389 | assert(Result == sourceIllegal && "UTF-8 to UTF-16 conversion failed"); |
Daniel Dunbar | b879c3c | 2009-09-22 10:03:52 +0000 | [diff] [blame] | 1390 | StringLength = NumBytes; |
| 1391 | return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(), |
| 1392 | StringLength)); |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1395 | // ConvertUTF8toUTF16 returns the length in ToPtr. |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1396 | StringLength = ToPtr - &ToBuf[0]; |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1397 | |
| 1398 | // Render the UTF-16 string into a byte array and convert to the target byte |
| 1399 | // order. |
| 1400 | // |
| 1401 | // FIXME: This isn't something we should need to do here. |
| 1402 | llvm::SmallString<128> AsBytes; |
| 1403 | AsBytes.reserve(StringLength * 2); |
| 1404 | for (unsigned i = 0; i != StringLength; ++i) { |
| 1405 | unsigned short Val = ToBuf[i]; |
| 1406 | if (TargetIsLSB) { |
| 1407 | AsBytes.push_back(Val & 0xFF); |
| 1408 | AsBytes.push_back(Val >> 8); |
| 1409 | } else { |
| 1410 | AsBytes.push_back(Val >> 8); |
| 1411 | AsBytes.push_back(Val & 0xFF); |
| 1412 | } |
| 1413 | } |
Daniel Dunbar | 4d93a4f | 2009-08-03 21:47:08 +0000 | [diff] [blame] | 1414 | // Append one extra null character, the second is automatically added by our |
| 1415 | // caller. |
| 1416 | AsBytes.push_back(0); |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1417 | |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1418 | IsUTF16 = true; |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1419 | return Map.GetOrCreateValue(llvm::StringRef(AsBytes.data(), AsBytes.size())); |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | llvm::Constant * |
| 1423 | CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { |
| 1424 | unsigned StringLength = 0; |
| 1425 | bool isUTF16 = false; |
| 1426 | llvm::StringMapEntry<llvm::Constant*> &Entry = |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1427 | GetConstantCFStringEntry(CFConstantStringMap, Literal, |
Daniel Dunbar | 91ade14 | 2009-07-23 23:41:22 +0000 | [diff] [blame] | 1428 | getTargetData().isLittleEndian(), |
| 1429 | isUTF16, StringLength); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1431 | if (llvm::Constant *C = Entry.getValue()) |
| 1432 | return C; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1434 | llvm::Constant *Zero = |
| 1435 | llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1436 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 | |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 1438 | // If we don't already have it, get __CFConstantStringClassReference. |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1439 | if (!CFConstantStringClassRef) { |
| 1440 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1441 | Ty = llvm::ArrayType::get(Ty, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | llvm::Constant *GV = CreateRuntimeVariable(Ty, |
Chris Lattner | aa64ca2 | 2009-07-16 16:48:25 +0000 | [diff] [blame] | 1443 | "__CFConstantStringClassReference"); |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1444 | // Decay array -> ptr |
| 1445 | CFConstantStringClassRef = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1446 | llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1447 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | |
Anders Carlsson | c2480a5 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1449 | QualType CFTy = getContext().getCFConstantStringType(); |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1450 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | const llvm::StructType *STy = |
Anders Carlsson | c2480a5 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1452 | cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
| 1453 | |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1454 | std::vector<llvm::Constant*> Fields(4); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1455 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1456 | // Class pointer. |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1457 | Fields[0] = CFConstantStringClassRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1459 | // Flags. |
Daniel Dunbar | d644ad6 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1460 | const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) : |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1462 | llvm::ConstantInt::get(Ty, 0x07C8); |
| 1463 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1464 | // String pointer. |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1465 | llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1466 | |
Chris Lattner | 4f8a2e2 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1467 | const char *Sect = 0; |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 1468 | llvm::GlobalValue::LinkageTypes Linkage; |
Chris Lattner | 4f8a2e2 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1469 | bool isConstant; |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1470 | if (isUTF16) { |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1471 | Sect = getContext().Target.getUnicodeStringSection(); |
Chris Lattner | 4f8a2e2 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1472 | // FIXME: why do utf strings get "_" labels instead of "L" labels? |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 1473 | Linkage = llvm::GlobalValue::InternalLinkage; |
Chris Lattner | 4f8a2e2 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1474 | // Note: -fwritable-strings doesn't make unicode CFStrings writable, but |
| 1475 | // does make plain ascii ones writable. |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1476 | isConstant = true; |
Chris Lattner | 4f8a2e2 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1477 | } else { |
| 1478 | Linkage = llvm::GlobalValue::PrivateLinkage; |
| 1479 | isConstant = !Features.WritableStrings; |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1480 | } |
Chris Lattner | 4f8a2e2 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1481 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | llvm::GlobalVariable *GV = |
Chris Lattner | 4f8a2e2 | 2009-10-14 05:55:45 +0000 | [diff] [blame] | 1483 | new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, |
| 1484 | ".str"); |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1485 | if (Sect) |
Daniel Dunbar | 08b216a | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1486 | GV->setSection(Sect); |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1487 | if (isUTF16) { |
| 1488 | unsigned Align = getContext().getTypeAlign(getContext().ShortTy)/8; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1489 | GV->setAlignment(Align); |
Daniel Dunbar | fd6cfcf | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1490 | } |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1491 | Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); |
| 1492 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1493 | // String length. |
| 1494 | Ty = getTypes().ConvertType(getContext().LongTy); |
Anders Carlsson | 157c321 | 2009-08-16 05:55:31 +0000 | [diff] [blame] | 1495 | Fields[3] = llvm::ConstantInt::get(Ty, StringLength); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1497 | // The struct. |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1498 | C = llvm::ConstantStruct::get(STy, Fields); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | GV = new llvm::GlobalVariable(getModule(), C->getType(), true, |
| 1500 | llvm::GlobalVariable::PrivateLinkage, C, |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 1501 | "_unnamed_cfstring_"); |
Daniel Dunbar | 08b216a | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1502 | if (const char *Sect = getContext().Target.getCFStringSection()) |
| 1503 | GV->setSection(Sect); |
Daniel Dunbar | 64509b2 | 2009-07-23 22:52:48 +0000 | [diff] [blame] | 1504 | Entry.setValue(GV); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | |
Anders Carlsson | 41b7c6b | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 1506 | return GV; |
Anders Carlsson | b04ea61 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1507 | } |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1508 | |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1509 | /// GetStringForStringLiteral - Return the appropriate bytes for a |
Daniel Dunbar | 6dfdf8c | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1510 | /// string literal, properly padded to match the literal type. |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1511 | std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { |
Daniel Dunbar | 6dfdf8c | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1512 | const char *StrData = E->getStrData(); |
| 1513 | unsigned Len = E->getByteLength(); |
| 1514 | |
| 1515 | const ConstantArrayType *CAT = |
| 1516 | getContext().getAsConstantArrayType(E->getType()); |
| 1517 | assert(CAT && "String isn't pointer or array!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | |
Chris Lattner | d42c29f | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1519 | // Resize the string to the right size. |
Daniel Dunbar | 6dfdf8c | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1520 | std::string Str(StrData, StrData+Len); |
| 1521 | uint64_t RealLen = CAT->getSize().getZExtValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | |
Chris Lattner | d42c29f | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1523 | if (E->isWide()) |
| 1524 | RealLen *= getContext().Target.getWCharWidth()/8; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | |
Daniel Dunbar | 6dfdf8c | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1526 | Str.resize(RealLen, '\0'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | |
Daniel Dunbar | 6dfdf8c | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1528 | return Str; |
| 1529 | } |
| 1530 | |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1531 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 1532 | /// constant array for the given string literal. |
| 1533 | llvm::Constant * |
| 1534 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { |
| 1535 | // FIXME: This can be more efficient. |
Eli Friedman | 49ddc5f | 2009-11-16 05:55:46 +0000 | [diff] [blame] | 1536 | // FIXME: We shouldn't need to bitcast the constant in the wide string case. |
| 1537 | llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S)); |
| 1538 | if (S->isWide()) { |
| 1539 | llvm::Type *DestTy = |
| 1540 | llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType())); |
| 1541 | C = llvm::ConstantExpr::getBitCast(C, DestTy); |
| 1542 | } |
| 1543 | return C; |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1546 | /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant |
| 1547 | /// array for the given ObjCEncodeExpr node. |
| 1548 | llvm::Constant * |
| 1549 | CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { |
| 1550 | std::string Str; |
| 1551 | getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
Eli Friedman | 4663a33 | 2009-03-07 20:17:55 +0000 | [diff] [blame] | 1552 | |
| 1553 | return GetAddrOfConstantCString(Str); |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1557 | /// GenerateWritableString -- Creates storage for a string literal. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1559 | bool constant, |
Daniel Dunbar | dfcf599 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1560 | CodeGenModule &CGM, |
| 1561 | const char *GlobalName) { |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1562 | // Create Constant for this string literal. Don't add a '\0'. |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1563 | llvm::Constant *C = |
| 1564 | llvm::ConstantArray::get(CGM.getLLVMContext(), str, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1566 | // Create a global variable for this string |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | return new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant, |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 1568 | llvm::GlobalValue::PrivateLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1569 | C, GlobalName); |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1572 | /// GetAddrOfConstantString - Returns a pointer to a character array |
| 1573 | /// containing the literal. This contents are exactly that of the |
| 1574 | /// given string, i.e. it will not be null terminated automatically; |
| 1575 | /// see GetAddrOfConstantCString. Note that whether the result is |
| 1576 | /// actually a pointer to an LLVM constant depends on |
| 1577 | /// Feature.WriteableStrings. |
| 1578 | /// |
| 1579 | /// The result has pointer to array type. |
Daniel Dunbar | dfcf599 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1580 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, |
| 1581 | const char *GlobalName) { |
Daniel Dunbar | 08b216a | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1582 | bool IsConstant = !Features.WritableStrings; |
| 1583 | |
| 1584 | // Get the default prefix if a name wasn't specified. |
| 1585 | if (!GlobalName) |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 1586 | GlobalName = ".str"; |
Daniel Dunbar | 08b216a | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1587 | |
| 1588 | // Don't share any string literals if strings aren't constant. |
| 1589 | if (!IsConstant) |
Daniel Dunbar | dfcf599 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1590 | return GenerateStringLiteral(str, false, *this, GlobalName); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | |
| 1592 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
Chris Lattner | 3afa3e1 | 2009-07-16 05:03:48 +0000 | [diff] [blame] | 1593 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1594 | |
| 1595 | if (Entry.getValue()) |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1596 | return Entry.getValue(); |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1597 | |
| 1598 | // Create a global variable for this. |
Daniel Dunbar | dfcf599 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1599 | llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); |
Chris Lattner | fb30009 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1600 | Entry.setValue(C); |
| 1601 | return C; |
| 1602 | } |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1603 | |
| 1604 | /// GetAddrOfConstantCString - Returns a pointer to a character |
| 1605 | /// array containing the literal and a terminating '\-' |
| 1606 | /// character. The result has pointer to array type. |
Daniel Dunbar | dfcf599 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1607 | llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, |
| 1608 | const char *GlobalName){ |
Chris Lattner | 2e41b0e | 2008-12-09 19:10:54 +0000 | [diff] [blame] | 1609 | return GetAddrOfConstantString(str + '\0', GlobalName); |
Daniel Dunbar | c4baa06 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1610 | } |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1611 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1612 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 1613 | /// properties for an implementation. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | void CodeGenModule::EmitObjCPropertyImplementations(const |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1615 | ObjCImplementationDecl *D) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1617 | i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1618 | ObjCPropertyImplDecl *PID = *i; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1620 | // Dynamic is just for type-checking. |
| 1621 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1622 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1623 | |
| 1624 | // Determine which methods need to be implemented, some may have |
| 1625 | // been overridden. Note that ::isSynthesized is not the method |
| 1626 | // we want, that just indicates if the decl came from a |
| 1627 | // property. What we want to know is if the method is defined in |
| 1628 | // this implementation. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1629 | if (!D->getInstanceMethod(PD->getGetterName())) |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1630 | CodeGenFunction(*this).GenerateObjCGetter( |
| 1631 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1632 | if (!PD->isReadOnly() && |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1633 | !D->getInstanceMethod(PD->getSetterName())) |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1634 | CodeGenFunction(*this).GenerateObjCSetter( |
| 1635 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1636 | } |
| 1637 | } |
| 1638 | } |
| 1639 | |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 1640 | /// EmitNamespace - Emit all declarations in a namespace. |
Anders Carlsson | 237f349 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 1641 | void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1642 | for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end(); |
Anders Carlsson | 237f349 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 1643 | I != E; ++I) |
| 1644 | EmitTopLevelDecl(*I); |
| 1645 | } |
| 1646 | |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 1647 | // EmitLinkageSpec - Emit all declarations in a linkage spec. |
| 1648 | void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { |
Eli Friedman | e480ce3 | 2009-08-01 20:48:04 +0000 | [diff] [blame] | 1649 | if (LSD->getLanguage() != LinkageSpecDecl::lang_c && |
| 1650 | LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 1651 | ErrorUnsupported(LSD, "linkage spec"); |
| 1652 | return; |
| 1653 | } |
| 1654 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1655 | for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end(); |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 1656 | I != E; ++I) |
| 1657 | EmitTopLevelDecl(*I); |
| 1658 | } |
| 1659 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1660 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 1661 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
| 1662 | // If an error has occurred, stop code generation, but continue |
| 1663 | // parsing and semantic analysis (to ensure all warnings and errors |
| 1664 | // are emitted). |
| 1665 | if (Diags.hasErrorOccurred()) |
| 1666 | return; |
| 1667 | |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1668 | // Ignore dependent declarations. |
| 1669 | if (D->getDeclContext() && D->getDeclContext()->isDependentContext()) |
| 1670 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1672 | switch (D->getKind()) { |
Anders Carlsson | 6c0a6e4 | 2009-08-25 13:14:46 +0000 | [diff] [blame] | 1673 | case Decl::CXXConversion: |
Anders Carlsson | 468fa63 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 1674 | case Decl::CXXMethod: |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1675 | case Decl::Function: |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 1676 | // Skip function templates |
| 1677 | if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate()) |
| 1678 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 1680 | EmitGlobal(cast<FunctionDecl>(D)); |
| 1681 | break; |
| 1682 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1683 | case Decl::Var: |
Anders Carlsson | ecf9bf0 | 2009-09-10 23:43:36 +0000 | [diff] [blame] | 1684 | EmitGlobal(cast<VarDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1685 | break; |
| 1686 | |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 1687 | // C++ Decls |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1688 | case Decl::Namespace: |
Anders Carlsson | 237f349 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 1689 | EmitNamespace(cast<NamespaceDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1690 | break; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1691 | // No code generation needed. |
John McCall | 1e9de05 | 2009-11-17 09:33:40 +0000 | [diff] [blame] | 1692 | case Decl::UsingShadow: |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1693 | case Decl::Using: |
Douglas Gregor | e5feb51 | 2009-09-02 23:49:23 +0000 | [diff] [blame] | 1694 | case Decl::UsingDirective: |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1695 | case Decl::ClassTemplate: |
| 1696 | case Decl::FunctionTemplate: |
Anders Carlsson | 649a17e | 2009-09-23 19:19:16 +0000 | [diff] [blame] | 1697 | case Decl::NamespaceAlias: |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1698 | break; |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 1699 | case Decl::CXXConstructor: |
Anders Carlsson | 0ade971 | 2009-11-24 05:16:24 +0000 | [diff] [blame] | 1700 | // Skip function templates |
| 1701 | if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate()) |
| 1702 | return; |
| 1703 | |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 1704 | EmitCXXConstructors(cast<CXXConstructorDecl>(D)); |
| 1705 | break; |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 1706 | case Decl::CXXDestructor: |
| 1707 | EmitCXXDestructors(cast<CXXDestructorDecl>(D)); |
| 1708 | break; |
Anders Carlsson | 8783543 | 2009-06-11 21:22:55 +0000 | [diff] [blame] | 1709 | |
| 1710 | case Decl::StaticAssert: |
| 1711 | // Nothing to do. |
| 1712 | break; |
| 1713 | |
Anders Carlsson | f747524 | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 1714 | // Objective-C Decls |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | |
Fariborz Jahanian | 3654e65 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 1716 | // Forward declarations, no (immediate) code generation. |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1717 | case Decl::ObjCClass: |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1718 | case Decl::ObjCForwardProtocol: |
Fariborz Jahanian | 629aed9 | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 1719 | case Decl::ObjCCategory: |
Chris Lattner | d18136a | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 1720 | case Decl::ObjCInterface: |
Chris Lattner | d18136a | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 1721 | break; |
| 1722 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1723 | case Decl::ObjCProtocol: |
Fariborz Jahanian | 629aed9 | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 1724 | Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1725 | break; |
| 1726 | |
| 1727 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1728 | // Categories have properties but don't support synthesize so we |
| 1729 | // can ignore them here. |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1730 | Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
| 1731 | break; |
| 1732 | |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1733 | case Decl::ObjCImplementation: { |
| 1734 | ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); |
| 1735 | EmitObjCPropertyImplementations(OMD); |
| 1736 | Runtime->GenerateClass(OMD); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1737 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | } |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1739 | case Decl::ObjCMethod: { |
| 1740 | ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); |
| 1741 | // If this is not a prototype, emit the body. |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1742 | if (OMD->getBody()) |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1743 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 1744 | break; |
| 1745 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1746 | case Decl::ObjCCompatibleAlias: |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1747 | // compatibility-alias is a directive and has no code gen. |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1748 | break; |
| 1749 | |
Anders Carlsson | cbaeb9e | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 1750 | case Decl::LinkageSpec: |
| 1751 | EmitLinkageSpec(cast<LinkageSpecDecl>(D)); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1752 | break; |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1753 | |
| 1754 | case Decl::FileScopeAsm: { |
| 1755 | FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); |
Benjamin Kramer | b11118b | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 1756 | llvm::StringRef AsmString = AD->getAsmString()->getString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1758 | const std::string &S = getModule().getModuleInlineAsm(); |
| 1759 | if (S.empty()) |
| 1760 | getModule().setModuleInlineAsm(AsmString); |
| 1761 | else |
Benjamin Kramer | b11118b | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 1762 | getModule().setModuleInlineAsm(S + '\n' + AsmString.str()); |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1763 | break; |
| 1764 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | |
| 1766 | default: |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1767 | // Make sure we handled everything we should, every other kind is a |
| 1768 | // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind |
| 1769 | // function. Need to recode Decl::Kind to do that easily. |
Daniel Dunbar | fce4be8 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1770 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
| 1771 | } |
| 1772 | } |