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