Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | bd36064 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 15 | #include "CGDebugInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "CodeGenFunction.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 17 | #include "CGCall.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 19 | #include "Mangle.h" |
Chris Lattner | bd36064 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/CompileOptions.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclCXX.h" |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 24 | #include "clang/Basic/Diagnostic.h" |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | e9b7d8a | 2009-04-01 15:50:34 +0000 | [diff] [blame] | 27 | #include "clang/Basic/ConvertUTF.h" |
Nate Begeman | ec9426c | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 28 | #include "llvm/CallingConv.h" |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 29 | #include "llvm/Module.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | #include "llvm/Intrinsics.h" |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | using namespace CodeGen; |
| 34 | |
| 35 | |
Chris Lattner | bd36064 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 36 | CodeGenModule::CodeGenModule(ASTContext &C, const CompileOptions &compileOpts, |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 37 | llvm::Module &M, const llvm::TargetData &TD, |
Chris Lattner | bd36064 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 38 | Diagnostic &diags) |
| 39 | : BlockModule(C, M, TD, Types, *this), Context(C), |
| 40 | Features(C.getLangOptions()), CompileOpts(compileOpts), TheModule(M), |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 41 | TheTargetData(TD), Diags(diags), Types(C, M, TD), Runtime(0), |
| 42 | MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0) { |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 3c8f153 | 2009-03-21 07:12:05 +0000 | [diff] [blame] | 44 | if (!Features.ObjC1) |
| 45 | Runtime = 0; |
| 46 | else if (!Features.NeXTRuntime) |
| 47 | Runtime = CreateGNUObjCRuntime(*this); |
| 48 | else if (Features.ObjCNonFragileABI) |
| 49 | Runtime = CreateMacNonFragileABIObjCRuntime(*this); |
| 50 | else |
| 51 | Runtime = CreateMacObjCRuntime(*this); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 52 | |
| 53 | // If debug info generation is enabled, create the CGDebugInfo object. |
Chris Lattner | bd36064 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 54 | DebugInfo = CompileOpts.DebugInfo ? new CGDebugInfo(this) : 0; |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | CodeGenModule::~CodeGenModule() { |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 58 | delete Runtime; |
| 59 | delete DebugInfo; |
| 60 | } |
| 61 | |
| 62 | void CodeGenModule::Release() { |
Chris Lattner | 82227ff | 2009-03-22 21:21:57 +0000 | [diff] [blame] | 63 | EmitDeferred(); |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 64 | if (Runtime) |
| 65 | if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) |
| 66 | AddGlobalCtor(ObjCInitFunction); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 67 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 68 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 69 | EmitAnnotations(); |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 70 | EmitLLVMUsed(); |
Daniel Dunbar | f1968f2 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 73 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 74 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 75 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, |
| 76 | bool OmitOnError) { |
| 77 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 78 | return; |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 79 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Daniel Dunbar | 56b8001 | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 80 | "cannot compile this %0 yet"); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 81 | std::string Msg = Type; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 82 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) |
| 83 | << Msg << S->getSourceRange(); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 84 | } |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 85 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 86 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 87 | /// specified decl yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 88 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, |
| 89 | bool OmitOnError) { |
| 90 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 91 | return; |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 92 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Daniel Dunbar | 56b8001 | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 93 | "cannot compile this %0 yet"); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 94 | std::string Msg = Type; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 95 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 98 | /// setGlobalVisibility - Set the visibility for the given LLVM |
| 99 | /// GlobalValue according to the given clang AST visibility value. |
| 100 | static void setGlobalVisibility(llvm::GlobalValue *GV, |
| 101 | VisibilityAttr::VisibilityTypes Vis) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 102 | // Do not change the visibility of internal definitions. |
| 103 | if (GV->hasInternalLinkage()) |
| 104 | return; |
| 105 | |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 106 | switch (Vis) { |
| 107 | default: assert(0 && "Unknown visibility!"); |
| 108 | case VisibilityAttr::DefaultVisibility: |
| 109 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 110 | break; |
| 111 | case VisibilityAttr::HiddenVisibility: |
| 112 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 113 | break; |
| 114 | case VisibilityAttr::ProtectedVisibility: |
| 115 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
Fariborz Jahanian | 7cd2e93 | 2009-04-03 03:28:57 +0000 | [diff] [blame] | 120 | static void setGlobalOptionVisibility(llvm::GlobalValue *GV, |
| 121 | LangOptions::VisibilityMode Vis) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 122 | // Do not change the visibility of internal definitions. |
| 123 | if (GV->hasInternalLinkage()) |
| 124 | return; |
| 125 | |
Fariborz Jahanian | 7cd2e93 | 2009-04-03 03:28:57 +0000 | [diff] [blame] | 126 | switch (Vis) { |
| 127 | default: assert(0 && "Unknown visibility!"); |
| 128 | case LangOptions::NonVisibility: |
| 129 | break; |
| 130 | case LangOptions::DefaultVisibility: |
| 131 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 132 | break; |
| 133 | case LangOptions::HiddenVisibility: |
| 134 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 135 | break; |
| 136 | case LangOptions::ProtectedVisibility: |
| 137 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 143 | /// \brief Retrieves the mangled name for the given declaration. |
| 144 | /// |
| 145 | /// If the given declaration requires a mangled name, returns an |
Chris Lattner | c50689b | 2009-03-21 06:31:09 +0000 | [diff] [blame] | 146 | /// const char* containing the mangled name. Otherwise, returns |
| 147 | /// the unmangled name. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 148 | /// |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 149 | const char *CodeGenModule::getMangledName(const NamedDecl *ND) { |
Chris Lattner | c50689b | 2009-03-21 06:31:09 +0000 | [diff] [blame] | 150 | // In C, functions with no attributes never need to be mangled. Fastpath them. |
| 151 | if (!getLangOptions().CPlusPlus && !ND->hasAttrs()) { |
| 152 | assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); |
Chris Lattner | 3c8f153 | 2009-03-21 07:12:05 +0000 | [diff] [blame] | 153 | return ND->getNameAsCString(); |
Chris Lattner | c50689b | 2009-03-21 06:31:09 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 156 | llvm::SmallString<256> Name; |
| 157 | llvm::raw_svector_ostream Out(Name); |
Daniel Dunbar | fe34557 | 2009-03-05 22:59:19 +0000 | [diff] [blame] | 158 | if (!mangleName(ND, Context, Out)) { |
| 159 | assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); |
Chris Lattner | 3c8f153 | 2009-03-21 07:12:05 +0000 | [diff] [blame] | 160 | return ND->getNameAsCString(); |
Daniel Dunbar | fe34557 | 2009-03-05 22:59:19 +0000 | [diff] [blame] | 161 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 162 | |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 163 | Name += '\0'; |
Chris Lattner | 3c8f153 | 2009-03-21 07:12:05 +0000 | [diff] [blame] | 164 | return MangledNames.GetOrCreateValue(Name.begin(), Name.end()).getKeyData(); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 167 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 168 | /// main() runs. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 169 | void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 170 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 171 | GlobalCtors.push_back(std::make_pair(Ctor, Priority)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 174 | /// AddGlobalDtor - Add a function to the list that will be called |
| 175 | /// when the module is unloaded. |
| 176 | void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 177 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 178 | GlobalDtors.push_back(std::make_pair(Dtor, Priority)); |
| 179 | } |
| 180 | |
| 181 | void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { |
| 182 | // Ctor function type is void()*. |
| 183 | llvm::FunctionType* CtorFTy = |
| 184 | llvm::FunctionType::get(llvm::Type::VoidTy, |
| 185 | std::vector<const llvm::Type*>(), |
| 186 | false); |
| 187 | llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); |
| 188 | |
| 189 | // Get the type of a ctor entry, { i32, void ()* }. |
Chris Lattner | 572cf09 | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 190 | llvm::StructType* CtorStructTy = |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 191 | llvm::StructType::get(llvm::Type::Int32Ty, |
| 192 | llvm::PointerType::getUnqual(CtorFTy), NULL); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 193 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 194 | // Construct the constructor and destructor arrays. |
| 195 | std::vector<llvm::Constant*> Ctors; |
| 196 | for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
| 197 | std::vector<llvm::Constant*> S; |
| 198 | S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); |
| 199 | S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); |
| 200 | Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 201 | } |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 202 | |
| 203 | if (!Ctors.empty()) { |
| 204 | llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); |
| 205 | new llvm::GlobalVariable(AT, false, |
| 206 | llvm::GlobalValue::AppendingLinkage, |
| 207 | llvm::ConstantArray::get(AT, Ctors), |
| 208 | GlobalName, |
| 209 | &TheModule); |
| 210 | } |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 213 | void CodeGenModule::EmitAnnotations() { |
| 214 | if (Annotations.empty()) |
| 215 | return; |
| 216 | |
| 217 | // Create a new global variable for the ConstantStruct in the Module. |
| 218 | llvm::Constant *Array = |
| 219 | llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), |
| 220 | Annotations.size()), |
| 221 | Annotations); |
| 222 | llvm::GlobalValue *gv = |
| 223 | new llvm::GlobalVariable(Array->getType(), false, |
| 224 | llvm::GlobalValue::AppendingLinkage, Array, |
| 225 | "llvm.global.annotations", &TheModule); |
| 226 | gv->setSection("llvm.metadata"); |
| 227 | } |
| 228 | |
Daniel Dunbar | 5c61d97 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 229 | void CodeGenModule::SetGlobalValueAttributes(const Decl *D, |
| 230 | bool IsInternal, |
| 231 | bool IsInline, |
| 232 | llvm::GlobalValue *GV, |
| 233 | bool ForDefinition) { |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 234 | // FIXME: Set up linkage and many other things. Note, this is a simple |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 235 | // approximation of what we really want. |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 236 | if (!ForDefinition) { |
| 237 | // Only a few attributes are set on declarations. |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 238 | if (D->getAttr<DLLImportAttr>()) { |
| 239 | // The dllimport attribute is overridden by a subsequent declaration as |
| 240 | // dllexport. |
Sebastian Redl | 3ef5db6 | 2009-01-05 20:53:53 +0000 | [diff] [blame] | 241 | if (!D->getAttr<DLLExportAttr>()) { |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 242 | // dllimport attribute can be applied only to function decls, not to |
| 243 | // definitions. |
| 244 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 245 | if (!FD->getBody()) |
| 246 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 247 | } else |
| 248 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
Sebastian Redl | 3ef5db6 | 2009-01-05 20:53:53 +0000 | [diff] [blame] | 249 | } |
Daniel Dunbar | 5e27314 | 2009-03-06 16:20:49 +0000 | [diff] [blame] | 250 | } else if (D->getAttr<WeakAttr>() || |
| 251 | D->getAttr<WeakImportAttr>()) { |
| 252 | // "extern_weak" is overloaded in LLVM; we probably should have |
| 253 | // separate linkage types for this. |
Duncan Sands | e3fedbe | 2009-03-11 08:40:02 +0000 | [diff] [blame] | 254 | GV->setLinkage(llvm::Function::ExternalWeakLinkage); |
Daniel Dunbar | 5e27314 | 2009-03-06 16:20:49 +0000 | [diff] [blame] | 255 | } |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 256 | } else { |
| 257 | if (IsInternal) { |
| 258 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 259 | } else { |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 260 | if (D->getAttr<DLLExportAttr>()) { |
| 261 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 262 | // The dllexport attribute is ignored for undefined symbols. |
| 263 | if (FD->getBody()) |
| 264 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
| 265 | } else |
| 266 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
Daniel Dunbar | 5e27314 | 2009-03-06 16:20:49 +0000 | [diff] [blame] | 267 | } else if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>() || |
| 268 | IsInline) |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 269 | GV->setLinkage(llvm::Function::WeakAnyLinkage); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 270 | } |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 271 | } |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 272 | |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 273 | // FIXME: Figure out the relative priority of the attribute, |
| 274 | // -fvisibility, and private_extern. |
Daniel Dunbar | 03abc9e | 2009-04-07 22:36:33 +0000 | [diff] [blame] | 275 | if (ForDefinition) { |
| 276 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
| 277 | setGlobalVisibility(GV, attr->getVisibility()); |
| 278 | else |
| 279 | setGlobalOptionVisibility(GV, getLangOptions().getVisibilityMode()); |
| 280 | } |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 281 | |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 282 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
| 283 | GV->setSection(SA->getName()); |
Daniel Dunbar | 5c61d97 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 284 | |
| 285 | // Only add to llvm.used when we see a definition, otherwise we |
| 286 | // might add multiple times or risk the value being replaced by a |
| 287 | // subsequent RAUW. |
| 288 | if (ForDefinition) { |
| 289 | if (D->getAttr<UsedAttr>()) |
| 290 | AddUsedGlobal(GV); |
| 291 | } |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 294 | void CodeGenModule::SetFunctionAttributes(const Decl *D, |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 295 | const CGFunctionInfo &Info, |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 296 | llvm::Function *F) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 297 | AttributeListType AttributeList; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 298 | ConstructAttributeList(Info, D, AttributeList); |
Eli Friedman | c134fcb | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 299 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 300 | F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
| 301 | AttributeList.size())); |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 302 | |
| 303 | // Set the appropriate calling convention for the Function. |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 304 | if (D->getAttr<FastCallAttr>()) |
Anton Korobeynikov | f1c9c09 | 2008-11-11 20:21:14 +0000 | [diff] [blame] | 305 | F->setCallingConv(llvm::CallingConv::X86_FastCall); |
| 306 | |
| 307 | if (D->getAttr<StdCallAttr>()) |
| 308 | F->setCallingConv(llvm::CallingConv::X86_StdCall); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /// SetFunctionAttributesForDefinition - Set function attributes |
| 312 | /// specific to a function definition. |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 313 | void CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, |
| 314 | llvm::Function *F) { |
| 315 | if (isa<ObjCMethodDecl>(D)) { |
| 316 | SetGlobalValueAttributes(D, true, false, F, true); |
| 317 | } else { |
| 318 | const FunctionDecl *FD = cast<FunctionDecl>(D); |
| 319 | SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, |
| 320 | FD->isInline(), F, true); |
| 321 | } |
| 322 | |
Daniel Dunbar | 74ac74a | 2009-03-02 04:58:03 +0000 | [diff] [blame] | 323 | if (!Features.Exceptions && !Features.ObjCNonFragileABI) |
Daniel Dunbar | f93349f | 2008-09-27 07:16:42 +0000 | [diff] [blame] | 324 | F->addFnAttr(llvm::Attribute::NoUnwind); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 325 | |
| 326 | if (D->getAttr<AlwaysInlineAttr>()) |
| 327 | F->addFnAttr(llvm::Attribute::AlwaysInline); |
Anders Carlsson | 81ebbde | 2009-02-19 19:22:11 +0000 | [diff] [blame] | 328 | |
| 329 | if (D->getAttr<NoinlineAttr>()) |
| 330 | F->addFnAttr(llvm::Attribute::NoInline); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, |
| 334 | llvm::Function *F) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 335 | SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 336 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 337 | SetFunctionAttributesForDefinition(MD, F); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, |
| 341 | llvm::Function *F) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 342 | SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 343 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 344 | SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, |
| 345 | FD->isInline(), F, false); |
| 346 | } |
| 347 | |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 348 | void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { |
| 349 | assert(!GV->isDeclaration() && |
| 350 | "Only globals with definition can force usage."); |
Chris Lattner | 35f38a2 | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 351 | LLVMUsed.push_back(GV); |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void CodeGenModule::EmitLLVMUsed() { |
| 355 | // Don't create llvm.used if there is no need. |
| 356 | if (LLVMUsed.empty()) |
| 357 | return; |
| 358 | |
Chris Lattner | 35f38a2 | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 359 | llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 360 | llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, LLVMUsed.size()); |
| 361 | |
| 362 | // Convert LLVMUsed to what ConstantArray needs. |
| 363 | std::vector<llvm::Constant*> UsedArray; |
| 364 | UsedArray.resize(LLVMUsed.size()); |
| 365 | for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { |
| 366 | UsedArray[i] = |
| 367 | llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), i8PTy); |
| 368 | } |
| 369 | |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 370 | llvm::GlobalVariable *GV = |
| 371 | new llvm::GlobalVariable(ATy, false, |
| 372 | llvm::GlobalValue::AppendingLinkage, |
Chris Lattner | 35f38a2 | 2009-03-31 22:37:52 +0000 | [diff] [blame] | 373 | llvm::ConstantArray::get(ATy, UsedArray), |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 374 | "llvm.used", &getModule()); |
| 375 | |
| 376 | GV->setSection("llvm.metadata"); |
| 377 | } |
| 378 | |
| 379 | void CodeGenModule::EmitDeferred() { |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 380 | // Emit code for any potentially referenced deferred decls. Since a |
| 381 | // previously unused static decl may become used during the generation of code |
| 382 | // for a static function, iterate until no changes are made. |
| 383 | while (!DeferredDeclsToEmit.empty()) { |
| 384 | const ValueDecl *D = DeferredDeclsToEmit.back(); |
| 385 | DeferredDeclsToEmit.pop_back(); |
| 386 | |
| 387 | // The mangled name for the decl must have been emitted in GlobalDeclMap. |
| 388 | // Look it up to see if it was defined with a stronger definition (e.g. an |
| 389 | // extern inline function with a strong function redefinition). If so, |
| 390 | // just ignore the deferred decl. |
| 391 | llvm::GlobalValue *CGRef = GlobalDeclMap[getMangledName(D)]; |
| 392 | assert(CGRef && "Deferred decl wasn't referenced?"); |
Anders Carlsson | b723f75 | 2009-01-04 02:08:04 +0000 | [diff] [blame] | 393 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 394 | if (!CGRef->isDeclaration()) |
| 395 | continue; |
| 396 | |
| 397 | // Otherwise, emit the definition and move on to the next one. |
| 398 | EmitGlobalDefinition(D); |
| 399 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 402 | /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the |
| 403 | /// annotation information for a given GlobalValue. The annotation struct is |
| 404 | /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 405 | /// GlobalValue being annotated. The second field is the constant string |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 406 | /// created from the AnnotateAttr's annotation. The third field is a constant |
| 407 | /// string containing the name of the translation unit. The fourth field is |
| 408 | /// the line number in the file of the annotated value declaration. |
| 409 | /// |
| 410 | /// FIXME: this does not unique the annotation string constants, as llvm-gcc |
| 411 | /// appears to. |
| 412 | /// |
| 413 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
| 414 | const AnnotateAttr *AA, |
| 415 | unsigned LineNo) { |
| 416 | llvm::Module *M = &getModule(); |
| 417 | |
| 418 | // get [N x i8] constants for the annotation string, and the filename string |
| 419 | // which are the 2nd and 3rd elements of the global annotation structure. |
| 420 | const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 421 | llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); |
| 422 | llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), |
| 423 | true); |
| 424 | |
| 425 | // Get the two global values corresponding to the ConstantArrays we just |
| 426 | // created to hold the bytes of the strings. |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 427 | const char *StringPrefix = getContext().Target.getStringSymbolPrefix(true); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 428 | llvm::GlobalValue *annoGV = |
| 429 | new llvm::GlobalVariable(anno->getType(), false, |
| 430 | llvm::GlobalValue::InternalLinkage, anno, |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 431 | GV->getName() + StringPrefix, M); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 432 | // translation unit name string, emitted into the llvm.metadata section. |
| 433 | llvm::GlobalValue *unitGV = |
| 434 | new llvm::GlobalVariable(unit->getType(), false, |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 435 | llvm::GlobalValue::InternalLinkage, unit, |
| 436 | StringPrefix, M); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 437 | |
| 438 | // Create the ConstantStruct that is the global annotion. |
| 439 | llvm::Constant *Fields[4] = { |
| 440 | llvm::ConstantExpr::getBitCast(GV, SBP), |
| 441 | llvm::ConstantExpr::getBitCast(annoGV, SBP), |
| 442 | llvm::ConstantExpr::getBitCast(unitGV, SBP), |
| 443 | llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) |
| 444 | }; |
| 445 | return llvm::ConstantStruct::get(Fields, 4, false); |
| 446 | } |
| 447 | |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 448 | bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { |
Daniel Dunbar | 5c61d97 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 449 | // Never defer when EmitAllDecls is specified or the decl has |
| 450 | // attribute used. |
| 451 | if (Features.EmitAllDecls || Global->getAttr<UsedAttr>()) |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 452 | return false; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 453 | |
| 454 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 455 | // Constructors and destructors should never be deferred. |
| 456 | if (FD->getAttr<ConstructorAttr>() || FD->getAttr<DestructorAttr>()) |
| 457 | return false; |
| 458 | |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 459 | // FIXME: What about inline, and/or extern inline? |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 460 | if (FD->getStorageClass() != FunctionDecl::Static) |
| 461 | return false; |
| 462 | } else { |
| 463 | const VarDecl *VD = cast<VarDecl>(Global); |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 464 | assert(VD->isFileVarDecl() && "Invalid decl"); |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 465 | |
| 466 | if (VD->getStorageClass() != VarDecl::Static) |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | return true; |
| 471 | } |
| 472 | |
| 473 | void CodeGenModule::EmitGlobal(const ValueDecl *Global) { |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 474 | // If this is an alias definition (which otherwise looks like a declaration) |
| 475 | // emit it now. |
| 476 | if (Global->getAttr<AliasAttr>()) |
| 477 | return EmitAliasDefinition(Global); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 478 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 479 | // Ignore declarations, they will be emitted on their first use. |
Daniel Dunbar | 5e1e1f9 | 2009-03-19 08:27:24 +0000 | [diff] [blame] | 480 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 481 | // Forward declarations are emitted lazily on first use. |
| 482 | if (!FD->isThisDeclarationADefinition()) |
| 483 | return; |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 484 | } else { |
| 485 | const VarDecl *VD = cast<VarDecl>(Global); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 486 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
| 487 | |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 488 | // Forward declarations are emitted lazily on first use. |
Daniel Dunbar | 7542bca | 2009-02-13 22:49:13 +0000 | [diff] [blame] | 489 | if (!VD->getInit() && VD->hasExternalStorage()) |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 490 | return; |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 493 | // Defer code generation when possible if this is a static definition, inline |
| 494 | // function etc. These we only want to emit if they are used. |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 495 | if (MayDeferGeneration(Global)) { |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 496 | // If the value has already been used, add it directly to the |
| 497 | // DeferredDeclsToEmit list. |
| 498 | const char *MangledName = getMangledName(Global); |
| 499 | if (GlobalDeclMap.count(MangledName)) |
| 500 | DeferredDeclsToEmit.push_back(Global); |
| 501 | else { |
| 502 | // Otherwise, remember that we saw a deferred decl with this name. The |
| 503 | // first use of the mangled name will cause it to move into |
| 504 | // DeferredDeclsToEmit. |
| 505 | DeferredDecls[MangledName] = Global; |
| 506 | } |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 507 | return; |
| 508 | } |
| 509 | |
| 510 | // Otherwise emit the definition. |
| 511 | EmitGlobalDefinition(Global); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 514 | void CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { |
| 515 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 516 | EmitGlobalFunctionDefinition(FD); |
| 517 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 518 | EmitGlobalVarDefinition(VD); |
| 519 | } else { |
| 520 | assert(0 && "Invalid argument to EmitGlobalDefinition()"); |
| 521 | } |
| 522 | } |
| 523 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 524 | /// GetOrCreateLLVMFunction - If the specified mangled name is not in the |
| 525 | /// module, create and return an llvm Function with the specified type. If there |
| 526 | /// is something in the module with the specified name, return it potentially |
| 527 | /// bitcasted to the right type. |
| 528 | /// |
| 529 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 530 | /// to set the attributes on the function when it is first created. |
| 531 | llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName, |
| 532 | const llvm::Type *Ty, |
| 533 | const FunctionDecl *D) { |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 534 | // Lookup the entry, lazily creating it if necessary. |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 535 | llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; |
| 536 | if (Entry) { |
| 537 | if (Entry->getType()->getElementType() == Ty) |
| 538 | return Entry; |
| 539 | |
| 540 | // Make sure the result is of the correct type. |
| 541 | const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); |
| 542 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
| 543 | } |
| 544 | |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 545 | // This is the first use or definition of a mangled name. If there is a |
| 546 | // deferred decl with this name, remember that we need to emit it at the end |
| 547 | // of the file. |
| 548 | llvm::DenseMap<const char*, const ValueDecl*>::iterator DDI = |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 549 | DeferredDecls.find(MangledName); |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 550 | if (DDI != DeferredDecls.end()) { |
| 551 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 552 | // list, and remove it from DeferredDecls (since we don't need it anymore). |
| 553 | DeferredDeclsToEmit.push_back(DDI->second); |
| 554 | DeferredDecls.erase(DDI); |
| 555 | } |
| 556 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 557 | // This function doesn't have a complete type (for example, the return |
| 558 | // type is an incomplete struct). Use a fake type instead, and make |
| 559 | // sure not to try to set attributes. |
| 560 | bool ShouldSetAttributes = true; |
| 561 | if (!isa<llvm::FunctionType>(Ty)) { |
| 562 | Ty = llvm::FunctionType::get(llvm::Type::VoidTy, |
| 563 | std::vector<const llvm::Type*>(), false); |
| 564 | ShouldSetAttributes = false; |
| 565 | } |
| 566 | llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), |
| 567 | llvm::Function::ExternalLinkage, |
Chris Lattner | d972678 | 2009-03-22 00:12:30 +0000 | [diff] [blame] | 568 | "", &getModule()); |
| 569 | F->setName(MangledName); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 570 | if (D && ShouldSetAttributes) |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 571 | SetFunctionAttributes(D, F); |
| 572 | Entry = F; |
| 573 | return F; |
| 574 | } |
| 575 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 576 | /// GetAddrOfFunction - Return the address of the given function. If Ty is |
| 577 | /// non-null, then this function will use the specified type if it has to |
| 578 | /// create it (this occurs when we see a definition of the function). |
| 579 | llvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D, |
| 580 | const llvm::Type *Ty) { |
| 581 | // If there was no specific requested type, just convert it now. |
| 582 | if (!Ty) |
| 583 | Ty = getTypes().ConvertType(D->getType()); |
| 584 | return GetOrCreateLLVMFunction(getMangledName(D), Ty, D); |
| 585 | } |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 587 | /// CreateRuntimeFunction - Create a new runtime function with the specified |
| 588 | /// type and name. |
| 589 | llvm::Constant * |
| 590 | CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, |
| 591 | const char *Name) { |
| 592 | // Convert Name to be a uniqued string from the IdentifierInfo table. |
| 593 | Name = getContext().Idents.get(Name).getName(); |
| 594 | return GetOrCreateLLVMFunction(Name, FTy, 0); |
| 595 | } |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 597 | /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, |
| 598 | /// create and return an llvm GlobalVariable with the specified type. If there |
| 599 | /// is something in the module with the specified name, return it potentially |
| 600 | /// bitcasted to the right type. |
| 601 | /// |
| 602 | /// If D is non-null, it specifies a decl that correspond to this. This is used |
| 603 | /// to set the attributes on the global when it is first created. |
| 604 | llvm::Constant *CodeGenModule::GetOrCreateLLVMGlobal(const char *MangledName, |
| 605 | const llvm::PointerType*Ty, |
| 606 | const VarDecl *D) { |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 607 | // Lookup the entry, lazily creating it if necessary. |
Chris Lattner | 5d4f5c7 | 2009-03-21 08:06:59 +0000 | [diff] [blame] | 608 | llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 609 | if (Entry) { |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 610 | if (Entry->getType() == Ty) |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 611 | return Entry; |
| 612 | |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 613 | // Make sure the result is of the correct type. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 614 | return llvm::ConstantExpr::getBitCast(Entry, Ty); |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 615 | } |
Chris Lattner | 67b0052 | 2009-03-21 09:44:56 +0000 | [diff] [blame] | 616 | |
| 617 | // This is the first use or definition of a mangled name. If there is a |
| 618 | // deferred decl with this name, remember that we need to emit it at the end |
| 619 | // of the file. |
| 620 | llvm::DenseMap<const char*, const ValueDecl*>::iterator DDI = |
| 621 | DeferredDecls.find(MangledName); |
| 622 | if (DDI != DeferredDecls.end()) { |
| 623 | // Move the potentially referenced deferred decl to the DeferredDeclsToEmit |
| 624 | // list, and remove it from DeferredDecls (since we don't need it anymore). |
| 625 | DeferredDeclsToEmit.push_back(DDI->second); |
| 626 | DeferredDecls.erase(DDI); |
| 627 | } |
| 628 | |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 629 | llvm::GlobalVariable *GV = |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 630 | new llvm::GlobalVariable(Ty->getElementType(), false, |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 631 | llvm::GlobalValue::ExternalLinkage, |
Chris Lattner | d972678 | 2009-03-22 00:12:30 +0000 | [diff] [blame] | 632 | 0, "", &getModule(), |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 633 | 0, Ty->getAddressSpace()); |
Chris Lattner | d972678 | 2009-03-22 00:12:30 +0000 | [diff] [blame] | 634 | GV->setName(MangledName); |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 635 | |
| 636 | // Handle things which are present even on external declarations. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 637 | if (D) { |
| 638 | // FIXME: This code is overly simple and should be merged with |
| 639 | // other global handling. |
| 640 | GV->setConstant(D->getType().isConstant(Context)); |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 642 | // FIXME: Merge with other attribute handling code. |
| 643 | if (D->getStorageClass() == VarDecl::PrivateExtern) |
| 644 | setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility); |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 646 | if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>()) |
| 647 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 648 | } |
| 649 | |
Chris Lattner | 99b5361 | 2009-03-21 08:03:33 +0000 | [diff] [blame] | 650 | return Entry = GV; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 653 | |
| 654 | /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the |
| 655 | /// given global variable. If Ty is non-null and if the global doesn't exist, |
| 656 | /// then it will be greated with the specified type instead of whatever the |
| 657 | /// normal requested type would be. |
| 658 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, |
| 659 | const llvm::Type *Ty) { |
| 660 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 661 | QualType ASTTy = D->getType(); |
| 662 | if (Ty == 0) |
| 663 | Ty = getTypes().ConvertTypeForMem(ASTTy); |
| 664 | |
| 665 | const llvm::PointerType *PTy = |
| 666 | llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
| 667 | return GetOrCreateLLVMGlobal(getMangledName(D), PTy, D); |
| 668 | } |
| 669 | |
| 670 | /// CreateRuntimeVariable - Create a new runtime global variable with the |
| 671 | /// specified type and name. |
| 672 | llvm::Constant * |
| 673 | CodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, |
| 674 | const char *Name) { |
| 675 | // Convert Name to be a uniqued string from the IdentifierInfo table. |
| 676 | Name = getContext().Idents.get(Name).getName(); |
| 677 | return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0); |
| 678 | } |
| 679 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 680 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 681 | llvm::Constant *Init = 0; |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 682 | QualType ASTTy = D->getType(); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 683 | |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 684 | if (D->getInit() == 0) { |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 685 | // This is a tentative definition; tentative definitions are |
| 686 | // implicitly initialized with { 0 } |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 687 | const llvm::Type *InitTy = getTypes().ConvertTypeForMem(ASTTy); |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 688 | if (ASTTy->isIncompleteArrayType()) { |
| 689 | // An incomplete array is normally [ TYPE x 0 ], but we need |
| 690 | // to fix it to [ TYPE x 1 ]. |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 691 | const llvm::ArrayType* ATy = cast<llvm::ArrayType>(InitTy); |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 692 | InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 693 | } |
| 694 | Init = llvm::Constant::getNullValue(InitTy); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 695 | } else { |
Anders Carlsson | e9352cc | 2009-04-08 04:48:15 +0000 | [diff] [blame] | 696 | Init = EmitConstantExpr(D->getInit(), D->getType()); |
Eli Friedman | 6e656f4 | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 697 | if (!Init) { |
Daniel Dunbar | 232350d | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 698 | ErrorUnsupported(D, "static initializer"); |
Eli Friedman | 6e656f4 | 2009-02-20 01:18:21 +0000 | [diff] [blame] | 699 | QualType T = D->getInit()->getType(); |
| 700 | Init = llvm::UndefValue::get(getTypes().ConvertType(T)); |
| 701 | } |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 702 | } |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 703 | |
Chris Lattner | 2d58406 | 2009-03-21 08:13:05 +0000 | [diff] [blame] | 704 | const llvm::Type* InitType = Init->getType(); |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 705 | llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 706 | |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 707 | // Strip off a bitcast if we got one back. |
| 708 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
| 709 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
| 710 | Entry = CE->getOperand(0); |
| 711 | } |
| 712 | |
| 713 | // Entry is now either a Function or GlobalVariable. |
| 714 | llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); |
| 715 | |
| 716 | // If we already have this global and it has an initializer, then |
| 717 | // we are in the rare situation where we emitted the defining |
| 718 | // declaration of the global and are now being asked to emit a |
| 719 | // definition which would be common. This occurs, for example, in |
| 720 | // the following situation because statics can be emitted out of |
| 721 | // order: |
| 722 | // |
| 723 | // static int x; |
| 724 | // static int *y = &x; |
| 725 | // static int x = 10; |
| 726 | // int **z = &y; |
| 727 | // |
| 728 | // Bail here so we don't blow away the definition. Note that if we |
| 729 | // can't distinguish here if we emitted a definition with a null |
| 730 | // initializer, but this case is safe. |
| 731 | if (GV && GV->hasInitializer() && !GV->getInitializer()->isNullValue()) { |
Daniel Dunbar | 232350d | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 732 | assert(!D->getInit() && "Emitting multiple definitions of a decl!"); |
| 733 | return; |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | // We have a definition after a declaration with the wrong type. |
| 737 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 738 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 739 | // (which will be a definition). |
| 740 | // |
| 741 | // This happens if there is a prototype for a global (e.g. |
| 742 | // "extern int x[];") and then a definition of a different type (e.g. |
| 743 | // "int x[10];"). This also happens when an initializer has a different type |
| 744 | // from the type of the global (this happens with unions). |
| 745 | // |
| 746 | // FIXME: This also ends up happening if there's a definition followed by |
| 747 | // a tentative definition! (Although Sema rejects that construct |
| 748 | // at the moment.) |
| 749 | if (GV == 0 || |
| 750 | GV->getType()->getElementType() != InitType || |
| 751 | GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { |
| 752 | |
| 753 | // Remove the old entry from GlobalDeclMap so that we'll create a new one. |
| 754 | GlobalDeclMap.erase(getMangledName(D)); |
Daniel Dunbar | 232350d | 2009-02-19 05:36:41 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 756 | // Make a new global with the correct type, this is now guaranteed to work. |
| 757 | GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 758 | GV->takeName(cast<llvm::GlobalValue>(Entry)); |
| 759 | |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 760 | // Replace all uses of the old global with the new global |
| 761 | llvm::Constant *NewPtrForOldDecl = |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 762 | llvm::ConstantExpr::getBitCast(GV, Entry->getType()); |
| 763 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 764 | |
| 765 | // Erase the old global, since it is no longer used. |
Chris Lattner | 570585c | 2009-03-21 09:16:30 +0000 | [diff] [blame] | 766 | cast<llvm::GlobalValue>(Entry)->eraseFromParent(); |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 767 | } |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 768 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 769 | if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { |
| 770 | SourceManager &SM = Context.getSourceManager(); |
| 771 | AddAnnotation(EmitAnnotateAttr(GV, AA, |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 772 | SM.getInstantiationLineNumber(D->getLocation()))); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 775 | GV->setInitializer(Init); |
Nuno Lopes | b381aac | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 776 | GV->setConstant(D->getType().isConstant(Context)); |
Eli Friedman | 0de40af | 2009-02-27 04:11:37 +0000 | [diff] [blame] | 777 | GV->setAlignment(getContext().getDeclAlignInBytes(D)); |
Eli Friedman | 08d7802 | 2008-05-29 11:10:27 +0000 | [diff] [blame] | 778 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 779 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 780 | setGlobalVisibility(GV, attr->getVisibility()); |
Fariborz Jahanian | 7cd2e93 | 2009-04-03 03:28:57 +0000 | [diff] [blame] | 781 | else |
| 782 | setGlobalOptionVisibility(GV, getLangOptions().getVisibilityMode()); |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 784 | // Set the llvm linkage type as appropriate. |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 785 | if (D->getStorageClass() == VarDecl::Static) |
| 786 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 787 | else if (D->getAttr<DLLImportAttr>()) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 788 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 789 | else if (D->getAttr<DLLExportAttr>()) |
| 790 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
Daniel Dunbar | 5e27314 | 2009-03-06 16:20:49 +0000 | [diff] [blame] | 791 | else if (D->getAttr<WeakAttr>() || D->getAttr<WeakImportAttr>()) |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 792 | GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 793 | else { |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 794 | // FIXME: This isn't right. This should handle common linkage and other |
| 795 | // stuff. |
| 796 | switch (D->getStorageClass()) { |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 797 | case VarDecl::Static: assert(0 && "This case handled above"); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 798 | case VarDecl::Auto: |
| 799 | case VarDecl::Register: |
| 800 | assert(0 && "Can't have auto or register globals"); |
| 801 | case VarDecl::None: |
Chris Lattner | bd36064 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 802 | if (!D->getInit() && !CompileOpts.NoCommon) |
Duncan Sands | ceb77d9 | 2009-03-11 20:15:27 +0000 | [diff] [blame] | 803 | GV->setLinkage(llvm::GlobalVariable::CommonLinkage); |
Anders Carlsson | 98883e1 | 2008-12-03 05:51:23 +0000 | [diff] [blame] | 804 | else |
| 805 | GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 806 | break; |
| 807 | case VarDecl::Extern: |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 808 | // FIXME: common |
| 809 | break; |
| 810 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 811 | case VarDecl::PrivateExtern: |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 812 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 813 | // FIXME: common |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 814 | break; |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 815 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 816 | } |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 817 | |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 818 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
| 819 | GV->setSection(SA->getName()); |
| 820 | |
Daniel Dunbar | 5c61d97 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 821 | if (D->getAttr<UsedAttr>()) |
| 822 | AddUsedGlobal(GV); |
| 823 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 824 | // Emit global variable debug information. |
Chris Lattner | 2d58406 | 2009-03-21 08:13:05 +0000 | [diff] [blame] | 825 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 826 | DI->setLocation(D->getLocation()); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 827 | DI->EmitGlobalVariable(GV, D); |
| 828 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 829 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 830 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 831 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 832 | void CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 833 | const llvm::FunctionType *Ty; |
Daniel Dunbar | d5d3180 | 2009-02-19 07:15:39 +0000 | [diff] [blame] | 834 | |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 835 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 836 | bool isVariadic = D->getType()->getAsFunctionProtoType()->isVariadic(); |
| 837 | |
| 838 | Ty = getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), isVariadic); |
| 839 | } else { |
| 840 | Ty = cast<llvm::FunctionType>(getTypes().ConvertType(D->getType())); |
| 841 | |
| 842 | // As a special case, make sure that definitions of K&R function |
| 843 | // "type foo()" aren't declared as varargs (which forces the backend |
| 844 | // to do unnecessary work). |
| 845 | if (D->getType()->isFunctionNoProtoType()) { |
| 846 | assert(Ty->isVarArg() && "Didn't lower type as expected"); |
| 847 | // Due to stret, the lowered function could have arguments. |
| 848 | // Just create the same type as was lowered by ConvertType |
| 849 | // but strip off the varargs bit. |
| 850 | std::vector<const llvm::Type*> Args(Ty->param_begin(), Ty->param_end()); |
| 851 | Ty = llvm::FunctionType::get(Ty->getReturnType(), Args, false); |
| 852 | } |
Chris Lattner | ff75e1d | 2009-03-22 19:35:37 +0000 | [diff] [blame] | 853 | } |
Daniel Dunbar | d5d3180 | 2009-02-19 07:15:39 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 3480950 | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 855 | // Get or create the prototype for teh function. |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 856 | llvm::Constant *Entry = GetAddrOfFunction(D, Ty); |
Chris Lattner | 3480950 | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 857 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 858 | // Strip off a bitcast if we got one back. |
| 859 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
| 860 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
| 861 | Entry = CE->getOperand(0); |
| 862 | } |
| 863 | |
| 864 | |
| 865 | if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { |
Daniel Dunbar | 4274581 | 2009-03-09 23:53:08 +0000 | [diff] [blame] | 866 | // If the types mismatch then we have to rewrite the definition. |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 867 | assert(cast<llvm::GlobalValue>(Entry)->isDeclaration() && |
| 868 | "Shouldn't replace non-declaration"); |
Chris Lattner | 3480950 | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 869 | |
Chris Lattner | 62b33ea | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 870 | // F is the Function* for the one with the wrong type, we must make a new |
| 871 | // Function* and update everything that used F (a declaration) with the new |
| 872 | // Function* (which will be a definition). |
| 873 | // |
| 874 | // This happens if there is a prototype for a function |
| 875 | // (e.g. "int f()") and then a definition of a different type |
| 876 | // (e.g. "int f(int x)"). Start by making a new function of the |
| 877 | // correct type, RAUW, then steal the name. |
Chris Lattner | 3480950 | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 878 | GlobalDeclMap.erase(getMangledName(D)); |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 879 | llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(D, Ty)); |
| 880 | NewFn->takeName(cast<llvm::GlobalValue>(Entry)); |
Chris Lattner | 62b33ea | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 881 | |
| 882 | // Replace uses of F with the Function we will endow with a body. |
| 883 | llvm::Constant *NewPtrForOldDecl = |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 884 | llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); |
| 885 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
Chris Lattner | 62b33ea | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 886 | |
| 887 | // Ok, delete the old function now, which is dead. |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 888 | cast<llvm::GlobalValue>(Entry)->eraseFromParent(); |
Chris Lattner | 62b33ea | 2009-03-21 08:38:50 +0000 | [diff] [blame] | 889 | |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 890 | Entry = NewFn; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 891 | } |
Chris Lattner | 0558e79 | 2009-03-21 09:25:43 +0000 | [diff] [blame] | 892 | |
| 893 | llvm::Function *Fn = cast<llvm::Function>(Entry); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 894 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 895 | CodeGenFunction(*this).GenerateCode(D, Fn); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 896 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 897 | SetFunctionAttributesForDefinition(D, Fn); |
| 898 | |
Chris Lattner | 3480950 | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 899 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 900 | AddGlobalCtor(Fn, CA->getPriority()); |
Chris Lattner | 3480950 | 2009-03-21 08:53:37 +0000 | [diff] [blame] | 901 | if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 902 | AddGlobalDtor(Fn, DA->getPriority()); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 905 | void CodeGenModule::EmitAliasDefinition(const ValueDecl *D) { |
| 906 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
| 907 | assert(AA && "Not an alias?"); |
| 908 | |
| 909 | const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); |
| 910 | |
| 911 | // Unique the name through the identifier table. |
| 912 | const char *AliaseeName = AA->getAliasee().c_str(); |
| 913 | AliaseeName = getContext().Idents.get(AliaseeName).getName(); |
| 914 | |
| 915 | // Create a reference to the named value. This ensures that it is emitted |
| 916 | // if a deferred decl. |
| 917 | llvm::Constant *Aliasee; |
| 918 | if (isa<llvm::FunctionType>(DeclTy)) |
| 919 | Aliasee = GetOrCreateLLVMFunction(AliaseeName, DeclTy, 0); |
| 920 | else |
| 921 | Aliasee = GetOrCreateLLVMGlobal(AliaseeName, |
| 922 | llvm::PointerType::getUnqual(DeclTy), 0); |
| 923 | |
| 924 | // Create the new alias itself, but don't set a name yet. |
| 925 | llvm::GlobalValue *GA = |
| 926 | new llvm::GlobalAlias(Aliasee->getType(), |
| 927 | llvm::Function::ExternalLinkage, |
| 928 | "", Aliasee, &getModule()); |
| 929 | |
| 930 | // See if there is already something with the alias' name in the module. |
| 931 | const char *MangledName = getMangledName(D); |
| 932 | llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; |
| 933 | |
| 934 | if (Entry && !Entry->isDeclaration()) { |
| 935 | // If there is a definition in the module, then it wins over the alias. |
| 936 | // This is dubious, but allow it to be safe. Just ignore the alias. |
| 937 | GA->eraseFromParent(); |
| 938 | return; |
| 939 | } |
| 940 | |
| 941 | if (Entry) { |
| 942 | // If there is a declaration in the module, then we had an extern followed |
| 943 | // by the alias, as in: |
| 944 | // extern int test6(); |
| 945 | // ... |
| 946 | // int test6() __attribute__((alias("test7"))); |
| 947 | // |
| 948 | // Remove it and replace uses of it with the alias. |
| 949 | |
| 950 | Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, |
| 951 | Entry->getType())); |
Chris Lattner | bd53271 | 2009-03-22 21:47:11 +0000 | [diff] [blame] | 952 | Entry->eraseFromParent(); |
| 953 | } |
| 954 | |
| 955 | // Now we know that there is no conflict, set the name. |
| 956 | Entry = GA; |
| 957 | GA->setName(MangledName); |
| 958 | |
| 959 | // Alias should never be internal or inline. |
| 960 | SetGlobalValueAttributes(D, false, false, GA, true); |
| 961 | } |
| 962 | |
Chris Lattner | b808c95 | 2009-03-22 21:56:56 +0000 | [diff] [blame] | 963 | /// getBuiltinLibFunction - Given a builtin id for a function like |
| 964 | /// "__builtin_fabsf", return a Function* for "fabsf". |
Mike Stump | c136e6c | 2009-02-27 22:42:30 +0000 | [diff] [blame] | 965 | llvm::Value *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 966 | assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || |
| 967 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && |
| 968 | "isn't a lib fn"); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 969 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 970 | // Get the name, skip over the __builtin_ prefix (if necessary). |
| 971 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID); |
| 972 | if (Context.BuiltinInfo.isLibFunction(BuiltinID)) |
| 973 | Name += 10; |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 974 | |
| 975 | // Get the type for the builtin. |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 976 | Builtin::Context::GetBuiltinTypeError Error; |
| 977 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context, Error); |
| 978 | assert(Error == Builtin::Context::GE_None && "Can't get builtin type"); |
| 979 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 980 | const llvm::FunctionType *Ty = |
| 981 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 982 | |
Chris Lattner | b808c95 | 2009-03-22 21:56:56 +0000 | [diff] [blame] | 983 | // Unique the name through the identifier table. |
| 984 | Name = getContext().Idents.get(Name).getName(); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 985 | // FIXME: param attributes for sext/zext etc. |
Chris Lattner | b808c95 | 2009-03-22 21:56:56 +0000 | [diff] [blame] | 986 | return GetOrCreateLLVMFunction(Name, Ty, 0); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 989 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, |
| 990 | unsigned NumTys) { |
| 991 | return llvm::Intrinsic::getDeclaration(&getModule(), |
| 992 | (llvm::Intrinsic::ID)IID, Tys, NumTys); |
| 993 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 994 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 995 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 996 | if (MemCpyFn) return MemCpyFn; |
Chris Lattner | 4e8a9e8 | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 997 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); |
| 998 | return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 999 | } |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1000 | |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 1001 | llvm::Function *CodeGenModule::getMemMoveFn() { |
| 1002 | if (MemMoveFn) return MemMoveFn; |
Chris Lattner | 4e8a9e8 | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 1003 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); |
| 1004 | return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 1007 | llvm::Function *CodeGenModule::getMemSetFn() { |
| 1008 | if (MemSetFn) return MemSetFn; |
Chris Lattner | 4e8a9e8 | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 1009 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); |
| 1010 | return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 1011 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 1012 | |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1013 | static void appendFieldAndPadding(CodeGenModule &CGM, |
| 1014 | std::vector<llvm::Constant*>& Fields, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1015 | FieldDecl *FieldD, FieldDecl *NextFieldD, |
| 1016 | llvm::Constant* Field, |
Chris Lattner | 3c8f153 | 2009-03-21 07:12:05 +0000 | [diff] [blame] | 1017 | RecordDecl* RD, const llvm::StructType *STy) { |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1018 | // Append the field. |
| 1019 | Fields.push_back(Field); |
| 1020 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1021 | int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1022 | |
| 1023 | int NextStructFieldNo; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1024 | if (!NextFieldD) { |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1025 | NextStructFieldNo = STy->getNumElements(); |
| 1026 | } else { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1027 | NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | // Append padding |
| 1031 | for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { |
| 1032 | llvm::Constant *C = |
| 1033 | llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); |
| 1034 | |
| 1035 | Fields.push_back(C); |
| 1036 | } |
| 1037 | } |
| 1038 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 1039 | llvm::Constant *CodeGenModule:: |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 1040 | GetAddrOfConstantCFString(const StringLiteral *Literal) { |
Steve Naroff | b59212a | 2009-04-01 21:16:31 +0000 | [diff] [blame] | 1041 | std::string str; |
| 1042 | unsigned StringLength; |
| 1043 | |
Steve Naroff | e9b7d8a | 2009-04-01 15:50:34 +0000 | [diff] [blame] | 1044 | bool isUTF16 = false; |
| 1045 | if (Literal->containsNonAsciiOrNull()) { |
| 1046 | // Convert from UTF-8 to UTF-16. |
| 1047 | llvm::SmallVector<UTF16, 128> ToBuf(Literal->getByteLength()); |
| 1048 | const UTF8 *FromPtr = (UTF8 *)Literal->getStrData(); |
| 1049 | UTF16 *ToPtr = &ToBuf[0]; |
| 1050 | |
| 1051 | ConversionResult Result; |
| 1052 | Result = ConvertUTF8toUTF16(&FromPtr, FromPtr+Literal->getByteLength(), |
| 1053 | &ToPtr, ToPtr+Literal->getByteLength(), |
| 1054 | strictConversion); |
| 1055 | assert(Result == conversionOK && "UTF-8 to UTF-16 conversion failed"); |
Steve Naroff | b59212a | 2009-04-01 21:16:31 +0000 | [diff] [blame] | 1056 | |
Steve Naroff | db6d4f6 | 2009-04-03 09:44:50 +0000 | [diff] [blame] | 1057 | // FIXME: Storing UTF-16 in a C string is a hack to test Unicode strings |
| 1058 | // without doing more surgery to this routine. Since we aren't explicitly |
| 1059 | // checking for endianness here, it's also a bug (when generating code for |
| 1060 | // a target that doesn't match the host endianness). Modeling this as an i16 |
| 1061 | // array is likely the cleanest solution. |
Steve Naroff | b59212a | 2009-04-01 21:16:31 +0000 | [diff] [blame] | 1062 | StringLength = ToPtr-&ToBuf[0]; |
| 1063 | str.assign((char *)&ToBuf[0], StringLength*2); // Twice as many UTF8 chars. |
Steve Naroff | e9b7d8a | 2009-04-01 15:50:34 +0000 | [diff] [blame] | 1064 | isUTF16 = true; |
Steve Naroff | b59212a | 2009-04-01 21:16:31 +0000 | [diff] [blame] | 1065 | } else { |
| 1066 | str.assign(Literal->getStrData(), Literal->getByteLength()); |
| 1067 | StringLength = str.length(); |
Steve Naroff | e9b7d8a | 2009-04-01 15:50:34 +0000 | [diff] [blame] | 1068 | } |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1069 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 1070 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 1071 | |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1072 | if (llvm::Constant *C = Entry.getValue()) |
| 1073 | return C; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1074 | |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1075 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 1076 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1077 | |
| 1078 | if (!CFConstantStringClassRef) { |
| 1079 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 1080 | Ty = llvm::ArrayType::get(Ty, 0); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1081 | |
| 1082 | // FIXME: This is fairly broken if |
| 1083 | // __CFConstantStringClassReference is already defined, in that it |
| 1084 | // will get renamed and the user will most likely see an opaque |
| 1085 | // error message. This is a general issue with relying on |
| 1086 | // particular names. |
| 1087 | llvm::GlobalVariable *GV = |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1088 | new llvm::GlobalVariable(Ty, false, |
| 1089 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 1090 | "__CFConstantStringClassReference", |
| 1091 | &getModule()); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1092 | |
| 1093 | // Decay array -> ptr |
| 1094 | CFConstantStringClassRef = |
| 1095 | llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1098 | QualType CFTy = getContext().getCFConstantStringType(); |
| 1099 | RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1100 | |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1101 | const llvm::StructType *STy = |
| 1102 | cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
| 1103 | |
| 1104 | std::vector<llvm::Constant*> Fields; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1105 | RecordDecl::field_iterator Field = CFRD->field_begin(getContext()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1106 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1107 | // Class pointer. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1108 | FieldDecl *CurField = *Field++; |
| 1109 | FieldDecl *NextField = *Field++; |
| 1110 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
| 1111 | CFConstantStringClassRef, CFRD, STy); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Flags. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1114 | CurField = NextField; |
| 1115 | NextField = *Field++; |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1116 | const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1117 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
Steve Naroff | e9b7d8a | 2009-04-01 15:50:34 +0000 | [diff] [blame] | 1118 | isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) |
| 1119 | : llvm::ConstantInt::get(Ty, 0x07C8), |
| 1120 | CFRD, STy); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1121 | |
| 1122 | // String pointer. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1123 | CurField = NextField; |
| 1124 | NextField = *Field++; |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1125 | llvm::Constant *C = llvm::ConstantArray::get(str); |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1126 | |
| 1127 | const char *Sect, *Prefix; |
| 1128 | bool isConstant; |
| 1129 | if (isUTF16) { |
| 1130 | Prefix = getContext().Target.getUnicodeStringSymbolPrefix(); |
| 1131 | Sect = getContext().Target.getUnicodeStringSection(); |
| 1132 | // FIXME: Why does GCC not set constant here? |
| 1133 | isConstant = false; |
| 1134 | } else { |
| 1135 | Prefix = getContext().Target.getStringSymbolPrefix(true); |
| 1136 | Sect = getContext().Target.getCFStringDataSection(); |
| 1137 | // FIXME: -fwritable-strings should probably affect this, but we |
| 1138 | // are following gcc here. |
| 1139 | isConstant = true; |
| 1140 | } |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1141 | llvm::GlobalVariable *GV = |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1142 | new llvm::GlobalVariable(C->getType(), isConstant, |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1143 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1144 | C, Prefix, &getModule()); |
| 1145 | if (Sect) |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1146 | GV->setSection(Sect); |
Daniel Dunbar | a9668e0 | 2009-04-03 00:57:44 +0000 | [diff] [blame] | 1147 | if (isUTF16) { |
| 1148 | unsigned Align = getContext().getTypeAlign(getContext().ShortTy)/8; |
| 1149 | GV->setAlignment(Align); |
| 1150 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1151 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1152 | llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2), |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1153 | CFRD, STy); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1154 | |
| 1155 | // String length. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1156 | CurField = NextField; |
| 1157 | NextField = 0; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1158 | Ty = getTypes().ConvertType(getContext().LongTy); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1159 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
Steve Naroff | b59212a | 2009-04-01 21:16:31 +0000 | [diff] [blame] | 1160 | llvm::ConstantInt::get(Ty, StringLength), CFRD, STy); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1161 | |
| 1162 | // The struct. |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1163 | C = llvm::ConstantStruct::get(STy, Fields); |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1164 | GV = new llvm::GlobalVariable(C->getType(), true, |
| 1165 | llvm::GlobalVariable::InternalLinkage, C, |
| 1166 | getContext().Target.getCFStringSymbolPrefix(), |
| 1167 | &getModule()); |
| 1168 | if (const char *Sect = getContext().Target.getCFStringSection()) |
| 1169 | GV->setSection(Sect); |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 1170 | Entry.setValue(GV); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1171 | |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 1172 | return GV; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1173 | } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1174 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1175 | /// GetStringForStringLiteral - Return the appropriate bytes for a |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1176 | /// string literal, properly padded to match the literal type. |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1177 | std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1178 | const char *StrData = E->getStrData(); |
| 1179 | unsigned Len = E->getByteLength(); |
| 1180 | |
| 1181 | const ConstantArrayType *CAT = |
| 1182 | getContext().getAsConstantArrayType(E->getType()); |
| 1183 | assert(CAT && "String isn't pointer or array!"); |
| 1184 | |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1185 | // Resize the string to the right size. |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1186 | std::string Str(StrData, StrData+Len); |
| 1187 | uint64_t RealLen = CAT->getSize().getZExtValue(); |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1188 | |
| 1189 | if (E->isWide()) |
| 1190 | RealLen *= getContext().Target.getWCharWidth()/8; |
| 1191 | |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1192 | Str.resize(RealLen, '\0'); |
| 1193 | |
| 1194 | return Str; |
| 1195 | } |
| 1196 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1197 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 1198 | /// constant array for the given string literal. |
| 1199 | llvm::Constant * |
| 1200 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { |
| 1201 | // FIXME: This can be more efficient. |
| 1202 | return GetAddrOfConstantString(GetStringForStringLiteral(S)); |
| 1203 | } |
| 1204 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1205 | /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant |
| 1206 | /// array for the given ObjCEncodeExpr node. |
| 1207 | llvm::Constant * |
| 1208 | CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { |
| 1209 | std::string Str; |
| 1210 | getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
Eli Friedman | a210f35 | 2009-03-07 20:17:55 +0000 | [diff] [blame] | 1211 | |
| 1212 | return GetAddrOfConstantCString(Str); |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1216 | /// GenerateWritableString -- Creates storage for a string literal. |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1217 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
| 1218 | bool constant, |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1219 | CodeGenModule &CGM, |
| 1220 | const char *GlobalName) { |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1221 | // Create Constant for this string literal. Don't add a '\0'. |
| 1222 | llvm::Constant *C = llvm::ConstantArray::get(str, false); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1223 | |
| 1224 | // Create a global variable for this string |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1225 | return new llvm::GlobalVariable(C->getType(), constant, |
| 1226 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1227 | C, GlobalName, &CGM.getModule()); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1230 | /// GetAddrOfConstantString - Returns a pointer to a character array |
| 1231 | /// containing the literal. This contents are exactly that of the |
| 1232 | /// given string, i.e. it will not be null terminated automatically; |
| 1233 | /// see GetAddrOfConstantCString. Note that whether the result is |
| 1234 | /// actually a pointer to an LLVM constant depends on |
| 1235 | /// Feature.WriteableStrings. |
| 1236 | /// |
| 1237 | /// The result has pointer to array type. |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1238 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, |
| 1239 | const char *GlobalName) { |
Daniel Dunbar | 8e5c2b8 | 2009-03-31 23:42:16 +0000 | [diff] [blame] | 1240 | bool IsConstant = !Features.WritableStrings; |
| 1241 | |
| 1242 | // Get the default prefix if a name wasn't specified. |
| 1243 | if (!GlobalName) |
| 1244 | GlobalName = getContext().Target.getStringSymbolPrefix(IsConstant); |
| 1245 | |
| 1246 | // Don't share any string literals if strings aren't constant. |
| 1247 | if (!IsConstant) |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1248 | return GenerateStringLiteral(str, false, *this, GlobalName); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1249 | |
| 1250 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 1251 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 1252 | |
| 1253 | if (Entry.getValue()) |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1254 | return Entry.getValue(); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1255 | |
| 1256 | // Create a global variable for this. |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1257 | llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1258 | Entry.setValue(C); |
| 1259 | return C; |
| 1260 | } |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1261 | |
| 1262 | /// GetAddrOfConstantCString - Returns a pointer to a character |
| 1263 | /// array containing the literal and a terminating '\-' |
| 1264 | /// character. The result has pointer to array type. |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1265 | llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, |
| 1266 | const char *GlobalName){ |
Chris Lattner | c9f29c6 | 2008-12-09 19:10:54 +0000 | [diff] [blame] | 1267 | return GetAddrOfConstantString(str + '\0', GlobalName); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1268 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1269 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1270 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 1271 | /// properties for an implementation. |
| 1272 | void CodeGenModule::EmitObjCPropertyImplementations(const |
| 1273 | ObjCImplementationDecl *D) { |
| 1274 | for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), |
| 1275 | e = D->propimpl_end(); i != e; ++i) { |
| 1276 | ObjCPropertyImplDecl *PID = *i; |
| 1277 | |
| 1278 | // Dynamic is just for type-checking. |
| 1279 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1280 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1281 | |
| 1282 | // Determine which methods need to be implemented, some may have |
| 1283 | // been overridden. Note that ::isSynthesized is not the method |
| 1284 | // we want, that just indicates if the decl came from a |
| 1285 | // property. What we want to know is if the method is defined in |
| 1286 | // this implementation. |
| 1287 | if (!D->getInstanceMethod(PD->getGetterName())) |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1288 | CodeGenFunction(*this).GenerateObjCGetter( |
| 1289 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1290 | if (!PD->isReadOnly() && |
| 1291 | !D->getInstanceMethod(PD->getSetterName())) |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1292 | CodeGenFunction(*this).GenerateObjCSetter( |
| 1293 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 1298 | /// EmitNamespace - Emit all declarations in a namespace. |
Anders Carlsson | 984e068 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 1299 | void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1300 | for (RecordDecl::decl_iterator I = ND->decls_begin(getContext()), |
| 1301 | E = ND->decls_end(getContext()); |
Anders Carlsson | 984e068 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 1302 | I != E; ++I) |
| 1303 | EmitTopLevelDecl(*I); |
| 1304 | } |
| 1305 | |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 1306 | // EmitLinkageSpec - Emit all declarations in a linkage spec. |
| 1307 | void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { |
| 1308 | if (LSD->getLanguage() != LinkageSpecDecl::lang_c) { |
| 1309 | ErrorUnsupported(LSD, "linkage spec"); |
| 1310 | return; |
| 1311 | } |
| 1312 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1313 | for (RecordDecl::decl_iterator I = LSD->decls_begin(getContext()), |
| 1314 | E = LSD->decls_end(getContext()); |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 1315 | I != E; ++I) |
| 1316 | EmitTopLevelDecl(*I); |
| 1317 | } |
| 1318 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1319 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 1320 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
| 1321 | // If an error has occurred, stop code generation, but continue |
| 1322 | // parsing and semantic analysis (to ensure all warnings and errors |
| 1323 | // are emitted). |
| 1324 | if (Diags.hasErrorOccurred()) |
| 1325 | return; |
| 1326 | |
| 1327 | switch (D->getKind()) { |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 1328 | case Decl::CXXMethod: |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1329 | case Decl::Function: |
| 1330 | case Decl::Var: |
| 1331 | EmitGlobal(cast<ValueDecl>(D)); |
| 1332 | break; |
| 1333 | |
| 1334 | case Decl::Namespace: |
Anders Carlsson | 984e068 | 2009-04-01 00:58:25 +0000 | [diff] [blame] | 1335 | EmitNamespace(cast<NamespaceDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1336 | break; |
| 1337 | |
| 1338 | // Objective-C Decls |
| 1339 | |
Fariborz Jahanian | 38e24c7 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 1340 | // Forward declarations, no (immediate) code generation. |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1341 | case Decl::ObjCClass: |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1342 | case Decl::ObjCForwardProtocol: |
Fariborz Jahanian | b31cb7f | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 1343 | case Decl::ObjCCategory: |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1344 | break; |
Chris Lattner | 285d0db | 2009-04-01 02:36:43 +0000 | [diff] [blame] | 1345 | case Decl::ObjCInterface: |
| 1346 | // If we already laid out this interface due to an @class, and if we |
| 1347 | // codegen'd a reference it, update the 'opaque' type to be a real type now. |
| 1348 | Types.UpdateCompletedType(cast<ObjCInterfaceDecl>(D)); |
| 1349 | break; |
| 1350 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1351 | case Decl::ObjCProtocol: |
Fariborz Jahanian | b31cb7f | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 1352 | Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1353 | break; |
| 1354 | |
| 1355 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1356 | // Categories have properties but don't support synthesize so we |
| 1357 | // can ignore them here. |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1358 | Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
| 1359 | break; |
| 1360 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1361 | case Decl::ObjCImplementation: { |
| 1362 | ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); |
| 1363 | EmitObjCPropertyImplementations(OMD); |
| 1364 | Runtime->GenerateClass(OMD); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1365 | break; |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1366 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1367 | case Decl::ObjCMethod: { |
| 1368 | ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); |
| 1369 | // If this is not a prototype, emit the body. |
| 1370 | if (OMD->getBody()) |
| 1371 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 1372 | break; |
| 1373 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1374 | case Decl::ObjCCompatibleAlias: |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1375 | // compatibility-alias is a directive and has no code gen. |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1376 | break; |
| 1377 | |
Anders Carlsson | 91e20dd | 2009-04-02 05:55:18 +0000 | [diff] [blame] | 1378 | case Decl::LinkageSpec: |
| 1379 | EmitLinkageSpec(cast<LinkageSpecDecl>(D)); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1380 | break; |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1381 | |
| 1382 | case Decl::FileScopeAsm: { |
| 1383 | FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); |
| 1384 | std::string AsmString(AD->getAsmString()->getStrData(), |
| 1385 | AD->getAsmString()->getByteLength()); |
| 1386 | |
| 1387 | const std::string &S = getModule().getModuleInlineAsm(); |
| 1388 | if (S.empty()) |
| 1389 | getModule().setModuleInlineAsm(AsmString); |
| 1390 | else |
| 1391 | getModule().setModuleInlineAsm(S + '\n' + AsmString); |
| 1392 | break; |
| 1393 | } |
| 1394 | |
| 1395 | default: |
| 1396 | // Make sure we handled everything we should, every other kind is |
| 1397 | // a non-top-level decl. FIXME: Would be nice to have an |
| 1398 | // isTopLevelDeclKind function. Need to recode Decl::Kind to do |
| 1399 | // that easily. |
| 1400 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
| 1401 | } |
| 1402 | } |