blob: c5e2538ca0a98c51a42c2f44afb5e9410537e370 [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"
Chris Lattner0f984262008-03-01 08:50:34 +000023#include "llvm/Module.h"
Chris Lattner0f984262008-03-01 08:50:34 +000024#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000025#include "llvm/ADT/StringMap.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000026#include "llvm/Support/Compiler.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000027#include "llvm/Target/TargetData.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000028#include <map>
Chris Lattnere160c9b2009-01-27 05:06:01 +000029
30
Chris Lattnerdce14062008-06-26 04:19:03 +000031using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000032using namespace CodeGen;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000033using llvm::dyn_cast;
34
35// The version of the runtime that this class targets. Must match the version
36// in the runtime.
37static const int RuntimeVersion = 8;
38static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000039
Chris Lattner0f984262008-03-01 08:50:34 +000040namespace {
Chris Lattnerdce14062008-06-26 04:19:03 +000041class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000042private:
Chris Lattnerdce14062008-06-26 04:19:03 +000043 CodeGen::CodeGenModule &CGM;
Chris Lattner0f984262008-03-01 08:50:34 +000044 llvm::Module &TheModule;
Chris Lattnere160c9b2009-01-27 05:06:01 +000045 const llvm::PointerType *SelectorTy;
46 const llvm::PointerType *PtrToInt8Ty;
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +000047 const llvm::FunctionType *IMPTy;
Chris Lattnere160c9b2009-01-27 05:06:01 +000048 const llvm::PointerType *IdTy;
49 const llvm::IntegerType *IntTy;
50 const llvm::PointerType *PtrTy;
51 const llvm::IntegerType *LongTy;
52 const llvm::PointerType *PtrToIntTy;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000053 std::vector<llvm::Constant*> Classes;
54 std::vector<llvm::Constant*> Categories;
55 std::vector<llvm::Constant*> ConstantStrings;
56 llvm::Function *LoadFunction;
57 llvm::StringMap<llvm::Constant*> ExistingProtocols;
58 typedef std::pair<std::string, std::string> TypedSelector;
59 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
60 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
61 // Some zeros used for GEPs in lots of places.
62 llvm::Constant *Zeros[2];
63 llvm::Constant *NULLPtr;
64private:
65 llvm::Constant *GenerateIvarList(
66 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
67 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
68 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
69 llvm::Constant *GenerateMethodList(const std::string &ClassName,
70 const std::string &CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +000071 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000072 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
73 bool isClassMethodList);
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +000074 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000075 llvm::Constant *GenerateProtocolList(
76 const llvm::SmallVectorImpl<std::string> &Protocols);
77 llvm::Constant *GenerateClassStructure(
78 llvm::Constant *MetaClass,
79 llvm::Constant *SuperClass,
80 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +000081 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000082 llvm::Constant *Version,
83 llvm::Constant *InstanceSize,
84 llvm::Constant *IVars,
85 llvm::Constant *Methods,
86 llvm::Constant *Protocols);
87 llvm::Constant *GenerateProtocolMethodList(
88 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
89 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
90 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
91 &Name="");
92 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
93 std::vector<llvm::Constant*> &V, const std::string &Name="");
94 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
95 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattner0f984262008-03-01 08:50:34 +000096public:
Chris Lattnerdce14062008-06-26 04:19:03 +000097 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Steve Naroff33fdb732009-03-31 16:53:37 +000098 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000099 virtual CodeGen::RValue
100 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000101 QualType ResultType,
102 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000103 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000104 bool IsClassMessage,
105 const CallArgList &CallArgs);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000106 virtual CodeGen::RValue
107 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000108 QualType ResultType,
109 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000110 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000111 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000112 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000113 bool IsClassMessage,
114 const CallArgList &CallArgs);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000115 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000116 const ObjCInterfaceDecl *OID);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000117 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000118
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000119 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
120 const ObjCContainerDecl *CD);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000121 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
122 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000123 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000124 const ObjCProtocolDecl *PD);
125 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000126 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000127 virtual llvm::Function *GetPropertyGetFunction();
128 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000129 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000130
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000131 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
132 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000133 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
134 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000135 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000136 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000137 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
138 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000139 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
140 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000141 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
142 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000143 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
144 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000145 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
146 QualType ObjectTy,
147 llvm::Value *BaseValue,
148 const ObjCIvarDecl *Ivar,
149 const FieldDecl *Field,
150 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000151 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
152 ObjCInterfaceDecl *Interface,
153 const ObjCIvarDecl *Ivar);
Chris Lattner0f984262008-03-01 08:50:34 +0000154};
155} // end anonymous namespace
156
Anton Korobeynikov20ff3102008-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 Lattnerdce14062008-06-26 04:19:03 +0000170CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
171 : CGM(cgm), TheModule(CGM.getModule()) {
Chris Lattnere160c9b2009-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 Lattnerdce14062008-06-26 04:19:03 +0000176
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000177 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000178 Zeros[1] = Zeros[0];
179 NULLPtr = llvm::ConstantPointerNull::get(
180 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-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 Lattnere160c9b2009-01-27 05:06:01 +0000185 SelectorTy = cast<llvm::PointerType>(
186 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
187
Chris Lattner391d77a2008-03-30 23:03:07 +0000188 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
189 PtrTy = PtrToInt8Ty;
190
191 // Object type
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000192 IdTy = cast<llvm::PointerType>(
193 CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType()));
Chris Lattner391d77a2008-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 Korobeynikov20ff3102008-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 Dunbar45d196b2008-11-01 01:53:16 +0000203llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000204 const ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000205 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000206 ClassName = Builder.CreateStructGEP(ClassName, 0);
207
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000208 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000209 llvm::Constant *ClassLookupFn =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000210 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
211 Params,
212 true),
213 "objc_lookup_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000214 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000215}
216
Chris Lattner8e67b632008-06-26 04:37:12 +0000217/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000218llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000219 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner077bf5e2008-11-24 03:33:13 +0000220 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner8e67b632008-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 Lattner5e7dcc62008-06-26 04:44:19 +0000231llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
232 const std::string &Name) {
Anton Korobeynikov20ff3102008-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 Naroff33fdb732009-03-31 16:53:37 +0000256llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) {
257 std::string Str(SL->getString()->getStrData(),
258 SL->getString()->getByteLength());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000259 std::vector<llvm::Constant*> Ivars;
260 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000261 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000262 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-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 Dunbar8f2926b2008-08-23 03:46:30 +0000274CodeGen::RValue
275CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000276 QualType ResultType,
277 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000278 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000279 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000280 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000281 bool IsClassMessage,
282 const CallArgList &CallArgs) {
Fariborz Jahanianb3716ef2009-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 Dunbarf56f1912008-08-25 08:19:24 +0000299 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000300 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000301 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Chris Lattner39d462e2009-04-18 23:12:40 +0000302 if (IsClassMessage)
303 ReceiverClass = CGF.Builder.CreateBitCast(CGF.Builder.CreateLoad(
304 CGF.Builder.CreateStructGEP(ReceiverClass, 0)), IdTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000305 // Construct the structure used to look up the IMP
306 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
307 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000308 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000309
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000310 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
311 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000312
313 // Get the IMP
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000314 std::vector<const llvm::Type*> Params;
315 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
316 Params.push_back(SelectorTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000317 llvm::Constant *lookupFunction =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000318 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
319 llvm::PointerType::getUnqual(impType), Params, true),
320 "objc_msg_lookup_super");
321
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000322 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000323 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000324 lookupArgs+2);
325
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000326 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000327}
328
329/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000330CodeGen::RValue
331CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000332 QualType ResultType,
333 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000334 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000335 bool IsClassMessage,
336 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000337 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000338 CallArgList ActualArgs;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000339
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000340 ActualArgs.push_back(
341 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
342 CGF.getContext().getObjCIdType()));
343 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
344 CGF.getContext().getObjCSelType()));
345 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
346
347 CodeGenTypes &Types = CGM.getTypes();
348 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
349 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
350
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000351 std::vector<const llvm::Type*> Params;
352 Params.push_back(Receiver->getType());
353 Params.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000354 llvm::Constant *lookupFunction =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000355 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
356 llvm::PointerType::getUnqual(impType), Params, true),
357 "objc_msg_lookup");
358
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000359 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000360
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000361 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000362}
363
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000364/// Generates a MethodList. Used in construction of a objc_class and
365/// objc_category structures.
366llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000367 const std::string &CategoryName,
368 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000369 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
370 bool isClassMethodList) {
371 // Get the method structure type.
372 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
373 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
374 PtrToInt8Ty, // Method types
375 llvm::PointerType::getUnqual(IMPTy), //Method pointer
376 NULL);
377 std::vector<llvm::Constant*> Methods;
378 std::vector<llvm::Constant*> Elements;
379 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
380 Elements.clear();
Chris Lattner077bf5e2008-11-24 03:33:13 +0000381 llvm::Constant *C =
382 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattnera4210072008-06-26 05:08:00 +0000383 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000384 Elements.push_back(
385 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
386 llvm::Constant *Method =
387 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000388 MethodSels[i].getAsString(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000389 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000390 Method = llvm::ConstantExpr::getBitCast(Method,
391 llvm::PointerType::getUnqual(IMPTy));
392 Elements.push_back(Method);
393 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
394 }
395
396 // Array of method structures
397 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000398 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000399 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000400 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000401
402 // Structure containing list pointer, array and array count
403 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
404 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
405 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
406 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
407 IntTy,
408 ObjCMethodArrayTy,
409 NULL);
410 // Refine next pointer type to concrete type
411 llvm::cast<llvm::OpaqueType>(
412 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
413 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
414
415 Methods.clear();
416 Methods.push_back(llvm::ConstantPointerNull::get(
417 llvm::PointerType::getUnqual(ObjCMethodListTy)));
418 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
419 MethodTypes.size()));
420 Methods.push_back(MethodArray);
421
422 // Create an instance of the structure
423 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
424}
425
426/// Generates an IvarList. Used in construction of a objc_class.
427llvm::Constant *CGObjCGNU::GenerateIvarList(
428 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
429 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
430 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
431 // Get the method structure type.
432 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
433 PtrToInt8Ty,
434 PtrToInt8Ty,
435 IntTy,
436 NULL);
437 std::vector<llvm::Constant*> Ivars;
438 std::vector<llvm::Constant*> Elements;
439 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
440 Elements.clear();
441 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
442 Zeros, 2));
443 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
444 Zeros, 2));
445 Elements.push_back(IvarOffsets[i]);
446 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
447 }
448
449 // Array of method structures
450 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
451 IvarNames.size());
452
453
454 Elements.clear();
Chris Lattnere160c9b2009-01-27 05:06:01 +0000455 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000456 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
457 // Structure containing array and array count
458 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
459 ObjCIvarArrayTy,
460 NULL);
461
462 // Create an instance of the structure
463 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
464}
465
466/// Generate a class structure
467llvm::Constant *CGObjCGNU::GenerateClassStructure(
468 llvm::Constant *MetaClass,
469 llvm::Constant *SuperClass,
470 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000471 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000472 llvm::Constant *Version,
473 llvm::Constant *InstanceSize,
474 llvm::Constant *IVars,
475 llvm::Constant *Methods,
476 llvm::Constant *Protocols) {
477 // Set up the class structure
478 // Note: Several of these are char*s when they should be ids. This is
479 // because the runtime performs this translation on load.
480 llvm::StructType *ClassTy = llvm::StructType::get(
481 PtrToInt8Ty, // class_pointer
482 PtrToInt8Ty, // super_class
483 PtrToInt8Ty, // name
484 LongTy, // version
485 LongTy, // info
486 LongTy, // instance_size
487 IVars->getType(), // ivars
488 Methods->getType(), // methods
489 // These are all filled in by the runtime, so we pretend
490 PtrTy, // dtable
491 PtrTy, // subclass_list
492 PtrTy, // sibling_class
493 PtrTy, // protocols
494 PtrTy, // gc_object_type
495 NULL);
496 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
497 llvm::Constant *NullP =
Chris Lattnere160c9b2009-01-27 05:06:01 +0000498 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000499 // Fill in the structure
500 std::vector<llvm::Constant*> Elements;
501 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
502 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000503 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000504 Elements.push_back(Zero);
505 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
506 Elements.push_back(InstanceSize);
507 Elements.push_back(IVars);
508 Elements.push_back(Methods);
509 Elements.push_back(NullP);
510 Elements.push_back(NullP);
511 Elements.push_back(NullP);
512 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
513 Elements.push_back(NullP);
514 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000515 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000516}
517
518llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
519 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
520 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
521 // Get the method structure type.
522 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
523 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
524 PtrToInt8Ty,
525 NULL);
526 std::vector<llvm::Constant*> Methods;
527 std::vector<llvm::Constant*> Elements;
528 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
529 Elements.clear();
530 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
531 Zeros, 2));
532 Elements.push_back(
533 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
534 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
535 }
536 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
537 MethodNames.size());
538 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
539 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
540 IntTy, ObjCMethodArrayTy, NULL);
541 Methods.clear();
542 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
543 Methods.push_back(Array);
544 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
545}
546// Create the protocol list structure used in classes, categories and so on
547llvm::Constant *CGObjCGNU::GenerateProtocolList(
548 const llvm::SmallVectorImpl<std::string> &Protocols) {
549 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
550 Protocols.size());
551 llvm::StructType *ProtocolListTy = llvm::StructType::get(
552 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
553 LongTy,//FIXME: Should be size_t
554 ProtocolArrayTy,
555 NULL);
556 std::vector<llvm::Constant*> Elements;
557 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
558 iter != endIter ; iter++) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000559 llvm::Constant *protocol = ExistingProtocols[*iter];
560 if (!protocol)
561 protocol = GenerateEmptyProtocol(*iter);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000562 llvm::Constant *Ptr =
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000563 llvm::ConstantExpr::getBitCast(protocol, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000564 Elements.push_back(Ptr);
565 }
566 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
567 Elements);
568 Elements.clear();
569 Elements.push_back(NULLPtr);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000570 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000571 Elements.push_back(ProtocolArray);
572 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
573}
574
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000575llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000576 const ObjCProtocolDecl *PD) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000577 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
578 const llvm::Type *T =
579 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
580 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
581}
582
583llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
584 const std::string &ProtocolName) {
585 llvm::SmallVector<std::string, 0> EmptyStringVector;
586 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
587
588 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
589 llvm::Constant *InstanceMethodList =
590 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
591 llvm::Constant *ClassMethodList =
592 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
593 // Protocols are objects containing lists of the methods implemented and
594 // protocols adopted.
595 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
596 PtrToInt8Ty,
597 ProtocolList->getType(),
598 InstanceMethodList->getType(),
599 ClassMethodList->getType(),
600 NULL);
601 std::vector<llvm::Constant*> Elements;
602 // The isa pointer must be set to a magic number so the runtime knows it's
603 // the correct layout.
604 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
605 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
606 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
607 Elements.push_back(ProtocolList);
608 Elements.push_back(InstanceMethodList);
609 Elements.push_back(ClassMethodList);
610 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000611}
612
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000613void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
614 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000615 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000616 llvm::SmallVector<std::string, 16> Protocols;
617 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
618 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000619 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000620 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
621 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000622 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(Context),
623 E = PD->instmeth_end(Context); iter != E; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000624 std::string TypeStr;
625 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
626 InstanceMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000627 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000628 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000629 }
630 // Collect information about class methods:
631 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
632 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000633 for (ObjCProtocolDecl::classmeth_iterator
634 iter = PD->classmeth_begin(Context),
635 endIter = PD->classmeth_end(Context) ; iter != endIter ; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000636 std::string TypeStr;
637 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
638 ClassMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000639 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000640 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000641 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000642
643 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
644 llvm::Constant *InstanceMethodList =
645 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
646 llvm::Constant *ClassMethodList =
647 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
648 // Protocols are objects containing lists of the methods implemented and
649 // protocols adopted.
650 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
651 PtrToInt8Ty,
652 ProtocolList->getType(),
653 InstanceMethodList->getType(),
654 ClassMethodList->getType(),
655 NULL);
656 std::vector<llvm::Constant*> Elements;
657 // The isa pointer must be set to a magic number so the runtime knows it's
658 // the correct layout.
659 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
660 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
661 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
662 Elements.push_back(ProtocolList);
663 Elements.push_back(InstanceMethodList);
664 Elements.push_back(ClassMethodList);
665 ExistingProtocols[ProtocolName] =
666 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
667 ".objc_protocol"), IdTy);
668}
669
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000670void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000671 std::string ClassName = OCD->getClassInterface()->getNameAsString();
672 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000673 // Collect information about instance methods
674 llvm::SmallVector<Selector, 16> InstanceMethodSels;
675 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000676 for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000677 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
678 InstanceMethodSels.push_back((*iter)->getSelector());
679 std::string TypeStr;
680 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
681 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
682 }
683
684 // Collect information about class methods
685 llvm::SmallVector<Selector, 16> ClassMethodSels;
686 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000687 for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000688 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
689 ClassMethodSels.push_back((*iter)->getSelector());
690 std::string TypeStr;
691 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
692 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
693 }
694
695 // Collect the names of referenced protocols
696 llvm::SmallVector<std::string, 16> Protocols;
697 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
698 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
699 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
700 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000701 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000702
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000703 std::vector<llvm::Constant*> Elements;
704 Elements.push_back(MakeConstantString(CategoryName));
705 Elements.push_back(MakeConstantString(ClassName));
706 // Instance method list
707 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000708 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000709 false), PtrTy));
710 // Class method list
711 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000712 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000713 PtrTy));
714 // Protocol list
715 Elements.push_back(llvm::ConstantExpr::getBitCast(
716 GenerateProtocolList(Protocols), PtrTy));
717 Categories.push_back(llvm::ConstantExpr::getBitCast(
718 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
719 PtrTy, PtrTy, NULL), Elements), PtrTy));
720}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000721
722void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
723 ASTContext &Context = CGM.getContext();
724
725 // Get the superclass name.
726 const ObjCInterfaceDecl * SuperClassDecl =
727 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000728 std::string SuperClassName;
729 if (SuperClassDecl)
730 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000731
732 // Get the class name
Chris Lattner09dc6662009-04-01 02:00:48 +0000733 ObjCInterfaceDecl *ClassDecl =
734 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000735 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000736
Chris Lattner09dc6662009-04-01 02:00:48 +0000737 // This is required by newer ObjC runtimes.
738 assert(!LateBoundIVars() &&"Late-bound instance variables not yet supported");
739
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000740 // Get the size of instances. For runtimes that support late-bound instances
741 // this should probably be something different (size just of instance
742 // varaibles in this class, not superclasses?).
Chris Lattner09dc6662009-04-01 02:00:48 +0000743 const llvm::Type *ObjTy =
744 CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
745 int instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000746
747 // Collect information about instance variables.
748 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
749 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
750 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
751 const llvm::StructLayout *Layout =
752 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
753 ObjTy = llvm::PointerType::getUnqual(ObjTy);
754 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
755 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
756 // Store the name
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000757 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
758 ->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000759 // Get the type encoding for this ivar
760 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000761 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000762 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
763 // Get the offset
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000764 FieldDecl *Field = ClassDecl->lookupFieldDeclForIvar(Context, (*iter));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000765 int offset =
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000766 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000767 IvarOffsets.push_back(
768 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
769 }
770
771 // Collect information about instance methods
772 llvm::SmallVector<Selector, 16> InstanceMethodSels;
773 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
774 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
775 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
776 InstanceMethodSels.push_back((*iter)->getSelector());
777 std::string TypeStr;
778 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
779 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
780 }
781
782 // Collect information about class methods
783 llvm::SmallVector<Selector, 16> ClassMethodSels;
784 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
785 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
786 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
787 ClassMethodSels.push_back((*iter)->getSelector());
788 std::string TypeStr;
789 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
790 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
791 }
792 // Collect the names of referenced protocols
793 llvm::SmallVector<std::string, 16> Protocols;
794 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
795 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
796 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000797 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000798
799
800
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000801 // Get the superclass pointer.
802 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +0000803 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000804 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
805 } else {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000806 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000807 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000808 // Empty vector used to construct empty method lists
809 llvm::SmallVector<llvm::Constant*, 1> empty;
810 // Generate the method and instance variable lists
811 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000812 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000813 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000814 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000815 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
816 IvarOffsets);
817 //Generate metaclass for class methods
818 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000819 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000820 empty, empty, empty), ClassMethodList, NULLPtr);
821 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +0000822 llvm::Constant *ClassStruct =
823 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
824 ClassName.c_str(), 0,
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000825 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000826 MethodList, GenerateProtocolList(Protocols));
827 // Add class structure to list to be added to the symtab later
828 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
829 Classes.push_back(ClassStruct);
830}
831
832llvm::Function *CGObjCGNU::ModuleInitFunction() {
833 // Only emit an ObjC load function if no Objective-C stuff has been called
834 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
835 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000836 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000837 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000838
Chris Lattnere160c9b2009-01-27 05:06:01 +0000839 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
840 SelectorTy->getElementType());
841 const llvm::Type *SelStructPtrTy = SelectorTy;
842 bool isSelOpaque = false;
843 if (SelStructTy == 0) {
844 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
845 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
846 isSelOpaque = true;
847 }
848
Eli Friedman1b8956e2008-06-01 16:00:02 +0000849 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +0000850 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +0000851 TheModule.addTypeName(".objc_id", IdTy);
852 TheModule.addTypeName(".objc_imp", IMPTy);
853
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000854 std::vector<llvm::Constant*> Elements;
855 // Generate statics list:
856 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
857 ConstantStrings.size() + 1);
858 ConstantStrings.push_back(NULLPtr);
859 Elements.push_back(MakeConstantString("NSConstantString",
860 ".objc_static_class_name"));
861 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
862 llvm::StructType *StaticsListTy =
863 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000864 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000865 llvm::Constant *Statics =
866 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000867 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000868 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000869 Elements.clear();
870 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000871 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000872 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000873 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
874 // Array of classes, categories, and constant objects
875 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
876 Classes.size() + Categories.size() + 2);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000877 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Chris Lattner630404b2008-06-26 04:10:42 +0000878 llvm::Type::Int16Ty,
879 llvm::Type::Int16Ty,
880 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000881
882 Elements.clear();
883 // Pointer to an array of selectors used in this module.
884 std::vector<llvm::Constant*> Selectors;
885 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
886 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
887 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000888 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
889 Elements.push_back(MakeConstantString(iter->first.second,
890 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000891 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
892 Elements.clear();
893 }
894 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
895 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000896 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000897 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000898 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000899 Elements.push_back(NULLPtr);
900 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
901 Elements.clear();
902 }
903 Elements.push_back(NULLPtr);
904 Elements.push_back(NULLPtr);
905 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
906 Elements.clear();
907 // Number of static selectors
908 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
909 llvm::Constant *SelectorList = MakeGlobal(
910 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
911 ".objc_selector_list");
Chris Lattnere160c9b2009-01-27 05:06:01 +0000912 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
913 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000914
915 // Now that all of the static selectors exist, create pointers to them.
916 int index = 0;
917 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
918 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
919 iter != iterEnd; ++iter) {
920 llvm::Constant *Idxs[] = {Zeros[0],
921 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000922 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
923 true, llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000924 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
925 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000926 // If selectors are defined as an opaque type, cast the pointer to this
927 // type.
928 if (isSelOpaque) {
929 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
930 llvm::PointerType::getUnqual(SelectorTy));
931 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000932 (*iter).second->setAliasee(SelPtr);
933 }
934 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
935 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
936 iter != iterEnd; iter++) {
937 llvm::Constant *Idxs[] = {Zeros[0],
938 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000939 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000940 llvm::GlobalValue::InternalLinkage,
941 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
942 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000943 // If selectors are defined as an opaque type, cast the pointer to this
944 // type.
945 if (isSelOpaque) {
946 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
947 llvm::PointerType::getUnqual(SelectorTy));
948 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000949 (*iter).second->setAliasee(SelPtr);
950 }
951 // Number of classes defined.
952 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
953 Classes.size()));
954 // Number of categories defined
955 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
956 Categories.size()));
957 // Create an array of classes, then categories, then static object instances
958 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
959 // NULL-terminated list of static object instances (mainly constant strings)
960 Classes.push_back(Statics);
961 Classes.push_back(NULLPtr);
962 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
963 Elements.push_back(ClassList);
964 // Construct the symbol table
965 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
966
967 // The symbol table is contained in a module which has some version-checking
968 // constants
969 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
970 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
971 Elements.clear();
972 // Runtime version used for compatibility checking.
973 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahanian91a0b512009-04-01 19:49:42 +0000974 // sizeof(ModuleTy)
975 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
976 Elements.push_back(llvm::ConstantInt::get(LongTy, td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000977 //FIXME: Should be the path to the file where this module was declared
978 Elements.push_back(NULLPtr);
979 Elements.push_back(SymTab);
980 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
981
982 // Create the load function calling the runtime entry point with the module
983 // structure
984 std::vector<const llvm::Type*> VoidArgs;
985 llvm::Function * LoadFunction = llvm::Function::Create(
986 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
987 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
988 &TheModule);
989 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000990 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000991 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000992
993 std::vector<const llvm::Type*> Params(1,
994 llvm::PointerType::getUnqual(ModuleTy));
995 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
996 llvm::Type::VoidTy, Params, true), "__objc_exec_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000997 Builder.CreateCall(Register, Module);
998 Builder.CreateRetVoid();
999 return LoadFunction;
1000}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001001
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001002llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
1003 const ObjCContainerDecl *CD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001004 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +00001005 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +00001006 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1007 std::string ClassName = OMD->getClassInterface()->getNameAsString();
1008 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001009 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001010
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001011 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001012 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001013 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001014 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1015 MethodName, isClassMethod);
1016
Gabor Greif984d0b42008-04-06 20:42:52 +00001017 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +00001018 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001019 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +00001020 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +00001021 return Method;
1022}
1023
Daniel Dunbar49f66022008-09-24 03:38:44 +00001024llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
1025 return 0;
1026}
1027
1028llvm::Function *CGObjCGNU::GetPropertySetFunction() {
1029 return 0;
1030}
1031
1032llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian26c82942009-03-30 18:02:14 +00001033 std::vector<const llvm::Type*> Params(1, IdTy);
1034 return cast<llvm::Function>(CGM.CreateRuntimeFunction(
1035 llvm::FunctionType::get(llvm::Type::VoidTy, Params, true),
1036 "objc_enumerationMutation"));
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001037}
1038
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001039void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1040 const Stmt &S) {
1041 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001042}
1043
1044void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +00001045 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001046 CGF.ErrorUnsupported(&S, "@throw statement");
1047}
1048
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001049llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001050 llvm::Value *AddrWeakObj)
1051{
1052 return 0;
1053}
1054
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001055void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1056 llvm::Value *src, llvm::Value *dst)
1057{
1058 return;
1059}
1060
Fariborz Jahanian58626502008-11-19 00:59:10 +00001061void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1062 llvm::Value *src, llvm::Value *dst)
1063{
1064 return;
1065}
1066
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001067void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1068 llvm::Value *src, llvm::Value *dst)
1069{
1070 return;
1071}
1072
Fariborz Jahanian58626502008-11-19 00:59:10 +00001073void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1074 llvm::Value *src, llvm::Value *dst)
1075{
1076 return;
1077}
1078
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001079LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1080 QualType ObjectTy,
1081 llvm::Value *BaseValue,
1082 const ObjCIvarDecl *Ivar,
1083 const FieldDecl *Field,
1084 unsigned CVRQualifiers) {
1085 if (Ivar->isBitField())
1086 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
1087 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001088 // TODO: Add a special case for isa (index 0)
1089 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
1090 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001091 LValue LV = LValue::MakeAddr(V,
1092 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
1093 LValue::SetObjCIvar(LV, true);
1094 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001095}
1096
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001097llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
1098 ObjCInterfaceDecl *Interface,
1099 const ObjCIvarDecl *Ivar) {
1100 const llvm::Type *InterfaceLTy =
1101 CGM.getTypes().ConvertType(
1102 CGM.getContext().getObjCInterfaceType(Interface));
1103 const llvm::StructLayout *Layout =
1104 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
1105 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
1106 uint64_t Offset =
1107 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
1108
1109 return llvm::ConstantInt::get(
1110 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
1111 Offset);
1112}
1113
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001114CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001115 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001116}