blob: e6ca536c66bf9d6ce9b49a9af4486e76067e0ea1 [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
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.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000255llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000256 std::vector<llvm::Constant*> Ivars;
257 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000258 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000259 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000260 llvm::Constant *ObjCStr = MakeGlobal(
261 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
262 Ivars, ".objc_str");
263 ConstantStrings.push_back(
264 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
265 return ObjCStr;
266}
267
268///Generates a message send where the super is the receiver. This is a message
269///send to self with special delivery semantics indicating which class's method
270///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000271CodeGen::RValue
272CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000273 QualType ResultType,
274 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000275 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000276 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000277 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000278 bool IsClassMessage,
279 const CallArgList &CallArgs) {
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000280 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
281
282 CallArgList ActualArgs;
283
284 ActualArgs.push_back(
285 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
286 CGF.getContext().getObjCIdType()));
287 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
288 CGF.getContext().getObjCSelType()));
289 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
290
291 CodeGenTypes &Types = CGM.getTypes();
292 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
293 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
294
295
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000296 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000297 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000298 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000299
300
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000301 // Construct the structure used to look up the IMP
302 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
303 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000304 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000305
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000306 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
307 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000308
309 // Get the IMP
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000310 std::vector<const llvm::Type*> Params;
311 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
312 Params.push_back(SelectorTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000313 llvm::Constant *lookupFunction =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000314 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
315 llvm::PointerType::getUnqual(impType), Params, true),
316 "objc_msg_lookup_super");
317
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000318 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000319 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000320 lookupArgs+2);
321
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000322 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000323}
324
325/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000326CodeGen::RValue
327CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000328 QualType ResultType,
329 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000330 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000331 bool IsClassMessage,
332 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000333 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000334 CallArgList ActualArgs;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000335
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000336 ActualArgs.push_back(
337 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
338 CGF.getContext().getObjCIdType()));
339 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
340 CGF.getContext().getObjCSelType()));
341 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
342
343 CodeGenTypes &Types = CGM.getTypes();
344 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
345 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
346
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000347 std::vector<const llvm::Type*> Params;
348 Params.push_back(Receiver->getType());
349 Params.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000350 llvm::Constant *lookupFunction =
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000351 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
352 llvm::PointerType::getUnqual(impType), Params, true),
353 "objc_msg_lookup");
354
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000355 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000356
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000357 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000358}
359
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000360/// Generates a MethodList. Used in construction of a objc_class and
361/// objc_category structures.
362llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000363 const std::string &CategoryName,
364 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000365 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
366 bool isClassMethodList) {
367 // Get the method structure type.
368 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
369 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
370 PtrToInt8Ty, // Method types
371 llvm::PointerType::getUnqual(IMPTy), //Method pointer
372 NULL);
373 std::vector<llvm::Constant*> Methods;
374 std::vector<llvm::Constant*> Elements;
375 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
376 Elements.clear();
Chris Lattner077bf5e2008-11-24 03:33:13 +0000377 llvm::Constant *C =
378 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattnera4210072008-06-26 05:08:00 +0000379 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000380 Elements.push_back(
381 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
382 llvm::Constant *Method =
383 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000384 MethodSels[i].getAsString(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000385 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000386 Method = llvm::ConstantExpr::getBitCast(Method,
387 llvm::PointerType::getUnqual(IMPTy));
388 Elements.push_back(Method);
389 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
390 }
391
392 // Array of method structures
393 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000394 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000395 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000396 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000397
398 // Structure containing list pointer, array and array count
399 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
400 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
401 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
402 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
403 IntTy,
404 ObjCMethodArrayTy,
405 NULL);
406 // Refine next pointer type to concrete type
407 llvm::cast<llvm::OpaqueType>(
408 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
409 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
410
411 Methods.clear();
412 Methods.push_back(llvm::ConstantPointerNull::get(
413 llvm::PointerType::getUnqual(ObjCMethodListTy)));
414 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
415 MethodTypes.size()));
416 Methods.push_back(MethodArray);
417
418 // Create an instance of the structure
419 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
420}
421
422/// Generates an IvarList. Used in construction of a objc_class.
423llvm::Constant *CGObjCGNU::GenerateIvarList(
424 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
425 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
426 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
427 // Get the method structure type.
428 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
429 PtrToInt8Ty,
430 PtrToInt8Ty,
431 IntTy,
432 NULL);
433 std::vector<llvm::Constant*> Ivars;
434 std::vector<llvm::Constant*> Elements;
435 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
436 Elements.clear();
437 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
438 Zeros, 2));
439 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
440 Zeros, 2));
441 Elements.push_back(IvarOffsets[i]);
442 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
443 }
444
445 // Array of method structures
446 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
447 IvarNames.size());
448
449
450 Elements.clear();
Chris Lattnere160c9b2009-01-27 05:06:01 +0000451 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000452 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
453 // Structure containing array and array count
454 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
455 ObjCIvarArrayTy,
456 NULL);
457
458 // Create an instance of the structure
459 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
460}
461
462/// Generate a class structure
463llvm::Constant *CGObjCGNU::GenerateClassStructure(
464 llvm::Constant *MetaClass,
465 llvm::Constant *SuperClass,
466 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000467 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000468 llvm::Constant *Version,
469 llvm::Constant *InstanceSize,
470 llvm::Constant *IVars,
471 llvm::Constant *Methods,
472 llvm::Constant *Protocols) {
473 // Set up the class structure
474 // Note: Several of these are char*s when they should be ids. This is
475 // because the runtime performs this translation on load.
476 llvm::StructType *ClassTy = llvm::StructType::get(
477 PtrToInt8Ty, // class_pointer
478 PtrToInt8Ty, // super_class
479 PtrToInt8Ty, // name
480 LongTy, // version
481 LongTy, // info
482 LongTy, // instance_size
483 IVars->getType(), // ivars
484 Methods->getType(), // methods
485 // These are all filled in by the runtime, so we pretend
486 PtrTy, // dtable
487 PtrTy, // subclass_list
488 PtrTy, // sibling_class
489 PtrTy, // protocols
490 PtrTy, // gc_object_type
491 NULL);
492 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
493 llvm::Constant *NullP =
Chris Lattnere160c9b2009-01-27 05:06:01 +0000494 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000495 // Fill in the structure
496 std::vector<llvm::Constant*> Elements;
497 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
498 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000499 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000500 Elements.push_back(Zero);
501 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
502 Elements.push_back(InstanceSize);
503 Elements.push_back(IVars);
504 Elements.push_back(Methods);
505 Elements.push_back(NullP);
506 Elements.push_back(NullP);
507 Elements.push_back(NullP);
508 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
509 Elements.push_back(NullP);
510 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000511 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000512}
513
514llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
515 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
516 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
517 // Get the method structure type.
518 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
519 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
520 PtrToInt8Ty,
521 NULL);
522 std::vector<llvm::Constant*> Methods;
523 std::vector<llvm::Constant*> Elements;
524 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
525 Elements.clear();
526 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
527 Zeros, 2));
528 Elements.push_back(
529 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
530 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
531 }
532 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
533 MethodNames.size());
534 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
535 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
536 IntTy, ObjCMethodArrayTy, NULL);
537 Methods.clear();
538 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
539 Methods.push_back(Array);
540 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
541}
542// Create the protocol list structure used in classes, categories and so on
543llvm::Constant *CGObjCGNU::GenerateProtocolList(
544 const llvm::SmallVectorImpl<std::string> &Protocols) {
545 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
546 Protocols.size());
547 llvm::StructType *ProtocolListTy = llvm::StructType::get(
548 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
549 LongTy,//FIXME: Should be size_t
550 ProtocolArrayTy,
551 NULL);
552 std::vector<llvm::Constant*> Elements;
553 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
554 iter != endIter ; iter++) {
555 llvm::Constant *Ptr =
556 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
557 Elements.push_back(Ptr);
558 }
559 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
560 Elements);
561 Elements.clear();
562 Elements.push_back(NULLPtr);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000563 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000564 Elements.push_back(ProtocolArray);
565 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
566}
567
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000568llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000569 const ObjCProtocolDecl *PD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000570 return ExistingProtocols[PD->getNameAsString()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000571}
572
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000573void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
574 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000575 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000576 llvm::SmallVector<std::string, 16> Protocols;
577 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
578 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000579 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000580 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
581 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
582 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
583 E = PD->instmeth_end(); iter != E; iter++) {
584 std::string TypeStr;
585 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
586 InstanceMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000587 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000588 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000589 }
590 // Collect information about class methods:
591 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
592 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
593 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
594 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
595 std::string TypeStr;
596 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
597 ClassMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000598 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000599 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000600 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000601
602 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
603 llvm::Constant *InstanceMethodList =
604 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
605 llvm::Constant *ClassMethodList =
606 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
607 // Protocols are objects containing lists of the methods implemented and
608 // protocols adopted.
609 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
610 PtrToInt8Ty,
611 ProtocolList->getType(),
612 InstanceMethodList->getType(),
613 ClassMethodList->getType(),
614 NULL);
615 std::vector<llvm::Constant*> Elements;
616 // The isa pointer must be set to a magic number so the runtime knows it's
617 // the correct layout.
618 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
619 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
620 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
621 Elements.push_back(ProtocolList);
622 Elements.push_back(InstanceMethodList);
623 Elements.push_back(ClassMethodList);
624 ExistingProtocols[ProtocolName] =
625 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
626 ".objc_protocol"), IdTy);
627}
628
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000629void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000630 std::string ClassName = OCD->getClassInterface()->getNameAsString();
631 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000632 // Collect information about instance methods
633 llvm::SmallVector<Selector, 16> InstanceMethodSels;
634 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000635 for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000636 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
637 InstanceMethodSels.push_back((*iter)->getSelector());
638 std::string TypeStr;
639 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
640 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
641 }
642
643 // Collect information about class methods
644 llvm::SmallVector<Selector, 16> ClassMethodSels;
645 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000646 for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000647 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
648 ClassMethodSels.push_back((*iter)->getSelector());
649 std::string TypeStr;
650 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
651 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
652 }
653
654 // Collect the names of referenced protocols
655 llvm::SmallVector<std::string, 16> Protocols;
656 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
657 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
658 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
659 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000660 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000661
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000662 std::vector<llvm::Constant*> Elements;
663 Elements.push_back(MakeConstantString(CategoryName));
664 Elements.push_back(MakeConstantString(ClassName));
665 // Instance method list
666 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000667 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000668 false), PtrTy));
669 // Class method list
670 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000671 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000672 PtrTy));
673 // Protocol list
674 Elements.push_back(llvm::ConstantExpr::getBitCast(
675 GenerateProtocolList(Protocols), PtrTy));
676 Categories.push_back(llvm::ConstantExpr::getBitCast(
677 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
678 PtrTy, PtrTy, NULL), Elements), PtrTy));
679}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000680
681void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
682 ASTContext &Context = CGM.getContext();
683
684 // Get the superclass name.
685 const ObjCInterfaceDecl * SuperClassDecl =
686 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000687 std::string SuperClassName;
688 if (SuperClassDecl)
689 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000690
691 // Get the class name
692 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000693 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000694
695 // Get the size of instances. For runtimes that support late-bound instances
696 // this should probably be something different (size just of instance
697 // varaibles in this class, not superclasses?).
698 int instanceSize = 0;
699 const llvm::Type *ObjTy = 0;
700 if (!LateBoundIVars()) {
701 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000702 instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000703 } else {
704 // This is required by newer ObjC runtimes.
705 assert(0 && "Late-bound instance variables not yet supported");
706 }
707
708 // Collect information about instance variables.
709 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
710 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
711 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
712 const llvm::StructLayout *Layout =
713 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
714 ObjTy = llvm::PointerType::getUnqual(ObjTy);
715 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
716 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
717 // Store the name
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000718 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
719 ->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000720 // Get the type encoding for this ivar
721 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000722 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000723 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
724 // Get the offset
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000725 FieldDecl *Field = ClassDecl->lookupFieldDeclForIvar(Context, (*iter));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000726 int offset =
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000727 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000728 IvarOffsets.push_back(
729 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
730 }
731
732 // Collect information about instance methods
733 llvm::SmallVector<Selector, 16> InstanceMethodSels;
734 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
735 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
736 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
737 InstanceMethodSels.push_back((*iter)->getSelector());
738 std::string TypeStr;
739 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
740 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
741 }
742
743 // Collect information about class methods
744 llvm::SmallVector<Selector, 16> ClassMethodSels;
745 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
746 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
747 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
748 ClassMethodSels.push_back((*iter)->getSelector());
749 std::string TypeStr;
750 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
751 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
752 }
753 // Collect the names of referenced protocols
754 llvm::SmallVector<std::string, 16> Protocols;
755 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
756 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
757 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000758 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000759
760
761
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000762 // Get the superclass pointer.
763 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +0000764 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000765 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
766 } else {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000767 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000768 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000769 // Empty vector used to construct empty method lists
770 llvm::SmallVector<llvm::Constant*, 1> empty;
771 // Generate the method and instance variable lists
772 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000773 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000774 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000775 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000776 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
777 IvarOffsets);
778 //Generate metaclass for class methods
779 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000780 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000781 empty, empty, empty), ClassMethodList, NULLPtr);
782 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +0000783 llvm::Constant *ClassStruct =
784 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
785 ClassName.c_str(), 0,
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000786 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000787 MethodList, GenerateProtocolList(Protocols));
788 // Add class structure to list to be added to the symtab later
789 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
790 Classes.push_back(ClassStruct);
791}
792
793llvm::Function *CGObjCGNU::ModuleInitFunction() {
794 // Only emit an ObjC load function if no Objective-C stuff has been called
795 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
796 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000797 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000798 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000799
Chris Lattnere160c9b2009-01-27 05:06:01 +0000800 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
801 SelectorTy->getElementType());
802 const llvm::Type *SelStructPtrTy = SelectorTy;
803 bool isSelOpaque = false;
804 if (SelStructTy == 0) {
805 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
806 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
807 isSelOpaque = true;
808 }
809
Eli Friedman1b8956e2008-06-01 16:00:02 +0000810 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +0000811 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +0000812 TheModule.addTypeName(".objc_id", IdTy);
813 TheModule.addTypeName(".objc_imp", IMPTy);
814
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000815 std::vector<llvm::Constant*> Elements;
816 // Generate statics list:
817 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
818 ConstantStrings.size() + 1);
819 ConstantStrings.push_back(NULLPtr);
820 Elements.push_back(MakeConstantString("NSConstantString",
821 ".objc_static_class_name"));
822 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
823 llvm::StructType *StaticsListTy =
824 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000825 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000826 llvm::Constant *Statics =
827 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000828 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000829 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000830 Elements.clear();
831 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000832 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000833 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000834 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
835 // Array of classes, categories, and constant objects
836 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
837 Classes.size() + Categories.size() + 2);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000838 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Chris Lattner630404b2008-06-26 04:10:42 +0000839 llvm::Type::Int16Ty,
840 llvm::Type::Int16Ty,
841 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000842
843 Elements.clear();
844 // Pointer to an array of selectors used in this module.
845 std::vector<llvm::Constant*> Selectors;
846 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
847 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
848 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000849 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
850 Elements.push_back(MakeConstantString(iter->first.second,
851 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000852 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
853 Elements.clear();
854 }
855 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
856 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000857 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000858 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000859 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000860 Elements.push_back(NULLPtr);
861 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
862 Elements.clear();
863 }
864 Elements.push_back(NULLPtr);
865 Elements.push_back(NULLPtr);
866 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
867 Elements.clear();
868 // Number of static selectors
869 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
870 llvm::Constant *SelectorList = MakeGlobal(
871 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
872 ".objc_selector_list");
Chris Lattnere160c9b2009-01-27 05:06:01 +0000873 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
874 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000875
876 // Now that all of the static selectors exist, create pointers to them.
877 int index = 0;
878 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
879 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
880 iter != iterEnd; ++iter) {
881 llvm::Constant *Idxs[] = {Zeros[0],
882 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000883 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
884 true, llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000885 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
886 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000887 // If selectors are defined as an opaque type, cast the pointer to this
888 // type.
889 if (isSelOpaque) {
890 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
891 llvm::PointerType::getUnqual(SelectorTy));
892 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000893 (*iter).second->setAliasee(SelPtr);
894 }
895 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
896 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
897 iter != iterEnd; iter++) {
898 llvm::Constant *Idxs[] = {Zeros[0],
899 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000900 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000901 llvm::GlobalValue::InternalLinkage,
902 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
903 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000904 // If selectors are defined as an opaque type, cast the pointer to this
905 // type.
906 if (isSelOpaque) {
907 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
908 llvm::PointerType::getUnqual(SelectorTy));
909 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000910 (*iter).second->setAliasee(SelPtr);
911 }
912 // Number of classes defined.
913 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
914 Classes.size()));
915 // Number of categories defined
916 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
917 Categories.size()));
918 // Create an array of classes, then categories, then static object instances
919 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
920 // NULL-terminated list of static object instances (mainly constant strings)
921 Classes.push_back(Statics);
922 Classes.push_back(NULLPtr);
923 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
924 Elements.push_back(ClassList);
925 // Construct the symbol table
926 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
927
928 // The symbol table is contained in a module which has some version-checking
929 // constants
930 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
931 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
932 Elements.clear();
933 // Runtime version used for compatibility checking.
934 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
935 //FIXME: Should be sizeof(ModuleTy)
936 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
937 //FIXME: Should be the path to the file where this module was declared
938 Elements.push_back(NULLPtr);
939 Elements.push_back(SymTab);
940 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
941
942 // Create the load function calling the runtime entry point with the module
943 // structure
944 std::vector<const llvm::Type*> VoidArgs;
945 llvm::Function * LoadFunction = llvm::Function::Create(
946 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
947 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
948 &TheModule);
949 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000950 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000951 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000952
953 std::vector<const llvm::Type*> Params(1,
954 llvm::PointerType::getUnqual(ModuleTy));
955 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
956 llvm::Type::VoidTy, Params, true), "__objc_exec_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000957 Builder.CreateCall(Register, Module);
958 Builder.CreateRetVoid();
959 return LoadFunction;
960}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000961
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000962llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
963 const ObjCContainerDecl *CD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000964 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +0000965 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +0000966 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
967 std::string ClassName = OMD->getClassInterface()->getNameAsString();
968 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000969 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000970
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000971 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000972 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000973 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000974 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
975 MethodName, isClassMethod);
976
Gabor Greif984d0b42008-04-06 20:42:52 +0000977 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000978 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000979 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000980 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000981 return Method;
982}
983
Daniel Dunbar49f66022008-09-24 03:38:44 +0000984llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
985 return 0;
986}
987
988llvm::Function *CGObjCGNU::GetPropertySetFunction() {
989 return 0;
990}
991
992llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000993 std::vector<const llvm::Type*> Params(1, IdTy);
994 return cast<llvm::Function>(CGM.CreateRuntimeFunction(
995 llvm::FunctionType::get(llvm::Type::VoidTy, Params, true),
996 "objc_enumerationMutation"));
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000997}
998
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000999void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1000 const Stmt &S) {
1001 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001002}
1003
1004void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +00001005 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001006 CGF.ErrorUnsupported(&S, "@throw statement");
1007}
1008
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001009llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001010 llvm::Value *AddrWeakObj)
1011{
1012 return 0;
1013}
1014
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001015void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1016 llvm::Value *src, llvm::Value *dst)
1017{
1018 return;
1019}
1020
Fariborz Jahanian58626502008-11-19 00:59:10 +00001021void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1022 llvm::Value *src, llvm::Value *dst)
1023{
1024 return;
1025}
1026
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001027void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1028 llvm::Value *src, llvm::Value *dst)
1029{
1030 return;
1031}
1032
Fariborz Jahanian58626502008-11-19 00:59:10 +00001033void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1034 llvm::Value *src, llvm::Value *dst)
1035{
1036 return;
1037}
1038
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001039LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1040 QualType ObjectTy,
1041 llvm::Value *BaseValue,
1042 const ObjCIvarDecl *Ivar,
1043 const FieldDecl *Field,
1044 unsigned CVRQualifiers) {
1045 if (Ivar->isBitField())
1046 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
1047 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001048 // TODO: Add a special case for isa (index 0)
1049 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
1050 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001051 LValue LV = LValue::MakeAddr(V,
1052 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
1053 LValue::SetObjCIvar(LV, true);
1054 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001055}
1056
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001057llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
1058 ObjCInterfaceDecl *Interface,
1059 const ObjCIvarDecl *Ivar) {
1060 const llvm::Type *InterfaceLTy =
1061 CGM.getTypes().ConvertType(
1062 CGM.getContext().getObjCInterfaceType(Interface));
1063 const llvm::StructLayout *Layout =
1064 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
1065 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
1066 uint64_t Offset =
1067 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
1068
1069 return llvm::ConstantInt::get(
1070 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
1071 Offset);
1072}
1073
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001074CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001075 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001076}