blob: 218365e316fe7b316a3533e64687d653a33ae47b [file] [log] [blame]
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
Chris Lattnera0fd5ee2008-03-01 08:50:34 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000010// This provides Objective-C code generation targetting the GNU runtime. The
11// class in this file generates structures used by the GNU Objective-C runtime
12// library. These structures are defined in objc/objc.h and objc/objc-api.h in
13// the GNU runtime distribution.
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "CGObjCRuntime.h"
Chris Lattner547907c2008-06-26 04:19:03 +000018#include "CodeGenModule.h"
Daniel Dunbara04840b2008-08-23 03:46:30 +000019#include "CodeGenFunction.h"
Chris Lattner547907c2008-06-26 04:19:03 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000022#include "clang/AST/DeclObjC.h"
Chris Lattner4a9e9272009-04-26 01:32:48 +000023#include "clang/AST/StmtObjC.h"
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000024#include "llvm/Module.h"
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000025#include "llvm/ADT/SmallVector.h"
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000026#include "llvm/ADT/StringMap.h"
Daniel Dunbarac93e472008-08-15 22:20:32 +000027#include "llvm/Support/Compiler.h"
Daniel Dunbarac93e472008-08-15 22:20:32 +000028#include "llvm/Target/TargetData.h"
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000029#include <map>
Chris Lattner431dc382009-01-27 05:06:01 +000030
31
Chris Lattner547907c2008-06-26 04:19:03 +000032using namespace clang;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000033using namespace CodeGen;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000034using llvm::dyn_cast;
35
36// The version of the runtime that this class targets. Must match the version
37// in the runtime.
38static const int RuntimeVersion = 8;
39static const int ProtocolVersion = 2;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000040
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000041namespace {
Chris Lattner547907c2008-06-26 04:19:03 +000042class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000043private:
Chris Lattner547907c2008-06-26 04:19:03 +000044 CodeGen::CodeGenModule &CGM;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000045 llvm::Module &TheModule;
Chris Lattner431dc382009-01-27 05:06:01 +000046 const llvm::PointerType *SelectorTy;
47 const llvm::PointerType *PtrToInt8Ty;
Fariborz Jahanianbb284742009-02-04 20:31:19 +000048 const llvm::FunctionType *IMPTy;
Chris Lattner431dc382009-01-27 05:06:01 +000049 const llvm::PointerType *IdTy;
50 const llvm::IntegerType *IntTy;
51 const llvm::PointerType *PtrTy;
52 const llvm::IntegerType *LongTy;
53 const llvm::PointerType *PtrToIntTy;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000054 std::vector<llvm::Constant*> Classes;
55 std::vector<llvm::Constant*> Categories;
56 std::vector<llvm::Constant*> ConstantStrings;
57 llvm::Function *LoadFunction;
58 llvm::StringMap<llvm::Constant*> ExistingProtocols;
59 typedef std::pair<std::string, std::string> TypedSelector;
60 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
61 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
62 // Some zeros used for GEPs in lots of places.
63 llvm::Constant *Zeros[2];
64 llvm::Constant *NULLPtr;
65private:
66 llvm::Constant *GenerateIvarList(
67 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
68 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
69 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
70 llvm::Constant *GenerateMethodList(const std::string &ClassName,
71 const std::string &CategoryName,
Chris Lattner578279d2008-06-26 05:08:00 +000072 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000073 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
74 bool isClassMethodList);
Fariborz Jahanian926c3712009-03-31 18:27:22 +000075 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000076 llvm::Constant *GenerateProtocolList(
77 const llvm::SmallVectorImpl<std::string> &Protocols);
78 llvm::Constant *GenerateClassStructure(
79 llvm::Constant *MetaClass,
80 llvm::Constant *SuperClass,
81 unsigned info,
Chris Lattnerad9c3f32008-06-26 04:47:04 +000082 const char *Name,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000083 llvm::Constant *Version,
84 llvm::Constant *InstanceSize,
85 llvm::Constant *IVars,
86 llvm::Constant *Methods,
87 llvm::Constant *Protocols);
88 llvm::Constant *GenerateProtocolMethodList(
89 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
90 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
91 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
92 &Name="");
93 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
94 std::vector<llvm::Constant*> &V, const std::string &Name="");
95 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
96 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000097public:
Chris Lattner547907c2008-06-26 04:19:03 +000098 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Steve Naroff2c8a08e2009-03-31 16:53:37 +000099 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *);
Daniel Dunbara04840b2008-08-23 03:46:30 +0000100 virtual CodeGen::RValue
101 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000102 QualType ResultType,
103 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000104 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000105 bool IsClassMessage,
106 const CallArgList &CallArgs);
Daniel Dunbara04840b2008-08-23 03:46:30 +0000107 virtual CodeGen::RValue
108 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000109 QualType ResultType,
110 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000111 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +0000112 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000113 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000114 bool IsClassMessage,
115 const CallArgList &CallArgs);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000116 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar434627a2008-08-16 00:25:02 +0000117 const ObjCInterfaceDecl *OID);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000118 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Chris Lattnerd71288e2008-06-26 04:37:12 +0000119
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000120 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
121 const ObjCContainerDecl *CD);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000122 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
123 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000124 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000125 const ObjCProtocolDecl *PD);
126 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000127 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbarf7103722008-09-24 03:38:44 +0000128 virtual llvm::Function *GetPropertyGetFunction();
129 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson58d16242008-08-31 04:05:03 +0000130 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlssonb01a2112008-09-09 10:04:29 +0000131
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000132 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
133 const Stmt &S);
Anders Carlssonb01a2112008-09-09 10:04:29 +0000134 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
135 const ObjCAtThrowStmt &S);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000136 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000137 llvm::Value *AddrWeakObj);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000138 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
139 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000140 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
141 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianf310b592008-11-20 19:23:36 +0000142 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
143 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000144 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
145 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000146 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
147 QualType ObjectTy,
148 llvm::Value *BaseValue,
149 const ObjCIvarDecl *Ivar,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000150 unsigned CVRQualifiers);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +0000151 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +0000152 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +0000153 const ObjCIvarDecl *Ivar);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000154};
155} // end anonymous namespace
156
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000157
158
159static std::string SymbolNameForClass(const std::string &ClassName) {
160 return ".objc_class_" + ClassName;
161}
162
163static std::string SymbolNameForMethod(const std::string &ClassName, const
164 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
165{
166 return "._objc_method_" + ClassName +"("+CategoryName+")"+
167 (isClassMethod ? "+" : "-") + MethodName;
168}
169
Chris Lattner547907c2008-06-26 04:19:03 +0000170CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
171 : CGM(cgm), TheModule(CGM.getModule()) {
Chris Lattner431dc382009-01-27 05:06:01 +0000172 IntTy = cast<llvm::IntegerType>(
173 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
174 LongTy = cast<llvm::IntegerType>(
175 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Chris Lattner547907c2008-06-26 04:19:03 +0000176
Sebastian Redle9e21b32008-12-04 00:10:55 +0000177 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000178 Zeros[1] = Zeros[0];
179 NULLPtr = llvm::ConstantPointerNull::get(
180 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattnerb326b172008-03-30 23:03:07 +0000181 // C string type. Used in lots of places.
182 PtrToInt8Ty =
183 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
184 // Get the selector Type.
Chris Lattner431dc382009-01-27 05:06:01 +0000185 SelectorTy = cast<llvm::PointerType>(
186 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
187
Chris Lattnerb326b172008-03-30 23:03:07 +0000188 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
189 PtrTy = PtrToInt8Ty;
190
191 // Object type
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000192 IdTy = cast<llvm::PointerType>(
193 CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType()));
Chris Lattnerb326b172008-03-30 23:03:07 +0000194
195 // IMP type
196 std::vector<const llvm::Type*> IMPArgs;
197 IMPArgs.push_back(IdTy);
198 IMPArgs.push_back(SelectorTy);
199 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000200}
201// This has to perform the lookup every time, since posing and related
202// techniques can modify the name -> class mapping.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000203llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbar434627a2008-08-16 00:25:02 +0000204 const ObjCInterfaceDecl *OID) {
Chris Lattner271d4c22008-11-24 05:29:24 +0000205 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Daniel Dunbar434627a2008-08-16 00:25:02 +0000206 ClassName = Builder.CreateStructGEP(ClassName, 0);
207
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000208 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000209 llvm::Constant *ClassLookupFn =
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000210 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
211 Params,
212 true),
213 "objc_lookup_class");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000214 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattnerb326b172008-03-30 23:03:07 +0000215}
216
Chris Lattnerd71288e2008-06-26 04:37:12 +0000217/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000218llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattnerd71288e2008-06-26 04:37:12 +0000219 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner3a8f2942008-11-24 03:33:13 +0000220 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattnerd71288e2008-06-26 04:37:12 +0000221 if (US == 0)
222 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
223 llvm::GlobalValue::InternalLinkage,
224 ".objc_untyped_selector_alias",
225 NULL, &TheModule);
226
227 return Builder.CreateLoad(US);
228
229}
230
Chris Lattnera5b18882008-06-26 04:44:19 +0000231llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
232 const std::string &Name) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000233 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
234 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
235 llvm::GlobalValue::InternalLinkage,
236 ConstStr, Name, &TheModule);
237 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
238}
239llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
240 std::vector<llvm::Constant*> &V, const std::string &Name) {
241 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
242 return new llvm::GlobalVariable(Ty, false,
243 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
244}
245llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
246 std::vector<llvm::Constant*> &V, const std::string &Name) {
247 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
248 return new llvm::GlobalVariable(Ty, false,
249 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
250}
251
252/// Generate an NSConstantString object.
253//TODO: In case there are any crazy people still using the GNU runtime without
254//an OpenStep implementation, this should let them select their own class for
255//constant strings.
Steve Naroff2c8a08e2009-03-31 16:53:37 +0000256llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) {
257 std::string Str(SL->getString()->getStrData(),
258 SL->getString()->getByteLength());
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000259 std::vector<llvm::Constant*> Ivars;
260 Ivars.push_back(NULLPtr);
Chris Lattnerbac12452008-06-21 21:44:18 +0000261 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000262 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000263 llvm::Constant *ObjCStr = MakeGlobal(
264 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
265 Ivars, ".objc_str");
266 ConstantStrings.push_back(
267 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
268 return ObjCStr;
269}
270
271///Generates a message send where the super is the receiver. This is a message
272///send to self with special delivery semantics indicating which class's method
273///should be called.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000274CodeGen::RValue
275CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000276 QualType ResultType,
277 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000278 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +0000279 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000280 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000281 bool IsClassMessage,
282 const CallArgList &CallArgs) {
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000283 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
284
285 CallArgList ActualArgs;
286
287 ActualArgs.push_back(
288 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
289 CGF.getContext().getObjCIdType()));
290 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
291 CGF.getContext().getObjCSelType()));
292 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
293
294 CodeGenTypes &Types = CGM.getTypes();
295 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
296 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
297
298
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000299 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000300 // TODO: This should be cached, not looked up every time.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000301 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Chris Lattner7eaa9f02009-04-25 23:19:45 +0000302 if (IsClassMessage)
303 {
304 ReceiverClass = CGF.Builder.CreateBitCast(ReceiverClass,
305 llvm::PointerType::getUnqual(IdTy));
306 ReceiverClass = CGF.Builder.CreateBitCast(CGF.Builder.CreateLoad(
307 ReceiverClass), IdTy);
308 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000309 // Construct the structure used to look up the IMP
310 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
311 IdTy, NULL);
Daniel Dunbara04840b2008-08-23 03:46:30 +0000312 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000313
Daniel Dunbara04840b2008-08-23 03:46:30 +0000314 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
315 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000316
317 // Get the IMP
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000318 std::vector<const llvm::Type*> Params;
319 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
320 Params.push_back(SelectorTy);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000321 llvm::Constant *lookupFunction =
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000322 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
323 llvm::PointerType::getUnqual(impType), Params, true),
324 "objc_msg_lookup_super");
325
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000326 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbara04840b2008-08-23 03:46:30 +0000327 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000328 lookupArgs+2);
329
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000330 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000331}
332
333/// Generate code for a message send expression.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000334CodeGen::RValue
335CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000336 QualType ResultType,
337 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000338 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000339 bool IsClassMessage,
340 const CallArgList &CallArgs) {
Daniel Dunbardd851282008-08-30 05:35:15 +0000341 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000342 CallArgList ActualArgs;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000343
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000344 ActualArgs.push_back(
345 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
346 CGF.getContext().getObjCIdType()));
347 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
348 CGF.getContext().getObjCSelType()));
349 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
350
351 CodeGenTypes &Types = CGM.getTypes();
352 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
353 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
354
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000355 std::vector<const llvm::Type*> Params;
356 Params.push_back(Receiver->getType());
357 Params.push_back(SelectorTy);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000358 llvm::Constant *lookupFunction =
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000359 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
360 llvm::PointerType::getUnqual(impType), Params, true),
361 "objc_msg_lookup");
362
Daniel Dunbara04840b2008-08-23 03:46:30 +0000363 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner7db5e942008-05-06 00:56:42 +0000364
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000365 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000366}
367
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000368/// Generates a MethodList. Used in construction of a objc_class and
369/// objc_category structures.
370llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattner578279d2008-06-26 05:08:00 +0000371 const std::string &CategoryName,
372 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000373 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
374 bool isClassMethodList) {
375 // Get the method structure type.
376 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
377 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
378 PtrToInt8Ty, // Method types
379 llvm::PointerType::getUnqual(IMPTy), //Method pointer
380 NULL);
381 std::vector<llvm::Constant*> Methods;
382 std::vector<llvm::Constant*> Elements;
383 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
384 Elements.clear();
Chris Lattner3a8f2942008-11-24 03:33:13 +0000385 llvm::Constant *C =
386 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattner578279d2008-06-26 05:08:00 +0000387 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000388 Elements.push_back(
389 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
390 llvm::Constant *Method =
391 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner3a8f2942008-11-24 03:33:13 +0000392 MethodSels[i].getAsString(),
Chris Lattnere7581092008-06-26 04:05:20 +0000393 isClassMethodList));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000394 Method = llvm::ConstantExpr::getBitCast(Method,
395 llvm::PointerType::getUnqual(IMPTy));
396 Elements.push_back(Method);
397 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
398 }
399
400 // Array of method structures
401 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattner578279d2008-06-26 05:08:00 +0000402 MethodSels.size());
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000403 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerbcb3e862008-06-26 04:52:29 +0000404 Methods);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000405
406 // Structure containing list pointer, array and array count
407 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
408 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
409 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
410 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
411 IntTy,
412 ObjCMethodArrayTy,
413 NULL);
414 // Refine next pointer type to concrete type
415 llvm::cast<llvm::OpaqueType>(
416 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
417 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
418
419 Methods.clear();
420 Methods.push_back(llvm::ConstantPointerNull::get(
421 llvm::PointerType::getUnqual(ObjCMethodListTy)));
422 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
423 MethodTypes.size()));
424 Methods.push_back(MethodArray);
425
426 // Create an instance of the structure
427 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
428}
429
430/// Generates an IvarList. Used in construction of a objc_class.
431llvm::Constant *CGObjCGNU::GenerateIvarList(
432 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
433 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
434 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
435 // Get the method structure type.
436 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
437 PtrToInt8Ty,
438 PtrToInt8Ty,
439 IntTy,
440 NULL);
441 std::vector<llvm::Constant*> Ivars;
442 std::vector<llvm::Constant*> Elements;
443 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
444 Elements.clear();
445 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
446 Zeros, 2));
447 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
448 Zeros, 2));
449 Elements.push_back(IvarOffsets[i]);
450 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
451 }
452
453 // Array of method structures
454 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
455 IvarNames.size());
456
457
458 Elements.clear();
Chris Lattner431dc382009-01-27 05:06:01 +0000459 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000460 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
461 // Structure containing array and array count
462 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
463 ObjCIvarArrayTy,
464 NULL);
465
466 // Create an instance of the structure
467 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
468}
469
470/// Generate a class structure
471llvm::Constant *CGObjCGNU::GenerateClassStructure(
472 llvm::Constant *MetaClass,
473 llvm::Constant *SuperClass,
474 unsigned info,
Chris Lattnerad9c3f32008-06-26 04:47:04 +0000475 const char *Name,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000476 llvm::Constant *Version,
477 llvm::Constant *InstanceSize,
478 llvm::Constant *IVars,
479 llvm::Constant *Methods,
480 llvm::Constant *Protocols) {
481 // Set up the class structure
482 // Note: Several of these are char*s when they should be ids. This is
483 // because the runtime performs this translation on load.
484 llvm::StructType *ClassTy = llvm::StructType::get(
485 PtrToInt8Ty, // class_pointer
486 PtrToInt8Ty, // super_class
487 PtrToInt8Ty, // name
488 LongTy, // version
489 LongTy, // info
490 LongTy, // instance_size
491 IVars->getType(), // ivars
492 Methods->getType(), // methods
493 // These are all filled in by the runtime, so we pretend
494 PtrTy, // dtable
495 PtrTy, // subclass_list
496 PtrTy, // sibling_class
497 PtrTy, // protocols
498 PtrTy, // gc_object_type
499 NULL);
500 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
501 llvm::Constant *NullP =
Chris Lattner431dc382009-01-27 05:06:01 +0000502 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000503 // Fill in the structure
504 std::vector<llvm::Constant*> Elements;
505 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
506 Elements.push_back(SuperClass);
Chris Lattnerad9c3f32008-06-26 04:47:04 +0000507 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000508 Elements.push_back(Zero);
509 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
510 Elements.push_back(InstanceSize);
511 Elements.push_back(IVars);
512 Elements.push_back(Methods);
513 Elements.push_back(NullP);
514 Elements.push_back(NullP);
515 Elements.push_back(NullP);
516 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
517 Elements.push_back(NullP);
518 // Create an instance of the structure
Chris Lattner4d018542008-07-21 06:31:05 +0000519 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000520}
521
522llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
523 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
524 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
525 // Get the method structure type.
526 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
527 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
528 PtrToInt8Ty,
529 NULL);
530 std::vector<llvm::Constant*> Methods;
531 std::vector<llvm::Constant*> Elements;
532 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
533 Elements.clear();
534 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
535 Zeros, 2));
536 Elements.push_back(
537 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
538 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
539 }
540 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
541 MethodNames.size());
542 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
543 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
544 IntTy, ObjCMethodArrayTy, NULL);
545 Methods.clear();
546 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
547 Methods.push_back(Array);
548 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
549}
550// Create the protocol list structure used in classes, categories and so on
551llvm::Constant *CGObjCGNU::GenerateProtocolList(
552 const llvm::SmallVectorImpl<std::string> &Protocols) {
553 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
554 Protocols.size());
555 llvm::StructType *ProtocolListTy = llvm::StructType::get(
556 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
557 LongTy,//FIXME: Should be size_t
558 ProtocolArrayTy,
559 NULL);
560 std::vector<llvm::Constant*> Elements;
561 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
562 iter != endIter ; iter++) {
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000563 llvm::Constant *protocol = ExistingProtocols[*iter];
564 if (!protocol)
565 protocol = GenerateEmptyProtocol(*iter);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000566 llvm::Constant *Ptr =
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000567 llvm::ConstantExpr::getBitCast(protocol, PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000568 Elements.push_back(Ptr);
569 }
570 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
571 Elements);
572 Elements.clear();
573 Elements.push_back(NULLPtr);
Chris Lattner431dc382009-01-27 05:06:01 +0000574 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000575 Elements.push_back(ProtocolArray);
576 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
577}
578
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000579llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000580 const ObjCProtocolDecl *PD) {
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000581 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
582 const llvm::Type *T =
583 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
584 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
585}
586
587llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
588 const std::string &ProtocolName) {
589 llvm::SmallVector<std::string, 0> EmptyStringVector;
590 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
591
592 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
593 llvm::Constant *InstanceMethodList =
594 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
595 llvm::Constant *ClassMethodList =
596 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
597 // Protocols are objects containing lists of the methods implemented and
598 // protocols adopted.
599 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
600 PtrToInt8Ty,
601 ProtocolList->getType(),
602 InstanceMethodList->getType(),
603 ClassMethodList->getType(),
604 NULL);
605 std::vector<llvm::Constant*> Elements;
606 // The isa pointer must be set to a magic number so the runtime knows it's
607 // the correct layout.
608 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
609 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
610 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
611 Elements.push_back(ProtocolList);
612 Elements.push_back(InstanceMethodList);
613 Elements.push_back(ClassMethodList);
614 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000615}
616
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000617void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
618 ASTContext &Context = CGM.getContext();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000619 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000620 llvm::SmallVector<std::string, 16> Protocols;
621 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
622 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattner271d4c22008-11-24 05:29:24 +0000623 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000624 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
625 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000626 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(Context),
627 E = PD->instmeth_end(Context); iter != E; iter++) {
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000628 std::string TypeStr;
629 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
630 InstanceMethodNames.push_back(
Chris Lattner3a8f2942008-11-24 03:33:13 +0000631 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000632 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000633 }
634 // Collect information about class methods:
635 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
636 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000637 for (ObjCProtocolDecl::classmeth_iterator
638 iter = PD->classmeth_begin(Context),
639 endIter = PD->classmeth_end(Context) ; iter != endIter ; iter++) {
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000640 std::string TypeStr;
641 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
642 ClassMethodNames.push_back(
Chris Lattner3a8f2942008-11-24 03:33:13 +0000643 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000644 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000645 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000646
647 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
648 llvm::Constant *InstanceMethodList =
649 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
650 llvm::Constant *ClassMethodList =
651 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
652 // Protocols are objects containing lists of the methods implemented and
653 // protocols adopted.
654 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
655 PtrToInt8Ty,
656 ProtocolList->getType(),
657 InstanceMethodList->getType(),
658 ClassMethodList->getType(),
659 NULL);
660 std::vector<llvm::Constant*> Elements;
661 // The isa pointer must be set to a magic number so the runtime knows it's
662 // the correct layout.
663 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
664 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
665 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
666 Elements.push_back(ProtocolList);
667 Elements.push_back(InstanceMethodList);
668 Elements.push_back(ClassMethodList);
669 ExistingProtocols[ProtocolName] =
670 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
671 ".objc_protocol"), IdTy);
672}
673
Daniel Dunbarac93e472008-08-15 22:20:32 +0000674void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000675 std::string ClassName = OCD->getClassInterface()->getNameAsString();
676 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000677 // Collect information about instance methods
678 llvm::SmallVector<Selector, 16> InstanceMethodSels;
679 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregorcd19b572009-04-23 01:02:12 +0000680 for (ObjCCategoryImplDecl::instmeth_iterator
681 iter = OCD->instmeth_begin(CGM.getContext()),
682 endIter = OCD->instmeth_end(CGM.getContext());
683 iter != endIter ; iter++) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000684 InstanceMethodSels.push_back((*iter)->getSelector());
685 std::string TypeStr;
686 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
687 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
688 }
689
690 // Collect information about class methods
691 llvm::SmallVector<Selector, 16> ClassMethodSels;
692 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregorcd19b572009-04-23 01:02:12 +0000693 for (ObjCCategoryImplDecl::classmeth_iterator
694 iter = OCD->classmeth_begin(CGM.getContext()),
695 endIter = OCD->classmeth_end(CGM.getContext());
696 iter != endIter ; iter++) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000697 ClassMethodSels.push_back((*iter)->getSelector());
698 std::string TypeStr;
699 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
700 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
701 }
702
703 // Collect the names of referenced protocols
704 llvm::SmallVector<std::string, 16> Protocols;
705 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
706 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
707 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
708 E = Protos.end(); I != E; ++I)
Chris Lattner271d4c22008-11-24 05:29:24 +0000709 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000710
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000711 std::vector<llvm::Constant*> Elements;
712 Elements.push_back(MakeConstantString(CategoryName));
713 Elements.push_back(MakeConstantString(ClassName));
714 // Instance method list
715 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattner578279d2008-06-26 05:08:00 +0000716 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000717 false), PtrTy));
718 // Class method list
719 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattner578279d2008-06-26 05:08:00 +0000720 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000721 PtrTy));
722 // Protocol list
723 Elements.push_back(llvm::ConstantExpr::getBitCast(
724 GenerateProtocolList(Protocols), PtrTy));
725 Categories.push_back(llvm::ConstantExpr::getBitCast(
726 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
727 PtrTy, PtrTy, NULL), Elements), PtrTy));
728}
Daniel Dunbarac93e472008-08-15 22:20:32 +0000729
730void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
731 ASTContext &Context = CGM.getContext();
732
733 // Get the superclass name.
734 const ObjCInterfaceDecl * SuperClassDecl =
735 OID->getClassInterface()->getSuperClass();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000736 std::string SuperClassName;
737 if (SuperClassDecl)
738 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000739
740 // Get the class name
Chris Lattnerb63b5502009-04-01 02:00:48 +0000741 ObjCInterfaceDecl *ClassDecl =
742 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000743 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000744
745 // Get the size of instances. For runtimes that support late-bound instances
746 // this should probably be something different (size just of instance
747 // varaibles in this class, not superclasses?).
Daniel Dunbar35403f92009-05-03 08:55:17 +0000748 const llvm::Type *ObjTy =
749 CGObjCRuntime::GetConcreteClassStruct(CGM, ClassDecl);
Chris Lattnerb63b5502009-04-01 02:00:48 +0000750 int instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000751
752 // Collect information about instance variables.
753 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
754 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
755 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
Daniel Dunbarac93e472008-08-15 22:20:32 +0000756 ObjTy = llvm::PointerType::getUnqual(ObjTy);
757 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
758 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
759 // Store the name
Chris Lattner271d4c22008-11-24 05:29:24 +0000760 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
761 ->getNameAsString()));
Daniel Dunbarac93e472008-08-15 22:20:32 +0000762 // Get the type encoding for this ivar
763 std::string TypeStr;
Daniel Dunbarc9197cd2008-10-17 20:21:44 +0000764 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000765 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
766 // Get the offset
Daniel Dunbarbdeefcc2009-04-22 08:20:31 +0000767 uint64_t Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000768 IvarOffsets.push_back(
Daniel Dunbarbdeefcc2009-04-22 08:20:31 +0000769 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset));
Daniel Dunbarac93e472008-08-15 22:20:32 +0000770 }
771
772 // Collect information about instance methods
773 llvm::SmallVector<Selector, 16> InstanceMethodSels;
774 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregorcd19b572009-04-23 01:02:12 +0000775 for (ObjCImplementationDecl::instmeth_iterator
776 iter = OID->instmeth_begin(CGM.getContext()),
777 endIter = OID->instmeth_end(CGM.getContext());
778 iter != endIter ; iter++) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000779 InstanceMethodSels.push_back((*iter)->getSelector());
780 std::string TypeStr;
781 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
782 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
783 }
784
785 // Collect information about class methods
786 llvm::SmallVector<Selector, 16> ClassMethodSels;
787 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregorcd19b572009-04-23 01:02:12 +0000788 for (ObjCImplementationDecl::classmeth_iterator
789 iter = OID->classmeth_begin(CGM.getContext()),
790 endIter = OID->classmeth_end(CGM.getContext());
791 iter != endIter ; iter++) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000792 ClassMethodSels.push_back((*iter)->getSelector());
793 std::string TypeStr;
794 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
795 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
796 }
797 // Collect the names of referenced protocols
798 llvm::SmallVector<std::string, 16> Protocols;
799 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
800 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
801 E = Protos.end(); I != E; ++I)
Chris Lattner271d4c22008-11-24 05:29:24 +0000802 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000803
804
805
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000806 // Get the superclass pointer.
807 llvm::Constant *SuperClass;
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000808 if (!SuperClassName.empty()) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000809 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
810 } else {
Chris Lattner431dc382009-01-27 05:06:01 +0000811 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000812 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000813 // Empty vector used to construct empty method lists
814 llvm::SmallVector<llvm::Constant*, 1> empty;
815 // Generate the method and instance variable lists
816 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattner578279d2008-06-26 05:08:00 +0000817 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000818 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattner578279d2008-06-26 05:08:00 +0000819 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000820 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
821 IvarOffsets);
822 //Generate metaclass for class methods
823 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner4d018542008-07-21 06:31:05 +0000824 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000825 empty, empty, empty), ClassMethodList, NULLPtr);
826 // Generate the class structure
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000827 llvm::Constant *ClassStruct =
828 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
829 ClassName.c_str(), 0,
Sebastian Redle9e21b32008-12-04 00:10:55 +0000830 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000831 MethodList, GenerateProtocolList(Protocols));
832 // Add class structure to list to be added to the symtab later
833 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
834 Classes.push_back(ClassStruct);
835}
836
837llvm::Function *CGObjCGNU::ModuleInitFunction() {
838 // Only emit an ObjC load function if no Objective-C stuff has been called
839 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
840 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikovb41e4972008-06-01 15:14:46 +0000841 UntypedSelectors.empty())
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000842 return NULL;
Eli Friedman7719bc82008-06-01 16:00:02 +0000843
Chris Lattner431dc382009-01-27 05:06:01 +0000844 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
845 SelectorTy->getElementType());
846 const llvm::Type *SelStructPtrTy = SelectorTy;
847 bool isSelOpaque = false;
848 if (SelStructTy == 0) {
849 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
850 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
851 isSelOpaque = true;
852 }
853
Eli Friedman7719bc82008-06-01 16:00:02 +0000854 // Name the ObjC types to make the IR a bit easier to read
Chris Lattner431dc382009-01-27 05:06:01 +0000855 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman7719bc82008-06-01 16:00:02 +0000856 TheModule.addTypeName(".objc_id", IdTy);
857 TheModule.addTypeName(".objc_imp", IMPTy);
858
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000859 std::vector<llvm::Constant*> Elements;
Chris Lattner7eaa9f02009-04-25 23:19:45 +0000860 llvm::Constant *Statics = NULLPtr;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000861 // Generate statics list:
Chris Lattner7eaa9f02009-04-25 23:19:45 +0000862 if (ConstantStrings.size()) {
863 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
864 ConstantStrings.size() + 1);
865 ConstantStrings.push_back(NULLPtr);
866 Elements.push_back(MakeConstantString("NSConstantString",
867 ".objc_static_class_name"));
868 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
869 ConstantStrings));
870 llvm::StructType *StaticsListTy =
871 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
872 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
873 Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
874 llvm::ArrayType *StaticsListArrayTy =
875 llvm::ArrayType::get(StaticsListPtrTy, 2);
876 Elements.clear();
877 Elements.push_back(Statics);
878 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
879 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
880 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
881 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000882 // Array of classes, categories, and constant objects
883 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
884 Classes.size() + Categories.size() + 2);
Chris Lattner431dc382009-01-27 05:06:01 +0000885 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Chris Lattneref843042008-06-26 04:10:42 +0000886 llvm::Type::Int16Ty,
887 llvm::Type::Int16Ty,
888 ClassListTy, NULL);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000889
890 Elements.clear();
891 // Pointer to an array of selectors used in this module.
892 std::vector<llvm::Constant*> Selectors;
893 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
894 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
895 iter != iterEnd ; ++iter) {
Chris Lattneref843042008-06-26 04:10:42 +0000896 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
897 Elements.push_back(MakeConstantString(iter->first.second,
898 ".objc_sel_types"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000899 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
900 Elements.clear();
901 }
902 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
903 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattneref843042008-06-26 04:10:42 +0000904 iter != iterEnd; ++iter) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000905 Elements.push_back(
Chris Lattneref843042008-06-26 04:10:42 +0000906 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000907 Elements.push_back(NULLPtr);
908 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
909 Elements.clear();
910 }
911 Elements.push_back(NULLPtr);
912 Elements.push_back(NULLPtr);
913 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
914 Elements.clear();
915 // Number of static selectors
916 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
917 llvm::Constant *SelectorList = MakeGlobal(
918 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
919 ".objc_selector_list");
Chris Lattner431dc382009-01-27 05:06:01 +0000920 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
921 SelStructPtrTy));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000922
923 // Now that all of the static selectors exist, create pointers to them.
924 int index = 0;
925 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
926 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
927 iter != iterEnd; ++iter) {
928 llvm::Constant *Idxs[] = {Zeros[0],
929 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattner431dc382009-01-27 05:06:01 +0000930 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
931 true, llvm::GlobalValue::InternalLinkage,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000932 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
933 ".objc_sel_ptr", &TheModule);
Chris Lattner431dc382009-01-27 05:06:01 +0000934 // If selectors are defined as an opaque type, cast the pointer to this
935 // type.
936 if (isSelOpaque) {
937 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
938 llvm::PointerType::getUnqual(SelectorTy));
939 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000940 (*iter).second->setAliasee(SelPtr);
941 }
942 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
943 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
944 iter != iterEnd; iter++) {
945 llvm::Constant *Idxs[] = {Zeros[0],
946 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattner431dc382009-01-27 05:06:01 +0000947 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000948 llvm::GlobalValue::InternalLinkage,
949 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
950 ".objc_sel_ptr", &TheModule);
Chris Lattner431dc382009-01-27 05:06:01 +0000951 // If selectors are defined as an opaque type, cast the pointer to this
952 // type.
953 if (isSelOpaque) {
954 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
955 llvm::PointerType::getUnqual(SelectorTy));
956 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000957 (*iter).second->setAliasee(SelPtr);
958 }
959 // Number of classes defined.
960 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
961 Classes.size()));
962 // Number of categories defined
963 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
964 Categories.size()));
965 // Create an array of classes, then categories, then static object instances
966 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
967 // NULL-terminated list of static object instances (mainly constant strings)
968 Classes.push_back(Statics);
969 Classes.push_back(NULLPtr);
970 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
971 Elements.push_back(ClassList);
972 // Construct the symbol table
973 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
974
975 // The symbol table is contained in a module which has some version-checking
976 // constants
977 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
978 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
979 Elements.clear();
980 // Runtime version used for compatibility checking.
981 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahaniandefa06a2009-04-01 19:49:42 +0000982 // sizeof(ModuleTy)
983 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
984 Elements.push_back(llvm::ConstantInt::get(LongTy, td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000985 //FIXME: Should be the path to the file where this module was declared
986 Elements.push_back(NULLPtr);
987 Elements.push_back(SymTab);
988 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
989
990 // Create the load function calling the runtime entry point with the module
991 // structure
992 std::vector<const llvm::Type*> VoidArgs;
993 llvm::Function * LoadFunction = llvm::Function::Create(
994 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
995 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
996 &TheModule);
997 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000998 CGBuilderTy Builder;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000999 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian6158e692009-03-30 18:02:14 +00001000
1001 std::vector<const llvm::Type*> Params(1,
1002 llvm::PointerType::getUnqual(ModuleTy));
1003 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
1004 llvm::Type::VoidTy, Params, true), "__objc_exec_class");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001005 Builder.CreateCall(Register, Module);
1006 Builder.CreateRetVoid();
1007 return LoadFunction;
1008}
Daniel Dunbarac93e472008-08-15 22:20:32 +00001009
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00001010llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
1011 const ObjCContainerDecl *CD) {
Daniel Dunbarac93e472008-08-15 22:20:32 +00001012 const ObjCCategoryImplDecl *OCD =
Steve Naroff438be772009-01-08 19:41:02 +00001013 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner3a8f2942008-11-24 03:33:13 +00001014 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1015 std::string ClassName = OMD->getClassInterface()->getNameAsString();
1016 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregor5d764842009-01-09 17:18:27 +00001017 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbarac93e472008-08-15 22:20:32 +00001018
Daniel Dunbar34bda882009-02-02 23:23:47 +00001019 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001020 const llvm::FunctionType *MethodTy =
Daniel Dunbar34bda882009-02-02 23:23:47 +00001021 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001022 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1023 MethodName, isClassMethod);
1024
Gabor Greif815e2c12008-04-06 20:42:52 +00001025 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattnerb326b172008-03-30 23:03:07 +00001026 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001027 FunctionName,
Chris Lattnerb326b172008-03-30 23:03:07 +00001028 &TheModule);
Chris Lattnerb326b172008-03-30 23:03:07 +00001029 return Method;
1030}
1031
Daniel Dunbarf7103722008-09-24 03:38:44 +00001032llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
1033 return 0;
1034}
1035
1036llvm::Function *CGObjCGNU::GetPropertySetFunction() {
1037 return 0;
1038}
1039
1040llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian6158e692009-03-30 18:02:14 +00001041 std::vector<const llvm::Type*> Params(1, IdTy);
1042 return cast<llvm::Function>(CGM.CreateRuntimeFunction(
1043 llvm::FunctionType::get(llvm::Type::VoidTy, Params, true),
1044 "objc_enumerationMutation"));
Anders Carlsson58d16242008-08-31 04:05:03 +00001045}
1046
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001047void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1048 const Stmt &S) {
1049 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlssonb01a2112008-09-09 10:04:29 +00001050}
1051
1052void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbarf7103722008-09-24 03:38:44 +00001053 const ObjCAtThrowStmt &S) {
Anders Carlssonb01a2112008-09-09 10:04:29 +00001054 CGF.ErrorUnsupported(&S, "@throw statement");
1055}
1056
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00001057llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00001058 llvm::Value *AddrWeakObj)
1059{
1060 return 0;
1061}
1062
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00001063void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1064 llvm::Value *src, llvm::Value *dst)
1065{
1066 return;
1067}
1068
Fariborz Jahanian17958902008-11-19 00:59:10 +00001069void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1070 llvm::Value *src, llvm::Value *dst)
1071{
1072 return;
1073}
1074
Fariborz Jahanianf310b592008-11-20 19:23:36 +00001075void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1076 llvm::Value *src, llvm::Value *dst)
1077{
1078 return;
1079}
1080
Fariborz Jahanian17958902008-11-19 00:59:10 +00001081void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1082 llvm::Value *src, llvm::Value *dst)
1083{
1084 return;
1085}
1086
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001087LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1088 QualType ObjectTy,
1089 llvm::Value *BaseValue,
1090 const ObjCIvarDecl *Ivar,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001091 unsigned CVRQualifiers) {
Daniel Dunbarf5254bd2009-04-21 01:19:28 +00001092 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar85d37542009-04-22 07:32:20 +00001093 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
1094 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00001095}
1096
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001097llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +00001098 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001099 const ObjCIvarDecl *Ivar) {
Daniel Dunbar85d37542009-04-22 07:32:20 +00001100 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
Daniel Dunbare5bb23c2009-04-22 09:39:34 +00001101 return llvm::ConstantInt::get(LongTy, Offset);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001102}
1103
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001104CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattner547907c2008-06-26 04:19:03 +00001105 return new CGObjCGNU(CGM);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +00001106}