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