blob: 2ddd6e15dc6873330fd39b4e0d7a08cf8d5a6c55 [file] [log] [blame]
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
Chris Lattner0f984262008-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 Korobeynikov20ff3102008-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 Lattner0f984262008-03-01 08:50:34 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "CGObjCRuntime.h"
Chris Lattnerdce14062008-06-26 04:19:03 +000018#include "CodeGenModule.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000019#include "CodeGenFunction.h"
Chris Lattnerdce14062008-06-26 04:19:03 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000022#include "clang/AST/DeclObjC.h"
Daniel Dunbar2bebbf02009-05-03 10:46:44 +000023#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000024#include "clang/AST/StmtObjC.h"
Chris Lattner0f984262008-03-01 08:50:34 +000025#include "llvm/Module.h"
Chris Lattner0f984262008-03-01 08:50:34 +000026#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000027#include "llvm/ADT/StringMap.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000028#include "llvm/Support/Compiler.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000029#include "llvm/Target/TargetData.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000030#include <map>
Chris Lattnere160c9b2009-01-27 05:06:01 +000031
32
Chris Lattnerdce14062008-06-26 04:19:03 +000033using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000034using namespace CodeGen;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000035using llvm::dyn_cast;
36
37// The version of the runtime that this class targets. Must match the version
38// in the runtime.
39static const int RuntimeVersion = 8;
40static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000041
Chris Lattner0f984262008-03-01 08:50:34 +000042namespace {
Chris Lattnerdce14062008-06-26 04:19:03 +000043class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000044private:
Chris Lattnerdce14062008-06-26 04:19:03 +000045 CodeGen::CodeGenModule &CGM;
Chris Lattner0f984262008-03-01 08:50:34 +000046 llvm::Module &TheModule;
Chris Lattnere160c9b2009-01-27 05:06:01 +000047 const llvm::PointerType *SelectorTy;
48 const llvm::PointerType *PtrToInt8Ty;
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +000049 const llvm::FunctionType *IMPTy;
Chris Lattnere160c9b2009-01-27 05:06:01 +000050 const llvm::PointerType *IdTy;
51 const llvm::IntegerType *IntTy;
52 const llvm::PointerType *PtrTy;
53 const llvm::IntegerType *LongTy;
54 const llvm::PointerType *PtrToIntTy;
Daniel Dunbar5efccb12009-05-04 15:31:17 +000055 llvm::GlobalAlias *ClassPtrAlias;
56 llvm::GlobalAlias *MetaClassPtrAlias;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000057 std::vector<llvm::Constant*> Classes;
58 std::vector<llvm::Constant*> Categories;
59 std::vector<llvm::Constant*> ConstantStrings;
60 llvm::Function *LoadFunction;
61 llvm::StringMap<llvm::Constant*> ExistingProtocols;
62 typedef std::pair<std::string, std::string> TypedSelector;
63 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
64 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
65 // Some zeros used for GEPs in lots of places.
66 llvm::Constant *Zeros[2];
67 llvm::Constant *NULLPtr;
68private:
69 llvm::Constant *GenerateIvarList(
70 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
71 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
72 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
73 llvm::Constant *GenerateMethodList(const std::string &ClassName,
74 const std::string &CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +000075 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000076 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
77 bool isClassMethodList);
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +000078 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000079 llvm::Constant *GenerateProtocolList(
80 const llvm::SmallVectorImpl<std::string> &Protocols);
81 llvm::Constant *GenerateClassStructure(
82 llvm::Constant *MetaClass,
83 llvm::Constant *SuperClass,
84 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +000085 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000086 llvm::Constant *Version,
87 llvm::Constant *InstanceSize,
88 llvm::Constant *IVars,
89 llvm::Constant *Methods,
90 llvm::Constant *Protocols);
91 llvm::Constant *GenerateProtocolMethodList(
92 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
93 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
94 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
95 &Name="");
96 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
97 std::vector<llvm::Constant*> &V, const std::string &Name="");
98 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
99 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattner0f984262008-03-01 08:50:34 +0000100public:
Chris Lattnerdce14062008-06-26 04:19:03 +0000101 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Steve Naroff33fdb732009-03-31 16:53:37 +0000102 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000103 virtual CodeGen::RValue
104 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000105 QualType ResultType,
106 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000107 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000108 bool IsClassMessage,
109 const CallArgList &CallArgs);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000110 virtual CodeGen::RValue
111 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000112 QualType ResultType,
113 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000114 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000115 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000116 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000117 bool IsClassMessage,
118 const CallArgList &CallArgs);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000119 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000120 const ObjCInterfaceDecl *OID);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000121 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000122
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000123 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
124 const ObjCContainerDecl *CD);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000125 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
126 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000127 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000128 const ObjCProtocolDecl *PD);
129 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000130 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000131 virtual llvm::Function *GetPropertyGetFunction();
132 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000133 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000134
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000135 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
136 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000137 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
138 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000139 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000140 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000141 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
142 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000143 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
144 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000145 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
146 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000147 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
148 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000149 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
150 QualType ObjectTy,
151 llvm::Value *BaseValue,
152 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000153 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000154 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +0000155 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000156 const ObjCIvarDecl *Ivar);
Chris Lattner0f984262008-03-01 08:50:34 +0000157};
158} // end anonymous namespace
159
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000160
161
162static std::string SymbolNameForClass(const std::string &ClassName) {
163 return ".objc_class_" + ClassName;
164}
165
166static std::string SymbolNameForMethod(const std::string &ClassName, const
167 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
168{
169 return "._objc_method_" + ClassName +"("+CategoryName+")"+
170 (isClassMethod ? "+" : "-") + MethodName;
171}
172
Chris Lattnerdce14062008-06-26 04:19:03 +0000173CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000174 : CGM(cgm), TheModule(CGM.getModule()), ClassPtrAlias(0),
175 MetaClassPtrAlias(0) {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000176 IntTy = cast<llvm::IntegerType>(
177 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
178 LongTy = cast<llvm::IntegerType>(
179 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Chris Lattnerdce14062008-06-26 04:19:03 +0000180
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000181 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000182 Zeros[1] = Zeros[0];
183 NULLPtr = llvm::ConstantPointerNull::get(
184 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000185 // C string type. Used in lots of places.
186 PtrToInt8Ty =
187 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
188 // Get the selector Type.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000189 SelectorTy = cast<llvm::PointerType>(
190 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
191
Chris Lattner391d77a2008-03-30 23:03:07 +0000192 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
193 PtrTy = PtrToInt8Ty;
194
195 // Object type
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000196 IdTy = cast<llvm::PointerType>(
197 CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType()));
Chris Lattner391d77a2008-03-30 23:03:07 +0000198
199 // IMP type
200 std::vector<const llvm::Type*> IMPArgs;
201 IMPArgs.push_back(IdTy);
202 IMPArgs.push_back(SelectorTy);
203 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000204}
205// This has to perform the lookup every time, since posing and related
206// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000207llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000208 const ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000209 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000210 ClassName = Builder.CreateStructGEP(ClassName, 0);
211
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000212 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000213 llvm::Constant *ClassLookupFn =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000214 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
215 Params,
216 true),
217 "objc_lookup_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000218 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000219}
220
Chris Lattner8e67b632008-06-26 04:37:12 +0000221/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000222llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000223 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner077bf5e2008-11-24 03:33:13 +0000224 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner8e67b632008-06-26 04:37:12 +0000225 if (US == 0)
226 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
227 llvm::GlobalValue::InternalLinkage,
228 ".objc_untyped_selector_alias",
229 NULL, &TheModule);
230
231 return Builder.CreateLoad(US);
232
233}
234
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000235llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
236 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000237 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
238 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
239 llvm::GlobalValue::InternalLinkage,
240 ConstStr, Name, &TheModule);
241 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
242}
243llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
244 std::vector<llvm::Constant*> &V, const std::string &Name) {
245 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
246 return new llvm::GlobalVariable(Ty, false,
247 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
248}
249llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
250 std::vector<llvm::Constant*> &V, const std::string &Name) {
251 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
252 return new llvm::GlobalVariable(Ty, false,
253 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
254}
255
256/// Generate an NSConstantString object.
257//TODO: In case there are any crazy people still using the GNU runtime without
258//an OpenStep implementation, this should let them select their own class for
259//constant strings.
Steve Naroff33fdb732009-03-31 16:53:37 +0000260llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) {
261 std::string Str(SL->getString()->getStrData(),
262 SL->getString()->getByteLength());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000263 std::vector<llvm::Constant*> Ivars;
264 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000265 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000266 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000267 llvm::Constant *ObjCStr = MakeGlobal(
268 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
269 Ivars, ".objc_str");
270 ConstantStrings.push_back(
271 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
272 return ObjCStr;
273}
274
275///Generates a message send where the super is the receiver. This is a message
276///send to self with special delivery semantics indicating which class's method
277///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000278CodeGen::RValue
279CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000280 QualType ResultType,
281 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000282 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000283 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000284 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000285 bool IsClassMessage,
286 const CallArgList &CallArgs) {
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000287 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
288
289 CallArgList ActualArgs;
290
291 ActualArgs.push_back(
292 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
293 CGF.getContext().getObjCIdType()));
294 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
295 CGF.getContext().getObjCSelType()));
296 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
297
298 CodeGenTypes &Types = CGM.getTypes();
299 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
300 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
301
302
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000303 // Set up global aliases for the metaclass or class pointer if they do not
304 // already exist. These will are forward-references which will be set to
305 // pointers to the class and metaclass structure created for the runtime load
306 // function. To send a message to super, we look up the value of the
307 // super_class pointer from either the class or metaclass structure.
308 llvm::Value *ReceiverClass = 0;
309 if (IsClassMessage) {
310 if (!MetaClassPtrAlias) {
311 MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
Chris Lattnerc6e2ab02009-05-04 16:56:33 +0000312 llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
313 Class->getNameAsString(), NULL, &TheModule);
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000314 }
315 ReceiverClass = MetaClassPtrAlias;
316 } else {
317 if (!ClassPtrAlias) {
318 ClassPtrAlias = new llvm::GlobalAlias(IdTy,
Chris Lattnerc6e2ab02009-05-04 16:56:33 +0000319 llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
320 Class->getNameAsString(), NULL, &TheModule);
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000321 }
322 ReceiverClass = ClassPtrAlias;
Chris Lattner71238f62009-04-25 23:19:45 +0000323 }
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000324 // Cast the pointer to a simplified version of the class structure
325 ReceiverClass = CGF.Builder.CreateBitCast(ReceiverClass,
326 llvm::PointerType::getUnqual(llvm::StructType::get(IdTy, IdTy, NULL)));
327 // Get the superclass pointer
328 ReceiverClass = CGF.Builder.CreateStructGEP(ReceiverClass, 1);
329 // Load the superclass pointer
330 ReceiverClass = CGF.Builder.CreateLoad(ReceiverClass);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000331 // Construct the structure used to look up the IMP
332 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
333 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000334 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000335
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000336 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000337 CGF.Builder.CreateStore(ReceiverClass,
338 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000339
340 // Get the IMP
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000341 std::vector<const llvm::Type*> Params;
342 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
343 Params.push_back(SelectorTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000344 llvm::Constant *lookupFunction =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000345 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
346 llvm::PointerType::getUnqual(impType), Params, true),
347 "objc_msg_lookup_super");
348
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000349 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000350 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000351 lookupArgs+2);
352
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000353 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000354}
355
356/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000357CodeGen::RValue
358CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000359 QualType ResultType,
360 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000361 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000362 bool IsClassMessage,
363 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000364 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000365 CallArgList ActualArgs;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000366
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000367 ActualArgs.push_back(
368 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
369 CGF.getContext().getObjCIdType()));
370 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
371 CGF.getContext().getObjCSelType()));
372 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
373
374 CodeGenTypes &Types = CGM.getTypes();
375 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
376 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
377
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000378 std::vector<const llvm::Type*> Params;
379 Params.push_back(Receiver->getType());
380 Params.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000381 llvm::Constant *lookupFunction =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000382 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
383 llvm::PointerType::getUnqual(impType), Params, true),
384 "objc_msg_lookup");
385
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000386 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000387
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000388 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000389}
390
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000391/// Generates a MethodList. Used in construction of a objc_class and
392/// objc_category structures.
393llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000394 const std::string &CategoryName,
395 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000396 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
397 bool isClassMethodList) {
398 // Get the method structure type.
399 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
400 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
401 PtrToInt8Ty, // Method types
402 llvm::PointerType::getUnqual(IMPTy), //Method pointer
403 NULL);
404 std::vector<llvm::Constant*> Methods;
405 std::vector<llvm::Constant*> Elements;
406 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
407 Elements.clear();
Chris Lattner077bf5e2008-11-24 03:33:13 +0000408 llvm::Constant *C =
409 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattnera4210072008-06-26 05:08:00 +0000410 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000411 Elements.push_back(
412 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
413 llvm::Constant *Method =
414 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000415 MethodSels[i].getAsString(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000416 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000417 Method = llvm::ConstantExpr::getBitCast(Method,
418 llvm::PointerType::getUnqual(IMPTy));
419 Elements.push_back(Method);
420 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
421 }
422
423 // Array of method structures
424 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000425 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000426 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000427 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000428
429 // Structure containing list pointer, array and array count
430 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
431 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
432 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
433 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
434 IntTy,
435 ObjCMethodArrayTy,
436 NULL);
437 // Refine next pointer type to concrete type
438 llvm::cast<llvm::OpaqueType>(
439 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
440 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
441
442 Methods.clear();
443 Methods.push_back(llvm::ConstantPointerNull::get(
444 llvm::PointerType::getUnqual(ObjCMethodListTy)));
445 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
446 MethodTypes.size()));
447 Methods.push_back(MethodArray);
448
449 // Create an instance of the structure
450 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
451}
452
453/// Generates an IvarList. Used in construction of a objc_class.
454llvm::Constant *CGObjCGNU::GenerateIvarList(
455 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
456 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
457 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
458 // Get the method structure type.
459 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
460 PtrToInt8Ty,
461 PtrToInt8Ty,
462 IntTy,
463 NULL);
464 std::vector<llvm::Constant*> Ivars;
465 std::vector<llvm::Constant*> Elements;
466 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
467 Elements.clear();
468 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
469 Zeros, 2));
470 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
471 Zeros, 2));
472 Elements.push_back(IvarOffsets[i]);
473 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
474 }
475
476 // Array of method structures
477 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
478 IvarNames.size());
479
480
481 Elements.clear();
Chris Lattnere160c9b2009-01-27 05:06:01 +0000482 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000483 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
484 // Structure containing array and array count
485 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
486 ObjCIvarArrayTy,
487 NULL);
488
489 // Create an instance of the structure
490 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
491}
492
493/// Generate a class structure
494llvm::Constant *CGObjCGNU::GenerateClassStructure(
495 llvm::Constant *MetaClass,
496 llvm::Constant *SuperClass,
497 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000498 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000499 llvm::Constant *Version,
500 llvm::Constant *InstanceSize,
501 llvm::Constant *IVars,
502 llvm::Constant *Methods,
503 llvm::Constant *Protocols) {
504 // Set up the class structure
505 // Note: Several of these are char*s when they should be ids. This is
506 // because the runtime performs this translation on load.
507 llvm::StructType *ClassTy = llvm::StructType::get(
508 PtrToInt8Ty, // class_pointer
509 PtrToInt8Ty, // super_class
510 PtrToInt8Ty, // name
511 LongTy, // version
512 LongTy, // info
513 LongTy, // instance_size
514 IVars->getType(), // ivars
515 Methods->getType(), // methods
516 // These are all filled in by the runtime, so we pretend
517 PtrTy, // dtable
518 PtrTy, // subclass_list
519 PtrTy, // sibling_class
520 PtrTy, // protocols
521 PtrTy, // gc_object_type
522 NULL);
523 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
524 llvm::Constant *NullP =
Chris Lattnere160c9b2009-01-27 05:06:01 +0000525 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000526 // Fill in the structure
527 std::vector<llvm::Constant*> Elements;
528 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
529 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000530 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000531 Elements.push_back(Zero);
532 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
533 Elements.push_back(InstanceSize);
534 Elements.push_back(IVars);
535 Elements.push_back(Methods);
536 Elements.push_back(NullP);
537 Elements.push_back(NullP);
538 Elements.push_back(NullP);
539 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
540 Elements.push_back(NullP);
541 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000542 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000543}
544
545llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
546 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
547 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
548 // Get the method structure type.
549 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
550 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
551 PtrToInt8Ty,
552 NULL);
553 std::vector<llvm::Constant*> Methods;
554 std::vector<llvm::Constant*> Elements;
555 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
556 Elements.clear();
557 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
558 Zeros, 2));
559 Elements.push_back(
560 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
561 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
562 }
563 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
564 MethodNames.size());
565 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
566 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
567 IntTy, ObjCMethodArrayTy, NULL);
568 Methods.clear();
569 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
570 Methods.push_back(Array);
571 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
572}
573// Create the protocol list structure used in classes, categories and so on
574llvm::Constant *CGObjCGNU::GenerateProtocolList(
575 const llvm::SmallVectorImpl<std::string> &Protocols) {
576 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
577 Protocols.size());
578 llvm::StructType *ProtocolListTy = llvm::StructType::get(
579 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
580 LongTy,//FIXME: Should be size_t
581 ProtocolArrayTy,
582 NULL);
583 std::vector<llvm::Constant*> Elements;
584 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
585 iter != endIter ; iter++) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000586 llvm::Constant *protocol = ExistingProtocols[*iter];
587 if (!protocol)
588 protocol = GenerateEmptyProtocol(*iter);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000589 llvm::Constant *Ptr =
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000590 llvm::ConstantExpr::getBitCast(protocol, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000591 Elements.push_back(Ptr);
592 }
593 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
594 Elements);
595 Elements.clear();
596 Elements.push_back(NULLPtr);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000597 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000598 Elements.push_back(ProtocolArray);
599 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
600}
601
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000602llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000603 const ObjCProtocolDecl *PD) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000604 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
605 const llvm::Type *T =
606 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
607 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
608}
609
610llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
611 const std::string &ProtocolName) {
612 llvm::SmallVector<std::string, 0> EmptyStringVector;
613 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
614
615 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
616 llvm::Constant *InstanceMethodList =
617 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
618 llvm::Constant *ClassMethodList =
619 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
620 // Protocols are objects containing lists of the methods implemented and
621 // protocols adopted.
622 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
623 PtrToInt8Ty,
624 ProtocolList->getType(),
625 InstanceMethodList->getType(),
626 ClassMethodList->getType(),
627 NULL);
628 std::vector<llvm::Constant*> Elements;
629 // The isa pointer must be set to a magic number so the runtime knows it's
630 // the correct layout.
631 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
632 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
633 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
634 Elements.push_back(ProtocolList);
635 Elements.push_back(InstanceMethodList);
636 Elements.push_back(ClassMethodList);
637 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000638}
639
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000640void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
641 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000642 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000643 llvm::SmallVector<std::string, 16> Protocols;
644 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
645 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000646 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000647 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
648 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000649 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(Context),
650 E = PD->instmeth_end(Context); iter != E; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000651 std::string TypeStr;
652 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
653 InstanceMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000654 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000655 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000656 }
657 // Collect information about class methods:
658 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
659 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000660 for (ObjCProtocolDecl::classmeth_iterator
661 iter = PD->classmeth_begin(Context),
662 endIter = PD->classmeth_end(Context) ; iter != endIter ; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000663 std::string TypeStr;
664 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
665 ClassMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000666 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000667 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000668 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000669
670 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
671 llvm::Constant *InstanceMethodList =
672 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
673 llvm::Constant *ClassMethodList =
674 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
675 // Protocols are objects containing lists of the methods implemented and
676 // protocols adopted.
677 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
678 PtrToInt8Ty,
679 ProtocolList->getType(),
680 InstanceMethodList->getType(),
681 ClassMethodList->getType(),
682 NULL);
683 std::vector<llvm::Constant*> Elements;
684 // The isa pointer must be set to a magic number so the runtime knows it's
685 // the correct layout.
686 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
687 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
688 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
689 Elements.push_back(ProtocolList);
690 Elements.push_back(InstanceMethodList);
691 Elements.push_back(ClassMethodList);
692 ExistingProtocols[ProtocolName] =
693 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
694 ".objc_protocol"), IdTy);
695}
696
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000697void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000698 std::string ClassName = OCD->getClassInterface()->getNameAsString();
699 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000700 // Collect information about instance methods
701 llvm::SmallVector<Selector, 16> InstanceMethodSels;
702 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +0000703 for (ObjCCategoryImplDecl::instmeth_iterator
704 iter = OCD->instmeth_begin(CGM.getContext()),
705 endIter = OCD->instmeth_end(CGM.getContext());
706 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000707 InstanceMethodSels.push_back((*iter)->getSelector());
708 std::string TypeStr;
709 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
710 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
711 }
712
713 // Collect information about class methods
714 llvm::SmallVector<Selector, 16> ClassMethodSels;
715 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +0000716 for (ObjCCategoryImplDecl::classmeth_iterator
717 iter = OCD->classmeth_begin(CGM.getContext()),
718 endIter = OCD->classmeth_end(CGM.getContext());
719 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000720 ClassMethodSels.push_back((*iter)->getSelector());
721 std::string TypeStr;
722 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
723 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
724 }
725
726 // Collect the names of referenced protocols
727 llvm::SmallVector<std::string, 16> Protocols;
728 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
729 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
730 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
731 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000732 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000733
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000734 std::vector<llvm::Constant*> Elements;
735 Elements.push_back(MakeConstantString(CategoryName));
736 Elements.push_back(MakeConstantString(ClassName));
737 // Instance method list
738 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000739 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000740 false), PtrTy));
741 // Class method list
742 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000743 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000744 PtrTy));
745 // Protocol list
746 Elements.push_back(llvm::ConstantExpr::getBitCast(
747 GenerateProtocolList(Protocols), PtrTy));
748 Categories.push_back(llvm::ConstantExpr::getBitCast(
749 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
750 PtrTy, PtrTy, NULL), Elements), PtrTy));
751}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000752
753void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
754 ASTContext &Context = CGM.getContext();
755
756 // Get the superclass name.
757 const ObjCInterfaceDecl * SuperClassDecl =
758 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000759 std::string SuperClassName;
760 if (SuperClassDecl)
761 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000762
763 // Get the class name
Chris Lattner09dc6662009-04-01 02:00:48 +0000764 ObjCInterfaceDecl *ClassDecl =
765 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000766 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000767
Daniel Dunbar2bebbf02009-05-03 10:46:44 +0000768 // Get the size of instances.
769 int instanceSize = Context.getASTObjCImplementationLayout(OID).getSize() / 8;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000770
771 // Collect information about instance variables.
772 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
773 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
774 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000775 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
776 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
777 // Store the name
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000778 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
779 ->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000780 // Get the type encoding for this ivar
781 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000782 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000783 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
784 // Get the offset
Daniel Dunbarc8cbf192009-04-22 08:20:31 +0000785 uint64_t Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000786 IvarOffsets.push_back(
Daniel Dunbarc8cbf192009-04-22 08:20:31 +0000787 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000788 }
789
790 // Collect information about instance methods
791 llvm::SmallVector<Selector, 16> InstanceMethodSels;
792 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +0000793 for (ObjCImplementationDecl::instmeth_iterator
794 iter = OID->instmeth_begin(CGM.getContext()),
795 endIter = OID->instmeth_end(CGM.getContext());
796 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000797 InstanceMethodSels.push_back((*iter)->getSelector());
798 std::string TypeStr;
799 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
800 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
801 }
802
803 // Collect information about class methods
804 llvm::SmallVector<Selector, 16> ClassMethodSels;
805 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +0000806 for (ObjCImplementationDecl::classmeth_iterator
807 iter = OID->classmeth_begin(CGM.getContext()),
808 endIter = OID->classmeth_end(CGM.getContext());
809 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000810 ClassMethodSels.push_back((*iter)->getSelector());
811 std::string TypeStr;
812 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
813 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
814 }
815 // Collect the names of referenced protocols
816 llvm::SmallVector<std::string, 16> Protocols;
817 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
818 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
819 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000820 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000821
822
823
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000824 // Get the superclass pointer.
825 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +0000826 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000827 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
828 } else {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000829 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000830 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000831 // Empty vector used to construct empty method lists
832 llvm::SmallVector<llvm::Constant*, 1> empty;
833 // Generate the method and instance variable lists
834 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000835 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000836 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000837 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000838 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
839 IvarOffsets);
840 //Generate metaclass for class methods
841 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000842 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000843 empty, empty, empty), ClassMethodList, NULLPtr);
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000844
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000845 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +0000846 llvm::Constant *ClassStruct =
847 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
848 ClassName.c_str(), 0,
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000849 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000850 MethodList, GenerateProtocolList(Protocols));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000851
852 // Resolve the class aliases, if they exist.
853 if (ClassPtrAlias) {
854 ClassPtrAlias->setAliasee(
855 llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
856 ClassPtrAlias = 0;
857 }
858 if (MetaClassPtrAlias) {
859 MetaClassPtrAlias->setAliasee(
860 llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
861 MetaClassPtrAlias = 0;
862 }
863
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000864 // Add class structure to list to be added to the symtab later
865 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
866 Classes.push_back(ClassStruct);
867}
868
869llvm::Function *CGObjCGNU::ModuleInitFunction() {
870 // Only emit an ObjC load function if no Objective-C stuff has been called
871 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
872 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000873 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000874 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000875
Chris Lattnere160c9b2009-01-27 05:06:01 +0000876 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
877 SelectorTy->getElementType());
878 const llvm::Type *SelStructPtrTy = SelectorTy;
879 bool isSelOpaque = false;
880 if (SelStructTy == 0) {
881 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
882 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
883 isSelOpaque = true;
884 }
885
Eli Friedman1b8956e2008-06-01 16:00:02 +0000886 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +0000887 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +0000888 TheModule.addTypeName(".objc_id", IdTy);
889 TheModule.addTypeName(".objc_imp", IMPTy);
890
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000891 std::vector<llvm::Constant*> Elements;
Chris Lattner71238f62009-04-25 23:19:45 +0000892 llvm::Constant *Statics = NULLPtr;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000893 // Generate statics list:
Chris Lattner71238f62009-04-25 23:19:45 +0000894 if (ConstantStrings.size()) {
895 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
896 ConstantStrings.size() + 1);
897 ConstantStrings.push_back(NULLPtr);
898 Elements.push_back(MakeConstantString("NSConstantString",
899 ".objc_static_class_name"));
900 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
901 ConstantStrings));
902 llvm::StructType *StaticsListTy =
903 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
904 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
905 Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
906 llvm::ArrayType *StaticsListArrayTy =
907 llvm::ArrayType::get(StaticsListPtrTy, 2);
908 Elements.clear();
909 Elements.push_back(Statics);
910 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
911 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
912 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
913 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000914 // Array of classes, categories, and constant objects
915 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
916 Classes.size() + Categories.size() + 2);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000917 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Chris Lattner630404b2008-06-26 04:10:42 +0000918 llvm::Type::Int16Ty,
919 llvm::Type::Int16Ty,
920 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000921
922 Elements.clear();
923 // Pointer to an array of selectors used in this module.
924 std::vector<llvm::Constant*> Selectors;
925 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
926 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
927 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000928 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
929 Elements.push_back(MakeConstantString(iter->first.second,
930 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000931 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
932 Elements.clear();
933 }
934 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
935 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000936 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000937 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000938 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000939 Elements.push_back(NULLPtr);
940 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
941 Elements.clear();
942 }
943 Elements.push_back(NULLPtr);
944 Elements.push_back(NULLPtr);
945 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
946 Elements.clear();
947 // Number of static selectors
948 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
949 llvm::Constant *SelectorList = MakeGlobal(
950 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
951 ".objc_selector_list");
Chris Lattnere160c9b2009-01-27 05:06:01 +0000952 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
953 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000954
955 // Now that all of the static selectors exist, create pointers to them.
956 int index = 0;
957 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
958 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
959 iter != iterEnd; ++iter) {
960 llvm::Constant *Idxs[] = {Zeros[0],
961 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000962 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
963 true, llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000964 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
965 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000966 // If selectors are defined as an opaque type, cast the pointer to this
967 // type.
968 if (isSelOpaque) {
969 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
970 llvm::PointerType::getUnqual(SelectorTy));
971 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000972 (*iter).second->setAliasee(SelPtr);
973 }
974 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
975 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
976 iter != iterEnd; iter++) {
977 llvm::Constant *Idxs[] = {Zeros[0],
978 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000979 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000980 llvm::GlobalValue::InternalLinkage,
981 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
982 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000983 // If selectors are defined as an opaque type, cast the pointer to this
984 // type.
985 if (isSelOpaque) {
986 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
987 llvm::PointerType::getUnqual(SelectorTy));
988 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000989 (*iter).second->setAliasee(SelPtr);
990 }
991 // Number of classes defined.
992 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
993 Classes.size()));
994 // Number of categories defined
995 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
996 Categories.size()));
997 // Create an array of classes, then categories, then static object instances
998 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
999 // NULL-terminated list of static object instances (mainly constant strings)
1000 Classes.push_back(Statics);
1001 Classes.push_back(NULLPtr);
1002 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
1003 Elements.push_back(ClassList);
1004 // Construct the symbol table
1005 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
1006
1007 // The symbol table is contained in a module which has some version-checking
1008 // constants
1009 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
1010 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
1011 Elements.clear();
1012 // Runtime version used for compatibility checking.
1013 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahanian91a0b512009-04-01 19:49:42 +00001014 // sizeof(ModuleTy)
1015 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
1016 Elements.push_back(llvm::ConstantInt::get(LongTy, td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001017 //FIXME: Should be the path to the file where this module was declared
1018 Elements.push_back(NULLPtr);
1019 Elements.push_back(SymTab);
1020 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
1021
1022 // Create the load function calling the runtime entry point with the module
1023 // structure
1024 std::vector<const llvm::Type*> VoidArgs;
1025 llvm::Function * LoadFunction = llvm::Function::Create(
1026 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
1027 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
1028 &TheModule);
1029 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001030 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001031 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian26c82942009-03-30 18:02:14 +00001032
1033 std::vector<const llvm::Type*> Params(1,
1034 llvm::PointerType::getUnqual(ModuleTy));
1035 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
1036 llvm::Type::VoidTy, Params, true), "__objc_exec_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001037 Builder.CreateCall(Register, Module);
1038 Builder.CreateRetVoid();
1039 return LoadFunction;
1040}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001041
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001042llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
1043 const ObjCContainerDecl *CD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001044 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +00001045 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +00001046 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1047 std::string ClassName = OMD->getClassInterface()->getNameAsString();
1048 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001049 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001050
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001051 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001052 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001053 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001054 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1055 MethodName, isClassMethod);
1056
Gabor Greif984d0b42008-04-06 20:42:52 +00001057 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +00001058 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001059 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +00001060 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +00001061 return Method;
1062}
1063
Daniel Dunbar49f66022008-09-24 03:38:44 +00001064llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
1065 return 0;
1066}
1067
1068llvm::Function *CGObjCGNU::GetPropertySetFunction() {
1069 return 0;
1070}
1071
1072llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian26c82942009-03-30 18:02:14 +00001073 std::vector<const llvm::Type*> Params(1, IdTy);
1074 return cast<llvm::Function>(CGM.CreateRuntimeFunction(
1075 llvm::FunctionType::get(llvm::Type::VoidTy, Params, true),
1076 "objc_enumerationMutation"));
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001077}
1078
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001079void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1080 const Stmt &S) {
1081 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001082}
1083
1084void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +00001085 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001086 CGF.ErrorUnsupported(&S, "@throw statement");
1087}
1088
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001089llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001090 llvm::Value *AddrWeakObj)
1091{
1092 return 0;
1093}
1094
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001095void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1096 llvm::Value *src, llvm::Value *dst)
1097{
1098 return;
1099}
1100
Fariborz Jahanian58626502008-11-19 00:59:10 +00001101void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1102 llvm::Value *src, llvm::Value *dst)
1103{
1104 return;
1105}
1106
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001107void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1108 llvm::Value *src, llvm::Value *dst)
1109{
1110 return;
1111}
1112
Fariborz Jahanian58626502008-11-19 00:59:10 +00001113void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1114 llvm::Value *src, llvm::Value *dst)
1115{
1116 return;
1117}
1118
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001119LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1120 QualType ObjectTy,
1121 llvm::Value *BaseValue,
1122 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001123 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00001124 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00001125 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
1126 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001127}
1128
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001129llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001130 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001131 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00001132 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
Daniel Dunbar84ad77a2009-04-22 09:39:34 +00001133 return llvm::ConstantInt::get(LongTy, Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001134}
1135
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001136CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001137 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001138}