blob: d1a165127712c28bbcfdd363df95c3420302e796 [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
Anders Carlsson58d16242008-08-31 04:05:03 +0000133 llvm::Function *EnumerationMutationFn;
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000134public:
135 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
136 ~ObjCTypesHelper();
137
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000138 llvm::Value *getMessageSendFn(bool IsSuper, const llvm::Type *ReturnTy);
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000139};
140
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000141class CGObjCMac : public CodeGen::CGObjCRuntime {
142private:
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000143 CodeGen::CodeGenModule &CGM;
144 ObjCTypesHelper ObjCTypes;
145 /// ObjCABI - FIXME: Not sure yet.
146 unsigned ObjCABI;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000147
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000148 /// LazySymbols - Symbols to generate a lazy reference for. See
149 /// DefinedSymbols and FinishModule().
150 std::set<IdentifierInfo*> LazySymbols;
151
152 /// DefinedSymbols - External symbols which are defined by this
153 /// module. The symbols in this list and LazySymbols are used to add
154 /// special linker symbols which ensure that Objective-C modules are
155 /// linked properly.
156 std::set<IdentifierInfo*> DefinedSymbols;
157
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000158 /// ClassNames - uniqued class names.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000159 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000160
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000161 /// MethodVarNames - uniqued method variable names.
162 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
163
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000164 /// MethodVarTypes - uniqued method type signatures. We have to use
165 /// a StringMap here because have no other unique reference.
166 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
167
Daniel Dunbar12996f52008-08-26 21:51:14 +0000168 /// MethodDefinitions - map of methods which have been defined in
169 /// this translation unit.
170 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
171
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000172 /// PropertyNames - uniqued method variable names.
173 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
174
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000175 /// ClassReferences - uniqued class references.
176 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
177
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000178 /// SelectorReferences - uniqued selector references.
179 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
180
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000181 /// Protocols - Protocols for which an objc_protocol structure has
182 /// been emitted. Forward declarations are handled by creating an
183 /// empty structure whose initializer is filled in when/if defined.
184 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
185
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000186 /// DefinedClasses - List of defined classes.
187 std::vector<llvm::GlobalValue*> DefinedClasses;
188
189 /// DefinedCategories - List of defined categories.
190 std::vector<llvm::GlobalValue*> DefinedCategories;
191
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000192 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000193 /// to prevent them from being clobbered.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000194 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000195
196 /// EmitImageInfo - Emit the image info marker used to encode some module
197 /// level information.
198 void EmitImageInfo();
199
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000200 /// EmitModuleInfo - Another marker encoding module level
201 /// information.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000202 void EmitModuleInfo();
203
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000204 /// EmitModuleSymols - Emit module symbols, the list of defined
205 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000206 llvm::Constant *EmitModuleSymbols();
207
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000208 /// FinishModule - Write out global data structures at the end of
209 /// processing a translation unit.
210 void FinishModule();
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000211
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000212 /// EmitClassExtension - Generate the class extension structure used
213 /// to store the weak ivar layout and properties. The return value
214 /// has type ClassExtensionPtrTy.
215 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
216
217 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
218 /// for the given class.
219 llvm::Value *EmitClassRef(llvm::IRBuilder<> &Builder,
220 const ObjCInterfaceDecl *ID);
221
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000222 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000223 QualType ResultType,
224 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000225 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000226 QualType Arg0Ty,
227 bool IsSuper,
228 const CallArgList &CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000229
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000230 /// EmitIvarList - Emit the ivar list for the given
231 /// implementation. If ForClass is true the list of class ivars
232 /// (i.e. metaclass ivars) is emitted, otherwise the list of
233 /// interface ivars will be emitted. The return value has type
234 /// IvarListPtrTy.
235 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
236 bool ForClass,
237 const llvm::Type *InterfaceTy);
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000238
239 /// EmitMetaClass - Emit a forward reference to the class structure
240 /// for the metaclass of the given interface. The return value has
241 /// type ClassPtrTy.
242 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
243
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000244 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000245 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000246 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
247 llvm::Constant *Protocols,
Daniel Dunbar12996f52008-08-26 21:51:14 +0000248 const llvm::Type *InterfaceTy,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000249 const ConstantVector &Methods);
250
251 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
252
253 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000254
255 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000256 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000257 llvm::Constant *EmitMethodList(const std::string &Name,
258 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000259 const ConstantVector &Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000260
261 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000262 /// method declarations.
263 /// - TypeName: The name for the type containing the methods.
264 /// - IsProtocol: True iff these methods are for a protocol.
265 /// - ClassMethds: True iff these are class methods.
266 /// - Required: When true, only "required" methods are
267 /// listed. Similarly, when false only "optional" methods are
268 /// listed. For classes this should always be true.
269 /// - begin, end: The method list to output.
270 ///
271 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000272 llvm::Constant *EmitMethodDescList(const std::string &Name,
273 const char *Section,
274 const ConstantVector &Methods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000275
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000276 /// EmitPropertyList - Emit the given property list. The return
277 /// value has type PropertyListPtrTy.
278 llvm::Constant *EmitPropertyList(const std::string &Name,
Daniel Dunbar698d6f32008-08-28 04:38:10 +0000279 const Decl *Container,
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000280 ObjCPropertyDecl * const *begin,
281 ObjCPropertyDecl * const *end);
282
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000283 /// EmitProtocolExtension - Generate the protocol extension
284 /// structure used to store optional instance and class methods, and
285 /// protocol properties. The return value has type
286 /// ProtocolExtensionPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000287 llvm::Constant *
288 EmitProtocolExtension(const ObjCProtocolDecl *PD,
289 const ConstantVector &OptInstanceMethods,
290 const ConstantVector &OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000291
292 /// EmitProtocolList - Generate the list of referenced
293 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000294 llvm::Constant *EmitProtocolList(const std::string &Name,
295 ObjCProtocolDecl::protocol_iterator begin,
296 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000297
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000298 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
299 /// for the given selector.
300 llvm::Value *EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel);
301
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000302 /// GetProtocolRef - Return a reference to the internal protocol
303 /// description, creating an empty one if it has not been
304 /// defined. The return value has type pointer-to ProtocolTy.
305 llvm::GlobalVariable *GetProtocolRef(const ObjCProtocolDecl *PD);
306
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000307 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000308 /// name. The return value has type char *.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000309 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000310
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000311 /// GetMethodVarName - Return a unique constant for the given
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000312 /// selector's name. The return value has type char *.
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000313 llvm::Constant *GetMethodVarName(Selector Sel);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000314 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000315 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000316
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000317 /// GetMethodVarType - Return a unique constant for the given
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000318 /// selector's name. The return value has type char *.
319
320 // FIXME: This is a horrible name.
Daniel Dunbar12996f52008-08-26 21:51:14 +0000321 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000322 llvm::Constant *GetMethodVarType(const std::string &Name);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000323
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000324 /// GetPropertyName - Return a unique constant for the given
325 /// name. The return value has type char *.
326 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
327
Daniel Dunbar698d6f32008-08-28 04:38:10 +0000328 // FIXME: This can be dropped once string functions are unified.
329 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
330 const Decl *Container);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000331
Daniel Dunbarace33292008-08-16 03:19:19 +0000332 /// GetNameForMethod - Return a name for the given method.
333 /// \param[out] NameOut - The return value.
334 void GetNameForMethod(const ObjCMethodDecl *OMD,
335 std::string &NameOut);
336
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000337public:
338 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000339 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000340
Daniel Dunbara04840b2008-08-23 03:46:30 +0000341 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000342 QualType ResultType,
343 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000344 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000345 bool IsClassMessage,
346 const CallArgList &CallArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000347
Daniel Dunbara04840b2008-08-23 03:46:30 +0000348 virtual CodeGen::RValue
349 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000350 QualType ResultType,
351 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000352 const ObjCInterfaceDecl *Class,
353 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000354 bool IsClassMessage,
355 const CallArgList &CallArgs);
Daniel Dunbar434627a2008-08-16 00:25:02 +0000356
357 virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000358 const ObjCInterfaceDecl *ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000359
360 virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
361
Daniel Dunbarac93e472008-08-15 22:20:32 +0000362 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000363
Daniel Dunbarac93e472008-08-15 22:20:32 +0000364 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000365
Daniel Dunbarac93e472008-08-15 22:20:32 +0000366 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000367
368 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000369 const ObjCProtocolDecl *PD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000370
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000371 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000372
373 virtual llvm::Function *ModuleInitFunction();
Anders Carlsson58d16242008-08-31 04:05:03 +0000374 virtual llvm::Function *EnumerationMutationFunction();
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000375};
376} // end anonymous namespace
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000377
378/* *** Helper Functions *** */
379
380/// getConstantGEP() - Help routine to construct simple GEPs.
381static llvm::Constant *getConstantGEP(llvm::Constant *C,
382 unsigned idx0,
383 unsigned idx1) {
384 llvm::Value *Idxs[] = {
385 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
386 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
387 };
388 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
389}
390
391/* *** CGObjCMac Public Interface *** */
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000392
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000393CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm)
394 : CGM(cgm),
395 ObjCTypes(cgm),
396 ObjCABI(1)
397{
398 // FIXME: How does this get set in GCC? And what does it even mean?
399 if (ObjCTypes.LongTy != CGM.getTypes().ConvertType(CGM.getContext().IntTy))
400 ObjCABI = 2;
401
402 EmitImageInfo();
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000403}
404
Daniel Dunbar434627a2008-08-16 00:25:02 +0000405/// GetClass - Return a reference to the class for the given interface
406/// decl.
407llvm::Value *CGObjCMac::GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000408 const ObjCInterfaceDecl *ID) {
409 return EmitClassRef(Builder, ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000410}
411
412/// GetSelector - Return the pointer to the unique'd string for this selector.
413llvm::Value *CGObjCMac::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000414 return EmitSelector(Builder, Sel);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000415}
416
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000417/// Generate a constant CFString object.
418/*
419 struct __builtin_CFString {
420 const int *isa; // point to __CFConstantStringClassReference
421 int flags;
422 const char *str;
423 long length;
424 };
425*/
426
427llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000428 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000429}
430
431/// Generates a message send where the super is the receiver. This is
432/// a message send to self with special delivery semantics indicating
433/// which class's method should be called.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000434CodeGen::RValue
435CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000436 QualType ResultType,
437 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000438 const ObjCInterfaceDecl *Class,
439 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000440 bool IsClassMessage,
441 const CallArgList &CallArgs) {
Daniel Dunbar15245e52008-08-23 04:28:29 +0000442 // Create and init a super structure; this is a (receiver, class)
443 // pair we will pass to objc_msgSendSuper.
444 llvm::Value *ObjCSuper =
445 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
446 llvm::Value *ReceiverAsObject =
447 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
448 CGF.Builder.CreateStore(ReceiverAsObject,
449 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar15245e52008-08-23 04:28:29 +0000450
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000451 // If this is a class message the metaclass is passed as the target.
452 llvm::Value *Target;
453 if (IsClassMessage) {
454 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
455 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
456 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
457 Target = Super;
458 } else {
459 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
460 }
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000461 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
462 // and ObjCTypes types.
463 const llvm::Type *ClassTy =
464 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
465 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000466 CGF.Builder.CreateStore(Target,
467 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
468
Daniel Dunbardd851282008-08-30 05:35:15 +0000469 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000470 ObjCSuper, ObjCTypes.SuperPtrCTy,
471 true, CallArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000472}
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000473
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000474/// Generate code for a message send expression.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000475CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000476 QualType ResultType,
477 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000478 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000479 bool IsClassMessage,
480 const CallArgList &CallArgs) {
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000481 llvm::Value *Arg0 =
482 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbardd851282008-08-30 05:35:15 +0000483 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000484 Arg0, CGF.getContext().getObjCIdType(),
485 false, CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000486}
487
488CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000489 QualType ResultType,
490 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000491 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000492 QualType Arg0Ty,
493 bool IsSuper,
494 const CallArgList &CallArgs) {
495 CallArgList ActualArgs;
496 ActualArgs.push_back(std::make_pair(Arg0, Arg0Ty));
Daniel Dunbardd851282008-08-30 05:35:15 +0000497 ActualArgs.push_back(std::make_pair(EmitSelector(CGF.Builder, Sel),
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000498 CGF.getContext().getObjCSelType()));
499 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000500
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000501 // FIXME: This is a hack, we are implicitly coordinating with
Daniel Dunbarcc37ac52008-09-03 00:27:26 +0000502 // EmitCall, which will move the return type to the first parameter
503 // and set the structure return flag. See getMessageSendFn().
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000504
Daniel Dunbardd851282008-08-30 05:35:15 +0000505 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000506 return CGF.EmitCall(ObjCTypes.getMessageSendFn(IsSuper, ReturnTy),
Daniel Dunbardd851282008-08-30 05:35:15 +0000507 ResultType, ActualArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000508}
509
510llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000511 const ObjCProtocolDecl *PD) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000512 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
513 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000514}
515
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000516/*
517 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
518 struct _objc_protocol {
519 struct _objc_protocol_extension *isa;
520 char *protocol_name;
521 struct _objc_protocol_list *protocol_list;
522 struct _objc__method_prototype_list *instance_methods;
523 struct _objc__method_prototype_list *class_methods
524 };
525
526 See EmitProtocolExtension().
527*/
528void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000529 // FIXME: I don't understand why gcc generates this, or where it is
530 // resolved. Investigate. Its also wasteful to look this up over and
531 // over.
532 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
533
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000534 const char *ProtocolName = PD->getName();
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000535
536 // Construct method lists.
537 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
538 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
539 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
540 e = PD->instmeth_end(); i != e; ++i) {
541 ObjCMethodDecl *MD = *i;
542 llvm::Constant *C = GetMethodDescriptionConstant(MD);
543 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
544 OptInstanceMethods.push_back(C);
545 } else {
546 InstanceMethods.push_back(C);
547 }
548 }
549
550 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
551 e = PD->classmeth_end(); i != e; ++i) {
552 ObjCMethodDecl *MD = *i;
553 llvm::Constant *C = GetMethodDescriptionConstant(MD);
554 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
555 OptClassMethods.push_back(C);
556 } else {
557 ClassMethods.push_back(C);
558 }
559 }
560
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000561 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000562 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000563 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000564 Values[2] =
565 EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(),
566 PD->protocol_begin(),
567 PD->protocol_end());
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000568 Values[3] =
569 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_")
570 + PD->getName(),
571 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
572 InstanceMethods);
573 Values[4] =
574 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_")
575 + PD->getName(),
576 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
577 ClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000578 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
579 Values);
580
581 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
582 if (Entry) {
583 // Already created, just update the initializer
584 Entry->setInitializer(Init);
585 } else {
586 Entry =
587 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
588 llvm::GlobalValue::InternalLinkage,
589 Init,
590 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
591 &CGM.getModule());
592 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
593 UsedGlobals.push_back(Entry);
594 // FIXME: Is this necessary? Why only for protocol?
595 Entry->setAlignment(4);
596 }
597}
598
599llvm::GlobalVariable *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
600 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
601
602 if (!Entry) {
603 std::vector<llvm::Constant*> Values(5);
604 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
605 Values[1] = GetClassName(PD->getIdentifier());
606 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
607 Values[3] = Values[4] =
608 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
609 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
610 Values);
611
612 Entry =
613 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
614 llvm::GlobalValue::InternalLinkage,
615 Init,
616 std::string("\01L_OBJC_PROTOCOL_")+PD->getName(),
617 &CGM.getModule());
618 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
619 UsedGlobals.push_back(Entry);
620 // FIXME: Is this necessary? Why only for protocol?
621 Entry->setAlignment(4);
622 }
623
624 return Entry;
625}
626
627/*
628 struct _objc_protocol_extension {
629 uint32_t size;
630 struct objc_method_description_list *optional_instance_methods;
631 struct objc_method_description_list *optional_class_methods;
632 struct objc_property_list *instance_properties;
633 };
634*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000635llvm::Constant *
636CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
637 const ConstantVector &OptInstanceMethods,
638 const ConstantVector &OptClassMethods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000639 uint64_t Size =
640 CGM.getTargetData().getABITypeSize(ObjCTypes.ProtocolExtensionTy);
641 std::vector<llvm::Constant*> Values(4);
642 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000643 Values[1] =
644 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_")
645 + PD->getName(),
646 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
647 OptInstanceMethods);
648 Values[2] =
649 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_")
650 + PD->getName(),
651 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
652 OptClassMethods);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000653 Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") +
654 PD->getName(),
Daniel Dunbar698d6f32008-08-28 04:38:10 +0000655 0,
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000656 PD->classprop_begin(),
657 PD->classprop_end());
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000658
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000659 // Return null if no extension bits are used.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000660 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
661 Values[3]->isNullValue())
662 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
663
664 llvm::Constant *Init =
665 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
666 llvm::GlobalVariable *GV =
667 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
668 llvm::GlobalValue::InternalLinkage,
669 Init,
670 (std::string("\01L_OBJC_PROTOCOLEXT_") +
671 PD->getName()),
672 &CGM.getModule());
673 // No special section, but goes in llvm.used
674 UsedGlobals.push_back(GV);
675
676 return GV;
677}
678
679/*
680 struct objc_protocol_list {
681 struct objc_protocol_list *next;
682 long count;
683 Protocol *list[];
684 };
685*/
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000686llvm::Constant *
687CGObjCMac::EmitProtocolList(const std::string &Name,
688 ObjCProtocolDecl::protocol_iterator begin,
689 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000690 std::vector<llvm::Constant*> ProtocolRefs;
691
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000692 for (; begin != end; ++begin)
693 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000694
695 // Just return null for empty protocol lists
696 if (ProtocolRefs.empty())
697 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
698
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000699 // This list is null terminated.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000700 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
701
702 std::vector<llvm::Constant*> Values(3);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000703 // This field is only used by the runtime.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000704 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
705 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
706 Values[2] =
707 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
708 ProtocolRefs.size()),
709 ProtocolRefs);
710
711 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
712 llvm::GlobalVariable *GV =
713 new llvm::GlobalVariable(Init->getType(), false,
714 llvm::GlobalValue::InternalLinkage,
715 Init,
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000716 Name,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000717 &CGM.getModule());
718 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
719 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
720}
721
722/*
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000723 struct _objc_property {
724 const char * const name;
725 const char * const attributes;
726 };
727
728 struct _objc_property_list {
729 uint32_t entsize; // sizeof (struct _objc_property)
730 uint32_t prop_count;
731 struct _objc_property[prop_count];
732 };
733*/
734llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbar698d6f32008-08-28 04:38:10 +0000735 const Decl *Container,
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000736 ObjCPropertyDecl * const *begin,
737 ObjCPropertyDecl * const *end) {
738 std::vector<llvm::Constant*> Properties, Prop(2);
739 for (; begin != end; ++begin) {
740 const ObjCPropertyDecl *PD = *begin;
741 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar698d6f32008-08-28 04:38:10 +0000742 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000743 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
744 Prop));
745 }
746
747 // Return null for empty list.
748 if (Properties.empty())
749 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
750
751 unsigned PropertySize =
752 CGM.getTargetData().getABITypeSize(ObjCTypes.PropertyTy);
753 std::vector<llvm::Constant*> Values(3);
754 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
755 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
756 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
757 Properties.size());
758 Values[2] = llvm::ConstantArray::get(AT, Properties);
759 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
760
761 llvm::GlobalVariable *GV =
762 new llvm::GlobalVariable(Init->getType(), false,
763 llvm::GlobalValue::InternalLinkage,
764 Init,
765 Name,
766 &CGM.getModule());
767 // No special section on property lists?
768 UsedGlobals.push_back(GV);
769 return llvm::ConstantExpr::getBitCast(GV,
770 ObjCTypes.PropertyListPtrTy);
771
772}
773
774/*
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000775 struct objc_method_description_list {
776 int count;
777 struct objc_method_description list[];
778 };
779*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000780llvm::Constant *
781CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
782 std::vector<llvm::Constant*> Desc(2);
783 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
784 ObjCTypes.SelectorPtrTy);
785 Desc[1] = GetMethodVarType(MD);
786 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
787 Desc);
788}
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000789
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000790llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
791 const char *Section,
792 const ConstantVector &Methods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000793 // Return null for empty list.
794 if (Methods.empty())
795 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
796
797 std::vector<llvm::Constant*> Values(2);
798 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
799 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
800 Methods.size());
801 Values[1] = llvm::ConstantArray::get(AT, Methods);
802 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
803
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000804 llvm::GlobalVariable *GV =
805 new llvm::GlobalVariable(Init->getType(), false,
806 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000807 Init, Name, &CGM.getModule());
808 GV->setSection(Section);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000809 UsedGlobals.push_back(GV);
810 return llvm::ConstantExpr::getBitCast(GV,
811 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000812}
813
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000814/*
815 struct _objc_category {
816 char *category_name;
817 char *class_name;
818 struct _objc_method_list *instance_methods;
819 struct _objc_method_list *class_methods;
820 struct _objc_protocol_list *protocols;
821 uint32_t size; // <rdar://4585769>
822 struct _objc_property_list *instance_properties;
823 };
824 */
Daniel Dunbarac93e472008-08-15 22:20:32 +0000825void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000826 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.CategoryTy);
827
Daniel Dunbar0cd49032008-08-26 23:03:11 +0000828 // FIXME: This is poor design, the OCD should have a pointer to the
829 // category decl. Additionally, note that Category can be null for
830 // the @implementation w/o an @interface case. Sema should just
831 // create one for us as it does for @implementation so everyone else
832 // can live life under a clear blue sky.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000833 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar0cd49032008-08-26 23:03:11 +0000834 const ObjCCategoryDecl *Category =
835 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000836 std::string ExtName(std::string(Interface->getName()) +
837 "_" +
838 OCD->getName());
839
Daniel Dunbar12996f52008-08-26 21:51:14 +0000840 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
841 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
842 e = OCD->instmeth_end(); i != e; ++i) {
843 // Instance methods should always be defined.
844 InstanceMethods.push_back(GetMethodConstant(*i));
845 }
846 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
847 e = OCD->classmeth_end(); i != e; ++i) {
848 // Class methods should always be defined.
849 ClassMethods.push_back(GetMethodConstant(*i));
850 }
851
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000852 std::vector<llvm::Constant*> Values(7);
853 Values[0] = GetClassName(OCD->getIdentifier());
854 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000855 Values[2] =
856 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
857 ExtName,
858 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +0000859 InstanceMethods);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000860 Values[3] =
861 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
862 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +0000863 ClassMethods);
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000864 if (Category) {
865 Values[4] =
866 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
867 Category->protocol_begin(),
868 Category->protocol_end());
869 } else {
870 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
871 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000872 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar0cd49032008-08-26 23:03:11 +0000873
874 // If there is no category @interface then there can be no properties.
875 if (Category) {
876 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Daniel Dunbar698d6f32008-08-28 04:38:10 +0000877 OCD,
Daniel Dunbar0cd49032008-08-26 23:03:11 +0000878 Category->classprop_begin(),
879 Category->classprop_end());
880 } else {
881 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
882 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000883
884 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
885 Values);
886
887 llvm::GlobalVariable *GV =
888 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
889 llvm::GlobalValue::InternalLinkage,
890 Init,
891 std::string("\01L_OBJC_CATEGORY_")+ExtName,
892 &CGM.getModule());
893 GV->setSection("__OBJC,__category,regular,no_dead_strip");
894 UsedGlobals.push_back(GV);
895 DefinedCategories.push_back(GV);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000896}
897
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000898// FIXME: Get from somewhere?
899enum ClassFlags {
900 eClassFlags_Factory = 0x00001,
901 eClassFlags_Meta = 0x00002,
902 // <rdr://5142207>
903 eClassFlags_HasCXXStructors = 0x02000,
904 eClassFlags_Hidden = 0x20000,
905 eClassFlags_ABI2_Hidden = 0x00010,
906 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
907};
908
909// <rdr://5142207&4705298&4843145>
910static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
911 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
912 // FIXME: Support -fvisibility
913 switch (attr->getVisibility()) {
914 default:
915 assert(0 && "Unknown visibility");
916 return false;
917 case VisibilityAttr::DefaultVisibility:
918 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
919 return false;
920 case VisibilityAttr::HiddenVisibility:
921 return true;
922 }
923 } else {
924 return false; // FIXME: Support -fvisibility
925 }
926}
927
928/*
929 struct _objc_class {
930 Class isa;
931 Class super_class;
932 const char *name;
933 long version;
934 long info;
935 long instance_size;
936 struct _objc_ivar_list *ivars;
937 struct _objc_method_list *methods;
938 struct _objc_cache *cache;
939 struct _objc_protocol_list *protocols;
940 // Objective-C 1.0 extensions (<rdr://4585769>)
941 const char *ivar_layout;
942 struct _objc_class_ext *ext;
943 };
944
945 See EmitClassExtension();
946 */
947void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000948 DefinedSymbols.insert(ID->getIdentifier());
949
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000950 const char *ClassName = ID->getName();
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000951 // FIXME: Gross
952 ObjCInterfaceDecl *Interface =
953 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000954 llvm::Constant *Protocols =
955 EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(),
956 Interface->protocol_begin(),
957 Interface->protocol_end());
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000958 const llvm::Type *InterfaceTy =
959 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
960 unsigned Flags = eClassFlags_Factory;
961 unsigned Size = CGM.getTargetData().getABITypeSize(InterfaceTy);
962
963 // FIXME: Set CXX-structors flag.
964 if (IsClassHidden(ID->getClassInterface()))
965 Flags |= eClassFlags_Hidden;
966
Daniel Dunbar12996f52008-08-26 21:51:14 +0000967 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
968 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
969 e = ID->instmeth_end(); i != e; ++i) {
970 // Instance methods should always be defined.
971 InstanceMethods.push_back(GetMethodConstant(*i));
972 }
973 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
974 e = ID->classmeth_end(); i != e; ++i) {
975 // Class methods should always be defined.
976 ClassMethods.push_back(GetMethodConstant(*i));
977 }
978
979 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
980 e = ID->propimpl_end(); i != e; ++i) {
981 ObjCPropertyImplDecl *PID = *i;
982
983 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
984 ObjCPropertyDecl *PD = PID->getPropertyDecl();
985
986 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
987 if (llvm::Constant *C = GetMethodConstant(MD))
988 InstanceMethods.push_back(C);
989 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
990 if (llvm::Constant *C = GetMethodConstant(MD))
991 InstanceMethods.push_back(C);
992 }
993 }
994
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000995 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar12996f52008-08-26 21:51:14 +0000996 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000997 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000998 // Record a reference to the super class.
999 LazySymbols.insert(Super->getIdentifier());
1000
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001001 Values[ 1] =
1002 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1003 ObjCTypes.ClassPtrTy);
1004 } else {
1005 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1006 }
1007 Values[ 2] = GetClassName(ID->getIdentifier());
1008 // Version is always 0.
1009 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1010 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1011 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1012 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001013 Values[ 7] =
1014 EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(),
1015 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001016 InstanceMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001017 // cache is always NULL.
1018 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1019 Values[ 9] = Protocols;
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001020 // FIXME: Set ivar_layout
1021 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001022 Values[11] = EmitClassExtension(ID);
1023 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1024 Values);
1025
1026 llvm::GlobalVariable *GV =
1027 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1028 llvm::GlobalValue::InternalLinkage,
1029 Init,
1030 std::string("\01L_OBJC_CLASS_")+ClassName,
1031 &CGM.getModule());
1032 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1033 UsedGlobals.push_back(GV);
1034 // FIXME: Why?
1035 GV->setAlignment(32);
1036 DefinedClasses.push_back(GV);
1037}
1038
1039llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1040 llvm::Constant *Protocols,
Daniel Dunbar12996f52008-08-26 21:51:14 +00001041 const llvm::Type *InterfaceTy,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001042 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001043 const char *ClassName = ID->getName();
1044 unsigned Flags = eClassFlags_Meta;
1045 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ClassTy);
1046
1047 if (IsClassHidden(ID->getClassInterface()))
1048 Flags |= eClassFlags_Hidden;
1049
1050 std::vector<llvm::Constant*> Values(12);
1051 // The isa for the metaclass is the root of the hierarchy.
1052 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1053 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1054 Root = Super;
1055 Values[ 0] =
1056 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1057 ObjCTypes.ClassPtrTy);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001058 // The super class for the metaclass is emitted as the name of the
1059 // super class. The runtime fixes this up to point to the
1060 // *metaclass* for the super class.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001061 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1062 Values[ 1] =
1063 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1064 ObjCTypes.ClassPtrTy);
1065 } else {
1066 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1067 }
1068 Values[ 2] = GetClassName(ID->getIdentifier());
1069 // Version is always 0.
1070 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1071 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1072 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1073 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001074 Values[ 7] =
1075 EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(),
1076 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001077 Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001078 // cache is always NULL.
1079 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1080 Values[ 9] = Protocols;
1081 // ivar_layout for metaclass is always NULL.
1082 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1083 // The class extension is always unused for metaclasses.
1084 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1085 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1086 Values);
1087
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001088 std::string Name("\01L_OBJC_METACLASS_");
1089 Name += ClassName;
1090
1091 // Check for a forward reference.
1092 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1093 if (GV) {
1094 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1095 "Forward metaclass reference has incorrect type.");
1096 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1097 GV->setInitializer(Init);
1098 } else {
1099 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1100 llvm::GlobalValue::InternalLinkage,
1101 Init, Name,
1102 &CGM.getModule());
1103 }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001104 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1105 UsedGlobals.push_back(GV);
1106 // FIXME: Why?
1107 GV->setAlignment(32);
1108
1109 return GV;
1110}
1111
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001112llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
1113 std::string Name("\01L_OBJC_METACLASS_");
1114 Name += ID->getName();
1115
1116 // FIXME: Should we look these up somewhere other than the
1117 // module. Its a bit silly since we only generate these while
1118 // processing an implementation, so exactly one pointer would work
1119 // if know when we entered/exitted an implementation block.
1120
1121 // Check for an existing forward reference.
1122 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name)) {
1123 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1124 "Forward metaclass reference has incorrect type.");
1125 return GV;
1126 } else {
1127 // Generate as an external reference to keep a consistent
1128 // module. This will be patched up when we emit the metaclass.
1129 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1130 llvm::GlobalValue::ExternalLinkage,
1131 0,
1132 Name,
1133 &CGM.getModule());
1134 }
1135}
1136
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001137/*
1138 struct objc_class_ext {
1139 uint32_t size;
1140 const char *weak_ivar_layout;
1141 struct _objc_property_list *properties;
1142 };
1143*/
1144llvm::Constant *
1145CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1146 uint64_t Size =
1147 CGM.getTargetData().getABITypeSize(ObjCTypes.ClassExtensionTy);
1148
1149 std::vector<llvm::Constant*> Values(3);
1150 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001151 // FIXME: Output weak_ivar_layout string.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001152 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001153 Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") +
1154 ID->getName(),
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001155 ID,
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001156 ID->getClassInterface()->classprop_begin(),
1157 ID->getClassInterface()->classprop_end());
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001158
1159 // Return null if no extension bits are used.
1160 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1161 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1162
1163 llvm::Constant *Init =
1164 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1165 llvm::GlobalVariable *GV =
1166 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1167 llvm::GlobalValue::InternalLinkage,
1168 Init,
1169 (std::string("\01L_OBJC_CLASSEXT_") +
1170 ID->getName()),
1171 &CGM.getModule());
1172 // No special section, but goes in llvm.used
1173 UsedGlobals.push_back(GV);
1174
1175 return GV;
1176}
1177
1178/*
1179 struct objc_ivar {
1180 char *ivar_name;
1181 char *ivar_type;
1182 int ivar_offset;
1183 };
1184
1185 struct objc_ivar_list {
1186 int ivar_count;
1187 struct objc_ivar list[count];
1188 };
1189 */
1190llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1191 bool ForClass,
1192 const llvm::Type *InterfaceTy) {
1193 std::vector<llvm::Constant*> Ivars, Ivar(3);
1194
1195 // When emitting the root class GCC emits ivar entries for the
1196 // actual class structure. It is not clear if we need to follow this
1197 // behavior; for now lets try and get away with not doing it. If so,
1198 // the cleanest solution would be to make up an ObjCInterfaceDecl
1199 // for the class.
1200 if (ForClass)
1201 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1202
1203 const llvm::StructLayout *Layout =
1204 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
1205 for (ObjCInterfaceDecl::ivar_iterator
1206 i = ID->getClassInterface()->ivar_begin(),
1207 e = ID->getClassInterface()->ivar_end(); i != e; ++i) {
1208 ObjCIvarDecl *V = *i;
1209 unsigned Offset =
1210 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(V));
1211 std::string TypeStr;
1212 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1213 Ivar[0] = GetMethodVarName(V->getIdentifier());
1214 CGM.getContext().getObjCEncodingForType(V->getType(), TypeStr,
1215 EncodingRecordTypes);
1216 Ivar[1] = GetMethodVarType(TypeStr);
1217 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
1218 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy,
1219 Ivar));
1220 }
1221
1222 // Return null for empty list.
1223 if (Ivars.empty())
1224 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1225
1226 std::vector<llvm::Constant*> Values(2);
1227 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1228 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1229 Ivars.size());
1230 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1231 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1232
1233 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1234 "\01L_OBJC_INSTANCE_VARIABLES_");
1235 llvm::GlobalVariable *GV =
1236 new llvm::GlobalVariable(Init->getType(), false,
1237 llvm::GlobalValue::InternalLinkage,
1238 Init,
1239 std::string(Prefix) + ID->getName(),
1240 &CGM.getModule());
1241 if (ForClass) {
1242 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1243 // FIXME: Why is this only here?
1244 GV->setAlignment(32);
1245 } else {
1246 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1247 }
1248 UsedGlobals.push_back(GV);
1249 return llvm::ConstantExpr::getBitCast(GV,
1250 ObjCTypes.IvarListPtrTy);
1251}
1252
1253/*
1254 struct objc_method {
1255 SEL method_name;
1256 char *method_types;
1257 void *method;
1258 };
1259
1260 struct objc_method_list {
1261 struct objc_method_list *obsolete;
1262 int count;
1263 struct objc_method methods_list[count];
1264 };
1265*/
Daniel Dunbar12996f52008-08-26 21:51:14 +00001266
1267/// GetMethodConstant - Return a struct objc_method constant for the
1268/// given method if it has been defined. The result is null if the
1269/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001270llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001271 // FIXME: Use DenseMap::lookup
1272 llvm::Function *Fn = MethodDefinitions[MD];
1273 if (!Fn)
1274 return 0;
1275
1276 std::vector<llvm::Constant*> Method(3);
1277 Method[0] =
1278 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1279 ObjCTypes.SelectorPtrTy);
1280 Method[1] = GetMethodVarType(MD);
1281 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1282 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1283}
1284
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001285llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1286 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001287 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001288 // Return null for empty list.
1289 if (Methods.empty())
1290 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1291
1292 std::vector<llvm::Constant*> Values(3);
1293 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1294 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1295 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1296 Methods.size());
1297 Values[2] = llvm::ConstantArray::get(AT, Methods);
1298 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1299
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001300 llvm::GlobalVariable *GV =
1301 new llvm::GlobalVariable(Init->getType(), false,
1302 llvm::GlobalValue::InternalLinkage,
1303 Init,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001304 Name,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001305 &CGM.getModule());
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001306 GV->setSection(Section);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001307 UsedGlobals.push_back(GV);
1308 return llvm::ConstantExpr::getBitCast(GV,
1309 ObjCTypes.MethodListPtrTy);
Daniel Dunbarace33292008-08-16 03:19:19 +00001310}
1311
1312llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD) {
1313 const llvm::Type *ReturnTy =
1314 CGM.getTypes().ConvertReturnType(OMD->getResultType());
1315 const llvm::Type *SelfTy =
1316 CGM.getTypes().ConvertType(OMD->getSelfDecl()->getType());
1317
1318 std::vector<const llvm::Type*> ArgTys;
1319 ArgTys.reserve(1 + 2 + OMD->param_size());
1320
1321 // FIXME: This is not something we should have to be dealing with
1322 // here.
1323 bool useStructRet =
1324 CodeGen::CodeGenFunction::hasAggregateLLVMType(OMD->getResultType());
1325 if (useStructRet) {
1326 ArgTys.push_back(llvm::PointerType::getUnqual(ReturnTy));
1327 ReturnTy = llvm::Type::VoidTy;
1328 }
1329
1330 // Implicit arguments
1331 ArgTys.push_back(SelfTy);
1332 ArgTys.push_back(ObjCTypes.SelectorPtrTy);
1333
1334 for (ObjCMethodDecl::param_const_iterator
1335 i = OMD->param_begin(), e = OMD->param_end();
1336 i != e; ++i) {
1337 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
Daniel Dunbara04840b2008-08-23 03:46:30 +00001338 if (Ty->isSingleValueType()) {
Daniel Dunbarace33292008-08-16 03:19:19 +00001339 ArgTys.push_back(Ty);
1340 } else {
1341 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
1342 }
1343 }
1344
1345 std::string Name;
1346 GetNameForMethod(OMD, Name);
1347
1348 llvm::Function *Method =
1349 llvm::Function::Create(llvm::FunctionType::get(ReturnTy,
1350 ArgTys,
1351 OMD->isVariadic()),
1352 llvm::GlobalValue::InternalLinkage,
1353 Name,
1354 &CGM.getModule());
Daniel Dunbar12996f52008-08-26 21:51:14 +00001355 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarace33292008-08-16 03:19:19 +00001356
Daniel Dunbara04840b2008-08-23 03:46:30 +00001357 unsigned Offset = 3; // Return plus self and selector implicit args.
1358 if (useStructRet) {
Daniel Dunbarace33292008-08-16 03:19:19 +00001359 Method->addParamAttr(1, llvm::ParamAttr::StructRet);
Daniel Dunbara04840b2008-08-23 03:46:30 +00001360 ++Offset;
1361 }
1362
1363 // FIXME: This is horrible, we need to be reusing the machinery in
1364 // CodeGenModule.cpp (SetFunctionAttributes).
1365 for (ObjCMethodDecl::param_const_iterator
1366 i = OMD->param_begin(), e = OMD->param_end();
1367 i != e; ++i, ++Offset) {
1368 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
1369 if (!Ty->isSingleValueType())
1370 Method->addParamAttr(Offset, llvm::ParamAttr::ByVal);
1371 }
Daniel Dunbarace33292008-08-16 03:19:19 +00001372
1373 return Method;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001374}
1375
1376llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001377 // Abuse this interface function as a place to finalize.
1378 FinishModule();
1379
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001380 return NULL;
1381}
1382
Anders Carlsson58d16242008-08-31 04:05:03 +00001383llvm::Function *CGObjCMac::EnumerationMutationFunction()
1384{
1385 return ObjCTypes.EnumerationMutationFn;
1386}
1387
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001388/* *** Private Interface *** */
1389
1390/// EmitImageInfo - Emit the image info marker used to encode some module
1391/// level information.
1392///
1393/// See: <rdr://4810609&4810587&4810587>
1394/// struct IMAGE_INFO {
1395/// unsigned version;
1396/// unsigned flags;
1397/// };
1398enum ImageInfoFlags {
1399 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
1400 eImageInfo_GarbageCollected = (1 << 1),
1401 eImageInfo_GCOnly = (1 << 2)
1402};
1403
1404void CGObjCMac::EmitImageInfo() {
1405 unsigned version = 0; // Version is unused?
1406 unsigned flags = 0;
1407
1408 // FIXME: Fix and continue?
1409 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
1410 flags |= eImageInfo_GarbageCollected;
1411 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
1412 flags |= eImageInfo_GCOnly;
1413
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001414 // Emitted as int[2];
1415 llvm::Constant *values[2] = {
1416 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
1417 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
1418 };
1419 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001420 llvm::GlobalVariable *GV =
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001421 new llvm::GlobalVariable(AT, true,
1422 llvm::GlobalValue::InternalLinkage,
1423 llvm::ConstantArray::get(AT, values, 2),
1424 "\01L_OBJC_IMAGE_INFO",
1425 &CGM.getModule());
1426
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001427 if (ObjCABI == 1) {
1428 GV->setSection("__OBJC, __image_info,regular");
1429 } else {
1430 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
1431 }
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001432
1433 UsedGlobals.push_back(GV);
1434}
1435
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001436
1437// struct objc_module {
1438// unsigned long version;
1439// unsigned long size;
1440// const char *name;
1441// Symtab symtab;
1442// };
1443
1444// FIXME: Get from somewhere
1445static const int ModuleVersion = 7;
1446
1447void CGObjCMac::EmitModuleInfo() {
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001448 uint64_t Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ModuleTy);
1449
1450 std::vector<llvm::Constant*> Values(4);
1451 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
1452 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbarac93e472008-08-15 22:20:32 +00001453 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001454 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001455 Values[3] = EmitModuleSymbols();
1456
1457 llvm::GlobalVariable *GV =
1458 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
1459 llvm::GlobalValue::InternalLinkage,
1460 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
1461 Values),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001462 "\01L_OBJC_MODULES",
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001463 &CGM.getModule());
1464 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
1465 UsedGlobals.push_back(GV);
1466}
1467
1468llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001469 unsigned NumClasses = DefinedClasses.size();
1470 unsigned NumCategories = DefinedCategories.size();
1471
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001472 // Return null if no symbols were defined.
1473 if (!NumClasses && !NumCategories)
1474 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
1475
1476 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001477 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1478 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
1479 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
1480 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
1481
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001482 // The runtime expects exactly the list of defined classes followed
1483 // by the list of defined categories, in a single array.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001484 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001485 for (unsigned i=0; i<NumClasses; i++)
1486 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
1487 ObjCTypes.Int8PtrTy);
1488 for (unsigned i=0; i<NumCategories; i++)
1489 Symbols[NumClasses + i] =
1490 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
1491 ObjCTypes.Int8PtrTy);
1492
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001493 Values[4] =
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001494 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001495 NumClasses + NumCategories),
1496 Symbols);
1497
1498 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1499
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001500 llvm::GlobalVariable *GV =
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001501 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001502 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001503 Init,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001504 "\01L_OBJC_SYMBOLS",
1505 &CGM.getModule());
1506 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
1507 UsedGlobals.push_back(GV);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001508 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
1509}
1510
1511llvm::Value *CGObjCMac::EmitClassRef(llvm::IRBuilder<> &Builder,
1512 const ObjCInterfaceDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001513 LazySymbols.insert(ID->getIdentifier());
1514
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001515 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
1516
1517 if (!Entry) {
1518 llvm::Constant *Casted =
1519 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
1520 ObjCTypes.ClassPtrTy);
1521 Entry =
1522 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
1523 llvm::GlobalValue::InternalLinkage,
1524 Casted, "\01L_OBJC_CLASS_REFERENCES_",
1525 &CGM.getModule());
1526 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
1527 UsedGlobals.push_back(Entry);
1528 }
1529
1530 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001531}
1532
Daniel Dunbar5eec6142008-08-12 03:39:23 +00001533llvm::Value *CGObjCMac::EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
1534 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
1535
1536 if (!Entry) {
1537 llvm::Constant *Casted =
1538 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
1539 ObjCTypes.SelectorPtrTy);
1540 Entry =
1541 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
1542 llvm::GlobalValue::InternalLinkage,
1543 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
1544 &CGM.getModule());
1545 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
1546 UsedGlobals.push_back(Entry);
1547 }
1548
1549 return Builder.CreateLoad(Entry, false, "tmp");
1550}
1551
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001552llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
1553 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001554
1555 if (!Entry) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001556 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001557 Entry =
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001558 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001559 llvm::GlobalValue::InternalLinkage,
1560 C, "\01L_OBJC_CLASS_NAME_",
1561 &CGM.getModule());
1562 Entry->setSection("__TEXT,__cstring,cstring_literals");
1563 UsedGlobals.push_back(Entry);
1564 }
1565
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001566 return getConstantGEP(Entry, 0, 0);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001567}
1568
Daniel Dunbar5eec6142008-08-12 03:39:23 +00001569llvm::Constant *CGObjCMac::GetMethodVarName(Selector Sel) {
1570 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
1571
1572 if (!Entry) {
1573 llvm::Constant *C = llvm::ConstantArray::get(Sel.getName());
1574 Entry =
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001575 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar5eec6142008-08-12 03:39:23 +00001576 llvm::GlobalValue::InternalLinkage,
1577 C, "\01L_OBJC_METH_VAR_NAME_",
1578 &CGM.getModule());
1579 Entry->setSection("__TEXT,__cstring,cstring_literals");
1580 UsedGlobals.push_back(Entry);
1581 }
1582
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001583 return getConstantGEP(Entry, 0, 0);
1584}
1585
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001586// FIXME: Merge into a single cstring creation function.
1587llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) {
1588 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
1589}
1590
1591// FIXME: Merge into a single cstring creation function.
1592llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) {
1593 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
1594}
1595
1596llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) {
1597 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001598
1599 if (!Entry) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001600 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001601 Entry =
1602 new llvm::GlobalVariable(C->getType(), false,
1603 llvm::GlobalValue::InternalLinkage,
1604 C, "\01L_OBJC_METH_VAR_TYPE_",
1605 &CGM.getModule());
1606 Entry->setSection("__TEXT,__cstring,cstring_literals");
1607 UsedGlobals.push_back(Entry);
1608 }
1609
1610 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00001611}
1612
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001613// FIXME: Merge into a single cstring creation function.
Daniel Dunbar12996f52008-08-26 21:51:14 +00001614llvm::Constant *CGObjCMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001615 std::string TypeStr;
Daniel Dunbar12996f52008-08-26 21:51:14 +00001616 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
1617 TypeStr);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001618 return GetMethodVarType(TypeStr);
1619}
1620
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001621// FIXME: Merge into a single cstring creation function.
1622llvm::Constant *CGObjCMac::GetPropertyName(IdentifierInfo *Ident) {
1623 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
1624
1625 if (!Entry) {
1626 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
1627 Entry =
1628 new llvm::GlobalVariable(C->getType(), false,
1629 llvm::GlobalValue::InternalLinkage,
1630 C, "\01L_OBJC_PROP_NAME_ATTR_",
1631 &CGM.getModule());
1632 Entry->setSection("__TEXT,__cstring,cstring_literals");
1633 UsedGlobals.push_back(Entry);
1634 }
1635
1636 return getConstantGEP(Entry, 0, 0);
1637}
1638
1639// FIXME: Merge into a single cstring creation function.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001640// FIXME: This Decl should be more precise.
1641llvm::Constant *CGObjCMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
1642 const Decl *Container) {
1643 std::string TypeStr;
1644 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001645 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
1646}
1647
Daniel Dunbarace33292008-08-16 03:19:19 +00001648void CGObjCMac::GetNameForMethod(const ObjCMethodDecl *D,
1649 std::string &NameOut) {
1650 // FIXME: Find the mangling GCC uses.
1651 std::stringstream s;
1652 s << (D->isInstance() ? "-" : "+");
1653 s << "[";
1654 s << D->getClassInterface()->getName();
1655 s << " ";
1656 s << D->getSelector().getName();
1657 s << "]";
1658 NameOut = s.str();
1659}
1660
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001661void CGObjCMac::FinishModule() {
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001662 EmitModuleInfo();
1663
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001664 std::vector<llvm::Constant*> Used;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001665
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001666 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001667 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001668 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001669 }
1670
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001671 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001672 llvm::GlobalValue *GV =
1673 new llvm::GlobalVariable(AT, false,
1674 llvm::GlobalValue::AppendingLinkage,
1675 llvm::ConstantArray::get(AT, Used),
1676 "llvm.used",
1677 &CGM.getModule());
1678
1679 GV->setSection("llvm.metadata");
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001680
1681 // Add assembler directives to add lazy undefined symbol references
1682 // for classes which are referenced but not defined. This is
1683 // important for correct linker interaction.
1684
1685 // FIXME: Uh, this isn't particularly portable.
1686 std::stringstream s;
1687 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
1688 e = LazySymbols.end(); i != e; ++i) {
1689 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
1690 }
1691 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
1692 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001693 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001694 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
1695 }
1696 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001697}
1698
1699/* *** */
1700
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001701ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Daniel Dunbardbdb9512008-08-23 18:37:06 +00001702 : CGM(cgm)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001703{
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001704 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1705 ASTContext &Ctx = CGM.getContext();
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001706
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001707 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001708 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001709 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001710 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1711
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001712 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
1713 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001714
1715 // FIXME: It would be nice to unify this with the opaque type, so
1716 // that the IR comes out a bit cleaner.
1717 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
1718 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00001719
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001720 MethodDescriptionTy =
1721 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001722 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001723 NULL);
1724 CGM.getModule().addTypeName("struct._objc_method_description",
1725 MethodDescriptionTy);
1726
1727 MethodDescriptionListTy =
1728 llvm::StructType::get(IntTy,
1729 llvm::ArrayType::get(MethodDescriptionTy, 0),
1730 NULL);
1731 CGM.getModule().addTypeName("struct._objc_method_description_list",
1732 MethodDescriptionListTy);
1733 MethodDescriptionListPtrTy =
1734 llvm::PointerType::getUnqual(MethodDescriptionListTy);
1735
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001736 PropertyTy = llvm::StructType::get(Int8PtrTy,
1737 Int8PtrTy,
1738 NULL);
1739 CGM.getModule().addTypeName("struct._objc_property",
1740 PropertyTy);
1741
1742 PropertyListTy = llvm::StructType::get(IntTy,
1743 IntTy,
1744 llvm::ArrayType::get(PropertyTy, 0),
1745 NULL);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001746 CGM.getModule().addTypeName("struct._objc_property_list",
1747 PropertyListTy);
1748 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
1749
1750 // Protocol description structures
1751
1752 ProtocolExtensionTy =
1753 llvm::StructType::get(Types.ConvertType(Ctx.IntTy),
1754 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1755 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1756 PropertyListPtrTy,
1757 NULL);
1758 CGM.getModule().addTypeName("struct._objc_protocol_extension",
1759 ProtocolExtensionTy);
1760 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
1761
1762 // Handle recursive construction of Protocl and ProtocolList types
1763
1764 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
1765 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
1766
1767 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
1768 LongTy,
1769 llvm::ArrayType::get(ProtocolTyHolder, 0),
1770 NULL);
1771 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
1772
1773 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolExtensionTy),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001774 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001775 llvm::PointerType::getUnqual(ProtocolListTyHolder),
1776 MethodDescriptionListPtrTy,
1777 MethodDescriptionListPtrTy,
1778 NULL);
1779 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
1780
1781 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
1782 CGM.getModule().addTypeName("struct._objc_protocol_list",
1783 ProtocolListTy);
1784 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
1785
1786 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
1787 CGM.getModule().addTypeName("struct.__objc_protocol", ProtocolTy);
1788 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001789
1790 // Class description structures
1791
1792 IvarTy = llvm::StructType::get(Int8PtrTy,
1793 Int8PtrTy,
1794 IntTy,
1795 NULL);
1796 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
1797
1798 IvarListTy = llvm::OpaqueType::get();
1799 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
1800 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
1801
1802 MethodTy = llvm::StructType::get(SelectorPtrTy,
1803 Int8PtrTy,
1804 Int8PtrTy,
1805 NULL);
1806 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
1807
1808 MethodListTy = llvm::OpaqueType::get();
1809 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
1810 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
1811
1812 CacheTy = llvm::OpaqueType::get();
1813 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
1814 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
1815
1816 ClassExtensionTy =
1817 llvm::StructType::get(IntTy,
1818 Int8PtrTy,
1819 PropertyListPtrTy,
1820 NULL);
1821 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
1822 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
1823
1824 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
1825
1826 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
1827 llvm::PointerType::getUnqual(ClassTyHolder),
1828 Int8PtrTy,
1829 LongTy,
1830 LongTy,
1831 LongTy,
1832 IvarListPtrTy,
1833 MethodListPtrTy,
1834 CachePtrTy,
1835 ProtocolListPtrTy,
1836 Int8PtrTy,
1837 ClassExtensionPtrTy,
1838 NULL);
1839 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
1840
1841 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
1842 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
1843 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
1844
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001845 CategoryTy = llvm::StructType::get(Int8PtrTy,
1846 Int8PtrTy,
1847 MethodListPtrTy,
1848 MethodListPtrTy,
1849 ProtocolListPtrTy,
1850 IntTy,
1851 PropertyListPtrTy,
1852 NULL);
1853 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
1854
Daniel Dunbar0ed60b02008-08-30 03:02:31 +00001855 // I'm not sure I like this. The implicit coordination is a bit
1856 // gross. We should solve this in a reasonable fashion because this
1857 // is a pretty common task (match some runtime data structure with
1858 // an LLVM data structure).
1859
1860 // FIXME: This is leaked.
1861 // FIXME: Merge with rewriter code?
1862 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
1863 SourceLocation(),
1864 &Ctx.Idents.get("_objc_super"), 0);
1865 FieldDecl *FieldDecls[2];
1866 FieldDecls[0] = FieldDecl::Create(Ctx, SourceLocation(), 0,
1867 Ctx.getObjCIdType());
1868 FieldDecls[1] = FieldDecl::Create(Ctx, SourceLocation(), 0,
1869 Ctx.getObjCClassType());
1870 RD->defineBody(FieldDecls, 2);
1871
1872 SuperCTy = Ctx.getTagDeclType(RD);
1873 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
1874
1875 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001876 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
Daniel Dunbar15245e52008-08-23 04:28:29 +00001877
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001878 // Global metadata structures
1879
1880 SymtabTy = llvm::StructType::get(LongTy,
1881 SelectorPtrTy,
1882 ShortTy,
1883 ShortTy,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001884 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001885 NULL);
1886 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
1887 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
1888
1889 ModuleTy =
1890 llvm::StructType::get(LongTy,
1891 LongTy,
1892 Int8PtrTy,
1893 SymtabPtrTy,
1894 NULL);
1895 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001896
1897 // Message send functions
1898
1899 std::vector<const llvm::Type*> Params;
1900 Params.push_back(ObjectPtrTy);
1901 Params.push_back(SelectorPtrTy);
1902 MessageSendFn = llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1903 Params,
1904 true),
1905 llvm::Function::ExternalLinkage,
1906 "objc_msgSend",
1907 &CGM.getModule());
1908
1909 Params.clear();
1910 Params.push_back(Int8PtrTy);
1911 Params.push_back(ObjectPtrTy);
1912 Params.push_back(SelectorPtrTy);
1913 MessageSendStretFn =
1914 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1915 Params,
1916 true),
1917 llvm::Function::ExternalLinkage,
1918 "objc_msgSend_stret",
1919 &CGM.getModule());
1920
1921 Params.clear();
1922 Params.push_back(SuperPtrTy);
1923 Params.push_back(SelectorPtrTy);
1924 MessageSendSuperFn =
1925 llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1926 Params,
1927 true),
1928 llvm::Function::ExternalLinkage,
1929 "objc_msgSendSuper",
1930 &CGM.getModule());
1931
1932 Params.clear();
1933 Params.push_back(Int8PtrTy);
1934 Params.push_back(SuperPtrTy);
1935 Params.push_back(SelectorPtrTy);
1936 MessageSendSuperStretFn =
1937 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1938 Params,
1939 true),
1940 llvm::Function::ExternalLinkage,
1941 "objc_msgSendSuper_stret",
1942 &CGM.getModule());
Anders Carlsson58d16242008-08-31 04:05:03 +00001943
1944 // Enumeration mutation.
1945
1946 Params.clear();
1947 Params.push_back(ObjectPtrTy);
1948 EnumerationMutationFn =
1949 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1950 Params,
1951 false),
1952 llvm::Function::ExternalLinkage,
1953 "objc_enumerationMutation",
1954 &CGM.getModule());
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001955}
1956
1957ObjCTypesHelper::~ObjCTypesHelper() {
1958}
1959
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001960llvm::Value *ObjCTypesHelper::getMessageSendFn(bool IsSuper,
1961 const llvm::Type *ReturnTy) {
1962 llvm::Function *F;
1963 llvm::FunctionType *CallFTy;
1964
1965 // FIXME: Should we be caching any of this?
1966 if (!ReturnTy->isSingleValueType()) {
1967 F = IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
1968 std::vector<const llvm::Type*> Params(3);
1969 Params[0] = llvm::PointerType::getUnqual(ReturnTy);
1970 Params[1] = IsSuper ? SuperPtrTy : ObjectPtrTy;
1971 Params[2] = SelectorPtrTy;
1972 CallFTy = llvm::FunctionType::get(llvm::Type::VoidTy, Params, true);
Daniel Dunbar952f4732008-08-29 17:28:43 +00001973 } else { // FIXME: floating point?
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001974 F = IsSuper ? MessageSendSuperFn : MessageSendFn;
1975 std::vector<const llvm::Type*> Params(2);
1976 Params[0] = IsSuper ? SuperPtrTy : ObjectPtrTy;
1977 Params[1] = SelectorPtrTy;
1978 CallFTy = llvm::FunctionType::get(ReturnTy, Params, true);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00001979 }
1980
Daniel Dunbar87062ff2008-08-23 09:25:55 +00001981 return llvm::ConstantExpr::getBitCast(F,
1982 llvm::PointerType::getUnqual(CallFTy));
Daniel Dunbar15245e52008-08-23 04:28:29 +00001983}
1984
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00001985/* *** */
1986
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001987CodeGen::CGObjCRuntime *
1988CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001989 return new CGObjCMac(CGM);
1990}