blob: 46e20c99ee37da49a89d3607a73a5a8ce68c235a [file] [log] [blame]
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
Chris Lattnera0fd5ee2008-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 Korobeynikovcd5d08d2008-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 Lattnera0fd5ee2008-03-01 08:50:34 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "CGObjCRuntime.h"
Chris Lattner547907c2008-06-26 04:19:03 +000018#include "CodeGenModule.h"
Daniel Dunbara04840b2008-08-23 03:46:30 +000019#include "CodeGenFunction.h"
Chris Lattner547907c2008-06-26 04:19:03 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000022#include "clang/AST/DeclObjC.h"
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000023#include "llvm/Module.h"
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000024#include "llvm/ADT/SmallVector.h"
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000025#include "llvm/ADT/StringMap.h"
Daniel Dunbarac93e472008-08-15 22:20:32 +000026#include "llvm/Support/Compiler.h"
Daniel Dunbarac93e472008-08-15 22:20:32 +000027#include "llvm/Target/TargetData.h"
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000028#include <map>
Chris Lattner431dc382009-01-27 05:06:01 +000029
30
Chris Lattner547907c2008-06-26 04:19:03 +000031using namespace clang;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000032using namespace CodeGen;
Anton Korobeynikovcd5d08d2008-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 Lattnera0fd5ee2008-03-01 08:50:34 +000039
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000040namespace {
Chris Lattner547907c2008-06-26 04:19:03 +000041class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000042private:
Chris Lattner547907c2008-06-26 04:19:03 +000043 CodeGen::CodeGenModule &CGM;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000044 llvm::Module &TheModule;
Chris Lattner431dc382009-01-27 05:06:01 +000045 const llvm::PointerType *SelectorTy;
46 const llvm::PointerType *PtrToInt8Ty;
Fariborz Jahanianbb284742009-02-04 20:31:19 +000047 const llvm::FunctionType *IMPTy;
Chris Lattner431dc382009-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 Korobeynikovcd5d08d2008-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 Lattner578279d2008-06-26 05:08:00 +000071 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000072 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
73 bool isClassMethodList);
Fariborz Jahanian926c3712009-03-31 18:27:22 +000074 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000075 llvm::Constant *GenerateProtocolList(
76 const llvm::SmallVectorImpl<std::string> &Protocols);
77 llvm::Constant *GenerateClassStructure(
78 llvm::Constant *MetaClass,
79 llvm::Constant *SuperClass,
80 unsigned info,
Chris Lattnerad9c3f32008-06-26 04:47:04 +000081 const char *Name,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000082 llvm::Constant *Version,
83 llvm::Constant *InstanceSize,
84 llvm::Constant *IVars,
85 llvm::Constant *Methods,
86 llvm::Constant *Protocols);
87 llvm::Constant *GenerateProtocolMethodList(
88 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
89 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
90 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
91 &Name="");
92 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
93 std::vector<llvm::Constant*> &V, const std::string &Name="");
94 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
95 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000096public:
Chris Lattner547907c2008-06-26 04:19:03 +000097 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Steve Naroff2c8a08e2009-03-31 16:53:37 +000098 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *);
Daniel Dunbara04840b2008-08-23 03:46:30 +000099 virtual CodeGen::RValue
100 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000101 QualType ResultType,
102 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000103 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000104 bool IsClassMessage,
105 const CallArgList &CallArgs);
Daniel Dunbara04840b2008-08-23 03:46:30 +0000106 virtual CodeGen::RValue
107 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000108 QualType ResultType,
109 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000110 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +0000111 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000112 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000113 bool IsClassMessage,
114 const CallArgList &CallArgs);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000115 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar434627a2008-08-16 00:25:02 +0000116 const ObjCInterfaceDecl *OID);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000117 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Chris Lattnerd71288e2008-06-26 04:37:12 +0000118
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000119 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
120 const ObjCContainerDecl *CD);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000121 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
122 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000123 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000124 const ObjCProtocolDecl *PD);
125 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000126 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbarf7103722008-09-24 03:38:44 +0000127 virtual llvm::Function *GetPropertyGetFunction();
128 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson58d16242008-08-31 04:05:03 +0000129 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlssonb01a2112008-09-09 10:04:29 +0000130
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000131 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
132 const Stmt &S);
Anders Carlssonb01a2112008-09-09 10:04:29 +0000133 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
134 const ObjCAtThrowStmt &S);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000135 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000136 llvm::Value *AddrWeakObj);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000137 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
138 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000139 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
140 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianf310b592008-11-20 19:23:36 +0000141 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
142 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000143 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
144 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000145 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
146 QualType ObjectTy,
147 llvm::Value *BaseValue,
148 const ObjCIvarDecl *Ivar,
149 const FieldDecl *Field,
150 unsigned CVRQualifiers);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +0000151 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
152 ObjCInterfaceDecl *Interface,
153 const ObjCIvarDecl *Ivar);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000154};
155} // end anonymous namespace
156
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000157
158
159static std::string SymbolNameForClass(const std::string &ClassName) {
160 return ".objc_class_" + ClassName;
161}
162
163static std::string SymbolNameForMethod(const std::string &ClassName, const
164 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
165{
166 return "._objc_method_" + ClassName +"("+CategoryName+")"+
167 (isClassMethod ? "+" : "-") + MethodName;
168}
169
Chris Lattner547907c2008-06-26 04:19:03 +0000170CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
171 : CGM(cgm), TheModule(CGM.getModule()) {
Chris Lattner431dc382009-01-27 05:06:01 +0000172 IntTy = cast<llvm::IntegerType>(
173 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
174 LongTy = cast<llvm::IntegerType>(
175 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Chris Lattner547907c2008-06-26 04:19:03 +0000176
Sebastian Redle9e21b32008-12-04 00:10:55 +0000177 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000178 Zeros[1] = Zeros[0];
179 NULLPtr = llvm::ConstantPointerNull::get(
180 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattnerb326b172008-03-30 23:03:07 +0000181 // C string type. Used in lots of places.
182 PtrToInt8Ty =
183 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
184 // Get the selector Type.
Chris Lattner431dc382009-01-27 05:06:01 +0000185 SelectorTy = cast<llvm::PointerType>(
186 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
187
Chris Lattnerb326b172008-03-30 23:03:07 +0000188 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
189 PtrTy = PtrToInt8Ty;
190
191 // Object type
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000192 IdTy = cast<llvm::PointerType>(
193 CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType()));
Chris Lattnerb326b172008-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 Korobeynikovcd5d08d2008-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 Dunbard916e6e2008-11-01 01:53:16 +0000203llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbar434627a2008-08-16 00:25:02 +0000204 const ObjCInterfaceDecl *OID) {
Chris Lattner271d4c22008-11-24 05:29:24 +0000205 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Daniel Dunbar434627a2008-08-16 00:25:02 +0000206 ClassName = Builder.CreateStructGEP(ClassName, 0);
207
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000208 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000209 llvm::Constant *ClassLookupFn =
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000210 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
211 Params,
212 true),
213 "objc_lookup_class");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000214 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattnerb326b172008-03-30 23:03:07 +0000215}
216
Chris Lattnerd71288e2008-06-26 04:37:12 +0000217/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000218llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattnerd71288e2008-06-26 04:37:12 +0000219 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner3a8f2942008-11-24 03:33:13 +0000220 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattnerd71288e2008-06-26 04:37:12 +0000221 if (US == 0)
222 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
223 llvm::GlobalValue::InternalLinkage,
224 ".objc_untyped_selector_alias",
225 NULL, &TheModule);
226
227 return Builder.CreateLoad(US);
228
229}
230
Chris Lattnera5b18882008-06-26 04:44:19 +0000231llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
232 const std::string &Name) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000233 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
234 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
235 llvm::GlobalValue::InternalLinkage,
236 ConstStr, Name, &TheModule);
237 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
238}
239llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
240 std::vector<llvm::Constant*> &V, const std::string &Name) {
241 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
242 return new llvm::GlobalVariable(Ty, false,
243 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
244}
245llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
246 std::vector<llvm::Constant*> &V, const std::string &Name) {
247 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
248 return new llvm::GlobalVariable(Ty, false,
249 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
250}
251
252/// Generate an NSConstantString object.
253//TODO: In case there are any crazy people still using the GNU runtime without
254//an OpenStep implementation, this should let them select their own class for
255//constant strings.
Steve Naroff2c8a08e2009-03-31 16:53:37 +0000256llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) {
257 std::string Str(SL->getString()->getStrData(),
258 SL->getString()->getByteLength());
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000259 std::vector<llvm::Constant*> Ivars;
260 Ivars.push_back(NULLPtr);
Chris Lattnerbac12452008-06-21 21:44:18 +0000261 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000262 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000263 llvm::Constant *ObjCStr = MakeGlobal(
264 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
265 Ivars, ".objc_str");
266 ConstantStrings.push_back(
267 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
268 return ObjCStr;
269}
270
271///Generates a message send where the super is the receiver. This is a message
272///send to self with special delivery semantics indicating which class's method
273///should be called.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000274CodeGen::RValue
275CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000276 QualType ResultType,
277 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000278 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +0000279 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000280 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000281 bool IsClassMessage,
282 const CallArgList &CallArgs) {
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000283 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
284
285 CallArgList ActualArgs;
286
287 ActualArgs.push_back(
288 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
289 CGF.getContext().getObjCIdType()));
290 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
291 CGF.getContext().getObjCSelType()));
292 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
293
294 CodeGenTypes &Types = CGM.getTypes();
295 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
296 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
297
298
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000299 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000300 // TODO: This should be cached, not looked up every time.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000301 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Chris Lattner27e28402009-04-19 01:33:30 +0000302
303
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000304 // Construct the structure used to look up the IMP
305 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
306 IdTy, NULL);
Daniel Dunbara04840b2008-08-23 03:46:30 +0000307 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000308
Daniel Dunbara04840b2008-08-23 03:46:30 +0000309 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
310 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000311
312 // Get the IMP
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000313 std::vector<const llvm::Type*> Params;
314 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
315 Params.push_back(SelectorTy);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000316 llvm::Constant *lookupFunction =
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000317 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
318 llvm::PointerType::getUnqual(impType), Params, true),
319 "objc_msg_lookup_super");
320
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000321 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbara04840b2008-08-23 03:46:30 +0000322 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000323 lookupArgs+2);
324
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000325 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000326}
327
328/// Generate code for a message send expression.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000329CodeGen::RValue
330CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000331 QualType ResultType,
332 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000333 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000334 bool IsClassMessage,
335 const CallArgList &CallArgs) {
Daniel Dunbardd851282008-08-30 05:35:15 +0000336 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000337 CallArgList ActualArgs;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000338
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000339 ActualArgs.push_back(
340 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
341 CGF.getContext().getObjCIdType()));
342 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
343 CGF.getContext().getObjCSelType()));
344 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
345
346 CodeGenTypes &Types = CGM.getTypes();
347 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
348 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
349
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000350 std::vector<const llvm::Type*> Params;
351 Params.push_back(Receiver->getType());
352 Params.push_back(SelectorTy);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000353 llvm::Constant *lookupFunction =
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000354 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
355 llvm::PointerType::getUnqual(impType), Params, true),
356 "objc_msg_lookup");
357
Daniel Dunbara04840b2008-08-23 03:46:30 +0000358 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner7db5e942008-05-06 00:56:42 +0000359
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000360 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000361}
362
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000363/// Generates a MethodList. Used in construction of a objc_class and
364/// objc_category structures.
365llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattner578279d2008-06-26 05:08:00 +0000366 const std::string &CategoryName,
367 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000368 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
369 bool isClassMethodList) {
370 // Get the method structure type.
371 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
372 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
373 PtrToInt8Ty, // Method types
374 llvm::PointerType::getUnqual(IMPTy), //Method pointer
375 NULL);
376 std::vector<llvm::Constant*> Methods;
377 std::vector<llvm::Constant*> Elements;
378 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
379 Elements.clear();
Chris Lattner3a8f2942008-11-24 03:33:13 +0000380 llvm::Constant *C =
381 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattner578279d2008-06-26 05:08:00 +0000382 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000383 Elements.push_back(
384 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
385 llvm::Constant *Method =
386 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner3a8f2942008-11-24 03:33:13 +0000387 MethodSels[i].getAsString(),
Chris Lattnere7581092008-06-26 04:05:20 +0000388 isClassMethodList));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000389 Method = llvm::ConstantExpr::getBitCast(Method,
390 llvm::PointerType::getUnqual(IMPTy));
391 Elements.push_back(Method);
392 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
393 }
394
395 // Array of method structures
396 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattner578279d2008-06-26 05:08:00 +0000397 MethodSels.size());
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000398 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerbcb3e862008-06-26 04:52:29 +0000399 Methods);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000400
401 // Structure containing list pointer, array and array count
402 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
403 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
404 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
405 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
406 IntTy,
407 ObjCMethodArrayTy,
408 NULL);
409 // Refine next pointer type to concrete type
410 llvm::cast<llvm::OpaqueType>(
411 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
412 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
413
414 Methods.clear();
415 Methods.push_back(llvm::ConstantPointerNull::get(
416 llvm::PointerType::getUnqual(ObjCMethodListTy)));
417 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
418 MethodTypes.size()));
419 Methods.push_back(MethodArray);
420
421 // Create an instance of the structure
422 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
423}
424
425/// Generates an IvarList. Used in construction of a objc_class.
426llvm::Constant *CGObjCGNU::GenerateIvarList(
427 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
428 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
429 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
430 // Get the method structure type.
431 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
432 PtrToInt8Ty,
433 PtrToInt8Ty,
434 IntTy,
435 NULL);
436 std::vector<llvm::Constant*> Ivars;
437 std::vector<llvm::Constant*> Elements;
438 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
439 Elements.clear();
440 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
441 Zeros, 2));
442 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
443 Zeros, 2));
444 Elements.push_back(IvarOffsets[i]);
445 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
446 }
447
448 // Array of method structures
449 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
450 IvarNames.size());
451
452
453 Elements.clear();
Chris Lattner431dc382009-01-27 05:06:01 +0000454 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000455 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
456 // Structure containing array and array count
457 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
458 ObjCIvarArrayTy,
459 NULL);
460
461 // Create an instance of the structure
462 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
463}
464
465/// Generate a class structure
466llvm::Constant *CGObjCGNU::GenerateClassStructure(
467 llvm::Constant *MetaClass,
468 llvm::Constant *SuperClass,
469 unsigned info,
Chris Lattnerad9c3f32008-06-26 04:47:04 +0000470 const char *Name,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000471 llvm::Constant *Version,
472 llvm::Constant *InstanceSize,
473 llvm::Constant *IVars,
474 llvm::Constant *Methods,
475 llvm::Constant *Protocols) {
476 // Set up the class structure
477 // Note: Several of these are char*s when they should be ids. This is
478 // because the runtime performs this translation on load.
479 llvm::StructType *ClassTy = llvm::StructType::get(
480 PtrToInt8Ty, // class_pointer
481 PtrToInt8Ty, // super_class
482 PtrToInt8Ty, // name
483 LongTy, // version
484 LongTy, // info
485 LongTy, // instance_size
486 IVars->getType(), // ivars
487 Methods->getType(), // methods
488 // These are all filled in by the runtime, so we pretend
489 PtrTy, // dtable
490 PtrTy, // subclass_list
491 PtrTy, // sibling_class
492 PtrTy, // protocols
493 PtrTy, // gc_object_type
494 NULL);
495 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
496 llvm::Constant *NullP =
Chris Lattner431dc382009-01-27 05:06:01 +0000497 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000498 // Fill in the structure
499 std::vector<llvm::Constant*> Elements;
500 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
501 Elements.push_back(SuperClass);
Chris Lattnerad9c3f32008-06-26 04:47:04 +0000502 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000503 Elements.push_back(Zero);
504 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
505 Elements.push_back(InstanceSize);
506 Elements.push_back(IVars);
507 Elements.push_back(Methods);
508 Elements.push_back(NullP);
509 Elements.push_back(NullP);
510 Elements.push_back(NullP);
511 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
512 Elements.push_back(NullP);
513 // Create an instance of the structure
Chris Lattner4d018542008-07-21 06:31:05 +0000514 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000515}
516
517llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
518 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
519 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
520 // Get the method structure type.
521 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
522 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
523 PtrToInt8Ty,
524 NULL);
525 std::vector<llvm::Constant*> Methods;
526 std::vector<llvm::Constant*> Elements;
527 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
528 Elements.clear();
529 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
530 Zeros, 2));
531 Elements.push_back(
532 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
533 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
534 }
535 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
536 MethodNames.size());
537 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
538 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
539 IntTy, ObjCMethodArrayTy, NULL);
540 Methods.clear();
541 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
542 Methods.push_back(Array);
543 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
544}
545// Create the protocol list structure used in classes, categories and so on
546llvm::Constant *CGObjCGNU::GenerateProtocolList(
547 const llvm::SmallVectorImpl<std::string> &Protocols) {
548 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
549 Protocols.size());
550 llvm::StructType *ProtocolListTy = llvm::StructType::get(
551 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
552 LongTy,//FIXME: Should be size_t
553 ProtocolArrayTy,
554 NULL);
555 std::vector<llvm::Constant*> Elements;
556 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
557 iter != endIter ; iter++) {
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000558 llvm::Constant *protocol = ExistingProtocols[*iter];
559 if (!protocol)
560 protocol = GenerateEmptyProtocol(*iter);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000561 llvm::Constant *Ptr =
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000562 llvm::ConstantExpr::getBitCast(protocol, PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000563 Elements.push_back(Ptr);
564 }
565 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
566 Elements);
567 Elements.clear();
568 Elements.push_back(NULLPtr);
Chris Lattner431dc382009-01-27 05:06:01 +0000569 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000570 Elements.push_back(ProtocolArray);
571 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
572}
573
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000574llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000575 const ObjCProtocolDecl *PD) {
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000576 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
577 const llvm::Type *T =
578 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
579 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
580}
581
582llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
583 const std::string &ProtocolName) {
584 llvm::SmallVector<std::string, 0> EmptyStringVector;
585 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
586
587 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
588 llvm::Constant *InstanceMethodList =
589 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
590 llvm::Constant *ClassMethodList =
591 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
592 // Protocols are objects containing lists of the methods implemented and
593 // protocols adopted.
594 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
595 PtrToInt8Ty,
596 ProtocolList->getType(),
597 InstanceMethodList->getType(),
598 ClassMethodList->getType(),
599 NULL);
600 std::vector<llvm::Constant*> Elements;
601 // The isa pointer must be set to a magic number so the runtime knows it's
602 // the correct layout.
603 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
604 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
605 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
606 Elements.push_back(ProtocolList);
607 Elements.push_back(InstanceMethodList);
608 Elements.push_back(ClassMethodList);
609 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000610}
611
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000612void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
613 ASTContext &Context = CGM.getContext();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000614 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000615 llvm::SmallVector<std::string, 16> Protocols;
616 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
617 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattner271d4c22008-11-24 05:29:24 +0000618 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000619 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
620 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000621 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(Context),
622 E = PD->instmeth_end(Context); iter != E; iter++) {
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000623 std::string TypeStr;
624 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
625 InstanceMethodNames.push_back(
Chris Lattner3a8f2942008-11-24 03:33:13 +0000626 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000627 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000628 }
629 // Collect information about class methods:
630 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
631 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000632 for (ObjCProtocolDecl::classmeth_iterator
633 iter = PD->classmeth_begin(Context),
634 endIter = PD->classmeth_end(Context) ; iter != endIter ; iter++) {
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000635 std::string TypeStr;
636 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
637 ClassMethodNames.push_back(
Chris Lattner3a8f2942008-11-24 03:33:13 +0000638 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000639 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000640 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000641
642 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
643 llvm::Constant *InstanceMethodList =
644 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
645 llvm::Constant *ClassMethodList =
646 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
647 // Protocols are objects containing lists of the methods implemented and
648 // protocols adopted.
649 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
650 PtrToInt8Ty,
651 ProtocolList->getType(),
652 InstanceMethodList->getType(),
653 ClassMethodList->getType(),
654 NULL);
655 std::vector<llvm::Constant*> Elements;
656 // The isa pointer must be set to a magic number so the runtime knows it's
657 // the correct layout.
658 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
659 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
660 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
661 Elements.push_back(ProtocolList);
662 Elements.push_back(InstanceMethodList);
663 Elements.push_back(ClassMethodList);
664 ExistingProtocols[ProtocolName] =
665 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
666 ".objc_protocol"), IdTy);
667}
668
Daniel Dunbarac93e472008-08-15 22:20:32 +0000669void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000670 std::string ClassName = OCD->getClassInterface()->getNameAsString();
671 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000672 // Collect information about instance methods
673 llvm::SmallVector<Selector, 16> InstanceMethodSels;
674 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Steve Naroffab63fd62009-01-08 17:28:14 +0000675 for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(),
Daniel Dunbarac93e472008-08-15 22:20:32 +0000676 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
677 InstanceMethodSels.push_back((*iter)->getSelector());
678 std::string TypeStr;
679 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
680 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
681 }
682
683 // Collect information about class methods
684 llvm::SmallVector<Selector, 16> ClassMethodSels;
685 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Steve Naroffab63fd62009-01-08 17:28:14 +0000686 for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(),
Daniel Dunbarac93e472008-08-15 22:20:32 +0000687 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
688 ClassMethodSels.push_back((*iter)->getSelector());
689 std::string TypeStr;
690 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
691 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
692 }
693
694 // Collect the names of referenced protocols
695 llvm::SmallVector<std::string, 16> Protocols;
696 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
697 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
698 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
699 E = Protos.end(); I != E; ++I)
Chris Lattner271d4c22008-11-24 05:29:24 +0000700 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000701
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000702 std::vector<llvm::Constant*> Elements;
703 Elements.push_back(MakeConstantString(CategoryName));
704 Elements.push_back(MakeConstantString(ClassName));
705 // Instance method list
706 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattner578279d2008-06-26 05:08:00 +0000707 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000708 false), PtrTy));
709 // Class method list
710 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattner578279d2008-06-26 05:08:00 +0000711 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000712 PtrTy));
713 // Protocol list
714 Elements.push_back(llvm::ConstantExpr::getBitCast(
715 GenerateProtocolList(Protocols), PtrTy));
716 Categories.push_back(llvm::ConstantExpr::getBitCast(
717 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
718 PtrTy, PtrTy, NULL), Elements), PtrTy));
719}
Daniel Dunbarac93e472008-08-15 22:20:32 +0000720
721void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
722 ASTContext &Context = CGM.getContext();
723
724 // Get the superclass name.
725 const ObjCInterfaceDecl * SuperClassDecl =
726 OID->getClassInterface()->getSuperClass();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000727 std::string SuperClassName;
728 if (SuperClassDecl)
729 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000730
731 // Get the class name
Chris Lattnerb63b5502009-04-01 02:00:48 +0000732 ObjCInterfaceDecl *ClassDecl =
733 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000734 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000735
736 // Get the size of instances. For runtimes that support late-bound instances
737 // this should probably be something different (size just of instance
738 // varaibles in this class, not superclasses?).
Chris Lattner9fe470d2009-04-19 06:02:28 +0000739 const llvm::Type *ObjTy;
740
741 if (ClassDecl->isForwardDecl())
742 ObjTy = llvm::StructType::get(NULL, NULL);
743 else
744 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
Chris Lattnerb63b5502009-04-01 02:00:48 +0000745 int instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000746
747 // Collect information about instance variables.
748 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
749 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
750 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
751 const llvm::StructLayout *Layout =
752 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
753 ObjTy = llvm::PointerType::getUnqual(ObjTy);
754 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
755 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
756 // Store the name
Chris Lattner271d4c22008-11-24 05:29:24 +0000757 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
758 ->getNameAsString()));
Daniel Dunbarac93e472008-08-15 22:20:32 +0000759 // Get the type encoding for this ivar
760 std::string TypeStr;
Daniel Dunbarc9197cd2008-10-17 20:21:44 +0000761 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000762 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
763 // Get the offset
Daniel Dunbarf0587c62009-04-20 00:37:55 +0000764 const FieldDecl *Field =
765 ClassDecl->lookupFieldDeclForIvar(Context, (*iter));
Daniel Dunbarac93e472008-08-15 22:20:32 +0000766 int offset =
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000767 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbarac93e472008-08-15 22:20:32 +0000768 IvarOffsets.push_back(
769 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
770 }
771
772 // Collect information about instance methods
773 llvm::SmallVector<Selector, 16> InstanceMethodSels;
774 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
775 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
776 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
777 InstanceMethodSels.push_back((*iter)->getSelector());
778 std::string TypeStr;
779 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
780 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
781 }
782
783 // Collect information about class methods
784 llvm::SmallVector<Selector, 16> ClassMethodSels;
785 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
786 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
787 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
788 ClassMethodSels.push_back((*iter)->getSelector());
789 std::string TypeStr;
790 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
791 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
792 }
793 // Collect the names of referenced protocols
794 llvm::SmallVector<std::string, 16> Protocols;
795 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
796 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
797 E = Protos.end(); I != E; ++I)
Chris Lattner271d4c22008-11-24 05:29:24 +0000798 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000799
800
801
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000802 // Get the superclass pointer.
803 llvm::Constant *SuperClass;
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000804 if (!SuperClassName.empty()) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000805 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
806 } else {
Chris Lattner431dc382009-01-27 05:06:01 +0000807 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000808 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000809 // Empty vector used to construct empty method lists
810 llvm::SmallVector<llvm::Constant*, 1> empty;
811 // Generate the method and instance variable lists
812 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattner578279d2008-06-26 05:08:00 +0000813 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000814 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattner578279d2008-06-26 05:08:00 +0000815 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000816 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
817 IvarOffsets);
818 //Generate metaclass for class methods
819 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner4d018542008-07-21 06:31:05 +0000820 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000821 empty, empty, empty), ClassMethodList, NULLPtr);
822 // Generate the class structure
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000823 llvm::Constant *ClassStruct =
824 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
825 ClassName.c_str(), 0,
Sebastian Redle9e21b32008-12-04 00:10:55 +0000826 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000827 MethodList, GenerateProtocolList(Protocols));
828 // Add class structure to list to be added to the symtab later
829 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
830 Classes.push_back(ClassStruct);
831}
832
833llvm::Function *CGObjCGNU::ModuleInitFunction() {
834 // Only emit an ObjC load function if no Objective-C stuff has been called
835 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
836 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikovb41e4972008-06-01 15:14:46 +0000837 UntypedSelectors.empty())
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000838 return NULL;
Eli Friedman7719bc82008-06-01 16:00:02 +0000839
Chris Lattner431dc382009-01-27 05:06:01 +0000840 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
841 SelectorTy->getElementType());
842 const llvm::Type *SelStructPtrTy = SelectorTy;
843 bool isSelOpaque = false;
844 if (SelStructTy == 0) {
845 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
846 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
847 isSelOpaque = true;
848 }
849
Eli Friedman7719bc82008-06-01 16:00:02 +0000850 // Name the ObjC types to make the IR a bit easier to read
Chris Lattner431dc382009-01-27 05:06:01 +0000851 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman7719bc82008-06-01 16:00:02 +0000852 TheModule.addTypeName(".objc_id", IdTy);
853 TheModule.addTypeName(".objc_imp", IMPTy);
854
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000855 std::vector<llvm::Constant*> Elements;
856 // Generate statics list:
857 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
858 ConstantStrings.size() + 1);
859 ConstantStrings.push_back(NULLPtr);
860 Elements.push_back(MakeConstantString("NSConstantString",
861 ".objc_static_class_name"));
862 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
863 llvm::StructType *StaticsListTy =
864 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattneref843042008-06-26 04:10:42 +0000865 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000866 llvm::Constant *Statics =
867 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner60eaae82008-06-24 17:01:28 +0000868 llvm::ArrayType *StaticsListArrayTy =
Chris Lattneref843042008-06-26 04:10:42 +0000869 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner60eaae82008-06-24 17:01:28 +0000870 Elements.clear();
871 Elements.push_back(Statics);
Chris Lattneref843042008-06-26 04:10:42 +0000872 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner60eaae82008-06-24 17:01:28 +0000873 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000874 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
875 // Array of classes, categories, and constant objects
876 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
877 Classes.size() + Categories.size() + 2);
Chris Lattner431dc382009-01-27 05:06:01 +0000878 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Chris Lattneref843042008-06-26 04:10:42 +0000879 llvm::Type::Int16Ty,
880 llvm::Type::Int16Ty,
881 ClassListTy, NULL);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000882
883 Elements.clear();
884 // Pointer to an array of selectors used in this module.
885 std::vector<llvm::Constant*> Selectors;
886 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
887 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
888 iter != iterEnd ; ++iter) {
Chris Lattneref843042008-06-26 04:10:42 +0000889 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
890 Elements.push_back(MakeConstantString(iter->first.second,
891 ".objc_sel_types"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000892 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
893 Elements.clear();
894 }
895 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
896 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattneref843042008-06-26 04:10:42 +0000897 iter != iterEnd; ++iter) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000898 Elements.push_back(
Chris Lattneref843042008-06-26 04:10:42 +0000899 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000900 Elements.push_back(NULLPtr);
901 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
902 Elements.clear();
903 }
904 Elements.push_back(NULLPtr);
905 Elements.push_back(NULLPtr);
906 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
907 Elements.clear();
908 // Number of static selectors
909 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
910 llvm::Constant *SelectorList = MakeGlobal(
911 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
912 ".objc_selector_list");
Chris Lattner431dc382009-01-27 05:06:01 +0000913 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
914 SelStructPtrTy));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000915
916 // Now that all of the static selectors exist, create pointers to them.
917 int index = 0;
918 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
919 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
920 iter != iterEnd; ++iter) {
921 llvm::Constant *Idxs[] = {Zeros[0],
922 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattner431dc382009-01-27 05:06:01 +0000923 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
924 true, llvm::GlobalValue::InternalLinkage,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000925 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
926 ".objc_sel_ptr", &TheModule);
Chris Lattner431dc382009-01-27 05:06:01 +0000927 // If selectors are defined as an opaque type, cast the pointer to this
928 // type.
929 if (isSelOpaque) {
930 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
931 llvm::PointerType::getUnqual(SelectorTy));
932 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000933 (*iter).second->setAliasee(SelPtr);
934 }
935 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
936 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
937 iter != iterEnd; iter++) {
938 llvm::Constant *Idxs[] = {Zeros[0],
939 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattner431dc382009-01-27 05:06:01 +0000940 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000941 llvm::GlobalValue::InternalLinkage,
942 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
943 ".objc_sel_ptr", &TheModule);
Chris Lattner431dc382009-01-27 05:06:01 +0000944 // If selectors are defined as an opaque type, cast the pointer to this
945 // type.
946 if (isSelOpaque) {
947 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
948 llvm::PointerType::getUnqual(SelectorTy));
949 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000950 (*iter).second->setAliasee(SelPtr);
951 }
952 // Number of classes defined.
953 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
954 Classes.size()));
955 // Number of categories defined
956 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
957 Categories.size()));
958 // Create an array of classes, then categories, then static object instances
959 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
960 // NULL-terminated list of static object instances (mainly constant strings)
961 Classes.push_back(Statics);
962 Classes.push_back(NULLPtr);
963 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
964 Elements.push_back(ClassList);
965 // Construct the symbol table
966 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
967
968 // The symbol table is contained in a module which has some version-checking
969 // constants
970 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
971 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
972 Elements.clear();
973 // Runtime version used for compatibility checking.
974 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahaniandefa06a2009-04-01 19:49:42 +0000975 // sizeof(ModuleTy)
976 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
977 Elements.push_back(llvm::ConstantInt::get(LongTy, td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000978 //FIXME: Should be the path to the file where this module was declared
979 Elements.push_back(NULLPtr);
980 Elements.push_back(SymTab);
981 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
982
983 // Create the load function calling the runtime entry point with the module
984 // structure
985 std::vector<const llvm::Type*> VoidArgs;
986 llvm::Function * LoadFunction = llvm::Function::Create(
987 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
988 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
989 &TheModule);
990 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000991 CGBuilderTy Builder;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000992 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000993
994 std::vector<const llvm::Type*> Params(1,
995 llvm::PointerType::getUnqual(ModuleTy));
996 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
997 llvm::Type::VoidTy, Params, true), "__objc_exec_class");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000998 Builder.CreateCall(Register, Module);
999 Builder.CreateRetVoid();
1000 return LoadFunction;
1001}
Daniel Dunbarac93e472008-08-15 22:20:32 +00001002
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00001003llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
1004 const ObjCContainerDecl *CD) {
Daniel Dunbarac93e472008-08-15 22:20:32 +00001005 const ObjCCategoryImplDecl *OCD =
Steve Naroff438be772009-01-08 19:41:02 +00001006 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner3a8f2942008-11-24 03:33:13 +00001007 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1008 std::string ClassName = OMD->getClassInterface()->getNameAsString();
1009 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregor5d764842009-01-09 17:18:27 +00001010 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbarac93e472008-08-15 22:20:32 +00001011
Daniel Dunbar34bda882009-02-02 23:23:47 +00001012 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001013 const llvm::FunctionType *MethodTy =
Daniel Dunbar34bda882009-02-02 23:23:47 +00001014 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001015 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1016 MethodName, isClassMethod);
1017
Gabor Greif815e2c12008-04-06 20:42:52 +00001018 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattnerb326b172008-03-30 23:03:07 +00001019 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001020 FunctionName,
Chris Lattnerb326b172008-03-30 23:03:07 +00001021 &TheModule);
Chris Lattnerb326b172008-03-30 23:03:07 +00001022 return Method;
1023}
1024
Daniel Dunbarf7103722008-09-24 03:38:44 +00001025llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
1026 return 0;
1027}
1028
1029llvm::Function *CGObjCGNU::GetPropertySetFunction() {
1030 return 0;
1031}
1032
1033llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian6158e692009-03-30 18:02:14 +00001034 std::vector<const llvm::Type*> Params(1, IdTy);
1035 return cast<llvm::Function>(CGM.CreateRuntimeFunction(
1036 llvm::FunctionType::get(llvm::Type::VoidTy, Params, true),
1037 "objc_enumerationMutation"));
Anders Carlsson58d16242008-08-31 04:05:03 +00001038}
1039
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001040void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1041 const Stmt &S) {
1042 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlssonb01a2112008-09-09 10:04:29 +00001043}
1044
1045void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbarf7103722008-09-24 03:38:44 +00001046 const ObjCAtThrowStmt &S) {
Anders Carlssonb01a2112008-09-09 10:04:29 +00001047 CGF.ErrorUnsupported(&S, "@throw statement");
1048}
1049
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00001050llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00001051 llvm::Value *AddrWeakObj)
1052{
1053 return 0;
1054}
1055
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00001056void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1057 llvm::Value *src, llvm::Value *dst)
1058{
1059 return;
1060}
1061
Fariborz Jahanian17958902008-11-19 00:59:10 +00001062void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1063 llvm::Value *src, llvm::Value *dst)
1064{
1065 return;
1066}
1067
Fariborz Jahanianf310b592008-11-20 19:23:36 +00001068void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1069 llvm::Value *src, llvm::Value *dst)
1070{
1071 return;
1072}
1073
Fariborz Jahanian17958902008-11-19 00:59:10 +00001074void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1075 llvm::Value *src, llvm::Value *dst)
1076{
1077 return;
1078}
1079
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001080LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1081 QualType ObjectTy,
1082 llvm::Value *BaseValue,
1083 const ObjCIvarDecl *Ivar,
1084 const FieldDecl *Field,
1085 unsigned CVRQualifiers) {
Daniel Dunbar0ac5bbc2009-04-21 00:41:40 +00001086 assert(Field == ObjectTy->getAsObjCInterfaceType()->getDecl()->lookupFieldDeclForIvar(CGM.getContext(), Ivar));
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001087 if (Ivar->isBitField())
1088 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
1089 CVRQualifiers);
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00001090 // TODO: Add a special case for isa (index 0)
1091 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
1092 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001093 LValue LV = LValue::MakeAddr(V,
1094 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
1095 LValue::SetObjCIvar(LV, true);
1096 return LV;
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00001097}
1098
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001099llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
1100 ObjCInterfaceDecl *Interface,
1101 const ObjCIvarDecl *Ivar) {
1102 const llvm::Type *InterfaceLTy =
1103 CGM.getTypes().ConvertType(
1104 CGM.getContext().getObjCInterfaceType(Interface));
1105 const llvm::StructLayout *Layout =
1106 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
Daniel Dunbarf0587c62009-04-20 00:37:55 +00001107 const FieldDecl *Field =
1108 Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001109 uint64_t Offset =
1110 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
1111
1112 return llvm::ConstantInt::get(
1113 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
1114 Offset);
1115}
1116
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001117CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattner547907c2008-06-26 04:19:03 +00001118 return new CGObjCGNU(CGM);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +00001119}