120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
20f984268b05edab2cc555a427c441baa9c252658Chris Lattner//
30f984268b05edab2cc555a427c441baa9c252658Chris Lattner//                     The LLVM Compiler Infrastructure
40f984268b05edab2cc555a427c441baa9c252658Chris Lattner//
50f984268b05edab2cc555a427c441baa9c252658Chris Lattner// This file is distributed under the University of Illinois Open Source
60f984268b05edab2cc555a427c441baa9c252658Chris Lattner// License. See LICENSE.TXT for details.
70f984268b05edab2cc555a427c441baa9c252658Chris Lattner//
80f984268b05edab2cc555a427c441baa9c252658Chris Lattner//===----------------------------------------------------------------------===//
90f984268b05edab2cc555a427c441baa9c252658Chris Lattner//
10fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner// This provides Objective-C code generation targeting the GNU runtime.  The
1120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov// class in this file generates structures used by the GNU Objective-C runtime
1220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov// library.  These structures are defined in objc/objc.h and objc/objc-api.h in
1320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov// the GNU runtime distribution.
140f984268b05edab2cc555a427c441baa9c252658Chris Lattner//
150f984268b05edab2cc555a427c441baa9c252658Chris Lattner//===----------------------------------------------------------------------===//
160f984268b05edab2cc555a427c441baa9c252658Chris Lattner
170f984268b05edab2cc555a427c441baa9c252658Chris Lattner#include "CGObjCRuntime.h"
1836f893c1efe367f929d92c8b125f964c22ba189eJohn McCall#include "CGCleanup.h"
1955fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CodeGenFunction.h"
2055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CodeGenModule.h"
21dce1406f1c1f572cfd61c494546572d63461c741Chris Lattner#include "clang/AST/ASTContext.h"
22e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Decl.h"
23af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "clang/AST/DeclObjC.h"
2419cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
2516f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h"
269f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall#include "clang/Basic/FileManager.h"
2755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/SourceManager.h"
280f984268b05edab2cc555a427c441baa9c252658Chris Lattner#include "llvm/ADT/SmallVector.h"
2920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/ADT/StringMap.h"
30651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "llvm/IR/CallSite.h"
313b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h"
323b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Intrinsics.h"
333b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/LLVMContext.h"
343b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Module.h"
357ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar#include "llvm/Support/Compiler.h"
365f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner#include <cstdarg>
37e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner
38e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner
39dce1406f1c1f572cfd61c494546572d63461c741Chris Lattnerusing namespace clang;
4046f45b9bec4a265ad8400a538e5ec3a5683617f1Daniel Dunbarusing namespace CodeGen;
4120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
420f984268b05edab2cc555a427c441baa9c252658Chris Lattner
430f984268b05edab2cc555a427c441baa9c252658Chris Lattnernamespace {
4481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// Class that lazily initialises the runtime function.  Avoids inserting the
4581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// types and the function declaration into a module if they're not used, and
4681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// avoids constructing the type more than once if it's used more than once.
479f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclass LazyRuntimeFunction {
489f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  CodeGenModule *CGM;
499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  std::vector<llvm::Type*> ArgTys;
509f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  const char *FunctionName;
51789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall  llvm::Constant *Function;
529f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  public:
5381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// Constructor leaves this class uninitialized, because it is intended to
5481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// be used as a field in another class and not all of the types that are
5581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// used as arguments will necessarily be available at construction time.
566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    LazyRuntimeFunction()
576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      : CGM(nullptr), FunctionName(nullptr), Function(nullptr) {}
589f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
5981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// Initialises the lazy function with the name, return type, and the types
6081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// of the arguments.
619f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    END_WITH_NULL
629f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    void init(CodeGenModule *Mod, const char *name,
639cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner        llvm::Type *RetTy, ...) {
649f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       CGM =Mod;
659f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       FunctionName = name;
666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines       Function = nullptr;
679735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       ArgTys.clear();
689f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       va_list Args;
699f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       va_start(Args, RetTy);
709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner         while (llvm::Type *ArgTy = va_arg(Args, llvm::Type*))
719f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall           ArgTys.push_back(ArgTy);
729f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       va_end(Args);
739f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       // Push the return type on at the end so we can pop it off easily
749f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       ArgTys.push_back(RetTy);
759f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall   }
7681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall   /// Overloaded cast operator, allows the class to be implicitly cast to an
7781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall   /// LLVM constant.
78789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall   operator llvm::Constant*() {
799f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall     if (!Function) {
806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines       if (!FunctionName) return nullptr;
819735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       // We put the return type on the end of the vector, so pop it back off
822acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner       llvm::Type *RetTy = ArgTys.back();
839f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       ArgTys.pop_back();
849f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       llvm::FunctionType *FTy = llvm::FunctionType::get(RetTy, ArgTys, false);
859f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       Function =
86789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall         cast<llvm::Constant>(CGM->CreateRuntimeFunction(FTy, FunctionName));
879735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       // We won't need to use the types again, so we may as well clean up the
889735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall       // vector now
899f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall       ArgTys.resize(0);
909f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall     }
919f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall     return Function;
929f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall   }
93789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall   operator llvm::Function*() {
945f0bcc4c3f4a75375a3571e71bffebf603f4ca67David Chisnall     return cast<llvm::Function>((llvm::Constant*)*this);
95789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnall   }
965f0bcc4c3f4a75375a3571e71bffebf603f4ca67David Chisnall
979f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall};
989f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
999f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
10081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// GNU Objective-C runtime code generation.  This class implements the parts of
101f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall/// Objective-C support that are specific to the GNU family of runtimes (GCC,
102f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall/// GNUstep and ObjFW).
1039f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclass CGObjCGNU : public CGObjCRuntime {
104c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnallprotected:
10581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The LLVM module into which output is inserted
1060f984268b05edab2cc555a427c441baa9c252658Chris Lattner  llvm::Module &TheModule;
10781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// strut objc_super.  Used for sending messages to super.  This structure
10881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// contains the receiver (object) and the expected class.
1092acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::StructType *ObjCSuperTy;
11081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// struct objc_super*.  The type of the argument to the superclass message
11181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// lookup functions.
1122acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::PointerType *PtrToObjCSuperTy;
11381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for selectors.  Opaque pointer (i8*) unless a header declaring
11481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// SEL is included in a header somewhere, in which case it will be whatever
11581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// type is declared in that header, most likely {i8*, i8*}.
1169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *SelectorTy;
11781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM i8 type.  Cached here to avoid repeatedly getting it in all of the
11881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// places where it's used
1192acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::IntegerType *Int8Ty;
12081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Pointer to i8 - LLVM type of char*, for all of the places where the
12181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// runtime needs to deal with C strings.
1229cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *PtrToInt8Ty;
12381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Instance Method Pointer type.  This is a pointer to a function that takes,
12481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// at a minimum, an object and a selector, and is the generic type for
12581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Objective-C methods.  Due to differences between variadic / non-variadic
12681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// calling conventions, it must always be cast to the correct type before
12781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// actually being used.
1289cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *IMPTy;
12981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Type of an untyped Objective-C object.  Clang treats id as a built-in type
13081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// when compiling Objective-C code, so this may be an opaque pointer (i8*),
13181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// but if the runtime header declaring it is included then it may be a
13281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// pointer to a structure.
1339cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *IdTy;
13481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Pointer to a pointer to an Objective-C object.  Used in the new ABI
13581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// message lookup function and some GC-related functions.
1362acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::PointerType *PtrToIdTy;
13781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The clang type of id.  Used when using the clang CGCall infrastructure to
13881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// call Objective-C methods.
139ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  CanQualType ASTIdTy;
14081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C int type.
1419cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::IntegerType *IntTy;
14281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for an opaque pointer.  This is identical to PtrToInt8Ty, but is
14381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// used in the code to document the difference between i8* meaning a pointer
14481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// to a C string and i8* meaning a pointer to some opaque type.
1459cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::PointerType *PtrTy;
14681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C long type.  The runtime uses this in a lot of places where
14781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// it should be using intptr_t, but we can't fix this without breaking
14881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// compatibility with GCC...
149ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  llvm::IntegerType *LongTy;
15081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C size_t.  Used in various runtime data structures.
1512acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::IntegerType *SizeTy;
15249de5289acea91c9d224c9aadd4d527e8f9fdaffDavid Chisnall  /// LLVM type for C intptr_t.
15349de5289acea91c9d224c9aadd4d527e8f9fdaffDavid Chisnall  llvm::IntegerType *IntPtrTy;
15481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C ptrdiff_t.  Mainly used in property accessor functions.
1552acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::IntegerType *PtrDiffTy;
15681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for C int*.  Used for GCC-ABI-compatible non-fragile instance
15781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// variables.
1582acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::PointerType *PtrToIntTy;
15981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM type for Objective-C BOOL type.
1602acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *BoolTy;
161917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// 32-bit integer type, to save us needing to look it up every time it's used.
162917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  llvm::IntegerType *Int32Ty;
163917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// 64-bit integer type, to save us needing to look it up every time it's used.
164917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  llvm::IntegerType *Int64Ty;
16581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Metadata kind used to tie method lookups to message sends.  The GNUstep
16681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// runtime provides some LLVM passes that can use this to do things like
16781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// automatic IMP caching and speculative inlining.
168c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  unsigned msgSendMDKind;
16981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Helper function that generates a constant string and returns a pointer to
17081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// the start of the string.  The result of this function can be used anywhere
17181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// where the C code specifies const char*.
1729735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  llvm::Constant *MakeConstantString(const std::string &Str,
1739735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                     const std::string &Name="") {
1749735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
175a5c04344fa70d6eec34344760c1fe511e16f2d76Jay Foad    return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros);
1769735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
17781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Emits a linkonce_odr string, whose name is the prefix followed by the
17881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// string value.  This allows the linker to combine the strings between
17981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// different modules.  Used for EH typeinfo names, selector strings, and a
18081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// few other things.
1819735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  llvm::Constant *ExportUniqueString(const std::string &Str,
1829735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                     const std::string prefix) {
1839735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    std::string name = prefix + Str;
1849735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::Constant *ConstStr = TheModule.getGlobalVariable(name);
1859735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    if (!ConstStr) {
18694010695f7fce626e41ef045b60def9c912e9ce8Chris Lattner      llvm::Constant *value = llvm::ConstantDataArray::getString(VMContext,Str);
1879735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true,
1889735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall              llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str);
1899735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    }
190a5c04344fa70d6eec34344760c1fe511e16f2d76Jay Foad    return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros);
1919735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
19281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a global structure, initialized by the elements in the vector.
19381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The element types must match the types of the structure elements in the
19481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// first argument.
1952acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::GlobalVariable *MakeGlobal(llvm::StructType *Ty,
196cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                                   ArrayRef<llvm::Constant *> V,
1975f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   StringRef Name="",
1989735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                   llvm::GlobalValue::LinkageTypes linkage
1999735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                         =llvm::GlobalValue::InternalLinkage) {
2009735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
2019735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    return new llvm::GlobalVariable(TheModule, Ty, false,
2029735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        linkage, C, Name);
2039735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
20481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a global array.  The vector must contain the same number of
20581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// elements that the array type declares, of the type specified as the array
20681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// element type.
2072acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::GlobalVariable *MakeGlobal(llvm::ArrayType *Ty,
208cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                                   ArrayRef<llvm::Constant *> V,
2095f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   StringRef Name="",
2109735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                   llvm::GlobalValue::LinkageTypes linkage
2119735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                         =llvm::GlobalValue::InternalLinkage) {
2129735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
2139735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    return new llvm::GlobalVariable(TheModule, Ty, false,
2149735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                    linkage, C, Name);
2159735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
21681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a global array, inferring the array type from the specified
21781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// element type and the size of the initialiser.
2182acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::GlobalVariable *MakeGlobalArray(llvm::Type *Ty,
219cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko                                        ArrayRef<llvm::Constant *> V,
2205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                        StringRef Name="",
2219735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                        llvm::GlobalValue::LinkageTypes linkage
2229735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall                                         =llvm::GlobalValue::InternalLinkage) {
2239735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    llvm::ArrayType *ArrayTy = llvm::ArrayType::get(Ty, V.size());
2249735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall    return MakeGlobal(ArrayTy, V, Name, linkage);
2259735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
226891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall  /// Returns a property name and encoding string.
227891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall  llvm::Constant *MakePropertyEncodingString(const ObjCPropertyDecl *PD,
228891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall                                             const Decl *Container) {
229de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime;
230891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall    if ((R.getKind() == ObjCRuntime::GNUstep) &&
231891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall        (R.getVersion() >= VersionTuple(1, 6))) {
232891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall      std::string NameAndAttributes;
233891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall      std::string TypeStr;
234891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall      CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
235891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall      NameAndAttributes += '\0';
236891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall      NameAndAttributes += TypeStr.length() + 3;
237891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall      NameAndAttributes += TypeStr;
238891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall      NameAndAttributes += '\0';
239891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall      NameAndAttributes += PD->getNameAsString();
240891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall      return llvm::ConstantExpr::getGetElementPtr(
241ef8225444452a1486bd721f3285301fe84643b00Stephen Hines          CGM.GetAddrOfConstantCString(NameAndAttributes), Zeros);
242891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall    }
243891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall    return MakeConstantString(PD->getNameAsString());
244891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall  }
245de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall  /// Push the property attributes into two structure fields.
246de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall  void PushPropertyAttributes(std::vector<llvm::Constant*> &Fields,
247de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall      ObjCPropertyDecl *property, bool isSynthesized=true, bool
248de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall      isDynamic=true) {
249de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    int attrs = property->getPropertyAttributes();
250de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    // For read-only properties, clear the copy and retain flags
251de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    if (attrs & ObjCPropertyDecl::OBJC_PR_readonly) {
252de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall      attrs &= ~ObjCPropertyDecl::OBJC_PR_copy;
253de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall      attrs &= ~ObjCPropertyDecl::OBJC_PR_retain;
254de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall      attrs &= ~ObjCPropertyDecl::OBJC_PR_weak;
255de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall      attrs &= ~ObjCPropertyDecl::OBJC_PR_strong;
256de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    }
257de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    // The first flags field has the same attribute values as clang uses internally
258de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    Fields.push_back(llvm::ConstantInt::get(Int8Ty, attrs & 0xff));
259de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    attrs >>= 8;
260de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    attrs <<= 2;
261de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    // For protocol properties, synthesized and dynamic have no meaning, so we
262de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    // reuse these flags to indicate that this is a protocol property (both set
263de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    // has no meaning, as a property can't be both synthesized and dynamic)
264de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    attrs |= isSynthesized ? (1<<0) : 0;
265de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    attrs |= isDynamic ? (1<<1) : 0;
266de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    // The second field is the next four fields left shifted by two, with the
267de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    // low bit set to indicate whether the field is synthesized or dynamic.
268de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    Fields.push_back(llvm::ConstantInt::get(Int8Ty, attrs & 0xff));
269de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    // Two padding fields
270de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
271de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
272de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall  }
27381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Ensures that the value has the required type, by inserting a bitcast if
27481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// required.  This function lets us avoid inserting bitcasts that are
27581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// redundant.
276bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value* EnforceType(CGBuilderTy &B, llvm::Value *V, llvm::Type *Ty) {
277c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    if (V->getType() == Ty) return V;
278c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    return B.CreateBitCast(V, Ty);
279c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  }
280c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // Some zeros used for GEPs in lots of places.
281c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Constant *Zeros[2];
28281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Null pointer value.  Mainly used as a terminator in various arrays.
283c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Constant *NULLPtr;
28481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// LLVM context.
285c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::LLVMContext &VMContext;
286c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnallprivate:
28781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Placeholder for the class.  Lots of things refer to the class before we've
28881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// actually emitted it.  We use this alias as a placeholder, and then replace
28981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// it with a pointer to the class structure before finally emitting the
29081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// module.
2915efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  llvm::GlobalAlias *ClassPtrAlias;
29281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Placeholder for the metaclass.  Lots of things refer to the class before
29381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// we've / actually emitted it.  We use this alias as a placeholder, and then
29481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// replace / it with a pointer to the metaclass structure before finally
29581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// emitting the / module.
2965efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  llvm::GlobalAlias *MetaClassPtrAlias;
29781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// All of the classes that have been generated for this compilation units.
29820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Classes;
29981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// All of the categories that have been generated for this compilation units.
30020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Categories;
30181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// All of the Objective-C constant strings that have been generated for this
30281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// compilation units.
30320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> ConstantStrings;
30481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Map from string values to Objective-C constant strings in the output.
30581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Used to prevent emitting Objective-C strings more than once.  This should
30681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// not be required at all - CodeGenModule should manage this list.
30748272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  llvm::StringMap<llvm::Constant*> ObjCStrings;
30881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// All of the protocols that have been declared.
30920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::StringMap<llvm::Constant*> ExistingProtocols;
31081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// For each variant of a selector, we store the type encoding and a
31181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// placeholder value.  For an untyped selector, the type will be the empty
31281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// string.  Selector references are all done via the module's selector table,
31381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// so we create an alias as a placeholder and then replace it with the real
31481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// value later.
3159f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector;
31681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Type of the selector map.  This is roughly equivalent to the structure
31781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// used in the GNUstep runtime, which maintains a list of all of the valid
31881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// types for a selector in a table.
3195f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef llvm::DenseMap<Selector, SmallVector<TypedSelector, 2> >
3209f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    SelectorMap;
32181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// A map from selectors to selector types.  This allows us to emit all
32281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// selectors of the same name and type together.
3239f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SelectorMap SelectorTable;
3249f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
32581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Selectors related to memory management.  When compiling in GC mode, we
32681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// omit these.
327ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  Selector RetainSel, ReleaseSel, AutoreleaseSel;
32881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Runtime functions used for memory management in GC mode.  Note that clang
32981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// supports code generation for calling these functions, but neither GNU
33081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// runtime actually supports this API properly yet.
3319f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn,
3329f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    WeakAssignFn, GlobalAssignFn;
3339f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
33429254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall  typedef std::pair<std::string, std::string> ClassAliasPair;
33529254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall  /// All classes that have aliases set for them.
33629254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall  std::vector<ClassAliasPair> ClassAliases;
33729254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall
3389735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnallprotected:
33981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function used for throwing Objective-C exceptions.
3409f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction ExceptionThrowFn;
341809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// Function used for rethrowing exceptions, used at the end of \@finally or
342809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \@synchronize blocks.
3439735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  LazyRuntimeFunction ExceptionReThrowFn;
34481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called when entering a catch function.  This is required for
34581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// differentiating Objective-C exceptions and foreign exceptions.
3469735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  LazyRuntimeFunction EnterCatchFn;
34781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called when exiting from a catch block.  Used to do exception
34881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// cleanup.
3499735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  LazyRuntimeFunction ExitCatchFn;
350809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// Function called when entering an \@synchronize block.  Acquires the lock.
3519f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction SyncEnterFn;
352809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// Function called when exiting an \@synchronize block.  Releases the lock.
3539f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction SyncExitFn;
3549f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
3559735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnallprivate:
3569735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall
35781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function called if fast enumeration detects that the collection is
35881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// modified during the update.
3599f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction EnumerationMutationFn;
36081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function for implementing synthesized property getters that return an
36181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// object.
3629f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction GetPropertyFn;
36381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function for implementing synthesized property setters that return an
36481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// object.
3659f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction SetPropertyFn;
36681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function used for non-object declared property getters.
3679f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction GetStructPropertyFn;
36881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Function used for non-object declared property setters.
3699f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  LazyRuntimeFunction SetStructPropertyFn;
3709f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
37181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The version of the runtime that this class targets.  Must match the
37281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// version in the runtime.
373a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall  int RuntimeVersion;
37481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The version of the protocol class.  Used to differentiate between ObjC1
37581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// and ObjC2 protocols.  Objective-C 1 protocols can not contain optional
37681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// components and can not contain declared properties.  We always emit
37781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Objective-C 2 property structures, but we have to pretend that they're
37881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Objective-C 1 property structures when targeting the GCC runtime or it
37981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// will abort.
3809f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  const int ProtocolVersion;
38120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovprivate:
38281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates an instance variable list structure.  This is a structure
38381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// containing a size and an array of structures containing instance variable
38481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// metadata.  This is used purely for introspection in the fragile ABI.  In
38581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// the non-fragile ABI, it's used for instance variable fixup.
386795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling  llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
387795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling                                   ArrayRef<llvm::Constant *> IvarTypes,
388795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling                                   ArrayRef<llvm::Constant *> IvarOffsets);
38981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a method list structure.  This is a structure containing a size
39081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// and an array of structures containing method metadata.
39181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  ///
39281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// This structure is used by both classes and categories, and contains a next
39381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// pointer allowing them to be chained together in a linked list.
3945f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  llvm::Constant *GenerateMethodList(const StringRef &ClassName,
3955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      const StringRef &CategoryName,
396795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling      ArrayRef<Selector> MethodSels,
397795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling      ArrayRef<llvm::Constant *> MethodTypes,
39820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      bool isClassMethodList);
399809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// Emits an empty protocol.  This is used for \@protocol() where no protocol
40081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// is found.  The runtime will (hopefully) fix up the pointer to refer to the
40181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// real protocol.
402f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
40381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a list of property metadata structures.  This follows the same
40481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// pattern as method and instance variable metadata lists.
405d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID,
4065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVectorImpl<Selector> &InstanceMethodSels,
4075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes);
40881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a list of referenced protocols.  Classes, categories, and
40981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// protocols all use this structure.
410795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling  llvm::Constant *GenerateProtocolList(ArrayRef<std::string> Protocols);
41181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// To ensure that all protocols are seen by the runtime, we add a category on
41281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// a class defined in the runtime, declaring no methods, but adopting the
41381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// protocols.  This is a horribly ugly hack, but it allows us to collect all
41481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// of the protocols without changing the ABI.
415c4a77906c259cba58c147d8468c406a430ecdcbbDmitri Gribenko  void GenerateProtocolHolderCategory();
41681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a class structure.
41720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *GenerateClassStructure(
41820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *MetaClass,
41920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *SuperClass,
42020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      unsigned info,
421d002cc6fc5b72bf00e3b7b4571ccf0f23c789b4bChris Lattner      const char *Name,
42220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *Version,
42320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *InstanceSize,
42420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *IVars,
42520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::Constant *Methods,
426d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *Protocols,
427d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *IvarOffsets,
4288c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall      llvm::Constant *Properties,
429917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall      llvm::Constant *StrongIvarBitmap,
430917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall      llvm::Constant *WeakIvarBitmap,
4318c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall      bool isMeta=false);
43281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Generates a method list.  This is used by protocols to define the required
43381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// and optional methods.
43420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *GenerateProtocolMethodList(
435795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling      ArrayRef<llvm::Constant *> MethodNames,
436795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling      ArrayRef<llvm::Constant *> MethodTypes);
43781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Returns a selector with the specified type encoding.  An empty string is
43881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// used to return an untyped selector (with the types field set to NULL).
439bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel,
4409f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    const std::string &TypeEncoding, bool lval);
44181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Returns the variable used to store the offset of an instance variable.
4429cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
4439cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian      const ObjCIvarDecl *Ivar);
44481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Emits a reference to a class.  This allows the linker to object if there
44581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// is no class of the matching name.
446f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCallprotected:
4472a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  void EmitClassRef(const std::string &className);
448c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall  /// Emits a pointer to the named class
449bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  virtual llvm::Value *GetClassNamed(CodeGenFunction &CGF,
450f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall                                     const std::string &Name, bool isWeak);
45181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// Looks up the method for sending a message to the specified object.  This
45281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// mechanism differs between the GCC and GNU runtimes, so this method must be
45381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// overridden in subclasses.
454c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
455c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::Value *&Receiver,
456c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                 llvm::Value *cmd,
45711311eaf6093a5374f94047df354d4c62d68c611Eli Friedman                                 llvm::MDNode *node,
45811311eaf6093a5374f94047df354d4c62d68c611Eli Friedman                                 MessageSendInfo &MSI) = 0;
459917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// Looks up the method for sending a message to a superclass.  This
460917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// mechanism differs between the GCC and GNU runtimes, so this method must
461917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// be overridden in subclasses.
462c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
463c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall                                      llvm::Value *ObjCSuper,
46411311eaf6093a5374f94047df354d4c62d68c611Eli Friedman                                      llvm::Value *cmd,
46511311eaf6093a5374f94047df354d4c62d68c611Eli Friedman                                      MessageSendInfo &MSI) = 0;
466917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// Libobjc2 uses a bitfield representation where small(ish) bitfields are
467917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// stored in a 64-bit value with the low bit set to 1 and the remaining 63
468917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// bits set to their values, LSB first, while larger ones are stored in a
469917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// structure of this / form:
470917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  ///
471917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// struct { int32_t length; int32_t values[length]; };
472917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  ///
473917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// The values in the array are stored in host-endian format, with the least
474917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// significant bit being assumed to come first in the bitfield.  Therefore,
475917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// a bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] },
476917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  /// while a bitfield / with the 63rd bit set will be 1<<64.
477795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling  llvm::Constant *MakeBitField(ArrayRef<bool> bits);
4780f984268b05edab2cc555a427c441baa9c252658Chris Lattnerpublic:
4799f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
4809f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      unsigned protocolClassVersion);
4819f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
482651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *GenerateConstantString(const StringLiteral *) override;
4839f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
484651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  RValue
485651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  GenerateMessageSend(CodeGenFunction &CGF, ReturnValueSlot Return,
486651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                      QualType ResultType, Selector Sel,
487651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                      llvm::Value *Receiver, const CallArgList &CallArgs,
488c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                      const ObjCInterfaceDecl *Class,
489651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                      const ObjCMethodDecl *Method) override;
490651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  RValue
491651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  GenerateMessageSendSuper(CodeGenFunction &CGF, ReturnValueSlot Return,
492651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           QualType ResultType, Selector Sel,
493f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                           const ObjCInterfaceDecl *Class,
494651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           bool isCategoryImpl, llvm::Value *Receiver,
495651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           bool IsClassMessage, const CallArgList &CallArgs,
496651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           const ObjCMethodDecl *Method) override;
497651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *GetClass(CodeGenFunction &CGF,
498651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                        const ObjCInterfaceDecl *OID) override;
499651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel,
500651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           bool lval = false) override;
501651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *GetSelector(CodeGenFunction &CGF,
502651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           const ObjCMethodDecl *Method) override;
503651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *GetEHType(QualType T) override;
504651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
505651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
506651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                 const ObjCContainerDecl *CD) override;
507651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void GenerateCategory(const ObjCCategoryImplDecl *CMD) override;
508651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void GenerateClass(const ObjCImplementationDecl *ClassDecl) override;
509651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) override;
510651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
511651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                   const ObjCProtocolDecl *PD) override;
512651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void GenerateProtocol(const ObjCProtocolDecl *PD) override;
513651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Function *ModuleInitFunction() override;
514651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *GetPropertyGetFunction() override;
515651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *GetPropertySetFunction() override;
516651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
517651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                                  bool copy) override;
518651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *GetSetStructFunction() override;
519651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *GetGetStructFunction() override;
520651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *GetCppAtomicObjectGetFunction() override;
521651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *GetCppAtomicObjectSetFunction() override;
522651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *EnumerationMutationFunction() override;
523651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
524651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void EmitTryStmt(CodeGenFunction &CGF,
525651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                   const ObjCAtTryStmt &S) override;
526651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void EmitSynchronizedStmt(CodeGenFunction &CGF,
527651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                            const ObjCAtSynchronizedStmt &S) override;
528651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void EmitThrowStmt(CodeGenFunction &CGF,
529651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                     const ObjCAtThrowStmt &S,
530651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                     bool ClearInsertionPoint=true) override;
531651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF,
532651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                 llvm::Value *AddrWeakObj) override;
533651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void EmitObjCWeakAssign(CodeGenFunction &CGF,
534651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                          llvm::Value *src, llvm::Value *dst) override;
535651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void EmitObjCGlobalAssign(CodeGenFunction &CGF,
536651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                            llvm::Value *src, llvm::Value *dest,
537651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                            bool threadlocal=false) override;
538651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void EmitObjCIvarAssign(CodeGenFunction &CGF, llvm::Value *src,
539651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                          llvm::Value *dest, llvm::Value *ivarOffset) override;
540651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void EmitObjCStrongCastAssign(CodeGenFunction &CGF,
541651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                llvm::Value *src, llvm::Value *dest) override;
542651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void EmitGCMemmoveCollectable(CodeGenFunction &CGF, llvm::Value *DestPtr,
543651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                llvm::Value *SrcPtr,
544651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                llvm::Value *Size) override;
545651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  LValue EmitObjCValueForIvar(CodeGenFunction &CGF, QualType ObjectTy,
546651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                              llvm::Value *BaseValue, const ObjCIvarDecl *Ivar,
547651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                              unsigned CVRQualifiers) override;
548651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
549651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                              const ObjCInterfaceDecl *Interface,
550651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                              const ObjCIvarDecl *Ivar) override;
551651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) override;
552651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM,
553651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                     const CGBlockInfo &blockInfo) override {
55489ecd41e0a6bfb3b0913dbe41c3c666340b308c7Fariborz Jahanian    return NULLPtr;
55589ecd41e0a6bfb3b0913dbe41c3c666340b308c7Fariborz Jahanian  }
556651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *BuildRCBlockLayout(CodeGenModule &CGM,
557651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                     const CGBlockInfo &blockInfo) override {
558c46b43517a18c8f41934a38a3580bd477ab54265Fariborz Jahanian    return NULLPtr;
559c46b43517a18c8f41934a38a3580bd477ab54265Fariborz Jahanian  }
560651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
561651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Constant *BuildByrefLayout(CodeGenModule &CGM, QualType T) override {
5623ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    return NULLPtr;
5633ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  }
564651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
565651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::GlobalVariable *GetClassGlobal(const std::string &Name,
566651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                       bool Weak = false) override {
5676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
5686f40e2244b0590f144c5ceee07981a962fbbc72eFariborz Jahanian  }
5690f984268b05edab2cc555a427c441baa9c252658Chris Lattner};
57081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// Class representing the legacy GCC Objective-C ABI.  This is the default when
57181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// -fobjc-nonfragile-abi is not specified.
57281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall///
57381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// The GCC ABI target actually generates code that is approximately compatible
57481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// with the new GNUstep runtime ABI, but refrains from using any features that
57581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// would not work with the GCC runtime.  For example, clang always generates
57681a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// the extended form of the class structure, and the extra fields are simply
57781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// ignored by GCC libobjc.
5789f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclass CGObjCGCC : public CGObjCGNU {
57981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The GCC ABI message lookup function.  Returns an IMP pointing to the
58081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// method implementation for this message.
581c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  LazyRuntimeFunction MsgLookupFn;
58281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// The GCC ABI superclass message lookup function.  Takes a pointer to a
58381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// structure describing the receiver and the class, and a selector as
58481a65f576842a1d02b1e3db21c14652c60a36448David Chisnall  /// arguments.  Returns the IMP for the corresponding method.
585c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  LazyRuntimeFunction MsgLookupSuperFn;
586c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnallprotected:
587651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
588651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         llvm::Value *cmd, llvm::MDNode *node,
589651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         MessageSendInfo &MSI) override {
590c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    CGBuilderTy &Builder = CGF.Builder;
5916f3887ec3652482d2e0e8959d8a89b785330b0b2David Chisnall    llvm::Value *args[] = {
592c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall            EnforceType(Builder, Receiver, IdTy),
5936f3887ec3652482d2e0e8959d8a89b785330b0b2David Chisnall            EnforceType(Builder, cmd, SelectorTy) };
594bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    llvm::CallSite imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
5956f3887ec3652482d2e0e8959d8a89b785330b0b2David Chisnall    imp->setMetadata(msgSendMDKind, node);
5966f3887ec3652482d2e0e8959d8a89b785330b0b2David Chisnall    return imp.getInstruction();
597c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  }
598651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, llvm::Value *ObjCSuper,
599651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                              llvm::Value *cmd, MessageSendInfo &MSI) override {
600c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      CGBuilderTy &Builder = CGF.Builder;
601c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper,
602c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall          PtrToObjCSuperTy), cmd};
603bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
604c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
6059f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  public:
606c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) {
607c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // IMP objc_msg_lookup(id, SEL);
6086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy,
6096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                       nullptr);
610c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // IMP objc_msg_lookup_super(struct objc_super*, SEL);
611c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
6126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              PtrToObjCSuperTy, SelectorTy, nullptr);
613c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
6149f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall};
61581a65f576842a1d02b1e3db21c14652c60a36448David Chisnall/// Class used when targeting the new GNUstep runtime ABI.
6169f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclass CGObjCGNUstep : public CGObjCGNU {
61781a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// The slot lookup function.  Returns a pointer to a cacheable structure
61881a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// that contains (among other things) the IMP.
619c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    LazyRuntimeFunction SlotLookupFn;
62081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// The GNUstep ABI superclass message lookup function.  Takes a pointer to
62181a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// a structure describing the receiver and the class, and a selector as
62281a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// arguments.  Returns the slot for the corresponding method.  Superclass
62381a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// message lookup rarely changes, so this is a good caching opportunity.
624c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    LazyRuntimeFunction SlotLookupSuperFn;
625d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    /// Specialised function for setting atomic retain properties
626d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    LazyRuntimeFunction SetPropertyAtomic;
627d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    /// Specialised function for setting atomic copy properties
628d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    LazyRuntimeFunction SetPropertyAtomicCopy;
629d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    /// Specialised function for setting nonatomic retain properties
630d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    LazyRuntimeFunction SetPropertyNonAtomic;
631d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    /// Specialised function for setting nonatomic copy properties
632d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    LazyRuntimeFunction SetPropertyNonAtomicCopy;
633d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    /// Function to perform atomic copies of C++ objects with nontrivial copy
634d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    /// constructors from Objective-C ivars.
635d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    LazyRuntimeFunction CxxAtomicObjectGetFn;
636d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    /// Function to perform atomic copies of C++ objects with nontrivial copy
637d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    /// constructors to Objective-C ivars.
638d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    LazyRuntimeFunction CxxAtomicObjectSetFn;
63981a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// Type of an slot structure pointer.  This is returned by the various
64081a65f576842a1d02b1e3db21c14652c60a36448David Chisnall    /// lookup functions.
641c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    llvm::Type *SlotTy;
6422b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall  public:
643651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::Constant *GetEHType(QualType T) override;
644c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  protected:
645651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
646651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           llvm::Value *cmd, llvm::MDNode *node,
647651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                           MessageSendInfo &MSI) override {
648c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      CGBuilderTy &Builder = CGF.Builder;
649c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Function *LookupFn = SlotLookupFn;
650c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
651c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // Store the receiver on the stack so that we can reload it later
652c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType());
653c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      Builder.CreateStore(Receiver, ReceiverPtr);
654c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
655c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *self;
656c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
657c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) {
658c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        self = CGF.LoadObjCSelf();
659c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      } else {
660c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        self = llvm::ConstantPointerNull::get(IdTy);
661c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      }
662c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
663c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // The lookup function is guaranteed not to capture the receiver pointer.
664c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      LookupFn->setDoesNotCapture(1);
665c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
6666f3887ec3652482d2e0e8959d8a89b785330b0b2David Chisnall      llvm::Value *args[] = {
667c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              EnforceType(Builder, ReceiverPtr, PtrToIdTy),
668c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              EnforceType(Builder, cmd, SelectorTy),
6696f3887ec3652482d2e0e8959d8a89b785330b0b2David Chisnall              EnforceType(Builder, self, IdTy) };
670bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      llvm::CallSite slot = CGF.EmitRuntimeCallOrInvoke(LookupFn, args);
6716f3887ec3652482d2e0e8959d8a89b785330b0b2David Chisnall      slot.setOnlyReadsMemory();
672c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      slot->setMetadata(msgSendMDKind, node);
673c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
674c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // Load the imp from the slot
6756f3887ec3652482d2e0e8959d8a89b785330b0b2David Chisnall      llvm::Value *imp =
6766f3887ec3652482d2e0e8959d8a89b785330b0b2David Chisnall        Builder.CreateLoad(Builder.CreateStructGEP(slot.getInstruction(), 4));
677c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
678c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // The lookup function may have changed the receiver, so make sure we use
679c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // the new one.
680c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      Receiver = Builder.CreateLoad(ReceiverPtr, true);
681c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      return imp;
682c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
683651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, llvm::Value *ObjCSuper,
684651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                llvm::Value *cmd,
685651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                MessageSendInfo &MSI) override {
686c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      CGBuilderTy &Builder = CGF.Builder;
687c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
688c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
689bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      llvm::CallInst *slot =
690bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall        CGF.EmitNounwindRuntimeCall(SlotLookupSuperFn, lookupArgs);
691c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      slot->setOnlyReadsMemory();
692c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
693c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      return Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
694c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
6959f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  public:
696c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNU(Mod, 9, 3) {
697de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall      const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime;
69865bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid Chisnall
6997650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner      llvm::StructType *SlotStructTy = llvm::StructType::get(PtrTy,
7006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          PtrTy, PtrTy, IntTy, IMPTy, nullptr);
701c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      SlotTy = llvm::PointerType::getUnqual(SlotStructTy);
702c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
703c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy,
7046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          SelectorTy, IdTy, nullptr);
705c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      // Slot_t objc_msg_lookup_super(struct objc_super*, SEL);
706c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
7076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              PtrToObjCSuperTy, SelectorTy, nullptr);
7089735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      // If we're in ObjC++ mode, then we want to make
7094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (CGM.getLangOpts().CPlusPlus) {
7109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner        llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
7119735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // void *__cxa_begin_catch(void *e)
7126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy, nullptr);
7139735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // void __cxa_end_catch(void)
7146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy, nullptr);
7159735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall        // void _Unwind_Resume_or_Rethrow(void*)
716d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall        ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
7176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            PtrTy, nullptr);
71865bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid Chisnall      } else if (R.getVersion() >= VersionTuple(1, 7)) {
71965bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid Chisnall        llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
72065bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid Chisnall        // id objc_begin_catch(void *e)
7216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy, nullptr);
72265bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid Chisnall        // void objc_end_catch(void)
7236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        ExitCatchFn.init(&CGM, "objc_end_catch", VoidTy, nullptr);
72465bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid Chisnall        // void _Unwind_Resume_or_Rethrow(void*)
72565bd4ac6ffbf6de30cd6f36735539ff8172a904aDavid Chisnall        ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy,
7266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            PtrTy, nullptr);
7279735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall      }
728d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
729d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
7306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          SelectorTy, IdTy, PtrDiffTy, nullptr);
731d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
7326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr);
733d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      SetPropertyNonAtomic.init(&CGM, "objc_setProperty_nonatomic", VoidTy,
7346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr);
735d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      SetPropertyNonAtomicCopy.init(&CGM, "objc_setProperty_nonatomic_copy",
7366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          VoidTy, IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr);
737d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // void objc_setCppObjectAtomic(void *dest, const void *src, void
738d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // *helper);
739d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      CxxAtomicObjectSetFn.init(&CGM, "objc_setCppObjectAtomic", VoidTy, PtrTy,
7406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          PtrTy, PtrTy, nullptr);
741d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // void objc_getCppObjectAtomic(void *dest, const void *src, void
742d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // *helper);
743d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      CxxAtomicObjectGetFn.init(&CGM, "objc_getCppObjectAtomic", VoidTy, PtrTy,
7446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          PtrTy, PtrTy, nullptr);
745d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    }
746651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::Constant *GetCppAtomicObjectGetFunction() override {
747d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // The optimised functions were added in version 1.7 of the GNUstep
748d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // runtime.
749d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
750d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall          VersionTuple(1, 7));
751d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      return CxxAtomicObjectGetFn;
752d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    }
753651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::Constant *GetCppAtomicObjectSetFunction() override {
754d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // The optimised functions were added in version 1.7 of the GNUstep
755d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // runtime.
756d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
757d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall          VersionTuple(1, 7));
758d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      return CxxAtomicObjectSetFn;
759d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall    }
760651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
761651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                                    bool copy) override {
762d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // The optimised property functions omit the GC check, and so are not
763d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // safe to use in GC mode.  The standard functions are fast in GC mode,
764d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // so there is less advantage in using them.
765d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      assert ((CGM.getLangOpts().getGC() == LangOptions::NonGC));
766d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // The optimised functions were added in version 1.7 of the GNUstep
767d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      // runtime.
768d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
769d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall          VersionTuple(1, 7));
770d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall
771d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      if (atomic) {
772d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall        if (copy) return SetPropertyAtomicCopy;
773d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall        return SetPropertyAtomic;
774d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall      }
775d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall
776651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return copy ? SetPropertyNonAtomicCopy : SetPropertyNonAtomic;
777c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    }
7789f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall};
7799f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
7802b3e50eef807c4ff4e247f4039dca8c55b8c4592Bill Wendling/// Support for the ObjFW runtime.
7810a7dd788dbef975f35f273c7ab913f480f7edd60John McCallclass CGObjCObjFW: public CGObjCGNU {
7820a7dd788dbef975f35f273c7ab913f480f7edd60John McCallprotected:
7830a7dd788dbef975f35f273c7ab913f480f7edd60John McCall  /// The GCC ABI message lookup function.  Returns an IMP pointing to the
7840a7dd788dbef975f35f273c7ab913f480f7edd60John McCall  /// method implementation for this message.
7850a7dd788dbef975f35f273c7ab913f480f7edd60John McCall  LazyRuntimeFunction MsgLookupFn;
78611311eaf6093a5374f94047df354d4c62d68c611Eli Friedman  /// stret lookup function.  While this does not seem to make sense at the
78711311eaf6093a5374f94047df354d4c62d68c611Eli Friedman  /// first look, this is required to call the correct forwarding function.
78811311eaf6093a5374f94047df354d4c62d68c611Eli Friedman  LazyRuntimeFunction MsgLookupFnSRet;
7890a7dd788dbef975f35f273c7ab913f480f7edd60John McCall  /// The GCC ABI superclass message lookup function.  Takes a pointer to a
7900a7dd788dbef975f35f273c7ab913f480f7edd60John McCall  /// structure describing the receiver and the class, and a selector as
7910a7dd788dbef975f35f273c7ab913f480f7edd60John McCall  /// arguments.  Returns the IMP for the corresponding method.
79211311eaf6093a5374f94047df354d4c62d68c611Eli Friedman  LazyRuntimeFunction MsgLookupSuperFn, MsgLookupSuperFnSRet;
7930a7dd788dbef975f35f273c7ab913f480f7edd60John McCall
794651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
795651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         llvm::Value *cmd, llvm::MDNode *node,
796651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         MessageSendInfo &MSI) override {
7970a7dd788dbef975f35f273c7ab913f480f7edd60John McCall    CGBuilderTy &Builder = CGF.Builder;
7980a7dd788dbef975f35f273c7ab913f480f7edd60John McCall    llvm::Value *args[] = {
7990a7dd788dbef975f35f273c7ab913f480f7edd60John McCall            EnforceType(Builder, Receiver, IdTy),
8000a7dd788dbef975f35f273c7ab913f480f7edd60John McCall            EnforceType(Builder, cmd, SelectorTy) };
80111311eaf6093a5374f94047df354d4c62d68c611Eli Friedman
80211311eaf6093a5374f94047df354d4c62d68c611Eli Friedman    llvm::CallSite imp;
80311311eaf6093a5374f94047df354d4c62d68c611Eli Friedman    if (CGM.ReturnTypeUsesSRet(MSI.CallInfo))
80411311eaf6093a5374f94047df354d4c62d68c611Eli Friedman      imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFnSRet, args);
80511311eaf6093a5374f94047df354d4c62d68c611Eli Friedman    else
80611311eaf6093a5374f94047df354d4c62d68c611Eli Friedman      imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
80711311eaf6093a5374f94047df354d4c62d68c611Eli Friedman
8080a7dd788dbef975f35f273c7ab913f480f7edd60John McCall    imp->setMetadata(msgSendMDKind, node);
8090a7dd788dbef975f35f273c7ab913f480f7edd60John McCall    return imp.getInstruction();
8100a7dd788dbef975f35f273c7ab913f480f7edd60John McCall  }
8110a7dd788dbef975f35f273c7ab913f480f7edd60John McCall
812651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, llvm::Value *ObjCSuper,
813651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                              llvm::Value *cmd, MessageSendInfo &MSI) override {
8140a7dd788dbef975f35f273c7ab913f480f7edd60John McCall      CGBuilderTy &Builder = CGF.Builder;
8150a7dd788dbef975f35f273c7ab913f480f7edd60John McCall      llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper,
8160a7dd788dbef975f35f273c7ab913f480f7edd60John McCall          PtrToObjCSuperTy), cmd};
81711311eaf6093a5374f94047df354d4c62d68c611Eli Friedman
81811311eaf6093a5374f94047df354d4c62d68c611Eli Friedman      if (CGM.ReturnTypeUsesSRet(MSI.CallInfo))
81911311eaf6093a5374f94047df354d4c62d68c611Eli Friedman        return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFnSRet, lookupArgs);
82011311eaf6093a5374f94047df354d4c62d68c611Eli Friedman      else
82111311eaf6093a5374f94047df354d4c62d68c611Eli Friedman        return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
8220a7dd788dbef975f35f273c7ab913f480f7edd60John McCall    }
8230a7dd788dbef975f35f273c7ab913f480f7edd60John McCall
824651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::Value *GetClassNamed(CodeGenFunction &CGF,
825651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                             const std::string &Name, bool isWeak) override {
826f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall    if (isWeak)
827bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      return CGObjCGNU::GetClassNamed(CGF, Name, isWeak);
828f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall
829f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall    EmitClassRef(Name);
830f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall
831f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall    std::string SymbolName = "_OBJC_CLASS_" + Name;
832f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall
833f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall    llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(SymbolName);
834f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall
835f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall    if (!ClassSymbol)
836f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall      ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
837f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall                                             llvm::GlobalValue::ExternalLinkage,
8386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                             nullptr, SymbolName);
839f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall
840f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall    return ClassSymbol;
841f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall  }
842f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall
843f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCallpublic:
8440a7dd788dbef975f35f273c7ab913f480f7edd60John McCall  CGObjCObjFW(CodeGenModule &Mod): CGObjCGNU(Mod, 9, 3) {
8450a7dd788dbef975f35f273c7ab913f480f7edd60John McCall    // IMP objc_msg_lookup(id, SEL);
8466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, nullptr);
84711311eaf6093a5374f94047df354d4c62d68c611Eli Friedman    MsgLookupFnSRet.init(&CGM, "objc_msg_lookup_stret", IMPTy, IdTy,
8486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                         SelectorTy, nullptr);
8490a7dd788dbef975f35f273c7ab913f480f7edd60John McCall    // IMP objc_msg_lookup_super(struct objc_super*, SEL);
8500a7dd788dbef975f35f273c7ab913f480f7edd60John McCall    MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
8516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                          PtrToObjCSuperTy, SelectorTy, nullptr);
85211311eaf6093a5374f94047df354d4c62d68c611Eli Friedman    MsgLookupSuperFnSRet.init(&CGM, "objc_msg_lookup_super_stret", IMPTy,
8536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                              PtrToObjCSuperTy, SelectorTy, nullptr);
8540a7dd788dbef975f35f273c7ab913f480f7edd60John McCall  }
855f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall};
8560f984268b05edab2cc555a427c441baa9c252658Chris Lattner} // end anonymous namespace
8570f984268b05edab2cc555a427c441baa9c252658Chris Lattner
85820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
8592a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner/// Emits a reference to a dummy variable which is emitted with each class.
8602a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner/// This ensures that a linker error will be generated when trying to link
8612a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner/// together modules where a referenced class is not defined.
862bb1c8600218b3244340331165fc7cba7bf227655Mike Stumpvoid CGObjCGNU::EmitClassRef(const std::string &className) {
8632a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  std::string symbolRef = "__objc_class_ref_" + className;
8642a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  // Don't emit two copies of the same symbol
865bb1c8600218b3244340331165fc7cba7bf227655Mike Stump  if (TheModule.getGlobalVariable(symbolRef))
866bb1c8600218b3244340331165fc7cba7bf227655Mike Stump    return;
8672a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  std::string symbolName = "__objc_class_name_" + className;
8682a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
8692a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  if (!ClassSymbol) {
8701c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
8716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                           llvm::GlobalValue::ExternalLinkage,
8726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                           nullptr, symbolName);
8732a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  }
8741c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson  new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
875f35271b8677a5f31498b89587b1da42a06dd46fdChris Lattner    llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
8762a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner}
87720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
8785f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerstatic std::string SymbolNameForMethod(const StringRef &ClassName,
8795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    const StringRef &CategoryName, const Selector MethodName,
8809f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    bool isClassMethod) {
8819f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  std::string MethodNameColonStripped = MethodName.getAsString();
882d346736dfe5ef584a2d0c9d27d217408ee394b9bDavid Chisnall  std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(),
883d346736dfe5ef584a2d0c9d27d217408ee394b9bDavid Chisnall      ':', '_');
8845f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  return (Twine(isClassMethod ? "_c_" : "_i_") + ClassName + "_" +
8859f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    CategoryName + "_" + MethodNameColonStripped).str();
88687935a8d5c0eeda8ad62815f4fc815ad25c7cd8eDavid Chisnall}
88720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
8889f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallCGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
8896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                     unsigned protocolClassVersion)
890de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  : CGObjCRuntime(cgm), TheModule(CGM.getModule()),
8916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    VMContext(cgm.getLLVMContext()), ClassPtrAlias(nullptr),
8926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    MetaClassPtrAlias(nullptr), RuntimeVersion(runtimeABIVersion),
8936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ProtocolVersion(protocolClassVersion) {
894c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall
895c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall  msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend");
896c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall
8979f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  CodeGenTypes &Types = CGM.getTypes();
898e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  IntTy = cast<llvm::IntegerType>(
8999f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Types.ConvertType(CGM.getContext().IntTy));
900e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  LongTy = cast<llvm::IntegerType>(
9019f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Types.ConvertType(CGM.getContext().LongTy));
9028fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  SizeTy = cast<llvm::IntegerType>(
9039f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Types.ConvertType(CGM.getContext().getSizeType()));
9048fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  PtrDiffTy = cast<llvm::IntegerType>(
9059f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Types.ConvertType(CGM.getContext().getPointerDiffType()));
9068fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall  BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
9071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
908d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Int8Ty = llvm::Type::getInt8Ty(VMContext);
909d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // C string type.  Used in lots of places.
910d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
911d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
9124a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
91320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Zeros[1] = Zeros[0];
914d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
915391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Get the selector Type.
9160d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  QualType selTy = CGM.getContext().getObjCSelType();
9170d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  if (QualType() == selTy) {
9180d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    SelectorTy = PtrToInt8Ty;
9190d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  } else {
9200d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy));
9210d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  }
922e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner
92396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
924391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  PtrTy = PtrToInt8Ty;
9251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
926917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  Int32Ty = llvm::Type::getInt32Ty(VMContext);
927917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  Int64Ty = llvm::Type::getInt64Ty(VMContext);
928917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall
92949de5289acea91c9d224c9aadd4d527e8f9fdaffDavid Chisnall  IntPtrTy =
930651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      CGM.getDataLayout().getPointerSizeInBits() == 32 ? Int32Ty : Int64Ty;
93149de5289acea91c9d224c9aadd4d527e8f9fdaffDavid Chisnall
932391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // Object type
9337bcf6c3e098e808282cc96309c02c08ab6229940David Chisnall  QualType UnqualIdTy = CGM.getContext().getObjCIdType();
9347bcf6c3e098e808282cc96309c02c08ab6229940David Chisnall  ASTIdTy = CanQualType();
9357bcf6c3e098e808282cc96309c02c08ab6229940David Chisnall  if (UnqualIdTy != QualType()) {
9367bcf6c3e098e808282cc96309c02c08ab6229940David Chisnall    ASTIdTy = CGM.getContext().getCanonicalType(UnqualIdTy);
9370d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall    IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
9387bcf6c3e098e808282cc96309c02c08ab6229940David Chisnall  } else {
9397bcf6c3e098e808282cc96309c02c08ab6229940David Chisnall    IdTy = PtrToInt8Ty;
9400d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall  }
941ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  PtrToIdTy = llvm::PointerType::getUnqual(IdTy);
9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  ObjCSuperTy = llvm::StructType::get(IdTy, IdTy, nullptr);
944c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy);
945c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
9469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
9479f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
9489f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_exception_throw(id);
9496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, nullptr);
9506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, nullptr);
9519f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // int objc_sync_enter(id);
9526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy, nullptr);
9539f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // int objc_sync_exit(id);
9546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy, nullptr);
9559f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
9569f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_enumerationMutation (id)
9579f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy,
9586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      IdTy, nullptr);
9599f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
9609f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // id objc_getProperty(id, SEL, ptrdiff_t, BOOL)
9619f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy,
9626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      PtrDiffTy, BoolTy, nullptr);
9639f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL)
9649f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy,
9656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      PtrDiffTy, IdTy, BoolTy, BoolTy, nullptr);
9669f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
9679f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy,
9686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      PtrDiffTy, BoolTy, BoolTy, nullptr);
9699f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
9709f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy,
9716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      PtrDiffTy, BoolTy, BoolTy, nullptr);
9729f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
973391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  // IMP type
9749cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *IMPArgs[] = { IdTy, SelectorTy };
975c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs,
976c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall              true));
977ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall
9784e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &Opts = CGM.getLangOpts();
979e289d81369914678db386f6aa86faf8f178e245dDouglas Gregor  if ((Opts.getGC() != LangOptions::NonGC) || Opts.ObjCAutoRefCount)
980f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall    RuntimeVersion = 10;
981f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall
9829735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  // Don't bother initialising the GC stuff unless we're compiling in GC mode
983e289d81369914678db386f6aa86faf8f178e245dDouglas Gregor  if (Opts.getGC() != LangOptions::NonGC) {
984a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall    // This is a bit of an hack.  We should sort this out by having a proper
985a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall    // CGObjCGNUstep subclass for GC, but we may want to really support the old
986a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall    // ABI and GC added in ObjectiveC2.framework, so we fudge it a bit for now
987ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // Get selectors needed in GC mode
988ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    RetainSel = GetNullarySelector("retain", CGM.getContext());
989ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    ReleaseSel = GetNullarySelector("release", CGM.getContext());
990ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext());
991ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall
992ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // Get functions needed in GC mode
993ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall
994ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_assign_ivar(id, id, ptrdiff_t);
9959f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy,
9966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        nullptr);
997ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_assign_strongCast (id, id*)
9989f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy,
9996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        PtrToIdTy, nullptr);
1000ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_assign_global(id, id*);
10019f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy,
10026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        nullptr);
1003ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_assign_weak(id, id*);
10046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy, nullptr);
1005ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // id objc_read_weak(id*);
10066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy, nullptr);
1007ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    // void *objc_memmove_collectable(void*, void *, size_t);
10089f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy,
10096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        SizeTy, nullptr);
1010ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  }
101120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
1012bb1c8600218b3244340331165fc7cba7bf227655Mike Stump
1013bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::Value *CGObjCGNU::GetClassNamed(CodeGenFunction &CGF,
1014d3fc7296ae95353173bae25ee4dd3ecf4d6dced1David Chisnall                                      const std::string &Name,
1015d3fc7296ae95353173bae25ee4dd3ecf4d6dced1David Chisnall                                      bool isWeak) {
1016c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall  llvm::Value *ClassName = CGM.GetAddrOfConstantCString(Name);
101741d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // With the incompatible ABI, this will need to be replaced with a direct
101841d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // reference to the class symbol.  For the compatible nonfragile ABI we are
101941d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // still performing this lookup at run time but emitting the symbol for the
102041d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // class externally so that we can make the switch later.
1021c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall  //
1022c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall  // Libobjc2 contains an LLVM pass that replaces calls to objc_lookup_class
1023c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall  // with memoized versions or with static references if it's safe to do so.
1024d3fc7296ae95353173bae25ee4dd3ecf4d6dced1David Chisnall  if (!isWeak)
1025d3fc7296ae95353173bae25ee4dd3ecf4d6dced1David Chisnall    EmitClassRef(Name);
1026bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  ClassName = CGF.Builder.CreateStructGEP(ClassName, 0);
1027ddb2a3d55a24a1dbdf9152621642d9a4b4fc2f61Daniel Dunbar
102820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ClassLookupFn =
1029da549e8995c447542d5631b8b67fcc3a9582797aJay Foad    CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, PtrToInt8Ty, true),
103026c8294dacc2efc83d8d560593ff863e7a32a48fFariborz Jahanian                              "objc_lookup_class");
1031bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return CGF.EmitNounwindRuntimeCall(ClassLookupFn, ClassName);
1032391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner}
1033391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
1034c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall// This has to perform the lookup every time, since posing and related
1035c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall// techniques can modify the name -> class mapping.
1036bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::Value *CGObjCGNU::GetClass(CodeGenFunction &CGF,
1037c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall                                 const ObjCInterfaceDecl *OID) {
1038bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return GetClassNamed(CGF, OID->getNameAsString(), OID->isWeakImported());
1039c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall}
1040bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::Value *CGObjCGNU::EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
1041bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return GetClassNamed(CGF, "NSAutoreleasePool", false);
1042c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall}
1043c7aed3b9fbfbb6e85c234a4ba6f00cab1901f2f7David Chisnall
1044bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel,
10459f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    const std::string &TypeEncoding, bool lval) {
10469f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
1047ad5b69da4774a4eec09af96af23a2fe793fd00f8Craig Topper  SmallVectorImpl<TypedSelector> &Types = SelectorTable[Sel];
10486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  llvm::GlobalAlias *SelValue = nullptr;
10499f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
10505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
10519f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      e = Types.end() ; i!=e ; i++) {
10529f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    if (i->first == TypeEncoding) {
10539f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      SelValue = i->second;
10549f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      break;
10559f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    }
10569f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  }
10576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!SelValue) {
10586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    SelValue = llvm::GlobalAlias::create(
10596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        SelectorTy->getElementType(), 0, llvm::GlobalValue::PrivateLinkage,
10606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        ".objc_selector_" + Sel.getAsString(), &TheModule);
10619f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Types.push_back(TypedSelector(TypeEncoding, SelValue));
10629f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  }
10639f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
1064c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  if (lval) {
1065bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    llvm::Value *tmp = CGF.CreateTempAlloca(SelValue->getType());
1066bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    CGF.Builder.CreateStore(SelValue, tmp);
1067c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    return tmp;
1068c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  }
1069c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  return SelValue;
10709f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall}
10719f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
1072bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel,
10739f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                    bool lval) {
1074bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return GetSelector(CGF, Sel, std::string(), lval);
1075df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian}
1076df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian
1077bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF,
1078bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                    const ObjCMethodDecl *Method) {
1079df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  std::string SelTypes;
1080df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
1081bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return GetSelector(CGF, Method->getSelector(), SelTypes, false);
10828e67b63530b4f39a48bc12d97376f373a6901279Chris Lattner}
10838e67b63530b4f39a48bc12d97376f373a6901279Chris Lattner
1084cf5abc7ba032bd35158e4d75b0bc92a482fc67e8Fariborz Jahanianllvm::Constant *CGObjCGNU::GetEHType(QualType T) {
10852b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall  if (T->isObjCIdType() || T->isObjCQualifiedIdType()) {
10862b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall    // With the old ABI, there was only one kind of catchall, which broke
10872b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall    // foreign exceptions.  With the new ABI, we use __objc_id_typeinfo as
10882b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall    // a pointer indicating object catchalls, and NULL to indicate real
10892b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall    // catchalls
10902b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall    if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
10912b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall      return MakeConstantString("@id");
10922b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall    } else {
10936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
10942b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall    }
10959735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  }
10962b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall
10972b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall  // All other types should be Objective-C interface pointer types.
10982b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall  const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>();
10992b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall  assert(OPT && "Invalid @catch type.");
11002b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall  const ObjCInterfaceDecl *IDecl = OPT->getObjectType()->getInterface();
11012b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall  assert(IDecl && "Invalid @catch type.");
11022b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall  return MakeConstantString(IDecl->getIdentifier()->getName());
11032b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall}
11042b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall
11052b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCallllvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
11062b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall  if (!CGM.getLangOpts().CPlusPlus)
11072b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall    return CGObjCGNU::GetEHType(T);
11082b07dd350a0d0b132ed529cb449fe2a5a56103b8John McCall
110980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // For Objective-C++, we want to provide the ability to catch both C++ and
111080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // Objective-C objects in the same function.
111180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
111280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // There's a particular fixed type info for 'id'.
111380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  if (T->isObjCIdType() ||
111480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall      T->isObjCQualifiedIdType()) {
111580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    llvm::Constant *IDEHType =
111680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall      CGM.getModule().getGlobalVariable("__objc_id_type_info");
111780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    if (!IDEHType)
111880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall      IDEHType =
111980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall        new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty,
112080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall                                 false,
112180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall                                 llvm::GlobalValue::ExternalLinkage,
11226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                 nullptr, "__objc_id_type_info");
112380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty);
112480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  }
112580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
112680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  const ObjCObjectPointerType *PT =
112780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    T->getAs<ObjCObjectPointerType>();
112880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  assert(PT && "Invalid @catch type.");
112980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  const ObjCInterfaceType *IT = PT->getInterfaceType();
113080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  assert(IT && "Invalid @catch type.");
113180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  std::string className = IT->getDecl()->getIdentifier()->getName();
113280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
113380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  std::string typeinfoName = "__objc_eh_typeinfo_" + className;
113480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
113580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // Return the existing typeinfo if it exists
113680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName);
1137acd76fe8e1b1b3fd83dac66848041629a8b06e8aDavid Chisnall  if (typeinfo)
1138acd76fe8e1b1b3fd83dac66848041629a8b06e8aDavid Chisnall    return llvm::ConstantExpr::getBitCast(typeinfo, PtrToInt8Ty);
113980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
114080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // Otherwise create it.
114180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
114280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // vtable for gnustep::libobjc::__objc_class_type_info
114380558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // It's quite ugly hard-coding this.  Ideally we'd generate it using the host
114480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // platform's name mangling.
114580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE";
114680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *Vtable = TheModule.getGlobalVariable(vtableName);
114780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  if (!Vtable) {
114880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true,
11496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                      llvm::GlobalValue::ExternalLinkage,
11506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                      nullptr, vtableName);
115180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  }
115280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2);
1153a5c04344fa70d6eec34344760c1fe511e16f2d76Jay Foad  Vtable = llvm::ConstantExpr::getGetElementPtr(Vtable, Two);
115480558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  Vtable = llvm::ConstantExpr::getBitCast(Vtable, PtrToInt8Ty);
115580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
115680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *typeName =
115780558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall    ExportUniqueString(className, "__objc_eh_typename_");
115880558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall
115980558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  std::vector<llvm::Constant*> fields;
116080558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  fields.push_back(Vtable);
116180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  fields.push_back(typeName);
116280558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  llvm::Constant *TI =
11637650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner      MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
11646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              nullptr), fields, "__objc_eh_typeinfo_" + className,
116580558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall          llvm::GlobalValue::LinkOnceODRLinkage);
116680558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty);
11675a180397870944548aaadeaebf58e415885b9489John McCall}
11685a180397870944548aaadeaebf58e415885b9489John McCall
116920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov/// Generate an NSConstantString object.
11700d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnallllvm::Constant *CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
117148272a078e54d501cb05edd797c54b8e82ceb520David Chisnall
11722f4eaef37476ae6891ede8ba215d0f6fd093629bBenjamin Kramer  std::string Str = SL->getString().str();
11730d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall
117448272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  // Look for an existing one
117548272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
117648272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  if (old != ObjCStrings.end())
117748272a078e54d501cb05edd797c54b8e82ceb520David Chisnall    return old->getValue();
117848272a078e54d501cb05edd797c54b8e82ceb520David Chisnall
11794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
118013df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall
118113df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  if (StringClass.empty()) StringClass = "NXConstantString";
118213df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall
118313df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  std::string Sym = "_OBJC_CLASS_";
118413df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  Sym += StringClass;
118513df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall
118613df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  llvm::Constant *isa = TheModule.getNamedGlobal(Sym);
118713df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall
118813df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  if (!isa)
118913df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall    isa = new llvm::GlobalVariable(TheModule, IdTy, /* isConstant */false,
11906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            llvm::GlobalValue::ExternalWeakLinkage, nullptr, Sym);
119113df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  else if (isa->getType() != PtrToIdTy)
119213df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall    isa = llvm::ConstantExpr::getBitCast(isa, PtrToIdTy);
119313df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall
119420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Ivars;
119513df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  Ivars.push_back(isa);
119613fd7e5111032f54b538dd66d035b0ccc1f82467Chris Lattner  Ivars.push_back(MakeConstantString(Str));
11974a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
119820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ObjCStr = MakeGlobal(
11996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    llvm::StructType::get(PtrToIdTy, PtrToInt8Ty, IntTy, nullptr),
120020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Ivars, ".objc_str");
120148272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
120248272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  ObjCStrings[Str] = ObjCStr;
120348272a078e54d501cb05edd797c54b8e82ceb520David Chisnall  ConstantStrings.push_back(ObjCStr);
120420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return ObjCStr;
120520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
120620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
120720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov///Generates a message send where the super is the receiver.  This is a message
120820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov///send to self with special delivery semantics indicating which class's method
120920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov///should be called.
12109f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallRValue
12119f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallCGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF,
1212ef072fd2f3347cfd857d6eb787b245b950771430John McCall                                    ReturnValueSlot Return,
12137f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                                    QualType ResultType,
12147f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                                    Selector Sel,
1215f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                                    const ObjCInterfaceDecl *Class,
12167ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian                                    bool isCategoryImpl,
1217f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                                    llvm::Value *Receiver,
121819cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar                                    bool IsClassMessage,
1219d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar                                    const CallArgList &CallArgs,
1220d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar                                    const ObjCMethodDecl *Method) {
12210bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall  CGBuilderTy &Builder = CGF.Builder;
12224e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
1223ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    if (Sel == RetainSel || Sel == AutoreleaseSel) {
12240bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall      return RValue::get(EnforceType(Builder, Receiver,
12250bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall                  CGM.getTypes().ConvertType(ResultType)));
1226ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    }
1227ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    if (Sel == ReleaseSel) {
12286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return RValue::get(nullptr);
1229ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    }
1230ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  }
1231db831948f32217ece8410099f9982ef900c3b3c3David Chisnall
1232bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::Value *cmd = GetSelector(CGF, Sel);
1233db831948f32217ece8410099f9982ef900c3b3c3David Chisnall
1234b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
1235b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian  CallArgList ActualArgs;
1236b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
123704c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy);
123804c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
1239f85e193739c953358c865005855253af4f68a497John McCall  ActualArgs.addFrom(CallArgs);
1240b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
1241de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
1242b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
12436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  llvm::Value *ReceiverClass = nullptr;
124448e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner  if (isCategoryImpl) {
12456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    llvm::Constant *classLookupFunction = nullptr;
124648e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    if (IsClassMessage)  {
124796e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
1248da549e8995c447542d5631b8b67fcc3a9582797aJay Foad            IdTy, PtrTy, true), "objc_get_meta_class");
124948e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    } else {
125096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
1251da549e8995c447542d5631b8b67fcc3a9582797aJay Foad            IdTy, PtrTy, true), "objc_get_class");
12525efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar    }
1253db831948f32217ece8410099f9982ef900c3b3c3David Chisnall    ReceiverClass = Builder.CreateCall(classLookupFunction,
125448e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner        MakeConstantString(Class->getNameAsString()));
12555efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  } else {
125648e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    // Set up global aliases for the metaclass or class pointer if they do not
125748e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    // already exist.  These will are forward-references which will be set to
1258bb1c8600218b3244340331165fc7cba7bf227655Mike Stump    // pointers to the class and metaclass structure created for the runtime
1259bb1c8600218b3244340331165fc7cba7bf227655Mike Stump    // load function.  To send a message to super, we look up the value of the
126048e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    // super_class pointer from either the class or metaclass structure.
126148e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    if (IsClassMessage)  {
126248e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      if (!MetaClassPtrAlias) {
12636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        MetaClassPtrAlias = llvm::GlobalAlias::create(
12646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            IdTy->getElementType(), 0, llvm::GlobalValue::InternalLinkage,
12656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            ".objc_metaclass_ref" + Class->getNameAsString(), &TheModule);
126648e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      }
126748e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      ReceiverClass = MetaClassPtrAlias;
126848e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner    } else {
126948e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      if (!ClassPtrAlias) {
12706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        ClassPtrAlias = llvm::GlobalAlias::create(
12716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            IdTy->getElementType(), 0, llvm::GlobalValue::InternalLinkage,
12726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            ".objc_class_ref" + Class->getNameAsString(), &TheModule);
127348e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      }
127448e6e7e8106f2404bfdd2d8abe7a840aeeaf047bChris Lattner      ReceiverClass = ClassPtrAlias;
12755efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar    }
127671238f67c5df13eef19450d66b7a7ab28377192aChris Lattner  }
12775efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  // Cast the pointer to a simplified version of the class structure
1278db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  ReceiverClass = Builder.CreateBitCast(ReceiverClass,
127996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      llvm::PointerType::getUnqual(
12806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        llvm::StructType::get(IdTy, IdTy, nullptr)));
12815efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  // Get the superclass pointer
1282db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  ReceiverClass = Builder.CreateStructGEP(ReceiverClass, 1);
12835efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  // Load the superclass pointer
1284db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  ReceiverClass = Builder.CreateLoad(ReceiverClass);
128520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Construct the structure used to look up the IMP
12867650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ObjCSuperTy = llvm::StructType::get(
12876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Receiver->getType(), IdTy, nullptr);
1288db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy);
1289b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
1290db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
1291db831948f32217ece8410099f9982ef900c3b3c3David Chisnall  Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
129220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1293c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  ObjCSuper = EnforceType(Builder, ObjCSuper, PtrToObjCSuperTy);
1294db831948f32217ece8410099f9982ef900c3b3c3David Chisnall
1295c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // Get the IMP
129611311eaf6093a5374f94047df354d4c62d68c611Eli Friedman  llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd, MSI);
1297de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  imp = EnforceType(Builder, imp, MSI.MessengerType);
129820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1299dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall  llvm::Value *impMD[] = {
1300dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall      llvm::MDString::get(VMContext, Sel.getAsString()),
1301dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall      llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()),
1302dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall      llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsClassMessage)
1303dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall   };
13046f141659cab11109d9931d92d0988f8850778de3Jay Foad  llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
1305dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall
13064b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  llvm::Instruction *call;
13076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  RValue msgRet = CGF.EmitCall(MSI.CallInfo, imp, Return, ActualArgs, nullptr,
13086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                               &call);
13094b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  call->setMetadata(msgSendMDKind, node);
13104b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  return msgRet;
131120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
131220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
13131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Generate code for a message send expression.
13149f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallRValue
13159f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallCGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF,
1316ef072fd2f3347cfd857d6eb787b245b950771430John McCall                               ReturnValueSlot Return,
13177f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                               QualType ResultType,
13187f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                               Selector Sel,
1319f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar                               llvm::Value *Receiver,
1320df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian                               const CallArgList &CallArgs,
1321c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall                               const ObjCInterfaceDecl *Class,
1322df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian                               const ObjCMethodDecl *Method) {
13230bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall  CGBuilderTy &Builder = CGF.Builder;
13240bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall
1325664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // Strip out message sends to retain / release in GC mode
13264e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
1327ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    if (Sel == RetainSel || Sel == AutoreleaseSel) {
13280bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall      return RValue::get(EnforceType(Builder, Receiver,
13290bbe0cfd374d0e564c4570b5e84a77f886c32d52David Chisnall                  CGM.getTypes().ConvertType(ResultType)));
1330ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    }
1331ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    if (Sel == ReleaseSel) {
13326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return RValue::get(nullptr);
1333ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall    }
1334ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  }
1335664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1336664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // If the return type is something that goes in an integer register, the
1337664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // runtime will handle 0 returns.  For other cases, we fill in the 0 value
1338664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // ourselves.
1339664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  //
1340664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // The language spec says the result of this kind of message send is
1341664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // undefined, but lots of people seem to have forgotten to read that
1342664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // paragraph and insist on sending messages to nil that have structure
1343664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // returns.  With GCC, this generates a random return value (whatever happens
1344664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  // to be on the stack / in those registers at the time) on most platforms,
1345c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // and generates an illegal instruction trap on SPARC.  With LLVM it corrupts
1346c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  // the stack.
1347c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  bool isPointerSizedReturn = (ResultType->isAnyPointerType() ||
1348c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall      ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType());
1349664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
13506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  llvm::BasicBlock *startBB = nullptr;
13516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  llvm::BasicBlock *messageBB = nullptr;
13526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  llvm::BasicBlock *continueBB = nullptr;
1353664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1354664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  if (!isPointerSizedReturn) {
1355664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    startBB = Builder.GetInsertBlock();
1356664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    messageBB = CGF.createBasicBlock("msgSend");
1357a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    continueBB = CGF.createBasicBlock("continue");
1358664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1359664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    llvm::Value *isNil = Builder.CreateICmpEQ(Receiver,
1360664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall            llvm::Constant::getNullValue(Receiver->getType()));
1361a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    Builder.CreateCondBr(isNil, continueBB, messageBB);
1362664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    CGF.EmitBlock(messageBB);
1363664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  }
1364664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
13650f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
1366df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  llvm::Value *cmd;
1367df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  if (Method)
1368bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    cmd = GetSelector(CGF, Method);
1369df9ccc6381314ccca6407abb209155e9273a631dFariborz Jahanian  else
1370bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    cmd = GetSelector(CGF, Sel);
1371c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  cmd = EnforceType(Builder, cmd, SelectorTy);
1372c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  Receiver = EnforceType(Builder, Receiver, IdTy);
1373c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall
1374c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  llvm::Value *impMD[] = {
1375c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        llvm::MDString::get(VMContext, Sel.getAsString()),
1376c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        llvm::MDString::get(VMContext, Class ? Class->getNameAsString() :""),
13776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext),
13786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                               Class!=nullptr)
1379c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall   };
13806f141659cab11109d9931d92d0988f8850778de3Jay Foad  llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
1381b3716efb0966620f205c939b671b579ad331ca0dFariborz Jahanian
1382c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall  CallArgList ActualArgs;
138304c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  ActualArgs.add(RValue::get(Receiver), ASTIdTy);
138404c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman  ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
1385f85e193739c953358c865005855253af4f68a497John McCall  ActualArgs.addFrom(CallArgs);
1386de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
1387de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
1388de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
138989c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall  // Get the IMP to call
139089c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall  llvm::Value *imp;
139189c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall
139289c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall  // If we have non-legacy dispatch specified, we try using the objc_msgSend()
139389c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall  // functions.  These are not supported on all platforms (or all runtimes on a
139489c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall  // given platform), so we
139589c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall  switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
139689c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall    case CodeGenOptions::Legacy:
139711311eaf6093a5374f94047df354d4c62d68c611Eli Friedman      imp = LookupIMP(CGF, Receiver, cmd, node, MSI);
139889c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall      break;
139989c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall    case CodeGenOptions::Mixed:
140089c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall    case CodeGenOptions::NonLegacy:
14016f3887ec3652482d2e0e8959d8a89b785330b0b2David Chisnall      if (CGM.ReturnTypeUsesFPRet(ResultType)) {
14026f3887ec3652482d2e0e8959d8a89b785330b0b2David Chisnall        imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
14036f3887ec3652482d2e0e8959d8a89b785330b0b2David Chisnall                                  "objc_msgSend_fpret");
1404de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      } else if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
140589c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall        // The actual types here don't matter - we're going to bitcast the
140689c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall        // function anyway
140789c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall        imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
140889c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall                                  "objc_msgSend_stret");
140989c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall      } else {
141089c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall        imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
141189c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall                                  "objc_msgSend");
141289c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall      }
141389c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall  }
141489c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall
1415403bc3f85f3b3461b73abdd9c632980bf95f34bdDavid Chisnall  // Reset the receiver in case the lookup modified it
1416403bc3f85f3b3461b73abdd9c632980bf95f34bdDavid Chisnall  ActualArgs[0] = CallArg(RValue::get(Receiver), ASTIdTy, false);
141789c300400b0b75d879da88f81c97a46feb0675beDavid Chisnall
1418de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  imp = EnforceType(Builder, imp, MSI.MessengerType);
141963e742bed90aab2e8e778e48fbbd4bce766b4373David Chisnall
14204b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  llvm::Instruction *call;
14216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  RValue msgRet = CGF.EmitCall(MSI.CallInfo, imp, Return, ActualArgs, nullptr,
14226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                               &call);
14234b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall  call->setMetadata(msgSendMDKind, node);
1424664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall
1425a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall
1426664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  if (!isPointerSizedReturn) {
1427a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    messageBB = CGF.Builder.GetInsertBlock();
1428a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    CGF.Builder.CreateBr(continueBB);
1429a54da05098aff1ca992f5292f3f6a39ec3cef31eDavid Chisnall    CGF.EmitBlock(continueBB);
1430664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    if (msgRet.isScalar()) {
1431664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      llvm::Value *v = msgRet.getScalarVal();
1432bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad      llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
1433664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(v, messageBB);
1434664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB);
1435664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      msgRet = RValue::get(phi);
1436664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    } else if (msgRet.isAggregate()) {
1437664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      llvm::Value *v = msgRet.getAggregateAddr();
1438bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad      llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
14392acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::PointerType *RetTy = cast<llvm::PointerType>(v->getType());
1440866163bf6c66211a9b4d5ba160ac02de04ce4be6David Chisnall      llvm::AllocaInst *NullVal =
1441866163bf6c66211a9b4d5ba160ac02de04ce4be6David Chisnall          CGF.CreateTempAlloca(RetTy->getElementType(), "null");
1442664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      CGF.InitTempAlloca(NullVal,
1443664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall          llvm::Constant::getNullValue(RetTy->getElementType()));
1444664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(v, messageBB);
1445664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(NullVal, startBB);
1446664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      msgRet = RValue::getAggregate(phi);
1447664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    } else /* isComplex() */ {
1448664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal();
1449bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad      llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2);
1450664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(v.first, messageBB);
1451664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()),
1452664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall          startBB);
1453bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad      llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2);
1454664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi2->addIncoming(v.second, messageBB);
1455664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()),
1456664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall          startBB);
1457664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall      msgRet = RValue::getComplex(phi, phi2);
1458664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall    }
1459664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  }
1460664b7c74aff2ccdf9d13eae08b3466326fc6cc1fDavid Chisnall  return msgRet;
146120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
146220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
14631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Generates a MethodList.  Used in construction of a objc_class and
146420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov/// objc_category structures.
1465795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendlingllvm::Constant *CGObjCGNU::
1466795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill WendlingGenerateMethodList(const StringRef &ClassName,
1467795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling                   const StringRef &CategoryName,
1468795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling                   ArrayRef<Selector> MethodSels,
1469795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling                   ArrayRef<llvm::Constant *> MethodTypes,
1470795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling                   bool isClassMethodList) {
14710f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  if (MethodSels.empty())
14720f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall    return NULLPtr;
14731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Get the method structure type.
14747650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ObjCMethodTy = llvm::StructType::get(
147520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty, // Really a selector, but the runtime creates it us.
147620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty, // Method types
1477c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    IMPTy, //Method pointer
14786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    nullptr);
147920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Methods;
148020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
148120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
148220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.clear();
14839f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    llvm::Constant *Method =
148420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
14859f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                                MethodSels[i],
14869f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall                                                isClassMethodList));
14879f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    assert(Method && "Can't generate metadata for method that doesn't exist");
14889f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
14899f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Elements.push_back(C);
14909f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Elements.push_back(MethodTypes[i]);
14919f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Method = llvm::ConstantExpr::getBitCast(Method,
1492c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall        IMPTy);
14939f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Elements.push_back(Method);
14949f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
149520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
149620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
149720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Array of method structures
149896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
14991e64a9531bf6e9d99ad3271b3a735ef702689b06Fariborz Jahanian                                                            Methods.size());
15007db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
1501fba676396703e87a5034e5f308aa30a633468f66Chris Lattner                                                         Methods);
150220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
150320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Structure containing list pointer, array and array count
1504c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner  llvm::StructType *ObjCMethodListTy = llvm::StructType::create(VMContext);
15059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(ObjCMethodListTy);
15069cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  ObjCMethodListTy->setBody(
15071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NextPtrTy,
15081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      IntTy,
150920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      ObjCMethodArrayTy,
15106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      nullptr);
151120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
151220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Methods.clear();
151303e205031b08669f05c41eed5b896fc94c4a12bbOwen Anderson  Methods.push_back(llvm::ConstantPointerNull::get(
151496e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson        llvm::PointerType::getUnqual(ObjCMethodListTy)));
1515917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  Methods.push_back(llvm::ConstantInt::get(Int32Ty, MethodTypes.size()));
151620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Methods.push_back(MethodArray);
15171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
151820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create an instance of the structure
151920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
152020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
152120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
152220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov/// Generates an IvarList.  Used in construction of a objc_class.
1523795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendlingllvm::Constant *CGObjCGNU::
1524795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill WendlingGenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
1525795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling                 ArrayRef<llvm::Constant *> IvarTypes,
1526795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling                 ArrayRef<llvm::Constant *> IvarOffsets) {
15271804463ee790ac3cc73f72597ccb38b983f7d263David Chisnall  if (IvarNames.size() == 0)
15281804463ee790ac3cc73f72597ccb38b983f7d263David Chisnall    return NULLPtr;
15291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Get the method structure type.
15307650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ObjCIvarTy = llvm::StructType::get(
153120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty,
153220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty,
153320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    IntTy,
15346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    nullptr);
153520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Ivars;
153620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
153720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
153820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.clear();
15398a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    Elements.push_back(IvarNames[i]);
15408a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    Elements.push_back(IvarTypes[i]);
154120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.push_back(IvarOffsets[i]);
154208e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson    Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
154320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
154420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
154520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Array of method structures
154696e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
154720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      IvarNames.size());
154820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
15491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
155020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
15514a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
15527db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
155320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Structure containing array and array count
15547650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
155520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    ObjCIvarArrayTy,
15566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    nullptr);
155720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
155820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create an instance of the structure
155920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
15600f984268b05edab2cc555a427c441baa9c252658Chris Lattner}
15610f984268b05edab2cc555a427c441baa9c252658Chris Lattner
156220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov/// Generate a class structure
156320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikovllvm::Constant *CGObjCGNU::GenerateClassStructure(
156420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *MetaClass,
156520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *SuperClass,
156620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    unsigned info,
1567d002cc6fc5b72bf00e3b7b4571ccf0f23c789b4bChris Lattner    const char *Name,
156820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *Version,
156920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *InstanceSize,
157020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *IVars,
157120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *Methods,
1572d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    llvm::Constant *Protocols,
1573d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    llvm::Constant *IvarOffsets,
15748c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall    llvm::Constant *Properties,
1575917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall    llvm::Constant *StrongIvarBitmap,
1576917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall    llvm::Constant *WeakIvarBitmap,
15778c757f9ea33dccff4da7810e0c1bda59c19ddc9aDavid Chisnall    bool isMeta) {
157820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Set up the class structure
157920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Note:  Several of these are char*s when they should be ids.  This is
158020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // because the runtime performs this translation on load.
1581d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  //
1582d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Fields marked New ABI are part of the GNUstep runtime.  We emit them
1583d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // anyway; the classes will still work with the GNU runtime, they will just
1584d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // be ignored.
15857650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ClassTy = llvm::StructType::get(
158613df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall      PtrToInt8Ty,        // isa
158720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrToInt8Ty,        // super_class
158820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrToInt8Ty,        // name
158920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      LongTy,             // version
159020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      LongTy,             // info
159120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      LongTy,             // instance_size
159220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      IVars->getType(),   // ivars
159320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      Methods->getType(), // methods
15941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // These are all filled in by the runtime, so we pretend
159520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // dtable
159620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // subclass_list
159720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // sibling_class
159820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // protocols
159920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy,              // gc_object_type
1600d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      // New ABI:
1601d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      LongTy,                 // abi_version
1602d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      IvarOffsets->getType(), // ivar_offsets
1603d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Properties->getType(),  // properties
16049d06ba82d8afb1cf01235f38f350ce5ad0c15444David Chisnall      IntPtrTy,               // strong_pointers
16059d06ba82d8afb1cf01235f38f350ce5ad0c15444David Chisnall      IntPtrTy,               // weak_pointers
16066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      nullptr);
16074a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
160820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Fill in the structure
160920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
16103c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
161120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(SuperClass);
1612d002cc6fc5b72bf00e3b7b4571ccf0f23c789b4bChris Lattner  Elements.push_back(MakeConstantString(Name, ".class_name"));
161320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(Zero);
16144a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Elements.push_back(llvm::ConstantInt::get(LongTy, info));
161505f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall  if (isMeta) {
161625a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow    llvm::DataLayout td(&TheModule);
1617e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck    Elements.push_back(
1618e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck        llvm::ConstantInt::get(LongTy,
1619e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck                               td.getTypeSizeInBits(ClassTy) /
1620e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck                                 CGM.getContext().getCharWidth()));
162105f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall  } else
162205f3a507bb53ed2bd5573d551a26654a604187ebDavid Chisnall    Elements.push_back(InstanceSize);
162320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(IVars);
162420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(Methods);
1625d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(NULLPtr);
1626d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(NULLPtr);
1627d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(NULLPtr);
16283c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
1629d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(NULLPtr);
1630917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  Elements.push_back(llvm::ConstantInt::get(LongTy, 1));
1631d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(IvarOffsets);
1632d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(Properties);
1633917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  Elements.push_back(StrongIvarBitmap);
1634917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  Elements.push_back(WeakIvarBitmap);
163520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create an instance of the structure
163641d63ed274d1bf7ba7459f218c8f6a385fc5c6d6David Chisnall  // This is now an externally visible symbol, so that we can speed up class
163713df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  // messages in the next ABI.  We may already have some weak references to
163813df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  // this, so check and fix them properly.
163913df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  std::string ClassSym((isMeta ? "_OBJC_METACLASS_": "_OBJC_CLASS_") +
164013df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall          std::string(Name));
164113df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  llvm::GlobalVariable *ClassRef = TheModule.getNamedGlobal(ClassSym);
164213df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  llvm::Constant *Class = MakeGlobal(ClassTy, Elements, ClassSym,
164313df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall          llvm::GlobalValue::ExternalLinkage);
164413df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  if (ClassRef) {
164513df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall      ClassRef->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(Class,
164613df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall                  ClassRef->getType()));
164713df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall      ClassRef->removeFromParent();
164813df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall      Class->setName(ClassSym);
164913df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  }
165013df6f6e2e6adfb9933ba1a16802d35942e26a5cDavid Chisnall  return Class;
165120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
165220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1653795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendlingllvm::Constant *CGObjCGNU::
1654795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill WendlingGenerateProtocolMethodList(ArrayRef<llvm::Constant *> MethodNames,
1655795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendling                           ArrayRef<llvm::Constant *> MethodTypes) {
16561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Get the method structure type.
16577650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
165820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
165920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    PtrToInt8Ty,
16606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    nullptr);
166120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Methods;
166220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
166320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
166420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.clear();
16651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Elements.push_back(MethodNames[i]);
16668a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    Elements.push_back(MethodTypes[i]);
166708e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson    Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
166820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
166996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
167020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      MethodNames.size());
16717db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
1672bb1c8600218b3244340331165fc7cba7bf227655Mike Stump                                                   Methods);
16737650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
16746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      IntTy, ObjCMethodArrayTy, nullptr);
167520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Methods.clear();
16764a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
167720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Methods.push_back(Array);
167820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
167920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
1680bb1c8600218b3244340331165fc7cba7bf227655Mike Stump
168120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov// Create the protocol list structure used in classes, categories and so on
1682795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendlingllvm::Constant *CGObjCGNU::GenerateProtocolList(ArrayRef<std::string>Protocols){
168396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
168420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      Protocols.size());
16857650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ProtocolListTy = llvm::StructType::get(
168620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrTy, //Should be a recurisve pointer, but it's always NULL here.
16878fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall      SizeTy,
168820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      ProtocolArrayTy,
16896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      nullptr);
16901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::vector<llvm::Constant*> Elements;
169120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
169220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      iter != endIter ; iter++) {
16936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    llvm::Constant *protocol = nullptr;
1694ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    llvm::StringMap<llvm::Constant*>::iterator value =
1695ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall      ExistingProtocols.find(*iter);
1696ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    if (value == ExistingProtocols.end()) {
1697f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian      protocol = GenerateEmptyProtocol(*iter);
1698ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    } else {
1699ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall      protocol = value->getValue();
1700ff80fabfa42d4aadbbaab4d61fa518d80b5a981aDavid Chisnall    }
17013c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
1702a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson                                                           PtrToInt8Ty);
170320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    Elements.push_back(Ptr);
170420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
17057db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
170620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      Elements);
170720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
170820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(NULLPtr);
17094a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson  Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
171020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(ProtocolArray);
171120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
171220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
171320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1714bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::Value *CGObjCGNU::GenerateProtocolRef(CodeGenFunction &CGF,
1715af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar                                            const ObjCProtocolDecl *PD) {
1716f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
17172acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *T =
1718f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian    CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
1719bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return CGF.Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
1720f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian}
1721f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian
1722f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanianllvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
1723f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  const std::string &ProtocolName) {
17245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<std::string, 0> EmptyStringVector;
17255f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 0> EmptyConstantVector;
1726f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian
1727f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
1728d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *MethodList =
1729f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian    GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
1730f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  // Protocols are objects containing lists of the methods implemented and
1731f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  // protocols adopted.
17327650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
1733f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian      PtrToInt8Ty,
1734f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian      ProtocolList->getType(),
1735d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList->getType(),
1736d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList->getType(),
1737d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList->getType(),
1738d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList->getType(),
17396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      nullptr);
17401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::vector<llvm::Constant*> Elements;
1741f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  // The isa pointer must be set to a magic number so the runtime knows it's
1742f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  // the correct layout.
17433c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getIntToPtr(
1744917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall        llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
1745f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1746f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  Elements.push_back(ProtocolList);
1747d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MethodList);
1748d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MethodList);
1749d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MethodList);
1750d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MethodList);
1751f8c4f5469d7db591eeacfc3381d91f773fb7af43Fariborz Jahanian  return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
175220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
175320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1754af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbarvoid CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
1755af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  ASTContext &Context = CGM.getContext();
17568ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string ProtocolName = PD->getNameAsString();
17571d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor
17581d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor  // Use the protocol definition, if there is one.
17591d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor  if (const ObjCProtocolDecl *Def = PD->getDefinition())
17601d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor    PD = Def;
17611d784b277cdfd4eba03680715d2a082b3f28d295Douglas Gregor
17625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<std::string, 16> Protocols;
1763651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *PI : PD->protocols())
1764651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Protocols.push_back(PI->getNameAsString());
17655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> InstanceMethodNames;
17665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
17675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames;
17685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes;
1769651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *I : PD->instance_methods()) {
1770af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar    std::string TypeStr;
1771651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Context.getObjCEncodingForMethodDecl(I, TypeStr);
1772651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (I->getImplementationControl() == ObjCMethodDecl::Optional) {
1773d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalInstanceMethodNames.push_back(
1774651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          MakeConstantString(I->getSelector().getAsString()));
1775d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1776a904e01839e8244d39b3d9bfc749ec93f1cbe1e1David Chisnall    } else {
1777a904e01839e8244d39b3d9bfc749ec93f1cbe1e1David Chisnall      InstanceMethodNames.push_back(
1778651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          MakeConstantString(I->getSelector().getAsString()));
1779a904e01839e8244d39b3d9bfc749ec93f1cbe1e1David Chisnall      InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1780d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1781af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  }
1782af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  // Collect information about class methods:
17835f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> ClassMethodNames;
17845f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> ClassMethodTypes;
17855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> OptionalClassMethodNames;
17865f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes;
1787651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *I : PD->class_methods()) {
1788af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar    std::string TypeStr;
1789651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Context.getObjCEncodingForMethodDecl(I,TypeStr);
1790651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (I->getImplementationControl() == ObjCMethodDecl::Optional) {
1791d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalClassMethodNames.push_back(
1792651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          MakeConstantString(I->getSelector().getAsString()));
1793d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr));
1794a904e01839e8244d39b3d9bfc749ec93f1cbe1e1David Chisnall    } else {
1795a904e01839e8244d39b3d9bfc749ec93f1cbe1e1David Chisnall      ClassMethodNames.push_back(
1796651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          MakeConstantString(I->getSelector().getAsString()));
1797a904e01839e8244d39b3d9bfc749ec93f1cbe1e1David Chisnall      ClassMethodTypes.push_back(MakeConstantString(TypeStr));
1798d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1799af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar  }
180020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
180120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
180220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *InstanceMethodList =
180320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
180420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ClassMethodList =
180520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
1806d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalInstanceMethodList =
1807d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    GenerateProtocolMethodList(OptionalInstanceMethodNames,
1808d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian            OptionalInstanceMethodTypes);
1809d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalClassMethodList =
1810d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    GenerateProtocolMethodList(OptionalClassMethodNames,
1811d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian            OptionalClassMethodTypes);
1812d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1813d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Property metadata: name, attributes, isSynthesized, setter name, setter
1814d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // types, getter name, getter types.
1815d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // The isSynthesized value is always set to 0 in a protocol.  It exists to
1816d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // simplify the runtime library by allowing it to use the same data
1817d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // structures for protocol metadata everywhere.
18187650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
1819de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall          PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty,
18206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, nullptr);
1821d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> Properties;
1822d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> OptionalProperties;
1823d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1824d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Add all of the property methods need adding to the method list and to the
1825d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // property metadata list.
1826651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (auto *property : PD->properties()) {
1827d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    std::vector<llvm::Constant*> Fields;
1828d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
18296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Fields.push_back(MakePropertyEncodingString(property, nullptr));
1830de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    PushPropertyAttributes(Fields, property);
1831891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall
1832d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1833d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      std::string TypeStr;
1834d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1835d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1836d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      InstanceMethodTypes.push_back(TypeEncoding);
1837d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1838d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(TypeEncoding);
1839d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1840d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1841d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1842d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1843d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1844d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      std::string TypeStr;
1845d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1846d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1847d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      InstanceMethodTypes.push_back(TypeEncoding);
1848d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1849d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(TypeEncoding);
1850d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1851d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1852d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
1853d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1854d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) {
1855d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1856d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
1857d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1858d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
1859d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  }
1860d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyArray = llvm::ConstantArray::get(
1861d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties);
1862d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant* PropertyListInitFields[] =
1863d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1864d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1865d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyListInit =
1866c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner      llvm::ConstantStruct::getAnon(PropertyListInitFields);
1867d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule,
1868d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage,
1869d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      PropertyListInit, ".objc_property_list");
1870d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1871d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalPropertyArray =
1872d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy,
1873d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          OptionalProperties.size()) , OptionalProperties);
1874d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant* OptionalPropertyListInitFields[] = {
1875d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr,
1876d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalPropertyArray };
1877d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1878d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalPropertyListInit =
1879c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner      llvm::ConstantStruct::getAnon(OptionalPropertyListInitFields);
1880d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule,
1881d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          OptionalPropertyListInit->getType(), false,
1882d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit,
1883d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          ".objc_property_list");
1884d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
188520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Protocols are objects containing lists of the methods implemented and
188620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // protocols adopted.
18877650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
188820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      PtrToInt8Ty,
188920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      ProtocolList->getType(),
189020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      InstanceMethodList->getType(),
189120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      ClassMethodList->getType(),
1892d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalInstanceMethodList->getType(),
1893d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalClassMethodList->getType(),
1894d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      PropertyList->getType(),
1895d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      OptionalPropertyList->getType(),
18966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      nullptr);
18971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::vector<llvm::Constant*> Elements;
189820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // The isa pointer must be set to a magic number so the runtime knows it's
189920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // the correct layout.
19003c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getIntToPtr(
1901917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall        llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
190220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
190320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(ProtocolList);
190420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(InstanceMethodList);
190520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(ClassMethodList);
1906d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(OptionalInstanceMethodList);
1907d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(OptionalClassMethodList);
1908d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(PropertyList);
1909d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(OptionalPropertyList);
19101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ExistingProtocols[ProtocolName] =
19113c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
191220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov          ".objc_protocol"), IdTy);
191320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
1914c4a77906c259cba58c147d8468c406a430ecdcbbDmitri Gribenkovoid CGObjCGNU::GenerateProtocolHolderCategory() {
1915d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Collect information about instance methods
19165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Selector, 1> MethodSels;
19175f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 1> MethodTypes;
1918d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
1919d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> Elements;
1920d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
1921d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  const std::string CategoryName = "AnotherHack";
1922d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MakeConstantString(CategoryName));
1923d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(MakeConstantString(ClassName));
1924d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Instance method list
1925d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1926d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));
1927d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Class method list
1928d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1929d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));
1930d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Protocol list
1931d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy,
1932d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ExistingProtocols.size());
19337650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *ProtocolListTy = llvm::StructType::get(
1934d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      PtrTy, //Should be a recurisve pointer, but it's always NULL here.
19358fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall      SizeTy,
1936d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ProtocolArrayTy,
19376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      nullptr);
1938d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> ProtocolElements;
1939d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  for (llvm::StringMapIterator<llvm::Constant*> iter =
1940d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian       ExistingProtocols.begin(), endIter = ExistingProtocols.end();
1941d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian       iter != endIter ; iter++) {
1942d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(),
1943d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian            PtrTy);
1944d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    ProtocolElements.push_back(Ptr);
1945d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  }
1946d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1947d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      ProtocolElements);
1948d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ProtocolElements.clear();
1949d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ProtocolElements.push_back(NULLPtr);
1950d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ProtocolElements.push_back(llvm::ConstantInt::get(LongTy,
1951d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian              ExistingProtocols.size()));
1952d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ProtocolElements.push_back(ProtocolArray);
1953d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy,
1954d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian                  ProtocolElements, ".objc_protocol_list"), PtrTy));
1955d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  Categories.push_back(llvm::ConstantExpr::getBitCast(
19567650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner        MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
19576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            PtrTy, PtrTy, PtrTy, nullptr), Elements), PtrTy));
1958d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian}
195920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
1960917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall/// Libobjc2 uses a bitfield representation where small(ish) bitfields are
1961917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall/// stored in a 64-bit value with the low bit set to 1 and the remaining 63
1962917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall/// bits set to their values, LSB first, while larger ones are stored in a
1963917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall/// structure of this / form:
1964917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall///
1965917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall/// struct { int32_t length; int32_t values[length]; };
1966917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall///
1967917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall/// The values in the array are stored in host-endian format, with the least
1968917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall/// significant bit being assumed to come first in the bitfield.  Therefore, a
1969917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall/// bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] }, while a
1970917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall/// bitfield / with the 63rd bit set will be 1<<64.
1971795b10062c2eaffae9e04241fb1a73cdbcb24a37Bill Wendlingllvm::Constant *CGObjCGNU::MakeBitField(ArrayRef<bool> bits) {
1972917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  int bitCount = bits.size();
1973651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  int ptrBits = CGM.getDataLayout().getPointerSizeInBits();
19749d06ba82d8afb1cf01235f38f350ce5ad0c15444David Chisnall  if (bitCount < ptrBits) {
1975917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall    uint64_t val = 1;
1976917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall    for (int i=0 ; i<bitCount ; ++i) {
1977e3c944a9f620dacb0c29d16e7d61c8e7fca10963Eli Friedman      if (bits[i]) val |= 1ULL<<(i+1);
1978917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall    }
19799d06ba82d8afb1cf01235f38f350ce5ad0c15444David Chisnall    return llvm::ConstantInt::get(IntPtrTy, val);
1980917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  }
1981cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<llvm::Constant *, 8> values;
1982917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  int v=0;
1983917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  while (v < bitCount) {
1984917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall    int32_t word = 0;
1985917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall    for (int i=0 ; (i<32) && (v<bitCount)  ; ++i) {
1986917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall      if (bits[v]) word |= 1<<i;
1987917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall      v++;
1988917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall    }
1989917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall    values.push_back(llvm::ConstantInt::get(Int32Ty, word));
1990917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  }
1991917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  llvm::ArrayType *arrayTy = llvm::ArrayType::get(Int32Ty, values.size());
1992917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  llvm::Constant *array = llvm::ConstantArray::get(arrayTy, values);
1993917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  llvm::Constant *fields[2] = {
1994917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall      llvm::ConstantInt::get(Int32Ty, values.size()),
1995917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall      array };
1996917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  llvm::Constant *GS = MakeGlobal(llvm::StructType::get(Int32Ty, arrayTy,
19976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        nullptr), fields);
199849de5289acea91c9d224c9aadd4d527e8f9fdaffDavid Chisnall  llvm::Constant *ptr = llvm::ConstantExpr::getPtrToInt(GS, IntPtrTy);
199949de5289acea91c9d224c9aadd4d527e8f9fdaffDavid Chisnall  return ptr;
2000917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall}
2001917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall
20027ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbarvoid CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
20038ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string ClassName = OCD->getClassInterface()->getNameAsString();
20048ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string CategoryName = OCD->getNameAsString();
20057ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about instance methods
20065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Selector, 16> InstanceMethodSels;
20075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
2008651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *I : OCD->instance_methods()) {
2009651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    InstanceMethodSels.push_back(I->getSelector());
20107ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    std::string TypeStr;
2011651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    CGM.getContext().getObjCEncodingForMethodDecl(I,TypeStr);
20128a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
20137ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
20147ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
20157ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about class methods
20165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Selector, 16> ClassMethodSels;
20175f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> ClassMethodTypes;
2018651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *I : OCD->class_methods()) {
2019651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ClassMethodSels.push_back(I->getSelector());
20207ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    std::string TypeStr;
2021651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    CGM.getContext().getObjCEncodingForMethodDecl(I,TypeStr);
20228a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    ClassMethodTypes.push_back(MakeConstantString(TypeStr));
20237ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
20247ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
20257ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect the names of referenced protocols
20265f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<std::string, 16> Protocols;
2027ad9e06d82ae627e858e5bfe4a7f1f9337b5b0a15David Chisnall  const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl();
2028ad9e06d82ae627e858e5bfe4a7f1f9337b5b0a15David Chisnall  const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols();
20297ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
20307ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar       E = Protos.end(); I != E; ++I)
2031d9d22dd9c94618490dbffb0e2caf222530ca39d3Chris Lattner    Protocols.push_back((*I)->getNameAsString());
20327ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
203320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
203420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(MakeConstantString(CategoryName));
203520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(MakeConstantString(ClassName));
20361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Instance method list
20373c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
2038a4210076fc1d7ac0a20b8b4a79e18a8ae33b9c69Chris Lattner          ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
203920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov          false), PtrTy));
204020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Class method list
20413c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
2042a4210076fc1d7ac0a20b8b4a79e18a8ae33b9c69Chris Lattner          ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
204320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        PtrTy));
204420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Protocol list
20453c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Elements.push_back(llvm::ConstantExpr::getBitCast(
204620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        GenerateProtocolList(Protocols), PtrTy));
20473c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  Categories.push_back(llvm::ConstantExpr::getBitCast(
20487650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner        MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
20496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines            PtrTy, PtrTy, PtrTy, nullptr), Elements), PtrTy));
205020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
20517ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
2052d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanianllvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID,
20535f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVectorImpl<Selector> &InstanceMethodSels,
20545f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) {
2055d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  ASTContext &Context = CGM.getContext();
2056de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall  // Property metadata: name, attributes, attributes2, padding1, padding2,
2057de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall  // setter name, setter types, getter name, getter types.
20587650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
2059de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall          PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty,
20606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, nullptr);
2061d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> Properties;
2062d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
2063d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Add all of the property methods need adding to the method list and to the
2064d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // property metadata list.
2065651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (auto *propertyImpl : OID->property_impls()) {
2066d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    std::vector<llvm::Constant*> Fields;
2067651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ObjCPropertyDecl *property = propertyImpl->getPropertyDecl();
206842ba04a072c69d84a6660e4498215086662910acDavid Chisnall    bool isSynthesized = (propertyImpl->getPropertyImplementation() ==
206942ba04a072c69d84a6660e4498215086662910acDavid Chisnall        ObjCPropertyImplDecl::Synthesize);
2070de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    bool isDynamic = (propertyImpl->getPropertyImplementation() ==
2071de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall        ObjCPropertyImplDecl::Dynamic);
2072d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
2073891dac73bdb090ff845982e4334f5b4c18353dbeDavid Chisnall    Fields.push_back(MakePropertyEncodingString(property, OID));
2074de38cb1fc8429e7adbd59308d19628cfda5aeee6David Chisnall    PushPropertyAttributes(Fields, property, isSynthesized, isDynamic);
2075d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
2076d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      std::string TypeStr;
2077d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Context.getObjCEncodingForMethodDecl(getter,TypeStr);
2078d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
207942ba04a072c69d84a6660e4498215086662910acDavid Chisnall      if (isSynthesized) {
208042ba04a072c69d84a6660e4498215086662910acDavid Chisnall        InstanceMethodTypes.push_back(TypeEncoding);
208142ba04a072c69d84a6660e4498215086662910acDavid Chisnall        InstanceMethodSels.push_back(getter->getSelector());
208242ba04a072c69d84a6660e4498215086662910acDavid Chisnall      }
2083d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
2084d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(TypeEncoding);
2085d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
2086d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
2087d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
2088d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
2089d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
2090d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      std::string TypeStr;
2091d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Context.getObjCEncodingForMethodDecl(setter,TypeStr);
2092d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
209342ba04a072c69d84a6660e4498215086662910acDavid Chisnall      if (isSynthesized) {
209442ba04a072c69d84a6660e4498215086662910acDavid Chisnall        InstanceMethodTypes.push_back(TypeEncoding);
209542ba04a072c69d84a6660e4498215086662910acDavid Chisnall        InstanceMethodSels.push_back(setter->getSelector());
209642ba04a072c69d84a6660e4498215086662910acDavid Chisnall      }
2097d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
2098d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(TypeEncoding);
2099d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    } else {
2100d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
2101d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      Fields.push_back(NULLPtr);
2102d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    }
2103d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
2104d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  }
2105d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::ArrayType *PropertyArrayTy =
2106d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      llvm::ArrayType::get(PropertyMetadataTy, Properties.size());
2107d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy,
2108d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          Properties);
2109d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant* PropertyListInitFields[] =
2110d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
2111d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
2112d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *PropertyListInit =
2113c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner      llvm::ConstantStruct::getAnon(PropertyListInitFields);
2114d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false,
2115d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          llvm::GlobalValue::InternalLinkage, PropertyListInit,
2116d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          ".objc_property_list");
2117d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian}
2118d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
211929254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnallvoid CGObjCGNU::RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {
212029254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall  // Get the class declaration for which the alias is specified.
212129254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall  ObjCInterfaceDecl *ClassDecl =
212229254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    const_cast<ObjCInterfaceDecl *>(OAD->getClassInterface());
212329254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall  std::string ClassName = ClassDecl->getNameAsString();
212429254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall  std::string AliasName = OAD->getNameAsString();
212529254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall  ClassAliases.push_back(ClassAliasPair(ClassName,AliasName));
212629254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall}
212729254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall
21287ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbarvoid CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
21297ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  ASTContext &Context = CGM.getContext();
21307ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
21317ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Get the superclass name.
21321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ObjCInterfaceDecl * SuperClassDecl =
21337ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    OID->getClassInterface()->getSuperClass();
21348ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string SuperClassName;
21352a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  if (SuperClassDecl) {
21368ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner    SuperClassName = SuperClassDecl->getNameAsString();
21372a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner    EmitClassRef(SuperClassName);
21382a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  }
21397ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
21407ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Get the class name
214109dc6660487fc2f4a4bb2032e30123d3e0da7230Chris Lattner  ObjCInterfaceDecl *ClassDecl =
214209dc6660487fc2f4a4bb2032e30123d3e0da7230Chris Lattner    const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
21438ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  std::string ClassName = ClassDecl->getNameAsString();
21442a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  // Emit the symbol that is used to generate linker errors if this class is
21452a8e4e1d8548dc3e30d7c9ba92127c7884a11448Chris Lattner  // referenced in other modules but not declared.
2146c51db23f58bd887b16cf91554aa86c430a2496e7Fariborz Jahanian  std::string classSymbolName = "__objc_class_name_" + ClassName;
21471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (llvm::GlobalVariable *symbol =
2148c51db23f58bd887b16cf91554aa86c430a2496e7Fariborz Jahanian      TheModule.getGlobalVariable(classSymbolName)) {
21494a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson    symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
2150c51db23f58bd887b16cf91554aa86c430a2496e7Fariborz Jahanian  } else {
21511c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    new llvm::GlobalVariable(TheModule, LongTy, false,
21524a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson    llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0),
21531c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    classSymbolName);
2154c51db23f58bd887b16cf91554aa86c430a2496e7Fariborz Jahanian  }
21551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21562bebbf0acee55404de4b8846713b64429e744e8fDaniel Dunbar  // Get the size of instances.
21575f022d82696c84e4d127c558871d68ac6273274eKen Dyck  int instanceSize =
21585f022d82696c84e4d127c558871d68ac6273274eKen Dyck    Context.getASTObjCImplementationLayout(OID).getSize().getQuantity();
21597ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
21607ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about instance variables.
21615f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> IvarNames;
21625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> IvarTypes;
21635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> IvarOffsets;
21641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2165d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  std::vector<llvm::Constant*> IvarOffsetValues;
2166917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  SmallVector<bool, 16> WeakIvars;
2167917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  SmallVector<bool, 16> StrongIvars;
2168d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
21691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  int superInstanceSize = !SuperClassDecl ? 0 :
21705f022d82696c84e4d127c558871d68ac6273274eKen Dyck    Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity();
21719cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // For non-fragile ivars, set the instance size to 0 - {the size of just this
21729cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // class}.  The runtime will then set this to the correct value on load.
21737edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith  if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
21749cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    instanceSize = 0 - (instanceSize - superInstanceSize);
21759cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  }
21767f63cb098f2461643f71ea590c1a477603f27f63David Chisnall
2177db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose  for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
2178db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose       IVD = IVD->getNextIvar()) {
21797ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      // Store the name
21807f63cb098f2461643f71ea590c1a477603f27f63David Chisnall      IvarNames.push_back(MakeConstantString(IVD->getNameAsString()));
21817ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      // Get the type encoding for this ivar
21827ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      std::string TypeStr;
21837f63cb098f2461643f71ea590c1a477603f27f63David Chisnall      Context.getObjCEncodingForType(IVD->getType(), TypeStr);
21848a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      IvarTypes.push_back(MakeConstantString(TypeStr));
21857ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar      // Get the offset
2186e5b4666d9b86ea30b90b599691e75d380f84ddd6Eli Friedman      uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
2187aecbf24a6bad049437f8fec97e557d414003a3dbDavid Chisnall      uint64_t Offset = BaseOffset;
21887edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith      if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
2189d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian        Offset = BaseOffset - superInstanceSize;
21909cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian      }
219163ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset);
219263ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      // Create the direct offset value
219363ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      std::string OffsetName = "__objc_ivar_offset_value_" + ClassName +"." +
219463ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall          IVD->getNameAsString();
219563ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(OffsetName);
219663ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      if (OffsetVar) {
219763ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall        OffsetVar->setInitializer(OffsetValue);
219863ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall        // If this is the real definition, change its linkage type so that
219963ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall        // different modules will use this one, rather than their private
220063ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall        // copy.
220163ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall        OffsetVar->setLinkage(llvm::GlobalValue::ExternalLinkage);
220263ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      } else
220363ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall        OffsetVar = new llvm::GlobalVariable(TheModule, IntTy,
2204d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          false, llvm::GlobalValue::ExternalLinkage,
220563ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall          OffsetValue,
2206d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          "__objc_ivar_offset_value_" + ClassName +"." +
220763ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall          IVD->getNameAsString());
220863ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      IvarOffsets.push_back(OffsetValue);
220963ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      IvarOffsetValues.push_back(OffsetVar);
2210917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall      Qualifiers::ObjCLifetime lt = IVD->getType().getQualifiers().getObjCLifetime();
2211917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall      switch (lt) {
2212917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall        case Qualifiers::OCL_Strong:
2213917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall          StrongIvars.push_back(true);
2214917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall          WeakIvars.push_back(false);
2215917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall          break;
2216917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall        case Qualifiers::OCL_Weak:
2217917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall          StrongIvars.push_back(false);
2218917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall          WeakIvars.push_back(true);
2219917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall          break;
2220917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall        default:
2221917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall          StrongIvars.push_back(false);
2222917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall          WeakIvars.push_back(false);
2223917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall      }
22247ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
2225917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  llvm::Constant *StrongIvarBitmap = MakeBitField(StrongIvars);
2226917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  llvm::Constant *WeakIvarBitmap = MakeBitField(WeakIvars);
22279f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::GlobalVariable *IvarOffsetArray =
22289f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    MakeGlobalArray(PtrToIntTy, IvarOffsetValues, ".ivar.offsets");
22299f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
22307ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
22317ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about instance methods
22325f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Selector, 16> InstanceMethodSels;
22335f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
2234651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *I : OID->instance_methods()) {
2235651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    InstanceMethodSels.push_back(I->getSelector());
22367ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    std::string TypeStr;
2237651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Context.getObjCEncodingForMethodDecl(I,TypeStr);
22388a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
22397ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
2240d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
2241d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels,
2242d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian          InstanceMethodTypes);
2243d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
22447ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
22457ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect information about class methods
22465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Selector, 16> ClassMethodSels;
22475f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 16> ClassMethodTypes;
2248651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *I : OID->class_methods()) {
2249651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ClassMethodSels.push_back(I->getSelector());
22507ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar    std::string TypeStr;
2251651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Context.getObjCEncodingForMethodDecl(I,TypeStr);
22528a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    ClassMethodTypes.push_back(MakeConstantString(TypeStr));
22537ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  }
22547ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar  // Collect the names of referenced protocols
22555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<std::string, 16> Protocols;
2256651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *I : ClassDecl->protocols())
2257651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Protocols.push_back(I->getNameAsString());
22587ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
225920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Get the superclass pointer.
226020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *SuperClass;
22618ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  if (!SuperClassName.empty()) {
226220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
226320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  } else {
226403e205031b08669f05c41eed5b896fc94c4a12bbOwen Anderson    SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
226520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
226620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Empty vector used to construct empty method lists
22675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Constant*, 1>  empty;
226820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Generate the method and instance variable lists
226920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
2270a4210076fc1d7ac0a20b8b4a79e18a8ae33b9c69Chris Lattner      InstanceMethodSels, InstanceMethodTypes, false);
227120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
2272a4210076fc1d7ac0a20b8b4a79e18a8ae33b9c69Chris Lattner      ClassMethodSels, ClassMethodTypes, true);
227320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
227420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      IvarOffsets);
22751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
22768a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // we emit a symbol containing the offset for each ivar in the class.  This
22778a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // allows code compiled for the non-Fragile ABI to inherit from code compiled
22788a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // for the legacy ABI, without causing problems.  The converse is also
22798a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // possible, but causes all ivar accesses to be fragile.
2280e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall
22818a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // Offset pointer for getting at the correct field in the ivar list when
22828a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // setting up the alias.  These are: The base address for the global, the
22838a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // ivar array (second field), the ivar in this list (set for each ivar), and
22848a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  // the offset (third field in ivar structure)
2285917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall  llvm::Type *IndexTy = Int32Ty;
22868a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
22876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      llvm::ConstantInt::get(IndexTy, 1), nullptr,
22888a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      llvm::ConstantInt::get(IndexTy, 2) };
22898a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall
2290db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose  unsigned ivarIndex = 0;
2291db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose  for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
2292db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose       IVD = IVD->getNextIvar()) {
22938a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
2294e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall          + IVD->getNameAsString();
2295db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose      offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, ivarIndex);
22968a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      // Get the correct ivar field
22978a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
2298a5c04344fa70d6eec34344760c1fe511e16f2d76Jay Foad              IvarList, offsetPointerIndexes);
2299e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall      // Get the existing variable, if one exists.
23008a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
23018a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      if (offset) {
230274a1a1f0b6f6c4468444196a02c033be521f7d79Ted Kremenek        offset->setInitializer(offsetValue);
230374a1a1f0b6f6c4468444196a02c033be521f7d79Ted Kremenek        // If this is the real definition, change its linkage type so that
230474a1a1f0b6f6c4468444196a02c033be521f7d79Ted Kremenek        // different modules will use this one, rather than their private
230574a1a1f0b6f6c4468444196a02c033be521f7d79Ted Kremenek        // copy.
230674a1a1f0b6f6c4468444196a02c033be521f7d79Ted Kremenek        offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
23078a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      } else {
230874a1a1f0b6f6c4468444196a02c033be521f7d79Ted Kremenek        // Add a new alias if there isn't one already.
230974a1a1f0b6f6c4468444196a02c033be521f7d79Ted Kremenek        offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
231074a1a1f0b6f6c4468444196a02c033be521f7d79Ted Kremenek                false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
231174a1a1f0b6f6c4468444196a02c033be521f7d79Ted Kremenek        (void) offset; // Silence dead store warning.
23128a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      }
2313db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose      ++ivarIndex;
23148a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  }
23159d06ba82d8afb1cf01235f38f350ce5ad0c15444David Chisnall  llvm::Constant *ZeroPtr = llvm::ConstantInt::get(IntPtrTy, 0);
231620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  //Generate metaclass for class methods
231720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
23186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      NULLPtr, 0x12L, ClassName.c_str(), nullptr, Zeros[0], GenerateIvarList(
2319917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall        empty, empty, empty), ClassMethodList, NULLPtr,
23209d06ba82d8afb1cf01235f38f350ce5ad0c15444David Chisnall      NULLPtr, NULLPtr, ZeroPtr, ZeroPtr, true);
23215efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar
232220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Generate the class structure
23238ec03f58c33c33a917f54bb7f2cd61b6d7ffe0caChris Lattner  llvm::Constant *ClassStruct =
2324d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian    GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L,
23256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                           ClassName.c_str(), nullptr,
23264a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson      llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
2327d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian      MethodList, GenerateProtocolList(Protocols), IvarOffsetArray,
2328917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall      Properties, StrongIvarBitmap, WeakIvarBitmap);
23295efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar
23305efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  // Resolve the class aliases, if they exist.
23315efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  if (ClassPtrAlias) {
23320b9c22b61a3c99c8c0e8d898adb0f60afa76584eDavid Chisnall    ClassPtrAlias->replaceAllUsesWith(
23333c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson        llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
23340b9c22b61a3c99c8c0e8d898adb0f60afa76584eDavid Chisnall    ClassPtrAlias->eraseFromParent();
23356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ClassPtrAlias = nullptr;
23365efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  }
23375efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  if (MetaClassPtrAlias) {
23380b9c22b61a3c99c8c0e8d898adb0f60afa76584eDavid Chisnall    MetaClassPtrAlias->replaceAllUsesWith(
23393c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson        llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
23400b9c22b61a3c99c8c0e8d898adb0f60afa76584eDavid Chisnall    MetaClassPtrAlias->eraseFromParent();
23416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    MetaClassPtrAlias = nullptr;
23425efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar  }
23435efccb1ab81a029ddeb3ea18e239d0ddc124ec2bDaniel Dunbar
234420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Add class structure to list to be added to the symtab later
23453c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
234620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Classes.push_back(ClassStruct);
234720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
234820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
2349c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian
23501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Function *CGObjCGNU::ModuleInitFunction() {
235120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Only emit an ObjC load function if no Objective-C stuff has been called
235220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
23539f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      ExistingProtocols.empty() && SelectorTable.empty())
23546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
23551b8956e1ce4c5770982d6e59edb16979b500677aEli Friedman
2356d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  // Add all referenced protocols to a category.
2357d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian  GenerateProtocolHolderCategory();
2358d9a1db3a4ded1c8f0daa43c0d6167576e2766453Fariborz Jahanian
23592acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
2360e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner          SelectorTy->getElementType());
2361ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  llvm::Type *SelStructPtrTy = SelectorTy;
23626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!SelStructTy) {
23636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, nullptr);
236496e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
2365e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner  }
2366e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner
236720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Elements;
236871238f67c5df13eef19450d66b7a7ab28377192aChris Lattner  llvm::Constant *Statics = NULLPtr;
236920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Generate statics list:
237071238f67c5df13eef19450d66b7a7ab28377192aChris Lattner  if (ConstantStrings.size()) {
237196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
237271238f67c5df13eef19450d66b7a7ab28377192aChris Lattner        ConstantStrings.size() + 1);
237371238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    ConstantStrings.push_back(NULLPtr);
23748a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall
23754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
23769f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
23771b0969590e712d7d52fc9c0d43d3ab85c36d07a6Daniel Dunbar    if (StringClass.empty()) StringClass = "NXConstantString";
23789f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
23798a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    Elements.push_back(MakeConstantString(StringClass,
23808a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall                ".objc_static_class_name"));
23817db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson    Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
238271238f67c5df13eef19450d66b7a7ab28377192aChris Lattner       ConstantStrings));
23831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::StructType *StaticsListTy =
23846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, nullptr);
2385a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson    llvm::Type *StaticsListPtrTy =
238696e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      llvm::PointerType::getUnqual(StaticsListTy);
238771238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
23881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::ArrayType *StaticsListArrayTy =
238996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      llvm::ArrayType::get(StaticsListPtrTy, 2);
239071238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    Elements.clear();
239171238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    Elements.push_back(Statics);
2392c9c88b4159791c48e486ca94e3743b5979e2b7a6Owen Anderson    Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
239371238f67c5df13eef19450d66b7a7ab28377192aChris Lattner    Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
23943c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
239571238f67c5df13eef19450d66b7a7ab28377192aChris Lattner  }
239620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Array of classes, categories, and constant objects
239796e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
239820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      Classes.size() + Categories.size()  + 2);
23997650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
24000032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                                     llvm::Type::getInt16Ty(VMContext),
24010032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                                     llvm::Type::getInt16Ty(VMContext),
24026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                                     ClassListTy, nullptr);
240320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
240420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
240520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Pointer to an array of selectors used in this module.
240620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::vector<llvm::Constant*> Selectors;
24079f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  std::vector<llvm::GlobalAlias*> SelectorAliases;
24089f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  for (SelectorMap::iterator iter = SelectorTable.begin(),
24099f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      iterEnd = SelectorTable.end(); iter != iterEnd ; ++iter) {
24109f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
24119f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    std::string SelNameStr = iter->first.getAsString();
24129f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    llvm::Constant *SelName = ExportUniqueString(SelNameStr, ".objc_sel_name");
24139f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
24145f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVectorImpl<TypedSelector> &Types = iter->second;
24155f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
24169f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        e = Types.end() ; i!=e ; i++) {
24179f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
24189f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      llvm::Constant *SelectorTypeEncoding = NULLPtr;
24199f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      if (!i->first.empty())
24209f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall        SelectorTypeEncoding = MakeConstantString(i->first, ".objc_sel_types");
24219f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
24229f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Elements.push_back(SelName);
24239f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Elements.push_back(SelectorTypeEncoding);
24249f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
24259f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      Elements.clear();
24269f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
24279f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      // Store the selector alias for later replacement
24289f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall      SelectorAliases.push_back(i->second);
24299f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    }
243020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
24319f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  unsigned SelectorCount = Selectors.size();
24329f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // NULL-terminate the selector list.  This should not actually be required,
24339f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // because the selector list has a length field.  Unfortunately, the GCC
24349f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // runtime decides to ignore the length field and expects a NULL terminator,
24359f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // and GCC cooperates with this by always setting the length to 0.
243620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(NULLPtr);
243720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(NULLPtr);
243808e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson  Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
243920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
24409f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
244120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Number of static selectors
24429f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  Elements.push_back(llvm::ConstantInt::get(LongTy, SelectorCount));
24439f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  llvm::Constant *SelectorList = MakeGlobalArray(SelStructTy, Selectors,
244420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov          ".objc_selector_list");
24451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
2446e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner    SelStructPtrTy));
244720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
244820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Now that all of the static selectors exist, create pointers to them.
24499f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  for (unsigned int i=0 ; i<SelectorCount ; i++) {
24509f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
245120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov    llvm::Constant *Idxs[] = {Zeros[0],
2452917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall      llvm::ConstantInt::get(Int32Ty, i), Zeros[0]};
24539f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    // FIXME: We're generating redundant loads and stores here!
2454c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    llvm::Constant *SelPtr = llvm::ConstantExpr::getGetElementPtr(SelectorList,
2455a5c04344fa70d6eec34344760c1fe511e16f2d76Jay Foad        makeArrayRef(Idxs, 2));
2456e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner    // If selectors are defined as an opaque type, cast the pointer to this
2457e160c9b09d2f5098dfdcca99b9c6485487e0c252Chris Lattner    // type.
2458c7ef46254339450de33125f712c7b99afaf4fbcdDavid Chisnall    SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy);
24599f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    SelectorAliases[i]->replaceAllUsesWith(SelPtr);
24609f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    SelectorAliases[i]->eraseFromParent();
246120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  }
24629f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
246320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Number of classes defined.
24641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
246520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        Classes.size()));
246620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Number of categories defined
24671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
246820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov        Categories.size()));
246920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create an array of classes, then categories, then static object instances
247020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Classes.insert(Classes.end(), Categories.begin(), Categories.end());
247120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  //  NULL-terminated list of static object instances (mainly constant strings)
247220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Classes.push_back(Statics);
247320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Classes.push_back(NULLPtr);
24747db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
247520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(ClassList);
24761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Construct the symbol table
247720ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
247820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
247920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // The symbol table is contained in a module which has some version-checking
248020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // constants
24817650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
2482a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall      PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy),
24836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      (RuntimeVersion >= 10) ? IntTy : nullptr, nullptr);
248420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.clear();
24859f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // Runtime version, used for ABI compatibility checking.
24869f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
248791a0b51438e5259a79d68d7450c453827beed9e2Fariborz Jahanian  // sizeof(ModuleTy)
248825a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow  llvm::DataLayout td(&TheModule);
2489e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck  Elements.push_back(
2490e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck    llvm::ConstantInt::get(LongTy,
2491e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck                           td.getTypeSizeInBits(ModuleTy) /
2492e0afc892e75f526f7ecce7dd152d946337632efeKen Dyck                             CGM.getContext().getCharWidth()));
24939f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall
24949f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  // The path to the source file where this module was declared
24959f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  SourceManager &SM = CGM.getContext().getSourceManager();
24969f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID());
24979f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  std::string path =
24989f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    std::string(mainFile->getDir()->getName()) + '/' + mainFile->getName();
24999f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  Elements.push_back(MakeConstantString(path, ".objc_source_file_name"));
250020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Elements.push_back(SymTab);
2501a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall
2502f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall  if (RuntimeVersion >= 10)
25034e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    switch (CGM.getLangOpts().getGC()) {
2504f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall      case LangOptions::GCOnly:
2505a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall        Elements.push_back(llvm::ConstantInt::get(IntTy, 2));
2506a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall        break;
2507f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall      case LangOptions::NonGC:
25084e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        if (CGM.getLangOpts().ObjCAutoRefCount)
2509f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall          Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2510f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall        else
2511f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall          Elements.push_back(llvm::ConstantInt::get(IntTy, 0));
2512f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall        break;
2513f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall      case LangOptions::HybridGC:
2514f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall          Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2515f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall        break;
2516f074885425af40e5e03569371e453d9f7ae43aa6David Chisnall    }
2517a2120039ed08a5d5d6096e2560fd32c6128e951aDavid Chisnall
251820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
251920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
252020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // Create the load function calling the runtime entry point with the module
252120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  // structure
252220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  llvm::Function * LoadFunction = llvm::Function::Create(
25230032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
252420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      llvm::GlobalValue::InternalLinkage, ".objc_load_function",
252520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      &TheModule);
25260032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::BasicBlock *EntryBB =
25270032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
2528a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson  CGBuilderTy Builder(VMContext);
252920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Builder.SetInsertPoint(EntryBB);
253026c8294dacc2efc83d8d560593ff863e7a32a48fFariborz Jahanian
253195d318c4c10437db40ca6e15fdf32e04012da58eBenjamin Kramer  llvm::FunctionType *FT =
2532da549e8995c447542d5631b8b67fcc3a9582797aJay Foad    llvm::FunctionType::get(Builder.getVoidTy(),
2533da549e8995c447542d5631b8b67fcc3a9582797aJay Foad                            llvm::PointerType::getUnqual(ModuleTy), true);
253495d318c4c10437db40ca6e15fdf32e04012da58eBenjamin Kramer  llvm::Value *Register = CGM.CreateRuntimeFunction(FT, "__objc_exec_class");
253520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Builder.CreateCall(Register, Module);
253629254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall
2537dccaa239ec125926a9c1fb5f328bce349e41637bDavid Chisnall  if (!ClassAliases.empty()) {
253829254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    llvm::Type *ArgTypes[2] = {PtrTy, PtrToInt8Ty};
253929254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    llvm::FunctionType *RegisterAliasTy =
254029254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall      llvm::FunctionType::get(Builder.getVoidTy(),
254129254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall                              ArgTypes, false);
254229254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    llvm::Function *RegisterAlias = llvm::Function::Create(
254329254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall      RegisterAliasTy,
254429254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall      llvm::GlobalValue::ExternalWeakLinkage, "class_registerAlias_np",
254529254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall      &TheModule);
254629254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    llvm::BasicBlock *AliasBB =
254729254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall      llvm::BasicBlock::Create(VMContext, "alias", LoadFunction);
254829254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    llvm::BasicBlock *NoAliasBB =
254929254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall      llvm::BasicBlock::Create(VMContext, "no_alias", LoadFunction);
255029254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall
255129254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    // Branch based on whether the runtime provided class_registerAlias_np()
255229254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    llvm::Value *HasRegisterAlias = Builder.CreateICmpNE(RegisterAlias,
255329254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall            llvm::Constant::getNullValue(RegisterAlias->getType()));
255429254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    Builder.CreateCondBr(HasRegisterAlias, AliasBB, NoAliasBB);
255529254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall
2556651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // The true branch (has alias registration function):
255729254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    Builder.SetInsertPoint(AliasBB);
255829254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    // Emit alias registration calls:
255929254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    for (std::vector<ClassAliasPair>::iterator iter = ClassAliases.begin();
256029254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall       iter != ClassAliases.end(); ++iter) {
256129254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall       llvm::Constant *TheClass =
256229254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall         TheModule.getGlobalVariable(("_OBJC_CLASS_" + iter->first).c_str(),
256329254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall            true);
25646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines       if (TheClass) {
256529254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall         TheClass = llvm::ConstantExpr::getBitCast(TheClass, PtrTy);
256629254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall         Builder.CreateCall2(RegisterAlias, TheClass,
256729254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall            MakeConstantString(iter->second));
256829254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall       }
256929254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    }
257029254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    // Jump to end:
257129254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    Builder.CreateBr(NoAliasBB);
257229254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall
257329254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    // Missing alias registration function, just return from the function:
257429254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall    Builder.SetInsertPoint(NoAliasBB);
257529254f4dd114fe2dd5c4e7a261ebea941973ad3dDavid Chisnall  }
257620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  Builder.CreateRetVoid();
25779cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian
257820ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  return LoadFunction;
257920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov}
25807ded7f4983dc4a20561db7a8d02c6b2435030961Daniel Dunbar
2581679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanianllvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
25821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          const ObjCContainerDecl *CD) {
25831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ObjCCategoryImplDecl *OCD =
25843e0a540b6d846178857289ec0eb8470a278d11a3Steve Naroff    dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
25855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef CategoryName = OCD ? OCD->getName() : "";
25865f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef ClassName = CD->getName();
25879f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  Selector MethodName = OMD->getSelector();
2588f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  bool isClassMethod = !OMD->isInstanceMethod();
2589391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
2590541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  CodeGenTypes &Types = CGM.getTypes();
25912acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *MethodTy =
2592de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
259320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov  std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
259420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov      MethodName, isClassMethod);
259520ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov
2596d6c93d703541c992e06eb9a59a2d826a30da65b2Daniel Dunbar  llvm::Function *Method
25971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = llvm::Function::Create(MethodTy,
25981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             llvm::GlobalValue::InternalLinkage,
25991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             FunctionName,
26001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             &TheModule);
2601391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  return Method;
2602391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner}
2603391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner
2604789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnallllvm::Constant *CGObjCGNU::GetPropertyGetFunction() {
26059f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return GetPropertyFn;
260649f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar}
260749f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar
2608789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnallllvm::Constant *CGObjCGNU::GetPropertySetFunction() {
26099f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return SetPropertyFn;
261049f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar}
261149f6602707887eea1a558a1dffe0213102f887f2Daniel Dunbar
2612ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Constant *CGObjCGNU::GetOptimizedPropertySetFunction(bool atomic,
2613ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                                           bool copy) {
26146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
2615ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
2616ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2617789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnallllvm::Constant *CGObjCGNU::GetGetStructFunction() {
26189f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return GetStructPropertyFn;
26198fac25d33b13e25f512dd921d4d5a4b565f5d175David Chisnall}
2620789ecdebd04aebdb2d6b801ebe8e04a189ebc023David Chisnallllvm::Constant *CGObjCGNU::GetSetStructFunction() {
26219f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return SetStructPropertyFn;
26226cc590602f41c3e98e8af0023d54296c8eca7910Fariborz Jahanian}
2623d397cfef01d49a41554309d67ea26340c39e1e94David Chisnallllvm::Constant *CGObjCGNU::GetCppAtomicObjectGetFunction() {
26246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
2625d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall}
2626d397cfef01d49a41554309d67ea26340c39e1e94David Chisnallllvm::Constant *CGObjCGNU::GetCppAtomicObjectSetFunction() {
26276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
2628e3173021fa3bfdf7e6759d67838e385a83b2d57eFariborz Jahanian}
26296cc590602f41c3e98e8af0023d54296c8eca7910Fariborz Jahanian
2630309a4368cd299cff30ab22709e2b772cf8059e45Daniel Dunbarllvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
26319f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall  return EnumerationMutationFn;
26322abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson}
26332abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson
26349f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF,
2635f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                     const ObjCAtSynchronizedStmt &S) {
26369735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn);
2637f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
26385dc0867af17b3bd6f567897433853b2b4767446cChris Lattner
26395dc0867af17b3bd6f567897433853b2b4767446cChris Lattner
26409f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF,
2641f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                            const ObjCAtTryStmt &S) {
2642f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // Unlike the Apple non-fragile runtimes, which also uses
2643f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // unwind-based zero cost exceptions, the GNU Objective C runtime's
2644f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // EH support isn't a veneer over C++ EH.  Instead, exception
2645c686004145b1f4dbeb38173a0886ba7040ae0089David Chisnall  // objects are created by objc_exception_throw and destroyed by
2646f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // the personality function; this avoids the need for bracketing
2647f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // catch handlers with calls to __blah_begin_catch/__blah_end_catch
2648f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // (or even _Unwind_DeleteException), but probably doesn't
2649f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // interoperate very well with foreign exceptions.
26509735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  //
265180558d2db10a96eada4d8fb79949b3263b44652bDavid Chisnall  // In Objective-C++ mode, we actually emit something equivalent to the C++
26529735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  // exception handler.
26539735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn);
26549735ca6e01113aec6a825dd5b1cb1b98fb377d31David Chisnall  return ;
265564d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
265664d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
26579f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
26586a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian                              const ObjCAtThrowStmt &S,
26596a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian                              bool ClearInsertionPoint) {
26605dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  llvm::Value *ExceptionAsObject;
26615dc0867af17b3bd6f567897433853b2b4767446cChris Lattner
26625dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  if (const Expr *ThrowExpr = S.getThrowExpr()) {
26632b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall    llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
26641e64a9531bf6e9d99ad3271b3a735ef702689b06Fariborz Jahanian    ExceptionAsObject = Exception;
26655dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  } else {
26661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
26675dc0867af17b3bd6f567897433853b2b4767446cChris Lattner           "Unexpected rethrow outside @catch block.");
26685dc0867af17b3bd6f567897433853b2b4767446cChris Lattner    ExceptionAsObject = CGF.ObjCEHValueStack.back();
26695dc0867af17b3bd6f567897433853b2b4767446cChris Lattner  }
2670578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer  ExceptionAsObject = CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy);
2671c686004145b1f4dbeb38173a0886ba7040ae0089David Chisnall  llvm::CallSite Throw =
2672bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      CGF.EmitRuntimeCallOrInvoke(ExceptionThrowFn, ExceptionAsObject);
2673c686004145b1f4dbeb38173a0886ba7040ae0089David Chisnall  Throw.setDoesNotReturn();
2674c972c92f757957bf414c6195b1f2ae25a2b4dd37Eli Friedman  CGF.Builder.CreateUnreachable();
26756a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian  if (ClearInsertionPoint)
26766a3c70ec59804347c99e96aa13934f63f4fed573Fariborz Jahanian    CGF.Builder.ClearInsertionPoint();
267764d5d6c5903157c521af496479d06dc26032d718Anders Carlsson}
267864d5d6c5903157c521af496479d06dc26032d718Anders Carlsson
26799f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallllvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF,
26801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          llvm::Value *AddrWeakObj) {
2681bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  CGBuilderTy &B = CGF.Builder;
268231fc0c1a16dabd397dd3323fb0dbe7052b87abd0David Chisnall  AddrWeakObj = EnforceType(B, AddrWeakObj, PtrToIdTy);
2683ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  return B.CreateCall(WeakReadFn, AddrWeakObj);
26846dc2317b59cb1180a59f6c283d96b7a5dfeb5307Fariborz Jahanian}
26856dc2317b59cb1180a59f6c283d96b7a5dfeb5307Fariborz Jahanian
26869f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF,
26871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                   llvm::Value *src, llvm::Value *dst) {
2688bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  CGBuilderTy &B = CGF.Builder;
2689ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  src = EnforceType(B, src, IdTy);
2690ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  dst = EnforceType(B, dst, PtrToIdTy);
2691ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  B.CreateCall2(WeakAssignFn, src, dst);
26923e283e344595e0bd499b13b30a92b7d9c10a2140Fariborz Jahanian}
26933e283e344595e0bd499b13b30a92b7d9c10a2140Fariborz Jahanian
26949f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF,
2695021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian                                     llvm::Value *src, llvm::Value *dst,
2696021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian                                     bool threadlocal) {
2697bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  CGBuilderTy &B = CGF.Builder;
2698ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  src = EnforceType(B, src, IdTy);
2699ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  dst = EnforceType(B, dst, PtrToIdTy);
2700021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian  if (!threadlocal)
2701021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian    B.CreateCall2(GlobalAssignFn, src, dst);
2702021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian  else
2703021a7a63984f0f912dc9e9dae2a1b3e1509a40ceFariborz Jahanian    // FIXME. Add threadloca assign API
2704b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("EmitObjCGlobalAssign - Threal Local API NYI");
270558626500527695865683d1d65053743de8770b60Fariborz Jahanian}
270658626500527695865683d1d65053743de8770b60Fariborz Jahanian
27079f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF,
27086c7a1f364796ce1acb988714e9e42076d1ce332eFariborz Jahanian                                   llvm::Value *src, llvm::Value *dst,
27096c7a1f364796ce1acb988714e9e42076d1ce332eFariborz Jahanian                                   llvm::Value *ivarOffset) {
2710bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  CGBuilderTy &B = CGF.Builder;
2711ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  src = EnforceType(B, src, IdTy);
2712b44eda31a214ad88616acbe28c3f0a70268adc6cDavid Chisnall  dst = EnforceType(B, dst, IdTy);
2713ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  B.CreateCall3(IvarAssignFn, src, dst, ivarOffset);
27147eda8367cf63caee8acf907356b1d199ccaa6e89Fariborz Jahanian}
27157eda8367cf63caee8acf907356b1d199ccaa6e89Fariborz Jahanian
27169f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF,
27171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                         llvm::Value *src, llvm::Value *dst) {
2718bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  CGBuilderTy &B = CGF.Builder;
2719ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  src = EnforceType(B, src, IdTy);
2720ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  dst = EnforceType(B, dst, PtrToIdTy);
2721ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall  B.CreateCall2(StrongCastAssignFn, src, dst);
272258626500527695865683d1d65053743de8770b60Fariborz Jahanian}
272358626500527695865683d1d65053743de8770b60Fariborz Jahanian
27249f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallvoid CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF,
27251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                         llvm::Value *DestPtr,
2726082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian                                         llvm::Value *SrcPtr,
272755bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian                                         llvm::Value *Size) {
2728bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  CGBuilderTy &B = CGF.Builder;
272968e5e1331d99140ae5dd7b27c3dd28618827dfc8David Chisnall  DestPtr = EnforceType(B, DestPtr, PtrTy);
273068e5e1331d99140ae5dd7b27c3dd28618827dfc8David Chisnall  SrcPtr = EnforceType(B, SrcPtr, PtrTy);
2731ef6e0f3218b64d8ea3b8b57ee657fec8da228a43David Chisnall
273255bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian  B.CreateCall3(MemMoveFn, DestPtr, SrcPtr, Size);
2733082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian}
2734082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian
27359cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanianllvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
27369cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian                              const ObjCInterfaceDecl *ID,
27379cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian                              const ObjCIvarDecl *Ivar) {
27389cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
27399cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    + '.' + Ivar->getNameAsString();
27409cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // Emit the variable and initialize it with what we think the correct value
27419cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // is.  This allows code compiled with non-fragile ivars to work correctly
27429cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // when linked against code which isn't (most of the time).
27438a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
27448a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  if (!IvarOffsetPointer) {
2745e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // This will cause a run-time crash if we accidentally use it.  A value of
2746e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // 0 would seem more sensible, but will silently overwrite the isa pointer
2747e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // causing a great deal of confusion.
2748e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    uint64_t Offset = -1;
2749e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // We can't call ComputeIvarBaseOffset() here if we have the
2750e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // implementation, because it will create an invalid ASTRecordLayout object
2751e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // that we are then stuck with forever, so we only initialize the ivar
2752e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // offset variable with a guess if we only have the interface.  The
2753e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // initializer will be reset later anyway, when we are generating the class
2754e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    // description.
2755e0d987626cc11317b36d6d3cc148c9a9d4b35850David Chisnall    if (!CGM.getContext().getObjCImplementation(
2756cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman              const_cast<ObjCInterfaceDecl *>(ID)))
2757e5b4666d9b86ea30b90b599691e75d380f84ddd6Eli Friedman      Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
2758d901da531433254210a08e8b1f9e1ad049b340aaDavid Chisnall
275949de5289acea91c9d224c9aadd4d527e8f9fdaffDavid Chisnall    llvm::ConstantInt *OffsetGuess = llvm::ConstantInt::get(Int32Ty, Offset,
2760243f108a7c5dc4733e9ef6b7b7c2cd0465cf2104Richard Trieu                             /*isSigned*/true);
27618a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    // Don't emit the guess in non-PIC code because the linker will not be able
27628a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    // to replace it with the real version for a library.  In non-PIC code you
27638a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    // must compile with the fragile ABI if you want to use ivars from a
27641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // GCC-compiled class.
27655e219cf7f896873c1c1e64b9e87a7dade99debbaChandler Carruth    if (CGM.getLangOpts().PICLevel || CGM.getLangOpts().PIELevel) {
27668a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
2767917b28b980a8300ce9c7c8d71b2a77bb93b7cdd2David Chisnall            Int32Ty, false,
27688a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall            llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
27698a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
27708a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall            IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
27718a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall            IvarOffsetGV, Name);
27728a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    } else {
27738a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall      IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
27743c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer              llvm::Type::getInt32PtrTy(VMContext), false,
27756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              llvm::GlobalValue::ExternalLinkage, nullptr, Name);
27768a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall    }
27779cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  }
27788a5a9aaddb627c0884c2ed8db55cc29fdb601195David Chisnall  return IvarOffsetPointer;
27799cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian}
27809cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian
27819f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallLValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF,
2782598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                       QualType ObjectTy,
2783598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                       llvm::Value *BaseValue,
2784598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                       const ObjCIvarDecl *Ivar,
2785598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                       unsigned CVRQualifiers) {
2786c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl *ID =
2787c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjectTy->getAs<ObjCObjectType>()->getInterface();
27889777687562c338601c2f17906e65e1c1a0aad96fDaniel Dunbar  return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
27899777687562c338601c2f17906e65e1c1a0aad96fDaniel Dunbar                                  EmitIvarOffset(CGF, ID, Ivar));
27900bb20361a321593887f067515dd04cf109f4c74aFariborz Jahanian}
2791bb1c8600218b3244340331165fc7cba7bf227655Mike Stump
27929cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanianstatic const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
27939cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian                                                  const ObjCInterfaceDecl *OID,
27949cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian                                                  const ObjCIvarDecl *OIVD) {
2795db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose  for (const ObjCIvarDecl *next = OID->all_declared_ivar_begin(); next;
2796db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose       next = next->getNextIvar()) {
2797db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    if (OIVD == next)
27989cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian      return OID;
27999cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  }
28001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28019cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  // Otherwise check in the super class.
28029cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
28039cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    return FindIvarInterface(Context, Super, OIVD);
28041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
28069cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian}
28070bb20361a321593887f067515dd04cf109f4c74aFariborz Jahanian
28089f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallllvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF,
28092a03192a02dbf4fdff438d1e658356bde871aba4Daniel Dunbar                         const ObjCInterfaceDecl *Interface,
2810f63aa3fd429cdb9145d78f0b656bc78754efedb9Fariborz Jahanian                         const ObjCIvarDecl *Ivar) {
2811260611a32535c851237926bfcf78869b13c07d5bJohn McCall  if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
28129cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian    Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
281363ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall    if (RuntimeVersion < 10)
281463ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      return CGF.Builder.CreateZExtOrBitCast(
281563ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall          CGF.Builder.CreateLoad(CGF.Builder.CreateLoad(
281663ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall                  ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar")),
281763ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall          PtrDiffTy);
281863ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall    std::string name = "__objc_ivar_offset_value_" +
281963ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      Interface->getNameAsString() +"." + Ivar->getNameAsString();
282063ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall    llvm::Value *Offset = TheModule.getGlobalVariable(name);
282163ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall    if (!Offset)
282263ff703393543904046462aee2ac0a53b8937a3eDavid Chisnall      Offset = new llvm::GlobalVariable(TheModule, IntTy,
28233fc81d32314eca98b51846f493a8bc77cf14a701David Chisnall          false, llvm::GlobalValue::LinkOnceAnyLinkage,
28243fc81d32314eca98b51846f493a8bc77cf14a701David Chisnall          llvm::Constant::getNullValue(IntTy), name);
28256614845b4f6724da8c6aca14ea720dbd157883a7David Chisnall    Offset = CGF.Builder.CreateLoad(Offset);
28266614845b4f6724da8c6aca14ea720dbd157883a7David Chisnall    if (Offset->getType() != PtrDiffTy)
28276614845b4f6724da8c6aca14ea720dbd157883a7David Chisnall      Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy);
28286614845b4f6724da8c6aca14ea720dbd157883a7David Chisnall    return Offset;
28299cd96ff7dfcb51fdba03df8803fff6cc36e9633fFariborz Jahanian  }
2830e5b4666d9b86ea30b90b599691e75d380f84ddd6Eli Friedman  uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
2831e5b4666d9b86ea30b90b599691e75d380f84ddd6Eli Friedman  return llvm::ConstantInt::get(PtrDiffTy, Offset, /*isSigned*/true);
2832f63aa3fd429cdb9145d78f0b656bc78754efedb9Fariborz Jahanian}
2833f63aa3fd429cdb9145d78f0b656bc78754efedb9Fariborz Jahanian
28349f6614e3c5d34998dd5a5cc6faf0746dcd611d49David ChisnallCGObjCRuntime *
28359f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnallclang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) {
2836260611a32535c851237926bfcf78869b13c07d5bJohn McCall  switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
283711d3f4cc27e6b923fc32481dc1bb5ec46c7d1f4bDavid Chisnall  case ObjCRuntime::GNUstep:
28389f6614e3c5d34998dd5a5cc6faf0746dcd611d49David Chisnall    return new CGObjCGNUstep(CGM);
2839260611a32535c851237926bfcf78869b13c07d5bJohn McCall
284011d3f4cc27e6b923fc32481dc1bb5ec46c7d1f4bDavid Chisnall  case ObjCRuntime::GCC:
2841260611a32535c851237926bfcf78869b13c07d5bJohn McCall    return new CGObjCGCC(CGM);
2842260611a32535c851237926bfcf78869b13c07d5bJohn McCall
2843f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall  case ObjCRuntime::ObjFW:
2844f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall    return new CGObjCObjFW(CGM);
2845f7226fbe677a9c7578fa0613491ed15c6dc6a5e1John McCall
2846260611a32535c851237926bfcf78869b13c07d5bJohn McCall  case ObjCRuntime::FragileMacOSX:
2847260611a32535c851237926bfcf78869b13c07d5bJohn McCall  case ObjCRuntime::MacOSX:
2848260611a32535c851237926bfcf78869b13c07d5bJohn McCall  case ObjCRuntime::iOS:
2849260611a32535c851237926bfcf78869b13c07d5bJohn McCall    llvm_unreachable("these runtimes are not GNU runtimes");
2850260611a32535c851237926bfcf78869b13c07d5bJohn McCall  }
2851260611a32535c851237926bfcf78869b13c07d5bJohn McCall  llvm_unreachable("bad runtime");
28520f984268b05edab2cc555a427c441baa9c252658Chris Lattner}
2853