xref: /minix3/external/bsd/llvm/dist/clang/lib/CodeGen/CGObjCGNU.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This provides Objective-C code generation targeting the GNU runtime.  The
11f4a2713aSLionel Sambuc // class in this file generates structures used by the GNU Objective-C runtime
12f4a2713aSLionel Sambuc // library.  These structures are defined in objc/objc.h and objc/objc-api.h in
13f4a2713aSLionel Sambuc // the GNU runtime distribution.
14f4a2713aSLionel Sambuc //
15f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc #include "CGObjCRuntime.h"
18f4a2713aSLionel Sambuc #include "CGCleanup.h"
19f4a2713aSLionel Sambuc #include "CodeGenFunction.h"
20f4a2713aSLionel Sambuc #include "CodeGenModule.h"
21f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
22f4a2713aSLionel Sambuc #include "clang/AST/Decl.h"
23f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
24f4a2713aSLionel Sambuc #include "clang/AST/RecordLayout.h"
25f4a2713aSLionel Sambuc #include "clang/AST/StmtObjC.h"
26f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
27f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
28f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
29f4a2713aSLionel Sambuc #include "llvm/ADT/StringMap.h"
30*0a6a1f1dSLionel Sambuc #include "llvm/IR/CallSite.h"
31f4a2713aSLionel Sambuc #include "llvm/IR/DataLayout.h"
32f4a2713aSLionel Sambuc #include "llvm/IR/Intrinsics.h"
33f4a2713aSLionel Sambuc #include "llvm/IR/LLVMContext.h"
34f4a2713aSLionel Sambuc #include "llvm/IR/Module.h"
35f4a2713aSLionel Sambuc #include "llvm/Support/Compiler.h"
36f4a2713aSLionel Sambuc #include <cstdarg>
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc 
39f4a2713aSLionel Sambuc using namespace clang;
40f4a2713aSLionel Sambuc using namespace CodeGen;
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc 
43f4a2713aSLionel Sambuc namespace {
44f4a2713aSLionel Sambuc /// Class that lazily initialises the runtime function.  Avoids inserting the
45f4a2713aSLionel Sambuc /// types and the function declaration into a module if they're not used, and
46f4a2713aSLionel Sambuc /// avoids constructing the type more than once if it's used more than once.
47f4a2713aSLionel Sambuc class LazyRuntimeFunction {
48f4a2713aSLionel Sambuc   CodeGenModule *CGM;
49f4a2713aSLionel Sambuc   std::vector<llvm::Type*> ArgTys;
50f4a2713aSLionel Sambuc   const char *FunctionName;
51f4a2713aSLionel Sambuc   llvm::Constant *Function;
52f4a2713aSLionel Sambuc   public:
53f4a2713aSLionel Sambuc     /// Constructor leaves this class uninitialized, because it is intended to
54f4a2713aSLionel Sambuc     /// be used as a field in another class and not all of the types that are
55f4a2713aSLionel Sambuc     /// used as arguments will necessarily be available at construction time.
LazyRuntimeFunction()56*0a6a1f1dSLionel Sambuc     LazyRuntimeFunction()
57*0a6a1f1dSLionel Sambuc       : CGM(nullptr), FunctionName(nullptr), Function(nullptr) {}
58f4a2713aSLionel Sambuc 
59f4a2713aSLionel Sambuc     /// Initialises the lazy function with the name, return type, and the types
60f4a2713aSLionel Sambuc     /// of the arguments.
61*0a6a1f1dSLionel Sambuc     LLVM_END_WITH_NULL
init(CodeGenModule * Mod,const char * name,llvm::Type * RetTy,...)62f4a2713aSLionel Sambuc     void init(CodeGenModule *Mod, const char *name,
63f4a2713aSLionel Sambuc         llvm::Type *RetTy, ...) {
64f4a2713aSLionel Sambuc        CGM =Mod;
65f4a2713aSLionel Sambuc        FunctionName = name;
66*0a6a1f1dSLionel Sambuc        Function = nullptr;
67f4a2713aSLionel Sambuc        ArgTys.clear();
68f4a2713aSLionel Sambuc        va_list Args;
69f4a2713aSLionel Sambuc        va_start(Args, RetTy);
70f4a2713aSLionel Sambuc          while (llvm::Type *ArgTy = va_arg(Args, llvm::Type*))
71f4a2713aSLionel Sambuc            ArgTys.push_back(ArgTy);
72f4a2713aSLionel Sambuc        va_end(Args);
73f4a2713aSLionel Sambuc        // Push the return type on at the end so we can pop it off easily
74f4a2713aSLionel Sambuc        ArgTys.push_back(RetTy);
75f4a2713aSLionel Sambuc    }
76f4a2713aSLionel Sambuc    /// Overloaded cast operator, allows the class to be implicitly cast to an
77f4a2713aSLionel Sambuc    /// LLVM constant.
operator llvm::Constant*()78f4a2713aSLionel Sambuc    operator llvm::Constant*() {
79f4a2713aSLionel Sambuc      if (!Function) {
80*0a6a1f1dSLionel Sambuc        if (!FunctionName) return nullptr;
81f4a2713aSLionel Sambuc        // We put the return type on the end of the vector, so pop it back off
82f4a2713aSLionel Sambuc        llvm::Type *RetTy = ArgTys.back();
83f4a2713aSLionel Sambuc        ArgTys.pop_back();
84f4a2713aSLionel Sambuc        llvm::FunctionType *FTy = llvm::FunctionType::get(RetTy, ArgTys, false);
85f4a2713aSLionel Sambuc        Function =
86f4a2713aSLionel Sambuc          cast<llvm::Constant>(CGM->CreateRuntimeFunction(FTy, FunctionName));
87f4a2713aSLionel Sambuc        // We won't need to use the types again, so we may as well clean up the
88f4a2713aSLionel Sambuc        // vector now
89f4a2713aSLionel Sambuc        ArgTys.resize(0);
90f4a2713aSLionel Sambuc      }
91f4a2713aSLionel Sambuc      return Function;
92f4a2713aSLionel Sambuc    }
operator llvm::Function*()93f4a2713aSLionel Sambuc    operator llvm::Function*() {
94f4a2713aSLionel Sambuc      return cast<llvm::Function>((llvm::Constant*)*this);
95f4a2713aSLionel Sambuc    }
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc };
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc /// GNU Objective-C runtime code generation.  This class implements the parts of
101f4a2713aSLionel Sambuc /// Objective-C support that are specific to the GNU family of runtimes (GCC,
102f4a2713aSLionel Sambuc /// GNUstep and ObjFW).
103f4a2713aSLionel Sambuc class CGObjCGNU : public CGObjCRuntime {
104f4a2713aSLionel Sambuc protected:
105f4a2713aSLionel Sambuc   /// The LLVM module into which output is inserted
106f4a2713aSLionel Sambuc   llvm::Module &TheModule;
107f4a2713aSLionel Sambuc   /// strut objc_super.  Used for sending messages to super.  This structure
108f4a2713aSLionel Sambuc   /// contains the receiver (object) and the expected class.
109f4a2713aSLionel Sambuc   llvm::StructType *ObjCSuperTy;
110f4a2713aSLionel Sambuc   /// struct objc_super*.  The type of the argument to the superclass message
111f4a2713aSLionel Sambuc   /// lookup functions.
112f4a2713aSLionel Sambuc   llvm::PointerType *PtrToObjCSuperTy;
113f4a2713aSLionel Sambuc   /// LLVM type for selectors.  Opaque pointer (i8*) unless a header declaring
114f4a2713aSLionel Sambuc   /// SEL is included in a header somewhere, in which case it will be whatever
115f4a2713aSLionel Sambuc   /// type is declared in that header, most likely {i8*, i8*}.
116f4a2713aSLionel Sambuc   llvm::PointerType *SelectorTy;
117f4a2713aSLionel Sambuc   /// LLVM i8 type.  Cached here to avoid repeatedly getting it in all of the
118f4a2713aSLionel Sambuc   /// places where it's used
119f4a2713aSLionel Sambuc   llvm::IntegerType *Int8Ty;
120f4a2713aSLionel Sambuc   /// Pointer to i8 - LLVM type of char*, for all of the places where the
121f4a2713aSLionel Sambuc   /// runtime needs to deal with C strings.
122f4a2713aSLionel Sambuc   llvm::PointerType *PtrToInt8Ty;
123f4a2713aSLionel Sambuc   /// Instance Method Pointer type.  This is a pointer to a function that takes,
124f4a2713aSLionel Sambuc   /// at a minimum, an object and a selector, and is the generic type for
125f4a2713aSLionel Sambuc   /// Objective-C methods.  Due to differences between variadic / non-variadic
126f4a2713aSLionel Sambuc   /// calling conventions, it must always be cast to the correct type before
127f4a2713aSLionel Sambuc   /// actually being used.
128f4a2713aSLionel Sambuc   llvm::PointerType *IMPTy;
129f4a2713aSLionel Sambuc   /// Type of an untyped Objective-C object.  Clang treats id as a built-in type
130f4a2713aSLionel Sambuc   /// when compiling Objective-C code, so this may be an opaque pointer (i8*),
131f4a2713aSLionel Sambuc   /// but if the runtime header declaring it is included then it may be a
132f4a2713aSLionel Sambuc   /// pointer to a structure.
133f4a2713aSLionel Sambuc   llvm::PointerType *IdTy;
134f4a2713aSLionel Sambuc   /// Pointer to a pointer to an Objective-C object.  Used in the new ABI
135f4a2713aSLionel Sambuc   /// message lookup function and some GC-related functions.
136f4a2713aSLionel Sambuc   llvm::PointerType *PtrToIdTy;
137f4a2713aSLionel Sambuc   /// The clang type of id.  Used when using the clang CGCall infrastructure to
138f4a2713aSLionel Sambuc   /// call Objective-C methods.
139f4a2713aSLionel Sambuc   CanQualType ASTIdTy;
140f4a2713aSLionel Sambuc   /// LLVM type for C int type.
141f4a2713aSLionel Sambuc   llvm::IntegerType *IntTy;
142f4a2713aSLionel Sambuc   /// LLVM type for an opaque pointer.  This is identical to PtrToInt8Ty, but is
143f4a2713aSLionel Sambuc   /// used in the code to document the difference between i8* meaning a pointer
144f4a2713aSLionel Sambuc   /// to a C string and i8* meaning a pointer to some opaque type.
145f4a2713aSLionel Sambuc   llvm::PointerType *PtrTy;
146f4a2713aSLionel Sambuc   /// LLVM type for C long type.  The runtime uses this in a lot of places where
147f4a2713aSLionel Sambuc   /// it should be using intptr_t, but we can't fix this without breaking
148f4a2713aSLionel Sambuc   /// compatibility with GCC...
149f4a2713aSLionel Sambuc   llvm::IntegerType *LongTy;
150f4a2713aSLionel Sambuc   /// LLVM type for C size_t.  Used in various runtime data structures.
151f4a2713aSLionel Sambuc   llvm::IntegerType *SizeTy;
152f4a2713aSLionel Sambuc   /// LLVM type for C intptr_t.
153f4a2713aSLionel Sambuc   llvm::IntegerType *IntPtrTy;
154f4a2713aSLionel Sambuc   /// LLVM type for C ptrdiff_t.  Mainly used in property accessor functions.
155f4a2713aSLionel Sambuc   llvm::IntegerType *PtrDiffTy;
156f4a2713aSLionel Sambuc   /// LLVM type for C int*.  Used for GCC-ABI-compatible non-fragile instance
157f4a2713aSLionel Sambuc   /// variables.
158f4a2713aSLionel Sambuc   llvm::PointerType *PtrToIntTy;
159f4a2713aSLionel Sambuc   /// LLVM type for Objective-C BOOL type.
160f4a2713aSLionel Sambuc   llvm::Type *BoolTy;
161f4a2713aSLionel Sambuc   /// 32-bit integer type, to save us needing to look it up every time it's used.
162f4a2713aSLionel Sambuc   llvm::IntegerType *Int32Ty;
163f4a2713aSLionel Sambuc   /// 64-bit integer type, to save us needing to look it up every time it's used.
164f4a2713aSLionel Sambuc   llvm::IntegerType *Int64Ty;
165f4a2713aSLionel Sambuc   /// Metadata kind used to tie method lookups to message sends.  The GNUstep
166f4a2713aSLionel Sambuc   /// runtime provides some LLVM passes that can use this to do things like
167f4a2713aSLionel Sambuc   /// automatic IMP caching and speculative inlining.
168f4a2713aSLionel Sambuc   unsigned msgSendMDKind;
169f4a2713aSLionel Sambuc   /// Helper function that generates a constant string and returns a pointer to
170f4a2713aSLionel Sambuc   /// the start of the string.  The result of this function can be used anywhere
171f4a2713aSLionel Sambuc   /// where the C code specifies const char*.
MakeConstantString(const std::string & Str,const std::string & Name="")172f4a2713aSLionel Sambuc   llvm::Constant *MakeConstantString(const std::string &Str,
173f4a2713aSLionel Sambuc                                      const std::string &Name="") {
174f4a2713aSLionel Sambuc     llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
175f4a2713aSLionel Sambuc     return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros);
176f4a2713aSLionel Sambuc   }
177f4a2713aSLionel Sambuc   /// Emits a linkonce_odr string, whose name is the prefix followed by the
178f4a2713aSLionel Sambuc   /// string value.  This allows the linker to combine the strings between
179f4a2713aSLionel Sambuc   /// different modules.  Used for EH typeinfo names, selector strings, and a
180f4a2713aSLionel Sambuc   /// few other things.
ExportUniqueString(const std::string & Str,const std::string prefix)181f4a2713aSLionel Sambuc   llvm::Constant *ExportUniqueString(const std::string &Str,
182f4a2713aSLionel Sambuc                                      const std::string prefix) {
183f4a2713aSLionel Sambuc     std::string name = prefix + Str;
184f4a2713aSLionel Sambuc     llvm::Constant *ConstStr = TheModule.getGlobalVariable(name);
185f4a2713aSLionel Sambuc     if (!ConstStr) {
186f4a2713aSLionel Sambuc       llvm::Constant *value = llvm::ConstantDataArray::getString(VMContext,Str);
187f4a2713aSLionel Sambuc       ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true,
188f4a2713aSLionel Sambuc               llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str);
189f4a2713aSLionel Sambuc     }
190f4a2713aSLionel Sambuc     return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros);
191f4a2713aSLionel Sambuc   }
192f4a2713aSLionel Sambuc   /// Generates a global structure, initialized by the elements in the vector.
193f4a2713aSLionel Sambuc   /// The element types must match the types of the structure elements in the
194f4a2713aSLionel Sambuc   /// first argument.
MakeGlobal(llvm::StructType * Ty,ArrayRef<llvm::Constant * > V,StringRef Name="",llvm::GlobalValue::LinkageTypes linkage=llvm::GlobalValue::InternalLinkage)195f4a2713aSLionel Sambuc   llvm::GlobalVariable *MakeGlobal(llvm::StructType *Ty,
196f4a2713aSLionel Sambuc                                    ArrayRef<llvm::Constant *> V,
197f4a2713aSLionel Sambuc                                    StringRef Name="",
198f4a2713aSLionel Sambuc                                    llvm::GlobalValue::LinkageTypes linkage
199f4a2713aSLionel Sambuc                                          =llvm::GlobalValue::InternalLinkage) {
200f4a2713aSLionel Sambuc     llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
201f4a2713aSLionel Sambuc     return new llvm::GlobalVariable(TheModule, Ty, false,
202f4a2713aSLionel Sambuc         linkage, C, Name);
203f4a2713aSLionel Sambuc   }
204f4a2713aSLionel Sambuc   /// Generates a global array.  The vector must contain the same number of
205f4a2713aSLionel Sambuc   /// elements that the array type declares, of the type specified as the array
206f4a2713aSLionel Sambuc   /// element type.
MakeGlobal(llvm::ArrayType * Ty,ArrayRef<llvm::Constant * > V,StringRef Name="",llvm::GlobalValue::LinkageTypes linkage=llvm::GlobalValue::InternalLinkage)207f4a2713aSLionel Sambuc   llvm::GlobalVariable *MakeGlobal(llvm::ArrayType *Ty,
208f4a2713aSLionel Sambuc                                    ArrayRef<llvm::Constant *> V,
209f4a2713aSLionel Sambuc                                    StringRef Name="",
210f4a2713aSLionel Sambuc                                    llvm::GlobalValue::LinkageTypes linkage
211f4a2713aSLionel Sambuc                                          =llvm::GlobalValue::InternalLinkage) {
212f4a2713aSLionel Sambuc     llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
213f4a2713aSLionel Sambuc     return new llvm::GlobalVariable(TheModule, Ty, false,
214f4a2713aSLionel Sambuc                                     linkage, C, Name);
215f4a2713aSLionel Sambuc   }
216f4a2713aSLionel Sambuc   /// Generates a global array, inferring the array type from the specified
217f4a2713aSLionel Sambuc   /// element type and the size of the initialiser.
MakeGlobalArray(llvm::Type * Ty,ArrayRef<llvm::Constant * > V,StringRef Name="",llvm::GlobalValue::LinkageTypes linkage=llvm::GlobalValue::InternalLinkage)218f4a2713aSLionel Sambuc   llvm::GlobalVariable *MakeGlobalArray(llvm::Type *Ty,
219f4a2713aSLionel Sambuc                                         ArrayRef<llvm::Constant *> V,
220f4a2713aSLionel Sambuc                                         StringRef Name="",
221f4a2713aSLionel Sambuc                                         llvm::GlobalValue::LinkageTypes linkage
222f4a2713aSLionel Sambuc                                          =llvm::GlobalValue::InternalLinkage) {
223f4a2713aSLionel Sambuc     llvm::ArrayType *ArrayTy = llvm::ArrayType::get(Ty, V.size());
224f4a2713aSLionel Sambuc     return MakeGlobal(ArrayTy, V, Name, linkage);
225f4a2713aSLionel Sambuc   }
226f4a2713aSLionel Sambuc   /// Returns a property name and encoding string.
MakePropertyEncodingString(const ObjCPropertyDecl * PD,const Decl * Container)227f4a2713aSLionel Sambuc   llvm::Constant *MakePropertyEncodingString(const ObjCPropertyDecl *PD,
228f4a2713aSLionel Sambuc                                              const Decl *Container) {
229f4a2713aSLionel Sambuc     const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime;
230f4a2713aSLionel Sambuc     if ((R.getKind() == ObjCRuntime::GNUstep) &&
231f4a2713aSLionel Sambuc         (R.getVersion() >= VersionTuple(1, 6))) {
232f4a2713aSLionel Sambuc       std::string NameAndAttributes;
233f4a2713aSLionel Sambuc       std::string TypeStr;
234f4a2713aSLionel Sambuc       CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
235f4a2713aSLionel Sambuc       NameAndAttributes += '\0';
236f4a2713aSLionel Sambuc       NameAndAttributes += TypeStr.length() + 3;
237f4a2713aSLionel Sambuc       NameAndAttributes += TypeStr;
238f4a2713aSLionel Sambuc       NameAndAttributes += '\0';
239f4a2713aSLionel Sambuc       NameAndAttributes += PD->getNameAsString();
240f4a2713aSLionel Sambuc       return llvm::ConstantExpr::getGetElementPtr(
241*0a6a1f1dSLionel Sambuc           CGM.GetAddrOfConstantCString(NameAndAttributes), Zeros);
242f4a2713aSLionel Sambuc     }
243f4a2713aSLionel Sambuc     return MakeConstantString(PD->getNameAsString());
244f4a2713aSLionel Sambuc   }
245f4a2713aSLionel Sambuc   /// Push the property attributes into two structure fields.
PushPropertyAttributes(std::vector<llvm::Constant * > & Fields,ObjCPropertyDecl * property,bool isSynthesized=true,bool isDynamic=true)246f4a2713aSLionel Sambuc   void PushPropertyAttributes(std::vector<llvm::Constant*> &Fields,
247f4a2713aSLionel Sambuc       ObjCPropertyDecl *property, bool isSynthesized=true, bool
248f4a2713aSLionel Sambuc       isDynamic=true) {
249f4a2713aSLionel Sambuc     int attrs = property->getPropertyAttributes();
250f4a2713aSLionel Sambuc     // For read-only properties, clear the copy and retain flags
251f4a2713aSLionel Sambuc     if (attrs & ObjCPropertyDecl::OBJC_PR_readonly) {
252f4a2713aSLionel Sambuc       attrs &= ~ObjCPropertyDecl::OBJC_PR_copy;
253f4a2713aSLionel Sambuc       attrs &= ~ObjCPropertyDecl::OBJC_PR_retain;
254f4a2713aSLionel Sambuc       attrs &= ~ObjCPropertyDecl::OBJC_PR_weak;
255f4a2713aSLionel Sambuc       attrs &= ~ObjCPropertyDecl::OBJC_PR_strong;
256f4a2713aSLionel Sambuc     }
257f4a2713aSLionel Sambuc     // The first flags field has the same attribute values as clang uses internally
258f4a2713aSLionel Sambuc     Fields.push_back(llvm::ConstantInt::get(Int8Ty, attrs & 0xff));
259f4a2713aSLionel Sambuc     attrs >>= 8;
260f4a2713aSLionel Sambuc     attrs <<= 2;
261f4a2713aSLionel Sambuc     // For protocol properties, synthesized and dynamic have no meaning, so we
262f4a2713aSLionel Sambuc     // reuse these flags to indicate that this is a protocol property (both set
263f4a2713aSLionel Sambuc     // has no meaning, as a property can't be both synthesized and dynamic)
264f4a2713aSLionel Sambuc     attrs |= isSynthesized ? (1<<0) : 0;
265f4a2713aSLionel Sambuc     attrs |= isDynamic ? (1<<1) : 0;
266f4a2713aSLionel Sambuc     // The second field is the next four fields left shifted by two, with the
267f4a2713aSLionel Sambuc     // low bit set to indicate whether the field is synthesized or dynamic.
268f4a2713aSLionel Sambuc     Fields.push_back(llvm::ConstantInt::get(Int8Ty, attrs & 0xff));
269f4a2713aSLionel Sambuc     // Two padding fields
270f4a2713aSLionel Sambuc     Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
271f4a2713aSLionel Sambuc     Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
272f4a2713aSLionel Sambuc   }
273f4a2713aSLionel Sambuc   /// Ensures that the value has the required type, by inserting a bitcast if
274f4a2713aSLionel Sambuc   /// required.  This function lets us avoid inserting bitcasts that are
275f4a2713aSLionel Sambuc   /// redundant.
EnforceType(CGBuilderTy & B,llvm::Value * V,llvm::Type * Ty)276f4a2713aSLionel Sambuc   llvm::Value* EnforceType(CGBuilderTy &B, llvm::Value *V, llvm::Type *Ty) {
277f4a2713aSLionel Sambuc     if (V->getType() == Ty) return V;
278f4a2713aSLionel Sambuc     return B.CreateBitCast(V, Ty);
279f4a2713aSLionel Sambuc   }
280f4a2713aSLionel Sambuc   // Some zeros used for GEPs in lots of places.
281f4a2713aSLionel Sambuc   llvm::Constant *Zeros[2];
282f4a2713aSLionel Sambuc   /// Null pointer value.  Mainly used as a terminator in various arrays.
283f4a2713aSLionel Sambuc   llvm::Constant *NULLPtr;
284f4a2713aSLionel Sambuc   /// LLVM context.
285f4a2713aSLionel Sambuc   llvm::LLVMContext &VMContext;
286f4a2713aSLionel Sambuc private:
287f4a2713aSLionel Sambuc   /// Placeholder for the class.  Lots of things refer to the class before we've
288f4a2713aSLionel Sambuc   /// actually emitted it.  We use this alias as a placeholder, and then replace
289f4a2713aSLionel Sambuc   /// it with a pointer to the class structure before finally emitting the
290f4a2713aSLionel Sambuc   /// module.
291f4a2713aSLionel Sambuc   llvm::GlobalAlias *ClassPtrAlias;
292f4a2713aSLionel Sambuc   /// Placeholder for the metaclass.  Lots of things refer to the class before
293f4a2713aSLionel Sambuc   /// we've / actually emitted it.  We use this alias as a placeholder, and then
294f4a2713aSLionel Sambuc   /// replace / it with a pointer to the metaclass structure before finally
295f4a2713aSLionel Sambuc   /// emitting the / module.
296f4a2713aSLionel Sambuc   llvm::GlobalAlias *MetaClassPtrAlias;
297f4a2713aSLionel Sambuc   /// All of the classes that have been generated for this compilation units.
298f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Classes;
299f4a2713aSLionel Sambuc   /// All of the categories that have been generated for this compilation units.
300f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Categories;
301f4a2713aSLionel Sambuc   /// All of the Objective-C constant strings that have been generated for this
302f4a2713aSLionel Sambuc   /// compilation units.
303f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> ConstantStrings;
304f4a2713aSLionel Sambuc   /// Map from string values to Objective-C constant strings in the output.
305f4a2713aSLionel Sambuc   /// Used to prevent emitting Objective-C strings more than once.  This should
306f4a2713aSLionel Sambuc   /// not be required at all - CodeGenModule should manage this list.
307f4a2713aSLionel Sambuc   llvm::StringMap<llvm::Constant*> ObjCStrings;
308f4a2713aSLionel Sambuc   /// All of the protocols that have been declared.
309f4a2713aSLionel Sambuc   llvm::StringMap<llvm::Constant*> ExistingProtocols;
310f4a2713aSLionel Sambuc   /// For each variant of a selector, we store the type encoding and a
311f4a2713aSLionel Sambuc   /// placeholder value.  For an untyped selector, the type will be the empty
312f4a2713aSLionel Sambuc   /// string.  Selector references are all done via the module's selector table,
313f4a2713aSLionel Sambuc   /// so we create an alias as a placeholder and then replace it with the real
314f4a2713aSLionel Sambuc   /// value later.
315f4a2713aSLionel Sambuc   typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector;
316f4a2713aSLionel Sambuc   /// Type of the selector map.  This is roughly equivalent to the structure
317f4a2713aSLionel Sambuc   /// used in the GNUstep runtime, which maintains a list of all of the valid
318f4a2713aSLionel Sambuc   /// types for a selector in a table.
319f4a2713aSLionel Sambuc   typedef llvm::DenseMap<Selector, SmallVector<TypedSelector, 2> >
320f4a2713aSLionel Sambuc     SelectorMap;
321f4a2713aSLionel Sambuc   /// A map from selectors to selector types.  This allows us to emit all
322f4a2713aSLionel Sambuc   /// selectors of the same name and type together.
323f4a2713aSLionel Sambuc   SelectorMap SelectorTable;
324f4a2713aSLionel Sambuc 
325f4a2713aSLionel Sambuc   /// Selectors related to memory management.  When compiling in GC mode, we
326f4a2713aSLionel Sambuc   /// omit these.
327f4a2713aSLionel Sambuc   Selector RetainSel, ReleaseSel, AutoreleaseSel;
328f4a2713aSLionel Sambuc   /// Runtime functions used for memory management in GC mode.  Note that clang
329f4a2713aSLionel Sambuc   /// supports code generation for calling these functions, but neither GNU
330f4a2713aSLionel Sambuc   /// runtime actually supports this API properly yet.
331f4a2713aSLionel Sambuc   LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn,
332f4a2713aSLionel Sambuc     WeakAssignFn, GlobalAssignFn;
333f4a2713aSLionel Sambuc 
334f4a2713aSLionel Sambuc   typedef std::pair<std::string, std::string> ClassAliasPair;
335f4a2713aSLionel Sambuc   /// All classes that have aliases set for them.
336f4a2713aSLionel Sambuc   std::vector<ClassAliasPair> ClassAliases;
337f4a2713aSLionel Sambuc 
338f4a2713aSLionel Sambuc protected:
339f4a2713aSLionel Sambuc   /// Function used for throwing Objective-C exceptions.
340f4a2713aSLionel Sambuc   LazyRuntimeFunction ExceptionThrowFn;
341f4a2713aSLionel Sambuc   /// Function used for rethrowing exceptions, used at the end of \@finally or
342f4a2713aSLionel Sambuc   /// \@synchronize blocks.
343f4a2713aSLionel Sambuc   LazyRuntimeFunction ExceptionReThrowFn;
344f4a2713aSLionel Sambuc   /// Function called when entering a catch function.  This is required for
345f4a2713aSLionel Sambuc   /// differentiating Objective-C exceptions and foreign exceptions.
346f4a2713aSLionel Sambuc   LazyRuntimeFunction EnterCatchFn;
347f4a2713aSLionel Sambuc   /// Function called when exiting from a catch block.  Used to do exception
348f4a2713aSLionel Sambuc   /// cleanup.
349f4a2713aSLionel Sambuc   LazyRuntimeFunction ExitCatchFn;
350f4a2713aSLionel Sambuc   /// Function called when entering an \@synchronize block.  Acquires the lock.
351f4a2713aSLionel Sambuc   LazyRuntimeFunction SyncEnterFn;
352f4a2713aSLionel Sambuc   /// Function called when exiting an \@synchronize block.  Releases the lock.
353f4a2713aSLionel Sambuc   LazyRuntimeFunction SyncExitFn;
354f4a2713aSLionel Sambuc 
355f4a2713aSLionel Sambuc private:
356f4a2713aSLionel Sambuc 
357f4a2713aSLionel Sambuc   /// Function called if fast enumeration detects that the collection is
358f4a2713aSLionel Sambuc   /// modified during the update.
359f4a2713aSLionel Sambuc   LazyRuntimeFunction EnumerationMutationFn;
360f4a2713aSLionel Sambuc   /// Function for implementing synthesized property getters that return an
361f4a2713aSLionel Sambuc   /// object.
362f4a2713aSLionel Sambuc   LazyRuntimeFunction GetPropertyFn;
363f4a2713aSLionel Sambuc   /// Function for implementing synthesized property setters that return an
364f4a2713aSLionel Sambuc   /// object.
365f4a2713aSLionel Sambuc   LazyRuntimeFunction SetPropertyFn;
366f4a2713aSLionel Sambuc   /// Function used for non-object declared property getters.
367f4a2713aSLionel Sambuc   LazyRuntimeFunction GetStructPropertyFn;
368f4a2713aSLionel Sambuc   /// Function used for non-object declared property setters.
369f4a2713aSLionel Sambuc   LazyRuntimeFunction SetStructPropertyFn;
370f4a2713aSLionel Sambuc 
371f4a2713aSLionel Sambuc   /// The version of the runtime that this class targets.  Must match the
372f4a2713aSLionel Sambuc   /// version in the runtime.
373f4a2713aSLionel Sambuc   int RuntimeVersion;
374f4a2713aSLionel Sambuc   /// The version of the protocol class.  Used to differentiate between ObjC1
375f4a2713aSLionel Sambuc   /// and ObjC2 protocols.  Objective-C 1 protocols can not contain optional
376f4a2713aSLionel Sambuc   /// components and can not contain declared properties.  We always emit
377f4a2713aSLionel Sambuc   /// Objective-C 2 property structures, but we have to pretend that they're
378f4a2713aSLionel Sambuc   /// Objective-C 1 property structures when targeting the GCC runtime or it
379f4a2713aSLionel Sambuc   /// will abort.
380f4a2713aSLionel Sambuc   const int ProtocolVersion;
381f4a2713aSLionel Sambuc private:
382f4a2713aSLionel Sambuc   /// Generates an instance variable list structure.  This is a structure
383f4a2713aSLionel Sambuc   /// containing a size and an array of structures containing instance variable
384f4a2713aSLionel Sambuc   /// metadata.  This is used purely for introspection in the fragile ABI.  In
385f4a2713aSLionel Sambuc   /// the non-fragile ABI, it's used for instance variable fixup.
386f4a2713aSLionel Sambuc   llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
387f4a2713aSLionel Sambuc                                    ArrayRef<llvm::Constant *> IvarTypes,
388f4a2713aSLionel Sambuc                                    ArrayRef<llvm::Constant *> IvarOffsets);
389f4a2713aSLionel Sambuc   /// Generates a method list structure.  This is a structure containing a size
390f4a2713aSLionel Sambuc   /// and an array of structures containing method metadata.
391f4a2713aSLionel Sambuc   ///
392f4a2713aSLionel Sambuc   /// This structure is used by both classes and categories, and contains a next
393f4a2713aSLionel Sambuc   /// pointer allowing them to be chained together in a linked list.
394*0a6a1f1dSLionel Sambuc   llvm::Constant *GenerateMethodList(StringRef ClassName,
395*0a6a1f1dSLionel Sambuc       StringRef CategoryName,
396f4a2713aSLionel Sambuc       ArrayRef<Selector> MethodSels,
397f4a2713aSLionel Sambuc       ArrayRef<llvm::Constant *> MethodTypes,
398f4a2713aSLionel Sambuc       bool isClassMethodList);
399f4a2713aSLionel Sambuc   /// Emits an empty protocol.  This is used for \@protocol() where no protocol
400f4a2713aSLionel Sambuc   /// is found.  The runtime will (hopefully) fix up the pointer to refer to the
401f4a2713aSLionel Sambuc   /// real protocol.
402f4a2713aSLionel Sambuc   llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
403f4a2713aSLionel Sambuc   /// Generates a list of property metadata structures.  This follows the same
404f4a2713aSLionel Sambuc   /// pattern as method and instance variable metadata lists.
405f4a2713aSLionel Sambuc   llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID,
406f4a2713aSLionel Sambuc         SmallVectorImpl<Selector> &InstanceMethodSels,
407f4a2713aSLionel Sambuc         SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes);
408f4a2713aSLionel Sambuc   /// Generates a list of referenced protocols.  Classes, categories, and
409f4a2713aSLionel Sambuc   /// protocols all use this structure.
410f4a2713aSLionel Sambuc   llvm::Constant *GenerateProtocolList(ArrayRef<std::string> Protocols);
411f4a2713aSLionel Sambuc   /// To ensure that all protocols are seen by the runtime, we add a category on
412f4a2713aSLionel Sambuc   /// a class defined in the runtime, declaring no methods, but adopting the
413f4a2713aSLionel Sambuc   /// protocols.  This is a horribly ugly hack, but it allows us to collect all
414f4a2713aSLionel Sambuc   /// of the protocols without changing the ABI.
415f4a2713aSLionel Sambuc   void GenerateProtocolHolderCategory();
416f4a2713aSLionel Sambuc   /// Generates a class structure.
417f4a2713aSLionel Sambuc   llvm::Constant *GenerateClassStructure(
418f4a2713aSLionel Sambuc       llvm::Constant *MetaClass,
419f4a2713aSLionel Sambuc       llvm::Constant *SuperClass,
420f4a2713aSLionel Sambuc       unsigned info,
421f4a2713aSLionel Sambuc       const char *Name,
422f4a2713aSLionel Sambuc       llvm::Constant *Version,
423f4a2713aSLionel Sambuc       llvm::Constant *InstanceSize,
424f4a2713aSLionel Sambuc       llvm::Constant *IVars,
425f4a2713aSLionel Sambuc       llvm::Constant *Methods,
426f4a2713aSLionel Sambuc       llvm::Constant *Protocols,
427f4a2713aSLionel Sambuc       llvm::Constant *IvarOffsets,
428f4a2713aSLionel Sambuc       llvm::Constant *Properties,
429f4a2713aSLionel Sambuc       llvm::Constant *StrongIvarBitmap,
430f4a2713aSLionel Sambuc       llvm::Constant *WeakIvarBitmap,
431f4a2713aSLionel Sambuc       bool isMeta=false);
432f4a2713aSLionel Sambuc   /// Generates a method list.  This is used by protocols to define the required
433f4a2713aSLionel Sambuc   /// and optional methods.
434f4a2713aSLionel Sambuc   llvm::Constant *GenerateProtocolMethodList(
435f4a2713aSLionel Sambuc       ArrayRef<llvm::Constant *> MethodNames,
436f4a2713aSLionel Sambuc       ArrayRef<llvm::Constant *> MethodTypes);
437f4a2713aSLionel Sambuc   /// Returns a selector with the specified type encoding.  An empty string is
438f4a2713aSLionel Sambuc   /// used to return an untyped selector (with the types field set to NULL).
439f4a2713aSLionel Sambuc   llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel,
440f4a2713aSLionel Sambuc     const std::string &TypeEncoding, bool lval);
441f4a2713aSLionel Sambuc   /// Returns the variable used to store the offset of an instance variable.
442f4a2713aSLionel Sambuc   llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
443f4a2713aSLionel Sambuc       const ObjCIvarDecl *Ivar);
444f4a2713aSLionel Sambuc   /// Emits a reference to a class.  This allows the linker to object if there
445f4a2713aSLionel Sambuc   /// is no class of the matching name.
446f4a2713aSLionel Sambuc protected:
447f4a2713aSLionel Sambuc   void EmitClassRef(const std::string &className);
448f4a2713aSLionel Sambuc   /// Emits a pointer to the named class
449f4a2713aSLionel Sambuc   virtual llvm::Value *GetClassNamed(CodeGenFunction &CGF,
450f4a2713aSLionel Sambuc                                      const std::string &Name, bool isWeak);
451f4a2713aSLionel Sambuc   /// Looks up the method for sending a message to the specified object.  This
452f4a2713aSLionel Sambuc   /// mechanism differs between the GCC and GNU runtimes, so this method must be
453f4a2713aSLionel Sambuc   /// overridden in subclasses.
454f4a2713aSLionel Sambuc   virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
455f4a2713aSLionel Sambuc                                  llvm::Value *&Receiver,
456f4a2713aSLionel Sambuc                                  llvm::Value *cmd,
457f4a2713aSLionel Sambuc                                  llvm::MDNode *node,
458f4a2713aSLionel Sambuc                                  MessageSendInfo &MSI) = 0;
459f4a2713aSLionel Sambuc   /// Looks up the method for sending a message to a superclass.  This
460f4a2713aSLionel Sambuc   /// mechanism differs between the GCC and GNU runtimes, so this method must
461f4a2713aSLionel Sambuc   /// be overridden in subclasses.
462f4a2713aSLionel Sambuc   virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
463f4a2713aSLionel Sambuc                                       llvm::Value *ObjCSuper,
464f4a2713aSLionel Sambuc                                       llvm::Value *cmd,
465f4a2713aSLionel Sambuc                                       MessageSendInfo &MSI) = 0;
466f4a2713aSLionel Sambuc   /// Libobjc2 uses a bitfield representation where small(ish) bitfields are
467f4a2713aSLionel Sambuc   /// stored in a 64-bit value with the low bit set to 1 and the remaining 63
468f4a2713aSLionel Sambuc   /// bits set to their values, LSB first, while larger ones are stored in a
469f4a2713aSLionel Sambuc   /// structure of this / form:
470f4a2713aSLionel Sambuc   ///
471f4a2713aSLionel Sambuc   /// struct { int32_t length; int32_t values[length]; };
472f4a2713aSLionel Sambuc   ///
473f4a2713aSLionel Sambuc   /// The values in the array are stored in host-endian format, with the least
474f4a2713aSLionel Sambuc   /// significant bit being assumed to come first in the bitfield.  Therefore,
475f4a2713aSLionel Sambuc   /// a bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] },
476f4a2713aSLionel Sambuc   /// while a bitfield / with the 63rd bit set will be 1<<64.
477f4a2713aSLionel Sambuc   llvm::Constant *MakeBitField(ArrayRef<bool> bits);
478f4a2713aSLionel Sambuc public:
479f4a2713aSLionel Sambuc   CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
480f4a2713aSLionel Sambuc       unsigned protocolClassVersion);
481f4a2713aSLionel Sambuc 
482*0a6a1f1dSLionel Sambuc   llvm::Constant *GenerateConstantString(const StringLiteral *) override;
483f4a2713aSLionel Sambuc 
484*0a6a1f1dSLionel Sambuc   RValue
485*0a6a1f1dSLionel Sambuc   GenerateMessageSend(CodeGenFunction &CGF, ReturnValueSlot Return,
486*0a6a1f1dSLionel Sambuc                       QualType ResultType, Selector Sel,
487*0a6a1f1dSLionel Sambuc                       llvm::Value *Receiver, const CallArgList &CallArgs,
488f4a2713aSLionel Sambuc                       const ObjCInterfaceDecl *Class,
489*0a6a1f1dSLionel Sambuc                       const ObjCMethodDecl *Method) override;
490*0a6a1f1dSLionel Sambuc   RValue
491*0a6a1f1dSLionel Sambuc   GenerateMessageSendSuper(CodeGenFunction &CGF, ReturnValueSlot Return,
492*0a6a1f1dSLionel Sambuc                            QualType ResultType, Selector Sel,
493f4a2713aSLionel Sambuc                            const ObjCInterfaceDecl *Class,
494*0a6a1f1dSLionel Sambuc                            bool isCategoryImpl, llvm::Value *Receiver,
495*0a6a1f1dSLionel Sambuc                            bool IsClassMessage, const CallArgList &CallArgs,
496*0a6a1f1dSLionel Sambuc                            const ObjCMethodDecl *Method) override;
497*0a6a1f1dSLionel Sambuc   llvm::Value *GetClass(CodeGenFunction &CGF,
498*0a6a1f1dSLionel Sambuc                         const ObjCInterfaceDecl *OID) override;
499*0a6a1f1dSLionel Sambuc   llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel,
500*0a6a1f1dSLionel Sambuc                            bool lval = false) override;
501*0a6a1f1dSLionel Sambuc   llvm::Value *GetSelector(CodeGenFunction &CGF,
502*0a6a1f1dSLionel Sambuc                            const ObjCMethodDecl *Method) override;
503*0a6a1f1dSLionel Sambuc   llvm::Constant *GetEHType(QualType T) override;
504f4a2713aSLionel Sambuc 
505*0a6a1f1dSLionel Sambuc   llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
506*0a6a1f1dSLionel Sambuc                                  const ObjCContainerDecl *CD) override;
507*0a6a1f1dSLionel Sambuc   void GenerateCategory(const ObjCCategoryImplDecl *CMD) override;
508*0a6a1f1dSLionel Sambuc   void GenerateClass(const ObjCImplementationDecl *ClassDecl) override;
509*0a6a1f1dSLionel Sambuc   void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) override;
510*0a6a1f1dSLionel Sambuc   llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
511*0a6a1f1dSLionel Sambuc                                    const ObjCProtocolDecl *PD) override;
512*0a6a1f1dSLionel Sambuc   void GenerateProtocol(const ObjCProtocolDecl *PD) override;
513*0a6a1f1dSLionel Sambuc   llvm::Function *ModuleInitFunction() override;
514*0a6a1f1dSLionel Sambuc   llvm::Constant *GetPropertyGetFunction() override;
515*0a6a1f1dSLionel Sambuc   llvm::Constant *GetPropertySetFunction() override;
516*0a6a1f1dSLionel Sambuc   llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
517*0a6a1f1dSLionel Sambuc                                                   bool copy) override;
518*0a6a1f1dSLionel Sambuc   llvm::Constant *GetSetStructFunction() override;
519*0a6a1f1dSLionel Sambuc   llvm::Constant *GetGetStructFunction() override;
520*0a6a1f1dSLionel Sambuc   llvm::Constant *GetCppAtomicObjectGetFunction() override;
521*0a6a1f1dSLionel Sambuc   llvm::Constant *GetCppAtomicObjectSetFunction() override;
522*0a6a1f1dSLionel Sambuc   llvm::Constant *EnumerationMutationFunction() override;
523f4a2713aSLionel Sambuc 
524*0a6a1f1dSLionel Sambuc   void EmitTryStmt(CodeGenFunction &CGF,
525*0a6a1f1dSLionel Sambuc                    const ObjCAtTryStmt &S) override;
526*0a6a1f1dSLionel Sambuc   void EmitSynchronizedStmt(CodeGenFunction &CGF,
527*0a6a1f1dSLionel Sambuc                             const ObjCAtSynchronizedStmt &S) override;
528*0a6a1f1dSLionel Sambuc   void EmitThrowStmt(CodeGenFunction &CGF,
529f4a2713aSLionel Sambuc                      const ObjCAtThrowStmt &S,
530*0a6a1f1dSLionel Sambuc                      bool ClearInsertionPoint=true) override;
531*0a6a1f1dSLionel Sambuc   llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF,
532*0a6a1f1dSLionel Sambuc                                  llvm::Value *AddrWeakObj) override;
533*0a6a1f1dSLionel Sambuc   void EmitObjCWeakAssign(CodeGenFunction &CGF,
534*0a6a1f1dSLionel Sambuc                           llvm::Value *src, llvm::Value *dst) override;
535*0a6a1f1dSLionel Sambuc   void EmitObjCGlobalAssign(CodeGenFunction &CGF,
536f4a2713aSLionel Sambuc                             llvm::Value *src, llvm::Value *dest,
537*0a6a1f1dSLionel Sambuc                             bool threadlocal=false) override;
538*0a6a1f1dSLionel Sambuc   void EmitObjCIvarAssign(CodeGenFunction &CGF, llvm::Value *src,
539*0a6a1f1dSLionel Sambuc                           llvm::Value *dest, llvm::Value *ivarOffset) override;
540*0a6a1f1dSLionel Sambuc   void EmitObjCStrongCastAssign(CodeGenFunction &CGF,
541*0a6a1f1dSLionel Sambuc                                 llvm::Value *src, llvm::Value *dest) override;
542*0a6a1f1dSLionel Sambuc   void EmitGCMemmoveCollectable(CodeGenFunction &CGF, llvm::Value *DestPtr,
543f4a2713aSLionel Sambuc                                 llvm::Value *SrcPtr,
544*0a6a1f1dSLionel Sambuc                                 llvm::Value *Size) override;
545*0a6a1f1dSLionel Sambuc   LValue EmitObjCValueForIvar(CodeGenFunction &CGF, QualType ObjectTy,
546*0a6a1f1dSLionel Sambuc                               llvm::Value *BaseValue, const ObjCIvarDecl *Ivar,
547*0a6a1f1dSLionel Sambuc                               unsigned CVRQualifiers) override;
548*0a6a1f1dSLionel Sambuc   llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
549f4a2713aSLionel Sambuc                               const ObjCInterfaceDecl *Interface,
550*0a6a1f1dSLionel Sambuc                               const ObjCIvarDecl *Ivar) override;
551*0a6a1f1dSLionel Sambuc   llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) override;
BuildGCBlockLayout(CodeGenModule & CGM,const CGBlockInfo & blockInfo)552*0a6a1f1dSLionel Sambuc   llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM,
553*0a6a1f1dSLionel Sambuc                                      const CGBlockInfo &blockInfo) override {
554f4a2713aSLionel Sambuc     return NULLPtr;
555f4a2713aSLionel Sambuc   }
BuildRCBlockLayout(CodeGenModule & CGM,const CGBlockInfo & blockInfo)556*0a6a1f1dSLionel Sambuc   llvm::Constant *BuildRCBlockLayout(CodeGenModule &CGM,
557*0a6a1f1dSLionel Sambuc                                      const CGBlockInfo &blockInfo) override {
558f4a2713aSLionel Sambuc     return NULLPtr;
559f4a2713aSLionel Sambuc   }
560f4a2713aSLionel Sambuc 
BuildByrefLayout(CodeGenModule & CGM,QualType T)561*0a6a1f1dSLionel Sambuc   llvm::Constant *BuildByrefLayout(CodeGenModule &CGM, QualType T) override {
562f4a2713aSLionel Sambuc     return NULLPtr;
563f4a2713aSLionel Sambuc   }
564f4a2713aSLionel Sambuc 
GetClassGlobal(const std::string & Name,bool Weak=false)565*0a6a1f1dSLionel Sambuc   llvm::GlobalVariable *GetClassGlobal(const std::string &Name,
566*0a6a1f1dSLionel Sambuc                                        bool Weak = false) override {
567*0a6a1f1dSLionel Sambuc     return nullptr;
568f4a2713aSLionel Sambuc   }
569f4a2713aSLionel Sambuc };
570f4a2713aSLionel Sambuc /// Class representing the legacy GCC Objective-C ABI.  This is the default when
571f4a2713aSLionel Sambuc /// -fobjc-nonfragile-abi is not specified.
572f4a2713aSLionel Sambuc ///
573f4a2713aSLionel Sambuc /// The GCC ABI target actually generates code that is approximately compatible
574f4a2713aSLionel Sambuc /// with the new GNUstep runtime ABI, but refrains from using any features that
575f4a2713aSLionel Sambuc /// would not work with the GCC runtime.  For example, clang always generates
576f4a2713aSLionel Sambuc /// the extended form of the class structure, and the extra fields are simply
577f4a2713aSLionel Sambuc /// ignored by GCC libobjc.
578f4a2713aSLionel Sambuc class CGObjCGCC : public CGObjCGNU {
579f4a2713aSLionel Sambuc   /// The GCC ABI message lookup function.  Returns an IMP pointing to the
580f4a2713aSLionel Sambuc   /// method implementation for this message.
581f4a2713aSLionel Sambuc   LazyRuntimeFunction MsgLookupFn;
582f4a2713aSLionel Sambuc   /// The GCC ABI superclass message lookup function.  Takes a pointer to a
583f4a2713aSLionel Sambuc   /// structure describing the receiver and the class, and a selector as
584f4a2713aSLionel Sambuc   /// arguments.  Returns the IMP for the corresponding method.
585f4a2713aSLionel Sambuc   LazyRuntimeFunction MsgLookupSuperFn;
586f4a2713aSLionel Sambuc protected:
LookupIMP(CodeGenFunction & CGF,llvm::Value * & Receiver,llvm::Value * cmd,llvm::MDNode * node,MessageSendInfo & MSI)587*0a6a1f1dSLionel Sambuc   llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
588*0a6a1f1dSLionel Sambuc                          llvm::Value *cmd, llvm::MDNode *node,
589*0a6a1f1dSLionel Sambuc                          MessageSendInfo &MSI) override {
590f4a2713aSLionel Sambuc     CGBuilderTy &Builder = CGF.Builder;
591f4a2713aSLionel Sambuc     llvm::Value *args[] = {
592f4a2713aSLionel Sambuc             EnforceType(Builder, Receiver, IdTy),
593f4a2713aSLionel Sambuc             EnforceType(Builder, cmd, SelectorTy) };
594f4a2713aSLionel Sambuc     llvm::CallSite imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
595f4a2713aSLionel Sambuc     imp->setMetadata(msgSendMDKind, node);
596f4a2713aSLionel Sambuc     return imp.getInstruction();
597f4a2713aSLionel Sambuc   }
LookupIMPSuper(CodeGenFunction & CGF,llvm::Value * ObjCSuper,llvm::Value * cmd,MessageSendInfo & MSI)598*0a6a1f1dSLionel Sambuc   llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, llvm::Value *ObjCSuper,
599*0a6a1f1dSLionel Sambuc                               llvm::Value *cmd, MessageSendInfo &MSI) override {
600f4a2713aSLionel Sambuc       CGBuilderTy &Builder = CGF.Builder;
601f4a2713aSLionel Sambuc       llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper,
602f4a2713aSLionel Sambuc           PtrToObjCSuperTy), cmd};
603f4a2713aSLionel Sambuc       return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
604f4a2713aSLionel Sambuc     }
605f4a2713aSLionel Sambuc   public:
CGObjCGCC(CodeGenModule & Mod)606f4a2713aSLionel Sambuc     CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) {
607f4a2713aSLionel Sambuc       // IMP objc_msg_lookup(id, SEL);
608*0a6a1f1dSLionel Sambuc       MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy,
609*0a6a1f1dSLionel Sambuc                        nullptr);
610f4a2713aSLionel Sambuc       // IMP objc_msg_lookup_super(struct objc_super*, SEL);
611f4a2713aSLionel Sambuc       MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
612*0a6a1f1dSLionel Sambuc               PtrToObjCSuperTy, SelectorTy, nullptr);
613f4a2713aSLionel Sambuc     }
614f4a2713aSLionel Sambuc };
615f4a2713aSLionel Sambuc /// Class used when targeting the new GNUstep runtime ABI.
616f4a2713aSLionel Sambuc class CGObjCGNUstep : public CGObjCGNU {
617f4a2713aSLionel Sambuc     /// The slot lookup function.  Returns a pointer to a cacheable structure
618f4a2713aSLionel Sambuc     /// that contains (among other things) the IMP.
619f4a2713aSLionel Sambuc     LazyRuntimeFunction SlotLookupFn;
620f4a2713aSLionel Sambuc     /// The GNUstep ABI superclass message lookup function.  Takes a pointer to
621f4a2713aSLionel Sambuc     /// a structure describing the receiver and the class, and a selector as
622f4a2713aSLionel Sambuc     /// arguments.  Returns the slot for the corresponding method.  Superclass
623f4a2713aSLionel Sambuc     /// message lookup rarely changes, so this is a good caching opportunity.
624f4a2713aSLionel Sambuc     LazyRuntimeFunction SlotLookupSuperFn;
625f4a2713aSLionel Sambuc     /// Specialised function for setting atomic retain properties
626f4a2713aSLionel Sambuc     LazyRuntimeFunction SetPropertyAtomic;
627f4a2713aSLionel Sambuc     /// Specialised function for setting atomic copy properties
628f4a2713aSLionel Sambuc     LazyRuntimeFunction SetPropertyAtomicCopy;
629f4a2713aSLionel Sambuc     /// Specialised function for setting nonatomic retain properties
630f4a2713aSLionel Sambuc     LazyRuntimeFunction SetPropertyNonAtomic;
631f4a2713aSLionel Sambuc     /// Specialised function for setting nonatomic copy properties
632f4a2713aSLionel Sambuc     LazyRuntimeFunction SetPropertyNonAtomicCopy;
633f4a2713aSLionel Sambuc     /// Function to perform atomic copies of C++ objects with nontrivial copy
634f4a2713aSLionel Sambuc     /// constructors from Objective-C ivars.
635f4a2713aSLionel Sambuc     LazyRuntimeFunction CxxAtomicObjectGetFn;
636f4a2713aSLionel Sambuc     /// Function to perform atomic copies of C++ objects with nontrivial copy
637f4a2713aSLionel Sambuc     /// constructors to Objective-C ivars.
638f4a2713aSLionel Sambuc     LazyRuntimeFunction CxxAtomicObjectSetFn;
639f4a2713aSLionel Sambuc     /// Type of an slot structure pointer.  This is returned by the various
640f4a2713aSLionel Sambuc     /// lookup functions.
641f4a2713aSLionel Sambuc     llvm::Type *SlotTy;
642f4a2713aSLionel Sambuc   public:
643*0a6a1f1dSLionel Sambuc     llvm::Constant *GetEHType(QualType T) override;
644f4a2713aSLionel Sambuc   protected:
LookupIMP(CodeGenFunction & CGF,llvm::Value * & Receiver,llvm::Value * cmd,llvm::MDNode * node,MessageSendInfo & MSI)645*0a6a1f1dSLionel Sambuc     llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
646*0a6a1f1dSLionel Sambuc                            llvm::Value *cmd, llvm::MDNode *node,
647*0a6a1f1dSLionel Sambuc                            MessageSendInfo &MSI) override {
648f4a2713aSLionel Sambuc       CGBuilderTy &Builder = CGF.Builder;
649f4a2713aSLionel Sambuc       llvm::Function *LookupFn = SlotLookupFn;
650f4a2713aSLionel Sambuc 
651f4a2713aSLionel Sambuc       // Store the receiver on the stack so that we can reload it later
652f4a2713aSLionel Sambuc       llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType());
653f4a2713aSLionel Sambuc       Builder.CreateStore(Receiver, ReceiverPtr);
654f4a2713aSLionel Sambuc 
655f4a2713aSLionel Sambuc       llvm::Value *self;
656f4a2713aSLionel Sambuc 
657f4a2713aSLionel Sambuc       if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) {
658f4a2713aSLionel Sambuc         self = CGF.LoadObjCSelf();
659f4a2713aSLionel Sambuc       } else {
660f4a2713aSLionel Sambuc         self = llvm::ConstantPointerNull::get(IdTy);
661f4a2713aSLionel Sambuc       }
662f4a2713aSLionel Sambuc 
663f4a2713aSLionel Sambuc       // The lookup function is guaranteed not to capture the receiver pointer.
664f4a2713aSLionel Sambuc       LookupFn->setDoesNotCapture(1);
665f4a2713aSLionel Sambuc 
666f4a2713aSLionel Sambuc       llvm::Value *args[] = {
667f4a2713aSLionel Sambuc               EnforceType(Builder, ReceiverPtr, PtrToIdTy),
668f4a2713aSLionel Sambuc               EnforceType(Builder, cmd, SelectorTy),
669f4a2713aSLionel Sambuc               EnforceType(Builder, self, IdTy) };
670f4a2713aSLionel Sambuc       llvm::CallSite slot = CGF.EmitRuntimeCallOrInvoke(LookupFn, args);
671f4a2713aSLionel Sambuc       slot.setOnlyReadsMemory();
672f4a2713aSLionel Sambuc       slot->setMetadata(msgSendMDKind, node);
673f4a2713aSLionel Sambuc 
674f4a2713aSLionel Sambuc       // Load the imp from the slot
675f4a2713aSLionel Sambuc       llvm::Value *imp =
676f4a2713aSLionel Sambuc         Builder.CreateLoad(Builder.CreateStructGEP(slot.getInstruction(), 4));
677f4a2713aSLionel Sambuc 
678f4a2713aSLionel Sambuc       // The lookup function may have changed the receiver, so make sure we use
679f4a2713aSLionel Sambuc       // the new one.
680f4a2713aSLionel Sambuc       Receiver = Builder.CreateLoad(ReceiverPtr, true);
681f4a2713aSLionel Sambuc       return imp;
682f4a2713aSLionel Sambuc     }
LookupIMPSuper(CodeGenFunction & CGF,llvm::Value * ObjCSuper,llvm::Value * cmd,MessageSendInfo & MSI)683*0a6a1f1dSLionel Sambuc     llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, llvm::Value *ObjCSuper,
684f4a2713aSLionel Sambuc                                 llvm::Value *cmd,
685*0a6a1f1dSLionel Sambuc                                 MessageSendInfo &MSI) override {
686f4a2713aSLionel Sambuc       CGBuilderTy &Builder = CGF.Builder;
687f4a2713aSLionel Sambuc       llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
688f4a2713aSLionel Sambuc 
689f4a2713aSLionel Sambuc       llvm::CallInst *slot =
690f4a2713aSLionel Sambuc         CGF.EmitNounwindRuntimeCall(SlotLookupSuperFn, lookupArgs);
691f4a2713aSLionel Sambuc       slot->setOnlyReadsMemory();
692f4a2713aSLionel Sambuc 
693f4a2713aSLionel Sambuc       return Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
694f4a2713aSLionel Sambuc     }
695f4a2713aSLionel Sambuc   public:
CGObjCGNUstep(CodeGenModule & Mod)696f4a2713aSLionel Sambuc     CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNU(Mod, 9, 3) {
697f4a2713aSLionel Sambuc       const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime;
698f4a2713aSLionel Sambuc 
699f4a2713aSLionel Sambuc       llvm::StructType *SlotStructTy = llvm::StructType::get(PtrTy,
700*0a6a1f1dSLionel Sambuc           PtrTy, PtrTy, IntTy, IMPTy, nullptr);
701f4a2713aSLionel Sambuc       SlotTy = llvm::PointerType::getUnqual(SlotStructTy);
702f4a2713aSLionel Sambuc       // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
703f4a2713aSLionel Sambuc       SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy,
704*0a6a1f1dSLionel Sambuc           SelectorTy, IdTy, nullptr);
705f4a2713aSLionel Sambuc       // Slot_t objc_msg_lookup_super(struct objc_super*, SEL);
706f4a2713aSLionel Sambuc       SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
707*0a6a1f1dSLionel Sambuc               PtrToObjCSuperTy, SelectorTy, nullptr);
708f4a2713aSLionel Sambuc       // If we're in ObjC++ mode, then we want to make
709f4a2713aSLionel Sambuc       if (CGM.getLangOpts().CPlusPlus) {
710f4a2713aSLionel Sambuc         llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
711f4a2713aSLionel Sambuc         // void *__cxa_begin_catch(void *e)
712*0a6a1f1dSLionel Sambuc         EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy, nullptr);
713f4a2713aSLionel Sambuc         // void __cxa_end_catch(void)
714*0a6a1f1dSLionel Sambuc         ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy, nullptr);
715f4a2713aSLionel Sambuc         // void _Unwind_Resume_or_Rethrow(void*)
716f4a2713aSLionel Sambuc         ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
717*0a6a1f1dSLionel Sambuc             PtrTy, nullptr);
718f4a2713aSLionel Sambuc       } else if (R.getVersion() >= VersionTuple(1, 7)) {
719f4a2713aSLionel Sambuc         llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
720f4a2713aSLionel Sambuc         // id objc_begin_catch(void *e)
721*0a6a1f1dSLionel Sambuc         EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy, nullptr);
722f4a2713aSLionel Sambuc         // void objc_end_catch(void)
723*0a6a1f1dSLionel Sambuc         ExitCatchFn.init(&CGM, "objc_end_catch", VoidTy, nullptr);
724f4a2713aSLionel Sambuc         // void _Unwind_Resume_or_Rethrow(void*)
725f4a2713aSLionel Sambuc         ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy,
726*0a6a1f1dSLionel Sambuc             PtrTy, nullptr);
727f4a2713aSLionel Sambuc       }
728f4a2713aSLionel Sambuc       llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
729f4a2713aSLionel Sambuc       SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
730*0a6a1f1dSLionel Sambuc           SelectorTy, IdTy, PtrDiffTy, nullptr);
731f4a2713aSLionel Sambuc       SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
732*0a6a1f1dSLionel Sambuc           IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr);
733f4a2713aSLionel Sambuc       SetPropertyNonAtomic.init(&CGM, "objc_setProperty_nonatomic", VoidTy,
734*0a6a1f1dSLionel Sambuc           IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr);
735f4a2713aSLionel Sambuc       SetPropertyNonAtomicCopy.init(&CGM, "objc_setProperty_nonatomic_copy",
736*0a6a1f1dSLionel Sambuc           VoidTy, IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr);
737f4a2713aSLionel Sambuc       // void objc_setCppObjectAtomic(void *dest, const void *src, void
738f4a2713aSLionel Sambuc       // *helper);
739f4a2713aSLionel Sambuc       CxxAtomicObjectSetFn.init(&CGM, "objc_setCppObjectAtomic", VoidTy, PtrTy,
740*0a6a1f1dSLionel Sambuc           PtrTy, PtrTy, nullptr);
741f4a2713aSLionel Sambuc       // void objc_getCppObjectAtomic(void *dest, const void *src, void
742f4a2713aSLionel Sambuc       // *helper);
743f4a2713aSLionel Sambuc       CxxAtomicObjectGetFn.init(&CGM, "objc_getCppObjectAtomic", VoidTy, PtrTy,
744*0a6a1f1dSLionel Sambuc           PtrTy, PtrTy, nullptr);
745f4a2713aSLionel Sambuc     }
GetCppAtomicObjectGetFunction()746*0a6a1f1dSLionel Sambuc     llvm::Constant *GetCppAtomicObjectGetFunction() override {
747f4a2713aSLionel Sambuc       // The optimised functions were added in version 1.7 of the GNUstep
748f4a2713aSLionel Sambuc       // runtime.
749f4a2713aSLionel Sambuc       assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
750f4a2713aSLionel Sambuc           VersionTuple(1, 7));
751f4a2713aSLionel Sambuc       return CxxAtomicObjectGetFn;
752f4a2713aSLionel Sambuc     }
GetCppAtomicObjectSetFunction()753*0a6a1f1dSLionel Sambuc     llvm::Constant *GetCppAtomicObjectSetFunction() override {
754f4a2713aSLionel Sambuc       // The optimised functions were added in version 1.7 of the GNUstep
755f4a2713aSLionel Sambuc       // runtime.
756f4a2713aSLionel Sambuc       assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
757f4a2713aSLionel Sambuc           VersionTuple(1, 7));
758f4a2713aSLionel Sambuc       return CxxAtomicObjectSetFn;
759f4a2713aSLionel Sambuc     }
GetOptimizedPropertySetFunction(bool atomic,bool copy)760*0a6a1f1dSLionel Sambuc     llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
761*0a6a1f1dSLionel Sambuc                                                     bool copy) override {
762f4a2713aSLionel Sambuc       // The optimised property functions omit the GC check, and so are not
763f4a2713aSLionel Sambuc       // safe to use in GC mode.  The standard functions are fast in GC mode,
764f4a2713aSLionel Sambuc       // so there is less advantage in using them.
765f4a2713aSLionel Sambuc       assert ((CGM.getLangOpts().getGC() == LangOptions::NonGC));
766f4a2713aSLionel Sambuc       // The optimised functions were added in version 1.7 of the GNUstep
767f4a2713aSLionel Sambuc       // runtime.
768f4a2713aSLionel Sambuc       assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
769f4a2713aSLionel Sambuc           VersionTuple(1, 7));
770f4a2713aSLionel Sambuc 
771f4a2713aSLionel Sambuc       if (atomic) {
772f4a2713aSLionel Sambuc         if (copy) return SetPropertyAtomicCopy;
773f4a2713aSLionel Sambuc         return SetPropertyAtomic;
774f4a2713aSLionel Sambuc       }
775f4a2713aSLionel Sambuc 
776*0a6a1f1dSLionel Sambuc       return copy ? SetPropertyNonAtomicCopy : SetPropertyNonAtomic;
777f4a2713aSLionel Sambuc     }
778f4a2713aSLionel Sambuc };
779f4a2713aSLionel Sambuc 
780f4a2713aSLionel Sambuc /// Support for the ObjFW runtime.
781f4a2713aSLionel Sambuc class CGObjCObjFW: public CGObjCGNU {
782f4a2713aSLionel Sambuc protected:
783f4a2713aSLionel Sambuc   /// The GCC ABI message lookup function.  Returns an IMP pointing to the
784f4a2713aSLionel Sambuc   /// method implementation for this message.
785f4a2713aSLionel Sambuc   LazyRuntimeFunction MsgLookupFn;
786f4a2713aSLionel Sambuc   /// stret lookup function.  While this does not seem to make sense at the
787f4a2713aSLionel Sambuc   /// first look, this is required to call the correct forwarding function.
788f4a2713aSLionel Sambuc   LazyRuntimeFunction MsgLookupFnSRet;
789f4a2713aSLionel Sambuc   /// The GCC ABI superclass message lookup function.  Takes a pointer to a
790f4a2713aSLionel Sambuc   /// structure describing the receiver and the class, and a selector as
791f4a2713aSLionel Sambuc   /// arguments.  Returns the IMP for the corresponding method.
792f4a2713aSLionel Sambuc   LazyRuntimeFunction MsgLookupSuperFn, MsgLookupSuperFnSRet;
793f4a2713aSLionel Sambuc 
LookupIMP(CodeGenFunction & CGF,llvm::Value * & Receiver,llvm::Value * cmd,llvm::MDNode * node,MessageSendInfo & MSI)794*0a6a1f1dSLionel Sambuc   llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
795*0a6a1f1dSLionel Sambuc                          llvm::Value *cmd, llvm::MDNode *node,
796*0a6a1f1dSLionel Sambuc                          MessageSendInfo &MSI) override {
797f4a2713aSLionel Sambuc     CGBuilderTy &Builder = CGF.Builder;
798f4a2713aSLionel Sambuc     llvm::Value *args[] = {
799f4a2713aSLionel Sambuc             EnforceType(Builder, Receiver, IdTy),
800f4a2713aSLionel Sambuc             EnforceType(Builder, cmd, SelectorTy) };
801f4a2713aSLionel Sambuc 
802f4a2713aSLionel Sambuc     llvm::CallSite imp;
803f4a2713aSLionel Sambuc     if (CGM.ReturnTypeUsesSRet(MSI.CallInfo))
804f4a2713aSLionel Sambuc       imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFnSRet, args);
805f4a2713aSLionel Sambuc     else
806f4a2713aSLionel Sambuc       imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
807f4a2713aSLionel Sambuc 
808f4a2713aSLionel Sambuc     imp->setMetadata(msgSendMDKind, node);
809f4a2713aSLionel Sambuc     return imp.getInstruction();
810f4a2713aSLionel Sambuc   }
811f4a2713aSLionel Sambuc 
LookupIMPSuper(CodeGenFunction & CGF,llvm::Value * ObjCSuper,llvm::Value * cmd,MessageSendInfo & MSI)812*0a6a1f1dSLionel Sambuc   llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, llvm::Value *ObjCSuper,
813*0a6a1f1dSLionel Sambuc                               llvm::Value *cmd, MessageSendInfo &MSI) override {
814f4a2713aSLionel Sambuc       CGBuilderTy &Builder = CGF.Builder;
815f4a2713aSLionel Sambuc       llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper,
816f4a2713aSLionel Sambuc           PtrToObjCSuperTy), cmd};
817f4a2713aSLionel Sambuc 
818f4a2713aSLionel Sambuc       if (CGM.ReturnTypeUsesSRet(MSI.CallInfo))
819f4a2713aSLionel Sambuc         return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFnSRet, lookupArgs);
820f4a2713aSLionel Sambuc       else
821f4a2713aSLionel Sambuc         return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
822f4a2713aSLionel Sambuc     }
823f4a2713aSLionel Sambuc 
GetClassNamed(CodeGenFunction & CGF,const std::string & Name,bool isWeak)824*0a6a1f1dSLionel Sambuc   llvm::Value *GetClassNamed(CodeGenFunction &CGF,
825*0a6a1f1dSLionel Sambuc                              const std::string &Name, bool isWeak) override {
826f4a2713aSLionel Sambuc     if (isWeak)
827f4a2713aSLionel Sambuc       return CGObjCGNU::GetClassNamed(CGF, Name, isWeak);
828f4a2713aSLionel Sambuc 
829f4a2713aSLionel Sambuc     EmitClassRef(Name);
830f4a2713aSLionel Sambuc 
831f4a2713aSLionel Sambuc     std::string SymbolName = "_OBJC_CLASS_" + Name;
832f4a2713aSLionel Sambuc 
833f4a2713aSLionel Sambuc     llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(SymbolName);
834f4a2713aSLionel Sambuc 
835f4a2713aSLionel Sambuc     if (!ClassSymbol)
836f4a2713aSLionel Sambuc       ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
837f4a2713aSLionel Sambuc                                              llvm::GlobalValue::ExternalLinkage,
838*0a6a1f1dSLionel Sambuc                                              nullptr, SymbolName);
839f4a2713aSLionel Sambuc 
840f4a2713aSLionel Sambuc     return ClassSymbol;
841f4a2713aSLionel Sambuc   }
842f4a2713aSLionel Sambuc 
843f4a2713aSLionel Sambuc public:
CGObjCObjFW(CodeGenModule & Mod)844f4a2713aSLionel Sambuc   CGObjCObjFW(CodeGenModule &Mod): CGObjCGNU(Mod, 9, 3) {
845f4a2713aSLionel Sambuc     // IMP objc_msg_lookup(id, SEL);
846*0a6a1f1dSLionel Sambuc     MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, nullptr);
847f4a2713aSLionel Sambuc     MsgLookupFnSRet.init(&CGM, "objc_msg_lookup_stret", IMPTy, IdTy,
848*0a6a1f1dSLionel Sambuc                          SelectorTy, nullptr);
849f4a2713aSLionel Sambuc     // IMP objc_msg_lookup_super(struct objc_super*, SEL);
850f4a2713aSLionel Sambuc     MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
851*0a6a1f1dSLionel Sambuc                           PtrToObjCSuperTy, SelectorTy, nullptr);
852f4a2713aSLionel Sambuc     MsgLookupSuperFnSRet.init(&CGM, "objc_msg_lookup_super_stret", IMPTy,
853*0a6a1f1dSLionel Sambuc                               PtrToObjCSuperTy, SelectorTy, nullptr);
854f4a2713aSLionel Sambuc   }
855f4a2713aSLionel Sambuc };
856f4a2713aSLionel Sambuc } // end anonymous namespace
857f4a2713aSLionel Sambuc 
858f4a2713aSLionel Sambuc 
859f4a2713aSLionel Sambuc /// Emits a reference to a dummy variable which is emitted with each class.
860f4a2713aSLionel Sambuc /// This ensures that a linker error will be generated when trying to link
861f4a2713aSLionel Sambuc /// together modules where a referenced class is not defined.
EmitClassRef(const std::string & className)862f4a2713aSLionel Sambuc void CGObjCGNU::EmitClassRef(const std::string &className) {
863f4a2713aSLionel Sambuc   std::string symbolRef = "__objc_class_ref_" + className;
864f4a2713aSLionel Sambuc   // Don't emit two copies of the same symbol
865f4a2713aSLionel Sambuc   if (TheModule.getGlobalVariable(symbolRef))
866f4a2713aSLionel Sambuc     return;
867f4a2713aSLionel Sambuc   std::string symbolName = "__objc_class_name_" + className;
868f4a2713aSLionel Sambuc   llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
869f4a2713aSLionel Sambuc   if (!ClassSymbol) {
870f4a2713aSLionel Sambuc     ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
871*0a6a1f1dSLionel Sambuc                                            llvm::GlobalValue::ExternalLinkage,
872*0a6a1f1dSLionel Sambuc                                            nullptr, symbolName);
873f4a2713aSLionel Sambuc   }
874f4a2713aSLionel Sambuc   new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
875f4a2713aSLionel Sambuc     llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
876f4a2713aSLionel Sambuc }
877f4a2713aSLionel Sambuc 
SymbolNameForMethod(StringRef ClassName,StringRef CategoryName,const Selector MethodName,bool isClassMethod)878*0a6a1f1dSLionel Sambuc static std::string SymbolNameForMethod( StringRef ClassName,
879*0a6a1f1dSLionel Sambuc      StringRef CategoryName, const Selector MethodName,
880f4a2713aSLionel Sambuc     bool isClassMethod) {
881f4a2713aSLionel Sambuc   std::string MethodNameColonStripped = MethodName.getAsString();
882f4a2713aSLionel Sambuc   std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(),
883f4a2713aSLionel Sambuc       ':', '_');
884f4a2713aSLionel Sambuc   return (Twine(isClassMethod ? "_c_" : "_i_") + ClassName + "_" +
885f4a2713aSLionel Sambuc     CategoryName + "_" + MethodNameColonStripped).str();
886f4a2713aSLionel Sambuc }
887f4a2713aSLionel Sambuc 
CGObjCGNU(CodeGenModule & cgm,unsigned runtimeABIVersion,unsigned protocolClassVersion)888f4a2713aSLionel Sambuc CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
889f4a2713aSLionel Sambuc                      unsigned protocolClassVersion)
890f4a2713aSLionel Sambuc   : CGObjCRuntime(cgm), TheModule(CGM.getModule()),
891*0a6a1f1dSLionel Sambuc     VMContext(cgm.getLLVMContext()), ClassPtrAlias(nullptr),
892*0a6a1f1dSLionel Sambuc     MetaClassPtrAlias(nullptr), RuntimeVersion(runtimeABIVersion),
893*0a6a1f1dSLionel Sambuc     ProtocolVersion(protocolClassVersion) {
894f4a2713aSLionel Sambuc 
895f4a2713aSLionel Sambuc   msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend");
896f4a2713aSLionel Sambuc 
897f4a2713aSLionel Sambuc   CodeGenTypes &Types = CGM.getTypes();
898f4a2713aSLionel Sambuc   IntTy = cast<llvm::IntegerType>(
899f4a2713aSLionel Sambuc       Types.ConvertType(CGM.getContext().IntTy));
900f4a2713aSLionel Sambuc   LongTy = cast<llvm::IntegerType>(
901f4a2713aSLionel Sambuc       Types.ConvertType(CGM.getContext().LongTy));
902f4a2713aSLionel Sambuc   SizeTy = cast<llvm::IntegerType>(
903f4a2713aSLionel Sambuc       Types.ConvertType(CGM.getContext().getSizeType()));
904f4a2713aSLionel Sambuc   PtrDiffTy = cast<llvm::IntegerType>(
905f4a2713aSLionel Sambuc       Types.ConvertType(CGM.getContext().getPointerDiffType()));
906f4a2713aSLionel Sambuc   BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
907f4a2713aSLionel Sambuc 
908f4a2713aSLionel Sambuc   Int8Ty = llvm::Type::getInt8Ty(VMContext);
909f4a2713aSLionel Sambuc   // C string type.  Used in lots of places.
910f4a2713aSLionel Sambuc   PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
911f4a2713aSLionel Sambuc 
912f4a2713aSLionel Sambuc   Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
913f4a2713aSLionel Sambuc   Zeros[1] = Zeros[0];
914f4a2713aSLionel Sambuc   NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
915f4a2713aSLionel Sambuc   // Get the selector Type.
916f4a2713aSLionel Sambuc   QualType selTy = CGM.getContext().getObjCSelType();
917f4a2713aSLionel Sambuc   if (QualType() == selTy) {
918f4a2713aSLionel Sambuc     SelectorTy = PtrToInt8Ty;
919f4a2713aSLionel Sambuc   } else {
920f4a2713aSLionel Sambuc     SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy));
921f4a2713aSLionel Sambuc   }
922f4a2713aSLionel Sambuc 
923f4a2713aSLionel Sambuc   PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
924f4a2713aSLionel Sambuc   PtrTy = PtrToInt8Ty;
925f4a2713aSLionel Sambuc 
926f4a2713aSLionel Sambuc   Int32Ty = llvm::Type::getInt32Ty(VMContext);
927f4a2713aSLionel Sambuc   Int64Ty = llvm::Type::getInt64Ty(VMContext);
928f4a2713aSLionel Sambuc 
929f4a2713aSLionel Sambuc   IntPtrTy =
930*0a6a1f1dSLionel Sambuc       CGM.getDataLayout().getPointerSizeInBits() == 32 ? Int32Ty : Int64Ty;
931f4a2713aSLionel Sambuc 
932f4a2713aSLionel Sambuc   // Object type
933f4a2713aSLionel Sambuc   QualType UnqualIdTy = CGM.getContext().getObjCIdType();
934f4a2713aSLionel Sambuc   ASTIdTy = CanQualType();
935f4a2713aSLionel Sambuc   if (UnqualIdTy != QualType()) {
936f4a2713aSLionel Sambuc     ASTIdTy = CGM.getContext().getCanonicalType(UnqualIdTy);
937f4a2713aSLionel Sambuc     IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
938f4a2713aSLionel Sambuc   } else {
939f4a2713aSLionel Sambuc     IdTy = PtrToInt8Ty;
940f4a2713aSLionel Sambuc   }
941f4a2713aSLionel Sambuc   PtrToIdTy = llvm::PointerType::getUnqual(IdTy);
942f4a2713aSLionel Sambuc 
943*0a6a1f1dSLionel Sambuc   ObjCSuperTy = llvm::StructType::get(IdTy, IdTy, nullptr);
944f4a2713aSLionel Sambuc   PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy);
945f4a2713aSLionel Sambuc 
946f4a2713aSLionel Sambuc   llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
947f4a2713aSLionel Sambuc 
948f4a2713aSLionel Sambuc   // void objc_exception_throw(id);
949*0a6a1f1dSLionel Sambuc   ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, nullptr);
950*0a6a1f1dSLionel Sambuc   ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, nullptr);
951f4a2713aSLionel Sambuc   // int objc_sync_enter(id);
952*0a6a1f1dSLionel Sambuc   SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy, nullptr);
953f4a2713aSLionel Sambuc   // int objc_sync_exit(id);
954*0a6a1f1dSLionel Sambuc   SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy, nullptr);
955f4a2713aSLionel Sambuc 
956f4a2713aSLionel Sambuc   // void objc_enumerationMutation (id)
957f4a2713aSLionel Sambuc   EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy,
958*0a6a1f1dSLionel Sambuc       IdTy, nullptr);
959f4a2713aSLionel Sambuc 
960f4a2713aSLionel Sambuc   // id objc_getProperty(id, SEL, ptrdiff_t, BOOL)
961f4a2713aSLionel Sambuc   GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy,
962*0a6a1f1dSLionel Sambuc       PtrDiffTy, BoolTy, nullptr);
963f4a2713aSLionel Sambuc   // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL)
964f4a2713aSLionel Sambuc   SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy,
965*0a6a1f1dSLionel Sambuc       PtrDiffTy, IdTy, BoolTy, BoolTy, nullptr);
966f4a2713aSLionel Sambuc   // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
967f4a2713aSLionel Sambuc   GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy,
968*0a6a1f1dSLionel Sambuc       PtrDiffTy, BoolTy, BoolTy, nullptr);
969f4a2713aSLionel Sambuc   // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
970f4a2713aSLionel Sambuc   SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy,
971*0a6a1f1dSLionel Sambuc       PtrDiffTy, BoolTy, BoolTy, nullptr);
972f4a2713aSLionel Sambuc 
973f4a2713aSLionel Sambuc   // IMP type
974f4a2713aSLionel Sambuc   llvm::Type *IMPArgs[] = { IdTy, SelectorTy };
975f4a2713aSLionel Sambuc   IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs,
976f4a2713aSLionel Sambuc               true));
977f4a2713aSLionel Sambuc 
978f4a2713aSLionel Sambuc   const LangOptions &Opts = CGM.getLangOpts();
979f4a2713aSLionel Sambuc   if ((Opts.getGC() != LangOptions::NonGC) || Opts.ObjCAutoRefCount)
980f4a2713aSLionel Sambuc     RuntimeVersion = 10;
981f4a2713aSLionel Sambuc 
982f4a2713aSLionel Sambuc   // Don't bother initialising the GC stuff unless we're compiling in GC mode
983f4a2713aSLionel Sambuc   if (Opts.getGC() != LangOptions::NonGC) {
984f4a2713aSLionel Sambuc     // This is a bit of an hack.  We should sort this out by having a proper
985f4a2713aSLionel Sambuc     // CGObjCGNUstep subclass for GC, but we may want to really support the old
986f4a2713aSLionel Sambuc     // ABI and GC added in ObjectiveC2.framework, so we fudge it a bit for now
987f4a2713aSLionel Sambuc     // Get selectors needed in GC mode
988f4a2713aSLionel Sambuc     RetainSel = GetNullarySelector("retain", CGM.getContext());
989f4a2713aSLionel Sambuc     ReleaseSel = GetNullarySelector("release", CGM.getContext());
990f4a2713aSLionel Sambuc     AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext());
991f4a2713aSLionel Sambuc 
992f4a2713aSLionel Sambuc     // Get functions needed in GC mode
993f4a2713aSLionel Sambuc 
994f4a2713aSLionel Sambuc     // id objc_assign_ivar(id, id, ptrdiff_t);
995f4a2713aSLionel Sambuc     IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy,
996*0a6a1f1dSLionel Sambuc         nullptr);
997f4a2713aSLionel Sambuc     // id objc_assign_strongCast (id, id*)
998f4a2713aSLionel Sambuc     StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy,
999*0a6a1f1dSLionel Sambuc         PtrToIdTy, nullptr);
1000f4a2713aSLionel Sambuc     // id objc_assign_global(id, id*);
1001f4a2713aSLionel Sambuc     GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy,
1002*0a6a1f1dSLionel Sambuc         nullptr);
1003f4a2713aSLionel Sambuc     // id objc_assign_weak(id, id*);
1004*0a6a1f1dSLionel Sambuc     WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy, nullptr);
1005f4a2713aSLionel Sambuc     // id objc_read_weak(id*);
1006*0a6a1f1dSLionel Sambuc     WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy, nullptr);
1007f4a2713aSLionel Sambuc     // void *objc_memmove_collectable(void*, void *, size_t);
1008f4a2713aSLionel Sambuc     MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy,
1009*0a6a1f1dSLionel Sambuc         SizeTy, nullptr);
1010f4a2713aSLionel Sambuc   }
1011f4a2713aSLionel Sambuc }
1012f4a2713aSLionel Sambuc 
GetClassNamed(CodeGenFunction & CGF,const std::string & Name,bool isWeak)1013f4a2713aSLionel Sambuc llvm::Value *CGObjCGNU::GetClassNamed(CodeGenFunction &CGF,
1014f4a2713aSLionel Sambuc                                       const std::string &Name,
1015f4a2713aSLionel Sambuc                                       bool isWeak) {
1016f4a2713aSLionel Sambuc   llvm::Value *ClassName = CGM.GetAddrOfConstantCString(Name);
1017f4a2713aSLionel Sambuc   // With the incompatible ABI, this will need to be replaced with a direct
1018f4a2713aSLionel Sambuc   // reference to the class symbol.  For the compatible nonfragile ABI we are
1019f4a2713aSLionel Sambuc   // still performing this lookup at run time but emitting the symbol for the
1020f4a2713aSLionel Sambuc   // class externally so that we can make the switch later.
1021f4a2713aSLionel Sambuc   //
1022f4a2713aSLionel Sambuc   // Libobjc2 contains an LLVM pass that replaces calls to objc_lookup_class
1023f4a2713aSLionel Sambuc   // with memoized versions or with static references if it's safe to do so.
1024f4a2713aSLionel Sambuc   if (!isWeak)
1025f4a2713aSLionel Sambuc     EmitClassRef(Name);
1026f4a2713aSLionel Sambuc   ClassName = CGF.Builder.CreateStructGEP(ClassName, 0);
1027f4a2713aSLionel Sambuc 
1028f4a2713aSLionel Sambuc   llvm::Constant *ClassLookupFn =
1029f4a2713aSLionel Sambuc     CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, PtrToInt8Ty, true),
1030f4a2713aSLionel Sambuc                               "objc_lookup_class");
1031f4a2713aSLionel Sambuc   return CGF.EmitNounwindRuntimeCall(ClassLookupFn, ClassName);
1032f4a2713aSLionel Sambuc }
1033f4a2713aSLionel Sambuc 
1034f4a2713aSLionel Sambuc // This has to perform the lookup every time, since posing and related
1035f4a2713aSLionel Sambuc // techniques can modify the name -> class mapping.
GetClass(CodeGenFunction & CGF,const ObjCInterfaceDecl * OID)1036f4a2713aSLionel Sambuc llvm::Value *CGObjCGNU::GetClass(CodeGenFunction &CGF,
1037f4a2713aSLionel Sambuc                                  const ObjCInterfaceDecl *OID) {
1038f4a2713aSLionel Sambuc   return GetClassNamed(CGF, OID->getNameAsString(), OID->isWeakImported());
1039f4a2713aSLionel Sambuc }
EmitNSAutoreleasePoolClassRef(CodeGenFunction & CGF)1040f4a2713aSLionel Sambuc llvm::Value *CGObjCGNU::EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
1041f4a2713aSLionel Sambuc   return GetClassNamed(CGF, "NSAutoreleasePool", false);
1042f4a2713aSLionel Sambuc }
1043f4a2713aSLionel Sambuc 
GetSelector(CodeGenFunction & CGF,Selector Sel,const std::string & TypeEncoding,bool lval)1044f4a2713aSLionel Sambuc llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel,
1045f4a2713aSLionel Sambuc     const std::string &TypeEncoding, bool lval) {
1046f4a2713aSLionel Sambuc 
1047f4a2713aSLionel Sambuc   SmallVectorImpl<TypedSelector> &Types = SelectorTable[Sel];
1048*0a6a1f1dSLionel Sambuc   llvm::GlobalAlias *SelValue = nullptr;
1049f4a2713aSLionel Sambuc 
1050f4a2713aSLionel Sambuc   for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
1051f4a2713aSLionel Sambuc       e = Types.end() ; i!=e ; i++) {
1052f4a2713aSLionel Sambuc     if (i->first == TypeEncoding) {
1053f4a2713aSLionel Sambuc       SelValue = i->second;
1054f4a2713aSLionel Sambuc       break;
1055f4a2713aSLionel Sambuc     }
1056f4a2713aSLionel Sambuc   }
1057*0a6a1f1dSLionel Sambuc   if (!SelValue) {
1058*0a6a1f1dSLionel Sambuc     SelValue = llvm::GlobalAlias::create(
1059*0a6a1f1dSLionel Sambuc         SelectorTy->getElementType(), 0, llvm::GlobalValue::PrivateLinkage,
1060*0a6a1f1dSLionel Sambuc         ".objc_selector_" + Sel.getAsString(), &TheModule);
1061f4a2713aSLionel Sambuc     Types.push_back(TypedSelector(TypeEncoding, SelValue));
1062f4a2713aSLionel Sambuc   }
1063f4a2713aSLionel Sambuc 
1064f4a2713aSLionel Sambuc   if (lval) {
1065f4a2713aSLionel Sambuc     llvm::Value *tmp = CGF.CreateTempAlloca(SelValue->getType());
1066f4a2713aSLionel Sambuc     CGF.Builder.CreateStore(SelValue, tmp);
1067f4a2713aSLionel Sambuc     return tmp;
1068f4a2713aSLionel Sambuc   }
1069f4a2713aSLionel Sambuc   return SelValue;
1070f4a2713aSLionel Sambuc }
1071f4a2713aSLionel Sambuc 
GetSelector(CodeGenFunction & CGF,Selector Sel,bool lval)1072f4a2713aSLionel Sambuc llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel,
1073f4a2713aSLionel Sambuc                                     bool lval) {
1074f4a2713aSLionel Sambuc   return GetSelector(CGF, Sel, std::string(), lval);
1075f4a2713aSLionel Sambuc }
1076f4a2713aSLionel Sambuc 
GetSelector(CodeGenFunction & CGF,const ObjCMethodDecl * Method)1077f4a2713aSLionel Sambuc llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF,
1078f4a2713aSLionel Sambuc                                     const ObjCMethodDecl *Method) {
1079f4a2713aSLionel Sambuc   std::string SelTypes;
1080f4a2713aSLionel Sambuc   CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
1081f4a2713aSLionel Sambuc   return GetSelector(CGF, Method->getSelector(), SelTypes, false);
1082f4a2713aSLionel Sambuc }
1083f4a2713aSLionel Sambuc 
GetEHType(QualType T)1084f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GetEHType(QualType T) {
1085f4a2713aSLionel Sambuc   if (T->isObjCIdType() || T->isObjCQualifiedIdType()) {
1086f4a2713aSLionel Sambuc     // With the old ABI, there was only one kind of catchall, which broke
1087f4a2713aSLionel Sambuc     // foreign exceptions.  With the new ABI, we use __objc_id_typeinfo as
1088f4a2713aSLionel Sambuc     // a pointer indicating object catchalls, and NULL to indicate real
1089f4a2713aSLionel Sambuc     // catchalls
1090f4a2713aSLionel Sambuc     if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1091f4a2713aSLionel Sambuc       return MakeConstantString("@id");
1092f4a2713aSLionel Sambuc     } else {
1093*0a6a1f1dSLionel Sambuc       return nullptr;
1094f4a2713aSLionel Sambuc     }
1095f4a2713aSLionel Sambuc   }
1096f4a2713aSLionel Sambuc 
1097f4a2713aSLionel Sambuc   // All other types should be Objective-C interface pointer types.
1098f4a2713aSLionel Sambuc   const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>();
1099f4a2713aSLionel Sambuc   assert(OPT && "Invalid @catch type.");
1100f4a2713aSLionel Sambuc   const ObjCInterfaceDecl *IDecl = OPT->getObjectType()->getInterface();
1101f4a2713aSLionel Sambuc   assert(IDecl && "Invalid @catch type.");
1102f4a2713aSLionel Sambuc   return MakeConstantString(IDecl->getIdentifier()->getName());
1103f4a2713aSLionel Sambuc }
1104f4a2713aSLionel Sambuc 
GetEHType(QualType T)1105f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
1106f4a2713aSLionel Sambuc   if (!CGM.getLangOpts().CPlusPlus)
1107f4a2713aSLionel Sambuc     return CGObjCGNU::GetEHType(T);
1108f4a2713aSLionel Sambuc 
1109f4a2713aSLionel Sambuc   // For Objective-C++, we want to provide the ability to catch both C++ and
1110f4a2713aSLionel Sambuc   // Objective-C objects in the same function.
1111f4a2713aSLionel Sambuc 
1112f4a2713aSLionel Sambuc   // There's a particular fixed type info for 'id'.
1113f4a2713aSLionel Sambuc   if (T->isObjCIdType() ||
1114f4a2713aSLionel Sambuc       T->isObjCQualifiedIdType()) {
1115f4a2713aSLionel Sambuc     llvm::Constant *IDEHType =
1116f4a2713aSLionel Sambuc       CGM.getModule().getGlobalVariable("__objc_id_type_info");
1117f4a2713aSLionel Sambuc     if (!IDEHType)
1118f4a2713aSLionel Sambuc       IDEHType =
1119f4a2713aSLionel Sambuc         new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty,
1120f4a2713aSLionel Sambuc                                  false,
1121f4a2713aSLionel Sambuc                                  llvm::GlobalValue::ExternalLinkage,
1122*0a6a1f1dSLionel Sambuc                                  nullptr, "__objc_id_type_info");
1123f4a2713aSLionel Sambuc     return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty);
1124f4a2713aSLionel Sambuc   }
1125f4a2713aSLionel Sambuc 
1126f4a2713aSLionel Sambuc   const ObjCObjectPointerType *PT =
1127f4a2713aSLionel Sambuc     T->getAs<ObjCObjectPointerType>();
1128f4a2713aSLionel Sambuc   assert(PT && "Invalid @catch type.");
1129f4a2713aSLionel Sambuc   const ObjCInterfaceType *IT = PT->getInterfaceType();
1130f4a2713aSLionel Sambuc   assert(IT && "Invalid @catch type.");
1131f4a2713aSLionel Sambuc   std::string className = IT->getDecl()->getIdentifier()->getName();
1132f4a2713aSLionel Sambuc 
1133f4a2713aSLionel Sambuc   std::string typeinfoName = "__objc_eh_typeinfo_" + className;
1134f4a2713aSLionel Sambuc 
1135f4a2713aSLionel Sambuc   // Return the existing typeinfo if it exists
1136f4a2713aSLionel Sambuc   llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName);
1137f4a2713aSLionel Sambuc   if (typeinfo)
1138f4a2713aSLionel Sambuc     return llvm::ConstantExpr::getBitCast(typeinfo, PtrToInt8Ty);
1139f4a2713aSLionel Sambuc 
1140f4a2713aSLionel Sambuc   // Otherwise create it.
1141f4a2713aSLionel Sambuc 
1142f4a2713aSLionel Sambuc   // vtable for gnustep::libobjc::__objc_class_type_info
1143f4a2713aSLionel Sambuc   // It's quite ugly hard-coding this.  Ideally we'd generate it using the host
1144f4a2713aSLionel Sambuc   // platform's name mangling.
1145f4a2713aSLionel Sambuc   const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE";
1146f4a2713aSLionel Sambuc   llvm::Constant *Vtable = TheModule.getGlobalVariable(vtableName);
1147f4a2713aSLionel Sambuc   if (!Vtable) {
1148f4a2713aSLionel Sambuc     Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true,
1149*0a6a1f1dSLionel Sambuc                                       llvm::GlobalValue::ExternalLinkage,
1150*0a6a1f1dSLionel Sambuc                                       nullptr, vtableName);
1151f4a2713aSLionel Sambuc   }
1152f4a2713aSLionel Sambuc   llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2);
1153f4a2713aSLionel Sambuc   Vtable = llvm::ConstantExpr::getGetElementPtr(Vtable, Two);
1154f4a2713aSLionel Sambuc   Vtable = llvm::ConstantExpr::getBitCast(Vtable, PtrToInt8Ty);
1155f4a2713aSLionel Sambuc 
1156f4a2713aSLionel Sambuc   llvm::Constant *typeName =
1157f4a2713aSLionel Sambuc     ExportUniqueString(className, "__objc_eh_typename_");
1158f4a2713aSLionel Sambuc 
1159f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> fields;
1160f4a2713aSLionel Sambuc   fields.push_back(Vtable);
1161f4a2713aSLionel Sambuc   fields.push_back(typeName);
1162f4a2713aSLionel Sambuc   llvm::Constant *TI =
1163f4a2713aSLionel Sambuc       MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
1164*0a6a1f1dSLionel Sambuc               nullptr), fields, "__objc_eh_typeinfo_" + className,
1165f4a2713aSLionel Sambuc           llvm::GlobalValue::LinkOnceODRLinkage);
1166f4a2713aSLionel Sambuc   return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty);
1167f4a2713aSLionel Sambuc }
1168f4a2713aSLionel Sambuc 
1169f4a2713aSLionel Sambuc /// Generate an NSConstantString object.
GenerateConstantString(const StringLiteral * SL)1170f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
1171f4a2713aSLionel Sambuc 
1172f4a2713aSLionel Sambuc   std::string Str = SL->getString().str();
1173f4a2713aSLionel Sambuc 
1174f4a2713aSLionel Sambuc   // Look for an existing one
1175f4a2713aSLionel Sambuc   llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
1176f4a2713aSLionel Sambuc   if (old != ObjCStrings.end())
1177f4a2713aSLionel Sambuc     return old->getValue();
1178f4a2713aSLionel Sambuc 
1179f4a2713aSLionel Sambuc   StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
1180f4a2713aSLionel Sambuc 
1181f4a2713aSLionel Sambuc   if (StringClass.empty()) StringClass = "NXConstantString";
1182f4a2713aSLionel Sambuc 
1183f4a2713aSLionel Sambuc   std::string Sym = "_OBJC_CLASS_";
1184f4a2713aSLionel Sambuc   Sym += StringClass;
1185f4a2713aSLionel Sambuc 
1186f4a2713aSLionel Sambuc   llvm::Constant *isa = TheModule.getNamedGlobal(Sym);
1187f4a2713aSLionel Sambuc 
1188f4a2713aSLionel Sambuc   if (!isa)
1189f4a2713aSLionel Sambuc     isa = new llvm::GlobalVariable(TheModule, IdTy, /* isConstant */false,
1190*0a6a1f1dSLionel Sambuc             llvm::GlobalValue::ExternalWeakLinkage, nullptr, Sym);
1191f4a2713aSLionel Sambuc   else if (isa->getType() != PtrToIdTy)
1192f4a2713aSLionel Sambuc     isa = llvm::ConstantExpr::getBitCast(isa, PtrToIdTy);
1193f4a2713aSLionel Sambuc 
1194f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Ivars;
1195f4a2713aSLionel Sambuc   Ivars.push_back(isa);
1196f4a2713aSLionel Sambuc   Ivars.push_back(MakeConstantString(Str));
1197f4a2713aSLionel Sambuc   Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
1198f4a2713aSLionel Sambuc   llvm::Constant *ObjCStr = MakeGlobal(
1199*0a6a1f1dSLionel Sambuc     llvm::StructType::get(PtrToIdTy, PtrToInt8Ty, IntTy, nullptr),
1200f4a2713aSLionel Sambuc     Ivars, ".objc_str");
1201f4a2713aSLionel Sambuc   ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
1202f4a2713aSLionel Sambuc   ObjCStrings[Str] = ObjCStr;
1203f4a2713aSLionel Sambuc   ConstantStrings.push_back(ObjCStr);
1204f4a2713aSLionel Sambuc   return ObjCStr;
1205f4a2713aSLionel Sambuc }
1206f4a2713aSLionel Sambuc 
1207f4a2713aSLionel Sambuc ///Generates a message send where the super is the receiver.  This is a message
1208f4a2713aSLionel Sambuc ///send to self with special delivery semantics indicating which class's method
1209f4a2713aSLionel Sambuc ///should be called.
1210f4a2713aSLionel Sambuc RValue
GenerateMessageSendSuper(CodeGenFunction & CGF,ReturnValueSlot Return,QualType ResultType,Selector Sel,const ObjCInterfaceDecl * Class,bool isCategoryImpl,llvm::Value * Receiver,bool IsClassMessage,const CallArgList & CallArgs,const ObjCMethodDecl * Method)1211f4a2713aSLionel Sambuc CGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF,
1212f4a2713aSLionel Sambuc                                     ReturnValueSlot Return,
1213f4a2713aSLionel Sambuc                                     QualType ResultType,
1214f4a2713aSLionel Sambuc                                     Selector Sel,
1215f4a2713aSLionel Sambuc                                     const ObjCInterfaceDecl *Class,
1216f4a2713aSLionel Sambuc                                     bool isCategoryImpl,
1217f4a2713aSLionel Sambuc                                     llvm::Value *Receiver,
1218f4a2713aSLionel Sambuc                                     bool IsClassMessage,
1219f4a2713aSLionel Sambuc                                     const CallArgList &CallArgs,
1220f4a2713aSLionel Sambuc                                     const ObjCMethodDecl *Method) {
1221f4a2713aSLionel Sambuc   CGBuilderTy &Builder = CGF.Builder;
1222f4a2713aSLionel Sambuc   if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
1223f4a2713aSLionel Sambuc     if (Sel == RetainSel || Sel == AutoreleaseSel) {
1224f4a2713aSLionel Sambuc       return RValue::get(EnforceType(Builder, Receiver,
1225f4a2713aSLionel Sambuc                   CGM.getTypes().ConvertType(ResultType)));
1226f4a2713aSLionel Sambuc     }
1227f4a2713aSLionel Sambuc     if (Sel == ReleaseSel) {
1228*0a6a1f1dSLionel Sambuc       return RValue::get(nullptr);
1229f4a2713aSLionel Sambuc     }
1230f4a2713aSLionel Sambuc   }
1231f4a2713aSLionel Sambuc 
1232f4a2713aSLionel Sambuc   llvm::Value *cmd = GetSelector(CGF, Sel);
1233f4a2713aSLionel Sambuc 
1234f4a2713aSLionel Sambuc 
1235f4a2713aSLionel Sambuc   CallArgList ActualArgs;
1236f4a2713aSLionel Sambuc 
1237f4a2713aSLionel Sambuc   ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy);
1238f4a2713aSLionel Sambuc   ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
1239f4a2713aSLionel Sambuc   ActualArgs.addFrom(CallArgs);
1240f4a2713aSLionel Sambuc 
1241f4a2713aSLionel Sambuc   MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
1242f4a2713aSLionel Sambuc 
1243*0a6a1f1dSLionel Sambuc   llvm::Value *ReceiverClass = nullptr;
1244f4a2713aSLionel Sambuc   if (isCategoryImpl) {
1245*0a6a1f1dSLionel Sambuc     llvm::Constant *classLookupFunction = nullptr;
1246f4a2713aSLionel Sambuc     if (IsClassMessage)  {
1247f4a2713aSLionel Sambuc       classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
1248f4a2713aSLionel Sambuc             IdTy, PtrTy, true), "objc_get_meta_class");
1249f4a2713aSLionel Sambuc     } else {
1250f4a2713aSLionel Sambuc       classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
1251f4a2713aSLionel Sambuc             IdTy, PtrTy, true), "objc_get_class");
1252f4a2713aSLionel Sambuc     }
1253f4a2713aSLionel Sambuc     ReceiverClass = Builder.CreateCall(classLookupFunction,
1254f4a2713aSLionel Sambuc         MakeConstantString(Class->getNameAsString()));
1255f4a2713aSLionel Sambuc   } else {
1256f4a2713aSLionel Sambuc     // Set up global aliases for the metaclass or class pointer if they do not
1257f4a2713aSLionel Sambuc     // already exist.  These will are forward-references which will be set to
1258f4a2713aSLionel Sambuc     // pointers to the class and metaclass structure created for the runtime
1259f4a2713aSLionel Sambuc     // load function.  To send a message to super, we look up the value of the
1260f4a2713aSLionel Sambuc     // super_class pointer from either the class or metaclass structure.
1261f4a2713aSLionel Sambuc     if (IsClassMessage)  {
1262f4a2713aSLionel Sambuc       if (!MetaClassPtrAlias) {
1263*0a6a1f1dSLionel Sambuc         MetaClassPtrAlias = llvm::GlobalAlias::create(
1264*0a6a1f1dSLionel Sambuc             IdTy->getElementType(), 0, llvm::GlobalValue::InternalLinkage,
1265*0a6a1f1dSLionel Sambuc             ".objc_metaclass_ref" + Class->getNameAsString(), &TheModule);
1266f4a2713aSLionel Sambuc       }
1267f4a2713aSLionel Sambuc       ReceiverClass = MetaClassPtrAlias;
1268f4a2713aSLionel Sambuc     } else {
1269f4a2713aSLionel Sambuc       if (!ClassPtrAlias) {
1270*0a6a1f1dSLionel Sambuc         ClassPtrAlias = llvm::GlobalAlias::create(
1271*0a6a1f1dSLionel Sambuc             IdTy->getElementType(), 0, llvm::GlobalValue::InternalLinkage,
1272*0a6a1f1dSLionel Sambuc             ".objc_class_ref" + Class->getNameAsString(), &TheModule);
1273f4a2713aSLionel Sambuc       }
1274f4a2713aSLionel Sambuc       ReceiverClass = ClassPtrAlias;
1275f4a2713aSLionel Sambuc     }
1276f4a2713aSLionel Sambuc   }
1277f4a2713aSLionel Sambuc   // Cast the pointer to a simplified version of the class structure
1278f4a2713aSLionel Sambuc   ReceiverClass = Builder.CreateBitCast(ReceiverClass,
1279f4a2713aSLionel Sambuc       llvm::PointerType::getUnqual(
1280*0a6a1f1dSLionel Sambuc         llvm::StructType::get(IdTy, IdTy, nullptr)));
1281f4a2713aSLionel Sambuc   // Get the superclass pointer
1282f4a2713aSLionel Sambuc   ReceiverClass = Builder.CreateStructGEP(ReceiverClass, 1);
1283f4a2713aSLionel Sambuc   // Load the superclass pointer
1284f4a2713aSLionel Sambuc   ReceiverClass = Builder.CreateLoad(ReceiverClass);
1285f4a2713aSLionel Sambuc   // Construct the structure used to look up the IMP
1286f4a2713aSLionel Sambuc   llvm::StructType *ObjCSuperTy = llvm::StructType::get(
1287*0a6a1f1dSLionel Sambuc       Receiver->getType(), IdTy, nullptr);
1288f4a2713aSLionel Sambuc   llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy);
1289f4a2713aSLionel Sambuc 
1290f4a2713aSLionel Sambuc   Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
1291f4a2713aSLionel Sambuc   Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
1292f4a2713aSLionel Sambuc 
1293f4a2713aSLionel Sambuc   ObjCSuper = EnforceType(Builder, ObjCSuper, PtrToObjCSuperTy);
1294f4a2713aSLionel Sambuc 
1295f4a2713aSLionel Sambuc   // Get the IMP
1296f4a2713aSLionel Sambuc   llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd, MSI);
1297f4a2713aSLionel Sambuc   imp = EnforceType(Builder, imp, MSI.MessengerType);
1298f4a2713aSLionel Sambuc 
1299*0a6a1f1dSLionel Sambuc   llvm::Metadata *impMD[] = {
1300f4a2713aSLionel Sambuc       llvm::MDString::get(VMContext, Sel.getAsString()),
1301f4a2713aSLionel Sambuc       llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()),
1302*0a6a1f1dSLionel Sambuc       llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1303*0a6a1f1dSLionel Sambuc           llvm::Type::getInt1Ty(VMContext), IsClassMessage))};
1304f4a2713aSLionel Sambuc   llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
1305f4a2713aSLionel Sambuc 
1306f4a2713aSLionel Sambuc   llvm::Instruction *call;
1307*0a6a1f1dSLionel Sambuc   RValue msgRet = CGF.EmitCall(MSI.CallInfo, imp, Return, ActualArgs, nullptr,
1308*0a6a1f1dSLionel Sambuc                                &call);
1309f4a2713aSLionel Sambuc   call->setMetadata(msgSendMDKind, node);
1310f4a2713aSLionel Sambuc   return msgRet;
1311f4a2713aSLionel Sambuc }
1312f4a2713aSLionel Sambuc 
1313f4a2713aSLionel Sambuc /// Generate code for a message send expression.
1314f4a2713aSLionel Sambuc RValue
GenerateMessageSend(CodeGenFunction & CGF,ReturnValueSlot Return,QualType ResultType,Selector Sel,llvm::Value * Receiver,const CallArgList & CallArgs,const ObjCInterfaceDecl * Class,const ObjCMethodDecl * Method)1315f4a2713aSLionel Sambuc CGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF,
1316f4a2713aSLionel Sambuc                                ReturnValueSlot Return,
1317f4a2713aSLionel Sambuc                                QualType ResultType,
1318f4a2713aSLionel Sambuc                                Selector Sel,
1319f4a2713aSLionel Sambuc                                llvm::Value *Receiver,
1320f4a2713aSLionel Sambuc                                const CallArgList &CallArgs,
1321f4a2713aSLionel Sambuc                                const ObjCInterfaceDecl *Class,
1322f4a2713aSLionel Sambuc                                const ObjCMethodDecl *Method) {
1323f4a2713aSLionel Sambuc   CGBuilderTy &Builder = CGF.Builder;
1324f4a2713aSLionel Sambuc 
1325f4a2713aSLionel Sambuc   // Strip out message sends to retain / release in GC mode
1326f4a2713aSLionel Sambuc   if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
1327f4a2713aSLionel Sambuc     if (Sel == RetainSel || Sel == AutoreleaseSel) {
1328f4a2713aSLionel Sambuc       return RValue::get(EnforceType(Builder, Receiver,
1329f4a2713aSLionel Sambuc                   CGM.getTypes().ConvertType(ResultType)));
1330f4a2713aSLionel Sambuc     }
1331f4a2713aSLionel Sambuc     if (Sel == ReleaseSel) {
1332*0a6a1f1dSLionel Sambuc       return RValue::get(nullptr);
1333f4a2713aSLionel Sambuc     }
1334f4a2713aSLionel Sambuc   }
1335f4a2713aSLionel Sambuc 
1336f4a2713aSLionel Sambuc   // If the return type is something that goes in an integer register, the
1337f4a2713aSLionel Sambuc   // runtime will handle 0 returns.  For other cases, we fill in the 0 value
1338f4a2713aSLionel Sambuc   // ourselves.
1339f4a2713aSLionel Sambuc   //
1340f4a2713aSLionel Sambuc   // The language spec says the result of this kind of message send is
1341f4a2713aSLionel Sambuc   // undefined, but lots of people seem to have forgotten to read that
1342f4a2713aSLionel Sambuc   // paragraph and insist on sending messages to nil that have structure
1343f4a2713aSLionel Sambuc   // returns.  With GCC, this generates a random return value (whatever happens
1344f4a2713aSLionel Sambuc   // to be on the stack / in those registers at the time) on most platforms,
1345f4a2713aSLionel Sambuc   // and generates an illegal instruction trap on SPARC.  With LLVM it corrupts
1346f4a2713aSLionel Sambuc   // the stack.
1347f4a2713aSLionel Sambuc   bool isPointerSizedReturn = (ResultType->isAnyPointerType() ||
1348f4a2713aSLionel Sambuc       ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType());
1349f4a2713aSLionel Sambuc 
1350*0a6a1f1dSLionel Sambuc   llvm::BasicBlock *startBB = nullptr;
1351*0a6a1f1dSLionel Sambuc   llvm::BasicBlock *messageBB = nullptr;
1352*0a6a1f1dSLionel Sambuc   llvm::BasicBlock *continueBB = nullptr;
1353f4a2713aSLionel Sambuc 
1354f4a2713aSLionel Sambuc   if (!isPointerSizedReturn) {
1355f4a2713aSLionel Sambuc     startBB = Builder.GetInsertBlock();
1356f4a2713aSLionel Sambuc     messageBB = CGF.createBasicBlock("msgSend");
1357f4a2713aSLionel Sambuc     continueBB = CGF.createBasicBlock("continue");
1358f4a2713aSLionel Sambuc 
1359f4a2713aSLionel Sambuc     llvm::Value *isNil = Builder.CreateICmpEQ(Receiver,
1360f4a2713aSLionel Sambuc             llvm::Constant::getNullValue(Receiver->getType()));
1361f4a2713aSLionel Sambuc     Builder.CreateCondBr(isNil, continueBB, messageBB);
1362f4a2713aSLionel Sambuc     CGF.EmitBlock(messageBB);
1363f4a2713aSLionel Sambuc   }
1364f4a2713aSLionel Sambuc 
1365f4a2713aSLionel Sambuc   IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
1366f4a2713aSLionel Sambuc   llvm::Value *cmd;
1367f4a2713aSLionel Sambuc   if (Method)
1368f4a2713aSLionel Sambuc     cmd = GetSelector(CGF, Method);
1369f4a2713aSLionel Sambuc   else
1370f4a2713aSLionel Sambuc     cmd = GetSelector(CGF, Sel);
1371f4a2713aSLionel Sambuc   cmd = EnforceType(Builder, cmd, SelectorTy);
1372f4a2713aSLionel Sambuc   Receiver = EnforceType(Builder, Receiver, IdTy);
1373f4a2713aSLionel Sambuc 
1374*0a6a1f1dSLionel Sambuc   llvm::Metadata *impMD[] = {
1375f4a2713aSLionel Sambuc       llvm::MDString::get(VMContext, Sel.getAsString()),
1376f4a2713aSLionel Sambuc       llvm::MDString::get(VMContext, Class ? Class->getNameAsString() : ""),
1377*0a6a1f1dSLionel Sambuc       llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1378*0a6a1f1dSLionel Sambuc           llvm::Type::getInt1Ty(VMContext), Class != nullptr))};
1379f4a2713aSLionel Sambuc   llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
1380f4a2713aSLionel Sambuc 
1381f4a2713aSLionel Sambuc   CallArgList ActualArgs;
1382f4a2713aSLionel Sambuc   ActualArgs.add(RValue::get(Receiver), ASTIdTy);
1383f4a2713aSLionel Sambuc   ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
1384f4a2713aSLionel Sambuc   ActualArgs.addFrom(CallArgs);
1385f4a2713aSLionel Sambuc 
1386f4a2713aSLionel Sambuc   MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
1387f4a2713aSLionel Sambuc 
1388f4a2713aSLionel Sambuc   // Get the IMP to call
1389f4a2713aSLionel Sambuc   llvm::Value *imp;
1390f4a2713aSLionel Sambuc 
1391f4a2713aSLionel Sambuc   // If we have non-legacy dispatch specified, we try using the objc_msgSend()
1392f4a2713aSLionel Sambuc   // functions.  These are not supported on all platforms (or all runtimes on a
1393f4a2713aSLionel Sambuc   // given platform), so we
1394f4a2713aSLionel Sambuc   switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
1395f4a2713aSLionel Sambuc     case CodeGenOptions::Legacy:
1396f4a2713aSLionel Sambuc       imp = LookupIMP(CGF, Receiver, cmd, node, MSI);
1397f4a2713aSLionel Sambuc       break;
1398f4a2713aSLionel Sambuc     case CodeGenOptions::Mixed:
1399f4a2713aSLionel Sambuc     case CodeGenOptions::NonLegacy:
1400f4a2713aSLionel Sambuc       if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1401f4a2713aSLionel Sambuc         imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
1402f4a2713aSLionel Sambuc                                   "objc_msgSend_fpret");
1403f4a2713aSLionel Sambuc       } else if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
1404f4a2713aSLionel Sambuc         // The actual types here don't matter - we're going to bitcast the
1405f4a2713aSLionel Sambuc         // function anyway
1406f4a2713aSLionel Sambuc         imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
1407f4a2713aSLionel Sambuc                                   "objc_msgSend_stret");
1408f4a2713aSLionel Sambuc       } else {
1409f4a2713aSLionel Sambuc         imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
1410f4a2713aSLionel Sambuc                                   "objc_msgSend");
1411f4a2713aSLionel Sambuc       }
1412f4a2713aSLionel Sambuc   }
1413f4a2713aSLionel Sambuc 
1414f4a2713aSLionel Sambuc   // Reset the receiver in case the lookup modified it
1415f4a2713aSLionel Sambuc   ActualArgs[0] = CallArg(RValue::get(Receiver), ASTIdTy, false);
1416f4a2713aSLionel Sambuc 
1417f4a2713aSLionel Sambuc   imp = EnforceType(Builder, imp, MSI.MessengerType);
1418f4a2713aSLionel Sambuc 
1419f4a2713aSLionel Sambuc   llvm::Instruction *call;
1420*0a6a1f1dSLionel Sambuc   RValue msgRet = CGF.EmitCall(MSI.CallInfo, imp, Return, ActualArgs, nullptr,
1421*0a6a1f1dSLionel Sambuc                                &call);
1422f4a2713aSLionel Sambuc   call->setMetadata(msgSendMDKind, node);
1423f4a2713aSLionel Sambuc 
1424f4a2713aSLionel Sambuc 
1425f4a2713aSLionel Sambuc   if (!isPointerSizedReturn) {
1426f4a2713aSLionel Sambuc     messageBB = CGF.Builder.GetInsertBlock();
1427f4a2713aSLionel Sambuc     CGF.Builder.CreateBr(continueBB);
1428f4a2713aSLionel Sambuc     CGF.EmitBlock(continueBB);
1429f4a2713aSLionel Sambuc     if (msgRet.isScalar()) {
1430f4a2713aSLionel Sambuc       llvm::Value *v = msgRet.getScalarVal();
1431f4a2713aSLionel Sambuc       llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
1432f4a2713aSLionel Sambuc       phi->addIncoming(v, messageBB);
1433f4a2713aSLionel Sambuc       phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB);
1434f4a2713aSLionel Sambuc       msgRet = RValue::get(phi);
1435f4a2713aSLionel Sambuc     } else if (msgRet.isAggregate()) {
1436f4a2713aSLionel Sambuc       llvm::Value *v = msgRet.getAggregateAddr();
1437f4a2713aSLionel Sambuc       llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
1438f4a2713aSLionel Sambuc       llvm::PointerType *RetTy = cast<llvm::PointerType>(v->getType());
1439f4a2713aSLionel Sambuc       llvm::AllocaInst *NullVal =
1440f4a2713aSLionel Sambuc           CGF.CreateTempAlloca(RetTy->getElementType(), "null");
1441f4a2713aSLionel Sambuc       CGF.InitTempAlloca(NullVal,
1442f4a2713aSLionel Sambuc           llvm::Constant::getNullValue(RetTy->getElementType()));
1443f4a2713aSLionel Sambuc       phi->addIncoming(v, messageBB);
1444f4a2713aSLionel Sambuc       phi->addIncoming(NullVal, startBB);
1445f4a2713aSLionel Sambuc       msgRet = RValue::getAggregate(phi);
1446f4a2713aSLionel Sambuc     } else /* isComplex() */ {
1447f4a2713aSLionel Sambuc       std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal();
1448f4a2713aSLionel Sambuc       llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2);
1449f4a2713aSLionel Sambuc       phi->addIncoming(v.first, messageBB);
1450f4a2713aSLionel Sambuc       phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()),
1451f4a2713aSLionel Sambuc           startBB);
1452f4a2713aSLionel Sambuc       llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2);
1453f4a2713aSLionel Sambuc       phi2->addIncoming(v.second, messageBB);
1454f4a2713aSLionel Sambuc       phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()),
1455f4a2713aSLionel Sambuc           startBB);
1456f4a2713aSLionel Sambuc       msgRet = RValue::getComplex(phi, phi2);
1457f4a2713aSLionel Sambuc     }
1458f4a2713aSLionel Sambuc   }
1459f4a2713aSLionel Sambuc   return msgRet;
1460f4a2713aSLionel Sambuc }
1461f4a2713aSLionel Sambuc 
1462f4a2713aSLionel Sambuc /// Generates a MethodList.  Used in construction of a objc_class and
1463f4a2713aSLionel Sambuc /// objc_category structures.
1464f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::
GenerateMethodList(StringRef ClassName,StringRef CategoryName,ArrayRef<Selector> MethodSels,ArrayRef<llvm::Constant * > MethodTypes,bool isClassMethodList)1465*0a6a1f1dSLionel Sambuc GenerateMethodList(StringRef ClassName,
1466*0a6a1f1dSLionel Sambuc                    StringRef CategoryName,
1467f4a2713aSLionel Sambuc                    ArrayRef<Selector> MethodSels,
1468f4a2713aSLionel Sambuc                    ArrayRef<llvm::Constant *> MethodTypes,
1469f4a2713aSLionel Sambuc                    bool isClassMethodList) {
1470f4a2713aSLionel Sambuc   if (MethodSels.empty())
1471f4a2713aSLionel Sambuc     return NULLPtr;
1472f4a2713aSLionel Sambuc   // Get the method structure type.
1473f4a2713aSLionel Sambuc   llvm::StructType *ObjCMethodTy = llvm::StructType::get(
1474f4a2713aSLionel Sambuc     PtrToInt8Ty, // Really a selector, but the runtime creates it us.
1475f4a2713aSLionel Sambuc     PtrToInt8Ty, // Method types
1476f4a2713aSLionel Sambuc     IMPTy, //Method pointer
1477*0a6a1f1dSLionel Sambuc     nullptr);
1478f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Methods;
1479f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Elements;
1480f4a2713aSLionel Sambuc   for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
1481f4a2713aSLionel Sambuc     Elements.clear();
1482f4a2713aSLionel Sambuc     llvm::Constant *Method =
1483f4a2713aSLionel Sambuc       TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
1484f4a2713aSLionel Sambuc                                                 MethodSels[i],
1485f4a2713aSLionel Sambuc                                                 isClassMethodList));
1486f4a2713aSLionel Sambuc     assert(Method && "Can't generate metadata for method that doesn't exist");
1487f4a2713aSLionel Sambuc     llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
1488f4a2713aSLionel Sambuc     Elements.push_back(C);
1489f4a2713aSLionel Sambuc     Elements.push_back(MethodTypes[i]);
1490f4a2713aSLionel Sambuc     Method = llvm::ConstantExpr::getBitCast(Method,
1491f4a2713aSLionel Sambuc         IMPTy);
1492f4a2713aSLionel Sambuc     Elements.push_back(Method);
1493f4a2713aSLionel Sambuc     Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
1494f4a2713aSLionel Sambuc   }
1495f4a2713aSLionel Sambuc 
1496f4a2713aSLionel Sambuc   // Array of method structures
1497f4a2713aSLionel Sambuc   llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
1498f4a2713aSLionel Sambuc                                                             Methods.size());
1499f4a2713aSLionel Sambuc   llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
1500f4a2713aSLionel Sambuc                                                          Methods);
1501f4a2713aSLionel Sambuc 
1502f4a2713aSLionel Sambuc   // Structure containing list pointer, array and array count
1503f4a2713aSLionel Sambuc   llvm::StructType *ObjCMethodListTy = llvm::StructType::create(VMContext);
1504f4a2713aSLionel Sambuc   llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(ObjCMethodListTy);
1505f4a2713aSLionel Sambuc   ObjCMethodListTy->setBody(
1506f4a2713aSLionel Sambuc       NextPtrTy,
1507f4a2713aSLionel Sambuc       IntTy,
1508f4a2713aSLionel Sambuc       ObjCMethodArrayTy,
1509*0a6a1f1dSLionel Sambuc       nullptr);
1510f4a2713aSLionel Sambuc 
1511f4a2713aSLionel Sambuc   Methods.clear();
1512f4a2713aSLionel Sambuc   Methods.push_back(llvm::ConstantPointerNull::get(
1513f4a2713aSLionel Sambuc         llvm::PointerType::getUnqual(ObjCMethodListTy)));
1514f4a2713aSLionel Sambuc   Methods.push_back(llvm::ConstantInt::get(Int32Ty, MethodTypes.size()));
1515f4a2713aSLionel Sambuc   Methods.push_back(MethodArray);
1516f4a2713aSLionel Sambuc 
1517f4a2713aSLionel Sambuc   // Create an instance of the structure
1518f4a2713aSLionel Sambuc   return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
1519f4a2713aSLionel Sambuc }
1520f4a2713aSLionel Sambuc 
1521f4a2713aSLionel Sambuc /// Generates an IvarList.  Used in construction of a objc_class.
1522f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::
GenerateIvarList(ArrayRef<llvm::Constant * > IvarNames,ArrayRef<llvm::Constant * > IvarTypes,ArrayRef<llvm::Constant * > IvarOffsets)1523f4a2713aSLionel Sambuc GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
1524f4a2713aSLionel Sambuc                  ArrayRef<llvm::Constant *> IvarTypes,
1525f4a2713aSLionel Sambuc                  ArrayRef<llvm::Constant *> IvarOffsets) {
1526f4a2713aSLionel Sambuc   if (IvarNames.size() == 0)
1527f4a2713aSLionel Sambuc     return NULLPtr;
1528f4a2713aSLionel Sambuc   // Get the method structure type.
1529f4a2713aSLionel Sambuc   llvm::StructType *ObjCIvarTy = llvm::StructType::get(
1530f4a2713aSLionel Sambuc     PtrToInt8Ty,
1531f4a2713aSLionel Sambuc     PtrToInt8Ty,
1532f4a2713aSLionel Sambuc     IntTy,
1533*0a6a1f1dSLionel Sambuc     nullptr);
1534f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Ivars;
1535f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Elements;
1536f4a2713aSLionel Sambuc   for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
1537f4a2713aSLionel Sambuc     Elements.clear();
1538f4a2713aSLionel Sambuc     Elements.push_back(IvarNames[i]);
1539f4a2713aSLionel Sambuc     Elements.push_back(IvarTypes[i]);
1540f4a2713aSLionel Sambuc     Elements.push_back(IvarOffsets[i]);
1541f4a2713aSLionel Sambuc     Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
1542f4a2713aSLionel Sambuc   }
1543f4a2713aSLionel Sambuc 
1544f4a2713aSLionel Sambuc   // Array of method structures
1545f4a2713aSLionel Sambuc   llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
1546f4a2713aSLionel Sambuc       IvarNames.size());
1547f4a2713aSLionel Sambuc 
1548f4a2713aSLionel Sambuc 
1549f4a2713aSLionel Sambuc   Elements.clear();
1550f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
1551f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
1552f4a2713aSLionel Sambuc   // Structure containing array and array count
1553f4a2713aSLionel Sambuc   llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
1554f4a2713aSLionel Sambuc     ObjCIvarArrayTy,
1555*0a6a1f1dSLionel Sambuc     nullptr);
1556f4a2713aSLionel Sambuc 
1557f4a2713aSLionel Sambuc   // Create an instance of the structure
1558f4a2713aSLionel Sambuc   return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
1559f4a2713aSLionel Sambuc }
1560f4a2713aSLionel Sambuc 
1561f4a2713aSLionel Sambuc /// Generate a class structure
GenerateClassStructure(llvm::Constant * MetaClass,llvm::Constant * SuperClass,unsigned info,const char * Name,llvm::Constant * Version,llvm::Constant * InstanceSize,llvm::Constant * IVars,llvm::Constant * Methods,llvm::Constant * Protocols,llvm::Constant * IvarOffsets,llvm::Constant * Properties,llvm::Constant * StrongIvarBitmap,llvm::Constant * WeakIvarBitmap,bool isMeta)1562f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GenerateClassStructure(
1563f4a2713aSLionel Sambuc     llvm::Constant *MetaClass,
1564f4a2713aSLionel Sambuc     llvm::Constant *SuperClass,
1565f4a2713aSLionel Sambuc     unsigned info,
1566f4a2713aSLionel Sambuc     const char *Name,
1567f4a2713aSLionel Sambuc     llvm::Constant *Version,
1568f4a2713aSLionel Sambuc     llvm::Constant *InstanceSize,
1569f4a2713aSLionel Sambuc     llvm::Constant *IVars,
1570f4a2713aSLionel Sambuc     llvm::Constant *Methods,
1571f4a2713aSLionel Sambuc     llvm::Constant *Protocols,
1572f4a2713aSLionel Sambuc     llvm::Constant *IvarOffsets,
1573f4a2713aSLionel Sambuc     llvm::Constant *Properties,
1574f4a2713aSLionel Sambuc     llvm::Constant *StrongIvarBitmap,
1575f4a2713aSLionel Sambuc     llvm::Constant *WeakIvarBitmap,
1576f4a2713aSLionel Sambuc     bool isMeta) {
1577f4a2713aSLionel Sambuc   // Set up the class structure
1578f4a2713aSLionel Sambuc   // Note:  Several of these are char*s when they should be ids.  This is
1579f4a2713aSLionel Sambuc   // because the runtime performs this translation on load.
1580f4a2713aSLionel Sambuc   //
1581f4a2713aSLionel Sambuc   // Fields marked New ABI are part of the GNUstep runtime.  We emit them
1582f4a2713aSLionel Sambuc   // anyway; the classes will still work with the GNU runtime, they will just
1583f4a2713aSLionel Sambuc   // be ignored.
1584f4a2713aSLionel Sambuc   llvm::StructType *ClassTy = llvm::StructType::get(
1585f4a2713aSLionel Sambuc       PtrToInt8Ty,        // isa
1586f4a2713aSLionel Sambuc       PtrToInt8Ty,        // super_class
1587f4a2713aSLionel Sambuc       PtrToInt8Ty,        // name
1588f4a2713aSLionel Sambuc       LongTy,             // version
1589f4a2713aSLionel Sambuc       LongTy,             // info
1590f4a2713aSLionel Sambuc       LongTy,             // instance_size
1591f4a2713aSLionel Sambuc       IVars->getType(),   // ivars
1592f4a2713aSLionel Sambuc       Methods->getType(), // methods
1593f4a2713aSLionel Sambuc       // These are all filled in by the runtime, so we pretend
1594f4a2713aSLionel Sambuc       PtrTy,              // dtable
1595f4a2713aSLionel Sambuc       PtrTy,              // subclass_list
1596f4a2713aSLionel Sambuc       PtrTy,              // sibling_class
1597f4a2713aSLionel Sambuc       PtrTy,              // protocols
1598f4a2713aSLionel Sambuc       PtrTy,              // gc_object_type
1599f4a2713aSLionel Sambuc       // New ABI:
1600f4a2713aSLionel Sambuc       LongTy,                 // abi_version
1601f4a2713aSLionel Sambuc       IvarOffsets->getType(), // ivar_offsets
1602f4a2713aSLionel Sambuc       Properties->getType(),  // properties
1603f4a2713aSLionel Sambuc       IntPtrTy,               // strong_pointers
1604f4a2713aSLionel Sambuc       IntPtrTy,               // weak_pointers
1605*0a6a1f1dSLionel Sambuc       nullptr);
1606f4a2713aSLionel Sambuc   llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
1607f4a2713aSLionel Sambuc   // Fill in the structure
1608f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Elements;
1609f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
1610f4a2713aSLionel Sambuc   Elements.push_back(SuperClass);
1611f4a2713aSLionel Sambuc   Elements.push_back(MakeConstantString(Name, ".class_name"));
1612f4a2713aSLionel Sambuc   Elements.push_back(Zero);
1613f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantInt::get(LongTy, info));
1614f4a2713aSLionel Sambuc   if (isMeta) {
1615f4a2713aSLionel Sambuc     llvm::DataLayout td(&TheModule);
1616f4a2713aSLionel Sambuc     Elements.push_back(
1617f4a2713aSLionel Sambuc         llvm::ConstantInt::get(LongTy,
1618f4a2713aSLionel Sambuc                                td.getTypeSizeInBits(ClassTy) /
1619f4a2713aSLionel Sambuc                                  CGM.getContext().getCharWidth()));
1620f4a2713aSLionel Sambuc   } else
1621f4a2713aSLionel Sambuc     Elements.push_back(InstanceSize);
1622f4a2713aSLionel Sambuc   Elements.push_back(IVars);
1623f4a2713aSLionel Sambuc   Elements.push_back(Methods);
1624f4a2713aSLionel Sambuc   Elements.push_back(NULLPtr);
1625f4a2713aSLionel Sambuc   Elements.push_back(NULLPtr);
1626f4a2713aSLionel Sambuc   Elements.push_back(NULLPtr);
1627f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
1628f4a2713aSLionel Sambuc   Elements.push_back(NULLPtr);
1629f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantInt::get(LongTy, 1));
1630f4a2713aSLionel Sambuc   Elements.push_back(IvarOffsets);
1631f4a2713aSLionel Sambuc   Elements.push_back(Properties);
1632f4a2713aSLionel Sambuc   Elements.push_back(StrongIvarBitmap);
1633f4a2713aSLionel Sambuc   Elements.push_back(WeakIvarBitmap);
1634f4a2713aSLionel Sambuc   // Create an instance of the structure
1635f4a2713aSLionel Sambuc   // This is now an externally visible symbol, so that we can speed up class
1636f4a2713aSLionel Sambuc   // messages in the next ABI.  We may already have some weak references to
1637f4a2713aSLionel Sambuc   // this, so check and fix them properly.
1638f4a2713aSLionel Sambuc   std::string ClassSym((isMeta ? "_OBJC_METACLASS_": "_OBJC_CLASS_") +
1639f4a2713aSLionel Sambuc           std::string(Name));
1640f4a2713aSLionel Sambuc   llvm::GlobalVariable *ClassRef = TheModule.getNamedGlobal(ClassSym);
1641f4a2713aSLionel Sambuc   llvm::Constant *Class = MakeGlobal(ClassTy, Elements, ClassSym,
1642f4a2713aSLionel Sambuc           llvm::GlobalValue::ExternalLinkage);
1643f4a2713aSLionel Sambuc   if (ClassRef) {
1644f4a2713aSLionel Sambuc       ClassRef->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(Class,
1645f4a2713aSLionel Sambuc                   ClassRef->getType()));
1646f4a2713aSLionel Sambuc       ClassRef->removeFromParent();
1647f4a2713aSLionel Sambuc       Class->setName(ClassSym);
1648f4a2713aSLionel Sambuc   }
1649f4a2713aSLionel Sambuc   return Class;
1650f4a2713aSLionel Sambuc }
1651f4a2713aSLionel Sambuc 
1652f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::
GenerateProtocolMethodList(ArrayRef<llvm::Constant * > MethodNames,ArrayRef<llvm::Constant * > MethodTypes)1653f4a2713aSLionel Sambuc GenerateProtocolMethodList(ArrayRef<llvm::Constant *> MethodNames,
1654f4a2713aSLionel Sambuc                            ArrayRef<llvm::Constant *> MethodTypes) {
1655f4a2713aSLionel Sambuc   // Get the method structure type.
1656f4a2713aSLionel Sambuc   llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
1657f4a2713aSLionel Sambuc     PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
1658f4a2713aSLionel Sambuc     PtrToInt8Ty,
1659*0a6a1f1dSLionel Sambuc     nullptr);
1660f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Methods;
1661f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Elements;
1662f4a2713aSLionel Sambuc   for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
1663f4a2713aSLionel Sambuc     Elements.clear();
1664f4a2713aSLionel Sambuc     Elements.push_back(MethodNames[i]);
1665f4a2713aSLionel Sambuc     Elements.push_back(MethodTypes[i]);
1666f4a2713aSLionel Sambuc     Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
1667f4a2713aSLionel Sambuc   }
1668f4a2713aSLionel Sambuc   llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
1669f4a2713aSLionel Sambuc       MethodNames.size());
1670f4a2713aSLionel Sambuc   llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
1671f4a2713aSLionel Sambuc                                                    Methods);
1672f4a2713aSLionel Sambuc   llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
1673*0a6a1f1dSLionel Sambuc       IntTy, ObjCMethodArrayTy, nullptr);
1674f4a2713aSLionel Sambuc   Methods.clear();
1675f4a2713aSLionel Sambuc   Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
1676f4a2713aSLionel Sambuc   Methods.push_back(Array);
1677f4a2713aSLionel Sambuc   return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
1678f4a2713aSLionel Sambuc }
1679f4a2713aSLionel Sambuc 
1680f4a2713aSLionel Sambuc // Create the protocol list structure used in classes, categories and so on
GenerateProtocolList(ArrayRef<std::string> Protocols)1681f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GenerateProtocolList(ArrayRef<std::string>Protocols){
1682f4a2713aSLionel Sambuc   llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
1683f4a2713aSLionel Sambuc       Protocols.size());
1684f4a2713aSLionel Sambuc   llvm::StructType *ProtocolListTy = llvm::StructType::get(
1685f4a2713aSLionel Sambuc       PtrTy, //Should be a recurisve pointer, but it's always NULL here.
1686f4a2713aSLionel Sambuc       SizeTy,
1687f4a2713aSLionel Sambuc       ProtocolArrayTy,
1688*0a6a1f1dSLionel Sambuc       nullptr);
1689f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Elements;
1690f4a2713aSLionel Sambuc   for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
1691f4a2713aSLionel Sambuc       iter != endIter ; iter++) {
1692*0a6a1f1dSLionel Sambuc     llvm::Constant *protocol = nullptr;
1693f4a2713aSLionel Sambuc     llvm::StringMap<llvm::Constant*>::iterator value =
1694f4a2713aSLionel Sambuc       ExistingProtocols.find(*iter);
1695f4a2713aSLionel Sambuc     if (value == ExistingProtocols.end()) {
1696f4a2713aSLionel Sambuc       protocol = GenerateEmptyProtocol(*iter);
1697f4a2713aSLionel Sambuc     } else {
1698f4a2713aSLionel Sambuc       protocol = value->getValue();
1699f4a2713aSLionel Sambuc     }
1700f4a2713aSLionel Sambuc     llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
1701f4a2713aSLionel Sambuc                                                            PtrToInt8Ty);
1702f4a2713aSLionel Sambuc     Elements.push_back(Ptr);
1703f4a2713aSLionel Sambuc   }
1704f4a2713aSLionel Sambuc   llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1705f4a2713aSLionel Sambuc       Elements);
1706f4a2713aSLionel Sambuc   Elements.clear();
1707f4a2713aSLionel Sambuc   Elements.push_back(NULLPtr);
1708f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
1709f4a2713aSLionel Sambuc   Elements.push_back(ProtocolArray);
1710f4a2713aSLionel Sambuc   return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
1711f4a2713aSLionel Sambuc }
1712f4a2713aSLionel Sambuc 
GenerateProtocolRef(CodeGenFunction & CGF,const ObjCProtocolDecl * PD)1713f4a2713aSLionel Sambuc llvm::Value *CGObjCGNU::GenerateProtocolRef(CodeGenFunction &CGF,
1714f4a2713aSLionel Sambuc                                             const ObjCProtocolDecl *PD) {
1715f4a2713aSLionel Sambuc   llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
1716f4a2713aSLionel Sambuc   llvm::Type *T =
1717f4a2713aSLionel Sambuc     CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
1718f4a2713aSLionel Sambuc   return CGF.Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
1719f4a2713aSLionel Sambuc }
1720f4a2713aSLionel Sambuc 
GenerateEmptyProtocol(const std::string & ProtocolName)1721f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
1722f4a2713aSLionel Sambuc   const std::string &ProtocolName) {
1723f4a2713aSLionel Sambuc   SmallVector<std::string, 0> EmptyStringVector;
1724f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 0> EmptyConstantVector;
1725f4a2713aSLionel Sambuc 
1726f4a2713aSLionel Sambuc   llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
1727f4a2713aSLionel Sambuc   llvm::Constant *MethodList =
1728f4a2713aSLionel Sambuc     GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
1729f4a2713aSLionel Sambuc   // Protocols are objects containing lists of the methods implemented and
1730f4a2713aSLionel Sambuc   // protocols adopted.
1731f4a2713aSLionel Sambuc   llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
1732f4a2713aSLionel Sambuc       PtrToInt8Ty,
1733f4a2713aSLionel Sambuc       ProtocolList->getType(),
1734f4a2713aSLionel Sambuc       MethodList->getType(),
1735f4a2713aSLionel Sambuc       MethodList->getType(),
1736f4a2713aSLionel Sambuc       MethodList->getType(),
1737f4a2713aSLionel Sambuc       MethodList->getType(),
1738*0a6a1f1dSLionel Sambuc       nullptr);
1739f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Elements;
1740f4a2713aSLionel Sambuc   // The isa pointer must be set to a magic number so the runtime knows it's
1741f4a2713aSLionel Sambuc   // the correct layout.
1742f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantExpr::getIntToPtr(
1743f4a2713aSLionel Sambuc         llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
1744f4a2713aSLionel Sambuc   Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1745f4a2713aSLionel Sambuc   Elements.push_back(ProtocolList);
1746f4a2713aSLionel Sambuc   Elements.push_back(MethodList);
1747f4a2713aSLionel Sambuc   Elements.push_back(MethodList);
1748f4a2713aSLionel Sambuc   Elements.push_back(MethodList);
1749f4a2713aSLionel Sambuc   Elements.push_back(MethodList);
1750f4a2713aSLionel Sambuc   return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
1751f4a2713aSLionel Sambuc }
1752f4a2713aSLionel Sambuc 
GenerateProtocol(const ObjCProtocolDecl * PD)1753f4a2713aSLionel Sambuc void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
1754f4a2713aSLionel Sambuc   ASTContext &Context = CGM.getContext();
1755f4a2713aSLionel Sambuc   std::string ProtocolName = PD->getNameAsString();
1756f4a2713aSLionel Sambuc 
1757f4a2713aSLionel Sambuc   // Use the protocol definition, if there is one.
1758f4a2713aSLionel Sambuc   if (const ObjCProtocolDecl *Def = PD->getDefinition())
1759f4a2713aSLionel Sambuc     PD = Def;
1760f4a2713aSLionel Sambuc 
1761f4a2713aSLionel Sambuc   SmallVector<std::string, 16> Protocols;
1762*0a6a1f1dSLionel Sambuc   for (const auto *PI : PD->protocols())
1763*0a6a1f1dSLionel Sambuc     Protocols.push_back(PI->getNameAsString());
1764f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> InstanceMethodNames;
1765f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
1766f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames;
1767f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes;
1768*0a6a1f1dSLionel Sambuc   for (const auto *I : PD->instance_methods()) {
1769f4a2713aSLionel Sambuc     std::string TypeStr;
1770*0a6a1f1dSLionel Sambuc     Context.getObjCEncodingForMethodDecl(I, TypeStr);
1771*0a6a1f1dSLionel Sambuc     if (I->getImplementationControl() == ObjCMethodDecl::Optional) {
1772f4a2713aSLionel Sambuc       OptionalInstanceMethodNames.push_back(
1773*0a6a1f1dSLionel Sambuc           MakeConstantString(I->getSelector().getAsString()));
1774f4a2713aSLionel Sambuc       OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1775f4a2713aSLionel Sambuc     } else {
1776f4a2713aSLionel Sambuc       InstanceMethodNames.push_back(
1777*0a6a1f1dSLionel Sambuc           MakeConstantString(I->getSelector().getAsString()));
1778f4a2713aSLionel Sambuc       InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1779f4a2713aSLionel Sambuc     }
1780f4a2713aSLionel Sambuc   }
1781f4a2713aSLionel Sambuc   // Collect information about class methods:
1782f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> ClassMethodNames;
1783f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> ClassMethodTypes;
1784f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> OptionalClassMethodNames;
1785f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes;
1786*0a6a1f1dSLionel Sambuc   for (const auto *I : PD->class_methods()) {
1787f4a2713aSLionel Sambuc     std::string TypeStr;
1788*0a6a1f1dSLionel Sambuc     Context.getObjCEncodingForMethodDecl(I,TypeStr);
1789*0a6a1f1dSLionel Sambuc     if (I->getImplementationControl() == ObjCMethodDecl::Optional) {
1790f4a2713aSLionel Sambuc       OptionalClassMethodNames.push_back(
1791*0a6a1f1dSLionel Sambuc           MakeConstantString(I->getSelector().getAsString()));
1792f4a2713aSLionel Sambuc       OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr));
1793f4a2713aSLionel Sambuc     } else {
1794f4a2713aSLionel Sambuc       ClassMethodNames.push_back(
1795*0a6a1f1dSLionel Sambuc           MakeConstantString(I->getSelector().getAsString()));
1796f4a2713aSLionel Sambuc       ClassMethodTypes.push_back(MakeConstantString(TypeStr));
1797f4a2713aSLionel Sambuc     }
1798f4a2713aSLionel Sambuc   }
1799f4a2713aSLionel Sambuc 
1800f4a2713aSLionel Sambuc   llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
1801f4a2713aSLionel Sambuc   llvm::Constant *InstanceMethodList =
1802f4a2713aSLionel Sambuc     GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
1803f4a2713aSLionel Sambuc   llvm::Constant *ClassMethodList =
1804f4a2713aSLionel Sambuc     GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
1805f4a2713aSLionel Sambuc   llvm::Constant *OptionalInstanceMethodList =
1806f4a2713aSLionel Sambuc     GenerateProtocolMethodList(OptionalInstanceMethodNames,
1807f4a2713aSLionel Sambuc             OptionalInstanceMethodTypes);
1808f4a2713aSLionel Sambuc   llvm::Constant *OptionalClassMethodList =
1809f4a2713aSLionel Sambuc     GenerateProtocolMethodList(OptionalClassMethodNames,
1810f4a2713aSLionel Sambuc             OptionalClassMethodTypes);
1811f4a2713aSLionel Sambuc 
1812f4a2713aSLionel Sambuc   // Property metadata: name, attributes, isSynthesized, setter name, setter
1813f4a2713aSLionel Sambuc   // types, getter name, getter types.
1814f4a2713aSLionel Sambuc   // The isSynthesized value is always set to 0 in a protocol.  It exists to
1815f4a2713aSLionel Sambuc   // simplify the runtime library by allowing it to use the same data
1816f4a2713aSLionel Sambuc   // structures for protocol metadata everywhere.
1817f4a2713aSLionel Sambuc   llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
1818f4a2713aSLionel Sambuc           PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty,
1819*0a6a1f1dSLionel Sambuc           PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, nullptr);
1820f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Properties;
1821f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> OptionalProperties;
1822f4a2713aSLionel Sambuc 
1823f4a2713aSLionel Sambuc   // Add all of the property methods need adding to the method list and to the
1824f4a2713aSLionel Sambuc   // property metadata list.
1825*0a6a1f1dSLionel Sambuc   for (auto *property : PD->properties()) {
1826f4a2713aSLionel Sambuc     std::vector<llvm::Constant*> Fields;
1827f4a2713aSLionel Sambuc 
1828*0a6a1f1dSLionel Sambuc     Fields.push_back(MakePropertyEncodingString(property, nullptr));
1829f4a2713aSLionel Sambuc     PushPropertyAttributes(Fields, property);
1830f4a2713aSLionel Sambuc 
1831f4a2713aSLionel Sambuc     if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1832f4a2713aSLionel Sambuc       std::string TypeStr;
1833f4a2713aSLionel Sambuc       Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1834f4a2713aSLionel Sambuc       llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1835f4a2713aSLionel Sambuc       InstanceMethodTypes.push_back(TypeEncoding);
1836f4a2713aSLionel Sambuc       Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1837f4a2713aSLionel Sambuc       Fields.push_back(TypeEncoding);
1838f4a2713aSLionel Sambuc     } else {
1839f4a2713aSLionel Sambuc       Fields.push_back(NULLPtr);
1840f4a2713aSLionel Sambuc       Fields.push_back(NULLPtr);
1841f4a2713aSLionel Sambuc     }
1842f4a2713aSLionel Sambuc     if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1843f4a2713aSLionel Sambuc       std::string TypeStr;
1844f4a2713aSLionel Sambuc       Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1845f4a2713aSLionel Sambuc       llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1846f4a2713aSLionel Sambuc       InstanceMethodTypes.push_back(TypeEncoding);
1847f4a2713aSLionel Sambuc       Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1848f4a2713aSLionel Sambuc       Fields.push_back(TypeEncoding);
1849f4a2713aSLionel Sambuc     } else {
1850f4a2713aSLionel Sambuc       Fields.push_back(NULLPtr);
1851f4a2713aSLionel Sambuc       Fields.push_back(NULLPtr);
1852f4a2713aSLionel Sambuc     }
1853f4a2713aSLionel Sambuc     if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) {
1854f4a2713aSLionel Sambuc       OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1855f4a2713aSLionel Sambuc     } else {
1856f4a2713aSLionel Sambuc       Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1857f4a2713aSLionel Sambuc     }
1858f4a2713aSLionel Sambuc   }
1859f4a2713aSLionel Sambuc   llvm::Constant *PropertyArray = llvm::ConstantArray::get(
1860f4a2713aSLionel Sambuc       llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties);
1861f4a2713aSLionel Sambuc   llvm::Constant* PropertyListInitFields[] =
1862f4a2713aSLionel Sambuc     {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1863f4a2713aSLionel Sambuc 
1864f4a2713aSLionel Sambuc   llvm::Constant *PropertyListInit =
1865f4a2713aSLionel Sambuc       llvm::ConstantStruct::getAnon(PropertyListInitFields);
1866f4a2713aSLionel Sambuc   llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule,
1867f4a2713aSLionel Sambuc       PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage,
1868f4a2713aSLionel Sambuc       PropertyListInit, ".objc_property_list");
1869f4a2713aSLionel Sambuc 
1870f4a2713aSLionel Sambuc   llvm::Constant *OptionalPropertyArray =
1871f4a2713aSLionel Sambuc       llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy,
1872f4a2713aSLionel Sambuc           OptionalProperties.size()) , OptionalProperties);
1873f4a2713aSLionel Sambuc   llvm::Constant* OptionalPropertyListInitFields[] = {
1874f4a2713aSLionel Sambuc       llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr,
1875f4a2713aSLionel Sambuc       OptionalPropertyArray };
1876f4a2713aSLionel Sambuc 
1877f4a2713aSLionel Sambuc   llvm::Constant *OptionalPropertyListInit =
1878f4a2713aSLionel Sambuc       llvm::ConstantStruct::getAnon(OptionalPropertyListInitFields);
1879f4a2713aSLionel Sambuc   llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule,
1880f4a2713aSLionel Sambuc           OptionalPropertyListInit->getType(), false,
1881f4a2713aSLionel Sambuc           llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit,
1882f4a2713aSLionel Sambuc           ".objc_property_list");
1883f4a2713aSLionel Sambuc 
1884f4a2713aSLionel Sambuc   // Protocols are objects containing lists of the methods implemented and
1885f4a2713aSLionel Sambuc   // protocols adopted.
1886f4a2713aSLionel Sambuc   llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
1887f4a2713aSLionel Sambuc       PtrToInt8Ty,
1888f4a2713aSLionel Sambuc       ProtocolList->getType(),
1889f4a2713aSLionel Sambuc       InstanceMethodList->getType(),
1890f4a2713aSLionel Sambuc       ClassMethodList->getType(),
1891f4a2713aSLionel Sambuc       OptionalInstanceMethodList->getType(),
1892f4a2713aSLionel Sambuc       OptionalClassMethodList->getType(),
1893f4a2713aSLionel Sambuc       PropertyList->getType(),
1894f4a2713aSLionel Sambuc       OptionalPropertyList->getType(),
1895*0a6a1f1dSLionel Sambuc       nullptr);
1896f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Elements;
1897f4a2713aSLionel Sambuc   // The isa pointer must be set to a magic number so the runtime knows it's
1898f4a2713aSLionel Sambuc   // the correct layout.
1899f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantExpr::getIntToPtr(
1900f4a2713aSLionel Sambuc         llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
1901f4a2713aSLionel Sambuc   Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1902f4a2713aSLionel Sambuc   Elements.push_back(ProtocolList);
1903f4a2713aSLionel Sambuc   Elements.push_back(InstanceMethodList);
1904f4a2713aSLionel Sambuc   Elements.push_back(ClassMethodList);
1905f4a2713aSLionel Sambuc   Elements.push_back(OptionalInstanceMethodList);
1906f4a2713aSLionel Sambuc   Elements.push_back(OptionalClassMethodList);
1907f4a2713aSLionel Sambuc   Elements.push_back(PropertyList);
1908f4a2713aSLionel Sambuc   Elements.push_back(OptionalPropertyList);
1909f4a2713aSLionel Sambuc   ExistingProtocols[ProtocolName] =
1910f4a2713aSLionel Sambuc     llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
1911f4a2713aSLionel Sambuc           ".objc_protocol"), IdTy);
1912f4a2713aSLionel Sambuc }
GenerateProtocolHolderCategory()1913f4a2713aSLionel Sambuc void CGObjCGNU::GenerateProtocolHolderCategory() {
1914f4a2713aSLionel Sambuc   // Collect information about instance methods
1915f4a2713aSLionel Sambuc   SmallVector<Selector, 1> MethodSels;
1916f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 1> MethodTypes;
1917f4a2713aSLionel Sambuc 
1918f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Elements;
1919f4a2713aSLionel Sambuc   const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
1920f4a2713aSLionel Sambuc   const std::string CategoryName = "AnotherHack";
1921f4a2713aSLionel Sambuc   Elements.push_back(MakeConstantString(CategoryName));
1922f4a2713aSLionel Sambuc   Elements.push_back(MakeConstantString(ClassName));
1923f4a2713aSLionel Sambuc   // Instance method list
1924f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1925f4a2713aSLionel Sambuc           ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));
1926f4a2713aSLionel Sambuc   // Class method list
1927f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1928f4a2713aSLionel Sambuc           ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));
1929f4a2713aSLionel Sambuc   // Protocol list
1930f4a2713aSLionel Sambuc   llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy,
1931f4a2713aSLionel Sambuc       ExistingProtocols.size());
1932f4a2713aSLionel Sambuc   llvm::StructType *ProtocolListTy = llvm::StructType::get(
1933f4a2713aSLionel Sambuc       PtrTy, //Should be a recurisve pointer, but it's always NULL here.
1934f4a2713aSLionel Sambuc       SizeTy,
1935f4a2713aSLionel Sambuc       ProtocolArrayTy,
1936*0a6a1f1dSLionel Sambuc       nullptr);
1937f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> ProtocolElements;
1938f4a2713aSLionel Sambuc   for (llvm::StringMapIterator<llvm::Constant*> iter =
1939f4a2713aSLionel Sambuc        ExistingProtocols.begin(), endIter = ExistingProtocols.end();
1940f4a2713aSLionel Sambuc        iter != endIter ; iter++) {
1941f4a2713aSLionel Sambuc     llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(),
1942f4a2713aSLionel Sambuc             PtrTy);
1943f4a2713aSLionel Sambuc     ProtocolElements.push_back(Ptr);
1944f4a2713aSLionel Sambuc   }
1945f4a2713aSLionel Sambuc   llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1946f4a2713aSLionel Sambuc       ProtocolElements);
1947f4a2713aSLionel Sambuc   ProtocolElements.clear();
1948f4a2713aSLionel Sambuc   ProtocolElements.push_back(NULLPtr);
1949f4a2713aSLionel Sambuc   ProtocolElements.push_back(llvm::ConstantInt::get(LongTy,
1950f4a2713aSLionel Sambuc               ExistingProtocols.size()));
1951f4a2713aSLionel Sambuc   ProtocolElements.push_back(ProtocolArray);
1952f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy,
1953f4a2713aSLionel Sambuc                   ProtocolElements, ".objc_protocol_list"), PtrTy));
1954f4a2713aSLionel Sambuc   Categories.push_back(llvm::ConstantExpr::getBitCast(
1955f4a2713aSLionel Sambuc         MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
1956*0a6a1f1dSLionel Sambuc             PtrTy, PtrTy, PtrTy, nullptr), Elements), PtrTy));
1957f4a2713aSLionel Sambuc }
1958f4a2713aSLionel Sambuc 
1959f4a2713aSLionel Sambuc /// Libobjc2 uses a bitfield representation where small(ish) bitfields are
1960f4a2713aSLionel Sambuc /// stored in a 64-bit value with the low bit set to 1 and the remaining 63
1961f4a2713aSLionel Sambuc /// bits set to their values, LSB first, while larger ones are stored in a
1962f4a2713aSLionel Sambuc /// structure of this / form:
1963f4a2713aSLionel Sambuc ///
1964f4a2713aSLionel Sambuc /// struct { int32_t length; int32_t values[length]; };
1965f4a2713aSLionel Sambuc ///
1966f4a2713aSLionel Sambuc /// The values in the array are stored in host-endian format, with the least
1967f4a2713aSLionel Sambuc /// significant bit being assumed to come first in the bitfield.  Therefore, a
1968f4a2713aSLionel Sambuc /// bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] }, while a
1969f4a2713aSLionel Sambuc /// bitfield / with the 63rd bit set will be 1<<64.
MakeBitField(ArrayRef<bool> bits)1970f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::MakeBitField(ArrayRef<bool> bits) {
1971f4a2713aSLionel Sambuc   int bitCount = bits.size();
1972*0a6a1f1dSLionel Sambuc   int ptrBits = CGM.getDataLayout().getPointerSizeInBits();
1973f4a2713aSLionel Sambuc   if (bitCount < ptrBits) {
1974f4a2713aSLionel Sambuc     uint64_t val = 1;
1975f4a2713aSLionel Sambuc     for (int i=0 ; i<bitCount ; ++i) {
1976f4a2713aSLionel Sambuc       if (bits[i]) val |= 1ULL<<(i+1);
1977f4a2713aSLionel Sambuc     }
1978f4a2713aSLionel Sambuc     return llvm::ConstantInt::get(IntPtrTy, val);
1979f4a2713aSLionel Sambuc   }
1980f4a2713aSLionel Sambuc   SmallVector<llvm::Constant *, 8> values;
1981f4a2713aSLionel Sambuc   int v=0;
1982f4a2713aSLionel Sambuc   while (v < bitCount) {
1983f4a2713aSLionel Sambuc     int32_t word = 0;
1984f4a2713aSLionel Sambuc     for (int i=0 ; (i<32) && (v<bitCount)  ; ++i) {
1985f4a2713aSLionel Sambuc       if (bits[v]) word |= 1<<i;
1986f4a2713aSLionel Sambuc       v++;
1987f4a2713aSLionel Sambuc     }
1988f4a2713aSLionel Sambuc     values.push_back(llvm::ConstantInt::get(Int32Ty, word));
1989f4a2713aSLionel Sambuc   }
1990f4a2713aSLionel Sambuc   llvm::ArrayType *arrayTy = llvm::ArrayType::get(Int32Ty, values.size());
1991f4a2713aSLionel Sambuc   llvm::Constant *array = llvm::ConstantArray::get(arrayTy, values);
1992f4a2713aSLionel Sambuc   llvm::Constant *fields[2] = {
1993f4a2713aSLionel Sambuc       llvm::ConstantInt::get(Int32Ty, values.size()),
1994f4a2713aSLionel Sambuc       array };
1995f4a2713aSLionel Sambuc   llvm::Constant *GS = MakeGlobal(llvm::StructType::get(Int32Ty, arrayTy,
1996*0a6a1f1dSLionel Sambuc         nullptr), fields);
1997f4a2713aSLionel Sambuc   llvm::Constant *ptr = llvm::ConstantExpr::getPtrToInt(GS, IntPtrTy);
1998f4a2713aSLionel Sambuc   return ptr;
1999f4a2713aSLionel Sambuc }
2000f4a2713aSLionel Sambuc 
GenerateCategory(const ObjCCategoryImplDecl * OCD)2001f4a2713aSLionel Sambuc void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
2002f4a2713aSLionel Sambuc   std::string ClassName = OCD->getClassInterface()->getNameAsString();
2003f4a2713aSLionel Sambuc   std::string CategoryName = OCD->getNameAsString();
2004f4a2713aSLionel Sambuc   // Collect information about instance methods
2005f4a2713aSLionel Sambuc   SmallVector<Selector, 16> InstanceMethodSels;
2006f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
2007*0a6a1f1dSLionel Sambuc   for (const auto *I : OCD->instance_methods()) {
2008*0a6a1f1dSLionel Sambuc     InstanceMethodSels.push_back(I->getSelector());
2009f4a2713aSLionel Sambuc     std::string TypeStr;
2010*0a6a1f1dSLionel Sambuc     CGM.getContext().getObjCEncodingForMethodDecl(I,TypeStr);
2011f4a2713aSLionel Sambuc     InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
2012f4a2713aSLionel Sambuc   }
2013f4a2713aSLionel Sambuc 
2014f4a2713aSLionel Sambuc   // Collect information about class methods
2015f4a2713aSLionel Sambuc   SmallVector<Selector, 16> ClassMethodSels;
2016f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> ClassMethodTypes;
2017*0a6a1f1dSLionel Sambuc   for (const auto *I : OCD->class_methods()) {
2018*0a6a1f1dSLionel Sambuc     ClassMethodSels.push_back(I->getSelector());
2019f4a2713aSLionel Sambuc     std::string TypeStr;
2020*0a6a1f1dSLionel Sambuc     CGM.getContext().getObjCEncodingForMethodDecl(I,TypeStr);
2021f4a2713aSLionel Sambuc     ClassMethodTypes.push_back(MakeConstantString(TypeStr));
2022f4a2713aSLionel Sambuc   }
2023f4a2713aSLionel Sambuc 
2024f4a2713aSLionel Sambuc   // Collect the names of referenced protocols
2025f4a2713aSLionel Sambuc   SmallVector<std::string, 16> Protocols;
2026f4a2713aSLionel Sambuc   const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl();
2027f4a2713aSLionel Sambuc   const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols();
2028f4a2713aSLionel Sambuc   for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
2029f4a2713aSLionel Sambuc        E = Protos.end(); I != E; ++I)
2030f4a2713aSLionel Sambuc     Protocols.push_back((*I)->getNameAsString());
2031f4a2713aSLionel Sambuc 
2032f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Elements;
2033f4a2713aSLionel Sambuc   Elements.push_back(MakeConstantString(CategoryName));
2034f4a2713aSLionel Sambuc   Elements.push_back(MakeConstantString(ClassName));
2035f4a2713aSLionel Sambuc   // Instance method list
2036f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
2037f4a2713aSLionel Sambuc           ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
2038f4a2713aSLionel Sambuc           false), PtrTy));
2039f4a2713aSLionel Sambuc   // Class method list
2040f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
2041f4a2713aSLionel Sambuc           ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
2042f4a2713aSLionel Sambuc         PtrTy));
2043f4a2713aSLionel Sambuc   // Protocol list
2044f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantExpr::getBitCast(
2045f4a2713aSLionel Sambuc         GenerateProtocolList(Protocols), PtrTy));
2046f4a2713aSLionel Sambuc   Categories.push_back(llvm::ConstantExpr::getBitCast(
2047f4a2713aSLionel Sambuc         MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
2048*0a6a1f1dSLionel Sambuc             PtrTy, PtrTy, PtrTy, nullptr), Elements), PtrTy));
2049f4a2713aSLionel Sambuc }
2050f4a2713aSLionel Sambuc 
GeneratePropertyList(const ObjCImplementationDecl * OID,SmallVectorImpl<Selector> & InstanceMethodSels,SmallVectorImpl<llvm::Constant * > & InstanceMethodTypes)2051f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID,
2052f4a2713aSLionel Sambuc         SmallVectorImpl<Selector> &InstanceMethodSels,
2053f4a2713aSLionel Sambuc         SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) {
2054f4a2713aSLionel Sambuc   ASTContext &Context = CGM.getContext();
2055f4a2713aSLionel Sambuc   // Property metadata: name, attributes, attributes2, padding1, padding2,
2056f4a2713aSLionel Sambuc   // setter name, setter types, getter name, getter types.
2057f4a2713aSLionel Sambuc   llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
2058f4a2713aSLionel Sambuc           PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty,
2059*0a6a1f1dSLionel Sambuc           PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, nullptr);
2060f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Properties;
2061f4a2713aSLionel Sambuc 
2062f4a2713aSLionel Sambuc   // Add all of the property methods need adding to the method list and to the
2063f4a2713aSLionel Sambuc   // property metadata list.
2064*0a6a1f1dSLionel Sambuc   for (auto *propertyImpl : OID->property_impls()) {
2065f4a2713aSLionel Sambuc     std::vector<llvm::Constant*> Fields;
2066*0a6a1f1dSLionel Sambuc     ObjCPropertyDecl *property = propertyImpl->getPropertyDecl();
2067f4a2713aSLionel Sambuc     bool isSynthesized = (propertyImpl->getPropertyImplementation() ==
2068f4a2713aSLionel Sambuc         ObjCPropertyImplDecl::Synthesize);
2069f4a2713aSLionel Sambuc     bool isDynamic = (propertyImpl->getPropertyImplementation() ==
2070f4a2713aSLionel Sambuc         ObjCPropertyImplDecl::Dynamic);
2071f4a2713aSLionel Sambuc 
2072f4a2713aSLionel Sambuc     Fields.push_back(MakePropertyEncodingString(property, OID));
2073f4a2713aSLionel Sambuc     PushPropertyAttributes(Fields, property, isSynthesized, isDynamic);
2074f4a2713aSLionel Sambuc     if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
2075f4a2713aSLionel Sambuc       std::string TypeStr;
2076f4a2713aSLionel Sambuc       Context.getObjCEncodingForMethodDecl(getter,TypeStr);
2077f4a2713aSLionel Sambuc       llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
2078f4a2713aSLionel Sambuc       if (isSynthesized) {
2079f4a2713aSLionel Sambuc         InstanceMethodTypes.push_back(TypeEncoding);
2080f4a2713aSLionel Sambuc         InstanceMethodSels.push_back(getter->getSelector());
2081f4a2713aSLionel Sambuc       }
2082f4a2713aSLionel Sambuc       Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
2083f4a2713aSLionel Sambuc       Fields.push_back(TypeEncoding);
2084f4a2713aSLionel Sambuc     } else {
2085f4a2713aSLionel Sambuc       Fields.push_back(NULLPtr);
2086f4a2713aSLionel Sambuc       Fields.push_back(NULLPtr);
2087f4a2713aSLionel Sambuc     }
2088f4a2713aSLionel Sambuc     if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
2089f4a2713aSLionel Sambuc       std::string TypeStr;
2090f4a2713aSLionel Sambuc       Context.getObjCEncodingForMethodDecl(setter,TypeStr);
2091f4a2713aSLionel Sambuc       llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
2092f4a2713aSLionel Sambuc       if (isSynthesized) {
2093f4a2713aSLionel Sambuc         InstanceMethodTypes.push_back(TypeEncoding);
2094f4a2713aSLionel Sambuc         InstanceMethodSels.push_back(setter->getSelector());
2095f4a2713aSLionel Sambuc       }
2096f4a2713aSLionel Sambuc       Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
2097f4a2713aSLionel Sambuc       Fields.push_back(TypeEncoding);
2098f4a2713aSLionel Sambuc     } else {
2099f4a2713aSLionel Sambuc       Fields.push_back(NULLPtr);
2100f4a2713aSLionel Sambuc       Fields.push_back(NULLPtr);
2101f4a2713aSLionel Sambuc     }
2102f4a2713aSLionel Sambuc     Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
2103f4a2713aSLionel Sambuc   }
2104f4a2713aSLionel Sambuc   llvm::ArrayType *PropertyArrayTy =
2105f4a2713aSLionel Sambuc       llvm::ArrayType::get(PropertyMetadataTy, Properties.size());
2106f4a2713aSLionel Sambuc   llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy,
2107f4a2713aSLionel Sambuc           Properties);
2108f4a2713aSLionel Sambuc   llvm::Constant* PropertyListInitFields[] =
2109f4a2713aSLionel Sambuc     {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
2110f4a2713aSLionel Sambuc 
2111f4a2713aSLionel Sambuc   llvm::Constant *PropertyListInit =
2112f4a2713aSLionel Sambuc       llvm::ConstantStruct::getAnon(PropertyListInitFields);
2113f4a2713aSLionel Sambuc   return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false,
2114f4a2713aSLionel Sambuc           llvm::GlobalValue::InternalLinkage, PropertyListInit,
2115f4a2713aSLionel Sambuc           ".objc_property_list");
2116f4a2713aSLionel Sambuc }
2117f4a2713aSLionel Sambuc 
RegisterAlias(const ObjCCompatibleAliasDecl * OAD)2118f4a2713aSLionel Sambuc void CGObjCGNU::RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {
2119f4a2713aSLionel Sambuc   // Get the class declaration for which the alias is specified.
2120f4a2713aSLionel Sambuc   ObjCInterfaceDecl *ClassDecl =
2121f4a2713aSLionel Sambuc     const_cast<ObjCInterfaceDecl *>(OAD->getClassInterface());
2122f4a2713aSLionel Sambuc   std::string ClassName = ClassDecl->getNameAsString();
2123f4a2713aSLionel Sambuc   std::string AliasName = OAD->getNameAsString();
2124f4a2713aSLionel Sambuc   ClassAliases.push_back(ClassAliasPair(ClassName,AliasName));
2125f4a2713aSLionel Sambuc }
2126f4a2713aSLionel Sambuc 
GenerateClass(const ObjCImplementationDecl * OID)2127f4a2713aSLionel Sambuc void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
2128f4a2713aSLionel Sambuc   ASTContext &Context = CGM.getContext();
2129f4a2713aSLionel Sambuc 
2130f4a2713aSLionel Sambuc   // Get the superclass name.
2131f4a2713aSLionel Sambuc   const ObjCInterfaceDecl * SuperClassDecl =
2132f4a2713aSLionel Sambuc     OID->getClassInterface()->getSuperClass();
2133f4a2713aSLionel Sambuc   std::string SuperClassName;
2134f4a2713aSLionel Sambuc   if (SuperClassDecl) {
2135f4a2713aSLionel Sambuc     SuperClassName = SuperClassDecl->getNameAsString();
2136f4a2713aSLionel Sambuc     EmitClassRef(SuperClassName);
2137f4a2713aSLionel Sambuc   }
2138f4a2713aSLionel Sambuc 
2139f4a2713aSLionel Sambuc   // Get the class name
2140f4a2713aSLionel Sambuc   ObjCInterfaceDecl *ClassDecl =
2141f4a2713aSLionel Sambuc     const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
2142f4a2713aSLionel Sambuc   std::string ClassName = ClassDecl->getNameAsString();
2143f4a2713aSLionel Sambuc   // Emit the symbol that is used to generate linker errors if this class is
2144f4a2713aSLionel Sambuc   // referenced in other modules but not declared.
2145f4a2713aSLionel Sambuc   std::string classSymbolName = "__objc_class_name_" + ClassName;
2146f4a2713aSLionel Sambuc   if (llvm::GlobalVariable *symbol =
2147f4a2713aSLionel Sambuc       TheModule.getGlobalVariable(classSymbolName)) {
2148f4a2713aSLionel Sambuc     symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
2149f4a2713aSLionel Sambuc   } else {
2150f4a2713aSLionel Sambuc     new llvm::GlobalVariable(TheModule, LongTy, false,
2151f4a2713aSLionel Sambuc     llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0),
2152f4a2713aSLionel Sambuc     classSymbolName);
2153f4a2713aSLionel Sambuc   }
2154f4a2713aSLionel Sambuc 
2155f4a2713aSLionel Sambuc   // Get the size of instances.
2156f4a2713aSLionel Sambuc   int instanceSize =
2157f4a2713aSLionel Sambuc     Context.getASTObjCImplementationLayout(OID).getSize().getQuantity();
2158f4a2713aSLionel Sambuc 
2159f4a2713aSLionel Sambuc   // Collect information about instance variables.
2160f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> IvarNames;
2161f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> IvarTypes;
2162f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> IvarOffsets;
2163f4a2713aSLionel Sambuc 
2164f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> IvarOffsetValues;
2165f4a2713aSLionel Sambuc   SmallVector<bool, 16> WeakIvars;
2166f4a2713aSLionel Sambuc   SmallVector<bool, 16> StrongIvars;
2167f4a2713aSLionel Sambuc 
2168f4a2713aSLionel Sambuc   int superInstanceSize = !SuperClassDecl ? 0 :
2169f4a2713aSLionel Sambuc     Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity();
2170f4a2713aSLionel Sambuc   // For non-fragile ivars, set the instance size to 0 - {the size of just this
2171f4a2713aSLionel Sambuc   // class}.  The runtime will then set this to the correct value on load.
2172f4a2713aSLionel Sambuc   if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2173f4a2713aSLionel Sambuc     instanceSize = 0 - (instanceSize - superInstanceSize);
2174f4a2713aSLionel Sambuc   }
2175f4a2713aSLionel Sambuc 
2176f4a2713aSLionel Sambuc   for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
2177f4a2713aSLionel Sambuc        IVD = IVD->getNextIvar()) {
2178f4a2713aSLionel Sambuc       // Store the name
2179f4a2713aSLionel Sambuc       IvarNames.push_back(MakeConstantString(IVD->getNameAsString()));
2180f4a2713aSLionel Sambuc       // Get the type encoding for this ivar
2181f4a2713aSLionel Sambuc       std::string TypeStr;
2182f4a2713aSLionel Sambuc       Context.getObjCEncodingForType(IVD->getType(), TypeStr);
2183f4a2713aSLionel Sambuc       IvarTypes.push_back(MakeConstantString(TypeStr));
2184f4a2713aSLionel Sambuc       // Get the offset
2185f4a2713aSLionel Sambuc       uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
2186f4a2713aSLionel Sambuc       uint64_t Offset = BaseOffset;
2187f4a2713aSLionel Sambuc       if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2188f4a2713aSLionel Sambuc         Offset = BaseOffset - superInstanceSize;
2189f4a2713aSLionel Sambuc       }
2190f4a2713aSLionel Sambuc       llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset);
2191f4a2713aSLionel Sambuc       // Create the direct offset value
2192f4a2713aSLionel Sambuc       std::string OffsetName = "__objc_ivar_offset_value_" + ClassName +"." +
2193f4a2713aSLionel Sambuc           IVD->getNameAsString();
2194f4a2713aSLionel Sambuc       llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(OffsetName);
2195f4a2713aSLionel Sambuc       if (OffsetVar) {
2196f4a2713aSLionel Sambuc         OffsetVar->setInitializer(OffsetValue);
2197f4a2713aSLionel Sambuc         // If this is the real definition, change its linkage type so that
2198f4a2713aSLionel Sambuc         // different modules will use this one, rather than their private
2199f4a2713aSLionel Sambuc         // copy.
2200f4a2713aSLionel Sambuc         OffsetVar->setLinkage(llvm::GlobalValue::ExternalLinkage);
2201f4a2713aSLionel Sambuc       } else
2202f4a2713aSLionel Sambuc         OffsetVar = new llvm::GlobalVariable(TheModule, IntTy,
2203f4a2713aSLionel Sambuc           false, llvm::GlobalValue::ExternalLinkage,
2204f4a2713aSLionel Sambuc           OffsetValue,
2205f4a2713aSLionel Sambuc           "__objc_ivar_offset_value_" + ClassName +"." +
2206f4a2713aSLionel Sambuc           IVD->getNameAsString());
2207f4a2713aSLionel Sambuc       IvarOffsets.push_back(OffsetValue);
2208f4a2713aSLionel Sambuc       IvarOffsetValues.push_back(OffsetVar);
2209f4a2713aSLionel Sambuc       Qualifiers::ObjCLifetime lt = IVD->getType().getQualifiers().getObjCLifetime();
2210f4a2713aSLionel Sambuc       switch (lt) {
2211f4a2713aSLionel Sambuc         case Qualifiers::OCL_Strong:
2212f4a2713aSLionel Sambuc           StrongIvars.push_back(true);
2213f4a2713aSLionel Sambuc           WeakIvars.push_back(false);
2214f4a2713aSLionel Sambuc           break;
2215f4a2713aSLionel Sambuc         case Qualifiers::OCL_Weak:
2216f4a2713aSLionel Sambuc           StrongIvars.push_back(false);
2217f4a2713aSLionel Sambuc           WeakIvars.push_back(true);
2218f4a2713aSLionel Sambuc           break;
2219f4a2713aSLionel Sambuc         default:
2220f4a2713aSLionel Sambuc           StrongIvars.push_back(false);
2221f4a2713aSLionel Sambuc           WeakIvars.push_back(false);
2222f4a2713aSLionel Sambuc       }
2223f4a2713aSLionel Sambuc   }
2224f4a2713aSLionel Sambuc   llvm::Constant *StrongIvarBitmap = MakeBitField(StrongIvars);
2225f4a2713aSLionel Sambuc   llvm::Constant *WeakIvarBitmap = MakeBitField(WeakIvars);
2226f4a2713aSLionel Sambuc   llvm::GlobalVariable *IvarOffsetArray =
2227f4a2713aSLionel Sambuc     MakeGlobalArray(PtrToIntTy, IvarOffsetValues, ".ivar.offsets");
2228f4a2713aSLionel Sambuc 
2229f4a2713aSLionel Sambuc 
2230f4a2713aSLionel Sambuc   // Collect information about instance methods
2231f4a2713aSLionel Sambuc   SmallVector<Selector, 16> InstanceMethodSels;
2232f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
2233*0a6a1f1dSLionel Sambuc   for (const auto *I : OID->instance_methods()) {
2234*0a6a1f1dSLionel Sambuc     InstanceMethodSels.push_back(I->getSelector());
2235f4a2713aSLionel Sambuc     std::string TypeStr;
2236*0a6a1f1dSLionel Sambuc     Context.getObjCEncodingForMethodDecl(I,TypeStr);
2237f4a2713aSLionel Sambuc     InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
2238f4a2713aSLionel Sambuc   }
2239f4a2713aSLionel Sambuc 
2240f4a2713aSLionel Sambuc   llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels,
2241f4a2713aSLionel Sambuc           InstanceMethodTypes);
2242f4a2713aSLionel Sambuc 
2243f4a2713aSLionel Sambuc 
2244f4a2713aSLionel Sambuc   // Collect information about class methods
2245f4a2713aSLionel Sambuc   SmallVector<Selector, 16> ClassMethodSels;
2246f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 16> ClassMethodTypes;
2247*0a6a1f1dSLionel Sambuc   for (const auto *I : OID->class_methods()) {
2248*0a6a1f1dSLionel Sambuc     ClassMethodSels.push_back(I->getSelector());
2249f4a2713aSLionel Sambuc     std::string TypeStr;
2250*0a6a1f1dSLionel Sambuc     Context.getObjCEncodingForMethodDecl(I,TypeStr);
2251f4a2713aSLionel Sambuc     ClassMethodTypes.push_back(MakeConstantString(TypeStr));
2252f4a2713aSLionel Sambuc   }
2253f4a2713aSLionel Sambuc   // Collect the names of referenced protocols
2254f4a2713aSLionel Sambuc   SmallVector<std::string, 16> Protocols;
2255*0a6a1f1dSLionel Sambuc   for (const auto *I : ClassDecl->protocols())
2256*0a6a1f1dSLionel Sambuc     Protocols.push_back(I->getNameAsString());
2257f4a2713aSLionel Sambuc 
2258f4a2713aSLionel Sambuc   // Get the superclass pointer.
2259f4a2713aSLionel Sambuc   llvm::Constant *SuperClass;
2260f4a2713aSLionel Sambuc   if (!SuperClassName.empty()) {
2261f4a2713aSLionel Sambuc     SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
2262f4a2713aSLionel Sambuc   } else {
2263f4a2713aSLionel Sambuc     SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
2264f4a2713aSLionel Sambuc   }
2265f4a2713aSLionel Sambuc   // Empty vector used to construct empty method lists
2266f4a2713aSLionel Sambuc   SmallVector<llvm::Constant*, 1>  empty;
2267f4a2713aSLionel Sambuc   // Generate the method and instance variable lists
2268f4a2713aSLionel Sambuc   llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
2269f4a2713aSLionel Sambuc       InstanceMethodSels, InstanceMethodTypes, false);
2270f4a2713aSLionel Sambuc   llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
2271f4a2713aSLionel Sambuc       ClassMethodSels, ClassMethodTypes, true);
2272f4a2713aSLionel Sambuc   llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
2273f4a2713aSLionel Sambuc       IvarOffsets);
2274f4a2713aSLionel Sambuc   // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
2275f4a2713aSLionel Sambuc   // we emit a symbol containing the offset for each ivar in the class.  This
2276f4a2713aSLionel Sambuc   // allows code compiled for the non-Fragile ABI to inherit from code compiled
2277f4a2713aSLionel Sambuc   // for the legacy ABI, without causing problems.  The converse is also
2278f4a2713aSLionel Sambuc   // possible, but causes all ivar accesses to be fragile.
2279f4a2713aSLionel Sambuc 
2280f4a2713aSLionel Sambuc   // Offset pointer for getting at the correct field in the ivar list when
2281f4a2713aSLionel Sambuc   // setting up the alias.  These are: The base address for the global, the
2282f4a2713aSLionel Sambuc   // ivar array (second field), the ivar in this list (set for each ivar), and
2283f4a2713aSLionel Sambuc   // the offset (third field in ivar structure)
2284f4a2713aSLionel Sambuc   llvm::Type *IndexTy = Int32Ty;
2285f4a2713aSLionel Sambuc   llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
2286*0a6a1f1dSLionel Sambuc       llvm::ConstantInt::get(IndexTy, 1), nullptr,
2287f4a2713aSLionel Sambuc       llvm::ConstantInt::get(IndexTy, 2) };
2288f4a2713aSLionel Sambuc 
2289f4a2713aSLionel Sambuc   unsigned ivarIndex = 0;
2290f4a2713aSLionel Sambuc   for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
2291f4a2713aSLionel Sambuc        IVD = IVD->getNextIvar()) {
2292f4a2713aSLionel Sambuc       const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
2293f4a2713aSLionel Sambuc           + IVD->getNameAsString();
2294f4a2713aSLionel Sambuc       offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, ivarIndex);
2295f4a2713aSLionel Sambuc       // Get the correct ivar field
2296f4a2713aSLionel Sambuc       llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
2297f4a2713aSLionel Sambuc               IvarList, offsetPointerIndexes);
2298f4a2713aSLionel Sambuc       // Get the existing variable, if one exists.
2299f4a2713aSLionel Sambuc       llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
2300f4a2713aSLionel Sambuc       if (offset) {
2301f4a2713aSLionel Sambuc         offset->setInitializer(offsetValue);
2302f4a2713aSLionel Sambuc         // If this is the real definition, change its linkage type so that
2303f4a2713aSLionel Sambuc         // different modules will use this one, rather than their private
2304f4a2713aSLionel Sambuc         // copy.
2305f4a2713aSLionel Sambuc         offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
2306f4a2713aSLionel Sambuc       } else {
2307f4a2713aSLionel Sambuc         // Add a new alias if there isn't one already.
2308f4a2713aSLionel Sambuc         offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
2309f4a2713aSLionel Sambuc                 false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
2310f4a2713aSLionel Sambuc         (void) offset; // Silence dead store warning.
2311f4a2713aSLionel Sambuc       }
2312f4a2713aSLionel Sambuc       ++ivarIndex;
2313f4a2713aSLionel Sambuc   }
2314f4a2713aSLionel Sambuc   llvm::Constant *ZeroPtr = llvm::ConstantInt::get(IntPtrTy, 0);
2315f4a2713aSLionel Sambuc   //Generate metaclass for class methods
2316f4a2713aSLionel Sambuc   llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
2317*0a6a1f1dSLionel Sambuc       NULLPtr, 0x12L, ClassName.c_str(), nullptr, Zeros[0], GenerateIvarList(
2318f4a2713aSLionel Sambuc         empty, empty, empty), ClassMethodList, NULLPtr,
2319f4a2713aSLionel Sambuc       NULLPtr, NULLPtr, ZeroPtr, ZeroPtr, true);
2320f4a2713aSLionel Sambuc 
2321f4a2713aSLionel Sambuc   // Generate the class structure
2322f4a2713aSLionel Sambuc   llvm::Constant *ClassStruct =
2323f4a2713aSLionel Sambuc     GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L,
2324*0a6a1f1dSLionel Sambuc                            ClassName.c_str(), nullptr,
2325f4a2713aSLionel Sambuc       llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
2326f4a2713aSLionel Sambuc       MethodList, GenerateProtocolList(Protocols), IvarOffsetArray,
2327f4a2713aSLionel Sambuc       Properties, StrongIvarBitmap, WeakIvarBitmap);
2328f4a2713aSLionel Sambuc 
2329f4a2713aSLionel Sambuc   // Resolve the class aliases, if they exist.
2330f4a2713aSLionel Sambuc   if (ClassPtrAlias) {
2331f4a2713aSLionel Sambuc     ClassPtrAlias->replaceAllUsesWith(
2332f4a2713aSLionel Sambuc         llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
2333f4a2713aSLionel Sambuc     ClassPtrAlias->eraseFromParent();
2334*0a6a1f1dSLionel Sambuc     ClassPtrAlias = nullptr;
2335f4a2713aSLionel Sambuc   }
2336f4a2713aSLionel Sambuc   if (MetaClassPtrAlias) {
2337f4a2713aSLionel Sambuc     MetaClassPtrAlias->replaceAllUsesWith(
2338f4a2713aSLionel Sambuc         llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
2339f4a2713aSLionel Sambuc     MetaClassPtrAlias->eraseFromParent();
2340*0a6a1f1dSLionel Sambuc     MetaClassPtrAlias = nullptr;
2341f4a2713aSLionel Sambuc   }
2342f4a2713aSLionel Sambuc 
2343f4a2713aSLionel Sambuc   // Add class structure to list to be added to the symtab later
2344f4a2713aSLionel Sambuc   ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
2345f4a2713aSLionel Sambuc   Classes.push_back(ClassStruct);
2346f4a2713aSLionel Sambuc }
2347f4a2713aSLionel Sambuc 
2348f4a2713aSLionel Sambuc 
ModuleInitFunction()2349f4a2713aSLionel Sambuc llvm::Function *CGObjCGNU::ModuleInitFunction() {
2350f4a2713aSLionel Sambuc   // Only emit an ObjC load function if no Objective-C stuff has been called
2351f4a2713aSLionel Sambuc   if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
2352f4a2713aSLionel Sambuc       ExistingProtocols.empty() && SelectorTable.empty())
2353*0a6a1f1dSLionel Sambuc     return nullptr;
2354f4a2713aSLionel Sambuc 
2355f4a2713aSLionel Sambuc   // Add all referenced protocols to a category.
2356f4a2713aSLionel Sambuc   GenerateProtocolHolderCategory();
2357f4a2713aSLionel Sambuc 
2358f4a2713aSLionel Sambuc   llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
2359f4a2713aSLionel Sambuc           SelectorTy->getElementType());
2360f4a2713aSLionel Sambuc   llvm::Type *SelStructPtrTy = SelectorTy;
2361*0a6a1f1dSLionel Sambuc   if (!SelStructTy) {
2362*0a6a1f1dSLionel Sambuc     SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, nullptr);
2363f4a2713aSLionel Sambuc     SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
2364f4a2713aSLionel Sambuc   }
2365f4a2713aSLionel Sambuc 
2366f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Elements;
2367f4a2713aSLionel Sambuc   llvm::Constant *Statics = NULLPtr;
2368f4a2713aSLionel Sambuc   // Generate statics list:
2369f4a2713aSLionel Sambuc   if (ConstantStrings.size()) {
2370f4a2713aSLionel Sambuc     llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
2371f4a2713aSLionel Sambuc         ConstantStrings.size() + 1);
2372f4a2713aSLionel Sambuc     ConstantStrings.push_back(NULLPtr);
2373f4a2713aSLionel Sambuc 
2374f4a2713aSLionel Sambuc     StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
2375f4a2713aSLionel Sambuc 
2376f4a2713aSLionel Sambuc     if (StringClass.empty()) StringClass = "NXConstantString";
2377f4a2713aSLionel Sambuc 
2378f4a2713aSLionel Sambuc     Elements.push_back(MakeConstantString(StringClass,
2379f4a2713aSLionel Sambuc                 ".objc_static_class_name"));
2380f4a2713aSLionel Sambuc     Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
2381f4a2713aSLionel Sambuc        ConstantStrings));
2382f4a2713aSLionel Sambuc     llvm::StructType *StaticsListTy =
2383*0a6a1f1dSLionel Sambuc       llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, nullptr);
2384f4a2713aSLionel Sambuc     llvm::Type *StaticsListPtrTy =
2385f4a2713aSLionel Sambuc       llvm::PointerType::getUnqual(StaticsListTy);
2386f4a2713aSLionel Sambuc     Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
2387f4a2713aSLionel Sambuc     llvm::ArrayType *StaticsListArrayTy =
2388f4a2713aSLionel Sambuc       llvm::ArrayType::get(StaticsListPtrTy, 2);
2389f4a2713aSLionel Sambuc     Elements.clear();
2390f4a2713aSLionel Sambuc     Elements.push_back(Statics);
2391f4a2713aSLionel Sambuc     Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
2392f4a2713aSLionel Sambuc     Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
2393f4a2713aSLionel Sambuc     Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
2394f4a2713aSLionel Sambuc   }
2395f4a2713aSLionel Sambuc   // Array of classes, categories, and constant objects
2396f4a2713aSLionel Sambuc   llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
2397f4a2713aSLionel Sambuc       Classes.size() + Categories.size()  + 2);
2398f4a2713aSLionel Sambuc   llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
2399f4a2713aSLionel Sambuc                                                      llvm::Type::getInt16Ty(VMContext),
2400f4a2713aSLionel Sambuc                                                      llvm::Type::getInt16Ty(VMContext),
2401*0a6a1f1dSLionel Sambuc                                                      ClassListTy, nullptr);
2402f4a2713aSLionel Sambuc 
2403f4a2713aSLionel Sambuc   Elements.clear();
2404f4a2713aSLionel Sambuc   // Pointer to an array of selectors used in this module.
2405f4a2713aSLionel Sambuc   std::vector<llvm::Constant*> Selectors;
2406f4a2713aSLionel Sambuc   std::vector<llvm::GlobalAlias*> SelectorAliases;
2407f4a2713aSLionel Sambuc   for (SelectorMap::iterator iter = SelectorTable.begin(),
2408f4a2713aSLionel Sambuc       iterEnd = SelectorTable.end(); iter != iterEnd ; ++iter) {
2409f4a2713aSLionel Sambuc 
2410f4a2713aSLionel Sambuc     std::string SelNameStr = iter->first.getAsString();
2411f4a2713aSLionel Sambuc     llvm::Constant *SelName = ExportUniqueString(SelNameStr, ".objc_sel_name");
2412f4a2713aSLionel Sambuc 
2413f4a2713aSLionel Sambuc     SmallVectorImpl<TypedSelector> &Types = iter->second;
2414f4a2713aSLionel Sambuc     for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
2415f4a2713aSLionel Sambuc         e = Types.end() ; i!=e ; i++) {
2416f4a2713aSLionel Sambuc 
2417f4a2713aSLionel Sambuc       llvm::Constant *SelectorTypeEncoding = NULLPtr;
2418f4a2713aSLionel Sambuc       if (!i->first.empty())
2419f4a2713aSLionel Sambuc         SelectorTypeEncoding = MakeConstantString(i->first, ".objc_sel_types");
2420f4a2713aSLionel Sambuc 
2421f4a2713aSLionel Sambuc       Elements.push_back(SelName);
2422f4a2713aSLionel Sambuc       Elements.push_back(SelectorTypeEncoding);
2423f4a2713aSLionel Sambuc       Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
2424f4a2713aSLionel Sambuc       Elements.clear();
2425f4a2713aSLionel Sambuc 
2426f4a2713aSLionel Sambuc       // Store the selector alias for later replacement
2427f4a2713aSLionel Sambuc       SelectorAliases.push_back(i->second);
2428f4a2713aSLionel Sambuc     }
2429f4a2713aSLionel Sambuc   }
2430f4a2713aSLionel Sambuc   unsigned SelectorCount = Selectors.size();
2431f4a2713aSLionel Sambuc   // NULL-terminate the selector list.  This should not actually be required,
2432f4a2713aSLionel Sambuc   // because the selector list has a length field.  Unfortunately, the GCC
2433f4a2713aSLionel Sambuc   // runtime decides to ignore the length field and expects a NULL terminator,
2434f4a2713aSLionel Sambuc   // and GCC cooperates with this by always setting the length to 0.
2435f4a2713aSLionel Sambuc   Elements.push_back(NULLPtr);
2436f4a2713aSLionel Sambuc   Elements.push_back(NULLPtr);
2437f4a2713aSLionel Sambuc   Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
2438f4a2713aSLionel Sambuc   Elements.clear();
2439f4a2713aSLionel Sambuc 
2440f4a2713aSLionel Sambuc   // Number of static selectors
2441f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantInt::get(LongTy, SelectorCount));
2442f4a2713aSLionel Sambuc   llvm::Constant *SelectorList = MakeGlobalArray(SelStructTy, Selectors,
2443f4a2713aSLionel Sambuc           ".objc_selector_list");
2444f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
2445f4a2713aSLionel Sambuc     SelStructPtrTy));
2446f4a2713aSLionel Sambuc 
2447f4a2713aSLionel Sambuc   // Now that all of the static selectors exist, create pointers to them.
2448f4a2713aSLionel Sambuc   for (unsigned int i=0 ; i<SelectorCount ; i++) {
2449f4a2713aSLionel Sambuc 
2450f4a2713aSLionel Sambuc     llvm::Constant *Idxs[] = {Zeros[0],
2451f4a2713aSLionel Sambuc       llvm::ConstantInt::get(Int32Ty, i), Zeros[0]};
2452f4a2713aSLionel Sambuc     // FIXME: We're generating redundant loads and stores here!
2453f4a2713aSLionel Sambuc     llvm::Constant *SelPtr = llvm::ConstantExpr::getGetElementPtr(SelectorList,
2454f4a2713aSLionel Sambuc         makeArrayRef(Idxs, 2));
2455f4a2713aSLionel Sambuc     // If selectors are defined as an opaque type, cast the pointer to this
2456f4a2713aSLionel Sambuc     // type.
2457f4a2713aSLionel Sambuc     SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy);
2458f4a2713aSLionel Sambuc     SelectorAliases[i]->replaceAllUsesWith(SelPtr);
2459f4a2713aSLionel Sambuc     SelectorAliases[i]->eraseFromParent();
2460f4a2713aSLionel Sambuc   }
2461f4a2713aSLionel Sambuc 
2462f4a2713aSLionel Sambuc   // Number of classes defined.
2463f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
2464f4a2713aSLionel Sambuc         Classes.size()));
2465f4a2713aSLionel Sambuc   // Number of categories defined
2466f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
2467f4a2713aSLionel Sambuc         Categories.size()));
2468f4a2713aSLionel Sambuc   // Create an array of classes, then categories, then static object instances
2469f4a2713aSLionel Sambuc   Classes.insert(Classes.end(), Categories.begin(), Categories.end());
2470f4a2713aSLionel Sambuc   //  NULL-terminated list of static object instances (mainly constant strings)
2471f4a2713aSLionel Sambuc   Classes.push_back(Statics);
2472f4a2713aSLionel Sambuc   Classes.push_back(NULLPtr);
2473f4a2713aSLionel Sambuc   llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
2474f4a2713aSLionel Sambuc   Elements.push_back(ClassList);
2475f4a2713aSLionel Sambuc   // Construct the symbol table
2476f4a2713aSLionel Sambuc   llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
2477f4a2713aSLionel Sambuc 
2478f4a2713aSLionel Sambuc   // The symbol table is contained in a module which has some version-checking
2479f4a2713aSLionel Sambuc   // constants
2480f4a2713aSLionel Sambuc   llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
2481f4a2713aSLionel Sambuc       PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy),
2482*0a6a1f1dSLionel Sambuc       (RuntimeVersion >= 10) ? IntTy : nullptr, nullptr);
2483f4a2713aSLionel Sambuc   Elements.clear();
2484f4a2713aSLionel Sambuc   // Runtime version, used for ABI compatibility checking.
2485f4a2713aSLionel Sambuc   Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
2486f4a2713aSLionel Sambuc   // sizeof(ModuleTy)
2487f4a2713aSLionel Sambuc   llvm::DataLayout td(&TheModule);
2488f4a2713aSLionel Sambuc   Elements.push_back(
2489f4a2713aSLionel Sambuc     llvm::ConstantInt::get(LongTy,
2490f4a2713aSLionel Sambuc                            td.getTypeSizeInBits(ModuleTy) /
2491f4a2713aSLionel Sambuc                              CGM.getContext().getCharWidth()));
2492f4a2713aSLionel Sambuc 
2493f4a2713aSLionel Sambuc   // The path to the source file where this module was declared
2494f4a2713aSLionel Sambuc   SourceManager &SM = CGM.getContext().getSourceManager();
2495f4a2713aSLionel Sambuc   const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID());
2496f4a2713aSLionel Sambuc   std::string path =
2497f4a2713aSLionel Sambuc     std::string(mainFile->getDir()->getName()) + '/' + mainFile->getName();
2498f4a2713aSLionel Sambuc   Elements.push_back(MakeConstantString(path, ".objc_source_file_name"));
2499f4a2713aSLionel Sambuc   Elements.push_back(SymTab);
2500f4a2713aSLionel Sambuc 
2501f4a2713aSLionel Sambuc   if (RuntimeVersion >= 10)
2502f4a2713aSLionel Sambuc     switch (CGM.getLangOpts().getGC()) {
2503f4a2713aSLionel Sambuc       case LangOptions::GCOnly:
2504f4a2713aSLionel Sambuc         Elements.push_back(llvm::ConstantInt::get(IntTy, 2));
2505f4a2713aSLionel Sambuc         break;
2506f4a2713aSLionel Sambuc       case LangOptions::NonGC:
2507f4a2713aSLionel Sambuc         if (CGM.getLangOpts().ObjCAutoRefCount)
2508f4a2713aSLionel Sambuc           Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2509f4a2713aSLionel Sambuc         else
2510f4a2713aSLionel Sambuc           Elements.push_back(llvm::ConstantInt::get(IntTy, 0));
2511f4a2713aSLionel Sambuc         break;
2512f4a2713aSLionel Sambuc       case LangOptions::HybridGC:
2513f4a2713aSLionel Sambuc           Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2514f4a2713aSLionel Sambuc         break;
2515f4a2713aSLionel Sambuc     }
2516f4a2713aSLionel Sambuc 
2517f4a2713aSLionel Sambuc   llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
2518f4a2713aSLionel Sambuc 
2519f4a2713aSLionel Sambuc   // Create the load function calling the runtime entry point with the module
2520f4a2713aSLionel Sambuc   // structure
2521f4a2713aSLionel Sambuc   llvm::Function * LoadFunction = llvm::Function::Create(
2522f4a2713aSLionel Sambuc       llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
2523f4a2713aSLionel Sambuc       llvm::GlobalValue::InternalLinkage, ".objc_load_function",
2524f4a2713aSLionel Sambuc       &TheModule);
2525f4a2713aSLionel Sambuc   llvm::BasicBlock *EntryBB =
2526f4a2713aSLionel Sambuc       llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
2527f4a2713aSLionel Sambuc   CGBuilderTy Builder(VMContext);
2528f4a2713aSLionel Sambuc   Builder.SetInsertPoint(EntryBB);
2529f4a2713aSLionel Sambuc 
2530f4a2713aSLionel Sambuc   llvm::FunctionType *FT =
2531f4a2713aSLionel Sambuc     llvm::FunctionType::get(Builder.getVoidTy(),
2532f4a2713aSLionel Sambuc                             llvm::PointerType::getUnqual(ModuleTy), true);
2533f4a2713aSLionel Sambuc   llvm::Value *Register = CGM.CreateRuntimeFunction(FT, "__objc_exec_class");
2534f4a2713aSLionel Sambuc   Builder.CreateCall(Register, Module);
2535f4a2713aSLionel Sambuc 
2536f4a2713aSLionel Sambuc   if (!ClassAliases.empty()) {
2537f4a2713aSLionel Sambuc     llvm::Type *ArgTypes[2] = {PtrTy, PtrToInt8Ty};
2538f4a2713aSLionel Sambuc     llvm::FunctionType *RegisterAliasTy =
2539f4a2713aSLionel Sambuc       llvm::FunctionType::get(Builder.getVoidTy(),
2540f4a2713aSLionel Sambuc                               ArgTypes, false);
2541f4a2713aSLionel Sambuc     llvm::Function *RegisterAlias = llvm::Function::Create(
2542f4a2713aSLionel Sambuc       RegisterAliasTy,
2543f4a2713aSLionel Sambuc       llvm::GlobalValue::ExternalWeakLinkage, "class_registerAlias_np",
2544f4a2713aSLionel Sambuc       &TheModule);
2545f4a2713aSLionel Sambuc     llvm::BasicBlock *AliasBB =
2546f4a2713aSLionel Sambuc       llvm::BasicBlock::Create(VMContext, "alias", LoadFunction);
2547f4a2713aSLionel Sambuc     llvm::BasicBlock *NoAliasBB =
2548f4a2713aSLionel Sambuc       llvm::BasicBlock::Create(VMContext, "no_alias", LoadFunction);
2549f4a2713aSLionel Sambuc 
2550f4a2713aSLionel Sambuc     // Branch based on whether the runtime provided class_registerAlias_np()
2551f4a2713aSLionel Sambuc     llvm::Value *HasRegisterAlias = Builder.CreateICmpNE(RegisterAlias,
2552f4a2713aSLionel Sambuc             llvm::Constant::getNullValue(RegisterAlias->getType()));
2553f4a2713aSLionel Sambuc     Builder.CreateCondBr(HasRegisterAlias, AliasBB, NoAliasBB);
2554f4a2713aSLionel Sambuc 
2555*0a6a1f1dSLionel Sambuc     // The true branch (has alias registration function):
2556f4a2713aSLionel Sambuc     Builder.SetInsertPoint(AliasBB);
2557f4a2713aSLionel Sambuc     // Emit alias registration calls:
2558f4a2713aSLionel Sambuc     for (std::vector<ClassAliasPair>::iterator iter = ClassAliases.begin();
2559f4a2713aSLionel Sambuc        iter != ClassAliases.end(); ++iter) {
2560f4a2713aSLionel Sambuc        llvm::Constant *TheClass =
2561f4a2713aSLionel Sambuc          TheModule.getGlobalVariable(("_OBJC_CLASS_" + iter->first).c_str(),
2562f4a2713aSLionel Sambuc             true);
2563*0a6a1f1dSLionel Sambuc        if (TheClass) {
2564f4a2713aSLionel Sambuc          TheClass = llvm::ConstantExpr::getBitCast(TheClass, PtrTy);
2565f4a2713aSLionel Sambuc          Builder.CreateCall2(RegisterAlias, TheClass,
2566f4a2713aSLionel Sambuc             MakeConstantString(iter->second));
2567f4a2713aSLionel Sambuc        }
2568f4a2713aSLionel Sambuc     }
2569f4a2713aSLionel Sambuc     // Jump to end:
2570f4a2713aSLionel Sambuc     Builder.CreateBr(NoAliasBB);
2571f4a2713aSLionel Sambuc 
2572f4a2713aSLionel Sambuc     // Missing alias registration function, just return from the function:
2573f4a2713aSLionel Sambuc     Builder.SetInsertPoint(NoAliasBB);
2574f4a2713aSLionel Sambuc   }
2575f4a2713aSLionel Sambuc   Builder.CreateRetVoid();
2576f4a2713aSLionel Sambuc 
2577f4a2713aSLionel Sambuc   return LoadFunction;
2578f4a2713aSLionel Sambuc }
2579f4a2713aSLionel Sambuc 
GenerateMethod(const ObjCMethodDecl * OMD,const ObjCContainerDecl * CD)2580f4a2713aSLionel Sambuc llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
2581f4a2713aSLionel Sambuc                                           const ObjCContainerDecl *CD) {
2582f4a2713aSLionel Sambuc   const ObjCCategoryImplDecl *OCD =
2583f4a2713aSLionel Sambuc     dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
2584f4a2713aSLionel Sambuc   StringRef CategoryName = OCD ? OCD->getName() : "";
2585f4a2713aSLionel Sambuc   StringRef ClassName = CD->getName();
2586f4a2713aSLionel Sambuc   Selector MethodName = OMD->getSelector();
2587f4a2713aSLionel Sambuc   bool isClassMethod = !OMD->isInstanceMethod();
2588f4a2713aSLionel Sambuc 
2589f4a2713aSLionel Sambuc   CodeGenTypes &Types = CGM.getTypes();
2590f4a2713aSLionel Sambuc   llvm::FunctionType *MethodTy =
2591f4a2713aSLionel Sambuc     Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
2592f4a2713aSLionel Sambuc   std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
2593f4a2713aSLionel Sambuc       MethodName, isClassMethod);
2594f4a2713aSLionel Sambuc 
2595f4a2713aSLionel Sambuc   llvm::Function *Method
2596f4a2713aSLionel Sambuc     = llvm::Function::Create(MethodTy,
2597f4a2713aSLionel Sambuc                              llvm::GlobalValue::InternalLinkage,
2598f4a2713aSLionel Sambuc                              FunctionName,
2599f4a2713aSLionel Sambuc                              &TheModule);
2600f4a2713aSLionel Sambuc   return Method;
2601f4a2713aSLionel Sambuc }
2602f4a2713aSLionel Sambuc 
GetPropertyGetFunction()2603f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GetPropertyGetFunction() {
2604f4a2713aSLionel Sambuc   return GetPropertyFn;
2605f4a2713aSLionel Sambuc }
2606f4a2713aSLionel Sambuc 
GetPropertySetFunction()2607f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GetPropertySetFunction() {
2608f4a2713aSLionel Sambuc   return SetPropertyFn;
2609f4a2713aSLionel Sambuc }
2610f4a2713aSLionel Sambuc 
GetOptimizedPropertySetFunction(bool atomic,bool copy)2611f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GetOptimizedPropertySetFunction(bool atomic,
2612f4a2713aSLionel Sambuc                                                            bool copy) {
2613*0a6a1f1dSLionel Sambuc   return nullptr;
2614f4a2713aSLionel Sambuc }
2615f4a2713aSLionel Sambuc 
GetGetStructFunction()2616f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GetGetStructFunction() {
2617f4a2713aSLionel Sambuc   return GetStructPropertyFn;
2618f4a2713aSLionel Sambuc }
GetSetStructFunction()2619f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GetSetStructFunction() {
2620f4a2713aSLionel Sambuc   return SetStructPropertyFn;
2621f4a2713aSLionel Sambuc }
GetCppAtomicObjectGetFunction()2622f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GetCppAtomicObjectGetFunction() {
2623*0a6a1f1dSLionel Sambuc   return nullptr;
2624f4a2713aSLionel Sambuc }
GetCppAtomicObjectSetFunction()2625f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::GetCppAtomicObjectSetFunction() {
2626*0a6a1f1dSLionel Sambuc   return nullptr;
2627f4a2713aSLionel Sambuc }
2628f4a2713aSLionel Sambuc 
EnumerationMutationFunction()2629f4a2713aSLionel Sambuc llvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
2630f4a2713aSLionel Sambuc   return EnumerationMutationFn;
2631f4a2713aSLionel Sambuc }
2632f4a2713aSLionel Sambuc 
EmitSynchronizedStmt(CodeGenFunction & CGF,const ObjCAtSynchronizedStmt & S)2633f4a2713aSLionel Sambuc void CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF,
2634f4a2713aSLionel Sambuc                                      const ObjCAtSynchronizedStmt &S) {
2635f4a2713aSLionel Sambuc   EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn);
2636f4a2713aSLionel Sambuc }
2637f4a2713aSLionel Sambuc 
2638f4a2713aSLionel Sambuc 
EmitTryStmt(CodeGenFunction & CGF,const ObjCAtTryStmt & S)2639f4a2713aSLionel Sambuc void CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF,
2640f4a2713aSLionel Sambuc                             const ObjCAtTryStmt &S) {
2641f4a2713aSLionel Sambuc   // Unlike the Apple non-fragile runtimes, which also uses
2642f4a2713aSLionel Sambuc   // unwind-based zero cost exceptions, the GNU Objective C runtime's
2643f4a2713aSLionel Sambuc   // EH support isn't a veneer over C++ EH.  Instead, exception
2644f4a2713aSLionel Sambuc   // objects are created by objc_exception_throw and destroyed by
2645f4a2713aSLionel Sambuc   // the personality function; this avoids the need for bracketing
2646f4a2713aSLionel Sambuc   // catch handlers with calls to __blah_begin_catch/__blah_end_catch
2647f4a2713aSLionel Sambuc   // (or even _Unwind_DeleteException), but probably doesn't
2648f4a2713aSLionel Sambuc   // interoperate very well with foreign exceptions.
2649f4a2713aSLionel Sambuc   //
2650f4a2713aSLionel Sambuc   // In Objective-C++ mode, we actually emit something equivalent to the C++
2651f4a2713aSLionel Sambuc   // exception handler.
2652f4a2713aSLionel Sambuc   EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn);
2653f4a2713aSLionel Sambuc   return ;
2654f4a2713aSLionel Sambuc }
2655f4a2713aSLionel Sambuc 
EmitThrowStmt(CodeGenFunction & CGF,const ObjCAtThrowStmt & S,bool ClearInsertionPoint)2656f4a2713aSLionel Sambuc void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
2657f4a2713aSLionel Sambuc                               const ObjCAtThrowStmt &S,
2658f4a2713aSLionel Sambuc                               bool ClearInsertionPoint) {
2659f4a2713aSLionel Sambuc   llvm::Value *ExceptionAsObject;
2660f4a2713aSLionel Sambuc 
2661f4a2713aSLionel Sambuc   if (const Expr *ThrowExpr = S.getThrowExpr()) {
2662f4a2713aSLionel Sambuc     llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
2663f4a2713aSLionel Sambuc     ExceptionAsObject = Exception;
2664f4a2713aSLionel Sambuc   } else {
2665f4a2713aSLionel Sambuc     assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
2666f4a2713aSLionel Sambuc            "Unexpected rethrow outside @catch block.");
2667f4a2713aSLionel Sambuc     ExceptionAsObject = CGF.ObjCEHValueStack.back();
2668f4a2713aSLionel Sambuc   }
2669f4a2713aSLionel Sambuc   ExceptionAsObject = CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy);
2670f4a2713aSLionel Sambuc   llvm::CallSite Throw =
2671f4a2713aSLionel Sambuc       CGF.EmitRuntimeCallOrInvoke(ExceptionThrowFn, ExceptionAsObject);
2672f4a2713aSLionel Sambuc   Throw.setDoesNotReturn();
2673f4a2713aSLionel Sambuc   CGF.Builder.CreateUnreachable();
2674f4a2713aSLionel Sambuc   if (ClearInsertionPoint)
2675f4a2713aSLionel Sambuc     CGF.Builder.ClearInsertionPoint();
2676f4a2713aSLionel Sambuc }
2677f4a2713aSLionel Sambuc 
EmitObjCWeakRead(CodeGenFunction & CGF,llvm::Value * AddrWeakObj)2678f4a2713aSLionel Sambuc llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF,
2679f4a2713aSLionel Sambuc                                           llvm::Value *AddrWeakObj) {
2680f4a2713aSLionel Sambuc   CGBuilderTy &B = CGF.Builder;
2681f4a2713aSLionel Sambuc   AddrWeakObj = EnforceType(B, AddrWeakObj, PtrToIdTy);
2682f4a2713aSLionel Sambuc   return B.CreateCall(WeakReadFn, AddrWeakObj);
2683f4a2713aSLionel Sambuc }
2684f4a2713aSLionel Sambuc 
EmitObjCWeakAssign(CodeGenFunction & CGF,llvm::Value * src,llvm::Value * dst)2685f4a2713aSLionel Sambuc void CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF,
2686f4a2713aSLionel Sambuc                                    llvm::Value *src, llvm::Value *dst) {
2687f4a2713aSLionel Sambuc   CGBuilderTy &B = CGF.Builder;
2688f4a2713aSLionel Sambuc   src = EnforceType(B, src, IdTy);
2689f4a2713aSLionel Sambuc   dst = EnforceType(B, dst, PtrToIdTy);
2690f4a2713aSLionel Sambuc   B.CreateCall2(WeakAssignFn, src, dst);
2691f4a2713aSLionel Sambuc }
2692f4a2713aSLionel Sambuc 
EmitObjCGlobalAssign(CodeGenFunction & CGF,llvm::Value * src,llvm::Value * dst,bool threadlocal)2693f4a2713aSLionel Sambuc void CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF,
2694f4a2713aSLionel Sambuc                                      llvm::Value *src, llvm::Value *dst,
2695f4a2713aSLionel Sambuc                                      bool threadlocal) {
2696f4a2713aSLionel Sambuc   CGBuilderTy &B = CGF.Builder;
2697f4a2713aSLionel Sambuc   src = EnforceType(B, src, IdTy);
2698f4a2713aSLionel Sambuc   dst = EnforceType(B, dst, PtrToIdTy);
2699f4a2713aSLionel Sambuc   if (!threadlocal)
2700f4a2713aSLionel Sambuc     B.CreateCall2(GlobalAssignFn, src, dst);
2701f4a2713aSLionel Sambuc   else
2702f4a2713aSLionel Sambuc     // FIXME. Add threadloca assign API
2703f4a2713aSLionel Sambuc     llvm_unreachable("EmitObjCGlobalAssign - Threal Local API NYI");
2704f4a2713aSLionel Sambuc }
2705f4a2713aSLionel Sambuc 
EmitObjCIvarAssign(CodeGenFunction & CGF,llvm::Value * src,llvm::Value * dst,llvm::Value * ivarOffset)2706f4a2713aSLionel Sambuc void CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF,
2707f4a2713aSLionel Sambuc                                    llvm::Value *src, llvm::Value *dst,
2708f4a2713aSLionel Sambuc                                    llvm::Value *ivarOffset) {
2709f4a2713aSLionel Sambuc   CGBuilderTy &B = CGF.Builder;
2710f4a2713aSLionel Sambuc   src = EnforceType(B, src, IdTy);
2711f4a2713aSLionel Sambuc   dst = EnforceType(B, dst, IdTy);
2712f4a2713aSLionel Sambuc   B.CreateCall3(IvarAssignFn, src, dst, ivarOffset);
2713f4a2713aSLionel Sambuc }
2714f4a2713aSLionel Sambuc 
EmitObjCStrongCastAssign(CodeGenFunction & CGF,llvm::Value * src,llvm::Value * dst)2715f4a2713aSLionel Sambuc void CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF,
2716f4a2713aSLionel Sambuc                                          llvm::Value *src, llvm::Value *dst) {
2717f4a2713aSLionel Sambuc   CGBuilderTy &B = CGF.Builder;
2718f4a2713aSLionel Sambuc   src = EnforceType(B, src, IdTy);
2719f4a2713aSLionel Sambuc   dst = EnforceType(B, dst, PtrToIdTy);
2720f4a2713aSLionel Sambuc   B.CreateCall2(StrongCastAssignFn, src, dst);
2721f4a2713aSLionel Sambuc }
2722f4a2713aSLionel Sambuc 
EmitGCMemmoveCollectable(CodeGenFunction & CGF,llvm::Value * DestPtr,llvm::Value * SrcPtr,llvm::Value * Size)2723f4a2713aSLionel Sambuc void CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF,
2724f4a2713aSLionel Sambuc                                          llvm::Value *DestPtr,
2725f4a2713aSLionel Sambuc                                          llvm::Value *SrcPtr,
2726f4a2713aSLionel Sambuc                                          llvm::Value *Size) {
2727f4a2713aSLionel Sambuc   CGBuilderTy &B = CGF.Builder;
2728f4a2713aSLionel Sambuc   DestPtr = EnforceType(B, DestPtr, PtrTy);
2729f4a2713aSLionel Sambuc   SrcPtr = EnforceType(B, SrcPtr, PtrTy);
2730f4a2713aSLionel Sambuc 
2731f4a2713aSLionel Sambuc   B.CreateCall3(MemMoveFn, DestPtr, SrcPtr, Size);
2732f4a2713aSLionel Sambuc }
2733f4a2713aSLionel Sambuc 
ObjCIvarOffsetVariable(const ObjCInterfaceDecl * ID,const ObjCIvarDecl * Ivar)2734f4a2713aSLionel Sambuc llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
2735f4a2713aSLionel Sambuc                               const ObjCInterfaceDecl *ID,
2736f4a2713aSLionel Sambuc                               const ObjCIvarDecl *Ivar) {
2737f4a2713aSLionel Sambuc   const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
2738f4a2713aSLionel Sambuc     + '.' + Ivar->getNameAsString();
2739f4a2713aSLionel Sambuc   // Emit the variable and initialize it with what we think the correct value
2740f4a2713aSLionel Sambuc   // is.  This allows code compiled with non-fragile ivars to work correctly
2741f4a2713aSLionel Sambuc   // when linked against code which isn't (most of the time).
2742f4a2713aSLionel Sambuc   llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
2743f4a2713aSLionel Sambuc   if (!IvarOffsetPointer) {
2744f4a2713aSLionel Sambuc     // This will cause a run-time crash if we accidentally use it.  A value of
2745f4a2713aSLionel Sambuc     // 0 would seem more sensible, but will silently overwrite the isa pointer
2746f4a2713aSLionel Sambuc     // causing a great deal of confusion.
2747f4a2713aSLionel Sambuc     uint64_t Offset = -1;
2748f4a2713aSLionel Sambuc     // We can't call ComputeIvarBaseOffset() here if we have the
2749f4a2713aSLionel Sambuc     // implementation, because it will create an invalid ASTRecordLayout object
2750f4a2713aSLionel Sambuc     // that we are then stuck with forever, so we only initialize the ivar
2751f4a2713aSLionel Sambuc     // offset variable with a guess if we only have the interface.  The
2752f4a2713aSLionel Sambuc     // initializer will be reset later anyway, when we are generating the class
2753f4a2713aSLionel Sambuc     // description.
2754f4a2713aSLionel Sambuc     if (!CGM.getContext().getObjCImplementation(
2755f4a2713aSLionel Sambuc               const_cast<ObjCInterfaceDecl *>(ID)))
2756f4a2713aSLionel Sambuc       Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
2757f4a2713aSLionel Sambuc 
2758f4a2713aSLionel Sambuc     llvm::ConstantInt *OffsetGuess = llvm::ConstantInt::get(Int32Ty, Offset,
2759f4a2713aSLionel Sambuc                              /*isSigned*/true);
2760f4a2713aSLionel Sambuc     // Don't emit the guess in non-PIC code because the linker will not be able
2761f4a2713aSLionel Sambuc     // to replace it with the real version for a library.  In non-PIC code you
2762f4a2713aSLionel Sambuc     // must compile with the fragile ABI if you want to use ivars from a
2763f4a2713aSLionel Sambuc     // GCC-compiled class.
2764f4a2713aSLionel Sambuc     if (CGM.getLangOpts().PICLevel || CGM.getLangOpts().PIELevel) {
2765f4a2713aSLionel Sambuc       llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
2766f4a2713aSLionel Sambuc             Int32Ty, false,
2767f4a2713aSLionel Sambuc             llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
2768f4a2713aSLionel Sambuc       IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
2769f4a2713aSLionel Sambuc             IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
2770f4a2713aSLionel Sambuc             IvarOffsetGV, Name);
2771f4a2713aSLionel Sambuc     } else {
2772f4a2713aSLionel Sambuc       IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
2773f4a2713aSLionel Sambuc               llvm::Type::getInt32PtrTy(VMContext), false,
2774*0a6a1f1dSLionel Sambuc               llvm::GlobalValue::ExternalLinkage, nullptr, Name);
2775f4a2713aSLionel Sambuc     }
2776f4a2713aSLionel Sambuc   }
2777f4a2713aSLionel Sambuc   return IvarOffsetPointer;
2778f4a2713aSLionel Sambuc }
2779f4a2713aSLionel Sambuc 
EmitObjCValueForIvar(CodeGenFunction & CGF,QualType ObjectTy,llvm::Value * BaseValue,const ObjCIvarDecl * Ivar,unsigned CVRQualifiers)2780f4a2713aSLionel Sambuc LValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF,
2781f4a2713aSLionel Sambuc                                        QualType ObjectTy,
2782f4a2713aSLionel Sambuc                                        llvm::Value *BaseValue,
2783f4a2713aSLionel Sambuc                                        const ObjCIvarDecl *Ivar,
2784f4a2713aSLionel Sambuc                                        unsigned CVRQualifiers) {
2785f4a2713aSLionel Sambuc   const ObjCInterfaceDecl *ID =
2786f4a2713aSLionel Sambuc     ObjectTy->getAs<ObjCObjectType>()->getInterface();
2787f4a2713aSLionel Sambuc   return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2788f4a2713aSLionel Sambuc                                   EmitIvarOffset(CGF, ID, Ivar));
2789f4a2713aSLionel Sambuc }
2790f4a2713aSLionel Sambuc 
FindIvarInterface(ASTContext & Context,const ObjCInterfaceDecl * OID,const ObjCIvarDecl * OIVD)2791f4a2713aSLionel Sambuc static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
2792f4a2713aSLionel Sambuc                                                   const ObjCInterfaceDecl *OID,
2793f4a2713aSLionel Sambuc                                                   const ObjCIvarDecl *OIVD) {
2794f4a2713aSLionel Sambuc   for (const ObjCIvarDecl *next = OID->all_declared_ivar_begin(); next;
2795f4a2713aSLionel Sambuc        next = next->getNextIvar()) {
2796f4a2713aSLionel Sambuc     if (OIVD == next)
2797f4a2713aSLionel Sambuc       return OID;
2798f4a2713aSLionel Sambuc   }
2799f4a2713aSLionel Sambuc 
2800f4a2713aSLionel Sambuc   // Otherwise check in the super class.
2801f4a2713aSLionel Sambuc   if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
2802f4a2713aSLionel Sambuc     return FindIvarInterface(Context, Super, OIVD);
2803f4a2713aSLionel Sambuc 
2804*0a6a1f1dSLionel Sambuc   return nullptr;
2805f4a2713aSLionel Sambuc }
2806f4a2713aSLionel Sambuc 
EmitIvarOffset(CodeGenFunction & CGF,const ObjCInterfaceDecl * Interface,const ObjCIvarDecl * Ivar)2807f4a2713aSLionel Sambuc llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF,
2808f4a2713aSLionel Sambuc                          const ObjCInterfaceDecl *Interface,
2809f4a2713aSLionel Sambuc                          const ObjCIvarDecl *Ivar) {
2810f4a2713aSLionel Sambuc   if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2811f4a2713aSLionel Sambuc     Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
2812f4a2713aSLionel Sambuc     if (RuntimeVersion < 10)
2813f4a2713aSLionel Sambuc       return CGF.Builder.CreateZExtOrBitCast(
2814f4a2713aSLionel Sambuc           CGF.Builder.CreateLoad(CGF.Builder.CreateLoad(
2815f4a2713aSLionel Sambuc                   ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar")),
2816f4a2713aSLionel Sambuc           PtrDiffTy);
2817f4a2713aSLionel Sambuc     std::string name = "__objc_ivar_offset_value_" +
2818f4a2713aSLionel Sambuc       Interface->getNameAsString() +"." + Ivar->getNameAsString();
2819f4a2713aSLionel Sambuc     llvm::Value *Offset = TheModule.getGlobalVariable(name);
2820f4a2713aSLionel Sambuc     if (!Offset)
2821f4a2713aSLionel Sambuc       Offset = new llvm::GlobalVariable(TheModule, IntTy,
2822f4a2713aSLionel Sambuc           false, llvm::GlobalValue::LinkOnceAnyLinkage,
2823f4a2713aSLionel Sambuc           llvm::Constant::getNullValue(IntTy), name);
2824f4a2713aSLionel Sambuc     Offset = CGF.Builder.CreateLoad(Offset);
2825f4a2713aSLionel Sambuc     if (Offset->getType() != PtrDiffTy)
2826f4a2713aSLionel Sambuc       Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy);
2827f4a2713aSLionel Sambuc     return Offset;
2828f4a2713aSLionel Sambuc   }
2829f4a2713aSLionel Sambuc   uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
2830f4a2713aSLionel Sambuc   return llvm::ConstantInt::get(PtrDiffTy, Offset, /*isSigned*/true);
2831f4a2713aSLionel Sambuc }
2832f4a2713aSLionel Sambuc 
2833f4a2713aSLionel Sambuc CGObjCRuntime *
CreateGNUObjCRuntime(CodeGenModule & CGM)2834f4a2713aSLionel Sambuc clang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) {
2835f4a2713aSLionel Sambuc   switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
2836f4a2713aSLionel Sambuc   case ObjCRuntime::GNUstep:
2837f4a2713aSLionel Sambuc     return new CGObjCGNUstep(CGM);
2838f4a2713aSLionel Sambuc 
2839f4a2713aSLionel Sambuc   case ObjCRuntime::GCC:
2840f4a2713aSLionel Sambuc     return new CGObjCGCC(CGM);
2841f4a2713aSLionel Sambuc 
2842f4a2713aSLionel Sambuc   case ObjCRuntime::ObjFW:
2843f4a2713aSLionel Sambuc     return new CGObjCObjFW(CGM);
2844f4a2713aSLionel Sambuc 
2845f4a2713aSLionel Sambuc   case ObjCRuntime::FragileMacOSX:
2846f4a2713aSLionel Sambuc   case ObjCRuntime::MacOSX:
2847f4a2713aSLionel Sambuc   case ObjCRuntime::iOS:
2848f4a2713aSLionel Sambuc     llvm_unreachable("these runtimes are not GNU runtimes");
2849f4a2713aSLionel Sambuc   }
2850f4a2713aSLionel Sambuc   llvm_unreachable("bad runtime");
2851f4a2713aSLionel Sambuc }
2852