blob: 7877baaf3ecf942c741d7e83bed5171f18d87cca [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);
74 llvm::Constant *GenerateProtocolList(
75 const llvm::SmallVectorImpl<std::string> &Protocols);
76 llvm::Constant *GenerateClassStructure(
77 llvm::Constant *MetaClass,
78 llvm::Constant *SuperClass,
79 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +000080 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000081 llvm::Constant *Version,
82 llvm::Constant *InstanceSize,
83 llvm::Constant *IVars,
84 llvm::Constant *Methods,
85 llvm::Constant *Protocols);
86 llvm::Constant *GenerateProtocolMethodList(
87 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
88 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
89 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
90 &Name="");
91 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
92 std::vector<llvm::Constant*> &V, const std::string &Name="");
93 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
94 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattner0f984262008-03-01 08:50:34 +000095public:
Chris Lattnerdce14062008-06-26 04:19:03 +000096 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000097 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000098 virtual CodeGen::RValue
99 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000100 QualType ResultType,
101 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000102 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000103 bool IsClassMessage,
104 const CallArgList &CallArgs);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000105 virtual CodeGen::RValue
106 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000107 QualType ResultType,
108 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000109 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000110 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000111 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000112 bool IsClassMessage,
113 const CallArgList &CallArgs);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000114 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000115 const ObjCInterfaceDecl *OID);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000116 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000117
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000118 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
119 const ObjCContainerDecl *CD);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000120 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
121 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000122 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000123 const ObjCProtocolDecl *PD);
124 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000125 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000126 virtual llvm::Function *GetPropertyGetFunction();
127 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000128 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000129
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000130 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
131 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000132 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
133 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000134 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000135 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000136 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
137 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000138 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
139 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000140 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
141 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000142 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
143 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000144 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
145 QualType ObjectTy,
146 llvm::Value *BaseValue,
147 const ObjCIvarDecl *Ivar,
148 const FieldDecl *Field,
149 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000150 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
151 ObjCInterfaceDecl *Interface,
152 const ObjCIvarDecl *Ivar);
Chris Lattner0f984262008-03-01 08:50:34 +0000153};
154} // end anonymous namespace
155
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000156
157
158static std::string SymbolNameForClass(const std::string &ClassName) {
159 return ".objc_class_" + ClassName;
160}
161
162static std::string SymbolNameForMethod(const std::string &ClassName, const
163 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
164{
165 return "._objc_method_" + ClassName +"("+CategoryName+")"+
166 (isClassMethod ? "+" : "-") + MethodName;
167}
168
Chris Lattnerdce14062008-06-26 04:19:03 +0000169CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
170 : CGM(cgm), TheModule(CGM.getModule()) {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000171 IntTy = cast<llvm::IntegerType>(
172 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
173 LongTy = cast<llvm::IntegerType>(
174 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Chris Lattnerdce14062008-06-26 04:19:03 +0000175
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000176 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000177 Zeros[1] = Zeros[0];
178 NULLPtr = llvm::ConstantPointerNull::get(
179 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000180 // C string type. Used in lots of places.
181 PtrToInt8Ty =
182 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
183 // Get the selector Type.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000184 SelectorTy = cast<llvm::PointerType>(
185 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
186
Chris Lattner391d77a2008-03-30 23:03:07 +0000187 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
188 PtrTy = PtrToInt8Ty;
189
190 // Object type
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000191 IdTy = cast<llvm::PointerType>(
192 CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType()));
Chris Lattner391d77a2008-03-30 23:03:07 +0000193
194 // IMP type
195 std::vector<const llvm::Type*> IMPArgs;
196 IMPArgs.push_back(IdTy);
197 IMPArgs.push_back(SelectorTy);
198 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000199}
200// This has to perform the lookup every time, since posing and related
201// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000202llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000203 const ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000204 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000205 ClassName = Builder.CreateStructGEP(ClassName, 0);
206
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000207 llvm::Constant *ClassLookupFn =
208 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
209 NULL);
210 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000211}
212
Chris Lattner8e67b632008-06-26 04:37:12 +0000213/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000214llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000215 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner077bf5e2008-11-24 03:33:13 +0000216 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner8e67b632008-06-26 04:37:12 +0000217 if (US == 0)
218 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
219 llvm::GlobalValue::InternalLinkage,
220 ".objc_untyped_selector_alias",
221 NULL, &TheModule);
222
223 return Builder.CreateLoad(US);
224
225}
226
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000227llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
228 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000229 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
230 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
231 llvm::GlobalValue::InternalLinkage,
232 ConstStr, Name, &TheModule);
233 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
234}
235llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
236 std::vector<llvm::Constant*> &V, const std::string &Name) {
237 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
238 return new llvm::GlobalVariable(Ty, false,
239 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
240}
241llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
242 std::vector<llvm::Constant*> &V, const std::string &Name) {
243 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
244 return new llvm::GlobalVariable(Ty, false,
245 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
246}
247
248/// Generate an NSConstantString object.
249//TODO: In case there are any crazy people still using the GNU runtime without
250//an OpenStep implementation, this should let them select their own class for
251//constant strings.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000252llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000253 std::vector<llvm::Constant*> Ivars;
254 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000255 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000256 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000257 llvm::Constant *ObjCStr = MakeGlobal(
258 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
259 Ivars, ".objc_str");
260 ConstantStrings.push_back(
261 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
262 return ObjCStr;
263}
264
265///Generates a message send where the super is the receiver. This is a message
266///send to self with special delivery semantics indicating which class's method
267///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000268CodeGen::RValue
269CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000270 QualType ResultType,
271 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000272 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000273 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000274 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000275 bool IsClassMessage,
276 const CallArgList &CallArgs) {
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000277 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
278
279 CallArgList ActualArgs;
280
281 ActualArgs.push_back(
282 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
283 CGF.getContext().getObjCIdType()));
284 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
285 CGF.getContext().getObjCSelType()));
286 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
287
288 CodeGenTypes &Types = CGM.getTypes();
289 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
290 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
291
292
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000293 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000294 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000295 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000296
297
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000298 // Construct the structure used to look up the IMP
299 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
300 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000301 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000302
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000303 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
304 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000305
306 // Get the IMP
307 llvm::Constant *lookupFunction =
308 TheModule.getOrInsertFunction("objc_msg_lookup_super",
309 llvm::PointerType::getUnqual(impType),
310 llvm::PointerType::getUnqual(ObjCSuperTy),
311 SelectorTy, NULL);
312 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000313 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000314 lookupArgs+2);
315
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000316 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000317}
318
319/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000320CodeGen::RValue
321CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000322 QualType ResultType,
323 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000324 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000325 bool IsClassMessage,
326 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000327 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000328 CallArgList ActualArgs;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000329
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000330 ActualArgs.push_back(
331 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
332 CGF.getContext().getObjCIdType()));
333 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
334 CGF.getContext().getObjCSelType()));
335 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
336
337 CodeGenTypes &Types = CGM.getTypes();
338 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
339 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
340
Chris Lattner0f984262008-03-01 08:50:34 +0000341 llvm::Constant *lookupFunction =
342 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000343 llvm::PointerType::getUnqual(impType),
344 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000345 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000346
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000347 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000348}
349
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000350/// Generates a MethodList. Used in construction of a objc_class and
351/// objc_category structures.
352llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000353 const std::string &CategoryName,
354 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000355 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
356 bool isClassMethodList) {
357 // Get the method structure type.
358 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
359 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
360 PtrToInt8Ty, // Method types
361 llvm::PointerType::getUnqual(IMPTy), //Method pointer
362 NULL);
363 std::vector<llvm::Constant*> Methods;
364 std::vector<llvm::Constant*> Elements;
365 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
366 Elements.clear();
Chris Lattner077bf5e2008-11-24 03:33:13 +0000367 llvm::Constant *C =
368 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattnera4210072008-06-26 05:08:00 +0000369 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000370 Elements.push_back(
371 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
372 llvm::Constant *Method =
373 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000374 MethodSels[i].getAsString(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000375 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000376 Method = llvm::ConstantExpr::getBitCast(Method,
377 llvm::PointerType::getUnqual(IMPTy));
378 Elements.push_back(Method);
379 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
380 }
381
382 // Array of method structures
383 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000384 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000385 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000386 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000387
388 // Structure containing list pointer, array and array count
389 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
390 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
391 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
392 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
393 IntTy,
394 ObjCMethodArrayTy,
395 NULL);
396 // Refine next pointer type to concrete type
397 llvm::cast<llvm::OpaqueType>(
398 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
399 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
400
401 Methods.clear();
402 Methods.push_back(llvm::ConstantPointerNull::get(
403 llvm::PointerType::getUnqual(ObjCMethodListTy)));
404 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
405 MethodTypes.size()));
406 Methods.push_back(MethodArray);
407
408 // Create an instance of the structure
409 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
410}
411
412/// Generates an IvarList. Used in construction of a objc_class.
413llvm::Constant *CGObjCGNU::GenerateIvarList(
414 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
415 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
416 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
417 // Get the method structure type.
418 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
419 PtrToInt8Ty,
420 PtrToInt8Ty,
421 IntTy,
422 NULL);
423 std::vector<llvm::Constant*> Ivars;
424 std::vector<llvm::Constant*> Elements;
425 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
426 Elements.clear();
427 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
428 Zeros, 2));
429 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
430 Zeros, 2));
431 Elements.push_back(IvarOffsets[i]);
432 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
433 }
434
435 // Array of method structures
436 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
437 IvarNames.size());
438
439
440 Elements.clear();
Chris Lattnere160c9b2009-01-27 05:06:01 +0000441 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000442 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
443 // Structure containing array and array count
444 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
445 ObjCIvarArrayTy,
446 NULL);
447
448 // Create an instance of the structure
449 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
450}
451
452/// Generate a class structure
453llvm::Constant *CGObjCGNU::GenerateClassStructure(
454 llvm::Constant *MetaClass,
455 llvm::Constant *SuperClass,
456 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000457 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000458 llvm::Constant *Version,
459 llvm::Constant *InstanceSize,
460 llvm::Constant *IVars,
461 llvm::Constant *Methods,
462 llvm::Constant *Protocols) {
463 // Set up the class structure
464 // Note: Several of these are char*s when they should be ids. This is
465 // because the runtime performs this translation on load.
466 llvm::StructType *ClassTy = llvm::StructType::get(
467 PtrToInt8Ty, // class_pointer
468 PtrToInt8Ty, // super_class
469 PtrToInt8Ty, // name
470 LongTy, // version
471 LongTy, // info
472 LongTy, // instance_size
473 IVars->getType(), // ivars
474 Methods->getType(), // methods
475 // These are all filled in by the runtime, so we pretend
476 PtrTy, // dtable
477 PtrTy, // subclass_list
478 PtrTy, // sibling_class
479 PtrTy, // protocols
480 PtrTy, // gc_object_type
481 NULL);
482 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
483 llvm::Constant *NullP =
Chris Lattnere160c9b2009-01-27 05:06:01 +0000484 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000485 // Fill in the structure
486 std::vector<llvm::Constant*> Elements;
487 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
488 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000489 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000490 Elements.push_back(Zero);
491 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
492 Elements.push_back(InstanceSize);
493 Elements.push_back(IVars);
494 Elements.push_back(Methods);
495 Elements.push_back(NullP);
496 Elements.push_back(NullP);
497 Elements.push_back(NullP);
498 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
499 Elements.push_back(NullP);
500 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000501 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000502}
503
504llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
505 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
506 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
507 // Get the method structure type.
508 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
509 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
510 PtrToInt8Ty,
511 NULL);
512 std::vector<llvm::Constant*> Methods;
513 std::vector<llvm::Constant*> Elements;
514 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
515 Elements.clear();
516 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
517 Zeros, 2));
518 Elements.push_back(
519 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
520 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
521 }
522 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
523 MethodNames.size());
524 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
525 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
526 IntTy, ObjCMethodArrayTy, NULL);
527 Methods.clear();
528 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
529 Methods.push_back(Array);
530 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
531}
532// Create the protocol list structure used in classes, categories and so on
533llvm::Constant *CGObjCGNU::GenerateProtocolList(
534 const llvm::SmallVectorImpl<std::string> &Protocols) {
535 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
536 Protocols.size());
537 llvm::StructType *ProtocolListTy = llvm::StructType::get(
538 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
539 LongTy,//FIXME: Should be size_t
540 ProtocolArrayTy,
541 NULL);
542 std::vector<llvm::Constant*> Elements;
543 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
544 iter != endIter ; iter++) {
545 llvm::Constant *Ptr =
546 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
547 Elements.push_back(Ptr);
548 }
549 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
550 Elements);
551 Elements.clear();
552 Elements.push_back(NULLPtr);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000553 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000554 Elements.push_back(ProtocolArray);
555 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
556}
557
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000558llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000559 const ObjCProtocolDecl *PD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000560 return ExistingProtocols[PD->getNameAsString()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000561}
562
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000563void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
564 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000565 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000566 llvm::SmallVector<std::string, 16> Protocols;
567 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
568 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000569 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000570 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
571 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
572 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
573 E = PD->instmeth_end(); iter != E; iter++) {
574 std::string TypeStr;
575 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
576 InstanceMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000577 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000578 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000579 }
580 // Collect information about class methods:
581 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
582 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
583 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
584 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
585 std::string TypeStr;
586 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
587 ClassMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000588 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000589 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000590 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000591
592 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
593 llvm::Constant *InstanceMethodList =
594 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
595 llvm::Constant *ClassMethodList =
596 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
597 // Protocols are objects containing lists of the methods implemented and
598 // protocols adopted.
599 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
600 PtrToInt8Ty,
601 ProtocolList->getType(),
602 InstanceMethodList->getType(),
603 ClassMethodList->getType(),
604 NULL);
605 std::vector<llvm::Constant*> Elements;
606 // The isa pointer must be set to a magic number so the runtime knows it's
607 // the correct layout.
608 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
609 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
610 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
611 Elements.push_back(ProtocolList);
612 Elements.push_back(InstanceMethodList);
613 Elements.push_back(ClassMethodList);
614 ExistingProtocols[ProtocolName] =
615 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
616 ".objc_protocol"), IdTy);
617}
618
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000619void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000620 std::string ClassName = OCD->getClassInterface()->getNameAsString();
621 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000622 // Collect information about instance methods
623 llvm::SmallVector<Selector, 16> InstanceMethodSels;
624 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000625 for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000626 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
627 InstanceMethodSels.push_back((*iter)->getSelector());
628 std::string TypeStr;
629 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
630 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
631 }
632
633 // Collect information about class methods
634 llvm::SmallVector<Selector, 16> ClassMethodSels;
635 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000636 for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000637 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
638 ClassMethodSels.push_back((*iter)->getSelector());
639 std::string TypeStr;
640 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
641 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
642 }
643
644 // Collect the names of referenced protocols
645 llvm::SmallVector<std::string, 16> Protocols;
646 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
647 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
648 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
649 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000650 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000651
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000652 std::vector<llvm::Constant*> Elements;
653 Elements.push_back(MakeConstantString(CategoryName));
654 Elements.push_back(MakeConstantString(ClassName));
655 // Instance method list
656 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000657 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000658 false), PtrTy));
659 // Class method list
660 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000661 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000662 PtrTy));
663 // Protocol list
664 Elements.push_back(llvm::ConstantExpr::getBitCast(
665 GenerateProtocolList(Protocols), PtrTy));
666 Categories.push_back(llvm::ConstantExpr::getBitCast(
667 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
668 PtrTy, PtrTy, NULL), Elements), PtrTy));
669}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000670
671void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
672 ASTContext &Context = CGM.getContext();
673
674 // Get the superclass name.
675 const ObjCInterfaceDecl * SuperClassDecl =
676 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000677 std::string SuperClassName;
678 if (SuperClassDecl)
679 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000680
681 // Get the class name
682 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000683 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000684
685 // Get the size of instances. For runtimes that support late-bound instances
686 // this should probably be something different (size just of instance
687 // varaibles in this class, not superclasses?).
688 int instanceSize = 0;
689 const llvm::Type *ObjTy = 0;
690 if (!LateBoundIVars()) {
691 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000692 instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000693 } else {
694 // This is required by newer ObjC runtimes.
695 assert(0 && "Late-bound instance variables not yet supported");
696 }
697
698 // Collect information about instance variables.
699 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
700 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
701 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
702 const llvm::StructLayout *Layout =
703 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
704 ObjTy = llvm::PointerType::getUnqual(ObjTy);
705 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
706 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
707 // Store the name
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000708 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
709 ->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000710 // Get the type encoding for this ivar
711 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000712 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000713 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
714 // Get the offset
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000715 FieldDecl *Field = ClassDecl->lookupFieldDeclForIvar(Context, (*iter));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000716 int offset =
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000717 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000718 IvarOffsets.push_back(
719 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
720 }
721
722 // Collect information about instance methods
723 llvm::SmallVector<Selector, 16> InstanceMethodSels;
724 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
725 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
726 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
727 InstanceMethodSels.push_back((*iter)->getSelector());
728 std::string TypeStr;
729 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
730 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
731 }
732
733 // Collect information about class methods
734 llvm::SmallVector<Selector, 16> ClassMethodSels;
735 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
736 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
737 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
738 ClassMethodSels.push_back((*iter)->getSelector());
739 std::string TypeStr;
740 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
741 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
742 }
743 // Collect the names of referenced protocols
744 llvm::SmallVector<std::string, 16> Protocols;
745 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
746 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
747 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000748 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000749
750
751
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000752 // Get the superclass pointer.
753 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +0000754 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000755 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
756 } else {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000757 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000758 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000759 // Empty vector used to construct empty method lists
760 llvm::SmallVector<llvm::Constant*, 1> empty;
761 // Generate the method and instance variable lists
762 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000763 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000764 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000765 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000766 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
767 IvarOffsets);
768 //Generate metaclass for class methods
769 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000770 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000771 empty, empty, empty), ClassMethodList, NULLPtr);
772 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +0000773 llvm::Constant *ClassStruct =
774 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
775 ClassName.c_str(), 0,
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000776 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000777 MethodList, GenerateProtocolList(Protocols));
778 // Add class structure to list to be added to the symtab later
779 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
780 Classes.push_back(ClassStruct);
781}
782
783llvm::Function *CGObjCGNU::ModuleInitFunction() {
784 // Only emit an ObjC load function if no Objective-C stuff has been called
785 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
786 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000787 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000788 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000789
Chris Lattnere160c9b2009-01-27 05:06:01 +0000790 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
791 SelectorTy->getElementType());
792 const llvm::Type *SelStructPtrTy = SelectorTy;
793 bool isSelOpaque = false;
794 if (SelStructTy == 0) {
795 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
796 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
797 isSelOpaque = true;
798 }
799
Eli Friedman1b8956e2008-06-01 16:00:02 +0000800 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +0000801 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +0000802 TheModule.addTypeName(".objc_id", IdTy);
803 TheModule.addTypeName(".objc_imp", IMPTy);
804
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000805 std::vector<llvm::Constant*> Elements;
806 // Generate statics list:
807 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
808 ConstantStrings.size() + 1);
809 ConstantStrings.push_back(NULLPtr);
810 Elements.push_back(MakeConstantString("NSConstantString",
811 ".objc_static_class_name"));
812 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
813 llvm::StructType *StaticsListTy =
814 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000815 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000816 llvm::Constant *Statics =
817 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000818 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000819 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000820 Elements.clear();
821 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000822 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000823 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000824 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
825 // Array of classes, categories, and constant objects
826 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
827 Classes.size() + Categories.size() + 2);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000828 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Chris Lattner630404b2008-06-26 04:10:42 +0000829 llvm::Type::Int16Ty,
830 llvm::Type::Int16Ty,
831 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000832
833 Elements.clear();
834 // Pointer to an array of selectors used in this module.
835 std::vector<llvm::Constant*> Selectors;
836 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
837 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
838 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000839 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
840 Elements.push_back(MakeConstantString(iter->first.second,
841 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000842 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
843 Elements.clear();
844 }
845 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
846 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000847 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000848 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000849 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000850 Elements.push_back(NULLPtr);
851 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
852 Elements.clear();
853 }
854 Elements.push_back(NULLPtr);
855 Elements.push_back(NULLPtr);
856 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
857 Elements.clear();
858 // Number of static selectors
859 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
860 llvm::Constant *SelectorList = MakeGlobal(
861 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
862 ".objc_selector_list");
Chris Lattnere160c9b2009-01-27 05:06:01 +0000863 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
864 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000865
866 // Now that all of the static selectors exist, create pointers to them.
867 int index = 0;
868 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
869 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
870 iter != iterEnd; ++iter) {
871 llvm::Constant *Idxs[] = {Zeros[0],
872 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000873 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
874 true, llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000875 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
876 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000877 // If selectors are defined as an opaque type, cast the pointer to this
878 // type.
879 if (isSelOpaque) {
880 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
881 llvm::PointerType::getUnqual(SelectorTy));
882 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000883 (*iter).second->setAliasee(SelPtr);
884 }
885 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
886 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
887 iter != iterEnd; iter++) {
888 llvm::Constant *Idxs[] = {Zeros[0],
889 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000890 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000891 llvm::GlobalValue::InternalLinkage,
892 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
893 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000894 // If selectors are defined as an opaque type, cast the pointer to this
895 // type.
896 if (isSelOpaque) {
897 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
898 llvm::PointerType::getUnqual(SelectorTy));
899 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000900 (*iter).second->setAliasee(SelPtr);
901 }
902 // Number of classes defined.
903 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
904 Classes.size()));
905 // Number of categories defined
906 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
907 Categories.size()));
908 // Create an array of classes, then categories, then static object instances
909 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
910 // NULL-terminated list of static object instances (mainly constant strings)
911 Classes.push_back(Statics);
912 Classes.push_back(NULLPtr);
913 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
914 Elements.push_back(ClassList);
915 // Construct the symbol table
916 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
917
918 // The symbol table is contained in a module which has some version-checking
919 // constants
920 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
921 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
922 Elements.clear();
923 // Runtime version used for compatibility checking.
924 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
925 //FIXME: Should be sizeof(ModuleTy)
926 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
927 //FIXME: Should be the path to the file where this module was declared
928 Elements.push_back(NULLPtr);
929 Elements.push_back(SymTab);
930 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
931
932 // Create the load function calling the runtime entry point with the module
933 // structure
934 std::vector<const llvm::Type*> VoidArgs;
935 llvm::Function * LoadFunction = llvm::Function::Create(
936 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
937 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
938 &TheModule);
939 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000940 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000941 Builder.SetInsertPoint(EntryBB);
942 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
943 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
944 Builder.CreateCall(Register, Module);
945 Builder.CreateRetVoid();
946 return LoadFunction;
947}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000948
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000949llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
950 const ObjCContainerDecl *CD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000951 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +0000952 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +0000953 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
954 std::string ClassName = OMD->getClassInterface()->getNameAsString();
955 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000956 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000957
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000958 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000959 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000960 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000961 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
962 MethodName, isClassMethod);
963
Gabor Greif984d0b42008-04-06 20:42:52 +0000964 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000965 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000966 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000967 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000968 return Method;
969}
970
Daniel Dunbar49f66022008-09-24 03:38:44 +0000971llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
972 return 0;
973}
974
975llvm::Function *CGObjCGNU::GetPropertySetFunction() {
976 return 0;
977}
978
979llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian660af742009-02-03 21:52:35 +0000980return
981 (llvm::Function*)TheModule.getOrInsertFunction(
982 "objc_enumerationMutation", llvm::Type::VoidTy, IdTy, NULL);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000983}
984
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000985void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
986 const Stmt &S) {
987 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000988}
989
990void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000991 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000992 CGF.ErrorUnsupported(&S, "@throw statement");
993}
994
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000995llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000996 llvm::Value *AddrWeakObj)
997{
998 return 0;
999}
1000
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001001void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1002 llvm::Value *src, llvm::Value *dst)
1003{
1004 return;
1005}
1006
Fariborz Jahanian58626502008-11-19 00:59:10 +00001007void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1008 llvm::Value *src, llvm::Value *dst)
1009{
1010 return;
1011}
1012
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001013void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1014 llvm::Value *src, llvm::Value *dst)
1015{
1016 return;
1017}
1018
Fariborz Jahanian58626502008-11-19 00:59:10 +00001019void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1020 llvm::Value *src, llvm::Value *dst)
1021{
1022 return;
1023}
1024
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001025LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1026 QualType ObjectTy,
1027 llvm::Value *BaseValue,
1028 const ObjCIvarDecl *Ivar,
1029 const FieldDecl *Field,
1030 unsigned CVRQualifiers) {
1031 if (Ivar->isBitField())
1032 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
1033 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001034 // TODO: Add a special case for isa (index 0)
1035 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
1036 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001037 LValue LV = LValue::MakeAddr(V,
1038 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
1039 LValue::SetObjCIvar(LV, true);
1040 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001041}
1042
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001043llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
1044 ObjCInterfaceDecl *Interface,
1045 const ObjCIvarDecl *Ivar) {
1046 const llvm::Type *InterfaceLTy =
1047 CGM.getTypes().ConvertType(
1048 CGM.getContext().getObjCInterfaceType(Interface));
1049 const llvm::StructLayout *Layout =
1050 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
1051 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
1052 uint64_t Offset =
1053 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
1054
1055 return llvm::ConstantInt::get(
1056 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
1057 Offset);
1058}
1059
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001060CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001061 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001062}