blob: ef4196e11fcdc655abff2114d61dd073ccdb80a5 [file] [log] [blame]
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
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//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbar1be1df32008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarace33292008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbarcffcdac2008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbar1be1df32008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000023#include "llvm/Module.h"
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000024#include "llvm/Support/IRBuilder.h"
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbarace33292008-08-16 03:19:19 +000026#include <sstream>
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000027
28using namespace clang;
29
30namespace {
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000031
Daniel Dunbarfe131f02008-08-27 02:31:56 +000032 typedef std::vector<llvm::Constant*> ConstantVector;
33
Daniel Dunbarcffcdac2008-08-13 03:21:16 +000034 // FIXME: We should find a nicer way to make the labels for
35 // metadata, string concatenation is lame.
36
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000037/// ObjCTypesHelper - Helper class that encapsulates lazy
38/// construction of varies types used during ObjC generation.
39class ObjCTypesHelper {
40private:
41 CodeGen::CodeGenModule &CGM;
42
Daniel Dunbar87062ff2008-08-23 09:25:55 +000043 llvm::Function *MessageSendFn, *MessageSendStretFn;
44 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn;
Daniel Dunbar5eec6142008-08-12 03:39:23 +000045
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000046public:
Daniel Dunbarb050fa62008-08-21 04:36:09 +000047 const llvm::Type *ShortTy, *IntTy, *LongTy;
48 const llvm::Type *Int8PtrTy;
Daniel Dunbar5eec6142008-08-12 03:39:23 +000049
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000050 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
51 const llvm::Type *ObjectPtrTy;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000052 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000053 const llvm::Type *SelectorPtrTy;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +000054 /// ProtocolPtrTy - LLVM type for external protocol handles
55 /// (typeof(Protocol))
56 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000057
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000058 // SuperCTy - clang type for struct objc_super.
59 QualType SuperCTy;
60 // SuperPtrCTy - clang type for struct objc_super *.
61 QualType SuperPtrCTy;
62
Daniel Dunbar15245e52008-08-23 04:28:29 +000063 /// SuperTy - LLVM type for struct objc_super.
64 const llvm::StructType *SuperTy;
Daniel Dunbar87062ff2008-08-23 09:25:55 +000065 /// SuperPtrTy - LLVM type for struct objc_super *.
66 const llvm::Type *SuperPtrTy;
Daniel Dunbar15245e52008-08-23 04:28:29 +000067
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000068 /// SymtabTy - LLVM type for struct objc_symtab.
69 const llvm::StructType *SymtabTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +000070 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
71 const llvm::Type *SymtabPtrTy;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000072 /// ModuleTy - LLVM type for struct objc_module.
73 const llvm::StructType *ModuleTy;
Daniel Dunbar5eec6142008-08-12 03:39:23 +000074
Daniel Dunbarcffcdac2008-08-13 03:21:16 +000075 /// ProtocolTy - LLVM type for struct objc_protocol.
76 const llvm::StructType *ProtocolTy;
77 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
78 const llvm::Type *ProtocolPtrTy;
79 /// ProtocolExtensionTy - LLVM type for struct
80 /// objc_protocol_extension.
81 const llvm::StructType *ProtocolExtensionTy;
82 /// ProtocolExtensionTy - LLVM type for struct
83 /// objc_protocol_extension *.
84 const llvm::Type *ProtocolExtensionPtrTy;
85 /// MethodDescriptionTy - LLVM type for struct
86 /// objc_method_description.
87 const llvm::StructType *MethodDescriptionTy;
88 /// MethodDescriptionListTy - LLVM type for struct
89 /// objc_method_description_list.
90 const llvm::StructType *MethodDescriptionListTy;
91 /// MethodDescriptionListPtrTy - LLVM type for struct
92 /// objc_method_description_list *.
93 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbara6eb6b72008-08-23 00:19:03 +000094 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
95 /// in GCC parlance).
96 const llvm::StructType *PropertyTy;
97 /// PropertyListTy - LLVM type for struct objc_property_list
98 /// (_prop_list_t in GCC parlance).
99 const llvm::StructType *PropertyListTy;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000100 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
101 const llvm::Type *PropertyListPtrTy;
102 /// ProtocolListTy - LLVM type for struct objc_property_list.
103 const llvm::Type *ProtocolListTy;
104 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
105 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000106 /// CategoryTy - LLVM type for struct objc_category.
107 const llvm::StructType *CategoryTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000108 /// ClassTy - LLVM type for struct objc_class.
109 const llvm::StructType *ClassTy;
110 /// ClassPtrTy - LLVM type for struct objc_class *.
111 const llvm::Type *ClassPtrTy;
112 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
113 const llvm::StructType *ClassExtensionTy;
114 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
115 const llvm::Type *ClassExtensionPtrTy;
116 /// CacheTy - LLVM type for struct objc_cache.
117 const llvm::Type *CacheTy;
118 /// CachePtrTy - LLVM type for struct objc_cache *.
119 const llvm::Type *CachePtrTy;
120 // IvarTy - LLVM type for struct objc_ivar.
121 const llvm::StructType *IvarTy;
122 /// IvarListTy - LLVM type for struct objc_ivar_list.
123 const llvm::Type *IvarListTy;
124 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
125 const llvm::Type *IvarListPtrTy;
126 // MethodTy - LLVM type for struct objc_method.
127 const llvm::StructType *MethodTy;
128 /// MethodListTy - LLVM type for struct objc_method_list.
129 const llvm::Type *MethodListTy;
130 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
131 const llvm::Type *MethodListPtrTy;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000132
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000133public:
134 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
135 ~ObjCTypesHelper();
136
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000137 llvm::Value *getMessageSendFn(bool IsSuper, const llvm::Type *ReturnTy);
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000138};
139
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000140class CGObjCMac : public CodeGen::CGObjCRuntime {
141private:
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000142 CodeGen::CodeGenModule &CGM;
143 ObjCTypesHelper ObjCTypes;
144 /// ObjCABI - FIXME: Not sure yet.
145 unsigned ObjCABI;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000146
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000147 /// LazySymbols - Symbols to generate a lazy reference for. See
148 /// DefinedSymbols and FinishModule().
149 std::set<IdentifierInfo*> LazySymbols;
150
151 /// DefinedSymbols - External symbols which are defined by this
152 /// module. The symbols in this list and LazySymbols are used to add
153 /// special linker symbols which ensure that Objective-C modules are
154 /// linked properly.
155 std::set<IdentifierInfo*> DefinedSymbols;
156
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000157 /// ClassNames - uniqued class names.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000158 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000159
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000160 /// MethodVarNames - uniqued method variable names.
161 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
162
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000163 /// MethodVarTypes - uniqued method type signatures. We have to use
164 /// a StringMap here because have no other unique reference.
165 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
166
Daniel Dunbar12996f52008-08-26 21:51:14 +0000167 /// MethodDefinitions - map of methods which have been defined in
168 /// this translation unit.
169 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
170
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000171 /// PropertyNames - uniqued method variable names.
172 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
173
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000174 /// ClassReferences - uniqued class references.
175 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
176
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000177 /// SelectorReferences - uniqued selector references.
178 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
179
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000180 /// Protocols - Protocols for which an objc_protocol structure has
181 /// been emitted. Forward declarations are handled by creating an
182 /// empty structure whose initializer is filled in when/if defined.
183 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
184
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000185 /// DefinedClasses - List of defined classes.
186 std::vector<llvm::GlobalValue*> DefinedClasses;
187
188 /// DefinedCategories - List of defined categories.
189 std::vector<llvm::GlobalValue*> DefinedCategories;
190
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000191 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000192 /// to prevent them from being clobbered.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000193 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000194
195 /// EmitImageInfo - Emit the image info marker used to encode some module
196 /// level information.
197 void EmitImageInfo();
198
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000199 /// EmitModuleInfo - Another marker encoding module level
200 /// information.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000201 void EmitModuleInfo();
202
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000203 /// EmitModuleSymols - Emit module symbols, the list of defined
204 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000205 llvm::Constant *EmitModuleSymbols();
206
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000207 /// FinishModule - Write out global data structures at the end of
208 /// processing a translation unit.
209 void FinishModule();
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000210
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000211 /// EmitClassExtension - Generate the class extension structure used
212 /// to store the weak ivar layout and properties. The return value
213 /// has type ClassExtensionPtrTy.
214 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
215
216 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
217 /// for the given class.
218 llvm::Value *EmitClassRef(llvm::IRBuilder<> &Builder,
219 const ObjCInterfaceDecl *ID);
220
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000221 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000222 QualType ResultType,
223 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000224 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000225 QualType Arg0Ty,
226 bool IsSuper,
227 const CallArgList &CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000228
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000229 /// EmitIvarList - Emit the ivar list for the given
230 /// implementation. If ForClass is true the list of class ivars
231 /// (i.e. metaclass ivars) is emitted, otherwise the list of
232 /// interface ivars will be emitted. The return value has type
233 /// IvarListPtrTy.
234 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
235 bool ForClass,
236 const llvm::Type *InterfaceTy);
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000237
238 /// EmitMetaClass - Emit a forward reference to the class structure
239 /// for the metaclass of the given interface. The return value has
240 /// type ClassPtrTy.
241 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
242
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000243 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000244 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000245 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
246 llvm::Constant *Protocols,
Daniel Dunbar12996f52008-08-26 21:51:14 +0000247 const llvm::Type *InterfaceTy,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000248 const ConstantVector &Methods);
249
250 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
251
252 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000253
254 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000255 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000256 llvm::Constant *EmitMethodList(const std::string &Name,
257 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000258 const ConstantVector &Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000259
260 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000261 /// method declarations.
262 /// - TypeName: The name for the type containing the methods.
263 /// - IsProtocol: True iff these methods are for a protocol.
264 /// - ClassMethds: True iff these are class methods.
265 /// - Required: When true, only "required" methods are
266 /// listed. Similarly, when false only "optional" methods are
267 /// listed. For classes this should always be true.
268 /// - begin, end: The method list to output.
269 ///
270 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000271 llvm::Constant *EmitMethodDescList(const std::string &Name,
272 const char *Section,
273 const ConstantVector &Methods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000274
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000275 /// EmitPropertyList - Emit the given property list. The return
276 /// value has type PropertyListPtrTy.
277 llvm::Constant *EmitPropertyList(const std::string &Name,
Daniel Dunbar698d6f32008-08-28 04:38:10 +0000278 const Decl *Container,
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000279 ObjCPropertyDecl * const *begin,
280 ObjCPropertyDecl * const *end);
281
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000282 /// EmitProtocolExtension - Generate the protocol extension
283 /// structure used to store optional instance and class methods, and
284 /// protocol properties. The return value has type
285 /// ProtocolExtensionPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000286 llvm::Constant *
287 EmitProtocolExtension(const ObjCProtocolDecl *PD,
288 const ConstantVector &OptInstanceMethods,
289 const ConstantVector &OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000290
291 /// EmitProtocolList - Generate the list of referenced
292 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000293 llvm::Constant *EmitProtocolList(const std::string &Name,
294 ObjCProtocolDecl::protocol_iterator begin,
295 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000296
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000297 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
298 /// for the given selector.
299 llvm::Value *EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel);
300
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000301 /// GetProtocolRef - Return a reference to the internal protocol
302 /// description, creating an empty one if it has not been
303 /// defined. The return value has type pointer-to ProtocolTy.
304 llvm::GlobalVariable *GetProtocolRef(const ObjCProtocolDecl *PD);
305
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000306 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000307 /// name. The return value has type char *.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000308 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000309
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000310 /// GetMethodVarName - Return a unique constant for the given
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000311 /// selector's name. The return value has type char *.
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000312 llvm::Constant *GetMethodVarName(Selector Sel);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000313 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000314 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000315
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000316 /// GetMethodVarType - Return a unique constant for the given
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000317 /// selector's name. The return value has type char *.
318
319 // FIXME: This is a horrible name.
Daniel Dunbar12996f52008-08-26 21:51:14 +0000320 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000321 llvm::Constant *GetMethodVarType(const std::string &Name);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000322
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000323 /// GetPropertyName - Return a unique constant for the given
324 /// name. The return value has type char *.
325 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
326
Daniel Dunbar698d6f32008-08-28 04:38:10 +0000327 // FIXME: This can be dropped once string functions are unified.
328 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
329 const Decl *Container);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000330
Daniel Dunbarace33292008-08-16 03:19:19 +0000331 /// GetNameForMethod - Return a name for the given method.
332 /// \param[out] NameOut - The return value.
333 void GetNameForMethod(const ObjCMethodDecl *OMD,
334 std::string &NameOut);
335
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000336public:
337 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000338 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000339
Daniel Dunbara04840b2008-08-23 03:46:30 +0000340 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000341 QualType ResultType,
342 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000343 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000344 bool IsClassMessage,
345 const CallArgList &CallArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000346
Daniel Dunbara04840b2008-08-23 03:46:30 +0000347 virtual CodeGen::RValue
348 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000349 QualType ResultType,
350 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000351 const ObjCInterfaceDecl *Class,
352 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000353 bool IsClassMessage,
354 const CallArgList &CallArgs);
Daniel Dunbar434627a2008-08-16 00:25:02 +0000355
356 virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000357 const ObjCInterfaceDecl *ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000358
359 virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
360
Daniel Dunbarac93e472008-08-15 22:20:32 +0000361 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000362
Daniel Dunbarac93e472008-08-15 22:20:32 +0000363 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000364
Daniel Dunbarac93e472008-08-15 22:20:32 +0000365 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000366
367 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000368 const ObjCProtocolDecl *PD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000369
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000370 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000371
372 virtual llvm::Function *ModuleInitFunction();
373};
374} // end anonymous namespace
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000375
376/* *** Helper Functions *** */
377
378/// getConstantGEP() - Help routine to construct simple GEPs.
379static llvm::Constant *getConstantGEP(llvm::Constant *C,
380 unsigned idx0,
381 unsigned idx1) {
382 llvm::Value *Idxs[] = {
383 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
384 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
385 };
386 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
387}
388
389/* *** CGObjCMac Public Interface *** */
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000390
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000391CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm)
392 : CGM(cgm),
393 ObjCTypes(cgm),
394 ObjCABI(1)
395{
396 // FIXME: How does this get set in GCC? And what does it even mean?
397 if (ObjCTypes.LongTy != CGM.getTypes().ConvertType(CGM.getContext().IntTy))
398 ObjCABI = 2;
399
400 EmitImageInfo();
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000401}
402
Daniel Dunbar434627a2008-08-16 00:25:02 +0000403/// GetClass - Return a reference to the class for the given interface
404/// decl.
405llvm::Value *CGObjCMac::GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000406 const ObjCInterfaceDecl *ID) {
407 return EmitClassRef(Builder, ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000408}
409
410/// GetSelector - Return the pointer to the unique'd string for this selector.
411llvm::Value *CGObjCMac::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000412 return EmitSelector(Builder, Sel);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000413}
414
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000415/// Generate a constant CFString object.
416/*
417 struct __builtin_CFString {
418 const int *isa; // point to __CFConstantStringClassReference
419 int flags;
420 const char *str;
421 long length;
422 };
423*/
424
425llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000426 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000427}
428
429/// Generates a message send where the super is the receiver. This is
430/// a message send to self with special delivery semantics indicating
431/// which class's method should be called.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000432CodeGen::RValue
433CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000434 QualType ResultType,
435 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000436 const ObjCInterfaceDecl *Class,
437 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000438 bool IsClassMessage,
439 const CallArgList &CallArgs) {
Daniel Dunbar15245e52008-08-23 04:28:29 +0000440 // Create and init a super structure; this is a (receiver, class)
441 // pair we will pass to objc_msgSendSuper.
442 llvm::Value *ObjCSuper =
443 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
444 llvm::Value *ReceiverAsObject =
445 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
446 CGF.Builder.CreateStore(ReceiverAsObject,
447 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar15245e52008-08-23 04:28:29 +0000448
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000449 // If this is a class message the metaclass is passed as the target.
450 llvm::Value *Target;
451 if (IsClassMessage) {
452 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
453 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
454 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
455 Target = Super;
456 } else {
457 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
458 }
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000459 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
460 // and ObjCTypes types.
461 const llvm::Type *ClassTy =
462 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
463 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000464 CGF.Builder.CreateStore(Target,
465 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
466
Daniel Dunbardd851282008-08-30 05:35:15 +0000467 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000468 ObjCSuper, ObjCTypes.SuperPtrCTy,
469 true, CallArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000470}
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000471
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000472/// Generate code for a message send expression.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000473CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000474 QualType ResultType,
475 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000476 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000477 bool IsClassMessage,
478 const CallArgList &CallArgs) {
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000479 llvm::Value *Arg0 =
480 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbardd851282008-08-30 05:35:15 +0000481 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000482 Arg0, CGF.getContext().getObjCIdType(),
483 false, CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000484}
485
486CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000487 QualType ResultType,
488 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000489 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000490 QualType Arg0Ty,
491 bool IsSuper,
492 const CallArgList &CallArgs) {
493 CallArgList ActualArgs;
494 ActualArgs.push_back(std::make_pair(Arg0, Arg0Ty));
Daniel Dunbardd851282008-08-30 05:35:15 +0000495 ActualArgs.push_back(std::make_pair(EmitSelector(CGF.Builder, Sel),
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000496 CGF.getContext().getObjCSelType()));
497 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000498
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000499 // FIXME: This is a hack, we are implicitly coordinating with
500 // EmitCallExprExt, which will move the return type to the first
501 // parameter and set the structure return flag. See
502 // getMessageSendFn().
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000503
Daniel Dunbardd851282008-08-30 05:35:15 +0000504 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000505 return CGF.EmitCall(ObjCTypes.getMessageSendFn(IsSuper, ReturnTy),
Daniel Dunbardd851282008-08-30 05:35:15 +0000506 ResultType, ActualArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000507}
508
509llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000510 const ObjCProtocolDecl *PD) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000511 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
512 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000513}
514
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000515/*
516 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
517 struct _objc_protocol {
518 struct _objc_protocol_extension *isa;
519 char *protocol_name;
520 struct _objc_protocol_list *protocol_list;
521 struct _objc__method_prototype_list *instance_methods;
522 struct _objc__method_prototype_list *class_methods
523 };
524
525 See EmitProtocolExtension().
526*/
527void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000528 // FIXME: I don't understand why gcc generates this, or where it is
529 // resolved. Investigate. Its also wasteful to look this up over and
530 // over.
531 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
532
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000533 const char *ProtocolName = PD->getName();
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000534
535 // Construct method lists.
536 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
537 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
538 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
539 e = PD->instmeth_end(); i != e; ++i) {
540 ObjCMethodDecl *MD = *i;
541 llvm::Constant *C = GetMethodDescriptionConstant(MD);
542 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
543 OptInstanceMethods.push_back(C);
544 } else {
545 InstanceMethods.push_back(C);
546 }
547 }
548
549 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
550 e = PD->classmeth_end(); i != e; ++i) {
551 ObjCMethodDecl *MD = *i;
552 llvm::Constant *C = GetMethodDescriptionConstant(MD);
553 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
554 OptClassMethods.push_back(C);
555 } else {
556 ClassMethods.push_back(C);
557 }
558 }
559
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000560 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000561 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000562 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000563 Values[2] =
564 EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(),
565 PD->protocol_begin(),
566 PD->protocol_end());
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000567 Values[3] =
568 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_")
569 + PD->getName(),
570 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
571 InstanceMethods);
572 Values[4] =
573 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_")
574 + PD->getName(),
575 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
576 ClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000577 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
578 Values);
579
580 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
581 if (Entry) {
582 // Already created, just update the initializer
583 Entry->setInitializer(Init);
584 } else {
585 Entry =
586 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
587 llvm::GlobalValue::InternalLinkage,
588 Init,
589 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
590 &CGM.getModule());
591 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
592 UsedGlobals.push_back(Entry);
593 // FIXME: Is this necessary? Why only for protocol?
594 Entry->setAlignment(4);
595 }
596}
597
598llvm::GlobalVariable *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
599 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
600
601 if (!Entry) {
602 std::vector<llvm::Constant*> Values(5);
603 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
604 Values[1] = GetClassName(PD->getIdentifier());
605 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
606 Values[3] = Values[4] =
607 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
608 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
609 Values);
610
611 Entry =
612 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
613 llvm::GlobalValue::InternalLinkage,
614 Init,
615 std::string("\01L_OBJC_PROTOCOL_")+PD->getName(),
616 &CGM.getModule());
617 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
618 UsedGlobals.push_back(Entry);
619 // FIXME: Is this necessary? Why only for protocol?
620 Entry->setAlignment(4);
621 }
622
623 return Entry;
624}
625
626/*
627 struct _objc_protocol_extension {
628 uint32_t size;
629 struct objc_method_description_list *optional_instance_methods;
630 struct objc_method_description_list *optional_class_methods;
631 struct objc_property_list *instance_properties;
632 };
633*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000634llvm::Constant *
635CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
636 const ConstantVector &OptInstanceMethods,
637 const ConstantVector &OptClassMethods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000638 uint64_t Size =
639 CGM.getTargetData().getABITypeSize(ObjCTypes.ProtocolExtensionTy);
640 std::vector<llvm::Constant*> Values(4);
641 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000642 Values[1] =
643 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_")
644 + PD->getName(),
645 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
646 OptInstanceMethods);
647 Values[2] =
648 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_")
649 + PD->getName(),
650 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
651 OptClassMethods);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000652 Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") +
653 PD->getName(),
Daniel Dunbar698d6f32008-08-28 04:38:10 +0000654 0,
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000655 PD->classprop_begin(),
656 PD->classprop_end());
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000657
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000658 // Return null if no extension bits are used.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000659 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
660 Values[3]->isNullValue())
661 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
662
663 llvm::Constant *Init =
664 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
665 llvm::GlobalVariable *GV =
666 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
667 llvm::GlobalValue::InternalLinkage,
668 Init,
669 (std::string("\01L_OBJC_PROTOCOLEXT_") +
670 PD->getName()),
671 &CGM.getModule());
672 // No special section, but goes in llvm.used
673 UsedGlobals.push_back(GV);
674
675 return GV;
676}
677
678/*
679 struct objc_protocol_list {
680 struct objc_protocol_list *next;
681 long count;
682 Protocol *list[];
683 };
684*/
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000685llvm::Constant *
686CGObjCMac::EmitProtocolList(const std::string &Name,
687 ObjCProtocolDecl::protocol_iterator begin,
688 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000689 std::vector<llvm::Constant*> ProtocolRefs;
690
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000691 for (; begin != end; ++begin)
692 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000693
694 // Just return null for empty protocol lists
695 if (ProtocolRefs.empty())
696 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
697
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000698 // This list is null terminated.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000699 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
700
701 std::vector<llvm::Constant*> Values(3);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000702 // This field is only used by the runtime.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000703 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
704 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
705 Values[2] =
706 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
707 ProtocolRefs.size()),
708 ProtocolRefs);
709
710 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
711 llvm::GlobalVariable *GV =
712 new llvm::GlobalVariable(Init->getType(), false,
713 llvm::GlobalValue::InternalLinkage,
714 Init,
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000715 Name,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000716 &CGM.getModule());
717 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
718 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
719}
720
721/*
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000722 struct _objc_property {
723 const char * const name;
724 const char * const attributes;
725 };
726
727 struct _objc_property_list {
728 uint32_t entsize; // sizeof (struct _objc_property)
729 uint32_t prop_count;
730 struct _objc_property[prop_count];
731 };
732*/
733llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbar698d6f32008-08-28 04:38:10 +0000734 const Decl *Container,
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000735 ObjCPropertyDecl * const *begin,
736 ObjCPropertyDecl * const *end) {
737 std::vector<llvm::Constant*> Properties, Prop(2);
738 for (; begin != end; ++begin) {
739 const ObjCPropertyDecl *PD = *begin;
740 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar698d6f32008-08-28 04:38:10 +0000741 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000742 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
743 Prop));
744 }
745
746 // Return null for empty list.
747 if (Properties.empty())
748 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
749
750 unsigned PropertySize =
751 CGM.getTargetData().getABITypeSize(ObjCTypes.PropertyTy);
752 std::vector<llvm::Constant*> Values(3);
753 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
754 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
755 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
756 Properties.size());
757 Values[2] = llvm::ConstantArray::get(AT, Properties);
758 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
759
760 llvm::GlobalVariable *GV =
761 new llvm::GlobalVariable(Init->getType(), false,
762 llvm::GlobalValue::InternalLinkage,
763 Init,
764 Name,
765 &CGM.getModule());
766 // No special section on property lists?
767 UsedGlobals.push_back(GV);
768 return llvm::ConstantExpr::getBitCast(GV,
769 ObjCTypes.PropertyListPtrTy);
770
771}
772
773/*
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000774 struct objc_method_description_list {
775 int count;
776 struct objc_method_description list[];
777 };
778*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000779llvm::Constant *
780CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
781 std::vector<llvm::Constant*> Desc(2);
782 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
783 ObjCTypes.SelectorPtrTy);
784 Desc[1] = GetMethodVarType(MD);
785 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
786 Desc);
787}
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000788
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000789llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
790 const char *Section,
791 const ConstantVector &Methods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000792 // Return null for empty list.
793 if (Methods.empty())
794 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
795
796 std::vector<llvm::Constant*> Values(2);
797 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
798 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
799 Methods.size());
800 Values[1] = llvm::ConstantArray::get(AT, Methods);
801 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
802
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000803 llvm::GlobalVariable *GV =
804 new llvm::GlobalVariable(Init->getType(), false,
805 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000806 Init, Name, &CGM.getModule());
807 GV->setSection(Section);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000808 UsedGlobals.push_back(GV);
809 return llvm::ConstantExpr::getBitCast(GV,
810 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000811}
812
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000813/*
814 struct _objc_category {
815 char *category_name;
816 char *class_name;
817 struct _objc_method_list *instance_methods;
818 struct _objc_method_list *class_methods;
819 struct _objc_protocol_list *protocols;
820 uint32_t size; // <rdar://4585769>
821 struct _objc_property_list *instance_properties;
822 };
823 */
Daniel Dunbarac93e472008-08-15 22:20:32 +0000824void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000825 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.CategoryTy);
826
Daniel Dunbar0cd49032008-08-26 23:03:11 +0000827 // FIXME: This is poor design, the OCD should have a pointer to the
828 // category decl. Additionally, note that Category can be null for
829 // the @implementation w/o an @interface case. Sema should just
830 // create one for us as it does for @implementation so everyone else
831 // can live life under a clear blue sky.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000832 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar0cd49032008-08-26 23:03:11 +0000833 const ObjCCategoryDecl *Category =
834 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000835 std::string ExtName(std::string(Interface->getName()) +
836 "_" +
837 OCD->getName());
838
Daniel Dunbar12996f52008-08-26 21:51:14 +0000839 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
840 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
841 e = OCD->instmeth_end(); i != e; ++i) {
842 // Instance methods should always be defined.
843 InstanceMethods.push_back(GetMethodConstant(*i));
844 }
845 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
846 e = OCD->classmeth_end(); i != e; ++i) {
847 // Class methods should always be defined.
848 ClassMethods.push_back(GetMethodConstant(*i));
849 }
850
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000851 std::vector<llvm::Constant*> Values(7);
852 Values[0] = GetClassName(OCD->getIdentifier());
853 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000854 Values[2] =
855 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
856 ExtName,
857 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +0000858 InstanceMethods);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000859 Values[3] =
860 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
861 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +0000862 ClassMethods);
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000863 if (Category) {
864 Values[4] =
865 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
866 Category->protocol_begin(),
867 Category->protocol_end());
868 } else {
869 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
870 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000871 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar0cd49032008-08-26 23:03:11 +0000872
873 // If there is no category @interface then there can be no properties.
874 if (Category) {
875 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Daniel Dunbar698d6f32008-08-28 04:38:10 +0000876 OCD,
Daniel Dunbar0cd49032008-08-26 23:03:11 +0000877 Category->classprop_begin(),
878 Category->classprop_end());
879 } else {
880 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
881 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000882
883 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
884 Values);
885
886 llvm::GlobalVariable *GV =
887 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
888 llvm::GlobalValue::InternalLinkage,
889 Init,
890 std::string("\01L_OBJC_CATEGORY_")+ExtName,
891 &CGM.getModule());
892 GV->setSection("__OBJC,__category,regular,no_dead_strip");
893 UsedGlobals.push_back(GV);
894 DefinedCategories.push_back(GV);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000895}
896
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000897// FIXME: Get from somewhere?
898enum ClassFlags {
899 eClassFlags_Factory = 0x00001,
900 eClassFlags_Meta = 0x00002,
901 // <rdr://5142207>
902 eClassFlags_HasCXXStructors = 0x02000,
903 eClassFlags_Hidden = 0x20000,
904 eClassFlags_ABI2_Hidden = 0x00010,
905 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
906};
907
908// <rdr://5142207&4705298&4843145>
909static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
910 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
911 // FIXME: Support -fvisibility
912 switch (attr->getVisibility()) {
913 default:
914 assert(0 && "Unknown visibility");
915 return false;
916 case VisibilityAttr::DefaultVisibility:
917 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
918 return false;
919 case VisibilityAttr::HiddenVisibility:
920 return true;
921 }
922 } else {
923 return false; // FIXME: Support -fvisibility
924 }
925}
926
927/*
928 struct _objc_class {
929 Class isa;
930 Class super_class;
931 const char *name;
932 long version;
933 long info;
934 long instance_size;
935 struct _objc_ivar_list *ivars;
936 struct _objc_method_list *methods;
937 struct _objc_cache *cache;
938 struct _objc_protocol_list *protocols;
939 // Objective-C 1.0 extensions (<rdr://4585769>)
940 const char *ivar_layout;
941 struct _objc_class_ext *ext;
942 };
943
944 See EmitClassExtension();
945 */
946void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000947 DefinedSymbols.insert(ID->getIdentifier());
948
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000949 const char *ClassName = ID->getName();
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000950 // FIXME: Gross
951 ObjCInterfaceDecl *Interface =
952 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000953 llvm::Constant *Protocols =
954 EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(),
955 Interface->protocol_begin(),
956 Interface->protocol_end());
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000957 const llvm::Type *InterfaceTy =
958 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
959 unsigned Flags = eClassFlags_Factory;
960 unsigned Size = CGM.getTargetData().getABITypeSize(InterfaceTy);
961
962 // FIXME: Set CXX-structors flag.
963 if (IsClassHidden(ID->getClassInterface()))
964 Flags |= eClassFlags_Hidden;
965
Daniel Dunbar12996f52008-08-26 21:51:14 +0000966 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
967 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
968 e = ID->instmeth_end(); i != e; ++i) {
969 // Instance methods should always be defined.
970 InstanceMethods.push_back(GetMethodConstant(*i));
971 }
972 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
973 e = ID->classmeth_end(); i != e; ++i) {
974 // Class methods should always be defined.
975 ClassMethods.push_back(GetMethodConstant(*i));
976 }
977
978 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
979 e = ID->propimpl_end(); i != e; ++i) {
980 ObjCPropertyImplDecl *PID = *i;
981
982 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
983 ObjCPropertyDecl *PD = PID->getPropertyDecl();
984
985 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
986 if (llvm::Constant *C = GetMethodConstant(MD))
987 InstanceMethods.push_back(C);
988 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
989 if (llvm::Constant *C = GetMethodConstant(MD))
990 InstanceMethods.push_back(C);
991 }
992 }
993
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000994 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar12996f52008-08-26 21:51:14 +0000995 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000996 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000997 // Record a reference to the super class.
998 LazySymbols.insert(Super->getIdentifier());
999
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001000 Values[ 1] =
1001 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1002 ObjCTypes.ClassPtrTy);
1003 } else {
1004 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1005 }
1006 Values[ 2] = GetClassName(ID->getIdentifier());
1007 // Version is always 0.
1008 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1009 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1010 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1011 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001012 Values[ 7] =
1013 EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(),
1014 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001015 InstanceMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001016 // cache is always NULL.
1017 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1018 Values[ 9] = Protocols;
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001019 // FIXME: Set ivar_layout
1020 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001021 Values[11] = EmitClassExtension(ID);
1022 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1023 Values);
1024
1025 llvm::GlobalVariable *GV =
1026 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1027 llvm::GlobalValue::InternalLinkage,
1028 Init,
1029 std::string("\01L_OBJC_CLASS_")+ClassName,
1030 &CGM.getModule());
1031 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1032 UsedGlobals.push_back(GV);
1033 // FIXME: Why?
1034 GV->setAlignment(32);
1035 DefinedClasses.push_back(GV);
1036}
1037
1038llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1039 llvm::Constant *Protocols,
Daniel Dunbar12996f52008-08-26 21:51:14 +00001040 const llvm::Type *InterfaceTy,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001041 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001042 const char *ClassName = ID->getName();
1043 unsigned Flags = eClassFlags_Meta;
1044 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ClassTy);
1045
1046 if (IsClassHidden(ID->getClassInterface()))
1047 Flags |= eClassFlags_Hidden;
1048
1049 std::vector<llvm::Constant*> Values(12);
1050 // The isa for the metaclass is the root of the hierarchy.
1051 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1052 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1053 Root = Super;
1054 Values[ 0] =
1055 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1056 ObjCTypes.ClassPtrTy);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001057 // The super class for the metaclass is emitted as the name of the
1058 // super class. The runtime fixes this up to point to the
1059 // *metaclass* for the super class.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001060 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1061 Values[ 1] =
1062 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1063 ObjCTypes.ClassPtrTy);
1064 } else {
1065 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1066 }
1067 Values[ 2] = GetClassName(ID->getIdentifier());
1068 // Version is always 0.
1069 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1070 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1071 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1072 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001073 Values[ 7] =
1074 EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(),
1075 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001076 Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001077 // cache is always NULL.
1078 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1079 Values[ 9] = Protocols;
1080 // ivar_layout for metaclass is always NULL.
1081 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1082 // The class extension is always unused for metaclasses.
1083 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1084 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1085 Values);
1086
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001087 std::string Name("\01L_OBJC_METACLASS_");
1088 Name += ClassName;
1089
1090 // Check for a forward reference.
1091 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1092 if (GV) {
1093 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1094 "Forward metaclass reference has incorrect type.");
1095 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1096 GV->setInitializer(Init);
1097 } else {
1098 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1099 llvm::GlobalValue::InternalLinkage,
1100 Init, Name,
1101 &CGM.getModule());
1102 }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001103 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1104 UsedGlobals.push_back(GV);
1105 // FIXME: Why?
1106 GV->setAlignment(32);
1107
1108 return GV;
1109}
1110
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001111llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
1112 std::string Name("\01L_OBJC_METACLASS_");
1113 Name += ID->getName();
1114
1115 // FIXME: Should we look these up somewhere other than the
1116 // module. Its a bit silly since we only generate these while
1117 // processing an implementation, so exactly one pointer would work
1118 // if know when we entered/exitted an implementation block.
1119
1120 // Check for an existing forward reference.
1121 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name)) {
1122 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1123 "Forward metaclass reference has incorrect type.");
1124 return GV;
1125 } else {
1126 // Generate as an external reference to keep a consistent
1127 // module. This will be patched up when we emit the metaclass.
1128 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1129 llvm::GlobalValue::ExternalLinkage,
1130 0,
1131 Name,
1132 &CGM.getModule());
1133 }
1134}
1135
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001136/*
1137 struct objc_class_ext {
1138 uint32_t size;
1139 const char *weak_ivar_layout;
1140 struct _objc_property_list *properties;
1141 };
1142*/
1143llvm::Constant *
1144CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1145 uint64_t Size =
1146 CGM.getTargetData().getABITypeSize(ObjCTypes.ClassExtensionTy);
1147
1148 std::vector<llvm::Constant*> Values(3);
1149 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001150 // FIXME: Output weak_ivar_layout string.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001151 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001152 Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") +
1153 ID->getName(),
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001154 ID,
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001155 ID->getClassInterface()->classprop_begin(),
1156 ID->getClassInterface()->classprop_end());
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001157
1158 // Return null if no extension bits are used.
1159 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1160 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1161
1162 llvm::Constant *Init =
1163 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1164 llvm::GlobalVariable *GV =
1165 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1166 llvm::GlobalValue::InternalLinkage,
1167 Init,
1168 (std::string("\01L_OBJC_CLASSEXT_") +
1169 ID->getName()),
1170 &CGM.getModule());
1171 // No special section, but goes in llvm.used
1172 UsedGlobals.push_back(GV);
1173
1174 return GV;
1175}
1176
1177/*
1178 struct objc_ivar {
1179 char *ivar_name;
1180 char *ivar_type;
1181 int ivar_offset;
1182 };
1183
1184 struct objc_ivar_list {
1185 int ivar_count;
1186 struct objc_ivar list[count];
1187 };
1188 */
1189llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1190 bool ForClass,
1191 const llvm::Type *InterfaceTy) {
1192 std::vector<llvm::Constant*> Ivars, Ivar(3);
1193
1194 // When emitting the root class GCC emits ivar entries for the
1195 // actual class structure. It is not clear if we need to follow this
1196 // behavior; for now lets try and get away with not doing it. If so,
1197 // the cleanest solution would be to make up an ObjCInterfaceDecl
1198 // for the class.
1199 if (ForClass)
1200 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1201
1202 const llvm::StructLayout *Layout =
1203 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
1204 for (ObjCInterfaceDecl::ivar_iterator
1205 i = ID->getClassInterface()->ivar_begin(),
1206 e = ID->getClassInterface()->ivar_end(); i != e; ++i) {
1207 ObjCIvarDecl *V = *i;
1208 unsigned Offset =
1209 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(V));
1210 std::string TypeStr;
1211 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1212 Ivar[0] = GetMethodVarName(V->getIdentifier());
1213 CGM.getContext().getObjCEncodingForType(V->getType(), TypeStr,
1214 EncodingRecordTypes);
1215 Ivar[1] = GetMethodVarType(TypeStr);
1216 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
1217 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy,
1218 Ivar));
1219 }
1220
1221 // Return null for empty list.
1222 if (Ivars.empty())
1223 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1224
1225 std::vector<llvm::Constant*> Values(2);
1226 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1227 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1228 Ivars.size());
1229 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1230 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1231
1232 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1233 "\01L_OBJC_INSTANCE_VARIABLES_");
1234 llvm::GlobalVariable *GV =
1235 new llvm::GlobalVariable(Init->getType(), false,
1236 llvm::GlobalValue::InternalLinkage,
1237 Init,
1238 std::string(Prefix) + ID->getName(),
1239 &CGM.getModule());
1240 if (ForClass) {
1241 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1242 // FIXME: Why is this only here?
1243 GV->setAlignment(32);
1244 } else {
1245 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1246 }
1247 UsedGlobals.push_back(GV);
1248 return llvm::ConstantExpr::getBitCast(GV,
1249 ObjCTypes.IvarListPtrTy);
1250}
1251
1252/*
1253 struct objc_method {
1254 SEL method_name;
1255 char *method_types;
1256 void *method;
1257 };
1258
1259 struct objc_method_list {
1260 struct objc_method_list *obsolete;
1261 int count;
1262 struct objc_method methods_list[count];
1263 };
1264*/
Daniel Dunbar12996f52008-08-26 21:51:14 +00001265
1266/// GetMethodConstant - Return a struct objc_method constant for the
1267/// given method if it has been defined. The result is null if the
1268/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001269llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001270 // FIXME: Use DenseMap::lookup
1271 llvm::Function *Fn = MethodDefinitions[MD];
1272 if (!Fn)
1273 return 0;
1274
1275 std::vector<llvm::Constant*> Method(3);
1276 Method[0] =
1277 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1278 ObjCTypes.SelectorPtrTy);
1279 Method[1] = GetMethodVarType(MD);
1280 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1281 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1282}
1283
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001284llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1285 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001286 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001287 // Return null for empty list.
1288 if (Methods.empty())
1289 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1290
1291 std::vector<llvm::Constant*> Values(3);
1292 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1293 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1294 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1295 Methods.size());
1296 Values[2] = llvm::ConstantArray::get(AT, Methods);
1297 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1298
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001299 llvm::GlobalVariable *GV =
1300 new llvm::GlobalVariable(Init->getType(), false,
1301 llvm::GlobalValue::InternalLinkage,
1302 Init,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001303 Name,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001304 &CGM.getModule());
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001305 GV->setSection(Section);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001306 UsedGlobals.push_back(GV);
1307 return llvm::ConstantExpr::getBitCast(GV,
1308 ObjCTypes.MethodListPtrTy);
Daniel Dunbarace33292008-08-16 03:19:19 +00001309}
1310
1311llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD) {
1312 const llvm::Type *ReturnTy =
1313 CGM.getTypes().ConvertReturnType(OMD->getResultType());
1314 const llvm::Type *SelfTy =
1315 CGM.getTypes().ConvertType(OMD->getSelfDecl()->getType());
1316
1317 std::vector<const llvm::Type*> ArgTys;
1318 ArgTys.reserve(1 + 2 + OMD->param_size());
1319
1320 // FIXME: This is not something we should have to be dealing with
1321 // here.
1322 bool useStructRet =
1323 CodeGen::CodeGenFunction::hasAggregateLLVMType(OMD->getResultType());
1324 if (useStructRet) {
1325 ArgTys.push_back(llvm::PointerType::getUnqual(ReturnTy));
1326 ReturnTy = llvm::Type::VoidTy;
1327 }
1328
1329 // Implicit arguments
1330 ArgTys.push_back(SelfTy);
1331 ArgTys.push_back(ObjCTypes.SelectorPtrTy);
1332
1333 for (ObjCMethodDecl::param_const_iterator
1334 i = OMD->param_begin(), e = OMD->param_end();
1335 i != e; ++i) {
1336 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
Daniel Dunbara04840b2008-08-23 03:46:30 +00001337 if (Ty->isSingleValueType()) {
Daniel Dunbarace33292008-08-16 03:19:19 +00001338 ArgTys.push_back(Ty);
1339 } else {
1340 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
1341 }
1342 }
1343
1344 std::string Name;
1345 GetNameForMethod(OMD, Name);
1346
1347 llvm::Function *Method =
1348 llvm::Function::Create(llvm::FunctionType::get(ReturnTy,
1349 ArgTys,
1350 OMD->isVariadic()),
1351 llvm::GlobalValue::InternalLinkage,
1352 Name,
1353 &CGM.getModule());
Daniel Dunbar12996f52008-08-26 21:51:14 +00001354 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarace33292008-08-16 03:19:19 +00001355
Daniel Dunbara04840b2008-08-23 03:46:30 +00001356 unsigned Offset = 3; // Return plus self and selector implicit args.
1357 if (useStructRet) {
Daniel Dunbarace33292008-08-16 03:19:19 +00001358 Method->addParamAttr(1, llvm::ParamAttr::StructRet);
Daniel Dunbara04840b2008-08-23 03:46:30 +00001359 ++Offset;
1360 }
1361
1362 // FIXME: This is horrible, we need to be reusing the machinery in
1363 // CodeGenModule.cpp (SetFunctionAttributes).
1364 for (ObjCMethodDecl::param_const_iterator
1365 i = OMD->param_begin(), e = OMD->param_end();
1366 i != e; ++i, ++Offset) {
1367 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
1368 if (!Ty->isSingleValueType())
1369 Method->addParamAttr(Offset, llvm::ParamAttr::ByVal);
1370 }
Daniel Dunbarace33292008-08-16 03:19:19 +00001371
1372 return Method;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001373}
1374
1375llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001376 // Abuse this interface function as a place to finalize.
1377 FinishModule();
1378
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001379 return NULL;
1380}
1381
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001382/* *** Private Interface *** */
1383
1384/// EmitImageInfo - Emit the image info marker used to encode some module
1385/// level information.
1386///
1387/// See: <rdr://4810609&4810587&4810587>
1388/// struct IMAGE_INFO {
1389/// unsigned version;
1390/// unsigned flags;
1391/// };
1392enum ImageInfoFlags {
1393 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
1394 eImageInfo_GarbageCollected = (1 << 1),
1395 eImageInfo_GCOnly = (1 << 2)
1396};
1397
1398void CGObjCMac::EmitImageInfo() {
1399 unsigned version = 0; // Version is unused?
1400 unsigned flags = 0;
1401
1402 // FIXME: Fix and continue?
1403 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
1404 flags |= eImageInfo_GarbageCollected;
1405 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
1406 flags |= eImageInfo_GCOnly;
1407
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001408 // Emitted as int[2];
1409 llvm::Constant *values[2] = {
1410 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
1411 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
1412 };
1413 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001414 llvm::GlobalVariable *GV =
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001415 new llvm::GlobalVariable(AT, true,
1416 llvm::GlobalValue::InternalLinkage,
1417 llvm::ConstantArray::get(AT, values, 2),
1418 "\01L_OBJC_IMAGE_INFO",
1419 &CGM.getModule());
1420
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001421 if (ObjCABI == 1) {
1422 GV->setSection("__OBJC, __image_info,regular");
1423 } else {
1424 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
1425 }
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001426
1427 UsedGlobals.push_back(GV);
1428}
1429
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001430
1431// struct objc_module {
1432// unsigned long version;
1433// unsigned long size;
1434// const char *name;
1435// Symtab symtab;
1436// };
1437
1438// FIXME: Get from somewhere
1439static const int ModuleVersion = 7;
1440
1441void CGObjCMac::EmitModuleInfo() {
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001442 uint64_t Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ModuleTy);
1443
1444 std::vector<llvm::Constant*> Values(4);
1445 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
1446 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbarac93e472008-08-15 22:20:32 +00001447 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001448 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001449 Values[3] = EmitModuleSymbols();
1450
1451 llvm::GlobalVariable *GV =
1452 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
1453 llvm::GlobalValue::InternalLinkage,
1454 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
1455 Values),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001456 "\01L_OBJC_MODULES",
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001457 &CGM.getModule());
1458 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
1459 UsedGlobals.push_back(GV);
1460}
1461
1462llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001463 unsigned NumClasses = DefinedClasses.size();
1464 unsigned NumCategories = DefinedCategories.size();
1465
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001466 // Return null if no symbols were defined.
1467 if (!NumClasses && !NumCategories)
1468 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
1469
1470 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001471 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1472 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
1473 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
1474 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
1475
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001476 // The runtime expects exactly the list of defined classes followed
1477 // by the list of defined categories, in a single array.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001478 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001479 for (unsigned i=0; i<NumClasses; i++)
1480 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
1481 ObjCTypes.Int8PtrTy);
1482 for (unsigned i=0; i<NumCategories; i++)
1483 Symbols[NumClasses + i] =
1484 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
1485 ObjCTypes.Int8PtrTy);
1486
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001487 Values[4] =
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001488 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001489 NumClasses + NumCategories),
1490 Symbols);
1491
1492 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1493
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001494 llvm::GlobalVariable *GV =
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001495 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001496 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001497 Init,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001498 "\01L_OBJC_SYMBOLS",
1499 &CGM.getModule());
1500 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
1501 UsedGlobals.push_back(GV);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001502 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
1503}
1504
1505llvm::Value *CGObjCMac::EmitClassRef(llvm::IRBuilder<> &Builder,
1506 const ObjCInterfaceDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001507 LazySymbols.insert(ID->getIdentifier());
1508
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001509 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
1510
1511 if (!Entry) {
1512 llvm::Constant *Casted =
1513 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
1514 ObjCTypes.ClassPtrTy);
1515 Entry =
1516 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
1517 llvm::GlobalValue::InternalLinkage,
1518 Casted, "\01L_OBJC_CLASS_REFERENCES_",
1519 &CGM.getModule());
1520 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
1521 UsedGlobals.push_back(Entry);
1522 }
1523
1524 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001525}
1526
Daniel Dunbar5eec6142008-08-12 03:39:23 +00001527llvm::Value *CGObjCMac::EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
1528 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
1529
1530 if (!Entry) {
1531 llvm::Constant *Casted =
1532 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
1533 ObjCTypes.SelectorPtrTy);
1534 Entry =
1535 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
1536 llvm::GlobalValue::InternalLinkage,
1537 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
1538 &CGM.getModule());
1539 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
1540 UsedGlobals.push_back(Entry);
1541 }
1542
1543 return Builder.CreateLoad(Entry, false, "tmp");
1544}
1545
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001546llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
1547 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001548
1549 if (!Entry) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001550 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001551 Entry =
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001552 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001553 llvm::GlobalValue::InternalLinkage,
1554 C, "\01L_OBJC_CLASS_NAME_",
1555 &CGM.getModule());
1556 Entry->setSection("__TEXT,__cstring,cstring_literals");
1557 UsedGlobals.push_back(Entry);
1558 }
1559
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001560 return getConstantGEP(Entry, 0, 0);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001561}
1562
Daniel Dunbar5eec6142008-08-12 03:39:23 +00001563llvm::Constant *CGObjCMac::GetMethodVarName(Selector Sel) {
1564 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
1565
1566 if (!Entry) {
1567 llvm::Constant *C = llvm::ConstantArray::get(Sel.getName());
1568 Entry =
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001569 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar5eec6142008-08-12 03:39:23 +00001570 llvm::GlobalValue::InternalLinkage,
1571 C, "\01L_OBJC_METH_VAR_NAME_",
1572 &CGM.getModule());
1573 Entry->setSection("__TEXT,__cstring,cstring_literals");
1574 UsedGlobals.push_back(Entry);
1575 }
1576
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001577 return getConstantGEP(Entry, 0, 0);
1578}
1579
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001580// FIXME: Merge into a single cstring creation function.
1581llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) {
1582 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
1583}
1584
1585// FIXME: Merge into a single cstring creation function.
1586llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) {
1587 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
1588}
1589
1590llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) {
1591 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001592
1593 if (!Entry) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001594 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001595 Entry =
1596 new llvm::GlobalVariable(C->getType(), false,
1597 llvm::GlobalValue::InternalLinkage,
1598 C, "\01L_OBJC_METH_VAR_TYPE_",
1599 &CGM.getModule());
1600 Entry->setSection("__TEXT,__cstring,cstring_literals");
1601 UsedGlobals.push_back(Entry);
1602 }
1603
1604 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00001605}
1606
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001607// FIXME: Merge into a single cstring creation function.
Daniel Dunbar12996f52008-08-26 21:51:14 +00001608llvm::Constant *CGObjCMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001609 std::string TypeStr;
Daniel Dunbar12996f52008-08-26 21:51:14 +00001610 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
1611 TypeStr);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001612 return GetMethodVarType(TypeStr);
1613}
1614
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001615// FIXME: Merge into a single cstring creation function.
1616llvm::Constant *CGObjCMac::GetPropertyName(IdentifierInfo *Ident) {
1617 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
1618
1619 if (!Entry) {
1620 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
1621 Entry =
1622 new llvm::GlobalVariable(C->getType(), false,
1623 llvm::GlobalValue::InternalLinkage,
1624 C, "\01L_OBJC_PROP_NAME_ATTR_",
1625 &CGM.getModule());
1626 Entry->setSection("__TEXT,__cstring,cstring_literals");
1627 UsedGlobals.push_back(Entry);
1628 }
1629
1630 return getConstantGEP(Entry, 0, 0);
1631}
1632
1633// FIXME: Merge into a single cstring creation function.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001634// FIXME: This Decl should be more precise.
1635llvm::Constant *CGObjCMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
1636 const Decl *Container) {
1637 std::string TypeStr;
1638 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001639 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
1640}
1641
Daniel Dunbarace33292008-08-16 03:19:19 +00001642void CGObjCMac::GetNameForMethod(const ObjCMethodDecl *D,
1643 std::string &NameOut) {
1644 // FIXME: Find the mangling GCC uses.
1645 std::stringstream s;
1646 s << (D->isInstance() ? "-" : "+");
1647 s << "[";
1648 s << D->getClassInterface()->getName();
1649 s << " ";
1650 s << D->getSelector().getName();
1651 s << "]";
1652 NameOut = s.str();
1653}
1654
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001655void CGObjCMac::FinishModule() {
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001656 EmitModuleInfo();
1657
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001658 std::vector<llvm::Constant*> Used;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001659
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001660 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001661 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001662 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001663 }
1664
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001665 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001666 llvm::GlobalValue *GV =
1667 new llvm::GlobalVariable(AT, false,
1668 llvm::GlobalValue::AppendingLinkage,
1669 llvm::ConstantArray::get(AT, Used),
1670 "llvm.used",
1671 &CGM.getModule());
1672
1673 GV->setSection("llvm.metadata");
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001674
1675 // Add assembler directives to add lazy undefined symbol references
1676 // for classes which are referenced but not defined. This is
1677 // important for correct linker interaction.
1678
1679 // FIXME: Uh, this isn't particularly portable.
1680 std::stringstream s;
1681 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
1682 e = LazySymbols.end(); i != e; ++i) {
1683 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
1684 }
1685 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
1686 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001687 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001688 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
1689 }
1690 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001691}
1692
1693/* *** */
1694
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001695ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Daniel Dunbardbdb9512008-08-23 18:37:06 +00001696 : CGM(cgm)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001697{
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001698 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1699 ASTContext &Ctx = CGM.getContext();
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001700
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001701 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001702 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001703 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001704 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1705
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001706 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
1707 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001708
1709 // FIXME: It would be nice to unify this with the opaque type, so
1710 // that the IR comes out a bit cleaner.
1711 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
1712 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001713
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001714 MethodDescriptionTy =
1715 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001716 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001717 NULL);
1718 CGM.getModule().addTypeName("struct._objc_method_description",
1719 MethodDescriptionTy);
1720
1721 MethodDescriptionListTy =
1722 llvm::StructType::get(IntTy,
1723 llvm::ArrayType::get(MethodDescriptionTy, 0),
1724 NULL);
1725 CGM.getModule().addTypeName("struct._objc_method_description_list",
1726 MethodDescriptionListTy);
1727 MethodDescriptionListPtrTy =
1728 llvm::PointerType::getUnqual(MethodDescriptionListTy);
1729
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001730 PropertyTy = llvm::StructType::get(Int8PtrTy,
1731 Int8PtrTy,
1732 NULL);
1733 CGM.getModule().addTypeName("struct._objc_property",
1734 PropertyTy);
1735
1736 PropertyListTy = llvm::StructType::get(IntTy,
1737 IntTy,
1738 llvm::ArrayType::get(PropertyTy, 0),
1739 NULL);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001740 CGM.getModule().addTypeName("struct._objc_property_list",
1741 PropertyListTy);
1742 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
1743
1744 // Protocol description structures
1745
1746 ProtocolExtensionTy =
1747 llvm::StructType::get(Types.ConvertType(Ctx.IntTy),
1748 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1749 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1750 PropertyListPtrTy,
1751 NULL);
1752 CGM.getModule().addTypeName("struct._objc_protocol_extension",
1753 ProtocolExtensionTy);
1754 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
1755
1756 // Handle recursive construction of Protocl and ProtocolList types
1757
1758 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
1759 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
1760
1761 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
1762 LongTy,
1763 llvm::ArrayType::get(ProtocolTyHolder, 0),
1764 NULL);
1765 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
1766
1767 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolExtensionTy),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001768 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001769 llvm::PointerType::getUnqual(ProtocolListTyHolder),
1770 MethodDescriptionListPtrTy,
1771 MethodDescriptionListPtrTy,
1772 NULL);
1773 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
1774
1775 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
1776 CGM.getModule().addTypeName("struct._objc_protocol_list",
1777 ProtocolListTy);
1778 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
1779
1780 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
1781 CGM.getModule().addTypeName("struct.__objc_protocol", ProtocolTy);
1782 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001783
1784 // Class description structures
1785
1786 IvarTy = llvm::StructType::get(Int8PtrTy,
1787 Int8PtrTy,
1788 IntTy,
1789 NULL);
1790 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
1791
1792 IvarListTy = llvm::OpaqueType::get();
1793 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
1794 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
1795
1796 MethodTy = llvm::StructType::get(SelectorPtrTy,
1797 Int8PtrTy,
1798 Int8PtrTy,
1799 NULL);
1800 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
1801
1802 MethodListTy = llvm::OpaqueType::get();
1803 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
1804 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
1805
1806 CacheTy = llvm::OpaqueType::get();
1807 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
1808 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
1809
1810 ClassExtensionTy =
1811 llvm::StructType::get(IntTy,
1812 Int8PtrTy,
1813 PropertyListPtrTy,
1814 NULL);
1815 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
1816 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
1817
1818 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
1819
1820 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
1821 llvm::PointerType::getUnqual(ClassTyHolder),
1822 Int8PtrTy,
1823 LongTy,
1824 LongTy,
1825 LongTy,
1826 IvarListPtrTy,
1827 MethodListPtrTy,
1828 CachePtrTy,
1829 ProtocolListPtrTy,
1830 Int8PtrTy,
1831 ClassExtensionPtrTy,
1832 NULL);
1833 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
1834
1835 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
1836 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
1837 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
1838
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001839 CategoryTy = llvm::StructType::get(Int8PtrTy,
1840 Int8PtrTy,
1841 MethodListPtrTy,
1842 MethodListPtrTy,
1843 ProtocolListPtrTy,
1844 IntTy,
1845 PropertyListPtrTy,
1846 NULL);
1847 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
1848
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001849 // I'm not sure I like this. The implicit coordination is a bit
1850 // gross. We should solve this in a reasonable fashion because this
1851 // is a pretty common task (match some runtime data structure with
1852 // an LLVM data structure).
1853
1854 // FIXME: This is leaked.
1855 // FIXME: Merge with rewriter code?
1856 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
1857 SourceLocation(),
1858 &Ctx.Idents.get("_objc_super"), 0);
1859 FieldDecl *FieldDecls[2];
1860 FieldDecls[0] = FieldDecl::Create(Ctx, SourceLocation(), 0,
1861 Ctx.getObjCIdType());
1862 FieldDecls[1] = FieldDecl::Create(Ctx, SourceLocation(), 0,
1863 Ctx.getObjCClassType());
1864 RD->defineBody(FieldDecls, 2);
1865
1866 SuperCTy = Ctx.getTagDeclType(RD);
1867 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
1868
1869 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001870 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
Daniel Dunbar15245e52008-08-23 04:28:29 +00001871
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001872 // Global metadata structures
1873
1874 SymtabTy = llvm::StructType::get(LongTy,
1875 SelectorPtrTy,
1876 ShortTy,
1877 ShortTy,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001878 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001879 NULL);
1880 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
1881 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
1882
1883 ModuleTy =
1884 llvm::StructType::get(LongTy,
1885 LongTy,
1886 Int8PtrTy,
1887 SymtabPtrTy,
1888 NULL);
1889 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001890
1891 // Message send functions
1892
1893 std::vector<const llvm::Type*> Params;
1894 Params.push_back(ObjectPtrTy);
1895 Params.push_back(SelectorPtrTy);
1896 MessageSendFn = llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1897 Params,
1898 true),
1899 llvm::Function::ExternalLinkage,
1900 "objc_msgSend",
1901 &CGM.getModule());
1902
1903 Params.clear();
1904 Params.push_back(Int8PtrTy);
1905 Params.push_back(ObjectPtrTy);
1906 Params.push_back(SelectorPtrTy);
1907 MessageSendStretFn =
1908 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1909 Params,
1910 true),
1911 llvm::Function::ExternalLinkage,
1912 "objc_msgSend_stret",
1913 &CGM.getModule());
1914
1915 Params.clear();
1916 Params.push_back(SuperPtrTy);
1917 Params.push_back(SelectorPtrTy);
1918 MessageSendSuperFn =
1919 llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1920 Params,
1921 true),
1922 llvm::Function::ExternalLinkage,
1923 "objc_msgSendSuper",
1924 &CGM.getModule());
1925
1926 Params.clear();
1927 Params.push_back(Int8PtrTy);
1928 Params.push_back(SuperPtrTy);
1929 Params.push_back(SelectorPtrTy);
1930 MessageSendSuperStretFn =
1931 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1932 Params,
1933 true),
1934 llvm::Function::ExternalLinkage,
1935 "objc_msgSendSuper_stret",
1936 &CGM.getModule());
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001937}
1938
1939ObjCTypesHelper::~ObjCTypesHelper() {
1940}
1941
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001942llvm::Value *ObjCTypesHelper::getMessageSendFn(bool IsSuper,
1943 const llvm::Type *ReturnTy) {
1944 llvm::Function *F;
1945 llvm::FunctionType *CallFTy;
1946
1947 // FIXME: Should we be caching any of this?
1948 if (!ReturnTy->isSingleValueType()) {
1949 F = IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
1950 std::vector<const llvm::Type*> Params(3);
1951 Params[0] = llvm::PointerType::getUnqual(ReturnTy);
1952 Params[1] = IsSuper ? SuperPtrTy : ObjectPtrTy;
1953 Params[2] = SelectorPtrTy;
1954 CallFTy = llvm::FunctionType::get(llvm::Type::VoidTy, Params, true);
Daniel Dunbar952f4732008-08-29 17:28:43 +00001955 } else { // FIXME: floating point?
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001956 F = IsSuper ? MessageSendSuperFn : MessageSendFn;
1957 std::vector<const llvm::Type*> Params(2);
1958 Params[0] = IsSuper ? SuperPtrTy : ObjectPtrTy;
1959 Params[1] = SelectorPtrTy;
1960 CallFTy = llvm::FunctionType::get(ReturnTy, Params, true);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00001961 }
1962
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001963 return llvm::ConstantExpr::getBitCast(F,
1964 llvm::PointerType::getUnqual(CallFTy));
Daniel Dunbar15245e52008-08-23 04:28:29 +00001965}
1966
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001967/* *** */
1968
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001969CodeGen::CGObjCRuntime *
1970CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001971 return new CGObjCMac(CGM);
1972}