Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-module state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Sanjiv Gupta | 40e56a1 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 15 | #include "CodeGenModule.h" |
| 16 | #include "CodeGenFunction.h" |
Daniel Dunbar | a8f0205 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 17 | #include "CGCall.h" |
Daniel Dunbar | 84bb85f | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 19 | #include "Mangle.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 825f6ea | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Diagnostic.h" |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Nate Begeman | dc6262e | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 26 | #include "llvm/CallingConv.h" |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 27 | #include "llvm/Module.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 28 | #include "llvm/Intrinsics.h" |
Anton Korobeynikov | cd5d08d | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
| 33 | |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 34 | CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, |
Chris Lattner | 22595b8 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 35 | llvm::Module &M, const llvm::TargetData &TD, |
Daniel Dunbar | 1be1df3 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 36 | Diagnostic &diags, bool GenerateDebugInfo) |
Chris Lattner | 22595b8 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 37 | : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags), |
Daniel Dunbar | fc69bde | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 38 | Types(C, M, TD), Runtime(0), MemCpyFn(0), MemMoveFn(0), MemSetFn(0), |
Anders Carlsson | 1f1cd39 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 39 | CFConstantStringClassRef(0), NSConcreteGlobalBlock(0) { |
Daniel Dunbar | fc69bde | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 40 | |
| 41 | if (Features.ObjC1) { |
Daniel Dunbar | 1be1df3 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 42 | if (Features.NeXTRuntime) { |
Fariborz Jahanian | d037481 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 43 | Runtime = Features.ObjCNonFragileABI ? CreateMacNonFragileABIObjCRuntime(*this) |
Fariborz Jahanian | 48543f5 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 44 | : CreateMacObjCRuntime(*this); |
Daniel Dunbar | fc69bde | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 45 | } else { |
| 46 | Runtime = CreateGNUObjCRuntime(*this); |
| 47 | } |
Daniel Dunbar | 8c85fac | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 48 | } |
Sanjiv Gupta | 40e56a1 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 49 | |
| 50 | // If debug info generation is enabled, create the CGDebugInfo object. |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 51 | DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0; |
Chris Lattner | cbfb551 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | CodeGenModule::~CodeGenModule() { |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 55 | delete Runtime; |
| 56 | delete DebugInfo; |
| 57 | } |
| 58 | |
| 59 | void CodeGenModule::Release() { |
Anton Korobeynikov | cd5d08d | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 60 | EmitStatics(); |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 61 | EmitAliases(); |
Daniel Dunbar | fc69bde | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 62 | if (Runtime) |
| 63 | if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) |
| 64 | AddGlobalCtor(ObjCInitFunction); |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 65 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 66 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Nate Begeman | 52da5c7 | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 67 | EmitAnnotations(); |
Daniel Dunbar | 18c2ec6 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 68 | BindRuntimeFunctions(); |
Chris Lattner | cbfb551 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 69 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 70 | |
Daniel Dunbar | 18c2ec6 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 71 | void CodeGenModule::BindRuntimeFunctions() { |
| 72 | // Deal with protecting runtime function names. |
| 73 | for (unsigned i = 0, e = RuntimeFunctions.size(); i < e; ++i) { |
| 74 | llvm::Function *Fn = RuntimeFunctions[i].first; |
| 75 | const std::string &Name = RuntimeFunctions[i].second; |
| 76 | |
Daniel Dunbar | fc1543e | 2008-11-19 06:15:35 +0000 | [diff] [blame] | 77 | // Discard unused runtime functions. |
| 78 | if (Fn->use_empty()) { |
| 79 | Fn->eraseFromParent(); |
| 80 | continue; |
| 81 | } |
| 82 | |
Daniel Dunbar | 18c2ec6 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 83 | // See if there is a conflict against a function. |
| 84 | llvm::Function *Conflict = TheModule.getFunction(Name); |
| 85 | if (Conflict) { |
| 86 | // Decide which version to take. If the conflict is a definition |
| 87 | // we are forced to take that, otherwise assume the runtime |
| 88 | // knows best. |
| 89 | if (!Conflict->isDeclaration()) { |
| 90 | llvm::Value *Casted = |
| 91 | llvm::ConstantExpr::getBitCast(Conflict, Fn->getType()); |
| 92 | Fn->replaceAllUsesWith(Casted); |
| 93 | Fn->eraseFromParent(); |
| 94 | } else { |
| 95 | Fn->takeName(Conflict); |
| 96 | llvm::Value *Casted = |
| 97 | llvm::ConstantExpr::getBitCast(Fn, Conflict->getType()); |
| 98 | Conflict->replaceAllUsesWith(Casted); |
| 99 | Conflict->eraseFromParent(); |
| 100 | } |
| 101 | } else { |
| 102 | // FIXME: There still may be conflicts with aliases and |
| 103 | // variables. |
| 104 | Fn->setName(Name); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 109 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 110 | /// specified stmt yet. |
Daniel Dunbar | 49bddf7 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 111 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, |
| 112 | bool OmitOnError) { |
| 113 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 114 | return; |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 115 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Daniel Dunbar | c5ba491 | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 116 | "cannot compile this %0 yet"); |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 117 | std::string Msg = Type; |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 118 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) |
| 119 | << Msg << S->getSourceRange(); |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 120 | } |
Chris Lattner | 0e4755d | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 121 | |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 122 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 123 | /// specified decl yet. |
Daniel Dunbar | 49bddf7 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 124 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, |
| 125 | bool OmitOnError) { |
| 126 | if (OmitOnError && getDiags().hasErrorOccurred()) |
| 127 | return; |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 128 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Daniel Dunbar | c5ba491 | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 129 | "cannot compile this %0 yet"); |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 130 | std::string Msg = Type; |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 131 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 134 | /// setGlobalVisibility - Set the visibility for the given LLVM |
| 135 | /// GlobalValue according to the given clang AST visibility value. |
| 136 | static void setGlobalVisibility(llvm::GlobalValue *GV, |
| 137 | VisibilityAttr::VisibilityTypes Vis) { |
Dan Gohman | 4751a3a | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 138 | switch (Vis) { |
| 139 | default: assert(0 && "Unknown visibility!"); |
| 140 | case VisibilityAttr::DefaultVisibility: |
| 141 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 142 | break; |
| 143 | case VisibilityAttr::HiddenVisibility: |
| 144 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 145 | break; |
| 146 | case VisibilityAttr::ProtectedVisibility: |
| 147 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 152 | /// \brief Retrieves the mangled name for the given declaration. |
| 153 | /// |
| 154 | /// If the given declaration requires a mangled name, returns an |
| 155 | /// IdentifierInfo* containing the mangled name. Otherwise, returns |
| 156 | /// the name of the declaration as an identifier. |
| 157 | /// |
| 158 | /// FIXME: Returning an IdentifierInfo* here is a total hack. We |
| 159 | /// really need some kind of string abstraction that either stores a |
| 160 | /// mangled name or stores an IdentifierInfo*. This will require |
| 161 | /// changes to the GlobalDeclMap, too. |
| 162 | /// |
| 163 | /// FIXME: Performance here is going to be terribly until we start |
| 164 | /// caching mangled names. However, we should fix the problem above |
| 165 | /// first. |
| 166 | IdentifierInfo *CodeGenModule::getMangledName(const NamedDecl *ND) const { |
| 167 | std::string Name; |
| 168 | llvm::raw_string_ostream Out(Name); |
| 169 | if (!mangleName(ND, Context, Out)) |
| 170 | return ND->getIdentifier(); |
| 171 | |
| 172 | return &Context.Idents.get(Out.str()); |
| 173 | } |
| 174 | |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 175 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 176 | /// main() runs. |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 177 | void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 178 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 179 | GlobalCtors.push_back(std::make_pair(Ctor, Priority)); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 182 | /// AddGlobalDtor - Add a function to the list that will be called |
| 183 | /// when the module is unloaded. |
| 184 | void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 185 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 186 | GlobalDtors.push_back(std::make_pair(Dtor, Priority)); |
| 187 | } |
| 188 | |
| 189 | void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { |
| 190 | // Ctor function type is void()*. |
| 191 | llvm::FunctionType* CtorFTy = |
| 192 | llvm::FunctionType::get(llvm::Type::VoidTy, |
| 193 | std::vector<const llvm::Type*>(), |
| 194 | false); |
| 195 | llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); |
| 196 | |
| 197 | // Get the type of a ctor entry, { i32, void ()* }. |
Chris Lattner | a18c12e | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 198 | llvm::StructType* CtorStructTy = |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 199 | llvm::StructType::get(llvm::Type::Int32Ty, |
| 200 | llvm::PointerType::getUnqual(CtorFTy), NULL); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 201 | |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 202 | // Construct the constructor and destructor arrays. |
| 203 | std::vector<llvm::Constant*> Ctors; |
| 204 | for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
| 205 | std::vector<llvm::Constant*> S; |
| 206 | S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); |
| 207 | S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); |
| 208 | Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 209 | } |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 210 | |
| 211 | if (!Ctors.empty()) { |
| 212 | llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); |
| 213 | new llvm::GlobalVariable(AT, false, |
| 214 | llvm::GlobalValue::AppendingLinkage, |
| 215 | llvm::ConstantArray::get(AT, Ctors), |
| 216 | GlobalName, |
| 217 | &TheModule); |
| 218 | } |
Chris Lattner | 753d259 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Nate Begeman | 52da5c7 | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 221 | void CodeGenModule::EmitAnnotations() { |
| 222 | if (Annotations.empty()) |
| 223 | return; |
| 224 | |
| 225 | // Create a new global variable for the ConstantStruct in the Module. |
| 226 | llvm::Constant *Array = |
| 227 | llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), |
| 228 | Annotations.size()), |
| 229 | Annotations); |
| 230 | llvm::GlobalValue *gv = |
| 231 | new llvm::GlobalVariable(Array->getType(), false, |
| 232 | llvm::GlobalValue::AppendingLinkage, Array, |
| 233 | "llvm.global.annotations", &TheModule); |
| 234 | gv->setSection("llvm.metadata"); |
| 235 | } |
| 236 | |
Daniel Dunbar | a8f0205 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 237 | static void SetGlobalValueAttributes(const Decl *D, |
| 238 | bool IsInternal, |
| 239 | bool IsInline, |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 240 | llvm::GlobalValue *GV, |
| 241 | bool ForDefinition) { |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 242 | // FIXME: Set up linkage and many other things. Note, this is a simple |
Nuno Lopes | 7853438 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 243 | // approximation of what we really want. |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 244 | if (!ForDefinition) { |
| 245 | // Only a few attributes are set on declarations. |
Anton Korobeynikov | b27a870 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 246 | if (D->getAttr<DLLImportAttr>()) { |
| 247 | // The dllimport attribute is overridden by a subsequent declaration as |
| 248 | // dllexport. |
Sebastian Redl | e299eb4 | 2009-01-05 20:53:53 +0000 | [diff] [blame] | 249 | if (!D->getAttr<DLLExportAttr>()) { |
Anton Korobeynikov | b27a870 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 250 | // dllimport attribute can be applied only to function decls, not to |
| 251 | // definitions. |
| 252 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 253 | if (!FD->getBody()) |
| 254 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 255 | } else |
| 256 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
Sebastian Redl | e299eb4 | 2009-01-05 20:53:53 +0000 | [diff] [blame] | 257 | } |
Anton Korobeynikov | b27a870 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 258 | } |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 259 | } else { |
| 260 | if (IsInternal) { |
| 261 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 262 | } else { |
Anton Korobeynikov | b27a870 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 263 | if (D->getAttr<DLLExportAttr>()) { |
| 264 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 265 | // The dllexport attribute is ignored for undefined symbols. |
| 266 | if (FD->getBody()) |
| 267 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
| 268 | } else |
| 269 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
| 270 | } else if (D->getAttr<WeakAttr>() || IsInline) |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 271 | GV->setLinkage(llvm::Function::WeakLinkage); |
| 272 | } |
Daniel Dunbar | a8f0205 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 273 | } |
Nuno Lopes | 7853438 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 274 | |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 275 | // FIXME: Figure out the relative priority of the attribute, |
| 276 | // -fvisibility, and private_extern. |
Daniel Dunbar | a8f0205 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 277 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 278 | setGlobalVisibility(GV, attr->getVisibility()); |
Nuno Lopes | 7853438 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 279 | // FIXME: else handle -fvisibility |
Daniel Dunbar | ced8914 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 280 | |
Daniel Dunbar | a8f0205 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 281 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
Daniel Dunbar | ced8914 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 282 | // Prefaced with special LLVM marker to indicate that the name |
| 283 | // should not be munged. |
| 284 | GV->setName("\01" + ALA->getLabel()); |
| 285 | } |
Daniel Dunbar | 9750972 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 286 | |
| 287 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
| 288 | GV->setSection(SA->getName()); |
Nuno Lopes | 7853438 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Devang Patel | a85a9ef | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 291 | void CodeGenModule::SetFunctionAttributes(const Decl *D, |
Daniel Dunbar | 3ad1f07 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 292 | const CGFunctionInfo &Info, |
Daniel Dunbar | 3ef2e85 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 293 | llvm::Function *F) { |
Devang Patel | a85a9ef | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 294 | AttributeListType AttributeList; |
Daniel Dunbar | 6ee022b | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 295 | ConstructAttributeList(Info, D, AttributeList); |
Eli Friedman | fa94dff | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 296 | |
Devang Patel | a85a9ef | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 297 | F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
| 298 | AttributeList.size())); |
Eli Friedman | 9be4221 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 299 | |
| 300 | // Set the appropriate calling convention for the Function. |
Daniel Dunbar | 3ad1f07 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 301 | if (D->getAttr<FastCallAttr>()) |
Anton Korobeynikov | 0ef7c38 | 2008-11-11 20:21:14 +0000 | [diff] [blame] | 302 | F->setCallingConv(llvm::CallingConv::X86_FastCall); |
| 303 | |
| 304 | if (D->getAttr<StdCallAttr>()) |
| 305 | F->setCallingConv(llvm::CallingConv::X86_StdCall); |
Daniel Dunbar | f278700 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /// SetFunctionAttributesForDefinition - Set function attributes |
| 309 | /// specific to a function definition. |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 310 | void CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, |
| 311 | llvm::Function *F) { |
| 312 | if (isa<ObjCMethodDecl>(D)) { |
| 313 | SetGlobalValueAttributes(D, true, false, F, true); |
| 314 | } else { |
| 315 | const FunctionDecl *FD = cast<FunctionDecl>(D); |
| 316 | SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, |
| 317 | FD->isInline(), F, true); |
| 318 | } |
| 319 | |
Daniel Dunbar | f278700 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 320 | if (!Features.Exceptions) |
Daniel Dunbar | 9575eb3 | 2008-09-27 07:16:42 +0000 | [diff] [blame] | 321 | F->addFnAttr(llvm::Attribute::NoUnwind); |
Daniel Dunbar | 0a2da71 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 322 | |
| 323 | if (D->getAttr<AlwaysInlineAttr>()) |
| 324 | F->addFnAttr(llvm::Attribute::AlwaysInline); |
Daniel Dunbar | f278700 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, |
| 328 | llvm::Function *F) { |
Daniel Dunbar | 34bda88 | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 329 | SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F); |
Daniel Dunbar | f278700 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 330 | |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 331 | SetFunctionAttributesForDefinition(MD, F); |
Daniel Dunbar | f278700 | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, |
| 335 | llvm::Function *F) { |
Daniel Dunbar | 34bda88 | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 336 | SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); |
Daniel Dunbar | 3ad1f07 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 337 | |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 338 | SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, |
| 339 | FD->isInline(), F, false); |
| 340 | } |
| 341 | |
Daniel Dunbar | 3ad1f07 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 342 | |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 343 | void CodeGenModule::EmitAliases() { |
| 344 | for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { |
| 345 | const FunctionDecl *D = Aliases[i]; |
| 346 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
| 347 | |
| 348 | // This is something of a hack, if the FunctionDecl got overridden |
| 349 | // then its attributes will be moved to the new declaration. In |
| 350 | // this case the current decl has no alias attribute, but we will |
| 351 | // eventually see it. |
| 352 | if (!AA) |
| 353 | continue; |
| 354 | |
| 355 | const std::string& aliaseeName = AA->getAliasee(); |
| 356 | llvm::Function *aliasee = getModule().getFunction(aliaseeName); |
| 357 | if (!aliasee) { |
| 358 | // FIXME: This isn't unsupported, this is just an error, which |
| 359 | // sema should catch, but... |
| 360 | ErrorUnsupported(D, "alias referencing a missing function"); |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | llvm::GlobalValue *GA = |
| 365 | new llvm::GlobalAlias(aliasee->getType(), |
| 366 | llvm::Function::ExternalLinkage, |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 367 | getMangledName(D)->getName(), aliasee, |
| 368 | &getModule()); |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 369 | |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 370 | llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 371 | if (Entry) { |
| 372 | // If we created a dummy function for this then replace it. |
| 373 | GA->takeName(Entry); |
| 374 | |
| 375 | llvm::Value *Casted = |
| 376 | llvm::ConstantExpr::getBitCast(GA, Entry->getType()); |
| 377 | Entry->replaceAllUsesWith(Casted); |
| 378 | Entry->eraseFromParent(); |
| 379 | |
| 380 | Entry = GA; |
| 381 | } |
| 382 | |
| 383 | // Alias should never be internal or inline. |
| 384 | SetGlobalValueAttributes(D, false, false, GA, true); |
| 385 | } |
Eli Friedman | 9be4221 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 388 | void CodeGenModule::EmitStatics() { |
| 389 | // Emit code for each used static decl encountered. Since a previously unused |
| 390 | // static decl may become used during the generation of code for a static |
| 391 | // function, iterate until no changes are made. |
| 392 | bool Changed; |
| 393 | do { |
| 394 | Changed = false; |
Anders Carlsson | 2e427d5 | 2009-01-04 02:08:04 +0000 | [diff] [blame] | 395 | |
| 396 | for (std::list<const ValueDecl*>::iterator i = StaticDecls.begin(), |
| 397 | e = StaticDecls.end(); i != e; ) { |
| 398 | const ValueDecl *D = *i; |
| 399 | |
Eli Friedman | a4d4e2f | 2008-05-27 04:58:01 +0000 | [diff] [blame] | 400 | // Check if we have used a decl with the same name |
| 401 | // FIXME: The AST should have some sort of aggregate decls or |
| 402 | // global symbol map. |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 403 | // FIXME: This is missing some important cases. For example, we |
| 404 | // need to check for uses in an alias and in a constructor. |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 405 | if (!GlobalDeclMap.count(getMangledName(D))) { |
Anders Carlsson | 2e427d5 | 2009-01-04 02:08:04 +0000 | [diff] [blame] | 406 | i++; |
Daniel Dunbar | ced8914 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 407 | continue; |
Anders Carlsson | 2e427d5 | 2009-01-04 02:08:04 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 410 | // Emit the definition. |
| 411 | EmitGlobalDefinition(D); |
| 412 | |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 413 | // Erase the used decl from the list. |
Anders Carlsson | 2e427d5 | 2009-01-04 02:08:04 +0000 | [diff] [blame] | 414 | i = StaticDecls.erase(i); |
| 415 | |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 416 | // Remember that we made a change. |
| 417 | Changed = true; |
| 418 | } |
| 419 | } while (Changed); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 422 | /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the |
| 423 | /// annotation information for a given GlobalValue. The annotation struct is |
| 424 | /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 425 | /// GlobalValue being annotated. The second field is the constant string |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 426 | /// created from the AnnotateAttr's annotation. The third field is a constant |
| 427 | /// string containing the name of the translation unit. The fourth field is |
| 428 | /// the line number in the file of the annotated value declaration. |
| 429 | /// |
| 430 | /// FIXME: this does not unique the annotation string constants, as llvm-gcc |
| 431 | /// appears to. |
| 432 | /// |
| 433 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
| 434 | const AnnotateAttr *AA, |
| 435 | unsigned LineNo) { |
| 436 | llvm::Module *M = &getModule(); |
| 437 | |
| 438 | // get [N x i8] constants for the annotation string, and the filename string |
| 439 | // which are the 2nd and 3rd elements of the global annotation structure. |
| 440 | const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 441 | llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); |
| 442 | llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), |
| 443 | true); |
| 444 | |
| 445 | // Get the two global values corresponding to the ConstantArrays we just |
| 446 | // created to hold the bytes of the strings. |
| 447 | llvm::GlobalValue *annoGV = |
| 448 | new llvm::GlobalVariable(anno->getType(), false, |
| 449 | llvm::GlobalValue::InternalLinkage, anno, |
| 450 | GV->getName() + ".str", M); |
| 451 | // translation unit name string, emitted into the llvm.metadata section. |
| 452 | llvm::GlobalValue *unitGV = |
| 453 | new llvm::GlobalVariable(unit->getType(), false, |
| 454 | llvm::GlobalValue::InternalLinkage, unit, ".str", M); |
| 455 | |
| 456 | // Create the ConstantStruct that is the global annotion. |
| 457 | llvm::Constant *Fields[4] = { |
| 458 | llvm::ConstantExpr::getBitCast(GV, SBP), |
| 459 | llvm::ConstantExpr::getBitCast(annoGV, SBP), |
| 460 | llvm::ConstantExpr::getBitCast(unitGV, SBP), |
| 461 | llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) |
| 462 | }; |
| 463 | return llvm::ConstantStruct::get(Fields, 4, false); |
| 464 | } |
| 465 | |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 466 | void CodeGenModule::EmitGlobal(const ValueDecl *Global) { |
| 467 | bool isDef, isStatic; |
| 468 | |
| 469 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 470 | // Aliases are deferred until code for everything else has been |
| 471 | // emitted. |
| 472 | if (FD->getAttr<AliasAttr>()) { |
| 473 | assert(!FD->isThisDeclarationADefinition() && |
| 474 | "Function alias cannot have a definition!"); |
| 475 | Aliases.push_back(FD); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | isDef = FD->isThisDeclarationADefinition(); |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 480 | isStatic = FD->getStorageClass() == FunctionDecl::Static; |
| 481 | } else if (const VarDecl *VD = cast<VarDecl>(Global)) { |
| 482 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
| 483 | |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 484 | isDef = !((VD->getStorageClass() == VarDecl::Extern || |
| 485 | VD->getStorageClass() == VarDecl::PrivateExtern) && |
| 486 | VD->getInit() == 0); |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 487 | isStatic = VD->getStorageClass() == VarDecl::Static; |
| 488 | } else { |
| 489 | assert(0 && "Invalid argument to EmitGlobal"); |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 493 | // Forward declarations are emitted lazily on first use. |
| 494 | if (!isDef) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 495 | return; |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 496 | |
| 497 | // If the global is a static, defer code generation until later so |
| 498 | // we can easily omit unused statics. |
Daniel Dunbar | 9bae865 | 2009-02-04 21:19:06 +0000 | [diff] [blame] | 499 | if (isStatic && !Features.EmitAllDecls) { |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 500 | StaticDecls.push_back(Global); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | // Otherwise emit the definition. |
| 505 | EmitGlobalDefinition(Global); |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 508 | void CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { |
| 509 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 510 | EmitGlobalFunctionDefinition(FD); |
| 511 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 512 | EmitGlobalVarDefinition(VD); |
| 513 | } else { |
| 514 | assert(0 && "Invalid argument to EmitGlobalDefinition()"); |
| 515 | } |
| 516 | } |
| 517 | |
Daniel Dunbar | 2188c53 | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 518 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) { |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 519 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 520 | |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 521 | QualType ASTTy = D->getType(); |
| 522 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
Daniel Dunbar | 2188c53 | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 523 | const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 524 | |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 525 | // Lookup the entry, lazily creating it if necessary. |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 526 | llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 527 | if (!Entry) { |
| 528 | llvm::GlobalVariable *GV = |
| 529 | new llvm::GlobalVariable(Ty, false, |
| 530 | llvm::GlobalValue::ExternalLinkage, |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 531 | 0, getMangledName(D)->getName(), &getModule(), |
| 532 | 0, ASTTy.getAddressSpace()); |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 533 | Entry = GV; |
| 534 | |
| 535 | // Handle things which are present even on external declarations. |
| 536 | |
| 537 | // FIXME: This code is overly simple and should be merged with |
| 538 | // other global handling. |
| 539 | |
| 540 | GV->setConstant(D->getType().isConstant(Context)); |
| 541 | |
| 542 | if (D->getStorageClass() == VarDecl::PrivateExtern) |
| 543 | setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility); |
| 544 | } |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 545 | |
Daniel Dunbar | 2188c53 | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 546 | // Make sure the result is of the correct type. |
| 547 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 551 | llvm::Constant *Init = 0; |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 552 | QualType ASTTy = D->getType(); |
| 553 | const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy); |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 555 | if (D->getInit() == 0) { |
Eli Friedman | 7008e9a | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 556 | // This is a tentative definition; tentative definitions are |
| 557 | // implicitly initialized with { 0 } |
| 558 | const llvm::Type* InitTy; |
| 559 | if (ASTTy->isIncompleteArrayType()) { |
| 560 | // An incomplete array is normally [ TYPE x 0 ], but we need |
| 561 | // to fix it to [ TYPE x 1 ]. |
| 562 | const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy); |
| 563 | InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); |
| 564 | } else { |
| 565 | InitTy = VarTy; |
| 566 | } |
| 567 | Init = llvm::Constant::getNullValue(InitTy); |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 568 | } else { |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 569 | Init = EmitConstantExpr(D->getInit()); |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 570 | } |
| 571 | const llvm::Type* InitType = Init->getType(); |
| 572 | |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 573 | llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 574 | llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry); |
| 575 | |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 576 | if (!GV) { |
| 577 | GV = new llvm::GlobalVariable(InitType, false, |
| 578 | llvm::GlobalValue::ExternalLinkage, |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 579 | 0, getMangledName(D)->getName(), |
| 580 | &getModule(), 0, ASTTy.getAddressSpace()); |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 581 | } else if (GV->getType() != |
| 582 | llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) { |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 583 | // We have a definition after a prototype with the wrong type. |
| 584 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 585 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 586 | // (which will be a definition). |
| 587 | // |
| 588 | // This happens if there is a prototype for a global (e.g. "extern int x[];") |
| 589 | // and then a definition of a different type (e.g. "int x[10];"). This also |
| 590 | // happens when an initializer has a different type from the type of the |
| 591 | // global (this happens with unions). |
Eli Friedman | 7008e9a | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 592 | // |
| 593 | // FIXME: This also ends up happening if there's a definition followed by |
| 594 | // a tentative definition! (Although Sema rejects that construct |
| 595 | // at the moment.) |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 596 | |
| 597 | // Save the old global |
| 598 | llvm::GlobalVariable *OldGV = GV; |
| 599 | |
| 600 | // Make a new global with the correct type |
| 601 | GV = new llvm::GlobalVariable(InitType, false, |
| 602 | llvm::GlobalValue::ExternalLinkage, |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 603 | 0, getMangledName(D)->getName(), |
| 604 | &getModule(), 0, ASTTy.getAddressSpace()); |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 605 | // Steal the name of the old global |
| 606 | GV->takeName(OldGV); |
| 607 | |
| 608 | // Replace all uses of the old global with the new global |
| 609 | llvm::Constant *NewPtrForOldDecl = |
| 610 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 611 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | 43a0ce8 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 612 | |
| 613 | // Erase the old global, since it is no longer used. |
| 614 | OldGV->eraseFromParent(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 615 | } |
Devang Patel | 8b5f530 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 616 | |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 617 | Entry = GV; |
Devang Patel | 8b5f530 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 618 | |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 619 | if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { |
| 620 | SourceManager &SM = Context.getSourceManager(); |
| 621 | AddAnnotation(EmitAnnotateAttr(GV, AA, |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 622 | SM.getInstantiationLineNumber(D->getLocation()))); |
Nate Begeman | 8a70417 | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 625 | GV->setInitializer(Init); |
Nuno Lopes | a7bbf56 | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 626 | GV->setConstant(D->getType().isConstant(Context)); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 627 | |
Eli Friedman | 7008e9a | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 628 | // FIXME: This is silly; getTypeAlign should just work for incomplete arrays |
| 629 | unsigned Align; |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 630 | if (const IncompleteArrayType* IAT = |
| 631 | Context.getAsIncompleteArrayType(D->getType())) |
Eli Friedman | 7008e9a | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 632 | Align = Context.getTypeAlign(IAT->getElementType()); |
| 633 | else |
| 634 | Align = Context.getTypeAlign(D->getType()); |
Eli Friedman | b232e99 | 2008-05-29 11:10:27 +0000 | [diff] [blame] | 635 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) { |
| 636 | Align = std::max(Align, AA->getAlignment()); |
| 637 | } |
| 638 | GV->setAlignment(Align / 8); |
| 639 | |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 640 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 641 | setGlobalVisibility(GV, attr->getVisibility()); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 642 | // FIXME: else handle -fvisibility |
Daniel Dunbar | ced8914 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 643 | |
| 644 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
| 645 | // Prefaced with special LLVM marker to indicate that the name |
| 646 | // should not be munged. |
| 647 | GV->setName("\01" + ALA->getLabel()); |
| 648 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 649 | |
| 650 | // Set the llvm linkage type as appropriate. |
Chris Lattner | 25094a4 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 651 | if (D->getStorageClass() == VarDecl::Static) |
| 652 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 653 | else if (D->getAttr<DLLImportAttr>()) |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 654 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 655 | else if (D->getAttr<DLLExportAttr>()) |
| 656 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
Chris Lattner | 25094a4 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 657 | else if (D->getAttr<WeakAttr>()) |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 658 | GV->setLinkage(llvm::GlobalVariable::WeakLinkage); |
Chris Lattner | 25094a4 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 659 | else { |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 660 | // FIXME: This isn't right. This should handle common linkage and other |
| 661 | // stuff. |
| 662 | switch (D->getStorageClass()) { |
Chris Lattner | 25094a4 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 663 | case VarDecl::Static: assert(0 && "This case handled above"); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 664 | case VarDecl::Auto: |
| 665 | case VarDecl::Register: |
| 666 | assert(0 && "Can't have auto or register globals"); |
| 667 | case VarDecl::None: |
| 668 | if (!D->getInit()) |
Eli Friedman | a7f4633 | 2008-05-29 11:03:17 +0000 | [diff] [blame] | 669 | GV->setLinkage(llvm::GlobalVariable::CommonLinkage); |
Anders Carlsson | 689bba8 | 2008-12-03 05:51:23 +0000 | [diff] [blame] | 670 | else |
| 671 | GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 672 | break; |
| 673 | case VarDecl::Extern: |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 674 | // FIXME: common |
| 675 | break; |
| 676 | |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 677 | case VarDecl::PrivateExtern: |
Daniel Dunbar | 3a80fc0 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 678 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 679 | // FIXME: common |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 680 | break; |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 681 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 682 | } |
Sanjiv Gupta | 54d9754 | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 683 | |
Daniel Dunbar | 9750972 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 684 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
| 685 | GV->setSection(SA->getName()); |
| 686 | |
Sanjiv Gupta | 54d9754 | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 687 | // Emit global variable debug information. |
| 688 | CGDebugInfo *DI = getDebugInfo(); |
| 689 | if(DI) { |
Daniel Dunbar | 6fc1f97 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 690 | DI->setLocation(D->getLocation()); |
Sanjiv Gupta | 54d9754 | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 691 | DI->EmitGlobalVariable(GV, D); |
| 692 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 695 | llvm::GlobalValue * |
| 696 | CodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D) { |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 697 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 698 | llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), |
| 699 | llvm::Function::ExternalLinkage, |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 700 | getMangledName(D)->getName(), |
| 701 | &getModule()); |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 702 | SetFunctionAttributes(D, F); |
| 703 | return F; |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | llvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) { |
Daniel Dunbar | 2188c53 | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 707 | QualType ASTTy = D->getType(); |
| 708 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
| 709 | const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 710 | |
| 711 | // Lookup the entry, lazily creating it if necessary. |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 712 | llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 713 | if (!Entry) |
| 714 | Entry = EmitForwardFunctionDefinition(D); |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 715 | |
Daniel Dunbar | 2188c53 | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 716 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | void CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { |
Douglas Gregor | 3556bc7 | 2009-02-13 00:10:09 +0000 | [diff] [blame^] | 720 | llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 721 | if (!Entry) { |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 722 | Entry = EmitForwardFunctionDefinition(D); |
| 723 | } else { |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 724 | // If the types mismatch then we have to rewrite the definition. |
| 725 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 726 | if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) { |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 727 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 728 | // F is the Function* for the one with the wrong type, we must make a new |
| 729 | // Function* and update everything that used F (a declaration) with the new |
| 730 | // Function* (which will be a definition). |
| 731 | // |
| 732 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 733 | // then a definition of a different type (e.g. "int f(int x)"). Start by |
| 734 | // making a new function of the correct type, RAUW, then steal the name. |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 735 | llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D); |
| 736 | NewFn->takeName(Entry); |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 737 | |
| 738 | // Replace uses of F with the Function we will endow with a body. |
| 739 | llvm::Constant *NewPtrForOldDecl = |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 740 | llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); |
| 741 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
| 742 | |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 743 | // Ok, delete the old function now, which is dead. |
Daniel Dunbar | a31eaf7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 744 | assert(Entry->isDeclaration() && "Shouldn't replace non-declaration"); |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 745 | Entry->eraseFromParent(); |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 746 | |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 747 | Entry = NewFn; |
| 748 | } |
| 749 | } |
| 750 | |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 751 | llvm::Function *Fn = cast<llvm::Function>(Entry); |
| 752 | CodeGenFunction(*this).GenerateCode(D, Fn); |
Daniel Dunbar | dd2e9ca | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 753 | |
Daniel Dunbar | 566a650 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 754 | SetFunctionAttributesForDefinition(D, Fn); |
| 755 | |
| 756 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) { |
| 757 | AddGlobalCtor(Fn, CA->getPriority()); |
| 758 | } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) { |
| 759 | AddGlobalDtor(Fn, DA->getPriority()); |
Daniel Dunbar | 7bf5b3d | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
Daniel Dunbar | 18c2ec6 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 763 | llvm::Function * |
| 764 | CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, |
| 765 | const std::string &Name) { |
| 766 | llvm::Function *Fn = llvm::Function::Create(FTy, |
| 767 | llvm::Function::ExternalLinkage, |
| 768 | "", &TheModule); |
| 769 | RuntimeFunctions.push_back(std::make_pair(Fn, Name)); |
| 770 | return Fn; |
| 771 | } |
| 772 | |
Chris Lattner | 9ec3ca2 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 773 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 774 | // Make sure that this type is translated. |
| 775 | Types.UpdateCompletedType(TD); |
Chris Lattner | 1b22f8b | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 779 | /// getBuiltinLibFunction |
| 780 | llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
Chris Lattner | 9f2d689 | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 781 | if (BuiltinID > BuiltinFunctions.size()) |
| 782 | BuiltinFunctions.resize(BuiltinID); |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 9f2d689 | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 784 | // Cache looked up functions. Since builtin id #0 is invalid we don't reserve |
| 785 | // a slot for it. |
| 786 | assert(BuiltinID && "Invalid Builtin ID"); |
| 787 | llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 788 | if (FunctionSlot) |
| 789 | return FunctionSlot; |
| 790 | |
| 791 | assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); |
| 792 | |
| 793 | // Get the name, skip over the __builtin_ prefix. |
| 794 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; |
| 795 | |
| 796 | // Get the type for the builtin. |
| 797 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); |
| 798 | const llvm::FunctionType *Ty = |
| 799 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 800 | |
| 801 | // FIXME: This has a serious problem with code like this: |
| 802 | // void abs() {} |
| 803 | // ... __builtin_abs(x); |
| 804 | // The two versions of abs will collide. The fix is for the builtin to win, |
| 805 | // and for the existing one to be turned into a constantexpr cast of the |
| 806 | // builtin. In the case where the existing one is a static function, it |
| 807 | // should just be renamed. |
Chris Lattner | 02c60f5 | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 808 | if (llvm::Function *Existing = getModule().getFunction(Name)) { |
| 809 | if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) |
| 810 | return FunctionSlot = Existing; |
| 811 | assert(Existing == 0 && "FIXME: Name collision"); |
| 812 | } |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 813 | |
| 814 | // FIXME: param attributes for sext/zext etc. |
Nate Begeman | ad320b6 | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 815 | return FunctionSlot = |
| 816 | llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, |
| 817 | &getModule()); |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 820 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, |
| 821 | unsigned NumTys) { |
| 822 | return llvm::Intrinsic::getDeclaration(&getModule(), |
| 823 | (llvm::Intrinsic::ID)IID, Tys, NumTys); |
| 824 | } |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 825 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 826 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 827 | if (MemCpyFn) return MemCpyFn; |
Chris Lattner | e73302f | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 828 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); |
| 829 | return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 830 | } |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 831 | |
Eli Friedman | 8f08a25 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 832 | llvm::Function *CodeGenModule::getMemMoveFn() { |
| 833 | if (MemMoveFn) return MemMoveFn; |
Chris Lattner | e73302f | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 834 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); |
| 835 | return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); |
Eli Friedman | 8f08a25 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Lauro Ramos Venancio | e5bef73 | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 838 | llvm::Function *CodeGenModule::getMemSetFn() { |
| 839 | if (MemSetFn) return MemSetFn; |
Chris Lattner | e73302f | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 840 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); |
| 841 | return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); |
Lauro Ramos Venancio | e5bef73 | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 842 | } |
Chris Lattner | 4b23f94 | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 843 | |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 844 | static void appendFieldAndPadding(CodeGenModule &CGM, |
| 845 | std::vector<llvm::Constant*>& Fields, |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 846 | FieldDecl *FieldD, FieldDecl *NextFieldD, |
| 847 | llvm::Constant* Field, |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 848 | RecordDecl* RD, const llvm::StructType *STy) |
| 849 | { |
| 850 | // Append the field. |
| 851 | Fields.push_back(Field); |
| 852 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 853 | int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 854 | |
| 855 | int NextStructFieldNo; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 856 | if (!NextFieldD) { |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 857 | NextStructFieldNo = STy->getNumElements(); |
| 858 | } else { |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 859 | NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | // Append padding |
| 863 | for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { |
| 864 | llvm::Constant *C = |
| 865 | llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); |
| 866 | |
| 867 | Fields.push_back(C); |
| 868 | } |
| 869 | } |
| 870 | |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 871 | // We still need to work out the details of handling UTF-16. |
| 872 | // See: <rdr://2996215> |
Chris Lattner | ab862cc | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 873 | llvm::Constant *CodeGenModule:: |
| 874 | GetAddrOfConstantCFString(const std::string &str) { |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 875 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 876 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 877 | |
| 878 | if (Entry.getValue()) |
| 879 | return Entry.getValue(); |
| 880 | |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 881 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 882 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 883 | |
| 884 | if (!CFConstantStringClassRef) { |
| 885 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 886 | Ty = llvm::ArrayType::get(Ty, 0); |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 887 | |
| 888 | // FIXME: This is fairly broken if |
| 889 | // __CFConstantStringClassReference is already defined, in that it |
| 890 | // will get renamed and the user will most likely see an opaque |
| 891 | // error message. This is a general issue with relying on |
| 892 | // particular names. |
| 893 | llvm::GlobalVariable *GV = |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 894 | new llvm::GlobalVariable(Ty, false, |
| 895 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 896 | "__CFConstantStringClassReference", |
| 897 | &getModule()); |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 898 | |
| 899 | // Decay array -> ptr |
| 900 | CFConstantStringClassRef = |
| 901 | llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 904 | QualType CFTy = getContext().getCFConstantStringType(); |
| 905 | RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 906 | |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 907 | const llvm::StructType *STy = |
| 908 | cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
| 909 | |
| 910 | std::vector<llvm::Constant*> Fields; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 911 | RecordDecl::field_iterator Field = CFRD->field_begin(); |
| 912 | |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 913 | // Class pointer. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 914 | FieldDecl *CurField = *Field++; |
| 915 | FieldDecl *NextField = *Field++; |
| 916 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
| 917 | CFConstantStringClassRef, CFRD, STy); |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 918 | |
| 919 | // Flags. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 920 | CurField = NextField; |
| 921 | NextField = *Field++; |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 922 | const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 923 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
| 924 | llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy); |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 925 | |
| 926 | // String pointer. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 927 | CurField = NextField; |
| 928 | NextField = *Field++; |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 929 | llvm::Constant *C = llvm::ConstantArray::get(str); |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 930 | C = new llvm::GlobalVariable(C->getType(), true, |
| 931 | llvm::GlobalValue::InternalLinkage, |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 932 | C, ".str", &getModule()); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 933 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 934 | llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2), |
| 935 | CFRD, STy); |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 936 | |
| 937 | // String length. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 938 | CurField = NextField; |
| 939 | NextField = 0; |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 940 | Ty = getTypes().ConvertType(getContext().LongTy); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 941 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
| 942 | llvm::ConstantInt::get(Ty, str.length()), CFRD, STy); |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 943 | |
| 944 | // The struct. |
Anders Carlsson | 0c0d895 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 945 | C = llvm::ConstantStruct::get(STy, Fields); |
Anders Carlsson | 9be009e | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 946 | llvm::GlobalVariable *GV = |
| 947 | new llvm::GlobalVariable(C->getType(), true, |
| 948 | llvm::GlobalVariable::InternalLinkage, |
| 949 | C, "", &getModule()); |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 950 | |
Anders Carlsson | 9be009e | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 951 | GV->setSection("__DATA,__cfstring"); |
| 952 | Entry.setValue(GV); |
Daniel Dunbar | dbdb951 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 953 | |
Anders Carlsson | 9be009e | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 954 | return GV; |
Anders Carlsson | 36a0487 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 955 | } |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 956 | |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 957 | /// GetStringForStringLiteral - Return the appropriate bytes for a |
Daniel Dunbar | 3c670e1 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 958 | /// string literal, properly padded to match the literal type. |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 959 | std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { |
Daniel Dunbar | 952f473 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 960 | if (E->isWide()) { |
| 961 | ErrorUnsupported(E, "wide string"); |
| 962 | return "FIXME"; |
| 963 | } |
| 964 | |
Daniel Dunbar | 3c670e1 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 965 | const char *StrData = E->getStrData(); |
| 966 | unsigned Len = E->getByteLength(); |
| 967 | |
| 968 | const ConstantArrayType *CAT = |
| 969 | getContext().getAsConstantArrayType(E->getType()); |
| 970 | assert(CAT && "String isn't pointer or array!"); |
| 971 | |
| 972 | // Resize the string to the right size |
| 973 | // FIXME: What about wchar_t strings? |
| 974 | std::string Str(StrData, StrData+Len); |
| 975 | uint64_t RealLen = CAT->getSize().getZExtValue(); |
| 976 | Str.resize(RealLen, '\0'); |
| 977 | |
| 978 | return Str; |
| 979 | } |
| 980 | |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 981 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 982 | /// constant array for the given string literal. |
| 983 | llvm::Constant * |
| 984 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { |
| 985 | // FIXME: This can be more efficient. |
| 986 | return GetAddrOfConstantString(GetStringForStringLiteral(S)); |
| 987 | } |
| 988 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 989 | /// GenerateWritableString -- Creates storage for a string literal. |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 990 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
| 991 | bool constant, |
Daniel Dunbar | a70e1d7 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 992 | CodeGenModule &CGM, |
| 993 | const char *GlobalName) { |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 994 | // Create Constant for this string literal. Don't add a '\0'. |
| 995 | llvm::Constant *C = llvm::ConstantArray::get(str, false); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 996 | |
| 997 | // Create a global variable for this string |
| 998 | C = new llvm::GlobalVariable(C->getType(), constant, |
| 999 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | a70e1d7 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1000 | C, |
| 1001 | GlobalName ? GlobalName : ".str", |
| 1002 | &CGM.getModule()); |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1003 | |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1004 | return C; |
| 1005 | } |
| 1006 | |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1007 | /// GetAddrOfConstantString - Returns a pointer to a character array |
| 1008 | /// containing the literal. This contents are exactly that of the |
| 1009 | /// given string, i.e. it will not be null terminated automatically; |
| 1010 | /// see GetAddrOfConstantCString. Note that whether the result is |
| 1011 | /// actually a pointer to an LLVM constant depends on |
| 1012 | /// Feature.WriteableStrings. |
| 1013 | /// |
| 1014 | /// The result has pointer to array type. |
Daniel Dunbar | a70e1d7 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1015 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, |
| 1016 | const char *GlobalName) { |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1017 | // Don't share any string literals if writable-strings is turned on. |
| 1018 | if (Features.WritableStrings) |
Daniel Dunbar | a70e1d7 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1019 | return GenerateStringLiteral(str, false, *this, GlobalName); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1020 | |
| 1021 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 1022 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 1023 | |
| 1024 | if (Entry.getValue()) |
| 1025 | return Entry.getValue(); |
| 1026 | |
| 1027 | // Create a global variable for this. |
Daniel Dunbar | a70e1d7 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1028 | llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1029 | Entry.setValue(C); |
| 1030 | return C; |
| 1031 | } |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1032 | |
| 1033 | /// GetAddrOfConstantCString - Returns a pointer to a character |
| 1034 | /// array containing the literal and a terminating '\-' |
| 1035 | /// character. The result has pointer to array type. |
Daniel Dunbar | a70e1d7 | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1036 | llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, |
| 1037 | const char *GlobalName){ |
Chris Lattner | b217601 | 2008-12-09 19:10:54 +0000 | [diff] [blame] | 1038 | return GetAddrOfConstantString(str + '\0', GlobalName); |
Daniel Dunbar | 31fe9c3 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1039 | } |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1040 | |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1041 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 1042 | /// properties for an implementation. |
| 1043 | void CodeGenModule::EmitObjCPropertyImplementations(const |
| 1044 | ObjCImplementationDecl *D) { |
| 1045 | for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), |
| 1046 | e = D->propimpl_end(); i != e; ++i) { |
| 1047 | ObjCPropertyImplDecl *PID = *i; |
| 1048 | |
| 1049 | // Dynamic is just for type-checking. |
| 1050 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1051 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1052 | |
| 1053 | // Determine which methods need to be implemented, some may have |
| 1054 | // been overridden. Note that ::isSynthesized is not the method |
| 1055 | // we want, that just indicates if the decl came from a |
| 1056 | // property. What we want to know is if the method is defined in |
| 1057 | // this implementation. |
| 1058 | if (!D->getInstanceMethod(PD->getGetterName())) |
Fariborz Jahanian | 91dd9d3 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1059 | CodeGenFunction(*this).GenerateObjCGetter( |
| 1060 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1061 | if (!PD->isReadOnly() && |
| 1062 | !D->getInstanceMethod(PD->getSetterName())) |
Fariborz Jahanian | 91dd9d3 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1063 | CodeGenFunction(*this).GenerateObjCSetter( |
| 1064 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1069 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 1070 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
| 1071 | // If an error has occurred, stop code generation, but continue |
| 1072 | // parsing and semantic analysis (to ensure all warnings and errors |
| 1073 | // are emitted). |
| 1074 | if (Diags.hasErrorOccurred()) |
| 1075 | return; |
| 1076 | |
| 1077 | switch (D->getKind()) { |
| 1078 | case Decl::Function: |
| 1079 | case Decl::Var: |
| 1080 | EmitGlobal(cast<ValueDecl>(D)); |
| 1081 | break; |
| 1082 | |
| 1083 | case Decl::Namespace: |
Daniel Dunbar | 952f473 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 1084 | ErrorUnsupported(D, "namespace"); |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1085 | break; |
| 1086 | |
| 1087 | // Objective-C Decls |
| 1088 | |
| 1089 | // Forward declarations, no (immediate) code generation. |
| 1090 | case Decl::ObjCClass: |
| 1091 | case Decl::ObjCCategory: |
| 1092 | case Decl::ObjCForwardProtocol: |
| 1093 | case Decl::ObjCInterface: |
| 1094 | break; |
| 1095 | |
| 1096 | case Decl::ObjCProtocol: |
| 1097 | Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); |
| 1098 | break; |
| 1099 | |
| 1100 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1101 | // Categories have properties but don't support synthesize so we |
| 1102 | // can ignore them here. |
| 1103 | |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1104 | Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
| 1105 | break; |
| 1106 | |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1107 | case Decl::ObjCImplementation: { |
| 1108 | ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); |
| 1109 | EmitObjCPropertyImplementations(OMD); |
| 1110 | Runtime->GenerateClass(OMD); |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1111 | break; |
Daniel Dunbar | 6b57d43 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1112 | } |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1113 | case Decl::ObjCMethod: { |
| 1114 | ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); |
| 1115 | // If this is not a prototype, emit the body. |
| 1116 | if (OMD->getBody()) |
| 1117 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 1118 | break; |
| 1119 | } |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1120 | case Decl::ObjCCompatibleAlias: |
Fariborz Jahanian | 2ac4cd3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1121 | // compatibility-alias is a directive and has no code gen. |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1122 | break; |
| 1123 | |
| 1124 | case Decl::LinkageSpec: { |
| 1125 | LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D); |
| 1126 | if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 1127 | ErrorUnsupported(LSD, "linkage spec"); |
Daniel Dunbar | 27250d3 | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1128 | // FIXME: implement C++ linkage, C linkage works mostly by C |
| 1129 | // language reuse already. |
| 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | case Decl::FileScopeAsm: { |
| 1134 | FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); |
| 1135 | std::string AsmString(AD->getAsmString()->getStrData(), |
| 1136 | AD->getAsmString()->getByteLength()); |
| 1137 | |
| 1138 | const std::string &S = getModule().getModuleInlineAsm(); |
| 1139 | if (S.empty()) |
| 1140 | getModule().setModuleInlineAsm(AsmString); |
| 1141 | else |
| 1142 | getModule().setModuleInlineAsm(S + '\n' + AsmString); |
| 1143 | break; |
| 1144 | } |
| 1145 | |
| 1146 | default: |
| 1147 | // Make sure we handled everything we should, every other kind is |
| 1148 | // a non-top-level decl. FIXME: Would be nice to have an |
| 1149 | // isTopLevelDeclKind function. Need to recode Decl::Kind to do |
| 1150 | // that easily. |
| 1151 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
| 1152 | } |
| 1153 | } |