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 | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 17 | #include "CGObjCRuntime.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Nate Begeman | ec9426c | 2008-03-09 03:09:36 +0000 | [diff] [blame] | 23 | #include "llvm/CallingConv.h" |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | #include "llvm/Intrinsics.h" |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Chris Lattner | a1945fa | 2008-04-30 16:05:42 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/Verifier.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
| 31 | |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 32 | CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 33 | llvm::Module &M, const llvm::TargetData &TD, |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 34 | Diagnostic &diags, bool GenerateDebugInfo) |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 35 | : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags), |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 36 | Types(C, M, TD), Runtime(0), MemCpyFn(0), MemMoveFn(0), MemSetFn(0), |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 37 | CFConstantStringClassRef(0) { |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 38 | |
| 39 | if (Features.ObjC1) { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 40 | if (Features.NeXTRuntime) { |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 41 | Runtime = CreateMacObjCRuntime(*this); |
| 42 | } else { |
| 43 | Runtime = CreateGNUObjCRuntime(*this); |
| 44 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 45 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 46 | |
| 47 | // If debug info generation is enabled, create the CGDebugInfo object. |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 48 | DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0; |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | CodeGenModule::~CodeGenModule() { |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 52 | delete Runtime; |
| 53 | delete DebugInfo; |
| 54 | } |
| 55 | |
| 56 | void CodeGenModule::Release() { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 57 | EmitStatics(); |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 58 | if (Runtime) |
| 59 | if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) |
| 60 | AddGlobalCtor(ObjCInitFunction); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 61 | EmitCtorList(GlobalCtors, "llvm.global_ctors"); |
| 62 | EmitCtorList(GlobalDtors, "llvm.global_dtors"); |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 63 | EmitAnnotations(); |
Chris Lattner | a1945fa | 2008-04-30 16:05:42 +0000 | [diff] [blame] | 64 | // Run the verifier to check that the generated code is consistent. |
| 65 | assert(!verifyModule(TheModule)); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 66 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 68 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 69 | /// specified stmt yet. |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 70 | void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) { |
| 71 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 72 | "cannot codegen this %0 yet"); |
| 73 | SourceRange Range = S->getSourceRange(); |
| 74 | std::string Msg = Type; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 75 | getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID, |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 76 | &Msg, 1, &Range, 1); |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 77 | } |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 78 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 79 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 80 | /// specified decl yet. |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 81 | void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) { |
| 82 | unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 83 | "cannot codegen this %0 yet"); |
| 84 | std::string Msg = Type; |
| 85 | getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID, |
| 86 | &Msg, 1); |
| 87 | } |
| 88 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 89 | /// setGlobalVisibility - Set the visibility for the given LLVM |
| 90 | /// GlobalValue according to the given clang AST visibility value. |
| 91 | static void setGlobalVisibility(llvm::GlobalValue *GV, |
| 92 | VisibilityAttr::VisibilityTypes Vis) { |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 93 | switch (Vis) { |
| 94 | default: assert(0 && "Unknown visibility!"); |
| 95 | case VisibilityAttr::DefaultVisibility: |
| 96 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 97 | break; |
| 98 | case VisibilityAttr::HiddenVisibility: |
| 99 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 100 | break; |
| 101 | case VisibilityAttr::ProtectedVisibility: |
| 102 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 107 | /// AddGlobalCtor - Add a function to the list that will be called before |
| 108 | /// main() runs. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 109 | void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 110 | // TODO: Type coercion of void()* types. |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 111 | GlobalCtors.push_back(std::make_pair(Ctor, Priority)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 114 | /// AddGlobalDtor - Add a function to the list that will be called |
| 115 | /// when the module is unloaded. |
| 116 | void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { |
| 117 | // TODO: Type coercion of void()* types. |
| 118 | GlobalDtors.push_back(std::make_pair(Dtor, Priority)); |
| 119 | } |
| 120 | |
| 121 | void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { |
| 122 | // Ctor function type is void()*. |
| 123 | llvm::FunctionType* CtorFTy = |
| 124 | llvm::FunctionType::get(llvm::Type::VoidTy, |
| 125 | std::vector<const llvm::Type*>(), |
| 126 | false); |
| 127 | llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); |
| 128 | |
| 129 | // Get the type of a ctor entry, { i32, void ()* }. |
Chris Lattner | 572cf09 | 2008-03-19 05:24:56 +0000 | [diff] [blame] | 130 | llvm::StructType* CtorStructTy = |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 131 | llvm::StructType::get(llvm::Type::Int32Ty, |
| 132 | llvm::PointerType::getUnqual(CtorFTy), NULL); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 133 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 134 | // Construct the constructor and destructor arrays. |
| 135 | std::vector<llvm::Constant*> Ctors; |
| 136 | for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
| 137 | std::vector<llvm::Constant*> S; |
| 138 | S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); |
| 139 | S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); |
| 140 | Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 141 | } |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 142 | |
| 143 | if (!Ctors.empty()) { |
| 144 | llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); |
| 145 | new llvm::GlobalVariable(AT, false, |
| 146 | llvm::GlobalValue::AppendingLinkage, |
| 147 | llvm::ConstantArray::get(AT, Ctors), |
| 148 | GlobalName, |
| 149 | &TheModule); |
| 150 | } |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 153 | void CodeGenModule::EmitAnnotations() { |
| 154 | if (Annotations.empty()) |
| 155 | return; |
| 156 | |
| 157 | // Create a new global variable for the ConstantStruct in the Module. |
| 158 | llvm::Constant *Array = |
| 159 | llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), |
| 160 | Annotations.size()), |
| 161 | Annotations); |
| 162 | llvm::GlobalValue *gv = |
| 163 | new llvm::GlobalVariable(Array->getType(), false, |
| 164 | llvm::GlobalValue::AppendingLinkage, Array, |
| 165 | "llvm.global.annotations", &TheModule); |
| 166 | gv->setSection("llvm.metadata"); |
| 167 | } |
| 168 | |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 169 | void CodeGenModule::SetGlobalValueAttributes(const FunctionDecl *FD, |
| 170 | llvm::GlobalValue *GV) { |
| 171 | // TODO: Set up linkage and many other things. Note, this is a simple |
| 172 | // approximation of what we really want. |
| 173 | if (FD->getStorageClass() == FunctionDecl::Static) |
| 174 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 175 | else if (FD->getAttr<DLLImportAttr>()) |
| 176 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 177 | else if (FD->getAttr<DLLExportAttr>()) |
| 178 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
| 179 | else if (FD->getAttr<WeakAttr>() || FD->isInline()) |
| 180 | GV->setLinkage(llvm::Function::WeakLinkage); |
| 181 | |
| 182 | if (const VisibilityAttr *attr = FD->getAttr<VisibilityAttr>()) |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 183 | setGlobalVisibility(GV, attr->getVisibility()); |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 184 | // FIXME: else handle -fvisibility |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 185 | |
| 186 | if (const AsmLabelAttr *ALA = FD->getAttr<AsmLabelAttr>()) { |
| 187 | // Prefaced with special LLVM marker to indicate that the name |
| 188 | // should not be munged. |
| 189 | GV->setName("\01" + ALA->getLabel()); |
| 190 | } |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 193 | void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, |
| 194 | llvm::Function *F, |
| 195 | const llvm::FunctionType *FTy) { |
| 196 | unsigned FuncAttrs = 0; |
| 197 | if (FD->getAttr<NoThrowAttr>()) |
| 198 | FuncAttrs |= llvm::ParamAttr::NoUnwind; |
| 199 | if (FD->getAttr<NoReturnAttr>()) |
| 200 | FuncAttrs |= llvm::ParamAttr::NoReturn; |
| 201 | |
| 202 | llvm::SmallVector<llvm::ParamAttrsWithIndex, 8> ParamAttrList; |
| 203 | if (FuncAttrs) |
| 204 | ParamAttrList.push_back(llvm::ParamAttrsWithIndex::get(0, FuncAttrs)); |
| 205 | // Note that there is parallel code in CodeGenFunction::EmitCallExpr |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 206 | bool AggregateReturn = CodeGenFunction::hasAggregateLLVMType(FD->getResultType()); |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 207 | if (AggregateReturn) |
| 208 | ParamAttrList.push_back( |
| 209 | llvm::ParamAttrsWithIndex::get(1, llvm::ParamAttr::StructRet)); |
| 210 | unsigned increment = AggregateReturn ? 2 : 1; |
Eli Friedman | c134fcb | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 211 | const FunctionTypeProto* FTP = dyn_cast<FunctionTypeProto>(FD->getType()); |
| 212 | if (FTP) { |
| 213 | for (unsigned i = 0; i < FTP->getNumArgs(); i++) { |
| 214 | QualType ParamType = FTP->getArgType(i); |
| 215 | unsigned ParamAttrs = 0; |
| 216 | if (ParamType->isRecordType()) |
| 217 | ParamAttrs |= llvm::ParamAttr::ByVal; |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 218 | if (ParamType->isSignedIntegerType() && |
| 219 | ParamType->isPromotableIntegerType()) |
Eli Friedman | c134fcb | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 220 | ParamAttrs |= llvm::ParamAttr::SExt; |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 221 | if (ParamType->isUnsignedIntegerType() && |
| 222 | ParamType->isPromotableIntegerType()) |
Eli Friedman | c134fcb | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 223 | ParamAttrs |= llvm::ParamAttr::ZExt; |
| 224 | if (ParamAttrs) |
| 225 | ParamAttrList.push_back(llvm::ParamAttrsWithIndex::get(i + increment, |
| 226 | ParamAttrs)); |
| 227 | } |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 228 | } |
Eli Friedman | c134fcb | 2008-06-04 19:41:28 +0000 | [diff] [blame] | 229 | |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 230 | F->setParamAttrs(llvm::PAListPtr::get(ParamAttrList.begin(), |
| 231 | ParamAttrList.size())); |
| 232 | |
| 233 | // Set the appropriate calling convention for the Function. |
| 234 | if (FD->getAttr<FastCallAttr>()) |
| 235 | F->setCallingConv(llvm::CallingConv::Fast); |
| 236 | |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 237 | SetGlobalValueAttributes(FD, F); |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 240 | void CodeGenModule::EmitStatics() { |
| 241 | // Emit code for each used static decl encountered. Since a previously unused |
| 242 | // static decl may become used during the generation of code for a static |
| 243 | // function, iterate until no changes are made. |
| 244 | bool Changed; |
| 245 | do { |
| 246 | Changed = false; |
| 247 | for (unsigned i = 0, e = StaticDecls.size(); i != e; ++i) { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 248 | const ValueDecl *D = StaticDecls[i]; |
Eli Friedman | 6f7e2ee | 2008-05-27 04:58:01 +0000 | [diff] [blame] | 249 | |
| 250 | // Check if we have used a decl with the same name |
| 251 | // FIXME: The AST should have some sort of aggregate decls or |
| 252 | // global symbol map. |
Daniel Dunbar | 90db882 | 2008-08-25 06:18:57 +0000 | [diff] [blame] | 253 | if (!GlobalDeclMap.count(D->getIdentifier())) |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 254 | continue; |
Eli Friedman | 6f7e2ee | 2008-05-27 04:58:01 +0000 | [diff] [blame] | 255 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 256 | // Emit the definition. |
| 257 | EmitGlobalDefinition(D); |
| 258 | |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 259 | // Erase the used decl from the list. |
| 260 | StaticDecls[i] = StaticDecls.back(); |
| 261 | StaticDecls.pop_back(); |
| 262 | --i; |
| 263 | --e; |
| 264 | |
| 265 | // Remember that we made a change. |
| 266 | Changed = true; |
| 267 | } |
| 268 | } while (Changed); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 271 | /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the |
| 272 | /// annotation information for a given GlobalValue. The annotation struct is |
| 273 | /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 274 | /// GlobalValue being annotated. The second field is the constant string |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 275 | /// created from the AnnotateAttr's annotation. The third field is a constant |
| 276 | /// string containing the name of the translation unit. The fourth field is |
| 277 | /// the line number in the file of the annotated value declaration. |
| 278 | /// |
| 279 | /// FIXME: this does not unique the annotation string constants, as llvm-gcc |
| 280 | /// appears to. |
| 281 | /// |
| 282 | llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, |
| 283 | const AnnotateAttr *AA, |
| 284 | unsigned LineNo) { |
| 285 | llvm::Module *M = &getModule(); |
| 286 | |
| 287 | // get [N x i8] constants for the annotation string, and the filename string |
| 288 | // which are the 2nd and 3rd elements of the global annotation structure. |
| 289 | const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 290 | llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); |
| 291 | llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), |
| 292 | true); |
| 293 | |
| 294 | // Get the two global values corresponding to the ConstantArrays we just |
| 295 | // created to hold the bytes of the strings. |
| 296 | llvm::GlobalValue *annoGV = |
| 297 | new llvm::GlobalVariable(anno->getType(), false, |
| 298 | llvm::GlobalValue::InternalLinkage, anno, |
| 299 | GV->getName() + ".str", M); |
| 300 | // translation unit name string, emitted into the llvm.metadata section. |
| 301 | llvm::GlobalValue *unitGV = |
| 302 | new llvm::GlobalVariable(unit->getType(), false, |
| 303 | llvm::GlobalValue::InternalLinkage, unit, ".str", M); |
| 304 | |
| 305 | // Create the ConstantStruct that is the global annotion. |
| 306 | llvm::Constant *Fields[4] = { |
| 307 | llvm::ConstantExpr::getBitCast(GV, SBP), |
| 308 | llvm::ConstantExpr::getBitCast(annoGV, SBP), |
| 309 | llvm::ConstantExpr::getBitCast(unitGV, SBP), |
| 310 | llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) |
| 311 | }; |
| 312 | return llvm::ConstantStruct::get(Fields, 4, false); |
| 313 | } |
| 314 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 315 | void CodeGenModule::EmitGlobal(const ValueDecl *Global) { |
| 316 | bool isDef, isStatic; |
| 317 | |
| 318 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { |
| 319 | isDef = (FD->isThisDeclarationADefinition() || |
| 320 | FD->getAttr<AliasAttr>()); |
| 321 | isStatic = FD->getStorageClass() == FunctionDecl::Static; |
| 322 | } else if (const VarDecl *VD = cast<VarDecl>(Global)) { |
| 323 | assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); |
| 324 | |
| 325 | isDef = !(VD->getStorageClass() == VarDecl::Extern && VD->getInit() == 0); |
| 326 | isStatic = VD->getStorageClass() == VarDecl::Static; |
| 327 | } else { |
| 328 | assert(0 && "Invalid argument to EmitGlobal"); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 332 | // Forward declarations are emitted lazily on first use. |
| 333 | if (!isDef) |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 334 | return; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 335 | |
| 336 | // If the global is a static, defer code generation until later so |
| 337 | // we can easily omit unused statics. |
| 338 | if (isStatic) { |
| 339 | StaticDecls.push_back(Global); |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | // Otherwise emit the definition. |
| 344 | EmitGlobalDefinition(Global); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 347 | void CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { |
| 348 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 349 | EmitGlobalFunctionDefinition(FD); |
| 350 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 351 | EmitGlobalVarDefinition(VD); |
| 352 | } else { |
| 353 | assert(0 && "Invalid argument to EmitGlobalDefinition()"); |
| 354 | } |
| 355 | } |
| 356 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 357 | llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) { |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 358 | assert(D->hasGlobalStorage() && "Not a global variable"); |
| 359 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 360 | QualType ASTTy = D->getType(); |
| 361 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 362 | const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 363 | |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 364 | // Lookup the entry, lazily creating it if necessary. |
Daniel Dunbar | 90db882 | 2008-08-25 06:18:57 +0000 | [diff] [blame] | 365 | llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 366 | if (!Entry) |
| 367 | Entry = new llvm::GlobalVariable(Ty, false, |
| 368 | llvm::GlobalValue::ExternalLinkage, |
| 369 | 0, D->getName(), &getModule(), 0, |
| 370 | ASTTy.getAddressSpace()); |
| 371 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 372 | // Make sure the result is of the correct type. |
| 373 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 377 | llvm::Constant *Init = 0; |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 378 | QualType ASTTy = D->getType(); |
| 379 | const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 381 | if (D->getInit() == 0) { |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 382 | // This is a tentative definition; tentative definitions are |
| 383 | // implicitly initialized with { 0 } |
| 384 | const llvm::Type* InitTy; |
| 385 | if (ASTTy->isIncompleteArrayType()) { |
| 386 | // An incomplete array is normally [ TYPE x 0 ], but we need |
| 387 | // to fix it to [ TYPE x 1 ]. |
| 388 | const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy); |
| 389 | InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); |
| 390 | } else { |
| 391 | InitTy = VarTy; |
| 392 | } |
| 393 | Init = llvm::Constant::getNullValue(InitTy); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 394 | } else { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 395 | Init = EmitConstantExpr(D->getInit()); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 396 | } |
| 397 | const llvm::Type* InitType = Init->getType(); |
| 398 | |
Daniel Dunbar | 90db882 | 2008-08-25 06:18:57 +0000 | [diff] [blame] | 399 | llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 400 | llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry); |
| 401 | |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 402 | if (!GV) { |
| 403 | GV = new llvm::GlobalVariable(InitType, false, |
| 404 | llvm::GlobalValue::ExternalLinkage, |
| 405 | 0, D->getName(), &getModule(), 0, |
| 406 | ASTTy.getAddressSpace()); |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 407 | } else if (GV->getType() != |
| 408 | llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) { |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 409 | // We have a definition after a prototype with the wrong type. |
| 410 | // We must make a new GlobalVariable* and update everything that used OldGV |
| 411 | // (a declaration or tentative definition) with the new GlobalVariable* |
| 412 | // (which will be a definition). |
| 413 | // |
| 414 | // This happens if there is a prototype for a global (e.g. "extern int x[];") |
| 415 | // and then a definition of a different type (e.g. "int x[10];"). This also |
| 416 | // happens when an initializer has a different type from the type of the |
| 417 | // global (this happens with unions). |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 418 | // |
| 419 | // FIXME: This also ends up happening if there's a definition followed by |
| 420 | // a tentative definition! (Although Sema rejects that construct |
| 421 | // at the moment.) |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 422 | |
| 423 | // Save the old global |
| 424 | llvm::GlobalVariable *OldGV = GV; |
| 425 | |
| 426 | // Make a new global with the correct type |
| 427 | GV = new llvm::GlobalVariable(InitType, false, |
| 428 | llvm::GlobalValue::ExternalLinkage, |
| 429 | 0, D->getName(), &getModule(), 0, |
| 430 | ASTTy.getAddressSpace()); |
| 431 | // Steal the name of the old global |
| 432 | GV->takeName(OldGV); |
| 433 | |
| 434 | // Replace all uses of the old global with the new global |
| 435 | llvm::Constant *NewPtrForOldDecl = |
| 436 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 437 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
Eli Friedman | 77ba708 | 2008-05-30 19:50:47 +0000 | [diff] [blame] | 438 | |
| 439 | // Erase the old global, since it is no longer used. |
| 440 | OldGV->eraseFromParent(); |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 441 | } |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 442 | |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 443 | Entry = GV; |
Devang Patel | 8e53e72 | 2007-10-26 16:31:40 +0000 | [diff] [blame] | 444 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 445 | if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { |
| 446 | SourceManager &SM = Context.getSourceManager(); |
| 447 | AddAnnotation(EmitAnnotateAttr(GV, AA, |
| 448 | SM.getLogicalLineNumber(D->getLocation()))); |
| 449 | } |
| 450 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 451 | GV->setInitializer(Init); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 452 | |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 453 | // FIXME: This is silly; getTypeAlign should just work for incomplete arrays |
| 454 | unsigned Align; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 455 | if (const IncompleteArrayType* IAT = |
| 456 | Context.getAsIncompleteArrayType(D->getType())) |
Eli Friedman | cd5f4aa | 2008-05-30 20:39:54 +0000 | [diff] [blame] | 457 | Align = Context.getTypeAlign(IAT->getElementType()); |
| 458 | else |
| 459 | Align = Context.getTypeAlign(D->getType()); |
Eli Friedman | 08d7802 | 2008-05-29 11:10:27 +0000 | [diff] [blame] | 460 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) { |
| 461 | Align = std::max(Align, AA->getAlignment()); |
| 462 | } |
| 463 | GV->setAlignment(Align / 8); |
| 464 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 465 | if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 466 | setGlobalVisibility(GV, attr->getVisibility()); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 467 | // FIXME: else handle -fvisibility |
Daniel Dunbar | a735ad8 | 2008-08-06 00:03:29 +0000 | [diff] [blame] | 468 | |
| 469 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
| 470 | // Prefaced with special LLVM marker to indicate that the name |
| 471 | // should not be munged. |
| 472 | GV->setName("\01" + ALA->getLabel()); |
| 473 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 474 | |
| 475 | // Set the llvm linkage type as appropriate. |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 476 | if (D->getStorageClass() == VarDecl::Static) |
| 477 | GV->setLinkage(llvm::Function::InternalLinkage); |
| 478 | else if (D->getAttr<DLLImportAttr>()) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 479 | GV->setLinkage(llvm::Function::DLLImportLinkage); |
| 480 | else if (D->getAttr<DLLExportAttr>()) |
| 481 | GV->setLinkage(llvm::Function::DLLExportLinkage); |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 482 | else if (D->getAttr<WeakAttr>()) |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 483 | GV->setLinkage(llvm::GlobalVariable::WeakLinkage); |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 484 | else { |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 485 | // FIXME: This isn't right. This should handle common linkage and other |
| 486 | // stuff. |
| 487 | switch (D->getStorageClass()) { |
Chris Lattner | 8fabd78 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 488 | case VarDecl::Static: assert(0 && "This case handled above"); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 489 | case VarDecl::Auto: |
| 490 | case VarDecl::Register: |
| 491 | assert(0 && "Can't have auto or register globals"); |
| 492 | case VarDecl::None: |
| 493 | if (!D->getInit()) |
Eli Friedman | a07b764 | 2008-05-29 11:03:17 +0000 | [diff] [blame] | 494 | GV->setLinkage(llvm::GlobalVariable::CommonLinkage); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 495 | break; |
| 496 | case VarDecl::Extern: |
| 497 | case VarDecl::PrivateExtern: |
| 498 | // todo: common |
| 499 | break; |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 500 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 501 | } |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 502 | |
| 503 | // Emit global variable debug information. |
| 504 | CGDebugInfo *DI = getDebugInfo(); |
| 505 | if(DI) { |
| 506 | if(D->getLocation().isValid()) |
| 507 | DI->setLocation(D->getLocation()); |
| 508 | DI->EmitGlobalVariable(GV, D); |
| 509 | } |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 510 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 511 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 512 | llvm::GlobalValue * |
| 513 | CodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D) { |
| 514 | // FIXME: param attributes for sext/zext etc. |
| 515 | if (const AliasAttr *AA = D->getAttr<AliasAttr>()) { |
| 516 | assert(!D->getBody() && "Unexpected alias attr on function with body."); |
| 517 | |
| 518 | const std::string& aliaseeName = AA->getAliasee(); |
| 519 | llvm::Function *aliasee = getModule().getFunction(aliaseeName); |
| 520 | llvm::GlobalValue *alias = new llvm::GlobalAlias(aliasee->getType(), |
| 521 | llvm::Function::ExternalLinkage, |
| 522 | D->getName(), |
| 523 | aliasee, |
| 524 | &getModule()); |
| 525 | SetGlobalValueAttributes(D, alias); |
| 526 | return alias; |
| 527 | } else { |
| 528 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 529 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 530 | llvm::Function *F = llvm::Function::Create(FTy, |
| 531 | llvm::Function::ExternalLinkage, |
| 532 | D->getName(), &getModule()); |
| 533 | |
| 534 | SetFunctionAttributes(D, F, FTy); |
| 535 | return F; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | llvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) { |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 540 | QualType ASTTy = D->getType(); |
| 541 | const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); |
| 542 | const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 543 | |
| 544 | // Lookup the entry, lazily creating it if necessary. |
Daniel Dunbar | 90db882 | 2008-08-25 06:18:57 +0000 | [diff] [blame] | 545 | llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 546 | if (!Entry) |
| 547 | Entry = EmitForwardFunctionDefinition(D); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 548 | |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 549 | return llvm::ConstantExpr::getBitCast(Entry, PTy); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | void CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { |
Daniel Dunbar | 90db882 | 2008-08-25 06:18:57 +0000 | [diff] [blame] | 553 | llvm::GlobalValue *&Entry = GlobalDeclMap[D->getIdentifier()]; |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 554 | if (!Entry) { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 555 | Entry = EmitForwardFunctionDefinition(D); |
| 556 | } else { |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 557 | // If the types mismatch then we have to rewrite the definition. |
| 558 | const llvm::Type *Ty = getTypes().ConvertType(D->getType()); |
| 559 | if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) { |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 560 | // Otherwise, we have a definition after a prototype with the wrong type. |
| 561 | // F is the Function* for the one with the wrong type, we must make a new |
| 562 | // Function* and update everything that used F (a declaration) with the new |
| 563 | // Function* (which will be a definition). |
| 564 | // |
| 565 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 566 | // then a definition of a different type (e.g. "int f(int x)"). Start by |
| 567 | // 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] | 568 | llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D); |
| 569 | NewFn->takeName(Entry); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 570 | |
| 571 | // Replace uses of F with the Function we will endow with a body. |
| 572 | llvm::Constant *NewPtrForOldDecl = |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 573 | llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); |
| 574 | Entry->replaceAllUsesWith(NewPtrForOldDecl); |
| 575 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 576 | // Ok, delete the old function now, which is dead. |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 577 | // FIXME: Add GlobalValue->eraseFromParent(). |
| 578 | assert(Entry->isDeclaration() && "Shouldn't replace non-declaration"); |
| 579 | if (llvm::Function *F = dyn_cast<llvm::Function>(Entry)) { |
| 580 | F->eraseFromParent(); |
| 581 | } else if (llvm::GlobalAlias *GA = dyn_cast<llvm::GlobalAlias>(Entry)) { |
| 582 | GA->eraseFromParent(); |
| 583 | } else { |
| 584 | assert(0 && "Invalid global variable type."); |
| 585 | } |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 586 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 587 | Entry = NewFn; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | if (D->getAttr<AliasAttr>()) { |
| 592 | ; |
| 593 | } else { |
| 594 | llvm::Function *Fn = cast<llvm::Function>(Entry); |
| 595 | CodeGenFunction(*this).GenerateCode(D, Fn); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 596 | |
Daniel Dunbar | 6379a7a | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 597 | // Set attributes specific to definition. |
| 598 | // FIXME: This needs to be cleaned up by clearly emitting the |
| 599 | // declaration / definition at separate times. |
| 600 | if (!Features.Exceptions) |
| 601 | Fn->addParamAttr(0, llvm::ParamAttr::NoUnwind); |
| 602 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 603 | if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) { |
| 604 | AddGlobalCtor(Fn, CA->getPriority()); |
| 605 | } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) { |
| 606 | AddGlobalDtor(Fn, DA->getPriority()); |
| 607 | } |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Chris Lattner | c5b8806 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 611 | void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { |
| 612 | // Make sure that this type is translated. |
| 613 | Types.UpdateCompletedType(TD); |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 617 | /// getBuiltinLibFunction |
| 618 | llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { |
Chris Lattner | 1426fec | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 619 | if (BuiltinID > BuiltinFunctions.size()) |
| 620 | BuiltinFunctions.resize(BuiltinID); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 621 | |
Chris Lattner | 1426fec | 2007-12-13 00:38:03 +0000 | [diff] [blame] | 622 | // Cache looked up functions. Since builtin id #0 is invalid we don't reserve |
| 623 | // a slot for it. |
| 624 | assert(BuiltinID && "Invalid Builtin ID"); |
| 625 | llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 626 | if (FunctionSlot) |
| 627 | return FunctionSlot; |
| 628 | |
| 629 | assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); |
| 630 | |
| 631 | // Get the name, skip over the __builtin_ prefix. |
| 632 | const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; |
| 633 | |
| 634 | // Get the type for the builtin. |
| 635 | QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); |
| 636 | const llvm::FunctionType *Ty = |
| 637 | cast<llvm::FunctionType>(getTypes().ConvertType(Type)); |
| 638 | |
| 639 | // FIXME: This has a serious problem with code like this: |
| 640 | // void abs() {} |
| 641 | // ... __builtin_abs(x); |
| 642 | // The two versions of abs will collide. The fix is for the builtin to win, |
| 643 | // and for the existing one to be turned into a constantexpr cast of the |
| 644 | // builtin. In the case where the existing one is a static function, it |
| 645 | // should just be renamed. |
Chris Lattner | c5e940f | 2007-08-31 04:44:06 +0000 | [diff] [blame] | 646 | if (llvm::Function *Existing = getModule().getFunction(Name)) { |
| 647 | if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) |
| 648 | return FunctionSlot = Existing; |
| 649 | assert(Existing == 0 && "FIXME: Name collision"); |
| 650 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 651 | |
| 652 | // FIXME: param attributes for sext/zext etc. |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 653 | return FunctionSlot = |
| 654 | llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, |
| 655 | &getModule()); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 658 | llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, |
| 659 | unsigned NumTys) { |
| 660 | return llvm::Intrinsic::getDeclaration(&getModule(), |
| 661 | (llvm::Intrinsic::ID)IID, Tys, NumTys); |
| 662 | } |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 663 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 664 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 665 | if (MemCpyFn) return MemCpyFn; |
| 666 | llvm::Intrinsic::ID IID; |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 667 | switch (Context.Target.getPointerWidth(0)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 668 | default: assert(0 && "Unknown ptr width"); |
| 669 | case 32: IID = llvm::Intrinsic::memcpy_i32; break; |
| 670 | case 64: IID = llvm::Intrinsic::memcpy_i64; break; |
| 671 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 672 | return MemCpyFn = getIntrinsic(IID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 673 | } |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 674 | |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 675 | llvm::Function *CodeGenModule::getMemMoveFn() { |
| 676 | if (MemMoveFn) return MemMoveFn; |
| 677 | llvm::Intrinsic::ID IID; |
| 678 | switch (Context.Target.getPointerWidth(0)) { |
| 679 | default: assert(0 && "Unknown ptr width"); |
| 680 | case 32: IID = llvm::Intrinsic::memmove_i32; break; |
| 681 | case 64: IID = llvm::Intrinsic::memmove_i64; break; |
| 682 | } |
| 683 | return MemMoveFn = getIntrinsic(IID); |
| 684 | } |
| 685 | |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 686 | llvm::Function *CodeGenModule::getMemSetFn() { |
| 687 | if (MemSetFn) return MemSetFn; |
| 688 | llvm::Intrinsic::ID IID; |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 689 | switch (Context.Target.getPointerWidth(0)) { |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 690 | default: assert(0 && "Unknown ptr width"); |
| 691 | case 32: IID = llvm::Intrinsic::memset_i32; break; |
| 692 | case 64: IID = llvm::Intrinsic::memset_i64; break; |
| 693 | } |
| 694 | return MemSetFn = getIntrinsic(IID); |
| 695 | } |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 696 | |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 697 | // We still need to work out the details of handling UTF-16. |
| 698 | // See: <rdr://2996215> |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 699 | llvm::Constant *CodeGenModule:: |
| 700 | GetAddrOfConstantCFString(const std::string &str) { |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 701 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 702 | CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 703 | |
| 704 | if (Entry.getValue()) |
| 705 | return Entry.getValue(); |
| 706 | |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 707 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 708 | llvm::Constant *Zeros[] = { Zero, Zero }; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 709 | |
| 710 | if (!CFConstantStringClassRef) { |
| 711 | const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); |
| 712 | Ty = llvm::ArrayType::get(Ty, 0); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 713 | |
| 714 | // FIXME: This is fairly broken if |
| 715 | // __CFConstantStringClassReference is already defined, in that it |
| 716 | // will get renamed and the user will most likely see an opaque |
| 717 | // error message. This is a general issue with relying on |
| 718 | // particular names. |
| 719 | llvm::GlobalVariable *GV = |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 720 | new llvm::GlobalVariable(Ty, false, |
| 721 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 722 | "__CFConstantStringClassReference", |
| 723 | &getModule()); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 724 | |
| 725 | // Decay array -> ptr |
| 726 | CFConstantStringClassRef = |
| 727 | llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 730 | std::vector<llvm::Constant*> Fields(4); |
| 731 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 732 | // Class pointer. |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 733 | Fields[0] = CFConstantStringClassRef; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 734 | |
| 735 | // Flags. |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 736 | const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); |
| 737 | Fields[1] = llvm::ConstantInt::get(Ty, 0x07C8); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 738 | |
| 739 | // String pointer. |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 740 | llvm::Constant *C = llvm::ConstantArray::get(str); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 741 | C = new llvm::GlobalVariable(C->getType(), true, |
| 742 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 743 | C, ".str", &getModule()); |
| 744 | Fields[2] = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 745 | |
| 746 | // String length. |
| 747 | Ty = getTypes().ConvertType(getContext().LongTy); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 748 | Fields[3] = llvm::ConstantInt::get(Ty, str.length()); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 749 | |
| 750 | // The struct. |
| 751 | Ty = getTypes().ConvertType(getContext().getCFConstantStringType()); |
| 752 | C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields); |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 753 | llvm::GlobalVariable *GV = |
| 754 | new llvm::GlobalVariable(C->getType(), true, |
| 755 | llvm::GlobalVariable::InternalLinkage, |
| 756 | C, "", &getModule()); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 757 | |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 758 | GV->setSection("__DATA,__cfstring"); |
| 759 | Entry.setValue(GV); |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 760 | |
Anders Carlsson | 0c67829 | 2007-11-01 00:41:52 +0000 | [diff] [blame] | 761 | return GV; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 762 | } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 763 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 764 | /// GetStringForStringLiteral - Return the appropriate bytes for a |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 765 | /// string literal, properly padded to match the literal type. |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 766 | std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 767 | assert(!E->isWide() && "FIXME: Wide strings not supported yet!"); |
| 768 | const char *StrData = E->getStrData(); |
| 769 | unsigned Len = E->getByteLength(); |
| 770 | |
| 771 | const ConstantArrayType *CAT = |
| 772 | getContext().getAsConstantArrayType(E->getType()); |
| 773 | assert(CAT && "String isn't pointer or array!"); |
| 774 | |
| 775 | // Resize the string to the right size |
| 776 | // FIXME: What about wchar_t strings? |
| 777 | std::string Str(StrData, StrData+Len); |
| 778 | uint64_t RealLen = CAT->getSize().getZExtValue(); |
| 779 | Str.resize(RealLen, '\0'); |
| 780 | |
| 781 | return Str; |
| 782 | } |
| 783 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 784 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 785 | /// constant array for the given string literal. |
| 786 | llvm::Constant * |
| 787 | CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { |
| 788 | // FIXME: This can be more efficient. |
| 789 | return GetAddrOfConstantString(GetStringForStringLiteral(S)); |
| 790 | } |
| 791 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 792 | /// GenerateWritableString -- Creates storage for a string literal. |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 793 | static llvm::Constant *GenerateStringLiteral(const std::string &str, |
| 794 | bool constant, |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 795 | CodeGenModule &CGM) { |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 796 | // Create Constant for this string literal. Don't add a '\0'. |
| 797 | llvm::Constant *C = llvm::ConstantArray::get(str, false); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 798 | |
| 799 | // Create a global variable for this string |
| 800 | C = new llvm::GlobalVariable(C->getType(), constant, |
| 801 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 802 | C, ".str", &CGM.getModule()); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 803 | |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 804 | return C; |
| 805 | } |
| 806 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 807 | /// GetAddrOfConstantString - Returns a pointer to a character array |
| 808 | /// containing the literal. This contents are exactly that of the |
| 809 | /// given string, i.e. it will not be null terminated automatically; |
| 810 | /// see GetAddrOfConstantCString. Note that whether the result is |
| 811 | /// actually a pointer to an LLVM constant depends on |
| 812 | /// Feature.WriteableStrings. |
| 813 | /// |
| 814 | /// The result has pointer to array type. |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 815 | llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str) { |
| 816 | // Don't share any string literals if writable-strings is turned on. |
| 817 | if (Features.WritableStrings) |
| 818 | return GenerateStringLiteral(str, false, *this); |
| 819 | |
| 820 | llvm::StringMapEntry<llvm::Constant *> &Entry = |
| 821 | ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); |
| 822 | |
| 823 | if (Entry.getValue()) |
| 824 | return Entry.getValue(); |
| 825 | |
| 826 | // Create a global variable for this. |
| 827 | llvm::Constant *C = GenerateStringLiteral(str, true, *this); |
| 828 | Entry.setValue(C); |
| 829 | return C; |
| 830 | } |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 831 | |
| 832 | /// GetAddrOfConstantCString - Returns a pointer to a character |
| 833 | /// array containing the literal and a terminating '\-' |
| 834 | /// character. The result has pointer to array type. |
| 835 | llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str) { |
Daniel Dunbar | 9d9b09c | 2008-08-15 18:29:12 +0000 | [diff] [blame] | 836 | return GetAddrOfConstantString(str + "\0"); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 837 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 838 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 839 | /// EmitObjCPropertyImplementations - Emit information for synthesized |
| 840 | /// properties for an implementation. |
| 841 | void CodeGenModule::EmitObjCPropertyImplementations(const |
| 842 | ObjCImplementationDecl *D) { |
| 843 | for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), |
| 844 | e = D->propimpl_end(); i != e; ++i) { |
| 845 | ObjCPropertyImplDecl *PID = *i; |
| 846 | |
| 847 | // Dynamic is just for type-checking. |
| 848 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 849 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 850 | |
| 851 | // Determine which methods need to be implemented, some may have |
| 852 | // been overridden. Note that ::isSynthesized is not the method |
| 853 | // we want, that just indicates if the decl came from a |
| 854 | // property. What we want to know is if the method is defined in |
| 855 | // this implementation. |
| 856 | if (!D->getInstanceMethod(PD->getGetterName())) |
| 857 | CodeGenFunction(*this).GenerateObjCGetter(PID); |
| 858 | if (!PD->isReadOnly() && |
| 859 | !D->getInstanceMethod(PD->getSetterName())) |
| 860 | CodeGenFunction(*this).GenerateObjCSetter(PID); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 865 | /// EmitTopLevelDecl - Emit code for a single top level declaration. |
| 866 | void CodeGenModule::EmitTopLevelDecl(Decl *D) { |
| 867 | // If an error has occurred, stop code generation, but continue |
| 868 | // parsing and semantic analysis (to ensure all warnings and errors |
| 869 | // are emitted). |
| 870 | if (Diags.hasErrorOccurred()) |
| 871 | return; |
| 872 | |
| 873 | switch (D->getKind()) { |
| 874 | case Decl::Function: |
| 875 | case Decl::Var: |
| 876 | EmitGlobal(cast<ValueDecl>(D)); |
| 877 | break; |
| 878 | |
| 879 | case Decl::Namespace: |
| 880 | assert(0 && "FIXME: Namespace unsupported"); |
| 881 | break; |
| 882 | |
| 883 | // Objective-C Decls |
| 884 | |
| 885 | // Forward declarations, no (immediate) code generation. |
| 886 | case Decl::ObjCClass: |
| 887 | case Decl::ObjCCategory: |
| 888 | case Decl::ObjCForwardProtocol: |
| 889 | case Decl::ObjCInterface: |
| 890 | break; |
| 891 | |
| 892 | case Decl::ObjCProtocol: |
| 893 | Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); |
| 894 | break; |
| 895 | |
| 896 | case Decl::ObjCCategoryImpl: |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 897 | // Categories have properties but don't support synthesize so we |
| 898 | // can ignore them here. |
| 899 | |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 900 | Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); |
| 901 | break; |
| 902 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 903 | case Decl::ObjCImplementation: { |
| 904 | ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); |
| 905 | EmitObjCPropertyImplementations(OMD); |
| 906 | Runtime->GenerateClass(OMD); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 907 | break; |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 908 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 909 | case Decl::ObjCMethod: { |
| 910 | ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); |
| 911 | // If this is not a prototype, emit the body. |
| 912 | if (OMD->getBody()) |
| 913 | CodeGenFunction(*this).GenerateObjCMethod(OMD); |
| 914 | break; |
| 915 | } |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 916 | case Decl::ObjCCompatibleAlias: |
| 917 | assert(0 && "FIXME: ObjCCompatibleAlias unsupported"); |
| 918 | break; |
| 919 | |
| 920 | case Decl::LinkageSpec: { |
| 921 | LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D); |
| 922 | if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 923 | ErrorUnsupported(LSD, "linkage spec"); |
Daniel Dunbar | 41071de | 2008-08-15 23:26:23 +0000 | [diff] [blame] | 924 | // FIXME: implement C++ linkage, C linkage works mostly by C |
| 925 | // language reuse already. |
| 926 | break; |
| 927 | } |
| 928 | |
| 929 | case Decl::FileScopeAsm: { |
| 930 | FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); |
| 931 | std::string AsmString(AD->getAsmString()->getStrData(), |
| 932 | AD->getAsmString()->getByteLength()); |
| 933 | |
| 934 | const std::string &S = getModule().getModuleInlineAsm(); |
| 935 | if (S.empty()) |
| 936 | getModule().setModuleInlineAsm(AsmString); |
| 937 | else |
| 938 | getModule().setModuleInlineAsm(S + '\n' + AsmString); |
| 939 | break; |
| 940 | } |
| 941 | |
| 942 | default: |
| 943 | // Make sure we handled everything we should, every other kind is |
| 944 | // a non-top-level decl. FIXME: Would be nice to have an |
| 945 | // isTopLevelDeclKind function. Need to recode Decl::Kind to do |
| 946 | // that easily. |
| 947 | assert(isa<TypeDecl>(D) && "Unsupported decl kind"); |
| 948 | } |
| 949 | } |
| 950 | |