Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-module state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "CodeGenModule.h" |
| 16 | #include "CodeGenFunction.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 17 | #include "CGCall.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 19 | #include "Mangle.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Diagnostic.h" |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Nate Begeman | ec9426c | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 26 | #include "llvm/CallingConv.h" |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 27 | #include "llvm/Module.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | #include "llvm/Intrinsics.h" |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
| 33 | |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 34 | CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 35 | llvm::Module &M, const llvm::TargetData &TD, |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 36 | Diagnostic &diags, bool GenerateDebugInfo) |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 37 | : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags), |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 38 | Types(C, M, TD), Runtime(0), MemCpyFn(0), MemMoveFn(0), MemSetFn(0), |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 39 | CFConstantStringClassRef(0), NSConcreteGlobalBlock(0), |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 40 | BlockDescriptorType(0), GenericBlockLiteralType(0) { |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 41 | |
| 42 | if (Features.ObjC1) { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 43 | if (Features.NeXTRuntime) { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 44 | Runtime = Features.ObjCNonFragileABI ? CreateMacNonFragileABIObjCRuntime(*this) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 45 | : CreateMacObjCRuntime(*this); |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 46 | } else { |
| 47 | Runtime = CreateGNUObjCRuntime(*this); |
| 48 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 49 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 50 | |
| 51 | // If debug info generation is enabled, create the CGDebugInfo object. |
Mike Stump | 26efc33 | 2009-02-13 18:36:05 +0000 | [diff] [blame] | 52 | DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0; |
| 53 | |
| 54 | Block.GlobalUniqueCount = 0; |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | CodeGenModule::~CodeGenModule() { |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 58 | delete Runtime; |
| 59 | delete DebugInfo; |
| 60 | } |
| 61 | |
| 62 | void CodeGenModule::Release() { |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 63 | EmitDeferred(); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 64 | EmitAliases(); |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 65 | if (Runtime) |
| 66 | if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) |
| 67 | AddGlobalCtor(ObjCInitFunction); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 68 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 69 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 70 | EmitAnnotations(); |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 71 | EmitLLVMUsed(); |
Daniel Dunbar | f1968f2 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 72 | BindRuntimeFunctions(); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 73 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | |
Daniel Dunbar | f1968f2 | 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 | 0293d54 | 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 | f1968f2 | 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 | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 113 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 114 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 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 | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 119 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Daniel Dunbar | 56b8001 | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 120 | "cannot compile this %0 yet"); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 121 | std::string Msg = Type; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 122 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) |
| 123 | << Msg << S->getSourceRange(); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 124 | } |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 125 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 126 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 127 | /// specified decl yet. |
Daniel Dunbar | 90df4b6 | 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 | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 132 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Daniel Dunbar | 56b8001 | 2009-02-06 19:18:03 +0000 | [diff] [blame] | 133 | "cannot compile this %0 yet"); |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 134 | std::string Msg = Type; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 135 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Daniel Dunbar | 41071de | 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 | 4f8d123 | 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 | 5f2bfd4 | 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 | 6d39760 | 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 | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 181 | void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 182 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 183 | GlobalCtors.push_back(std::make_pair(Ctor, Priority)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Daniel Dunbar | 6bfed7e | 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 | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 189 | // FIXME: Type coercion of void()* types. |
Daniel Dunbar | 6bfed7e | 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 | 572cf09 | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 202 | llvm::StructType* CtorStructTy = |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 203 | llvm::StructType::get(llvm::Type::Int32Ty, |
| 204 | llvm::PointerType::getUnqual(CtorFTy), NULL); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 205 | |
Daniel Dunbar | 6bfed7e | 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 | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 213 | } |
Daniel Dunbar | 6bfed7e | 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 | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Nate Begeman | 532485c | 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 | 5c61d97 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 241 | void CodeGenModule::SetGlobalValueAttributes(const Decl *D, |
| 242 | bool IsInternal, |
| 243 | bool IsInline, |
| 244 | llvm::GlobalValue *GV, |
| 245 | bool ForDefinition) { |
Daniel Dunbar | 4998888 | 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 | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 247 | // approximation of what we really want. |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 248 | if (!ForDefinition) { |
| 249 | // Only a few attributes are set on declarations. |
Anton Korobeynikov | 2f40270 | 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 | 3ef5db6 | 2009-01-05 20:53:53 +0000 | [diff] [blame] | 253 | if (!D->getAttr<DLLExportAttr>()) { |
Anton Korobeynikov | 2f40270 | 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 | 3ef5db6 | 2009-01-05 20:53:53 +0000 | [diff] [blame] | 261 | } |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 262 | } |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 263 | } else { |
| 264 | if (IsInternal) { |
| 265 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 266 | } else { |
Anton Korobeynikov | 2f40270 | 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 | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 275 | GV->setLinkage(llvm::Function::WeakLinkage); |
| 276 | } |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 277 | } |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 278 | |
Daniel Dunbar | 4998888 | 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 | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 281 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 282 | setGlobalVisibility(GV, attr->getVisibility()); |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 283 | // FIXME: else handle -fvisibility |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 284 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 285 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
Daniel Dunbar | a735ad8 | 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 | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 290 | |
| 291 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
| 292 | GV->setSection(SA->getName()); |
Daniel Dunbar | 5c61d97 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 293 | |
| 294 | // Only add to llvm.used when we see a definition, otherwise we |
| 295 | // might add multiple times or risk the value being replaced by a |
| 296 | // subsequent RAUW. |
| 297 | if (ForDefinition) { |
| 298 | if (D->getAttr<UsedAttr>()) |
| 299 | AddUsedGlobal(GV); |
| 300 | } |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 303 | void CodeGenModule::SetFunctionAttributes(const Decl *D, |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 304 | const CGFunctionInfo &Info, |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 305 | llvm::Function *F) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 306 | AttributeListType AttributeList; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 307 | ConstructAttributeList(Info, D, AttributeList); |
Eli Friedman | c134fcb | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 308 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 309 | F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
| 310 | AttributeList.size())); |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 311 | |
| 312 | // Set the appropriate calling convention for the Function. |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 313 | if (D->getAttr<FastCallAttr>()) |
Anton Korobeynikov | f1c9c09 | 2008-11-11 20:21:14 +0000 | [diff] [blame] | 314 | F->setCallingConv(llvm::CallingConv::X86_FastCall); |
| 315 | |
| 316 | if (D->getAttr<StdCallAttr>()) |
| 317 | F->setCallingConv(llvm::CallingConv::X86_StdCall); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /// SetFunctionAttributesForDefinition - Set function attributes |
| 321 | /// specific to a function definition. |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 322 | void CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, |
| 323 | llvm::Function *F) { |
| 324 | if (isa<ObjCMethodDecl>(D)) { |
| 325 | SetGlobalValueAttributes(D, true, false, F, true); |
| 326 | } else { |
| 327 | const FunctionDecl *FD = cast<FunctionDecl>(D); |
| 328 | SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, |
| 329 | FD->isInline(), F, true); |
| 330 | } |
| 331 | |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 332 | if (!Features.Exceptions) |
Daniel Dunbar | f93349f | 2008-09-27 07:16:42 +0000 | [diff] [blame] | 333 | F->addFnAttr(llvm::Attribute::NoUnwind); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 334 | |
| 335 | if (D->getAttr<AlwaysInlineAttr>()) |
| 336 | F->addFnAttr(llvm::Attribute::AlwaysInline); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, |
| 340 | llvm::Function *F) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 341 | SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 342 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 343 | SetFunctionAttributesForDefinition(MD, F); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, |
| 347 | llvm::Function *F) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 348 | SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 349 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 350 | SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, |
| 351 | FD->isInline(), F, false); |
| 352 | } |
| 353 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 354 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 355 | void CodeGenModule::EmitAliases() { |
| 356 | for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { |
| 357 | const FunctionDecl *D = Aliases[i]; |
| 358 | const AliasAttr *AA = D->getAttr<AliasAttr>(); |
| 359 | |
| 360 | // This is something of a hack, if the FunctionDecl got overridden |
| 361 | // then its attributes will be moved to the new declaration. In |
| 362 | // this case the current decl has no alias attribute, but we will |
| 363 | // eventually see it. |
| 364 | if (!AA) |
| 365 | continue; |
| 366 | |
| 367 | const std::string& aliaseeName = AA->getAliasee(); |
| 368 | llvm::Function *aliasee = getModule().getFunction(aliaseeName); |
| 369 | if (!aliasee) { |
| 370 | // FIXME: This isn't unsupported, this is just an error, which |
| 371 | // sema should catch, but... |
| 372 | ErrorUnsupported(D, "alias referencing a missing function"); |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | llvm::GlobalValue *GA = |
| 377 | new llvm::GlobalAlias(aliasee->getType(), |
| 378 | llvm::Function::ExternalLinkage, |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 379 | getMangledName(D)->getName(), aliasee, |
| 380 | &getModule()); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 381 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 382 | llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 383 | if (Entry) { |
| 384 | // If we created a dummy function for this then replace it. |
| 385 | GA->takeName(Entry); |
| 386 | |
| 387 | llvm::Value *Casted = |
| 388 | llvm::ConstantExpr::getBitCast(GA, Entry->getType()); |
| 389 | Entry->replaceAllUsesWith(Casted); |
| 390 | Entry->eraseFromParent(); |
| 391 | |
| 392 | Entry = GA; |
| 393 | } |
| 394 | |
| 395 | // Alias should never be internal or inline. |
| 396 | SetGlobalValueAttributes(D, false, false, GA, true); |
| 397 | } |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 400 | void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { |
| 401 | assert(!GV->isDeclaration() && |
| 402 | "Only globals with definition can force usage."); |
| 403 | llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 404 | LLVMUsed.push_back(llvm::ConstantExpr::getBitCast(GV, i8PTy)); |
| 405 | } |
| 406 | |
| 407 | void CodeGenModule::EmitLLVMUsed() { |
| 408 | // Don't create llvm.used if there is no need. |
| 409 | if (LLVMUsed.empty()) |
| 410 | return; |
| 411 | |
| 412 | llvm::ArrayType *ATy = llvm::ArrayType::get(LLVMUsed[0]->getType(), |
| 413 | LLVMUsed.size()); |
| 414 | llvm::GlobalVariable *GV = |
| 415 | new llvm::GlobalVariable(ATy, false, |
| 416 | llvm::GlobalValue::AppendingLinkage, |
| 417 | llvm::ConstantArray::get(ATy, LLVMUsed), |
| 418 | "llvm.used", &getModule()); |
| 419 | |
| 420 | GV->setSection("llvm.metadata"); |
| 421 | } |
| 422 | |
| 423 | void CodeGenModule::EmitDeferred() { |
| 424 | // Emit code for any deferred decl which was used. Since a |
| 425 | // previously unused static decl may become used during the |
| 426 | // generation of code for a static function, iterate until no |
| 427 | // changes are made. |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 428 | bool Changed; |
| 429 | do { |
| 430 | Changed = false; |
Anders Carlsson | b723f75 | 2009-01-04 02:08:04 +0000 | [diff] [blame] | 431 | |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 432 | for (std::list<const ValueDecl*>::iterator i = DeferredDecls.begin(), |
| 433 | e = DeferredDecls.end(); i != e; ) { |
Anders Carlsson | b723f75 | 2009-01-04 02:08:04 +0000 | [diff] [blame] | 434 | const ValueDecl *D = *i; |
| 435 | |
Eli Friedman | 6f7e2ee | 2008-05-27 04:58:01 +0000 | [diff] [blame] | 436 | // Check if we have used a decl with the same name |
| 437 | // FIXME: The AST should have some sort of aggregate decls or |
| 438 | // global symbol map. |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 439 | // FIXME: This is missing some important cases. For example, we |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 440 | // need to check for uses in an alias. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 441 | if (!GlobalDeclMap.count(getMangledName(D))) { |
Anders Carlsson | b723f75 | 2009-01-04 02:08:04 +0000 | [diff] [blame] | 442 | i++; |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 443 | continue; |
Anders Carlsson | b723f75 | 2009-01-04 02:08:04 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 446 | // Emit the definition. |
| 447 | EmitGlobalDefinition(D); |
| 448 | |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 449 | // Erase the used decl from the list. |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 450 | i = DeferredDecls.erase(i); |
Anders Carlsson | b723f75 | 2009-01-04 02:08:04 +0000 | [diff] [blame] | 451 | |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 452 | // Remember that we made a change. |
| 453 | Changed = true; |
| 454 | } |
| 455 | } while (Changed); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 458 | /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the |
| 459 | /// annotation information for a given GlobalValue. The annotation struct is |
| 460 | /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 461 | /// GlobalValue being annotated. The second field is the constant string |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 462 | /// created from the AnnotateAttr's annotation. The third field is a constant |
| 463 | /// string containing the name of the translation unit. The fourth field is |
| 464 | /// the line number in the file of the annotated value declaration. |
| 465 | /// |
| 466 | /// FIXME: this does not unique the annotation string constants, as llvm-gcc |
| 467 | /// appears to. |
| 468 | /// |
| 469 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
| 470 | const AnnotateAttr *AA, |
| 471 | unsigned LineNo) { |
| 472 | llvm::Module *M = &getModule(); |
| 473 | |
| 474 | // get [N x i8] constants for the annotation string, and the filename string |
| 475 | // which are the 2nd and 3rd elements of the global annotation structure. |
| 476 | const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 477 | llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); |
| 478 | llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), |
| 479 | true); |
| 480 | |
| 481 | // Get the two global values corresponding to the ConstantArrays we just |
| 482 | // created to hold the bytes of the strings. |
| 483 | llvm::GlobalValue *annoGV = |
| 484 | new llvm::GlobalVariable(anno->getType(), false, |
| 485 | llvm::GlobalValue::InternalLinkage, anno, |
| 486 | GV->getName() + ".str", M); |
| 487 | // translation unit name string, emitted into the llvm.metadata section. |
| 488 | llvm::GlobalValue *unitGV = |
| 489 | new llvm::GlobalVariable(unit->getType(), false, |
| 490 | llvm::GlobalValue::InternalLinkage, unit, ".str", M); |
| 491 | |
| 492 | // Create the ConstantStruct that is the global annotion. |
| 493 | llvm::Constant *Fields[4] = { |
| 494 | llvm::ConstantExpr::getBitCast(GV, SBP), |
| 495 | llvm::ConstantExpr::getBitCast(annoGV, SBP), |
| 496 | llvm::ConstantExpr::getBitCast(unitGV, SBP), |
| 497 | llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) |
| 498 | }; |
| 499 | return llvm::ConstantStruct::get(Fields, 4, false); |
| 500 | } |
| 501 | |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 502 | bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { |
Daniel Dunbar | 5c61d97 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 503 | // Never defer when EmitAllDecls is specified or the decl has |
| 504 | // attribute used. |
| 505 | if (Features.EmitAllDecls || Global->getAttr<UsedAttr>()) |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 506 | return false; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 507 | |
| 508 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 509 | // Constructors and destructors should never be deferred. |
| 510 | if (FD->getAttr<ConstructorAttr>() || FD->getAttr<DestructorAttr>()) |
| 511 | return false; |
| 512 | |
| 513 | if (FD->getStorageClass() != FunctionDecl::Static) |
| 514 | return false; |
| 515 | } else { |
| 516 | const VarDecl *VD = cast<VarDecl>(Global); |
| 517 | assert(VD->isFileVarDecl() && "Invalid decl."); |
| 518 | |
| 519 | if (VD->getStorageClass() != VarDecl::Static) |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | void CodeGenModule::EmitGlobal(const ValueDecl *Global) { |
| 527 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 528 | // Aliases are deferred until code for everything else has been |
| 529 | // emitted. |
| 530 | if (FD->getAttr<AliasAttr>()) { |
| 531 | assert(!FD->isThisDeclarationADefinition() && |
| 532 | "Function alias cannot have a definition!"); |
| 533 | Aliases.push_back(FD); |
| 534 | return; |
| 535 | } |
| 536 | |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 537 | // Forward declarations are emitted lazily on first use. |
| 538 | if (!FD->isThisDeclarationADefinition()) |
| 539 | return; |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 540 | } else { |
| 541 | const VarDecl *VD = cast<VarDecl>(Global); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 542 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
| 543 | |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 544 | // Forward declarations are emitted lazily on first use. |
Daniel Dunbar | 7542bca | 2009-02-13 22:49:13 +0000 | [diff] [blame] | 545 | if (!VD->getInit() && VD->hasExternalStorage()) |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 546 | return; |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Daniel Dunbar | 73241df | 2009-02-13 21:18:01 +0000 | [diff] [blame] | 549 | // Defer code generation when possible. |
| 550 | if (MayDeferGeneration(Global)) { |
Daniel Dunbar | 0269871 | 2009-02-13 20:29:50 +0000 | [diff] [blame] | 551 | DeferredDecls.push_back(Global); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 552 | return; |
| 553 | } |
| 554 | |
| 555 | // Otherwise emit the definition. |
| 556 | EmitGlobalDefinition(Global); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 559 | void CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { |
| 560 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 561 | EmitGlobalFunctionDefinition(FD); |
| 562 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 563 | EmitGlobalVarDefinition(VD); |
| 564 | } else { |
| 565 | assert(0 && "Invalid argument to EmitGlobalDefinition()"); |
| 566 | } |
| 567 | } |
| 568 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 569 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) { |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 570 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 571 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 572 | QualType ASTTy = D->getType(); |
| 573 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 574 | const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 575 | |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 576 | // Lookup the entry, lazily creating it if necessary. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 577 | llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 578 | if (!Entry) { |
| 579 | llvm::GlobalVariable *GV = |
| 580 | new llvm::GlobalVariable(Ty, false, |
| 581 | llvm::GlobalValue::ExternalLinkage, |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 582 | 0, getMangledName(D)->getName(), &getModule(), |
| 583 | 0, ASTTy.getAddressSpace()); |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 584 | Entry = GV; |
| 585 | |
| 586 | // Handle things which are present even on external declarations. |
| 587 | |
| 588 | // FIXME: This code is overly simple and should be merged with |
| 589 | // other global handling. |
| 590 | |
| 591 | GV->setConstant(D->getType().isConstant(Context)); |
| 592 | |
| 593 | if (D->getStorageClass() == VarDecl::PrivateExtern) |
| 594 | setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility); |
| 595 | } |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 596 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 597 | // Make sure the result is of the correct type. |
| 598 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 602 | llvm::Constant *Init = 0; |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 603 | QualType ASTTy = D->getType(); |
| 604 | const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 606 | if (D->getInit() == 0) { |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 607 | // This is a tentative definition; tentative definitions are |
| 608 | // implicitly initialized with { 0 } |
| 609 | const llvm::Type* InitTy; |
| 610 | if (ASTTy->isIncompleteArrayType()) { |
| 611 | // An incomplete array is normally [ TYPE x 0 ], but we need |
| 612 | // to fix it to [ TYPE x 1 ]. |
| 613 | const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy); |
| 614 | InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); |
| 615 | } else { |
| 616 | InitTy = VarTy; |
| 617 | } |
| 618 | Init = llvm::Constant::getNullValue(InitTy); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 619 | } else { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 620 | Init = EmitConstantExpr(D->getInit()); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 621 | } |
| 622 | const llvm::Type* InitType = Init->getType(); |
| 623 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 624 | llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 625 | llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry); |
| 626 | |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 627 | if (!GV) { |
| 628 | GV = new llvm::GlobalVariable(InitType, false, |
| 629 | llvm::GlobalValue::ExternalLinkage, |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 630 | 0, getMangledName(D)->getName(), |
| 631 | &getModule(), 0, ASTTy.getAddressSpace()); |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 632 | } else if (GV->getType() != |
| 633 | llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) { |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 634 | // We have a definition after a prototype with the wrong type. |
| 635 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 636 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 637 | // (which will be a definition). |
| 638 | // |
| 639 | // This happens if there is a prototype for a global (e.g. "extern int x[];") |
| 640 | // and then a definition of a different type (e.g. "int x[10];"). This also |
| 641 | // happens when an initializer has a different type from the type of the |
| 642 | // global (this happens with unions). |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 643 | // |
| 644 | // FIXME: This also ends up happening if there's a definition followed by |
| 645 | // a tentative definition! (Although Sema rejects that construct |
| 646 | // at the moment.) |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 647 | |
| 648 | // Save the old global |
| 649 | llvm::GlobalVariable *OldGV = GV; |
| 650 | |
| 651 | // Make a new global with the correct type |
| 652 | GV = new llvm::GlobalVariable(InitType, false, |
| 653 | llvm::GlobalValue::ExternalLinkage, |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 654 | 0, getMangledName(D)->getName(), |
| 655 | &getModule(), 0, ASTTy.getAddressSpace()); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 656 | // Steal the name of the old global |
| 657 | GV->takeName(OldGV); |
| 658 | |
| 659 | // Replace all uses of the old global with the new global |
| 660 | llvm::Constant *NewPtrForOldDecl = |
| 661 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 662 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 663 | |
| 664 | // Erase the old global, since it is no longer used. |
| 665 | OldGV->eraseFromParent(); |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 666 | } |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 667 | |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 668 | Entry = GV; |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 669 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 670 | if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { |
| 671 | SourceManager &SM = Context.getSourceManager(); |
| 672 | AddAnnotation(EmitAnnotateAttr(GV, AA, |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 673 | SM.getInstantiationLineNumber(D->getLocation()))); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 676 | GV->setInitializer(Init); |
Nuno Lopes | b381aac | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 677 | GV->setConstant(D->getType().isConstant(Context)); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 678 | |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 679 | // FIXME: This is silly; getTypeAlign should just work for incomplete arrays |
| 680 | unsigned Align; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 681 | if (const IncompleteArrayType* IAT = |
| 682 | Context.getAsIncompleteArrayType(D->getType())) |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 683 | Align = Context.getTypeAlign(IAT->getElementType()); |
| 684 | else |
| 685 | Align = Context.getTypeAlign(D->getType()); |
Daniel Dunbar | 6aee306 | 2009-02-18 19:45:21 +0000 | [diff] [blame^] | 686 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) |
Eli Friedman | 08d7802 | 2008-05-29 11:10:27 +0000 | [diff] [blame] | 687 | Align = std::max(Align, AA->getAlignment()); |
Eli Friedman | 08d7802 | 2008-05-29 11:10:27 +0000 | [diff] [blame] | 688 | GV->setAlignment(Align / 8); |
| 689 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 690 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 691 | setGlobalVisibility(GV, attr->getVisibility()); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 692 | // FIXME: else handle -fvisibility |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 693 | |
| 694 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
| 695 | // Prefaced with special LLVM marker to indicate that the name |
| 696 | // should not be munged. |
| 697 | GV->setName("\01" + ALA->getLabel()); |
| 698 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 699 | |
| 700 | // Set the llvm linkage type as appropriate. |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 701 | if (D->getStorageClass() == VarDecl::Static) |
| 702 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 703 | else if (D->getAttr<DLLImportAttr>()) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 704 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 705 | else if (D->getAttr<DLLExportAttr>()) |
| 706 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 707 | else if (D->getAttr<WeakAttr>()) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 708 | GV->setLinkage(llvm::GlobalVariable::WeakLinkage); |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 709 | else { |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 710 | // FIXME: This isn't right. This should handle common linkage and other |
| 711 | // stuff. |
| 712 | switch (D->getStorageClass()) { |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 713 | case VarDecl::Static: assert(0 && "This case handled above"); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 714 | case VarDecl::Auto: |
| 715 | case VarDecl::Register: |
| 716 | assert(0 && "Can't have auto or register globals"); |
| 717 | case VarDecl::None: |
| 718 | if (!D->getInit()) |
Eli Friedman | a07b764 | 2008-05-29 11:03:17 +0000 | [diff] [blame] | 719 | GV->setLinkage(llvm::GlobalVariable::CommonLinkage); |
Anders Carlsson | 98883e1 | 2008-12-03 05:51:23 +0000 | [diff] [blame] | 720 | else |
| 721 | GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 722 | break; |
| 723 | case VarDecl::Extern: |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 724 | // FIXME: common |
| 725 | break; |
| 726 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 727 | case VarDecl::PrivateExtern: |
Daniel Dunbar | 4998888 | 2009-01-13 02:25:00 +0000 | [diff] [blame] | 728 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 729 | // FIXME: common |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 730 | break; |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 731 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 732 | } |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 733 | |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 734 | if (const SectionAttr *SA = D->getAttr<SectionAttr>()) |
| 735 | GV->setSection(SA->getName()); |
| 736 | |
Daniel Dunbar | 5c61d97 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 737 | if (D->getAttr<UsedAttr>()) |
| 738 | AddUsedGlobal(GV); |
| 739 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 740 | // Emit global variable debug information. |
| 741 | CGDebugInfo *DI = getDebugInfo(); |
| 742 | if(DI) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 743 | DI->setLocation(D->getLocation()); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 744 | DI->EmitGlobalVariable(GV, D); |
| 745 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 746 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 747 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 748 | llvm::GlobalValue * |
| 749 | CodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D) { |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 750 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 751 | llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), |
| 752 | llvm::Function::ExternalLinkage, |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 753 | getMangledName(D)->getName(), |
| 754 | &getModule()); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 755 | SetFunctionAttributes(D, F); |
| 756 | return F; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | llvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) { |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 760 | QualType ASTTy = D->getType(); |
| 761 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
| 762 | const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 763 | |
| 764 | // Lookup the entry, lazily creating it if necessary. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 765 | llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 766 | if (!Entry) |
| 767 | Entry = EmitForwardFunctionDefinition(D); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 768 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 769 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | void CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 773 | llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 774 | if (!Entry) { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 775 | Entry = EmitForwardFunctionDefinition(D); |
| 776 | } else { |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 777 | // If the types mismatch then we have to rewrite the definition. |
| 778 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 779 | if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 780 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 781 | // F is the Function* for the one with the wrong type, we must make a new |
| 782 | // Function* and update everything that used F (a declaration) with the new |
| 783 | // Function* (which will be a definition). |
| 784 | // |
| 785 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 786 | // then a definition of a different type (e.g. "int f(int x)"). Start by |
| 787 | // making a new function of the correct type, RAUW, then steal the name. |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 788 | llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D); |
| 789 | NewFn->takeName(Entry); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 790 | |
| 791 | // Replace uses of F with the Function we will endow with a body. |
| 792 | llvm::Constant *NewPtrForOldDecl = |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 793 | llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); |
| 794 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
| 795 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 796 | // Ok, delete the old function now, which is dead. |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 797 | assert(Entry->isDeclaration() && "Shouldn't replace non-declaration"); |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 798 | Entry->eraseFromParent(); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 799 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 800 | Entry = NewFn; |
| 801 | } |
| 802 | } |
| 803 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 804 | llvm::Function *Fn = cast<llvm::Function>(Entry); |
| 805 | CodeGenFunction(*this).GenerateCode(D, Fn); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 806 | |
Daniel Dunbar | 219df66 | 2008-09-08 23:44:31 +0000 | [diff] [blame] | 807 | SetFunctionAttributesForDefinition(D, Fn); |
| 808 | |
| 809 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) { |
| 810 | AddGlobalCtor(Fn, CA->getPriority()); |
| 811 | } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) { |
| 812 | AddGlobalDtor(Fn, DA->getPriority()); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Daniel Dunbar | f1968f2 | 2008-10-01 00:49:24 +0000 | [diff] [blame] | 816 | llvm::Function * |
| 817 | CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, |
| 818 | const std::string &Name) { |
| 819 | llvm::Function *Fn = llvm::Function::Create(FTy, |
| 820 | llvm::Function::ExternalLinkage, |
| 821 | "", &TheModule); |
| 822 | RuntimeFunctions.push_back(std::make_pair(Fn, Name)); |
| 823 | return Fn; |
| 824 | } |
| 825 | |
Chris Lattner | c5b8806 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 826 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 827 | // Make sure that this type is translated. |
| 828 | Types.UpdateCompletedType(TD); |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 832 | /// getBuiltinLibFunction |
| 833 | llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
Chris Lattner | 1426fec | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 834 | if (BuiltinID > BuiltinFunctions.size()) |
| 835 | BuiltinFunctions.resize(BuiltinID); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 836 | |
Chris Lattner | 1426fec | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 837 | // Cache looked up functions. Since builtin id #0 is invalid we don't reserve |
| 838 | // a slot for it. |
| 839 | assert(BuiltinID && "Invalid Builtin ID"); |
| 840 | llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 841 | if (FunctionSlot) |
| 842 | return FunctionSlot; |
| 843 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 844 | assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || |
| 845 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && |
| 846 | "isn't a lib fn"); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 847 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 848 | // Get the name, skip over the __builtin_ prefix (if necessary). |
| 849 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID); |
| 850 | if (Context.BuiltinInfo.isLibFunction(BuiltinID)) |
| 851 | Name += 10; |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 852 | |
| 853 | // Get the type for the builtin. |
Douglas Gregor | 370ab3f | 2009-02-14 01:52:53 +0000 | [diff] [blame] | 854 | Builtin::Context::GetBuiltinTypeError Error; |
| 855 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context, Error); |
| 856 | assert(Error == Builtin::Context::GE_None && "Can't get builtin type"); |
| 857 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 858 | const llvm::FunctionType *Ty = |
| 859 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 860 | |
| 861 | // FIXME: This has a serious problem with code like this: |
| 862 | // void abs() {} |
| 863 | // ... __builtin_abs(x); |
| 864 | // The two versions of abs will collide. The fix is for the builtin to win, |
| 865 | // and for the existing one to be turned into a constantexpr cast of the |
| 866 | // builtin. In the case where the existing one is a static function, it |
| 867 | // should just be renamed. |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 868 | if (llvm::Function *Existing = getModule().getFunction(Name)) { |
| 869 | if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) |
| 870 | return FunctionSlot = Existing; |
| 871 | assert(Existing == 0 && "FIXME: Name collision"); |
| 872 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 873 | |
| 874 | // FIXME: param attributes for sext/zext etc. |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 875 | return FunctionSlot = |
| 876 | llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, |
| 877 | &getModule()); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 880 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, |
| 881 | unsigned NumTys) { |
| 882 | return llvm::Intrinsic::getDeclaration(&getModule(), |
| 883 | (llvm::Intrinsic::ID)IID, Tys, NumTys); |
| 884 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 885 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 886 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 887 | if (MemCpyFn) return MemCpyFn; |
Chris Lattner | 4e8a9e8 | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 888 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); |
| 889 | return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 890 | } |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 891 | |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 892 | llvm::Function *CodeGenModule::getMemMoveFn() { |
| 893 | if (MemMoveFn) return MemMoveFn; |
Chris Lattner | 4e8a9e8 | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 894 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); |
| 895 | return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 898 | llvm::Function *CodeGenModule::getMemSetFn() { |
| 899 | if (MemSetFn) return MemSetFn; |
Chris Lattner | 4e8a9e8 | 2008-11-21 16:43:15 +0000 | [diff] [blame] | 900 | const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); |
| 901 | return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 902 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 903 | |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 904 | static void appendFieldAndPadding(CodeGenModule &CGM, |
| 905 | std::vector<llvm::Constant*>& Fields, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 906 | FieldDecl *FieldD, FieldDecl *NextFieldD, |
| 907 | llvm::Constant* Field, |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 908 | RecordDecl* RD, const llvm::StructType *STy) |
| 909 | { |
| 910 | // Append the field. |
| 911 | Fields.push_back(Field); |
| 912 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 913 | int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 914 | |
| 915 | int NextStructFieldNo; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 916 | if (!NextFieldD) { |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 917 | NextStructFieldNo = STy->getNumElements(); |
| 918 | } else { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 919 | NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | // Append padding |
| 923 | for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { |
| 924 | llvm::Constant *C = |
| 925 | llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); |
| 926 | |
| 927 | Fields.push_back(C); |
| 928 | } |
| 929 | } |
| 930 | |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 931 | // We still need to work out the details of handling UTF-16. |
| 932 | // See: <rdr://2996215> |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 933 | llvm::Constant *CodeGenModule:: |
| 934 | GetAddrOfConstantCFString(const std::string &str) { |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 935 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 936 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 937 | |
| 938 | if (Entry.getValue()) |
| 939 | return Entry.getValue(); |
| 940 | |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 941 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 942 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 943 | |
| 944 | if (!CFConstantStringClassRef) { |
| 945 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 946 | Ty = llvm::ArrayType::get(Ty, 0); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 947 | |
| 948 | // FIXME: This is fairly broken if |
| 949 | // __CFConstantStringClassReference is already defined, in that it |
| 950 | // will get renamed and the user will most likely see an opaque |
| 951 | // error message. This is a general issue with relying on |
| 952 | // particular names. |
| 953 | llvm::GlobalVariable *GV = |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 954 | new llvm::GlobalVariable(Ty, false, |
| 955 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 956 | "__CFConstantStringClassReference", |
| 957 | &getModule()); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 958 | |
| 959 | // Decay array -> ptr |
| 960 | CFConstantStringClassRef = |
| 961 | llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 964 | QualType CFTy = getContext().getCFConstantStringType(); |
| 965 | RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 966 | |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 967 | const llvm::StructType *STy = |
| 968 | cast<llvm::StructType>(getTypes().ConvertType(CFTy)); |
| 969 | |
| 970 | std::vector<llvm::Constant*> Fields; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 971 | RecordDecl::field_iterator Field = CFRD->field_begin(); |
| 972 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 973 | // Class pointer. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 974 | FieldDecl *CurField = *Field++; |
| 975 | FieldDecl *NextField = *Field++; |
| 976 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
| 977 | CFConstantStringClassRef, CFRD, STy); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 978 | |
| 979 | // Flags. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 980 | CurField = NextField; |
| 981 | NextField = *Field++; |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 982 | const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 983 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
| 984 | llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 985 | |
| 986 | // String pointer. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 987 | CurField = NextField; |
| 988 | NextField = *Field++; |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 989 | llvm::Constant *C = llvm::ConstantArray::get(str); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 990 | C = new llvm::GlobalVariable(C->getType(), true, |
| 991 | llvm::GlobalValue::InternalLinkage, |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 992 | C, ".str", &getModule()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 993 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 994 | llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2), |
| 995 | CFRD, STy); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 996 | |
| 997 | // String length. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 998 | CurField = NextField; |
| 999 | NextField = 0; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1000 | Ty = getTypes().ConvertType(getContext().LongTy); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1001 | appendFieldAndPadding(*this, Fields, CurField, NextField, |
| 1002 | llvm::ConstantInt::get(Ty, str.length()), CFRD, STy); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1003 | |
| 1004 | // The struct. |
Anders Carlsson | e3daa76 | 2008-11-15 18:54:24 +0000 | [diff] [blame] | 1005 | C = llvm::ConstantStruct::get(STy, Fields); |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 1006 | llvm::GlobalVariable *GV = |
| 1007 | new llvm::GlobalVariable(C->getType(), true, |
| 1008 | llvm::GlobalVariable::InternalLinkage, |
| 1009 | C, "", &getModule()); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1010 | |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 1011 | GV->setSection("__DATA,__cfstring"); |
| 1012 | Entry.setValue(GV); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1013 | |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 1014 | return GV; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 1015 | } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1016 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1017 | /// GetStringForStringLiteral - Return the appropriate bytes for a |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1018 | /// string literal, properly padded to match the literal type. |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1019 | std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 1020 | if (E->isWide()) { |
| 1021 | ErrorUnsupported(E, "wide string"); |
| 1022 | return "FIXME"; |
| 1023 | } |
| 1024 | |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 1025 | const char *StrData = E->getStrData(); |
| 1026 | unsigned Len = E->getByteLength(); |
| 1027 | |
| 1028 | const ConstantArrayType *CAT = |
| 1029 | getContext().getAsConstantArrayType(E->getType()); |
| 1030 | assert(CAT && "String isn't pointer or array!"); |
| 1031 | |
| 1032 | // Resize the string to the right size |
| 1033 | // FIXME: What about wchar_t strings? |
| 1034 | std::string Str(StrData, StrData+Len); |
| 1035 | uint64_t RealLen = CAT->getSize().getZExtValue(); |
| 1036 | Str.resize(RealLen, '\0'); |
| 1037 | |
| 1038 | return Str; |
| 1039 | } |
| 1040 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1041 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 1042 | /// constant array for the given string literal. |
| 1043 | llvm::Constant * |
| 1044 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { |
| 1045 | // FIXME: This can be more efficient. |
| 1046 | return GetAddrOfConstantString(GetStringForStringLiteral(S)); |
| 1047 | } |
| 1048 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1049 | /// GenerateWritableString -- Creates storage for a string literal. |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1050 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
| 1051 | bool constant, |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1052 | CodeGenModule &CGM, |
| 1053 | const char *GlobalName) { |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1054 | // Create Constant for this string literal. Don't add a '\0'. |
| 1055 | llvm::Constant *C = llvm::ConstantArray::get(str, false); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1056 | |
| 1057 | // Create a global variable for this string |
| 1058 | C = new llvm::GlobalVariable(C->getType(), constant, |
| 1059 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1060 | C, |
| 1061 | GlobalName ? GlobalName : ".str", |
| 1062 | &CGM.getModule()); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1063 | |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1064 | return C; |
| 1065 | } |
| 1066 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1067 | /// GetAddrOfConstantString - Returns a pointer to a character array |
| 1068 | /// containing the literal. This contents are exactly that of the |
| 1069 | /// given string, i.e. it will not be null terminated automatically; |
| 1070 | /// see GetAddrOfConstantCString. Note that whether the result is |
| 1071 | /// actually a pointer to an LLVM constant depends on |
| 1072 | /// Feature.WriteableStrings. |
| 1073 | /// |
| 1074 | /// The result has pointer to array type. |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1075 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, |
| 1076 | const char *GlobalName) { |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1077 | // Don't share any string literals if writable-strings is turned on. |
| 1078 | if (Features.WritableStrings) |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1079 | return GenerateStringLiteral(str, false, *this, GlobalName); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1080 | |
| 1081 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 1082 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 1083 | |
| 1084 | if (Entry.getValue()) |
| 1085 | return Entry.getValue(); |
| 1086 | |
| 1087 | // Create a global variable for this. |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1088 | llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 1089 | Entry.setValue(C); |
| 1090 | return C; |
| 1091 | } |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1092 | |
| 1093 | /// GetAddrOfConstantCString - Returns a pointer to a character |
| 1094 | /// array containing the literal and a terminating '\-' |
| 1095 | /// character. The result has pointer to array type. |
Daniel Dunbar | 5fabf9d | 2008-10-17 21:56:50 +0000 | [diff] [blame] | 1096 | llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, |
| 1097 | const char *GlobalName){ |
Chris Lattner | c9f29c6 | 2008-12-09 19:10:54 +0000 | [diff] [blame] | 1098 | return GetAddrOfConstantString(str + '\0', GlobalName); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 1099 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1100 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1101 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 1102 | /// properties for an implementation. |
| 1103 | void CodeGenModule::EmitObjCPropertyImplementations(const |
| 1104 | ObjCImplementationDecl *D) { |
| 1105 | for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), |
| 1106 | e = D->propimpl_end(); i != e; ++i) { |
| 1107 | ObjCPropertyImplDecl *PID = *i; |
| 1108 | |
| 1109 | // Dynamic is just for type-checking. |
| 1110 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1111 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1112 | |
| 1113 | // Determine which methods need to be implemented, some may have |
| 1114 | // been overridden. Note that ::isSynthesized is not the method |
| 1115 | // we want, that just indicates if the decl came from a |
| 1116 | // property. What we want to know is if the method is defined in |
| 1117 | // this implementation. |
| 1118 | if (!D->getInstanceMethod(PD->getGetterName())) |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1119 | CodeGenFunction(*this).GenerateObjCGetter( |
| 1120 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1121 | if (!PD->isReadOnly() && |
| 1122 | !D->getInstanceMethod(PD->getSetterName())) |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 1123 | CodeGenFunction(*this).GenerateObjCSetter( |
| 1124 | const_cast<ObjCImplementationDecl *>(D), PID); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1129 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 1130 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
| 1131 | // If an error has occurred, stop code generation, but continue |
| 1132 | // parsing and semantic analysis (to ensure all warnings and errors |
| 1133 | // are emitted). |
| 1134 | if (Diags.hasErrorOccurred()) |
| 1135 | return; |
| 1136 | |
| 1137 | switch (D->getKind()) { |
| 1138 | case Decl::Function: |
| 1139 | case Decl::Var: |
| 1140 | EmitGlobal(cast<ValueDecl>(D)); |
| 1141 | break; |
| 1142 | |
| 1143 | case Decl::Namespace: |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 1144 | ErrorUnsupported(D, "namespace"); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1145 | break; |
| 1146 | |
| 1147 | // Objective-C Decls |
| 1148 | |
| 1149 | // Forward declarations, no (immediate) code generation. |
| 1150 | case Decl::ObjCClass: |
| 1151 | case Decl::ObjCCategory: |
| 1152 | case Decl::ObjCForwardProtocol: |
| 1153 | case Decl::ObjCInterface: |
| 1154 | break; |
| 1155 | |
| 1156 | case Decl::ObjCProtocol: |
| 1157 | Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); |
| 1158 | break; |
| 1159 | |
| 1160 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1161 | // Categories have properties but don't support synthesize so we |
| 1162 | // can ignore them here. |
| 1163 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1164 | Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
| 1165 | break; |
| 1166 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1167 | case Decl::ObjCImplementation: { |
| 1168 | ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); |
| 1169 | EmitObjCPropertyImplementations(OMD); |
| 1170 | Runtime->GenerateClass(OMD); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1171 | break; |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1172 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1173 | case Decl::ObjCMethod: { |
| 1174 | ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); |
| 1175 | // If this is not a prototype, emit the body. |
| 1176 | if (OMD->getBody()) |
| 1177 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 1178 | break; |
| 1179 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1180 | case Decl::ObjCCompatibleAlias: |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1181 | // compatibility-alias is a directive and has no code gen. |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1182 | break; |
| 1183 | |
| 1184 | case Decl::LinkageSpec: { |
| 1185 | LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D); |
| 1186 | if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 1187 | ErrorUnsupported(LSD, "linkage spec"); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 1188 | // FIXME: implement C++ linkage, C linkage works mostly by C |
| 1189 | // language reuse already. |
| 1190 | break; |
| 1191 | } |
| 1192 | |
| 1193 | case Decl::FileScopeAsm: { |
| 1194 | FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); |
| 1195 | std::string AsmString(AD->getAsmString()->getStrData(), |
| 1196 | AD->getAsmString()->getByteLength()); |
| 1197 | |
| 1198 | const std::string &S = getModule().getModuleInlineAsm(); |
| 1199 | if (S.empty()) |
| 1200 | getModule().setModuleInlineAsm(AsmString); |
| 1201 | else |
| 1202 | getModule().setModuleInlineAsm(S + '\n' + AsmString); |
| 1203 | break; |
| 1204 | } |
| 1205 | |
| 1206 | default: |
| 1207 | // Make sure we handled everything we should, every other kind is |
| 1208 | // a non-top-level decl. FIXME: Would be nice to have an |
| 1209 | // isTopLevelDeclKind function. Need to recode Decl::Kind to do |
| 1210 | // that easily. |
| 1211 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
| 1212 | } |
| 1213 | } |