1193326Sed//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10221345Sdim// This provides Objective-C code generation targeting the GNU runtime.  The
11193326Sed// class in this file generates structures used by the GNU Objective-C runtime
12193326Sed// library.  These structures are defined in objc/objc.h and objc/objc-api.h in
13193326Sed// the GNU runtime distribution.
14193326Sed//
15193326Sed//===----------------------------------------------------------------------===//
16193326Sed
17193326Sed#include "CGObjCRuntime.h"
18249423Sdim#include "CGCleanup.h"
19249423Sdim#include "CodeGenFunction.h"
20193326Sed#include "CodeGenModule.h"
21193326Sed#include "clang/AST/ASTContext.h"
22193326Sed#include "clang/AST/Decl.h"
23193326Sed#include "clang/AST/DeclObjC.h"
24193326Sed#include "clang/AST/RecordLayout.h"
25193326Sed#include "clang/AST/StmtObjC.h"
26249423Sdim#include "clang/Basic/FileManager.h"
27221345Sdim#include "clang/Basic/SourceManager.h"
28193326Sed#include "llvm/ADT/SmallVector.h"
29193326Sed#include "llvm/ADT/StringMap.h"
30276479Sdim#include "llvm/IR/CallSite.h"
31249423Sdim#include "llvm/IR/DataLayout.h"
32249423Sdim#include "llvm/IR/Intrinsics.h"
33249423Sdim#include "llvm/IR/LLVMContext.h"
34249423Sdim#include "llvm/IR/Module.h"
35193326Sed#include "llvm/Support/Compiler.h"
36226633Sdim#include <cstdarg>
37193326Sed
38193326Sed
39193326Sedusing namespace clang;
40193326Sedusing namespace CodeGen;
41193326Sed
42193326Sed
43193326Sednamespace {
44221345Sdim/// Class that lazily initialises the runtime function.  Avoids inserting the
45221345Sdim/// types and the function declaration into a module if they're not used, and
46221345Sdim/// avoids constructing the type more than once if it's used more than once.
47221345Sdimclass LazyRuntimeFunction {
48221345Sdim  CodeGenModule *CGM;
49288943Sdim  llvm::FunctionType *FTy;
50221345Sdim  const char *FunctionName;
51223017Sdim  llvm::Constant *Function;
52288943Sdim
53288943Sdimpublic:
54288943Sdim  /// Constructor leaves this class uninitialized, because it is intended to
55288943Sdim  /// be used as a field in another class and not all of the types that are
56288943Sdim  /// used as arguments will necessarily be available at construction time.
57288943Sdim  LazyRuntimeFunction()
58276479Sdim      : CGM(nullptr), FunctionName(nullptr), Function(nullptr) {}
59221345Sdim
60288943Sdim  /// Initialises the lazy function with the name, return type, and the types
61288943Sdim  /// of the arguments.
62288943Sdim  LLVM_END_WITH_NULL
63288943Sdim  void init(CodeGenModule *Mod, const char *name, llvm::Type *RetTy, ...) {
64288943Sdim    CGM = Mod;
65288943Sdim    FunctionName = name;
66288943Sdim    Function = nullptr;
67288943Sdim    std::vector<llvm::Type *> ArgTys;
68288943Sdim    va_list Args;
69288943Sdim    va_start(Args, RetTy);
70288943Sdim    while (llvm::Type *ArgTy = va_arg(Args, llvm::Type *))
71288943Sdim      ArgTys.push_back(ArgTy);
72288943Sdim    va_end(Args);
73288943Sdim    FTy = llvm::FunctionType::get(RetTy, ArgTys, false);
74288943Sdim  }
75223017Sdim
76288943Sdim  llvm::FunctionType *getType() { return FTy; }
77288943Sdim
78288943Sdim  /// Overloaded cast operator, allows the class to be implicitly cast to an
79288943Sdim  /// LLVM constant.
80288943Sdim  operator llvm::Constant *() {
81288943Sdim    if (!Function) {
82288943Sdim      if (!FunctionName)
83288943Sdim        return nullptr;
84288943Sdim      Function =
85288943Sdim          cast<llvm::Constant>(CGM->CreateRuntimeFunction(FTy, FunctionName));
86288943Sdim    }
87288943Sdim    return Function;
88288943Sdim  }
89288943Sdim  operator llvm::Function *() {
90288943Sdim    return cast<llvm::Function>((llvm::Constant *)*this);
91288943Sdim  }
92221345Sdim};
93221345Sdim
94221345Sdim
95221345Sdim/// GNU Objective-C runtime code generation.  This class implements the parts of
96239462Sdim/// Objective-C support that are specific to the GNU family of runtimes (GCC,
97239462Sdim/// GNUstep and ObjFW).
98221345Sdimclass CGObjCGNU : public CGObjCRuntime {
99221345Sdimprotected:
100221345Sdim  /// The LLVM module into which output is inserted
101193326Sed  llvm::Module &TheModule;
102221345Sdim  /// strut objc_super.  Used for sending messages to super.  This structure
103221345Sdim  /// contains the receiver (object) and the expected class.
104226633Sdim  llvm::StructType *ObjCSuperTy;
105221345Sdim  /// struct objc_super*.  The type of the argument to the superclass message
106221345Sdim  /// lookup functions.
107226633Sdim  llvm::PointerType *PtrToObjCSuperTy;
108221345Sdim  /// LLVM type for selectors.  Opaque pointer (i8*) unless a header declaring
109221345Sdim  /// SEL is included in a header somewhere, in which case it will be whatever
110221345Sdim  /// type is declared in that header, most likely {i8*, i8*}.
111224145Sdim  llvm::PointerType *SelectorTy;
112221345Sdim  /// LLVM i8 type.  Cached here to avoid repeatedly getting it in all of the
113221345Sdim  /// places where it's used
114226633Sdim  llvm::IntegerType *Int8Ty;
115221345Sdim  /// Pointer to i8 - LLVM type of char*, for all of the places where the
116221345Sdim  /// runtime needs to deal with C strings.
117224145Sdim  llvm::PointerType *PtrToInt8Ty;
118221345Sdim  /// Instance Method Pointer type.  This is a pointer to a function that takes,
119221345Sdim  /// at a minimum, an object and a selector, and is the generic type for
120221345Sdim  /// Objective-C methods.  Due to differences between variadic / non-variadic
121221345Sdim  /// calling conventions, it must always be cast to the correct type before
122221345Sdim  /// actually being used.
123224145Sdim  llvm::PointerType *IMPTy;
124221345Sdim  /// Type of an untyped Objective-C object.  Clang treats id as a built-in type
125221345Sdim  /// when compiling Objective-C code, so this may be an opaque pointer (i8*),
126221345Sdim  /// but if the runtime header declaring it is included then it may be a
127221345Sdim  /// pointer to a structure.
128224145Sdim  llvm::PointerType *IdTy;
129221345Sdim  /// Pointer to a pointer to an Objective-C object.  Used in the new ABI
130221345Sdim  /// message lookup function and some GC-related functions.
131226633Sdim  llvm::PointerType *PtrToIdTy;
132221345Sdim  /// The clang type of id.  Used when using the clang CGCall infrastructure to
133221345Sdim  /// call Objective-C methods.
134204643Srdivacky  CanQualType ASTIdTy;
135221345Sdim  /// LLVM type for C int type.
136224145Sdim  llvm::IntegerType *IntTy;
137221345Sdim  /// LLVM type for an opaque pointer.  This is identical to PtrToInt8Ty, but is
138221345Sdim  /// used in the code to document the difference between i8* meaning a pointer
139221345Sdim  /// to a C string and i8* meaning a pointer to some opaque type.
140224145Sdim  llvm::PointerType *PtrTy;
141221345Sdim  /// LLVM type for C long type.  The runtime uses this in a lot of places where
142221345Sdim  /// it should be using intptr_t, but we can't fix this without breaking
143221345Sdim  /// compatibility with GCC...
144224145Sdim  llvm::IntegerType *LongTy;
145221345Sdim  /// LLVM type for C size_t.  Used in various runtime data structures.
146226633Sdim  llvm::IntegerType *SizeTy;
147226633Sdim  /// LLVM type for C intptr_t.
148226633Sdim  llvm::IntegerType *IntPtrTy;
149221345Sdim  /// LLVM type for C ptrdiff_t.  Mainly used in property accessor functions.
150226633Sdim  llvm::IntegerType *PtrDiffTy;
151221345Sdim  /// LLVM type for C int*.  Used for GCC-ABI-compatible non-fragile instance
152221345Sdim  /// variables.
153226633Sdim  llvm::PointerType *PtrToIntTy;
154221345Sdim  /// LLVM type for Objective-C BOOL type.
155226633Sdim  llvm::Type *BoolTy;
156226633Sdim  /// 32-bit integer type, to save us needing to look it up every time it's used.
157226633Sdim  llvm::IntegerType *Int32Ty;
158226633Sdim  /// 64-bit integer type, to save us needing to look it up every time it's used.
159226633Sdim  llvm::IntegerType *Int64Ty;
160221345Sdim  /// Metadata kind used to tie method lookups to message sends.  The GNUstep
161221345Sdim  /// runtime provides some LLVM passes that can use this to do things like
162221345Sdim  /// automatic IMP caching and speculative inlining.
163221345Sdim  unsigned msgSendMDKind;
164221345Sdim  /// Helper function that generates a constant string and returns a pointer to
165221345Sdim  /// the start of the string.  The result of this function can be used anywhere
166221345Sdim  /// where the C code specifies const char*.
167221345Sdim  llvm::Constant *MakeConstantString(const std::string &Str,
168221345Sdim                                     const std::string &Name="") {
169296417Sdim    ConstantAddress Array = CGM.GetAddrOfConstantCString(Str, Name.c_str());
170296417Sdim    return llvm::ConstantExpr::getGetElementPtr(Array.getElementType(),
171296417Sdim                                                Array.getPointer(), Zeros);
172221345Sdim  }
173221345Sdim  /// Emits a linkonce_odr string, whose name is the prefix followed by the
174221345Sdim  /// string value.  This allows the linker to combine the strings between
175221345Sdim  /// different modules.  Used for EH typeinfo names, selector strings, and a
176221345Sdim  /// few other things.
177221345Sdim  llvm::Constant *ExportUniqueString(const std::string &Str,
178221345Sdim                                     const std::string prefix) {
179221345Sdim    std::string name = prefix + Str;
180288943Sdim    auto *ConstStr = TheModule.getGlobalVariable(name);
181221345Sdim    if (!ConstStr) {
182234353Sdim      llvm::Constant *value = llvm::ConstantDataArray::getString(VMContext,Str);
183221345Sdim      ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true,
184221345Sdim              llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str);
185221345Sdim    }
186288943Sdim    return llvm::ConstantExpr::getGetElementPtr(ConstStr->getValueType(),
187288943Sdim                                                ConstStr, Zeros);
188221345Sdim  }
189221345Sdim  /// Generates a global structure, initialized by the elements in the vector.
190221345Sdim  /// The element types must match the types of the structure elements in the
191221345Sdim  /// first argument.
192226633Sdim  llvm::GlobalVariable *MakeGlobal(llvm::StructType *Ty,
193249423Sdim                                   ArrayRef<llvm::Constant *> V,
194296417Sdim                                   CharUnits Align,
195226633Sdim                                   StringRef Name="",
196221345Sdim                                   llvm::GlobalValue::LinkageTypes linkage
197221345Sdim                                         =llvm::GlobalValue::InternalLinkage) {
198221345Sdim    llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
199296417Sdim    auto GV = new llvm::GlobalVariable(TheModule, Ty, false,
200296417Sdim                                       linkage, C, Name);
201296417Sdim    GV->setAlignment(Align.getQuantity());
202296417Sdim    return GV;
203221345Sdim  }
204221345Sdim  /// Generates a global array.  The vector must contain the same number of
205221345Sdim  /// elements that the array type declares, of the type specified as the array
206221345Sdim  /// element type.
207226633Sdim  llvm::GlobalVariable *MakeGlobal(llvm::ArrayType *Ty,
208249423Sdim                                   ArrayRef<llvm::Constant *> V,
209296417Sdim                                   CharUnits Align,
210226633Sdim                                   StringRef Name="",
211221345Sdim                                   llvm::GlobalValue::LinkageTypes linkage
212221345Sdim                                         =llvm::GlobalValue::InternalLinkage) {
213221345Sdim    llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
214296417Sdim    auto GV = new llvm::GlobalVariable(TheModule, Ty, false,
215296417Sdim                                       linkage, C, Name);
216296417Sdim    GV->setAlignment(Align.getQuantity());
217296417Sdim    return GV;
218221345Sdim  }
219221345Sdim  /// Generates a global array, inferring the array type from the specified
220221345Sdim  /// element type and the size of the initialiser.
221226633Sdim  llvm::GlobalVariable *MakeGlobalArray(llvm::Type *Ty,
222249423Sdim                                        ArrayRef<llvm::Constant *> V,
223296417Sdim                                        CharUnits Align,
224226633Sdim                                        StringRef Name="",
225221345Sdim                                        llvm::GlobalValue::LinkageTypes linkage
226221345Sdim                                         =llvm::GlobalValue::InternalLinkage) {
227221345Sdim    llvm::ArrayType *ArrayTy = llvm::ArrayType::get(Ty, V.size());
228296417Sdim    return MakeGlobal(ArrayTy, V, Align, Name, linkage);
229221345Sdim  }
230243830Sdim  /// Returns a property name and encoding string.
231243830Sdim  llvm::Constant *MakePropertyEncodingString(const ObjCPropertyDecl *PD,
232243830Sdim                                             const Decl *Container) {
233249423Sdim    const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime;
234243830Sdim    if ((R.getKind() == ObjCRuntime::GNUstep) &&
235243830Sdim        (R.getVersion() >= VersionTuple(1, 6))) {
236243830Sdim      std::string NameAndAttributes;
237243830Sdim      std::string TypeStr;
238243830Sdim      CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
239243830Sdim      NameAndAttributes += '\0';
240243830Sdim      NameAndAttributes += TypeStr.length() + 3;
241243830Sdim      NameAndAttributes += TypeStr;
242243830Sdim      NameAndAttributes += '\0';
243243830Sdim      NameAndAttributes += PD->getNameAsString();
244296417Sdim      return MakeConstantString(NameAndAttributes);
245243830Sdim    }
246243830Sdim    return MakeConstantString(PD->getNameAsString());
247243830Sdim  }
248249423Sdim  /// Push the property attributes into two structure fields.
249249423Sdim  void PushPropertyAttributes(std::vector<llvm::Constant*> &Fields,
250249423Sdim      ObjCPropertyDecl *property, bool isSynthesized=true, bool
251249423Sdim      isDynamic=true) {
252249423Sdim    int attrs = property->getPropertyAttributes();
253249423Sdim    // For read-only properties, clear the copy and retain flags
254249423Sdim    if (attrs & ObjCPropertyDecl::OBJC_PR_readonly) {
255249423Sdim      attrs &= ~ObjCPropertyDecl::OBJC_PR_copy;
256249423Sdim      attrs &= ~ObjCPropertyDecl::OBJC_PR_retain;
257249423Sdim      attrs &= ~ObjCPropertyDecl::OBJC_PR_weak;
258249423Sdim      attrs &= ~ObjCPropertyDecl::OBJC_PR_strong;
259249423Sdim    }
260249423Sdim    // The first flags field has the same attribute values as clang uses internally
261249423Sdim    Fields.push_back(llvm::ConstantInt::get(Int8Ty, attrs & 0xff));
262249423Sdim    attrs >>= 8;
263249423Sdim    attrs <<= 2;
264249423Sdim    // For protocol properties, synthesized and dynamic have no meaning, so we
265249423Sdim    // reuse these flags to indicate that this is a protocol property (both set
266249423Sdim    // has no meaning, as a property can't be both synthesized and dynamic)
267249423Sdim    attrs |= isSynthesized ? (1<<0) : 0;
268249423Sdim    attrs |= isDynamic ? (1<<1) : 0;
269249423Sdim    // The second field is the next four fields left shifted by two, with the
270249423Sdim    // low bit set to indicate whether the field is synthesized or dynamic.
271249423Sdim    Fields.push_back(llvm::ConstantInt::get(Int8Ty, attrs & 0xff));
272249423Sdim    // Two padding fields
273249423Sdim    Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
274249423Sdim    Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
275249423Sdim  }
276221345Sdim  /// Ensures that the value has the required type, by inserting a bitcast if
277221345Sdim  /// required.  This function lets us avoid inserting bitcasts that are
278221345Sdim  /// redundant.
279249423Sdim  llvm::Value* EnforceType(CGBuilderTy &B, llvm::Value *V, llvm::Type *Ty) {
280221345Sdim    if (V->getType() == Ty) return V;
281221345Sdim    return B.CreateBitCast(V, Ty);
282221345Sdim  }
283296417Sdim  Address EnforceType(CGBuilderTy &B, Address V, llvm::Type *Ty) {
284296417Sdim    if (V.getType() == Ty) return V;
285296417Sdim    return B.CreateBitCast(V, Ty);
286296417Sdim  }
287221345Sdim  // Some zeros used for GEPs in lots of places.
288221345Sdim  llvm::Constant *Zeros[2];
289221345Sdim  /// Null pointer value.  Mainly used as a terminator in various arrays.
290221345Sdim  llvm::Constant *NULLPtr;
291221345Sdim  /// LLVM context.
292221345Sdim  llvm::LLVMContext &VMContext;
293221345Sdimprivate:
294221345Sdim  /// Placeholder for the class.  Lots of things refer to the class before we've
295221345Sdim  /// actually emitted it.  We use this alias as a placeholder, and then replace
296221345Sdim  /// it with a pointer to the class structure before finally emitting the
297221345Sdim  /// module.
298193326Sed  llvm::GlobalAlias *ClassPtrAlias;
299221345Sdim  /// Placeholder for the metaclass.  Lots of things refer to the class before
300221345Sdim  /// we've / actually emitted it.  We use this alias as a placeholder, and then
301221345Sdim  /// replace / it with a pointer to the metaclass structure before finally
302221345Sdim  /// emitting the / module.
303193326Sed  llvm::GlobalAlias *MetaClassPtrAlias;
304221345Sdim  /// All of the classes that have been generated for this compilation units.
305193326Sed  std::vector<llvm::Constant*> Classes;
306221345Sdim  /// All of the categories that have been generated for this compilation units.
307193326Sed  std::vector<llvm::Constant*> Categories;
308221345Sdim  /// All of the Objective-C constant strings that have been generated for this
309221345Sdim  /// compilation units.
310193326Sed  std::vector<llvm::Constant*> ConstantStrings;
311221345Sdim  /// Map from string values to Objective-C constant strings in the output.
312221345Sdim  /// Used to prevent emitting Objective-C strings more than once.  This should
313221345Sdim  /// not be required at all - CodeGenModule should manage this list.
314203955Srdivacky  llvm::StringMap<llvm::Constant*> ObjCStrings;
315221345Sdim  /// All of the protocols that have been declared.
316193326Sed  llvm::StringMap<llvm::Constant*> ExistingProtocols;
317221345Sdim  /// For each variant of a selector, we store the type encoding and a
318221345Sdim  /// placeholder value.  For an untyped selector, the type will be the empty
319221345Sdim  /// string.  Selector references are all done via the module's selector table,
320221345Sdim  /// so we create an alias as a placeholder and then replace it with the real
321221345Sdim  /// value later.
322221345Sdim  typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector;
323221345Sdim  /// Type of the selector map.  This is roughly equivalent to the structure
324221345Sdim  /// used in the GNUstep runtime, which maintains a list of all of the valid
325221345Sdim  /// types for a selector in a table.
326226633Sdim  typedef llvm::DenseMap<Selector, SmallVector<TypedSelector, 2> >
327221345Sdim    SelectorMap;
328221345Sdim  /// A map from selectors to selector types.  This allows us to emit all
329221345Sdim  /// selectors of the same name and type together.
330221345Sdim  SelectorMap SelectorTable;
331221345Sdim
332221345Sdim  /// Selectors related to memory management.  When compiling in GC mode, we
333221345Sdim  /// omit these.
334203955Srdivacky  Selector RetainSel, ReleaseSel, AutoreleaseSel;
335221345Sdim  /// Runtime functions used for memory management in GC mode.  Note that clang
336221345Sdim  /// supports code generation for calling these functions, but neither GNU
337221345Sdim  /// runtime actually supports this API properly yet.
338221345Sdim  LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn,
339221345Sdim    WeakAssignFn, GlobalAssignFn;
340221345Sdim
341234353Sdim  typedef std::pair<std::string, std::string> ClassAliasPair;
342234353Sdim  /// All classes that have aliases set for them.
343234353Sdim  std::vector<ClassAliasPair> ClassAliases;
344234353Sdim
345221345Sdimprotected:
346221345Sdim  /// Function used for throwing Objective-C exceptions.
347221345Sdim  LazyRuntimeFunction ExceptionThrowFn;
348239462Sdim  /// Function used for rethrowing exceptions, used at the end of \@finally or
349239462Sdim  /// \@synchronize blocks.
350221345Sdim  LazyRuntimeFunction ExceptionReThrowFn;
351221345Sdim  /// Function called when entering a catch function.  This is required for
352221345Sdim  /// differentiating Objective-C exceptions and foreign exceptions.
353221345Sdim  LazyRuntimeFunction EnterCatchFn;
354221345Sdim  /// Function called when exiting from a catch block.  Used to do exception
355221345Sdim  /// cleanup.
356221345Sdim  LazyRuntimeFunction ExitCatchFn;
357239462Sdim  /// Function called when entering an \@synchronize block.  Acquires the lock.
358221345Sdim  LazyRuntimeFunction SyncEnterFn;
359239462Sdim  /// Function called when exiting an \@synchronize block.  Releases the lock.
360221345Sdim  LazyRuntimeFunction SyncExitFn;
361221345Sdim
362193326Sedprivate:
363221345Sdim
364221345Sdim  /// Function called if fast enumeration detects that the collection is
365221345Sdim  /// modified during the update.
366221345Sdim  LazyRuntimeFunction EnumerationMutationFn;
367221345Sdim  /// Function for implementing synthesized property getters that return an
368221345Sdim  /// object.
369221345Sdim  LazyRuntimeFunction GetPropertyFn;
370221345Sdim  /// Function for implementing synthesized property setters that return an
371221345Sdim  /// object.
372221345Sdim  LazyRuntimeFunction SetPropertyFn;
373221345Sdim  /// Function used for non-object declared property getters.
374221345Sdim  LazyRuntimeFunction GetStructPropertyFn;
375221345Sdim  /// Function used for non-object declared property setters.
376221345Sdim  LazyRuntimeFunction SetStructPropertyFn;
377221345Sdim
378221345Sdim  /// The version of the runtime that this class targets.  Must match the
379221345Sdim  /// version in the runtime.
380223017Sdim  int RuntimeVersion;
381221345Sdim  /// The version of the protocol class.  Used to differentiate between ObjC1
382221345Sdim  /// and ObjC2 protocols.  Objective-C 1 protocols can not contain optional
383221345Sdim  /// components and can not contain declared properties.  We always emit
384221345Sdim  /// Objective-C 2 property structures, but we have to pretend that they're
385221345Sdim  /// Objective-C 1 property structures when targeting the GCC runtime or it
386221345Sdim  /// will abort.
387221345Sdim  const int ProtocolVersion;
388221345Sdimprivate:
389221345Sdim  /// Generates an instance variable list structure.  This is a structure
390221345Sdim  /// containing a size and an array of structures containing instance variable
391221345Sdim  /// metadata.  This is used purely for introspection in the fragile ABI.  In
392221345Sdim  /// the non-fragile ABI, it's used for instance variable fixup.
393234353Sdim  llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
394234353Sdim                                   ArrayRef<llvm::Constant *> IvarTypes,
395234353Sdim                                   ArrayRef<llvm::Constant *> IvarOffsets);
396221345Sdim  /// Generates a method list structure.  This is a structure containing a size
397221345Sdim  /// and an array of structures containing method metadata.
398221345Sdim  ///
399221345Sdim  /// This structure is used by both classes and categories, and contains a next
400221345Sdim  /// pointer allowing them to be chained together in a linked list.
401280031Sdim  llvm::Constant *GenerateMethodList(StringRef ClassName,
402280031Sdim      StringRef CategoryName,
403234353Sdim      ArrayRef<Selector> MethodSels,
404234353Sdim      ArrayRef<llvm::Constant *> MethodTypes,
405193326Sed      bool isClassMethodList);
406239462Sdim  /// Emits an empty protocol.  This is used for \@protocol() where no protocol
407221345Sdim  /// is found.  The runtime will (hopefully) fix up the pointer to refer to the
408221345Sdim  /// real protocol.
409193326Sed  llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
410221345Sdim  /// Generates a list of property metadata structures.  This follows the same
411221345Sdim  /// pattern as method and instance variable metadata lists.
412198092Srdivacky  llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID,
413226633Sdim        SmallVectorImpl<Selector> &InstanceMethodSels,
414226633Sdim        SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes);
415221345Sdim  /// Generates a list of referenced protocols.  Classes, categories, and
416221345Sdim  /// protocols all use this structure.
417234353Sdim  llvm::Constant *GenerateProtocolList(ArrayRef<std::string> Protocols);
418221345Sdim  /// To ensure that all protocols are seen by the runtime, we add a category on
419221345Sdim  /// a class defined in the runtime, declaring no methods, but adopting the
420221345Sdim  /// protocols.  This is a horribly ugly hack, but it allows us to collect all
421221345Sdim  /// of the protocols without changing the ABI.
422249423Sdim  void GenerateProtocolHolderCategory();
423221345Sdim  /// Generates a class structure.
424193326Sed  llvm::Constant *GenerateClassStructure(
425193326Sed      llvm::Constant *MetaClass,
426193326Sed      llvm::Constant *SuperClass,
427193326Sed      unsigned info,
428193326Sed      const char *Name,
429193326Sed      llvm::Constant *Version,
430193326Sed      llvm::Constant *InstanceSize,
431193326Sed      llvm::Constant *IVars,
432193326Sed      llvm::Constant *Methods,
433198092Srdivacky      llvm::Constant *Protocols,
434198092Srdivacky      llvm::Constant *IvarOffsets,
435207619Srdivacky      llvm::Constant *Properties,
436226633Sdim      llvm::Constant *StrongIvarBitmap,
437226633Sdim      llvm::Constant *WeakIvarBitmap,
438207619Srdivacky      bool isMeta=false);
439221345Sdim  /// Generates a method list.  This is used by protocols to define the required
440221345Sdim  /// and optional methods.
441193326Sed  llvm::Constant *GenerateProtocolMethodList(
442234353Sdim      ArrayRef<llvm::Constant *> MethodNames,
443234353Sdim      ArrayRef<llvm::Constant *> MethodTypes);
444221345Sdim  /// Returns a selector with the specified type encoding.  An empty string is
445221345Sdim  /// used to return an untyped selector (with the types field set to NULL).
446249423Sdim  llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel,
447296417Sdim                           const std::string &TypeEncoding);
448221345Sdim  /// Returns the variable used to store the offset of an instance variable.
449193326Sed  llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
450193326Sed      const ObjCIvarDecl *Ivar);
451221345Sdim  /// Emits a reference to a class.  This allows the linker to object if there
452221345Sdim  /// is no class of the matching name.
453239462Sdimprotected:
454194613Sed  void EmitClassRef(const std::string &className);
455224145Sdim  /// Emits a pointer to the named class
456249423Sdim  virtual llvm::Value *GetClassNamed(CodeGenFunction &CGF,
457239462Sdim                                     const std::string &Name, bool isWeak);
458221345Sdim  /// Looks up the method for sending a message to the specified object.  This
459221345Sdim  /// mechanism differs between the GCC and GNU runtimes, so this method must be
460221345Sdim  /// overridden in subclasses.
461221345Sdim  virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
462221345Sdim                                 llvm::Value *&Receiver,
463221345Sdim                                 llvm::Value *cmd,
464261991Sdim                                 llvm::MDNode *node,
465261991Sdim                                 MessageSendInfo &MSI) = 0;
466226633Sdim  /// Looks up the method for sending a message to a superclass.  This
467226633Sdim  /// mechanism differs between the GCC and GNU runtimes, so this method must
468226633Sdim  /// be overridden in subclasses.
469221345Sdim  virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
470296417Sdim                                      Address ObjCSuper,
471261991Sdim                                      llvm::Value *cmd,
472261991Sdim                                      MessageSendInfo &MSI) = 0;
473226633Sdim  /// Libobjc2 uses a bitfield representation where small(ish) bitfields are
474226633Sdim  /// stored in a 64-bit value with the low bit set to 1 and the remaining 63
475226633Sdim  /// bits set to their values, LSB first, while larger ones are stored in a
476226633Sdim  /// structure of this / form:
477226633Sdim  ///
478226633Sdim  /// struct { int32_t length; int32_t values[length]; };
479226633Sdim  ///
480226633Sdim  /// The values in the array are stored in host-endian format, with the least
481226633Sdim  /// significant bit being assumed to come first in the bitfield.  Therefore,
482226633Sdim  /// a bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] },
483226633Sdim  /// while a bitfield / with the 63rd bit set will be 1<<64.
484234353Sdim  llvm::Constant *MakeBitField(ArrayRef<bool> bits);
485193326Sedpublic:
486221345Sdim  CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
487221345Sdim      unsigned protocolClassVersion);
488221345Sdim
489296417Sdim  ConstantAddress GenerateConstantString(const StringLiteral *) override;
490221345Sdim
491276479Sdim  RValue
492276479Sdim  GenerateMessageSend(CodeGenFunction &CGF, ReturnValueSlot Return,
493276479Sdim                      QualType ResultType, Selector Sel,
494276479Sdim                      llvm::Value *Receiver, const CallArgList &CallArgs,
495207619Srdivacky                      const ObjCInterfaceDecl *Class,
496276479Sdim                      const ObjCMethodDecl *Method) override;
497276479Sdim  RValue
498276479Sdim  GenerateMessageSendSuper(CodeGenFunction &CGF, ReturnValueSlot Return,
499276479Sdim                           QualType ResultType, Selector Sel,
500193326Sed                           const ObjCInterfaceDecl *Class,
501276479Sdim                           bool isCategoryImpl, llvm::Value *Receiver,
502276479Sdim                           bool IsClassMessage, const CallArgList &CallArgs,
503276479Sdim                           const ObjCMethodDecl *Method) override;
504276479Sdim  llvm::Value *GetClass(CodeGenFunction &CGF,
505276479Sdim                        const ObjCInterfaceDecl *OID) override;
506296417Sdim  llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) override;
507296417Sdim  Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) override;
508276479Sdim  llvm::Value *GetSelector(CodeGenFunction &CGF,
509276479Sdim                           const ObjCMethodDecl *Method) override;
510276479Sdim  llvm::Constant *GetEHType(QualType T) override;
511198092Srdivacky
512276479Sdim  llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
513276479Sdim                                 const ObjCContainerDecl *CD) override;
514276479Sdim  void GenerateCategory(const ObjCCategoryImplDecl *CMD) override;
515276479Sdim  void GenerateClass(const ObjCImplementationDecl *ClassDecl) override;
516276479Sdim  void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) override;
517276479Sdim  llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
518276479Sdim                                   const ObjCProtocolDecl *PD) override;
519276479Sdim  void GenerateProtocol(const ObjCProtocolDecl *PD) override;
520276479Sdim  llvm::Function *ModuleInitFunction() override;
521276479Sdim  llvm::Constant *GetPropertyGetFunction() override;
522276479Sdim  llvm::Constant *GetPropertySetFunction() override;
523276479Sdim  llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
524276479Sdim                                                  bool copy) override;
525276479Sdim  llvm::Constant *GetSetStructFunction() override;
526276479Sdim  llvm::Constant *GetGetStructFunction() override;
527276479Sdim  llvm::Constant *GetCppAtomicObjectGetFunction() override;
528276479Sdim  llvm::Constant *GetCppAtomicObjectSetFunction() override;
529276479Sdim  llvm::Constant *EnumerationMutationFunction() override;
530198092Srdivacky
531276479Sdim  void EmitTryStmt(CodeGenFunction &CGF,
532276479Sdim                   const ObjCAtTryStmt &S) override;
533276479Sdim  void EmitSynchronizedStmt(CodeGenFunction &CGF,
534276479Sdim                            const ObjCAtSynchronizedStmt &S) override;
535276479Sdim  void EmitThrowStmt(CodeGenFunction &CGF,
536276479Sdim                     const ObjCAtThrowStmt &S,
537276479Sdim                     bool ClearInsertionPoint=true) override;
538276479Sdim  llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF,
539296417Sdim                                 Address AddrWeakObj) override;
540276479Sdim  void EmitObjCWeakAssign(CodeGenFunction &CGF,
541296417Sdim                          llvm::Value *src, Address dst) override;
542276479Sdim  void EmitObjCGlobalAssign(CodeGenFunction &CGF,
543296417Sdim                            llvm::Value *src, Address dest,
544276479Sdim                            bool threadlocal=false) override;
545276479Sdim  void EmitObjCIvarAssign(CodeGenFunction &CGF, llvm::Value *src,
546296417Sdim                          Address dest, llvm::Value *ivarOffset) override;
547276479Sdim  void EmitObjCStrongCastAssign(CodeGenFunction &CGF,
548296417Sdim                                llvm::Value *src, Address dest) override;
549296417Sdim  void EmitGCMemmoveCollectable(CodeGenFunction &CGF, Address DestPtr,
550296417Sdim                                Address SrcPtr,
551276479Sdim                                llvm::Value *Size) override;
552276479Sdim  LValue EmitObjCValueForIvar(CodeGenFunction &CGF, QualType ObjectTy,
553276479Sdim                              llvm::Value *BaseValue, const ObjCIvarDecl *Ivar,
554276479Sdim                              unsigned CVRQualifiers) override;
555276479Sdim  llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
556276479Sdim                              const ObjCInterfaceDecl *Interface,
557276479Sdim                              const ObjCIvarDecl *Ivar) override;
558276479Sdim  llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) override;
559276479Sdim  llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM,
560276479Sdim                                     const CGBlockInfo &blockInfo) override {
561212904Sdim    return NULLPtr;
562212904Sdim  }
563276479Sdim  llvm::Constant *BuildRCBlockLayout(CodeGenModule &CGM,
564276479Sdim                                     const CGBlockInfo &blockInfo) override {
565243830Sdim    return NULLPtr;
566243830Sdim  }
567276479Sdim
568276479Sdim  llvm::Constant *BuildByrefLayout(CodeGenModule &CGM, QualType T) override {
569249423Sdim    return NULLPtr;
570249423Sdim  }
571276479Sdim
572276479Sdim  llvm::GlobalVariable *GetClassGlobal(const std::string &Name,
573276479Sdim                                       bool Weak = false) override {
574276479Sdim    return nullptr;
575223017Sdim  }
576193326Sed};
577221345Sdim/// Class representing the legacy GCC Objective-C ABI.  This is the default when
578221345Sdim/// -fobjc-nonfragile-abi is not specified.
579221345Sdim///
580221345Sdim/// The GCC ABI target actually generates code that is approximately compatible
581221345Sdim/// with the new GNUstep runtime ABI, but refrains from using any features that
582221345Sdim/// would not work with the GCC runtime.  For example, clang always generates
583221345Sdim/// the extended form of the class structure, and the extra fields are simply
584221345Sdim/// ignored by GCC libobjc.
585221345Sdimclass CGObjCGCC : public CGObjCGNU {
586221345Sdim  /// The GCC ABI message lookup function.  Returns an IMP pointing to the
587221345Sdim  /// method implementation for this message.
588221345Sdim  LazyRuntimeFunction MsgLookupFn;
589221345Sdim  /// The GCC ABI superclass message lookup function.  Takes a pointer to a
590221345Sdim  /// structure describing the receiver and the class, and a selector as
591221345Sdim  /// arguments.  Returns the IMP for the corresponding method.
592221345Sdim  LazyRuntimeFunction MsgLookupSuperFn;
593221345Sdimprotected:
594276479Sdim  llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
595276479Sdim                         llvm::Value *cmd, llvm::MDNode *node,
596276479Sdim                         MessageSendInfo &MSI) override {
597221345Sdim    CGBuilderTy &Builder = CGF.Builder;
598228379Sdim    llvm::Value *args[] = {
599234353Sdim            EnforceType(Builder, Receiver, IdTy),
600234353Sdim            EnforceType(Builder, cmd, SelectorTy) };
601249423Sdim    llvm::CallSite imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
602228379Sdim    imp->setMetadata(msgSendMDKind, node);
603228379Sdim    return imp.getInstruction();
604221345Sdim  }
605296417Sdim  llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
606276479Sdim                              llvm::Value *cmd, MessageSendInfo &MSI) override {
607221345Sdim      CGBuilderTy &Builder = CGF.Builder;
608221345Sdim      llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper,
609296417Sdim          PtrToObjCSuperTy).getPointer(), cmd};
610249423Sdim      return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
611221345Sdim    }
612221345Sdim  public:
613221345Sdim    CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) {
614221345Sdim      // IMP objc_msg_lookup(id, SEL);
615276479Sdim      MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy,
616276479Sdim                       nullptr);
617221345Sdim      // IMP objc_msg_lookup_super(struct objc_super*, SEL);
618221345Sdim      MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
619276479Sdim              PtrToObjCSuperTy, SelectorTy, nullptr);
620221345Sdim    }
621221345Sdim};
622221345Sdim/// Class used when targeting the new GNUstep runtime ABI.
623221345Sdimclass CGObjCGNUstep : public CGObjCGNU {
624221345Sdim    /// The slot lookup function.  Returns a pointer to a cacheable structure
625221345Sdim    /// that contains (among other things) the IMP.
626221345Sdim    LazyRuntimeFunction SlotLookupFn;
627221345Sdim    /// The GNUstep ABI superclass message lookup function.  Takes a pointer to
628221345Sdim    /// a structure describing the receiver and the class, and a selector as
629221345Sdim    /// arguments.  Returns the slot for the corresponding method.  Superclass
630221345Sdim    /// message lookup rarely changes, so this is a good caching opportunity.
631221345Sdim    LazyRuntimeFunction SlotLookupSuperFn;
632249423Sdim    /// Specialised function for setting atomic retain properties
633249423Sdim    LazyRuntimeFunction SetPropertyAtomic;
634249423Sdim    /// Specialised function for setting atomic copy properties
635249423Sdim    LazyRuntimeFunction SetPropertyAtomicCopy;
636249423Sdim    /// Specialised function for setting nonatomic retain properties
637249423Sdim    LazyRuntimeFunction SetPropertyNonAtomic;
638249423Sdim    /// Specialised function for setting nonatomic copy properties
639249423Sdim    LazyRuntimeFunction SetPropertyNonAtomicCopy;
640249423Sdim    /// Function to perform atomic copies of C++ objects with nontrivial copy
641249423Sdim    /// constructors from Objective-C ivars.
642249423Sdim    LazyRuntimeFunction CxxAtomicObjectGetFn;
643249423Sdim    /// Function to perform atomic copies of C++ objects with nontrivial copy
644249423Sdim    /// constructors to Objective-C ivars.
645249423Sdim    LazyRuntimeFunction CxxAtomicObjectSetFn;
646221345Sdim    /// Type of an slot structure pointer.  This is returned by the various
647221345Sdim    /// lookup functions.
648221345Sdim    llvm::Type *SlotTy;
649243830Sdim  public:
650276479Sdim    llvm::Constant *GetEHType(QualType T) override;
651221345Sdim  protected:
652276479Sdim    llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
653276479Sdim                           llvm::Value *cmd, llvm::MDNode *node,
654276479Sdim                           MessageSendInfo &MSI) override {
655221345Sdim      CGBuilderTy &Builder = CGF.Builder;
656221345Sdim      llvm::Function *LookupFn = SlotLookupFn;
657221345Sdim
658221345Sdim      // Store the receiver on the stack so that we can reload it later
659296417Sdim      Address ReceiverPtr =
660296417Sdim        CGF.CreateTempAlloca(Receiver->getType(), CGF.getPointerAlign());
661221345Sdim      Builder.CreateStore(Receiver, ReceiverPtr);
662221345Sdim
663221345Sdim      llvm::Value *self;
664221345Sdim
665221345Sdim      if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) {
666221345Sdim        self = CGF.LoadObjCSelf();
667221345Sdim      } else {
668221345Sdim        self = llvm::ConstantPointerNull::get(IdTy);
669221345Sdim      }
670221345Sdim
671221345Sdim      // The lookup function is guaranteed not to capture the receiver pointer.
672221345Sdim      LookupFn->setDoesNotCapture(1);
673221345Sdim
674228379Sdim      llvm::Value *args[] = {
675296417Sdim              EnforceType(Builder, ReceiverPtr.getPointer(), PtrToIdTy),
676234353Sdim              EnforceType(Builder, cmd, SelectorTy),
677234353Sdim              EnforceType(Builder, self, IdTy) };
678249423Sdim      llvm::CallSite slot = CGF.EmitRuntimeCallOrInvoke(LookupFn, args);
679228379Sdim      slot.setOnlyReadsMemory();
680221345Sdim      slot->setMetadata(msgSendMDKind, node);
681221345Sdim
682221345Sdim      // Load the imp from the slot
683296417Sdim      llvm::Value *imp = Builder.CreateAlignedLoad(
684296417Sdim          Builder.CreateStructGEP(nullptr, slot.getInstruction(), 4),
685296417Sdim          CGF.getPointerAlign());
686221345Sdim
687221345Sdim      // The lookup function may have changed the receiver, so make sure we use
688221345Sdim      // the new one.
689221345Sdim      Receiver = Builder.CreateLoad(ReceiverPtr, true);
690221345Sdim      return imp;
691221345Sdim    }
692296417Sdim    llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
693276479Sdim                                llvm::Value *cmd,
694276479Sdim                                MessageSendInfo &MSI) override {
695221345Sdim      CGBuilderTy &Builder = CGF.Builder;
696296417Sdim      llvm::Value *lookupArgs[] = {ObjCSuper.getPointer(), cmd};
697221345Sdim
698249423Sdim      llvm::CallInst *slot =
699249423Sdim        CGF.EmitNounwindRuntimeCall(SlotLookupSuperFn, lookupArgs);
700221345Sdim      slot->setOnlyReadsMemory();
701221345Sdim
702296417Sdim      return Builder.CreateAlignedLoad(Builder.CreateStructGEP(nullptr, slot, 4),
703296417Sdim                                       CGF.getPointerAlign());
704221345Sdim    }
705221345Sdim  public:
706221345Sdim    CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNU(Mod, 9, 3) {
707249423Sdim      const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime;
708249423Sdim
709224145Sdim      llvm::StructType *SlotStructTy = llvm::StructType::get(PtrTy,
710276479Sdim          PtrTy, PtrTy, IntTy, IMPTy, nullptr);
711221345Sdim      SlotTy = llvm::PointerType::getUnqual(SlotStructTy);
712221345Sdim      // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
713221345Sdim      SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy,
714276479Sdim          SelectorTy, IdTy, nullptr);
715221345Sdim      // Slot_t objc_msg_lookup_super(struct objc_super*, SEL);
716221345Sdim      SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
717276479Sdim              PtrToObjCSuperTy, SelectorTy, nullptr);
718221345Sdim      // If we're in ObjC++ mode, then we want to make
719234353Sdim      if (CGM.getLangOpts().CPlusPlus) {
720224145Sdim        llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
721221345Sdim        // void *__cxa_begin_catch(void *e)
722276479Sdim        EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy, nullptr);
723221345Sdim        // void __cxa_end_catch(void)
724276479Sdim        ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy, nullptr);
725221345Sdim        // void _Unwind_Resume_or_Rethrow(void*)
726249423Sdim        ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
727276479Sdim            PtrTy, nullptr);
728249423Sdim      } else if (R.getVersion() >= VersionTuple(1, 7)) {
729249423Sdim        llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
730249423Sdim        // id objc_begin_catch(void *e)
731276479Sdim        EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy, nullptr);
732249423Sdim        // void objc_end_catch(void)
733276479Sdim        ExitCatchFn.init(&CGM, "objc_end_catch", VoidTy, nullptr);
734249423Sdim        // void _Unwind_Resume_or_Rethrow(void*)
735249423Sdim        ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy,
736276479Sdim            PtrTy, nullptr);
737221345Sdim      }
738249423Sdim      llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
739249423Sdim      SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
740276479Sdim          SelectorTy, IdTy, PtrDiffTy, nullptr);
741249423Sdim      SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
742276479Sdim          IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr);
743249423Sdim      SetPropertyNonAtomic.init(&CGM, "objc_setProperty_nonatomic", VoidTy,
744276479Sdim          IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr);
745249423Sdim      SetPropertyNonAtomicCopy.init(&CGM, "objc_setProperty_nonatomic_copy",
746276479Sdim          VoidTy, IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr);
747249423Sdim      // void objc_setCppObjectAtomic(void *dest, const void *src, void
748249423Sdim      // *helper);
749249423Sdim      CxxAtomicObjectSetFn.init(&CGM, "objc_setCppObjectAtomic", VoidTy, PtrTy,
750276479Sdim          PtrTy, PtrTy, nullptr);
751249423Sdim      // void objc_getCppObjectAtomic(void *dest, const void *src, void
752249423Sdim      // *helper);
753249423Sdim      CxxAtomicObjectGetFn.init(&CGM, "objc_getCppObjectAtomic", VoidTy, PtrTy,
754276479Sdim          PtrTy, PtrTy, nullptr);
755221345Sdim    }
756276479Sdim    llvm::Constant *GetCppAtomicObjectGetFunction() override {
757249423Sdim      // The optimised functions were added in version 1.7 of the GNUstep
758249423Sdim      // runtime.
759249423Sdim      assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
760249423Sdim          VersionTuple(1, 7));
761249423Sdim      return CxxAtomicObjectGetFn;
762249423Sdim    }
763276479Sdim    llvm::Constant *GetCppAtomicObjectSetFunction() override {
764249423Sdim      // The optimised functions were added in version 1.7 of the GNUstep
765249423Sdim      // runtime.
766249423Sdim      assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
767249423Sdim          VersionTuple(1, 7));
768249423Sdim      return CxxAtomicObjectSetFn;
769249423Sdim    }
770276479Sdim    llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
771276479Sdim                                                    bool copy) override {
772249423Sdim      // The optimised property functions omit the GC check, and so are not
773249423Sdim      // safe to use in GC mode.  The standard functions are fast in GC mode,
774249423Sdim      // so there is less advantage in using them.
775249423Sdim      assert ((CGM.getLangOpts().getGC() == LangOptions::NonGC));
776249423Sdim      // The optimised functions were added in version 1.7 of the GNUstep
777249423Sdim      // runtime.
778249423Sdim      assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
779249423Sdim          VersionTuple(1, 7));
780249423Sdim
781249423Sdim      if (atomic) {
782249423Sdim        if (copy) return SetPropertyAtomicCopy;
783249423Sdim        return SetPropertyAtomic;
784249423Sdim      }
785249423Sdim
786276479Sdim      return copy ? SetPropertyNonAtomicCopy : SetPropertyNonAtomic;
787249423Sdim    }
788221345Sdim};
789221345Sdim
790261991Sdim/// Support for the ObjFW runtime.
791243830Sdimclass CGObjCObjFW: public CGObjCGNU {
792243830Sdimprotected:
793243830Sdim  /// The GCC ABI message lookup function.  Returns an IMP pointing to the
794243830Sdim  /// method implementation for this message.
795243830Sdim  LazyRuntimeFunction MsgLookupFn;
796261991Sdim  /// stret lookup function.  While this does not seem to make sense at the
797261991Sdim  /// first look, this is required to call the correct forwarding function.
798261991Sdim  LazyRuntimeFunction MsgLookupFnSRet;
799243830Sdim  /// The GCC ABI superclass message lookup function.  Takes a pointer to a
800243830Sdim  /// structure describing the receiver and the class, and a selector as
801243830Sdim  /// arguments.  Returns the IMP for the corresponding method.
802261991Sdim  LazyRuntimeFunction MsgLookupSuperFn, MsgLookupSuperFnSRet;
803243830Sdim
804276479Sdim  llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
805276479Sdim                         llvm::Value *cmd, llvm::MDNode *node,
806276479Sdim                         MessageSendInfo &MSI) override {
807243830Sdim    CGBuilderTy &Builder = CGF.Builder;
808243830Sdim    llvm::Value *args[] = {
809243830Sdim            EnforceType(Builder, Receiver, IdTy),
810243830Sdim            EnforceType(Builder, cmd, SelectorTy) };
811261991Sdim
812261991Sdim    llvm::CallSite imp;
813261991Sdim    if (CGM.ReturnTypeUsesSRet(MSI.CallInfo))
814261991Sdim      imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFnSRet, args);
815261991Sdim    else
816261991Sdim      imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
817261991Sdim
818243830Sdim    imp->setMetadata(msgSendMDKind, node);
819243830Sdim    return imp.getInstruction();
820243830Sdim  }
821243830Sdim
822296417Sdim  llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
823276479Sdim                              llvm::Value *cmd, MessageSendInfo &MSI) override {
824243830Sdim      CGBuilderTy &Builder = CGF.Builder;
825296417Sdim      llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper.getPointer(),
826243830Sdim          PtrToObjCSuperTy), cmd};
827261991Sdim
828261991Sdim      if (CGM.ReturnTypeUsesSRet(MSI.CallInfo))
829261991Sdim        return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFnSRet, lookupArgs);
830261991Sdim      else
831261991Sdim        return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
832243830Sdim    }
833243830Sdim
834276479Sdim  llvm::Value *GetClassNamed(CodeGenFunction &CGF,
835276479Sdim                             const std::string &Name, bool isWeak) override {
836239462Sdim    if (isWeak)
837249423Sdim      return CGObjCGNU::GetClassNamed(CGF, Name, isWeak);
838239462Sdim
839239462Sdim    EmitClassRef(Name);
840239462Sdim
841239462Sdim    std::string SymbolName = "_OBJC_CLASS_" + Name;
842239462Sdim
843239462Sdim    llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(SymbolName);
844239462Sdim
845239462Sdim    if (!ClassSymbol)
846239462Sdim      ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
847239462Sdim                                             llvm::GlobalValue::ExternalLinkage,
848276479Sdim                                             nullptr, SymbolName);
849239462Sdim
850239462Sdim    return ClassSymbol;
851239462Sdim  }
852239462Sdim
853239462Sdimpublic:
854243830Sdim  CGObjCObjFW(CodeGenModule &Mod): CGObjCGNU(Mod, 9, 3) {
855243830Sdim    // IMP objc_msg_lookup(id, SEL);
856276479Sdim    MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, nullptr);
857261991Sdim    MsgLookupFnSRet.init(&CGM, "objc_msg_lookup_stret", IMPTy, IdTy,
858276479Sdim                         SelectorTy, nullptr);
859243830Sdim    // IMP objc_msg_lookup_super(struct objc_super*, SEL);
860243830Sdim    MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
861276479Sdim                          PtrToObjCSuperTy, SelectorTy, nullptr);
862261991Sdim    MsgLookupSuperFnSRet.init(&CGM, "objc_msg_lookup_super_stret", IMPTy,
863276479Sdim                              PtrToObjCSuperTy, SelectorTy, nullptr);
864243830Sdim  }
865239462Sdim};
866193326Sed} // end anonymous namespace
867193326Sed
868193326Sed
869194613Sed/// Emits a reference to a dummy variable which is emitted with each class.
870194613Sed/// This ensures that a linker error will be generated when trying to link
871194613Sed/// together modules where a referenced class is not defined.
872198092Srdivackyvoid CGObjCGNU::EmitClassRef(const std::string &className) {
873194613Sed  std::string symbolRef = "__objc_class_ref_" + className;
874194613Sed  // Don't emit two copies of the same symbol
875198092Srdivacky  if (TheModule.getGlobalVariable(symbolRef))
876198092Srdivacky    return;
877194613Sed  std::string symbolName = "__objc_class_name_" + className;
878194613Sed  llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
879194613Sed  if (!ClassSymbol) {
880198092Srdivacky    ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
881276479Sdim                                           llvm::GlobalValue::ExternalLinkage,
882276479Sdim                                           nullptr, symbolName);
883194613Sed  }
884198092Srdivacky  new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
885198092Srdivacky    llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
886194613Sed}
887193326Sed
888280031Sdimstatic std::string SymbolNameForMethod( StringRef ClassName,
889280031Sdim     StringRef CategoryName, const Selector MethodName,
890221345Sdim    bool isClassMethod) {
891221345Sdim  std::string MethodNameColonStripped = MethodName.getAsString();
892202379Srdivacky  std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(),
893202379Srdivacky      ':', '_');
894226633Sdim  return (Twine(isClassMethod ? "_c_" : "_i_") + ClassName + "_" +
895221345Sdim    CategoryName + "_" + MethodNameColonStripped).str();
896193326Sed}
897193326Sed
898221345SdimCGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
899276479Sdim                     unsigned protocolClassVersion)
900234353Sdim  : CGObjCRuntime(cgm), TheModule(CGM.getModule()),
901276479Sdim    VMContext(cgm.getLLVMContext()), ClassPtrAlias(nullptr),
902276479Sdim    MetaClassPtrAlias(nullptr), RuntimeVersion(runtimeABIVersion),
903276479Sdim    ProtocolVersion(protocolClassVersion) {
904207619Srdivacky
905207619Srdivacky  msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend");
906207619Srdivacky
907221345Sdim  CodeGenTypes &Types = CGM.getTypes();
908193326Sed  IntTy = cast<llvm::IntegerType>(
909221345Sdim      Types.ConvertType(CGM.getContext().IntTy));
910193326Sed  LongTy = cast<llvm::IntegerType>(
911221345Sdim      Types.ConvertType(CGM.getContext().LongTy));
912218893Sdim  SizeTy = cast<llvm::IntegerType>(
913221345Sdim      Types.ConvertType(CGM.getContext().getSizeType()));
914218893Sdim  PtrDiffTy = cast<llvm::IntegerType>(
915221345Sdim      Types.ConvertType(CGM.getContext().getPointerDiffType()));
916218893Sdim  BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
917198092Srdivacky
918198092Srdivacky  Int8Ty = llvm::Type::getInt8Ty(VMContext);
919198092Srdivacky  // C string type.  Used in lots of places.
920198092Srdivacky  PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
921198092Srdivacky
922193326Sed  Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
923193326Sed  Zeros[1] = Zeros[0];
924198092Srdivacky  NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
925193326Sed  // Get the selector Type.
926202879Srdivacky  QualType selTy = CGM.getContext().getObjCSelType();
927202879Srdivacky  if (QualType() == selTy) {
928202879Srdivacky    SelectorTy = PtrToInt8Ty;
929202879Srdivacky  } else {
930202879Srdivacky    SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy));
931202879Srdivacky  }
932193326Sed
933193326Sed  PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
934193326Sed  PtrTy = PtrToInt8Ty;
935198092Srdivacky
936226633Sdim  Int32Ty = llvm::Type::getInt32Ty(VMContext);
937226633Sdim  Int64Ty = llvm::Type::getInt64Ty(VMContext);
938226633Sdim
939226633Sdim  IntPtrTy =
940276479Sdim      CGM.getDataLayout().getPointerSizeInBits() == 32 ? Int32Ty : Int64Ty;
941226633Sdim
942193326Sed  // Object type
943221345Sdim  QualType UnqualIdTy = CGM.getContext().getObjCIdType();
944221345Sdim  ASTIdTy = CanQualType();
945221345Sdim  if (UnqualIdTy != QualType()) {
946221345Sdim    ASTIdTy = CGM.getContext().getCanonicalType(UnqualIdTy);
947221345Sdim    IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
948221345Sdim  } else {
949202879Srdivacky    IdTy = PtrToInt8Ty;
950202879Srdivacky  }
951203955Srdivacky  PtrToIdTy = llvm::PointerType::getUnqual(IdTy);
952198092Srdivacky
953276479Sdim  ObjCSuperTy = llvm::StructType::get(IdTy, IdTy, nullptr);
954221345Sdim  PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy);
955221345Sdim
956224145Sdim  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
957221345Sdim
958221345Sdim  // void objc_exception_throw(id);
959276479Sdim  ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, nullptr);
960276479Sdim  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, nullptr);
961221345Sdim  // int objc_sync_enter(id);
962276479Sdim  SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy, nullptr);
963221345Sdim  // int objc_sync_exit(id);
964276479Sdim  SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy, nullptr);
965221345Sdim
966221345Sdim  // void objc_enumerationMutation (id)
967221345Sdim  EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy,
968276479Sdim      IdTy, nullptr);
969221345Sdim
970221345Sdim  // id objc_getProperty(id, SEL, ptrdiff_t, BOOL)
971221345Sdim  GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy,
972276479Sdim      PtrDiffTy, BoolTy, nullptr);
973221345Sdim  // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL)
974221345Sdim  SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy,
975276479Sdim      PtrDiffTy, IdTy, BoolTy, BoolTy, nullptr);
976221345Sdim  // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
977221345Sdim  GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy,
978276479Sdim      PtrDiffTy, BoolTy, BoolTy, nullptr);
979221345Sdim  // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
980221345Sdim  SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy,
981276479Sdim      PtrDiffTy, BoolTy, BoolTy, nullptr);
982221345Sdim
983193326Sed  // IMP type
984224145Sdim  llvm::Type *IMPArgs[] = { IdTy, SelectorTy };
985221345Sdim  IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs,
986221345Sdim              true));
987203955Srdivacky
988234353Sdim  const LangOptions &Opts = CGM.getLangOpts();
989226633Sdim  if ((Opts.getGC() != LangOptions::NonGC) || Opts.ObjCAutoRefCount)
990224145Sdim    RuntimeVersion = 10;
991224145Sdim
992221345Sdim  // Don't bother initialising the GC stuff unless we're compiling in GC mode
993226633Sdim  if (Opts.getGC() != LangOptions::NonGC) {
994223017Sdim    // This is a bit of an hack.  We should sort this out by having a proper
995223017Sdim    // CGObjCGNUstep subclass for GC, but we may want to really support the old
996223017Sdim    // ABI and GC added in ObjectiveC2.framework, so we fudge it a bit for now
997203955Srdivacky    // Get selectors needed in GC mode
998203955Srdivacky    RetainSel = GetNullarySelector("retain", CGM.getContext());
999203955Srdivacky    ReleaseSel = GetNullarySelector("release", CGM.getContext());
1000203955Srdivacky    AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext());
1001203955Srdivacky
1002203955Srdivacky    // Get functions needed in GC mode
1003203955Srdivacky
1004203955Srdivacky    // id objc_assign_ivar(id, id, ptrdiff_t);
1005221345Sdim    IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy,
1006276479Sdim        nullptr);
1007203955Srdivacky    // id objc_assign_strongCast (id, id*)
1008221345Sdim    StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy,
1009276479Sdim        PtrToIdTy, nullptr);
1010203955Srdivacky    // id objc_assign_global(id, id*);
1011221345Sdim    GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy,
1012276479Sdim        nullptr);
1013203955Srdivacky    // id objc_assign_weak(id, id*);
1014276479Sdim    WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy, nullptr);
1015203955Srdivacky    // id objc_read_weak(id*);
1016276479Sdim    WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy, nullptr);
1017203955Srdivacky    // void *objc_memmove_collectable(void*, void *, size_t);
1018221345Sdim    MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy,
1019276479Sdim        SizeTy, nullptr);
1020203955Srdivacky  }
1021193326Sed}
1022198092Srdivacky
1023249423Sdimllvm::Value *CGObjCGNU::GetClassNamed(CodeGenFunction &CGF,
1024224145Sdim                                      const std::string &Name,
1025224145Sdim                                      bool isWeak) {
1026296417Sdim  llvm::Constant *ClassName = MakeConstantString(Name);
1027202379Srdivacky  // With the incompatible ABI, this will need to be replaced with a direct
1028202379Srdivacky  // reference to the class symbol.  For the compatible nonfragile ABI we are
1029202379Srdivacky  // still performing this lookup at run time but emitting the symbol for the
1030202379Srdivacky  // class externally so that we can make the switch later.
1031224145Sdim  //
1032224145Sdim  // Libobjc2 contains an LLVM pass that replaces calls to objc_lookup_class
1033224145Sdim  // with memoized versions or with static references if it's safe to do so.
1034224145Sdim  if (!isWeak)
1035224145Sdim    EmitClassRef(Name);
1036193326Sed
1037193326Sed  llvm::Constant *ClassLookupFn =
1038226633Sdim    CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, PtrToInt8Ty, true),
1039193326Sed                              "objc_lookup_class");
1040249423Sdim  return CGF.EmitNounwindRuntimeCall(ClassLookupFn, ClassName);
1041193326Sed}
1042193326Sed
1043224145Sdim// This has to perform the lookup every time, since posing and related
1044224145Sdim// techniques can modify the name -> class mapping.
1045249423Sdimllvm::Value *CGObjCGNU::GetClass(CodeGenFunction &CGF,
1046224145Sdim                                 const ObjCInterfaceDecl *OID) {
1047249423Sdim  return GetClassNamed(CGF, OID->getNameAsString(), OID->isWeakImported());
1048224145Sdim}
1049249423Sdimllvm::Value *CGObjCGNU::EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
1050249423Sdim  return GetClassNamed(CGF, "NSAutoreleasePool", false);
1051224145Sdim}
1052224145Sdim
1053249423Sdimllvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel,
1054296417Sdim                                    const std::string &TypeEncoding) {
1055221345Sdim
1056261991Sdim  SmallVectorImpl<TypedSelector> &Types = SelectorTable[Sel];
1057276479Sdim  llvm::GlobalAlias *SelValue = nullptr;
1058221345Sdim
1059226633Sdim  for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
1060221345Sdim      e = Types.end() ; i!=e ; i++) {
1061221345Sdim    if (i->first == TypeEncoding) {
1062221345Sdim      SelValue = i->second;
1063221345Sdim      break;
1064221345Sdim    }
1065221345Sdim  }
1066276479Sdim  if (!SelValue) {
1067276479Sdim    SelValue = llvm::GlobalAlias::create(
1068296417Sdim        SelectorTy->getElementType(), 0, llvm::GlobalValue::PrivateLinkage,
1069276479Sdim        ".objc_selector_" + Sel.getAsString(), &TheModule);
1070288943Sdim    Types.emplace_back(TypeEncoding, SelValue);
1071221345Sdim  }
1072221345Sdim
1073221345Sdim  return SelValue;
1074221345Sdim}
1075221345Sdim
1076296417SdimAddress CGObjCGNU::GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) {
1077296417Sdim  llvm::Value *SelValue = GetSelector(CGF, Sel);
1078296417Sdim
1079296417Sdim  // Store it to a temporary.  Does this satisfy the semantics of
1080296417Sdim  // GetAddrOfSelector?  Hopefully.
1081296417Sdim  Address tmp = CGF.CreateTempAlloca(SelValue->getType(),
1082296417Sdim                                     CGF.getPointerAlign());
1083296417Sdim  CGF.Builder.CreateStore(SelValue, tmp);
1084296417Sdim  return tmp;
1085193326Sed}
1086193326Sed
1087296417Sdimllvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel) {
1088296417Sdim  return GetSelector(CGF, Sel, std::string());
1089296417Sdim}
1090296417Sdim
1091249423Sdimllvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF,
1092249423Sdim                                    const ObjCMethodDecl *Method) {
1093193326Sed  std::string SelTypes;
1094193326Sed  CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
1095296417Sdim  return GetSelector(CGF, Method->getSelector(), SelTypes);
1096221345Sdim}
1097193326Sed
1098221345Sdimllvm::Constant *CGObjCGNU::GetEHType(QualType T) {
1099243830Sdim  if (T->isObjCIdType() || T->isObjCQualifiedIdType()) {
1100243830Sdim    // With the old ABI, there was only one kind of catchall, which broke
1101243830Sdim    // foreign exceptions.  With the new ABI, we use __objc_id_typeinfo as
1102243830Sdim    // a pointer indicating object catchalls, and NULL to indicate real
1103243830Sdim    // catchalls
1104243830Sdim    if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1105243830Sdim      return MakeConstantString("@id");
1106243830Sdim    } else {
1107276479Sdim      return nullptr;
1108243830Sdim    }
1109243830Sdim  }
1110221345Sdim
1111243830Sdim  // All other types should be Objective-C interface pointer types.
1112243830Sdim  const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>();
1113243830Sdim  assert(OPT && "Invalid @catch type.");
1114243830Sdim  const ObjCInterfaceDecl *IDecl = OPT->getObjectType()->getInterface();
1115243830Sdim  assert(IDecl && "Invalid @catch type.");
1116243830Sdim  return MakeConstantString(IDecl->getIdentifier()->getName());
1117243830Sdim}
1118243830Sdim
1119243830Sdimllvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
1120243830Sdim  if (!CGM.getLangOpts().CPlusPlus)
1121243830Sdim    return CGObjCGNU::GetEHType(T);
1122243830Sdim
1123221345Sdim  // For Objective-C++, we want to provide the ability to catch both C++ and
1124221345Sdim  // Objective-C objects in the same function.
1125193326Sed
1126221345Sdim  // There's a particular fixed type info for 'id'.
1127221345Sdim  if (T->isObjCIdType() ||
1128221345Sdim      T->isObjCQualifiedIdType()) {
1129221345Sdim    llvm::Constant *IDEHType =
1130221345Sdim      CGM.getModule().getGlobalVariable("__objc_id_type_info");
1131221345Sdim    if (!IDEHType)
1132221345Sdim      IDEHType =
1133221345Sdim        new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty,
1134221345Sdim                                 false,
1135221345Sdim                                 llvm::GlobalValue::ExternalLinkage,
1136276479Sdim                                 nullptr, "__objc_id_type_info");
1137221345Sdim    return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty);
1138221345Sdim  }
1139193326Sed
1140221345Sdim  const ObjCObjectPointerType *PT =
1141221345Sdim    T->getAs<ObjCObjectPointerType>();
1142221345Sdim  assert(PT && "Invalid @catch type.");
1143221345Sdim  const ObjCInterfaceType *IT = PT->getInterfaceType();
1144221345Sdim  assert(IT && "Invalid @catch type.");
1145221345Sdim  std::string className = IT->getDecl()->getIdentifier()->getName();
1146193326Sed
1147221345Sdim  std::string typeinfoName = "__objc_eh_typeinfo_" + className;
1148212904Sdim
1149221345Sdim  // Return the existing typeinfo if it exists
1150221345Sdim  llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName);
1151234353Sdim  if (typeinfo)
1152234353Sdim    return llvm::ConstantExpr::getBitCast(typeinfo, PtrToInt8Ty);
1153221345Sdim
1154221345Sdim  // Otherwise create it.
1155221345Sdim
1156221345Sdim  // vtable for gnustep::libobjc::__objc_class_type_info
1157221345Sdim  // It's quite ugly hard-coding this.  Ideally we'd generate it using the host
1158221345Sdim  // platform's name mangling.
1159221345Sdim  const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE";
1160288943Sdim  auto *Vtable = TheModule.getGlobalVariable(vtableName);
1161221345Sdim  if (!Vtable) {
1162221345Sdim    Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true,
1163276479Sdim                                      llvm::GlobalValue::ExternalLinkage,
1164276479Sdim                                      nullptr, vtableName);
1165198092Srdivacky  }
1166221345Sdim  llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2);
1167288943Sdim  auto *BVtable = llvm::ConstantExpr::getBitCast(
1168288943Sdim      llvm::ConstantExpr::getGetElementPtr(Vtable->getValueType(), Vtable, Two),
1169288943Sdim      PtrToInt8Ty);
1170198092Srdivacky
1171221345Sdim  llvm::Constant *typeName =
1172221345Sdim    ExportUniqueString(className, "__objc_eh_typename_");
1173198092Srdivacky
1174221345Sdim  std::vector<llvm::Constant*> fields;
1175288943Sdim  fields.push_back(BVtable);
1176221345Sdim  fields.push_back(typeName);
1177221345Sdim  llvm::Constant *TI =
1178296417Sdim      MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, nullptr),
1179296417Sdim                 fields, CGM.getPointerAlign(),
1180296417Sdim                 "__objc_eh_typeinfo_" + className,
1181221345Sdim          llvm::GlobalValue::LinkOnceODRLinkage);
1182221345Sdim  return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty);
1183193326Sed}
1184193326Sed
1185193326Sed/// Generate an NSConstantString object.
1186296417SdimConstantAddress CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
1187203955Srdivacky
1188212904Sdim  std::string Str = SL->getString().str();
1189296417Sdim  CharUnits Align = CGM.getPointerAlign();
1190202879Srdivacky
1191203955Srdivacky  // Look for an existing one
1192203955Srdivacky  llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
1193203955Srdivacky  if (old != ObjCStrings.end())
1194296417Sdim    return ConstantAddress(old->getValue(), Align);
1195203955Srdivacky
1196234353Sdim  StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
1197234353Sdim
1198234353Sdim  if (StringClass.empty()) StringClass = "NXConstantString";
1199234353Sdim
1200234353Sdim  std::string Sym = "_OBJC_CLASS_";
1201234353Sdim  Sym += StringClass;
1202234353Sdim
1203234353Sdim  llvm::Constant *isa = TheModule.getNamedGlobal(Sym);
1204234353Sdim
1205234353Sdim  if (!isa)
1206234353Sdim    isa = new llvm::GlobalVariable(TheModule, IdTy, /* isConstant */false,
1207276479Sdim            llvm::GlobalValue::ExternalWeakLinkage, nullptr, Sym);
1208234353Sdim  else if (isa->getType() != PtrToIdTy)
1209234353Sdim    isa = llvm::ConstantExpr::getBitCast(isa, PtrToIdTy);
1210234353Sdim
1211193326Sed  std::vector<llvm::Constant*> Ivars;
1212234353Sdim  Ivars.push_back(isa);
1213193326Sed  Ivars.push_back(MakeConstantString(Str));
1214193326Sed  Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
1215193326Sed  llvm::Constant *ObjCStr = MakeGlobal(
1216276479Sdim    llvm::StructType::get(PtrToIdTy, PtrToInt8Ty, IntTy, nullptr),
1217296417Sdim    Ivars, Align, ".objc_str");
1218203955Srdivacky  ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
1219203955Srdivacky  ObjCStrings[Str] = ObjCStr;
1220203955Srdivacky  ConstantStrings.push_back(ObjCStr);
1221296417Sdim  return ConstantAddress(ObjCStr, Align);
1222193326Sed}
1223193326Sed
1224193326Sed///Generates a message send where the super is the receiver.  This is a message
1225193326Sed///send to self with special delivery semantics indicating which class's method
1226193326Sed///should be called.
1227221345SdimRValue
1228221345SdimCGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF,
1229208600Srdivacky                                    ReturnValueSlot Return,
1230193326Sed                                    QualType ResultType,
1231193326Sed                                    Selector Sel,
1232193326Sed                                    const ObjCInterfaceDecl *Class,
1233193326Sed                                    bool isCategoryImpl,
1234193326Sed                                    llvm::Value *Receiver,
1235193326Sed                                    bool IsClassMessage,
1236198092Srdivacky                                    const CallArgList &CallArgs,
1237198092Srdivacky                                    const ObjCMethodDecl *Method) {
1238223017Sdim  CGBuilderTy &Builder = CGF.Builder;
1239234353Sdim  if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
1240203955Srdivacky    if (Sel == RetainSel || Sel == AutoreleaseSel) {
1241223017Sdim      return RValue::get(EnforceType(Builder, Receiver,
1242223017Sdim                  CGM.getTypes().ConvertType(ResultType)));
1243203955Srdivacky    }
1244203955Srdivacky    if (Sel == ReleaseSel) {
1245276479Sdim      return RValue::get(nullptr);
1246203955Srdivacky    }
1247203955Srdivacky  }
1248193326Sed
1249249423Sdim  llvm::Value *cmd = GetSelector(CGF, Sel);
1250207619Srdivacky
1251207619Srdivacky
1252193326Sed  CallArgList ActualArgs;
1253193326Sed
1254221345Sdim  ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy);
1255221345Sdim  ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
1256224145Sdim  ActualArgs.addFrom(CallArgs);
1257193326Sed
1258234353Sdim  MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
1259193326Sed
1260276479Sdim  llvm::Value *ReceiverClass = nullptr;
1261193326Sed  if (isCategoryImpl) {
1262276479Sdim    llvm::Constant *classLookupFunction = nullptr;
1263193326Sed    if (IsClassMessage)  {
1264193326Sed      classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
1265226633Sdim            IdTy, PtrTy, true), "objc_get_meta_class");
1266193326Sed    } else {
1267193326Sed      classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
1268226633Sdim            IdTy, PtrTy, true), "objc_get_class");
1269193326Sed    }
1270207619Srdivacky    ReceiverClass = Builder.CreateCall(classLookupFunction,
1271193326Sed        MakeConstantString(Class->getNameAsString()));
1272193326Sed  } else {
1273193326Sed    // Set up global aliases for the metaclass or class pointer if they do not
1274193326Sed    // already exist.  These will are forward-references which will be set to
1275198092Srdivacky    // pointers to the class and metaclass structure created for the runtime
1276198092Srdivacky    // load function.  To send a message to super, we look up the value of the
1277193326Sed    // super_class pointer from either the class or metaclass structure.
1278193326Sed    if (IsClassMessage)  {
1279193326Sed      if (!MetaClassPtrAlias) {
1280276479Sdim        MetaClassPtrAlias = llvm::GlobalAlias::create(
1281296417Sdim            IdTy->getElementType(), 0, llvm::GlobalValue::InternalLinkage,
1282276479Sdim            ".objc_metaclass_ref" + Class->getNameAsString(), &TheModule);
1283193326Sed      }
1284193326Sed      ReceiverClass = MetaClassPtrAlias;
1285193326Sed    } else {
1286193326Sed      if (!ClassPtrAlias) {
1287276479Sdim        ClassPtrAlias = llvm::GlobalAlias::create(
1288296417Sdim            IdTy->getElementType(), 0, llvm::GlobalValue::InternalLinkage,
1289276479Sdim            ".objc_class_ref" + Class->getNameAsString(), &TheModule);
1290193326Sed      }
1291193326Sed      ReceiverClass = ClassPtrAlias;
1292193326Sed    }
1293193326Sed  }
1294193326Sed  // Cast the pointer to a simplified version of the class structure
1295288943Sdim  llvm::Type *CastTy = llvm::StructType::get(IdTy, IdTy, nullptr);
1296207619Srdivacky  ReceiverClass = Builder.CreateBitCast(ReceiverClass,
1297288943Sdim                                        llvm::PointerType::getUnqual(CastTy));
1298193326Sed  // Get the superclass pointer
1299288943Sdim  ReceiverClass = Builder.CreateStructGEP(CastTy, ReceiverClass, 1);
1300193326Sed  // Load the superclass pointer
1301296417Sdim  ReceiverClass =
1302296417Sdim    Builder.CreateAlignedLoad(ReceiverClass, CGF.getPointerAlign());
1303193326Sed  // Construct the structure used to look up the IMP
1304224145Sdim  llvm::StructType *ObjCSuperTy = llvm::StructType::get(
1305276479Sdim      Receiver->getType(), IdTy, nullptr);
1306193326Sed
1307296417Sdim  // FIXME: Is this really supposed to be a dynamic alloca?
1308296417Sdim  Address ObjCSuper = Address(Builder.CreateAlloca(ObjCSuperTy),
1309296417Sdim                              CGF.getPointerAlign());
1310296417Sdim
1311288943Sdim  Builder.CreateStore(Receiver,
1312296417Sdim                   Builder.CreateStructGEP(ObjCSuper, 0, CharUnits::Zero()));
1313288943Sdim  Builder.CreateStore(ReceiverClass,
1314296417Sdim                   Builder.CreateStructGEP(ObjCSuper, 1, CGF.getPointerSize()));
1315193326Sed
1316221345Sdim  ObjCSuper = EnforceType(Builder, ObjCSuper, PtrToObjCSuperTy);
1317221345Sdim
1318193326Sed  // Get the IMP
1319261991Sdim  llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd, MSI);
1320234353Sdim  imp = EnforceType(Builder, imp, MSI.MessengerType);
1321207619Srdivacky
1322280031Sdim  llvm::Metadata *impMD[] = {
1323207619Srdivacky      llvm::MDString::get(VMContext, Sel.getAsString()),
1324207619Srdivacky      llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()),
1325280031Sdim      llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1326280031Sdim          llvm::Type::getInt1Ty(VMContext), IsClassMessage))};
1327221345Sdim  llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
1328193326Sed
1329207619Srdivacky  llvm::Instruction *call;
1330296417Sdim  RValue msgRet = CGF.EmitCall(MSI.CallInfo, imp, Return, ActualArgs,
1331296417Sdim                               CGCalleeInfo(), &call);
1332207619Srdivacky  call->setMetadata(msgSendMDKind, node);
1333207619Srdivacky  return msgRet;
1334193326Sed}
1335193326Sed
1336198092Srdivacky/// Generate code for a message send expression.
1337221345SdimRValue
1338221345SdimCGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF,
1339208600Srdivacky                               ReturnValueSlot Return,
1340193326Sed                               QualType ResultType,
1341193326Sed                               Selector Sel,
1342193326Sed                               llvm::Value *Receiver,
1343193326Sed                               const CallArgList &CallArgs,
1344207619Srdivacky                               const ObjCInterfaceDecl *Class,
1345193326Sed                               const ObjCMethodDecl *Method) {
1346223017Sdim  CGBuilderTy &Builder = CGF.Builder;
1347223017Sdim
1348207619Srdivacky  // Strip out message sends to retain / release in GC mode
1349234353Sdim  if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
1350203955Srdivacky    if (Sel == RetainSel || Sel == AutoreleaseSel) {
1351223017Sdim      return RValue::get(EnforceType(Builder, Receiver,
1352223017Sdim                  CGM.getTypes().ConvertType(ResultType)));
1353203955Srdivacky    }
1354203955Srdivacky    if (Sel == ReleaseSel) {
1355276479Sdim      return RValue::get(nullptr);
1356203955Srdivacky    }
1357203955Srdivacky  }
1358207619Srdivacky
1359207619Srdivacky  // If the return type is something that goes in an integer register, the
1360207619Srdivacky  // runtime will handle 0 returns.  For other cases, we fill in the 0 value
1361207619Srdivacky  // ourselves.
1362207619Srdivacky  //
1363207619Srdivacky  // The language spec says the result of this kind of message send is
1364207619Srdivacky  // undefined, but lots of people seem to have forgotten to read that
1365207619Srdivacky  // paragraph and insist on sending messages to nil that have structure
1366207619Srdivacky  // returns.  With GCC, this generates a random return value (whatever happens
1367207619Srdivacky  // to be on the stack / in those registers at the time) on most platforms,
1368221345Sdim  // and generates an illegal instruction trap on SPARC.  With LLVM it corrupts
1369221345Sdim  // the stack.
1370221345Sdim  bool isPointerSizedReturn = (ResultType->isAnyPointerType() ||
1371221345Sdim      ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType());
1372207619Srdivacky
1373276479Sdim  llvm::BasicBlock *startBB = nullptr;
1374276479Sdim  llvm::BasicBlock *messageBB = nullptr;
1375276479Sdim  llvm::BasicBlock *continueBB = nullptr;
1376207619Srdivacky
1377207619Srdivacky  if (!isPointerSizedReturn) {
1378207619Srdivacky    startBB = Builder.GetInsertBlock();
1379207619Srdivacky    messageBB = CGF.createBasicBlock("msgSend");
1380208600Srdivacky    continueBB = CGF.createBasicBlock("continue");
1381207619Srdivacky
1382207619Srdivacky    llvm::Value *isNil = Builder.CreateICmpEQ(Receiver,
1383207619Srdivacky            llvm::Constant::getNullValue(Receiver->getType()));
1384208600Srdivacky    Builder.CreateCondBr(isNil, continueBB, messageBB);
1385207619Srdivacky    CGF.EmitBlock(messageBB);
1386207619Srdivacky  }
1387207619Srdivacky
1388198092Srdivacky  IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
1389193326Sed  llvm::Value *cmd;
1390193326Sed  if (Method)
1391249423Sdim    cmd = GetSelector(CGF, Method);
1392193326Sed  else
1393249423Sdim    cmd = GetSelector(CGF, Sel);
1394221345Sdim  cmd = EnforceType(Builder, cmd, SelectorTy);
1395221345Sdim  Receiver = EnforceType(Builder, Receiver, IdTy);
1396221345Sdim
1397280031Sdim  llvm::Metadata *impMD[] = {
1398280031Sdim      llvm::MDString::get(VMContext, Sel.getAsString()),
1399280031Sdim      llvm::MDString::get(VMContext, Class ? Class->getNameAsString() : ""),
1400280031Sdim      llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1401280031Sdim          llvm::Type::getInt1Ty(VMContext), Class != nullptr))};
1402221345Sdim  llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
1403221345Sdim
1404193326Sed  CallArgList ActualArgs;
1405221345Sdim  ActualArgs.add(RValue::get(Receiver), ASTIdTy);
1406221345Sdim  ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
1407224145Sdim  ActualArgs.addFrom(CallArgs);
1408193326Sed
1409234353Sdim  MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
1410193326Sed
1411234353Sdim  // Get the IMP to call
1412234353Sdim  llvm::Value *imp;
1413207619Srdivacky
1414234353Sdim  // If we have non-legacy dispatch specified, we try using the objc_msgSend()
1415234353Sdim  // functions.  These are not supported on all platforms (or all runtimes on a
1416234353Sdim  // given platform), so we
1417234353Sdim  switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
1418234353Sdim    case CodeGenOptions::Legacy:
1419261991Sdim      imp = LookupIMP(CGF, Receiver, cmd, node, MSI);
1420234353Sdim      break;
1421234353Sdim    case CodeGenOptions::Mixed:
1422234353Sdim    case CodeGenOptions::NonLegacy:
1423234353Sdim      if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1424234353Sdim        imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
1425234353Sdim                                  "objc_msgSend_fpret");
1426234353Sdim      } else if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
1427234353Sdim        // The actual types here don't matter - we're going to bitcast the
1428234353Sdim        // function anyway
1429234353Sdim        imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
1430234353Sdim                                  "objc_msgSend_stret");
1431234353Sdim      } else {
1432234353Sdim        imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
1433234353Sdim                                  "objc_msgSend");
1434234353Sdim      }
1435234353Sdim  }
1436234353Sdim
1437234353Sdim  // Reset the receiver in case the lookup modified it
1438234353Sdim  ActualArgs[0] = CallArg(RValue::get(Receiver), ASTIdTy, false);
1439234353Sdim
1440234353Sdim  imp = EnforceType(Builder, imp, MSI.MessengerType);
1441234353Sdim
1442207619Srdivacky  llvm::Instruction *call;
1443296417Sdim  RValue msgRet = CGF.EmitCall(MSI.CallInfo, imp, Return, ActualArgs,
1444296417Sdim                               CGCalleeInfo(), &call);
1445207619Srdivacky  call->setMetadata(msgSendMDKind, node);
1446193326Sed
1447208600Srdivacky
1448207619Srdivacky  if (!isPointerSizedReturn) {
1449208600Srdivacky    messageBB = CGF.Builder.GetInsertBlock();
1450208600Srdivacky    CGF.Builder.CreateBr(continueBB);
1451208600Srdivacky    CGF.EmitBlock(continueBB);
1452207619Srdivacky    if (msgRet.isScalar()) {
1453207619Srdivacky      llvm::Value *v = msgRet.getScalarVal();
1454221345Sdim      llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
1455207619Srdivacky      phi->addIncoming(v, messageBB);
1456207619Srdivacky      phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB);
1457207619Srdivacky      msgRet = RValue::get(phi);
1458207619Srdivacky    } else if (msgRet.isAggregate()) {
1459296417Sdim      Address v = msgRet.getAggregateAddress();
1460296417Sdim      llvm::PHINode *phi = Builder.CreatePHI(v.getType(), 2);
1461296417Sdim      llvm::Type *RetTy = v.getElementType();
1462296417Sdim      Address NullVal = CGF.CreateTempAlloca(RetTy, v.getAlignment(), "null");
1463296417Sdim      CGF.InitTempAlloca(NullVal, llvm::Constant::getNullValue(RetTy));
1464296417Sdim      phi->addIncoming(v.getPointer(), messageBB);
1465296417Sdim      phi->addIncoming(NullVal.getPointer(), startBB);
1466296417Sdim      msgRet = RValue::getAggregate(Address(phi, v.getAlignment()));
1467207619Srdivacky    } else /* isComplex() */ {
1468207619Srdivacky      std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal();
1469221345Sdim      llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2);
1470207619Srdivacky      phi->addIncoming(v.first, messageBB);
1471207619Srdivacky      phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()),
1472207619Srdivacky          startBB);
1473221345Sdim      llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2);
1474207619Srdivacky      phi2->addIncoming(v.second, messageBB);
1475207619Srdivacky      phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()),
1476207619Srdivacky          startBB);
1477207619Srdivacky      msgRet = RValue::getComplex(phi, phi2);
1478207619Srdivacky    }
1479207619Srdivacky  }
1480207619Srdivacky  return msgRet;
1481193326Sed}
1482193326Sed
1483198092Srdivacky/// Generates a MethodList.  Used in construction of a objc_class and
1484193326Sed/// objc_category structures.
1485234353Sdimllvm::Constant *CGObjCGNU::
1486280031SdimGenerateMethodList(StringRef ClassName,
1487280031Sdim                   StringRef CategoryName,
1488234353Sdim                   ArrayRef<Selector> MethodSels,
1489234353Sdim                   ArrayRef<llvm::Constant *> MethodTypes,
1490234353Sdim                   bool isClassMethodList) {
1491198092Srdivacky  if (MethodSels.empty())
1492198092Srdivacky    return NULLPtr;
1493198092Srdivacky  // Get the method structure type.
1494224145Sdim  llvm::StructType *ObjCMethodTy = llvm::StructType::get(
1495193326Sed    PtrToInt8Ty, // Really a selector, but the runtime creates it us.
1496193326Sed    PtrToInt8Ty, // Method types
1497221345Sdim    IMPTy, //Method pointer
1498276479Sdim    nullptr);
1499193326Sed  std::vector<llvm::Constant*> Methods;
1500193326Sed  std::vector<llvm::Constant*> Elements;
1501193326Sed  for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
1502193326Sed    Elements.clear();
1503221345Sdim    llvm::Constant *Method =
1504193326Sed      TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
1505221345Sdim                                                MethodSels[i],
1506221345Sdim                                                isClassMethodList));
1507221345Sdim    assert(Method && "Can't generate metadata for method that doesn't exist");
1508221345Sdim    llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
1509221345Sdim    Elements.push_back(C);
1510221345Sdim    Elements.push_back(MethodTypes[i]);
1511221345Sdim    Method = llvm::ConstantExpr::getBitCast(Method,
1512221345Sdim        IMPTy);
1513221345Sdim    Elements.push_back(Method);
1514221345Sdim    Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
1515193326Sed  }
1516193326Sed
1517193326Sed  // Array of method structures
1518193326Sed  llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
1519193326Sed                                                            Methods.size());
1520193326Sed  llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
1521193326Sed                                                         Methods);
1522193326Sed
1523193326Sed  // Structure containing list pointer, array and array count
1524226633Sdim  llvm::StructType *ObjCMethodListTy = llvm::StructType::create(VMContext);
1525224145Sdim  llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(ObjCMethodListTy);
1526224145Sdim  ObjCMethodListTy->setBody(
1527198092Srdivacky      NextPtrTy,
1528198092Srdivacky      IntTy,
1529193326Sed      ObjCMethodArrayTy,
1530276479Sdim      nullptr);
1531193326Sed
1532193326Sed  Methods.clear();
1533193326Sed  Methods.push_back(llvm::ConstantPointerNull::get(
1534193326Sed        llvm::PointerType::getUnqual(ObjCMethodListTy)));
1535226633Sdim  Methods.push_back(llvm::ConstantInt::get(Int32Ty, MethodTypes.size()));
1536193326Sed  Methods.push_back(MethodArray);
1537198092Srdivacky
1538193326Sed  // Create an instance of the structure
1539296417Sdim  return MakeGlobal(ObjCMethodListTy, Methods, CGM.getPointerAlign(),
1540296417Sdim                    ".objc_method_list");
1541193326Sed}
1542193326Sed
1543193326Sed/// Generates an IvarList.  Used in construction of a objc_class.
1544234353Sdimllvm::Constant *CGObjCGNU::
1545234353SdimGenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
1546234353Sdim                 ArrayRef<llvm::Constant *> IvarTypes,
1547234353Sdim                 ArrayRef<llvm::Constant *> IvarOffsets) {
1548199482Srdivacky  if (IvarNames.size() == 0)
1549199482Srdivacky    return NULLPtr;
1550198092Srdivacky  // Get the method structure type.
1551224145Sdim  llvm::StructType *ObjCIvarTy = llvm::StructType::get(
1552193326Sed    PtrToInt8Ty,
1553193326Sed    PtrToInt8Ty,
1554193326Sed    IntTy,
1555276479Sdim    nullptr);
1556193326Sed  std::vector<llvm::Constant*> Ivars;
1557193326Sed  std::vector<llvm::Constant*> Elements;
1558193326Sed  for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
1559193326Sed    Elements.clear();
1560198092Srdivacky    Elements.push_back(IvarNames[i]);
1561198092Srdivacky    Elements.push_back(IvarTypes[i]);
1562193326Sed    Elements.push_back(IvarOffsets[i]);
1563193326Sed    Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
1564193326Sed  }
1565193326Sed
1566193326Sed  // Array of method structures
1567193326Sed  llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
1568193326Sed      IvarNames.size());
1569193326Sed
1570198092Srdivacky
1571193326Sed  Elements.clear();
1572193326Sed  Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
1573193326Sed  Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
1574193326Sed  // Structure containing array and array count
1575224145Sdim  llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
1576193326Sed    ObjCIvarArrayTy,
1577276479Sdim    nullptr);
1578193326Sed
1579193326Sed  // Create an instance of the structure
1580296417Sdim  return MakeGlobal(ObjCIvarListTy, Elements, CGM.getPointerAlign(),
1581296417Sdim                    ".objc_ivar_list");
1582193326Sed}
1583193326Sed
1584193326Sed/// Generate a class structure
1585193326Sedllvm::Constant *CGObjCGNU::GenerateClassStructure(
1586193326Sed    llvm::Constant *MetaClass,
1587193326Sed    llvm::Constant *SuperClass,
1588193326Sed    unsigned info,
1589193326Sed    const char *Name,
1590193326Sed    llvm::Constant *Version,
1591193326Sed    llvm::Constant *InstanceSize,
1592193326Sed    llvm::Constant *IVars,
1593193326Sed    llvm::Constant *Methods,
1594198092Srdivacky    llvm::Constant *Protocols,
1595198092Srdivacky    llvm::Constant *IvarOffsets,
1596207619Srdivacky    llvm::Constant *Properties,
1597226633Sdim    llvm::Constant *StrongIvarBitmap,
1598226633Sdim    llvm::Constant *WeakIvarBitmap,
1599207619Srdivacky    bool isMeta) {
1600193326Sed  // Set up the class structure
1601193326Sed  // Note:  Several of these are char*s when they should be ids.  This is
1602193326Sed  // because the runtime performs this translation on load.
1603198092Srdivacky  //
1604198092Srdivacky  // Fields marked New ABI are part of the GNUstep runtime.  We emit them
1605198092Srdivacky  // anyway; the classes will still work with the GNU runtime, they will just
1606198092Srdivacky  // be ignored.
1607224145Sdim  llvm::StructType *ClassTy = llvm::StructType::get(
1608234353Sdim      PtrToInt8Ty,        // isa
1609193326Sed      PtrToInt8Ty,        // super_class
1610193326Sed      PtrToInt8Ty,        // name
1611193326Sed      LongTy,             // version
1612193326Sed      LongTy,             // info
1613193326Sed      LongTy,             // instance_size
1614193326Sed      IVars->getType(),   // ivars
1615193326Sed      Methods->getType(), // methods
1616198092Srdivacky      // These are all filled in by the runtime, so we pretend
1617193326Sed      PtrTy,              // dtable
1618193326Sed      PtrTy,              // subclass_list
1619193326Sed      PtrTy,              // sibling_class
1620193326Sed      PtrTy,              // protocols
1621193326Sed      PtrTy,              // gc_object_type
1622198092Srdivacky      // New ABI:
1623198092Srdivacky      LongTy,                 // abi_version
1624198092Srdivacky      IvarOffsets->getType(), // ivar_offsets
1625198092Srdivacky      Properties->getType(),  // properties
1626228379Sdim      IntPtrTy,               // strong_pointers
1627228379Sdim      IntPtrTy,               // weak_pointers
1628276479Sdim      nullptr);
1629193326Sed  llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
1630193326Sed  // Fill in the structure
1631193326Sed  std::vector<llvm::Constant*> Elements;
1632193326Sed  Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
1633193326Sed  Elements.push_back(SuperClass);
1634193326Sed  Elements.push_back(MakeConstantString(Name, ".class_name"));
1635193326Sed  Elements.push_back(Zero);
1636193326Sed  Elements.push_back(llvm::ConstantInt::get(LongTy, info));
1637219077Sdim  if (isMeta) {
1638243830Sdim    llvm::DataLayout td(&TheModule);
1639221345Sdim    Elements.push_back(
1640221345Sdim        llvm::ConstantInt::get(LongTy,
1641221345Sdim                               td.getTypeSizeInBits(ClassTy) /
1642221345Sdim                                 CGM.getContext().getCharWidth()));
1643219077Sdim  } else
1644219077Sdim    Elements.push_back(InstanceSize);
1645193326Sed  Elements.push_back(IVars);
1646193326Sed  Elements.push_back(Methods);
1647198092Srdivacky  Elements.push_back(NULLPtr);
1648198092Srdivacky  Elements.push_back(NULLPtr);
1649198092Srdivacky  Elements.push_back(NULLPtr);
1650193326Sed  Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
1651198092Srdivacky  Elements.push_back(NULLPtr);
1652226633Sdim  Elements.push_back(llvm::ConstantInt::get(LongTy, 1));
1653198092Srdivacky  Elements.push_back(IvarOffsets);
1654198092Srdivacky  Elements.push_back(Properties);
1655226633Sdim  Elements.push_back(StrongIvarBitmap);
1656226633Sdim  Elements.push_back(WeakIvarBitmap);
1657193326Sed  // Create an instance of the structure
1658202379Srdivacky  // This is now an externally visible symbol, so that we can speed up class
1659234353Sdim  // messages in the next ABI.  We may already have some weak references to
1660234353Sdim  // this, so check and fix them properly.
1661234353Sdim  std::string ClassSym((isMeta ? "_OBJC_METACLASS_": "_OBJC_CLASS_") +
1662234353Sdim          std::string(Name));
1663234353Sdim  llvm::GlobalVariable *ClassRef = TheModule.getNamedGlobal(ClassSym);
1664296417Sdim  llvm::Constant *Class =
1665296417Sdim    MakeGlobal(ClassTy, Elements, CGM.getPointerAlign(), ClassSym,
1666296417Sdim               llvm::GlobalValue::ExternalLinkage);
1667234353Sdim  if (ClassRef) {
1668234353Sdim      ClassRef->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(Class,
1669234353Sdim                  ClassRef->getType()));
1670234353Sdim      ClassRef->removeFromParent();
1671234353Sdim      Class->setName(ClassSym);
1672234353Sdim  }
1673234353Sdim  return Class;
1674193326Sed}
1675193326Sed
1676234353Sdimllvm::Constant *CGObjCGNU::
1677234353SdimGenerateProtocolMethodList(ArrayRef<llvm::Constant *> MethodNames,
1678234353Sdim                           ArrayRef<llvm::Constant *> MethodTypes) {
1679198092Srdivacky  // Get the method structure type.
1680224145Sdim  llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
1681193326Sed    PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
1682193326Sed    PtrToInt8Ty,
1683276479Sdim    nullptr);
1684193326Sed  std::vector<llvm::Constant*> Methods;
1685193326Sed  std::vector<llvm::Constant*> Elements;
1686193326Sed  for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
1687193326Sed    Elements.clear();
1688198092Srdivacky    Elements.push_back(MethodNames[i]);
1689198092Srdivacky    Elements.push_back(MethodTypes[i]);
1690193326Sed    Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
1691193326Sed  }
1692193326Sed  llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
1693193326Sed      MethodNames.size());
1694198092Srdivacky  llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
1695198092Srdivacky                                                   Methods);
1696224145Sdim  llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
1697276479Sdim      IntTy, ObjCMethodArrayTy, nullptr);
1698193326Sed  Methods.clear();
1699193326Sed  Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
1700193326Sed  Methods.push_back(Array);
1701296417Sdim  return MakeGlobal(ObjCMethodDescListTy, Methods, CGM.getPointerAlign(),
1702296417Sdim                    ".objc_method_list");
1703193326Sed}
1704198092Srdivacky
1705193326Sed// Create the protocol list structure used in classes, categories and so on
1706234353Sdimllvm::Constant *CGObjCGNU::GenerateProtocolList(ArrayRef<std::string>Protocols){
1707193326Sed  llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
1708193326Sed      Protocols.size());
1709224145Sdim  llvm::StructType *ProtocolListTy = llvm::StructType::get(
1710193326Sed      PtrTy, //Should be a recurisve pointer, but it's always NULL here.
1711218893Sdim      SizeTy,
1712193326Sed      ProtocolArrayTy,
1713276479Sdim      nullptr);
1714198092Srdivacky  std::vector<llvm::Constant*> Elements;
1715193326Sed  for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
1716193326Sed      iter != endIter ; iter++) {
1717276479Sdim    llvm::Constant *protocol = nullptr;
1718199990Srdivacky    llvm::StringMap<llvm::Constant*>::iterator value =
1719199990Srdivacky      ExistingProtocols.find(*iter);
1720199990Srdivacky    if (value == ExistingProtocols.end()) {
1721193326Sed      protocol = GenerateEmptyProtocol(*iter);
1722199990Srdivacky    } else {
1723199990Srdivacky      protocol = value->getValue();
1724199990Srdivacky    }
1725198092Srdivacky    llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
1726198092Srdivacky                                                           PtrToInt8Ty);
1727193326Sed    Elements.push_back(Ptr);
1728193326Sed  }
1729193326Sed  llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1730193326Sed      Elements);
1731193326Sed  Elements.clear();
1732193326Sed  Elements.push_back(NULLPtr);
1733193326Sed  Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
1734193326Sed  Elements.push_back(ProtocolArray);
1735296417Sdim  return MakeGlobal(ProtocolListTy, Elements, CGM.getPointerAlign(),
1736296417Sdim                    ".objc_protocol_list");
1737193326Sed}
1738193326Sed
1739249423Sdimllvm::Value *CGObjCGNU::GenerateProtocolRef(CodeGenFunction &CGF,
1740193326Sed                                            const ObjCProtocolDecl *PD) {
1741193326Sed  llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
1742226633Sdim  llvm::Type *T =
1743193326Sed    CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
1744249423Sdim  return CGF.Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
1745193326Sed}
1746193326Sed
1747193326Sedllvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
1748193326Sed  const std::string &ProtocolName) {
1749226633Sdim  SmallVector<std::string, 0> EmptyStringVector;
1750226633Sdim  SmallVector<llvm::Constant*, 0> EmptyConstantVector;
1751193326Sed
1752193326Sed  llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
1753198092Srdivacky  llvm::Constant *MethodList =
1754193326Sed    GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
1755193326Sed  // Protocols are objects containing lists of the methods implemented and
1756193326Sed  // protocols adopted.
1757224145Sdim  llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
1758193326Sed      PtrToInt8Ty,
1759193326Sed      ProtocolList->getType(),
1760198092Srdivacky      MethodList->getType(),
1761198092Srdivacky      MethodList->getType(),
1762198092Srdivacky      MethodList->getType(),
1763198092Srdivacky      MethodList->getType(),
1764276479Sdim      nullptr);
1765198092Srdivacky  std::vector<llvm::Constant*> Elements;
1766193326Sed  // The isa pointer must be set to a magic number so the runtime knows it's
1767193326Sed  // the correct layout.
1768193326Sed  Elements.push_back(llvm::ConstantExpr::getIntToPtr(
1769226633Sdim        llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
1770193326Sed  Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1771193326Sed  Elements.push_back(ProtocolList);
1772198092Srdivacky  Elements.push_back(MethodList);
1773198092Srdivacky  Elements.push_back(MethodList);
1774198092Srdivacky  Elements.push_back(MethodList);
1775198092Srdivacky  Elements.push_back(MethodList);
1776296417Sdim  return MakeGlobal(ProtocolTy, Elements, CGM.getPointerAlign(),
1777296417Sdim                    ".objc_protocol");
1778193326Sed}
1779193326Sed
1780193326Sedvoid CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
1781193326Sed  ASTContext &Context = CGM.getContext();
1782193326Sed  std::string ProtocolName = PD->getNameAsString();
1783234353Sdim
1784234353Sdim  // Use the protocol definition, if there is one.
1785234353Sdim  if (const ObjCProtocolDecl *Def = PD->getDefinition())
1786234353Sdim    PD = Def;
1787234353Sdim
1788226633Sdim  SmallVector<std::string, 16> Protocols;
1789276479Sdim  for (const auto *PI : PD->protocols())
1790276479Sdim    Protocols.push_back(PI->getNameAsString());
1791226633Sdim  SmallVector<llvm::Constant*, 16> InstanceMethodNames;
1792226633Sdim  SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
1793226633Sdim  SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames;
1794226633Sdim  SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes;
1795276479Sdim  for (const auto *I : PD->instance_methods()) {
1796193326Sed    std::string TypeStr;
1797276479Sdim    Context.getObjCEncodingForMethodDecl(I, TypeStr);
1798276479Sdim    if (I->getImplementationControl() == ObjCMethodDecl::Optional) {
1799243830Sdim      OptionalInstanceMethodNames.push_back(
1800276479Sdim          MakeConstantString(I->getSelector().getAsString()));
1801243830Sdim      OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1802243830Sdim    } else {
1803198092Srdivacky      InstanceMethodNames.push_back(
1804276479Sdim          MakeConstantString(I->getSelector().getAsString()));
1805198092Srdivacky      InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1806198092Srdivacky    }
1807193326Sed  }
1808193326Sed  // Collect information about class methods:
1809226633Sdim  SmallVector<llvm::Constant*, 16> ClassMethodNames;
1810226633Sdim  SmallVector<llvm::Constant*, 16> ClassMethodTypes;
1811226633Sdim  SmallVector<llvm::Constant*, 16> OptionalClassMethodNames;
1812226633Sdim  SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes;
1813276479Sdim  for (const auto *I : PD->class_methods()) {
1814193326Sed    std::string TypeStr;
1815276479Sdim    Context.getObjCEncodingForMethodDecl(I,TypeStr);
1816276479Sdim    if (I->getImplementationControl() == ObjCMethodDecl::Optional) {
1817243830Sdim      OptionalClassMethodNames.push_back(
1818276479Sdim          MakeConstantString(I->getSelector().getAsString()));
1819243830Sdim      OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr));
1820243830Sdim    } else {
1821198092Srdivacky      ClassMethodNames.push_back(
1822276479Sdim          MakeConstantString(I->getSelector().getAsString()));
1823198092Srdivacky      ClassMethodTypes.push_back(MakeConstantString(TypeStr));
1824198092Srdivacky    }
1825193326Sed  }
1826193326Sed
1827193326Sed  llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
1828193326Sed  llvm::Constant *InstanceMethodList =
1829193326Sed    GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
1830193326Sed  llvm::Constant *ClassMethodList =
1831193326Sed    GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
1832198092Srdivacky  llvm::Constant *OptionalInstanceMethodList =
1833198092Srdivacky    GenerateProtocolMethodList(OptionalInstanceMethodNames,
1834198092Srdivacky            OptionalInstanceMethodTypes);
1835198092Srdivacky  llvm::Constant *OptionalClassMethodList =
1836198092Srdivacky    GenerateProtocolMethodList(OptionalClassMethodNames,
1837198092Srdivacky            OptionalClassMethodTypes);
1838198092Srdivacky
1839198092Srdivacky  // Property metadata: name, attributes, isSynthesized, setter name, setter
1840198092Srdivacky  // types, getter name, getter types.
1841198092Srdivacky  // The isSynthesized value is always set to 0 in a protocol.  It exists to
1842198092Srdivacky  // simplify the runtime library by allowing it to use the same data
1843198092Srdivacky  // structures for protocol metadata everywhere.
1844224145Sdim  llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
1845249423Sdim          PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty,
1846276479Sdim          PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, nullptr);
1847198092Srdivacky  std::vector<llvm::Constant*> Properties;
1848198092Srdivacky  std::vector<llvm::Constant*> OptionalProperties;
1849198092Srdivacky
1850198092Srdivacky  // Add all of the property methods need adding to the method list and to the
1851198092Srdivacky  // property metadata list.
1852276479Sdim  for (auto *property : PD->properties()) {
1853198092Srdivacky    std::vector<llvm::Constant*> Fields;
1854198092Srdivacky
1855276479Sdim    Fields.push_back(MakePropertyEncodingString(property, nullptr));
1856249423Sdim    PushPropertyAttributes(Fields, property);
1857243830Sdim
1858198092Srdivacky    if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1859198092Srdivacky      std::string TypeStr;
1860198092Srdivacky      Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1861198092Srdivacky      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1862198092Srdivacky      InstanceMethodTypes.push_back(TypeEncoding);
1863198092Srdivacky      Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1864198092Srdivacky      Fields.push_back(TypeEncoding);
1865198092Srdivacky    } else {
1866198092Srdivacky      Fields.push_back(NULLPtr);
1867198092Srdivacky      Fields.push_back(NULLPtr);
1868198092Srdivacky    }
1869198092Srdivacky    if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1870198092Srdivacky      std::string TypeStr;
1871198092Srdivacky      Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1872198092Srdivacky      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1873198092Srdivacky      InstanceMethodTypes.push_back(TypeEncoding);
1874198092Srdivacky      Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1875198092Srdivacky      Fields.push_back(TypeEncoding);
1876198092Srdivacky    } else {
1877198092Srdivacky      Fields.push_back(NULLPtr);
1878198092Srdivacky      Fields.push_back(NULLPtr);
1879198092Srdivacky    }
1880198092Srdivacky    if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) {
1881198092Srdivacky      OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1882198092Srdivacky    } else {
1883198092Srdivacky      Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1884198092Srdivacky    }
1885198092Srdivacky  }
1886198092Srdivacky  llvm::Constant *PropertyArray = llvm::ConstantArray::get(
1887198092Srdivacky      llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties);
1888198092Srdivacky  llvm::Constant* PropertyListInitFields[] =
1889198092Srdivacky    {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1890198092Srdivacky
1891198092Srdivacky  llvm::Constant *PropertyListInit =
1892224145Sdim      llvm::ConstantStruct::getAnon(PropertyListInitFields);
1893198092Srdivacky  llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule,
1894198092Srdivacky      PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage,
1895198092Srdivacky      PropertyListInit, ".objc_property_list");
1896198092Srdivacky
1897198092Srdivacky  llvm::Constant *OptionalPropertyArray =
1898198092Srdivacky      llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy,
1899198092Srdivacky          OptionalProperties.size()) , OptionalProperties);
1900198092Srdivacky  llvm::Constant* OptionalPropertyListInitFields[] = {
1901198092Srdivacky      llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr,
1902198092Srdivacky      OptionalPropertyArray };
1903198092Srdivacky
1904198092Srdivacky  llvm::Constant *OptionalPropertyListInit =
1905224145Sdim      llvm::ConstantStruct::getAnon(OptionalPropertyListInitFields);
1906198092Srdivacky  llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule,
1907198092Srdivacky          OptionalPropertyListInit->getType(), false,
1908198092Srdivacky          llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit,
1909198092Srdivacky          ".objc_property_list");
1910198092Srdivacky
1911193326Sed  // Protocols are objects containing lists of the methods implemented and
1912193326Sed  // protocols adopted.
1913224145Sdim  llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
1914193326Sed      PtrToInt8Ty,
1915193326Sed      ProtocolList->getType(),
1916193326Sed      InstanceMethodList->getType(),
1917193326Sed      ClassMethodList->getType(),
1918198092Srdivacky      OptionalInstanceMethodList->getType(),
1919198092Srdivacky      OptionalClassMethodList->getType(),
1920198092Srdivacky      PropertyList->getType(),
1921198092Srdivacky      OptionalPropertyList->getType(),
1922276479Sdim      nullptr);
1923198092Srdivacky  std::vector<llvm::Constant*> Elements;
1924193326Sed  // The isa pointer must be set to a magic number so the runtime knows it's
1925193326Sed  // the correct layout.
1926193326Sed  Elements.push_back(llvm::ConstantExpr::getIntToPtr(
1927226633Sdim        llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
1928193326Sed  Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1929193326Sed  Elements.push_back(ProtocolList);
1930193326Sed  Elements.push_back(InstanceMethodList);
1931193326Sed  Elements.push_back(ClassMethodList);
1932198092Srdivacky  Elements.push_back(OptionalInstanceMethodList);
1933198092Srdivacky  Elements.push_back(OptionalClassMethodList);
1934198092Srdivacky  Elements.push_back(PropertyList);
1935198092Srdivacky  Elements.push_back(OptionalPropertyList);
1936198092Srdivacky  ExistingProtocols[ProtocolName] =
1937193326Sed    llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
1938296417Sdim          CGM.getPointerAlign(), ".objc_protocol"), IdTy);
1939193326Sed}
1940249423Sdimvoid CGObjCGNU::GenerateProtocolHolderCategory() {
1941198092Srdivacky  // Collect information about instance methods
1942226633Sdim  SmallVector<Selector, 1> MethodSels;
1943226633Sdim  SmallVector<llvm::Constant*, 1> MethodTypes;
1944193326Sed
1945198092Srdivacky  std::vector<llvm::Constant*> Elements;
1946198092Srdivacky  const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
1947198092Srdivacky  const std::string CategoryName = "AnotherHack";
1948198092Srdivacky  Elements.push_back(MakeConstantString(CategoryName));
1949198092Srdivacky  Elements.push_back(MakeConstantString(ClassName));
1950198092Srdivacky  // Instance method list
1951198092Srdivacky  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1952198092Srdivacky          ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));
1953198092Srdivacky  // Class method list
1954198092Srdivacky  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1955198092Srdivacky          ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));
1956198092Srdivacky  // Protocol list
1957198092Srdivacky  llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy,
1958198092Srdivacky      ExistingProtocols.size());
1959224145Sdim  llvm::StructType *ProtocolListTy = llvm::StructType::get(
1960198092Srdivacky      PtrTy, //Should be a recurisve pointer, but it's always NULL here.
1961218893Sdim      SizeTy,
1962198092Srdivacky      ProtocolArrayTy,
1963276479Sdim      nullptr);
1964198092Srdivacky  std::vector<llvm::Constant*> ProtocolElements;
1965198092Srdivacky  for (llvm::StringMapIterator<llvm::Constant*> iter =
1966198092Srdivacky       ExistingProtocols.begin(), endIter = ExistingProtocols.end();
1967198092Srdivacky       iter != endIter ; iter++) {
1968198092Srdivacky    llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(),
1969198092Srdivacky            PtrTy);
1970198092Srdivacky    ProtocolElements.push_back(Ptr);
1971198092Srdivacky  }
1972198092Srdivacky  llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1973198092Srdivacky      ProtocolElements);
1974198092Srdivacky  ProtocolElements.clear();
1975198092Srdivacky  ProtocolElements.push_back(NULLPtr);
1976198092Srdivacky  ProtocolElements.push_back(llvm::ConstantInt::get(LongTy,
1977198092Srdivacky              ExistingProtocols.size()));
1978198092Srdivacky  ProtocolElements.push_back(ProtocolArray);
1979198092Srdivacky  Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy,
1980296417Sdim                  ProtocolElements, CGM.getPointerAlign(),
1981296417Sdim                  ".objc_protocol_list"), PtrTy));
1982198092Srdivacky  Categories.push_back(llvm::ConstantExpr::getBitCast(
1983224145Sdim        MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
1984296417Sdim            PtrTy, PtrTy, PtrTy, nullptr), Elements, CGM.getPointerAlign()),
1985296417Sdim        PtrTy));
1986198092Srdivacky}
1987198092Srdivacky
1988226633Sdim/// Libobjc2 uses a bitfield representation where small(ish) bitfields are
1989226633Sdim/// stored in a 64-bit value with the low bit set to 1 and the remaining 63
1990226633Sdim/// bits set to their values, LSB first, while larger ones are stored in a
1991226633Sdim/// structure of this / form:
1992226633Sdim///
1993226633Sdim/// struct { int32_t length; int32_t values[length]; };
1994226633Sdim///
1995226633Sdim/// The values in the array are stored in host-endian format, with the least
1996226633Sdim/// significant bit being assumed to come first in the bitfield.  Therefore, a
1997226633Sdim/// bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] }, while a
1998226633Sdim/// bitfield / with the 63rd bit set will be 1<<64.
1999234353Sdimllvm::Constant *CGObjCGNU::MakeBitField(ArrayRef<bool> bits) {
2000226633Sdim  int bitCount = bits.size();
2001276479Sdim  int ptrBits = CGM.getDataLayout().getPointerSizeInBits();
2002228379Sdim  if (bitCount < ptrBits) {
2003226633Sdim    uint64_t val = 1;
2004226633Sdim    for (int i=0 ; i<bitCount ; ++i) {
2005226633Sdim      if (bits[i]) val |= 1ULL<<(i+1);
2006226633Sdim    }
2007228379Sdim    return llvm::ConstantInt::get(IntPtrTy, val);
2008226633Sdim  }
2009249423Sdim  SmallVector<llvm::Constant *, 8> values;
2010226633Sdim  int v=0;
2011226633Sdim  while (v < bitCount) {
2012226633Sdim    int32_t word = 0;
2013226633Sdim    for (int i=0 ; (i<32) && (v<bitCount)  ; ++i) {
2014226633Sdim      if (bits[v]) word |= 1<<i;
2015226633Sdim      v++;
2016226633Sdim    }
2017226633Sdim    values.push_back(llvm::ConstantInt::get(Int32Ty, word));
2018226633Sdim  }
2019226633Sdim  llvm::ArrayType *arrayTy = llvm::ArrayType::get(Int32Ty, values.size());
2020226633Sdim  llvm::Constant *array = llvm::ConstantArray::get(arrayTy, values);
2021226633Sdim  llvm::Constant *fields[2] = {
2022226633Sdim      llvm::ConstantInt::get(Int32Ty, values.size()),
2023226633Sdim      array };
2024226633Sdim  llvm::Constant *GS = MakeGlobal(llvm::StructType::get(Int32Ty, arrayTy,
2025296417Sdim        nullptr), fields, CharUnits::fromQuantity(4));
2026226633Sdim  llvm::Constant *ptr = llvm::ConstantExpr::getPtrToInt(GS, IntPtrTy);
2027226633Sdim  return ptr;
2028226633Sdim}
2029226633Sdim
2030193326Sedvoid CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
2031193326Sed  std::string ClassName = OCD->getClassInterface()->getNameAsString();
2032193326Sed  std::string CategoryName = OCD->getNameAsString();
2033193326Sed  // Collect information about instance methods
2034226633Sdim  SmallVector<Selector, 16> InstanceMethodSels;
2035226633Sdim  SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
2036276479Sdim  for (const auto *I : OCD->instance_methods()) {
2037276479Sdim    InstanceMethodSels.push_back(I->getSelector());
2038193326Sed    std::string TypeStr;
2039276479Sdim    CGM.getContext().getObjCEncodingForMethodDecl(I,TypeStr);
2040198092Srdivacky    InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
2041193326Sed  }
2042193326Sed
2043193326Sed  // Collect information about class methods
2044226633Sdim  SmallVector<Selector, 16> ClassMethodSels;
2045226633Sdim  SmallVector<llvm::Constant*, 16> ClassMethodTypes;
2046276479Sdim  for (const auto *I : OCD->class_methods()) {
2047276479Sdim    ClassMethodSels.push_back(I->getSelector());
2048193326Sed    std::string TypeStr;
2049276479Sdim    CGM.getContext().getObjCEncodingForMethodDecl(I,TypeStr);
2050198092Srdivacky    ClassMethodTypes.push_back(MakeConstantString(TypeStr));
2051193326Sed  }
2052193326Sed
2053193326Sed  // Collect the names of referenced protocols
2054226633Sdim  SmallVector<std::string, 16> Protocols;
2055205219Srdivacky  const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl();
2056205219Srdivacky  const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols();
2057193326Sed  for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
2058193326Sed       E = Protos.end(); I != E; ++I)
2059193326Sed    Protocols.push_back((*I)->getNameAsString());
2060193326Sed
2061193326Sed  std::vector<llvm::Constant*> Elements;
2062193326Sed  Elements.push_back(MakeConstantString(CategoryName));
2063193326Sed  Elements.push_back(MakeConstantString(ClassName));
2064198092Srdivacky  // Instance method list
2065193326Sed  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
2066193326Sed          ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
2067193326Sed          false), PtrTy));
2068193326Sed  // Class method list
2069193326Sed  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
2070193326Sed          ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
2071193326Sed        PtrTy));
2072193326Sed  // Protocol list
2073193326Sed  Elements.push_back(llvm::ConstantExpr::getBitCast(
2074193326Sed        GenerateProtocolList(Protocols), PtrTy));
2075193326Sed  Categories.push_back(llvm::ConstantExpr::getBitCast(
2076224145Sdim        MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
2077296417Sdim            PtrTy, PtrTy, PtrTy, nullptr), Elements, CGM.getPointerAlign()),
2078296417Sdim        PtrTy));
2079193326Sed}
2080193326Sed
2081198092Srdivackyllvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID,
2082226633Sdim        SmallVectorImpl<Selector> &InstanceMethodSels,
2083226633Sdim        SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) {
2084198092Srdivacky  ASTContext &Context = CGM.getContext();
2085249423Sdim  // Property metadata: name, attributes, attributes2, padding1, padding2,
2086249423Sdim  // setter name, setter types, getter name, getter types.
2087224145Sdim  llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
2088249423Sdim          PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty,
2089276479Sdim          PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, nullptr);
2090198092Srdivacky  std::vector<llvm::Constant*> Properties;
2091198092Srdivacky
2092198092Srdivacky  // Add all of the property methods need adding to the method list and to the
2093198092Srdivacky  // property metadata list.
2094276479Sdim  for (auto *propertyImpl : OID->property_impls()) {
2095198092Srdivacky    std::vector<llvm::Constant*> Fields;
2096276479Sdim    ObjCPropertyDecl *property = propertyImpl->getPropertyDecl();
2097204643Srdivacky    bool isSynthesized = (propertyImpl->getPropertyImplementation() ==
2098204643Srdivacky        ObjCPropertyImplDecl::Synthesize);
2099249423Sdim    bool isDynamic = (propertyImpl->getPropertyImplementation() ==
2100249423Sdim        ObjCPropertyImplDecl::Dynamic);
2101198092Srdivacky
2102243830Sdim    Fields.push_back(MakePropertyEncodingString(property, OID));
2103249423Sdim    PushPropertyAttributes(Fields, property, isSynthesized, isDynamic);
2104198092Srdivacky    if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
2105198092Srdivacky      std::string TypeStr;
2106198092Srdivacky      Context.getObjCEncodingForMethodDecl(getter,TypeStr);
2107198092Srdivacky      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
2108204643Srdivacky      if (isSynthesized) {
2109204643Srdivacky        InstanceMethodTypes.push_back(TypeEncoding);
2110204643Srdivacky        InstanceMethodSels.push_back(getter->getSelector());
2111204643Srdivacky      }
2112198092Srdivacky      Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
2113198092Srdivacky      Fields.push_back(TypeEncoding);
2114198092Srdivacky    } else {
2115198092Srdivacky      Fields.push_back(NULLPtr);
2116198092Srdivacky      Fields.push_back(NULLPtr);
2117198092Srdivacky    }
2118198092Srdivacky    if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
2119198092Srdivacky      std::string TypeStr;
2120198092Srdivacky      Context.getObjCEncodingForMethodDecl(setter,TypeStr);
2121198092Srdivacky      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
2122204643Srdivacky      if (isSynthesized) {
2123204643Srdivacky        InstanceMethodTypes.push_back(TypeEncoding);
2124204643Srdivacky        InstanceMethodSels.push_back(setter->getSelector());
2125204643Srdivacky      }
2126198092Srdivacky      Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
2127198092Srdivacky      Fields.push_back(TypeEncoding);
2128198092Srdivacky    } else {
2129198092Srdivacky      Fields.push_back(NULLPtr);
2130198092Srdivacky      Fields.push_back(NULLPtr);
2131198092Srdivacky    }
2132198092Srdivacky    Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
2133198092Srdivacky  }
2134198092Srdivacky  llvm::ArrayType *PropertyArrayTy =
2135198092Srdivacky      llvm::ArrayType::get(PropertyMetadataTy, Properties.size());
2136198092Srdivacky  llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy,
2137198092Srdivacky          Properties);
2138198092Srdivacky  llvm::Constant* PropertyListInitFields[] =
2139198092Srdivacky    {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
2140198092Srdivacky
2141198092Srdivacky  llvm::Constant *PropertyListInit =
2142224145Sdim      llvm::ConstantStruct::getAnon(PropertyListInitFields);
2143198092Srdivacky  return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false,
2144198092Srdivacky          llvm::GlobalValue::InternalLinkage, PropertyListInit,
2145198092Srdivacky          ".objc_property_list");
2146198092Srdivacky}
2147198092Srdivacky
2148234353Sdimvoid CGObjCGNU::RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {
2149234353Sdim  // Get the class declaration for which the alias is specified.
2150234353Sdim  ObjCInterfaceDecl *ClassDecl =
2151234353Sdim    const_cast<ObjCInterfaceDecl *>(OAD->getClassInterface());
2152288943Sdim  ClassAliases.emplace_back(ClassDecl->getNameAsString(),
2153288943Sdim                            OAD->getNameAsString());
2154234353Sdim}
2155234353Sdim
2156193326Sedvoid CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
2157193326Sed  ASTContext &Context = CGM.getContext();
2158193326Sed
2159193326Sed  // Get the superclass name.
2160198092Srdivacky  const ObjCInterfaceDecl * SuperClassDecl =
2161193326Sed    OID->getClassInterface()->getSuperClass();
2162193326Sed  std::string SuperClassName;
2163194613Sed  if (SuperClassDecl) {
2164193326Sed    SuperClassName = SuperClassDecl->getNameAsString();
2165194613Sed    EmitClassRef(SuperClassName);
2166194613Sed  }
2167193326Sed
2168193326Sed  // Get the class name
2169193326Sed  ObjCInterfaceDecl *ClassDecl =
2170193326Sed    const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
2171193326Sed  std::string ClassName = ClassDecl->getNameAsString();
2172194613Sed  // Emit the symbol that is used to generate linker errors if this class is
2173194613Sed  // referenced in other modules but not declared.
2174195341Sed  std::string classSymbolName = "__objc_class_name_" + ClassName;
2175198092Srdivacky  if (llvm::GlobalVariable *symbol =
2176195341Sed      TheModule.getGlobalVariable(classSymbolName)) {
2177195341Sed    symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
2178195341Sed  } else {
2179198092Srdivacky    new llvm::GlobalVariable(TheModule, LongTy, false,
2180198092Srdivacky    llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0),
2181198092Srdivacky    classSymbolName);
2182195341Sed  }
2183198092Srdivacky
2184193326Sed  // Get the size of instances.
2185218893Sdim  int instanceSize =
2186218893Sdim    Context.getASTObjCImplementationLayout(OID).getSize().getQuantity();
2187193326Sed
2188193326Sed  // Collect information about instance variables.
2189226633Sdim  SmallVector<llvm::Constant*, 16> IvarNames;
2190226633Sdim  SmallVector<llvm::Constant*, 16> IvarTypes;
2191226633Sdim  SmallVector<llvm::Constant*, 16> IvarOffsets;
2192198092Srdivacky
2193198092Srdivacky  std::vector<llvm::Constant*> IvarOffsetValues;
2194226633Sdim  SmallVector<bool, 16> WeakIvars;
2195226633Sdim  SmallVector<bool, 16> StrongIvars;
2196198092Srdivacky
2197198092Srdivacky  int superInstanceSize = !SuperClassDecl ? 0 :
2198218893Sdim    Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity();
2199193326Sed  // For non-fragile ivars, set the instance size to 0 - {the size of just this
2200193326Sed  // class}.  The runtime will then set this to the correct value on load.
2201243830Sdim  if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2202193326Sed    instanceSize = 0 - (instanceSize - superInstanceSize);
2203193326Sed  }
2204207619Srdivacky
2205226633Sdim  for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
2206226633Sdim       IVD = IVD->getNextIvar()) {
2207193326Sed      // Store the name
2208207619Srdivacky      IvarNames.push_back(MakeConstantString(IVD->getNameAsString()));
2209193326Sed      // Get the type encoding for this ivar
2210193326Sed      std::string TypeStr;
2211207619Srdivacky      Context.getObjCEncodingForType(IVD->getType(), TypeStr);
2212198092Srdivacky      IvarTypes.push_back(MakeConstantString(TypeStr));
2213193326Sed      // Get the offset
2214207619Srdivacky      uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
2215199482Srdivacky      uint64_t Offset = BaseOffset;
2216243830Sdim      if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2217198092Srdivacky        Offset = BaseOffset - superInstanceSize;
2218193326Sed      }
2219224145Sdim      llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset);
2220224145Sdim      // Create the direct offset value
2221224145Sdim      std::string OffsetName = "__objc_ivar_offset_value_" + ClassName +"." +
2222224145Sdim          IVD->getNameAsString();
2223224145Sdim      llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(OffsetName);
2224224145Sdim      if (OffsetVar) {
2225224145Sdim        OffsetVar->setInitializer(OffsetValue);
2226224145Sdim        // If this is the real definition, change its linkage type so that
2227224145Sdim        // different modules will use this one, rather than their private
2228224145Sdim        // copy.
2229224145Sdim        OffsetVar->setLinkage(llvm::GlobalValue::ExternalLinkage);
2230224145Sdim      } else
2231224145Sdim        OffsetVar = new llvm::GlobalVariable(TheModule, IntTy,
2232198092Srdivacky          false, llvm::GlobalValue::ExternalLinkage,
2233224145Sdim          OffsetValue,
2234198092Srdivacky          "__objc_ivar_offset_value_" + ClassName +"." +
2235224145Sdim          IVD->getNameAsString());
2236224145Sdim      IvarOffsets.push_back(OffsetValue);
2237224145Sdim      IvarOffsetValues.push_back(OffsetVar);
2238226633Sdim      Qualifiers::ObjCLifetime lt = IVD->getType().getQualifiers().getObjCLifetime();
2239226633Sdim      switch (lt) {
2240226633Sdim        case Qualifiers::OCL_Strong:
2241226633Sdim          StrongIvars.push_back(true);
2242226633Sdim          WeakIvars.push_back(false);
2243226633Sdim          break;
2244226633Sdim        case Qualifiers::OCL_Weak:
2245226633Sdim          StrongIvars.push_back(false);
2246226633Sdim          WeakIvars.push_back(true);
2247226633Sdim          break;
2248226633Sdim        default:
2249226633Sdim          StrongIvars.push_back(false);
2250226633Sdim          WeakIvars.push_back(false);
2251226633Sdim      }
2252193326Sed  }
2253226633Sdim  llvm::Constant *StrongIvarBitmap = MakeBitField(StrongIvars);
2254226633Sdim  llvm::Constant *WeakIvarBitmap = MakeBitField(WeakIvars);
2255221345Sdim  llvm::GlobalVariable *IvarOffsetArray =
2256296417Sdim    MakeGlobalArray(PtrToIntTy, IvarOffsetValues, CGM.getPointerAlign(),
2257296417Sdim                    ".ivar.offsets");
2258193326Sed
2259221345Sdim
2260193326Sed  // Collect information about instance methods
2261226633Sdim  SmallVector<Selector, 16> InstanceMethodSels;
2262226633Sdim  SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
2263276479Sdim  for (const auto *I : OID->instance_methods()) {
2264276479Sdim    InstanceMethodSels.push_back(I->getSelector());
2265193326Sed    std::string TypeStr;
2266276479Sdim    Context.getObjCEncodingForMethodDecl(I,TypeStr);
2267198092Srdivacky    InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
2268193326Sed  }
2269193326Sed
2270198092Srdivacky  llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels,
2271198092Srdivacky          InstanceMethodTypes);
2272198092Srdivacky
2273198092Srdivacky
2274193326Sed  // Collect information about class methods
2275226633Sdim  SmallVector<Selector, 16> ClassMethodSels;
2276226633Sdim  SmallVector<llvm::Constant*, 16> ClassMethodTypes;
2277276479Sdim  for (const auto *I : OID->class_methods()) {
2278276479Sdim    ClassMethodSels.push_back(I->getSelector());
2279193326Sed    std::string TypeStr;
2280276479Sdim    Context.getObjCEncodingForMethodDecl(I,TypeStr);
2281198092Srdivacky    ClassMethodTypes.push_back(MakeConstantString(TypeStr));
2282193326Sed  }
2283193326Sed  // Collect the names of referenced protocols
2284226633Sdim  SmallVector<std::string, 16> Protocols;
2285276479Sdim  for (const auto *I : ClassDecl->protocols())
2286276479Sdim    Protocols.push_back(I->getNameAsString());
2287193326Sed
2288193326Sed  // Get the superclass pointer.
2289193326Sed  llvm::Constant *SuperClass;
2290193326Sed  if (!SuperClassName.empty()) {
2291193326Sed    SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
2292193326Sed  } else {
2293193326Sed    SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
2294193326Sed  }
2295193326Sed  // Empty vector used to construct empty method lists
2296226633Sdim  SmallVector<llvm::Constant*, 1>  empty;
2297193326Sed  // Generate the method and instance variable lists
2298193326Sed  llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
2299193326Sed      InstanceMethodSels, InstanceMethodTypes, false);
2300193326Sed  llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
2301193326Sed      ClassMethodSels, ClassMethodTypes, true);
2302193326Sed  llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
2303193326Sed      IvarOffsets);
2304198092Srdivacky  // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
2305198092Srdivacky  // we emit a symbol containing the offset for each ivar in the class.  This
2306198092Srdivacky  // allows code compiled for the non-Fragile ABI to inherit from code compiled
2307198092Srdivacky  // for the legacy ABI, without causing problems.  The converse is also
2308198092Srdivacky  // possible, but causes all ivar accesses to be fragile.
2309218893Sdim
2310198092Srdivacky  // Offset pointer for getting at the correct field in the ivar list when
2311198092Srdivacky  // setting up the alias.  These are: The base address for the global, the
2312198092Srdivacky  // ivar array (second field), the ivar in this list (set for each ivar), and
2313198092Srdivacky  // the offset (third field in ivar structure)
2314226633Sdim  llvm::Type *IndexTy = Int32Ty;
2315198092Srdivacky  llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
2316276479Sdim      llvm::ConstantInt::get(IndexTy, 1), nullptr,
2317198092Srdivacky      llvm::ConstantInt::get(IndexTy, 2) };
2318198092Srdivacky
2319226633Sdim  unsigned ivarIndex = 0;
2320226633Sdim  for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
2321226633Sdim       IVD = IVD->getNextIvar()) {
2322198092Srdivacky      const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
2323218893Sdim          + IVD->getNameAsString();
2324226633Sdim      offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, ivarIndex);
2325198092Srdivacky      // Get the correct ivar field
2326198092Srdivacky      llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
2327288943Sdim          cast<llvm::GlobalVariable>(IvarList)->getValueType(), IvarList,
2328288943Sdim          offsetPointerIndexes);
2329218893Sdim      // Get the existing variable, if one exists.
2330198092Srdivacky      llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
2331198092Srdivacky      if (offset) {
2332234353Sdim        offset->setInitializer(offsetValue);
2333234353Sdim        // If this is the real definition, change its linkage type so that
2334234353Sdim        // different modules will use this one, rather than their private
2335234353Sdim        // copy.
2336234353Sdim        offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
2337198092Srdivacky      } else {
2338234353Sdim        // Add a new alias if there isn't one already.
2339234353Sdim        offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
2340234353Sdim                false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
2341234353Sdim        (void) offset; // Silence dead store warning.
2342198092Srdivacky      }
2343226633Sdim      ++ivarIndex;
2344198092Srdivacky  }
2345228379Sdim  llvm::Constant *ZeroPtr = llvm::ConstantInt::get(IntPtrTy, 0);
2346193326Sed  //Generate metaclass for class methods
2347193326Sed  llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
2348276479Sdim      NULLPtr, 0x12L, ClassName.c_str(), nullptr, Zeros[0], GenerateIvarList(
2349226633Sdim        empty, empty, empty), ClassMethodList, NULLPtr,
2350228379Sdim      NULLPtr, NULLPtr, ZeroPtr, ZeroPtr, true);
2351193326Sed
2352193326Sed  // Generate the class structure
2353193326Sed  llvm::Constant *ClassStruct =
2354198092Srdivacky    GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L,
2355276479Sdim                           ClassName.c_str(), nullptr,
2356193326Sed      llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
2357198092Srdivacky      MethodList, GenerateProtocolList(Protocols), IvarOffsetArray,
2358226633Sdim      Properties, StrongIvarBitmap, WeakIvarBitmap);
2359193326Sed
2360193326Sed  // Resolve the class aliases, if they exist.
2361193326Sed  if (ClassPtrAlias) {
2362218893Sdim    ClassPtrAlias->replaceAllUsesWith(
2363193326Sed        llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
2364218893Sdim    ClassPtrAlias->eraseFromParent();
2365276479Sdim    ClassPtrAlias = nullptr;
2366193326Sed  }
2367193326Sed  if (MetaClassPtrAlias) {
2368218893Sdim    MetaClassPtrAlias->replaceAllUsesWith(
2369193326Sed        llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
2370218893Sdim    MetaClassPtrAlias->eraseFromParent();
2371276479Sdim    MetaClassPtrAlias = nullptr;
2372193326Sed  }
2373193326Sed
2374193326Sed  // Add class structure to list to be added to the symtab later
2375193326Sed  ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
2376193326Sed  Classes.push_back(ClassStruct);
2377193326Sed}
2378193326Sed
2379195099Sed
2380198092Srdivackyllvm::Function *CGObjCGNU::ModuleInitFunction() {
2381193326Sed  // Only emit an ObjC load function if no Objective-C stuff has been called
2382193326Sed  if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
2383221345Sdim      ExistingProtocols.empty() && SelectorTable.empty())
2384276479Sdim    return nullptr;
2385193326Sed
2386198092Srdivacky  // Add all referenced protocols to a category.
2387198092Srdivacky  GenerateProtocolHolderCategory();
2388198092Srdivacky
2389226633Sdim  llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
2390193326Sed          SelectorTy->getElementType());
2391224145Sdim  llvm::Type *SelStructPtrTy = SelectorTy;
2392276479Sdim  if (!SelStructTy) {
2393276479Sdim    SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, nullptr);
2394193326Sed    SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
2395193326Sed  }
2396193326Sed
2397193326Sed  std::vector<llvm::Constant*> Elements;
2398193326Sed  llvm::Constant *Statics = NULLPtr;
2399193326Sed  // Generate statics list:
2400288943Sdim  if (!ConstantStrings.empty()) {
2401193326Sed    llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
2402193326Sed        ConstantStrings.size() + 1);
2403193326Sed    ConstantStrings.push_back(NULLPtr);
2404198092Srdivacky
2405234353Sdim    StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
2406221345Sdim
2407199990Srdivacky    if (StringClass.empty()) StringClass = "NXConstantString";
2408221345Sdim
2409198092Srdivacky    Elements.push_back(MakeConstantString(StringClass,
2410198092Srdivacky                ".objc_static_class_name"));
2411193326Sed    Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
2412193326Sed       ConstantStrings));
2413198092Srdivacky    llvm::StructType *StaticsListTy =
2414276479Sdim      llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, nullptr);
2415198092Srdivacky    llvm::Type *StaticsListPtrTy =
2416198092Srdivacky      llvm::PointerType::getUnqual(StaticsListTy);
2417296417Sdim    Statics = MakeGlobal(StaticsListTy, Elements, CGM.getPointerAlign(),
2418296417Sdim                         ".objc_statics");
2419193326Sed    llvm::ArrayType *StaticsListArrayTy =
2420193326Sed      llvm::ArrayType::get(StaticsListPtrTy, 2);
2421193326Sed    Elements.clear();
2422193326Sed    Elements.push_back(Statics);
2423193326Sed    Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
2424296417Sdim    Statics = MakeGlobal(StaticsListArrayTy, Elements,
2425296417Sdim                         CGM.getPointerAlign(), ".objc_statics_ptr");
2426193326Sed    Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
2427193326Sed  }
2428193326Sed  // Array of classes, categories, and constant objects
2429193326Sed  llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
2430193326Sed      Classes.size() + Categories.size()  + 2);
2431224145Sdim  llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
2432198092Srdivacky                                                     llvm::Type::getInt16Ty(VMContext),
2433198092Srdivacky                                                     llvm::Type::getInt16Ty(VMContext),
2434276479Sdim                                                     ClassListTy, nullptr);
2435193326Sed
2436193326Sed  Elements.clear();
2437193326Sed  // Pointer to an array of selectors used in this module.
2438193326Sed  std::vector<llvm::Constant*> Selectors;
2439221345Sdim  std::vector<llvm::GlobalAlias*> SelectorAliases;
2440221345Sdim  for (SelectorMap::iterator iter = SelectorTable.begin(),
2441221345Sdim      iterEnd = SelectorTable.end(); iter != iterEnd ; ++iter) {
2442221345Sdim
2443221345Sdim    std::string SelNameStr = iter->first.getAsString();
2444221345Sdim    llvm::Constant *SelName = ExportUniqueString(SelNameStr, ".objc_sel_name");
2445221345Sdim
2446226633Sdim    SmallVectorImpl<TypedSelector> &Types = iter->second;
2447226633Sdim    for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
2448221345Sdim        e = Types.end() ; i!=e ; i++) {
2449221345Sdim
2450221345Sdim      llvm::Constant *SelectorTypeEncoding = NULLPtr;
2451221345Sdim      if (!i->first.empty())
2452221345Sdim        SelectorTypeEncoding = MakeConstantString(i->first, ".objc_sel_types");
2453221345Sdim
2454221345Sdim      Elements.push_back(SelName);
2455221345Sdim      Elements.push_back(SelectorTypeEncoding);
2456221345Sdim      Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
2457221345Sdim      Elements.clear();
2458221345Sdim
2459221345Sdim      // Store the selector alias for later replacement
2460221345Sdim      SelectorAliases.push_back(i->second);
2461221345Sdim    }
2462193326Sed  }
2463221345Sdim  unsigned SelectorCount = Selectors.size();
2464221345Sdim  // NULL-terminate the selector list.  This should not actually be required,
2465221345Sdim  // because the selector list has a length field.  Unfortunately, the GCC
2466221345Sdim  // runtime decides to ignore the length field and expects a NULL terminator,
2467221345Sdim  // and GCC cooperates with this by always setting the length to 0.
2468193326Sed  Elements.push_back(NULLPtr);
2469193326Sed  Elements.push_back(NULLPtr);
2470193326Sed  Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
2471193326Sed  Elements.clear();
2472221345Sdim
2473193326Sed  // Number of static selectors
2474221345Sdim  Elements.push_back(llvm::ConstantInt::get(LongTy, SelectorCount));
2475288943Sdim  llvm::GlobalVariable *SelectorList =
2476296417Sdim      MakeGlobalArray(SelStructTy, Selectors, CGM.getPointerAlign(),
2477296417Sdim                      ".objc_selector_list");
2478198092Srdivacky  Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
2479193326Sed    SelStructPtrTy));
2480193326Sed
2481193326Sed  // Now that all of the static selectors exist, create pointers to them.
2482221345Sdim  for (unsigned int i=0 ; i<SelectorCount ; i++) {
2483221345Sdim
2484193326Sed    llvm::Constant *Idxs[] = {Zeros[0],
2485226633Sdim      llvm::ConstantInt::get(Int32Ty, i), Zeros[0]};
2486221345Sdim    // FIXME: We're generating redundant loads and stores here!
2487288943Sdim    llvm::Constant *SelPtr = llvm::ConstantExpr::getGetElementPtr(
2488288943Sdim        SelectorList->getValueType(), SelectorList, makeArrayRef(Idxs, 2));
2489193326Sed    // If selectors are defined as an opaque type, cast the pointer to this
2490193326Sed    // type.
2491221345Sdim    SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy);
2492221345Sdim    SelectorAliases[i]->replaceAllUsesWith(SelPtr);
2493221345Sdim    SelectorAliases[i]->eraseFromParent();
2494193326Sed  }
2495221345Sdim
2496193326Sed  // Number of classes defined.
2497198092Srdivacky  Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
2498193326Sed        Classes.size()));
2499193326Sed  // Number of categories defined
2500198092Srdivacky  Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
2501193326Sed        Categories.size()));
2502193326Sed  // Create an array of classes, then categories, then static object instances
2503193326Sed  Classes.insert(Classes.end(), Categories.begin(), Categories.end());
2504193326Sed  //  NULL-terminated list of static object instances (mainly constant strings)
2505193326Sed  Classes.push_back(Statics);
2506193326Sed  Classes.push_back(NULLPtr);
2507193326Sed  llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
2508193326Sed  Elements.push_back(ClassList);
2509198092Srdivacky  // Construct the symbol table
2510296417Sdim  llvm::Constant *SymTab =
2511296417Sdim    MakeGlobal(SymTabTy, Elements, CGM.getPointerAlign());
2512193326Sed
2513193326Sed  // The symbol table is contained in a module which has some version-checking
2514193326Sed  // constants
2515224145Sdim  llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
2516223017Sdim      PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy),
2517276479Sdim      (RuntimeVersion >= 10) ? IntTy : nullptr, nullptr);
2518193326Sed  Elements.clear();
2519221345Sdim  // Runtime version, used for ABI compatibility checking.
2520221345Sdim  Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
2521193326Sed  // sizeof(ModuleTy)
2522243830Sdim  llvm::DataLayout td(&TheModule);
2523221345Sdim  Elements.push_back(
2524221345Sdim    llvm::ConstantInt::get(LongTy,
2525221345Sdim                           td.getTypeSizeInBits(ModuleTy) /
2526221345Sdim                             CGM.getContext().getCharWidth()));
2527221345Sdim
2528221345Sdim  // The path to the source file where this module was declared
2529221345Sdim  SourceManager &SM = CGM.getContext().getSourceManager();
2530221345Sdim  const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID());
2531221345Sdim  std::string path =
2532221345Sdim    std::string(mainFile->getDir()->getName()) + '/' + mainFile->getName();
2533221345Sdim  Elements.push_back(MakeConstantString(path, ".objc_source_file_name"));
2534223017Sdim  Elements.push_back(SymTab);
2535221345Sdim
2536224145Sdim  if (RuntimeVersion >= 10)
2537234353Sdim    switch (CGM.getLangOpts().getGC()) {
2538224145Sdim      case LangOptions::GCOnly:
2539223017Sdim        Elements.push_back(llvm::ConstantInt::get(IntTy, 2));
2540223017Sdim        break;
2541224145Sdim      case LangOptions::NonGC:
2542234353Sdim        if (CGM.getLangOpts().ObjCAutoRefCount)
2543224145Sdim          Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2544224145Sdim        else
2545224145Sdim          Elements.push_back(llvm::ConstantInt::get(IntTy, 0));
2546224145Sdim        break;
2547224145Sdim      case LangOptions::HybridGC:
2548224145Sdim          Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2549224145Sdim        break;
2550224145Sdim    }
2551223017Sdim
2552296417Sdim  llvm::Value *Module = MakeGlobal(ModuleTy, Elements, CGM.getPointerAlign());
2553193326Sed
2554193326Sed  // Create the load function calling the runtime entry point with the module
2555193326Sed  // structure
2556193326Sed  llvm::Function * LoadFunction = llvm::Function::Create(
2557198092Srdivacky      llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
2558193326Sed      llvm::GlobalValue::InternalLinkage, ".objc_load_function",
2559193326Sed      &TheModule);
2560198092Srdivacky  llvm::BasicBlock *EntryBB =
2561198092Srdivacky      llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
2562296417Sdim  CGBuilderTy Builder(CGM, VMContext);
2563193326Sed  Builder.SetInsertPoint(EntryBB);
2564193326Sed
2565223017Sdim  llvm::FunctionType *FT =
2566226633Sdim    llvm::FunctionType::get(Builder.getVoidTy(),
2567226633Sdim                            llvm::PointerType::getUnqual(ModuleTy), true);
2568223017Sdim  llvm::Value *Register = CGM.CreateRuntimeFunction(FT, "__objc_exec_class");
2569193326Sed  Builder.CreateCall(Register, Module);
2570234353Sdim
2571234353Sdim  if (!ClassAliases.empty()) {
2572234353Sdim    llvm::Type *ArgTypes[2] = {PtrTy, PtrToInt8Ty};
2573234353Sdim    llvm::FunctionType *RegisterAliasTy =
2574234353Sdim      llvm::FunctionType::get(Builder.getVoidTy(),
2575234353Sdim                              ArgTypes, false);
2576234353Sdim    llvm::Function *RegisterAlias = llvm::Function::Create(
2577234353Sdim      RegisterAliasTy,
2578234353Sdim      llvm::GlobalValue::ExternalWeakLinkage, "class_registerAlias_np",
2579234353Sdim      &TheModule);
2580234353Sdim    llvm::BasicBlock *AliasBB =
2581234353Sdim      llvm::BasicBlock::Create(VMContext, "alias", LoadFunction);
2582234353Sdim    llvm::BasicBlock *NoAliasBB =
2583234353Sdim      llvm::BasicBlock::Create(VMContext, "no_alias", LoadFunction);
2584234353Sdim
2585234353Sdim    // Branch based on whether the runtime provided class_registerAlias_np()
2586234353Sdim    llvm::Value *HasRegisterAlias = Builder.CreateICmpNE(RegisterAlias,
2587234353Sdim            llvm::Constant::getNullValue(RegisterAlias->getType()));
2588234353Sdim    Builder.CreateCondBr(HasRegisterAlias, AliasBB, NoAliasBB);
2589234353Sdim
2590276479Sdim    // The true branch (has alias registration function):
2591234353Sdim    Builder.SetInsertPoint(AliasBB);
2592234353Sdim    // Emit alias registration calls:
2593234353Sdim    for (std::vector<ClassAliasPair>::iterator iter = ClassAliases.begin();
2594234353Sdim       iter != ClassAliases.end(); ++iter) {
2595234353Sdim       llvm::Constant *TheClass =
2596234353Sdim         TheModule.getGlobalVariable(("_OBJC_CLASS_" + iter->first).c_str(),
2597234353Sdim            true);
2598276479Sdim       if (TheClass) {
2599234353Sdim         TheClass = llvm::ConstantExpr::getBitCast(TheClass, PtrTy);
2600288943Sdim         Builder.CreateCall(RegisterAlias,
2601288943Sdim                            {TheClass, MakeConstantString(iter->second)});
2602234353Sdim       }
2603234353Sdim    }
2604234353Sdim    // Jump to end:
2605234353Sdim    Builder.CreateBr(NoAliasBB);
2606234353Sdim
2607234353Sdim    // Missing alias registration function, just return from the function:
2608234353Sdim    Builder.SetInsertPoint(NoAliasBB);
2609234353Sdim  }
2610193326Sed  Builder.CreateRetVoid();
2611193326Sed
2612193326Sed  return LoadFunction;
2613193326Sed}
2614193326Sed
2615193326Sedllvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
2616198092Srdivacky                                          const ObjCContainerDecl *CD) {
2617198092Srdivacky  const ObjCCategoryImplDecl *OCD =
2618193326Sed    dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
2619226633Sdim  StringRef CategoryName = OCD ? OCD->getName() : "";
2620226633Sdim  StringRef ClassName = CD->getName();
2621221345Sdim  Selector MethodName = OMD->getSelector();
2622193326Sed  bool isClassMethod = !OMD->isInstanceMethod();
2623193326Sed
2624193326Sed  CodeGenTypes &Types = CGM.getTypes();
2625226633Sdim  llvm::FunctionType *MethodTy =
2626234353Sdim    Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
2627193326Sed  std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
2628193326Sed      MethodName, isClassMethod);
2629193326Sed
2630198092Srdivacky  llvm::Function *Method
2631198092Srdivacky    = llvm::Function::Create(MethodTy,
2632198092Srdivacky                             llvm::GlobalValue::InternalLinkage,
2633198092Srdivacky                             FunctionName,
2634198092Srdivacky                             &TheModule);
2635193326Sed  return Method;
2636193326Sed}
2637193326Sed
2638223017Sdimllvm::Constant *CGObjCGNU::GetPropertyGetFunction() {
2639221345Sdim  return GetPropertyFn;
2640193326Sed}
2641193326Sed
2642223017Sdimllvm::Constant *CGObjCGNU::GetPropertySetFunction() {
2643221345Sdim  return SetPropertyFn;
2644193326Sed}
2645193326Sed
2646234353Sdimllvm::Constant *CGObjCGNU::GetOptimizedPropertySetFunction(bool atomic,
2647234353Sdim                                                           bool copy) {
2648276479Sdim  return nullptr;
2649234353Sdim}
2650234353Sdim
2651223017Sdimllvm::Constant *CGObjCGNU::GetGetStructFunction() {
2652221345Sdim  return GetStructPropertyFn;
2653207619Srdivacky}
2654223017Sdimllvm::Constant *CGObjCGNU::GetSetStructFunction() {
2655221345Sdim  return SetStructPropertyFn;
2656218893Sdim}
2657249423Sdimllvm::Constant *CGObjCGNU::GetCppAtomicObjectGetFunction() {
2658276479Sdim  return nullptr;
2659234353Sdim}
2660249423Sdimllvm::Constant *CGObjCGNU::GetCppAtomicObjectSetFunction() {
2661276479Sdim  return nullptr;
2662249423Sdim}
2663207619Srdivacky
2664198092Srdivackyllvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
2665221345Sdim  return EnumerationMutationFn;
2666193326Sed}
2667193326Sed
2668221345Sdimvoid CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF,
2669210299Sed                                     const ObjCAtSynchronizedStmt &S) {
2670221345Sdim  EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn);
2671210299Sed}
2672193326Sed
2673193326Sed
2674221345Sdimvoid CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF,
2675210299Sed                            const ObjCAtTryStmt &S) {
2676210299Sed  // Unlike the Apple non-fragile runtimes, which also uses
2677210299Sed  // unwind-based zero cost exceptions, the GNU Objective C runtime's
2678210299Sed  // EH support isn't a veneer over C++ EH.  Instead, exception
2679243830Sdim  // objects are created by objc_exception_throw and destroyed by
2680210299Sed  // the personality function; this avoids the need for bracketing
2681210299Sed  // catch handlers with calls to __blah_begin_catch/__blah_end_catch
2682210299Sed  // (or even _Unwind_DeleteException), but probably doesn't
2683210299Sed  // interoperate very well with foreign exceptions.
2684221345Sdim  //
2685221345Sdim  // In Objective-C++ mode, we actually emit something equivalent to the C++
2686221345Sdim  // exception handler.
2687221345Sdim  EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn);
2688221345Sdim  return ;
2689193326Sed}
2690193326Sed
2691221345Sdimvoid CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
2692249423Sdim                              const ObjCAtThrowStmt &S,
2693249423Sdim                              bool ClearInsertionPoint) {
2694193326Sed  llvm::Value *ExceptionAsObject;
2695193326Sed
2696193326Sed  if (const Expr *ThrowExpr = S.getThrowExpr()) {
2697226633Sdim    llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
2698193326Sed    ExceptionAsObject = Exception;
2699193326Sed  } else {
2700198092Srdivacky    assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
2701193326Sed           "Unexpected rethrow outside @catch block.");
2702193326Sed    ExceptionAsObject = CGF.ObjCEHValueStack.back();
2703193326Sed  }
2704226633Sdim  ExceptionAsObject = CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy);
2705243830Sdim  llvm::CallSite Throw =
2706249423Sdim      CGF.EmitRuntimeCallOrInvoke(ExceptionThrowFn, ExceptionAsObject);
2707243830Sdim  Throw.setDoesNotReturn();
2708239462Sdim  CGF.Builder.CreateUnreachable();
2709249423Sdim  if (ClearInsertionPoint)
2710249423Sdim    CGF.Builder.ClearInsertionPoint();
2711193326Sed}
2712193326Sed
2713221345Sdimllvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF,
2714296417Sdim                                          Address AddrWeakObj) {
2715249423Sdim  CGBuilderTy &B = CGF.Builder;
2716223017Sdim  AddrWeakObj = EnforceType(B, AddrWeakObj, PtrToIdTy);
2717296417Sdim  return B.CreateCall(WeakReadFn.getType(), WeakReadFn,
2718296417Sdim                      AddrWeakObj.getPointer());
2719193326Sed}
2720193326Sed
2721221345Sdimvoid CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF,
2722296417Sdim                                   llvm::Value *src, Address dst) {
2723249423Sdim  CGBuilderTy &B = CGF.Builder;
2724203955Srdivacky  src = EnforceType(B, src, IdTy);
2725203955Srdivacky  dst = EnforceType(B, dst, PtrToIdTy);
2726296417Sdim  B.CreateCall(WeakAssignFn.getType(), WeakAssignFn,
2727296417Sdim               {src, dst.getPointer()});
2728193326Sed}
2729193326Sed
2730221345Sdimvoid CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF,
2731296417Sdim                                     llvm::Value *src, Address dst,
2732212904Sdim                                     bool threadlocal) {
2733249423Sdim  CGBuilderTy &B = CGF.Builder;
2734203955Srdivacky  src = EnforceType(B, src, IdTy);
2735203955Srdivacky  dst = EnforceType(B, dst, PtrToIdTy);
2736288943Sdim  // FIXME. Add threadloca assign API
2737288943Sdim  assert(!threadlocal && "EmitObjCGlobalAssign - Threal Local API NYI");
2738296417Sdim  B.CreateCall(GlobalAssignFn.getType(), GlobalAssignFn,
2739296417Sdim               {src, dst.getPointer()});
2740193326Sed}
2741193326Sed
2742221345Sdimvoid CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF,
2743296417Sdim                                   llvm::Value *src, Address dst,
2744198092Srdivacky                                   llvm::Value *ivarOffset) {
2745249423Sdim  CGBuilderTy &B = CGF.Builder;
2746203955Srdivacky  src = EnforceType(B, src, IdTy);
2747223017Sdim  dst = EnforceType(B, dst, IdTy);
2748296417Sdim  B.CreateCall(IvarAssignFn.getType(), IvarAssignFn,
2749296417Sdim               {src, dst.getPointer(), ivarOffset});
2750193326Sed}
2751193326Sed
2752221345Sdimvoid CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF,
2753296417Sdim                                         llvm::Value *src, Address dst) {
2754249423Sdim  CGBuilderTy &B = CGF.Builder;
2755203955Srdivacky  src = EnforceType(B, src, IdTy);
2756203955Srdivacky  dst = EnforceType(B, dst, PtrToIdTy);
2757296417Sdim  B.CreateCall(StrongCastAssignFn.getType(), StrongCastAssignFn,
2758296417Sdim               {src, dst.getPointer()});
2759193326Sed}
2760193326Sed
2761221345Sdimvoid CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF,
2762296417Sdim                                         Address DestPtr,
2763296417Sdim                                         Address SrcPtr,
2764210299Sed                                         llvm::Value *Size) {
2765249423Sdim  CGBuilderTy &B = CGF.Builder;
2766223017Sdim  DestPtr = EnforceType(B, DestPtr, PtrTy);
2767223017Sdim  SrcPtr = EnforceType(B, SrcPtr, PtrTy);
2768203955Srdivacky
2769296417Sdim  B.CreateCall(MemMoveFn.getType(), MemMoveFn,
2770296417Sdim               {DestPtr.getPointer(), SrcPtr.getPointer(), Size});
2771198092Srdivacky}
2772198092Srdivacky
2773193326Sedllvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
2774193326Sed                              const ObjCInterfaceDecl *ID,
2775193326Sed                              const ObjCIvarDecl *Ivar) {
2776193326Sed  const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
2777193326Sed    + '.' + Ivar->getNameAsString();
2778193326Sed  // Emit the variable and initialize it with what we think the correct value
2779193326Sed  // is.  This allows code compiled with non-fragile ivars to work correctly
2780193326Sed  // when linked against code which isn't (most of the time).
2781198092Srdivacky  llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
2782198092Srdivacky  if (!IvarOffsetPointer) {
2783218893Sdim    // This will cause a run-time crash if we accidentally use it.  A value of
2784218893Sdim    // 0 would seem more sensible, but will silently overwrite the isa pointer
2785218893Sdim    // causing a great deal of confusion.
2786218893Sdim    uint64_t Offset = -1;
2787218893Sdim    // We can't call ComputeIvarBaseOffset() here if we have the
2788218893Sdim    // implementation, because it will create an invalid ASTRecordLayout object
2789218893Sdim    // that we are then stuck with forever, so we only initialize the ivar
2790218893Sdim    // offset variable with a guess if we only have the interface.  The
2791218893Sdim    // initializer will be reset later anyway, when we are generating the class
2792218893Sdim    // description.
2793218893Sdim    if (!CGM.getContext().getObjCImplementation(
2794207619Srdivacky              const_cast<ObjCInterfaceDecl *>(ID)))
2795207619Srdivacky      Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
2796207619Srdivacky
2797226633Sdim    llvm::ConstantInt *OffsetGuess = llvm::ConstantInt::get(Int32Ty, Offset,
2798226633Sdim                             /*isSigned*/true);
2799198092Srdivacky    // Don't emit the guess in non-PIC code because the linker will not be able
2800198092Srdivacky    // to replace it with the real version for a library.  In non-PIC code you
2801198092Srdivacky    // must compile with the fragile ABI if you want to use ivars from a
2802198092Srdivacky    // GCC-compiled class.
2803234353Sdim    if (CGM.getLangOpts().PICLevel || CGM.getLangOpts().PIELevel) {
2804198092Srdivacky      llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
2805226633Sdim            Int32Ty, false,
2806198092Srdivacky            llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
2807198092Srdivacky      IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
2808198092Srdivacky            IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
2809198092Srdivacky            IvarOffsetGV, Name);
2810198092Srdivacky    } else {
2811198092Srdivacky      IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
2812198092Srdivacky              llvm::Type::getInt32PtrTy(VMContext), false,
2813276479Sdim              llvm::GlobalValue::ExternalLinkage, nullptr, Name);
2814198092Srdivacky    }
2815193326Sed  }
2816198092Srdivacky  return IvarOffsetPointer;
2817193326Sed}
2818193326Sed
2819221345SdimLValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF,
2820193326Sed                                       QualType ObjectTy,
2821193326Sed                                       llvm::Value *BaseValue,
2822193326Sed                                       const ObjCIvarDecl *Ivar,
2823193326Sed                                       unsigned CVRQualifiers) {
2824208600Srdivacky  const ObjCInterfaceDecl *ID =
2825208600Srdivacky    ObjectTy->getAs<ObjCObjectType>()->getInterface();
2826193326Sed  return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2827193326Sed                                  EmitIvarOffset(CGF, ID, Ivar));
2828193326Sed}
2829198092Srdivacky
2830193326Sedstatic const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
2831193326Sed                                                  const ObjCInterfaceDecl *OID,
2832193326Sed                                                  const ObjCIvarDecl *OIVD) {
2833226633Sdim  for (const ObjCIvarDecl *next = OID->all_declared_ivar_begin(); next;
2834226633Sdim       next = next->getNextIvar()) {
2835226633Sdim    if (OIVD == next)
2836193326Sed      return OID;
2837193326Sed  }
2838198092Srdivacky
2839193326Sed  // Otherwise check in the super class.
2840193326Sed  if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
2841193326Sed    return FindIvarInterface(Context, Super, OIVD);
2842198092Srdivacky
2843276479Sdim  return nullptr;
2844193326Sed}
2845193326Sed
2846221345Sdimllvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF,
2847193326Sed                         const ObjCInterfaceDecl *Interface,
2848193326Sed                         const ObjCIvarDecl *Ivar) {
2849239462Sdim  if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2850193326Sed    Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
2851224145Sdim    if (RuntimeVersion < 10)
2852224145Sdim      return CGF.Builder.CreateZExtOrBitCast(
2853296417Sdim          CGF.Builder.CreateDefaultAlignedLoad(CGF.Builder.CreateAlignedLoad(
2854296417Sdim                  ObjCIvarOffsetVariable(Interface, Ivar),
2855296417Sdim                  CGF.getPointerAlign(), "ivar")),
2856224145Sdim          PtrDiffTy);
2857224145Sdim    std::string name = "__objc_ivar_offset_value_" +
2858224145Sdim      Interface->getNameAsString() +"." + Ivar->getNameAsString();
2859296417Sdim    CharUnits Align = CGM.getIntAlign();
2860224145Sdim    llvm::Value *Offset = TheModule.getGlobalVariable(name);
2861296417Sdim    if (!Offset) {
2862296417Sdim      auto GV = new llvm::GlobalVariable(TheModule, IntTy,
2863226633Sdim          false, llvm::GlobalValue::LinkOnceAnyLinkage,
2864226633Sdim          llvm::Constant::getNullValue(IntTy), name);
2865296417Sdim      GV->setAlignment(Align.getQuantity());
2866296417Sdim      Offset = GV;
2867296417Sdim    }
2868296417Sdim    Offset = CGF.Builder.CreateAlignedLoad(Offset, Align);
2869234353Sdim    if (Offset->getType() != PtrDiffTy)
2870234353Sdim      Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy);
2871234353Sdim    return Offset;
2872193326Sed  }
2873193326Sed  uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
2874226633Sdim  return llvm::ConstantInt::get(PtrDiffTy, Offset, /*isSigned*/true);
2875193326Sed}
2876193326Sed
2877221345SdimCGObjCRuntime *
2878221345Sdimclang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) {
2879239462Sdim  switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
2880239462Sdim  case ObjCRuntime::GNUstep:
2881221345Sdim    return new CGObjCGNUstep(CGM);
2882239462Sdim
2883239462Sdim  case ObjCRuntime::GCC:
2884239462Sdim    return new CGObjCGCC(CGM);
2885239462Sdim
2886239462Sdim  case ObjCRuntime::ObjFW:
2887239462Sdim    return new CGObjCObjFW(CGM);
2888239462Sdim
2889239462Sdim  case ObjCRuntime::FragileMacOSX:
2890239462Sdim  case ObjCRuntime::MacOSX:
2891239462Sdim  case ObjCRuntime::iOS:
2892296417Sdim  case ObjCRuntime::WatchOS:
2893239462Sdim    llvm_unreachable("these runtimes are not GNU runtimes");
2894239462Sdim  }
2895239462Sdim  llvm_unreachable("bad runtime");
2896193326Sed}
2897