blob: a1bd9d75ea86cf27e6f00d2bdefa234d9be540dc [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;
Chris Lattner391d77a2008-03-30 23:03:07 +000047 const llvm::Type *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,
110 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000111 bool IsClassMessage,
112 const CallArgList &CallArgs);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000113 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000114 const ObjCInterfaceDecl *OID);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000115 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000116
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000117 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
118 const ObjCContainerDecl *CD);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000119 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
120 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000121 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000122 const ObjCProtocolDecl *PD);
123 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000124 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000125 virtual llvm::Function *GetPropertyGetFunction();
126 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000127 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000128
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000129 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
130 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000131 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
132 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000133 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000134 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000135 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
136 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000137 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
138 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000139 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
140 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000141 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
142 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000143 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
144 QualType ObjectTy,
145 llvm::Value *BaseValue,
146 const ObjCIvarDecl *Ivar,
147 const FieldDecl *Field,
148 unsigned CVRQualifiers);
Chris Lattner0f984262008-03-01 08:50:34 +0000149};
150} // end anonymous namespace
151
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000152
153
154static std::string SymbolNameForClass(const std::string &ClassName) {
155 return ".objc_class_" + ClassName;
156}
157
158static std::string SymbolNameForMethod(const std::string &ClassName, const
159 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
160{
161 return "._objc_method_" + ClassName +"("+CategoryName+")"+
162 (isClassMethod ? "+" : "-") + MethodName;
163}
164
Chris Lattnerdce14062008-06-26 04:19:03 +0000165CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
166 : CGM(cgm), TheModule(CGM.getModule()) {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000167 IntTy = cast<llvm::IntegerType>(
168 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
169 LongTy = cast<llvm::IntegerType>(
170 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Chris Lattnerdce14062008-06-26 04:19:03 +0000171
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000172 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000173 Zeros[1] = Zeros[0];
174 NULLPtr = llvm::ConstantPointerNull::get(
175 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000176 // C string type. Used in lots of places.
177 PtrToInt8Ty =
178 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
179 // Get the selector Type.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000180 SelectorTy = cast<llvm::PointerType>(
181 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
182
Chris Lattner391d77a2008-03-30 23:03:07 +0000183 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
184 PtrTy = PtrToInt8Ty;
185
186 // Object type
187 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
188 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000189 llvm::Type *ObjectTy = llvm::StructType::get(OpaqueIdTy, NULL);
190 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(
191 ObjectTy);
192 ObjectTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
193 IdTy = llvm::PointerType::getUnqual(ObjectTy);
Chris Lattner391d77a2008-03-30 23:03:07 +0000194
195 // IMP type
196 std::vector<const llvm::Type*> IMPArgs;
197 IMPArgs.push_back(IdTy);
198 IMPArgs.push_back(SelectorTy);
199 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000200}
201// This has to perform the lookup every time, since posing and related
202// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000203llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000204 const ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000205 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000206 ClassName = Builder.CreateStructGEP(ClassName, 0);
207
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000208 llvm::Constant *ClassLookupFn =
209 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
210 NULL);
211 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000212}
213
Chris Lattner8e67b632008-06-26 04:37:12 +0000214/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000215llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000216 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner077bf5e2008-11-24 03:33:13 +0000217 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner8e67b632008-06-26 04:37:12 +0000218 if (US == 0)
219 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
220 llvm::GlobalValue::InternalLinkage,
221 ".objc_untyped_selector_alias",
222 NULL, &TheModule);
223
224 return Builder.CreateLoad(US);
225
226}
227
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000228llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
229 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000230 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
231 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
232 llvm::GlobalValue::InternalLinkage,
233 ConstStr, Name, &TheModule);
234 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
235}
236llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
237 std::vector<llvm::Constant*> &V, const std::string &Name) {
238 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
239 return new llvm::GlobalVariable(Ty, false,
240 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
241}
242llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
243 std::vector<llvm::Constant*> &V, const std::string &Name) {
244 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
245 return new llvm::GlobalVariable(Ty, false,
246 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
247}
248
249/// Generate an NSConstantString object.
250//TODO: In case there are any crazy people still using the GNU runtime without
251//an OpenStep implementation, this should let them select their own class for
252//constant strings.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000253llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000254 std::vector<llvm::Constant*> Ivars;
255 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000256 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000257 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000258 llvm::Constant *ObjCStr = MakeGlobal(
259 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
260 Ivars, ".objc_str");
261 ConstantStrings.push_back(
262 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
263 return ObjCStr;
264}
265
266///Generates a message send where the super is the receiver. This is a message
267///send to self with special delivery semantics indicating which class's method
268///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000269CodeGen::RValue
270CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000271 QualType ResultType,
272 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000273 const ObjCInterfaceDecl *Class,
274 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000275 bool IsClassMessage,
276 const CallArgList &CallArgs) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000277 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000278 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000279 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000280 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000281 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000282 std::vector<const llvm::Type*> impArgTypes;
283 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000284 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000285
286 // Avoid an explicit cast on the IMP by getting a version that has the right
287 // return type.
288 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
289 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000290 // Construct the structure used to look up the IMP
291 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
292 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000293 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000294 // FIXME: volatility
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000295 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
296 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000297
298 // Get the IMP
299 llvm::Constant *lookupFunction =
300 TheModule.getOrInsertFunction("objc_msg_lookup_super",
301 llvm::PointerType::getUnqual(impType),
302 llvm::PointerType::getUnqual(ObjCSuperTy),
303 SelectorTy, NULL);
304 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000305 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000306 lookupArgs+2);
307
308 // Call the method
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000309 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000310 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000311 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000312 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000313 CGF.getContext().getObjCSelType()));
314 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000315 return CGF.EmitCall(CGM.getTypes().getFunctionInfo(ResultType, ActualArgs),
316 imp, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000317}
318
319/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000320CodeGen::RValue
321CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000322 QualType ResultType,
323 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000324 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000325 bool IsClassMessage,
326 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000327 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
328 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000329
330 // Look up the method implementation.
331 std::vector<const llvm::Type*> impArgTypes;
332 const llvm::Type *RetTy;
333 //TODO: Revisit this when LLVM supports aggregate return types.
334 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
335 RetTy = ReturnTy;
336 } else {
337 // For struct returns allocate the space in the caller and pass it up to
338 // the sender.
339 RetTy = llvm::Type::VoidTy;
340 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
341 }
342 impArgTypes.push_back(Receiver->getType());
343 impArgTypes.push_back(SelectorTy);
344
345 // Avoid an explicit cast on the IMP by getting a version that has the right
346 // return type.
347 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
348 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000349
350 llvm::Constant *lookupFunction =
351 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000352 llvm::PointerType::getUnqual(impType),
353 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000354 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000355
356 // Call the method.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000357 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000358 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000359 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000360 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000361 CGF.getContext().getObjCSelType()));
362 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000363 return CGF.EmitCall(CGM.getTypes().getFunctionInfo(ResultType, ActualArgs),
364 imp, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000365}
366
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000367/// Generates a MethodList. Used in construction of a objc_class and
368/// objc_category structures.
369llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000370 const std::string &CategoryName,
371 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000372 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
373 bool isClassMethodList) {
374 // Get the method structure type.
375 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
376 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
377 PtrToInt8Ty, // Method types
378 llvm::PointerType::getUnqual(IMPTy), //Method pointer
379 NULL);
380 std::vector<llvm::Constant*> Methods;
381 std::vector<llvm::Constant*> Elements;
382 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
383 Elements.clear();
Chris Lattner077bf5e2008-11-24 03:33:13 +0000384 llvm::Constant *C =
385 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattnera4210072008-06-26 05:08:00 +0000386 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000387 Elements.push_back(
388 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
389 llvm::Constant *Method =
390 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000391 MethodSels[i].getAsString(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000392 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000393 Method = llvm::ConstantExpr::getBitCast(Method,
394 llvm::PointerType::getUnqual(IMPTy));
395 Elements.push_back(Method);
396 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
397 }
398
399 // Array of method structures
400 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000401 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000402 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000403 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000404
405 // Structure containing list pointer, array and array count
406 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
407 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
408 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
409 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
410 IntTy,
411 ObjCMethodArrayTy,
412 NULL);
413 // Refine next pointer type to concrete type
414 llvm::cast<llvm::OpaqueType>(
415 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
416 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
417
418 Methods.clear();
419 Methods.push_back(llvm::ConstantPointerNull::get(
420 llvm::PointerType::getUnqual(ObjCMethodListTy)));
421 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
422 MethodTypes.size()));
423 Methods.push_back(MethodArray);
424
425 // Create an instance of the structure
426 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
427}
428
429/// Generates an IvarList. Used in construction of a objc_class.
430llvm::Constant *CGObjCGNU::GenerateIvarList(
431 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
432 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
433 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
434 // Get the method structure type.
435 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
436 PtrToInt8Ty,
437 PtrToInt8Ty,
438 IntTy,
439 NULL);
440 std::vector<llvm::Constant*> Ivars;
441 std::vector<llvm::Constant*> Elements;
442 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
443 Elements.clear();
444 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
445 Zeros, 2));
446 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
447 Zeros, 2));
448 Elements.push_back(IvarOffsets[i]);
449 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
450 }
451
452 // Array of method structures
453 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
454 IvarNames.size());
455
456
457 Elements.clear();
Chris Lattnere160c9b2009-01-27 05:06:01 +0000458 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000459 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
460 // Structure containing array and array count
461 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
462 ObjCIvarArrayTy,
463 NULL);
464
465 // Create an instance of the structure
466 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
467}
468
469/// Generate a class structure
470llvm::Constant *CGObjCGNU::GenerateClassStructure(
471 llvm::Constant *MetaClass,
472 llvm::Constant *SuperClass,
473 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000474 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000475 llvm::Constant *Version,
476 llvm::Constant *InstanceSize,
477 llvm::Constant *IVars,
478 llvm::Constant *Methods,
479 llvm::Constant *Protocols) {
480 // Set up the class structure
481 // Note: Several of these are char*s when they should be ids. This is
482 // because the runtime performs this translation on load.
483 llvm::StructType *ClassTy = llvm::StructType::get(
484 PtrToInt8Ty, // class_pointer
485 PtrToInt8Ty, // super_class
486 PtrToInt8Ty, // name
487 LongTy, // version
488 LongTy, // info
489 LongTy, // instance_size
490 IVars->getType(), // ivars
491 Methods->getType(), // methods
492 // These are all filled in by the runtime, so we pretend
493 PtrTy, // dtable
494 PtrTy, // subclass_list
495 PtrTy, // sibling_class
496 PtrTy, // protocols
497 PtrTy, // gc_object_type
498 NULL);
499 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
500 llvm::Constant *NullP =
Chris Lattnere160c9b2009-01-27 05:06:01 +0000501 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000502 // Fill in the structure
503 std::vector<llvm::Constant*> Elements;
504 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
505 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000506 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000507 Elements.push_back(Zero);
508 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
509 Elements.push_back(InstanceSize);
510 Elements.push_back(IVars);
511 Elements.push_back(Methods);
512 Elements.push_back(NullP);
513 Elements.push_back(NullP);
514 Elements.push_back(NullP);
515 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
516 Elements.push_back(NullP);
517 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000518 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000519}
520
521llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
522 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
523 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
524 // Get the method structure type.
525 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
526 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
527 PtrToInt8Ty,
528 NULL);
529 std::vector<llvm::Constant*> Methods;
530 std::vector<llvm::Constant*> Elements;
531 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
532 Elements.clear();
533 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
534 Zeros, 2));
535 Elements.push_back(
536 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
537 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
538 }
539 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
540 MethodNames.size());
541 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
542 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
543 IntTy, ObjCMethodArrayTy, NULL);
544 Methods.clear();
545 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
546 Methods.push_back(Array);
547 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
548}
549// Create the protocol list structure used in classes, categories and so on
550llvm::Constant *CGObjCGNU::GenerateProtocolList(
551 const llvm::SmallVectorImpl<std::string> &Protocols) {
552 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
553 Protocols.size());
554 llvm::StructType *ProtocolListTy = llvm::StructType::get(
555 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
556 LongTy,//FIXME: Should be size_t
557 ProtocolArrayTy,
558 NULL);
559 std::vector<llvm::Constant*> Elements;
560 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
561 iter != endIter ; iter++) {
562 llvm::Constant *Ptr =
563 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
564 Elements.push_back(Ptr);
565 }
566 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
567 Elements);
568 Elements.clear();
569 Elements.push_back(NULLPtr);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000570 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000571 Elements.push_back(ProtocolArray);
572 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
573}
574
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000575llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000576 const ObjCProtocolDecl *PD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000577 return ExistingProtocols[PD->getNameAsString()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000578}
579
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000580void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
581 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000582 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000583 llvm::SmallVector<std::string, 16> Protocols;
584 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
585 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000586 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000587 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
588 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
589 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
590 E = PD->instmeth_end(); iter != E; iter++) {
591 std::string TypeStr;
592 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
593 InstanceMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000594 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000595 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000596 }
597 // Collect information about class methods:
598 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
599 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
600 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
601 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
602 std::string TypeStr;
603 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
604 ClassMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000605 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000606 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000607 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000608
609 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
610 llvm::Constant *InstanceMethodList =
611 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
612 llvm::Constant *ClassMethodList =
613 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
614 // Protocols are objects containing lists of the methods implemented and
615 // protocols adopted.
616 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
617 PtrToInt8Ty,
618 ProtocolList->getType(),
619 InstanceMethodList->getType(),
620 ClassMethodList->getType(),
621 NULL);
622 std::vector<llvm::Constant*> Elements;
623 // The isa pointer must be set to a magic number so the runtime knows it's
624 // the correct layout.
625 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
626 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
627 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
628 Elements.push_back(ProtocolList);
629 Elements.push_back(InstanceMethodList);
630 Elements.push_back(ClassMethodList);
631 ExistingProtocols[ProtocolName] =
632 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
633 ".objc_protocol"), IdTy);
634}
635
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000636void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000637 std::string ClassName = OCD->getClassInterface()->getNameAsString();
638 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000639 // Collect information about instance methods
640 llvm::SmallVector<Selector, 16> InstanceMethodSels;
641 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000642 for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000643 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
644 InstanceMethodSels.push_back((*iter)->getSelector());
645 std::string TypeStr;
646 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
647 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
648 }
649
650 // Collect information about class methods
651 llvm::SmallVector<Selector, 16> ClassMethodSels;
652 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000653 for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000654 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
655 ClassMethodSels.push_back((*iter)->getSelector());
656 std::string TypeStr;
657 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
658 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
659 }
660
661 // Collect the names of referenced protocols
662 llvm::SmallVector<std::string, 16> Protocols;
663 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
664 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
665 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
666 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000667 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000668
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000669 std::vector<llvm::Constant*> Elements;
670 Elements.push_back(MakeConstantString(CategoryName));
671 Elements.push_back(MakeConstantString(ClassName));
672 // Instance method list
673 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000674 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000675 false), PtrTy));
676 // Class method list
677 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000678 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000679 PtrTy));
680 // Protocol list
681 Elements.push_back(llvm::ConstantExpr::getBitCast(
682 GenerateProtocolList(Protocols), PtrTy));
683 Categories.push_back(llvm::ConstantExpr::getBitCast(
684 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
685 PtrTy, PtrTy, NULL), Elements), PtrTy));
686}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000687
688void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
689 ASTContext &Context = CGM.getContext();
690
691 // Get the superclass name.
692 const ObjCInterfaceDecl * SuperClassDecl =
693 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000694 std::string SuperClassName;
695 if (SuperClassDecl)
696 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000697
698 // Get the class name
699 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000700 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000701
702 // Get the size of instances. For runtimes that support late-bound instances
703 // this should probably be something different (size just of instance
704 // varaibles in this class, not superclasses?).
705 int instanceSize = 0;
706 const llvm::Type *ObjTy = 0;
707 if (!LateBoundIVars()) {
708 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000709 instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000710 } else {
711 // This is required by newer ObjC runtimes.
712 assert(0 && "Late-bound instance variables not yet supported");
713 }
714
715 // Collect information about instance variables.
716 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
717 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
718 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
719 const llvm::StructLayout *Layout =
720 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
721 ObjTy = llvm::PointerType::getUnqual(ObjTy);
722 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
723 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
724 // Store the name
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000725 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
726 ->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000727 // Get the type encoding for this ivar
728 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000729 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000730 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
731 // Get the offset
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000732 FieldDecl *Field = ClassDecl->lookupFieldDeclForIvar(Context, (*iter));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000733 int offset =
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000734 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000735 IvarOffsets.push_back(
736 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
737 }
738
739 // Collect information about instance methods
740 llvm::SmallVector<Selector, 16> InstanceMethodSels;
741 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
742 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
743 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
744 InstanceMethodSels.push_back((*iter)->getSelector());
745 std::string TypeStr;
746 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
747 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
748 }
749
750 // Collect information about class methods
751 llvm::SmallVector<Selector, 16> ClassMethodSels;
752 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
753 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
754 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
755 ClassMethodSels.push_back((*iter)->getSelector());
756 std::string TypeStr;
757 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
758 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
759 }
760 // Collect the names of referenced protocols
761 llvm::SmallVector<std::string, 16> Protocols;
762 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
763 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
764 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000765 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000766
767
768
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000769 // Get the superclass pointer.
770 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +0000771 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000772 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
773 } else {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000774 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000775 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000776 // Empty vector used to construct empty method lists
777 llvm::SmallVector<llvm::Constant*, 1> empty;
778 // Generate the method and instance variable lists
779 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000780 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000781 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000782 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000783 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
784 IvarOffsets);
785 //Generate metaclass for class methods
786 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000787 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000788 empty, empty, empty), ClassMethodList, NULLPtr);
789 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +0000790 llvm::Constant *ClassStruct =
791 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
792 ClassName.c_str(), 0,
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000793 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000794 MethodList, GenerateProtocolList(Protocols));
795 // Add class structure to list to be added to the symtab later
796 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
797 Classes.push_back(ClassStruct);
798}
799
800llvm::Function *CGObjCGNU::ModuleInitFunction() {
801 // Only emit an ObjC load function if no Objective-C stuff has been called
802 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
803 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000804 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000805 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000806
Chris Lattnere160c9b2009-01-27 05:06:01 +0000807 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
808 SelectorTy->getElementType());
809 const llvm::Type *SelStructPtrTy = SelectorTy;
810 bool isSelOpaque = false;
811 if (SelStructTy == 0) {
812 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
813 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
814 isSelOpaque = true;
815 }
816
Eli Friedman1b8956e2008-06-01 16:00:02 +0000817 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +0000818 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +0000819 TheModule.addTypeName(".objc_id", IdTy);
820 TheModule.addTypeName(".objc_imp", IMPTy);
821
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000822 std::vector<llvm::Constant*> Elements;
823 // Generate statics list:
824 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
825 ConstantStrings.size() + 1);
826 ConstantStrings.push_back(NULLPtr);
827 Elements.push_back(MakeConstantString("NSConstantString",
828 ".objc_static_class_name"));
829 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
830 llvm::StructType *StaticsListTy =
831 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000832 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000833 llvm::Constant *Statics =
834 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000835 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000836 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000837 Elements.clear();
838 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000839 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000840 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000841 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
842 // Array of classes, categories, and constant objects
843 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
844 Classes.size() + Categories.size() + 2);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000845 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Chris Lattner630404b2008-06-26 04:10:42 +0000846 llvm::Type::Int16Ty,
847 llvm::Type::Int16Ty,
848 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000849
850 Elements.clear();
851 // Pointer to an array of selectors used in this module.
852 std::vector<llvm::Constant*> Selectors;
853 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
854 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
855 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000856 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
857 Elements.push_back(MakeConstantString(iter->first.second,
858 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000859 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
860 Elements.clear();
861 }
862 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
863 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000864 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000865 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000866 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000867 Elements.push_back(NULLPtr);
868 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
869 Elements.clear();
870 }
871 Elements.push_back(NULLPtr);
872 Elements.push_back(NULLPtr);
873 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
874 Elements.clear();
875 // Number of static selectors
876 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
877 llvm::Constant *SelectorList = MakeGlobal(
878 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
879 ".objc_selector_list");
Chris Lattnere160c9b2009-01-27 05:06:01 +0000880 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
881 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000882
883 // Now that all of the static selectors exist, create pointers to them.
884 int index = 0;
885 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
886 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
887 iter != iterEnd; ++iter) {
888 llvm::Constant *Idxs[] = {Zeros[0],
889 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000890 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
891 true, llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000892 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
893 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000894 // If selectors are defined as an opaque type, cast the pointer to this
895 // type.
896 if (isSelOpaque) {
897 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
898 llvm::PointerType::getUnqual(SelectorTy));
899 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000900 (*iter).second->setAliasee(SelPtr);
901 }
902 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
903 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
904 iter != iterEnd; iter++) {
905 llvm::Constant *Idxs[] = {Zeros[0],
906 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000907 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000908 llvm::GlobalValue::InternalLinkage,
909 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
910 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000911 // If selectors are defined as an opaque type, cast the pointer to this
912 // type.
913 if (isSelOpaque) {
914 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
915 llvm::PointerType::getUnqual(SelectorTy));
916 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000917 (*iter).second->setAliasee(SelPtr);
918 }
919 // Number of classes defined.
920 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
921 Classes.size()));
922 // Number of categories defined
923 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
924 Categories.size()));
925 // Create an array of classes, then categories, then static object instances
926 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
927 // NULL-terminated list of static object instances (mainly constant strings)
928 Classes.push_back(Statics);
929 Classes.push_back(NULLPtr);
930 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
931 Elements.push_back(ClassList);
932 // Construct the symbol table
933 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
934
935 // The symbol table is contained in a module which has some version-checking
936 // constants
937 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
938 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
939 Elements.clear();
940 // Runtime version used for compatibility checking.
941 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
942 //FIXME: Should be sizeof(ModuleTy)
943 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
944 //FIXME: Should be the path to the file where this module was declared
945 Elements.push_back(NULLPtr);
946 Elements.push_back(SymTab);
947 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
948
949 // Create the load function calling the runtime entry point with the module
950 // structure
951 std::vector<const llvm::Type*> VoidArgs;
952 llvm::Function * LoadFunction = llvm::Function::Create(
953 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
954 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
955 &TheModule);
956 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000957 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000958 Builder.SetInsertPoint(EntryBB);
959 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
960 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
961 Builder.CreateCall(Register, Module);
962 Builder.CreateRetVoid();
963 return LoadFunction;
964}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000965
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000966llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
967 const ObjCContainerDecl *CD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000968 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +0000969 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +0000970 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
971 std::string ClassName = OMD->getClassInterface()->getNameAsString();
972 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000973 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000974
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000975 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000976 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000977 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000978 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
979 MethodName, isClassMethod);
980
Gabor Greif984d0b42008-04-06 20:42:52 +0000981 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000982 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000983 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000984 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000985 return Method;
986}
987
Daniel Dunbar49f66022008-09-24 03:38:44 +0000988llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
989 return 0;
990}
991
992llvm::Function *CGObjCGNU::GetPropertySetFunction() {
993 return 0;
994}
995
996llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian660af742009-02-03 21:52:35 +0000997return
998 (llvm::Function*)TheModule.getOrInsertFunction(
999 "objc_enumerationMutation", llvm::Type::VoidTy, IdTy, NULL);
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001000}
1001
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001002void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1003 const Stmt &S) {
1004 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001005}
1006
1007void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +00001008 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001009 CGF.ErrorUnsupported(&S, "@throw statement");
1010}
1011
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001012llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001013 llvm::Value *AddrWeakObj)
1014{
1015 return 0;
1016}
1017
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001018void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1019 llvm::Value *src, llvm::Value *dst)
1020{
1021 return;
1022}
1023
Fariborz Jahanian58626502008-11-19 00:59:10 +00001024void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1025 llvm::Value *src, llvm::Value *dst)
1026{
1027 return;
1028}
1029
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001030void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1031 llvm::Value *src, llvm::Value *dst)
1032{
1033 return;
1034}
1035
Fariborz Jahanian58626502008-11-19 00:59:10 +00001036void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1037 llvm::Value *src, llvm::Value *dst)
1038{
1039 return;
1040}
1041
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001042LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1043 QualType ObjectTy,
1044 llvm::Value *BaseValue,
1045 const ObjCIvarDecl *Ivar,
1046 const FieldDecl *Field,
1047 unsigned CVRQualifiers) {
1048 if (Ivar->isBitField())
1049 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
1050 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001051 // TODO: Add a special case for isa (index 0)
1052 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
1053 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001054 LValue LV = LValue::MakeAddr(V,
1055 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
1056 LValue::SetObjCIvar(LV, true);
1057 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001058}
1059
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001060CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001061 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001062}