blob: 798c9d3c4be8eda431012afe09cf0d5be74795ed [file] [log] [blame]
Daniel Dunbarc17a4d32008-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 Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000023#include "llvm/Module.h"
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000024#include "llvm/Support/IRBuilder.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000026#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000027
28using namespace clang;
29
30namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000031
Daniel Dunbarae226fa2008-08-27 02:31:56 +000032 typedef std::vector<llvm::Constant*> ConstantVector;
33
Daniel Dunbar6efc0c52008-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 Dunbarbbce49b2008-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 Dunbar14c80b72008-08-23 09:25:55 +000043 llvm::Function *MessageSendFn, *MessageSendStretFn;
44 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000045
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000046public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000047 const llvm::Type *ShortTy, *IntTy, *LongTy;
48 const llvm::Type *Int8PtrTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000049
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000050 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
51 const llvm::Type *ObjectPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000052 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000053 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000054 /// ProtocolPtrTy - LLVM type for external protocol handles
55 /// (typeof(Protocol))
56 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000057
Daniel Dunbar19cd87e2008-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 Dunbare8b470d2008-08-23 04:28:29 +000063 /// SuperTy - LLVM type for struct objc_super.
64 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000065 /// SuperPtrTy - LLVM type for struct objc_super *.
66 const llvm::Type *SuperPtrTy;
Daniel Dunbare8b470d2008-08-23 04:28:29 +000067
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000068 /// SymtabTy - LLVM type for struct objc_symtab.
69 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +000070 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
71 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000072 /// ModuleTy - LLVM type for struct objc_module.
73 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000074
Daniel Dunbar6efc0c52008-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 Dunbarc8ef5512008-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 Dunbar6efc0c52008-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 Dunbar86e253a2008-08-22 20:34:54 +0000106 /// CategoryTy - LLVM type for struct objc_category.
107 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-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 Dunbar6efc0c52008-08-13 03:21:16 +0000132
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000133 llvm::Function *EnumerationMutationFn;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000134public:
135 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
136 ~ObjCTypesHelper();
137
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000138 llvm::Value *getMessageSendFn(bool IsSuper, const llvm::Type *ReturnTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000139};
140
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000141class CGObjCMac : public CodeGen::CGObjCRuntime {
142private:
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000143 CodeGen::CodeGenModule &CGM;
144 ObjCTypesHelper ObjCTypes;
145 /// ObjCABI - FIXME: Not sure yet.
146 unsigned ObjCABI;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000147
Daniel Dunbar242d4dc2008-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 Dunbar4e2d7d02008-08-12 06:48:42 +0000158 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000159 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000160
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000161 /// MethodVarNames - uniqued method variable names.
162 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
163
Daniel Dunbar6efc0c52008-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 Dunbarc45ef602008-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 Dunbarc8ef5512008-08-23 00:19:03 +0000172 /// PropertyNames - uniqued method variable names.
173 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
174
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000175 /// ClassReferences - uniqued class references.
176 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
177
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000178 /// SelectorReferences - uniqued selector references.
179 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
180
Daniel Dunbar6efc0c52008-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 Dunbar27f9d772008-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 Dunbar6efc0c52008-08-13 03:21:16 +0000192 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000193 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000194 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-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 Dunbar4e2d7d02008-08-12 06:48:42 +0000200 /// EmitModuleInfo - Another marker encoding module level
201 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000202 void EmitModuleInfo();
203
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000204 /// EmitModuleSymols - Emit module symbols, the list of defined
205 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000206 llvm::Constant *EmitModuleSymbols();
207
Daniel Dunbarf77ac862008-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 Dunbar6efc0c52008-08-13 03:21:16 +0000211
Daniel Dunbar27f9d772008-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 Dunbar14c80b72008-08-23 09:25:55 +0000222 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000223 QualType ResultType,
224 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000225 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000226 QualType Arg0Ty,
227 bool IsSuper,
228 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000229
Daniel Dunbar27f9d772008-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 Dunbarf56f1912008-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 Dunbar27f9d772008-08-21 04:36:09 +0000244 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000245 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000246 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
247 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000248 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-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 Dunbar27f9d772008-08-21 04:36:09 +0000254
255 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000256 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000257 llvm::Constant *EmitMethodList(const std::string &Name,
258 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000259 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000260
261 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-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 Dunbarae226fa2008-08-27 02:31:56 +0000272 llvm::Constant *EmitMethodDescList(const std::string &Name,
273 const char *Section,
274 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000275
Daniel Dunbarc8ef5512008-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 Dunbarc56f34a2008-08-28 04:38:10 +0000279 const Decl *Container,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000280 ObjCPropertyDecl * const *begin,
281 ObjCPropertyDecl * const *end);
282
Daniel Dunbar6efc0c52008-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 Dunbarae226fa2008-08-27 02:31:56 +0000287 llvm::Constant *
288 EmitProtocolExtension(const ObjCProtocolDecl *PD,
289 const ConstantVector &OptInstanceMethods,
290 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000291
292 /// EmitProtocolList - Generate the list of referenced
293 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000294 llvm::Constant *EmitProtocolList(const std::string &Name,
295 ObjCProtocolDecl::protocol_iterator begin,
296 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000297
Daniel Dunbar27f9d772008-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 Dunbar6efc0c52008-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 Dunbar4e2d7d02008-08-12 06:48:42 +0000307 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000308 /// name. The return value has type char *.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000309 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000310
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000311 /// GetMethodVarName - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000312 /// selector's name. The return value has type char *.
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000313 llvm::Constant *GetMethodVarName(Selector Sel);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000314 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000315 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000316
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000317 /// GetMethodVarType - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000318 /// selector's name. The return value has type char *.
319
320 // FIXME: This is a horrible name.
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000321 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000322 llvm::Constant *GetMethodVarType(const std::string &Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000323
Daniel Dunbarc8ef5512008-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 Dunbarc56f34a2008-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 Dunbarc8ef5512008-08-23 00:19:03 +0000331
Daniel Dunbarb7ec2462008-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 Dunbarc17a4d32008-08-11 02:45:11 +0000337public:
338 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000339 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000340
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000341 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000342 QualType ResultType,
343 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000344 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000345 bool IsClassMessage,
346 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000347
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000348 virtual CodeGen::RValue
349 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000350 QualType ResultType,
351 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000352 const ObjCInterfaceDecl *Class,
353 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000354 bool IsClassMessage,
355 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000356
357 virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000358 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000359
360 virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
361
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000362 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000363
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000364 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000365
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000366 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000367
368 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000369 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000370
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000371 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000372
373 virtual llvm::Function *ModuleInitFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000374 virtual llvm::Function *EnumerationMutationFunction();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000375};
376} // end anonymous namespace
Daniel Dunbarbbce49b2008-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 Dunbarc17a4d32008-08-11 02:45:11 +0000392
Daniel Dunbarbbce49b2008-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 Dunbarc17a4d32008-08-11 02:45:11 +0000403}
404
Daniel Dunbarddb2a3d2008-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 Dunbar27f9d772008-08-21 04:36:09 +0000408 const ObjCInterfaceDecl *ID) {
409 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-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 Dunbar259d93d2008-08-12 03:39:23 +0000414 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000415}
416
Daniel Dunbarbbce49b2008-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 Dunbar3e9df992008-08-23 18:37:06 +0000428 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-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 Dunbar8f2926b2008-08-23 03:46:30 +0000434CodeGen::RValue
435CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000436 QualType ResultType,
437 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000438 const ObjCInterfaceDecl *Class,
439 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000440 bool IsClassMessage,
441 const CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-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 Dunbare8b470d2008-08-23 04:28:29 +0000450
Daniel Dunbarf56f1912008-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 Dunbar19cd87e2008-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 Dunbarf56f1912008-08-25 08:19:24 +0000466 CGF.Builder.CreateStore(Target,
467 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
468
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000469 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000470 ObjCSuper, ObjCTypes.SuperPtrCTy,
471 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000472}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000473
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000474/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000475CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000476 QualType ResultType,
477 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000478 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000479 bool IsClassMessage,
480 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000481 llvm::Value *Arg0 =
482 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000483 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000484 Arg0, CGF.getContext().getObjCIdType(),
485 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000486}
487
488CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000489 QualType ResultType,
490 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000491 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-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 Dunbar7f8ea5c2008-08-30 05:35:15 +0000497 ActualArgs.push_back(std::make_pair(EmitSelector(CGF.Builder, Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000498 CGF.getContext().getObjCSelType()));
499 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000500
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000501 // FIXME: This is a hack, we are implicitly coordinating with
502 // EmitCallExprExt, which will move the return type to the first
503 // parameter and set the structure return flag. See
504 // getMessageSendFn().
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000505
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000506 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000507 return CGF.EmitCall(ObjCTypes.getMessageSendFn(IsSuper, ReturnTy),
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000508 ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000509}
510
511llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000512 const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000513 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
514 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000515}
516
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000517/*
518 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
519 struct _objc_protocol {
520 struct _objc_protocol_extension *isa;
521 char *protocol_name;
522 struct _objc_protocol_list *protocol_list;
523 struct _objc__method_prototype_list *instance_methods;
524 struct _objc__method_prototype_list *class_methods
525 };
526
527 See EmitProtocolExtension().
528*/
529void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000530 // FIXME: I don't understand why gcc generates this, or where it is
531 // resolved. Investigate. Its also wasteful to look this up over and
532 // over.
533 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
534
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000535 const char *ProtocolName = PD->getName();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000536
537 // Construct method lists.
538 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
539 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
540 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
541 e = PD->instmeth_end(); i != e; ++i) {
542 ObjCMethodDecl *MD = *i;
543 llvm::Constant *C = GetMethodDescriptionConstant(MD);
544 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
545 OptInstanceMethods.push_back(C);
546 } else {
547 InstanceMethods.push_back(C);
548 }
549 }
550
551 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
552 e = PD->classmeth_end(); i != e; ++i) {
553 ObjCMethodDecl *MD = *i;
554 llvm::Constant *C = GetMethodDescriptionConstant(MD);
555 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
556 OptClassMethods.push_back(C);
557 } else {
558 ClassMethods.push_back(C);
559 }
560 }
561
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000562 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000563 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000564 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000565 Values[2] =
566 EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(),
567 PD->protocol_begin(),
568 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000569 Values[3] =
570 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_")
571 + PD->getName(),
572 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
573 InstanceMethods);
574 Values[4] =
575 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_")
576 + PD->getName(),
577 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
578 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000579 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
580 Values);
581
582 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
583 if (Entry) {
584 // Already created, just update the initializer
585 Entry->setInitializer(Init);
586 } else {
587 Entry =
588 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
589 llvm::GlobalValue::InternalLinkage,
590 Init,
591 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
592 &CGM.getModule());
593 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
594 UsedGlobals.push_back(Entry);
595 // FIXME: Is this necessary? Why only for protocol?
596 Entry->setAlignment(4);
597 }
598}
599
600llvm::GlobalVariable *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
601 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
602
603 if (!Entry) {
604 std::vector<llvm::Constant*> Values(5);
605 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
606 Values[1] = GetClassName(PD->getIdentifier());
607 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
608 Values[3] = Values[4] =
609 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
610 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
611 Values);
612
613 Entry =
614 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
615 llvm::GlobalValue::InternalLinkage,
616 Init,
617 std::string("\01L_OBJC_PROTOCOL_")+PD->getName(),
618 &CGM.getModule());
619 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
620 UsedGlobals.push_back(Entry);
621 // FIXME: Is this necessary? Why only for protocol?
622 Entry->setAlignment(4);
623 }
624
625 return Entry;
626}
627
628/*
629 struct _objc_protocol_extension {
630 uint32_t size;
631 struct objc_method_description_list *optional_instance_methods;
632 struct objc_method_description_list *optional_class_methods;
633 struct objc_property_list *instance_properties;
634 };
635*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000636llvm::Constant *
637CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
638 const ConstantVector &OptInstanceMethods,
639 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000640 uint64_t Size =
641 CGM.getTargetData().getABITypeSize(ObjCTypes.ProtocolExtensionTy);
642 std::vector<llvm::Constant*> Values(4);
643 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000644 Values[1] =
645 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_")
646 + PD->getName(),
647 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
648 OptInstanceMethods);
649 Values[2] =
650 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_")
651 + PD->getName(),
652 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
653 OptClassMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000654 Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") +
655 PD->getName(),
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000656 0,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000657 PD->classprop_begin(),
658 PD->classprop_end());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000659
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000660 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000661 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
662 Values[3]->isNullValue())
663 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
664
665 llvm::Constant *Init =
666 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
667 llvm::GlobalVariable *GV =
668 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
669 llvm::GlobalValue::InternalLinkage,
670 Init,
671 (std::string("\01L_OBJC_PROTOCOLEXT_") +
672 PD->getName()),
673 &CGM.getModule());
674 // No special section, but goes in llvm.used
675 UsedGlobals.push_back(GV);
676
677 return GV;
678}
679
680/*
681 struct objc_protocol_list {
682 struct objc_protocol_list *next;
683 long count;
684 Protocol *list[];
685 };
686*/
Daniel Dunbardbc933702008-08-21 21:57:41 +0000687llvm::Constant *
688CGObjCMac::EmitProtocolList(const std::string &Name,
689 ObjCProtocolDecl::protocol_iterator begin,
690 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000691 std::vector<llvm::Constant*> ProtocolRefs;
692
Daniel Dunbardbc933702008-08-21 21:57:41 +0000693 for (; begin != end; ++begin)
694 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000695
696 // Just return null for empty protocol lists
697 if (ProtocolRefs.empty())
698 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
699
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000700 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000701 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
702
703 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000704 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000705 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
706 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
707 Values[2] =
708 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
709 ProtocolRefs.size()),
710 ProtocolRefs);
711
712 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
713 llvm::GlobalVariable *GV =
714 new llvm::GlobalVariable(Init->getType(), false,
715 llvm::GlobalValue::InternalLinkage,
716 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +0000717 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000718 &CGM.getModule());
719 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
720 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
721}
722
723/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000724 struct _objc_property {
725 const char * const name;
726 const char * const attributes;
727 };
728
729 struct _objc_property_list {
730 uint32_t entsize; // sizeof (struct _objc_property)
731 uint32_t prop_count;
732 struct _objc_property[prop_count];
733 };
734*/
735llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000736 const Decl *Container,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000737 ObjCPropertyDecl * const *begin,
738 ObjCPropertyDecl * const *end) {
739 std::vector<llvm::Constant*> Properties, Prop(2);
740 for (; begin != end; ++begin) {
741 const ObjCPropertyDecl *PD = *begin;
742 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000743 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000744 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
745 Prop));
746 }
747
748 // Return null for empty list.
749 if (Properties.empty())
750 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
751
752 unsigned PropertySize =
753 CGM.getTargetData().getABITypeSize(ObjCTypes.PropertyTy);
754 std::vector<llvm::Constant*> Values(3);
755 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
756 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
757 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
758 Properties.size());
759 Values[2] = llvm::ConstantArray::get(AT, Properties);
760 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
761
762 llvm::GlobalVariable *GV =
763 new llvm::GlobalVariable(Init->getType(), false,
764 llvm::GlobalValue::InternalLinkage,
765 Init,
766 Name,
767 &CGM.getModule());
768 // No special section on property lists?
769 UsedGlobals.push_back(GV);
770 return llvm::ConstantExpr::getBitCast(GV,
771 ObjCTypes.PropertyListPtrTy);
772
773}
774
775/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000776 struct objc_method_description_list {
777 int count;
778 struct objc_method_description list[];
779 };
780*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000781llvm::Constant *
782CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
783 std::vector<llvm::Constant*> Desc(2);
784 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
785 ObjCTypes.SelectorPtrTy);
786 Desc[1] = GetMethodVarType(MD);
787 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
788 Desc);
789}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000790
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000791llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
792 const char *Section,
793 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000794 // Return null for empty list.
795 if (Methods.empty())
796 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
797
798 std::vector<llvm::Constant*> Values(2);
799 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
800 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
801 Methods.size());
802 Values[1] = llvm::ConstantArray::get(AT, Methods);
803 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
804
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000805 llvm::GlobalVariable *GV =
806 new llvm::GlobalVariable(Init->getType(), false,
807 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000808 Init, Name, &CGM.getModule());
809 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000810 UsedGlobals.push_back(GV);
811 return llvm::ConstantExpr::getBitCast(GV,
812 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000813}
814
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000815/*
816 struct _objc_category {
817 char *category_name;
818 char *class_name;
819 struct _objc_method_list *instance_methods;
820 struct _objc_method_list *class_methods;
821 struct _objc_protocol_list *protocols;
822 uint32_t size; // <rdar://4585769>
823 struct _objc_property_list *instance_properties;
824 };
825 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000826void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000827 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.CategoryTy);
828
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000829 // FIXME: This is poor design, the OCD should have a pointer to the
830 // category decl. Additionally, note that Category can be null for
831 // the @implementation w/o an @interface case. Sema should just
832 // create one for us as it does for @implementation so everyone else
833 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000834 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000835 const ObjCCategoryDecl *Category =
836 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000837 std::string ExtName(std::string(Interface->getName()) +
838 "_" +
839 OCD->getName());
840
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000841 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
842 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
843 e = OCD->instmeth_end(); i != e; ++i) {
844 // Instance methods should always be defined.
845 InstanceMethods.push_back(GetMethodConstant(*i));
846 }
847 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
848 e = OCD->classmeth_end(); i != e; ++i) {
849 // Class methods should always be defined.
850 ClassMethods.push_back(GetMethodConstant(*i));
851 }
852
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000853 std::vector<llvm::Constant*> Values(7);
854 Values[0] = GetClassName(OCD->getIdentifier());
855 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000856 Values[2] =
857 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
858 ExtName,
859 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000860 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000861 Values[3] =
862 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
863 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000864 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000865 if (Category) {
866 Values[4] =
867 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
868 Category->protocol_begin(),
869 Category->protocol_end());
870 } else {
871 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
872 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000873 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000874
875 // If there is no category @interface then there can be no properties.
876 if (Category) {
877 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000878 OCD,
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000879 Category->classprop_begin(),
880 Category->classprop_end());
881 } else {
882 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
883 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000884
885 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
886 Values);
887
888 llvm::GlobalVariable *GV =
889 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
890 llvm::GlobalValue::InternalLinkage,
891 Init,
892 std::string("\01L_OBJC_CATEGORY_")+ExtName,
893 &CGM.getModule());
894 GV->setSection("__OBJC,__category,regular,no_dead_strip");
895 UsedGlobals.push_back(GV);
896 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000897}
898
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000899// FIXME: Get from somewhere?
900enum ClassFlags {
901 eClassFlags_Factory = 0x00001,
902 eClassFlags_Meta = 0x00002,
903 // <rdr://5142207>
904 eClassFlags_HasCXXStructors = 0x02000,
905 eClassFlags_Hidden = 0x20000,
906 eClassFlags_ABI2_Hidden = 0x00010,
907 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
908};
909
910// <rdr://5142207&4705298&4843145>
911static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
912 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
913 // FIXME: Support -fvisibility
914 switch (attr->getVisibility()) {
915 default:
916 assert(0 && "Unknown visibility");
917 return false;
918 case VisibilityAttr::DefaultVisibility:
919 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
920 return false;
921 case VisibilityAttr::HiddenVisibility:
922 return true;
923 }
924 } else {
925 return false; // FIXME: Support -fvisibility
926 }
927}
928
929/*
930 struct _objc_class {
931 Class isa;
932 Class super_class;
933 const char *name;
934 long version;
935 long info;
936 long instance_size;
937 struct _objc_ivar_list *ivars;
938 struct _objc_method_list *methods;
939 struct _objc_cache *cache;
940 struct _objc_protocol_list *protocols;
941 // Objective-C 1.0 extensions (<rdr://4585769>)
942 const char *ivar_layout;
943 struct _objc_class_ext *ext;
944 };
945
946 See EmitClassExtension();
947 */
948void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000949 DefinedSymbols.insert(ID->getIdentifier());
950
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000951 const char *ClassName = ID->getName();
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000952 // FIXME: Gross
953 ObjCInterfaceDecl *Interface =
954 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000955 llvm::Constant *Protocols =
956 EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(),
957 Interface->protocol_begin(),
958 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000959 const llvm::Type *InterfaceTy =
960 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
961 unsigned Flags = eClassFlags_Factory;
962 unsigned Size = CGM.getTargetData().getABITypeSize(InterfaceTy);
963
964 // FIXME: Set CXX-structors flag.
965 if (IsClassHidden(ID->getClassInterface()))
966 Flags |= eClassFlags_Hidden;
967
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000968 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
969 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
970 e = ID->instmeth_end(); i != e; ++i) {
971 // Instance methods should always be defined.
972 InstanceMethods.push_back(GetMethodConstant(*i));
973 }
974 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
975 e = ID->classmeth_end(); i != e; ++i) {
976 // Class methods should always be defined.
977 ClassMethods.push_back(GetMethodConstant(*i));
978 }
979
980 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
981 e = ID->propimpl_end(); i != e; ++i) {
982 ObjCPropertyImplDecl *PID = *i;
983
984 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
985 ObjCPropertyDecl *PD = PID->getPropertyDecl();
986
987 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
988 if (llvm::Constant *C = GetMethodConstant(MD))
989 InstanceMethods.push_back(C);
990 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
991 if (llvm::Constant *C = GetMethodConstant(MD))
992 InstanceMethods.push_back(C);
993 }
994 }
995
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000996 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000997 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000998 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000999 // Record a reference to the super class.
1000 LazySymbols.insert(Super->getIdentifier());
1001
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001002 Values[ 1] =
1003 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1004 ObjCTypes.ClassPtrTy);
1005 } else {
1006 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1007 }
1008 Values[ 2] = GetClassName(ID->getIdentifier());
1009 // Version is always 0.
1010 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1011 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1012 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1013 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001014 Values[ 7] =
1015 EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(),
1016 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001017 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001018 // cache is always NULL.
1019 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1020 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001021 // FIXME: Set ivar_layout
1022 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001023 Values[11] = EmitClassExtension(ID);
1024 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1025 Values);
1026
1027 llvm::GlobalVariable *GV =
1028 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1029 llvm::GlobalValue::InternalLinkage,
1030 Init,
1031 std::string("\01L_OBJC_CLASS_")+ClassName,
1032 &CGM.getModule());
1033 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1034 UsedGlobals.push_back(GV);
1035 // FIXME: Why?
1036 GV->setAlignment(32);
1037 DefinedClasses.push_back(GV);
1038}
1039
1040llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1041 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001042 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001043 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001044 const char *ClassName = ID->getName();
1045 unsigned Flags = eClassFlags_Meta;
1046 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ClassTy);
1047
1048 if (IsClassHidden(ID->getClassInterface()))
1049 Flags |= eClassFlags_Hidden;
1050
1051 std::vector<llvm::Constant*> Values(12);
1052 // The isa for the metaclass is the root of the hierarchy.
1053 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1054 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1055 Root = Super;
1056 Values[ 0] =
1057 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1058 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001059 // The super class for the metaclass is emitted as the name of the
1060 // super class. The runtime fixes this up to point to the
1061 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001062 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1063 Values[ 1] =
1064 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1065 ObjCTypes.ClassPtrTy);
1066 } else {
1067 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1068 }
1069 Values[ 2] = GetClassName(ID->getIdentifier());
1070 // Version is always 0.
1071 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1072 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1073 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1074 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001075 Values[ 7] =
1076 EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(),
1077 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001078 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001079 // cache is always NULL.
1080 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1081 Values[ 9] = Protocols;
1082 // ivar_layout for metaclass is always NULL.
1083 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1084 // The class extension is always unused for metaclasses.
1085 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1086 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1087 Values);
1088
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001089 std::string Name("\01L_OBJC_METACLASS_");
1090 Name += ClassName;
1091
1092 // Check for a forward reference.
1093 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1094 if (GV) {
1095 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1096 "Forward metaclass reference has incorrect type.");
1097 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1098 GV->setInitializer(Init);
1099 } else {
1100 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1101 llvm::GlobalValue::InternalLinkage,
1102 Init, Name,
1103 &CGM.getModule());
1104 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001105 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1106 UsedGlobals.push_back(GV);
1107 // FIXME: Why?
1108 GV->setAlignment(32);
1109
1110 return GV;
1111}
1112
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001113llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
1114 std::string Name("\01L_OBJC_METACLASS_");
1115 Name += ID->getName();
1116
1117 // FIXME: Should we look these up somewhere other than the
1118 // module. Its a bit silly since we only generate these while
1119 // processing an implementation, so exactly one pointer would work
1120 // if know when we entered/exitted an implementation block.
1121
1122 // Check for an existing forward reference.
1123 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name)) {
1124 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1125 "Forward metaclass reference has incorrect type.");
1126 return GV;
1127 } else {
1128 // Generate as an external reference to keep a consistent
1129 // module. This will be patched up when we emit the metaclass.
1130 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1131 llvm::GlobalValue::ExternalLinkage,
1132 0,
1133 Name,
1134 &CGM.getModule());
1135 }
1136}
1137
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001138/*
1139 struct objc_class_ext {
1140 uint32_t size;
1141 const char *weak_ivar_layout;
1142 struct _objc_property_list *properties;
1143 };
1144*/
1145llvm::Constant *
1146CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1147 uint64_t Size =
1148 CGM.getTargetData().getABITypeSize(ObjCTypes.ClassExtensionTy);
1149
1150 std::vector<llvm::Constant*> Values(3);
1151 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001152 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001153 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001154 Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") +
1155 ID->getName(),
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001156 ID,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001157 ID->getClassInterface()->classprop_begin(),
1158 ID->getClassInterface()->classprop_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001159
1160 // Return null if no extension bits are used.
1161 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1162 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1163
1164 llvm::Constant *Init =
1165 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1166 llvm::GlobalVariable *GV =
1167 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1168 llvm::GlobalValue::InternalLinkage,
1169 Init,
1170 (std::string("\01L_OBJC_CLASSEXT_") +
1171 ID->getName()),
1172 &CGM.getModule());
1173 // No special section, but goes in llvm.used
1174 UsedGlobals.push_back(GV);
1175
1176 return GV;
1177}
1178
1179/*
1180 struct objc_ivar {
1181 char *ivar_name;
1182 char *ivar_type;
1183 int ivar_offset;
1184 };
1185
1186 struct objc_ivar_list {
1187 int ivar_count;
1188 struct objc_ivar list[count];
1189 };
1190 */
1191llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1192 bool ForClass,
1193 const llvm::Type *InterfaceTy) {
1194 std::vector<llvm::Constant*> Ivars, Ivar(3);
1195
1196 // When emitting the root class GCC emits ivar entries for the
1197 // actual class structure. It is not clear if we need to follow this
1198 // behavior; for now lets try and get away with not doing it. If so,
1199 // the cleanest solution would be to make up an ObjCInterfaceDecl
1200 // for the class.
1201 if (ForClass)
1202 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1203
1204 const llvm::StructLayout *Layout =
1205 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
1206 for (ObjCInterfaceDecl::ivar_iterator
1207 i = ID->getClassInterface()->ivar_begin(),
1208 e = ID->getClassInterface()->ivar_end(); i != e; ++i) {
1209 ObjCIvarDecl *V = *i;
1210 unsigned Offset =
1211 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(V));
1212 std::string TypeStr;
1213 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1214 Ivar[0] = GetMethodVarName(V->getIdentifier());
1215 CGM.getContext().getObjCEncodingForType(V->getType(), TypeStr,
1216 EncodingRecordTypes);
1217 Ivar[1] = GetMethodVarType(TypeStr);
1218 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
1219 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy,
1220 Ivar));
1221 }
1222
1223 // Return null for empty list.
1224 if (Ivars.empty())
1225 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1226
1227 std::vector<llvm::Constant*> Values(2);
1228 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1229 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1230 Ivars.size());
1231 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1232 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1233
1234 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1235 "\01L_OBJC_INSTANCE_VARIABLES_");
1236 llvm::GlobalVariable *GV =
1237 new llvm::GlobalVariable(Init->getType(), false,
1238 llvm::GlobalValue::InternalLinkage,
1239 Init,
1240 std::string(Prefix) + ID->getName(),
1241 &CGM.getModule());
1242 if (ForClass) {
1243 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1244 // FIXME: Why is this only here?
1245 GV->setAlignment(32);
1246 } else {
1247 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1248 }
1249 UsedGlobals.push_back(GV);
1250 return llvm::ConstantExpr::getBitCast(GV,
1251 ObjCTypes.IvarListPtrTy);
1252}
1253
1254/*
1255 struct objc_method {
1256 SEL method_name;
1257 char *method_types;
1258 void *method;
1259 };
1260
1261 struct objc_method_list {
1262 struct objc_method_list *obsolete;
1263 int count;
1264 struct objc_method methods_list[count];
1265 };
1266*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001267
1268/// GetMethodConstant - Return a struct objc_method constant for the
1269/// given method if it has been defined. The result is null if the
1270/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001271llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001272 // FIXME: Use DenseMap::lookup
1273 llvm::Function *Fn = MethodDefinitions[MD];
1274 if (!Fn)
1275 return 0;
1276
1277 std::vector<llvm::Constant*> Method(3);
1278 Method[0] =
1279 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1280 ObjCTypes.SelectorPtrTy);
1281 Method[1] = GetMethodVarType(MD);
1282 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1283 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1284}
1285
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001286llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1287 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001288 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001289 // Return null for empty list.
1290 if (Methods.empty())
1291 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1292
1293 std::vector<llvm::Constant*> Values(3);
1294 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1295 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1296 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1297 Methods.size());
1298 Values[2] = llvm::ConstantArray::get(AT, Methods);
1299 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1300
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001301 llvm::GlobalVariable *GV =
1302 new llvm::GlobalVariable(Init->getType(), false,
1303 llvm::GlobalValue::InternalLinkage,
1304 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001305 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001306 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001307 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001308 UsedGlobals.push_back(GV);
1309 return llvm::ConstantExpr::getBitCast(GV,
1310 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001311}
1312
1313llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD) {
1314 const llvm::Type *ReturnTy =
1315 CGM.getTypes().ConvertReturnType(OMD->getResultType());
1316 const llvm::Type *SelfTy =
1317 CGM.getTypes().ConvertType(OMD->getSelfDecl()->getType());
1318
1319 std::vector<const llvm::Type*> ArgTys;
1320 ArgTys.reserve(1 + 2 + OMD->param_size());
1321
1322 // FIXME: This is not something we should have to be dealing with
1323 // here.
1324 bool useStructRet =
1325 CodeGen::CodeGenFunction::hasAggregateLLVMType(OMD->getResultType());
1326 if (useStructRet) {
1327 ArgTys.push_back(llvm::PointerType::getUnqual(ReturnTy));
1328 ReturnTy = llvm::Type::VoidTy;
1329 }
1330
1331 // Implicit arguments
1332 ArgTys.push_back(SelfTy);
1333 ArgTys.push_back(ObjCTypes.SelectorPtrTy);
1334
1335 for (ObjCMethodDecl::param_const_iterator
1336 i = OMD->param_begin(), e = OMD->param_end();
1337 i != e; ++i) {
1338 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001339 if (Ty->isSingleValueType()) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001340 ArgTys.push_back(Ty);
1341 } else {
1342 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
1343 }
1344 }
1345
1346 std::string Name;
1347 GetNameForMethod(OMD, Name);
1348
1349 llvm::Function *Method =
1350 llvm::Function::Create(llvm::FunctionType::get(ReturnTy,
1351 ArgTys,
1352 OMD->isVariadic()),
1353 llvm::GlobalValue::InternalLinkage,
1354 Name,
1355 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001356 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001357
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001358 unsigned Offset = 3; // Return plus self and selector implicit args.
1359 if (useStructRet) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001360 Method->addParamAttr(1, llvm::ParamAttr::StructRet);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001361 ++Offset;
1362 }
1363
1364 // FIXME: This is horrible, we need to be reusing the machinery in
1365 // CodeGenModule.cpp (SetFunctionAttributes).
1366 for (ObjCMethodDecl::param_const_iterator
1367 i = OMD->param_begin(), e = OMD->param_end();
1368 i != e; ++i, ++Offset) {
1369 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
1370 if (!Ty->isSingleValueType())
1371 Method->addParamAttr(Offset, llvm::ParamAttr::ByVal);
1372 }
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001373
1374 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001375}
1376
1377llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001378 // Abuse this interface function as a place to finalize.
1379 FinishModule();
1380
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001381 return NULL;
1382}
1383
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001384llvm::Function *CGObjCMac::EnumerationMutationFunction()
1385{
1386 return ObjCTypes.EnumerationMutationFn;
1387}
1388
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001389/* *** Private Interface *** */
1390
1391/// EmitImageInfo - Emit the image info marker used to encode some module
1392/// level information.
1393///
1394/// See: <rdr://4810609&4810587&4810587>
1395/// struct IMAGE_INFO {
1396/// unsigned version;
1397/// unsigned flags;
1398/// };
1399enum ImageInfoFlags {
1400 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
1401 eImageInfo_GarbageCollected = (1 << 1),
1402 eImageInfo_GCOnly = (1 << 2)
1403};
1404
1405void CGObjCMac::EmitImageInfo() {
1406 unsigned version = 0; // Version is unused?
1407 unsigned flags = 0;
1408
1409 // FIXME: Fix and continue?
1410 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
1411 flags |= eImageInfo_GarbageCollected;
1412 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
1413 flags |= eImageInfo_GCOnly;
1414
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001415 // Emitted as int[2];
1416 llvm::Constant *values[2] = {
1417 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
1418 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
1419 };
1420 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001421 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001422 new llvm::GlobalVariable(AT, true,
1423 llvm::GlobalValue::InternalLinkage,
1424 llvm::ConstantArray::get(AT, values, 2),
1425 "\01L_OBJC_IMAGE_INFO",
1426 &CGM.getModule());
1427
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001428 if (ObjCABI == 1) {
1429 GV->setSection("__OBJC, __image_info,regular");
1430 } else {
1431 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
1432 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001433
1434 UsedGlobals.push_back(GV);
1435}
1436
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001437
1438// struct objc_module {
1439// unsigned long version;
1440// unsigned long size;
1441// const char *name;
1442// Symtab symtab;
1443// };
1444
1445// FIXME: Get from somewhere
1446static const int ModuleVersion = 7;
1447
1448void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001449 uint64_t Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ModuleTy);
1450
1451 std::vector<llvm::Constant*> Values(4);
1452 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
1453 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001454 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001455 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001456 Values[3] = EmitModuleSymbols();
1457
1458 llvm::GlobalVariable *GV =
1459 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
1460 llvm::GlobalValue::InternalLinkage,
1461 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
1462 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001463 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001464 &CGM.getModule());
1465 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
1466 UsedGlobals.push_back(GV);
1467}
1468
1469llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001470 unsigned NumClasses = DefinedClasses.size();
1471 unsigned NumCategories = DefinedCategories.size();
1472
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001473 // Return null if no symbols were defined.
1474 if (!NumClasses && !NumCategories)
1475 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
1476
1477 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001478 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1479 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
1480 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
1481 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
1482
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001483 // The runtime expects exactly the list of defined classes followed
1484 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001485 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001486 for (unsigned i=0; i<NumClasses; i++)
1487 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
1488 ObjCTypes.Int8PtrTy);
1489 for (unsigned i=0; i<NumCategories; i++)
1490 Symbols[NumClasses + i] =
1491 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
1492 ObjCTypes.Int8PtrTy);
1493
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001494 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001495 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001496 NumClasses + NumCategories),
1497 Symbols);
1498
1499 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1500
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001501 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001502 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001503 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001504 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001505 "\01L_OBJC_SYMBOLS",
1506 &CGM.getModule());
1507 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
1508 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001509 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
1510}
1511
1512llvm::Value *CGObjCMac::EmitClassRef(llvm::IRBuilder<> &Builder,
1513 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001514 LazySymbols.insert(ID->getIdentifier());
1515
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001516 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
1517
1518 if (!Entry) {
1519 llvm::Constant *Casted =
1520 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
1521 ObjCTypes.ClassPtrTy);
1522 Entry =
1523 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
1524 llvm::GlobalValue::InternalLinkage,
1525 Casted, "\01L_OBJC_CLASS_REFERENCES_",
1526 &CGM.getModule());
1527 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
1528 UsedGlobals.push_back(Entry);
1529 }
1530
1531 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001532}
1533
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001534llvm::Value *CGObjCMac::EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
1535 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
1536
1537 if (!Entry) {
1538 llvm::Constant *Casted =
1539 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
1540 ObjCTypes.SelectorPtrTy);
1541 Entry =
1542 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
1543 llvm::GlobalValue::InternalLinkage,
1544 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
1545 &CGM.getModule());
1546 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
1547 UsedGlobals.push_back(Entry);
1548 }
1549
1550 return Builder.CreateLoad(Entry, false, "tmp");
1551}
1552
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001553llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
1554 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001555
1556 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001557 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001558 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001559 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001560 llvm::GlobalValue::InternalLinkage,
1561 C, "\01L_OBJC_CLASS_NAME_",
1562 &CGM.getModule());
1563 Entry->setSection("__TEXT,__cstring,cstring_literals");
1564 UsedGlobals.push_back(Entry);
1565 }
1566
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001567 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001568}
1569
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001570llvm::Constant *CGObjCMac::GetMethodVarName(Selector Sel) {
1571 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
1572
1573 if (!Entry) {
1574 llvm::Constant *C = llvm::ConstantArray::get(Sel.getName());
1575 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001576 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001577 llvm::GlobalValue::InternalLinkage,
1578 C, "\01L_OBJC_METH_VAR_NAME_",
1579 &CGM.getModule());
1580 Entry->setSection("__TEXT,__cstring,cstring_literals");
1581 UsedGlobals.push_back(Entry);
1582 }
1583
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001584 return getConstantGEP(Entry, 0, 0);
1585}
1586
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001587// FIXME: Merge into a single cstring creation function.
1588llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) {
1589 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
1590}
1591
1592// FIXME: Merge into a single cstring creation function.
1593llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) {
1594 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
1595}
1596
1597llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) {
1598 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001599
1600 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001601 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001602 Entry =
1603 new llvm::GlobalVariable(C->getType(), false,
1604 llvm::GlobalValue::InternalLinkage,
1605 C, "\01L_OBJC_METH_VAR_TYPE_",
1606 &CGM.getModule());
1607 Entry->setSection("__TEXT,__cstring,cstring_literals");
1608 UsedGlobals.push_back(Entry);
1609 }
1610
1611 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001612}
1613
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001614// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001615llvm::Constant *CGObjCMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001616 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001617 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
1618 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001619 return GetMethodVarType(TypeStr);
1620}
1621
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001622// FIXME: Merge into a single cstring creation function.
1623llvm::Constant *CGObjCMac::GetPropertyName(IdentifierInfo *Ident) {
1624 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
1625
1626 if (!Entry) {
1627 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
1628 Entry =
1629 new llvm::GlobalVariable(C->getType(), false,
1630 llvm::GlobalValue::InternalLinkage,
1631 C, "\01L_OBJC_PROP_NAME_ATTR_",
1632 &CGM.getModule());
1633 Entry->setSection("__TEXT,__cstring,cstring_literals");
1634 UsedGlobals.push_back(Entry);
1635 }
1636
1637 return getConstantGEP(Entry, 0, 0);
1638}
1639
1640// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001641// FIXME: This Decl should be more precise.
1642llvm::Constant *CGObjCMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
1643 const Decl *Container) {
1644 std::string TypeStr;
1645 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001646 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
1647}
1648
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001649void CGObjCMac::GetNameForMethod(const ObjCMethodDecl *D,
1650 std::string &NameOut) {
1651 // FIXME: Find the mangling GCC uses.
1652 std::stringstream s;
1653 s << (D->isInstance() ? "-" : "+");
1654 s << "[";
1655 s << D->getClassInterface()->getName();
1656 s << " ";
1657 s << D->getSelector().getName();
1658 s << "]";
1659 NameOut = s.str();
1660}
1661
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001662void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001663 EmitModuleInfo();
1664
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001665 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001666
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001667 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001668 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001669 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001670 }
1671
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001672 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001673 llvm::GlobalValue *GV =
1674 new llvm::GlobalVariable(AT, false,
1675 llvm::GlobalValue::AppendingLinkage,
1676 llvm::ConstantArray::get(AT, Used),
1677 "llvm.used",
1678 &CGM.getModule());
1679
1680 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001681
1682 // Add assembler directives to add lazy undefined symbol references
1683 // for classes which are referenced but not defined. This is
1684 // important for correct linker interaction.
1685
1686 // FIXME: Uh, this isn't particularly portable.
1687 std::stringstream s;
1688 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
1689 e = LazySymbols.end(); i != e; ++i) {
1690 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
1691 }
1692 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
1693 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001694 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001695 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
1696 }
1697 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001698}
1699
1700/* *** */
1701
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001702ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Daniel Dunbar3e9df992008-08-23 18:37:06 +00001703 : CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001704{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001705 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1706 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001707
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001708 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001709 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001710 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001711 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1712
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001713 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
1714 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001715
1716 // FIXME: It would be nice to unify this with the opaque type, so
1717 // that the IR comes out a bit cleaner.
1718 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
1719 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001720
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001721 MethodDescriptionTy =
1722 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001723 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001724 NULL);
1725 CGM.getModule().addTypeName("struct._objc_method_description",
1726 MethodDescriptionTy);
1727
1728 MethodDescriptionListTy =
1729 llvm::StructType::get(IntTy,
1730 llvm::ArrayType::get(MethodDescriptionTy, 0),
1731 NULL);
1732 CGM.getModule().addTypeName("struct._objc_method_description_list",
1733 MethodDescriptionListTy);
1734 MethodDescriptionListPtrTy =
1735 llvm::PointerType::getUnqual(MethodDescriptionListTy);
1736
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001737 PropertyTy = llvm::StructType::get(Int8PtrTy,
1738 Int8PtrTy,
1739 NULL);
1740 CGM.getModule().addTypeName("struct._objc_property",
1741 PropertyTy);
1742
1743 PropertyListTy = llvm::StructType::get(IntTy,
1744 IntTy,
1745 llvm::ArrayType::get(PropertyTy, 0),
1746 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001747 CGM.getModule().addTypeName("struct._objc_property_list",
1748 PropertyListTy);
1749 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
1750
1751 // Protocol description structures
1752
1753 ProtocolExtensionTy =
1754 llvm::StructType::get(Types.ConvertType(Ctx.IntTy),
1755 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1756 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1757 PropertyListPtrTy,
1758 NULL);
1759 CGM.getModule().addTypeName("struct._objc_protocol_extension",
1760 ProtocolExtensionTy);
1761 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
1762
1763 // Handle recursive construction of Protocl and ProtocolList types
1764
1765 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
1766 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
1767
1768 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
1769 LongTy,
1770 llvm::ArrayType::get(ProtocolTyHolder, 0),
1771 NULL);
1772 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
1773
1774 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolExtensionTy),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001775 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001776 llvm::PointerType::getUnqual(ProtocolListTyHolder),
1777 MethodDescriptionListPtrTy,
1778 MethodDescriptionListPtrTy,
1779 NULL);
1780 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
1781
1782 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
1783 CGM.getModule().addTypeName("struct._objc_protocol_list",
1784 ProtocolListTy);
1785 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
1786
1787 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
1788 CGM.getModule().addTypeName("struct.__objc_protocol", ProtocolTy);
1789 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001790
1791 // Class description structures
1792
1793 IvarTy = llvm::StructType::get(Int8PtrTy,
1794 Int8PtrTy,
1795 IntTy,
1796 NULL);
1797 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
1798
1799 IvarListTy = llvm::OpaqueType::get();
1800 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
1801 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
1802
1803 MethodTy = llvm::StructType::get(SelectorPtrTy,
1804 Int8PtrTy,
1805 Int8PtrTy,
1806 NULL);
1807 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
1808
1809 MethodListTy = llvm::OpaqueType::get();
1810 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
1811 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
1812
1813 CacheTy = llvm::OpaqueType::get();
1814 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
1815 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
1816
1817 ClassExtensionTy =
1818 llvm::StructType::get(IntTy,
1819 Int8PtrTy,
1820 PropertyListPtrTy,
1821 NULL);
1822 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
1823 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
1824
1825 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
1826
1827 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
1828 llvm::PointerType::getUnqual(ClassTyHolder),
1829 Int8PtrTy,
1830 LongTy,
1831 LongTy,
1832 LongTy,
1833 IvarListPtrTy,
1834 MethodListPtrTy,
1835 CachePtrTy,
1836 ProtocolListPtrTy,
1837 Int8PtrTy,
1838 ClassExtensionPtrTy,
1839 NULL);
1840 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
1841
1842 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
1843 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
1844 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
1845
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001846 CategoryTy = llvm::StructType::get(Int8PtrTy,
1847 Int8PtrTy,
1848 MethodListPtrTy,
1849 MethodListPtrTy,
1850 ProtocolListPtrTy,
1851 IntTy,
1852 PropertyListPtrTy,
1853 NULL);
1854 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
1855
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001856 // I'm not sure I like this. The implicit coordination is a bit
1857 // gross. We should solve this in a reasonable fashion because this
1858 // is a pretty common task (match some runtime data structure with
1859 // an LLVM data structure).
1860
1861 // FIXME: This is leaked.
1862 // FIXME: Merge with rewriter code?
1863 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
1864 SourceLocation(),
1865 &Ctx.Idents.get("_objc_super"), 0);
1866 FieldDecl *FieldDecls[2];
1867 FieldDecls[0] = FieldDecl::Create(Ctx, SourceLocation(), 0,
1868 Ctx.getObjCIdType());
1869 FieldDecls[1] = FieldDecl::Create(Ctx, SourceLocation(), 0,
1870 Ctx.getObjCClassType());
1871 RD->defineBody(FieldDecls, 2);
1872
1873 SuperCTy = Ctx.getTagDeclType(RD);
1874 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
1875
1876 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001877 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001878
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001879 // Global metadata structures
1880
1881 SymtabTy = llvm::StructType::get(LongTy,
1882 SelectorPtrTy,
1883 ShortTy,
1884 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001885 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001886 NULL);
1887 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
1888 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
1889
1890 ModuleTy =
1891 llvm::StructType::get(LongTy,
1892 LongTy,
1893 Int8PtrTy,
1894 SymtabPtrTy,
1895 NULL);
1896 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001897
1898 // Message send functions
1899
1900 std::vector<const llvm::Type*> Params;
1901 Params.push_back(ObjectPtrTy);
1902 Params.push_back(SelectorPtrTy);
1903 MessageSendFn = llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1904 Params,
1905 true),
1906 llvm::Function::ExternalLinkage,
1907 "objc_msgSend",
1908 &CGM.getModule());
1909
1910 Params.clear();
1911 Params.push_back(Int8PtrTy);
1912 Params.push_back(ObjectPtrTy);
1913 Params.push_back(SelectorPtrTy);
1914 MessageSendStretFn =
1915 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1916 Params,
1917 true),
1918 llvm::Function::ExternalLinkage,
1919 "objc_msgSend_stret",
1920 &CGM.getModule());
1921
1922 Params.clear();
1923 Params.push_back(SuperPtrTy);
1924 Params.push_back(SelectorPtrTy);
1925 MessageSendSuperFn =
1926 llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1927 Params,
1928 true),
1929 llvm::Function::ExternalLinkage,
1930 "objc_msgSendSuper",
1931 &CGM.getModule());
1932
1933 Params.clear();
1934 Params.push_back(Int8PtrTy);
1935 Params.push_back(SuperPtrTy);
1936 Params.push_back(SelectorPtrTy);
1937 MessageSendSuperStretFn =
1938 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1939 Params,
1940 true),
1941 llvm::Function::ExternalLinkage,
1942 "objc_msgSendSuper_stret",
1943 &CGM.getModule());
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001944
1945 // Enumeration mutation.
1946
1947 Params.clear();
1948 Params.push_back(ObjectPtrTy);
1949 EnumerationMutationFn =
1950 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1951 Params,
1952 false),
1953 llvm::Function::ExternalLinkage,
1954 "objc_enumerationMutation",
1955 &CGM.getModule());
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001956}
1957
1958ObjCTypesHelper::~ObjCTypesHelper() {
1959}
1960
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001961llvm::Value *ObjCTypesHelper::getMessageSendFn(bool IsSuper,
1962 const llvm::Type *ReturnTy) {
1963 llvm::Function *F;
1964 llvm::FunctionType *CallFTy;
1965
1966 // FIXME: Should we be caching any of this?
1967 if (!ReturnTy->isSingleValueType()) {
1968 F = IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
1969 std::vector<const llvm::Type*> Params(3);
1970 Params[0] = llvm::PointerType::getUnqual(ReturnTy);
1971 Params[1] = IsSuper ? SuperPtrTy : ObjectPtrTy;
1972 Params[2] = SelectorPtrTy;
1973 CallFTy = llvm::FunctionType::get(llvm::Type::VoidTy, Params, true);
Daniel Dunbar662174c82008-08-29 17:28:43 +00001974 } else { // FIXME: floating point?
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001975 F = IsSuper ? MessageSendSuperFn : MessageSendFn;
1976 std::vector<const llvm::Type*> Params(2);
1977 Params[0] = IsSuper ? SuperPtrTy : ObjectPtrTy;
1978 Params[1] = SelectorPtrTy;
1979 CallFTy = llvm::FunctionType::get(ReturnTy, Params, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001980 }
1981
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001982 return llvm::ConstantExpr::getBitCast(F,
1983 llvm::PointerType::getUnqual(CallFTy));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001984}
1985
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001986/* *** */
1987
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001988CodeGen::CGObjCRuntime *
1989CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001990 return new CGObjCMac(CGM);
1991}