CGObjCMac.cpp revision 8f2926b73ed635afecd020da787af6a837601a2b
1//===------- 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"
15
16#include "CodeGenModule.h"
17#include "CodeGenFunction.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclObjC.h"
21#include "clang/Basic/LangOptions.h"
22
23#include "llvm/Module.h"
24#include "llvm/Support/IRBuilder.h"
25#include "llvm/Target/TargetData.h"
26#include <sstream>
27
28using namespace clang;
29
30namespace {
31
32  // FIXME: We should find a nicer way to make the labels for
33  // metadata, string concatenation is lame.
34
35/// ObjCTypesHelper - Helper class that encapsulates lazy
36/// construction of varies types used during ObjC generation.
37class ObjCTypesHelper {
38private:
39  CodeGen::CodeGenModule &CGM;
40
41  const llvm::StructType *CFStringType;
42  llvm::Constant *CFConstantStringClassReference;
43  llvm::Function *MessageSendFn;
44
45public:
46  const llvm::Type *ShortTy, *IntTy, *LongTy;
47  const llvm::Type *Int8PtrTy;
48
49  /// ObjectPtrTy - LLVM type for object handles (typeof(id))
50  const llvm::Type *ObjectPtrTy;
51  /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
52  const llvm::Type *SelectorPtrTy;
53  /// ProtocolPtrTy - LLVM type for external protocol handles
54  /// (typeof(Protocol))
55  const llvm::Type *ExternalProtocolPtrTy;
56
57  /// SymtabTy - LLVM type for struct objc_symtab.
58  const llvm::StructType *SymtabTy;
59  /// SymtabPtrTy - LLVM type for struct objc_symtab *.
60  const llvm::Type *SymtabPtrTy;
61  /// ModuleTy - LLVM type for struct objc_module.
62  const llvm::StructType *ModuleTy;
63
64  /// ProtocolTy - LLVM type for struct objc_protocol.
65  const llvm::StructType *ProtocolTy;
66  /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
67  const llvm::Type *ProtocolPtrTy;
68  /// ProtocolExtensionTy - LLVM type for struct
69  /// objc_protocol_extension.
70  const llvm::StructType *ProtocolExtensionTy;
71  /// ProtocolExtensionTy - LLVM type for struct
72  /// objc_protocol_extension *.
73  const llvm::Type *ProtocolExtensionPtrTy;
74  /// MethodDescriptionTy - LLVM type for struct
75  /// objc_method_description.
76  const llvm::StructType *MethodDescriptionTy;
77  /// MethodDescriptionListTy - LLVM type for struct
78  /// objc_method_description_list.
79  const llvm::StructType *MethodDescriptionListTy;
80  /// MethodDescriptionListPtrTy - LLVM type for struct
81  /// objc_method_description_list *.
82  const llvm::Type *MethodDescriptionListPtrTy;
83  /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
84  /// in GCC parlance).
85  const llvm::StructType *PropertyTy;
86  /// PropertyListTy - LLVM type for struct objc_property_list
87  /// (_prop_list_t in GCC parlance).
88  const llvm::StructType *PropertyListTy;
89  /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
90  const llvm::Type *PropertyListPtrTy;
91  /// ProtocolListTy - LLVM type for struct objc_property_list.
92  const llvm::Type *ProtocolListTy;
93  /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
94  const llvm::Type *ProtocolListPtrTy;
95  /// CategoryTy - LLVM type for struct objc_category.
96  const llvm::StructType *CategoryTy;
97  /// ClassTy - LLVM type for struct objc_class.
98  const llvm::StructType *ClassTy;
99  /// ClassPtrTy - LLVM type for struct objc_class *.
100  const llvm::Type *ClassPtrTy;
101  /// ClassExtensionTy - LLVM type for struct objc_class_ext.
102  const llvm::StructType *ClassExtensionTy;
103  /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
104  const llvm::Type *ClassExtensionPtrTy;
105  /// CacheTy - LLVM type for struct objc_cache.
106  const llvm::Type *CacheTy;
107  /// CachePtrTy - LLVM type for struct objc_cache *.
108  const llvm::Type *CachePtrTy;
109  // IvarTy - LLVM type for struct objc_ivar.
110  const llvm::StructType *IvarTy;
111  /// IvarListTy - LLVM type for struct objc_ivar_list.
112  const llvm::Type *IvarListTy;
113  /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
114  const llvm::Type *IvarListPtrTy;
115  // MethodTy - LLVM type for struct objc_method.
116  const llvm::StructType *MethodTy;
117  /// MethodListTy - LLVM type for struct objc_method_list.
118  const llvm::Type *MethodListTy;
119  /// MethodListPtrTy - LLVM type for struct objc_method_list *.
120  const llvm::Type *MethodListPtrTy;
121
122public:
123  ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
124  ~ObjCTypesHelper();
125
126  llvm::Constant *getCFConstantStringClassReference();
127  const llvm::StructType *getCFStringType();
128  llvm::Function *getMessageSendFn();
129};
130
131class CGObjCMac : public CodeGen::CGObjCRuntime {
132private:
133  CodeGen::CodeGenModule &CGM;
134  ObjCTypesHelper ObjCTypes;
135  /// ObjCABI - FIXME: Not sure yet.
136  unsigned ObjCABI;
137
138  /// ClassNames - uniqued class names.
139  llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
140
141  /// MethodVarNames - uniqued method variable names.
142  llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
143
144  /// MethodVarTypes - uniqued method type signatures. We have to use
145  /// a StringMap here because have no other unique reference.
146  llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
147
148  /// PropertyNames - uniqued method variable names.
149  llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
150
151  /// ClassReferences - uniqued class references.
152  llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
153
154  /// SelectorReferences - uniqued selector references.
155  llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
156
157  /// Protocols - Protocols for which an objc_protocol structure has
158  /// been emitted. Forward declarations are handled by creating an
159  /// empty structure whose initializer is filled in when/if defined.
160  llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
161
162  /// DefinedClasses - List of defined classes.
163  std::vector<llvm::GlobalValue*> DefinedClasses;
164
165  /// DefinedCategories - List of defined categories.
166  std::vector<llvm::GlobalValue*> DefinedCategories;
167
168  /// UsedGlobals - List of globals to pack into the llvm.used metadata
169  /// to prevent them from being clobbered.
170  std::vector<llvm::GlobalVariable*> UsedGlobals;
171
172  /// EmitImageInfo - Emit the image info marker used to encode some module
173  /// level information.
174  void EmitImageInfo();
175
176  /// EmitModuleInfo - Another marker encoding module level
177  /// information.
178  void EmitModuleInfo();
179
180  /// EmitModuleSymols - Emit module symbols, the list of defined
181  /// classes and categories. The result has type SymtabPtrTy.
182  llvm::Constant *EmitModuleSymbols();
183
184  /// FinishModule - Write out global data structures at the end of
185  /// processing a translation unit.
186  void FinishModule();
187
188  /// EmitClassExtension - Generate the class extension structure used
189  /// to store the weak ivar layout and properties. The return value
190  /// has type ClassExtensionPtrTy.
191  llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
192
193  /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
194  /// for the given class.
195  llvm::Value *EmitClassRef(llvm::IRBuilder<> &Builder,
196                            const ObjCInterfaceDecl *ID);
197
198  /// EmitIvarList - Emit the ivar list for the given
199  /// implementation. If ForClass is true the list of class ivars
200  /// (i.e. metaclass ivars) is emitted, otherwise the list of
201  /// interface ivars will be emitted. The return value has type
202  /// IvarListPtrTy.
203  llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
204                               bool ForClass,
205                               const llvm::Type *InterfaceTy);
206
207  /// EmitMetaClass - Emit a class structure for the metaclass of the
208  /// given implementation. return value has type ClassPtrTy.
209  llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
210                                llvm::Constant *Protocols,
211                                const llvm::Type *InterfaceTy);
212
213  /// EmitMethodList - Emit the method list for the given
214  /// implementation. If ForClass is true the list of class methods
215  /// will be emitted, otherwise the list of instance methods will be
216  /// generated. The return value has type MethodListPtrTy.
217  llvm::Constant *EmitMethodList(const std::string &Name,
218                                 const char *Section,
219                   llvm::SmallVector<ObjCMethodDecl*, 32>::const_iterator begin,
220                   llvm::SmallVector<ObjCMethodDecl*, 32>::const_iterator end);
221
222  /// EmitMethodDescList - Emit a method description list for a list of
223  /// method declarations.
224  ///  - TypeName: The name for the type containing the methods.
225  ///  - IsProtocol: True iff these methods are for a protocol.
226  ///  - ClassMethds: True iff these are class methods.
227  ///  - Required: When true, only "required" methods are
228  ///    listed. Similarly, when false only "optional" methods are
229  ///    listed. For classes this should always be true.
230  ///  - begin, end: The method list to output.
231  ///
232  /// The return value has type MethodDescriptionListPtrTy.
233  llvm::Constant *EmitMethodDescList(const std::string &TypeName,
234                                     bool IsProtocol,
235                                     bool ClassMethods,
236                                     bool Required,
237                                     ObjCMethodDecl * const *begin,
238                                     ObjCMethodDecl * const *end);
239
240  /// EmitPropertyList - Emit the given property list. The return
241  /// value has type PropertyListPtrTy.
242  llvm::Constant *EmitPropertyList(const std::string &Name,
243                                   ObjCPropertyDecl * const *begin,
244                                   ObjCPropertyDecl * const *end);
245
246  /// EmitProtocolExtension - Generate the protocol extension
247  /// structure used to store optional instance and class methods, and
248  /// protocol properties. The return value has type
249  /// ProtocolExtensionPtrTy.
250  llvm::Constant *EmitProtocolExtension(const ObjCProtocolDecl *PD);
251
252  /// EmitProtocolList - Generate the list of referenced
253  /// protocols. The return value has type ProtocolListPtrTy.
254  llvm::Constant *EmitProtocolList(const std::string &Name,
255                                   ObjCProtocolDecl::protocol_iterator begin,
256                                   ObjCProtocolDecl::protocol_iterator end);
257
258  /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
259  /// for the given selector.
260  llvm::Value *EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel);
261
262  /// GetProtocolRef - Return a reference to the internal protocol
263  /// description, creating an empty one if it has not been
264  /// defined. The return value has type pointer-to ProtocolTy.
265  llvm::GlobalVariable *GetProtocolRef(const ObjCProtocolDecl *PD);
266
267  /// GetClassName - Return a unique constant for the given selector's
268  /// name. The return value has type char *.
269  llvm::Constant *GetClassName(IdentifierInfo *Ident);
270
271  /// GetMethodVarName - Return a unique constant for the given
272  /// selector's name. The return value has type char *.
273  llvm::Constant *GetMethodVarName(Selector Sel);
274  llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
275  llvm::Constant *GetMethodVarName(const std::string &Name);
276
277  /// GetMethodVarType - Return a unique constant for the given
278  /// selector's name. The return value has type char *.
279
280  // FIXME: This is a horrible name.
281  llvm::Constant *GetMethodVarType(ObjCMethodDecl *D);
282  llvm::Constant *GetMethodVarType(const std::string &Name);
283
284  /// GetPropertyName - Return a unique constant for the given
285  /// name. The return value has type char *.
286  llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
287
288  // FIXME: This is a horrible name too.
289  llvm::Constant *GetPropertyType(const ObjCPropertyDecl *PD);
290
291  /// GetNameForMethod - Return a name for the given method.
292  /// \param[out] NameOut - The return value.
293  void GetNameForMethod(const ObjCMethodDecl *OMD,
294                        std::string &NameOut);
295
296public:
297  CGObjCMac(CodeGen::CodeGenModule &cgm);
298  virtual llvm::Constant *GenerateConstantString(const std::string &String);
299
300  virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
301                                              const ObjCMessageExpr *E,
302                                              llvm::Value *Receiver);
303
304  virtual CodeGen::RValue
305  GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
306                           const ObjCMessageExpr *E,
307                           const ObjCInterfaceDecl *SuperClass,
308                           llvm::Value *Receiver);
309
310  virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder,
311                                const ObjCInterfaceDecl *ID);
312
313  virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
314
315  virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
316
317  virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
318
319  virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
320
321  virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
322                                           const ObjCProtocolDecl *PD);
323
324  virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
325
326  virtual llvm::Function *ModuleInitFunction();
327};
328} // end anonymous namespace
329
330/* *** Helper Functions *** */
331
332/// getConstantGEP() - Help routine to construct simple GEPs.
333static llvm::Constant *getConstantGEP(llvm::Constant *C,
334                                      unsigned idx0,
335                                      unsigned idx1) {
336  llvm::Value *Idxs[] = {
337    llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
338    llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
339  };
340  return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
341}
342
343/* *** CGObjCMac Public Interface *** */
344
345CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm)
346  : CGM(cgm),
347    ObjCTypes(cgm),
348    ObjCABI(1)
349{
350  // FIXME: How does this get set in GCC? And what does it even mean?
351  if (ObjCTypes.LongTy != CGM.getTypes().ConvertType(CGM.getContext().IntTy))
352      ObjCABI = 2;
353
354  EmitImageInfo();
355}
356
357/// GetClass - Return a reference to the class for the given interface
358/// decl.
359llvm::Value *CGObjCMac::GetClass(llvm::IRBuilder<> &Builder,
360                                 const ObjCInterfaceDecl *ID) {
361  return EmitClassRef(Builder, ID);
362}
363
364/// GetSelector - Return the pointer to the unique'd string for this selector.
365llvm::Value *CGObjCMac::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
366  return EmitSelector(Builder, Sel);
367}
368
369/// Generate a constant CFString object.
370/*
371   struct __builtin_CFString {
372     const int *isa; // point to __CFConstantStringClassReference
373     int flags;
374     const char *str;
375     long length;
376   };
377*/
378
379llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
380  // FIXME: I have no idea what this constant is (it is a magic
381  // constant in GCC as well). Most likely the encoding of the string
382  // and at least one part of it relates to UTF-16. Is this just the
383  // code for UTF-8? Where is this handled for us?
384  //  See: <rdr://2996215>
385  unsigned flags = 0x07c8;
386
387  // FIXME: Use some machinery to unique this. We can't reuse the CGM
388  // one since we put them in a different section.
389  llvm::Constant *StringC = llvm::ConstantArray::get(String);
390  llvm::Constant *StringGV =
391    new llvm::GlobalVariable(StringC->getType(), true,
392                             llvm::GlobalValue::InternalLinkage,
393                             StringC, ".str", &CGM.getModule());
394  llvm::Constant *Values[4] = {
395    ObjCTypes.getCFConstantStringClassReference(),
396    llvm::ConstantInt::get(llvm::Type::Int32Ty, flags),
397    getConstantGEP(StringGV, 0, 0), // Decay array -> ptr
398    llvm::ConstantInt::get(ObjCTypes.LongTy, String.size())
399  };
400
401  llvm::Constant *CFStringC =
402    llvm::ConstantStruct::get(ObjCTypes.getCFStringType(),
403                              std::vector<llvm::Constant*>(Values, Values+4));
404
405  llvm::GlobalVariable *CFStringGV =
406    new llvm::GlobalVariable(CFStringC->getType(), true,
407                             llvm::GlobalValue::InternalLinkage,
408                             CFStringC, "",
409                             &CGM.getModule());
410
411  CFStringGV->setSection("__DATA, __cfstring");
412
413  return CFStringGV;
414}
415
416/// Generates a message send where the super is the receiver.  This is
417/// a message send to self with special delivery semantics indicating
418/// which class's method should be called.
419CodeGen::RValue
420CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
421                                    const ObjCMessageExpr *E,
422                                    const ObjCInterfaceDecl *SuperClass,
423                                    llvm::Value *Receiver) {
424  assert(0 && "Cannot generate message send to super for Mac runtime.");
425  return CodeGen::RValue::get(0);
426}
427
428/// Generate code for a message send expression.
429CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
430                                               const ObjCMessageExpr *E,
431                                               llvm::Value *Receiver) {
432  const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(E->getType());
433  llvm::Function *F = ObjCTypes.getMessageSendFn();
434  llvm::Value *Args[2];
435  Args[0] = CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
436  Args[1] = EmitSelector(CGF.Builder, E->getSelector());
437
438  std::vector<const llvm::Type*> Params;
439  Params.push_back(ObjCTypes.ObjectPtrTy);
440  Params.push_back(ObjCTypes.SelectorPtrTy);
441  llvm::FunctionType *CallFTy = llvm::FunctionType::get(ReturnTy,
442                                                        Params,
443                                                        true);
444  llvm::Type *PCallFTy = llvm::PointerType::getUnqual(CallFTy);
445  llvm::Constant *C = llvm::ConstantExpr::getBitCast(F, PCallFTy);
446  return CGF.EmitCallExprExt(C, E->getType(),
447                             E->arg_begin(),
448                             E->arg_end(),
449                             Args, 2);
450}
451
452llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
453                                            const ObjCProtocolDecl *PD) {
454  return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
455                                        ObjCTypes.ExternalProtocolPtrTy);
456}
457
458/*
459     // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
460  struct _objc_protocol {
461    struct _objc_protocol_extension *isa;
462    char *protocol_name;
463    struct _objc_protocol_list *protocol_list;
464    struct _objc__method_prototype_list *instance_methods;
465    struct _objc__method_prototype_list *class_methods
466  };
467
468  See EmitProtocolExtension().
469*/
470void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
471  const char *ProtocolName = PD->getName();
472
473  std::vector<llvm::Constant*> Values(5);
474  Values[0] = EmitProtocolExtension(PD);
475  Values[1] = GetClassName(PD->getIdentifier());
476  Values[2] =
477    EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(),
478                     PD->protocol_begin(),
479                     PD->protocol_end());
480  Values[3] = EmitMethodDescList(ProtocolName,
481                                 true, // IsProtocol
482                                 false, // ClassMethods
483                                 true, // Required
484                                 PD->instmeth_begin(),
485                                 PD->instmeth_end());
486  Values[4] = EmitMethodDescList(ProtocolName,
487                                 true, // IsProtocol
488                                 true, // ClassMethods
489                                 true, // Required
490                                 PD->classmeth_begin(),
491                                 PD->classmeth_end());
492  llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
493                                                   Values);
494
495  llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
496  if (Entry) {
497    // Already created, just update the initializer
498    Entry->setInitializer(Init);
499  } else {
500    Entry =
501      new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
502                               llvm::GlobalValue::InternalLinkage,
503                               Init,
504                               std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
505                               &CGM.getModule());
506    Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
507    UsedGlobals.push_back(Entry);
508    // FIXME: Is this necessary? Why only for protocol?
509    Entry->setAlignment(4);
510  }
511}
512
513llvm::GlobalVariable *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
514  llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
515
516  if (!Entry) {
517    std::vector<llvm::Constant*> Values(5);
518    Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
519    Values[1] = GetClassName(PD->getIdentifier());
520    Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
521    Values[3] = Values[4] =
522      llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
523    llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
524                                                     Values);
525
526    Entry =
527      new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
528                               llvm::GlobalValue::InternalLinkage,
529                               Init,
530                               std::string("\01L_OBJC_PROTOCOL_")+PD->getName(),
531                               &CGM.getModule());
532    Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
533    UsedGlobals.push_back(Entry);
534    // FIXME: Is this necessary? Why only for protocol?
535    Entry->setAlignment(4);
536  }
537
538  return Entry;
539}
540
541/*
542  struct _objc_protocol_extension {
543    uint32_t size;
544    struct objc_method_description_list *optional_instance_methods;
545    struct objc_method_description_list *optional_class_methods;
546    struct objc_property_list *instance_properties;
547  };
548*/
549llvm::Constant *CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD) {
550  uint64_t Size =
551    CGM.getTargetData().getABITypeSize(ObjCTypes.ProtocolExtensionTy);
552  std::vector<llvm::Constant*> Values(4);
553  Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
554  Values[1] = EmitMethodDescList(PD->getName(),
555                                 true, // IsProtocol
556                                 false, // ClassMethods
557                                 false, // Required
558                                 PD->instmeth_begin(),
559                                 PD->instmeth_end());
560  Values[2] = EmitMethodDescList(PD->getName(),
561                                 true, // IsProtocol
562                                 true, // ClassMethods
563                                 false, // Required
564                                 PD->classmeth_begin(),
565                                 PD->classmeth_end());
566  Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") +
567                               PD->getName(),
568                               PD->classprop_begin(),
569                               PD->classprop_end());
570
571  // Return null if no extension bits are used.
572  if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
573      Values[3]->isNullValue())
574    return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
575
576  llvm::Constant *Init =
577    llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
578  llvm::GlobalVariable *GV =
579      new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
580                               llvm::GlobalValue::InternalLinkage,
581                               Init,
582                               (std::string("\01L_OBJC_PROTOCOLEXT_") +
583                                PD->getName()),
584                               &CGM.getModule());
585  // No special section, but goes in llvm.used
586  UsedGlobals.push_back(GV);
587
588  return GV;
589}
590
591/*
592  struct objc_protocol_list {
593    struct objc_protocol_list *next;
594    long count;
595    Protocol *list[];
596  };
597*/
598llvm::Constant *
599CGObjCMac::EmitProtocolList(const std::string &Name,
600                            ObjCProtocolDecl::protocol_iterator begin,
601                            ObjCProtocolDecl::protocol_iterator end) {
602  std::vector<llvm::Constant*> ProtocolRefs;
603
604  for (; begin != end; ++begin)
605    ProtocolRefs.push_back(GetProtocolRef(*begin));
606
607  // Just return null for empty protocol lists
608  if (ProtocolRefs.empty())
609    return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
610
611  // This list is null terminated.
612  ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
613
614  std::vector<llvm::Constant*> Values(3);
615  // This field is only used by the runtime.
616  Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
617  Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
618  Values[2] =
619    llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
620                                                  ProtocolRefs.size()),
621                             ProtocolRefs);
622
623  llvm::Constant *Init = llvm::ConstantStruct::get(Values);
624  llvm::GlobalVariable *GV =
625    new llvm::GlobalVariable(Init->getType(), false,
626                             llvm::GlobalValue::InternalLinkage,
627                             Init,
628                             Name,
629                             &CGM.getModule());
630  GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
631  return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
632}
633
634/*
635  struct _objc_property {
636    const char * const name;
637    const char * const attributes;
638  };
639
640  struct _objc_property_list {
641    uint32_t entsize; // sizeof (struct _objc_property)
642    uint32_t prop_count;
643    struct _objc_property[prop_count];
644  };
645*/
646llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
647                                            ObjCPropertyDecl * const *begin,
648                                            ObjCPropertyDecl * const *end) {
649  std::vector<llvm::Constant*> Properties, Prop(2);
650  for (; begin != end; ++begin) {
651    const ObjCPropertyDecl *PD = *begin;
652    Prop[0] = GetPropertyName(PD->getIdentifier());
653    Prop[1] = GetPropertyType(PD);
654    Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
655                                                   Prop));
656  }
657
658  // Return null for empty list.
659  if (Properties.empty())
660    return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
661
662  unsigned PropertySize =
663    CGM.getTargetData().getABITypeSize(ObjCTypes.PropertyTy);
664  std::vector<llvm::Constant*> Values(3);
665  Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
666  Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
667  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
668                                             Properties.size());
669  Values[2] = llvm::ConstantArray::get(AT, Properties);
670  llvm::Constant *Init = llvm::ConstantStruct::get(Values);
671
672  llvm::GlobalVariable *GV =
673    new llvm::GlobalVariable(Init->getType(), false,
674                             llvm::GlobalValue::InternalLinkage,
675                             Init,
676                             Name,
677                             &CGM.getModule());
678  // No special section on property lists?
679  UsedGlobals.push_back(GV);
680  return llvm::ConstantExpr::getBitCast(GV,
681                                        ObjCTypes.PropertyListPtrTy);
682
683}
684
685/*
686  struct objc_method_description_list {
687    int count;
688    struct objc_method_description list[];
689  };
690*/
691llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &TypeName,
692                                              bool IsProtocol,
693                                              bool ClassMethods,
694                                              bool Required,
695                                              ObjCMethodDecl * const *begin,
696                                              ObjCMethodDecl * const *end) {
697  std::vector<llvm::Constant*> Methods, Desc(2);
698  for (; begin != end; ++begin) {
699    ObjCMethodDecl *D = *begin;
700    bool IsRequired = D->getImplementationControl() != ObjCMethodDecl::Optional;
701
702    // Skip if this method is required and we are outputting optional
703    // methods, or vice versa.
704    if (Required != IsRequired)
705      continue;
706
707    Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(D->getSelector()),
708                                             ObjCTypes.SelectorPtrTy);
709    Desc[1] = GetMethodVarType(D);
710    Methods.push_back(llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
711                                                Desc));
712  }
713
714  // Return null for empty list.
715  if (Methods.empty())
716    return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
717
718  std::vector<llvm::Constant*> Values(2);
719  Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
720  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
721                                             Methods.size());
722  Values[1] = llvm::ConstantArray::get(AT, Methods);
723  llvm::Constant *Init = llvm::ConstantStruct::get(Values);
724
725  char Prefix[256];
726  sprintf(Prefix, "\01L_OBJC_%s%sMETHODS_%s",
727          IsProtocol ? "PROTOCOL_" : "",
728          ClassMethods ? "CLASS_" : "INSTANCE_",
729          !Required ? "OPT_" : "");
730  llvm::GlobalVariable *GV =
731    new llvm::GlobalVariable(Init->getType(), false,
732                             llvm::GlobalValue::InternalLinkage,
733                             Init,
734                             std::string(Prefix) + TypeName,
735                             &CGM.getModule());
736  if (ClassMethods) {
737    GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
738  } else {
739    GV->setSection("__OBJC,__cat_inst_meth,regular,no_dead_strip");
740  }
741  UsedGlobals.push_back(GV);
742  return llvm::ConstantExpr::getBitCast(GV,
743                                        ObjCTypes.MethodDescriptionListPtrTy);
744}
745
746/*
747  struct _objc_category {
748    char *category_name;
749    char *class_name;
750    struct _objc_method_list *instance_methods;
751    struct _objc_method_list *class_methods;
752    struct _objc_protocol_list *protocols;
753    uint32_t size; // <rdar://4585769>
754    struct _objc_property_list *instance_properties;
755  };
756 */
757void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
758  unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.CategoryTy);
759
760  const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
761  std::string ExtName(std::string(Interface->getName()) +
762                      "_" +
763                      OCD->getName());
764
765  std::vector<llvm::Constant*> Values(7);
766  Values[0] = GetClassName(OCD->getIdentifier());
767  Values[1] = GetClassName(Interface->getIdentifier());
768  Values[2] =
769    EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
770                   ExtName,
771                   "__OBJC,__cat_inst_meth,regular,no_dead_strip",
772                   OCD->instmeth_begin(),
773                   OCD->instmeth_end());
774  Values[3] =
775    EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
776                   "__OBJC,__cat_class_meth,regular,no_dead_strip",
777                   OCD->classmeth_begin(),
778                   OCD->classmeth_end());
779  Values[4] =
780    EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
781                     Interface->protocol_begin(),
782                     Interface->protocol_end());
783  Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
784  Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
785                               Interface->classprop_begin(),
786                               Interface->classprop_end());
787
788  llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
789                                                   Values);
790
791  llvm::GlobalVariable *GV =
792    new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
793                             llvm::GlobalValue::InternalLinkage,
794                             Init,
795                             std::string("\01L_OBJC_CATEGORY_")+ExtName,
796                             &CGM.getModule());
797  GV->setSection("__OBJC,__category,regular,no_dead_strip");
798  UsedGlobals.push_back(GV);
799  DefinedCategories.push_back(GV);
800}
801
802// FIXME: Get from somewhere?
803enum ClassFlags {
804  eClassFlags_Factory              = 0x00001,
805  eClassFlags_Meta                 = 0x00002,
806  // <rdr://5142207>
807  eClassFlags_HasCXXStructors      = 0x02000,
808  eClassFlags_Hidden               = 0x20000,
809  eClassFlags_ABI2_Hidden          = 0x00010,
810  eClassFlags_ABI2_HasCXXStructors = 0x00004   // <rdr://4923634>
811};
812
813// <rdr://5142207&4705298&4843145>
814static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
815  if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
816    // FIXME: Support -fvisibility
817    switch (attr->getVisibility()) {
818    default:
819      assert(0 && "Unknown visibility");
820      return false;
821    case VisibilityAttr::DefaultVisibility:
822    case VisibilityAttr::ProtectedVisibility:  // FIXME: What do we do here?
823      return false;
824    case VisibilityAttr::HiddenVisibility:
825      return true;
826    }
827  } else {
828    return false; // FIXME: Support -fvisibility
829  }
830}
831
832/*
833  struct _objc_class {
834    Class isa;
835    Class super_class;
836    const char *name;
837    long version;
838    long info;
839    long instance_size;
840    struct _objc_ivar_list *ivars;
841    struct _objc_method_list *methods;
842    struct _objc_cache *cache;
843    struct _objc_protocol_list *protocols;
844    // Objective-C 1.0 extensions (<rdr://4585769>)
845    const char *ivar_layout;
846    struct _objc_class_ext *ext;
847  };
848
849  See EmitClassExtension();
850 */
851void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
852  const char *ClassName = ID->getName();
853  // FIXME: Gross
854  ObjCInterfaceDecl *Interface =
855    const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
856  llvm::Constant *Protocols =
857    EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(),
858                     Interface->protocol_begin(),
859                     Interface->protocol_end());
860  const llvm::Type *InterfaceTy =
861   CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
862  unsigned Flags = eClassFlags_Factory;
863  unsigned Size = CGM.getTargetData().getABITypeSize(InterfaceTy);
864
865  // FIXME: Set CXX-structors flag.
866  if (IsClassHidden(ID->getClassInterface()))
867    Flags |= eClassFlags_Hidden;
868
869  std::vector<llvm::Constant*> Values(12);
870  Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy);
871  if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
872    Values[ 1] =
873      llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
874                                     ObjCTypes.ClassPtrTy);
875  } else {
876    Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
877  }
878  Values[ 2] = GetClassName(ID->getIdentifier());
879  // Version is always 0.
880  Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
881  Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
882  Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
883  Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
884  Values[ 7] =
885    EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(),
886                   "__OBJC,__inst_meth,regular,no_dead_strip",
887                   ID->instmeth_begin(),
888                   ID->instmeth_end());
889  // cache is always NULL.
890  Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
891  Values[ 9] = Protocols;
892  // FIXME: Set ivar_layout
893  Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
894  Values[11] = EmitClassExtension(ID);
895  llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
896                                                   Values);
897
898  llvm::GlobalVariable *GV =
899    new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
900                             llvm::GlobalValue::InternalLinkage,
901                             Init,
902                             std::string("\01L_OBJC_CLASS_")+ClassName,
903                             &CGM.getModule());
904  GV->setSection("__OBJC,__class,regular,no_dead_strip");
905  UsedGlobals.push_back(GV);
906  // FIXME: Why?
907  GV->setAlignment(32);
908  DefinedClasses.push_back(GV);
909}
910
911llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
912                                         llvm::Constant *Protocols,
913                                         const llvm::Type *InterfaceTy) {
914  const char *ClassName = ID->getName();
915  unsigned Flags = eClassFlags_Meta;
916  unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ClassTy);
917
918  if (IsClassHidden(ID->getClassInterface()))
919    Flags |= eClassFlags_Hidden;
920
921  std::vector<llvm::Constant*> Values(12);
922  // The isa for the metaclass is the root of the hierarchy.
923  const ObjCInterfaceDecl *Root = ID->getClassInterface();
924  while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
925    Root = Super;
926  Values[ 0] =
927    llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
928                                   ObjCTypes.ClassPtrTy);
929  // The super class for the metaclass is emitted as the name of the
930  // super class. The runtime fixes this up to point to the
931  // *metaclass* for the super class.
932  if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
933    Values[ 1] =
934      llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
935                                     ObjCTypes.ClassPtrTy);
936  } else {
937    Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
938  }
939  Values[ 2] = GetClassName(ID->getIdentifier());
940  // Version is always 0.
941  Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
942  Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
943  Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
944  Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
945  Values[ 7] =
946    EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(),
947                   "__OBJC,__inst_meth,regular,no_dead_strip",
948                   ID->classmeth_begin(),
949                   ID->classmeth_end());
950  // cache is always NULL.
951  Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
952  Values[ 9] = Protocols;
953  // ivar_layout for metaclass is always NULL.
954  Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
955  // The class extension is always unused for metaclasses.
956  Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
957  llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
958                                                   Values);
959
960  llvm::GlobalVariable *GV =
961    new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
962                             llvm::GlobalValue::InternalLinkage,
963                             Init,
964                             std::string("\01L_OBJC_METACLASS_")+ClassName,
965                             &CGM.getModule());
966  GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
967  UsedGlobals.push_back(GV);
968  // FIXME: Why?
969  GV->setAlignment(32);
970
971  return GV;
972}
973
974/*
975  struct objc_class_ext {
976    uint32_t size;
977    const char *weak_ivar_layout;
978    struct _objc_property_list *properties;
979  };
980*/
981llvm::Constant *
982CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
983  uint64_t Size =
984    CGM.getTargetData().getABITypeSize(ObjCTypes.ClassExtensionTy);
985
986  std::vector<llvm::Constant*> Values(3);
987  Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
988  // FIXME: Output weak_ivar_layout string.
989  Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
990  Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") +
991                               ID->getName(),
992                               ID->getClassInterface()->classprop_begin(),
993                               ID->getClassInterface()->classprop_end());
994
995  // Return null if no extension bits are used.
996  if (Values[1]->isNullValue() && Values[2]->isNullValue())
997    return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
998
999  llvm::Constant *Init =
1000    llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1001  llvm::GlobalVariable *GV =
1002    new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1003                             llvm::GlobalValue::InternalLinkage,
1004                             Init,
1005                             (std::string("\01L_OBJC_CLASSEXT_") +
1006                              ID->getName()),
1007                             &CGM.getModule());
1008  // No special section, but goes in llvm.used
1009  UsedGlobals.push_back(GV);
1010
1011  return GV;
1012}
1013
1014/*
1015  struct objc_ivar {
1016    char *ivar_name;
1017    char *ivar_type;
1018    int ivar_offset;
1019  };
1020
1021  struct objc_ivar_list {
1022    int ivar_count;
1023    struct objc_ivar list[count];
1024  };
1025 */
1026llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1027                                        bool ForClass,
1028                                        const llvm::Type *InterfaceTy) {
1029  std::vector<llvm::Constant*> Ivars, Ivar(3);
1030
1031  // When emitting the root class GCC emits ivar entries for the
1032  // actual class structure. It is not clear if we need to follow this
1033  // behavior; for now lets try and get away with not doing it. If so,
1034  // the cleanest solution would be to make up an ObjCInterfaceDecl
1035  // for the class.
1036  if (ForClass)
1037    return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1038
1039  const llvm::StructLayout *Layout =
1040    CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
1041  for (ObjCInterfaceDecl::ivar_iterator
1042         i = ID->getClassInterface()->ivar_begin(),
1043         e = ID->getClassInterface()->ivar_end(); i != e; ++i) {
1044    ObjCIvarDecl *V = *i;
1045    unsigned Offset =
1046      Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(V));
1047    std::string TypeStr;
1048    llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1049    Ivar[0] = GetMethodVarName(V->getIdentifier());
1050    CGM.getContext().getObjCEncodingForType(V->getType(), TypeStr,
1051                                            EncodingRecordTypes);
1052    Ivar[1] = GetMethodVarType(TypeStr);
1053    Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
1054    Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy,
1055                                              Ivar));
1056  }
1057
1058  // Return null for empty list.
1059  if (Ivars.empty())
1060    return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1061
1062  std::vector<llvm::Constant*> Values(2);
1063  Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1064  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1065                                             Ivars.size());
1066  Values[1] = llvm::ConstantArray::get(AT, Ivars);
1067  llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1068
1069  const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1070                        "\01L_OBJC_INSTANCE_VARIABLES_");
1071  llvm::GlobalVariable *GV =
1072    new llvm::GlobalVariable(Init->getType(), false,
1073                             llvm::GlobalValue::InternalLinkage,
1074                             Init,
1075                             std::string(Prefix) + ID->getName(),
1076                             &CGM.getModule());
1077  if (ForClass) {
1078    GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1079    // FIXME: Why is this only here?
1080    GV->setAlignment(32);
1081  } else {
1082    GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1083  }
1084  UsedGlobals.push_back(GV);
1085  return llvm::ConstantExpr::getBitCast(GV,
1086                                        ObjCTypes.IvarListPtrTy);
1087}
1088
1089/*
1090  struct objc_method {
1091    SEL method_name;
1092    char *method_types;
1093    void *method;
1094  };
1095
1096  struct objc_method_list {
1097    struct objc_method_list *obsolete;
1098    int count;
1099    struct objc_method methods_list[count];
1100  };
1101*/
1102llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1103                                          const char *Section,
1104                   llvm::SmallVector<ObjCMethodDecl*, 32>::const_iterator begin,
1105                   llvm::SmallVector<ObjCMethodDecl*, 32>::const_iterator end) {
1106  std::vector<llvm::Constant*> Methods, Method(3);
1107
1108  for (; begin != end; ++begin) {
1109    ObjCMethodDecl *MD = *begin;
1110
1111    Method[0] =
1112      llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1113                                     ObjCTypes.SelectorPtrTy);
1114    Method[1] = GetMethodVarType(MD);
1115
1116    // FIXME: This is gross, we shouldn't be looking up by name.
1117    std::string Name;
1118    GetNameForMethod(MD, Name);
1119    Method[2] =
1120      llvm::ConstantExpr::getBitCast(CGM.getModule().getFunction(Name),
1121                                     ObjCTypes.Int8PtrTy);
1122    Methods.push_back(llvm::ConstantStruct::get(ObjCTypes.MethodTy,
1123                                                Method));
1124  }
1125
1126  // Return null for empty list.
1127  if (Methods.empty())
1128    return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1129
1130  std::vector<llvm::Constant*> Values(3);
1131  Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1132  Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1133  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1134                                             Methods.size());
1135  Values[2] = llvm::ConstantArray::get(AT, Methods);
1136  llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1137
1138  llvm::GlobalVariable *GV =
1139    new llvm::GlobalVariable(Init->getType(), false,
1140                             llvm::GlobalValue::InternalLinkage,
1141                             Init,
1142                             Name,
1143                             &CGM.getModule());
1144  GV->setSection(Section);
1145  UsedGlobals.push_back(GV);
1146  return llvm::ConstantExpr::getBitCast(GV,
1147                                        ObjCTypes.MethodListPtrTy);
1148}
1149
1150llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD) {
1151  const llvm::Type *ReturnTy =
1152    CGM.getTypes().ConvertReturnType(OMD->getResultType());
1153  const llvm::Type *SelfTy =
1154    CGM.getTypes().ConvertType(OMD->getSelfDecl()->getType());
1155
1156  std::vector<const llvm::Type*> ArgTys;
1157  ArgTys.reserve(1 + 2 + OMD->param_size());
1158
1159  // FIXME: This is not something we should have to be dealing with
1160  // here.
1161  bool useStructRet =
1162    CodeGen::CodeGenFunction::hasAggregateLLVMType(OMD->getResultType());
1163  if (useStructRet) {
1164    ArgTys.push_back(llvm::PointerType::getUnqual(ReturnTy));
1165    ReturnTy = llvm::Type::VoidTy;
1166  }
1167
1168  // Implicit arguments
1169  ArgTys.push_back(SelfTy);
1170  ArgTys.push_back(ObjCTypes.SelectorPtrTy);
1171
1172  for (ObjCMethodDecl::param_const_iterator
1173         i = OMD->param_begin(), e = OMD->param_end();
1174       i != e; ++i) {
1175    const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
1176    if (Ty->isSingleValueType()) {
1177      ArgTys.push_back(Ty);
1178    } else {
1179      ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
1180    }
1181  }
1182
1183  std::string Name;
1184  GetNameForMethod(OMD, Name);
1185
1186  llvm::Function *Method =
1187    llvm::Function::Create(llvm::FunctionType::get(ReturnTy,
1188                                                   ArgTys,
1189                                                   OMD->isVariadic()),
1190                           llvm::GlobalValue::InternalLinkage,
1191                           Name,
1192                           &CGM.getModule());
1193
1194  unsigned Offset = 3; // Return plus self and selector implicit args.
1195  if (useStructRet) {
1196    Method->addParamAttr(1, llvm::ParamAttr::StructRet);
1197    ++Offset;
1198  }
1199
1200  // FIXME: This is horrible, we need to be reusing the machinery in
1201  // CodeGenModule.cpp (SetFunctionAttributes).
1202  for (ObjCMethodDecl::param_const_iterator
1203         i = OMD->param_begin(), e = OMD->param_end();
1204       i != e; ++i, ++Offset) {
1205    const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
1206    if (!Ty->isSingleValueType())
1207      Method->addParamAttr(Offset, llvm::ParamAttr::ByVal);
1208  }
1209
1210  return Method;
1211}
1212
1213llvm::Function *CGObjCMac::ModuleInitFunction() {
1214  // Abuse this interface function as a place to finalize.
1215  FinishModule();
1216
1217  return NULL;
1218}
1219
1220/* *** Private Interface *** */
1221
1222/// EmitImageInfo - Emit the image info marker used to encode some module
1223/// level information.
1224///
1225/// See: <rdr://4810609&4810587&4810587>
1226/// struct IMAGE_INFO {
1227///   unsigned version;
1228///   unsigned flags;
1229/// };
1230enum ImageInfoFlags {
1231  eImageInfo_FixAndContinue   = (1 << 0), // FIXME: Not sure what this implies
1232  eImageInfo_GarbageCollected = (1 << 1),
1233  eImageInfo_GCOnly           = (1 << 2)
1234};
1235
1236void CGObjCMac::EmitImageInfo() {
1237  unsigned version = 0; // Version is unused?
1238  unsigned flags = 0;
1239
1240  // FIXME: Fix and continue?
1241  if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
1242    flags |= eImageInfo_GarbageCollected;
1243  if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
1244    flags |= eImageInfo_GCOnly;
1245
1246  // Emitted as int[2];
1247  llvm::Constant *values[2] = {
1248    llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
1249    llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
1250  };
1251  llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
1252  llvm::GlobalVariable *GV =
1253    new llvm::GlobalVariable(AT, true,
1254                             llvm::GlobalValue::InternalLinkage,
1255                             llvm::ConstantArray::get(AT, values, 2),
1256                             "\01L_OBJC_IMAGE_INFO",
1257                             &CGM.getModule());
1258
1259  if (ObjCABI == 1) {
1260    GV->setSection("__OBJC, __image_info,regular");
1261  } else {
1262    GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
1263  }
1264
1265  UsedGlobals.push_back(GV);
1266}
1267
1268
1269// struct objc_module {
1270//   unsigned long version;
1271//   unsigned long size;
1272//   const char *name;
1273//   Symtab symtab;
1274// };
1275
1276// FIXME: Get from somewhere
1277static const int ModuleVersion = 7;
1278
1279void CGObjCMac::EmitModuleInfo() {
1280  uint64_t Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ModuleTy);
1281
1282  std::vector<llvm::Constant*> Values(4);
1283  Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
1284  Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1285  // This used to be the filename, now it is unused. <rdr://4327263>
1286  Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
1287  Values[3] = EmitModuleSymbols();
1288
1289  llvm::GlobalVariable *GV =
1290    new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
1291                             llvm::GlobalValue::InternalLinkage,
1292                             llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
1293                                                       Values),
1294                             "\01L_OBJC_MODULES",
1295                             &CGM.getModule());
1296  GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
1297  UsedGlobals.push_back(GV);
1298}
1299
1300llvm::Constant *CGObjCMac::EmitModuleSymbols() {
1301  std::vector<llvm::Constant*> Values(5);
1302  unsigned NumClasses = DefinedClasses.size();
1303  unsigned NumCategories = DefinedCategories.size();
1304
1305  Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1306  Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
1307  Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
1308  Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
1309
1310  // The runtime expects exactly the list of defined classes followed
1311  // by the list of defined categories, in a single array.
1312  std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
1313  for (unsigned i=0; i<NumClasses; i++)
1314    Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
1315                                                ObjCTypes.Int8PtrTy);
1316  for (unsigned i=0; i<NumCategories; i++)
1317    Symbols[NumClasses + i] =
1318      llvm::ConstantExpr::getBitCast(DefinedCategories[i],
1319                                     ObjCTypes.Int8PtrTy);
1320
1321  Values[4] =
1322    llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
1323                                                  NumClasses + NumCategories),
1324                             Symbols);
1325
1326  llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1327
1328  llvm::GlobalVariable *GV =
1329    new llvm::GlobalVariable(Init->getType(), false,
1330                             llvm::GlobalValue::InternalLinkage,
1331                             Init,
1332                             "\01L_OBJC_SYMBOLS",
1333                             &CGM.getModule());
1334  GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
1335  UsedGlobals.push_back(GV);
1336  return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
1337}
1338
1339llvm::Value *CGObjCMac::EmitClassRef(llvm::IRBuilder<> &Builder,
1340                                     const ObjCInterfaceDecl *ID) {
1341  llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
1342
1343  if (!Entry) {
1344    llvm::Constant *Casted =
1345      llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
1346                                     ObjCTypes.ClassPtrTy);
1347    Entry =
1348      new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
1349                               llvm::GlobalValue::InternalLinkage,
1350                               Casted, "\01L_OBJC_CLASS_REFERENCES_",
1351                               &CGM.getModule());
1352    Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
1353    UsedGlobals.push_back(Entry);
1354  }
1355
1356  return Builder.CreateLoad(Entry, false, "tmp");
1357}
1358
1359llvm::Value *CGObjCMac::EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
1360  llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
1361
1362  if (!Entry) {
1363    llvm::Constant *Casted =
1364      llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
1365                                     ObjCTypes.SelectorPtrTy);
1366    Entry =
1367      new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
1368                               llvm::GlobalValue::InternalLinkage,
1369                               Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
1370                               &CGM.getModule());
1371    Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
1372    UsedGlobals.push_back(Entry);
1373  }
1374
1375  return Builder.CreateLoad(Entry, false, "tmp");
1376}
1377
1378llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
1379  llvm::GlobalVariable *&Entry = ClassNames[Ident];
1380
1381  if (!Entry) {
1382    llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
1383    Entry =
1384      new llvm::GlobalVariable(C->getType(), false,
1385                               llvm::GlobalValue::InternalLinkage,
1386                               C, "\01L_OBJC_CLASS_NAME_",
1387                               &CGM.getModule());
1388    Entry->setSection("__TEXT,__cstring,cstring_literals");
1389    UsedGlobals.push_back(Entry);
1390  }
1391
1392  return getConstantGEP(Entry, 0, 0);
1393}
1394
1395llvm::Constant *CGObjCMac::GetMethodVarName(Selector Sel) {
1396  llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
1397
1398  if (!Entry) {
1399    llvm::Constant *C = llvm::ConstantArray::get(Sel.getName());
1400    Entry =
1401      new llvm::GlobalVariable(C->getType(), false,
1402                               llvm::GlobalValue::InternalLinkage,
1403                               C, "\01L_OBJC_METH_VAR_NAME_",
1404                               &CGM.getModule());
1405    Entry->setSection("__TEXT,__cstring,cstring_literals");
1406    UsedGlobals.push_back(Entry);
1407  }
1408
1409  return getConstantGEP(Entry, 0, 0);
1410}
1411
1412// FIXME: Merge into a single cstring creation function.
1413llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) {
1414  return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
1415}
1416
1417// FIXME: Merge into a single cstring creation function.
1418llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) {
1419  return GetMethodVarName(&CGM.getContext().Idents.get(Name));
1420}
1421
1422llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) {
1423  llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
1424
1425  if (!Entry) {
1426    llvm::Constant *C = llvm::ConstantArray::get(Name);
1427    Entry =
1428      new llvm::GlobalVariable(C->getType(), false,
1429                               llvm::GlobalValue::InternalLinkage,
1430                               C, "\01L_OBJC_METH_VAR_TYPE_",
1431                               &CGM.getModule());
1432    Entry->setSection("__TEXT,__cstring,cstring_literals");
1433    UsedGlobals.push_back(Entry);
1434  }
1435
1436  return getConstantGEP(Entry, 0, 0);
1437}
1438
1439// FIXME: Merge into a single cstring creation function.
1440llvm::Constant *CGObjCMac::GetMethodVarType(ObjCMethodDecl *D) {
1441  std::string TypeStr;
1442  CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr);
1443  return GetMethodVarType(TypeStr);
1444}
1445
1446// FIXME: Merge into a single cstring creation function.
1447llvm::Constant *CGObjCMac::GetPropertyName(IdentifierInfo *Ident) {
1448  llvm::GlobalVariable *&Entry = PropertyNames[Ident];
1449
1450  if (!Entry) {
1451    llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
1452    Entry =
1453      new llvm::GlobalVariable(C->getType(), false,
1454                               llvm::GlobalValue::InternalLinkage,
1455                               C, "\01L_OBJC_PROP_NAME_ATTR_",
1456                               &CGM.getModule());
1457    Entry->setSection("__TEXT,__cstring,cstring_literals");
1458    UsedGlobals.push_back(Entry);
1459  }
1460
1461  return getConstantGEP(Entry, 0, 0);
1462}
1463
1464// FIXME: Merge into a single cstring creation function.
1465llvm::Constant *CGObjCMac::GetPropertyType(const ObjCPropertyDecl *PD) {
1466  std::string TypeStr("MOOO!");
1467  //CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr);
1468  return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
1469}
1470
1471void CGObjCMac::GetNameForMethod(const ObjCMethodDecl *D,
1472                                 std::string &NameOut) {
1473  // FIXME: Find the mangling GCC uses.
1474  std::stringstream s;
1475  s << (D->isInstance() ? "-" : "+");
1476  s << "[";
1477  s << D->getClassInterface()->getName();
1478  s << " ";
1479  s << D->getSelector().getName();
1480  s << "]";
1481  NameOut = s.str();
1482}
1483
1484void CGObjCMac::FinishModule() {
1485  EmitModuleInfo();
1486
1487  std::vector<llvm::Constant*> Used;
1488
1489  for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
1490         e = UsedGlobals.end(); i != e; ++i) {
1491    Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
1492  }
1493
1494  llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
1495  llvm::GlobalValue *GV =
1496    new llvm::GlobalVariable(AT, false,
1497                             llvm::GlobalValue::AppendingLinkage,
1498                             llvm::ConstantArray::get(AT, Used),
1499                             "llvm.used",
1500                             &CGM.getModule());
1501
1502  GV->setSection("llvm.metadata");
1503}
1504
1505/* *** */
1506
1507ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
1508  : CGM(cgm),
1509    CFStringType(0),
1510    CFConstantStringClassReference(0),
1511    MessageSendFn(0)
1512{
1513  CodeGen::CodeGenTypes &Types = CGM.getTypes();
1514  ASTContext &Ctx = CGM.getContext();
1515
1516  ShortTy = Types.ConvertType(Ctx.ShortTy);
1517  IntTy = Types.ConvertType(Ctx.IntTy);
1518  LongTy = Types.ConvertType(Ctx.LongTy);
1519  Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1520
1521  ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
1522  SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
1523
1524  // FIXME: It would be nice to unify this with the opaque type, so
1525  // that the IR comes out a bit cleaner.
1526  const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
1527  ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
1528
1529  MethodDescriptionTy =
1530    llvm::StructType::get(SelectorPtrTy,
1531                          Int8PtrTy,
1532                          NULL);
1533  CGM.getModule().addTypeName("struct._objc_method_description",
1534                              MethodDescriptionTy);
1535
1536  MethodDescriptionListTy =
1537    llvm::StructType::get(IntTy,
1538                          llvm::ArrayType::get(MethodDescriptionTy, 0),
1539                          NULL);
1540  CGM.getModule().addTypeName("struct._objc_method_description_list",
1541                              MethodDescriptionListTy);
1542  MethodDescriptionListPtrTy =
1543    llvm::PointerType::getUnqual(MethodDescriptionListTy);
1544
1545  PropertyTy = llvm::StructType::get(Int8PtrTy,
1546                                     Int8PtrTy,
1547                                     NULL);
1548  CGM.getModule().addTypeName("struct._objc_property",
1549                              PropertyTy);
1550
1551  PropertyListTy = llvm::StructType::get(IntTy,
1552                                         IntTy,
1553                                         llvm::ArrayType::get(PropertyTy, 0),
1554                                         NULL);
1555  CGM.getModule().addTypeName("struct._objc_property_list",
1556                              PropertyListTy);
1557  PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
1558
1559  // Protocol description structures
1560
1561  ProtocolExtensionTy =
1562    llvm::StructType::get(Types.ConvertType(Ctx.IntTy),
1563                          llvm::PointerType::getUnqual(MethodDescriptionListTy),
1564                          llvm::PointerType::getUnqual(MethodDescriptionListTy),
1565                          PropertyListPtrTy,
1566                          NULL);
1567  CGM.getModule().addTypeName("struct._objc_protocol_extension",
1568                              ProtocolExtensionTy);
1569  ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
1570
1571  // Handle recursive construction of Protocl and ProtocolList types
1572
1573  llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
1574  llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
1575
1576  T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
1577                            LongTy,
1578                            llvm::ArrayType::get(ProtocolTyHolder, 0),
1579                            NULL);
1580  cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
1581
1582  T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolExtensionTy),
1583                            Int8PtrTy,
1584                            llvm::PointerType::getUnqual(ProtocolListTyHolder),
1585                            MethodDescriptionListPtrTy,
1586                            MethodDescriptionListPtrTy,
1587                            NULL);
1588  cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
1589
1590  ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
1591  CGM.getModule().addTypeName("struct._objc_protocol_list",
1592                              ProtocolListTy);
1593  ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
1594
1595  ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
1596  CGM.getModule().addTypeName("struct.__objc_protocol", ProtocolTy);
1597  ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
1598
1599  // Class description structures
1600
1601  IvarTy = llvm::StructType::get(Int8PtrTy,
1602                                 Int8PtrTy,
1603                                 IntTy,
1604                                 NULL);
1605  CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
1606
1607  IvarListTy = llvm::OpaqueType::get();
1608  CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
1609  IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
1610
1611  MethodTy = llvm::StructType::get(SelectorPtrTy,
1612                                   Int8PtrTy,
1613                                   Int8PtrTy,
1614                                   NULL);
1615  CGM.getModule().addTypeName("struct._objc_method", MethodTy);
1616
1617  MethodListTy = llvm::OpaqueType::get();
1618  CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
1619  MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
1620
1621  CacheTy = llvm::OpaqueType::get();
1622  CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
1623  CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
1624
1625  ClassExtensionTy =
1626    llvm::StructType::get(IntTy,
1627                          Int8PtrTy,
1628                          PropertyListPtrTy,
1629                          NULL);
1630  CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
1631  ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
1632
1633  llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
1634
1635  T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
1636                            llvm::PointerType::getUnqual(ClassTyHolder),
1637                            Int8PtrTy,
1638                            LongTy,
1639                            LongTy,
1640                            LongTy,
1641                            IvarListPtrTy,
1642                            MethodListPtrTy,
1643                            CachePtrTy,
1644                            ProtocolListPtrTy,
1645                            Int8PtrTy,
1646                            ClassExtensionPtrTy,
1647                            NULL);
1648  cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
1649
1650  ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
1651  CGM.getModule().addTypeName("struct._objc_class", ClassTy);
1652  ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
1653
1654  CategoryTy = llvm::StructType::get(Int8PtrTy,
1655                                     Int8PtrTy,
1656                                     MethodListPtrTy,
1657                                     MethodListPtrTy,
1658                                     ProtocolListPtrTy,
1659                                     IntTy,
1660                                     PropertyListPtrTy,
1661                                     NULL);
1662  CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
1663
1664  // Global metadata structures
1665
1666  SymtabTy = llvm::StructType::get(LongTy,
1667                                   SelectorPtrTy,
1668                                   ShortTy,
1669                                   ShortTy,
1670                                   llvm::ArrayType::get(Int8PtrTy, 0),
1671                                   NULL);
1672  CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
1673  SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
1674
1675  ModuleTy =
1676    llvm::StructType::get(LongTy,
1677                          LongTy,
1678                          Int8PtrTy,
1679                          SymtabPtrTy,
1680                          NULL);
1681  CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
1682}
1683
1684ObjCTypesHelper::~ObjCTypesHelper() {
1685}
1686
1687const llvm::StructType *ObjCTypesHelper::getCFStringType() {
1688  if (!CFStringType) {
1689    CFStringType =
1690      llvm::StructType::get(llvm::PointerType::getUnqual(llvm::Type::Int32Ty),
1691                            llvm::Type::Int32Ty,
1692                            Int8PtrTy,
1693                            LongTy,
1694                            NULL);
1695
1696    CGM.getModule().addTypeName("struct.__builtin_CFString", CFStringType);
1697  }
1698
1699  return CFStringType;
1700}
1701
1702llvm::Constant *ObjCTypesHelper::getCFConstantStringClassReference() {
1703  if (!CFConstantStringClassReference) {
1704    llvm::GlobalValue *GV =
1705      new llvm::GlobalVariable(llvm::ArrayType::get(llvm::Type::Int32Ty, 0),
1706                               false,
1707                               llvm::GlobalValue::ExternalLinkage,
1708                               0, "__CFConstantStringClassReference",
1709                                                     &CGM.getModule());
1710
1711    // Decay to pointer.
1712    CFConstantStringClassReference = getConstantGEP(GV, 0, 0);
1713  }
1714
1715  return CFConstantStringClassReference;
1716}
1717
1718llvm::Function *ObjCTypesHelper::getMessageSendFn() {
1719  if (!MessageSendFn) {
1720    std::vector<const llvm::Type*> Params;
1721    Params.push_back(ObjectPtrTy);
1722    Params.push_back(SelectorPtrTy);
1723    MessageSendFn = llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1724                                                                   Params,
1725                                                                   true),
1726                                           llvm::Function::ExternalLinkage,
1727                                           "objc_msgSend",
1728                                           &CGM.getModule());
1729  }
1730
1731  return MessageSendFn;
1732}
1733
1734/* *** */
1735
1736CodeGen::CGObjCRuntime *
1737CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
1738  return new CGObjCMac(CGM);
1739}
1740