blob: 3b4309d8941b6c75789253aeac1a415ee08bea77 [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
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000133public:
134 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
135 ~ObjCTypesHelper();
136
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000137 llvm::Value *getMessageSendFn(bool IsSuper, const llvm::Type *ReturnTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000138};
139
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000140class CGObjCMac : public CodeGen::CGObjCRuntime {
141private:
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000142 CodeGen::CodeGenModule &CGM;
143 ObjCTypesHelper ObjCTypes;
144 /// ObjCABI - FIXME: Not sure yet.
145 unsigned ObjCABI;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000146
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000147 /// LazySymbols - Symbols to generate a lazy reference for. See
148 /// DefinedSymbols and FinishModule().
149 std::set<IdentifierInfo*> LazySymbols;
150
151 /// DefinedSymbols - External symbols which are defined by this
152 /// module. The symbols in this list and LazySymbols are used to add
153 /// special linker symbols which ensure that Objective-C modules are
154 /// linked properly.
155 std::set<IdentifierInfo*> DefinedSymbols;
156
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000157 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000158 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000159
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000160 /// MethodVarNames - uniqued method variable names.
161 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
162
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000163 /// MethodVarTypes - uniqued method type signatures. We have to use
164 /// a StringMap here because have no other unique reference.
165 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
166
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000167 /// MethodDefinitions - map of methods which have been defined in
168 /// this translation unit.
169 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
170
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000171 /// PropertyNames - uniqued method variable names.
172 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
173
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000174 /// ClassReferences - uniqued class references.
175 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
176
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000177 /// SelectorReferences - uniqued selector references.
178 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
179
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000180 /// Protocols - Protocols for which an objc_protocol structure has
181 /// been emitted. Forward declarations are handled by creating an
182 /// empty structure whose initializer is filled in when/if defined.
183 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
184
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000185 /// DefinedClasses - List of defined classes.
186 std::vector<llvm::GlobalValue*> DefinedClasses;
187
188 /// DefinedCategories - List of defined categories.
189 std::vector<llvm::GlobalValue*> DefinedCategories;
190
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000191 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000192 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000193 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000194
195 /// EmitImageInfo - Emit the image info marker used to encode some module
196 /// level information.
197 void EmitImageInfo();
198
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000199 /// EmitModuleInfo - Another marker encoding module level
200 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000201 void EmitModuleInfo();
202
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000203 /// EmitModuleSymols - Emit module symbols, the list of defined
204 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000205 llvm::Constant *EmitModuleSymbols();
206
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000207 /// FinishModule - Write out global data structures at the end of
208 /// processing a translation unit.
209 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000210
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000211 /// EmitClassExtension - Generate the class extension structure used
212 /// to store the weak ivar layout and properties. The return value
213 /// has type ClassExtensionPtrTy.
214 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
215
216 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
217 /// for the given class.
218 llvm::Value *EmitClassRef(llvm::IRBuilder<> &Builder,
219 const ObjCInterfaceDecl *ID);
220
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000221 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
222 const ObjCMessageExpr *E,
223 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000224 QualType Arg0Ty,
225 bool IsSuper,
226 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000227
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000228 /// EmitIvarList - Emit the ivar list for the given
229 /// implementation. If ForClass is true the list of class ivars
230 /// (i.e. metaclass ivars) is emitted, otherwise the list of
231 /// interface ivars will be emitted. The return value has type
232 /// IvarListPtrTy.
233 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
234 bool ForClass,
235 const llvm::Type *InterfaceTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000236
237 /// EmitMetaClass - Emit a forward reference to the class structure
238 /// for the metaclass of the given interface. The return value has
239 /// type ClassPtrTy.
240 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
241
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000242 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000243 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000244 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
245 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000246 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000247 const ConstantVector &Methods);
248
249 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
250
251 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000252
253 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000254 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000255 llvm::Constant *EmitMethodList(const std::string &Name,
256 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000257 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000258
259 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000260 /// method declarations.
261 /// - TypeName: The name for the type containing the methods.
262 /// - IsProtocol: True iff these methods are for a protocol.
263 /// - ClassMethds: True iff these are class methods.
264 /// - Required: When true, only "required" methods are
265 /// listed. Similarly, when false only "optional" methods are
266 /// listed. For classes this should always be true.
267 /// - begin, end: The method list to output.
268 ///
269 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000270 llvm::Constant *EmitMethodDescList(const std::string &Name,
271 const char *Section,
272 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000273
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000274 /// EmitPropertyList - Emit the given property list. The return
275 /// value has type PropertyListPtrTy.
276 llvm::Constant *EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000277 const Decl *Container,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000278 ObjCPropertyDecl * const *begin,
279 ObjCPropertyDecl * const *end);
280
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000281 /// EmitProtocolExtension - Generate the protocol extension
282 /// structure used to store optional instance and class methods, and
283 /// protocol properties. The return value has type
284 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000285 llvm::Constant *
286 EmitProtocolExtension(const ObjCProtocolDecl *PD,
287 const ConstantVector &OptInstanceMethods,
288 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000289
290 /// EmitProtocolList - Generate the list of referenced
291 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000292 llvm::Constant *EmitProtocolList(const std::string &Name,
293 ObjCProtocolDecl::protocol_iterator begin,
294 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000295
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000296 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
297 /// for the given selector.
298 llvm::Value *EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel);
299
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000300 /// GetProtocolRef - Return a reference to the internal protocol
301 /// description, creating an empty one if it has not been
302 /// defined. The return value has type pointer-to ProtocolTy.
303 llvm::GlobalVariable *GetProtocolRef(const ObjCProtocolDecl *PD);
304
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000305 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000306 /// name. The return value has type char *.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000307 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000308
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000309 /// GetMethodVarName - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000310 /// selector's name. The return value has type char *.
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000311 llvm::Constant *GetMethodVarName(Selector Sel);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000312 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000313 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000314
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000315 /// GetMethodVarType - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000316 /// selector's name. The return value has type char *.
317
318 // FIXME: This is a horrible name.
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000319 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000320 llvm::Constant *GetMethodVarType(const std::string &Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000321
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000322 /// GetPropertyName - Return a unique constant for the given
323 /// name. The return value has type char *.
324 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
325
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000326 // FIXME: This can be dropped once string functions are unified.
327 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
328 const Decl *Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000329
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000330 /// GetNameForMethod - Return a name for the given method.
331 /// \param[out] NameOut - The return value.
332 void GetNameForMethod(const ObjCMethodDecl *OMD,
333 std::string &NameOut);
334
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000335public:
336 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000337 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000338
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000339 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
340 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000341 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000342 bool IsClassMessage,
343 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000344
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000345 virtual CodeGen::RValue
346 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
347 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000348 const ObjCInterfaceDecl *Class,
349 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000350 bool IsClassMessage,
351 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000352
353 virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000354 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000355
356 virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
357
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000358 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000359
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000360 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000361
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000362 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000363
364 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000365 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000366
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000367 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000368
369 virtual llvm::Function *ModuleInitFunction();
370};
371} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000372
373/* *** Helper Functions *** */
374
375/// getConstantGEP() - Help routine to construct simple GEPs.
376static llvm::Constant *getConstantGEP(llvm::Constant *C,
377 unsigned idx0,
378 unsigned idx1) {
379 llvm::Value *Idxs[] = {
380 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
381 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
382 };
383 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
384}
385
386/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000387
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000388CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm)
389 : CGM(cgm),
390 ObjCTypes(cgm),
391 ObjCABI(1)
392{
393 // FIXME: How does this get set in GCC? And what does it even mean?
394 if (ObjCTypes.LongTy != CGM.getTypes().ConvertType(CGM.getContext().IntTy))
395 ObjCABI = 2;
396
397 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000398}
399
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000400/// GetClass - Return a reference to the class for the given interface
401/// decl.
402llvm::Value *CGObjCMac::GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000403 const ObjCInterfaceDecl *ID) {
404 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000405}
406
407/// GetSelector - Return the pointer to the unique'd string for this selector.
408llvm::Value *CGObjCMac::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000409 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000410}
411
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000412/// Generate a constant CFString object.
413/*
414 struct __builtin_CFString {
415 const int *isa; // point to __CFConstantStringClassReference
416 int flags;
417 const char *str;
418 long length;
419 };
420*/
421
422llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000423 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000424}
425
426/// Generates a message send where the super is the receiver. This is
427/// a message send to self with special delivery semantics indicating
428/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000429CodeGen::RValue
430CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
431 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000432 const ObjCInterfaceDecl *Class,
433 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000434 bool IsClassMessage,
435 const CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000436 // Create and init a super structure; this is a (receiver, class)
437 // pair we will pass to objc_msgSendSuper.
438 llvm::Value *ObjCSuper =
439 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
440 llvm::Value *ReceiverAsObject =
441 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
442 CGF.Builder.CreateStore(ReceiverAsObject,
443 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000444
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000445 // If this is a class message the metaclass is passed as the target.
446 llvm::Value *Target;
447 if (IsClassMessage) {
448 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
449 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
450 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
451 Target = Super;
452 } else {
453 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
454 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000455 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
456 // and ObjCTypes types.
457 const llvm::Type *ClassTy =
458 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
459 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000460 CGF.Builder.CreateStore(Target,
461 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
462
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000463 return EmitMessageSend(CGF, E,
464 ObjCSuper, ObjCTypes.SuperPtrCTy,
465 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000466}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000467
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000468/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000469CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
470 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000471 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000472 bool IsClassMessage,
473 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000474 llvm::Value *Arg0 =
475 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000476 return EmitMessageSend(CGF, E,
477 Arg0, CGF.getContext().getObjCIdType(),
478 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000479}
480
481CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
482 const ObjCMessageExpr *E,
483 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000484 QualType Arg0Ty,
485 bool IsSuper,
486 const CallArgList &CallArgs) {
487 CallArgList ActualArgs;
488 ActualArgs.push_back(std::make_pair(Arg0, Arg0Ty));
489 ActualArgs.push_back(std::make_pair(EmitSelector(CGF.Builder,
490 E->getSelector()),
491 CGF.getContext().getObjCSelType()));
492 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000493
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000494 // FIXME: This is a hack, we are implicitly coordinating with
495 // EmitCallExprExt, which will move the return type to the first
496 // parameter and set the structure return flag. See
497 // getMessageSendFn().
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000498
499 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(E->getType());
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000500 return CGF.EmitCall(ObjCTypes.getMessageSendFn(IsSuper, ReturnTy),
501 E->getType(),
502 ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000503}
504
505llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000506 const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000507 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
508 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000509}
510
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000511/*
512 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
513 struct _objc_protocol {
514 struct _objc_protocol_extension *isa;
515 char *protocol_name;
516 struct _objc_protocol_list *protocol_list;
517 struct _objc__method_prototype_list *instance_methods;
518 struct _objc__method_prototype_list *class_methods
519 };
520
521 See EmitProtocolExtension().
522*/
523void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000524 // FIXME: I don't understand why gcc generates this, or where it is
525 // resolved. Investigate. Its also wasteful to look this up over and
526 // over.
527 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
528
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000529 const char *ProtocolName = PD->getName();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000530
531 // Construct method lists.
532 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
533 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
534 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
535 e = PD->instmeth_end(); i != e; ++i) {
536 ObjCMethodDecl *MD = *i;
537 llvm::Constant *C = GetMethodDescriptionConstant(MD);
538 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
539 OptInstanceMethods.push_back(C);
540 } else {
541 InstanceMethods.push_back(C);
542 }
543 }
544
545 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
546 e = PD->classmeth_end(); i != e; ++i) {
547 ObjCMethodDecl *MD = *i;
548 llvm::Constant *C = GetMethodDescriptionConstant(MD);
549 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
550 OptClassMethods.push_back(C);
551 } else {
552 ClassMethods.push_back(C);
553 }
554 }
555
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000556 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000557 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000558 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000559 Values[2] =
560 EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(),
561 PD->protocol_begin(),
562 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000563 Values[3] =
564 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_")
565 + PD->getName(),
566 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
567 InstanceMethods);
568 Values[4] =
569 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_")
570 + PD->getName(),
571 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
572 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000573 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
574 Values);
575
576 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
577 if (Entry) {
578 // Already created, just update the initializer
579 Entry->setInitializer(Init);
580 } else {
581 Entry =
582 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
583 llvm::GlobalValue::InternalLinkage,
584 Init,
585 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
586 &CGM.getModule());
587 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
588 UsedGlobals.push_back(Entry);
589 // FIXME: Is this necessary? Why only for protocol?
590 Entry->setAlignment(4);
591 }
592}
593
594llvm::GlobalVariable *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
595 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
596
597 if (!Entry) {
598 std::vector<llvm::Constant*> Values(5);
599 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
600 Values[1] = GetClassName(PD->getIdentifier());
601 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
602 Values[3] = Values[4] =
603 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
604 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
605 Values);
606
607 Entry =
608 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
609 llvm::GlobalValue::InternalLinkage,
610 Init,
611 std::string("\01L_OBJC_PROTOCOL_")+PD->getName(),
612 &CGM.getModule());
613 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
614 UsedGlobals.push_back(Entry);
615 // FIXME: Is this necessary? Why only for protocol?
616 Entry->setAlignment(4);
617 }
618
619 return Entry;
620}
621
622/*
623 struct _objc_protocol_extension {
624 uint32_t size;
625 struct objc_method_description_list *optional_instance_methods;
626 struct objc_method_description_list *optional_class_methods;
627 struct objc_property_list *instance_properties;
628 };
629*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000630llvm::Constant *
631CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
632 const ConstantVector &OptInstanceMethods,
633 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000634 uint64_t Size =
635 CGM.getTargetData().getABITypeSize(ObjCTypes.ProtocolExtensionTy);
636 std::vector<llvm::Constant*> Values(4);
637 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000638 Values[1] =
639 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_")
640 + PD->getName(),
641 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
642 OptInstanceMethods);
643 Values[2] =
644 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_")
645 + PD->getName(),
646 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
647 OptClassMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000648 Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") +
649 PD->getName(),
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000650 0,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000651 PD->classprop_begin(),
652 PD->classprop_end());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000653
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000654 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000655 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
656 Values[3]->isNullValue())
657 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
658
659 llvm::Constant *Init =
660 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
661 llvm::GlobalVariable *GV =
662 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
663 llvm::GlobalValue::InternalLinkage,
664 Init,
665 (std::string("\01L_OBJC_PROTOCOLEXT_") +
666 PD->getName()),
667 &CGM.getModule());
668 // No special section, but goes in llvm.used
669 UsedGlobals.push_back(GV);
670
671 return GV;
672}
673
674/*
675 struct objc_protocol_list {
676 struct objc_protocol_list *next;
677 long count;
678 Protocol *list[];
679 };
680*/
Daniel Dunbardbc933702008-08-21 21:57:41 +0000681llvm::Constant *
682CGObjCMac::EmitProtocolList(const std::string &Name,
683 ObjCProtocolDecl::protocol_iterator begin,
684 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000685 std::vector<llvm::Constant*> ProtocolRefs;
686
Daniel Dunbardbc933702008-08-21 21:57:41 +0000687 for (; begin != end; ++begin)
688 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000689
690 // Just return null for empty protocol lists
691 if (ProtocolRefs.empty())
692 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
693
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000694 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000695 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
696
697 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000698 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000699 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
700 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
701 Values[2] =
702 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
703 ProtocolRefs.size()),
704 ProtocolRefs);
705
706 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
707 llvm::GlobalVariable *GV =
708 new llvm::GlobalVariable(Init->getType(), false,
709 llvm::GlobalValue::InternalLinkage,
710 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +0000711 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000712 &CGM.getModule());
713 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
714 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
715}
716
717/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000718 struct _objc_property {
719 const char * const name;
720 const char * const attributes;
721 };
722
723 struct _objc_property_list {
724 uint32_t entsize; // sizeof (struct _objc_property)
725 uint32_t prop_count;
726 struct _objc_property[prop_count];
727 };
728*/
729llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000730 const Decl *Container,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000731 ObjCPropertyDecl * const *begin,
732 ObjCPropertyDecl * const *end) {
733 std::vector<llvm::Constant*> Properties, Prop(2);
734 for (; begin != end; ++begin) {
735 const ObjCPropertyDecl *PD = *begin;
736 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000737 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000738 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
739 Prop));
740 }
741
742 // Return null for empty list.
743 if (Properties.empty())
744 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
745
746 unsigned PropertySize =
747 CGM.getTargetData().getABITypeSize(ObjCTypes.PropertyTy);
748 std::vector<llvm::Constant*> Values(3);
749 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
750 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
751 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
752 Properties.size());
753 Values[2] = llvm::ConstantArray::get(AT, Properties);
754 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
755
756 llvm::GlobalVariable *GV =
757 new llvm::GlobalVariable(Init->getType(), false,
758 llvm::GlobalValue::InternalLinkage,
759 Init,
760 Name,
761 &CGM.getModule());
762 // No special section on property lists?
763 UsedGlobals.push_back(GV);
764 return llvm::ConstantExpr::getBitCast(GV,
765 ObjCTypes.PropertyListPtrTy);
766
767}
768
769/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000770 struct objc_method_description_list {
771 int count;
772 struct objc_method_description list[];
773 };
774*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000775llvm::Constant *
776CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
777 std::vector<llvm::Constant*> Desc(2);
778 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
779 ObjCTypes.SelectorPtrTy);
780 Desc[1] = GetMethodVarType(MD);
781 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
782 Desc);
783}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000784
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000785llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
786 const char *Section,
787 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000788 // Return null for empty list.
789 if (Methods.empty())
790 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
791
792 std::vector<llvm::Constant*> Values(2);
793 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
794 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
795 Methods.size());
796 Values[1] = llvm::ConstantArray::get(AT, Methods);
797 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
798
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000799 llvm::GlobalVariable *GV =
800 new llvm::GlobalVariable(Init->getType(), false,
801 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000802 Init, Name, &CGM.getModule());
803 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000804 UsedGlobals.push_back(GV);
805 return llvm::ConstantExpr::getBitCast(GV,
806 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000807}
808
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000809/*
810 struct _objc_category {
811 char *category_name;
812 char *class_name;
813 struct _objc_method_list *instance_methods;
814 struct _objc_method_list *class_methods;
815 struct _objc_protocol_list *protocols;
816 uint32_t size; // <rdar://4585769>
817 struct _objc_property_list *instance_properties;
818 };
819 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000820void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000821 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.CategoryTy);
822
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000823 // FIXME: This is poor design, the OCD should have a pointer to the
824 // category decl. Additionally, note that Category can be null for
825 // the @implementation w/o an @interface case. Sema should just
826 // create one for us as it does for @implementation so everyone else
827 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000828 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000829 const ObjCCategoryDecl *Category =
830 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000831 std::string ExtName(std::string(Interface->getName()) +
832 "_" +
833 OCD->getName());
834
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000835 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
836 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
837 e = OCD->instmeth_end(); i != e; ++i) {
838 // Instance methods should always be defined.
839 InstanceMethods.push_back(GetMethodConstant(*i));
840 }
841 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
842 e = OCD->classmeth_end(); i != e; ++i) {
843 // Class methods should always be defined.
844 ClassMethods.push_back(GetMethodConstant(*i));
845 }
846
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000847 std::vector<llvm::Constant*> Values(7);
848 Values[0] = GetClassName(OCD->getIdentifier());
849 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000850 Values[2] =
851 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
852 ExtName,
853 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000854 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000855 Values[3] =
856 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
857 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000858 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000859 if (Category) {
860 Values[4] =
861 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
862 Category->protocol_begin(),
863 Category->protocol_end());
864 } else {
865 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
866 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000867 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000868
869 // If there is no category @interface then there can be no properties.
870 if (Category) {
871 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000872 OCD,
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000873 Category->classprop_begin(),
874 Category->classprop_end());
875 } else {
876 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
877 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000878
879 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
880 Values);
881
882 llvm::GlobalVariable *GV =
883 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
884 llvm::GlobalValue::InternalLinkage,
885 Init,
886 std::string("\01L_OBJC_CATEGORY_")+ExtName,
887 &CGM.getModule());
888 GV->setSection("__OBJC,__category,regular,no_dead_strip");
889 UsedGlobals.push_back(GV);
890 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000891}
892
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000893// FIXME: Get from somewhere?
894enum ClassFlags {
895 eClassFlags_Factory = 0x00001,
896 eClassFlags_Meta = 0x00002,
897 // <rdr://5142207>
898 eClassFlags_HasCXXStructors = 0x02000,
899 eClassFlags_Hidden = 0x20000,
900 eClassFlags_ABI2_Hidden = 0x00010,
901 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
902};
903
904// <rdr://5142207&4705298&4843145>
905static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
906 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
907 // FIXME: Support -fvisibility
908 switch (attr->getVisibility()) {
909 default:
910 assert(0 && "Unknown visibility");
911 return false;
912 case VisibilityAttr::DefaultVisibility:
913 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
914 return false;
915 case VisibilityAttr::HiddenVisibility:
916 return true;
917 }
918 } else {
919 return false; // FIXME: Support -fvisibility
920 }
921}
922
923/*
924 struct _objc_class {
925 Class isa;
926 Class super_class;
927 const char *name;
928 long version;
929 long info;
930 long instance_size;
931 struct _objc_ivar_list *ivars;
932 struct _objc_method_list *methods;
933 struct _objc_cache *cache;
934 struct _objc_protocol_list *protocols;
935 // Objective-C 1.0 extensions (<rdr://4585769>)
936 const char *ivar_layout;
937 struct _objc_class_ext *ext;
938 };
939
940 See EmitClassExtension();
941 */
942void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000943 DefinedSymbols.insert(ID->getIdentifier());
944
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000945 const char *ClassName = ID->getName();
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000946 // FIXME: Gross
947 ObjCInterfaceDecl *Interface =
948 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000949 llvm::Constant *Protocols =
950 EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(),
951 Interface->protocol_begin(),
952 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000953 const llvm::Type *InterfaceTy =
954 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
955 unsigned Flags = eClassFlags_Factory;
956 unsigned Size = CGM.getTargetData().getABITypeSize(InterfaceTy);
957
958 // FIXME: Set CXX-structors flag.
959 if (IsClassHidden(ID->getClassInterface()))
960 Flags |= eClassFlags_Hidden;
961
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000962 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
963 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
964 e = ID->instmeth_end(); i != e; ++i) {
965 // Instance methods should always be defined.
966 InstanceMethods.push_back(GetMethodConstant(*i));
967 }
968 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
969 e = ID->classmeth_end(); i != e; ++i) {
970 // Class methods should always be defined.
971 ClassMethods.push_back(GetMethodConstant(*i));
972 }
973
974 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
975 e = ID->propimpl_end(); i != e; ++i) {
976 ObjCPropertyImplDecl *PID = *i;
977
978 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
979 ObjCPropertyDecl *PD = PID->getPropertyDecl();
980
981 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
982 if (llvm::Constant *C = GetMethodConstant(MD))
983 InstanceMethods.push_back(C);
984 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
985 if (llvm::Constant *C = GetMethodConstant(MD))
986 InstanceMethods.push_back(C);
987 }
988 }
989
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000990 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000991 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000992 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000993 // Record a reference to the super class.
994 LazySymbols.insert(Super->getIdentifier());
995
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000996 Values[ 1] =
997 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
998 ObjCTypes.ClassPtrTy);
999 } else {
1000 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1001 }
1002 Values[ 2] = GetClassName(ID->getIdentifier());
1003 // Version is always 0.
1004 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1005 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1006 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1007 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001008 Values[ 7] =
1009 EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(),
1010 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001011 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001012 // cache is always NULL.
1013 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1014 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001015 // FIXME: Set ivar_layout
1016 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001017 Values[11] = EmitClassExtension(ID);
1018 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1019 Values);
1020
1021 llvm::GlobalVariable *GV =
1022 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1023 llvm::GlobalValue::InternalLinkage,
1024 Init,
1025 std::string("\01L_OBJC_CLASS_")+ClassName,
1026 &CGM.getModule());
1027 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1028 UsedGlobals.push_back(GV);
1029 // FIXME: Why?
1030 GV->setAlignment(32);
1031 DefinedClasses.push_back(GV);
1032}
1033
1034llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1035 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001036 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001037 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001038 const char *ClassName = ID->getName();
1039 unsigned Flags = eClassFlags_Meta;
1040 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ClassTy);
1041
1042 if (IsClassHidden(ID->getClassInterface()))
1043 Flags |= eClassFlags_Hidden;
1044
1045 std::vector<llvm::Constant*> Values(12);
1046 // The isa for the metaclass is the root of the hierarchy.
1047 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1048 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1049 Root = Super;
1050 Values[ 0] =
1051 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1052 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001053 // The super class for the metaclass is emitted as the name of the
1054 // super class. The runtime fixes this up to point to the
1055 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001056 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1057 Values[ 1] =
1058 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1059 ObjCTypes.ClassPtrTy);
1060 } else {
1061 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1062 }
1063 Values[ 2] = GetClassName(ID->getIdentifier());
1064 // Version is always 0.
1065 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1066 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1067 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1068 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001069 Values[ 7] =
1070 EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(),
1071 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001072 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001073 // cache is always NULL.
1074 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1075 Values[ 9] = Protocols;
1076 // ivar_layout for metaclass is always NULL.
1077 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1078 // The class extension is always unused for metaclasses.
1079 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1080 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1081 Values);
1082
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001083 std::string Name("\01L_OBJC_METACLASS_");
1084 Name += ClassName;
1085
1086 // Check for a forward reference.
1087 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1088 if (GV) {
1089 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1090 "Forward metaclass reference has incorrect type.");
1091 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1092 GV->setInitializer(Init);
1093 } else {
1094 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1095 llvm::GlobalValue::InternalLinkage,
1096 Init, Name,
1097 &CGM.getModule());
1098 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001099 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1100 UsedGlobals.push_back(GV);
1101 // FIXME: Why?
1102 GV->setAlignment(32);
1103
1104 return GV;
1105}
1106
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001107llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
1108 std::string Name("\01L_OBJC_METACLASS_");
1109 Name += ID->getName();
1110
1111 // FIXME: Should we look these up somewhere other than the
1112 // module. Its a bit silly since we only generate these while
1113 // processing an implementation, so exactly one pointer would work
1114 // if know when we entered/exitted an implementation block.
1115
1116 // Check for an existing forward reference.
1117 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name)) {
1118 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1119 "Forward metaclass reference has incorrect type.");
1120 return GV;
1121 } else {
1122 // Generate as an external reference to keep a consistent
1123 // module. This will be patched up when we emit the metaclass.
1124 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1125 llvm::GlobalValue::ExternalLinkage,
1126 0,
1127 Name,
1128 &CGM.getModule());
1129 }
1130}
1131
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001132/*
1133 struct objc_class_ext {
1134 uint32_t size;
1135 const char *weak_ivar_layout;
1136 struct _objc_property_list *properties;
1137 };
1138*/
1139llvm::Constant *
1140CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1141 uint64_t Size =
1142 CGM.getTargetData().getABITypeSize(ObjCTypes.ClassExtensionTy);
1143
1144 std::vector<llvm::Constant*> Values(3);
1145 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001146 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001147 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001148 Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") +
1149 ID->getName(),
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001150 ID,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001151 ID->getClassInterface()->classprop_begin(),
1152 ID->getClassInterface()->classprop_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001153
1154 // Return null if no extension bits are used.
1155 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1156 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1157
1158 llvm::Constant *Init =
1159 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1160 llvm::GlobalVariable *GV =
1161 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1162 llvm::GlobalValue::InternalLinkage,
1163 Init,
1164 (std::string("\01L_OBJC_CLASSEXT_") +
1165 ID->getName()),
1166 &CGM.getModule());
1167 // No special section, but goes in llvm.used
1168 UsedGlobals.push_back(GV);
1169
1170 return GV;
1171}
1172
1173/*
1174 struct objc_ivar {
1175 char *ivar_name;
1176 char *ivar_type;
1177 int ivar_offset;
1178 };
1179
1180 struct objc_ivar_list {
1181 int ivar_count;
1182 struct objc_ivar list[count];
1183 };
1184 */
1185llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1186 bool ForClass,
1187 const llvm::Type *InterfaceTy) {
1188 std::vector<llvm::Constant*> Ivars, Ivar(3);
1189
1190 // When emitting the root class GCC emits ivar entries for the
1191 // actual class structure. It is not clear if we need to follow this
1192 // behavior; for now lets try and get away with not doing it. If so,
1193 // the cleanest solution would be to make up an ObjCInterfaceDecl
1194 // for the class.
1195 if (ForClass)
1196 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1197
1198 const llvm::StructLayout *Layout =
1199 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
1200 for (ObjCInterfaceDecl::ivar_iterator
1201 i = ID->getClassInterface()->ivar_begin(),
1202 e = ID->getClassInterface()->ivar_end(); i != e; ++i) {
1203 ObjCIvarDecl *V = *i;
1204 unsigned Offset =
1205 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(V));
1206 std::string TypeStr;
1207 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1208 Ivar[0] = GetMethodVarName(V->getIdentifier());
1209 CGM.getContext().getObjCEncodingForType(V->getType(), TypeStr,
1210 EncodingRecordTypes);
1211 Ivar[1] = GetMethodVarType(TypeStr);
1212 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
1213 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy,
1214 Ivar));
1215 }
1216
1217 // Return null for empty list.
1218 if (Ivars.empty())
1219 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1220
1221 std::vector<llvm::Constant*> Values(2);
1222 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1223 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1224 Ivars.size());
1225 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1226 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1227
1228 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1229 "\01L_OBJC_INSTANCE_VARIABLES_");
1230 llvm::GlobalVariable *GV =
1231 new llvm::GlobalVariable(Init->getType(), false,
1232 llvm::GlobalValue::InternalLinkage,
1233 Init,
1234 std::string(Prefix) + ID->getName(),
1235 &CGM.getModule());
1236 if (ForClass) {
1237 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1238 // FIXME: Why is this only here?
1239 GV->setAlignment(32);
1240 } else {
1241 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1242 }
1243 UsedGlobals.push_back(GV);
1244 return llvm::ConstantExpr::getBitCast(GV,
1245 ObjCTypes.IvarListPtrTy);
1246}
1247
1248/*
1249 struct objc_method {
1250 SEL method_name;
1251 char *method_types;
1252 void *method;
1253 };
1254
1255 struct objc_method_list {
1256 struct objc_method_list *obsolete;
1257 int count;
1258 struct objc_method methods_list[count];
1259 };
1260*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001261
1262/// GetMethodConstant - Return a struct objc_method constant for the
1263/// given method if it has been defined. The result is null if the
1264/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001265llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001266 // FIXME: Use DenseMap::lookup
1267 llvm::Function *Fn = MethodDefinitions[MD];
1268 if (!Fn)
1269 return 0;
1270
1271 std::vector<llvm::Constant*> Method(3);
1272 Method[0] =
1273 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1274 ObjCTypes.SelectorPtrTy);
1275 Method[1] = GetMethodVarType(MD);
1276 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1277 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1278}
1279
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001280llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1281 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001282 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001283 // Return null for empty list.
1284 if (Methods.empty())
1285 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1286
1287 std::vector<llvm::Constant*> Values(3);
1288 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1289 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1290 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1291 Methods.size());
1292 Values[2] = llvm::ConstantArray::get(AT, Methods);
1293 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1294
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001295 llvm::GlobalVariable *GV =
1296 new llvm::GlobalVariable(Init->getType(), false,
1297 llvm::GlobalValue::InternalLinkage,
1298 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001299 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001300 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001301 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001302 UsedGlobals.push_back(GV);
1303 return llvm::ConstantExpr::getBitCast(GV,
1304 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001305}
1306
1307llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD) {
1308 const llvm::Type *ReturnTy =
1309 CGM.getTypes().ConvertReturnType(OMD->getResultType());
1310 const llvm::Type *SelfTy =
1311 CGM.getTypes().ConvertType(OMD->getSelfDecl()->getType());
1312
1313 std::vector<const llvm::Type*> ArgTys;
1314 ArgTys.reserve(1 + 2 + OMD->param_size());
1315
1316 // FIXME: This is not something we should have to be dealing with
1317 // here.
1318 bool useStructRet =
1319 CodeGen::CodeGenFunction::hasAggregateLLVMType(OMD->getResultType());
1320 if (useStructRet) {
1321 ArgTys.push_back(llvm::PointerType::getUnqual(ReturnTy));
1322 ReturnTy = llvm::Type::VoidTy;
1323 }
1324
1325 // Implicit arguments
1326 ArgTys.push_back(SelfTy);
1327 ArgTys.push_back(ObjCTypes.SelectorPtrTy);
1328
1329 for (ObjCMethodDecl::param_const_iterator
1330 i = OMD->param_begin(), e = OMD->param_end();
1331 i != e; ++i) {
1332 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001333 if (Ty->isSingleValueType()) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001334 ArgTys.push_back(Ty);
1335 } else {
1336 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
1337 }
1338 }
1339
1340 std::string Name;
1341 GetNameForMethod(OMD, Name);
1342
1343 llvm::Function *Method =
1344 llvm::Function::Create(llvm::FunctionType::get(ReturnTy,
1345 ArgTys,
1346 OMD->isVariadic()),
1347 llvm::GlobalValue::InternalLinkage,
1348 Name,
1349 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001350 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001351
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001352 unsigned Offset = 3; // Return plus self and selector implicit args.
1353 if (useStructRet) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001354 Method->addParamAttr(1, llvm::ParamAttr::StructRet);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001355 ++Offset;
1356 }
1357
1358 // FIXME: This is horrible, we need to be reusing the machinery in
1359 // CodeGenModule.cpp (SetFunctionAttributes).
1360 for (ObjCMethodDecl::param_const_iterator
1361 i = OMD->param_begin(), e = OMD->param_end();
1362 i != e; ++i, ++Offset) {
1363 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
1364 if (!Ty->isSingleValueType())
1365 Method->addParamAttr(Offset, llvm::ParamAttr::ByVal);
1366 }
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001367
1368 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001369}
1370
1371llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001372 // Abuse this interface function as a place to finalize.
1373 FinishModule();
1374
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001375 return NULL;
1376}
1377
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001378/* *** Private Interface *** */
1379
1380/// EmitImageInfo - Emit the image info marker used to encode some module
1381/// level information.
1382///
1383/// See: <rdr://4810609&4810587&4810587>
1384/// struct IMAGE_INFO {
1385/// unsigned version;
1386/// unsigned flags;
1387/// };
1388enum ImageInfoFlags {
1389 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
1390 eImageInfo_GarbageCollected = (1 << 1),
1391 eImageInfo_GCOnly = (1 << 2)
1392};
1393
1394void CGObjCMac::EmitImageInfo() {
1395 unsigned version = 0; // Version is unused?
1396 unsigned flags = 0;
1397
1398 // FIXME: Fix and continue?
1399 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
1400 flags |= eImageInfo_GarbageCollected;
1401 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
1402 flags |= eImageInfo_GCOnly;
1403
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001404 // Emitted as int[2];
1405 llvm::Constant *values[2] = {
1406 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
1407 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
1408 };
1409 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001410 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001411 new llvm::GlobalVariable(AT, true,
1412 llvm::GlobalValue::InternalLinkage,
1413 llvm::ConstantArray::get(AT, values, 2),
1414 "\01L_OBJC_IMAGE_INFO",
1415 &CGM.getModule());
1416
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001417 if (ObjCABI == 1) {
1418 GV->setSection("__OBJC, __image_info,regular");
1419 } else {
1420 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
1421 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001422
1423 UsedGlobals.push_back(GV);
1424}
1425
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001426
1427// struct objc_module {
1428// unsigned long version;
1429// unsigned long size;
1430// const char *name;
1431// Symtab symtab;
1432// };
1433
1434// FIXME: Get from somewhere
1435static const int ModuleVersion = 7;
1436
1437void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001438 uint64_t Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ModuleTy);
1439
1440 std::vector<llvm::Constant*> Values(4);
1441 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
1442 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001443 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001444 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001445 Values[3] = EmitModuleSymbols();
1446
1447 llvm::GlobalVariable *GV =
1448 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
1449 llvm::GlobalValue::InternalLinkage,
1450 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
1451 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001452 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001453 &CGM.getModule());
1454 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
1455 UsedGlobals.push_back(GV);
1456}
1457
1458llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001459 unsigned NumClasses = DefinedClasses.size();
1460 unsigned NumCategories = DefinedCategories.size();
1461
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001462 // Return null if no symbols were defined.
1463 if (!NumClasses && !NumCategories)
1464 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
1465
1466 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001467 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1468 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
1469 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
1470 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
1471
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001472 // The runtime expects exactly the list of defined classes followed
1473 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001474 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001475 for (unsigned i=0; i<NumClasses; i++)
1476 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
1477 ObjCTypes.Int8PtrTy);
1478 for (unsigned i=0; i<NumCategories; i++)
1479 Symbols[NumClasses + i] =
1480 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
1481 ObjCTypes.Int8PtrTy);
1482
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001483 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001484 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001485 NumClasses + NumCategories),
1486 Symbols);
1487
1488 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1489
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001490 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001491 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001492 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001493 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001494 "\01L_OBJC_SYMBOLS",
1495 &CGM.getModule());
1496 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
1497 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001498 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
1499}
1500
1501llvm::Value *CGObjCMac::EmitClassRef(llvm::IRBuilder<> &Builder,
1502 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001503 LazySymbols.insert(ID->getIdentifier());
1504
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001505 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
1506
1507 if (!Entry) {
1508 llvm::Constant *Casted =
1509 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
1510 ObjCTypes.ClassPtrTy);
1511 Entry =
1512 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
1513 llvm::GlobalValue::InternalLinkage,
1514 Casted, "\01L_OBJC_CLASS_REFERENCES_",
1515 &CGM.getModule());
1516 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
1517 UsedGlobals.push_back(Entry);
1518 }
1519
1520 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001521}
1522
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001523llvm::Value *CGObjCMac::EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
1524 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
1525
1526 if (!Entry) {
1527 llvm::Constant *Casted =
1528 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
1529 ObjCTypes.SelectorPtrTy);
1530 Entry =
1531 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
1532 llvm::GlobalValue::InternalLinkage,
1533 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
1534 &CGM.getModule());
1535 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
1536 UsedGlobals.push_back(Entry);
1537 }
1538
1539 return Builder.CreateLoad(Entry, false, "tmp");
1540}
1541
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001542llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
1543 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001544
1545 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001546 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001547 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001548 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001549 llvm::GlobalValue::InternalLinkage,
1550 C, "\01L_OBJC_CLASS_NAME_",
1551 &CGM.getModule());
1552 Entry->setSection("__TEXT,__cstring,cstring_literals");
1553 UsedGlobals.push_back(Entry);
1554 }
1555
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001556 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001557}
1558
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001559llvm::Constant *CGObjCMac::GetMethodVarName(Selector Sel) {
1560 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
1561
1562 if (!Entry) {
1563 llvm::Constant *C = llvm::ConstantArray::get(Sel.getName());
1564 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001565 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001566 llvm::GlobalValue::InternalLinkage,
1567 C, "\01L_OBJC_METH_VAR_NAME_",
1568 &CGM.getModule());
1569 Entry->setSection("__TEXT,__cstring,cstring_literals");
1570 UsedGlobals.push_back(Entry);
1571 }
1572
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001573 return getConstantGEP(Entry, 0, 0);
1574}
1575
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001576// FIXME: Merge into a single cstring creation function.
1577llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) {
1578 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
1579}
1580
1581// FIXME: Merge into a single cstring creation function.
1582llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) {
1583 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
1584}
1585
1586llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) {
1587 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001588
1589 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001590 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001591 Entry =
1592 new llvm::GlobalVariable(C->getType(), false,
1593 llvm::GlobalValue::InternalLinkage,
1594 C, "\01L_OBJC_METH_VAR_TYPE_",
1595 &CGM.getModule());
1596 Entry->setSection("__TEXT,__cstring,cstring_literals");
1597 UsedGlobals.push_back(Entry);
1598 }
1599
1600 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001601}
1602
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001603// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001604llvm::Constant *CGObjCMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001605 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001606 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
1607 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001608 return GetMethodVarType(TypeStr);
1609}
1610
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001611// FIXME: Merge into a single cstring creation function.
1612llvm::Constant *CGObjCMac::GetPropertyName(IdentifierInfo *Ident) {
1613 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
1614
1615 if (!Entry) {
1616 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
1617 Entry =
1618 new llvm::GlobalVariable(C->getType(), false,
1619 llvm::GlobalValue::InternalLinkage,
1620 C, "\01L_OBJC_PROP_NAME_ATTR_",
1621 &CGM.getModule());
1622 Entry->setSection("__TEXT,__cstring,cstring_literals");
1623 UsedGlobals.push_back(Entry);
1624 }
1625
1626 return getConstantGEP(Entry, 0, 0);
1627}
1628
1629// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001630// FIXME: This Decl should be more precise.
1631llvm::Constant *CGObjCMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
1632 const Decl *Container) {
1633 std::string TypeStr;
1634 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001635 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
1636}
1637
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001638void CGObjCMac::GetNameForMethod(const ObjCMethodDecl *D,
1639 std::string &NameOut) {
1640 // FIXME: Find the mangling GCC uses.
1641 std::stringstream s;
1642 s << (D->isInstance() ? "-" : "+");
1643 s << "[";
1644 s << D->getClassInterface()->getName();
1645 s << " ";
1646 s << D->getSelector().getName();
1647 s << "]";
1648 NameOut = s.str();
1649}
1650
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001651void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001652 EmitModuleInfo();
1653
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001654 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001655
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001656 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001657 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001658 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001659 }
1660
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001661 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001662 llvm::GlobalValue *GV =
1663 new llvm::GlobalVariable(AT, false,
1664 llvm::GlobalValue::AppendingLinkage,
1665 llvm::ConstantArray::get(AT, Used),
1666 "llvm.used",
1667 &CGM.getModule());
1668
1669 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001670
1671 // Add assembler directives to add lazy undefined symbol references
1672 // for classes which are referenced but not defined. This is
1673 // important for correct linker interaction.
1674
1675 // FIXME: Uh, this isn't particularly portable.
1676 std::stringstream s;
1677 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
1678 e = LazySymbols.end(); i != e; ++i) {
1679 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
1680 }
1681 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
1682 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001683 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001684 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
1685 }
1686 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001687}
1688
1689/* *** */
1690
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001691ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Daniel Dunbar3e9df992008-08-23 18:37:06 +00001692 : CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001693{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001694 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1695 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001696
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001697 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001698 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001699 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001700 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1701
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001702 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
1703 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001704
1705 // FIXME: It would be nice to unify this with the opaque type, so
1706 // that the IR comes out a bit cleaner.
1707 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
1708 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001709
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001710 MethodDescriptionTy =
1711 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001712 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001713 NULL);
1714 CGM.getModule().addTypeName("struct._objc_method_description",
1715 MethodDescriptionTy);
1716
1717 MethodDescriptionListTy =
1718 llvm::StructType::get(IntTy,
1719 llvm::ArrayType::get(MethodDescriptionTy, 0),
1720 NULL);
1721 CGM.getModule().addTypeName("struct._objc_method_description_list",
1722 MethodDescriptionListTy);
1723 MethodDescriptionListPtrTy =
1724 llvm::PointerType::getUnqual(MethodDescriptionListTy);
1725
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001726 PropertyTy = llvm::StructType::get(Int8PtrTy,
1727 Int8PtrTy,
1728 NULL);
1729 CGM.getModule().addTypeName("struct._objc_property",
1730 PropertyTy);
1731
1732 PropertyListTy = llvm::StructType::get(IntTy,
1733 IntTy,
1734 llvm::ArrayType::get(PropertyTy, 0),
1735 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001736 CGM.getModule().addTypeName("struct._objc_property_list",
1737 PropertyListTy);
1738 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
1739
1740 // Protocol description structures
1741
1742 ProtocolExtensionTy =
1743 llvm::StructType::get(Types.ConvertType(Ctx.IntTy),
1744 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1745 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1746 PropertyListPtrTy,
1747 NULL);
1748 CGM.getModule().addTypeName("struct._objc_protocol_extension",
1749 ProtocolExtensionTy);
1750 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
1751
1752 // Handle recursive construction of Protocl and ProtocolList types
1753
1754 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
1755 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
1756
1757 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
1758 LongTy,
1759 llvm::ArrayType::get(ProtocolTyHolder, 0),
1760 NULL);
1761 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
1762
1763 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolExtensionTy),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001764 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001765 llvm::PointerType::getUnqual(ProtocolListTyHolder),
1766 MethodDescriptionListPtrTy,
1767 MethodDescriptionListPtrTy,
1768 NULL);
1769 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
1770
1771 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
1772 CGM.getModule().addTypeName("struct._objc_protocol_list",
1773 ProtocolListTy);
1774 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
1775
1776 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
1777 CGM.getModule().addTypeName("struct.__objc_protocol", ProtocolTy);
1778 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001779
1780 // Class description structures
1781
1782 IvarTy = llvm::StructType::get(Int8PtrTy,
1783 Int8PtrTy,
1784 IntTy,
1785 NULL);
1786 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
1787
1788 IvarListTy = llvm::OpaqueType::get();
1789 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
1790 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
1791
1792 MethodTy = llvm::StructType::get(SelectorPtrTy,
1793 Int8PtrTy,
1794 Int8PtrTy,
1795 NULL);
1796 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
1797
1798 MethodListTy = llvm::OpaqueType::get();
1799 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
1800 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
1801
1802 CacheTy = llvm::OpaqueType::get();
1803 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
1804 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
1805
1806 ClassExtensionTy =
1807 llvm::StructType::get(IntTy,
1808 Int8PtrTy,
1809 PropertyListPtrTy,
1810 NULL);
1811 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
1812 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
1813
1814 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
1815
1816 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
1817 llvm::PointerType::getUnqual(ClassTyHolder),
1818 Int8PtrTy,
1819 LongTy,
1820 LongTy,
1821 LongTy,
1822 IvarListPtrTy,
1823 MethodListPtrTy,
1824 CachePtrTy,
1825 ProtocolListPtrTy,
1826 Int8PtrTy,
1827 ClassExtensionPtrTy,
1828 NULL);
1829 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
1830
1831 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
1832 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
1833 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
1834
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001835 CategoryTy = llvm::StructType::get(Int8PtrTy,
1836 Int8PtrTy,
1837 MethodListPtrTy,
1838 MethodListPtrTy,
1839 ProtocolListPtrTy,
1840 IntTy,
1841 PropertyListPtrTy,
1842 NULL);
1843 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
1844
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001845 // I'm not sure I like this. The implicit coordination is a bit
1846 // gross. We should solve this in a reasonable fashion because this
1847 // is a pretty common task (match some runtime data structure with
1848 // an LLVM data structure).
1849
1850 // FIXME: This is leaked.
1851 // FIXME: Merge with rewriter code?
1852 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
1853 SourceLocation(),
1854 &Ctx.Idents.get("_objc_super"), 0);
1855 FieldDecl *FieldDecls[2];
1856 FieldDecls[0] = FieldDecl::Create(Ctx, SourceLocation(), 0,
1857 Ctx.getObjCIdType());
1858 FieldDecls[1] = FieldDecl::Create(Ctx, SourceLocation(), 0,
1859 Ctx.getObjCClassType());
1860 RD->defineBody(FieldDecls, 2);
1861
1862 SuperCTy = Ctx.getTagDeclType(RD);
1863 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
1864
1865 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001866 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001867
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001868 // Global metadata structures
1869
1870 SymtabTy = llvm::StructType::get(LongTy,
1871 SelectorPtrTy,
1872 ShortTy,
1873 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001874 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001875 NULL);
1876 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
1877 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
1878
1879 ModuleTy =
1880 llvm::StructType::get(LongTy,
1881 LongTy,
1882 Int8PtrTy,
1883 SymtabPtrTy,
1884 NULL);
1885 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001886
1887 // Message send functions
1888
1889 std::vector<const llvm::Type*> Params;
1890 Params.push_back(ObjectPtrTy);
1891 Params.push_back(SelectorPtrTy);
1892 MessageSendFn = llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1893 Params,
1894 true),
1895 llvm::Function::ExternalLinkage,
1896 "objc_msgSend",
1897 &CGM.getModule());
1898
1899 Params.clear();
1900 Params.push_back(Int8PtrTy);
1901 Params.push_back(ObjectPtrTy);
1902 Params.push_back(SelectorPtrTy);
1903 MessageSendStretFn =
1904 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1905 Params,
1906 true),
1907 llvm::Function::ExternalLinkage,
1908 "objc_msgSend_stret",
1909 &CGM.getModule());
1910
1911 Params.clear();
1912 Params.push_back(SuperPtrTy);
1913 Params.push_back(SelectorPtrTy);
1914 MessageSendSuperFn =
1915 llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1916 Params,
1917 true),
1918 llvm::Function::ExternalLinkage,
1919 "objc_msgSendSuper",
1920 &CGM.getModule());
1921
1922 Params.clear();
1923 Params.push_back(Int8PtrTy);
1924 Params.push_back(SuperPtrTy);
1925 Params.push_back(SelectorPtrTy);
1926 MessageSendSuperStretFn =
1927 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1928 Params,
1929 true),
1930 llvm::Function::ExternalLinkage,
1931 "objc_msgSendSuper_stret",
1932 &CGM.getModule());
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001933}
1934
1935ObjCTypesHelper::~ObjCTypesHelper() {
1936}
1937
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001938llvm::Value *ObjCTypesHelper::getMessageSendFn(bool IsSuper,
1939 const llvm::Type *ReturnTy) {
1940 llvm::Function *F;
1941 llvm::FunctionType *CallFTy;
1942
1943 // FIXME: Should we be caching any of this?
1944 if (!ReturnTy->isSingleValueType()) {
1945 F = IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
1946 std::vector<const llvm::Type*> Params(3);
1947 Params[0] = llvm::PointerType::getUnqual(ReturnTy);
1948 Params[1] = IsSuper ? SuperPtrTy : ObjectPtrTy;
1949 Params[2] = SelectorPtrTy;
1950 CallFTy = llvm::FunctionType::get(llvm::Type::VoidTy, Params, true);
Daniel Dunbar662174c82008-08-29 17:28:43 +00001951 } else { // FIXME: floating point?
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001952 F = IsSuper ? MessageSendSuperFn : MessageSendFn;
1953 std::vector<const llvm::Type*> Params(2);
1954 Params[0] = IsSuper ? SuperPtrTy : ObjectPtrTy;
1955 Params[1] = SelectorPtrTy;
1956 CallFTy = llvm::FunctionType::get(ReturnTy, Params, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001957 }
1958
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001959 return llvm::ConstantExpr::getBitCast(F,
1960 llvm::PointerType::getUnqual(CallFTy));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001961}
1962
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001963/* *** */
1964
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001965CodeGen::CGObjCRuntime *
1966CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001967 return new CGObjCMac(CGM);
1968}