blob: bc713bd8cc29e4d3b04002e1a10b544a23601d92 [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);
Steve Naroff33fdb732009-03-31 16:53:37 +000097 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *);
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
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000207 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000208 llvm::Constant *ClassLookupFn =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000209 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
210 Params,
211 true),
212 "objc_lookup_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000213 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000214}
215
Chris Lattner8e67b632008-06-26 04:37:12 +0000216/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000217llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000218 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner077bf5e2008-11-24 03:33:13 +0000219 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner8e67b632008-06-26 04:37:12 +0000220 if (US == 0)
221 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
222 llvm::GlobalValue::InternalLinkage,
223 ".objc_untyped_selector_alias",
224 NULL, &TheModule);
225
226 return Builder.CreateLoad(US);
227
228}
229
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000230llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
231 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000232 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
233 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
234 llvm::GlobalValue::InternalLinkage,
235 ConstStr, Name, &TheModule);
236 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
237}
238llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
239 std::vector<llvm::Constant*> &V, const std::string &Name) {
240 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
241 return new llvm::GlobalVariable(Ty, false,
242 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
243}
244llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
245 std::vector<llvm::Constant*> &V, const std::string &Name) {
246 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
247 return new llvm::GlobalVariable(Ty, false,
248 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
249}
250
251/// Generate an NSConstantString object.
252//TODO: In case there are any crazy people still using the GNU runtime without
253//an OpenStep implementation, this should let them select their own class for
254//constant strings.
Steve Naroff33fdb732009-03-31 16:53:37 +0000255llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) {
256 std::string Str(SL->getString()->getStrData(),
257 SL->getString()->getByteLength());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000258 std::vector<llvm::Constant*> Ivars;
259 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000260 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000261 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000262 llvm::Constant *ObjCStr = MakeGlobal(
263 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
264 Ivars, ".objc_str");
265 ConstantStrings.push_back(
266 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
267 return ObjCStr;
268}
269
270///Generates a message send where the super is the receiver. This is a message
271///send to self with special delivery semantics indicating which class's method
272///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000273CodeGen::RValue
274CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000275 QualType ResultType,
276 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000277 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000278 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000279 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000280 bool IsClassMessage,
281 const CallArgList &CallArgs) {
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000282 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
283
284 CallArgList ActualArgs;
285
286 ActualArgs.push_back(
287 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
288 CGF.getContext().getObjCIdType()));
289 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
290 CGF.getContext().getObjCSelType()));
291 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
292
293 CodeGenTypes &Types = CGM.getTypes();
294 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
295 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
296
297
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000298 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000299 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000300 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000301
302
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000303 // Construct the structure used to look up the IMP
304 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
305 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000306 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000307
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000308 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
309 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000310
311 // Get the IMP
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000312 std::vector<const llvm::Type*> Params;
313 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
314 Params.push_back(SelectorTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000315 llvm::Constant *lookupFunction =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000316 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
317 llvm::PointerType::getUnqual(impType), Params, true),
318 "objc_msg_lookup_super");
319
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000320 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000321 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000322 lookupArgs+2);
323
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000324 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000325}
326
327/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000328CodeGen::RValue
329CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000330 QualType ResultType,
331 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000332 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000333 bool IsClassMessage,
334 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000335 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000336 CallArgList ActualArgs;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000337
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000338 ActualArgs.push_back(
339 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
340 CGF.getContext().getObjCIdType()));
341 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
342 CGF.getContext().getObjCSelType()));
343 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
344
345 CodeGenTypes &Types = CGM.getTypes();
346 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
347 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
348
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000349 std::vector<const llvm::Type*> Params;
350 Params.push_back(Receiver->getType());
351 Params.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000352 llvm::Constant *lookupFunction =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000353 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
354 llvm::PointerType::getUnqual(impType), Params, true),
355 "objc_msg_lookup");
356
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000357 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000358
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000359 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000360}
361
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000362/// Generates a MethodList. Used in construction of a objc_class and
363/// objc_category structures.
364llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000365 const std::string &CategoryName,
366 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000367 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
368 bool isClassMethodList) {
369 // Get the method structure type.
370 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
371 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
372 PtrToInt8Ty, // Method types
373 llvm::PointerType::getUnqual(IMPTy), //Method pointer
374 NULL);
375 std::vector<llvm::Constant*> Methods;
376 std::vector<llvm::Constant*> Elements;
377 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
378 Elements.clear();
Chris Lattner077bf5e2008-11-24 03:33:13 +0000379 llvm::Constant *C =
380 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattnera4210072008-06-26 05:08:00 +0000381 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000382 Elements.push_back(
383 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
384 llvm::Constant *Method =
385 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000386 MethodSels[i].getAsString(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000387 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000388 Method = llvm::ConstantExpr::getBitCast(Method,
389 llvm::PointerType::getUnqual(IMPTy));
390 Elements.push_back(Method);
391 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
392 }
393
394 // Array of method structures
395 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000396 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000397 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000398 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000399
400 // Structure containing list pointer, array and array count
401 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
402 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
403 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
404 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
405 IntTy,
406 ObjCMethodArrayTy,
407 NULL);
408 // Refine next pointer type to concrete type
409 llvm::cast<llvm::OpaqueType>(
410 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
411 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
412
413 Methods.clear();
414 Methods.push_back(llvm::ConstantPointerNull::get(
415 llvm::PointerType::getUnqual(ObjCMethodListTy)));
416 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
417 MethodTypes.size()));
418 Methods.push_back(MethodArray);
419
420 // Create an instance of the structure
421 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
422}
423
424/// Generates an IvarList. Used in construction of a objc_class.
425llvm::Constant *CGObjCGNU::GenerateIvarList(
426 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
427 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
428 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
429 // Get the method structure type.
430 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
431 PtrToInt8Ty,
432 PtrToInt8Ty,
433 IntTy,
434 NULL);
435 std::vector<llvm::Constant*> Ivars;
436 std::vector<llvm::Constant*> Elements;
437 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
438 Elements.clear();
439 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
440 Zeros, 2));
441 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
442 Zeros, 2));
443 Elements.push_back(IvarOffsets[i]);
444 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
445 }
446
447 // Array of method structures
448 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
449 IvarNames.size());
450
451
452 Elements.clear();
Chris Lattnere160c9b2009-01-27 05:06:01 +0000453 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000454 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
455 // Structure containing array and array count
456 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
457 ObjCIvarArrayTy,
458 NULL);
459
460 // Create an instance of the structure
461 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
462}
463
464/// Generate a class structure
465llvm::Constant *CGObjCGNU::GenerateClassStructure(
466 llvm::Constant *MetaClass,
467 llvm::Constant *SuperClass,
468 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000469 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000470 llvm::Constant *Version,
471 llvm::Constant *InstanceSize,
472 llvm::Constant *IVars,
473 llvm::Constant *Methods,
474 llvm::Constant *Protocols) {
475 // Set up the class structure
476 // Note: Several of these are char*s when they should be ids. This is
477 // because the runtime performs this translation on load.
478 llvm::StructType *ClassTy = llvm::StructType::get(
479 PtrToInt8Ty, // class_pointer
480 PtrToInt8Ty, // super_class
481 PtrToInt8Ty, // name
482 LongTy, // version
483 LongTy, // info
484 LongTy, // instance_size
485 IVars->getType(), // ivars
486 Methods->getType(), // methods
487 // These are all filled in by the runtime, so we pretend
488 PtrTy, // dtable
489 PtrTy, // subclass_list
490 PtrTy, // sibling_class
491 PtrTy, // protocols
492 PtrTy, // gc_object_type
493 NULL);
494 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
495 llvm::Constant *NullP =
Chris Lattnere160c9b2009-01-27 05:06:01 +0000496 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000497 // Fill in the structure
498 std::vector<llvm::Constant*> Elements;
499 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
500 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000501 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000502 Elements.push_back(Zero);
503 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
504 Elements.push_back(InstanceSize);
505 Elements.push_back(IVars);
506 Elements.push_back(Methods);
507 Elements.push_back(NullP);
508 Elements.push_back(NullP);
509 Elements.push_back(NullP);
510 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
511 Elements.push_back(NullP);
512 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000513 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000514}
515
516llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
517 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
518 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
519 // Get the method structure type.
520 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
521 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
522 PtrToInt8Ty,
523 NULL);
524 std::vector<llvm::Constant*> Methods;
525 std::vector<llvm::Constant*> Elements;
526 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
527 Elements.clear();
528 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
529 Zeros, 2));
530 Elements.push_back(
531 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
532 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
533 }
534 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
535 MethodNames.size());
536 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
537 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
538 IntTy, ObjCMethodArrayTy, NULL);
539 Methods.clear();
540 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
541 Methods.push_back(Array);
542 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
543}
544// Create the protocol list structure used in classes, categories and so on
545llvm::Constant *CGObjCGNU::GenerateProtocolList(
546 const llvm::SmallVectorImpl<std::string> &Protocols) {
547 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
548 Protocols.size());
549 llvm::StructType *ProtocolListTy = llvm::StructType::get(
550 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
551 LongTy,//FIXME: Should be size_t
552 ProtocolArrayTy,
553 NULL);
554 std::vector<llvm::Constant*> Elements;
555 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
556 iter != endIter ; iter++) {
557 llvm::Constant *Ptr =
558 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
559 Elements.push_back(Ptr);
560 }
561 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
562 Elements);
563 Elements.clear();
564 Elements.push_back(NULLPtr);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000565 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000566 Elements.push_back(ProtocolArray);
567 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
568}
569
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000570llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000571 const ObjCProtocolDecl *PD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000572 return ExistingProtocols[PD->getNameAsString()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000573}
574
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000575void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
576 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000577 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000578 llvm::SmallVector<std::string, 16> Protocols;
579 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
580 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000581 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000582 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
583 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
584 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
585 E = PD->instmeth_end(); iter != E; iter++) {
586 std::string TypeStr;
587 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
588 InstanceMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000589 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000590 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000591 }
592 // Collect information about class methods:
593 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
594 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
595 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
596 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
597 std::string TypeStr;
598 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
599 ClassMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000600 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000601 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000602 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000603
604 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
605 llvm::Constant *InstanceMethodList =
606 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
607 llvm::Constant *ClassMethodList =
608 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
609 // Protocols are objects containing lists of the methods implemented and
610 // protocols adopted.
611 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
612 PtrToInt8Ty,
613 ProtocolList->getType(),
614 InstanceMethodList->getType(),
615 ClassMethodList->getType(),
616 NULL);
617 std::vector<llvm::Constant*> Elements;
618 // The isa pointer must be set to a magic number so the runtime knows it's
619 // the correct layout.
620 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
621 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
622 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
623 Elements.push_back(ProtocolList);
624 Elements.push_back(InstanceMethodList);
625 Elements.push_back(ClassMethodList);
626 ExistingProtocols[ProtocolName] =
627 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
628 ".objc_protocol"), IdTy);
629}
630
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000631void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000632 std::string ClassName = OCD->getClassInterface()->getNameAsString();
633 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000634 // Collect information about instance methods
635 llvm::SmallVector<Selector, 16> InstanceMethodSels;
636 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000637 for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000638 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
639 InstanceMethodSels.push_back((*iter)->getSelector());
640 std::string TypeStr;
641 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
642 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
643 }
644
645 // Collect information about class methods
646 llvm::SmallVector<Selector, 16> ClassMethodSels;
647 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000648 for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000649 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
650 ClassMethodSels.push_back((*iter)->getSelector());
651 std::string TypeStr;
652 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
653 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
654 }
655
656 // Collect the names of referenced protocols
657 llvm::SmallVector<std::string, 16> Protocols;
658 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
659 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
660 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
661 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000662 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000663
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000664 std::vector<llvm::Constant*> Elements;
665 Elements.push_back(MakeConstantString(CategoryName));
666 Elements.push_back(MakeConstantString(ClassName));
667 // Instance method list
668 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000669 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000670 false), PtrTy));
671 // Class method list
672 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000673 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000674 PtrTy));
675 // Protocol list
676 Elements.push_back(llvm::ConstantExpr::getBitCast(
677 GenerateProtocolList(Protocols), PtrTy));
678 Categories.push_back(llvm::ConstantExpr::getBitCast(
679 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
680 PtrTy, PtrTy, NULL), Elements), PtrTy));
681}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000682
683void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
684 ASTContext &Context = CGM.getContext();
685
686 // Get the superclass name.
687 const ObjCInterfaceDecl * SuperClassDecl =
688 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000689 std::string SuperClassName;
690 if (SuperClassDecl)
691 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000692
693 // Get the class name
694 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000695 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000696
697 // Get the size of instances. For runtimes that support late-bound instances
698 // this should probably be something different (size just of instance
699 // varaibles in this class, not superclasses?).
700 int instanceSize = 0;
701 const llvm::Type *ObjTy = 0;
702 if (!LateBoundIVars()) {
703 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000704 instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000705 } else {
706 // This is required by newer ObjC runtimes.
707 assert(0 && "Late-bound instance variables not yet supported");
708 }
709
710 // Collect information about instance variables.
711 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
712 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
713 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
714 const llvm::StructLayout *Layout =
715 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
716 ObjTy = llvm::PointerType::getUnqual(ObjTy);
717 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
718 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
719 // Store the name
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000720 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
721 ->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000722 // Get the type encoding for this ivar
723 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000724 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000725 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
726 // Get the offset
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000727 FieldDecl *Field = ClassDecl->lookupFieldDeclForIvar(Context, (*iter));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000728 int offset =
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000729 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000730 IvarOffsets.push_back(
731 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
732 }
733
734 // Collect information about instance methods
735 llvm::SmallVector<Selector, 16> InstanceMethodSels;
736 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
737 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
738 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
739 InstanceMethodSels.push_back((*iter)->getSelector());
740 std::string TypeStr;
741 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
742 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
743 }
744
745 // Collect information about class methods
746 llvm::SmallVector<Selector, 16> ClassMethodSels;
747 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
748 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
749 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
750 ClassMethodSels.push_back((*iter)->getSelector());
751 std::string TypeStr;
752 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
753 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
754 }
755 // Collect the names of referenced protocols
756 llvm::SmallVector<std::string, 16> Protocols;
757 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
758 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
759 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000760 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000761
762
763
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000764 // Get the superclass pointer.
765 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +0000766 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000767 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
768 } else {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000769 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000770 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000771 // Empty vector used to construct empty method lists
772 llvm::SmallVector<llvm::Constant*, 1> empty;
773 // Generate the method and instance variable lists
774 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000775 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000776 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000777 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000778 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
779 IvarOffsets);
780 //Generate metaclass for class methods
781 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000782 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000783 empty, empty, empty), ClassMethodList, NULLPtr);
784 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +0000785 llvm::Constant *ClassStruct =
786 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
787 ClassName.c_str(), 0,
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000788 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000789 MethodList, GenerateProtocolList(Protocols));
790 // Add class structure to list to be added to the symtab later
791 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
792 Classes.push_back(ClassStruct);
793}
794
795llvm::Function *CGObjCGNU::ModuleInitFunction() {
796 // Only emit an ObjC load function if no Objective-C stuff has been called
797 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
798 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000799 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000800 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000801
Chris Lattnere160c9b2009-01-27 05:06:01 +0000802 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
803 SelectorTy->getElementType());
804 const llvm::Type *SelStructPtrTy = SelectorTy;
805 bool isSelOpaque = false;
806 if (SelStructTy == 0) {
807 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
808 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
809 isSelOpaque = true;
810 }
811
Eli Friedman1b8956e2008-06-01 16:00:02 +0000812 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +0000813 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +0000814 TheModule.addTypeName(".objc_id", IdTy);
815 TheModule.addTypeName(".objc_imp", IMPTy);
816
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000817 std::vector<llvm::Constant*> Elements;
818 // Generate statics list:
819 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
820 ConstantStrings.size() + 1);
821 ConstantStrings.push_back(NULLPtr);
822 Elements.push_back(MakeConstantString("NSConstantString",
823 ".objc_static_class_name"));
824 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
825 llvm::StructType *StaticsListTy =
826 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000827 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000828 llvm::Constant *Statics =
829 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000830 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000831 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000832 Elements.clear();
833 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000834 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000835 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000836 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
837 // Array of classes, categories, and constant objects
838 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
839 Classes.size() + Categories.size() + 2);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000840 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Chris Lattner630404b2008-06-26 04:10:42 +0000841 llvm::Type::Int16Ty,
842 llvm::Type::Int16Ty,
843 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000844
845 Elements.clear();
846 // Pointer to an array of selectors used in this module.
847 std::vector<llvm::Constant*> Selectors;
848 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
849 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
850 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000851 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
852 Elements.push_back(MakeConstantString(iter->first.second,
853 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000854 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
855 Elements.clear();
856 }
857 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
858 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000859 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000860 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000861 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000862 Elements.push_back(NULLPtr);
863 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
864 Elements.clear();
865 }
866 Elements.push_back(NULLPtr);
867 Elements.push_back(NULLPtr);
868 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
869 Elements.clear();
870 // Number of static selectors
871 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
872 llvm::Constant *SelectorList = MakeGlobal(
873 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
874 ".objc_selector_list");
Chris Lattnere160c9b2009-01-27 05:06:01 +0000875 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
876 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000877
878 // Now that all of the static selectors exist, create pointers to them.
879 int index = 0;
880 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
881 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
882 iter != iterEnd; ++iter) {
883 llvm::Constant *Idxs[] = {Zeros[0],
884 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000885 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
886 true, llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000887 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
888 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000889 // If selectors are defined as an opaque type, cast the pointer to this
890 // type.
891 if (isSelOpaque) {
892 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
893 llvm::PointerType::getUnqual(SelectorTy));
894 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000895 (*iter).second->setAliasee(SelPtr);
896 }
897 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
898 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
899 iter != iterEnd; iter++) {
900 llvm::Constant *Idxs[] = {Zeros[0],
901 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000902 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000903 llvm::GlobalValue::InternalLinkage,
904 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
905 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000906 // If selectors are defined as an opaque type, cast the pointer to this
907 // type.
908 if (isSelOpaque) {
909 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
910 llvm::PointerType::getUnqual(SelectorTy));
911 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000912 (*iter).second->setAliasee(SelPtr);
913 }
914 // Number of classes defined.
915 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
916 Classes.size()));
917 // Number of categories defined
918 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
919 Categories.size()));
920 // Create an array of classes, then categories, then static object instances
921 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
922 // NULL-terminated list of static object instances (mainly constant strings)
923 Classes.push_back(Statics);
924 Classes.push_back(NULLPtr);
925 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
926 Elements.push_back(ClassList);
927 // Construct the symbol table
928 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
929
930 // The symbol table is contained in a module which has some version-checking
931 // constants
932 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
933 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
934 Elements.clear();
935 // Runtime version used for compatibility checking.
936 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
937 //FIXME: Should be sizeof(ModuleTy)
938 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
939 //FIXME: Should be the path to the file where this module was declared
940 Elements.push_back(NULLPtr);
941 Elements.push_back(SymTab);
942 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
943
944 // Create the load function calling the runtime entry point with the module
945 // structure
946 std::vector<const llvm::Type*> VoidArgs;
947 llvm::Function * LoadFunction = llvm::Function::Create(
948 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
949 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
950 &TheModule);
951 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000952 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000953 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000954
955 std::vector<const llvm::Type*> Params(1,
956 llvm::PointerType::getUnqual(ModuleTy));
957 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
958 llvm::Type::VoidTy, Params, true), "__objc_exec_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000959 Builder.CreateCall(Register, Module);
960 Builder.CreateRetVoid();
961 return LoadFunction;
962}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000963
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000964llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
965 const ObjCContainerDecl *CD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000966 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +0000967 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +0000968 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
969 std::string ClassName = OMD->getClassInterface()->getNameAsString();
970 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000971 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000972
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000973 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000974 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000975 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000976 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
977 MethodName, isClassMethod);
978
Gabor Greif984d0b42008-04-06 20:42:52 +0000979 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000980 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000981 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000982 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000983 return Method;
984}
985
Daniel Dunbar49f66022008-09-24 03:38:44 +0000986llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
987 return 0;
988}
989
990llvm::Function *CGObjCGNU::GetPropertySetFunction() {
991 return 0;
992}
993
994llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000995 std::vector<const llvm::Type*> Params(1, IdTy);
996 return cast<llvm::Function>(CGM.CreateRuntimeFunction(
997 llvm::FunctionType::get(llvm::Type::VoidTy, Params, true),
998 "objc_enumerationMutation"));
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000999}
1000
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001001void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1002 const Stmt &S) {
1003 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001004}
1005
1006void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +00001007 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001008 CGF.ErrorUnsupported(&S, "@throw statement");
1009}
1010
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001011llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001012 llvm::Value *AddrWeakObj)
1013{
1014 return 0;
1015}
1016
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001017void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1018 llvm::Value *src, llvm::Value *dst)
1019{
1020 return;
1021}
1022
Fariborz Jahanian58626502008-11-19 00:59:10 +00001023void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1024 llvm::Value *src, llvm::Value *dst)
1025{
1026 return;
1027}
1028
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001029void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1030 llvm::Value *src, llvm::Value *dst)
1031{
1032 return;
1033}
1034
Fariborz Jahanian58626502008-11-19 00:59:10 +00001035void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1036 llvm::Value *src, llvm::Value *dst)
1037{
1038 return;
1039}
1040
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001041LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1042 QualType ObjectTy,
1043 llvm::Value *BaseValue,
1044 const ObjCIvarDecl *Ivar,
1045 const FieldDecl *Field,
1046 unsigned CVRQualifiers) {
1047 if (Ivar->isBitField())
1048 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
1049 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001050 // TODO: Add a special case for isa (index 0)
1051 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
1052 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001053 LValue LV = LValue::MakeAddr(V,
1054 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
1055 LValue::SetObjCIvar(LV, true);
1056 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001057}
1058
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001059llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
1060 ObjCInterfaceDecl *Interface,
1061 const ObjCIvarDecl *Ivar) {
1062 const llvm::Type *InterfaceLTy =
1063 CGM.getTypes().ConvertType(
1064 CGM.getContext().getObjCInterfaceType(Interface));
1065 const llvm::StructLayout *Layout =
1066 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
1067 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
1068 uint64_t Offset =
1069 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
1070
1071 return llvm::ConstantInt::get(
1072 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
1073 Offset);
1074}
1075
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001076CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001077 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001078}