blob: f7936fda55417603b5e930d7a16e662f80e9586e [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 Lattner33b3bc92009-04-19 01:33:30 +0000302
303
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000304 // Construct the structure used to look up the IMP
305 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
306 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000307 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000308
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000309 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
310 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000311
312 // Get the IMP
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000313 std::vector<const llvm::Type*> Params;
314 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
315 Params.push_back(SelectorTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000316 llvm::Constant *lookupFunction =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000317 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
318 llvm::PointerType::getUnqual(impType), Params, true),
319 "objc_msg_lookup_super");
320
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000321 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000322 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000323 lookupArgs+2);
324
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000325 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000326}
327
328/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000329CodeGen::RValue
330CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000331 QualType ResultType,
332 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000333 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000334 bool IsClassMessage,
335 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000336 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000337 CallArgList ActualArgs;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000338
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000339 ActualArgs.push_back(
340 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
341 CGF.getContext().getObjCIdType()));
342 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
343 CGF.getContext().getObjCSelType()));
344 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
345
346 CodeGenTypes &Types = CGM.getTypes();
347 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
348 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
349
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000350 std::vector<const llvm::Type*> Params;
351 Params.push_back(Receiver->getType());
352 Params.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000353 llvm::Constant *lookupFunction =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000354 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
355 llvm::PointerType::getUnqual(impType), Params, true),
356 "objc_msg_lookup");
357
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000358 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000359
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000360 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000361}
362
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000363/// Generates a MethodList. Used in construction of a objc_class and
364/// objc_category structures.
365llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000366 const std::string &CategoryName,
367 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000368 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
369 bool isClassMethodList) {
370 // Get the method structure type.
371 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
372 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
373 PtrToInt8Ty, // Method types
374 llvm::PointerType::getUnqual(IMPTy), //Method pointer
375 NULL);
376 std::vector<llvm::Constant*> Methods;
377 std::vector<llvm::Constant*> Elements;
378 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
379 Elements.clear();
Chris Lattner077bf5e2008-11-24 03:33:13 +0000380 llvm::Constant *C =
381 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattnera4210072008-06-26 05:08:00 +0000382 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000383 Elements.push_back(
384 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
385 llvm::Constant *Method =
386 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000387 MethodSels[i].getAsString(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000388 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000389 Method = llvm::ConstantExpr::getBitCast(Method,
390 llvm::PointerType::getUnqual(IMPTy));
391 Elements.push_back(Method);
392 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
393 }
394
395 // Array of method structures
396 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000397 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000398 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000399 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000400
401 // Structure containing list pointer, array and array count
402 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
403 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
404 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
405 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
406 IntTy,
407 ObjCMethodArrayTy,
408 NULL);
409 // Refine next pointer type to concrete type
410 llvm::cast<llvm::OpaqueType>(
411 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
412 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
413
414 Methods.clear();
415 Methods.push_back(llvm::ConstantPointerNull::get(
416 llvm::PointerType::getUnqual(ObjCMethodListTy)));
417 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
418 MethodTypes.size()));
419 Methods.push_back(MethodArray);
420
421 // Create an instance of the structure
422 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
423}
424
425/// Generates an IvarList. Used in construction of a objc_class.
426llvm::Constant *CGObjCGNU::GenerateIvarList(
427 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
428 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
429 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
430 // Get the method structure type.
431 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
432 PtrToInt8Ty,
433 PtrToInt8Ty,
434 IntTy,
435 NULL);
436 std::vector<llvm::Constant*> Ivars;
437 std::vector<llvm::Constant*> Elements;
438 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
439 Elements.clear();
440 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
441 Zeros, 2));
442 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
443 Zeros, 2));
444 Elements.push_back(IvarOffsets[i]);
445 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
446 }
447
448 // Array of method structures
449 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
450 IvarNames.size());
451
452
453 Elements.clear();
Chris Lattnere160c9b2009-01-27 05:06:01 +0000454 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000455 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
456 // Structure containing array and array count
457 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
458 ObjCIvarArrayTy,
459 NULL);
460
461 // Create an instance of the structure
462 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
463}
464
465/// Generate a class structure
466llvm::Constant *CGObjCGNU::GenerateClassStructure(
467 llvm::Constant *MetaClass,
468 llvm::Constant *SuperClass,
469 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000470 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000471 llvm::Constant *Version,
472 llvm::Constant *InstanceSize,
473 llvm::Constant *IVars,
474 llvm::Constant *Methods,
475 llvm::Constant *Protocols) {
476 // Set up the class structure
477 // Note: Several of these are char*s when they should be ids. This is
478 // because the runtime performs this translation on load.
479 llvm::StructType *ClassTy = llvm::StructType::get(
480 PtrToInt8Ty, // class_pointer
481 PtrToInt8Ty, // super_class
482 PtrToInt8Ty, // name
483 LongTy, // version
484 LongTy, // info
485 LongTy, // instance_size
486 IVars->getType(), // ivars
487 Methods->getType(), // methods
488 // These are all filled in by the runtime, so we pretend
489 PtrTy, // dtable
490 PtrTy, // subclass_list
491 PtrTy, // sibling_class
492 PtrTy, // protocols
493 PtrTy, // gc_object_type
494 NULL);
495 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
496 llvm::Constant *NullP =
Chris Lattnere160c9b2009-01-27 05:06:01 +0000497 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000498 // Fill in the structure
499 std::vector<llvm::Constant*> Elements;
500 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
501 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000502 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000503 Elements.push_back(Zero);
504 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
505 Elements.push_back(InstanceSize);
506 Elements.push_back(IVars);
507 Elements.push_back(Methods);
508 Elements.push_back(NullP);
509 Elements.push_back(NullP);
510 Elements.push_back(NullP);
511 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
512 Elements.push_back(NullP);
513 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000514 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000515}
516
517llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
518 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
519 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
520 // Get the method structure type.
521 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
522 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
523 PtrToInt8Ty,
524 NULL);
525 std::vector<llvm::Constant*> Methods;
526 std::vector<llvm::Constant*> Elements;
527 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
528 Elements.clear();
529 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
530 Zeros, 2));
531 Elements.push_back(
532 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
533 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
534 }
535 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
536 MethodNames.size());
537 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
538 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
539 IntTy, ObjCMethodArrayTy, NULL);
540 Methods.clear();
541 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
542 Methods.push_back(Array);
543 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
544}
545// Create the protocol list structure used in classes, categories and so on
546llvm::Constant *CGObjCGNU::GenerateProtocolList(
547 const llvm::SmallVectorImpl<std::string> &Protocols) {
548 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
549 Protocols.size());
550 llvm::StructType *ProtocolListTy = llvm::StructType::get(
551 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
552 LongTy,//FIXME: Should be size_t
553 ProtocolArrayTy,
554 NULL);
555 std::vector<llvm::Constant*> Elements;
556 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
557 iter != endIter ; iter++) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000558 llvm::Constant *protocol = ExistingProtocols[*iter];
559 if (!protocol)
560 protocol = GenerateEmptyProtocol(*iter);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000561 llvm::Constant *Ptr =
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000562 llvm::ConstantExpr::getBitCast(protocol, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000563 Elements.push_back(Ptr);
564 }
565 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
566 Elements);
567 Elements.clear();
568 Elements.push_back(NULLPtr);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000569 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000570 Elements.push_back(ProtocolArray);
571 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
572}
573
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000574llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000575 const ObjCProtocolDecl *PD) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000576 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
577 const llvm::Type *T =
578 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
579 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
580}
581
582llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
583 const std::string &ProtocolName) {
584 llvm::SmallVector<std::string, 0> EmptyStringVector;
585 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
586
587 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
588 llvm::Constant *InstanceMethodList =
589 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
590 llvm::Constant *ClassMethodList =
591 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
592 // Protocols are objects containing lists of the methods implemented and
593 // protocols adopted.
594 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
595 PtrToInt8Ty,
596 ProtocolList->getType(),
597 InstanceMethodList->getType(),
598 ClassMethodList->getType(),
599 NULL);
600 std::vector<llvm::Constant*> Elements;
601 // The isa pointer must be set to a magic number so the runtime knows it's
602 // the correct layout.
603 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
604 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
605 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
606 Elements.push_back(ProtocolList);
607 Elements.push_back(InstanceMethodList);
608 Elements.push_back(ClassMethodList);
609 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000610}
611
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000612void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
613 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000614 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000615 llvm::SmallVector<std::string, 16> Protocols;
616 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
617 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000618 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000619 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
620 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000621 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(Context),
622 E = PD->instmeth_end(Context); iter != E; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000623 std::string TypeStr;
624 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
625 InstanceMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000626 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000627 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000628 }
629 // Collect information about class methods:
630 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
631 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000632 for (ObjCProtocolDecl::classmeth_iterator
633 iter = PD->classmeth_begin(Context),
634 endIter = PD->classmeth_end(Context) ; iter != endIter ; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000635 std::string TypeStr;
636 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
637 ClassMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000638 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000639 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000640 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000641
642 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
643 llvm::Constant *InstanceMethodList =
644 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
645 llvm::Constant *ClassMethodList =
646 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
647 // Protocols are objects containing lists of the methods implemented and
648 // protocols adopted.
649 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
650 PtrToInt8Ty,
651 ProtocolList->getType(),
652 InstanceMethodList->getType(),
653 ClassMethodList->getType(),
654 NULL);
655 std::vector<llvm::Constant*> Elements;
656 // The isa pointer must be set to a magic number so the runtime knows it's
657 // the correct layout.
658 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
659 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
660 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
661 Elements.push_back(ProtocolList);
662 Elements.push_back(InstanceMethodList);
663 Elements.push_back(ClassMethodList);
664 ExistingProtocols[ProtocolName] =
665 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
666 ".objc_protocol"), IdTy);
667}
668
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000669void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000670 std::string ClassName = OCD->getClassInterface()->getNameAsString();
671 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000672 // Collect information about instance methods
673 llvm::SmallVector<Selector, 16> InstanceMethodSels;
674 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000675 for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000676 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
677 InstanceMethodSels.push_back((*iter)->getSelector());
678 std::string TypeStr;
679 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
680 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
681 }
682
683 // Collect information about class methods
684 llvm::SmallVector<Selector, 16> ClassMethodSels;
685 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000686 for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000687 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
688 ClassMethodSels.push_back((*iter)->getSelector());
689 std::string TypeStr;
690 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
691 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
692 }
693
694 // Collect the names of referenced protocols
695 llvm::SmallVector<std::string, 16> Protocols;
696 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
697 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
698 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
699 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000700 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000701
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000702 std::vector<llvm::Constant*> Elements;
703 Elements.push_back(MakeConstantString(CategoryName));
704 Elements.push_back(MakeConstantString(ClassName));
705 // Instance method list
706 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000707 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000708 false), PtrTy));
709 // Class method list
710 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000711 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000712 PtrTy));
713 // Protocol list
714 Elements.push_back(llvm::ConstantExpr::getBitCast(
715 GenerateProtocolList(Protocols), PtrTy));
716 Categories.push_back(llvm::ConstantExpr::getBitCast(
717 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
718 PtrTy, PtrTy, NULL), Elements), PtrTy));
719}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000720
721void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
722 ASTContext &Context = CGM.getContext();
723
724 // Get the superclass name.
725 const ObjCInterfaceDecl * SuperClassDecl =
726 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000727 std::string SuperClassName;
728 if (SuperClassDecl)
729 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000730
731 // Get the class name
Chris Lattner09dc6662009-04-01 02:00:48 +0000732 ObjCInterfaceDecl *ClassDecl =
733 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000734 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000735
Chris Lattner09dc6662009-04-01 02:00:48 +0000736 // This is required by newer ObjC runtimes.
737 assert(!LateBoundIVars() &&"Late-bound instance variables not yet supported");
738
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000739 // Get the size of instances. For runtimes that support late-bound instances
740 // this should probably be something different (size just of instance
741 // varaibles in this class, not superclasses?).
Chris Lattnerb7b58b12009-04-19 06:02:28 +0000742 const llvm::Type *ObjTy;
743
744 if (ClassDecl->isForwardDecl())
745 ObjTy = llvm::StructType::get(NULL, NULL);
746 else
747 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
Chris Lattner09dc6662009-04-01 02:00:48 +0000748 int instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000749
750 // Collect information about instance variables.
751 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
752 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
753 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
754 const llvm::StructLayout *Layout =
755 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
756 ObjTy = llvm::PointerType::getUnqual(ObjTy);
757 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
758 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
759 // Store the name
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000760 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
761 ->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000762 // Get the type encoding for this ivar
763 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000764 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000765 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
766 // Get the offset
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000767 FieldDecl *Field = ClassDecl->lookupFieldDeclForIvar(Context, (*iter));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000768 int offset =
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000769 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000770 IvarOffsets.push_back(
771 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
772 }
773
774 // Collect information about instance methods
775 llvm::SmallVector<Selector, 16> InstanceMethodSels;
776 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
777 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
778 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
779 InstanceMethodSels.push_back((*iter)->getSelector());
780 std::string TypeStr;
781 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
782 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
783 }
784
785 // Collect information about class methods
786 llvm::SmallVector<Selector, 16> ClassMethodSels;
787 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
788 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
789 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
790 ClassMethodSels.push_back((*iter)->getSelector());
791 std::string TypeStr;
792 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
793 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
794 }
795 // Collect the names of referenced protocols
796 llvm::SmallVector<std::string, 16> Protocols;
797 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
798 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
799 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000800 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000801
802
803
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000804 // Get the superclass pointer.
805 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +0000806 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000807 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
808 } else {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000809 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000810 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000811 // Empty vector used to construct empty method lists
812 llvm::SmallVector<llvm::Constant*, 1> empty;
813 // Generate the method and instance variable lists
814 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000815 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000816 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000817 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000818 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
819 IvarOffsets);
820 //Generate metaclass for class methods
821 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000822 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000823 empty, empty, empty), ClassMethodList, NULLPtr);
824 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +0000825 llvm::Constant *ClassStruct =
826 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
827 ClassName.c_str(), 0,
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000828 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000829 MethodList, GenerateProtocolList(Protocols));
830 // Add class structure to list to be added to the symtab later
831 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
832 Classes.push_back(ClassStruct);
833}
834
835llvm::Function *CGObjCGNU::ModuleInitFunction() {
836 // Only emit an ObjC load function if no Objective-C stuff has been called
837 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
838 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000839 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000840 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000841
Chris Lattnere160c9b2009-01-27 05:06:01 +0000842 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
843 SelectorTy->getElementType());
844 const llvm::Type *SelStructPtrTy = SelectorTy;
845 bool isSelOpaque = false;
846 if (SelStructTy == 0) {
847 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
848 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
849 isSelOpaque = true;
850 }
851
Eli Friedman1b8956e2008-06-01 16:00:02 +0000852 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +0000853 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +0000854 TheModule.addTypeName(".objc_id", IdTy);
855 TheModule.addTypeName(".objc_imp", IMPTy);
856
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000857 std::vector<llvm::Constant*> Elements;
858 // Generate statics list:
859 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
860 ConstantStrings.size() + 1);
861 ConstantStrings.push_back(NULLPtr);
862 Elements.push_back(MakeConstantString("NSConstantString",
863 ".objc_static_class_name"));
864 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
865 llvm::StructType *StaticsListTy =
866 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000867 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000868 llvm::Constant *Statics =
869 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000870 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000871 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000872 Elements.clear();
873 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000874 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000875 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000876 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
877 // Array of classes, categories, and constant objects
878 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
879 Classes.size() + Categories.size() + 2);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000880 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Chris Lattner630404b2008-06-26 04:10:42 +0000881 llvm::Type::Int16Ty,
882 llvm::Type::Int16Ty,
883 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000884
885 Elements.clear();
886 // Pointer to an array of selectors used in this module.
887 std::vector<llvm::Constant*> Selectors;
888 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
889 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
890 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000891 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
892 Elements.push_back(MakeConstantString(iter->first.second,
893 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000894 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
895 Elements.clear();
896 }
897 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
898 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000899 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000900 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000901 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000902 Elements.push_back(NULLPtr);
903 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
904 Elements.clear();
905 }
906 Elements.push_back(NULLPtr);
907 Elements.push_back(NULLPtr);
908 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
909 Elements.clear();
910 // Number of static selectors
911 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
912 llvm::Constant *SelectorList = MakeGlobal(
913 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
914 ".objc_selector_list");
Chris Lattnere160c9b2009-01-27 05:06:01 +0000915 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
916 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000917
918 // Now that all of the static selectors exist, create pointers to them.
919 int index = 0;
920 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
921 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
922 iter != iterEnd; ++iter) {
923 llvm::Constant *Idxs[] = {Zeros[0],
924 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000925 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
926 true, llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000927 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
928 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000929 // If selectors are defined as an opaque type, cast the pointer to this
930 // type.
931 if (isSelOpaque) {
932 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
933 llvm::PointerType::getUnqual(SelectorTy));
934 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000935 (*iter).second->setAliasee(SelPtr);
936 }
937 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
938 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
939 iter != iterEnd; iter++) {
940 llvm::Constant *Idxs[] = {Zeros[0],
941 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000942 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000943 llvm::GlobalValue::InternalLinkage,
944 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
945 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000946 // If selectors are defined as an opaque type, cast the pointer to this
947 // type.
948 if (isSelOpaque) {
949 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
950 llvm::PointerType::getUnqual(SelectorTy));
951 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000952 (*iter).second->setAliasee(SelPtr);
953 }
954 // Number of classes defined.
955 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
956 Classes.size()));
957 // Number of categories defined
958 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
959 Categories.size()));
960 // Create an array of classes, then categories, then static object instances
961 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
962 // NULL-terminated list of static object instances (mainly constant strings)
963 Classes.push_back(Statics);
964 Classes.push_back(NULLPtr);
965 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
966 Elements.push_back(ClassList);
967 // Construct the symbol table
968 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
969
970 // The symbol table is contained in a module which has some version-checking
971 // constants
972 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
973 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
974 Elements.clear();
975 // Runtime version used for compatibility checking.
976 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahanian91a0b512009-04-01 19:49:42 +0000977 // sizeof(ModuleTy)
978 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
979 Elements.push_back(llvm::ConstantInt::get(LongTy, td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000980 //FIXME: Should be the path to the file where this module was declared
981 Elements.push_back(NULLPtr);
982 Elements.push_back(SymTab);
983 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
984
985 // Create the load function calling the runtime entry point with the module
986 // structure
987 std::vector<const llvm::Type*> VoidArgs;
988 llvm::Function * LoadFunction = llvm::Function::Create(
989 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
990 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
991 &TheModule);
992 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000993 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000994 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000995
996 std::vector<const llvm::Type*> Params(1,
997 llvm::PointerType::getUnqual(ModuleTy));
998 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
999 llvm::Type::VoidTy, Params, true), "__objc_exec_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001000 Builder.CreateCall(Register, Module);
1001 Builder.CreateRetVoid();
1002 return LoadFunction;
1003}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001004
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001005llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
1006 const ObjCContainerDecl *CD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001007 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +00001008 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +00001009 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1010 std::string ClassName = OMD->getClassInterface()->getNameAsString();
1011 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001012 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001013
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001014 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001015 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001016 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001017 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1018 MethodName, isClassMethod);
1019
Gabor Greif984d0b42008-04-06 20:42:52 +00001020 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +00001021 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001022 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +00001023 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +00001024 return Method;
1025}
1026
Daniel Dunbar49f66022008-09-24 03:38:44 +00001027llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
1028 return 0;
1029}
1030
1031llvm::Function *CGObjCGNU::GetPropertySetFunction() {
1032 return 0;
1033}
1034
1035llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian26c82942009-03-30 18:02:14 +00001036 std::vector<const llvm::Type*> Params(1, IdTy);
1037 return cast<llvm::Function>(CGM.CreateRuntimeFunction(
1038 llvm::FunctionType::get(llvm::Type::VoidTy, Params, true),
1039 "objc_enumerationMutation"));
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001040}
1041
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001042void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1043 const Stmt &S) {
1044 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001045}
1046
1047void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +00001048 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001049 CGF.ErrorUnsupported(&S, "@throw statement");
1050}
1051
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001052llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001053 llvm::Value *AddrWeakObj)
1054{
1055 return 0;
1056}
1057
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001058void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1059 llvm::Value *src, llvm::Value *dst)
1060{
1061 return;
1062}
1063
Fariborz Jahanian58626502008-11-19 00:59:10 +00001064void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1065 llvm::Value *src, llvm::Value *dst)
1066{
1067 return;
1068}
1069
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001070void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1071 llvm::Value *src, llvm::Value *dst)
1072{
1073 return;
1074}
1075
Fariborz Jahanian58626502008-11-19 00:59:10 +00001076void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1077 llvm::Value *src, llvm::Value *dst)
1078{
1079 return;
1080}
1081
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001082LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1083 QualType ObjectTy,
1084 llvm::Value *BaseValue,
1085 const ObjCIvarDecl *Ivar,
1086 const FieldDecl *Field,
1087 unsigned CVRQualifiers) {
1088 if (Ivar->isBitField())
1089 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
1090 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001091 // TODO: Add a special case for isa (index 0)
1092 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
1093 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001094 LValue LV = LValue::MakeAddr(V,
1095 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
1096 LValue::SetObjCIvar(LV, true);
1097 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001098}
1099
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001100llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
1101 ObjCInterfaceDecl *Interface,
1102 const ObjCIvarDecl *Ivar) {
1103 const llvm::Type *InterfaceLTy =
1104 CGM.getTypes().ConvertType(
1105 CGM.getContext().getObjCInterfaceType(Interface));
1106 const llvm::StructLayout *Layout =
1107 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
1108 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
1109 uint64_t Offset =
1110 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
1111
1112 return llvm::ConstantInt::get(
1113 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
1114 Offset);
1115}
1116
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001117CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001118 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001119}