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