blob: ae9d86137c827ccade93c06a86a73447284c436f [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 Lattner547907c2008-06-26 04:19:03 +000029using namespace clang;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000030using namespace CodeGen;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000031using llvm::dyn_cast;
32
33// The version of the runtime that this class targets. Must match the version
34// in the runtime.
35static const int RuntimeVersion = 8;
36static const int ProtocolVersion = 2;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000037
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000038namespace {
Chris Lattner547907c2008-06-26 04:19:03 +000039class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000040private:
Chris Lattner547907c2008-06-26 04:19:03 +000041 CodeGen::CodeGenModule &CGM;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000042 llvm::Module &TheModule;
Chris Lattner3d9dcdd2009-01-21 18:52:19 +000043 const llvm::PointerType *SelectorTy;
44 const llvm::Type *ExpectedSelTy;
Chris Lattnerb326b172008-03-30 23:03:07 +000045 const llvm::Type *PtrToInt8Ty;
46 const llvm::Type *IMPTy;
47 const llvm::Type *IdTy;
48 const llvm::Type *IntTy;
49 const llvm::Type *PtrTy;
50 const llvm::Type *LongTy;
51 const llvm::Type *PtrToIntTy;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000052 std::vector<llvm::Constant*> Classes;
53 std::vector<llvm::Constant*> Categories;
54 std::vector<llvm::Constant*> ConstantStrings;
55 llvm::Function *LoadFunction;
56 llvm::StringMap<llvm::Constant*> ExistingProtocols;
57 typedef std::pair<std::string, std::string> TypedSelector;
58 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
59 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
60 // Some zeros used for GEPs in lots of places.
61 llvm::Constant *Zeros[2];
62 llvm::Constant *NULLPtr;
63private:
64 llvm::Constant *GenerateIvarList(
65 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
66 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
67 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
68 llvm::Constant *GenerateMethodList(const std::string &ClassName,
69 const std::string &CategoryName,
Chris Lattner578279d2008-06-26 05:08:00 +000070 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000071 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
72 bool isClassMethodList);
73 llvm::Constant *GenerateProtocolList(
74 const llvm::SmallVectorImpl<std::string> &Protocols);
75 llvm::Constant *GenerateClassStructure(
76 llvm::Constant *MetaClass,
77 llvm::Constant *SuperClass,
78 unsigned info,
Chris Lattnerad9c3f32008-06-26 04:47:04 +000079 const char *Name,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000080 llvm::Constant *Version,
81 llvm::Constant *InstanceSize,
82 llvm::Constant *IVars,
83 llvm::Constant *Methods,
84 llvm::Constant *Protocols);
85 llvm::Constant *GenerateProtocolMethodList(
86 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
87 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
88 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
89 &Name="");
90 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
91 std::vector<llvm::Constant*> &V, const std::string &Name="");
92 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
93 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000094public:
Chris Lattner547907c2008-06-26 04:19:03 +000095 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000096 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbara04840b2008-08-23 03:46:30 +000097 virtual CodeGen::RValue
98 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +000099 QualType ResultType,
100 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000101 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000102 bool IsClassMessage,
103 const CallArgList &CallArgs);
Daniel Dunbara04840b2008-08-23 03:46:30 +0000104 virtual CodeGen::RValue
105 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000106 QualType ResultType,
107 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000108 const ObjCInterfaceDecl *Class,
109 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000110 bool IsClassMessage,
111 const CallArgList &CallArgs);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000112 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar434627a2008-08-16 00:25:02 +0000113 const ObjCInterfaceDecl *OID);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000114 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Chris Lattnerd71288e2008-06-26 04:37:12 +0000115
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000116 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
117 const ObjCContainerDecl *CD);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000118 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
119 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000120 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000121 const ObjCProtocolDecl *PD);
122 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000123 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbarf7103722008-09-24 03:38:44 +0000124 virtual llvm::Function *GetPropertyGetFunction();
125 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson58d16242008-08-31 04:05:03 +0000126 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlssonb01a2112008-09-09 10:04:29 +0000127
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000128 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
129 const Stmt &S);
Anders Carlssonb01a2112008-09-09 10:04:29 +0000130 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
131 const ObjCAtThrowStmt &S);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000132 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000133 llvm::Value *AddrWeakObj);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000134 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
135 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000136 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
137 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianf310b592008-11-20 19:23:36 +0000138 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
139 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000140 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
141 llvm::Value *src, llvm::Value *dest);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000142};
143} // end anonymous namespace
144
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000145
146
147static std::string SymbolNameForClass(const std::string &ClassName) {
148 return ".objc_class_" + ClassName;
149}
150
151static std::string SymbolNameForMethod(const std::string &ClassName, const
152 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
153{
154 return "._objc_method_" + ClassName +"("+CategoryName+")"+
155 (isClassMethod ? "+" : "-") + MethodName;
156}
157
Chris Lattner547907c2008-06-26 04:19:03 +0000158CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
159 : CGM(cgm), TheModule(CGM.getModule()) {
160 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
161 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
162
Sebastian Redle9e21b32008-12-04 00:10:55 +0000163 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000164 Zeros[1] = Zeros[0];
165 NULLPtr = llvm::ConstantPointerNull::get(
166 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattnerb326b172008-03-30 23:03:07 +0000167 // C string type. Used in lots of places.
168 PtrToInt8Ty =
169 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
170 // Get the selector Type.
Chris Lattner3d9dcdd2009-01-21 18:52:19 +0000171 SelectorTy = cast<llvm::PointerType>(
172 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
173
174 ExpectedSelTy =
175 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType());
176
Chris Lattnerb326b172008-03-30 23:03:07 +0000177 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
178 PtrTy = PtrToInt8Ty;
179
180 // Object type
181 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
182 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
183 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
184 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
185 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
186 IdTy = llvm::PointerType::getUnqual(IdTy);
187
188 // IMP type
189 std::vector<const llvm::Type*> IMPArgs;
190 IMPArgs.push_back(IdTy);
191 IMPArgs.push_back(SelectorTy);
192 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000193}
194// This has to perform the lookup every time, since posing and related
195// techniques can modify the name -> class mapping.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000196llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbar434627a2008-08-16 00:25:02 +0000197 const ObjCInterfaceDecl *OID) {
Chris Lattner271d4c22008-11-24 05:29:24 +0000198 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Daniel Dunbar434627a2008-08-16 00:25:02 +0000199 ClassName = Builder.CreateStructGEP(ClassName, 0);
200
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000201 llvm::Constant *ClassLookupFn =
202 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
203 NULL);
204 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattnerb326b172008-03-30 23:03:07 +0000205}
206
Chris Lattnerd71288e2008-06-26 04:37:12 +0000207/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000208llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattnerd71288e2008-06-26 04:37:12 +0000209 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner3a8f2942008-11-24 03:33:13 +0000210 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattnerd71288e2008-06-26 04:37:12 +0000211 if (US == 0)
212 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
213 llvm::GlobalValue::InternalLinkage,
214 ".objc_untyped_selector_alias",
215 NULL, &TheModule);
216
217 return Builder.CreateLoad(US);
218
219}
220
Chris Lattnera5b18882008-06-26 04:44:19 +0000221llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
222 const std::string &Name) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000223 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
224 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
225 llvm::GlobalValue::InternalLinkage,
226 ConstStr, Name, &TheModule);
227 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
228}
229llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
230 std::vector<llvm::Constant*> &V, const std::string &Name) {
231 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
232 return new llvm::GlobalVariable(Ty, false,
233 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
234}
235llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
236 std::vector<llvm::Constant*> &V, const std::string &Name) {
237 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
238 return new llvm::GlobalVariable(Ty, false,
239 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
240}
241
242/// Generate an NSConstantString object.
243//TODO: In case there are any crazy people still using the GNU runtime without
244//an OpenStep implementation, this should let them select their own class for
245//constant strings.
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000246llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000247 std::vector<llvm::Constant*> Ivars;
248 Ivars.push_back(NULLPtr);
Chris Lattnerbac12452008-06-21 21:44:18 +0000249 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000250 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000251 llvm::Constant *ObjCStr = MakeGlobal(
252 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
253 Ivars, ".objc_str");
254 ConstantStrings.push_back(
255 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
256 return ObjCStr;
257}
258
259///Generates a message send where the super is the receiver. This is a message
260///send to self with special delivery semantics indicating which class's method
261///should be called.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000262CodeGen::RValue
263CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000264 QualType ResultType,
265 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000266 const ObjCInterfaceDecl *Class,
267 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000268 bool IsClassMessage,
269 const CallArgList &CallArgs) {
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000270 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Daniel Dunbardd851282008-08-30 05:35:15 +0000271 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000272 // TODO: This should be cached, not looked up every time.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000273 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Daniel Dunbardd851282008-08-30 05:35:15 +0000274 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000275 std::vector<const llvm::Type*> impArgTypes;
276 impArgTypes.push_back(Receiver->getType());
Chris Lattnerb326b172008-03-30 23:03:07 +0000277 impArgTypes.push_back(SelectorTy);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000278
279 // Avoid an explicit cast on the IMP by getting a version that has the right
280 // return type.
281 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
282 true);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000283 // Construct the structure used to look up the IMP
284 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
285 IdTy, NULL);
Daniel Dunbara04840b2008-08-23 03:46:30 +0000286 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman2e630542008-06-13 23:01:12 +0000287 // FIXME: volatility
Daniel Dunbara04840b2008-08-23 03:46:30 +0000288 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
289 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000290
291 // Get the IMP
292 llvm::Constant *lookupFunction =
293 TheModule.getOrInsertFunction("objc_msg_lookup_super",
294 llvm::PointerType::getUnqual(impType),
295 llvm::PointerType::getUnqual(ObjCSuperTy),
296 SelectorTy, NULL);
297 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbara04840b2008-08-23 03:46:30 +0000298 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000299 lookupArgs+2);
300
301 // Call the method
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000302 CallArgList ActualArgs;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000303 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000304 CGF.getContext().getObjCIdType()));
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000305 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000306 CGF.getContext().getObjCSelType()));
307 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbardd851282008-08-30 05:35:15 +0000308 return CGF.EmitCall(imp, ResultType, ActualArgs);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000309}
310
311/// Generate code for a message send expression.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000312CodeGen::RValue
313CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000314 QualType ResultType,
315 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000316 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000317 bool IsClassMessage,
318 const CallArgList &CallArgs) {
Daniel Dunbardd851282008-08-30 05:35:15 +0000319 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
320 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000321
322 // Look up the method implementation.
323 std::vector<const llvm::Type*> impArgTypes;
324 const llvm::Type *RetTy;
325 //TODO: Revisit this when LLVM supports aggregate return types.
326 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
327 RetTy = ReturnTy;
328 } else {
329 // For struct returns allocate the space in the caller and pass it up to
330 // the sender.
331 RetTy = llvm::Type::VoidTy;
332 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
333 }
334 impArgTypes.push_back(Receiver->getType());
335 impArgTypes.push_back(SelectorTy);
336
337 // Avoid an explicit cast on the IMP by getting a version that has the right
338 // return type.
339 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
340 true);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000341
342 llvm::Constant *lookupFunction =
343 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattnerb326b172008-03-30 23:03:07 +0000344 llvm::PointerType::getUnqual(impType),
345 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbara04840b2008-08-23 03:46:30 +0000346 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner7db5e942008-05-06 00:56:42 +0000347
348 // Call the method.
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000349 CallArgList ActualArgs;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000350 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000351 CGF.getContext().getObjCIdType()));
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000352 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000353 CGF.getContext().getObjCSelType()));
354 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbardd851282008-08-30 05:35:15 +0000355 return CGF.EmitCall(imp, ResultType, ActualArgs);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000356}
357
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000358/// Generates a MethodList. Used in construction of a objc_class and
359/// objc_category structures.
360llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattner578279d2008-06-26 05:08:00 +0000361 const std::string &CategoryName,
362 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000363 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
364 bool isClassMethodList) {
365 // Get the method structure type.
366 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
367 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
368 PtrToInt8Ty, // Method types
369 llvm::PointerType::getUnqual(IMPTy), //Method pointer
370 NULL);
371 std::vector<llvm::Constant*> Methods;
372 std::vector<llvm::Constant*> Elements;
373 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
374 Elements.clear();
Chris Lattner3a8f2942008-11-24 03:33:13 +0000375 llvm::Constant *C =
376 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattner578279d2008-06-26 05:08:00 +0000377 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000378 Elements.push_back(
379 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
380 llvm::Constant *Method =
381 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner3a8f2942008-11-24 03:33:13 +0000382 MethodSels[i].getAsString(),
Chris Lattnere7581092008-06-26 04:05:20 +0000383 isClassMethodList));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000384 Method = llvm::ConstantExpr::getBitCast(Method,
385 llvm::PointerType::getUnqual(IMPTy));
386 Elements.push_back(Method);
387 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
388 }
389
390 // Array of method structures
391 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattner578279d2008-06-26 05:08:00 +0000392 MethodSels.size());
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000393 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerbcb3e862008-06-26 04:52:29 +0000394 Methods);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000395
396 // Structure containing list pointer, array and array count
397 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
398 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
399 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
400 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
401 IntTy,
402 ObjCMethodArrayTy,
403 NULL);
404 // Refine next pointer type to concrete type
405 llvm::cast<llvm::OpaqueType>(
406 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
407 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
408
409 Methods.clear();
410 Methods.push_back(llvm::ConstantPointerNull::get(
411 llvm::PointerType::getUnqual(ObjCMethodListTy)));
412 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
413 MethodTypes.size()));
414 Methods.push_back(MethodArray);
415
416 // Create an instance of the structure
417 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
418}
419
420/// Generates an IvarList. Used in construction of a objc_class.
421llvm::Constant *CGObjCGNU::GenerateIvarList(
422 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
423 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
424 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
425 // Get the method structure type.
426 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
427 PtrToInt8Ty,
428 PtrToInt8Ty,
429 IntTy,
430 NULL);
431 std::vector<llvm::Constant*> Ivars;
432 std::vector<llvm::Constant*> Elements;
433 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
434 Elements.clear();
435 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
436 Zeros, 2));
437 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
438 Zeros, 2));
439 Elements.push_back(IvarOffsets[i]);
440 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
441 }
442
443 // Array of method structures
444 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
445 IvarNames.size());
446
447
448 Elements.clear();
449 Elements.push_back(llvm::ConstantInt::get(
450 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
451 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
452 // Structure containing array and array count
453 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
454 ObjCIvarArrayTy,
455 NULL);
456
457 // Create an instance of the structure
458 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
459}
460
461/// Generate a class structure
462llvm::Constant *CGObjCGNU::GenerateClassStructure(
463 llvm::Constant *MetaClass,
464 llvm::Constant *SuperClass,
465 unsigned info,
Chris Lattnerad9c3f32008-06-26 04:47:04 +0000466 const char *Name,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000467 llvm::Constant *Version,
468 llvm::Constant *InstanceSize,
469 llvm::Constant *IVars,
470 llvm::Constant *Methods,
471 llvm::Constant *Protocols) {
472 // Set up the class structure
473 // Note: Several of these are char*s when they should be ids. This is
474 // because the runtime performs this translation on load.
475 llvm::StructType *ClassTy = llvm::StructType::get(
476 PtrToInt8Ty, // class_pointer
477 PtrToInt8Ty, // super_class
478 PtrToInt8Ty, // name
479 LongTy, // version
480 LongTy, // info
481 LongTy, // instance_size
482 IVars->getType(), // ivars
483 Methods->getType(), // methods
484 // These are all filled in by the runtime, so we pretend
485 PtrTy, // dtable
486 PtrTy, // subclass_list
487 PtrTy, // sibling_class
488 PtrTy, // protocols
489 PtrTy, // gc_object_type
490 NULL);
491 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
492 llvm::Constant *NullP =
493 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
494 // Fill in the structure
495 std::vector<llvm::Constant*> Elements;
496 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
497 Elements.push_back(SuperClass);
Chris Lattnerad9c3f32008-06-26 04:47:04 +0000498 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000499 Elements.push_back(Zero);
500 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
501 Elements.push_back(InstanceSize);
502 Elements.push_back(IVars);
503 Elements.push_back(Methods);
504 Elements.push_back(NullP);
505 Elements.push_back(NullP);
506 Elements.push_back(NullP);
507 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
508 Elements.push_back(NullP);
509 // Create an instance of the structure
Chris Lattner4d018542008-07-21 06:31:05 +0000510 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000511}
512
513llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
514 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
515 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
516 // Get the method structure type.
517 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
518 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
519 PtrToInt8Ty,
520 NULL);
521 std::vector<llvm::Constant*> Methods;
522 std::vector<llvm::Constant*> Elements;
523 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
524 Elements.clear();
525 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
526 Zeros, 2));
527 Elements.push_back(
528 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
529 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
530 }
531 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
532 MethodNames.size());
533 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
534 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
535 IntTy, ObjCMethodArrayTy, NULL);
536 Methods.clear();
537 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
538 Methods.push_back(Array);
539 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
540}
541// Create the protocol list structure used in classes, categories and so on
542llvm::Constant *CGObjCGNU::GenerateProtocolList(
543 const llvm::SmallVectorImpl<std::string> &Protocols) {
544 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
545 Protocols.size());
546 llvm::StructType *ProtocolListTy = llvm::StructType::get(
547 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
548 LongTy,//FIXME: Should be size_t
549 ProtocolArrayTy,
550 NULL);
551 std::vector<llvm::Constant*> Elements;
552 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
553 iter != endIter ; iter++) {
554 llvm::Constant *Ptr =
555 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
556 Elements.push_back(Ptr);
557 }
558 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
559 Elements);
560 Elements.clear();
561 Elements.push_back(NULLPtr);
562 Elements.push_back(llvm::ConstantInt::get(
563 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
564 Elements.push_back(ProtocolArray);
565 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
566}
567
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000568llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000569 const ObjCProtocolDecl *PD) {
Chris Lattner271d4c22008-11-24 05:29:24 +0000570 return ExistingProtocols[PD->getNameAsString()];
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000571}
572
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000573void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
574 ASTContext &Context = CGM.getContext();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000575 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000576 llvm::SmallVector<std::string, 16> Protocols;
577 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
578 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattner271d4c22008-11-24 05:29:24 +0000579 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000580 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
581 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
582 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
583 E = PD->instmeth_end(); iter != E; iter++) {
584 std::string TypeStr;
585 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
586 InstanceMethodNames.push_back(
Chris Lattner3a8f2942008-11-24 03:33:13 +0000587 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000588 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000589 }
590 // Collect information about class methods:
591 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
592 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
593 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
594 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
595 std::string TypeStr;
596 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
597 ClassMethodNames.push_back(
Chris Lattner3a8f2942008-11-24 03:33:13 +0000598 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000599 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000600 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000601
602 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
603 llvm::Constant *InstanceMethodList =
604 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
605 llvm::Constant *ClassMethodList =
606 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
607 // Protocols are objects containing lists of the methods implemented and
608 // protocols adopted.
609 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
610 PtrToInt8Ty,
611 ProtocolList->getType(),
612 InstanceMethodList->getType(),
613 ClassMethodList->getType(),
614 NULL);
615 std::vector<llvm::Constant*> Elements;
616 // The isa pointer must be set to a magic number so the runtime knows it's
617 // the correct layout.
618 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
619 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
620 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
621 Elements.push_back(ProtocolList);
622 Elements.push_back(InstanceMethodList);
623 Elements.push_back(ClassMethodList);
624 ExistingProtocols[ProtocolName] =
625 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
626 ".objc_protocol"), IdTy);
627}
628
Daniel Dunbarac93e472008-08-15 22:20:32 +0000629void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000630 std::string ClassName = OCD->getClassInterface()->getNameAsString();
631 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000632 // Collect information about instance methods
633 llvm::SmallVector<Selector, 16> InstanceMethodSels;
634 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Steve Naroffab63fd62009-01-08 17:28:14 +0000635 for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(),
Daniel Dunbarac93e472008-08-15 22:20:32 +0000636 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
637 InstanceMethodSels.push_back((*iter)->getSelector());
638 std::string TypeStr;
639 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
640 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
641 }
642
643 // Collect information about class methods
644 llvm::SmallVector<Selector, 16> ClassMethodSels;
645 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Steve Naroffab63fd62009-01-08 17:28:14 +0000646 for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(),
Daniel Dunbarac93e472008-08-15 22:20:32 +0000647 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
648 ClassMethodSels.push_back((*iter)->getSelector());
649 std::string TypeStr;
650 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
651 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
652 }
653
654 // Collect the names of referenced protocols
655 llvm::SmallVector<std::string, 16> Protocols;
656 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
657 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
658 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
659 E = Protos.end(); I != E; ++I)
Chris Lattner271d4c22008-11-24 05:29:24 +0000660 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000661
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000662 std::vector<llvm::Constant*> Elements;
663 Elements.push_back(MakeConstantString(CategoryName));
664 Elements.push_back(MakeConstantString(ClassName));
665 // Instance method list
666 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattner578279d2008-06-26 05:08:00 +0000667 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000668 false), PtrTy));
669 // Class method list
670 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattner578279d2008-06-26 05:08:00 +0000671 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000672 PtrTy));
673 // Protocol list
674 Elements.push_back(llvm::ConstantExpr::getBitCast(
675 GenerateProtocolList(Protocols), PtrTy));
676 Categories.push_back(llvm::ConstantExpr::getBitCast(
677 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
678 PtrTy, PtrTy, NULL), Elements), PtrTy));
679}
Daniel Dunbarac93e472008-08-15 22:20:32 +0000680
681void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
682 ASTContext &Context = CGM.getContext();
683
684 // Get the superclass name.
685 const ObjCInterfaceDecl * SuperClassDecl =
686 OID->getClassInterface()->getSuperClass();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000687 std::string SuperClassName;
688 if (SuperClassDecl)
689 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000690
691 // Get the class name
692 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000693 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000694
695 // Get the size of instances. For runtimes that support late-bound instances
696 // this should probably be something different (size just of instance
697 // varaibles in this class, not superclasses?).
698 int instanceSize = 0;
699 const llvm::Type *ObjTy = 0;
700 if (!LateBoundIVars()) {
701 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
Daniel Dunbard8439f22009-01-12 21:08:18 +0000702 instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000703 } else {
704 // This is required by newer ObjC runtimes.
705 assert(0 && "Late-bound instance variables not yet supported");
706 }
707
708 // Collect information about instance variables.
709 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
710 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
711 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
712 const llvm::StructLayout *Layout =
713 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
714 ObjTy = llvm::PointerType::getUnqual(ObjTy);
715 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
716 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
717 // Store the name
Chris Lattner271d4c22008-11-24 05:29:24 +0000718 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
719 ->getNameAsString()));
Daniel Dunbarac93e472008-08-15 22:20:32 +0000720 // Get the type encoding for this ivar
721 std::string TypeStr;
Daniel Dunbarc9197cd2008-10-17 20:21:44 +0000722 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000723 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
724 // Get the offset
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000725 FieldDecl *Field = ClassDecl->lookupFieldDeclForIvar(Context, (*iter));
Daniel Dunbarac93e472008-08-15 22:20:32 +0000726 int offset =
Fariborz Jahanian86008c02008-12-15 20:35:07 +0000727 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbarac93e472008-08-15 22:20:32 +0000728 IvarOffsets.push_back(
729 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
730 }
731
732 // Collect information about instance methods
733 llvm::SmallVector<Selector, 16> InstanceMethodSels;
734 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
735 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
736 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
737 InstanceMethodSels.push_back((*iter)->getSelector());
738 std::string TypeStr;
739 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
740 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
741 }
742
743 // Collect information about class methods
744 llvm::SmallVector<Selector, 16> ClassMethodSels;
745 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
746 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
747 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
748 ClassMethodSels.push_back((*iter)->getSelector());
749 std::string TypeStr;
750 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
751 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
752 }
753 // Collect the names of referenced protocols
754 llvm::SmallVector<std::string, 16> Protocols;
755 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
756 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
757 E = Protos.end(); I != E; ++I)
Chris Lattner271d4c22008-11-24 05:29:24 +0000758 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000759
760
761
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000762 // Get the superclass pointer.
763 llvm::Constant *SuperClass;
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000764 if (!SuperClassName.empty()) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000765 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
766 } else {
767 SuperClass = llvm::ConstantPointerNull::get(
768 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
769 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000770 // Empty vector used to construct empty method lists
771 llvm::SmallVector<llvm::Constant*, 1> empty;
772 // Generate the method and instance variable lists
773 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattner578279d2008-06-26 05:08:00 +0000774 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000775 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattner578279d2008-06-26 05:08:00 +0000776 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000777 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
778 IvarOffsets);
779 //Generate metaclass for class methods
780 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner4d018542008-07-21 06:31:05 +0000781 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000782 empty, empty, empty), ClassMethodList, NULLPtr);
783 // Generate the class structure
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000784 llvm::Constant *ClassStruct =
785 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
786 ClassName.c_str(), 0,
Sebastian Redle9e21b32008-12-04 00:10:55 +0000787 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000788 MethodList, GenerateProtocolList(Protocols));
789 // Add class structure to list to be added to the symtab later
790 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
791 Classes.push_back(ClassStruct);
792}
793
794llvm::Function *CGObjCGNU::ModuleInitFunction() {
795 // Only emit an ObjC load function if no Objective-C stuff has been called
796 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
797 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikovb41e4972008-06-01 15:14:46 +0000798 UntypedSelectors.empty())
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000799 return NULL;
Eli Friedman7719bc82008-06-01 16:00:02 +0000800
Chris Lattner3d9dcdd2009-01-21 18:52:19 +0000801 const llvm::StructType *SelStructTy =
Chris Lattnerd72ad582009-01-21 19:21:36 +0000802 cast<llvm::StructType>(SelectorTy->getElementType());
Chris Lattner3d9dcdd2009-01-21 18:52:19 +0000803
Eli Friedman7719bc82008-06-01 16:00:02 +0000804 // Name the ObjC types to make the IR a bit easier to read
805 TheModule.addTypeName(".objc_selector", SelectorTy);
806 TheModule.addTypeName(".objc_id", IdTy);
807 TheModule.addTypeName(".objc_imp", IMPTy);
808
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000809 std::vector<llvm::Constant*> Elements;
810 // Generate statics list:
811 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
812 ConstantStrings.size() + 1);
813 ConstantStrings.push_back(NULLPtr);
814 Elements.push_back(MakeConstantString("NSConstantString",
815 ".objc_static_class_name"));
816 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
817 llvm::StructType *StaticsListTy =
818 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattneref843042008-06-26 04:10:42 +0000819 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000820 llvm::Constant *Statics =
821 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner60eaae82008-06-24 17:01:28 +0000822 llvm::ArrayType *StaticsListArrayTy =
Chris Lattneref843042008-06-26 04:10:42 +0000823 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner60eaae82008-06-24 17:01:28 +0000824 Elements.clear();
825 Elements.push_back(Statics);
Chris Lattneref843042008-06-26 04:10:42 +0000826 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner60eaae82008-06-24 17:01:28 +0000827 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000828 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
829 // Array of classes, categories, and constant objects
830 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
831 Classes.size() + Categories.size() + 2);
Chris Lattneref843042008-06-26 04:10:42 +0000832 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelectorTy,
833 llvm::Type::Int16Ty,
834 llvm::Type::Int16Ty,
835 ClassListTy, NULL);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000836
837 Elements.clear();
838 // Pointer to an array of selectors used in this module.
839 std::vector<llvm::Constant*> Selectors;
840 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
841 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
842 iter != iterEnd ; ++iter) {
Chris Lattneref843042008-06-26 04:10:42 +0000843 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
844 Elements.push_back(MakeConstantString(iter->first.second,
845 ".objc_sel_types"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000846 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
847 Elements.clear();
848 }
849 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
850 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattneref843042008-06-26 04:10:42 +0000851 iter != iterEnd; ++iter) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000852 Elements.push_back(
Chris Lattneref843042008-06-26 04:10:42 +0000853 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000854 Elements.push_back(NULLPtr);
855 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
856 Elements.clear();
857 }
858 Elements.push_back(NULLPtr);
859 Elements.push_back(NULLPtr);
860 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
861 Elements.clear();
862 // Number of static selectors
863 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
864 llvm::Constant *SelectorList = MakeGlobal(
865 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
866 ".objc_selector_list");
867 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
868
869 // Now that all of the static selectors exist, create pointers to them.
870 int index = 0;
871 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
872 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
873 iter != iterEnd; ++iter) {
874 llvm::Constant *Idxs[] = {Zeros[0],
875 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
876 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
877 llvm::GlobalValue::InternalLinkage,
878 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
879 ".objc_sel_ptr", &TheModule);
880 (*iter).second->setAliasee(SelPtr);
881 }
882 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
883 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
884 iter != iterEnd; iter++) {
885 llvm::Constant *Idxs[] = {Zeros[0],
886 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
887 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
888 llvm::GlobalValue::InternalLinkage,
889 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
890 ".objc_sel_ptr", &TheModule);
891 (*iter).second->setAliasee(SelPtr);
892 }
893 // Number of classes defined.
894 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
895 Classes.size()));
896 // Number of categories defined
897 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
898 Categories.size()));
899 // Create an array of classes, then categories, then static object instances
900 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
901 // NULL-terminated list of static object instances (mainly constant strings)
902 Classes.push_back(Statics);
903 Classes.push_back(NULLPtr);
904 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
905 Elements.push_back(ClassList);
906 // Construct the symbol table
907 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
908
909 // The symbol table is contained in a module which has some version-checking
910 // constants
911 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
912 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
913 Elements.clear();
914 // Runtime version used for compatibility checking.
915 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
916 //FIXME: Should be sizeof(ModuleTy)
917 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
918 //FIXME: Should be the path to the file where this module was declared
919 Elements.push_back(NULLPtr);
920 Elements.push_back(SymTab);
921 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
922
923 // Create the load function calling the runtime entry point with the module
924 // structure
925 std::vector<const llvm::Type*> VoidArgs;
926 llvm::Function * LoadFunction = llvm::Function::Create(
927 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
928 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
929 &TheModule);
930 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000931 CGBuilderTy Builder;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000932 Builder.SetInsertPoint(EntryBB);
933 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
934 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
935 Builder.CreateCall(Register, Module);
936 Builder.CreateRetVoid();
937 return LoadFunction;
938}
Daniel Dunbarac93e472008-08-15 22:20:32 +0000939
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000940llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
941 const ObjCContainerDecl *CD) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000942 const ObjCCategoryImplDecl *OCD =
Steve Naroff438be772009-01-08 19:41:02 +0000943 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner3a8f2942008-11-24 03:33:13 +0000944 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
945 std::string ClassName = OMD->getClassInterface()->getNameAsString();
946 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregor5d764842009-01-09 17:18:27 +0000947 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000948
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000949 const llvm::FunctionType *MethodTy =
950 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000951 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
952 MethodName, isClassMethod);
953
Gabor Greif815e2c12008-04-06 20:42:52 +0000954 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattnerb326b172008-03-30 23:03:07 +0000955 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000956 FunctionName,
Chris Lattnerb326b172008-03-30 23:03:07 +0000957 &TheModule);
Chris Lattnerb326b172008-03-30 23:03:07 +0000958 return Method;
959}
960
Daniel Dunbarf7103722008-09-24 03:38:44 +0000961llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
962 return 0;
963}
964
965llvm::Function *CGObjCGNU::GetPropertySetFunction() {
966 return 0;
967}
968
969llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Anders Carlsson58d16242008-08-31 04:05:03 +0000970 return 0;
971}
972
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000973void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
974 const Stmt &S) {
975 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlssonb01a2112008-09-09 10:04:29 +0000976}
977
978void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbarf7103722008-09-24 03:38:44 +0000979 const ObjCAtThrowStmt &S) {
Anders Carlssonb01a2112008-09-09 10:04:29 +0000980 CGF.ErrorUnsupported(&S, "@throw statement");
981}
982
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000983llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000984 llvm::Value *AddrWeakObj)
985{
986 return 0;
987}
988
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000989void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
990 llvm::Value *src, llvm::Value *dst)
991{
992 return;
993}
994
Fariborz Jahanian17958902008-11-19 00:59:10 +0000995void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
996 llvm::Value *src, llvm::Value *dst)
997{
998 return;
999}
1000
Fariborz Jahanianf310b592008-11-20 19:23:36 +00001001void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1002 llvm::Value *src, llvm::Value *dst)
1003{
1004 return;
1005}
1006
Fariborz Jahanian17958902008-11-19 00:59:10 +00001007void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1008 llvm::Value *src, llvm::Value *dst)
1009{
1010 return;
1011}
1012
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001013CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattner547907c2008-06-26 04:19:03 +00001014 return new CGObjCGNU(CGM);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +00001015}