blob: 98102ae9cd3b6caa04aa6d87905d125d8578a3fc [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,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000149 unsigned CVRQualifiers);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +0000150 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +0000151 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +0000152 const ObjCIvarDecl *Ivar);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000153};
154} // end anonymous namespace
155
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000156
157
158static std::string SymbolNameForClass(const std::string &ClassName) {
159 return ".objc_class_" + ClassName;
160}
161
162static std::string SymbolNameForMethod(const std::string &ClassName, const
163 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
164{
165 return "._objc_method_" + ClassName +"("+CategoryName+")"+
166 (isClassMethod ? "+" : "-") + MethodName;
167}
168
Chris Lattner547907c2008-06-26 04:19:03 +0000169CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
170 : CGM(cgm), TheModule(CGM.getModule()) {
Chris Lattner431dc382009-01-27 05:06:01 +0000171 IntTy = cast<llvm::IntegerType>(
172 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
173 LongTy = cast<llvm::IntegerType>(
174 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Chris Lattner547907c2008-06-26 04:19:03 +0000175
Sebastian Redle9e21b32008-12-04 00:10:55 +0000176 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000177 Zeros[1] = Zeros[0];
178 NULLPtr = llvm::ConstantPointerNull::get(
179 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattnerb326b172008-03-30 23:03:07 +0000180 // C string type. Used in lots of places.
181 PtrToInt8Ty =
182 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
183 // Get the selector Type.
Chris Lattner431dc382009-01-27 05:06:01 +0000184 SelectorTy = cast<llvm::PointerType>(
185 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
186
Chris Lattnerb326b172008-03-30 23:03:07 +0000187 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
188 PtrTy = PtrToInt8Ty;
189
190 // Object type
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000191 IdTy = cast<llvm::PointerType>(
192 CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType()));
Chris Lattnerb326b172008-03-30 23:03:07 +0000193
194 // IMP type
195 std::vector<const llvm::Type*> IMPArgs;
196 IMPArgs.push_back(IdTy);
197 IMPArgs.push_back(SelectorTy);
198 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000199}
200// This has to perform the lookup every time, since posing and related
201// techniques can modify the name -> class mapping.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000202llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbar434627a2008-08-16 00:25:02 +0000203 const ObjCInterfaceDecl *OID) {
Chris Lattner271d4c22008-11-24 05:29:24 +0000204 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Daniel Dunbar434627a2008-08-16 00:25:02 +0000205 ClassName = Builder.CreateStructGEP(ClassName, 0);
206
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000207 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000208 llvm::Constant *ClassLookupFn =
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000209 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
210 Params,
211 true),
212 "objc_lookup_class");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000213 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattnerb326b172008-03-30 23:03:07 +0000214}
215
Chris Lattnerd71288e2008-06-26 04:37:12 +0000216/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000217llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattnerd71288e2008-06-26 04:37:12 +0000218 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner3a8f2942008-11-24 03:33:13 +0000219 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattnerd71288e2008-06-26 04:37:12 +0000220 if (US == 0)
221 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
222 llvm::GlobalValue::InternalLinkage,
223 ".objc_untyped_selector_alias",
224 NULL, &TheModule);
225
226 return Builder.CreateLoad(US);
227
228}
229
Chris Lattnera5b18882008-06-26 04:44:19 +0000230llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
231 const std::string &Name) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000232 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
233 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
234 llvm::GlobalValue::InternalLinkage,
235 ConstStr, Name, &TheModule);
236 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
237}
238llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
239 std::vector<llvm::Constant*> &V, const std::string &Name) {
240 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
241 return new llvm::GlobalVariable(Ty, false,
242 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
243}
244llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
245 std::vector<llvm::Constant*> &V, const std::string &Name) {
246 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
247 return new llvm::GlobalVariable(Ty, false,
248 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
249}
250
251/// Generate an NSConstantString object.
252//TODO: In case there are any crazy people still using the GNU runtime without
253//an OpenStep implementation, this should let them select their own class for
254//constant strings.
Steve Naroff2c8a08e2009-03-31 16:53:37 +0000255llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) {
256 std::string Str(SL->getString()->getStrData(),
257 SL->getString()->getByteLength());
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000258 std::vector<llvm::Constant*> Ivars;
259 Ivars.push_back(NULLPtr);
Chris Lattnerbac12452008-06-21 21:44:18 +0000260 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000261 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000262 llvm::Constant *ObjCStr = MakeGlobal(
263 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
264 Ivars, ".objc_str");
265 ConstantStrings.push_back(
266 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
267 return ObjCStr;
268}
269
270///Generates a message send where the super is the receiver. This is a message
271///send to self with special delivery semantics indicating which class's method
272///should be called.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000273CodeGen::RValue
274CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000275 QualType ResultType,
276 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000277 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +0000278 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000279 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000280 bool IsClassMessage,
281 const CallArgList &CallArgs) {
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000282 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
283
284 CallArgList ActualArgs;
285
286 ActualArgs.push_back(
287 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
288 CGF.getContext().getObjCIdType()));
289 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
290 CGF.getContext().getObjCSelType()));
291 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
292
293 CodeGenTypes &Types = CGM.getTypes();
294 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
295 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
296
297
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000298 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000299 // TODO: This should be cached, not looked up every time.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000300 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Chris Lattner27e28402009-04-19 01:33:30 +0000301
302
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000303 // Construct the structure used to look up the IMP
304 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
305 IdTy, NULL);
Daniel Dunbara04840b2008-08-23 03:46:30 +0000306 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000307
Daniel Dunbara04840b2008-08-23 03:46:30 +0000308 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
309 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000310
311 // Get the IMP
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000312 std::vector<const llvm::Type*> Params;
313 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
314 Params.push_back(SelectorTy);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000315 llvm::Constant *lookupFunction =
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000316 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
317 llvm::PointerType::getUnqual(impType), Params, true),
318 "objc_msg_lookup_super");
319
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000320 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbara04840b2008-08-23 03:46:30 +0000321 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000322 lookupArgs+2);
323
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000324 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000325}
326
327/// Generate code for a message send expression.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000328CodeGen::RValue
329CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000330 QualType ResultType,
331 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000332 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000333 bool IsClassMessage,
334 const CallArgList &CallArgs) {
Daniel Dunbardd851282008-08-30 05:35:15 +0000335 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000336 CallArgList ActualArgs;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000337
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000338 ActualArgs.push_back(
339 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
340 CGF.getContext().getObjCIdType()));
341 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
342 CGF.getContext().getObjCSelType()));
343 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
344
345 CodeGenTypes &Types = CGM.getTypes();
346 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
347 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
348
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000349 std::vector<const llvm::Type*> Params;
350 Params.push_back(Receiver->getType());
351 Params.push_back(SelectorTy);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000352 llvm::Constant *lookupFunction =
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000353 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
354 llvm::PointerType::getUnqual(impType), Params, true),
355 "objc_msg_lookup");
356
Daniel Dunbara04840b2008-08-23 03:46:30 +0000357 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner7db5e942008-05-06 00:56:42 +0000358
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000359 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000360}
361
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000362/// Generates a MethodList. Used in construction of a objc_class and
363/// objc_category structures.
364llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattner578279d2008-06-26 05:08:00 +0000365 const std::string &CategoryName,
366 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000367 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
368 bool isClassMethodList) {
369 // Get the method structure type.
370 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
371 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
372 PtrToInt8Ty, // Method types
373 llvm::PointerType::getUnqual(IMPTy), //Method pointer
374 NULL);
375 std::vector<llvm::Constant*> Methods;
376 std::vector<llvm::Constant*> Elements;
377 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
378 Elements.clear();
Chris Lattner3a8f2942008-11-24 03:33:13 +0000379 llvm::Constant *C =
380 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattner578279d2008-06-26 05:08:00 +0000381 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000382 Elements.push_back(
383 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
384 llvm::Constant *Method =
385 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner3a8f2942008-11-24 03:33:13 +0000386 MethodSels[i].getAsString(),
Chris Lattnere7581092008-06-26 04:05:20 +0000387 isClassMethodList));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000388 Method = llvm::ConstantExpr::getBitCast(Method,
389 llvm::PointerType::getUnqual(IMPTy));
390 Elements.push_back(Method);
391 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
392 }
393
394 // Array of method structures
395 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattner578279d2008-06-26 05:08:00 +0000396 MethodSels.size());
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000397 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerbcb3e862008-06-26 04:52:29 +0000398 Methods);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000399
400 // Structure containing list pointer, array and array count
401 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
402 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
403 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
404 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
405 IntTy,
406 ObjCMethodArrayTy,
407 NULL);
408 // Refine next pointer type to concrete type
409 llvm::cast<llvm::OpaqueType>(
410 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
411 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
412
413 Methods.clear();
414 Methods.push_back(llvm::ConstantPointerNull::get(
415 llvm::PointerType::getUnqual(ObjCMethodListTy)));
416 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
417 MethodTypes.size()));
418 Methods.push_back(MethodArray);
419
420 // Create an instance of the structure
421 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
422}
423
424/// Generates an IvarList. Used in construction of a objc_class.
425llvm::Constant *CGObjCGNU::GenerateIvarList(
426 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
427 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
428 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
429 // Get the method structure type.
430 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
431 PtrToInt8Ty,
432 PtrToInt8Ty,
433 IntTy,
434 NULL);
435 std::vector<llvm::Constant*> Ivars;
436 std::vector<llvm::Constant*> Elements;
437 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
438 Elements.clear();
439 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
440 Zeros, 2));
441 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
442 Zeros, 2));
443 Elements.push_back(IvarOffsets[i]);
444 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
445 }
446
447 // Array of method structures
448 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
449 IvarNames.size());
450
451
452 Elements.clear();
Chris Lattner431dc382009-01-27 05:06:01 +0000453 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000454 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
455 // Structure containing array and array count
456 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
457 ObjCIvarArrayTy,
458 NULL);
459
460 // Create an instance of the structure
461 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
462}
463
464/// Generate a class structure
465llvm::Constant *CGObjCGNU::GenerateClassStructure(
466 llvm::Constant *MetaClass,
467 llvm::Constant *SuperClass,
468 unsigned info,
Chris Lattnerad9c3f32008-06-26 04:47:04 +0000469 const char *Name,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000470 llvm::Constant *Version,
471 llvm::Constant *InstanceSize,
472 llvm::Constant *IVars,
473 llvm::Constant *Methods,
474 llvm::Constant *Protocols) {
475 // Set up the class structure
476 // Note: Several of these are char*s when they should be ids. This is
477 // because the runtime performs this translation on load.
478 llvm::StructType *ClassTy = llvm::StructType::get(
479 PtrToInt8Ty, // class_pointer
480 PtrToInt8Ty, // super_class
481 PtrToInt8Ty, // name
482 LongTy, // version
483 LongTy, // info
484 LongTy, // instance_size
485 IVars->getType(), // ivars
486 Methods->getType(), // methods
487 // These are all filled in by the runtime, so we pretend
488 PtrTy, // dtable
489 PtrTy, // subclass_list
490 PtrTy, // sibling_class
491 PtrTy, // protocols
492 PtrTy, // gc_object_type
493 NULL);
494 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
495 llvm::Constant *NullP =
Chris Lattner431dc382009-01-27 05:06:01 +0000496 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000497 // Fill in the structure
498 std::vector<llvm::Constant*> Elements;
499 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
500 Elements.push_back(SuperClass);
Chris Lattnerad9c3f32008-06-26 04:47:04 +0000501 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000502 Elements.push_back(Zero);
503 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
504 Elements.push_back(InstanceSize);
505 Elements.push_back(IVars);
506 Elements.push_back(Methods);
507 Elements.push_back(NullP);
508 Elements.push_back(NullP);
509 Elements.push_back(NullP);
510 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
511 Elements.push_back(NullP);
512 // Create an instance of the structure
Chris Lattner4d018542008-07-21 06:31:05 +0000513 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000514}
515
516llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
517 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
518 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
519 // Get the method structure type.
520 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
521 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
522 PtrToInt8Ty,
523 NULL);
524 std::vector<llvm::Constant*> Methods;
525 std::vector<llvm::Constant*> Elements;
526 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
527 Elements.clear();
528 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
529 Zeros, 2));
530 Elements.push_back(
531 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
532 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
533 }
534 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
535 MethodNames.size());
536 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
537 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
538 IntTy, ObjCMethodArrayTy, NULL);
539 Methods.clear();
540 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
541 Methods.push_back(Array);
542 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
543}
544// Create the protocol list structure used in classes, categories and so on
545llvm::Constant *CGObjCGNU::GenerateProtocolList(
546 const llvm::SmallVectorImpl<std::string> &Protocols) {
547 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
548 Protocols.size());
549 llvm::StructType *ProtocolListTy = llvm::StructType::get(
550 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
551 LongTy,//FIXME: Should be size_t
552 ProtocolArrayTy,
553 NULL);
554 std::vector<llvm::Constant*> Elements;
555 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
556 iter != endIter ; iter++) {
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000557 llvm::Constant *protocol = ExistingProtocols[*iter];
558 if (!protocol)
559 protocol = GenerateEmptyProtocol(*iter);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000560 llvm::Constant *Ptr =
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000561 llvm::ConstantExpr::getBitCast(protocol, PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000562 Elements.push_back(Ptr);
563 }
564 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
565 Elements);
566 Elements.clear();
567 Elements.push_back(NULLPtr);
Chris Lattner431dc382009-01-27 05:06:01 +0000568 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000569 Elements.push_back(ProtocolArray);
570 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
571}
572
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000573llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000574 const ObjCProtocolDecl *PD) {
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000575 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
576 const llvm::Type *T =
577 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
578 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
579}
580
581llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
582 const std::string &ProtocolName) {
583 llvm::SmallVector<std::string, 0> EmptyStringVector;
584 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
585
586 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
587 llvm::Constant *InstanceMethodList =
588 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
589 llvm::Constant *ClassMethodList =
590 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
591 // Protocols are objects containing lists of the methods implemented and
592 // protocols adopted.
593 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
594 PtrToInt8Ty,
595 ProtocolList->getType(),
596 InstanceMethodList->getType(),
597 ClassMethodList->getType(),
598 NULL);
599 std::vector<llvm::Constant*> Elements;
600 // The isa pointer must be set to a magic number so the runtime knows it's
601 // the correct layout.
602 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
603 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
604 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
605 Elements.push_back(ProtocolList);
606 Elements.push_back(InstanceMethodList);
607 Elements.push_back(ClassMethodList);
608 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000609}
610
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000611void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
612 ASTContext &Context = CGM.getContext();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000613 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000614 llvm::SmallVector<std::string, 16> Protocols;
615 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
616 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattner271d4c22008-11-24 05:29:24 +0000617 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000618 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
619 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000620 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(Context),
621 E = PD->instmeth_end(Context); iter != E; iter++) {
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000622 std::string TypeStr;
623 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
624 InstanceMethodNames.push_back(
Chris Lattner3a8f2942008-11-24 03:33:13 +0000625 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000626 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000627 }
628 // Collect information about class methods:
629 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
630 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000631 for (ObjCProtocolDecl::classmeth_iterator
632 iter = PD->classmeth_begin(Context),
633 endIter = PD->classmeth_end(Context) ; iter != endIter ; iter++) {
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000634 std::string TypeStr;
635 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
636 ClassMethodNames.push_back(
Chris Lattner3a8f2942008-11-24 03:33:13 +0000637 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000638 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000639 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000640
641 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
642 llvm::Constant *InstanceMethodList =
643 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
644 llvm::Constant *ClassMethodList =
645 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
646 // Protocols are objects containing lists of the methods implemented and
647 // protocols adopted.
648 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
649 PtrToInt8Ty,
650 ProtocolList->getType(),
651 InstanceMethodList->getType(),
652 ClassMethodList->getType(),
653 NULL);
654 std::vector<llvm::Constant*> Elements;
655 // The isa pointer must be set to a magic number so the runtime knows it's
656 // the correct layout.
657 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
658 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
659 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
660 Elements.push_back(ProtocolList);
661 Elements.push_back(InstanceMethodList);
662 Elements.push_back(ClassMethodList);
663 ExistingProtocols[ProtocolName] =
664 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
665 ".objc_protocol"), IdTy);
666}
667
Daniel Dunbarac93e472008-08-15 22:20:32 +0000668void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000669 std::string ClassName = OCD->getClassInterface()->getNameAsString();
670 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000671 // Collect information about instance methods
672 llvm::SmallVector<Selector, 16> InstanceMethodSels;
673 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregorcd19b572009-04-23 01:02:12 +0000674 for (ObjCCategoryImplDecl::instmeth_iterator
675 iter = OCD->instmeth_begin(CGM.getContext()),
676 endIter = OCD->instmeth_end(CGM.getContext());
677 iter != endIter ; iter++) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000678 InstanceMethodSels.push_back((*iter)->getSelector());
679 std::string TypeStr;
680 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
681 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
682 }
683
684 // Collect information about class methods
685 llvm::SmallVector<Selector, 16> ClassMethodSels;
686 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregorcd19b572009-04-23 01:02:12 +0000687 for (ObjCCategoryImplDecl::classmeth_iterator
688 iter = OCD->classmeth_begin(CGM.getContext()),
689 endIter = OCD->classmeth_end(CGM.getContext());
690 iter != endIter ; iter++) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000691 ClassMethodSels.push_back((*iter)->getSelector());
692 std::string TypeStr;
693 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
694 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
695 }
696
697 // Collect the names of referenced protocols
698 llvm::SmallVector<std::string, 16> Protocols;
699 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
700 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
701 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
702 E = Protos.end(); I != E; ++I)
Chris Lattner271d4c22008-11-24 05:29:24 +0000703 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000704
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000705 std::vector<llvm::Constant*> Elements;
706 Elements.push_back(MakeConstantString(CategoryName));
707 Elements.push_back(MakeConstantString(ClassName));
708 // Instance method list
709 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattner578279d2008-06-26 05:08:00 +0000710 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000711 false), PtrTy));
712 // Class method list
713 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattner578279d2008-06-26 05:08:00 +0000714 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000715 PtrTy));
716 // Protocol list
717 Elements.push_back(llvm::ConstantExpr::getBitCast(
718 GenerateProtocolList(Protocols), PtrTy));
719 Categories.push_back(llvm::ConstantExpr::getBitCast(
720 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
721 PtrTy, PtrTy, NULL), Elements), PtrTy));
722}
Daniel Dunbarac93e472008-08-15 22:20:32 +0000723
724void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
725 ASTContext &Context = CGM.getContext();
726
727 // Get the superclass name.
728 const ObjCInterfaceDecl * SuperClassDecl =
729 OID->getClassInterface()->getSuperClass();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000730 std::string SuperClassName;
731 if (SuperClassDecl)
732 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000733
734 // Get the class name
Chris Lattnerb63b5502009-04-01 02:00:48 +0000735 ObjCInterfaceDecl *ClassDecl =
736 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000737 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000738
739 // Get the size of instances. For runtimes that support late-bound instances
740 // this should probably be something different (size just of instance
741 // varaibles in this class, not superclasses?).
Daniel Dunbare5bb23c2009-04-22 09:39:34 +0000742 const llvm::Type *ObjTy = GetConcreteClassStruct(CGM, ClassDecl);
Chris Lattnerb63b5502009-04-01 02:00:48 +0000743 int instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000744
745 // Collect information about instance variables.
746 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
747 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
748 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
Daniel Dunbarac93e472008-08-15 22:20:32 +0000749 ObjTy = llvm::PointerType::getUnqual(ObjTy);
750 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
751 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
752 // Store the name
Chris Lattner271d4c22008-11-24 05:29:24 +0000753 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
754 ->getNameAsString()));
Daniel Dunbarac93e472008-08-15 22:20:32 +0000755 // Get the type encoding for this ivar
756 std::string TypeStr;
Daniel Dunbarc9197cd2008-10-17 20:21:44 +0000757 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000758 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
759 // Get the offset
Daniel Dunbarbdeefcc2009-04-22 08:20:31 +0000760 uint64_t Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000761 IvarOffsets.push_back(
Daniel Dunbarbdeefcc2009-04-22 08:20:31 +0000762 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset));
Daniel Dunbarac93e472008-08-15 22:20:32 +0000763 }
764
765 // Collect information about instance methods
766 llvm::SmallVector<Selector, 16> InstanceMethodSels;
767 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregorcd19b572009-04-23 01:02:12 +0000768 for (ObjCImplementationDecl::instmeth_iterator
769 iter = OID->instmeth_begin(CGM.getContext()),
770 endIter = OID->instmeth_end(CGM.getContext());
771 iter != endIter ; iter++) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000772 InstanceMethodSels.push_back((*iter)->getSelector());
773 std::string TypeStr;
774 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
775 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
776 }
777
778 // Collect information about class methods
779 llvm::SmallVector<Selector, 16> ClassMethodSels;
780 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregorcd19b572009-04-23 01:02:12 +0000781 for (ObjCImplementationDecl::classmeth_iterator
782 iter = OID->classmeth_begin(CGM.getContext()),
783 endIter = OID->classmeth_end(CGM.getContext());
784 iter != endIter ; iter++) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000785 ClassMethodSels.push_back((*iter)->getSelector());
786 std::string TypeStr;
787 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
788 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
789 }
790 // Collect the names of referenced protocols
791 llvm::SmallVector<std::string, 16> Protocols;
792 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
793 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
794 E = Protos.end(); I != E; ++I)
Chris Lattner271d4c22008-11-24 05:29:24 +0000795 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000796
797
798
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000799 // Get the superclass pointer.
800 llvm::Constant *SuperClass;
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000801 if (!SuperClassName.empty()) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000802 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
803 } else {
Chris Lattner431dc382009-01-27 05:06:01 +0000804 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000805 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000806 // Empty vector used to construct empty method lists
807 llvm::SmallVector<llvm::Constant*, 1> empty;
808 // Generate the method and instance variable lists
809 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattner578279d2008-06-26 05:08:00 +0000810 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000811 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattner578279d2008-06-26 05:08:00 +0000812 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000813 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
814 IvarOffsets);
815 //Generate metaclass for class methods
816 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner4d018542008-07-21 06:31:05 +0000817 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000818 empty, empty, empty), ClassMethodList, NULLPtr);
819 // Generate the class structure
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000820 llvm::Constant *ClassStruct =
821 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
822 ClassName.c_str(), 0,
Sebastian Redle9e21b32008-12-04 00:10:55 +0000823 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000824 MethodList, GenerateProtocolList(Protocols));
825 // Add class structure to list to be added to the symtab later
826 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
827 Classes.push_back(ClassStruct);
828}
829
830llvm::Function *CGObjCGNU::ModuleInitFunction() {
831 // Only emit an ObjC load function if no Objective-C stuff has been called
832 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
833 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikovb41e4972008-06-01 15:14:46 +0000834 UntypedSelectors.empty())
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000835 return NULL;
Eli Friedman7719bc82008-06-01 16:00:02 +0000836
Chris Lattner431dc382009-01-27 05:06:01 +0000837 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
838 SelectorTy->getElementType());
839 const llvm::Type *SelStructPtrTy = SelectorTy;
840 bool isSelOpaque = false;
841 if (SelStructTy == 0) {
842 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
843 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
844 isSelOpaque = true;
845 }
846
Eli Friedman7719bc82008-06-01 16:00:02 +0000847 // Name the ObjC types to make the IR a bit easier to read
Chris Lattner431dc382009-01-27 05:06:01 +0000848 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman7719bc82008-06-01 16:00:02 +0000849 TheModule.addTypeName(".objc_id", IdTy);
850 TheModule.addTypeName(".objc_imp", IMPTy);
851
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000852 std::vector<llvm::Constant*> Elements;
853 // Generate statics list:
854 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
855 ConstantStrings.size() + 1);
856 ConstantStrings.push_back(NULLPtr);
857 Elements.push_back(MakeConstantString("NSConstantString",
858 ".objc_static_class_name"));
859 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
860 llvm::StructType *StaticsListTy =
861 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattneref843042008-06-26 04:10:42 +0000862 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000863 llvm::Constant *Statics =
864 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner60eaae82008-06-24 17:01:28 +0000865 llvm::ArrayType *StaticsListArrayTy =
Chris Lattneref843042008-06-26 04:10:42 +0000866 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner60eaae82008-06-24 17:01:28 +0000867 Elements.clear();
868 Elements.push_back(Statics);
Chris Lattneref843042008-06-26 04:10:42 +0000869 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner60eaae82008-06-24 17:01:28 +0000870 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000871 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
872 // Array of classes, categories, and constant objects
873 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
874 Classes.size() + Categories.size() + 2);
Chris Lattner431dc382009-01-27 05:06:01 +0000875 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Chris Lattneref843042008-06-26 04:10:42 +0000876 llvm::Type::Int16Ty,
877 llvm::Type::Int16Ty,
878 ClassListTy, NULL);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000879
880 Elements.clear();
881 // Pointer to an array of selectors used in this module.
882 std::vector<llvm::Constant*> Selectors;
883 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
884 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
885 iter != iterEnd ; ++iter) {
Chris Lattneref843042008-06-26 04:10:42 +0000886 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
887 Elements.push_back(MakeConstantString(iter->first.second,
888 ".objc_sel_types"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000889 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
890 Elements.clear();
891 }
892 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
893 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattneref843042008-06-26 04:10:42 +0000894 iter != iterEnd; ++iter) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000895 Elements.push_back(
Chris Lattneref843042008-06-26 04:10:42 +0000896 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000897 Elements.push_back(NULLPtr);
898 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
899 Elements.clear();
900 }
901 Elements.push_back(NULLPtr);
902 Elements.push_back(NULLPtr);
903 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
904 Elements.clear();
905 // Number of static selectors
906 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
907 llvm::Constant *SelectorList = MakeGlobal(
908 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
909 ".objc_selector_list");
Chris Lattner431dc382009-01-27 05:06:01 +0000910 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
911 SelStructPtrTy));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000912
913 // Now that all of the static selectors exist, create pointers to them.
914 int index = 0;
915 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
916 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
917 iter != iterEnd; ++iter) {
918 llvm::Constant *Idxs[] = {Zeros[0],
919 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattner431dc382009-01-27 05:06:01 +0000920 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
921 true, llvm::GlobalValue::InternalLinkage,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000922 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
923 ".objc_sel_ptr", &TheModule);
Chris Lattner431dc382009-01-27 05:06:01 +0000924 // If selectors are defined as an opaque type, cast the pointer to this
925 // type.
926 if (isSelOpaque) {
927 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
928 llvm::PointerType::getUnqual(SelectorTy));
929 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000930 (*iter).second->setAliasee(SelPtr);
931 }
932 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
933 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
934 iter != iterEnd; iter++) {
935 llvm::Constant *Idxs[] = {Zeros[0],
936 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattner431dc382009-01-27 05:06:01 +0000937 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000938 llvm::GlobalValue::InternalLinkage,
939 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
940 ".objc_sel_ptr", &TheModule);
Chris Lattner431dc382009-01-27 05:06:01 +0000941 // If selectors are defined as an opaque type, cast the pointer to this
942 // type.
943 if (isSelOpaque) {
944 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
945 llvm::PointerType::getUnqual(SelectorTy));
946 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000947 (*iter).second->setAliasee(SelPtr);
948 }
949 // Number of classes defined.
950 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
951 Classes.size()));
952 // Number of categories defined
953 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
954 Categories.size()));
955 // Create an array of classes, then categories, then static object instances
956 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
957 // NULL-terminated list of static object instances (mainly constant strings)
958 Classes.push_back(Statics);
959 Classes.push_back(NULLPtr);
960 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
961 Elements.push_back(ClassList);
962 // Construct the symbol table
963 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
964
965 // The symbol table is contained in a module which has some version-checking
966 // constants
967 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
968 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
969 Elements.clear();
970 // Runtime version used for compatibility checking.
971 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahaniandefa06a2009-04-01 19:49:42 +0000972 // sizeof(ModuleTy)
973 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
974 Elements.push_back(llvm::ConstantInt::get(LongTy, td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000975 //FIXME: Should be the path to the file where this module was declared
976 Elements.push_back(NULLPtr);
977 Elements.push_back(SymTab);
978 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
979
980 // Create the load function calling the runtime entry point with the module
981 // structure
982 std::vector<const llvm::Type*> VoidArgs;
983 llvm::Function * LoadFunction = llvm::Function::Create(
984 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
985 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
986 &TheModule);
987 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000988 CGBuilderTy Builder;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000989 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000990
991 std::vector<const llvm::Type*> Params(1,
992 llvm::PointerType::getUnqual(ModuleTy));
993 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
994 llvm::Type::VoidTy, Params, true), "__objc_exec_class");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000995 Builder.CreateCall(Register, Module);
996 Builder.CreateRetVoid();
997 return LoadFunction;
998}
Daniel Dunbarac93e472008-08-15 22:20:32 +0000999
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00001000llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
1001 const ObjCContainerDecl *CD) {
Daniel Dunbarac93e472008-08-15 22:20:32 +00001002 const ObjCCategoryImplDecl *OCD =
Steve Naroff438be772009-01-08 19:41:02 +00001003 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner3a8f2942008-11-24 03:33:13 +00001004 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1005 std::string ClassName = OMD->getClassInterface()->getNameAsString();
1006 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregor5d764842009-01-09 17:18:27 +00001007 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbarac93e472008-08-15 22:20:32 +00001008
Daniel Dunbar34bda882009-02-02 23:23:47 +00001009 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001010 const llvm::FunctionType *MethodTy =
Daniel Dunbar34bda882009-02-02 23:23:47 +00001011 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001012 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1013 MethodName, isClassMethod);
1014
Gabor Greif815e2c12008-04-06 20:42:52 +00001015 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattnerb326b172008-03-30 23:03:07 +00001016 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001017 FunctionName,
Chris Lattnerb326b172008-03-30 23:03:07 +00001018 &TheModule);
Chris Lattnerb326b172008-03-30 23:03:07 +00001019 return Method;
1020}
1021
Daniel Dunbarf7103722008-09-24 03:38:44 +00001022llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
1023 return 0;
1024}
1025
1026llvm::Function *CGObjCGNU::GetPropertySetFunction() {
1027 return 0;
1028}
1029
1030llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian6158e692009-03-30 18:02:14 +00001031 std::vector<const llvm::Type*> Params(1, IdTy);
1032 return cast<llvm::Function>(CGM.CreateRuntimeFunction(
1033 llvm::FunctionType::get(llvm::Type::VoidTy, Params, true),
1034 "objc_enumerationMutation"));
Anders Carlsson58d16242008-08-31 04:05:03 +00001035}
1036
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001037void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1038 const Stmt &S) {
1039 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlssonb01a2112008-09-09 10:04:29 +00001040}
1041
1042void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbarf7103722008-09-24 03:38:44 +00001043 const ObjCAtThrowStmt &S) {
Anders Carlssonb01a2112008-09-09 10:04:29 +00001044 CGF.ErrorUnsupported(&S, "@throw statement");
1045}
1046
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00001047llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00001048 llvm::Value *AddrWeakObj)
1049{
1050 return 0;
1051}
1052
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00001053void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1054 llvm::Value *src, llvm::Value *dst)
1055{
1056 return;
1057}
1058
Fariborz Jahanian17958902008-11-19 00:59:10 +00001059void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1060 llvm::Value *src, llvm::Value *dst)
1061{
1062 return;
1063}
1064
Fariborz Jahanianf310b592008-11-20 19:23:36 +00001065void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1066 llvm::Value *src, llvm::Value *dst)
1067{
1068 return;
1069}
1070
Fariborz Jahanian17958902008-11-19 00:59:10 +00001071void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1072 llvm::Value *src, llvm::Value *dst)
1073{
1074 return;
1075}
1076
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001077LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1078 QualType ObjectTy,
1079 llvm::Value *BaseValue,
1080 const ObjCIvarDecl *Ivar,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001081 unsigned CVRQualifiers) {
Daniel Dunbarf5254bd2009-04-21 01:19:28 +00001082 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar85d37542009-04-22 07:32:20 +00001083 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
1084 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00001085}
1086
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001087llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +00001088 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001089 const ObjCIvarDecl *Ivar) {
Daniel Dunbar85d37542009-04-22 07:32:20 +00001090 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
Daniel Dunbare5bb23c2009-04-22 09:39:34 +00001091 return llvm::ConstantInt::get(LongTy, Offset);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001092}
1093
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001094CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattner547907c2008-06-26 04:19:03 +00001095 return new CGObjCGNU(CGM);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +00001096}